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

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"
44
#include "missing.h"
Lennart Poettering's avatar
Lennart Poettering committed
45
46
47
48
49
50
51
52
53

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

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

        return u;
}

80
81
82
83
84
85
86
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
87
88
int unit_add_name(Unit *u, const char *text) {
        UnitType t;
89
        char *s, *i = NULL;
Lennart Poettering's avatar
Lennart Poettering committed
90
91
92
93
94
        int r;

        assert(u);
        assert(text);

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

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

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

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

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

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

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

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

124
125
126
        /* Ensure that this unit is either instanced or not instanced,
         * but not both. */
        if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) {
127
128
129
130
131
132
133
134
135
136
137
                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;
        }

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

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

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

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

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

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

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

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

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

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

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

        assert(u);
        assert(name);

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

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

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

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

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

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

210
        unit_add_to_dbus_queue(u);
211

212
213
214
        return 0;
}

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

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

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

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

236
237
238
        if (UNIT_VTABLE(u)->no_gc)
                return true;

239
240
241
        if (u->meta.no_gc)
                return true;

242
243
244
245
246
247
248
249
250
251
252
253
254
        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
255
256
void unit_add_to_load_queue(Unit *u) {
        assert(u);
257
        assert(u->meta.type != _UNIT_TYPE_INVALID);
Lennart Poettering's avatar
Lennart Poettering committed
258
259
260
261
262
263
264
265

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

266
267
268
269
270
271
272
273
274
275
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;
}

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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);
}

294
295
void unit_add_to_dbus_queue(Unit *u) {
        assert(u);
296
        assert(u->meta.type != _UNIT_TYPE_INVALID);
297

298
        if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue)
299
300
                return;

301
302
        /* Shortcut things if nobody cares */
        if (!bus_has_subscriber(u->meta.manager)) {
303
304
305
306
                u->meta.sent_dbus_new_signal = true;
                return;
        }

307
308
309
310
        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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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);
325
326

                unit_add_to_gc_queue(other);
Lennart Poettering's avatar
Lennart Poettering committed
327
328
329
330
331
332
333
334
335
336
337
338
        }

        set_free(s);
}

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

        assert(u);

339
340
        bus_unit_send_removed_signal(u);

341
342
343
344
        if (u->meta.load_state != UNIT_STUB)
                if (UNIT_VTABLE(u)->done)
                        UNIT_VTABLE(u)->done(u);

Lennart Poettering's avatar
Lennart Poettering committed
345
346
347
        SET_FOREACH(t, u->meta.names, i)
                hashmap_remove_value(u->meta.manager->units, t, u);

348
349
350
351
352
353
        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
354
355
356
        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
357
358
359
        if (u->meta.in_load_queue)
                LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);

360
361
362
        if (u->meta.in_dbus_queue)
                LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);

363
364
365
        if (u->meta.in_cleanup_queue)
                LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);

366
367
368
369
370
        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--;
        }

371
        cgroup_bonding_free_list(u->meta.cgroup_bondings);
Lennart Poettering's avatar
Lennart Poettering committed
372
373

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

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

378
        free(u->meta.instance);
Lennart Poettering's avatar
Lennart Poettering committed
379
380
381
382
383
384
        free(u);
}

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

385
386
387
388
389
        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
390
         * shortcut failed loading to UNIT_INACTIVE_FAILED. */
Lennart Poettering's avatar
Lennart Poettering committed
391
392
393
394

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

395
396
397
398
399
400
const char* unit_sub_state_to_string(Unit *u) {
        assert(u);

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

401
402
403
static void complete_move(Set **s, Set **other) {
        assert(s);
        assert(other);
Lennart Poettering's avatar
Lennart Poettering committed
404

405
406
        if (!*other)
                return;
Lennart Poettering's avatar
Lennart Poettering committed
407
408

        if (*s)
409
410
411
412
413
414
                set_move(*s, *other);
        else {
                *s = *other;
                *other = NULL;
        }
}
Lennart Poettering's avatar
Lennart Poettering committed
415

416
417
418
static void merge_names(Unit *u, Unit *other) {
        char *t;
        Iterator i;
Lennart Poettering's avatar
Lennart Poettering committed
419

420
421
422
423
424
        assert(u);
        assert(other);

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

425
        set_free_free(other->meta.names);
426
427
428
429
430
        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
431
432
}

433
434
435
static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
        Iterator i;
        Unit *back;
Lennart Poettering's avatar
Lennart Poettering committed
436
        int r;
437
438
439
440
441

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

442
        /* Fix backwards pointers */
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
        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
463
464
465
466
467
        UnitDependency d;

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

470
471
        other = unit_follow_merge(other);

472
473
474
        if (other == u)
                return 0;

475
476
477
        if (u->meta.type != other->meta.type)
                return -EINVAL;

478
        if (!u->meta.instance != !other->meta.instance)
Lennart Poettering's avatar
Lennart Poettering committed
479
480
                return -EINVAL;

481
        if (other->meta.load_state != UNIT_STUB &&
482
            other->meta.load_state != UNIT_ERROR)
483
                return -EEXIST;
Lennart Poettering's avatar
Lennart Poettering committed
484

485
486
487
        if (other->meta.job)
                return -EEXIST;

488
        if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other)))
489
490
                return -EEXIST;

Lennart Poettering's avatar
Lennart Poettering committed
491
        /* Merge names */
492
        merge_names(u, other);
Lennart Poettering's avatar
Lennart Poettering committed
493
494
495

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

498
499
500
        other->meta.load_state = UNIT_MERGED;
        other->meta.merged_into = u;

501
502
503
504
505
506
507
        /* 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);
508
509
510
511
512
513
514
        unit_add_to_cleanup_queue(other);

        return 0;
}

int unit_merge_by_name(Unit *u, const char *name) {
        Unit *other;
515
516
        int r;
        char *s = NULL;
517
518
519
520

        assert(u);
        assert(name);

521
522
523
524
525
526
527
528
529
530
        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;
        }

531
        if (!(other = manager_get_unit(u->meta.manager, name)))
532
533
534
                r = unit_add_name(u, name);
        else
                r = unit_merge(u, other);
535

536
537
        free(s);
        return r;
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
}

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

555
556
557
558
        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)
559
560
561
562
563
                return 0;

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

564
        if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
565
566
                return r;

567
        if (u->meta.manager->running_as == MANAGER_SYSTEM)
568
                if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
569
570
                        return r;

Lennart Poettering's avatar
Lennart Poettering committed
571
572
573
574
575
576
577
578
579
        return 0;
}

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

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

580
        return strna(u->meta.id);
Lennart Poettering's avatar
Lennart Poettering committed
581
582
583
584
585
586
}

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

        assert(u);
599
        assert(u->meta.type >= 0);
Lennart Poettering's avatar
Lennart Poettering committed
600
601
602

        if (!prefix)
                prefix = "";
603
604
        p2 = strappend(prefix, "\t");
        prefix2 = p2 ? p2 : prefix;
Lennart Poettering's avatar
Lennart Poettering committed
605
606

        fprintf(f,
607
                "%s-> Unit %s:\n"
Lennart Poettering's avatar
Lennart Poettering committed
608
                "%s\tDescription: %s\n"
609
                "%s\tInstance: %s\n"
Lennart Poettering's avatar
Lennart Poettering committed
610
                "%s\tUnit Load State: %s\n"
611
                "%s\tUnit Active State: %s\n"
612
                "%s\tInactive Exit Timestamp: %s\n"
613
                "%s\tActive Enter Timestamp: %s\n"
614
                "%s\tActive Exit Timestamp: %s\n"
615
                "%s\tInactive Enter Timestamp: %s\n"
616
617
                "%s\tGC Check Good: %s\n"
                "%s\tNeed Daemon Reload: %s\n",
618
                prefix, u->meta.id,
Lennart Poettering's avatar
Lennart Poettering committed
619
                prefix, unit_description(u),
620
                prefix, strna(u->meta.instance),
621
                prefix, unit_load_state_to_string(u->meta.load_state),
622
                prefix, unit_active_state_to_string(unit_active_state(u)),
623
624
625
626
                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)),
627
628
                prefix, yes_no(unit_check_gc(u)),
                prefix, yes_no(unit_need_daemon_reload(u)));
629

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

633
634
        if ((following = unit_following(u)))
                fprintf(f, "%s\tFollowing: %s\n", prefix, following->meta.id);
635

636
637
638
        if (u->meta.fragment_path)
                fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);

639
640
641
        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
642
643
644
645
        for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
                Unit *other;

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

649
        if (u->meta.load_state == UNIT_LOADED) {
650
651
                fprintf(f,
                        "%s\tRecursive Stop: %s\n"
652
                        "%s\tStopWhenUnneeded: %s\n"
653
654
                        "%s\tRefuseManualStart: %s\n"
                        "%s\tRefuseManualStop: %s\n"
655
656
                        "%s\tDefaultDependencies: %s\n"
                        "%s\tIgnoreDependencyFailure: %s\n",
657
                        prefix, yes_no(u->meta.recursive_stop),
658
                        prefix, yes_no(u->meta.stop_when_unneeded),
659
660
                        prefix, yes_no(u->meta.refuse_manual_start),
                        prefix, yes_no(u->meta.refuse_manual_stop),
661
662
                        prefix, yes_no(u->meta.default_dependencies),
                        prefix, yes_no(u->meta.ignore_dependency_failure));
663

664
665
666
                LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
                        fprintf(f, "%s\tControlGroup: %s:%s\n",
                                prefix, b->controller, b->path);
667

668
669
                if (UNIT_VTABLE(u)->dump)
                        UNIT_VTABLE(u)->dump(u, f, prefix2);
670
671
672
673

        } else if (u->meta.load_state == UNIT_MERGED)
                fprintf(f,
                        "%s\tMerged into: %s\n",
674
                        prefix, u->meta.merged_into->meta.id);
675
        else if (u->meta.load_state == UNIT_ERROR)
676
677
                fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror(-u->meta.load_error));

Lennart Poettering's avatar
Lennart Poettering committed
678
679
680
681

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

682
        free(p2);
Lennart Poettering's avatar
Lennart Poettering committed
683
684
685
}

/* Common implementation for multiple backends */
686
int unit_load_fragment_and_dropin(Unit *u) {
687
688
689
690
691
        int r;

        assert(u);

        /* Load a .service file */
692
        if ((r = unit_load_fragment(u)) < 0)
693
694
                return r;

695
        if (u->meta.load_state == UNIT_STUB)
696
697
698
699
700
701
702
703
704
705
                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 */
706
int unit_load_fragment_and_dropin_optional(Unit *u) {
707
        int r;
Lennart Poettering's avatar
Lennart Poettering committed
708
709
710

        assert(u);

711
712
713
714
        /* Same as unit_load_fragment_and_dropin(), but whether
         * something can be loaded or not doesn't matter. */

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

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

Lennart Poettering's avatar
Lennart Poettering committed
721
        /* Load drop-in directory data */
722
        if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
723
724
                return r;

725
        return 0;
Lennart Poettering's avatar
Lennart Poettering committed
726
727
}

728
int unit_add_default_target_dependency(Unit *u, Unit *target) {
729
730
731
732
733
734
        assert(u);
        assert(target);

        if (target->meta.type != UNIT_TARGET)
                return 0;

735
736
737
738
739
740
        /* Only add the dependency if boths units are loaded, so that
         * that loop check below is reliable */
        if (u->meta.load_state != UNIT_LOADED ||
            target->meta.load_state != UNIT_LOADED)
                return 0;

741
742
743
744
745
746
747
748
        /* Don't create loops */
        if (set_get(target->meta.dependencies[UNIT_BEFORE], u))
                return 0;

        return unit_add_dependency(target, UNIT_AFTER, u, true);
}

static int unit_add_default_dependencies(Unit *u) {
749
        Unit *target;
750
751
752
753
754
        Iterator i;
        int r;

        assert(u);

755
756
        SET_FOREACH(target, u->meta.dependencies[UNIT_REQUIRED_BY], i)
                if ((r = unit_add_default_target_dependency(u, target)) < 0)
757
758
                        return r;

759
760
        SET_FOREACH(target, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
                if ((r = unit_add_default_target_dependency(u, target)) < 0)
761
762
                        return r;

763
764
        SET_FOREACH(target, u->meta.dependencies[UNIT_WANTED_BY], i)
                if ((r = unit_add_default_target_dependency(u, target)) < 0)
765
766
767
768
769
                        return r;

        return 0;
}

Lennart Poettering's avatar
Lennart Poettering committed
770
771
772
773
774
775
776
777
778
779
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;
        }

780
781
782
        if (u->meta.type == _UNIT_TYPE_INVALID)
                return -EINVAL;

Lennart Poettering's avatar
Lennart Poettering committed
783
784
785
        if (u->meta.load_state != UNIT_STUB)
                return 0;

786
787
        if (UNIT_VTABLE(u)->load)
                if ((r = UNIT_VTABLE(u)->load(u)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
788
                        goto fail;
789

790
        if (u->meta.load_state == UNIT_STUB) {
791
792
793
794
                r = -ENOENT;
                goto fail;
        }

795
796
797
798
799
        if (u->meta.load_state == UNIT_LOADED &&
            u->meta.default_dependencies)
                if ((r = unit_add_default_dependencies(u)) < 0)
                        goto fail;

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

        unit_add_to_dbus_queue(unit_follow_merge(u));
803
        unit_add_to_gc_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
804
805
806
807

        return 0;

fail:
808
        u->meta.load_state = UNIT_ERROR;
809
        u->meta.load_error = r;
810
        unit_add_to_dbus_queue(u);
811

812
        log_debug("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
813

Lennart Poettering's avatar
Lennart Poettering committed
814
815
816
817
        return r;
}

/* Errors:
818
819
820
821
 *         -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
822
823
824
825
826
827
 */
int unit_start(Unit *u) {
        UnitActiveState state;

        assert(u);

828
829
830
        if (u->meta.load_state != UNIT_LOADED)
                return -EINVAL;

831
832
833
834
        /* 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
835
836
837
838
        state = unit_active_state(u);
        if (UNIT_IS_ACTIVE_OR_RELOADING(state))
                return -EALREADY;

839
840
841
842
        /* 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
843
844
845
846
847
848
        /* 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. */

849
        unit_add_to_dbus_queue(u);
850
851
852

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

Lennart Poettering's avatar
Lennart Poettering committed
853
854
855
856
857
858
859
860
861
        return UNIT_VTABLE(u)->start(u);
}

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

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

862
863
864
865
866
867
868
bool unit_can_isolate(Unit *u) {
        assert(u);

        return unit_can_start(u) &&
                u->meta.allow_isolate;
}

Lennart Poettering's avatar
Lennart Poettering committed
869
870
871
872
873
874
875
876
877
878
879
/* 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);
880
        if (UNIT_IS_INACTIVE_OR_FAILED(state))
Lennart Poettering's avatar
Lennart Poettering committed
881
882
                return -EALREADY;

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

886
        unit_add_to_dbus_queue(u);
887
888
889

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

Lennart Poettering's avatar
Lennart Poettering committed
890
891
892
893
894
895
896
897
898
899
900
901
902
        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);

903
904
905
        if (u->meta.load_state != UNIT_LOADED)
                return -EINVAL;

Lennart Poettering's avatar
Lennart Poettering committed
906
907
908
909
        if (!unit_can_reload(u))
                return -EBADR;

        state = unit_active_state(u);
910
        if (state == UNIT_RELOADING)
Lennart Poettering's avatar
Lennart Poettering committed
911
912
                return -EALREADY;

913
        if (state != UNIT_ACTIVE)
Lennart Poettering's avatar
Lennart Poettering committed
914
915
                return -ENOEXEC;

916
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
        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);
}

932
static void unit_check_unneeded(Unit *u) {
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
        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;

951
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
952
953
954
955
956
957
958
                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;

959
        log_info("Service %s is not needed anymore. Stopping.", u->meta.id);
960
961

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

Lennart Poettering's avatar
Lennart Poettering committed
965
966
967
968
969
970
971
972
973
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)))
974
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
Lennart Poettering's avatar
Lennart Poettering committed
975

976
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
Lennart Poettering's avatar
Lennart Poettering committed
977
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
978
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
Lennart Poettering's avatar
Lennart Poettering committed
979
980
981

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

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

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

        SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i)
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                        manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
Lennart Poettering's avatar
Lennart Poettering committed
995
996
997
998
999
1000
}

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