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