System Calls
1 Library Routines
Library functions are implementations of high level, frequently used operations. Several methods are collected together into an archive.
Well known archives are at
- /usr/lib
- /lib
1.1 Example Program
#include <stdio.h> int main(void) { printf("hello\n"); return 0; }
with g++ -Wall -o hello hello.cpp
.
1.2 Man Pages
The man
pages section 1 is devoted to programs ("apps"). Section 2
is devoted to system calls; section 3 is devoted to library calls.
1.3 ltrace
man ltrace
- Try
ltrace hello
Even better, tryltrace /bin/ls
1.4 ldd /bin/ls
man ldd
linux-vdso.so.1 => (0x00007fff6ecf2000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f501fdfe000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f501fbf5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f501f82f000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f501f5c2000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f501f3be000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5020059000)
libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f501f1b8000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f501ef9a000)
- Exercise: Explain the above (by the end of this course). What are the numbers in parentheses? What is the arrow (=>) suggesting?
2 System Calls
System calls are "methods" (functions) implemented within the OS kernel. An application process calls these methods in much the same way, but not exactly the same way, as it does its own methods or those from the library.
Calling a system call changes the CPU mode: from user mode to privileged mode. Invoking a system call is translated by the compiler using a special machine instruction designed for that specific purpose: save the current "context", change CPU modes, etc. After performing the syscall, it uses a special machine instruction to return to the calling process.
A system call returns a coded integer, indicating success or specific failure. Caller of a system call should always check this value and take relevant action. A system call almost always changes global variables and/or environment that are visible to other processes. The effects are not undoable.
System calls are designed to be independent of programming languages. On Linux, the C standard library (glibc) provides wrappers for each system call. The wrappers adapt the system call for use in C programs.
2.1 OS as a Collection of SysCalls
- System Calls are calls to methods already built into the OS. The OS has a table of pointers to system calls.
- Listing of SysCalls https://filippo.io/linux-syscall-table/ Searchable; From the git repository syscall-64.tbl.
- https://github.com/torvalds/linux/blob/master/include/linux/syscalls.h
Also at linux-kernel-source-tree/
include/linux/syscalls.h
2.2 CPU Instructions and Registers
- System calls are services that userspace can call to request the kernel to perform something for them (and therefore execute in kernel space).
- CPUs have special instructions to make and return-from system calls.
- While still in userspace, calling a syscall requires writing the arguments to CPU registers. The system call number will always be written in EAX, while the the rest of the arguments will go into EBX, ECX, etc.
2.3 File Sys Related SysCalls
open, close, read, write
- per-process open file table
dup
2.4 Program to Process
- Given a program and arguments, to create a process from it is the most crucial thing that an OS does.
- execve - execute program
int execve(const char *filename, char *const argv[], char *const envp[]);
- What files are "programs"?
- Try
file path-name-to-a-file
- binary executables native to the OS; ELF … for Linux
- or a script starting with shebang
#!
pathname-to-interpreter
- Try
- Parent and child processes
- fork - create a new process
pid_t fork(void);
- vfork - create a child process and block parent
pid_t fork(void);
execve
does not create a process; it takes over the "body" of the calling process.- E.g., when you ran
ls
within a shell, the shell created a child, the child invoked execve. - Humans think "No parents should outlive their child."
- Parent process awaits the childs termination.
- fork - create a new process
2.5 Sys Call Tracing
man strace
- Run the executable with
strace ./hello
.
Towards the end, find a line write(1, "hello\n", 6)
. {"There it
is. The face behind the printf() mask." – from the web.} Each line
that got printed corresponds to a syscall made by ./hello.
Exercise: Learn execve("./hello", ["./hello"], [/* 59 vars */])
.
Even more interesting is strace /bin/ls
2.6 Special Files
- Show and tell on thor.
uname -a
/proc
directory on Linuxls -l /dev/null /dev/zero /dev/random /dev/urandom
2.7 Library Calls v Sys Calls
Lib methods run completely in CPU user mode. Some lib methods are a
more convenient interface for the programmer to the system functions
that do the real work. System calls run in CPU kernel mode on the
user's behalf and are entry points to the kernel. E.g., the library
function printf()
formats the data into strings and writes the
string using the system call write()
.
2.8 Sys Call Files in the Linux src tree
/usr/src/linux/
is usually a sym link to the src tree/usr/src/linux/arch/x86/syscalls/syscall_64.tbl
./syscall_64.tbl/usr/src/linux/include/linux/syscalls.h
./syscalls.h
3 Reference
- Richard Stevens, APUE
- Slides on Sys Calls Tarek Abdelzaher + Vikram Adve, U of Illinois. Highly Recommended Reading.
- https://0xax.gitbooks.io/linux-insides/content/SysCall/linux-syscall-2.html