mirror of
https://github.com/troglobit/finit.git
synced 2026-11-15 19:13:11 +00:00
Refactor, shared cleanup fn for service_kill() and service_monitor()
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
d37c20f467
commit
6cdcacf20b
1 file changed
+29
-27
+29
-27
@@ -620,6 +620,19 @@ static void service_kill(svc_t *svc)
|
|||||||
print(2, NULL);
|
print(2, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Clean up any lingering state from dead/killed services
|
||||||
|
*/
|
||||||
|
static void service_cleanup(svc_t *svc)
|
||||||
|
{
|
||||||
|
char *fn;
|
||||||
|
|
||||||
|
fn = pid_file(svc);
|
||||||
|
if (remove(fn) && errno != ENOENT)
|
||||||
|
logit(LOG_CRIT, "Failed removing service %s pidfile %s",
|
||||||
|
basename(svc->cmd), fn);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* service_stop - Stop service
|
* service_stop - Stop service
|
||||||
* @svc: Service to stop
|
* @svc: Service to stop
|
||||||
@@ -629,7 +642,7 @@ static void service_kill(svc_t *svc)
|
|||||||
*/
|
*/
|
||||||
static int service_stop(svc_t *svc)
|
static int service_stop(svc_t *svc)
|
||||||
{
|
{
|
||||||
int res = 0;
|
int rc = 0;
|
||||||
|
|
||||||
if (!svc)
|
if (!svc)
|
||||||
return 1;
|
return 1;
|
||||||
@@ -657,25 +670,18 @@ static int service_stop(svc_t *svc)
|
|||||||
print_desc("Stopping ", svc->desc);
|
print_desc("Stopping ", svc->desc);
|
||||||
|
|
||||||
if (!svc_is_sysv(svc)) {
|
if (!svc_is_sysv(svc)) {
|
||||||
if (svc->pid <= 1)
|
if (svc->pid > 1) {
|
||||||
goto cleanup;
|
/* Kill all children in the same proess group, e.g. logit */
|
||||||
|
rc = kill(-svc->pid, svc->sighalt);
|
||||||
|
|
||||||
res = kill(-svc->pid, svc->sighalt);
|
/* PID lost or forking process never really started */
|
||||||
|
if (rc == -1 && ESRCH == errno)
|
||||||
|
service_cleanup(svc);
|
||||||
|
} else
|
||||||
|
service_cleanup(svc);
|
||||||
|
|
||||||
/* PID lost or forking process never really started */
|
/* No longer running, update books. */
|
||||||
if (res == -1 && ESRCH == errno) {
|
svc->start_time = svc->pid = 0;
|
||||||
char *fn;
|
|
||||||
|
|
||||||
/* XXX: Same clenup as done by service_monitor(), refactor */
|
|
||||||
cleanup:
|
|
||||||
fn = pid_file(svc);
|
|
||||||
if (remove(fn) && errno != ENOENT)
|
|
||||||
logit(LOG_CRIT, "Failed removing service %s pidfile %s",
|
|
||||||
basename(svc->cmd), fn);
|
|
||||||
|
|
||||||
/* No longer running, update books. */
|
|
||||||
svc->start_time = svc->pid = 0;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
char *args[] = { svc->cmd, "stop", NULL };
|
char *args[] = { svc->cmd, "stop", NULL };
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
@@ -689,18 +695,18 @@ static int service_stop(svc_t *svc)
|
|||||||
break;
|
break;
|
||||||
case -1:
|
case -1:
|
||||||
_pe("Failed fork() to call sysv script '%s stop'", svc->cmd);
|
_pe("Failed fork() to call sysv script '%s stop'", svc->cmd);
|
||||||
res = 1;
|
rc = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
res = WEXITSTATUS(complete(svc->cmd, pid));
|
rc = WEXITSTATUS(complete(svc->cmd, pid));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (runlevel != 1)
|
if (runlevel != 1)
|
||||||
print_result(res);
|
print_result(rc);
|
||||||
|
|
||||||
return res;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1159,11 +1165,7 @@ void service_monitor(pid_t lost, int status)
|
|||||||
|
|
||||||
/* Try removing PID file (in case service does not clean up after itself) */
|
/* Try removing PID file (in case service does not clean up after itself) */
|
||||||
if (svc_is_daemon(svc)) {
|
if (svc_is_daemon(svc)) {
|
||||||
char *fn;
|
service_cleanup(svc);
|
||||||
|
|
||||||
fn = pid_file(svc);
|
|
||||||
if (remove(fn) && errno != ENOENT)
|
|
||||||
logit(LOG_CRIT, "Failed removing service %s pidfile %s", basename(svc->cmd), fn);
|
|
||||||
} else if (svc_is_runtask(svc)) {
|
} else if (svc_is_runtask(svc)) {
|
||||||
if (WIFEXITED(status) && !WEXITSTATUS(status))
|
if (WIFEXITED(status) && !WEXITSTATUS(status))
|
||||||
svc->started = 1;
|
svc->started = 1;
|
||||||
|
|||||||
Reference in new issue
Block a user