util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [bug] renice not POSIX compliant
@ 2019-09-14 13:55 Stephane Chazelas
  2019-11-08 11:08 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Stephane Chazelas @ 2019-09-14 13:55 UTC (permalink / raw)
  To: util-linux

Hi, there are a few POSIX compliance issues with the "renice"
command from util-linux.

First and more importantly, per POSIX, like for "nice", "-n"
is meant to take an "increment"/"adjustment", not an absolute
value.

As in

renice -n 5 "$pid"

is meant to increase the nice value by 5. While the traditional,
BSD-style:

renice 5 "$pid"

sets its absolute value to 5.

util-linux added "-n" in 2009 for POSIX compliance (see
https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/commit/?id=7cfbafda9c484a8cadefc47ee115086e803d9391),
but the "-n" is just ignored. Since 2010, the --help message
does say that -n/--priority takes an "increment"
(https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/commit/?id=296351b0f112c821bffbe8ce7aa35d716251051d)
but that's not what happens in practice (it still sets the
niceness to the value specified).

$ nice -n 1 sh -c 'renice -n 2 "$$"; ps -o nice,comm -p "$$"; exit'
29606 (process ID) old priority 1, new priority 2
 NI COMMAND
  2 sh


Also, a POSIX renice utility is meant to parse the options the
usual (getopt(3)) way. In particular,

renice -gn 1 -- 123

Is meant to increase the niceness of pgid 123 by 1, but fails in
util-linux renice.

$ renice -gn 1 -- "$$"
renice: invalid priorty '-gn'
Try 'renice --help' for more information.

-p, -g, -u are meant to specify how *all* operands are to be
interpreted, they are not options that take arguments.

renice +1 123
renice +1 123 -u me

are unspecified by POSIX, so it's fine for util-linux to handle
the BSD way, but

renice -n 1 -u me -p 123

Is meant to increase the niceness of users "me", "-p" and "123".

Also, calling it "--priority" is quite misleading as the
niceness is rather the opposite of priority. GNU "nice" uses
"--adjustment" as the long-option equivalent of "-n" (note that
FreeBSD also calls it "priority" though)..

IMO, the best way to handle it would be to do a BSD-style
parsing when the first argument matches the [-+]?\d+ RE, and do
a POSIX getopt_long parsing otherwise with:

Change the (POSIX style) options to:

  -n/--adjustment <num>: niceness increment
  -v/--value (deprecated: --priority) <num>: exact niceness value
  -g/--pgrp (no arg): interpret operands as gids.
  -u/--user (no arg): interpret operands as user names/ids
  -p/-pid (no arg): interpret operands as pids.
  --help/--version...

And keep the

renice niceness [[-p/--pid] pid ...] [[-g/--pgrp] pgid ...] [[-u/--user] user ...]

usage for backward compatibility and BSD compatibility.

Note that there's currently a bug in the latest version of the
POSIX specification in that it says that positive increments
should *decrease* the niceness
(http://austingroupbugs.net/view.php?id=1286).

See
https://unix.stackexchange.com/questions/541673/how-do-i-start-a-shell-the-way-all-the-processes-ran-from-within-it-and-processe/541691#541691
for more details.

-- 
Stephane


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

end of thread, other threads:[~2019-11-08 11:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-14 13:55 [bug] renice not POSIX compliant Stephane Chazelas
2019-11-08 11:08 ` Karel Zak

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).