chiark / gitweb /
manager: properly return newly created job in transaction_add_job_and_dependencies()
[elogind.git] / target.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "unit.h"
25 #include "target.h"
26 #include "load-fragment.h"
27 #include "log.h"
28
29 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
30         [TARGET_DEAD] = UNIT_INACTIVE,
31         [TARGET_ACTIVE] = UNIT_ACTIVE
32 };
33
34 static const char* const state_string_table[_TARGET_STATE_MAX] = {
35         [TARGET_DEAD] = "dead",
36         [TARGET_ACTIVE] = "active"
37 };
38
39 static int target_init(Unit *u) {
40         int r;
41         assert(u);
42
43         /* Make sure this config file actually exists */
44
45         if ((r = unit_load_fragment_and_dropin(u)) <= 0)
46                 return r < 0 ? r : -ENOENT;
47
48         return 0;
49 }
50
51 static void target_dump(Unit *u, FILE *f, const char *prefix) {
52         Target *t = TARGET(u);
53
54         assert(t);
55         assert(f);
56
57         fprintf(f,
58                 "%sTarget State: %s\n",
59                 prefix, state_string_table[t->state]);
60 }
61
62 static void target_set_state(Target *t, TargetState state) {
63         TargetState old_state;
64         assert(t);
65
66         old_state = t->state;
67         t->state = state;
68
69         log_debug("%s changed %s → %s", unit_id(UNIT(t)), state_string_table[old_state], state_string_table[state]);
70
71         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
72 }
73
74 static int target_start(Unit *u) {
75         Target *t = TARGET(u);
76
77         assert(t);
78         assert(t->state == TARGET_DEAD);
79
80         target_set_state(t, TARGET_ACTIVE);
81         return 0;
82 }
83
84 static int target_stop(Unit *u) {
85         Target *t = TARGET(u);
86
87         assert(t);
88         assert(t->state == TARGET_ACTIVE);
89
90         target_set_state(t, TARGET_DEAD);
91         return 0;
92 }
93
94 static UnitActiveState target_active_state(Unit *u) {
95         assert(u);
96
97         return state_translation_table[TARGET(u)->state];
98 }
99
100 const UnitVTable target_vtable = {
101         .suffix = ".target",
102
103         .init = target_init,
104
105         .dump = target_dump,
106
107         .start = target_start,
108         .stop = target_stop,
109
110         .active_state = target_active_state
111 };