]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sysv-generator/sysv-generator.c
systemctl: fix silent failure when --root is not found
[thirdparty/systemd.git] / src / sysv-generator / sysv-generator.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
95ed3294
TA
2
3#include <errno.h>
4#include <stdio.h>
5#include <unistd.h>
6
b5efdb8a 7#include "alloc-util.h"
c279613f 8#include "dirent-util.h"
41e2036e 9#include "exit-status.h"
3ffd4af2 10#include "fd-util.h"
c279613f 11#include "fileio.h"
7f0cc637 12#include "generator.h"
07630cea 13#include "hashmap.h"
8fcde012 14#include "hexdecoct.h"
07630cea
LP
15#include "install.h"
16#include "log.h"
5e332028 17#include "main-func.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
30static 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
7a44c7e3 45static const char *arg_dest = NULL;
8fba1c8d 46
95ed3294
TA
47typedef 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
75db809a 62static SysvStub* free_sysvstub(SysvStub *s) {
c279613f 63 if (!s)
75db809a 64 return NULL;
c279613f 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);
75db809a 74 return mfree(s);
8fba1c8d 75}
8fba1c8d
ZJS
76DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, free_sysvstub);
77
78static void free_sysvstub_hashmapp(Hashmap **h) {
224b0e7a 79 hashmap_free_with_destructor(*h, free_sysvstub);
8fba1c8d 80}
95ed3294 81
b7e71846 82static int add_alias(const char *service, const char *alias) {
c279613f 83 const char *link;
b7e71846
MB
84 int r;
85
86 assert(service);
87 assert(alias);
88
270384b2 89 link = prefix_roota(arg_dest, alias);
b7e71846
MB
90
91 r = symlink(service, link);
92 if (r < 0) {
93 if (errno == EEXIST)
94 return 0;
c279613f 95
b7e71846
MB
96 return -errno;
97 }
98
99 return 1;
100}
101
95ed3294 102static int generate_unit_file(SysvStub *s) {
98bad05e 103 _cleanup_free_ char *path_escaped = NULL;
95ed3294 104 _cleanup_fclose_ FILE *f = NULL;
c279613f 105 const char *unit;
95ed3294
TA
106 int r;
107
c279613f
LP
108 assert(s);
109
110 if (!s->loaded)
111 return 0;
112
98bad05e
LP
113 path_escaped = specifier_escape(s->path);
114 if (!path_escaped)
115 return log_oom();
116
270384b2 117 unit = prefix_roota(arg_dest, s->name);
c279613f 118
77354c7e
MP
119 /* We might already have a symlink with the same name from a Provides:,
120 * or from backup files like /etc/init.d/foo.bak. Real scripts always win,
121 * so remove an existing link */
4e558983 122 if (is_symlink(unit) > 0) {
c279613f 123 log_warning("Overwriting existing symlink %s with real service.", unit);
9993ef2e 124 (void) unlink(unit);
77354c7e
MP
125 }
126
95ed3294 127 f = fopen(unit, "wxe");
4a62c710
MS
128 if (!f)
129 return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
95ed3294
TA
130
131 fprintf(f,
132 "# Automatically generated by systemd-sysv-generator\n\n"
133 "[Unit]\n"
aad0a2c8 134 "Documentation=man:systemd-sysv-generator(8)\n"
c279613f 135 "SourcePath=%s\n",
98bad05e
LP
136 path_escaped);
137
138 if (s->description) {
c2b2df60 139 _cleanup_free_ char *t = NULL;
c279613f 140
98bad05e
LP
141 t = specifier_escape(s->description);
142 if (!t)
143 return log_oom();
144
145 fprintf(f, "Description=%s\n", t);
146 }
95ed3294 147
c584ffc0
LN
148 STRV_FOREACH(p, s->before)
149 fprintf(f, "Before=%s\n", *p);
150 STRV_FOREACH(p, s->after)
151 fprintf(f, "After=%s\n", *p);
152 STRV_FOREACH(p, s->wants)
153 fprintf(f, "Wants=%s\n", *p);
95ed3294
TA
154
155 fprintf(f,
156 "\n[Service]\n"
157 "Type=forking\n"
158 "Restart=no\n"
159 "TimeoutSec=5min\n"
160 "IgnoreSIGPIPE=no\n"
161 "KillMode=process\n"
162 "GuessMainPID=no\n"
163 "RemainAfterExit=%s\n",
164 yes_no(!s->pid_file));
165
98bad05e 166 if (s->pid_file) {
c2b2df60 167 _cleanup_free_ char *t = NULL;
98bad05e
LP
168
169 t = specifier_escape(s->pid_file);
170 if (!t)
171 return log_oom();
172
173 fprintf(f, "PIDFile=%s\n", t);
174 }
95ed3294 175
41e2036e
LP
176 /* Consider two special LSB exit codes a clean exit */
177 if (s->has_lsb)
178 fprintf(f,
179 "SuccessExitStatus=%i %i\n",
180 EXIT_NOTINSTALLED,
181 EXIT_NOTCONFIGURED);
182
95ed3294
TA
183 fprintf(f,
184 "ExecStart=%s start\n"
185 "ExecStop=%s stop\n",
98bad05e 186 path_escaped, path_escaped);
95ed3294
TA
187
188 if (s->reload)
98bad05e 189 fprintf(f, "ExecReload=%s reload\n", path_escaped);
95ed3294 190
c279613f
LP
191 r = fflush_and_check(f);
192 if (r < 0)
193 return log_error_errno(r, "Failed to write unit %s: %m", unit);
194
7f0cc637
ZJS
195 STRV_FOREACH(p, s->wanted_by)
196 (void) generator_add_symlink(arg_dest, *p, "wants", s->name);
95ed3294 197
c279613f 198 return 1;
95ed3294
TA
199}
200
201static bool usage_contains_reload(const char *line) {
202 return (strcasestr(line, "{reload|") ||
203 strcasestr(line, "{reload}") ||
204 strcasestr(line, "{reload\"") ||
205 strcasestr(line, "|reload|") ||
206 strcasestr(line, "|reload}") ||
207 strcasestr(line, "|reload\""));
208}
209
210static char *sysv_translate_name(const char *name) {
0b2ec8a3
DH
211 _cleanup_free_ char *c = NULL;
212 char *res;
95ed3294 213
264581a2
FS
214 c = strdup(name);
215 if (!c)
0b2ec8a3 216 return NULL;
95ed3294 217
0b2ec8a3
DH
218 res = endswith(c, ".sh");
219 if (res)
220 *res = 0;
95ed3294 221
37cbc1d5 222 if (unit_name_mangle(c, 0, &res) < 0)
0b2ec8a3
DH
223 return NULL;
224
225 return res;
95ed3294
TA
226}
227
7532e6d4 228static int sysv_translate_facility(SysvStub *s, unsigned line, const char *name, char **ret) {
95ed3294
TA
229
230 /* We silently ignore the $ prefix here. According to the LSB
231 * spec it simply indicates whether something is a
232 * standardized name or a distribution-specific one. Since we
233 * just follow what already exists and do not introduce new
234 * uses or names we don't care who introduced a new name. */
235
236 static const char * const table[] = {
237 /* LSB defined facilities */
238 "local_fs", NULL,
239 "network", SPECIAL_NETWORK_ONLINE_TARGET,
240 "named", SPECIAL_NSS_LOOKUP_TARGET,
241 "portmap", SPECIAL_RPCBIND_TARGET,
242 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
243 "syslog", NULL,
244 "time", SPECIAL_TIME_SYNC_TARGET,
245 };
246
7532e6d4 247 const char *filename;
c279613f 248 char *filename_no_sh, *e, *m;
95ed3294 249 const char *n;
4e488555 250 unsigned i;
c279613f 251 int r;
95ed3294
TA
252
253 assert(name);
7532e6d4 254 assert(s);
c279613f 255 assert(ret);
95ed3294 256
7532e6d4
FS
257 filename = basename(s->path);
258
95ed3294
TA
259 n = *name == '$' ? name + 1 : name;
260
261 for (i = 0; i < ELEMENTSOF(table); i += 2) {
95ed3294
TA
262 if (!streq(table[i], n))
263 continue;
264
e932f540
LP
265 if (!table[i+1]) {
266 *ret = NULL;
95ed3294 267 return 0;
e932f540 268 }
95ed3294 269
c279613f
LP
270 m = strdup(table[i+1]);
271 if (!m)
95ed3294
TA
272 return log_oom();
273
c279613f
LP
274 *ret = m;
275 return 1;
276 }
277
278 /* If we don't know this name, fallback heuristics to figure
279 * out whether something is a target or a service alias. */
280
281 /* Facilities starting with $ are most likely targets */
282 if (*name == '$') {
283 r = unit_name_build(n, NULL, ".target", ret);
284 if (r < 0)
7532e6d4 285 return log_error_errno(r, "[%s:%u] Could not build name for facility %s: %m", s->path, line, name);
c279613f 286
e932f540 287 return 1;
95ed3294
TA
288 }
289
c279613f 290 /* Strip ".sh" suffix from file name for comparison */
2f82562b 291 filename_no_sh = strdupa_safe(filename);
40780877 292 e = endswith(filename_no_sh, ".sh");
3315f085 293 if (e) {
4e488555 294 *e = '\0';
3315f085
LP
295 filename = filename_no_sh;
296 }
29e0e6d8 297
c279613f 298 /* Names equaling the file name of the services are redundant */
e932f540
LP
299 if (streq_ptr(n, filename)) {
300 *ret = NULL;
95ed3294 301 return 0;
e932f540 302 }
95ed3294 303
c279613f
LP
304 /* Everything else we assume to be normal service names */
305 m = sysv_translate_name(n);
306 if (!m)
307 return log_oom();
95ed3294 308
c279613f 309 *ret = m;
95ed3294
TA
310 return 1;
311}
312
1e2fee5f 313static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) {
1e2fee5f
ZJS
314 int r;
315
c279613f
LP
316 assert(s);
317 assert(full_text);
318 assert(text);
1e2fee5f 319
c279613f
LP
320 for (;;) {
321 _cleanup_free_ char *word = NULL, *m = NULL;
1e2fee5f 322
4ec85141 323 r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
1e2fee5f 324 if (r < 0)
7532e6d4 325 return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line);
1e2fee5f 326 if (r == 0)
c279613f
LP
327 break;
328
7532e6d4 329 r = sysv_translate_facility(s, line, word, &m);
c279613f 330 if (r <= 0) /* continue on error */
1e2fee5f
ZJS
331 continue;
332
c279613f
LP
333 switch (unit_name_to_type(m)) {
334
335 case UNIT_SERVICE:
1e2fee5f
ZJS
336 log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
337 r = add_alias(s->name, m);
e987f2a8 338 if (r < 0)
f2341e0a 339 log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m);
c279613f
LP
340 break;
341
342 case UNIT_TARGET:
343
1e2fee5f
ZJS
344 /* NB: SysV targets which are provided by a
345 * service are pulled in by the services, as
346 * an indication that the generic service is
347 * now available. This is strictly one-way.
348 * The targets do NOT pull in SysV services! */
c279613f 349
1e2fee5f
ZJS
350 r = strv_extend(&s->before, m);
351 if (r < 0)
352 return log_oom();
c279613f 353
1e2fee5f
ZJS
354 r = strv_extend(&s->wants, m);
355 if (r < 0)
356 return log_oom();
c279613f 357
1e2fee5f
ZJS
358 if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET)) {
359 r = strv_extend(&s->before, SPECIAL_NETWORK_TARGET);
360 if (r < 0)
361 return log_oom();
bd9ad4ff
LN
362 r = strv_extend(&s->wants, SPECIAL_NETWORK_TARGET);
363 if (r < 0)
364 return log_oom();
1e2fee5f 365 }
c279613f
LP
366
367 break;
368
369 case _UNIT_TYPE_INVALID:
2c09a745 370 log_warning("Unit name '%s' is invalid", m);
c279613f
LP
371 break;
372
373 default:
2c09a745 374 log_warning("Unknown unit type for unit '%s'", m);
c279613f 375 }
1e2fee5f 376 }
c279613f 377
1e2fee5f
ZJS
378 return 0;
379}
380
381static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) {
1e2fee5f
ZJS
382 int r;
383
c279613f
LP
384 assert(s);
385 assert(full_text);
386 assert(text);
1e2fee5f 387
c279613f
LP
388 for (;;) {
389 _cleanup_free_ char *word = NULL, *m = NULL;
390 bool is_before;
1e2fee5f 391
4ec85141 392 r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
c279613f 393 if (r < 0)
7532e6d4 394 return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line);
1e2fee5f 395 if (r == 0)
c279613f
LP
396 break;
397
7532e6d4 398 r = sysv_translate_facility(s, line, word, &m);
c279613f 399 if (r <= 0) /* continue on error */
1e2fee5f
ZJS
400 continue;
401
402 is_before = startswith_no_case(full_text, "X-Start-Before:");
403
404 if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET) && !is_before) {
405 /* the network-online target is special, as it needs to be actively pulled in */
406 r = strv_extend(&s->after, m);
407 if (r < 0)
408 return log_oom();
c279613f 409
1e2fee5f 410 r = strv_extend(&s->wants, m);
e987f2a8 411 } else
1e2fee5f 412 r = strv_extend(is_before ? &s->before : &s->after, m);
1e2fee5f 413 if (r < 0)
e987f2a8 414 return log_oom();
1e2fee5f 415 }
c279613f 416
1e2fee5f
ZJS
417 return 0;
418}
419
95ed3294 420static int load_sysv(SysvStub *s) {
c2b2df60 421 _cleanup_fclose_ FILE *f = NULL;
95ed3294
TA
422 unsigned line = 0;
423 int r;
424 enum {
425 NORMAL,
426 DESCRIPTION,
427 LSB,
428 LSB_DESCRIPTION,
429 USAGE_CONTINUATION
430 } state = NORMAL;
431 _cleanup_free_ char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL;
432 char *description;
433 bool supports_reload = false;
434
435 assert(s);
436
437 f = fopen(s->path, "re");
c279613f
LP
438 if (!f) {
439 if (errno == ENOENT)
440 return 0;
95ed3294 441
c279613f
LP
442 return log_error_errno(errno, "Failed to open %s: %m", s->path);
443 }
77354c7e 444
c279613f 445 log_debug("Loading SysV script %s", s->path);
95ed3294 446
e393eff6
LP
447 for (;;) {
448 _cleanup_free_ char *l = NULL;
c279613f 449 char *t;
95ed3294 450
e393eff6
LP
451 r = read_line(f, LONG_LINE_MAX, &l);
452 if (r < 0)
453 return log_error_errno(r, "Failed to read configuration file '%s': %m", s->path);
454 if (r == 0)
455 break;
456
95ed3294
TA
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. */
e393eff6 464 if (state == USAGE_CONTINUATION ||
95ed3294
TA
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;
580bf613 535 const char *j;
95ed3294 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);
580bf613
YW
544 if (!isempty(j) && !strextend_with_separator(&chkconfig_description, " ", j))
545 return log_oom();
95ed3294 546
3742095b 547 } else if (IN_SET(state, LSB, LSB_DESCRIPTION)) {
95ed3294
TA
548
549 if (startswith_no_case(t, "Provides:")) {
95ed3294
TA
550 state = LSB;
551
1e2fee5f
ZJS
552 r = handle_provides(s, line, t, t + 9);
553 if (r < 0)
554 return r;
c279613f 555
95ed3294
TA
556 } else if (startswith_no_case(t, "Required-Start:") ||
557 startswith_no_case(t, "Should-Start:") ||
558 startswith_no_case(t, "X-Start-Before:") ||
559 startswith_no_case(t, "X-Start-After:")) {
95ed3294
TA
560
561 state = LSB;
562
1e2fee5f
ZJS
563 r = handle_dependencies(s, line, t, strchr(t, ':') + 1);
564 if (r < 0)
565 return r;
95ed3294 566
95ed3294 567 } else if (startswith_no_case(t, "Description:")) {
c279613f 568 const char *j;
95ed3294
TA
569
570 state = LSB_DESCRIPTION;
571
3c6f7c34 572 j = empty_to_null(strstrip(t+12));
95ed3294 573
c279613f
LP
574 r = free_and_strdup(&long_description, j);
575 if (r < 0)
576 return log_oom();
95ed3294
TA
577
578 } else if (startswith_no_case(t, "Short-Description:")) {
c279613f 579 const char *j;
95ed3294
TA
580
581 state = LSB;
582
3c6f7c34 583 j = empty_to_null(strstrip(t+18));
95ed3294 584
c279613f
LP
585 r = free_and_strdup(&short_description, j);
586 if (r < 0)
587 return log_oom();
95ed3294
TA
588
589 } else if (state == LSB_DESCRIPTION) {
590
591 if (startswith(l, "#\t") || startswith(l, "# ")) {
c279613f 592 const char *j;
95ed3294
TA
593
594 j = strstrip(t);
580bf613
YW
595 if (!isempty(j) && !strextend_with_separator(&long_description, " ", j))
596 return log_oom();
95ed3294
TA
597 } else
598 state = LSB;
599 }
600 }
601 }
602
603 s->reload = supports_reload;
604
605 /* We use the long description only if
606 * no short description is set. */
607
608 if (short_description)
609 description = short_description;
610 else if (chkconfig_description)
611 description = chkconfig_description;
612 else if (long_description)
613 description = long_description;
614 else
615 description = NULL;
616
617 if (description) {
618 char *d;
619
b910cc72 620 d = strjoin(s->has_lsb ? "LSB: " : "SYSV: ", description);
95ed3294 621 if (!d)
c279613f 622 return log_oom();
95ed3294
TA
623
624 s->description = d;
625 }
626
c279613f 627 s->loaded = true;
95ed3294
TA
628 return 0;
629}
630
631static int fix_order(SysvStub *s, Hashmap *all_services) {
632 SysvStub *other;
95ed3294
TA
633 int r;
634
635 assert(s);
636
c279613f
LP
637 if (!s->loaded)
638 return 0;
639
95ed3294
TA
640 if (s->sysv_start_priority < 0)
641 return 0;
642
90e74a66 643 HASHMAP_FOREACH(other, all_services) {
95ed3294
TA
644 if (s == other)
645 continue;
646
c279613f
LP
647 if (!other->loaded)
648 continue;
649
95ed3294
TA
650 if (other->sysv_start_priority < 0)
651 continue;
652
653 /* If both units have modern headers we don't care
654 * about the priorities */
655 if (s->has_lsb && other->has_lsb)
656 continue;
657
658 if (other->sysv_start_priority < s->sysv_start_priority) {
659 r = strv_extend(&s->after, other->name);
660 if (r < 0)
661 return log_oom();
c279613f
LP
662
663 } else if (other->sysv_start_priority > s->sysv_start_priority) {
95ed3294
TA
664 r = strv_extend(&s->before, other->name);
665 if (r < 0)
666 return log_oom();
c279613f 667 } else
95ed3294
TA
668 continue;
669
670 /* FIXME: Maybe we should compare the name here lexicographically? */
671 }
672
673 return 0;
674}
675
4143c6c3
LP
676static int acquire_search_path(const char *def, const char *envvar, char ***ret) {
677 _cleanup_strv_free_ char **l = NULL;
678 const char *e;
679 int r;
680
681 assert(def);
682 assert(envvar);
683
684 e = getenv(envvar);
685 if (e) {
686 r = path_split_and_make_absolute(e, &l);
687 if (r < 0)
688 return log_error_errno(r, "Failed to make $%s search path absolute: %m", envvar);
689 }
690
691 if (strv_isempty(l)) {
692 strv_free(l);
693
bea1a013 694 l = strv_new(def);
4143c6c3
LP
695 if (!l)
696 return log_oom();
697 }
698
699 if (!path_strv_resolve_uniq(l, NULL))
700 return log_oom();
701
ae2a15bc 702 *ret = TAKE_PTR(l);
4143c6c3
LP
703
704 return 0;
705}
706
a8ffe6fb 707static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
4143c6c3 708 _cleanup_strv_free_ char **sysvinit_path = NULL;
0ec0deaa 709 int r;
95ed3294 710
c279613f 711 assert(lp);
c279613f 712
4143c6c3
LP
713 r = acquire_search_path(SYSTEM_SYSVINIT_PATH, "SYSTEMD_SYSVINIT_PATH", &sysvinit_path);
714 if (r < 0)
715 return r;
716
717 STRV_FOREACH(path, sysvinit_path) {
95ed3294 718 _cleanup_closedir_ DIR *d = NULL;
95ed3294
TA
719
720 d = opendir(*path);
721 if (!d) {
722 if (errno != ENOENT)
c279613f 723 log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path);
95ed3294
TA
724 continue;
725 }
726
c279613f 727 FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) {
95ed3294 728 _cleanup_free_ char *fpath = NULL, *name = NULL;
8fba1c8d 729 _cleanup_(free_sysvstubp) SysvStub *service = NULL;
805e5dda 730 struct stat st;
95ed3294 731
7b729f86 732 if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
c279613f 733 log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name);
95ed3294 734 continue;
805e5dda 735 }
95ed3294
TA
736
737 if (!(st.st_mode & S_IXUSR))
738 continue;
739
805e5dda
LP
740 if (!S_ISREG(st.st_mode))
741 continue;
742
95ed3294
TA
743 name = sysv_translate_name(de->d_name);
744 if (!name)
745 return log_oom();
746
a986501b
LP
747 if (hashmap_contains(all_services, name))
748 continue;
749
5ae87056 750 r = unit_file_exists(UNIT_FILE_SYSTEM, lp, name);
76ec966f 751 if (r < 0 && !IN_SET(r, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) {
0ec0deaa
LP
752 log_debug_errno(r, "Failed to detect whether %s exists, skipping: %m", name);
753 continue;
3c6d8e57 754 } else if (r != 0) {
0ec0deaa 755 log_debug("Native unit for %s already exists, skipping.", name);
f4f01ec1
MP
756 continue;
757 }
758
657ee2d8 759 fpath = path_join(*path, de->d_name);
c279613f
LP
760 if (!fpath)
761 return log_oom();
762
0e42cbe2
LP
763 log_warning("SysV service '%s' lacks a native systemd unit file. "
764 "Automatically generating a unit file for compatibility. "
765 "Please update package to include a native systemd unit file, in order to make it more safe and robust.", fpath);
766
470ab28d 767 service = new(SysvStub, 1);
95ed3294
TA
768 if (!service)
769 return log_oom();
770
470ab28d
LP
771 *service = (SysvStub) {
772 .sysv_start_priority = -1,
773 .name = TAKE_PTR(name),
774 .path = TAKE_PTR(fpath),
775 };
95ed3294
TA
776
777 r = hashmap_put(all_services, service->name, service);
805e5dda 778 if (r < 0)
95ed3294
TA
779 return log_oom();
780
470ab28d 781 TAKE_PTR(service);
95ed3294
TA
782 }
783 }
784
785 return 0;
786}
787
a8ffe6fb 788static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) {
95ed3294 789 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
4143c6c3 790 _cleanup_strv_free_ char **sysvrcnd_path = NULL;
c279613f 791 SysvStub *service;
c279613f 792 int r;
95ed3294 793
c279613f
LP
794 assert(lp);
795
4143c6c3
LP
796 r = acquire_search_path(SYSTEM_SYSVRCND_PATH, "SYSTEMD_SYSVRCND_PATH", &sysvrcnd_path);
797 if (r < 0)
798 return r;
799
e198eba7
ZJS
800 STRV_FOREACH(p, sysvrcnd_path)
801 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
c279613f
LP
802 _cleanup_closedir_ DIR *d = NULL;
803 _cleanup_free_ char *path = NULL;
95ed3294 804
657ee2d8 805 path = path_join(*p, rcnd_table[i].path);
c279613f
LP
806 if (!path) {
807 r = log_oom();
808 goto finish;
809 }
95ed3294
TA
810
811 d = opendir(path);
812 if (!d) {
813 if (errno != ENOENT)
c279613f 814 log_warning_errno(errno, "Opening %s failed, ignoring: %m", path);
95ed3294
TA
815
816 continue;
817 }
818
8746820b 819 FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to enumerate directory %s, ignoring: %m", path)) {
c279613f 820 _cleanup_free_ char *name = NULL, *fpath = NULL;
95ed3294
TA
821 int a, b;
822
b9c59555 823 if (de->d_name[0] != 'S')
95ed3294
TA
824 continue;
825
826 if (strlen(de->d_name) < 4)
827 continue;
828
829 a = undecchar(de->d_name[1]);
830 b = undecchar(de->d_name[2]);
831
832 if (a < 0 || b < 0)
833 continue;
834
657ee2d8 835 fpath = path_join(*p, de->d_name);
95ed3294 836 if (!fpath) {
c279613f 837 r = log_oom();
95ed3294
TA
838 goto finish;
839 }
840
841 name = sysv_translate_name(de->d_name + 3);
842 if (!name) {
843 r = log_oom();
844 goto finish;
845 }
846
8c84621c 847 service = hashmap_get(all_services, name);
9ed794a3 848 if (!service) {
c279613f 849 log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name);
95ed3294
TA
850 continue;
851 }
852
b9c59555 853 service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority);
95ed3294 854
de7fef4b 855 r = set_ensure_put(&runlevel_services[i], NULL, service);
b9c59555
LP
856 if (r < 0) {
857 log_oom();
858 goto finish;
95ed3294
TA
859 }
860 }
861 }
862
e198eba7 863 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++)
90e74a66 864 SET_FOREACH(service, runlevel_services[i]) {
95ed3294 865 r = strv_extend(&service->before, rcnd_table[i].target);
c279613f
LP
866 if (r < 0) {
867 log_oom();
868 goto finish;
869 }
260ad50f 870 r = strv_extend(&service->wanted_by, rcnd_table[i].target);
c279613f
LP
871 if (r < 0) {
872 log_oom();
873 goto finish;
874 }
95ed3294
TA
875 }
876
95ed3294
TA
877 r = 0;
878
879finish:
e198eba7 880 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++)
95ed3294
TA
881 set_free(runlevel_services[i]);
882
883 return r;
884}
885
7a44c7e3 886static int run(const char *dest, const char *dest_early, const char *dest_late) {
5921fc3c 887 _cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
8e766630 888 _cleanup_(lookup_paths_free) LookupPaths lp = {};
95ed3294 889 SysvStub *service;
c279613f 890 int r;
95ed3294 891
7a44c7e3 892 assert_se(arg_dest = dest_late);
95ed3294 893
99aad9a2 894 r = lookup_paths_init_or_warn(&lp, UNIT_FILE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL);
47031c92 895 if (r < 0)
99aad9a2 896 return r;
95ed3294 897
d5099efc 898 all_services = hashmap_new(&string_hash_ops);
47031c92
ZJS
899 if (!all_services)
900 return log_oom();
95ed3294 901
a8ffe6fb 902 r = enumerate_sysv(&lp, all_services);
c279613f 903 if (r < 0)
47031c92 904 return r;
95ed3294 905
a8ffe6fb 906 r = set_dependencies_from_rcnd(&lp, all_services);
c279613f 907 if (r < 0)
47031c92 908 return r;
95ed3294 909
90e74a66 910 HASHMAP_FOREACH(service, all_services)
c279613f 911 (void) load_sysv(service);
b3fae863 912
90e74a66 913 HASHMAP_FOREACH(service, all_services) {
c279613f
LP
914 (void) fix_order(service, all_services);
915 (void) generate_unit_file(service);
95ed3294
TA
916 }
917
47031c92 918 return 0;
95ed3294 919}
47031c92 920
7a44c7e3 921DEFINE_MAIN_GENERATOR_FUNCTION(run);