Where are syscalls located in glibc source
Asked Answered
F

4

26

So I was looking through the linux glibc source and I don't see where it actually does anything. The following is from io/chdir.c but it is indicative of many of the source files. What's going on here? Obviously I am missing something. What's the secret, where does it make a system call or actually do something?

stub_warning is some legacy craziness. __set_errno seems to be a simple macro that sets errno. And while I find a million usages of weak_alias I don't see it defined anywhere.

Is there a helpful guide to understanding how glibc works somewhere?

#include <errno.h>
#include <stddef.h>
#include <unistd.h>

/* Change the current directory to PATH.  */
int
__chdir (path)
     const char *path;
{
  if (path == NULL)
    {
      __set_errno (EINVAL);
      return -1;
    }

  __set_errno (ENOSYS);
  return -1;
}
stub_warning (chdir)

weak_alias (__chdir, chdir)
#include <stub-tag.h> 
Forkey answered 29/6, 2011 at 3:54 Comment(1)
Ouch. Many more layers to this onion than I anticipated. Thanks to everyone. Stay tuned. More hilarity may ensue in coming weeks.Forkey
J
18

What you've found is a stub function for systems it's not implemented on. You need to look under the sysdeps tree for the actual implementation. The following may be of interest:

  • sysdeps/unix/sysv/linux
  • sysdeps/posix
  • sysdeps/i386 (or x86_64 or whatever your cpu arch is)
Juggernaut answered 29/6, 2011 at 3:59 Comment(1)
Thanks for accepting my answer, but please be sure to look at the others too, especially if you're interested in how glibc builds syscall wrappers. My answer was less targetted at your specific question (about chdir) but may be more useful if you want to know where/how more complex (not just a syscall wrapper) system-specific functions are implemented.Juggernaut
J
11

The actual system call code for chdir() is auto-generated on most systems supported by glibc, by the script make-syscalls.sh. That's why you can't find it in the source tree.

Jacobs answered 29/6, 2011 at 4:6 Comment(0)
J
7

That's a generic stub that is used if another definition doesn't exist; weak_alias is a cpp macro which tells the linker that __chdir should be used when chdir is requested, but only if no other definition is found. (See weak symbols for more details.)

chdir is actually a system call; there will be per-OS system call bindings in the gibc source tree, which will override the stub definition with a real one that calls into the kernel. This allows glibc to present a stable interface across systems which may not have all of the system calls that glibc knows about.

Jesuit answered 29/6, 2011 at 4:1 Comment(0)
A
6

Note that the actual system calls aren't defined anywhere in the source tree - they're generated at build time from syscalls.list (linked is the one in sysdeps/unix, there are additional ones further down), a series of macros in sysdep.h (linked linux/i386), and a script that actually generates the source files.

Autosuggestion answered 29/6, 2011 at 4:11 Comment(1)
without sources, on a live system, how can I see the os api called ? is it possible to strace an application , but only the code executed into glibc ?Sweepstake

© 2022 - 2024 — McMap. All rights reserved.