Passing parameters to system calls

15,888

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

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

Share:
15,888
Jeff
Author by

Jeff

Updated on June 09, 2022

Comments

  • Jeff
    Jeff over 1 year

    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?

  • Jeff
    Jeff over 12 years
    1 refers to the # of parameters, but what does 4 refer to?
  • Nikolai Fetissov
    Nikolai Fetissov over 12 years
    No, 1 is a parameter - stdout file descriptor number in this case, 4 is the length of the buffer, as in write( FILENO_STDOUT, boo, 4 );