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