1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
5f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
7f08c3bdfSopenharmony_ci *
8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
9f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Further, this software is distributed without any warranty that it is
13f08c3bdfSopenharmony_ci * free of the rightful claim of any third person regarding infringement
14f08c3bdfSopenharmony_ci * or the like.  Any license provided herein, whether implied or
15f08c3bdfSopenharmony_ci * otherwise, applies only to this software file.  Patent licenses, if
16f08c3bdfSopenharmony_ci * any, provided herein do not apply to combinations of this program with
17f08c3bdfSopenharmony_ci * other software, or any other product whatsoever.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
20f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
21f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24f08c3bdfSopenharmony_ci * Mountain View, CA  94043, or:
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci * http://www.sgi.com
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * For further information regarding this notice, see:
29f08c3bdfSopenharmony_ci *
30f08c3bdfSopenharmony_ci * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci */
33f08c3bdfSopenharmony_ci/* $Id: setpgid01.c,v 1.7 2009/11/02 13:57:18 subrata_modak Exp $ */
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci/*
36f08c3bdfSopenharmony_ci * Description:
37f08c3bdfSopenharmony_ci * Verify that:
38f08c3bdfSopenharmony_ci *   1. Basic functionality test for setpgid(2).
39f08c3bdfSopenharmony_ci *   2. Check functioning of setpgid(2) with pid = 0 and pgid = 0.
40f08c3bdfSopenharmony_ci */
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci#include <errno.h>
43f08c3bdfSopenharmony_ci#include <string.h>
44f08c3bdfSopenharmony_ci#include <signal.h>
45f08c3bdfSopenharmony_ci#include <stdlib.h>
46f08c3bdfSopenharmony_ci#include <sys/wait.h>
47f08c3bdfSopenharmony_ci#include "test.h"
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_cistatic void setup(void);
50f08c3bdfSopenharmony_cistatic void cleanup(void);
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cichar *TCID = "setpgid01";
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cistatic void setpgid_test1(void);
55f08c3bdfSopenharmony_cistatic void setpgid_test2(void);
56f08c3bdfSopenharmony_cistatic void (*testfunc[])(void) = { setpgid_test1, setpgid_test2};
57f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(testfunc);
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ciint main(int ac, char **av)
60f08c3bdfSopenharmony_ci{
61f08c3bdfSopenharmony_ci	int i, lc;
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	setup();
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
68f08c3bdfSopenharmony_ci		tst_count = 0;
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci		for (i = 0; i < TST_TOTAL; i++)
71f08c3bdfSopenharmony_ci			(*testfunc[i])();
72f08c3bdfSopenharmony_ci	}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	cleanup();
75f08c3bdfSopenharmony_ci	tst_exit();
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic void setpgid_test1(void)
79f08c3bdfSopenharmony_ci{
80f08c3bdfSopenharmony_ci	pid_t pgid, pid;
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	pgid = getpgrp();
83f08c3bdfSopenharmony_ci	pid = getpid();
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	TEST(setpgid(pid, pgid));
86f08c3bdfSopenharmony_ci	if (TEST_RETURN == -1 || getpgrp() != pgid) {
87f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO, "test setpgid(%d, %d) fail",
88f08c3bdfSopenharmony_ci			 pid, pgid);
89f08c3bdfSopenharmony_ci	} else {
90f08c3bdfSopenharmony_ci		tst_resm(TPASS, "test setpgid(%d, %d) success", pid, pgid);
91f08c3bdfSopenharmony_ci	}
92f08c3bdfSopenharmony_ci}
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_cistatic int wait4child(pid_t child)
95f08c3bdfSopenharmony_ci{
96f08c3bdfSopenharmony_ci	int status;
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	if (waitpid(child, &status, 0) == -1)
99f08c3bdfSopenharmony_ci		tst_resm(TBROK|TERRNO, "waitpid");
100f08c3bdfSopenharmony_ci	if (WIFEXITED(status))
101f08c3bdfSopenharmony_ci		return WEXITSTATUS(status);
102f08c3bdfSopenharmony_ci	else
103f08c3bdfSopenharmony_ci		return status;
104f08c3bdfSopenharmony_ci}
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_cistatic void setpgid_test2(void)
107f08c3bdfSopenharmony_ci{
108f08c3bdfSopenharmony_ci	int ret;
109f08c3bdfSopenharmony_ci	pid_t pgid, pid;
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	pid = FORK_OR_VFORK();
112f08c3bdfSopenharmony_ci	if (pid == -1)
113f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "fork()");
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	if (pid != 0) {
116f08c3bdfSopenharmony_ci		ret = wait4child(pid);
117f08c3bdfSopenharmony_ci	} else {
118f08c3bdfSopenharmony_ci		pid = getpid();
119f08c3bdfSopenharmony_ci		TEST(setpgid(0, 0));
120f08c3bdfSopenharmony_ci		pgid = getpgrp();
121f08c3bdfSopenharmony_ci		if (TEST_RETURN == -1) {
122f08c3bdfSopenharmony_ci			fprintf(stderr, "setpgid(0, 0) fails in "
123f08c3bdfSopenharmony_ci				"child process: %s\n", strerror(TEST_ERRNO));
124f08c3bdfSopenharmony_ci			exit(1);
125f08c3bdfSopenharmony_ci		} else if (pgid != pid) {
126f08c3bdfSopenharmony_ci			fprintf(stderr, "setpgid(0, 0) fails to make PGID"
127f08c3bdfSopenharmony_ci				"equal to PID\n");
128f08c3bdfSopenharmony_ci			exit(1);
129f08c3bdfSopenharmony_ci		} else {
130f08c3bdfSopenharmony_ci			exit(0);
131f08c3bdfSopenharmony_ci		}
132f08c3bdfSopenharmony_ci	}
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci	if (ret == 0)
135f08c3bdfSopenharmony_ci		tst_resm(TPASS, "test setpgid(0, 0) success");
136f08c3bdfSopenharmony_ci	else
137f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "test setpgid(0, 0) fail");
138f08c3bdfSopenharmony_ci}
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_cistatic void setup(void)
142f08c3bdfSopenharmony_ci{
143f08c3bdfSopenharmony_ci	tst_sig(FORK, DEF_HANDLER, cleanup);
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	TEST_PAUSE;
146f08c3bdfSopenharmony_ci}
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_cistatic void cleanup(void)
149f08c3bdfSopenharmony_ci{
150f08c3bdfSopenharmony_ci}
151