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