Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to system calls

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?

like image 674
Jeff Avatar asked Apr 24 '11 16:04

Jeff


People also ask

What are the 3 methods to pass parameters to the operating system?

Question: Describe the three general methods used to pass parameters to the OS 1- Pass the parameters in registers 2-Store parameters in a block in memory and address of block passed as a parameter in register 3- Place or push the parameter in a stack then pop off the stack by the operating system The services and ...

How parameters are passed in function calls?

When the function is called, the parameter passed to it must be a variable, and that variable's address is passed to the function. Any time the function's body uses the parameter, it uses the variable at the address that was passed. For example, suppose that inc is defined as follows. int w = 1; inc(w);

What are passing parameters?

parameter passing The mechanism used to pass parameters to a procedure (subroutine) or function. The most common methods are to pass the value of the actual parameter (call by value), or to pass the address of the memory location where the actual parameter is stored (call by reference).


2 Answers

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;
}
like image 111
Nikolai Fetissov Avatar answered Oct 05 '22 17:10

Nikolai Fetissov


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).

like image 35
wallyk Avatar answered Oct 05 '22 16:10

wallyk