annotate toys/pending/init.c @ 1775:57f2a26fa92c draft toast

To ensure that toybox can be installed alongside busybox without confusing update-alternatives, the paths of the links installed by toybox should match those installed by busybox. This is accomplished by changing the flags of a few tools within toybox.
author Paul Barker <paul@paulbarker.me.uk>
date Sat, 04 Apr 2015 11:58:06 -0500
parents c0c91437138b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* init.c - init program.
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Harvind Singh <harvindsingh1981@gmail.com>
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * No Standard
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 USE_INIT(NEWTOY(init, "", TOYFLAG_SBIN))
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
9
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 config INIT
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 bool "init"
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 default n
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 help
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 usage: init
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
16 System V style init.
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
17
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
18 First program to run (as PID 1) when the system comes up, reading
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
19 /etc/inittab to determine actions.
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 */
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 #include "toys.h"
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
23 #include <sys/reboot.h>
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 struct action_list_seed {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 struct action_list_seed *next;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 uint8_t action;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 char *terminal_name;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 char *command;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
31 } *action_list_pointer = NULL;
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 int caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
33
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 //INITTAB action defination
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #define SYSINIT 0x01
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 #define WAIT 0x02
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 #define ONCE 0x04
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 #define RESPAWN 0x08
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 #define ASKFIRST 0x10
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 #define CTRLALTDEL 0x20
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 #define SHUTDOWN 0x40
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 #define RESTART 0x80
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
43
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 static void initialize_console(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 int fd;
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
47 char *p = getenv("CONSOLE");
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
48
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
49 if (!p) p = getenv("console");
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 if (!p) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 fd = open("/dev/null", O_RDWR);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 if (fd >= 0) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
53 while (fd < 2) fd = dup(fd);
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
54 while (fd > 2) close(fd--);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 fd = open(p, O_RDWR | O_NONBLOCK | O_NOCTTY);
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
58 if (fd < 0) printf("Unable to open console %s\n",p);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 dup2(fd,0);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 dup2(fd,1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 dup2(fd,2);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
65
1187
18cc63376e66 init: don't use VT_OPENQRY.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1185
diff changeset
66 if (!getenv("TERM")) putenv("TERM=linux");
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
68
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
69 static void reset_term(int fd)
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 struct termios terminal;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
72
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
73 tcgetattr(fd, &terminal);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
74 terminal.c_cc[VINTR] = 3; //ctrl-c
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
75 terminal.c_cc[VQUIT] = 28; /*ctrl-\*/
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
76 terminal.c_cc[VERASE] = 127; //ctrl-?
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
77 terminal.c_cc[VKILL] = 21; //ctrl-u
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
78 terminal.c_cc[VEOF] = 4; //ctrl-d
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
79 terminal.c_cc[VSTART] = 17; //ctrl-q
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
80 terminal.c_cc[VSTOP] = 19; //ctrl-s
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
81 terminal.c_cc[VSUSP] = 26; //ctrl-z
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
82
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 terminal.c_line = 0;
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
84 terminal.c_cflag &= CRTSCTS|PARODD|PARENB|CSTOPB|CSIZE|CBAUDEX|CBAUD;
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
85 terminal.c_cflag |= CLOCAL|HUPCL|CREAD;
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
86
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
87 //enable start/stop input and output control + map CR to NL on input
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
88 terminal.c_iflag = IXON|IXOFF|ICRNL;
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
89
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
90 //Map NL to CR-NL on output
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
91 terminal.c_oflag = ONLCR|OPOST;
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 terminal.c_lflag = IEXTEN|ECHOKE|ECHOCTL|ECHOK|ECHOE|ECHO|ICANON|ISIG;
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
93 tcsetattr(fd, TCSANOW, &terminal);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
95
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 static void add_new_action(uint8_t action,char *command,char *term)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 struct action_list_seed *x,**y;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
99
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 y = &action_list_pointer;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 x = *y;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
102 while (x) {
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
103 if (!(strcmp(x->command, command)) && !(strcmp(x->terminal_name, term))) {
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
104 *y = x->next; //remove from the list
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
105 while(*y) y = &(*y)->next; //traverse through list till end
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 x->next = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 y = &(x)->next;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 x = *y;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
112
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 //create a new node
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 if (!x) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 x = xzalloc(sizeof(*x));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 x->command = xstrdup(command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 x->terminal_name = xstrdup(term);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 x->action = action;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 *y = x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
122
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 static void inittab_parsing(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 int i, fd, line_number = 0, token_count = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 char *p, *q, *extracted_token, *tty_name = NULL, *command = NULL, *tmp;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 uint8_t action = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 char *act_name = "sysinit\0wait\0once\0respawn\0askfirst\0ctrlaltdel\0"
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 "shutdown\0restart\0";
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
130
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 fd = open("/etc/inittab", O_RDONLY);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 if (fd < 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 error_msg("Unable to open /etc/inittab. Using Default inittab");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 add_new_action(SYSINIT, "/etc/init.d/rcS", "");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 add_new_action(RESPAWN, "/sbin/getty -n -l /bin/sh -L 115200 tty1 vt100", "");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 while((q = p = get_line(fd))) { //read single line from /etc/inittab
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 char *x;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
139
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
140 if ((x = strchr(p, '#'))) *x = '\0';
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 line_number++;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 token_count = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 action = 0;
1481
c0c91437138b A few fixes for issues reported in static analysis.
Ashwini Sharma <ak.ashwini1981.gmail.com>
parents: 1298
diff changeset
144 tty_name = command = NULL;
c0c91437138b A few fixes for issues reported in static analysis.
Ashwini Sharma <ak.ashwini1981.gmail.com>
parents: 1298
diff changeset
145
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
146 while ((extracted_token = strsep(&p,":"))) {
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 token_count++;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 switch (token_count) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 case 1:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 if (*extracted_token) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
151 if (!strncmp(extracted_token, "/dev/", 5))
1183
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1181
diff changeset
152 tty_name = xmprintf("%s",extracted_token);
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1181
diff changeset
153 else tty_name = xmprintf("/dev/%s",extracted_token);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 } else tty_name = xstrdup("");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 case 2:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 case 3:
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
159 for (tmp = act_name, i = 0; *tmp; i++, tmp += strlen(tmp) +1) {
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
160 if (!strcmp(tmp, extracted_token)) {
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 action = 1 << i;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 if (!*tmp) error_msg("Invalid action at line number %d ---- ignoring",line_number);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 case 4:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 command = xstrdup(extracted_token);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 default:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 error_msg("Bad inittab entry at line %d", line_number);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 } //while token
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
175
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 if (q) free(q);
1481
c0c91437138b A few fixes for issues reported in static analysis.
Ashwini Sharma <ak.ashwini1981.gmail.com>
parents: 1298
diff changeset
177 if (token_count != 4) {
c0c91437138b A few fixes for issues reported in static analysis.
Ashwini Sharma <ak.ashwini1981.gmail.com>
parents: 1298
diff changeset
178 free(tty_name);
c0c91437138b A few fixes for issues reported in static analysis.
Ashwini Sharma <ak.ashwini1981.gmail.com>
parents: 1298
diff changeset
179 free(command);
c0c91437138b A few fixes for issues reported in static analysis.
Ashwini Sharma <ak.ashwini1981.gmail.com>
parents: 1298
diff changeset
180 continue;
c0c91437138b A few fixes for issues reported in static analysis.
Ashwini Sharma <ak.ashwini1981.gmail.com>
parents: 1298
diff changeset
181 }
c0c91437138b A few fixes for issues reported in static analysis.
Ashwini Sharma <ak.ashwini1981.gmail.com>
parents: 1298
diff changeset
182 if (action) add_new_action(action, command, tty_name);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 free(tty_name);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 free(command);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
185 } //while line
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
186
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 close(fd);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
190
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 static void run_command(char *command)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 char *final_command[128];
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 int hyphen = (command[0]=='-');
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
195
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 command = command + hyphen;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
197 if (!strpbrk(command, "?<>'\";[]{}\\|=()*&^$!`~")) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 char *next_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 char *extracted_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 int x = 0;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
201
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 next_command = strncpy(toybuf, command - hyphen, sizeof(toybuf));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 next_command[sizeof(toybuf) - 1] = toybuf[sizeof(toybuf) - 1 ] = '\0';
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 command = next_command + hyphen;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
205 while ((extracted_command = strsep(&next_command," \t"))) {
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 if (*extracted_command) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 final_command[x] = extracted_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 x++;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 final_command[x] = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 snprintf(toybuf, sizeof(toybuf), "exec %s", command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 command = "-/bin/sh"+1;
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
215 final_command[0] = ("-/bin/sh"+!hyphen);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
216 final_command[1] = "-c";
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
217 final_command[2] = toybuf;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
218 final_command[3] = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
220 if (hyphen) ioctl(0, TIOCSCTTY, 0);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 execvp(command, final_command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 error_msg("unable to run %s",command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
224
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 //runs all same type of actions
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 static pid_t final_run(struct action_list_seed *x)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 int fd;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 sigset_t signal_set;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
231
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 sigfillset(&signal_set);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 sigprocmask(SIG_BLOCK, &signal_set, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 if (x->action & ASKFIRST) pid = fork();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
235 else pid = vfork();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
236
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 if (pid > 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 //parent process or error
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 //unblock the signals
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 sigfillset(&signal_set);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
241 sigprocmask(SIG_UNBLOCK, &signal_set, NULL);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
242
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 return pid;
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
244 } else if (pid < 0) {
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
245 perror_msg("fork fail");
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
246 sleep(1);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
247 return 0;
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
248 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
249
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
250 //new born child process
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 sigset_t signal_set_c;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
252 sigfillset(&signal_set_c);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
253 sigprocmask(SIG_UNBLOCK, &signal_set_c, NULL);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
254 setsid(); //new session
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
255
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 if (x->terminal_name[0]) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
257 close(0);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
258 fd = open(x->terminal_name, (O_RDWR|O_NONBLOCK),0600);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
259 if (fd != 0) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
260 error_msg("Unable to open %s,%s\n", x->terminal_name, strerror(errno));
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
261 _exit(EXIT_FAILURE);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 } else {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
263 dup2(0, 1);
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
264 dup2(0, 2);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 }
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
267 reset_term(0);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
268 run_command(x->command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 _exit(-1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
270 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
271
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
272 static struct action_list_seed* mark_as_terminated_process(pid_t pid)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
273 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
274 struct action_list_seed *x;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
275
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
276 if (pid > 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
277 for (x = action_list_pointer; x; x = x->next) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 if (x->pid == pid) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
279 x->pid = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
280 return x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
281 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
282 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
283 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
284
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
285 return NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
286 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
287
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
288 static void waitforpid(pid_t pid)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
289 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
290 if (pid <= 0) return;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
291
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
292 for(;;) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
293 pid_t y = wait(NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
294 mark_as_terminated_process(y);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
295 if (kill(y, 0)) break;
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
296 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
297 }
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
298
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
299 static void run_action_from_list(int action)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
300 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
302 struct action_list_seed *x = action_list_pointer;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
303
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
304 for (; x; x = x->next) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
305 if (!(x->action & action)) continue;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
306 if (x->action & (SHUTDOWN|ONCE|SYSINIT|CTRLALTDEL|WAIT)) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
307 pid = final_run(x);
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
308 if (!pid) return;
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
309 if (x->action & (SHUTDOWN|SYSINIT|CTRLALTDEL|WAIT)) waitforpid(pid);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 if (x->action & (ASKFIRST|RESPAWN))
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 if (!(x->pid)) x->pid = final_run(x);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 }
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
314 }
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
315
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
316 static void set_default(void)
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
318 sigset_t signal_set_c;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
319
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
320 sigatexit(SIG_DFL);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 sigfillset(&signal_set_c);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 sigprocmask(SIG_UNBLOCK,&signal_set_c, NULL);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
323
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
324 run_action_from_list(SHUTDOWN);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
325 error_msg("The system is going down NOW!");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
326 kill(-1, SIGTERM);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
327 error_msg("Sent SIGTERM to all processes");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 sync();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 kill(-1,SIGKILL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 sync();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
332 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
333
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 static void halt_poweroff_reboot_handler(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 unsigned int reboot_magic_no = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 pid_t pid;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
338
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
339 set_default();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
340
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 switch (sig_no) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
342 case SIGUSR1:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 error_msg("Requesting system halt");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
344 reboot_magic_no=RB_HALT_SYSTEM;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
345 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
346 case SIGUSR2:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
347 error_msg("Requesting system poweroff");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
348 reboot_magic_no=RB_POWER_OFF;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
349 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
350 case SIGTERM:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 error_msg("Requesting system reboot");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
352 reboot_magic_no=RB_AUTOBOOT;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
353 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
354 default:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
355 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
356 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
357
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
358 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
359 pid = vfork();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
360
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
361 if (pid == 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
362 reboot(reboot_magic_no);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
363 _exit(EXIT_SUCCESS);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
364 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
365
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
366 while(1) sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
367 }
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
368
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
369 static void restart_init_handler(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
370 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
371 struct action_list_seed *x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
372 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
373 int fd;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
374
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
375 for (x = action_list_pointer; x; x = x->next) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
376 if (!(x->action & RESTART)) continue;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
377
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
378 set_default();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
379
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
380 if (x->terminal_name[0]) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
381 close(0);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
382 fd = open(x->terminal_name, (O_RDWR|O_NONBLOCK),0600);
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
383
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
384 if (fd != 0) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
385 error_msg("Unable to open %s,%s\n", x->terminal_name, strerror(errno));
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
386 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
387 pid = vfork();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
388
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
389 if (pid == 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
390 reboot(RB_HALT_SYSTEM);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
391 _exit(EXIT_SUCCESS);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
392 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
393
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
394 while(1) sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
395 } else {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
396 dup2(0, 1);
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
397 dup2(0, 2);
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
398 reset_term(0);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
399 run_command(x->command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
400 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
401 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
402 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
403 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
404
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
405 static void catch_signal(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
406 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
407 caught_signal = sig_no;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
408 error_msg("signal seen");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
409 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
410
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
411 static void pause_handler(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
412 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
413 int signal_backup,errno_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
414 pid_t pid;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
415
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
416 errno_backup = errno;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
417 signal_backup = caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
418 signal(SIGCONT, catch_signal);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
419
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
420 while(1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
421 if (caught_signal == SIGCONT) break;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
422 do pid = waitpid(-1,NULL,WNOHANG); while((pid==-1) && (errno=EINTR));
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 mark_as_terminated_process(pid);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
424 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
425 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
426
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
427 signal(SIGCONT, SIG_DFL);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
428 errno = errno_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
429 caught_signal = signal_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
430 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
431
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
432 static int check_if_pending_signals(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
433 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
434 int signal_caught = 0;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
435
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
436 while(1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
437 int sig = caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
438 if (!sig) return signal_caught;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
439 caught_signal = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
440 signal_caught = 1;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
441 if (sig == SIGINT) run_action_from_list(CTRLALTDEL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
442 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
443 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
444
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
445 void init_main(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
446 {
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
447 struct sigaction sig_act;
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
448
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
449 if (getpid() != 1) error_exit("Already running");
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
450 printf("Started init\n");
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
451 initialize_console();
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
452 reset_term(0);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
453
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
454 if (chdir("/")) perror_exit("Can't cd to /");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
455 setsid();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
456
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
457 putenv("HOME=/");
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
458 putenv("PATH=/sbin:/usr/sbin:/bin:/usr/bin");
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
459 putenv("SHELL=/bin/sh");
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
460 putenv("USER=root");
1298
c25ee9918e65 First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents: 1261
diff changeset
461
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
462 inittab_parsing();
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
463 signal(SIGUSR1, halt_poweroff_reboot_handler);//halt
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
464 signal(SIGUSR2, halt_poweroff_reboot_handler);//poweroff
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
465 signal(SIGTERM, halt_poweroff_reboot_handler);//reboot
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
466 signal(SIGQUIT, restart_init_handler);//restart init
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
467 memset(&sig_act, 0, sizeof(sig_act));
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
468 sigfillset(&sig_act.sa_mask);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
469 sigdelset(&sig_act.sa_mask, SIGCONT);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
470 sig_act.sa_handler = pause_handler;
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
471 sigaction(SIGTSTP, &sig_act, NULL);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
472 memset(&sig_act, 0, sizeof(sig_act));
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
473 sig_act.sa_handler = catch_signal;
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
474 sigaction(SIGINT, &sig_act, NULL);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
475 sigaction(SIGHUP, &sig_act, NULL);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
476 run_action_from_list(SYSINIT);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
477 check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
478 run_action_from_list(WAIT);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
479 check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
480 run_action_from_list(ONCE);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
481 while (1) {
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
482 int suspected_WNOHANG = check_if_pending_signals();
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
483
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
484 run_action_from_list(RESPAWN | ASKFIRST);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
485 suspected_WNOHANG = suspected_WNOHANG|check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
486 sleep(1);//let cpu breath
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
487 suspected_WNOHANG = suspected_WNOHANG|check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
488 if (suspected_WNOHANG) suspected_WNOHANG=WNOHANG;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
489
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
490 while(1) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
491 pid_t pid = waitpid(-1, NULL, suspected_WNOHANG);
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
492
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
493 if (pid <= 0) break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
494 mark_as_terminated_process(pid);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
495 suspected_WNOHANG = WNOHANG;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
496 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
497 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
498 }