]>
| Commit | Line | Data |
|---|---|---|
| db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 95ed3294 | 2 | |
| 95ed3294 | 3 | #include <stdio.h> |
| 8857aa74 | 4 | #include <stdlib.h> |
| 95ed3294 TA |
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" |
| 8857aa74 | 12 | #include "extract-word.h" |
| 3ffd4af2 | 13 | #include "fd-util.h" |
| c279613f | 14 | #include "fileio.h" |
| 7f0cc637 | 15 | #include "generator.h" |
| 8857aa74 | 16 | #include "glyph-util.h" |
| 07630cea | 17 | #include "hashmap.h" |
| 8fcde012 | 18 | #include "hexdecoct.h" |
| 3ed075cf | 19 | #include "initrd-util.h" |
| 07630cea LP |
20 | #include "install.h" |
| 21 | #include "log.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 |
35 | static 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 | 50 | static const char *arg_dest = NULL; |
| 8fba1c8d | 51 | |
| 95ed3294 TA |
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; | |
| 260ad50f | 61 | char **wanted_by; |
| 95ed3294 TA |
62 | bool has_lsb; |
| 63 | bool reload; | |
| c279613f | 64 | bool loaded; |
| 95ed3294 TA |
65 | } SysvStub; |
| 66 | ||
| 4100e0f2 | 67 | static SysvStub* sysvstub_free(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 | } |
| 4100e0f2 | 81 | DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, sysvstub_free); |
| 8fba1c8d | 82 | |
| 4100e0f2 YW |
83 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 84 | sysvstub_hash_ops, | |
| 85 | char, string_hash_func, string_compare_func, | |
| 86 | SysvStub, sysvstub_free); | |
| 95ed3294 | 87 | |
| b7e71846 | 88 | static int add_alias(const char *service, const char *alias) { |
| 8e7e4a73 | 89 | _cleanup_free_ char *link = NULL; |
| b7e71846 MB |
90 | |
| 91 | assert(service); | |
| 92 | assert(alias); | |
| 93 | ||
| 8e7e4a73 LP |
94 | link = path_join(arg_dest, alias); |
| 95 | if (!link) | |
| 96 | return -ENOMEM; | |
| b7e71846 | 97 | |
| 8e7e4a73 | 98 | if (symlink(service, link) < 0) { |
| b7e71846 MB |
99 | if (errno == EEXIST) |
| 100 | return 0; | |
| c279613f | 101 | |
| b7e71846 MB |
102 | return -errno; |
| 103 | } | |
| 104 | ||
| 105 | return 1; | |
| 106 | } | |
| 107 | ||
| 95ed3294 | 108 | static int generate_unit_file(SysvStub *s) { |
| 8e7e4a73 | 109 | _cleanup_free_ char *path_escaped = NULL, *unit = NULL; |
| 95ed3294 | 110 | _cleanup_fclose_ FILE *f = NULL; |
| 95ed3294 TA |
111 | int r; |
| 112 | ||
| c279613f LP |
113 | assert(s); |
| 114 | ||
| 115 | if (!s->loaded) | |
| 116 | return 0; | |
| 117 | ||
| 98bad05e LP |
118 | path_escaped = specifier_escape(s->path); |
| 119 | if (!path_escaped) | |
| 120 | return log_oom(); | |
| 121 | ||
| 8e7e4a73 LP |
122 | unit = path_join(arg_dest, s->name); |
| 123 | if (!unit) | |
| 124 | return log_oom(); | |
| c279613f | 125 | |
| 77354c7e MP |
126 | /* We might already have a symlink with the same name from a Provides:, |
| 127 | * or from backup files like /etc/init.d/foo.bak. Real scripts always win, | |
| 128 | * so remove an existing link */ | |
| 4e558983 | 129 | if (is_symlink(unit) > 0) { |
| c279613f | 130 | log_warning("Overwriting existing symlink %s with real service.", unit); |
| 9993ef2e | 131 | (void) unlink(unit); |
| 77354c7e MP |
132 | } |
| 133 | ||
| 95ed3294 | 134 | f = fopen(unit, "wxe"); |
| 4a62c710 MS |
135 | if (!f) |
| 136 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
| 95ed3294 TA |
137 | |
| 138 | fprintf(f, | |
| 139 | "# Automatically generated by systemd-sysv-generator\n\n" | |
| 140 | "[Unit]\n" | |
| aad0a2c8 | 141 | "Documentation=man:systemd-sysv-generator(8)\n" |
| c279613f | 142 | "SourcePath=%s\n", |
| 98bad05e LP |
143 | path_escaped); |
| 144 | ||
| 145 | if (s->description) { | |
| c2b2df60 | 146 | _cleanup_free_ char *t = NULL; |
| c279613f | 147 | |
| 98bad05e LP |
148 | t = specifier_escape(s->description); |
| 149 | if (!t) | |
| 150 | return log_oom(); | |
| 151 | ||
| 152 | fprintf(f, "Description=%s\n", t); | |
| 153 | } | |
| 95ed3294 | 154 | |
| c584ffc0 LN |
155 | STRV_FOREACH(p, s->before) |
| 156 | fprintf(f, "Before=%s\n", *p); | |
| 157 | STRV_FOREACH(p, s->after) | |
| 158 | fprintf(f, "After=%s\n", *p); | |
| 159 | STRV_FOREACH(p, s->wants) | |
| 160 | fprintf(f, "Wants=%s\n", *p); | |
| 95ed3294 TA |
161 | |
| 162 | fprintf(f, | |
| 163 | "\n[Service]\n" | |
| 164 | "Type=forking\n" | |
| 165 | "Restart=no\n" | |
| 166 | "TimeoutSec=5min\n" | |
| 167 | "IgnoreSIGPIPE=no\n" | |
| 168 | "KillMode=process\n" | |
| 169 | "GuessMainPID=no\n" | |
| 170 | "RemainAfterExit=%s\n", | |
| 171 | yes_no(!s->pid_file)); | |
| 172 | ||
| 98bad05e | 173 | if (s->pid_file) { |
| c2b2df60 | 174 | _cleanup_free_ char *t = NULL; |
| 98bad05e LP |
175 | |
| 176 | t = specifier_escape(s->pid_file); | |
| 177 | if (!t) | |
| 178 | return log_oom(); | |
| 179 | ||
| 180 | fprintf(f, "PIDFile=%s\n", t); | |
| 181 | } | |
| 95ed3294 | 182 | |
| 41e2036e LP |
183 | /* Consider two special LSB exit codes a clean exit */ |
| 184 | if (s->has_lsb) | |
| 185 | fprintf(f, | |
| 186 | "SuccessExitStatus=%i %i\n", | |
| 187 | EXIT_NOTINSTALLED, | |
| 188 | EXIT_NOTCONFIGURED); | |
| 189 | ||
| 95ed3294 TA |
190 | fprintf(f, |
| 191 | "ExecStart=%s start\n" | |
| 192 | "ExecStop=%s stop\n", | |
| 98bad05e | 193 | path_escaped, path_escaped); |
| 95ed3294 TA |
194 | |
| 195 | if (s->reload) | |
| 98bad05e | 196 | fprintf(f, "ExecReload=%s reload\n", path_escaped); |
| 95ed3294 | 197 | |
| c279613f LP |
198 | r = fflush_and_check(f); |
| 199 | if (r < 0) | |
| 200 | return log_error_errno(r, "Failed to write unit %s: %m", unit); | |
| 201 | ||
| 7f0cc637 ZJS |
202 | STRV_FOREACH(p, s->wanted_by) |
| 203 | (void) generator_add_symlink(arg_dest, *p, "wants", s->name); | |
| 95ed3294 | 204 | |
| c279613f | 205 | return 1; |
| 95ed3294 TA |
206 | } |
| 207 | ||
| 208 | static bool usage_contains_reload(const char *line) { | |
| 209 | return (strcasestr(line, "{reload|") || | |
| 210 | strcasestr(line, "{reload}") || | |
| 211 | strcasestr(line, "{reload\"") || | |
| 212 | strcasestr(line, "|reload|") || | |
| 213 | strcasestr(line, "|reload}") || | |
| 214 | strcasestr(line, "|reload\"")); | |
| 215 | } | |
| 216 | ||
| 217 | static char *sysv_translate_name(const char *name) { | |
| 0b2ec8a3 DH |
218 | _cleanup_free_ char *c = NULL; |
| 219 | char *res; | |
| 95ed3294 | 220 | |
| 264581a2 FS |
221 | c = strdup(name); |
| 222 | if (!c) | |
| 0b2ec8a3 | 223 | return NULL; |
| 95ed3294 | 224 | |
| 0b2ec8a3 DH |
225 | res = endswith(c, ".sh"); |
| 226 | if (res) | |
| 227 | *res = 0; | |
| 95ed3294 | 228 | |
| 37cbc1d5 | 229 | if (unit_name_mangle(c, 0, &res) < 0) |
| 0b2ec8a3 DH |
230 | return NULL; |
| 231 | ||
| 232 | return res; | |
| 95ed3294 TA |
233 | } |
| 234 | ||
| 7532e6d4 | 235 | static int sysv_translate_facility(SysvStub *s, unsigned line, const char *name, char **ret) { |
| 95ed3294 TA |
236 | |
| 237 | /* We silently ignore the $ prefix here. According to the LSB | |
| 238 | * spec it simply indicates whether something is a | |
| 239 | * standardized name or a distribution-specific one. Since we | |
| 240 | * just follow what already exists and do not introduce new | |
| 241 | * uses or names we don't care who introduced a new name. */ | |
| 242 | ||
| 243 | static const char * const table[] = { | |
| 244 | /* LSB defined facilities */ | |
| 245 | "local_fs", NULL, | |
| 246 | "network", SPECIAL_NETWORK_ONLINE_TARGET, | |
| 247 | "named", SPECIAL_NSS_LOOKUP_TARGET, | |
| 248 | "portmap", SPECIAL_RPCBIND_TARGET, | |
| 249 | "remote_fs", SPECIAL_REMOTE_FS_TARGET, | |
| 250 | "syslog", NULL, | |
| 251 | "time", SPECIAL_TIME_SYNC_TARGET, | |
| 252 | }; | |
| 253 | ||
| 1d8a8a7d | 254 | _cleanup_free_ char *filename = NULL; |
| 95ed3294 | 255 | const char *n; |
| 1d8a8a7d | 256 | char *e, *m; |
| c279613f | 257 | int r; |
| 95ed3294 TA |
258 | |
| 259 | assert(name); | |
| 7532e6d4 | 260 | assert(s); |
| c279613f | 261 | assert(ret); |
| 95ed3294 | 262 | |
| 1d8a8a7d LP |
263 | r = path_extract_filename(s->path, &filename); |
| 264 | if (r < 0) | |
| 265 | return log_error_errno(r, "Failed to extract file name from path '%s': %m", s->path); | |
| 7532e6d4 | 266 | |
| 95ed3294 TA |
267 | n = *name == '$' ? name + 1 : name; |
| 268 | ||
| 1d8a8a7d | 269 | for (size_t i = 0; i < ELEMENTSOF(table); i += 2) { |
| 95ed3294 TA |
270 | if (!streq(table[i], n)) |
| 271 | continue; | |
| 272 | ||
| e932f540 LP |
273 | if (!table[i+1]) { |
| 274 | *ret = NULL; | |
| 95ed3294 | 275 | return 0; |
| e932f540 | 276 | } |
| 95ed3294 | 277 | |
| c279613f LP |
278 | m = strdup(table[i+1]); |
| 279 | if (!m) | |
| 95ed3294 TA |
280 | return log_oom(); |
| 281 | ||
| c279613f LP |
282 | *ret = m; |
| 283 | return 1; | |
| 284 | } | |
| 285 | ||
| 286 | /* If we don't know this name, fallback heuristics to figure | |
| 287 | * out whether something is a target or a service alias. */ | |
| 288 | ||
| 289 | /* Facilities starting with $ are most likely targets */ | |
| 290 | if (*name == '$') { | |
| 291 | r = unit_name_build(n, NULL, ".target", ret); | |
| 292 | if (r < 0) | |
| 7532e6d4 | 293 | return log_error_errno(r, "[%s:%u] Could not build name for facility %s: %m", s->path, line, name); |
| c279613f | 294 | |
| e932f540 | 295 | return 1; |
| 95ed3294 TA |
296 | } |
| 297 | ||
| c279613f | 298 | /* Strip ".sh" suffix from file name for comparison */ |
| 1d8a8a7d LP |
299 | e = endswith(filename, ".sh"); |
| 300 | if (e) | |
| 4e488555 | 301 | *e = '\0'; |
| 29e0e6d8 | 302 | |
| c279613f | 303 | /* Names equaling the file name of the services are redundant */ |
| e932f540 LP |
304 | if (streq_ptr(n, filename)) { |
| 305 | *ret = NULL; | |
| 95ed3294 | 306 | return 0; |
| e932f540 | 307 | } |
| 95ed3294 | 308 | |
| c279613f LP |
309 | /* Everything else we assume to be normal service names */ |
| 310 | m = sysv_translate_name(n); | |
| 311 | if (!m) | |
| 312 | return log_oom(); | |
| 95ed3294 | 313 | |
| c279613f | 314 | *ret = m; |
| 95ed3294 TA |
315 | return 1; |
| 316 | } | |
| 317 | ||
| 1e2fee5f | 318 | static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) { |
| 1e2fee5f ZJS |
319 | int r; |
| 320 | ||
| c279613f LP |
321 | assert(s); |
| 322 | assert(full_text); | |
| 323 | assert(text); | |
| 1e2fee5f | 324 | |
| c279613f LP |
325 | for (;;) { |
| 326 | _cleanup_free_ char *word = NULL, *m = NULL; | |
| 1e2fee5f | 327 | |
| 4ec85141 | 328 | r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX); |
| 1e2fee5f | 329 | if (r < 0) |
| 7532e6d4 | 330 | return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line); |
| 1e2fee5f | 331 | if (r == 0) |
| c279613f LP |
332 | break; |
| 333 | ||
| 7532e6d4 | 334 | r = sysv_translate_facility(s, line, word, &m); |
| c279613f | 335 | if (r <= 0) /* continue on error */ |
| 1e2fee5f ZJS |
336 | continue; |
| 337 | ||
| c279613f LP |
338 | switch (unit_name_to_type(m)) { |
| 339 | ||
| 340 | case UNIT_SERVICE: | |
| 1e2fee5f ZJS |
341 | log_debug("Adding Provides: alias '%s' for '%s'", m, s->name); |
| 342 | r = add_alias(s->name, m); | |
| e987f2a8 | 343 | if (r < 0) |
| f2341e0a | 344 | log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m); |
| c279613f LP |
345 | break; |
| 346 | ||
| 347 | case UNIT_TARGET: | |
| 348 | ||
| 1e2fee5f ZJS |
349 | /* NB: SysV targets which are provided by a |
| 350 | * service are pulled in by the services, as | |
| 351 | * an indication that the generic service is | |
| 352 | * now available. This is strictly one-way. | |
| 353 | * The targets do NOT pull in SysV services! */ | |
| c279613f | 354 | |
| 1e2fee5f ZJS |
355 | r = strv_extend(&s->before, m); |
| 356 | if (r < 0) | |
| 357 | return log_oom(); | |
| c279613f | 358 | |
| 1e2fee5f ZJS |
359 | r = strv_extend(&s->wants, m); |
| 360 | if (r < 0) | |
| 361 | return log_oom(); | |
| c279613f | 362 | |
| 1e2fee5f ZJS |
363 | if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET)) { |
| 364 | r = strv_extend(&s->before, SPECIAL_NETWORK_TARGET); | |
| 365 | if (r < 0) | |
| 366 | return log_oom(); | |
| bd9ad4ff LN |
367 | r = strv_extend(&s->wants, SPECIAL_NETWORK_TARGET); |
| 368 | if (r < 0) | |
| 369 | return log_oom(); | |
| 1e2fee5f | 370 | } |
| c279613f LP |
371 | |
| 372 | break; | |
| 373 | ||
| 374 | case _UNIT_TYPE_INVALID: | |
| 2c09a745 | 375 | log_warning("Unit name '%s' is invalid", m); |
| c279613f LP |
376 | break; |
| 377 | ||
| 378 | default: | |
| 2c09a745 | 379 | log_warning("Unknown unit type for unit '%s'", m); |
| c279613f | 380 | } |
| 1e2fee5f | 381 | } |
| c279613f | 382 | |
| 1e2fee5f ZJS |
383 | return 0; |
| 384 | } | |
| 385 | ||
| 386 | static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) { | |
| 1e2fee5f ZJS |
387 | int r; |
| 388 | ||
| c279613f LP |
389 | assert(s); |
| 390 | assert(full_text); | |
| 391 | assert(text); | |
| 1e2fee5f | 392 | |
| c279613f LP |
393 | for (;;) { |
| 394 | _cleanup_free_ char *word = NULL, *m = NULL; | |
| 395 | bool is_before; | |
| 1e2fee5f | 396 | |
| 4ec85141 | 397 | r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX); |
| c279613f | 398 | if (r < 0) |
| 7532e6d4 | 399 | return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line); |
| 1e2fee5f | 400 | if (r == 0) |
| c279613f LP |
401 | break; |
| 402 | ||
| 7532e6d4 | 403 | r = sysv_translate_facility(s, line, word, &m); |
| c279613f | 404 | if (r <= 0) /* continue on error */ |
| 1e2fee5f ZJS |
405 | continue; |
| 406 | ||
| 407 | is_before = startswith_no_case(full_text, "X-Start-Before:"); | |
| 408 | ||
| 409 | if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET) && !is_before) { | |
| 410 | /* the network-online target is special, as it needs to be actively pulled in */ | |
| 411 | r = strv_extend(&s->after, m); | |
| 412 | if (r < 0) | |
| 413 | return log_oom(); | |
| c279613f | 414 | |
| 1e2fee5f | 415 | r = strv_extend(&s->wants, m); |
| e987f2a8 | 416 | } else |
| 1e2fee5f | 417 | r = strv_extend(is_before ? &s->before : &s->after, m); |
| 1e2fee5f | 418 | if (r < 0) |
| e987f2a8 | 419 | return log_oom(); |
| 1e2fee5f | 420 | } |
| c279613f | 421 | |
| 1e2fee5f ZJS |
422 | return 0; |
| 423 | } | |
| 424 | ||
| 95ed3294 | 425 | static int load_sysv(SysvStub *s) { |
| c2b2df60 | 426 | _cleanup_fclose_ FILE *f = NULL; |
| 95ed3294 TA |
427 | unsigned line = 0; |
| 428 | int r; | |
| 429 | enum { | |
| 430 | NORMAL, | |
| 431 | DESCRIPTION, | |
| 432 | LSB, | |
| 433 | LSB_DESCRIPTION, | |
| 434 | USAGE_CONTINUATION | |
| 435 | } state = NORMAL; | |
| 436 | _cleanup_free_ char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL; | |
| 437 | char *description; | |
| 438 | bool supports_reload = false; | |
| 439 | ||
| 440 | assert(s); | |
| 441 | ||
| 442 | f = fopen(s->path, "re"); | |
| c279613f LP |
443 | if (!f) { |
| 444 | if (errno == ENOENT) | |
| 445 | return 0; | |
| 95ed3294 | 446 | |
| c279613f LP |
447 | return log_error_errno(errno, "Failed to open %s: %m", s->path); |
| 448 | } | |
| 77354c7e | 449 | |
| c279613f | 450 | log_debug("Loading SysV script %s", s->path); |
| 95ed3294 | 451 | |
| e393eff6 LP |
452 | for (;;) { |
| 453 | _cleanup_free_ char *l = NULL; | |
| 95ed3294 | 454 | |
| 0ff6ff2b | 455 | r = read_stripped_line(f, LONG_LINE_MAX, &l); |
| e393eff6 LP |
456 | if (r < 0) |
| 457 | return log_error_errno(r, "Failed to read configuration file '%s': %m", s->path); | |
| 458 | if (r == 0) | |
| 459 | break; | |
| 460 | ||
| 95ed3294 TA |
461 | line++; |
| 462 | ||
| 0ff6ff2b | 463 | if (l[0] != '#') { |
| 95ed3294 TA |
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 || |
| 0ff6ff2b LP |
468 | (state == NORMAL && strcasestr(l, "usage"))) { |
| 469 | if (usage_contains_reload(l)) { | |
| 95ed3294 TA |
470 | supports_reload = true; |
| 471 | state = NORMAL; | |
| 0ff6ff2b | 472 | } else if (endswith(l, "\\")) |
| 95ed3294 TA |
473 | state = USAGE_CONTINUATION; |
| 474 | else | |
| 475 | state = NORMAL; | |
| 476 | } | |
| 477 | ||
| 478 | continue; | |
| 479 | } | |
| 480 | ||
| 0ff6ff2b | 481 | if (state == NORMAL && streq(l, "### BEGIN INIT INFO")) { |
| 95ed3294 TA |
482 | state = LSB; |
| 483 | s->has_lsb = true; | |
| 484 | continue; | |
| 485 | } | |
| 486 | ||
| 0ff6ff2b | 487 | if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(l, "### END INIT INFO")) { |
| 95ed3294 TA |
488 | state = NORMAL; |
| 489 | continue; | |
| 490 | } | |
| 491 | ||
| 0ff6ff2b | 492 | char *t = l + 1; |
| 95ed3294 TA |
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 | ||
| 634 | static 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 |
679 | static 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 | 710 | static 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; |
| 4100e0f2 | 732 | _cleanup_(sysvstub_freep) 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 | 766 | log_struct(LOG_WARNING, |
| bb56c27f ZJS |
767 | LOG_MESSAGE("SysV service '%s' lacks a native systemd unit file, " |
| 768 | "automatically generating a unit file for compatibility.\n" | |
| 769 | "Please update package to include a native systemd unit file.\n" | |
| f4d4ca6e LP |
770 | "%s This compatibility logic is deprecated, expect removal soon. %s", |
| 771 | fpath, | |
| 1ae9b0cf ZJS |
772 | glyph(GLYPH_WARNING_SIGN), |
| 773 | glyph(GLYPH_WARNING_SIGN)), | |
| 3cf6a3a3 YW |
774 | LOG_MESSAGE_ID(SD_MESSAGE_SYSV_GENERATOR_DEPRECATED_STR), |
| 775 | LOG_ITEM("SYSVSCRIPT=%s", fpath), | |
| 776 | LOG_ITEM("UNIT=%s", name)); | |
| 0e42cbe2 | 777 | |
| 470ab28d | 778 | service = new(SysvStub, 1); |
| 95ed3294 TA |
779 | if (!service) |
| 780 | return log_oom(); | |
| 781 | ||
| 470ab28d LP |
782 | *service = (SysvStub) { |
| 783 | .sysv_start_priority = -1, | |
| 784 | .name = TAKE_PTR(name), | |
| 785 | .path = TAKE_PTR(fpath), | |
| 786 | }; | |
| 95ed3294 TA |
787 | |
| 788 | r = hashmap_put(all_services, service->name, service); | |
| 805e5dda | 789 | if (r < 0) |
| 95ed3294 TA |
790 | return log_oom(); |
| 791 | ||
| 470ab28d | 792 | TAKE_PTR(service); |
| 95ed3294 TA |
793 | } |
| 794 | } | |
| 795 | ||
| 796 | return 0; | |
| 797 | } | |
| 798 | ||
| a8ffe6fb | 799 | static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) { |
| 95ed3294 | 800 | Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {}; |
| 4143c6c3 | 801 | _cleanup_strv_free_ char **sysvrcnd_path = NULL; |
| c279613f | 802 | SysvStub *service; |
| c279613f | 803 | int r; |
| 95ed3294 | 804 | |
| c279613f LP |
805 | assert(lp); |
| 806 | ||
| 4143c6c3 LP |
807 | r = acquire_search_path(SYSTEM_SYSVRCND_PATH, "SYSTEMD_SYSVRCND_PATH", &sysvrcnd_path); |
| 808 | if (r < 0) | |
| 809 | return r; | |
| 810 | ||
| e198eba7 | 811 | STRV_FOREACH(p, sysvrcnd_path) |
| b3a9d980 | 812 | for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++) { |
| c279613f LP |
813 | _cleanup_closedir_ DIR *d = NULL; |
| 814 | _cleanup_free_ char *path = NULL; | |
| 95ed3294 | 815 | |
| 657ee2d8 | 816 | path = path_join(*p, rcnd_table[i].path); |
| c279613f LP |
817 | if (!path) { |
| 818 | r = log_oom(); | |
| 819 | goto finish; | |
| 820 | } | |
| 95ed3294 TA |
821 | |
| 822 | d = opendir(path); | |
| 823 | if (!d) { | |
| 824 | if (errno != ENOENT) | |
| c279613f | 825 | log_warning_errno(errno, "Opening %s failed, ignoring: %m", path); |
| 95ed3294 TA |
826 | |
| 827 | continue; | |
| 828 | } | |
| 829 | ||
| 8746820b | 830 | FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to enumerate directory %s, ignoring: %m", path)) { |
| c279613f | 831 | _cleanup_free_ char *name = NULL, *fpath = NULL; |
| 95ed3294 TA |
832 | int a, b; |
| 833 | ||
| b9c59555 | 834 | if (de->d_name[0] != 'S') |
| 95ed3294 TA |
835 | continue; |
| 836 | ||
| 837 | if (strlen(de->d_name) < 4) | |
| 838 | continue; | |
| 839 | ||
| 840 | a = undecchar(de->d_name[1]); | |
| 841 | b = undecchar(de->d_name[2]); | |
| 842 | ||
| 843 | if (a < 0 || b < 0) | |
| 844 | continue; | |
| 845 | ||
| 657ee2d8 | 846 | fpath = path_join(*p, de->d_name); |
| 95ed3294 | 847 | if (!fpath) { |
| c279613f | 848 | r = log_oom(); |
| 95ed3294 TA |
849 | goto finish; |
| 850 | } | |
| 851 | ||
| 852 | name = sysv_translate_name(de->d_name + 3); | |
| 853 | if (!name) { | |
| 854 | r = log_oom(); | |
| 855 | goto finish; | |
| 856 | } | |
| 857 | ||
| 8c84621c | 858 | service = hashmap_get(all_services, name); |
| 9ed794a3 | 859 | if (!service) { |
| c279613f | 860 | log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name); |
| 95ed3294 TA |
861 | continue; |
| 862 | } | |
| 863 | ||
| b9c59555 | 864 | service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority); |
| 95ed3294 | 865 | |
| de7fef4b | 866 | r = set_ensure_put(&runlevel_services[i], NULL, service); |
| b9c59555 LP |
867 | if (r < 0) { |
| 868 | log_oom(); | |
| 869 | goto finish; | |
| 95ed3294 TA |
870 | } |
| 871 | } | |
| 872 | } | |
| 873 | ||
| e198eba7 | 874 | for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++) |
| 90e74a66 | 875 | SET_FOREACH(service, runlevel_services[i]) { |
| 95ed3294 | 876 | r = strv_extend(&service->before, rcnd_table[i].target); |
| c279613f LP |
877 | if (r < 0) { |
| 878 | log_oom(); | |
| 879 | goto finish; | |
| 880 | } | |
| 260ad50f | 881 | r = strv_extend(&service->wanted_by, rcnd_table[i].target); |
| c279613f LP |
882 | if (r < 0) { |
| 883 | log_oom(); | |
| 884 | goto finish; | |
| 885 | } | |
| 95ed3294 TA |
886 | } |
| 887 | ||
| 95ed3294 TA |
888 | r = 0; |
| 889 | ||
| 890 | finish: | |
| e198eba7 | 891 | for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++) |
| 95ed3294 TA |
892 | set_free(runlevel_services[i]); |
| 893 | ||
| 894 | return r; | |
| 895 | } | |
| 896 | ||
| 7a44c7e3 | 897 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
| 4100e0f2 | 898 | _cleanup_hashmap_free_ Hashmap *all_services = NULL; |
| 7dfc7139 | 899 | _cleanup_(lookup_paths_done) LookupPaths lp = {}; |
| 95ed3294 | 900 | SysvStub *service; |
| c279613f | 901 | int r; |
| 95ed3294 | 902 | |
| 3ed075cf LP |
903 | if (in_initrd()) { |
| 904 | log_debug("Skipping generator, running in the initrd."); | |
| 905 | return EXIT_SUCCESS; | |
| 906 | } | |
| 907 | ||
| 7a44c7e3 | 908 | assert_se(arg_dest = dest_late); |
| 95ed3294 | 909 | |
| 4870133b | 910 | r = lookup_paths_init_or_warn(&lp, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL); |
| 47031c92 | 911 | if (r < 0) |
| 99aad9a2 | 912 | return r; |
| 95ed3294 | 913 | |
| 4100e0f2 | 914 | all_services = hashmap_new(&sysvstub_hash_ops); |
| 47031c92 ZJS |
915 | if (!all_services) |
| 916 | return log_oom(); | |
| 95ed3294 | 917 | |
| a8ffe6fb | 918 | r = enumerate_sysv(&lp, all_services); |
| c279613f | 919 | if (r < 0) |
| 47031c92 | 920 | return r; |
| 95ed3294 | 921 | |
| a8ffe6fb | 922 | r = set_dependencies_from_rcnd(&lp, all_services); |
| c279613f | 923 | if (r < 0) |
| 47031c92 | 924 | return r; |
| 95ed3294 | 925 | |
| 90e74a66 | 926 | HASHMAP_FOREACH(service, all_services) |
| c279613f | 927 | (void) load_sysv(service); |
| b3fae863 | 928 | |
| 90e74a66 | 929 | HASHMAP_FOREACH(service, all_services) { |
| c279613f LP |
930 | (void) fix_order(service, all_services); |
| 931 | (void) generate_unit_file(service); | |
| 95ed3294 TA |
932 | } |
| 933 | ||
| 47031c92 | 934 | return 0; |
| 95ed3294 | 935 | } |
| 47031c92 | 936 | |
| 7a44c7e3 | 937 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |