]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/target.c
unit: add DefaultDependencies= setting
[thirdparty/systemd.git] / src / target.c
CommitLineData
c22cbe26
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
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
d46de8a1 22#include <errno.h>
23a177ef 23#include <signal.h>
d46de8a1 24
87f0e418 25#include "unit.h"
c22cbe26
LP
26#include "target.h"
27#include "load-fragment.h"
fa068367 28#include "log.h"
4139c1b2 29#include "dbus-target.h"
514f4ef5 30#include "special.h"
c22cbe26 31
fa068367
LP
32static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
33 [TARGET_DEAD] = UNIT_INACTIVE,
34 [TARGET_ACTIVE] = UNIT_ACTIVE
35};
36
fa068367
LP
37static 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
e537352b 44 if (state != old_state)
40d50879 45 log_debug("%s changed %s -> %s",
4cd1fbcc 46 t->meta.id,
a16e1123
LP
47 target_state_to_string(old_state),
48 target_state_to_string(state));
c22cbe26 49
fa068367
LP
50 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
51}
52
a40eb732
LP
53static int target_add_default_dependencies(Target *t) {
54 Iterator i;
55 Unit *other;
56 int r;
57
58 /* Imply ordering for requirement dependencies
59 * on target units. */
60
61 SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES], i)
62 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
63 return r;
64 SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
65 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
66 return r;
67 SET_FOREACH(other, t->meta.dependencies[UNIT_WANTS], i)
68 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
69 return r;
70
71 return 0;
72}
73
74static int target_load(Unit *u) {
75 Target *t = TARGET(u);
76 int r;
77
78 assert(t);
79
80 if ((r = unit_load_fragment_and_dropin(u)) < 0)
81 return r;
82
83 /* This is a new unit? Then let's add in some extras */
84 if (u->meta.load_state == UNIT_LOADED) {
85 if (u->meta.default_dependencies)
86 if ((r = target_add_default_dependencies(t)) < 0)
87 return r;
88 }
89
90 return 0;
91}
92
a16e1123
LP
93static int target_coldplug(Unit *u) {
94 Target *t = TARGET(u);
95
96 assert(t);
97 assert(t->state == TARGET_DEAD);
98
99 if (t->deserialized_state != t->state)
100 target_set_state(t, t->deserialized_state);
101
102 return 0;
103}
104
105static void target_dump(Unit *u, FILE *f, const char *prefix) {
106 Target *t = TARGET(u);
107
108 assert(t);
109 assert(f);
110
111 fprintf(f,
112 "%sTarget State: %s\n",
113 prefix, target_state_to_string(t->state));
114}
115
fa068367
LP
116static int target_start(Unit *u) {
117 Target *t = TARGET(u);
118
119 assert(t);
120 assert(t->state == TARGET_DEAD);
121
122 target_set_state(t, TARGET_ACTIVE);
123 return 0;
124}
c22cbe26 125
fa068367
LP
126static int target_stop(Unit *u) {
127 Target *t = TARGET(u);
128
129 assert(t);
130 assert(t->state == TARGET_ACTIVE);
131
132 target_set_state(t, TARGET_DEAD);
133 return 0;
c22cbe26
LP
134}
135
a16e1123
LP
136static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
137 Target *s = TARGET(u);
138
139 assert(s);
140 assert(f);
141 assert(fds);
142
143 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
144 return 0;
145}
146
147static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
148 Target *s = TARGET(u);
149
150 assert(u);
151 assert(key);
152 assert(value);
153 assert(fds);
154
155 if (streq(key, "state")) {
156 TargetState state;
157
158 if ((state = target_state_from_string(value)) < 0)
159 log_debug("Failed to parse state value %s", value);
160 else
161 s->deserialized_state = state;
162
163 } else
164 log_debug("Unknown serialization key '%s'", key);
165
166 return 0;
167}
168
87f0e418 169static UnitActiveState target_active_state(Unit *u) {
fa068367
LP
170 assert(u);
171
172 return state_translation_table[TARGET(u)->state];
c22cbe26
LP
173}
174
10a94420
LP
175static const char *target_sub_state_to_string(Unit *u) {
176 assert(u);
177
a16e1123 178 return target_state_to_string(TARGET(u)->state);
10a94420
LP
179}
180
e293f0f0
LP
181int target_get_runlevel(Target *t) {
182
183 static const struct {
184 const char *special;
185 const int runlevel;
186 } table[] = {
187 { SPECIAL_RUNLEVEL5_TARGET, '5' },
188 { SPECIAL_RUNLEVEL4_TARGET, '4' },
189 { SPECIAL_RUNLEVEL3_TARGET, '3' },
190 { SPECIAL_RUNLEVEL2_TARGET, '2' },
514f4ef5
LP
191 { SPECIAL_RESCUE_TARGET, '1' },
192 { SPECIAL_POWEROFF_TARGET, '0' },
193 { SPECIAL_REBOOT_TARGET, '6' },
e293f0f0
LP
194 };
195
196 unsigned i;
197
198 assert(t);
199
200 /* Tries to determine if this is a SysV runlevel and returns
201 * it if that is so. */
202
203 for (i = 0; i < ELEMENTSOF(table); i++)
204 if (unit_has_name(UNIT(t), table[i].special))
205 return table[i].runlevel;
206
207 return 0;
208}
209
a16e1123
LP
210static const char* const target_state_table[_TARGET_STATE_MAX] = {
211 [TARGET_DEAD] = "dead",
212 [TARGET_ACTIVE] = "active"
213};
214
215DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
216
87f0e418 217const UnitVTable target_vtable = {
c22cbe26
LP
218 .suffix = ".target",
219
a40eb732 220 .load = target_load,
a16e1123 221 .coldplug = target_coldplug,
fa068367
LP
222
223 .dump = target_dump,
224
225 .start = target_start,
226 .stop = target_stop,
c22cbe26 227
a16e1123
LP
228 .serialize = target_serialize,
229 .deserialize_item = target_deserialize_item,
230
10a94420 231 .active_state = target_active_state,
4139c1b2
LP
232 .sub_state_to_string = target_sub_state_to_string,
233
234 .bus_message_handler = bus_target_message_handler
c22cbe26 235};