From: "William Hubbs" <williamh@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/openrc:master commit in: src/rc/
Date: Fri, 19 May 2017 23:30:00 +0000 (UTC) [thread overview]
Message-ID: <1495235619.49b8a573a195f4b2cee992cd10678694da0a6f4f.williamh@OpenRC> (raw)
commit: 49b8a573a195f4b2cee992cd10678694da0a6f4f
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Fri May 19 18:37:30 2017 +0000
Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Fri May 19 23:13:39 2017 +0000
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=49b8a573
add kill_all helper
This is similar to the sysvinit killall5 utility. It should only be used
in service scripts, so it will not be installed in the path.
This closes #129.
src/rc/.gitignore | 1 +
src/rc/Makefile | 6 +-
src/rc/kill_all.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 256 insertions(+), 1 deletion(-)
diff --git a/src/rc/.gitignore b/src/rc/.gitignore
index c3e7b3f6..91d57075 100644
--- a/src/rc/.gitignore
+++ b/src/rc/.gitignore
@@ -62,3 +62,4 @@ openrc
openrc-init
openrc-run
openrc-shutdown
+kill_all
diff --git a/src/rc/Makefile b/src/rc/Makefile
index 2bc03f76..5874ed17 100644
--- a/src/rc/Makefile
+++ b/src/rc/Makefile
@@ -14,7 +14,7 @@ SRCS+= rc-selinux.c
endif
ifeq (${OS},Linux)
-SRCS+= openrc-init.c openrc-shutdown.c
+SRCS+= kill_all.c openrc-init.c openrc-shutdown.c
endif
CLEANFILES= version.h rc-selinux.o
@@ -44,6 +44,7 @@ RC_SBINPROGS= mark_service_starting mark_service_started \
rc-abort swclock
ifeq (${OS},Linux)
+RC_BINPROGS+= kill_all
SBINPROGS+= openrc-init openrc-shutdown
endif
@@ -99,6 +100,9 @@ checkpath: rc-selinux.o
endif
${CC} ${LOCAL_CFLAGS} ${LOCAL_LDFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${LDADD}
+kill_all: kill_all.o _usage.o
+ ${CC} ${LOCAL_CFLAGS} ${LOCAL_LDFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${LDADD}
+
einfon einfo ewarnn ewarn eerrorn eerror ebegin eend ewend \
eindent eoutdent esyslog eval_ecolors ewaitfile \
veinfo vewarn vebegin veend vewend veindent veoutdent: do_e.o rc-misc.o
diff --git a/src/rc/kill_all.c b/src/rc/kill_all.c
new file mode 100644
index 00000000..a4b15c87
--- /dev/null
+++ b/src/rc/kill_all.c
@@ -0,0 +1,250 @@
+/*
+ * kill_all.c
+ * Sends a signal to all processes on the system.
+ */
+
+/*
+ * 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.
+ */
+
+
+#include <dirent.h>
+#include <errno.h>
+#include <getopt.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include "einfo.h"
+#include "rc.h"
+#include "rc-misc.h"
+#include "_usage.h"
+
+const char *applet = NULL;
+const char *extraopts = "[signal number]";
+const char *getoptstring = "do:" getoptstring_COMMON;
+const struct option longopts[] = {
+ { "dry-run", 0, NULL, 'd' },
+ { "omit", 1, NULL, 'o' },
+ longopts_COMMON
+};
+const char * const longopts_help[] = {
+ "print what would be done",
+ "omit this pid (can be repeated)",
+ longopts_help_COMMON
+};
+const char *usagestring = NULL;
+
+static int mount_proc(void)
+{
+ pid_t pid;
+ pid_t rc;
+ int status;
+
+ if (exists("/proc/version"))
+ return 0;
+ pid = fork();
+ switch(pid) {
+ case -1:
+ syslog(LOG_ERR, "Unable to fork");
+ return -1;
+ break;
+ case 0:
+ /* attempt to mount /proc */
+ execl("mount", "mount", "-t", "proc", "proc", "/proc", NULL);
+ syslog(LOG_ERR, "Unable to execute mount");
+ exit(1);
+ break;
+ default:
+ /* wait for child process */
+ while ((rc = wait(&status)) != pid)
+ if (rc < 0 && errno == ECHILD)
+ break;
+ if (rc != pid || WEXITSTATUS(status) != 0)
+ syslog(LOG_ERR, "mount returned non-zero exit status");
+ break;
+ }
+ if (! exists("/proc/version")) {
+ syslog(LOG_ERR, "Could not mount /proc");
+ return -1;
+ }
+ return 0;
+}
+
+static bool is_user_process(pid_t pid)
+{
+ char buf[PATH_MAX+1];
+ FILE *fp;
+ char path[PATH_MAX+1];
+ pid_t temp_pid;
+ bool user_process = true;
+
+ while (pid >0 && user_process) {
+ if (pid == 2) {
+ user_process = false;
+ continue;
+ }
+ snprintf(path, sizeof(path), "/proc/%d/status", pid);
+ fp = fopen(path, "r");
+ /*
+ * if we could not open the file, the process disappeared, which
+ * leaves us no way to determine for sure whether it was a user
+ * process or kernel thread, so we say it is a kernel thread to
+ * avoid accidentally killing it.
+ */
+ if (!fp) {
+ user_process = false;
+ continue;
+ }
+ temp_pid = -1;
+ while (! feof(fp)) {
+ buf[0] = 0;
+ if (fgets(buf, sizeof(buf), fp))
+ sscanf(buf, "PPid: %d", &temp_pid);
+ else
+ break;
+ }
+ fclose(fp);
+ if (temp_pid == -1) {
+ syslog(LOG_ERR, "Unable to read pid from /proc/%d/status", pid);
+ user_process = false;
+ continue;
+ }
+ pid = temp_pid;
+ }
+ return user_process;
+}
+
+static int signal_processes(int sig, RC_STRINGLIST *omits, bool dryrun)
+{
+ sigset_t signals;
+ sigset_t oldsigs;
+ DIR *dir;
+ struct dirent *d;
+ char buf[PATH_MAX+1];
+ pid_t pid;
+ int sendcount = 0;
+
+ kill(-1, SIGSTOP);
+ sigfillset(&signals);
+ sigemptyset(&oldsigs);
+ sigprocmask(SIG_SETMASK, &signals, &oldsigs);
+ /*
+ * Open the /proc directory.
+ * CWD must be /proc to avoid problems if / is affected by the killing
+ * (i.e. depends on fuse).
+ */
+ if (chdir("/proc") == -1) {
+ syslog(LOG_ERR, "chdir /proc failed");
+ sigprocmask(SIG_SETMASK, &oldsigs, NULL);
+ kill(-1, SIGCONT);
+ return -1;
+ }
+ dir = opendir(".");
+ if (!dir) {
+ syslog(LOG_ERR, "cannot opendir(/proc)");
+ sigprocmask(SIG_SETMASK, &oldsigs, NULL);
+ kill(-1, SIGCONT);
+ return -1;
+ }
+
+ /* Walk through the directory. */
+ while ((d = readdir(dir)) != NULL) {
+ /* Is this a process? */
+ pid = (pid_t) atoi(d->d_name);
+ if (pid == 0)
+ continue;
+
+ /* Is this a process we have been requested to omit? */
+ sprintf(buf, "%d", pid);
+ if (rc_stringlist_find(omits, buf))
+ continue;
+
+ /* Is this process in our session? */
+ if (getsid(getpid()) == getsid(pid))
+ continue;
+
+ /* Is this a kernel thread? */
+ if (!is_user_process(pid))
+ continue;
+
+ if (dryrun)
+ einfo("Would send signal %d to process %d", sig, pid);
+ else if (kill(pid, sig) == 0)
+ sendcount++;
+ }
+ closedir(dir);
+ sigprocmask(SIG_SETMASK, &oldsigs, NULL);
+ kill(-1, SIGCONT);
+ return sendcount;
+}
+
+int main(int argc, char **argv)
+{
+ char *arg = NULL;
+ int opt;
+ bool dryrun = false;
+ RC_STRINGLIST *omits = rc_stringlist_new();
+ int sig = SIGKILL;
+ char *here;
+ char *token;
+
+ /* Ensure that we are only quiet when explicitly told to be */
+ unsetenv("EINFO_QUIET");
+
+ applet = basename_c(argv[0]);
+ rc_stringlist_addu(omits, "1");
+ while ((opt = getopt_long(argc, argv, getoptstring,
+ longopts, (int *) 0)) != -1)
+ {
+ switch (opt) {
+ case 'd':
+ dryrun = true;
+ break;
+ case 'o':
+ here = optarg;
+ while ((token = strsep(&here, ",;:"))) {
+ if ((pid_t) atoi(token) > 0)
+ rc_stringlist_addu(omits, token);
+ else {
+ eerror("Invalid omit pid value %s", token);
+ usage(EXIT_FAILURE);
+ }
+ }
+ break;
+ case_RC_COMMON_GETOPT
+ }
+ }
+
+ if (argc > optind) {
+ arg = argv[optind];
+ sig = atoi(arg);
+ if (sig <= 0 || sig > 31) {
+ rc_stringlist_free(omits);
+ eerror("Invalid signal %s", arg);
+ usage(EXIT_FAILURE);
+ }
+ }
+
+ openlog(applet, LOG_CONS|LOG_PID, LOG_DAEMON);
+ if (mount_proc() != 0) {
+ rc_stringlist_free(omits);
+ eerrorx("Unable to mount /proc file system");
+ }
+ signal_processes(sig, omits, dryrun);
+ rc_stringlist_free(omits);
+ return 0;
+}
next reply other threads:[~2017-05-19 23:30 UTC|newest]
Thread overview: 257+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-19 23:30 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-08-24 16:45 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-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=1495235619.49b8a573a195f4b2cee992cd10678694da0a6f4f.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