[comp.unix.questions] Weird syscall returns

guy%gorodish@Sun.COM (Guy Harris) (07/16/87)

(This is now a UNIX discussion rather than a C discussion, so I'm
moving it to comp.unix.questions.)

> > Other versions of "nice" may just return 0 on success, but they all
> > return -1 on error.
> 
> How about "all reasonable versions return -1 on error"?  Here in the
> backwaters of UNIX work-a-likes (aka XENIX), the manual says
> 
> 	 Note that 'nice' is unusual in the way return codes are 
> 	handled.  It differs from most other system calls in two ways: the
> 	value -1 is a valid return code (in the case where the new nice
> 	value is 19), and the system call either works or ignores the
> 	request; THERE IS NEVER AN ERROR.  [my emphasis]

Well, if this is really the case (i.e., they didn't just botch the
manual page), I'd be curious to know why they changed the AT&T code.
(Yes, Xenix *IS* derived from AT&T code; it's not a "rewritten from
scratch" "work-alike".)

My suspicion is that the documentation writers just got confused.
Try writing a program that attempts to reduce your "nice", and run
that program when not the super-user.  Have the program set "errno"
to zero before making the call, and check both the return code from
"nice" and the value of "errno" after making the call.  If the return
code isn't -1, and the value of "errno" isn't EPERM, somebody doing the
software screwed up.  If the return code is -1, and the value of
"errno" is EPERM, somebody doing the documentation screwed up.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

guy%gorodish@Sun.COM (Guy Harris) (07/17/87)

> Whoa there, let's take a look:
> return code		sys call
> non-negative int or -1	creat, dup, fork, lseek, msgget, nice, open,
> 
> So, what call returns a -1 as an error that can also return a negative
> integer as a valid value?  NONE!  I know about maus, but since we're
> talking portable, don't even mention it.  My claim is that since *most*
> of the system calls return n >= 0 on success, it is more natural to test
> for !(n >= 0) which is the same as n < 0 for failure.

Whoa there, let's take a *careful* look this time.  "nice" *can* return a
negative integer ("Upon successful completion, 'nice' returns the new nice
value minus 20," from the S5R2 manual page, and the new "nice" value
can be less than 20).  It is OK to test for "n < 0" if the call is
guaranteed to return a non-negative integer on success, but NOT if it
can return a negative integer.

"nice" is even worse, because (at least on systems where it returns
the new "nice" value) it can return -1 on *success*!  You can only
test this by clearing "errno", making the call, and testing "errno"
after the call.  (The same is true of "ptrace".)
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com