1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Cyril Hrubis <[email protected]> 2012
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
7*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
8*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
9*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
12*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
17*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
18*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*49cdfc7eSAndroid Build Coastguard Worker *
20*49cdfc7eSAndroid Build Coastguard Worker */
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker /*
23*49cdfc7eSAndroid Build Coastguard Worker * Test Name: sendmsg01
24*49cdfc7eSAndroid Build Coastguard Worker *
25*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
26*49cdfc7eSAndroid Build Coastguard Worker * Verify that sendmsg() returns the proper errno for various failure cases
27*49cdfc7eSAndroid Build Coastguard Worker *
28*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
29*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
30*49cdfc7eSAndroid Build Coastguard Worker * 05/2003 Modified by Manoj Iyer - Make setup function set up lo device.
31*49cdfc7eSAndroid Build Coastguard Worker */
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/signal.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <sys/file.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
51*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "sendmsg01";
54*49cdfc7eSAndroid Build Coastguard Worker int testno;
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker static char buf[1024], bigbuf[128 * 1024];
57*49cdfc7eSAndroid Build Coastguard Worker static int s;
58*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in sin1, sin2;
59*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_un sun1;
60*49cdfc7eSAndroid Build Coastguard Worker static struct msghdr msgdat;
61*49cdfc7eSAndroid Build Coastguard Worker static char cbuf[4096];
62*49cdfc7eSAndroid Build Coastguard Worker static struct cmsghdr *control;
63*49cdfc7eSAndroid Build Coastguard Worker static int controllen;
64*49cdfc7eSAndroid Build Coastguard Worker static struct iovec iov[1];
65*49cdfc7eSAndroid Build Coastguard Worker static int sfd; /* shared between do_child and start_server */
66*49cdfc7eSAndroid Build Coastguard Worker static int ufd; /* shared between do_child and start_server */
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
69*49cdfc7eSAndroid Build Coastguard Worker static void setup0(void);
70*49cdfc7eSAndroid Build Coastguard Worker static void setup1(void);
71*49cdfc7eSAndroid Build Coastguard Worker static void setup2(void);
72*49cdfc7eSAndroid Build Coastguard Worker static void setup3(void);
73*49cdfc7eSAndroid Build Coastguard Worker static void setup4(void);
74*49cdfc7eSAndroid Build Coastguard Worker static void setup5(void);
75*49cdfc7eSAndroid Build Coastguard Worker static void setup6(void);
76*49cdfc7eSAndroid Build Coastguard Worker static void setup8(void);
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
79*49cdfc7eSAndroid Build Coastguard Worker static void cleanup0(void);
80*49cdfc7eSAndroid Build Coastguard Worker static void cleanup1(void);
81*49cdfc7eSAndroid Build Coastguard Worker static void cleanup4(void);
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void);
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t { /* test case structure */
86*49cdfc7eSAndroid Build Coastguard Worker int domain; /* PF_INET, PF_UNIX, ... */
87*49cdfc7eSAndroid Build Coastguard Worker int type; /* SOCK_STREAM, SOCK_DGRAM ... */
88*49cdfc7eSAndroid Build Coastguard Worker int proto; /* protocol number (usually 0 = default) */
89*49cdfc7eSAndroid Build Coastguard Worker struct iovec *iov;
90*49cdfc7eSAndroid Build Coastguard Worker int iovcnt; /* # elements in iovec */
91*49cdfc7eSAndroid Build Coastguard Worker void *buf; /* send data buffer */
92*49cdfc7eSAndroid Build Coastguard Worker int buflen; /* send buffer length */
93*49cdfc7eSAndroid Build Coastguard Worker struct msghdr *msg;
94*49cdfc7eSAndroid Build Coastguard Worker unsigned flags;
95*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr *to; /* destination */
96*49cdfc7eSAndroid Build Coastguard Worker int tolen; /* length of "to" buffer */
97*49cdfc7eSAndroid Build Coastguard Worker int retval; /* syscall return value */
98*49cdfc7eSAndroid Build Coastguard Worker int experrno; /* expected errno */
99*49cdfc7eSAndroid Build Coastguard Worker void (*setup) (void);
100*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup) (void);
101*49cdfc7eSAndroid Build Coastguard Worker char *desc;
102*49cdfc7eSAndroid Build Coastguard Worker };
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t tdat[] = {
105*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
106*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
107*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
108*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
109*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
110*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
111*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
112*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
113*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
114*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
115*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
116*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
117*49cdfc7eSAndroid Build Coastguard Worker .experrno = EBADF,
118*49cdfc7eSAndroid Build Coastguard Worker .setup = setup0,
119*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup0,
120*49cdfc7eSAndroid Build Coastguard Worker .desc = "bad file descriptor"}
121*49cdfc7eSAndroid Build Coastguard Worker ,
122*49cdfc7eSAndroid Build Coastguard Worker {.domain = 0,
123*49cdfc7eSAndroid Build Coastguard Worker .type = 0,
124*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
125*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
126*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
127*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
128*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
129*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
130*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
131*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
132*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
133*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
134*49cdfc7eSAndroid Build Coastguard Worker .experrno = ENOTSOCK,
135*49cdfc7eSAndroid Build Coastguard Worker .setup = setup0,
136*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup0,
137*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid socket"}
138*49cdfc7eSAndroid Build Coastguard Worker ,
139*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
140*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
141*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
142*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
143*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
144*49cdfc7eSAndroid Build Coastguard Worker .buf = (void *)-1,
145*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
146*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
147*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
148*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
149*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
150*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
151*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
152*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
153*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
154*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid send buffer"}
155*49cdfc7eSAndroid Build Coastguard Worker ,
156*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
157*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
158*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
159*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
160*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
161*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
162*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
163*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
164*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
165*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin2,
166*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin2),
167*49cdfc7eSAndroid Build Coastguard Worker .retval = 0,
168*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
169*49cdfc7eSAndroid Build Coastguard Worker .setup = setup5,
170*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
171*49cdfc7eSAndroid Build Coastguard Worker .desc = "connected TCP"}
172*49cdfc7eSAndroid Build Coastguard Worker ,
173*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
174*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
175*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
176*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
177*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
178*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
179*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
180*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
181*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
182*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
183*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
184*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
185*49cdfc7eSAndroid Build Coastguard Worker .experrno = EPIPE,
186*49cdfc7eSAndroid Build Coastguard Worker .setup = setup3,
187*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
188*49cdfc7eSAndroid Build Coastguard Worker .desc = "not connected TCP"}
189*49cdfc7eSAndroid Build Coastguard Worker ,
190*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
191*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
192*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
193*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
194*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
195*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
196*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
197*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
198*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
199*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
200*49cdfc7eSAndroid Build Coastguard Worker .tolen = 1,
201*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
202*49cdfc7eSAndroid Build Coastguard Worker .experrno = EINVAL,
203*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
204*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
205*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid to buffer length"},
206*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
207*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
208*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
209*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
210*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
211*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
212*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
213*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
214*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
215*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)-1,
216*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(struct sockaddr),
217*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
218*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
219*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
220*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
221*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid to buffer"},
222*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
223*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
224*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
225*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
226*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
227*49cdfc7eSAndroid Build Coastguard Worker .buf = bigbuf,
228*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(bigbuf),
229*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
230*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
231*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
232*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
233*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
234*49cdfc7eSAndroid Build Coastguard Worker .experrno = EMSGSIZE,
235*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
236*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
237*49cdfc7eSAndroid Build Coastguard Worker .desc = "UDP message too big"}
238*49cdfc7eSAndroid Build Coastguard Worker ,
239*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
240*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
241*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
242*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
243*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
244*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
245*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
246*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
247*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
248*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
249*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
250*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
251*49cdfc7eSAndroid Build Coastguard Worker .experrno = EPIPE,
252*49cdfc7eSAndroid Build Coastguard Worker .setup = setup2,
253*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
254*49cdfc7eSAndroid Build Coastguard Worker .desc = "local endpoint shutdown"}
255*49cdfc7eSAndroid Build Coastguard Worker ,
256*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
257*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
258*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
259*49cdfc7eSAndroid Build Coastguard Worker .iov = NULL,
260*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
261*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
262*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
263*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
264*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
265*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
266*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
267*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
268*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
269*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
270*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
271*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid iovec pointer"}
272*49cdfc7eSAndroid Build Coastguard Worker ,
273*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
274*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
275*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
276*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
277*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
278*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
279*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
280*49cdfc7eSAndroid Build Coastguard Worker .msg = NULL,
281*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
282*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
283*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
284*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
285*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
286*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
287*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
288*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid msghdr pointer"}
289*49cdfc7eSAndroid Build Coastguard Worker ,
290*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_UNIX,
291*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
292*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
293*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
294*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
295*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
296*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
297*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
298*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
299*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sun1,
300*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sun1),
301*49cdfc7eSAndroid Build Coastguard Worker .retval = 0,
302*49cdfc7eSAndroid Build Coastguard Worker .experrno = 0,
303*49cdfc7eSAndroid Build Coastguard Worker .setup = setup4,
304*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup4,
305*49cdfc7eSAndroid Build Coastguard Worker .desc = "rights passing"}
306*49cdfc7eSAndroid Build Coastguard Worker ,
307*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
308*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
309*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
310*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
311*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
312*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
313*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
314*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
315*49cdfc7eSAndroid Build Coastguard Worker .flags = MSG_OOB,
316*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sin1,
317*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
318*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
319*49cdfc7eSAndroid Build Coastguard Worker .experrno = EOPNOTSUPP,
320*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
321*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
322*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid flags set"}
323*49cdfc7eSAndroid Build Coastguard Worker ,
324*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_UNIX,
325*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
326*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
327*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
328*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
329*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
330*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
331*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
332*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
333*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sun1,
334*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sun1),
335*49cdfc7eSAndroid Build Coastguard Worker .retval = 0,
336*49cdfc7eSAndroid Build Coastguard Worker .experrno = EOPNOTSUPP,
337*49cdfc7eSAndroid Build Coastguard Worker .setup = setup6,
338*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup4,
339*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid cmsg length"}
340*49cdfc7eSAndroid Build Coastguard Worker ,
341*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_UNIX,
342*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
343*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
344*49cdfc7eSAndroid Build Coastguard Worker .iov = iov,
345*49cdfc7eSAndroid Build Coastguard Worker .iovcnt = 1,
346*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
347*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
348*49cdfc7eSAndroid Build Coastguard Worker .msg = &msgdat,
349*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
350*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr *)&sun1,
351*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sun1),
352*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
353*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
354*49cdfc7eSAndroid Build Coastguard Worker .setup = setup8,
355*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup4,
356*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid cmsg pointer"}
357*49cdfc7eSAndroid Build Coastguard Worker };
358*49cdfc7eSAndroid Build Coastguard Worker
359*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
360*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char * argv[])361*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
362*49cdfc7eSAndroid Build Coastguard Worker {
363*49cdfc7eSAndroid Build Coastguard Worker int lc;
364*49cdfc7eSAndroid Build Coastguard Worker
365*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, NULL, NULL);
366*49cdfc7eSAndroid Build Coastguard Worker
367*49cdfc7eSAndroid Build Coastguard Worker setup();
368*49cdfc7eSAndroid Build Coastguard Worker
369*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); ++lc) {
370*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
371*49cdfc7eSAndroid Build Coastguard Worker for (testno = 0; testno < TST_TOTAL; ++testno) {
372*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].setup();
373*49cdfc7eSAndroid Build Coastguard Worker
374*49cdfc7eSAndroid Build Coastguard Worker iov[0].iov_base = tdat[testno].buf;
375*49cdfc7eSAndroid Build Coastguard Worker iov[0].iov_len = tdat[testno].buflen;
376*49cdfc7eSAndroid Build Coastguard Worker if (tdat[testno].type != SOCK_STREAM) {
377*49cdfc7eSAndroid Build Coastguard Worker msgdat.msg_name = tdat[testno].to;
378*49cdfc7eSAndroid Build Coastguard Worker msgdat.msg_namelen = tdat[testno].tolen;
379*49cdfc7eSAndroid Build Coastguard Worker }
380*49cdfc7eSAndroid Build Coastguard Worker msgdat.msg_iov = tdat[testno].iov;
381*49cdfc7eSAndroid Build Coastguard Worker msgdat.msg_iovlen = tdat[testno].iovcnt;
382*49cdfc7eSAndroid Build Coastguard Worker msgdat.msg_control = control;
383*49cdfc7eSAndroid Build Coastguard Worker msgdat.msg_controllen = controllen;
384*49cdfc7eSAndroid Build Coastguard Worker msgdat.msg_flags = 0;
385*49cdfc7eSAndroid Build Coastguard Worker
386*49cdfc7eSAndroid Build Coastguard Worker TEST(sendmsg(s, tdat[testno].msg, tdat[testno].flags));
387*49cdfc7eSAndroid Build Coastguard Worker
388*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN > 0)
389*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN = 0;
390*49cdfc7eSAndroid Build Coastguard Worker
391*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN != tdat[testno].retval ||
392*49cdfc7eSAndroid Build Coastguard Worker (TEST_RETURN < 0 &&
393*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO != tdat[testno].experrno)) {
394*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "%s ; returned"
395*49cdfc7eSAndroid Build Coastguard Worker " %ld (expected %d), errno %d (expected"
396*49cdfc7eSAndroid Build Coastguard Worker " %d)", tdat[testno].desc,
397*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN, tdat[testno].retval,
398*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO, tdat[testno].experrno);
399*49cdfc7eSAndroid Build Coastguard Worker } else {
400*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "%s successful",
401*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].desc);
402*49cdfc7eSAndroid Build Coastguard Worker }
403*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].cleanup();
404*49cdfc7eSAndroid Build Coastguard Worker }
405*49cdfc7eSAndroid Build Coastguard Worker }
406*49cdfc7eSAndroid Build Coastguard Worker cleanup();
407*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
408*49cdfc7eSAndroid Build Coastguard Worker }
409*49cdfc7eSAndroid Build Coastguard Worker
start_server(struct sockaddr_in * sin0,struct sockaddr_un * sun0)410*49cdfc7eSAndroid Build Coastguard Worker static pid_t start_server(struct sockaddr_in *sin0, struct sockaddr_un *sun0)
411*49cdfc7eSAndroid Build Coastguard Worker {
412*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
413*49cdfc7eSAndroid Build Coastguard Worker socklen_t slen = sizeof(*sin0);
414*49cdfc7eSAndroid Build Coastguard Worker
415*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_family = AF_INET;
416*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_port = 0; /* pick random free port */
417*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_addr.s_addr = INADDR_ANY;
418*49cdfc7eSAndroid Build Coastguard Worker
419*49cdfc7eSAndroid Build Coastguard Worker /* set up inet socket */
420*49cdfc7eSAndroid Build Coastguard Worker sfd = socket(PF_INET, SOCK_STREAM, 0);
421*49cdfc7eSAndroid Build Coastguard Worker if (sfd < 0) {
422*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "server socket failed: %s",
423*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
424*49cdfc7eSAndroid Build Coastguard Worker return -1;
425*49cdfc7eSAndroid Build Coastguard Worker }
426*49cdfc7eSAndroid Build Coastguard Worker if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
427*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "server bind failed: %s",
428*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
429*49cdfc7eSAndroid Build Coastguard Worker return -1;
430*49cdfc7eSAndroid Build Coastguard Worker }
431*49cdfc7eSAndroid Build Coastguard Worker if (listen(sfd, 10) < 0) {
432*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "server listen failed: %s",
433*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
434*49cdfc7eSAndroid Build Coastguard Worker return -1;
435*49cdfc7eSAndroid Build Coastguard Worker }
436*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
437*49cdfc7eSAndroid Build Coastguard Worker
438*49cdfc7eSAndroid Build Coastguard Worker /* set up UNIX-domain socket */
439*49cdfc7eSAndroid Build Coastguard Worker ufd = socket(PF_UNIX, SOCK_DGRAM, 0);
440*49cdfc7eSAndroid Build Coastguard Worker if (ufd < 0) {
441*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "server UD socket failed: %s",
442*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
443*49cdfc7eSAndroid Build Coastguard Worker return -1;
444*49cdfc7eSAndroid Build Coastguard Worker }
445*49cdfc7eSAndroid Build Coastguard Worker if (bind(ufd, (struct sockaddr *)sun0, sizeof(*sun0))) {
446*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "server UD bind failed: %s",
447*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
448*49cdfc7eSAndroid Build Coastguard Worker return -1;
449*49cdfc7eSAndroid Build Coastguard Worker }
450*49cdfc7eSAndroid Build Coastguard Worker
451*49cdfc7eSAndroid Build Coastguard Worker switch ((pid = tst_fork())) {
452*49cdfc7eSAndroid Build Coastguard Worker case 0:
453*49cdfc7eSAndroid Build Coastguard Worker do_child();
454*49cdfc7eSAndroid Build Coastguard Worker break;
455*49cdfc7eSAndroid Build Coastguard Worker case -1:
456*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "server fork failed: %s",
457*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
458*49cdfc7eSAndroid Build Coastguard Worker default:
459*49cdfc7eSAndroid Build Coastguard Worker close(sfd);
460*49cdfc7eSAndroid Build Coastguard Worker close(ufd);
461*49cdfc7eSAndroid Build Coastguard Worker return pid;
462*49cdfc7eSAndroid Build Coastguard Worker }
463*49cdfc7eSAndroid Build Coastguard Worker
464*49cdfc7eSAndroid Build Coastguard Worker exit(1);
465*49cdfc7eSAndroid Build Coastguard Worker }
466*49cdfc7eSAndroid Build Coastguard Worker
do_child(void)467*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void)
468*49cdfc7eSAndroid Build Coastguard Worker {
469*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in fsin;
470*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_un fsun;
471*49cdfc7eSAndroid Build Coastguard Worker fd_set afds, rfds;
472*49cdfc7eSAndroid Build Coastguard Worker int nfds, cc, fd;
473*49cdfc7eSAndroid Build Coastguard Worker
474*49cdfc7eSAndroid Build Coastguard Worker FD_ZERO(&afds);
475*49cdfc7eSAndroid Build Coastguard Worker FD_SET(sfd, &afds);
476*49cdfc7eSAndroid Build Coastguard Worker FD_SET(ufd, &afds);
477*49cdfc7eSAndroid Build Coastguard Worker
478*49cdfc7eSAndroid Build Coastguard Worker nfds = MAX(sfd + 1, ufd + 1);
479*49cdfc7eSAndroid Build Coastguard Worker
480*49cdfc7eSAndroid Build Coastguard Worker /* accept connections until killed */
481*49cdfc7eSAndroid Build Coastguard Worker while (1) {
482*49cdfc7eSAndroid Build Coastguard Worker socklen_t fromlen;
483*49cdfc7eSAndroid Build Coastguard Worker
484*49cdfc7eSAndroid Build Coastguard Worker memcpy(&rfds, &afds, sizeof(rfds));
485*49cdfc7eSAndroid Build Coastguard Worker
486*49cdfc7eSAndroid Build Coastguard Worker if (select(nfds, &rfds, NULL, NULL, NULL) < 0)
487*49cdfc7eSAndroid Build Coastguard Worker if (errno != EINTR)
488*49cdfc7eSAndroid Build Coastguard Worker exit(1);
489*49cdfc7eSAndroid Build Coastguard Worker if (FD_ISSET(sfd, &rfds)) {
490*49cdfc7eSAndroid Build Coastguard Worker int newfd;
491*49cdfc7eSAndroid Build Coastguard Worker
492*49cdfc7eSAndroid Build Coastguard Worker fromlen = sizeof(fsin);
493*49cdfc7eSAndroid Build Coastguard Worker newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
494*49cdfc7eSAndroid Build Coastguard Worker if (newfd >= 0) {
495*49cdfc7eSAndroid Build Coastguard Worker FD_SET(newfd, &afds);
496*49cdfc7eSAndroid Build Coastguard Worker nfds = MAX(nfds, newfd + 1);
497*49cdfc7eSAndroid Build Coastguard Worker }
498*49cdfc7eSAndroid Build Coastguard Worker }
499*49cdfc7eSAndroid Build Coastguard Worker if (FD_ISSET(ufd, &rfds)) {
500*49cdfc7eSAndroid Build Coastguard Worker int newfd;
501*49cdfc7eSAndroid Build Coastguard Worker
502*49cdfc7eSAndroid Build Coastguard Worker fromlen = sizeof(fsun);
503*49cdfc7eSAndroid Build Coastguard Worker newfd = accept(ufd, (struct sockaddr *)&fsun, &fromlen);
504*49cdfc7eSAndroid Build Coastguard Worker if (newfd >= 0)
505*49cdfc7eSAndroid Build Coastguard Worker FD_SET(newfd, &afds);
506*49cdfc7eSAndroid Build Coastguard Worker }
507*49cdfc7eSAndroid Build Coastguard Worker for (fd = 0; fd < nfds; ++fd) {
508*49cdfc7eSAndroid Build Coastguard Worker if (fd != sfd && fd != ufd && FD_ISSET(fd, &rfds)) {
509*49cdfc7eSAndroid Build Coastguard Worker cc = read(fd, buf, sizeof(buf));
510*49cdfc7eSAndroid Build Coastguard Worker if (cc == 0 || (cc < 0 && errno != EINTR)) {
511*49cdfc7eSAndroid Build Coastguard Worker close(fd);
512*49cdfc7eSAndroid Build Coastguard Worker FD_CLR(fd, &afds);
513*49cdfc7eSAndroid Build Coastguard Worker }
514*49cdfc7eSAndroid Build Coastguard Worker }
515*49cdfc7eSAndroid Build Coastguard Worker }
516*49cdfc7eSAndroid Build Coastguard Worker }
517*49cdfc7eSAndroid Build Coastguard Worker }
518*49cdfc7eSAndroid Build Coastguard Worker
519*49cdfc7eSAndroid Build Coastguard Worker static pid_t pid;
520*49cdfc7eSAndroid Build Coastguard Worker static char tmpsunpath[1024];
521*49cdfc7eSAndroid Build Coastguard Worker
setup(void)522*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
523*49cdfc7eSAndroid Build Coastguard Worker {
524*49cdfc7eSAndroid Build Coastguard Worker
525*49cdfc7eSAndroid Build Coastguard Worker int ret = 0;
526*49cdfc7eSAndroid Build Coastguard Worker
527*49cdfc7eSAndroid Build Coastguard Worker tst_require_root();
528*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
529*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
530*49cdfc7eSAndroid Build Coastguard Worker
531*49cdfc7eSAndroid Build Coastguard Worker
532*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
533*49cdfc7eSAndroid Build Coastguard Worker snprintf(tmpsunpath, 1024, "udsock%ld", (long)time(NULL));
534*49cdfc7eSAndroid Build Coastguard Worker sun1.sun_family = AF_UNIX;
535*49cdfc7eSAndroid Build Coastguard Worker strcpy(sun1.sun_path, tmpsunpath);
536*49cdfc7eSAndroid Build Coastguard Worker
537*49cdfc7eSAndroid Build Coastguard Worker /* this test will fail or in some cases hang if no eth or lo is
538*49cdfc7eSAndroid Build Coastguard Worker * configured, so making sure in setup that at least lo is up
539*49cdfc7eSAndroid Build Coastguard Worker */
540*49cdfc7eSAndroid Build Coastguard Worker ret = system("ip link set lo up");
541*49cdfc7eSAndroid Build Coastguard Worker if (WEXITSTATUS(ret) != 0) {
542*49cdfc7eSAndroid Build Coastguard Worker ret = system("ifconfig lo up 127.0.0.1");
543*49cdfc7eSAndroid Build Coastguard Worker if (WEXITSTATUS(ret) != 0) {
544*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup,
545*49cdfc7eSAndroid Build Coastguard Worker "ip/ifconfig failed to bring up loop back device");
546*49cdfc7eSAndroid Build Coastguard Worker }
547*49cdfc7eSAndroid Build Coastguard Worker }
548*49cdfc7eSAndroid Build Coastguard Worker
549*49cdfc7eSAndroid Build Coastguard Worker pid = start_server(&sin1, &sun1);
550*49cdfc7eSAndroid Build Coastguard Worker
551*49cdfc7eSAndroid Build Coastguard Worker signal(SIGPIPE, SIG_IGN);
552*49cdfc7eSAndroid Build Coastguard Worker }
553*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)554*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
555*49cdfc7eSAndroid Build Coastguard Worker {
556*49cdfc7eSAndroid Build Coastguard Worker if (pid > 0)
557*49cdfc7eSAndroid Build Coastguard Worker kill(pid, SIGKILL); /* kill server, if server exists */
558*49cdfc7eSAndroid Build Coastguard Worker unlink(tmpsunpath);
559*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
560*49cdfc7eSAndroid Build Coastguard Worker }
561*49cdfc7eSAndroid Build Coastguard Worker
setup0(void)562*49cdfc7eSAndroid Build Coastguard Worker static void setup0(void)
563*49cdfc7eSAndroid Build Coastguard Worker {
564*49cdfc7eSAndroid Build Coastguard Worker if (tdat[testno].experrno == EBADF)
565*49cdfc7eSAndroid Build Coastguard Worker s = 400; /* anything not an open file */
566*49cdfc7eSAndroid Build Coastguard Worker else if ((s = open("/dev/null", O_WRONLY)) == -1)
567*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "error opening /dev/null - "
568*49cdfc7eSAndroid Build Coastguard Worker "errno: %s", strerror(errno));
569*49cdfc7eSAndroid Build Coastguard Worker }
570*49cdfc7eSAndroid Build Coastguard Worker
cleanup0(void)571*49cdfc7eSAndroid Build Coastguard Worker static void cleanup0(void)
572*49cdfc7eSAndroid Build Coastguard Worker {
573*49cdfc7eSAndroid Build Coastguard Worker s = -1;
574*49cdfc7eSAndroid Build Coastguard Worker }
575*49cdfc7eSAndroid Build Coastguard Worker
setup1(void)576*49cdfc7eSAndroid Build Coastguard Worker static void setup1(void)
577*49cdfc7eSAndroid Build Coastguard Worker {
578*49cdfc7eSAndroid Build Coastguard Worker s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
579*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].proto);
580*49cdfc7eSAndroid Build Coastguard Worker if (tdat[testno].type == SOCK_STREAM &&
581*49cdfc7eSAndroid Build Coastguard Worker connect(s, (struct sockaddr *)tdat[testno].to,
582*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].tolen) < 0) {
583*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "connect failed: %s", strerror(errno));
584*49cdfc7eSAndroid Build Coastguard Worker }
585*49cdfc7eSAndroid Build Coastguard Worker }
586*49cdfc7eSAndroid Build Coastguard Worker
cleanup1(void)587*49cdfc7eSAndroid Build Coastguard Worker static void cleanup1(void)
588*49cdfc7eSAndroid Build Coastguard Worker {
589*49cdfc7eSAndroid Build Coastguard Worker close(s);
590*49cdfc7eSAndroid Build Coastguard Worker s = -1;
591*49cdfc7eSAndroid Build Coastguard Worker }
592*49cdfc7eSAndroid Build Coastguard Worker
setup2(void)593*49cdfc7eSAndroid Build Coastguard Worker static void setup2(void)
594*49cdfc7eSAndroid Build Coastguard Worker {
595*49cdfc7eSAndroid Build Coastguard Worker setup1(); /* get a socket in s */
596*49cdfc7eSAndroid Build Coastguard Worker if (shutdown(s, 1) < 0) {
597*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "socket setup failed connect "
598*49cdfc7eSAndroid Build Coastguard Worker "test %d: %s", testno, strerror(errno));
599*49cdfc7eSAndroid Build Coastguard Worker }
600*49cdfc7eSAndroid Build Coastguard Worker }
601*49cdfc7eSAndroid Build Coastguard Worker
setup3(void)602*49cdfc7eSAndroid Build Coastguard Worker static void setup3(void)
603*49cdfc7eSAndroid Build Coastguard Worker {
604*49cdfc7eSAndroid Build Coastguard Worker s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
605*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].proto);
606*49cdfc7eSAndroid Build Coastguard Worker }
607*49cdfc7eSAndroid Build Coastguard Worker
608*49cdfc7eSAndroid Build Coastguard Worker static char tmpfilename[1024];
609*49cdfc7eSAndroid Build Coastguard Worker static int tfd;
610*49cdfc7eSAndroid Build Coastguard Worker
setup4(void)611*49cdfc7eSAndroid Build Coastguard Worker static void setup4(void)
612*49cdfc7eSAndroid Build Coastguard Worker {
613*49cdfc7eSAndroid Build Coastguard Worker
614*49cdfc7eSAndroid Build Coastguard Worker setup1(); /* get a socket in s */
615*49cdfc7eSAndroid Build Coastguard Worker
616*49cdfc7eSAndroid Build Coastguard Worker strcpy(tmpfilename, "sockXXXXXX");
617*49cdfc7eSAndroid Build Coastguard Worker tfd = mkstemp(tmpfilename);
618*49cdfc7eSAndroid Build Coastguard Worker if (tfd < 0) {
619*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup4, "socket setup failed: %s",
620*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
621*49cdfc7eSAndroid Build Coastguard Worker }
622*49cdfc7eSAndroid Build Coastguard Worker control = (struct cmsghdr *)cbuf;
623*49cdfc7eSAndroid Build Coastguard Worker memset(cbuf, 0x00, sizeof(cbuf));
624*49cdfc7eSAndroid Build Coastguard Worker control->cmsg_len = sizeof(struct cmsghdr) + 4;
625*49cdfc7eSAndroid Build Coastguard Worker control->cmsg_level = SOL_SOCKET;
626*49cdfc7eSAndroid Build Coastguard Worker control->cmsg_type = SCM_RIGHTS;
627*49cdfc7eSAndroid Build Coastguard Worker *(int *)CMSG_DATA(control) = tfd;
628*49cdfc7eSAndroid Build Coastguard Worker controllen = control->cmsg_len;
629*49cdfc7eSAndroid Build Coastguard Worker }
630*49cdfc7eSAndroid Build Coastguard Worker
cleanup4(void)631*49cdfc7eSAndroid Build Coastguard Worker static void cleanup4(void)
632*49cdfc7eSAndroid Build Coastguard Worker {
633*49cdfc7eSAndroid Build Coastguard Worker cleanup1();
634*49cdfc7eSAndroid Build Coastguard Worker close(tfd);
635*49cdfc7eSAndroid Build Coastguard Worker tfd = -1;
636*49cdfc7eSAndroid Build Coastguard Worker control = 0;
637*49cdfc7eSAndroid Build Coastguard Worker controllen = 0;
638*49cdfc7eSAndroid Build Coastguard Worker }
639*49cdfc7eSAndroid Build Coastguard Worker
setup5(void)640*49cdfc7eSAndroid Build Coastguard Worker static void setup5(void)
641*49cdfc7eSAndroid Build Coastguard Worker {
642*49cdfc7eSAndroid Build Coastguard Worker s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
643*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].proto);
644*49cdfc7eSAndroid Build Coastguard Worker
645*49cdfc7eSAndroid Build Coastguard Worker SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
646*49cdfc7eSAndroid Build Coastguard Worker
647*49cdfc7eSAndroid Build Coastguard Worker /* slight change destination (port) so connect() is to different
648*49cdfc7eSAndroid Build Coastguard Worker * 5-tuple than already connected
649*49cdfc7eSAndroid Build Coastguard Worker */
650*49cdfc7eSAndroid Build Coastguard Worker sin2 = sin1;
651*49cdfc7eSAndroid Build Coastguard Worker sin2.sin_port = TST_GET_UNUSED_PORT(cleanup, AF_INET, SOCK_STREAM);
652*49cdfc7eSAndroid Build Coastguard Worker }
653*49cdfc7eSAndroid Build Coastguard Worker
setup6(void)654*49cdfc7eSAndroid Build Coastguard Worker static void setup6(void)
655*49cdfc7eSAndroid Build Coastguard Worker {
656*49cdfc7eSAndroid Build Coastguard Worker setup4();
657*49cdfc7eSAndroid Build Coastguard Worker /*
658*49cdfc7eSAndroid Build Coastguard Worker controllen = control->cmsg_len = sizeof(struct cmsghdr) - 4;
659*49cdfc7eSAndroid Build Coastguard Worker */
660*49cdfc7eSAndroid Build Coastguard Worker controllen = control->cmsg_len = 0;
661*49cdfc7eSAndroid Build Coastguard Worker }
662*49cdfc7eSAndroid Build Coastguard Worker
setup8(void)663*49cdfc7eSAndroid Build Coastguard Worker static void setup8(void)
664*49cdfc7eSAndroid Build Coastguard Worker {
665*49cdfc7eSAndroid Build Coastguard Worker setup4();
666*49cdfc7eSAndroid Build Coastguard Worker control = (struct cmsghdr *)-1;
667*49cdfc7eSAndroid Build Coastguard Worker }
668