blob: b708689c11e133372f9f77f727602e0839c95473 [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>
35#include <common/time.h>
36#include <common/version.h>
37
38#include <types/acl.h>
39#include <types/capture.h>
40#include <types/client.h>
41#include <types/global.h>
42#include <types/polling.h>
43#include <types/proxy.h>
44#include <types/server.h>
45
46#include <proto/acl.h>
47#include <proto/backend.h>
48#include <proto/buffers.h>
Willy Tarreau3e76e722007-10-17 18:57:38 +020049#include <proto/dumpstats.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020050#include <proto/fd.h>
51#include <proto/log.h>
52#include <proto/protocols.h>
53#include <proto/proto_uxst.h>
54#include <proto/queue.h>
Willy Tarreau3e76e722007-10-17 18:57:38 +020055#include <proto/senddata.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020056#include <proto/session.h>
57#include <proto/stream_sock.h>
58#include <proto/task.h>
59
60#ifndef MAXPATHLEN
61#define MAXPATHLEN 128
62#endif
63
Willy Tarreaudabf2e22007-10-28 21:59:24 +010064static int uxst_bind_listeners(struct protocol *proto);
65static int uxst_unbind_listeners(struct protocol *proto);
66
67/* Note: must not be declared <const> as its list will be overwritten */
68static struct protocol proto_unix = {
69 .name = "unix_stream",
70 .sock_domain = PF_UNIX,
71 .sock_type = SOCK_STREAM,
72 .sock_prot = 0,
73 .sock_family = AF_UNIX,
74 .sock_addrlen = sizeof(struct sockaddr_un),
75 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
76 .read = &stream_sock_read,
77 .write = &stream_sock_write,
78 .bind_all = uxst_bind_listeners,
79 .unbind_all = uxst_unbind_listeners,
80 .enable_all = enable_all_listeners,
81 .disable_all = disable_all_listeners,
82 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
83 .nb_listeners = 0,
84};
85
86
87/********************************
88 * 1) low-level socket functions
89 ********************************/
90
91
Willy Tarreau92fb9832007-10-16 17:34:28 +020092/* This function creates a named PF_UNIX stream socket at address <path>. Note
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020093 * that the path cannot be NULL nor empty. <uid> and <gid> different of -1 will
94 * be used to change the socket owner. If <mode> is not 0, it will be used to
95 * restrict access to the socket. While it is known not to be portable on every
96 * OS, it's still useful where it works.
Willy Tarreau92fb9832007-10-16 17:34:28 +020097 * It returns the assigned file descriptor, or -1 in the event of an error.
98 */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020099static int create_uxst_socket(const char *path, uid_t uid, gid_t gid, mode_t mode)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200100{
101 char tempname[MAXPATHLEN];
102 char backname[MAXPATHLEN];
103 struct sockaddr_un addr;
104
105 int ret, sock;
106
107 /* 1. create socket names */
108 if (!path[0]) {
109 Alert("Invalid name for a UNIX socket. Aborting.\n");
110 goto err_return;
111 }
112
113 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
114 if (ret < 0 || ret >= MAXPATHLEN) {
115 Alert("name too long for UNIX socket. Aborting.\n");
116 goto err_return;
117 }
118
119 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
120 if (ret < 0 || ret >= MAXPATHLEN) {
121 Alert("name too long for UNIX socket. Aborting.\n");
122 goto err_return;
123 }
124
125 /* 2. clean existing orphaned entries */
126 if (unlink(tempname) < 0 && errno != ENOENT) {
127 Alert("error when trying to unlink previous UNIX socket. Aborting.\n");
128 goto err_return;
129 }
130
131 if (unlink(backname) < 0 && errno != ENOENT) {
132 Alert("error when trying to unlink previous UNIX socket. Aborting.\n");
133 goto err_return;
134 }
135
136 /* 3. backup existing socket */
137 if (link(path, backname) < 0 && errno != ENOENT) {
138 Alert("error when trying to preserve previous UNIX socket. Aborting.\n");
139 goto err_return;
140 }
141
142 /* 4. prepare new socket */
143 addr.sun_family = AF_UNIX;
144 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
145 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
146
147 sock = socket(PF_UNIX, SOCK_STREAM, 0);
148 if (sock < 0) {
149 Alert("cannot create socket for UNIX listener. Aborting.\n");
150 goto err_unlink_back;
151 }
152
153 if (sock >= global.maxsock) {
154 Alert("socket(): not enough free sockets for UNIX listener. Raise -n argument. Aborting.\n");
155 goto err_unlink_temp;
156 }
157
158 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
159 Alert("cannot make UNIX socket non-blocking. Aborting.\n");
160 goto err_unlink_temp;
161 }
162
163 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
164 /* note that bind() creates the socket <tempname> on the file system */
165 Alert("cannot bind socket for UNIX listener. Aborting.\n");
166 goto err_unlink_temp;
167 }
168
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200169 if (((uid != -1 || gid != -1) && (chown(tempname, uid, gid) == -1)) ||
170 (mode != 0 && chmod(tempname, mode) == -1)) {
171 Alert("cannot change UNIX socket ownership. Aborting.\n");
172 goto err_unlink_temp;
173 }
174
Willy Tarreau92fb9832007-10-16 17:34:28 +0200175 if (listen(sock, 0) < 0) {
176 Alert("cannot listen to socket for UNIX listener. Aborting.\n");
177 goto err_unlink_temp;
178 }
179
180 /* 5. install.
181 * Point of no return: we are ready, we'll switch the sockets. We don't
182 * fear loosing the socket <path> because we have a copy of it in
183 * backname.
184 */
185 if (rename(tempname, path) < 0) {
186 Alert("cannot switch final and temporary sockets for UNIX listener. Aborting.\n");
187 goto err_rename;
188 }
189
190 /* 6. cleanup */
191 unlink(backname); /* no need to keep this one either */
192
193 return sock;
194
195 err_rename:
196 ret = rename(backname, path);
197 if (ret < 0 && errno == ENOENT)
198 unlink(path);
199 err_unlink_temp:
200 unlink(tempname);
201 close(sock);
202 err_unlink_back:
203 unlink(backname);
204 err_return:
205 return -1;
206}
207
208/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
209 * anymore. It practises best effort, and no error is returned.
210 */
211static void destroy_uxst_socket(const char *path)
212{
213 struct sockaddr_un addr;
214 int sock, ret;
215
216 /* We might have been chrooted, so we may not be able to access the
217 * socket. In order to avoid bothering the other end, we connect with a
218 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
219 * is enough to know if the socket is still live or not. If it's live
220 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
221 * ECONNREFUSED. In this case, we do not touch it because it's used
222 * by some other process.
223 */
224 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
225 if (sock < 0)
226 return;
227
228 addr.sun_family = AF_UNIX;
229 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
Willy Tarreau10ae5482007-10-18 16:15:52 +0200230 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200231 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
232 if (ret < 0 && errno == ECONNREFUSED) {
233 /* Connect failed: the socket still exists but is not used
234 * anymore. Let's remove this socket now.
235 */
236 unlink(path);
237 }
238 close(sock);
239}
240
241
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100242/********************************
243 * 2) listener-oriented functions
244 ********************************/
245
246
247/* This function creates the UNIX socket associated to the listener. It changes
248 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
249 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
250 */
251static int uxst_bind_listener(struct listener *listener)
252{
253 int fd;
254
255 if (listener->state != LI_ASSIGNED)
256 return ERR_NONE; /* already bound */
257
258 fd = create_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path,
259 listener->perm.ux.uid,
260 listener->perm.ux.gid,
261 listener->perm.ux.mode);
262 if (fd == -1)
263 return ERR_FATAL;
264
265 /* the socket is now listening */
266 listener->fd = fd;
267 listener->state = LI_LISTEN;
268
269 /* the function for the accept() event */
270 fd_insert(fd);
271 fdtab[fd].cb[DIR_RD].f = listener->accept;
272 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
273 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
274 fdtab[fd].owner = (struct task *)listener; /* reference the listener instead of a task */
275 fdtab[fd].state = FD_STLISTEN;
276 fdtab[fd].peeraddr = NULL;
277 fdtab[fd].peerlen = 0;
278 fdtab[fd].listener = NULL;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100279 return ERR_NONE;
280}
281
282/* This function closes the UNIX sockets for the specified listener.
283 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
284 */
285static int uxst_unbind_listener(struct listener *listener)
286{
287 if (listener->state == LI_READY)
288 EV_FD_CLR(listener->fd, DIR_RD);
289
290 if (listener->state >= LI_LISTEN) {
Willy Tarreau8eebe5e2007-10-28 22:07:08 +0100291 fd_delete(listener->fd);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100292 listener->state = LI_ASSIGNED;
293 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
294 }
295 return ERR_NONE;
296}
297
298/* Add a listener to the list of unix stream listeners. The listener's state
299 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
300 * listeners is updated. This is the function to use to add a new listener.
301 */
302void uxst_add_listener(struct listener *listener)
303{
304 if (listener->state != LI_INIT)
305 return;
306 listener->state = LI_ASSIGNED;
307 listener->proto = &proto_unix;
308 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
309 proto_unix.nb_listeners++;
310}
311
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100312/********************************
313 * 3) protocol-oriented functions
314 ********************************/
315
316
Willy Tarreau92fb9832007-10-16 17:34:28 +0200317/* This function creates all UNIX sockets bound to the protocol entry <proto>.
318 * It is intended to be used as the protocol's bind_all() function.
319 * The sockets will be registered but not added to any fd_set, in order not to
320 * loose them across the fork(). A call to uxst_enable_listeners() is needed
321 * to complete initialization.
322 *
323 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
324 */
325static int uxst_bind_listeners(struct protocol *proto)
326{
327 struct listener *listener;
328 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200329
330 list_for_each_entry(listener, &proto->listeners, proto_list) {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100331 err |= uxst_bind_listener(listener);
332 if (err != ERR_NONE)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200333 continue;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200334 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200335 return err;
336}
337
Willy Tarreau92fb9832007-10-16 17:34:28 +0200338
339/* This function stops all listening UNIX sockets bound to the protocol
340 * <proto>. It does not detaches them from the protocol.
341 * It always returns ERR_NONE.
342 */
343static int uxst_unbind_listeners(struct protocol *proto)
344{
345 struct listener *listener;
346
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100347 list_for_each_entry(listener, &proto->listeners, proto_list)
348 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200349 return ERR_NONE;
350}
351
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100352
353/********************************
354 * 4) high-level functions
355 ********************************/
356
357
Willy Tarreau92fb9832007-10-16 17:34:28 +0200358/*
359 * This function is called on a read event from a listen socket, corresponding
360 * to an accept. It tries to accept as many connections as possible.
361 * It returns 0. Since we use UNIX sockets on the local system for monitoring
362 * purposes and other related things, we do not need to output as many messages
363 * as with TCP which can fall under attack.
364 */
365int uxst_event_accept(int fd) {
366 struct listener *l = (struct listener *)fdtab[fd].owner;
367 struct session *s;
368 struct task *t;
369 int cfd;
370 int max_accept;
371
372 if (global.nbproc > 1)
373 max_accept = 8; /* let other processes catch some connections too */
374 else
375 max_accept = -1;
376
377 while (max_accept--) {
378 struct sockaddr_storage addr;
379 socklen_t laddr = sizeof(addr);
380
381 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
382 switch (errno) {
383 case EAGAIN:
384 case EINTR:
385 case ECONNABORTED:
386 return 0; /* nothing more to accept */
387 case ENFILE:
388 /* Process reached system FD limit. Check system tunables. */
389 return 0;
390 case EMFILE:
391 /* Process reached process FD limit. Check 'ulimit-n'. */
392 return 0;
393 case ENOBUFS:
394 case ENOMEM:
395 /* Process reached system memory limit. Check system tunables. */
396 return 0;
397 default:
398 return 0;
399 }
400 }
401
402 if (l->nbconn >= l->maxconn) {
403 /* too many connections, we shoot this one and return.
404 * FIXME: it would be better to simply switch the listener's
405 * state to LI_FULL and disable the FD. We could re-enable
406 * it upon fd_delete(), but this requires all protocols to
407 * be switched.
408 */
409 close(cfd);
410 return 0;
411 }
412
413 if ((s = pool_alloc2(pool2_session)) == NULL) {
414 Alert("out of memory in uxst_event_accept().\n");
415 close(cfd);
416 return 0;
417 }
418
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100419 s->flags = 0;
420
Willy Tarreau92fb9832007-10-16 17:34:28 +0200421 if ((t = pool_alloc2(pool2_task)) == NULL) {
422 Alert("out of memory in uxst_event_accept().\n");
423 close(cfd);
424 pool_free2(pool2_session, s);
425 return 0;
426 }
427
428 s->cli_addr = addr;
429
430 /* FIXME: should be checked earlier */
431 if (cfd >= global.maxsock) {
432 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
433 close(cfd);
434 pool_free2(pool2_task, t);
435 pool_free2(pool2_session, s);
436 return 0;
437 }
438
439 if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) {
440 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
441 close(cfd);
442 pool_free2(pool2_task, t);
443 pool_free2(pool2_session, s);
444 return 0;
445 }
446
447 t->wq = NULL;
448 t->qlist.p = NULL;
449 t->state = TASK_IDLE;
450 t->process = l->handler;
451 t->context = s;
452
453 s->task = t;
454 s->fe = NULL;
455 s->be = NULL;
456
457 s->cli_state = CL_STDATA;
458 s->srv_state = SV_STIDLE;
459 s->req = s->rep = NULL; /* will be allocated later */
460
461 s->cli_fd = cfd;
462 s->srv_fd = -1;
463 s->srv = NULL;
464 s->pend_pos = NULL;
465
466 memset(&s->logs, 0, sizeof(s->logs));
467 memset(&s->txn, 0, sizeof(s->txn));
468
Willy Tarreau3e76e722007-10-17 18:57:38 +0200469 s->data_state = DATA_ST_INIT;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200470 s->data_source = DATA_SRC_NONE;
471 s->uniq_id = totalconn;
472
473 if ((s->req = pool_alloc2(pool2_buffer)) == NULL) { /* no memory */
474 close(cfd); /* nothing can be done for this fd without memory */
475 pool_free2(pool2_task, t);
476 pool_free2(pool2_session, s);
477 return 0;
478 }
479
480 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL) { /* no memory */
481 pool_free2(pool2_buffer, s->req);
482 close(cfd); /* nothing can be done for this fd without memory */
483 pool_free2(pool2_task, t);
484 pool_free2(pool2_session, s);
485 return 0;
486 }
487
488 buffer_init(s->req);
489 buffer_init(s->rep);
490 s->req->rlim += BUFSIZE;
491 s->rep->rlim += BUFSIZE;
492
493 fd_insert(cfd);
494 fdtab[cfd].owner = t;
495 fdtab[cfd].listener = l;
496 fdtab[cfd].state = FD_STREADY;
497 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
498 fdtab[cfd].cb[DIR_RD].b = s->req;
499 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
500 fdtab[cfd].cb[DIR_WR].b = s->rep;
501 fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
502 fdtab[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200503
504 tv_eternity(&s->req->rex);
505 tv_eternity(&s->req->wex);
506 tv_eternity(&s->req->cex);
507 tv_eternity(&s->rep->rex);
508 tv_eternity(&s->rep->wex);
509
510 tv_eternity(&s->req->wto);
511 tv_eternity(&s->req->cto);
512 tv_eternity(&s->req->rto);
513 tv_eternity(&s->rep->rto);
514 tv_eternity(&s->rep->cto);
515 tv_eternity(&s->rep->wto);
516
517 if (l->timeout)
518 s->req->rto = *l->timeout;
519
520 if (l->timeout)
521 s->rep->wto = *l->timeout;
522
523 tv_eternity(&t->expire);
524 if (l->timeout && tv_isset(l->timeout)) {
525 EV_FD_SET(cfd, DIR_RD);
526 tv_add(&s->req->rex, &now, &s->req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200527 t->expire = s->req->rex;
528 }
529
530 task_queue(t);
531 task_wakeup(t);
532
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++;
540
541 //fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p, cfd=%d, maxfd=%d\n", p, actconn, totalconn, t, cfd, maxfd);
542 } /* end of while (p->feconn < p->maxconn) */
543 //fprintf(stderr,"fct %s:%d\n", __FUNCTION__, __LINE__);
544 return 0;
545}
546
547/*
548 * manages the client FSM and its socket. It returns 1 if a state has changed
549 * (and a resync may be needed), otherwise 0.
550 */
551static int process_uxst_cli(struct session *t)
552{
553 int s = t->srv_state;
554 int c = t->cli_state;
555 struct buffer *req = t->req;
556 struct buffer *rep = t->rep;
557 //fprintf(stderr,"fct %s:%d\n", __FUNCTION__, __LINE__);
558 if (c == CL_STDATA) {
559 /* FIXME: this error handling is partly buggy because we always report
560 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
561 * or HEADER phase. BTW, it's not logical to expire the client while
562 * we're waiting for the server to connect.
563 */
564 /* read or write error */
565 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
566 buffer_shutr(req);
567 buffer_shutw(rep);
568 fd_delete(t->cli_fd);
569 t->cli_state = CL_STCLOSE;
570 if (!(t->flags & SN_ERR_MASK))
571 t->flags |= SN_ERR_CLICL;
572 if (!(t->flags & SN_FINST_MASK)) {
573 if (t->pend_pos)
574 t->flags |= SN_FINST_Q;
575 else if (s == SV_STCONN)
576 t->flags |= SN_FINST_C;
577 else
578 t->flags |= SN_FINST_D;
579 }
580 return 1;
581 }
582 /* last read, or end of server write */
583 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
584 EV_FD_CLR(t->cli_fd, DIR_RD);
585 buffer_shutr(req);
586 t->cli_state = CL_STSHUTR;
587 return 1;
588 }
589 /* last server read and buffer empty */
590 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
591 EV_FD_CLR(t->cli_fd, DIR_WR);
592 buffer_shutw(rep);
593 shutdown(t->cli_fd, SHUT_WR);
594 /* We must ensure that the read part is still alive when switching
595 * to shutw */
596 EV_FD_SET(t->cli_fd, DIR_RD);
597 tv_add_ifset(&req->rex, &now, &req->rto);
598 t->cli_state = CL_STSHUTW;
599 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
600 return 1;
601 }
602 /* read timeout */
603 else if (tv_isle(&req->rex, &now)) {
604 EV_FD_CLR(t->cli_fd, DIR_RD);
605 buffer_shutr(req);
606 t->cli_state = CL_STSHUTR;
607 if (!(t->flags & SN_ERR_MASK))
608 t->flags |= SN_ERR_CLITO;
609 if (!(t->flags & SN_FINST_MASK)) {
610 if (t->pend_pos)
611 t->flags |= SN_FINST_Q;
612 else if (s == SV_STCONN)
613 t->flags |= SN_FINST_C;
614 else
615 t->flags |= SN_FINST_D;
616 }
617 return 1;
618 }
619 /* write timeout */
620 else if (tv_isle(&rep->wex, &now)) {
621 EV_FD_CLR(t->cli_fd, DIR_WR);
622 buffer_shutw(rep);
623 shutdown(t->cli_fd, SHUT_WR);
624 /* We must ensure that the read part is still alive when switching
625 * to shutw */
626 EV_FD_SET(t->cli_fd, DIR_RD);
627 tv_add_ifset(&req->rex, &now, &req->rto);
628
629 t->cli_state = CL_STSHUTW;
630 if (!(t->flags & SN_ERR_MASK))
631 t->flags |= SN_ERR_CLITO;
632 if (!(t->flags & SN_FINST_MASK)) {
633 if (t->pend_pos)
634 t->flags |= SN_FINST_Q;
635 else if (s == SV_STCONN)
636 t->flags |= SN_FINST_C;
637 else
638 t->flags |= SN_FINST_D;
639 }
640 return 1;
641 }
642
643 if (req->l >= req->rlim - req->data) {
644 /* no room to read more data */
645 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
646 /* stop reading until we get some space */
647 tv_eternity(&req->rex);
648 }
649 } else {
650 /* there's still some space in the buffer */
651 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
652 if (!tv_isset(&req->rto) ||
653 (t->srv_state < SV_STDATA && tv_isset(&req->wto)))
654 /* If the client has no timeout, or if the server not ready yet, and we
655 * know for sure that it can expire, then it's cleaner to disable the
656 * timeout on the client side so that too low values cannot make the
657 * sessions abort too early.
658 */
659 tv_eternity(&req->rex);
660 else
661 tv_add(&req->rex, &now, &req->rto);
662 }
663 }
664
665 if ((rep->l == 0) ||
666 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
667 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
668 /* stop writing */
669 tv_eternity(&rep->wex);
670 }
671 } else {
672 /* buffer not empty */
673 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
674 /* restart writing */
675 if (tv_add_ifset(&rep->wex, &now, &rep->wto)) {
676 /* FIXME: to prevent the client from expiring read timeouts during writes,
677 * we refresh it. */
678 req->rex = rep->wex;
679 }
680 else
681 tv_eternity(&rep->wex);
682 }
683 }
684 return 0; /* other cases change nothing */
685 }
686 else if (c == CL_STSHUTR) {
687 if (rep->flags & BF_WRITE_ERROR) {
688 buffer_shutw(rep);
689 fd_delete(t->cli_fd);
690 t->cli_state = CL_STCLOSE;
691 if (!(t->flags & SN_ERR_MASK))
692 t->flags |= SN_ERR_CLICL;
693 if (!(t->flags & SN_FINST_MASK)) {
694 if (t->pend_pos)
695 t->flags |= SN_FINST_Q;
696 else if (s == SV_STCONN)
697 t->flags |= SN_FINST_C;
698 else
699 t->flags |= SN_FINST_D;
700 }
701 return 1;
702 }
703 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
704 buffer_shutw(rep);
705 fd_delete(t->cli_fd);
706 t->cli_state = CL_STCLOSE;
707 return 1;
708 }
709 else if (tv_isle(&rep->wex, &now)) {
710 buffer_shutw(rep);
711 fd_delete(t->cli_fd);
712 t->cli_state = CL_STCLOSE;
713 if (!(t->flags & SN_ERR_MASK))
714 t->flags |= SN_ERR_CLITO;
715 if (!(t->flags & SN_FINST_MASK)) {
716 if (t->pend_pos)
717 t->flags |= SN_FINST_Q;
718 else if (s == SV_STCONN)
719 t->flags |= SN_FINST_C;
720 else
721 t->flags |= SN_FINST_D;
722 }
723 return 1;
724 }
725
726 if (rep->l == 0) {
727 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
728 /* stop writing */
729 tv_eternity(&rep->wex);
730 }
731 } else {
732 /* buffer not empty */
733 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
734 /* restart writing */
735 if (!tv_add_ifset(&rep->wex, &now, &rep->wto))
736 tv_eternity(&rep->wex);
737 }
738 }
739 return 0;
740 }
741 else if (c == CL_STSHUTW) {
742 if (req->flags & BF_READ_ERROR) {
743 buffer_shutr(req);
744 fd_delete(t->cli_fd);
745 t->cli_state = CL_STCLOSE;
746 if (!(t->flags & SN_ERR_MASK))
747 t->flags |= SN_ERR_CLICL;
748 if (!(t->flags & SN_FINST_MASK)) {
749 if (t->pend_pos)
750 t->flags |= SN_FINST_Q;
751 else if (s == SV_STCONN)
752 t->flags |= SN_FINST_C;
753 else
754 t->flags |= SN_FINST_D;
755 }
756 return 1;
757 }
758 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
759 buffer_shutr(req);
760 fd_delete(t->cli_fd);
761 t->cli_state = CL_STCLOSE;
762 return 1;
763 }
764 else if (tv_isle(&req->rex, &now)) {
765 buffer_shutr(req);
766 fd_delete(t->cli_fd);
767 t->cli_state = CL_STCLOSE;
768 if (!(t->flags & SN_ERR_MASK))
769 t->flags |= SN_ERR_CLITO;
770 if (!(t->flags & SN_FINST_MASK)) {
771 if (t->pend_pos)
772 t->flags |= SN_FINST_Q;
773 else if (s == SV_STCONN)
774 t->flags |= SN_FINST_C;
775 else
776 t->flags |= SN_FINST_D;
777 }
778 return 1;
779 }
780 else if (req->l >= req->rlim - req->data) {
781 /* no room to read more data */
782
783 /* FIXME-20050705: is it possible for a client to maintain a session
784 * after the timeout by sending more data after it receives a close ?
785 */
786
787 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
788 /* stop reading until we get some space */
789 tv_eternity(&req->rex);
790 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
791 }
792 } else {
793 /* there's still some space in the buffer */
794 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
795 if (!tv_add_ifset(&req->rex, &now, &req->rto))
796 tv_eternity(&req->rex);
797 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
798 }
799 }
800 return 0;
801 }
802 else { /* CL_STCLOSE: nothing to do */
803 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
804 int len;
805 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be?t->be->id:"",
806 (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
807 write(1, trash, len);
808 }
809 return 0;
810 }
811 return 0;
812}
813
814#if 0
815 /* FIXME! This part has not been completely converted yet, and it may
816 * still be very specific to TCPv4 ! Also, it relies on some parameters
817 * such as conn_retries which are not set upon accept().
818 */
819/*
820 * Manages the server FSM and its socket. It returns 1 if a state has changed
821 * (and a resync may be needed), otherwise 0.
822 */
823static int process_uxst_srv(struct session *t)
824{
825 int s = t->srv_state;
826 int c = t->cli_state;
827 struct buffer *req = t->req;
828 struct buffer *rep = t->rep;
829 int conn_err;
830
831 if (s == SV_STIDLE) {
832 if (c == CL_STCLOSE || c == CL_STSHUTW ||
833 (c == CL_STSHUTR &&
834 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
835 tv_eternity(&req->cex);
836 if (t->pend_pos)
837 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
838 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C);
839 return 1;
840 }
841 else {
842 /* FIXME: reimplement the TARPIT check here */
843
844 /* Right now, we will need to create a connection to the server.
845 * We might already have tried, and got a connection pending, in
846 * which case we will not do anything till it's pending. It's up
847 * to any other session to release it and wake us up again.
848 */
849 if (t->pend_pos) {
850 if (!tv_isle(&req->cex, &now))
851 return 0;
852 else {
853 /* we've been waiting too long here */
854 tv_eternity(&req->cex);
855 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
856 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q);
857 if (t->srv)
858 t->srv->failed_conns++;
859 if (t->fe)
860 t->fe->failed_conns++;
861 return 1;
862 }
863 }
864
865 do {
866 /* first, get a connection */
867 if (srv_redispatch_connect(t))
868 return t->srv_state != SV_STIDLE;
869
870 /* try to (re-)connect to the server, and fail if we expire the
871 * number of retries.
872 */
873 if (srv_retryable_connect(t)) {
874 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
875 return t->srv_state != SV_STIDLE;
876 }
877 } while (1);
878 }
879 }
880 else if (s == SV_STCONN) { /* connection in progress */
881 if (c == CL_STCLOSE || c == CL_STSHUTW ||
882 (c == CL_STSHUTR &&
883 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
884 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
885 tv_eternity(&req->cex);
886 fd_delete(t->srv_fd);
887 if (t->srv)
888 t->srv->cur_sess--;
889
890 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C);
891 return 1;
892 }
893 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
894 //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);
895 return 0; /* nothing changed */
896 }
897 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
898 /* timeout, asynchronous connect error or first write error */
899 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
900
901 fd_delete(t->srv_fd);
902 if (t->srv)
903 t->srv->cur_sess--;
904
905 if (!(req->flags & BF_WRITE_STATUS))
906 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
907 else
908 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
909
910 /* ensure that we have enough retries left */
911 if (srv_count_retry_down(t, conn_err))
912 return 1;
913
914 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
915 /* We're on our last chance, and the REDISP option was specified.
916 * We will ignore cookie and force to balance or use the dispatcher.
917 */
918 /* let's try to offer this slot to anybody */
919 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200920 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200921
922 if (t->srv)
923 t->srv->failed_conns++;
924 t->be->failed_conns++;
925
926 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
927 t->srv = NULL; /* it's left to the dispatcher to choose a server */
928
929 /* first, get a connection */
930 if (srv_redispatch_connect(t))
931 return t->srv_state != SV_STIDLE;
932 }
933
934 do {
935 /* Now we will try to either reconnect to the same server or
936 * connect to another server. If the connection gets queued
937 * because all servers are saturated, then we will go back to
938 * the SV_STIDLE state.
939 */
940 if (srv_retryable_connect(t)) {
941 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
942 return t->srv_state != SV_STCONN;
943 }
944
945 /* we need to redispatch the connection to another server */
946 if (srv_redispatch_connect(t))
947 return t->srv_state != SV_STCONN;
948 } while (1);
949 }
950 else { /* no error or write 0 */
951 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
952
953 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
954 if (req->l == 0) /* nothing to write */ {
955 EV_FD_CLR(t->srv_fd, DIR_WR);
956 tv_eternity(&req->wex);
957 } else /* need the right to write */ {
958 EV_FD_SET(t->srv_fd, DIR_WR);
959 if (tv_add_ifset(&req->wex, &now, &req->wto)) {
960 /* FIXME: to prevent the server from expiring read timeouts during writes,
961 * we refresh it. */
962 rep->rex = req->wex;
963 }
964 else
965 tv_eternity(&req->wex);
966 }
967
968 EV_FD_SET(t->srv_fd, DIR_RD);
969 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
970 tv_eternity(&rep->rex);
971
972 t->srv_state = SV_STDATA;
973 if (t->srv)
974 t->srv->cum_sess++;
975 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
976
977 /* if the user wants to log as soon as possible, without counting
978 bytes from the server, then this is the right moment. */
979 if (t->fe && t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
980 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
981 //uxst_sess_log(t);
982 }
983 tv_eternity(&req->cex);
984 return 1;
985 }
986 }
987 else if (s == SV_STDATA) {
988 /* read or write error */
989 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
990 buffer_shutr(rep);
991 buffer_shutw(req);
992 fd_delete(t->srv_fd);
993 if (t->srv) {
994 t->srv->cur_sess--;
995 t->srv->failed_resp++;
996 }
997 t->be->failed_resp++;
998 t->srv_state = SV_STCLOSE;
999 if (!(t->flags & SN_ERR_MASK))
1000 t->flags |= SN_ERR_SRVCL;
1001 if (!(t->flags & SN_FINST_MASK))
1002 t->flags |= SN_FINST_D;
1003 /* We used to have a free connection slot. Since we'll never use it,
1004 * we have to inform the server that it may be used by another session.
1005 */
1006 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001007 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001008
1009 return 1;
1010 }
1011 /* last read, or end of client write */
1012 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
1013 EV_FD_CLR(t->srv_fd, DIR_RD);
1014 buffer_shutr(rep);
1015 t->srv_state = SV_STSHUTR;
1016 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1017 return 1;
1018 }
1019 /* end of client read and no more data to send */
1020 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
1021 EV_FD_CLR(t->srv_fd, DIR_WR);
1022 buffer_shutw(req);
1023 shutdown(t->srv_fd, SHUT_WR);
1024 /* We must ensure that the read part is still alive when switching
1025 * to shutw */
1026 EV_FD_SET(t->srv_fd, DIR_RD);
1027 tv_add_ifset(&rep->rex, &now, &rep->rto);
1028
1029 t->srv_state = SV_STSHUTW;
1030 return 1;
1031 }
1032 /* read timeout */
1033 else if (tv_isle(&rep->rex, &now)) {
1034 EV_FD_CLR(t->srv_fd, DIR_RD);
1035 buffer_shutr(rep);
1036 t->srv_state = SV_STSHUTR;
1037 if (!(t->flags & SN_ERR_MASK))
1038 t->flags |= SN_ERR_SRVTO;
1039 if (!(t->flags & SN_FINST_MASK))
1040 t->flags |= SN_FINST_D;
1041 return 1;
1042 }
1043 /* write timeout */
1044 else if (tv_isle(&req->wex, &now)) {
1045 EV_FD_CLR(t->srv_fd, DIR_WR);
1046 buffer_shutw(req);
1047 shutdown(t->srv_fd, SHUT_WR);
1048 /* We must ensure that the read part is still alive when switching
1049 * to shutw */
1050 EV_FD_SET(t->srv_fd, DIR_RD);
1051 tv_add_ifset(&rep->rex, &now, &rep->rto);
1052 t->srv_state = SV_STSHUTW;
1053 if (!(t->flags & SN_ERR_MASK))
1054 t->flags |= SN_ERR_SRVTO;
1055 if (!(t->flags & SN_FINST_MASK))
1056 t->flags |= SN_FINST_D;
1057 return 1;
1058 }
1059
1060 /* recompute request time-outs */
1061 if (req->l == 0) {
1062 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
1063 /* stop writing */
1064 tv_eternity(&req->wex);
1065 }
1066 }
1067 else { /* buffer not empty, there are still data to be transferred */
1068 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
1069 /* restart writing */
1070 if (tv_add_ifset(&req->wex, &now, &req->wto)) {
1071 /* FIXME: to prevent the server from expiring read timeouts during writes,
1072 * we refresh it. */
1073 rep->rex = req->wex;
1074 }
1075 else
1076 tv_eternity(&req->wex);
1077 }
1078 }
1079
1080 /* recompute response time-outs */
1081 if (rep->l == BUFSIZE) { /* no room to read more data */
1082 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
1083 tv_eternity(&rep->rex);
1084 }
1085 }
1086 else {
1087 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
1088 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
1089 tv_eternity(&rep->rex);
1090 }
1091 }
1092
1093 return 0; /* other cases change nothing */
1094 }
1095 else if (s == SV_STSHUTR) {
1096 if (req->flags & BF_WRITE_ERROR) {
1097 //EV_FD_CLR(t->srv_fd, DIR_WR);
1098 buffer_shutw(req);
1099 fd_delete(t->srv_fd);
1100 if (t->srv) {
1101 t->srv->cur_sess--;
1102 t->srv->failed_resp++;
1103 }
1104 t->be->failed_resp++;
1105 //close(t->srv_fd);
1106 t->srv_state = SV_STCLOSE;
1107 if (!(t->flags & SN_ERR_MASK))
1108 t->flags |= SN_ERR_SRVCL;
1109 if (!(t->flags & SN_FINST_MASK))
1110 t->flags |= SN_FINST_D;
1111 /* We used to have a free connection slot. Since we'll never use it,
1112 * we have to inform the server that it may be used by another session.
1113 */
1114 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001115 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001116
1117 return 1;
1118 }
1119 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
1120 //EV_FD_CLR(t->srv_fd, DIR_WR);
1121 buffer_shutw(req);
1122 fd_delete(t->srv_fd);
1123 if (t->srv)
1124 t->srv->cur_sess--;
1125 //close(t->srv_fd);
1126 t->srv_state = SV_STCLOSE;
1127 /* We used to have a free connection slot. Since we'll never use it,
1128 * we have to inform the server that it may be used by another session.
1129 */
1130 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001131 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001132
1133 return 1;
1134 }
1135 else if (tv_isle(&req->wex, &now)) {
1136 //EV_FD_CLR(t->srv_fd, DIR_WR);
1137 buffer_shutw(req);
1138 fd_delete(t->srv_fd);
1139 if (t->srv)
1140 t->srv->cur_sess--;
1141 //close(t->srv_fd);
1142 t->srv_state = SV_STCLOSE;
1143 if (!(t->flags & SN_ERR_MASK))
1144 t->flags |= SN_ERR_SRVTO;
1145 if (!(t->flags & SN_FINST_MASK))
1146 t->flags |= SN_FINST_D;
1147 /* We used to have a free connection slot. Since we'll never use it,
1148 * we have to inform the server that it may be used by another session.
1149 */
1150 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001151 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001152
1153 return 1;
1154 }
1155 else if (req->l == 0) {
1156 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
1157 /* stop writing */
1158 tv_eternity(&req->wex);
1159 }
1160 }
1161 else { /* buffer not empty */
1162 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
1163 /* restart writing */
1164 if (!tv_add_ifset(&req->wex, &now, &req->wto))
1165 tv_eternity(&req->wex);
1166 }
1167 }
1168 return 0;
1169 }
1170 else if (s == SV_STSHUTW) {
1171 if (rep->flags & BF_READ_ERROR) {
1172 //EV_FD_CLR(t->srv_fd, DIR_RD);
1173 buffer_shutr(rep);
1174 fd_delete(t->srv_fd);
1175 if (t->srv) {
1176 t->srv->cur_sess--;
1177 t->srv->failed_resp++;
1178 }
1179 t->be->failed_resp++;
1180 //close(t->srv_fd);
1181 t->srv_state = SV_STCLOSE;
1182 if (!(t->flags & SN_ERR_MASK))
1183 t->flags |= SN_ERR_SRVCL;
1184 if (!(t->flags & SN_FINST_MASK))
1185 t->flags |= SN_FINST_D;
1186 /* We used to have a free connection slot. Since we'll never use it,
1187 * we have to inform the server that it may be used by another session.
1188 */
1189 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001190 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001191
1192 return 1;
1193 }
1194 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
1195 //EV_FD_CLR(t->srv_fd, DIR_RD);
1196 buffer_shutr(rep);
1197 fd_delete(t->srv_fd);
1198 if (t->srv)
1199 t->srv->cur_sess--;
1200 //close(t->srv_fd);
1201 t->srv_state = SV_STCLOSE;
1202 /* We used to have a free connection slot. Since we'll never use it,
1203 * we have to inform the server that it may be used by another session.
1204 */
1205 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001206 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001207
1208 return 1;
1209 }
1210 else if (tv_isle(&rep->rex, &now)) {
1211 //EV_FD_CLR(t->srv_fd, DIR_RD);
1212 buffer_shutr(rep);
1213 fd_delete(t->srv_fd);
1214 if (t->srv)
1215 t->srv->cur_sess--;
1216 //close(t->srv_fd);
1217 t->srv_state = SV_STCLOSE;
1218 if (!(t->flags & SN_ERR_MASK))
1219 t->flags |= SN_ERR_SRVTO;
1220 if (!(t->flags & SN_FINST_MASK))
1221 t->flags |= SN_FINST_D;
1222 /* We used to have a free connection slot. Since we'll never use it,
1223 * we have to inform the server that it may be used by another session.
1224 */
1225 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001226 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001227
1228 return 1;
1229 }
1230 else if (rep->l == BUFSIZE) { /* no room to read more data */
1231 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
1232 tv_eternity(&rep->rex);
1233 }
1234 }
1235 else {
1236 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
1237 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
1238 tv_eternity(&rep->rex);
1239 }
1240 }
1241 return 0;
1242 }
1243 else { /* SV_STCLOSE : nothing to do */
1244 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1245 int len;
1246 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1247 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
1248 write(1, trash, len);
1249 }
1250 return 0;
1251 }
1252 return 0;
1253}
1254
1255/* Processes the client and server jobs of a session task, then
1256 * puts it back to the wait queue in a clean state, or
1257 * cleans up its resources if it must be deleted. Returns
1258 * the time the task accepts to wait, or TIME_ETERNITY for
1259 * infinity.
1260 */
1261void process_uxst_session(struct task *t, struct timeval *next)
1262{
1263 struct session *s = t->context;
1264 int fsm_resync = 0;
1265
1266 do {
1267 fsm_resync = 0;
1268 fsm_resync |= process_uxst_cli(s);
1269 if (s->srv_state == SV_STIDLE) {
1270 if (s->cli_state == CL_STCLOSE || s->cli_state == CL_STSHUTW) {
1271 s->srv_state = SV_STCLOSE;
1272 fsm_resync |= 1;
1273 continue;
1274 }
1275 if (s->cli_state == CL_STSHUTR ||
1276 (s->req->l >= s->req->rlim - s->req->data)) {
1277 if (s->req->l == 0) {
1278 s->srv_state = SV_STCLOSE;
1279 fsm_resync |= 1;
1280 continue;
1281 }
1282 /* OK we have some remaining data to process */
1283 /* Just as an exercice, we copy the req into the resp,
1284 * and flush the req.
1285 */
1286 memcpy(s->rep->data, s->req->data, sizeof(s->rep->data));
1287 s->rep->l = s->req->l;
1288 s->rep->rlim = s->rep->data + BUFSIZE;
1289 s->rep->w = s->rep->data;
1290 s->rep->lr = s->rep->r = s->rep->data + s->rep->l;
1291
1292 s->req->l = 0;
1293 s->srv_state = SV_STCLOSE;
1294
1295 fsm_resync |= 1;
1296 continue;
1297 }
1298 }
1299 } while (fsm_resync);
1300
1301 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001302
1303 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1304 session_process_counters(s);
1305
Willy Tarreau92fb9832007-10-16 17:34:28 +02001306 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1307 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1308
1309 t->expire = s->req->rex;
1310 tv_min(&t->expire, &s->req->rex, &s->req->wex);
1311 tv_bound(&t->expire, &s->req->cex);
1312 tv_bound(&t->expire, &s->rep->rex);
1313 tv_bound(&t->expire, &s->rep->wex);
1314
1315 /* restore t to its place in the task list */
1316 task_queue(t);
1317
1318 *next = t->expire;
1319 return; /* nothing more to do */
1320 }
1321
1322 if (s->fe)
1323 s->fe->feconn--;
1324 if (s->be && (s->flags & SN_BE_ASSIGNED))
1325 s->be->beconn--;
1326 actconn--;
1327
1328 if (unlikely((global.mode & MODE_DEBUG) &&
1329 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1330 int len;
1331 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
1332 s->uniq_id, s->be->id,
1333 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
1334 write(1, trash, len);
1335 }
1336
1337 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001338 session_process_counters(s);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001339
1340 /* let's do a final log if we need it */
1341 if (s->logs.logwait &&
1342 !(s->flags & SN_MONITOR) &&
1343 (s->req->total || !(s->fe && s->fe->options & PR_O_NULLNOLOG))) {
1344 //uxst_sess_log(s);
1345 }
1346
1347 /* the task MUST not be in the run queue anymore */
1348 task_delete(t);
1349 session_free(s);
1350 task_free(t);
1351 tv_eternity(next);
1352}
1353#endif /* not converted */
1354
1355
1356/* Processes data exchanges on the statistics socket. The client processing
1357 * is called and the task is put back in the wait queue or it is cleared.
1358 * In order to ease the transition, we simply simulate the server status
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001359 * for now. It only knows states SV_STIDLE, SV_STCONN, SV_STDATA, and
1360 * SV_STCLOSE. Returns in <next> the task's expiration date.
Willy Tarreau92fb9832007-10-16 17:34:28 +02001361 */
1362void process_uxst_stats(struct task *t, struct timeval *next)
1363{
1364 struct session *s = t->context;
1365 struct listener *listener;
1366 int fsm_resync = 0;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001367 int last_rep_l;
Willy Tarreau92fb9832007-10-16 17:34:28 +02001368
1369 do {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001370 char *args[MAX_UXST_ARGS + 1];
1371 char *line, *p;
1372 int arg;
1373
Willy Tarreau3e76e722007-10-17 18:57:38 +02001374 fsm_resync = process_uxst_cli(s);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001375
1376 if (s->cli_state == CL_STCLOSE || s->cli_state == CL_STSHUTW) {
1377 s->srv_state = SV_STCLOSE;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001378 break;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001379 }
1380
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001381 switch (s->srv_state) {
1382 case SV_STIDLE:
1383 /* stats output not initialized yet */
1384 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
1385 s->data_source = DATA_SRC_STATS;
1386 s->srv_state = SV_STCONN;
1387 fsm_resync |= 1;
1388 break;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001389
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001390 case SV_STCONN: /* will change to SV_STANALYZE */
1391 /* stats initialized, but waiting for the command */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001392 line = s->req->data;
1393 p = memchr(line, '\n', s->req->l);
1394
1395 if (!p)
1396 continue;
1397
1398 *p = '\0';
1399
1400 while (isspace((unsigned char)*line))
1401 line++;
1402
1403 arg = 0;
1404 args[arg] = line;
1405
1406 while (*line && arg < MAX_UXST_ARGS) {
1407 if (isspace((unsigned char)*line)) {
1408 *line++ = '\0';
1409
1410 while (isspace((unsigned char)*line))
1411 line++;
1412
1413 args[++arg] = line;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001414 continue;
1415 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001416
1417 line++;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001418 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001419
1420 while (++arg <= MAX_UXST_ARGS)
1421 args[arg] = line;
1422
1423 if (!strcmp(args[0], "show")) {
1424 if (!strcmp(args[1], "stat")) {
1425 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001426 s->data_ctx.stats.flags |= STAT_BOUND;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001427 s->data_ctx.stats.iid = atoi(args[2]);
1428 s->data_ctx.stats.type = atoi(args[3]);
1429 s->data_ctx.stats.sid = atoi(args[4]);
1430 }
1431
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001432 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
1433 s->data_ctx.stats.flags |= STAT_FMT_CSV;
1434 s->srv_state = SV_STDATA;
1435 fsm_resync |= 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +02001436 continue;
1437 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001438
1439 if (!strcmp(args[1], "info")) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001440 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
1441 s->data_ctx.stats.flags |= STAT_FMT_CSV;
1442 s->srv_state = SV_STDATA;
1443 fsm_resync |= 1;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001444 continue;
1445 }
Willy Tarreau92fb9832007-10-16 17:34:28 +02001446 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001447
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001448 s->srv_state = SV_STCLOSE;
1449 fsm_resync |= 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001450 continue;
1451
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001452 case SV_STDATA:
1453 /* OK we have to process the request. Since it is possible
1454 * that we get there with the client output paused, we
1455 * will simply check that we have really sent some data
1456 * and wake the client up if needed.
1457 */
1458 last_rep_l = s->rep->l;
1459 if (stats_dump_raw(s, NULL) != 0) {
1460 s->srv_state = SV_STCLOSE;
1461 fsm_resync |= 1;
1462 }
1463 if (s->rep->l != last_rep_l)
1464 fsm_resync |= 1;
1465 break;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001466 }
Willy Tarreau92fb9832007-10-16 17:34:28 +02001467 } while (fsm_resync);
1468
1469 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
1470 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1471 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1472
1473 t->expire = s->req->rex;
1474 tv_min(&t->expire, &s->req->rex, &s->req->wex);
1475 tv_bound(&t->expire, &s->req->cex);
1476 tv_bound(&t->expire, &s->rep->rex);
1477 tv_bound(&t->expire, &s->rep->wex);
1478
1479 /* restore t to its place in the task list */
1480 task_queue(t);
1481
1482 *next = t->expire;
1483 return; /* nothing more to do */
1484 }
1485
1486 actconn--;
1487 listener = fdtab[s->cli_fd].listener;
1488 if (listener) {
1489 listener->nbconn--;
1490 if (listener->state == LI_FULL &&
1491 listener->nbconn < listener->maxconn) {
1492 /* we should reactivate the listener */
1493 EV_FD_SET(listener->fd, DIR_RD);
1494 listener->state = LI_READY;
1495 }
1496 }
1497
1498 /* the task MUST not be in the run queue anymore */
1499 task_delete(t);
1500 session_free(s);
1501 task_free(t);
1502 tv_eternity(next);
1503}
1504
Willy Tarreau92fb9832007-10-16 17:34:28 +02001505__attribute__((constructor))
1506static void __uxst_protocol_init(void)
1507{
1508 protocol_register(&proto_unix);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001509}
1510
1511
1512/*
1513 * Local variables:
1514 * c-indent-level: 8
1515 * c-basic-offset: 8
1516 * End:
1517 */