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