blob: 3e6eaf8663062188b92cd30caddf2affe14a5e97 [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 * setsid01.c
23 *
24 * DESCRIPTION
25 * Test to check the error and trivial conditions in setsid system call
26 *
27 * USAGE
28 * setsid01
29 *
30 * RESTRICTIONS
31 * This test doesn't follow good LTP format - PLEASE FIX!
32 */
robbiewfa451a12003-03-27 20:52:36 +000033#include <wait.h>
plars865695b2001-08-27 22:15:12 +000034#include <limits.h>
35#include <signal.h>
36#include <errno.h>
plars865695b2001-08-27 22:15:12 +000037#include <unistd.h>
robbiewfa451a12003-03-27 20:52:36 +000038#include <sys/param.h>
39#include <sys/types.h>
40#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000041#include "test.h"
plars865695b2001-08-27 22:15:12 +000042
43#define INVAL_FLAG -1
44#define USER2 301
45#define INVAL_MAXGRP NGROUPS_MAX + 1
subrata_modakbdbaec52009-02-26 12:14:51 +000046#define INVAL_USER 999999
plars865695b2001-08-27 22:15:12 +000047#define INVAL_PID 999999
48
49char *TCID = "setsid01";
50int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000051
robbiewd34d5812005-07-11 22:28:09 +000052#ifdef UCLINUX
53static char *argv0;
54#endif
55
56void do_child_1(void);
57void do_child_2(void);
plars865695b2001-08-27 22:15:12 +000058void setup(void);
59void cleanup(void);
60
robbiewfa451a12003-03-27 20:52:36 +000061int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000062{
63 int pid;
robbiewd34d5812005-07-11 22:28:09 +000064 int fail = 0;
plars865695b2001-08-27 22:15:12 +000065 int ret, status;
plars865695b2001-08-27 22:15:12 +000066 int exno = 0;
67
Cyril Hrubis89af32a2012-10-24 16:39:11 +020068 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
plars865695b2001-08-27 22:15:12 +000070
Garrett Cooper53740502010-12-16 00:04:01 -080071 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080072 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +000073#ifdef UCLINUX
74 argv0 = av[0];
75
76 maybe_run_child(&do_child_1, "n", 1);
77 maybe_run_child(&do_child_2, "n", 2);
78#endif
79
plars865695b2001-08-27 22:15:12 +000080 /*
81 * perform global setup for the test
82 */
83 setup();
84
plars865695b2001-08-27 22:15:12 +000085 for (lc = 0; TEST_LOOPING(lc); lc++) {
86
Caspar Zhangd59a6592013-03-07 14:59:12 +080087 /* reset tst_count in case we are looping */
88 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000089
90 /*
91 * When the process group having forked of a child
92 * and then it attached itself to another process
93 * group and tries to setsid
94 */
robbiewd34d5812005-07-11 22:28:09 +000095 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +000096
97 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +000098 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +000099 tst_resm(TFAIL, "Fork failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800100
plars865695b2001-08-27 22:15:12 +0000101 }
102 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000103#ifdef UCLINUX
104 if (self_exec(argv0, "n", 1) < 0) {
105 tst_resm(TFAIL, "self_exec failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800106
plars865695b2001-08-27 22:15:12 +0000107 }
robbiewd34d5812005-07-11 22:28:09 +0000108#else
109 do_child_1();
110#endif
plars865695b2001-08-27 22:15:12 +0000111 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000112 if (setpgid(0, 0) < 0) {
113 tst_resm(TFAIL,
114 "setpgid(parent) failed: %s",
115 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000116 fail = 1;
117 }
118
119 if ((ret = wait(&status)) > 0) {
plars865695b2001-08-27 22:15:12 +0000120 if (status != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000121 tst_resm(TFAIL,
122 "Test {%d} exited "
123 "status 0x%0x (wanted 0x0)",
124 ret, status);
plars865695b2001-08-27 22:15:12 +0000125 fail = 1;
126 }
127 }
128 }
129 exit(0);
130 } else {
131 if ((ret = wait(&status)) > 0) {
plars865695b2001-08-27 22:15:12 +0000132 if (status != 0) {
vapier0b343cd2006-02-26 21:47:45 +0000133 tst_resm(TFAIL, "Test {%d} exited "
subrata_modak56207ce2009-03-23 13:35:39 +0000134 "status 0x%0x (wanted 0x0)",
135 ret, status);
plars865695b2001-08-27 22:15:12 +0000136 fail = 1;
137 }
138 }
139 }
140
vapier0b343cd2006-02-26 21:47:45 +0000141 if (!(fail || exno)) {
142 tst_resm(TPASS, "all misc tests passed");
plars865695b2001-08-27 22:15:12 +0000143 }
144 }
145 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800146 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800147
plars865695b2001-08-27 22:15:12 +0000148}
149
150/*
robbiewd34d5812005-07-11 22:28:09 +0000151 * do_child_1()
152 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400153void do_child_1(void)
robbiewd34d5812005-07-11 22:28:09 +0000154{
155 int exno = 0;
156 int retval, ret, status;
157 int pid;
158
159 sleep(1);
subrata_modakbdbaec52009-02-26 12:14:51 +0000160
robbiewd34d5812005-07-11 22:28:09 +0000161 if (setpgid(0, 0) < 0) {
vapierea3adb92006-02-26 21:41:05 +0000162 tst_resm(TFAIL, "setpgid(0,0) failed: %s", strerror(errno));
robbiewd34d5812005-07-11 22:28:09 +0000163 exno = 1;
164 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000165
robbiewd34d5812005-07-11 22:28:09 +0000166 if ((pid = FORK_OR_VFORK()) == -1) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100167 tst_brkm(TFAIL, NULL, "Fork failed");
robbiewd34d5812005-07-11 22:28:09 +0000168 }
169 if (pid == 0) {
170#ifdef UCLINUX
171 if (self_exec(argv0, "n", 2) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100172 tst_brkm(TFAIL, NULL, "self_exec failed");
robbiewd34d5812005-07-11 22:28:09 +0000173 }
174#else
175 do_child_2();
176#endif
177 } else {
178 retval = setpgid(0, getppid());
179 if (retval < 0) {
180 tst_resm(TFAIL, "setpgid failed, errno :%d", errno);
181 exno = 2;
182 }
subrata_modak56207ce2009-03-23 13:35:39 +0000183
robbiewd34d5812005-07-11 22:28:09 +0000184 retval = setsid();
subrata_modak56207ce2009-03-23 13:35:39 +0000185
robbiewd34d5812005-07-11 22:28:09 +0000186 if (errno == EPERM) {
187 tst_resm(TPASS, "setsid SUCCESS to set "
188 "errno to EPERM");
189 } else {
190 tst_resm(TFAIL, "setsid failed, expected %d,"
191 "return %d", -1, errno);
192 exno = 3;
193 }
194 kill(pid, SIGKILL);
195 if ((ret = wait(&status)) > 0) {
robbiewd34d5812005-07-11 22:28:09 +0000196 if (status != 9) {
subrata_modak56207ce2009-03-23 13:35:39 +0000197 tst_resm(TFAIL,
198 "Test {%d} exited status 0x%-x (wanted 0x9)",
199 ret, status);
robbiewd34d5812005-07-11 22:28:09 +0000200 exno = 4;
201 }
202 }
203 }
204 exit(exno);
205}
206
207/*
208 * do_child_2()
209 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400210void do_child_2(void)
robbiewd34d5812005-07-11 22:28:09 +0000211{
subrata_modak56207ce2009-03-23 13:35:39 +0000212 for (;;) ;
robbiewd34d5812005-07-11 22:28:09 +0000213}
214
215/*
plars865695b2001-08-27 22:15:12 +0000216 * setup() - performs all ONE TIME setup for this test
217 */
subrata_modak56207ce2009-03-23 13:35:39 +0000218void setup(void)
plars865695b2001-08-27 22:15:12 +0000219{
Garrett Cooper2c282152010-12-16 00:55:50 -0800220
plars865695b2001-08-27 22:15:12 +0000221 tst_sig(FORK, DEF_HANDLER, cleanup);
222
223 umask(0);
224
plars865695b2001-08-27 22:15:12 +0000225 TEST_PAUSE;
226}
227
228/*
229 * cleanup() - performs all the ONE TIME cleanup for this test at completion
230 * or premature exit
231 */
subrata_modak56207ce2009-03-23 13:35:39 +0000232void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000233{
plars865695b2001-08-27 22:15:12 +0000234
Chris Dearmanec6edca2012-10-17 19:54:01 -0700235}