zsh-workers
 help / color / mirror / code / Atom feed
From: "_RuRo_ (Андрей Стоцкий)" <ruro.ruro@ya.ru>
To: zsh-workers <zsh-workers@zsh.org>
Subject: [PATCH] POSIX compliant nice error checking
Date: Wed, 16 Oct 2019 19:26:06 +0300	[thread overview]
Message-ID: <5360011571243166@vla1-d97dbca235a9.qloud-c.yandex.net> (raw)

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


             reply	other threads:[~2019-10-16 16:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20191016162708epcas2p37dc37493509eba8460bca124ffc3c32b@epcas2p3.samsung.com>
2019-10-16 16:26 ` _RuRo_ (Андрей Стоцкий) [this message]
2019-10-16 16:43   ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5360011571243166@vla1-d97dbca235a9.qloud-c.yandex.net \
    --to=ruro.ruro@ya.ru \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).