Skip to content

Lesson 3 of 9 · 26m

Syscalls: The Real Linux API Boundary

Use strace, syscall numbers, and errno behavior to understand what programs ask the kernel to do — and where abstraction layers hide the cost.

Applications do not open files, create sockets, allocate memory mappings, or spawn processes by magic. They ask the kernel through system calls. Everything above that — libc functions, language runtimes, frameworks, and deployment tooling — is convenient wrapping around a narrow boundary. When performance or security behavior stops making sense, look at the syscall stream. It is the closest thing Linux has to ground truth.

The boundary is narrow and expensive

A syscall transitions from user mode to kernel mode through a controlled entry point. The kernel validates arguments, performs privilege checks, copies data across the boundary, executes kernel code, and returns a result in a register. The cost is small per call but enormous when repeated millions of times.

strace -c -o /tmp/write.stat sh -c 'yes x | head -n 10000 >/dev/null'
cat /tmp/write.stat

You will commonly see write, read, mmap, brk, openat, close, futex, and polling syscalls dominate. -c summarizes count, time, and errors; it is the fastest first pass before following individual calls.

libc calls are not syscall names

printf() may buffer in user memory and eventually call write(). malloc() often manages memory in user space and only occasionally extends the heap or maps memory with mmap(). fopen() normally becomes openat() on modern Linux. Do not assume one library call maps to one kernel call.

strace -f -tt -o /tmp/cat.trace cat /etc/passwd >/dev/null
sed -n '1,30p' /tmp/cat.trace

A typical trace shows execve, architecture setup, dynamic linker maps, openat for /etc/passwd, read, write, close, and exit. That sequence is a better description of program behavior than the one-line shell command.

Errors are values crossing the boundary

Linux syscalls report failure through -1 in libc and an errno value such as EACCES, ENOENT, EAGAIN, or EINTR. High-level runtimes translate these into exceptions. The syscall trace preserves the original result:

openat(AT_FDCWD, "/root/private", O_RDONLY) = -1 EACCES (Permission denied)
read(3, 0x7f..., 131072)                  = -1 EAGAIN (Resource temporarily unavailable)

EAGAIN often means nonblocking I/O had no data. EINTR means a signal interrupted a slow call. Neither automatically means application failure.

Syscalls reveal hidden coupling

A program expected to read one file may also read locale data, NSS configuration, shared libraries, timezone files, and randomness. A network client may block in connect, not read. A Java service may spend time in futex because threads contend. strace does not explain intent, but it removes abstraction fog.

Use targeted tracing in production because it can slow a process substantially:

sudo strace -p 1234 -c -f -o /tmp/p1234.stat
sudo timeout 5 strace -p 1234 -f -e trace=network,openat,read,write -o /tmp/p1234.trace

Security lives at this boundary too

Seccomp filters which syscalls a process may make. Capabilities affect privileged operations. Namespaces change what kernel objects a process sees. Containers are not magic isolation objects; they are combinations of these kernel controls applied to ordinary processes.

Field notes

  • Never run broad strace on a latency-critical production process without understanding overhead and data exposure.
  • Traces can contain secrets in filenames, command arguments, or network payloads; sanitize before sharing.
  • ltrace traces library calls, not syscalls, and is unavailable or misleading for many modern static binaries.
  • Static binaries still make syscalls; static linking removes the dynamic loader, not the kernel boundary.

Was this lesson helpful?

Sign in to rate