blob: eaaaabf50b34322ecfa71b5e184d509559afd307 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * File descriptors management functions.
3 *
Willy Tarreau4f60f162007-04-08 16:39:58 +02004 * Copyright 2000-2007 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
21#include <types/fd.h>
22#include <types/global.h>
23
Willy Tarreau2a429502006-10-15 14:52:29 +020024#include <proto/fd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
26struct fdtab *fdtab = NULL; /* array of all the file descriptors */
27int maxfd; /* # of the highest fd + 1 */
28int totalconn; /* total # of terminated sessions */
29int actconn; /* # of active sessions */
30
Willy Tarreaubaaee002006-06-26 02:48:02 +020031int cfg_polling_mechanism = 0; /* POLL_USE_{SELECT|POLL|EPOLL} */
32
Willy Tarreau4f60f162007-04-08 16:39:58 +020033struct poller pollers[MAX_POLLERS];
34struct poller cur_poller;
35int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37
Willy Tarreau4f60f162007-04-08 16:39:58 +020038/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
39 * The file descriptor is also closed.
40 */
41void fd_delete(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +020042{
Willy Tarreau4f60f162007-04-08 16:39:58 +020043 EV_FD_CLO(fd);
44 close(fd);
45 fdtab[fd].state = FD_STCLOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau4f60f162007-04-08 16:39:58 +020047 while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE))
48 maxfd--;
Willy Tarreaubaaee002006-06-26 02:48:02 +020049}
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
Willy Tarreau4f60f162007-04-08 16:39:58 +020052/* disable the specified poller */
53void disable_poller(const char *poller_name)
54{
55 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Willy Tarreau4f60f162007-04-08 16:39:58 +020057 for (p = 0; p < nbpollers; p++)
58 if (strcmp(pollers[p].name, poller_name) == 0)
59 pollers[p].pref = 0;
60}
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
62/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020063 * Initialize the pollers till the best one is found.
64 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020066int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +020067{
Willy Tarreau4f60f162007-04-08 16:39:58 +020068 int p;
69 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
Willy Tarreau4f60f162007-04-08 16:39:58 +020072 do {
73 bp = NULL;
74 for (p = 0; p < nbpollers; p++)
75 if (!bp || (pollers[p].pref > bp->pref))
76 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Willy Tarreau4f60f162007-04-08 16:39:58 +020078 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +020079 break;
80
Willy Tarreau4f60f162007-04-08 16:39:58 +020081 if (bp->init(bp)) {
82 memcpy(&cur_poller, bp, sizeof(*bp));
83 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 }
Willy Tarreau4f60f162007-04-08 16:39:58 +020085 } while (!bp || bp->pref == 0);
86 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +020087}
88
Willy Tarreaubaaee002006-06-26 02:48:02 +020089/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020090 * Deinitialize the pollers.
91 */
92void deinit_pollers() {
93
94 struct poller *bp;
95 int p;
96
97 for (p = 0; p < nbpollers; p++) {
98 bp = &pollers[p];
99
100 if (bp && bp->pref)
101 bp->term(bp);
102 }
103}
104
105/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200106 * Lists the known pollers on <out>.
107 * Should be performed only before initialization.
108 */
109int list_pollers(FILE *out)
110{
111 int p;
112 int last, next;
113 int usable;
114 struct poller *bp;
115
116 fprintf(out, "Available polling systems :\n");
117
118 usable = 0;
119 bp = NULL;
120 last = next = -1;
121 while (1) {
122 for (p = 0; p < nbpollers; p++) {
123 if (!bp || (pollers[p].pref > bp->pref))
124 bp = &pollers[p];
125 if ((next < 0 || pollers[p].pref > next)
126 && (last < 0 || pollers[p].pref < last))
127 next = pollers[p].pref;
128 }
129
130 if (next == -1)
131 break;
132
133 for (p = 0; p < nbpollers; p++) {
134 if (pollers[p].pref == next) {
135 fprintf(out, " %10s : ", pollers[p].name);
136 if (pollers[p].pref == 0)
137 fprintf(out, "disabled, ");
138 else
139 fprintf(out, "pref=%3d, ", pollers[p].pref);
140 if (pollers[p].test(&pollers[p])) {
141 fprintf(out, " test result OK");
142 if (next > 0)
143 usable++;
144 } else
145 fprintf(out, " test result FAILED");
146 fprintf(out, "\n");
147 }
148 }
149 last = next;
150 next = -1;
151 };
152 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
153 return 0;
154}
155
156/*
157 * Some pollers may lose their connection after a fork(). It may be necessary
158 * to create initialize part of them again. Returns 0 in case of failure,
159 * otherwise 1. The fork() function may be NULL if unused. In case of error,
160 * the the current poller is destroyed and the caller is responsible for trying
161 * another one by calling init_pollers() again.
162 */
163int fork_poller()
164{
165 if (cur_poller.fork) {
166 if (cur_poller.fork(&cur_poller))
167 return 1;
168 cur_poller.term(&cur_poller);
169 return 0;
170 }
171 return 1;
172}
173
174/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175 * Local variables:
176 * c-indent-level: 8
177 * c-basic-offset: 8
178 * End:
179 */