xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/kcmp/kcmp03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Xiao Yang <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker  /*
7*49cdfc7eSAndroid Build Coastguard Worker  * Testname: kcmp03.c
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Description:
10*49cdfc7eSAndroid Build Coastguard Worker  * 1) kcmp() returns 0 if the processes share the same file system information.
11*49cdfc7eSAndroid Build Coastguard Worker  * 2) kcmp() returns 0 if the processes share I/O context.
12*49cdfc7eSAndroid Build Coastguard Worker  * 3) kcmp() returns 0 if the processes share the same list of System V
13*49cdfc7eSAndroid Build Coastguard Worker  *    semaphore undo operations.
14*49cdfc7eSAndroid Build Coastguard Worker  * 4) kcmp() returns 0 if the processes share the same address space.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/kcmp.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #define STACK_SIZE	(1024*1024)
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static int pid1;
29*49cdfc7eSAndroid Build Coastguard Worker static int pid2;
30*49cdfc7eSAndroid Build Coastguard Worker static void *stack;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
33*49cdfc7eSAndroid Build Coastguard Worker 	int clone_type;
34*49cdfc7eSAndroid Build Coastguard Worker 	int kcmp_type;
35*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
36*49cdfc7eSAndroid Build Coastguard Worker 	{CLONE_VM, KCMP_VM},
37*49cdfc7eSAndroid Build Coastguard Worker 	{CLONE_FS, KCMP_FS},
38*49cdfc7eSAndroid Build Coastguard Worker 	{CLONE_IO, KCMP_IO},
39*49cdfc7eSAndroid Build Coastguard Worker 	{CLONE_SYSVSEM, KCMP_SYSVSEM}
40*49cdfc7eSAndroid Build Coastguard Worker };
41*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)42*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	stack = SAFE_MALLOC(STACK_SIZE);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)47*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	free(stack);
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
do_child(void * arg)52*49cdfc7eSAndroid Build Coastguard Worker static int do_child(void *arg)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	pid2 = getpid();
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	TEST(kcmp(pid1, pid2, *(int *)arg, 0, 0));
57*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
58*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "kcmp() failed unexpectedly");
59*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
60*49cdfc7eSAndroid Build Coastguard Worker 	}
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == 0)
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "kcmp() returned the expected value");
64*49cdfc7eSAndroid Build Coastguard Worker 	else
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "kcmp() returned the unexpected value");
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
verify_kcmp(unsigned int n)70*49cdfc7eSAndroid Build Coastguard Worker static void verify_kcmp(unsigned int n)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	int res;
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	pid1 = getpid();
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	res = ltp_clone(tc->clone_type | SIGCHLD, do_child, &tc->kcmp_type,
79*49cdfc7eSAndroid Build Coastguard Worker 			STACK_SIZE, stack);
80*49cdfc7eSAndroid Build Coastguard Worker 	if (res == -1)
81*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "clone() Failed");
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
85*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
86*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
87*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
88*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
89*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_kcmp,
90*49cdfc7eSAndroid Build Coastguard Worker };
91