plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 22 | * setpgrp02.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the basic functionality of the setpgrp(2) syscall. |
| 26 | * |
| 27 | * ALGORITHM |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 28 | * Check the values that setpgrp() and getpgrp() return. The setpgrp() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * returns 0 on success in Linux, but, in DYNIX/ptx this call returns |
| 30 | * the new pgid. |
| 31 | * |
| 32 | * USAGE: <for command-line> |
robbiew | a3b949c | 2001-09-17 18:04:26 +0000 | [diff] [blame] | 33 | * setpgrp02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | * where, -c n : Run n copies concurrently. |
| 35 | * -f : Turn off functionality Testing. |
| 36 | * -i n : Execute test n times. |
| 37 | * -I x : Execute test for x seconds. |
| 38 | * -P x : Pause for x seconds between iterations. |
| 39 | * -t : Turn on syscall timing. |
| 40 | * |
| 41 | * HISTORY |
| 42 | * 07/2001 Ported by Wayne Boyer |
| 43 | * |
| 44 | * RESTRICTIONS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | */ |
| 47 | #include <errno.h> |
robbiew | f1ca238 | 2003-03-27 20:28:50 +0000 | [diff] [blame] | 48 | #include <wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
robbiew | a3b949c | 2001-09-17 18:04:26 +0000 | [diff] [blame] | 51 | char *TCID = "setpgrp02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
| 54 | void setup(void); |
| 55 | void cleanup(void); |
| 56 | |
robbiew | f1ca238 | 2003-03-27 20:28:50 +0000 | [diff] [blame] | 57 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 59 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 60 | const char *msg; /* message returned by parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
robbiew | f1ca238 | 2003-03-27 20:28:50 +0000 | [diff] [blame] | 62 | int pid, oldpgrp; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | int e_code, status, retval = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 65 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 66 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | setup(); |
| 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 72 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 73 | /* reset tst_count in case we are looping */ |
| 74 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 76 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | tst_brkm(TBROK, cleanup, "fork() failed"); |
| 78 | } |
| 79 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | if (pid == 0) { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | oldpgrp = getpgrp(); |
| 82 | |
| 83 | TEST(setpgrp()); |
| 84 | |
| 85 | if (TEST_RETURN != 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | retval = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | tst_resm(TFAIL, "setpgrp() FAILED, errno:%d", |
| 88 | errno); |
| 89 | continue; |
| 90 | } |
| 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | if (getpgrp() == oldpgrp) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | retval = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | tst_resm(TFAIL, "setpgrp() FAILED to set " |
| 95 | "new group id"); |
| 96 | continue; |
| 97 | } else { |
| 98 | tst_resm(TPASS, "functionality is correct"); |
| 99 | } |
robbiew | a3b949c | 2001-09-17 18:04:26 +0000 | [diff] [blame] | 100 | exit(retval); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | } else { /* parent */ |
robbiew | a3b949c | 2001-09-17 18:04:26 +0000 | [diff] [blame] | 102 | /* wait for the child to finish */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | wait(&status); |
| 104 | /* make sure the child returned a good exit status */ |
| 105 | e_code = status >> 8; |
| 106 | if ((e_code != 0) || (retval != 0)) { |
| 107 | tst_resm(TFAIL, "Failures reported above"); |
| 108 | } |
robbiew | a3b949c | 2001-09-17 18:04:26 +0000 | [diff] [blame] | 109 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
Garrett Cooper | 49f7622 | 2010-12-19 10:22:13 -0800 | [diff] [blame] | 112 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * setup() - performs all ONE TIME setup for this test. |
| 117 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 118 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | TEST_PAUSE; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 128 | * completion or premature exit. |
| 129 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 130 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | |
Garrett Cooper | 49f7622 | 2010-12-19 10:22:13 -0800 | [diff] [blame] | 133 | } |