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 ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.