问题
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