plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 3 | * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved. |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 4 | * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 10 | * |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 15 | * |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 22 | * Test to check the error and trivial conditions in setpgid system call |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 23 | * |
| 24 | * EPERM - The calling process, process specified by pid and the target |
| 25 | * process group must be in the same session. |
| 26 | * |
| 27 | * EACCESS - Proccess cannot change process group ID of a child after child |
| 28 | * has performed exec() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | */ |
plars | a90f7cc | 2003-07-08 18:27:58 +0000 | [diff] [blame] | 30 | |
Steven Jackson | 249f405 | 2016-12-13 16:16:00 +0000 | [diff] [blame^] | 31 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | #include <limits.h> |
| 33 | #include <signal.h> |
| 34 | #include <errno.h> |
| 35 | #include <sys/param.h> |
robbiew | f1ca238 | 2003-03-27 20:28:50 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
| 39 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 41 | #define TEST_APP "setpgid03_child" |
| 42 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | char *TCID = "setpgid03"; |
| 44 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 46 | static void do_child(void); |
| 47 | static void setup(void); |
| 48 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
robbiew | f1ca238 | 2003-03-27 20:28:50 +0000 | [diff] [blame] | 50 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | { |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 52 | int child_pid; |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 53 | int status; |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 54 | int rval; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 55 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 57 | tst_parse_opts(ac, av, NULL, NULL); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 58 | #ifdef UCLINUX |
| 59 | maybe_run_child(&do_child, ""); |
| 60 | #endif |
| 61 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | setup(); |
| 63 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 65 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 66 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 68 | /* Child is in new session we are not alowed to change pgid */ |
| 69 | if ((child_pid = FORK_OR_VFORK()) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | tst_brkm(TBROK, cleanup, "fork() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 72 | if (child_pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 73 | #ifdef UCLINUX |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 74 | if (self_exec(av[0], "") < 0) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 75 | tst_brkm(TBROK, cleanup, "self_exec failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 76 | #else |
| 77 | do_child(); |
| 78 | #endif |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 79 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 81 | TST_SAFE_CHECKPOINT_WAIT(cleanup, 0); |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 82 | rval = setpgid(child_pid, getppid()); |
| 83 | if (rval == -1 && errno == EPERM) { |
| 84 | tst_resm(TPASS, "setpgid failed with EPERM"); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 85 | } else { |
| 86 | tst_resm(TFAIL, |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 87 | "retval %d, errno %d, expected errno %d", |
| 88 | rval, errno, EPERM); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | } |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 90 | TST_SAFE_CHECKPOINT_WAKE(cleanup, 0); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 91 | |
| 92 | if (wait(&status) < 0) |
| 93 | tst_resm(TFAIL | TERRNO, "wait() for child 1 failed"); |
| 94 | |
| 95 | if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) |
| 96 | tst_resm(TFAIL, "child 1 failed with status %d", |
| 97 | WEXITSTATUS(status)); |
| 98 | |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 99 | /* Child after exec() we are no longer allowed to set pgid */ |
| 100 | if ((child_pid = FORK_OR_VFORK()) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_resm(TFAIL, "Fork failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 102 | |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 103 | if (child_pid == 0) { |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 104 | if (execlp(TEST_APP, TEST_APP, NULL) < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | perror("exec failed"); |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | exit(127); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | } |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 109 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 110 | TST_SAFE_CHECKPOINT_WAIT(cleanup, 0); |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 111 | rval = setpgid(child_pid, getppid()); |
| 112 | if (rval == -1 && errno == EACCES) { |
| 113 | tst_resm(TPASS, "setpgid failed with EACCES"); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 114 | } else { |
| 115 | tst_resm(TFAIL, |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 116 | "retval %d, errno %d, expected errno %d", |
| 117 | rval, errno, EACCES); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 118 | } |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 119 | TST_SAFE_CHECKPOINT_WAKE(cleanup, 0); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 120 | |
| 121 | if (wait(&status) < 0) |
| 122 | tst_resm(TFAIL | TERRNO, "wait() for child 2 failed"); |
| 123 | |
| 124 | if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) |
| 125 | tst_resm(TFAIL, "child 2 failed with status %d", |
| 126 | WEXITSTATUS(status)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | } |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 128 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 130 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 133 | static void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 134 | { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 135 | if (setsid() < 0) { |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 136 | printf("CHILD: setsid() failed, errno: %d\n", errno); |
| 137 | exit(2); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 138 | } |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 139 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 140 | TST_SAFE_CHECKPOINT_WAKE(NULL, 0); |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 141 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 142 | TST_SAFE_CHECKPOINT_WAIT(NULL, 0); |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 143 | |
| 144 | exit(0); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 147 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 150 | |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 151 | tst_tmpdir(); |
| 152 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 153 | TST_CHECKPOINT_INIT(tst_rmdir); |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | umask(0); |
| 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | TEST_PAUSE; |
| 158 | } |
| 159 | |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 160 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | { |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 162 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 163 | } |