blob: 9dd365a50b9d87123da3a3679fffd4f77c115244 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * File descriptors management functions.
3 *
Willy Tarreauec6c5df2008-07-15 00:22:45 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau2ff76222007-04-09 19:29:56 +020013#include <stdio.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020014#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <sys/types.h>
17
Willy Tarreau2dd0d472006-06-29 17:53:05 +020018#include <common/compat.h>
19#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreau2a429502006-10-15 14:52:29 +020021#include <proto/fd.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020022#include <proto/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24struct fdtab *fdtab = NULL; /* array of all the file descriptors */
25int maxfd; /* # of the highest fd + 1 */
26int totalconn; /* total # of terminated sessions */
27int actconn; /* # of active sessions */
28
Willy Tarreau4f60f162007-04-08 16:39:58 +020029struct poller pollers[MAX_POLLERS];
30struct poller cur_poller;
31int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
33
Willy Tarreau4f60f162007-04-08 16:39:58 +020034/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
35 * The file descriptor is also closed.
36 */
37void fd_delete(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +020038{
Willy Tarreau4f60f162007-04-08 16:39:58 +020039 EV_FD_CLO(fd);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020040 port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port);
41 fdtab[fd].port_range = NULL;
Willy Tarreau4f60f162007-04-08 16:39:58 +020042 close(fd);
43 fdtab[fd].state = FD_STCLOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau4f60f162007-04-08 16:39:58 +020045 while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE))
46 maxfd--;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047}
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
Willy Tarreau4f60f162007-04-08 16:39:58 +020050/* disable the specified poller */
51void disable_poller(const char *poller_name)
52{
53 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau4f60f162007-04-08 16:39:58 +020055 for (p = 0; p < nbpollers; p++)
56 if (strcmp(pollers[p].name, poller_name) == 0)
57 pollers[p].pref = 0;
58}
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
60/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020061 * Initialize the pollers till the best one is found.
62 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020064int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +020065{
Willy Tarreau4f60f162007-04-08 16:39:58 +020066 int p;
67 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau4f60f162007-04-08 16:39:58 +020070 do {
71 bp = NULL;
72 for (p = 0; p < nbpollers; p++)
73 if (!bp || (pollers[p].pref > bp->pref))
74 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreau4f60f162007-04-08 16:39:58 +020076 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +020077 break;
78
Willy Tarreau4f60f162007-04-08 16:39:58 +020079 if (bp->init(bp)) {
80 memcpy(&cur_poller, bp, sizeof(*bp));
81 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 }
Willy Tarreau4f60f162007-04-08 16:39:58 +020083 } while (!bp || bp->pref == 0);
84 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +020085}
86
Willy Tarreaubaaee002006-06-26 02:48:02 +020087/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020088 * Deinitialize the pollers.
89 */
90void deinit_pollers() {
91
92 struct poller *bp;
93 int p;
94
95 for (p = 0; p < nbpollers; p++) {
96 bp = &pollers[p];
97
98 if (bp && bp->pref)
99 bp->term(bp);
100 }
101}
102
103/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200104 * Lists the known pollers on <out>.
105 * Should be performed only before initialization.
106 */
107int list_pollers(FILE *out)
108{
109 int p;
110 int last, next;
111 int usable;
112 struct poller *bp;
113
114 fprintf(out, "Available polling systems :\n");
115
116 usable = 0;
117 bp = NULL;
118 last = next = -1;
119 while (1) {
120 for (p = 0; p < nbpollers; p++) {
121 if (!bp || (pollers[p].pref > bp->pref))
122 bp = &pollers[p];
123 if ((next < 0 || pollers[p].pref > next)
124 && (last < 0 || pollers[p].pref < last))
125 next = pollers[p].pref;
126 }
127
128 if (next == -1)
129 break;
130
131 for (p = 0; p < nbpollers; p++) {
132 if (pollers[p].pref == next) {
133 fprintf(out, " %10s : ", pollers[p].name);
134 if (pollers[p].pref == 0)
135 fprintf(out, "disabled, ");
136 else
137 fprintf(out, "pref=%3d, ", pollers[p].pref);
138 if (pollers[p].test(&pollers[p])) {
139 fprintf(out, " test result OK");
140 if (next > 0)
141 usable++;
142 } else
143 fprintf(out, " test result FAILED");
144 fprintf(out, "\n");
145 }
146 }
147 last = next;
148 next = -1;
149 };
150 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
151 return 0;
152}
153
154/*
155 * Some pollers may lose their connection after a fork(). It may be necessary
156 * to create initialize part of them again. Returns 0 in case of failure,
157 * otherwise 1. The fork() function may be NULL if unused. In case of error,
158 * the the current poller is destroyed and the caller is responsible for trying
159 * another one by calling init_pollers() again.
160 */
161int fork_poller()
162{
163 if (cur_poller.fork) {
164 if (cur_poller.fork(&cur_poller))
165 return 1;
166 cur_poller.term(&cur_poller);
167 return 0;
168 }
169 return 1;
170}
171
172/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173 * Local variables:
174 * c-indent-level: 8
175 * c-basic-offset: 8
176 * End:
177 */