ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/fdlist.c
Revision: 1736
Committed: Sun Jan 13 09:31:46 2013 UTC (11 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 4768 byte(s)
Log Message:
- Forward-port -r1732 [Dropped support for linux rt signals]

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision