10 # Needed to prevent respawning during a reboot...
15 puts('Starting %s...' % id)
25 # Here we do some process monitoring...
26 Signal.trap(:SIGCHLD) do
29 pid = Process.wait(-1, Process::WNOHANG)
30 key = $daemons.key(pid)
33 cmd = $daemonCmds[key]
35 $daemonCmds.delete(key)
37 # Respawn any processes that exit...
38 if key != 'openrc' && !$shuttingDown
52 puts('*** init-ng v1.0.0 starting...')
54 launch('openrc', %w[/sbin/rc sysinit])
55 Process.wait($daemons['openrc'])
57 launch('openrc', %w[/sbin/rc boot])
58 Process.wait($daemons['openrc'])
60 launch('openrc', %w[/sbin/rc default])
61 Process.wait($daemons['openrc'])
69 launch('openrc', %w[/sbin/rc reboot])
70 Process.wait($daemons['openrc'])
78 ctl = UNIXSocket.open('/run/initctl')
79 ctl.puts(cmd.join(' '))
80 puts(ctl.readline.chomp)
85 # This shows the one of the hazards of coding this in Ruby...
87 # LINUX_REBOOT_CMD_POWER_OFF == 0x4321FEDC
88 # LINUX_REBOOT_CMD_RESTART == 0x01234567
89 # LINUX_REBOOT_CMD_HALT == 0xCDEF0123
90 map = { poweroff: 0x4321fedc, restart: 0x01234567, halt: 0xcdef0123 }
92 # LINUX_REBOOT_MAGIC1 == 0xfee1dead
93 # LINUX_REBOOT_MAGIC2C == 0x20112000
94 syscall(169, 0xfee1dead, 0x20112000, map[cmd])
105 # Start of the script proper
107 # If we're not PID 1, parse & send commands to /run/initctl
111 when 'poweroff', 'restart', 'halt'
112 do_cmd(ARGV[0].to_sym)
114 do_cmd(ARGV.shift.to_sym, *ARGV)
116 map = { poweroff: 0x4321fedc, restart: 0x01234567, halt: 0xcdef0123 }
118 syscall(169, 0xfee1dead, 0x20112000, map[:poweroff])
120 puts('I know the following commands: poweroff, restart, halt, status, test')
135 launch('agetty1', %w[/sbin/agetty tty1 --noclear --autologin root])
139 launch("agetty#{n}", %W[/sbin/agetty tty#{n} --noclear])
144 server = UNIXServer.open('/run/initctl')
145 rescue Errno::EADDRINUSE
146 File.delete('/run/initctl')
152 args = ctl.readline.chomp.split
153 cmd = args.shift.to_sym
155 when :poweroff, :restart, :halt
159 # ctl.puts($daemons[args.first] ? 'ok' : 'dead')
160 # This is NFG. It only shows on the main console, not on the socket, so
161 # you get nothing if you're ssh'ed in...
162 $daemons.each { |key, value| puts(key + ': ' + (value ? '[OK]' : '[!!]') + ' (' + value.to_s + ')') }
165 puts('Received start msg from s-s-d: ' + args.to_s)
167 puts('Received stop msg from s-s-d: ' + args.to_s)