blob: 8c892416712a818e37819d2cc8e54f81b76fe34a [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;
415
Willy Tarreau92fb9832007-10-16 17:34:28 +0200416 if ((t = pool_alloc2(pool2_task)) == NULL) {
417 Alert("out of memory in uxst_event_accept().\n");
418 close(cfd);
419 pool_free2(pool2_session, s);
420 return 0;
421 }
422
423 s->cli_addr = addr;
424
425 /* FIXME: should be checked earlier */
426 if (cfd >= global.maxsock) {
427 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
428 close(cfd);
429 pool_free2(pool2_task, t);
430 pool_free2(pool2_session, s);
431 return 0;
432 }
433
434 if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) {
435 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
436 close(cfd);
437 pool_free2(pool2_task, t);
438 pool_free2(pool2_session, s);
439 return 0;
440 }
441
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200442 task_init(t);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200443 t->process = l->handler;
444 t->context = s;
Willy Tarreau91e99932008-06-30 07:51:00 +0200445 t->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200446
447 s->task = t;
448 s->fe = NULL;
449 s->be = NULL;
450
451 s->cli_state = CL_STDATA;
452 s->srv_state = SV_STIDLE;
453 s->req = s->rep = NULL; /* will be allocated later */
454
455 s->cli_fd = cfd;
456 s->srv_fd = -1;
457 s->srv = NULL;
458 s->pend_pos = NULL;
459
460 memset(&s->logs, 0, sizeof(s->logs));
461 memset(&s->txn, 0, sizeof(s->txn));
462
Willy Tarreau3e76e722007-10-17 18:57:38 +0200463 s->data_state = DATA_ST_INIT;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200464 s->data_source = DATA_SRC_NONE;
465 s->uniq_id = totalconn;
466
467 if ((s->req = pool_alloc2(pool2_buffer)) == NULL) { /* no memory */
468 close(cfd); /* nothing can be done for this fd without memory */
469 pool_free2(pool2_task, t);
470 pool_free2(pool2_session, s);
471 return 0;
472 }
473
474 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL) { /* no memory */
475 pool_free2(pool2_buffer, s->req);
476 close(cfd); /* nothing can be done for this fd without memory */
477 pool_free2(pool2_task, t);
478 pool_free2(pool2_session, s);
479 return 0;
480 }
481
482 buffer_init(s->req);
483 buffer_init(s->rep);
484 s->req->rlim += BUFSIZE;
485 s->rep->rlim += BUFSIZE;
486
487 fd_insert(cfd);
488 fdtab[cfd].owner = t;
489 fdtab[cfd].listener = l;
490 fdtab[cfd].state = FD_STREADY;
491 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
492 fdtab[cfd].cb[DIR_RD].b = s->req;
493 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
494 fdtab[cfd].cb[DIR_WR].b = s->rep;
495 fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
496 fdtab[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200497
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200498 s->req->rex = TICK_ETERNITY;
499 s->req->wex = TICK_ETERNITY;
500 s->req->cex = TICK_ETERNITY;
501 s->rep->rex = TICK_ETERNITY;
502 s->rep->wex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200503
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200504 s->req->wto = TICK_ETERNITY;
505 s->req->cto = TICK_ETERNITY;
506 s->req->rto = TICK_ETERNITY;
507 s->rep->rto = TICK_ETERNITY;
508 s->rep->cto = TICK_ETERNITY;
509 s->rep->wto = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200510
511 if (l->timeout)
512 s->req->rto = *l->timeout;
513
514 if (l->timeout)
515 s->rep->wto = *l->timeout;
516
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200517 t->expire = TICK_ETERNITY;
518 if (l->timeout && *l->timeout) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200519 EV_FD_SET(cfd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200520 s->req->rex = tick_add(now_ms, s->req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200521 t->expire = s->req->rex;
522 }
523
Willy Tarreau92fb9832007-10-16 17:34:28 +0200524 task_wakeup(t);
525
526 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
527 if (l->nbconn >= l->maxconn) {
528 EV_FD_CLR(l->fd, DIR_RD);
529 l->state = LI_FULL;
530 }
531 actconn++;
532 totalconn++;
533
534 //fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p, cfd=%d, maxfd=%d\n", p, actconn, totalconn, t, cfd, maxfd);
535 } /* end of while (p->feconn < p->maxconn) */
536 //fprintf(stderr,"fct %s:%d\n", __FUNCTION__, __LINE__);
537 return 0;
538}
539
540/*
541 * manages the client FSM and its socket. It returns 1 if a state has changed
542 * (and a resync may be needed), otherwise 0.
543 */
544static int process_uxst_cli(struct session *t)
545{
546 int s = t->srv_state;
547 int c = t->cli_state;
548 struct buffer *req = t->req;
549 struct buffer *rep = t->rep;
550 //fprintf(stderr,"fct %s:%d\n", __FUNCTION__, __LINE__);
551 if (c == CL_STDATA) {
552 /* FIXME: this error handling is partly buggy because we always report
553 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
554 * or HEADER phase. BTW, it's not logical to expire the client while
555 * we're waiting for the server to connect.
556 */
557 /* read or write error */
558 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200559 buffer_shutr_done(req);
560 buffer_shutw_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200561 fd_delete(t->cli_fd);
562 t->cli_state = CL_STCLOSE;
563 if (!(t->flags & SN_ERR_MASK))
564 t->flags |= SN_ERR_CLICL;
565 if (!(t->flags & SN_FINST_MASK)) {
566 if (t->pend_pos)
567 t->flags |= SN_FINST_Q;
568 else if (s == SV_STCONN)
569 t->flags |= SN_FINST_C;
570 else
571 t->flags |= SN_FINST_D;
572 }
573 return 1;
574 }
575 /* last read, or end of server write */
576 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
577 EV_FD_CLR(t->cli_fd, DIR_RD);
578 buffer_shutr(req);
579 t->cli_state = CL_STSHUTR;
580 return 1;
581 }
582 /* last server read and buffer empty */
583 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
584 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200585 buffer_shutw_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200586 shutdown(t->cli_fd, SHUT_WR);
587 /* We must ensure that the read part is still alive when switching
588 * to shutw */
589 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200590 req->rex = tick_add_ifset(now_ms, req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200591 t->cli_state = CL_STSHUTW;
592 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
593 return 1;
594 }
595 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200596 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200597 EV_FD_CLR(t->cli_fd, DIR_RD);
598 buffer_shutr(req);
599 t->cli_state = CL_STSHUTR;
600 if (!(t->flags & SN_ERR_MASK))
601 t->flags |= SN_ERR_CLITO;
602 if (!(t->flags & SN_FINST_MASK)) {
603 if (t->pend_pos)
604 t->flags |= SN_FINST_Q;
605 else if (s == SV_STCONN)
606 t->flags |= SN_FINST_C;
607 else
608 t->flags |= SN_FINST_D;
609 }
610 return 1;
611 }
612 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200613 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200614 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200615 buffer_shutw_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200616 shutdown(t->cli_fd, SHUT_WR);
617 /* We must ensure that the read part is still alive when switching
618 * to shutw */
619 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200620 req->rex = tick_add_ifset(now_ms, req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200621
622 t->cli_state = CL_STSHUTW;
623 if (!(t->flags & SN_ERR_MASK))
624 t->flags |= SN_ERR_CLITO;
625 if (!(t->flags & SN_FINST_MASK)) {
626 if (t->pend_pos)
627 t->flags |= SN_FINST_Q;
628 else if (s == SV_STCONN)
629 t->flags |= SN_FINST_C;
630 else
631 t->flags |= SN_FINST_D;
632 }
633 return 1;
634 }
635
636 if (req->l >= req->rlim - req->data) {
637 /* no room to read more data */
638 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
639 /* stop reading until we get some space */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200640 req->rex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200641 }
642 } else {
643 /* there's still some space in the buffer */
644 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200645 if (!req->rto ||
646 (t->srv_state < SV_STDATA && req->wto))
Willy Tarreau92fb9832007-10-16 17:34:28 +0200647 /* If the client has no timeout, or if the server not ready yet, and we
648 * know for sure that it can expire, then it's cleaner to disable the
649 * timeout on the client side so that too low values cannot make the
650 * sessions abort too early.
651 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200652 req->rex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200653 else
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200654 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200655 }
656 }
657
658 if ((rep->l == 0) ||
659 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
660 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
661 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200662 rep->wex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200663 }
664 } else {
665 /* buffer not empty */
666 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
667 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200668 rep->wex = tick_add_ifset(now_ms, rep->wto);
669 if (rep->wex) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200670 /* FIXME: to prevent the client from expiring read timeouts during writes,
671 * we refresh it. */
672 req->rex = rep->wex;
673 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200674 }
675 }
676 return 0; /* other cases change nothing */
677 }
678 else if (c == CL_STSHUTR) {
679 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200680 buffer_shutw_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200681 fd_delete(t->cli_fd);
682 t->cli_state = CL_STCLOSE;
683 if (!(t->flags & SN_ERR_MASK))
684 t->flags |= SN_ERR_CLICL;
685 if (!(t->flags & SN_FINST_MASK)) {
686 if (t->pend_pos)
687 t->flags |= SN_FINST_Q;
688 else if (s == SV_STCONN)
689 t->flags |= SN_FINST_C;
690 else
691 t->flags |= SN_FINST_D;
692 }
693 return 1;
694 }
695 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200696 buffer_shutw_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200697 fd_delete(t->cli_fd);
698 t->cli_state = CL_STCLOSE;
699 return 1;
700 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200701 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200702 buffer_shutw_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200703 fd_delete(t->cli_fd);
704 t->cli_state = CL_STCLOSE;
705 if (!(t->flags & SN_ERR_MASK))
706 t->flags |= SN_ERR_CLITO;
707 if (!(t->flags & SN_FINST_MASK)) {
708 if (t->pend_pos)
709 t->flags |= SN_FINST_Q;
710 else if (s == SV_STCONN)
711 t->flags |= SN_FINST_C;
712 else
713 t->flags |= SN_FINST_D;
714 }
715 return 1;
716 }
717
718 if (rep->l == 0) {
719 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
720 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200721 rep->wex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200722 }
723 } else {
724 /* buffer not empty */
725 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
726 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200727 rep->wex = tick_add_ifset(now_ms, rep->wto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200728 }
729 }
730 return 0;
731 }
732 else if (c == CL_STSHUTW) {
733 if (req->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200734 buffer_shutr_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200735 fd_delete(t->cli_fd);
736 t->cli_state = CL_STCLOSE;
737 if (!(t->flags & SN_ERR_MASK))
738 t->flags |= SN_ERR_CLICL;
739 if (!(t->flags & SN_FINST_MASK)) {
740 if (t->pend_pos)
741 t->flags |= SN_FINST_Q;
742 else if (s == SV_STCONN)
743 t->flags |= SN_FINST_C;
744 else
745 t->flags |= SN_FINST_D;
746 }
747 return 1;
748 }
749 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200750 buffer_shutr_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200751 fd_delete(t->cli_fd);
752 t->cli_state = CL_STCLOSE;
753 return 1;
754 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200755 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200756 buffer_shutr_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200757 fd_delete(t->cli_fd);
758 t->cli_state = CL_STCLOSE;
759 if (!(t->flags & SN_ERR_MASK))
760 t->flags |= SN_ERR_CLITO;
761 if (!(t->flags & SN_FINST_MASK)) {
762 if (t->pend_pos)
763 t->flags |= SN_FINST_Q;
764 else if (s == SV_STCONN)
765 t->flags |= SN_FINST_C;
766 else
767 t->flags |= SN_FINST_D;
768 }
769 return 1;
770 }
771 else if (req->l >= req->rlim - req->data) {
772 /* no room to read more data */
773
774 /* FIXME-20050705: is it possible for a client to maintain a session
775 * after the timeout by sending more data after it receives a close ?
776 */
777
778 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
779 /* stop reading until we get some space */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200780 req->rex = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200781 }
782 } else {
783 /* there's still some space in the buffer */
784 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200785 req->rex = tick_add_ifset(now_ms, req->rto);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200786 }
787 }
788 return 0;
789 }
790 else { /* CL_STCLOSE: nothing to do */
791 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
792 int len;
793 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be?t->be->id:"",
794 (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
795 write(1, trash, len);
796 }
797 return 0;
798 }
799 return 0;
800}
801
802#if 0
803 /* FIXME! This part has not been completely converted yet, and it may
804 * still be very specific to TCPv4 ! Also, it relies on some parameters
805 * such as conn_retries which are not set upon accept().
806 */
807/*
808 * Manages the server FSM and its socket. It returns 1 if a state has changed
809 * (and a resync may be needed), otherwise 0.
810 */
811static int process_uxst_srv(struct session *t)
812{
813 int s = t->srv_state;
814 int c = t->cli_state;
815 struct buffer *req = t->req;
816 struct buffer *rep = t->rep;
817 int conn_err;
818
819 if (s == SV_STIDLE) {
820 if (c == CL_STCLOSE || c == CL_STSHUTW ||
821 (c == CL_STSHUTR &&
822 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
823 tv_eternity(&req->cex);
824 if (t->pend_pos)
825 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
826 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C);
827 return 1;
828 }
829 else {
830 /* FIXME: reimplement the TARPIT check here */
831
832 /* Right now, we will need to create a connection to the server.
833 * We might already have tried, and got a connection pending, in
834 * which case we will not do anything till it's pending. It's up
835 * to any other session to release it and wake us up again.
836 */
837 if (t->pend_pos) {
838 if (!tv_isle(&req->cex, &now))
839 return 0;
840 else {
841 /* we've been waiting too long here */
842 tv_eternity(&req->cex);
843 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
844 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q);
845 if (t->srv)
846 t->srv->failed_conns++;
847 if (t->fe)
848 t->fe->failed_conns++;
849 return 1;
850 }
851 }
852
853 do {
854 /* first, get a connection */
855 if (srv_redispatch_connect(t))
856 return t->srv_state != SV_STIDLE;
857
858 /* try to (re-)connect to the server, and fail if we expire the
859 * number of retries.
860 */
861 if (srv_retryable_connect(t)) {
862 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
863 return t->srv_state != SV_STIDLE;
864 }
865 } while (1);
866 }
867 }
868 else if (s == SV_STCONN) { /* connection in progress */
869 if (c == CL_STCLOSE || c == CL_STSHUTW ||
870 (c == CL_STSHUTR &&
871 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
872 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
873 tv_eternity(&req->cex);
874 fd_delete(t->srv_fd);
875 if (t->srv)
876 t->srv->cur_sess--;
877
878 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C);
879 return 1;
880 }
881 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
882 //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);
883 return 0; /* nothing changed */
884 }
885 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
886 /* timeout, asynchronous connect error or first write error */
887 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
888
889 fd_delete(t->srv_fd);
890 if (t->srv)
891 t->srv->cur_sess--;
892
893 if (!(req->flags & BF_WRITE_STATUS))
894 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
895 else
896 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
897
898 /* ensure that we have enough retries left */
899 if (srv_count_retry_down(t, conn_err))
900 return 1;
901
902 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
903 /* We're on our last chance, and the REDISP option was specified.
904 * We will ignore cookie and force to balance or use the dispatcher.
905 */
906 /* let's try to offer this slot to anybody */
907 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200908 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200909
910 if (t->srv)
911 t->srv->failed_conns++;
912 t->be->failed_conns++;
913
914 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
915 t->srv = NULL; /* it's left to the dispatcher to choose a server */
916
917 /* first, get a connection */
918 if (srv_redispatch_connect(t))
919 return t->srv_state != SV_STIDLE;
920 }
921
922 do {
923 /* Now we will try to either reconnect to the same server or
924 * connect to another server. If the connection gets queued
925 * because all servers are saturated, then we will go back to
926 * the SV_STIDLE state.
927 */
928 if (srv_retryable_connect(t)) {
929 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
930 return t->srv_state != SV_STCONN;
931 }
932
933 /* we need to redispatch the connection to another server */
934 if (srv_redispatch_connect(t))
935 return t->srv_state != SV_STCONN;
936 } while (1);
937 }
938 else { /* no error or write 0 */
939 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
940
941 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
942 if (req->l == 0) /* nothing to write */ {
943 EV_FD_CLR(t->srv_fd, DIR_WR);
944 tv_eternity(&req->wex);
945 } else /* need the right to write */ {
946 EV_FD_SET(t->srv_fd, DIR_WR);
947 if (tv_add_ifset(&req->wex, &now, &req->wto)) {
948 /* FIXME: to prevent the server from expiring read timeouts during writes,
949 * we refresh it. */
950 rep->rex = req->wex;
951 }
952 else
953 tv_eternity(&req->wex);
954 }
955
956 EV_FD_SET(t->srv_fd, DIR_RD);
957 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
958 tv_eternity(&rep->rex);
959
960 t->srv_state = SV_STDATA;
961 if (t->srv)
962 t->srv->cum_sess++;
963 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
964
965 /* if the user wants to log as soon as possible, without counting
966 bytes from the server, then this is the right moment. */
967 if (t->fe && t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
968 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
969 //uxst_sess_log(t);
970 }
971 tv_eternity(&req->cex);
972 return 1;
973 }
974 }
975 else if (s == SV_STDATA) {
976 /* read or write error */
977 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200978 buffer_shutr_done(rep);
979 buffer_shutw_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200980 fd_delete(t->srv_fd);
981 if (t->srv) {
982 t->srv->cur_sess--;
983 t->srv->failed_resp++;
984 }
985 t->be->failed_resp++;
986 t->srv_state = SV_STCLOSE;
987 if (!(t->flags & SN_ERR_MASK))
988 t->flags |= SN_ERR_SRVCL;
989 if (!(t->flags & SN_FINST_MASK))
990 t->flags |= SN_FINST_D;
991 /* We used to have a free connection slot. Since we'll never use it,
992 * we have to inform the server that it may be used by another session.
993 */
994 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200995 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200996
997 return 1;
998 }
999 /* last read, or end of client write */
1000 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
1001 EV_FD_CLR(t->srv_fd, DIR_RD);
1002 buffer_shutr(rep);
1003 t->srv_state = SV_STSHUTR;
1004 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1005 return 1;
1006 }
1007 /* end of client read and no more data to send */
1008 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
1009 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001010 buffer_shutw_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001011 shutdown(t->srv_fd, SHUT_WR);
1012 /* We must ensure that the read part is still alive when switching
1013 * to shutw */
1014 EV_FD_SET(t->srv_fd, DIR_RD);
1015 tv_add_ifset(&rep->rex, &now, &rep->rto);
1016
1017 t->srv_state = SV_STSHUTW;
1018 return 1;
1019 }
1020 /* read timeout */
1021 else if (tv_isle(&rep->rex, &now)) {
1022 EV_FD_CLR(t->srv_fd, DIR_RD);
1023 buffer_shutr(rep);
1024 t->srv_state = SV_STSHUTR;
1025 if (!(t->flags & SN_ERR_MASK))
1026 t->flags |= SN_ERR_SRVTO;
1027 if (!(t->flags & SN_FINST_MASK))
1028 t->flags |= SN_FINST_D;
1029 return 1;
1030 }
1031 /* write timeout */
1032 else if (tv_isle(&req->wex, &now)) {
1033 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001034 buffer_shutw_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001035 shutdown(t->srv_fd, SHUT_WR);
1036 /* We must ensure that the read part is still alive when switching
1037 * to shutw */
1038 EV_FD_SET(t->srv_fd, DIR_RD);
1039 tv_add_ifset(&rep->rex, &now, &rep->rto);
1040 t->srv_state = SV_STSHUTW;
1041 if (!(t->flags & SN_ERR_MASK))
1042 t->flags |= SN_ERR_SRVTO;
1043 if (!(t->flags & SN_FINST_MASK))
1044 t->flags |= SN_FINST_D;
1045 return 1;
1046 }
1047
1048 /* recompute request time-outs */
1049 if (req->l == 0) {
1050 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
1051 /* stop writing */
1052 tv_eternity(&req->wex);
1053 }
1054 }
1055 else { /* buffer not empty, there are still data to be transferred */
1056 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
1057 /* restart writing */
1058 if (tv_add_ifset(&req->wex, &now, &req->wto)) {
1059 /* FIXME: to prevent the server from expiring read timeouts during writes,
1060 * we refresh it. */
1061 rep->rex = req->wex;
1062 }
1063 else
1064 tv_eternity(&req->wex);
1065 }
1066 }
1067
1068 /* recompute response time-outs */
1069 if (rep->l == BUFSIZE) { /* no room to read more data */
1070 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
1071 tv_eternity(&rep->rex);
1072 }
1073 }
1074 else {
1075 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
1076 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
1077 tv_eternity(&rep->rex);
1078 }
1079 }
1080
1081 return 0; /* other cases change nothing */
1082 }
1083 else if (s == SV_STSHUTR) {
1084 if (req->flags & BF_WRITE_ERROR) {
1085 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001086 buffer_shutw_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001087 fd_delete(t->srv_fd);
1088 if (t->srv) {
1089 t->srv->cur_sess--;
1090 t->srv->failed_resp++;
1091 }
1092 t->be->failed_resp++;
1093 //close(t->srv_fd);
1094 t->srv_state = SV_STCLOSE;
1095 if (!(t->flags & SN_ERR_MASK))
1096 t->flags |= SN_ERR_SRVCL;
1097 if (!(t->flags & SN_FINST_MASK))
1098 t->flags |= SN_FINST_D;
1099 /* We used to have a free connection slot. Since we'll never use it,
1100 * we have to inform the server that it may be used by another session.
1101 */
1102 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001103 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001104
1105 return 1;
1106 }
1107 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
1108 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001109 buffer_shutw_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001110 fd_delete(t->srv_fd);
1111 if (t->srv)
1112 t->srv->cur_sess--;
1113 //close(t->srv_fd);
1114 t->srv_state = SV_STCLOSE;
1115 /* We used to have a free connection slot. Since we'll never use it,
1116 * we have to inform the server that it may be used by another session.
1117 */
1118 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001119 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001120
1121 return 1;
1122 }
1123 else if (tv_isle(&req->wex, &now)) {
1124 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001125 buffer_shutw_done(req);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001126 fd_delete(t->srv_fd);
1127 if (t->srv)
1128 t->srv->cur_sess--;
1129 //close(t->srv_fd);
1130 t->srv_state = SV_STCLOSE;
1131 if (!(t->flags & SN_ERR_MASK))
1132 t->flags |= SN_ERR_SRVTO;
1133 if (!(t->flags & SN_FINST_MASK))
1134 t->flags |= SN_FINST_D;
1135 /* We used to have a free connection slot. Since we'll never use it,
1136 * we have to inform the server that it may be used by another session.
1137 */
1138 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001139 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001140
1141 return 1;
1142 }
1143 else if (req->l == 0) {
1144 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
1145 /* stop writing */
1146 tv_eternity(&req->wex);
1147 }
1148 }
1149 else { /* buffer not empty */
1150 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
1151 /* restart writing */
1152 if (!tv_add_ifset(&req->wex, &now, &req->wto))
1153 tv_eternity(&req->wex);
1154 }
1155 }
1156 return 0;
1157 }
1158 else if (s == SV_STSHUTW) {
1159 if (rep->flags & BF_READ_ERROR) {
1160 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001161 buffer_shutr_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001162 fd_delete(t->srv_fd);
1163 if (t->srv) {
1164 t->srv->cur_sess--;
1165 t->srv->failed_resp++;
1166 }
1167 t->be->failed_resp++;
1168 //close(t->srv_fd);
1169 t->srv_state = SV_STCLOSE;
1170 if (!(t->flags & SN_ERR_MASK))
1171 t->flags |= SN_ERR_SRVCL;
1172 if (!(t->flags & SN_FINST_MASK))
1173 t->flags |= SN_FINST_D;
1174 /* We used to have a free connection slot. Since we'll never use it,
1175 * we have to inform the server that it may be used by another session.
1176 */
1177 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001178 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001179
1180 return 1;
1181 }
1182 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
1183 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001184 buffer_shutr_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001185 fd_delete(t->srv_fd);
1186 if (t->srv)
1187 t->srv->cur_sess--;
1188 //close(t->srv_fd);
1189 t->srv_state = SV_STCLOSE;
1190 /* We used to have a free connection slot. Since we'll never use it,
1191 * we have to inform the server that it may be used by another session.
1192 */
1193 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001194 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001195
1196 return 1;
1197 }
1198 else if (tv_isle(&rep->rex, &now)) {
1199 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001200 buffer_shutr_done(rep);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001201 fd_delete(t->srv_fd);
1202 if (t->srv)
1203 t->srv->cur_sess--;
1204 //close(t->srv_fd);
1205 t->srv_state = SV_STCLOSE;
1206 if (!(t->flags & SN_ERR_MASK))
1207 t->flags |= SN_ERR_SRVTO;
1208 if (!(t->flags & SN_FINST_MASK))
1209 t->flags |= SN_FINST_D;
1210 /* We used to have a free connection slot. Since we'll never use it,
1211 * we have to inform the server that it may be used by another session.
1212 */
1213 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001214 process_srv_queue(t->srv);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001215
1216 return 1;
1217 }
1218 else if (rep->l == BUFSIZE) { /* no room to read more data */
1219 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
1220 tv_eternity(&rep->rex);
1221 }
1222 }
1223 else {
1224 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
1225 if (!tv_add_ifset(&rep->rex, &now, &rep->rto))
1226 tv_eternity(&rep->rex);
1227 }
1228 }
1229 return 0;
1230 }
1231 else { /* SV_STCLOSE : nothing to do */
1232 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1233 int len;
1234 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1235 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
1236 write(1, trash, len);
1237 }
1238 return 0;
1239 }
1240 return 0;
1241}
1242
1243/* Processes the client and server jobs of a session task, then
1244 * puts it back to the wait queue in a clean state, or
1245 * cleans up its resources if it must be deleted. Returns
1246 * the time the task accepts to wait, or TIME_ETERNITY for
1247 * infinity.
1248 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001249void process_uxst_session(struct task *t, int *next)
Willy Tarreau92fb9832007-10-16 17:34:28 +02001250{
1251 struct session *s = t->context;
1252 int fsm_resync = 0;
1253
1254 do {
1255 fsm_resync = 0;
1256 fsm_resync |= process_uxst_cli(s);
1257 if (s->srv_state == SV_STIDLE) {
1258 if (s->cli_state == CL_STCLOSE || s->cli_state == CL_STSHUTW) {
1259 s->srv_state = SV_STCLOSE;
1260 fsm_resync |= 1;
1261 continue;
1262 }
1263 if (s->cli_state == CL_STSHUTR ||
1264 (s->req->l >= s->req->rlim - s->req->data)) {
1265 if (s->req->l == 0) {
1266 s->srv_state = SV_STCLOSE;
1267 fsm_resync |= 1;
1268 continue;
1269 }
1270 /* OK we have some remaining data to process */
1271 /* Just as an exercice, we copy the req into the resp,
1272 * and flush the req.
1273 */
1274 memcpy(s->rep->data, s->req->data, sizeof(s->rep->data));
1275 s->rep->l = s->req->l;
1276 s->rep->rlim = s->rep->data + BUFSIZE;
1277 s->rep->w = s->rep->data;
1278 s->rep->lr = s->rep->r = s->rep->data + s->rep->l;
1279
1280 s->req->l = 0;
1281 s->srv_state = SV_STCLOSE;
1282
1283 fsm_resync |= 1;
1284 continue;
1285 }
1286 }
1287 } while (fsm_resync);
1288
1289 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001290
1291 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1292 session_process_counters(s);
1293
Willy Tarreau92fb9832007-10-16 17:34:28 +02001294 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1295 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1296
1297 t->expire = s->req->rex;
1298 tv_min(&t->expire, &s->req->rex, &s->req->wex);
1299 tv_bound(&t->expire, &s->req->cex);
1300 tv_bound(&t->expire, &s->rep->rex);
1301 tv_bound(&t->expire, &s->rep->wex);
1302
1303 /* restore t to its place in the task list */
1304 task_queue(t);
1305
1306 *next = t->expire;
1307 return; /* nothing more to do */
1308 }
1309
1310 if (s->fe)
1311 s->fe->feconn--;
1312 if (s->be && (s->flags & SN_BE_ASSIGNED))
1313 s->be->beconn--;
1314 actconn--;
1315
1316 if (unlikely((global.mode & MODE_DEBUG) &&
1317 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1318 int len;
1319 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
1320 s->uniq_id, s->be->id,
1321 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
1322 write(1, trash, len);
1323 }
1324
1325 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001326 session_process_counters(s);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001327
1328 /* let's do a final log if we need it */
1329 if (s->logs.logwait &&
1330 !(s->flags & SN_MONITOR) &&
1331 (s->req->total || !(s->fe && s->fe->options & PR_O_NULLNOLOG))) {
1332 //uxst_sess_log(s);
1333 }
1334
1335 /* the task MUST not be in the run queue anymore */
1336 task_delete(t);
1337 session_free(s);
1338 task_free(t);
1339 tv_eternity(next);
1340}
1341#endif /* not converted */
1342
1343
1344/* Processes data exchanges on the statistics socket. The client processing
1345 * is called and the task is put back in the wait queue or it is cleared.
1346 * In order to ease the transition, we simply simulate the server status
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001347 * for now. It only knows states SV_STIDLE, SV_STCONN, SV_STDATA, and
1348 * SV_STCLOSE. Returns in <next> the task's expiration date.
Willy Tarreau92fb9832007-10-16 17:34:28 +02001349 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001350void process_uxst_stats(struct task *t, int *next)
Willy Tarreau92fb9832007-10-16 17:34:28 +02001351{
1352 struct session *s = t->context;
1353 struct listener *listener;
1354 int fsm_resync = 0;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001355 int last_rep_l;
Willy Tarreau92fb9832007-10-16 17:34:28 +02001356
1357 do {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001358 char *args[MAX_UXST_ARGS + 1];
1359 char *line, *p;
1360 int arg;
1361
Willy Tarreau3e76e722007-10-17 18:57:38 +02001362 fsm_resync = process_uxst_cli(s);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001363
1364 if (s->cli_state == CL_STCLOSE || s->cli_state == CL_STSHUTW) {
1365 s->srv_state = SV_STCLOSE;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001366 break;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001367 }
1368
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001369 switch (s->srv_state) {
1370 case SV_STIDLE:
1371 /* stats output not initialized yet */
1372 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
1373 s->data_source = DATA_SRC_STATS;
1374 s->srv_state = SV_STCONN;
1375 fsm_resync |= 1;
1376 break;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001377
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001378 case SV_STCONN: /* will change to SV_STANALYZE */
1379 /* stats initialized, but waiting for the command */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001380 line = s->req->data;
1381 p = memchr(line, '\n', s->req->l);
1382
1383 if (!p)
1384 continue;
1385
1386 *p = '\0';
1387
1388 while (isspace((unsigned char)*line))
1389 line++;
1390
1391 arg = 0;
1392 args[arg] = line;
1393
1394 while (*line && arg < MAX_UXST_ARGS) {
1395 if (isspace((unsigned char)*line)) {
1396 *line++ = '\0';
1397
1398 while (isspace((unsigned char)*line))
1399 line++;
1400
1401 args[++arg] = line;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001402 continue;
1403 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001404
1405 line++;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001406 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001407
1408 while (++arg <= MAX_UXST_ARGS)
1409 args[arg] = line;
1410
1411 if (!strcmp(args[0], "show")) {
1412 if (!strcmp(args[1], "stat")) {
1413 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001414 s->data_ctx.stats.flags |= STAT_BOUND;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001415 s->data_ctx.stats.iid = atoi(args[2]);
1416 s->data_ctx.stats.type = atoi(args[3]);
1417 s->data_ctx.stats.sid = atoi(args[4]);
1418 }
1419
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001420 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
1421 s->data_ctx.stats.flags |= STAT_FMT_CSV;
1422 s->srv_state = SV_STDATA;
1423 fsm_resync |= 1;
Willy Tarreau92fb9832007-10-16 17:34:28 +02001424 continue;
1425 }
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001426
1427 if (!strcmp(args[1], "info")) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001428 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
1429 s->data_ctx.stats.flags |= STAT_FMT_CSV;
1430 s->srv_state = SV_STDATA;
1431 fsm_resync |= 1;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001432 continue;
1433 }
Willy Tarreau92fb9832007-10-16 17:34:28 +02001434 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001435
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001436 s->srv_state = SV_STCLOSE;
1437 fsm_resync |= 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001438 continue;
1439
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001440 case SV_STDATA:
1441 /* OK we have to process the request. Since it is possible
1442 * that we get there with the client output paused, we
1443 * will simply check that we have really sent some data
1444 * and wake the client up if needed.
1445 */
1446 last_rep_l = s->rep->l;
1447 if (stats_dump_raw(s, NULL) != 0) {
1448 s->srv_state = SV_STCLOSE;
1449 fsm_resync |= 1;
1450 }
1451 if (s->rep->l != last_rep_l)
1452 fsm_resync |= 1;
1453 break;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001454 }
Willy Tarreau92fb9832007-10-16 17:34:28 +02001455 } while (fsm_resync);
1456
1457 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
1458 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1459 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
1460
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001461 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1462 tick_first(s->rep->rex, s->rep->wex));
1463 t->expire = tick_first(t->expire, s->req->cex);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001464
1465 /* restore t to its place in the task list */
1466 task_queue(t);
1467
1468 *next = t->expire;
1469 return; /* nothing more to do */
1470 }
1471
1472 actconn--;
1473 listener = fdtab[s->cli_fd].listener;
1474 if (listener) {
1475 listener->nbconn--;
1476 if (listener->state == LI_FULL &&
1477 listener->nbconn < listener->maxconn) {
1478 /* we should reactivate the listener */
1479 EV_FD_SET(listener->fd, DIR_RD);
1480 listener->state = LI_READY;
1481 }
1482 }
1483
1484 /* the task MUST not be in the run queue anymore */
1485 task_delete(t);
1486 session_free(s);
1487 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001488 *next = TICK_ETERNITY;
Willy Tarreau92fb9832007-10-16 17:34:28 +02001489}
1490
Willy Tarreau92fb9832007-10-16 17:34:28 +02001491__attribute__((constructor))
1492static void __uxst_protocol_init(void)
1493{
1494 protocol_register(&proto_unix);
Willy Tarreau92fb9832007-10-16 17:34:28 +02001495}
1496
1497
1498/*
1499 * Local variables:
1500 * c-indent-level: 8
1501 * c-basic-offset: 8
1502 * End:
1503 */