xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/proc/proc-self-syscall.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission to use, copy, modify, and distribute this software for any
5*4882a593Smuzhiyun  * purpose with or without fee is hereby granted, provided that the above
6*4882a593Smuzhiyun  * copyright notice and this permission notice appear in all copies.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*4882a593Smuzhiyun  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*4882a593Smuzhiyun  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*4882a593Smuzhiyun  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*4882a593Smuzhiyun  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*4882a593Smuzhiyun  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun #include <unistd.h>
17*4882a593Smuzhiyun #include <sys/syscall.h>
18*4882a593Smuzhiyun #include <sys/types.h>
19*4882a593Smuzhiyun #include <sys/stat.h>
20*4882a593Smuzhiyun #include <fcntl.h>
21*4882a593Smuzhiyun #include <errno.h>
22*4882a593Smuzhiyun #include <string.h>
23*4882a593Smuzhiyun #include <stdio.h>
24*4882a593Smuzhiyun 
sys_read(int fd,void * buf,size_t len)25*4882a593Smuzhiyun static inline ssize_t sys_read(int fd, void *buf, size_t len)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	return syscall(SYS_read, fd, buf, len);
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
main(void)30*4882a593Smuzhiyun int main(void)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	char buf1[64];
33*4882a593Smuzhiyun 	char buf2[64];
34*4882a593Smuzhiyun 	int fd;
35*4882a593Smuzhiyun 	ssize_t rv;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	fd = open("/proc/self/syscall", O_RDONLY);
38*4882a593Smuzhiyun 	if (fd == -1) {
39*4882a593Smuzhiyun 		if (errno == ENOENT)
40*4882a593Smuzhiyun 			return 4;
41*4882a593Smuzhiyun 		return 1;
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	/* Do direct system call as libc can wrap anything. */
45*4882a593Smuzhiyun 	snprintf(buf1, sizeof(buf1), "%ld 0x%lx 0x%lx 0x%lx",
46*4882a593Smuzhiyun 		 (long)SYS_read, (long)fd, (long)buf2, (long)sizeof(buf2));
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	memset(buf2, 0, sizeof(buf2));
49*4882a593Smuzhiyun 	rv = sys_read(fd, buf2, sizeof(buf2));
50*4882a593Smuzhiyun 	if (rv < 0)
51*4882a593Smuzhiyun 		return 1;
52*4882a593Smuzhiyun 	if (rv < strlen(buf1))
53*4882a593Smuzhiyun 		return 1;
54*4882a593Smuzhiyun 	if (strncmp(buf1, buf2, strlen(buf1)) != 0)
55*4882a593Smuzhiyun 		return 1;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	return 0;
58*4882a593Smuzhiyun }
59