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