0

Is it necessary to check for errors after calling uname(2)?

According to the manual page:

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

and under the ERRORS heading,

EFAULT buf is not valid.

is the only error listed.

Can I assume, therefore, that uname will never fail if given a valid struct utsname buffer?

yossarian
  • 1,537
  • 14
  • 21

1 Answers1

0

No.

Just because EFAULT is the only errno listed, it doesn't mean uname will never fail as long as you pass a valid buffer. It could fail for any other reason. i.e. if uname's return code is -1, then it failed irrespective of the value of errno.

errno is only meaningful if the function/system call indicates failure; it doesn't, on its own, define the success/failure of a library function/syscall.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • @P.P. But for what reason could it ever fail? Are there any other reasons than the feature was disabled during kernel compilation, such as it has been replaced with another implementation through `LD_LIBRARY_PATH`. But the latter would require another version of libc, right. – user877329 Jun 27 '16 at 17:04
  • 1
    @user877329 Using LD_LIBRARY_PATH and/or LD_PRELOAD etc are essentially hacks and all bets are off if that's the case. The chances of uname failing may be close to 0 (excluding scenarios like bugs in kernel). On [Linux](http://man7.org/linux/man-pages/man2/uname.2.html), the information comes from the running kernel. But POSIX doesn't define where this info comes from. So, on another OS it may be implemented differently and could fail for some any reason.From a user point of view, checking the return value is the pragmatic approach rather than relying on the knowledge of any implementation/OS. – P.P Jun 28 '16 at 08:08