service.c revision 0d624a785aed0423ee87f70d33de97df9662844b
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <signal.h>
#include <dirent.h>
#include <unistd.h>
#include "unit.h"
#include "service.h"
#include "load-fragment.h"
#include "load-dropin.h"
#include "log.h"
#include "strv.h"
#include "unit-name.h"
#include "dbus-service.h"
#include "special.h"
#define COMMENTS "#;\n"
#define NEWLINES "\n\r"
#define LINE_MAX 4096
typedef enum RunlevelType {
} RunlevelType;
static const struct {
const char *path;
const char *target;
const RunlevelType type;
} rcnd_table[] = {
/* Standard SysV runlevels */
/* SUSE style boot.d */
/* Debian style rcS.d */
};
#define RUNLEVELS_UP "12345"
/* #define RUNLEVELS_DOWN "06" */
/* #define RUNLEVELS_BOOT "bBsS" */
};
static void service_init(Unit *u) {
assert(u);
s->sysv_start_priority = -1;
s->socket_fd = -1;
}
static void service_unwatch_control_pid(Service *s) {
assert(s);
if (s->control_pid <= 0)
return;
s->control_pid = 0;
}
static void service_unwatch_main_pid(Service *s) {
assert(s);
if (s->main_pid <= 0)
return;
s->main_pid = 0;
}
assert(s);
if (pid <= 1)
return -EINVAL;
return -EINVAL;
log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
s->main_pid_known = true;
return 0;
}
static void service_close_socket_fd(Service *s) {
assert(s);
if (s->socket_fd < 0)
return;
s->socket_fd = -1;
}
static void service_connection_unref(Service *s) {
assert(s);
if (!s->socket)
return;
}
static void service_done(Unit *u) {
assert(s);
free(s->sysv_runlevels);
s->sysv_runlevels = NULL;
free(s->status_text);
s->status_text = NULL;
s->control_command = NULL;
/* This will leak a process, but at least no memory or any of
* our resources */
if (s->bus_name) {
}
unit_unwatch_timer(u, &s->timer_watch);
}
static char *sysv_translate_name(const char *name) {
char *r;
return NULL;
/* Drop SuSE-style boot. prefix */
/* Drop Debian-style .sh suffix */
else
/* Normal init scripts */
return r;
}
static const char * const table[] = {
/* LSB defined facilities */
"$local_fs", SPECIAL_LOCAL_FS_TARGET,
"$network", SPECIAL_NETWORK_TARGET,
"$named", SPECIAL_NSS_LOOKUP_TARGET,
"$portmap", SPECIAL_RPCBIND_TARGET,
"$remote_fs", SPECIAL_REMOTE_FS_TARGET,
"$syslog", SPECIAL_SYSLOG_TARGET,
"$time", SPECIAL_RTC_SET_TARGET,
/* Debian extensions */
"$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
"$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
"$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
};
unsigned i;
char *r;
return -ENOMEM;
goto finish;
}
if (*name == '$')
return 0;
if (!(r = sysv_translate_name(name)))
return -ENOMEM;
if (_r)
*_r = r;
return 1;
}
static int sysv_fix_order(Service *s) {
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. */
Service *t;
if (s == t)
continue;
if (t->sysv_start_priority < 0)
continue;
/* If both units have modern headers we don't care
* about the priorities */
if ((!s->sysv_path || s->sysv_has_lsb) &&
(!t->sysv_path || t->sysv_has_lsb))
continue;
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? */
return r;
}
return 0;
}
ExecCommand *c;
return NULL;
free(c);
return NULL;
}
free(c);
return NULL;
}
return c;
}
static int sysv_exec_commands(Service *s) {
ExecCommand *c;
assert(s);
return -ENOMEM;
return -ENOMEM;
return -ENOMEM;
return 0;
}
FILE *f;
Unit *u;
unsigned line = 0;
int r;
enum {
LSB,
assert(s);
u = UNIT(s);
goto finish;
}
r = -ENOMEM;
goto finish;
}
while (!feof(f)) {
char l[LINE_MAX], *t;
if (!fgets(l, sizeof(l), f)) {
if (feof(f))
break;
r = -errno;
goto finish;
}
line++;
t = strstrip(l);
if (*t != '#')
continue;
s->sysv_has_lsb = true;
continue;
}
continue;
}
t++;
t += strspn(t, WHITESPACE);
/* Try to parse Red Hat style chkconfig headers */
if (startswith_no_case(t, "chkconfig:")) {
int start_priority;
char runlevels[16], *k;
&start_priority) != 2) {
continue;
}
/* A start priority gathered from the
* symlink farms is preferred over the
* data from the LSB header. */
else if (s->sysv_start_priority < 0)
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:")) {
char *d;
if (t[k-1] == '\\') {
state = DESCRIPTION;
t[k-1] = 0;
}
r = -ENOMEM;
goto finish;
}
u->meta.description = d;
} else if (startswith_no_case(t, "pidfile:")) {
char *fn;
if (!path_is_absolute(fn)) {
continue;
}
r = -ENOMEM;
goto finish;
}
}
} else if (state == DESCRIPTION) {
/* Try to parse Red Hat style description
* continuation */
char *d;
if (t[k-1] == '\\')
t[k-1] = 0;
else
r = -ENOMEM;
goto finish;
}
u->meta.description = d;
if (startswith_no_case(t, "Provides:")) {
char *i, *w;
size_t z;
FOREACH_WORD(w, z, t+9, i) {
char *n, *m;
if (!(n = strndup(w, z))) {
r = -ENOMEM;
goto finish;
}
r = sysv_translate_facility(n, &m);
free(n);
if (r < 0)
goto finish;
if (r == 0)
continue;
if (unit_name_to_type(m) == UNIT_SERVICE)
r = unit_add_name(u, m);
else {
}
free(m);
if (r < 0)
goto finish;
}
} 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;
char *n, *m;
if (!(n = strndup(w, z))) {
r = -ENOMEM;
goto finish;
}
r = sysv_translate_facility(n, &m);
free(n);
if (r < 0)
goto finish;
if (r == 0)
continue;
r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
free(m);
if (r < 0)
goto finish;
}
} else if (startswith_no_case(t, "Default-Start:")) {
char *k, *d;
if (k[0] != 0) {
if (!(d = strdup(k))) {
r = -ENOMEM;
goto finish;
}
free(s->sysv_runlevels);
s->sysv_runlevels = d;
}
} else if (startswith_no_case(t, "Description:")) {
char *d;
r = -ENOMEM;
goto finish;
}
u->meta.description = d;
} else if (startswith_no_case(t, "Short-Description:") &&
!u->meta.description) {
char *d;
/* We use the short description only
* if no long description is set. */
r = -ENOMEM;
goto finish;
}
u->meta.description = d;
} else if (startswith_no_case(t, "X-Interactive:")) {
int b;
continue;
}
if (b)
else
} else if (state == LSB_DESCRIPTION) {
char *d;
r = -ENOMEM;
goto finish;
}
u->meta.description = d;
} else
}
}
}
if ((r = sysv_exec_commands(s)) < 0)
goto finish;
/* 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. */
goto finish;
} else
/* Don't timeout special services during boot (like fsck) */
s->timeout_usec = 0;
/* Special setting for all SysV services */
s->type = SERVICE_FORKING;
s->valid_no_process = true;
s->kill_mode = KILL_PROCESS_GROUP;
s->restart = SERVICE_ONCE;
r = 0;
if (f)
fclose(f);
return r;
}
char **p;
assert(s);
/* For SysV services we strip the boot. or .sh
return -ENOENT;
char *path;
int r;
return -ENOMEM;
r = service_load_sysv_path(s, path);
/* Try Debian style xxx.sh source'able init scripts */
r = service_load_sysv_path(s, path);
}
/* Try SUSE style boot.xxx init scripts */
return -ENOMEM;
r = service_load_sysv_path(s, path);
}
if (r < 0)
return r;
break;
}
return 0;
}
static int service_load_sysv(Service *s) {
const char *t;
Iterator i;
int r;
assert(s);
/* Load service data from SysV init scripts, preferably with
* LSB headers ... */
return 0;
if ((r = service_load_sysv_name(s, t)) < 0)
return r;
continue;
if ((r == service_load_sysv_name(s, t)) < 0)
return r;
break;
}
return 0;
}
static int service_add_bus_name(Service *s) {
char *n;
int r;
assert(s);
return 0;
r = unit_merge_by_name(UNIT(s), n);
free(n);
return r;
}
static int service_verify(Service *s) {
assert(s);
return 0;
if (!s->exec_command[SERVICE_EXEC_START]) {
return -EINVAL;
}
return -EINVAL;
}
log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
return -EINVAL;
}
return -EINVAL;
}
return 0;
}
static int service_load(Unit *u) {
int r;
assert(s);
/* Load a .service file */
if ((r = unit_load_fragment(u)) < 0)
return r;
/* Load a classic init script as a fallback, if we couldn't find anything */
if ((r = service_load_sysv(s)) < 0)
return r;
/* Still nothing found? Then let's give up */
return -ENOENT;
/* We were able to load something, then let's add in the
* dropin directories. */
if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
return r;
/* This is a new unit? Then let's add in some extras */
if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
return r;
if ((r = unit_add_default_cgroup(u)) < 0)
return r;
if ((r = sysv_fix_order(s)) < 0)
return r;
if (s->bus_name) {
if ((r = service_add_bus_name(s)) < 0)
return r;
if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
return r;
}
s->notify_access = NOTIFY_MAIN;
}
return service_verify(s);
}
const char *prefix2;
char *p2;
assert(s);
fprintf(f,
"%sService State: %s\n"
"%sPermissionsStartOnly: %s\n"
"%sRootDirectoryStartOnly: %s\n"
"%sValidNoProcess: %s\n"
"%sKillMode: %s\n"
"%sType: %s\n"
"%sNotifyAccess: %s\n",
if (s->control_pid > 0)
fprintf(f,
"%sControl PID: %lu\n",
prefix, (unsigned long) s->control_pid);
if (s->main_pid > 0)
fprintf(f,
"%sMain PID: %lu\n",
if (s->pid_file)
fprintf(f,
"%sPIDFile: %s\n",
if (s->bus_name)
fprintf(f,
"%sBusName: %s\n"
"%sBus Name Good: %s\n",
for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
if (!s->exec_command[c])
continue;
fprintf(f, "%s-> %s:\n",
}
if (s->sysv_path)
fprintf(f,
"%sSysV Init Script Path: %s\n"
"%sSysV Init Script has LSB Header: %s\n",
if (s->sysv_start_priority >= 0)
fprintf(f,
"%sSysVStartPriority: %i\n",
prefix, s->sysv_start_priority);
if (s->sysv_runlevels)
fprintf(f, "%sSysVRunLevels: %s\n",
prefix, s->sysv_runlevels);
if (s->status_text)
fprintf(f, "%sStatus Text: %s\n",
prefix, s->status_text);
}
static int service_load_pid_file(Service *s) {
char *k;
int r;
assert(s);
if (s->main_pid_known)
return 0;
if (!s->pid_file)
return -ENOENT;
if ((r = read_one_line_file(s->pid_file, &k)) < 0)
return r;
free(k);
if (r < 0)
return r;
log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
return -ESRCH;
}
if ((r = service_set_main_pid(s, pid)) < 0)
return r;
/* FIXME: we need to do something here */
return r;
return 0;
}
Iterator i;
char *t;
int r;
assert(s);
if (s->socket_fd >= 0)
return 0;
/* Collects all Socket objects that belong to this
* service. Note that a service might have multiple sockets
* via multiple names. */
return -ENOMEM;
char *k;
Unit *p;
/* Look for all socket objects that go by any of our
* units and collect their fds */
if (!(k = unit_name_change_suffix(t, ".socket"))) {
r = -ENOMEM;
goto fail;
}
free(k);
if (!p)
continue;
goto fail;
}
return 0;
fail:
return r;
}
static int service_notify_sockets_dead(Service *s) {
Iterator i;
int r;
assert(s);
if (s->socket_fd >= 0)
return 0;
/* Notifies all our sockets when we die */
if ((r = service_get_sockets(s, &set)) < 0)
return r;
return 0;
}
assert(s);
if (state != SERVICE_START_PRE &&
state != SERVICE_START &&
state != SERVICE_START_POST &&
state != SERVICE_RELOAD &&
state != SERVICE_STOP &&
state != SERVICE_STOP_SIGTERM &&
state != SERVICE_STOP_SIGKILL &&
state != SERVICE_STOP_POST &&
state != SERVICE_FINAL_SIGTERM &&
state != SERVICE_FINAL_SIGKILL &&
if (state != SERVICE_START &&
state != SERVICE_START_POST &&
state != SERVICE_RUNNING &&
state != SERVICE_RELOAD &&
state != SERVICE_STOP &&
state != SERVICE_STOP_SIGTERM &&
if (state != SERVICE_START_PRE &&
state != SERVICE_START &&
state != SERVICE_START_POST &&
state != SERVICE_RELOAD &&
state != SERVICE_STOP &&
state != SERVICE_STOP_SIGTERM &&
state != SERVICE_STOP_SIGKILL &&
state != SERVICE_STOP_POST &&
state != SERVICE_FINAL_SIGTERM &&
state != SERVICE_FINAL_SIGKILL) {
s->control_command = NULL;
}
if (state == SERVICE_DEAD ||
state == SERVICE_STOP ||
state == SERVICE_STOP_SIGTERM ||
state == SERVICE_STOP_SIGKILL ||
state == SERVICE_STOP_POST ||
state == SERVICE_FINAL_SIGTERM ||
state == SERVICE_FINAL_SIGKILL ||
state == SERVICE_MAINTENANCE ||
if (state != SERVICE_START_PRE &&
state != SERVICE_START &&
state != SERVICE_START_POST &&
state != SERVICE_RUNNING &&
state != SERVICE_RELOAD &&
state != SERVICE_STOP &&
state != SERVICE_STOP_SIGTERM &&
state != SERVICE_STOP_SIGKILL &&
state != SERVICE_STOP_POST &&
state != SERVICE_FINAL_SIGTERM &&
state != SERVICE_FINAL_SIGKILL &&
}
log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
}
static int service_coldplug(Unit *u) {
int r;
assert(s);
if (s->deserialized_state != s->state) {
if (s->deserialized_state == SERVICE_START_PRE ||
s->deserialized_state == SERVICE_START ||
s->deserialized_state == SERVICE_START_POST ||
s->deserialized_state == SERVICE_RELOAD ||
s->deserialized_state == SERVICE_STOP ||
s->deserialized_state == SERVICE_STOP_SIGTERM ||
s->deserialized_state == SERVICE_STOP_SIGKILL ||
s->deserialized_state == SERVICE_STOP_POST ||
s->deserialized_state == SERVICE_FINAL_SIGTERM ||
s->deserialized_state == SERVICE_FINAL_SIGKILL ||
s->deserialized_state == SERVICE_AUTO_RESTART) {
usec_t k;
return r;
}
}
if ((s->deserialized_state == SERVICE_START &&
(s->type == SERVICE_FORKING ||
s->type == SERVICE_DBUS ||
s->type == SERVICE_FINISH ||
s->type == SERVICE_NOTIFY)) ||
s->deserialized_state == SERVICE_START_POST ||
s->deserialized_state == SERVICE_RUNNING ||
s->deserialized_state == SERVICE_RELOAD ||
s->deserialized_state == SERVICE_STOP ||
s->deserialized_state == SERVICE_STOP_SIGTERM ||
if (s->main_pid > 0)
return r;
if (s->deserialized_state == SERVICE_START_PRE ||
s->deserialized_state == SERVICE_START ||
s->deserialized_state == SERVICE_START_POST ||
s->deserialized_state == SERVICE_RELOAD ||
s->deserialized_state == SERVICE_STOP ||
s->deserialized_state == SERVICE_STOP_SIGTERM ||
s->deserialized_state == SERVICE_STOP_SIGKILL ||
s->deserialized_state == SERVICE_STOP_POST ||
s->deserialized_state == SERVICE_FINAL_SIGTERM ||
if (s->control_pid > 0)
return r;
service_set_state(s, s->deserialized_state);
}
return 0;
}
Iterator i;
int r;
unsigned rn_fds = 0;
assert(s);
if (s->socket_fd >= 0)
return 0;
if ((r = service_get_sockets(s, &set)) < 0)
return r;
int *cfds;
unsigned cn_fds;
goto fail;
if (!cfds)
continue;
if (!rfds) {
} else {
int *t;
r = -ENOMEM;
goto fail;
}
rfds = t;
}
}
return 0;
fail:
return r;
}
static int service_spawn(
Service *s,
ExecCommand *c,
bool timeout,
bool pass_fds,
bool apply_permissions,
bool apply_chroot,
bool set_notify_socket,
int r;
unsigned n_fds = 0;
assert(s);
assert(c);
if (pass_fds ||
if (s->socket_fd >= 0) {
n_fds = 1;
} else {
goto fail;
}
}
if (timeout && s->timeout_usec) {
goto fail;
} else
r = -ENOMEM;
goto fail;
}
if (set_notify_socket) {
char *t;
r = -ENOMEM;
goto fail;
}
free(t);
if (!env) {
r = -ENOMEM;
goto fail;
}
} else
r = exec_spawn(c,
argv,
&s->exec_context,
env,
s->meta.cgroup_bondings,
&pid);
if (set_notify_socket)
if (r < 0)
goto fail;
if (fdsbuf)
/* FIXME: we need to do something here */
goto fail;
return 0;
fail:
if (set_notify_socket)
if (timeout)
return r;
}
static int main_pid_good(Service *s) {
assert(s);
/* Returns 0 if the pid is dead, 1 if it is good, -1 if we
* don't know */
/* If we know the pid file, then lets just check if it is
* still valid */
if (s->main_pid_known)
return s->main_pid > 0;
/* We don't know the pid */
return -EAGAIN;
}
static int control_pid_good(Service *s) {
assert(s);
return s->control_pid > 0;
}
static int cgroup_good(Service *s) {
int r;
assert(s);
return r;
return !r;
}
int r;
assert(s);
if (!success)
s->failure = true;
if (allow_restart &&
s->allow_restart &&
(s->restart == SERVICE_RESTART_ALWAYS ||
goto fail;
} else
return;
fail:
service_enter_dead(s, false, false);
}
int r;
assert(s);
if (!success)
s->failure = true;
if ((r = service_spawn(s,
s->control_command,
true,
false,
false,
&s->control_pid)) < 0)
goto fail;
} else
service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
return;
fail:
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
}
int r;
bool sent = false;
assert(s);
if (!success)
s->failure = true;
if (s->kill_mode == KILL_CONTROL_GROUP) {
goto fail;
} else
sent = true;
}
if (!sent) {
r = 0;
if (s->main_pid > 0) {
r = -errno;
else
sent = true;
}
if (s->control_pid > 0) {
if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
r = -errno;
else
sent = true;
}
if (r < 0)
goto fail;
}
}
if (s->timeout_usec > 0)
goto fail;
service_set_state(s, state);
service_enter_stop_post(s, true);
else
service_enter_dead(s, true, true);
return;
fail:
service_enter_stop_post(s, false);
else
service_enter_dead(s, false, true);
}
int r;
assert(s);
if (!success)
s->failure = true;
if ((r = service_spawn(s,
s->control_command,
true,
false,
false,
&s->control_pid)) < 0)
goto fail;
} else
service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
return;
fail:
service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
}
int main_pid_ok, cgroup_ok;
assert(s);
if (!success)
s->failure = true;
main_pid_ok = main_pid_good(s);
cgroup_ok = cgroup_good(s);
else if (s->valid_no_process)
else
service_enter_stop(s, true);
}
static void service_enter_start_post(Service *s) {
int r;
assert(s);
if ((r = service_spawn(s,
s->control_command,
true,
false,
false,
&s->control_pid)) < 0)
goto fail;
} else
service_enter_running(s, true);
return;
fail:
service_enter_stop(s, false);
}
static void service_enter_start(Service *s) {
int r;
assert(s);
if (s->type == SERVICE_FORKING)
else
if ((r = service_spawn(s,
true,
true,
true,
s->notify_access != NOTIFY_NONE,
&pid)) < 0)
goto fail;
if (s->type == SERVICE_SIMPLE) {
/* For simple services we immediately start
* the START_POST binaries. */
service_set_main_pid(s, pid);
} else if (s->type == SERVICE_FORKING) {
/* For forking services we wait until the start
* process exited. */
s->control_pid = pid;
} else if (s->type == SERVICE_FINISH ||
s->type == SERVICE_DBUS ||
s->type == SERVICE_NOTIFY) {
/* For finishing services we wait until the start
* process exited, too, but it is our main process. */
/* For D-Bus services we know the main pid right away,
* but wait for the bus name to appear on the
* bus. Notify services are similar. */
service_set_main_pid(s, pid);
} else
assert_not_reached("Unknown service type");
return;
fail:
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
}
static void service_enter_start_pre(Service *s) {
int r;
assert(s);
if ((r = service_spawn(s,
s->control_command,
true,
false,
false,
&s->control_pid)) < 0)
goto fail;
} else
return;
fail:
service_enter_dead(s, false, true);
}
static void service_enter_restart(Service *s) {
int r;
assert(s);
service_enter_dead(s, true, false);
goto fail;
return;
fail:
service_enter_dead(s, false, false);
}
static void service_enter_reload(Service *s) {
int r;
assert(s);
if ((r = service_spawn(s,
s->control_command,
true,
false,
false,
&s->control_pid)) < 0)
goto fail;
} else
service_enter_running(s, true);
return;
fail:
service_enter_stop(s, false);
}
int r;
assert(s);
assert(s->control_command);
if (!success)
s->failure = true;
if ((r = service_spawn(s,
s->control_command,
true,
false,
false,
&s->control_pid)) < 0)
goto fail;
return;
fail:
if (s->state == SERVICE_START_PRE)
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
else if (s->state == SERVICE_STOP)
service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
else if (s->state == SERVICE_STOP_POST)
service_enter_dead(s, false, true);
else
service_enter_stop(s, false);
}
static int service_start(Unit *u) {
assert(s);
/* We cannot fulfill this request right now, try again later
* please! */
if (s->state == SERVICE_STOP ||
s->state == SERVICE_STOP_SIGTERM ||
s->state == SERVICE_STOP_SIGKILL ||
s->state == SERVICE_STOP_POST ||
s->state == SERVICE_FINAL_SIGTERM ||
s->state == SERVICE_FINAL_SIGKILL)
return -EAGAIN;
/* Already on it! */
if (s->state == SERVICE_START_PRE ||
s->state == SERVICE_START ||
s->state == SERVICE_START_POST)
return 0;
assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
/* Make sure we don't enter a busy loop of some kind. */
if (!ratelimit_test(&s->ratelimit)) {
return -ECANCELED;
}
s->failure = false;
s->main_pid_known = false;
s->allow_restart = true;
return 0;
}
static int service_stop(Unit *u) {
assert(s);
/* Cannot do this now */
if (s->state == SERVICE_START_PRE ||
s->state == SERVICE_START ||
s->state == SERVICE_START_POST ||
s->state == SERVICE_RELOAD)
return -EAGAIN;
/* Already on it */
if (s->state == SERVICE_STOP ||
s->state == SERVICE_STOP_SIGTERM ||
s->state == SERVICE_STOP_SIGKILL ||
s->state == SERVICE_STOP_POST ||
s->state == SERVICE_FINAL_SIGTERM ||
s->state == SERVICE_FINAL_SIGKILL)
return 0;
if (s->state == SERVICE_AUTO_RESTART) {
return 0;
}
/* This is a user request, so don't do restarts on this
* shutdown. */
s->allow_restart = false;
service_enter_stop(s, true);
return 0;
}
static int service_reload(Unit *u) {
assert(s);
return 0;
}
static bool service_can_reload(Unit *u) {
assert(s);
return !!s->exec_command[SERVICE_EXEC_RELOAD];
}
assert(u);
assert(f);
if (s->control_pid > 0)
if (s->main_pid_known && s->main_pid > 0)
/* There's a minor uncleanliness here: if there are multiple
* commands attached here, we will start from the first one
* again */
if (s->control_command_id >= 0)
unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
if (s->socket_fd >= 0) {
int copy;
return copy;
}
return 0;
}
int r;
assert(u);
else
s->deserialized_state = state;
int b;
if ((b = parse_boolean(value)) < 0)
else
else
s->control_pid = pid;
else
int b;
if ((b = parse_boolean(value)) < 0)
else
s->main_pid_known = b;
else {
s->control_command_id = id;
}
int fd;
else {
if (s->socket_fd >= 0)
}
} else
return 0;
}
assert(u);
}
static const char *service_sub_state_to_string(Unit *u) {
assert(u);
}
static bool service_check_gc(Unit *u) {
assert(s);
return !!s->sysv_path;
}
static bool service_check_snapshot(Unit *u) {
assert(s);
return !s->got_socket_fd;
}
bool success;
assert(s);
s->main_pid = 0;
if (s->type != SERVICE_FORKING) {
}
log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
/* The service exited, so the service is officially
* gone. */
switch (s->state) {
case SERVICE_START_POST:
case SERVICE_RELOAD:
case SERVICE_STOP:
/* Need to wait until the operation is
* done */
break;
case SERVICE_START:
if (s->type == SERVICE_FINISH) {
/* This was our main goal, so let's go on */
if (success)
else
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
break;
} else {
/* Fall through */
}
case SERVICE_RUNNING:
break;
case SERVICE_STOP_SIGTERM:
case SERVICE_STOP_SIGKILL:
if (!control_pid_good(s))
/* If there is still a control process, wait for that first */
break;
default:
assert_not_reached("Uh, main process died at wrong time.");
}
} else if (s->control_pid == pid) {
if (s->control_command)
s->control_pid = 0;
log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
/* If we are shutting things down anyway we
* don't care about failing commands. */
/* There is another command to *
* execute, so let's do that. */
service_run_next(s, success);
} else {
/* No further commands for this step, so let's
* figure out what to do next */
s->control_command = NULL;
switch (s->state) {
case SERVICE_START_PRE:
if (success)
else
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
break;
case SERVICE_START:
/* Let's try to load the pid
* file here if we can. We
* ignore the return value,
* since the PID file might
* actually be created by a
* START_POST script */
if (success) {
if (s->pid_file)
} else
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
break;
case SERVICE_START_POST:
int r;
/* Hmm, let's see if we can
* load the pid now after the
* start-post scripts got
* executed. */
if ((r = service_load_pid_file(s)) < 0)
}
/* Fall through */
case SERVICE_RELOAD:
if (success)
service_enter_running(s, true);
else
service_enter_stop(s, false);
break;
case SERVICE_STOP:
break;
case SERVICE_STOP_SIGTERM:
case SERVICE_STOP_SIGKILL:
if (main_pid_good(s) <= 0)
/* If there is still a service
* process around, wait until
* that one quit, too */
break;
case SERVICE_STOP_POST:
case SERVICE_FINAL_SIGTERM:
case SERVICE_FINAL_SIGKILL:
service_enter_dead(s, success, true);
break;
default:
assert_not_reached("Uh, control process died at wrong time.");
}
}
}
}
assert(s);
assert(w == &s->timer_watch);
switch (s->state) {
case SERVICE_START_PRE:
case SERVICE_START:
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
break;
case SERVICE_START_POST:
case SERVICE_RELOAD:
service_enter_stop(s, false);
break;
case SERVICE_STOP:
service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
break;
case SERVICE_STOP_SIGTERM:
service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
break;
case SERVICE_STOP_SIGKILL:
/* Uh, wie sent a SIGKILL and it is still not gone?
* Must be something we cannot kill, so let's just be
* weirded out and continue */
service_enter_stop_post(s, false);
break;
case SERVICE_STOP_POST:
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
break;
case SERVICE_FINAL_SIGTERM:
service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
break;
case SERVICE_FINAL_SIGKILL:
service_enter_dead(s, false, true);
break;
case SERVICE_AUTO_RESTART:
break;
default:
assert_not_reached("Timeout at wrong time.");
}
}
static void service_cgroup_notify_event(Unit *u) {
assert(u);
switch (s->state) {
/* Waiting for SIGCHLD is usually more interesting,
* why we ignore the cgroup events for most cases,
* except when we don't know pid which to expect the
* SIGCHLD for. */
case SERVICE_RUNNING:
service_enter_running(s, true);
break;
default:
;
}
}
const char *e;
assert(u);
if (s->notify_access == NOTIFY_NONE) {
log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
return;
}
log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
return;
}
/* Interpret MAINPID= */
(s->state == SERVICE_START ||
s->state == SERVICE_START_POST ||
s->state == SERVICE_RUNNING ||
s->state == SERVICE_RELOAD)) {
log_warning("Failed to parse %s", e);
else {
service_set_main_pid(s, pid);
}
}
/* Interpret READY= */
if (s->type == SERVICE_NOTIFY &&
s->state == SERVICE_START &&
}
/* Interpret STATUS= */
char *t;
if (!(t = strdup(e+7))) {
log_error("Failed to allocate string.");
return;
}
free(s->status_text);
s->status_text = t;
}
}
static int service_enumerate(Manager *m) {
char **p;
unsigned i;
int r;
assert(m);
for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
r = -ENOMEM;
goto finish;
}
if (d)
closedir(d);
continue;
}
int a, b;
continue;
continue;
continue;
if (a < 0 || b < 0)
continue;
r = -ENOMEM;
goto finish;
}
continue;
}
r = -ENOMEM;
goto finish;
}
continue;
}
goto finish;
goto finish;
goto finish;
/* We honour K links only for
* runlevels we assume the
* stop jobs will be
* implicitly added by the
* core logic. Also, we don't
* really distuingish here
* between the runlevels 0 and
* 6 and just add them to the
* special shutdown target. On
* SUSE the boot.d/ runlevel
* is also used for shutdown,
* so we add links for that
* too to the shutdown
* target.*/
goto finish;
goto finish;
goto finish;
}
}
}
r = 0;
if (d)
closedir(d);
return r;
}
static void service_bus_name_owner_change(
Unit *u,
const char *name,
const char *old_owner,
const char *new_owner) {
assert(s);
log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
else if (old_owner)
else
s->bus_name_good = !!new_owner;
if (s->type == SERVICE_DBUS) {
/* service_enter_running() will figure out what to
* do */
if (s->state == SERVICE_RUNNING)
service_enter_running(s, true);
} else if (new_owner &&
s->main_pid <= 0 &&
(s->state == SERVICE_START ||
s->state == SERVICE_START_POST ||
s->state == SERVICE_RUNNING ||
s->state == SERVICE_RELOAD)) {
/* Try to acquire PID from bus service */
log_debug("Trying to acquire PID from D-Bus name...");
}
}
static void service_bus_query_pid_done(
Unit *u,
const char *name,
assert(s);
if (s->main_pid <= 0 &&
(s->state == SERVICE_START ||
s->state == SERVICE_START_POST ||
s->state == SERVICE_RUNNING ||
s->state == SERVICE_RELOAD))
service_set_main_pid(s, pid);
}
assert(s);
/* This is called by the socket code when instantiating a new
* service for a stream socket and the socket needs to be
* configured. */
return -EINVAL;
if (s->socket_fd >= 0)
return -EBUSY;
if (s->state != SERVICE_DEAD)
return -EAGAIN;
s->got_socket_fd = true;
return 0;
}
static const char* const service_state_table[_SERVICE_STATE_MAX] = {
[SERVICE_DEAD] = "dead",
[SERVICE_START_PRE] = "start-pre",
[SERVICE_START] = "start",
[SERVICE_START_POST] = "start-post",
[SERVICE_RUNNING] = "running",
[SERVICE_EXITED] = "exited",
[SERVICE_RELOAD] = "reload",
[SERVICE_STOP] = "stop",
[SERVICE_STOP_SIGTERM] = "stop-sigterm",
[SERVICE_STOP_SIGKILL] = "stop-sigkill",
[SERVICE_STOP_POST] = "stop-post",
[SERVICE_FINAL_SIGTERM] = "final-sigterm",
[SERVICE_FINAL_SIGKILL] = "final-sigkill",
[SERVICE_MAINTENANCE] = "maintenance",
[SERVICE_AUTO_RESTART] = "auto-restart",
};
static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
[SERVICE_ONCE] = "once",
[SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
[SERVICE_RESTART_ALWAYS] = "restart-always",
};
static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
[SERVICE_SIMPLE] = "simple",
[SERVICE_FORKING] = "forking",
[SERVICE_FINISH] = "finish",
[SERVICE_DBUS] = "dbus",
[SERVICE_NOTIFY] = "notify"
};
static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
[SERVICE_EXEC_START_PRE] = "ExecStartPre",
[SERVICE_EXEC_START] = "ExecStart",
[SERVICE_EXEC_START_POST] = "ExecStartPost",
[SERVICE_EXEC_RELOAD] = "ExecReload",
[SERVICE_EXEC_STOP] = "ExecStop",
[SERVICE_EXEC_STOP_POST] = "ExecStopPost",
};
static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
[NOTIFY_NONE] = "none",
[NOTIFY_MAIN] = "main",
[NOTIFY_ALL] = "all"
};
const UnitVTable service_vtable = {
.suffix = ".service",
.init = service_init,
.done = service_done,
.load = service_load,
.dump = service_dump,
.start = service_start,
.stop = service_stop,
.reload = service_reload,
};