]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/core/job.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / core / job.h
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1+ */
2#pragma once
3
4/***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
23#include <stdbool.h>
24
25#include "sd-event.h"
26
27#include "list.h"
28#include "unit-name.h"
29
30typedef struct Job Job;
31typedef struct JobDependency JobDependency;
32typedef enum JobType JobType;
33typedef enum JobState JobState;
34typedef enum JobMode JobMode;
35typedef enum JobResult JobResult;
36
37/* Be careful when changing the job types! Adjust job_merging_table[] accordingly! */
38enum JobType {
39 JOB_START, /* if a unit does not support being started, we'll just wait until it becomes active */
40 JOB_VERIFY_ACTIVE,
41
42 JOB_STOP,
43
44 JOB_RELOAD, /* if running, reload */
45
46 /* Note that restarts are first treated like JOB_STOP, but
47 * then instead of finishing are patched to become
48 * JOB_START. */
49 JOB_RESTART, /* If running, stop. Then start unconditionally. */
50
51 _JOB_TYPE_MAX_MERGING,
52
53 /* JOB_NOP can enter into a transaction, but as it won't pull in
54 * any dependencies and it uses the special 'nop_job' slot in Unit,
55 * it won't have to merge with anything (except possibly into another
56 * JOB_NOP, previously installed). JOB_NOP is special-cased in
57 * job_type_is_*() functions so that the transaction can be
58 * activated. */
59 JOB_NOP = _JOB_TYPE_MAX_MERGING, /* do nothing */
60
61 _JOB_TYPE_MAX_IN_TRANSACTION,
62
63 /* JOB_TRY_RESTART can never appear in a transaction, because
64 * it always collapses into JOB_RESTART or JOB_NOP before entering.
65 * Thus we never need to merge it with anything. */
66 JOB_TRY_RESTART = _JOB_TYPE_MAX_IN_TRANSACTION, /* if running, stop and then start */
67
68 /* Similar to JOB_TRY_RESTART but collapses to JOB_RELOAD or JOB_NOP */
69 JOB_TRY_RELOAD,
70
71 /* JOB_RELOAD_OR_START won't enter into a transaction and cannot result
72 * from transaction merging (there's no way for JOB_RELOAD and
73 * JOB_START to meet in one transaction). It can result from a merge
74 * during job installation, but then it will immediately collapse into
75 * one of the two simpler types. */
76 JOB_RELOAD_OR_START, /* if running, reload, otherwise start */
77
78 _JOB_TYPE_MAX,
79 _JOB_TYPE_INVALID = -1
80};
81
82enum JobState {
83 JOB_WAITING,
84 JOB_RUNNING,
85 _JOB_STATE_MAX,
86 _JOB_STATE_INVALID = -1
87};
88
89enum JobMode {
90 JOB_FAIL, /* Fail if a conflicting job is already queued */
91 JOB_REPLACE, /* Replace an existing conflicting job */
92 JOB_REPLACE_IRREVERSIBLY,/* Like JOB_REPLACE + produce irreversible jobs */
93 JOB_ISOLATE, /* Start a unit, and stop all others */
94 JOB_FLUSH, /* Flush out all other queued jobs when queing this one */
95 JOB_IGNORE_DEPENDENCIES, /* Ignore both requirement and ordering dependencies */
96 JOB_IGNORE_REQUIREMENTS, /* Ignore requirement dependencies */
97 _JOB_MODE_MAX,
98 _JOB_MODE_INVALID = -1
99};
100
101enum JobResult {
102 JOB_DONE, /* Job completed successfully */
103 JOB_CANCELED, /* Job canceled by a conflicting job installation or by explicit cancel request */
104 JOB_TIMEOUT, /* Job timeout elapsed */
105 JOB_FAILED, /* Job failed */
106 JOB_DEPENDENCY, /* A required dependency job did not result in JOB_DONE */
107 JOB_SKIPPED, /* Negative result of JOB_VERIFY_ACTIVE */
108 JOB_INVALID, /* JOB_RELOAD of inactive unit */
109 JOB_ASSERT, /* Couldn't start a unit, because an assert didn't hold */
110 JOB_UNSUPPORTED, /* Couldn't start a unit, because the unit type is not supported on the system */
111 JOB_COLLECTED, /* Job was garbage collected, since nothing needed it anymore */
112 _JOB_RESULT_MAX,
113 _JOB_RESULT_INVALID = -1
114};
115
116#include "unit.h"
117
118struct JobDependency {
119 /* Encodes that the 'subject' job needs the 'object' job in
120 * some way. This structure is used only while building a transaction. */
121 Job *subject;
122 Job *object;
123
124 LIST_FIELDS(JobDependency, subject);
125 LIST_FIELDS(JobDependency, object);
126
127 bool matters:1;
128 bool conflicts:1;
129};
130
131struct Job {
132 Manager *manager;
133 Unit *unit;
134
135 LIST_FIELDS(Job, transaction);
136 LIST_FIELDS(Job, run_queue);
137 LIST_FIELDS(Job, dbus_queue);
138 LIST_FIELDS(Job, gc_queue);
139
140 LIST_HEAD(JobDependency, subject_list);
141 LIST_HEAD(JobDependency, object_list);
142
143 /* Used for graph algs as a "I have been here" marker */
144 Job* marker;
145 unsigned generation;
146
147 uint32_t id;
148
149 JobType type;
150 JobState state;
151
152 sd_event_source *timer_event_source;
153 usec_t begin_usec;
154 usec_t begin_running_usec;
155
156 /*
157 * This tracks where to send signals, and also which clients
158 * are allowed to call DBus methods on the job (other than
159 * root).
160 *
161 * There can be more than one client, because of job merging.
162 */
163 sd_bus_track *bus_track;
164 char **deserialized_clients;
165
166 JobResult result;
167
168 bool installed:1;
169 bool in_run_queue:1;
170 bool matters_to_anchor:1;
171 bool in_dbus_queue:1;
172 bool sent_dbus_new_signal:1;
173 bool ignore_order:1;
174 bool irreversible:1;
175 bool in_gc_queue:1;
176 bool ref_by_private_bus:1;
177};
178
179Job* job_new(Unit *unit, JobType type);
180Job* job_new_raw(Unit *unit);
181void job_free(Job *job);
182Job* job_install(Job *j);
183int job_install_deserialized(Job *j);
184void job_uninstall(Job *j);
185void job_dump(Job *j, FILE*f, const char *prefix);
186int job_serialize(Job *j, FILE *f);
187int job_deserialize(Job *j, FILE *f);
188int job_coldplug(Job *j);
189
190JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts);
191void job_dependency_free(JobDependency *l);
192
193int job_merge(Job *j, Job *other);
194
195JobType job_type_lookup_merge(JobType a, JobType b) _pure_;
196
197_pure_ static inline bool job_type_is_mergeable(JobType a, JobType b) {
198 return job_type_lookup_merge(a, b) >= 0;
199}
200
201_pure_ static inline bool job_type_is_conflicting(JobType a, JobType b) {
202 return a != JOB_NOP && b != JOB_NOP && !job_type_is_mergeable(a, b);
203}
204
205_pure_ static inline bool job_type_is_superset(JobType a, JobType b) {
206 /* Checks whether operation a is a "superset" of b in its actions */
207 if (b == JOB_NOP)
208 return true;
209 if (a == JOB_NOP)
210 return false;
211 return a == job_type_lookup_merge(a, b);
212}
213
214bool job_type_is_redundant(JobType a, UnitActiveState b) _pure_;
215
216/* Collapses a state-dependent job type into a simpler type by observing
217 * the state of the unit which it is going to be applied to. */
218JobType job_type_collapse(JobType t, Unit *u);
219
220int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u);
221
222void job_add_to_run_queue(Job *j);
223void job_add_to_dbus_queue(Job *j);
224
225int job_start_timer(Job *j, bool job_running);
226
227int job_run_and_invalidate(Job *j);
228int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already);
229
230char *job_dbus_path(Job *j);
231
232void job_shutdown_magic(Job *j);
233
234int job_get_timeout(Job *j, usec_t *timeout) _pure_;
235
236bool job_check_gc(Job *j);
237void job_add_to_gc_queue(Job *j);
238
239int job_get_before(Job *j, Job*** ret);
240int job_get_after(Job *j, Job*** ret);
241
242const char* job_type_to_string(JobType t) _const_;
243JobType job_type_from_string(const char *s) _pure_;
244
245const char* job_state_to_string(JobState t) _const_;
246JobState job_state_from_string(const char *s) _pure_;
247
248const char* job_mode_to_string(JobMode t) _const_;
249JobMode job_mode_from_string(const char *s) _pure_;
250
251const char* job_result_to_string(JobResult t) _const_;
252JobResult job_result_from_string(const char *s) _pure_;
253
254const char* job_type_to_access_method(JobType t);