annotate toys/pending/init.c @ 1276:d48bdc1cb017 draft

Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
author Rob Landley <rob@landley.net>
date Tue, 06 May 2014 06:31:28 -0500
parents 9e105bab92e5
children c25ee9918e65
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
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
16 init the system.
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 */
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 #include "toys.h"
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
20 #include <sys/reboot.h>
986
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 struct action_list_seed {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 struct action_list_seed *next;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 uint8_t action;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 char *terminal_name;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 char *command;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
28 } *action_list_pointer = NULL;
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 int caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
30
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 //INITTAB action defination
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 #define SYSINIT 0x01
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 #define WAIT 0x02
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 #define ONCE 0x04
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #define RESPAWN 0x08
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 #define ASKFIRST 0x10
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 #define CTRLALTDEL 0x20
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 #define SHUTDOWN 0x40
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 #define RESTART 0x80
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
40
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 static void initialize_console(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 int fd;
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
44 char *p = (p = getenv("CONSOLE")) ? p : getenv("console");
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
45
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 if (!p) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 fd = open("/dev/null", O_RDWR);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 if (fd >= 0) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
49 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
50 while (fd > 2) close(fd--);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 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
54 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
55 else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 dup2(fd,0);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 dup2(fd,1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 dup2(fd,2);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
61
1187
18cc63376e66 init: don't use VT_OPENQRY.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1185
diff changeset
62 if (!getenv("TERM")) putenv("TERM=linux");
986
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
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
65 static void set_sane_term(void)
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 struct termios terminal;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
68
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
69 tcgetattr(0, &terminal);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
70 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
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 terminal.c_line = 0;
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
80 terminal.c_cflag = terminal.c_cflag&(CRTSCTS|PARODD|PARENB|CSTOPB|CSIZE|CBAUDEX|CBAUD);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
81 terminal.c_cflag = terminal.c_cflag|(CLOCAL|HUPCL|CREAD);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
82 terminal.c_iflag = IXON|IXOFF|ICRNL;//enable start/stop input and output control + map CR to NL on input
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
83 terminal.c_oflag = ONLCR|OPOST;//Map NL to CR-NL on output
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 terminal.c_lflag = IEXTEN|ECHOKE|ECHOCTL|ECHOK|ECHOE|ECHO|ICANON|ISIG;
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
85 tcsetattr(0, TCSANOW, &terminal);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
87
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 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
89 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 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
91
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 y = &action_list_pointer;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 x = *y;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
94 while (x) {
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
95 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
96 *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
97 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
98 x->next = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 y = &(x)->next;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 x = *y;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
104
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 //create a new node
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 if (!x) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 x = xzalloc(sizeof(*x));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 x->command = xstrdup(command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 x->terminal_name = xstrdup(term);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 x->action = action;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 *y = x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
114
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 static void inittab_parsing(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 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
118 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
119 uint8_t action = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 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
121 "shutdown\0restart\0";
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 fd = open("/etc/inittab", O_RDONLY);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 if (fd < 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 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
126 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
127 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
128 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 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
130 char *x;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
131
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
132 if ((x = strchr(p, '#'))) *x = '\0';
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 line_number++;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 token_count = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 action = 0;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
136 while ((extracted_token = strsep(&p,":"))) {
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 token_count++;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 switch (token_count) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 case 1:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 if (*extracted_token) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
141 if (!strncmp(extracted_token, "/dev/", 5))
1183
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1181
diff changeset
142 tty_name = xmprintf("%s",extracted_token);
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1181
diff changeset
143 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
144 } else tty_name = xstrdup("");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
146 case 2:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 case 3:
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
149 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
150 if (!strcmp(tmp, extracted_token)) {
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 action = 1 << i;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 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
156 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 case 4:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 command = xstrdup(extracted_token);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
159 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 default:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 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
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 } //while token
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
165
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 if (q) free(q);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 if (token_count != 4) continue;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 if (action) add_new_action(action, command, tty_name);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 free(tty_name);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 free(command);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
171 } //while line
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
172
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 close(fd);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
176
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 static void run_command(char *command)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 char *final_command[128];
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 int hyphen = (command[0]=='-');
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
181
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 command = command + hyphen;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 if (!strpbrk(command, "?<>'\";[]{}\\|=()*&^$!`~")) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 char *next_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 char *extracted_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 int x = 0;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
187
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 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
189 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
190 command = next_command + hyphen;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
191 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
192 if (*extracted_command) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 final_command[x] = extracted_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 x++;
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 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
197 final_command[x] = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 snprintf(toybuf, sizeof(toybuf), "exec %s", command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 command = "-/bin/sh"+1;
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
201 final_command[0] = ("-/bin/sh"+!hyphen);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
202 final_command[1] = "-c";
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 final_command[2] = toybuf;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 final_command[3] = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 if (hyphen) ioctl(0, TIOCSCTTY, 0);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 execvp(command, final_command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 error_msg("unable to run %s",command);
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 //runs all same type of actions
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 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
213 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 int fd;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 sigset_t signal_set;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
217
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
218 sigfillset(&signal_set);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 sigprocmask(SIG_BLOCK, &signal_set, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
220 if (x->action & ASKFIRST) pid = fork();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 else pid = vfork();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
222
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 if (pid > 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 //parent process or error
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 //unblock the signals
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 sigfillset(&signal_set);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 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
228
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 return pid;
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
230 } else if (pid < 0) {
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
231 perror_msg("fork fail");
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
232 sleep(1);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
233 return 0;
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
234 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
235
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 //new born child process
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 sigset_t signal_set_c;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 sigfillset(&signal_set_c);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 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
240 setsid(); //new session
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
241
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
242 if (x->terminal_name[0]) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 close(0);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
244 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
245 if (fd != 0) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
246 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
247 _exit(EXIT_FAILURE);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
248 } else {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
249 dup2(0, 1);
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
250 dup2(0, 2);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
252 }
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
253 set_sane_term();
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
254 run_command(x->command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
255 _exit(-1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
257
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 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
259 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 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
261
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 if (pid > 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
263 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
264 if (x->pid == pid) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 x->pid = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 return x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
268 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
270
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
271 return NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
272 }
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 static void waitforpid(pid_t pid)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
275 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
276 if (pid <= 0) return;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
277
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 for(;;) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
279 pid_t y = wait(NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
280 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
281 if (kill(y, 0)) break;
986
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 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
284 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
285 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
286 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
287 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
288
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
289 for (; x; x = x->next) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
290 if (!(x->action & action)) continue;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
291 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
292 pid = final_run(x);
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
293 if (!pid) return;
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
294 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
295 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
296 if (x->action & (ASKFIRST|RESPAWN))
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
297 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
298 }
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
299 }
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
300
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
301 static void set_default(void)
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
302 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
303 sigset_t signal_set_c;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
304
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
305 signal(SIGUSR1,SIG_DFL);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
306 signal(SIGUSR2,SIG_DFL);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
307 signal(SIGTERM,SIG_DFL);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
308 signal(SIGQUIT,SIG_DFL);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
309 signal(SIGINT,SIG_DFL);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
310 signal(SIGHUP,SIG_DFL);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
311 signal(SIGTSTP,SIG_DFL);
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
312 signal(SIGSTOP,SIG_DFL);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 sigfillset(&signal_set_c);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
314 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
315
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
316 run_action_from_list(SHUTDOWN);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 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
318 kill(-1, SIGTERM);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
319 error_msg("Sent SIGTERM to all processes");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
320 sync();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 kill(-1,SIGKILL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
323 sync();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
324 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
325
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
326 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
327 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 unsigned int reboot_magic_no = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 pid_t pid;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
330
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
331 set_default();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
332
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
333 switch (sig_no) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 case SIGUSR1:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 error_msg("Requesting system halt");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 reboot_magic_no=RB_HALT_SYSTEM;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 case SIGUSR2:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
339 error_msg("Requesting system poweroff");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
340 reboot_magic_no=RB_POWER_OFF;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
342 case SIGTERM:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 error_msg("Requesting system reboot");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
344 reboot_magic_no=RB_AUTOBOOT;
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 default:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
347 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
348 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
349
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
350 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 pid = vfork();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
352
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
353 if (pid == 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
354 reboot(reboot_magic_no);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
355 _exit(EXIT_SUCCESS);
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 while(1) sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
359 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
360 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
361 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
362 struct action_list_seed *x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
363 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
364 int fd;
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 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
367 if (!(x->action & RESTART)) continue;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
368
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
369 set_default();
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
370
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
371 if (x->terminal_name[0]) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
372 close(0);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
373 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
374
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
375 if (fd != 0) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
376 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
377 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
378 pid = vfork();
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 (pid == 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
381 reboot(RB_HALT_SYSTEM);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
382 _exit(EXIT_SUCCESS);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
383 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
384
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
385 while(1) sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
386 } else {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
387 dup2(0, 1);
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
388 dup2(0, 2);
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
389 set_sane_term();
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
390 run_command(x->command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
391 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
392 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
393 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
394 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
395
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
396 static void catch_signal(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
397 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
398 caught_signal = sig_no;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
399 error_msg("signal seen");
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 static void pause_handler(int sig_no)
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 int signal_backup,errno_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
405 pid_t pid;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
406
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
407 errno_backup = errno;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
408 signal_backup = caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
409 signal(SIGCONT, catch_signal);
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
410
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
411 while(1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
412 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
413 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
414 mark_as_terminated_process(pid);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
415 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
416 }
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
417
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
418 signal(SIGCONT, SIG_DFL);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
419 errno = errno_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
420 caught_signal = signal_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
421 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
422
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 static int check_if_pending_signals(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
424 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
425 int signal_caught = 0;
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
426
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
427 while(1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
428 int sig = caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
429 if (!sig) return signal_caught;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
430 caught_signal = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
431 signal_caught = 1;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
432 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
433 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
434 }
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 void init_main(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
437 {
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
438 struct sigaction sig_act;
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
439
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
440 if (getpid() != 1) error_exit("Already running");
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
441 printf("Started init\n");
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
442 initialize_console();
1261
9e105bab92e5 Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents: 1251
diff changeset
443 set_sane_term();
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
444
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
445 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
446 setsid();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
447
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
448 putenv("HOME=/");
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
449 putenv("PATH=/sbin:/usr/sbin:/bin:/usr/bin");
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
450 putenv("SHELL=/bin/sh");
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
451 putenv("USER=root");
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
452 inittab_parsing();
1185
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
453 signal(SIGUSR1, halt_poweroff_reboot_handler);//halt
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
454 signal(SIGUSR2, halt_poweroff_reboot_handler);//poweroff
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
455 signal(SIGTERM, halt_poweroff_reboot_handler);//reboot
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
456 signal(SIGQUIT, restart_init_handler);//restart init
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
457 memset(&sig_act, 0, sizeof(sig_act));
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
458 sigfillset(&sig_act.sa_mask);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
459 sigdelset(&sig_act.sa_mask, SIGCONT);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
460 sig_act.sa_handler = pause_handler;
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
461 sigaction(SIGTSTP, &sig_act, NULL);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
462 memset(&sig_act, 0, sizeof(sig_act));
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
463 sig_act.sa_handler = catch_signal;
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
464 sigaction(SIGINT, &sig_act, NULL);
2a4f1dc494d0 -Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1183
diff changeset
465 sigaction(SIGHUP, &sig_act, NULL);
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
466 run_action_from_list(SYSINIT);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
467 check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
468 run_action_from_list(WAIT);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
469 check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
470 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
471 while (1) {
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
472 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
473
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
474 run_action_from_list(RESPAWN | ASKFIRST);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
475 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
476 sleep(1);//let cpu breath
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
477 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
478 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
479
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
480 while(1) {
1181
a2f80613be36 Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents: 986
diff changeset
481 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
482
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
483 if (pid <= 0) break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
484 mark_as_terminated_process(pid);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
485 suspected_WNOHANG = WNOHANG;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
486 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
487 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
488 }