blob: 9c370faf93d7179b2e40a40f8eae05e849ee7f75 [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/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020090 * Lists the known pollers on <out>.
91 * Should be performed only before initialization.
92 */
93int list_pollers(FILE *out)
94{
95 int p;
96 int last, next;
97 int usable;
98 struct poller *bp;
99
100 fprintf(out, "Available polling systems :\n");
101
102 usable = 0;
103 bp = NULL;
104 last = next = -1;
105 while (1) {
106 for (p = 0; p < nbpollers; p++) {
107 if (!bp || (pollers[p].pref > bp->pref))
108 bp = &pollers[p];
109 if ((next < 0 || pollers[p].pref > next)
110 && (last < 0 || pollers[p].pref < last))
111 next = pollers[p].pref;
112 }
113
114 if (next == -1)
115 break;
116
117 for (p = 0; p < nbpollers; p++) {
118 if (pollers[p].pref == next) {
119 fprintf(out, " %10s : ", pollers[p].name);
120 if (pollers[p].pref == 0)
121 fprintf(out, "disabled, ");
122 else
123 fprintf(out, "pref=%3d, ", pollers[p].pref);
124 if (pollers[p].test(&pollers[p])) {
125 fprintf(out, " test result OK");
126 if (next > 0)
127 usable++;
128 } else
129 fprintf(out, " test result FAILED");
130 fprintf(out, "\n");
131 }
132 }
133 last = next;
134 next = -1;
135 };
136 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
137 return 0;
138}
139
140/*
141 * Some pollers may lose their connection after a fork(). It may be necessary
142 * to create initialize part of them again. Returns 0 in case of failure,
143 * otherwise 1. The fork() function may be NULL if unused. In case of error,
144 * the the current poller is destroyed and the caller is responsible for trying
145 * another one by calling init_pollers() again.
146 */
147int fork_poller()
148{
149 if (cur_poller.fork) {
150 if (cur_poller.fork(&cur_poller))
151 return 1;
152 cur_poller.term(&cur_poller);
153 return 0;
154 }
155 return 1;
156}
157
158/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159 * Local variables:
160 * c-indent-level: 8
161 * c-basic-offset: 8
162 * End:
163 */