Debian Codegraph @

src/fd.c

raw
1 /*
2 * stunnel TLS offloading and load-balancing proxy
3 * Copyright (C) 1998-2026 Michal Trojnara <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * 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.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses>.
17 *
18 * Linking stunnel statically or dynamically with other modules is making
19 * a combined work based on stunnel. Thus, the terms and conditions of
20 * the GNU General Public License cover the whole combination.
21 *
22 * In addition, as a special exception, the copyright holder of stunnel
23 * gives you permission to combine stunnel with free software programs or
24 * libraries that are released under the GNU LGPL and with code included
25 * in the standard release of OpenSSL under the OpenSSL License (or
26 * modified versions of such code, with unchanged license). You may copy
27 * and distribute such a system following the terms of the GNU GPL for
28 * stunnel and the licenses of the other code concerned.
29 *
30 * Note that people who make modified versions of stunnel are not obligated
31 * to grant this special exception for their modified versions; it is their
32 * choice whether to do so. The GNU General Public License gives permission
33 * to release a modified version without this exception; this exception
34 * also makes it possible to release a modified version which carries
35 * forward this exception.
36 */
37
38 #include "prototypes.h"
39
40 #if defined HAVE_PIPE2 && defined HAVE_ACCEPT4
41 #define USE_NEW_LINUX_API 1
42 #endif
43
44 /* try to use non-POSIX O_NDELAY on obsolete BSD systems */
45 #if !defined O_NONBLOCK && defined O_NDELAY
46 #define O_NONBLOCK O_NDELAY
47 #endif
48
49 /**************************************** prototypes */
50
51 NOEXPORT SOCKET setup_fd(SOCKET, int, const char *);
52
53 /**************************************** internal limit of file descriptors */
54
55 #ifndef USE_FORK
56
57 static SOCKET max_fds;
58
59 void get_limits(void) { /* set max_fds and max_clients */
60 /* start with current ulimit */
61 #if defined(HAVE_SYSCONF)
62 errno=0;
63 max_fds=(SOCKET)sysconf(_SC_OPEN_MAX);
64 if(errno)
65 ioerror("sysconf");
66 if(max_fds<0)
67 max_fds=0; /* unlimited */
68 #elif defined(HAVE_GETRLIMIT)
69 struct rlimit rlim;
70
71 if(getrlimit(RLIMIT_NOFILE, &rlim)<0) {
72 ioerror("getrlimit");
73 max_fds=0; /* unlimited */
74 } else
75 max_fds=rlim.rlim_cur!=RLIM_INFINITY ? rlim.rlim_cur : 0;
76 #else
77 max_fds=0; /* unlimited */
78 #endif /* HAVE_SYSCONF || HAVE_GETRLIMIT */
79
80 #if !defined(USE_WIN32) && !defined(USE_POLL) && !defined(__INNOTEK_LIBC__)
81 /* apply FD_SETSIZE if select() is used on Unix */
82 if(!max_fds || max_fds>FD_SETSIZE)
83 max_fds=FD_SETSIZE; /* start with select() limit */
84 #endif /* select() on Unix */
85
86 /* stunnel needs at least 16 file descriptors */
87 if(max_fds && max_fds<16)
88 max_fds=16;
89
90 if(max_fds) {
91 max_clients=(int)(max_fds>=256 ? max_fds*125/256 : (max_fds-6)/2);
92 s_log(LOG_DEBUG, "Clients allowed=%d", max_clients);
93 } else {
94 max_clients=0;
95 s_log(LOG_DEBUG, "No limit detected for the number of clients");
96 }
97 }
98
99 #endif
100
101 /**************************************** file descriptor validation */
102
103 SOCKET s_socket(int domain, int type, int protocol, int nonblock, const char *msg) {
104 SOCKET fd;
105
106 #ifdef USE_NEW_LINUX_API
107 if(nonblock)
108 type|=SOCK_NONBLOCK;
109 type|=SOCK_CLOEXEC;
110 #endif
111 #ifdef USE_WIN32
112 /* http://stackoverflow.com/questions/4993119 */
113 /* CreateProcess() needs a non-overlapped handle */
114 fd=WSASocket(domain, type, protocol, NULL, 0, 0);
115 #else /* USE_WIN32 */
116 fd=socket(domain, type, protocol);
117 #endif /* USE_WIN32 */
118 return setup_fd(fd, nonblock, msg);
119 }
120
121 SOCKET s_accept(SOCKET sockfd, struct sockaddr *addr, socklen_t *addrlen,
122 int nonblock, const char *msg) {
123 SOCKET fd;
124
125 #ifdef USE_NEW_LINUX_API
126 if(nonblock)
127 fd=accept4(sockfd, addr, addrlen, SOCK_NONBLOCK|SOCK_CLOEXEC);
128 else
129 fd=accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);
130 #else
131 fd=accept(sockfd, addr, addrlen);
132 #endif
133 return setup_fd(fd, nonblock, msg);
134 }
135
136 #ifndef USE_WIN32
137
138 int s_socketpair(int domain, int type, int protocol, SOCKET sv[2],
139 int nonblock, const char *msg) {
140 #ifdef USE_NEW_LINUX_API
141 if(nonblock)
142 type|=SOCK_NONBLOCK;
143 type|=SOCK_CLOEXEC;
144 #endif
145 if(socketpair(domain, type, protocol, sv)<0) {
146 ioerror(msg);
147 return -1;
148 }
149 if(setup_fd(sv[0], nonblock, msg)<0) {
150 closesocket(sv[1]);
151 return -1;
152 }
153 if(setup_fd(sv[1], nonblock, msg)<0) {
154 closesocket(sv[0]);
155 return -1;
156 }
157 return 0;
158 }
159
160 int s_pipe(int pipefd[2], int nonblock, const char *msg) {
161 int retval;
162
163 #ifdef USE_NEW_LINUX_API
164 if(nonblock)
165 retval=pipe2(pipefd, O_NONBLOCK|O_CLOEXEC);
166 else
167 retval=pipe2(pipefd, O_CLOEXEC);
168 #else
169 retval=pipe(pipefd);
170 #endif
171 if(retval<0) {
172 ioerror(msg);
173 return -1;
174 }
175 if(setup_fd(pipefd[0], nonblock, msg)<0) {
176 close(pipefd[1]);
177 return -1;
178 }
179 if(setup_fd(pipefd[1], nonblock, msg)<0) {
180 close(pipefd[0]);
181 return -1;
182 }
183 return 0;
184 }
185
186 #endif /* USE_WIN32 */
187
188 NOEXPORT SOCKET setup_fd(SOCKET fd, int nonblock, const char *msg) {
189 #if !defined USE_NEW_LINUX_API && defined FD_CLOEXEC
190 int err;
191 #endif
192
193 if(fd==INVALID_SOCKET) {
194 sockerror(msg);
195 return INVALID_SOCKET;
196 }
197 #ifndef USE_FORK
198 if(max_fds && fd>=max_fds) {
199 s_log(LOG_ERR, "%s: FD=%ld out of range (max %d)",
200 msg, (long)fd, (int)max_fds);
201 closesocket(fd);
202 return INVALID_SOCKET;
203 }
204 #endif
205
206 #ifdef USE_NEW_LINUX_API
207 (void)nonblock; /* squash the unused parameter warning */
208 #else /* set O_NONBLOCK and F_SETFD */
209 set_nonblock(fd, (unsigned long)nonblock);
210 #ifdef FD_CLOEXEC
211 do {
212 err=fcntl(fd, F_SETFD, FD_CLOEXEC);
213 } while(err<0 && get_last_socket_error()==S_EINTR);
214 if(err<0)
215 sockerror("fcntl SETFD"); /* non-critical */
216 #endif /* FD_CLOEXEC */
217 #endif /* USE_NEW_LINUX_API */
218
219 #ifdef DEBUG_FD_ALLOC
220 s_log(LOG_DEBUG, "%s: FD=%ld allocated (%sblocking mode)",
221 msg, (long)fd, nonblock?"non-":"");
222 #endif /* DEBUG_FD_ALLOC */
223
224 return fd;
225 }
226
227 void set_nonblock(SOCKET fd, unsigned long nonblock) {
228 #if defined F_GETFL && defined F_SETFL && defined O_NONBLOCK && !defined __INNOTEK_LIBC__
229 int err, flags;
230
231 do {
232 flags=fcntl(fd, F_GETFL, 0);
233 } while(flags<0 && get_last_socket_error()==S_EINTR);
234 if(flags<0) {
235 sockerror("fcntl GETFL"); /* non-critical */
236 return;
237 }
238 if(nonblock)
239 flags|=O_NONBLOCK;
240 else
241 flags&=~O_NONBLOCK;
242 do {
243 err=fcntl(fd, F_SETFL, flags);
244 } while(err<0 && get_last_socket_error()==S_EINTR);
245 if(err<0)
246 sockerror("fcntl SETFL"); /* non-critical */
247 #else /* WIN32 or similar */
248 if(ioctlsocket(fd, (long)FIONBIO, &nonblock)<0)
249 sockerror("ioctlsocket"); /* non-critical */
250 #if 0
251 else
252 s_log(LOG_DEBUG, "Socket %d set to %s mode",
253 fd, nonblock ? "non-blocking" : "blocking");
254 #endif
255 #endif
256 }
257
258 /* end of fd.c */