Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 2 | * include/proto/listener.h |
| 3 | * This file declares listener management primitives. |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 22 | #ifndef _PROTO_LISTENER_H |
| 23 | #define _PROTO_LISTENER_H |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 25 | #include <types/listener.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 27 | /* This function adds the specified listener's file descriptor to the polling |
| 28 | * lists if it is in the LI_LISTEN state. The listener enters LI_READY or |
| 29 | * LI_FULL state depending on its number of connections. |
| 30 | */ |
| 31 | void enable_listener(struct listener *listener); |
| 32 | |
| 33 | /* This function removes the specified listener's file descriptor from the |
| 34 | * polling lists if it is in the LI_READY or in the LI_FULL state. The listener |
| 35 | * enters LI_LISTEN. |
| 36 | */ |
| 37 | void disable_listener(struct listener *listener); |
| 38 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 39 | /* This function tries to temporarily disable a listener, depending on the OS |
| 40 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 41 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 42 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 43 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 44 | * is disabled. It normally returns non-zero, unless an error is reported. |
| 45 | */ |
| 46 | int pause_listener(struct listener *l); |
| 47 | |
| 48 | /* This function tries to resume a temporarily disabled listener. |
| 49 | * The resulting state will either be LI_READY or LI_FULL. 0 is returned |
| 50 | * in case of failure to resume (eg: dead socket). |
| 51 | */ |
| 52 | int resume_listener(struct listener *l); |
| 53 | |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 54 | /* Marks a ready listener as full so that the session code tries to re-enable |
| 55 | * it upon next close() using resume_listener(). |
| 56 | */ |
| 57 | void listener_full(struct listener *l); |
| 58 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 59 | /* This function adds all of the protocol's listener's file descriptors to the |
| 60 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 61 | * used as a protocol's generic enable_all() primitive, for use after the |
| 62 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 63 | * their number of connections. It always returns ERR_NONE. |
| 64 | */ |
| 65 | int enable_all_listeners(struct protocol *proto); |
| 66 | |
| 67 | /* This function removes all of the protocol's listener's file descriptors from |
| 68 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 69 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 70 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 71 | */ |
| 72 | int disable_all_listeners(struct protocol *proto); |
| 73 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 74 | /* Marks a ready listener as limited so that we only try to re-enable it when |
| 75 | * resources are free again. It will be queued into the specified queue. |
| 76 | */ |
| 77 | void limit_listener(struct listener *l, struct list *list); |
| 78 | |
| 79 | /* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */ |
| 80 | void dequeue_all_listeners(struct list *list); |
| 81 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 82 | /* This function closes the listening socket for the specified listener, |
| 83 | * provided that it's already in a listening state. The listener enters the |
| 84 | * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended |
| 85 | * to be used as a generic function for standard protocols. |
| 86 | */ |
| 87 | int unbind_listener(struct listener *listener); |
| 88 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 89 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 90 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 91 | * detach them from the protocol. It always returns ERR_NONE. |
| 92 | */ |
| 93 | int unbind_all_listeners(struct protocol *proto); |
| 94 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 95 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 96 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
| 97 | * number of listeners is updated. Note that the listener must have previously |
| 98 | * been unbound. This is the generic function to use to remove a listener. |
| 99 | */ |
| 100 | void delete_listener(struct listener *listener); |
| 101 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 102 | /* This function is called on a read event from a listening socket, corresponding |
| 103 | * to an accept. It tries to accept as many connections as possible, and for each |
| 104 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 105 | */ |
| 106 | int listener_accept(int fd); |
| 107 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 108 | /* |
| 109 | * Registers the bind keyword list <kwl> as a list of valid keywords for next |
| 110 | * parsing sessions. |
| 111 | */ |
| 112 | void bind_register_keywords(struct bind_kw_list *kwl); |
| 113 | |
| 114 | /* Return a pointer to the bind keyword <kw>, or NULL if not found. */ |
| 115 | struct bind_kw *bind_find_kw(const char *kw); |
| 116 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 117 | /* Dumps all registered "bind" keywords to the <out> string pointer. */ |
| 118 | void bind_dump_kws(char **out); |
| 119 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 120 | /* allocate an bind_conf struct for a bind line, and chain it to list head <lh>. |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 121 | * If <arg> is not NULL, it is duplicated into ->arg to store useful config |
| 122 | * information for error reporting. |
| 123 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 124 | static inline struct bind_conf *bind_conf_alloc(struct list *lh, const char *file, int line, const char *arg) |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 125 | { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 126 | struct bind_conf *bind_conf = (void *)calloc(1, sizeof(struct bind_conf)); |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 128 | bind_conf->file = strdup(file); |
| 129 | bind_conf->line = line; |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 130 | if (lh) |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 131 | LIST_ADDQ(lh, &bind_conf->by_fe); |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 132 | if (arg) |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 133 | bind_conf->arg = strdup(arg); |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 134 | |
| 135 | bind_conf->ux.uid = -1; |
| 136 | bind_conf->ux.gid = -1; |
| 137 | bind_conf->ux.mode = 0; |
| 138 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 139 | LIST_INIT(&bind_conf->listeners); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 140 | return bind_conf; |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 143 | #endif /* _PROTO_LISTENER_H */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Local variables: |
| 147 | * c-indent-level: 8 |
| 148 | * c-basic-offset: 8 |
| 149 | * End: |
| 150 | */ |