1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "path-util.h"
31 static void dump_changes(UnitFileChange *c, unsigned n) {
36 for (i = 0; i < n; i++) {
37 if (c[i].type == UNIT_FILE_UNLINK)
38 printf("rm '%s'\n", c[i].path);
39 else if (c[i].type == UNIT_FILE_SYMLINK)
40 printf("ln -s '%s' '%s'\n", c[i].source, c[i].path);
44 int main(int argc, char* argv[]) {
49 const char *const files[] = { "avahi-daemon.service", NULL };
50 const char *const files2[] = { "/home/lennart/test.service", NULL };
51 UnitFileChange *changes = NULL;
52 unsigned n_changes = 0;
54 h = hashmap_new(string_hash_func, string_compare_func);
55 r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
58 HASHMAP_FOREACH(p, h, i) {
61 s = unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(p->path));
63 assert_se(p->state == s);
65 fprintf(stderr, "%s (%s)\n",
67 unit_file_state_to_string(p->state));
70 unit_file_list_free(h);
74 r = unit_file_enable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes);
79 r = unit_file_enable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes);
82 dump_changes(changes, n_changes);
83 unit_file_changes_free(changes, n_changes);
85 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_ENABLED);
92 r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes);
95 dump_changes(changes, n_changes);
96 unit_file_changes_free(changes, n_changes);
98 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_DISABLED);
104 r = unit_file_mask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes);
107 r = unit_file_mask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes);
110 dump_changes(changes, n_changes);
111 unit_file_changes_free(changes, n_changes);
113 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_MASKED);
119 r = unit_file_unmask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes);
121 log_error("unmask2");
122 r = unit_file_unmask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes);
125 dump_changes(changes, n_changes);
126 unit_file_changes_free(changes, n_changes);
128 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_DISABLED);
134 r = unit_file_mask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes);
137 dump_changes(changes, n_changes);
138 unit_file_changes_free(changes, n_changes);
140 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_MASKED);
142 log_error("disable");
146 r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes);
148 log_error("disable2");
149 r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes);
152 dump_changes(changes, n_changes);
153 unit_file_changes_free(changes, n_changes);
155 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_MASKED);
161 r = unit_file_unmask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes);
164 dump_changes(changes, n_changes);
165 unit_file_changes_free(changes, n_changes);
167 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_DISABLED);
169 log_error("enable files2");
173 r = unit_file_enable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, false, &changes, &n_changes);
176 dump_changes(changes, n_changes);
177 unit_file_changes_free(changes, n_changes);
179 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_ENABLED);
181 log_error("disable files2");
185 r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, &changes, &n_changes);
188 dump_changes(changes, n_changes);
189 unit_file_changes_free(changes, n_changes);
191 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
193 log_error("link files2");
197 r = unit_file_link(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, false, &changes, &n_changes);
200 dump_changes(changes, n_changes);
201 unit_file_changes_free(changes, n_changes);
203 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_LINKED);
205 log_error("disable files2");
209 r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, &changes, &n_changes);
212 dump_changes(changes, n_changes);
213 unit_file_changes_free(changes, n_changes);
215 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
217 log_error("link files2");
221 r = unit_file_link(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, false, &changes, &n_changes);
224 dump_changes(changes, n_changes);
225 unit_file_changes_free(changes, n_changes);
227 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_LINKED);
229 log_error("reenable files2");
233 r = unit_file_reenable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, false, &changes, &n_changes);
236 dump_changes(changes, n_changes);
237 unit_file_changes_free(changes, n_changes);
239 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_ENABLED);
241 log_error("disable files2");
245 r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, &changes, &n_changes);
248 dump_changes(changes, n_changes);
249 unit_file_changes_free(changes, n_changes);
251 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
252 log_error("preset files");
256 r = unit_file_preset(UNIT_FILE_SYSTEM, false, NULL, (char**) files, UNIT_FILE_PRESET_FULL, false, &changes, &n_changes);
259 dump_changes(changes, n_changes);
260 unit_file_changes_free(changes, n_changes);
262 assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files[0])) == UNIT_FILE_ENABLED);