blob: 409c659064052d22fcf2b09f743b4d47d81e0c82 [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;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200169 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100170 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200171
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200172 err = ERR_NONE;
173
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100174 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100175 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100176 *errmsg = 0;
177
178 if (listener->state != LI_ASSIGNED)
179 return ERR_NONE; /* already bound */
180
181 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200182
Willy Tarreau40aa0702013-03-10 23:51:38 +0100183 /* if the listener already has an fd assigned, then we were offered the
184 * fd by an external process (most likely the parent), and we don't want
185 * to create a new socket. However we still want to set a few flags on
186 * the socket.
187 */
188 fd = listener->fd;
189 ext = (fd >= 0);
190 if (ext)
191 goto fd_ready;
192
Willy Tarreauccfccef2014-05-10 01:49:15 +0200193 if (path[0]) {
194 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
195 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200196 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200197 msg = "name too long for UNIX socket";
198 goto err_return;
199 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200200
Willy Tarreauccfccef2014-05-10 01:49:15 +0200201 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
202 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200203 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200204 msg = "name too long for UNIX socket";
205 goto err_return;
206 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200207
Willy Tarreauccfccef2014-05-10 01:49:15 +0200208 /* 2. clean existing orphaned entries */
209 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200210 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200211 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 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200216 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200217 msg = "error when trying to unlink previous UNIX socket";
218 goto err_return;
219 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200220
Willy Tarreauccfccef2014-05-10 01:49:15 +0200221 /* 3. backup existing socket */
222 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200223 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200224 msg = "error when trying to preserve previous UNIX socket";
225 goto err_return;
226 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200227
Willy Tarreauccfccef2014-05-10 01:49:15 +0200228 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
229 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200230 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200231 else {
232 /* first char is zero, it's an abstract socket whose address
233 * is defined by all the bytes past this zero.
234 */
235 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
236 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200237 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200238
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100239 fd = socket(PF_UNIX, SOCK_STREAM, 0);
240 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200241 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100242 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200243 goto err_unlink_back;
244 }
245
Willy Tarreau40aa0702013-03-10 23:51:38 +0100246 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100247 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200248 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100249 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200250 goto err_unlink_temp;
251 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100252
253 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200254 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100255 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200256 goto err_unlink_temp;
257 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100258
Willy Tarreau40aa0702013-03-10 23:51:38 +0100259 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200260 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200261 if (errno == EADDRINUSE) {
262 /* the old process might still own it, let's retry */
263 err |= ERR_RETRYABLE | ERR_ALERT;
264 msg = "cannot listen to socket";
265 }
266 else {
267 err |= ERR_FATAL | ERR_ALERT;
268 msg = "cannot bind UNIX socket";
269 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200270 goto err_unlink_temp;
271 }
272
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100273 /* <uid> and <gid> different of -1 will be used to change the socket owner.
274 * If <mode> is not 0, it will be used to restrict access to the socket.
275 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200276 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100277 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200278 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100279 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
280 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
281 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200282 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100283 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200284 goto err_unlink_temp;
285 }
286
Willy Tarreau40aa0702013-03-10 23:51:38 +0100287 ready = 0;
288 ready_len = sizeof(ready);
289 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
290 ready = 0;
291
292 if (!(ext && ready) && /* only listen if not already done by external process */
293 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200294 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100295 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200296 goto err_unlink_temp;
297 }
298
Willy Tarreauccfccef2014-05-10 01:49:15 +0200299 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200300 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200301 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200302 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200303 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200304 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100305 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200306 goto err_rename;
307 }
308
Willy Tarreauccfccef2014-05-10 01:49:15 +0200309 /* Cleanup: If we're bound to an fd inherited from the parent, we
Willy Tarreau40aa0702013-03-10 23:51:38 +0100310 * want to ensure that destroy_uxst_socket() will never remove the
Willy Tarreauccfccef2014-05-10 01:49:15 +0200311 * path, and for this we simply clear the path to the socket, which
312 * under Linux corresponds to an abstract socket.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100313 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200314 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100315 unlink(backname);
316 else
317 ((struct sockaddr_un *)&listener->addr)->sun_path[0] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200318
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100319 /* the socket is now listening */
320 listener->fd = fd;
321 listener->state = LI_LISTEN;
322
323 /* the function for the accept() event */
324 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200325 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200326 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200327 return err;
328
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100329 err_rename:
330 ret = rename(backname, path);
331 if (ret < 0 && errno == ENOENT)
332 unlink(path);
333 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200334 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100335 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100336 close(fd);
337 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200338 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100339 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100340 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100341 if (msg && errlen) {
342 if (!ext)
343 snprintf(errmsg, errlen, "%s [%s]", msg, path);
344 else
345 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
346 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200347 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100348}
349
350/* This function closes the UNIX sockets for the specified listener.
351 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
352 */
353static int uxst_unbind_listener(struct listener *listener)
354{
Willy Tarreaube58c382011-07-24 18:28:10 +0200355 if (listener->state > LI_ASSIGNED) {
356 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100357 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
358 }
359 return ERR_NONE;
360}
361
362/* Add a listener to the list of unix stream listeners. The listener's state
363 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
364 * listeners is updated. This is the function to use to add a new listener.
365 */
366void uxst_add_listener(struct listener *listener)
367{
368 if (listener->state != LI_INIT)
369 return;
370 listener->state = LI_ASSIGNED;
371 listener->proto = &proto_unix;
372 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
373 proto_unix.nb_listeners++;
374}
375
Willy Tarreau47f48c42014-05-09 22:57:47 +0200376
377/*
378 * This function initiates a UNIX connection establishment to the target assigned
379 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
380 * and will be selected by the system. conn->target may point either to a valid
381 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
382 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
383 * whether there are data waiting for being sent or not, in order to adjust data
384 * write polling and on some platforms. The <delack> argument is ignored.
385 *
386 * Note that a pending send_proxy message accounts for data.
387 *
388 * It can return one of :
389 * - SN_ERR_NONE if everything's OK
390 * - SN_ERR_SRVTO if there are no more servers
391 * - SN_ERR_SRVCL if the connection was refused by the server
392 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
393 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
394 * - SN_ERR_INTERNAL for any other purely internal errors
395 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
396 *
397 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
398 * it's invalid and the caller has nothing to do.
399 */
400int uxst_connect_server(struct connection *conn, int data, int delack)
401{
402 int fd;
403 struct server *srv;
404 struct proxy *be;
405
Willy Tarreau7bb21532014-05-10 09:48:28 +0200406 conn->flags = 0;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200407
408 switch (obj_type(conn->target)) {
409 case OBJ_TYPE_PROXY:
410 be = objt_proxy(conn->target);
411 srv = NULL;
412 break;
413 case OBJ_TYPE_SERVER:
414 srv = objt_server(conn->target);
415 be = srv->proxy;
416 break;
417 default:
418 conn->flags |= CO_FL_ERROR;
419 return SN_ERR_INTERNAL;
420 }
421
422 if ((fd = conn->t.sock.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
423 qfprintf(stderr, "Cannot get a server socket.\n");
424
425 if (errno == ENFILE) {
426 conn->err_code = CO_ER_SYS_FDLIM;
427 send_log(be, LOG_EMERG,
428 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
429 be->id, maxfd);
430 }
431 else if (errno == EMFILE) {
432 conn->err_code = CO_ER_PROC_FDLIM;
433 send_log(be, LOG_EMERG,
434 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
435 be->id, maxfd);
436 }
437 else if (errno == ENOBUFS || errno == ENOMEM) {
438 conn->err_code = CO_ER_SYS_MEMLIM;
439 send_log(be, LOG_EMERG,
440 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
441 be->id, maxfd);
442 }
443 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
444 conn->err_code = CO_ER_NOPROTO;
445 }
446 else
447 conn->err_code = CO_ER_SOCK_ERR;
448
449 /* this is a resource error */
450 conn->flags |= CO_FL_ERROR;
451 return SN_ERR_RESOURCE;
452 }
453
454 if (fd >= global.maxsock) {
455 /* do not log anything there, it's a normal condition when this option
456 * is used to serialize connections to a server !
457 */
458 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
459 close(fd);
460 conn->err_code = CO_ER_CONF_FDLIM;
461 conn->flags |= CO_FL_ERROR;
462 return SN_ERR_PRXCOND; /* it is a configuration limit */
463 }
464
465 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
466 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
467 close(fd);
468 conn->err_code = CO_ER_SOCK_ERR;
469 conn->flags |= CO_FL_ERROR;
470 return SN_ERR_INTERNAL;
471 }
472
473 /* if a send_proxy is there, there are data */
474 data |= conn->send_proxy_ofs;
475
476 if (global.tune.server_sndbuf)
477 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
478
479 if (global.tune.server_rcvbuf)
480 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
481
Willy Tarreau7bb21532014-05-10 09:48:28 +0200482 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
483 if (errno == EALREADY || errno == EISCONN) {
484 conn->flags &= ~CO_FL_WAIT_L4_CONN;
485 }
486 else if (errno == EINPROGRESS) {
487 conn->flags |= CO_FL_WAIT_L4_CONN;
488 }
489 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200490 char *msg;
491 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
492 msg = "no free ports";
493 conn->err_code = CO_ER_FREE_PORTS;
494 }
495 else {
496 msg = "local address already in use";
497 conn->err_code = CO_ER_ADDR_INUSE;
498 }
499
500 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
501 close(fd);
502 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
503 conn->flags |= CO_FL_ERROR;
504 return SN_ERR_RESOURCE;
505 }
506 else if (errno == ETIMEDOUT) {
507 close(fd);
508 conn->err_code = CO_ER_SOCK_ERR;
509 conn->flags |= CO_FL_ERROR;
510 return SN_ERR_SRVTO;
511 }
512 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
513 close(fd);
514 conn->err_code = CO_ER_SOCK_ERR;
515 conn->flags |= CO_FL_ERROR;
516 return SN_ERR_SRVCL;
517 }
518 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200519 else {
520 /* connect() already succeeded, which is quite usual for unix
521 * sockets. Let's avoid a second connect() probe to complete it,
522 * but we need to ensure we'll wake up if there's no more handshake
523 * pending (eg: for health checks).
524 */
525 conn->flags &= ~CO_FL_WAIT_L4_CONN;
526 if (!(conn->flags & CO_FL_HANDSHAKE))
527 data = 1;
528 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200529
530 conn->flags |= CO_FL_ADDR_TO_SET;
531
532 /* Prepare to send a few handshakes related to the on-wire protocol. */
533 if (conn->send_proxy_ofs)
534 conn->flags |= CO_FL_SEND_PROXY;
535
536 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200537 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
538 if (conn->flags & CO_FL_HANDSHAKE)
539 conn_sock_want_send(conn); /* for connect status or proxy protocol */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200540
541 if (conn_xprt_init(conn) < 0) {
542 conn_force_close(conn);
543 conn->flags |= CO_FL_ERROR;
544 return SN_ERR_RESOURCE;
545 }
546
547 if (data)
548 conn_data_want_send(conn); /* prepare to send data if any */
549
550 return SN_ERR_NONE; /* connection is OK */
551}
552
553
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100554/********************************
555 * 3) protocol-oriented functions
556 ********************************/
557
558
Willy Tarreau92fb9832007-10-16 17:34:28 +0200559/* This function creates all UNIX sockets bound to the protocol entry <proto>.
560 * It is intended to be used as the protocol's bind_all() function.
561 * The sockets will be registered but not added to any fd_set, in order not to
562 * loose them across the fork(). A call to uxst_enable_listeners() is needed
563 * to complete initialization.
564 *
565 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
566 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200567static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200568{
569 struct listener *listener;
570 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200571
572 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200573 err |= uxst_bind_listener(listener, errmsg, errlen);
574 if (err & ERR_ABORT)
575 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200576 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200577 return err;
578}
579
Willy Tarreau92fb9832007-10-16 17:34:28 +0200580
581/* This function stops all listening UNIX sockets bound to the protocol
582 * <proto>. It does not detaches them from the protocol.
583 * It always returns ERR_NONE.
584 */
585static int uxst_unbind_listeners(struct protocol *proto)
586{
587 struct listener *listener;
588
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100589 list_for_each_entry(listener, &proto->listeners, proto_list)
590 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200591 return ERR_NONE;
592}
593
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200594/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200595static 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 +0200596{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200597 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200598 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200599 return ERR_ALERT | ERR_FATAL;
600 }
601
Willy Tarreau290e63a2012-09-20 18:07:14 +0200602 conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200603 return 0;
604}
605
606/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200607static 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 +0200608{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200609 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200610 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200611 return ERR_ALERT | ERR_FATAL;
612 }
613
Willy Tarreau290e63a2012-09-20 18:07:14 +0200614 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200615 return 0;
616}
617
618/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200619static 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 +0200620{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200621 struct group *group;
622
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200623 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200624 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200625 return ERR_ALERT | ERR_FATAL;
626 }
627
628 group = getgrnam(args[cur_arg + 1]);
629 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200630 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200631 return ERR_ALERT | ERR_FATAL;
632 }
633
Willy Tarreau290e63a2012-09-20 18:07:14 +0200634 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200635 return 0;
636}
637
638/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200639static 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 +0200640{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200641 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200642 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200643 return ERR_ALERT | ERR_FATAL;
644 }
645
Willy Tarreau290e63a2012-09-20 18:07:14 +0200646 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200647 return 0;
648}
649
650/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200651static 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 +0200652{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200653 struct passwd *user;
654
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200655 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200656 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200657 return ERR_ALERT | ERR_FATAL;
658 }
659
660 user = getpwnam(args[cur_arg + 1]);
661 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200662 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200663 return ERR_ALERT | ERR_FATAL;
664 }
665
Willy Tarreau290e63a2012-09-20 18:07:14 +0200666 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200667 return 0;
668}
669
670/* Note: must not be declared <const> as its list will be overwritten.
671 * Please take care of keeping this list alphabetically sorted, doing so helps
672 * all code contributors.
673 * Optional keywords are also declared with a NULL ->parse() function so that
674 * the config parser can report an appropriate error when a known keyword was
675 * not enabled.
676 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200677static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200678 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
679 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
680 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
681 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
682 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
683 { NULL, NULL, 0 },
684}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100685
686/********************************
687 * 4) high-level functions
688 ********************************/
689
Willy Tarreau92fb9832007-10-16 17:34:28 +0200690__attribute__((constructor))
691static void __uxst_protocol_init(void)
692{
693 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200694 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200695}
696
697
698/*
699 * Local variables:
700 * c-indent-level: 8
701 * c-basic-offset: 8
702 * End:
703 */