/[svn]/ircd-hybrid-7.2/src/fdlist.c
ViewVC logotype

Contents of /ircd-hybrid-7.2/src/fdlist.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1001 - (show annotations)
Sat Aug 29 22:44:44 2009 UTC (11 years, 6 months ago) by michael
File MIME type: text/x-chdr
File size: 5454 byte(s)
- remove half done and broken win32 support

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 int fdmax;
78 struct rlimit limit;
79
80 if (!getrlimit(RLIMIT_FD_MAX, &limit))
81 {
82 limit.rlim_cur = limit.rlim_max;
83 setrlimit(RLIMIT_FD_MAX, &limit);
84 }
85
86 fdmax = getdtablesize();
87
88 /* allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and
89 * some not really LEAKED_FDS */
90 fdmax = IRCD_MAX(fdmax, LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN);
91
92 /* under no condition shall this raise over 65536
93 * for example user ip heap is sized 2*hard_fdlimit */
94 fdmax = IRCD_MIN(fdmax, 65536);
95
96 if (fdmax != hard_fdlimit)
97 execute_callback(fdlimit_cb, fdmax);
98 }
99
100 static inline unsigned int
101 hash_fd(int fd)
102 {
103 return (((unsigned) fd) % FD_HASH_SIZE);
104 }
105
106 fde_t *
107 lookup_fd(int fd)
108 {
109 fde_t *F = fd_hash[hash_fd(fd)];
110
111 while (F)
112 {
113 if (F->fd == fd)
114 return (F);
115 F = F->hnext;
116 }
117
118 return (NULL);
119 }
120
121 /* Called to open a given filedescriptor */
122 void
123 fd_open(fde_t *F, int fd, int is_socket, const char *desc)
124 {
125 unsigned int hashv = hash_fd(fd);
126 assert(fd >= 0);
127
128 F->fd = fd;
129 F->comm_index = -1;
130 if (desc)
131 strlcpy(F->desc, desc, sizeof(F->desc));
132 /* Note: normally we'd have to clear the other flags,
133 * but currently F is always cleared before calling us.. */
134 F->flags.open = 1;
135 F->flags.is_socket = is_socket;
136 F->hnext = fd_hash[hashv];
137 fd_hash[hashv] = F;
138
139 number_fd++;
140 }
141
142 /* Called to close a given filedescriptor */
143 void
144 fd_close(fde_t *F)
145 {
146 unsigned int hashv = hash_fd(F->fd);
147
148 if (F == fd_next_in_loop)
149 fd_next_in_loop = F->hnext;
150
151 if (F->flags.is_socket)
152 comm_setselect(F, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
153
154 delete_resolver_queries(F);
155
156 #ifdef HAVE_LIBCRYPTO
157 if (F->ssl)
158 SSL_free(F->ssl);
159 #endif
160
161 if (fd_hash[hashv] == F)
162 fd_hash[hashv] = F->hnext;
163 else {
164 fde_t *prev;
165
166 /* let it core if not found */
167 for (prev = fd_hash[hashv]; prev->hnext != F; prev = prev->hnext)
168 ;
169 prev->hnext = F->hnext;
170 }
171
172 /* Unlike squid, we're actually closing the FD here! -- adrian */
173 close(F->fd);
174 number_fd--;
175
176 memset(F, 0, sizeof(fde_t));
177 }
178
179 /*
180 * fd_dump() - dump the list of active filedescriptors
181 */
182 void
183 fd_dump(struct Client *source_p)
184 {
185 int i;
186 fde_t *F;
187
188 for (i = 0; i < FD_HASH_SIZE; i++)
189 for (F = fd_hash[i]; F != NULL; F = F->hnext)
190 sendto_one(source_p, ":%s %d %s :fd %-5d desc '%s'",
191 me.name, RPL_STATSDEBUG, source_p->name,
192 F->fd, F->desc);
193 }
194
195 /*
196 * fd_note() - set the fd note
197 *
198 * Note: must be careful not to overflow fd_table[fd].desc when
199 * calling.
200 */
201 void
202 fd_note(fde_t *F, const char *format, ...)
203 {
204 va_list args;
205
206 if (format != NULL)
207 {
208 va_start(args, format);
209 vsnprintf(F->desc, sizeof(F->desc), format, args);
210 va_end(args);
211 }
212 else
213 F->desc[0] = '\0';
214 }
215
216 /* Make sure stdio descriptors (0-2) and profiler descriptor (3)
217 * always go somewhere harmless. Use -foreground for profiling
218 * or executing from gdb */
219 void
220 close_standard_fds(void)
221 {
222 int i;
223
224 for (i = 0; i < LOWEST_SAFE_FD; i++)
225 {
226 close(i);
227 if (open(PATH_DEVNULL, O_RDWR) < 0)
228 exit(-1); /* we're hosed if we can't even open /dev/null */
229 }
230 }
231
232 void
233 close_fds(fde_t *one)
234 {
235 int i;
236 fde_t *F;
237
238 for (i = 0; i < FD_HASH_SIZE; i++)
239 for (F = fd_hash[i]; F != NULL; F = F->hnext)
240 if (F != one)
241 close(F->fd);
242 }

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28