Schrodinger ZHU
Schrodinger ZHU

Reputation: 341

Is there a direct way to know the dispatch order of errnos from manpage?

Background

I am implementing syscall wrappers for libc. As a natural requirement, I will need to add unit tests to the wrappers. I rely heavily on https://man7.org/linux/man-pages/dir_all_alphabetic.html and https://linux.die.net/man/2/ to learn about the expected return values and errno of these wrappers.

The Problem

Well, sometimes the errno does not work out as expected. For example,

unsigned long page_size = sysconf(_SC_PAGESIZE);
void * page = mmap(nullptr, page_size, PROT_READ | PROT_WRITE,
                                  MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
EXPECT_THAT(mlock(holder.addr, negative_size), Fails(EINVAL));

According to the manpage, the above syscall wrapper should have set EINVAL purely based POSIX's expectation, since:

EINVAL: The result of the addition addr+len was less than addr (e.g., the addition may have resulted in an overflow).

However, if you run it in a constrained environment, say ulimit -l 16384. You will actually get a ENOMEM. This is because:

ENOMEM: (Linux 2.6.9 and later) the caller had a nonzero RLIMIT_MEMLOCK soft resource limit, but tried to lock more memory than the limit permitted. This limit is not enforced if the process is privileged (CAP_IPC_LOCK).

If you check the code inside Linux kernel, the permissions and limits are checked before the overflow examination. (https://elixir.bootlin.com/linux/latest/source/mm/mlock.c#L624)

This looks a little bit unsatisfactory to me in the sense that solely based on the manpage, I actually cannot know the expect behavior of the syscall wrappers. I also notice that man7.org and linux.die.net even list these errnos in completely different order. Therefore, is there a clean way to know which errno is actually expected? Do I need to inspect the kernel code everytime?

Upvotes: 0

Views: 21

Answers (0)

Related Questions