ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/fdlist.c
Revision: 2916
Committed: Sat Jan 25 21:09:18 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 4788 byte(s)
Log Message:
- Clean up all files in include/ (fixed indentation, removed whitespaces/tabs)
- Fixed copyright years

File Contents

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

Properties

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