etc/rc.d/{var,tmp} and sub-shell usage?!

Oliver Fromme olli at lurza.secnetix.de
Wed May 23 09:12:02 UTC 2007


Ralf S. Engelschall wrote:
 > I've just stumbled over a confusing sub-shell usage in our
 > src/etc/rc.d/{var,tmp} scripts where I'm sure the sub-shells are totally
 > unnecessary and useless. I also do not see any difference under run-time
 > except that the sub-shell usage is slower, of course ;-)
 > 
 > Nevertheless, I'm a little bit curious whether someone else sees _ANY_
 > reason to keep those sub-shell constructs? If nobody has any objections
 > I would just cleanup these two scripts by removing the sub-shell
 > constructs...
 > [...]
 >  [Nn][Oo])
 >         ;;
 >  *)
 > -       if (/bin/mkdir -p /tmp/.diskless 2> /dev/null); then
 > +       if /bin/mkdir -p /tmp/.diskless 2> /dev/null; then
 >                 rmdir /tmp/.diskless
 >         else
 >                 if [ -h /tmp ]; then

Additionally, I think it's not a good idea to use
"mkdir -p" to check if a directory is writable.
If the directory already exists (for whatever
reason), "mkdir -p" succeeds even if the file
system is not writable.

Normally you would use touch(1), but the problem
is that touch is in /usr, so it might not be
available in single-user mode (which is probably
the reason why the original author used mkdir in
the first place).

The best solution is probably to use /bin/ln, and
include the PID in the name to reduce the risk of
accidental file name collisions.  (Note that this
code is running before the system is multi-user,
so writing to /tmp as root doesn't introduce a
security issue here, as far as I can tell.)

test_file=/tmp/.diskless.$$
if /bin/ln -sf foo $test_file 2>/dev/null; then
        rm $test_file
else
        [...]
fi

Well ...  Thinking about it, there's a good chance
that the PID is always the same during the boot
sequence of scripts (some low number anyway), so
maybe something like $(/bin/date +%s) should be
used in the name of the test file instead od the
PID.  But maybe that's just overkill.

Best regards
   Oliver

PS:  I also noticed that there's really a lot of
redundant (i.e. superfluous) use of braces "${}"
for variable expansion in the scripts, which makes
them more difficult to read (IMHO).  Is there some
style guideline that requires it?  Just wondering ...

-- 
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

One Unix to rule them all, One Resolver to find them,
One IP to bring them all and in the zone to bind them.


More information about the freebsd-current mailing list