ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/fdlist.c
(Generate patch)

Comparing ircd-hybrid/trunk/src/fdlist.c (file contents):
Revision 1736 by michael, Sun Jan 13 09:31:46 2013 UTC vs.
Revision 5590 by michael, Tue Feb 17 17:55:01 2015 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  fdlist.c: Maintains a list of file descriptors.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
4 > *  Copyright (c) 1997-2015 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
# Line 16 | Line 15
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
18 > *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19   *  USA
20 < *
21 < *  $Id$
20 > */
21 >
22 > /*! \file fdlist.c
23 > * \brief Maintains a list of file descriptors.
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
# Line 29 | Line 31
31   #include "ircd.h"    /* GlobalSetOptions */
32   #include "irc_string.h"
33   #include "s_bsd.h"   /* comm_setselect */
34 < #include "conf.h"  /* ServerInfo */
34 > #include "conf.h"  /* ConfigServerInfo */
35   #include "send.h"
36   #include "memory.h"
37   #include "numeric.h"
38 < #include "s_misc.h"
39 < #include "irc_res.h"
38 > #include "misc.h"
39 > #include "res.h"
40  
41   fde_t *fd_hash[FD_HASH_SIZE];
42   fde_t *fd_next_in_loop = NULL;
# Line 42 | Line 44 | int number_fd = LEAKED_FDS;
44   int hard_fdlimit = 0;
45  
46  
47 < static int
48 < set_fdlimit(void)
47 > void
48 > fdlist_init(void)
49   {
50    int fdmax;
51    struct rlimit limit;
# Line 63 | Line 65 | set_fdlimit(void)
65    /* under no condition shall this raise over 65536
66     * for example user ip heap is sized 2*hard_fdlimit */
67    hard_fdlimit = IRCD_MIN(fdmax, 65536);
66
67  return -1;
68 }
69
70 void
71 fdlist_init(void)
72 {
73  set_fdlimit();
68   }
69  
70   static inline unsigned int
71   hash_fd(int fd)
72   {
73 <  return (((unsigned) fd) % FD_HASH_SIZE);
73 >  return ((unsigned int)fd) % FD_HASH_SIZE;
74   }
75  
76   fde_t *
# Line 87 | Line 81 | lookup_fd(int fd)
81    while (F)
82    {
83      if (F->fd == fd)
84 <      return (F);
84 >      return F;
85      F = F->hnext;
86    }
87  
88 <  return (NULL);
88 >  return NULL;
89   }
90  
91   /* Called to open a given filedescriptor */
# Line 99 | Line 93 | void
93   fd_open(fde_t *F, int fd, int is_socket, const char *desc)
94   {
95    unsigned int hashv = hash_fd(fd);
96 +
97    assert(fd >= 0);
98 +  assert(!F->flags.open);
99  
100    F->fd = fd;
101    F->comm_index = -1;
102 +
103    if (desc)
104      strlcpy(F->desc, desc, sizeof(F->desc));
105 +
106    /* Note: normally we'd have to clear the other flags,
107     * but currently F is always cleared before calling us.. */
108    F->flags.open = 1;
# Line 121 | Line 119 | fd_close(fde_t *F)
119   {
120    unsigned int hashv = hash_fd(F->fd);
121  
122 +  assert(F->flags.open);
123 +
124    if (F == fd_next_in_loop)
125      fd_next_in_loop = F->hnext;
126  
# Line 136 | Line 136 | fd_close(fde_t *F)
136  
137    if (fd_hash[hashv] == F)
138      fd_hash[hashv] = F->hnext;
139 <  else {
139 >  else
140 >  {
141      fde_t *prev;
142  
143      /* let it core if not found */
# Line 156 | Line 157 | fd_close(fde_t *F)
157   * fd_dump() - dump the list of active filedescriptors
158   */
159   void
160 < fd_dump(struct Client *source_p)
160 > fd_dump(struct Client *source_p, int parc, char *parv[])
161   {
162 <  int i;
163 <  fde_t *F;
164 <
165 <  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);
162 >  for (unsigned int i = 0; i < FD_HASH_SIZE; ++i)
163 >    for (fde_t *F = fd_hash[i]; F; F = F->hnext)
164 >      sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
165 >                         "F :fd %-5d desc '%s'", F->fd, F->desc);
166   }
167  
168   /*
# Line 179 | Line 176 | fd_note(fde_t *F, const char *format, ..
176   {
177    va_list args;
178  
179 <  if (format != NULL)
179 >  if (format)
180    {
181      va_start(args, format);
182      vsnprintf(F->desc, sizeof(F->desc), format, args);
# Line 195 | Line 192 | fd_note(fde_t *F, const char *format, ..
192   void
193   close_standard_fds(void)
194   {
195 <  int i;
199 <
200 <  for (i = 0; i < LOWEST_SAFE_FD; i++)
195 >  for (unsigned int i = 0; i < LOWEST_SAFE_FD; ++i)
196    {
197      close(i);
198 +
199      if (open("/dev/null", O_RDWR) < 0)
200        exit(-1); /* we're hosed if we can't even open /dev/null */
201    }
# Line 208 | Line 204 | close_standard_fds(void)
204   void
205   close_fds(fde_t *one)
206   {
207 <  int i;
208 <  fde_t *F;
213 <
214 <  for (i = 0; i < FD_HASH_SIZE; i++)
215 <    for (F = fd_hash[i]; F != NULL; F = F->hnext)
207 >  for (unsigned int i = 0; i < FD_HASH_SIZE; ++i)
208 >    for (fde_t *F = fd_hash[i]; F; F = F->hnext)
209        if (F != one)
210          close(F->fd);
211   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)