summaryrefslogtreecommitdiffstats
path: root/scraps/syscall_test.c
blob: 739e5cf2c0e9ddbc781e1308ffc6d4aba9427248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#define _GNU_SOURCE
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/wait.h>
// #include <sys/types.h>
// #include <sys/stat.h>

int cloned_func(void *arg)
{
	printf("hello from clone\n");
	fflush(stdout);
	return 0;
}

int main()
{
	char *temp_filename = "./tempfile";
	char *message = "special message\n";
	char *clone_stack = malloc(1024);
	char *clone_stack_top = clone_stack + 1024;

	// SYS_fork
	printf("testing fork\n");
	fflush(stdout);
	pid_t child_pid = fork();
	if (child_pid == 0) { // if in child
		printf("testing exec\n");
		fflush(stdout);
		// SYS_execve
		execl("/bin/ls", "ls", (char*)0);
		return 0;
	}
	wait(NULL);

	// SYS_clone
	printf("testing clone\n");
	pid_t cloned_pid = clone(cloned_func, clone_stack_top, SIGCHLD, 0);
	wait(NULL);

	// SYS_creat
	printf("testing creat\n");
	int tfd = creat(temp_filename, S_IRWXU);
	close(tfd);

	// SYS_open
	printf("testing open\n");
	tfd = open(temp_filename, O_RDWR);
	write(tfd, message, strlen(message));
	close(tfd);

	// SYS_openat
	printf("testing openat\n");
	tfd = openat(AT_FDCWD, temp_filename, O_RDWR | O_APPEND);
	close(tfd);

	// SYS_unlink
	printf("testing unlink\n");
	unlink(temp_filename);

	// SYS_unlinkat
	printf("testing unlinkat\n");
	tfd = creat(temp_filename, S_IRWXU);
	close(tfd);
	unlinkat(AT_FDCWD, temp_filename, 0);

	return 0;
}