3

Where can I find the source code of some of the system calls? For example, I am looking for the implementation of fstat as described here.

mahmood
  • 23,197
  • 49
  • 147
  • 242
  • 2
    They're defined in the kernel source using SYSCALL_DEFINEx, for example [fstat here](http://lxr.free-electrons.com/source/fs/stat.c#L203). – Joachim Isaksson Feb 14 '16 at 16:41

1 Answers1

6

A system call is mostly implemented inside the Linux kernel, with a tiny glue code in the C standard library. But see also vdso(7).

From the user-land point of view, a system call (they are listed in syscalls(2)...) is a single machine instruction (often SYSENTER) with some calling conventions (e.g. defining which machine register hold the syscall number - e.g. __NR_stat from /usr/include/asm/unistd_64.h....-, and which other registers contain the arguments to the system call).

Use strace(1) to understand which system calls are done by a given program or process.

The C standard library has a tiny wrapper function (which invokes the kernel, following the ABI, and deals with error reporting & errno).

For stat(2), the C wrapping function is e.g. in stat/stat.c for musl-libc.

Inside the kernel code, most of the work happens in fs/stat.c (e.g. after line 207).

See also this & that answers

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547