What does errno do?
Remember the errno global; it's an uninitialized global integer in the process's uninitialized data segment (process layout was covered in Chapter 2, Virtual Memory).
What is errno for? Whenever a system call fails, it returns -1 to userspace. But why did it fail? Ah, the error diagnostic, the reason it failed, is returned to userspace like this: glibc, in conjunction with the kernel, pokes the global errno with a positive integer value. This value is actually an index into a two-dimensional array of English error messages (which is NULL-terminated); it's called _sys_errlist. So, looking up _sys_errlist[errno] reveals the English error message: the reason the system call failed.
Instead of the developer performing all ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access