unit.c 39.5 KB
Newer Older
Lennart Poettering's avatar
Lennart Poettering committed
1
2
/*-*- Mode: C; c-basic-offset: 8 -*-*/

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

Lennart Poettering's avatar
Lennart Poettering committed
22
23
24
25
26
27
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <sys/poll.h>
28
29
#include <stdlib.h>
#include <unistd.h>
Lennart Poettering's avatar
Lennart Poettering committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#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"            \
66
        "-_.\\"
Lennart Poettering's avatar
Lennart Poettering committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

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;

84
85
86
        if (e == n)
                return false;

Lennart Poettering's avatar
Lennart Poettering committed
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
158
159
160
161
162
163
164
165
166
167
168
        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;
        }

Lennart Poettering's avatar
Lennart Poettering committed
169
170
171
        if (u->meta.type == _UNIT_TYPE_INVALID)
                LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);

Lennart Poettering's avatar
Lennart Poettering committed
172
173
174
175
176
        u->meta.type = t;

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

177
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
178
179
180
        return 0;
}

181
182
183
184
185
186
187
188
189
190
191
192
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;
193
194

        unit_add_to_dbus_queue(u);
195
196
197
        return 0;
}

Lennart Poettering's avatar
Lennart Poettering committed
198
199
200
201
202
203
204
205
206
207
int unit_set_description(Unit *u, const char *description) {
        char *s;

        assert(u);

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

        free(u->meta.description);
        u->meta.description = s;
208
209

        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
210
211
212
        return 0;
}

Lennart Poettering's avatar
Lennart Poettering committed
213
214
215
216
217
218
219
220
221
222
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;
}

223
224
225
226
227
228
229
230
231
232
void unit_add_to_cleanup_queue(Unit *u) {
        assert(u);

        if (u->meta.in_cleanup_queue)
                return;

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

233
234
235
236
237
238
239
240
241
242
void unit_add_to_dbus_queue(Unit *u) {
        assert(u);

        if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
                return;

        LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
        u->meta.in_dbus_queue = true;
}

Lennart Poettering's avatar
Lennart Poettering committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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);

269
270
        bus_unit_send_removed_signal(u);

Lennart Poettering's avatar
Lennart Poettering committed
271
272
        /* Detach from next 'bigger' objects */

273
274
        cgroup_bonding_free_list(u->meta.cgroup_bondings);

Lennart Poettering's avatar
Lennart Poettering committed
275
276
277
        SET_FOREACH(t, u->meta.names, i)
                hashmap_remove_value(u->meta.manager->units, t, u);

Lennart Poettering's avatar
Lennart Poettering committed
278
279
280
        if (u->meta.type != _UNIT_TYPE_INVALID)
                LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);

Lennart Poettering's avatar
Lennart Poettering committed
281
282
283
        if (u->meta.in_load_queue)
                LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);

284
285
286
        if (u->meta.in_dbus_queue)
                LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);

287
288
289
290
        if (u->meta.in_cleanup_queue)
                LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);

        if (u->meta.load_state != UNIT_STUB)
Lennart Poettering's avatar
Lennart Poettering committed
291
292
293
294
295
296
297
298
299
300
301
                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);
302
        free(u->meta.fragment_path);
Lennart Poettering's avatar
Lennart Poettering committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

        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);
}

320
321
322
static void complete_move(Set **s, Set **other) {
        assert(s);
        assert(other);
Lennart Poettering's avatar
Lennart Poettering committed
323

324
325
        if (!*other)
                return;
Lennart Poettering's avatar
Lennart Poettering committed
326
327

        if (*s)
328
329
330
331
332
333
                set_move(*s, *other);
        else {
                *s = *other;
                *other = NULL;
        }
}
Lennart Poettering's avatar
Lennart Poettering committed
334

335
336
337
static void merge_names(Unit *u, Unit *other) {
        char *t;
        Iterator i;
Lennart Poettering's avatar
Lennart Poettering committed
338

339
340
341
342
343
344
345
346
347
348
349
350
351
352
        assert(u);
        assert(other);

        complete_move(&u->meta.names, &other->meta.names);

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

        set_free(other->meta.names);
        other->meta.names = NULL;
        other->meta.id = NULL;

        SET_FOREACH(t, u->meta.names, i)
                assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
Lennart Poettering's avatar
Lennart Poettering committed
353
354
}

355
356
357
static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
        Iterator i;
        Unit *back;
Lennart Poettering's avatar
Lennart Poettering committed
358
        int r;
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

        assert(u);
        assert(other);
        assert(d < _UNIT_DEPENDENCY_MAX);

        SET_FOREACH(back, other->meta.dependencies[d], i) {
                UnitDependency k;

                for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
                        if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {

                                if (r == -EEXIST)
                                        set_remove(back->meta.dependencies[k], other);
                                else
                                        assert(r == -ENOENT);
                        }
        }

        complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);

        set_free(other->meta.dependencies[d]);
        other->meta.dependencies[d] = NULL;
}

int unit_merge(Unit *u, Unit *other) {
Lennart Poettering's avatar
Lennart Poettering committed
384
385
386
387
388
389
        UnitDependency d;

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

390
391
392
        if (other == u)
                return 0;

Lennart Poettering's avatar
Lennart Poettering committed
393
394
395
396
397
398
        /* This merges 'other' into 'unit'. FIXME: This does not
         * rollback on failure. */

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

399
400
        if (other->meta.load_state != UNIT_STUB)
                return -EEXIST;
Lennart Poettering's avatar
Lennart Poettering committed
401
402

        /* Merge names */
403
        merge_names(u, other);
Lennart Poettering's avatar
Lennart Poettering committed
404
405
406

        /* Merge dependencies */
        for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
407
                merge_dependencies(u, other, d);
Lennart Poettering's avatar
Lennart Poettering committed
408

409
410
        unit_add_to_dbus_queue(u);

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
        other->meta.load_state = UNIT_MERGED;
        other->meta.merged_into = u;

        unit_add_to_cleanup_queue(other);

        return 0;
}

int unit_merge_by_name(Unit *u, const char *name) {
        Unit *other;

        assert(u);
        assert(name);

        if (!(other = manager_get_unit(u->meta.manager, name)))
                return unit_add_name(u, name);

        return unit_merge(u, other);
}

Unit* unit_follow_merge(Unit *u) {
        assert(u);

        while (u->meta.load_state == UNIT_MERGED)
                assert_se(u = u->meta.merged_into);

        return u;
}

int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
        int r;

        assert(u);
        assert(c);

        if (c->output != EXEC_OUTPUT_KERNEL && c->output != EXEC_OUTPUT_SYSLOG)
                return 0;

        /* If syslog or kernel logging is requested, make sure our own
         * logging daemon is run first. */

        if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
                return r;

        if (u->meta.manager->running_as != MANAGER_SESSION)
                if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
                        return r;

Lennart Poettering's avatar
Lennart Poettering committed
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
        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) {
        char *t;
        UnitDependency d;
        Iterator i;
484
485
        char *p2;
        const char *prefix2;
486
        CGroupBonding *b;
Lennart Poettering's avatar
Lennart Poettering committed
487
488
489
490
491

        assert(u);

        if (!prefix)
                prefix = "";
492
493
        p2 = strappend(prefix, "\t");
        prefix2 = p2 ? p2 : prefix;
Lennart Poettering's avatar
Lennart Poettering committed
494
495
496
497
498

        fprintf(f,
                "%s→ Unit %s:\n"
                "%s\tDescription: %s\n"
                "%s\tUnit Load State: %s\n"
499
                "%s\tUnit Active State: %s\n",
Lennart Poettering's avatar
Lennart Poettering committed
500
501
                prefix, unit_id(u),
                prefix, unit_description(u),
502
                prefix, unit_load_state_to_string(u->meta.load_state),
503
                prefix, unit_active_state_to_string(unit_active_state(u)));
504

Lennart Poettering's avatar
Lennart Poettering committed
505
506
507
        SET_FOREACH(t, u->meta.names, i)
                fprintf(f, "%s\tName: %s\n", prefix, t);

508
509
510
        if (u->meta.fragment_path)
                fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);

Lennart Poettering's avatar
Lennart Poettering committed
511
512
513
514
515
516
517
        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)
518
                        fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
Lennart Poettering's avatar
Lennart Poettering committed
519
520
        }

521
522
523
524
525
526
527
528
529
530
        fprintf(f,
                "%s\tRecursive Stop: %s\n"
                "%s\tStop When Unneeded: %s\n",
                prefix, yes_no(u->meta.recursive_stop),
                prefix, yes_no(u->meta.stop_when_unneeded));

        if (u->meta.load_state == UNIT_LOADED) {
                LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
                        fprintf(f, "%s\tControlGroup: %s:%s\n",
                                prefix, b->controller, b->path);
531

532
533
534
                if (UNIT_VTABLE(u)->dump)
                        UNIT_VTABLE(u)->dump(u, f, prefix2);
        }
Lennart Poettering's avatar
Lennart Poettering committed
535
536
537
538

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

539
        free(p2);
Lennart Poettering's avatar
Lennart Poettering committed
540
541
542
}

/* Common implementation for multiple backends */
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
int unit_load_fragment_and_dropin(Unit *u, UnitLoadState *new_state) {
        int r;

        assert(u);
        assert(new_state);
        assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);

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

        if (*new_state == UNIT_STUB)
                return -ENOENT;

        /* Load drop-in directory data */
        if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
                return r;

        return 0;
}

/* Common implementation for multiple backends */
int unit_load_fragment_and_dropin_optional(Unit *u, UnitLoadState *new_state) {
        int r;
Lennart Poettering's avatar
Lennart Poettering committed
567
568

        assert(u);
569
570
        assert(new_state);
        assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
Lennart Poettering's avatar
Lennart Poettering committed
571

572
573
574
575
576
        /* Same as unit_load_fragment_and_dropin(), but whether
         * something can be loaded or not doesn't matter. */

        /* Load a .service file */
        if ((r = unit_load_fragment(u, new_state)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
577
578
                return r;

579
580
        if (*new_state == UNIT_STUB)
                *new_state = UNIT_LOADED;
581

Lennart Poettering's avatar
Lennart Poettering committed
582
        /* Load drop-in directory data */
583
        if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
584
585
                return r;

586
        return 0;
Lennart Poettering's avatar
Lennart Poettering committed
587
588
589
590
}

int unit_load(Unit *u) {
        int r;
591
        UnitLoadState res;
Lennart Poettering's avatar
Lennart Poettering committed
592
593
594
595
596
597
598
599
600
601
602

        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;

603
604
605
        if (UNIT_VTABLE(u)->init) {
                res = UNIT_STUB;
                if ((r = UNIT_VTABLE(u)->init(u, &res)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
606
                        goto fail;
607
608
609
610
611
612
613
614
615
616
617
        }

        if (res == UNIT_STUB) {
                r = -ENOENT;
                goto fail;
        }

        u->meta.load_state = res;
        assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);

        unit_add_to_dbus_queue(unit_follow_merge(u));
Lennart Poettering's avatar
Lennart Poettering committed
618
619
620
621
622

        return 0;

fail:
        u->meta.load_state = UNIT_FAILED;
623
        unit_add_to_dbus_queue(u);
624
625
626

        log_error("Failed to load configuration for %s: %s", unit_id(u), strerror(-r));

Lennart Poettering's avatar
Lennart Poettering committed
627
628
629
630
631
632
633
634
635
636
637
638
639
        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);

640
641
642
643
        /* If this is already (being) started, then this will
         * succeed. Note that this will even succeed if this unit is
         * not startable by the user. This is relied on to detect when
         * we need to wait for units and when waiting is finished. */
Lennart Poettering's avatar
Lennart Poettering committed
644
645
646
647
        state = unit_active_state(u);
        if (UNIT_IS_ACTIVE_OR_RELOADING(state))
                return -EALREADY;

648
649
650
651
        /* If it is stopped, but we cannot start it, then fail */
        if (!UNIT_VTABLE(u)->start)
                return -EBADR;

Lennart Poettering's avatar
Lennart Poettering committed
652
653
654
655
656
657
        /* 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. */

658
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
        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);

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

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

Lennart Poettering's avatar
Lennart Poettering committed
685
686
687
        if (state == UNIT_DEACTIVATING)
                return 0;

688
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
        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;

712
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
        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);
}

728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
static void unit_check_uneeded(Unit *u) {
        Iterator i;
        Unit *other;

        assert(u);

        /* If this service shall be shut down when unneeded then do
         * so. */

        if (!u->meta.stop_when_unneeded)
                return;

        if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
                return;

        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        return;

        SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        return;

        SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        return;

        log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));

        /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
        manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
}

Lennart Poettering's avatar
Lennart Poettering committed
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
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)));

796
797
798
799
800
801
802
803
804
        if (u->meta.recursive_stop) {
                /* Pull down units need us recursively if enabled */
                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);
        }

        /* Garbage collect services that might not be needed anymore, if enabled */
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
Lennart Poettering's avatar
Lennart Poettering committed
805
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
806
807
808
809
810
811
812
813
814
815
816
817
818
                        unit_check_uneeded(other);
        SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        unit_check_uneeded(other);
        SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        unit_check_uneeded(other);
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        unit_check_uneeded(other);
        SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        unit_check_uneeded(other);
Lennart Poettering's avatar
Lennart Poettering committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
}

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. */
843
                        job_add_to_run_queue(u->meta.job);
Lennart Poettering's avatar
Lennart Poettering committed
844
845
846
847

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

Lennart Poettering's avatar
Lennart Poettering committed
848
                        /* Let's check whether this state change
Lennart Poettering's avatar
Lennart Poettering committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
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
905
906
907
                         * 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);
908
909
910
911

        /* Maybe we finished startup and are now ready for being
         * stopped because unneeded? */
        unit_check_uneeded(u);
912
913

        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
914
915
}

916
int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
917
918
919
920
        struct epoll_event ev;

        assert(u);
        assert(fd >= 0);
921
        assert(w);
922
        assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
Lennart Poettering's avatar
Lennart Poettering committed
923
924

        zero(ev);
925
        ev.data.ptr = w;
Lennart Poettering's avatar
Lennart Poettering committed
926
927
        ev.events = events;

928
929
930
931
932
        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
933

934
935
        w->fd = fd;
        w->type = WATCH_FD;
936
        w->data.unit = u;
Lennart Poettering's avatar
Lennart Poettering committed
937

938
        return 0;
Lennart Poettering's avatar
Lennart Poettering committed
939
940
}

941
void unit_unwatch_fd(Unit *u, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
942
        assert(u);
943
        assert(w);
Lennart Poettering's avatar
Lennart Poettering committed
944

945
946
947
        if (w->type == WATCH_INVALID)
                return;

948
949
        assert(w->type == WATCH_FD);
        assert(w->data.unit == u);
950
951
952
953
        assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);

        w->fd = -1;
        w->type = WATCH_INVALID;
954
        w->data.unit = NULL;
Lennart Poettering's avatar
Lennart Poettering committed
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
}

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));
}

971
int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
972
        struct itimerspec its;
973
        int flags, fd;
Lennart Poettering's avatar
Lennart Poettering committed
974
975
976
        bool ours;

        assert(u);
977
        assert(w);
978
        assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
Lennart Poettering's avatar
Lennart Poettering committed
979
980
981

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

982
        if (w->type == WATCH_TIMER) {
Lennart Poettering's avatar
Lennart Poettering committed
983
                ours = false;
984
                fd = w->fd;
Lennart Poettering's avatar
Lennart Poettering committed
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        } 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 {