blob: d74bcb4c971065265948d21740c5e88d7271d751 [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>
Willy Tarreau3e76e722007-10-17 18:57:38 +020050#include <proto/senddata.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020051#include <proto/session.h>
52#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;
249
250 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;
259
260 /* 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;
269 fdtab[fd].owner = (struct task *)listener; /* reference the listener instead of a task */
270 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) {
361 struct listener *l = (struct listener *)fdtab[fd].owner;
362 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 */
404 close(cfd);
405 return 0;
406 }
407
408 if ((s = pool_alloc2(pool2_session)) == NULL) {
409 Alert("out of memory in uxst_event_accept().\n");
410 close(cfd);
411 return 0;
412 }
413
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100414 s->flags = 0;
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200415 s->analysis = 0;
Willy Tarreauf8533202008-08-16 14:55:08 +0200416 s->term_trace = 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100417
Willy Tarreau92fb9832007-10-16 17:34:28 +0200418 if ((t = pool_alloc2(pool2_task)) == NULL) {
419 Alert("out of memory in uxst_event_accept().\n");
420 close(cfd);
421 pool_free2(pool2_session, s);
422 return 0;
423 }
424
425 s->cli_addr = addr;
426
427 /* FIXME: should be checked earlier */
428 if (cfd >= global.maxsock) {
429 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
430 close(cfd);
431 pool_free2(pool2_task, t);
432 pool_free2(pool2_session, s);
433 return 0;
434 }
435
436 if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) {
437 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
438 close(cfd);
439 pool_free2(pool2_task, t);
440 pool_free2(pool2_session, s);
441 return 0;
442 }
443
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200444 task_init(t);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200445 t->process = l->handler;
446 t->context = s;
Willy Tarreau91e99932008-06-30 07:51:00 +0200447 t->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200448
449 s->task = t;
450 s->fe = NULL;
451 s->be = NULL;
452
453 s->cli_state = CL_STDATA;
454 s->srv_state = SV_STIDLE;
455 s->req = s->rep = NULL; /* will be allocated later */
456
457 s->cli_fd = cfd;
458 s->srv_fd = -1;
459 s->srv = NULL;
460 s->pend_pos = NULL;
461
462 memset(&s->logs, 0, sizeof(s->logs));
463 memset(&s->txn, 0, sizeof(s->txn));
464
Willy Tarreau3e76e722007-10-17 18:57:38 +0200465 s->data_state = DATA_ST_INIT;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200466 s->data_source = DATA_SRC_NONE;
467 s->uniq_id = totalconn;
468
469 if ((s->req = pool_alloc2(pool2_buffer)) == NULL) { /* no memory */
470 close(cfd); /* nothing can be done for this fd without memory */
471 pool_free2(pool2_task, t);
472 pool_free2(pool2_session, s);
473 return 0;
474 }
475
476 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL) { /* no memory */
477 pool_free2(pool2_buffer, s->req);
478 close(cfd); /* nothing can be done for this fd without memory */
479 pool_free2(pool2_task, t);
480 pool_free2(pool2_session, s);
481 return 0;
482 }
483
484 buffer_init(s->req);
485 buffer_init(s->rep);
486 s->req->rlim += BUFSIZE;
487 s->rep->rlim += BUFSIZE;
488
489 fd_insert(cfd);
490 fdtab[cfd].owner = t;
491 fdtab[cfd].listener = l;
492 fdtab[cfd].state = FD_STREADY;
493 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
494 fdtab[cfd].cb[DIR_RD].b = s->req;
495 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
496 fdtab[cfd].cb[DIR_WR].b = s->rep;
497 fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
498 fdtab[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200499
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200500 s->req->rex = TICK_ETERNITY;
501 s->req->wex = TICK_ETERNITY;
502 s->req->cex = TICK_ETERNITY;
503 s->rep->rex = TICK_ETERNITY;
504 s->rep->wex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200505
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200506 s->req->wto = TICK_ETERNITY;
507 s->req->cto = TICK_ETERNITY;
508 s->req->rto = TICK_ETERNITY;
509 s->rep->rto = TICK_ETERNITY;
510 s->rep->cto = TICK_ETERNITY;
511 s->rep->wto = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200512
513 if (l->timeout)
514 s->req->rto = *l->timeout;
515
516 if (l->timeout)
517 s->rep->wto = *l->timeout;
518
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200519 t->expire = TICK_ETERNITY;
520 if (l->timeout && *l->timeout) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200521 EV_FD_SET(cfd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200522 s->req->rex = tick_add(now_ms, s->req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200523 t->expire = s->req->rex;
524 }
525
Willy Tarreau92fb9832007-10-16 17:34:28 +0200526 task_wakeup(t);
527
528 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
529 if (l->nbconn >= l->maxconn) {
530 EV_FD_CLR(l->fd, DIR_RD);
531 l->state = LI_FULL;
532 }
533 actconn++;
534 totalconn++;
535
536 //fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p, cfd=%d, maxfd=%d\n", p, actconn, totalconn, t, cfd, maxfd);
537 } /* end of while (p->feconn < p->maxconn) */
538 //fprintf(stderr,"fct %s:%d\n", __FUNCTION__, __LINE__);
539 return 0;
540}
541
542/*
543 * manages the client FSM and its socket. It returns 1 if a state has changed
544 * (and a resync may be needed), otherwise 0.
545 */
546static int process_uxst_cli(struct session *t)
547{
548 int s = t->srv_state;
549 int c = t->cli_state;
550 struct buffer *req = t->req;
551 struct buffer *rep = t->rep;
552 //fprintf(stderr,"fct %s:%d\n", __FUNCTION__, __LINE__);
553 if (c == CL_STDATA) {
554 /* FIXME: this error handling is partly buggy because we always report
555 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
556 * or HEADER phase. BTW, it's not logical to expire the client while
557 * we're waiting for the server to connect.
558 */
559 /* read or write error */
560 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +0200561 buffer_shutr(req);
562 buffer_shutw(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200563 fd_delete(t->cli_fd);
564 t->cli_state = CL_STCLOSE;
565 if (!(t->flags & SN_ERR_MASK))
566 t->flags |= SN_ERR_CLICL;
567 if (!(t->flags & SN_FINST_MASK)) {
568 if (t->pend_pos)
569 t->flags |= SN_FINST_Q;
570 else if (s == SV_STCONN)
571 t->flags |= SN_FINST_C;
572 else
573 t->flags |= SN_FINST_D;
574 }
575 return 1;
576 }
577 /* last read, or end of server write */
578 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
579 EV_FD_CLR(t->cli_fd, DIR_RD);
580 buffer_shutr(req);
581 t->cli_state = CL_STSHUTR;
582 return 1;
583 }
584 /* last server read and buffer empty */
585 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
586 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreauba392ce2008-08-16 21:13:23 +0200587 buffer_shutw(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200588 shutdown(t->cli_fd, SHUT_WR);
589 /* We must ensure that the read part is still alive when switching
590 * to shutw */
591 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200592 req->rex = tick_add_ifset(now_ms, req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200593 t->cli_state = CL_STSHUTW;
594 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
595 return 1;
596 }
597 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200598 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200599 EV_FD_CLR(t->cli_fd, DIR_RD);
600 buffer_shutr(req);
601 t->cli_state = CL_STSHUTR;
602 if (!(t->flags & SN_ERR_MASK))
603 t->flags |= SN_ERR_CLITO;
604 if (!(t->flags & SN_FINST_MASK)) {
605 if (t->pend_pos)
606 t->flags |= SN_FINST_Q;
607 else if (s == SV_STCONN)
608 t->flags |= SN_FINST_C;
609 else
610 t->flags |= SN_FINST_D;
611 }
612 return 1;
613 }
614 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200615 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200616 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreauba392ce2008-08-16 21:13:23 +0200617 buffer_shutw(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200618 shutdown(t->cli_fd, SHUT_WR);
619 /* We must ensure that the read part is still alive when switching
620 * to shutw */
621 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200622 req->rex = tick_add_ifset(now_ms, req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200623
624 t->cli_state = CL_STSHUTW;
625 if (!(t->flags & SN_ERR_MASK))
626 t->flags |= SN_ERR_CLITO;
627 if (!(t->flags & SN_FINST_MASK)) {
628 if (t->pend_pos)
629 t->flags |= SN_FINST_Q;
630 else if (s == SV_STCONN)
631 t->flags |= SN_FINST_C;
632 else
633 t->flags |= SN_FINST_D;
634 }
635 return 1;
636 }
637
638 if (req->l >= req->rlim - req->data) {
639 /* no room to read more data */
640 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
641 /* stop reading until we get some space */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200642 req->rex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200643 }
644 } else {
645 /* there's still some space in the buffer */
646 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200647 if (!req->rto ||
648 (t->srv_state < SV_STDATA && req->wto))
Willy Tarreau92fb9832007-10-16 17:34:28 +0200649 /* If the client has no timeout, or if the server not ready yet, and we
650 * know for sure that it can expire, then it's cleaner to disable the
651 * timeout on the client side so that too low values cannot make the
652 * sessions abort too early.
653 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200654 req->rex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200655 else
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200656 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200657 }
658 }
659
660 if ((rep->l == 0) ||
661 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
662 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
663 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200664 rep->wex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200665 }
666 } else {
667 /* buffer not empty */
668 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
669 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200670 rep->wex = tick_add_ifset(now_ms, rep->wto);
671 if (rep->wex) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200672 /* FIXME: to prevent the client from expiring read timeouts during writes,
673 * we refresh it. */
674 req->rex = rep->wex;
675 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200676 }
677 }
678 return 0; /* other cases change nothing */
679 }
680 else if (c == CL_STSHUTR) {
681 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +0200682 buffer_shutw(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200683 fd_delete(t->cli_fd);
684 t->cli_state = CL_STCLOSE;
685 if (!(t->flags & SN_ERR_MASK))
686 t->flags |= SN_ERR_CLICL;
687 if (!(t->flags & SN_FINST_MASK)) {
688 if (t->pend_pos)
689 t->flags |= SN_FINST_Q;
690 else if (s == SV_STCONN)
691 t->flags |= SN_FINST_C;
692 else
693 t->flags |= SN_FINST_D;
694 }
695 return 1;
696 }
697 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauba392ce2008-08-16 21:13:23 +0200698 buffer_shutw(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200699 fd_delete(t->cli_fd);
700 t->cli_state = CL_STCLOSE;
701 return 1;
702 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200703 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreauba392ce2008-08-16 21:13:23 +0200704 buffer_shutw(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200705 fd_delete(t->cli_fd);
706 t->cli_state = CL_STCLOSE;
707 if (!(t->flags & SN_ERR_MASK))
708 t->flags |= SN_ERR_CLITO;
709 if (!(t->flags & SN_FINST_MASK)) {
710 if (t->pend_pos)
711 t->flags |= SN_FINST_Q;
712 else if (s == SV_STCONN)
713 t->flags |= SN_FINST_C;
714 else
715 t->flags |= SN_FINST_D;
716 }
717 return 1;
718 }
719
720 if (rep->l == 0) {
721 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
722 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200723 rep->wex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200724 }
725 } else {
726 /* buffer not empty */
727 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
728 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200729 rep->wex = tick_add_ifset(now_ms, rep->wto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200730 }
731 }
732 return 0;
733 }
734 else if (c == CL_STSHUTW) {
735 if (req->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +0200736 buffer_shutr(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200737 fd_delete(t->cli_fd);
738 t->cli_state = CL_STCLOSE;
739 if (!(t->flags & SN_ERR_MASK))
740 t->flags |= SN_ERR_CLICL;
741 if (!(t->flags & SN_FINST_MASK)) {
742 if (t->pend_pos)
743 t->flags |= SN_FINST_Q;
744 else if (s == SV_STCONN)
745 t->flags |= SN_FINST_C;
746 else
747 t->flags |= SN_FINST_D;
748 }
749 return 1;
750 }
751 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauba392ce2008-08-16 21:13:23 +0200752 buffer_shutr(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200753 fd_delete(t->cli_fd);
754 t->cli_state = CL_STCLOSE;
755 return 1;
756 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200757 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreauba392ce2008-08-16 21:13:23 +0200758 buffer_shutr(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200759 fd_delete(t->cli_fd);
760 t->cli_state = CL_STCLOSE;
761 if (!(t->flags & SN_ERR_MASK))
762 t->flags |= SN_ERR_CLITO;
763 if (!(t->flags & SN_FINST_MASK)) {
764 if (t->pend_pos)
765 t->flags |= SN_FINST_Q;
766 else if (s == SV_STCONN)
767 t->flags |= SN_FINST_C;
768 else
769 t->flags |= SN_FINST_D;
770 }
771 return 1;
772 }
773 else if (req->l >= req->rlim - req->data) {
774 /* no room to read more data */
775
776 /* FIXME-20050705: is it possible for a client to maintain a session
777 * after the timeout by sending more data after it receives a close ?
778 */
779
780 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
781 /* stop reading until we get some space */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200782 req->rex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200783 }
784 } else {
785 /* there's still some space in the buffer */
786 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200787 req->rex = tick_add_ifset(now_ms, req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200788 }
789 }
790 return 0;
791 }
792 else { /* CL_STCLOSE: nothing to do */
793 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
794 int len;
795 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be?t->be->id:"",
796 (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
797 write(1, trash, len);
798 }
799 return 0;
800 }
801 return 0;
802}
803
804#if 0
805 /* FIXME! This part has not been completely converted yet, and it may
806 * still be very specific to TCPv4 ! Also, it relies on some parameters
807 * such as conn_retries which are not set upon accept().
808 */
809/*
810 * Manages the server FSM and its socket. It returns 1 if a state has changed
811 * (and a resync may be needed), otherwise 0.
812 */
813static int process_uxst_srv(struct session *t)
814{
815 int s = t->srv_state;
816 int c = t->cli_state;
817 struct buffer *req = t->req;
818 struct buffer *rep = t->rep;
819 int conn_err;
820
821 if (s == SV_STIDLE) {
822 if (c == CL_STCLOSE || c == CL_STSHUTW ||
823 (c == CL_STSHUTR &&
824 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
825 tv_eternity(&req->cex);
826 if (t->pend_pos)
827 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
828 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C);
829 return 1;
830 }
831 else {
832 /* FIXME: reimplement the TARPIT check here */
833
834 /* Right now, we will need to create a connection to the server.
835 * We might already have tried, and got a connection pending, in
836 * which case we will not do anything till it's pending. It's up
837 * to any other session to release it and wake us up again.
838 */
839 if (t->pend_pos) {
840 if (!tv_isle(&req->cex, &now))
841 return 0;
842 else {
843 /* we've been waiting too long here */
844 tv_eternity(&req->cex);
845 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
846 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q);
847 if (t->srv)
848 t->srv->failed_conns++;
849 if (t->fe)
850 t->fe->failed_conns++;
851 return 1;
852 }
853 }
854
855 do {
856 /* first, get a connection */
857 if (srv_redispatch_connect(t))
858 return t->srv_state != SV_STIDLE;
859
860 /* try to (re-)connect to the server, and fail if we expire the
861 * number of retries.
862 */
863 if (srv_retryable_connect(t)) {
864 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
865 return t->srv_state != SV_STIDLE;
866 }
867 } while (1);
868 }
869 }
870 else if (s == SV_STCONN) { /* connection in progress */
871 if (c == CL_STCLOSE || c == CL_STSHUTW ||
872 (c == CL_STSHUTR &&
873 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
874 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
875 tv_eternity(&req->cex);
876 fd_delete(t->srv_fd);
877 if (t->srv)
878 t->srv->cur_sess--;
879
880 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C);
881 return 1;
882 }
883 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
884 //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, req->cex.tv_sec, req->cex.tv_usec);
885 return 0; /* nothing changed */
886 }
887 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
888 /* timeout, asynchronous connect error or first write error */
889 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
890
891 fd_delete(t->srv_fd);
892 if (t->srv)
893 t->srv->cur_sess--;
894
895 if (!(req->flags & BF_WRITE_STATUS))
896 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
897 else
898 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
899
900 /* ensure that we have enough retries left */
901 if (srv_count_retry_down(t, conn_err))
902 return 1;
903
904 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
905 /* We're on our last chance, and the REDISP option was specified.
906 * We will ignore cookie and force to balance or use the dispatcher.
907 */
908 /* let's try to offer this slot to anybody */
909 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200910 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200911
912 if (t->srv)
913 t->srv->failed_conns++;
914 t->be->failed_conns++;
915
916 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
917 t->srv = NULL; /* it's left to the dispatcher to choose a server */
918
919 /* first, get a connection */
920 if (srv_redispatch_connect(t))
921 return t->srv_state != SV_STIDLE;
922 }
923
924 do {
925 /* Now we will try to either reconnect to the same server or
926 * connect to another server. If the connection gets queued
927 * because all servers are saturated, then we will go back to
928 * the SV_STIDLE state.
929 */
930 if (srv_retryable_connect(t)) {
931 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
932 return t->srv_state != SV_STCONN;
933 }
934
935 /* we need to redispatch the connection to another server */
936 if (srv_redispatch_connect(t))
937 return t->srv_state != SV_STCONN;
938 } while (1);
939 }
940 else { /* no error or write 0 */
941 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
942
943 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
944 if (req->l == 0) /* nothing to write */ {
945 EV_FD_CLR(t->srv_fd, DIR_WR);
946 tv_eternity(&req->wex);
947 } else /* need the right to write */ {
948 EV_FD_SET(t->srv_fd, DIR_WR);
949 if (tv_add_ifset(&req->wex, &now, &req->wto)) {
950 /* FIXME: to prevent the server from expiring read timeouts during writes,
951 * we refresh it. */
952 rep->rex = req->wex;
953 }
954 else
955 tv_eternity(&req->wex);
956 }
957
958 EV_FD_SET(t->srv_fd, DIR_RD);
959 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
960 tv_eternity(&rep->rex);
961
962 t->srv_state = SV_STDATA;
963 if (t->srv)
964 t->srv->cum_sess++;
965 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
966
967 /* if the user wants to log as soon as possible, without counting
968 bytes from the server, then this is the right moment. */
969 if (t->fe && t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
970 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
971 //uxst_sess_log(t);
972 }
973 tv_eternity(&req->cex);
974 return 1;
975 }
976 }
977 else if (s == SV_STDATA) {
978 /* read or write error */
979 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +0200980 buffer_shutr(rep);
981 buffer_shutw(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200982 fd_delete(t->srv_fd);
983 if (t->srv) {
984 t->srv->cur_sess--;
985 t->srv->failed_resp++;
986 }
987 t->be->failed_resp++;
988 t->srv_state = SV_STCLOSE;
989 if (!(t->flags & SN_ERR_MASK))
990 t->flags |= SN_ERR_SRVCL;
991 if (!(t->flags & SN_FINST_MASK))
992 t->flags |= SN_FINST_D;
993 /* We used to have a free connection slot. Since we'll never use it,
994 * we have to inform the server that it may be used by another session.
995 */
996 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200997 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200998
999 return 1;
1000 }
1001 /* last read, or end of client write */
1002 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
1003 EV_FD_CLR(t->srv_fd, DIR_RD);
1004 buffer_shutr(rep);
1005 t->srv_state = SV_STSHUTR;
1006 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1007 return 1;
1008 }
1009 /* end of client read and no more data to send */
1010 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
1011 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreauba392ce2008-08-16 21:13:23 +02001012 buffer_shutw(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001013 shutdown(t->srv_fd, SHUT_WR);
1014 /* We must ensure that the read part is still alive when switching
1015 * to shutw */
1016 EV_FD_SET(t->srv_fd, DIR_RD);
1017 tv_add_ifset(&rep->rex, &now, &rep->rto);
1018
1019 t->srv_state = SV_STSHUTW;
1020 return 1;
1021 }
1022 /* read timeout */
1023 else if (tv_isle(&rep->rex, &now)) {
1024 EV_FD_CLR(t->srv_fd, DIR_RD);
1025 buffer_shutr(rep);
1026 t->srv_state = SV_STSHUTR;
1027 if (!(t->flags & SN_ERR_MASK))
1028 t->flags |= SN_ERR_SRVTO;
1029 if (!(t->flags & SN_FINST_MASK))
1030 t->flags |= SN_FINST_D;
1031 return 1;
1032 }
1033 /* write timeout */
1034 else if (tv_isle(&req->wex, &now)) {
1035 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreauba392ce2008-08-16 21:13:23 +02001036 buffer_shutw(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001037 shutdown(t->srv_fd, SHUT_WR);
1038 /* We must ensure that the read part is still alive when switching
1039 * to shutw */
1040 EV_FD_SET(t->srv_fd, DIR_RD);
1041 tv_add_ifset(&rep->rex, &now, &rep->rto);
1042 t->srv_state = SV_STSHUTW;
1043 if (!(t->flags & SN_ERR_MASK))
1044 t->flags |= SN_ERR_SRVTO;
1045 if (!(t->flags & SN_FINST_MASK))
1046 t->flags |= SN_FINST_D;
1047 return 1;
1048 }
1049
1050 /* recompute request time-outs */
1051 if (req->l == 0) {
1052 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
1053 /* stop writing */
1054 tv_eternity(&req->wex);
1055 }
1056 }
1057 else { /* buffer not empty, there are still data to be transferred */
1058 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
1059 /* restart writing */
1060 if (tv_add_ifset(&req->wex, &now, &req->wto)) {
1061 /* FIXME: to prevent the server from expiring read timeouts during writes,
1062 * we refresh it. */
1063 rep->rex = req->wex;
1064 }
1065 else
1066 tv_eternity(&req->wex);
1067 }
1068 }
1069
1070 /* recompute response time-outs */
1071 if (rep->l == BUFSIZE) { /* no room to read more data */
1072 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
1073 tv_eternity(&rep->rex);
1074 }
1075 }
1076 else {
1077 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
1078 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
1079 tv_eternity(&rep->rex);
1080 }
1081 }
1082
1083 return 0; /* other cases change nothing */
1084 }
1085 else if (s == SV_STSHUTR) {
1086 if (req->flags & BF_WRITE_ERROR) {
1087 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreauba392ce2008-08-16 21:13:23 +02001088 buffer_shutw(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001089 fd_delete(t->srv_fd);
1090 if (t->srv) {
1091 t->srv->cur_sess--;
1092 t->srv->failed_resp++;
1093 }
1094 t->be->failed_resp++;
1095 //close(t->srv_fd);
1096 t->srv_state = SV_STCLOSE;
1097 if (!(t->flags & SN_ERR_MASK))
1098 t->flags |= SN_ERR_SRVCL;
1099 if (!(t->flags & SN_FINST_MASK))
1100 t->flags |= SN_FINST_D;
1101 /* We used to have a free connection slot. Since we'll never use it,
1102 * we have to inform the server that it may be used by another session.
1103 */
1104 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001105 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001106
1107 return 1;
1108 }
1109 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
1110 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreauba392ce2008-08-16 21:13:23 +02001111 buffer_shutw(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001112 fd_delete(t->srv_fd);
1113 if (t->srv)
1114 t->srv->cur_sess--;
1115 //close(t->srv_fd);
1116 t->srv_state = SV_STCLOSE;
1117 /* We used to have a free connection slot. Since we'll never use it,
1118 * we have to inform the server that it may be used by another session.
1119 */
1120 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001121 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001122
1123 return 1;
1124 }
1125 else if (tv_isle(&req->wex, &now)) {
1126 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreauba392ce2008-08-16 21:13:23 +02001127 buffer_shutw(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001128 fd_delete(t->srv_fd);
1129 if (t->srv)
1130 t->srv->cur_sess--;
1131 //close(t->srv_fd);
1132 t->srv_state = SV_STCLOSE;
1133 if (!(t->flags & SN_ERR_MASK))
1134 t->flags |= SN_ERR_SRVTO;
1135 if (!(t->flags & SN_FINST_MASK))
1136 t->flags |= SN_FINST_D;
1137 /* We used to have a free connection slot. Since we'll never use it,
1138 * we have to inform the server that it may be used by another session.
1139 */
1140 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001141 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001142
1143 return 1;
1144 }
1145 else if (req->l == 0) {
1146 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
1147 /* stop writing */
1148 tv_eternity(&req->wex);
1149 }
1150 }
1151 else { /* buffer not empty */
1152 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
1153 /* restart writing */
1154 if (!tv_add_ifset(&req->wex, &now, &req->wto))
1155 tv_eternity(&req->wex);
1156 }
1157 }
1158 return 0;
1159 }
1160 else if (s == SV_STSHUTW) {
1161 if (rep->flags & BF_READ_ERROR) {
1162 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauba392ce2008-08-16 21:13:23 +02001163 buffer_shutr(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001164 fd_delete(t->srv_fd);
1165 if (t->srv) {
1166 t->srv->cur_sess--;
1167 t->srv->failed_resp++;
1168 }
1169 t->be->failed_resp++;
1170 //close(t->srv_fd);
1171 t->srv_state = SV_STCLOSE;
1172 if (!(t->flags & SN_ERR_MASK))
1173 t->flags |= SN_ERR_SRVCL;
1174 if (!(t->flags & SN_FINST_MASK))
1175 t->flags |= SN_FINST_D;
1176 /* We used to have a free connection slot. Since we'll never use it,
1177 * we have to inform the server that it may be used by another session.
1178 */
1179 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001180 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001181
1182 return 1;
1183 }
1184 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
1185 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauba392ce2008-08-16 21:13:23 +02001186 buffer_shutr(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001187 fd_delete(t->srv_fd);
1188 if (t->srv)
1189 t->srv->cur_sess--;
1190 //close(t->srv_fd);
1191 t->srv_state = SV_STCLOSE;
1192 /* We used to have a free connection slot. Since we'll never use it,
1193 * we have to inform the server that it may be used by another session.
1194 */
1195 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001196 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001197
1198 return 1;
1199 }
1200 else if (tv_isle(&rep->rex, &now)) {
1201 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauba392ce2008-08-16 21:13:23 +02001202 buffer_shutr(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001203 fd_delete(t->srv_fd);
1204 if (t->srv)
1205 t->srv->cur_sess--;
1206 //close(t->srv_fd);
1207 t->srv_state = SV_STCLOSE;
1208 if (!(t->flags & SN_ERR_MASK))
1209 t->flags |= SN_ERR_SRVTO;
1210 if (!(t->flags & SN_FINST_MASK))
1211 t->flags |= SN_FINST_D;
1212 /* We used to have a free connection slot. Since we'll never use it,
1213 * we have to inform the server that it may be used by another session.
1214 */
1215 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001216 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001217
1218 return 1;
1219 }
1220 else if (rep->l == BUFSIZE) { /* no room to read more data */
1221 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
1222 tv_eternity(&rep->rex);
1223 }
1224 }
1225 else {
1226 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
1227 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
1228 tv_eternity(&rep->rex);
1229 }
1230 }
1231 return 0;
1232 }
1233 else { /* SV_STCLOSE : nothing to do */
1234 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1235 int len;
1236 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1237 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
1238 write(1, trash, len);
1239 }
1240 return 0;
1241 }
1242 return 0;
1243}
1244
1245/* Processes the client and server jobs of a session task, then
1246 * puts it back to the wait queue in a clean state, or
1247 * cleans up its resources if it must be deleted. Returns
1248 * the time the task accepts to wait, or TIME_ETERNITY for
1249 * infinity.
1250 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001251void process_uxst_session(struct task *t, int *next)
Willy Tarreau92fb9832007-10-16 17:34:28 +02001252{
1253 struct session *s = t->context;
1254 int fsm_resync = 0;
1255
1256 do {
1257 fsm_resync = 0;
1258 fsm_resync |= process_uxst_cli(s);
1259 if (s->srv_state == SV_STIDLE) {
1260 if (s->cli_state == CL_STCLOSE || s->cli_state == CL_STSHUTW) {
1261 s->srv_state = SV_STCLOSE;
1262 fsm_resync |= 1;
1263 continue;
1264 }
1265 if (s->cli_state == CL_STSHUTR ||
1266 (s->req->l >= s->req->rlim - s->req->data)) {
1267 if (s->req->l == 0) {
1268 s->srv_state = SV_STCLOSE;
1269 fsm_resync |= 1;
1270 continue;
1271 }
1272 /* OK we have some remaining data to process */
1273 /* Just as an exercice, we copy the req into the resp,
1274 * and flush the req.
1275 */
1276 memcpy(s->rep->data, s->req->data, sizeof(s->rep->data));
1277 s->rep->l = s->req->l;
1278 s->rep->rlim = s->rep->data + BUFSIZE;
1279 s->rep->w = s->rep->data;
1280 s->rep->lr = s->rep->r = s->rep->data + s->rep->l;
1281
1282 s->req->l = 0;
1283 s->srv_state = SV_STCLOSE;
1284
1285 fsm_resync |= 1;
1286 continue;
1287 }
1288 }
1289 } while (fsm_resync);
1290
1291 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001292
1293 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1294 session_process_counters(s);
1295
Willy Tarreau92fb9832007-10-16 17:34:28 +02001296 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1297 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1298
1299 t->expire = s->req->rex;
1300 tv_min(&t->expire, &s->req->rex, &s->req->wex);
1301 tv_bound(&t->expire, &s->req->cex);
1302 tv_bound(&t->expire, &s->rep->rex);
1303 tv_bound(&t->expire, &s->rep->wex);
1304
1305 /* restore t to its place in the task list */
1306 task_queue(t);
1307
1308 *next = t->expire;
1309 return; /* nothing more to do */
1310 }
1311
1312 if (s->fe)
1313 s->fe->feconn--;
1314 if (s->be && (s->flags & SN_BE_ASSIGNED))
1315 s->be->beconn--;
1316 actconn--;
1317
1318 if (unlikely((global.mode & MODE_DEBUG) &&
1319 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1320 int len;
1321 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
1322 s->uniq_id, s->be->id,
1323 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
1324 write(1, trash, len);
1325 }
1326
1327 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001328 session_process_counters(s);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001329
1330 /* let's do a final log if we need it */
1331 if (s->logs.logwait &&
1332 !(s->flags & SN_MONITOR) &&
1333 (s->req->total || !(s->fe && s->fe->options & PR_O_NULLNOLOG))) {
1334 //uxst_sess_log(s);
1335 }
1336
1337 /* the task MUST not be in the run queue anymore */
1338 task_delete(t);
1339 session_free(s);
1340 task_free(t);
1341 tv_eternity(next);
1342}
1343#endif /* not converted */
1344
1345
1346/* Processes data exchanges on the statistics socket. The client processing
1347 * is called and the task is put back in the wait queue or it is cleared.
1348 * In order to ease the transition, we simply simulate the server status
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001349 * for now. It only knows states SV_STIDLE, SV_STCONN, SV_STDATA, and
1350 * SV_STCLOSE. Returns in <next> the task's expiration date.
Willy Tarreau92fb9832007-10-16 17:34:28 +02001351 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001352void process_uxst_stats(struct task *t, int *next)
Willy Tarreau92fb9832007-10-16 17:34:28 +02001353{
1354 struct session *s = t->context;
1355 struct listener *listener;
1356 int fsm_resync = 0;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001357 int last_rep_l;
Willy Tarreau92fb9832007-10-16 17:34:28 +02001358
1359 do {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001360 char *args[MAX_UXST_ARGS + 1];
1361 char *line, *p;
1362 int arg;
1363
Willy Tarreau3e76e722007-10-17 18:57:38 +02001364 fsm_resync = process_uxst_cli(s);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001365
1366 if (s->cli_state == CL_STCLOSE || s->cli_state == CL_STSHUTW) {
1367 s->srv_state = SV_STCLOSE;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001368 break;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001369 }
1370
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001371 switch (s->srv_state) {
1372 case SV_STIDLE:
1373 /* stats output not initialized yet */
1374 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
1375 s->data_source = DATA_SRC_STATS;
1376 s->srv_state = SV_STCONN;
1377 fsm_resync |= 1;
1378 break;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001379
Willy Tarreauadfb8562008-08-11 15:24:42 +02001380 case SV_STCONN: /* should be changed to SV_STHEADERS or something more obvious */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001381 /* stats initialized, but waiting for the command */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001382 line = s->req->data;
1383 p = memchr(line, '\n', s->req->l);
1384
1385 if (!p)
1386 continue;
1387
1388 *p = '\0';
1389
1390 while (isspace((unsigned char)*line))
1391 line++;
1392
1393 arg = 0;
1394 args[arg] = line;
1395
1396 while (*line && arg < MAX_UXST_ARGS) {
1397 if (isspace((unsigned char)*line)) {
1398 *line++ = '\0';
1399
1400 while (isspace((unsigned char)*line))
1401 line++;
1402
1403 args[++arg] = line;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001404 continue;
1405 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001406
1407 line++;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001408 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001409
1410 while (++arg <= MAX_UXST_ARGS)
1411 args[arg] = line;
1412
1413 if (!strcmp(args[0], "show")) {
1414 if (!strcmp(args[1], "stat")) {
1415 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001416 s->data_ctx.stats.flags |= STAT_BOUND;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001417 s->data_ctx.stats.iid = atoi(args[2]);
1418 s->data_ctx.stats.type = atoi(args[3]);
1419 s->data_ctx.stats.sid = atoi(args[4]);
1420 }
1421
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001422 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
1423 s->data_ctx.stats.flags |= STAT_FMT_CSV;
1424 s->srv_state = SV_STDATA;
1425 fsm_resync |= 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +02001426 continue;
1427 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001428
1429 if (!strcmp(args[1], "info")) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001430 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
1431 s->data_ctx.stats.flags |= STAT_FMT_CSV;
1432 s->srv_state = SV_STDATA;
1433 fsm_resync |= 1;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001434 continue;
1435 }
Willy Tarreau92fb9832007-10-16 17:34:28 +02001436 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001437
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001438 s->srv_state = SV_STCLOSE;
1439 fsm_resync |= 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001440 continue;
1441
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001442 case SV_STDATA:
1443 /* OK we have to process the request. Since it is possible
1444 * that we get there with the client output paused, we
1445 * will simply check that we have really sent some data
1446 * and wake the client up if needed.
1447 */
1448 last_rep_l = s->rep->l;
1449 if (stats_dump_raw(s, NULL) != 0) {
1450 s->srv_state = SV_STCLOSE;
1451 fsm_resync |= 1;
1452 }
1453 if (s->rep->l != last_rep_l)
1454 fsm_resync |= 1;
1455 break;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001456 }
Willy Tarreau92fb9832007-10-16 17:34:28 +02001457 } while (fsm_resync);
1458
1459 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
1460 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1461 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1462
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001463 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1464 tick_first(s->rep->rex, s->rep->wex));
1465 t->expire = tick_first(t->expire, s->req->cex);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001466
1467 /* restore t to its place in the task list */
1468 task_queue(t);
1469
1470 *next = t->expire;
1471 return; /* nothing more to do */
1472 }
1473
1474 actconn--;
1475 listener = fdtab[s->cli_fd].listener;
1476 if (listener) {
1477 listener->nbconn--;
1478 if (listener->state == LI_FULL &&
1479 listener->nbconn < listener->maxconn) {
1480 /* we should reactivate the listener */
1481 EV_FD_SET(listener->fd, DIR_RD);
1482 listener->state = LI_READY;
1483 }
1484 }
1485
1486 /* the task MUST not be in the run queue anymore */
1487 task_delete(t);
1488 session_free(s);
1489 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001490 *next = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +02001491}
1492
Willy Tarreau92fb9832007-10-16 17:34:28 +02001493__attribute__((constructor))
1494static void __uxst_protocol_init(void)
1495{
1496 protocol_register(&proto_unix);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001497}
1498
1499
1500/*
1501 * Local variables:
1502 * c-indent-level: 8
1503 * c-basic-offset: 8
1504 * End:
1505 */