]>
Commit | Line | Data |
---|---|---|
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" |
c22cbe26 | 30 | |
fa068367 LP |
31 | static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = { |
32 | [TARGET_DEAD] = UNIT_INACTIVE, | |
33 | [TARGET_ACTIVE] = UNIT_ACTIVE | |
34 | }; | |
35 | ||
fa068367 LP |
36 | static void target_set_state(Target *t, TargetState state) { |
37 | TargetState old_state; | |
38 | assert(t); | |
39 | ||
40 | old_state = t->state; | |
41 | t->state = state; | |
42 | ||
e537352b | 43 | if (state != old_state) |
40d50879 | 44 | log_debug("%s changed %s -> %s", |
a16e1123 LP |
45 | UNIT(t)->meta.id, |
46 | target_state_to_string(old_state), | |
47 | target_state_to_string(state)); | |
c22cbe26 | 48 | |
fa068367 LP |
49 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]); |
50 | } | |
51 | ||
a16e1123 LP |
52 | static int target_coldplug(Unit *u) { |
53 | Target *t = TARGET(u); | |
54 | ||
55 | assert(t); | |
56 | assert(t->state == TARGET_DEAD); | |
57 | ||
58 | if (t->deserialized_state != t->state) | |
59 | target_set_state(t, t->deserialized_state); | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
64 | static void target_dump(Unit *u, FILE *f, const char *prefix) { | |
65 | Target *t = TARGET(u); | |
66 | ||
67 | assert(t); | |
68 | assert(f); | |
69 | ||
70 | fprintf(f, | |
71 | "%sTarget State: %s\n", | |
72 | prefix, target_state_to_string(t->state)); | |
73 | } | |
74 | ||
fa068367 LP |
75 | static int target_start(Unit *u) { |
76 | Target *t = TARGET(u); | |
77 | ||
78 | assert(t); | |
79 | assert(t->state == TARGET_DEAD); | |
80 | ||
81 | target_set_state(t, TARGET_ACTIVE); | |
82 | return 0; | |
83 | } | |
c22cbe26 | 84 | |
fa068367 LP |
85 | static int target_stop(Unit *u) { |
86 | Target *t = TARGET(u); | |
87 | ||
88 | assert(t); | |
89 | assert(t->state == TARGET_ACTIVE); | |
90 | ||
91 | target_set_state(t, TARGET_DEAD); | |
92 | return 0; | |
c22cbe26 LP |
93 | } |
94 | ||
a16e1123 LP |
95 | static int target_serialize(Unit *u, FILE *f, FDSet *fds) { |
96 | Target *s = TARGET(u); | |
97 | ||
98 | assert(s); | |
99 | assert(f); | |
100 | assert(fds); | |
101 | ||
102 | unit_serialize_item(u, f, "state", target_state_to_string(s->state)); | |
103 | return 0; | |
104 | } | |
105 | ||
106 | static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
107 | Target *s = TARGET(u); | |
108 | ||
109 | assert(u); | |
110 | assert(key); | |
111 | assert(value); | |
112 | assert(fds); | |
113 | ||
114 | if (streq(key, "state")) { | |
115 | TargetState state; | |
116 | ||
117 | if ((state = target_state_from_string(value)) < 0) | |
118 | log_debug("Failed to parse state value %s", value); | |
119 | else | |
120 | s->deserialized_state = state; | |
121 | ||
122 | } else | |
123 | log_debug("Unknown serialization key '%s'", key); | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
87f0e418 | 128 | static UnitActiveState target_active_state(Unit *u) { |
fa068367 LP |
129 | assert(u); |
130 | ||
131 | return state_translation_table[TARGET(u)->state]; | |
c22cbe26 LP |
132 | } |
133 | ||
10a94420 LP |
134 | static const char *target_sub_state_to_string(Unit *u) { |
135 | assert(u); | |
136 | ||
a16e1123 | 137 | return target_state_to_string(TARGET(u)->state); |
10a94420 LP |
138 | } |
139 | ||
e293f0f0 LP |
140 | int target_get_runlevel(Target *t) { |
141 | ||
142 | static const struct { | |
143 | const char *special; | |
144 | const int runlevel; | |
145 | } table[] = { | |
146 | { SPECIAL_RUNLEVEL5_TARGET, '5' }, | |
147 | { SPECIAL_RUNLEVEL4_TARGET, '4' }, | |
148 | { SPECIAL_RUNLEVEL3_TARGET, '3' }, | |
149 | { SPECIAL_RUNLEVEL2_TARGET, '2' }, | |
150 | { SPECIAL_RUNLEVEL1_TARGET, '1' }, | |
151 | { SPECIAL_RUNLEVEL0_TARGET, '0' }, | |
152 | { SPECIAL_RUNLEVEL6_TARGET, '6' }, | |
153 | }; | |
154 | ||
155 | unsigned i; | |
156 | ||
157 | assert(t); | |
158 | ||
159 | /* Tries to determine if this is a SysV runlevel and returns | |
160 | * it if that is so. */ | |
161 | ||
162 | for (i = 0; i < ELEMENTSOF(table); i++) | |
163 | if (unit_has_name(UNIT(t), table[i].special)) | |
164 | return table[i].runlevel; | |
165 | ||
166 | return 0; | |
167 | } | |
168 | ||
a16e1123 LP |
169 | static const char* const target_state_table[_TARGET_STATE_MAX] = { |
170 | [TARGET_DEAD] = "dead", | |
171 | [TARGET_ACTIVE] = "active" | |
172 | }; | |
173 | ||
174 | DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState); | |
175 | ||
87f0e418 | 176 | const UnitVTable target_vtable = { |
c22cbe26 LP |
177 | .suffix = ".target", |
178 | ||
e537352b | 179 | .load = unit_load_fragment_and_dropin, |
a16e1123 | 180 | .coldplug = target_coldplug, |
fa068367 LP |
181 | |
182 | .dump = target_dump, | |
183 | ||
184 | .start = target_start, | |
185 | .stop = target_stop, | |
c22cbe26 | 186 | |
a16e1123 LP |
187 | .serialize = target_serialize, |
188 | .deserialize_item = target_deserialize_item, | |
189 | ||
10a94420 | 190 | .active_state = target_active_state, |
4139c1b2 LP |
191 | .sub_state_to_string = target_sub_state_to_string, |
192 | ||
193 | .bus_message_handler = bus_target_message_handler | |
c22cbe26 | 194 | }; |