| 1 |
adx |
30 |
/* |
| 2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
adx |
30 |
* |
| 4 |
michael |
8279 |
* Copyright (c) 1997-2018 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 "irc_string.h" |
| 31 |
|
|
#include "s_bsd.h" /* comm_setselect */ |
| 32 |
|
|
#include "send.h" |
| 33 |
|
|
#include "memory.h" |
| 34 |
|
|
#include "numeric.h" |
| 35 |
michael |
3347 |
#include "misc.h" |
| 36 |
michael |
3322 |
#include "res.h" |
| 37 |
adx |
30 |
|
| 38 |
michael |
8365 |
|
| 39 |
michael |
8339 |
fde_t *fd_table; |
| 40 |
adx |
30 |
int number_fd = LEAKED_FDS; |
| 41 |
|
|
int hard_fdlimit = 0; |
| 42 |
michael |
8339 |
int highest_fd; |
| 43 |
adx |
30 |
|
| 44 |
|
|
|
| 45 |
michael |
4878 |
void |
| 46 |
|
|
fdlist_init(void) |
| 47 |
adx |
30 |
{ |
| 48 |
|
|
struct rlimit limit; |
| 49 |
|
|
|
| 50 |
michael |
1236 |
if (!getrlimit(RLIMIT_NOFILE, &limit)) |
| 51 |
adx |
30 |
{ |
| 52 |
|
|
limit.rlim_cur = limit.rlim_max; |
| 53 |
michael |
1236 |
setrlimit(RLIMIT_NOFILE, &limit); |
| 54 |
adx |
30 |
} |
| 55 |
|
|
|
| 56 |
|
|
/* allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and |
| 57 |
|
|
* some not really LEAKED_FDS */ |
| 58 |
michael |
8339 |
hard_fdlimit = IRCD_MAX(getdtablesize(), LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN); |
| 59 |
|
|
fd_table = xcalloc(sizeof(fde_t) * hard_fdlimit); |
| 60 |
adx |
30 |
} |
| 61 |
|
|
|
| 62 |
michael |
8339 |
static void |
| 63 |
|
|
fdlist_update_highest_fd(int fd, int opening) |
| 64 |
adx |
30 |
{ |
| 65 |
michael |
8339 |
if (fd < highest_fd) |
| 66 |
|
|
return; |
| 67 |
adx |
30 |
|
| 68 |
michael |
8339 |
assert(fd < hard_fdlimit); |
| 69 |
adx |
30 |
|
| 70 |
michael |
8339 |
if (fd > highest_fd) |
| 71 |
adx |
30 |
{ |
| 72 |
michael |
8339 |
/* |
| 73 |
|
|
* assert() that we are not closing a FD bigger than our known highest FD. |
| 74 |
|
|
*/ |
| 75 |
|
|
assert(opening); |
| 76 |
|
|
highest_fd = fd; |
| 77 |
|
|
return; |
| 78 |
adx |
30 |
} |
| 79 |
|
|
|
| 80 |
michael |
8339 |
/* If we are here, then fd == highest_fd */ |
| 81 |
|
|
/* |
| 82 |
|
|
* assert() that we are closing the highest FD; we can't be re-opening it. |
| 83 |
|
|
*/ |
| 84 |
|
|
assert(!opening); |
| 85 |
|
|
|
| 86 |
|
|
while (highest_fd >= 0 && fd_table[highest_fd].flags.open == 0) |
| 87 |
|
|
--highest_fd; |
| 88 |
adx |
30 |
} |
| 89 |
|
|
|
| 90 |
|
|
/* Called to open a given filedescriptor */ |
| 91 |
michael |
8339 |
fde_t * |
| 92 |
|
|
fd_open(int fd, int is_socket, const char *desc) |
| 93 |
adx |
30 |
{ |
| 94 |
michael |
8339 |
fde_t *F = &fd_table[fd]; |
| 95 |
michael |
5590 |
|
| 96 |
adx |
30 |
assert(fd >= 0); |
| 97 |
michael |
8339 |
assert(F->fd == 0); |
| 98 |
|
|
assert(F->flags.open == 0); |
| 99 |
adx |
30 |
|
| 100 |
michael |
8339 |
/* |
| 101 |
|
|
* Note: normally we'd have to clear the other flags, but currently F |
| 102 |
|
|
* is always cleared before calling us. |
| 103 |
|
|
*/ |
| 104 |
adx |
30 |
F->fd = fd; |
| 105 |
|
|
F->comm_index = -1; |
| 106 |
michael |
8339 |
F->flags.open = 1; |
| 107 |
|
|
F->flags.is_socket = is_socket; |
| 108 |
michael |
3602 |
|
| 109 |
adx |
30 |
if (desc) |
| 110 |
|
|
strlcpy(F->desc, desc, sizeof(F->desc)); |
| 111 |
michael |
3602 |
|
| 112 |
michael |
8339 |
++number_fd; |
| 113 |
|
|
fdlist_update_highest_fd(fd, 1); |
| 114 |
adx |
30 |
|
| 115 |
michael |
8339 |
return F; |
| 116 |
adx |
30 |
} |
| 117 |
|
|
|
| 118 |
|
|
/* Called to close a given filedescriptor */ |
| 119 |
|
|
void |
| 120 |
|
|
fd_close(fde_t *F) |
| 121 |
|
|
{ |
| 122 |
michael |
8339 |
const int fd = F->fd; |
| 123 |
adx |
30 |
|
| 124 |
michael |
8339 |
assert(F->fd >= 0); |
| 125 |
michael |
5590 |
assert(F->flags.open); |
| 126 |
|
|
|
| 127 |
adx |
30 |
if (F->flags.is_socket) |
| 128 |
|
|
comm_setselect(F, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); |
| 129 |
|
|
|
| 130 |
michael |
992 |
delete_resolver_queries(F); |
| 131 |
adx |
30 |
|
| 132 |
michael |
7105 |
if (tls_isusing(&F->ssl)) |
| 133 |
|
|
tls_free(&F->ssl); |
| 134 |
adx |
30 |
|
| 135 |
|
|
/* Unlike squid, we're actually closing the FD here! -- adrian */ |
| 136 |
|
|
close(F->fd); |
| 137 |
michael |
8339 |
memset(F, 0, sizeof(*F)); /* Must set F->flags.open == 0 before fdlist_update_highest_fd() */ |
| 138 |
michael |
436 |
|
| 139 |
michael |
8339 |
--number_fd; |
| 140 |
|
|
fdlist_update_highest_fd(fd, 0); |
| 141 |
adx |
30 |
} |
| 142 |
|
|
|
| 143 |
|
|
/* |
| 144 |
|
|
* fd_dump() - dump the list of active filedescriptors |
| 145 |
|
|
*/ |
| 146 |
|
|
void |
| 147 |
michael |
4479 |
fd_dump(struct Client *source_p, int parc, char *parv[]) |
| 148 |
adx |
30 |
{ |
| 149 |
michael |
8339 |
for (int fd = 0; fd <= highest_fd; ++fd) |
| 150 |
|
|
{ |
| 151 |
|
|
const fde_t *F = &fd_table[fd]; |
| 152 |
|
|
|
| 153 |
|
|
if (F->flags.open) |
| 154 |
michael |
5583 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
| 155 |
michael |
3589 |
"F :fd %-5d desc '%s'", F->fd, F->desc); |
| 156 |
michael |
8339 |
} |
| 157 |
adx |
30 |
} |
| 158 |
|
|
|
| 159 |
|
|
/* |
| 160 |
|
|
* fd_note() - set the fd note |
| 161 |
|
|
* |
| 162 |
|
|
* Note: must be careful not to overflow fd_table[fd].desc when |
| 163 |
|
|
* calling. |
| 164 |
|
|
*/ |
| 165 |
|
|
void |
| 166 |
|
|
fd_note(fde_t *F, const char *format, ...) |
| 167 |
|
|
{ |
| 168 |
|
|
va_list args; |
| 169 |
|
|
|
| 170 |
michael |
3600 |
if (format) |
| 171 |
adx |
30 |
{ |
| 172 |
|
|
va_start(args, format); |
| 173 |
|
|
vsnprintf(F->desc, sizeof(F->desc), format, args); |
| 174 |
|
|
va_end(args); |
| 175 |
|
|
} |
| 176 |
|
|
else |
| 177 |
|
|
F->desc[0] = '\0'; |
| 178 |
|
|
} |
| 179 |
|
|
|
| 180 |
|
|
/* Make sure stdio descriptors (0-2) and profiler descriptor (3) |
| 181 |
|
|
* always go somewhere harmless. Use -foreground for profiling |
| 182 |
|
|
* or executing from gdb */ |
| 183 |
|
|
void |
| 184 |
|
|
close_standard_fds(void) |
| 185 |
|
|
{ |
| 186 |
michael |
8339 |
for (int i = 0; i < LOWEST_SAFE_FD; ++i) |
| 187 |
adx |
30 |
{ |
| 188 |
|
|
close(i); |
| 189 |
michael |
3600 |
|
| 190 |
michael |
1329 |
if (open("/dev/null", O_RDWR) < 0) |
| 191 |
michael |
6553 |
exit(EXIT_FAILURE); /* we're hosed if we can't even open /dev/null */ |
| 192 |
adx |
30 |
} |
| 193 |
|
|
} |
| 194 |
|
|
|
| 195 |
|
|
void |
| 196 |
michael |
8339 |
close_fds(void) |
| 197 |
adx |
30 |
{ |
| 198 |
michael |
8339 |
for (int fd = 0; fd <= highest_fd; ++fd) |
| 199 |
|
|
close(fd); |
| 200 |
adx |
30 |
} |