Skip to content

Lesson 2 of 9 · 24m

Syscalls: The User/Kernel Boundary

Why read, write, open, clone, and mmap define the real API of Linux — and how strace turns mysterious behavior into evidence.

Application code does not talk to hardware. It asks the kernel, through system calls, and the kernel decides what is allowed, what blocks, and what fails. Most engineers treat syscalls as trivia until a deploy hangs on a filesystem lock or a service cannot open one more socket; then the syscall boundary becomes the whole incident.

A syscall is a controlled trap into the kernel

openat(AT_FDCWD, "/etc/passwd", O_RDONLY) = 3
read(3, "root:x:0:0:root:/root:/bin/bash
", 4096) = 1050
close(3) = 0

The process enters the kernel with a syscall number and arguments. The kernel validates pointers, permissions, capabilities, and object state, performs the operation, then returns a small integer result or -1 with errno. Your application sees simple APIs; the kernel sees a security boundary crossed millions of times per second.

That is why seemingly innocent operations can be expensive. A loop doing one-byte read() calls does not merely 'use CPU'; it pays a boundary crossing every byte. Buffered I/O exists because syscall overhead is real.

strace is the beginner's microscope

strace -f -tt -o /tmp/app.strace -p 18213
grep -E 'EAGAIN|ENOENT|EACCES|connect\(' /tmp/app.strace | tail

-f follows children and threads, -tt timestamps each call, and -p attaches to a running process. Do this briefly on production: strace can dramatically slow the target because every syscall becomes observable work.

A classic finding:

connect(12, {sa_family=AF_INET, sin_port=443, sin_addr=...}, 16) = -1 EINPROGRESS
poll([{fd=12, events=POLLOUT}], 1, 3000) = 0

Translation: the client started a nonblocking connect, waited three seconds, and timed out. No amount of application-level logging tells you that unless someone remembered to log the timeout distinctly.

Blocking is not always a bug

read() on an empty socket blocks. futex() waits for locks. epoll_wait() is how event loops sleep efficiently. A process at 0% CPU is often correctly parked in the kernel. The diagnostic question is not 'why is it blocked?' but 'what object is it blocked on, and who was supposed to make progress?'

/proc/<pid>/stack (when permitted) and /proc/<pid>/wchan can show kernel wait locations. For user-space locks, gdb thread backtraces or runtime-native tools are better.

Permissions are evaluated at syscall time

Being able to start a process does not imply permission for later actions. A service may open a privileged port before dropping privileges, then fail to reopen a log file after rotation because the new directory mode excludes its runtime user. Syscalls evaluate current credentials, file modes, capabilities, LSM policy, mount flags, and namespace context — a lot of production behavior is accumulated context, not the binary itself.

Field notes

  • EAGAIN means 'not ready now' for nonblocking I/O; treating it as a fatal error creates flaky networking bugs.
  • ENOENT is not always a missing file. It can mean a missing intermediate directory or an interpreter named in a script's shebang.
  • Use strace -c for a cheap syscall profile before diving into a full trace.
  • On production hosts, prefer bpftrace/perf for aggregate questions and short targeted strace for individual-process mysteries.

Was this lesson helpful?

Sign in to rate