[babe786] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Ondrej Palkovsky
|
---|
| 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 | /**
|
---|
| 30 | * @file ns.c
|
---|
| 31 | * @brief Naming service for HelenOS IPC.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[38edb96] | 34 | #include <ipc/ipc.h>
|
---|
| 35 | #include <ipc/ns.h>
|
---|
[69cdeec] | 36 | #include <stdio.h>
|
---|
| 37 | #include <unistd.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <errno.h>
|
---|
[babe786] | 40 | #include <assert.h>
|
---|
| 41 | #include <libadt/list.h>
|
---|
| 42 | #include <libadt/hash_table.h>
|
---|
[0b99e40] | 43 | #include <sysinfo.h>
|
---|
| 44 | #include <ddi.h>
|
---|
| 45 | #include <as.h>
|
---|
[babe786] | 46 |
|
---|
| 47 | #define NAME "NS"
|
---|
| 48 |
|
---|
| 49 | #define NS_HASH_TABLE_CHAINS 20
|
---|
| 50 |
|
---|
[043dcc27] | 51 | static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
|
---|
| 52 | static int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid);
|
---|
| 53 |
|
---|
[babe786] | 54 | /* Static functions implementing NS hash table operations. */
|
---|
| 55 | static hash_index_t ns_hash(unsigned long *key);
|
---|
| 56 | static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
|
---|
[043dcc27] | 57 | static void ns_remove(link_t *item);
|
---|
[babe786] | 58 |
|
---|
| 59 | /** Operations for NS hash table. */
|
---|
| 60 | static hash_table_operations_t ns_hash_table_ops = {
|
---|
| 61 | .hash = ns_hash,
|
---|
| 62 | .compare = ns_compare,
|
---|
[043dcc27] | 63 | .remove_callback = ns_remove
|
---|
[babe786] | 64 | };
|
---|
| 65 |
|
---|
| 66 | /** NS hash table structure. */
|
---|
| 67 | static hash_table_t ns_hash_table;
|
---|
| 68 |
|
---|
| 69 | /** NS hash table item. */
|
---|
| 70 | typedef struct {
|
---|
| 71 | link_t link;
|
---|
| 72 | ipcarg_t service; /**< Number of the service. */
|
---|
| 73 | ipcarg_t phone; /**< Phone registered with the service. */
|
---|
[043dcc27] | 74 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */
|
---|
[babe786] | 75 | } hashed_service_t;
|
---|
| 76 |
|
---|
| 77 | int static ping_phone;
|
---|
[69cdeec] | 78 |
|
---|
[0b99e40] | 79 | static void get_realtime_as(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 80 | {
|
---|
| 81 | static void *addr = NULL;
|
---|
| 82 | void *ph_addr;
|
---|
| 83 |
|
---|
| 84 | if (!addr) {
|
---|
| 85 | ph_addr = (void *)sysinfo_value("clock.faddr");
|
---|
| 86 | if (!ph_addr) {
|
---|
| 87 | ipc_answer_fast(callid, ENOENT, 0, 0);
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
| 90 | addr = (void *)(200*1024*1024); /* TODO: intelligent freemem space */
|
---|
| 91 | map_physmem(task_get_id(), ph_addr, addr, 1,
|
---|
| 92 | AS_AREA_READ | AS_AREA_CACHEABLE);
|
---|
| 93 | }
|
---|
[3f695aad] | 94 | ipc_answer_fast(callid, 0, (ipcarg_t)addr, AS_AREA_READ | AS_AREA_CACHEABLE);
|
---|
[0b99e40] | 95 | }
|
---|
| 96 |
|
---|
[69cdeec] | 97 | int main(int argc, char **argv)
|
---|
| 98 | {
|
---|
| 99 | ipc_call_t call;
|
---|
| 100 | ipc_callid_t callid;
|
---|
[6efe0ddf] | 101 | char *as_area;
|
---|
[69cdeec] | 102 |
|
---|
| 103 | ipcarg_t retval, arg1, arg2;
|
---|
| 104 |
|
---|
[afa6e74] | 105 | // printf("%s: Naming service started.\n", NAME);
|
---|
[babe786] | 106 |
|
---|
[043dcc27] | 107 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, &ns_hash_table_ops)) {
|
---|
[afa6e74] | 108 | // printf("%s: cannot create hash table\n", NAME);
|
---|
[babe786] | 109 | return ENOMEM;
|
---|
| 110 | }
|
---|
[250717cc] | 111 |
|
---|
[69cdeec] | 112 | while (1) {
|
---|
[04a73cdf] | 113 | callid = ipc_wait_for_call(&call);
|
---|
[250717cc] | 114 | // printf("NS: Call in_phone_hash=%lX...", call.in_phone_hash);
|
---|
[4c61e60] | 115 | switch (IPC_GET_METHOD(call)) {
|
---|
[6efe0ddf] | 116 | case IPC_M_AS_AREA_SEND:
|
---|
[abda850] | 117 | as_area = (char *)IPC_GET_ARG1(call);
|
---|
| 118 | // printf("Received as_area: %P, size:%d\n", as_area, IPC_GET_ARG2(call));
|
---|
[250717cc] | 119 | retval = ipc_answer_fast(callid, 0,(sysarg_t)(1024*1024), 0);
|
---|
[8a568e3] | 120 | if (!retval) {
|
---|
[afa6e74] | 121 | // printf("Reading shared memory...");
|
---|
| 122 | // printf("Text: %s", as_area);
|
---|
[8a568e3] | 123 | } else
|
---|
[afa6e74] | 124 | // printf("Failed answer: %d\n", retval);
|
---|
[8a568e3] | 125 | continue;
|
---|
[0b99e40] | 126 | case IPC_M_AS_AREA_RECV:
|
---|
| 127 | get_realtime_as(callid, &call);
|
---|
| 128 | continue;
|
---|
[6180b57] | 129 | case IPC_M_INTERRUPT:
|
---|
| 130 | break;
|
---|
[7048773] | 131 | case IPC_M_PHONE_HUNGUP:
|
---|
| 132 | retval = 0;
|
---|
| 133 | break;
|
---|
[043dcc27] | 134 | case IPC_M_CONNECT_TO_ME:
|
---|
[babe786] | 135 | /*
|
---|
[043dcc27] | 136 | * Server requests service registration.
|
---|
[babe786] | 137 | */
|
---|
[043dcc27] | 138 | retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG3(call), &call);
|
---|
| 139 | ping_phone = IPC_GET_ARG3(call);
|
---|
[69cdeec] | 140 | break;
|
---|
[7048773] | 141 | case IPC_M_CONNECT_ME_TO:
|
---|
[043dcc27] | 142 | /*
|
---|
| 143 | * Client requests to be connected to a service.
|
---|
| 144 | */
|
---|
| 145 | retval = connect_to_service(IPC_GET_ARG1(call), &call, callid);
|
---|
[11eae82] | 146 | break;
|
---|
[108602e] | 147 | case NS_HANGUP:
|
---|
[afa6e74] | 148 | // printf("Closing connection.\n");
|
---|
[108602e] | 149 | retval = EHANGUP;
|
---|
| 150 | break;
|
---|
[69cdeec] | 151 | case NS_PING:
|
---|
[afa6e74] | 152 | // printf("Ping...%P %P\n", IPC_GET_ARG1(call),
|
---|
| 153 | // IPC_GET_ARG2(call));
|
---|
[69cdeec] | 154 | retval = 0;
|
---|
| 155 | arg1 = 0xdead;
|
---|
| 156 | arg2 = 0xbeef;
|
---|
| 157 | break;
|
---|
| 158 | case NS_PING_SVC:
|
---|
[afa6e74] | 159 | // printf("NS:Pinging service %d\n", ping_phone);
|
---|
[babe786] | 160 | ipc_call_sync(ping_phone, NS_PING, 0xbeef, 0);
|
---|
[afa6e74] | 161 | // printf("NS:Got pong\n");
|
---|
[69cdeec] | 162 | break;
|
---|
| 163 | default:
|
---|
[afa6e74] | 164 | // printf("Unknown method: %zd\n", IPC_GET_METHOD(call));
|
---|
[69cdeec] | 165 | retval = ENOENT;
|
---|
| 166 | break;
|
---|
| 167 | }
|
---|
[602ca36b] | 168 | if (! (callid & IPC_CALLID_NOTIFICATION)) {
|
---|
[250717cc] | 169 | // printf("Answering.\n");
|
---|
| 170 | ipc_answer_fast(callid, retval, arg1, arg2);
|
---|
[602ca36b] | 171 | }
|
---|
[69cdeec] | 172 | }
|
---|
| 173 | }
|
---|
[babe786] | 174 |
|
---|
[108602e] | 175 | /** Register service.
|
---|
[043dcc27] | 176 | *
|
---|
| 177 | * @param service Service to be registered.
|
---|
| 178 | * @param phone phone Phone to be used for connections to the service.
|
---|
| 179 | * @param call Pointer to call structure.
|
---|
| 180 | *
|
---|
| 181 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 182 | */
|
---|
| 183 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
|
---|
| 184 | {
|
---|
| 185 | unsigned long keys[3] = { service, call->in_phone_hash, 0 };
|
---|
| 186 | hashed_service_t *hs;
|
---|
| 187 |
|
---|
[afa6e74] | 188 | // printf("Registering service %d on phone %d...", service, phone);
|
---|
[043dcc27] | 189 |
|
---|
| 190 | if (hash_table_find(&ns_hash_table, keys)) {
|
---|
[afa6e74] | 191 | // printf("Service %d already registered.\n", service);
|
---|
[043dcc27] | 192 | return EEXISTS;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
|
---|
| 196 | if (!hs) {
|
---|
[afa6e74] | 197 | // printf("Failed to register service %d.\n", service);
|
---|
[043dcc27] | 198 | return ENOMEM;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | link_initialize(&hs->link);
|
---|
| 202 | hs->service = service;
|
---|
| 203 | hs->phone = phone;
|
---|
| 204 | hs->in_phone_hash = call->in_phone_hash;
|
---|
| 205 | hash_table_insert(&ns_hash_table, keys, &hs->link);
|
---|
| 206 |
|
---|
| 207 | return 0;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /** Connect client to service.
|
---|
| 211 | *
|
---|
| 212 | * @param service Service to be connected to.
|
---|
| 213 | * @param call Pointer to call structure.
|
---|
| 214 | * @param callid Call ID of the request.
|
---|
| 215 | *
|
---|
| 216 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 217 | */
|
---|
| 218 | int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
|
---|
| 219 | {
|
---|
| 220 | unsigned long keys[3] = { service, 0, 0 };
|
---|
| 221 | link_t *hlp;
|
---|
| 222 | hashed_service_t *hs;
|
---|
| 223 |
|
---|
| 224 | hlp = hash_table_find(&ns_hash_table, keys);
|
---|
| 225 | if (!hlp) {
|
---|
[250717cc] | 226 | // printf("Service %d not registered.\n", service);
|
---|
[043dcc27] | 227 | return ENOENT;
|
---|
| 228 | }
|
---|
| 229 | hs = hash_table_get_instance(hlp, hashed_service_t, link);
|
---|
[afa6e74] | 230 | // printf("Connecting in_phone_hash=%lX to service at phone %d...", call->in_phone_hash, hs->phone);
|
---|
[043dcc27] | 231 | return ipc_forward_fast(callid, hs->phone, 0, 0);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[babe786] | 234 | /** Compute hash index into NS hash table.
|
---|
| 235 | *
|
---|
[043dcc27] | 236 | * @param key Pointer keys. However, only the first key (i.e. service number)
|
---|
| 237 | * is used to compute the hash index.
|
---|
| 238 | * @return Hash index corresponding to key[0].
|
---|
[babe786] | 239 | */
|
---|
| 240 | hash_index_t ns_hash(unsigned long *key)
|
---|
| 241 | {
|
---|
| 242 | assert(key);
|
---|
| 243 | return *key % NS_HASH_TABLE_CHAINS;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /** Compare a key with hashed item.
|
---|
| 247 | *
|
---|
[043dcc27] | 248 | * This compare function always ignores the third key.
|
---|
| 249 | * It exists only to make it possible to remove records
|
---|
| 250 | * originating from connection with key[1] in_phone_hash
|
---|
| 251 | * value. Note that this is close to being classified
|
---|
| 252 | * as a nasty hack.
|
---|
| 253 | *
|
---|
| 254 | * @param key Array of keys.
|
---|
| 255 | * @param keys Must be lesser or equal to 3.
|
---|
[babe786] | 256 | * @param item Pointer to a hash table item.
|
---|
| 257 | * @return Non-zero if the key matches the item, zero otherwise.
|
---|
| 258 | */
|
---|
[043dcc27] | 259 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[babe786] | 260 | {
|
---|
| 261 | hashed_service_t *hs;
|
---|
| 262 |
|
---|
| 263 | assert(key);
|
---|
[043dcc27] | 264 | assert(keys <= 3);
|
---|
[babe786] | 265 | assert(item);
|
---|
| 266 |
|
---|
| 267 | hs = hash_table_get_instance(item, hashed_service_t, link);
|
---|
| 268 |
|
---|
[043dcc27] | 269 | if (keys == 2)
|
---|
| 270 | return key[1] == hs->in_phone_hash;
|
---|
| 271 | else
|
---|
| 272 | return key[0] == hs->service;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /** Perform actions after removal of item from the hash table.
|
---|
| 276 | *
|
---|
| 277 | * @param item Item that was removed from the hash table.
|
---|
| 278 | */
|
---|
| 279 | void ns_remove(link_t *item)
|
---|
| 280 | {
|
---|
| 281 | assert(item);
|
---|
| 282 | free(hash_table_get_instance(item, hashed_service_t, link));
|
---|
[babe786] | 283 | }
|
---|