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