plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | * |
| 32 | */ |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 33 | /* $Id: setpgid01.c,v 1.7 2009/11/02 13:57:18 subrata_modak Exp $ */ |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Description: |
| 37 | * Verify that: |
| 38 | * 1. Basic functionality test for setpgid(2). |
Xiaoguang Wang | 8866f06 | 2014-05-19 09:45:15 +0800 | [diff] [blame] | 39 | * 2. Check functioning of setpgid(2) with pid = 0 and pgid = 0. |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 40 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | |
| 42 | #include <errno.h> |
| 43 | #include <string.h> |
| 44 | #include <signal.h> |
| 45 | #include <stdlib.h> |
Xiaoguang Wang | 8866f06 | 2014-05-19 09:45:15 +0800 | [diff] [blame] | 46 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 49 | static void setup(void); |
| 50 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 52 | char *TCID = "setpgid01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 54 | static void setpgid_test1(void); |
Xiaoguang Wang | 8866f06 | 2014-05-19 09:45:15 +0800 | [diff] [blame] | 55 | static void setpgid_test2(void); |
| 56 | static void (*testfunc[])(void) = { setpgid_test1, setpgid_test2}; |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 57 | int TST_TOTAL = ARRAY_SIZE(testfunc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | { |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 61 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 62 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 64 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 65 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 70 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 71 | |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 72 | for (i = 0; i < TST_TOTAL; i++) |
| 73 | (*testfunc[i])(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 74 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 77 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 78 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 80 | static void setpgid_test1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | { |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 82 | pid_t pgid, pid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | pgid = getpgrp(); |
| 85 | pid = getpid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 87 | TEST(setpgid(pid, pgid)); |
| 88 | if (TEST_RETURN == -1 || getpgrp() != pgid) { |
| 89 | tst_resm(TFAIL | TTERRNO, "test setpgid(%d, %d) fail", |
| 90 | pid, pgid); |
| 91 | } else { |
| 92 | tst_resm(TPASS, "test setpgid(%d, %d) success", pid, pgid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 94 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
Xiaoguang Wang | 8866f06 | 2014-05-19 09:45:15 +0800 | [diff] [blame] | 96 | static int wait4child(pid_t child) |
| 97 | { |
| 98 | int status; |
| 99 | |
| 100 | if (waitpid(child, &status, 0) == -1) |
| 101 | tst_resm(TBROK|TERRNO, "waitpid"); |
| 102 | if (WIFEXITED(status)) |
| 103 | return WEXITSTATUS(status); |
| 104 | else |
| 105 | return status; |
| 106 | } |
| 107 | |
| 108 | static void setpgid_test2(void) |
| 109 | { |
| 110 | int ret; |
| 111 | pid_t pgid, pid; |
| 112 | |
| 113 | pid = FORK_OR_VFORK(); |
| 114 | if (pid == -1) |
| 115 | tst_brkm(TBROK | TERRNO, cleanup, "fork()"); |
| 116 | |
| 117 | if (pid != 0) { |
| 118 | ret = wait4child(pid); |
| 119 | } else { |
| 120 | pid = getpid(); |
| 121 | TEST(setpgid(0, 0)); |
| 122 | pgid = getpgrp(); |
| 123 | if (TEST_RETURN == -1) { |
| 124 | fprintf(stderr, "setpgid(0, 0) fails in " |
| 125 | "child process: %s\n", strerror(TEST_ERRNO)); |
| 126 | exit(1); |
| 127 | } else if (pgid != pid) { |
| 128 | fprintf(stderr, "setpgid(0, 0) fails to make PGID" |
| 129 | "equal to PID\n"); |
| 130 | exit(1); |
| 131 | } else { |
| 132 | exit(0); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (ret == 0) |
| 137 | tst_resm(TPASS, "test setpgid(0, 0) success"); |
| 138 | else |
| 139 | tst_resm(TFAIL, "test setpgid(0, 0) fail"); |
| 140 | } |
| 141 | |
| 142 | |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 143 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | { |
Xiaoguang Wang | 8866f06 | 2014-05-19 09:45:15 +0800 | [diff] [blame] | 145 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
Xiaoguang Wang | 700c3f9 | 2014-05-19 09:45:14 +0800 | [diff] [blame] | 147 | TEST_PAUSE; |
| 148 | } |
| 149 | |
| 150 | static void cleanup(void) |
| 151 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 152 | } |