]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sysv-generator/sysv-generator.c
Merge pull request #18007 from fw-strlen/ipv6_masq_and_dnat
[thirdparty/systemd.git] / src / sysv-generator / sysv-generator.c
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
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
78 static void free_sysvstub_hashmapp(Hashmap **h) {
79 hashmap_free_with_destructor(*h, free_sysvstub);
80 }
81
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
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 char **p;
107 int r;
108
109 assert(s);
110
111 if (!s->loaded)
112 return 0;
113
114 path_escaped = specifier_escape(s->path);
115 if (!path_escaped)
116 return log_oom();
117
118 unit = prefix_roota(arg_dest, s->name);
119
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 */
123 if (is_symlink(unit) > 0) {
124 log_warning("Overwriting existing symlink %s with real service.", unit);
125 (void) unlink(unit);
126 }
127
128 f = fopen(unit, "wxe");
129 if (!f)
130 return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
131
132 fprintf(f,
133 "# Automatically generated by systemd-sysv-generator\n\n"
134 "[Unit]\n"
135 "Documentation=man:systemd-sysv-generator(8)\n"
136 "SourcePath=%s\n",
137 path_escaped);
138
139 if (s->description) {
140 _cleanup_free_ char *t;
141
142 t = specifier_escape(s->description);
143 if (!t)
144 return log_oom();
145
146 fprintf(f, "Description=%s\n", t);
147 }
148
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);
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
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 }
176
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
184 fprintf(f,
185 "ExecStart=%s start\n"
186 "ExecStop=%s stop\n",
187 path_escaped, path_escaped);
188
189 if (s->reload)
190 fprintf(f, "ExecReload=%s reload\n", path_escaped);
191
192 r = fflush_and_check(f);
193 if (r < 0)
194 return log_error_errno(r, "Failed to write unit %s: %m", unit);
195
196 STRV_FOREACH(p, s->wanted_by)
197 (void) generator_add_symlink(arg_dest, *p, "wants", s->name);
198
199 return 1;
200 }
201
202 static 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
211 static char *sysv_translate_name(const char *name) {
212 _cleanup_free_ char *c = NULL;
213 char *res;
214
215 c = strdup(name);
216 if (!c)
217 return NULL;
218
219 res = endswith(c, ".sh");
220 if (res)
221 *res = 0;
222
223 if (unit_name_mangle(c, 0, &res) < 0)
224 return NULL;
225
226 return res;
227 }
228
229 static int sysv_translate_facility(SysvStub *s, unsigned line, const char *name, char **ret) {
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
248 const char *filename;
249 char *filename_no_sh, *e, *m;
250 const char *n;
251 unsigned i;
252 int r;
253
254 assert(name);
255 assert(s);
256 assert(ret);
257
258 filename = basename(s->path);
259
260 n = *name == '$' ? name + 1 : name;
261
262 for (i = 0; i < ELEMENTSOF(table); i += 2) {
263 if (!streq(table[i], n))
264 continue;
265
266 if (!table[i+1]) {
267 *ret = NULL;
268 return 0;
269 }
270
271 m = strdup(table[i+1]);
272 if (!m)
273 return log_oom();
274
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)
286 return log_error_errno(r, "[%s:%u] Could not build name for facility %s: %m", s->path, line, name);
287
288 return 1;
289 }
290
291 /* Strip ".sh" suffix from file name for comparison */
292 filename_no_sh = strdupa(filename);
293 e = endswith(filename_no_sh, ".sh");
294 if (e) {
295 *e = '\0';
296 filename = filename_no_sh;
297 }
298
299 /* Names equaling the file name of the services are redundant */
300 if (streq_ptr(n, filename)) {
301 *ret = NULL;
302 return 0;
303 }
304
305 /* Everything else we assume to be normal service names */
306 m = sysv_translate_name(n);
307 if (!m)
308 return log_oom();
309
310 *ret = m;
311 return 1;
312 }
313
314 static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) {
315 int r;
316
317 assert(s);
318 assert(full_text);
319 assert(text);
320
321 for (;;) {
322 _cleanup_free_ char *word = NULL, *m = NULL;
323
324 r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
325 if (r < 0)
326 return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line);
327 if (r == 0)
328 break;
329
330 r = sysv_translate_facility(s, line, word, &m);
331 if (r <= 0) /* continue on error */
332 continue;
333
334 switch (unit_name_to_type(m)) {
335
336 case UNIT_SERVICE:
337 log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
338 r = add_alias(s->name, m);
339 if (r < 0)
340 log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m);
341 break;
342
343 case UNIT_TARGET:
344
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! */
350
351 r = strv_extend(&s->before, m);
352 if (r < 0)
353 return log_oom();
354
355 r = strv_extend(&s->wants, m);
356 if (r < 0)
357 return log_oom();
358
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();
363 r = strv_extend(&s->wants, SPECIAL_NETWORK_TARGET);
364 if (r < 0)
365 return log_oom();
366 }
367
368 break;
369
370 case _UNIT_TYPE_INVALID:
371 log_warning("Unit name '%s' is invalid", m);
372 break;
373
374 default:
375 log_warning("Unknown unit type for unit '%s'", m);
376 }
377 }
378
379 return 0;
380 }
381
382 static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) {
383 int r;
384
385 assert(s);
386 assert(full_text);
387 assert(text);
388
389 for (;;) {
390 _cleanup_free_ char *word = NULL, *m = NULL;
391 bool is_before;
392
393 r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
394 if (r < 0)
395 return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line);
396 if (r == 0)
397 break;
398
399 r = sysv_translate_facility(s, line, word, &m);
400 if (r <= 0) /* continue on error */
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();
410
411 r = strv_extend(&s->wants, m);
412 } else
413 r = strv_extend(is_before ? &s->before : &s->after, m);
414 if (r < 0)
415 return log_oom();
416 }
417
418 return 0;
419 }
420
421 static 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");
439 if (!f) {
440 if (errno == ENOENT)
441 return 0;
442
443 return log_error_errno(errno, "Failed to open %s: %m", s->path);
444 }
445
446 log_debug("Loading SysV script %s", s->path);
447
448 for (;;) {
449 _cleanup_free_ char *l = NULL;
450 char *t;
451
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
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. */
465 if (state == USAGE_CONTINUATION ||
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
485 if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(t, "### END INIT INFO")) {
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
499 size_t k;
500 const char *j;
501
502 k = strlen(t);
503 if (k > 0 && t[k-1] == '\\') {
504 state = DESCRIPTION;
505 t[k-1] = 0;
506 }
507
508 j = empty_to_null(strstrip(t+12));
509
510 r = free_and_strdup(&chkconfig_description, j);
511 if (r < 0)
512 return log_oom();
513
514 } else if (startswith_no_case(t, "pidfile:")) {
515 const char *fn;
516
517 state = NORMAL;
518
519 fn = strstrip(t+8);
520 if (!path_is_absolute(fn)) {
521 log_error("[%s:%u] PID file not absolute. Ignoring.", s->path, line);
522 continue;
523 }
524
525 r = free_and_strdup(&s->pid_file, fn);
526 if (r < 0)
527 return log_oom();
528 }
529
530 } else if (state == DESCRIPTION) {
531
532 /* Try to parse Red Hat style description
533 * continuation */
534
535 size_t k;
536 char *j;
537
538 k = strlen(t);
539 if (k > 0 && t[k-1] == '\\')
540 t[k-1] = 0;
541 else
542 state = NORMAL;
543
544 j = strstrip(t);
545 if (!isempty(j)) {
546 char *d = NULL;
547
548 if (chkconfig_description)
549 d = strjoin(chkconfig_description, " ", j);
550 else
551 d = strdup(j);
552 if (!d)
553 return log_oom();
554
555 free(chkconfig_description);
556 chkconfig_description = d;
557 }
558
559 } else if (IN_SET(state, LSB, LSB_DESCRIPTION)) {
560
561 if (startswith_no_case(t, "Provides:")) {
562 state = LSB;
563
564 r = handle_provides(s, line, t, t + 9);
565 if (r < 0)
566 return r;
567
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:")) {
572
573 state = LSB;
574
575 r = handle_dependencies(s, line, t, strchr(t, ':') + 1);
576 if (r < 0)
577 return r;
578
579 } else if (startswith_no_case(t, "Description:")) {
580 const char *j;
581
582 state = LSB_DESCRIPTION;
583
584 j = empty_to_null(strstrip(t+12));
585
586 r = free_and_strdup(&long_description, j);
587 if (r < 0)
588 return log_oom();
589
590 } else if (startswith_no_case(t, "Short-Description:")) {
591 const char *j;
592
593 state = LSB;
594
595 j = empty_to_null(strstrip(t+18));
596
597 r = free_and_strdup(&short_description, j);
598 if (r < 0)
599 return log_oom();
600
601 } else if (state == LSB_DESCRIPTION) {
602
603 if (startswith(l, "#\t") || startswith(l, "# ")) {
604 const char *j;
605
606 j = strstrip(t);
607 if (!isempty(j)) {
608 char *d = NULL;
609
610 if (long_description)
611 d = strjoin(long_description, " ", t);
612 else
613 d = strdup(j);
614 if (!d)
615 return log_oom();
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 = strjoin(s->has_lsb ? "LSB: " : "SYSV: ", description);
645 if (!d)
646 return log_oom();
647
648 s->description = d;
649 }
650
651 s->loaded = true;
652 return 0;
653 }
654
655 static int fix_order(SysvStub *s, Hashmap *all_services) {
656 SysvStub *other;
657 int r;
658
659 assert(s);
660
661 if (!s->loaded)
662 return 0;
663
664 if (s->sysv_start_priority < 0)
665 return 0;
666
667 HASHMAP_FOREACH(other, all_services) {
668 if (s == other)
669 continue;
670
671 if (!other->loaded)
672 continue;
673
674 if (other->sysv_start_priority < 0)
675 continue;
676
677 /* If both units have modern headers we don't care
678 * about the priorities */
679 if (s->has_lsb && other->has_lsb)
680 continue;
681
682 if (other->sysv_start_priority < s->sysv_start_priority) {
683 r = strv_extend(&s->after, other->name);
684 if (r < 0)
685 return log_oom();
686
687 } else if (other->sysv_start_priority > s->sysv_start_priority) {
688 r = strv_extend(&s->before, other->name);
689 if (r < 0)
690 return log_oom();
691 } else
692 continue;
693
694 /* FIXME: Maybe we should compare the name here lexicographically? */
695 }
696
697 return 0;
698 }
699
700 static int acquire_search_path(const char *def, const char *envvar, char ***ret) {
701 _cleanup_strv_free_ char **l = NULL;
702 const char *e;
703 int r;
704
705 assert(def);
706 assert(envvar);
707
708 e = getenv(envvar);
709 if (e) {
710 r = path_split_and_make_absolute(e, &l);
711 if (r < 0)
712 return log_error_errno(r, "Failed to make $%s search path absolute: %m", envvar);
713 }
714
715 if (strv_isempty(l)) {
716 strv_free(l);
717
718 l = strv_new(def);
719 if (!l)
720 return log_oom();
721 }
722
723 if (!path_strv_resolve_uniq(l, NULL))
724 return log_oom();
725
726 *ret = TAKE_PTR(l);
727
728 return 0;
729 }
730
731 static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
732 _cleanup_strv_free_ char **sysvinit_path = NULL;
733 char **path;
734 int r;
735
736 assert(lp);
737
738 r = acquire_search_path(SYSTEM_SYSVINIT_PATH, "SYSTEMD_SYSVINIT_PATH", &sysvinit_path);
739 if (r < 0)
740 return r;
741
742 STRV_FOREACH(path, sysvinit_path) {
743 _cleanup_closedir_ DIR *d = NULL;
744 struct dirent *de;
745
746 d = opendir(*path);
747 if (!d) {
748 if (errno != ENOENT)
749 log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path);
750 continue;
751 }
752
753 FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) {
754 _cleanup_free_ char *fpath = NULL, *name = NULL;
755 _cleanup_(free_sysvstubp) SysvStub *service = NULL;
756 struct stat st;
757
758 if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
759 log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name);
760 continue;
761 }
762
763 if (!(st.st_mode & S_IXUSR))
764 continue;
765
766 if (!S_ISREG(st.st_mode))
767 continue;
768
769 name = sysv_translate_name(de->d_name);
770 if (!name)
771 return log_oom();
772
773 if (hashmap_contains(all_services, name))
774 continue;
775
776 r = unit_file_exists(UNIT_FILE_SYSTEM, lp, name);
777 if (r < 0 && !IN_SET(r, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) {
778 log_debug_errno(r, "Failed to detect whether %s exists, skipping: %m", name);
779 continue;
780 } else if (r != 0) {
781 log_debug("Native unit for %s already exists, skipping.", name);
782 continue;
783 }
784
785 fpath = path_join(*path, de->d_name);
786 if (!fpath)
787 return log_oom();
788
789 log_warning("SysV service '%s' lacks a native systemd unit file. "
790 "Automatically generating a unit file for compatibility. "
791 "Please update package to include a native systemd unit file, in order to make it more safe and robust.", fpath);
792
793 service = new(SysvStub, 1);
794 if (!service)
795 return log_oom();
796
797 *service = (SysvStub) {
798 .sysv_start_priority = -1,
799 .name = TAKE_PTR(name),
800 .path = TAKE_PTR(fpath),
801 };
802
803 r = hashmap_put(all_services, service->name, service);
804 if (r < 0)
805 return log_oom();
806
807 TAKE_PTR(service);
808 }
809 }
810
811 return 0;
812 }
813
814 static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) {
815 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
816 _cleanup_strv_free_ char **sysvrcnd_path = NULL;
817 SysvStub *service;
818 char **p;
819 int r;
820
821 assert(lp);
822
823 r = acquire_search_path(SYSTEM_SYSVRCND_PATH, "SYSTEMD_SYSVRCND_PATH", &sysvrcnd_path);
824 if (r < 0)
825 return r;
826
827 STRV_FOREACH(p, sysvrcnd_path)
828 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
829 _cleanup_closedir_ DIR *d = NULL;
830 _cleanup_free_ char *path = NULL;
831 struct dirent *de;
832
833 path = path_join(*p, rcnd_table[i].path);
834 if (!path) {
835 r = log_oom();
836 goto finish;
837 }
838
839 d = opendir(path);
840 if (!d) {
841 if (errno != ENOENT)
842 log_warning_errno(errno, "Opening %s failed, ignoring: %m", path);
843
844 continue;
845 }
846
847 FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to enumerate directory %s, ignoring: %m", path)) {
848 _cleanup_free_ char *name = NULL, *fpath = NULL;
849 int a, b;
850
851 if (de->d_name[0] != 'S')
852 continue;
853
854 if (strlen(de->d_name) < 4)
855 continue;
856
857 a = undecchar(de->d_name[1]);
858 b = undecchar(de->d_name[2]);
859
860 if (a < 0 || b < 0)
861 continue;
862
863 fpath = path_join(*p, de->d_name);
864 if (!fpath) {
865 r = log_oom();
866 goto finish;
867 }
868
869 name = sysv_translate_name(de->d_name + 3);
870 if (!name) {
871 r = log_oom();
872 goto finish;
873 }
874
875 service = hashmap_get(all_services, name);
876 if (!service) {
877 log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name);
878 continue;
879 }
880
881 service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority);
882
883 r = set_ensure_put(&runlevel_services[i], NULL, service);
884 if (r < 0) {
885 log_oom();
886 goto finish;
887 }
888 }
889 }
890
891 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++)
892 SET_FOREACH(service, runlevel_services[i]) {
893 r = strv_extend(&service->before, rcnd_table[i].target);
894 if (r < 0) {
895 log_oom();
896 goto finish;
897 }
898 r = strv_extend(&service->wanted_by, rcnd_table[i].target);
899 if (r < 0) {
900 log_oom();
901 goto finish;
902 }
903 }
904
905 r = 0;
906
907 finish:
908 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++)
909 set_free(runlevel_services[i]);
910
911 return r;
912 }
913
914 static int run(const char *dest, const char *dest_early, const char *dest_late) {
915 _cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
916 _cleanup_(lookup_paths_free) LookupPaths lp = {};
917 SysvStub *service;
918 int r;
919
920 assert_se(arg_dest = dest_late);
921
922 r = lookup_paths_init(&lp, UNIT_FILE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL);
923 if (r < 0)
924 return log_error_errno(r, "Failed to find lookup paths: %m");
925
926 all_services = hashmap_new(&string_hash_ops);
927 if (!all_services)
928 return log_oom();
929
930 r = enumerate_sysv(&lp, all_services);
931 if (r < 0)
932 return r;
933
934 r = set_dependencies_from_rcnd(&lp, all_services);
935 if (r < 0)
936 return r;
937
938 HASHMAP_FOREACH(service, all_services)
939 (void) load_sysv(service);
940
941 HASHMAP_FOREACH(service, all_services) {
942 (void) fix_order(service, all_services);
943 (void) generate_unit_file(service);
944 }
945
946 return 0;
947 }
948
949 DEFINE_MAIN_GENERATOR_FUNCTION(run);