Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | a6e3be7 | 2016-08-10 18:24:48 +0200 | [diff] [blame] | 13 | #include <sys/types.h> |
Willy Tarreau | b550d00 | 2015-02-20 16:53:25 +0100 | [diff] [blame] | 14 | #include <sys/socket.h> |
| 15 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 17 | #include <haproxy/errors.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 18 | #include <haproxy/list.h> |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame] | 19 | #include <haproxy/listener.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/protocol.h> |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 21 | #include <haproxy/proxy.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 22 | #include <haproxy/tools.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 23 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 24 | |
| 25 | /* List head of all registered protocols */ |
| 26 | static struct list protocols = LIST_HEAD_INIT(protocols); |
William Lallemand | 2d3f8a4 | 2018-09-11 16:51:28 +0200 | [diff] [blame] | 27 | struct protocol *__protocol_by_family[AF_CUST_MAX] = { }; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 28 | |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 29 | /* 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 Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 35 | /* Registers the protocol <proto> */ |
| 36 | void protocol_register(struct protocol *proto) |
| 37 | { |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 38 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 39 | LIST_ADDQ(&protocols, &proto->list); |
William Lallemand | 2d3f8a4 | 2018-09-11 16:51:28 +0200 | [diff] [blame] | 40 | if (proto->sock_domain >= 0 && proto->sock_domain < AF_CUST_MAX) |
Willy Tarreau | b550d00 | 2015-02-20 16:53:25 +0100 | [diff] [blame] | 41 | __protocol_by_family[proto->sock_domain] = proto; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 42 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* Unregisters the protocol <proto>. Note that all listeners must have |
| 46 | * previously been unbound. |
| 47 | */ |
| 48 | void protocol_unregister(struct protocol *proto) |
| 49 | { |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 50 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 51 | LIST_DEL(&proto->list); |
| 52 | LIST_INIT(&proto->list); |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 53 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* binds all listeners of all registered protocols. Returns a composition |
| 57 | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 58 | */ |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 59 | int protocol_bind_all(int verbose) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 60 | { |
| 61 | struct protocol *proto; |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame] | 62 | struct listener *listener; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 63 | struct receiver *receiver; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 64 | char msg[100]; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 65 | char *errmsg; |
| 66 | void *handler; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 67 | int err, lerr; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 68 | |
| 69 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 70 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 71 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 72 | list_for_each_entry(receiver, &proto->listeners, proto_list) { |
| 73 | listener = LIST_ELEM(receiver, struct listener *, rx); |
| 74 | |
| 75 | /* FIXME: horrible hack, we don't have a way to register |
| 76 | * a handler when creating the receiver yet, so we still |
| 77 | * have to take care of special cases here. |
| 78 | */ |
| 79 | handler = listener->rx.proto->accept; |
| 80 | if (!handler && listener->bind_conf->frontend->mode == PR_MODE_SYSLOG) { |
| 81 | extern void syslog_fd_handler(int); |
| 82 | handler = syslog_fd_handler; |
| 83 | } |
| 84 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 85 | lerr = proto->fam->bind(receiver, handler, &errmsg); |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 86 | err |= lerr; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 87 | |
| 88 | /* errors are reported if <verbose> is set or if they are fatal */ |
| 89 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 90 | struct proxy *px = listener->bind_conf->frontend; |
| 91 | |
| 92 | if (lerr & ERR_ALERT) |
| 93 | ha_alert("Starting %s %s: %s\n", |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 94 | proxy_type_str(px), px->id, errmsg); |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 95 | else if (lerr & ERR_WARN) |
| 96 | ha_warning("Starting %s %s: %s\n", |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 97 | proxy_type_str(px), px->id, errmsg); |
| 98 | free(errmsg); errmsg = NULL; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 99 | } |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 100 | if (lerr & ERR_ABORT) |
| 101 | break; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 103 | if (lerr & ~ERR_WARN) |
| 104 | continue; |
| 105 | |
| 106 | /* for now there's still always a listening function */ |
| 107 | BUG_ON(!proto->listen); |
| 108 | lerr = proto->listen(listener, msg, sizeof(msg)); |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 109 | err |= lerr; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 110 | |
| 111 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 112 | struct proxy *px = listener->bind_conf->frontend; |
| 113 | |
| 114 | if (lerr & ERR_ALERT) |
| 115 | ha_alert("Starting %s %s: %s\n", |
| 116 | proxy_type_str(px), px->id, msg); |
| 117 | else if (lerr & ERR_WARN) |
| 118 | ha_warning("Starting %s %s: %s\n", |
| 119 | proxy_type_str(px), px->id, msg); |
| 120 | } |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 121 | if (lerr & ERR_ABORT) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 122 | break; |
| 123 | } |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 124 | if (err & ERR_ABORT) |
| 125 | break; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 126 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 127 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 128 | return err; |
| 129 | } |
| 130 | |
| 131 | /* unbinds all listeners of all registered protocols. They are also closed. |
| 132 | * This must be performed before calling exit() in order to get a chance to |
| 133 | * remove file-system based sockets and pipes. |
| 134 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT. |
| 135 | */ |
| 136 | int protocol_unbind_all(void) |
| 137 | { |
| 138 | struct protocol *proto; |
Willy Tarreau | ca21262 | 2020-09-02 10:31:31 +0200 | [diff] [blame] | 139 | struct listener *listener; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 140 | int err; |
| 141 | |
| 142 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 143 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 144 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 145 | list_for_each_entry(listener, &proto->listeners, rx.proto_list) |
Willy Tarreau | ca21262 | 2020-09-02 10:31:31 +0200 | [diff] [blame] | 146 | unbind_listener(listener); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 147 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 148 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 149 | return err; |
| 150 | } |
| 151 | |
| 152 | /* enables all listeners of all registered protocols. This is intended to be |
| 153 | * used after a fork() to enable reading on all file descriptors. Returns a |
| 154 | * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 155 | */ |
| 156 | int protocol_enable_all(void) |
| 157 | { |
| 158 | struct protocol *proto; |
| 159 | int err; |
| 160 | |
| 161 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 162 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 163 | list_for_each_entry(proto, &protocols, list) { |
| 164 | if (proto->enable_all) { |
| 165 | err |= proto->enable_all(proto); |
| 166 | } |
| 167 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 168 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 169 | return err; |
| 170 | } |
| 171 | |
| 172 | /* disables all listeners of all registered protocols. This may be used before |
| 173 | * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE, |
| 174 | * ERR_RETRYABLE, ERR_FATAL. |
| 175 | */ |
| 176 | int protocol_disable_all(void) |
| 177 | { |
| 178 | struct protocol *proto; |
| 179 | int err; |
| 180 | |
| 181 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 182 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 183 | list_for_each_entry(proto, &protocols, list) { |
| 184 | if (proto->disable_all) { |
| 185 | err |= proto->disable_all(proto); |
| 186 | } |
| 187 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 188 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 189 | return err; |
| 190 | } |
| 191 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 192 | /* |
| 193 | * Local variables: |
| 194 | * c-indent-level: 8 |
| 195 | * c-basic-offset: 8 |
| 196 | * End: |
| 197 | */ |