blob: ed8f0e4ad6ecd381697c0327534cbd3bab4248a3 [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 */
Steven Jackson249f4052016-12-13 16:16:00 +000033#include <sys/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;
plars865695b2001-08-27 22:15:12 +000069
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010070 tst_parse_opts(ac, av, NULL, NULL);
robbiewd34d5812005-07-11 22:28:09 +000071#ifdef UCLINUX
72 argv0 = av[0];
73
74 maybe_run_child(&do_child_1, "n", 1);
75 maybe_run_child(&do_child_2, "n", 2);
76#endif
77
plars865695b2001-08-27 22:15:12 +000078 /*
79 * perform global setup for the test
80 */
81 setup();
82
plars865695b2001-08-27 22:15:12 +000083 for (lc = 0; TEST_LOOPING(lc); lc++) {
84
Caspar Zhangd59a6592013-03-07 14:59:12 +080085 /* reset tst_count in case we are looping */
86 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000087
88 /*
89 * When the process group having forked of a child
90 * and then it attached itself to another process
91 * group and tries to setsid
92 */
robbiewd34d5812005-07-11 22:28:09 +000093 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +000094
95 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +000096 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +000097 tst_resm(TFAIL, "Fork failed");
Garrett Cooper2c282152010-12-16 00:55:50 -080098
plars865695b2001-08-27 22:15:12 +000099 }
100 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000101#ifdef UCLINUX
102 if (self_exec(argv0, "n", 1) < 0) {
103 tst_resm(TFAIL, "self_exec failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800104
plars865695b2001-08-27 22:15:12 +0000105 }
robbiewd34d5812005-07-11 22:28:09 +0000106#else
107 do_child_1();
108#endif
plars865695b2001-08-27 22:15:12 +0000109 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000110 if (setpgid(0, 0) < 0) {
111 tst_resm(TFAIL,
112 "setpgid(parent) failed: %s",
113 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000114 fail = 1;
115 }
116
117 if ((ret = wait(&status)) > 0) {
plars865695b2001-08-27 22:15:12 +0000118 if (status != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000119 tst_resm(TFAIL,
120 "Test {%d} exited "
121 "status 0x%0x (wanted 0x0)",
122 ret, status);
plars865695b2001-08-27 22:15:12 +0000123 fail = 1;
124 }
125 }
126 }
127 exit(0);
128 } else {
129 if ((ret = wait(&status)) > 0) {
plars865695b2001-08-27 22:15:12 +0000130 if (status != 0) {
vapier0b343cd2006-02-26 21:47:45 +0000131 tst_resm(TFAIL, "Test {%d} exited "
subrata_modak56207ce2009-03-23 13:35:39 +0000132 "status 0x%0x (wanted 0x0)",
133 ret, status);
plars865695b2001-08-27 22:15:12 +0000134 fail = 1;
135 }
136 }
137 }
138
vapier0b343cd2006-02-26 21:47:45 +0000139 if (!(fail || exno)) {
140 tst_resm(TPASS, "all misc tests passed");
plars865695b2001-08-27 22:15:12 +0000141 }
142 }
143 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800144 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800145
plars865695b2001-08-27 22:15:12 +0000146}
147
148/*
robbiewd34d5812005-07-11 22:28:09 +0000149 * do_child_1()
150 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400151void do_child_1(void)
robbiewd34d5812005-07-11 22:28:09 +0000152{
153 int exno = 0;
154 int retval, ret, status;
155 int pid;
156
157 sleep(1);
subrata_modakbdbaec52009-02-26 12:14:51 +0000158
robbiewd34d5812005-07-11 22:28:09 +0000159 if (setpgid(0, 0) < 0) {
vapierea3adb92006-02-26 21:41:05 +0000160 tst_resm(TFAIL, "setpgid(0,0) failed: %s", strerror(errno));
robbiewd34d5812005-07-11 22:28:09 +0000161 exno = 1;
162 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000163
robbiewd34d5812005-07-11 22:28:09 +0000164 if ((pid = FORK_OR_VFORK()) == -1) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100165 tst_brkm(TFAIL, NULL, "Fork failed");
robbiewd34d5812005-07-11 22:28:09 +0000166 }
167 if (pid == 0) {
168#ifdef UCLINUX
169 if (self_exec(argv0, "n", 2) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100170 tst_brkm(TFAIL, NULL, "self_exec failed");
robbiewd34d5812005-07-11 22:28:09 +0000171 }
172#else
173 do_child_2();
174#endif
175 } else {
176 retval = setpgid(0, getppid());
177 if (retval < 0) {
178 tst_resm(TFAIL, "setpgid failed, errno :%d", errno);
179 exno = 2;
180 }
subrata_modak56207ce2009-03-23 13:35:39 +0000181
robbiewd34d5812005-07-11 22:28:09 +0000182 retval = setsid();
subrata_modak56207ce2009-03-23 13:35:39 +0000183
robbiewd34d5812005-07-11 22:28:09 +0000184 if (errno == EPERM) {
185 tst_resm(TPASS, "setsid SUCCESS to set "
186 "errno to EPERM");
187 } else {
188 tst_resm(TFAIL, "setsid failed, expected %d,"
189 "return %d", -1, errno);
190 exno = 3;
191 }
192 kill(pid, SIGKILL);
193 if ((ret = wait(&status)) > 0) {
robbiewd34d5812005-07-11 22:28:09 +0000194 if (status != 9) {
subrata_modak56207ce2009-03-23 13:35:39 +0000195 tst_resm(TFAIL,
196 "Test {%d} exited status 0x%-x (wanted 0x9)",
197 ret, status);
robbiewd34d5812005-07-11 22:28:09 +0000198 exno = 4;
199 }
200 }
201 }
202 exit(exno);
203}
204
205/*
206 * do_child_2()
207 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400208void do_child_2(void)
robbiewd34d5812005-07-11 22:28:09 +0000209{
subrata_modak56207ce2009-03-23 13:35:39 +0000210 for (;;) ;
robbiewd34d5812005-07-11 22:28:09 +0000211}
212
213/*
plars865695b2001-08-27 22:15:12 +0000214 * setup() - performs all ONE TIME setup for this test
215 */
subrata_modak56207ce2009-03-23 13:35:39 +0000216void setup(void)
plars865695b2001-08-27 22:15:12 +0000217{
Garrett Cooper2c282152010-12-16 00:55:50 -0800218
plars865695b2001-08-27 22:15:12 +0000219 tst_sig(FORK, DEF_HANDLER, cleanup);
220
221 umask(0);
222
plars865695b2001-08-27 22:15:12 +0000223 TEST_PAUSE;
224}
225
226/*
227 * cleanup() - performs all the ONE TIME cleanup for this test at completion
228 * or premature exit
229 */
subrata_modak56207ce2009-03-23 13:35:39 +0000230void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000231{
plars865695b2001-08-27 22:15:12 +0000232
Chris Dearmanec6edca2012-10-17 19:54:01 -0700233}