chiark / gitweb /
emacs: disable tabs in .h files, too
[elogind.git] / src / target.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 #include <signal.h>
24
25 #include "unit.h"
26 #include "target.h"
27 #include "load-fragment.h"
28 #include "log.h"
29 #include "dbus-target.h"
30 #include "special.h"
31
32 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
33         [TARGET_DEAD] = UNIT_INACTIVE,
34         [TARGET_ACTIVE] = UNIT_ACTIVE
35 };
36
37 static void target_set_state(Target *t, TargetState state) {
38         TargetState old_state;
39         assert(t);
40
41         old_state = t->state;
42         t->state = state;
43
44         if (state != old_state)
45                 log_debug("%s changed %s -> %s",
46                           t->meta.id,
47                           target_state_to_string(old_state),
48                           target_state_to_string(state));
49
50         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
51 }
52
53 static int target_add_default_dependencies(Target *t) {
54         Iterator i;
55         Unit *other;
56         int r;
57
58         /* Imply ordering for requirement dependencies on target
59          * units. Note that when the user created a contradicting
60          * ordering manually we won't add anything in here to make
61          * sure we don't create a loop. */
62
63         SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES], i)
64                 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
65                         if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
66                                 return r;
67         SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
68                 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
69                         if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
70                                 return r;
71         SET_FOREACH(other, t->meta.dependencies[UNIT_WANTS], i)
72                 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
73                         if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
74                                 return r;
75
76         return 0;
77 }
78
79 static int target_load(Unit *u) {
80         Target *t = TARGET(u);
81         int r;
82
83         assert(t);
84
85         if ((r = unit_load_fragment_and_dropin(u)) < 0)
86                 return r;
87
88         /* This is a new unit? Then let's add in some extras */
89         if (u->meta.load_state == UNIT_LOADED) {
90                 if (u->meta.default_dependencies)
91                         if ((r = target_add_default_dependencies(t)) < 0)
92                                 return r;
93         }
94
95         return 0;
96 }
97
98 static int target_coldplug(Unit *u) {
99         Target *t = TARGET(u);
100
101         assert(t);
102         assert(t->state == TARGET_DEAD);
103
104         if (t->deserialized_state != t->state)
105                 target_set_state(t, t->deserialized_state);
106
107         return 0;
108 }
109
110 static void target_dump(Unit *u, FILE *f, const char *prefix) {
111         Target *t = TARGET(u);
112
113         assert(t);
114         assert(f);
115
116         fprintf(f,
117                 "%sTarget State: %s\n",
118                 prefix, target_state_to_string(t->state));
119 }
120
121 static int target_start(Unit *u) {
122         Target *t = TARGET(u);
123
124         assert(t);
125         assert(t->state == TARGET_DEAD);
126
127         target_set_state(t, TARGET_ACTIVE);
128         return 0;
129 }
130
131 static int target_stop(Unit *u) {
132         Target *t = TARGET(u);
133
134         assert(t);
135         assert(t->state == TARGET_ACTIVE);
136
137         target_set_state(t, TARGET_DEAD);
138         return 0;
139 }
140
141 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
142         Target *s = TARGET(u);
143
144         assert(s);
145         assert(f);
146         assert(fds);
147
148         unit_serialize_item(u, f, "state", target_state_to_string(s->state));
149         return 0;
150 }
151
152 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
153         Target *s = TARGET(u);
154
155         assert(u);
156         assert(key);
157         assert(value);
158         assert(fds);
159
160         if (streq(key, "state")) {
161                 TargetState state;
162
163                 if ((state = target_state_from_string(value)) < 0)
164                         log_debug("Failed to parse state value %s", value);
165                 else
166                         s->deserialized_state = state;
167
168         } else
169                 log_debug("Unknown serialization key '%s'", key);
170
171         return 0;
172 }
173
174 static UnitActiveState target_active_state(Unit *u) {
175         assert(u);
176
177         return state_translation_table[TARGET(u)->state];
178 }
179
180 static const char *target_sub_state_to_string(Unit *u) {
181         assert(u);
182
183         return target_state_to_string(TARGET(u)->state);
184 }
185
186 static const char* const target_state_table[_TARGET_STATE_MAX] = {
187         [TARGET_DEAD] = "dead",
188         [TARGET_ACTIVE] = "active"
189 };
190
191 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
192
193 const UnitVTable target_vtable = {
194         .suffix = ".target",
195
196         .load = target_load,
197         .coldplug = target_coldplug,
198
199         .dump = target_dump,
200
201         .start = target_start,
202         .stop = target_stop,
203
204         .serialize = target_serialize,
205         .deserialize_item = target_deserialize_item,
206
207         .active_state = target_active_state,
208         .sub_state_to_string = target_sub_state_to_string,
209
210         .bus_message_handler = bus_target_message_handler
211 };