- }
-
- r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
- if (r < 0)
- log_warning_unit(UNIT(s)->id,
- "%s failed to install watchdog timer: %s",
- UNIT(s)->id, strerror(-r));
-}
-
-static void service_reset_watchdog(Service *s) {
- assert(s);
-
- dual_timestamp_get(&s->watchdog_timestamp);
- service_handle_watchdog(s);
-}
-
-static void service_done(Unit *u) {
- Service *s = SERVICE(u);
-
- assert(s);
-
- free(s->pid_file);
- s->pid_file = NULL;
-
-#ifdef HAVE_SYSV_COMPAT
- free(s->sysv_runlevels);
- s->sysv_runlevels = NULL;
-#endif
-
- free(s->status_text);
- s->status_text = NULL;
-
- cgroup_context_done(&s->cgroup_context);
- exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
- exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
- s->control_command = NULL;
- s->main_command = NULL;
-
- 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;
-
- /* This will leak a process, but at least no memory or any of
- * our resources */
- service_unwatch_main_pid(s);
- service_unwatch_control_pid(s);
- service_unwatch_pid_file(s);
-
- if (s->bus_name) {
- unit_unwatch_bus_name(u, s->bus_name);
- free(s->bus_name);
- s->bus_name = NULL;
- }
-
- service_close_socket_fd(s);
- service_connection_unref(s);
-
- unit_ref_unset(&s->accept_socket);
-
- service_stop_watchdog(s);
-
- unit_unwatch_timer(u, &s->timer_watch);
-}
-
-#ifdef HAVE_SYSV_COMPAT
-static char *sysv_translate_name(const char *name) {
- char *r;
-
- r = new(char, strlen(name) + sizeof(".service"));
- if (!r)
- return NULL;
-
- if (endswith(name, ".sh"))
- /* Drop .sh suffix */
- strcpy(stpcpy(r, name) - 3, ".service");
- else
- /* Normal init script name */
- strcpy(stpcpy(r, name), ".service");
-
- return r;
-}
-
-static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
-
- /* 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. */
-
- static const char * const table[] = {
- /* LSB defined facilities */
- "local_fs", NULL,
- "network", SPECIAL_NETWORK_TARGET,
- "named", SPECIAL_NSS_LOOKUP_TARGET,
- "portmap", SPECIAL_RPCBIND_TARGET,
- "remote_fs", SPECIAL_REMOTE_FS_TARGET,
- "syslog", NULL,
- "time", SPECIAL_TIME_SYNC_TARGET,
- };
-
- unsigned i;
- char *r;
- const char *n;
-
- assert(name);
- assert(_r);
-
- n = *name == '$' ? name + 1 : name;
-
- for (i = 0; i < ELEMENTSOF(table); i += 2) {
-
- if (!streq(table[i], n))
- continue;
-
- if (!table[i+1])
- return 0;
-
- r = strdup(table[i+1]);
- if (!r)
- return log_oom();
-
- goto finish;
- }
-
- /* If we don't know this name, fallback heuristics to figure
- * out whether something is a target or a service alias. */
-
- if (*name == '$') {
- if (!unit_prefix_is_valid(n))
- return -EINVAL;
-
- /* Facilities starting with $ are most likely targets */
- r = unit_name_build(n, NULL, ".target");
- } else if (filename && streq(name, filename))
- /* Names equaling the file name of the services are redundant */
- return 0;
- else
- /* Everything else we assume to be normal service names */
- r = sysv_translate_name(n);
-
- if (!r)
- return -ENOMEM;
-
-finish:
- *_r = r;
-
- return 1;
-}
-
-static int sysv_fix_order(Service *s) {
- Unit *other;
- int r;
-
- assert(s);
-
- if (s->sysv_start_priority < 0)
- return 0;
-
- /* For each pair of services where at least one lacks a LSB
- * header, we use the start priority value to order things. */
-
- LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
- Service *t;
- UnitDependency d;
- bool special_s, special_t;
-
- t = SERVICE(other);
-
- if (s == t)
- continue;
-
- if (UNIT(t)->load_state != UNIT_LOADED)
- continue;
-
- if (t->sysv_start_priority < 0)
- continue;
-
- /* If both units have modern headers we don't care
- * about the priorities */
- if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
- (UNIT(t)->fragment_path || t->sysv_has_lsb))
- continue;
-
- 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)
- 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? */
-
- if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
- 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;
-}
-
-static int sysv_exec_commands(Service *s, const bool supports_reload) {
- ExecCommand *c;
-
- assert(s);
- assert(s->is_sysv);
- assert(UNIT(s)->source_path);
-
- c = exec_command_new(UNIT(s)->source_path, "start");
- if (!c)
- return -ENOMEM;
- exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
-
- c = exec_command_new(UNIT(s)->source_path, "stop");
- if (!c)
- return -ENOMEM;
- exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
-
- 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);
- }
-
- return 0;
-}
-
-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\""));
-}
-
-static int service_load_sysv_path(Service *s, const char *path) {
- FILE *f;
- Unit *u;
- unsigned line = 0;
- int r;
- enum {
- NORMAL,
- DESCRIPTION,
- LSB,
- LSB_DESCRIPTION,
- USAGE_CONTINUATION
- } state = NORMAL;
- char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
- struct stat st;
- bool supports_reload = false;
-
- assert(s);
- assert(path);
-
- u = UNIT(s);