blob: c0a00904ca85540087434c319c45a7b8322dbf80 [file] [log] [blame]
Willy Tarreaud1d54542012-09-12 22:58:11 +02001/*
2 * Protocol registration functions.
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
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 Tarreaua6e3be72016-08-10 18:24:48 +020013#include <sys/types.h>
Willy Tarreaub550d002015-02-20 16:53:25 +010014#include <sys/socket.h>
15
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/errors.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020018#include <haproxy/list.h>
Willy Tarreau94320852020-09-01 18:48:35 +020019#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/protocol.h>
Willy Tarreaue91bff22020-09-02 11:11:43 +020021#include <haproxy/proxy.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020022#include <haproxy/tools.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020023
Willy Tarreaud1d54542012-09-12 22:58:11 +020024
25/* List head of all registered protocols */
26static struct list protocols = LIST_HEAD_INIT(protocols);
Willy Tarreau910c64d2020-09-16 21:28:15 +020027struct protocol *__protocol_by_family[AF_CUST_MAX][2][2] = { };
Willy Tarreaud1d54542012-09-12 22:58:11 +020028
Willy Tarreaudaacf362019-07-24 16:45:02 +020029/* This is the global spinlock we may need to register/unregister listeners or
30 * protocols. Its main purpose is in fact to serialize the rare stop/deinit()
31 * phases.
32 */
33__decl_spinlock(proto_lock);
34
Willy Tarreaud1d54542012-09-12 22:58:11 +020035/* Registers the protocol <proto> */
36void protocol_register(struct protocol *proto)
37{
Willy Tarreaudaacf362019-07-24 16:45:02 +020038 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020039 LIST_ADDQ(&protocols, &proto->list);
William Lallemand2d3f8a42018-09-11 16:51:28 +020040 if (proto->sock_domain >= 0 && proto->sock_domain < AF_CUST_MAX)
Willy Tarreau910c64d2020-09-16 21:28:15 +020041 __protocol_by_family[proto->sock_domain]
42 [proto->sock_type == SOCK_DGRAM]
43 [proto->ctrl_type == SOCK_DGRAM] = proto;
Willy Tarreaudaacf362019-07-24 16:45:02 +020044 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020045}
46
47/* Unregisters the protocol <proto>. Note that all listeners must have
48 * previously been unbound.
49 */
50void protocol_unregister(struct protocol *proto)
51{
Willy Tarreaudaacf362019-07-24 16:45:02 +020052 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020053 LIST_DEL(&proto->list);
54 LIST_INIT(&proto->list);
Willy Tarreaudaacf362019-07-24 16:45:02 +020055 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020056}
57
58/* binds all listeners of all registered protocols. Returns a composition
59 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
60 */
Willy Tarreaue91bff22020-09-02 11:11:43 +020061int protocol_bind_all(int verbose)
Willy Tarreaud1d54542012-09-12 22:58:11 +020062{
63 struct protocol *proto;
Willy Tarreau94320852020-09-01 18:48:35 +020064 struct listener *listener;
Willy Tarreaufc974882020-09-02 18:22:11 +020065 struct receiver *receiver;
Willy Tarreaue91bff22020-09-02 11:11:43 +020066 char msg[100];
Willy Tarreaufc974882020-09-02 18:22:11 +020067 char *errmsg;
68 void *handler;
Willy Tarreaue91bff22020-09-02 11:11:43 +020069 int err, lerr;
Willy Tarreaud1d54542012-09-12 22:58:11 +020070
71 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +020072 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020073 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +020074 list_for_each_entry(receiver, &proto->receivers, proto_list) {
Willy Tarreaufc974882020-09-02 18:22:11 +020075 listener = LIST_ELEM(receiver, struct listener *, rx);
76
77 /* FIXME: horrible hack, we don't have a way to register
78 * a handler when creating the receiver yet, so we still
79 * have to take care of special cases here.
80 */
81 handler = listener->rx.proto->accept;
82 if (!handler && listener->bind_conf->frontend->mode == PR_MODE_SYSLOG) {
83 extern void syslog_fd_handler(int);
84 handler = syslog_fd_handler;
85 }
86
Willy Tarreauf1f66092020-09-04 08:15:31 +020087 lerr = proto->fam->bind(receiver, handler, &errmsg);
Willy Tarreaufc974882020-09-02 18:22:11 +020088 err |= lerr;
Willy Tarreaue91bff22020-09-02 11:11:43 +020089
90 /* errors are reported if <verbose> is set or if they are fatal */
91 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
92 struct proxy *px = listener->bind_conf->frontend;
93
94 if (lerr & ERR_ALERT)
95 ha_alert("Starting %s %s: %s\n",
Willy Tarreaufc974882020-09-02 18:22:11 +020096 proxy_type_str(px), px->id, errmsg);
Willy Tarreaue91bff22020-09-02 11:11:43 +020097 else if (lerr & ERR_WARN)
98 ha_warning("Starting %s %s: %s\n",
Willy Tarreaufc974882020-09-02 18:22:11 +020099 proxy_type_str(px), px->id, errmsg);
100 free(errmsg); errmsg = NULL;
Willy Tarreaue91bff22020-09-02 11:11:43 +0200101 }
Willy Tarreaufc974882020-09-02 18:22:11 +0200102 if (lerr & ERR_ABORT)
103 break;
Willy Tarreaue91bff22020-09-02 11:11:43 +0200104
Willy Tarreaufc974882020-09-02 18:22:11 +0200105 if (lerr & ~ERR_WARN)
106 continue;
107
108 /* for now there's still always a listening function */
109 BUG_ON(!proto->listen);
110 lerr = proto->listen(listener, msg, sizeof(msg));
Willy Tarreaue91bff22020-09-02 11:11:43 +0200111 err |= lerr;
Willy Tarreaufc974882020-09-02 18:22:11 +0200112
113 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
114 struct proxy *px = listener->bind_conf->frontend;
115
116 if (lerr & ERR_ALERT)
117 ha_alert("Starting %s %s: %s\n",
118 proxy_type_str(px), px->id, msg);
119 else if (lerr & ERR_WARN)
120 ha_warning("Starting %s %s: %s\n",
121 proxy_type_str(px), px->id, msg);
122 }
Willy Tarreaue91bff22020-09-02 11:11:43 +0200123 if (lerr & ERR_ABORT)
Willy Tarreaud1d54542012-09-12 22:58:11 +0200124 break;
125 }
Willy Tarreaue91bff22020-09-02 11:11:43 +0200126 if (err & ERR_ABORT)
127 break;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200128 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200129 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200130 return err;
131}
132
133/* unbinds all listeners of all registered protocols. They are also closed.
134 * This must be performed before calling exit() in order to get a chance to
135 * remove file-system based sockets and pipes.
136 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
137 */
138int protocol_unbind_all(void)
139{
140 struct protocol *proto;
Willy Tarreauca212622020-09-02 10:31:31 +0200141 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200142 int err;
143
144 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200145 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200146 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200147 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreauca212622020-09-02 10:31:31 +0200148 unbind_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200149 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200150 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200151 return err;
152}
153
Willy Tarreau09819d12020-09-24 16:26:50 +0200154/* pauses all listeners of all registered protocols. This is typically
155 * used on SIG_TTOU to release all listening sockets for the time needed to
156 * try to bind a new process. The listeners enter LI_PAUSED. It returns
157 * ERR_NONE, with ERR_FATAL on failure.
158 */
159int protocol_pause_all(void)
160{
161 struct protocol *proto;
162 struct listener *listener;
163 int err;
164
165 err = 0;
166 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
167 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200168 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreau09819d12020-09-24 16:26:50 +0200169 if (!pause_listener(listener))
170 err |= ERR_FATAL;
171 }
172 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
173 return err;
174}
175
176/* resumes all listeners of all registered protocols. This is typically used on
177 * SIG_TTIN to re-enable listening sockets after a new process failed to bind.
178 * The listeners switch to LI_READY/LI_FULL. It returns ERR_NONE, with ERR_FATAL
179 * on failure.
180 */
181int protocol_resume_all(void)
182{
183 struct protocol *proto;
184 struct listener *listener;
185 int err;
186
187 err = 0;
188 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
189 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200190 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreau09819d12020-09-24 16:26:50 +0200191 if (!resume_listener(listener))
192 err |= ERR_FATAL;
193 }
194 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
195 return err;
196}
197
Willy Tarreaud1d54542012-09-12 22:58:11 +0200198/* enables all listeners of all registered protocols. This is intended to be
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200199 * used after a fork() to enable reading on all file descriptors. Returns
200 * composition of ERR_NONE.
Willy Tarreaud1d54542012-09-12 22:58:11 +0200201 */
202int protocol_enable_all(void)
203{
204 struct protocol *proto;
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200205 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200206
Willy Tarreaudaacf362019-07-24 16:45:02 +0200207 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200208 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200209 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200210 enable_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200211 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200212 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200213 return ERR_NONE;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200214}
215
Willy Tarreaud1d54542012-09-12 22:58:11 +0200216/*
217 * Local variables:
218 * c-indent-level: 8
219 * c-basic-offset: 8
220 * End:
221 */