• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  * 	getpgid01.c
23  *
24  * DESCRIPTION
25  *	Testcase to check the basic functionality of getpgid().
26  *
27  * ALGORITHM
28  * 	block1: Does getpgid(0), and checks for error.
29  * 	block2: Does getpgid(getpid()) and checks for error.
30  * 	block3: Does getpgid(getppid()) and checks for error.
31  * 	block4: Verifies that getpgid(getpgid(0)) == getpgid(0).
32  * 	block5: Does getpgid(1) and checks for error.
33  *
34  * USAGE
35  * 	getpgid01
36  *
37  * HISTORY
38  *	07/2001 Ported by Wayne Boyer
39  *
40  * RESTRICTIONS
41  *	Expects that there are no EPERM limitations on getting the
42  *	process group ID from proc 1 (init).
43  */
44 #define _GNU_SOURCE 1
45 
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdarg.h>
49 #include <sys/wait.h>
50 #include <sys/types.h>
51 #include "test.h"
52 
53 void setup(void);
54 void cleanup(void);
55 
56 char *TCID = "getpgid01";
57 int TST_TOTAL = 1;
58 
main(int ac,char ** av)59 int main(int ac, char **av)
60 {
61 	int lc;
62 
63 	register int pgid_0, pgid_1;
64 	register int my_pid, my_ppid;
65 	int ex_stat, fail = 0;
66 
67 	tst_parse_opts(ac, av, NULL, NULL);
68 
69 	setup();
70 
71 	for (lc = 0; TEST_LOOPING(lc); lc++) {
72 		tst_count = 0;
73 
74 		if ((pgid_0 = FORK_OR_VFORK()) == -1)
75 			tst_brkm(TBROK, cleanup, "fork failed");
76 		if (pgid_0 > 0) {
77 			while ((pgid_0 = wait(&ex_stat)) != -1) ;
78 
79 			if (WEXITSTATUS(ex_stat) == 0)
80 				tst_resm(TINFO, "%s PASSED", TCID);
81 			else
82 				tst_resm(TINFO, "%s FAILED", TCID);
83 
84 			exit(0);
85 		}
86 
87 		tst_resm(TINFO, "Enter block 1");
88 		fail = 0;
89 		if ((pgid_0 = getpgid(0)) == -1) {
90 			tst_resm(TFAIL | TERRNO, "getpgid(0) failed");
91 			fail = 1;
92 		}
93 
94 		if (fail)
95 			tst_resm(TINFO, "Test block 1: getpgid(0) FAILED");
96 		else
97 			tst_resm(TPASS, "Test block 1: getpgid(0) PASSED");
98 		tst_resm(TINFO, "Exit block 1");
99 
100 //block2:
101 		tst_resm(TINFO, "Enter block 2");
102 		fail = 0;
103 
104 		my_pid = getpid();
105 		if ((pgid_1 = getpgid(my_pid)) == -1)
106 			tst_resm(TFAIL | TERRNO, "getpgid(%d) failed", my_pid);
107 
108 		if (pgid_0 != pgid_1) {
109 			tst_resm(TFAIL, "getpgid(my_pid=%d) != getpgid(0) "
110 				 "[%d != %d]", my_pid, pgid_1, pgid_0);
111 			fail = 1;
112 		}
113 		if (fail)
114 			tst_resm(TINFO, "Test block 2: getpgid(getpid()) "
115 				 "FAILED");
116 		else
117 			tst_resm(TPASS, "Test block 2: getpgid(getpid()) "
118 				 "PASSED");
119 		tst_resm(TINFO, "Exit block 2");
120 
121 //block3:
122 		tst_resm(TINFO, "Enter block 3");
123 		fail = 0;
124 
125 		my_ppid = getppid();
126 		if ((pgid_1 = getpgid(my_ppid)) == -1)
127 			tst_resm(TFAIL | TERRNO, "getpgid(%d) failed", my_ppid);
128 
129 		if (pgid_0 != pgid_1) {
130 			tst_resm(TFAIL, "getpgid(%d) != getpgid(0) [%d != %d]",
131 				 my_ppid, pgid_1, pgid_0);
132 			fail = 1;
133 		}
134 
135 		if (fail) {
136 			tst_resm(TINFO, "Test block 3: getpgid(getppid()) "
137 				 "FAILED");
138 		} else {
139 			tst_resm(TPASS, "Test block 3: getpgid(getppid()) "
140 				 "PASSED");
141 		}
142 		tst_resm(TINFO, "Exit block 3");
143 
144 //block4:
145 		tst_resm(TINFO, "Enter block 4");
146 		fail = 0;
147 
148 		if ((pgid_1 = getpgid(pgid_0)) < 0)
149 			tst_resm(TFAIL | TERRNO, "getpgid(%d) failed", pgid_0);
150 
151 		if (pgid_0 != pgid_1) {
152 			tst_resm(TFAIL, "getpgid(%d) != getpgid(0) [%d != %d]",
153 				 pgid_0, pgid_1, pgid_0);
154 			fail = 1;
155 		}
156 
157 		if (fail)
158 			tst_resm(TINFO, "Test block 4: getpgid(1) FAILED");
159 		else
160 			tst_resm(TPASS, "Test block 4: getpgid(1) PASSED");
161 		tst_resm(TINFO, "Exit block 4");
162 
163 //block5:
164 		tst_resm(TINFO, "Enter block 5");
165 		fail = 0;
166 
167 		if (getpgid(1) < 0) {
168 			tst_resm(TFAIL | TERRNO, "getpgid(1) failed");
169 			fail = 1;
170 		}
171 
172 		if (fail)
173 			tst_resm(TINFO, "Test block 5: getpgid(1) FAILED");
174 		else
175 			tst_resm(TPASS, "Test block 5: getpgid(1) PASSED");
176 		tst_resm(TINFO, "Exit block 5");
177 	}
178 	cleanup();
179 	tst_exit();
180 
181 }
182 
setup(void)183 void setup(void)
184 {
185 
186 	tst_sig(FORK, DEF_HANDLER, cleanup);
187 
188 	TEST_PAUSE;
189 }
190 
cleanup(void)191 void cleanup(void)
192 {
193 }
194