]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
95ed3294 TA |
2 | |
3 | #include <errno.h> | |
4 | #include <stdio.h> | |
5 | #include <unistd.h> | |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
e393eff6 | 8 | #include "def.h" |
c279613f | 9 | #include "dirent-util.h" |
41e2036e | 10 | #include "exit-status.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
c279613f | 12 | #include "fileio.h" |
7f0cc637 | 13 | #include "generator.h" |
07630cea | 14 | #include "hashmap.h" |
8fcde012 | 15 | #include "hexdecoct.h" |
07630cea LP |
16 | #include "install.h" |
17 | #include "log.h" | |
95ed3294 | 18 | #include "mkdir.h" |
95ed3294 | 19 | #include "path-lookup.h" |
07630cea | 20 | #include "path-util.h" |
f2341e0a | 21 | #include "set.h" |
07630cea | 22 | #include "special.h" |
98bad05e | 23 | #include "specifier.h" |
8fcde012 | 24 | #include "stat-util.h" |
07630cea LP |
25 | #include "string-util.h" |
26 | #include "strv.h" | |
27 | #include "unit-name.h" | |
28 | #include "util.h" | |
95ed3294 | 29 | |
95ed3294 TA |
30 | static const struct { |
31 | const char *path; | |
32 | const char *target; | |
95ed3294 TA |
33 | } rcnd_table[] = { |
34 | /* Standard SysV runlevels for start-up */ | |
788d2b08 LP |
35 | { "rc1.d", SPECIAL_RESCUE_TARGET }, |
36 | { "rc2.d", SPECIAL_MULTI_USER_TARGET }, | |
37 | { "rc3.d", SPECIAL_MULTI_USER_TARGET }, | |
38 | { "rc4.d", SPECIAL_MULTI_USER_TARGET }, | |
39 | { "rc5.d", SPECIAL_GRAPHICAL_TARGET }, | |
40 | ||
41 | /* We ignore the SysV runlevels for shutdown here, as SysV services get default dependencies anyway, and that | |
42 | * means they are shut down anyway at system power off if running. */ | |
95ed3294 TA |
43 | }; |
44 | ||
dd422d1e | 45 | static const char *arg_dest = "/tmp"; |
8fba1c8d | 46 | |
95ed3294 TA |
47 | typedef struct SysvStub { |
48 | char *name; | |
49 | char *path; | |
50 | char *description; | |
51 | int sysv_start_priority; | |
52 | char *pid_file; | |
53 | char **before; | |
54 | char **after; | |
55 | char **wants; | |
260ad50f | 56 | char **wanted_by; |
95ed3294 TA |
57 | bool has_lsb; |
58 | bool reload; | |
c279613f | 59 | bool loaded; |
95ed3294 TA |
60 | } SysvStub; |
61 | ||
8fba1c8d | 62 | static void free_sysvstub(SysvStub *s) { |
c279613f LP |
63 | if (!s) |
64 | return; | |
65 | ||
8fba1c8d ZJS |
66 | free(s->name); |
67 | free(s->path); | |
68 | free(s->description); | |
69 | free(s->pid_file); | |
70 | strv_free(s->before); | |
71 | strv_free(s->after); | |
72 | strv_free(s->wants); | |
73 | strv_free(s->wanted_by); | |
8fba1c8d ZJS |
74 | free(s); |
75 | } | |
76 | ||
77 | DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, free_sysvstub); | |
78 | ||
79 | static void free_sysvstub_hashmapp(Hashmap **h) { | |
224b0e7a | 80 | hashmap_free_with_destructor(*h, free_sysvstub); |
8fba1c8d | 81 | } |
95ed3294 | 82 | |
b7e71846 | 83 | static int add_alias(const char *service, const char *alias) { |
c279613f | 84 | const char *link; |
b7e71846 MB |
85 | int r; |
86 | ||
87 | assert(service); | |
88 | assert(alias); | |
89 | ||
c279613f | 90 | link = strjoina(arg_dest, "/", alias); |
b7e71846 MB |
91 | |
92 | r = symlink(service, link); | |
93 | if (r < 0) { | |
94 | if (errno == EEXIST) | |
95 | return 0; | |
c279613f | 96 | |
b7e71846 MB |
97 | return -errno; |
98 | } | |
99 | ||
100 | return 1; | |
101 | } | |
102 | ||
95ed3294 | 103 | static int generate_unit_file(SysvStub *s) { |
98bad05e | 104 | _cleanup_free_ char *path_escaped = NULL; |
95ed3294 | 105 | _cleanup_fclose_ FILE *f = NULL; |
c279613f LP |
106 | const char *unit; |
107 | char **p; | |
95ed3294 TA |
108 | int r; |
109 | ||
c279613f LP |
110 | assert(s); |
111 | ||
112 | if (!s->loaded) | |
113 | return 0; | |
114 | ||
98bad05e LP |
115 | path_escaped = specifier_escape(s->path); |
116 | if (!path_escaped) | |
117 | return log_oom(); | |
118 | ||
c279613f LP |
119 | unit = strjoina(arg_dest, "/", s->name); |
120 | ||
77354c7e MP |
121 | /* We might already have a symlink with the same name from a Provides:, |
122 | * or from backup files like /etc/init.d/foo.bak. Real scripts always win, | |
123 | * so remove an existing link */ | |
4e558983 | 124 | if (is_symlink(unit) > 0) { |
c279613f | 125 | log_warning("Overwriting existing symlink %s with real service.", unit); |
9993ef2e | 126 | (void) unlink(unit); |
77354c7e MP |
127 | } |
128 | ||
95ed3294 | 129 | f = fopen(unit, "wxe"); |
4a62c710 MS |
130 | if (!f) |
131 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
95ed3294 TA |
132 | |
133 | fprintf(f, | |
134 | "# Automatically generated by systemd-sysv-generator\n\n" | |
135 | "[Unit]\n" | |
aad0a2c8 | 136 | "Documentation=man:systemd-sysv-generator(8)\n" |
c279613f | 137 | "SourcePath=%s\n", |
98bad05e LP |
138 | path_escaped); |
139 | ||
140 | if (s->description) { | |
141 | _cleanup_free_ char *t; | |
c279613f | 142 | |
98bad05e LP |
143 | t = specifier_escape(s->description); |
144 | if (!t) | |
145 | return log_oom(); | |
146 | ||
147 | fprintf(f, "Description=%s\n", t); | |
148 | } | |
95ed3294 | 149 | |
c584ffc0 LN |
150 | STRV_FOREACH(p, s->before) |
151 | fprintf(f, "Before=%s\n", *p); | |
152 | STRV_FOREACH(p, s->after) | |
153 | fprintf(f, "After=%s\n", *p); | |
154 | STRV_FOREACH(p, s->wants) | |
155 | fprintf(f, "Wants=%s\n", *p); | |
95ed3294 TA |
156 | |
157 | fprintf(f, | |
158 | "\n[Service]\n" | |
159 | "Type=forking\n" | |
160 | "Restart=no\n" | |
161 | "TimeoutSec=5min\n" | |
162 | "IgnoreSIGPIPE=no\n" | |
163 | "KillMode=process\n" | |
164 | "GuessMainPID=no\n" | |
165 | "RemainAfterExit=%s\n", | |
166 | yes_no(!s->pid_file)); | |
167 | ||
98bad05e LP |
168 | if (s->pid_file) { |
169 | _cleanup_free_ char *t; | |
170 | ||
171 | t = specifier_escape(s->pid_file); | |
172 | if (!t) | |
173 | return log_oom(); | |
174 | ||
175 | fprintf(f, "PIDFile=%s\n", t); | |
176 | } | |
95ed3294 | 177 | |
41e2036e LP |
178 | /* Consider two special LSB exit codes a clean exit */ |
179 | if (s->has_lsb) | |
180 | fprintf(f, | |
181 | "SuccessExitStatus=%i %i\n", | |
182 | EXIT_NOTINSTALLED, | |
183 | EXIT_NOTCONFIGURED); | |
184 | ||
95ed3294 TA |
185 | fprintf(f, |
186 | "ExecStart=%s start\n" | |
187 | "ExecStop=%s stop\n", | |
98bad05e | 188 | path_escaped, path_escaped); |
95ed3294 TA |
189 | |
190 | if (s->reload) | |
98bad05e | 191 | fprintf(f, "ExecReload=%s reload\n", path_escaped); |
95ed3294 | 192 | |
c279613f LP |
193 | r = fflush_and_check(f); |
194 | if (r < 0) | |
195 | return log_error_errno(r, "Failed to write unit %s: %m", unit); | |
196 | ||
7f0cc637 ZJS |
197 | STRV_FOREACH(p, s->wanted_by) |
198 | (void) generator_add_symlink(arg_dest, *p, "wants", s->name); | |
95ed3294 | 199 | |
c279613f | 200 | return 1; |
95ed3294 TA |
201 | } |
202 | ||
203 | static bool usage_contains_reload(const char *line) { | |
204 | return (strcasestr(line, "{reload|") || | |
205 | strcasestr(line, "{reload}") || | |
206 | strcasestr(line, "{reload\"") || | |
207 | strcasestr(line, "|reload|") || | |
208 | strcasestr(line, "|reload}") || | |
209 | strcasestr(line, "|reload\"")); | |
210 | } | |
211 | ||
212 | static char *sysv_translate_name(const char *name) { | |
0b2ec8a3 DH |
213 | _cleanup_free_ char *c = NULL; |
214 | char *res; | |
95ed3294 | 215 | |
264581a2 FS |
216 | c = strdup(name); |
217 | if (!c) | |
0b2ec8a3 | 218 | return NULL; |
95ed3294 | 219 | |
0b2ec8a3 DH |
220 | res = endswith(c, ".sh"); |
221 | if (res) | |
222 | *res = 0; | |
95ed3294 | 223 | |
37cbc1d5 | 224 | if (unit_name_mangle(c, 0, &res) < 0) |
0b2ec8a3 DH |
225 | return NULL; |
226 | ||
227 | return res; | |
95ed3294 TA |
228 | } |
229 | ||
7532e6d4 | 230 | static int sysv_translate_facility(SysvStub *s, unsigned line, const char *name, char **ret) { |
95ed3294 TA |
231 | |
232 | /* We silently ignore the $ prefix here. According to the LSB | |
233 | * spec it simply indicates whether something is a | |
234 | * standardized name or a distribution-specific one. Since we | |
235 | * just follow what already exists and do not introduce new | |
236 | * uses or names we don't care who introduced a new name. */ | |
237 | ||
238 | static const char * const table[] = { | |
239 | /* LSB defined facilities */ | |
240 | "local_fs", NULL, | |
241 | "network", SPECIAL_NETWORK_ONLINE_TARGET, | |
242 | "named", SPECIAL_NSS_LOOKUP_TARGET, | |
243 | "portmap", SPECIAL_RPCBIND_TARGET, | |
244 | "remote_fs", SPECIAL_REMOTE_FS_TARGET, | |
245 | "syslog", NULL, | |
246 | "time", SPECIAL_TIME_SYNC_TARGET, | |
247 | }; | |
248 | ||
7532e6d4 | 249 | const char *filename; |
c279613f | 250 | char *filename_no_sh, *e, *m; |
95ed3294 | 251 | const char *n; |
4e488555 | 252 | unsigned i; |
c279613f | 253 | int r; |
95ed3294 TA |
254 | |
255 | assert(name); | |
7532e6d4 | 256 | assert(s); |
c279613f | 257 | assert(ret); |
95ed3294 | 258 | |
7532e6d4 FS |
259 | filename = basename(s->path); |
260 | ||
95ed3294 TA |
261 | n = *name == '$' ? name + 1 : name; |
262 | ||
263 | for (i = 0; i < ELEMENTSOF(table); i += 2) { | |
95ed3294 TA |
264 | if (!streq(table[i], n)) |
265 | continue; | |
266 | ||
e932f540 LP |
267 | if (!table[i+1]) { |
268 | *ret = NULL; | |
95ed3294 | 269 | return 0; |
e932f540 | 270 | } |
95ed3294 | 271 | |
c279613f LP |
272 | m = strdup(table[i+1]); |
273 | if (!m) | |
95ed3294 TA |
274 | return log_oom(); |
275 | ||
c279613f LP |
276 | *ret = m; |
277 | return 1; | |
278 | } | |
279 | ||
280 | /* If we don't know this name, fallback heuristics to figure | |
281 | * out whether something is a target or a service alias. */ | |
282 | ||
283 | /* Facilities starting with $ are most likely targets */ | |
284 | if (*name == '$') { | |
285 | r = unit_name_build(n, NULL, ".target", ret); | |
286 | if (r < 0) | |
7532e6d4 | 287 | return log_error_errno(r, "[%s:%u] Could not build name for facility %s: %m", s->path, line, name); |
c279613f | 288 | |
e932f540 | 289 | return 1; |
95ed3294 TA |
290 | } |
291 | ||
c279613f | 292 | /* Strip ".sh" suffix from file name for comparison */ |
4e488555 | 293 | filename_no_sh = strdupa(filename); |
40780877 | 294 | e = endswith(filename_no_sh, ".sh"); |
3315f085 | 295 | if (e) { |
4e488555 | 296 | *e = '\0'; |
3315f085 LP |
297 | filename = filename_no_sh; |
298 | } | |
29e0e6d8 | 299 | |
c279613f | 300 | /* Names equaling the file name of the services are redundant */ |
e932f540 LP |
301 | if (streq_ptr(n, filename)) { |
302 | *ret = NULL; | |
95ed3294 | 303 | return 0; |
e932f540 | 304 | } |
95ed3294 | 305 | |
c279613f LP |
306 | /* Everything else we assume to be normal service names */ |
307 | m = sysv_translate_name(n); | |
308 | if (!m) | |
309 | return log_oom(); | |
95ed3294 | 310 | |
c279613f | 311 | *ret = m; |
95ed3294 TA |
312 | return 1; |
313 | } | |
314 | ||
1e2fee5f | 315 | static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) { |
1e2fee5f ZJS |
316 | int r; |
317 | ||
c279613f LP |
318 | assert(s); |
319 | assert(full_text); | |
320 | assert(text); | |
1e2fee5f | 321 | |
c279613f LP |
322 | for (;;) { |
323 | _cleanup_free_ char *word = NULL, *m = NULL; | |
1e2fee5f | 324 | |
c279613f | 325 | r = extract_first_word(&text, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX); |
1e2fee5f | 326 | if (r < 0) |
7532e6d4 | 327 | return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line); |
1e2fee5f | 328 | if (r == 0) |
c279613f LP |
329 | break; |
330 | ||
7532e6d4 | 331 | r = sysv_translate_facility(s, line, word, &m); |
c279613f | 332 | if (r <= 0) /* continue on error */ |
1e2fee5f ZJS |
333 | continue; |
334 | ||
c279613f LP |
335 | switch (unit_name_to_type(m)) { |
336 | ||
337 | case UNIT_SERVICE: | |
1e2fee5f ZJS |
338 | log_debug("Adding Provides: alias '%s' for '%s'", m, s->name); |
339 | r = add_alias(s->name, m); | |
e987f2a8 | 340 | if (r < 0) |
f2341e0a | 341 | log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m); |
c279613f LP |
342 | break; |
343 | ||
344 | case UNIT_TARGET: | |
345 | ||
1e2fee5f ZJS |
346 | /* NB: SysV targets which are provided by a |
347 | * service are pulled in by the services, as | |
348 | * an indication that the generic service is | |
349 | * now available. This is strictly one-way. | |
350 | * The targets do NOT pull in SysV services! */ | |
c279613f | 351 | |
1e2fee5f ZJS |
352 | r = strv_extend(&s->before, m); |
353 | if (r < 0) | |
354 | return log_oom(); | |
c279613f | 355 | |
1e2fee5f ZJS |
356 | r = strv_extend(&s->wants, m); |
357 | if (r < 0) | |
358 | return log_oom(); | |
c279613f | 359 | |
1e2fee5f ZJS |
360 | if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET)) { |
361 | r = strv_extend(&s->before, SPECIAL_NETWORK_TARGET); | |
362 | if (r < 0) | |
363 | return log_oom(); | |
bd9ad4ff LN |
364 | r = strv_extend(&s->wants, SPECIAL_NETWORK_TARGET); |
365 | if (r < 0) | |
366 | return log_oom(); | |
1e2fee5f | 367 | } |
c279613f LP |
368 | |
369 | break; | |
370 | ||
371 | case _UNIT_TYPE_INVALID: | |
2c09a745 | 372 | log_warning("Unit name '%s' is invalid", m); |
c279613f LP |
373 | break; |
374 | ||
375 | default: | |
2c09a745 | 376 | log_warning("Unknown unit type for unit '%s'", m); |
c279613f | 377 | } |
1e2fee5f | 378 | } |
c279613f | 379 | |
1e2fee5f ZJS |
380 | return 0; |
381 | } | |
382 | ||
383 | static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) { | |
1e2fee5f ZJS |
384 | int r; |
385 | ||
c279613f LP |
386 | assert(s); |
387 | assert(full_text); | |
388 | assert(text); | |
1e2fee5f | 389 | |
c279613f LP |
390 | for (;;) { |
391 | _cleanup_free_ char *word = NULL, *m = NULL; | |
392 | bool is_before; | |
1e2fee5f | 393 | |
c279613f LP |
394 | r = extract_first_word(&text, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX); |
395 | if (r < 0) | |
7532e6d4 | 396 | return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line); |
1e2fee5f | 397 | if (r == 0) |
c279613f LP |
398 | break; |
399 | ||
7532e6d4 | 400 | r = sysv_translate_facility(s, line, word, &m); |
c279613f | 401 | if (r <= 0) /* continue on error */ |
1e2fee5f ZJS |
402 | continue; |
403 | ||
404 | is_before = startswith_no_case(full_text, "X-Start-Before:"); | |
405 | ||
406 | if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET) && !is_before) { | |
407 | /* the network-online target is special, as it needs to be actively pulled in */ | |
408 | r = strv_extend(&s->after, m); | |
409 | if (r < 0) | |
410 | return log_oom(); | |
c279613f | 411 | |
1e2fee5f | 412 | r = strv_extend(&s->wants, m); |
e987f2a8 | 413 | } else |
1e2fee5f | 414 | r = strv_extend(is_before ? &s->before : &s->after, m); |
1e2fee5f | 415 | if (r < 0) |
e987f2a8 | 416 | return log_oom(); |
1e2fee5f | 417 | } |
c279613f | 418 | |
1e2fee5f ZJS |
419 | return 0; |
420 | } | |
421 | ||
95ed3294 TA |
422 | static int load_sysv(SysvStub *s) { |
423 | _cleanup_fclose_ FILE *f; | |
424 | unsigned line = 0; | |
425 | int r; | |
426 | enum { | |
427 | NORMAL, | |
428 | DESCRIPTION, | |
429 | LSB, | |
430 | LSB_DESCRIPTION, | |
431 | USAGE_CONTINUATION | |
432 | } state = NORMAL; | |
433 | _cleanup_free_ char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL; | |
434 | char *description; | |
435 | bool supports_reload = false; | |
436 | ||
437 | assert(s); | |
438 | ||
439 | f = fopen(s->path, "re"); | |
c279613f LP |
440 | if (!f) { |
441 | if (errno == ENOENT) | |
442 | return 0; | |
95ed3294 | 443 | |
c279613f LP |
444 | return log_error_errno(errno, "Failed to open %s: %m", s->path); |
445 | } | |
77354c7e | 446 | |
c279613f | 447 | log_debug("Loading SysV script %s", s->path); |
95ed3294 | 448 | |
e393eff6 LP |
449 | for (;;) { |
450 | _cleanup_free_ char *l = NULL; | |
c279613f | 451 | char *t; |
95ed3294 | 452 | |
e393eff6 LP |
453 | r = read_line(f, LONG_LINE_MAX, &l); |
454 | if (r < 0) | |
455 | return log_error_errno(r, "Failed to read configuration file '%s': %m", s->path); | |
456 | if (r == 0) | |
457 | break; | |
458 | ||
95ed3294 TA |
459 | line++; |
460 | ||
461 | t = strstrip(l); | |
462 | if (*t != '#') { | |
463 | /* Try to figure out whether this init script supports | |
464 | * the reload operation. This heuristic looks for | |
465 | * "Usage" lines which include the reload option. */ | |
e393eff6 | 466 | if (state == USAGE_CONTINUATION || |
95ed3294 TA |
467 | (state == NORMAL && strcasestr(t, "usage"))) { |
468 | if (usage_contains_reload(t)) { | |
469 | supports_reload = true; | |
470 | state = NORMAL; | |
471 | } else if (t[strlen(t)-1] == '\\') | |
472 | state = USAGE_CONTINUATION; | |
473 | else | |
474 | state = NORMAL; | |
475 | } | |
476 | ||
477 | continue; | |
478 | } | |
479 | ||
480 | if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) { | |
481 | state = LSB; | |
482 | s->has_lsb = true; | |
483 | continue; | |
484 | } | |
485 | ||
3742095b | 486 | if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(t, "### END INIT INFO")) { |
95ed3294 TA |
487 | state = NORMAL; |
488 | continue; | |
489 | } | |
490 | ||
491 | t++; | |
492 | t += strspn(t, WHITESPACE); | |
493 | ||
494 | if (state == NORMAL) { | |
495 | ||
496 | /* Try to parse Red Hat style description */ | |
497 | ||
498 | if (startswith_no_case(t, "description:")) { | |
499 | ||
c279613f | 500 | size_t k; |
859fdd1e | 501 | const char *j; |
95ed3294 | 502 | |
c279613f LP |
503 | k = strlen(t); |
504 | if (k > 0 && t[k-1] == '\\') { | |
95ed3294 TA |
505 | state = DESCRIPTION; |
506 | t[k-1] = 0; | |
507 | } | |
508 | ||
3c6f7c34 | 509 | j = empty_to_null(strstrip(t+12)); |
95ed3294 | 510 | |
c279613f LP |
511 | r = free_and_strdup(&chkconfig_description, j); |
512 | if (r < 0) | |
513 | return log_oom(); | |
95ed3294 TA |
514 | |
515 | } else if (startswith_no_case(t, "pidfile:")) { | |
c279613f | 516 | const char *fn; |
95ed3294 TA |
517 | |
518 | state = NORMAL; | |
519 | ||
520 | fn = strstrip(t+8); | |
521 | if (!path_is_absolute(fn)) { | |
f2341e0a | 522 | log_error("[%s:%u] PID file not absolute. Ignoring.", s->path, line); |
95ed3294 TA |
523 | continue; |
524 | } | |
525 | ||
c279613f LP |
526 | r = free_and_strdup(&s->pid_file, fn); |
527 | if (r < 0) | |
528 | return log_oom(); | |
95ed3294 TA |
529 | } |
530 | ||
531 | } else if (state == DESCRIPTION) { | |
532 | ||
533 | /* Try to parse Red Hat style description | |
534 | * continuation */ | |
535 | ||
c279613f | 536 | size_t k; |
95ed3294 TA |
537 | char *j; |
538 | ||
c279613f LP |
539 | k = strlen(t); |
540 | if (k > 0 && t[k-1] == '\\') | |
95ed3294 TA |
541 | t[k-1] = 0; |
542 | else | |
543 | state = NORMAL; | |
544 | ||
545 | j = strstrip(t); | |
c279613f | 546 | if (!isempty(j)) { |
95ed3294 TA |
547 | char *d = NULL; |
548 | ||
549 | if (chkconfig_description) | |
605405c6 | 550 | d = strjoin(chkconfig_description, " ", j); |
95ed3294 TA |
551 | else |
552 | d = strdup(j); | |
95ed3294 | 553 | if (!d) |
c279613f | 554 | return log_oom(); |
95ed3294 TA |
555 | |
556 | free(chkconfig_description); | |
557 | chkconfig_description = d; | |
558 | } | |
559 | ||
3742095b | 560 | } else if (IN_SET(state, LSB, LSB_DESCRIPTION)) { |
95ed3294 TA |
561 | |
562 | if (startswith_no_case(t, "Provides:")) { | |
95ed3294 TA |
563 | state = LSB; |
564 | ||
1e2fee5f ZJS |
565 | r = handle_provides(s, line, t, t + 9); |
566 | if (r < 0) | |
567 | return r; | |
c279613f | 568 | |
95ed3294 TA |
569 | } else if (startswith_no_case(t, "Required-Start:") || |
570 | startswith_no_case(t, "Should-Start:") || | |
571 | startswith_no_case(t, "X-Start-Before:") || | |
572 | startswith_no_case(t, "X-Start-After:")) { | |
95ed3294 TA |
573 | |
574 | state = LSB; | |
575 | ||
1e2fee5f ZJS |
576 | r = handle_dependencies(s, line, t, strchr(t, ':') + 1); |
577 | if (r < 0) | |
578 | return r; | |
95ed3294 | 579 | |
95ed3294 | 580 | } else if (startswith_no_case(t, "Description:")) { |
c279613f | 581 | const char *j; |
95ed3294 TA |
582 | |
583 | state = LSB_DESCRIPTION; | |
584 | ||
3c6f7c34 | 585 | j = empty_to_null(strstrip(t+12)); |
95ed3294 | 586 | |
c279613f LP |
587 | r = free_and_strdup(&long_description, j); |
588 | if (r < 0) | |
589 | return log_oom(); | |
95ed3294 TA |
590 | |
591 | } else if (startswith_no_case(t, "Short-Description:")) { | |
c279613f | 592 | const char *j; |
95ed3294 TA |
593 | |
594 | state = LSB; | |
595 | ||
3c6f7c34 | 596 | j = empty_to_null(strstrip(t+18)); |
95ed3294 | 597 | |
c279613f LP |
598 | r = free_and_strdup(&short_description, j); |
599 | if (r < 0) | |
600 | return log_oom(); | |
95ed3294 TA |
601 | |
602 | } else if (state == LSB_DESCRIPTION) { | |
603 | ||
604 | if (startswith(l, "#\t") || startswith(l, "# ")) { | |
c279613f | 605 | const char *j; |
95ed3294 TA |
606 | |
607 | j = strstrip(t); | |
c279613f | 608 | if (!isempty(j)) { |
95ed3294 TA |
609 | char *d = NULL; |
610 | ||
611 | if (long_description) | |
605405c6 | 612 | d = strjoin(long_description, " ", t); |
95ed3294 TA |
613 | else |
614 | d = strdup(j); | |
95ed3294 | 615 | if (!d) |
c279613f | 616 | return log_oom(); |
95ed3294 TA |
617 | |
618 | free(long_description); | |
619 | long_description = d; | |
620 | } | |
621 | ||
622 | } else | |
623 | state = LSB; | |
624 | } | |
625 | } | |
626 | } | |
627 | ||
628 | s->reload = supports_reload; | |
629 | ||
630 | /* We use the long description only if | |
631 | * no short description is set. */ | |
632 | ||
633 | if (short_description) | |
634 | description = short_description; | |
635 | else if (chkconfig_description) | |
636 | description = chkconfig_description; | |
637 | else if (long_description) | |
638 | description = long_description; | |
639 | else | |
640 | description = NULL; | |
641 | ||
642 | if (description) { | |
643 | char *d; | |
644 | ||
645 | d = strappend(s->has_lsb ? "LSB: " : "SYSV: ", description); | |
646 | if (!d) | |
c279613f | 647 | return log_oom(); |
95ed3294 TA |
648 | |
649 | s->description = d; | |
650 | } | |
651 | ||
c279613f | 652 | s->loaded = true; |
95ed3294 TA |
653 | return 0; |
654 | } | |
655 | ||
656 | static int fix_order(SysvStub *s, Hashmap *all_services) { | |
657 | SysvStub *other; | |
658 | Iterator j; | |
659 | int r; | |
660 | ||
661 | assert(s); | |
662 | ||
c279613f LP |
663 | if (!s->loaded) |
664 | return 0; | |
665 | ||
95ed3294 TA |
666 | if (s->sysv_start_priority < 0) |
667 | return 0; | |
668 | ||
669 | HASHMAP_FOREACH(other, all_services, j) { | |
670 | if (s == other) | |
671 | continue; | |
672 | ||
c279613f LP |
673 | if (!other->loaded) |
674 | continue; | |
675 | ||
95ed3294 TA |
676 | if (other->sysv_start_priority < 0) |
677 | continue; | |
678 | ||
679 | /* If both units have modern headers we don't care | |
680 | * about the priorities */ | |
681 | if (s->has_lsb && other->has_lsb) | |
682 | continue; | |
683 | ||
684 | if (other->sysv_start_priority < s->sysv_start_priority) { | |
685 | r = strv_extend(&s->after, other->name); | |
686 | if (r < 0) | |
687 | return log_oom(); | |
c279613f LP |
688 | |
689 | } else if (other->sysv_start_priority > s->sysv_start_priority) { | |
95ed3294 TA |
690 | r = strv_extend(&s->before, other->name); |
691 | if (r < 0) | |
692 | return log_oom(); | |
c279613f | 693 | } else |
95ed3294 TA |
694 | continue; |
695 | ||
696 | /* FIXME: Maybe we should compare the name here lexicographically? */ | |
697 | } | |
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
4143c6c3 LP |
702 | static int acquire_search_path(const char *def, const char *envvar, char ***ret) { |
703 | _cleanup_strv_free_ char **l = NULL; | |
704 | const char *e; | |
705 | int r; | |
706 | ||
707 | assert(def); | |
708 | assert(envvar); | |
709 | ||
710 | e = getenv(envvar); | |
711 | if (e) { | |
712 | r = path_split_and_make_absolute(e, &l); | |
713 | if (r < 0) | |
714 | return log_error_errno(r, "Failed to make $%s search path absolute: %m", envvar); | |
715 | } | |
716 | ||
717 | if (strv_isempty(l)) { | |
718 | strv_free(l); | |
719 | ||
720 | l = strv_new(def, NULL); | |
721 | if (!l) | |
722 | return log_oom(); | |
723 | } | |
724 | ||
725 | if (!path_strv_resolve_uniq(l, NULL)) | |
726 | return log_oom(); | |
727 | ||
ae2a15bc | 728 | *ret = TAKE_PTR(l); |
4143c6c3 LP |
729 | |
730 | return 0; | |
731 | } | |
732 | ||
a8ffe6fb | 733 | static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) { |
4143c6c3 | 734 | _cleanup_strv_free_ char **sysvinit_path = NULL; |
95ed3294 | 735 | char **path; |
0ec0deaa | 736 | int r; |
95ed3294 | 737 | |
c279613f | 738 | assert(lp); |
c279613f | 739 | |
4143c6c3 LP |
740 | r = acquire_search_path(SYSTEM_SYSVINIT_PATH, "SYSTEMD_SYSVINIT_PATH", &sysvinit_path); |
741 | if (r < 0) | |
742 | return r; | |
743 | ||
744 | STRV_FOREACH(path, sysvinit_path) { | |
95ed3294 TA |
745 | _cleanup_closedir_ DIR *d = NULL; |
746 | struct dirent *de; | |
747 | ||
748 | d = opendir(*path); | |
749 | if (!d) { | |
750 | if (errno != ENOENT) | |
c279613f | 751 | log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path); |
95ed3294 TA |
752 | continue; |
753 | } | |
754 | ||
c279613f | 755 | FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) { |
95ed3294 | 756 | _cleanup_free_ char *fpath = NULL, *name = NULL; |
8fba1c8d | 757 | _cleanup_(free_sysvstubp) SysvStub *service = NULL; |
805e5dda | 758 | struct stat st; |
95ed3294 | 759 | |
7b729f86 | 760 | if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) { |
c279613f | 761 | log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name); |
95ed3294 | 762 | continue; |
805e5dda | 763 | } |
95ed3294 TA |
764 | |
765 | if (!(st.st_mode & S_IXUSR)) | |
766 | continue; | |
767 | ||
805e5dda LP |
768 | if (!S_ISREG(st.st_mode)) |
769 | continue; | |
770 | ||
95ed3294 TA |
771 | name = sysv_translate_name(de->d_name); |
772 | if (!name) | |
773 | return log_oom(); | |
774 | ||
a986501b LP |
775 | if (hashmap_contains(all_services, name)) |
776 | continue; | |
777 | ||
5ae87056 | 778 | r = unit_file_exists(UNIT_FILE_SYSTEM, lp, name); |
76ec966f | 779 | if (r < 0 && !IN_SET(r, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) { |
0ec0deaa LP |
780 | log_debug_errno(r, "Failed to detect whether %s exists, skipping: %m", name); |
781 | continue; | |
3c6d8e57 | 782 | } else if (r != 0) { |
0ec0deaa | 783 | log_debug("Native unit for %s already exists, skipping.", name); |
f4f01ec1 MP |
784 | continue; |
785 | } | |
786 | ||
605405c6 | 787 | fpath = strjoin(*path, "/", de->d_name); |
c279613f LP |
788 | if (!fpath) |
789 | return log_oom(); | |
790 | ||
95ed3294 TA |
791 | service = new0(SysvStub, 1); |
792 | if (!service) | |
793 | return log_oom(); | |
794 | ||
795 | service->sysv_start_priority = -1; | |
1cc6c93a YW |
796 | service->name = TAKE_PTR(name); |
797 | service->path = TAKE_PTR(fpath); | |
95ed3294 TA |
798 | |
799 | r = hashmap_put(all_services, service->name, service); | |
805e5dda | 800 | if (r < 0) |
95ed3294 TA |
801 | return log_oom(); |
802 | ||
805e5dda | 803 | service = NULL; |
95ed3294 TA |
804 | } |
805 | } | |
806 | ||
807 | return 0; | |
808 | } | |
809 | ||
a8ffe6fb | 810 | static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) { |
95ed3294 | 811 | Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {}; |
4143c6c3 | 812 | _cleanup_strv_free_ char **sysvrcnd_path = NULL; |
c279613f LP |
813 | SysvStub *service; |
814 | unsigned i; | |
815 | Iterator j; | |
816 | char **p; | |
817 | int r; | |
95ed3294 | 818 | |
c279613f LP |
819 | assert(lp); |
820 | ||
4143c6c3 LP |
821 | r = acquire_search_path(SYSTEM_SYSVRCND_PATH, "SYSTEMD_SYSVRCND_PATH", &sysvrcnd_path); |
822 | if (r < 0) | |
823 | return r; | |
824 | ||
825 | STRV_FOREACH(p, sysvrcnd_path) { | |
95ed3294 | 826 | for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) { |
c279613f LP |
827 | |
828 | _cleanup_closedir_ DIR *d = NULL; | |
829 | _cleanup_free_ char *path = NULL; | |
95ed3294 TA |
830 | struct dirent *de; |
831 | ||
605405c6 | 832 | path = strjoin(*p, "/", rcnd_table[i].path); |
c279613f LP |
833 | if (!path) { |
834 | r = log_oom(); | |
835 | goto finish; | |
836 | } | |
95ed3294 TA |
837 | |
838 | d = opendir(path); | |
839 | if (!d) { | |
840 | if (errno != ENOENT) | |
c279613f | 841 | log_warning_errno(errno, "Opening %s failed, ignoring: %m", path); |
95ed3294 TA |
842 | |
843 | continue; | |
844 | } | |
845 | ||
c279613f LP |
846 | FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", path)) { |
847 | _cleanup_free_ char *name = NULL, *fpath = NULL; | |
95ed3294 TA |
848 | int a, b; |
849 | ||
b9c59555 | 850 | if (de->d_name[0] != 'S') |
95ed3294 TA |
851 | continue; |
852 | ||
853 | if (strlen(de->d_name) < 4) | |
854 | continue; | |
855 | ||
856 | a = undecchar(de->d_name[1]); | |
857 | b = undecchar(de->d_name[2]); | |
858 | ||
859 | if (a < 0 || b < 0) | |
860 | continue; | |
861 | ||
605405c6 | 862 | fpath = strjoin(*p, "/", de->d_name); |
95ed3294 | 863 | if (!fpath) { |
c279613f | 864 | r = log_oom(); |
95ed3294 TA |
865 | goto finish; |
866 | } | |
867 | ||
868 | name = sysv_translate_name(de->d_name + 3); | |
869 | if (!name) { | |
870 | r = log_oom(); | |
871 | goto finish; | |
872 | } | |
873 | ||
8c84621c | 874 | service = hashmap_get(all_services, name); |
9ed794a3 | 875 | if (!service) { |
c279613f | 876 | log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name); |
95ed3294 TA |
877 | continue; |
878 | } | |
879 | ||
b9c59555 | 880 | service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority); |
95ed3294 | 881 | |
b9c59555 LP |
882 | r = set_ensure_allocated(&runlevel_services[i], NULL); |
883 | if (r < 0) { | |
884 | log_oom(); | |
885 | goto finish; | |
886 | } | |
95ed3294 | 887 | |
b9c59555 LP |
888 | r = set_put(runlevel_services[i], service); |
889 | if (r < 0) { | |
890 | log_oom(); | |
891 | goto finish; | |
95ed3294 TA |
892 | } |
893 | } | |
894 | } | |
c279613f | 895 | } |
95ed3294 | 896 | |
95ed3294 TA |
897 | for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) |
898 | SET_FOREACH(service, runlevel_services[i], j) { | |
899 | r = strv_extend(&service->before, rcnd_table[i].target); | |
c279613f LP |
900 | if (r < 0) { |
901 | log_oom(); | |
902 | goto finish; | |
903 | } | |
260ad50f | 904 | r = strv_extend(&service->wanted_by, rcnd_table[i].target); |
c279613f LP |
905 | if (r < 0) { |
906 | log_oom(); | |
907 | goto finish; | |
908 | } | |
95ed3294 TA |
909 | } |
910 | ||
95ed3294 TA |
911 | r = 0; |
912 | ||
913 | finish: | |
95ed3294 TA |
914 | for (i = 0; i < ELEMENTSOF(rcnd_table); i++) |
915 | set_free(runlevel_services[i]); | |
916 | ||
917 | return r; | |
918 | } | |
919 | ||
920 | int main(int argc, char *argv[]) { | |
5921fc3c | 921 | _cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL; |
8e766630 | 922 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
95ed3294 TA |
923 | SysvStub *service; |
924 | Iterator j; | |
c279613f | 925 | int r; |
95ed3294 TA |
926 | |
927 | if (argc > 1 && argc != 4) { | |
928 | log_error("This program takes three or no arguments."); | |
929 | return EXIT_FAILURE; | |
930 | } | |
931 | ||
932 | if (argc > 1) | |
933 | arg_dest = argv[3]; | |
934 | ||
6c347d50 LP |
935 | log_set_prohibit_ipc(true); |
936 | log_set_target(LOG_TARGET_AUTO); | |
95ed3294 TA |
937 | log_parse_environment(); |
938 | log_open(); | |
939 | ||
940 | umask(0022); | |
941 | ||
4943d143 | 942 | r = lookup_paths_init(&lp, UNIT_FILE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL); |
95ed3294 | 943 | if (r < 0) { |
c279613f LP |
944 | log_error_errno(r, "Failed to find lookup paths: %m"); |
945 | goto finish; | |
95ed3294 TA |
946 | } |
947 | ||
d5099efc | 948 | all_services = hashmap_new(&string_hash_ops); |
95ed3294 | 949 | if (!all_services) { |
c279613f LP |
950 | r = log_oom(); |
951 | goto finish; | |
95ed3294 TA |
952 | } |
953 | ||
a8ffe6fb | 954 | r = enumerate_sysv(&lp, all_services); |
c279613f LP |
955 | if (r < 0) |
956 | goto finish; | |
95ed3294 | 957 | |
a8ffe6fb | 958 | r = set_dependencies_from_rcnd(&lp, all_services); |
c279613f LP |
959 | if (r < 0) |
960 | goto finish; | |
95ed3294 | 961 | |
c279613f LP |
962 | HASHMAP_FOREACH(service, all_services, j) |
963 | (void) load_sysv(service); | |
b3fae863 | 964 | |
95ed3294 | 965 | HASHMAP_FOREACH(service, all_services, j) { |
c279613f LP |
966 | (void) fix_order(service, all_services); |
967 | (void) generate_unit_file(service); | |
95ed3294 TA |
968 | } |
969 | ||
c279613f LP |
970 | r = 0; |
971 | ||
972 | finish: | |
973 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
95ed3294 | 974 | } |