Synopsis:
In programs written for Linux, a system call is an entry point into the Linux kernel, requesting an action for the calling program. The system call changes processor state from user mode to kernel mode, allowing the processor to access protected kernel memory.
System calls usually result from the execution of a C or C++ library function, which internally invokes the Linux syscall entry point. Each system call is identified by a number. syscall places the call number and any supplied parameters into architecture specific registers and traps to kernel mode.
Many system calls interact with devices attached to the system or virtual devices through device drivers. Each device driver has an associated file in /dev.
Linux System Calls:
-
Sources of information:
- man syscall
- man syscalls
-
man -k system | grep "(2)" | more -- Linux api calls
man -k is equivalent to apropos - man -k system | grep "(3)" | more -- c library calls
-
To find call details, e.g., needed headers, calling parameters, and return value:
man 2 statfs (or other system call name) - Technical description of syscall: syscall(2)
- System Call Table: man 2 syscalls
-
ioctl:
ioctl device interface - Wikipedia
Controlling hardware with ioctls ioctl - Linux man page
Device drivers - tutorial sequence, io-control
-
Devices:
Linux devices are classified as block or character devices, depending on whether they are read and written with blocks of characters or a single character per read/write call. You can see all the devices currently on your Linux machine with the command:
cat /proc/devicesDevices support a universal interface:
- int open(const char* path, int flags)
- ssize_t read(int fd, void* buf, size_t count)
- ssize_t write(int fd, void* buf, size_t count)
- off_t lseek(int fd, off_t offset, int whence) - block devices only
- int close(int fd)
They also support a universal "custom" interface:
- int ioctl(int fd, int request, ...)
The arguments after request, if any, are unique to the device, e.g., the "custom" part. Here's a code fragment from a Windows sockets library I wrote:
size_t Socket::bytesWaiting() { unsigned long int ret; ::ioctlsocket(socket_, FIONREAD, &ret); return (size_t)ret; }
For a Linux program you would replace ioctlsocket with ioctl. - Quick Look at Linux System Programming, ppt
- Examples:
-
More System Call References:
-
Bash Programming References:
-
Other Related References:
creating a character device
C Programming with Directories on Linux
Differences between network calls in Windows and Linux
Creating a Linux Kernel driver with Visual Studio - CodeProject assembler from GCC
more assembler from gcc generate asm from CLang
assembly for syscall
X86 assembly - interface with Linux
Assembly - System Calls
twenty ways to say hello