blob: adc1b466032a3606e5b3e96e247a05f41e8fc60b [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 Tarreau631a70f2014-07-07 21:07:51 +020071 .pause = uxst_pause_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010072 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
73 .nb_listeners = 0,
74};
75
Willy Tarreaudabf2e22007-10-28 21:59:24 +010076/********************************
77 * 1) low-level socket functions
78 ********************************/
79
Willy Tarreau59b94792012-05-11 16:16:40 +020080/*
81 * Retrieves the source address for the socket <fd>, with <dir> indicating
82 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
83 * success, -1 in case of error. The socket's source address is stored in
84 * <sa> for <salen> bytes.
85 */
86int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
87{
88 if (dir)
89 return getsockname(fd, sa, &salen);
90 else
91 return getpeername(fd, sa, &salen);
92}
93
94
95/*
96 * Retrieves the original destination address for the socket <fd>, with <dir>
97 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
98 * case of success, -1 in case of error. The socket's source address is stored
99 * in <sa> for <salen> bytes.
100 */
101int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
102{
103 if (dir)
104 return getpeername(fd, sa, &salen);
105 else
106 return getsockname(fd, sa, &salen);
107}
108
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100109
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100110/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
111 * anymore. It practises best effort, and no error is returned.
112 */
113static void destroy_uxst_socket(const char *path)
114{
115 struct sockaddr_un addr;
116 int sock, ret;
117
Willy Tarreau40aa0702013-03-10 23:51:38 +0100118 /* if the path was cleared, we do nothing */
119 if (!*path)
120 return;
121
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100122 /* We might have been chrooted, so we may not be able to access the
123 * socket. In order to avoid bothering the other end, we connect with a
124 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
125 * is enough to know if the socket is still live or not. If it's live
126 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
127 * ECONNREFUSED. In this case, we do not touch it because it's used
128 * by some other process.
129 */
130 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
131 if (sock < 0)
132 return;
133
134 addr.sun_family = AF_UNIX;
135 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
136 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
137 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
138 if (ret < 0 && errno == ECONNREFUSED) {
139 /* Connect failed: the socket still exists but is not used
140 * anymore. Let's remove this socket now.
141 */
142 unlink(path);
143 }
144 close(sock);
145}
146
147
148/********************************
149 * 2) listener-oriented functions
150 ********************************/
151
152
153/* This function creates a UNIX socket associated to the listener. It changes
154 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100155 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
156 * may return a warning or an error message in <errmsg> if the message is at
157 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
158 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200159 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100160static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200161{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100162 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200163 char tempname[MAXPATHLEN];
164 char backname[MAXPATHLEN];
165 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100166 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100167 const char *path;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100168 int ext, ready;
169 socklen_t ready_len;
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200170 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100171 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200172
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200173 err = ERR_NONE;
174
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100175 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100176 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100177 *errmsg = 0;
178
179 if (listener->state != LI_ASSIGNED)
180 return ERR_NONE; /* already bound */
181
182 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200183
Willy Tarreau40aa0702013-03-10 23:51:38 +0100184 /* if the listener already has an fd assigned, then we were offered the
185 * fd by an external process (most likely the parent), and we don't want
186 * to create a new socket. However we still want to set a few flags on
187 * the socket.
188 */
189 fd = listener->fd;
190 ext = (fd >= 0);
191 if (ext)
192 goto fd_ready;
193
Willy Tarreauccfccef2014-05-10 01:49:15 +0200194 if (path[0]) {
195 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
196 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200197 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200198 msg = "name too long for UNIX socket";
199 goto err_return;
200 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200201
Willy Tarreauccfccef2014-05-10 01:49:15 +0200202 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
203 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200204 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200205 msg = "name too long for UNIX socket";
206 goto err_return;
207 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200208
Willy Tarreauccfccef2014-05-10 01:49:15 +0200209 /* 2. clean existing orphaned entries */
210 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200211 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200212 msg = "error when trying to unlink previous UNIX socket";
213 goto err_return;
214 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200215
Willy Tarreauccfccef2014-05-10 01:49:15 +0200216 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200217 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200218 msg = "error when trying to unlink previous UNIX socket";
219 goto err_return;
220 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200221
Willy Tarreauccfccef2014-05-10 01:49:15 +0200222 /* 3. backup existing socket */
223 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200224 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200225 msg = "error when trying to preserve previous UNIX socket";
226 goto err_return;
227 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200228
Willy Tarreauccfccef2014-05-10 01:49:15 +0200229 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
230 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200231 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200232 else {
233 /* first char is zero, it's an abstract socket whose address
234 * is defined by all the bytes past this zero.
235 */
236 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
237 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200238 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200239
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100240 fd = socket(PF_UNIX, SOCK_STREAM, 0);
241 if (fd < 0) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200242 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100243 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200244 goto err_unlink_back;
245 }
246
Willy Tarreau40aa0702013-03-10 23:51:38 +0100247 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100248 if (fd >= global.maxsock) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200249 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100250 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200251 goto err_unlink_temp;
252 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100253
254 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200255 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100256 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200257 goto err_unlink_temp;
258 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100259
Willy Tarreau40aa0702013-03-10 23:51:38 +0100260 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200261 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200262 if (errno == EADDRINUSE) {
263 /* the old process might still own it, let's retry */
264 err |= ERR_RETRYABLE | ERR_ALERT;
265 msg = "cannot listen to socket";
266 }
267 else {
268 err |= ERR_FATAL | ERR_ALERT;
269 msg = "cannot bind UNIX socket";
270 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200271 goto err_unlink_temp;
272 }
273
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100274 /* <uid> and <gid> different of -1 will be used to change the socket owner.
275 * If <mode> is not 0, it will be used to restrict access to the socket.
276 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200277 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100278 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200279 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100280 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
281 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
282 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200283 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100284 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200285 goto err_unlink_temp;
286 }
287
Willy Tarreau40aa0702013-03-10 23:51:38 +0100288 ready = 0;
289 ready_len = sizeof(ready);
290 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
291 ready = 0;
292
293 if (!(ext && ready) && /* only listen if not already done by external process */
294 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200295 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100296 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200297 goto err_unlink_temp;
298 }
299
Willy Tarreauccfccef2014-05-10 01:49:15 +0200300 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200301 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200302 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200303 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200304 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200305 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100306 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200307 goto err_rename;
308 }
309
Willy Tarreauccfccef2014-05-10 01:49:15 +0200310 /* Cleanup: If we're bound to an fd inherited from the parent, we
Willy Tarreau40aa0702013-03-10 23:51:38 +0100311 * want to ensure that destroy_uxst_socket() will never remove the
Willy Tarreauccfccef2014-05-10 01:49:15 +0200312 * path, and for this we simply clear the path to the socket, which
313 * under Linux corresponds to an abstract socket.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100314 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200315 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100316 unlink(backname);
317 else
318 ((struct sockaddr_un *)&listener->addr)->sun_path[0] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200319
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100320 /* the socket is now listening */
321 listener->fd = fd;
322 listener->state = LI_LISTEN;
323
324 /* the function for the accept() event */
325 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200326 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200327 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200328 return err;
329
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100330 err_rename:
331 ret = rename(backname, path);
332 if (ret < 0 && errno == ENOENT)
333 unlink(path);
334 err_unlink_temp:
Jan Seda76ad9982014-06-26 20:44:05 +0200335 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100336 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100337 close(fd);
338 err_unlink_back:
Jan Seda76ad9982014-06-26 20:44:05 +0200339 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100340 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100341 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100342 if (msg && errlen) {
343 if (!ext)
344 snprintf(errmsg, errlen, "%s [%s]", msg, path);
345 else
346 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
347 }
Willy Tarreau58dd0f32014-07-07 18:36:45 +0200348 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100349}
350
351/* This function closes the UNIX sockets for the specified listener.
352 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
353 */
354static int uxst_unbind_listener(struct listener *listener)
355{
Willy Tarreaube58c382011-07-24 18:28:10 +0200356 if (listener->state > LI_ASSIGNED) {
357 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100358 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
359 }
360 return ERR_NONE;
361}
362
363/* Add a listener to the list of unix stream listeners. The listener's state
364 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
365 * listeners is updated. This is the function to use to add a new listener.
366 */
367void uxst_add_listener(struct listener *listener)
368{
369 if (listener->state != LI_INIT)
370 return;
371 listener->state = LI_ASSIGNED;
372 listener->proto = &proto_unix;
373 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
374 proto_unix.nb_listeners++;
375}
376
Willy Tarreau631a70f2014-07-07 21:07:51 +0200377/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
378 * was totally stopped, or > 0 if correctly paused. Nothing is done for
379 * plain unix sockets since currently it's the new process which handles
380 * the renaming. Abstract sockets are completely unbound.
381 */
382int uxst_pause_listener(struct listener *l)
383{
384 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
385 return 1;
386
387 unbind_listener(l);
388 return 0;
389}
390
Willy Tarreau47f48c42014-05-09 22:57:47 +0200391
392/*
393 * This function initiates a UNIX connection establishment to the target assigned
394 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
395 * and will be selected by the system. conn->target may point either to a valid
396 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
397 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
398 * whether there are data waiting for being sent or not, in order to adjust data
399 * write polling and on some platforms. The <delack> argument is ignored.
400 *
401 * Note that a pending send_proxy message accounts for data.
402 *
403 * It can return one of :
404 * - SN_ERR_NONE if everything's OK
405 * - SN_ERR_SRVTO if there are no more servers
406 * - SN_ERR_SRVCL if the connection was refused by the server
407 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
408 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
409 * - SN_ERR_INTERNAL for any other purely internal errors
410 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
411 *
412 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
413 * it's invalid and the caller has nothing to do.
414 */
415int uxst_connect_server(struct connection *conn, int data, int delack)
416{
417 int fd;
418 struct server *srv;
419 struct proxy *be;
420
Willy Tarreau7bb21532014-05-10 09:48:28 +0200421 conn->flags = 0;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200422
423 switch (obj_type(conn->target)) {
424 case OBJ_TYPE_PROXY:
425 be = objt_proxy(conn->target);
426 srv = NULL;
427 break;
428 case OBJ_TYPE_SERVER:
429 srv = objt_server(conn->target);
430 be = srv->proxy;
431 break;
432 default:
433 conn->flags |= CO_FL_ERROR;
434 return SN_ERR_INTERNAL;
435 }
436
437 if ((fd = conn->t.sock.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
438 qfprintf(stderr, "Cannot get a server socket.\n");
439
440 if (errno == ENFILE) {
441 conn->err_code = CO_ER_SYS_FDLIM;
442 send_log(be, LOG_EMERG,
443 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
444 be->id, maxfd);
445 }
446 else if (errno == EMFILE) {
447 conn->err_code = CO_ER_PROC_FDLIM;
448 send_log(be, LOG_EMERG,
449 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
450 be->id, maxfd);
451 }
452 else if (errno == ENOBUFS || errno == ENOMEM) {
453 conn->err_code = CO_ER_SYS_MEMLIM;
454 send_log(be, LOG_EMERG,
455 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
456 be->id, maxfd);
457 }
458 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
459 conn->err_code = CO_ER_NOPROTO;
460 }
461 else
462 conn->err_code = CO_ER_SOCK_ERR;
463
464 /* this is a resource error */
465 conn->flags |= CO_FL_ERROR;
466 return SN_ERR_RESOURCE;
467 }
468
469 if (fd >= global.maxsock) {
470 /* do not log anything there, it's a normal condition when this option
471 * is used to serialize connections to a server !
472 */
473 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
474 close(fd);
475 conn->err_code = CO_ER_CONF_FDLIM;
476 conn->flags |= CO_FL_ERROR;
477 return SN_ERR_PRXCOND; /* it is a configuration limit */
478 }
479
480 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
481 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
482 close(fd);
483 conn->err_code = CO_ER_SOCK_ERR;
484 conn->flags |= CO_FL_ERROR;
485 return SN_ERR_INTERNAL;
486 }
487
488 /* if a send_proxy is there, there are data */
489 data |= conn->send_proxy_ofs;
490
491 if (global.tune.server_sndbuf)
492 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
493
494 if (global.tune.server_rcvbuf)
495 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
496
Willy Tarreau7bb21532014-05-10 09:48:28 +0200497 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
498 if (errno == EALREADY || errno == EISCONN) {
499 conn->flags &= ~CO_FL_WAIT_L4_CONN;
500 }
501 else if (errno == EINPROGRESS) {
502 conn->flags |= CO_FL_WAIT_L4_CONN;
503 }
504 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200505 char *msg;
506 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
507 msg = "no free ports";
508 conn->err_code = CO_ER_FREE_PORTS;
509 }
510 else {
511 msg = "local address already in use";
512 conn->err_code = CO_ER_ADDR_INUSE;
513 }
514
515 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
516 close(fd);
517 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
518 conn->flags |= CO_FL_ERROR;
519 return SN_ERR_RESOURCE;
520 }
521 else if (errno == ETIMEDOUT) {
522 close(fd);
523 conn->err_code = CO_ER_SOCK_ERR;
524 conn->flags |= CO_FL_ERROR;
525 return SN_ERR_SRVTO;
526 }
527 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
528 close(fd);
529 conn->err_code = CO_ER_SOCK_ERR;
530 conn->flags |= CO_FL_ERROR;
531 return SN_ERR_SRVCL;
532 }
533 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200534 else {
535 /* connect() already succeeded, which is quite usual for unix
536 * sockets. Let's avoid a second connect() probe to complete it,
537 * but we need to ensure we'll wake up if there's no more handshake
538 * pending (eg: for health checks).
539 */
540 conn->flags &= ~CO_FL_WAIT_L4_CONN;
541 if (!(conn->flags & CO_FL_HANDSHAKE))
542 data = 1;
543 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200544
545 conn->flags |= CO_FL_ADDR_TO_SET;
546
547 /* Prepare to send a few handshakes related to the on-wire protocol. */
548 if (conn->send_proxy_ofs)
549 conn->flags |= CO_FL_SEND_PROXY;
550
551 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200552 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
553 if (conn->flags & CO_FL_HANDSHAKE)
554 conn_sock_want_send(conn); /* for connect status or proxy protocol */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200555
556 if (conn_xprt_init(conn) < 0) {
557 conn_force_close(conn);
558 conn->flags |= CO_FL_ERROR;
559 return SN_ERR_RESOURCE;
560 }
561
562 if (data)
563 conn_data_want_send(conn); /* prepare to send data if any */
564
565 return SN_ERR_NONE; /* connection is OK */
566}
567
568
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100569/********************************
570 * 3) protocol-oriented functions
571 ********************************/
572
573
Willy Tarreau92fb9832007-10-16 17:34:28 +0200574/* This function creates all UNIX sockets bound to the protocol entry <proto>.
575 * It is intended to be used as the protocol's bind_all() function.
576 * The sockets will be registered but not added to any fd_set, in order not to
577 * loose them across the fork(). A call to uxst_enable_listeners() is needed
578 * to complete initialization.
579 *
580 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
581 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200582static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200583{
584 struct listener *listener;
585 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200586
587 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200588 err |= uxst_bind_listener(listener, errmsg, errlen);
589 if (err & ERR_ABORT)
590 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200591 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200592 return err;
593}
594
Willy Tarreau92fb9832007-10-16 17:34:28 +0200595
596/* This function stops all listening UNIX sockets bound to the protocol
597 * <proto>. It does not detaches them from the protocol.
598 * It always returns ERR_NONE.
599 */
600static int uxst_unbind_listeners(struct protocol *proto)
601{
602 struct listener *listener;
603
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100604 list_for_each_entry(listener, &proto->listeners, proto_list)
605 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200606 return ERR_NONE;
607}
608
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200609/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200610static 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 +0200611{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200612 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200613 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200614 return ERR_ALERT | ERR_FATAL;
615 }
616
Willy Tarreau290e63a2012-09-20 18:07:14 +0200617 conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200618 return 0;
619}
620
621/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200622static 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 +0200623{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200624 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200625 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200626 return ERR_ALERT | ERR_FATAL;
627 }
628
Willy Tarreau290e63a2012-09-20 18:07:14 +0200629 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200630 return 0;
631}
632
633/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200634static 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 +0200635{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200636 struct group *group;
637
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200638 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200639 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200640 return ERR_ALERT | ERR_FATAL;
641 }
642
643 group = getgrnam(args[cur_arg + 1]);
644 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200645 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200646 return ERR_ALERT | ERR_FATAL;
647 }
648
Willy Tarreau290e63a2012-09-20 18:07:14 +0200649 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200650 return 0;
651}
652
653/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200654static 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 +0200655{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200656 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200657 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200658 return ERR_ALERT | ERR_FATAL;
659 }
660
Willy Tarreau290e63a2012-09-20 18:07:14 +0200661 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200662 return 0;
663}
664
665/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200666static 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 +0200667{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200668 struct passwd *user;
669
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200670 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200671 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200672 return ERR_ALERT | ERR_FATAL;
673 }
674
675 user = getpwnam(args[cur_arg + 1]);
676 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200677 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200678 return ERR_ALERT | ERR_FATAL;
679 }
680
Willy Tarreau290e63a2012-09-20 18:07:14 +0200681 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200682 return 0;
683}
684
685/* Note: must not be declared <const> as its list will be overwritten.
686 * Please take care of keeping this list alphabetically sorted, doing so helps
687 * all code contributors.
688 * Optional keywords are also declared with a NULL ->parse() function so that
689 * the config parser can report an appropriate error when a known keyword was
690 * not enabled.
691 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200692static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200693 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
694 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
695 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
696 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
697 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
698 { NULL, NULL, 0 },
699}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100700
701/********************************
702 * 4) high-level functions
703 ********************************/
704
Willy Tarreau92fb9832007-10-16 17:34:28 +0200705__attribute__((constructor))
706static void __uxst_protocol_init(void)
707{
708 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200709 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200710}
711
712
713/*
714 * Local variables:
715 * c-indent-level: 8
716 * c-basic-offset: 8
717 * End:
718 */