blob: 3fa383decec8a732601f81763fd0a5a52c3bd322 [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>
Willy Tarreaud0a895d2012-09-18 17:40:35 +020016#include <pwd.h>
17#include <grp.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <syslog.h>
22#include <time.h>
23
Willy Tarreau92fb9832007-10-16 17:34:28 +020024#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/un.h>
28
29#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010032#include <common/errors.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020033#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/time.h>
36#include <common/version.h>
37
Willy Tarreau92fb9832007-10-16 17:34:28 +020038#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020039
Willy Tarreau47f48c42014-05-09 22:57:47 +020040#include <proto/connection.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020041#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020042#include <proto/listener.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020043#include <proto/log.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020044#include <proto/protocol.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020045#include <proto/proto_uxst.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020046#include <proto/task.h>
47
Emeric Bruncf20bf12010-10-22 16:06:11 +020048static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
49static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010050static int uxst_unbind_listeners(struct protocol *proto);
Willy Tarreau47f48c42014-05-09 22:57:47 +020051static int uxst_connect_server(struct connection *conn, int data, int delack);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010052
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 Tarreaubbebbbf2012-05-07 21:22:09 +020062 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020063 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020064 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010065 .bind_all = uxst_bind_listeners,
66 .unbind_all = uxst_unbind_listeners,
67 .enable_all = enable_all_listeners,
68 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020069 .get_src = uxst_get_src,
70 .get_dst = uxst_get_dst,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010071 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
72 .nb_listeners = 0,
73};
74
Willy Tarreaudabf2e22007-10-28 21:59:24 +010075/********************************
76 * 1) low-level socket functions
77 ********************************/
78
Willy Tarreau59b94792012-05-11 16:16:40 +020079/*
80 * Retrieves the source address for the socket <fd>, with <dir> indicating
81 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
82 * success, -1 in case of error. The socket's source address is stored in
83 * <sa> for <salen> bytes.
84 */
85int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
86{
87 if (dir)
88 return getsockname(fd, sa, &salen);
89 else
90 return getpeername(fd, sa, &salen);
91}
92
93
94/*
95 * Retrieves the original destination address for the socket <fd>, with <dir>
96 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
97 * case of success, -1 in case of error. The socket's source address is stored
98 * in <sa> for <salen> bytes.
99 */
100int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
101{
102 if (dir)
103 return getpeername(fd, sa, &salen);
104 else
105 return getsockname(fd, sa, &salen);
106}
107
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100108
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100109/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
110 * anymore. It practises best effort, and no error is returned.
111 */
112static void destroy_uxst_socket(const char *path)
113{
114 struct sockaddr_un addr;
115 int sock, ret;
116
Willy Tarreau40aa0702013-03-10 23:51:38 +0100117 /* if the path was cleared, we do nothing */
118 if (!*path)
119 return;
120
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100121 /* We might have been chrooted, so we may not be able to access the
122 * socket. In order to avoid bothering the other end, we connect with a
123 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
124 * is enough to know if the socket is still live or not. If it's live
125 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
126 * ECONNREFUSED. In this case, we do not touch it because it's used
127 * by some other process.
128 */
129 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
130 if (sock < 0)
131 return;
132
133 addr.sun_family = AF_UNIX;
134 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
135 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
136 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
137 if (ret < 0 && errno == ECONNREFUSED) {
138 /* Connect failed: the socket still exists but is not used
139 * anymore. Let's remove this socket now.
140 */
141 unlink(path);
142 }
143 close(sock);
144}
145
146
147/********************************
148 * 2) listener-oriented functions
149 ********************************/
150
151
152/* This function creates a UNIX socket associated to the listener. It changes
153 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100154 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
155 * may return a warning or an error message in <errmsg> if the message is at
156 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
157 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200158 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100159static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200160{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100161 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200162 char tempname[MAXPATHLEN];
163 char backname[MAXPATHLEN];
164 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100165 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100166 const char *path;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100167 int ext, ready;
168 socklen_t ready_len;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100169
170 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200171
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100172 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100173 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100174 *errmsg = 0;
175
176 if (listener->state != LI_ASSIGNED)
177 return ERR_NONE; /* already bound */
178
179 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200180
Willy Tarreau40aa0702013-03-10 23:51:38 +0100181 /* if the listener already has an fd assigned, then we were offered the
182 * fd by an external process (most likely the parent), and we don't want
183 * to create a new socket. However we still want to set a few flags on
184 * the socket.
185 */
186 fd = listener->fd;
187 ext = (fd >= 0);
188 if (ext)
189 goto fd_ready;
190
Willy Tarreau92fb9832007-10-16 17:34:28 +0200191 /* 1. create socket names */
192 if (!path[0]) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100193 msg = "Invalid empty name for a UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200194 goto err_return;
195 }
196
197 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
198 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100199 msg = "name too long for UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200200 goto err_return;
201 }
202
203 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
204 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100205 msg = "name too long for UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200206 goto err_return;
207 }
208
209 /* 2. clean existing orphaned entries */
210 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100211 msg = "error when trying to unlink previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200212 goto err_return;
213 }
214
215 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100216 msg = "error when trying to unlink previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200217 goto err_return;
218 }
219
220 /* 3. backup existing socket */
221 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100222 msg = "error when trying to preserve previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200223 goto err_return;
224 }
225
226 /* 4. prepare new socket */
227 addr.sun_family = AF_UNIX;
228 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
229 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
230
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100231 fd = socket(PF_UNIX, SOCK_STREAM, 0);
232 if (fd < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100233 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200234 goto err_unlink_back;
235 }
236
Willy Tarreau40aa0702013-03-10 23:51:38 +0100237 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100238 if (fd >= global.maxsock) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100239 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200240 goto err_unlink_temp;
241 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100242
243 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100244 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200245 goto err_unlink_temp;
246 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100247
Willy Tarreau40aa0702013-03-10 23:51:38 +0100248 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200249 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreaub40dc942010-11-07 12:10:51 +0100250 msg = "cannot bind UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200251 goto err_unlink_temp;
252 }
253
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100254 /* <uid> and <gid> different of -1 will be used to change the socket owner.
255 * If <mode> is not 0, it will be used to restrict access to the socket.
256 * While it is known not to be portable on every OS, it's still useful
257 * where it works.
258 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100259 if (!ext &&
260 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
261 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
262 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100263 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200264 goto err_unlink_temp;
265 }
266
Willy Tarreau40aa0702013-03-10 23:51:38 +0100267 ready = 0;
268 ready_len = sizeof(ready);
269 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
270 ready = 0;
271
272 if (!(ext && ready) && /* only listen if not already done by external process */
273 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100274 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200275 goto err_unlink_temp;
276 }
277
278 /* 5. install.
279 * Point of no return: we are ready, we'll switch the sockets. We don't
280 * fear loosing the socket <path> because we have a copy of it in
281 * backname.
282 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100283 if (!ext && rename(tempname, path) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100284 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200285 goto err_rename;
286 }
287
Willy Tarreau40aa0702013-03-10 23:51:38 +0100288 /* 6. cleanup. If we're bound to an fd inherited from the parent, we
289 * want to ensure that destroy_uxst_socket() will never remove the
290 * path, and for this we simply clear the path to the socket.
291 */
292 if (!ext)
293 unlink(backname);
294 else
295 ((struct sockaddr_un *)&listener->addr)->sun_path[0] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200296
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100297 /* the socket is now listening */
298 listener->fd = fd;
299 listener->state = LI_LISTEN;
300
301 /* the function for the accept() event */
302 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200303 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200304 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100305 return ERR_NONE;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100306 err_rename:
307 ret = rename(backname, path);
308 if (ret < 0 && errno == ENOENT)
309 unlink(path);
310 err_unlink_temp:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100311 if (!ext)
312 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100313 close(fd);
314 err_unlink_back:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100315 if (!ext)
316 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100317 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100318 if (msg && errlen) {
319 if (!ext)
320 snprintf(errmsg, errlen, "%s [%s]", msg, path);
321 else
322 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
323 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100324 return ERR_FATAL | ERR_ALERT;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100325}
326
327/* This function closes the UNIX sockets for the specified listener.
328 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
329 */
330static int uxst_unbind_listener(struct listener *listener)
331{
Willy Tarreaube58c382011-07-24 18:28:10 +0200332 if (listener->state > LI_ASSIGNED) {
333 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100334 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
335 }
336 return ERR_NONE;
337}
338
339/* Add a listener to the list of unix stream listeners. The listener's state
340 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
341 * listeners is updated. This is the function to use to add a new listener.
342 */
343void uxst_add_listener(struct listener *listener)
344{
345 if (listener->state != LI_INIT)
346 return;
347 listener->state = LI_ASSIGNED;
348 listener->proto = &proto_unix;
349 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
350 proto_unix.nb_listeners++;
351}
352
Willy Tarreau47f48c42014-05-09 22:57:47 +0200353
354/*
355 * This function initiates a UNIX connection establishment to the target assigned
356 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
357 * and will be selected by the system. conn->target may point either to a valid
358 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
359 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
360 * whether there are data waiting for being sent or not, in order to adjust data
361 * write polling and on some platforms. The <delack> argument is ignored.
362 *
363 * Note that a pending send_proxy message accounts for data.
364 *
365 * It can return one of :
366 * - SN_ERR_NONE if everything's OK
367 * - SN_ERR_SRVTO if there are no more servers
368 * - SN_ERR_SRVCL if the connection was refused by the server
369 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
370 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
371 * - SN_ERR_INTERNAL for any other purely internal errors
372 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
373 *
374 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
375 * it's invalid and the caller has nothing to do.
376 */
377int uxst_connect_server(struct connection *conn, int data, int delack)
378{
379 int fd;
380 struct server *srv;
381 struct proxy *be;
382
383 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
384
385 switch (obj_type(conn->target)) {
386 case OBJ_TYPE_PROXY:
387 be = objt_proxy(conn->target);
388 srv = NULL;
389 break;
390 case OBJ_TYPE_SERVER:
391 srv = objt_server(conn->target);
392 be = srv->proxy;
393 break;
394 default:
395 conn->flags |= CO_FL_ERROR;
396 return SN_ERR_INTERNAL;
397 }
398
399 if ((fd = conn->t.sock.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
400 qfprintf(stderr, "Cannot get a server socket.\n");
401
402 if (errno == ENFILE) {
403 conn->err_code = CO_ER_SYS_FDLIM;
404 send_log(be, LOG_EMERG,
405 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
406 be->id, maxfd);
407 }
408 else if (errno == EMFILE) {
409 conn->err_code = CO_ER_PROC_FDLIM;
410 send_log(be, LOG_EMERG,
411 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
412 be->id, maxfd);
413 }
414 else if (errno == ENOBUFS || errno == ENOMEM) {
415 conn->err_code = CO_ER_SYS_MEMLIM;
416 send_log(be, LOG_EMERG,
417 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
418 be->id, maxfd);
419 }
420 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
421 conn->err_code = CO_ER_NOPROTO;
422 }
423 else
424 conn->err_code = CO_ER_SOCK_ERR;
425
426 /* this is a resource error */
427 conn->flags |= CO_FL_ERROR;
428 return SN_ERR_RESOURCE;
429 }
430
431 if (fd >= global.maxsock) {
432 /* do not log anything there, it's a normal condition when this option
433 * is used to serialize connections to a server !
434 */
435 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
436 close(fd);
437 conn->err_code = CO_ER_CONF_FDLIM;
438 conn->flags |= CO_FL_ERROR;
439 return SN_ERR_PRXCOND; /* it is a configuration limit */
440 }
441
442 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
443 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
444 close(fd);
445 conn->err_code = CO_ER_SOCK_ERR;
446 conn->flags |= CO_FL_ERROR;
447 return SN_ERR_INTERNAL;
448 }
449
450 /* if a send_proxy is there, there are data */
451 data |= conn->send_proxy_ofs;
452
453 if (global.tune.server_sndbuf)
454 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
455
456 if (global.tune.server_rcvbuf)
457 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
458
459 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
460 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
461 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
462 char *msg;
463 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
464 msg = "no free ports";
465 conn->err_code = CO_ER_FREE_PORTS;
466 }
467 else {
468 msg = "local address already in use";
469 conn->err_code = CO_ER_ADDR_INUSE;
470 }
471
472 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
473 close(fd);
474 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
475 conn->flags |= CO_FL_ERROR;
476 return SN_ERR_RESOURCE;
477 }
478 else if (errno == ETIMEDOUT) {
479 close(fd);
480 conn->err_code = CO_ER_SOCK_ERR;
481 conn->flags |= CO_FL_ERROR;
482 return SN_ERR_SRVTO;
483 }
484 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
485 close(fd);
486 conn->err_code = CO_ER_SOCK_ERR;
487 conn->flags |= CO_FL_ERROR;
488 return SN_ERR_SRVCL;
489 }
490 }
491
492 conn->flags |= CO_FL_ADDR_TO_SET;
493
494 /* Prepare to send a few handshakes related to the on-wire protocol. */
495 if (conn->send_proxy_ofs)
496 conn->flags |= CO_FL_SEND_PROXY;
497
498 conn_ctrl_init(conn); /* registers the FD */
499 fdtab[fd].linger_risk = 1; /* close hard if needed */
500 conn_sock_want_send(conn); /* for connect status */
501
502 if (conn_xprt_init(conn) < 0) {
503 conn_force_close(conn);
504 conn->flags |= CO_FL_ERROR;
505 return SN_ERR_RESOURCE;
506 }
507
508 if (data)
509 conn_data_want_send(conn); /* prepare to send data if any */
510
511 return SN_ERR_NONE; /* connection is OK */
512}
513
514
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100515/********************************
516 * 3) protocol-oriented functions
517 ********************************/
518
519
Willy Tarreau92fb9832007-10-16 17:34:28 +0200520/* This function creates all UNIX sockets bound to the protocol entry <proto>.
521 * It is intended to be used as the protocol's bind_all() function.
522 * The sockets will be registered but not added to any fd_set, in order not to
523 * loose them across the fork(). A call to uxst_enable_listeners() is needed
524 * to complete initialization.
525 *
526 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
527 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200528static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200529{
530 struct listener *listener;
531 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200532
533 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200534 err |= uxst_bind_listener(listener, errmsg, errlen);
535 if (err & ERR_ABORT)
536 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200537 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200538 return err;
539}
540
Willy Tarreau92fb9832007-10-16 17:34:28 +0200541
542/* This function stops all listening UNIX sockets bound to the protocol
543 * <proto>. It does not detaches them from the protocol.
544 * It always returns ERR_NONE.
545 */
546static int uxst_unbind_listeners(struct protocol *proto)
547{
548 struct listener *listener;
549
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100550 list_for_each_entry(listener, &proto->listeners, proto_list)
551 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200552 return ERR_NONE;
553}
554
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200555/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200556static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200557{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200558 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200559 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200560 return ERR_ALERT | ERR_FATAL;
561 }
562
Willy Tarreau290e63a2012-09-20 18:07:14 +0200563 conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200564 return 0;
565}
566
567/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200568static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200569{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200570 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200571 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200572 return ERR_ALERT | ERR_FATAL;
573 }
574
Willy Tarreau290e63a2012-09-20 18:07:14 +0200575 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200576 return 0;
577}
578
579/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200580static int bind_parse_group(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200581{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200582 struct group *group;
583
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200584 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200585 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200586 return ERR_ALERT | ERR_FATAL;
587 }
588
589 group = getgrnam(args[cur_arg + 1]);
590 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200591 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200592 return ERR_ALERT | ERR_FATAL;
593 }
594
Willy Tarreau290e63a2012-09-20 18:07:14 +0200595 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200596 return 0;
597}
598
599/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200600static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200601{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200602 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200603 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200604 return ERR_ALERT | ERR_FATAL;
605 }
606
Willy Tarreau290e63a2012-09-20 18:07:14 +0200607 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200608 return 0;
609}
610
611/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200612static int bind_parse_user(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200613{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200614 struct passwd *user;
615
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200616 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200617 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200618 return ERR_ALERT | ERR_FATAL;
619 }
620
621 user = getpwnam(args[cur_arg + 1]);
622 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200623 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200624 return ERR_ALERT | ERR_FATAL;
625 }
626
Willy Tarreau290e63a2012-09-20 18:07:14 +0200627 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200628 return 0;
629}
630
631/* Note: must not be declared <const> as its list will be overwritten.
632 * Please take care of keeping this list alphabetically sorted, doing so helps
633 * all code contributors.
634 * Optional keywords are also declared with a NULL ->parse() function so that
635 * the config parser can report an appropriate error when a known keyword was
636 * not enabled.
637 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200638static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200639 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
640 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
641 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
642 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
643 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
644 { NULL, NULL, 0 },
645}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100646
647/********************************
648 * 4) high-level functions
649 ********************************/
650
Willy Tarreau92fb9832007-10-16 17:34:28 +0200651__attribute__((constructor))
652static void __uxst_protocol_init(void)
653{
654 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200655 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200656}
657
658
659/*
660 * Local variables:
661 * c-indent-level: 8
662 * c-basic-offset: 8
663 * End:
664 */