1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
< |
* fdlist.c: Maintains a list of file descriptors. |
2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
4 |
> |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
|
* |
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 |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
* USA |
20 |
< |
* |
21 |
< |
* $Id$ |
20 |
> |
*/ |
21 |
> |
|
22 |
> |
/*! \file fdlist.c |
23 |
> |
* \brief Maintains a list of file descriptors. |
24 |
> |
* \version $Id$ |
25 |
|
*/ |
26 |
|
|
27 |
|
#include "stdinc.h" |
35 |
|
#include "send.h" |
36 |
|
#include "memory.h" |
37 |
|
#include "numeric.h" |
38 |
< |
#include "s_misc.h" |
39 |
< |
#include "irc_res.h" |
38 |
> |
#include "misc.h" |
39 |
> |
#include "res.h" |
40 |
|
|
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; |
43 |
– |
struct Callback *fdlimit_cb = NULL; |
44 |
– |
|
45 |
– |
static void * |
46 |
– |
changing_fdlimit(va_list args) |
47 |
– |
{ |
48 |
– |
int old_fdlimit = hard_fdlimit; |
45 |
|
|
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 |
– |
} |
46 |
|
|
47 |
< |
return NULL; |
48 |
< |
} |
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) |
47 |
> |
static int |
48 |
> |
set_fdlimit(void) |
49 |
|
{ |
50 |
|
int fdmax; |
51 |
|
struct rlimit limit; |
64 |
|
|
65 |
|
/* under no condition shall this raise over 65536 |
66 |
|
* for example user ip heap is sized 2*hard_fdlimit */ |
67 |
< |
fdmax = IRCD_MIN(fdmax, 65536); |
67 |
> |
hard_fdlimit = IRCD_MIN(fdmax, 65536); |
68 |
> |
|
69 |
> |
return -1; |
70 |
> |
} |
71 |
|
|
72 |
< |
if (fdmax != hard_fdlimit) |
73 |
< |
execute_callback(fdlimit_cb, fdmax); |
72 |
> |
void |
73 |
> |
fdlist_init(void) |
74 |
> |
{ |
75 |
> |
set_fdlimit(); |
76 |
|
} |
77 |
|
|
78 |
|
static inline unsigned int |
79 |
|
hash_fd(int fd) |
80 |
|
{ |
81 |
< |
return (((unsigned) fd) % FD_HASH_SIZE); |
81 |
> |
return ((unsigned int)fd) % FD_HASH_SIZE; |
82 |
|
} |
83 |
|
|
84 |
|
fde_t * |
89 |
|
while (F) |
90 |
|
{ |
91 |
|
if (F->fd == fd) |
92 |
< |
return (F); |
92 |
> |
return F; |
93 |
|
F = F->hnext; |
94 |
|
} |
95 |
|
|
96 |
< |
return (NULL); |
96 |
> |
return NULL; |
97 |
|
} |
98 |
|
|
99 |
|
/* Called to open a given filedescriptor */ |
105 |
|
|
106 |
|
F->fd = fd; |
107 |
|
F->comm_index = -1; |
108 |
+ |
|
109 |
|
if (desc) |
110 |
|
strlcpy(F->desc, desc, sizeof(F->desc)); |
111 |
+ |
|
112 |
|
/* Note: normally we'd have to clear the other flags, |
113 |
|
* but currently F is always cleared before calling us.. */ |
114 |
|
F->flags.open = 1; |
140 |
|
|
141 |
|
if (fd_hash[hashv] == F) |
142 |
|
fd_hash[hashv] = F->hnext; |
143 |
< |
else { |
143 |
> |
else |
144 |
> |
{ |
145 |
|
fde_t *prev; |
146 |
|
|
147 |
|
/* let it core if not found */ |
163 |
|
void |
164 |
|
fd_dump(struct Client *source_p) |
165 |
|
{ |
166 |
< |
int i; |
167 |
< |
fde_t *F; |
168 |
< |
|
169 |
< |
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); |
166 |
> |
for (unsigned int i = 0; i < FD_HASH_SIZE; ++i) |
167 |
> |
for (fde_t *F = fd_hash[i]; F; F = F->hnext) |
168 |
> |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
169 |
> |
"F :fd %-5d desc '%s'", F->fd, F->desc); |
170 |
|
} |
171 |
|
|
172 |
|
/* |
180 |
|
{ |
181 |
|
va_list args; |
182 |
|
|
183 |
< |
if (format != NULL) |
183 |
> |
if (format) |
184 |
|
{ |
185 |
|
va_start(args, format); |
186 |
|
vsnprintf(F->desc, sizeof(F->desc), format, args); |
196 |
|
void |
197 |
|
close_standard_fds(void) |
198 |
|
{ |
199 |
< |
int i; |
224 |
< |
|
225 |
< |
for (i = 0; i < LOWEST_SAFE_FD; i++) |
199 |
> |
for (unsigned int i = 0; i < LOWEST_SAFE_FD; ++i) |
200 |
|
{ |
201 |
|
close(i); |
202 |
+ |
|
203 |
|
if (open("/dev/null", O_RDWR) < 0) |
204 |
|
exit(-1); /* we're hosed if we can't even open /dev/null */ |
205 |
|
} |
208 |
|
void |
209 |
|
close_fds(fde_t *one) |
210 |
|
{ |
211 |
< |
int i; |
212 |
< |
fde_t *F; |
238 |
< |
|
239 |
< |
for (i = 0; i < FD_HASH_SIZE; i++) |
240 |
< |
for (F = fd_hash[i]; F != NULL; F = F->hnext) |
211 |
> |
for (unsigned int i = 0; i < FD_HASH_SIZE; ++i) |
212 |
> |
for (fde_t *F = fd_hash[i]; F; F = F->hnext) |
213 |
|
if (F != one) |
214 |
|
close(F->fd); |
215 |
|
} |