ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/fdlist.c
Revision: 9857
Committed: Fri Jan 1 04:43:22 2021 UTC (5 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 4107 byte(s)
Log Message:
- Bump 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 9857 * Copyright (c) 1997-2021 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 adx 30
34 michael 8365
35 michael 8339 fde_t *fd_table;
36 adx 30 int number_fd = LEAKED_FDS;
37     int hard_fdlimit = 0;
38 michael 8520 int highest_fd = -1;
39 adx 30
40    
41 michael 4878 void
42     fdlist_init(void)
43 adx 30 {
44 michael 8522 /*
45     * Allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and
46     * some not really LEAKED_FDS
47     */
48 michael 8774 hard_fdlimit = IRCD_MAX(hard_fdlimit, LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN);
49     fd_table = xcalloc(sizeof(*fd_table) * hard_fdlimit);
50 adx 30 }
51    
52 michael 8339 static void
53 michael 8658 fdlist_update_highest_fd(int fd, bool opening)
54 adx 30 {
55 michael 8339 if (fd < highest_fd)
56     return;
57 adx 30
58 michael 8339 assert(fd < hard_fdlimit);
59 adx 30
60 michael 8339 if (fd > highest_fd)
61 adx 30 {
62 michael 8339 /*
63     * assert() that we are not closing a FD bigger than our known highest FD.
64     */
65 michael 8658 assert(opening == true);
66 michael 8339 highest_fd = fd;
67     return;
68 adx 30 }
69    
70 michael 8339 /* If we are here, then fd == highest_fd */
71     /*
72     * assert() that we are closing the highest FD; we can't be re-opening it.
73     */
74 michael 8658 assert(opening == false);
75 michael 8339
76 michael 8658 while (highest_fd >= 0 && fd_table[highest_fd].flags.open == false)
77 michael 8339 --highest_fd;
78 adx 30 }
79    
80     /* Called to open a given filedescriptor */
81 michael 8339 fde_t *
82 michael 8658 fd_open(int fd, bool is_socket, const char *desc)
83 adx 30 {
84 michael 8339 fde_t *F = &fd_table[fd];
85 michael 5590
86 adx 30 assert(fd >= 0);
87 michael 8339 assert(F->fd == 0);
88 michael 8658 assert(F->flags.open == false);
89 adx 30
90 michael 8339 /*
91     * Note: normally we'd have to clear the other flags, but currently F
92     * is always cleared before calling us.
93     */
94 adx 30 F->fd = fd;
95     F->comm_index = -1;
96 michael 8658 F->flags.open = true;
97 michael 8339 F->flags.is_socket = is_socket;
98 michael 3602
99 adx 30 if (desc)
100 michael 8770 F->desc = xstrndup(desc, FD_DESC_SIZE);
101 michael 3602
102 michael 8658 fdlist_update_highest_fd(F->fd, true);
103 michael 8339 ++number_fd;
104 adx 30
105 michael 8339 return F;
106 adx 30 }
107    
108     /* Called to close a given filedescriptor */
109 michael 8528 fde_t *
110 adx 30 fd_close(fde_t *F)
111     {
112 michael 8339 assert(F->fd >= 0);
113 michael 8658 assert(F->flags.open == true);
114 michael 5590
115 michael 8658 if (F->flags.is_socket == true)
116 adx 30 comm_setselect(F, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
117    
118 michael 9157 if (tls_isusing(&F->tls))
119     tls_free(&F->tls);
120 adx 30
121 michael 8770 xfree(F->desc);
122 adx 30 /* Unlike squid, we're actually closing the FD here! -- adrian */
123     close(F->fd);
124 michael 8660 F->flags.open = false; /* Must set F->flags.open == false before fdlist_update_highest_fd() */
125 michael 436
126 michael 8658 fdlist_update_highest_fd(F->fd, false);
127 michael 8339 --number_fd;
128 michael 8528
129     memset(F, 0, sizeof(*F));
130    
131     return F;
132 adx 30 }
133    
134     /*
135     * fd_note() - set the fd note
136     *
137     * Note: must be careful not to overflow fd_table[fd].desc when
138     * calling.
139     */
140     void
141     fd_note(fde_t *F, const char *format, ...)
142     {
143 michael 3600 if (format)
144 adx 30 {
145 michael 8770 char buf[FD_DESC_SIZE + 1];
146     va_list args;
147    
148 adx 30 va_start(args, format);
149 michael 8770 vsnprintf(buf, sizeof(buf), format, args);
150 adx 30 va_end(args);
151 michael 8770
152 michael 8772 xfree(F->desc);
153 michael 8770 F->desc = xstrdup(buf);
154 adx 30 }
155     else
156 michael 8770 {
157     xfree(F->desc);
158     F->desc = NULL;
159     }
160 adx 30 }
161    
162     /* Make sure stdio descriptors (0-2) and profiler descriptor (3)
163     * always go somewhere harmless. Use -foreground for profiling
164     * or executing from gdb */
165     void
166     close_standard_fds(void)
167     {
168 michael 8339 for (int i = 0; i < LOWEST_SAFE_FD; ++i)
169 adx 30 {
170     close(i);
171 michael 3600
172 michael 1329 if (open("/dev/null", O_RDWR) < 0)
173 michael 6553 exit(EXIT_FAILURE); /* we're hosed if we can't even open /dev/null */
174 adx 30 }
175     }
176    
177     void
178 michael 8339 close_fds(void)
179 adx 30 {
180 michael 8339 for (int fd = 0; fd <= highest_fd; ++fd)
181     close(fd);
182 adx 30 }

Properties

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