]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sysv-generator/sysv-generator.c
journal: Serialize __MONOTONIC_TIMESTAMP metadata field as well
[thirdparty/systemd.git] / src / sysv-generator / sysv-generator.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
95ed3294
TA
2
3#include <errno.h>
4#include <stdio.h>
5#include <unistd.h>
6
f4d4ca6e
LP
7#include "sd-messages.h"
8
b5efdb8a 9#include "alloc-util.h"
c279613f 10#include "dirent-util.h"
41e2036e 11#include "exit-status.h"
3ffd4af2 12#include "fd-util.h"
c279613f 13#include "fileio.h"
7f0cc637 14#include "generator.h"
07630cea 15#include "hashmap.h"
8fcde012 16#include "hexdecoct.h"
3ed075cf 17#include "initrd-util.h"
07630cea
LP
18#include "install.h"
19#include "log.h"
5e332028 20#include "main-func.h"
95ed3294 21#include "mkdir.h"
95ed3294 22#include "path-lookup.h"
07630cea 23#include "path-util.h"
f2341e0a 24#include "set.h"
07630cea 25#include "special.h"
98bad05e 26#include "specifier.h"
8fcde012 27#include "stat-util.h"
07630cea
LP
28#include "string-util.h"
29#include "strv.h"
30#include "unit-name.h"
95ed3294 31
5d97475b
LP
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
95ed3294
TA
35static const struct {
36 const char *path;
37 const char *target;
95ed3294
TA
38} rcnd_table[] = {
39 /* Standard SysV runlevels for start-up */
788d2b08
LP
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. */
95ed3294
TA
48};
49
7a44c7e3 50static const char *arg_dest = NULL;
8fba1c8d 51
95ed3294
TA
52typedef 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;
260ad50f 61 char **wanted_by;
95ed3294
TA
62 bool has_lsb;
63 bool reload;
c279613f 64 bool loaded;
95ed3294
TA
65} SysvStub;
66
75db809a 67static SysvStub* free_sysvstub(SysvStub *s) {
c279613f 68 if (!s)
75db809a 69 return NULL;
c279613f 70
8fba1c8d
ZJS
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);
75db809a 79 return mfree(s);
8fba1c8d 80}
8fba1c8d
ZJS
81DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, free_sysvstub);
82
83static void free_sysvstub_hashmapp(Hashmap **h) {
224b0e7a 84 hashmap_free_with_destructor(*h, free_sysvstub);
8fba1c8d 85}
95ed3294 86
b7e71846 87static int add_alias(const char *service, const char *alias) {
8e7e4a73 88 _cleanup_free_ char *link = NULL;
b7e71846
MB
89
90 assert(service);
91 assert(alias);
92
8e7e4a73
LP
93 link = path_join(arg_dest, alias);
94 if (!link)
95 return -ENOMEM;
b7e71846 96
8e7e4a73 97 if (symlink(service, link) < 0) {
b7e71846
MB
98 if (errno == EEXIST)
99 return 0;
c279613f 100
b7e71846
MB
101 return -errno;
102 }
103
104 return 1;
105}
106
95ed3294 107static int generate_unit_file(SysvStub *s) {
8e7e4a73 108 _cleanup_free_ char *path_escaped = NULL, *unit = NULL;
95ed3294 109 _cleanup_fclose_ FILE *f = NULL;
95ed3294
TA
110 int r;
111
c279613f
LP
112 assert(s);
113
114 if (!s->loaded)
115 return 0;
116
98bad05e
LP
117 path_escaped = specifier_escape(s->path);
118 if (!path_escaped)
119 return log_oom();
120
8e7e4a73
LP
121 unit = path_join(arg_dest, s->name);
122 if (!unit)
123 return log_oom();
c279613f 124
77354c7e
MP
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 */
4e558983 128 if (is_symlink(unit) > 0) {
c279613f 129 log_warning("Overwriting existing symlink %s with real service.", unit);
9993ef2e 130 (void) unlink(unit);
77354c7e
MP
131 }
132
95ed3294 133 f = fopen(unit, "wxe");
4a62c710
MS
134 if (!f)
135 return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
95ed3294
TA
136
137 fprintf(f,
138 "# Automatically generated by systemd-sysv-generator\n\n"
139 "[Unit]\n"
aad0a2c8 140 "Documentation=man:systemd-sysv-generator(8)\n"
c279613f 141 "SourcePath=%s\n",
98bad05e
LP
142 path_escaped);
143
144 if (s->description) {
c2b2df60 145 _cleanup_free_ char *t = NULL;
c279613f 146
98bad05e
LP
147 t = specifier_escape(s->description);
148 if (!t)
149 return log_oom();
150
151 fprintf(f, "Description=%s\n", t);
152 }
95ed3294 153
c584ffc0
LN
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);
95ed3294
TA
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
98bad05e 172 if (s->pid_file) {
c2b2df60 173 _cleanup_free_ char *t = NULL;
98bad05e
LP
174
175 t = specifier_escape(s->pid_file);
176 if (!t)
177 return log_oom();
178
179 fprintf(f, "PIDFile=%s\n", t);
180 }
95ed3294 181
41e2036e
LP
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
95ed3294
TA
189 fprintf(f,
190 "ExecStart=%s start\n"
191 "ExecStop=%s stop\n",
98bad05e 192 path_escaped, path_escaped);
95ed3294
TA
193
194 if (s->reload)
98bad05e 195 fprintf(f, "ExecReload=%s reload\n", path_escaped);
95ed3294 196
c279613f
LP
197 r = fflush_and_check(f);
198 if (r < 0)
199 return log_error_errno(r, "Failed to write unit %s: %m", unit);
200
7f0cc637
ZJS
201 STRV_FOREACH(p, s->wanted_by)
202 (void) generator_add_symlink(arg_dest, *p, "wants", s->name);
95ed3294 203
c279613f 204 return 1;
95ed3294
TA
205}
206
207static 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
216static char *sysv_translate_name(const char *name) {
0b2ec8a3
DH
217 _cleanup_free_ char *c = NULL;
218 char *res;
95ed3294 219
264581a2
FS
220 c = strdup(name);
221 if (!c)
0b2ec8a3 222 return NULL;
95ed3294 223
0b2ec8a3
DH
224 res = endswith(c, ".sh");
225 if (res)
226 *res = 0;
95ed3294 227
37cbc1d5 228 if (unit_name_mangle(c, 0, &res) < 0)
0b2ec8a3
DH
229 return NULL;
230
231 return res;
95ed3294
TA
232}
233
7532e6d4 234static int sysv_translate_facility(SysvStub *s, unsigned line, const char *name, char **ret) {
95ed3294
TA
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
1d8a8a7d 253 _cleanup_free_ char *filename = NULL;
95ed3294 254 const char *n;
1d8a8a7d 255 char *e, *m;
c279613f 256 int r;
95ed3294
TA
257
258 assert(name);
7532e6d4 259 assert(s);
c279613f 260 assert(ret);
95ed3294 261
1d8a8a7d
LP
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);
7532e6d4 265
95ed3294
TA
266 n = *name == '$' ? name + 1 : name;
267
1d8a8a7d 268 for (size_t i = 0; i < ELEMENTSOF(table); i += 2) {
95ed3294
TA
269 if (!streq(table[i], n))
270 continue;
271
e932f540
LP
272 if (!table[i+1]) {
273 *ret = NULL;
95ed3294 274 return 0;
e932f540 275 }
95ed3294 276
c279613f
LP
277 m = strdup(table[i+1]);
278 if (!m)
95ed3294
TA
279 return log_oom();
280
c279613f
LP
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)
7532e6d4 292 return log_error_errno(r, "[%s:%u] Could not build name for facility %s: %m", s->path, line, name);
c279613f 293
e932f540 294 return 1;
95ed3294
TA
295 }
296
c279613f 297 /* Strip ".sh" suffix from file name for comparison */
1d8a8a7d
LP
298 e = endswith(filename, ".sh");
299 if (e)
4e488555 300 *e = '\0';
29e0e6d8 301
c279613f 302 /* Names equaling the file name of the services are redundant */
e932f540
LP
303 if (streq_ptr(n, filename)) {
304 *ret = NULL;
95ed3294 305 return 0;
e932f540 306 }
95ed3294 307
c279613f
LP
308 /* Everything else we assume to be normal service names */
309 m = sysv_translate_name(n);
310 if (!m)
311 return log_oom();
95ed3294 312
c279613f 313 *ret = m;
95ed3294
TA
314 return 1;
315}
316
1e2fee5f 317static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) {
1e2fee5f
ZJS
318 int r;
319
c279613f
LP
320 assert(s);
321 assert(full_text);
322 assert(text);
1e2fee5f 323
c279613f
LP
324 for (;;) {
325 _cleanup_free_ char *word = NULL, *m = NULL;
1e2fee5f 326
4ec85141 327 r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
1e2fee5f 328 if (r < 0)
7532e6d4 329 return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line);
1e2fee5f 330 if (r == 0)
c279613f
LP
331 break;
332
7532e6d4 333 r = sysv_translate_facility(s, line, word, &m);
c279613f 334 if (r <= 0) /* continue on error */
1e2fee5f
ZJS
335 continue;
336
c279613f
LP
337 switch (unit_name_to_type(m)) {
338
339 case UNIT_SERVICE:
1e2fee5f
ZJS
340 log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
341 r = add_alias(s->name, m);
e987f2a8 342 if (r < 0)
f2341e0a 343 log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m);
c279613f
LP
344 break;
345
346 case UNIT_TARGET:
347
1e2fee5f
ZJS
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! */
c279613f 353
1e2fee5f
ZJS
354 r = strv_extend(&s->before, m);
355 if (r < 0)
356 return log_oom();
c279613f 357
1e2fee5f
ZJS
358 r = strv_extend(&s->wants, m);
359 if (r < 0)
360 return log_oom();
c279613f 361
1e2fee5f
ZJS
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();
bd9ad4ff
LN
366 r = strv_extend(&s->wants, SPECIAL_NETWORK_TARGET);
367 if (r < 0)
368 return log_oom();
1e2fee5f 369 }
c279613f
LP
370
371 break;
372
373 case _UNIT_TYPE_INVALID:
2c09a745 374 log_warning("Unit name '%s' is invalid", m);
c279613f
LP
375 break;
376
377 default:
2c09a745 378 log_warning("Unknown unit type for unit '%s'", m);
c279613f 379 }
1e2fee5f 380 }
c279613f 381
1e2fee5f
ZJS
382 return 0;
383}
384
385static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) {
1e2fee5f
ZJS
386 int r;
387
c279613f
LP
388 assert(s);
389 assert(full_text);
390 assert(text);
1e2fee5f 391
c279613f
LP
392 for (;;) {
393 _cleanup_free_ char *word = NULL, *m = NULL;
394 bool is_before;
1e2fee5f 395
4ec85141 396 r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
c279613f 397 if (r < 0)
7532e6d4 398 return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line);
1e2fee5f 399 if (r == 0)
c279613f
LP
400 break;
401
7532e6d4 402 r = sysv_translate_facility(s, line, word, &m);
c279613f 403 if (r <= 0) /* continue on error */
1e2fee5f
ZJS
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();
c279613f 413
1e2fee5f 414 r = strv_extend(&s->wants, m);
e987f2a8 415 } else
1e2fee5f 416 r = strv_extend(is_before ? &s->before : &s->after, m);
1e2fee5f 417 if (r < 0)
e987f2a8 418 return log_oom();
1e2fee5f 419 }
c279613f 420
1e2fee5f
ZJS
421 return 0;
422}
423
95ed3294 424static int load_sysv(SysvStub *s) {
c2b2df60 425 _cleanup_fclose_ FILE *f = NULL;
95ed3294
TA
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");
c279613f
LP
442 if (!f) {
443 if (errno == ENOENT)
444 return 0;
95ed3294 445
c279613f
LP
446 return log_error_errno(errno, "Failed to open %s: %m", s->path);
447 }
77354c7e 448
c279613f 449 log_debug("Loading SysV script %s", s->path);
95ed3294 450
e393eff6
LP
451 for (;;) {
452 _cleanup_free_ char *l = NULL;
95ed3294 453
0ff6ff2b 454 r = read_stripped_line(f, LONG_LINE_MAX, &l);
e393eff6
LP
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
95ed3294
TA
460 line++;
461
0ff6ff2b 462 if (l[0] != '#') {
95ed3294
TA
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. */
e393eff6 466 if (state == USAGE_CONTINUATION ||
0ff6ff2b
LP
467 (state == NORMAL && strcasestr(l, "usage"))) {
468 if (usage_contains_reload(l)) {
95ed3294
TA
469 supports_reload = true;
470 state = NORMAL;
0ff6ff2b 471 } else if (endswith(l, "\\"))
95ed3294
TA
472 state = USAGE_CONTINUATION;
473 else
474 state = NORMAL;
475 }
476
477 continue;
478 }
479
0ff6ff2b 480 if (state == NORMAL && streq(l, "### BEGIN INIT INFO")) {
95ed3294
TA
481 state = LSB;
482 s->has_lsb = true;
483 continue;
484 }
485
0ff6ff2b 486 if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(l, "### END INIT INFO")) {
95ed3294
TA
487 state = NORMAL;
488 continue;
489 }
490
0ff6ff2b 491 char *t = l + 1;
95ed3294
TA
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
c279613f 500 size_t k;
859fdd1e 501 const char *j;
95ed3294 502
c279613f
LP
503 k = strlen(t);
504 if (k > 0 && t[k-1] == '\\') {
95ed3294
TA
505 state = DESCRIPTION;
506 t[k-1] = 0;
507 }
508
3c6f7c34 509 j = empty_to_null(strstrip(t+12));
95ed3294 510
c279613f
LP
511 r = free_and_strdup(&chkconfig_description, j);
512 if (r < 0)
513 return log_oom();
95ed3294
TA
514
515 } else if (startswith_no_case(t, "pidfile:")) {
c279613f 516 const char *fn;
95ed3294
TA
517
518 state = NORMAL;
519
520 fn = strstrip(t+8);
521 if (!path_is_absolute(fn)) {
f2341e0a 522 log_error("[%s:%u] PID file not absolute. Ignoring.", s->path, line);
95ed3294
TA
523 continue;
524 }
525
c279613f
LP
526 r = free_and_strdup(&s->pid_file, fn);
527 if (r < 0)
528 return log_oom();
95ed3294
TA
529 }
530
531 } else if (state == DESCRIPTION) {
532
533 /* Try to parse Red Hat style description
534 * continuation */
535
c279613f 536 size_t k;
580bf613 537 const char *j;
95ed3294 538
c279613f
LP
539 k = strlen(t);
540 if (k > 0 && t[k-1] == '\\')
95ed3294
TA
541 t[k-1] = 0;
542 else
543 state = NORMAL;
544
545 j = strstrip(t);
580bf613
YW
546 if (!isempty(j) && !strextend_with_separator(&chkconfig_description, " ", j))
547 return log_oom();
95ed3294 548
3742095b 549 } else if (IN_SET(state, LSB, LSB_DESCRIPTION)) {
95ed3294
TA
550
551 if (startswith_no_case(t, "Provides:")) {
95ed3294
TA
552 state = LSB;
553
1e2fee5f
ZJS
554 r = handle_provides(s, line, t, t + 9);
555 if (r < 0)
556 return r;
c279613f 557
95ed3294
TA
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:")) {
95ed3294
TA
562
563 state = LSB;
564
1e2fee5f
ZJS
565 r = handle_dependencies(s, line, t, strchr(t, ':') + 1);
566 if (r < 0)
567 return r;
95ed3294 568
95ed3294 569 } else if (startswith_no_case(t, "Description:")) {
c279613f 570 const char *j;
95ed3294
TA
571
572 state = LSB_DESCRIPTION;
573
3c6f7c34 574 j = empty_to_null(strstrip(t+12));
95ed3294 575
c279613f
LP
576 r = free_and_strdup(&long_description, j);
577 if (r < 0)
578 return log_oom();
95ed3294
TA
579
580 } else if (startswith_no_case(t, "Short-Description:")) {
c279613f 581 const char *j;
95ed3294
TA
582
583 state = LSB;
584
3c6f7c34 585 j = empty_to_null(strstrip(t+18));
95ed3294 586
c279613f
LP
587 r = free_and_strdup(&short_description, j);
588 if (r < 0)
589 return log_oom();
95ed3294
TA
590
591 } else if (state == LSB_DESCRIPTION) {
592
593 if (startswith(l, "#\t") || startswith(l, "# ")) {
c279613f 594 const char *j;
95ed3294
TA
595
596 j = strstrip(t);
580bf613
YW
597 if (!isempty(j) && !strextend_with_separator(&long_description, " ", j))
598 return log_oom();
95ed3294
TA
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
b910cc72 622 d = strjoin(s->has_lsb ? "LSB: " : "SYSV: ", description);
95ed3294 623 if (!d)
c279613f 624 return log_oom();
95ed3294
TA
625
626 s->description = d;
627 }
628
c279613f 629 s->loaded = true;
95ed3294
TA
630 return 0;
631}
632
633static int fix_order(SysvStub *s, Hashmap *all_services) {
634 SysvStub *other;
95ed3294
TA
635 int r;
636
637 assert(s);
638
c279613f
LP
639 if (!s->loaded)
640 return 0;
641
95ed3294
TA
642 if (s->sysv_start_priority < 0)
643 return 0;
644
90e74a66 645 HASHMAP_FOREACH(other, all_services) {
95ed3294
TA
646 if (s == other)
647 continue;
648
c279613f
LP
649 if (!other->loaded)
650 continue;
651
95ed3294
TA
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();
c279613f
LP
664
665 } else if (other->sysv_start_priority > s->sysv_start_priority) {
95ed3294
TA
666 r = strv_extend(&s->before, other->name);
667 if (r < 0)
668 return log_oom();
c279613f 669 } else
95ed3294
TA
670 continue;
671
672 /* FIXME: Maybe we should compare the name here lexicographically? */
673 }
674
675 return 0;
676}
677
4143c6c3
LP
678static 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
bea1a013 696 l = strv_new(def);
4143c6c3
LP
697 if (!l)
698 return log_oom();
699 }
700
701 if (!path_strv_resolve_uniq(l, NULL))
702 return log_oom();
703
ae2a15bc 704 *ret = TAKE_PTR(l);
4143c6c3
LP
705
706 return 0;
707}
708
a8ffe6fb 709static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
4143c6c3 710 _cleanup_strv_free_ char **sysvinit_path = NULL;
0ec0deaa 711 int r;
95ed3294 712
c279613f 713 assert(lp);
c279613f 714
4143c6c3
LP
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) {
95ed3294 720 _cleanup_closedir_ DIR *d = NULL;
95ed3294
TA
721
722 d = opendir(*path);
723 if (!d) {
724 if (errno != ENOENT)
c279613f 725 log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path);
95ed3294
TA
726 continue;
727 }
728
c279613f 729 FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) {
95ed3294 730 _cleanup_free_ char *fpath = NULL, *name = NULL;
8fba1c8d 731 _cleanup_(free_sysvstubp) SysvStub *service = NULL;
805e5dda 732 struct stat st;
95ed3294 733
7b729f86 734 if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
c279613f 735 log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name);
95ed3294 736 continue;
805e5dda 737 }
95ed3294
TA
738
739 if (!(st.st_mode & S_IXUSR))
740 continue;
741
805e5dda
LP
742 if (!S_ISREG(st.st_mode))
743 continue;
744
95ed3294
TA
745 name = sysv_translate_name(de->d_name);
746 if (!name)
747 return log_oom();
748
a986501b
LP
749 if (hashmap_contains(all_services, name))
750 continue;
751
4870133b 752 r = unit_file_exists(RUNTIME_SCOPE_SYSTEM, lp, name);
76ec966f 753 if (r < 0 && !IN_SET(r, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) {
0ec0deaa
LP
754 log_debug_errno(r, "Failed to detect whether %s exists, skipping: %m", name);
755 continue;
3c6d8e57 756 } else if (r != 0) {
0ec0deaa 757 log_debug("Native unit for %s already exists, skipping.", name);
f4f01ec1
MP
758 continue;
759 }
760
657ee2d8 761 fpath = path_join(*path, de->d_name);
c279613f
LP
762 if (!fpath)
763 return log_oom();
764
f4d4ca6e
LP
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);
0e42cbe2 775
470ab28d 776 service = new(SysvStub, 1);
95ed3294
TA
777 if (!service)
778 return log_oom();
779
470ab28d
LP
780 *service = (SysvStub) {
781 .sysv_start_priority = -1,
782 .name = TAKE_PTR(name),
783 .path = TAKE_PTR(fpath),
784 };
95ed3294
TA
785
786 r = hashmap_put(all_services, service->name, service);
805e5dda 787 if (r < 0)
95ed3294
TA
788 return log_oom();
789
470ab28d 790 TAKE_PTR(service);
95ed3294
TA
791 }
792 }
793
794 return 0;
795}
796
a8ffe6fb 797static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) {
95ed3294 798 Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
4143c6c3 799 _cleanup_strv_free_ char **sysvrcnd_path = NULL;
c279613f 800 SysvStub *service;
c279613f 801 int r;
95ed3294 802
c279613f
LP
803 assert(lp);
804
4143c6c3
LP
805 r = acquire_search_path(SYSTEM_SYSVRCND_PATH, "SYSTEMD_SYSVRCND_PATH", &sysvrcnd_path);
806 if (r < 0)
807 return r;
808
e198eba7 809 STRV_FOREACH(p, sysvrcnd_path)
b3a9d980 810 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++) {
c279613f
LP
811 _cleanup_closedir_ DIR *d = NULL;
812 _cleanup_free_ char *path = NULL;
95ed3294 813
657ee2d8 814 path = path_join(*p, rcnd_table[i].path);
c279613f
LP
815 if (!path) {
816 r = log_oom();
817 goto finish;
818 }
95ed3294
TA
819
820 d = opendir(path);
821 if (!d) {
822 if (errno != ENOENT)
c279613f 823 log_warning_errno(errno, "Opening %s failed, ignoring: %m", path);
95ed3294
TA
824
825 continue;
826 }
827
8746820b 828 FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to enumerate directory %s, ignoring: %m", path)) {
c279613f 829 _cleanup_free_ char *name = NULL, *fpath = NULL;
95ed3294
TA
830 int a, b;
831
b9c59555 832 if (de->d_name[0] != 'S')
95ed3294
TA
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
657ee2d8 844 fpath = path_join(*p, de->d_name);
95ed3294 845 if (!fpath) {
c279613f 846 r = log_oom();
95ed3294
TA
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
8c84621c 856 service = hashmap_get(all_services, name);
9ed794a3 857 if (!service) {
c279613f 858 log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name);
95ed3294
TA
859 continue;
860 }
861
b9c59555 862 service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority);
95ed3294 863
de7fef4b 864 r = set_ensure_put(&runlevel_services[i], NULL, service);
b9c59555
LP
865 if (r < 0) {
866 log_oom();
867 goto finish;
95ed3294
TA
868 }
869 }
870 }
871
e198eba7 872 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++)
90e74a66 873 SET_FOREACH(service, runlevel_services[i]) {
95ed3294 874 r = strv_extend(&service->before, rcnd_table[i].target);
c279613f
LP
875 if (r < 0) {
876 log_oom();
877 goto finish;
878 }
260ad50f 879 r = strv_extend(&service->wanted_by, rcnd_table[i].target);
c279613f
LP
880 if (r < 0) {
881 log_oom();
882 goto finish;
883 }
95ed3294
TA
884 }
885
95ed3294
TA
886 r = 0;
887
888finish:
e198eba7 889 for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++)
95ed3294
TA
890 set_free(runlevel_services[i]);
891
892 return r;
893}
894
7a44c7e3 895static int run(const char *dest, const char *dest_early, const char *dest_late) {
5921fc3c 896 _cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
7dfc7139 897 _cleanup_(lookup_paths_done) LookupPaths lp = {};
95ed3294 898 SysvStub *service;
c279613f 899 int r;
95ed3294 900
3ed075cf
LP
901 if (in_initrd()) {
902 log_debug("Skipping generator, running in the initrd.");
903 return EXIT_SUCCESS;
904 }
905
7a44c7e3 906 assert_se(arg_dest = dest_late);
95ed3294 907
4870133b 908 r = lookup_paths_init_or_warn(&lp, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL);
47031c92 909 if (r < 0)
99aad9a2 910 return r;
95ed3294 911
d5099efc 912 all_services = hashmap_new(&string_hash_ops);
47031c92
ZJS
913 if (!all_services)
914 return log_oom();
95ed3294 915
a8ffe6fb 916 r = enumerate_sysv(&lp, all_services);
c279613f 917 if (r < 0)
47031c92 918 return r;
95ed3294 919
a8ffe6fb 920 r = set_dependencies_from_rcnd(&lp, all_services);
c279613f 921 if (r < 0)
47031c92 922 return r;
95ed3294 923
90e74a66 924 HASHMAP_FOREACH(service, all_services)
c279613f 925 (void) load_sysv(service);
b3fae863 926
90e74a66 927 HASHMAP_FOREACH(service, all_services) {
c279613f
LP
928 (void) fix_order(service, all_services);
929 (void) generate_unit_file(service);
95ed3294
TA
930 }
931
47031c92 932 return 0;
95ed3294 933}
47031c92 934
7a44c7e3 935DEFINE_MAIN_GENERATOR_FUNCTION(run);