Passing parameters to system calls

萝らか妹 提交于 2019-12-22 05:48:17

问题


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

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:

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

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


回答1:


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;
}



回答2:


The prototype for syscall is

   #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. syscall is not the normal interface to a particular system call. Those can be explicitly coded, for example write(fd, buf, len).



来源:https://stackoverflow.com/questions/5771717/passing-parameters-to-system-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!