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