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