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