service.c 134 KB
Newer Older
1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2

3 4 5 6 7 8
/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
9 10
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
11 12 13 14 15
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
  Lesser General Public License for more details.
17

18
  You should have received a copy of the GNU Lesser General Public License
19 20 21
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

22
#include <errno.h>
23
#include <signal.h>
24 25
#include <dirent.h>
#include <unistd.h>
26
#include <sys/reboot.h>
27

28
#include "manager.h"
Lennart Poettering's avatar
Lennart Poettering committed
29
#include "unit.h"
30 31 32
#include "service.h"
#include "load-fragment.h"
#include "load-dropin.h"
33
#include "log.h"
34
#include "strv.h"
35
#include "unit-name.h"
36
#include "unit-printf.h"
37
#include "dbus-service.h"
38
#include "special.h"
39
#include "bus-errors.h"
40
#include "exit-status.h"
41
#include "def.h"
Kay Sievers's avatar
Kay Sievers committed
42
#include "path-util.h"
43
#include "util.h"
44
#include "utf8.h"
45

46
#ifdef HAVE_SYSV_COMPAT
47

48
#define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
49

50 51
typedef enum RunlevelType {
        RUNLEVEL_UP,
52
        RUNLEVEL_DOWN
53 54 55 56 57 58 59
} RunlevelType;

static const struct {
        const char *path;
        const char *target;
        const RunlevelType type;
} rcnd_table[] = {
60
        /* Standard SysV runlevels for start-up */
61
        { "rc1.d",  SPECIAL_RESCUE_TARGET,    RUNLEVEL_UP },
62 63 64 65 66
        { "rc2.d",  SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
        { "rc3.d",  SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
        { "rc4.d",  SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
        { "rc5.d",  SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },

67 68 69 70 71 72 73 74
        /* Standard SysV runlevels for shutdown */
        { "rc0.d",  SPECIAL_POWEROFF_TARGET,  RUNLEVEL_DOWN },
        { "rc6.d",  SPECIAL_REBOOT_TARGET,    RUNLEVEL_DOWN }

        /* Note that the order here matters, as we read the
           directories in this order, and we want to make sure that
           sysv_start_priority is known when we first load the
           unit. And that value we only know from S links. Hence
75
           UP must be read before DOWN */
76 77
};

78
#define RUNLEVELS_UP "12345"
79
#endif
80

81
static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
Lennart Poettering's avatar
Lennart Poettering committed
82 83 84 85 86
        [SERVICE_DEAD] = UNIT_INACTIVE,
        [SERVICE_START_PRE] = UNIT_ACTIVATING,
        [SERVICE_START] = UNIT_ACTIVATING,
        [SERVICE_START_POST] = UNIT_ACTIVATING,
        [SERVICE_RUNNING] = UNIT_ACTIVE,
Lennart Poettering's avatar
Lennart Poettering committed
87
        [SERVICE_EXITED] = UNIT_ACTIVE,
88
        [SERVICE_RELOAD] = UNIT_RELOADING,
Lennart Poettering's avatar
Lennart Poettering committed
89 90 91 92 93 94
        [SERVICE_STOP] = UNIT_DEACTIVATING,
        [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
        [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
        [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
        [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
        [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
95
        [SERVICE_FAILED] = UNIT_FAILED,
96
        [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
97
};
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/* For Type=idle we never want to delay any other jobs, hence we
 * consider idle jobs active as soon as we start working on them */
static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
        [SERVICE_DEAD] = UNIT_INACTIVE,
        [SERVICE_START_PRE] = UNIT_ACTIVE,
        [SERVICE_START] = UNIT_ACTIVE,
        [SERVICE_START_POST] = UNIT_ACTIVE,
        [SERVICE_RUNNING] = UNIT_ACTIVE,
        [SERVICE_EXITED] = UNIT_ACTIVE,
        [SERVICE_RELOAD] = UNIT_RELOADING,
        [SERVICE_STOP] = UNIT_DEACTIVATING,
        [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
        [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
        [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
        [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
        [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
        [SERVICE_FAILED] = UNIT_FAILED,
        [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
};

119 120 121 122
static void service_init(Unit *u) {
        Service *s = SERVICE(u);

        assert(u);
Michal Schmidt's avatar
Michal Schmidt committed
123
        assert(u->load_state == UNIT_STUB);
124

125 126
        s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
        s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
127
        s->restart_usec = DEFAULT_RESTART_USEC;
128
        s->type = _SERVICE_TYPE_INVALID;
129 130 131

        s->watchdog_watch.type = WATCH_INVALID;

132
        s->timer_watch.type = WATCH_INVALID;
133
#ifdef HAVE_SYSV_COMPAT
134
        s->sysv_start_priority = -1;
135
        s->sysv_start_priority_from_rcnd = -1;
136
#endif
137
        s->socket_fd = -1;
138
        s->guess_main_pid = true;
139 140

        exec_context_init(&s->exec_context);
141
        kill_context_init(&s->kill_context);
142

143
        RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
144 145 146 147

        s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
}

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static void service_unwatch_control_pid(Service *s) {
        assert(s);

        if (s->control_pid <= 0)
                return;

        unit_unwatch_pid(UNIT(s), s->control_pid);
        s->control_pid = 0;
}

static void service_unwatch_main_pid(Service *s) {
        assert(s);

        if (s->main_pid <= 0)
                return;

        unit_unwatch_pid(UNIT(s), s->main_pid);
        s->main_pid = 0;
}

168 169 170 171 172 173 174 175 176 177 178
static void service_unwatch_pid_file(Service *s) {
        if (!s->pid_file_pathspec)
                return;

        log_debug("Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
        path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
        path_spec_done(s->pid_file_pathspec);
        free(s->pid_file_pathspec);
        s->pid_file_pathspec = NULL;
}

179
static int service_set_main_pid(Service *s, pid_t pid) {
180 181
        pid_t ppid;

182 183 184 185 186 187 188 189
        assert(s);

        if (pid <= 1)
                return -EINVAL;

        if (pid == getpid())
                return -EINVAL;

190 191 192 193
        s->main_pid = pid;
        s->main_pid_known = true;

        if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
194
                log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
195
                            UNIT(s)->id, (unsigned long) pid);
196

197 198 199
                s->main_pid_alien = true;
        } else
                s->main_pid_alien = false;
200

201 202
        exec_status_start(&s->main_exec_status, pid);

203 204 205
        return 0;
}

206 207 208 209 210 211 212 213 214 215
static void service_close_socket_fd(Service *s) {
        assert(s);

        if (s->socket_fd < 0)
                return;

        close_nointr_nofail(s->socket_fd);
        s->socket_fd = -1;
}

216 217 218
static void service_connection_unref(Service *s) {
        assert(s);

219
        if (!UNIT_DEREF(s->accept_socket))
220 221
                return;

222 223
        socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
        unit_ref_unset(&s->accept_socket);
224 225
}

226 227 228
static void service_stop_watchdog(Service *s) {
        assert(s);

229
        unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
230 231 232 233
        s->watchdog_timestamp.realtime = 0;
        s->watchdog_timestamp.monotonic = 0;
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);

static void service_handle_watchdog(Service *s) {
        usec_t offset;
        int r;

        assert(s);

        if (s->watchdog_usec == 0)
                return;

        offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
        if (offset >= s->watchdog_usec) {
                log_error("%s watchdog timeout!", UNIT(s)->id);
                service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
                return;
        }

        r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
        if (r < 0)
                log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
}

257 258 259 260
static void service_reset_watchdog(Service *s) {
        assert(s);

        dual_timestamp_get(&s->watchdog_timestamp);
261
        service_handle_watchdog(s);
262 263
}

Lennart Poettering's avatar
Lennart Poettering committed
264 265
static void service_done(Unit *u) {
        Service *s = SERVICE(u);
Lennart Poettering's avatar
Lennart Poettering committed
266 267 268 269 270 271

        assert(s);

        free(s->pid_file);
        s->pid_file = NULL;

272
#ifdef HAVE_SYSV_COMPAT
273 274
        free(s->sysv_runlevels);
        s->sysv_runlevels = NULL;
275
#endif
276

277 278 279
        free(s->status_text);
        s->status_text = NULL;

Lennart Poettering's avatar
Lennart Poettering committed
280
        exec_context_done(&s->exec_context);
281
        exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
Lennart Poettering's avatar
Lennart Poettering committed
282
        s->control_command = NULL;
283
        s->main_command = NULL;
Lennart Poettering's avatar
Lennart Poettering committed
284

285 286 287 288 289 290 291 292 293 294
        set_free(s->restart_ignore_status.code);
        s->restart_ignore_status.code = NULL;
        set_free(s->restart_ignore_status.signal);
        s->restart_ignore_status.signal = NULL;

        set_free(s->success_status.code);
        s->success_status.code = NULL;
        set_free(s->success_status.signal);
        s->success_status.signal = NULL;

Lennart Poettering's avatar
Lennart Poettering committed
295 296
        /* This will leak a process, but at least no memory or any of
         * our resources */
297 298
        service_unwatch_main_pid(s);
        service_unwatch_control_pid(s);
299
        service_unwatch_pid_file(s);
Lennart Poettering's avatar
Lennart Poettering committed
300

301
        if (s->bus_name)  {
Michal Schmidt's avatar
Michal Schmidt committed
302
                unit_unwatch_bus_name(u, s->bus_name);
303 304 305 306
                free(s->bus_name);
                s->bus_name = NULL;
        }

307
        service_close_socket_fd(s);
308
        service_connection_unref(s);
309

310
        unit_ref_unset(&s->accept_socket);
311

312 313
        service_stop_watchdog(s);

314
        unit_unwatch_timer(u, &s->timer_watch);
Lennart Poettering's avatar
Lennart Poettering committed
315 316
}

317
#ifdef HAVE_SYSV_COMPAT
318 319 320 321 322 323
static char *sysv_translate_name(const char *name) {
        char *r;

        if (!(r = new(char, strlen(name) + sizeof(".service"))))
                return NULL;

Koen Kooi's avatar
Koen Kooi committed
324
#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
325 326 327 328
        if (endswith(name, ".sh"))
                /* Drop Debian-style .sh suffix */
                strcpy(stpcpy(r, name) - 3, ".service");
#endif
329
#ifdef TARGET_FRUGALWARE
330
        if (startswith(name, "rc."))
331 332
                /* Drop Frugalware-style rc. prefix */
                strcpy(stpcpy(r, name + 3), ".service");
Tom Gundersen's avatar
Tom Gundersen committed
333
#endif
334 335 336 337 338 339 340
        else
                /* Normal init scripts */
                strcpy(stpcpy(r, name), ".service");

        return r;
}

341
static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
342

343 344 345 346 347 348
        /* We silently ignore the $ prefix here. According to the LSB
         * spec it simply indicates whether something is a
         * standardized name or a distribution-specific one. Since we
         * just follow what already exists and do not introduce new
         * uses or names we don't care who introduced a new name. */

349
        static const char * const table[] = {
350
                /* LSB defined facilities */
351
                "local_fs",             SPECIAL_LOCAL_FS_TARGET,
Dexter Morgan's avatar
Dexter Morgan committed
352 353
#if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
#else
354 355 356 357
                /* Due to unfortunate name selection in Mandriva,
                 * $network is provided by network-up which is ordered
                 * after network which actually starts interfaces.
                 * To break the loop, just ignore it */
358
                "network",              SPECIAL_NETWORK_TARGET,
359
#endif
360 361 362 363
                "named",                SPECIAL_NSS_LOOKUP_TARGET,
                "portmap",              SPECIAL_RPCBIND_TARGET,
                "remote_fs",            SPECIAL_REMOTE_FS_TARGET,
                "syslog",               SPECIAL_SYSLOG_TARGET,
364
                "time",                 SPECIAL_TIME_SYNC_TARGET,
365

Kay Sievers's avatar
Kay Sievers committed
366 367 368 369 370
                /* common extensions */
                "mail-transfer-agent",  SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
                "x-display-manager",    SPECIAL_DISPLAY_MANAGER_SERVICE,
                "null",                 NULL,

Koen Kooi's avatar
Koen Kooi committed
371
#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
372
                "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
373 374
#endif

Kay Sievers's avatar
Kay Sievers committed
375 376 377
#ifdef TARGET_SUSE
                "smtp",                 SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
#endif
378 379 380 381
        };

        unsigned i;
        char *r;
382
        const char *n;
383

384 385
        assert(name);
        assert(_r);
386

387
        n = *name == '$' ? name + 1 : name;
388

389
        for (i = 0; i < ELEMENTSOF(table); i += 2) {
390

391 392
                if (!streq(table[i], n))
                        continue;
393

394 395 396 397 398 399 400 401
                if (!table[i+1])
                        return 0;

                if (!(r = strdup(table[i+1])))
                        return -ENOMEM;

                goto finish;
        }
402

403
        /* If we don't know this name, fallback heuristics to figure
404
         * out whether something is a target or a service alias. */
405

406 407 408 409
        if (*name == '$') {
                if (!unit_prefix_is_valid(n))
                        return -EINVAL;

410 411
                /* Facilities starting with $ are most likely targets */
                r = unit_name_build(n, NULL, ".target");
412
        } else if (filename && streq(name, filename))
Harald Hoyer's avatar
Harald Hoyer committed
413
                /* Names equaling the file name of the services are redundant */
414
                return 0;
415
        else
416 417
                /* Everything else we assume to be normal service names */
                r = sysv_translate_name(n);
418

419
        if (!r)
420 421 422
                return -ENOMEM;

finish:
423
        *_r = r;
424 425 426 427

        return 1;
}

428
static int sysv_fix_order(Service *s) {
Michal Schmidt's avatar
Michal Schmidt committed
429
        Unit *other;
430 431 432 433 434 435 436
        int r;

        assert(s);

        if (s->sysv_start_priority < 0)
                return 0;

437 438
        /* For each pair of services where at least one lacks a LSB
         * header, we use the start priority value to order things. */
439

440
        LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
441 442
                Service *t;
                UnitDependency d;
443
                bool special_s, special_t;
444

445
                t = SERVICE(other);
446 447 448

                if (s == t)
                        continue;
449

450
                if (UNIT(t)->load_state != UNIT_LOADED)
451
                        continue;
452 453 454 455

                if (t->sysv_start_priority < 0)
                        continue;

456 457
                /* If both units have modern headers we don't care
                 * about the priorities */
458 459
                if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
                    (UNIT(t)->fragment_path || t->sysv_has_lsb))
460 461
                        continue;

462 463 464 465 466 467 468 469
                special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
                special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);

                if (special_t && !special_s)
                        d = UNIT_AFTER;
                else if (special_s && !special_t)
                        d = UNIT_BEFORE;
                else if (t->sysv_start_priority < s->sysv_start_priority)
470 471 472 473 474 475 476 477
                        d = UNIT_AFTER;
                else if (t->sysv_start_priority > s->sysv_start_priority)
                        d = UNIT_BEFORE;
                else
                        continue;

                /* FIXME: Maybe we should compare the name here lexicographically? */

Lennart Poettering's avatar
Lennart Poettering committed
478
                if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
                        return r;
        }

        return 0;
}

static ExecCommand *exec_command_new(const char *path, const char *arg1) {
        ExecCommand *c;

        if (!(c = new0(ExecCommand, 1)))
                return NULL;

        if (!(c->path = strdup(path))) {
                free(c);
                return NULL;
        }

        if (!(c->argv = strv_new(path, arg1, NULL))) {
                free(c->path);
                free(c);
                return NULL;
        }

        return c;
}

505
static int sysv_exec_commands(Service *s, const bool supports_reload) {
506 507 508
        ExecCommand *c;

        assert(s);
509 510
        assert(s->is_sysv);
        assert(UNIT(s)->source_path);
511

512 513
        c = exec_command_new(UNIT(s)->source_path, "start");
        if (!c)
514 515 516
                return -ENOMEM;
        exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);

517 518
        c = exec_command_new(UNIT(s)->source_path, "stop");
        if (!c)
519 520 521
                return -ENOMEM;
        exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);

522 523 524 525 526 527
        if (supports_reload) {
                c = exec_command_new(UNIT(s)->source_path, "reload");
                if (!c)
                        return -ENOMEM;
                exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
        }
528 529 530 531

        return 0;
}

532 533 534 535 536 537 538 539 540
static bool usage_contains_reload(const char *line) {
        return (strcasestr(line, "{reload|") ||
                strcasestr(line, "{reload}") ||
                strcasestr(line, "{reload\"") ||
                strcasestr(line, "|reload|") ||
                strcasestr(line, "|reload}") ||
                strcasestr(line, "|reload\""));
}

541
static int service_load_sysv_path(Service *s, const char *path) {
542 543 544 545 546 547 548 549
        FILE *f;
        Unit *u;
        unsigned line = 0;
        int r;
        enum {
                NORMAL,
                DESCRIPTION,
                LSB,
550 551
                LSB_DESCRIPTION,
                USAGE_CONTINUATION
552
        } state = NORMAL;
553
        char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
554
        struct stat st;
555
        bool supports_reload = false;
556 557 558

        assert(s);
        assert(path);
559 560 561

        u = UNIT(s);

562 563
        f = fopen(path, "re");
        if (!f) {
564 565 566 567
                r = errno == ENOENT ? 0 : -errno;
                goto finish;
        }

568 569 570 571 572
        if (fstat(fileno(f), &st) < 0) {
                r = -errno;
                goto finish;
        }

573 574 575
        free(u->source_path);
        u->source_path = strdup(path);
        if (!u->source_path) {
576 577 578
                r = -ENOMEM;
                goto finish;
        }
579
        u->source_mtime = timespec_load(&st.st_mtim);
580 581

        if (null_or_empty(&st)) {
Michal Schmidt's avatar
Michal Schmidt committed
582
                u->load_state = UNIT_MASKED;
583 584 585 586
                r = 0;
                goto finish;
        }

587 588
        s->is_sysv = true;

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
        while (!feof(f)) {
                char l[LINE_MAX], *t;

                if (!fgets(l, sizeof(l), f)) {
                        if (feof(f))
                                break;

                        r = -errno;
                        log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
                        goto finish;
                }

                line++;

                t = strstrip(l);
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
                if (*t != '#') {
                        /* Try to figure out whether this init script supports
                         * the reload operation. This heuristic looks for
                         * "Usage" lines which include the reload option. */
                        if ( state == USAGE_CONTINUATION ||
                            (state == NORMAL && strcasestr(t, "usage"))) {
                                if (usage_contains_reload(t)) {
                                        supports_reload = true;
                                        state = NORMAL;
                                } else if (t[strlen(t)-1] == '\\')
                                        state = USAGE_CONTINUATION;
                                else
                                        state = NORMAL;
                        }

619
                        continue;
620
                }
621 622 623

                if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
                        state = LSB;
624
                        s->sysv_has_lsb = true;
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
                        continue;
                }

                if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
                        state = NORMAL;
                        continue;
                }

                t++;
                t += strspn(t, WHITESPACE);

                if (state == NORMAL) {

                        /* Try to parse Red Hat style chkconfig headers */

640
                        if (startswith_no_case(t, "chkconfig:")) {
641
                                int start_priority;
642
                                char runlevels[16], *k;
643 644 645

                                state = NORMAL;

646 647 648
                                if (sscanf(t+10, "%15s %i %*i",
                                           runlevels,
                                           &start_priority) != 2) {
649 650 651 652 653

                                        log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
                                        continue;
                                }

654 655 656
                                /* A start priority gathered from the
                                 * symlink farms is preferred over the
                                 * data from the LSB header. */
657
                                if (start_priority < 0 || start_priority > 99)
658
                                        log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
659
                                else
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
                                        s->sysv_start_priority = start_priority;

                                char_array_0(runlevels);
                                k = delete_chars(runlevels, WHITESPACE "-");

                                if (k[0]) {
                                        char *d;

                                        if (!(d = strdup(k))) {
                                                r = -ENOMEM;
                                                goto finish;
                                        }

                                        free(s->sysv_runlevels);
                                        s->sysv_runlevels = d;
675 676
                                }

677
                        } else if (startswith_no_case(t, "description:")) {
678 679 680

                                size_t k = strlen(t);
                                char *d;
681
                                const char *j;
682 683 684 685 686 687

                                if (t[k-1] == '\\') {
                                        state = DESCRIPTION;
                                        t[k-1] = 0;
                                }

688 689 690 691 692 693 694
                                if ((j = strstrip(t+12)) && *j) {
                                        if (!(d = strdup(j))) {
                                                r = -ENOMEM;
                                                goto finish;
                                        }
                                } else
                                        d = NULL;
695

696 697
                                free(chkconfig_description);
                                chkconfig_description = d;
698

699
                        } else if (startswith_no_case(t, "pidfile:")) {
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

                                char *fn;

                                state = NORMAL;

                                fn = strstrip(t+8);
                                if (!path_is_absolute(fn)) {
                                        log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
                                        continue;
                                }

                                if (!(fn = strdup(fn))) {
                                        r = -ENOMEM;
                                        goto finish;
                                }

                                free(s->pid_file);
                                s->pid_file = fn;
                        }

                } else if (state == DESCRIPTION) {

                        /* Try to parse Red Hat style description
                         * continuation */

                        size_t k = strlen(t);
726
                        char *j;
727 728 729 730 731 732

                        if (t[k-1] == '\\')
                                t[k-1] = 0;
                        else
                                state = NORMAL;

733 734 735 736
                        if ((j = strstrip(t)) && *j) {
                                char *d = NULL;

                                if (chkconfig_description)
737
                                        d = strjoin(chkconfig_description, " ", j, NULL);
738 739
                                else
                                        d = strdup(j);
740

741 742 743 744 745 746 747 748
                                if (!d) {
                                        r = -ENOMEM;
                                        goto finish;
                                }

                                free(chkconfig_description);
                                chkconfig_description = d;
                        }
749 750 751

                } else if (state == LSB || state == LSB_DESCRIPTION) {

752
                        if (startswith_no_case(t, "Provides:")) {
753 754 755 756 757
                                char *i, *w;
                                size_t z;

                                state = LSB;

758
                                FOREACH_WORD_QUOTED(w, z, t+9, i) {
759 760 761 762 763 764 765
                                        char *n, *m;

                                        if (!(n = strndup(w, z))) {
                                                r = -ENOMEM;
                                                goto finish;
                                        }

Kay Sievers's avatar
Kay Sievers committed
766
                                        r = sysv_translate_facility(n, path_get_file_name(path), &m);
767 768 769 770 771 772 773 774
                                        free(n);

                                        if (r < 0)
                                                goto finish;

                                        if (r == 0)
                                                continue;

775 776
                                        if (unit_name_to_type(m) == UNIT_SERVICE)
                                                r = unit_add_name(u, m);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
                                        else
                                                /* NB: SysV targets
                                                 * which are provided
                                                 * by a service are
                                                 * pulled in by the
                                                 * services, as an
                                                 * indication that the
                                                 * generic service is
                                                 * now available. This
                                                 * is strictly
                                                 * one-way. The
                                                 * targets do NOT pull
                                                 * in the SysV
                                                 * services! */
                                                r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
792

793
                                        if (r < 0)
794
                                                log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
795 796

                                        free(m);
797 798
                                }

799 800 801 802
                        } else if (startswith_no_case(t, "Required-Start:") ||
                                   startswith_no_case(t, "Should-Start:") ||
                                   startswith_no_case(t, "X-Start-Before:") ||
                                   startswith_no_case(t, "X-Start-After:")) {
803 804 805 806 807
                                char *i, *w;
                                size_t z;

                                state = LSB;

808
                                FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
809 810 811 812 813 814 815
                                        char *n, *m;

                                        if (!(n = strndup(w, z))) {
                                                r = -ENOMEM;
                                                goto finish;
                                        }

Kay Sievers's avatar
Kay Sievers committed
816
                                        r = sysv_translate_facility(n, path_get_file_name(path), &m);
817

818 819 820 821 822 823 824
                                        if (r < 0) {
                                                log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
                                                free(n);
                                                continue;
                                        }

                                        free(n);
825 826 827 828

                                        if (r == 0)
                                                continue;

829
                                        r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
830 831

                                        if (r < 0)
832
                                                log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
833 834

                                        free(m);
835
                                }
836
                        } else if (startswith_no_case(t, "Default-Start:")) {
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
                                char *k, *d;

                                state = LSB;

                                k = delete_chars(t+14, WHITESPACE "-");

                                if (k[0] != 0) {
                                        if (!(d = strdup(k))) {
                                                r = -ENOMEM;
                                                goto finish;
                                        }

                                        free(s->sysv_runlevels);
                                        s->sysv_runlevels = d;
                                }
852

853 854
                        } else if (startswith_no_case(t, "Description:")) {
                                char *d, *j;
855

856 857
                                state = LSB_DESCRIPTION;

858 859 860 861 862 863 864
                                if ((j = strstrip(t+12)) && *j) {
                                        if (!(d = strdup(j))) {
                                                r = -ENOMEM;
                                                goto finish;
                                        }
                                } else
                                        d = NULL;
865

866 867
                                free(long_description);
                                long_description = d;
868

869
                        } else if (startswith_no_case(t, "Short-Description:")) {
870
                                char *d, *j;
871 872 873

                                state = LSB;

874 875 876 877 878 879 880
                                if ((j = strstrip(t+18)) && *j) {
                                        if (!(d = strdup(j))) {
                                                r = -ENOMEM;
                                                goto finish;
                                        }
                                } else
                                        d = NULL;
881

882 883
                                free(short_description);
                                short_description = d;
884 885 886 887

                        } else if (state == LSB_DESCRIPTION) {

                                if (startswith(l, "#\t") || startswith(l, "#  ")) {
888
                                        char *j;
889

890 891 892 893
                                        if ((j = strstrip(t)) && *j) {
                                                char *d = NULL;

                                                if (long_description)
894
                                                        d = strjoin(long_description, " ", t, NULL);
895 896 897 898 899 900 901 902 903 904
                                                else
                                                        d = strdup(j);

                                                if (!d) {
                                                        r = -ENOMEM;
                                                        goto finish;
                                                }

                                                free(long_description);
                                                long_description = d;
905 906 907 908 909 910 911 912
                                        }

                                } else
                                        state = LSB;
                        }
                }
        }

913
        if ((r = sysv_exec_commands(s, supports_reload)) < 0)
914 915
                goto finish;

916
        if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
917 918 919 920 921 922
                /* If there a runlevels configured for this service
                 * but none of the standard ones, then we assume this
                 * is some special kind of service (which might be
                 * needed for early boot) and don't create any links
                 * to it. */

923
                UNIT(s)->default_dependencies = false;
924 925

                /* Don't timeout special services during boot (like fsck) */
926 927 928 929 930 931 932
                s->timeout_start_usec = 0;
                s->timeout_stop_usec = 0;
        } else {
                s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
                s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
        }

Lennart Poettering's avatar
Lennart Poettering committed
933
        /* Special setting for all SysV services */
934
        s->type = SERVICE_FORKING;
935
        s->remain_after_exit = !s->pid_file;
936
        s->guess_main_pid = false;
937
        s->restart = SERVICE_RESTART_NO;
938
        s->exec_context.ignore_sigpipe = false;
939
        s->kill_context.kill_mode = KILL_PROCESS;
Lennart Poettering's avatar
Lennart Poettering committed
940

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
        /* We use the long description only if
         * no short description is set. */

        if (short_description)
                description = short_description;
        else if (chkconfig_description)
                description = chkconfig_description;
        else if (long_description)
                description = long_description;
        else
                description = NULL;

        if (description) {
                char *d;

956
                if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
957 958 959 960
                        r = -ENOMEM;
                        goto finish;
                }

Michal Schmidt's avatar
Michal Schmidt committed
961
                u->description = d;
962 963
        }

964 965 966 967 968 969
        /* The priority that has been set in /etc/rcN.d/ hierarchies
         * takes precedence over what is stored as default in the LSB
         * header */
        if (s->sysv_start_priority_from_rcnd >= 0)
                s->sysv_start_priority = s->sysv_start_priority_from_rcnd;

Michal Schmidt's avatar
Michal Schmidt committed
970
        u->load_state = UNIT_LOADED;
971
        r = 0;
972 973 974 975 976 977

finish:

        if (f)
                fclose(f);

978 979 980 981
        free(short_description);
        free(long_description);
        free(chkconfig_description);

982 983 984
        return r;
}

985
static int service_load_sysv_name(Service *s, const char *name) {
986 987 988 989 990
        char **p;

        assert(s);
        assert(name);

991
        /* For SysV services we strip the rc.* and *.sh
Lennart Poettering's avatar