]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/unit.h
service: don't warn if the pidfile still exists after SIGCHLD
[thirdparty/systemd.git] / src / unit.h
CommitLineData
03467c88 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
87f0e418
LP
2
3#ifndef foounithfoo
4#define foounithfoo
5
a7334b09
LP
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
87f0e418
LP
25#include <stdbool.h>
26#include <stdlib.h>
27
28typedef union Unit Unit;
29typedef struct Meta Meta;
30typedef struct UnitVTable UnitVTable;
31typedef enum UnitType UnitType;
32typedef enum UnitLoadState UnitLoadState;
33typedef enum UnitActiveState UnitActiveState;
34typedef enum UnitDependency UnitDependency;
35
87f0e418
LP
36#include "set.h"
37#include "util.h"
38#include "list.h"
39#include "socket-util.h"
40#include "execute.h"
52661efd 41#include "condition.h"
a4375746 42#include "install.h"
87f0e418 43
87f0e418
LP
44enum UnitType {
45 UNIT_SERVICE = 0,
87f0e418
LP
46 UNIT_SOCKET,
47 UNIT_TARGET,
48 UNIT_DEVICE,
49 UNIT_MOUNT,
50 UNIT_AUTOMOUNT,
51 UNIT_SNAPSHOT,
41447faf 52 UNIT_TIMER,
07b0b134 53 UNIT_SWAP,
01f78473 54 UNIT_PATH,
87f0e418 55 _UNIT_TYPE_MAX,
50159e6a 56 _UNIT_TYPE_INVALID = -1
87f0e418
LP
57};
58
59enum UnitLoadState {
60 UNIT_STUB,
61 UNIT_LOADED,
fdf20a31 62 UNIT_ERROR,
23a177ef 63 UNIT_MERGED,
6daf4f90 64 UNIT_MASKED,
94f04347
LP
65 _UNIT_LOAD_STATE_MAX,
66 _UNIT_LOAD_STATE_INVALID = -1
87f0e418
LP
67};
68
69enum UnitActiveState {
70 UNIT_ACTIVE,
032ff4af 71 UNIT_RELOADING,
87f0e418 72 UNIT_INACTIVE,
fdf20a31 73 UNIT_FAILED,
87f0e418
LP
74 UNIT_ACTIVATING,
75 UNIT_DEACTIVATING,
94f04347
LP
76 _UNIT_ACTIVE_STATE_MAX,
77 _UNIT_ACTIVE_STATE_INVALID = -1
87f0e418
LP
78};
79
80static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
032ff4af 81 return t == UNIT_ACTIVE || t == UNIT_RELOADING;
87f0e418
LP
82}
83
84static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
032ff4af 85 return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_RELOADING;
87f0e418
LP
86}
87
88static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
fdf20a31 89 return t == UNIT_INACTIVE || t == UNIT_FAILED || t == UNIT_DEACTIVATING;
6124958c
LP
90}
91
fdf20a31
MM
92static inline bool UNIT_IS_INACTIVE_OR_FAILED(UnitActiveState t) {
93 return t == UNIT_INACTIVE || t == UNIT_FAILED;
87f0e418
LP
94}
95
96enum UnitDependency {
97 /* Positive dependencies */
98 UNIT_REQUIRES,
9e2f7c11 99 UNIT_REQUIRES_OVERRIDABLE,
87f0e418 100 UNIT_REQUISITE,
9e2f7c11
LP
101 UNIT_REQUISITE_OVERRIDABLE,
102 UNIT_WANTS,
b81884e7 103 UNIT_BIND_TO,
87f0e418
LP
104
105 /* Inverse of the above */
9e2f7c11 106 UNIT_REQUIRED_BY, /* inverse of 'requires' and 'requisite' is 'required_by' */
f14e15f8 107 UNIT_REQUIRED_BY_OVERRIDABLE, /* inverse of 'requires_overridable' and 'requisite_overridable' is 'soft_required_by' */
9e2f7c11 108 UNIT_WANTED_BY, /* inverse of 'wants' */
b81884e7 109 UNIT_BOUND_BY, /* inverse of 'bind_to' */
87f0e418
LP
110
111 /* Negative dependencies */
69dd2852
LP
112 UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicted_by' */
113 UNIT_CONFLICTED_BY,
87f0e418
LP
114
115 /* Order */
701cc384 116 UNIT_BEFORE, /* inverse of 'before' is 'after' and vice versa */
87f0e418
LP
117 UNIT_AFTER,
118
5de9682c
LP
119 /* On Failure */
120 UNIT_ON_FAILURE,
121
701cc384
LP
122 /* Reference information for GC logic */
123 UNIT_REFERENCES, /* Inverse of 'references' is 'referenced_by' */
124 UNIT_REFERENCED_BY,
125
87f0e418
LP
126 _UNIT_DEPENDENCY_MAX,
127 _UNIT_DEPENDENCY_INVALID = -1
128};
129
ef734fd6
LP
130#include "manager.h"
131#include "job.h"
8e274523 132#include "cgroup.h"
ab1f0633 133#include "cgroup-attr.h"
ef734fd6 134
87f0e418
LP
135struct Meta {
136 Manager *manager;
23a177ef 137
87f0e418
LP
138 UnitType type;
139 UnitLoadState load_state;
23a177ef 140 Unit *merged_into;
87f0e418
LP
141
142 char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
9e2f7c11 143 char *instance;
87f0e418
LP
144
145 Set *names;
146 Set *dependencies[_UNIT_DEPENDENCY_MAX];
147
148 char *description;
faf919f1 149
6be1e7d5 150 char *fragment_path; /* if loaded from a config file this is the primary path to it */
45fb0699 151 usec_t fragment_mtime;
87f0e418
LP
152
153 /* If there is something to do with this unit, then this is
154 * the job for it */
155 Job *job;
156
faf919f1
LP
157 usec_t job_timeout;
158
52661efd
LP
159 /* Conditions to check */
160 LIST_HEAD(Condition, conditions);
161
90bbc946
LP
162 dual_timestamp condition_timestamp;
163
63983207
LP
164 dual_timestamp inactive_exit_timestamp;
165 dual_timestamp active_enter_timestamp;
166 dual_timestamp active_exit_timestamp;
167 dual_timestamp inactive_enter_timestamp;
87f0e418 168
8e274523
LP
169 /* Counterparts in the cgroup filesystem */
170 CGroupBonding *cgroup_bondings;
ab1f0633 171 CGroupAttribute *cgroup_attributes;
8e274523 172
ef734fd6 173 /* Per type list */
ab5c3e3f 174 LIST_FIELDS(Meta, units_by_type);
c1e1601e 175
701cc384
LP
176 /* Load queue */
177 LIST_FIELDS(Meta, load_queue);
178
c1e1601e
LP
179 /* D-Bus queue */
180 LIST_FIELDS(Meta, dbus_queue);
23a177ef
LP
181
182 /* Cleanup queue */
183 LIST_FIELDS(Meta, cleanup_queue);
9d58f1db 184
701cc384
LP
185 /* GC queue */
186 LIST_FIELDS(Meta, gc_queue);
187
188 /* Used during GC sweeps */
eced69b3 189 unsigned gc_marker;
701cc384 190
7fab9d01
LP
191 /* When deserializing, temporarily store the job type for this
192 * unit here, if there was a job scheduled */
193 int deserialized_job; /* This is actually of type JobType */
194
8821a00f
LP
195 /* Error code when we didn't manage to load the unit (negative) */
196 int load_error;
197
a4375746
LP
198 /* Cached unit file state */
199 UnitFileState unit_file_state;
200
9d58f1db
LP
201 /* Garbage collect us we nobody wants or requires us anymore */
202 bool stop_when_unneeded;
203
35b8ca3a 204 /* Create default dependencies */
a40eb732
LP
205 bool default_dependencies;
206
b5e9dba8
LP
207 /* Refuse manual starting, allow starting only indirectly via dependency. */
208 bool refuse_manual_start;
209
210 /* Don't allow the user to stop this unit manually, allow stopping only indirectly via dependency. */
211 bool refuse_manual_stop;
212
2528a7a6
LP
213 /* Allow isolation requests */
214 bool allow_isolate;
215
222ae6a8
LP
216 /* Isolate OnFailure unit */
217 bool on_failure_isolate;
218
c8f4d764
LP
219 /* Ignore this unit when isolating */
220 bool ignore_on_isolate;
221
7a6000a6
LP
222 /* Ignore this unit when snapshotting */
223 bool ignore_on_snapshot;
224
90bbc946
LP
225 /* Did the last condition check suceed? */
226 bool condition_result;
227
9d58f1db
LP
228 bool in_load_queue:1;
229 bool in_dbus_queue:1;
230 bool in_cleanup_queue:1;
701cc384
LP
231 bool in_gc_queue:1;
232
9d58f1db 233 bool sent_dbus_new_signal:1;
6c073082
LP
234
235 bool no_gc:1;
cd6d0a45
LP
236
237 bool in_audit:1;
87f0e418
LP
238};
239
240#include "service.h"
241#include "timer.h"
242#include "socket.h"
243#include "target.h"
244#include "device.h"
245#include "mount.h"
246#include "automount.h"
247#include "snapshot.h"
07b0b134 248#include "swap.h"
01f78473 249#include "path.h"
87f0e418
LP
250
251union Unit {
252 Meta meta;
253 Service service;
254 Timer timer;
255 Socket socket;
256 Target target;
257 Device device;
258 Mount mount;
259 Automount automount;
260 Snapshot snapshot;
07b0b134 261 Swap swap;
01f78473 262 Path path;
87f0e418
LP
263};
264
265struct UnitVTable {
266 const char *suffix;
267
f975e971
LP
268 /* Config file sections this unit type understands, separated
269 * by NUL chars */
270 const char *sections;
271
e537352b 272 /* This should reset all type-specific variables. This should
a16e1123
LP
273 * not allocate memory, and is called with zero-initialized
274 * data. It should hence only initialize variables that need
275 * to be set != 0. */
e537352b
LP
276 void (*init)(Unit *u);
277
a16e1123
LP
278 /* This should free all type-specific variables. It should be
279 * idempotent. */
280 void (*done)(Unit *u);
281
e537352b
LP
282 /* Actually load data from disk. This may fail, and should set
283 * load_state to UNIT_LOADED, UNIT_MERGED or leave it at
284 * UNIT_STUB if no configuration could be found. */
285 int (*load)(Unit *u);
286
e537352b
LP
287 /* If a a lot of units got created via enumerate(), this is
288 * where to actually set the state and call unit_notify(). */
f50e0a01 289 int (*coldplug)(Unit *u);
87f0e418
LP
290
291 void (*dump)(Unit *u, FILE *f, const char *prefix);
292
293 int (*start)(Unit *u);
294 int (*stop)(Unit *u);
295 int (*reload)(Unit *u);
296
8a0867d6
LP
297 int (*kill)(Unit *u, KillWho w, KillMode m, int signo, DBusError *error);
298
87f0e418
LP
299 bool (*can_reload)(Unit *u);
300
a16e1123
LP
301 /* Write all data that cannot be restored from other sources
302 * away using unit_serialize_item() */
303 int (*serialize)(Unit *u, FILE *f, FDSet *fds);
304
305 /* Restore one item from the serialization */
306 int (*deserialize_item)(Unit *u, const char *key, const char *data, FDSet *fds);
307
87f0e418
LP
308 /* Boils down the more complex internal state of this unit to
309 * a simpler one that the engine can understand */
310 UnitActiveState (*active_state)(Unit *u);
311
10a94420
LP
312 /* Returns the substate specific to this unit type as
313 * string. This is purely information so that we can give the
35b8ca3a 314 * user a more fine grained explanation in which actual state a
10a94420
LP
315 * unit is in. */
316 const char* (*sub_state_to_string)(Unit *u);
317
701cc384
LP
318 /* Return true when there is reason to keep this entry around
319 * even nothing references it and it isn't active in any
320 * way */
321 bool (*check_gc)(Unit *u);
322
323 /* Return true when this unit is suitable for snapshotting */
324 bool (*check_snapshot)(Unit *u);
325
acbb0225 326 void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
87f0e418 327 void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
acbb0225 328 void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
7824bbeb 329
5f4b19f4
LP
330 /* Check whether unit needs a daemon reload */
331 bool (*need_daemon_reload)(Unit *u);
332
fdf20a31
MM
333 /* Reset failed state if we are in failed state */
334 void (*reset_failed)(Unit *u);
5632e374 335
05e343b7
LP
336 /* Called whenever any of the cgroups this unit watches for
337 * ran empty */
8e274523
LP
338 void (*cgroup_notify_empty)(Unit *u);
339
8c47c732 340 /* Called whenever a process of this unit sends us a message */
c952c6ec 341 void (*notify_message)(Unit *u, pid_t pid, char **tags);
8c47c732 342
05e343b7
LP
343 /* Called whenever a name thus Unit registered for comes or
344 * goes away. */
345 void (*bus_name_owner_change)(Unit *u, const char *name, const char *old_owner, const char *new_owner);
346
347 /* Called whenever a bus PID lookup finishes */
348 void (*bus_query_pid_done)(Unit *u, const char *name, pid_t pid);
349
4139c1b2 350 /* Called for each message received on the bus */
5e8d1c9a 351 DBusHandlerResult (*bus_message_handler)(Unit *u, DBusConnection *c, DBusMessage *message);
4139c1b2 352
a7f241db
LP
353 /* Return the unit this unit is following */
354 Unit *(*following)(Unit *u);
355
6210e7fc
LP
356 /* Return the set of units that are following each other */
357 int (*following_set)(Unit *u, Set **s);
358
f50e0a01
LP
359 /* This is called for each unit type and should be used to
360 * enumerate existing devices and load them. However,
361 * everything that is loaded here should still stay in
362 * inactive state. It is the job of the coldplug() call above
363 * to put the units into the initial state. */
7824bbeb 364 int (*enumerate)(Manager *m);
f50e0a01
LP
365
366 /* Type specific cleanups. */
7824bbeb 367 void (*shutdown)(Manager *m);
9d58f1db 368
c4e2ceae 369 /* When sending out PropertiesChanged signal, which properties
96d4ce01 370 * shall be invalidated? This is a NUL separated list of
c4e2ceae
LP
371 * strings, to minimize relocations a little. */
372 const char *bus_invalidating_properties;
373
374 /* The interface name */
375 const char *bus_interface;
376
9d58f1db
LP
377 /* Can units of this type have multiple names? */
378 bool no_alias:1;
379
9d58f1db
LP
380 /* Instances make no sense for this type */
381 bool no_instances:1;
382
701cc384
LP
383 /* Exclude from automatic gc */
384 bool no_gc:1;
c497c7a9 385
9e58ff9c
LP
386 /* Show status updates on the console */
387 bool show_status:1;
87f0e418
LP
388};
389
390extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
391
392#define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
393
394/* For casting a unit into the various unit types */
395#define DEFINE_CAST(UPPERCASE, MixedCase) \
396 static inline MixedCase* UPPERCASE(Unit *u) { \
399ab2b1 397 if (_unlikely_(!u || u->meta.type != UNIT_##UPPERCASE)) \
87f0e418
LP
398 return NULL; \
399 \
400 return (MixedCase*) u; \
401 }
402
403/* For casting the various unit types into a unit */
399ab2b1 404#define UNIT(u) ((Unit*) (&(u)->meta))
87f0e418
LP
405
406DEFINE_CAST(SOCKET, Socket);
407DEFINE_CAST(TIMER, Timer);
408DEFINE_CAST(SERVICE, Service);
409DEFINE_CAST(TARGET, Target);
410DEFINE_CAST(DEVICE, Device);
411DEFINE_CAST(MOUNT, Mount);
412DEFINE_CAST(AUTOMOUNT, Automount);
413DEFINE_CAST(SNAPSHOT, Snapshot);
07b0b134 414DEFINE_CAST(SWAP, Swap);
01f78473 415DEFINE_CAST(PATH, Path);
87f0e418 416
87f0e418
LP
417Unit *unit_new(Manager *m);
418void unit_free(Unit *u);
419
420int unit_add_name(Unit *u, const char *name);
9e2f7c11 421
701cc384 422int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference);
2c966c03
LP
423int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference);
424
701cc384 425int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
2c966c03
LP
426int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference);
427
701cc384 428int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
2c966c03 429int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference);
0ae97ec1 430
23a177ef
LP
431int unit_add_exec_dependencies(Unit *u, ExecContext *c);
432
8e274523
LP
433int unit_add_cgroup(Unit *u, CGroupBonding *b);
434int unit_add_cgroup_from_text(Unit *u, const char *name);
d686d8a9 435int unit_add_default_cgroups(Unit *u);
8e274523 436CGroupBonding* unit_get_default_cgroup(Unit *u);
ab1f0633 437int unit_add_cgroup_attribute(Unit *u, const char *controller, const char *name, const char *value, CGroupAttributeMapCallback map_callback);
8e274523 438
0ae97ec1 439int unit_choose_id(Unit *u, const char *name);
f50e0a01 440int unit_set_description(Unit *u, const char *description);
87f0e418 441
701cc384
LP
442bool unit_check_gc(Unit *u);
443
87f0e418 444void unit_add_to_load_queue(Unit *u);
c1e1601e 445void unit_add_to_dbus_queue(Unit *u);
23a177ef 446void unit_add_to_cleanup_queue(Unit *u);
701cc384 447void unit_add_to_gc_queue(Unit *u);
87f0e418
LP
448
449int unit_merge(Unit *u, Unit *other);
23a177ef
LP
450int unit_merge_by_name(Unit *u, const char *other);
451
452Unit *unit_follow_merge(Unit *u);
87f0e418 453
e537352b
LP
454int unit_load_fragment_and_dropin(Unit *u);
455int unit_load_fragment_and_dropin_optional(Unit *u);
87f0e418
LP
456int unit_load(Unit *unit);
457
87f0e418
LP
458const char *unit_description(Unit *u);
459
f278026d
LP
460bool unit_has_name(Unit *u, const char *name);
461
87f0e418
LP
462UnitActiveState unit_active_state(Unit *u);
463
10a94420
LP
464const char* unit_sub_state_to_string(Unit *u);
465
87f0e418
LP
466void unit_dump(Unit *u, FILE *f, const char *prefix);
467
468bool unit_can_reload(Unit *u);
469bool unit_can_start(Unit *u);
2528a7a6 470bool unit_can_isolate(Unit *u);
87f0e418
LP
471
472int unit_start(Unit *u);
473int unit_stop(Unit *u);
474int unit_reload(Unit *u);
475
8a0867d6
LP
476int unit_kill(Unit *u, KillWho w, KillMode m, int signo, DBusError *error);
477
e2f3b44c 478void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success);
87f0e418 479
acbb0225
LP
480int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
481void unit_unwatch_fd(Unit *u, Watch *w);
87f0e418
LP
482
483int unit_watch_pid(Unit *u, pid_t pid);
484void unit_unwatch_pid(Unit *u, pid_t pid);
485
acbb0225
LP
486int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
487void unit_unwatch_timer(Unit *u, Watch *w);
87f0e418 488
05e343b7
LP
489int unit_watch_bus_name(Unit *u, const char *name);
490void unit_unwatch_bus_name(Unit *u, const char *name);
491
87f0e418
LP
492bool unit_job_is_applicable(Unit *u, JobType j);
493
0301abf4
LP
494int set_unit_path(const char *p);
495
50159e6a
LP
496char *unit_dbus_path(Unit *u);
497
f6ff8c29 498int unit_load_related_unit(Unit *u, const char *type, Unit **_found);
a16e1123 499int unit_get_related_unit(Unit *u, const char *type, Unit **_found);
f6ff8c29 500
9e2f7c11
LP
501char *unit_name_printf(Unit *u, const char* text);
502char *unit_full_printf(Unit *u, const char *text);
503char **unit_full_printf_strv(Unit *u, char **l);
504
a16e1123
LP
505bool unit_can_serialize(Unit *u);
506int unit_serialize(Unit *u, FILE *f, FDSet *fds);
93a46b0b 507void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *value, ...) _printf_attr_(4,5);
a16e1123
LP
508void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value);
509int unit_deserialize(Unit *u, FILE *f, FDSet *fds);
510
6e2ef85b
LP
511int unit_add_node_link(Unit *u, const char *what, bool wants);
512
cca098b0
LP
513int unit_coldplug(Unit *u);
514
9e58ff9c
LP
515void unit_status_printf(Unit *u, const char *format, ...);
516
45fb0699
LP
517bool unit_need_daemon_reload(Unit *u);
518
fdf20a31 519void unit_reset_failed(Unit *u);
5632e374 520
a7f241db
LP
521Unit *unit_following(Unit *u);
522
18ffdfda 523bool unit_pending_inactive(Unit *u);
f976f3f6 524bool unit_pending_active(Unit *u);
18ffdfda 525
bba34eed
LP
526int unit_add_default_target_dependency(Unit *u, Unit *target);
527
6210e7fc
LP
528int unit_following_set(Unit *u, Set **s);
529
71fad675 530UnitType unit_name_to_type(const char *n);
b9c0d441 531bool unit_name_is_valid(const char *n, bool template_ok);
71fad675 532
c0daa706
LP
533void unit_trigger_on_failure(Unit *u);
534
90bbc946
LP
535bool unit_condition_test(Unit *u);
536
a4375746
LP
537UnitFileState unit_get_unit_file_state(Unit *u);
538
94f04347
LP
539const char *unit_load_state_to_string(UnitLoadState i);
540UnitLoadState unit_load_state_from_string(const char *s);
541
542const char *unit_active_state_to_string(UnitActiveState i);
543UnitActiveState unit_active_state_from_string(const char *s);
544
545const char *unit_dependency_to_string(UnitDependency i);
546UnitDependency unit_dependency_from_string(const char *s);
547
87f0e418 548#endif