]>
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 | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
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 | |
94f04347 LP |
27 | #include "set.h" |
28 | #include "unit.h" | |
60918275 | 29 | #include "macro.h" |
94f04347 LP |
30 | #include "strv.h" |
31 | #include "load-fragment.h" | |
32 | #include "load-dropin.h" | |
f50e0a01 | 33 | #include "log.h" |
4139c1b2 | 34 | #include "dbus-job.h" |
60918275 | 35 | |
87f0e418 | 36 | Job* job_new(Manager *m, JobType type, Unit *unit) { |
60918275 LP |
37 | Job *j; |
38 | ||
39 | assert(m); | |
40 | assert(type < _JOB_TYPE_MAX); | |
87f0e418 | 41 | assert(unit); |
60918275 LP |
42 | |
43 | if (!(j = new0(Job, 1))) | |
44 | return NULL; | |
45 | ||
46 | j->manager = m; | |
47 | j->id = m->current_job_id++; | |
48 | j->type = type; | |
87f0e418 | 49 | j->unit = unit; |
60918275 | 50 | |
faf919f1 LP |
51 | j->timer_watch.type = WATCH_INVALID; |
52 | ||
e5b5ae50 | 53 | /* We don't link it here, that's what job_dependency() is for */ |
60918275 LP |
54 | |
55 | return j; | |
56 | } | |
57 | ||
60918275 LP |
58 | void job_free(Job *j) { |
59 | assert(j); | |
60 | ||
61 | /* Detach from next 'bigger' objects */ | |
ac1135be | 62 | if (j->installed) { |
7535cc78 | 63 | bus_job_send_removed_signal(j, !j->failed); |
c1e1601e | 64 | |
701cc384 | 65 | if (j->unit->meta.job == j) { |
87f0e418 | 66 | j->unit->meta.job = NULL; |
701cc384 LP |
67 | unit_add_to_gc_queue(j->unit); |
68 | } | |
60918275 LP |
69 | |
70 | hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id)); | |
ac1135be | 71 | j->installed = false; |
60918275 LP |
72 | } |
73 | ||
5cb5a6ff | 74 | /* Detach from next 'smaller' objects */ |
23a177ef | 75 | manager_transaction_unlink_job(j->manager, j, true); |
60918275 | 76 | |
c1e1601e LP |
77 | if (j->in_run_queue) |
78 | LIST_REMOVE(Job, run_queue, j->manager->run_queue, j); | |
79 | ||
80 | if (j->in_dbus_queue) | |
81 | LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j); | |
82 | ||
faf919f1 LP |
83 | if (j->timer_watch.type != WATCH_INVALID) { |
84 | assert(j->timer_watch.type == WATCH_JOB_TIMER); | |
85 | assert(j->timer_watch.data.job == j); | |
86 | assert(j->timer_watch.fd >= 0); | |
87 | ||
88 | assert_se(epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_DEL, j->timer_watch.fd, NULL) >= 0); | |
89 | close_nointr_nofail(j->timer_watch.fd); | |
90 | } | |
91 | ||
a567261a | 92 | free(j->bus_client); |
60918275 LP |
93 | free(j); |
94 | } | |
a66d02c3 | 95 | |
69dd2852 | 96 | JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) { |
e5b5ae50 LP |
97 | JobDependency *l; |
98 | ||
99 | assert(object); | |
100 | ||
101 | /* Adds a new job link, which encodes that the 'subject' job | |
102 | * needs the 'object' job in some way. If 'subject' is NULL | |
103 | * this means the 'anchor' job (i.e. the one the user | |
104 | * explcitily asked for) is the requester. */ | |
105 | ||
ceed3570 | 106 | if (!(l = new0(JobDependency, 1))) |
e5b5ae50 LP |
107 | return NULL; |
108 | ||
109 | l->subject = subject; | |
110 | l->object = object; | |
111 | l->matters = matters; | |
69dd2852 | 112 | l->conflicts = conflicts; |
e5b5ae50 | 113 | |
44d8db9e LP |
114 | if (subject) |
115 | LIST_PREPEND(JobDependency, subject, subject->subject_list, l); | |
116 | else | |
117 | LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l); | |
e5b5ae50 | 118 | |
44d8db9e | 119 | LIST_PREPEND(JobDependency, object, object->object_list, l); |
e5b5ae50 LP |
120 | |
121 | return l; | |
122 | } | |
123 | ||
124 | void job_dependency_free(JobDependency *l) { | |
125 | assert(l); | |
126 | ||
44d8db9e LP |
127 | if (l->subject) |
128 | LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l); | |
e5b5ae50 | 129 | else |
44d8db9e | 130 | LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l); |
e5b5ae50 | 131 | |
44d8db9e | 132 | LIST_REMOVE(JobDependency, object, l->object->object_list, l); |
e5b5ae50 LP |
133 | |
134 | free(l); | |
135 | } | |
136 | ||
1ffba6fe | 137 | void job_dump(Job *j, FILE*f, const char *prefix) { |
a66d02c3 LP |
138 | assert(j); |
139 | assert(f); | |
140 | ||
9eb63b3c LP |
141 | if (!prefix) |
142 | prefix = ""; | |
143 | ||
ceed3570 | 144 | fprintf(f, |
40d50879 LP |
145 | "%s-> Job %u:\n" |
146 | "%s\tAction: %s -> %s\n" | |
5cb5a6ff LP |
147 | "%s\tState: %s\n" |
148 | "%s\tForced: %s\n", | |
ceed3570 | 149 | prefix, j->id, |
9e2f7c11 | 150 | prefix, j->unit->meta.id, job_type_to_string(j->type), |
94f04347 | 151 | prefix, job_state_to_string(j->state), |
9e2f7c11 | 152 | prefix, yes_no(j->override)); |
a66d02c3 | 153 | } |
e5b5ae50 LP |
154 | |
155 | bool job_is_anchor(Job *j) { | |
156 | JobDependency *l; | |
157 | ||
158 | assert(j); | |
159 | ||
44d8db9e | 160 | LIST_FOREACH(object, l, j->object_list) |
e5b5ae50 LP |
161 | if (!l->subject) |
162 | return true; | |
163 | ||
164 | return false; | |
165 | } | |
1ffba6fe LP |
166 | |
167 | static bool types_match(JobType a, JobType b, JobType c, JobType d) { | |
168 | return | |
169 | (a == c && b == d) || | |
170 | (a == d && b == c); | |
171 | } | |
172 | ||
173 | int job_type_merge(JobType *a, JobType b) { | |
174 | if (*a == b) | |
175 | return 0; | |
176 | ||
177 | /* Merging is associative! a merged with b merged with c is | |
178 | * the same as a merged with c merged with b. */ | |
179 | ||
180 | /* Mergeability is transitive! if a can be merged with b and b | |
181 | * with c then a also with c */ | |
182 | ||
183 | /* Also, if a merged with b cannot be merged with c, then | |
184 | * either a or b cannot be merged with c either */ | |
185 | ||
5cb5a6ff | 186 | if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE)) |
1ffba6fe LP |
187 | *a = JOB_START; |
188 | else if (types_match(*a, b, JOB_START, JOB_RELOAD) || | |
189 | types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) || | |
5cb5a6ff | 190 | types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) || |
1ffba6fe LP |
191 | types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START)) |
192 | *a = JOB_RELOAD_OR_START; | |
193 | else if (types_match(*a, b, JOB_START, JOB_RESTART) || | |
194 | types_match(*a, b, JOB_START, JOB_TRY_RESTART) || | |
5cb5a6ff | 195 | types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) || |
1ffba6fe LP |
196 | types_match(*a, b, JOB_RELOAD, JOB_RESTART) || |
197 | types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) || | |
198 | types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) || | |
199 | types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART)) | |
200 | *a = JOB_RESTART; | |
5cb5a6ff | 201 | else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD)) |
1ffba6fe | 202 | *a = JOB_RELOAD; |
5cb5a6ff | 203 | else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) || |
1ffba6fe LP |
204 | types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART)) |
205 | *a = JOB_TRY_RESTART; | |
206 | else | |
207 | return -EEXIST; | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
5cb5a6ff | 212 | bool job_type_is_mergeable(JobType a, JobType b) { |
1ffba6fe LP |
213 | return job_type_merge(&a, b) >= 0; |
214 | } | |
215 | ||
216 | bool job_type_is_superset(JobType a, JobType b) { | |
217 | ||
5cb5a6ff LP |
218 | /* Checks whether operation a is a "superset" of b in its |
219 | * actions */ | |
1ffba6fe LP |
220 | |
221 | if (a == b) | |
222 | return true; | |
223 | ||
224 | switch (a) { | |
225 | case JOB_START: | |
5cb5a6ff | 226 | return b == JOB_VERIFY_ACTIVE; |
1ffba6fe LP |
227 | |
228 | case JOB_RELOAD: | |
5cb5a6ff LP |
229 | return |
230 | b == JOB_VERIFY_ACTIVE; | |
1ffba6fe LP |
231 | |
232 | case JOB_RELOAD_OR_START: | |
233 | return | |
234 | b == JOB_RELOAD || | |
5cb5a6ff LP |
235 | b == JOB_START || |
236 | b == JOB_VERIFY_ACTIVE; | |
1ffba6fe LP |
237 | |
238 | case JOB_RESTART: | |
239 | return | |
240 | b == JOB_START || | |
5cb5a6ff | 241 | b == JOB_VERIFY_ACTIVE || |
1ffba6fe LP |
242 | b == JOB_RELOAD || |
243 | b == JOB_RELOAD_OR_START || | |
244 | b == JOB_TRY_RESTART; | |
245 | ||
246 | case JOB_TRY_RESTART: | |
247 | return | |
5cb5a6ff | 248 | b == JOB_VERIFY_ACTIVE || |
1ffba6fe LP |
249 | b == JOB_RELOAD; |
250 | default: | |
251 | return false; | |
252 | ||
253 | } | |
254 | } | |
e094e853 LP |
255 | |
256 | bool job_type_is_conflicting(JobType a, JobType b) { | |
cd2dbd7d LP |
257 | assert(a >= 0 && a < _JOB_TYPE_MAX); |
258 | assert(b >= 0 && b < _JOB_TYPE_MAX); | |
e094e853 | 259 | |
5cb5a6ff | 260 | return (a == JOB_STOP) != (b == JOB_STOP); |
e094e853 | 261 | } |
cd2dbd7d | 262 | |
593fbdd2 LP |
263 | bool job_type_is_redundant(JobType a, UnitActiveState b) { |
264 | switch (a) { | |
265 | ||
266 | case JOB_START: | |
267 | return | |
268 | b == UNIT_ACTIVE || | |
032ff4af | 269 | b == UNIT_RELOADING; |
593fbdd2 LP |
270 | |
271 | case JOB_STOP: | |
272 | return | |
6124958c | 273 | b == UNIT_INACTIVE || |
fdf20a31 | 274 | b == UNIT_FAILED; |
593fbdd2 LP |
275 | |
276 | case JOB_VERIFY_ACTIVE: | |
277 | return | |
278 | b == UNIT_ACTIVE || | |
032ff4af | 279 | b == UNIT_RELOADING; |
593fbdd2 LP |
280 | |
281 | case JOB_RELOAD: | |
282 | return | |
032ff4af | 283 | b == UNIT_RELOADING; |
593fbdd2 LP |
284 | |
285 | case JOB_RELOAD_OR_START: | |
286 | return | |
287 | b == UNIT_ACTIVATING || | |
032ff4af | 288 | b == UNIT_RELOADING; |
593fbdd2 LP |
289 | |
290 | case JOB_RESTART: | |
291 | return | |
292 | b == UNIT_ACTIVATING; | |
293 | ||
294 | case JOB_TRY_RESTART: | |
295 | return | |
296 | b == UNIT_ACTIVATING; | |
297 | ||
298 | default: | |
299 | assert_not_reached("Invalid job type"); | |
300 | } | |
301 | } | |
302 | ||
5cb5a6ff | 303 | bool job_is_runnable(Job *j) { |
034c6ed7 | 304 | Iterator i; |
87f0e418 | 305 | Unit *other; |
5cb5a6ff LP |
306 | |
307 | assert(j); | |
ac1135be | 308 | assert(j->installed); |
5cb5a6ff | 309 | |
87f0e418 | 310 | /* Checks whether there is any job running for the units this |
5cb5a6ff LP |
311 | * job needs to be running after (in the case of a 'positive' |
312 | * job type) or before (in the case of a 'negative' job type | |
313 | * . */ | |
314 | ||
315 | if (j->type == JOB_START || | |
316 | j->type == JOB_VERIFY_ACTIVE || | |
317 | j->type == JOB_RELOAD || | |
318 | j->type == JOB_RELOAD_OR_START) { | |
319 | ||
320 | /* Immediate result is that the job is or might be | |
321 | * started. In this case lets wait for the | |
322 | * dependencies, regardless whether they are | |
323 | * starting or stopping something. */ | |
324 | ||
87f0e418 | 325 | SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i) |
5cb5a6ff LP |
326 | if (other->meta.job) |
327 | return false; | |
328 | } | |
329 | ||
330 | /* Also, if something else is being stopped and we should | |
331 | * change state after it, then lets wait. */ | |
332 | ||
87f0e418 | 333 | SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i) |
5cb5a6ff LP |
334 | if (other->meta.job && |
335 | (other->meta.job->type == JOB_STOP || | |
336 | other->meta.job->type == JOB_RESTART || | |
337 | other->meta.job->type == JOB_TRY_RESTART)) | |
338 | return false; | |
339 | ||
340 | /* This means that for a service a and a service b where b | |
341 | * shall be started after a: | |
342 | * | |
343 | * start a + start b → 1st step start a, 2nd step start b | |
344 | * start a + stop b → 1st step stop b, 2nd step start a | |
345 | * stop a + start b → 1st step stop a, 2nd step start b | |
346 | * stop a + stop b → 1st step stop b, 2nd step stop a | |
347 | * | |
348 | * This has the side effect that restarts are properly | |
349 | * synchronized too. */ | |
350 | ||
351 | return true; | |
352 | } | |
353 | ||
354 | int job_run_and_invalidate(Job *j) { | |
355 | int r; | |
2cf19a7a LP |
356 | uint32_t id; |
357 | Manager *m; | |
ac1135be | 358 | |
5cb5a6ff | 359 | assert(j); |
ac1135be | 360 | assert(j->installed); |
5cb5a6ff | 361 | |
034c6ed7 LP |
362 | if (j->in_run_queue) { |
363 | LIST_REMOVE(Job, run_queue, j->manager->run_queue, j); | |
364 | j->in_run_queue = false; | |
365 | } | |
5cb5a6ff LP |
366 | |
367 | if (j->state != JOB_WAITING) | |
368 | return 0; | |
369 | ||
034c6ed7 LP |
370 | if (!job_is_runnable(j)) |
371 | return -EAGAIN; | |
372 | ||
83c60c9f | 373 | j->state = JOB_RUNNING; |
c1e1601e | 374 | job_add_to_dbus_queue(j); |
83c60c9f | 375 | |
2cf19a7a LP |
376 | /* While we execute this operation the job might go away (for |
377 | * example: because it is replaced by a new, conflicting | |
378 | * job.) To make sure we don't access a freed job later on we | |
379 | * store the id here, so that we can verify the job is still | |
380 | * valid. */ | |
381 | id = j->id; | |
382 | m = j->manager; | |
383 | ||
5cb5a6ff LP |
384 | switch (j->type) { |
385 | ||
386 | case JOB_START: | |
87f0e418 | 387 | r = unit_start(j->unit); |
5cb5a6ff LP |
388 | if (r == -EBADR) |
389 | r = 0; | |
390 | break; | |
391 | ||
392 | case JOB_VERIFY_ACTIVE: { | |
87f0e418 LP |
393 | UnitActiveState t = unit_active_state(j->unit); |
394 | if (UNIT_IS_ACTIVE_OR_RELOADING(t)) | |
5cb5a6ff | 395 | r = -EALREADY; |
87f0e418 | 396 | else if (t == UNIT_ACTIVATING) |
5cb5a6ff LP |
397 | r = -EAGAIN; |
398 | else | |
399 | r = -ENOEXEC; | |
400 | break; | |
401 | } | |
402 | ||
403 | case JOB_STOP: | |
87f0e418 | 404 | r = unit_stop(j->unit); |
5cb5a6ff LP |
405 | break; |
406 | ||
407 | case JOB_RELOAD: | |
87f0e418 | 408 | r = unit_reload(j->unit); |
5cb5a6ff LP |
409 | break; |
410 | ||
411 | case JOB_RELOAD_OR_START: | |
87f0e418 LP |
412 | if (unit_active_state(j->unit) == UNIT_ACTIVE) |
413 | r = unit_reload(j->unit); | |
5cb5a6ff | 414 | else |
87f0e418 | 415 | r = unit_start(j->unit); |
5cb5a6ff LP |
416 | break; |
417 | ||
418 | case JOB_RESTART: { | |
87f0e418 | 419 | UnitActiveState t = unit_active_state(j->unit); |
fdf20a31 | 420 | if (t == UNIT_INACTIVE || t == UNIT_FAILED || t == UNIT_ACTIVATING) { |
5cb5a6ff | 421 | j->type = JOB_START; |
87f0e418 | 422 | r = unit_start(j->unit); |
5cb5a6ff | 423 | } else |
87f0e418 | 424 | r = unit_stop(j->unit); |
5cb5a6ff LP |
425 | break; |
426 | } | |
427 | ||
428 | case JOB_TRY_RESTART: { | |
87f0e418 | 429 | UnitActiveState t = unit_active_state(j->unit); |
fdf20a31 | 430 | if (t == UNIT_INACTIVE || t == UNIT_FAILED || t == UNIT_DEACTIVATING) |
5cb5a6ff | 431 | r = -ENOEXEC; |
87f0e418 | 432 | else if (t == UNIT_ACTIVATING) { |
5cb5a6ff | 433 | j->type = JOB_START; |
87f0e418 | 434 | r = unit_start(j->unit); |
5cb5a6ff | 435 | } else |
87f0e418 | 436 | r = unit_stop(j->unit); |
5cb5a6ff LP |
437 | break; |
438 | } | |
439 | ||
440 | default: | |
44d8db9e | 441 | assert_not_reached("Unknown job type"); |
5cb5a6ff LP |
442 | } |
443 | ||
2cf19a7a LP |
444 | if ((j = manager_get_job(m, id))) { |
445 | if (r == -EALREADY) | |
446 | r = job_finish_and_invalidate(j, true); | |
447 | else if (r == -EAGAIN) | |
448 | j->state = JOB_WAITING; | |
449 | else if (r < 0) | |
450 | r = job_finish_and_invalidate(j, false); | |
451 | } | |
5cb5a6ff LP |
452 | |
453 | return r; | |
454 | } | |
455 | ||
456 | int job_finish_and_invalidate(Job *j, bool success) { | |
87f0e418 LP |
457 | Unit *u; |
458 | Unit *other; | |
b866264a | 459 | JobType t; |
034c6ed7 | 460 | Iterator i; |
5cb5a6ff LP |
461 | |
462 | assert(j); | |
ac1135be | 463 | assert(j->installed); |
5cb5a6ff | 464 | |
c1e1601e | 465 | job_add_to_dbus_queue(j); |
f50e0a01 | 466 | |
034c6ed7 | 467 | /* Patch restart jobs so that they become normal start jobs */ |
5cb5a6ff | 468 | if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) { |
f50e0a01 | 469 | |
40d50879 | 470 | log_debug("Converting job %s/%s -> %s/%s", |
9e2f7c11 LP |
471 | j->unit->meta.id, job_type_to_string(j->type), |
472 | j->unit->meta.id, job_type_to_string(JOB_START)); | |
f50e0a01 | 473 | |
9c2d9caa | 474 | j->state = JOB_WAITING; |
5cb5a6ff | 475 | j->type = JOB_START; |
cc42e081 LP |
476 | |
477 | job_add_to_run_queue(j); | |
034c6ed7 | 478 | return 0; |
5cb5a6ff LP |
479 | } |
480 | ||
76bf48b7 LP |
481 | j->failed = !success; |
482 | ||
9c2d9caa LP |
483 | log_debug("Job %s/%s finished, success=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(success)); |
484 | ||
76bf48b7 LP |
485 | if (j->failed) |
486 | j->manager->n_failed_jobs ++; | |
487 | ||
87f0e418 | 488 | u = j->unit; |
5cb5a6ff LP |
489 | t = j->type; |
490 | job_free(j); | |
491 | ||
9e58ff9c | 492 | if (!success) |
9aab5a73 | 493 | unit_status_printf(u, "Starting %s " ANSI_HIGHLIGHT_ON "failed" ANSI_HIGHLIGHT_OFF ".\n", unit_description(u)); |
9e58ff9c | 494 | |
5cb5a6ff LP |
495 | /* Fail depending jobs on failure */ |
496 | if (!success) { | |
497 | ||
498 | if (t == JOB_START || | |
499 | t == JOB_VERIFY_ACTIVE || | |
500 | t == JOB_RELOAD_OR_START) { | |
501 | ||
87f0e418 | 502 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i) |
3b6fdb5b LP |
503 | if (!other->meta.ignore_dependency_failure && |
504 | other->meta.job && | |
dda5a135 LP |
505 | (other->meta.job->type == JOB_START || |
506 | other->meta.job->type == JOB_VERIFY_ACTIVE || | |
507 | other->meta.job->type == JOB_RELOAD_OR_START)) | |
5cb5a6ff LP |
508 | job_finish_and_invalidate(other->meta.job, false); |
509 | ||
9e2f7c11 | 510 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i) |
3b6fdb5b LP |
511 | if (!other->meta.ignore_dependency_failure && |
512 | other->meta.job && | |
9e2f7c11 | 513 | !other->meta.job->override && |
dda5a135 LP |
514 | (other->meta.job->type == JOB_START || |
515 | other->meta.job->type == JOB_VERIFY_ACTIVE || | |
516 | other->meta.job->type == JOB_RELOAD_OR_START)) | |
5cb5a6ff LP |
517 | job_finish_and_invalidate(other->meta.job, false); |
518 | ||
519 | } else if (t == JOB_STOP) { | |
520 | ||
87f0e418 | 521 | SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i) |
3b6fdb5b LP |
522 | if (!other->meta.ignore_dependency_failure && |
523 | other->meta.job && | |
dda5a135 LP |
524 | (other->meta.job->type == JOB_START || |
525 | other->meta.job->type == JOB_VERIFY_ACTIVE || | |
526 | other->meta.job->type == JOB_RELOAD_OR_START)) | |
5cb5a6ff | 527 | job_finish_and_invalidate(other->meta.job, false); |
69dd2852 LP |
528 | |
529 | SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i) | |
530 | if (!other->meta.ignore_dependency_failure && | |
531 | other->meta.job && | |
532 | (other->meta.job->type == JOB_START || | |
533 | other->meta.job->type == JOB_VERIFY_ACTIVE || | |
534 | other->meta.job->type == JOB_RELOAD_OR_START)) | |
535 | job_finish_and_invalidate(other->meta.job, false); | |
5cb5a6ff LP |
536 | } |
537 | } | |
538 | ||
539 | /* Try to start the next jobs that can be started */ | |
87f0e418 | 540 | SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i) |
5cb5a6ff | 541 | if (other->meta.job) |
c1e1601e | 542 | job_add_to_run_queue(other->meta.job); |
87f0e418 | 543 | SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i) |
5cb5a6ff | 544 | if (other->meta.job) |
c1e1601e | 545 | job_add_to_run_queue(other->meta.job); |
5cb5a6ff | 546 | |
b0c918b9 LP |
547 | manager_check_finished(u->meta.manager); |
548 | ||
5cb5a6ff LP |
549 | return 0; |
550 | } | |
034c6ed7 | 551 | |
faf919f1 LP |
552 | int job_start_timer(Job *j) { |
553 | struct itimerspec its; | |
554 | struct epoll_event ev; | |
555 | int fd, r; | |
556 | assert(j); | |
557 | ||
558 | if (j->unit->meta.job_timeout <= 0 || | |
559 | j->timer_watch.type == WATCH_JOB_TIMER) | |
560 | return 0; | |
561 | ||
562 | assert(j->timer_watch.type == WATCH_INVALID); | |
563 | ||
564 | if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) { | |
565 | r = -errno; | |
566 | goto fail; | |
567 | } | |
568 | ||
569 | zero(its); | |
570 | timespec_store(&its.it_value, j->unit->meta.job_timeout); | |
571 | ||
572 | if (timerfd_settime(fd, 0, &its, NULL) < 0) { | |
573 | r = -errno; | |
574 | goto fail; | |
575 | } | |
576 | ||
577 | zero(ev); | |
578 | ev.data.ptr = &j->timer_watch; | |
579 | ev.events = EPOLLIN; | |
580 | ||
581 | if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { | |
582 | r = -errno; | |
583 | goto fail; | |
584 | } | |
585 | ||
586 | j->timer_watch.type = WATCH_JOB_TIMER; | |
587 | j->timer_watch.fd = fd; | |
588 | j->timer_watch.data.job = j; | |
589 | ||
590 | return 0; | |
591 | ||
592 | fail: | |
593 | if (fd >= 0) | |
594 | close_nointr_nofail(fd); | |
595 | ||
596 | return r; | |
597 | } | |
598 | ||
c1e1601e | 599 | void job_add_to_run_queue(Job *j) { |
034c6ed7 | 600 | assert(j); |
ac1135be | 601 | assert(j->installed); |
034c6ed7 LP |
602 | |
603 | if (j->in_run_queue) | |
604 | return; | |
605 | ||
606 | LIST_PREPEND(Job, run_queue, j->manager->run_queue, j); | |
607 | j->in_run_queue = true; | |
608 | } | |
94f04347 | 609 | |
c1e1601e LP |
610 | void job_add_to_dbus_queue(Job *j) { |
611 | assert(j); | |
612 | assert(j->installed); | |
613 | ||
614 | if (j->in_dbus_queue) | |
615 | return; | |
616 | ||
a567261a LP |
617 | /* We don't check if anybody is subscribed here, since this |
618 | * job might just have been created and not yet assigned to a | |
619 | * connection/client. */ | |
94b6dfa2 | 620 | |
c1e1601e LP |
621 | LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j); |
622 | j->in_dbus_queue = true; | |
623 | } | |
624 | ||
ea430986 LP |
625 | char *job_dbus_path(Job *j) { |
626 | char *p; | |
627 | ||
628 | assert(j); | |
629 | ||
630 | if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0) | |
631 | return NULL; | |
632 | ||
633 | return p; | |
634 | } | |
635 | ||
faf919f1 LP |
636 | void job_timer_event(Job *j, uint64_t n_elapsed, Watch *w) { |
637 | assert(j); | |
638 | assert(w == &j->timer_watch); | |
639 | ||
640 | log_warning("Job %s/%s timed out.", j->unit->meta.id, job_type_to_string(j->type)); | |
641 | job_finish_and_invalidate(j, false); | |
642 | } | |
643 | ||
94f04347 LP |
644 | static const char* const job_state_table[_JOB_STATE_MAX] = { |
645 | [JOB_WAITING] = "waiting", | |
646 | [JOB_RUNNING] = "running" | |
647 | }; | |
648 | ||
649 | DEFINE_STRING_TABLE_LOOKUP(job_state, JobState); | |
650 | ||
651 | static const char* const job_type_table[_JOB_TYPE_MAX] = { | |
652 | [JOB_START] = "start", | |
653 | [JOB_VERIFY_ACTIVE] = "verify-active", | |
654 | [JOB_STOP] = "stop", | |
655 | [JOB_RELOAD] = "reload", | |
656 | [JOB_RELOAD_OR_START] = "reload-or-start", | |
657 | [JOB_RESTART] = "restart", | |
658 | [JOB_TRY_RESTART] = "try-restart", | |
659 | }; | |
660 | ||
661 | DEFINE_STRING_TABLE_LOOKUP(job_type, JobType); | |
b548631a LP |
662 | |
663 | static const char* const job_mode_table[_JOB_MODE_MAX] = { | |
664 | [JOB_FAIL] = "fail", | |
c497c7a9 LP |
665 | [JOB_REPLACE] = "replace", |
666 | [JOB_ISOLATE] = "isolate" | |
b548631a LP |
667 | }; |
668 | ||
669 | DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode); |