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