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