]> git.ipfire.org Git - thirdparty/systemd.git/blame - job.c
Use @ instead = as abstract namespace socket prefix
[thirdparty/systemd.git] / job.c
CommitLineData
60918275
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3#include <assert.h>
1ffba6fe 4#include <errno.h>
60918275
LP
5
6#include "macro.h"
7#include "job.h"
8
87f0e418 9Job* job_new(Manager *m, JobType type, Unit *unit) {
60918275
LP
10 Job *j;
11
12 assert(m);
13 assert(type < _JOB_TYPE_MAX);
87f0e418 14 assert(unit);
60918275
LP
15
16 if (!(j = new0(Job, 1)))
17 return NULL;
18
19 j->manager = m;
20 j->id = m->current_job_id++;
21 j->type = type;
87f0e418 22 j->unit = unit;
60918275 23
e5b5ae50 24 /* We don't link it here, that's what job_dependency() is for */
60918275
LP
25
26 return j;
27}
28
60918275
LP
29void job_free(Job *j) {
30 assert(j);
31
32 /* Detach from next 'bigger' objects */
ac1135be 33 if (j->installed) {
87f0e418
LP
34 if (j->unit->meta.job == j)
35 j->unit->meta.job = NULL;
60918275
LP
36
37 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
ac1135be 38 j->installed = false;
60918275
LP
39 }
40
5cb5a6ff 41 /* Detach from next 'smaller' objects */
302d0040 42 manager_transaction_unlink_job(j->manager, j);
60918275
LP
43
44 free(j);
45}
a66d02c3 46
e5b5ae50
LP
47JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) {
48 JobDependency *l;
49
50 assert(object);
51
52 /* Adds a new job link, which encodes that the 'subject' job
53 * needs the 'object' job in some way. If 'subject' is NULL
54 * this means the 'anchor' job (i.e. the one the user
55 * explcitily asked for) is the requester. */
56
ceed3570 57 if (!(l = new0(JobDependency, 1)))
e5b5ae50
LP
58 return NULL;
59
60 l->subject = subject;
61 l->object = object;
62 l->matters = matters;
63
44d8db9e
LP
64 if (subject)
65 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
66 else
67 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
e5b5ae50 68
44d8db9e 69 LIST_PREPEND(JobDependency, object, object->object_list, l);
e5b5ae50
LP
70
71 return l;
72}
73
74void job_dependency_free(JobDependency *l) {
75 assert(l);
76
44d8db9e
LP
77 if (l->subject)
78 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
e5b5ae50 79 else
44d8db9e 80 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
e5b5ae50 81
44d8db9e 82 LIST_REMOVE(JobDependency, object, l->object->object_list, l);
e5b5ae50
LP
83
84 free(l);
85}
86
87void job_dependency_delete(Job *subject, Job *object, bool *matters) {
88 JobDependency *l;
89
90 assert(object);
91
44d8db9e 92 LIST_FOREACH(object, l, object->object_list) {
e5b5ae50
LP
93 assert(l->object == object);
94
95 if (l->subject == subject)
96 break;
97 }
98
99 if (!l) {
100 if (matters)
101 *matters = false;
102 return;
103 }
104
105 if (matters)
106 *matters = l->matters;
107
108 job_dependency_free(l);
109}
110
1ffba6fe 111const char* job_type_to_string(JobType t) {
a66d02c3
LP
112
113 static const char* const job_type_table[_JOB_TYPE_MAX] = {
42f4e3c4 114 [JOB_START] = "start",
5cb5a6ff 115 [JOB_VERIFY_ACTIVE] = "verify-active",
42f4e3c4 116 [JOB_STOP] = "stop",
42f4e3c4 117 [JOB_RELOAD] = "reload",
e5b5ae50 118 [JOB_RELOAD_OR_START] = "reload-or-start",
42f4e3c4
LP
119 [JOB_RESTART] = "restart",
120 [JOB_TRY_RESTART] = "try-restart",
a66d02c3
LP
121 };
122
1ffba6fe
LP
123 if (t < 0 || t >= _JOB_TYPE_MAX)
124 return "n/a";
125
126 return job_type_table[t];
127}
128
129void job_dump(Job *j, FILE*f, const char *prefix) {
130
a66d02c3 131 static const char* const job_state_table[_JOB_STATE_MAX] = {
42f4e3c4 132 [JOB_WAITING] = "waiting",
5cb5a6ff 133 [JOB_RUNNING] = "running"
a66d02c3
LP
134 };
135
136 assert(j);
137 assert(f);
138
ceed3570 139 fprintf(f,
44d8db9e 140 "%s→ Job %u:\n"
ceed3570 141 "%s\tAction: %s → %s\n"
5cb5a6ff
LP
142 "%s\tState: %s\n"
143 "%s\tForced: %s\n",
ceed3570 144 prefix, j->id,
87f0e418 145 prefix, unit_id(j->unit), job_type_to_string(j->type),
5cb5a6ff
LP
146 prefix, job_state_table[j->state],
147 prefix, yes_no(j->forced));
a66d02c3 148}
e5b5ae50
LP
149
150bool job_is_anchor(Job *j) {
151 JobDependency *l;
152
153 assert(j);
154
44d8db9e 155 LIST_FOREACH(object, l, j->object_list)
e5b5ae50
LP
156 if (!l->subject)
157 return true;
158
159 return false;
160}
1ffba6fe
LP
161
162static bool types_match(JobType a, JobType b, JobType c, JobType d) {
163 return
164 (a == c && b == d) ||
165 (a == d && b == c);
166}
167
168int job_type_merge(JobType *a, JobType b) {
169 if (*a == b)
170 return 0;
171
172 /* Merging is associative! a merged with b merged with c is
173 * the same as a merged with c merged with b. */
174
175 /* Mergeability is transitive! if a can be merged with b and b
176 * with c then a also with c */
177
178 /* Also, if a merged with b cannot be merged with c, then
179 * either a or b cannot be merged with c either */
180
5cb5a6ff 181 if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
1ffba6fe
LP
182 *a = JOB_START;
183 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
184 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
5cb5a6ff 185 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
1ffba6fe
LP
186 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
187 *a = JOB_RELOAD_OR_START;
188 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
189 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
5cb5a6ff 190 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
1ffba6fe
LP
191 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
192 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
193 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
194 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
195 *a = JOB_RESTART;
5cb5a6ff 196 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
1ffba6fe 197 *a = JOB_RELOAD;
5cb5a6ff 198 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
1ffba6fe
LP
199 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
200 *a = JOB_TRY_RESTART;
201 else
202 return -EEXIST;
203
204 return 0;
205}
206
5cb5a6ff 207bool job_type_is_mergeable(JobType a, JobType b) {
1ffba6fe
LP
208 return job_type_merge(&a, b) >= 0;
209}
210
211bool job_type_is_superset(JobType a, JobType b) {
212
5cb5a6ff
LP
213 /* Checks whether operation a is a "superset" of b in its
214 * actions */
1ffba6fe
LP
215
216 if (a == b)
217 return true;
218
219 switch (a) {
220 case JOB_START:
5cb5a6ff 221 return b == JOB_VERIFY_ACTIVE;
1ffba6fe
LP
222
223 case JOB_RELOAD:
5cb5a6ff
LP
224 return
225 b == JOB_VERIFY_ACTIVE;
1ffba6fe
LP
226
227 case JOB_RELOAD_OR_START:
228 return
229 b == JOB_RELOAD ||
5cb5a6ff
LP
230 b == JOB_START ||
231 b == JOB_VERIFY_ACTIVE;
1ffba6fe
LP
232
233 case JOB_RESTART:
234 return
235 b == JOB_START ||
5cb5a6ff 236 b == JOB_VERIFY_ACTIVE ||
1ffba6fe
LP
237 b == JOB_RELOAD ||
238 b == JOB_RELOAD_OR_START ||
239 b == JOB_TRY_RESTART;
240
241 case JOB_TRY_RESTART:
242 return
5cb5a6ff 243 b == JOB_VERIFY_ACTIVE ||
1ffba6fe
LP
244 b == JOB_RELOAD;
245 default:
246 return false;
247
248 }
249}
e094e853
LP
250
251bool job_type_is_conflicting(JobType a, JobType b) {
cd2dbd7d
LP
252 assert(a >= 0 && a < _JOB_TYPE_MAX);
253 assert(b >= 0 && b < _JOB_TYPE_MAX);
e094e853 254
5cb5a6ff 255 return (a == JOB_STOP) != (b == JOB_STOP);
e094e853 256}
cd2dbd7d 257
5cb5a6ff 258bool job_is_runnable(Job *j) {
034c6ed7 259 Iterator i;
87f0e418 260 Unit *other;
5cb5a6ff
LP
261
262 assert(j);
ac1135be 263 assert(j->installed);
5cb5a6ff 264
87f0e418 265 /* Checks whether there is any job running for the units this
5cb5a6ff
LP
266 * job needs to be running after (in the case of a 'positive'
267 * job type) or before (in the case of a 'negative' job type
268 * . */
269
270 if (j->type == JOB_START ||
271 j->type == JOB_VERIFY_ACTIVE ||
272 j->type == JOB_RELOAD ||
273 j->type == JOB_RELOAD_OR_START) {
274
275 /* Immediate result is that the job is or might be
276 * started. In this case lets wait for the
277 * dependencies, regardless whether they are
278 * starting or stopping something. */
279
87f0e418 280 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
5cb5a6ff
LP
281 if (other->meta.job)
282 return false;
283 }
284
285 /* Also, if something else is being stopped and we should
286 * change state after it, then lets wait. */
287
87f0e418 288 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
5cb5a6ff
LP
289 if (other->meta.job &&
290 (other->meta.job->type == JOB_STOP ||
291 other->meta.job->type == JOB_RESTART ||
292 other->meta.job->type == JOB_TRY_RESTART))
293 return false;
294
295 /* This means that for a service a and a service b where b
296 * shall be started after a:
297 *
298 * start a + start b → 1st step start a, 2nd step start b
299 * start a + stop b → 1st step stop b, 2nd step start a
300 * stop a + start b → 1st step stop a, 2nd step start b
301 * stop a + stop b → 1st step stop b, 2nd step stop a
302 *
303 * This has the side effect that restarts are properly
304 * synchronized too. */
305
306 return true;
307}
308
309int job_run_and_invalidate(Job *j) {
310 int r;
ac1135be 311
5cb5a6ff 312 assert(j);
ac1135be 313 assert(j->installed);
5cb5a6ff 314
034c6ed7
LP
315 if (j->in_run_queue) {
316 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
317 j->in_run_queue = false;
318 }
5cb5a6ff
LP
319
320 if (j->state != JOB_WAITING)
321 return 0;
322
034c6ed7
LP
323 if (!job_is_runnable(j))
324 return -EAGAIN;
325
83c60c9f
LP
326 j->state = JOB_RUNNING;
327
5cb5a6ff
LP
328 switch (j->type) {
329
330 case JOB_START:
87f0e418 331 r = unit_start(j->unit);
5cb5a6ff
LP
332 if (r == -EBADR)
333 r = 0;
334 break;
335
336 case JOB_VERIFY_ACTIVE: {
87f0e418
LP
337 UnitActiveState t = unit_active_state(j->unit);
338 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
5cb5a6ff 339 r = -EALREADY;
87f0e418 340 else if (t == UNIT_ACTIVATING)
5cb5a6ff
LP
341 r = -EAGAIN;
342 else
343 r = -ENOEXEC;
344 break;
345 }
346
347 case JOB_STOP:
87f0e418 348 r = unit_stop(j->unit);
5cb5a6ff
LP
349 break;
350
351 case JOB_RELOAD:
87f0e418 352 r = unit_reload(j->unit);
5cb5a6ff
LP
353 break;
354
355 case JOB_RELOAD_OR_START:
87f0e418
LP
356 if (unit_active_state(j->unit) == UNIT_ACTIVE)
357 r = unit_reload(j->unit);
5cb5a6ff 358 else
87f0e418 359 r = unit_start(j->unit);
5cb5a6ff
LP
360 break;
361
362 case JOB_RESTART: {
87f0e418
LP
363 UnitActiveState t = unit_active_state(j->unit);
364 if (t == UNIT_INACTIVE || t == UNIT_ACTIVATING) {
5cb5a6ff 365 j->type = JOB_START;
87f0e418 366 r = unit_start(j->unit);
5cb5a6ff 367 } else
87f0e418 368 r = unit_stop(j->unit);
5cb5a6ff
LP
369 break;
370 }
371
372 case JOB_TRY_RESTART: {
87f0e418
LP
373 UnitActiveState t = unit_active_state(j->unit);
374 if (t == UNIT_INACTIVE || t == UNIT_DEACTIVATING)
5cb5a6ff 375 r = -ENOEXEC;
87f0e418 376 else if (t == UNIT_ACTIVATING) {
5cb5a6ff 377 j->type = JOB_START;
87f0e418 378 r = unit_start(j->unit);
5cb5a6ff 379 } else
87f0e418 380 r = unit_stop(j->unit);
5cb5a6ff
LP
381 break;
382 }
383
384 default:
44d8db9e 385 assert_not_reached("Unknown job type");
5cb5a6ff
LP
386 }
387
83c60c9f 388 if (r == -EALREADY)
5cb5a6ff 389 r = job_finish_and_invalidate(j, true);
83c60c9f
LP
390 else if (r == -EAGAIN) {
391 j->state = JOB_WAITING;
392 return -EAGAIN;
393 } else if (r < 0)
5cb5a6ff
LP
394 r = job_finish_and_invalidate(j, false);
395
396 return r;
397}
398
399int job_finish_and_invalidate(Job *j, bool success) {
87f0e418
LP
400 Unit *u;
401 Unit *other;
402 UnitType t;
034c6ed7 403 Iterator i;
5cb5a6ff
LP
404
405 assert(j);
ac1135be 406 assert(j->installed);
5cb5a6ff 407
034c6ed7 408 /* Patch restart jobs so that they become normal start jobs */
5cb5a6ff
LP
409 if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
410 j->state = JOB_RUNNING;
411 j->type = JOB_START;
034c6ed7
LP
412 job_schedule_run(j);
413 return 0;
5cb5a6ff
LP
414 }
415
87f0e418 416 u = j->unit;
5cb5a6ff
LP
417 t = j->type;
418 job_free(j);
419
420 /* Fail depending jobs on failure */
421 if (!success) {
422
423 if (t == JOB_START ||
424 t == JOB_VERIFY_ACTIVE ||
425 t == JOB_RELOAD_OR_START) {
426
87f0e418 427 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
5cb5a6ff
LP
428 if (other->meta.job &&
429 (other->meta.type == JOB_START ||
430 other->meta.type == JOB_VERIFY_ACTIVE ||
431 other->meta.type == JOB_RELOAD_OR_START))
432 job_finish_and_invalidate(other->meta.job, false);
433
87f0e418 434 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
5cb5a6ff
LP
435 if (other->meta.job &&
436 !other->meta.job->forced &&
437 (other->meta.type == JOB_START ||
438 other->meta.type == JOB_VERIFY_ACTIVE ||
439 other->meta.type == JOB_RELOAD_OR_START))
440 job_finish_and_invalidate(other->meta.job, false);
441
442 } else if (t == JOB_STOP) {
443
87f0e418 444 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
5cb5a6ff
LP
445 if (other->meta.job &&
446 (t == JOB_START ||
447 t == JOB_VERIFY_ACTIVE ||
448 t == JOB_RELOAD_OR_START))
449 job_finish_and_invalidate(other->meta.job, false);
450 }
451 }
452
453 /* Try to start the next jobs that can be started */
87f0e418 454 SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
5cb5a6ff 455 if (other->meta.job)
034c6ed7 456 job_schedule_run(other->meta.job);
87f0e418 457 SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
5cb5a6ff 458 if (other->meta.job)
034c6ed7 459 job_schedule_run(other->meta.job);
5cb5a6ff
LP
460
461 return 0;
462}
034c6ed7
LP
463
464void job_schedule_run(Job *j) {
465 assert(j);
ac1135be 466 assert(j->installed);
034c6ed7
LP
467
468 if (j->in_run_queue)
469 return;
470
471 LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
472 j->in_run_queue = true;
473}