blob: f83d34e1198c1bf9ac5d515ad84ab826951df390 [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 Tarreauccfccef2014-05-10 01:49:15 +0200191 if (path[0]) {
192 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
193 if (ret < 0 || ret >= MAXPATHLEN) {
194 msg = "name too long for UNIX socket";
195 goto err_return;
196 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200197
Willy Tarreauccfccef2014-05-10 01:49:15 +0200198 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
199 if (ret < 0 || ret >= MAXPATHLEN) {
200 msg = "name too long for UNIX socket";
201 goto err_return;
202 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200203
Willy Tarreauccfccef2014-05-10 01:49:15 +0200204 /* 2. clean existing orphaned entries */
205 if (unlink(tempname) < 0 && errno != ENOENT) {
206 msg = "error when trying to unlink previous UNIX socket";
207 goto err_return;
208 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200209
Willy Tarreauccfccef2014-05-10 01:49:15 +0200210 if (unlink(backname) < 0 && errno != ENOENT) {
211 msg = "error when trying to unlink previous UNIX socket";
212 goto err_return;
213 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200214
Willy Tarreauccfccef2014-05-10 01:49:15 +0200215 /* 3. backup existing socket */
216 if (link(path, backname) < 0 && errno != ENOENT) {
217 msg = "error when trying to preserve previous UNIX socket";
218 goto err_return;
219 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200220
Willy Tarreauccfccef2014-05-10 01:49:15 +0200221 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
222 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200223 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200224 else {
225 /* first char is zero, it's an abstract socket whose address
226 * is defined by all the bytes past this zero.
227 */
228 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
229 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200230 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200231
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100232 fd = socket(PF_UNIX, SOCK_STREAM, 0);
233 if (fd < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100234 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200235 goto err_unlink_back;
236 }
237
Willy Tarreau40aa0702013-03-10 23:51:38 +0100238 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100239 if (fd >= global.maxsock) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100240 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200241 goto err_unlink_temp;
242 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100243
244 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100245 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200246 goto err_unlink_temp;
247 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100248
Willy Tarreau40aa0702013-03-10 23:51:38 +0100249 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200250 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreaub40dc942010-11-07 12:10:51 +0100251 msg = "cannot bind UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200252 goto err_unlink_temp;
253 }
254
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100255 /* <uid> and <gid> different of -1 will be used to change the socket owner.
256 * If <mode> is not 0, it will be used to restrict access to the socket.
257 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200258 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100259 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200260 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100261 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
262 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
263 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100264 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200265 goto err_unlink_temp;
266 }
267
Willy Tarreau40aa0702013-03-10 23:51:38 +0100268 ready = 0;
269 ready_len = sizeof(ready);
270 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
271 ready = 0;
272
273 if (!(ext && ready) && /* only listen if not already done by external process */
274 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100275 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200276 goto err_unlink_temp;
277 }
278
Willy Tarreauccfccef2014-05-10 01:49:15 +0200279 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200280 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200281 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200282 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200283 if (!ext && path[0] && 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 Tarreauccfccef2014-05-10 01:49:15 +0200288 /* Cleanup: If we're bound to an fd inherited from the parent, we
Willy Tarreau40aa0702013-03-10 23:51:38 +0100289 * want to ensure that destroy_uxst_socket() will never remove the
Willy Tarreauccfccef2014-05-10 01:49:15 +0200290 * path, and for this we simply clear the path to the socket, which
291 * under Linux corresponds to an abstract socket.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100292 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200293 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100294 unlink(backname);
295 else
296 ((struct sockaddr_un *)&listener->addr)->sun_path[0] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200297
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100298 /* the socket is now listening */
299 listener->fd = fd;
300 listener->state = LI_LISTEN;
301
302 /* the function for the accept() event */
303 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200304 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200305 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100306 return ERR_NONE;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100307 err_rename:
308 ret = rename(backname, path);
309 if (ret < 0 && errno == ENOENT)
310 unlink(path);
311 err_unlink_temp:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100312 if (!ext)
313 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100314 close(fd);
315 err_unlink_back:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100316 if (!ext)
317 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100318 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100319 if (msg && errlen) {
320 if (!ext)
321 snprintf(errmsg, errlen, "%s [%s]", msg, path);
322 else
323 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
324 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100325 return ERR_FATAL | ERR_ALERT;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100326}
327
328/* This function closes the UNIX sockets for the specified listener.
329 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
330 */
331static int uxst_unbind_listener(struct listener *listener)
332{
Willy Tarreaube58c382011-07-24 18:28:10 +0200333 if (listener->state > LI_ASSIGNED) {
334 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100335 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
336 }
337 return ERR_NONE;
338}
339
340/* Add a listener to the list of unix stream listeners. The listener's state
341 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
342 * listeners is updated. This is the function to use to add a new listener.
343 */
344void uxst_add_listener(struct listener *listener)
345{
346 if (listener->state != LI_INIT)
347 return;
348 listener->state = LI_ASSIGNED;
349 listener->proto = &proto_unix;
350 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
351 proto_unix.nb_listeners++;
352}
353
Willy Tarreau47f48c42014-05-09 22:57:47 +0200354
355/*
356 * This function initiates a UNIX connection establishment to the target assigned
357 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
358 * and will be selected by the system. conn->target may point either to a valid
359 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
360 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
361 * whether there are data waiting for being sent or not, in order to adjust data
362 * write polling and on some platforms. The <delack> argument is ignored.
363 *
364 * Note that a pending send_proxy message accounts for data.
365 *
366 * It can return one of :
367 * - SN_ERR_NONE if everything's OK
368 * - SN_ERR_SRVTO if there are no more servers
369 * - SN_ERR_SRVCL if the connection was refused by the server
370 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
371 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
372 * - SN_ERR_INTERNAL for any other purely internal errors
373 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
374 *
375 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
376 * it's invalid and the caller has nothing to do.
377 */
378int uxst_connect_server(struct connection *conn, int data, int delack)
379{
380 int fd;
381 struct server *srv;
382 struct proxy *be;
383
Willy Tarreau7bb21532014-05-10 09:48:28 +0200384 conn->flags = 0;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200385
386 switch (obj_type(conn->target)) {
387 case OBJ_TYPE_PROXY:
388 be = objt_proxy(conn->target);
389 srv = NULL;
390 break;
391 case OBJ_TYPE_SERVER:
392 srv = objt_server(conn->target);
393 be = srv->proxy;
394 break;
395 default:
396 conn->flags |= CO_FL_ERROR;
397 return SN_ERR_INTERNAL;
398 }
399
400 if ((fd = conn->t.sock.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
401 qfprintf(stderr, "Cannot get a server socket.\n");
402
403 if (errno == ENFILE) {
404 conn->err_code = CO_ER_SYS_FDLIM;
405 send_log(be, LOG_EMERG,
406 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
407 be->id, maxfd);
408 }
409 else if (errno == EMFILE) {
410 conn->err_code = CO_ER_PROC_FDLIM;
411 send_log(be, LOG_EMERG,
412 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
413 be->id, maxfd);
414 }
415 else if (errno == ENOBUFS || errno == ENOMEM) {
416 conn->err_code = CO_ER_SYS_MEMLIM;
417 send_log(be, LOG_EMERG,
418 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
419 be->id, maxfd);
420 }
421 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
422 conn->err_code = CO_ER_NOPROTO;
423 }
424 else
425 conn->err_code = CO_ER_SOCK_ERR;
426
427 /* this is a resource error */
428 conn->flags |= CO_FL_ERROR;
429 return SN_ERR_RESOURCE;
430 }
431
432 if (fd >= global.maxsock) {
433 /* do not log anything there, it's a normal condition when this option
434 * is used to serialize connections to a server !
435 */
436 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
437 close(fd);
438 conn->err_code = CO_ER_CONF_FDLIM;
439 conn->flags |= CO_FL_ERROR;
440 return SN_ERR_PRXCOND; /* it is a configuration limit */
441 }
442
443 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
444 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
445 close(fd);
446 conn->err_code = CO_ER_SOCK_ERR;
447 conn->flags |= CO_FL_ERROR;
448 return SN_ERR_INTERNAL;
449 }
450
451 /* if a send_proxy is there, there are data */
452 data |= conn->send_proxy_ofs;
453
454 if (global.tune.server_sndbuf)
455 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
456
457 if (global.tune.server_rcvbuf)
458 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
459
Willy Tarreau7bb21532014-05-10 09:48:28 +0200460 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
461 if (errno == EALREADY || errno == EISCONN) {
462 conn->flags &= ~CO_FL_WAIT_L4_CONN;
463 }
464 else if (errno == EINPROGRESS) {
465 conn->flags |= CO_FL_WAIT_L4_CONN;
466 }
467 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200468 char *msg;
469 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
470 msg = "no free ports";
471 conn->err_code = CO_ER_FREE_PORTS;
472 }
473 else {
474 msg = "local address already in use";
475 conn->err_code = CO_ER_ADDR_INUSE;
476 }
477
478 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
479 close(fd);
480 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
481 conn->flags |= CO_FL_ERROR;
482 return SN_ERR_RESOURCE;
483 }
484 else if (errno == ETIMEDOUT) {
485 close(fd);
486 conn->err_code = CO_ER_SOCK_ERR;
487 conn->flags |= CO_FL_ERROR;
488 return SN_ERR_SRVTO;
489 }
490 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
491 close(fd);
492 conn->err_code = CO_ER_SOCK_ERR;
493 conn->flags |= CO_FL_ERROR;
494 return SN_ERR_SRVCL;
495 }
496 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200497 else {
498 /* connect() already succeeded, which is quite usual for unix
499 * sockets. Let's avoid a second connect() probe to complete it,
500 * but we need to ensure we'll wake up if there's no more handshake
501 * pending (eg: for health checks).
502 */
503 conn->flags &= ~CO_FL_WAIT_L4_CONN;
504 if (!(conn->flags & CO_FL_HANDSHAKE))
505 data = 1;
506 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200507
508 conn->flags |= CO_FL_ADDR_TO_SET;
509
510 /* Prepare to send a few handshakes related to the on-wire protocol. */
511 if (conn->send_proxy_ofs)
512 conn->flags |= CO_FL_SEND_PROXY;
513
514 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200515 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
516 if (conn->flags & CO_FL_HANDSHAKE)
517 conn_sock_want_send(conn); /* for connect status or proxy protocol */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200518
519 if (conn_xprt_init(conn) < 0) {
520 conn_force_close(conn);
521 conn->flags |= CO_FL_ERROR;
522 return SN_ERR_RESOURCE;
523 }
524
525 if (data)
526 conn_data_want_send(conn); /* prepare to send data if any */
527
528 return SN_ERR_NONE; /* connection is OK */
529}
530
531
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100532/********************************
533 * 3) protocol-oriented functions
534 ********************************/
535
536
Willy Tarreau92fb9832007-10-16 17:34:28 +0200537/* This function creates all UNIX sockets bound to the protocol entry <proto>.
538 * It is intended to be used as the protocol's bind_all() function.
539 * The sockets will be registered but not added to any fd_set, in order not to
540 * loose them across the fork(). A call to uxst_enable_listeners() is needed
541 * to complete initialization.
542 *
543 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
544 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200545static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200546{
547 struct listener *listener;
548 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200549
550 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200551 err |= uxst_bind_listener(listener, errmsg, errlen);
552 if (err & ERR_ABORT)
553 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200554 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200555 return err;
556}
557
Willy Tarreau92fb9832007-10-16 17:34:28 +0200558
559/* This function stops all listening UNIX sockets bound to the protocol
560 * <proto>. It does not detaches them from the protocol.
561 * It always returns ERR_NONE.
562 */
563static int uxst_unbind_listeners(struct protocol *proto)
564{
565 struct listener *listener;
566
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100567 list_for_each_entry(listener, &proto->listeners, proto_list)
568 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200569 return ERR_NONE;
570}
571
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200572/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200573static 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 +0200574{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200575 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200576 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200577 return ERR_ALERT | ERR_FATAL;
578 }
579
Willy Tarreau290e63a2012-09-20 18:07:14 +0200580 conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200581 return 0;
582}
583
584/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200585static 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 +0200586{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200587 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200588 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200589 return ERR_ALERT | ERR_FATAL;
590 }
591
Willy Tarreau290e63a2012-09-20 18:07:14 +0200592 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200593 return 0;
594}
595
596/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200597static 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 +0200598{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200599 struct group *group;
600
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200601 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200602 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200603 return ERR_ALERT | ERR_FATAL;
604 }
605
606 group = getgrnam(args[cur_arg + 1]);
607 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200608 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200609 return ERR_ALERT | ERR_FATAL;
610 }
611
Willy Tarreau290e63a2012-09-20 18:07:14 +0200612 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200613 return 0;
614}
615
616/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200617static 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 +0200618{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200619 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200620 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200621 return ERR_ALERT | ERR_FATAL;
622 }
623
Willy Tarreau290e63a2012-09-20 18:07:14 +0200624 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200625 return 0;
626}
627
628/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200629static 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 +0200630{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200631 struct passwd *user;
632
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200633 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200634 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200635 return ERR_ALERT | ERR_FATAL;
636 }
637
638 user = getpwnam(args[cur_arg + 1]);
639 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200640 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200641 return ERR_ALERT | ERR_FATAL;
642 }
643
Willy Tarreau290e63a2012-09-20 18:07:14 +0200644 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200645 return 0;
646}
647
648/* Note: must not be declared <const> as its list will be overwritten.
649 * Please take care of keeping this list alphabetically sorted, doing so helps
650 * all code contributors.
651 * Optional keywords are also declared with a NULL ->parse() function so that
652 * the config parser can report an appropriate error when a known keyword was
653 * not enabled.
654 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200655static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200656 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
657 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
658 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
659 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
660 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
661 { NULL, NULL, 0 },
662}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100663
664/********************************
665 * 4) high-level functions
666 ********************************/
667
Willy Tarreau92fb9832007-10-16 17:34:28 +0200668__attribute__((constructor))
669static void __uxst_protocol_init(void)
670{
671 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200672 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200673}
674
675
676/*
677 * Local variables:
678 * c-indent-level: 8
679 * c-basic-offset: 8
680 * End:
681 */