System Calls Passing
System Calls Passing
If a system call occur, we have to pass parameter to the Kernal part of the
Operating system.
For example look at the given open() system call:
• C
#include <fcntl.h>
• C
#include <fcntl.h>
#include <stdio.h>
int main()
{
const char* pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;
if (fd == -1) {
perror("Error opening file");
return 1;
}
close(fd);
return 0;
}
• C
#include <stdio.h>
#include <fcntl.h>
int main() {
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;
int params[3];
// Block of data(parameters) in array
params[0] = (int)pathname;
params[1] = flags;
params[2] = mode;
if (fd == -1) {
perror("Error opening file");
return 1;
}
close(fd);
return 0;
}
• C
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;
int fd;
asm volatile(
"mov %1, %%rdi\n"
"mov %2, %%rsi\n"
"mov %3, %%rdx\n"
"mov $2, %%rax\n"
"syscall"
: "=a" (fd)
: "r" (pathname), "r" (flags), "r" (mode)
: "%rdi", "%rsi", "%rdx"
);
if (fd == -1) {
perror("Error opening file");
return 1;
}
close(fd);
return 0;
}