xref: /openbmc/linux/tools/testing/selftests/proc/proc-self-syscall.c (revision 2eb0f624b709e78ec8e2f4c3412947703db99301)
1 #define _GNU_SOURCE
2 #include <unistd.h>
3 #include <sys/syscall.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <stdio.h>
11 
12 static inline ssize_t sys_read(int fd, void *buf, size_t len)
13 {
14 	return syscall(SYS_read, fd, buf, len);
15 }
16 
17 int main(void)
18 {
19 	char buf1[64];
20 	char buf2[64];
21 	int fd;
22 	ssize_t rv;
23 
24 	fd = open("/proc/self/syscall", O_RDONLY);
25 	if (fd == -1) {
26 		if (errno == ENOENT)
27 			return 2;
28 		return 1;
29 	}
30 
31 	/* Do direct system call as libc can wrap anything. */
32 	snprintf(buf1, sizeof(buf1), "%ld 0x%lx 0x%lx 0x%lx",
33 		 (long)SYS_read, (long)fd, (long)buf2, (long)sizeof(buf2));
34 
35 	memset(buf2, 0, sizeof(buf2));
36 	rv = sys_read(fd, buf2, sizeof(buf2));
37 	if (rv < 0)
38 		return 1;
39 	if (rv < strlen(buf1))
40 		return 1;
41 	if (strncmp(buf1, buf2, strlen(buf1)) != 0)
42 		return 1;
43 
44 	return 0;
45 }
46