1 /*
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
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
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 */
33 /* $Id: setpgid01.c,v 1.7 2009/11/02 13:57:18 subrata_modak Exp $ */
34
35 /*
36 * Description:
37 * Verify that:
38 * 1. Basic functionality test for setpgid(2).
39 * 2. Check functioning of setpgid(2) with pid = 0 and pgid = 0.
40 */
41
42 #include <errno.h>
43 #include <string.h>
44 #include <signal.h>
45 #include <stdlib.h>
46 #include <sys/wait.h>
47 #include "test.h"
48
49 static void setup(void);
50 static void cleanup(void);
51
52 char *TCID = "setpgid01";
53
54 static void setpgid_test1(void);
55 static void setpgid_test2(void);
56 static void (*testfunc[])(void) = { setpgid_test1, setpgid_test2};
57 int TST_TOTAL = ARRAY_SIZE(testfunc);
58
main(int ac,char ** av)59 int main(int ac, char **av)
60 {
61 int i, lc;
62
63 tst_parse_opts(ac, av, NULL, NULL);
64
65 setup();
66
67 for (lc = 0; TEST_LOOPING(lc); lc++) {
68 tst_count = 0;
69
70 for (i = 0; i < TST_TOTAL; i++)
71 (*testfunc[i])();
72 }
73
74 cleanup();
75 tst_exit();
76 }
77
setpgid_test1(void)78 static void setpgid_test1(void)
79 {
80 pid_t pgid, pid;
81
82 pgid = getpgrp();
83 pid = getpid();
84
85 TEST(setpgid(pid, pgid));
86 if (TEST_RETURN == -1 || getpgrp() != pgid) {
87 tst_resm(TFAIL | TTERRNO, "test setpgid(%d, %d) fail",
88 pid, pgid);
89 } else {
90 tst_resm(TPASS, "test setpgid(%d, %d) success", pid, pgid);
91 }
92 }
93
wait4child(pid_t child)94 static int wait4child(pid_t child)
95 {
96 int status;
97
98 if (waitpid(child, &status, 0) == -1)
99 tst_resm(TBROK|TERRNO, "waitpid");
100 if (WIFEXITED(status))
101 return WEXITSTATUS(status);
102 else
103 return status;
104 }
105
setpgid_test2(void)106 static void setpgid_test2(void)
107 {
108 int ret;
109 pid_t pgid, pid;
110
111 pid = FORK_OR_VFORK();
112 if (pid == -1)
113 tst_brkm(TBROK | TERRNO, cleanup, "fork()");
114
115 if (pid != 0) {
116 ret = wait4child(pid);
117 } else {
118 pid = getpid();
119 TEST(setpgid(0, 0));
120 pgid = getpgrp();
121 if (TEST_RETURN == -1) {
122 fprintf(stderr, "setpgid(0, 0) fails in "
123 "child process: %s\n", strerror(TEST_ERRNO));
124 exit(1);
125 } else if (pgid != pid) {
126 fprintf(stderr, "setpgid(0, 0) fails to make PGID"
127 "equal to PID\n");
128 exit(1);
129 } else {
130 exit(0);
131 }
132 }
133
134 if (ret == 0)
135 tst_resm(TPASS, "test setpgid(0, 0) success");
136 else
137 tst_resm(TFAIL, "test setpgid(0, 0) fail");
138 }
139
140
setup(void)141 static void setup(void)
142 {
143 tst_sig(FORK, DEF_HANDLER, cleanup);
144
145 TEST_PAUSE;
146 }
147
cleanup(void)148 static void cleanup(void)
149 {
150 }
151