]>
| Commit | Line | Data |
|---|---|---|
| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
| 2 | ||
| 3 | #include <stdio.h> | |
| 4 | #include <stdlib.h> | |
| 5 | #include <unistd.h> | |
| 6 | ||
| 7 | #include "sd-messages.h" | |
| 8 | ||
| 9 | #include "alloc-util.h" | |
| 10 | #include "dirent-util.h" | |
| 11 | #include "exit-status.h" | |
| 12 | #include "extract-word.h" | |
| 13 | #include "fd-util.h" | |
| 14 | #include "fileio.h" | |
| 15 | #include "generator.h" | |
| 16 | #include "glyph-util.h" | |
| 17 | #include "hashmap.h" | |
| 18 | #include "hexdecoct.h" | |
| 19 | #include "initrd-util.h" | |
| 20 | #include "install.h" | |
| 21 | #include "log.h" | |
| 22 | #include "path-lookup.h" | |
| 23 | #include "path-util.h" | |
| 24 | #include "set.h" | |
| 25 | #include "special.h" | |
| 26 | #include "specifier.h" | |
| 27 | #include "stat-util.h" | |
| 28 | #include "string-util.h" | |
| 29 | #include "strv.h" | |
| 30 | #include "unit-name.h" | |
| 31 | ||
| 32 | /* 🚨 Note: this generator is deprecated! Please do not add new features! Instead, please port remaining SysV | |
| 33 | * scripts over to native unit files! Thank you! 🚨 */ | |
| 34 | ||
| 35 | static const struct { | |
| 36 | const char *path; | |
| 37 | const char *target; | |
| 38 | } rcnd_table[] = { | |
| 39 | /* Standard SysV runlevels for start-up */ | |
| 40 | { "rc1.d", SPECIAL_RESCUE_TARGET }, | |
| 41 | { "rc2.d", SPECIAL_MULTI_USER_TARGET }, | |
| 42 | { "rc3.d", SPECIAL_MULTI_USER_TARGET }, | |
| 43 | { "rc4.d", SPECIAL_MULTI_USER_TARGET }, | |
| 44 | { "rc5.d", SPECIAL_GRAPHICAL_TARGET }, | |
| 45 | ||
| 46 | /* We ignore the SysV runlevels for shutdown here, as SysV services get default dependencies anyway, and that | |
| 47 | * means they are shut down anyway at system power off if running. */ | |
| 48 | }; | |
| 49 | ||
| 50 | static const char *arg_dest = NULL; | |
| 51 | ||
| 52 | typedef struct SysvStub { | |
| 53 | char *name; | |
| 54 | char *path; | |
| 55 | char *description; | |
| 56 | int sysv_start_priority; | |
| 57 | char *pid_file; | |
| 58 | char **before; | |
| 59 | char **after; | |
| 60 | char **wants; | |
| 61 | char **wanted_by; | |
| 62 | bool has_lsb; | |
| 63 | bool reload; | |
| 64 | bool loaded; | |
| 65 | } SysvStub; | |
| 66 | ||
| 67 | static SysvStub* sysvstub_free(SysvStub *s) { | |
| 68 | if (!s) | |
| 69 | return NULL; | |
| 70 | ||
| 71 | free(s->name); | |
| 72 | free(s->path); | |
| 73 | free(s->description); | |
| 74 | free(s->pid_file); | |
| 75 | strv_free(s->before); | |
| 76 | strv_free(s->after); | |
| 77 | strv_free(s->wants); | |
| 78 | strv_free(s->wanted_by); | |
| 79 | return mfree(s); | |
| 80 | } | |
| 81 | DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, sysvstub_free); | |
| 82 | ||
| 83 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( | |
| 84 | sysvstub_hash_ops, | |
| 85 | char, string_hash_func, string_compare_func, | |
| 86 | SysvStub, sysvstub_free); | |
| 87 | ||
| 88 | static int add_alias(const char *service, const char *alias) { | |
| 89 | _cleanup_free_ char *link = NULL; | |
| 90 | ||
| 91 | assert(service); | |
| 92 | assert(alias); | |
| 93 | ||
| 94 | link = path_join(arg_dest, alias); | |
| 95 | if (!link) | |
| 96 | return -ENOMEM; | |
| 97 | ||
| 98 | if (symlink(service, link) < 0) { | |
| 99 | if (errno == EEXIST) | |
| 100 | return 0; | |
| 101 | ||
| 102 | return -errno; | |
| 103 | } | |
| 104 | ||
| 105 | return 1; | |
| 106 | } | |
| 107 | ||
| 108 | static int generate_unit_file(SysvStub *s) { | |
| 109 | _cleanup_free_ char *path_escaped = NULL, *unit = NULL; | |
| 110 | _cleanup_fclose_ FILE *f = NULL; | |
| 111 | int r; | |
| 112 | ||
| 113 | assert(s); | |
| 114 | ||
| 115 | if (!s->loaded) | |
| 116 | return 0; | |
| 117 | ||
| 118 | path_escaped = specifier_escape(s->path); | |
| 119 | if (!path_escaped) | |
| 120 | return log_oom(); | |
| 121 | ||
| 122 | unit = path_join(arg_dest, s->name); | |
| 123 | if (!unit) | |
| 124 | return log_oom(); | |
| 125 | ||
| 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 */ | |
| 129 | if (is_symlink(unit) > 0) { | |
| 130 | log_warning("Overwriting existing symlink %s with real service.", unit); | |
| 131 | (void) unlink(unit); | |
| 132 | } | |
| 133 | ||
| 134 | f = fopen(unit, "wxe"); | |
| 135 | if (!f) | |
| 136 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
| 137 | ||
| 138 | fprintf(f, | |
| 139 | "# Automatically generated by systemd-sysv-generator\n\n" | |
| 140 | "[Unit]\n" | |
| 141 | "Documentation=man:systemd-sysv-generator(8)\n" | |
| 142 | "SourcePath=%s\n", | |
| 143 | path_escaped); | |
| 144 | ||
| 145 | if (s->description) { | |
| 146 | _cleanup_free_ char *t = NULL; | |
| 147 | ||
| 148 | t = specifier_escape(s->description); | |
| 149 | if (!t) | |
| 150 | return log_oom(); | |
| 151 | ||
| 152 | fprintf(f, "Description=%s\n", t); | |
| 153 | } | |
| 154 | ||
| 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); | |
| 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 | ||
| 173 | if (s->pid_file) { | |
| 174 | _cleanup_free_ char *t = NULL; | |
| 175 | ||
| 176 | t = specifier_escape(s->pid_file); | |
| 177 | if (!t) | |
| 178 | return log_oom(); | |
| 179 | ||
| 180 | fprintf(f, "PIDFile=%s\n", t); | |
| 181 | } | |
| 182 | ||
| 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 | ||
| 190 | fprintf(f, | |
| 191 | "ExecStart=%s start\n" | |
| 192 | "ExecStop=%s stop\n", | |
| 193 | path_escaped, path_escaped); | |
| 194 | ||
| 195 | if (s->reload) | |
| 196 | fprintf(f, "ExecReload=%s reload\n", path_escaped); | |
| 197 | ||
| 198 | r = fflush_and_check(f); | |
| 199 | if (r < 0) | |
| 200 | return log_error_errno(r, "Failed to write unit %s: %m", unit); | |
| 201 | ||
| 202 | STRV_FOREACH(p, s->wanted_by) | |
| 203 | (void) generator_add_symlink(arg_dest, *p, "wants", s->name); | |
| 204 | ||
| 205 | return 1; | |
| 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) { | |
| 218 | _cleanup_free_ char *c = NULL; | |
| 219 | char *res; | |
| 220 | ||
| 221 | c = strdup(name); | |
| 222 | if (!c) | |
| 223 | return NULL; | |
| 224 | ||
| 225 | res = endswith(c, ".sh"); | |
| 226 | if (res) | |
| 227 | *res = 0; | |
| 228 | ||
| 229 | if (unit_name_mangle(c, 0, &res) < 0) | |
| 230 | return NULL; | |
| 231 | ||
| 232 | return res; | |
| 233 | } | |
| 234 | ||
| 235 | static int sysv_translate_facility(SysvStub *s, unsigned line, const char *name, char **ret) { | |
| 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 | ||
| 254 | _cleanup_free_ char *filename = NULL; | |
| 255 | const char *n; | |
| 256 | char *e, *m; | |
| 257 | int r; | |
| 258 | ||
| 259 | assert(name); | |
| 260 | assert(s); | |
| 261 | assert(ret); | |
| 262 | ||
| 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); | |
| 266 | ||
| 267 | n = *name == '$' ? name + 1 : name; | |
| 268 | ||
| 269 | for (size_t i = 0; i < ELEMENTSOF(table); i += 2) { | |
| 270 | if (!streq(table[i], n)) | |
| 271 | continue; | |
| 272 | ||
| 273 | if (!table[i+1]) { | |
| 274 | *ret = NULL; | |
| 275 | return 0; | |
| 276 | } | |
| 277 | ||
| 278 | m = strdup(table[i+1]); | |
| 279 | if (!m) | |
| 280 | return log_oom(); | |
| 281 | ||
| 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) | |
| 293 | return log_error_errno(r, "[%s:%u] Could not build name for facility %s: %m", s->path, line, name); | |
| 294 | ||
| 295 | return 1; | |
| 296 | } | |
| 297 | ||
| 298 | /* Strip ".sh" suffix from file name for comparison */ | |
| 299 | e = endswith(filename, ".sh"); | |
| 300 | if (e) | |
| 301 | *e = '\0'; | |
| 302 | ||
| 303 | /* Names equaling the file name of the services are redundant */ | |
| 304 | if (streq_ptr(n, filename)) { | |
| 305 | *ret = NULL; | |
| 306 | return 0; | |
| 307 | } | |
| 308 | ||
| 309 | /* Everything else we assume to be normal service names */ | |
| 310 | m = sysv_translate_name(n); | |
| 311 | if (!m) | |
| 312 | return log_oom(); | |
| 313 | ||
| 314 | *ret = m; | |
| 315 | return 1; | |
| 316 | } | |
| 317 | ||
| 318 | static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) { | |
| 319 | int r; | |
| 320 | ||
| 321 | assert(s); | |
| 322 | assert(full_text); | |
| 323 | assert(text); | |
| 324 | ||
| 325 | for (;;) { | |
| 326 | _cleanup_free_ char *word = NULL, *m = NULL; | |
| 327 | ||
| 328 | r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX); | |
| 329 | if (r < 0) | |
| 330 | return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line); | |
| 331 | if (r == 0) | |
| 332 | break; | |
| 333 | ||
| 334 | r = sysv_translate_facility(s, line, word, &m); | |
| 335 | if (r <= 0) /* continue on error */ | |
| 336 | continue; | |
| 337 | ||
| 338 | switch (unit_name_to_type(m)) { | |
| 339 | ||
| 340 | case UNIT_SERVICE: | |
| 341 | log_debug("Adding Provides: alias '%s' for '%s'", m, s->name); | |
| 342 | r = add_alias(s->name, m); | |
| 343 | if (r < 0) | |
| 344 | log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m); | |
| 345 | break; | |
| 346 | ||
| 347 | case UNIT_TARGET: | |
| 348 | ||
| 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! */ | |
| 354 | ||
| 355 | r = strv_extend(&s->before, m); | |
| 356 | if (r < 0) | |
| 357 | return log_oom(); | |
| 358 | ||
| 359 | r = strv_extend(&s->wants, m); | |
| 360 | if (r < 0) | |
| 361 | return log_oom(); | |
| 362 | ||
| 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(); | |
| 367 | r = strv_extend(&s->wants, SPECIAL_NETWORK_TARGET); | |
| 368 | if (r < 0) | |
| 369 | return log_oom(); | |
| 370 | } | |
| 371 | ||
| 372 | break; | |
| 373 | ||
| 374 | case _UNIT_TYPE_INVALID: | |
| 375 | log_warning("Unit name '%s' is invalid", m); | |
| 376 | break; | |
| 377 | ||
| 378 | default: | |
| 379 | log_warning("Unknown unit type for unit '%s'", m); | |
| 380 | } | |
| 381 | } | |
| 382 | ||
| 383 | return 0; | |
| 384 | } | |
| 385 | ||
| 386 | static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) { | |
| 387 | int r; | |
| 388 | ||
| 389 | assert(s); | |
| 390 | assert(full_text); | |
| 391 | assert(text); | |
| 392 | ||
| 393 | for (;;) { | |
| 394 | _cleanup_free_ char *word = NULL, *m = NULL; | |
| 395 | bool is_before; | |
| 396 | ||
| 397 | r = extract_first_word(&text, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX); | |
| 398 | if (r < 0) | |
| 399 | return log_error_errno(r, "[%s:%u] Failed to parse word from provides string: %m", s->path, line); | |
| 400 | if (r == 0) | |
| 401 | break; | |
| 402 | ||
| 403 | r = sysv_translate_facility(s, line, word, &m); | |
| 404 | if (r <= 0) /* continue on error */ | |
| 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(); | |
| 414 | ||
| 415 | r = strv_extend(&s->wants, m); | |
| 416 | } else | |
| 417 | r = strv_extend(is_before ? &s->before : &s->after, m); | |
| 418 | if (r < 0) | |
| 419 | return log_oom(); | |
| 420 | } | |
| 421 | ||
| 422 | return 0; | |
| 423 | } | |
| 424 | ||
| 425 | static int load_sysv(SysvStub *s) { | |
| 426 | _cleanup_fclose_ FILE *f = NULL; | |
| 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"); | |
| 443 | if (!f) { | |
| 444 | if (errno == ENOENT) | |
| 445 | return 0; | |
| 446 | ||
| 447 | return log_error_errno(errno, "Failed to open %s: %m", s->path); | |
| 448 | } | |
| 449 | ||
| 450 | log_debug("Loading SysV script %s", s->path); | |
| 451 | ||
| 452 | for (;;) { | |
| 453 | _cleanup_free_ char *l = NULL; | |
| 454 | ||
| 455 | r = read_stripped_line(f, LONG_LINE_MAX, &l); | |
| 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 | ||
| 461 | line++; | |
| 462 | ||
| 463 | if (l[0] != '#') { | |
| 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. */ | |
| 467 | if (state == USAGE_CONTINUATION || | |
| 468 | (state == NORMAL && strcasestr(l, "usage"))) { | |
| 469 | if (usage_contains_reload(l)) { | |
| 470 | supports_reload = true; | |
| 471 | state = NORMAL; | |
| 472 | } else if (endswith(l, "\\")) | |
| 473 | state = USAGE_CONTINUATION; | |
| 474 | else | |
| 475 | state = NORMAL; | |
| 476 | } | |
| 477 | ||
| 478 | continue; | |
| 479 | } | |
| 480 | ||
| 481 | if (state == NORMAL && streq(l, "### BEGIN INIT INFO")) { | |
| 482 | state = LSB; | |
| 483 | s->has_lsb = true; | |
| 484 | continue; | |
| 485 | } | |
| 486 | ||
| 487 | if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(l, "### END INIT INFO")) { | |
| 488 | state = NORMAL; | |
| 489 | continue; | |
| 490 | } | |
| 491 | ||
| 492 | char *t = l + 1; | |
| 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 | ||
| 501 | size_t k; | |
| 502 | const char *j; | |
| 503 | ||
| 504 | k = strlen(t); | |
| 505 | if (k > 0 && t[k-1] == '\\') { | |
| 506 | state = DESCRIPTION; | |
| 507 | t[k-1] = 0; | |
| 508 | } | |
| 509 | ||
| 510 | j = empty_to_null(strstrip(t+12)); | |
| 511 | ||
| 512 | r = free_and_strdup(&chkconfig_description, j); | |
| 513 | if (r < 0) | |
| 514 | return log_oom(); | |
| 515 | ||
| 516 | } else if (startswith_no_case(t, "pidfile:")) { | |
| 517 | const char *fn; | |
| 518 | ||
| 519 | state = NORMAL; | |
| 520 | ||
| 521 | fn = strstrip(t+8); | |
| 522 | if (!path_is_absolute(fn)) { | |
| 523 | log_error("[%s:%u] PID file not absolute. Ignoring.", s->path, line); | |
| 524 | continue; | |
| 525 | } | |
| 526 | ||
| 527 | r = free_and_strdup(&s->pid_file, fn); | |
| 528 | if (r < 0) | |
| 529 | return log_oom(); | |
| 530 | } | |
| 531 | ||
| 532 | } else if (state == DESCRIPTION) { | |
| 533 | ||
| 534 | /* Try to parse Red Hat style description | |
| 535 | * continuation */ | |
| 536 | ||
| 537 | size_t k; | |
| 538 | const char *j; | |
| 539 | ||
| 540 | k = strlen(t); | |
| 541 | if (k > 0 && t[k-1] == '\\') | |
| 542 | t[k-1] = 0; | |
| 543 | else | |
| 544 | state = NORMAL; | |
| 545 | ||
| 546 | j = strstrip(t); | |
| 547 | if (!isempty(j) && !strextend_with_separator(&chkconfig_description, " ", j)) | |
| 548 | return log_oom(); | |
| 549 | ||
| 550 | } else if (IN_SET(state, LSB, LSB_DESCRIPTION)) { | |
| 551 | ||
| 552 | if (startswith_no_case(t, "Provides:")) { | |
| 553 | state = LSB; | |
| 554 | ||
| 555 | r = handle_provides(s, line, t, t + 9); | |
| 556 | if (r < 0) | |
| 557 | return r; | |
| 558 | ||
| 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:")) { | |
| 563 | ||
| 564 | state = LSB; | |
| 565 | ||
| 566 | r = handle_dependencies(s, line, t, strchr(t, ':') + 1); | |
| 567 | if (r < 0) | |
| 568 | return r; | |
| 569 | ||
| 570 | } else if (startswith_no_case(t, "Description:")) { | |
| 571 | const char *j; | |
| 572 | ||
| 573 | state = LSB_DESCRIPTION; | |
| 574 | ||
| 575 | j = empty_to_null(strstrip(t+12)); | |
| 576 | ||
| 577 | r = free_and_strdup(&long_description, j); | |
| 578 | if (r < 0) | |
| 579 | return log_oom(); | |
| 580 | ||
| 581 | } else if (startswith_no_case(t, "Short-Description:")) { | |
| 582 | const char *j; | |
| 583 | ||
| 584 | state = LSB; | |
| 585 | ||
| 586 | j = empty_to_null(strstrip(t+18)); | |
| 587 | ||
| 588 | r = free_and_strdup(&short_description, j); | |
| 589 | if (r < 0) | |
| 590 | return log_oom(); | |
| 591 | ||
| 592 | } else if (state == LSB_DESCRIPTION) { | |
| 593 | ||
| 594 | if (startswith(l, "#\t") || startswith(l, "# ")) { | |
| 595 | const char *j; | |
| 596 | ||
| 597 | j = strstrip(t); | |
| 598 | if (!isempty(j) && !strextend_with_separator(&long_description, " ", j)) | |
| 599 | return log_oom(); | |
| 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 | ||
| 623 | d = strjoin(s->has_lsb ? "LSB: " : "SYSV: ", description); | |
| 624 | if (!d) | |
| 625 | return log_oom(); | |
| 626 | ||
| 627 | s->description = d; | |
| 628 | } | |
| 629 | ||
| 630 | s->loaded = true; | |
| 631 | return 0; | |
| 632 | } | |
| 633 | ||
| 634 | static int fix_order(SysvStub *s, Hashmap *all_services) { | |
| 635 | SysvStub *other; | |
| 636 | int r; | |
| 637 | ||
| 638 | assert(s); | |
| 639 | ||
| 640 | if (!s->loaded) | |
| 641 | return 0; | |
| 642 | ||
| 643 | if (s->sysv_start_priority < 0) | |
| 644 | return 0; | |
| 645 | ||
| 646 | HASHMAP_FOREACH(other, all_services) { | |
| 647 | if (s == other) | |
| 648 | continue; | |
| 649 | ||
| 650 | if (!other->loaded) | |
| 651 | continue; | |
| 652 | ||
| 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(); | |
| 665 | ||
| 666 | } else if (other->sysv_start_priority > s->sysv_start_priority) { | |
| 667 | r = strv_extend(&s->before, other->name); | |
| 668 | if (r < 0) | |
| 669 | return log_oom(); | |
| 670 | } else | |
| 671 | continue; | |
| 672 | ||
| 673 | /* FIXME: Maybe we should compare the name here lexicographically? */ | |
| 674 | } | |
| 675 | ||
| 676 | return 0; | |
| 677 | } | |
| 678 | ||
| 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 | ||
| 697 | l = strv_new(def); | |
| 698 | if (!l) | |
| 699 | return log_oom(); | |
| 700 | } | |
| 701 | ||
| 702 | if (!path_strv_resolve_uniq(l, NULL)) | |
| 703 | return log_oom(); | |
| 704 | ||
| 705 | *ret = TAKE_PTR(l); | |
| 706 | ||
| 707 | return 0; | |
| 708 | } | |
| 709 | ||
| 710 | static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) { | |
| 711 | _cleanup_strv_free_ char **sysvinit_path = NULL; | |
| 712 | int r; | |
| 713 | ||
| 714 | assert(lp); | |
| 715 | ||
| 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) { | |
| 721 | _cleanup_closedir_ DIR *d = NULL; | |
| 722 | ||
| 723 | d = opendir(*path); | |
| 724 | if (!d) { | |
| 725 | if (errno != ENOENT) | |
| 726 | log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path); | |
| 727 | continue; | |
| 728 | } | |
| 729 | ||
| 730 | FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) { | |
| 731 | _cleanup_free_ char *fpath = NULL, *name = NULL; | |
| 732 | _cleanup_(sysvstub_freep) SysvStub *service = NULL; | |
| 733 | struct stat st; | |
| 734 | ||
| 735 | if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) { | |
| 736 | log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name); | |
| 737 | continue; | |
| 738 | } | |
| 739 | ||
| 740 | if (!(st.st_mode & S_IXUSR)) | |
| 741 | continue; | |
| 742 | ||
| 743 | if (!S_ISREG(st.st_mode)) | |
| 744 | continue; | |
| 745 | ||
| 746 | name = sysv_translate_name(de->d_name); | |
| 747 | if (!name) | |
| 748 | return log_oom(); | |
| 749 | ||
| 750 | if (hashmap_contains(all_services, name)) | |
| 751 | continue; | |
| 752 | ||
| 753 | r = unit_file_exists(RUNTIME_SCOPE_SYSTEM, lp, name); | |
| 754 | if (r < 0 && !IN_SET(r, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) { | |
| 755 | log_debug_errno(r, "Failed to detect whether %s exists, skipping: %m", name); | |
| 756 | continue; | |
| 757 | } else if (r != 0) { | |
| 758 | log_debug("Native unit for %s already exists, skipping.", name); | |
| 759 | continue; | |
| 760 | } | |
| 761 | ||
| 762 | fpath = path_join(*path, de->d_name); | |
| 763 | if (!fpath) | |
| 764 | return log_oom(); | |
| 765 | ||
| 766 | log_struct(LOG_WARNING, | |
| 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" | |
| 770 | "%s This compatibility logic is deprecated, expect removal soon. %s", | |
| 771 | fpath, | |
| 772 | glyph(GLYPH_WARNING_SIGN), | |
| 773 | glyph(GLYPH_WARNING_SIGN)), | |
| 774 | LOG_MESSAGE_ID(SD_MESSAGE_SYSV_GENERATOR_DEPRECATED_STR), | |
| 775 | LOG_ITEM("SYSVSCRIPT=%s", fpath), | |
| 776 | LOG_ITEM("UNIT=%s", name)); | |
| 777 | ||
| 778 | service = new(SysvStub, 1); | |
| 779 | if (!service) | |
| 780 | return log_oom(); | |
| 781 | ||
| 782 | *service = (SysvStub) { | |
| 783 | .sysv_start_priority = -1, | |
| 784 | .name = TAKE_PTR(name), | |
| 785 | .path = TAKE_PTR(fpath), | |
| 786 | }; | |
| 787 | ||
| 788 | r = hashmap_put(all_services, service->name, service); | |
| 789 | if (r < 0) | |
| 790 | return log_oom(); | |
| 791 | ||
| 792 | TAKE_PTR(service); | |
| 793 | } | |
| 794 | } | |
| 795 | ||
| 796 | return 0; | |
| 797 | } | |
| 798 | ||
| 799 | static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) { | |
| 800 | Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {}; | |
| 801 | _cleanup_strv_free_ char **sysvrcnd_path = NULL; | |
| 802 | SysvStub *service; | |
| 803 | int r; | |
| 804 | ||
| 805 | assert(lp); | |
| 806 | ||
| 807 | r = acquire_search_path(SYSTEM_SYSVRCND_PATH, "SYSTEMD_SYSVRCND_PATH", &sysvrcnd_path); | |
| 808 | if (r < 0) | |
| 809 | return r; | |
| 810 | ||
| 811 | STRV_FOREACH(p, sysvrcnd_path) | |
| 812 | for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++) { | |
| 813 | _cleanup_closedir_ DIR *d = NULL; | |
| 814 | _cleanup_free_ char *path = NULL; | |
| 815 | ||
| 816 | path = path_join(*p, rcnd_table[i].path); | |
| 817 | if (!path) { | |
| 818 | r = log_oom(); | |
| 819 | goto finish; | |
| 820 | } | |
| 821 | ||
| 822 | d = opendir(path); | |
| 823 | if (!d) { | |
| 824 | if (errno != ENOENT) | |
| 825 | log_warning_errno(errno, "Opening %s failed, ignoring: %m", path); | |
| 826 | ||
| 827 | continue; | |
| 828 | } | |
| 829 | ||
| 830 | FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to enumerate directory %s, ignoring: %m", path)) { | |
| 831 | _cleanup_free_ char *name = NULL, *fpath = NULL; | |
| 832 | int a, b; | |
| 833 | ||
| 834 | if (de->d_name[0] != 'S') | |
| 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 | ||
| 846 | fpath = path_join(*p, de->d_name); | |
| 847 | if (!fpath) { | |
| 848 | r = log_oom(); | |
| 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 | ||
| 858 | service = hashmap_get(all_services, name); | |
| 859 | if (!service) { | |
| 860 | log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name); | |
| 861 | continue; | |
| 862 | } | |
| 863 | ||
| 864 | service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority); | |
| 865 | ||
| 866 | r = set_ensure_put(&runlevel_services[i], NULL, service); | |
| 867 | if (r < 0) { | |
| 868 | log_oom(); | |
| 869 | goto finish; | |
| 870 | } | |
| 871 | } | |
| 872 | } | |
| 873 | ||
| 874 | for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++) | |
| 875 | SET_FOREACH(service, runlevel_services[i]) { | |
| 876 | r = strv_extend(&service->before, rcnd_table[i].target); | |
| 877 | if (r < 0) { | |
| 878 | log_oom(); | |
| 879 | goto finish; | |
| 880 | } | |
| 881 | r = strv_extend(&service->wanted_by, rcnd_table[i].target); | |
| 882 | if (r < 0) { | |
| 883 | log_oom(); | |
| 884 | goto finish; | |
| 885 | } | |
| 886 | } | |
| 887 | ||
| 888 | r = 0; | |
| 889 | ||
| 890 | finish: | |
| 891 | for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i++) | |
| 892 | set_free(runlevel_services[i]); | |
| 893 | ||
| 894 | return r; | |
| 895 | } | |
| 896 | ||
| 897 | static int run(const char *dest, const char *dest_early, const char *dest_late) { | |
| 898 | _cleanup_hashmap_free_ Hashmap *all_services = NULL; | |
| 899 | _cleanup_(lookup_paths_done) LookupPaths lp = {}; | |
| 900 | SysvStub *service; | |
| 901 | int r; | |
| 902 | ||
| 903 | if (in_initrd()) { | |
| 904 | log_debug("Skipping generator, running in the initrd."); | |
| 905 | return EXIT_SUCCESS; | |
| 906 | } | |
| 907 | ||
| 908 | assert_se(arg_dest = dest_late); | |
| 909 | ||
| 910 | r = lookup_paths_init_or_warn(&lp, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL); | |
| 911 | if (r < 0) | |
| 912 | return r; | |
| 913 | ||
| 914 | all_services = hashmap_new(&sysvstub_hash_ops); | |
| 915 | if (!all_services) | |
| 916 | return log_oom(); | |
| 917 | ||
| 918 | r = enumerate_sysv(&lp, all_services); | |
| 919 | if (r < 0) | |
| 920 | return r; | |
| 921 | ||
| 922 | r = set_dependencies_from_rcnd(&lp, all_services); | |
| 923 | if (r < 0) | |
| 924 | return r; | |
| 925 | ||
| 926 | HASHMAP_FOREACH(service, all_services) | |
| 927 | (void) load_sysv(service); | |
| 928 | ||
| 929 | HASHMAP_FOREACH(service, all_services) { | |
| 930 | (void) fix_order(service, all_services); | |
| 931 | (void) generate_unit_file(service); | |
| 932 | } | |
| 933 | ||
| 934 | return 0; | |
| 935 | } | |
| 936 | ||
| 937 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |