summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2014-09-22 14:43:30 +0200
committerKarel Zak2014-09-22 14:43:30 +0200
commitfb2ba0666349ee25edae322ff6a3f0c0c942703f (patch)
treeea1f1e12d13016a42664b6117a7f2b28d029505d
parentlib/swapprober: add missing header file (diff)
parentrenice: reorder usage() option descriptions (diff)
downloadkernel-qcow2-util-linux-fb2ba0666349ee25edae322ff6a3f0c0c942703f.tar.gz
kernel-qcow2-util-linux-fb2ba0666349ee25edae322ff6a3f0c0c942703f.tar.xz
kernel-qcow2-util-linux-fb2ba0666349ee25edae322ff6a3f0c0c942703f.zip
Merge branch 'renice' of git://github.com/kerolasa/lelux-utiliteetit
* 'renice' of git://github.com/kerolasa/lelux-utiliteetit: renice: reorder usage() option descriptions rename: add getpriority() message lookup table renice: fix numeric uid argument parsing renice: avoid having same lines of code twice renice: disallow --priority <arg> without pid argument rename: use usage and version print out macros renice: reorder functions to avoid need of function prototype
-rw-r--r--sys-utils/renice.c111
1 files changed, 55 insertions, 56 deletions
diff --git a/sys-utils/renice.c b/sys-utils/renice.c
index c0378e1a5..100f6a534 100644
--- a/sys-utils/renice.c
+++ b/sys-utils/renice.c
@@ -48,36 +48,65 @@
#include "c.h"
#include "closestream.h"
-static int donice(int,int,int);
+const char *idtype[] = {
+ [PRIO_PROCESS] = N_("process ID"),
+ [PRIO_PGRP] = N_("process group ID"),
+ [PRIO_USER] = N_("user ID"),
+};
static void __attribute__((__noreturn__)) usage(FILE *out)
{
- fputs(_("\nUsage:\n"), out);
+ fputs(USAGE_HEADER, out);
fprintf(out,
_(" %1$s [-n] <priority> [-p|--pid] <pid>...\n"
" %1$s [-n] <priority> -g|--pgrp <pgid>...\n"
" %1$s [-n] <priority> -u|--user <user>...\n"),
program_invocation_short_name);
+ fputs(USAGE_OPTIONS, out);
+ fputs(_(" -n, --priority <num> specify the nice increment value\n"), out);
+ fputs(_(" -p, --pid <id> interpret argument as process ID (default)\n"), out);
+ fputs(_(" -g, --pgrp <id> interpret argument as process group ID\n"), out);
+ fputs(_(" -u, --user <name|id> interpret argument as username or user ID\n"), out);
+ fputs(USAGE_SEPARATOR, out);
+ fputs(USAGE_HELP, out);
+ fputs(USAGE_VERSION, out);
+ fprintf(out, USAGE_MAN_TAIL("renice(1)"));
+ exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
- fputs(_("\nOptions:\n"), out);
- fputs(_(" -g, --pgrp <id> interpret argument as process group ID\n"
- " -n, --priority <num> specify the nice increment value\n"
- " -p, --pid <id> interpret argument as process ID (default)\n"
- " -u, --user <name|id> interpret argument as username or user ID\n"
- " -h, --help display help text and exit\n"
- " -V, --version display version information and exit\n"), out);
+static int getprio(const int which, const int who, int *prio)
+{
+ errno = 0;
+ *prio = getpriority(which, who);
+ if (*prio == -1 && errno) {
+ warn(_("failed to get priority for %d (%s)"), who, idtype[which]);
+ return -errno;
+ }
+ return 0;
+}
- fputs(_("\nFor more information see renice(1).\n"), out);
+static int donice(const int which, const int who, const int prio)
+{
+ int oldprio, newprio;
- exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+ if (getprio(which, who, &oldprio) != 0)
+ return 1;
+ if (setpriority(which, who, prio) < 0) {
+ warn(_("failed to set priority for %d (%s)"), who, idtype[which]);
+ return 1;
+ }
+ if (getprio(which, who, &newprio) != 0)
+ return 1;
+ printf(_("%d (%s) old priority %d, new priority %d\n"),
+ who, idtype[which], oldprio, newprio);
+ return 0;
}
/*
* Change the priority (the nice value) of processes
* or groups of processes which are already running.
*/
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
{
int which = PRIO_PROCESS;
int who = 0, prio, errs = 0;
@@ -99,20 +128,19 @@ main(int argc, char **argv)
if (strcmp(*argv, "-v") == 0 ||
strcmp(*argv, "-V") == 0 ||
strcmp(*argv, "--version") == 0) {
- printf(_("%s from %s\n"),
- program_invocation_short_name, PACKAGE_STRING);
- exit(EXIT_SUCCESS);
+ printf(UTIL_LINUX_VERSION);
+ return EXIT_SUCCESS;
}
}
- if (argc < 2)
- usage(stderr);
-
- if (strcmp(*argv, "-n") == 0 || strcmp(*argv, "--priority") == 0) {
+ if (*argv && (strcmp(*argv, "-n") == 0 || strcmp(*argv, "--priority") == 0)) {
argc--;
argv++;
}
+ if (argc < 2)
+ usage(stderr);
+
prio = strtol(*argv, &endptr, 10);
if (*endptr)
usage(stderr);
@@ -134,18 +162,21 @@ main(int argc, char **argv)
continue;
}
if (which == PRIO_USER) {
- register struct passwd *pwd = getpwnam(*argv);
+ struct passwd *pwd = getpwnam(*argv);
- if (pwd == NULL) {
+ if (pwd != NULL)
+ who = pwd->pw_uid;
+ else
+ who = strtol(*argv, &endptr, 10);
+ if (who < 0 || *endptr) {
warnx(_("unknown user %s"), *argv);
errs = 1;
continue;
}
- who = pwd->pw_uid;
} else {
who = strtol(*argv, &endptr, 10);
if (who < 0 || *endptr) {
- warnx(_("bad value %s"), *argv);
+ warnx(_("bad %s value: %s"), idtype[which], *argv);
errs = 1;
continue;
}
@@ -154,35 +185,3 @@ main(int argc, char **argv)
}
return errs != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
-
-static int
-donice(int which, int who, int prio) {
- int oldprio, newprio;
- const char *idtype = _("process ID");
-
- if (which == PRIO_USER)
- idtype = _("user ID");
- else if (which == PRIO_PGRP)
- idtype = _("process group ID");
-
- errno = 0;
- oldprio = getpriority(which, who);
- if (oldprio == -1 && errno) {
- warn(_("failed to get priority for %d (%s)"), who, idtype);
- return 1;
- }
- if (setpriority(which, who, prio) < 0) {
- warn(_("failed to set priority for %d (%s)"), who, idtype);
- return 1;
- }
- errno = 0;
- newprio = getpriority(which, who);
- if (newprio == -1 && errno) {
- warn(_("failed to get priority for %d (%s)"), who, idtype);
- return 1;
- }
-
- printf(_("%d (%s) old priority %d, new priority %d\n"),
- who, idtype, oldprio, newprio);
- return 0;
-}