unit.c 78.9 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"
45
#include "cgroup-attr.h"
Lennart Poettering's avatar
Lennart Poettering committed
46
47
48
49
50
51
52
53
54

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
55
        [UNIT_SNAPSHOT] = &snapshot_vtable,
56
57
        [UNIT_SWAP] = &swap_vtable,
        [UNIT_PATH] = &path_vtable
Lennart Poettering's avatar
Lennart Poettering committed
58
59
};

60
Unit *unit_new(Manager *m, size_t size) {
Lennart Poettering's avatar
Lennart Poettering committed
61
62
63
        Unit *u;

        assert(m);
64
        assert(size >= sizeof(Meta));
Lennart Poettering's avatar
Lennart Poettering committed
65

66
67
        u = malloc0(size);
        if (!u)
Lennart Poettering's avatar
Lennart Poettering committed
68
69
                return NULL;

70
71
        u->meta.names = set_new(string_hash_func, string_compare_func);
        if (!u->meta.names) {
Lennart Poettering's avatar
Lennart Poettering committed
72
73
74
75
76
77
                free(u);
                return NULL;
        }

        u->meta.manager = m;
        u->meta.type = _UNIT_TYPE_INVALID;
78
        u->meta.deserialized_job = _JOB_TYPE_INVALID;
79
        u->meta.default_dependencies = true;
80
        u->meta.unit_file_state = _UNIT_FILE_STATE_INVALID;
Lennart Poettering's avatar
Lennart Poettering committed
81
82
83
84

        return u;
}

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

        assert(u);
        assert(text);

100
101
102
        if (unit_name_is_template(text)) {
                if (!u->meta.instance)
                        return -EINVAL;
Lennart Poettering's avatar
Lennart Poettering committed
103

104
105
106
                s = unit_name_replace_instance(text, u->meta.instance);
        } else
                s = strdup(text);
Lennart Poettering's avatar
Lennart Poettering committed
107

108
109
        if (!s)
                return -ENOMEM;
Lennart Poettering's avatar
Lennart Poettering committed
110

111
        if (!unit_name_is_valid(s, false)) {
112
113
114
                r = -EINVAL;
                goto fail;
        }
115

116
        assert_se((t = unit_name_to_type(s)) >= 0);
Lennart Poettering's avatar
Lennart Poettering committed
117

118
119
120
121
        if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) {
                r = -EINVAL;
                goto fail;
        }
Lennart Poettering's avatar
Lennart Poettering committed
122

123
124
        if ((r = unit_name_to_instance(s, &i)) < 0)
                goto fail;
Lennart Poettering's avatar
Lennart Poettering committed
125

126
127
        if (i && unit_vtable[t]->no_instances) {
                r = -EINVAL;
128
                goto fail;
129
        }
130

131
132
133
        /* Ensure that this unit is either instanced or not instanced,
         * but not both. */
        if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) {
134
135
136
137
138
139
140
141
142
143
144
                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;
        }

145
146
147
148
149
        if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
                r = -E2BIG;
                goto fail;
        }

150
151
152
153
        if ((r = set_put(u->meta.names, s)) < 0) {
                if (r == -EEXIST)
                        r = 0;
                goto fail;
Lennart Poettering's avatar
Lennart Poettering committed
154
155
156
157
        }

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

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

163
                u->meta.type = t;
164
165
166
                u->meta.id = s;
                u->meta.instance = i;

Lennart Poettering's avatar
Lennart Poettering committed
167
                LIST_PREPEND(Meta, units_by_type, u->meta.manager->units_by_type[t], &u->meta);
168
169
170

                if (UNIT_VTABLE(u)->init)
                        UNIT_VTABLE(u)->init(u);
171
172
        } else
                free(i);
Lennart Poettering's avatar
Lennart Poettering committed
173

174
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
175
        return 0;
176
177
178
179
180
181

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

        return r;
Lennart Poettering's avatar
Lennart Poettering committed
182
183
}

184
int unit_choose_id(Unit *u, const char *name) {
185
186
        char *s, *t = NULL, *i;
        int r;
187
188
189
190

        assert(u);
        assert(name);

191
192
193
194
195
196
197
198
199
200
201
        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;
        }

202
        /* Selects one of the names of this unit as the id */
203
204
        s = set_get(u->meta.names, (char*) name);
        free(t);
205

206
        if (!s)
207
208
                return -ENOENT;

209
210
211
        if ((r = unit_name_to_instance(s, &i)) < 0)
                return r;

212
        u->meta.id = s;
213
214
215
216

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

217
        unit_add_to_dbus_queue(u);
218

219
220
221
        return 0;
}

Lennart Poettering's avatar
Lennart Poettering committed
222
223
224
225
226
227
228
229
230
231
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;
232
233

        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
234
235
236
        return 0;
}

237
238
239
bool unit_check_gc(Unit *u) {
        assert(u);

240
241
242
        if (u->meta.load_state == UNIT_STUB)
                return true;

243
244
245
        if (UNIT_VTABLE(u)->no_gc)
                return true;

246
247
248
        if (u->meta.no_gc)
                return true;

249
250
251
252
253
254
255
256
257
258
259
260
261
        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
262
263
void unit_add_to_load_queue(Unit *u) {
        assert(u);
264
        assert(u->meta.type != _UNIT_TYPE_INVALID);
Lennart Poettering's avatar
Lennart Poettering committed
265
266
267
268
269
270
271
272

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

273
274
275
276
277
278
279
280
281
282
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;
}

283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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);
}

301
302
void unit_add_to_dbus_queue(Unit *u) {
        assert(u);
303
        assert(u->meta.type != _UNIT_TYPE_INVALID);
304

305
        if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue)
306
307
                return;

308
309
        /* Shortcut things if nobody cares */
        if (!bus_has_subscriber(u->meta.manager)) {
310
311
312
313
                u->meta.sent_dbus_new_signal = true;
                return;
        }

314
315
316
317
        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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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);
332
333

                unit_add_to_gc_queue(other);
Lennart Poettering's avatar
Lennart Poettering committed
334
335
336
337
338
339
340
341
342
343
344
345
        }

        set_free(s);
}

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

        assert(u);

346
347
        bus_unit_send_removed_signal(u);

348
349
350
351
        if (u->meta.load_state != UNIT_STUB)
                if (UNIT_VTABLE(u)->done)
                        UNIT_VTABLE(u)->done(u);

Lennart Poettering's avatar
Lennart Poettering committed
352
353
354
        SET_FOREACH(t, u->meta.names, i)
                hashmap_remove_value(u->meta.manager->units, t, u);

355
356
357
358
359
360
        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
361
        if (u->meta.type != _UNIT_TYPE_INVALID)
Lennart Poettering's avatar
Lennart Poettering committed
362
                LIST_REMOVE(Meta, units_by_type, u->meta.manager->units_by_type[u->meta.type], &u->meta);
Lennart Poettering's avatar
Lennart Poettering committed
363

Lennart Poettering's avatar
Lennart Poettering committed
364
365
366
        if (u->meta.in_load_queue)
                LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);

367
368
369
        if (u->meta.in_dbus_queue)
                LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);

370
371
372
        if (u->meta.in_cleanup_queue)
                LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);

373
374
375
376
377
        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--;
        }

378
        cgroup_bonding_free_list(u->meta.cgroup_bondings, u->meta.manager->n_reloading <= 0);
379
        cgroup_attribute_free_list(u->meta.cgroup_attributes);
Lennart Poettering's avatar
Lennart Poettering committed
380
381

        free(u->meta.description);
382
        free(u->meta.fragment_path);
383
        free(u->meta.instance);
Lennart Poettering's avatar
Lennart Poettering committed
384

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

387
388
        condition_free_list(u->meta.conditions);

389
390
391
        while (u->meta.refs)
                unit_ref_unset(u->meta.refs);

Lennart Poettering's avatar
Lennart Poettering committed
392
393
394
395
396
397
        free(u);
}

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

398
399
400
401
402
        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
403
         * shortcut failed loading to UNIT_INACTIVE_FAILED. */
Lennart Poettering's avatar
Lennart Poettering committed
404
405
406
407

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

408
409
410
411
412
413
const char* unit_sub_state_to_string(Unit *u) {
        assert(u);

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

414
415
416
static void complete_move(Set **s, Set **other) {
        assert(s);
        assert(other);
Lennart Poettering's avatar
Lennart Poettering committed
417

418
419
        if (!*other)
                return;
Lennart Poettering's avatar
Lennart Poettering committed
420
421

        if (*s)
422
423
424
425
426
427
                set_move(*s, *other);
        else {
                *s = *other;
                *other = NULL;
        }
}
Lennart Poettering's avatar
Lennart Poettering committed
428

429
430
431
static void merge_names(Unit *u, Unit *other) {
        char *t;
        Iterator i;
Lennart Poettering's avatar
Lennart Poettering committed
432

433
434
435
436
437
        assert(u);
        assert(other);

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

438
        set_free_free(other->meta.names);
439
440
441
442
443
        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
444
445
}

446
447
448
static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
        Iterator i;
        Unit *back;
Lennart Poettering's avatar
Lennart Poettering committed
449
        int r;
450
451
452
453
454

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

455
        /* Fix backwards pointers */
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
        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
476
477
478
479
480
        UnitDependency d;

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

483
484
        other = unit_follow_merge(other);

485
486
487
        if (other == u)
                return 0;

488
489
490
        if (u->meta.type != other->meta.type)
                return -EINVAL;

491
        if (!u->meta.instance != !other->meta.instance)
Lennart Poettering's avatar
Lennart Poettering committed
492
493
                return -EINVAL;

494
        if (other->meta.load_state != UNIT_STUB &&
495
            other->meta.load_state != UNIT_ERROR)
496
                return -EEXIST;
Lennart Poettering's avatar
Lennart Poettering committed
497

498
499
500
        if (other->meta.job)
                return -EEXIST;

501
        if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other)))
502
503
                return -EEXIST;

Lennart Poettering's avatar
Lennart Poettering committed
504
        /* Merge names */
505
        merge_names(u, other);
Lennart Poettering's avatar
Lennart Poettering committed
506

507
508
509
510
        /* Redirect all references */
        while (other->meta.refs)
                unit_ref_set(other->meta.refs, u);

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

515
516
517
        other->meta.load_state = UNIT_MERGED;
        other->meta.merged_into = u;

518
519
520
521
522
523
524
        /* 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);
525
526
527
528
529
530
531
        unit_add_to_cleanup_queue(other);

        return 0;
}

int unit_merge_by_name(Unit *u, const char *name) {
        Unit *other;
532
533
        int r;
        char *s = NULL;
534
535
536
537

        assert(u);
        assert(name);

538
539
540
541
542
543
544
545
546
547
        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;
        }

548
        if (!(other = manager_get_unit(u->meta.manager, name)))
549
550
551
                r = unit_add_name(u, name);
        else
                r = unit_merge(u, other);
552

553
554
        free(s);
        return r;
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
}

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

572
573
        if (c->std_output != EXEC_OUTPUT_KMSG &&
            c->std_output != EXEC_OUTPUT_SYSLOG &&
574
            c->std_output != EXEC_OUTPUT_JOURNAL &&
575
576
            c->std_output != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
            c->std_output != EXEC_OUTPUT_SYSLOG_AND_CONSOLE &&
577
            c->std_output != EXEC_OUTPUT_JOURNAL_AND_CONSOLE &&
578
            c->std_error != EXEC_OUTPUT_KMSG &&
579
            c->std_error != EXEC_OUTPUT_SYSLOG &&
580
            c->std_error != EXEC_OUTPUT_JOURNAL &&
581
            c->std_error != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
582
            c->std_error != EXEC_OUTPUT_JOURNAL_AND_CONSOLE &&
583
            c->std_error != EXEC_OUTPUT_SYSLOG_AND_CONSOLE)
584
585
586
587
588
                return 0;

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

589
        if (u->meta.manager->running_as == MANAGER_SYSTEM)
590
                if ((r = unit_add_two_dependencies_by_name(u, UNIT_REQUIRES, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, NULL, true)) < 0)
591
592
                        return r;

Lennart Poettering's avatar
Lennart Poettering committed
593
594
595
596
597
598
599
600
601
        return 0;
}

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

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

602
        return strna(u->meta.id);
Lennart Poettering's avatar
Lennart Poettering committed
603
604
605
606
607
608
}

void unit_dump(Unit *u, FILE *f, const char *prefix) {
        char *t;
        UnitDependency d;
        Iterator i;
609
610
        char *p2;
        const char *prefix2;
611
612
613
614
        char
                timestamp1[FORMAT_TIMESTAMP_MAX],
                timestamp2[FORMAT_TIMESTAMP_MAX],
                timestamp3[FORMAT_TIMESTAMP_MAX],
615
616
                timestamp4[FORMAT_TIMESTAMP_MAX],
                timespan[FORMAT_TIMESPAN_MAX];
617
        Unit *following;
Lennart Poettering's avatar
Lennart Poettering committed
618
619

        assert(u);
620
        assert(u->meta.type >= 0);
Lennart Poettering's avatar
Lennart Poettering committed
621
622
623

        if (!prefix)
                prefix = "";
624
625
        p2 = strappend(prefix, "\t");
        prefix2 = p2 ? p2 : prefix;
Lennart Poettering's avatar
Lennart Poettering committed
626
627

        fprintf(f,
628
                "%s-> Unit %s:\n"
Lennart Poettering's avatar
Lennart Poettering committed
629
                "%s\tDescription: %s\n"
630
                "%s\tInstance: %s\n"
Lennart Poettering's avatar
Lennart Poettering committed
631
                "%s\tUnit Load State: %s\n"
632
                "%s\tUnit Active State: %s\n"
633
                "%s\tInactive Exit Timestamp: %s\n"
634
                "%s\tActive Enter Timestamp: %s\n"
635
                "%s\tActive Exit Timestamp: %s\n"
636
                "%s\tInactive Enter Timestamp: %s\n"
637
638
                "%s\tGC Check Good: %s\n"
                "%s\tNeed Daemon Reload: %s\n",
639
                prefix, u->meta.id,
Lennart Poettering's avatar
Lennart Poettering committed
640
                prefix, unit_description(u),
641
                prefix, strna(u->meta.instance),
642
                prefix, unit_load_state_to_string(u->meta.load_state),
643
                prefix, unit_active_state_to_string(unit_active_state(u)),
644
645
646
647
                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)),
648
649
                prefix, yes_no(unit_check_gc(u)),
                prefix, yes_no(unit_need_daemon_reload(u)));
650

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

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

657
658
659
        if (u->meta.fragment_path)
                fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);

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

663
664
        condition_dump_list(u->meta.conditions, f, prefix);

665
666
667
668
669
670
671
        if (dual_timestamp_is_set(&u->meta.condition_timestamp))
                fprintf(f,
                        "%s\tCondition Timestamp: %s\n"
                        "%s\tCondition Result: %s\n",
                        prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.condition_timestamp.realtime)),
                        prefix, yes_no(u->meta.condition_result));

Lennart Poettering's avatar
Lennart Poettering committed
672
673
674
675
        for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
                Unit *other;

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

679
        if (u->meta.load_state == UNIT_LOADED) {
680
681
682
                CGroupBonding *b;
                CGroupAttribute *a;

683
                fprintf(f,
684
                        "%s\tStopWhenUnneeded: %s\n"
685
686
                        "%s\tRefuseManualStart: %s\n"
                        "%s\tRefuseManualStop: %s\n"
687
                        "%s\tDefaultDependencies: %s\n"
688
                        "%s\tOnFailureIsolate: %s\n"
689
690
                        "%s\tIgnoreOnIsolate: %s\n"
                        "%s\tIgnoreOnSnapshot: %s\n",
691
                        prefix, yes_no(u->meta.stop_when_unneeded),
692
693
                        prefix, yes_no(u->meta.refuse_manual_start),
                        prefix, yes_no(u->meta.refuse_manual_stop),
694
                        prefix, yes_no(u->meta.default_dependencies),
695
                        prefix, yes_no(u->meta.on_failure_isolate),
696
697
                        prefix, yes_no(u->meta.ignore_on_isolate),
                        prefix, yes_no(u->meta.ignore_on_snapshot));
698

699
700
701
                LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
                        fprintf(f, "%s\tControlGroup: %s:%s\n",
                                prefix, b->controller, b->path);
702

703
704
705
706
707
708
                LIST_FOREACH(by_unit, a, u->meta.cgroup_attributes) {
                        char *v = NULL;

                        if (a->map_callback)
                                a->map_callback(a->controller, a->name, a->value, &v);

709
                        fprintf(f, "%s\tControlGroupAttribute: %s %s \"%s\"\n",
710
711
712
713
                                prefix, a->controller, a->name, v ? v : a->value);

                        free(v);
                }
714

715
716
                if (UNIT_VTABLE(u)->dump)
                        UNIT_VTABLE(u)->dump(u, f, prefix2);
717
718
719
720

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

Lennart Poettering's avatar
Lennart Poettering committed
725
726
727
728

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

729
        free(p2);
Lennart Poettering's avatar
Lennart Poettering committed
730
731
732
}

/* Common implementation for multiple backends */
733
int unit_load_fragment_and_dropin(Unit *u) {
734
735
736
737
738
        int r;

        assert(u);

        /* Load a .service file */
739
        if ((r = unit_load_fragment(u)) < 0)
740
741
                return r;

742
        if (u->meta.load_state == UNIT_STUB)
743
744
745
746
747
748
749
750
751
752
                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 */
753
int unit_load_fragment_and_dropin_optional(Unit *u) {
754
        int r;
Lennart Poettering's avatar
Lennart Poettering committed
755
756
757

        assert(u);

758
759
760
761
        /* Same as unit_load_fragment_and_dropin(), but whether
         * something can be loaded or not doesn't matter. */

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

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

Lennart Poettering's avatar
Lennart Poettering committed
768
        /* Load drop-in directory data */
769
        if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
770
771
                return r;

772
        return 0;
Lennart Poettering's avatar
Lennart Poettering committed
773
774
}

775
int unit_add_default_target_dependency(Unit *u, Unit *target) {
776
777
778
779
780
781
        assert(u);
        assert(target);

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

Harald Hoyer's avatar
Harald Hoyer committed
782
        /* Only add the dependency if both units are loaded, so that
783
784
785
786
787
         * that loop check below is reliable */
        if (u->meta.load_state != UNIT_LOADED ||
            target->meta.load_state != UNIT_LOADED)
                return 0;

788
789
790
        /* If either side wants no automatic dependencies, then let's
         * skip this */
        if (!u->meta.default_dependencies ||
791
            !target->meta.default_dependencies)
792
793
                return 0;

794
795
796
797
798
799
800
801
        /* 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) {
802
803
804
805
806
807
808
        static const UnitDependency deps[] = {
                UNIT_REQUIRED_BY,
                UNIT_REQUIRED_BY_OVERRIDABLE,
                UNIT_WANTED_BY,
                UNIT_BOUND_BY
        };

809
        Unit *target;
810
811
        Iterator i;
        int r;
812
        unsigned k;
813
814
815

        assert(u);

816
817
818
819
        for (k = 0; k < ELEMENTSOF(deps); k++)
                SET_FOREACH(target, u->meta.dependencies[deps[k]], i)
                        if ((r = unit_add_default_target_dependency(u, target)) < 0)
                                return r;
820

821
822
823
        return 0;
}

Lennart Poettering's avatar
Lennart Poettering committed
824
825
826
827
828
829
830
831
832
833
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;
        }

834
835
836
        if (u->meta.type == _UNIT_TYPE_INVALID)
                return -EINVAL;

Lennart Poettering's avatar
Lennart Poettering committed
837
838
839
        if (u->meta.load_state != UNIT_STUB)
                return 0;

840
841
        if (UNIT_VTABLE(u)->load)
                if ((r = UNIT_VTABLE(u)->load(u)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
842
                        goto fail;
843

844
        if (u->meta.load_state == UNIT_STUB) {
845
846
847
848
                r = -ENOENT;
                goto fail;
        }

849
850
851
852
853
        if (u->meta.load_state == UNIT_LOADED &&
            u->meta.default_dependencies)
                if ((r = unit_add_default_dependencies(u)) < 0)
                        goto fail;

854
855
856
857
858
859
860
861
862
863
        if (u->meta.on_failure_isolate &&
            set_size(u->meta.dependencies[UNIT_ON_FAILURE]) > 1) {

                log_error("More than one OnFailure= dependencies specified for %s but OnFailureIsolate= enabled. Refusing.",
                          u->meta.id);

                r = -EINVAL;
                goto fail;
        }

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

        unit_add_to_dbus_queue(unit_follow_merge(u));
867
        unit_add_to_gc_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
868
869
870
871

        return 0;

fail:
872
        u->meta.load_state = UNIT_ERROR;
873
        u->meta.load_error = r;
874
        unit_add_to_dbus_queue(u);
875
        unit_add_to_gc_queue(u);
876

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

Lennart Poettering's avatar
Lennart Poettering committed
879
880
881
        return r;
}

882
883
884
885
886
887
888
889
890
bool unit_condition_test(Unit *u) {
        assert(u);

        dual_timestamp_get(&u->meta.condition_timestamp);
        u->meta.condition_result = condition_test_list(u->meta.conditions);

        return u->meta.condition_result;
}

Lennart Poettering's avatar
Lennart Poettering committed
891
/* Errors:
892
893
894
895
 *         -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
896
897
898
 */
int unit_start(Unit *u) {
        UnitActiveState state;
899
        Unit *following;
Lennart Poettering's avatar
Lennart Poettering committed
900
901
902

        assert(u);

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

906
907
908
909
        /* If this is already 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
910
911
912
913
        state = unit_active_state(u);
        if (UNIT_IS_ACTIVE_OR_RELOADING(state))
                return -EALREADY;

914
915
916
917
918
919
        /* If the conditions failed, don't do anything at all. If we
         * already are activating this call might still be useful to
         * speed up activation in case there is some hold-off time,
         * but we don't want to recheck the condition in that case. */
        if (state != UNIT_ACTIVATING &&
            !unit_condition_test(u)) {
920
921
922
923
                log_debug("Starting of %s requested but condition failed. Ignoring.", u->meta.id);
                return -EALREADY;
        }

924
925
926
927
928
929
930
931
932
933
        /* Forward to the main object, if we aren't it. */
        if ((following = unit_following(u))) {
                log_debug("Redirecting start request from %s to %s.", u->meta.id, following->meta.id);
                return unit_start(following);
        }

        /* 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
934
935
936
937
938
939
        /* 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. */

940
        unit_add_to_dbus_queue(u);
941

942
        unit_status_printf(u, NULL, "Starting %s...", unit_description(u));
Lennart Poettering's avatar
Lennart Poettering committed
943
944
945
946
947
948
949
950
951
        return UNIT_VTABLE(u)->start(u);
}

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

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

952
953
954
955
956
957
958
bool unit_can_isolate(Unit *u) {
        assert(u);

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

Lennart Poettering's avatar
Lennart Poettering committed
959
960
961
962
963
964
965
/* 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;
966
        Unit *following;
Lennart Poettering's avatar
Lennart Poettering committed
967
968
969
970

        assert(u);

        state = unit_active_state(u);
971
        if (UNIT_IS_INACTIVE_OR_FAILED(state))
Lennart Poettering's avatar
Lennart Poettering committed
972
973
                return -EALREADY;

974
975
976
977
978
        if ((following = unit_following(u))) {
                log_debug("Redirecting stop request from %s to %s.", u->meta.id, following->meta.id);
                return unit_stop(following);
        }

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

982
        unit_add_to_dbus_queue(u);
983

984
        unit_status_printf(u, NULL, "Stopping %s...", unit_description(u));
Lennart Poettering's avatar
Lennart Poettering committed
985
986
987
988
989
990
991
992
993
994
        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;
995
        Unit *following;
Lennart Poettering's avatar
Lennart Poettering committed
996
997
998

        assert(u);

999
1000
        if (u->meta.load_state != UNIT_LOADED)
                return -EINVAL;