ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/fdlist.c
Revision: 31
Committed: Sun Oct 2 20:34:05 2005 UTC (19 years, 10 months ago) by knight
Content type: text/x-csrc
Original Path: ircd-hybrid/src/fdlist.c
File size: 6161 byte(s)
Log Message:
- Fix svn:keywords

File Contents

# User Rev Content
1 adx 30 /*
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 knight 31 * $Id$
23 adx 30 */
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     if (F->dns_query != NULL)
166     {
167     delete_resolver_queries(F->dns_query);
168     MyFree(F->dns_query);
169     }
170    
171     #ifdef HAVE_LIBCRYPTO
172     if (F->ssl)
173     SSL_free(F->ssl);
174     #endif
175    
176     if (fd_hash[hashv] == F)
177     fd_hash[hashv] = F->hnext;
178     else {
179     fde_t *prev;
180    
181     /* let it core if not found */
182     for (prev = fd_hash[hashv]; prev->hnext != F; prev = prev->hnext)
183     ;
184     prev->hnext = F->hnext;
185     }
186    
187     /* Unlike squid, we're actually closing the FD here! -- adrian */
188     #ifdef _WIN32
189     if (F->flags.is_socket)
190     closesocket(F->fd);
191     else
192     CloseHandle((HANDLE)F->fd);
193     #else
194     close(F->fd);
195     #endif
196     number_fd--;
197     #ifdef INVARIANTS
198     memset(F, '\0', sizeof(fde_t));
199     #endif
200     }
201    
202     /*
203     * fd_dump() - dump the list of active filedescriptors
204     */
205     void
206     fd_dump(struct Client *source_p)
207     {
208     int i;
209     fde_t *F;
210    
211     for (i = 0; i < FD_HASH_SIZE; i++)
212     for (F = fd_hash[i]; F != NULL; F = F->hnext)
213     sendto_one(source_p, ":%s %d %s :fd %-5d desc '%s'",
214     me.name, RPL_STATSDEBUG, source_p->name,
215     F->fd, F->desc);
216     }
217    
218     /*
219     * fd_note() - set the fd note
220     *
221     * Note: must be careful not to overflow fd_table[fd].desc when
222     * calling.
223     */
224     void
225     fd_note(fde_t *F, const char *format, ...)
226     {
227     va_list args;
228    
229     if (format != NULL)
230     {
231     va_start(args, format);
232     vsnprintf(F->desc, sizeof(F->desc), format, args);
233     va_end(args);
234     }
235     else
236     F->desc[0] = '\0';
237     }
238    
239     /* Make sure stdio descriptors (0-2) and profiler descriptor (3)
240     * always go somewhere harmless. Use -foreground for profiling
241     * or executing from gdb */
242     #ifndef _WIN32
243     void
244     close_standard_fds(void)
245     {
246     int i;
247    
248     for (i = 0; i < LOWEST_SAFE_FD; i++)
249     {
250     close(i);
251     if (open(PATH_DEVNULL, O_RDWR) < 0)
252     exit(-1); /* we're hosed if we can't even open /dev/null */
253     }
254     }
255     #endif
256    
257     void
258     close_fds(fde_t *one)
259     {
260     int i;
261     fde_t *F;
262    
263     for (i = 0; i < FD_HASH_SIZE; i++)
264     for (F = fd_hash[i]; F != NULL; F = F->hnext)
265     if (F != one)
266     {
267     #ifdef _WIN32
268     if (F->flags.is_socket)
269     closesocket(F->fd);
270     else
271     CloseHandle((HANDLE)F->fd);
272     #else
273     close(F->fd);
274     #endif
275     }
276     }

Properties

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