]>
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" |
c22cbe26 | 29 | |
fa068367 LP |
30 | static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = { |
31 | [TARGET_DEAD] = UNIT_INACTIVE, | |
32 | [TARGET_ACTIVE] = UNIT_ACTIVE | |
33 | }; | |
34 | ||
35 | static const char* const state_string_table[_TARGET_STATE_MAX] = { | |
36 | [TARGET_DEAD] = "dead", | |
37 | [TARGET_ACTIVE] = "active" | |
38 | }; | |
39 | ||
40 | static void target_dump(Unit *u, FILE *f, const char *prefix) { | |
41 | Target *t = TARGET(u); | |
42 | ||
43 | assert(t); | |
44 | assert(f); | |
45 | ||
46 | fprintf(f, | |
47 | "%sTarget State: %s\n", | |
48 | prefix, state_string_table[t->state]); | |
49 | } | |
50 | ||
51 | static void target_set_state(Target *t, TargetState state) { | |
52 | TargetState old_state; | |
53 | assert(t); | |
54 | ||
a90ebccc LP |
55 | if (state == t->state) |
56 | return; | |
57 | ||
fa068367 LP |
58 | old_state = t->state; |
59 | t->state = state; | |
60 | ||
ba389502 | 61 | log_debug("%s changed %s → %s", unit_id(UNIT(t)), state_string_table[old_state], state_string_table[state]); |
c22cbe26 | 62 | |
fa068367 LP |
63 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]); |
64 | } | |
65 | ||
66 | static int target_start(Unit *u) { | |
67 | Target *t = TARGET(u); | |
68 | ||
69 | assert(t); | |
70 | assert(t->state == TARGET_DEAD); | |
71 | ||
72 | target_set_state(t, TARGET_ACTIVE); | |
73 | return 0; | |
74 | } | |
c22cbe26 | 75 | |
fa068367 LP |
76 | static int target_stop(Unit *u) { |
77 | Target *t = TARGET(u); | |
78 | ||
79 | assert(t); | |
80 | assert(t->state == TARGET_ACTIVE); | |
81 | ||
82 | target_set_state(t, TARGET_DEAD); | |
83 | return 0; | |
c22cbe26 LP |
84 | } |
85 | ||
87f0e418 | 86 | static UnitActiveState target_active_state(Unit *u) { |
fa068367 LP |
87 | assert(u); |
88 | ||
89 | return state_translation_table[TARGET(u)->state]; | |
c22cbe26 LP |
90 | } |
91 | ||
e293f0f0 LP |
92 | int target_get_runlevel(Target *t) { |
93 | ||
94 | static const struct { | |
95 | const char *special; | |
96 | const int runlevel; | |
97 | } table[] = { | |
98 | { SPECIAL_RUNLEVEL5_TARGET, '5' }, | |
99 | { SPECIAL_RUNLEVEL4_TARGET, '4' }, | |
100 | { SPECIAL_RUNLEVEL3_TARGET, '3' }, | |
101 | { SPECIAL_RUNLEVEL2_TARGET, '2' }, | |
102 | { SPECIAL_RUNLEVEL1_TARGET, '1' }, | |
103 | { SPECIAL_RUNLEVEL0_TARGET, '0' }, | |
104 | { SPECIAL_RUNLEVEL6_TARGET, '6' }, | |
105 | }; | |
106 | ||
107 | unsigned i; | |
108 | ||
109 | assert(t); | |
110 | ||
111 | /* Tries to determine if this is a SysV runlevel and returns | |
112 | * it if that is so. */ | |
113 | ||
114 | for (i = 0; i < ELEMENTSOF(table); i++) | |
115 | if (unit_has_name(UNIT(t), table[i].special)) | |
116 | return table[i].runlevel; | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
87f0e418 | 121 | const UnitVTable target_vtable = { |
c22cbe26 LP |
122 | .suffix = ".target", |
123 | ||
23a177ef | 124 | .init = unit_load_fragment_and_dropin, |
fa068367 LP |
125 | |
126 | .dump = target_dump, | |
127 | ||
128 | .start = target_start, | |
129 | .stop = target_stop, | |
c22cbe26 LP |
130 | |
131 | .active_state = target_active_state | |
132 | }; |