Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the various unistd.h under /usr/include in Linux?

Tags:

c

linux

Under the /usr/include directory in Linux i entered the command: find -type f -name unistd.h which gave the following output:

./unistd.h ./linux/unistd.h ./asm-generic/unistd.h ./bits/unistd.h ./asm/unistd.h ./sys/unistd.h

my question is, what is the purpose of each unistd.h, since there is only one definiton of that file in the single unix specification ?

Thanks in advance.

like image 261
Marwan Tanager Avatar asked Jun 01 '10 09:06

Marwan Tanager


1 Answers

linux/unistd.h actually points to asm/unistd.h, which in turn points to either asm/unistd_32.h or asm/unistd_64.h, which is where system call numbers are defined and presented to user space depending on the system's architecture. These come from the kernel.

bits/unistd.h is a collection of macros that augment unistd.h (mostly stuff to help prevent buffer overflows), which is conditionally included via:

/* Define some macros helping to catch buffer overflows.  */
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
# include <bits/unistd.h>
#endif

In essence, the only POSIX required header is in fact, just unistd.h, the rest are either extensions, or definitions from the kernel. So, just including unistd.h is all you have to worry about doing, everything you need will be pulled in depending on your architecture and whatever build options you've selected.

like image 75
Tim Post Avatar answered Oct 21 '22 04:10

Tim Post