aboutsummaryrefslogtreecommitdiff
path: root/bin/kill/kill.c
diff options
context:
space:
mode:
authorJilles Tjoelker <jilles@FreeBSD.org>2010-12-21 22:47:34 +0000
committerJilles Tjoelker <jilles@FreeBSD.org>2010-12-21 22:47:34 +0000
commit0a62a9caa9b95a5ae7fe502628ef0fd31dca16bd (patch)
tree571f5b4935a3ac4f69aff2b5cfb327687b1f59fe /bin/kill/kill.c
parent05bcfef170d1a2a70062fd93c864679ef3daf49f (diff)
downloadsrc-0a62a9caa9b95a5ae7fe502628ef0fd31dca16bd.tar.gz
src-0a62a9caa9b95a5ae7fe502628ef0fd31dca16bd.zip
sh: Add kill builtin.
This allows specifying a %job (which is equivalent to the corresponding process group). Additionally, it improves reliability of kill from sh in high-load situations and ensures "kill" finds the correct utility regardless of PATH, as required by POSIX (unless the undocumented %builtin mechanism is used). Side effect: fatal errors (any error other than kill(2) failure) now return exit status 2 instead of 1. (This is consistent with other sh builtins, but not in NetBSD.) Code size increases about 1K on i386. Obtained from: NetBSD
Notes
Notes: svn path=/head/; revision=216629
Diffstat (limited to 'bin/kill/kill.c')
-rw-r--r--bin/kill/kill.c43
1 files changed, 32 insertions, 11 deletions
diff --git a/bin/kill/kill.c b/bin/kill/kill.c
index f4ab2df3e01a..edfc92832648 100644
--- a/bin/kill/kill.c
+++ b/bin/kill/kill.c
@@ -49,6 +49,12 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h>
#include <string.h>
+#ifdef SHELL
+#define main killcmd
+#include "bltin/bltin.h"
+#include "error.h"
+#endif
+
static void nosig(const char *);
static void printsignals(FILE *);
static int signame_to_signum(const char *);
@@ -75,16 +81,16 @@ main(int argc, char *argv[])
usage();
numsig = strtol(*argv, &ep, 10);
if (!**argv || *ep)
- errx(1, "illegal signal number: %s", *argv);
+ errx(2, "illegal signal number: %s", *argv);
if (numsig >= 128)
numsig -= 128;
if (numsig <= 0 || numsig >= sys_nsig)
nosig(*argv);
printf("%s\n", sys_signame[numsig]);
- exit(0);
+ return (0);
}
printsignals(stdout);
- exit(0);
+ return (0);
}
if (!strcmp(*argv, "-s")) {
@@ -107,7 +113,7 @@ main(int argc, char *argv[])
} else if (isdigit(**argv)) {
numsig = strtol(*argv, &ep, 10);
if (!**argv || *ep)
- errx(1, "illegal signal number: %s", *argv);
+ errx(2, "illegal signal number: %s", *argv);
if (numsig < 0)
nosig(*argv);
} else
@@ -122,16 +128,23 @@ main(int argc, char *argv[])
usage();
for (errors = 0; argc; argc--, argv++) {
- pid = strtol(*argv, &ep, 10);
- if (!**argv || *ep)
- errx(1, "illegal process id: %s", *argv);
- else if (kill(pid, numsig) == -1) {
+#ifdef SHELL
+ if (**argv == '%')
+ pid = getjobpgrp(*argv);
+ else
+#endif
+ {
+ pid = strtol(*argv, &ep, 10);
+ if (!**argv || *ep)
+ errx(2, "illegal process id: %s", *argv);
+ }
+ if (kill(pid, numsig) == -1) {
warn("%s", *argv);
errors = 1;
}
}
- exit(errors);
+ return (errors);
}
static int
@@ -154,7 +167,11 @@ nosig(const char *name)
warnx("unknown signal %s; valid signals:", name);
printsignals(stderr);
- exit(1);
+#ifdef SHELL
+ error(NULL);
+#else
+ exit(2);
+#endif
}
static void
@@ -180,5 +197,9 @@ usage(void)
" kill -l [exit_status]",
" kill -signal_name pid ...",
" kill -signal_number pid ...");
- exit(1);
+#ifdef SHELL
+ error(NULL);
+#else
+ exit(2);
+#endif
}