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 | |
| 13 | #include <common/config.h> |
| 14 | #include <common/errors.h> |
| 15 | #include <common/mini-clist.h> |
| 16 | #include <common/standard.h> |
| 17 | |
| 18 | #include <types/protocol.h> |
| 19 | |
| 20 | /* List head of all registered protocols */ |
| 21 | static struct list protocols = LIST_HEAD_INIT(protocols); |
| 22 | |
| 23 | /* Registers the protocol <proto> */ |
| 24 | void protocol_register(struct protocol *proto) |
| 25 | { |
| 26 | LIST_ADDQ(&protocols, &proto->list); |
| 27 | } |
| 28 | |
| 29 | /* Unregisters the protocol <proto>. Note that all listeners must have |
| 30 | * previously been unbound. |
| 31 | */ |
| 32 | void protocol_unregister(struct protocol *proto) |
| 33 | { |
| 34 | LIST_DEL(&proto->list); |
| 35 | LIST_INIT(&proto->list); |
| 36 | } |
| 37 | |
| 38 | /* binds all listeners of all registered protocols. Returns a composition |
| 39 | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 40 | */ |
| 41 | int protocol_bind_all(char *errmsg, int errlen) |
| 42 | { |
| 43 | struct protocol *proto; |
| 44 | int err; |
| 45 | |
| 46 | err = 0; |
| 47 | list_for_each_entry(proto, &protocols, list) { |
| 48 | if (proto->bind_all) { |
| 49 | err |= proto->bind_all(proto, errmsg, errlen); |
| 50 | if ( err & ERR_ABORT ) |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | return err; |
| 55 | } |
| 56 | |
| 57 | /* unbinds all listeners of all registered protocols. They are also closed. |
| 58 | * This must be performed before calling exit() in order to get a chance to |
| 59 | * remove file-system based sockets and pipes. |
| 60 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT. |
| 61 | */ |
| 62 | int protocol_unbind_all(void) |
| 63 | { |
| 64 | struct protocol *proto; |
| 65 | int err; |
| 66 | |
| 67 | err = 0; |
| 68 | list_for_each_entry(proto, &protocols, list) { |
| 69 | if (proto->unbind_all) { |
| 70 | err |= proto->unbind_all(proto); |
| 71 | } |
| 72 | } |
| 73 | return err; |
| 74 | } |
| 75 | |
| 76 | /* enables all listeners of all registered protocols. This is intended to be |
| 77 | * used after a fork() to enable reading on all file descriptors. Returns a |
| 78 | * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 79 | */ |
| 80 | int protocol_enable_all(void) |
| 81 | { |
| 82 | struct protocol *proto; |
| 83 | int err; |
| 84 | |
| 85 | err = 0; |
| 86 | list_for_each_entry(proto, &protocols, list) { |
| 87 | if (proto->enable_all) { |
| 88 | err |= proto->enable_all(proto); |
| 89 | } |
| 90 | } |
| 91 | return err; |
| 92 | } |
| 93 | |
| 94 | /* disables all listeners of all registered protocols. This may be used before |
| 95 | * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE, |
| 96 | * ERR_RETRYABLE, ERR_FATAL. |
| 97 | */ |
| 98 | int protocol_disable_all(void) |
| 99 | { |
| 100 | struct protocol *proto; |
| 101 | int err; |
| 102 | |
| 103 | err = 0; |
| 104 | list_for_each_entry(proto, &protocols, list) { |
| 105 | if (proto->disable_all) { |
| 106 | err |= proto->disable_all(proto); |
| 107 | } |
| 108 | } |
| 109 | return err; |
| 110 | } |
| 111 | |
| 112 | /* Returns the protocol handler for socket family <family> or NULL if not found */ |
| 113 | struct protocol *protocol_by_family(int family) |
| 114 | { |
| 115 | struct protocol *proto; |
| 116 | |
| 117 | list_for_each_entry(proto, &protocols, list) { |
| 118 | if (proto->sock_domain == family) |
| 119 | return proto; |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Local variables: |
| 126 | * c-indent-level: 8 |
| 127 | * c-basic-offset: 8 |
| 128 | * End: |
| 129 | */ |