Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] POSIX compliant nice error checking



In `Src/exec.c`, the return value of nice(5) is checked, however according to
POSIX (http://man7.org/linux/man-pages/man2/nice.2.html), one should check errno
instead, since nice can return negative values even on success. Since nice(5)
increments the niceness by 5, the return value can be negative, if the original
niceness was less than -5. For example, with nice -6, you'll get the annoying
`zsh: nice(5) failed: success` error message:

```
> sudo renice -6 $$
17187 (process ID) old priority 0, new priority -6
> cat &
[1] 6200
zsh: nice(5) failed: success
[1]  + 6200 suspended (tty input)  cat
> kill %1
[1]  + 6200 terminated  cat
```

With nice -5 this doesn't happen:

```
> sudo renice -5 $$
17187 (process ID) old priority -6, new priority -5
> cat &
[1] 7559
[1]  + 7559 suspended (tty input)  cat
> kill %1
[1]  + 7559 terminated  cat
```

Checking errno instead of the return value fixes this.

---
 Src/exec.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/Src/exec.c b/Src/exec.c
index 042ba065a..08411ce28 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -2762,9 +2762,12 @@ execcmd_fork(Estate state, int how, int type, Wordcode varspc,
     sigtrapped[SIGEXIT] = 0;
 #ifdef HAVE_NICE
     /* Check if we should run background jobs at a lower priority. */
-    if ((how & Z_ASYNC) && isset(BGNICE))
-	if (nice(5) < 0)
+    if ((how & Z_ASYNC) && isset(BGNICE)) {
+	errno = 0;
+	nice(5);
+	if (errno)
 	    zwarn("nice(5) failed: %e", errno);
+    }
 #endif /* HAVE_NICE */
 
     return 0;
-- 
2.23.0



Messages sorted by: Reverse Date, Date, Thread, Author