cposixaio

Why _SC_AIO_MAX is well-defined while sysconf(_SC_AIO_MAX) return -1?


Here is a simple demo:

#include <sys/stat.h>
#include <aio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
#ifdef _SC_AIO_MAX
    printf("_SC_AIO_MAX is defined\n");
if (sysconf(_SC_AIO_MAX) == -1)
{
    printf("unsupported\n");
    printf("_SC_AIO_MAX = %d\n", _SC_AIO_MAX);
    printf("sysconf(_SC_AIO_MAX) = %d\n", sysconf(_SC_AIO_MAX));
}
#else
    printf("_SC_AIO_MAX is undefined\n");
#endif
return 0;
}

output:

_SC_AIO_MAX is defined unsupported _SC_AIO_MAX = 24 sysconf(_SC_AIO_MAX) = -1

live demo: https://wandbox.org/permlink/7GDzyvEUgRwMHX95

As you can see, _SC_AIO_MAX is defined to be 24, but sysconf(_SC_AIO_MAX) returns -1. According man 3 sysconf

       *  If  name  corresponds  to a maximum or minimum limit, and that limit is indeterminate, -1 is re‐
          turned and errno is not changed.  (To distinguish an indeterminate limit from an error, set  er‐
          rno to zero before the call, and then check whether errno is nonzero when -1 is returned.)

But the limit has been defined to be 24, why does sysconf still return -1?


Solution

  • _SC_AIO_MAX = 24 is not the value of the limit, it's the identifier of the limit that you would like to access.

    getconf(24) == -1 means either:

    1. There is an error (check errno to see if there was an error); or

    2. That the limit is indeterminate.

    Some documentation mentions that you should set errno to 0 before calling getconf to make sure you can tell the difference between these two cases.

    (2) might happen when functionality is available but has been disable, etc.