chiark / gitweb /
util: declare __progname in util.h
[elogind.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
31 #include "manager.h"
32 #include "log.h"
33 #include "mount-setup.h"
34
35 int main(int argc, char *argv[]) {
36         Manager *m = NULL;
37         Unit *target = NULL;
38         Job *job = NULL;
39         int r, retval = 1;
40         const char *default_unit;
41
42         if (argc >= 2)
43                 default_unit = argv[1];
44         else
45                 default_unit = SPECIAL_DEFAULT_TARGET;
46
47         /* Move out of the way, so that we won't block unmounts */
48         assert_se(chdir("/")  == 0);
49
50         /* Reset all signal handlers. */
51         assert_se(reset_all_signal_handlers() == 0);
52
53         /* Become a session leader if we aren't one yet. */
54         setsid();
55
56         /* Disable the umask logic */
57         umask(0);
58
59         /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
60         dbus_connection_set_change_sigpipe(FALSE);
61
62         /* Mount /dev, /sys and friends */
63         mount_setup();
64
65         /* Set up logging */
66         log_set_target(LOG_TARGET_CONSOLE);
67
68         /* Open the logging devices, if possible and necessary*/
69         log_open_syslog();
70         log_open_kmsg();
71
72         if ((r = manager_new(&m)) < 0) {
73                 log_error("Failed to allocate manager object: %s", strerror(-r));
74                 goto finish;
75         }
76
77         if ((r = manager_coldplug(m)) < 0) {
78                 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
79                 goto finish;
80         }
81
82         log_debug("Activating default unit: %s", default_unit);
83
84         if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
85                 log_error("Failed to load default target: %s", strerror(-r));
86                 goto finish;
87         }
88
89         printf("→ By units:\n");
90         manager_dump_units(m, stdout, "\t");
91
92         if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
93                 log_error("Failed to start default target: %s", strerror(-r));
94                 goto finish;
95         }
96
97         printf("→ By jobs:\n");
98         manager_dump_jobs(m, stdout, "\t");
99
100         if ((r = manager_loop(m)) < 0) {
101                 log_error("Failed to run mainloop: %s", strerror(-r));
102                 goto finish;
103         }
104
105         retval = 0;
106
107 finish:
108         if (m)
109                 manager_free(m);
110
111         log_debug("Exit.");
112
113         dbus_shutdown();
114
115         return retval;
116 }