chiark / gitweb /
systemctl: use format_timespan() where applicable
[elogind.git] / src / job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
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
22 #include <assert.h>
23 #include <errno.h>
24
25 #include "set.h"
26 #include "unit.h"
27 #include "macro.h"
28 #include "strv.h"
29 #include "load-fragment.h"
30 #include "load-dropin.h"
31 #include "log.h"
32 #include "dbus-job.h"
33
34 Job* job_new(Manager *m, JobType type, Unit *unit) {
35         Job *j;
36
37         assert(m);
38         assert(type < _JOB_TYPE_MAX);
39         assert(unit);
40
41         if (!(j = new0(Job, 1)))
42                 return NULL;
43
44         j->manager = m;
45         j->id = m->current_job_id++;
46         j->type = type;
47         j->unit = unit;
48
49         /* We don't link it here, that's what job_dependency() is for */
50
51         return j;
52 }
53
54 void job_free(Job *j) {
55         assert(j);
56
57         /* Detach from next 'bigger' objects */
58         if (j->installed) {
59                 bus_job_send_removed_signal(j, !j->failed);
60
61                 if (j->unit->meta.job == j) {
62                         j->unit->meta.job = NULL;
63                         unit_add_to_gc_queue(j->unit);
64                 }
65
66                 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
67                 j->installed = false;
68         }
69
70         /* Detach from next 'smaller' objects */
71         manager_transaction_unlink_job(j->manager, j, true);
72
73         if (j->in_run_queue)
74                 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
75
76         if (j->in_dbus_queue)
77                 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
78
79         free(j);
80 }
81
82 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) {
83         JobDependency *l;
84
85         assert(object);
86
87         /* Adds a new job link, which encodes that the 'subject' job
88          * needs the 'object' job in some way. If 'subject' is NULL
89          * this means the 'anchor' job (i.e. the one the user
90          * explcitily asked for) is the requester. */
91
92         if (!(l = new0(JobDependency, 1)))
93                 return NULL;
94
95         l->subject = subject;
96         l->object = object;
97         l->matters = matters;
98
99         if (subject)
100                 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
101         else
102                 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
103
104         LIST_PREPEND(JobDependency, object, object->object_list, l);
105
106         return l;
107 }
108
109 void job_dependency_free(JobDependency *l) {
110         assert(l);
111
112         if (l->subject)
113                 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
114         else
115                 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
116
117         LIST_REMOVE(JobDependency, object, l->object->object_list, l);
118
119         free(l);
120 }
121
122 void job_dependency_delete(Job *subject, Job *object, bool *matters) {
123         JobDependency *l;
124
125         assert(object);
126
127         LIST_FOREACH(object, l, object->object_list) {
128                 assert(l->object == object);
129
130                 if (l->subject == subject)
131                         break;
132         }
133
134         if (!l) {
135                 if (matters)
136                         *matters = false;
137                 return;
138         }
139
140         if (matters)
141                 *matters = l->matters;
142
143         job_dependency_free(l);
144 }
145
146 void job_dump(Job *j, FILE*f, const char *prefix) {
147         assert(j);
148         assert(f);
149
150         if (!prefix)
151                 prefix = "";
152
153         fprintf(f,
154                 "%s-> Job %u:\n"
155                 "%s\tAction: %s -> %s\n"
156                 "%s\tState: %s\n"
157                 "%s\tForced: %s\n",
158                 prefix, j->id,
159                 prefix, j->unit->meta.id, job_type_to_string(j->type),
160                 prefix, job_state_to_string(j->state),
161                 prefix, yes_no(j->override));
162 }
163
164 bool job_is_anchor(Job *j) {
165         JobDependency *l;
166
167         assert(j);
168
169         LIST_FOREACH(object, l, j->object_list)
170                 if (!l->subject)
171                         return true;
172
173         return false;
174 }
175
176 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
177         return
178                 (a == c && b == d) ||
179                 (a == d && b == c);
180 }
181
182 int job_type_merge(JobType *a, JobType b) {
183         if (*a == b)
184                 return 0;
185
186         /* Merging is associative! a merged with b merged with c is
187          * the same as a merged with c merged with b. */
188
189         /* Mergeability is transitive! if a can be merged with b and b
190          * with c then a also with c */
191
192         /* Also, if a merged with b cannot be merged with c, then
193          * either a or b cannot be merged with c either */
194
195         if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
196                 *a = JOB_START;
197         else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
198                  types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
199                  types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
200                  types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
201                 *a = JOB_RELOAD_OR_START;
202         else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
203                  types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
204                  types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
205                  types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
206                  types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
207                  types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
208                  types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
209                 *a = JOB_RESTART;
210         else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
211                 *a = JOB_RELOAD;
212         else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
213                  types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
214                 *a = JOB_TRY_RESTART;
215         else
216                 return -EEXIST;
217
218         return 0;
219 }
220
221 bool job_type_is_mergeable(JobType a, JobType b) {
222         return job_type_merge(&a, b) >= 0;
223 }
224
225 bool job_type_is_superset(JobType a, JobType b) {
226
227         /* Checks whether operation a is a "superset" of b in its
228          * actions */
229
230         if (a == b)
231                 return true;
232
233         switch (a) {
234                 case JOB_START:
235                         return b == JOB_VERIFY_ACTIVE;
236
237                 case JOB_RELOAD:
238                         return
239                                 b == JOB_VERIFY_ACTIVE;
240
241                 case JOB_RELOAD_OR_START:
242                         return
243                                 b == JOB_RELOAD ||
244                                 b == JOB_START ||
245                                 b == JOB_VERIFY_ACTIVE;
246
247                 case JOB_RESTART:
248                         return
249                                 b == JOB_START ||
250                                 b == JOB_VERIFY_ACTIVE ||
251                                 b == JOB_RELOAD ||
252                                 b == JOB_RELOAD_OR_START ||
253                                 b == JOB_TRY_RESTART;
254
255                 case JOB_TRY_RESTART:
256                         return
257                                 b == JOB_VERIFY_ACTIVE ||
258                                 b == JOB_RELOAD;
259                 default:
260                         return false;
261
262         }
263 }
264
265 bool job_type_is_conflicting(JobType a, JobType b) {
266         assert(a >= 0 && a < _JOB_TYPE_MAX);
267         assert(b >= 0 && b < _JOB_TYPE_MAX);
268
269         return (a == JOB_STOP) != (b == JOB_STOP);
270 }
271
272 bool job_type_is_redundant(JobType a, UnitActiveState b) {
273         switch (a) {
274
275         case JOB_START:
276                 return
277                         b == UNIT_ACTIVE ||
278                         b == UNIT_RELOADING;
279
280         case JOB_STOP:
281                 return
282                         b == UNIT_INACTIVE ||
283                         b == UNIT_MAINTENANCE;
284
285         case JOB_VERIFY_ACTIVE:
286                 return
287                         b == UNIT_ACTIVE ||
288                         b == UNIT_RELOADING;
289
290         case JOB_RELOAD:
291                 return
292                         b == UNIT_RELOADING;
293
294         case JOB_RELOAD_OR_START:
295                 return
296                         b == UNIT_ACTIVATING ||
297                         b == UNIT_RELOADING;
298
299         case JOB_RESTART:
300                 return
301                         b == UNIT_ACTIVATING;
302
303         case JOB_TRY_RESTART:
304                 return
305                         b == UNIT_ACTIVATING;
306
307         default:
308                 assert_not_reached("Invalid job type");
309         }
310 }
311
312 bool job_is_runnable(Job *j) {
313         Iterator i;
314         Unit *other;
315
316         assert(j);
317         assert(j->installed);
318
319         /* Checks whether there is any job running for the units this
320          * job needs to be running after (in the case of a 'positive'
321          * job type) or before (in the case of a 'negative' job type
322          * . */
323
324         if (j->type == JOB_START ||
325             j->type == JOB_VERIFY_ACTIVE ||
326             j->type == JOB_RELOAD ||
327             j->type == JOB_RELOAD_OR_START) {
328
329                 /* Immediate result is that the job is or might be
330                  * started. In this case lets wait for the
331                  * dependencies, regardless whether they are
332                  * starting or stopping something. */
333
334                 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
335                         if (other->meta.job)
336                                 return false;
337         }
338
339         /* Also, if something else is being stopped and we should
340          * change state after it, then lets wait. */
341
342         SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
343                 if (other->meta.job &&
344                     (other->meta.job->type == JOB_STOP ||
345                      other->meta.job->type == JOB_RESTART ||
346                      other->meta.job->type == JOB_TRY_RESTART))
347                         return false;
348
349         /* This means that for a service a and a service b where b
350          * shall be started after a:
351          *
352          *  start a + start b â†’ 1st step start a, 2nd step start b
353          *  start a + stop b  â†’ 1st step stop b,  2nd step start a
354          *  stop a  + start b â†’ 1st step stop a,  2nd step start b
355          *  stop a  + stop b  â†’ 1st step stop b,  2nd step stop a
356          *
357          *  This has the side effect that restarts are properly
358          *  synchronized too. */
359
360         return true;
361 }
362
363 int job_run_and_invalidate(Job *j) {
364         int r;
365
366         assert(j);
367         assert(j->installed);
368
369         if (j->in_run_queue) {
370                 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
371                 j->in_run_queue = false;
372         }
373
374         if (j->state != JOB_WAITING)
375                 return 0;
376
377         if (!job_is_runnable(j))
378                 return -EAGAIN;
379
380         j->state = JOB_RUNNING;
381         job_add_to_dbus_queue(j);
382
383         switch (j->type) {
384
385                 case JOB_START:
386                         r = unit_start(j->unit);
387                         if (r == -EBADR)
388                                 r = 0;
389                         break;
390
391                 case JOB_VERIFY_ACTIVE: {
392                         UnitActiveState t = unit_active_state(j->unit);
393                         if (UNIT_IS_ACTIVE_OR_RELOADING(t))
394                                 r = -EALREADY;
395                         else if (t == UNIT_ACTIVATING)
396                                 r = -EAGAIN;
397                         else
398                                 r = -ENOEXEC;
399                         break;
400                 }
401
402                 case JOB_STOP:
403                         r = unit_stop(j->unit);
404                         break;
405
406                 case JOB_RELOAD:
407                         r = unit_reload(j->unit);
408                         break;
409
410                 case JOB_RELOAD_OR_START:
411                         if (unit_active_state(j->unit) == UNIT_ACTIVE)
412                                 r = unit_reload(j->unit);
413                         else
414                                 r = unit_start(j->unit);
415                         break;
416
417                 case JOB_RESTART: {
418                         UnitActiveState t = unit_active_state(j->unit);
419                         if (t == UNIT_INACTIVE || t == UNIT_MAINTENANCE || t == UNIT_ACTIVATING) {
420                                 j->type = JOB_START;
421                                 r = unit_start(j->unit);
422                         } else
423                                 r = unit_stop(j->unit);
424                         break;
425                 }
426
427                 case JOB_TRY_RESTART: {
428                         UnitActiveState t = unit_active_state(j->unit);
429                         if (t == UNIT_INACTIVE || t == UNIT_MAINTENANCE || t == UNIT_DEACTIVATING)
430                                 r = -ENOEXEC;
431                         else if (t == UNIT_ACTIVATING) {
432                                 j->type = JOB_START;
433                                 r = unit_start(j->unit);
434                         } else
435                                 r = unit_stop(j->unit);
436                         break;
437                 }
438
439                 default:
440                         assert_not_reached("Unknown job type");
441         }
442
443         if (r == -EALREADY)
444                 r = job_finish_and_invalidate(j, true);
445         else if (r == -EAGAIN) {
446                 j->state = JOB_WAITING;
447                 return -EAGAIN;
448         } else if (r < 0)
449                 r = job_finish_and_invalidate(j, false);
450
451         return r;
452 }
453
454 int job_finish_and_invalidate(Job *j, bool success) {
455         Unit *u;
456         Unit *other;
457         JobType t;
458         Iterator i;
459
460         assert(j);
461         assert(j->installed);
462
463         log_debug("Job %s/%s finished, success=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(success));
464         job_add_to_dbus_queue(j);
465
466         /* Patch restart jobs so that they become normal start jobs */
467         if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
468
469                 log_debug("Converting job %s/%s -> %s/%s",
470                           j->unit->meta.id, job_type_to_string(j->type),
471                           j->unit->meta.id, job_type_to_string(JOB_START));
472
473                 j->state = JOB_RUNNING;
474                 j->type = JOB_START;
475
476                 job_add_to_run_queue(j);
477                 return 0;
478         }
479
480         j->failed = !success;
481         u = j->unit;
482         t = j->type;
483         job_free(j);
484
485         /* Fail depending jobs on failure */
486         if (!success) {
487
488                 if (t == JOB_START ||
489                     t == JOB_VERIFY_ACTIVE ||
490                     t == JOB_RELOAD_OR_START) {
491
492                         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
493                                 if (other->meta.job &&
494                                     (other->meta.job->type == JOB_START ||
495                                      other->meta.job->type == JOB_VERIFY_ACTIVE ||
496                                      other->meta.job->type == JOB_RELOAD_OR_START))
497                                         job_finish_and_invalidate(other->meta.job, false);
498
499                         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
500                                 if (other->meta.job &&
501                                     !other->meta.job->override &&
502                                     (other->meta.job->type == JOB_START ||
503                                      other->meta.job->type == JOB_VERIFY_ACTIVE ||
504                                      other->meta.job->type == JOB_RELOAD_OR_START))
505                                         job_finish_and_invalidate(other->meta.job, false);
506
507                 } else if (t == JOB_STOP) {
508
509                         SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
510                                 if (other->meta.job &&
511                                     (other->meta.job->type == JOB_START ||
512                                      other->meta.job->type == JOB_VERIFY_ACTIVE ||
513                                      other->meta.job->type == JOB_RELOAD_OR_START))
514                                         job_finish_and_invalidate(other->meta.job, false);
515                 }
516         }
517
518         /* Try to start the next jobs that can be started */
519         SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
520                 if (other->meta.job)
521                         job_add_to_run_queue(other->meta.job);
522         SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
523                 if (other->meta.job)
524                         job_add_to_run_queue(other->meta.job);
525
526         return 0;
527 }
528
529 void job_add_to_run_queue(Job *j) {
530         assert(j);
531         assert(j->installed);
532
533         if (j->in_run_queue)
534                 return;
535
536         LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
537         j->in_run_queue = true;
538 }
539
540 void job_add_to_dbus_queue(Job *j) {
541         assert(j);
542         assert(j->installed);
543
544         if (j->in_dbus_queue)
545                 return;
546
547         if (set_isempty(j->manager->subscribed)) {
548                 j->sent_dbus_new_signal = true;
549                 return;
550         }
551
552         LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
553         j->in_dbus_queue = true;
554 }
555
556 char *job_dbus_path(Job *j) {
557         char *p;
558
559         assert(j);
560
561         if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
562                 return NULL;
563
564         return p;
565 }
566
567 static const char* const job_state_table[_JOB_STATE_MAX] = {
568         [JOB_WAITING] = "waiting",
569         [JOB_RUNNING] = "running"
570 };
571
572 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
573
574 static const char* const job_type_table[_JOB_TYPE_MAX] = {
575         [JOB_START] = "start",
576         [JOB_VERIFY_ACTIVE] = "verify-active",
577         [JOB_STOP] = "stop",
578         [JOB_RELOAD] = "reload",
579         [JOB_RELOAD_OR_START] = "reload-or-start",
580         [JOB_RESTART] = "restart",
581         [JOB_TRY_RESTART] = "try-restart",
582 };
583
584 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
585
586 static const char* const job_mode_table[_JOB_MODE_MAX] = {
587         [JOB_FAIL] = "fail",
588         [JOB_REPLACE] = "replace",
589         [JOB_ISOLATE] = "isolate"
590 };
591
592 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);