blob: 60efb6dd7114642468b8353930bac36ab981526c [file] [log] [blame]
Willy Tarreau92fb9832007-10-16 17:34:28 +02001/*
2 * UNIX SOCK_STREAM protocol layer (uxst)
3 *
Willy Tarreau2c9f5b12009-08-16 19:12:36 +02004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreau92fb9832007-10-16 17:34:28 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21
22#include <sys/param.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/un.h>
27
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010031#include <common/errors.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020032#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020036#include <common/time.h>
37#include <common/version.h>
38
Willy Tarreau92fb9832007-10-16 17:34:28 +020039#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020040
41#include <proto/acl.h>
42#include <proto/backend.h>
43#include <proto/buffers.h>
44#include <proto/fd.h>
45#include <proto/log.h>
46#include <proto/protocols.h>
47#include <proto/proto_uxst.h>
48#include <proto/queue.h>
49#include <proto/session.h>
Willy Tarreaub1356cf2008-12-07 16:06:43 +010050#include <proto/stream_interface.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020051#include <proto/stream_sock.h>
52#include <proto/task.h>
53
54#ifndef MAXPATHLEN
55#define MAXPATHLEN 128
56#endif
57
Willy Tarreaudabf2e22007-10-28 21:59:24 +010058static int uxst_bind_listeners(struct protocol *proto);
59static int uxst_unbind_listeners(struct protocol *proto);
60
61/* Note: must not be declared <const> as its list will be overwritten */
62static struct protocol proto_unix = {
63 .name = "unix_stream",
64 .sock_domain = PF_UNIX,
65 .sock_type = SOCK_STREAM,
66 .sock_prot = 0,
67 .sock_family = AF_UNIX,
68 .sock_addrlen = sizeof(struct sockaddr_un),
69 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
70 .read = &stream_sock_read,
71 .write = &stream_sock_write,
72 .bind_all = uxst_bind_listeners,
73 .unbind_all = uxst_unbind_listeners,
74 .enable_all = enable_all_listeners,
75 .disable_all = disable_all_listeners,
76 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
77 .nb_listeners = 0,
78};
79
Willy Tarreaudabf2e22007-10-28 21:59:24 +010080/********************************
81 * 1) low-level socket functions
82 ********************************/
83
84
Willy Tarreau92fb9832007-10-16 17:34:28 +020085/* This function creates a named PF_UNIX stream socket at address <path>. Note
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020086 * that the path cannot be NULL nor empty. <uid> and <gid> different of -1 will
87 * be used to change the socket owner. If <mode> is not 0, it will be used to
88 * restrict access to the socket. While it is known not to be portable on every
89 * OS, it's still useful where it works.
Willy Tarreau92fb9832007-10-16 17:34:28 +020090 * It returns the assigned file descriptor, or -1 in the event of an error.
91 */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020092static int create_uxst_socket(const char *path, uid_t uid, gid_t gid, mode_t mode)
Willy Tarreau92fb9832007-10-16 17:34:28 +020093{
94 char tempname[MAXPATHLEN];
95 char backname[MAXPATHLEN];
96 struct sockaddr_un addr;
97
98 int ret, sock;
99
100 /* 1. create socket names */
101 if (!path[0]) {
102 Alert("Invalid name for a UNIX socket. Aborting.\n");
103 goto err_return;
104 }
105
106 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
107 if (ret < 0 || ret >= MAXPATHLEN) {
108 Alert("name too long for UNIX socket. Aborting.\n");
109 goto err_return;
110 }
111
112 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
113 if (ret < 0 || ret >= MAXPATHLEN) {
114 Alert("name too long for UNIX socket. Aborting.\n");
115 goto err_return;
116 }
117
118 /* 2. clean existing orphaned entries */
119 if (unlink(tempname) < 0 && errno != ENOENT) {
120 Alert("error when trying to unlink previous UNIX socket. Aborting.\n");
121 goto err_return;
122 }
123
124 if (unlink(backname) < 0 && errno != ENOENT) {
125 Alert("error when trying to unlink previous UNIX socket. Aborting.\n");
126 goto err_return;
127 }
128
129 /* 3. backup existing socket */
130 if (link(path, backname) < 0 && errno != ENOENT) {
131 Alert("error when trying to preserve previous UNIX socket. Aborting.\n");
132 goto err_return;
133 }
134
135 /* 4. prepare new socket */
136 addr.sun_family = AF_UNIX;
137 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
138 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
139
140 sock = socket(PF_UNIX, SOCK_STREAM, 0);
141 if (sock < 0) {
142 Alert("cannot create socket for UNIX listener. Aborting.\n");
143 goto err_unlink_back;
144 }
145
146 if (sock >= global.maxsock) {
147 Alert("socket(): not enough free sockets for UNIX listener. Raise -n argument. Aborting.\n");
148 goto err_unlink_temp;
149 }
150
151 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
152 Alert("cannot make UNIX socket non-blocking. Aborting.\n");
153 goto err_unlink_temp;
154 }
155
156 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
157 /* note that bind() creates the socket <tempname> on the file system */
158 Alert("cannot bind socket for UNIX listener. Aborting.\n");
159 goto err_unlink_temp;
160 }
161
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200162 if (((uid != -1 || gid != -1) && (chown(tempname, uid, gid) == -1)) ||
163 (mode != 0 && chmod(tempname, mode) == -1)) {
164 Alert("cannot change UNIX socket ownership. Aborting.\n");
165 goto err_unlink_temp;
166 }
167
Willy Tarreau92fb9832007-10-16 17:34:28 +0200168 if (listen(sock, 0) < 0) {
169 Alert("cannot listen to socket for UNIX listener. Aborting.\n");
170 goto err_unlink_temp;
171 }
172
173 /* 5. install.
174 * Point of no return: we are ready, we'll switch the sockets. We don't
175 * fear loosing the socket <path> because we have a copy of it in
176 * backname.
177 */
178 if (rename(tempname, path) < 0) {
179 Alert("cannot switch final and temporary sockets for UNIX listener. Aborting.\n");
180 goto err_rename;
181 }
182
183 /* 6. cleanup */
184 unlink(backname); /* no need to keep this one either */
185
186 return sock;
187
188 err_rename:
189 ret = rename(backname, path);
190 if (ret < 0 && errno == ENOENT)
191 unlink(path);
192 err_unlink_temp:
193 unlink(tempname);
194 close(sock);
195 err_unlink_back:
196 unlink(backname);
197 err_return:
198 return -1;
199}
200
201/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
202 * anymore. It practises best effort, and no error is returned.
203 */
204static void destroy_uxst_socket(const char *path)
205{
206 struct sockaddr_un addr;
207 int sock, ret;
208
209 /* We might have been chrooted, so we may not be able to access the
210 * socket. In order to avoid bothering the other end, we connect with a
211 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
212 * is enough to know if the socket is still live or not. If it's live
213 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
214 * ECONNREFUSED. In this case, we do not touch it because it's used
215 * by some other process.
216 */
217 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
218 if (sock < 0)
219 return;
220
221 addr.sun_family = AF_UNIX;
222 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
Willy Tarreau10ae5482007-10-18 16:15:52 +0200223 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200224 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
225 if (ret < 0 && errno == ECONNREFUSED) {
226 /* Connect failed: the socket still exists but is not used
227 * anymore. Let's remove this socket now.
228 */
229 unlink(path);
230 }
231 close(sock);
232}
233
234
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100235/********************************
236 * 2) listener-oriented functions
237 ********************************/
238
239
240/* This function creates the UNIX socket associated to the listener. It changes
241 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
242 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
243 */
244static int uxst_bind_listener(struct listener *listener)
245{
246 int fd;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100247
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100248 if (listener->state != LI_ASSIGNED)
249 return ERR_NONE; /* already bound */
250
251 fd = create_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path,
252 listener->perm.ux.uid,
253 listener->perm.ux.gid,
254 listener->perm.ux.mode);
255 if (fd == -1)
256 return ERR_FATAL;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100257
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100258 /* the socket is now listening */
259 listener->fd = fd;
260 listener->state = LI_LISTEN;
261
262 /* the function for the accept() event */
263 fd_insert(fd);
264 fdtab[fd].cb[DIR_RD].f = listener->accept;
265 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
266 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200267 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100268 fdtab[fd].state = FD_STLISTEN;
269 fdtab[fd].peeraddr = NULL;
270 fdtab[fd].peerlen = 0;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100271 return ERR_NONE;
272}
273
274/* This function closes the UNIX sockets for the specified listener.
275 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
276 */
277static int uxst_unbind_listener(struct listener *listener)
278{
279 if (listener->state == LI_READY)
280 EV_FD_CLR(listener->fd, DIR_RD);
281
282 if (listener->state >= LI_LISTEN) {
Willy Tarreau8eebe5e2007-10-28 22:07:08 +0100283 fd_delete(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100284 listener->state = LI_ASSIGNED;
285 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
286 }
287 return ERR_NONE;
288}
289
290/* Add a listener to the list of unix stream listeners. The listener's state
291 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
292 * listeners is updated. This is the function to use to add a new listener.
293 */
294void uxst_add_listener(struct listener *listener)
295{
296 if (listener->state != LI_INIT)
297 return;
298 listener->state = LI_ASSIGNED;
299 listener->proto = &proto_unix;
300 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
301 proto_unix.nb_listeners++;
302}
303
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100304/********************************
305 * 3) protocol-oriented functions
306 ********************************/
307
308
Willy Tarreau92fb9832007-10-16 17:34:28 +0200309/* This function creates all UNIX sockets bound to the protocol entry <proto>.
310 * It is intended to be used as the protocol's bind_all() function.
311 * The sockets will be registered but not added to any fd_set, in order not to
312 * loose them across the fork(). A call to uxst_enable_listeners() is needed
313 * to complete initialization.
314 *
315 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
316 */
317static int uxst_bind_listeners(struct protocol *proto)
318{
319 struct listener *listener;
320 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200321
322 list_for_each_entry(listener, &proto->listeners, proto_list) {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100323 err |= uxst_bind_listener(listener);
324 if (err != ERR_NONE)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200325 continue;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200326 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200327 return err;
328}
329
Willy Tarreau92fb9832007-10-16 17:34:28 +0200330
331/* This function stops all listening UNIX sockets bound to the protocol
332 * <proto>. It does not detaches them from the protocol.
333 * It always returns ERR_NONE.
334 */
335static int uxst_unbind_listeners(struct protocol *proto)
336{
337 struct listener *listener;
338
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100339 list_for_each_entry(listener, &proto->listeners, proto_list)
340 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200341 return ERR_NONE;
342}
343
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100344
345/********************************
346 * 4) high-level functions
347 ********************************/
348
349
Willy Tarreau92fb9832007-10-16 17:34:28 +0200350/*
351 * This function is called on a read event from a listen socket, corresponding
352 * to an accept. It tries to accept as many connections as possible.
353 * It returns 0. Since we use UNIX sockets on the local system for monitoring
354 * purposes and other related things, we do not need to output as many messages
355 * as with TCP which can fall under attack.
356 */
357int uxst_event_accept(int fd) {
Willy Tarreaueabf3132008-08-29 23:36:51 +0200358 struct listener *l = fdtab[fd].owner;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200359 struct session *s;
360 struct task *t;
361 int cfd;
362 int max_accept;
363
364 if (global.nbproc > 1)
365 max_accept = 8; /* let other processes catch some connections too */
366 else
367 max_accept = -1;
368
Willy Tarreau2d045592009-03-28 11:02:18 +0100369 while (max_accept--) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200370 struct sockaddr_storage addr;
371 socklen_t laddr = sizeof(addr);
372
373 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
374 switch (errno) {
375 case EAGAIN:
376 case EINTR:
377 case ECONNABORTED:
378 return 0; /* nothing more to accept */
379 case ENFILE:
380 /* Process reached system FD limit. Check system tunables. */
381 return 0;
382 case EMFILE:
383 /* Process reached process FD limit. Check 'ulimit-n'. */
384 return 0;
385 case ENOBUFS:
386 case ENOMEM:
387 /* Process reached system memory limit. Check system tunables. */
388 return 0;
389 default:
390 return 0;
391 }
392 }
393
Willy Tarreau2d045592009-03-28 11:02:18 +0100394 if (l->nbconn >= l->maxconn || actconn >= global.maxconn) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200395 /* too many connections, we shoot this one and return.
396 * FIXME: it would be better to simply switch the listener's
397 * state to LI_FULL and disable the FD. We could re-enable
398 * it upon fd_delete(), but this requires all protocols to
399 * be switched.
400 */
Willy Tarreaua11e9762008-12-01 01:44:25 +0100401 goto out_close;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200402 }
403
404 if ((s = pool_alloc2(pool2_session)) == NULL) {
405 Alert("out of memory in uxst_event_accept().\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100406 goto out_close;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200407 }
408
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100409 LIST_ADDQ(&sessions, &s->list);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100410 LIST_INIT(&s->back_refs);
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100411
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100412 s->flags = 0;
Willy Tarreauf8533202008-08-16 14:55:08 +0200413 s->term_trace = 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100414
Willy Tarreaua4613182009-03-21 18:13:21 +0100415 if ((t = task_new()) == NULL) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200416 Alert("out of memory in uxst_event_accept().\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100417 goto out_free_session;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200418 }
419
420 s->cli_addr = addr;
421
422 /* FIXME: should be checked earlier */
423 if (cfd >= global.maxsock) {
424 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100425 goto out_free_task;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200426 }
427
428 if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) {
429 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100430 goto out_free_task;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200431 }
432
Willy Tarreau92fb9832007-10-16 17:34:28 +0200433 t->process = l->handler;
434 t->context = s;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200435 t->nice = l->nice;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200436
437 s->task = t;
Willy Tarreaub5654f62008-12-07 16:45:10 +0100438 s->listener = l;
Willy Tarreau89a63132009-08-16 17:41:45 +0200439 s->fe = s->be = l->private;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200440
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100441 s->ana_state = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200442 s->req = s->rep = NULL; /* will be allocated later */
443
Willy Tarreaua11e9762008-12-01 01:44:25 +0100444 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
445 s->si[0].err_type = SI_ET_NONE;
446 s->si[0].err_loc = NULL;
447 s->si[0].owner = t;
Willy Tarreaudc85b392009-08-18 07:38:19 +0200448 s->si[0].update = stream_sock_data_finish;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100449 s->si[0].shutr = stream_sock_shutr;
450 s->si[0].shutw = stream_sock_shutw;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100451 s->si[0].chk_rcv = stream_sock_chk_rcv;
452 s->si[0].chk_snd = stream_sock_chk_snd;
Willy Tarreaudc85b392009-08-18 07:38:19 +0200453 s->si[0].connect = NULL;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100454 s->si[0].fd = cfd;
455 s->si[0].flags = SI_FL_NONE;
456 s->si[0].exp = TICK_ETERNITY;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100457
458 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
459 s->si[1].err_type = SI_ET_NONE;
460 s->si[1].err_loc = NULL;
461 s->si[1].owner = t;
Willy Tarreaudc85b392009-08-18 07:38:19 +0200462 s->si[1].update = stream_sock_data_finish;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100463 s->si[1].shutr = stream_sock_shutr;
464 s->si[1].shutw = stream_sock_shutw;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100465 s->si[1].chk_rcv = stream_sock_chk_rcv;
466 s->si[1].chk_snd = stream_sock_chk_snd;
Willy Tarreaudc85b392009-08-18 07:38:19 +0200467 s->si[1].connect = NULL;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100468 s->si[1].exp = TICK_ETERNITY;
469 s->si[1].fd = -1; /* just to help with debugging */
470 s->si[1].flags = SI_FL_NONE;
471
472 s->srv = s->prev_srv = s->srv_conn = NULL;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200473 s->pend_pos = NULL;
474
475 memset(&s->logs, 0, sizeof(s->logs));
476 memset(&s->txn, 0, sizeof(s->txn));
477
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +0100478 s->logs.tv_accept = now; /* corrected date for internal use */
479
Willy Tarreau3e76e722007-10-17 18:57:38 +0200480 s->data_state = DATA_ST_INIT;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200481 s->data_source = DATA_SRC_NONE;
482 s->uniq_id = totalconn;
483
Willy Tarreaua11e9762008-12-01 01:44:25 +0100484 if ((s->req = pool_alloc2(pool2_buffer)) == NULL)
485 goto out_free_task;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200486
Willy Tarreau27a674e2009-08-17 07:23:33 +0200487 s->req->size = global.tune.bufsize;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100488 buffer_init(s->req);
Willy Tarreaua11e9762008-12-01 01:44:25 +0100489 s->req->prod = &s->si[0];
490 s->req->cons = &s->si[1];
491 s->si[0].ib = s->si[1].ob = s->req;
492 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100493 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaua11e9762008-12-01 01:44:25 +0100494
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100495 s->req->analysers = l->analysers;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100496
497 s->req->wto = TICK_ETERNITY;
498 s->req->cto = TICK_ETERNITY;
499 s->req->rto = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200500
Willy Tarreaua11e9762008-12-01 01:44:25 +0100501 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL)
502 goto out_free_req;
503
Willy Tarreau27a674e2009-08-17 07:23:33 +0200504 s->rep->size = global.tune.bufsize;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200505 buffer_init(s->rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200506
Willy Tarreaua11e9762008-12-01 01:44:25 +0100507 s->rep->prod = &s->si[1];
508 s->rep->cons = &s->si[0];
509 s->si[0].ob = s->si[1].ib = s->rep;
510
511 s->rep->rto = TICK_ETERNITY;
512 s->rep->cto = TICK_ETERNITY;
513 s->rep->wto = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200514
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200515 s->req->rex = TICK_ETERNITY;
516 s->req->wex = TICK_ETERNITY;
Willy Tarreauffab5b42008-08-17 18:03:28 +0200517 s->req->analyse_exp = TICK_ETERNITY;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200518 s->rep->rex = TICK_ETERNITY;
519 s->rep->wex = TICK_ETERNITY;
Willy Tarreauffab5b42008-08-17 18:03:28 +0200520 s->rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200521
Willy Tarreaua11e9762008-12-01 01:44:25 +0100522 t->expire = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200523
Willy Tarreaua11e9762008-12-01 01:44:25 +0100524 if (l->timeout) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200525 s->req->rto = *l->timeout;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200526 s->rep->wto = *l->timeout;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200527 }
528
Willy Tarreaua11e9762008-12-01 01:44:25 +0100529 fd_insert(cfd);
530 fdtab[cfd].owner = &s->si[0];
Willy Tarreaua11e9762008-12-01 01:44:25 +0100531 fdtab[cfd].state = FD_STREADY;
532 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
533 fdtab[cfd].cb[DIR_RD].b = s->req;
534 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
535 fdtab[cfd].cb[DIR_WR].b = s->rep;
536 fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
537 fdtab[cfd].peerlen = sizeof(s->cli_addr);
538
539 EV_FD_SET(cfd, DIR_RD);
540
Willy Tarreaufdccded2008-08-29 18:19:04 +0200541 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200542
543 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
544 if (l->nbconn >= l->maxconn) {
545 EV_FD_CLR(l->fd, DIR_RD);
546 l->state = LI_FULL;
547 }
548 actconn++;
549 totalconn++;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100550 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200551 return 0;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100552
553 out_free_req:
554 pool_free2(pool2_buffer, s->req);
555 out_free_task:
Willy Tarreaua4613182009-03-21 18:13:21 +0100556 task_free(t);
Willy Tarreaua11e9762008-12-01 01:44:25 +0100557 out_free_session:
558 LIST_DEL(&s->list);
559 pool_free2(pool2_session, s);
560 out_close:
561 close(cfd);
562 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200563}
564
Willy Tarreau92fb9832007-10-16 17:34:28 +0200565__attribute__((constructor))
566static void __uxst_protocol_init(void)
567{
568 protocol_register(&proto_unix);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200569}
570
571
572/*
573 * Local variables:
574 * c-indent-level: 8
575 * c-basic-offset: 8
576 * End:
577 */