[40313e4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup ns
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[d9c8c81] | 33 | #include <adt/hash_table.h>
|
---|
[40313e4] | 34 | #include <assert.h>
|
---|
[7b616e2] | 35 | #include <async.h>
|
---|
[40313e4] | 36 | #include <errno.h>
|
---|
[c7bbf029] | 37 | #include <stdio.h>
|
---|
[38d150e] | 38 | #include <stdlib.h>
|
---|
[40313e4] | 39 | #include "service.h"
|
---|
| 40 | #include "ns.h"
|
---|
| 41 |
|
---|
| 42 | /** Service hash table item. */
|
---|
| 43 | typedef struct {
|
---|
[062d900] | 44 | ht_link_t link;
|
---|
[a35b458] | 45 |
|
---|
[9cfbf2f] | 46 | /** Service ID */
|
---|
| 47 | service_t service;
|
---|
[a35b458] | 48 |
|
---|
[7b616e2] | 49 | /** Session to the service */
|
---|
| 50 | async_sess_t *sess;
|
---|
[40313e4] | 51 | } hashed_service_t;
|
---|
| 52 |
|
---|
[062d900] | 53 | static size_t service_key_hash(void *key)
|
---|
[40313e4] | 54 | {
|
---|
[9cfbf2f] | 55 | return *(service_t *) key;
|
---|
[40313e4] | 56 | }
|
---|
| 57 |
|
---|
[062d900] | 58 | static size_t service_hash(const ht_link_t *item)
|
---|
[40313e4] | 59 | {
|
---|
[9cfbf2f] | 60 | hashed_service_t *service =
|
---|
| 61 | hash_table_get_inst(item, hashed_service_t, link);
|
---|
[a35b458] | 62 |
|
---|
[9cfbf2f] | 63 | return service->service;
|
---|
[40313e4] | 64 | }
|
---|
| 65 |
|
---|
[062d900] | 66 | static bool service_key_equal(void *key, const ht_link_t *item)
|
---|
[40313e4] | 67 | {
|
---|
[9cfbf2f] | 68 | hashed_service_t *service =
|
---|
| 69 | hash_table_get_inst(item, hashed_service_t, link);
|
---|
[a35b458] | 70 |
|
---|
[9cfbf2f] | 71 | return service->service == *(service_t *) key;
|
---|
[40313e4] | 72 | }
|
---|
| 73 |
|
---|
| 74 | /** Operations for service hash table. */
|
---|
[062d900] | 75 | static hash_table_ops_t service_hash_table_ops = {
|
---|
[40313e4] | 76 | .hash = service_hash,
|
---|
[062d900] | 77 | .key_hash = service_key_hash,
|
---|
| 78 | .key_equal = service_key_equal,
|
---|
[4e00f87] | 79 | .equal = NULL,
|
---|
| 80 | .remove_callback = NULL
|
---|
[40313e4] | 81 | };
|
---|
| 82 |
|
---|
| 83 | /** Service hash table structure. */
|
---|
| 84 | static hash_table_t service_hash_table;
|
---|
| 85 |
|
---|
| 86 | /** Pending connection structure. */
|
---|
| 87 | typedef struct {
|
---|
| 88 | link_t link;
|
---|
[9cfbf2f] | 89 | service_t service; /**< Service ID */
|
---|
| 90 | iface_t iface; /**< Interface ID */
|
---|
| 91 | ipc_callid_t callid; /**< Call ID waiting for the connection */
|
---|
| 92 | sysarg_t arg3; /**< Third argument */
|
---|
[40313e4] | 93 | } pending_conn_t;
|
---|
| 94 |
|
---|
[b72efe8] | 95 | static list_t pending_conn;
|
---|
[40313e4] | 96 |
|
---|
[b7fd2a0] | 97 | errno_t service_init(void)
|
---|
[40313e4] | 98 | {
|
---|
[9cfbf2f] | 99 | if (!hash_table_create(&service_hash_table, 0, 0,
|
---|
| 100 | &service_hash_table_ops)) {
|
---|
| 101 | printf("%s: No memory available for services\n", NAME);
|
---|
[40313e4] | 102 | return ENOMEM;
|
---|
| 103 | }
|
---|
[a35b458] | 104 |
|
---|
[40313e4] | 105 | list_initialize(&pending_conn);
|
---|
[a35b458] | 106 |
|
---|
[40313e4] | 107 | return EOK;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /** Process pending connection requests */
|
---|
| 111 | void process_pending_conn(void)
|
---|
| 112 | {
|
---|
| 113 | loop:
|
---|
[9cfbf2f] | 114 | list_foreach(pending_conn, link, pending_conn_t, pending) {
|
---|
| 115 | ht_link_t *link = hash_table_find(&service_hash_table, &pending->service);
|
---|
[40313e4] | 116 | if (!link)
|
---|
| 117 | continue;
|
---|
[a35b458] | 118 |
|
---|
[9cfbf2f] | 119 | hashed_service_t *hashed_service = hash_table_get_inst(link, hashed_service_t, link);
|
---|
[7b616e2] | 120 | async_exch_t *exch = async_exchange_begin(hashed_service->sess);
|
---|
| 121 | async_forward_fast(pending->callid, exch, pending->iface,
|
---|
| 122 | pending->arg3, 0, IPC_FF_NONE);
|
---|
| 123 | async_exchange_end(exch);
|
---|
[a35b458] | 124 |
|
---|
[9cfbf2f] | 125 | list_remove(&pending->link);
|
---|
| 126 | free(pending);
|
---|
[a35b458] | 127 |
|
---|
[40313e4] | 128 | goto loop;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /** Register service.
|
---|
| 133 | *
|
---|
| 134 | * @param service Service to be registered.
|
---|
| 135 | * @param phone Phone to be used for connections to the service.
|
---|
| 136 | * @param call Pointer to call structure.
|
---|
| 137 | *
|
---|
| 138 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 139 | *
|
---|
| 140 | */
|
---|
[b7fd2a0] | 141 | errno_t register_service(service_t service, sysarg_t phone, ipc_call_t *call)
|
---|
[40313e4] | 142 | {
|
---|
[062d900] | 143 | if (hash_table_find(&service_hash_table, &service))
|
---|
[8a637a4] | 144 | return EEXIST;
|
---|
[a35b458] | 145 |
|
---|
[9cfbf2f] | 146 | hashed_service_t *hashed_service =
|
---|
| 147 | (hashed_service_t *) malloc(sizeof(hashed_service_t));
|
---|
| 148 | if (!hashed_service)
|
---|
[40313e4] | 149 | return ENOMEM;
|
---|
[a35b458] | 150 |
|
---|
[9cfbf2f] | 151 | hashed_service->service = service;
|
---|
[7b616e2] | 152 | hashed_service->sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 153 | if (hashed_service->sess == NULL)
|
---|
| 154 | return EIO;
|
---|
[a35b458] | 155 |
|
---|
[9cfbf2f] | 156 | hash_table_insert(&service_hash_table, &hashed_service->link);
|
---|
[007e6efa] | 157 | return EOK;
|
---|
[40313e4] | 158 | }
|
---|
| 159 |
|
---|
| 160 | /** Connect client to service.
|
---|
| 161 | *
|
---|
| 162 | * @param service Service to be connected to.
|
---|
[9cfbf2f] | 163 | * @param iface Interface to be connected to.
|
---|
[40313e4] | 164 | * @param call Pointer to call structure.
|
---|
| 165 | * @param callid Call ID of the request.
|
---|
| 166 | *
|
---|
| 167 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 168 | *
|
---|
| 169 | */
|
---|
[9cfbf2f] | 170 | void connect_to_service(service_t service, iface_t iface, ipc_call_t *call,
|
---|
| 171 | ipc_callid_t callid)
|
---|
[40313e4] | 172 | {
|
---|
[9cfbf2f] | 173 | sysarg_t arg3 = IPC_GET_ARG3(*call);
|
---|
| 174 | sysarg_t flags = IPC_GET_ARG4(*call);
|
---|
[b7fd2a0] | 175 | errno_t retval;
|
---|
[a35b458] | 176 |
|
---|
[062d900] | 177 | ht_link_t *link = hash_table_find(&service_hash_table, &service);
|
---|
[40313e4] | 178 | if (!link) {
|
---|
[9cfbf2f] | 179 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
[40313e4] | 180 | /* Blocking connection, add to pending list */
|
---|
[9cfbf2f] | 181 | pending_conn_t *pending =
|
---|
[40313e4] | 182 | (pending_conn_t *) malloc(sizeof(pending_conn_t));
|
---|
[9cfbf2f] | 183 | if (!pending) {
|
---|
[40313e4] | 184 | retval = ENOMEM;
|
---|
| 185 | goto out;
|
---|
| 186 | }
|
---|
[a35b458] | 187 |
|
---|
[9cfbf2f] | 188 | link_initialize(&pending->link);
|
---|
| 189 | pending->service = service;
|
---|
| 190 | pending->iface = iface;
|
---|
| 191 | pending->callid = callid;
|
---|
| 192 | pending->arg3 = arg3;
|
---|
[a35b458] | 193 |
|
---|
[9cfbf2f] | 194 | list_append(&pending->link, &pending_conn);
|
---|
[40313e4] | 195 | return;
|
---|
| 196 | }
|
---|
[a35b458] | 197 |
|
---|
[40313e4] | 198 | retval = ENOENT;
|
---|
| 199 | goto out;
|
---|
| 200 | }
|
---|
[a35b458] | 201 |
|
---|
[9cfbf2f] | 202 | hashed_service_t *hashed_service = hash_table_get_inst(link, hashed_service_t, link);
|
---|
[7b616e2] | 203 | async_exch_t *exch = async_exchange_begin(hashed_service->sess);
|
---|
| 204 | async_forward_fast(callid, exch, iface, arg3, 0, IPC_FF_NONE);
|
---|
| 205 | async_exchange_end(exch);
|
---|
[c638c07] | 206 | return;
|
---|
[a35b458] | 207 |
|
---|
[40313e4] | 208 | out:
|
---|
[7b616e2] | 209 | async_answer_0(callid, retval);
|
---|
[40313e4] | 210 | }
|
---|
| 211 |
|
---|
| 212 | /**
|
---|
| 213 | * @}
|
---|
| 214 | */
|
---|