]> git.ipfire.org Git - people/ms/systemd.git/blob - unit.c
license: add GPLv2+ license blurbs everwhere
[people/ms/systemd.git] / unit.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 #include <string.h>
25 #include <sys/epoll.h>
26 #include <sys/timerfd.h>
27 #include <sys/poll.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30
31 #include "set.h"
32 #include "unit.h"
33 #include "macro.h"
34 #include "strv.h"
35 #include "load-fragment.h"
36 #include "load-dropin.h"
37 #include "log.h"
38
39 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
40 [UNIT_SERVICE] = &service_vtable,
41 [UNIT_TIMER] = &timer_vtable,
42 [UNIT_SOCKET] = &socket_vtable,
43 [UNIT_TARGET] = &target_vtable,
44 [UNIT_DEVICE] = &device_vtable,
45 [UNIT_MOUNT] = &mount_vtable,
46 [UNIT_AUTOMOUNT] = &automount_vtable,
47 [UNIT_SNAPSHOT] = &snapshot_vtable
48 };
49
50 UnitType unit_name_to_type(const char *n) {
51 UnitType t;
52
53 assert(n);
54
55 for (t = 0; t < _UNIT_TYPE_MAX; t++)
56 if (endswith(n, unit_vtable[t]->suffix))
57 return t;
58
59 return _UNIT_TYPE_INVALID;
60 }
61
62 #define VALID_CHARS \
63 "0123456789" \
64 "abcdefghijklmnopqrstuvwxyz" \
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
66 "-_.\\"
67
68 bool unit_name_is_valid(const char *n) {
69 UnitType t;
70 const char *e, *i;
71
72 assert(n);
73
74 if (strlen(n) >= UNIT_NAME_MAX)
75 return false;
76
77 t = unit_name_to_type(n);
78 if (t < 0 || t >= _UNIT_TYPE_MAX)
79 return false;
80
81 if (!(e = strrchr(n, '.')))
82 return false;
83
84 if (e == n)
85 return false;
86
87 for (i = n; i < e; i++)
88 if (!strchr(VALID_CHARS, *i))
89 return false;
90
91 return true;
92 }
93
94 char *unit_name_change_suffix(const char *n, const char *suffix) {
95 char *e, *r;
96 size_t a, b;
97
98 assert(n);
99 assert(unit_name_is_valid(n));
100 assert(suffix);
101
102 assert_se(e = strrchr(n, '.'));
103 a = e - n;
104 b = strlen(suffix);
105
106 if (!(r = new(char, a + b + 1)))
107 return NULL;
108
109 memcpy(r, n, a);
110 memcpy(r+a, suffix, b+1);
111
112 return r;
113 }
114
115 Unit *unit_new(Manager *m) {
116 Unit *u;
117
118 assert(m);
119
120 if (!(u = new0(Unit, 1)))
121 return NULL;
122
123 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
124 free(u);
125 return NULL;
126 }
127
128 u->meta.manager = m;
129 u->meta.type = _UNIT_TYPE_INVALID;
130
131 return u;
132 }
133
134 int unit_add_name(Unit *u, const char *text) {
135 UnitType t;
136 char *s;
137 int r;
138
139 assert(u);
140 assert(text);
141
142 if (!unit_name_is_valid(text))
143 return -EINVAL;
144
145 if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
146 return -EINVAL;
147
148 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
149 return -EINVAL;
150
151 if (!(s = strdup(text)))
152 return -ENOMEM;
153
154 if ((r = set_put(u->meta.names, s)) < 0) {
155 free(s);
156
157 if (r == -EEXIST)
158 return 0;
159
160 return r;
161 }
162
163 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
164 set_remove(u->meta.names, s);
165 free(s);
166 return r;
167 }
168
169 if (u->meta.type == _UNIT_TYPE_INVALID)
170 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
171
172 u->meta.type = t;
173
174 if (!u->meta.id)
175 u->meta.id = s;
176
177 return 0;
178 }
179
180 int unit_choose_id(Unit *u, const char *name) {
181 char *s;
182
183 assert(u);
184 assert(name);
185
186 /* Selects one of the names of this unit as the id */
187
188 if (!(s = set_get(u->meta.names, (char*) name)))
189 return -ENOENT;
190
191 u->meta.id = s;
192 return 0;
193 }
194
195 int unit_set_description(Unit *u, const char *description) {
196 char *s;
197
198 assert(u);
199
200 if (!(s = strdup(description)))
201 return -ENOMEM;
202
203 free(u->meta.description);
204 u->meta.description = s;
205 return 0;
206 }
207
208 void unit_add_to_load_queue(Unit *u) {
209 assert(u);
210
211 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
212 return;
213
214 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
215 u->meta.in_load_queue = true;
216 }
217
218 static void bidi_set_free(Unit *u, Set *s) {
219 Iterator i;
220 Unit *other;
221
222 assert(u);
223
224 /* Frees the set and makes sure we are dropped from the
225 * inverse pointers */
226
227 SET_FOREACH(other, s, i) {
228 UnitDependency d;
229
230 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
231 set_remove(other->meta.dependencies[d], u);
232 }
233
234 set_free(s);
235 }
236
237 void unit_free(Unit *u) {
238 UnitDependency d;
239 Iterator i;
240 char *t;
241
242 assert(u);
243
244 /* Detach from next 'bigger' objects */
245
246 SET_FOREACH(t, u->meta.names, i)
247 hashmap_remove_value(u->meta.manager->units, t, u);
248
249 if (u->meta.type != _UNIT_TYPE_INVALID)
250 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
251
252 if (u->meta.in_load_queue)
253 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
254
255 if (u->meta.load_state == UNIT_LOADED)
256 if (UNIT_VTABLE(u)->done)
257 UNIT_VTABLE(u)->done(u);
258
259 /* Free data and next 'smaller' objects */
260 if (u->meta.job)
261 job_free(u->meta.job);
262
263 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
264 bidi_set_free(u, u->meta.dependencies[d]);
265
266 free(u->meta.description);
267 free(u->meta.load_path);
268
269 while ((t = set_steal_first(u->meta.names)))
270 free(t);
271 set_free(u->meta.names);
272
273 free(u);
274 }
275
276 UnitActiveState unit_active_state(Unit *u) {
277 assert(u);
278
279 if (u->meta.load_state != UNIT_LOADED)
280 return UNIT_INACTIVE;
281
282 return UNIT_VTABLE(u)->active_state(u);
283 }
284
285 static int ensure_merge(Set **s, Set *other) {
286
287 if (!other)
288 return 0;
289
290 if (*s)
291 return set_merge(*s, other);
292
293 if (!(*s = set_copy(other)))
294 return -ENOMEM;
295
296 return 0;
297 }
298
299 /* FIXME: Does not rollback on failure! Needs to fix special unit
300 * pointers. Needs to merge names and dependencies properly.*/
301 int unit_merge(Unit *u, Unit *other) {
302 int r;
303 UnitDependency d;
304
305 assert(u);
306 assert(other);
307 assert(u->meta.manager == other->meta.manager);
308
309 /* This merges 'other' into 'unit'. FIXME: This does not
310 * rollback on failure. */
311
312 if (u->meta.type != u->meta.type)
313 return -EINVAL;
314
315 if (u->meta.load_state != UNIT_STUB)
316 return -EINVAL;
317
318 /* Merge names */
319 if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0)
320 return r;
321
322 /* Merge dependencies */
323 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
324 /* fixme, the inverse mapping is missing */
325 if ((r = ensure_merge(&u->meta.dependencies[d], other->meta.dependencies[d])) < 0)
326 return r;
327
328 return 0;
329 }
330
331 const char* unit_id(Unit *u) {
332 assert(u);
333
334 if (u->meta.id)
335 return u->meta.id;
336
337 return set_first(u->meta.names);
338 }
339
340 const char *unit_description(Unit *u) {
341 assert(u);
342
343 if (u->meta.description)
344 return u->meta.description;
345
346 return unit_id(u);
347 }
348
349 void unit_dump(Unit *u, FILE *f, const char *prefix) {
350
351 char *t;
352 UnitDependency d;
353 Iterator i;
354 char *prefix2;
355
356 assert(u);
357
358 if (!prefix)
359 prefix = "";
360 prefix2 = strappend(prefix, "\t");
361 if (!prefix2)
362 prefix2 = "";
363
364 fprintf(f,
365 "%s→ Unit %s:\n"
366 "%s\tDescription: %s\n"
367 "%s\tUnit Load State: %s\n"
368 "%s\tUnit Active State: %s\n"
369 "%s\tRecursive Stop: %s\n"
370 "%s\tStop When Unneeded: %s\n",
371 prefix, unit_id(u),
372 prefix, unit_description(u),
373 prefix, unit_load_state_to_string(u->meta.load_state),
374 prefix, unit_active_state_to_string(unit_active_state(u)),
375 prefix, yes_no(u->meta.recursive_stop),
376 prefix, yes_no(u->meta.stop_when_unneeded));
377
378 if (u->meta.load_path)
379 fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path);
380
381 SET_FOREACH(t, u->meta.names, i)
382 fprintf(f, "%s\tName: %s\n", prefix, t);
383
384 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
385 Unit *other;
386
387 if (set_isempty(u->meta.dependencies[d]))
388 continue;
389
390 SET_FOREACH(other, u->meta.dependencies[d], i)
391 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
392 }
393
394 if (UNIT_VTABLE(u)->dump)
395 UNIT_VTABLE(u)->dump(u, f, prefix2);
396
397 if (u->meta.job)
398 job_dump(u->meta.job, f, prefix2);
399
400 free(prefix2);
401 }
402
403 /* Common implementation for multiple backends */
404 int unit_load_fragment_and_dropin(Unit *u) {
405 int r, ret;
406
407 assert(u);
408
409 /* Load a .socket file */
410 if ((r = unit_load_fragment(u)) < 0)
411 return r;
412
413 ret = r > 0;
414
415 /* Load drop-in directory data */
416 if ((r = unit_load_dropin(u)) < 0)
417 return r;
418
419 return ret;
420 }
421
422 int unit_load(Unit *u) {
423 int r;
424
425 assert(u);
426
427 if (u->meta.in_load_queue) {
428 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
429 u->meta.in_load_queue = false;
430 }
431
432 if (u->meta.load_state != UNIT_STUB)
433 return 0;
434
435 if (UNIT_VTABLE(u)->init)
436 if ((r = UNIT_VTABLE(u)->init(u)) < 0)
437 goto fail;
438
439 u->meta.load_state = UNIT_LOADED;
440 return 0;
441
442 fail:
443 u->meta.load_state = UNIT_FAILED;
444 return r;
445 }
446
447 /* Errors:
448 * -EBADR: This unit type does not support starting.
449 * -EALREADY: Unit is already started.
450 * -EAGAIN: An operation is already in progress. Retry later.
451 */
452 int unit_start(Unit *u) {
453 UnitActiveState state;
454
455 assert(u);
456
457 if (!UNIT_VTABLE(u)->start)
458 return -EBADR;
459
460 state = unit_active_state(u);
461 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
462 return -EALREADY;
463
464 /* We don't suppress calls to ->start() here when we are
465 * already starting, to allow this request to be used as a
466 * "hurry up" call, for example when the unit is in some "auto
467 * restart" state where it waits for a holdoff timer to elapse
468 * before it will start again. */
469
470 return UNIT_VTABLE(u)->start(u);
471 }
472
473 bool unit_can_start(Unit *u) {
474 assert(u);
475
476 return !!UNIT_VTABLE(u)->start;
477 }
478
479 /* Errors:
480 * -EBADR: This unit type does not support stopping.
481 * -EALREADY: Unit is already stopped.
482 * -EAGAIN: An operation is already in progress. Retry later.
483 */
484 int unit_stop(Unit *u) {
485 UnitActiveState state;
486
487 assert(u);
488
489 if (!UNIT_VTABLE(u)->stop)
490 return -EBADR;
491
492 state = unit_active_state(u);
493 if (state == UNIT_INACTIVE)
494 return -EALREADY;
495
496 if (state == UNIT_DEACTIVATING)
497 return 0;
498
499 return UNIT_VTABLE(u)->stop(u);
500 }
501
502 /* Errors:
503 * -EBADR: This unit type does not support reloading.
504 * -ENOEXEC: Unit is not started.
505 * -EAGAIN: An operation is already in progress. Retry later.
506 */
507 int unit_reload(Unit *u) {
508 UnitActiveState state;
509
510 assert(u);
511
512 if (!unit_can_reload(u))
513 return -EBADR;
514
515 state = unit_active_state(u);
516 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
517 return -EALREADY;
518
519 if (unit_active_state(u) != UNIT_ACTIVE)
520 return -ENOEXEC;
521
522 return UNIT_VTABLE(u)->reload(u);
523 }
524
525 bool unit_can_reload(Unit *u) {
526 assert(u);
527
528 if (!UNIT_VTABLE(u)->reload)
529 return false;
530
531 if (!UNIT_VTABLE(u)->can_reload)
532 return true;
533
534 return UNIT_VTABLE(u)->can_reload(u);
535 }
536
537 static void unit_check_uneeded(Unit *u) {
538 Iterator i;
539 Unit *other;
540
541 assert(u);
542
543 /* If this service shall be shut down when unneeded then do
544 * so. */
545
546 if (!u->meta.stop_when_unneeded)
547 return;
548
549 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
550 return;
551
552 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
553 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
554 return;
555
556 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
557 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
558 return;
559
560 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
561 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
562 return;
563
564 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
565
566 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
567 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
568 }
569
570 static void retroactively_start_dependencies(Unit *u) {
571 Iterator i;
572 Unit *other;
573
574 assert(u);
575 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
576
577 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
578 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
579 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
580
581 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
582 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
583 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
584
585 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
586 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
587 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
588
589 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
590 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
591 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
592
593 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
594 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
595 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
596 }
597
598 static void retroactively_stop_dependencies(Unit *u) {
599 Iterator i;
600 Unit *other;
601
602 assert(u);
603 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
604
605 if (u->meta.recursive_stop) {
606 /* Pull down units need us recursively if enabled */
607 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
608 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
609 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
610 }
611
612 /* Garbage collect services that might not be needed anymore, if enabled */
613 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
614 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
615 unit_check_uneeded(other);
616 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
617 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
618 unit_check_uneeded(other);
619 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
620 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
621 unit_check_uneeded(other);
622 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
623 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
624 unit_check_uneeded(other);
625 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
626 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
627 unit_check_uneeded(other);
628 }
629
630 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
631 assert(u);
632 assert(os < _UNIT_ACTIVE_STATE_MAX);
633 assert(ns < _UNIT_ACTIVE_STATE_MAX);
634 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
635 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
636
637 if (os == ns)
638 return;
639
640 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
641 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
642 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
643 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
644
645 if (u->meta.job) {
646
647 if (u->meta.job->state == JOB_WAITING)
648
649 /* So we reached a different state for this
650 * job. Let's see if we can run it now if it
651 * failed previously due to EAGAIN. */
652 job_schedule_run(u->meta.job);
653
654 else {
655 assert(u->meta.job->state == JOB_RUNNING);
656
657 /* Let's check whether this state change
658 * constitutes a finished job, or maybe
659 * cotradicts a running job and hence needs to
660 * invalidate jobs. */
661
662 switch (u->meta.job->type) {
663
664 case JOB_START:
665 case JOB_VERIFY_ACTIVE:
666
667 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
668 job_finish_and_invalidate(u->meta.job, true);
669 return;
670 } else if (ns == UNIT_ACTIVATING)
671 return;
672 else
673 job_finish_and_invalidate(u->meta.job, false);
674
675 break;
676
677 case JOB_RELOAD:
678 case JOB_RELOAD_OR_START:
679
680 if (ns == UNIT_ACTIVE) {
681 job_finish_and_invalidate(u->meta.job, true);
682 return;
683 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
684 return;
685 else
686 job_finish_and_invalidate(u->meta.job, false);
687
688 break;
689
690 case JOB_STOP:
691 case JOB_RESTART:
692 case JOB_TRY_RESTART:
693
694 if (ns == UNIT_INACTIVE) {
695 job_finish_and_invalidate(u->meta.job, true);
696 return;
697 } else if (ns == UNIT_DEACTIVATING)
698 return;
699 else
700 job_finish_and_invalidate(u->meta.job, false);
701
702 break;
703
704 default:
705 assert_not_reached("Job type unknown");
706 }
707 }
708 }
709
710 /* If this state change happened without being requested by a
711 * job, then let's retroactively start or stop dependencies */
712
713 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
714 retroactively_start_dependencies(u);
715 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
716 retroactively_stop_dependencies(u);
717
718 /* Maybe we finished startup and are now ready for being
719 * stopped because unneeded? */
720 unit_check_uneeded(u);
721 }
722
723 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
724 struct epoll_event ev;
725
726 assert(u);
727 assert(fd >= 0);
728 assert(w);
729 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
730
731 zero(ev);
732 ev.data.ptr = w;
733 ev.events = events;
734
735 if (epoll_ctl(u->meta.manager->epoll_fd,
736 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
737 fd,
738 &ev) < 0)
739 return -errno;
740
741 w->fd = fd;
742 w->type = WATCH_FD;
743 w->data.unit = u;
744
745 return 0;
746 }
747
748 void unit_unwatch_fd(Unit *u, Watch *w) {
749 assert(u);
750 assert(w);
751
752 if (w->type == WATCH_INVALID)
753 return;
754
755 assert(w->type == WATCH_FD);
756 assert(w->data.unit == u);
757 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
758
759 w->fd = -1;
760 w->type = WATCH_INVALID;
761 w->data.unit = NULL;
762 }
763
764 int unit_watch_pid(Unit *u, pid_t pid) {
765 assert(u);
766 assert(pid >= 1);
767
768 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
769 }
770
771 void unit_unwatch_pid(Unit *u, pid_t pid) {
772 assert(u);
773 assert(pid >= 1);
774
775 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
776 }
777
778 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
779 struct itimerspec its;
780 int flags, fd;
781 bool ours;
782
783 assert(u);
784 assert(w);
785 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
786
787 /* This will try to reuse the old timer if there is one */
788
789 if (w->type == WATCH_TIMER) {
790 ours = false;
791 fd = w->fd;
792 } else {
793 ours = true;
794 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
795 return -errno;
796 }
797
798 zero(its);
799
800 if (delay <= 0) {
801 /* Set absolute time in the past, but not 0, since we
802 * don't want to disarm the timer */
803 its.it_value.tv_sec = 0;
804 its.it_value.tv_nsec = 1;
805
806 flags = TFD_TIMER_ABSTIME;
807 } else {
808 timespec_store(&its.it_value, delay);
809 flags = 0;
810 }
811
812 /* This will also flush the elapse counter */
813 if (timerfd_settime(fd, flags, &its, NULL) < 0)
814 goto fail;
815
816 if (w->type == WATCH_INVALID) {
817 struct epoll_event ev;
818
819 zero(ev);
820 ev.data.ptr = w;
821 ev.events = EPOLLIN;
822
823 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
824 goto fail;
825 }
826
827 w->fd = fd;
828 w->type = WATCH_TIMER;
829 w->data.unit = u;
830
831 return 0;
832
833 fail:
834 if (ours)
835 close_nointr_nofail(fd);
836
837 return -errno;
838 }
839
840 void unit_unwatch_timer(Unit *u, Watch *w) {
841 assert(u);
842 assert(w);
843
844 if (w->type == WATCH_INVALID)
845 return;
846
847 assert(w->type == WATCH_TIMER && w->data.unit == u);
848
849 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
850 assert_se(close_nointr(w->fd) == 0);
851
852 w->fd = -1;
853 w->type = WATCH_INVALID;
854 w->data.unit = NULL;
855 }
856
857 bool unit_job_is_applicable(Unit *u, JobType j) {
858 assert(u);
859 assert(j >= 0 && j < _JOB_TYPE_MAX);
860
861 switch (j) {
862
863 case JOB_VERIFY_ACTIVE:
864 case JOB_START:
865 return true;
866
867 case JOB_STOP:
868 case JOB_RESTART:
869 case JOB_TRY_RESTART:
870 return unit_can_start(u);
871
872 case JOB_RELOAD:
873 return unit_can_reload(u);
874
875 case JOB_RELOAD_OR_START:
876 return unit_can_reload(u) && unit_can_start(u);
877
878 default:
879 assert_not_reached("Invalid job type");
880 }
881 }
882
883 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
884
885 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
886 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
887 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
888 [UNIT_WANTS] = UNIT_WANTED_BY,
889 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
890 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
891 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
892 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
893 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
894 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
895 [UNIT_BEFORE] = UNIT_AFTER,
896 [UNIT_AFTER] = UNIT_BEFORE
897 };
898 int r;
899
900 assert(u);
901 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
902 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
903 assert(other);
904
905 /* We won't allow dependencies on ourselves. We will not
906 * consider them an error however. */
907 if (u == other)
908 return 0;
909
910 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
911 return r;
912
913 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
914 return r;
915
916 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
917 return r;
918
919 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
920 set_remove(u->meta.dependencies[d], other);
921 return r;
922 }
923
924 return 0;
925 }
926
927 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
928 Unit *other;
929 int r;
930
931 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
932 return r;
933
934 if ((r = unit_add_dependency(u, d, other)) < 0)
935 return r;
936
937 return 0;
938 }
939
940 const char *unit_path(void) {
941 char *e;
942
943 if ((e = getenv("UNIT_PATH")))
944 if (path_is_absolute(e))
945 return e;
946
947 return UNIT_PATH;
948 }
949
950 int set_unit_path(const char *p) {
951 char *cwd, *c;
952 int r;
953
954 /* This is mostly for debug purposes */
955
956 if (path_is_absolute(p)) {
957 if (!(c = strdup(p)))
958 return -ENOMEM;
959 } else {
960 if (!(cwd = get_current_dir_name()))
961 return -errno;
962
963 r = asprintf(&c, "%s/%s", cwd, p);
964 free(cwd);
965
966 if (r < 0)
967 return -ENOMEM;
968 }
969
970 if (setenv("UNIT_PATH", c, 0) < 0) {
971 r = -errno;
972 free(c);
973 return r;
974 }
975
976 return 0;
977 }
978
979 char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) {
980 char *r, *t;
981 const char *f;
982 size_t a, b, c;
983
984 assert(path);
985
986 /* Takes a path and a suffix and prefix and makes a nice
987 * string suitable as unit name of it, escaping all weird
988 * chars on the way.
989 *
990 * / becomes ., and all chars not alloweed in a unit name get
991 * escaped as \xFF, including \ and ., of course. This
992 * escaping is hence reversible.
993 */
994
995 if (!prefix)
996 prefix = "";
997
998 if (!suffix)
999 suffix = "";
1000
1001 a = strlen(prefix);
1002 b = strlen(path);
1003 c = strlen(suffix);
1004
1005 if (!(r = new(char, a+b*4+c+1)))
1006 return NULL;
1007
1008 memcpy(r, prefix, a);
1009
1010 for (f = path, t = r+a; *f; f++) {
1011 if (*f == '/')
1012 *(t++) = '.';
1013 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1014 *(t++) = '\\';
1015 *(t++) = 'x';
1016 *(t++) = hexchar(*f > 4);
1017 *(t++) = hexchar(*f);
1018 } else
1019 *(t++) = *f;
1020 }
1021
1022 memcpy(t, suffix, c+1);
1023
1024 return r;
1025 }
1026
1027 char *unit_dbus_path(Unit *u) {
1028 char *p, *e;
1029
1030 assert(u);
1031
1032 if (!(e = bus_path_escape(unit_id(u))))
1033 return NULL;
1034
1035 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1036 free(e);
1037 return NULL;
1038 }
1039
1040 free(e);
1041 return p;
1042 }
1043
1044 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1045 [UNIT_SERVICE] = "service",
1046 [UNIT_TIMER] = "timer",
1047 [UNIT_SOCKET] = "socket",
1048 [UNIT_TARGET] = "target",
1049 [UNIT_DEVICE] = "device",
1050 [UNIT_MOUNT] = "mount",
1051 [UNIT_AUTOMOUNT] = "automount",
1052 [UNIT_SNAPSHOT] = "snapshot"
1053 };
1054
1055 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1056
1057 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1058 [UNIT_STUB] = "stub",
1059 [UNIT_LOADED] = "loaded",
1060 [UNIT_FAILED] = "failed"
1061 };
1062
1063 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1064
1065 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1066 [UNIT_ACTIVE] = "active",
1067 [UNIT_INACTIVE] = "inactive",
1068 [UNIT_ACTIVATING] = "activating",
1069 [UNIT_DEACTIVATING] = "deactivating"
1070 };
1071
1072 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1073
1074 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1075 [UNIT_REQUIRES] = "Requires",
1076 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1077 [UNIT_WANTS] = "Wants",
1078 [UNIT_REQUISITE] = "Requisite",
1079 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1080 [UNIT_REQUIRED_BY] = "RequiredBy",
1081 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1082 [UNIT_WANTED_BY] = "WantedBy",
1083 [UNIT_CONFLICTS] = "Conflicts",
1084 [UNIT_BEFORE] = "Before",
1085 [UNIT_AFTER] = "After",
1086 };
1087
1088 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);