1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "dirent-util.h"
9 #include "exit-status.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "generator.h"
13 #include "hashmap.h"
14 #include "hexdecoct.h"
15 #include "install.h"
16 #include "log.h"
17 #include "main-func.h"
18 #include "mkdir.h"
19 #include "path-lookup.h"
20 #include "path-util.h"
21 #include "set.h"
22 #include "special.h"
23 #include "specifier.h"
24 #include "stat-util.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "unit-name.h"
28 #include "util.h"
29
30 static const struct {
31 const char *path;
32 const char *target;
33 } rcnd_table[] = {
34 /* Standard SysV runlevels for start-up */
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. */
43 };
44
45 static const char *arg_dest = NULL;
46
47 typedef struct SysvStub {
48 char *name;
49 char *path;
50 char *description;
51 int sysv_start_priority;
52 char *pid_file;
53 char **before;
54 char **after;
55 char **wants;
56 char **wanted_by;
57 bool has_lsb;
58 bool reload;
59 bool loaded;
60 } SysvStub;
61
free_sysvstub(SysvStub * s)62 static SysvStub* free_sysvstub(SysvStub *s) {
63 if (!s)
64 return NULL;
65
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);
74 return mfree(s);
75 }
76 DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, free_sysvstub);
77
free_sysvstub_hashmapp(Hashmap ** h)78 static void free_sysvstub_hashmapp(Hashmap **h) {
79 hashmap_free_with_destructor(*h, free_sysvstub);
80 }
81
add_alias(const char * service,const char * alias)82 static int add_alias(const char *service, const char *alias) {
83 const char *link;
84 int r;
85
86 assert(service);
87 assert(alias);
88
89 link = prefix_roota(arg_dest, alias);
90
91 r = symlink(service, link);
92 if (r < 0) {
93 if (errno == EEXIST)
94 return 0;
95
96 return -errno;
97 }
98
99 return 1;
100 }
101
generate_unit_file(SysvStub * s)102 static int generate_unit_file(SysvStub *s) {
103 _cleanup_free_ char *path_escaped = NULL;
104 _cleanup_fclose_ FILE *f = NULL;
105 const char *unit;
106 int r;
107
108 assert(s);
109
110 if (!s->loaded)
111 return 0;
112
113 path_escaped = specifier_escape(s->path);
114 if (!path_escaped)
115 return log_oom();
116
117 unit = prefix_roota(arg_dest, s->name);
118
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 */
122 if (is_symlink(unit) > 0) {
123 log_warning("Overwriting existing symlink %s with real service.", unit);
124 (void) unlink(unit);
125 }
126
127 f = fopen(unit, "wxe");
128 if (!f)
129 return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
130
131 fprintf(f,
132 "# Automatically generated by systemd-sysv-generator\n\n"
133 "[Unit]\n"
134 "Documentation=man:systemd-sysv-generator(8)\n"
135 "SourcePath=%s\n",
136 path_escaped);
137
138 if (s->description) {
139 _cleanup_free_ char *t = NULL;
140
141 t = specifier_escape(s->description);
142 if (!t)
143 return log_oom();
144
145 fprintf(f, "Description=%s\n", t);
146 }
147
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);
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
166 if (s->pid_file) {
167 _cleanup_free_ char *t = NULL;
168
169 t = specifier_escape(s->pid_file);
170 if (!t)
171 return log_oom();
172
173 fprintf(f, "PIDFile=%s\n", t);
174 }
175
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
183 fprintf(f,
184 "ExecStart=%s start\n"
185 "ExecStop=%s stop\n",
186 path_escaped, path_escaped);
187
188 if (s->reload)
189 fprintf(f, "ExecReload=%s reload\n", path_escaped);
190
191 r = fflush_and_check(f);
192 if (r < 0)
193 return log_error_errno(r, "Failed to write unit %s: %m", unit);
194
195 STRV_FOREACH(p, s->wanted_by)
196 (void) generator_add_symlink(arg_dest, *p, "wants", s->name);
197
198 return 1;
199 }
200
usage_contains_reload(const char * line)201 static 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
sysv_translate_name(const char * name)210 static char *sysv_translate_name(const char *name) {
211 _cleanup_free_ char *c = NULL;
212 char *res;
213
214 c = strdup(name);
215 if (!c)
216 return NULL;
217
218 res = endswith(c, ".sh");
219 if (res)
220 *res = 0;
221
222 if (unit_name_mangle(c, 0, &res) < 0)
223 return NULL;
224
225 return res;
226 }
227
sysv_translate_facility(SysvStub * s,unsigned line,const char * name,char ** ret)228 static int sysv_translate_facility(SysvStub *s, unsigned line, const char *name, char **ret) {
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
247 const char *filename;
248 char *filename_no_sh, *e, *m;
249 const char *n;
250 unsigned i;
251 int r;
252
253 assert(name);
254 assert(s);
255 assert(ret);
256
257 filename = basename(s->path);
258
259 n = *name == '$' ? name + 1 : name;
260
261 for (i = 0; i < ELEMENTSOF(table); i += 2) {
262 if (!streq(table[i], n))
263 continue;
264
265 if (!table[i+1]) {
266 *ret = NULL;
267 return 0;
268 }
269
270 m = strdup(table[i+1]);
271 if (!m)
272 return log_oom();
273
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)
285 return log_error_errno(r, "[%s:%u] Could not build name for facility %s: %m", s->path, line, name);
286
287 return 1;
288 }
289
290 /* Strip ".sh" suffix from file name for comparison */
291 filename_no_sh = strdupa_safe(filename);
292 e = endswith(filename_no_sh, ".sh");
293 if (e) {
294 *e = '\0';
295 filename = filename_no_sh;
296 }
297
298 /* Names equaling the file name of the services are redundant */
299 if (streq_ptr(n, filename)) {
300 *ret = NULL;
301 return 0;
302 }
303
304 /* Everything else we assume to be normal service names */
305 m = sysv_translate_name(n);
306 if (!m)
307 return log_oom();
308
309 *ret = m;
310 return 1;
311 }
312
handle_provides(SysvStub * s,unsigned line,const char * full_text,const char * text)313 static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) {
314 int r;
315
316 assert(s);
317 assert(full_text);
318 assert(text);
319
320 for (;;) {
321 _cleanup_free_ char *word = NULL, *m = NULL;
322
323 r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
324 if (r < 0)
325 return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line);
326 if (r == 0)
327 break;
328
329 r = sysv_translate_facility(s, line, word, &m);
330 if (r <= 0) /* continue on error */
331 continue;
332
333 switch (unit_name_to_type(m)) {
334
335 case UNIT_SERVICE:
336 log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
337 r = add_alias(s->name, m);
338 if (r < 0)
339 log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m);
340 break;
341
342 case UNIT_TARGET:
343
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! */
349
350 r = strv_extend(&s->before, m);
351 if (r < 0)
352 return log_oom();
353
354 r = strv_extend(&s->wants, m);
355 if (r < 0)
356 return log_oom();
357
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();
362 r = strv_extend(&s->wants, SPECIAL_NETWORK_TARGET);
363 if (r < 0)
364 return log_oom();
365 }
366
367 break;
368
369 case _UNIT_TYPE_INVALID:
370 log_warning("Unit name '%s' is invalid", m);
371 break;
372
373 default:
374 log_warning("Unknown unit type for unit '%s'", m);
375 }
376 }
377
378 return 0;
379 }
380
handle_dependencies(SysvStub * s,unsigned line,const char * full_text,const char * text)381 static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) {
382 int r;
383
384 assert(s);
385 assert(full_text);
386 assert(text);
387
388 for (;;) {
389 _cleanup_free_ char *word = NULL, *m = NULL;
390 bool is_before;
391
392 r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
393 if (r < 0)
394 return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line);
395 if (r == 0)
396 break;
397
398 r = sysv_translate_facility(s, line, word, &m);
399 if (r <= 0) /* continue on error */
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();
409
410 r = strv_extend(&s->wants, m);
411 } else
412 r = strv_extend(is_before ? &s->before : &s->after, m);
413 if (r < 0)
414 return log_oom();
415 }
416
417 return 0;
418 }
419
load_sysv(SysvStub * s)420 static int load_sysv(SysvStub *s) {
421 _cleanup_fclose_ FILE *f = NULL;
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");
438 if (!f) {
439 if (errno == ENOENT)
440 return 0;
441
442 return log_error_errno(errno, "Failed to open %s: %m", s->path);
443 }
444
445 log_debug("Loading SysV script %s", s->path);
446
447 for (;;) {
448 _cleanup_free_ char *l = NULL;
449 char *t;
450
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
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
484 if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(t, "### END INIT INFO")) {
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
498 size_t k;
499 const char *j;
500
501 k = strlen(t);
502 if (k > 0 && t[k-1] == '\\') {
503 state = DESCRIPTION;
504 t[k-1] = 0;
505 }
506
507 j = empty_to_null(strstrip(t+12));
508
509 r = free_and_strdup(&chkconfig_description, j);
510 if (r < 0)
511 return log_oom();
512
513 } else if (startswith_no_case(t, "pidfile:")) {
514 const char *fn;
515
516 state = NORMAL;
517
518 fn = strstrip(t+8);
519 if (!path_is_absolute(fn)) {
520 log_error("[%s:%u] PID file not absolute. Ignoring.", s->path, line);
521 continue;
522 }
523
524 r = free_and_strdup(&s->pid_file, fn);
525 if (r < 0)
526 return log_oom();
527 }
528
529 } else if (state == DESCRIPTION) {
530
531 /* Try to parse Red Hat style description
532 * continuation */
533
534 size_t k;
535 const char *j;
536
537 k = strlen(t);
538 if (k > 0 && t[k-1] == '\\')
539 t[k-1] = 0;
540 else
541 state = NORMAL;
542
543 j = strstrip(t);
544 if (!isempty(j) && !strextend_with_separator(&chkconfig_description, " ", j))
545 return log_oom();
546
547 } else if (IN_SET(state, LSB, LSB_DESCRIPTION)) {
548
549 if (startswith_no_case(t, "Provides:")) {
550 state = LSB;
551
552 r = handle_provides(s, line, t, t + 9);
553 if (r < 0)
554 return r;
555
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:")) {
560
561 state = LSB;
562
563 r = handle_dependencies(s, line, t, strchr(t, ':') + 1);
564 if (r < 0)
565 return r;
566
567 } else if (startswith_no_case(t, "Description:")) {
568 const char *j;
569
570 state = LSB_DESCRIPTION;
571
572 j = empty_to_null(strstrip(t+12));
573
574 r = free_and_strdup(&long_description, j);
575 if (r < 0)
576 return log_oom();
577
578 } else if (startswith_no_case(t, "Short-Description:")) {
579 const char *j;
580
581 state = LSB;
582
583 j = empty_to_null(strstrip(t+18));
584
585 r = free_and_strdup(&short_description, j);
586 if (r < 0)
587 return log_oom();
588
589 } else if (state == LSB_DESCRIPTION) {
590
591 if (startswith(l, "#\t") || startswith(l, "# ")) {
592 const char *j;
593
594 j = strstrip(t);
595 if (!isempty(j) && !strextend_with_separator(&long_description, " ", j))
596 return log_oom();
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
620 d = strjoin(s->has_lsb ? "LSB: " : "SYSV: ", description);
621 if (!d)
622 return log_oom();
623
624 s->description = d;
625 }
626
627 s->loaded = true;
628 return 0;
629 }
630
fix_order(SysvStub * s,Hashmap * all_services)631 static int fix_order(SysvStub *s, Hashmap *all_services) {
632 SysvStub *other;
633 int r;
634
635 assert(s);
636
637 if (!s->loaded)
638 return 0;
639
640 if (s->sysv_start_priority < 0)
641 return 0;
642
643 HASHMAP_FOREACH(other, all_services) {
644 if (s == other)
645 continue;
646
647 if (!other->loaded)
648 continue;
649
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();
662
663 } else if (other->sysv_start_priority > s->sysv_start_priority) {
664 r = strv_extend(&s->before, other->name);
665 if (r < 0)
666 return log_oom();
667 } else
668 continue;
669
670 /* FIXME: Maybe we should compare the name here lexicographically? */
671 }
672
673 return 0;
674 }
675
acquire_search_path(const char * def,const char * envvar,char *** ret)676 static 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
694 l = strv_new(def);
695 if (!l)
696 return log_oom();
697 }
698
699 if (!path_strv_resolve_uniq(l, NULL))
700 return log_oom();
701
702 *ret = TAKE_PTR(l);
703
704 return 0;
705 }
706
enumerate_sysv(const LookupPaths * lp,Hashmap * all_services)707 static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
708 _cleanup_strv_free_ char **sysvinit_path = NULL;
709 int r;
710
711 assert(lp);
712
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) {
718 _cleanup_closedir_ DIR *d = NULL;
719
720 d = opendir(*path);
721 if (!d) {
722 if (errno != ENOENT)
723 log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path);
724 continue;
725 }
726
727 FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) {
728 _cleanup_free_ char *fpath = NULL, *name = NULL;
729 _cleanup_(free_sysvstubp) SysvStub *service = NULL;
730 struct stat st;
731
732 if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
733 log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name);
734 continue;
735 }
736
737 if (!(st.st_mode & S_IXUSR))
738 continue;
739
740 if (!S_ISREG(st.st_mode))
741 continue;
742
743 name = sysv_translate_name(de->d_name);
744 if (!name)
745 return log_oom();
746
747 if (hashmap_contains(all_services, name))
748 continue;
749
750 r = unit_file_exists(LOOKUP_SCOPE_SYSTEM, lp, name);
751 if (r < 0 && !IN_SET(r, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) {
752 log_debug_errno(r, "Failed to detect whether %s exists, skipping: %m", name);
753 continue;
754 } else if (r != 0) {
755 log_debug("Native unit for %s already exists, skipping.", name);
756 continue;
757 }
758
759 fpath = path_join(*path, de->d_name);
760 if (!fpath)
761 return log_oom();
762
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
767 service = new(SysvStub, 1);
768 if (!service)
769 return log_oom();
770
771 *service = (SysvStub) {
772 .sysv_start_priority = -1,
773 .name = TAKE_PTR(name),
774 .path = TAKE_PTR(fpath),
775 };
776
777 r = hashmap_put(all_services, service->name, service);
778 if (r < 0)
779 return log_oom();
780
781 TAKE_PTR(service);
782 }
783 }
784
785 return 0;
786 }
787
set_dependencies_from_rcnd(const LookupPaths * lp,Hashmap * all_services)788 static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) {
789 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
790 _cleanup_strv_free_ char **sysvrcnd_path = NULL;
791 SysvStub *service;
792 int r;
793
794 assert(lp);
795
796 r = acquire_search_path(SYSTEM_SYSVRCND_PATH, "SYSTEMD_SYSVRCND_PATH", &sysvrcnd_path);
797 if (r < 0)
798 return r;
799
800 STRV_FOREACH(p, sysvrcnd_path)
801 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
802 _cleanup_closedir_ DIR *d = NULL;
803 _cleanup_free_ char *path = NULL;
804
805 path = path_join(*p, rcnd_table[i].path);
806 if (!path) {
807 r = log_oom();
808 goto finish;
809 }
810
811 d = opendir(path);
812 if (!d) {
813 if (errno != ENOENT)
814 log_warning_errno(errno, "Opening %s failed, ignoring: %m", path);
815
816 continue;
817 }
818
819 FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to enumerate directory %s, ignoring: %m", path)) {
820 _cleanup_free_ char *name = NULL, *fpath = NULL;
821 int a, b;
822
823 if (de->d_name[0] != 'S')
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
835 fpath = path_join(*p, de->d_name);
836 if (!fpath) {
837 r = log_oom();
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
847 service = hashmap_get(all_services, name);
848 if (!service) {
849 log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name);
850 continue;
851 }
852
853 service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority);
854
855 r = set_ensure_put(&runlevel_services[i], NULL, service);
856 if (r < 0) {
857 log_oom();
858 goto finish;
859 }
860 }
861 }
862
863 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++)
864 SET_FOREACH(service, runlevel_services[i]) {
865 r = strv_extend(&service->before, rcnd_table[i].target);
866 if (r < 0) {
867 log_oom();
868 goto finish;
869 }
870 r = strv_extend(&service->wanted_by, rcnd_table[i].target);
871 if (r < 0) {
872 log_oom();
873 goto finish;
874 }
875 }
876
877 r = 0;
878
879 finish:
880 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++)
881 set_free(runlevel_services[i]);
882
883 return r;
884 }
885
run(const char * dest,const char * dest_early,const char * dest_late)886 static int run(const char *dest, const char *dest_early, const char *dest_late) {
887 _cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
888 _cleanup_(lookup_paths_free) LookupPaths lp = {};
889 SysvStub *service;
890 int r;
891
892 assert_se(arg_dest = dest_late);
893
894 r = lookup_paths_init_or_warn(&lp, LOOKUP_SCOPE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL);
895 if (r < 0)
896 return r;
897
898 all_services = hashmap_new(&string_hash_ops);
899 if (!all_services)
900 return log_oom();
901
902 r = enumerate_sysv(&lp, all_services);
903 if (r < 0)
904 return r;
905
906 r = set_dependencies_from_rcnd(&lp, all_services);
907 if (r < 0)
908 return r;
909
910 HASHMAP_FOREACH(service, all_services)
911 (void) load_sysv(service);
912
913 HASHMAP_FOREACH(service, all_services) {
914 (void) fix_order(service, all_services);
915 (void) generate_unit_file(service);
916 }
917
918 return 0;
919 }
920
921 DEFINE_MAIN_GENERATOR_FUNCTION(run);
922