18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#define _GNU_SOURCE
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <stdio.h>
58c2ecf20Sopenharmony_ci#include <stdlib.h>
68c2ecf20Sopenharmony_ci#include <signal.h>
78c2ecf20Sopenharmony_ci#include <limits.h>
88c2ecf20Sopenharmony_ci#include <unistd.h>
98c2ecf20Sopenharmony_ci#include <errno.h>
108c2ecf20Sopenharmony_ci#include <string.h>
118c2ecf20Sopenharmony_ci#include <fcntl.h>
128c2ecf20Sopenharmony_ci#include <linux/unistd.h>
138c2ecf20Sopenharmony_ci#include <linux/kcmp.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <sys/syscall.h>
168c2ecf20Sopenharmony_ci#include <sys/types.h>
178c2ecf20Sopenharmony_ci#include <sys/stat.h>
188c2ecf20Sopenharmony_ci#include <sys/wait.h>
198c2ecf20Sopenharmony_ci#include <sys/epoll.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "../kselftest.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic long sys_kcmp(int pid1, int pid2, int type, unsigned long fd1, unsigned long fd2)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	return syscall(__NR_kcmp, pid1, pid2, type, fd1, fd2);
268c2ecf20Sopenharmony_ci}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic const unsigned int duped_num = 64;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciint main(int argc, char **argv)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	const char kpath[] = "kcmp-test-file";
338c2ecf20Sopenharmony_ci	struct kcmp_epoll_slot epoll_slot;
348c2ecf20Sopenharmony_ci	struct epoll_event ev;
358c2ecf20Sopenharmony_ci	int pid1, pid2;
368c2ecf20Sopenharmony_ci	int pipefd[2];
378c2ecf20Sopenharmony_ci	int fd1, fd2;
388c2ecf20Sopenharmony_ci	int epollfd;
398c2ecf20Sopenharmony_ci	int status;
408c2ecf20Sopenharmony_ci	int fddup;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	fd1 = open(kpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
438c2ecf20Sopenharmony_ci	pid1 = getpid();
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	if (fd1 < 0) {
468c2ecf20Sopenharmony_ci		perror("Can't create file");
478c2ecf20Sopenharmony_ci		ksft_exit_fail();
488c2ecf20Sopenharmony_ci	}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (pipe(pipefd)) {
518c2ecf20Sopenharmony_ci		perror("Can't create pipe");
528c2ecf20Sopenharmony_ci		ksft_exit_fail();
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	epollfd = epoll_create1(0);
568c2ecf20Sopenharmony_ci	if (epollfd < 0) {
578c2ecf20Sopenharmony_ci		perror("epoll_create1 failed");
588c2ecf20Sopenharmony_ci		ksft_exit_fail();
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	memset(&ev, 0xff, sizeof(ev));
628c2ecf20Sopenharmony_ci	ev.events = EPOLLIN | EPOLLOUT;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, pipefd[0], &ev)) {
658c2ecf20Sopenharmony_ci		perror("epoll_ctl failed");
668c2ecf20Sopenharmony_ci		ksft_exit_fail();
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	fddup = dup2(pipefd[1], duped_num);
708c2ecf20Sopenharmony_ci	if (fddup < 0) {
718c2ecf20Sopenharmony_ci		perror("dup2 failed");
728c2ecf20Sopenharmony_ci		ksft_exit_fail();
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fddup, &ev)) {
768c2ecf20Sopenharmony_ci		perror("epoll_ctl failed");
778c2ecf20Sopenharmony_ci		ksft_exit_fail();
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci	close(fddup);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	pid2 = fork();
828c2ecf20Sopenharmony_ci	if (pid2 < 0) {
838c2ecf20Sopenharmony_ci		perror("fork failed");
848c2ecf20Sopenharmony_ci		ksft_exit_fail();
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	if (!pid2) {
888c2ecf20Sopenharmony_ci		int pid2 = getpid();
898c2ecf20Sopenharmony_ci		int ret;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci		fd2 = open(kpath, O_RDWR, 0644);
928c2ecf20Sopenharmony_ci		if (fd2 < 0) {
938c2ecf20Sopenharmony_ci			perror("Can't open file");
948c2ecf20Sopenharmony_ci			ksft_exit_fail();
958c2ecf20Sopenharmony_ci		}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci		/* An example of output and arguments */
988c2ecf20Sopenharmony_ci		printf("pid1: %6d pid2: %6d FD: %2ld FILES: %2ld VM: %2ld "
998c2ecf20Sopenharmony_ci		       "FS: %2ld SIGHAND: %2ld IO: %2ld SYSVSEM: %2ld "
1008c2ecf20Sopenharmony_ci		       "INV: %2ld\n",
1018c2ecf20Sopenharmony_ci		       pid1, pid2,
1028c2ecf20Sopenharmony_ci		       sys_kcmp(pid1, pid2, KCMP_FILE,		fd1, fd2),
1038c2ecf20Sopenharmony_ci		       sys_kcmp(pid1, pid2, KCMP_FILES,		0, 0),
1048c2ecf20Sopenharmony_ci		       sys_kcmp(pid1, pid2, KCMP_VM,		0, 0),
1058c2ecf20Sopenharmony_ci		       sys_kcmp(pid1, pid2, KCMP_FS,		0, 0),
1068c2ecf20Sopenharmony_ci		       sys_kcmp(pid1, pid2, KCMP_SIGHAND,	0, 0),
1078c2ecf20Sopenharmony_ci		       sys_kcmp(pid1, pid2, KCMP_IO,		0, 0),
1088c2ecf20Sopenharmony_ci		       sys_kcmp(pid1, pid2, KCMP_SYSVSEM,	0, 0),
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci			/* This one should fail */
1118c2ecf20Sopenharmony_ci		       sys_kcmp(pid1, pid2, KCMP_TYPES + 1,	0, 0));
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci		/* This one should return same fd */
1148c2ecf20Sopenharmony_ci		ret = sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd1);
1158c2ecf20Sopenharmony_ci		if (ret) {
1168c2ecf20Sopenharmony_ci			printf("FAIL: 0 expected but %d returned (%s)\n",
1178c2ecf20Sopenharmony_ci				ret, strerror(errno));
1188c2ecf20Sopenharmony_ci			ksft_inc_fail_cnt();
1198c2ecf20Sopenharmony_ci			ret = -1;
1208c2ecf20Sopenharmony_ci		} else {
1218c2ecf20Sopenharmony_ci			printf("PASS: 0 returned as expected\n");
1228c2ecf20Sopenharmony_ci			ksft_inc_pass_cnt();
1238c2ecf20Sopenharmony_ci		}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci		/* Compare with self */
1268c2ecf20Sopenharmony_ci		ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
1278c2ecf20Sopenharmony_ci		if (ret) {
1288c2ecf20Sopenharmony_ci			printf("FAIL: 0 expected but %d returned (%s)\n",
1298c2ecf20Sopenharmony_ci				ret, strerror(errno));
1308c2ecf20Sopenharmony_ci			ksft_inc_fail_cnt();
1318c2ecf20Sopenharmony_ci			ret = -1;
1328c2ecf20Sopenharmony_ci		} else {
1338c2ecf20Sopenharmony_ci			printf("PASS: 0 returned as expected\n");
1348c2ecf20Sopenharmony_ci			ksft_inc_pass_cnt();
1358c2ecf20Sopenharmony_ci		}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		/* Compare epoll target */
1388c2ecf20Sopenharmony_ci		epoll_slot = (struct kcmp_epoll_slot) {
1398c2ecf20Sopenharmony_ci			.efd	= epollfd,
1408c2ecf20Sopenharmony_ci			.tfd	= duped_num,
1418c2ecf20Sopenharmony_ci			.toff	= 0,
1428c2ecf20Sopenharmony_ci		};
1438c2ecf20Sopenharmony_ci		ret = sys_kcmp(pid1, pid1, KCMP_EPOLL_TFD, pipefd[1],
1448c2ecf20Sopenharmony_ci			       (unsigned long)(void *)&epoll_slot);
1458c2ecf20Sopenharmony_ci		if (ret) {
1468c2ecf20Sopenharmony_ci			printf("FAIL: 0 expected but %d returned (%s)\n",
1478c2ecf20Sopenharmony_ci				ret, strerror(errno));
1488c2ecf20Sopenharmony_ci			ksft_inc_fail_cnt();
1498c2ecf20Sopenharmony_ci			ret = -1;
1508c2ecf20Sopenharmony_ci		} else {
1518c2ecf20Sopenharmony_ci			printf("PASS: 0 returned as expected\n");
1528c2ecf20Sopenharmony_ci			ksft_inc_pass_cnt();
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		ksft_print_cnts();
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci		if (ret)
1588c2ecf20Sopenharmony_ci			ksft_exit_fail();
1598c2ecf20Sopenharmony_ci		else
1608c2ecf20Sopenharmony_ci			ksft_exit_pass();
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	waitpid(pid2, &status, P_ALL);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return ksft_exit_pass();
1668c2ecf20Sopenharmony_ci}
167