blob: 2140ae6cae18da79a70bf4c01bcfa4faf77fc436 [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
Willy Tarreau92fb9832007-10-16 17:34:28 +020022#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
27#include <common/compat.h>
28#include <common/config.h>
29#include <common/debug.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010030#include <common/errors.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020031#include <common/mini-clist.h>
32#include <common/standard.h>
33#include <common/time.h>
34#include <common/version.h>
35
Willy Tarreau92fb9832007-10-16 17:34:28 +020036#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020037
Willy Tarreau92fb9832007-10-16 17:34:28 +020038#include <proto/fd.h>
39#include <proto/log.h>
40#include <proto/protocols.h>
41#include <proto/proto_uxst.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020042#include <proto/stream_sock.h>
43#include <proto/task.h>
44
Emeric Bruncf20bf12010-10-22 16:06:11 +020045static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
46static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010047static int uxst_unbind_listeners(struct protocol *proto);
48
49/* Note: must not be declared <const> as its list will be overwritten */
50static struct protocol proto_unix = {
51 .name = "unix_stream",
52 .sock_domain = PF_UNIX,
53 .sock_type = SOCK_STREAM,
54 .sock_prot = 0,
55 .sock_family = AF_UNIX,
56 .sock_addrlen = sizeof(struct sockaddr_un),
57 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaueb472682010-05-28 18:46:57 +020058 .accept = &stream_sock_accept,
Emeric Bruncf20bf12010-10-22 16:06:11 +020059 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010060 .bind_all = uxst_bind_listeners,
61 .unbind_all = uxst_unbind_listeners,
62 .enable_all = enable_all_listeners,
63 .disable_all = disable_all_listeners,
64 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
65 .nb_listeners = 0,
66};
67
Willy Tarreaudabf2e22007-10-28 21:59:24 +010068/********************************
69 * 1) low-level socket functions
70 ********************************/
71
72
Cyril Bonté1f5848a2010-11-14 17:03:19 +010073/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
74 * anymore. It practises best effort, and no error is returned.
75 */
76static void destroy_uxst_socket(const char *path)
77{
78 struct sockaddr_un addr;
79 int sock, ret;
80
81 /* We might have been chrooted, so we may not be able to access the
82 * socket. In order to avoid bothering the other end, we connect with a
83 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
84 * is enough to know if the socket is still live or not. If it's live
85 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
86 * ECONNREFUSED. In this case, we do not touch it because it's used
87 * by some other process.
88 */
89 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
90 if (sock < 0)
91 return;
92
93 addr.sun_family = AF_UNIX;
94 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
95 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
96 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
97 if (ret < 0 && errno == ECONNREFUSED) {
98 /* Connect failed: the socket still exists but is not used
99 * anymore. Let's remove this socket now.
100 */
101 unlink(path);
102 }
103 close(sock);
104}
105
106
107/********************************
108 * 2) listener-oriented functions
109 ********************************/
110
111
112/* This function creates a UNIX socket associated to the listener. It changes
113 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
114 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200115 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100116static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200117{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100118 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200119 char tempname[MAXPATHLEN];
120 char backname[MAXPATHLEN];
121 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100122 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100123 const char *path;
124
125 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200126
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100127 /* ensure we never return garbage */
128 if (errmsg && errlen)
129 *errmsg = 0;
130
131 if (listener->state != LI_ASSIGNED)
132 return ERR_NONE; /* already bound */
133
134 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200135
136 /* 1. create socket names */
137 if (!path[0]) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100138 msg = "Invalid empty name for a UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200139 goto err_return;
140 }
141
142 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
143 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100144 msg = "name too long for UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200145 goto err_return;
146 }
147
148 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
149 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100150 msg = "name too long for UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200151 goto err_return;
152 }
153
154 /* 2. clean existing orphaned entries */
155 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100156 msg = "error when trying to unlink previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200157 goto err_return;
158 }
159
160 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100161 msg = "error when trying to unlink previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200162 goto err_return;
163 }
164
165 /* 3. backup existing socket */
166 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100167 msg = "error when trying to preserve previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200168 goto err_return;
169 }
170
171 /* 4. prepare new socket */
172 addr.sun_family = AF_UNIX;
173 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
174 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
175
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100176 fd = socket(PF_UNIX, SOCK_STREAM, 0);
177 if (fd < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100178 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200179 goto err_unlink_back;
180 }
181
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100182 if (fd >= global.maxsock) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100183 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200184 goto err_unlink_temp;
185 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100186
187 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100188 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200189 goto err_unlink_temp;
190 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100191
192 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200193 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreaub40dc942010-11-07 12:10:51 +0100194 msg = "cannot bind UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200195 goto err_unlink_temp;
196 }
197
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100198 /* <uid> and <gid> different of -1 will be used to change the socket owner.
199 * If <mode> is not 0, it will be used to restrict access to the socket.
200 * While it is known not to be portable on every OS, it's still useful
201 * where it works.
202 */
203 if (((listener->perm.ux.uid != -1 || listener->perm.ux.gid != -1) &&
204 (chown(tempname, listener->perm.ux.uid, listener->perm.ux.gid) == -1)) ||
205 (listener->perm.ux.mode != 0 && chmod(tempname, listener->perm.ux.mode) == -1)) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100206 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200207 goto err_unlink_temp;
208 }
209
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100210 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100211 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200212 goto err_unlink_temp;
213 }
214
215 /* 5. install.
216 * Point of no return: we are ready, we'll switch the sockets. We don't
217 * fear loosing the socket <path> because we have a copy of it in
218 * backname.
219 */
220 if (rename(tempname, path) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100221 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200222 goto err_rename;
223 }
224
225 /* 6. cleanup */
226 unlink(backname); /* no need to keep this one either */
227
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100228 /* the socket is now listening */
229 listener->fd = fd;
230 listener->state = LI_LISTEN;
231
232 /* the function for the accept() event */
233 fd_insert(fd);
Willy Tarreaueb472682010-05-28 18:46:57 +0200234 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100235 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
236 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200237 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100238 fdtab[fd].state = FD_STLISTEN;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200239 fdinfo[fd].peeraddr = NULL;
240 fdinfo[fd].peerlen = 0;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100241 return ERR_NONE;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100242 err_rename:
243 ret = rename(backname, path);
244 if (ret < 0 && errno == ENOENT)
245 unlink(path);
246 err_unlink_temp:
247 unlink(tempname);
248 close(fd);
249 err_unlink_back:
250 unlink(backname);
251 err_return:
252 if (msg && errlen)
253 snprintf(errmsg, errlen, "%s [%s]", msg, path);
254 return ERR_FATAL | ERR_ALERT;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100255}
256
257/* This function closes the UNIX sockets for the specified listener.
258 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
259 */
260static int uxst_unbind_listener(struct listener *listener)
261{
Willy Tarreaube58c382011-07-24 18:28:10 +0200262 if (listener->state > LI_ASSIGNED) {
263 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100264 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
265 }
266 return ERR_NONE;
267}
268
269/* Add a listener to the list of unix stream listeners. The listener's state
270 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
271 * listeners is updated. This is the function to use to add a new listener.
272 */
273void uxst_add_listener(struct listener *listener)
274{
275 if (listener->state != LI_INIT)
276 return;
277 listener->state = LI_ASSIGNED;
278 listener->proto = &proto_unix;
279 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
280 proto_unix.nb_listeners++;
281}
282
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100283/********************************
284 * 3) protocol-oriented functions
285 ********************************/
286
287
Willy Tarreau92fb9832007-10-16 17:34:28 +0200288/* This function creates all UNIX sockets bound to the protocol entry <proto>.
289 * It is intended to be used as the protocol's bind_all() function.
290 * The sockets will be registered but not added to any fd_set, in order not to
291 * loose them across the fork(). A call to uxst_enable_listeners() is needed
292 * to complete initialization.
293 *
294 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
295 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200296static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200297{
298 struct listener *listener;
299 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200300
301 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200302 err |= uxst_bind_listener(listener, errmsg, errlen);
303 if (err & ERR_ABORT)
304 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200305 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200306 return err;
307}
308
Willy Tarreau92fb9832007-10-16 17:34:28 +0200309
310/* This function stops all listening UNIX sockets bound to the protocol
311 * <proto>. It does not detaches them from the protocol.
312 * It always returns ERR_NONE.
313 */
314static int uxst_unbind_listeners(struct protocol *proto)
315{
316 struct listener *listener;
317
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100318 list_for_each_entry(listener, &proto->listeners, proto_list)
319 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200320 return ERR_NONE;
321}
322
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100323
324/********************************
325 * 4) high-level functions
326 ********************************/
327
Willy Tarreau92fb9832007-10-16 17:34:28 +0200328__attribute__((constructor))
329static void __uxst_protocol_init(void)
330{
331 protocol_register(&proto_unix);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200332}
333
334
335/*
336 * Local variables:
337 * c-indent-level: 8
338 * c-basic-offset: 8
339 * End:
340 */