blob: 98c9555520acfd92a4e84c26847eacbd59fb1ffe [file] [log] [blame]
Willy Tarreau92fb9832007-10-16 17:34:28 +02001/*
2 * UNIX SOCK_STREAM protocol layer (uxst)
3 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau92fb9832007-10-16 17:34:28 +02005 *
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>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21
22#include <sys/param.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/un.h>
27
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010031#include <common/errors.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020032#include <common/mini-clist.h>
33#include <common/standard.h>
34#include <common/time.h>
35#include <common/version.h>
36
Willy Tarreau92fb9832007-10-16 17:34:28 +020037#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020038
Willy Tarreau92fb9832007-10-16 17:34:28 +020039#include <proto/fd.h>
40#include <proto/log.h>
41#include <proto/protocols.h>
42#include <proto/proto_uxst.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020043#include <proto/stream_sock.h>
44#include <proto/task.h>
45
46#ifndef MAXPATHLEN
47#define MAXPATHLEN 128
48#endif
49
Willy Tarreaudabf2e22007-10-28 21:59:24 +010050static int uxst_bind_listeners(struct protocol *proto);
51static int uxst_unbind_listeners(struct protocol *proto);
52
53/* Note: must not be declared <const> as its list will be overwritten */
54static struct protocol proto_unix = {
55 .name = "unix_stream",
56 .sock_domain = PF_UNIX,
57 .sock_type = SOCK_STREAM,
58 .sock_prot = 0,
59 .sock_family = AF_UNIX,
60 .sock_addrlen = sizeof(struct sockaddr_un),
61 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaueb472682010-05-28 18:46:57 +020062 .accept = &stream_sock_accept,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010063 .read = &stream_sock_read,
64 .write = &stream_sock_write,
65 .bind_all = uxst_bind_listeners,
66 .unbind_all = uxst_unbind_listeners,
67 .enable_all = enable_all_listeners,
68 .disable_all = disable_all_listeners,
69 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
70 .nb_listeners = 0,
71};
72
Willy Tarreaudabf2e22007-10-28 21:59:24 +010073/********************************
74 * 1) low-level socket functions
75 ********************************/
76
77
Willy Tarreau92fb9832007-10-16 17:34:28 +020078/* This function creates a named PF_UNIX stream socket at address <path>. Note
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020079 * that the path cannot be NULL nor empty. <uid> and <gid> different of -1 will
80 * be used to change the socket owner. If <mode> is not 0, it will be used to
81 * restrict access to the socket. While it is known not to be portable on every
82 * OS, it's still useful where it works.
Willy Tarreau92fb9832007-10-16 17:34:28 +020083 * It returns the assigned file descriptor, or -1 in the event of an error.
84 */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020085static int create_uxst_socket(const char *path, uid_t uid, gid_t gid, mode_t mode)
Willy Tarreau92fb9832007-10-16 17:34:28 +020086{
87 char tempname[MAXPATHLEN];
88 char backname[MAXPATHLEN];
89 struct sockaddr_un addr;
90
91 int ret, sock;
92
93 /* 1. create socket names */
94 if (!path[0]) {
Willy Tarreau5d536342009-10-14 15:16:48 +020095 Alert("Invalid empty name for a UNIX socket. Aborting.\n");
Willy Tarreau92fb9832007-10-16 17:34:28 +020096 goto err_return;
97 }
98
99 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
100 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200101 Alert("name too long for UNIX socket (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200102 goto err_return;
103 }
104
105 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
106 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200107 Alert("name too long for UNIX socket (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200108 goto err_return;
109 }
110
111 /* 2. clean existing orphaned entries */
112 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200113 Alert("error when trying to unlink previous UNIX socket (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200114 goto err_return;
115 }
116
117 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200118 Alert("error when trying to unlink previous UNIX socket (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200119 goto err_return;
120 }
121
122 /* 3. backup existing socket */
123 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200124 Alert("error when trying to preserve previous UNIX socket (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200125 goto err_return;
126 }
127
128 /* 4. prepare new socket */
129 addr.sun_family = AF_UNIX;
130 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
131 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
132
133 sock = socket(PF_UNIX, SOCK_STREAM, 0);
134 if (sock < 0) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200135 Alert("cannot create socket for UNIX listener (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200136 goto err_unlink_back;
137 }
138
139 if (sock >= global.maxsock) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200140 Alert("socket(): not enough free sockets for UNIX listener (%s). Raise -n argument. Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200141 goto err_unlink_temp;
142 }
143
144 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
145 Alert("cannot make UNIX socket non-blocking. Aborting.\n");
146 goto err_unlink_temp;
147 }
148
149 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
150 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau5d536342009-10-14 15:16:48 +0200151 Alert("cannot bind socket for UNIX listener (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200152 goto err_unlink_temp;
153 }
154
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200155 if (((uid != -1 || gid != -1) && (chown(tempname, uid, gid) == -1)) ||
156 (mode != 0 && chmod(tempname, mode) == -1)) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200157 Alert("cannot change UNIX socket ownership (%s). Aborting.\n", path);
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200158 goto err_unlink_temp;
159 }
160
Willy Tarreau92fb9832007-10-16 17:34:28 +0200161 if (listen(sock, 0) < 0) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200162 Alert("cannot listen to socket for UNIX listener (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200163 goto err_unlink_temp;
164 }
165
166 /* 5. install.
167 * Point of no return: we are ready, we'll switch the sockets. We don't
168 * fear loosing the socket <path> because we have a copy of it in
169 * backname.
170 */
171 if (rename(tempname, path) < 0) {
Willy Tarreau5d536342009-10-14 15:16:48 +0200172 Alert("cannot switch final and temporary sockets for UNIX listener (%s). Aborting.\n", path);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200173 goto err_rename;
174 }
175
176 /* 6. cleanup */
177 unlink(backname); /* no need to keep this one either */
178
179 return sock;
180
181 err_rename:
182 ret = rename(backname, path);
183 if (ret < 0 && errno == ENOENT)
184 unlink(path);
185 err_unlink_temp:
186 unlink(tempname);
187 close(sock);
188 err_unlink_back:
189 unlink(backname);
190 err_return:
191 return -1;
192}
193
194/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
195 * anymore. It practises best effort, and no error is returned.
196 */
197static void destroy_uxst_socket(const char *path)
198{
199 struct sockaddr_un addr;
200 int sock, ret;
201
202 /* We might have been chrooted, so we may not be able to access the
203 * socket. In order to avoid bothering the other end, we connect with a
204 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
205 * is enough to know if the socket is still live or not. If it's live
206 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
207 * ECONNREFUSED. In this case, we do not touch it because it's used
208 * by some other process.
209 */
210 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
211 if (sock < 0)
212 return;
213
214 addr.sun_family = AF_UNIX;
215 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
Willy Tarreau10ae5482007-10-18 16:15:52 +0200216 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200217 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
218 if (ret < 0 && errno == ECONNREFUSED) {
219 /* Connect failed: the socket still exists but is not used
220 * anymore. Let's remove this socket now.
221 */
222 unlink(path);
223 }
224 close(sock);
225}
226
227
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100228/********************************
229 * 2) listener-oriented functions
230 ********************************/
231
232
233/* This function creates the UNIX socket associated to the listener. It changes
234 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
235 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
236 */
237static int uxst_bind_listener(struct listener *listener)
238{
239 int fd;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100240
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100241 if (listener->state != LI_ASSIGNED)
242 return ERR_NONE; /* already bound */
243
244 fd = create_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path,
245 listener->perm.ux.uid,
246 listener->perm.ux.gid,
247 listener->perm.ux.mode);
248 if (fd == -1)
249 return ERR_FATAL;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100250
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100251 /* the socket is now listening */
252 listener->fd = fd;
253 listener->state = LI_LISTEN;
254
255 /* the function for the accept() event */
256 fd_insert(fd);
Willy Tarreaueb472682010-05-28 18:46:57 +0200257 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100258 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
259 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200260 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100261 fdtab[fd].state = FD_STLISTEN;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200262 fdinfo[fd].peeraddr = NULL;
263 fdinfo[fd].peerlen = 0;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100264 return ERR_NONE;
265}
266
267/* This function closes the UNIX sockets for the specified listener.
268 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
269 */
270static int uxst_unbind_listener(struct listener *listener)
271{
272 if (listener->state == LI_READY)
273 EV_FD_CLR(listener->fd, DIR_RD);
274
275 if (listener->state >= LI_LISTEN) {
Willy Tarreau8eebe5e2007-10-28 22:07:08 +0100276 fd_delete(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100277 listener->state = LI_ASSIGNED;
278 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
279 }
280 return ERR_NONE;
281}
282
283/* Add a listener to the list of unix stream listeners. The listener's state
284 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
285 * listeners is updated. This is the function to use to add a new listener.
286 */
287void uxst_add_listener(struct listener *listener)
288{
289 if (listener->state != LI_INIT)
290 return;
291 listener->state = LI_ASSIGNED;
292 listener->proto = &proto_unix;
293 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
294 proto_unix.nb_listeners++;
295}
296
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100297/********************************
298 * 3) protocol-oriented functions
299 ********************************/
300
301
Willy Tarreau92fb9832007-10-16 17:34:28 +0200302/* This function creates all UNIX sockets bound to the protocol entry <proto>.
303 * It is intended to be used as the protocol's bind_all() function.
304 * The sockets will be registered but not added to any fd_set, in order not to
305 * loose them across the fork(). A call to uxst_enable_listeners() is needed
306 * to complete initialization.
307 *
308 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
309 */
310static int uxst_bind_listeners(struct protocol *proto)
311{
312 struct listener *listener;
313 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200314
315 list_for_each_entry(listener, &proto->listeners, proto_list) {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100316 err |= uxst_bind_listener(listener);
317 if (err != ERR_NONE)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200318 continue;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200319 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200320 return err;
321}
322
Willy Tarreau92fb9832007-10-16 17:34:28 +0200323
324/* This function stops all listening UNIX sockets bound to the protocol
325 * <proto>. It does not detaches them from the protocol.
326 * It always returns ERR_NONE.
327 */
328static int uxst_unbind_listeners(struct protocol *proto)
329{
330 struct listener *listener;
331
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100332 list_for_each_entry(listener, &proto->listeners, proto_list)
333 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200334 return ERR_NONE;
335}
336
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100337
338/********************************
339 * 4) high-level functions
340 ********************************/
341
Willy Tarreau92fb9832007-10-16 17:34:28 +0200342__attribute__((constructor))
343static void __uxst_protocol_init(void)
344{
345 protocol_register(&proto_unix);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200346}
347
348
349/*
350 * Local variables:
351 * c-indent-level: 8
352 * c-basic-offset: 8
353 * End:
354 */