| 1 | /*- |
|---|
| 2 | * Copyright (c) 2006 Verdens Gang AS |
|---|
| 3 | * Copyright (c) 2006 Linpro AS |
|---|
| 4 | * Copyright (c) 2007 OmniTI Computer Consulting, Inc. |
|---|
| 5 | * Copyright (c) 2007 Theo Schlossnagle |
|---|
| 6 | * All rights reserved. |
|---|
| 7 | * |
|---|
| 8 | * Redistribution and use in source and binary forms, with or without |
|---|
| 9 | * modification, are permitted provided that the following conditions |
|---|
| 10 | * are met: |
|---|
| 11 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer. |
|---|
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 14 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 15 | * documentation and/or other materials provided with the distribution. |
|---|
| 16 | * |
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
|---|
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 27 | * SUCH DAMAGE. |
|---|
| 28 | * |
|---|
| 29 | * $Id$ |
|---|
| 30 | * |
|---|
| 31 | * XXX: We need to pass sessions back into the event engine when they are |
|---|
| 32 | * reused. Not sure what the most efficient way is for that. For now |
|---|
| 33 | * write the session pointer to a pipe which the event engine monitors. |
|---|
| 34 | */ |
|---|
| 35 | |
|---|
| 36 | #include "config.h" |
|---|
| 37 | #if defined(HAVE_PORT_CREATE) |
|---|
| 38 | |
|---|
| 39 | #include <stdio.h> |
|---|
| 40 | #include <errno.h> |
|---|
| 41 | #include <string.h> |
|---|
| 42 | #include <stdlib.h> |
|---|
| 43 | #include <unistd.h> |
|---|
| 44 | |
|---|
| 45 | #include <port.h> |
|---|
| 46 | |
|---|
| 47 | #ifndef HAVE_CLOCK_GETTIME |
|---|
| 48 | #include "compat/clock_gettime.h" |
|---|
| 49 | #endif |
|---|
| 50 | |
|---|
| 51 | #include "shmlog.h" |
|---|
| 52 | #include "cache.h" |
|---|
| 53 | #include "cache_acceptor.h" |
|---|
| 54 | |
|---|
| 55 | #define MAX_EVENTS 256 |
|---|
| 56 | static pthread_t vca_ports_thread; |
|---|
| 57 | int solaris_dport = -1; |
|---|
| 58 | |
|---|
| 59 | static VTAILQ_HEAD(,sess) sesshead = VTAILQ_HEAD_INITIALIZER(sesshead); |
|---|
| 60 | |
|---|
| 61 | static void |
|---|
| 62 | vca_add(int fd, void *data) |
|---|
| 63 | { |
|---|
| 64 | AZ(port_associate(solaris_dport, PORT_SOURCE_FD, fd, POLLIN | POLLERR | POLLPRI, data)); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | static void |
|---|
| 68 | vca_del(int fd) |
|---|
| 69 | { |
|---|
| 70 | port_dissociate(solaris_dport, PORT_SOURCE_FD, fd); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | static void |
|---|
| 74 | vca_port_ev(port_event_t *ev) { |
|---|
| 75 | struct sess *sp; |
|---|
| 76 | if(ev->portev_source == PORT_SOURCE_USER) { |
|---|
| 77 | sp = ev->portev_user; |
|---|
| 78 | CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
|---|
| 79 | assert(sp->fd >= 0); |
|---|
| 80 | AZ(sp->obj); |
|---|
| 81 | VTAILQ_INSERT_TAIL(&sesshead, sp, list); |
|---|
| 82 | vca_add(sp->fd, sp); |
|---|
| 83 | } else { |
|---|
| 84 | int i; |
|---|
| 85 | CAST_OBJ_NOTNULL(sp, ev->portev_user, SESS_MAGIC); |
|---|
| 86 | if(ev->portev_events & POLLERR) { |
|---|
| 87 | VTAILQ_REMOVE(&sesshead, sp, list); |
|---|
| 88 | vca_close_session(sp, "EOF"); |
|---|
| 89 | SES_Delete(sp); |
|---|
| 90 | return; |
|---|
| 91 | } |
|---|
| 92 | i = HTC_Rx(sp->htc); |
|---|
| 93 | if (i == 0) |
|---|
| 94 | return; |
|---|
| 95 | if (i > 0) { |
|---|
| 96 | VTAILQ_REMOVE(&sesshead, sp, list); |
|---|
| 97 | if (sp->fd != -1) |
|---|
| 98 | vca_del(sp->fd); |
|---|
| 99 | vca_handover(sp, i); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | static void * |
|---|
| 106 | vca_main(void *arg) |
|---|
| 107 | { |
|---|
| 108 | struct sess *sp; |
|---|
| 109 | |
|---|
| 110 | (void)arg; |
|---|
| 111 | |
|---|
| 112 | solaris_dport = port_create(); |
|---|
| 113 | assert(solaris_dport >= 0); |
|---|
| 114 | |
|---|
| 115 | while (1) { |
|---|
| 116 | port_event_t ev[MAX_EVENTS]; |
|---|
| 117 | int nevents, ei; |
|---|
| 118 | double deadline; |
|---|
| 119 | struct timespec ts; |
|---|
| 120 | ts.tv_sec = 0L; |
|---|
| 121 | ts.tv_nsec = 50L /*ms*/ * 1000L /*us*/ * 1000L /*ns*/; |
|---|
| 122 | nevents = 1; |
|---|
| 123 | if (port_getn(solaris_dport, ev, MAX_EVENTS, &nevents, &ts) == 0) { |
|---|
| 124 | for (ei=0; ei<nevents; ei++) { |
|---|
| 125 | vca_port_ev(ev + ei); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | /* check for timeouts */ |
|---|
| 129 | deadline = TIM_real() - params->sess_timeout; |
|---|
| 130 | for (;;) { |
|---|
| 131 | sp = VTAILQ_FIRST(&sesshead); |
|---|
| 132 | if (sp == NULL) |
|---|
| 133 | break; |
|---|
| 134 | if (sp->t_open > deadline) |
|---|
| 135 | break; |
|---|
| 136 | VTAILQ_REMOVE(&sesshead, sp, list); |
|---|
| 137 | if(sp->fd != -1) |
|---|
| 138 | vca_del(sp->fd); |
|---|
| 139 | vca_close_session(sp, "timeout"); |
|---|
| 140 | SES_Delete(sp); |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | static void |
|---|
| 146 | vca_ports_pass(struct sess *sp) |
|---|
| 147 | { |
|---|
| 148 | int r; |
|---|
| 149 | while((r = port_send(solaris_dport, 0, sp)) == -1 && |
|---|
| 150 | errno == EAGAIN); |
|---|
| 151 | AZ(r); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /*--------------------------------------------------------------------*/ |
|---|
| 155 | |
|---|
| 156 | static void |
|---|
| 157 | vca_ports_init(void) |
|---|
| 158 | { |
|---|
| 159 | |
|---|
| 160 | AZ(pthread_create(&vca_ports_thread, NULL, vca_main, NULL)); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | struct acceptor acceptor_ports = { |
|---|
| 164 | .name = "ports", |
|---|
| 165 | .init = vca_ports_init, |
|---|
| 166 | .pass = vca_ports_pass |
|---|
| 167 | }; |
|---|
| 168 | |
|---|
| 169 | #endif /* defined(HAVE_PORT_CREATE) */ |
|---|