]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sysv-generator/sysv-generator.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 "mkdir.h"
18 #include "path-lookup.h"
19 #include "path-util.h"
20 #include "set.h"
21 #include "special.h"
22 #include "specifier.h"
23 #include "stat-util.h"
24 #include "string-util.h"
25 #include "strv.h"
26 #include "unit-name.h"
27 #include "util.h"
28
29 static const struct {
30 const char *path;
31 const char *target;
32 } rcnd_table[] = {
33 /* Standard SysV runlevels for start-up */
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. */
42 };
43
44 static const char *arg_dest = "/tmp";
45
46 typedef 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;
55 char **wanted_by;
56 bool has_lsb;
57 bool reload;
58 bool loaded;
59 } SysvStub;
60
61 static void free_sysvstub(SysvStub *s) {
62 if (!s)
63 return;
64
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);
73 free(s);
74 }
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 = strjoina(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 = strjoina(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_QUOTES|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_QUOTES|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 = strappend(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 Iterator j;
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, j) {
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 = strjoin(*path, "/", de->d_name);
787 if (!fpath)
788 return log_oom();
789
790 service = new0(SysvStub, 1);
791 if (!service)
792 return log_oom();
793
794 service->sysv_start_priority = -1;
795 service->name = TAKE_PTR(name);
796 service->path = TAKE_PTR(fpath);
797
798 r = hashmap_put(all_services, service->name, service);
799 if (r < 0)
800 return log_oom();
801
802 service = NULL;
803 }
804 }
805
806 return 0;
807 }
808
809 static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) {
810 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
811 _cleanup_strv_free_ char **sysvrcnd_path = NULL;
812 SysvStub *service;
813 unsigned i;
814 Iterator j;
815 char **p;
816 int r;
817
818 assert(lp);
819
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) {
825 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
826
827 _cleanup_closedir_ DIR *d = NULL;
828 _cleanup_free_ char *path = NULL;
829 struct dirent *de;
830
831 path = strjoin(*p, "/", rcnd_table[i].path);
832 if (!path) {
833 r = log_oom();
834 goto finish;
835 }
836
837 d = opendir(path);
838 if (!d) {
839 if (errno != ENOENT)
840 log_warning_errno(errno, "Opening %s failed, ignoring: %m", path);
841
842 continue;
843 }
844
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;
847 int a, b;
848
849 if (de->d_name[0] != 'S')
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
861 fpath = strjoin(*p, "/", de->d_name);
862 if (!fpath) {
863 r = log_oom();
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
873 service = hashmap_get(all_services, name);
874 if (!service) {
875 log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name);
876 continue;
877 }
878
879 service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority);
880
881 r = set_ensure_allocated(&runlevel_services[i], NULL);
882 if (r < 0) {
883 log_oom();
884 goto finish;
885 }
886
887 r = set_put(runlevel_services[i], service);
888 if (r < 0) {
889 log_oom();
890 goto finish;
891 }
892 }
893 }
894 }
895
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);
899 if (r < 0) {
900 log_oom();
901 goto finish;
902 }
903 r = strv_extend(&service->wanted_by, rcnd_table[i].target);
904 if (r < 0) {
905 log_oom();
906 goto finish;
907 }
908 }
909
910 r = 0;
911
912 finish:
913 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
914 set_free(runlevel_services[i]);
915
916 return r;
917 }
918
919 int main(int argc, char *argv[]) {
920 _cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
921 _cleanup_(lookup_paths_free) LookupPaths lp = {};
922 SysvStub *service;
923 Iterator j;
924 int r;
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
934 log_set_prohibit_ipc(true);
935 log_set_target(LOG_TARGET_AUTO);
936 log_parse_environment();
937 log_open();
938
939 umask(0022);
940
941 r = lookup_paths_init(&lp, UNIT_FILE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL);
942 if (r < 0) {
943 log_error_errno(r, "Failed to find lookup paths: %m");
944 goto finish;
945 }
946
947 all_services = hashmap_new(&string_hash_ops);
948 if (!all_services) {
949 r = log_oom();
950 goto finish;
951 }
952
953 r = enumerate_sysv(&lp, all_services);
954 if (r < 0)
955 goto finish;
956
957 r = set_dependencies_from_rcnd(&lp, all_services);
958 if (r < 0)
959 goto finish;
960
961 HASHMAP_FOREACH(service, all_services, j)
962 (void) load_sysv(service);
963
964 HASHMAP_FOREACH(service, all_services, j) {
965 (void) fix_order(service, all_services);
966 (void) generate_unit_file(service);
967 }
968
969 r = 0;
970
971 finish:
972 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
973 }