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 |
| 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | void cleanup(void); |
| 66 | void setup(void); |
| 67 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | char *TCID = "getsid01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
| 71 | pid_t p_sid; |
| 72 | |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 73 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 75 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 76 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | pid_t pid, c_pid, c_sid; |
| 78 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 79 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 80 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | } |
| 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | /* The following loop checks looping state if -i option given */ |
| 86 | |
| 87 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 88 | /* reset tst_count in case we are looping */ |
| 89 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
| 91 | /* call the system call with the TEST() macro */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 92 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | TEST(getsid(0)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | if (TEST_RETURN == -1) { |
| 96 | tst_resm(TFAIL, "call failed - errno = %d " |
| 97 | "- %s", TEST_ERRNO, strerror(TEST_ERRNO)); |
| 98 | continue; |
| 99 | } |
| 100 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 101 | /* save the return value in a global variable */ |
| 102 | p_sid = TEST_RETURN; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 104 | if ((pid = FORK_OR_VFORK()) == -1) { |
| 105 | tst_brkm(TBROK, cleanup, "could not fork"); |
| 106 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 108 | if (pid == 0) { /* child */ |
| 109 | if ((c_pid = getpid()) == -1) { |
| 110 | tst_resm(TINFO, "getpid failed in " |
| 111 | "functionality test"); |
| 112 | exit(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 115 | /* |
| 116 | * if the session ID of the child is the |
| 117 | * same as the parent then things look good |
| 118 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 120 | if ((c_sid = getsid(0)) == -1) { |
| 121 | tst_resm(TINFO, "getsid failed in " |
| 122 | "functionality test"); |
| 123 | exit(2); |
| 124 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 126 | if (c_sid == p_sid) { |
| 127 | tst_resm(TPASS, "session ID is " |
| 128 | "correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | tst_resm(TFAIL, "session ID is " |
| 131 | "not correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 133 | exit(0); |
| 134 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 136 | waitpid(pid, NULL, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 141 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * setup() - performs all the ONE TIME setup for this test. |
| 146 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 149 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | tst_sig(FORK, DEF_HANDLER, cleanup); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | TEST_PAUSE; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 157 | * or premature exit. |
| 158 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 162 | } |