Search code examples
cprocesslinux-kernelsystem-callsfile-descriptor

How to know if we get an error or a group ID using fcntl and F_GETOWN ?


Here is the syntax of using fcntl to manipulate file descriptors:

val = fcntl(fd, F_GETOWN, 0)

In the docs it's mentioned that this command returns a positive process ID or a negative process group ID.

And also it's mentioned that a value of -1 means an error has occurred.

How I can determine if the function had an error or returned the negative value of a group ID?


Solution

  • -1 is an error. Other negative values are process group IDs. To quote the Single UNIX specification for this function (emphasis mine):

    F_GETOWN

    If fildes refers to a socket, get the process ID or process group ID specified to receive SIGURG signals when out-of-band data is available. Positive values shall indicate a process ID; negative values, other than -1, shall indicate a process group ID; the value zero shall indicate that no SIGURG signals are to be sent. If fildes does not refer to a socket, the results are unspecified.

    Keep in mind that a process group ID of -1 doesn't make much sense anyway. It would imply that pid 1 (i.e, init) is the leader of a process group. This isn't possible in any normal system, and would have undesirable effects.