Home > Software engineering >  Where is `unshare` defined on Linux?
Where is `unshare` defined on Linux?

Time:09-11

I am trying to use the unshare system call, however I am unable to locate the header where it is defined. I tried:

#define _GNU_SOURCE
#include <sched.h>

But to no result (The function isn't there, I looked at the header myself). Which header should I include?

CodePudding user response:

I have tried and failed to reproduce the statement in the question: My test program builds without issues.

I looked up the unshare(2) man page and found

SYNOPSIS
       #define _GNU_SOURCE
       #include <sched.h>

       int unshare(int flags);

so I wrote a small test program

#define _GNU_SOURCE
#include <sched.h>

int main(void)
{
  unshare(123);
  return 0;
}

which I then built with all warnings enabled and treated as errors:

$ gcc -std=c11 -pedantic -Wall -Wextra -Werror -o unshare-ex unshare-ex.c
$ _

without any problems.

So I rebuilt the program with

$ gcc -g -O2 -save-temps=obj -std=c11 -pedantic -Wall -Wextra -Werror -o unshare-ex unshare-ex.c
$ _

and took a look at unshare-ex.i to trace the include files. As it turns out, /usr/include/sched.h eventually includes /usr/include/bits/sched.h which contains the definition of the unshare() function which expands to

extern int unshare (int __flags) __attribute__ ((__nothrow__ , __leaf__));

but is written in /usr/include/bits/sched.h as

extern int unshare (int __flags) __THROW;

I am not sure what the problem is here.

FWIW, both /usr/include/sched.h and /usr/include/bits/sched.h are shipped as part of the glibc-headers-x86-2.35-15.fc36.noarch package here.

CodePudding user response:

Apparently instead of

#define _GNU_SOURCE

It should be

#define __USE_GNU
  • Related