1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
27 #include "load-fragment.h"
30 static const UnitActiveState state_translation_table
[_TARGET_STATE_MAX
] = {
31 [TARGET_DEAD
] = UNIT_INACTIVE
,
32 [TARGET_ACTIVE
] = UNIT_ACTIVE
35 static const char* const state_string_table
[_TARGET_STATE_MAX
] = {
36 [TARGET_DEAD
] = "dead",
37 [TARGET_ACTIVE
] = "active"
40 static void target_dump(Unit
*u
, FILE *f
, const char *prefix
) {
41 Target
*t
= TARGET(u
);
47 "%sTarget State: %s\n",
48 prefix
, state_string_table
[t
->state
]);
51 static void target_set_state(Target
*t
, TargetState state
) {
52 TargetState old_state
;
58 if (state
!= old_state
)
59 log_debug("%s changed %s → %s", UNIT(t
)->meta
.id
, state_string_table
[old_state
], state_string_table
[state
]);
61 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
]);
64 static int target_start(Unit
*u
) {
65 Target
*t
= TARGET(u
);
68 assert(t
->state
== TARGET_DEAD
);
70 target_set_state(t
, TARGET_ACTIVE
);
74 static int target_stop(Unit
*u
) {
75 Target
*t
= TARGET(u
);
78 assert(t
->state
== TARGET_ACTIVE
);
80 target_set_state(t
, TARGET_DEAD
);
84 static UnitActiveState
target_active_state(Unit
*u
) {
87 return state_translation_table
[TARGET(u
)->state
];
90 static const char *target_sub_state_to_string(Unit
*u
) {
93 return state_string_table
[TARGET(u
)->state
];
96 int target_get_runlevel(Target
*t
) {
102 { SPECIAL_RUNLEVEL5_TARGET
, '5' },
103 { SPECIAL_RUNLEVEL4_TARGET
, '4' },
104 { SPECIAL_RUNLEVEL3_TARGET
, '3' },
105 { SPECIAL_RUNLEVEL2_TARGET
, '2' },
106 { SPECIAL_RUNLEVEL1_TARGET
, '1' },
107 { SPECIAL_RUNLEVEL0_TARGET
, '0' },
108 { SPECIAL_RUNLEVEL6_TARGET
, '6' },
115 /* Tries to determine if this is a SysV runlevel and returns
116 * it if that is so. */
118 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
119 if (unit_has_name(UNIT(t
), table
[i
].special
))
120 return table
[i
].runlevel
;
125 const UnitVTable target_vtable
= {
128 .load
= unit_load_fragment_and_dropin
,
132 .start
= target_start
,
135 .active_state
= target_active_state
,
136 .sub_state_to_string
= target_sub_state_to_string