mirror of
https://github.com/troglobit/finit.git
synced 2026-11-02 13:20:32 +00:00
service: one owner for user-requested start/stop/restart
The D-Bus methods carried byte-for-byte copies of api.c's static start/stop/restart helpers. Promote them to service.c alongside service_reload(), which already serves both callers, and reduce both sides to svc_parse_jobstr-style adapters. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
4 files changed
+90
-75
No files matched your search
@@ -61,53 +61,19 @@ static int call(int (*action)(svc_t *, void *), char *buf, size_t len)
|
|||||||
static int stop(svc_t *svc, void *user_data)
|
static int stop(svc_t *svc, void *user_data)
|
||||||
{
|
{
|
||||||
(void)user_data;
|
(void)user_data;
|
||||||
|
return service_stop_now(svc);
|
||||||
if (!svc)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
service_timeout_cancel(svc);
|
|
||||||
svc_stop(svc);
|
|
||||||
service_step(svc);
|
|
||||||
if (!IS_RESERVED_RUNLEVEL(runlevel))
|
|
||||||
service_step_all(SVC_TYPE_ANY);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int start(svc_t *svc, void *user_data)
|
static int start(svc_t *svc, void *user_data)
|
||||||
{
|
{
|
||||||
(void)user_data;
|
(void)user_data;
|
||||||
|
return service_start_now(svc);
|
||||||
if (!svc)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
service_timeout_cancel(svc);
|
|
||||||
svc_start(svc);
|
|
||||||
service_step(svc);
|
|
||||||
if (!IS_RESERVED_RUNLEVEL(runlevel))
|
|
||||||
service_step_all(SVC_TYPE_ANY);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* NOTE: this does not wait for svc to be stopped first, that is the
|
|
||||||
* responsibility of initctl to do. Otherwise we'd block PID 1,
|
|
||||||
* or introduce some nasty race conditions.
|
|
||||||
*/
|
|
||||||
static int restart(svc_t *svc, void *user_data)
|
static int restart(svc_t *svc, void *user_data)
|
||||||
{
|
{
|
||||||
if (!svc)
|
(void)user_data;
|
||||||
return 1;
|
return service_restart_now(svc);
|
||||||
|
|
||||||
if (!svc_is_running(svc))
|
|
||||||
return start(svc, user_data);
|
|
||||||
|
|
||||||
service_timeout_cancel(svc);
|
|
||||||
service_stop(svc);
|
|
||||||
service_step(svc);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int reload(svc_t *svc, void *user_data)
|
static int reload(svc_t *svc, void *user_data)
|
||||||
|
|||||||
+12
-37
@@ -314,46 +314,24 @@ static int manager_get_service(link_call_t *call, void *userdata)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Service-control helpers used by Start/Stop/Restart/Reload. These
|
/* svc_parse_jobstr-style adapters over the shared service helpers */
|
||||||
* mirror the static helpers in api.c — kept private here so api.c
|
|
||||||
* stays untouched in this increment. */
|
|
||||||
|
|
||||||
static int dbus_apply_stop(svc_t *svc, void *user_data)
|
static int dbus_apply_stop(svc_t *svc, void *user_data)
|
||||||
{
|
{
|
||||||
(void)user_data;
|
(void)user_data;
|
||||||
if (!svc)
|
return service_stop_now(svc);
|
||||||
return 1;
|
|
||||||
service_timeout_cancel(svc);
|
|
||||||
svc_stop(svc);
|
|
||||||
service_step(svc);
|
|
||||||
if (!IS_RESERVED_RUNLEVEL(runlevel))
|
|
||||||
service_step_all(SVC_TYPE_ANY);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dbus_apply_start(svc_t *svc, void *user_data)
|
static int dbus_apply_start(svc_t *svc, void *user_data)
|
||||||
{
|
{
|
||||||
(void)user_data;
|
(void)user_data;
|
||||||
if (!svc)
|
return service_start_now(svc);
|
||||||
return 1;
|
|
||||||
service_timeout_cancel(svc);
|
|
||||||
svc_start(svc);
|
|
||||||
service_step(svc);
|
|
||||||
if (!IS_RESERVED_RUNLEVEL(runlevel))
|
|
||||||
service_step_all(SVC_TYPE_ANY);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dbus_apply_restart(svc_t *svc, void *user_data)
|
static int dbus_apply_restart(svc_t *svc, void *user_data)
|
||||||
{
|
{
|
||||||
if (!svc)
|
(void)user_data;
|
||||||
return 1;
|
return service_restart_now(svc);
|
||||||
if (!svc_is_running(svc))
|
|
||||||
return dbus_apply_start(svc, user_data);
|
|
||||||
service_timeout_cancel(svc);
|
|
||||||
service_stop(svc);
|
|
||||||
service_step(svc);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct dispatch_ctx {
|
struct dispatch_ctx {
|
||||||
@@ -685,18 +663,15 @@ static int service1_start (link_call_t *c, void *u) { return service_action_met
|
|||||||
static int service1_stop (link_call_t *c, void *u) { return service_action_method(c, u, dbus_apply_stop); }
|
static int service1_stop (link_call_t *c, void *u) { return service_action_method(c, u, dbus_apply_stop); }
|
||||||
static int service1_restart(link_call_t *c, void *u) { return service_action_method(c, u, dbus_apply_restart); }
|
static int service1_restart(link_call_t *c, void *u) { return service_action_method(c, u, dbus_apply_restart); }
|
||||||
|
|
||||||
static int service1_reload(link_call_t *call, void *userdata)
|
static int dbus_apply_reload(svc_t *svc, void *user_data)
|
||||||
{
|
{
|
||||||
svc_t *svc = userdata;
|
(void)user_data;
|
||||||
|
return service_reload(svc);
|
||||||
|
}
|
||||||
|
|
||||||
if (!svc)
|
static int service1_reload(link_call_t *c, void *u)
|
||||||
return link_call_reply_error(call,
|
{
|
||||||
"org.finit.Error.NoSuchService",
|
return service_action_method(c, u, dbus_apply_reload);
|
||||||
"Service object no longer valid");
|
|
||||||
|
|
||||||
service_reload(svc);
|
|
||||||
(void)link_call_reply(call);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const link_method_t service_methods[] = {
|
static const link_method_t service_methods[] = {
|
||||||
|
|||||||
@@ -1442,6 +1442,77 @@ int service_stop(svc_t *svc)
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* service_stop_now - Stop a service on user request
|
||||||
|
* @svc: Service to stop
|
||||||
|
*
|
||||||
|
* Shared by the legacy API and the D-Bus methods, like
|
||||||
|
* service_reload().
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK(0) on success, non-zero if @svc is NULL.
|
||||||
|
*/
|
||||||
|
int service_stop_now(svc_t *svc)
|
||||||
|
{
|
||||||
|
if (!svc)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
service_timeout_cancel(svc);
|
||||||
|
svc_stop(svc);
|
||||||
|
service_step(svc);
|
||||||
|
if (!IS_RESERVED_RUNLEVEL(runlevel))
|
||||||
|
service_step_all(SVC_TYPE_ANY);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* service_start_now - Start a service on user request
|
||||||
|
* @svc: Service to start
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK(0) on success, non-zero if @svc is NULL.
|
||||||
|
*/
|
||||||
|
int service_start_now(svc_t *svc)
|
||||||
|
{
|
||||||
|
if (!svc)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
service_timeout_cancel(svc);
|
||||||
|
svc_start(svc);
|
||||||
|
service_step(svc);
|
||||||
|
if (!IS_RESERVED_RUNLEVEL(runlevel))
|
||||||
|
service_step_all(SVC_TYPE_ANY);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* service_restart_now - Restart a service on user request
|
||||||
|
* @svc: Service to restart
|
||||||
|
*
|
||||||
|
* Does not wait for @svc to stop first, that is the caller's
|
||||||
|
* (initctl) responsibility. Otherwise we'd block PID 1, or
|
||||||
|
* introduce some nasty race conditions.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK(0) on success, non-zero if @svc is NULL.
|
||||||
|
*/
|
||||||
|
int service_restart_now(svc_t *svc)
|
||||||
|
{
|
||||||
|
if (!svc)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (!svc_is_running(svc))
|
||||||
|
return service_start_now(svc);
|
||||||
|
|
||||||
|
service_timeout_cancel(svc);
|
||||||
|
service_stop(svc);
|
||||||
|
service_step(svc);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* service_reload - Request reload of a service, driven by the state machine
|
* service_reload - Request reload of a service, driven by the state machine
|
||||||
* @svc: Service to reload
|
* @svc: Service to reload
|
||||||
|
|||||||
@@ -60,6 +60,9 @@ void service_forked (svc_t *svc);
|
|||||||
void service_ready (svc_t *svc, int ready);
|
void service_ready (svc_t *svc, int ready);
|
||||||
|
|
||||||
int service_stop (svc_t *svc);
|
int service_stop (svc_t *svc);
|
||||||
|
int service_start_now (svc_t *svc);
|
||||||
|
int service_stop_now (svc_t *svc);
|
||||||
|
int service_restart_now (svc_t *svc);
|
||||||
int service_reload (svc_t *svc);
|
int service_reload (svc_t *svc);
|
||||||
int service_step (svc_t *svc);
|
int service_step (svc_t *svc);
|
||||||
void service_step_all (int types);
|
void service_step_all (int types);
|
||||||
|
|||||||
Reference in new issue
Block a user