blob: b3bf7545cd3ef6e7df7d6046901d9bd3907ea723 [file] [log] [blame]
Willy Tarreau92fb9832007-10-16 17:34:28 +02001/*
2 * UNIX SOCK_STREAM protocol layer (uxst)
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 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>
Willy Tarreau3e76e722007-10-17 18:57:38 +020044#include <proto/dumpstats.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020045#include <proto/fd.h>
46#include <proto/log.h>
47#include <proto/protocols.h>
48#include <proto/proto_uxst.h>
49#include <proto/queue.h>
50#include <proto/session.h>
Willy Tarreaub1356cf2008-12-07 16:06:43 +010051#include <proto/stream_interface.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020052#include <proto/stream_sock.h>
53#include <proto/task.h>
54
55#ifndef MAXPATHLEN
56#define MAXPATHLEN 128
57#endif
58
Willy Tarreaudabf2e22007-10-28 21:59:24 +010059static int uxst_bind_listeners(struct protocol *proto);
60static int uxst_unbind_listeners(struct protocol *proto);
61
62/* Note: must not be declared <const> as its list will be overwritten */
63static struct protocol proto_unix = {
64 .name = "unix_stream",
65 .sock_domain = PF_UNIX,
66 .sock_type = SOCK_STREAM,
67 .sock_prot = 0,
68 .sock_family = AF_UNIX,
69 .sock_addrlen = sizeof(struct sockaddr_un),
70 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
71 .read = &stream_sock_read,
72 .write = &stream_sock_write,
73 .bind_all = uxst_bind_listeners,
74 .unbind_all = uxst_unbind_listeners,
75 .enable_all = enable_all_listeners,
76 .disable_all = disable_all_listeners,
77 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
78 .nb_listeners = 0,
79};
80
81
82/********************************
83 * 1) low-level socket functions
84 ********************************/
85
86
Willy Tarreau92fb9832007-10-16 17:34:28 +020087/* This function creates a named PF_UNIX stream socket at address <path>. Note
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020088 * that the path cannot be NULL nor empty. <uid> and <gid> different of -1 will
89 * be used to change the socket owner. If <mode> is not 0, it will be used to
90 * restrict access to the socket. While it is known not to be portable on every
91 * OS, it's still useful where it works.
Willy Tarreau92fb9832007-10-16 17:34:28 +020092 * It returns the assigned file descriptor, or -1 in the event of an error.
93 */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020094static int create_uxst_socket(const char *path, uid_t uid, gid_t gid, mode_t mode)
Willy Tarreau92fb9832007-10-16 17:34:28 +020095{
96 char tempname[MAXPATHLEN];
97 char backname[MAXPATHLEN];
98 struct sockaddr_un addr;
99
100 int ret, sock;
101
102 /* 1. create socket names */
103 if (!path[0]) {
104 Alert("Invalid name for a UNIX socket. Aborting.\n");
105 goto err_return;
106 }
107
108 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
109 if (ret < 0 || ret >= MAXPATHLEN) {
110 Alert("name too long for UNIX socket. Aborting.\n");
111 goto err_return;
112 }
113
114 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
115 if (ret < 0 || ret >= MAXPATHLEN) {
116 Alert("name too long for UNIX socket. Aborting.\n");
117 goto err_return;
118 }
119
120 /* 2. clean existing orphaned entries */
121 if (unlink(tempname) < 0 && errno != ENOENT) {
122 Alert("error when trying to unlink previous UNIX socket. Aborting.\n");
123 goto err_return;
124 }
125
126 if (unlink(backname) < 0 && errno != ENOENT) {
127 Alert("error when trying to unlink previous UNIX socket. Aborting.\n");
128 goto err_return;
129 }
130
131 /* 3. backup existing socket */
132 if (link(path, backname) < 0 && errno != ENOENT) {
133 Alert("error when trying to preserve previous UNIX socket. Aborting.\n");
134 goto err_return;
135 }
136
137 /* 4. prepare new socket */
138 addr.sun_family = AF_UNIX;
139 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
140 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
141
142 sock = socket(PF_UNIX, SOCK_STREAM, 0);
143 if (sock < 0) {
144 Alert("cannot create socket for UNIX listener. Aborting.\n");
145 goto err_unlink_back;
146 }
147
148 if (sock >= global.maxsock) {
149 Alert("socket(): not enough free sockets for UNIX listener. Raise -n argument. Aborting.\n");
150 goto err_unlink_temp;
151 }
152
153 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
154 Alert("cannot make UNIX socket non-blocking. Aborting.\n");
155 goto err_unlink_temp;
156 }
157
158 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
159 /* note that bind() creates the socket <tempname> on the file system */
160 Alert("cannot bind socket for UNIX listener. Aborting.\n");
161 goto err_unlink_temp;
162 }
163
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200164 if (((uid != -1 || gid != -1) && (chown(tempname, uid, gid) == -1)) ||
165 (mode != 0 && chmod(tempname, mode) == -1)) {
166 Alert("cannot change UNIX socket ownership. Aborting.\n");
167 goto err_unlink_temp;
168 }
169
Willy Tarreau92fb9832007-10-16 17:34:28 +0200170 if (listen(sock, 0) < 0) {
171 Alert("cannot listen to socket for UNIX listener. Aborting.\n");
172 goto err_unlink_temp;
173 }
174
175 /* 5. install.
176 * Point of no return: we are ready, we'll switch the sockets. We don't
177 * fear loosing the socket <path> because we have a copy of it in
178 * backname.
179 */
180 if (rename(tempname, path) < 0) {
181 Alert("cannot switch final and temporary sockets for UNIX listener. Aborting.\n");
182 goto err_rename;
183 }
184
185 /* 6. cleanup */
186 unlink(backname); /* no need to keep this one either */
187
188 return sock;
189
190 err_rename:
191 ret = rename(backname, path);
192 if (ret < 0 && errno == ENOENT)
193 unlink(path);
194 err_unlink_temp:
195 unlink(tempname);
196 close(sock);
197 err_unlink_back:
198 unlink(backname);
199 err_return:
200 return -1;
201}
202
203/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
204 * anymore. It practises best effort, and no error is returned.
205 */
206static void destroy_uxst_socket(const char *path)
207{
208 struct sockaddr_un addr;
209 int sock, ret;
210
211 /* We might have been chrooted, so we may not be able to access the
212 * socket. In order to avoid bothering the other end, we connect with a
213 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
214 * is enough to know if the socket is still live or not. If it's live
215 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
216 * ECONNREFUSED. In this case, we do not touch it because it's used
217 * by some other process.
218 */
219 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
220 if (sock < 0)
221 return;
222
223 addr.sun_family = AF_UNIX;
224 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
Willy Tarreau10ae5482007-10-18 16:15:52 +0200225 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200226 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
227 if (ret < 0 && errno == ECONNREFUSED) {
228 /* Connect failed: the socket still exists but is not used
229 * anymore. Let's remove this socket now.
230 */
231 unlink(path);
232 }
233 close(sock);
234}
235
236
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100237/********************************
238 * 2) listener-oriented functions
239 ********************************/
240
241
242/* This function creates the UNIX socket associated to the listener. It changes
243 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
244 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
245 */
246static int uxst_bind_listener(struct listener *listener)
247{
248 int fd;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100249
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100250 if (listener->state != LI_ASSIGNED)
251 return ERR_NONE; /* already bound */
252
253 fd = create_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path,
254 listener->perm.ux.uid,
255 listener->perm.ux.gid,
256 listener->perm.ux.mode);
257 if (fd == -1)
258 return ERR_FATAL;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100259
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100260 /* the socket is now listening */
261 listener->fd = fd;
262 listener->state = LI_LISTEN;
263
264 /* the function for the accept() event */
265 fd_insert(fd);
266 fdtab[fd].cb[DIR_RD].f = listener->accept;
267 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
268 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200269 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100270 fdtab[fd].state = FD_STLISTEN;
271 fdtab[fd].peeraddr = NULL;
272 fdtab[fd].peerlen = 0;
273 fdtab[fd].listener = NULL;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100274 return ERR_NONE;
275}
276
277/* This function closes the UNIX sockets for the specified listener.
278 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
279 */
280static int uxst_unbind_listener(struct listener *listener)
281{
282 if (listener->state == LI_READY)
283 EV_FD_CLR(listener->fd, DIR_RD);
284
285 if (listener->state >= LI_LISTEN) {
Willy Tarreau8eebe5e2007-10-28 22:07:08 +0100286 fd_delete(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100287 listener->state = LI_ASSIGNED;
288 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
289 }
290 return ERR_NONE;
291}
292
293/* Add a listener to the list of unix stream listeners. The listener's state
294 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
295 * listeners is updated. This is the function to use to add a new listener.
296 */
297void uxst_add_listener(struct listener *listener)
298{
299 if (listener->state != LI_INIT)
300 return;
301 listener->state = LI_ASSIGNED;
302 listener->proto = &proto_unix;
303 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
304 proto_unix.nb_listeners++;
305}
306
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100307/********************************
308 * 3) protocol-oriented functions
309 ********************************/
310
311
Willy Tarreau92fb9832007-10-16 17:34:28 +0200312/* This function creates all UNIX sockets bound to the protocol entry <proto>.
313 * It is intended to be used as the protocol's bind_all() function.
314 * The sockets will be registered but not added to any fd_set, in order not to
315 * loose them across the fork(). A call to uxst_enable_listeners() is needed
316 * to complete initialization.
317 *
318 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
319 */
320static int uxst_bind_listeners(struct protocol *proto)
321{
322 struct listener *listener;
323 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200324
325 list_for_each_entry(listener, &proto->listeners, proto_list) {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100326 err |= uxst_bind_listener(listener);
327 if (err != ERR_NONE)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200328 continue;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200329 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200330 return err;
331}
332
Willy Tarreau92fb9832007-10-16 17:34:28 +0200333
334/* This function stops all listening UNIX sockets bound to the protocol
335 * <proto>. It does not detaches them from the protocol.
336 * It always returns ERR_NONE.
337 */
338static int uxst_unbind_listeners(struct protocol *proto)
339{
340 struct listener *listener;
341
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100342 list_for_each_entry(listener, &proto->listeners, proto_list)
343 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200344 return ERR_NONE;
345}
346
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100347
348/********************************
349 * 4) high-level functions
350 ********************************/
351
352
Willy Tarreau92fb9832007-10-16 17:34:28 +0200353/*
354 * This function is called on a read event from a listen socket, corresponding
355 * to an accept. It tries to accept as many connections as possible.
356 * It returns 0. Since we use UNIX sockets on the local system for monitoring
357 * purposes and other related things, we do not need to output as many messages
358 * as with TCP which can fall under attack.
359 */
360int uxst_event_accept(int fd) {
Willy Tarreaueabf3132008-08-29 23:36:51 +0200361 struct listener *l = fdtab[fd].owner;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200362 struct session *s;
363 struct task *t;
364 int cfd;
365 int max_accept;
366
367 if (global.nbproc > 1)
368 max_accept = 8; /* let other processes catch some connections too */
369 else
370 max_accept = -1;
371
372 while (max_accept--) {
373 struct sockaddr_storage addr;
374 socklen_t laddr = sizeof(addr);
375
376 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
377 switch (errno) {
378 case EAGAIN:
379 case EINTR:
380 case ECONNABORTED:
381 return 0; /* nothing more to accept */
382 case ENFILE:
383 /* Process reached system FD limit. Check system tunables. */
384 return 0;
385 case EMFILE:
386 /* Process reached process FD limit. Check 'ulimit-n'. */
387 return 0;
388 case ENOBUFS:
389 case ENOMEM:
390 /* Process reached system memory limit. Check system tunables. */
391 return 0;
392 default:
393 return 0;
394 }
395 }
396
397 if (l->nbconn >= l->maxconn) {
398 /* too many connections, we shoot this one and return.
399 * FIXME: it would be better to simply switch the listener's
400 * state to LI_FULL and disable the FD. We could re-enable
401 * it upon fd_delete(), but this requires all protocols to
402 * be switched.
403 */
Willy Tarreaua11e9762008-12-01 01:44:25 +0100404 goto out_close;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200405 }
406
407 if ((s = pool_alloc2(pool2_session)) == NULL) {
408 Alert("out of memory in uxst_event_accept().\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100409 goto out_close;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200410 }
411
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100412 LIST_ADDQ(&sessions, &s->list);
413
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100414 s->flags = 0;
Willy Tarreauf8533202008-08-16 14:55:08 +0200415 s->term_trace = 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100416
Willy Tarreau92fb9832007-10-16 17:34:28 +0200417 if ((t = pool_alloc2(pool2_task)) == NULL) {
418 Alert("out of memory in uxst_event_accept().\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100419 goto out_free_session;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200420 }
421
422 s->cli_addr = addr;
423
424 /* FIXME: should be checked earlier */
425 if (cfd >= global.maxsock) {
426 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100427 goto out_free_task;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200428 }
429
430 if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) {
431 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100432 goto out_free_task;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200433 }
434
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200435 task_init(t);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200436 t->process = l->handler;
437 t->context = s;
Willy Tarreau91e99932008-06-30 07:51:00 +0200438 t->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200439
440 s->task = t;
441 s->fe = NULL;
442 s->be = NULL;
443
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100444 s->ana_state = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200445 s->req = s->rep = NULL; /* will be allocated later */
446
Willy Tarreaua11e9762008-12-01 01:44:25 +0100447 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
448 s->si[0].err_type = SI_ET_NONE;
449 s->si[0].err_loc = NULL;
450 s->si[0].owner = t;
451 s->si[0].shutr = stream_sock_shutr;
452 s->si[0].shutw = stream_sock_shutw;
453 s->si[0].fd = cfd;
454 s->si[0].flags = SI_FL_NONE;
455 s->si[0].exp = TICK_ETERNITY;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100456
457 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
458 s->si[1].err_type = SI_ET_NONE;
459 s->si[1].err_loc = NULL;
460 s->si[1].owner = t;
461 s->si[1].shutr = stream_sock_shutr;
462 s->si[1].shutw = stream_sock_shutw;
463 s->si[1].exp = TICK_ETERNITY;
464 s->si[1].fd = -1; /* just to help with debugging */
465 s->si[1].flags = SI_FL_NONE;
466
467 s->srv = s->prev_srv = s->srv_conn = NULL;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200468 s->pend_pos = NULL;
469
470 memset(&s->logs, 0, sizeof(s->logs));
471 memset(&s->txn, 0, sizeof(s->txn));
472
Willy Tarreau3e76e722007-10-17 18:57:38 +0200473 s->data_state = DATA_ST_INIT;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200474 s->data_source = DATA_SRC_NONE;
475 s->uniq_id = totalconn;
476
Willy Tarreaua11e9762008-12-01 01:44:25 +0100477 if ((s->req = pool_alloc2(pool2_buffer)) == NULL)
478 goto out_free_task;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200479
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100480 buffer_init(s->req);
Willy Tarreaua11e9762008-12-01 01:44:25 +0100481 s->req->prod = &s->si[0];
482 s->req->cons = &s->si[1];
483 s->si[0].ib = s->si[1].ob = s->req;
484 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
485
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100486 s->req->analysers = l->analysers;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100487
488 s->req->wto = TICK_ETERNITY;
489 s->req->cto = TICK_ETERNITY;
490 s->req->rto = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200491
Willy Tarreaua11e9762008-12-01 01:44:25 +0100492 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL)
493 goto out_free_req;
494
Willy Tarreau92fb9832007-10-16 17:34:28 +0200495 buffer_init(s->rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200496
Willy Tarreaua11e9762008-12-01 01:44:25 +0100497 s->rep->prod = &s->si[1];
498 s->rep->cons = &s->si[0];
499 s->si[0].ob = s->si[1].ib = s->rep;
500
501 s->rep->rto = TICK_ETERNITY;
502 s->rep->cto = TICK_ETERNITY;
503 s->rep->wto = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200504
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200505 s->req->rex = TICK_ETERNITY;
506 s->req->wex = TICK_ETERNITY;
Willy Tarreauffab5b42008-08-17 18:03:28 +0200507 s->req->analyse_exp = TICK_ETERNITY;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200508 s->rep->rex = TICK_ETERNITY;
509 s->rep->wex = TICK_ETERNITY;
Willy Tarreauffab5b42008-08-17 18:03:28 +0200510 s->rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200511
Willy Tarreaua11e9762008-12-01 01:44:25 +0100512 t->expire = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200513
Willy Tarreaua11e9762008-12-01 01:44:25 +0100514 if (l->timeout) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200515 s->req->rto = *l->timeout;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200516 s->rep->wto = *l->timeout;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200517 }
518
Willy Tarreaua11e9762008-12-01 01:44:25 +0100519 fd_insert(cfd);
520 fdtab[cfd].owner = &s->si[0];
521 fdtab[cfd].listener = l;
522 fdtab[cfd].state = FD_STREADY;
523 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
524 fdtab[cfd].cb[DIR_RD].b = s->req;
525 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
526 fdtab[cfd].cb[DIR_WR].b = s->rep;
527 fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
528 fdtab[cfd].peerlen = sizeof(s->cli_addr);
529
530 EV_FD_SET(cfd, DIR_RD);
531
Willy Tarreaufdccded2008-08-29 18:19:04 +0200532 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200533
534 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
535 if (l->nbconn >= l->maxconn) {
536 EV_FD_CLR(l->fd, DIR_RD);
537 l->state = LI_FULL;
538 }
539 actconn++;
540 totalconn++;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100541 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200542 return 0;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100543
544 out_free_req:
545 pool_free2(pool2_buffer, s->req);
546 out_free_task:
547 pool_free2(pool2_task, t);
548 out_free_session:
549 LIST_DEL(&s->list);
550 pool_free2(pool2_session, s);
551 out_close:
552 close(cfd);
553 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200554}
555
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100556/* Parses the request line in <cmd> and possibly starts dumping stats on
557 * s->rep with the hijack bit set. Returns 1 if OK, 0 in case of any error.
558 * The line is modified after parsing.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200559 */
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100560int unix_sock_parse_request(struct session *s, char *line)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200561{
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100562 char *args[MAX_UXST_ARGS + 1];
563 int arg;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200564
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100565 while (isspace((unsigned char)*line))
566 line++;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200567
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100568 arg = 0;
569 args[arg] = line;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200570
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100571 while (*line && arg < MAX_UXST_ARGS) {
572 if (isspace((unsigned char)*line)) {
573 *line++ = '\0';
Willy Tarreau92fb9832007-10-16 17:34:28 +0200574
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100575 while (isspace((unsigned char)*line))
576 line++;
577
578 args[++arg] = line;
579 continue;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200580 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100581
582 line++;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200583 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200584
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100585 while (++arg <= MAX_UXST_ARGS)
586 args[arg] = line;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200587
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100588 if (strcmp(args[0], "show") == 0) {
589 if (strcmp(args[1], "stat") == 0) {
590 if (*args[2] && *args[3] && *args[4]) {
591 s->data_ctx.stats.flags |= STAT_BOUND;
592 s->data_ctx.stats.iid = atoi(args[2]);
593 s->data_ctx.stats.type = atoi(args[3]);
594 s->data_ctx.stats.sid = atoi(args[4]);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200595 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100596
597 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
598 s->data_ctx.stats.flags |= STAT_FMT_CSV;
599 s->ana_state = STATS_ST_REP;
600 buffer_start_hijack(s->rep);
601 stats_dump_raw_to_buffer(s, s->rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200602 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100603 else if (strcmp(args[1], "info") == 0) {
604 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
605 s->data_ctx.stats.flags |= STAT_FMT_CSV;
606 s->ana_state = STATS_ST_REP;
607 buffer_start_hijack(s->rep);
608 stats_dump_raw_to_buffer(s, s->rep);
609 }
610 else { /* neither "stat" nor "info" */
611 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200612 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100613 }
614 else { /* not "show" */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200615 return 0;
616 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100617 return 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200618}
619
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100620/* Processes the stats interpreter on the statistics socket.
621 * In order to ease the transition, we simply simulate the server status
622 * for now. It only knows states STATS_ST_INIT, STATS_ST_REQ, STATS_ST_REP, and
623 * STATS_ST_CLOSE. It removes the AN_REQ_UNIX_STATS bit from req->analysers
624 * once done. It always returns 0.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200625 */
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100626int uxst_req_analyser_stats(struct session *s, struct buffer *req)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200627{
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100628 char *line, *p;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200629
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100630 switch (s->ana_state) {
631 case STATS_ST_INIT:
632 /* Stats output not initialized yet */
633 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
634 s->data_source = DATA_SRC_STATS;
635 s->ana_state = STATS_ST_REQ;
636 /* fall through */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200637
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100638 case STATS_ST_REQ:
639 /* Now, stats are initialized, hijack is not set, and
640 * we are waiting for a complete request line.
641 */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200642
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100643 line = s->req->data;
644 p = memchr(line, '\n', s->req->l);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200645
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100646 if (p) {
647 *p = '\0';
648 if (!unix_sock_parse_request(s, line)) {
649 /* invalid request */
650 buffer_shutw_now(s->req);
651 s->ana_state = 0;
652 req->analysers = 0;
653 return 0;
654 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200655 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200656
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100657 /* processing a valid or incomplete request */
658 if ((req->flags & BF_FULL) || /* invalid request */
659 (req->flags & BF_READ_ERROR) || /* input error */
660 (req->flags & BF_READ_TIMEOUT) || /* read timeout */
661 tick_is_expired(req->analyse_exp, now_ms) || /* request timeout */
662 (req->flags & BF_SHUTR)) { /* input closed */
663 buffer_shutw_now(s->req);
664 s->ana_state = 0;
665 req->analysers = 0;
666 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200667 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200668
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100669 /* don't forward nor abort */
670 buffer_write_dis(req);
671 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200672
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100673 case STATS_ST_REP:
674 /* do nothing while response is being processed */
675 buffer_write_dis(s->req);
676 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200677
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100678 case STATS_ST_CLOSE:
679 /* end of dump */
680 s->req->analysers &= ~AN_REQ_UNIX_STATS;
681 s->ana_state = 0;
682 break;
683 }
684 return 0;
685}
Willy Tarreau92fb9832007-10-16 17:34:28 +0200686
Willy Tarreau92fb9832007-10-16 17:34:28 +0200687
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100688/* This function is the unix-stream equivalent of the global process_session().
689 * It is currently limited to unix-stream processing on control sockets such as
690 * stats, and has no server-side. The two functions should be merged into one
691 * once client and server sides are better delimited. Note that the server-side
692 * still exists but remains in SI_ST_INI state forever, so that any call is a
693 * NOP.
694 */
695void uxst_process_session(struct task *t, int *next)
696{
697 struct session *s = t->context;
698 struct listener *listener;
699 int resync;
700 unsigned int rqf_last, rpf_last;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200701
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100702 /* 1a: Check for low level timeouts if needed. We just set a flag on
703 * stream interfaces when their timeouts have expired.
704 */
705 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
706 stream_int_check_timeouts(&s->si[0]);
707 buffer_check_timeouts(s->req);
708 buffer_check_timeouts(s->rep);
709 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200710
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100711 /* copy req/rep flags so that we can detect shutdowns */
712 rqf_last = s->req->flags;
713 rpf_last = s->rep->flags;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200714
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100715 /* 1b: check for low-level errors reported at the stream interface. */
716 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
717 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
718 s->si[0].shutr(&s->si[0]);
719 s->si[0].shutw(&s->si[0]);
720 stream_int_report_error(&s->si[0]);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200721 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100722 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200723
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100724 /* check buffer timeouts, and close the corresponding stream interfaces
725 * for future reads or writes. Note: this will also concern upper layers
726 * but we do not touch any other flag. We must be careful and correctly
727 * detect state changes when calling them.
728 */
729 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
730 if (s->req->flags & BF_READ_TIMEOUT)
731 s->req->prod->shutr(s->req->prod);
732 if (s->req->flags & BF_WRITE_TIMEOUT)
733 s->req->cons->shutw(s->req->cons);
734 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200735
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100736 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
737 if (s->rep->flags & BF_READ_TIMEOUT)
738 s->rep->prod->shutr(s->rep->prod);
739 if (s->rep->flags & BF_WRITE_TIMEOUT)
740 s->rep->cons->shutw(s->rep->cons);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200741 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200742
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100743 /* Check for connection closure */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200744
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100745 resync_stream_interface:
Willy Tarreau92fb9832007-10-16 17:34:28 +0200746
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100747 /* nothing special to be done on client side */
748 if (unlikely(s->req->prod->state == SI_ST_DIS))
749 s->req->prod->state = SI_ST_CLO;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200750
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100751 /*
752 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
753 * at this point.
754 */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200755
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100756 /**** Process layer 7 below ****/
Willy Tarreau92fb9832007-10-16 17:34:28 +0200757
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100758 resync = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200759
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100760 /* Analyse request */
761 if ((s->req->flags & BF_MASK_ANALYSER) ||
762 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
763 unsigned int flags = s->req->flags;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200764
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100765 if (s->req->prod->state >= SI_ST_EST) {
766 /* it's up to the analysers to reset write_ena */
767 buffer_write_ena(s->req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200768
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100769 /* We will call all analysers for which a bit is set in
770 * s->req->analysers, following the bit order from LSB
771 * to MSB. The analysers must remove themselves from
772 * the list when not needed. This while() loop is in
773 * fact a cleaner if().
Willy Tarreau92fb9832007-10-16 17:34:28 +0200774 */
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100775 while (s->req->analysers) {
776 if (s->req->analysers & AN_REQ_UNIX_STATS)
777 if (!uxst_req_analyser_stats(s, s->req))
778 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200779
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100780 /* Just make sure that nobody set a wrong flag causing an endless loop */
781 s->req->analysers &= AN_REQ_UNIX_STATS;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200782
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100783 /* we don't want to loop anyway */
784 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200785 }
786 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100787 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
788 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
789 if (s->req->flags != flags)
790 resync = 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200791 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200792
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100793 /* reflect what the L7 analysers have seen last */
794 rqf_last = s->req->flags;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200795
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100796 /*
797 * Now forward all shutdown requests between both sides of the buffer
798 */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200799
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100800 /* first, let's check if the request buffer needs to shutdown(write) */
801 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
802 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
803 buffer_shutw_now(s->req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200804
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100805 /* shutdown(write) pending */
806 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
807 s->req->cons->shutw(s->req->cons);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200808
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100809 /* shutdown(write) done on server side, we must stop the client too */
810 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
811 !s->req->analysers))
812 buffer_shutr_now(s->req);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100813
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100814 /* shutdown(read) pending */
815 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
816 s->req->prod->shutr(s->req->prod);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100817
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100818 /*
819 * Here we want to check if we need to resync or not.
820 */
821 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
822 resync = 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200823
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100824 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200825
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100826 /* according to benchmarks, it makes sense to resync now */
827 if (resync)
828 goto resync_stream_interface;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200829
Willy Tarreau92fb9832007-10-16 17:34:28 +0200830
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100831 /* Analyse response */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200832
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100833 buffer_write_ena(s->rep);
834 if (unlikely(s->rep->flags & BF_HIJACK)) {
835 /* In inject mode, we wake up everytime something has
836 * happened on the write side of the buffer.
837 */
838 unsigned int flags = s->rep->flags;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200839
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100840 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
841 !(s->rep->flags & BF_FULL)) {
842 /* it is the only hijacker right now */
843 stats_dump_raw_to_buffer(s, s->rep);
844 }
845 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
846 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
847 if (s->rep->flags != flags)
848 resync = 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200849 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100850 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
851 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
852 unsigned int flags = s->rep->flags;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200853
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100854 if (s->rep->prod->state >= SI_ST_EST) {
855 /* it's up to the analysers to reset write_ena */
856 buffer_write_ena(s->rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200857 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100858 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
859 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
860 if (s->rep->flags != flags)
861 resync = 1;
862 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100863
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100864 /* reflect what the L7 analysers have seen last */
865 rpf_last = s->rep->flags;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100866
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100867 /*
868 * Now forward all shutdown requests between both sides of the buffer
869 */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100870
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100871 /*
872 * FIXME: this is probably where we should produce error responses.
873 */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100874
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100875 /* first, let's check if the request buffer needs to shutdown(write) */
876 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
877 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
878 buffer_shutw_now(s->rep);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100879
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100880 /* shutdown(write) pending */
881 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
882 s->rep->cons->shutw(s->rep->cons);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100883
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100884 /* shutdown(write) done on the client side, we must stop the server too */
885 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW))
886 buffer_shutr_now(s->rep);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100887
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100888 /* shutdown(read) pending */
889 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
890 s->rep->prod->shutr(s->rep->prod);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100891
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100892 /*
893 * Here we want to check if we need to resync or not.
894 */
895 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
896 resync = 1;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100897
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100898 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100899
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100900 if (resync)
901 goto resync_stream_interface;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100902
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100903 if (likely(s->rep->cons->state != SI_ST_CLO)) {
904 if (s->rep->cons->state == SI_ST_EST)
905 stream_sock_data_finish(s->rep->cons);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200906
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100907 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
908 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
909 s->si[0].prev_state = s->si[0].state;
910 s->si[0].flags = SI_FL_NONE;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200911
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100912 /* Trick: if a request is being waiting for the server to respond,
913 * and if we know the server can timeout, we don't want the timeout
914 * to expire on the client side first, but we're still interested
915 * in passing data from the client to the server (eg: POST). Thus,
916 * we can cancel the client's request timeout if the server's
917 * request timeout is set and the server has not yet sent a response.
918 */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200919
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100920 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
921 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
922 s->req->rex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200923
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200924 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
925 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100926 if (s->req->analysers)
927 t->expire = tick_first(t->expire, s->req->analyse_exp);
928
929 if (s->si[0].exp)
930 t->expire = tick_first(t->expire, s->si[0].exp);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200931
932 /* restore t to its place in the task list */
933 task_queue(t);
934
935 *next = t->expire;
936 return; /* nothing more to do */
937 }
938
939 actconn--;
Willy Tarreau7e5067d2008-12-07 16:27:56 +0100940 listener = fdtab[s->si[0].fd].listener;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200941 if (listener) {
942 listener->nbconn--;
943 if (listener->state == LI_FULL &&
944 listener->nbconn < listener->maxconn) {
945 /* we should reactivate the listener */
946 EV_FD_SET(listener->fd, DIR_RD);
947 listener->state = LI_READY;
948 }
949 }
950
951 /* the task MUST not be in the run queue anymore */
952 task_delete(t);
953 session_free(s);
954 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200955 *next = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200956}
957
Willy Tarreau92fb9832007-10-16 17:34:28 +0200958__attribute__((constructor))
959static void __uxst_protocol_init(void)
960{
961 protocol_register(&proto_unix);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200962}
963
964
965/*
966 * Local variables:
967 * c-indent-level: 8
968 * c-basic-offset: 8
969 * End:
970 */