[prog] check if script parent process is "init"

dominik schramm dominik.schramm at gmxpro.net
Sat Apr 17 11:10:01 EST 2004


Hi list, and hi John in particular,

I can't let go, sorry...

Thanks for all your remarks first.
What struck me is that, although John's comments are very clear and 
logical, the approach sketched below does work the way I wanted it,
I only tried this at work on SuSE Linux (7.3, to be precise), though.

John Clarke <johnc+linuxchix at kirriwa.net> writes:

>> The scripts themselves can then tell by the name they were called 
>> with if they are run automatically or manually. 
> 
> No, they can't.  If they're called as /etc/init.d/rc?.d/[SK]* then
> they're *probably* being run by init, but you can't guarantee that.
> What's to stop me running them manually by that name? 
> Nothing at all.

The reason why I wanted this is that some daemons have to be started 
automatically when the OS boots up, but init scripts are not provided.
Because some daemons require a password on the startup command line,
I wanted to ensure that the scripts used during init cannot be 
accidentally used like "/etc/init.d/mydaemon start".
If the administrator intentionally bypasses this safety check by using
"/etc/rc3.d/S96mydaemon start" -- well, that's his/her decision.
No one can prevent a careless admin to 
  echo 'root password is "topsecret"' | wall

The method I'm using to determine if the script is being run by init
right now relies on the following check:

# quiet, inverse matching
if grep -q -v "^Ppid:[^0-9]*1$" /proc/$$/status
then
        echo must be run by init
        exit 1
fi

> [...] In the SysVInit scripts, init runs /etc/rc.d/rc which in turn
> runs the various startup scripts. [...] 
> /etc/rc.d/rc contains essentially this loop (cutting out all the 
> irrelevant stuff):
> 
>     # Now run the START scripts.
>     for i in /etc/rc$runlevel.d/S*; do
>         $i start
>     done

Debian actually has this in this loop:
   startup $i start

with "startup" being a function containing:
   $debug "$@"

As I said, I haven't checked the aforementioned approach with Debian.
I'd say, it really won't work.

> So when the scripts are run, they'll have $PPID set to the pid of 
> the shell which is running /etc/rc.d/rc.

Unfortunately I don't have a SuSE system at home, but is it possible 
that SuSE *sources* the init script? Like this:

for i in S*; do
   . $i start
done

Then the parent process would in fact be init and the PPID would be 1.

It must be something like this, because on SuSE the grep from above
does work: on system startup I can see the daemons being started.
But when I manually run "/etc/init.d/mydaemon start", I get the 
error "must be run by init".

On the other hand, this would mean that init scripts can only be 
Bash scripts if rc itself is a Bash script (am I right?), which
in Debian it isn't.

Could anyone using SuSE check?
 
regards,
dominik




More information about the Programming mailing list