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