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