1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
/*
* Protocol registration functions.
*
* Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <haproxy/api.h>
#include <haproxy/errors.h>
#include <haproxy/list.h>
#include <haproxy/listener.h>
#include <haproxy/protocol.h>
#include <haproxy/proxy.h>
#include <haproxy/tools.h>
/* List head of all registered protocols */
static struct list protocols = LIST_HEAD_INIT(protocols);
struct protocol *__protocol_by_family[AF_CUST_MAX][PROTO_NUM_TYPES][2] __read_mostly = { };
/* This is the global spinlock we may need to register/unregister listeners or
* protocols. Its main purpose is in fact to serialize the rare stop/deinit()
* phases.
*/
__decl_spinlock(proto_lock);
/* Registers the protocol <proto> */
void protocol_register(struct protocol *proto)
{
int sock_domain = proto->fam->sock_domain;
BUG_ON(sock_domain < 0 || sock_domain >= AF_CUST_MAX);
BUG_ON(proto->proto_type >= PROTO_NUM_TYPES);
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
LIST_APPEND(&protocols, &proto->list);
__protocol_by_family[sock_domain]
[proto->proto_type]
[proto->xprt_type == PROTO_TYPE_DGRAM] = proto;
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
}
/* Unregisters the protocol <proto>. Note that all listeners must have
* previously been unbound.
*/
void protocol_unregister(struct protocol *proto)
{
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
LIST_DELETE(&proto->list);
LIST_INIT(&proto->list);
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
}
/* binds all listeners of all registered protocols. Returns a composition
* of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
*/
int protocol_bind_all(int verbose)
{
struct protocol *proto;
struct listener *listener;
struct receiver *receiver;
char msg[1000];
char *errmsg;
int err, lerr;
err = 0;
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
list_for_each_entry(proto, &protocols, list) {
list_for_each_entry(receiver, &proto->receivers, proto_list) {
listener = LIST_ELEM(receiver, struct listener *, rx);
lerr = proto->fam->bind(receiver, &errmsg);
err |= lerr;
/* errors are reported if <verbose> is set or if they are fatal */
if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
struct proxy *px = listener->bind_conf->frontend;
if (lerr & ERR_ALERT)
ha_alert("Binding [%s:%d] for %s %s: %s\n",
listener->bind_conf->file, listener->bind_conf->line,
proxy_type_str(px), px->id, errmsg);
else if (lerr & ERR_WARN)
ha_warning("Binding [%s:%d] for %s %s: %s\n",
listener->bind_conf->file, listener->bind_conf->line,
proxy_type_str(px), px->id, errmsg);
}
if (lerr != ERR_NONE)
ha_free(&errmsg);
if (lerr & ERR_ABORT)
break;
if (lerr & ~ERR_WARN)
continue;
/* for now there's still always a listening function */
BUG_ON(!proto->listen);
lerr = proto->listen(listener, msg, sizeof(msg));
err |= lerr;
if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
struct proxy *px = listener->bind_conf->frontend;
if (lerr & ERR_ALERT)
ha_alert("Starting [%s:%d] for %s %s: %s\n",
listener->bind_conf->file, listener->bind_conf->line,
proxy_type_str(px), px->id, msg);
else if (lerr & ERR_WARN)
ha_warning("Starting [%s:%d] for %s %s: %s\n",
listener->bind_conf->file, listener->bind_conf->line,
proxy_type_str(px), px->id, msg);
}
if (lerr & ERR_ABORT)
break;
}
if (err & ERR_ABORT)
break;
}
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
return err;
}
/* unbinds all listeners of all registered protocols. They are also closed.
* This must be performed before calling exit() in order to get a chance to
* remove file-system based sockets and pipes.
* Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
*/
int protocol_unbind_all(void)
{
struct protocol *proto;
struct listener *listener;
int err;
err = 0;
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
list_for_each_entry(proto, &protocols, list) {
list_for_each_entry(listener, &proto->receivers, rx.proto_list)
unbind_listener(listener);
}
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
return err;
}
/* stops all listeners of all registered protocols. This will normally catch
* every single listener, all protocols included. This is to be used during
* soft_stop() only. It does not return any error.
*/
void protocol_stop_now(void)
{
struct protocol *proto;
struct listener *listener, *lback;
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
list_for_each_entry(proto, &protocols, list) {
list_for_each_entry_safe(listener, lback, &proto->receivers, rx.proto_list)
stop_listener(listener, 0, 1);
}
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
}
/* pauses all listeners of all registered protocols. This is typically
* used on SIG_TTOU to release all listening sockets for the time needed to
* try to bind a new process. The listeners enter LI_PAUSED. It returns
* ERR_NONE, with ERR_FATAL on failure.
*/
int protocol_pause_all(void)
{
struct protocol *proto;
struct listener *listener;
int err;
err = 0;
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
list_for_each_entry(proto, &protocols, list) {
list_for_each_entry(listener, &proto->receivers, rx.proto_list)
if (!pause_listener(listener, 0))
err |= ERR_FATAL;
}
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
return err;
}
/* resumes all listeners of all registered protocols. This is typically used on
* SIG_TTIN to re-enable listening sockets after a new process failed to bind.
* The listeners switch to LI_READY/LI_FULL. It returns ERR_NONE, with ERR_FATAL
* on failure.
*/
int protocol_resume_all(void)
{
struct protocol *proto;
struct listener *listener;
int err;
err = 0;
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
list_for_each_entry(proto, &protocols, list) {
list_for_each_entry(listener, &proto->receivers, rx.proto_list)
if (!resume_listener(listener, 0))
err |= ERR_FATAL;
}
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
return err;
}
/* enables all listeners of all registered protocols. This is intended to be
* used after a fork() to enable reading on all file descriptors. Returns
* composition of ERR_NONE.
*/
int protocol_enable_all(void)
{
struct protocol *proto;
struct listener *listener;
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
list_for_each_entry(proto, &protocols, list) {
list_for_each_entry(listener, &proto->receivers, rx.proto_list)
enable_listener(listener);
}
HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
return ERR_NONE;
}
/*
* Local variables:
* c-indent-level: 8
* c-basic-offset: 8
* End:
*/
|