Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * File descriptors management functions. |
| 3 | * |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 13 | #include <stdio.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 14 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 15 | #include <unistd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 18 | #include <common/compat.h> |
| 19 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | |
| 21 | #include <types/fd.h> |
| 22 | #include <types/global.h> |
| 23 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 24 | #include <proto/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | |
| 26 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
| 27 | int maxfd; /* # of the highest fd + 1 */ |
| 28 | int totalconn; /* total # of terminated sessions */ |
| 29 | int actconn; /* # of active sessions */ |
| 30 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | int cfg_polling_mechanism = 0; /* POLL_USE_{SELECT|POLL|EPOLL} */ |
| 32 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 33 | struct poller pollers[MAX_POLLERS]; |
| 34 | struct poller cur_poller; |
| 35 | int nbpollers = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
| 37 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 38 | /********************* |
| 39 | * generic functions |
| 40 | *********************/ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 42 | extern int select_register(struct poller *p); |
| 43 | #if defined(ENABLE_POLL) |
| 44 | extern int poll_register(struct poller *p); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 45 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | #if defined(ENABLE_EPOLL) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 47 | extern int epoll_register(struct poller *p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #endif |
Willy Tarreau | e1a7a2f | 2007-04-09 16:11:49 +0200 | [diff] [blame] | 49 | #if defined(ENABLE_KQUEUE) |
| 50 | extern int kqueue_register(struct poller *p); |
| 51 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 54 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 55 | * The file descriptor is also closed. |
| 56 | */ |
| 57 | void fd_delete(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 59 | EV_FD_CLO(fd); |
| 60 | close(fd); |
| 61 | fdtab[fd].state = FD_STCLOSE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 63 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 64 | maxfd--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | |
| 67 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 68 | /* registers all known pollers */ |
| 69 | void register_pollers() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 71 | if (select_register(&pollers[nbpollers])) |
| 72 | nbpollers++; |
| 73 | #if defined(ENABLE_POLL) |
| 74 | poll_register(&pollers[nbpollers]); |
| 75 | nbpollers++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 78 | #if defined(ENABLE_EPOLL) |
| 79 | epoll_register(&pollers[nbpollers]); |
| 80 | nbpollers++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | #endif |
Willy Tarreau | e1a7a2f | 2007-04-09 16:11:49 +0200 | [diff] [blame] | 82 | |
| 83 | #if defined(ENABLE_KQUEUE) |
| 84 | kqueue_register(&pollers[nbpollers]); |
| 85 | nbpollers++; |
| 86 | #endif |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 87 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 88 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 89 | /* disable the specified poller */ |
| 90 | void disable_poller(const char *poller_name) |
| 91 | { |
| 92 | int p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 94 | for (p = 0; p < nbpollers; p++) |
| 95 | if (strcmp(pollers[p].name, poller_name) == 0) |
| 96 | pollers[p].pref = 0; |
| 97 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | |
| 99 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 100 | * Initialize the pollers till the best one is found. |
| 101 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 103 | int init_pollers() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 105 | int p; |
| 106 | struct poller *bp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 109 | do { |
| 110 | bp = NULL; |
| 111 | for (p = 0; p < nbpollers; p++) |
| 112 | if (!bp || (pollers[p].pref > bp->pref)) |
| 113 | bp = &pollers[p]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 115 | if (!bp || bp->pref == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | break; |
| 117 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 118 | if (bp->init(bp)) { |
| 119 | memcpy(&cur_poller, bp, sizeof(*bp)); |
| 120 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 121 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 122 | } while (!bp || bp->pref == 0); |
| 123 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 127 | * Lists the known pollers on <out>. |
| 128 | * Should be performed only before initialization. |
| 129 | */ |
| 130 | int list_pollers(FILE *out) |
| 131 | { |
| 132 | int p; |
| 133 | int last, next; |
| 134 | int usable; |
| 135 | struct poller *bp; |
| 136 | |
| 137 | fprintf(out, "Available polling systems :\n"); |
| 138 | |
| 139 | usable = 0; |
| 140 | bp = NULL; |
| 141 | last = next = -1; |
| 142 | while (1) { |
| 143 | for (p = 0; p < nbpollers; p++) { |
| 144 | if (!bp || (pollers[p].pref > bp->pref)) |
| 145 | bp = &pollers[p]; |
| 146 | if ((next < 0 || pollers[p].pref > next) |
| 147 | && (last < 0 || pollers[p].pref < last)) |
| 148 | next = pollers[p].pref; |
| 149 | } |
| 150 | |
| 151 | if (next == -1) |
| 152 | break; |
| 153 | |
| 154 | for (p = 0; p < nbpollers; p++) { |
| 155 | if (pollers[p].pref == next) { |
| 156 | fprintf(out, " %10s : ", pollers[p].name); |
| 157 | if (pollers[p].pref == 0) |
| 158 | fprintf(out, "disabled, "); |
| 159 | else |
| 160 | fprintf(out, "pref=%3d, ", pollers[p].pref); |
| 161 | if (pollers[p].test(&pollers[p])) { |
| 162 | fprintf(out, " test result OK"); |
| 163 | if (next > 0) |
| 164 | usable++; |
| 165 | } else |
| 166 | fprintf(out, " test result FAILED"); |
| 167 | fprintf(out, "\n"); |
| 168 | } |
| 169 | } |
| 170 | last = next; |
| 171 | next = -1; |
| 172 | }; |
| 173 | fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none"); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 179 | * to create initialize part of them again. Returns 0 in case of failure, |
| 180 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 181 | * the the current poller is destroyed and the caller is responsible for trying |
| 182 | * another one by calling init_pollers() again. |
| 183 | */ |
| 184 | int fork_poller() |
| 185 | { |
| 186 | if (cur_poller.fork) { |
| 187 | if (cur_poller.fork(&cur_poller)) |
| 188 | return 1; |
| 189 | cur_poller.term(&cur_poller); |
| 190 | return 0; |
| 191 | } |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 196 | * Local variables: |
| 197 | * c-indent-level: 8 |
| 198 | * c-basic-offset: 8 |
| 199 | * End: |
| 200 | */ |