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