blob: 9af523d88f216096cd977a3189328828e60a5641 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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 */
robbiewe861ea92003-03-26 20:28:36 +000044#define _GNU_SOURCE 1
plars865695b2001-08-27 22:15:12 +000045
plars865695b2001-08-27 22:15:12 +000046#include <errno.h>
robbiewe861ea92003-03-26 20:28:36 +000047#include <unistd.h>
robbiew3a280882003-03-03 15:49:21 +000048#include <stdarg.h>
Steven Jackson249f4052016-12-13 16:16:00 +000049#include <sys/wait.h>
robbiewe861ea92003-03-26 20:28:36 +000050#include <sys/types.h>
51#include "test.h"
plars865695b2001-08-27 22:15:12 +000052
53void setup(void);
54void cleanup(void);
55
56char *TCID = "getpgid01";
57int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000058
robbiewe861ea92003-03-26 20:28:36 +000059int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000060{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020061 int lc;
plars865695b2001-08-27 22:15:12 +000062
subrata_modak56207ce2009-03-23 13:35:39 +000063 register int pgid_0, pgid_1;
64 register int my_pid, my_ppid;
plars865695b2001-08-27 22:15:12 +000065 int ex_stat, fail = 0;
66
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010067 tst_parse_opts(ac, av, NULL, NULL);
plars865695b2001-08-27 22:15:12 +000068
69 setup();
70
plars865695b2001-08-27 22:15:12 +000071 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080072 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000073
Garrett Cooperebf7df22010-12-19 01:52:15 -080074 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) ;
plars865695b2001-08-27 22:15:12 +000078
Garrett Cooperebf7df22010-12-19 01:52:15 -080079 if (WEXITSTATUS(ex_stat) == 0)
plars865695b2001-08-27 22:15:12 +000080 tst_resm(TINFO, "%s PASSED", TCID);
Garrett Cooperebf7df22010-12-19 01:52:15 -080081 else
plars865695b2001-08-27 22:15:12 +000082 tst_resm(TINFO, "%s FAILED", TCID);
plars865695b2001-08-27 22:15:12 +000083
plars865695b2001-08-27 22:15:12 +000084 exit(0);
85 }
86
plars865695b2001-08-27 22:15:12 +000087 tst_resm(TINFO, "Enter block 1");
88 fail = 0;
Garrett Cooperebf7df22010-12-19 01:52:15 -080089 if ((pgid_0 = getpgid(0)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 tst_resm(TFAIL | TERRNO, "getpgid(0) failed");
plars865695b2001-08-27 22:15:12 +000091 fail = 1;
92 }
93
Garrett Cooperebf7df22010-12-19 01:52:15 -080094 if (fail)
plars865695b2001-08-27 22:15:12 +000095 tst_resm(TINFO, "Test block 1: getpgid(0) FAILED");
Garrett Cooperebf7df22010-12-19 01:52:15 -080096 else
plars865695b2001-08-27 22:15:12 +000097 tst_resm(TPASS, "Test block 1: getpgid(0) PASSED");
plars865695b2001-08-27 22:15:12 +000098 tst_resm(TINFO, "Exit block 1");
99
robbiewe861ea92003-03-26 20:28:36 +0000100//block2:
plars865695b2001-08-27 22:15:12 +0000101 tst_resm(TINFO, "Enter block 2");
102 fail = 0;
103
104 my_pid = getpid();
Garrett Cooperebf7df22010-12-19 01:52:15 -0800105 if ((pgid_1 = getpgid(my_pid)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 tst_resm(TFAIL | TERRNO, "getpgid(%d) failed", my_pid);
Garrett Cooper2c282152010-12-16 00:55:50 -0800107
plars865695b2001-08-27 22:15:12 +0000108 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 }
Garrett Cooperebf7df22010-12-19 01:52:15 -0800113 if (fail)
plars865695b2001-08-27 22:15:12 +0000114 tst_resm(TINFO, "Test block 2: getpgid(getpid()) "
115 "FAILED");
Garrett Cooperebf7df22010-12-19 01:52:15 -0800116 else
117 tst_resm(TPASS, "Test block 2: getpgid(getpid()) "
plars865695b2001-08-27 22:15:12 +0000118 "PASSED");
plars865695b2001-08-27 22:15:12 +0000119 tst_resm(TINFO, "Exit block 2");
120
robbiewe861ea92003-03-26 20:28:36 +0000121//block3:
plars865695b2001-08-27 22:15:12 +0000122 tst_resm(TINFO, "Enter block 3");
123 fail = 0;
124
125 my_ppid = getppid();
Garrett Cooperebf7df22010-12-19 01:52:15 -0800126 if ((pgid_1 = getpgid(my_ppid)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 tst_resm(TFAIL | TERRNO, "getpgid(%d) failed", my_ppid);
Garrett Cooper2c282152010-12-16 00:55:50 -0800128
plars865695b2001-08-27 22:15:12 +0000129 if (pgid_0 != pgid_1) {
Garrett Cooperebf7df22010-12-19 01:52:15 -0800130 tst_resm(TFAIL, "getpgid(%d) != getpgid(0) [%d != %d]",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 my_ppid, pgid_1, pgid_0);
plars865695b2001-08-27 22:15:12 +0000132 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
robbiewe861ea92003-03-26 20:28:36 +0000144//block4:
plars865695b2001-08-27 22:15:12 +0000145 tst_resm(TINFO, "Enter block 4");
146 fail = 0;
147
Garrett Cooperebf7df22010-12-19 01:52:15 -0800148 if ((pgid_1 = getpgid(pgid_0)) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 tst_resm(TFAIL | TERRNO, "getpgid(%d) failed", pgid_0);
Garrett Cooper2c282152010-12-16 00:55:50 -0800150
plars865695b2001-08-27 22:15:12 +0000151 if (pgid_0 != pgid_1) {
Garrett Cooperebf7df22010-12-19 01:52:15 -0800152 tst_resm(TFAIL, "getpgid(%d) != getpgid(0) [%d != %d]",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 pgid_0, pgid_1, pgid_0);
plars865695b2001-08-27 22:15:12 +0000154 fail = 1;
155 }
156
Garrett Cooperebf7df22010-12-19 01:52:15 -0800157 if (fail)
plars865695b2001-08-27 22:15:12 +0000158 tst_resm(TINFO, "Test block 4: getpgid(1) FAILED");
Garrett Cooperebf7df22010-12-19 01:52:15 -0800159 else
plars865695b2001-08-27 22:15:12 +0000160 tst_resm(TPASS, "Test block 4: getpgid(1) PASSED");
plars865695b2001-08-27 22:15:12 +0000161 tst_resm(TINFO, "Exit block 4");
162
robbiewe861ea92003-03-26 20:28:36 +0000163//block5:
plars865695b2001-08-27 22:15:12 +0000164 tst_resm(TINFO, "Enter block 5");
165 fail = 0;
166
167 if (getpgid(1) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 tst_resm(TFAIL | TERRNO, "getpgid(1) failed");
plars865695b2001-08-27 22:15:12 +0000169 fail = 1;
170 }
171
Garrett Cooperebf7df22010-12-19 01:52:15 -0800172 if (fail)
plars865695b2001-08-27 22:15:12 +0000173 tst_resm(TINFO, "Test block 5: getpgid(1) FAILED");
Garrett Cooperebf7df22010-12-19 01:52:15 -0800174 else
plars865695b2001-08-27 22:15:12 +0000175 tst_resm(TPASS, "Test block 5: getpgid(1) PASSED");
plars865695b2001-08-27 22:15:12 +0000176 tst_resm(TINFO, "Exit block 5");
177 }
178 cleanup();
Garrett Cooperebf7df22010-12-19 01:52:15 -0800179 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800180
plars865695b2001-08-27 22:15:12 +0000181}
182
Mike Frysingerc57fba52014-04-09 18:56:30 -0400183void setup(void)
plars865695b2001-08-27 22:15:12 +0000184{
Garrett Cooper2c282152010-12-16 00:55:50 -0800185
plars865695b2001-08-27 22:15:12 +0000186 tst_sig(FORK, DEF_HANDLER, cleanup);
187
plars865695b2001-08-27 22:15:12 +0000188 TEST_PAUSE;
189}
190
Mike Frysingerc57fba52014-04-09 18:56:30 -0400191void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000192{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700193}