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