| 1 |
adx |
30 |
/* |
| 2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
adx |
30 |
* |
| 4 |
michael |
5347 |
* Copyright (c) 1997-2015 ircd-hybrid development team |
| 5 |
adx |
30 |
* |
| 6 |
|
|
* This program is free software; you can redistribute it and/or modify |
| 7 |
|
|
* it under the terms of the GNU General Public License as published by |
| 8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
|
|
* (at your option) any later version. |
| 10 |
|
|
* |
| 11 |
|
|
* This program is distributed in the hope that it will be useful, |
| 12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
|
|
* GNU General Public License for more details. |
| 15 |
|
|
* |
| 16 |
|
|
* You should have received a copy of the GNU General Public License |
| 17 |
|
|
* along with this program; if not, write to the Free Software |
| 18 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
adx |
30 |
* USA |
| 20 |
|
|
*/ |
| 21 |
michael |
1243 |
|
| 22 |
michael |
2916 |
/*! \file fdlist.c |
| 23 |
|
|
* \brief Maintains a list of file descriptors. |
| 24 |
|
|
* \version $Id$ |
| 25 |
|
|
*/ |
| 26 |
|
|
|
| 27 |
adx |
30 |
#include "stdinc.h" |
| 28 |
|
|
#include "fdlist.h" |
| 29 |
|
|
#include "client.h" /* struct Client */ |
| 30 |
|
|
#include "event.h" |
| 31 |
|
|
#include "ircd.h" /* GlobalSetOptions */ |
| 32 |
|
|
#include "irc_string.h" |
| 33 |
|
|
#include "s_bsd.h" /* comm_setselect */ |
| 34 |
michael |
4340 |
#include "conf.h" /* ConfigServerInfo */ |
| 35 |
adx |
30 |
#include "send.h" |
| 36 |
|
|
#include "memory.h" |
| 37 |
|
|
#include "numeric.h" |
| 38 |
michael |
3347 |
#include "misc.h" |
| 39 |
michael |
3322 |
#include "res.h" |
| 40 |
adx |
30 |
|
| 41 |
|
|
fde_t *fd_hash[FD_HASH_SIZE]; |
| 42 |
|
|
fde_t *fd_next_in_loop = NULL; |
| 43 |
|
|
int number_fd = LEAKED_FDS; |
| 44 |
|
|
int hard_fdlimit = 0; |
| 45 |
|
|
|
| 46 |
|
|
|
| 47 |
michael |
4878 |
void |
| 48 |
|
|
fdlist_init(void) |
| 49 |
adx |
30 |
{ |
| 50 |
|
|
int fdmax; |
| 51 |
|
|
struct rlimit limit; |
| 52 |
|
|
|
| 53 |
michael |
1236 |
if (!getrlimit(RLIMIT_NOFILE, &limit)) |
| 54 |
adx |
30 |
{ |
| 55 |
|
|
limit.rlim_cur = limit.rlim_max; |
| 56 |
michael |
1236 |
setrlimit(RLIMIT_NOFILE, &limit); |
| 57 |
adx |
30 |
} |
| 58 |
|
|
|
| 59 |
|
|
fdmax = getdtablesize(); |
| 60 |
|
|
|
| 61 |
|
|
/* allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and |
| 62 |
|
|
* some not really LEAKED_FDS */ |
| 63 |
|
|
fdmax = IRCD_MAX(fdmax, LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN); |
| 64 |
|
|
|
| 65 |
|
|
/* under no condition shall this raise over 65536 |
| 66 |
|
|
* for example user ip heap is sized 2*hard_fdlimit */ |
| 67 |
michael |
1736 |
hard_fdlimit = IRCD_MIN(fdmax, 65536); |
| 68 |
adx |
30 |
} |
| 69 |
|
|
|
| 70 |
|
|
static inline unsigned int |
| 71 |
|
|
hash_fd(int fd) |
| 72 |
|
|
{ |
| 73 |
michael |
2916 |
return ((unsigned int)fd) % FD_HASH_SIZE; |
| 74 |
adx |
30 |
} |
| 75 |
|
|
|
| 76 |
|
|
fde_t * |
| 77 |
|
|
lookup_fd(int fd) |
| 78 |
|
|
{ |
| 79 |
|
|
fde_t *F = fd_hash[hash_fd(fd)]; |
| 80 |
|
|
|
| 81 |
|
|
while (F) |
| 82 |
|
|
{ |
| 83 |
|
|
if (F->fd == fd) |
| 84 |
michael |
2916 |
return F; |
| 85 |
adx |
30 |
F = F->hnext; |
| 86 |
|
|
} |
| 87 |
|
|
|
| 88 |
michael |
2916 |
return NULL; |
| 89 |
adx |
30 |
} |
| 90 |
|
|
|
| 91 |
|
|
/* Called to open a given filedescriptor */ |
| 92 |
|
|
void |
| 93 |
|
|
fd_open(fde_t *F, int fd, int is_socket, const char *desc) |
| 94 |
|
|
{ |
| 95 |
|
|
unsigned int hashv = hash_fd(fd); |
| 96 |
|
|
assert(fd >= 0); |
| 97 |
|
|
|
| 98 |
|
|
F->fd = fd; |
| 99 |
|
|
F->comm_index = -1; |
| 100 |
michael |
3602 |
|
| 101 |
adx |
30 |
if (desc) |
| 102 |
|
|
strlcpy(F->desc, desc, sizeof(F->desc)); |
| 103 |
michael |
3602 |
|
| 104 |
adx |
30 |
/* Note: normally we'd have to clear the other flags, |
| 105 |
|
|
* but currently F is always cleared before calling us.. */ |
| 106 |
|
|
F->flags.open = 1; |
| 107 |
|
|
F->flags.is_socket = is_socket; |
| 108 |
|
|
F->hnext = fd_hash[hashv]; |
| 109 |
|
|
fd_hash[hashv] = F; |
| 110 |
|
|
|
| 111 |
|
|
number_fd++; |
| 112 |
|
|
} |
| 113 |
|
|
|
| 114 |
|
|
/* Called to close a given filedescriptor */ |
| 115 |
|
|
void |
| 116 |
|
|
fd_close(fde_t *F) |
| 117 |
|
|
{ |
| 118 |
|
|
unsigned int hashv = hash_fd(F->fd); |
| 119 |
|
|
|
| 120 |
|
|
if (F == fd_next_in_loop) |
| 121 |
|
|
fd_next_in_loop = F->hnext; |
| 122 |
|
|
|
| 123 |
|
|
if (F->flags.is_socket) |
| 124 |
|
|
comm_setselect(F, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); |
| 125 |
|
|
|
| 126 |
michael |
992 |
delete_resolver_queries(F); |
| 127 |
adx |
30 |
|
| 128 |
|
|
#ifdef HAVE_LIBCRYPTO |
| 129 |
|
|
if (F->ssl) |
| 130 |
|
|
SSL_free(F->ssl); |
| 131 |
|
|
#endif |
| 132 |
|
|
|
| 133 |
|
|
if (fd_hash[hashv] == F) |
| 134 |
|
|
fd_hash[hashv] = F->hnext; |
| 135 |
michael |
3602 |
else |
| 136 |
|
|
{ |
| 137 |
adx |
30 |
fde_t *prev; |
| 138 |
|
|
|
| 139 |
|
|
/* let it core if not found */ |
| 140 |
|
|
for (prev = fd_hash[hashv]; prev->hnext != F; prev = prev->hnext) |
| 141 |
|
|
; |
| 142 |
|
|
prev->hnext = F->hnext; |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
|
/* Unlike squid, we're actually closing the FD here! -- adrian */ |
| 146 |
|
|
close(F->fd); |
| 147 |
|
|
number_fd--; |
| 148 |
michael |
436 |
|
| 149 |
|
|
memset(F, 0, sizeof(fde_t)); |
| 150 |
adx |
30 |
} |
| 151 |
|
|
|
| 152 |
|
|
/* |
| 153 |
|
|
* fd_dump() - dump the list of active filedescriptors |
| 154 |
|
|
*/ |
| 155 |
|
|
void |
| 156 |
michael |
4479 |
fd_dump(struct Client *source_p, int parc, char *parv[]) |
| 157 |
adx |
30 |
{ |
| 158 |
michael |
3589 |
for (unsigned int i = 0; i < FD_HASH_SIZE; ++i) |
| 159 |
|
|
for (fde_t *F = fd_hash[i]; F; F = F->hnext) |
| 160 |
|
|
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
| 161 |
|
|
"F :fd %-5d desc '%s'", F->fd, F->desc); |
| 162 |
adx |
30 |
} |
| 163 |
|
|
|
| 164 |
|
|
/* |
| 165 |
|
|
* fd_note() - set the fd note |
| 166 |
|
|
* |
| 167 |
|
|
* Note: must be careful not to overflow fd_table[fd].desc when |
| 168 |
|
|
* calling. |
| 169 |
|
|
*/ |
| 170 |
|
|
void |
| 171 |
|
|
fd_note(fde_t *F, const char *format, ...) |
| 172 |
|
|
{ |
| 173 |
|
|
va_list args; |
| 174 |
|
|
|
| 175 |
michael |
3600 |
if (format) |
| 176 |
adx |
30 |
{ |
| 177 |
|
|
va_start(args, format); |
| 178 |
|
|
vsnprintf(F->desc, sizeof(F->desc), format, args); |
| 179 |
|
|
va_end(args); |
| 180 |
|
|
} |
| 181 |
|
|
else |
| 182 |
|
|
F->desc[0] = '\0'; |
| 183 |
|
|
} |
| 184 |
|
|
|
| 185 |
|
|
/* Make sure stdio descriptors (0-2) and profiler descriptor (3) |
| 186 |
|
|
* always go somewhere harmless. Use -foreground for profiling |
| 187 |
|
|
* or executing from gdb */ |
| 188 |
|
|
void |
| 189 |
|
|
close_standard_fds(void) |
| 190 |
|
|
{ |
| 191 |
michael |
3600 |
for (unsigned int i = 0; i < LOWEST_SAFE_FD; ++i) |
| 192 |
adx |
30 |
{ |
| 193 |
|
|
close(i); |
| 194 |
michael |
3600 |
|
| 195 |
michael |
1329 |
if (open("/dev/null", O_RDWR) < 0) |
| 196 |
adx |
30 |
exit(-1); /* we're hosed if we can't even open /dev/null */ |
| 197 |
|
|
} |
| 198 |
|
|
} |
| 199 |
|
|
|
| 200 |
|
|
void |
| 201 |
|
|
close_fds(fde_t *one) |
| 202 |
|
|
{ |
| 203 |
michael |
3600 |
for (unsigned int i = 0; i < FD_HASH_SIZE; ++i) |
| 204 |
michael |
3602 |
for (fde_t *F = fd_hash[i]; F; F = F->hnext) |
| 205 |
adx |
30 |
if (F != one) |
| 206 |
|
|
close(F->fd); |
| 207 |
|
|
} |