From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 3/7] kill: use libc error printing facilities and exit values
Date: Thu, 7 Mar 2013 23:27:20 +0000 [thread overview]
Message-ID: <1362698844-21292-4-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1362698844-21292-1-git-send-email-kerolasa@iki.fi>
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
next prev parent reply other threads:[~2013-03-07 23:27 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2013-03-07 23:27 ` [PATCH 4/7] kill, procs: fix coding style Sami Kerola
2013-03-07 23:27 ` [PATCH 5/7] kill, procs: move function declarations to kill.h Sami Kerola
2013-03-07 23:27 ` [PATCH 6/7] kill, procs: use pid_t for pids 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
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=1362698844-21292-4-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.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