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

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

  Copyright 2010 Lennart Poettering

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

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

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

Lennart Poettering's avatar
Lennart Poettering committed
22
23
24
25
26
27
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <sys/poll.h>
28
29
#include <stdlib.h>
#include <unistd.h>
Lennart Poettering's avatar
Lennart Poettering committed
30
31
32
33
34
35
36
37

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

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

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

        return u;
}

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

        assert(u);
        assert(text);

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

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

100
101
        if (!s)
                return -ENOMEM;
Lennart Poettering's avatar
Lennart Poettering committed
102

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

108
        assert_se((t = unit_name_to_type(s)) >= 0);
Lennart Poettering's avatar
Lennart Poettering committed
109

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

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

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        if (i && unit_vtable[t]->no_instances)
                goto fail;

        if (u->meta.type != _UNIT_TYPE_INVALID && !streq_ptr(u->meta.instance, i)) {
                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;
        }

133
134
135
136
137
        if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
                r = -E2BIG;
                goto fail;
        }

138
139
140
141
        if ((r = set_put(u->meta.names, s)) < 0) {
                if (r == -EEXIST)
                        r = 0;
                goto fail;
Lennart Poettering's avatar
Lennart Poettering committed
142
143
144
145
        }

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

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

151
                u->meta.type = t;
152
153
154
155
                u->meta.id = s;
                u->meta.instance = i;

                LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
156
157
158

                if (UNIT_VTABLE(u)->init)
                        UNIT_VTABLE(u)->init(u);
159
160
        } else
                free(i);
Lennart Poettering's avatar
Lennart Poettering committed
161

162
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
163
        return 0;
164
165
166
167
168
169

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

        return r;
Lennart Poettering's avatar
Lennart Poettering committed
170
171
}

172
int unit_choose_id(Unit *u, const char *name) {
173
        char *s, *t = NULL;
174
175
176
177

        assert(u);
        assert(name);

178
179
180
181
182
183
184
185
186
187
188
        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;
        }

189
        /* Selects one of the names of this unit as the id */
190
191
        s = set_get(u->meta.names, (char*) name);
        free(t);
192

193
        if (!s)
194
195
196
                return -ENOENT;

        u->meta.id = s;
197
        unit_add_to_dbus_queue(u);
198

199
200
201
        return 0;
}

Lennart Poettering's avatar
Lennart Poettering committed
202
203
204
205
206
207
208
209
210
211
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;
212
213

        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
214
215
216
        return 0;
}

217
218
219
bool unit_check_gc(Unit *u) {
        assert(u);

220
221
222
        if (u->meta.load_state == UNIT_STUB)
                return true;

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
        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
239
240
void unit_add_to_load_queue(Unit *u) {
        assert(u);
241
        assert(u->meta.type != _UNIT_TYPE_INVALID);
Lennart Poettering's avatar
Lennart Poettering committed
242
243
244
245
246
247
248
249

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

250
251
252
253
254
255
256
257
258
259
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;
}

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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);
}

278
279
void unit_add_to_dbus_queue(Unit *u) {
        assert(u);
280
        assert(u->meta.type != _UNIT_TYPE_INVALID);
281

282
        if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue)
283
284
                return;

285
286
287
288
289
        if (set_isempty(u->meta.manager->subscribed)) {
                u->meta.sent_dbus_new_signal = true;
                return;
        }

290
291
292
293
        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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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);
308
309

                unit_add_to_gc_queue(other);
Lennart Poettering's avatar
Lennart Poettering committed
310
311
312
313
314
315
316
317
318
319
320
321
        }

        set_free(s);
}

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

        assert(u);

322
323
        bus_unit_send_removed_signal(u);

324
325
326
327
        if (u->meta.load_state != UNIT_STUB)
                if (UNIT_VTABLE(u)->done)
                        UNIT_VTABLE(u)->done(u);

Lennart Poettering's avatar
Lennart Poettering committed
328
329
330
        SET_FOREACH(t, u->meta.names, i)
                hashmap_remove_value(u->meta.manager->units, t, u);

331
332
333
334
335
336
        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
337
338
339
        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
340
341
342
        if (u->meta.in_load_queue)
                LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);

343
344
345
        if (u->meta.in_dbus_queue)
                LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);

346
347
348
        if (u->meta.in_cleanup_queue)
                LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);

349
350
351
352
353
        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--;
        }

354
        cgroup_bonding_free_list(u->meta.cgroup_bondings);
Lennart Poettering's avatar
Lennart Poettering committed
355
356

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

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

361
362
        free(u->meta.instance);

Lennart Poettering's avatar
Lennart Poettering committed
363
364
365
366
367
368
        free(u);
}

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

369
370
371
372
373
374
        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
375
376
377
378

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

379
380
381
382
383
384
const char* unit_sub_state_to_string(Unit *u) {
        assert(u);

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

385
386
387
static void complete_move(Set **s, Set **other) {
        assert(s);
        assert(other);
Lennart Poettering's avatar
Lennart Poettering committed
388

389
390
        if (!*other)
                return;
Lennart Poettering's avatar
Lennart Poettering committed
391
392

        if (*s)
393
394
395
396
397
398
                set_move(*s, *other);
        else {
                *s = *other;
                *other = NULL;
        }
}
Lennart Poettering's avatar
Lennart Poettering committed
399

400
401
402
static void merge_names(Unit *u, Unit *other) {
        char *t;
        Iterator i;
Lennart Poettering's avatar
Lennart Poettering committed
403

404
405
406
407
408
        assert(u);
        assert(other);

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

409
        set_free_free(other->meta.names);
410
411
412
413
414
        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
415
416
}

417
418
419
static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
        Iterator i;
        Unit *back;
Lennart Poettering's avatar
Lennart Poettering committed
420
        int r;
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

        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
446
447
448
449
450
        UnitDependency d;

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

453
454
        other = unit_follow_merge(other);

455
456
457
        if (other == u)
                return 0;

458
459
460
461
        if (u->meta.type != other->meta.type)
                return -EINVAL;

        if (!streq_ptr(u->meta.instance, other->meta.instance))
Lennart Poettering's avatar
Lennart Poettering committed
462
463
                return -EINVAL;

464
465
        if (other->meta.load_state != UNIT_STUB &&
            other->meta.load_state != UNIT_FAILED)
466
                return -EEXIST;
Lennart Poettering's avatar
Lennart Poettering committed
467

468
469
470
        if (other->meta.job)
                return -EEXIST;

471
        if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(unit_active_state(other)))
472
473
                return -EEXIST;

Lennart Poettering's avatar
Lennart Poettering committed
474
        /* Merge names */
475
        merge_names(u, other);
Lennart Poettering's avatar
Lennart Poettering committed
476
477
478

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

481
482
483
        other->meta.load_state = UNIT_MERGED;
        other->meta.merged_into = u;

484
485
486
487
488
489
490
        /* 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);
491
492
493
494
495
496
497
        unit_add_to_cleanup_queue(other);

        return 0;
}

int unit_merge_by_name(Unit *u, const char *name) {
        Unit *other;
498
499
        int r;
        char *s = NULL;
500
501
502
503

        assert(u);
        assert(name);

504
505
506
507
508
509
510
511
512
513
        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;
        }

514
        if (!(other = manager_get_unit(u->meta.manager, name)))
515
516
517
                r = unit_add_name(u, name);
        else
                r = unit_merge(u, other);
518

519
520
        free(s);
        return r;
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
}

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

538
539
540
541
        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)
542
543
544
545
546
                return 0;

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

547
        if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
548
549
                return r;

550
        if (u->meta.manager->running_as == MANAGER_SYSTEM)
551
                if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
552
553
                        return r;

Lennart Poettering's avatar
Lennart Poettering committed
554
555
556
557
558
559
560
561
562
        return 0;
}

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

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

563
        return u->meta.id;
Lennart Poettering's avatar
Lennart Poettering committed
564
565
566
567
568
569
}

void unit_dump(Unit *u, FILE *f, const char *prefix) {
        char *t;
        UnitDependency d;
        Iterator i;
570
571
        char *p2;
        const char *prefix2;
572
        CGroupBonding *b;
573
574
575
576
577
        char
                timestamp1[FORMAT_TIMESTAMP_MAX],
                timestamp2[FORMAT_TIMESTAMP_MAX],
                timestamp3[FORMAT_TIMESTAMP_MAX],
                timestamp4[FORMAT_TIMESTAMP_MAX];
Lennart Poettering's avatar
Lennart Poettering committed
578
579

        assert(u);
580
        assert(u->meta.type >= 0);
Lennart Poettering's avatar
Lennart Poettering committed
581
582
583

        if (!prefix)
                prefix = "";
584
585
        p2 = strappend(prefix, "\t");
        prefix2 = p2 ? p2 : prefix;
Lennart Poettering's avatar
Lennart Poettering committed
586
587

        fprintf(f,
588
                "%s-> Unit %s:\n"
Lennart Poettering's avatar
Lennart Poettering committed
589
                "%s\tDescription: %s\n"
590
                "%s\tInstance: %s\n"
Lennart Poettering's avatar
Lennart Poettering committed
591
                "%s\tUnit Load State: %s\n"
592
                "%s\tUnit Active State: %s\n"
593
                "%s\tInactive Exit Timestamp: %s\n"
594
                "%s\tActive Enter Timestamp: %s\n"
595
                "%s\tActive Exit Timestamp: %s\n"
596
                "%s\tInactive Enter Timestamp: %s\n"
597
                "%s\tGC Check Good: %s\n",
598
                prefix, u->meta.id,
Lennart Poettering's avatar
Lennart Poettering committed
599
                prefix, unit_description(u),
600
                prefix, strna(u->meta.instance),
601
                prefix, unit_load_state_to_string(u->meta.load_state),
602
                prefix, unit_active_state_to_string(unit_active_state(u)),
603
604
605
606
                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)),
607
                prefix, yes_no(unit_check_gc(u)));
608

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

612
613
614
        if (u->meta.fragment_path)
                fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);

Lennart Poettering's avatar
Lennart Poettering committed
615
616
617
618
        for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
                Unit *other;

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

622
        if (u->meta.load_state == UNIT_LOADED) {
623
624
                fprintf(f,
                        "%s\tRecursive Stop: %s\n"
625
626
627
                        "%s\tStopWhenUnneeded: %s\n"
                        "%s\tOnlyByDependency: %s\n"
                        "%s\tDefaultDependencies: %s\n",
628
                        prefix, yes_no(u->meta.recursive_stop),
629
630
631
                        prefix, yes_no(u->meta.stop_when_unneeded),
                        prefix, yes_no(u->meta.only_by_dependency),
                        prefix, yes_no(u->meta.default_dependencies));
632

633
634
635
                LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
                        fprintf(f, "%s\tControlGroup: %s:%s\n",
                                prefix, b->controller, b->path);
636

637
638
                if (UNIT_VTABLE(u)->dump)
                        UNIT_VTABLE(u)->dump(u, f, prefix2);
639
640
641
642

        } else if (u->meta.load_state == UNIT_MERGED)
                fprintf(f,
                        "%s\tMerged into: %s\n",
643
                        prefix, u->meta.merged_into->meta.id);
Lennart Poettering's avatar
Lennart Poettering committed
644
645
646
647

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

648
        free(p2);
Lennart Poettering's avatar
Lennart Poettering committed
649
650
651
}

/* Common implementation for multiple backends */
652
int unit_load_fragment_and_dropin(Unit *u) {
653
654
655
656
657
        int r;

        assert(u);

        /* Load a .service file */
658
        if ((r = unit_load_fragment(u)) < 0)
659
660
                return r;

661
        if (u->meta.load_state == UNIT_STUB)
662
663
664
665
666
667
668
669
670
671
                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 */
672
int unit_load_fragment_and_dropin_optional(Unit *u) {
673
        int r;
Lennart Poettering's avatar
Lennart Poettering committed
674
675
676

        assert(u);

677
678
679
680
        /* Same as unit_load_fragment_and_dropin(), but whether
         * something can be loaded or not doesn't matter. */

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

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

Lennart Poettering's avatar
Lennart Poettering committed
687
        /* Load drop-in directory data */
688
        if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
689
690
                return r;

691
        return 0;
Lennart Poettering's avatar
Lennart Poettering committed
692
693
}

694
695
696
697
698
699
700
701
702
703
/* 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
704
705
706
707
708
709
710
711
712
713
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;
        }

714
715
716
        if (u->meta.type == _UNIT_TYPE_INVALID)
                return -EINVAL;

Lennart Poettering's avatar
Lennart Poettering committed
717
718
719
        if (u->meta.load_state != UNIT_STUB)
                return 0;

720
721
        if (UNIT_VTABLE(u)->load)
                if ((r = UNIT_VTABLE(u)->load(u)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
722
                        goto fail;
723

724
        if (u->meta.load_state == UNIT_STUB) {
725
726
727
728
729
730
731
                r = -ENOENT;
                goto fail;
        }

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

        unit_add_to_dbus_queue(unit_follow_merge(u));
732
        unit_add_to_gc_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
733
734
735
736
737

        return 0;

fail:
        u->meta.load_state = UNIT_FAILED;
738
        unit_add_to_dbus_queue(u);
739

Lennart Poettering's avatar
Lennart Poettering committed
740
        log_debug("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
741

Lennart Poettering's avatar
Lennart Poettering committed
742
743
744
745
        return r;
}

/* Errors:
746
747
748
749
 *         -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
750
751
752
753
754
755
 */
int unit_start(Unit *u) {
        UnitActiveState state;

        assert(u);

756
757
758
        if (u->meta.load_state != UNIT_LOADED)
                return -EINVAL;

759
760
761
762
        /* 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
763
764
765
766
        state = unit_active_state(u);
        if (UNIT_IS_ACTIVE_OR_RELOADING(state))
                return -EALREADY;

767
768
769
770
        /* 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
771
772
773
774
775
776
        /* 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. */

777
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
        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);
798
        if (UNIT_IS_INACTIVE_OR_MAINTENANCE(state))
Lennart Poettering's avatar
Lennart Poettering committed
799
800
                return -EALREADY;

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

804
        unit_add_to_dbus_queue(u);
Lennart Poettering's avatar
Lennart Poettering committed
805
806
807
808
809
810
811
812
813
814
815
816
817
        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);

818
819
820
        if (u->meta.load_state != UNIT_LOADED)
                return -EINVAL;

Lennart Poettering's avatar
Lennart Poettering committed
821
822
823
824
        if (!unit_can_reload(u))
                return -EBADR;

        state = unit_active_state(u);
825
        if (unit_active_state(u) == UNIT_RELOADING)
Lennart Poettering's avatar
Lennart Poettering committed
826
827
828
829
830
                return -EALREADY;

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

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

847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
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;

866
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
867
868
869
870
871
872
873
                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;

874
        log_debug("Service %s is not needed anymore. Stopping.", u->meta.id);
875
876
877
878
879

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

Lennart Poettering's avatar
Lennart Poettering committed
880
881
882
883
884
885
886
887
888
889
890
static void retroactively_start_dependencies(Unit *u) {
        Iterator i;
        Unit *other;

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

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

891
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
Lennart Poettering's avatar
Lennart Poettering committed
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
                if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                        manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);

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

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

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

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

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

915
916
917
918
919
920
921
922
923
        if (u->meta.recursive_stop) {
                /* Pull down units need us recursively if enabled */
                SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
                        if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                                manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
        }

        /* Garbage collect services that might not be needed anymore, if enabled */
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
Lennart Poettering's avatar
Lennart Poettering committed
924
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
925
                        unit_check_uneeded(other);
926
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
927
928
929
930
931
932
933
934
                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);
935
        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
936
937
                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                        unit_check_uneeded(other);
Lennart Poettering's avatar
Lennart Poettering committed
938
939
940
}

void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
941
        bool unexpected = false;
942
        dual_timestamp ts;
943

Lennart Poettering's avatar
Lennart Poettering committed
944
945
946
947
        assert(u);
        assert(os < _UNIT_ACTIVE_STATE_MAX);
        assert(ns < _UNIT_ACTIVE_STATE_MAX);

948
949
950
951
952
953
        /* 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
954

955
        dual_timestamp_get(&ts);
956

957
        if (UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && !UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
958
                u->meta.inactive_exit_timestamp = ts;
959
        else if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
960
961
                u->meta.inactive_enter_timestamp = ts;

Lennart Poettering's avatar
Lennart Poettering committed
962
        if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
963
                u->meta.active_enter_timestamp = ts;
Lennart Poettering's avatar
Lennart Poettering committed
964
        else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
965
                u->meta.active_exit_timestamp = ts;
Lennart Poettering's avatar
Lennart Poettering committed
966

967
        timer_unit_notify(u, ns);
968
        path_unit_notify(u, ns);
969

Lennart Poettering's avatar
Lennart Poettering committed
970
971
972
973
974
975
976
        if (u->meta.job) {

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

                        /* So we reached a different state for this
                         * job. Let's see if we can run it now if it
                         * failed previously due to EAGAIN. */
977
                        job_add_to_run_queue(u->meta.job);
Lennart Poettering's avatar
Lennart Poettering committed
978
979


980
981
982
                /* 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
983

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

986
987
                case JOB_START:
                case JOB_VERIFY_ACTIVE:
Lennart Poettering's avatar
Lennart Poettering committed
988

989
990
991
992
993
994
                        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
995

996
                        break;
Lennart Poettering's avatar
Lennart Poettering committed
997

998
999
                case JOB_RELOAD:
                case JOB_RELOAD_OR_START:
Lennart Poettering's avatar
Lennart Poettering committed
1000