1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**************************************************************************
18  *
19  *    TEST IDENTIFIER	: socketcall03
20  *
21  *    EXECUTED BY	: All user
22  *
23  *    TEST TITLE	: Basic test for socketcall(2) for bind(2)
24  *
25  *    TEST CASE TOTAL	: 1
26  *
27  *    AUTHOR		: sowmya adiga<sowmya.adiga@wipro.com>
28  *
29  *    SIGNALS
30  *	Uses SIGUSR1 to pause before test if option set.
31  *	(See the parse_opts(3) man page).
32  *
33  *    DESCRIPTION
34  *	This is a phase I test for the socketcall(2) system call.
35  *	It is intended to provide a limited exposure of the system call.
36  *
37  *	Setup:
38  *	  Setup signal handling.
39  *	  Pause for SIGUSR1 if option specified.
40  *
41  *	Test:
42  *        Execute system call
43  *	  Check return code, if system call failed (return=-1)
44  *	  Log the errno and Issue a FAIL message.
45  *	  Otherwise, Issue a PASS message.
46  *
47  *	Cleanup:
48  *	  Print errno log and/or timing stats if options given
49  *
50  * USAGE:  <for command-line>
51  *  socketcall03 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
52  *		where,		-c n : Run n copies concurrently
53  *				-e   : Turn on errno logging.
54  *				-h   : Show this help screen
55  *				-i n : Execute test n times.
56  *				-I x : Execute test for x seconds.
57  *				-p   : Pause for SIGUSR1 before starting
58  *				-P x : Pause for x seconds between iterations.
59  *				-t   : Turn on syscall timing.
60  *
61  * RESTRICTIONS
62  * None
63  *****************************************************************************/
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <errno.h>
67 #include <sys/syscall.h>
68 #include <unistd.h>
69 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <linux/net.h>
72 #include <sys/un.h>
73 #include <netinet/in.h>
74 
75 #include "test.h"
76 
77 char *TCID = "socketcall03";
78 
79 #ifdef __NR_socketcall
80 
81 #define socketcall(call, args) syscall(__NR_socketcall, call, args)
82 
83 void setup();
84 void cleanup();
85 void setup1(void);
86 
87 int TST_TOTAL = 1;
88 int s;
89 unsigned long args[3];
90 struct sockaddr_in si;
91 
92 struct test_case_t {
93 	int domain;
94 	int type;
95 	int pro;
96 	int call;
97 	void (*setupfunc) (void);
98 	char *desc;
99 } TC = {
100 AF_INET, SOCK_STREAM, 6, SYS_BIND, setup1, "bind call"};
101 
102 int main(int ac, char **av)
103 {
104 	int lc;
105 
106 	tst_parse_opts(ac, av, NULL, NULL);
107 
108 	setup();
109 
110 	/* check looping state */
111 	for (lc = 0; TEST_LOOPING(lc); lc++) {
112 
113 		tst_count = 0;
114 
115 		TC.setupfunc();
116 
117 		TEST(socketcall(TC.call, args));
118 
119 		/* check return code */
120 
121 		if (TEST_RETURN == -1) {
122 			tst_resm(TFAIL | TERRNO, "socketcall() Failed "
123 				 " with return=%ld", TEST_RETURN);
124 		} else {
125 			tst_resm(TPASS, "socketcall() passed "
126 				 "for %s with return=%ld ",
127 				 TC.desc, TEST_RETURN);
128 
129 			close(s);
130 		}
131 	}
132 
133 	/* cleanup and exit */
134 	cleanup();
135 
136 	tst_exit();
137 }
138 
139 /*setup1()*/
140 void setup1(void)
141 {
142 	si.sin_family = AF_INET;
143 	si.sin_addr.s_addr = htons(INADDR_ANY);
144 	si.sin_port = 0;
145 
146 	if ((s = socket(TC.domain, TC.type, TC.pro)) == -1) {
147 		tst_brkm(TBROK, NULL, "socket creation failed");
148 	}
149 	args[0] = s;
150 	args[1] = (unsigned long)&si;
151 	args[2] = sizeof(si);
152 }
153 
154 /* setup() - performs all ONE TIME setup for this test. */
155 void setup(void)
156 {
157 
158 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
159 
160 	TEST_PAUSE;
161 }
162 
163 /*
164  * cleanup() - performs all ONE TIME cleanup for this test at
165  *		completion or premature exit.
166  */
167 void cleanup(void)
168 {
169 }
170 
171 #else
172 
173 int TST_TOTAL = 0;
174 
175 int main(void)
176 {
177 	tst_resm(TPASS, "socket call test on this architecture disabled.");
178 	tst_exit();
179 }
180 
181 #endif
182