Skip to content

Commit

Permalink
tools/testing/selftests/kcmp/kcmp_test.c: add KCMP_EPOLL_TFD testing
Browse files Browse the repository at this point in the history
KCMP's KCMP_EPOLL_TFD mode merged in commit 0791e36 ("kcmp: add
KCMP_EPOLL_TFD mode to compare epoll target files") we've had no selftest
for it yet (except in criu development list).  Thus add it.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Cyrill Gorcunov <[email protected]>
Cc: Andrey Vagin <[email protected]>
Cc: Pavel Emelyanov <[email protected]>
Cc: Michael Kerrisk <[email protected]>
Cc: Shuah Khan <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
cyrillos authored and torvalds committed Sep 9, 2017
1 parent b4ccec4 commit 66559a6
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions tools/testing/selftests/kcmp/kcmp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,35 @@
#include <errno.h>
#include <string.h>
#include <fcntl.h>

#include <linux/unistd.h>
#include <linux/kcmp.h>

#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/epoll.h>

#include "../kselftest.h"

static long sys_kcmp(int pid1, int pid2, int type, int fd1, int fd2)
static long sys_kcmp(int pid1, int pid2, int type, unsigned long fd1, unsigned long fd2)
{
return syscall(__NR_kcmp, pid1, pid2, type, fd1, fd2);
}

static const unsigned int duped_num = 64;

int main(int argc, char **argv)
{
const char kpath[] = "kcmp-test-file";
struct kcmp_epoll_slot epoll_slot;
struct epoll_event ev;
int pid1, pid2;
int pipefd[2];
int fd1, fd2;
int epollfd;
int status;
int fddup;

fd1 = open(kpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
pid1 = getpid();
Expand All @@ -39,6 +46,37 @@ int main(int argc, char **argv)
ksft_exit_fail();
}

if (pipe(pipefd)) {
perror("Can't create pipe");
ksft_exit_fail();
}

epollfd = epoll_create1(0);
if (epollfd < 0) {
perror("epoll_create1 failed");
ksft_exit_fail();
}

memset(&ev, 0xff, sizeof(ev));
ev.events = EPOLLIN | EPOLLOUT;

if (epoll_ctl(epollfd, EPOLL_CTL_ADD, pipefd[0], &ev)) {
perror("epoll_ctl failed");
ksft_exit_fail();
}

fddup = dup2(pipefd[1], duped_num);
if (fddup < 0) {
perror("dup2 failed");
ksft_exit_fail();
}

if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fddup, &ev)) {
perror("epoll_ctl failed");
ksft_exit_fail();
}
close(fddup);

pid2 = fork();
if (pid2 < 0) {
perror("fork failed");
Expand Down Expand Up @@ -95,6 +133,24 @@ int main(int argc, char **argv)
ksft_inc_pass_cnt();
}

/* Compare epoll target */
epoll_slot = (struct kcmp_epoll_slot) {
.efd = epollfd,
.tfd = duped_num,
.toff = 0,
};
ret = sys_kcmp(pid1, pid1, KCMP_EPOLL_TFD, pipefd[1],
(unsigned long)(void *)&epoll_slot);
if (ret) {
printf("FAIL: 0 expected but %d returned (%s)\n",
ret, strerror(errno));
ksft_inc_fail_cnt();
ret = -1;
} else {
printf("PASS: 0 returned as expected\n");
ksft_inc_pass_cnt();
}

ksft_print_cnts();

if (ret)
Expand Down

0 comments on commit 66559a6

Please sign in to comment.