top(1): use modern interfaces for nice and related

- attempt and fail, rather than check for permission.
- use macro rather than explicit "-20"
This commit is contained in:
Eitan Adler 2018-06-10 09:15:13 +00:00
parent 1a8f56dd3a
commit a01160996b
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=334922
2 changed files with 10 additions and 32 deletions

View File

@ -379,14 +379,10 @@ kill_procs(char *str)
char *nptr;
int signum = SIGTERM; /* default */
int procnum;
int uid;
/* reset error array */
ERR_RESET;
/* remember our uid */
uid = getuid();
/* skip over leading white space */
while (isspace(*str)) str++;
@ -429,13 +425,8 @@ kill_procs(char *str)
}
else
{
/* check process owner if we're not root */
if (uid && (uid != proc_owner(procnum)))
{
ERROR(str, EACCES);
}
/* go in for the kill */
else if (kill(procnum, signum) == -1)
if (kill(procnum, signum) == -1)
{
/* chalk up an error */
ERROR(str, errno);
@ -458,10 +449,8 @@ renice_procs(char *str)
char negate;
int prio;
int procnum;
int uid;
ERR_RESET;
uid = getuid();
/* allow for negative priority values */
if ((negate = (*str == '-')) != 0)
@ -499,12 +488,7 @@ renice_procs(char *str)
ERROR(str, 0);
}
/* check process owner if we're not root */
else if (uid && (uid != proc_owner(procnum)))
{
ERROR(str, EACCES);
}
else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
{
ERROR(str, errno);
}

View File

@ -16,9 +16,9 @@
#include <sys/time.h>
#include <sys/cdefs.h>
#include <sys/limits.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/signal.h>
#include <time.h>
#include <errno.h>
#include <getopt.h>
@ -439,19 +439,13 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
break;
case 'q': /* be quick about it */
/* only allow this if user is really root */
if (getuid() == 0)
{
/* be very un-nice! */
nice(-20);
}
else
{
fprintf(stderr,
"%s: warning: `-q' option can only be used by root\n",
myname);
warnings++;
}
errno = 0;
i = setpriority(PRIO_PROCESS, 0, PRIO_MIN);
if (i == -1 && errno != 0) {
fprintf(stderr,
"%s: warning: `-q' option failed (%m)\n", myname);
warnings++;
}
break;
case 'm': /* select display mode */