josh
josh

Reputation: 113

Why do some Linux system calls not have a wrapper, but are documented as if they do?

Let's look at the gettid system call as an example:
http://man7.org/linux/man-pages/man2/gettid.2.html

I know gettid is not implemented in libc and I need to make a system call directly in order to use it (syscall(SYS_gettid)). I have verified this myself with this C code:

#include <stdio.h>
#include <sys/types.h>

int main(){

 pid_t a = gettid();
 return 0;
}

which doesn't link and gives this warning when compiling: warning: implicit declaration of function 'gettid'; did you mean 'getline'.

Now my question is, why has the Linux documentation documented it as if this function actually exists?

SYNOPSIS 

   #include <sys/types.h>

       pid_t gettid(void);

They have no example of how to make a direct system call and instead they have the above code snippet which doesn't exist and can't be used. Is there something I'm missing?

Upvotes: 5

Views: 964

Answers (1)

S.S. Anne
S.S. Anne

Reputation: 15576

The syscall doesn't have a wrapper in the GNU C library (before 2.30), this is just a prototype of how the function would look if it did.

As noted in the man page:

NOTES
Glibc does not provide a wrapper for this system call; call it using syscall(2).

Here's an example of the gettid wrapper:

#define _GNU_SOURCE
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

pid_t gettid(void)
{
    pid_t tid = (pid_t)syscall(SYS_gettid);
    return tid;
}

As you can see, this is the same prototype as described in the man-page. The prototype in the man-page is just for reference, so you can create a wrapper around the system call if you (or the libc developers) so choose.

If you're just starting to learn C, I suggest you stop trying to understand system calls and their wrappers in the C library until you have more experience in the language. The difference will then be clear.

Upvotes: 5

Related Questions