unit.c 60.7 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>
30
#include <sys/stat.h>
Lennart Poettering's avatar
Lennart Poettering committed
31
32
33
34
35
36
37
38

#include "set.h"
#include "unit.h"
#include "macro.h"
#include "strv.h"
#include "load-fragment.h"
#include "load-dropin.h"
#include "log.h"
39
40
#include "unit-name.h"
#include "specifier.h"
41
#include "dbus-unit.h"
42
#include "special.h"
43
#include "cgroup-util.h"
Lennart Poettering's avatar
Lennart Poettering committed
44
45
46
47
48
49
50
51
52

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,
Maarten Lankhorst's avatar
Maarten Lankhorst committed
53
        [UNIT_SNAPSHOT] = &snapshot_vtable,
54
55
        [UNIT_SWAP] = &swap_vtable,
        [UNIT_PATH] = &path_vtable
Lennart Poettering's avatar
Lennart Poettering committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
};

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;
73
        u->meta.deserialized_job = _JOB_TYPE_INVALID;
74
        u->meta.default_dependencies = true;
Lennart Poettering's avatar
Lennart Poettering committed
75
76
77
78

        return u;
}

79
80
81
82
83
84
85
bool unit_has_name(Unit *u, const char *name) {
        assert(u);
        assert(name);

        return !!set_get(u->meta.names, (char*) name);
}

Lennart Poettering's avatar
Lennart Poettering committed
86
87
int unit_add_name(Unit *u, const char *text) {
        UnitType t;
88
        char *s, *i = NULL;
Lennart Poettering's avatar
Lennart Poettering committed
89
90
91
92
93
        int r;

        assert(u);
        assert(text);

94
95
96
        if (unit_name_is_template(text)) {
                if (!u->meta.instance)
                        return -EINVAL;
Lennart Poettering's avatar
Lennart Poettering committed
97

98
99
100
                s = unit_name_replace_instance(text, u->meta.instance);
        } else
                s = strdup(text);
Lennart Poettering's avatar
Lennart Poettering committed
101

102
103
        if (!s)
                return -ENOMEM;
Lennart Poettering's avatar
Lennart Poettering committed
104

105
106
107
108
        if (!unit_name_is_valid(s)) {
                r = -EINVAL;
                goto fail;
        }
109

110
        assert_se((t = unit_name_to_type(s)) >= 0);
Lennart Poettering's avatar
Lennart Poettering committed
111

112
113
114
115
        if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) {
                r = -EINVAL;
                goto fail;
        }
Lennart Poettering's avatar
Lennart Poettering committed
116

117
118
        if ((r = unit_name_to_instance(s, &i)) < 0)
                goto fail;
Lennart Poettering's avatar
Lennart Poettering committed
119

120
121
122
        if (i && unit_vtable[t]->no_instances)
                goto fail;

123
124
125
        /* Ensure that this unit is either instanced or not instanced,
         * but not both. */
        if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) {
126
127
128
129
130
131
132
133
134
135
136
                r = -EINVAL;
                goto fail;
        }

        if (unit_vtable[t]->no_alias &&
            !set_isempty(u->meta.names) &&
            !set_get(u->meta.names, s)) {
                r = -EEXIST;
                goto fail;
        }

137
138
139
140
141
        if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
                r = -E2BIG;
                goto fail;
        }

142
143
144
145
        if ((r = set_put(u->meta.names, s)) < 0) {
                if (r == -EEXIST)
                        r = 0;
                goto fail;
Lennart Poettering's avatar
Lennart Poettering committed
146
147
148
149
        }

        if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
                set_remove(u->meta.names, s);
150
                goto fail;
Lennart Poettering's avatar
Lennart Poettering committed
151
152
        }

153
        if (u->meta.type == _UNIT_TYPE_INVALID) {
Lennart Poettering's avatar
Lennart Poettering committed
154

155
                u->meta.type = t;
156
157
158
159
                u->meta.id = s;
                u->meta.instance = i;

                LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
160
161
162

                if (UNIT_VTABLE(u)->init)
                        UNIT_VTABLE(u)->init(u);
163
164
        } else
                free(i);
Lennart Poettering's avatar
Lennart Poettering committed
165

166
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
167
        return 0;
168
169
170
171
172
173

fail:
        free(s);
        free(i);

        return r;
Lennart Poettering's avatar
Lennart Poettering committed
174
175
}

176
int unit_choose_id(Unit *u, const char *name) {
177
178
        char *s, *t = NULL, *i;
        int r;
179
180
181
182

        assert(u);
        assert(name);

183
184
185
186
187
188
189
190
191
192
193
        if (unit_name_is_template(name)) {

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

                if (!(t = unit_name_replace_instance(name, u->meta.instance)))
                        return -ENOMEM;

                name = t;
        }

194
        /* Selects one of the names of this unit as the id */
195
196
        s = set_get(u->meta.names, (char*) name);
        free(t);
197

198
        if (!s)
199
200
                return -ENOENT;

201
202
203
        if ((r = unit_name_to_instance(s, &i)) < 0)
                return r;

204
        u->meta.id = s;
205
206
207
208

        free(u->meta.instance);
        u->meta.instance = i;

209
        unit_add_to_dbus_queue(u);
210

211
212
213
        return 0;
}

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

        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
226
227
228
        return 0;
}

229
230
231
bool unit_check_gc(Unit *u) {
        assert(u);

232
233
234
        if (u->meta.load_state == UNIT_STUB)
                return true;

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
        if (UNIT_VTABLE(u)->no_gc)
                return true;

        if (u->meta.job)
                return true;

        if (unit_active_state(u) != UNIT_INACTIVE)
                return true;

        if (UNIT_VTABLE(u)->check_gc)
                if (UNIT_VTABLE(u)->check_gc(u))
                        return true;

        return false;
}

Lennart Poettering's avatar
Lennart Poettering committed
251
252
void unit_add_to_load_queue(Unit *u) {
        assert(u);
253
        assert(u->meta.type != _UNIT_TYPE_INVALID);
Lennart Poettering's avatar
Lennart Poettering committed
254
255
256
257
258
259
260
261

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

262
263
264
265
266
267
268
269
270
271
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;
}

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
void unit_add_to_gc_queue(Unit *u) {
        assert(u);

        if (u->meta.in_gc_queue || u->meta.in_cleanup_queue)
                return;

        if (unit_check_gc(u))
                return;

        LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
        u->meta.in_gc_queue = true;

        u->meta.manager->n_in_gc_queue ++;

        if (u->meta.manager->gc_queue_timestamp <= 0)
                u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC);
}

290
291
void unit_add_to_dbus_queue(Unit *u) {
        assert(u);
292
        assert(u->meta.type != _UNIT_TYPE_INVALID);
293

294
        if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue)
295
296
                return;

297
298
        /* Shortcut things if nobody cares */
        if (!bus_has_subscriber(u->meta.manager)) {
299
300
301
302
                u->meta.sent_dbus_new_signal = true;
                return;
        }

303
304
305
306
        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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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);
321
322

                unit_add_to_gc_queue(other);
Lennart Poettering's avatar
Lennart Poettering committed
323
324
325
326
327
328
329
330
331
332
333
334
        }

        set_free(s);
}

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

        assert(u);

335
336
        bus_unit_send_removed_signal(u);

337
338
339
340
        if (u->meta.load_state != UNIT_STUB)
                if (UNIT_VTABLE(u)->done)
                        UNIT_VTABLE(u)->done(u);

Lennart Poettering's avatar
Lennart Poettering committed
341
342
343
        SET_FOREACH(t, u->meta.names, i)
                hashmap_remove_value(u->meta.manager->units, t, u);

344
345
346
347
348
349
        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]);

Lennart Poettering's avatar
Lennart Poettering committed
350
351
352
        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
353
354
355
        if (u->meta.in_load_queue)
                LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);

356
357
358
        if (u->meta.in_dbus_queue)
                LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);

359
360
361
        if (u->meta.in_cleanup_queue)
                LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);

362
363
364
365
366
        if (u->meta.in_gc_queue) {
                LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
                u->meta.manager->n_in_gc_queue--;
        }

367
        cgroup_bonding_free_list(u->meta.cgroup_bondings);
Lennart Poettering's avatar
Lennart Poettering committed
368
369

        free(u->meta.description);
370
        free(u->meta.fragment_path);
Lennart Poettering's avatar
Lennart Poettering committed
371

372
        set_free_free(u->meta.names);
Lennart Poettering's avatar
Lennart Poettering committed
373

374
375
        free(u->meta.instance);

Lennart Poettering's avatar
Lennart Poettering committed
376
377
378
379
380
381
        free(u);
}

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

382
383
384
385
386
387
        if (u->meta.load_state == UNIT_MERGED)
                return unit_active_state(unit_follow_merge(u));

        /* After a reload it might happen that a unit is not correctly
         * loaded but still has a process around. That's why we won't
         * shortcut failed loading to UNIT_INACTIVE_MAINTENANCE. */
Lennart Poettering's avatar
Lennart Poettering committed
388
389
390
391

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

392
393
394
395
396
397
const char* unit_sub_state_to_string(Unit *u) {
        assert(u);

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

398
399
400
static void complete_move(Set **s, Set **other) {
        assert(s);
        assert(other);
Lennart Poettering's avatar
Lennart Poettering committed
401

402
403
        if (!*other)
                return;
Lennart Poettering's avatar
Lennart Poettering committed
404
405

        if (*s)
406
407
408
409
410
411
                set_move(*s, *other);
        else {
                *s = *other;
                *other = NULL;
        }
}
Lennart Poettering's avatar
Lennart Poettering committed
412

413
414
415
static void merge_names(Unit *u, Unit *other) {
        char *t;
        Iterator i;
Lennart Poettering's avatar
Lennart Poettering committed
416

417
418
419
420
421
        assert(u);
        assert(other);

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

422
        set_free_free(other->meta.names);
423
424
425
426
427
        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
428
429
}

430
431
432
static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
        Iterator i;
        Unit *back;
Lennart Poettering's avatar
Lennart Poettering committed
433
        int r;
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

        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
459
460
461
462
463
        UnitDependency d;

        assert(u);
        assert(other);
        assert(u->meta.manager == other->meta.manager);
464
        assert(u->meta.type != _UNIT_TYPE_INVALID);
Lennart Poettering's avatar
Lennart Poettering committed
465

466
467
        other = unit_follow_merge(other);

468
469
470
        if (other == u)
                return 0;

471
472
473
        if (u->meta.type != other->meta.type)
                return -EINVAL;

474
        if (!u->meta.instance != !other->meta.instance)
Lennart Poettering's avatar
Lennart Poettering committed
475
476
                return -EINVAL;

477
478
        if (other->meta.load_state != UNIT_STUB &&
            other->meta.load_state != UNIT_FAILED)
479
                return -EEXIST;
Lennart Poettering's avatar
Lennart Poettering committed
480

481
482
483
        if (other->meta.job)
                return -EEXIST;

484
        if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(unit_active_state(other)))
485
486
                return -EEXIST;

Lennart Poettering's avatar
Lennart Poettering committed
487
        /* Merge names */
488
        merge_names(u, other);
Lennart Poettering's avatar
Lennart Poettering committed
489
490
491

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

494
495
496
        other->meta.load_state = UNIT_MERGED;
        other->meta.merged_into = u;

497
498
499
500
501
502
503
        /* If there is still some data attached to the other node, we
         * don't need it anymore, and can free it. */
        if (other->meta.load_state != UNIT_STUB)
                if (UNIT_VTABLE(other)->done)
                        UNIT_VTABLE(other)->done(other);

        unit_add_to_dbus_queue(u);
504
505
506
507
508
509
510
        unit_add_to_cleanup_queue(other);

        return 0;
}

int unit_merge_by_name(Unit *u, const char *name) {
        Unit *other;
511
512
        int r;
        char *s = NULL;
513
514
515
516

        assert(u);
        assert(name);

517
518
519
520
521
522
523
524
525
526
        if (unit_name_is_template(name)) {
                if (!u->meta.instance)
                        return -EINVAL;

                if (!(s = unit_name_replace_instance(name, u->meta.instance)))
                        return -ENOMEM;

                name = s;
        }

527
        if (!(other = manager_get_unit(u->meta.manager, name)))
528
529
530
                r = unit_add_name(u, name);
        else
                r = unit_merge(u, other);
531

532
533
        free(s);
        return r;
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
}

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

551
552
553
554
        if (c->std_output != EXEC_OUTPUT_KMSG &&
            c->std_output != EXEC_OUTPUT_SYSLOG &&
            c->std_error != EXEC_OUTPUT_KMSG &&
            c->std_error != EXEC_OUTPUT_SYSLOG)
555
556
557
558
559
                return 0;

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

560
        if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
561
562
                return r;

563
        if (u->meta.manager->running_as == MANAGER_SYSTEM)
564
                if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
565
566
                        return r;

Lennart Poettering's avatar
Lennart Poettering committed
567
568
569
570
571
572
573
574
575
        return 0;
}

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

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

576
        return u->meta.id;
Lennart Poettering's avatar
Lennart Poettering committed
577
578
579
580
581
582
}

void unit_dump(Unit *u, FILE *f, const char *prefix) {
        char *t;
        UnitDependency d;
        Iterator i;
583
584
        char *p2;
        const char *prefix2;
585
        CGroupBonding *b;
586
587
588
589
        char
                timestamp1[FORMAT_TIMESTAMP_MAX],
                timestamp2[FORMAT_TIMESTAMP_MAX],
                timestamp3[FORMAT_TIMESTAMP_MAX],
590
591
                timestamp4[FORMAT_TIMESTAMP_MAX],
                timespan[FORMAT_TIMESPAN_MAX];
Lennart Poettering's avatar
Lennart Poettering committed
592
593

        assert(u);
594
        assert(u->meta.type >= 0);
Lennart Poettering's avatar
Lennart Poettering committed
595
596
597

        if (!prefix)
                prefix = "";
598
599
        p2 = strappend(prefix, "\t");
        prefix2 = p2 ? p2 : prefix;
Lennart Poettering's avatar
Lennart Poettering committed
600
601

        fprintf(f,
602
                "%s-> Unit %s:\n"
Lennart Poettering's avatar
Lennart Poettering committed
603
                "%s\tDescription: %s\n"
604
                "%s\tInstance: %s\n"
Lennart Poettering's avatar
Lennart Poettering committed
605
                "%s\tUnit Load State: %s\n"
606
                "%s\tUnit Active State: %s\n"
607
                "%s\tInactive Exit Timestamp: %s\n"
608
                "%s\tActive Enter Timestamp: %s\n"
609
                "%s\tActive Exit Timestamp: %s\n"
610
                "%s\tInactive Enter Timestamp: %s\n"
611
612
                "%s\tGC Check Good: %s\n"
                "%s\tNeed Daemon Reload: %s\n",
613
                prefix, u->meta.id,
Lennart Poettering's avatar
Lennart Poettering committed
614
                prefix, unit_description(u),
615
                prefix, strna(u->meta.instance),
616
                prefix, unit_load_state_to_string(u->meta.load_state),
617
                prefix, unit_active_state_to_string(unit_active_state(u)),
618
619
620
621
                prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.inactive_exit_timestamp.realtime)),
                prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_enter_timestamp.realtime)),
                prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->meta.active_exit_timestamp.realtime)),
                prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->meta.inactive_enter_timestamp.realtime)),
622
623
                prefix, yes_no(unit_check_gc(u)),
                prefix, yes_no(unit_need_daemon_reload(u)));
624

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

628
629
630
        if (u->meta.fragment_path)
                fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);

631
632
633
        if (u->meta.job_timeout > 0)
                fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->meta.job_timeout));

Lennart Poettering's avatar
Lennart Poettering committed
634
635
636
637
        for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
                Unit *other;

                SET_FOREACH(other, u->meta.dependencies[d], i)
638
                        fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id);
Lennart Poettering's avatar
Lennart Poettering committed
639
640
        }

641
        if (u->meta.load_state == UNIT_LOADED) {
642
643
                fprintf(f,
                        "%s\tRecursive Stop: %s\n"
644
645
                        "%s\tStopWhenUnneeded: %s\n"
                        "%s\tOnlyByDependency: %s\n"
646
647
                        "%s\tDefaultDependencies: %s\n"
                        "%s\tIgnoreDependencyFailure: %s\n",
648
                        prefix, yes_no(u->meta.recursive_stop),
649
650
                        prefix, yes_no(u->meta.stop_when_unneeded),
                        prefix, yes_no(u->meta.only_by_dependency),
651
652
                        prefix, yes_no(u->meta.default_dependencies),
                        prefix, yes_no(u->meta.ignore_dependency_failure));
653

654
655
656
                LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
                        fprintf(f, "%s\tControlGroup: %s:%s\n",
                                prefix, b->controller, b->path);
657

658
659
                if (UNIT_VTABLE(u)->dump)
                        UNIT_VTABLE(u)->dump(u, f, prefix2);
660
661
662
663

        } else if (u->meta.load_state == UNIT_MERGED)
                fprintf(f,
                        "%s\tMerged into: %s\n",
664
                        prefix, u->meta.merged_into->meta.id);
Lennart Poettering's avatar
Lennart Poettering committed
665
666
667
668

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

669
        free(p2);
Lennart Poettering's avatar
Lennart Poettering committed
670
671
672
}

/* Common implementation for multiple backends */
673
int unit_load_fragment_and_dropin(Unit *u) {
674
675
676
677
678
        int r;

        assert(u);

        /* Load a .service file */
679
        if ((r = unit_load_fragment(u)) < 0)
680
681
                return r;

682
        if (u->meta.load_state == UNIT_STUB)
683
684
685
686
687
688
689
690
691
692
                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 */
693
int unit_load_fragment_and_dropin_optional(Unit *u) {
694
        int r;
Lennart Poettering's avatar
Lennart Poettering committed
695
696
697

        assert(u);

698
699
700
701
        /* Same as unit_load_fragment_and_dropin(), but whether
         * something can be loaded or not doesn't matter. */

        /* Load a .service file */
702
        if ((r = unit_load_fragment(u)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
703
704
                return r;

705
706
        if (u->meta.load_state == UNIT_STUB)
                u->meta.load_state = UNIT_LOADED;
707

Lennart Poettering's avatar
Lennart Poettering committed
708
        /* Load drop-in directory data */
709
        if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
710
711
                return r;

712
        return 0;
Lennart Poettering's avatar
Lennart Poettering committed
713
714
}

715
716
717
718
719
720
721
722
723
724
/* Common implementation for multiple backends */
int unit_load_nop(Unit *u) {
        assert(u);

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

        return 0;
}

Lennart Poettering's avatar
Lennart Poettering committed
725
726
727
728
729
730
731
732
733
734
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;
        }

735
736
737
        if (u->meta.type == _UNIT_TYPE_INVALID)
                return -EINVAL;

Lennart Poettering's avatar
Lennart Poettering committed
738
739
740
        if (u->meta.load_state != UNIT_STUB)
                return 0;

741
742
        if (UNIT_VTABLE(u)->load)
                if ((r = UNIT_VTABLE(u)->load(u)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
743
                        goto fail;
744

745
        if (u->meta.load_state == UNIT_STUB) {
746
747
748
749
750
751
752
                r = -ENOENT;
                goto fail;
        }

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

        unit_add_to_dbus_queue(unit_follow_merge(u));
753
        unit_add_to_gc_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
754
755
756
757
758

        return 0;

fail:
        u->meta.load_state = UNIT_FAILED;
759
        unit_add_to_dbus_queue(u);
760

761
        log_notice("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
762

Lennart Poettering's avatar
Lennart Poettering committed
763
764
765
766
        return r;
}

/* Errors:
767
768
769
770
 *         -EBADR:     This unit type does not support starting.
 *         -EALREADY:  Unit is already started.
 *         -EAGAIN:    An operation is already in progress. Retry later.
 *         -ECANCELED: Too many requests for now.
Lennart Poettering's avatar
Lennart Poettering committed
771
772
773
774
775
776
 */
int unit_start(Unit *u) {
        UnitActiveState state;

        assert(u);

777
778
779
        if (u->meta.load_state != UNIT_LOADED)
                return -EINVAL;

780
781
782
783
        /* 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
784
785
786
787
        state = unit_active_state(u);
        if (UNIT_IS_ACTIVE_OR_RELOADING(state))
                return -EALREADY;

788
789
790
791
        /* 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
792
793
794
795
796
797
        /* 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. */

798
        unit_add_to_dbus_queue(u);
799
800
801

        unit_status_printf(u, "Starting %s...\n", unit_description(u));

Lennart Poettering's avatar
Lennart Poettering committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
        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);
822
        if (UNIT_IS_INACTIVE_OR_MAINTENANCE(state))
Lennart Poettering's avatar
Lennart Poettering committed
823
824
                return -EALREADY;

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

828
        unit_add_to_dbus_queue(u);
829
830
831

        unit_status_printf(u, "Stopping %s...\n", unit_description(u));

Lennart Poettering's avatar
Lennart Poettering committed
832
833
834
835
836
837
838
839
840
841
842
843
844
        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);

845
846
847
        if (u->meta.load_state != UNIT_LOADED)
                return -EINVAL;

Lennart Poettering's avatar
Lennart Poettering committed
848
849
850
851
        if (!unit_can_reload(u))
                return -EBADR;

        state = unit_active_state(u);
852
        if (unit_active_state(u) == UNIT_RELOADING)
Lennart Poettering's avatar
Lennart Poettering committed
853
854
855
856
857
                return -EALREADY;

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

858
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
        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);
}

874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
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;

893
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
894
895
896
897
898
899
900
                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;

901
        log_info("Service %s is not needed anymore. Stopping.", u->meta.id);
902
903

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

Lennart Poettering's avatar
Lennart Poettering committed
907
908
909
910
911
912
913
914
915
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)))
916
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
Lennart Poettering's avatar
Lennart Poettering committed
917

918
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
Lennart Poettering's avatar
Lennart Poettering committed
919
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
920
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
Lennart Poettering's avatar
Lennart Poettering committed
921
922
923

        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
924
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
Lennart Poettering's avatar
Lennart Poettering committed
925
926
927

        SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
928
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
Lennart Poettering's avatar
Lennart Poettering committed
929
930
931

        SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
932
                        manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
Lennart Poettering's avatar
Lennart Poettering committed
933
934
935
936
937
938
939
940
941
}

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

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

942
943
944
945
        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)))
946
                                manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
947
948
949
950
        }

        /* 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
951
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
952
                        unit_check_uneeded(other);
953
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
954
955
956
957
958
959
960
961
                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);
962
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
963
964
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        unit_check_uneeded(other);
Lennart Poettering's avatar
Lennart Poettering committed
965
966
967
}

void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
968
        dual_timestamp ts;
969
        bool unexpected;
970

Lennart Poettering's avatar
Lennart Poettering committed
971
972
973
974
        assert(u);
        assert(os < _UNIT_ACTIVE_STATE_MAX);
        assert(ns < _UNIT_ACTIVE_STATE_MAX);

975
976
977
978
979
980
        /* Note that this is called for all low-level state changes,
         * even if they might map to the same high-level
         * UnitActiveState! That means that ns == os is OK an expected
         * behaviour here. For example: if a mount point is remounted
         * this function will be called too and the utmp code below
         * relies on that! */
Lennart Poettering's avatar
Lennart Poettering committed
981

982
        dual_timestamp_get(&ts);
983

984
        if (UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && !UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
985
                u->meta.inactive_exit_timestamp = ts;
986
        else if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
987
988
                u->meta.inactive_enter_timestamp = ts;

Lennart Poettering's avatar
Lennart Poettering committed
989
        if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
990
                u->meta.active_enter_timestamp = ts;
Lennart Poettering's avatar
Lennart Poettering committed
991
        else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
992
                u->meta.active_exit_timestamp = ts;
Lennart Poettering's avatar
Lennart Poettering committed
993

994
995
996
        if (UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
                cgroup_bonding_trim_list(u->meta.cgroup_bondings, true);

997
        timer_unit_notify(u, ns);
998
        path_unit_notify(u, ns);
999

Lennart Poettering's avatar
Lennart Poettering committed
1000
        if (u->meta.job) {
1001
                unexpected = false;
Lennart Poettering's avatar
Lennart Poettering committed
1002
1003
1004
1005
1006
1007

                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. */
1008
                        job_add_to_run_queue(u->meta.job);
Lennart Poettering's avatar
Lennart Poettering committed
1009

1010
1011
1012
                /* Let's check whether this state change constitutes a
                 * finished job, or maybe cotradicts a running job and
                 * hence needs to invalidate jobs. */
Lennart Poettering's avatar
Lennart Poettering committed
1013

1014
                switch (u->meta.job->type) {
Lennart Poettering's avatar
Lennart Poettering committed
1015

1016
1017
                case JOB_START:
                case JOB_VERIFY_ACTIVE:
Lennart Poettering's avatar
Lennart Poettering committed
1018

1019
1020
1021
1022
1023
1024
                        if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
                                job_finish_and_invalidate(u->meta.job, true);
                        else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
                                unexpected = true;
                                job_finish_and_invalidate(u->meta.job, false);
                        }
Lennart Poettering's avatar
Lennart Poettering committed
1025

1026
                        break;
Lennart Poettering's avatar
Lennart Poettering committed
1027

1028
1029
                case JOB_RELOAD:
                case JOB_RELOAD_OR_START:
Lennart Poettering's avatar
Lennart Poettering committed
1030

1031
                        if (u->meta.job->state == JOB_RUNNING) {
1032
                                if (ns == UNIT_ACTIVE)
Lennart Poettering's avatar
Lennart Poettering committed
1033
                                        job_finish_and_invalidate(u->meta.job, true);
1034
                                else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) {
1035
                                        unexpected = true;
Lennart Poettering's avatar
Lennart Poettering committed
1036
                                        job_finish_and_invalidate(u->meta.job, false);
1037
                                }
1038
                        }
Lennart Poettering's avatar
Lennart Poettering committed
1039

1040
                        break;
Lennart Poettering's avatar
Lennart Poettering committed
1041

1042
1043
1044
                case JOB_STOP:
                case JOB_RESTART:
                case JOB_TRY_RESTART:
Lennart Poettering's avatar
Lennart Poettering committed
1045

1046
                        if (ns == UNIT_INACTIVE || ns == UNIT_MAINTENANCE)
1047
1048
1049
1050
1051
                                job_finish_and_invalidate(u->meta.job, true);
                        else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
                                unexpected = true;
                                job_finish_and_invalidate(u->meta.job, false);
                        }
Lennart Poettering's avatar
Lennart Poettering committed
1052

1053
                        break;
Lennart Poettering's avatar
Lennart Poettering committed
1054

1055
1056
                default:
                        assert_not_reached("Job type unknown");
Lennart Poettering's avatar
Lennart Poettering committed
1057
1058
                }

1059
1060
1061
        } else
                unexpected = true;

1062
1063
1064
1065
1066
1067
1068
        /* If this state change happened without being requested by a
         * job, then let's retroactively start or stop
         * dependencies. We skip that step when deserializing, since
         * we don't want to create any additional jobs just because
         * something is already activated. */

        if (unexpected && u->meta.manager->n_deserializing <= 0) {
1069
1070
1071
1072
                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);
1073
        }
1074

1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
        if (ns != os && ns == UNIT_MAINTENANCE) {
                Iterator i;
                Unit *other;

                SET_FOREACH(other, u->meta.dependencies[UNIT_ON_FAILURE], i)
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);

                log_notice("Unit %s entered maintenance state.", u->meta.id);
        }

1085
1086
        /* Some names are special */
        if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1087
                if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
1088
1089
1090
                        /* The bus just might have become available,
                         * hence try to connect to it, if we aren't
                         * yet connected. */
1091
                        bus_init(u->meta.manager);
1092
1093
                }

1094
1095
1096
1097
                if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
                        /* The syslog daemon just might have become
                         * available, hence try to connect to it, if
                         * we aren't yet connected. */
1098
                        log_open();
1099

1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
                if (u->meta.type == UNIT_MOUNT)
                        /* Another directory became available, let's
                         * check if that is enough to write our utmp
                         * entry. */
                        manager_write_utmp_reboot(u->meta.manager);

                if (u->meta.type == UNIT_TARGET)
                        /* A target got activated, maybe this is a runlevel? */
                        manager_write_utmp_runlevel(u->meta.manager, u);

        } else if (!UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1111
1112

                if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1113
1114
1115
                        /* The syslog daemon might just have
                         * terminated, hence try to disconnect from
                         * it. */
1116
                        log_close_syslog();
1117
1118
1119
1120
1121

                /* We don't care about D-Bus here, since we'll get an
                 * asynchronous notification for it anyway. */
        }

1122
1123
1124
        /* Maybe we finished startup and are now ready for being
         * stopped because unneeded? */
        unit_check_uneeded(u);
1125
1126

        unit_add_to_dbus_queue(u);
1127
        unit_add_to_gc_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
1128
1129
}

1130
int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
1131
1132
1133
1134
        struct epoll_event ev;

        assert(u);
        assert(fd >= 0);
1135
        assert(w);
1136
        assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
Lennart Poettering's avatar
Lennart Poettering committed
1137
1138

        zero(ev);
1139
        ev.data.ptr = w;
Lennart Poettering's avatar
Lennart Poettering committed
1140
1141
        ev.events = events;

1142
1143
1144
1145
1146
        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
1147

1148
1149
        w->fd = fd;
        w->type = WATCH_FD;
1150
        w->data.unit = u;
Lennart Poettering's avatar
Lennart Poettering committed
1151

1152
        return 0;
Lennart Poettering's avatar
Lennart Poettering committed
1153
1154
}

1155
void unit_unwatch_fd(Unit *u, Watch *w) {
Lennart Poettering's avatar
Lennart Poettering committed
1156
        assert(u);
1157
        assert(w);
Lennart Poettering's avatar
Lennart Poettering committed
1158

1159
1160
1161
        if (w->type == WATCH_INVALID)
                return;

1162
1163
        assert(w->type == WATCH_FD);
        assert(w->data.unit == u);
1164
1165
1166
1167
        assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);

        w->fd = -1;
        w->type = WATCH_INVALID;
1168
        w->data.unit = NULL;
Lennart Poettering's avatar
Lennart Poettering committed
1169
1170
1171
1172
1173
1174
}

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

1175
1176
1177
        /* Watch a specific PID. We only support one unit watching
         * each PID for now. */

1178
        return hashmap_put(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
Lennart Poettering's avatar
Lennart Poettering committed
1179
1180
1181
1182
1183
1184
}

void unit_unwatch_pid(Unit *u,