]> git.proxmox.com Git - dab.git/blame - scripts/init.pl
improve init.log: start one dummy child to avoid that we get killed
[dab.git] / scripts / init.pl
CommitLineData
8ab34b87
DM
1#!/usr/bin/perl -w
2
3use strict;
4use POSIX qw (:sys_wait_h strftime);
5use POSIX qw(EINTR);
6use 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
16mkdir "/dev";
17mkdir "/var/";
18mkdir "/var/log";
19
20my $logfile = "/var/log/init.log";
21
22close (STDOUT);
23open (STDOUT, ">>$logfile");
24close (STDERR);
25open STDERR, ">&STDOUT";
26
27select STDERR; $| = 1; # make unbuffered
28select STDOUT; $| = 1; # make unbuffered
29
30my $args = join (" ", @ARGV);
31
32if ($$ != 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
45print "starting init $args\n";
46
47# only start once when pid == 1
48# ignore runlevel change requests
49exit (0) if $$ != 1;
50
51if (! -d "/proc/$$") {
52 system ("mount -t proc proc /proc") == 0 ||
53 die "unable to mount proc filesystem\n";
54}
55
56system ("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
61if (fork() == 0) {
62 $0 = 'dummy child';
63 for (;;) { sleep 5; }
64 exit 0;
65}
66
8ab34b87
DM
67# provide simple syslog
68
e84c155b
DM
69my $sock = IO::Socket::UNIX->new (Local => "/dev/log", Listen => 5) ||
70 die "can't open socket /dev/log - $!\n";
8ab34b87
DM
71
72while ((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 87print "exit init: $!\n";
8ab34b87 88exit (0);