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