blob: d84ce0a8267020f4d622f87a36dcff89945a84d6 [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 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +020025struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +020026int maxfd; /* # of the highest fd + 1 */
27int totalconn; /* total # of terminated sessions */
28int actconn; /* # of active sessions */
29
Willy Tarreau4f60f162007-04-08 16:39:58 +020030struct poller pollers[MAX_POLLERS];
31struct poller cur_poller;
32int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34
Willy Tarreau4f60f162007-04-08 16:39:58 +020035/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
36 * The file descriptor is also closed.
37 */
38void fd_delete(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +020039{
Willy Tarreau49b046d2012-08-09 12:11:58 +020040 cur_poller.clo(fd);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +020041 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
42 fdinfo[fd].port_range = NULL;
Willy Tarreau4f60f162007-04-08 16:39:58 +020043 close(fd);
Willy Tarreaudb3b3262012-07-05 23:19:22 +020044 fdtab[fd].owner = NULL;
Willy Tarreau037d2c12012-11-06 02:34:46 +010045 fdtab[fd].updated = fdtab[fd].new = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreaudb3b3262012-07-05 23:19:22 +020047 while ((maxfd-1 >= 0) && !fdtab[maxfd-1].owner)
Willy Tarreau4f60f162007-04-08 16:39:58 +020048 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++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200123 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100124 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200125 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100126 if (!bp || (pollers[p].pref > bp->pref))
127 bp = &pollers[p];
128 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200129 }
130
131 if (next == -1)
132 break;
133
134 for (p = 0; p < nbpollers; p++) {
135 if (pollers[p].pref == next) {
136 fprintf(out, " %10s : ", pollers[p].name);
137 if (pollers[p].pref == 0)
138 fprintf(out, "disabled, ");
139 else
140 fprintf(out, "pref=%3d, ", pollers[p].pref);
141 if (pollers[p].test(&pollers[p])) {
142 fprintf(out, " test result OK");
143 if (next > 0)
144 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100145 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200146 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100147 if (bp == &pollers[p])
148 bp = NULL;
149 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200150 fprintf(out, "\n");
151 }
152 }
153 last = next;
154 next = -1;
155 };
156 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
157 return 0;
158}
159
160/*
161 * Some pollers may lose their connection after a fork(). It may be necessary
162 * to create initialize part of them again. Returns 0 in case of failure,
163 * otherwise 1. The fork() function may be NULL if unused. In case of error,
164 * the the current poller is destroyed and the caller is responsible for trying
165 * another one by calling init_pollers() again.
166 */
167int fork_poller()
168{
169 if (cur_poller.fork) {
170 if (cur_poller.fork(&cur_poller))
171 return 1;
172 cur_poller.term(&cur_poller);
173 return 0;
174 }
175 return 1;
176}
177
178/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 * Local variables:
180 * c-indent-level: 8
181 * c-basic-offset: 8
182 * End:
183 */