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