1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
4 * Copyright (c) Linux Test Project, 2015-2024
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify that, kcmp() returns -1 and sets errno to
11 *
12 * 1. ESRCH if pid does not exist
13 * 2. EINVAL if type is invalid (KCMP_TYPES + 1)
14 * 3. EINVAL if type is invalid (-1)
15 * 4. EINVAL if type is invalid (INT_MIN)
16 * 5. EINVAL if type is invalid (INT_MAX)
17 * 6. EBADF if file descriptor is invalid
18 */
19
20 #define _GNU_SOURCE
21
22 #include "tst_test.h"
23 #include "lapi/fcntl.h"
24 #include "lapi/kcmp.h"
25
26 #define TEST_FILE "test_file"
27 #define TEST_FILE2 "test_file2"
28
29 static int fd1;
30 static int fd2;
31 static int fd_fake;
32 static int pid1;
33 static int pid_unused;
34 static int fd_fake = -1;
35
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <limits.h>
39
40 #define TYPE_DESC(x) .type = x, .desc = #x
41 static struct test_case {
42 int *pid1;
43 int *pid2;
44 int type;
45 char *desc;
46 int *fd1;
47 int *fd2;
48 int exp_errno;
49 } test_cases[] = {
50 {&pid1, &pid_unused, TYPE_DESC(KCMP_FILE), &fd1, &fd2, ESRCH},
51 {&pid1, &pid1, TYPE_DESC(KCMP_TYPES + 1), &fd1, &fd2, EINVAL},
52 {&pid1, &pid1, TYPE_DESC(-1), &fd1, &fd2, EINVAL},
53 {&pid1, &pid1, TYPE_DESC(INT_MIN), &fd1, &fd2, EINVAL},
54 {&pid1, &pid1, TYPE_DESC(INT_MAX), &fd1, &fd2, EINVAL},
55 {&pid1, &pid1, TYPE_DESC(KCMP_FILE), &fd1, &fd_fake, EBADF}
56 };
57
setup(void)58 static void setup(void)
59 {
60 pid1 = getpid();
61 pid_unused = tst_get_unused_pid();
62
63 fd1 = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR | O_TRUNC, 0644);
64 fd2 = SAFE_OPEN(TEST_FILE2, O_CREAT | O_RDWR | O_TRUNC, 0644);
65 }
66
cleanup(void)67 static void cleanup(void)
68 {
69 if (fd1 > 0)
70 SAFE_CLOSE(fd1);
71
72 if (fd2 > 0)
73 SAFE_CLOSE(fd2);
74 }
75
verify_kcmp(unsigned int n)76 static void verify_kcmp(unsigned int n)
77 {
78 struct test_case *tc = &test_cases[n];
79
80 TST_EXP_FAIL(kcmp(*(tc->pid1), *(tc->pid2), tc->type,
81 *(tc->fd1), *(tc->fd2)), tc->exp_errno, "kcmp(%d,%d,%s,%d,%d)",
82 *tc->pid1, *tc->pid2, tc->desc, *tc->fd1, *tc->fd2);
83 }
84
85 static struct tst_test test = {
86 .tcnt = ARRAY_SIZE(test_cases),
87 .setup = setup,
88 .cleanup = cleanup,
89 .test = verify_kcmp,
90 .needs_tmpdir = 1
91 };
92