- assert(path);
-
- u = UNIT(s);
-
- f = fopen(path, "re");
- if (!f) {
- r = errno == ENOENT ? 0 : -errno;
- goto finish;
- }
-
- if (fstat(fileno(f), &st) < 0) {
- r = -errno;
- goto finish;
- }
-
- free(u->source_path);
- u->source_path = strdup(path);
- if (!u->source_path) {
- r = -ENOMEM;
- goto finish;
- }
- u->source_mtime = timespec_load(&st.st_mtim);
-
- if (null_or_empty(&st)) {
- u->load_state = UNIT_MASKED;
- r = 0;
- goto finish;
- }
-
- s->is_sysv = true;
-
- while (!feof(f)) {
- char l[LINE_MAX], *t;
-
- if (!fgets(l, sizeof(l), f)) {
- if (feof(f))
- break;
-
- r = -errno;
- log_error_unit(u->id,
- "Failed to read configuration file '%s': %s",
- path, strerror(-r));
- goto finish;
- }
-
- line++;
-
- t = strstrip(l);
- 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;
- }
-
- continue;
- }
-
- if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
- state = LSB;
- s->sysv_has_lsb = true;
- 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 */
-
- if (startswith_no_case(t, "chkconfig:")) {
- int start_priority;
- char runlevels[16], *k;
-
- state = NORMAL;
-
- if (sscanf(t+10, "%15s %i %*i",
- runlevels,
- &start_priority) != 2) {
-
- log_warning_unit(u->id,
- "[%s:%u] Failed to parse chkconfig line. Ignoring.",
- path, line);
- continue;
- }
-
- /* A start priority gathered from the
- * symlink farms is preferred over the
- * data from the LSB header. */
- if (start_priority < 0 || start_priority > 99)
- log_warning_unit(u->id,
- "[%s:%u] Start priority out of range. Ignoring.",
- path, line);
- else
- 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;
- }
-
- } else if (startswith_no_case(t, "description:")) {
-
- size_t k = strlen(t);
- char *d;
- const char *j;
-
- if (t[k-1] == '\\') {
- state = DESCRIPTION;
- t[k-1] = 0;
- }
-
- if ((j = strstrip(t+12)) && *j) {
- if (!(d = strdup(j))) {
- r = -ENOMEM;
- goto finish;
- }
- } else
- d = NULL;
-
- free(chkconfig_description);
- chkconfig_description = d;
-
- } else if (startswith_no_case(t, "pidfile:")) {
-
- char *fn;
-
- state = NORMAL;
-
- fn = strstrip(t+8);
- if (!path_is_absolute(fn)) {
- log_warning_unit(u->id,
- "[%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);
- char *j;
-
- if (t[k-1] == '\\')
- t[k-1] = 0;
- else
- state = NORMAL;
-
- if ((j = strstrip(t)) && *j) {
- char *d = NULL;
-
- if (chkconfig_description)
- d = strjoin(chkconfig_description, " ", j, NULL);
- else
- d = strdup(j);
-
- if (!d) {
- r = -ENOMEM;
- goto finish;
- }
-
- free(chkconfig_description);
- chkconfig_description = d;
- }
-
- } else if (state == LSB || state == LSB_DESCRIPTION) {
-
- if (startswith_no_case(t, "Provides:")) {
- char *i, *w;
- size_t z;
-
- state = LSB;
-
- FOREACH_WORD_QUOTED(w, z, t+9, i) {
- char *n, *m;
-
- if (!(n = strndup(w, z))) {
- r = -ENOMEM;
- goto finish;
- }
-
- r = sysv_translate_facility(n, path_get_file_name(path), &m);
- free(n);
-
- if (r < 0)
- goto finish;
-
- if (r == 0)
- continue;
-
- if (unit_name_to_type(m) == UNIT_SERVICE)
- r = unit_merge_by_name(u, m);
- 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);
-
- if (r < 0)
- log_error_unit(u->id,
- "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
- path, line, m, strerror(-r));
-
- free(m);
- }
-
- } 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:")) {
- char *i, *w;
- size_t z;
-
- state = LSB;
-
- FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
- char *n, *m;
-
- if (!(n = strndup(w, z))) {
- r = -ENOMEM;
- goto finish;
- }