Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions managing stream_interface structures |
| 3 | * |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 5 | * |
| 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 <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <common/compat.h> |
| 23 | #include <common/config.h> |
| 24 | #include <common/debug.h> |
| 25 | #include <common/standard.h> |
| 26 | #include <common/ticks.h> |
| 27 | #include <common/time.h> |
| 28 | |
| 29 | #include <proto/buffers.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 30 | #include <proto/fd.h> |
Willy Tarreau | 269358d | 2009-09-20 20:14:49 +0200 | [diff] [blame] | 31 | #include <proto/stream_interface.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 32 | #include <proto/stream_sock.h> |
| 33 | #include <proto/task.h> |
| 34 | |
| 35 | /* |
| 36 | * This function only has to be called once after a wakeup event in case of |
| 37 | * suspected timeout. It controls the stream interface timeouts and sets |
| 38 | * si->flags accordingly. It does NOT close anything, as this timeout may |
| 39 | * be used for any purpose. It returns 1 if the timeout fired, otherwise |
| 40 | * zero. |
| 41 | */ |
| 42 | int stream_int_check_timeouts(struct stream_interface *si) |
| 43 | { |
| 44 | if (tick_is_expired(si->exp, now_ms)) { |
| 45 | si->flags |= SI_FL_EXP; |
| 46 | return 1; |
| 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 51 | /* to be called only when in SI_ST_DIS with SI_FL_ERR */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 52 | void stream_int_report_error(struct stream_interface *si) |
| 53 | { |
| 54 | if (!si->err_type) |
| 55 | si->err_type = SI_ET_DATA_ERR; |
| 56 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 57 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 58 | si->ib->flags |= BF_READ_ERROR; |
| 59 | } |
| 60 | |
| 61 | /* |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 62 | * Returns a message to the client ; the connection is shut down for read, |
| 63 | * and the request is cleared so that no server connection can be initiated. |
| 64 | * The buffer is marked for read shutdown on the other side to protect the |
| 65 | * message, and the buffer write is enabled. The message is contained in a |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 66 | * "chunk". If it is null, then an empty message is used. The reply buffer does |
| 67 | * not need to be empty before this, and its contents will not be overwritten. |
| 68 | * The primary goal of this function is to return error messages to a client. |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 69 | */ |
| 70 | void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg) |
| 71 | { |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 72 | buffer_auto_read(si->ib); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 73 | buffer_abort(si->ib); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 74 | buffer_auto_close(si->ib); |
| 75 | buffer_erase(si->ib); |
Willy Tarreau | 798e128 | 2010-12-12 13:06:00 +0100 | [diff] [blame] | 76 | |
| 77 | buffer_cut_tail(si->ob); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 78 | if (likely(msg && msg->len)) |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 79 | buffer_write(si->ob, msg->str, msg->len); |
| 80 | |
| 81 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 82 | buffer_auto_read(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 83 | buffer_auto_close(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 84 | buffer_shutr_now(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 87 | /* default update function for scheduled tasks, not used for embedded tasks */ |
| 88 | void stream_int_update(struct stream_interface *si) |
| 89 | { |
| 90 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 91 | __FUNCTION__, |
| 92 | si, si->state, si->ib->flags, si->ob->flags); |
| 93 | |
| 94 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 95 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 96 | } |
| 97 | |
| 98 | /* default update function for embedded tasks, to be used at the end of the i/o handler */ |
| 99 | void stream_int_update_embedded(struct stream_interface *si) |
| 100 | { |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 101 | int old_flags = si->flags; |
| 102 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 103 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 104 | __FUNCTION__, |
| 105 | si, si->state, si->ib->flags, si->ob->flags); |
| 106 | |
| 107 | if (si->state != SI_ST_EST) |
| 108 | return; |
| 109 | |
| 110 | if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW)) |
| 111 | si->shutw(si); |
| 112 | |
| 113 | if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0) |
| 114 | si->flags |= SI_FL_WAIT_DATA; |
| 115 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 116 | /* we're almost sure that we need some space if the buffer is not |
| 117 | * empty, even if it's not full, because the applets can't fill it. |
| 118 | */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 119 | if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 120 | si->flags |= SI_FL_WAIT_ROOM; |
| 121 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 122 | if (si->ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 123 | if (tick_isset(si->ob->wex)) |
| 124 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
| 125 | } |
| 126 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 127 | if (si->ib->flags & BF_READ_ACTIVITY || |
| 128 | (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) { |
| 129 | if (tick_isset(si->ib->rex)) |
| 130 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 131 | } |
| 132 | |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 133 | /* save flags to detect changes */ |
| 134 | old_flags = si->flags; |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 135 | if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL && |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 136 | (si->ob->prod->flags & SI_FL_WAIT_ROOM))) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 137 | si->ob->prod->chk_rcv(si->ob->prod); |
| 138 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 139 | if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) && |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 140 | (si->ib->cons->flags & SI_FL_WAIT_DATA)) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 141 | si->ib->cons->chk_snd(si->ib->cons); |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 142 | /* check if the consumer has freed some space */ |
| 143 | if (!(si->ib->flags & BF_FULL)) |
| 144 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 145 | } |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 146 | |
| 147 | /* Note that we're trying to wake up in two conditions here : |
| 148 | * - special event, which needs the holder task attention |
| 149 | * - status indicating that the applet can go on working. This |
| 150 | * is rather hard because we might be blocking on output and |
| 151 | * don't want to wake up on input and vice-versa. The idea is |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 152 | * to only rely on the changes the chk_* might have performed. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 153 | */ |
| 154 | if (/* check stream interface changes */ |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 155 | ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) || |
| 156 | |
| 157 | /* changes on the production side */ |
| 158 | (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) || |
| 159 | si->state != SI_ST_EST || |
| 160 | (si->flags & SI_FL_ERR) || |
| 161 | ((si->ib->flags & BF_READ_PARTIAL) && |
| 162 | (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) || |
| 163 | |
| 164 | /* changes on the consumption side */ |
| 165 | (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) || |
| 166 | ((si->ob->flags & BF_WRITE_ACTIVITY) && |
| 167 | ((si->ob->flags & BF_SHUTW) || |
| 168 | si->ob->prod->state != SI_ST_EST || |
| 169 | ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 170 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 171 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 172 | } |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 173 | if (si->ib->flags & BF_READ_ACTIVITY) |
| 174 | si->ib->flags &= ~BF_READ_DONTWAIT; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* default shutr function for scheduled tasks */ |
| 178 | void stream_int_shutr(struct stream_interface *si) |
| 179 | { |
| 180 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 181 | __FUNCTION__, |
| 182 | si, si->state, si->ib->flags, si->ob->flags); |
| 183 | |
| 184 | si->ib->flags &= ~BF_SHUTR_NOW; |
| 185 | if (si->ib->flags & BF_SHUTR) |
| 186 | return; |
| 187 | si->ib->flags |= BF_SHUTR; |
| 188 | si->ib->rex = TICK_ETERNITY; |
| 189 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 190 | |
| 191 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
| 192 | return; |
| 193 | |
| 194 | if (si->ob->flags & BF_SHUTW) { |
| 195 | si->state = SI_ST_DIS; |
| 196 | si->exp = TICK_ETERNITY; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 197 | |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 198 | if (si->release) |
| 199 | si->release(si); |
| 200 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 201 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 202 | /* note that if the task exist, it must unregister itself once it runs */ |
| 203 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 204 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 205 | } |
| 206 | |
| 207 | /* default shutw function for scheduled tasks */ |
| 208 | void stream_int_shutw(struct stream_interface *si) |
| 209 | { |
| 210 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 211 | __FUNCTION__, |
| 212 | si, si->state, si->ib->flags, si->ob->flags); |
| 213 | |
| 214 | si->ob->flags &= ~BF_SHUTW_NOW; |
| 215 | if (si->ob->flags & BF_SHUTW) |
| 216 | return; |
| 217 | si->ob->flags |= BF_SHUTW; |
| 218 | si->ob->wex = TICK_ETERNITY; |
| 219 | si->flags &= ~SI_FL_WAIT_DATA; |
| 220 | |
| 221 | switch (si->state) { |
| 222 | case SI_ST_EST: |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 223 | if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ))) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 224 | break; |
| 225 | |
| 226 | /* fall through */ |
| 227 | case SI_ST_CON: |
| 228 | case SI_ST_CER: |
Willy Tarreau | 32d3ee9 | 2010-12-29 14:03:02 +0100 | [diff] [blame] | 229 | case SI_ST_QUE: |
| 230 | case SI_ST_TAR: |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 231 | si->state = SI_ST_DIS; |
| 232 | /* fall through */ |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 233 | |
| 234 | if (si->release) |
| 235 | si->release(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 236 | default: |
| 237 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 238 | si->ib->flags |= BF_SHUTR; |
| 239 | si->ib->rex = TICK_ETERNITY; |
| 240 | si->exp = TICK_ETERNITY; |
| 241 | } |
| 242 | |
| 243 | /* note that if the task exist, it must unregister itself once it runs */ |
| 244 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 245 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 246 | } |
| 247 | |
| 248 | /* default chk_rcv function for scheduled tasks */ |
| 249 | void stream_int_chk_rcv(struct stream_interface *si) |
| 250 | { |
| 251 | struct buffer *ib = si->ib; |
| 252 | |
| 253 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 254 | __FUNCTION__, |
| 255 | si, si->state, si->ib->flags, si->ob->flags); |
| 256 | |
| 257 | if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR))) |
| 258 | return; |
| 259 | |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 260 | if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 261 | /* stop reading */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 262 | if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 263 | si->flags |= SI_FL_WAIT_ROOM; |
| 264 | } |
| 265 | else { |
| 266 | /* (re)start reading */ |
| 267 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 268 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 269 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* default chk_snd function for scheduled tasks */ |
| 274 | void stream_int_chk_snd(struct stream_interface *si) |
| 275 | { |
| 276 | struct buffer *ob = si->ob; |
| 277 | |
| 278 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 279 | __FUNCTION__, |
| 280 | si, si->state, si->ib->flags, si->ob->flags); |
| 281 | |
| 282 | if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW))) |
| 283 | return; |
| 284 | |
| 285 | if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */ |
| 286 | (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */ |
| 287 | return; |
| 288 | |
| 289 | /* Otherwise there are remaining data to be sent in the buffer, |
| 290 | * so we tell the handler. |
| 291 | */ |
| 292 | si->flags &= ~SI_FL_WAIT_DATA; |
| 293 | if (!tick_isset(ob->wex)) |
| 294 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
| 295 | |
| 296 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 297 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 298 | } |
| 299 | |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 300 | /* Register an applet to handle a stream_interface as part of the stream |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 301 | * interface's owner task, which is returned. The SI will wake it up everytime |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 302 | * it is solicited. The task's processing function must call the applet's |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 303 | * function before returning. It must be deleted by the task handler using |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 304 | * stream_int_unregister_handler(), possibly from within the function itself. |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 305 | * It also pre-initializes applet.state to zero. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 306 | */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 307 | struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 308 | { |
| 309 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner); |
| 310 | |
| 311 | si->update = stream_int_update_embedded; |
| 312 | si->shutr = stream_int_shutr; |
| 313 | si->shutw = stream_int_shutw; |
| 314 | si->chk_rcv = stream_int_chk_rcv; |
| 315 | si->chk_snd = stream_int_chk_snd; |
| 316 | si->connect = NULL; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 317 | si->target.type = TARG_TYPE_APPLET; |
| 318 | si->target.ptr.a = app; |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 319 | si->applet.state = 0; |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 320 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 321 | si->flags |= SI_FL_WAIT_DATA; |
| 322 | return si->owner; |
| 323 | } |
| 324 | |
| 325 | /* Register a function to handle a stream_interface as a standalone task. The |
| 326 | * new task itself is returned and is assigned as si->owner. The stream_interface |
| 327 | * pointer will be pointed to by the task's context. The handler can be detached |
| 328 | * by using stream_int_unregister_handler(). |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 329 | * FIXME: the code should be updated to ensure that we don't change si->owner |
| 330 | * anymore as this is not needed. However, process_session still relies on it. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 331 | */ |
| 332 | struct task *stream_int_register_handler_task(struct stream_interface *si, |
| 333 | struct task *(*fct)(struct task *)) |
| 334 | { |
| 335 | struct task *t; |
| 336 | |
| 337 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner); |
| 338 | |
| 339 | si->update = stream_int_update; |
| 340 | si->shutr = stream_int_shutr; |
| 341 | si->shutw = stream_int_shutw; |
| 342 | si->chk_rcv = stream_int_chk_rcv; |
| 343 | si->chk_snd = stream_int_chk_snd; |
| 344 | si->connect = NULL; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 345 | si->target.type = TARG_TYPE_NONE; |
| 346 | si->target.ptr.v = NULL; |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 347 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 348 | si->flags |= SI_FL_WAIT_DATA; |
| 349 | |
| 350 | t = task_new(); |
| 351 | si->owner = t; |
| 352 | if (!t) |
| 353 | return t; |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 354 | |
| 355 | si->target.type = TARG_TYPE_TASK; |
| 356 | si->target.ptr.t = t; |
| 357 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 358 | t->process = fct; |
| 359 | t->context = si; |
| 360 | task_wakeup(si->owner, TASK_WOKEN_INIT); |
| 361 | |
| 362 | return t; |
| 363 | } |
| 364 | |
| 365 | /* Unregister a stream interface handler. This must be called by the handler task |
| 366 | * itself when it detects that it is in the SI_ST_DIS state. This function can |
| 367 | * both detach standalone handlers and embedded handlers. |
| 368 | */ |
| 369 | void stream_int_unregister_handler(struct stream_interface *si) |
| 370 | { |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 371 | if (si->target.type == TARG_TYPE_TASK) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 372 | /* external handler : kill the task */ |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 373 | task_delete(si->target.ptr.t); |
| 374 | task_free(si->target.ptr.t); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 375 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 376 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 377 | si->owner = NULL; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 378 | si->target.type = TARG_TYPE_NONE; |
| 379 | si->target.ptr.v = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 382 | /* |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 383 | * Local variables: |
| 384 | * c-indent-level: 8 |
| 385 | * c-basic-offset: 8 |
| 386 | * End: |
| 387 | */ |