chiark / gitweb /
notify: add minimal readiness/status protocol for spawned daemons
[elogind.git] / src / manager.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef foomanagerhfoo
4 #define foomanagerhfoo
5
6 /***
7   This file is part of systemd.
8
9   Copyright 2010 Lennart Poettering
10
11   systemd is free software; you can redistribute it and/or modify it
12   under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2 of the License, or
14   (at your option) any later version.
15
16   systemd is distributed in the hope that it will be useful, but
17   WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19   General Public License for more details.
20
21   You should have received a copy of the GNU General Public License
22   along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 ***/
24
25 #include <stdbool.h>
26 #include <inttypes.h>
27 #include <stdio.h>
28 #include <dbus/dbus.h>
29
30 #include "fdset.h"
31
32 /* Enforce upper limit how many names we allow */
33 #define MANAGER_MAX_NAMES 2048
34
35 typedef struct Manager Manager;
36 typedef enum WatchType WatchType;
37 typedef struct Watch Watch;
38
39 typedef enum ManagerExitCode {
40         MANAGER_RUNNING,
41         MANAGER_EXIT,
42         MANAGER_RELOAD,
43         MANAGER_REEXECUTE,
44         _MANAGER_EXIT_CODE_MAX,
45         _MANAGER_EXIT_CODE_INVALID = -1
46 } ManagerExitCode;
47
48 typedef enum ManagerRunningAs {
49         MANAGER_INIT,      /* root and pid=1 */
50         MANAGER_SYSTEM,    /* root and pid!=1 */
51         MANAGER_SESSION,   /* non-root, for a session */
52         _MANAGER_RUNNING_AS_MAX,
53         _MANAGER_RUNNING_AS_INVALID = -1
54 } ManagerRunningAs;
55
56 enum WatchType {
57         WATCH_INVALID,
58         WATCH_SIGNAL,
59         WATCH_NOTIFY,
60         WATCH_FD,
61         WATCH_TIMER,
62         WATCH_MOUNT,
63         WATCH_UDEV,
64         WATCH_DBUS_WATCH,
65         WATCH_DBUS_TIMEOUT
66 };
67
68 struct Watch {
69         int fd;
70         WatchType type;
71         union {
72                 union Unit *unit;
73                 DBusWatch *bus_watch;
74                 DBusTimeout *bus_timeout;
75         } data;
76         bool fd_is_dupped:1;
77         bool socket_accept:1;
78 };
79
80 #include "unit.h"
81 #include "job.h"
82 #include "hashmap.h"
83 #include "list.h"
84 #include "set.h"
85 #include "dbus.h"
86 #include "path-lookup.h"
87
88 #define SPECIAL_DEFAULT_TARGET "default.target"
89
90 /* This is not really intended to be started by directly. This is
91  * mostly so that other targets (reboot/halt/poweroff) can depend on
92  * it to bring all services down that want to be brought down on
93  * system shutdown. */
94 #define SPECIAL_SHUTDOWN_TARGET "shutdown.target"
95
96 #define SPECIAL_LOGGER_SOCKET "systemd-logger.socket"
97
98 #define SPECIAL_KBREQUEST_TARGET "kbrequest.target"
99 #define SPECIAL_SIGPWR_TARGET "sigpwr.target"
100 #define SPECIAL_CTRL_ALT_DEL_TARGET "ctrl-alt-del.target"
101
102 #define SPECIAL_LOCAL_FS_TARGET "local-fs.target"         /* LSB's $local_fs */
103 #define SPECIAL_REMOTE_FS_TARGET "remote-fs.target"       /* LSB's $remote_fs */
104 #define SPECIAL_SWAP_TARGET "swap.target"
105 #define SPECIAL_NETWORK_TARGET "network.target"           /* LSB's $network */
106 #define SPECIAL_NSS_LOOKUP_TARGET "nss-lookup.target"     /* LSB's $named */
107 #define SPECIAL_RPCBIND_TARGET "rpcbind.target"           /* LSB's $portmap */
108 #define SPECIAL_SYSLOG_TARGET "syslog.target"             /* LSB's $syslog; Should pull in syslog.socket or syslog.service */
109 #define SPECIAL_RTC_SET_TARGET "rtc-set.target"           /* LSB's $time */
110 #define SPECIAL_DISPLAY_MANAGER_SERVICE "display-manager.service"       /* Debian's $x-display-manager */
111 #define SPECIAL_MAIL_TRANSFER_AGENT_TARGET "mail-transfer-agent.target" /* Debian's $mail-{transport|transfer-agent */
112 #define SPECIAL_BASIC_TARGET "basic.target"
113 #define SPECIAL_SYSINIT_TARGET "sysinit.target"
114 #define SPECIAL_RESCUE_TARGET "rescue.target"
115 #define SPECIAL_EXIT_SERVICE "exit.service"
116
117 #ifndef SPECIAL_DBUS_SERVICE
118 #define SPECIAL_DBUS_SERVICE "dbus.service"
119 #endif
120
121 #ifndef SPECIAL_SYSLOG_SERVICE
122 #define SPECIAL_SYSLOG_SERVICE "syslog.service"
123 #endif
124
125 /* For SysV compatibility. Usually an alias for a saner target. On
126  * SysV-free systems this doesn't exist. */
127 #define SPECIAL_RUNLEVEL0_TARGET "runlevel0.target"
128 #define SPECIAL_RUNLEVEL1_TARGET "runlevel1.target"
129 #define SPECIAL_RUNLEVEL2_TARGET "runlevel2.target"
130 #define SPECIAL_RUNLEVEL3_TARGET "runlevel3.target"
131 #define SPECIAL_RUNLEVEL4_TARGET "runlevel4.target"
132 #define SPECIAL_RUNLEVEL5_TARGET "runlevel5.target"
133 #define SPECIAL_RUNLEVEL6_TARGET "runlevel6.target"
134
135 struct Manager {
136         uint32_t current_job_id;
137
138         /* Note that the set of units we know of is allowed to be
139          * incosistent. However the subset of it that is loaded may
140          * not, and the list of jobs may neither. */
141
142         /* Active jobs and units */
143         Hashmap *units;  /* name string => Unit object n:1 */
144         Hashmap *jobs;   /* job id => Job object 1:1 */
145
146         /* To make it easy to iterate through the units of a specific
147          * type we maintain a per type linked list */
148         LIST_HEAD(Meta, units_per_type[_UNIT_TYPE_MAX]);
149
150         /* Units that need to be loaded */
151         LIST_HEAD(Meta, load_queue); /* this is actually more a stack than a queue, but uh. */
152
153         /* Jobs that need to be run */
154         LIST_HEAD(Job, run_queue);   /* more a stack than a queue, too */
155
156         /* Units and jobs that have not yet been announced via
157          * D-Bus. When something about a job changes it is added here
158          * if it is not in there yet. This allows easy coalescing of
159          * D-Bus change signals. */
160         LIST_HEAD(Meta, dbus_unit_queue);
161         LIST_HEAD(Job, dbus_job_queue);
162
163         /* Units to remove */
164         LIST_HEAD(Meta, cleanup_queue);
165
166         /* Units to check when doing GC */
167         LIST_HEAD(Meta, gc_queue);
168
169         /* Jobs to be added */
170         Hashmap *transaction_jobs;      /* Unit object => Job object list 1:1 */
171         JobDependency *transaction_anchor;
172
173         Hashmap *watch_pids;  /* pid => Unit object n:1 */
174
175         Watch notify_watch;
176         Watch signal_watch;
177
178         int epoll_fd;
179
180         unsigned n_snapshots;
181
182         LookupPaths lookup_paths;
183
184         char **environment;
185
186         timestamp startup_timestamp;
187
188         /* Data specific to the device subsystem */
189         struct udev* udev;
190         struct udev_monitor* udev_monitor;
191         Watch udev_watch;
192
193         /* Data specific to the mount subsystem */
194         FILE *proc_self_mountinfo;
195         Watch mount_watch;
196
197         /* Data specific to the swap filesystem */
198         FILE *proc_swaps;
199
200         /* Data specific to the D-Bus subsystem */
201         DBusConnection *api_bus, *system_bus;
202         Set *subscribed;
203         DBusMessage *queued_message; /* This is used during reloading:
204                                       * before the reload we queue the
205                                       * reply message here, and
206                                       * afterwards we send it */
207
208         Hashmap *watch_bus;  /* D-Bus names => Unit object n:1 */
209         int32_t name_data_slot;
210
211         /* Data specific to the Automount subsystem */
212         int dev_autofs_fd;
213
214         /* Data specific to the cgroup subsystem */
215         Hashmap *cgroup_bondings; /* path string => CGroupBonding object 1:n */
216         char *cgroup_controller;
217         char *cgroup_hierarchy;
218
219         usec_t gc_queue_timestamp;
220         int gc_marker;
221         unsigned n_in_gc_queue;
222
223         /* Flags */
224         ManagerRunningAs running_as;
225         ManagerExitCode exit_code:4;
226
227         bool dispatching_load_queue:1;
228         bool dispatching_run_queue:1;
229         bool dispatching_dbus_queue:1;
230
231         bool request_api_bus_dispatch:1;
232         bool request_system_bus_dispatch:1;
233
234         bool utmp_reboot_written:1;
235
236         bool confirm_spawn:1;
237 };
238
239 int manager_new(ManagerRunningAs running_as, bool confirm_spawn, Manager **m);
240 void manager_free(Manager *m);
241
242 int manager_enumerate(Manager *m);
243 int manager_coldplug(Manager *m);
244 int manager_startup(Manager *m, FILE *serialization, FDSet *fds);
245
246 Job *manager_get_job(Manager *m, uint32_t id);
247 Unit *manager_get_unit(Manager *m, const char *name);
248
249 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u);
250 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j);
251
252 int manager_load_unit_prepare(Manager *m, const char *name, const char *path, Unit **_ret);
253 int manager_load_unit(Manager *m, const char *name, const char *path, Unit **_ret);
254
255 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool force, Job **_ret);
256 int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool force, Job **_ret);
257
258 void manager_dump_units(Manager *s, FILE *f, const char *prefix);
259 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix);
260
261 void manager_transaction_unlink_job(Manager *m, Job *j, bool delete_dependencies);
262
263 void manager_clear_jobs(Manager *m);
264
265 unsigned manager_dispatch_load_queue(Manager *m);
266 unsigned manager_dispatch_run_queue(Manager *m);
267 unsigned manager_dispatch_dbus_queue(Manager *m);
268
269 int manager_loop(Manager *m);
270
271 void manager_write_utmp_reboot(Manager *m);
272 void manager_write_utmp_runlevel(Manager *m, Unit *t);
273
274 void manager_dispatch_bus_name_owner_changed(Manager *m, const char *name, const char* old_owner, const char *new_owner);
275 void manager_dispatch_bus_query_pid_done(Manager *m, const char *name, pid_t pid);
276
277 int manager_open_serialization(FILE **_f);
278
279 int manager_serialize(Manager *m, FILE *f, FDSet *fds);
280 int manager_deserialize(Manager *m, FILE *f, FDSet *fds);
281
282 int manager_reload(Manager *m);
283
284 const char *manager_running_as_to_string(ManagerRunningAs i);
285 ManagerRunningAs manager_running_as_from_string(const char *s);
286
287 #endif