]>
Commit | Line | Data |
---|---|---|
8ab34b87 DM |
1 | #!/usr/bin/perl -w |
2 | ||
3 | use strict; | |
4 | use POSIX qw (:sys_wait_h strftime); | |
5 | use POSIX qw(EINTR); | |
6 | use IO::Socket::UNIX; | |
7 | ||
8 | $SIG{CHLD} = sub { | |
9 | 1 while waitpid(-1, WNOHANG) > 0; | |
10 | }; | |
11 | $SIG{INT} = sub { | |
12 | print "stopping init\n"; | |
13 | exit (0); | |
14 | }; | |
15 | ||
16 | mkdir "/dev"; | |
17 | mkdir "/var/"; | |
18 | mkdir "/var/log"; | |
19 | ||
20 | my $logfile = "/var/log/init.log"; | |
21 | ||
22 | close (STDOUT); | |
23 | open (STDOUT, ">>$logfile"); | |
24 | close (STDERR); | |
25 | open STDERR, ">&STDOUT"; | |
26 | ||
27 | select STDERR; $| = 1; # make unbuffered | |
28 | select STDOUT; $| = 1; # make unbuffered | |
29 | ||
30 | my $args = join (" ", @ARGV); | |
31 | ||
32 | if ($$ != 1) { | |
33 | my $l = shift @ARGV; | |
34 | ||
35 | if (defined ($l) && $l eq '0') { | |
36 | print "initctl $args\n"; | |
37 | kill 2, 1; | |
38 | } else { | |
39 | print "initctl $args (ignored)\n"; | |
40 | } | |
41 | ||
42 | exit (0); | |
43 | } | |
44 | ||
45 | print "starting init $args\n"; | |
46 | ||
47 | # only start once when pid == 1 | |
48 | # ignore runlevel change requests | |
49 | exit (0) if $$ != 1; | |
50 | ||
51 | if (! -d "/proc/$$") { | |
52 | system ("mount -t proc proc /proc") == 0 || | |
53 | die "unable to mount proc filesystem\n"; | |
54 | } | |
55 | ||
56 | system ("hostname localhost") == 0 || | |
57 | die "unable to set hostname\n"; | |
58 | ||
59 | ||
e84c155b DM |
60 | # start one child doing nothing - to avoid that we get killed |
61 | if (fork() == 0) { | |
62 | $0 = 'dummy child'; | |
63 | for (;;) { sleep 5; } | |
64 | exit 0; | |
65 | } | |
66 | ||
8ab34b87 DM |
67 | # provide simple syslog |
68 | ||
e84c155b DM |
69 | my $sock = IO::Socket::UNIX->new (Local => "/dev/log", Listen => 5) || |
70 | die "can't open socket /dev/log - $!\n"; | |
8ab34b87 DM |
71 | |
72 | while ((my $fd = $sock->accept()) ||($! == EINTR)) { | |
73 | ||
74 | next if !$fd; # EINTR | |
75 | ||
76 | while (defined (my $line = <$fd>)) { | |
77 | $line =~ s/\0/\n/g; | |
78 | chomp $line; | |
79 | $line =~ s/^<\d+>//mg; | |
80 | next if $line =~ m/^\s*$/; | |
81 | print "$line\n"; | |
82 | } | |
83 | ||
84 | close ($fd); | |
85 | } | |
86 | ||
e84c155b | 87 | print "exit init: $!\n"; |
8ab34b87 | 88 | exit (0); |