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