Warm tip: This article is reproduced from serverfault.com, please click

What are the arguments of syscall (2) and their purpose?

发布于 2020-12-07 09:00:23

I came across this wikibook which shows that there can be 7 arguments passed to syscall, and I want to understand what is the purpose of each one of them.

This is not specified in the man page.
Also, I have found a table of the arguments you can pass to each syscall but I don't necessarily understand the common ground between them.

Ideally, I'm looking for an answer that specifies

arg #1 - pid  
arg #2 - ... 

Architecture is x86-64.

Questioner
Eliran Turgeman
Viewed
0
Martin B 2020-12-08 05:14:03

The table you linked to provides the list of arguments you're asking for.

I think what may be confusing you is that syscall doesn't perform a single operation. Rather, it's a general-purpose API for anything you might want to ask the kernel to do -- open a file, map memory, fork a new process, and so on.

The specific operation you want the kernel to do is selected using the syscall number. Just picking the first one from the table, syscall 0 (known as sys_read) reads from a file.

Obviously, the operations performed by the different syscalls require different arguments. Staying with the sys_read example, we see in the table that it takes three arguments: The file descriptor (fd), a pointer to a buffer (buf) and a number of bytes to read (count). So if you wanted to call this syscall, you would do it as follows:

#include <sys/syscall.h>

// Assuming these have been initialized appropriately.
unsigned int fd = ...;
char * buf = ...;
size_t count = ...;

syscall(SYS_read, fd, buf, count);

This answer has more information on when you might want to choose to use syscall. The short version is that the reasons to use syscall are few and far between.

Edit: As @PeterCordes points out, syscall numbers differ between ABIs, so you should use the constants defined in sys/syscall.h instead of hardcoding syscall numbers yourself.