blob: 079d976a30716e6317d83fad0c1de32c95b49aab [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * 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 Tarreaudd815982007-10-16 12:25:14 +020021
Willy Tarreaud1d54542012-09-12 22:58:11 +020022#ifndef _PROTO_LISTENER_H
23#define _PROTO_LISTENER_H
Willy Tarreaudd815982007-10-16 12:25:14 +020024
Thierry FOURNIERd3879e82013-06-10 15:09:19 +020025#include <string.h>
26
Willy Tarreaud1d54542012-09-12 22:58:11 +020027#include <types/listener.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020028
Willy Tarreaudabf2e22007-10-28 21:59:24 +010029/* This function adds the specified listener's file descriptor to the polling
30 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
31 * LI_FULL state depending on its number of connections.
32 */
33void enable_listener(struct listener *listener);
34
35/* This function removes the specified listener's file descriptor from the
36 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
37 * enters LI_LISTEN.
38 */
39void disable_listener(struct listener *listener);
40
Willy Tarreaube58c382011-07-24 18:28:10 +020041/* This function tries to temporarily disable a listener, depending on the OS
42 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
43 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
44 * closes upon SHUT_WR and refuses to rebind. So a common validation path
45 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
46 * is disabled. It normally returns non-zero, unless an error is reported.
47 */
48int pause_listener(struct listener *l);
49
50/* This function tries to resume a temporarily disabled listener.
51 * The resulting state will either be LI_READY or LI_FULL. 0 is returned
52 * in case of failure to resume (eg: dead socket).
53 */
54int resume_listener(struct listener *l);
55
Willy Tarreau62793712011-07-24 19:23:38 +020056/* Marks a ready listener as full so that the session code tries to re-enable
57 * it upon next close() using resume_listener().
58 */
59void listener_full(struct listener *l);
60
Willy Tarreaudabf2e22007-10-28 21:59:24 +010061/* This function adds all of the protocol's listener's file descriptors to the
62 * polling lists when they are in the LI_LISTEN state. It is intended to be
63 * used as a protocol's generic enable_all() primitive, for use after the
64 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
65 * their number of connections. It always returns ERR_NONE.
66 */
67int enable_all_listeners(struct protocol *proto);
68
69/* This function removes all of the protocol's listener's file descriptors from
70 * the polling lists when they are in the LI_READY or LI_FULL states. It is
71 * intended to be used as a protocol's generic disable_all() primitive. It puts
72 * the listeners into LI_LISTEN, and always returns ERR_NONE.
73 */
74int disable_all_listeners(struct protocol *proto);
75
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020076/* Marks a ready listener as limited so that we only try to re-enable it when
77 * resources are free again. It will be queued into the specified queue.
78 */
79void limit_listener(struct listener *l, struct list *list);
80
81/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
82void dequeue_all_listeners(struct list *list);
83
Willy Tarreaub648d632007-10-28 22:13:50 +010084/* This function closes the listening socket for the specified listener,
85 * provided that it's already in a listening state. The listener enters the
86 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
87 * to be used as a generic function for standard protocols.
88 */
89int unbind_listener(struct listener *listener);
90
Olivier Houchard1fc05162017-04-06 01:05:05 +020091/* This function pretends the listener is dead, but keeps the FD opened, so
92 * that we can provide it, for conf reloading.
93 */
94int unbind_listener_no_close(struct listener *listener);
95
Willy Tarreau3acf8c32007-10-28 22:35:41 +010096/* This function closes all listening sockets bound to the protocol <proto>,
97 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
98 * detach them from the protocol. It always returns ERR_NONE.
99 */
100int unbind_all_listeners(struct protocol *proto);
101
Willy Tarreau1a64d162007-10-28 22:26:05 +0100102/* Delete a listener from its protocol's list of listeners. The listener's
103 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
104 * number of listeners is updated. Note that the listener must have previously
105 * been unbound. This is the generic function to use to remove a listener.
106 */
107void delete_listener(struct listener *listener);
108
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200109/* This function is called on a read event from a listening socket, corresponding
110 * to an accept. It tries to accept as many connections as possible, and for each
111 * calls the listener's accept handler (generally the frontend's accept handler).
112 */
Willy Tarreau7a798e52016-04-14 11:13:20 +0200113void listener_accept(int fd);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200114
Willy Tarreau26982662012-09-12 23:17:10 +0200115/*
116 * Registers the bind keyword list <kwl> as a list of valid keywords for next
117 * parsing sessions.
118 */
119void bind_register_keywords(struct bind_kw_list *kwl);
120
121/* Return a pointer to the bind keyword <kw>, or NULL if not found. */
122struct bind_kw *bind_find_kw(const char *kw);
123
Willy Tarreau8638f482012-09-18 18:01:17 +0200124/* Dumps all registered "bind" keywords to the <out> string pointer. */
125void bind_dump_kws(char **out);
126
Willy Tarreauc95bad52016-12-22 00:13:31 +0100127/* allocate an bind_conf struct for a bind line, and chain it to the frontend <fe>.
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200128 * If <arg> is not NULL, it is duplicated into ->arg to store useful config
129 * information for error reporting.
130 */
Willy Tarreauc95bad52016-12-22 00:13:31 +0100131static inline struct bind_conf *bind_conf_alloc(struct proxy *fe, const char *file,
Willy Tarreau71a8c7c2016-12-21 22:04:54 +0100132 int line, const char *arg, struct xprt_ops *xprt)
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200133{
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200134 struct bind_conf *bind_conf = (void *)calloc(1, sizeof(struct bind_conf));
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200135
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200136 bind_conf->file = strdup(file);
137 bind_conf->line = line;
Willy Tarreauc95bad52016-12-22 00:13:31 +0100138 LIST_ADDQ(&fe->conf.bind, &bind_conf->by_fe);
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200139 if (arg)
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200140 bind_conf->arg = strdup(arg);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200141
142 bind_conf->ux.uid = -1;
143 bind_conf->ux.gid = -1;
144 bind_conf->ux.mode = 0;
Willy Tarreau71a8c7c2016-12-21 22:04:54 +0100145 bind_conf->xprt = xprt;
Willy Tarreauc95bad52016-12-22 00:13:31 +0100146 bind_conf->frontend = fe;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200147
Willy Tarreau4348fad2012-09-20 16:48:07 +0200148 LIST_INIT(&bind_conf->listeners);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200149 return bind_conf;
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200150}
151
Olivier Houchardf73629d2017-04-05 22:33:04 +0200152extern struct xfer_sock_list *xfer_sock_list;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200153#endif /* _PROTO_LISTENER_H */
Willy Tarreaudd815982007-10-16 12:25:14 +0200154
155/*
156 * Local variables:
157 * c-indent-level: 8
158 * c-basic-offset: 8
159 * End:
160 */