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