5

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?

Boann
  • 48,794
  • 16
  • 117
  • 146
josh
  • 103
  • 1
  • 6
  • 1
    @Scheff nothing is broken, this is an actual thing: https://ubuntuforums.org/showthread.php?t=345317 , issue exists for some other sys calls as-well. – josh Aug 31 '19 at 14:51
  • 1
    @josh, the docs you link specify both the version of glibc that first provided a wrapper function, and the mechanism ([`syscall()`](http://man7.org/linux/man-pages/man2/syscall.2.html)) that had to be used instead in earlier versions. So what exactly are you asking? – John Bollinger Aug 31 '19 at 14:52
  • So linux itself doesn't provide any of the wrappers, its the job of libc to do so – josh Aug 31 '19 at 14:54
  • josh Nice work and good style accepting my poky hint. Good luck. – Yunnosch Aug 31 '19 at 15:00
  • Linux itself is just the operating system kernel. A Linux *distribution* combines that with libraries and utilities to make a functional operating system. Syscall wrappers are functions that reside in libraries, as indeed is the general-purpose `syscall()` function, too. They indeed are not part of the kernel, and which are available indeed does depend on the system libraries. – John Bollinger Aug 31 '19 at 15:01
  • 1
    @JohnBollinger that wrapper was added to GlibC version 2.30, published at the first of August this year, a mere four weeks ago, so we should give the OP a bit of slack here, I think ;-) – deamentiaemundi Aug 31 '19 at 15:20
  • @deamentiaemundi *finally*. How the heck did it take them *this long* to implement it? – S.S. Anne Aug 31 '19 at 15:29
  • @deamentiaemundi, I have no expectation that the OP should be completely up to date with respect to when particular features were added to Glibc, but inasmuch as the documentation that they themselves presented seems to contradict the claims they are making, I do think it reasonable to ask for some clarification of what they want to know. – John Bollinger Aug 31 '19 at 16:29

1 Answers1

5

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.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • This syscall *does* have a wrapper in new-enough Glibc, as indeed the online manpage the OP linked indicates. From comments, part of their confusion revolves around the difference between system calls in the kernel-interface sense and system calls in the wrapper-function sense, and whose responsibility it is to provide the latter. – John Bollinger Aug 31 '19 at 16:25
  • @JohnBollinger Which I covered in my answer to the OP's other question. See edit. – S.S. Anne Aug 31 '19 at 19:36
  • I tried with an very old gcc version and it works. https://wandbox.org/permlink/oQf8xPjRWZl1Zkf8 . Do they backport this wrapper? – Rick Aug 24 '22 at 16:24
  • See comments in https://stackoverflow.com/a/61980016/5983841 – Rick Aug 25 '22 at 07:21
  • @Rick It was added to glibc after I wrote the answer. This is nearly three years old, after all – S.S. Anne Aug 26 '22 at 01:13