]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
95ed3294 | 2 | /*** |
95ed3294 | 3 | Copyright 2014 Thomas H.P. Andersen |
95ed3294 | 4 | Copyright 2011 Michal Schmidt |
95ed3294 TA |
5 | ***/ |
6 | ||
7 | #include <errno.h> | |
8 | #include <stdio.h> | |
9 | #include <unistd.h> | |
10 | ||
b5efdb8a | 11 | #include "alloc-util.h" |
c279613f | 12 | #include "dirent-util.h" |
41e2036e | 13 | #include "exit-status.h" |
3ffd4af2 | 14 | #include "fd-util.h" |
c279613f | 15 | #include "fileio.h" |
7f0cc637 | 16 | #include "generator.h" |
07630cea | 17 | #include "hashmap.h" |
8fcde012 | 18 | #include "hexdecoct.h" |
07630cea LP |
19 | #include "install.h" |
20 | #include "log.h" | |
95ed3294 | 21 | #include "mkdir.h" |
95ed3294 | 22 | #include "path-lookup.h" |
07630cea | 23 | #include "path-util.h" |
f2341e0a | 24 | #include "set.h" |
07630cea | 25 | #include "special.h" |
98bad05e | 26 | #include "specifier.h" |
8fcde012 | 27 | #include "stat-util.h" |
07630cea LP |
28 | #include "string-util.h" |
29 | #include "strv.h" | |
30 | #include "unit-name.h" | |
31 | #include "util.h" | |
95ed3294 | 32 | |
95ed3294 TA |
33 | static const struct { |
34 | const char *path; | |
35 | const char *target; | |
95ed3294 TA |
36 | } rcnd_table[] = { |
37 | /* Standard SysV runlevels for start-up */ | |
788d2b08 LP |
38 | { "rc1.d", SPECIAL_RESCUE_TARGET }, |
39 | { "rc2.d", SPECIAL_MULTI_USER_TARGET }, | |
40 | { "rc3.d", SPECIAL_MULTI_USER_TARGET }, | |
41 | { "rc4.d", SPECIAL_MULTI_USER_TARGET }, | |
42 | { "rc5.d", SPECIAL_GRAPHICAL_TARGET }, | |
43 | ||
44 | /* We ignore the SysV runlevels for shutdown here, as SysV services get default dependencies anyway, and that | |
45 | * means they are shut down anyway at system power off if running. */ | |
95ed3294 TA |
46 | }; |
47 | ||
dd422d1e | 48 | static const char *arg_dest = "/tmp"; |
8fba1c8d | 49 | |
95ed3294 TA |
50 | typedef struct SysvStub { |
51 | char *name; | |
52 | char *path; | |
53 | char *description; | |
54 | int sysv_start_priority; | |
55 | char *pid_file; | |
56 | char **before; | |
57 | char **after; | |
58 | char **wants; | |
260ad50f | 59 | char **wanted_by; |
95ed3294 TA |
60 | bool has_lsb; |
61 | bool reload; | |
c279613f | 62 | bool loaded; |
95ed3294 TA |
63 | } SysvStub; |
64 | ||
8fba1c8d | 65 | static void free_sysvstub(SysvStub *s) { |
c279613f LP |
66 | if (!s) |
67 | return; | |
68 | ||
8fba1c8d ZJS |
69 | free(s->name); |
70 | free(s->path); | |
71 | free(s->description); | |
72 | free(s->pid_file); | |
73 | strv_free(s->before); | |
74 | strv_free(s->after); | |
75 | strv_free(s->wants); | |
76 | strv_free(s->wanted_by); | |
8fba1c8d ZJS |
77 | free(s); |
78 | } | |
79 | ||
80 | DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, free_sysvstub); | |
81 | ||
82 | static void free_sysvstub_hashmapp(Hashmap **h) { | |
224b0e7a | 83 | hashmap_free_with_destructor(*h, free_sysvstub); |
8fba1c8d | 84 | } |
95ed3294 | 85 | |
b7e71846 | 86 | static int add_alias(const char *service, const char *alias) { |
c279613f | 87 | const char *link; |
b7e71846 MB |
88 | int r; |
89 | ||
90 | assert(service); | |
91 | assert(alias); | |
92 | ||
c279613f | 93 | link = strjoina(arg_dest, "/", alias); |
b7e71846 MB |
94 | |
95 | r = symlink(service, link); | |
96 | if (r < 0) { | |
97 | if (errno == EEXIST) | |
98 | return 0; | |
c279613f | 99 | |
b7e71846 MB |
100 | return -errno; |
101 | } | |
102 | ||
103 | return 1; | |
104 | } | |
105 | ||
95ed3294 | 106 | static int generate_unit_file(SysvStub *s) { |
98bad05e | 107 | _cleanup_free_ char *path_escaped = NULL; |
95ed3294 | 108 | _cleanup_fclose_ FILE *f = NULL; |
c279613f LP |
109 | const char *unit; |
110 | char **p; | |
95ed3294 TA |
111 | int r; |
112 | ||
c279613f LP |
113 | assert(s); |
114 | ||
115 | if (!s->loaded) | |
116 | return 0; | |
117 | ||
98bad05e LP |
118 | path_escaped = specifier_escape(s->path); |
119 | if (!path_escaped) | |
120 | return log_oom(); | |
121 | ||
c279613f LP |
122 | unit = strjoina(arg_dest, "/", s->name); |
123 | ||
77354c7e MP |
124 | /* We might already have a symlink with the same name from a Provides:, |
125 | * or from backup files like /etc/init.d/foo.bak. Real scripts always win, | |
126 | * so remove an existing link */ | |
4e558983 | 127 | if (is_symlink(unit) > 0) { |
c279613f | 128 | log_warning("Overwriting existing symlink %s with real service.", unit); |
9993ef2e | 129 | (void) unlink(unit); |
77354c7e MP |
130 | } |
131 | ||
95ed3294 | 132 | f = fopen(unit, "wxe"); |
4a62c710 MS |
133 | if (!f) |
134 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
95ed3294 TA |
135 | |
136 | fprintf(f, | |
137 | "# Automatically generated by systemd-sysv-generator\n\n" | |
138 | "[Unit]\n" | |
aad0a2c8 | 139 | "Documentation=man:systemd-sysv-generator(8)\n" |
c279613f | 140 | "SourcePath=%s\n", |
98bad05e LP |
141 | path_escaped); |
142 | ||
143 | if (s->description) { | |
144 | _cleanup_free_ char *t; | |
c279613f | 145 | |
98bad05e LP |
146 | t = specifier_escape(s->description); |
147 | if (!t) | |
148 | return log_oom(); | |
149 | ||
150 | fprintf(f, "Description=%s\n", t); | |
151 | } | |
95ed3294 | 152 | |
c584ffc0 LN |
153 | STRV_FOREACH(p, s->before) |
154 | fprintf(f, "Before=%s\n", *p); | |
155 | STRV_FOREACH(p, s->after) | |
156 | fprintf(f, "After=%s\n", *p); | |
157 | STRV_FOREACH(p, s->wants) | |
158 | fprintf(f, "Wants=%s\n", *p); | |
95ed3294 TA |
159 | |
160 | fprintf(f, | |
161 | "\n[Service]\n" | |
162 | "Type=forking\n" | |
163 | "Restart=no\n" | |
164 | "TimeoutSec=5min\n" | |
165 | "IgnoreSIGPIPE=no\n" | |
166 | "KillMode=process\n" | |
167 | "GuessMainPID=no\n" | |
168 | "RemainAfterExit=%s\n", | |
169 | yes_no(!s->pid_file)); | |
170 | ||
98bad05e LP |
171 | if (s->pid_file) { |
172 | _cleanup_free_ char *t; | |
173 | ||
174 | t = specifier_escape(s->pid_file); | |
175 | if (!t) | |
176 | return log_oom(); | |
177 | ||
178 | fprintf(f, "PIDFile=%s\n", t); | |
179 | } | |
95ed3294 | 180 | |
41e2036e LP |
181 | /* Consider two special LSB exit codes a clean exit */ |
182 | if (s->has_lsb) | |
183 | fprintf(f, | |
184 | "SuccessExitStatus=%i %i\n", | |
185 | EXIT_NOTINSTALLED, | |
186 | EXIT_NOTCONFIGURED); | |
187 | ||
95ed3294 TA |
188 | fprintf(f, |
189 | "ExecStart=%s start\n" | |
190 | "ExecStop=%s stop\n", | |
98bad05e | 191 | path_escaped, path_escaped); |
95ed3294 TA |
192 | |
193 | if (s->reload) | |
98bad05e | 194 | fprintf(f, "ExecReload=%s reload\n", path_escaped); |
95ed3294 | 195 | |
c279613f LP |
196 | r = fflush_and_check(f); |
197 | if (r < 0) | |
198 | return log_error_errno(r, "Failed to write unit %s: %m", unit); | |
199 | ||
7f0cc637 ZJS |
200 | STRV_FOREACH(p, s->wanted_by) |
201 | (void) generator_add_symlink(arg_dest, *p, "wants", s->name); | |
95ed3294 | 202 | |
c279613f | 203 | return 1; |
95ed3294 TA |
204 | } |
205 | ||
206 | static bool usage_contains_reload(const char *line) { | |
207 | return (strcasestr(line, "{reload|") || | |
208 | strcasestr(line, "{reload}") || | |
209 | strcasestr(line, "{reload\"") || | |
210 | strcasestr(line, "|reload|") || | |
211 | strcasestr(line, "|reload}") || | |
212 | strcasestr(line, "|reload\"")); | |
213 | } | |
214 | ||
215 | static char *sysv_translate_name(const char *name) { | |
0b2ec8a3 DH |
216 | _cleanup_free_ char *c = NULL; |
217 | char *res; | |
95ed3294 | 218 | |
264581a2 FS |
219 | c = strdup(name); |
220 | if (!c) | |
0b2ec8a3 | 221 | return NULL; |
95ed3294 | 222 | |
0b2ec8a3 DH |
223 | res = endswith(c, ".sh"); |
224 | if (res) | |
225 | *res = 0; | |
95ed3294 | 226 | |
37cbc1d5 | 227 | if (unit_name_mangle(c, 0, &res) < 0) |
0b2ec8a3 DH |
228 | return NULL; |
229 | ||
230 | return res; | |
95ed3294 TA |
231 | } |
232 | ||
7532e6d4 | 233 | static int sysv_translate_facility(SysvStub *s, unsigned line, const char *name, char **ret) { |
95ed3294 TA |
234 | |
235 | /* We silently ignore the $ prefix here. According to the LSB | |
236 | * spec it simply indicates whether something is a | |
237 | * standardized name or a distribution-specific one. Since we | |
238 | * just follow what already exists and do not introduce new | |
239 | * uses or names we don't care who introduced a new name. */ | |
240 | ||
241 | static const char * const table[] = { | |
242 | /* LSB defined facilities */ | |
243 | "local_fs", NULL, | |
244 | "network", SPECIAL_NETWORK_ONLINE_TARGET, | |
245 | "named", SPECIAL_NSS_LOOKUP_TARGET, | |
246 | "portmap", SPECIAL_RPCBIND_TARGET, | |
247 | "remote_fs", SPECIAL_REMOTE_FS_TARGET, | |
248 | "syslog", NULL, | |
249 | "time", SPECIAL_TIME_SYNC_TARGET, | |
250 | }; | |
251 | ||
7532e6d4 | 252 | const char *filename; |
c279613f | 253 | char *filename_no_sh, *e, *m; |
95ed3294 | 254 | const char *n; |
4e488555 | 255 | unsigned i; |
c279613f | 256 | int r; |
95ed3294 TA |
257 | |
258 | assert(name); | |
7532e6d4 | 259 | assert(s); |
c279613f | 260 | assert(ret); |
95ed3294 | 261 | |
7532e6d4 FS |
262 | filename = basename(s->path); |
263 | ||
95ed3294 TA |
264 | n = *name == '$' ? name + 1 : name; |
265 | ||
266 | for (i = 0; i < ELEMENTSOF(table); i += 2) { | |
95ed3294 TA |
267 | if (!streq(table[i], n)) |
268 | continue; | |
269 | ||
e932f540 LP |
270 | if (!table[i+1]) { |
271 | *ret = NULL; | |
95ed3294 | 272 | return 0; |
e932f540 | 273 | } |
95ed3294 | 274 | |
c279613f LP |
275 | m = strdup(table[i+1]); |
276 | if (!m) | |
95ed3294 TA |
277 | return log_oom(); |
278 | ||
c279613f LP |
279 | *ret = m; |
280 | return 1; | |
281 | } | |
282 | ||
283 | /* If we don't know this name, fallback heuristics to figure | |
284 | * out whether something is a target or a service alias. */ | |
285 | ||
286 | /* Facilities starting with $ are most likely targets */ | |
287 | if (*name == '$') { | |
288 | r = unit_name_build(n, NULL, ".target", ret); | |
289 | if (r < 0) | |
7532e6d4 | 290 | return log_error_errno(r, "[%s:%u] Could not build name for facility %s: %m", s->path, line, name); |
c279613f | 291 | |
e932f540 | 292 | return 1; |
95ed3294 TA |
293 | } |
294 | ||
c279613f | 295 | /* Strip ".sh" suffix from file name for comparison */ |
4e488555 | 296 | filename_no_sh = strdupa(filename); |
40780877 | 297 | e = endswith(filename_no_sh, ".sh"); |
3315f085 | 298 | if (e) { |
4e488555 | 299 | *e = '\0'; |
3315f085 LP |
300 | filename = filename_no_sh; |
301 | } | |
29e0e6d8 | 302 | |
c279613f | 303 | /* Names equaling the file name of the services are redundant */ |
e932f540 LP |
304 | if (streq_ptr(n, filename)) { |
305 | *ret = NULL; | |
95ed3294 | 306 | return 0; |
e932f540 | 307 | } |
95ed3294 | 308 | |
c279613f LP |
309 | /* Everything else we assume to be normal service names */ |
310 | m = sysv_translate_name(n); | |
311 | if (!m) | |
312 | return log_oom(); | |
95ed3294 | 313 | |
c279613f | 314 | *ret = m; |
95ed3294 TA |
315 | return 1; |
316 | } | |
317 | ||
1e2fee5f | 318 | static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) { |
1e2fee5f ZJS |
319 | int r; |
320 | ||
c279613f LP |
321 | assert(s); |
322 | assert(full_text); | |
323 | assert(text); | |
1e2fee5f | 324 | |
c279613f LP |
325 | for (;;) { |
326 | _cleanup_free_ char *word = NULL, *m = NULL; | |
1e2fee5f | 327 | |
c279613f | 328 | r = extract_first_word(&text, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX); |
1e2fee5f | 329 | if (r < 0) |
7532e6d4 | 330 | return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line); |
1e2fee5f | 331 | if (r == 0) |
c279613f LP |
332 | break; |
333 | ||
7532e6d4 | 334 | r = sysv_translate_facility(s, line, word, &m); |
c279613f | 335 | if (r <= 0) /* continue on error */ |
1e2fee5f ZJS |
336 | continue; |
337 | ||
c279613f LP |
338 | switch (unit_name_to_type(m)) { |
339 | ||
340 | case UNIT_SERVICE: | |
1e2fee5f ZJS |
341 | log_debug("Adding Provides: alias '%s' for '%s'", m, s->name); |
342 | r = add_alias(s->name, m); | |
e987f2a8 | 343 | if (r < 0) |
f2341e0a | 344 | log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m); |
c279613f LP |
345 | break; |
346 | ||
347 | case UNIT_TARGET: | |
348 | ||
1e2fee5f ZJS |
349 | /* NB: SysV targets which are provided by a |
350 | * service are pulled in by the services, as | |
351 | * an indication that the generic service is | |
352 | * now available. This is strictly one-way. | |
353 | * The targets do NOT pull in SysV services! */ | |
c279613f | 354 | |
1e2fee5f ZJS |
355 | r = strv_extend(&s->before, m); |
356 | if (r < 0) | |
357 | return log_oom(); | |
c279613f | 358 | |
1e2fee5f ZJS |
359 | r = strv_extend(&s->wants, m); |
360 | if (r < 0) | |
361 | return log_oom(); | |
c279613f | 362 | |
1e2fee5f ZJS |
363 | if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET)) { |
364 | r = strv_extend(&s->before, SPECIAL_NETWORK_TARGET); | |
365 | if (r < 0) | |
366 | return log_oom(); | |
bd9ad4ff LN |
367 | r = strv_extend(&s->wants, SPECIAL_NETWORK_TARGET); |
368 | if (r < 0) | |
369 | return log_oom(); | |
1e2fee5f | 370 | } |
c279613f LP |
371 | |
372 | break; | |
373 | ||
374 | case _UNIT_TYPE_INVALID: | |
2c09a745 | 375 | log_warning("Unit name '%s' is invalid", m); |
c279613f LP |
376 | break; |
377 | ||
378 | default: | |
2c09a745 | 379 | log_warning("Unknown unit type for unit '%s'", m); |
c279613f | 380 | } |
1e2fee5f | 381 | } |
c279613f | 382 | |
1e2fee5f ZJS |
383 | return 0; |
384 | } | |
385 | ||
386 | static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) { | |
1e2fee5f ZJS |
387 | int r; |
388 | ||
c279613f LP |
389 | assert(s); |
390 | assert(full_text); | |
391 | assert(text); | |
1e2fee5f | 392 | |
c279613f LP |
393 | for (;;) { |
394 | _cleanup_free_ char *word = NULL, *m = NULL; | |
395 | bool is_before; | |
1e2fee5f | 396 | |
c279613f LP |
397 | r = extract_first_word(&text, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX); |
398 | if (r < 0) | |
7532e6d4 | 399 | return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line); |
1e2fee5f | 400 | if (r == 0) |
c279613f LP |
401 | break; |
402 | ||
7532e6d4 | 403 | r = sysv_translate_facility(s, line, word, &m); |
c279613f | 404 | if (r <= 0) /* continue on error */ |
1e2fee5f ZJS |
405 | continue; |
406 | ||
407 | is_before = startswith_no_case(full_text, "X-Start-Before:"); | |
408 | ||
409 | if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET) && !is_before) { | |
410 | /* the network-online target is special, as it needs to be actively pulled in */ | |
411 | r = strv_extend(&s->after, m); | |
412 | if (r < 0) | |
413 | return log_oom(); | |
c279613f | 414 | |
1e2fee5f | 415 | r = strv_extend(&s->wants, m); |
e987f2a8 | 416 | } else |
1e2fee5f | 417 | r = strv_extend(is_before ? &s->before : &s->after, m); |
1e2fee5f | 418 | if (r < 0) |
e987f2a8 | 419 | return log_oom(); |
1e2fee5f | 420 | } |
c279613f | 421 | |
1e2fee5f ZJS |
422 | return 0; |
423 | } | |
424 | ||
95ed3294 TA |
425 | static int load_sysv(SysvStub *s) { |
426 | _cleanup_fclose_ FILE *f; | |
427 | unsigned line = 0; | |
428 | int r; | |
429 | enum { | |
430 | NORMAL, | |
431 | DESCRIPTION, | |
432 | LSB, | |
433 | LSB_DESCRIPTION, | |
434 | USAGE_CONTINUATION | |
435 | } state = NORMAL; | |
436 | _cleanup_free_ char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL; | |
437 | char *description; | |
438 | bool supports_reload = false; | |
c279613f | 439 | char l[LINE_MAX]; |
95ed3294 TA |
440 | |
441 | assert(s); | |
442 | ||
443 | f = fopen(s->path, "re"); | |
c279613f LP |
444 | if (!f) { |
445 | if (errno == ENOENT) | |
446 | return 0; | |
95ed3294 | 447 | |
c279613f LP |
448 | return log_error_errno(errno, "Failed to open %s: %m", s->path); |
449 | } | |
77354c7e | 450 | |
c279613f | 451 | log_debug("Loading SysV script %s", s->path); |
95ed3294 | 452 | |
c279613f LP |
453 | FOREACH_LINE(l, f, goto fail) { |
454 | char *t; | |
95ed3294 TA |
455 | |
456 | line++; | |
457 | ||
458 | t = strstrip(l); | |
459 | if (*t != '#') { | |
460 | /* Try to figure out whether this init script supports | |
461 | * the reload operation. This heuristic looks for | |
462 | * "Usage" lines which include the reload option. */ | |
463 | if ( state == USAGE_CONTINUATION || | |
464 | (state == NORMAL && strcasestr(t, "usage"))) { | |
465 | if (usage_contains_reload(t)) { | |
466 | supports_reload = true; | |
467 | state = NORMAL; | |
468 | } else if (t[strlen(t)-1] == '\\') | |
469 | state = USAGE_CONTINUATION; | |
470 | else | |
471 | state = NORMAL; | |
472 | } | |
473 | ||
474 | continue; | |
475 | } | |
476 | ||
477 | if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) { | |
478 | state = LSB; | |
479 | s->has_lsb = true; | |
480 | continue; | |
481 | } | |
482 | ||
3742095b | 483 | if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(t, "### END INIT INFO")) { |
95ed3294 TA |
484 | state = NORMAL; |
485 | continue; | |
486 | } | |
487 | ||
488 | t++; | |
489 | t += strspn(t, WHITESPACE); | |
490 | ||
491 | if (state == NORMAL) { | |
492 | ||
493 | /* Try to parse Red Hat style description */ | |
494 | ||
495 | if (startswith_no_case(t, "description:")) { | |
496 | ||
c279613f | 497 | size_t k; |
859fdd1e | 498 | const char *j; |
95ed3294 | 499 | |
c279613f LP |
500 | k = strlen(t); |
501 | if (k > 0 && t[k-1] == '\\') { | |
95ed3294 TA |
502 | state = DESCRIPTION; |
503 | t[k-1] = 0; | |
504 | } | |
505 | ||
3c6f7c34 | 506 | j = empty_to_null(strstrip(t+12)); |
95ed3294 | 507 | |
c279613f LP |
508 | r = free_and_strdup(&chkconfig_description, j); |
509 | if (r < 0) | |
510 | return log_oom(); | |
95ed3294 TA |
511 | |
512 | } else if (startswith_no_case(t, "pidfile:")) { | |
c279613f | 513 | const char *fn; |
95ed3294 TA |
514 | |
515 | state = NORMAL; | |
516 | ||
517 | fn = strstrip(t+8); | |
518 | if (!path_is_absolute(fn)) { | |
f2341e0a | 519 | log_error("[%s:%u] PID file not absolute. Ignoring.", s->path, line); |
95ed3294 TA |
520 | continue; |
521 | } | |
522 | ||
c279613f LP |
523 | r = free_and_strdup(&s->pid_file, fn); |
524 | if (r < 0) | |
525 | return log_oom(); | |
95ed3294 TA |
526 | } |
527 | ||
528 | } else if (state == DESCRIPTION) { | |
529 | ||
530 | /* Try to parse Red Hat style description | |
531 | * continuation */ | |
532 | ||
c279613f | 533 | size_t k; |
95ed3294 TA |
534 | char *j; |
535 | ||
c279613f LP |
536 | k = strlen(t); |
537 | if (k > 0 && t[k-1] == '\\') | |
95ed3294 TA |
538 | t[k-1] = 0; |
539 | else | |
540 | state = NORMAL; | |
541 | ||
542 | j = strstrip(t); | |
c279613f | 543 | if (!isempty(j)) { |
95ed3294 TA |
544 | char *d = NULL; |
545 | ||
546 | if (chkconfig_description) | |
605405c6 | 547 | d = strjoin(chkconfig_description, " ", j); |
95ed3294 TA |
548 | else |
549 | d = strdup(j); | |
95ed3294 | 550 | if (!d) |
c279613f | 551 | return log_oom(); |
95ed3294 TA |
552 | |
553 | free(chkconfig_description); | |
554 | chkconfig_description = d; | |
555 | } | |
556 | ||
3742095b | 557 | } else if (IN_SET(state, LSB, LSB_DESCRIPTION)) { |
95ed3294 TA |
558 | |
559 | if (startswith_no_case(t, "Provides:")) { | |
95ed3294 TA |
560 | state = LSB; |
561 | ||
1e2fee5f ZJS |
562 | r = handle_provides(s, line, t, t + 9); |
563 | if (r < 0) | |
564 | return r; | |
c279613f | 565 | |
95ed3294 TA |
566 | } else if (startswith_no_case(t, "Required-Start:") || |
567 | startswith_no_case(t, "Should-Start:") || | |
568 | startswith_no_case(t, "X-Start-Before:") || | |
569 | startswith_no_case(t, "X-Start-After:")) { | |
95ed3294 TA |
570 | |
571 | state = LSB; | |
572 | ||
1e2fee5f ZJS |
573 | r = handle_dependencies(s, line, t, strchr(t, ':') + 1); |
574 | if (r < 0) | |
575 | return r; | |
95ed3294 | 576 | |
95ed3294 | 577 | } else if (startswith_no_case(t, "Description:")) { |
c279613f | 578 | const char *j; |
95ed3294 TA |
579 | |
580 | state = LSB_DESCRIPTION; | |
581 | ||
3c6f7c34 | 582 | j = empty_to_null(strstrip(t+12)); |
95ed3294 | 583 | |
c279613f LP |
584 | r = free_and_strdup(&long_description, j); |
585 | if (r < 0) | |
586 | return log_oom(); | |
95ed3294 TA |
587 | |
588 | } else if (startswith_no_case(t, "Short-Description:")) { | |
c279613f | 589 | const char *j; |
95ed3294 TA |
590 | |
591 | state = LSB; | |
592 | ||
3c6f7c34 | 593 | j = empty_to_null(strstrip(t+18)); |
95ed3294 | 594 | |
c279613f LP |
595 | r = free_and_strdup(&short_description, j); |
596 | if (r < 0) | |
597 | return log_oom(); | |
95ed3294 TA |
598 | |
599 | } else if (state == LSB_DESCRIPTION) { | |
600 | ||
601 | if (startswith(l, "#\t") || startswith(l, "# ")) { | |
c279613f | 602 | const char *j; |
95ed3294 TA |
603 | |
604 | j = strstrip(t); | |
c279613f | 605 | if (!isempty(j)) { |
95ed3294 TA |
606 | char *d = NULL; |
607 | ||
608 | if (long_description) | |
605405c6 | 609 | d = strjoin(long_description, " ", t); |
95ed3294 TA |
610 | else |
611 | d = strdup(j); | |
95ed3294 | 612 | if (!d) |
c279613f | 613 | return log_oom(); |
95ed3294 TA |
614 | |
615 | free(long_description); | |
616 | long_description = d; | |
617 | } | |
618 | ||
619 | } else | |
620 | state = LSB; | |
621 | } | |
622 | } | |
623 | } | |
624 | ||
625 | s->reload = supports_reload; | |
626 | ||
627 | /* We use the long description only if | |
628 | * no short description is set. */ | |
629 | ||
630 | if (short_description) | |
631 | description = short_description; | |
632 | else if (chkconfig_description) | |
633 | description = chkconfig_description; | |
634 | else if (long_description) | |
635 | description = long_description; | |
636 | else | |
637 | description = NULL; | |
638 | ||
639 | if (description) { | |
640 | char *d; | |
641 | ||
642 | d = strappend(s->has_lsb ? "LSB: " : "SYSV: ", description); | |
643 | if (!d) | |
c279613f | 644 | return log_oom(); |
95ed3294 TA |
645 | |
646 | s->description = d; | |
647 | } | |
648 | ||
c279613f | 649 | s->loaded = true; |
95ed3294 | 650 | return 0; |
c279613f LP |
651 | |
652 | fail: | |
653 | return log_error_errno(errno, "Failed to read configuration file '%s': %m", s->path); | |
95ed3294 TA |
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 | } |