]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
60918275 | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
60918275 | 22 | #include <assert.h> |
1ffba6fe | 23 | #include <errno.h> |
faf919f1 LP |
24 | #include <sys/timerfd.h> |
25 | #include <sys/epoll.h> | |
60918275 | 26 | |
718db961 LP |
27 | #include "sd-id128.h" |
28 | #include "sd-messages.h" | |
94f04347 LP |
29 | #include "set.h" |
30 | #include "unit.h" | |
60918275 | 31 | #include "macro.h" |
94f04347 LP |
32 | #include "strv.h" |
33 | #include "load-fragment.h" | |
34 | #include "load-dropin.h" | |
f50e0a01 | 35 | #include "log.h" |
4139c1b2 | 36 | #include "dbus-job.h" |
c65eb836 | 37 | #include "special.h" |
f485606b | 38 | #include "async.h" |
c65eb836 | 39 | #include "virt.h" |
8f8f05a9 | 40 | #include "dbus.h" |
97e6a119 | 41 | |
39a18c60 | 42 | Job* job_new_raw(Unit *unit) { |
60918275 LP |
43 | Job *j; |
44 | ||
39a18c60 MS |
45 | /* used for deserialization */ |
46 | ||
87f0e418 | 47 | assert(unit); |
60918275 | 48 | |
39a18c60 MS |
49 | j = new0(Job, 1); |
50 | if (!j) | |
60918275 LP |
51 | return NULL; |
52 | ||
668ad332 | 53 | j->manager = unit->manager; |
87f0e418 | 54 | j->unit = unit; |
e0209d83 | 55 | j->type = _JOB_TYPE_INVALID; |
faf919f1 | 56 | |
39a18c60 MS |
57 | return j; |
58 | } | |
59 | ||
60 | Job* job_new(Unit *unit, JobType type) { | |
61 | Job *j; | |
62 | ||
63 | assert(type < _JOB_TYPE_MAX); | |
64 | ||
65 | j = job_new_raw(unit); | |
66 | if (!j) | |
67 | return NULL; | |
68 | ||
69 | j->id = j->manager->current_job_id++; | |
70 | j->type = type; | |
71 | ||
e5b5ae50 | 72 | /* We don't link it here, that's what job_dependency() is for */ |
60918275 LP |
73 | |
74 | return j; | |
75 | } | |
76 | ||
97e7d748 MS |
77 | void job_free(Job *j) { |
78 | assert(j); | |
79 | assert(!j->installed); | |
02a3bcc6 MS |
80 | assert(!j->transaction_prev); |
81 | assert(!j->transaction_next); | |
82 | assert(!j->subject_list); | |
83 | assert(!j->object_list); | |
60918275 | 84 | |
c1e1601e | 85 | if (j->in_run_queue) |
71fda00f | 86 | LIST_REMOVE(run_queue, j->manager->run_queue, j); |
c1e1601e LP |
87 | |
88 | if (j->in_dbus_queue) | |
71fda00f | 89 | LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j); |
c1e1601e | 90 | |
718db961 | 91 | sd_event_source_unref(j->timer_event_source); |
faf919f1 | 92 | |
8f8f05a9 LP |
93 | sd_bus_track_unref(j->subscribed); |
94 | strv_free(j->deserialized_subscribed); | |
faf919f1 | 95 | |
60918275 LP |
96 | free(j); |
97 | } | |
a66d02c3 | 98 | |
05d576f1 | 99 | void job_uninstall(Job *j) { |
e0209d83 MS |
100 | Job **pj; |
101 | ||
05d576f1 | 102 | assert(j->installed); |
e0209d83 MS |
103 | |
104 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; | |
105 | assert(*pj == j); | |
106 | ||
05d576f1 MS |
107 | /* Detach from next 'bigger' objects */ |
108 | ||
39a18c60 MS |
109 | /* daemon-reload should be transparent to job observers */ |
110 | if (j->manager->n_reloading <= 0) | |
111 | bus_job_send_removed_signal(j); | |
05d576f1 | 112 | |
e0209d83 MS |
113 | *pj = NULL; |
114 | ||
d6a093d0 | 115 | unit_add_to_gc_queue(j->unit); |
05d576f1 MS |
116 | |
117 | hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id)); | |
118 | j->installed = false; | |
119 | } | |
120 | ||
656bbffc MS |
121 | static bool job_type_allows_late_merge(JobType t) { |
122 | /* Tells whether it is OK to merge a job of type 't' with an already | |
123 | * running job. | |
124 | * Reloads cannot be merged this way. Think of the sequence: | |
125 | * 1. Reload of a daemon is in progress; the daemon has already loaded | |
126 | * its config file, but hasn't completed the reload operation yet. | |
127 | * 2. Edit foo's config file. | |
128 | * 3. Trigger another reload to have the daemon use the new config. | |
129 | * Should the second reload job be merged into the first one, the daemon | |
130 | * would not know about the new config. | |
131 | * JOB_RESTART jobs on the other hand can be merged, because they get | |
132 | * patched into JOB_START after stopping the unit. So if we see a | |
133 | * JOB_RESTART running, it means the unit hasn't stopped yet and at | |
134 | * this time the merge is still allowed. */ | |
e0209d83 | 135 | return t != JOB_RELOAD; |
656bbffc MS |
136 | } |
137 | ||
138 | static void job_merge_into_installed(Job *j, Job *other) { | |
139 | assert(j->installed); | |
140 | assert(j->unit == other->unit); | |
141 | ||
e0209d83 MS |
142 | if (j->type != JOB_NOP) |
143 | job_type_merge_and_collapse(&j->type, other->type, j->unit); | |
144 | else | |
145 | assert(other->type == JOB_NOP); | |
656bbffc MS |
146 | |
147 | j->override = j->override || other->override; | |
23ade460 | 148 | j->irreversible = j->irreversible || other->irreversible; |
e45460d6 | 149 | j->ignore_order = j->ignore_order || other->ignore_order; |
656bbffc MS |
150 | } |
151 | ||
152 | Job* job_install(Job *j) { | |
e0209d83 MS |
153 | Job **pj; |
154 | Job *uj; | |
05d576f1 | 155 | |
656bbffc | 156 | assert(!j->installed); |
e0209d83 MS |
157 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
158 | ||
159 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; | |
160 | uj = *pj; | |
656bbffc | 161 | |
05d576f1 | 162 | if (uj) { |
e0209d83 | 163 | if (j->type != JOB_NOP && job_type_is_conflicting(uj->type, j->type)) |
1abc85b8 | 164 | job_finish_and_invalidate(uj, JOB_CANCELED, false); |
656bbffc MS |
165 | else { |
166 | /* not conflicting, i.e. mergeable */ | |
167 | ||
e0209d83 | 168 | if (j->type == JOB_NOP || uj->state == JOB_WAITING || |
656bbffc MS |
169 | (job_type_allows_late_merge(j->type) && job_type_is_superset(uj->type, j->type))) { |
170 | job_merge_into_installed(uj, j); | |
66870f90 ZJS |
171 | log_debug_unit(uj->unit->id, |
172 | "Merged into installed job %s/%s as %u", | |
173 | uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id); | |
656bbffc MS |
174 | return uj; |
175 | } else { | |
176 | /* already running and not safe to merge into */ | |
177 | /* Patch uj to become a merged job and re-run it. */ | |
178 | /* XXX It should be safer to queue j to run after uj finishes, but it is | |
179 | * not currently possible to have more than one installed job per unit. */ | |
180 | job_merge_into_installed(uj, j); | |
66870f90 ZJS |
181 | log_debug_unit(uj->unit->id, |
182 | "Merged into running job, re-running: %s/%s as %u", | |
183 | uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id); | |
656bbffc | 184 | uj->state = JOB_WAITING; |
637f8b8e | 185 | uj->manager->n_running_jobs--; |
656bbffc MS |
186 | return uj; |
187 | } | |
188 | } | |
05d576f1 MS |
189 | } |
190 | ||
656bbffc | 191 | /* Install the job */ |
e0209d83 | 192 | *pj = j; |
05d576f1 MS |
193 | j->installed = true; |
194 | j->manager->n_installed_jobs ++; | |
66870f90 ZJS |
195 | log_debug_unit(j->unit->id, |
196 | "Installed new job %s/%s as %u", | |
197 | j->unit->id, job_type_to_string(j->type), (unsigned) j->id); | |
656bbffc | 198 | return j; |
05d576f1 MS |
199 | } |
200 | ||
e0209d83 MS |
201 | int job_install_deserialized(Job *j) { |
202 | Job **pj; | |
203 | ||
39a18c60 MS |
204 | assert(!j->installed); |
205 | ||
e0209d83 MS |
206 | if (j->type < 0 || j->type >= _JOB_TYPE_MAX_IN_TRANSACTION) { |
207 | log_debug("Invalid job type %s in deserialization.", strna(job_type_to_string(j->type))); | |
208 | return -EINVAL; | |
209 | } | |
210 | ||
211 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; | |
212 | ||
213 | if (*pj) { | |
66870f90 ZJS |
214 | log_debug_unit(j->unit->id, |
215 | "Unit %s already has a job installed. Not installing deserialized job.", | |
216 | j->unit->id); | |
e0209d83 | 217 | return -EEXIST; |
39a18c60 | 218 | } |
e0209d83 | 219 | *pj = j; |
39a18c60 | 220 | j->installed = true; |
66870f90 ZJS |
221 | log_debug_unit(j->unit->id, |
222 | "Reinstalled deserialized job %s/%s as %u", | |
223 | j->unit->id, job_type_to_string(j->type), (unsigned) j->id); | |
e0209d83 | 224 | return 0; |
39a18c60 MS |
225 | } |
226 | ||
1da4264f | 227 | JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) { |
e5b5ae50 LP |
228 | JobDependency *l; |
229 | ||
230 | assert(object); | |
231 | ||
232 | /* Adds a new job link, which encodes that the 'subject' job | |
233 | * needs the 'object' job in some way. If 'subject' is NULL | |
234 | * this means the 'anchor' job (i.e. the one the user | |
35b8ca3a | 235 | * explicitly asked for) is the requester. */ |
e5b5ae50 | 236 | |
ceed3570 | 237 | if (!(l = new0(JobDependency, 1))) |
e5b5ae50 LP |
238 | return NULL; |
239 | ||
240 | l->subject = subject; | |
241 | l->object = object; | |
242 | l->matters = matters; | |
69dd2852 | 243 | l->conflicts = conflicts; |
e5b5ae50 | 244 | |
44d8db9e | 245 | if (subject) |
71fda00f | 246 | LIST_PREPEND(subject, subject->subject_list, l); |
e5b5ae50 | 247 | |
71fda00f | 248 | LIST_PREPEND(object, object->object_list, l); |
e5b5ae50 LP |
249 | |
250 | return l; | |
251 | } | |
252 | ||
1da4264f | 253 | void job_dependency_free(JobDependency *l) { |
e5b5ae50 LP |
254 | assert(l); |
255 | ||
44d8db9e | 256 | if (l->subject) |
71fda00f | 257 | LIST_REMOVE(subject, l->subject->subject_list, l); |
e5b5ae50 | 258 | |
71fda00f | 259 | LIST_REMOVE(object, l->object->object_list, l); |
e5b5ae50 LP |
260 | |
261 | free(l); | |
262 | } | |
263 | ||
1ffba6fe | 264 | void job_dump(Job *j, FILE*f, const char *prefix) { |
a66d02c3 LP |
265 | assert(j); |
266 | assert(f); | |
267 | ||
9eb63b3c LP |
268 | if (!prefix) |
269 | prefix = ""; | |
270 | ||
ceed3570 | 271 | fprintf(f, |
40d50879 LP |
272 | "%s-> Job %u:\n" |
273 | "%s\tAction: %s -> %s\n" | |
5cb5a6ff | 274 | "%s\tState: %s\n" |
23ade460 MS |
275 | "%s\tForced: %s\n" |
276 | "%s\tIrreversible: %s\n", | |
ceed3570 | 277 | prefix, j->id, |
ac155bb8 | 278 | prefix, j->unit->id, job_type_to_string(j->type), |
94f04347 | 279 | prefix, job_state_to_string(j->state), |
23ade460 MS |
280 | prefix, yes_no(j->override), |
281 | prefix, yes_no(j->irreversible)); | |
a66d02c3 | 282 | } |
e5b5ae50 | 283 | |
348e27fe MS |
284 | /* |
285 | * Merging is commutative, so imagine the matrix as symmetric. We store only | |
286 | * its lower triangle to avoid duplication. We don't store the main diagonal, | |
287 | * because A merged with A is simply A. | |
288 | * | |
e0209d83 MS |
289 | * If the resulting type is collapsed immediately afterwards (to get rid of |
290 | * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain), | |
291 | * the following properties hold: | |
292 | * | |
348e27fe MS |
293 | * Merging is associative! A merged with B merged with C is the same as |
294 | * A merged with C merged with B. | |
295 | * | |
296 | * Mergeability is transitive! If A can be merged with B and B with C then | |
297 | * A also with C. | |
298 | * | |
299 | * Also, if A merged with B cannot be merged with C, then either A or B cannot | |
300 | * be merged with C either. | |
301 | */ | |
302 | static const JobType job_merging_table[] = { | |
e0209d83 MS |
303 | /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */ |
304 | /*********************************************************************************/ | |
348e27fe MS |
305 | /*JOB_START */ |
306 | /*JOB_VERIFY_ACTIVE */ JOB_START, | |
307 | /*JOB_STOP */ -1, -1, | |
308 | /*JOB_RELOAD */ JOB_RELOAD_OR_START, JOB_RELOAD, -1, | |
e0209d83 | 309 | /*JOB_RESTART */ JOB_RESTART, JOB_RESTART, -1, JOB_RESTART, |
348e27fe MS |
310 | }; |
311 | ||
312 | JobType job_type_lookup_merge(JobType a, JobType b) { | |
e0209d83 MS |
313 | assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2); |
314 | assert(a >= 0 && a < _JOB_TYPE_MAX_MERGING); | |
315 | assert(b >= 0 && b < _JOB_TYPE_MAX_MERGING); | |
1ffba6fe LP |
316 | |
317 | if (a == b) | |
348e27fe | 318 | return a; |
1ffba6fe | 319 | |
348e27fe MS |
320 | if (a < b) { |
321 | JobType tmp = a; | |
322 | a = b; | |
323 | b = tmp; | |
1ffba6fe | 324 | } |
e094e853 | 325 | |
348e27fe | 326 | return job_merging_table[(a - 1) * a / 2 + b]; |
e094e853 | 327 | } |
cd2dbd7d | 328 | |
593fbdd2 LP |
329 | bool job_type_is_redundant(JobType a, UnitActiveState b) { |
330 | switch (a) { | |
331 | ||
332 | case JOB_START: | |
333 | return | |
334 | b == UNIT_ACTIVE || | |
032ff4af | 335 | b == UNIT_RELOADING; |
593fbdd2 LP |
336 | |
337 | case JOB_STOP: | |
338 | return | |
6124958c | 339 | b == UNIT_INACTIVE || |
fdf20a31 | 340 | b == UNIT_FAILED; |
593fbdd2 LP |
341 | |
342 | case JOB_VERIFY_ACTIVE: | |
343 | return | |
344 | b == UNIT_ACTIVE || | |
032ff4af | 345 | b == UNIT_RELOADING; |
593fbdd2 LP |
346 | |
347 | case JOB_RELOAD: | |
348 | return | |
032ff4af | 349 | b == UNIT_RELOADING; |
593fbdd2 | 350 | |
593fbdd2 LP |
351 | case JOB_RESTART: |
352 | return | |
353 | b == UNIT_ACTIVATING; | |
354 | ||
e0209d83 MS |
355 | default: |
356 | assert_not_reached("Invalid job type"); | |
357 | } | |
358 | } | |
359 | ||
360 | void job_type_collapse(JobType *t, Unit *u) { | |
361 | UnitActiveState s; | |
362 | ||
363 | switch (*t) { | |
364 | ||
593fbdd2 | 365 | case JOB_TRY_RESTART: |
e0209d83 MS |
366 | s = unit_active_state(u); |
367 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s)) | |
368 | *t = JOB_NOP; | |
369 | else | |
370 | *t = JOB_RESTART; | |
371 | break; | |
372 | ||
373 | case JOB_RELOAD_OR_START: | |
374 | s = unit_active_state(u); | |
375 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s)) | |
376 | *t = JOB_START; | |
377 | else | |
378 | *t = JOB_RELOAD; | |
379 | break; | |
593fbdd2 LP |
380 | |
381 | default: | |
e0209d83 | 382 | ; |
593fbdd2 LP |
383 | } |
384 | } | |
385 | ||
e0209d83 MS |
386 | int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u) { |
387 | JobType t = job_type_lookup_merge(*a, b); | |
388 | if (t < 0) | |
389 | return -EEXIST; | |
390 | *a = t; | |
391 | job_type_collapse(a, u); | |
392 | return 0; | |
393 | } | |
394 | ||
9588bc32 | 395 | static bool job_is_runnable(Job *j) { |
034c6ed7 | 396 | Iterator i; |
87f0e418 | 397 | Unit *other; |
5cb5a6ff LP |
398 | |
399 | assert(j); | |
ac1135be | 400 | assert(j->installed); |
5cb5a6ff | 401 | |
87f0e418 | 402 | /* Checks whether there is any job running for the units this |
5cb5a6ff | 403 | * job needs to be running after (in the case of a 'positive' |
e67c3609 LP |
404 | * job type) or before (in the case of a 'negative' job |
405 | * type. */ | |
406 | ||
66ca4ec4 LP |
407 | /* Note that unit types have a say in what is runnable, |
408 | * too. For example, if they return -EAGAIN from | |
409 | * unit_start() they can indicate they are not | |
410 | * runnable yet. */ | |
411 | ||
e67c3609 | 412 | /* First check if there is an override */ |
cebe0d41 | 413 | if (j->ignore_order) |
e67c3609 | 414 | return true; |
5cb5a6ff | 415 | |
e0209d83 MS |
416 | if (j->type == JOB_NOP) |
417 | return true; | |
418 | ||
5cb5a6ff LP |
419 | if (j->type == JOB_START || |
420 | j->type == JOB_VERIFY_ACTIVE || | |
e0209d83 | 421 | j->type == JOB_RELOAD) { |
5cb5a6ff LP |
422 | |
423 | /* Immediate result is that the job is or might be | |
424 | * started. In this case lets wait for the | |
425 | * dependencies, regardless whether they are | |
426 | * starting or stopping something. */ | |
427 | ||
ac155bb8 MS |
428 | SET_FOREACH(other, j->unit->dependencies[UNIT_AFTER], i) |
429 | if (other->job) | |
5cb5a6ff LP |
430 | return false; |
431 | } | |
432 | ||
433 | /* Also, if something else is being stopped and we should | |
434 | * change state after it, then lets wait. */ | |
435 | ||
ac155bb8 MS |
436 | SET_FOREACH(other, j->unit->dependencies[UNIT_BEFORE], i) |
437 | if (other->job && | |
438 | (other->job->type == JOB_STOP || | |
e0209d83 | 439 | other->job->type == JOB_RESTART)) |
5cb5a6ff LP |
440 | return false; |
441 | ||
442 | /* This means that for a service a and a service b where b | |
443 | * shall be started after a: | |
444 | * | |
445 | * start a + start b → 1st step start a, 2nd step start b | |
446 | * start a + stop b → 1st step stop b, 2nd step start a | |
447 | * stop a + start b → 1st step stop a, 2nd step start b | |
448 | * stop a + stop b → 1st step stop b, 2nd step stop a | |
449 | * | |
450 | * This has the side effect that restarts are properly | |
451 | * synchronized too. */ | |
452 | ||
453 | return true; | |
454 | } | |
455 | ||
bbd1a837 | 456 | static void job_change_type(Job *j, JobType newtype) { |
66870f90 ZJS |
457 | log_debug_unit(j->unit->id, |
458 | "Converting job %s/%s -> %s/%s", | |
459 | j->unit->id, job_type_to_string(j->type), | |
460 | j->unit->id, job_type_to_string(newtype)); | |
bbd1a837 MS |
461 | |
462 | j->type = newtype; | |
463 | } | |
464 | ||
5cb5a6ff LP |
465 | int job_run_and_invalidate(Job *j) { |
466 | int r; | |
2cf19a7a | 467 | uint32_t id; |
637f8b8e | 468 | Manager *m = j->manager; |
ac1135be | 469 | |
5cb5a6ff | 470 | assert(j); |
ac1135be | 471 | assert(j->installed); |
e0209d83 | 472 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
66aa6f7f | 473 | assert(j->in_run_queue); |
5cb5a6ff | 474 | |
71fda00f | 475 | LIST_REMOVE(run_queue, j->manager->run_queue, j); |
66aa6f7f | 476 | j->in_run_queue = false; |
5cb5a6ff LP |
477 | |
478 | if (j->state != JOB_WAITING) | |
479 | return 0; | |
480 | ||
034c6ed7 LP |
481 | if (!job_is_runnable(j)) |
482 | return -EAGAIN; | |
483 | ||
83c60c9f | 484 | j->state = JOB_RUNNING; |
637f8b8e | 485 | m->n_running_jobs++; |
c1e1601e | 486 | job_add_to_dbus_queue(j); |
83c60c9f | 487 | |
2cf19a7a LP |
488 | /* While we execute this operation the job might go away (for |
489 | * example: because it is replaced by a new, conflicting | |
490 | * job.) To make sure we don't access a freed job later on we | |
491 | * store the id here, so that we can verify the job is still | |
492 | * valid. */ | |
493 | id = j->id; | |
2cf19a7a | 494 | |
5cb5a6ff LP |
495 | switch (j->type) { |
496 | ||
497 | case JOB_START: | |
87f0e418 | 498 | r = unit_start(j->unit); |
57339f47 | 499 | |
dd17d388 | 500 | /* If this unit cannot be started, then simply wait */ |
5cb5a6ff LP |
501 | if (r == -EBADR) |
502 | r = 0; | |
503 | break; | |
504 | ||
505 | case JOB_VERIFY_ACTIVE: { | |
87f0e418 LP |
506 | UnitActiveState t = unit_active_state(j->unit); |
507 | if (UNIT_IS_ACTIVE_OR_RELOADING(t)) | |
5cb5a6ff | 508 | r = -EALREADY; |
87f0e418 | 509 | else if (t == UNIT_ACTIVATING) |
5cb5a6ff LP |
510 | r = -EAGAIN; |
511 | else | |
6a371e23 | 512 | r = -EBADR; |
5cb5a6ff LP |
513 | break; |
514 | } | |
515 | ||
516 | case JOB_STOP: | |
dd17d388 | 517 | case JOB_RESTART: |
87f0e418 | 518 | r = unit_stop(j->unit); |
57339f47 | 519 | |
dd17d388 | 520 | /* If this unit cannot stopped, then simply wait. */ |
57339f47 LP |
521 | if (r == -EBADR) |
522 | r = 0; | |
5cb5a6ff LP |
523 | break; |
524 | ||
525 | case JOB_RELOAD: | |
87f0e418 | 526 | r = unit_reload(j->unit); |
5cb5a6ff LP |
527 | break; |
528 | ||
e0209d83 MS |
529 | case JOB_NOP: |
530 | r = -EALREADY; | |
531 | break; | |
532 | ||
5cb5a6ff | 533 | default: |
44d8db9e | 534 | assert_not_reached("Unknown job type"); |
5cb5a6ff LP |
535 | } |
536 | ||
e0209d83 MS |
537 | j = manager_get_job(m, id); |
538 | if (j) { | |
2cf19a7a | 539 | if (r == -EALREADY) |
5273510e | 540 | r = job_finish_and_invalidate(j, JOB_DONE, true); |
6a371e23 | 541 | else if (r == -EBADR) |
5273510e | 542 | r = job_finish_and_invalidate(j, JOB_SKIPPED, true); |
6a371e23 ZJS |
543 | else if (r == -ENOEXEC) |
544 | r = job_finish_and_invalidate(j, JOB_INVALID, true); | |
637f8b8e | 545 | else if (r == -EAGAIN) { |
2cf19a7a | 546 | j->state = JOB_WAITING; |
637f8b8e MS |
547 | m->n_running_jobs--; |
548 | } else if (r < 0) | |
5273510e | 549 | r = job_finish_and_invalidate(j, JOB_FAILED, true); |
2cf19a7a | 550 | } |
5cb5a6ff LP |
551 | |
552 | return r; | |
553 | } | |
554 | ||
44a6b1b6 | 555 | _pure_ static const char *job_get_status_message_format(Unit *u, JobType t, JobResult result) { |
c6918296 | 556 | const UnitStatusMessageFormats *format_table; |
877d54e9 LP |
557 | |
558 | assert(u); | |
559 | assert(t >= 0); | |
560 | assert(t < _JOB_TYPE_MAX); | |
c6918296 MS |
561 | |
562 | format_table = &UNIT_VTABLE(u)->status_message_formats; | |
563 | if (!format_table) | |
877d54e9 LP |
564 | return NULL; |
565 | ||
566 | if (t == JOB_START) | |
567 | return format_table->finished_start_job[result]; | |
568 | else if (t == JOB_STOP || t == JOB_RESTART) | |
569 | return format_table->finished_stop_job[result]; | |
570 | ||
571 | return NULL; | |
572 | } | |
e02cd6f7 | 573 | |
44a6b1b6 | 574 | _pure_ static const char *job_get_status_message_format_try_harder(Unit *u, JobType t, JobResult result) { |
877d54e9 LP |
575 | const char *format; |
576 | ||
577 | assert(u); | |
578 | assert(t >= 0); | |
579 | assert(t < _JOB_TYPE_MAX); | |
580 | ||
581 | format = job_get_status_message_format(u, t, result); | |
582 | if (format) | |
583 | return format; | |
584 | ||
585 | /* Return generic strings */ | |
e02cd6f7 | 586 | if (t == JOB_START) { |
877d54e9 LP |
587 | if (result == JOB_DONE) |
588 | return "Started %s."; | |
589 | else if (result == JOB_FAILED) | |
590 | return "Failed to start %s."; | |
591 | else if (result == JOB_DEPENDENCY) | |
592 | return "Dependency failed for %s."; | |
593 | else if (result == JOB_TIMEOUT) | |
594 | return "Timed out starting %s."; | |
595 | } else if (t == JOB_STOP || t == JOB_RESTART) { | |
596 | if (result == JOB_DONE) | |
597 | return "Stopped %s."; | |
598 | else if (result == JOB_FAILED) | |
599 | return "Stopped (with error) %s."; | |
600 | else if (result == JOB_TIMEOUT) | |
601 | return "Timed out stoppping %s."; | |
602 | } else if (t == JOB_RELOAD) { | |
603 | if (result == JOB_DONE) | |
604 | return "Reloaded %s."; | |
605 | else if (result == JOB_FAILED) | |
606 | return "Reload failed for %s."; | |
607 | else if (result == JOB_TIMEOUT) | |
608 | return "Timed out reloading %s."; | |
609 | } | |
610 | ||
611 | return NULL; | |
612 | } | |
613 | ||
614 | static void job_print_status_message(Unit *u, JobType t, JobResult result) { | |
615 | const char *format; | |
e02cd6f7 | 616 | |
877d54e9 LP |
617 | assert(u); |
618 | assert(t >= 0); | |
619 | assert(t < _JOB_TYPE_MAX); | |
620 | ||
bcfce235 LP |
621 | DISABLE_WARNING_FORMAT_NONLITERAL; |
622 | ||
877d54e9 LP |
623 | if (t == JOB_START) { |
624 | format = job_get_status_message_format(u, t, result); | |
c6918296 MS |
625 | if (!format) |
626 | return; | |
627 | ||
e02cd6f7 LP |
628 | switch (result) { |
629 | ||
630 | case JOB_DONE: | |
69120666 | 631 | if (u->condition_result) |
076a24ad | 632 | unit_status_printf(u, ANSI_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, format); |
e02cd6f7 LP |
633 | break; |
634 | ||
635 | case JOB_FAILED: | |
cb8ccb22 | 636 | manager_flip_auto_status(u->manager, true); |
49b1d377 | 637 | unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON "FAILED" ANSI_HIGHLIGHT_OFF, format); |
984a2be4 | 638 | manager_status_printf(u->manager, false, NULL, "See 'systemctl status %s' for details.", u->id); |
e02cd6f7 LP |
639 | break; |
640 | ||
641 | case JOB_DEPENDENCY: | |
cb8ccb22 | 642 | manager_flip_auto_status(u->manager, true); |
49b1d377 | 643 | unit_status_printf(u, ANSI_HIGHLIGHT_YELLOW_ON "DEPEND" ANSI_HIGHLIGHT_OFF, format); |
e02cd6f7 LP |
644 | break; |
645 | ||
646 | case JOB_TIMEOUT: | |
cb8ccb22 | 647 | manager_flip_auto_status(u->manager, true); |
49b1d377 | 648 | unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, format); |
e02cd6f7 LP |
649 | break; |
650 | ||
651 | default: | |
652 | ; | |
653 | } | |
654 | ||
1f136e7a | 655 | } else if (t == JOB_STOP || t == JOB_RESTART) { |
e02cd6f7 | 656 | |
877d54e9 | 657 | format = job_get_status_message_format(u, t, result); |
c6918296 MS |
658 | if (!format) |
659 | return; | |
660 | ||
e02cd6f7 LP |
661 | switch (result) { |
662 | ||
663 | case JOB_TIMEOUT: | |
cb8ccb22 | 664 | manager_flip_auto_status(u->manager, true); |
49b1d377 | 665 | unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, format); |
e02cd6f7 LP |
666 | break; |
667 | ||
668 | case JOB_DONE: | |
669 | case JOB_FAILED: | |
076a24ad | 670 | unit_status_printf(u, ANSI_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, format); |
e02cd6f7 LP |
671 | break; |
672 | ||
673 | default: | |
674 | ; | |
675 | } | |
7cf82e0b MS |
676 | |
677 | } else if (t == JOB_VERIFY_ACTIVE) { | |
678 | ||
679 | /* When verify-active detects the unit is inactive, report it. | |
680 | * Most likely a DEPEND warning from a requisiting unit will | |
681 | * occur next and it's nice to see what was requisited. */ | |
682 | if (result == JOB_SKIPPED) | |
49b1d377 | 683 | unit_status_printf(u, ANSI_HIGHLIGHT_ON " INFO " ANSI_HIGHLIGHT_OFF, "%s is not active."); |
e02cd6f7 | 684 | } |
bcfce235 LP |
685 | |
686 | REENABLE_WARNING; | |
e02cd6f7 LP |
687 | } |
688 | ||
877d54e9 LP |
689 | static void job_log_status_message(Unit *u, JobType t, JobResult result) { |
690 | const char *format; | |
691 | char buf[LINE_MAX]; | |
692 | ||
693 | assert(u); | |
694 | assert(t >= 0); | |
695 | assert(t < _JOB_TYPE_MAX); | |
696 | ||
81270860 LP |
697 | /* Skip this if it goes to the console. since we already print |
698 | * to the console anyway... */ | |
699 | ||
700 | if (log_on_console()) | |
701 | return; | |
702 | ||
877d54e9 LP |
703 | format = job_get_status_message_format_try_harder(u, t, result); |
704 | if (!format) | |
705 | return; | |
706 | ||
bcfce235 | 707 | DISABLE_WARNING_FORMAT_NONLITERAL; |
877d54e9 LP |
708 | snprintf(buf, sizeof(buf), format, unit_description(u)); |
709 | char_array_0(buf); | |
bcfce235 | 710 | REENABLE_WARNING; |
877d54e9 LP |
711 | |
712 | if (t == JOB_START) { | |
713 | sd_id128_t mid; | |
714 | ||
715 | mid = result == JOB_DONE ? SD_MESSAGE_UNIT_STARTED : SD_MESSAGE_UNIT_FAILED; | |
bbc9006e MT |
716 | log_struct_unit(result == JOB_DONE ? LOG_INFO : LOG_ERR, |
717 | u->id, | |
1ca6783f | 718 | MESSAGE_ID(mid), |
877d54e9 LP |
719 | "RESULT=%s", job_result_to_string(result), |
720 | "MESSAGE=%s", buf, | |
721 | NULL); | |
722 | ||
723 | } else if (t == JOB_STOP) | |
bbc9006e MT |
724 | log_struct_unit(result == JOB_DONE ? LOG_INFO : LOG_ERR, |
725 | u->id, | |
1ca6783f | 726 | MESSAGE_ID(SD_MESSAGE_UNIT_STOPPED), |
877d54e9 LP |
727 | "RESULT=%s", job_result_to_string(result), |
728 | "MESSAGE=%s", buf, | |
729 | NULL); | |
730 | ||
731 | else if (t == JOB_RELOAD) | |
bbc9006e MT |
732 | log_struct_unit(result == JOB_DONE ? LOG_INFO : LOG_ERR, |
733 | u->id, | |
1ca6783f | 734 | MESSAGE_ID(SD_MESSAGE_UNIT_RELOADED), |
877d54e9 LP |
735 | "RESULT=%s", job_result_to_string(result), |
736 | "MESSAGE=%s", buf, | |
737 | NULL); | |
738 | } | |
877d54e9 | 739 | |
5273510e | 740 | int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) { |
87f0e418 LP |
741 | Unit *u; |
742 | Unit *other; | |
b866264a | 743 | JobType t; |
034c6ed7 | 744 | Iterator i; |
5cb5a6ff LP |
745 | |
746 | assert(j); | |
ac1135be | 747 | assert(j->installed); |
e0209d83 | 748 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
5cb5a6ff | 749 | |
c6918296 MS |
750 | u = j->unit; |
751 | t = j->type; | |
752 | ||
753 | j->result = result; | |
754 | ||
637f8b8e MS |
755 | if (j->state == JOB_RUNNING) |
756 | j->manager->n_running_jobs--; | |
757 | ||
66870f90 ZJS |
758 | log_debug_unit(u->id, "Job %s/%s finished, result=%s", |
759 | u->id, job_type_to_string(t), job_result_to_string(result)); | |
c6918296 MS |
760 | |
761 | job_print_status_message(u, t, result); | |
877d54e9 | 762 | job_log_status_message(u, t, result); |
c6918296 | 763 | |
c1e1601e | 764 | job_add_to_dbus_queue(j); |
f50e0a01 | 765 | |
034c6ed7 | 766 | /* Patch restart jobs so that they become normal start jobs */ |
c6918296 | 767 | if (result == JOB_DONE && t == JOB_RESTART) { |
f50e0a01 | 768 | |
bbd1a837 | 769 | job_change_type(j, JOB_START); |
9c2d9caa | 770 | j->state = JOB_WAITING; |
cc42e081 LP |
771 | |
772 | job_add_to_run_queue(j); | |
57981b98 | 773 | |
57981b98 | 774 | goto finish; |
5cb5a6ff LP |
775 | } |
776 | ||
6a371e23 | 777 | if (result == JOB_FAILED || result == JOB_INVALID) |
76bf48b7 LP |
778 | j->manager->n_failed_jobs ++; |
779 | ||
97e7d748 | 780 | job_uninstall(j); |
5cb5a6ff LP |
781 | job_free(j); |
782 | ||
783 | /* Fail depending jobs on failure */ | |
5273510e | 784 | if (result != JOB_DONE && recursive) { |
5cb5a6ff LP |
785 | |
786 | if (t == JOB_START || | |
e0209d83 | 787 | t == JOB_VERIFY_ACTIVE) { |
5cb5a6ff | 788 | |
ac155bb8 MS |
789 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY], i) |
790 | if (other->job && | |
791 | (other->job->type == JOB_START || | |
e0209d83 | 792 | other->job->type == JOB_VERIFY_ACTIVE)) |
5273510e | 793 | job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true); |
5cb5a6ff | 794 | |
ac155bb8 MS |
795 | SET_FOREACH(other, u->dependencies[UNIT_BOUND_BY], i) |
796 | if (other->job && | |
797 | (other->job->type == JOB_START || | |
e0209d83 | 798 | other->job->type == JOB_VERIFY_ACTIVE)) |
5273510e | 799 | job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true); |
e6a3ff95 | 800 | |
ac155bb8 MS |
801 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i) |
802 | if (other->job && | |
803 | !other->job->override && | |
804 | (other->job->type == JOB_START || | |
e0209d83 | 805 | other->job->type == JOB_VERIFY_ACTIVE)) |
5273510e | 806 | job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true); |
5cb5a6ff LP |
807 | |
808 | } else if (t == JOB_STOP) { | |
809 | ||
ac155bb8 MS |
810 | SET_FOREACH(other, u->dependencies[UNIT_CONFLICTED_BY], i) |
811 | if (other->job && | |
812 | (other->job->type == JOB_START || | |
e0209d83 | 813 | other->job->type == JOB_VERIFY_ACTIVE)) |
5273510e | 814 | job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true); |
5cb5a6ff LP |
815 | } |
816 | } | |
817 | ||
c0daa706 | 818 | /* Trigger OnFailure dependencies that are not generated by |
66870f90 | 819 | * the unit itself. We don't treat JOB_CANCELED as failure in |
c0daa706 LP |
820 | * this context. And JOB_FAILURE is already handled by the |
821 | * unit itself. */ | |
222ae6a8 | 822 | if (result == JOB_TIMEOUT || result == JOB_DEPENDENCY) { |
bbc9006e MT |
823 | log_struct_unit(LOG_NOTICE, |
824 | u->id, | |
66870f90 | 825 | "JOB_TYPE=%s", job_type_to_string(t), |
38c888a4 | 826 | "JOB_RESULT=%s", job_result_to_string(result), |
66870f90 | 827 | "Job %s/%s failed with result '%s'.", |
ac155bb8 | 828 | u->id, |
222ae6a8 | 829 | job_type_to_string(t), |
66870f90 ZJS |
830 | job_result_to_string(result), |
831 | NULL); | |
222ae6a8 | 832 | |
3ecaa09b | 833 | unit_start_on_failure(u); |
222ae6a8 | 834 | } |
c0daa706 | 835 | |
3ecaa09b LP |
836 | unit_trigger_notify(u); |
837 | ||
57981b98 | 838 | finish: |
5cb5a6ff | 839 | /* Try to start the next jobs that can be started */ |
ac155bb8 MS |
840 | SET_FOREACH(other, u->dependencies[UNIT_AFTER], i) |
841 | if (other->job) | |
842 | job_add_to_run_queue(other->job); | |
843 | SET_FOREACH(other, u->dependencies[UNIT_BEFORE], i) | |
844 | if (other->job) | |
845 | job_add_to_run_queue(other->job); | |
5cb5a6ff | 846 | |
ac155bb8 | 847 | manager_check_finished(u->manager); |
b0c918b9 | 848 | |
5273510e | 849 | return 0; |
5cb5a6ff | 850 | } |
034c6ed7 | 851 | |
718db961 LP |
852 | static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *userdata) { |
853 | Job *j = userdata; | |
faf919f1 | 854 | |
718db961 LP |
855 | assert(j); |
856 | assert(s == j->timer_event_source); | |
faf919f1 | 857 | |
718db961 LP |
858 | log_warning_unit(j->unit->id, "Job %s/%s timed out.", |
859 | j->unit->id, job_type_to_string(j->type)); | |
faf919f1 | 860 | |
718db961 LP |
861 | job_finish_and_invalidate(j, JOB_TIMEOUT, true); |
862 | return 0; | |
863 | } | |
faf919f1 | 864 | |
718db961 LP |
865 | int job_start_timer(Job *j) { |
866 | int r; | |
faf919f1 | 867 | |
8bb310c3 | 868 | if (j->timer_event_source) |
718db961 | 869 | return 0; |
faf919f1 | 870 | |
718db961 | 871 | j->begin_usec = now(CLOCK_MONOTONIC); |
faf919f1 | 872 | |
8bb310c3 ZJS |
873 | if (j->unit->job_timeout <= 0) |
874 | return 0; | |
875 | ||
6a0f1f6d LP |
876 | r = sd_event_add_time( |
877 | j->manager->event, | |
878 | &j->timer_event_source, | |
879 | CLOCK_MONOTONIC, | |
880 | j->begin_usec + j->unit->job_timeout, 0, | |
881 | job_dispatch_timer, j); | |
718db961 LP |
882 | if (r < 0) |
883 | return r; | |
faf919f1 | 884 | |
718db961 | 885 | return 0; |
faf919f1 LP |
886 | } |
887 | ||
c1e1601e | 888 | void job_add_to_run_queue(Job *j) { |
034c6ed7 | 889 | assert(j); |
ac1135be | 890 | assert(j->installed); |
034c6ed7 LP |
891 | |
892 | if (j->in_run_queue) | |
893 | return; | |
894 | ||
752b5905 LP |
895 | if (!j->manager->run_queue) |
896 | sd_event_source_set_enabled(j->manager->run_queue_event_source, SD_EVENT_ONESHOT); | |
897 | ||
71fda00f | 898 | LIST_PREPEND(run_queue, j->manager->run_queue, j); |
034c6ed7 LP |
899 | j->in_run_queue = true; |
900 | } | |
94f04347 | 901 | |
c1e1601e LP |
902 | void job_add_to_dbus_queue(Job *j) { |
903 | assert(j); | |
904 | assert(j->installed); | |
905 | ||
906 | if (j->in_dbus_queue) | |
907 | return; | |
908 | ||
a567261a LP |
909 | /* We don't check if anybody is subscribed here, since this |
910 | * job might just have been created and not yet assigned to a | |
911 | * connection/client. */ | |
94b6dfa2 | 912 | |
71fda00f | 913 | LIST_PREPEND(dbus_queue, j->manager->dbus_job_queue, j); |
c1e1601e LP |
914 | j->in_dbus_queue = true; |
915 | } | |
916 | ||
ea430986 LP |
917 | char *job_dbus_path(Job *j) { |
918 | char *p; | |
919 | ||
920 | assert(j); | |
921 | ||
ccd06097 | 922 | if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0) |
ea430986 LP |
923 | return NULL; |
924 | ||
925 | return p; | |
926 | } | |
927 | ||
39a18c60 MS |
928 | int job_serialize(Job *j, FILE *f, FDSet *fds) { |
929 | fprintf(f, "job-id=%u\n", j->id); | |
930 | fprintf(f, "job-type=%s\n", job_type_to_string(j->type)); | |
931 | fprintf(f, "job-state=%s\n", job_state_to_string(j->state)); | |
932 | fprintf(f, "job-override=%s\n", yes_no(j->override)); | |
23ade460 | 933 | fprintf(f, "job-irreversible=%s\n", yes_no(j->irreversible)); |
39a18c60 MS |
934 | fprintf(f, "job-sent-dbus-new-signal=%s\n", yes_no(j->sent_dbus_new_signal)); |
935 | fprintf(f, "job-ignore-order=%s\n", yes_no(j->ignore_order)); | |
718db961 LP |
936 | |
937 | if (j->begin_usec > 0) | |
ccd06097 | 938 | fprintf(f, "job-begin="USEC_FMT"\n", j->begin_usec); |
718db961 | 939 | |
8f8f05a9 | 940 | bus_track_serialize(j->subscribed, f); |
39a18c60 MS |
941 | |
942 | /* End marker */ | |
943 | fputc('\n', f); | |
944 | return 0; | |
945 | } | |
946 | ||
947 | int job_deserialize(Job *j, FILE *f, FDSet *fds) { | |
718db961 LP |
948 | assert(j); |
949 | ||
39a18c60 MS |
950 | for (;;) { |
951 | char line[LINE_MAX], *l, *v; | |
952 | size_t k; | |
953 | ||
954 | if (!fgets(line, sizeof(line), f)) { | |
955 | if (feof(f)) | |
956 | return 0; | |
957 | return -errno; | |
958 | } | |
959 | ||
960 | char_array_0(line); | |
961 | l = strstrip(line); | |
962 | ||
963 | /* End marker */ | |
964 | if (l[0] == 0) | |
965 | return 0; | |
966 | ||
967 | k = strcspn(l, "="); | |
968 | ||
969 | if (l[k] == '=') { | |
970 | l[k] = 0; | |
971 | v = l+k+1; | |
972 | } else | |
973 | v = l+k; | |
974 | ||
975 | if (streq(l, "job-id")) { | |
718db961 | 976 | |
39a18c60 MS |
977 | if (safe_atou32(v, &j->id) < 0) |
978 | log_debug("Failed to parse job id value %s", v); | |
718db961 | 979 | |
39a18c60 | 980 | } else if (streq(l, "job-type")) { |
718db961 LP |
981 | JobType t; |
982 | ||
983 | t = job_type_from_string(v); | |
39a18c60 MS |
984 | if (t < 0) |
985 | log_debug("Failed to parse job type %s", v); | |
e0209d83 MS |
986 | else if (t >= _JOB_TYPE_MAX_IN_TRANSACTION) |
987 | log_debug("Cannot deserialize job of type %s", v); | |
39a18c60 MS |
988 | else |
989 | j->type = t; | |
718db961 | 990 | |
39a18c60 | 991 | } else if (streq(l, "job-state")) { |
718db961 LP |
992 | JobState s; |
993 | ||
994 | s = job_state_from_string(v); | |
39a18c60 MS |
995 | if (s < 0) |
996 | log_debug("Failed to parse job state %s", v); | |
997 | else | |
998 | j->state = s; | |
718db961 | 999 | |
39a18c60 | 1000 | } else if (streq(l, "job-override")) { |
718db961 LP |
1001 | int b; |
1002 | ||
1003 | b = parse_boolean(v); | |
39a18c60 MS |
1004 | if (b < 0) |
1005 | log_debug("Failed to parse job override flag %s", v); | |
1006 | else | |
1007 | j->override = j->override || b; | |
718db961 | 1008 | |
23ade460 | 1009 | } else if (streq(l, "job-irreversible")) { |
718db961 LP |
1010 | int b; |
1011 | ||
1012 | b = parse_boolean(v); | |
23ade460 MS |
1013 | if (b < 0) |
1014 | log_debug("Failed to parse job irreversible flag %s", v); | |
1015 | else | |
1016 | j->irreversible = j->irreversible || b; | |
718db961 | 1017 | |
39a18c60 | 1018 | } else if (streq(l, "job-sent-dbus-new-signal")) { |
718db961 LP |
1019 | int b; |
1020 | ||
1021 | b = parse_boolean(v); | |
39a18c60 MS |
1022 | if (b < 0) |
1023 | log_debug("Failed to parse job sent_dbus_new_signal flag %s", v); | |
1024 | else | |
1025 | j->sent_dbus_new_signal = j->sent_dbus_new_signal || b; | |
718db961 | 1026 | |
39a18c60 | 1027 | } else if (streq(l, "job-ignore-order")) { |
718db961 LP |
1028 | int b; |
1029 | ||
1030 | b = parse_boolean(v); | |
39a18c60 MS |
1031 | if (b < 0) |
1032 | log_debug("Failed to parse job ignore_order flag %s", v); | |
1033 | else | |
1034 | j->ignore_order = j->ignore_order || b; | |
718db961 LP |
1035 | |
1036 | } else if (streq(l, "job-begin")) { | |
1037 | unsigned long long ull; | |
1038 | ||
1039 | if (sscanf(v, "%llu", &ull) != 1) | |
1040 | log_debug("Failed to parse job-begin value %s", v); | |
39a18c60 | 1041 | else |
718db961 LP |
1042 | j->begin_usec = ull; |
1043 | ||
8f8f05a9 | 1044 | } else if (streq(l, "subscribed")) { |
718db961 | 1045 | |
8f8f05a9 LP |
1046 | if (strv_extend(&j->deserialized_subscribed, v) < 0) |
1047 | return log_oom(); | |
39a18c60 MS |
1048 | } |
1049 | } | |
1050 | } | |
1051 | ||
1052 | int job_coldplug(Job *j) { | |
718db961 LP |
1053 | int r; |
1054 | ||
1055 | assert(j); | |
39a18c60 | 1056 | |
8f8f05a9 LP |
1057 | /* After deserialization is complete and the bus connection |
1058 | * set up again, let's start watching our subscribers again */ | |
1059 | r = bus_track_coldplug(j->manager, &j->subscribed, &j->deserialized_subscribed); | |
1060 | if (r < 0) | |
1061 | return r; | |
1062 | ||
1727a595 MM |
1063 | if (j->state == JOB_WAITING) |
1064 | job_add_to_run_queue(j); | |
1065 | ||
8bb310c3 | 1066 | if (j->begin_usec == 0 || j->unit->job_timeout == 0) |
39a18c60 MS |
1067 | return 0; |
1068 | ||
718db961 LP |
1069 | if (j->timer_event_source) |
1070 | j->timer_event_source = sd_event_source_unref(j->timer_event_source); | |
39a18c60 | 1071 | |
6a0f1f6d LP |
1072 | r = sd_event_add_time( |
1073 | j->manager->event, | |
1074 | &j->timer_event_source, | |
1075 | CLOCK_MONOTONIC, | |
1076 | j->begin_usec + j->unit->job_timeout, 0, | |
1077 | job_dispatch_timer, j); | |
718db961 LP |
1078 | if (r < 0) |
1079 | log_debug("Failed to restart timeout for job: %s", strerror(-r)); | |
1080 | ||
1081 | return r; | |
39a18c60 MS |
1082 | } |
1083 | ||
c65eb836 LP |
1084 | void job_shutdown_magic(Job *j) { |
1085 | assert(j); | |
1086 | ||
1087 | /* The shutdown target gets some special treatment here: we | |
1088 | * tell the kernel to begin with flushing its disk caches, to | |
1089 | * optimize shutdown time a bit. Ideally we wouldn't hardcode | |
1090 | * this magic into PID 1. However all other processes aren't | |
1091 | * options either since they'd exit much sooner than PID 1 and | |
1092 | * asynchronous sync() would cause their exit to be | |
1093 | * delayed. */ | |
1094 | ||
c2756a68 | 1095 | if (j->type != JOB_START) |
c65eb836 LP |
1096 | return; |
1097 | ||
c2756a68 LP |
1098 | if (j->unit->manager->running_as != SYSTEMD_SYSTEM) |
1099 | return; | |
1100 | ||
1101 | if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET)) | |
c65eb836 LP |
1102 | return; |
1103 | ||
5b1869ea OB |
1104 | /* In case messages on console has been disabled on boot */ |
1105 | j->unit->manager->no_console_output = false; | |
1106 | ||
c65eb836 LP |
1107 | if (detect_container(NULL) > 0) |
1108 | return; | |
1109 | ||
1110 | asynchronous_sync(); | |
1111 | } | |
1112 | ||
68db7a3b ZJS |
1113 | int job_get_timeout(Job *j, uint64_t *timeout) { |
1114 | Unit *u = j->unit; | |
1115 | uint64_t x = -1, y = -1; | |
1116 | int r = 0, q = 0; | |
1117 | ||
1118 | assert(u); | |
1119 | ||
1120 | if (j->timer_event_source) { | |
1121 | r = sd_event_source_get_time(j->timer_event_source, &x); | |
1122 | if (r < 0) | |
1123 | return r; | |
1124 | r = 1; | |
1125 | } | |
1126 | ||
1127 | if (UNIT_VTABLE(u)->get_timeout) { | |
1128 | q = UNIT_VTABLE(u)->get_timeout(u, &y); | |
1129 | if (q < 0) | |
1130 | return q; | |
1131 | } | |
1132 | ||
1133 | if (r == 0 && q == 0) | |
1134 | return 0; | |
1135 | ||
1136 | *timeout = MIN(x, y); | |
1137 | ||
68db7a3b ZJS |
1138 | return 1; |
1139 | } | |
1140 | ||
94f04347 LP |
1141 | static const char* const job_state_table[_JOB_STATE_MAX] = { |
1142 | [JOB_WAITING] = "waiting", | |
1143 | [JOB_RUNNING] = "running" | |
1144 | }; | |
1145 | ||
1146 | DEFINE_STRING_TABLE_LOOKUP(job_state, JobState); | |
1147 | ||
1148 | static const char* const job_type_table[_JOB_TYPE_MAX] = { | |
1149 | [JOB_START] = "start", | |
1150 | [JOB_VERIFY_ACTIVE] = "verify-active", | |
1151 | [JOB_STOP] = "stop", | |
1152 | [JOB_RELOAD] = "reload", | |
1153 | [JOB_RELOAD_OR_START] = "reload-or-start", | |
1154 | [JOB_RESTART] = "restart", | |
1155 | [JOB_TRY_RESTART] = "try-restart", | |
e0209d83 | 1156 | [JOB_NOP] = "nop", |
94f04347 LP |
1157 | }; |
1158 | ||
1159 | DEFINE_STRING_TABLE_LOOKUP(job_type, JobType); | |
b548631a LP |
1160 | |
1161 | static const char* const job_mode_table[_JOB_MODE_MAX] = { | |
1162 | [JOB_FAIL] = "fail", | |
c497c7a9 | 1163 | [JOB_REPLACE] = "replace", |
23ade460 | 1164 | [JOB_REPLACE_IRREVERSIBLY] = "replace-irreversibly", |
e67c3609 | 1165 | [JOB_ISOLATE] = "isolate", |
2c5859af | 1166 | [JOB_FLUSH] = "flush", |
cebe0d41 | 1167 | [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies", |
255baef6 | 1168 | [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements", |
b548631a LP |
1169 | }; |
1170 | ||
1171 | DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode); | |
5d44db4a LP |
1172 | |
1173 | static const char* const job_result_table[_JOB_RESULT_MAX] = { | |
1174 | [JOB_DONE] = "done", | |
1175 | [JOB_CANCELED] = "canceled", | |
1176 | [JOB_TIMEOUT] = "timeout", | |
1177 | [JOB_FAILED] = "failed", | |
d68201e9 | 1178 | [JOB_DEPENDENCY] = "dependency", |
6a371e23 ZJS |
1179 | [JOB_SKIPPED] = "skipped", |
1180 | [JOB_INVALID] = "invalid", | |
5d44db4a LP |
1181 | }; |
1182 | ||
1183 | DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult); |