Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
0003  *
0004  * Permission to use, copy, modify, and distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 #include <unistd.h>
0017 #include <sys/syscall.h>
0018 #include <sys/types.h>
0019 #include <sys/stat.h>
0020 #include <fcntl.h>
0021 #include <errno.h>
0022 #include <string.h>
0023 #include <stdio.h>
0024 
0025 static inline ssize_t sys_read(int fd, void *buf, size_t len)
0026 {
0027     return syscall(SYS_read, fd, buf, len);
0028 }
0029 
0030 int main(void)
0031 {
0032     char buf1[64];
0033     char buf2[64];
0034     int fd;
0035     ssize_t rv;
0036 
0037     fd = open("/proc/self/syscall", O_RDONLY);
0038     if (fd == -1) {
0039         if (errno == ENOENT)
0040             return 4;
0041         return 1;
0042     }
0043 
0044     /* Do direct system call as libc can wrap anything. */
0045     snprintf(buf1, sizeof(buf1), "%ld 0x%lx 0x%lx 0x%lx",
0046          (long)SYS_read, (long)fd, (long)buf2, (long)sizeof(buf2));
0047 
0048     memset(buf2, 0, sizeof(buf2));
0049     rv = sys_read(fd, buf2, sizeof(buf2));
0050     if (rv < 0)
0051         return 1;
0052     if (rv < strlen(buf1))
0053         return 1;
0054     if (strncmp(buf1, buf2, strlen(buf1)) != 0)
0055         return 1;
0056 
0057     return 0;
0058 }