1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker * NAME
22*49cdfc7eSAndroid Build Coastguard Worker * setsid01.c
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker * Test to check the error and trivial conditions in setsid system call
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * USAGE
28*49cdfc7eSAndroid Build Coastguard Worker * setsid01
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker * RESTRICTIONS
31*49cdfc7eSAndroid Build Coastguard Worker * This test doesn't follow good LTP format - PLEASE FIX!
32*49cdfc7eSAndroid Build Coastguard Worker */
33*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/param.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_FLAG -1
44*49cdfc7eSAndroid Build Coastguard Worker #define USER2 301
45*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_MAXGRP NGROUPS_MAX + 1
46*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_USER 999999
47*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_PID 999999
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "setsid01";
50*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker void do_child_1(void);
53*49cdfc7eSAndroid Build Coastguard Worker void do_child_2(void);
54*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
55*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
56*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)57*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker int pid;
60*49cdfc7eSAndroid Build Coastguard Worker int fail = 0;
61*49cdfc7eSAndroid Build Coastguard Worker int ret, status;
62*49cdfc7eSAndroid Build Coastguard Worker int exno = 0;
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker int lc;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker /*
69*49cdfc7eSAndroid Build Coastguard Worker * perform global setup for the test
70*49cdfc7eSAndroid Build Coastguard Worker */
71*49cdfc7eSAndroid Build Coastguard Worker setup();
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker /* reset tst_count in case we are looping */
76*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker /*
79*49cdfc7eSAndroid Build Coastguard Worker * When the process group having forked of a child
80*49cdfc7eSAndroid Build Coastguard Worker * and then it attached itself to another process
81*49cdfc7eSAndroid Build Coastguard Worker * group and tries to setsid
82*49cdfc7eSAndroid Build Coastguard Worker */
83*49cdfc7eSAndroid Build Coastguard Worker pid = tst_fork();
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
86*49cdfc7eSAndroid Build Coastguard Worker if ((pid = tst_fork()) == -1) {
87*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Fork failed");
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
91*49cdfc7eSAndroid Build Coastguard Worker do_child_1();
92*49cdfc7eSAndroid Build Coastguard Worker } else {
93*49cdfc7eSAndroid Build Coastguard Worker if (setpgid(0, 0) < 0) {
94*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
95*49cdfc7eSAndroid Build Coastguard Worker "setpgid(parent) failed: %s",
96*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
97*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker if ((ret = wait(&status)) > 0) {
101*49cdfc7eSAndroid Build Coastguard Worker if (status != 0) {
102*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
103*49cdfc7eSAndroid Build Coastguard Worker "Test {%d} exited "
104*49cdfc7eSAndroid Build Coastguard Worker "status 0x%0x (wanted 0x0)",
105*49cdfc7eSAndroid Build Coastguard Worker ret, status);
106*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker exit(0);
111*49cdfc7eSAndroid Build Coastguard Worker } else {
112*49cdfc7eSAndroid Build Coastguard Worker if ((ret = wait(&status)) > 0) {
113*49cdfc7eSAndroid Build Coastguard Worker if (status != 0) {
114*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Test {%d} exited "
115*49cdfc7eSAndroid Build Coastguard Worker "status 0x%0x (wanted 0x0)",
116*49cdfc7eSAndroid Build Coastguard Worker ret, status);
117*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker if (!(fail || exno)) {
123*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "all misc tests passed");
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker }
126*49cdfc7eSAndroid Build Coastguard Worker cleanup();
127*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
128*49cdfc7eSAndroid Build Coastguard Worker
129*49cdfc7eSAndroid Build Coastguard Worker }
130*49cdfc7eSAndroid Build Coastguard Worker
131*49cdfc7eSAndroid Build Coastguard Worker /*
132*49cdfc7eSAndroid Build Coastguard Worker * do_child_1()
133*49cdfc7eSAndroid Build Coastguard Worker */
do_child_1(void)134*49cdfc7eSAndroid Build Coastguard Worker void do_child_1(void)
135*49cdfc7eSAndroid Build Coastguard Worker {
136*49cdfc7eSAndroid Build Coastguard Worker int exno = 0;
137*49cdfc7eSAndroid Build Coastguard Worker int retval, ret, status;
138*49cdfc7eSAndroid Build Coastguard Worker int pid;
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
141*49cdfc7eSAndroid Build Coastguard Worker
142*49cdfc7eSAndroid Build Coastguard Worker if (setpgid(0, 0) < 0) {
143*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "setpgid(0,0) failed: %s", strerror(errno));
144*49cdfc7eSAndroid Build Coastguard Worker exno = 1;
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker if ((pid = tst_fork()) == -1) {
148*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, NULL, "Fork failed");
149*49cdfc7eSAndroid Build Coastguard Worker }
150*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
151*49cdfc7eSAndroid Build Coastguard Worker do_child_2();
152*49cdfc7eSAndroid Build Coastguard Worker } else {
153*49cdfc7eSAndroid Build Coastguard Worker retval = setpgid(0, getppid());
154*49cdfc7eSAndroid Build Coastguard Worker if (retval < 0) {
155*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "setpgid failed, errno :%d", errno);
156*49cdfc7eSAndroid Build Coastguard Worker exno = 2;
157*49cdfc7eSAndroid Build Coastguard Worker }
158*49cdfc7eSAndroid Build Coastguard Worker
159*49cdfc7eSAndroid Build Coastguard Worker retval = setsid();
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker if (errno == EPERM) {
162*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "setsid SUCCESS to set "
163*49cdfc7eSAndroid Build Coastguard Worker "errno to EPERM");
164*49cdfc7eSAndroid Build Coastguard Worker } else {
165*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "setsid failed, expected %d, "
166*49cdfc7eSAndroid Build Coastguard Worker "return %d", -1, errno);
167*49cdfc7eSAndroid Build Coastguard Worker exno = 3;
168*49cdfc7eSAndroid Build Coastguard Worker }
169*49cdfc7eSAndroid Build Coastguard Worker kill(pid, SIGKILL);
170*49cdfc7eSAndroid Build Coastguard Worker if ((ret = wait(&status)) > 0) {
171*49cdfc7eSAndroid Build Coastguard Worker if (status != 9) {
172*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
173*49cdfc7eSAndroid Build Coastguard Worker "Test {%d} exited status 0x%-x (wanted 0x9)",
174*49cdfc7eSAndroid Build Coastguard Worker ret, status);
175*49cdfc7eSAndroid Build Coastguard Worker exno = 4;
176*49cdfc7eSAndroid Build Coastguard Worker }
177*49cdfc7eSAndroid Build Coastguard Worker }
178*49cdfc7eSAndroid Build Coastguard Worker }
179*49cdfc7eSAndroid Build Coastguard Worker exit(exno);
180*49cdfc7eSAndroid Build Coastguard Worker }
181*49cdfc7eSAndroid Build Coastguard Worker
182*49cdfc7eSAndroid Build Coastguard Worker /*
183*49cdfc7eSAndroid Build Coastguard Worker * do_child_2()
184*49cdfc7eSAndroid Build Coastguard Worker */
do_child_2(void)185*49cdfc7eSAndroid Build Coastguard Worker void do_child_2(void)
186*49cdfc7eSAndroid Build Coastguard Worker {
187*49cdfc7eSAndroid Build Coastguard Worker for (;;) ;
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker
190*49cdfc7eSAndroid Build Coastguard Worker /*
191*49cdfc7eSAndroid Build Coastguard Worker * setup() - performs all ONE TIME setup for this test
192*49cdfc7eSAndroid Build Coastguard Worker */
setup(void)193*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
194*49cdfc7eSAndroid Build Coastguard Worker {
195*49cdfc7eSAndroid Build Coastguard Worker
196*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
197*49cdfc7eSAndroid Build Coastguard Worker
198*49cdfc7eSAndroid Build Coastguard Worker umask(0);
199*49cdfc7eSAndroid Build Coastguard Worker
200*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
201*49cdfc7eSAndroid Build Coastguard Worker }
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Worker /*
204*49cdfc7eSAndroid Build Coastguard Worker * cleanup() - performs all the ONE TIME cleanup for this test at completion
205*49cdfc7eSAndroid Build Coastguard Worker * or premature exit
206*49cdfc7eSAndroid Build Coastguard Worker */
cleanup(void)207*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
208*49cdfc7eSAndroid Build Coastguard Worker {
209*49cdfc7eSAndroid Build Coastguard Worker
210*49cdfc7eSAndroid Build Coastguard Worker }
211