blob: 43cbe5a92053eea7e20825d333b2ff2a699f7994 [file] [log] [blame]
Emeric Brun8af3bb02021-03-18 16:52:17 +01001/*
2 * DGRAM protocol layer on top of AF_UNIX
3 *
4 * Copyright 2020 HAProxy Technologies, Emeric Brun <ebrun@haproxy.com>
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 <ctype.h>
14#include <errno.h>
Emeric Brun8af3bb02021-03-18 16:52:17 +010015#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <time.h>
19
20#include <sys/param.h>
21#include <sys/socket.h>
22#include <sys/types.h>
23#include <sys/un.h>
24
25#include <haproxy/fd.h>
26#include <haproxy/listener.h>
27#include <haproxy/log.h>
28#include <haproxy/namespace.h>
29#include <haproxy/protocol.h>
30#include <haproxy/sock.h>
31#include <haproxy/sock_unix.h>
Aurelien DARRAGONde63efb2023-02-06 19:23:40 +010032#include <haproxy/tools.h>
Emeric Brun8af3bb02021-03-18 16:52:17 +010033
34static int uxdg_bind_listener(struct listener *listener, char *errmsg, int errlen);
35static void uxdg_enable_listener(struct listener *listener);
36static void uxdg_disable_listener(struct listener *listener);
37static int uxdg_suspend_receiver(struct receiver *rx);
38
39/* Note: must not be declared <const> as its list will be overwritten */
40struct protocol proto_uxdg = {
41 .name = "uxdg",
42
43 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +020044 .xprt_type = PROTO_TYPE_DGRAM,
Emeric Brun8af3bb02021-03-18 16:52:17 +010045 .listen = uxdg_bind_listener,
46 .enable = uxdg_enable_listener,
47 .disable = uxdg_disable_listener,
48 .add = default_add_listener,
49 .unbind = default_unbind_listener,
50 .suspend = default_suspend_listener,
51 .resume = default_resume_listener,
52
53 /* binding layer */
54 .rx_suspend = uxdg_suspend_receiver,
55
56 /* address family */
57 .fam = &proto_fam_unix,
58
59 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020060 .proto_type = PROTO_TYPE_DGRAM,
Emeric Brun8af3bb02021-03-18 16:52:17 +010061 .sock_type = SOCK_DGRAM,
62 .sock_prot = 0,
63 .rx_enable = sock_enable,
64 .rx_disable = sock_disable,
65 .rx_unbind = sock_unbind,
66 .receivers = LIST_HEAD_INIT(proto_uxdg.receivers),
67 .nb_receivers = 0,
68};
69
70INITCALL1(STG_REGISTER, protocol_register, &proto_uxdg);
71
72/* This function tries to bind dgram unix socket listener. It may return a warning or
73 * an error message in <errmsg> if the message is at most <errlen> bytes long
74 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
75 * The return value is composed from ERR_ABORT, ERR_WARN,
76 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
77 * was alright and that no message was returned. ERR_RETRYABLE means that an
78 * error occurred but that it may vanish after a retry (eg: port in use), and
79 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
80 * the meaning of the error, but just indicate that a message is present which
81 * should be displayed with the respective level. Last, ERR_ABORT indicates
82 * that it's pointless to try to start other listeners. No error message is
83 * returned if errlen is NULL.
84 */
85int uxdg_bind_listener(struct listener *listener, char *errmsg, int errlen)
86{
87 int err = ERR_NONE;
88 char *msg = NULL;
89
90 /* ensure we never return garbage */
91 if (errlen)
92 *errmsg = 0;
93
94 if (listener->state != LI_ASSIGNED)
95 return ERR_NONE; /* already bound */
96
97 if (!(listener->rx.flags & RX_F_BOUND)) {
98 msg = "receiving socket not bound";
Aurelien DARRAGONd861dc92023-02-06 18:50:51 +010099 err |= ERR_FATAL | ERR_ALERT;
Emeric Brun8af3bb02021-03-18 16:52:17 +0100100 goto uxdg_return;
101 }
102
103 listener_set_state(listener, LI_LISTEN);
104
105 uxdg_return:
106 if (msg && errlen) {
Aurelien DARRAGONde63efb2023-02-06 19:23:40 +0100107 char *path_str;
108
109 path_str = sa2str((struct sockaddr_storage *)&listener->rx.addr, 0, 0);
110 snprintf(errmsg, errlen, "%s for [%s]", msg, ((path_str) ? path_str : ""));
111 ha_free(&path_str);
Emeric Brun8af3bb02021-03-18 16:52:17 +0100112 }
113 return err;
114}
115
116/* Enable receipt of incoming connections for listener <l>. The receiver must
117 * still be valid.
118 */
119static void uxdg_enable_listener(struct listener *l)
120{
121 fd_want_recv_safe(l->rx.fd);
122}
123
124/* Disable receipt of incoming connections for listener <l>. The receiver must
125 * still be valid.
126 */
127static void uxdg_disable_listener(struct listener *l)
128{
129 fd_stop_recv(l->rx.fd);
130}
131
132/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
Aurelien DARRAGON2338dba2023-02-22 14:34:28 +0100133 * was totally stopped, or > 0 if correctly suspended. For plain unix sockets
134 * we only disable the listener to prevent data from being handled but nothing
135 * more is done since currently it's the new process which handles the renaming.
136 * Abstract sockets are completely unbound and closed so there's no need to stop
137 * the poller.
Emeric Brun8af3bb02021-03-18 16:52:17 +0100138 */
139static int uxdg_suspend_receiver(struct receiver *rx)
140{
141 struct listener *l = LIST_ELEM(rx, struct listener *, rx);
142
Aurelien DARRAGON2338dba2023-02-22 14:34:28 +0100143 if (((struct sockaddr_un *)&rx->addr)->sun_path[0]) {
144 uxdg_disable_listener(l);
Emeric Brun8af3bb02021-03-18 16:52:17 +0100145 return 1;
Aurelien DARRAGON2338dba2023-02-22 14:34:28 +0100146 }
Emeric Brun8af3bb02021-03-18 16:52:17 +0100147
148 /* Listener's lock already held. Call lockless version of
149 * unbind_listener. */
150 do_unbind_listener(l);
151 return 0;
152}
153
154/*
155 * Local variables:
156 * c-indent-level: 8
157 * c-basic-offset: 8
158 * End:
159 */