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 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 39 | * The file descriptor is also closed. |
| 40 | */ |
| 41 | void fd_delete(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 43 | EV_FD_CLO(fd); |
| 44 | close(fd); |
| 45 | fdtab[fd].state = FD_STCLOSE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 47 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 48 | maxfd--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 52 | /* disable the specified poller */ |
| 53 | void disable_poller(const char *poller_name) |
| 54 | { |
| 55 | int p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 57 | for (p = 0; p < nbpollers; p++) |
| 58 | if (strcmp(pollers[p].name, poller_name) == 0) |
| 59 | pollers[p].pref = 0; |
| 60 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | |
| 62 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 63 | * Initialize the pollers till the best one is found. |
| 64 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 66 | int init_pollers() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 68 | int p; |
| 69 | struct poller *bp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 72 | do { |
| 73 | bp = NULL; |
| 74 | for (p = 0; p < nbpollers; p++) |
| 75 | if (!bp || (pollers[p].pref > bp->pref)) |
| 76 | bp = &pollers[p]; |
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 (!bp || bp->pref == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | break; |
| 80 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 81 | if (bp->init(bp)) { |
| 82 | memcpy(&cur_poller, bp, sizeof(*bp)); |
| 83 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 85 | } while (!bp || bp->pref == 0); |
| 86 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 89 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 90 | * Deinitialize the pollers. |
| 91 | */ |
| 92 | void 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 Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 106 | * Lists the known pollers on <out>. |
| 107 | * Should be performed only before initialization. |
| 108 | */ |
| 109 | int 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 | */ |
| 163 | int 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | * Local variables: |
| 176 | * c-indent-level: 8 |
| 177 | * c-basic-offset: 8 |
| 178 | * End: |
| 179 | */ |