nice(): Put back old return value, keeping [EPERM] error.

Commit r279154 changed the API and ABI significantly, and {NZERO} is still
wrong.

Also, preserve errno on success instead of setting it to 0.

PR:		189821
Reported by:	bde
Relnotes:	yes
This commit is contained in:
Jilles Tjoelker 2015-02-28 18:22:10 +00:00
parent bd96bd15b2
commit c317cb51b2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=279397
3 changed files with 42 additions and 12 deletions

View File

@ -93,7 +93,11 @@ ATF_TC_HEAD(nice_priority, tc)
ATF_TC_BODY(nice_priority, tc)
{
#ifdef __FreeBSD__
int i, pri, pri2, nic;
#else
int i, pri, nic;
#endif
pid_t pid;
int sta;
@ -106,8 +110,10 @@ ATF_TC_BODY(nice_priority, tc)
pri = getpriority(PRIO_PROCESS, 0);
ATF_REQUIRE(errno == 0);
#ifdef __NetBSD__
if (nic != pri)
atf_tc_fail("nice(3) and getpriority(2) conflict");
#endif
/*
* Also verify that the nice(3) values
@ -119,10 +125,18 @@ ATF_TC_BODY(nice_priority, tc)
if (pid == 0) {
errno = 0;
#ifdef __FreeBSD__
pri = getpriority(PRIO_PROCESS, 0);
#else
pri2 = getpriority(PRIO_PROCESS, 0);
#endif
ATF_REQUIRE(errno == 0);
#ifdef __FreeBSD__
if (pri != pri2)
#else
if (nic != pri)
#endif
_exit(EXIT_FAILURE);
_exit(EXIT_SUCCESS);
@ -161,7 +175,11 @@ ATF_TC_HEAD(nice_thread, tc)
ATF_TC_BODY(nice_thread, tc)
{
pthread_t tid[5];
#ifdef __FreeBSD__
int pri, rv, val;
#else
int rv, val;
#endif
size_t i;
/*
@ -173,7 +191,12 @@ ATF_TC_BODY(nice_thread, tc)
val = nice(i);
ATF_REQUIRE(val != -1);
#ifdef __FreeBSD__
pri = getpriority(PRIO_PROCESS, 0);
rv = pthread_create(&tid[i], NULL, threadfunc, &pri);
#else
rv = pthread_create(&tid[i], NULL, threadfunc, &val);
#endif
ATF_REQUIRE(rv == 0);
rv = pthread_join(tid[i], NULL);

View File

@ -28,7 +28,7 @@
.\" @(#)nice.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
.Dd February 22, 2015
.Dd February 28, 2015
.Dt NICE 3
.Os
.Sh NAME
@ -48,9 +48,9 @@ This interface is obsoleted by
.Pp
The
.Fn nice
function obtains the scheduling priority of the process
from the system and sets it to the priority value specified in
.Fa incr .
function adds
.Fa incr
to the scheduling priority of the process.
The priority is a value in the range -20 to 20.
The default priority is 0; lower priorities cause more favorable scheduling.
Only the super-user may lower priorities.
@ -60,8 +60,9 @@ Children inherit the priority of their parent processes via
.Sh RETURN VALUES
Upon successful completion,
.Fn nice
returns the new nice value minus
.Dv NZERO .
returns 0, and
.Va errno
is unchanged.
Otherwise, \-1 is returned, the process' nice value is not changed, and
.Va errno
is set to indicate the error.
@ -84,7 +85,11 @@ argument is negative and the caller does not have appropriate privileges.
The
.Fn nice
function conforms to
.St -xpg4.2 .
.St -p1003.1-2008
except for the return value.
This implementation returns 0 upon successful completion but
the standard requires returning the new nice value,
which could be \-1.
.Sh HISTORY
A
.Fn nice

View File

@ -45,16 +45,18 @@ __FBSDID("$FreeBSD$");
int
nice(int incr)
{
int prio;
int saverrno, prio;
saverrno = errno;
errno = 0;
prio = getpriority(PRIO_PROCESS, 0);
if (prio == -1 && errno)
return -1;
if (prio == -1 && errno != 0)
return (-1);
if (setpriority(PRIO_PROCESS, 0, prio + incr) == -1) {
if (errno == EACCES)
errno = EPERM;
return -1;
return (-1);
}
return getpriority(PRIO_PROCESS, 0);
errno = saverrno;
return (0);
}