Fri Feb 16 22:20:18 2018 UTC ()
Use the same variable name for the accepted socket as with the AF_LOCAL test.
Call getpeereid on the accepted socket.


(christos)
diff -r1.9 -r1.10 src/tests/net/net/t_tcp.c

cvs diff -r1.9 -r1.10 src/tests/net/net/t_tcp.c (expand / switch to unified diff)

--- src/tests/net/net/t_tcp.c 2018/02/16 19:24:16 1.9
+++ src/tests/net/net/t_tcp.c 2018/02/16 22:20:18 1.10
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: t_tcp.c,v 1.9 2018/02/16 19:24:16 christos Exp $ */ 1/* $NetBSD: t_tcp.c,v 1.10 2018/02/16 22:20:18 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE. 33 * POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#ifdef __RCSID 37#ifdef __RCSID
38__RCSID("$Id: t_tcp.c,v 1.9 2018/02/16 19:24:16 christos Exp $"); 38__RCSID("$Id: t_tcp.c,v 1.10 2018/02/16 22:20:18 christos Exp $");
39#endif 39#endif
40 40
41/* Example code. Should block; does with accept not paccept. */ 41/* Example code. Should block; does with accept not paccept. */
42/* Original by: Justin Cormack <justin@specialbusrvrervice.com> */ 42/* Original by: Justin Cormack <justin@specialbusrvrervice.com> */
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <sys/types.h> 45#include <sys/types.h>
46#include <sys/socket.h> 46#include <sys/socket.h>
47#include <netinet/in.h> 47#include <netinet/in.h>
48#include <stdio.h> 48#include <stdio.h>
49#include <sched.h> 49#include <sched.h>
50#include <stdbool.h> 50#include <stdbool.h>
51#include <unistd.h> 51#include <unistd.h>
@@ -61,27 +61,27 @@ __RCSID("$Id: t_tcp.c,v 1.9 2018/02/16 1 @@ -61,27 +61,27 @@ __RCSID("$Id: t_tcp.c,v 1.9 2018/02/16 1
61#ifdef __linux__ 61#ifdef __linux__
62#define paccept(a, b, c, d, e) accept4((a), (b), (c), (e)) 62#define paccept(a, b, c, d, e) accept4((a), (b), (c), (e))
63#endif 63#endif
64 64
65static void 65static void
66ding(int al) 66ding(int al)
67{ 67{
68} 68}
69 69
70static void  70static void
71paccept_block(sa_family_t sfamily, sa_family_t cfamily, 71paccept_block(sa_family_t sfamily, sa_family_t cfamily,
72 bool pacceptblock, bool fcntlblock) 72 bool pacceptblock, bool fcntlblock)
73{ 73{
74 int srvr = -1, clnt = -1, as = -1; 74 int srvr = -1, clnt = -1, acpt = -1;
75 int ok, fl; 75 int ok, fl;
76 int count = 5; 76 int count = 5;
77 ssize_t n; 77 ssize_t n;
78 char buf[10]; 78 char buf[10];
79 struct sockaddr_storage ss, bs; 79 struct sockaddr_storage ss, bs;
80 struct sockaddr_in *sin; 80 struct sockaddr_in *sin;
81 struct sockaddr_in6 *sin6; 81 struct sockaddr_in6 *sin6;
82 struct sigaction sa; 82 struct sigaction sa;
83 socklen_t slen; 83 socklen_t slen;
84 uid_t euid; 84 uid_t euid;
85 gid_t egid; 85 gid_t egid;
86 86
87 srvr = socket(sfamily, SOCK_STREAM | SOCK_NONBLOCK, 0); 87 srvr = socket(sfamily, SOCK_STREAM | SOCK_NONBLOCK, 0);
@@ -144,27 +144,27 @@ paccept_block(sa_family_t sfamily, sa_fa @@ -144,27 +144,27 @@ paccept_block(sa_family_t sfamily, sa_fa
144 addrlen = sizeof(*sin); 144 addrlen = sizeof(*sin);
145#ifdef BSD4_4 145#ifdef BSD4_4
146 sin->sin_len = sizeof(*sin); 146 sin->sin_len = sizeof(*sin);
147#endif 147#endif
148 sin->sin_family = AF_INET; 148 sin->sin_family = AF_INET;
149 sin->sin_port = sin6->sin6_port; 149 sin->sin_port = sin6->sin6_port;
150 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 150 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
151 } 151 }
152 152
153 /* may not connect first time */ 153 /* may not connect first time */
154 ok = connect(clnt, (struct sockaddr *) &bs, addrlen); 154 ok = connect(clnt, (struct sockaddr *) &bs, addrlen);
155 if (ok != -1 || errno != EINPROGRESS) 155 if (ok != -1 || errno != EINPROGRESS)
156 FAIL("expected connect to fail"); 156 FAIL("expected connect to fail");
157 as = paccept(srvr, NULL, NULL, NULL, pacceptblock ? 0 : SOCK_NONBLOCK); 157 acpt = paccept(srvr, NULL, NULL, NULL, pacceptblock ? 0 : SOCK_NONBLOCK);
158again: 158again:
159 ok = connect(clnt, (struct sockaddr *) &bs, addrlen); 159 ok = connect(clnt, (struct sockaddr *) &bs, addrlen);
160 if (ok == -1 && errno != EISCONN) { 160 if (ok == -1 && errno != EISCONN) {
161 if (count-- && errno == EALREADY) { 161 if (count-- && errno == EALREADY) {
162 fprintf(stderr, "retry\n"); 162 fprintf(stderr, "retry\n");
163 struct timespec ts; 163 struct timespec ts;
164 ts.tv_sec = 0; 164 ts.tv_sec = 0;
165 ts.tv_nsec = 1000000; 165 ts.tv_nsec = 1000000;
166 nanosleep(&ts, NULL); 166 nanosleep(&ts, NULL);
167#if 0 167#if 0
168 sched_yield(); 168 sched_yield();
169#endif 169#endif
170 goto again; 170 goto again;
@@ -172,71 +172,72 @@ again: @@ -172,71 +172,72 @@ again:
172 FAIL("connect failed"); 172 FAIL("connect failed");
173 } 173 }
174 174
175#if 0 175#if 0
176 fl = fcntl(srvr, F_GETFL, 0); 176 fl = fcntl(srvr, F_GETFL, 0);
177 if (fl == -1) 177 if (fl == -1)
178 FAIL("fnctl getfl"); 178 FAIL("fnctl getfl");
179 179
180 ok = fcntl(srvr, F_SETFL, fl & ~O_NONBLOCK); 180 ok = fcntl(srvr, F_SETFL, fl & ~O_NONBLOCK);
181 if (ok == -1) 181 if (ok == -1)
182 FAIL("fnctl setfl"); 182 FAIL("fnctl setfl");
183#endif 183#endif
184 184
 185 if (acpt == -1) { /* not true under NetBSD */
 186 acpt = paccept(srvr, NULL, NULL, NULL,
 187 pacceptblock ? 0 : SOCK_NONBLOCK);
 188 if (acpt == -1)
 189 FAIL("paccept");
 190 }
185 /* This is supposed to only work on Unix sockets but returns garbage */ 191 /* This is supposed to only work on Unix sockets but returns garbage */
186 if (getpeereid(clnt, &euid, &egid) != -1) 192 if (getpeereid(clnt, &euid, &egid) != -1)
187 FAIL("getpeereid(clnt)"); 193 FAIL("getpeereid(clnt)");
188 /* This is supposed to only work on Unix sockets but returns garbage */ 194 /* This is supposed to only work on Unix sockets but returns garbage */
189 if (getpeereid(srvr, &euid, &egid) != -1) 195 if (getpeereid(acpt, &euid, &egid) != -1)
190 FAIL("getpeereid(srvr)"); 196 FAIL("getpeereid(srvr)");
191 197
192 if (as == -1) { /* not true under NetBSD */ 
193 as = paccept(srvr, NULL, NULL, NULL, pacceptblock ? 0 : SOCK_NONBLOCK); 
194 if (as == -1) 
195 FAIL("paccept"); 
196 } 
197 if (fcntlblock) { 198 if (fcntlblock) {
198 fl = fcntl(as, F_GETFL, 0); 199 fl = fcntl(acpt, F_GETFL, 0);
199 if (fl == -1) 200 if (fl == -1)
200 FAIL("fnctl"); 201 FAIL("fnctl");
201 if (fl != (O_RDWR|O_NONBLOCK)) 202 if (fl != (O_RDWR|O_NONBLOCK))
202 FAIL("fl 0x%x != 0x%x\n", fl, O_RDWR|O_NONBLOCK); 203 FAIL("fl 0x%x != 0x%x\n", fl, O_RDWR|O_NONBLOCK);
203 ok = fcntl(as, F_SETFL, fl & ~O_NONBLOCK); 204 ok = fcntl(acpt, F_SETFL, fl & ~O_NONBLOCK);
204 if (ok == -1) 205 if (ok == -1)
205 FAIL("fnctl setfl"); 206 FAIL("fnctl setfl");
206 207
207 fl = fcntl(as, F_GETFL, 0); 208 fl = fcntl(acpt, F_GETFL, 0);
208 if (fl & O_NONBLOCK) 209 if (fl & O_NONBLOCK)
209 FAIL("fl non blocking after reset"); 210 FAIL("fl non blocking after reset");
210 } 211 }
211 sa.sa_handler = ding; 212 sa.sa_handler = ding;
212 sa.sa_flags = 0; 213 sa.sa_flags = 0;
213 sigemptyset(&sa.sa_mask); 214 sigemptyset(&sa.sa_mask);
214 sigaction(SIGALRM, &sa, NULL); 215 sigaction(SIGALRM, &sa, NULL);
215 alarm(1); 216 alarm(1);
216 n = read(as, buf, 10); 217 n = read(acpt, buf, 10);
217 218
218 if (pacceptblock || fcntlblock) { 219 if (pacceptblock || fcntlblock) {
219 if (n == -1 && errno != EINTR) 220 if (n == -1 && errno != EINTR)
220 FAIL("read"); 221 FAIL("read");
221 } else { 222 } else {
222 if (n != -1 || errno != EWOULDBLOCK) 223 if (n != -1 || errno != EWOULDBLOCK)
223 FAIL("read"); 224 FAIL("read");
224 } 225 }
225 return; 226 return;
226fail: 227fail:
227 close(srvr); 228 close(srvr);
228 close(clnt); 229 close(clnt);
229 close(as); 230 close(acpt);
230} 231}
231 232
232#ifndef TEST 233#ifndef TEST
233 234
234ATF_TC(paccept44_reset_nonblock); 235ATF_TC(paccept44_reset_nonblock);
235ATF_TC_HEAD(paccept44_reset_nonblock, tc) 236ATF_TC_HEAD(paccept44_reset_nonblock, tc)
236{ 237{
237 238
238 atf_tc_set_md_var(tc, "descr", "Check that paccept(2) resets " 239 atf_tc_set_md_var(tc, "descr", "Check that paccept(2) resets "
239 "the non-blocking flag on non-blocking sockets (ipv4->ipv4)"); 240 "the non-blocking flag on non-blocking sockets (ipv4->ipv4)");
240} 241}
241 242
242ATF_TC_BODY(paccept44_reset_nonblock, tc) 243ATF_TC_BODY(paccept44_reset_nonblock, tc)