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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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  */
58 #define _GNU_SOURCE 1
59 
60 #include <errno.h>
61 #include <sys/wait.h>
62 #include <unistd.h>
63 #include "test.h"
64 
65 void cleanup(void);
66 void setup(void);
67 
68 char *TCID = "getsid01";
69 int TST_TOTAL = 1;
70 
71 pid_t p_sid;
72 
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 	int lc;
76 	pid_t pid, c_pid, c_sid;
77 
78 	tst_parse_opts(ac, av, NULL, NULL);
79 
80 	setup();		/* global setup */
81 
82 	/* The following loop checks looping state if -i option given */
83 
84 	for (lc = 0; TEST_LOOPING(lc); lc++) {
85 		/* reset tst_count in case we are looping */
86 		tst_count = 0;
87 
88 		/* call the system call with the TEST() macro */
89 
90 		TEST(getsid(0));
91 
92 		if (TEST_RETURN == -1) {
93 			tst_resm(TFAIL, "call failed - errno = %d "
94 				 "- %s", TEST_ERRNO, strerror(TEST_ERRNO));
95 			continue;
96 		}
97 
98 		/* save the return value in a global variable */
99 		p_sid = TEST_RETURN;
100 
101 		if ((pid = FORK_OR_VFORK()) == -1) {
102 			tst_brkm(TBROK, cleanup, "could not fork");
103 		}
104 
105 		if (pid == 0) {	/* child */
106 			if ((c_pid = getpid()) == -1) {
107 				tst_resm(TINFO, "getpid failed in "
108 					 "functionality test");
109 				exit(1);
110 			}
111 
112 			/*
113 			 * if the session ID of the child is the
114 			 * same as the parent then things look good
115 			 */
116 
117 			if ((c_sid = getsid(0)) == -1) {
118 				tst_resm(TINFO, "getsid failed in "
119 					 "functionality test");
120 				exit(2);
121 			}
122 
123 			if (c_sid == p_sid) {
124 				tst_resm(TPASS, "session ID is "
125 					 "correct");
126 			} else {
127 				tst_resm(TFAIL, "session ID is "
128 					 "not correct");
129 			}
130 			exit(0);
131 
132 		} else {
133 			waitpid(pid, NULL, 0);
134 		}
135 	}
136 
137 	cleanup();
138 	tst_exit();
139 }
140 
141 /*
142  * setup() - performs all the ONE TIME setup for this test.
143  */
setup(void)144 void setup(void)
145 {
146 
147 	tst_sig(FORK, DEF_HANDLER, cleanup);
148 
149 	TEST_PAUSE;
150 }
151 
152 /*
153  * cleanup() - performs all the ONE TIME cleanup for this test at completion
154  * 	       or premature exit.
155  */
cleanup(void)156 void cleanup(void)
157 {
158 
159 }
160