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 | |
robbiew | f1ca238 | 2003-03-27 20:28:50 +0000 | [diff] [blame] | 31 | #include <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 | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 46 | static struct tst_checkpoint checkpoint; |
| 47 | |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 48 | static void do_child(void); |
| 49 | static void setup(void); |
| 50 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
robbiew | f1ca238 | 2003-03-27 20:28:50 +0000 | [diff] [blame] | 52 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | { |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 54 | int child_pid; |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 55 | int status; |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 56 | int rval; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 57 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 58 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 60 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 61 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 62 | #ifdef UCLINUX |
| 63 | maybe_run_child(&do_child, ""); |
| 64 | #endif |
| 65 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | setup(); |
| 67 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 69 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 70 | tst_count = 0; |
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 | /* Child is in new session we are not alowed to change pgid */ |
| 73 | if ((child_pid = FORK_OR_VFORK()) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | tst_brkm(TBROK, cleanup, "fork() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 76 | if (child_pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 77 | #ifdef UCLINUX |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 78 | if (self_exec(av[0], "") < 0) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 79 | tst_brkm(TBROK, cleanup, "self_exec failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 80 | #else |
| 81 | do_child(); |
| 82 | #endif |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 83 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 85 | TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint); |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 86 | rval = setpgid(child_pid, getppid()); |
| 87 | if (rval == -1 && errno == EPERM) { |
| 88 | tst_resm(TPASS, "setpgid failed with EPERM"); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 89 | } else { |
| 90 | tst_resm(TFAIL, |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 91 | "retval %d, errno %d, expected errno %d", |
| 92 | rval, errno, EPERM); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | } |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 94 | TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 95 | |
| 96 | if (wait(&status) < 0) |
| 97 | tst_resm(TFAIL | TERRNO, "wait() for child 1 failed"); |
| 98 | |
| 99 | if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) |
| 100 | tst_resm(TFAIL, "child 1 failed with status %d", |
| 101 | WEXITSTATUS(status)); |
| 102 | |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 103 | /* Child after exec() we are no longer allowed to set pgid */ |
| 104 | if ((child_pid = FORK_OR_VFORK()) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | tst_resm(TFAIL, "Fork failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 106 | |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 107 | if (child_pid == 0) { |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 108 | if (execlp(TEST_APP, TEST_APP, NULL) < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | perror("exec failed"); |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | exit(127); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | } |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 113 | |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 114 | TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint); |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 115 | rval = setpgid(child_pid, getppid()); |
| 116 | if (rval == -1 && errno == EACCES) { |
| 117 | tst_resm(TPASS, "setpgid failed with EACCES"); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 118 | } else { |
| 119 | tst_resm(TFAIL, |
Cyril Hrubis | 25af3d2 | 2014-01-20 16:31:48 +0100 | [diff] [blame] | 120 | "retval %d, errno %d, expected errno %d", |
| 121 | rval, errno, EACCES); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 122 | } |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 123 | TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint); |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 124 | |
| 125 | if (wait(&status) < 0) |
| 126 | tst_resm(TFAIL | TERRNO, "wait() for child 2 failed"); |
| 127 | |
| 128 | if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) |
| 129 | tst_resm(TFAIL, "child 2 failed with status %d", |
| 130 | WEXITSTATUS(status)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | } |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 134 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 137 | static void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 138 | { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 139 | if (setsid() < 0) { |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 140 | printf("CHILD: setsid() failed, errno: %d\n", errno); |
| 141 | exit(2); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 142 | } |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 143 | |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 144 | TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint); |
| 145 | |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 146 | TST_CHECKPOINT_CHILD_WAIT(&checkpoint); |
| 147 | |
| 148 | exit(0); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 151 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 154 | |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 155 | tst_tmpdir(); |
| 156 | |
Jan Stancek | 4caa631 | 2014-10-06 14:57:56 +0200 | [diff] [blame] | 157 | TST_CHECKPOINT_CREATE(&checkpoint); |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 158 | checkpoint.timeout = 10000; |
| 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | umask(0); |
| 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | TEST_PAUSE; |
| 163 | } |
| 164 | |
Stanislav Kholmanskikh | 6bfe99a | 2013-12-12 13:21:54 +0400 | [diff] [blame] | 165 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | { |
Stanislav Kholmanskikh | 52167a1 | 2013-12-12 13:21:55 +0400 | [diff] [blame] | 167 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 168 | } |