Linux 将参数传递给系统调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5771717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:48:20  来源:igfitidea点击:

Passing parameters to system calls

clinuxkernelparameter-passingsystem-calls

提问by Jeff

I did a basic helloWorld system call example that had no parameters and was just:

我做了一个基本的 helloWorld 系统调用示例,它没有参数,只是:

int main()
{
   syscall(__NR_helloWorld);
   return 0;
}

But now I am trying to figure out how to pass actual arguments to the system call (ie. a long). What is the format exactly, I tried:

但是现在我想弄清楚如何将实际参数传递给系统调用(即 a long)。究竟是什么格式,我试过:

int main()
{
   long input = 1;
   long result = syscall(__NR_someSysCall, long input, long);
   return 0;
}

Where it takes a longand returns a long, but it is not compiling correctly; what is the correct syntax?

它需要 along并返回 a long,但它没有正确编译;什么是正确的语法?

采纳答案by Nikolai Fetissov

Remove the type names. It's just a function call. Example:

删除类型名称。这只是一个函数调用。例子:

#include <sys/syscall.h>
#include <unistd.h>

int main( int argc, char* argv[] ) {
    char str[] = "boo\n";
    syscall( __NR_write, STDOUT_FILENO, str, sizeof(str) - 1 );
    return 0;
}

回答by wallyk

The prototype for syscallis

的原型syscall

   #define _GNU_SOURCE        /* or _BSD_SOURCE or _SVID_SOURCE */
   #include <unistd.h>
   #include <sys/syscall.h>   /* For SYS_xxx definitions */

   int syscall(int number, ...);

This says that it takes a variable number (and type) of parameters. That depends on the particular system call. syscallis not the normal interface to a particular system call. Those can be explicitly coded, for example write(fd, buf, len).

这表示它需要可变数量(和类型)的参数。这取决于特定的系统调用。 syscall不是特定系统调用的正常接口。那些可以被显式编码,例如write(fd, buf, len)