nice: return EPERM instead of EACCES
authorAlexey Kodanev <aleksei.kodanev@bell-sw.com>
Tue, 29 Jun 2021 13:31:30 +0000 (16:31 +0300)
committerRich Felker <dalias@aerifal.cx>
Tue, 8 Mar 2022 22:15:14 +0000 (17:15 -0500)
To comply with POSIX, change errno from EACCES to EPERM
when the caller did not have the required privilege.

src/unistd/nice.c

index 6c25c8c..1c2295f 100644 (file)
@@ -1,4 +1,5 @@
 #include <unistd.h>
+#include <errno.h>
 #include <sys/resource.h>
 #include <limits.h>
 #include "syscall.h"
@@ -12,5 +13,11 @@ int nice(int inc)
                prio += getpriority(PRIO_PROCESS, 0);
        if (prio > NZERO-1) prio = NZERO-1;
        if (prio < -NZERO) prio = -NZERO;
-       return setpriority(PRIO_PROCESS, 0, prio) ? -1 : prio;
+       if (setpriority(PRIO_PROCESS, 0, prio)) {
+               if (errno == EACCES)
+                       errno = EPERM;
+               return -1;
+       } else {
+               return prio;
+       }
 }