]> git.ipfire.org Git - people/ms/systemd.git/blob - main.c
hostname: set hostname early during boottime
[people/ms/systemd.git] / main.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <dbus/dbus.h>
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <getopt.h>
31
32 #include "manager.h"
33 #include "log.h"
34 #include "mount-setup.h"
35 #include "hostname-setup.h"
36 #include "load-fragment.h"
37
38 static enum {
39 ACTION_RUN,
40 ACTION_HELP,
41 ACTION_TEST
42 } action = ACTION_RUN;
43
44 static char *default_unit = NULL;
45 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
46
47 static int set_default_unit(const char *u) {
48 char *c;
49
50 assert(u);
51
52 if (!(c = strdup(u)))
53 return -ENOMEM;
54
55 free(default_unit);
56 default_unit = c;
57 return 0;
58 }
59
60 static int parse_proc_cmdline_word(const char *word) {
61
62 static const char * const rlmap[] = {
63 "single", SPECIAL_RUNLEVEL1_TARGET,
64 "-s", SPECIAL_RUNLEVEL1_TARGET,
65 "s", SPECIAL_RUNLEVEL1_TARGET,
66 "S", SPECIAL_RUNLEVEL1_TARGET,
67 "1", SPECIAL_RUNLEVEL1_TARGET,
68 "2", SPECIAL_RUNLEVEL2_TARGET,
69 "3", SPECIAL_RUNLEVEL3_TARGET,
70 "4", SPECIAL_RUNLEVEL4_TARGET,
71 "5", SPECIAL_RUNLEVEL5_TARGET
72 };
73
74 if (startswith(word, "systemd.default="))
75 return set_default_unit(word + 16);
76
77 else if (startswith(word, "systemd.log_target=")) {
78
79 if (log_set_target_from_string(word + 19) < 0)
80 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
81
82 } else if (startswith(word, "systemd.log_level=")) {
83
84 if (log_set_max_level_from_string(word + 18) < 0)
85 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
86
87 } else {
88 unsigned i;
89
90 /* SysV compatibility */
91
92 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
93 if (streq(word, rlmap[i]))
94 return set_default_unit(rlmap[i+1]);
95 }
96
97 return 0;
98 }
99
100 static int parse_proc_cmdline(void) {
101 char *line;
102 int r;
103 char *w;
104 size_t l;
105 char *state;
106
107 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
108 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
109 return 0;
110 }
111
112 FOREACH_WORD_QUOTED(w, l, line, state) {
113 char *word;
114
115 if (!(word = strndup(w, l))) {
116 r = -ENOMEM;
117 goto finish;
118 }
119
120 r = parse_proc_cmdline_word(word);
121 free(word);
122
123 if (r < 0)
124 goto finish;
125 }
126
127 r = 0;
128
129 finish:
130 free(line);
131 return r;
132 }
133
134 static int parse_argv(int argc, char *argv[]) {
135
136 enum {
137 ARG_LOG_LEVEL = 0x100,
138 ARG_LOG_TARGET,
139 ARG_DEFAULT,
140 ARG_RUNNING_AS,
141 ARG_TEST
142 };
143
144 static const struct option options[] = {
145 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
146 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
147 { "default", required_argument, NULL, ARG_DEFAULT },
148 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
149 { "test", no_argument, NULL, ARG_TEST },
150 { "help", no_argument, NULL, 'h' },
151 { NULL, 0, NULL, 0 }
152 };
153
154 int c, r;
155
156 assert(argc >= 1);
157 assert(argv);
158
159 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
160
161 switch (c) {
162
163 case ARG_LOG_LEVEL:
164 if ((r = log_set_max_level_from_string(optarg)) < 0) {
165 log_error("Failed to parse log level %s.", optarg);
166 return r;
167 }
168
169 break;
170
171 case ARG_LOG_TARGET:
172
173 if ((r = log_set_target_from_string(optarg)) < 0) {
174 log_error("Failed to parse log target %s.", optarg);
175 return r;
176 }
177
178 break;
179
180 case ARG_DEFAULT:
181
182 if ((r = set_default_unit(optarg)) < 0) {
183 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
184 return r;
185 }
186
187 break;
188
189 case ARG_RUNNING_AS: {
190 ManagerRunningAs as;
191
192 if ((as = manager_running_as_from_string(optarg)) < 0) {
193 log_error("Failed to parse running as value %s", optarg);
194 return -EINVAL;
195 }
196
197 running_as = as;
198 break;
199 }
200
201 case ARG_TEST:
202 action = ACTION_TEST;
203 break;
204
205 case 'h':
206 action = ACTION_HELP;
207 break;
208
209 case '?':
210 return -EINVAL;
211
212 default:
213 log_error("Unknown option code %c", c);
214 return -EINVAL;
215 }
216
217 return 0;
218 }
219
220 static int help(void) {
221
222 printf("%s [options]\n\n"
223 " -h --help Show this help\n"
224 " --default=UNIT Set default unit\n"
225 " --log-level=LEVEL Set log level\n"
226 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
227 " --running-as=AS Set running as (init, system, session)\n"
228 " --test Determine startup sequence, dump it and exit\n",
229 __progname);
230
231 return 0;
232 }
233
234 int main(int argc, char *argv[]) {
235 Manager *m = NULL;
236 Unit *target = NULL;
237 Job *job = NULL;
238 int r, retval = 1;
239
240 if (getpid() == 1)
241 running_as = MANAGER_INIT;
242 else if (getuid() == 0)
243 running_as = MANAGER_SYSTEM;
244 else
245 running_as = MANAGER_SESSION;
246
247 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
248 goto finish;
249
250 /* Mount /proc, /sys and friends, so that /proc/cmdline and
251 * /proc/$PID/fd is available. */
252 if (mount_setup() < 0)
253 goto finish;
254
255 /* Reset all signal handlers. */
256 assert_se(reset_all_signal_handlers() == 0);
257
258 /* Close all open files */
259 assert_se(close_all_fds(NULL, 0) == 0);
260
261 if (running_as != MANAGER_SESSION)
262 if (parse_proc_cmdline() < 0)
263 goto finish;
264
265 log_parse_environment();
266
267 if (parse_argv(argc, argv) < 0)
268 goto finish;
269
270 if (action == ACTION_HELP) {
271 retval = help();
272 goto finish;
273 }
274
275 assert_se(action == ACTION_RUN || action == ACTION_TEST);
276
277 /* Set up PATH unless it is already set */
278 setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", false);
279
280 /* Move out of the way, so that we won't block unmounts */
281 assert_se(chdir("/") == 0);
282
283 /* Become a session leader if we aren't one yet. */
284 setsid();
285
286 /* Disable the umask logic */
287 umask(0);
288
289 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
290 dbus_connection_set_change_sigpipe(FALSE);
291
292 /* Open the logging devices, if possible and necessary*/
293 log_open_syslog();
294 log_open_kmsg();
295
296 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
297
298 if (running_as == MANAGER_INIT)
299 if (hostname_setup() < 0)
300 goto finish;
301
302 if ((r = manager_new(running_as, &m)) < 0) {
303 log_error("Failed to allocate manager object: %s", strerror(-r));
304 goto finish;
305 }
306
307 if ((r = manager_coldplug(m)) < 0) {
308 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
309 goto finish;
310 }
311
312 log_debug("Activating default unit: %s", default_unit);
313
314 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
315 log_error("Failed to load default target: %s", strerror(-r));
316
317 log_info("Trying to load rescue target...");
318 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) {
319 log_error("Failed to load rescue target: %s", strerror(-r));
320 goto finish;
321 }
322 }
323
324 if (action == ACTION_TEST) {
325 printf("→ By units:\n");
326 manager_dump_units(m, stdout, "\t");
327 }
328
329 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
330 log_error("Failed to start default target: %s", strerror(-r));
331 goto finish;
332 }
333
334 if (action == ACTION_TEST) {
335 printf("→ By jobs:\n");
336 manager_dump_jobs(m, stdout, "\t");
337
338 if (getpid() == 1)
339 pause();
340
341 retval = 0;
342 goto finish;
343 }
344
345 if ((r = manager_loop(m)) < 0) {
346 log_error("Failed to run mainloop: %s", strerror(-r));
347 goto finish;
348 }
349
350 retval = 0;
351
352 log_debug("Exit.");
353
354 finish:
355 if (m)
356 manager_free(m);
357
358 free(default_unit);
359
360 dbus_shutdown();
361
362 return retval;
363 }