renice: avoid having same lines of code twice
Add getprio() function to avoid duplication of a simple task. Reviewed-by: Karel Zak <kzak@redhat.com> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
0bb01bb0b7
commit
00b490f002
|
@ -67,8 +67,19 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
|
||||||
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int getprio(const int which, const int who, const char *idtype, int *prio)
|
||||||
donice(int which, int who, int prio) {
|
{
|
||||||
|
errno = 0;
|
||||||
|
*prio = getpriority(which, who);
|
||||||
|
if (*prio == -1 && errno) {
|
||||||
|
warn(_("failed to get priority for %d (%s)"), who, idtype);
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int donice(int which, int who, int prio)
|
||||||
|
{
|
||||||
int oldprio, newprio;
|
int oldprio, newprio;
|
||||||
const char *idtype = _("process ID");
|
const char *idtype = _("process ID");
|
||||||
|
|
||||||
|
@ -76,24 +87,14 @@ donice(int which, int who, int prio) {
|
||||||
idtype = _("user ID");
|
idtype = _("user ID");
|
||||||
else if (which == PRIO_PGRP)
|
else if (which == PRIO_PGRP)
|
||||||
idtype = _("process group ID");
|
idtype = _("process group ID");
|
||||||
|
if (getprio(which, who, idtype, &oldprio) != 0)
|
||||||
errno = 0;
|
|
||||||
oldprio = getpriority(which, who);
|
|
||||||
if (oldprio == -1 && errno) {
|
|
||||||
warn(_("failed to get priority for %d (%s)"), who, idtype);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
if (setpriority(which, who, prio) < 0) {
|
if (setpriority(which, who, prio) < 0) {
|
||||||
warn(_("failed to set priority for %d (%s)"), who, idtype);
|
warn(_("failed to set priority for %d (%s)"), who, idtype);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
errno = 0;
|
if (getprio(which, who, idtype, &newprio) != 0)
|
||||||
newprio = getpriority(which, who);
|
|
||||||
if (newprio == -1 && errno) {
|
|
||||||
warn(_("failed to get priority for %d (%s)"), who, idtype);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
|
|
||||||
printf(_("%d (%s) old priority %d, new priority %d\n"),
|
printf(_("%d (%s) old priority %d, new priority %d\n"),
|
||||||
who, idtype, oldprio, newprio);
|
who, idtype, oldprio, newprio);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -103,8 +104,7 @@ donice(int which, int who, int prio) {
|
||||||
* Change the priority (the nice value) of processes
|
* Change the priority (the nice value) of processes
|
||||||
* or groups of processes which are already running.
|
* or groups of processes which are already running.
|
||||||
*/
|
*/
|
||||||
int
|
int main(int argc, char **argv)
|
||||||
main(int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
int which = PRIO_PROCESS;
|
int which = PRIO_PROCESS;
|
||||||
int who = 0, prio, errs = 0;
|
int who = 0, prio, errs = 0;
|
||||||
|
|
Loading…
Reference in New Issue