]> git.ipfire.org Git - people/ms/systemd.git/blame - main.c
manager: make running_as configurable
[people/ms/systemd.git] / main.c
CommitLineData
60918275
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
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
ea430986
LP
22#include <dbus/dbus.h>
23
60918275
LP
24#include <stdio.h>
25#include <errno.h>
26#include <string.h>
16354eff 27#include <unistd.h>
4ade7963
LP
28#include <sys/types.h>
29#include <sys/stat.h>
f170852a 30#include <getopt.h>
60918275
LP
31
32#include "manager.h"
16354eff 33#include "log.h"
4ade7963 34#include "mount-setup.h"
60918275 35
f170852a
LP
36static enum {
37 ACTION_RUN,
38 ACTION_HELP
39} action = ACTION_RUN;
40
41static char *default_unit = NULL;
a5dab5ce 42static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
f170852a
LP
43
44static int set_default_unit(const char *u) {
45 char *c;
46
47 assert(u);
48
49 if (!(c = strdup(u)))
50 return -ENOMEM;
51
52 free(default_unit);
53 default_unit = c;
54 return 0;
55}
56
57static int parse_proc_cmdline_word(const char *word) {
58
59 static const char * const rlmap[] = {
60 "single", SPECIAL_RUNLEVEL1_TARGET,
61 "-s", SPECIAL_RUNLEVEL1_TARGET,
62 "s", SPECIAL_RUNLEVEL1_TARGET,
63 "S", SPECIAL_RUNLEVEL1_TARGET,
64 "1", SPECIAL_RUNLEVEL1_TARGET,
65 "2", SPECIAL_RUNLEVEL2_TARGET,
66 "3", SPECIAL_RUNLEVEL3_TARGET,
67 "4", SPECIAL_RUNLEVEL4_TARGET,
68 "5", SPECIAL_RUNLEVEL5_TARGET
69 };
70
71 if (startswith(word, "systemd.default="))
72 return set_default_unit(word + 15);
73
74 else if (startswith(word, "systemd.log_target=")) {
75
76 if (log_set_target_from_string(word + 19) < 0)
77 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
78
79 } else if (startswith(word, "systemd.log_level=")) {
80
81 if (log_set_max_level_from_string(word + 18) < 0)
82 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
83
84 } else {
85 unsigned i;
86
87 /* SysV compatibility */
88
89 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
90 if (streq(word, rlmap[i]))
91 return set_default_unit(rlmap[i+1]);
92 }
93
94 return 0;
95}
96
97static int parse_proc_cmdline(void) {
98 char *line;
99 int r;
100 char *w;
101 size_t l;
102 char *state;
103
104 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
105 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
106 return 0;
107 }
108
109 FOREACH_WORD_QUOTED(w, l, line, state) {
110 char *word;
111
112 if (!(word = strndup(w, l))) {
113 r = -ENOMEM;
114 goto finish;
115 }
116
117 r = parse_proc_cmdline_word(word);
118 free(word);
119
120 if (r < 0)
121 goto finish;
122 }
123
124 r = 0;
125
126finish:
127 free(line);
128 return r;
129}
130
131static int parse_argv(int argc, char *argv[]) {
132
133 enum {
134 ARG_LOG_LEVEL = 0x100,
135 ARG_LOG_TARGET,
a5dab5ce
LP
136 ARG_DEFAULT,
137 ARG_RUNNING_AS
f170852a
LP
138 };
139
140 static const struct option options[] = {
141 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
142 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
143 { "default", required_argument, NULL, ARG_DEFAULT },
a5dab5ce 144 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
f170852a
LP
145 { "help", no_argument, NULL, 'h' },
146 { NULL, 0, NULL, 0 }
147 };
148
149 int c, r;
150
151 assert(argc >= 1);
152 assert(argv);
153
154 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
155
156 switch (c) {
157
158 case ARG_LOG_LEVEL:
159 if ((r = log_set_max_level_from_string(optarg)) < 0) {
160 log_error("Failed to parse log level %s.", optarg);
161 return r;
162 }
163
164 break;
165
166 case ARG_LOG_TARGET:
167
168 if ((r = log_set_target_from_string(optarg)) < 0) {
169 log_error("Failed to parse log target %s.", optarg);
170 return r;
171 }
172
173 break;
174
175 case ARG_DEFAULT:
176
177 if ((r = set_default_unit(optarg)) < 0) {
178 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
179 return r;
180 }
181
182 break;
183
a5dab5ce
LP
184 case ARG_RUNNING_AS: {
185 ManagerRunningAs as;
186
187 if ((as = manager_running_as_from_string(optarg)) < 0) {
188 log_error("Failed to parse running as value %s", optarg);
189 return -EINVAL;
190 }
191
192 running_as = as;
193 break;
194 }
195
f170852a
LP
196 case 'h':
197 action = ACTION_HELP;
198 break;
199
200 case '?':
201 return -EINVAL;
202
203 default:
204 log_error("Unknown option code %c", c);
205 return -EINVAL;
206 }
207
208 if (optind < argc) {
209 log_error("Too many arguments.");
210 return -EINVAL;
211 }
212
213 return 0;
214}
215
216static int help(void) {
217
218 printf("%s [options]\n\n"
219 " -h --help Show this help\n"
220 " --default=UNIT Set default unit\n"
221 " --log-level=LEVEL Set log level\n"
a5dab5ce
LP
222 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
223 " --running-as=AS Set running as (init, system, sesstion)\n",
f170852a
LP
224 __progname);
225
226 return 0;
227}
228
60918275
LP
229int main(int argc, char *argv[]) {
230 Manager *m = NULL;
87f0e418 231 Unit *target = NULL;
60918275
LP
232 Job *job = NULL;
233 int r, retval = 1;
27b14a22 234
a5dab5ce
LP
235 if (getpid() == 1)
236 running_as = MANAGER_INIT;
237 else if (getuid() == 0)
238 running_as = MANAGER_SYSTEM;
239 else
240 running_as = MANAGER_SESSION;
241
f170852a
LP
242 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
243 goto finish;
60918275 244
f170852a
LP
245 /* Mount /proc, /sys and friends, so that /proc/cmdline and
246 * /proc/$PID/fd is available. */
247 mount_setup();
4ade7963
LP
248
249 /* Reset all signal handlers. */
250 assert_se(reset_all_signal_handlers() == 0);
251
f170852a
LP
252 /* Close all open files */
253 assert_se(close_all_fds(NULL, 0) == 0);
254
a5dab5ce
LP
255 if (running_as != MANAGER_SESSION)
256 if (parse_proc_cmdline() < 0)
257 goto finish;
f170852a
LP
258
259 log_parse_environment();
260
261 if (parse_argv(argc, argv) < 0)
262 goto finish;
263
264 if (action == ACTION_HELP) {
265 retval = help();
266 goto finish;
267 }
268
269 assert_se(action == ACTION_RUN);
270
271 /* Move out of the way, so that we won't block unmounts */
272 assert_se(chdir("/") == 0);
273
4ade7963
LP
274 /* Become a session leader if we aren't one yet. */
275 setsid();
276
277 /* Disable the umask logic */
278 umask(0);
279
280 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
281 dbus_connection_set_change_sigpipe(FALSE);
282
4ade7963
LP
283 /* Open the logging devices, if possible and necessary*/
284 log_open_syslog();
285 log_open_kmsg();
286
a5dab5ce
LP
287 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
288
289 if ((r = manager_new(running_as, &m)) < 0) {
8e274523 290 log_error("Failed to allocate manager object: %s", strerror(-r));
60918275
LP
291 goto finish;
292 }
293
f50e0a01
LP
294 if ((r = manager_coldplug(m)) < 0) {
295 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
296 goto finish;
297 }
298
27b14a22
LP
299 log_debug("Activating default unit: %s", default_unit);
300
301 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
c22cbe26 302 log_error("Failed to load default target: %s", strerror(-r));
60918275
LP
303 goto finish;
304 }
305
d46de8a1
LP
306 printf("→ By units:\n");
307 manager_dump_units(m, stdout, "\t");
308
8f5847c4
LP
309 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
310 log_error("Failed to start default target: %s", strerror(-r));
311 goto finish;
312 }
11dd41ce 313
44d8db9e 314 printf("→ By jobs:\n");
cea8e32e
LP
315 manager_dump_jobs(m, stdout, "\t");
316
c20cae32
LP
317 if ((r = manager_loop(m)) < 0) {
318 log_error("Failed to run mainloop: %s", strerror(-r));
319 goto finish;
320 }
9152c765 321
60918275
LP
322 retval = 0;
323
f170852a
LP
324 log_debug("Exit.");
325
60918275
LP
326finish:
327 if (m)
328 manager_free(m);
329
f170852a 330 free(default_unit);
b9cd2ec1 331
ea430986
LP
332 dbus_shutdown();
333
60918275
LP
334 return retval;
335}