162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission to use, copy, modify, and distribute this software for any
562306a36Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
662306a36Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
962306a36Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1062306a36Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1162306a36Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1262306a36Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1362306a36Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1462306a36Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci#include <unistd.h>
1762306a36Sopenharmony_ci#include <sys/syscall.h>
1862306a36Sopenharmony_ci#include <sys/types.h>
1962306a36Sopenharmony_ci#include <sys/stat.h>
2062306a36Sopenharmony_ci#include <fcntl.h>
2162306a36Sopenharmony_ci#include <errno.h>
2262306a36Sopenharmony_ci#include <string.h>
2362306a36Sopenharmony_ci#include <stdio.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_cistatic inline ssize_t sys_read(int fd, void *buf, size_t len)
2662306a36Sopenharmony_ci{
2762306a36Sopenharmony_ci	return syscall(SYS_read, fd, buf, len);
2862306a36Sopenharmony_ci}
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ciint main(void)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	char buf1[64];
3362306a36Sopenharmony_ci	char buf2[64];
3462306a36Sopenharmony_ci	int fd;
3562306a36Sopenharmony_ci	ssize_t rv;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	fd = open("/proc/self/syscall", O_RDONLY);
3862306a36Sopenharmony_ci	if (fd == -1) {
3962306a36Sopenharmony_ci		if (errno == ENOENT)
4062306a36Sopenharmony_ci			return 4;
4162306a36Sopenharmony_ci		return 1;
4262306a36Sopenharmony_ci	}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	/* Do direct system call as libc can wrap anything. */
4562306a36Sopenharmony_ci	snprintf(buf1, sizeof(buf1), "%ld 0x%lx 0x%lx 0x%lx",
4662306a36Sopenharmony_ci		 (long)SYS_read, (long)fd, (long)buf2, (long)sizeof(buf2));
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	memset(buf2, 0, sizeof(buf2));
4962306a36Sopenharmony_ci	rv = sys_read(fd, buf2, sizeof(buf2));
5062306a36Sopenharmony_ci	if (rv < 0)
5162306a36Sopenharmony_ci		return 1;
5262306a36Sopenharmony_ci	if (rv < strlen(buf1))
5362306a36Sopenharmony_ci		return 1;
5462306a36Sopenharmony_ci	if (strncmp(buf1, buf2, strlen(buf1)) != 0)
5562306a36Sopenharmony_ci		return 1;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	return 0;
5862306a36Sopenharmony_ci}
59