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