blob: d7567509e00d06535104d8896e9920854bad4df9 [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;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100273 return ERR_NONE;
274}
275
276/* This function closes the UNIX sockets for the specified listener.
277 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
278 */
279static int uxst_unbind_listener(struct listener *listener)
280{
281 if (listener->state == LI_READY)
282 EV_FD_CLR(listener->fd, DIR_RD);
283
284 if (listener->state >= LI_LISTEN) {
Willy Tarreau8eebe5e2007-10-28 22:07:08 +0100285 fd_delete(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100286 listener->state = LI_ASSIGNED;
287 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
288 }
289 return ERR_NONE;
290}
291
292/* Add a listener to the list of unix stream listeners. The listener's state
293 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
294 * listeners is updated. This is the function to use to add a new listener.
295 */
296void uxst_add_listener(struct listener *listener)
297{
298 if (listener->state != LI_INIT)
299 return;
300 listener->state = LI_ASSIGNED;
301 listener->proto = &proto_unix;
302 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
303 proto_unix.nb_listeners++;
304}
305
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100306/********************************
307 * 3) protocol-oriented functions
308 ********************************/
309
310
Willy Tarreau92fb9832007-10-16 17:34:28 +0200311/* This function creates all UNIX sockets bound to the protocol entry <proto>.
312 * It is intended to be used as the protocol's bind_all() function.
313 * The sockets will be registered but not added to any fd_set, in order not to
314 * loose them across the fork(). A call to uxst_enable_listeners() is needed
315 * to complete initialization.
316 *
317 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
318 */
319static int uxst_bind_listeners(struct protocol *proto)
320{
321 struct listener *listener;
322 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200323
324 list_for_each_entry(listener, &proto->listeners, proto_list) {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100325 err |= uxst_bind_listener(listener);
326 if (err != ERR_NONE)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200327 continue;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200328 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200329 return err;
330}
331
Willy Tarreau92fb9832007-10-16 17:34:28 +0200332
333/* This function stops all listening UNIX sockets bound to the protocol
334 * <proto>. It does not detaches them from the protocol.
335 * It always returns ERR_NONE.
336 */
337static int uxst_unbind_listeners(struct protocol *proto)
338{
339 struct listener *listener;
340
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100341 list_for_each_entry(listener, &proto->listeners, proto_list)
342 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200343 return ERR_NONE;
344}
345
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100346
347/********************************
348 * 4) high-level functions
349 ********************************/
350
351
Willy Tarreau92fb9832007-10-16 17:34:28 +0200352/*
353 * This function is called on a read event from a listen socket, corresponding
354 * to an accept. It tries to accept as many connections as possible.
355 * It returns 0. Since we use UNIX sockets on the local system for monitoring
356 * purposes and other related things, we do not need to output as many messages
357 * as with TCP which can fall under attack.
358 */
359int uxst_event_accept(int fd) {
Willy Tarreaueabf3132008-08-29 23:36:51 +0200360 struct listener *l = fdtab[fd].owner;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200361 struct session *s;
362 struct task *t;
363 int cfd;
364 int max_accept;
365
366 if (global.nbproc > 1)
367 max_accept = 8; /* let other processes catch some connections too */
368 else
369 max_accept = -1;
370
371 while (max_accept--) {
372 struct sockaddr_storage addr;
373 socklen_t laddr = sizeof(addr);
374
375 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
376 switch (errno) {
377 case EAGAIN:
378 case EINTR:
379 case ECONNABORTED:
380 return 0; /* nothing more to accept */
381 case ENFILE:
382 /* Process reached system FD limit. Check system tunables. */
383 return 0;
384 case EMFILE:
385 /* Process reached process FD limit. Check 'ulimit-n'. */
386 return 0;
387 case ENOBUFS:
388 case ENOMEM:
389 /* Process reached system memory limit. Check system tunables. */
390 return 0;
391 default:
392 return 0;
393 }
394 }
395
396 if (l->nbconn >= l->maxconn) {
397 /* too many connections, we shoot this one and return.
398 * FIXME: it would be better to simply switch the listener's
399 * state to LI_FULL and disable the FD. We could re-enable
400 * it upon fd_delete(), but this requires all protocols to
401 * be switched.
402 */
Willy Tarreaua11e9762008-12-01 01:44:25 +0100403 goto out_close;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200404 }
405
406 if ((s = pool_alloc2(pool2_session)) == NULL) {
407 Alert("out of memory in uxst_event_accept().\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100408 goto out_close;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200409 }
410
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100411 LIST_ADDQ(&sessions, &s->list);
412
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100413 s->flags = 0;
Willy Tarreauf8533202008-08-16 14:55:08 +0200414 s->term_trace = 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100415
Willy Tarreau92fb9832007-10-16 17:34:28 +0200416 if ((t = pool_alloc2(pool2_task)) == NULL) {
417 Alert("out of memory in uxst_event_accept().\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100418 goto out_free_session;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200419 }
420
421 s->cli_addr = addr;
422
423 /* FIXME: should be checked earlier */
424 if (cfd >= global.maxsock) {
425 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100426 goto out_free_task;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200427 }
428
429 if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) {
430 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
Willy Tarreaua11e9762008-12-01 01:44:25 +0100431 goto out_free_task;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200432 }
433
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200434 task_init(t);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200435 t->process = l->handler;
436 t->context = s;
Willy Tarreau91e99932008-06-30 07:51:00 +0200437 t->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200438
439 s->task = t;
Willy Tarreaub5654f62008-12-07 16:45:10 +0100440 s->listener = l;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200441 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];
Willy Tarreaua11e9762008-12-01 01:44:25 +0100521 fdtab[cfd].state = FD_STREADY;
522 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
523 fdtab[cfd].cb[DIR_RD].b = s->req;
524 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
525 fdtab[cfd].cb[DIR_WR].b = s->rep;
526 fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
527 fdtab[cfd].peerlen = sizeof(s->cli_addr);
528
529 EV_FD_SET(cfd, DIR_RD);
530
Willy Tarreaufdccded2008-08-29 18:19:04 +0200531 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200532
533 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
534 if (l->nbconn >= l->maxconn) {
535 EV_FD_CLR(l->fd, DIR_RD);
536 l->state = LI_FULL;
537 }
538 actconn++;
539 totalconn++;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100540 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200541 return 0;
Willy Tarreaua11e9762008-12-01 01:44:25 +0100542
543 out_free_req:
544 pool_free2(pool2_buffer, s->req);
545 out_free_task:
546 pool_free2(pool2_task, t);
547 out_free_session:
548 LIST_DEL(&s->list);
549 pool_free2(pool2_session, s);
550 out_close:
551 close(cfd);
552 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200553}
554
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100555/* Parses the request line in <cmd> and possibly starts dumping stats on
556 * s->rep with the hijack bit set. Returns 1 if OK, 0 in case of any error.
557 * The line is modified after parsing.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200558 */
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100559int unix_sock_parse_request(struct session *s, char *line)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200560{
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100561 char *args[MAX_UXST_ARGS + 1];
562 int arg;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200563
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100564 while (isspace((unsigned char)*line))
565 line++;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200566
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100567 arg = 0;
568 args[arg] = line;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200569
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100570 while (*line && arg < MAX_UXST_ARGS) {
571 if (isspace((unsigned char)*line)) {
572 *line++ = '\0';
Willy Tarreau92fb9832007-10-16 17:34:28 +0200573
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100574 while (isspace((unsigned char)*line))
575 line++;
576
577 args[++arg] = line;
578 continue;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200579 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100580
581 line++;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200582 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200583
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100584 while (++arg <= MAX_UXST_ARGS)
585 args[arg] = line;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200586
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100587 if (strcmp(args[0], "show") == 0) {
588 if (strcmp(args[1], "stat") == 0) {
589 if (*args[2] && *args[3] && *args[4]) {
590 s->data_ctx.stats.flags |= STAT_BOUND;
591 s->data_ctx.stats.iid = atoi(args[2]);
592 s->data_ctx.stats.type = atoi(args[3]);
593 s->data_ctx.stats.sid = atoi(args[4]);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200594 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100595
596 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
597 s->data_ctx.stats.flags |= STAT_FMT_CSV;
598 s->ana_state = STATS_ST_REP;
599 buffer_start_hijack(s->rep);
600 stats_dump_raw_to_buffer(s, s->rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200601 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100602 else if (strcmp(args[1], "info") == 0) {
603 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
604 s->data_ctx.stats.flags |= STAT_FMT_CSV;
605 s->ana_state = STATS_ST_REP;
606 buffer_start_hijack(s->rep);
607 stats_dump_raw_to_buffer(s, s->rep);
608 }
609 else { /* neither "stat" nor "info" */
610 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200611 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100612 }
613 else { /* not "show" */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200614 return 0;
615 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100616 return 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200617}
618
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100619/* Processes the stats interpreter on the statistics socket.
620 * In order to ease the transition, we simply simulate the server status
621 * for now. It only knows states STATS_ST_INIT, STATS_ST_REQ, STATS_ST_REP, and
622 * STATS_ST_CLOSE. It removes the AN_REQ_UNIX_STATS bit from req->analysers
623 * once done. It always returns 0.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200624 */
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100625int uxst_req_analyser_stats(struct session *s, struct buffer *req)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200626{
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100627 char *line, *p;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200628
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100629 switch (s->ana_state) {
630 case STATS_ST_INIT:
631 /* Stats output not initialized yet */
632 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
633 s->data_source = DATA_SRC_STATS;
634 s->ana_state = STATS_ST_REQ;
635 /* fall through */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200636
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100637 case STATS_ST_REQ:
638 /* Now, stats are initialized, hijack is not set, and
639 * we are waiting for a complete request line.
640 */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200641
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100642 line = s->req->data;
643 p = memchr(line, '\n', s->req->l);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200644
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100645 if (p) {
646 *p = '\0';
647 if (!unix_sock_parse_request(s, line)) {
648 /* invalid request */
649 buffer_shutw_now(s->req);
650 s->ana_state = 0;
651 req->analysers = 0;
652 return 0;
653 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200654 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200655
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100656 /* processing a valid or incomplete request */
657 if ((req->flags & BF_FULL) || /* invalid request */
658 (req->flags & BF_READ_ERROR) || /* input error */
659 (req->flags & BF_READ_TIMEOUT) || /* read timeout */
660 tick_is_expired(req->analyse_exp, now_ms) || /* request timeout */
661 (req->flags & BF_SHUTR)) { /* input closed */
662 buffer_shutw_now(s->req);
663 s->ana_state = 0;
664 req->analysers = 0;
665 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200666 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200667
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100668 /* don't forward nor abort */
669 buffer_write_dis(req);
670 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200671
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100672 case STATS_ST_REP:
673 /* do nothing while response is being processed */
674 buffer_write_dis(s->req);
675 return 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200676
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100677 case STATS_ST_CLOSE:
678 /* end of dump */
679 s->req->analysers &= ~AN_REQ_UNIX_STATS;
680 s->ana_state = 0;
681 break;
682 }
683 return 0;
684}
Willy Tarreau92fb9832007-10-16 17:34:28 +0200685
Willy Tarreau92fb9832007-10-16 17:34:28 +0200686
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100687/* This function is the unix-stream equivalent of the global process_session().
688 * It is currently limited to unix-stream processing on control sockets such as
689 * stats, and has no server-side. The two functions should be merged into one
690 * once client and server sides are better delimited. Note that the server-side
691 * still exists but remains in SI_ST_INI state forever, so that any call is a
692 * NOP.
693 */
694void uxst_process_session(struct task *t, int *next)
695{
696 struct session *s = t->context;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100697 int resync;
698 unsigned int rqf_last, rpf_last;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200699
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100700 /* 1a: Check for low level timeouts if needed. We just set a flag on
701 * stream interfaces when their timeouts have expired.
702 */
703 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
704 stream_int_check_timeouts(&s->si[0]);
705 buffer_check_timeouts(s->req);
706 buffer_check_timeouts(s->rep);
707 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200708
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100709 /* copy req/rep flags so that we can detect shutdowns */
710 rqf_last = s->req->flags;
711 rpf_last = s->rep->flags;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200712
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100713 /* 1b: check for low-level errors reported at the stream interface. */
714 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
715 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
716 s->si[0].shutr(&s->si[0]);
717 s->si[0].shutw(&s->si[0]);
718 stream_int_report_error(&s->si[0]);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200719 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100720 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200721
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100722 /* check buffer timeouts, and close the corresponding stream interfaces
723 * for future reads or writes. Note: this will also concern upper layers
724 * but we do not touch any other flag. We must be careful and correctly
725 * detect state changes when calling them.
726 */
727 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
728 if (s->req->flags & BF_READ_TIMEOUT)
729 s->req->prod->shutr(s->req->prod);
730 if (s->req->flags & BF_WRITE_TIMEOUT)
731 s->req->cons->shutw(s->req->cons);
732 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200733
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100734 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
735 if (s->rep->flags & BF_READ_TIMEOUT)
736 s->rep->prod->shutr(s->rep->prod);
737 if (s->rep->flags & BF_WRITE_TIMEOUT)
738 s->rep->cons->shutw(s->rep->cons);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200739 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200740
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100741 /* Check for connection closure */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200742
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100743 resync_stream_interface:
Willy Tarreau92fb9832007-10-16 17:34:28 +0200744
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100745 /* nothing special to be done on client side */
746 if (unlikely(s->req->prod->state == SI_ST_DIS))
747 s->req->prod->state = SI_ST_CLO;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200748
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100749 /*
750 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
751 * at this point.
752 */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200753
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100754 /**** Process layer 7 below ****/
Willy Tarreau92fb9832007-10-16 17:34:28 +0200755
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100756 resync = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200757
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100758 /* Analyse request */
759 if ((s->req->flags & BF_MASK_ANALYSER) ||
760 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
761 unsigned int flags = s->req->flags;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200762
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100763 if (s->req->prod->state >= SI_ST_EST) {
764 /* it's up to the analysers to reset write_ena */
765 buffer_write_ena(s->req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200766
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100767 /* We will call all analysers for which a bit is set in
768 * s->req->analysers, following the bit order from LSB
769 * to MSB. The analysers must remove themselves from
770 * the list when not needed. This while() loop is in
771 * fact a cleaner if().
Willy Tarreau92fb9832007-10-16 17:34:28 +0200772 */
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100773 while (s->req->analysers) {
774 if (s->req->analysers & AN_REQ_UNIX_STATS)
775 if (!uxst_req_analyser_stats(s, s->req))
776 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200777
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100778 /* Just make sure that nobody set a wrong flag causing an endless loop */
779 s->req->analysers &= AN_REQ_UNIX_STATS;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200780
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100781 /* we don't want to loop anyway */
782 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200783 }
784 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100785 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
786 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
787 if (s->req->flags != flags)
788 resync = 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200789 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200790
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100791 /* reflect what the L7 analysers have seen last */
792 rqf_last = s->req->flags;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200793
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100794 /*
795 * Now forward all shutdown requests between both sides of the buffer
796 */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200797
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100798 /* first, let's check if the request buffer needs to shutdown(write) */
799 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
800 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
801 buffer_shutw_now(s->req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200802
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100803 /* shutdown(write) pending */
804 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
805 s->req->cons->shutw(s->req->cons);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200806
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100807 /* shutdown(write) done on server side, we must stop the client too */
808 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
809 !s->req->analysers))
810 buffer_shutr_now(s->req);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100811
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100812 /* shutdown(read) pending */
813 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
814 s->req->prod->shutr(s->req->prod);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100815
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100816 /*
817 * Here we want to check if we need to resync or not.
818 */
819 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
820 resync = 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200821
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100822 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200823
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100824 /* according to benchmarks, it makes sense to resync now */
825 if (resync)
826 goto resync_stream_interface;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200827
Willy Tarreau92fb9832007-10-16 17:34:28 +0200828
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100829 /* Analyse response */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200830
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100831 buffer_write_ena(s->rep);
832 if (unlikely(s->rep->flags & BF_HIJACK)) {
833 /* In inject mode, we wake up everytime something has
834 * happened on the write side of the buffer.
835 */
836 unsigned int flags = s->rep->flags;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200837
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100838 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
839 !(s->rep->flags & BF_FULL)) {
840 /* it is the only hijacker right now */
841 stats_dump_raw_to_buffer(s, s->rep);
842 }
843 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
844 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
845 if (s->rep->flags != flags)
846 resync = 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200847 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100848 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
849 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
850 unsigned int flags = s->rep->flags;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200851
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100852 if (s->rep->prod->state >= SI_ST_EST) {
853 /* it's up to the analysers to reset write_ena */
854 buffer_write_ena(s->rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200855 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100856 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
857 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
858 if (s->rep->flags != flags)
859 resync = 1;
860 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100861
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100862 /* reflect what the L7 analysers have seen last */
863 rpf_last = s->rep->flags;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100864
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100865 /*
866 * Now forward all shutdown requests between both sides of the buffer
867 */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100868
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100869 /*
870 * FIXME: this is probably where we should produce error responses.
871 */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100872
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100873 /* first, let's check if the request buffer needs to shutdown(write) */
874 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
875 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
876 buffer_shutw_now(s->rep);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100877
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100878 /* shutdown(write) pending */
879 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
880 s->rep->cons->shutw(s->rep->cons);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100881
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100882 /* shutdown(write) done on the client side, we must stop the server too */
883 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW))
884 buffer_shutr_now(s->rep);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100885
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100886 /* shutdown(read) pending */
887 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
888 s->rep->prod->shutr(s->rep->prod);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100889
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100890 /*
891 * Here we want to check if we need to resync or not.
892 */
893 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
894 resync = 1;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100895
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100896 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100897
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100898 if (resync)
899 goto resync_stream_interface;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100900
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100901 if (likely(s->rep->cons->state != SI_ST_CLO)) {
902 if (s->rep->cons->state == SI_ST_EST)
903 stream_sock_data_finish(s->rep->cons);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200904
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100905 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
906 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
907 s->si[0].prev_state = s->si[0].state;
908 s->si[0].flags = SI_FL_NONE;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200909
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100910 /* Trick: if a request is being waiting for the server to respond,
911 * and if we know the server can timeout, we don't want the timeout
912 * to expire on the client side first, but we're still interested
913 * in passing data from the client to the server (eg: POST). Thus,
914 * we can cancel the client's request timeout if the server's
915 * request timeout is set and the server has not yet sent a response.
916 */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200917
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100918 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
919 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
920 s->req->rex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200921
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200922 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
923 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100924 if (s->req->analysers)
925 t->expire = tick_first(t->expire, s->req->analyse_exp);
926
927 if (s->si[0].exp)
928 t->expire = tick_first(t->expire, s->si[0].exp);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200929
930 /* restore t to its place in the task list */
931 task_queue(t);
932
933 *next = t->expire;
934 return; /* nothing more to do */
935 }
936
937 actconn--;
Willy Tarreaub5654f62008-12-07 16:45:10 +0100938 if (s->listener) {
939 s->listener->nbconn--;
940 if (s->listener->state == LI_FULL &&
941 s->listener->nbconn < s->listener->maxconn) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200942 /* we should reactivate the listener */
Willy Tarreaub5654f62008-12-07 16:45:10 +0100943 EV_FD_SET(s->listener->fd, DIR_RD);
944 s->listener->state = LI_READY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200945 }
946 }
947
948 /* the task MUST not be in the run queue anymore */
949 task_delete(t);
950 session_free(s);
951 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200952 *next = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200953}
954
Willy Tarreau92fb9832007-10-16 17:34:28 +0200955__attribute__((constructor))
956static void __uxst_protocol_init(void)
957{
958 protocol_register(&proto_unix);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200959}
960
961
962/*
963 * Local variables:
964 * c-indent-level: 8
965 * c-basic-offset: 8
966 * End:
967 */