Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find system call source code?

In Linux where can I find the source code for all system calls given that I have the source tree? Also if I were to want to look up the source code and assembly for a particular system call is there something that I can type in terminal like my_system_call?

like image 274
Dr.Knowitall Avatar asked Apr 13 '12 23:04

Dr.Knowitall


People also ask

Where do system calls come from?

A system call is a way for programs to interact with the operating system. A computer program makes a system call when it makes a request to the operating system's kernel. System call provides the services of the operating system to the user programs via Application Program Interface(API).

Where are the system calls stored in Unix environment?

The program simply sets the identification number of the system call, along with some arguments, in a predefined location (a register named EAX, and arguments are stored inside registers named EBX, ECX, EDX etc.)

Where is open syscall defined?

Definition of the open system call is located in the fs/open.c source code file and looks pretty small for the first view: SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) { if (force_o_largefile()) flags |= O_LARGEFILE; return do_sys_open(AT_FDCWD, filename, flags, mode); }


3 Answers

You'll need the Linux kernel sources in order to see the actual source of the system calls. Manual pages, if installed on your local system, only contain the documentation of the calls and not their source itself.

Unfortunately for you, system calls aren't stored in just one particular location in the whole kernel tree. This is because various system calls can refer to different parts of the system (process management, filesystem management, etc.) and therefore it would be infeasible to store them apart from the part of the tree related to that particular part of the system.

The best thing you can do is look for the SYSCALL_DEFINE[0-6] macro. It is used (obviously) to define the given block of code as a system call. For example, fs/ioctl.c has the following code :

SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
{
/* do freaky ioctl stuff */
}

Such a definition means that the ioctl syscall is declared and takes three arguments. The number next to the SYSCALL_DEFINE means the number of arguments. For example, in the case of getpid(void), declared in kernel/timer.c, we have the following code :

SYSCALL_DEFINE0(getpid)
{
        return task_tgid_vnr(current);
}

Hope that clears things up a little.

like image 190
Daniel Kamil Kozar Avatar answered Oct 10 '22 06:10

Daniel Kamil Kozar


From an application's point of view, a system call is an elementary and atomic operation done by the kernel.

The Assembly Howto explains what is happening, in terms of machine instruction.

Of course, the kernel is doing a lot of things when handling a syscall.

Actually, you almost could believe that the entire kernel code is devoted to handle all system calls (this is not entirely true, but almost; from applications' point of view, the kernel is only visible thru system calls). The other answer by Daniel Kamil Kozar is explaining what kernel function is starting the handling of some system call (but very often, many other parts of the kernel indirectly participate to system calls; for example, the scheduler participates indirectly into implementing fork because it manages the child process created by a successful fork syscall).

like image 39
Basile Starynkevitch Avatar answered Oct 10 '22 05:10

Basile Starynkevitch


I know it's old, but I was searching for the source for _system_call() too and found this tidbit

Actual code for system_call entry point can be found in /usr/src/linux/kernel/sys_call.S Actual code for many of the system calls can be found in /usr/src/linux/kernel/sys.c, and the rest are found elsewhere. find is your friend.

I assume this is dated, because I don't even have that file. However, grep found ENTRY(system_call) in arch/x86/kernel/entry_64.S and seems to be the thing that calls the individual system calls. I'm not up on my intel-syntax x86 asm right now, so you'll have to look and see if this is what you wanted.

like image 2
KitsuneYMG Avatar answered Oct 10 '22 06:10

KitsuneYMG