1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * NAME
22f08c3bdfSopenharmony_ci *	setpgrp02.c
23f08c3bdfSopenharmony_ci *
24f08c3bdfSopenharmony_ci * DESCRIPTION
25f08c3bdfSopenharmony_ci *	Testcase to check the basic functionality of the setpgrp(2) syscall.
26f08c3bdfSopenharmony_ci *
27f08c3bdfSopenharmony_ci * ALGORITHM
28f08c3bdfSopenharmony_ci *	Check the values that setpgrp() and getpgrp() return. The setpgrp()
29f08c3bdfSopenharmony_ci *	returns 0 on success in Linux, but, in DYNIX/ptx this call returns
30f08c3bdfSopenharmony_ci *	the new pgid.
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci * USAGE:  <for command-line>
33f08c3bdfSopenharmony_ci *  setpgrp02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
34f08c3bdfSopenharmony_ci *     where,  -c n : Run n copies concurrently.
35f08c3bdfSopenharmony_ci *             -f   : Turn off functionality Testing.
36f08c3bdfSopenharmony_ci *             -i n : Execute test n times.
37f08c3bdfSopenharmony_ci *             -I x : Execute test for x seconds.
38f08c3bdfSopenharmony_ci *             -P x : Pause for x seconds between iterations.
39f08c3bdfSopenharmony_ci *             -t   : Turn on syscall timing.
40f08c3bdfSopenharmony_ci *
41f08c3bdfSopenharmony_ci * HISTORY
42f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
43f08c3bdfSopenharmony_ci *
44f08c3bdfSopenharmony_ci * RESTRICTIONS
45f08c3bdfSopenharmony_ci *	None
46f08c3bdfSopenharmony_ci */
47f08c3bdfSopenharmony_ci#include <errno.h>
48f08c3bdfSopenharmony_ci#include <sys/wait.h>
49f08c3bdfSopenharmony_ci#include "test.h"
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cichar *TCID = "setpgrp02";
52f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_civoid setup(void);
55f08c3bdfSopenharmony_civoid cleanup(void);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ciint main(int ac, char **av)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	int lc;
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	int pid, oldpgrp;
62f08c3bdfSopenharmony_ci	int e_code, status, retval = 0;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	setup();
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci		/* reset tst_count in case we are looping */
71f08c3bdfSopenharmony_ci		tst_count = 0;
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci		if ((pid = FORK_OR_VFORK()) == -1) {
74f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "fork() failed");
75f08c3bdfSopenharmony_ci		}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci		if (pid == 0) {	/* child */
78f08c3bdfSopenharmony_ci			oldpgrp = getpgrp();
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci			TEST(setpgrp());
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci			if (TEST_RETURN != 0) {
83f08c3bdfSopenharmony_ci				retval = 1;
84f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "setpgrp() FAILED, errno:%d",
85f08c3bdfSopenharmony_ci					 errno);
86f08c3bdfSopenharmony_ci				continue;
87f08c3bdfSopenharmony_ci			}
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci			if (getpgrp() == oldpgrp) {
90f08c3bdfSopenharmony_ci				retval = 1;
91f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "setpgrp() FAILED to set "
92f08c3bdfSopenharmony_ci					 "new group id");
93f08c3bdfSopenharmony_ci				continue;
94f08c3bdfSopenharmony_ci			} else {
95f08c3bdfSopenharmony_ci				tst_resm(TPASS, "functionality is correct");
96f08c3bdfSopenharmony_ci			}
97f08c3bdfSopenharmony_ci			exit(retval);
98f08c3bdfSopenharmony_ci		} else {	/* parent */
99f08c3bdfSopenharmony_ci			/* wait for the child to finish */
100f08c3bdfSopenharmony_ci			wait(&status);
101f08c3bdfSopenharmony_ci			/* make sure the child returned a good exit status */
102f08c3bdfSopenharmony_ci			e_code = status >> 8;
103f08c3bdfSopenharmony_ci			if ((e_code != 0) || (retval != 0)) {
104f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "Failures reported above");
105f08c3bdfSopenharmony_ci			}
106f08c3bdfSopenharmony_ci			cleanup();
107f08c3bdfSopenharmony_ci		}
108f08c3bdfSopenharmony_ci	}
109f08c3bdfSopenharmony_ci	tst_exit();
110f08c3bdfSopenharmony_ci}
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci/*
113f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test.
114f08c3bdfSopenharmony_ci */
115f08c3bdfSopenharmony_civoid setup(void)
116f08c3bdfSopenharmony_ci{
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ci	tst_sig(FORK, DEF_HANDLER, cleanup);
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ci	TEST_PAUSE;
121f08c3bdfSopenharmony_ci}
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci/*
124f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at
125f08c3bdfSopenharmony_ci *	       completion or premature exit.
126f08c3bdfSopenharmony_ci */
127f08c3bdfSopenharmony_civoid cleanup(void)
128f08c3bdfSopenharmony_ci{
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci}
131