reboot

Denis Vlasenko vda at ilport.com.ua
Mon Oct 17 07:41:25 UTC 2005


> If init isn't doing anything then you can move its functions elsewhere, yes.  
> (Heck, you don't _need_ to run init.  I have systems that run a shell script 
> as PID 1...)

I run a shell script on the system where I am now.

#!/bin/sh
unset HOSTNAME
...
unset TERM
PATH=/bin:/usr/bin
exec >/dev/console 2>&1 </dev/null
(
cd /etc/rc.d
env - PATH="$PATH" /etc/rc.d/bootprog=3.runlevel
)
# Close all descriptors
exec >&- 2>&- <&-
while true; do env - sleep 32000; done
 
> > > > After all, we do not mount devices by signaling init.
> > >
> > > No, init gets execed as the first process in the system and thus runs the
> > > init script.  There's nothing running _to_signal init, it's the first
> > > process.
> > >
> > > > We do not up network interfaces by signaling init.
> > >
> > > The ones brought up by the init script are kicked off by init.
> >
> > I meant "we do not do <some admin actions> by 'kill -<somesig> 1', why do
> > we do this particular admin action (reboot) in this bizarre way?".
> 
> A) historical reasons.
> 
> B) Telling init to switch runlevels is, on some systems, done by sending it 
> the "usr1" signal or some such.  (Don't remember the details, it's been a 
> while.)
> 
> > > > We can kill all processes, remount ro and reboot without signaling
> > > > init.
> > >
> > > And on a system where init is set to respawn daemons, every time you kill
> > > one init will start it up again, and you'll be involved in an endless
> > > game of whack-a-mole.  (Or you'll shutdown with daemons starting up,
> > > doing who knows what to the filesystem state...)
> >
> > This is because inits which respawn daemons are a bad idea. They are trying
> > to perform two unrelated things at once: (1) to wait for orphaned
> > processes,
> 
> That's actually a side effect of its' intended functions.  Init is 30 years 
> old but reparent_to_init is about 5 years old.
> 
> > and (2) to respawn processes.
> 
> This was its original purpose.
> 
> > It's old System V way of doing thing, and it is _wrong_.
> 
> So it should _not_ respawn mingetty on /dev/tty* when you exit?

On my system supervise does it way better. Say, I can stop
getty respawning on a particular tty wuth single command:
svc -d /var/service/tty6.
How would you do it with init? By editing inittab and sending
some signal to init, right? Ain't that way more complicated?
 
> The rest of the daemon spawning behavior emerged out of the need to respawn 
> "login" sessions back when teletypes used rolls of paper...
> 
> > If you start a separate "daemon spawner" process at system start
> > and thus will split this functionality between two processes, init's code
> > gets much simpler:
> 
> You're describing inetd.

Sort of. I prefer svscan/supervise from daemontools.

> > 1) init does not need to respawn anything.
> 
> So you're suggesting that it:
> 
> A) bring the system up in the first place (which involves spawning stuff, and 
> there's no way to _avoid_ init doing this, it's sort of the core of its' 
> being).
> 
> B) Wait to receive notification of processes that exit.  (Including every 
> process it ever spawned, which has no other parent.)
> 
> C) Hope that some _other_ daemon does something about it?
>
> Ok, here's some questions for you:
> 
> 1) What does init do if the process spawning daemon gets whacked by the OOM 
> killer?  (Who spawned the process spawning daemon?  Only PID 1 has the 
> special code in the kernel to prevent things like the "kill" or "stop" 
> signals from ever being delivered to it...)

On my system:

  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME CPU COMMAND
 4264 root      16   0  2128 1164   792 R     2,9  0,4   0:00   0 top
 1922 root      14  -1  143M  77M  2304 S <   0,9 31,6   4:43   0 X
 2022 root      16   0 46584  28M 12796 S     0,9 11,4   1:15   0 firefox-bin
    1 root      16   0  1192  668   572 S     0,0  0,2   0:01   0 init
  717 root      17   0   428  200   160 S     0,0  0,0   0:00   0 sleep
...
  700 root      16   0   132   20     8 S     0,0  0,0   0:00   0 svscan

svscan is _tiny_ even compared to sleep. OOM killer won't pick it for kill.
(sleep is from coreutils-5.2.1 compiled against uclibc, svscan is compiled
with dietlibc)

> 2) Doesn't this new process spawning daemon have to be notified that the 
> system is shutting down now?  (If nothing else, but killing it first so it 
> isn't fighting you when you kill everything else?)

killall -15 is a perfect notification to every process, svscan included,
that it is time to clean up and exit. Reboot script does not need any special
knowledge about "daemon spawner". It just TERMs everybody. Isn't this logical?

> You're arguing against something people have spent 30 years making work.  They 
> do it that way for a reason.  Go make it work your way and then come back to 

sendmail is maybe 30 years old too. It doesn't make sendmail any better.

> us when you hit a tricky corner case having to do with process group 
> inheritance or console ownership some such piece of evil, and we'll tell you 
> how it was worked out in the existing code many years ago...

I feared I will hit something like this, but didn't. See the output
of 'ps -AH e' below sig. For example, after "supervise getty_tty2" you see
a login session with midnight commander. There's also a bunch
of getty's spawned. A httpd. A ntp. A gpm. A klog and syslog replacements.

As you can see, it's real desktop system, not a router.
 
> > 2) init does not need to know how to shutdown/reboot.
> >
> > In fact, both reboot and init can be implemented with shell scripts
> > (and they are on my system).
> 
> Sure.  Embedded systems have the advantage that they generally aren't doing 
> much, and are thus trivial to quiesce.

I started from some router boxes, but then migrated all other boxes
under my control, including desktops, to this method of spawning/controlling
processes as it turned out to be much simpler.

That "runlevels" nonsense is like a bad dream. I can selectively
stop/start _any_ subset of daemons with few svc commands,
not just subsets defined in inittab.

Let's imagine... hmm.. that I want to stop everything and go to singleuser
for filesystem repair.

# cd /var/service
# svc -d * */log; svc -u getty*

Everything will stop, then only getty's will restart.

> If you don't _need_ init, don't use it...

I am doing exactly this, and want to let people know that it actually
works rather nice. It's not a religious war, I am not trying
to "convert" you or everybody else.
--
vda

  PID TTY      STAT   TIME COMMAND
    1 ?        S      0:01 /bin/sh /init
    2 ?        SW     0:00   [migration/0]
    3 ?        SWN    0:00   [ksoftirqd/0]
    4 ?        SW<    0:00   [events/0]
    5 ?        SW<    0:00   [khelper]
    6 ?        SW<    0:00   [kthread]
    8 ?        SW<    0:00     [kacpid]
   80 ?        SW<    0:00     [kblockd/0]
  200 ?        SW     0:00     [pdflush]
  201 ?        SW     0:00     [pdflush]
  203 ?        SW<    0:00     [aio/0]
  444 ?        SW<    0:00     [ata/0]
  507 ?        SW<    0:00     [reiserfs/0]
 1077 ?        SW<    0:00     [rpciod/0]
  202 ?        SW     0:00   [kswapd0]
  204 ?        SW     0:00   [cifsoplockd]
  282 ?        SW     0:00   [kseriod]
  456 ?        SW     0:00   [kgameportd]
  538 ?        S<     0:00   udevd UDEV_LOG=debug
  592 ?        S      0:00   rpc.portmap
  700 ?        S      0:00   svscan /var/service PATH=/bin:/usr/bin
  718 ?        S      0:00     supervise fw PATH=/bin:/usr/bin
  719 ?        S      0:00     supervise gpm PATH=/bin:/usr/bin
 1660 ?        S      0:00       gpm -D -2 -m /dev/psaux -t ps2
  720 ?        S      0:00     supervise nfs PATH=/bin:/usr/bin
  741 ?        S      0:00       /bin/sh ./run PATH=/bin:/usr/bin
 1089 ?        S      0:00         sleep 32000
  721 ?        S      0:00     supervise ntp PATH=/bin:/usr/bin
  726 ?        SL     0:00       ntpd -D 0 -c conf -f drift -s stat -l /proc/self/fd/2 -p /dev/null -k /dev/null -g -d -n
  722 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  735 ?        S      0:00       multilog t /var/log/service/ntp
  723 ?        S      0:00     supervise smb PATH=/bin:/usr/bin
  724 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  729 ?        S      0:00       multilog t /var/log/service/smb
  725 ?        S      0:00     supervise top PATH=/bin:/usr/bin
  759 ?        S      0:34       top c s TERM=linux
  727 ?        S      0:00     supervise dhcp PATH=/bin:/usr/bin
  728 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  753 ?        S      0:00       multilog t /var/log/service/dhcp
  742 ?        S      0:00     supervise klog PATH=/bin:/usr/bin
  779 ?        S      0:00       socklog ucspi
  743 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  775 ?        S      0:00       svlogd -tt /var/log/service/klog
  744 ?        S      0:00     supervise once PATH=/bin:/usr/bin
  745 ?        S      0:00     supervise sshd PATH=/bin:/usr/bin
  746 ?        S      0:00       /usr/bin/sshd -D -e -p22 -u0
  782 ?        S      0:00     supervise r_zebra PATH=/bin:/usr/bin
  783 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  791 ?        S      0:00       multilog t /var/log/service/r_zebra
  784 ?        S      0:00     supervise httpd PATH=/bin:/usr/bin
  825 ?        S      0:00       tcpserver -v -R -H -l 0 -c 40 0.0.0.0 www setuidgid root httpd -X -f /.local/var/service/httpd/httpd.conf
  785 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  820 ?        S      0:00       multilog t /var/log/service/httpd
  786 ?        S      0:00     supervise pgsql PATH=/bin:/usr/bin
  787 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  816 ?        S      0:00       multilog t /var/log/service/pgsql
  804 ?        S      0:00     supervise smb_s PATH=/bin:/usr/bin
  805 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  834 ?        S      0:00       multilog t /var/log/service/smb_s
  806 ?        S      0:00     supervise nfs_mountd PATH=/bin:/usr/bin
  855 ?        S      0:00       rpc.mountd --foreground --debug all
  807 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  861 ?        S      0:00       multilog t /var/log/service/nfs_mountd
  812 ?        S      0:00     supervise getty_ttyM0 PATH=/bin:/usr/bin
  813 ?        S      0:00     supervise getty_ttyS0 PATH=/bin:/usr/bin
  814 ?        S      0:00     supervise nmeter PATH=/bin:/usr/bin
  881 ?        S      0:00       nmeter t c i x p b nif
  895 ?        S      0:00     supervise ovpn-1 PATH=/bin:/usr/bin
  896 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  903 ?        S      0:00       multilog /var/log/service/ovpn-1
  929 ?        S      0:00     supervise r_ospf PATH=/bin:/usr/bin
  930 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  937 ?        S      0:00       multilog /var/log/service/r_ospf
  931 ?        S      0:00     supervise getty_tty1 PATH=/bin:/usr/bin
  932 ?        S      0:00     supervise getty_tty2 PATH=/bin:/usr/bin
 1012 ?        S      0:00       login -- vda
 1780 tty2     S      0:00         -bash HOME=/home/vda PATH=/sbin:/bin:/usr/sbin:/usr/bin SHELL=/bin/bash TERM=linux MAIL=/var/mail/vda LOGNAME=vda
 1906 tty2     S      0:00           mc MANPATH=/usr/man TERM=linux SHELL=/bin/bash GROFF_NO_SGR= PAGER=most MAIL=/home/vda/maildir PATH=/home/vda/bin:/bin:/usr/bin PWD=/home/vda
 1907 ?        S      0:00             cons.saver /dev/tty2 MANPATH=/usr/man TERM=linux SHELL=/bin/bash GROFF_NO_SGR= PAGER=most MAIL=/home/vda/maildir PATH=/home/vda/bin:/bin:/u
  933 ?        S      0:00     supervise getty_tty3 PATH=/bin:/usr/bin
 1005 tty3     S      0:00       agetty 38400 /dev/tty3 linux TERM=linux
  934 ?        S      0:00     supervise getty_tty4 PATH=/bin:/usr/bin
 1004 tty4     S      0:00       agetty 38400 /dev/tty4 linux TERM=linux
  935 ?        S      0:00     supervise getty_tty5 PATH=/bin:/usr/bin
 1003 tty5     S      0:00       agetty 38400 /dev/tty5 linux TERM=linux
  936 ?        S      0:00     supervise getty_tty6 PATH=/bin:/usr/bin
 1002 tty6     S      0:00       agetty 38400 /dev/tty6 linux TERM=linux
  944 ?        S      0:00     supervise getty_tty7 PATH=/bin:/usr/bin
  973 tty7     S      0:00       agetty 38400 /dev/tty7 linux TERM=linux
  945 ?        S      0:00     supervise getty_tty8 PATH=/bin:/usr/bin
  978 tty8     S      0:00       agetty 38400 /dev/tty8 linux TERM=linux
  946 ?        S      0:00     supervise syslog PATH=/bin:/usr/bin
  947 ?        S      0:00       socklog unix /dev/log PATH=/bin:/usr/bin PWD=/.local/var/service/syslog SHLVL=0 GID=50 UID=58
  948 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  949 ?        S      0:00       svlogd /var/log/service/syslog
  950 ?        S      0:00     supervise nfs_statd PATH=/bin:/usr/bin
  951 ?        S      0:00       rpc.statd -F -d
  952 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  962 ?        S      0:00       multilog t /var/log/service/nfs_statd
  953 ?        S      0:00     supervise automount PATH=/bin:/usr/bin
  996 ?        S      0:00       automount -f -s -v -d --timeout 15 /.local/mnt/auto program /root/bin/mapper.sh
  970 ?        S      0:00     supervise log PATH=/bin:/usr/bin
  983 ?        S      0:01       multilog t n5 /var/log/service/automount
  971 ?        S      0:00     supervise watcher PATH=/bin:/usr/bin
  972 ?        S      0:00     supervise udhcpd_eth0 PATH=/bin:/usr/bin
  717 ?        S      0:00   sleep 32000
 1071 ?        SW     0:00   [nfsd]
 1072 ?        SW     0:00   [nfsd]
 1073 ?        SW     0:00   [nfsd]
 1074 ?        SW     0:00   [nfsd]
 1076 ?        SW     0:00   [lockd]
 1289 ?        SW     0:00   [khubd]
 1493 ?        SW     0:00   [scsi_eh_0]
 1498 ?        SW     0:00   [usb-storage]
 1921 ?        S      0:00   xinit MANPATH=/usr/man SHELL=/bin/bash TERM=linux GROFF_NO_SGR= PAGER=most PATH=/home/vda/bin:/bin:/usr/bin MAIL=/home/vda/maildir PWD=/.share/root/$
 1922 ?        S<     1:51     X :0 MANPATH=/usr/man SHELL=/bin/bash TERM=linux GROFF_NO_SGR= PAGER=most PATH=/home/vda/bin:/bin:/usr/bin MAIL=/home/vda/maildir PWD=/.share/root$
 1941 ?        S      0:00     /bin/sh /usr/bin/startkde MANPATH=/usr/man TERM=linux SHELL=/bin/bash GROFF_NO_SGR= PAGER=most MAIL=/home/vda/maildir PATH=/home/vda/bin:/bin:/usr$
 1983 ?        S      0:00       kwrapper ksmserver MANPATH=/usr/man SHELL=/bin/bash TERM=linux GS_LIB=/home/vda/.fonts KDE_FULL_SESSION=true GROFF_NO_SGR= PAGER=most PATH=/home$
 1965 ?        S      0:00   kdeinit Running... .                                                                                                                                $
 1970 ?        S      0:00     klauncher [kdeinit] t]                                                                                                                            $
 1987 ?        S      0:02     kwin [kdeinit]                                                                                                                                    $
 1993 ?        S      0:00     kio_file [kdeinit] file /.local/tmp/ksocket-root/klauncherdWDBuc.slave-socket /.local/tmp/ksocket-root/kdesktopjeoV5b.slave-socket ] file /.local/$
 2003 ?        S      0:30     konsole [kdeinit]                                                                                                                                 $
 2005 pts/1    S      0:00       /bin/bash MANPATH=/usr/man SHELL=/bin/bash TERM=xterm GS_LIB=/home/vda/.fonts KDE_FULL_SESSION=true GROFF_NO_SGR= PAGER=most PATH=/home/vda/bin:$
 2675 pts/1    S      0:00         mc MANPATH=/usr/man KDE_MULTIHEAD=false TERM=xterm SHELL=/bin/bash GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/vda/.gtkrc-2.0:/.share/root/.kde/sha$
 3085 pts/1    D      0:00           mplayer 135241main_neutronstar4lunch-magic.mov_NASA%20WebV_Oct3.mpg MANPATH=/usr/man KDE_MULTIHEAD=false SHELL=/bin/bash TERM=xterm GTK2_RC_$
 2907 pts/2    S      0:00       /bin/bash MANPATH=/usr/man SHELL=/bin/bash TERM=xterm GS_LIB=/home/vda/.fonts KDE_FULL_SESSION=true GROFF_NO_SGR= PAGER=most PATH=/home/vda/bin:$
 2909 pts/2    S      0:00         ssh vda at 172.16.22.5 MANPATH=/usr/man KDE_MULTIHEAD=false SHELL=/bin/bash TERM=xterm GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/vda/.gtkrc-2.0:/.sh$
 2985 pts/3    S      0:00       /bin/bash MANPATH=/usr/man SHELL=/bin/bash TERM=xterm GS_LIB=/home/vda/.fonts KDE_FULL_SESSION=true GROFF_NO_SGR= PAGER=most PATH=/home/vda/bin:$
 3092 pts/3    S      0:00         /bin/sh /home/vda/bin/psahe MANPATH=/usr/man KDE_MULTIHEAD=false TERM=xterm SHELL=/bin/bash GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/vda/.gtkrc-$
 3093 pts/3    R      0:00           ps -AH e --width=500 MANPATH=/usr/man KDE_MULTIHEAD=false SHELL=/bin/bash TERM=xterm GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/vda/.gtkrc-2.0:/$
 3094 pts/3    R      0:00           most MANPATH=/usr/man KDE_MULTIHEAD=false SHELL=/bin/bash TERM=xterm GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/vda/.gtkrc-2.0:/.share/root/.kde$
 3026 pts/4    S      0:00       /bin/bash MANPATH=/usr/man SHELL=/bin/bash TERM=xterm GS_LIB=/home/vda/.fonts KDE_FULL_SESSION=true GROFF_NO_SGR= PAGER=most PATH=/home/vda/bin:$
 1968 ?        S      0:00   dcopserver [kdeinit] --nosid it] --nosid                                                                                                            $
 1973 ?        S      0:07   kded [kdeinit]                                                                                                                                      $
 1985 ?        S      0:00   ksmserver [kdeinit] t]                                                                                                                              $
 1986 ?        S      0:00   kaccess [kdeinit]                                                                                                                                   $
 1989 ?        S      0:06   kdesktop [kdeinit] ]                                                                                                                                $
 1992 ?        S      0:19   kicker [kdeinit]                                                                                                                                    $
 1998 ?        S      0:01     ksysguardd MANPATH=/usr/man KDE_MULTIHEAD=false TERM=linux SHELL=/bin/bash GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/vda/.gtkrc-2.0:/.share/root/.kde$
 2022 ?        S      0:10     /usr/app/firefox-1.0.4/firefox-bin MANPATH=/usr/man KDE_MULTIHEAD=false SHELL=/bin/bash TERM=linux DESKTOP_STARTUP_ID=firebird;1129525213;219230;1$



More information about the busybox mailing list