ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/fdlist.c
Revision: 1236
Committed: Thu Sep 29 11:21:27 2011 UTC (12 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 5433 byte(s)
Log Message:
- Getting rid of rlimits.h. Relying on RLIMIT_NOFILE is
  more than enough nowadays

File Contents

# User Rev Content
1 adx 30 /*
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 knight 31 * $Id$
23 adx 30 */
24     #include "stdinc.h"
25     #include "fdlist.h"
26     #include "client.h" /* struct Client */
27     #include "common.h"
28     #include "event.h"
29     #include "ircd.h" /* GlobalSetOptions */
30     #include "irc_string.h"
31     #include "s_bsd.h" /* comm_setselect */
32     #include "s_conf.h" /* ServerInfo */
33     #include "send.h"
34     #include "memory.h"
35     #include "numeric.h"
36    
37     fde_t *fd_hash[FD_HASH_SIZE];
38     fde_t *fd_next_in_loop = NULL;
39     int number_fd = LEAKED_FDS;
40     int hard_fdlimit = 0;
41     struct Callback *fdlimit_cb = NULL;
42    
43     static void *
44     changing_fdlimit(va_list args)
45     {
46     int old_fdlimit = hard_fdlimit;
47    
48     hard_fdlimit = va_arg(args, int);
49    
50     if (ServerInfo.max_clients > MAXCLIENTS_MAX)
51     {
52     if (old_fdlimit != 0)
53     sendto_realops_flags(UMODE_ALL, L_ALL,
54     "HARD_FDLIMIT changed to %d, adjusting MAXCLIENTS to %d",
55     hard_fdlimit, MAXCLIENTS_MAX);
56    
57     ServerInfo.max_clients = MAXCLIENTS_MAX;
58     }
59    
60     return NULL;
61     }
62    
63     void
64     fdlist_init(void)
65     {
66     memset(&fd_hash, 0, sizeof(fd_hash));
67    
68     fdlimit_cb = register_callback("changing_fdlimit", changing_fdlimit);
69     eventAddIsh("recalc_fdlimit", recalc_fdlimit, NULL, 58);
70     recalc_fdlimit(NULL);
71     }
72    
73     void
74     recalc_fdlimit(void *unused)
75     {
76     int fdmax;
77     struct rlimit limit;
78    
79 michael 1236 if (!getrlimit(RLIMIT_NOFILE, &limit))
80 adx 30 {
81     limit.rlim_cur = limit.rlim_max;
82 michael 1236 setrlimit(RLIMIT_NOFILE, &limit);
83 adx 30 }
84    
85     fdmax = getdtablesize();
86    
87     /* allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and
88     * some not really LEAKED_FDS */
89     fdmax = IRCD_MAX(fdmax, LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN);
90    
91     /* under no condition shall this raise over 65536
92     * for example user ip heap is sized 2*hard_fdlimit */
93     fdmax = IRCD_MIN(fdmax, 65536);
94    
95     if (fdmax != hard_fdlimit)
96     execute_callback(fdlimit_cb, fdmax);
97     }
98    
99     static inline unsigned int
100     hash_fd(int fd)
101     {
102     return (((unsigned) fd) % FD_HASH_SIZE);
103     }
104    
105     fde_t *
106     lookup_fd(int fd)
107     {
108     fde_t *F = fd_hash[hash_fd(fd)];
109    
110     while (F)
111     {
112     if (F->fd == fd)
113     return (F);
114     F = F->hnext;
115     }
116    
117     return (NULL);
118     }
119    
120     /* Called to open a given filedescriptor */
121     void
122     fd_open(fde_t *F, int fd, int is_socket, const char *desc)
123     {
124     unsigned int hashv = hash_fd(fd);
125     assert(fd >= 0);
126    
127     F->fd = fd;
128     F->comm_index = -1;
129     if (desc)
130     strlcpy(F->desc, desc, sizeof(F->desc));
131     /* Note: normally we'd have to clear the other flags,
132     * but currently F is always cleared before calling us.. */
133     F->flags.open = 1;
134     F->flags.is_socket = is_socket;
135     F->hnext = fd_hash[hashv];
136     fd_hash[hashv] = F;
137    
138     number_fd++;
139     }
140    
141     /* Called to close a given filedescriptor */
142     void
143     fd_close(fde_t *F)
144     {
145     unsigned int hashv = hash_fd(F->fd);
146    
147     if (F == fd_next_in_loop)
148     fd_next_in_loop = F->hnext;
149    
150     if (F->flags.is_socket)
151     comm_setselect(F, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
152    
153 michael 992 delete_resolver_queries(F);
154 adx 30
155     #ifdef HAVE_LIBCRYPTO
156     if (F->ssl)
157     SSL_free(F->ssl);
158     #endif
159    
160     if (fd_hash[hashv] == F)
161     fd_hash[hashv] = F->hnext;
162     else {
163     fde_t *prev;
164    
165     /* let it core if not found */
166     for (prev = fd_hash[hashv]; prev->hnext != F; prev = prev->hnext)
167     ;
168     prev->hnext = F->hnext;
169     }
170    
171     /* Unlike squid, we're actually closing the FD here! -- adrian */
172     close(F->fd);
173     number_fd--;
174 michael 436
175     memset(F, 0, sizeof(fde_t));
176 adx 30 }
177    
178     /*
179     * fd_dump() - dump the list of active filedescriptors
180     */
181     void
182     fd_dump(struct Client *source_p)
183     {
184     int i;
185     fde_t *F;
186    
187     for (i = 0; i < FD_HASH_SIZE; i++)
188     for (F = fd_hash[i]; F != NULL; F = F->hnext)
189     sendto_one(source_p, ":%s %d %s :fd %-5d desc '%s'",
190     me.name, RPL_STATSDEBUG, source_p->name,
191     F->fd, F->desc);
192     }
193    
194     /*
195     * fd_note() - set the fd note
196     *
197     * Note: must be careful not to overflow fd_table[fd].desc when
198     * calling.
199     */
200     void
201     fd_note(fde_t *F, const char *format, ...)
202     {
203     va_list args;
204    
205     if (format != NULL)
206     {
207     va_start(args, format);
208     vsnprintf(F->desc, sizeof(F->desc), format, args);
209     va_end(args);
210     }
211     else
212     F->desc[0] = '\0';
213     }
214    
215     /* Make sure stdio descriptors (0-2) and profiler descriptor (3)
216     * always go somewhere harmless. Use -foreground for profiling
217     * or executing from gdb */
218     void
219     close_standard_fds(void)
220     {
221     int i;
222    
223     for (i = 0; i < LOWEST_SAFE_FD; i++)
224     {
225     close(i);
226     if (open(PATH_DEVNULL, O_RDWR) < 0)
227     exit(-1); /* we're hosed if we can't even open /dev/null */
228     }
229     }
230    
231     void
232     close_fds(fde_t *one)
233     {
234     int i;
235     fde_t *F;
236    
237     for (i = 0; i < FD_HASH_SIZE; i++)
238     for (F = fd_hash[i]; F != NULL; F = F->hnext)
239     if (F != one)
240     close(F->fd);
241     }

Properties

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