blob: 9ed9581cd88130c4fdeb50c3e39f743bbca28c4a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
subrata_modak923b23f2009-11-02 13:57:16 +000033/* $Id: setpgid01.c,v 1.7 2009/11/02 13:57:18 subrata_modak Exp $ */
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080034
35/*
36 * Description:
37 * Verify that:
38 * 1. Basic functionality test for setpgid(2).
Xiaoguang Wang8866f062014-05-19 09:45:15 +080039 * 2. Check functioning of setpgid(2) with pid = 0 and pgid = 0.
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080040 */
plars865695b2001-08-27 22:15:12 +000041
42#include <errno.h>
43#include <string.h>
44#include <signal.h>
45#include <stdlib.h>
Xiaoguang Wang8866f062014-05-19 09:45:15 +080046#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000047#include "test.h"
plars865695b2001-08-27 22:15:12 +000048
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080049static void setup(void);
50static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000051
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020052char *TCID = "setpgid01";
plars865695b2001-08-27 22:15:12 +000053
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080054static void setpgid_test1(void);
Xiaoguang Wang8866f062014-05-19 09:45:15 +080055static void setpgid_test2(void);
56static void (*testfunc[])(void) = { setpgid_test1, setpgid_test2};
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080057int TST_TOTAL = ARRAY_SIZE(testfunc);
plars865695b2001-08-27 22:15:12 +000058
subrata_modak56207ce2009-03-23 13:35:39 +000059int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000060{
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080061 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020062 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000063
Garrett Cooper45e285d2010-11-22 12:19:25 -080064 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080065 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000066
subrata_modak56207ce2009-03-23 13:35:39 +000067 setup();
plars865695b2001-08-27 22:15:12 +000068
subrata_modak56207ce2009-03-23 13:35:39 +000069 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080070 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +000071
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080072 for (i = 0; i < TST_TOTAL; i++)
73 (*testfunc[i])();
Garrett Cooper2c282152010-12-16 00:55:50 -080074 }
plars865695b2001-08-27 22:15:12 +000075
subrata_modak56207ce2009-03-23 13:35:39 +000076 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080077 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080078}
plars865695b2001-08-27 22:15:12 +000079
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080080static void setpgid_test1(void)
plars865695b2001-08-27 22:15:12 +000081{
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080082 pid_t pgid, pid;
plars865695b2001-08-27 22:15:12 +000083
subrata_modak56207ce2009-03-23 13:35:39 +000084 pgid = getpgrp();
85 pid = getpid();
plars865695b2001-08-27 22:15:12 +000086
Xiaoguang Wang700c3f92014-05-19 09:45:14 +080087 TEST(setpgid(pid, pgid));
88 if (TEST_RETURN == -1 || getpgrp() != pgid) {
89 tst_resm(TFAIL | TTERRNO, "test setpgid(%d, %d) fail",
90 pid, pgid);
91 } else {
92 tst_resm(TPASS, "test setpgid(%d, %d) success", pid, pgid);
plars865695b2001-08-27 22:15:12 +000093 }
Garrett Cooper2c282152010-12-16 00:55:50 -080094}
plars865695b2001-08-27 22:15:12 +000095
Xiaoguang Wang8866f062014-05-19 09:45:15 +080096static int wait4child(pid_t child)
97{
98 int status;
99
100 if (waitpid(child, &status, 0) == -1)
101 tst_resm(TBROK|TERRNO, "waitpid");
102 if (WIFEXITED(status))
103 return WEXITSTATUS(status);
104 else
105 return status;
106}
107
108static void setpgid_test2(void)
109{
110 int ret;
111 pid_t pgid, pid;
112
113 pid = FORK_OR_VFORK();
114 if (pid == -1)
115 tst_brkm(TBROK | TERRNO, cleanup, "fork()");
116
117 if (pid != 0) {
118 ret = wait4child(pid);
119 } else {
120 pid = getpid();
121 TEST(setpgid(0, 0));
122 pgid = getpgrp();
123 if (TEST_RETURN == -1) {
124 fprintf(stderr, "setpgid(0, 0) fails in "
125 "child process: %s\n", strerror(TEST_ERRNO));
126 exit(1);
127 } else if (pgid != pid) {
128 fprintf(stderr, "setpgid(0, 0) fails to make PGID"
129 "equal to PID\n");
130 exit(1);
131 } else {
132 exit(0);
133 }
134 }
135
136 if (ret == 0)
137 tst_resm(TPASS, "test setpgid(0, 0) success");
138 else
139 tst_resm(TFAIL, "test setpgid(0, 0) fail");
140}
141
142
Xiaoguang Wang700c3f92014-05-19 09:45:14 +0800143static void setup(void)
plars865695b2001-08-27 22:15:12 +0000144{
Xiaoguang Wang8866f062014-05-19 09:45:15 +0800145 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000146
Xiaoguang Wang700c3f92014-05-19 09:45:14 +0800147 TEST_PAUSE;
148}
149
150static void cleanup(void)
151{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700152}