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 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 90 | * Lists the known pollers on <out>. |
| 91 | * Should be performed only before initialization. |
| 92 | */ |
| 93 | int list_pollers(FILE *out) |
| 94 | { |
| 95 | int p; |
| 96 | int last, next; |
| 97 | int usable; |
| 98 | struct poller *bp; |
| 99 | |
| 100 | fprintf(out, "Available polling systems :\n"); |
| 101 | |
| 102 | usable = 0; |
| 103 | bp = NULL; |
| 104 | last = next = -1; |
| 105 | while (1) { |
| 106 | for (p = 0; p < nbpollers; p++) { |
| 107 | if (!bp || (pollers[p].pref > bp->pref)) |
| 108 | bp = &pollers[p]; |
| 109 | if ((next < 0 || pollers[p].pref > next) |
| 110 | && (last < 0 || pollers[p].pref < last)) |
| 111 | next = pollers[p].pref; |
| 112 | } |
| 113 | |
| 114 | if (next == -1) |
| 115 | break; |
| 116 | |
| 117 | for (p = 0; p < nbpollers; p++) { |
| 118 | if (pollers[p].pref == next) { |
| 119 | fprintf(out, " %10s : ", pollers[p].name); |
| 120 | if (pollers[p].pref == 0) |
| 121 | fprintf(out, "disabled, "); |
| 122 | else |
| 123 | fprintf(out, "pref=%3d, ", pollers[p].pref); |
| 124 | if (pollers[p].test(&pollers[p])) { |
| 125 | fprintf(out, " test result OK"); |
| 126 | if (next > 0) |
| 127 | usable++; |
| 128 | } else |
| 129 | fprintf(out, " test result FAILED"); |
| 130 | fprintf(out, "\n"); |
| 131 | } |
| 132 | } |
| 133 | last = next; |
| 134 | next = -1; |
| 135 | }; |
| 136 | fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none"); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 142 | * to create initialize part of them again. Returns 0 in case of failure, |
| 143 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 144 | * the the current poller is destroyed and the caller is responsible for trying |
| 145 | * another one by calling init_pollers() again. |
| 146 | */ |
| 147 | int fork_poller() |
| 148 | { |
| 149 | if (cur_poller.fork) { |
| 150 | if (cur_poller.fork(&cur_poller)) |
| 151 | return 1; |
| 152 | cur_poller.term(&cur_poller); |
| 153 | return 0; |
| 154 | } |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 159 | * Local variables: |
| 160 | * c-indent-level: 8 |
| 161 | * c-basic-offset: 8 |
| 162 | * End: |
| 163 | */ |