/etc/init.d/vz: get rid of confusing error from echo

Sometimes /etc/init.d/vz (re)start can spit this error:

  /etc/init.d/vz: line 200: echo: write error: Broken pipe

Here is the code around line 200:

  conf="`zcat /proc/config.gz 2>/dev/null | grep -E -v '^#|^$'`"

  for opt in $opt_must; do
          if ! echo "${conf}" | grep -q "${opt}="; then   # <-- LINE 200

The problem here is grep -q exits immediately when it finds the first match,
so sending side of the pipe receives SIGPIPE. If that would be say /bin/echo
then it would exit too and everything will be OK. But since built-in echo
from bash is used it is bash who is receiving SIGPIPE.

Apparently bash can't realise that this SIGPIPE refers to echo and thus echo
should stop trying to write to the pipe. So, echo calls another write()
and gets EPIPE. This is the error bash reports about.

Possible solutions are:
(1) Get rid of $conf variable, instead run zcat a few times.
    zcat is a program (not built-in) and it will react to SIGPIPE
    the way we expect (i.e. exit).
(2) Use /bin/echo program instead of built-in. /bin/echo will also react
    to SIGPIPE in a correct way.
(3) Just redirect echo stderr to /dev/null, suppressing the 'write failed'
    error which in this very case is perfectly normal.

I do not like solution (1) since it slows down the process.
Solution (2) is fine, but I like (3) even better.

This (3) is what this patch does. I do not foresee any negative side-effects
from redirecting echo's stderr to /dev/null.

http://bugzilla.openvz.org/show_bug.cgi?id=1042

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
1 file changed