Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 13: System Calls


handle = open("/tmp/test.txt", O_RDONLY);

ptr = (void*)malloc(150);

bytes = read(handle, ptr, 150);
printf("%s", ptr);

close(handle);
return 0;
}

The sample program opens/tmp/test.txt, reads the first 150 bytes, and writes them to standard
output — a very simple version of the standardUnixheadcommand.

How many system calls does the program use? The only ones that are immediately visible areopen,
read,andclose(their implementation is discussed in Chapter 8). However, theprintfunction is also
implemented by system calls in the standard library. It would, of course, be possible to find out which
system calls are used by reading the source code of the standard library, but this would be tedious. A
simpler option is to use thestracetool, which logs all system calls issued by an application and makes
this information available to programmers — this tool is indispensable when debugging programs. Nat-
urally, the kernel must provide special support for logging system calls as discussed in Section 13.3.3
(not surprisingly, support is also provided in the form of a system call (ptrace); our only interest is in its
output).

The followingstracewrites a list of all issued system calls to the filetest.syscalls^1 :

wolfgang@meitner> strace -o log.txt ./shead

The contents oflog.txtare more voluminous than you might have expected:

execve("./shead", ["./shead"], [/ 27 vars /]) = 0
uname(sys="Linux", node="jupiter", ...) = 0
brk(0) = 0x8049750
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, ..., -1, 0) = 0x40017000
open("/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, st_mode=S_IFREG|0644, st_size=85268, ...) = 0
old_mmap(NULL, 85268, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40018000
close(3) = 0
open("/lib/i686/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\ 1 \ 1 \ 1 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 3 \ 0 \ 3 \ 0 \ 1 \ 0 \ 0 \ 0 \ 200 \302"..., 1024) = 1024
fstat64(3, st_mode=S_IFREG|0755, st_size=5634864, ...) = 0
old_mmap(NULL, 1242920, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x4002d000
mprotect(0x40153000, 38696, PROT_NONE) = 0
old_mmap(0x40153000, 24576, PROT_READ|PROT_WRITE, ..., 3, 0x125000) = 0x40153000
old_mmap(0x40159000, 14120, PROT_READ|PROT_WRITE, ..., -1, 0) = 0x40159000
close(3) = 0
munmap(0x40018000, 85268) = 0
getpid() = 10604
open("/tmp/test.txt", O_RDONLY) = 3
brk(0) = 0x8049750


(^1) stracehas other options to specify exactly which data are saved; they are documented in thestrace(1)manual page.

Free download pdf