Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Protocol registration functions. |
| 3 | * |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 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 | |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <common/config.h> |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 17 | #include <common/errors.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 18 | #include <common/mini-clist.h> |
| 19 | #include <common/standard.h> |
| 20 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 21 | #include <proto/fd.h> |
| 22 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 23 | /* List head of all registered protocols */ |
| 24 | static struct list protocols = LIST_HEAD_INIT(protocols); |
| 25 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 26 | /* This function adds the specified listener's file descriptor to the polling |
| 27 | * lists if it is in the LI_LISTEN state. The listener enters LI_READY or |
| 28 | * LI_FULL state depending on its number of connections. |
| 29 | */ |
| 30 | void enable_listener(struct listener *listener) |
| 31 | { |
| 32 | if (listener->state == LI_LISTEN) { |
| 33 | if (listener->nbconn < listener->maxconn) { |
| 34 | EV_FD_SET(listener->fd, DIR_RD); |
| 35 | listener->state = LI_READY; |
| 36 | } else { |
| 37 | listener->state = LI_FULL; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /* This function removes the specified listener's file descriptor from the |
| 43 | * polling lists if it is in the LI_READY or in the LI_FULL state. The listener |
| 44 | * enters LI_LISTEN. |
| 45 | */ |
| 46 | void disable_listener(struct listener *listener) |
| 47 | { |
| 48 | if (listener->state < LI_READY) |
| 49 | return; |
| 50 | if (listener->state == LI_READY) |
| 51 | EV_FD_CLR(listener->fd, DIR_RD); |
| 52 | listener->state = LI_LISTEN; |
| 53 | } |
| 54 | |
| 55 | /* This function adds all of the protocol's listener's file descriptors to the |
| 56 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 57 | * used as a protocol's generic enable_all() primitive, for use after the |
| 58 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 59 | * their number of connections. It always returns ERR_NONE. |
| 60 | */ |
| 61 | int enable_all_listeners(struct protocol *proto) |
| 62 | { |
| 63 | struct listener *listener; |
| 64 | |
| 65 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 66 | enable_listener(listener); |
| 67 | return ERR_NONE; |
| 68 | } |
| 69 | |
| 70 | /* This function removes all of the protocol's listener's file descriptors from |
| 71 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 72 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 73 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 74 | */ |
| 75 | int disable_all_listeners(struct protocol *proto) |
| 76 | { |
| 77 | struct listener *listener; |
| 78 | |
| 79 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 80 | disable_listener(listener); |
| 81 | return ERR_NONE; |
| 82 | } |
| 83 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 84 | /* 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 | */ |
| 89 | int unbind_listener(struct listener *listener) |
| 90 | { |
| 91 | if (listener->state == LI_READY) |
| 92 | EV_FD_CLR(listener->fd, DIR_RD); |
| 93 | |
| 94 | if (listener->state >= LI_LISTEN) { |
| 95 | fd_delete(listener->fd); |
| 96 | listener->state = LI_ASSIGNED; |
| 97 | } |
| 98 | return ERR_NONE; |
| 99 | } |
| 100 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 101 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 102 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 103 | * detach them from the protocol. It always returns ERR_NONE. |
| 104 | */ |
| 105 | int unbind_all_listeners(struct protocol *proto) |
| 106 | { |
| 107 | struct listener *listener; |
| 108 | |
| 109 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 110 | unbind_listener(listener); |
| 111 | return ERR_NONE; |
| 112 | } |
| 113 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 114 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 115 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
| 116 | * number of listeners is updated. Note that the listener must have previously |
| 117 | * been unbound. This is the generic function to use to remove a listener. |
| 118 | */ |
| 119 | void delete_listener(struct listener *listener) |
| 120 | { |
| 121 | if (listener->state != LI_ASSIGNED) |
| 122 | return; |
| 123 | listener->state = LI_INIT; |
| 124 | LIST_DEL(&listener->proto_list); |
| 125 | listener->proto->nb_listeners--; |
| 126 | } |
| 127 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 128 | /* Registers the protocol <proto> */ |
| 129 | void protocol_register(struct protocol *proto) |
| 130 | { |
| 131 | LIST_ADDQ(&protocols, &proto->list); |
| 132 | } |
| 133 | |
| 134 | /* Unregisters the protocol <proto>. Note that all listeners must have |
| 135 | * previously been unbound. |
| 136 | */ |
| 137 | void protocol_unregister(struct protocol *proto) |
| 138 | { |
| 139 | LIST_DEL(&proto->list); |
| 140 | LIST_INIT(&proto->list); |
| 141 | } |
| 142 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 143 | /* binds all listeners of all registered protocols. Returns a composition |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 144 | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 145 | */ |
| 146 | int protocol_bind_all(void) |
| 147 | { |
| 148 | struct protocol *proto; |
| 149 | int err; |
| 150 | |
| 151 | err = 0; |
| 152 | list_for_each_entry(proto, &protocols, list) { |
| 153 | if (proto->bind_all) |
| 154 | err |= proto->bind_all(proto); |
| 155 | } |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | /* unbinds all listeners of all registered protocols. They are also closed. |
| 160 | * This must be performed before calling exit() in order to get a chance to |
| 161 | * remove file-system based sockets and pipes. |
| 162 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 163 | */ |
| 164 | int protocol_unbind_all(void) |
| 165 | { |
| 166 | struct protocol *proto; |
| 167 | int err; |
| 168 | |
| 169 | err = 0; |
| 170 | list_for_each_entry(proto, &protocols, list) { |
| 171 | if (proto->unbind_all) |
| 172 | err |= proto->unbind_all(proto); |
| 173 | } |
| 174 | return err; |
| 175 | } |
| 176 | |
| 177 | /* enables all listeners of all registered protocols. This is intended to be |
| 178 | * used after a fork() to enable reading on all file descriptors. Returns a |
| 179 | * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 180 | */ |
| 181 | int protocol_enable_all(void) |
| 182 | { |
| 183 | struct protocol *proto; |
| 184 | int err; |
| 185 | |
| 186 | err = 0; |
| 187 | list_for_each_entry(proto, &protocols, list) { |
| 188 | if (proto->enable_all) |
| 189 | err |= proto->enable_all(proto); |
| 190 | } |
| 191 | return err; |
| 192 | } |
| 193 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 194 | /* disables all listeners of all registered protocols. This may be used before |
| 195 | * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE, |
| 196 | * ERR_RETRYABLE, ERR_FATAL. |
| 197 | */ |
| 198 | int protocol_disable_all(void) |
| 199 | { |
| 200 | struct protocol *proto; |
| 201 | int err; |
| 202 | |
| 203 | err = 0; |
| 204 | list_for_each_entry(proto, &protocols, list) { |
| 205 | if (proto->disable_all) |
| 206 | err |= proto->disable_all(proto); |
| 207 | } |
| 208 | return err; |
| 209 | } |
| 210 | |