ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-7.2/src/fdlist.c
Revision: 992
Committed: Mon Aug 17 19:19:16 2009 UTC (14 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 6059 byte(s)
Log Message:
- fix possible auth/dns related memleaks

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * fdlist.c: Maintains a list of file descriptors.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24 #include "stdinc.h"
25 #include "fdlist.h"
26 #include "client.h" /* struct Client */
27 #include "common.h"
28 #include "event.h"
29 #include "ircd.h" /* GlobalSetOptions */
30 #include "irc_string.h"
31 #include "rlimits.h"
32 #include "s_bsd.h" /* comm_setselect */
33 #include "s_conf.h" /* ServerInfo */
34 #include "send.h"
35 #include "memory.h"
36 #include "numeric.h"
37
38 fde_t *fd_hash[FD_HASH_SIZE];
39 fde_t *fd_next_in_loop = NULL;
40 int number_fd = LEAKED_FDS;
41 int hard_fdlimit = 0;
42 struct Callback *fdlimit_cb = NULL;
43
44 static void *
45 changing_fdlimit(va_list args)
46 {
47 int old_fdlimit = hard_fdlimit;
48
49 hard_fdlimit = va_arg(args, int);
50
51 if (ServerInfo.max_clients > MAXCLIENTS_MAX)
52 {
53 if (old_fdlimit != 0)
54 sendto_realops_flags(UMODE_ALL, L_ALL,
55 "HARD_FDLIMIT changed to %d, adjusting MAXCLIENTS to %d",
56 hard_fdlimit, MAXCLIENTS_MAX);
57
58 ServerInfo.max_clients = MAXCLIENTS_MAX;
59 }
60
61 return NULL;
62 }
63
64 void
65 fdlist_init(void)
66 {
67 memset(&fd_hash, 0, sizeof(fd_hash));
68
69 fdlimit_cb = register_callback("changing_fdlimit", changing_fdlimit);
70 eventAddIsh("recalc_fdlimit", recalc_fdlimit, NULL, 58);
71 recalc_fdlimit(NULL);
72 }
73
74 void
75 recalc_fdlimit(void *unused)
76 {
77 #ifdef _WIN32
78 /* this is what WSAStartup() usually returns. Even though they say
79 * the value is for compatibility reasons and should be ignored,
80 * we actually can create even more sockets... */
81 hard_fdlimit = 32767;
82 #else
83 int fdmax;
84 struct rlimit limit;
85
86 if (!getrlimit(RLIMIT_FD_MAX, &limit))
87 {
88 limit.rlim_cur = limit.rlim_max;
89 setrlimit(RLIMIT_FD_MAX, &limit);
90 }
91
92 fdmax = getdtablesize();
93
94 /* allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and
95 * some not really LEAKED_FDS */
96 fdmax = IRCD_MAX(fdmax, LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN);
97
98 /* under no condition shall this raise over 65536
99 * for example user ip heap is sized 2*hard_fdlimit */
100 fdmax = IRCD_MIN(fdmax, 65536);
101
102 if (fdmax != hard_fdlimit)
103 execute_callback(fdlimit_cb, fdmax);
104 #endif
105 }
106
107 static inline unsigned int
108 hash_fd(int fd)
109 {
110 #ifdef _WIN32
111 return ((((unsigned) fd) >> 2) % FD_HASH_SIZE);
112 #else
113 return (((unsigned) fd) % FD_HASH_SIZE);
114 #endif
115 }
116
117 fde_t *
118 lookup_fd(int fd)
119 {
120 fde_t *F = fd_hash[hash_fd(fd)];
121
122 while (F)
123 {
124 if (F->fd == fd)
125 return (F);
126 F = F->hnext;
127 }
128
129 return (NULL);
130 }
131
132 /* Called to open a given filedescriptor */
133 void
134 fd_open(fde_t *F, int fd, int is_socket, const char *desc)
135 {
136 unsigned int hashv = hash_fd(fd);
137 assert(fd >= 0);
138
139 F->fd = fd;
140 F->comm_index = -1;
141 if (desc)
142 strlcpy(F->desc, desc, sizeof(F->desc));
143 /* Note: normally we'd have to clear the other flags,
144 * but currently F is always cleared before calling us.. */
145 F->flags.open = 1;
146 F->flags.is_socket = is_socket;
147 F->hnext = fd_hash[hashv];
148 fd_hash[hashv] = F;
149
150 number_fd++;
151 }
152
153 /* Called to close a given filedescriptor */
154 void
155 fd_close(fde_t *F)
156 {
157 unsigned int hashv = hash_fd(F->fd);
158
159 if (F == fd_next_in_loop)
160 fd_next_in_loop = F->hnext;
161
162 if (F->flags.is_socket)
163 comm_setselect(F, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
164
165 delete_resolver_queries(F);
166
167 #ifdef HAVE_LIBCRYPTO
168 if (F->ssl)
169 SSL_free(F->ssl);
170 #endif
171
172 if (fd_hash[hashv] == F)
173 fd_hash[hashv] = F->hnext;
174 else {
175 fde_t *prev;
176
177 /* let it core if not found */
178 for (prev = fd_hash[hashv]; prev->hnext != F; prev = prev->hnext)
179 ;
180 prev->hnext = F->hnext;
181 }
182
183 /* Unlike squid, we're actually closing the FD here! -- adrian */
184 #ifdef _WIN32
185 if (F->flags.is_socket)
186 closesocket(F->fd);
187 else
188 CloseHandle((HANDLE)F->fd);
189 #else
190 close(F->fd);
191 #endif
192 number_fd--;
193
194 memset(F, 0, sizeof(fde_t));
195 }
196
197 /*
198 * fd_dump() - dump the list of active filedescriptors
199 */
200 void
201 fd_dump(struct Client *source_p)
202 {
203 int i;
204 fde_t *F;
205
206 for (i = 0; i < FD_HASH_SIZE; i++)
207 for (F = fd_hash[i]; F != NULL; F = F->hnext)
208 sendto_one(source_p, ":%s %d %s :fd %-5d desc '%s'",
209 me.name, RPL_STATSDEBUG, source_p->name,
210 F->fd, F->desc);
211 }
212
213 /*
214 * fd_note() - set the fd note
215 *
216 * Note: must be careful not to overflow fd_table[fd].desc when
217 * calling.
218 */
219 void
220 fd_note(fde_t *F, const char *format, ...)
221 {
222 va_list args;
223
224 if (format != NULL)
225 {
226 va_start(args, format);
227 vsnprintf(F->desc, sizeof(F->desc), format, args);
228 va_end(args);
229 }
230 else
231 F->desc[0] = '\0';
232 }
233
234 /* Make sure stdio descriptors (0-2) and profiler descriptor (3)
235 * always go somewhere harmless. Use -foreground for profiling
236 * or executing from gdb */
237 #ifndef _WIN32
238 void
239 close_standard_fds(void)
240 {
241 int i;
242
243 for (i = 0; i < LOWEST_SAFE_FD; i++)
244 {
245 close(i);
246 if (open(PATH_DEVNULL, O_RDWR) < 0)
247 exit(-1); /* we're hosed if we can't even open /dev/null */
248 }
249 }
250 #endif
251
252 void
253 close_fds(fde_t *one)
254 {
255 int i;
256 fde_t *F;
257
258 for (i = 0; i < FD_HASH_SIZE; i++)
259 for (F = fd_hash[i]; F != NULL; F = F->hnext)
260 if (F != one)
261 {
262 #ifdef _WIN32
263 if (F->flags.is_socket)
264 closesocket(F->fd);
265 else
266 CloseHandle((HANDLE)F->fd);
267 #else
268 close(F->fd);
269 #endif
270 }
271 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision