zsh-workers
 help / color / Atom feed
* [PATCH] POSIX compliant nice error checking
@ 2019-10-16 16:26 ` _RuRo_ (Андрей Стоцкий)
  2019-10-16 16:43   ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: _RuRo_ (Андрей Стоцкий) @ 2019-10-16 16:26 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] POSIX compliant nice error checking
  2019-10-16 16:26 ` [PATCH] POSIX compliant nice error checking _RuRo_ (Андрей Стоцкий)
@ 2019-10-16 16:43   ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2019-10-16 16:43 UTC (permalink / raw)
  To: zsh-workers

On Wed, 2019-10-16 at 19:26 +0300, _RuRo_ (Андрей Стоцкий) wrote:
> 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:

Thanks, I've committed that --- I suppose there's a vague possibility of
a problem with older systems that might not have set errno but that
doesn't seem worth worrying about.

pws

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20191016162708epcas2p37dc37493509eba8460bca124ffc3c32b@epcas2p3.samsung.com>
2019-10-16 16:26 ` [PATCH] POSIX compliant nice error checking _RuRo_ (Андрей Стоцкий)
2019-10-16 16:43   ` Peter Stephenson

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git