Skip to content

Linux Foundations / Understand the system underneath ยท Step 22 of 38

Lesson: System callsSwitch
โ† Linux Foundations

System administration

System calls

See how programs request services from the kernel and trace those requests with strace.

A program running in user mode cannot open a file or send a packet on its own. It asks the kernel with a system call, the defined interface between user space and the kernel.

From function to kernel

Most programs do not invoke system calls by hand. They call functions in the C library, such as read() or write(), and the library does the low level step. That step places a call number and arguments where the kernel expects them, then runs a special instruction (syscall on x86-64) that switches to kernel mode. Call numbers differ between architectures.

Not every library function is a system call. strlen() works entirely in user space, and printf() usually collects text in a buffer first, and the library calls write later.

Common calls

  • openat opens a file and returns a file descriptor, a small number.
  • read and write move data through a descriptor.
  • close releases it.
  • clone (or fork) creates a new process.
  • execve replaces the current program with another.

Their manual pages are in section 2, for example man 2 read. The list is in syscalls(2).

Trace them with strace

strace shows the system calls a program makes. You may need to install it.

bash
echo hi > note.txt
strace -e trace=openat,read,write,close cat note.txt
openat(AT_FDCWD, "note.txt", O_RDONLY) = 3
read(3, "hi\n", 131072)                = 3
write(1, "hi\n", 3)                    = 3
read(3, "", 131072)                    = 0
close(3)                               = 0

This is shortened: library loading appears first, and some versions of cat copy with a different call, so you may not see these exact lines.

Read a line as name, arguments, then result after =. Descriptor 1 is standard output. A read that returns 0 means end of file.

Errors

A failed call shows -1 and an error name:

bash
strace -e trace=openat cat missing.txt
openat(AT_FDCWD, "missing.txt", O_RDONLY) = -1 ENOENT (No such file or directory)

The C library stores that error code in errno, and the program usually turns it into a message. EACCES means permission denied.

Useful options

  • strace -f follows child processes.
  • strace -c prints a summary table of calls, errors and time.
  • strace -o trace.txt writes the output to a file.
  • strace -p PID attaches to a running process, which often needs root.

Tracing slows a program, and its output can contain passwords or other private data.

For more on file descriptors and redirection, use the streams and text lab.

Try it

  1. Run strace -c ls and find the most frequent call.
  2. Run strace -e trace=execve sh -c 'ls; ls' and see which programs start. Add -f and compare.
  3. Trace cat on a file that does not exist and find the error name.

Check your understanding

System calls

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.