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