blob: 3cf4d138a3849068a78021a1da8d249ff9c49c9e [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
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 */
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"
64#include "usctest.h"
plars865695b2001-08-27 22:15:12 +000065
66void cleanup(void);
67void setup(void);
68
subrata_modak56207ce2009-03-23 13:35:39 +000069char *TCID = "getsid01";
plars865695b2001-08-27 22:15:12 +000070int TST_TOTAL = 1;
71extern int Tst_count;
72
73pid_t p_sid;
74
robbiew96d23372003-03-26 20:40:04 +000075int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000076{
subrata_modak56207ce2009-03-23 13:35:39 +000077 int lc; /* loop counter */
78 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000079 pid_t pid, c_pid, c_sid;
80
81 /* parse standard options */
Garrett Coopere1f008e2010-12-14 00:21:59 -080082 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +000083 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
84 }
85
subrata_modak56207ce2009-03-23 13:35:39 +000086 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000087
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_modakbdbaec52009-02-26 12:14:51 +000095
plars865695b2001-08-27 22:15:12 +000096 TEST(getsid(0));
subrata_modakbdbaec52009-02-26 12:14:51 +000097
plars865695b2001-08-27 22:15:12 +000098 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
robbiewd34d5812005-07-11 22:28:09 +0000109 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000110 tst_brkm(TBROK, cleanup, "could not fork");
111 }
112
subrata_modak56207ce2009-03-23 13:35:39 +0000113 if (pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +0000114 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_modak56207ce2009-03-23 13:35:39 +0000150 /*NOTREACHED*/ return 0;
plars865695b2001-08-27 22:15:12 +0000151}
152
153/*
154 * setup() - performs all the ONE TIME setup for this test.
155 */
subrata_modak56207ce2009-03-23 13:35:39 +0000156void setup(void)
plars865695b2001-08-27 22:15:12 +0000157{
158 /* capture signals */
159 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modakbdbaec52009-02-26 12:14:51 +0000160
plars865695b2001-08-27 22:15:12 +0000161 /* 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_modak56207ce2009-03-23 13:35:39 +0000169void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000170{
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}