Deleted Added
full compact
accept_fd_leak.c (135425) accept_fd_leak.c (136843)
1/*-
2 * Copyright (c) 2004 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 2004 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/tools/regression/sockets/accept_fd_leak/accept_fd_leak.c 135425 2004-09-18 13:06:00Z rwatson $
26 * $FreeBSD: head/tools/regression/sockets/accept_fd_leak/accept_fd_leak.c 136843 2004-10-23 22:11:35Z rwatson $
27 */
28
29#include <sys/types.h>
30#include <sys/socket.h>
31
32#include <netinet/in.h>
33
27 */
28
29#include <sys/types.h>
30#include <sys/socket.h>
31
32#include <netinet/in.h>
33
34#include <err.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
41#define LOOPS 500

--- 12 unchanged lines hidden (view full) ---

54 socklen_t size;
55 int fd1, fd2, fd3, i, s;
56
57 /*
58 * Check for sequential fd allocation, and give up early if not.
59 */
60 fd1 = dup(STDIN_FILENO);
61 fd2 = dup(STDIN_FILENO);
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
42#define LOOPS 500

--- 12 unchanged lines hidden (view full) ---

55 socklen_t size;
56 int fd1, fd2, fd3, i, s;
57
58 /*
59 * Check for sequential fd allocation, and give up early if not.
60 */
61 fd1 = dup(STDIN_FILENO);
62 fd2 = dup(STDIN_FILENO);
62 if (fd2 != fd1 + 1) {
63 fprintf(stderr, "Non-sequential fd allocation!\n");
64 exit(-1);
65 }
63 if (fd2 != fd1 + 1)
64 errx(-1, "Non-sequential fd allocation\n");
66
67 s = socket(PF_INET, SOCK_STREAM, 0);
65
66 s = socket(PF_INET, SOCK_STREAM, 0);
68 if (s == -1) {
69 perror("socket");
70 exit(-1);
71 }
67 if (s == -1)
68 errx(-1, "socket: %s", strerror(errno));
72
73 bzero(&sin, sizeof(sin));
74 sin.sin_len = sizeof(sin);
75 sin.sin_family = AF_INET;
76 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
77 sin.sin_port = htons(8080);
78
69
70 bzero(&sin, sizeof(sin));
71 sin.sin_len = sizeof(sin);
72 sin.sin_family = AF_INET;
73 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
74 sin.sin_port = htons(8080);
75
79 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
80 perror("bind");
81 exit(-1);
82 }
76 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0)
77 errx(-1, "bind: %s", strerror(errno));
83
78
84 if (listen(s, -1) != 0) {
85 perror("listen");
86 exit(-1);
87 }
79 if (listen(s, -1) != 0)
80 errx(-1, "listen: %s", strerror(errno));
88
89 i = fcntl(s, F_GETFL);
81
82 i = fcntl(s, F_GETFL);
90 if (i == -1) {
91 perror("F_GETFL");
92 exit(-1);
93 }
83 if (i == -1)
84 errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
94 i |= O_NONBLOCK;
85 i |= O_NONBLOCK;
95 if (fcntl(s, F_SETFL, i) != 0) {
96 perror("F_SETFL");
97 exit(-1);
98 }
86 if (fcntl(s, F_SETFL, i) != 0)
87 errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
99 i = fcntl(s, F_GETFL);
88 i = fcntl(s, F_GETFL);
100 if (i == -1) {
101 perror("F_GETFL");
102 exit(-1);
103 }
104 if ((i & O_NONBLOCK) != O_NONBLOCK) {
105 fprintf(stderr, "Failed to set O_NONBLOCK (i=%d)\n", i);
106 exit(-1);
107 }
89 if (i == -1)
90 errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
91 if ((i & O_NONBLOCK) != O_NONBLOCK)
92 errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i);
108
109 for (i = 0; i < LOOPS; i++) {
110 size = sizeof(sin);
93
94 for (i = 0; i < LOOPS; i++) {
95 size = sizeof(sin);
111 if (accept(s, (struct sockaddr *)&sin, &size) != -1) {
112 fprintf(stderr, "accept succeeded!\n");
113 exit(-1);
114 }
115 if (errno != EAGAIN) {
116 perror("accept");
117 exit(-1);
118 }
96 if (accept(s, (struct sockaddr *)&sin, &size) != -1)
97 errx(-1, "accept succeeded\n");
98 if (errno != EAGAIN)
99 errx(-1, "accept: %s", strerror(errno));
119 }
120
121 /*
122 * Allocate a file descriptor and make sure it's fd2+2. 2 because
123 * we allocate an fd for the socket.
124 */
125 fd3 = dup(STDIN_FILENO);
100 }
101
102 /*
103 * Allocate a file descriptor and make sure it's fd2+2. 2 because
104 * we allocate an fd for the socket.
105 */
106 fd3 = dup(STDIN_FILENO);
126 if (fd3 != fd2 + 2) {
127 fprintf(stderr, "FAIL (%d, %d, %d)\n", fd1, fd2, fd3);
128 exit(-1);
129 } else
107 if (fd3 != fd2 + 2)
108 errx(-1, "FAIL (%d, %d, %d)\n", fd1, fd2, fd3);
109 else
130 fprintf(stderr, "PASS\n");
131
132 return (0);
133}
110 fprintf(stderr, "PASS\n");
111
112 return (0);
113}