unit.c 26.2 KB
Newer Older
Lennart Poettering's avatar
Lennart Poettering committed
1
2
3
4
5
6
7
8
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <sys/poll.h>
9
10
#include <stdlib.h>
#include <unistd.h>
Lennart Poettering's avatar
Lennart Poettering committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include "set.h"
#include "unit.h"
#include "macro.h"
#include "strv.h"
#include "load-fragment.h"
#include "load-dropin.h"
#include "log.h"

const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
        [UNIT_SERVICE] = &service_vtable,
        [UNIT_TIMER] = &timer_vtable,
        [UNIT_SOCKET] = &socket_vtable,
        [UNIT_TARGET] = &target_vtable,
        [UNIT_DEVICE] = &device_vtable,
        [UNIT_MOUNT] = &mount_vtable,
        [UNIT_AUTOMOUNT] = &automount_vtable,
        [UNIT_SNAPSHOT] = &snapshot_vtable
};

UnitType unit_name_to_type(const char *n) {
        UnitType t;

        assert(n);

        for (t = 0; t < _UNIT_TYPE_MAX; t++)
                if (endswith(n, unit_vtable[t]->suffix))
                        return t;

        return _UNIT_TYPE_INVALID;
}

#define VALID_CHARS                             \
        "0123456789"                            \
        "abcdefghijklmnopqrstuvwxyz"            \
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"            \
47
        "-_.\\"
Lennart Poettering's avatar
Lennart Poettering committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

bool unit_name_is_valid(const char *n) {
        UnitType t;
        const char *e, *i;

        assert(n);

        if (strlen(n) >= UNIT_NAME_MAX)
                return false;

        t = unit_name_to_type(n);
        if (t < 0 || t >= _UNIT_TYPE_MAX)
                return false;

        if (!(e = strrchr(n, '.')))
                return false;

65
66
67
        if (e == n)
                return false;

Lennart Poettering's avatar
Lennart Poettering committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
        for (i = n; i < e; i++)
                if (!strchr(VALID_CHARS, *i))
                        return false;

        return true;
}

char *unit_name_change_suffix(const char *n, const char *suffix) {
        char *e, *r;
        size_t a, b;

        assert(n);
        assert(unit_name_is_valid(n));
        assert(suffix);

        assert_se(e = strrchr(n, '.'));
        a = e - n;
        b = strlen(suffix);

        if (!(r = new(char, a + b + 1)))
                return NULL;

        memcpy(r, n, a);
        memcpy(r+a, suffix, b+1);

        return r;
}

Unit *unit_new(Manager *m) {
        Unit *u;

        assert(m);

        if (!(u = new0(Unit, 1)))
                return NULL;

        if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
                free(u);
                return NULL;
        }

        u->meta.manager = m;
        u->meta.type = _UNIT_TYPE_INVALID;

        return u;
}

int unit_add_name(Unit *u, const char *text) {
        UnitType t;
        char *s;
        int r;

        assert(u);
        assert(text);

        if (!unit_name_is_valid(text))
                return -EINVAL;

        if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
                return -EINVAL;

        if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
                return -EINVAL;

        if (!(s = strdup(text)))
                return -ENOMEM;

        if ((r = set_put(u->meta.names, s)) < 0) {
                free(s);

                if (r == -EEXIST)
                        return 0;

                return r;
        }

        if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
                set_remove(u->meta.names, s);
                free(s);
                return r;
        }

        u->meta.type = t;

        if (!u->meta.id)
                u->meta.id = s;

        return 0;
}

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
int unit_choose_id(Unit *u, const char *name) {
        char *s;

        assert(u);
        assert(name);

        /* Selects one of the names of this unit as the id */

        if (!(s = set_get(u->meta.names, (char*) name)))
                return -ENOENT;

        u->meta.id = s;
        return 0;
}

Lennart Poettering's avatar
Lennart Poettering committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
void unit_add_to_load_queue(Unit *u) {
        assert(u);

        if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
                return;

        LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
        u->meta.in_load_queue = true;
}

static void bidi_set_free(Unit *u, Set *s) {
        Iterator i;
        Unit *other;

        assert(u);

        /* Frees the set and makes sure we are dropped from the
         * inverse pointers */

        SET_FOREACH(other, s, i) {
                UnitDependency d;

                for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
                        set_remove(other->meta.dependencies[d], u);
        }

        set_free(s);
}

void unit_free(Unit *u) {
        UnitDependency d;
        Iterator i;
        char *t;

        assert(u);

        /* Detach from next 'bigger' objects */

        SET_FOREACH(t, u->meta.names, i)
                hashmap_remove_value(u->meta.manager->units, t, u);

        if (u->meta.in_load_queue)
                LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);

        if (u->meta.load_state == UNIT_LOADED)
                if (UNIT_VTABLE(u)->done)
                        UNIT_VTABLE(u)->done(u);

        /* Free data and next 'smaller' objects */
        if (u->meta.job)
                job_free(u->meta.job);

        for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
                bidi_set_free(u, u->meta.dependencies[d]);

        free(u->meta.description);
229
        free(u->meta.load_path);
Lennart Poettering's avatar
Lennart Poettering committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

        while ((t = set_steal_first(u->meta.names)))
                free(t);
        set_free(u->meta.names);

        free(u);
}

UnitActiveState unit_active_state(Unit *u) {
        assert(u);

        if (u->meta.load_state != UNIT_LOADED)
                return UNIT_INACTIVE;

        return UNIT_VTABLE(u)->active_state(u);
}

static int ensure_merge(Set **s, Set *other) {

        if (!other)
                return 0;

        if (*s)
                return set_merge(*s, other);

        if (!(*s = set_copy(other)))
                return -ENOMEM;

        return 0;
}

261
262
/* FIXME: Does not rollback on failure! Needs to fix special unit
 * pointers. Needs to merge names and dependencies properly.*/
Lennart Poettering's avatar
Lennart Poettering committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
int unit_merge(Unit *u, Unit *other) {
        int r;
        UnitDependency d;

        assert(u);
        assert(other);
        assert(u->meta.manager == other->meta.manager);

        /* This merges 'other' into 'unit'. FIXME: This does not
         * rollback on failure. */

        if (u->meta.type != u->meta.type)
                return -EINVAL;

        if (u->meta.load_state != UNIT_STUB)
                return -EINVAL;

        /* Merge names */
        if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0)
                return r;

        /* Merge dependencies */
        for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
                /* fixme, the inverse mapping is missing */
                if ((r = ensure_merge(&u->meta.dependencies[d], other->meta.dependencies[d])) < 0)
                        return r;

        return 0;
}

const char* unit_id(Unit *u) {
        assert(u);

        if (u->meta.id)
                return u->meta.id;

        return set_first(u->meta.names);
}

const char *unit_description(Unit *u) {
        assert(u);

        if (u->meta.description)
                return u->meta.description;

        return unit_id(u);
}

void unit_dump(Unit *u, FILE *f, const char *prefix) {

        static const char* const load_state_table[_UNIT_LOAD_STATE_MAX] = {
                [UNIT_STUB] = "stub",
                [UNIT_LOADED] = "loaded",
                [UNIT_FAILED] = "failed"
        };

        static const char* const active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
                [UNIT_ACTIVE] = "active",
                [UNIT_INACTIVE] = "inactive",
                [UNIT_ACTIVATING] = "activating",
                [UNIT_DEACTIVATING] = "deactivating"
        };

        static const char* const dependency_table[_UNIT_DEPENDENCY_MAX] = {
                [UNIT_REQUIRES] = "Requires",
                [UNIT_SOFT_REQUIRES] = "SoftRequires",
                [UNIT_WANTS] = "Wants",
                [UNIT_REQUISITE] = "Requisite",
                [UNIT_SOFT_REQUISITE] = "SoftRequisite",
                [UNIT_REQUIRED_BY] = "RequiredBy",
                [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
                [UNIT_WANTED_BY] = "WantedBy",
                [UNIT_CONFLICTS] = "Conflicts",
                [UNIT_BEFORE] = "Before",
                [UNIT_AFTER] = "After",
        };

        char *t;
        UnitDependency d;
        Iterator i;
        char *prefix2;

        assert(u);

        if (!prefix)
                prefix = "";
        prefix2 = strappend(prefix, "\t");
        if (!prefix2)
                prefix2 = "";

        fprintf(f,
                "%s→ Unit %s:\n"
                "%s\tDescription: %s\n"
                "%s\tUnit Load State: %s\n"
                "%s\tUnit Active State: %s\n",
                prefix, unit_id(u),
                prefix, unit_description(u),
                prefix, load_state_table[u->meta.load_state],
                prefix, active_state_table[unit_active_state(u)]);

363
364
365
        if (u->meta.load_path)
                fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path);

Lennart Poettering's avatar
Lennart Poettering committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
        SET_FOREACH(t, u->meta.names, i)
                fprintf(f, "%s\tName: %s\n", prefix, t);

        for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
                Unit *other;

                if (set_isempty(u->meta.dependencies[d]))
                        continue;

                SET_FOREACH(other, u->meta.dependencies[d], i)
                        fprintf(f, "%s\t%s: %s\n", prefix, dependency_table[d], unit_id(other));
        }

        if (UNIT_VTABLE(u)->dump)
                UNIT_VTABLE(u)->dump(u, f, prefix2);

        if (u->meta.job)
                job_dump(u->meta.job, f, prefix2);

        free(prefix2);
}

/* Common implementation for multiple backends */
int unit_load_fragment_and_dropin(Unit *u) {
390
        int r, ret;
Lennart Poettering's avatar
Lennart Poettering committed
391
392
393
394
395
396
397

        assert(u);

        /* Load a .socket file */
        if ((r = unit_load_fragment(u)) < 0)
                return r;

398
399
        ret = r > 0;

Lennart Poettering's avatar
Lennart Poettering committed
400
401
402
403
        /* Load drop-in directory data */
        if ((r = unit_load_dropin(u)) < 0)
                return r;

404
        return ret;
Lennart Poettering's avatar
Lennart Poettering committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
}

int unit_load(Unit *u) {
        int r;

        assert(u);

        if (u->meta.in_load_queue) {
                LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
                u->meta.in_load_queue = false;
        }

        if (u->meta.load_state != UNIT_STUB)
                return 0;

        if (UNIT_VTABLE(u)->init)
                if ((r = UNIT_VTABLE(u)->init(u)) < 0)
                        goto fail;

        u->meta.load_state = UNIT_LOADED;
        return 0;

fail:
        u->meta.load_state = UNIT_FAILED;
        return r;
}

/* Errors:
 *         -EBADR:    This unit type does not support starting.
 *         -EALREADY: Unit is already started.
 *         -EAGAIN:   An operation is already in progress. Retry later.
 */
int unit_start(Unit *u) {
        UnitActiveState state;

        assert(u);

        if (!UNIT_VTABLE(u)->start)
                return -EBADR;

        state = unit_active_state(u);
        if (UNIT_IS_ACTIVE_OR_RELOADING(state))
                return -EALREADY;

        /* We don't suppress calls to ->start() here when we are
         * already starting, to allow this request to be used as a
         * "hurry up" call, for example when the unit is in some "auto
         * restart" state where it waits for a holdoff timer to elapse
         * before it will start again. */

        return UNIT_VTABLE(u)->start(u);
}

bool unit_can_start(Unit *u) {
        assert(u);

        return !!UNIT_VTABLE(u)->start;
}

/* Errors:
 *         -EBADR:    This unit type does not support stopping.
 *         -EALREADY: Unit is already stopped.
 *         -EAGAIN:   An operation is already in progress. Retry later.
 */
int unit_stop(Unit *u) {
        UnitActiveState state;

        assert(u);

        if (!UNIT_VTABLE(u)->stop)
                return -EBADR;

        state = unit_active_state(u);
        if (state == UNIT_INACTIVE)
                return -EALREADY;

        if (state == UNIT_DEACTIVATING)
                return 0;

        return UNIT_VTABLE(u)->stop(u);
}

/* Errors:
 *         -EBADR:    This unit type does not support reloading.
 *         -ENOEXEC:  Unit is not started.
 *         -EAGAIN:   An operation is already in progress. Retry later.
 */
int unit_reload(Unit *u) {
        UnitActiveState state;

        assert(u);

        if (!unit_can_reload(u))
                return -EBADR;

        state = unit_active_state(u);
        if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
                return -EALREADY;

        if (unit_active_state(u) != UNIT_ACTIVE)
                return -ENOEXEC;

        return UNIT_VTABLE(u)->reload(u);
}

bool unit_can_reload(Unit *u) {
        assert(u);

        if (!UNIT_VTABLE(u)->reload)
                return false;

        if (!UNIT_VTABLE(u)->can_reload)
                return true;

        return UNIT_VTABLE(u)->can_reload(u);
}

static void retroactively_start_dependencies(Unit *u) {
        Iterator i;
        Unit *other;

        assert(u);
        assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));

        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);

        SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);

        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);

        SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);

        SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                        manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
}

static void retroactively_stop_dependencies(Unit *u) {
        Iterator i;
        Unit *other;

        assert(u);
        assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));

        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
}

void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
        assert(u);
        assert(os < _UNIT_ACTIVE_STATE_MAX);
        assert(ns < _UNIT_ACTIVE_STATE_MAX);
        assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
        assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));

        if (os == ns)
                return;

        if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
                u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
        else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
                u->meta.active_exit_timestamp = now(CLOCK_REALTIME);

        if (u->meta.job) {

                if (u->meta.job->state == JOB_WAITING)

                        /* So we reached a different state for this
                         * job. Let's see if we can run it now if it
                         * failed previously due to EAGAIN. */
                        job_schedule_run(u->meta.job);

                else {
                        assert(u->meta.job->state == JOB_RUNNING);

                        /* Let's check of this state change
                         * constitutes a finished job, or maybe
                         * cotradicts a running job and hence needs to
                         * invalidate jobs. */

                        switch (u->meta.job->type) {

                        case JOB_START:
                        case JOB_VERIFY_ACTIVE:

                                if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
                                        job_finish_and_invalidate(u->meta.job, true);
                                        return;
                                } else if (ns == UNIT_ACTIVATING)
                                        return;
                                else
                                        job_finish_and_invalidate(u->meta.job, false);

                                break;

                        case JOB_RELOAD:
                        case JOB_RELOAD_OR_START:

                                if (ns == UNIT_ACTIVE) {
                                        job_finish_and_invalidate(u->meta.job, true);
                                        return;
                                } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
                                        return;
                                else
                                        job_finish_and_invalidate(u->meta.job, false);

                                break;

                        case JOB_STOP:
                        case JOB_RESTART:
                        case JOB_TRY_RESTART:

                                if (ns == UNIT_INACTIVE) {
                                        job_finish_and_invalidate(u->meta.job, true);
                                        return;
                                } else if (ns == UNIT_DEACTIVATING)
                                        return;
                                else
                                        job_finish_and_invalidate(u->meta.job, false);

                                break;

                        default:
                                assert_not_reached("Job type unknown");
                        }
                }
        }

        /* If this state change happened without being requested by a
         * job, then let's retroactively start or stop dependencies */

        if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
                retroactively_start_dependencies(u);
        else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
                retroactively_stop_dependencies(u);
}

651
int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
652
653
654
655
        struct epoll_event ev;

        assert(u);
        assert(fd >= 0);
656
657
        assert(w);
        assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->unit == u));
Lennart Poettering's avatar
Lennart Poettering committed
658
659

        zero(ev);
660
        ev.data.ptr = w;
Lennart Poettering's avatar
Lennart Poettering committed
661
662
        ev.events = events;

663
664
665
666
667
        if (epoll_ctl(u->meta.manager->epoll_fd,
                      w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
                      fd,
                      &ev) < 0)
                return -errno;
Lennart Poettering's avatar
Lennart Poettering committed
668

669
670
671
        w->fd = fd;
        w->type = WATCH_FD;
        w->unit = u;
Lennart Poettering's avatar
Lennart Poettering committed
672

673
        return 0;
Lennart Poettering's avatar
Lennart Poettering committed
674
675
}

676
void unit_unwatch_fd(Unit *u, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
677
        assert(u);
678
        assert(w);
Lennart Poettering's avatar
Lennart Poettering committed
679

680
681
682
683
684
685
686
687
688
        if (w->type == WATCH_INVALID)
                return;

        assert(w->type == WATCH_FD && w->unit == u);
        assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);

        w->fd = -1;
        w->type = WATCH_INVALID;
        w->unit = NULL;
Lennart Poettering's avatar
Lennart Poettering committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
}

int unit_watch_pid(Unit *u, pid_t pid) {
        assert(u);
        assert(pid >= 1);

        return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
}

void unit_unwatch_pid(Unit *u, pid_t pid) {
        assert(u);
        assert(pid >= 1);

        hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
}

705
int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
706
        struct itimerspec its;
707
        int flags, fd;
Lennart Poettering's avatar
Lennart Poettering committed
708
709
710
        bool ours;

        assert(u);
711
712
        assert(w);
        assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->unit == u));
Lennart Poettering's avatar
Lennart Poettering committed
713
714
715

        /* This will try to reuse the old timer if there is one */

716
        if (w->type == WATCH_TIMER) {
Lennart Poettering's avatar
Lennart Poettering committed
717
                ours = false;
718
                fd = w->fd;
Lennart Poettering's avatar
Lennart Poettering committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
        } else {
                ours = true;
                if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
                        return -errno;
        }

        zero(its);

        if (delay <= 0) {
                /* Set absolute time in the past, but not 0, since we
                 * don't want to disarm the timer */
                its.it_value.tv_sec = 0;
                its.it_value.tv_nsec = 1;

                flags = TFD_TIMER_ABSTIME;
        } else {
                timespec_store(&its.it_value, delay);
                flags = 0;
        }

        /* This will also flush the elapse counter */
        if (timerfd_settime(fd, flags, &its, NULL) < 0)
                goto fail;

743
744
        if (w->type == WATCH_INVALID) {
                struct epoll_event ev;
Lennart Poettering's avatar
Lennart Poettering committed
745

746
747
748
749
750
751
752
753
754
755
756
                zero(ev);
                ev.data.ptr = w;
                ev.events = POLLIN;

                if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
                        goto fail;
        }

        w->fd = fd;
        w->type = WATCH_TIMER;
        w->unit = u;
Lennart Poettering's avatar
Lennart Poettering committed
757
758
759
760
761
762
763
764
765
766

        return 0;

fail:
        if (ours)
                assert_se(close_nointr(fd) == 0);

        return -errno;
}

767
void unit_unwatch_timer(Unit *u, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
768
        assert(u);
769
        assert(w);
Lennart Poettering's avatar
Lennart Poettering committed
770

771
        if (w->type == WATCH_INVALID)
Lennart Poettering's avatar
Lennart Poettering committed
772
773
                return;

774
775
776
777
778
779
780
781
        assert(w->type == WATCH_TIMER && w->unit == u);

        assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
        assert_se(close_nointr(w->fd) == 0);

        w->fd = -1;
        w->type = WATCH_INVALID;
        w->unit = NULL;
Lennart Poettering's avatar
Lennart Poettering committed
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
}

bool unit_job_is_applicable(Unit *u, JobType j) {
        assert(u);
        assert(j >= 0 && j < _JOB_TYPE_MAX);

        switch (j) {

        case JOB_VERIFY_ACTIVE:
        case JOB_START:
                return true;

        case JOB_STOP:
        case JOB_RESTART:
        case JOB_TRY_RESTART:
                return unit_can_start(u);

        case JOB_RELOAD:
                return unit_can_reload(u);

        case JOB_RELOAD_OR_START:
                return unit_can_reload(u) && unit_can_start(u);

        default:
                assert_not_reached("Invalid job type");
        }
}

int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {

        static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
                [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
                [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
                [UNIT_WANTS] = UNIT_WANTED_BY,
                [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
                [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
                [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
                [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
                [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
                [UNIT_CONFLICTS] = UNIT_CONFLICTS,
                [UNIT_BEFORE] = UNIT_AFTER,
                [UNIT_AFTER] = UNIT_BEFORE
        };
        int r;

        assert(u);
        assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
        assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
        assert(other);

        /* We won't allow dependencies on ourselves. We will not
         * consider them an error however. */
        if (u == other)
                return 0;

        if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
                return r;

        if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
                return r;

        if ((r = set_put(u->meta.dependencies[d], other)) < 0)
                return r;

        if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
                set_remove(u->meta.dependencies[d], other);
                return r;
        }

        return 0;
}
853

854
855
856
857
858
859
860
861
862
863
864
865
866
int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
        Unit *other;
        int r;

        if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
                return r;

        if ((r = unit_add_dependency(u, d, other)) < 0)
                return r;

        return 0;
}

867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
const char *unit_path(void) {
        char *e;

        if ((e = getenv("UNIT_PATH")))
                if (path_is_absolute(e))
                    return e;

        return UNIT_PATH;
}

int set_unit_path(const char *p) {
        char *cwd, *c;
        int r;

        /* This is mostly for debug purposes */

        if (path_is_absolute(p)) {
                if (!(c = strdup(p)))
                        return -ENOMEM;
        } else {
                if (!(cwd = get_current_dir_name()))
                        return -errno;

                r = asprintf(&c, "%s/%s", cwd, p);
                free(cwd);

                if (r < 0)
                        return -ENOMEM;
        }

        if (setenv("UNIT_PATH", c, 0) < 0) {
                r = -errno;
                free(c);
                return r;
        }

        return 0;
}
905

Lennart Poettering's avatar
Lennart Poettering committed
906
char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) {
907
908
        char *r, *t;
        const char *f;
Lennart Poettering's avatar
Lennart Poettering committed
909
        size_t a, b, c;
910
911
912

        assert(path);

Lennart Poettering's avatar
Lennart Poettering committed
913
914
915
        /* Takes a path and a suffix and prefix and makes a nice
         * string suitable as unit name of it, escaping all weird
         * chars on the way.
916
         *
Lennart Poettering's avatar
Lennart Poettering committed
917
918
919
         * / becomes ., and all chars not alloweed in a unit name get
         * escaped as \xFF, including \ and ., of course. This
         * escaping is hence reversible.
920
921
         */

Lennart Poettering's avatar
Lennart Poettering committed
922
923
924
925
926
        if (!prefix)
                prefix = "";

        if (!suffix)
                suffix = "";
927

Lennart Poettering's avatar
Lennart Poettering committed
928
929
930
931
932
        a = strlen(prefix);
        b = strlen(path);
        c = strlen(suffix);

        if (!(r = new(char, a+b*4+c+1)))
933
934
                return NULL;

Lennart Poettering's avatar
Lennart Poettering committed
935
936
937
        memcpy(r, prefix, a);

        for (f = path, t = r+a; *f; f++) {
938
                if (*f == '/')
Lennart Poettering's avatar
Lennart Poettering committed
939
940
                        *(t++) = '.';
                else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
941
942
943
944
945
946
947
948
                        *(t++) = '\\';
                        *(t++) = 'x';
                        *(t++) = hexchar(*f > 4);
                        *(t++) = hexchar(*f);
                } else
                        *(t++) = *f;
        }

Lennart Poettering's avatar
Lennart Poettering committed
949
        memcpy(t, suffix, c+1);
950
951
952

        return r;
}