Subject: Re: xdm and other daemon problems
To: Ian Goldby <Ian.Goldby@mail.com>
From: Matthias Buelow <mkb@mukappabeta.de>
List: netbsd-help
Date: 03/28/2001 00:05:05
[I have set Cc: to netbsd-help since although this was private
mail, the discussion originated there and I think the answer is of
general interest.]

Ian Goldby <Ian.Goldby@mail.com> writes:

>>>It turned out that my problem with daemons not starting properly was
>>>because I had previously replaced /etc/sh with a link to /etc/ksh so
>>

(I replied)
>>odd, since ksh is supposed to be upwards-compatible with sh..
>>it would be nice if you could identify the problems when running
>>certain sh-scripts with ksh (I mean, after all, the NetBSD startup
>>scripts aren't particularly complicated or twisted sh scripts which
>>could trigger some compatibility problems in other shells...)

Ian replied:
>I've had a look at this, but didn't get very far. I did the following
>experiments:
>
>1) mv /bin/sh /bin/sh.old; ln -s /bin/ksh /bin/sh
>Rebooted machine
>Result: The messages Starting xdm. Starting inetd. Starting cron. all
>come up, followed by the date, but then nothing else happens where
>previously I would have gone into X. (I also didn't get the tty login

Well, I have established this situation myself here (NetBSD's pdksh
as /bin/sh) and I noticed that not only did xdm fail to get started
but all other daemons invoked via run_rc_command() in /etc/rc.subr
were not started either.  No error is printed, though, and it fails
because of the following:

Both /bin/sh and the script are correct and the culprit is pdksh.
The problem manifests itself in /etc/rc.subr, run_rc_command(),
where the variable _doit is assigned the command line string to be
run (there are two alternatives, depending on whether _chroot is
set but both expose the same problem with (pd)ksh):

                                _doit="\  
${_user:+su -m $_user -c 'sh -c \"}\
${_chdir:+cd $_chdir; }\
${_nice:+nice -n $_nice }\
$command $_flags $command_args\ 
${_user:+\"'}"                          

Notice the single quote "'" in the ${:+} expansion.  It should be
treated literally, as long as the parameter substitution is inside
double-quotes, which it is.  However, pdksh seems to get confused
here and the result is that _doit comes out empty and thus nothing
gets run.

The problem can be simulated very easily on the command line
(variable a is unset):

sh$ echo "${a:-'}"
'
sh$
pdksh$ echo "${a:-'}"
> 
> ^C
pdksh$ 

Notice that the behaviour of "pdksh" here is the same as if the
expansion were not enclosed in double quotes!  The above behaviour
of "sh" is what I expect after having consulted [BK95] and what
all of the other shells I tried it on exhibited, which are: AT&T
ksh88 (HP-UX), ksh93 (NetBSD), SysV Bourne sh (HP-UX) and /bin/sh
(NetBSD).

So the answer is, no, you currently cannot use NetBSD's /bin/ksh
(which is the PD KSH) as a substitute for /bin/sh because the
startup scripts will trigger a bug in it and fail.  I'll now file
a pr on ksh and hopefully this'll get fixed.

mkb

[BK95]	Morris I. Bolsky, David G. Korn, "The New KornShell
	Command and Programming Language," Prentice-Hall PTR,
	New Jersey, 1995.