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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * getsid01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * getsid01 - call getsid() and make sure it succeeds |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * loop if that option was specified |
| 29 | * issue the system call |
| 30 | * check the return value |
| 31 | * if return value == -1 |
| 32 | * issue a FAIL message, break remaining tests and cleanup |
| 33 | * if we are doing functional testing |
| 34 | * save the return value from the getsid() call - the session ID |
| 35 | * fork a child and get the child's session ID |
| 36 | * if the child's session ID == the parent's session ID |
| 37 | * issue a PASS message |
| 38 | * else |
| 39 | * issue a FAIL message |
| 40 | * else issue a PASS message |
| 41 | * call cleanup |
| 42 | * |
| 43 | * USAGE: <for command-line> |
| 44 | * getsid01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 45 | * where, -c n : Run n copies concurrently. |
| 46 | * -f : Turn off functionality Testing. |
| 47 | * -i n : Execute test n times. |
| 48 | * -I x : Execute test for x seconds. |
| 49 | * -P x : Pause for x seconds between iterations. |
| 50 | * -t : Turn on syscall timing. |
| 51 | * |
| 52 | * HISTORY |
| 53 | * 07/2001 Ported by Wayne Boyer |
| 54 | * |
| 55 | * RESTRICTIONS |
| 56 | * none |
| 57 | */ |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 58 | #define _GNU_SOURCE 1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 60 | #include <errno.h> |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 61 | #include <wait.h> |
| 62 | #include <unistd.h> |
| 63 | #include "test.h" |
| 64 | #include "usctest.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
| 66 | void cleanup(void); |
| 67 | void setup(void); |
| 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | char *TCID = "getsid01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | int TST_TOTAL = 1; |
| 71 | extern int Tst_count; |
| 72 | |
| 73 | pid_t p_sid; |
| 74 | |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 75 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | int lc; /* loop counter */ |
| 78 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | pid_t pid, c_pid, c_sid; |
| 80 | |
| 81 | /* parse standard options */ |
Garrett Cooper | e1f008e | 2010-12-14 00:21:59 -0800 | [diff] [blame^] | 82 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
| 84 | } |
| 85 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
| 88 | /* The following loop checks looping state if -i option given */ |
| 89 | |
| 90 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 91 | /* reset Tst_count in case we are looping */ |
| 92 | Tst_count = 0; |
| 93 | |
| 94 | /* call the system call with the TEST() macro */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 95 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | TEST(getsid(0)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | if (TEST_RETURN == -1) { |
| 99 | tst_resm(TFAIL, "call failed - errno = %d " |
| 100 | "- %s", TEST_ERRNO, strerror(TEST_ERRNO)); |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | if (STD_FUNCTIONAL_TEST) { |
| 105 | |
| 106 | /* save the return value in a global variable */ |
| 107 | p_sid = TEST_RETURN; |
| 108 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 109 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | tst_brkm(TBROK, cleanup, "could not fork"); |
| 111 | } |
| 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | if (pid == 0) { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | if ((c_pid = getpid()) == -1) { |
| 115 | tst_resm(TINFO, "getpid failed in " |
| 116 | "functionality test"); |
| 117 | exit(1); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * if the session ID of the child is the |
| 122 | * same as the parent then things look good |
| 123 | */ |
| 124 | |
| 125 | if ((c_sid = getsid(0)) == -1) { |
| 126 | tst_resm(TINFO, "getsid failed in " |
| 127 | "functionality test"); |
| 128 | exit(2); |
| 129 | } |
| 130 | |
| 131 | if (c_sid == p_sid) { |
| 132 | tst_resm(TPASS, "session ID is " |
| 133 | "correct"); |
| 134 | } else { |
| 135 | tst_resm(TFAIL, "session ID is " |
| 136 | "not correct"); |
| 137 | } |
| 138 | exit(0); |
| 139 | |
| 140 | } else { |
| 141 | waitpid(pid, NULL, 0); |
| 142 | } |
| 143 | } else { |
| 144 | tst_resm(TPASS, "call succeeded"); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | cleanup(); |
| 149 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | /*NOTREACHED*/ return 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* |
| 154 | * setup() - performs all the ONE TIME setup for this test. |
| 155 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
| 158 | /* capture signals */ |
| 159 | tst_sig(FORK, DEF_HANDLER, cleanup); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | /* Pause if that option was specified */ |
| 162 | TEST_PAUSE; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 167 | * or premature exit. |
| 168 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | { |
| 171 | /* |
| 172 | * print timing stats if that option was specified. |
| 173 | * print errno log if that option was specified. |
| 174 | */ |
| 175 | TEST_CLEANUP; |
| 176 | |
| 177 | /* exit with return code appropriate for results */ |
| 178 | tst_exit(); |
| 179 | } |