~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/kcmp/kcmp_test.c

Version: ~ [ linux-6.0 ] ~ [ linux-5.19.12 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.71 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.146 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.215 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.260 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.295 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.330 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 23 static 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 
 28 static const unsigned int duped_num = 64;
 29 
 30 int 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 }
167 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp