Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : #pragma once
3 :
4 : #include <stdbool.h>
5 : #include <stdio.h>
6 :
7 : #include "sd-bus.h"
8 : #include "sd-device.h"
9 : #include "sd-event.h"
10 :
11 : #include "cgroup-util.h"
12 : #include "fdset.h"
13 : #include "hashmap.h"
14 : #include "ip-address-access.h"
15 : #include "list.h"
16 : #include "prioq.h"
17 : #include "ratelimit.h"
18 :
19 : struct libmnt_monitor;
20 : typedef struct Unit Unit;
21 :
22 : /* Enforce upper limit how many names we allow */
23 : #define MANAGER_MAX_NAMES 131072 /* 128K */
24 :
25 : typedef struct Manager Manager;
26 :
27 : /* An externally visible state. We don't actually maintain this as state variable, but derive it from various fields
28 : * when requested */
29 : typedef enum ManagerState {
30 : MANAGER_INITIALIZING,
31 : MANAGER_STARTING,
32 : MANAGER_RUNNING,
33 : MANAGER_DEGRADED,
34 : MANAGER_MAINTENANCE,
35 : MANAGER_STOPPING,
36 : _MANAGER_STATE_MAX,
37 : _MANAGER_STATE_INVALID = -1
38 : } ManagerState;
39 :
40 : typedef enum ManagerObjective {
41 : MANAGER_OK,
42 : MANAGER_EXIT,
43 : MANAGER_RELOAD,
44 : MANAGER_REEXECUTE,
45 : MANAGER_REBOOT,
46 : MANAGER_POWEROFF,
47 : MANAGER_HALT,
48 : MANAGER_KEXEC,
49 : MANAGER_SWITCH_ROOT,
50 : _MANAGER_OBJECTIVE_MAX,
51 : _MANAGER_OBJECTIVE_INVALID = -1
52 : } ManagerObjective;
53 :
54 : typedef enum StatusType {
55 : STATUS_TYPE_EPHEMERAL,
56 : STATUS_TYPE_NORMAL,
57 : STATUS_TYPE_EMERGENCY,
58 : } StatusType;
59 :
60 : typedef enum OOMPolicy {
61 : OOM_CONTINUE, /* The kernel kills the process it wants to kill, and that's it */
62 : OOM_STOP, /* The kernel kills the process it wants to kill, and we stop the unit */
63 : OOM_KILL, /* The kernel kills the process it wants to kill, and all others in the unit, and we stop the unit */
64 : _OOM_POLICY_MAX,
65 : _OOM_POLICY_INVALID = -1
66 : } OOMPolicy;
67 :
68 : /* Notes:
69 : * 1. TIMESTAMP_FIRMWARE, TIMESTAMP_LOADER, TIMESTAMP_KERNEL, TIMESTAMP_INITRD,
70 : * TIMESTAMP_SECURITY_START, and TIMESTAMP_SECURITY_FINISH are set only when
71 : * the manager is system and not running under container environment.
72 : *
73 : * 2. The monotonic timestamp of TIMESTAMP_KERNEL is always zero.
74 : *
75 : * 3. The realtime timestamp of TIMESTAMP_KERNEL will be unset if the system does not
76 : * have RTC.
77 : *
78 : * 4. TIMESTAMP_FIRMWARE and TIMESTAMP_LOADER will be unset if the system does not
79 : * have RTC, or systemd is built without EFI support.
80 : *
81 : * 5. The monotonic timestamps of TIMESTAMP_FIRMWARE and TIMESTAMP_LOADER are stored as
82 : * negative of the actual value.
83 : *
84 : * 6. TIMESTAMP_USERSPACE is the timestamp of when the manager was started.
85 : *
86 : * 7. TIMESTAMP_INITRD_* are set only when the system is booted with an initrd.
87 : */
88 :
89 : typedef enum ManagerTimestamp {
90 : MANAGER_TIMESTAMP_FIRMWARE,
91 : MANAGER_TIMESTAMP_LOADER,
92 : MANAGER_TIMESTAMP_KERNEL,
93 : MANAGER_TIMESTAMP_INITRD,
94 : MANAGER_TIMESTAMP_USERSPACE,
95 : MANAGER_TIMESTAMP_FINISH,
96 :
97 : MANAGER_TIMESTAMP_SECURITY_START,
98 : MANAGER_TIMESTAMP_SECURITY_FINISH,
99 : MANAGER_TIMESTAMP_GENERATORS_START,
100 : MANAGER_TIMESTAMP_GENERATORS_FINISH,
101 : MANAGER_TIMESTAMP_UNITS_LOAD_START,
102 : MANAGER_TIMESTAMP_UNITS_LOAD_FINISH,
103 :
104 : MANAGER_TIMESTAMP_INITRD_SECURITY_START,
105 : MANAGER_TIMESTAMP_INITRD_SECURITY_FINISH,
106 : MANAGER_TIMESTAMP_INITRD_GENERATORS_START,
107 : MANAGER_TIMESTAMP_INITRD_GENERATORS_FINISH,
108 : MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_START,
109 : MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH,
110 : _MANAGER_TIMESTAMP_MAX,
111 : _MANAGER_TIMESTAMP_INVALID = -1,
112 : } ManagerTimestamp;
113 :
114 : #include "execute.h"
115 : #include "job.h"
116 : #include "path-lookup.h"
117 : #include "show-status.h"
118 : #include "unit-name.h"
119 :
120 : typedef enum ManagerTestRunFlags {
121 : MANAGER_TEST_NORMAL = 0, /* run normally */
122 : MANAGER_TEST_RUN_MINIMAL = 1 << 0, /* create basic data structures */
123 : MANAGER_TEST_RUN_BASIC = 1 << 1, /* interact with the environment */
124 : MANAGER_TEST_RUN_ENV_GENERATORS = 1 << 2, /* also run env generators */
125 : MANAGER_TEST_RUN_GENERATORS = 1 << 3, /* also run unit generators */
126 : MANAGER_TEST_FULL = MANAGER_TEST_RUN_BASIC | MANAGER_TEST_RUN_ENV_GENERATORS | MANAGER_TEST_RUN_GENERATORS,
127 : } ManagerTestRunFlags;
128 :
129 : assert_cc((MANAGER_TEST_FULL & UINT8_MAX) == MANAGER_TEST_FULL);
130 :
131 : struct Manager {
132 : /* Note that the set of units we know of is allowed to be
133 : * inconsistent. However the subset of it that is loaded may
134 : * not, and the list of jobs may neither. */
135 :
136 : /* Active jobs and units */
137 : Hashmap *units; /* name string => Unit object n:1 */
138 : Hashmap *units_by_invocation_id;
139 : Hashmap *jobs; /* job id => Job object 1:1 */
140 :
141 : /* To make it easy to iterate through the units of a specific
142 : * type we maintain a per type linked list */
143 : LIST_HEAD(Unit, units_by_type[_UNIT_TYPE_MAX]);
144 :
145 : /* Units that need to be loaded */
146 : LIST_HEAD(Unit, load_queue); /* this is actually more a stack than a queue, but uh. */
147 :
148 : /* Jobs that need to be run */
149 : struct Prioq *run_queue;
150 :
151 : /* Units and jobs that have not yet been announced via
152 : * D-Bus. When something about a job changes it is added here
153 : * if it is not in there yet. This allows easy coalescing of
154 : * D-Bus change signals. */
155 : LIST_HEAD(Unit, dbus_unit_queue);
156 : LIST_HEAD(Job, dbus_job_queue);
157 :
158 : /* Units to remove */
159 : LIST_HEAD(Unit, cleanup_queue);
160 :
161 : /* Units and jobs to check when doing GC */
162 : LIST_HEAD(Unit, gc_unit_queue);
163 : LIST_HEAD(Job, gc_job_queue);
164 :
165 : /* Units that should be realized */
166 : LIST_HEAD(Unit, cgroup_realize_queue);
167 :
168 : /* Units whose cgroup ran empty */
169 : LIST_HEAD(Unit, cgroup_empty_queue);
170 :
171 : /* Units whose memory.event fired */
172 : LIST_HEAD(Unit, cgroup_oom_queue);
173 :
174 : /* Target units whose default target dependencies haven't been set yet */
175 : LIST_HEAD(Unit, target_deps_queue);
176 :
177 : /* Units that might be subject to StopWhenUnneeded= clean-up */
178 : LIST_HEAD(Unit, stop_when_unneeded_queue);
179 :
180 : sd_event *event;
181 :
182 : /* This maps PIDs we care about to units that are interested in. We allow multiple units to he interested in
183 : * the same PID and multiple PIDs to be relevant to the same unit. Since in most cases only a single unit will
184 : * be interested in the same PID we use a somewhat special encoding here: the first unit interested in a PID is
185 : * stored directly in the hashmap, keyed by the PID unmodified. If there are other units interested too they'll
186 : * be stored in a NULL-terminated array, and keyed by the negative PID. This is safe as pid_t is signed and
187 : * negative PIDs are not used for regular processes but process groups, which we don't care about in this
188 : * context, but this allows us to use the negative range for our own purposes. */
189 : Hashmap *watch_pids; /* pid => unit as well as -pid => array of units */
190 :
191 : /* A set contains all units which cgroup should be refreshed after startup */
192 : Set *startup_units;
193 :
194 : /* A set which contains all currently failed units */
195 : Set *failed_units;
196 :
197 : sd_event_source *run_queue_event_source;
198 :
199 : char *notify_socket;
200 : int notify_fd;
201 : sd_event_source *notify_event_source;
202 :
203 : int cgroups_agent_fd;
204 : sd_event_source *cgroups_agent_event_source;
205 :
206 : int signal_fd;
207 : sd_event_source *signal_event_source;
208 :
209 : sd_event_source *sigchld_event_source;
210 :
211 : int time_change_fd;
212 : sd_event_source *time_change_event_source;
213 :
214 : sd_event_source *timezone_change_event_source;
215 :
216 : sd_event_source *jobs_in_progress_event_source;
217 :
218 : int user_lookup_fds[2];
219 : sd_event_source *user_lookup_event_source;
220 :
221 : sd_event_source *sync_bus_names_event_source;
222 :
223 : UnitFileScope unit_file_scope;
224 : LookupPaths lookup_paths;
225 : Hashmap *unit_id_map;
226 : Hashmap *unit_name_map;
227 : Set *unit_path_cache;
228 : usec_t unit_cache_mtime;
229 :
230 : char **transient_environment; /* The environment, as determined from config files, kernel cmdline and environment generators */
231 : char **client_environment; /* Environment variables created by clients through the bus API */
232 :
233 : usec_t runtime_watchdog;
234 : usec_t reboot_watchdog;
235 : usec_t kexec_watchdog;
236 :
237 : dual_timestamp timestamps[_MANAGER_TIMESTAMP_MAX];
238 :
239 : /* Data specific to the device subsystem */
240 : sd_device_monitor *device_monitor;
241 : Hashmap *devices_by_sysfs;
242 :
243 : /* Data specific to the mount subsystem */
244 : struct libmnt_monitor *mount_monitor;
245 : sd_event_source *mount_event_source;
246 :
247 : /* Data specific to the swap filesystem */
248 : FILE *proc_swaps;
249 : sd_event_source *swap_event_source;
250 : Hashmap *swaps_by_devnode;
251 :
252 : /* Data specific to the D-Bus subsystem */
253 : sd_bus *api_bus, *system_bus;
254 : Set *private_buses;
255 : int private_listen_fd;
256 : sd_event_source *private_listen_event_source;
257 :
258 : /* Contains all the clients that are subscribed to signals via
259 : the API bus. Note that private bus connections are always
260 : considered subscribes, since they last for very short only,
261 : and it is much simpler that way. */
262 : sd_bus_track *subscribed;
263 : char **deserialized_subscribed;
264 :
265 : /* This is used during reloading: before the reload we queue
266 : * the reply message here, and afterwards we send it */
267 : sd_bus_message *pending_reload_message;
268 :
269 : Hashmap *watch_bus; /* D-Bus names => Unit object n:1 */
270 :
271 : bool send_reloading_done;
272 :
273 : uint32_t current_job_id;
274 : uint32_t default_unit_job_id;
275 :
276 : /* Data specific to the Automount subsystem */
277 : int dev_autofs_fd;
278 :
279 : /* Data specific to the cgroup subsystem */
280 : Hashmap *cgroup_unit;
281 : CGroupMask cgroup_supported;
282 : char *cgroup_root;
283 :
284 : /* Notifications from cgroups, when the unified hierarchy is used is done via inotify. */
285 : int cgroup_inotify_fd;
286 : sd_event_source *cgroup_inotify_event_source;
287 :
288 : /* Maps for finding the unit for each inotify watch descriptor for the cgroup.events and
289 : * memory.events cgroupv2 attributes. */
290 : Hashmap *cgroup_control_inotify_wd_unit;
291 : Hashmap *cgroup_memory_inotify_wd_unit;
292 :
293 : /* A defer event for handling cgroup empty events and processing them after SIGCHLD in all cases. */
294 : sd_event_source *cgroup_empty_event_source;
295 : sd_event_source *cgroup_oom_event_source;
296 :
297 : /* Make sure the user cannot accidentally unmount our cgroup
298 : * file system */
299 : int pin_cgroupfs_fd;
300 :
301 : unsigned gc_marker;
302 :
303 : /* The stat() data the last time we saw /etc/localtime */
304 : usec_t etc_localtime_mtime;
305 : bool etc_localtime_accessible:1;
306 :
307 : ManagerObjective objective:5;
308 :
309 : /* Flags */
310 : bool dispatching_load_queue:1;
311 :
312 : bool taint_usr:1;
313 :
314 : /* Have we already sent out the READY=1 notification? */
315 : bool ready_sent:1;
316 :
317 : /* Have we already printed the taint line if necessary? */
318 : bool taint_logged:1;
319 :
320 : /* Have we ever changed the "kernel.pid_max" sysctl? */
321 : bool sysctl_pid_max_changed:1;
322 :
323 : ManagerTestRunFlags test_run_flags:8;
324 :
325 : /* If non-zero, exit with the following value when the systemd
326 : * process terminate. Useful for containers: systemd-nspawn could get
327 : * the return value. */
328 : uint8_t return_value;
329 :
330 : ShowStatus show_status;
331 : StatusUnitFormat status_unit_format;
332 : char *confirm_spawn;
333 : bool no_console_output;
334 : bool service_watchdogs;
335 :
336 : ExecOutput default_std_output, default_std_error;
337 :
338 : usec_t default_restart_usec, default_timeout_start_usec, default_timeout_stop_usec;
339 : usec_t default_timeout_abort_usec;
340 : bool default_timeout_abort_set;
341 :
342 : usec_t default_start_limit_interval;
343 : unsigned default_start_limit_burst;
344 :
345 : bool default_cpu_accounting;
346 : bool default_memory_accounting;
347 : bool default_io_accounting;
348 : bool default_blockio_accounting;
349 : bool default_tasks_accounting;
350 : bool default_ip_accounting;
351 :
352 : uint64_t default_tasks_max;
353 : usec_t default_timer_accuracy_usec;
354 :
355 : OOMPolicy default_oom_policy;
356 :
357 : int original_log_level;
358 : LogTarget original_log_target;
359 : bool log_level_overridden:1;
360 : bool log_target_overridden:1;
361 :
362 : struct rlimit *rlimit[_RLIMIT_MAX];
363 :
364 : /* non-zero if we are reloading or reexecuting, */
365 : int n_reloading;
366 :
367 : unsigned n_installed_jobs;
368 : unsigned n_failed_jobs;
369 :
370 : /* Jobs in progress watching */
371 : unsigned n_running_jobs;
372 : unsigned n_on_console;
373 : unsigned jobs_in_progress_iteration;
374 :
375 : /* Do we have any outstanding password prompts? */
376 : int have_ask_password;
377 : int ask_password_inotify_fd;
378 : sd_event_source *ask_password_event_source;
379 :
380 : /* Type=idle pipes */
381 : int idle_pipe[4];
382 : sd_event_source *idle_pipe_event_source;
383 :
384 : char *switch_root;
385 : char *switch_root_init;
386 :
387 : /* This maps all possible path prefixes to the units needing
388 : * them. It's a hashmap with a path string as key and a Set as
389 : * value where Unit objects are contained. */
390 : Hashmap *units_requiring_mounts_for;
391 :
392 : /* Used for processing polkit authorization responses */
393 : Hashmap *polkit_registry;
394 :
395 : /* Dynamic users/groups, indexed by their name */
396 : Hashmap *dynamic_users;
397 :
398 : /* Keep track of all UIDs and GIDs any of our services currently use. This is useful for the RemoveIPC= logic. */
399 : Hashmap *uid_refs;
400 : Hashmap *gid_refs;
401 :
402 : /* ExecRuntime, indexed by their owner unit id */
403 : Hashmap *exec_runtime_by_id;
404 :
405 : /* When the user hits C-A-D more than 7 times per 2s, do something immediately... */
406 : RateLimit ctrl_alt_del_ratelimit;
407 : EmergencyAction cad_burst_action;
408 :
409 : const char *unit_log_field;
410 : const char *unit_log_format_string;
411 :
412 : const char *invocation_log_field;
413 : const char *invocation_log_format_string;
414 :
415 : int first_boot; /* tri-state */
416 :
417 : /* Prefixes of e.g. RuntimeDirectory= */
418 : char *prefix[_EXEC_DIRECTORY_TYPE_MAX];
419 :
420 : /* Used in the SIGCHLD and sd_notify() message invocation logic to avoid that we dispatch the same event
421 : * multiple times on the same unit. */
422 : unsigned sigchldgen;
423 : unsigned notifygen;
424 :
425 : bool honor_device_enumeration;
426 : };
427 :
428 0 : static inline usec_t manager_default_timeout_abort_usec(Manager *m) {
429 0 : assert(m);
430 0 : return m->default_timeout_abort_set ? m->default_timeout_abort_usec : m->default_timeout_stop_usec;
431 : }
432 :
433 : #define MANAGER_IS_SYSTEM(m) ((m)->unit_file_scope == UNIT_FILE_SYSTEM)
434 : #define MANAGER_IS_USER(m) ((m)->unit_file_scope != UNIT_FILE_SYSTEM)
435 :
436 : #define MANAGER_IS_RELOADING(m) ((m)->n_reloading > 0)
437 :
438 : #define MANAGER_IS_FINISHED(m) (dual_timestamp_is_set((m)->timestamps + MANAGER_TIMESTAMP_FINISH))
439 :
440 : /* The objective is set to OK as soon as we enter the main loop, and set otherwise as soon as we are done with it */
441 : #define MANAGER_IS_RUNNING(m) ((m)->objective == MANAGER_OK)
442 :
443 : #define MANAGER_IS_TEST_RUN(m) ((m)->test_run_flags != 0)
444 :
445 : int manager_new(UnitFileScope scope, ManagerTestRunFlags test_run_flags, Manager **m);
446 : Manager* manager_free(Manager *m);
447 23 : DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
448 :
449 : int manager_startup(Manager *m, FILE *serialization, FDSet *fds);
450 :
451 : Job *manager_get_job(Manager *m, uint32_t id);
452 : Unit *manager_get_unit(Manager *m, const char *name);
453 :
454 : int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j);
455 :
456 : int manager_load_unit_prepare(Manager *m, const char *name, const char *path, sd_bus_error *e, Unit **_ret);
457 : int manager_load_unit(Manager *m, const char *name, const char *path, sd_bus_error *e, Unit **_ret);
458 : int manager_load_startable_unit_or_warn(Manager *m, const char *name, const char *path, Unit **ret);
459 : int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u);
460 :
461 : int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, Set *affected_jobs, sd_bus_error *e, Job **_ret);
462 : int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, sd_bus_error *e, Job **_ret);
463 : int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, Job **ret);
464 : int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error *e);
465 :
466 : void manager_dump_units(Manager *s, FILE *f, const char *prefix);
467 : void manager_dump_jobs(Manager *s, FILE *f, const char *prefix);
468 : void manager_dump(Manager *s, FILE *f, const char *prefix);
469 : int manager_get_dump_string(Manager *m, char **ret);
470 :
471 : void manager_clear_jobs(Manager *m);
472 :
473 : void manager_unwatch_pid(Manager *m, pid_t pid);
474 :
475 : unsigned manager_dispatch_load_queue(Manager *m);
476 :
477 : int manager_default_environment(Manager *m);
478 : int manager_transient_environment_add(Manager *m, char **plus);
479 : int manager_client_environment_modify(Manager *m, char **minus, char **plus);
480 : int manager_get_effective_environment(Manager *m, char ***ret);
481 :
482 : int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit);
483 :
484 : int manager_loop(Manager *m);
485 :
486 : int manager_open_serialization(Manager *m, FILE **_f);
487 :
488 : int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool switching_root);
489 : int manager_deserialize(Manager *m, FILE *f, FDSet *fds);
490 :
491 : int manager_reload(Manager *m);
492 :
493 : void manager_reset_failed(Manager *m);
494 :
495 : void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success);
496 : void manager_send_unit_plymouth(Manager *m, Unit *u);
497 :
498 : bool manager_unit_inactive_or_pending(Manager *m, const char *name);
499 :
500 : void manager_check_finished(Manager *m);
501 :
502 : void manager_recheck_dbus(Manager *m);
503 : void manager_recheck_journal(Manager *m);
504 :
505 : void manager_set_show_status(Manager *m, ShowStatus mode);
506 : void manager_set_first_boot(Manager *m, bool b);
507 :
508 : void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) _printf_(4,5);
509 : void manager_flip_auto_status(Manager *m, bool enable);
510 :
511 : Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path);
512 :
513 : ManagerState manager_state(Manager *m);
514 :
515 : int manager_update_failed_units(Manager *m, Unit *u, bool failed);
516 :
517 : void manager_unref_uid(Manager *m, uid_t uid, bool destroy_now);
518 : int manager_ref_uid(Manager *m, uid_t uid, bool clean_ipc);
519 :
520 : void manager_unref_gid(Manager *m, gid_t gid, bool destroy_now);
521 : int manager_ref_gid(Manager *m, gid_t gid, bool destroy_now);
522 :
523 : void manager_vacuum_uid_refs(Manager *m);
524 : void manager_vacuum_gid_refs(Manager *m);
525 :
526 : void manager_serialize_uid_refs(Manager *m, FILE *f);
527 : void manager_deserialize_uid_refs_one(Manager *m, const char *value);
528 :
529 : void manager_serialize_gid_refs(Manager *m, FILE *f);
530 : void manager_deserialize_gid_refs_one(Manager *m, const char *value);
531 :
532 : char *manager_taint_string(Manager *m);
533 :
534 : void manager_ref_console(Manager *m);
535 : void manager_unref_console(Manager *m);
536 :
537 : void manager_override_log_level(Manager *m, int level);
538 : void manager_restore_original_log_level(Manager *m);
539 :
540 : void manager_override_log_target(Manager *m, LogTarget target);
541 : void manager_restore_original_log_target(Manager *m);
542 :
543 : const char *manager_state_to_string(ManagerState m) _const_;
544 : ManagerState manager_state_from_string(const char *s) _pure_;
545 :
546 : const char *manager_get_confirm_spawn(Manager *m);
547 : bool manager_is_confirm_spawn_disabled(Manager *m);
548 : void manager_disable_confirm_spawn(void);
549 :
550 : const char *manager_timestamp_to_string(ManagerTimestamp m) _const_;
551 : ManagerTimestamp manager_timestamp_from_string(const char *s) _pure_;
552 : ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s);
553 :
554 : const char* oom_policy_to_string(OOMPolicy i) _const_;
555 : OOMPolicy oom_policy_from_string(const char *s) _pure_;
|