Created
November 13, 2019 13:37
-
-
Save surinoel/d2ae6ad2750901e9d44999d860f59423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define SYSCALL_NUM 256 | |
void *sys_call_table[SYSCALL_NUM] = { 0 }; | |
int my_open(char *str) { | |
printf("my_open() system call\n"); | |
printf("str = %s\n", str); | |
return 0; | |
} | |
int my_read(int fd, char *buf, int size) { | |
printf("my_read() system call\n"); | |
printf("fd = %d, buf = %s, size = %d\n", fd, buf, size); | |
return 0; | |
} | |
int my_write(int fd, char *buf, int size) { | |
printf("my_write() system call\n"); | |
printf("fd = %d, buf = %s, size = %d\n", fd, buf, size); | |
return 0; | |
} | |
void my_close(int fd) { | |
printf("my_close() system call\n"); | |
} | |
void kill_proc(int signum) { | |
printf("signum = %d\n", signum); | |
} | |
void (*my_signal(int signum, void(*handler)(int)))(int) { | |
printf("my_signal() system call\n"); | |
printf("signum = %d\n", signum); | |
handler(signum); | |
return kill_proc; | |
} | |
void init_sys_call_table(void) { | |
sys_call_table[0] = my_open; | |
sys_call_table[1] = my_read; | |
sys_call_table[2] = my_write; | |
sys_call_table[3] = my_close; | |
sys_call_table[4] = my_signal; | |
} | |
void syscall(int syscall_num) { | |
int fd = 3; | |
int kill_signal = 9; | |
char filename[32] = "test.txt"; | |
char buf[32] = "contents"; | |
int len = strlen(buf); | |
switch (syscall_num) { | |
case 0: | |
((int(*)(char *))sys_call_table[0])(filename); | |
break; | |
case 1: | |
((int(*)(int, char *, int))sys_call_table[1])(fd, buf, len); | |
break; | |
case 2: | |
((int(*)(int, char *, int))sys_call_table[2])(fd, buf, len); | |
break; | |
case 3: | |
((void(*)(int))sys_call_table[3])(fd); | |
break; | |
case 4: | |
((void(*)(int, void(*)(int)))(int)sys_call_table[4])(kill_signal, kill_proc); | |
break; | |
default: | |
printf("There are no system call\n"); | |
break; | |
} | |
} | |
int main(void) { | |
int i; | |
init_sys_call_table(); | |
for (i = 0; i < 5; i++) { | |
syscall(i); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment