forked from fifteenhex/smolutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.c
More file actions
202 lines (151 loc) · 3.66 KB
/
Copy pathinit.c
File metadata and controls
202 lines (151 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-License-Identifier: GPL-3.0-or-later
#include "config.h"
#include "common.h"
#include "nolibc_extensions/signal.h"
#define STARTUP_PATH "/sbin/startup"
#define GETTY_PATH "/sbin/getty"
#define GETTY_NAME "getty"
#define SHELL_PATH "/bin/smolsh"
static const char cmdline_opt_prefix[] = "smolinit.";
static const char cmdline_opt_getty[] = "getty=";
static const char cmdline_opt_hostname[] = "hostname=";
static const char cmdline_opt_dhcpif[] = "dhcpif=";
struct getty {
const char *tty_path;
pid_t getty_pid;
};
static struct getty gettys[16];
static unsigned num_gettys = 0;
static const char *hostname = NULL;
static const char *dhcpif = NULL;
static void parse_cmdline(int argc, char **argv)
{
int i;
debug("cmdline args:\n");
/* First arg will be the program name, skip that */
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
debug("%s\n", arg);
if (STARTS_WITH(arg, cmdline_opt_prefix)) {
const char *opt = arg + STRLEN(cmdline_opt_prefix);
if (STARTS_WITH(opt, cmdline_opt_getty)) {
const char *tty_path = opt + STRLEN(cmdline_opt_getty);
debug("Will start getty on TTY %s\n", tty_path);
/*
* I guess its safe to just point the argv memory to avoid
* wasting memory copying strings.
*/
gettys[num_gettys++].tty_path = tty_path;
}
else if (!hostname && STARTS_WITH(opt, cmdline_opt_hostname)) {
const char *name = opt + STRLEN(cmdline_opt_hostname);
debug("Hostname will be %s\n", name);
hostname = name;
}
else if (!dhcpif && STARTS_WITH(opt, cmdline_opt_dhcpif)) {
const char *intf = opt + STRLEN(cmdline_opt_dhcpif);
debug("Will configure %s via DHCP\n", intf);
dhcpif = intf;
}
}
}
}
static void parse_environment(char **envp)
{
char *var;
debug("environment variables\n");
while (true) {
var = *envp++;
if (!var)
break;
debug("%s\n", var);
}
}
static int spawn_getty(struct getty *getty)
{
const char *tty_path = getty->tty_path;
char *newenviron[] = { NULL };
char * const newargv[] = {
GETTY_NAME,
tty_path,
SHELL_PATH,
NULL
};
int tty_fd;
pid_t pid;
pid = spawn(GETTY_PATH, newargv, newenviron);
if (pid == -1)
return -1;
getty->getty_pid = pid;
return 0;
}
static void handle_sig(int sig)
{
printf("got sig!\n");
}
static int setup_signals(void)
{
struct sigaction act = {
.sa_flags = SA_RESTART,
.sa_handler = handle_sig,
};
int ret;
ret = sigaction(SIGUSR1, &act, NULL);
if (ret)
printf("failed to setup signals: %d\n", errno);
return 0;
}
static inline int run_startup(void)
{
char *startup_args[6] = {
"startup",
};
int startup_argc = 1;
int ret;
if (!hostname)
hostname="smol";
startup_args[startup_argc++] = "-h";
startup_args[startup_argc++] = hostname;
if (dhcpif) {
startup_args[startup_argc++] = "-n";
startup_args[startup_argc++] = dhcpif;
}
ret = spawn_and_wait_args(STARTUP_PATH, startup_args);
if (ret)
error("startup failed\n");
return 0;
}
int main (int argc, char **argv, char **envp)
{
int ret, i;
printf("smolutils init (%s, %s)\n", __DATE__, __TIME__);
parse_cmdline(argc, argv);
parse_environment(envp);
ret = run_startup();
if (ret)
return 1;
setup_signals();
/* Spawn each of the configured gettys */
for (i = 0; i < num_gettys; i++) {
ret = spawn_getty(&gettys[i]);
if (ret) {
error("Failed to spawn getty\n");
return 1; /* hmmm */
}
}
/* Now sit in wait for one of the gettys to exit */
while (true) {
pid_t pid;
int i;
pid = wait(NULL);
verbose("pid %d came home\n", (int) pid);
for (i = 0; i < num_gettys; i++) {
struct getty *getty = &gettys[i];
if (getty->getty_pid == pid) {
spawn_getty(getty);
break;
}
}
}
return 0;
}