No "Fixes:" in comment because the current behavior emulates old Linux behavior which is possibly not a bug.

SUS 1997 to POSIX 2024 require to return the new nice value.
https://pubs.opengroup.org/onlinepubs/007908799/xsh/nice.html
https://pubs.opengroup.org/onlinepubs/9799919799/functions/nice.html
https://man7.org/linux/man-pages/man2/nice.2.html

FreeBSD still returns 0:
https://man.freebsd.org/cgi/man.cgi?query=nice&sektion=3

Ancient Unix returned nothing :-)
http://man.cat-v.org/unix_10th/2/nice

--
Regards,
Christian

From 40d17d32e4c0a7dc69f39e57f3d0f9f07fca5c75 Mon Sep 17 00:00:00 2001
From: Christian Franke <christian.fra...@t-online.de>
Date: Wed, 27 Nov 2024 18:54:37 +0100
Subject: [PATCH] Cygwin: nice: align return value and errno with POSIX and
 Linux

Return new nice value instead of 0 on success.
Set errno to EPERM instead of EACCES on failure.

Signed-off-by: Christian Franke <christian.fra...@t-online.de>
---
 winsup/cygwin/release/3.6.0 |  4 ++++
 winsup/cygwin/syscalls.cc   | 11 ++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/release/3.6.0 b/winsup/cygwin/release/3.6.0
index ef7e4018f..1b2f00ad8 100644
--- a/winsup/cygwin/release/3.6.0
+++ b/winsup/cygwin/release/3.6.0
@@ -48,3 +48,7 @@ What changed:
   or EPERM if Windows would silently set a lower priority
   (HIGH_PRIORITY_CLASS instead of REALTIME_PRIORITY_CLASS) due to
   missing administrator privileges.
+
+- nice(2) now returns the new nice value instead of 0 on success
+  and sets errno to EPERM instead of EACCES on failure.  This confirms
+  to POSIX and Linux (glibc >= 2.2.4) behavior.
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 72537bc5a..60350b690 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -3959,7 +3959,16 @@ out:
 extern "C" int
 nice (int incr)
 {
-  return setpriority (PRIO_PROCESS, myself->pid, myself->nice + incr);
+  if (setpriority (PRIO_PROCESS, myself->pid, myself->nice + incr))
+    {
+      /* POSIX: EPERM instead of EACCES. */
+      set_errno (EPERM);
+      return -1;
+    }
+
+  /* POSIX: return the new nice value.  Linux glibc >= 2.2.4 provides
+     conformance with POSIX (FreeBSD returns 0). */
+  return myself->nice;
 }
 
 static void
-- 
2.45.1

Reply via email to