blob: 5b0970ffdd0f66a40c0f83c1c8c5226a7786c36a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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 */
robbiew96d23372003-03-26 20:40:04 +000058#define _GNU_SOURCE 1
plars865695b2001-08-27 22:15:12 +000059
60#include <errno.h>
robbiew96d23372003-03-26 20:40:04 +000061#include <wait.h>
62#include <unistd.h>
63#include "test.h"
plars865695b2001-08-27 22:15:12 +000064
65void cleanup(void);
66void setup(void);
67
subrata_modak56207ce2009-03-23 13:35:39 +000068char *TCID = "getsid01";
plars865695b2001-08-27 22:15:12 +000069int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000070
71pid_t p_sid;
72
robbiew96d23372003-03-26 20:40:04 +000073int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000074{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020075 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020076 const char *msg;
plars865695b2001-08-27 22:15:12 +000077 pid_t pid, c_pid, c_sid;
78
Garrett Cooper45e285d2010-11-22 12:19:25 -080079 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080080 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000081 }
82
subrata_modak56207ce2009-03-23 13:35:39 +000083 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000084
85 /* The following loop checks looping state if -i option given */
86
87 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080088 /* reset tst_count in case we are looping */
89 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000090
91 /* call the system call with the TEST() macro */
subrata_modakbdbaec52009-02-26 12:14:51 +000092
plars865695b2001-08-27 22:15:12 +000093 TEST(getsid(0));
subrata_modakbdbaec52009-02-26 12:14:51 +000094
plars865695b2001-08-27 22:15:12 +000095 if (TEST_RETURN == -1) {
96 tst_resm(TFAIL, "call failed - errno = %d "
97 "- %s", TEST_ERRNO, strerror(TEST_ERRNO));
98 continue;
99 }
100
Cyril Hrubise38b9612014-06-02 17:20:57 +0200101 /* save the return value in a global variable */
102 p_sid = TEST_RETURN;
plars865695b2001-08-27 22:15:12 +0000103
Cyril Hrubise38b9612014-06-02 17:20:57 +0200104 if ((pid = FORK_OR_VFORK()) == -1) {
105 tst_brkm(TBROK, cleanup, "could not fork");
106 }
plars865695b2001-08-27 22:15:12 +0000107
Cyril Hrubise38b9612014-06-02 17:20:57 +0200108 if (pid == 0) { /* child */
109 if ((c_pid = getpid()) == -1) {
110 tst_resm(TINFO, "getpid failed in "
111 "functionality test");
112 exit(1);
plars865695b2001-08-27 22:15:12 +0000113 }
114
Cyril Hrubise38b9612014-06-02 17:20:57 +0200115 /*
116 * if the session ID of the child is the
117 * same as the parent then things look good
118 */
plars865695b2001-08-27 22:15:12 +0000119
Cyril Hrubise38b9612014-06-02 17:20:57 +0200120 if ((c_sid = getsid(0)) == -1) {
121 tst_resm(TINFO, "getsid failed in "
122 "functionality test");
123 exit(2);
124 }
plars865695b2001-08-27 22:15:12 +0000125
Cyril Hrubise38b9612014-06-02 17:20:57 +0200126 if (c_sid == p_sid) {
127 tst_resm(TPASS, "session ID is "
128 "correct");
plars865695b2001-08-27 22:15:12 +0000129 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 tst_resm(TFAIL, "session ID is "
131 "not correct");
plars865695b2001-08-27 22:15:12 +0000132 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200133 exit(0);
134
plars865695b2001-08-27 22:15:12 +0000135 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200136 waitpid(pid, NULL, 0);
plars865695b2001-08-27 22:15:12 +0000137 }
138 }
139
140 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800141 tst_exit();
plars865695b2001-08-27 22:15:12 +0000142}
143
144/*
145 * setup() - performs all the ONE TIME setup for this test.
146 */
subrata_modak56207ce2009-03-23 13:35:39 +0000147void setup(void)
plars865695b2001-08-27 22:15:12 +0000148{
Garrett Cooper2c282152010-12-16 00:55:50 -0800149
plars865695b2001-08-27 22:15:12 +0000150 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modakbdbaec52009-02-26 12:14:51 +0000151
plars865695b2001-08-27 22:15:12 +0000152 TEST_PAUSE;
153}
154
155/*
156 * cleanup() - performs all the ONE TIME cleanup for this test at completion
157 * or premature exit.
158 */
subrata_modak56207ce2009-03-23 13:35:39 +0000159void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000160{
plars865695b2001-08-27 22:15:12 +0000161
Chris Dearmanec6edca2012-10-17 19:54:01 -0700162}