* [PATCH 1/7] kill: add long options
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
@ 2013-03-07 23:27 ` Sami Kerola
2013-03-07 23:27 ` [PATCH 2/7] kill: align with howto-usage-function.txt Sami Kerola
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Sami Kerola @ 2013-03-07 23:27 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/kill.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index 10730a3..e43f23a 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -193,11 +193,11 @@ int main (int argc, char *argv[])
printf(UTIL_LINUX_VERSION);
return 0;
}
- if (! strcmp (arg, "-a")) {
+ if (! strcmp (arg, "-a") || ! strcmp (arg, "--all")) {
check_all++;
continue;
}
- if (! strcmp (arg, "-l")) {
+ if (! strcmp (arg, "-l") || ! strcmp (arg, "--list")) {
if (argc < 2) {
printsignals (stdout);
return 0;
@@ -214,13 +214,13 @@ int main (int argc, char *argv[])
printsig (numsig);
return 0;
}
- if (! strcmp (arg, "-p")) {
+ if (! strcmp (arg, "-p") || ! strcmp (arg, "--pid")) {
do_pid++;
if (do_kill)
return usage (1);
continue;
}
- if (! strcmp (arg, "-s")) {
+ if (! strcmp (arg, "-s") || ! strcmp (arg, "--signal")) {
if (argc < 2) {
return usage (1);
}
@@ -235,7 +235,7 @@ int main (int argc, char *argv[])
}
continue;
}
- if (! strcmp (arg, "-q")) {
+ if (! strcmp (arg, "-q") || ! strcmp (arg, "--queue")) {
if (argc < 2)
return usage (1);
argc--, argv++;
--
1.8.1.5
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 2/7] kill: align with howto-usage-function.txt
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
2013-03-07 23:27 ` [PATCH 1/7] kill: add long options Sami Kerola
@ 2013-03-07 23:27 ` Sami Kerola
2013-03-07 23:27 ` [PATCH 3/7] kill: use libc error printing facilities and exit values Sami Kerola
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Sami Kerola @ 2013-03-07 23:27 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/kill.c | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index e43f23a..2b6256c 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -405,14 +405,25 @@ static void printsignals (FILE *fp)
fputc ('\n', fp);
}
-static int usage (int status)
+static int usage(int status)
{
- FILE *fp;
-
- fp = (status == 0 ? stdout : stderr);
- fprintf (fp, _("usage: %s [ -s signal | -p ] [ -a ] pid ...\n"), progname);
- fprintf (fp, _(" %s -l [ signal ]\n"), progname);
- return status;
+ FILE *out;
+ out = (status == 0 ? stdout : stderr);
+
+ fputs(USAGE_HEADER, out);
+ fprintf(out, _(" %s [options] <pid | commandname> [...]\n"), program_invocation_short_name);
+ fputs(USAGE_OPTIONS, out);
+ fputs(_(" -a, --all all commands including not same uid as parent process\n"), out);
+ fputs(_(" -s, --signal <sig> send specified signal\n"), out);
+ fputs(_(" -q, --queue <sig> use sigqueue(2) rather than kill(2)\n"), out);
+ fputs(_(" -p, --pid print pids without signaling them\n"), out);
+ fputs(_(" -l, --list <name> list signal names\n"), out);
+ fputs(USAGE_SEPARATOR, out);
+ fputs(USAGE_HELP, out);
+ fputs(USAGE_VERSION, out);
+ fprintf(out, USAGE_MAN_TAIL("kill(1)"));
+
+ return status;
}
static int kill_verbose (char *procname, int pid, int sig)
--
1.8.1.5
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 3/7] kill: use libc error printing facilities and exit values
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
2013-03-07 23:27 ` [PATCH 1/7] kill: add long options Sami Kerola
2013-03-07 23:27 ` [PATCH 2/7] kill: align with howto-usage-function.txt Sami Kerola
@ 2013-03-07 23:27 ` Sami Kerola
2013-03-07 23:27 ` [PATCH 4/7] kill, procs: fix coding style Sami Kerola
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Sami Kerola @ 2013-03-07 23:27 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/kill.c | 54 +++++++++++++++++++++++-------------------------------
misc-utils/procs.c | 2 +-
2 files changed, 24 insertions(+), 32 deletions(-)
diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index 2b6256c..c9fb76b 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -148,8 +148,6 @@ static void printsignals (FILE *fp);
static int usage (int status);
static int kill_verbose (char *procname, int pid, int sig);
-static char *progname;
-
#ifdef HAVE_SIGQUEUE
static int use_sigval;
static union sigval sigdata;
@@ -158,21 +156,17 @@ static union sigval sigdata;
int main (int argc, char *argv[])
{
int errors, numsig, pid;
- char *ep, *arg, *p;
+ char *ep, *arg;
int do_pid, do_kill, check_all;
int *pids, *ip;
- progname = argv[0];
- if ((p = strrchr(progname, '/')) != NULL)
- progname = p+1;
-
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
atexit(close_stdout);
numsig = SIGTERM;
- do_pid = (! strcmp (progname, "pid")); /* Yecch */
+ do_pid = (! strcmp (program_invocation_short_name, "pid")); /* Yecch */
do_kill = 0;
check_all = 0;
@@ -191,7 +185,7 @@ int main (int argc, char *argv[])
if (! strcmp (arg, "-v") || ! strcmp (arg, "-V") ||
! strcmp (arg, "--version")) {
printf(UTIL_LINUX_VERSION);
- return 0;
+ return EXIT_SUCCESS;
}
if (! strcmp (arg, "-a") || ! strcmp (arg, "--all")) {
check_all++;
@@ -200,44 +194,42 @@ int main (int argc, char *argv[])
if (! strcmp (arg, "-l") || ! strcmp (arg, "--list")) {
if (argc < 2) {
printsignals (stdout);
- return 0;
+ return EXIT_SUCCESS;
}
if (argc > 2) {
- return usage (1);
+ return usage (EXIT_FAILURE);
}
/* argc == 2, accept "kill -l $?" */
arg = argv[1];
- if ((numsig = arg_to_signum (arg, 1)) < 0) {
- fprintf (stderr, _("%s: unknown signal %s\n"), progname, arg);
- return 1;
- }
+ if ((numsig = arg_to_signum (arg, 1)) < 0)
+ errx(EXIT_FAILURE, _("unknown signal: %s"), arg);
printsig (numsig);
- return 0;
+ return EXIT_SUCCESS;
}
if (! strcmp (arg, "-p") || ! strcmp (arg, "--pid")) {
do_pid++;
if (do_kill)
- return usage (1);
+ return usage (EXIT_FAILURE);
continue;
}
if (! strcmp (arg, "-s") || ! strcmp (arg, "--signal")) {
if (argc < 2) {
- return usage (1);
+ return usage (EXIT_FAILURE);
}
do_kill++;
if (do_pid)
- return usage (1);
+ return usage (EXIT_FAILURE);
argc--, argv++;
arg = *argv;
if ((numsig = arg_to_signum (arg, 0)) < 0) {
nosig (arg);
- return 1;
+ return EXIT_FAILURE;
}
continue;
}
if (! strcmp (arg, "-q") || ! strcmp (arg, "--queue")) {
if (argc < 2)
- return usage (1);
+ return usage (EXIT_FAILURE);
argc--, argv++;
arg = *argv;
#ifdef HAVE_SIGQUEUE
@@ -256,16 +248,16 @@ int main (int argc, char *argv[])
break;
arg++;
if ((numsig = arg_to_signum (arg, 0)) < 0) {
- return usage (1);
+ return usage (EXIT_FAILURE);
}
do_kill++;
if (do_pid)
- return usage (1);
+ return usage (EXIT_FAILURE);
continue;
}
if (! *argv) {
- return usage (1);
+ return usage (EXIT_FAILURE);
}
if (do_pid) {
numsig = -1;
@@ -274,7 +266,7 @@ int main (int argc, char *argv[])
/* we're done with the options.
the rest of the arguments should be process ids and names.
kill them. */
- for (errors = 0; (arg = *argv) != NULL; argv++) {
+ for (errors = EXIT_SUCCESS; (arg = *argv) != NULL; argv++) {
pid = strtol (arg, &ep, 10);
if (! *ep)
errors += kill_verbose (arg, pid, numsig);
@@ -282,8 +274,7 @@ int main (int argc, char *argv[])
pids = get_pids (arg, check_all);
if (! pids) {
errors++;
- fprintf (stderr, _("%s: can't find process \"%s\"\n"),
- progname, arg);
+ warnx (_("cannot find process \"%s\""), arg);
continue;
}
for (ip = pids; *ip >= 0; ip++)
@@ -291,7 +282,9 @@ int main (int argc, char *argv[])
free (pids);
}
}
- return (errors);
+ if (errors != EXIT_SUCCESS)
+ errors = EXIT_FAILURE;
+ return errors;
}
#ifdef SIGRTMIN
@@ -362,7 +355,7 @@ static int arg_to_signum (char *arg, int maskbit)
static void nosig (char *name)
{
- fprintf (stderr, _("%s: unknown signal %s; valid signals:\n"), progname, name);
+ warnx (_("unknown signal %s; valid signals:"), name);
printsignals (stderr);
}
@@ -442,8 +435,7 @@ static int kill_verbose (char *procname, int pid, int sig)
rc = kill (pid, sig);
if (rc < 0) {
- fprintf (stderr, "%s ", progname);
- perror (procname);
+ warn(_("sending signal to %s failed"), procname);
return 1;
}
return 0;
diff --git a/misc-utils/procs.c b/misc-utils/procs.c
index e76c390..f3057fe 100644
--- a/misc-utils/procs.c
+++ b/misc-utils/procs.c
@@ -41,7 +41,7 @@ get_pids (char *process_name, int get_all) {
dir = opendir ("/proc");
if (! dir) {
- perror ("opendir /proc");
+ warn ("opendir /proc");
return NULL;
}
uid = getuid ();
--
1.8.1.5
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 4/7] kill, procs: fix coding style
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
` (2 preceding siblings ...)
2013-03-07 23:27 ` [PATCH 3/7] kill: use libc error printing facilities and exit values Sami Kerola
@ 2013-03-07 23:27 ` Sami Kerola
2013-03-07 23:27 ` [PATCH 5/7] kill, procs: move function declarations to kill.h Sami Kerola
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Sami Kerola @ 2013-03-07 23:27 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/kill.c | 405 ++++++++++++++++++++++++++---------------------------
misc-utils/procs.c | 172 ++++++++++++-----------
2 files changed, 290 insertions(+), 287 deletions(-)
diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index c9fb76b..5e349c8 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -141,150 +141,147 @@ struct signv {
extern char *mybasename(char *);
-static int arg_to_signum (char *arg, int mask);
-static void nosig (char *name);
-static void printsig (int sig);
-static void printsignals (FILE *fp);
-static int usage (int status);
-static int kill_verbose (char *procname, int pid, int sig);
+static int arg_to_signum(char *arg, int mask);
+static void nosig(char *name);
+static void printsig(int sig);
+static void printsignals(FILE *fp);
+static int usage(int status);
+static int kill_verbose(char *procname, int pid, int sig);
#ifdef HAVE_SIGQUEUE
static int use_sigval;
static union sigval sigdata;
#endif
-int main (int argc, char *argv[])
+int main(int argc, char **argv)
{
- int errors, numsig, pid;
- char *ep, *arg;
- int do_pid, do_kill, check_all;
- int *pids, *ip;
-
- setlocale(LC_ALL, "");
- bindtextdomain(PACKAGE, LOCALEDIR);
- textdomain(PACKAGE);
- atexit(close_stdout);
-
- numsig = SIGTERM;
- do_pid = (! strcmp (program_invocation_short_name, "pid")); /* Yecch */
- do_kill = 0;
- check_all = 0;
-
- /* loop through the arguments.
- actually, -a is the only option can be used with other options.
- `kill' is basically a one-option-at-most program. */
- for (argc--, argv++; argc > 0; argc--, argv++) {
- arg = *argv;
- if (*arg != '-') {
- break;
- }
- if (! strcmp (arg, "--")) {
- argc--, argv++;
- break;
- }
- if (! strcmp (arg, "-v") || ! strcmp (arg, "-V") ||
- ! strcmp (arg, "--version")) {
- printf(UTIL_LINUX_VERSION);
- return EXIT_SUCCESS;
- }
- if (! strcmp (arg, "-a") || ! strcmp (arg, "--all")) {
- check_all++;
- continue;
- }
- if (! strcmp (arg, "-l") || ! strcmp (arg, "--list")) {
- if (argc < 2) {
- printsignals (stdout);
- return EXIT_SUCCESS;
- }
- if (argc > 2) {
- return usage (EXIT_FAILURE);
- }
- /* argc == 2, accept "kill -l $?" */
- arg = argv[1];
- if ((numsig = arg_to_signum (arg, 1)) < 0)
- errx(EXIT_FAILURE, _("unknown signal: %s"), arg);
- printsig (numsig);
- return EXIT_SUCCESS;
- }
- if (! strcmp (arg, "-p") || ! strcmp (arg, "--pid")) {
- do_pid++;
- if (do_kill)
- return usage (EXIT_FAILURE);
- continue;
- }
- if (! strcmp (arg, "-s") || ! strcmp (arg, "--signal")) {
- if (argc < 2) {
- return usage (EXIT_FAILURE);
- }
- do_kill++;
- if (do_pid)
- return usage (EXIT_FAILURE);
- argc--, argv++;
- arg = *argv;
- if ((numsig = arg_to_signum (arg, 0)) < 0) {
- nosig (arg);
- return EXIT_FAILURE;
- }
- continue;
- }
- if (! strcmp (arg, "-q") || ! strcmp (arg, "--queue")) {
- if (argc < 2)
- return usage (EXIT_FAILURE);
- argc--, argv++;
- arg = *argv;
+ int errors, numsig, pid;
+ char *ep, *arg;
+ int do_pid, do_kill, check_all;
+ int *pids, *ip;
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+ atexit(close_stdout);
+
+ numsig = SIGTERM;
+ do_pid = (!strcmp(program_invocation_short_name, "pid")); /* Yecch */
+ do_kill = 0;
+ check_all = 0;
+
+ /* Loop through the arguments. Actually, -a is the only option
+ * can be used with other options. 'kill' is basically a
+ * one-option-at-most program. */
+ for (argc--, argv++; argc > 0; argc--, argv++) {
+ arg = *argv;
+ if (*arg != '-')
+ break;
+ if (!strcmp(arg, "--")) {
+ argc--, argv++;
+ break;
+ }
+ if (!strcmp(arg, "-v") || !strcmp(arg, "-V") ||
+ !strcmp(arg, "--version")) {
+ printf(UTIL_LINUX_VERSION);
+ return EXIT_SUCCESS;
+ }
+ if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
+ check_all++;
+ continue;
+ }
+ if (!strcmp(arg, "-l") || !strcmp(arg, "--list")) {
+ if (argc < 2) {
+ printsignals(stdout);
+ return EXIT_SUCCESS;
+ }
+ if (argc > 2) {
+ return usage(EXIT_FAILURE);
+ }
+ /* argc == 2, accept "kill -l $?" */
+ arg = argv[1];
+ if ((numsig = arg_to_signum(arg, 1)) < 0)
+ errx(EXIT_FAILURE, _("unknown signal: %s"),
+ arg);
+ printsig(numsig);
+ return EXIT_SUCCESS;
+ }
+ if (!strcmp(arg, "-p") || !strcmp(arg, "--pid")) {
+ do_pid++;
+ if (do_kill)
+ return usage(EXIT_FAILURE);
+ continue;
+ }
+ if (!strcmp(arg, "-s") || !strcmp(arg, "--signal")) {
+ if (argc < 2)
+ return usage(EXIT_FAILURE);
+ do_kill++;
+ if (do_pid)
+ return usage(EXIT_FAILURE);
+ argc--, argv++;
+ arg = *argv;
+ if ((numsig = arg_to_signum(arg, 0)) < 0) {
+ nosig(arg);
+ return EXIT_FAILURE;
+ }
+ continue;
+ }
+ if (!strcmp(arg, "-q") || !strcmp(arg, "--queue")) {
+ if (argc < 2)
+ return usage(EXIT_FAILURE);
+ argc--, argv++;
+ arg = *argv;
#ifdef HAVE_SIGQUEUE
- sigdata.sival_int = strtos32_or_err(arg, _("invalid sigval argument"));
- use_sigval = 1;
+ sigdata.sival_int =
+ strtos32_or_err(arg, _("invalid sigval argument"));
+ use_sigval = 1;
#endif
- continue;
- }
- /* `arg' begins with a dash but is not a known option.
- so it's probably something like -HUP, or -1/-n
- try to deal with it.
- -n could be signal n, or pid -n (i.e. process group n).
- In case of doubt POSIX tells us to assume a signal.
- If a signal has been parsed, assume it's a pid, break */
- if (do_kill)
- break;
- arg++;
- if ((numsig = arg_to_signum (arg, 0)) < 0) {
- return usage (EXIT_FAILURE);
+ continue;
+ }
+ /* 'arg' begins with a dash but is not a known option.
+ * So it is probably something like -HUP, or -1/-n try to
+ * deal with it. -n could be signal n, or pid -n (i.e.,
+ * process group n). In case of doubt POSIX tells us to
+ * assume a signal. If a signal has been parsed, assume
+ * it i+
+ s a pid, break. */
+ if (do_kill)
+ break;
+ arg++;
+ if ((numsig = arg_to_signum(arg, 0)) < 0)
+ return usage(EXIT_FAILURE);
+ do_kill++;
+ if (do_pid)
+ return usage(EXIT_FAILURE);
+ continue;
}
- do_kill++;
+
+ if (!*argv)
+ return usage(EXIT_FAILURE);
if (do_pid)
- return usage (EXIT_FAILURE);
- continue;
- }
-
- if (! *argv) {
- return usage (EXIT_FAILURE);
- }
- if (do_pid) {
- numsig = -1;
- }
-
- /* we're done with the options.
- the rest of the arguments should be process ids and names.
- kill them. */
- for (errors = EXIT_SUCCESS; (arg = *argv) != NULL; argv++) {
- pid = strtol (arg, &ep, 10);
- if (! *ep)
- errors += kill_verbose (arg, pid, numsig);
- else {
- pids = get_pids (arg, check_all);
- if (! pids) {
- errors++;
- warnx (_("cannot find process \"%s\""), arg);
- continue;
- }
- for (ip = pids; *ip >= 0; ip++)
- errors += kill_verbose (arg, *ip, numsig);
- free (pids);
+ numsig = -1;
+
+ /* We are done with the options. The rest of the arguments
+ * should be process ids and names, and kill them. */
+ for (errors = EXIT_SUCCESS; (arg = *argv) != NULL; argv++) {
+ pid = strtol(arg, &ep, 10);
+ if (!*ep)
+ errors += kill_verbose(arg, pid, numsig);
+ else {
+ pids = get_pids(arg, check_all);
+ if (!pids) {
+ errors++;
+ warnx(_("cannot find process \"%s\""), arg);
+ continue;
+ }
+ for (ip = pids; *ip >= 0; ip++)
+ errors += kill_verbose(arg, *ip, numsig);
+ free(pids);
+ }
}
- }
- if (errors != EXIT_SUCCESS)
- errors = EXIT_FAILURE;
- return errors;
+ if (errors != EXIT_SUCCESS)
+ errors = EXIT_FAILURE;
+ return errors;
}
#ifdef SIGRTMIN
@@ -310,92 +307,92 @@ static int rtsig_to_signum(char *sig)
num = maxi ? SIGRTMAX - num : SIGRTMIN + num;
- if (num < SIGRTMIN || num > SIGRTMAX)
+ if (num < SIGRTMIN || SIGRTMAX < num)
return -1;
return num;
}
#endif
-static int signame_to_signum (char *sig)
+static int signame_to_signum(char *sig)
{
- size_t n;
+ size_t n;
- if (! strncasecmp (sig, "sig", 3))
- sig += 3;
+ if (!strncasecmp(sig, "sig", 3))
+ sig += 3;
#ifdef SIGRTMIN
- /* RT signals */
- if (!strncasecmp(sig, "rt", 2))
- return rtsig_to_signum(sig + 2);
+ /* RT signals */
+ if (!strncasecmp(sig, "rt", 2))
+ return rtsig_to_signum(sig + 2);
#endif
- /* Normal sugnals */
- for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
- if (! strcasecmp (sys_signame[n].name, sig))
- return sys_signame[n].val;
- }
- return (-1);
+ /* Normal sugnals */
+ for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+ if (!strcasecmp(sys_signame[n].name, sig))
+ return sys_signame[n].val;
+ }
+ return -1;
}
-static int arg_to_signum (char *arg, int maskbit)
+static int arg_to_signum(char *arg, int maskbit)
{
- int numsig;
- char *ep;
-
- if (isdigit (*arg)) {
- numsig = strtol (arg, &ep, 10);
- if (numsig >= NSIG && maskbit && (numsig & 128) != 0)
- numsig -= 128;
- if (*ep != 0 || numsig < 0 || numsig >= NSIG)
- return (-1);
- return (numsig);
- }
- return signame_to_signum (arg);
+ int numsig;
+ char *ep;
+
+ if (isdigit(*arg)) {
+ numsig = strtol(arg, &ep, 10);
+ if (NSIG <= numsig && maskbit && (numsig & 128) != 0)
+ numsig -= 128;
+ if (*ep != 0 || numsig < 0 || NSIG <= numsig)
+ return -1;
+ return numsig;
+ }
+ return signame_to_signum(arg);
}
-static void nosig (char *name)
+static void nosig(char *name)
{
- warnx (_("unknown signal %s; valid signals:"), name);
- printsignals (stderr);
+ warnx(_("unknown signal %s; valid signals:"), name);
+ printsignals(stderr);
}
-static void printsig (int sig)
+static void printsig(int sig)
{
- size_t n;
+ size_t n;
- for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
- if (sys_signame[n].val == sig) {
- printf ("%s\n", sys_signame[n].name);
- return;
+ for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+ if (sys_signame[n].val == sig) {
+ printf("%s\n", sys_signame[n].name);
+ return;
+ }
}
- }
#ifdef SIGRTMIN
- if (sig >= SIGRTMIN && sig <= SIGRTMAX) {
- printf ("RT%d\n", sig - SIGRTMIN);
- return;
- }
+ if (SIGRTMIN <= sig && sig <= SIGRTMAX) {
+ printf("RT%d\n", sig - SIGRTMIN);
+ return;
+ }
#endif
- printf("%d\n", sig);
+ printf("%d\n", sig);
}
-static void printsignals (FILE *fp)
+static void printsignals(FILE *fp)
{
- size_t n, lth, lpos = 0;
-
- for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
- lth = 1+strlen(sys_signame[n].name);
- if (lpos+lth > 72) {
- fputc ('\n', fp);
- lpos = 0;
- } else if (lpos)
- fputc (' ', fp);
- lpos += lth;
- fputs (sys_signame[n].name, fp);
- }
+ size_t n, lth, lpos = 0;
+
+ for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+ lth = 1 + strlen(sys_signame[n].name);
+ if (lpos + lth > 72) {
+ fputc('\n', fp);
+ lpos = 0;
+ } else if (lpos)
+ fputc(' ', fp);
+ lpos += lth;
+ fputs(sys_signame[n].name, fp);
+ }
#ifdef SIGRTMIN
- fputs (" RT<N> RTMIN+<N> RTMAX-<N>", fp);
+ fputs(" RT<N> RTMIN+<N> RTMAX-<N>", fp);
#endif
- fputc ('\n', fp);
+ fputc('\n', fp);
}
static int usage(int status)
@@ -419,24 +416,24 @@ static int usage(int status)
return status;
}
-static int kill_verbose (char *procname, int pid, int sig)
+static int kill_verbose(char *procname, int pid, int sig)
{
- int rc;
+ int rc;
- if (sig < 0) {
- printf ("%d\n", pid);
- return 0;
- }
+ if (sig < 0) {
+ printf("%d\n", pid);
+ return 0;
+ }
#ifdef HAVE_SIGQUEUE
- if (use_sigval)
- rc = sigqueue(pid, sig, sigdata);
- else
+ if (use_sigval)
+ rc = sigqueue(pid, sig, sigdata);
+ else
#endif
- rc = kill (pid, sig);
+ rc = kill(pid, sig);
- if (rc < 0) {
- warn(_("sending signal to %s failed"), procname);
- return 1;
- }
- return 0;
+ if (rc < 0) {
+ warn(_("sending signal to %s failed"), procname);
+ return 1;
+ }
+ return 0;
}
diff --git a/misc-utils/procs.c b/misc-utils/procs.c
index f3057fe..071a63a 100644
--- a/misc-utils/procs.c
+++ b/misc-utils/procs.c
@@ -12,8 +12,6 @@
*
*/
-#define _POSIX_SOURCE 1
-
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
@@ -25,102 +23,110 @@
#include "kill.h"
#include "xalloc.h"
-extern char *mybasename (char *);
-static char *parse_parens (char *buf);
-
-int *
-get_pids (char *process_name, int get_all) {
- DIR *dir;
- struct dirent *ent;
- int status;
- char *dname, fname[100], *cp, buf[256];
- struct stat st;
- uid_t uid;
- FILE *fp;
- int pid, *pids, num_pids, pids_size;
+extern char *mybasename(char *);
+static char *parse_parens(char *buf);
- dir = opendir ("/proc");
- if (! dir) {
- warn ("opendir /proc");
- return NULL;
- }
- uid = getuid ();
- pids = NULL;
- num_pids = pids_size = 0;
+int *get_pids(char *process_name, int get_all)
+{
+ DIR *dir;
+ struct dirent *ent;
+ int status;
+ char *dname, fname[100], *cp, buf[256];
+ struct stat st;
+ uid_t uid;
+ FILE *fp;
+ int pid, *pids, num_pids, pids_size;
- while ((ent = readdir (dir)) != NULL) {
- dname = ent->d_name;
- if (! isdigit (*dname)) continue;
- pid = atoi (dname);
- sprintf (fname, "/proc/%d/cmdline", pid);
- /* get the process owner */
- status = stat (fname, &st);
- if (status != 0) continue;
- if (! get_all && uid != st.st_uid) continue;
- /* get the command line */
- fp = fopen (fname, "r");
- if (! fp) continue;
- cp = fgets (buf, sizeof (buf), fp);
- fclose (fp);
- /* an empty command line means the process is swapped out */
- if (! cp || ! *cp) {
- /* get the process name from the statfile */
- sprintf (fname, "/proc/%d/stat", pid);
- fp = fopen (fname, "r");
- if (! fp) continue;
- cp = fgets (buf, sizeof (buf), fp);
- fclose (fp);
- if (cp == NULL) continue;
- cp = parse_parens (buf);
- if (cp == NULL) continue;
+ dir = opendir("/proc");
+ if (!dir) {
+ warn("opendir /proc");
+ return NULL;
}
- /* ok, we got the process name. */
- if (strcmp (process_name, mybasename (cp))) continue;
- while (pids_size < num_pids + 2) {
- pids_size += 5;
- pids = (int *) xrealloc (pids, sizeof (int) * pids_size);
- }
- if (pids) {
- pids[num_pids++] = pid;
- pids[num_pids] = -1;
+ uid = getuid();
+ pids = NULL;
+ num_pids = pids_size = 0;
+
+ while ((ent = readdir(dir)) != NULL) {
+ dname = ent->d_name;
+ if (!isdigit(*dname))
+ continue;
+ pid = atoi(dname);
+ sprintf(fname, "/proc/%d/cmdline", pid);
+ /* get the process owner */
+ status = stat(fname, &st);
+ if (status != 0)
+ continue;
+ if (!get_all && uid != st.st_uid)
+ continue;
+ /* get the command line */
+ fp = fopen(fname, "r");
+ if (!fp)
+ continue;
+ cp = fgets(buf, sizeof(buf), fp);
+ fclose(fp);
+ /* an empty command line means the process is swapped out */
+ if (!cp || !*cp) {
+ /* get the process name from the statfile */
+ sprintf(fname, "/proc/%d/stat", pid);
+ fp = fopen(fname, "r");
+ if (!fp)
+ continue;
+ cp = fgets(buf, sizeof(buf), fp);
+ fclose(fp);
+ if (cp == NULL)
+ continue;
+ cp = parse_parens(buf);
+ if (cp == NULL)
+ continue;
+ }
+ /* ok, we got the process name. */
+ if (strcmp(process_name, mybasename(cp)))
+ continue;
+ while (pids_size < num_pids + 2) {
+ pids_size += 5;
+ pids = (int *)xrealloc(pids, sizeof(int) * pids_size);
+ }
+ if (pids) {
+ pids[num_pids++] = pid;
+ pids[num_pids] = -1;
+ }
}
- }
- closedir (dir);
- return pids;
+ closedir(dir);
+ return pids;
}
/*
* parse_parens () -- return an index just past the first open paren in
* buf, and terminate the string at the matching close paren.
*/
-static char *parse_parens (char *buf)
+static char *parse_parens(char *buf)
{
- char *cp, *ip;
- int depth;
+ char *cp, *ip;
+ int depth;
- cp = strchr (buf, '(');
- if (cp == NULL) return NULL;
- cp++;
- depth = 1;
- for (ip = cp; *ip; ip++) {
- if (*ip == '(')
- depth++;
- if (*ip == ')') {
- depth--;
- if (depth == 0) {
- *ip = 0;
- break;
- }
+ cp = strchr(buf, '(');
+ if (cp == NULL)
+ return NULL;
+ cp++;
+ depth = 1;
+ for (ip = cp; *ip; ip++) {
+ if (*ip == '(')
+ depth++;
+ if (*ip == ')') {
+ depth--;
+ if (depth == 0) {
+ *ip = 0;
+ break;
+ }
+ }
}
- }
- return cp;
+ return cp;
}
-char *mybasename (char *path)
+char *mybasename(char *path)
{
- char *cp;
+ char *cp;
- cp = strrchr (path, '/');
- return (cp ? cp + 1 : path);
+ cp = strrchr(path, '/');
+ return (cp ? cp + 1 : path);
}
-
--
1.8.1.5
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 5/7] kill, procs: move function declarations to kill.h
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
` (3 preceding siblings ...)
2013-03-07 23:27 ` [PATCH 4/7] kill, procs: fix coding style Sami Kerola
@ 2013-03-07 23:27 ` Sami Kerola
2013-03-07 23:27 ` [PATCH 6/7] kill, procs: use pid_t for pids Sami Kerola
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Sami Kerola @ 2013-03-07 23:27 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/kill.c | 2 --
misc-utils/kill.h | 8 +++++++-
misc-utils/procs.c | 1 -
3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index 5e349c8..b0145cd 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -139,8 +139,6 @@ struct signv {
#endif
};
-extern char *mybasename(char *);
-
static int arg_to_signum(char *arg, int mask);
static void nosig(char *name);
static void printsig(int sig);
diff --git a/misc-utils/kill.h b/misc-utils/kill.h
index 27a12a8..43c6ed7 100644
--- a/misc-utils/kill.h
+++ b/misc-utils/kill.h
@@ -1 +1,7 @@
-extern int *get_pids (char *process_name, int get_all);
+#ifndef UTIL_LINUX_KILL
+#define UTIL_LINUX_KILL
+
+extern int *get_pids(char *process_name, int get_all);
+extern char *mybasename(char *);
+
+#endif
diff --git a/misc-utils/procs.c b/misc-utils/procs.c
index 071a63a..544966e 100644
--- a/misc-utils/procs.c
+++ b/misc-utils/procs.c
@@ -23,7 +23,6 @@
#include "kill.h"
#include "xalloc.h"
-extern char *mybasename(char *);
static char *parse_parens(char *buf);
int *get_pids(char *process_name, int get_all)
--
1.8.1.5
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 6/7] kill, procs: use pid_t for pids
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
` (4 preceding siblings ...)
2013-03-07 23:27 ` [PATCH 5/7] kill, procs: move function declarations to kill.h Sami Kerola
@ 2013-03-07 23:27 ` Sami Kerola
2013-03-07 23:27 ` [PATCH 7/7] docs: add long options and tidy up kill.1 Sami Kerola
2013-03-12 14:29 ` [PATCH 0/7] kill: modernization after undeprecation Karel Zak
7 siblings, 0 replies; 9+ messages in thread
From: Sami Kerola @ 2013-03-07 23:27 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Reference: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/threads.h#n30
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/kill.c | 6 +++---
misc-utils/kill.h | 2 +-
misc-utils/procs.c | 17 ++++++++---------
3 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index b0145cd..742f349 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -156,7 +156,7 @@ int main(int argc, char **argv)
int errors, numsig, pid;
char *ep, *arg;
int do_pid, do_kill, check_all;
- int *pids, *ip;
+ pid_t *pids, *ip;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
@@ -414,12 +414,12 @@ static int usage(int status)
return status;
}
-static int kill_verbose(char *procname, int pid, int sig)
+static int kill_verbose(char *procname, pid_t pid, int sig)
{
int rc;
if (sig < 0) {
- printf("%d\n", pid);
+ printf("%ld\n", (long)pid);
return 0;
}
#ifdef HAVE_SIGQUEUE
diff --git a/misc-utils/kill.h b/misc-utils/kill.h
index 43c6ed7..fb02994 100644
--- a/misc-utils/kill.h
+++ b/misc-utils/kill.h
@@ -1,7 +1,7 @@
#ifndef UTIL_LINUX_KILL
#define UTIL_LINUX_KILL
-extern int *get_pids(char *process_name, int get_all);
+extern pid_t *get_pids(char *process_name, int get_all);
extern char *mybasename(char *);
#endif
diff --git a/misc-utils/procs.c b/misc-utils/procs.c
index 544966e..c4cb06e 100644
--- a/misc-utils/procs.c
+++ b/misc-utils/procs.c
@@ -25,16 +25,16 @@
static char *parse_parens(char *buf);
-int *get_pids(char *process_name, int get_all)
+pid_t *get_pids(char *process_name, int get_all)
{
DIR *dir;
struct dirent *ent;
int status;
- char *dname, fname[100], *cp, buf[256];
+ char fname[100], *cp, buf[256], *end;
struct stat st;
uid_t uid;
FILE *fp;
- int pid, *pids, num_pids, pids_size;
+ pid_t pid, *pids, num_pids, pids_size;
dir = opendir("/proc");
if (!dir) {
@@ -46,11 +46,10 @@ int *get_pids(char *process_name, int get_all)
num_pids = pids_size = 0;
while ((ent = readdir(dir)) != NULL) {
- dname = ent->d_name;
- if (!isdigit(*dname))
+ pid = strtol(ent->d_name, &end, 10);
+ if (errno || ent->d_name == end || (end && *end))
continue;
- pid = atoi(dname);
- sprintf(fname, "/proc/%d/cmdline", pid);
+ sprintf(fname, "/proc/%ld/cmdline", (long)pid);
/* get the process owner */
status = stat(fname, &st);
if (status != 0)
@@ -66,7 +65,7 @@ int *get_pids(char *process_name, int get_all)
/* an empty command line means the process is swapped out */
if (!cp || !*cp) {
/* get the process name from the statfile */
- sprintf(fname, "/proc/%d/stat", pid);
+ sprintf(fname, "/proc/%ld/stat", (long)pid);
fp = fopen(fname, "r");
if (!fp)
continue;
@@ -83,7 +82,7 @@ int *get_pids(char *process_name, int get_all)
continue;
while (pids_size < num_pids + 2) {
pids_size += 5;
- pids = (int *)xrealloc(pids, sizeof(int) * pids_size);
+ pids = xrealloc(pids, sizeof(pid_t) * pids_size);
}
if (pids) {
pids[num_pids++] = pid;
--
1.8.1.5
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 7/7] docs: add long options and tidy up kill.1
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
` (5 preceding siblings ...)
2013-03-07 23:27 ` [PATCH 6/7] kill, procs: use pid_t for pids Sami Kerola
@ 2013-03-07 23:27 ` Sami Kerola
2013-03-12 14:29 ` [PATCH 0/7] kill: modernization after undeprecation Karel Zak
7 siblings, 0 replies; 9+ messages in thread
From: Sami Kerola @ 2013-03-07 23:27 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/kill.1 | 88 +++++++++++++++++++++++++++++--------------------------
1 file changed, 46 insertions(+), 42 deletions(-)
diff --git a/misc-utils/kill.1 b/misc-utils/kill.1
index 404fb72..47bac5d 100644
--- a/misc-utils/kill.1
+++ b/misc-utils/kill.1
@@ -1,7 +1,7 @@
.\" Copyright 1994 Salvatore Valente (svalente@mit.edu)
.\" Copyright 1992 Rickard E. Faith (faith@cs.unc.edu)
.\" May be distributed under the GNU General Public License
-.TH KILL 1 "February 2011" "util-linux" "User Commands"
+.TH KILL 1 "March 2013" "util-linux" "User Commands"
.SH NAME
kill \- terminate a process
.SH SYNOPSIS
@@ -19,15 +19,14 @@ kill \- terminate a process
.SH DESCRIPTION
The command
.B kill
-sends the specified signal to the specified process or process group.
-If no signal is specified, the TERM signal is sent. The TERM signal
-will kill processes which do not catch this signal. For other processes,
-it may be necessary to use the KILL (9) signal, since this signal cannot
-be caught.
+sends the specified signal to the specified process or process group. If no
+signal is specified, the TERM signal is sent. The TERM signal will kill
+processes which do not catch this signal. For other processes, it may be
+necessary to use the KILL (9) signal, since this signal cannot be caught.
.PP
-Most modern shells have a builtin kill function, with a usage rather similar
-to that of the command described here. The `-a' and `-p' options,
-and the possibility to specify processes by command name are a local extension.
+Most modern shells have a builtin kill function, with a usage rather similar to
+that of the command described here. The '-a' and '-p' options, and the
+possibility to specify processes by command name are a local extension.
.PP
If sig is 0, then no signal is sent, but error checking is still performed.
.SH OPTIONS
@@ -38,7 +37,6 @@ Specify the list of processes that
should signal. Each
.I pid
can be one of five things:
-
.RS
.TP
.I n
@@ -57,65 +55,66 @@ All processes with pid larger than 1 will be signaled.
.BI - n
where
.I n
-is larger than 1.
-All processes in process group
+is larger than 1. All processes in process group
.I n
-are signaled. When an argument of the form `-n' is given,
-and it is meant to denote a process group,
-either the signal must be specified first, or the argument must be preceded
-by a `--' option, otherwise it will be taken as the signal to send.
+are signaled. When an argument of the form '-n' is given, and it is meant to
+denote a process group, either the signal must be specified first, or the
+argument must be preceded by a '--' option, otherwise it will be taken as the
+signal to send.
+.RE
.TP
.I commandname
All processes invoked using that name will be signaled.
-.RE
.TP
-.BI \-s " signal"
-Specify the signal to send.
-The signal may be given as a signal name or number.
+\fB\-s\fR, \fB\-\-signal\fR \fIsignal\fR
+Specify the signal to send. The signal may be given as a signal name or
+number.
.TP
-.B \-l
+\fB\-l\fR, \fB\-\-list\fR [\fIsignal\fR]
Print a list of signal names. These are found in
-.I /usr/include/linux/signal.h
+.I /usr/\:include/\:linux/\:signal.h
.TP
-.B \-a
-Do not restrict the commandname-to-pid conversion to processes
-with the same uid as the present process.
+\fB\-a\fR, \fB\-\-all\fR
+Do not restrict the commandname-to-pid conversion to processes with the same
+uid as the present process.
.TP
-.B \-p
+\fB\-p\fR, \fB\-\-pid\fR
Specify that
.B kill
-should only print the process id (pid)
-of the named processes, and not send any signals.
+should only print the process id (pid) of the named processes, and not send any
+signals.
.TP
-.BI \-q " sigval"
+\fB\-q\fR, \fB\-\-queue\fR \fIsigval\fR
Use
.BR sigqueue (2)
rather than
.BR kill (2)
-and the sigval argument is used to specify an integer to be sent with the
-signal. If the receiving process has installed a handler for this signal using
-the SA_SIGINFO flag to
+and the
+.I sigval
+argument is used to specify an integer to be sent with the signal. If the
+receiving process has installed a handler for this signal using the SA_SIGINFO
+flag to
.BR sigaction (2),
then it can obtain this data via the si_value field of the siginfo_t structure.
.SH NOTES
-It's not possible to send a signal to explicitly selected thread in a
+It is not possible to send a signal to explicitly selected thread in a
multithreaded process by
.BR kill (2)
-syscall. If
+syscall. If
.BR kill(2)
-is used to send a signal to a thread group, then
-kernel selects arbitrary member of the thread group that has not blocked
-the signal. For more details see
+is used to send a signal to a thread group, then kernel selects arbitrary
+member of the thread group that has not blocked the signal. For more details
+see
.BR clone (2)
CLONE_THREAD description.
-
+.PP
The command
.BR kill (1)
as well as syscall
.BR kill (2)
accepts TID (thread ID, see
.BR gettid (2))
-as argument. In this case the kill behavior is not changed and the signal is
+as argument. In this case the kill behavior is not changed and the signal is
also delivered to the thread group rather than to the specified thread.
.SH "SEE ALSO"
.BR bash (1),
@@ -124,8 +123,13 @@ also delivered to the thread group rather than to the specified thread.
.BR sigvec (2),
.BR signal (7)
.SH AUTHOR
-Taken from BSD 4.4. The ability to translate process names to process
-ids was added by Salvatore Valente <svalente@mit.edu>.
+Taken from BSD 4.4. The ability to translate process names to process ids was
+added by
+.MT svalente@mit.edu
+Salvatore Valente
+.ME .
.SH AVAILABILITY
The kill command is part of the util-linux package and is available from
-ftp://ftp.kernel.org/pub/linux/utils/util-linux/.
+.UR ftp://\:ftp.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
+Linux Kernel Archive
+.UE .
--
1.8.1.5
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH 0/7] kill: modernization after undeprecation
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
` (6 preceding siblings ...)
2013-03-07 23:27 ` [PATCH 7/7] docs: add long options and tidy up kill.1 Sami Kerola
@ 2013-03-12 14:29 ` Karel Zak
7 siblings, 0 replies; 9+ messages in thread
From: Karel Zak @ 2013-03-12 14:29 UTC (permalink / raw)
To: Sami Kerola; +Cc: util-linux
On Thu, Mar 07, 2013 at 11:27:17PM +0000, Sami Kerola wrote:
> Sami Kerola (7):
> kill: add long options
> kill: align with howto-usage-function.txt
> kill: use libc error printing facilities and exit values
> kill, procs: fix coding style
> kill, procs: move function declarations to kill.h
Not applied this one. The include/ headers are usually for lib/
stuff. The another reason that the misc-util/proc.c code would be
better to move to lib/procutils.c (after some refactoring) -- I'll
do that.
> kill, procs: use pid_t for pids
> docs: add long options and tidy up kill.1
>
> misc-utils/kill.1 | 88 ++++++-----
> misc-utils/kill.c | 442 ++++++++++++++++++++++++++---------------------------
> misc-utils/kill.h | 8 +-
> misc-utils/procs.c | 170 +++++++++++----------
> 4 files changed, 360 insertions(+), 348 deletions(-)
Applied, thanks.
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 9+ messages in thread