# Copyright (c) 1998 Prosa Srl. This is free software, according to the GPL.

   puts ""
   puts "Starting init process ..."
   puts ""

   # /etc/init.d/options tells what to do and what not to do...
   source /etc/init.d/options

   if $options(fsck.root) {
       puts "Checking root filesystem"
       # Use sys_exec instead of exec to prevent using /tmp files
       if ![sys_fork]  {
           set F [open /etc/fstab]; gets $F root; close $F
           set root [lindex $root 0]
           # The runtime system may lack /sbin/fsck
           set fsck /sbin/fsck.minix
           if [file exists /sbin/fsck] {set fsck /sbin/fsck}
           sys_exec $fsck -a $root
       } else {

   if $options(remount) {
       puts "Remounting /"
       # Use internal mount command

   if $options(proc) {
       puts "Mounting /proc"
       # Use internal command once again
       mount proc

   if $options(swap) {
       puts "Activating swap space"
       catch {exec /sbin/swapon -a}

   if $options(fsck) {
       puts "Checking all filesystems"
       catch {exec fsck -R -A -V -a}

   if $options(mount) {
       puts "Mounting all the filesystems"
       # First, clear mtab and enter / in it
       set F [open /etc/mtab w]; close $F
       exec mount -f /
       catch {exec umount /proc}
       catch {exec mount -a -t nonfs}

   if [llength $options(lowmem)] {
       puts "Reducing free memory"
       set F [open /proc/sys/vm/freepages w]
       puts $F $options(lowmem)

   if $options(rm) {
       puts "Removing /tmp/* and /var/run/*"
       foreach n "[glob -nocomplain /tmp/*] [glob -nocomplain /var/run/*]" {
           rm $n
   # Recreate /var/run/utmp, so mingetty is happy
   set F [open /var/run/utmp w]; close $F

   if [llength $options(modules)] {
       puts "Loading modules:"
       foreach n $options(modules) {
           puts "   $n"
           if [catch {exec /sbin/insmod $n} msg] {
               puts stderr "module $n: error: \"$msg\""

   # Set hostname
   if $options(proc) {
       set F [open /etc/hostname r]
       gets $F hostname
       close $F
       set F [open /proc/sys/kernel/hostname w]
       puts $F $hostname
       close $F

   # Run scripts in /etc/init.d/scripts
   foreach n [lsort [glob -nocomplain /etc/init.d/scripts/S*]] {
       catch "source $n"

   # Ok, drop options to save memory
   unset options

   # Run respawning tasks in /etc/init.d/respawn
   foreach n [lsort [glob -nocomplain /etc/init.d/respawn/S*]] {
       set newpid [sys_fork]
       if $newpid {
           set pid($newpid) $n
       } else {
           source $n; exit

   # Run user applications in /etc/applications
   foreach n [lsort [glob -nocomplain /etc/applications/*]] {
       if ![file isfile $n] continue; # skip directories
       if ![sys_fork] {
           catch {unset pid}
           newcmdname [file tail $n]
           source $n; exit
       } else {
           puts "   Application: [file tail $n]"

   # Now wait for processes to die
   while 1 {
       set reaped [sys_wait]
       set reboot 0
       if [file exists /etc/reboot] {
           rm /etc/reboot
           set reboot 1
       if [file exists /etc/halt] {
           rm /etc/halt
           set reboot 2

       # No reboot, respawn a process if needed
       puts -nonewline "Reaped: $reaped"
       set oldpid [lindex $reaped 0]
       if [info exists pid($oldpid)] {
           set app $pid($oldpid); unset pid($oldpid)
           puts ": $app"
           set newpid [sys_fork]
           if $newpid {
               set pid($newpid) $app
           } else {
               unset pid; source $app; exit
       } else {puts ""}

   # Reboot or halt time has arrived

   puts "Sending TERM signal..."
   outp 0x80 4
   sys_kill -15 -1;
   after 1000; outp 0x80 3
   after 1000; outp 0x80 2
   after 1000; outp 0x80 1
   puts "Sending KILL signal..."
   sys_kill -9 -1; after 500
   puts "Unmounting filesystems"
   catch umount;   after 200
   if $reboot==2 {
       puts "System halted"
       outp 0x80 0
       sys_reboot halt
   puts "Rebooting..."
   outp 0x80 0
   after 500
   outp 0x80 0xff
   sys_reboot reboot