Skip to content
systemcoding.dev
Kernels

How glibc Wrappers Turn Function Calls Into System Calls

glibc wrappers are not thin pass-throughs. See how open becomes openat, exit becomes exit_group, fork becomes clone, and which calls never reach the kernel.

7 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the GNU, Linux and C logos.

When you call open() in a C program, you probably imagine a straight line to a kernel function called open. It is not that simple. The function you call lives in the C library, glibc on most Linux systems, and it does a bit of work before and after the instruction that actually enters the kernel. Sometimes it asks the kernel for something different from what your source says. Knowing how that layer works explains puzzling strace output, why some documented functions have no system call behind them, and why an old kernel can still run new programs. This post uses glibc 2.40 on x86-64 with a 6.12 kernel.

The three jobs of a wrapper

A typical wrapper for a simple call does three things.

  1. Marshal the arguments. It moves the values the C compiler passed into the registers the system call ABI expects: the number in rax, arguments in rdi, rsi, rdx, r10, r8, r9. The C calling convention uses rcx for the fourth argument, but the syscall instruction overwrites rcx, so the kernel convention uses r10 instead. The wrapper copies the value across.

  2. Execute the trap. It runs the syscall instruction. The kernel does its work and returns.

  3. Translate the result. It checks whether the result is in the error range -4095 to -1, stores the positive error number in errno and returns -1, or returns the value unchanged.

For a call like getuid() that is the whole story. glibc generates these wrappers from a short table, sysdeps/unix/syscalls.list, plus hand-written ones in sysdeps/unix/sysv/linux/ where more logic is needed. You can read the source, which is the primary reference for everything below.

The name you call is not always the name the kernel sees

Run a program that calls open under strace, and the trace does not show open:

opener.cc
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
	int fd = open("/etc/hostname", O_RDONLY);

	if (fd >= 0)
		close(fd);
	return 0;
}
Shell
strace -e trace=openat,close ./opener
Text
openat(AT_FDCWD, "/etc/hostname", O_RDONLY) = 3
close(3)                                = 0
+++ exited with 0 +++

glibc implements open by calling the openat system call with AT_FDCWD as the directory file descriptor. The older open system call still exists in the kernel on x86-64 for compatibility with old binaries, but modern glibc uses openat for everything. The reason is that openat is the more general call: open is just openat relative to the current directory. A C library that offers both through one kernel entry point has less code to maintain.

The same pattern shows up in other names:

You callglibc asks the kernel forWhy
openopenatOne general call serves both
stat, lstat, fstatnewfstatat or statx on some buildsThe kernel exposes one flexible call
forkclonefork is clone with a fixed set of flags
exitexit_groupEnd every thread in the process, not just one
pthread_createclone3Create a thread with a new stack and thread-local storage
dup2dup2 or dup3dup3 adds a flags argument

The exact choice can vary by glibc version and architecture, which is why you should not hard-code expectations about the system call from the function name. Run strace on your own build to see which one it uses.

exit is more than a system call

exit() is a good example of a library function that does most of its work in user space. The C standard says exit runs functions registered with atexit, flushes and closes all stdio streams, and removes temporary files. glibc does all of that before it calls the kernel:

exitdemo.cc
#include <stdio.h>
#include <stdlib.h>

static void bye(void)
{
	puts("cleanup ran");
}

int main(void)
{
	atexit(bye);
	puts("main done");
	exit(3);
}
Text
main done
cleanup ran

The last thing glibc does is call the exit_group system call, which terminates every thread in the process. There is also a plain exit system call that ends only the calling thread. A program that wants to skip the library cleanup can call _exit(), which goes straight to exit_group without running the atexit handlers or flushing stdio. Forgetting this difference is a classic cause of lost output: a buffered printf before _exit never reaches the file.

fork is a library story

The fork() function does more than the kernel's clone. glibc's version runs the handlers registered with pthread_atfork, takes internal locks so the child gets a consistent copy of the allocator state, and resets some per-thread data in the child. Only then does it make the system call, which in a trace looks like this:

Text
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f2c1d3a1a10) = 4172

The SIGCHLD in the flags is what makes this a fork rather than a thread: it says the parent should receive that signal when the child exits. The kernel does not have a fork operation at all in the sense C programmers mean, since it provides the general clone and lets the library pick the flags. The process id and addresses in your trace will differ.

Cancellation points and signals

Some wrappers contain extra code because POSIX requires it. A number of calls, including read, write, open, close and nanosleep, are cancellation points: if another thread has requested cancellation of this one with pthread_cancel, the call must act on it. glibc wraps these calls so that before and after the trap it checks the cancellation state and, if needed, unwinds the thread. Since glibc 2.34, which merged the threading library into the main library, this machinery lives in libc itself.

A related case is the restart behavior after a signal. If a signal handler interrupts a blocking call such as read, the kernel may return -EINTR or restart the call automatically depending on how the handler was installed. The wrapper passes EINTR through to your program unchanged; deciding whether to retry belongs to you.

Calls that never enter the kernel

Some functions you might expect to be system calls are not, or are system calls only some of the time.

  • **getpid()** is a real system call. Older glibc versions cached the result in user space, which broke after clone with certain flags, and the cache was removed in glibc 2.25.

  • **time(), gettimeofday() and clock_gettime()** use the vDSO, a small shared object the kernel maps into each process. It reads the time from a page the kernel keeps updated, so no privilege transition occurs. On x86-64 glibc looks up these symbols from the vDSO at startup and calls them through a pointer.

  • **malloc()** usually does not enter the kernel either. It hands out memory from an arena it already owns, and calls brk or mmap only when the arena must grow.

  • **printf()** and the other stdio functions collect output in a buffer, as the earlier exit example showed.

You can confirm the vDSO case by tracing a loop of clock_gettime calls. The trace shows the startup calls and the exit, but not one entry per time query.

The generic escape hatch

For calls glibc has no wrapper for, because the kernel added them more recently than the library, there is the generic syscall() function. It takes the number and the arguments and returns with the standard errno convention:

rawcall.cc
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>

int main(void)
{
	long pid = syscall(SYS_getpid);

	printf("pid %ld\n", pid);
	return 0;
}

The SYS_getpid constant comes from <sys/syscall.h>, which reflects the numbers of the kernel headers glibc was built against. A new system call is available through syscall() as soon as the kernel has it, and a named wrapper follows in a later glibc release. The cost of using syscall() is portability and type safety: the arguments are all long, so the compiler cannot check them.

Why wrappers keep old programs working

The kernel promises not to break programs that use its system call interface. glibc is built so that a single binary can run on kernels with different features. When a wrapper uses a newer system call, such as openat2 or clone3, it can fall back to an older one if the kernel returns ENOSYS. That is also why a program linked against a recent glibc can still start on an older kernel, as long as the library's minimum supported kernel version is met.

When to bypass the wrapper

Skip the wrapper only when you need a call glibc has not wrapped yet, or when you are writing something with no C library, such as a tiny static assembly program or a runtime like Go's that has its own. In every other case, use the wrapper: it handles errno, cancellation, restarts and the fallbacks for you.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.