Search code examples
pthreadsposixmanualmanpage

How to read the manual page for POSIX refering to error codes?


I am currently working with pthread and read the documentation from here: Pthread Manual Pthread Join.

However, when I read the pages I see the ERRORS, but not the corresponding return values, which will get returned from pthread_join. So my question is, are the ERRORS ordered in ascending order (since it might be an enumerator)?


Solution

  • Error values such as EDEADLK are macros defined by including the <errno.h> header file.

    Example:

    #include <pthread.h>
    #include <errno.h>
    
    ...
    
    int retval = pthread_join( threadID, NULL );
    if ( retval == EDEADLK )
    {
        // error-handling code for deadlock
    }
    else if ( retval == EINVAL )
    {
        // error-handling code for invalid thread id
    }
    else if ( retval == ESRCH  )
    {
        // error-handling code for no such thread id
    }
    

    Note that the above code is correct only for Linux, as the only error number specified by POSIX for pthread_join() is EDEADLCK.

    Per the POSIX standard for error numbers, (bolded portions particularly relevant to your question):

    2.3 Error Numbers

    Most functions can provide an error number. The means by which each function provides its error numbers is specified in its description.

    Some functions provide the error number in a variable accessed through the symbol errno, defined by including the <errno.h> header. The value of errno should only be examined when it is indicated to be valid by a function's return value. No function in this volume of POSIX.1-2008 shall set errno to zero. For each thread of a process, the value of errno shall not be affected by function calls or assignments to errno by other threads.

    Some functions return an error number directly as the function value. These functions return a value of zero to indicate success.

    If more than one error occurs in processing a function call, any one of the possible errors may be returned, as the order of detection is undefined.

    Implementations may support additional errors not included in this list, may generate errors included in this list under circumstances other than those described here, or may contain extensions or limitations that prevent some errors from occurring.

    The ERRORS section on each reference page specifies which error conditions shall be detected by all implementations ("shall fail") and which may be optionally detected by an implementation ("may fail"). If no error condition is detected, the action requested shall be successful. If an error condition is detected, the action requested may have been partially performed, unless otherwise stated.

    Implementations may generate error numbers listed here under circumstances other than those described, if and only if all those error conditions can always be treated identically to the error conditions as described in this volume of POSIX.1-2008. Implementations shall not generate a different error number from one required by this volume of POSIX.1-2008 for an error condition described in this volume of POSIX.1-2008, but may generate additional errors unless explicitly disallowed for a particular function.

    Each implementation shall document, in the conformance document, situations in which each of the optional conditions defined in POSIX.1-2008 is detected. The conformance document may also contain statements that one or more of the optional error conditions are not detected.

    Certain threads-related functions are not allowed to return an error code of [EINTR]. Where this applies it is stated in the ERRORS section on the individual function pages.

    The following macro names identify the possible error numbers, in the context of the functions specifically defined in this volume of POSIX.1-2008; these general descriptions are more precisely defined in the ERRORS sections of the functions that return them. Only these macro names should be used in programs, since the actual value of the error number is unspecified. All values listed in this section shall be unique, except as noted below. The values for all these macros shall be found in the <errno.h> header defined in the Base Definitions volume of POSIX.1-2008. The actual values are unspecified by this volume of POSIX.1-2008.

    [E2BIG]
        Argument list too long. The sum of the number of bytes
        used by the new process image's argument list and environment
        list is greater than the system-imposed limit of {ARG_MAX} bytes.
    
        or:
    
        Lack of space in an output buffer.
    
        or:
    
        Argument is greater than the system-imposed maximum.
    [EACCES]
        Permission denied. An attempt was made to access a file
        in a way forbidden by its file access permissions.
    [EADDRINUSE]
        Address in use. The specified address is in use.
    [EADDRNOTAVAIL]
        Address not available. The specified address is not
        available from the local system.
    [EAFNOSUPPORT]
        Address family not supported. The implementation does
        not support the specified address family, or the specified
        address is not a valid address for the address family
        of the specified socket.
    [EAGAIN]
        Resource temporarily unavailable. This is a temporary
        condition and later calls to the same routine may complete
        normally.
    [EALREADY]
        Connection already in progress. A connection request is already
        in progress for the specified socket.
        .
        .
        .