]>
Commit | Line | Data |
---|---|---|
83096483 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2011 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 | ||
22 | #include <errno.h> | |
23 | #include <string.h> | |
24 | #include <stdio.h> | |
25 | #include <fcntl.h> | |
26 | ||
27 | #include "util.h" | |
28 | #include "install.h" | |
29 | ||
30 | static void dump_changes(UnitFileChange *c, unsigned n) { | |
31 | unsigned i; | |
32 | ||
33 | assert(n == 0 || c); | |
34 | ||
35 | for (i = 0; i < n; i++) { | |
36 | if (c[i].type == UNIT_FILE_UNLINK) | |
37 | printf("rm '%s'\n", c[i].path); | |
38 | else if (c[i].type == UNIT_FILE_SYMLINK) | |
39 | printf("ln -s '%s' '%s'\n", c[i].source, c[i].path); | |
40 | } | |
41 | } | |
42 | ||
43 | int main(int argc, char* argv[]) { | |
44 | Hashmap *h; | |
45 | UnitFileList *p; | |
46 | Iterator i; | |
47 | int r; | |
48 | const char *const files[] = { "avahi-daemon.service", NULL }; | |
49 | const char *const files2[] = { "/home/lennart/test.service", NULL }; | |
50 | UnitFileChange *changes = NULL; | |
51 | unsigned n_changes = 0; | |
52 | ||
53 | h = hashmap_new(string_hash_func, string_compare_func); | |
54 | r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h); | |
55 | assert_se(r == 0); | |
56 | ||
57 | HASHMAP_FOREACH(p, h, i) { | |
58 | UnitFileState s; | |
59 | ||
60 | s = unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(p->path)); | |
61 | ||
62 | assert_se(p->state == s); | |
63 | ||
64 | fprintf(stderr, "%s (%s)\n", | |
65 | p->path, | |
66 | unit_file_state_to_string(p->state)); | |
67 | } | |
68 | ||
69 | unit_file_list_free(h); | |
70 | ||
71 | log_error("enable"); | |
72 | ||
73 | r = unit_file_enable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes); | |
74 | assert_se(r >= 0); | |
75 | ||
76 | log_error("enable2"); | |
77 | ||
78 | r = unit_file_enable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes); | |
79 | assert_se(r >= 0); | |
80 | ||
81 | dump_changes(changes, n_changes); | |
82 | unit_file_changes_free(changes, n_changes); | |
83 | ||
84 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_ENABLED); | |
85 | ||
86 | log_error("disable"); | |
87 | ||
88 | changes = NULL; | |
89 | n_changes = 0; | |
90 | ||
91 | r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes); | |
92 | assert_se(r >= 0); | |
93 | ||
94 | dump_changes(changes, n_changes); | |
95 | unit_file_changes_free(changes, n_changes); | |
96 | ||
97 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_DISABLED); | |
98 | ||
99 | log_error("mask"); | |
100 | changes = NULL; | |
101 | n_changes = 0; | |
102 | ||
103 | r = unit_file_mask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes); | |
104 | assert_se(r >= 0); | |
105 | log_error("mask2"); | |
106 | r = unit_file_mask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes); | |
107 | assert_se(r >= 0); | |
108 | ||
109 | dump_changes(changes, n_changes); | |
110 | unit_file_changes_free(changes, n_changes); | |
111 | ||
112 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_MASKED); | |
113 | ||
114 | log_error("unmask"); | |
115 | changes = NULL; | |
116 | n_changes = 0; | |
117 | ||
118 | r = unit_file_unmask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes); | |
119 | assert_se(r >= 0); | |
120 | log_error("unmask2"); | |
121 | r = unit_file_unmask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes); | |
122 | assert_se(r >= 0); | |
123 | ||
124 | dump_changes(changes, n_changes); | |
125 | unit_file_changes_free(changes, n_changes); | |
126 | ||
127 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_DISABLED); | |
128 | ||
129 | log_error("mask"); | |
130 | changes = NULL; | |
131 | n_changes = 0; | |
132 | ||
133 | r = unit_file_mask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes); | |
134 | assert_se(r >= 0); | |
135 | ||
136 | dump_changes(changes, n_changes); | |
137 | unit_file_changes_free(changes, n_changes); | |
138 | ||
139 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_MASKED); | |
140 | ||
141 | log_error("disable"); | |
142 | changes = NULL; | |
143 | n_changes = 0; | |
144 | ||
145 | r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes); | |
146 | assert_se(r >= 0); | |
147 | log_error("disable2"); | |
148 | r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes); | |
149 | assert_se(r >= 0); | |
150 | ||
151 | dump_changes(changes, n_changes); | |
152 | unit_file_changes_free(changes, n_changes); | |
153 | ||
154 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_MASKED); | |
155 | ||
156 | log_error("umask"); | |
157 | changes = NULL; | |
158 | n_changes = 0; | |
159 | ||
160 | r = unit_file_unmask(UNIT_FILE_SYSTEM, false, NULL, (char**) files, &changes, &n_changes); | |
161 | assert_se(r >= 0); | |
162 | ||
163 | dump_changes(changes, n_changes); | |
164 | unit_file_changes_free(changes, n_changes); | |
165 | ||
166 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, files[0]) == UNIT_FILE_DISABLED); | |
167 | ||
168 | log_error("enable files2"); | |
169 | changes = NULL; | |
170 | n_changes = 0; | |
171 | ||
172 | r = unit_file_enable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, false, &changes, &n_changes); | |
173 | assert_se(r >= 0); | |
174 | ||
175 | dump_changes(changes, n_changes); | |
176 | unit_file_changes_free(changes, n_changes); | |
177 | ||
178 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(files2[0])) == UNIT_FILE_ENABLED); | |
179 | ||
180 | log_error("disable files2"); | |
181 | changes = NULL; | |
182 | n_changes = 0; | |
183 | ||
184 | r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, &changes, &n_changes); | |
185 | assert_se(r >= 0); | |
186 | ||
187 | dump_changes(changes, n_changes); | |
188 | unit_file_changes_free(changes, n_changes); | |
189 | ||
190 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(files2[0])) == _UNIT_FILE_STATE_INVALID); | |
191 | ||
192 | log_error("link files2"); | |
193 | changes = NULL; | |
194 | n_changes = 0; | |
195 | ||
196 | r = unit_file_link(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, false, &changes, &n_changes); | |
197 | assert_se(r >= 0); | |
198 | ||
199 | dump_changes(changes, n_changes); | |
200 | unit_file_changes_free(changes, n_changes); | |
201 | ||
202 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(files2[0])) == UNIT_FILE_LINKED); | |
203 | ||
204 | log_error("disable files2"); | |
205 | changes = NULL; | |
206 | n_changes = 0; | |
207 | ||
208 | r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, &changes, &n_changes); | |
209 | assert_se(r >= 0); | |
210 | ||
211 | dump_changes(changes, n_changes); | |
212 | unit_file_changes_free(changes, n_changes); | |
213 | ||
214 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(files2[0])) == _UNIT_FILE_STATE_INVALID); | |
215 | ||
216 | log_error("link files2"); | |
217 | changes = NULL; | |
218 | n_changes = 0; | |
219 | ||
220 | r = unit_file_link(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, false, &changes, &n_changes); | |
221 | assert_se(r >= 0); | |
222 | ||
223 | dump_changes(changes, n_changes); | |
224 | unit_file_changes_free(changes, n_changes); | |
225 | ||
226 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(files2[0])) == UNIT_FILE_LINKED); | |
227 | ||
228 | log_error("reenable files2"); | |
229 | changes = NULL; | |
230 | n_changes = 0; | |
231 | ||
232 | r = unit_file_reenable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, false, &changes, &n_changes); | |
233 | assert_se(r >= 0); | |
234 | ||
235 | dump_changes(changes, n_changes); | |
236 | unit_file_changes_free(changes, n_changes); | |
237 | ||
238 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(files2[0])) == UNIT_FILE_ENABLED); | |
239 | ||
240 | log_error("disable files2"); | |
241 | changes = NULL; | |
242 | n_changes = 0; | |
243 | ||
244 | r = unit_file_disable(UNIT_FILE_SYSTEM, false, NULL, (char**) files2, &changes, &n_changes); | |
245 | assert_se(r >= 0); | |
246 | ||
247 | dump_changes(changes, n_changes); | |
248 | unit_file_changes_free(changes, n_changes); | |
249 | ||
250 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(files2[0])) == _UNIT_FILE_STATE_INVALID); | |
251 | log_error("preset files"); | |
252 | changes = NULL; | |
253 | n_changes = 0; | |
254 | ||
255 | r = unit_file_preset(UNIT_FILE_SYSTEM, false, NULL, (char**) files, false, &changes, &n_changes); | |
256 | assert_se(r >= 0); | |
257 | ||
258 | dump_changes(changes, n_changes); | |
259 | unit_file_changes_free(changes, n_changes); | |
260 | ||
261 | assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, file_name_from_path(files[0])) == UNIT_FILE_ENABLED); | |
262 | ||
263 | return 0; | |
264 | } |