Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1#define _GNU_SOURCE
  2
  3#include <stdio.h>
  4#include <stdlib.h>
  5#include <signal.h>
  6#include <limits.h>
  7#include <unistd.h>
  8#include <errno.h>
  9#include <string.h>
 10#include <fcntl.h>
 11
 12#include <linux/unistd.h>
 13#include <linux/kcmp.h>
 14
 15#include <sys/syscall.h>
 16#include <sys/types.h>
 17#include <sys/stat.h>
 18#include <sys/wait.h>
 
 19
 20#include "../kselftest.h"
 21
 22static long sys_kcmp(int pid1, int pid2, int type, int fd1, int fd2)
 23{
 24	return syscall(__NR_kcmp, pid1, pid2, type, fd1, fd2);
 25}
 26
 
 
 27int main(int argc, char **argv)
 28{
 29	const char kpath[] = "kcmp-test-file";
 
 
 30	int pid1, pid2;
 
 31	int fd1, fd2;
 
 32	int status;
 
 33
 34	fd1 = open(kpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
 35	pid1 = getpid();
 36
 37	if (fd1 < 0) {
 38		perror("Can't create file");
 39		ksft_exit_fail();
 40	}
 41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 42	pid2 = fork();
 43	if (pid2 < 0) {
 44		perror("fork failed");
 45		ksft_exit_fail();
 46	}
 47
 48	if (!pid2) {
 49		int pid2 = getpid();
 50		int ret;
 51
 52		fd2 = open(kpath, O_RDWR, 0644);
 53		if (fd2 < 0) {
 54			perror("Can't open file");
 55			ksft_exit_fail();
 56		}
 57
 58		/* An example of output and arguments */
 59		printf("pid1: %6d pid2: %6d FD: %2ld FILES: %2ld VM: %2ld "
 60		       "FS: %2ld SIGHAND: %2ld IO: %2ld SYSVSEM: %2ld "
 61		       "INV: %2ld\n",
 62		       pid1, pid2,
 63		       sys_kcmp(pid1, pid2, KCMP_FILE,		fd1, fd2),
 64		       sys_kcmp(pid1, pid2, KCMP_FILES,		0, 0),
 65		       sys_kcmp(pid1, pid2, KCMP_VM,		0, 0),
 66		       sys_kcmp(pid1, pid2, KCMP_FS,		0, 0),
 67		       sys_kcmp(pid1, pid2, KCMP_SIGHAND,	0, 0),
 68		       sys_kcmp(pid1, pid2, KCMP_IO,		0, 0),
 69		       sys_kcmp(pid1, pid2, KCMP_SYSVSEM,	0, 0),
 70
 71			/* This one should fail */
 72		       sys_kcmp(pid1, pid2, KCMP_TYPES + 1,	0, 0));
 73
 74		/* This one should return same fd */
 75		ret = sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd1);
 76		if (ret) {
 77			printf("FAIL: 0 expected but %d returned (%s)\n",
 78				ret, strerror(errno));
 79			ksft_inc_fail_cnt();
 80			ret = -1;
 81		} else {
 82			printf("PASS: 0 returned as expected\n");
 83			ksft_inc_pass_cnt();
 84		}
 85
 86		/* Compare with self */
 87		ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 88		if (ret) {
 89			printf("FAIL: 0 expected but %d returned (%s)\n",
 90				ret, strerror(errno));
 91			ksft_inc_fail_cnt();
 92			ret = -1;
 93		} else {
 94			printf("PASS: 0 returned as expected\n");
 95			ksft_inc_pass_cnt();
 96		}
 97
 98		ksft_print_cnts();
 99
100		if (ret)
101			ksft_exit_fail();
102		else
103			ksft_exit_pass();
104	}
105
106	waitpid(pid2, &status, P_ALL);
107
108	return ksft_exit_pass();
109}
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2#define _GNU_SOURCE
  3
  4#include <stdio.h>
  5#include <stdlib.h>
  6#include <signal.h>
  7#include <limits.h>
  8#include <unistd.h>
  9#include <errno.h>
 10#include <string.h>
 11#include <fcntl.h>
 
 12#include <linux/unistd.h>
 13#include <linux/kcmp.h>
 14
 15#include <sys/syscall.h>
 16#include <sys/types.h>
 17#include <sys/stat.h>
 18#include <sys/wait.h>
 19#include <sys/epoll.h>
 20
 21#include "../kselftest.h"
 22
 23static long sys_kcmp(int pid1, int pid2, int type, unsigned long fd1, unsigned long fd2)
 24{
 25	return syscall(__NR_kcmp, pid1, pid2, type, fd1, fd2);
 26}
 27
 28static const unsigned int duped_num = 64;
 29
 30int main(int argc, char **argv)
 31{
 32	const char kpath[] = "kcmp-test-file";
 33	struct kcmp_epoll_slot epoll_slot;
 34	struct epoll_event ev;
 35	int pid1, pid2;
 36	int pipefd[2];
 37	int fd1, fd2;
 38	int epollfd;
 39	int status;
 40	int fddup;
 41
 42	fd1 = open(kpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
 43	pid1 = getpid();
 44
 45	if (fd1 < 0) {
 46		perror("Can't create file");
 47		ksft_exit_fail();
 48	}
 49
 50	if (pipe(pipefd)) {
 51		perror("Can't create pipe");
 52		ksft_exit_fail();
 53	}
 54
 55	epollfd = epoll_create1(0);
 56	if (epollfd < 0) {
 57		perror("epoll_create1 failed");
 58		ksft_exit_fail();
 59	}
 60
 61	memset(&ev, 0xff, sizeof(ev));
 62	ev.events = EPOLLIN | EPOLLOUT;
 63
 64	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, pipefd[0], &ev)) {
 65		perror("epoll_ctl failed");
 66		ksft_exit_fail();
 67	}
 68
 69	fddup = dup2(pipefd[1], duped_num);
 70	if (fddup < 0) {
 71		perror("dup2 failed");
 72		ksft_exit_fail();
 73	}
 74
 75	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fddup, &ev)) {
 76		perror("epoll_ctl failed");
 77		ksft_exit_fail();
 78	}
 79	close(fddup);
 80
 81	pid2 = fork();
 82	if (pid2 < 0) {
 83		perror("fork failed");
 84		ksft_exit_fail();
 85	}
 86
 87	if (!pid2) {
 88		int pid2 = getpid();
 89		int ret;
 90
 91		fd2 = open(kpath, O_RDWR, 0644);
 92		if (fd2 < 0) {
 93			perror("Can't open file");
 94			ksft_exit_fail();
 95		}
 96
 97		/* An example of output and arguments */
 98		printf("pid1: %6d pid2: %6d FD: %2ld FILES: %2ld VM: %2ld "
 99		       "FS: %2ld SIGHAND: %2ld IO: %2ld SYSVSEM: %2ld "
100		       "INV: %2ld\n",
101		       pid1, pid2,
102		       sys_kcmp(pid1, pid2, KCMP_FILE,		fd1, fd2),
103		       sys_kcmp(pid1, pid2, KCMP_FILES,		0, 0),
104		       sys_kcmp(pid1, pid2, KCMP_VM,		0, 0),
105		       sys_kcmp(pid1, pid2, KCMP_FS,		0, 0),
106		       sys_kcmp(pid1, pid2, KCMP_SIGHAND,	0, 0),
107		       sys_kcmp(pid1, pid2, KCMP_IO,		0, 0),
108		       sys_kcmp(pid1, pid2, KCMP_SYSVSEM,	0, 0),
109
110			/* This one should fail */
111		       sys_kcmp(pid1, pid2, KCMP_TYPES + 1,	0, 0));
112
113		/* This one should return same fd */
114		ret = sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd1);
115		if (ret) {
116			printf("FAIL: 0 expected but %d returned (%s)\n",
117				ret, strerror(errno));
118			ksft_inc_fail_cnt();
119			ret = -1;
120		} else {
121			printf("PASS: 0 returned as expected\n");
122			ksft_inc_pass_cnt();
123		}
124
125		/* Compare with self */
126		ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
127		if (ret) {
128			printf("FAIL: 0 expected but %d returned (%s)\n",
129				ret, strerror(errno));
130			ksft_inc_fail_cnt();
131			ret = -1;
132		} else {
133			printf("PASS: 0 returned as expected\n");
134			ksft_inc_pass_cnt();
135		}
136
137		/* Compare epoll target */
138		epoll_slot = (struct kcmp_epoll_slot) {
139			.efd	= epollfd,
140			.tfd	= duped_num,
141			.toff	= 0,
142		};
143		ret = sys_kcmp(pid1, pid1, KCMP_EPOLL_TFD, pipefd[1],
144			       (unsigned long)(void *)&epoll_slot);
145		if (ret) {
146			printf("FAIL: 0 expected but %d returned (%s)\n",
147				ret, strerror(errno));
148			ksft_inc_fail_cnt();
149			ret = -1;
150		} else {
151			printf("PASS: 0 returned as expected\n");
152			ksft_inc_pass_cnt();
153		}
154
155		ksft_print_cnts();
156
157		if (ret)
158			ksft_exit_fail();
159		else
160			ksft_exit_pass();
161	}
162
163	waitpid(pid2, &status, P_ALL);
164
165	return ksft_exit_pass();
166}