1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Xiao Yang <yangx.jy@cn.fujitsu.com>
4 * Copyright (c) Linux Test Project, 2016-2024
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify that, kcmp() returns 0 if the processes
11 *
12 * 1. share the same file system information
13 * 2. share I/O context
14 * 3. share the same list of System V semaphore undo operations
15 * 4. share the same address space
16 */
17
18 #define _GNU_SOURCE
19
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <sys/wait.h>
23 #include "tst_test.h"
24 #include "lapi/kcmp.h"
25 #include "lapi/sched.h"
26
27 #define STACK_SIZE (1024*1024)
28
29 static int pid1;
30 static int pid2;
31 static void *stack;
32
33 #define ARGS(x, y) .clone_type = x, .kcmp_type = y, .desc = #x ", " #y
34 static struct tcase {
35 int clone_type;
36 int kcmp_type;
37 char *desc;
38 } tcases[] = {
39 {ARGS(CLONE_VM, KCMP_VM)},
40 {ARGS(CLONE_FS, KCMP_FS)},
41 {ARGS(CLONE_IO, KCMP_IO)},
42 {ARGS(CLONE_SYSVSEM, KCMP_SYSVSEM)}
43 };
44
setup(void)45 static void setup(void)
46 {
47 stack = SAFE_MALLOC(STACK_SIZE);
48 }
49
cleanup(void)50 static void cleanup(void)
51 {
52 free(stack);
53 }
54
do_child(void * arg)55 static int do_child(void *arg)
56 {
57 pid2 = getpid();
58 TST_EXP_PASS(kcmp(pid1, pid2, *(int *)arg, 0, 0));
59 return 0;
60 }
61
verify_kcmp(unsigned int n)62 static void verify_kcmp(unsigned int n)
63 {
64 int res;
65 struct tcase *tc = &tcases[n];
66
67 pid1 = getpid();
68 tst_res(TINFO, "Testing %s", tc->desc);
69
70 res = ltp_clone(tc->clone_type | SIGCHLD, do_child, &tc->kcmp_type,
71 STACK_SIZE, stack);
72 if (res == -1)
73 tst_res(TFAIL | TERRNO, "clone() Failed");
74 }
75
76 static struct tst_test test = {
77 .tcnt = ARRAY_SIZE(tcases),
78 .setup = setup,
79 .cleanup = cleanup,
80 .forks_child = 1,
81 .test = verify_kcmp,
82 };
83