1/*
2 * Copyright (c) 2016 Xiao Yang <yangx.jy@cn.fujitsu.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.
16 */
17
18 /*
19 * Testname: kcmp03.c
20 *
21 * Description:
22 * 1) kcmp() returns 0 if the processes share the same file system information.
23 * 2) kcmp() returns 0 if the processes share I/O context.
24 * 3) kcmp() returns 0 if the processes share the same list of System V
25 *    semaphore undo operations.
26 * 4) kcmp() returns 0 if the processes share the same address space.
27 */
28
29#define _GNU_SOURCE
30
31#include <errno.h>
32#include <stdlib.h>
33#include <sched.h>
34#include <sys/wait.h>
35#include "tst_test.h"
36#include "kcmp.h"
37#include "lapi/sched.h"
38
39#define STACK_SIZE	(1024*1024)
40
41static int pid1;
42static int pid2;
43static void *stack;
44
45static struct tcase {
46	int clone_type;
47	int kcmp_type;
48} tcases[] = {
49	{CLONE_VM, KCMP_VM},
50	{CLONE_FS, KCMP_FS},
51	{CLONE_IO, KCMP_IO},
52	{CLONE_SYSVSEM, KCMP_SYSVSEM}
53};
54
55static void setup(void)
56{
57	stack = SAFE_MALLOC(STACK_SIZE);
58}
59
60static void cleanup(void)
61{
62	free(stack);
63}
64
65static int do_child(void *arg)
66{
67	pid2 = getpid();
68
69	TEST(kcmp(pid1, pid2, *(int *)arg, 0, 0));
70	if (TEST_RETURN == -1) {
71		tst_res(TFAIL | TTERRNO, "kcmp() failed unexpectedly");
72		return 0;
73	}
74
75	if (TEST_RETURN == 0)
76		tst_res(TPASS, "kcmp() returned the expected value");
77	else
78		tst_res(TFAIL, "kcmp() returned the unexpected value");
79
80	return 0;
81}
82
83static void verify_kcmp(unsigned int n)
84{
85	int res;
86
87	struct tcase *tc = &tcases[n];
88
89	pid1 = getpid();
90
91	res = ltp_clone(tc->clone_type | SIGCHLD, do_child, &tc->kcmp_type,
92			STACK_SIZE, stack);
93	if (res == -1)
94		tst_res(TFAIL | TERRNO, "clone() Failed");
95}
96
97static struct tst_test test = {
98	.tcnt = ARRAY_SIZE(tcases),
99	.setup = setup,
100	.cleanup = cleanup,
101	.forks_child = 1,
102	.test = verify_kcmp,
103	.min_kver = "3.5.0"
104};
105