public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "William Hubbs" <williamh@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/openrc:master commit in: src/rc/
Date: Thu, 24 Aug 2017 16:45:35 +0000 (UTC)	[thread overview]
Message-ID: <1503592458.d7938f54f29193251e083ad35a7d464949829096.williamh@OpenRC> (raw)

commit:     d7938f54f29193251e083ad35a7d464949829096
Author:     William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Thu Aug 24 16:34:18 2017 +0000
Commit:     William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Thu Aug 24 16:34:18 2017 +0000
URL:        https://gitweb.gentoo.org/proj/openrc.git/commit/?id=d7938f54

start-stop-daemon: move --retry processing code to a shared module

This was part of start-stop-daemon; however, it needs to be shared in
order to be used by supervise-daemon.

 src/rc/Makefile            |   2 +-
 src/rc/rc-schedules.c      | 425 +++++++++++++++++++++++++++++++++++++++++++++
 src/rc/rc-schedules.h      |  27 +++
 src/rc/start-stop-daemon.c | 402 +-----------------------------------------
 4 files changed, 461 insertions(+), 395 deletions(-)

diff --git a/src/rc/Makefile b/src/rc/Makefile
index 19adcafb..e6c9f4a2 100644
--- a/src/rc/Makefile
+++ b/src/rc/Makefile
@@ -156,7 +156,7 @@ rc-service service: rc-service.o _usage.o rc-misc.o
 rc-update: rc-update.o _usage.o rc-misc.o
 	${CC} ${LOCAL_CFLAGS} ${LOCAL_LDFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${LDADD}
 
-start-stop-daemon: start-stop-daemon.o _usage.o rc-misc.o
+start-stop-daemon: start-stop-daemon.o _usage.o rc-misc.o rc-schedules.o
 	${CC} ${LOCAL_CFLAGS} ${LOCAL_LDFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${LDADD}
 
 supervise-daemon: supervise-daemon.o _usage.o rc-misc.o

diff --git a/src/rc/rc-schedules.c b/src/rc/rc-schedules.c
new file mode 100644
index 00000000..0390ef9c
--- /dev/null
+++ b/src/rc/rc-schedules.c
@@ -0,0 +1,425 @@
+/*
+ * The functions in this file control the stopping of daemons by
+ * start-stop-daemon and supervise-daemon.
+ */
+
+/*
+ * Copyright (c) 2015 The OpenRC Authors.
+ * See the Authors file at the top-level directory of this distribution and
+ * https://github.com/OpenRC/openrc/blob/master/AUTHORS
+ *
+ * This file is part of OpenRC. It is subject to the license terms in
+ * the LICENSE file found in the top-level directory of this
+ * distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+ * This file may not be copied, modified, propagated, or distributed
+ *    except according to the terms contained in the LICENSE file.
+ */
+
+/* nano seconds */
+#define POLL_INTERVAL   20000000
+#define WAIT_PIDFILE   500000000
+#define ONE_SECOND    1000000000
+#define ONE_MS           1000000
+
+#include <ctype.h>
+#include <errno.h>
+#include <signal.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include "einfo.h"
+#include "queue.h"
+#include "rc.h"
+#include "rc-misc.h"
+#include "rc-schedules.h"
+#include "helpers.h"
+
+typedef struct scheduleitem {
+	enum {
+		SC_TIMEOUT,
+		SC_SIGNAL,
+		SC_GOTO,
+		SC_FOREVER,
+	} type;
+	int value;
+	struct scheduleitem *gotoitem;
+	TAILQ_ENTRY(scheduleitem) entries;
+} SCHEDULEITEM;
+
+static TAILQ_HEAD(, scheduleitem) schedule;
+
+void initialize_schedulelist(void)
+{
+	TAILQ_INIT(&schedule);
+}
+
+void free_schedulelist(void)
+{
+	SCHEDULEITEM *s1 = TAILQ_FIRST(&schedule);
+	SCHEDULEITEM *s2;
+
+	while (s1) {
+		s2 = TAILQ_NEXT(s1, entries);
+		free(s1);
+		s1 = s2;
+	}
+	TAILQ_INIT(&schedule);
+}
+
+int parse_signal(const char *applet, const char *sig)
+{
+	typedef struct signalpair
+	{
+		const char *name;
+		int signal;
+	} SIGNALPAIR;
+
+#define signalpair_item(name) { #name, SIG##name },
+
+	static const SIGNALPAIR signallist[] = {
+		signalpair_item(HUP)
+		signalpair_item(INT)
+		signalpair_item(QUIT)
+		signalpair_item(ILL)
+		signalpair_item(TRAP)
+		signalpair_item(ABRT)
+		signalpair_item(BUS)
+		signalpair_item(FPE)
+		signalpair_item(KILL)
+		signalpair_item(USR1)
+		signalpair_item(SEGV)
+		signalpair_item(USR2)
+		signalpair_item(PIPE)
+		signalpair_item(ALRM)
+		signalpair_item(TERM)
+		signalpair_item(CHLD)
+		signalpair_item(CONT)
+		signalpair_item(STOP)
+		signalpair_item(TSTP)
+		signalpair_item(TTIN)
+		signalpair_item(TTOU)
+		signalpair_item(URG)
+		signalpair_item(XCPU)
+		signalpair_item(XFSZ)
+		signalpair_item(VTALRM)
+		signalpair_item(PROF)
+#ifdef SIGWINCH
+		signalpair_item(WINCH)
+#endif
+#ifdef SIGIO
+		signalpair_item(IO)
+#endif
+#ifdef SIGPWR
+		signalpair_item(PWR)
+#endif
+		signalpair_item(SYS)
+		{ "NULL",	0 },
+	};
+
+	unsigned int i = 0;
+	const char *s;
+
+	if (!sig || *sig == '\0')
+		return -1;
+
+	if (sscanf(sig, "%u", &i) == 1) {
+		if (i < NSIG)
+			return i;
+		eerrorx("%s: `%s' is not a valid signal", applet, sig);
+	}
+
+	if (strncmp(sig, "SIG", 3) == 0)
+		s = sig + 3;
+	else
+		s = NULL;
+
+	for (i = 0; i < ARRAY_SIZE(signallist); ++i)
+		if (strcmp(sig, signallist[i].name) == 0 ||
+		    (s && strcmp(s, signallist[i].name) == 0))
+			return signallist[i].signal;
+
+	eerrorx("%s: `%s' is not a valid signal", applet, sig);
+	/* NOTREACHED */
+}
+
+static SCHEDULEITEM *parse_schedule_item(const char *applet, const char *string)
+{
+	const char *after_hyph;
+	int sig;
+	SCHEDULEITEM *item = xmalloc(sizeof(*item));
+
+	item->value = 0;
+	item->gotoitem = NULL;
+	if (strcmp(string,"forever") == 0)
+		item->type = SC_FOREVER;
+	else if (isdigit((unsigned char)string[0])) {
+		item->type = SC_TIMEOUT;
+		errno = 0;
+		if (sscanf(string, "%d", &item->value) != 1)
+			eerrorx("%s: invalid timeout value in schedule `%s'",
+			    applet, string);
+	} else if ((after_hyph = string + (string[0] == '-')) &&
+	    ((sig = parse_signal(applet, after_hyph)) != -1))
+	{
+		item->type = SC_SIGNAL;
+		item->value = (int)sig;
+	} else
+		eerrorx("%s: invalid schedule item `%s'", applet, string);
+
+	return item;
+}
+
+void parse_schedule(const char *applet, const char *string, int timeout)
+{
+	char buffer[20];
+	const char *slash;
+	int count = 0;
+	SCHEDULEITEM *repeatat = NULL;
+	size_t len;
+	SCHEDULEITEM *item;
+
+	if (string)
+		for (slash = string; *slash; slash++)
+			if (*slash == '/')
+				count++;
+
+	free_schedulelist();
+
+	if (count == 0) {
+		item = xmalloc(sizeof(*item));
+		item->type = SC_SIGNAL;
+		item->value = timeout;
+		item->gotoitem = NULL;
+		TAILQ_INSERT_TAIL(&schedule, item, entries);
+
+		item = xmalloc(sizeof(*item));
+		item->type = SC_TIMEOUT;
+		item->gotoitem = NULL;
+		TAILQ_INSERT_TAIL(&schedule, item, entries);
+		if (string) {
+			if (sscanf(string, "%d", &item->value) != 1)
+				eerrorx("%s: invalid timeout in schedule",
+				    applet);
+		} else
+			item->value = 5;
+
+		return;
+	}
+
+	while (string != NULL) {
+		if ((slash = strchr(string, '/')))
+			len = slash - string;
+		else
+			len = strlen(string);
+
+		if (len >= (ptrdiff_t)sizeof(buffer))
+			eerrorx("%s: invalid schedule item, far too long",
+			    applet);
+
+		memcpy(buffer, string, len);
+		buffer[len] = 0;
+		string = slash ? slash + 1 : NULL;
+
+		item = parse_schedule_item(applet, buffer);
+		TAILQ_INSERT_TAIL(&schedule, item, entries);
+		if (item->type == SC_FOREVER) {
+			if (repeatat)
+				eerrorx("%s: invalid schedule, `forever' "
+				    "appears more than once", applet);
+
+			repeatat = item;
+			continue;
+		}
+	}
+
+	if (repeatat) {
+		item = xmalloc(sizeof(*item));
+		item->type = SC_GOTO;
+		item->value = 0;
+		item->gotoitem = repeatat;
+		TAILQ_INSERT_TAIL(&schedule, item, entries);
+	}
+
+	return;
+}
+
+/* return number of processes killed, -1 on error */
+int do_stop(const char *applet, const char *exec, const char *const *argv,
+    pid_t pid, uid_t uid,int sig, bool test)
+{
+	RC_PIDLIST *pids;
+	RC_PID *pi;
+	RC_PID *np;
+	bool killed;
+	int nkilled = 0;
+
+	if (pid)
+		pids = rc_find_pids(NULL, NULL, 0, pid);
+	else
+		pids = rc_find_pids(exec, argv, uid, pid);
+
+	if (!pids)
+		return 0;
+
+	LIST_FOREACH_SAFE(pi, pids, entries, np) {
+		if (test) {
+			einfo("Would send signal %d to PID %d", sig, pi->pid);
+			nkilled++;
+		} else {
+			ebeginv("Sending signal %d to PID %d", sig, pi->pid);
+			errno = 0;
+			killed = (kill(pi->pid, sig) == 0 ||
+			    errno == ESRCH ? true : false);
+			eendv(killed ? 0 : 1,
+				"%s: failed to send signal %d to PID %d: %s",
+				applet, sig, pi->pid, strerror(errno));
+			if (!killed) {
+				nkilled = -1;
+			} else {
+				if (nkilled != -1)
+					nkilled++;
+			}
+		}
+		free(pi);
+	}
+
+	free(pids);
+	return nkilled;
+}
+
+int run_stop_schedule(const char *applet,
+		const char *exec, const char *const *argv,
+		const char *pidfile, uid_t uid,
+    bool test, bool progress)
+{
+	SCHEDULEITEM *item = TAILQ_FIRST(&schedule);
+	int nkilled = 0;
+	int tkilled = 0;
+	int nrunning = 0;
+	long nloops, nsecs;
+	struct timespec ts;
+	pid_t pid = 0;
+	const char *const *p;
+	bool progressed = false;
+
+	if (exec)
+		einfov("Will stop %s", exec);
+	if (pidfile)
+		einfov("Will stop PID in pidfile `%s'", pidfile);
+	if (uid)
+		einfov("Will stop processes owned by UID %d", uid);
+	if (argv && *argv) {
+		einfovn("Will stop processes of `");
+		if (rc_yesno(getenv("EINFO_VERBOSE"))) {
+			for (p = argv; p && *p; p++) {
+				if (p != argv)
+					printf(" ");
+				printf("%s", *p);
+			}
+			printf("'\n");
+		}
+	}
+
+	if (pidfile) {
+		pid = get_pid(applet, pidfile);
+		if (pid == -1)
+			return 0;
+	}
+
+	while (item) {
+		switch (item->type) {
+		case SC_GOTO:
+			item = item->gotoitem;
+			continue;
+
+		case SC_SIGNAL:
+			nrunning = 0;
+			nkilled = do_stop(applet, exec, argv, pid, uid, item->value, test);
+			if (nkilled == 0) {
+				if (tkilled == 0) {
+					if (progressed)
+						printf("\n");
+					eerror("%s: no matching processes found", applet);
+				}
+				return tkilled;
+			}
+			else if (nkilled == -1)
+				return 0;
+
+			tkilled += nkilled;
+			break;
+		case SC_TIMEOUT:
+			if (item->value < 1) {
+				item = NULL;
+				break;
+			}
+
+			ts.tv_sec = 0;
+			ts.tv_nsec = POLL_INTERVAL;
+
+			for (nsecs = 0; nsecs < item->value; nsecs++) {
+				for (nloops = 0;
+				     nloops < ONE_SECOND / POLL_INTERVAL;
+				     nloops++)
+				{
+					if ((nrunning = do_stop(applet, exec, argv,
+						    pid, uid, 0, test)) == 0)
+						return 0;
+
+
+					if (nanosleep(&ts, NULL) == -1) {
+						if (progressed) {
+							printf("\n");
+							progressed = false;
+						}
+						if (errno == EINTR)
+							eerror("%s: caught an"
+							    " interrupt", applet);
+						else {
+							eerror("%s: nanosleep: %s",
+							    applet, strerror(errno));
+							return 0;
+						}
+					}
+				}
+				if (progress) {
+					printf(".");
+					fflush(stdout);
+					progressed = true;
+				}
+			}
+			break;
+		default:
+			if (progressed) {
+				printf("\n");
+				progressed = false;
+			}
+			eerror("%s: invalid schedule item `%d'",
+			    applet, item->type);
+			return 0;
+		}
+
+		if (item)
+			item = TAILQ_NEXT(item, entries);
+	}
+
+	if (test || (tkilled > 0 && nrunning == 0))
+		return nkilled;
+
+	if (progressed)
+		printf("\n");
+	if (nrunning == 1)
+		eerror("%s: %d process refused to stop", applet, nrunning);
+	else
+		eerror("%s: %d process(es) refused to stop", applet, nrunning);
+
+	return -nrunning;
+}

diff --git a/src/rc/rc-schedules.h b/src/rc/rc-schedules.h
new file mode 100644
index 00000000..6bed7916
--- /dev/null
+++ b/src/rc/rc-schedules.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2017 The OpenRC Authors.
+ * See the Authors file at the top-level directory of this distribution and
+ * https://github.com/OpenRC/openrc/blob/master/AUTHORS
+ *
+ * This file is part of OpenRC. It is subject to the license terms in
+ * the LICENSE file found in the top-level directory of this
+ * distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+ * This file may not be copied, modified, propagated, or distributed
+ *    except according to the terms contained in the LICENSE file.
+ */
+
+#ifndef __RC_SCHEDULES_H
+#define __RC_SCHEDULES_H
+
+void initialize_schedulelist(void);
+void free_schedulelist(void);
+int parse_signal(const char *applet, const char *sig);
+void parse_schedule(const char *applet, const char *string, int timeout);
+int do_stop(const char *applet, const char *exec, const char *const *argv,
+		pid_t pid, uid_t uid,int sig, bool test);
+int run_stop_schedule(const char *applet,
+		const char *exec, const char *const *argv,
+		const char *pidfile, uid_t uid,
+		bool test, bool progress);
+
+#endif

diff --git a/src/rc/start-stop-daemon.c b/src/rc/start-stop-daemon.c
index 451d4a5c..df5b3184 100644
--- a/src/rc/start-stop-daemon.c
+++ b/src/rc/start-stop-daemon.c
@@ -19,10 +19,6 @@
  *    except according to the terms contained in the LICENSE file.
  */
 
-/* nano seconds */
-#define POLL_INTERVAL   20000000
-#define WAIT_PIDFILE   500000000
-#define ONE_SECOND    1000000000
 #define ONE_MS           1000000
 
 #include <sys/types.h>
@@ -63,6 +59,7 @@ static struct pam_conv conv = { NULL, NULL};
 #include "queue.h"
 #include "rc.h"
 #include "rc-misc.h"
+#include "rc-schedules.h"
 #include "_usage.h"
 #include "helpers.h"
 
@@ -130,20 +127,6 @@ const char * const longopts_help[] = {
 };
 const char *usagestring = NULL;
 
-typedef struct scheduleitem
-{
-	enum
-		{
-			SC_TIMEOUT,
-			SC_SIGNAL,
-			SC_GOTO,
-			SC_FOREVER
-		} type;
-	int value;
-	struct scheduleitem *gotoitem;
-	TAILQ_ENTRY(scheduleitem) entries;
-} SCHEDULEITEM;
-TAILQ_HEAD(, scheduleitem) schedule;
 static char **nav;
 
 static char *changeuser, *ch_root, *ch_dir;
@@ -167,20 +150,6 @@ static inline int ioprio_set(int which _unused,
 #endif
 
 static void
-free_schedulelist(void)
-{
-	SCHEDULEITEM *s1 = TAILQ_FIRST(&schedule);
-	SCHEDULEITEM *s2;
-
-	while (s1) {
-		s2 = TAILQ_NEXT(s1, entries);
-		free(s1);
-		s1 = s2;
-	}
-	TAILQ_INIT(&schedule);
-}
-
-static void
 cleanup(void)
 {
 	free(changeuser);
@@ -188,360 +157,6 @@ cleanup(void)
 	free_schedulelist();
 }
 
-static int
-parse_signal(const char *sig)
-{
-	typedef struct signalpair
-	{
-		const char *name;
-		int signal;
-	} SIGNALPAIR;
-
-#define signalpair_item(name) { #name, SIG##name },
-
-	static const SIGNALPAIR signallist[] = {
-		signalpair_item(HUP)
-		signalpair_item(INT)
-		signalpair_item(QUIT)
-		signalpair_item(ILL)
-		signalpair_item(TRAP)
-		signalpair_item(ABRT)
-		signalpair_item(BUS)
-		signalpair_item(FPE)
-		signalpair_item(KILL)
-		signalpair_item(USR1)
-		signalpair_item(SEGV)
-		signalpair_item(USR2)
-		signalpair_item(PIPE)
-		signalpair_item(ALRM)
-		signalpair_item(TERM)
-		signalpair_item(CHLD)
-		signalpair_item(CONT)
-		signalpair_item(STOP)
-		signalpair_item(TSTP)
-		signalpair_item(TTIN)
-		signalpair_item(TTOU)
-		signalpair_item(URG)
-		signalpair_item(XCPU)
-		signalpair_item(XFSZ)
-		signalpair_item(VTALRM)
-		signalpair_item(PROF)
-#ifdef SIGWINCH
-		signalpair_item(WINCH)
-#endif
-#ifdef SIGIO
-		signalpair_item(IO)
-#endif
-#ifdef SIGPWR
-		signalpair_item(PWR)
-#endif
-		signalpair_item(SYS)
-		{ "NULL",	0 },
-	};
-
-	unsigned int i = 0;
-	const char *s;
-
-	if (!sig || *sig == '\0')
-		return -1;
-
-	if (sscanf(sig, "%u", &i) == 1) {
-		if (i < NSIG)
-			return i;
-		eerrorx("%s: `%s' is not a valid signal", applet, sig);
-	}
-
-	if (strncmp(sig, "SIG", 3) == 0)
-		s = sig + 3;
-	else
-		s = NULL;
-
-	for (i = 0; i < ARRAY_SIZE(signallist); ++i)
-		if (strcmp(sig, signallist[i].name) == 0 ||
-		    (s && strcmp(s, signallist[i].name) == 0))
-			return signallist[i].signal;
-
-	eerrorx("%s: `%s' is not a valid signal", applet, sig);
-	/* NOTREACHED */
-}
-
-static SCHEDULEITEM *
-parse_schedule_item(const char *string)
-{
-	const char *after_hyph;
-	int sig;
-	SCHEDULEITEM *item = xmalloc(sizeof(*item));
-
-	item->value = 0;
-	item->gotoitem = NULL;
-	if (strcmp(string,"forever") == 0)
-		item->type = SC_FOREVER;
-	else if (isdigit((unsigned char)string[0])) {
-		item->type = SC_TIMEOUT;
-		errno = 0;
-		if (sscanf(string, "%d", &item->value) != 1)
-			eerrorx("%s: invalid timeout value in schedule `%s'",
-			    applet, string);
-	} else if ((after_hyph = string + (string[0] == '-')) &&
-	    ((sig = parse_signal(after_hyph)) != -1))
-	{
-		item->type = SC_SIGNAL;
-		item->value = (int)sig;
-	} else
-		eerrorx("%s: invalid schedule item `%s'", applet, string);
-
-	return item;
-}
-
-static void
-parse_schedule(const char *string, int timeout)
-{
-	char buffer[20];
-	const char *slash;
-	int count = 0;
-	SCHEDULEITEM *repeatat = NULL;
-	size_t len;
-	SCHEDULEITEM *item;
-
-	if (string)
-		for (slash = string; *slash; slash++)
-			if (*slash == '/')
-				count++;
-
-	free_schedulelist();
-
-	if (count == 0) {
-		item = xmalloc(sizeof(*item));
-		item->type = SC_SIGNAL;
-		item->value = timeout;
-		item->gotoitem = NULL;
-		TAILQ_INSERT_TAIL(&schedule, item, entries);
-
-		item = xmalloc(sizeof(*item));
-		item->type = SC_TIMEOUT;
-		item->gotoitem = NULL;
-		TAILQ_INSERT_TAIL(&schedule, item, entries);
-		if (string) {
-			if (sscanf(string, "%d", &item->value) != 1)
-				eerrorx("%s: invalid timeout in schedule",
-				    applet);
-		} else
-			item->value = 5;
-
-		return;
-	}
-
-	while (string != NULL) {
-		if ((slash = strchr(string, '/')))
-			len = slash - string;
-		else
-			len = strlen(string);
-
-		if (len >= (ptrdiff_t)sizeof(buffer))
-			eerrorx("%s: invalid schedule item, far too long",
-			    applet);
-
-		memcpy(buffer, string, len);
-		buffer[len] = 0;
-		string = slash ? slash + 1 : NULL;
-
-		item = parse_schedule_item(buffer);
-		TAILQ_INSERT_TAIL(&schedule, item, entries);
-		if (item->type == SC_FOREVER) {
-			if (repeatat)
-				eerrorx("%s: invalid schedule, `forever' "
-				    "appears more than once", applet);
-
-			repeatat = item;
-			continue;
-		}
-	}
-
-	if (repeatat) {
-		item = xmalloc(sizeof(*item));
-		item->type = SC_GOTO;
-		item->value = 0;
-		item->gotoitem = repeatat;
-		TAILQ_INSERT_TAIL(&schedule, item, entries);
-	}
-
-	return;
-}
-
-/* return number of processed killed, -1 on error */
-static int
-do_stop(const char *exec, const char *const *argv,
-    pid_t pid, uid_t uid,int sig, bool test)
-{
-	RC_PIDLIST *pids;
-	RC_PID *pi;
-	RC_PID *np;
-	bool killed;
-	int nkilled = 0;
-
-	if (pid)
-		pids = rc_find_pids(NULL, NULL, 0, pid);
-	else
-		pids = rc_find_pids(exec, argv, uid, pid);
-
-	if (!pids)
-		return 0;
-
-	LIST_FOREACH_SAFE(pi, pids, entries, np) {
-		if (test) {
-			einfo("Would send signal %d to PID %d", sig, pi->pid);
-			nkilled++;
-		} else {
-			ebeginv("Sending signal %d to PID %d", sig, pi->pid);
-			errno = 0;
-			killed = (kill(pi->pid, sig) == 0 ||
-			    errno == ESRCH ? true : false);
-			eendv(killed ? 0 : 1,
-				"%s: failed to send signal %d to PID %d: %s",
-				applet, sig, pi->pid, strerror(errno));
-			if (!killed) {
-				nkilled = -1;
-			} else {
-				if (nkilled != -1)
-					nkilled++;
-			}
-		}
-		free(pi);
-	}
-
-	free(pids);
-	return nkilled;
-}
-
-static int
-run_stop_schedule(const char *exec, const char *const *argv,
-    const char *pidfile, uid_t uid,
-    bool test, bool progress)
-{
-	SCHEDULEITEM *item = TAILQ_FIRST(&schedule);
-	int nkilled = 0;
-	int tkilled = 0;
-	int nrunning = 0;
-	long nloops, nsecs;
-	struct timespec ts;
-	pid_t pid = 0;
-	const char *const *p;
-	bool progressed = false;
-
-	if (exec)
-		einfov("Will stop %s", exec);
-	if (pidfile)
-		einfov("Will stop PID in pidfile `%s'", pidfile);
-	if (uid)
-		einfov("Will stop processes owned by UID %d", uid);
-	if (argv && *argv) {
-		einfovn("Will stop processes of `");
-		if (rc_yesno(getenv("EINFO_VERBOSE"))) {
-			for (p = argv; p && *p; p++) {
-				if (p != argv)
-					printf(" ");
-				printf("%s", *p);
-			}
-			printf("'\n");
-		}
-	}
-
-	if (pidfile) {
-		pid = get_pid(applet, pidfile);
-		if (pid == -1)
-			return 0;
-	}
-
-	while (item) {
-		switch (item->type) {
-		case SC_GOTO:
-			item = item->gotoitem;
-			continue;
-
-		case SC_SIGNAL:
-			nrunning = 0;
-			nkilled = do_stop(exec, argv, pid, uid, item->value, test);
-			if (nkilled == 0) {
-				if (tkilled == 0) {
-					if (progressed)
-						printf("\n");
-					eerror("%s: no matching processes found", applet);
-				}
-				return tkilled;
-			}
-			else if (nkilled == -1)
-				return 0;
-
-			tkilled += nkilled;
-			break;
-		case SC_TIMEOUT:
-			if (item->value < 1) {
-				item = NULL;
-				break;
-			}
-
-			ts.tv_sec = 0;
-			ts.tv_nsec = POLL_INTERVAL;
-
-			for (nsecs = 0; nsecs < item->value; nsecs++) {
-				for (nloops = 0;
-				     nloops < ONE_SECOND / POLL_INTERVAL;
-				     nloops++)
-				{
-					if ((nrunning = do_stop(exec, argv,
-						    pid, uid, 0, test)) == 0)
-						return 0;
-
-
-					if (nanosleep(&ts, NULL) == -1) {
-						if (progressed) {
-							printf("\n");
-							progressed = false;
-						}
-						if (errno == EINTR)
-							eerror("%s: caught an"
-							    " interrupt", applet);
-						else {
-							eerror("%s: nanosleep: %s",
-							    applet, strerror(errno));
-							return 0;
-						}
-					}
-				}
-				if (progress) {
-					printf(".");
-					fflush(stdout);
-					progressed = true;
-				}
-			}
-			break;
-		default:
-			if (progressed) {
-				printf("\n");
-				progressed = false;
-			}
-			eerror("%s: invalid schedule item `%d'",
-			    applet, item->type);
-			return 0;
-		}
-
-		if (item)
-			item = TAILQ_NEXT(item, entries);
-	}
-
-	if (test || (tkilled > 0 && nrunning == 0))
-		return nkilled;
-
-	if (progressed)
-		printf("\n");
-	if (nrunning == 1)
-		eerror("%s: %d process refused to stop", applet, nrunning);
-	else
-		eerror("%s: %d process(es) refused to stop", applet, nrunning);
-
-	return -nrunning;
-}
-
 static void
 handle_signal(int sig)
 {
@@ -682,7 +297,6 @@ int main(int argc, char **argv)
 	unsigned int start_wait = 0;
 
 	applet = basename_c(argv[0]);
-	TAILQ_INIT(&schedule);
 	atexit(cleanup);
 
 	signal_setup(SIGINT, handle_signal);
@@ -851,7 +465,7 @@ int main(int argc, char **argv)
 			break;
 
 		case 's':  /* --signal <signal> */
-			sig = parse_signal(optarg);
+			sig = parse_signal(applet, optarg);
 			break;
 
 		case 't':  /* --test */
@@ -1037,12 +651,12 @@ int main(int argc, char **argv)
 		if (!stop)
 			oknodo = true;
 		if (retry)
-			parse_schedule(retry, sig);
+			parse_schedule(applet, retry, sig);
 		else if (test || oknodo)
-			parse_schedule("0", sig);
+			parse_schedule(applet, "0", sig);
 		else
-			parse_schedule(NULL, sig);
-		i = run_stop_schedule(exec, (const char *const *)margv,
+			parse_schedule(applet, NULL, sig);
+		i = run_stop_schedule(applet, exec, (const char *const *)margv,
 		    pidfile, uid, test, progress);
 
 		if (i < 0)
@@ -1069,7 +683,7 @@ int main(int argc, char **argv)
 	else
 		pid = 0;
 
-	if (do_stop(exec, (const char * const *)margv, pid, uid,
+	if (do_stop(applet, exec, (const char * const *)margv, pid, uid,
 		0, test) > 0)
 		eerrorx("%s: %s is already running", applet, exec);
 
@@ -1349,7 +963,7 @@ int main(int argc, char **argv)
 				}
 			} else
 				pid = 0;
-			if (do_stop(exec, (const char *const *)margv,
+			if (do_stop(applet, exec, (const char *const *)margv,
 				pid, uid, 0, test) > 0)
 				alive = true;
 		}


             reply	other threads:[~2017-08-24 16:45 UTC|newest]

Thread overview: 257+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-24 16:45 William Hubbs [this message]
  -- strict thread matches above, loose matches on Subject: below --
2018-11-06  3:38 [gentoo-commits] proj/openrc:master commit in: src/rc/ William Hubbs
2018-11-06  3:38 William Hubbs
2018-11-02 23:24 William Hubbs
2018-11-02 23:24 William Hubbs
2018-10-15 16:52 William Hubbs
2018-10-15 16:52 William Hubbs
2018-10-09 16:35 William Hubbs
2018-10-06 18:03 William Hubbs
2018-10-06 18:03 William Hubbs
2018-06-29 20:32 William Hubbs
2018-06-28 18:03 William Hubbs
2018-06-20 14:38 William Hubbs
2018-06-19 23:00 William Hubbs
2018-06-19 23:00 William Hubbs
2018-06-19 22:07 William Hubbs
2018-06-19 21:34 William Hubbs
2018-06-19 21:34 William Hubbs
2018-06-19 21:34 William Hubbs
2018-06-19 21:34 William Hubbs
2018-06-14 19:56 William Hubbs
2018-06-13 21:28 William Hubbs
2018-05-22 22:12 William Hubbs
2018-05-22 22:12 William Hubbs
2018-05-22 22:12 William Hubbs
2018-05-15 22:11 William Hubbs
2018-05-15 22:11 William Hubbs
2018-05-15  0:11 William Hubbs
2018-05-15  0:11 William Hubbs
2018-05-11 18:19 William Hubbs
2018-05-09 22:35 William Hubbs
2018-05-07 23:21 William Hubbs
2018-05-07 23:21 William Hubbs
2018-03-15  1:43 William Hubbs
2018-03-12  2:43 William Hubbs
2018-02-28 18:45 William Hubbs
2018-02-26 20:34 William Hubbs
2018-02-26 19:15 William Hubbs
2018-02-26 19:15 William Hubbs
2018-02-26 18:23 William Hubbs
2018-02-24 23:03 William Hubbs
2018-02-24 23:03 William Hubbs
2018-02-22 22:17 William Hubbs
2018-02-22 18:55 William Hubbs
2018-02-22  0:17 William Hubbs
2018-02-22  0:17 William Hubbs
2018-02-22  0:17 William Hubbs
2018-02-21 19:50 William Hubbs
2018-02-20 22:36 William Hubbs
2018-02-20 22:36 William Hubbs
2018-02-20 22:36 William Hubbs
2018-02-16 20:07 William Hubbs
2018-02-16 20:07 William Hubbs
2018-02-16 20:07 William Hubbs
2018-02-14 23:37 William Hubbs
2018-02-14 23:37 William Hubbs
2018-01-24 23:44 William Hubbs
2018-01-23 23:08 William Hubbs
2018-01-16 19:36 William Hubbs
2018-01-16 19:14 William Hubbs
2018-01-09 23:35 William Hubbs
2017-11-29 21:12 William Hubbs
2017-11-28 23:17 William Hubbs
2017-11-28 23:17 William Hubbs
2017-11-07 21:33 William Hubbs
2017-11-07 21:33 William Hubbs
2017-11-07 21:33 William Hubbs
2017-11-07 21:33 William Hubbs
2017-10-26 22:01 William Hubbs
2017-10-26 18:58 William Hubbs
2017-10-26 18:58 William Hubbs
2017-10-26 18:58 William Hubbs
2017-10-25 20:10 William Hubbs
2017-10-18 23:09 William Hubbs
2017-10-05 23:31 William Hubbs
2017-09-22 22:25 William Hubbs
2017-09-18 18:31 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-15 18:31 William Hubbs
2017-09-06 22:33 William Hubbs
2017-08-25 16:48 William Hubbs
2017-07-24 23:27 William Hubbs
2017-06-12 15:41 William Hubbs
2017-06-12 15:41 William Hubbs
2017-06-07 16:34 William Hubbs
2017-05-30 21:25 William Hubbs
2017-05-30 21:25 William Hubbs
2017-05-22 16:30 William Hubbs
2017-05-19 23:30 William Hubbs
2017-05-16  0:00 William Hubbs
2017-05-16  0:00 William Hubbs
2017-05-12  2:42 William Hubbs
2017-05-12  2:42 William Hubbs
2017-05-12  2:42 William Hubbs
2017-05-11 16:38 William Hubbs
2017-04-29 22:56 William Hubbs
2017-04-29 22:56 William Hubbs
2017-04-29 22:56 William Hubbs
2017-04-29 14:44 William Hubbs
2017-04-17 17:24 William Hubbs
2017-04-13 17:56 William Hubbs
2017-04-07 12:43 William Hubbs
2017-04-03 15:45 William Hubbs
2017-01-25 23:58 William Hubbs
2017-01-05  0:24 William Hubbs
2017-01-04 23:23 William Hubbs
2016-11-30 22:49 William Hubbs
2016-11-03 16:13 William Hubbs
2016-10-24 17:48 William Hubbs
2016-10-24 17:43 William Hubbs
2016-10-11 15:31 William Hubbs
2016-09-30 22:10 William Hubbs
2016-09-19 17:10 William Hubbs
2016-09-16 13:58 William Hubbs
2016-09-16 13:58 William Hubbs
2016-08-25 16:17 William Hubbs
2016-07-25 18:54 William Hubbs
2016-07-16 20:17 William Hubbs
2016-07-14 17:25 William Hubbs
2016-06-10 22:45 William Hubbs
2016-05-24 16:43 William Hubbs
2016-05-24 16:43 William Hubbs
2016-05-13 18:00 William Hubbs
2016-05-13 17:12 William Hubbs
2016-05-04 23:24 William Hubbs
2016-04-11 16:18 William Hubbs
2016-02-12 18:58 William Hubbs
2016-01-22 18:53 William Hubbs
2016-01-20 17:29 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-14 17:33 William Hubbs
2016-01-14 17:33 William Hubbs
2016-01-14 17:33 William Hubbs
2016-01-14 17:33 William Hubbs
2016-01-12 20:12 William Hubbs
2015-12-06  0:14 William Hubbs
2015-12-02 21:46 William Hubbs
2015-10-13 13:36 William Hubbs
2015-10-05 15:27 William Hubbs
2015-10-04 20:37 William Hubbs
2015-09-18 17:45 William Hubbs
2015-08-02 14:12 William Hubbs
2015-07-23 17:48 William Hubbs
2015-04-24 18:13 William Hubbs
2015-03-25 13:36 William Hubbs
2015-03-25  6:10 William Hubbs
2015-03-25  4:38 William Hubbs
2015-03-24 20:53 William Hubbs
2015-03-24 20:40 William Hubbs
2015-02-19 21:16 William Hubbs
2015-02-15 22:15 William Hubbs
2015-02-15 22:10 William Hubbs
2015-01-22 18:54 William Hubbs
2014-09-20 21:52 William Hubbs
2014-08-22 19:10 William Hubbs
2014-07-25 16:06 ` William Hubbs
2014-08-22 19:10 William Hubbs
2014-08-11 18:18 ` William Hubbs
2014-07-28 15:51 William Hubbs
2014-08-22 19:10 ` William Hubbs
2014-07-19 18:06 William Hubbs
2014-07-18  4:47 William Hubbs
2014-07-16 23:01 William Hubbs
2014-07-16 23:01 William Hubbs
2014-07-16 19:48 William Hubbs
2014-07-16 19:17 William Hubbs
2014-07-16 18:14 William Hubbs
2014-07-13 19:16 William Hubbs
2014-07-11 20:24 William Hubbs
2014-07-11 20:19 William Hubbs
2014-01-18 20:02 William Hubbs
2013-12-13 18:20 William Hubbs
2013-12-13 18:05 William Hubbs
2013-12-01 17:25 William Hubbs
2013-10-12 14:50 William Hubbs
2013-10-06 17:36 William Hubbs
2013-10-04 16:23 William Hubbs
2013-09-24  6:52 William Hubbs
2013-09-23 22:59 William Hubbs
2013-09-23 18:38 William Hubbs
2013-09-23 18:38 William Hubbs
2013-09-04  0:46 William Hubbs
2013-08-26 22:26 William Hubbs
2013-08-26 20:59 William Hubbs
2013-07-23 23:01 William Hubbs
2013-07-16 18:56 William Hubbs
2013-05-01 23:24 William Hubbs
2013-02-25 20:50 William Hubbs
2013-02-25 20:50 William Hubbs
2013-02-25 20:50 William Hubbs
2013-02-25 20:50 William Hubbs
2013-02-15 19:35 William Hubbs
2012-10-27 18:12 William Hubbs
2012-10-24 17:38 William Hubbs
2012-10-17 23:48 William Hubbs
2012-10-09 23:56 William Hubbs
2012-10-09 21:50 William Hubbs
2012-09-14 21:00 Christian Ruppert
2012-09-12 19:36 Christian Ruppert
2012-09-12 19:00 Christian Ruppert
2012-08-18 22:26 Christian Ruppert
2012-08-18 21:26 Christian Ruppert
2012-05-23 23:31 Mike Frysinger
2012-05-16 22:16 Mike Frysinger
2012-05-06  5:16 Mike Frysinger
2012-04-24  3:32 Christian Ruppert
2012-02-24  2:38 William Hubbs
2012-02-21  2:09 William Hubbs
2012-02-21  1:08 Jory Pratt
2012-02-12  1:23 Christian Ruppert
2012-02-10 23:25 Christian Ruppert
2012-01-31 22:59 William Hubbs
2012-01-28 18:05 Christian Ruppert
2012-01-28 15:45 Christian Ruppert
2012-01-24 18:41 Christian Ruppert
2012-01-23 10:27 Robin H. Johnson
2012-01-23 10:27 Robin H. Johnson
2012-01-23  5:27 Robin H. Johnson
2012-01-22 20:46 William Hubbs
2011-12-31  2:36 Christian Ruppert
2011-12-30 15:03 Christian Ruppert
2011-12-30 15:03 Christian Ruppert
2011-12-29 12:59 Christian Ruppert
2011-12-29  2:18 Christian Ruppert
2011-12-29  2:10 Christian Ruppert
2011-12-29  1:50 Christian Ruppert
2011-12-11 20:43 William Hubbs
2011-11-19  8:11 Mike Frysinger
2011-11-19  8:11 Mike Frysinger
2011-11-19  8:11 Mike Frysinger
2011-11-17 22:10 William Hubbs
2011-11-15 21:26 William Hubbs
2011-11-09  5:10 Mike Frysinger
2011-09-08 17:22 Christian Ruppert
2011-09-08 17:22 Christian Ruppert
2011-09-02 16:47 William Hubbs
2011-09-01 22:14 William Hubbs
2011-07-26 21:59 William Hubbs
2011-07-20 19:40 William Hubbs
2011-07-05 22:52 Christian Ruppert
2011-07-04 22:54 Christian Ruppert
2011-06-30 18:21 Christian Ruppert
2011-06-27 21:21 Christian Ruppert
2011-06-05 14:52 Christian Ruppert
2011-05-28 16:12 Mike Frysinger
2011-05-28 15:42 Mike Frysinger
2011-05-23 19:25 William Hubbs
2011-02-16 15:02 William Hubbs
2011-02-15  0:50 William Hubbs
2011-02-07  8:30 William Hubbs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1503592458.d7938f54f29193251e083ad35a7d464949829096.williamh@OpenRC \
    --to=williamh@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox