Debian Codegraph @

src/core/job.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "core-forward.h"
5 #include "list.h"
6
7 /* Be careful when changing the job types! Adjust job_merging_table[] accordingly! */
8 enum JobType {
9 JOB_START, /* if a unit does not support being started, we'll just wait until it becomes active */
10 JOB_VERIFY_ACTIVE,
11
12 JOB_STOP,
13
14 JOB_RELOAD, /* if running, reload */
15
16 /* Note that restarts are first treated like JOB_STOP, but
17 * then instead of finishing are patched to become
18 * JOB_START. */
19 JOB_RESTART, /* If running, stop. Then start unconditionally. */
20
21 _JOB_TYPE_MAX_MERGING,
22
23 /* JOB_NOP can enter into a transaction, but as it won't pull in
24 * any dependencies and it uses the special 'nop_job' slot in Unit,
25 * it won't have to merge with anything (except possibly into another
26 * JOB_NOP, previously installed). JOB_NOP is special-cased in
27 * job_type_is_*() functions so that the transaction can be
28 * activated. */
29 JOB_NOP = _JOB_TYPE_MAX_MERGING, /* do nothing */
30
31 _JOB_TYPE_MAX_IN_TRANSACTION,
32
33 /* JOB_TRY_RESTART can never appear in a transaction, because
34 * it always collapses into JOB_RESTART or JOB_NOP before entering.
35 * Thus we never need to merge it with anything. */
36 JOB_TRY_RESTART = _JOB_TYPE_MAX_IN_TRANSACTION, /* if running, stop and then start */
37
38 /* Similar to JOB_TRY_RESTART but collapses to JOB_RELOAD or JOB_NOP */
39 JOB_TRY_RELOAD,
40
41 /* JOB_RELOAD_OR_START won't enter into a transaction and cannot result
42 * from transaction merging (there's no way for JOB_RELOAD and
43 * JOB_START to meet in one transaction). It can result from a merge
44 * during job installation, but then it will immediately collapse into
45 * one of the two simpler types. */
46 JOB_RELOAD_OR_START, /* if running, reload, otherwise start */
47
48 _JOB_TYPE_MAX,
49 _JOB_TYPE_INVALID = -EINVAL,
50 };
51
52 typedef enum JobState {
53 JOB_WAITING,
54 JOB_RUNNING,
55 JOB_FINISHED,
56 _JOB_STATE_MAX,
57 _JOB_STATE_INVALID = -EINVAL,
58 } JobState;
59
60 typedef enum JobResult {
61 JOB_DONE, /* Job completed successfully (or skipped due to an unmet ConditionXYZ=) */
62 JOB_CANCELED, /* Job canceled by a conflicting job installation or by explicit cancel request */
63 JOB_TIMEOUT, /* Job timeout elapsed */
64 JOB_FAILED, /* Job failed */
65 JOB_DEPENDENCY, /* A required dependency job did not result in JOB_DONE */
66 JOB_SKIPPED, /* Negative result of JOB_VERIFY_ACTIVE or skip due to ExecCondition= */
67 JOB_INVALID, /* JOB_RELOAD of inactive unit */
68 JOB_ASSERT, /* Couldn't start a unit, because an assert didn't hold */
69 JOB_UNSUPPORTED, /* Couldn't start a unit, because the unit type is not supported on the system */
70 JOB_COLLECTED, /* Job was garbage collected, since nothing needed it anymore */
71 JOB_ONCE, /* Unit was started before, and hence can't be started again */
72 JOB_FROZEN, /* Unit is currently frozen, so we can't safely operate on it */
73 JOB_CONCURRENCY, /* Slice the unit is in has its hard concurrency limit reached */
74 _JOB_RESULT_MAX,
75 _JOB_RESULT_INVALID = -EINVAL,
76 } JobResult;
77
78 typedef struct JobDependency {
79 /* Encodes that the 'subject' job needs the 'object' job in
80 * some way. This structure is used only while building a transaction. */
81 Job *subject;
82 Job *object;
83
84 LIST_FIELDS(JobDependency, subject);
85 LIST_FIELDS(JobDependency, object);
86
87 bool matters:1;
88 bool conflicts:1;
89 } JobDependency;
90
91 typedef struct Job {
92 Manager *manager;
93 Unit *unit;
94
95 LIST_FIELDS(Job, transaction);
96 LIST_FIELDS(Job, dbus_queue);
97 LIST_FIELDS(Job, gc_queue);
98
99 LIST_HEAD(JobDependency, subject_list);
100 LIST_HEAD(JobDependency, object_list);
101
102 /* Used for graph algs as a "I have been here" marker */
103 Job *marker;
104 unsigned generation;
105
106 uint32_t id;
107
108 JobType type;
109 JobState state;
110 JobResult result;
111
112 unsigned run_queue_idx;
113
114 sd_event_source *timer_event_source;
115 usec_t begin_usec;
116 usec_t begin_running_usec;
117
118 /*
119 * This tracks where to send signals, and also which clients
120 * are allowed to call DBus methods on the job (other than
121 * root).
122 *
123 * There can be more than one client, because of job merging.
124 */
125 sd_bus_track *bus_track;
126 char **deserialized_clients;
127
128 /* If non-NULL, a varlink connection streaming updates. */
129 sd_varlink *varlink;
130
131 /* If the job had a specific trigger that needs to be advertised (eg: a path unit), store it. */
132 ActivationDetails *activation_details;
133
134 bool installed:1;
135 bool in_run_queue:1;
136
137 bool matters_to_anchor:1;
138 bool refuse_late_merge:1;
139 bool ignore_order:1;
140 bool irreversible:1;
141
142 bool in_dbus_queue:1;
143 bool sent_dbus_new_signal:1;
144
145 bool ref_by_private_bus:1;
146
147 bool in_gc_queue:1;
148
149 bool varlink_notify_job_changes:1;
150 } Job;
151
152 Job* job_new(Unit *unit, JobType type);
153 Job* job_new_raw(Unit *unit);
154 void job_unlink(Job *job);
155 Job* job_free(Job *job);
156 DEFINE_TRIVIAL_CLEANUP_FUNC(Job*, job_free);
157
158 Job* job_install(Job *j);
159 int job_install_deserialized(Job *j);
160 void job_uninstall(Job *j);
161
162 void job_dump(Job *j, FILE *f, const char *prefix);
163 int job_serialize(Job *j, FILE *f);
164 int job_deserialize(Job *j, FILE *f);
165 int job_coldplug(Job *j);
166
167 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts);
168 void job_dependency_free(JobDependency *l);
169
170 JobType job_type_lookup_merge(JobType a, JobType b) _const_;
171
172 static inline bool job_type_is_mergeable(JobType a, JobType b) {
173 return job_type_lookup_merge(a, b) >= 0;
174 }
175
176 static inline bool job_type_is_conflicting(JobType a, JobType b) {
177 return a != JOB_NOP && b != JOB_NOP && !job_type_is_mergeable(a, b);
178 }
179
180 static inline bool job_type_is_superset(JobType a, JobType b) {
181 /* Checks whether operation a is a "superset" of b in its actions */
182 if (b == JOB_NOP)
183 return true;
184 if (a == JOB_NOP)
185 return false;
186 return a == job_type_lookup_merge(a, b);
187 }
188
189 bool job_type_is_redundant(JobType a, UnitActiveState b) _const_;
190
191 /* Collapses a state-dependent job type into a simpler type by observing
192 * the state of the unit which it is going to be applied to. */
193 JobType job_type_collapse(JobType t, Unit *u);
194
195 int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u);
196
197 void job_add_to_run_queue(Job *j);
198 void job_add_to_dbus_queue(Job *j);
199
200 int job_start_timer(Job *j, bool job_running);
201
202 int job_run_and_invalidate(Job *j);
203 int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already);
204
205 char* job_dbus_path(Job *j);
206
207 void job_shutdown_magic(Job *j);
208
209 int job_get_timeout(Job *j, usec_t *ret);
210
211 bool job_may_gc(Job *j);
212 void job_add_to_gc_queue(Job *j);
213
214 int job_get_before(Job *j, Job*** ret);
215 int job_get_after(Job *j, Job*** ret);
216
217 DECLARE_STRING_TABLE_LOOKUP(job_type, JobType);
218
219 DECLARE_STRING_TABLE_LOOKUP(job_state, JobState);
220
221 DECLARE_STRING_TABLE_LOOKUP(job_result, JobResult);
222
223 const char* job_type_to_access_method(JobType t);
224
225 int job_compare(Job *a, Job *b, UnitDependencyAtom assume_dep);
226
227 void job_set_activation_details(Job *j, ActivationDetails *info);