[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>
|
---|
[bfd1546] | 43 | #include <bool.h>
|
---|
[69cdeec] | 44 | #include <unistd.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 | #include <errno.h>
|
---|
[babe786] | 47 | #include <assert.h>
|
---|
| 48 | #include <libadt/list.h>
|
---|
| 49 | #include <libadt/hash_table.h>
|
---|
[0b99e40] | 50 | #include <sysinfo.h>
|
---|
[bfd1546] | 51 | #include <loader/loader.h>
|
---|
[0b99e40] | 52 | #include <ddi.h>
|
---|
| 53 | #include <as.h>
|
---|
[babe786] | 54 |
|
---|
[e623197] | 55 | #define NAME "ns"
|
---|
[babe786] | 56 |
|
---|
| 57 | #define NS_HASH_TABLE_CHAINS 20
|
---|
| 58 |
|
---|
[043dcc27] | 59 | static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
|
---|
[0eb58f1] | 60 | static int connect_to_service(ipcarg_t service, ipc_call_t *call,
|
---|
| 61 | ipc_callid_t callid);
|
---|
[043dcc27] | 62 |
|
---|
[bfd1546] | 63 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
|
---|
| 64 | ipc_callid_t callid);
|
---|
| 65 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
|
---|
| 66 | ipc_callid_t callid);
|
---|
| 67 |
|
---|
[babe786] | 68 | /* Static functions implementing NS hash table operations. */
|
---|
| 69 | static hash_index_t ns_hash(unsigned long *key);
|
---|
| 70 | static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
|
---|
[043dcc27] | 71 | static void ns_remove(link_t *item);
|
---|
[babe786] | 72 |
|
---|
| 73 | /** Operations for NS hash table. */
|
---|
| 74 | static hash_table_operations_t ns_hash_table_ops = {
|
---|
| 75 | .hash = ns_hash,
|
---|
| 76 | .compare = ns_compare,
|
---|
[043dcc27] | 77 | .remove_callback = ns_remove
|
---|
[babe786] | 78 | };
|
---|
| 79 |
|
---|
| 80 | /** NS hash table structure. */
|
---|
| 81 | static hash_table_t ns_hash_table;
|
---|
| 82 |
|
---|
| 83 | /** NS hash table item. */
|
---|
| 84 | typedef struct {
|
---|
| 85 | link_t link;
|
---|
| 86 | ipcarg_t service; /**< Number of the service. */
|
---|
| 87 | ipcarg_t phone; /**< Phone registered with the service. */
|
---|
[043dcc27] | 88 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */
|
---|
[babe786] | 89 | } hashed_service_t;
|
---|
| 90 |
|
---|
[51dbadf3] | 91 | static void *clockaddr = NULL;
|
---|
| 92 | static void *klogaddr = NULL;
|
---|
| 93 |
|
---|
[bfd1546] | 94 | /** Request for connection to a clonable service. */
|
---|
| 95 | typedef struct {
|
---|
| 96 | link_t link;
|
---|
| 97 | ipcarg_t service;
|
---|
[76dd25b] | 98 | ipc_call_t call;
|
---|
[bfd1546] | 99 | ipc_callid_t callid;
|
---|
| 100 | } cs_req_t;
|
---|
| 101 |
|
---|
| 102 | /** List of clonable-service connection requests. */
|
---|
| 103 | static link_t cs_req;
|
---|
| 104 |
|
---|
| 105 | /** Return true if @a service is clonable. */
|
---|
| 106 | static bool service_clonable(int service)
|
---|
| 107 | {
|
---|
| 108 | return service == SERVICE_LOAD;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[ae318d3] | 111 | static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name, void **addr)
|
---|
[0b99e40] | 112 | {
|
---|
| 113 | void *ph_addr;
|
---|
[ae318d3] | 114 |
|
---|
[51dbadf3] | 115 | if (!*addr) {
|
---|
[f8ddd17] | 116 | ph_addr = (void *) sysinfo_value(name);
|
---|
[0b99e40] | 117 | if (!ph_addr) {
|
---|
[b74959bd] | 118 | ipc_answer_0(callid, ENOENT);
|
---|
[0b99e40] | 119 | return;
|
---|
| 120 | }
|
---|
[2057572] | 121 | *addr = as_get_mappable_page(PAGE_SIZE);
|
---|
[ae318d3] | 122 | if (physmem_map(ph_addr, *addr, 1,
|
---|
| 123 | AS_AREA_READ | AS_AREA_CACHEABLE) != 0) {
|
---|
| 124 | ipc_answer_0(callid, ENOENT);
|
---|
| 125 | return;
|
---|
| 126 | }
|
---|
[0b99e40] | 127 | }
|
---|
[b74959bd] | 128 | ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ);
|
---|
[0b99e40] | 129 | }
|
---|
| 130 |
|
---|
[69cdeec] | 131 | int main(int argc, char **argv)
|
---|
| 132 | {
|
---|
[e623197] | 133 | printf(NAME ": HelenOS IPC Naming Service\n");
|
---|
| 134 |
|
---|
[69cdeec] | 135 | ipc_call_t call;
|
---|
| 136 | ipc_callid_t callid;
|
---|
| 137 |
|
---|
[a46da63] | 138 | ipcarg_t retval;
|
---|
[69cdeec] | 139 |
|
---|
[0eb58f1] | 140 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
|
---|
| 141 | &ns_hash_table_ops)) {
|
---|
[e623197] | 142 | printf(NAME ": No memory available\n");
|
---|
[babe786] | 143 | return ENOMEM;
|
---|
| 144 | }
|
---|
[bfd1546] | 145 |
|
---|
| 146 | list_initialize(&cs_req);
|
---|
[e623197] | 147 |
|
---|
| 148 | printf(NAME ": Accepting connections\n");
|
---|
[69cdeec] | 149 | while (1) {
|
---|
[04a73cdf] | 150 | callid = ipc_wait_for_call(&call);
|
---|
[4c61e60] | 151 | switch (IPC_GET_METHOD(call)) {
|
---|
[27d293a] | 152 | case IPC_M_SHARE_IN:
|
---|
[51dbadf3] | 153 | switch (IPC_GET_ARG3(call)) {
|
---|
| 154 | case SERVICE_MEM_REALTIME:
|
---|
[ae318d3] | 155 | get_as_area(callid, &call, "clock.faddr", &clockaddr);
|
---|
[51dbadf3] | 156 | break;
|
---|
| 157 | case SERVICE_MEM_KLOG:
|
---|
[ae318d3] | 158 | get_as_area(callid, &call, "klog.faddr", &klogaddr);
|
---|
[51dbadf3] | 159 | break;
|
---|
| 160 | default:
|
---|
[b74959bd] | 161 | ipc_answer_0(callid, ENOENT);
|
---|
[51dbadf3] | 162 | }
|
---|
[0b99e40] | 163 | continue;
|
---|
[7048773] | 164 | case IPC_M_PHONE_HUNGUP:
|
---|
[20614d0] | 165 | retval = EOK;
|
---|
[7048773] | 166 | break;
|
---|
[043dcc27] | 167 | case IPC_M_CONNECT_TO_ME:
|
---|
[babe786] | 168 | /*
|
---|
[043dcc27] | 169 | * Server requests service registration.
|
---|
[babe786] | 170 | */
|
---|
[bfd1546] | 171 | if (service_clonable(IPC_GET_ARG1(call))) {
|
---|
| 172 | register_clonable(IPC_GET_ARG1(call),
|
---|
| 173 | IPC_GET_ARG5(call), &call, callid);
|
---|
| 174 | continue;
|
---|
| 175 | } else {
|
---|
| 176 | retval = register_service(IPC_GET_ARG1(call),
|
---|
| 177 | IPC_GET_ARG5(call), &call);
|
---|
| 178 | }
|
---|
[69cdeec] | 179 | break;
|
---|
[7048773] | 180 | case IPC_M_CONNECT_ME_TO:
|
---|
[043dcc27] | 181 | /*
|
---|
| 182 | * Client requests to be connected to a service.
|
---|
| 183 | */
|
---|
[bfd1546] | 184 | if (service_clonable(IPC_GET_ARG1(call))) {
|
---|
| 185 | connect_to_clonable(IPC_GET_ARG1(call),
|
---|
| 186 | &call, callid);
|
---|
| 187 | continue;
|
---|
| 188 | } else {
|
---|
| 189 | retval = connect_to_service(IPC_GET_ARG1(call),
|
---|
| 190 | &call, callid);
|
---|
| 191 | }
|
---|
[11eae82] | 192 | break;
|
---|
[69cdeec] | 193 | default:
|
---|
| 194 | retval = ENOENT;
|
---|
| 195 | break;
|
---|
| 196 | }
|
---|
[0eb58f1] | 197 | if (!(callid & IPC_CALLID_NOTIFICATION)) {
|
---|
[b74959bd] | 198 | ipc_answer_0(callid, retval);
|
---|
[602ca36b] | 199 | }
|
---|
[69cdeec] | 200 | }
|
---|
[e623197] | 201 |
|
---|
| 202 | /* Not reached */
|
---|
| 203 | return 0;
|
---|
[69cdeec] | 204 | }
|
---|
[babe786] | 205 |
|
---|
[108602e] | 206 | /** Register service.
|
---|
[043dcc27] | 207 | *
|
---|
[bfd1546] | 208 | * @param service Service to be registered.
|
---|
| 209 | * @param phone Phone to be used for connections to the service.
|
---|
| 210 | * @param call Pointer to call structure.
|
---|
[043dcc27] | 211 | *
|
---|
[bfd1546] | 212 | * @return Zero on success or a value from @ref errno.h.
|
---|
[043dcc27] | 213 | */
|
---|
| 214 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
|
---|
| 215 | {
|
---|
[0eb58f1] | 216 | unsigned long keys[3] = {
|
---|
| 217 | service,
|
---|
| 218 | call->in_phone_hash,
|
---|
| 219 | 0
|
---|
| 220 | };
|
---|
[043dcc27] | 221 | hashed_service_t *hs;
|
---|
[bfd1546] | 222 |
|
---|
[043dcc27] | 223 | if (hash_table_find(&ns_hash_table, keys)) {
|
---|
| 224 | return EEXISTS;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
|
---|
| 228 | if (!hs) {
|
---|
| 229 | return ENOMEM;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | link_initialize(&hs->link);
|
---|
| 233 | hs->service = service;
|
---|
| 234 | hs->phone = phone;
|
---|
| 235 | hs->in_phone_hash = call->in_phone_hash;
|
---|
| 236 | hash_table_insert(&ns_hash_table, keys, &hs->link);
|
---|
| 237 |
|
---|
| 238 | return 0;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /** Connect client to service.
|
---|
| 242 | *
|
---|
[bfd1546] | 243 | * @param service Service to be connected to.
|
---|
| 244 | * @param call Pointer to call structure.
|
---|
| 245 | * @param callid Call ID of the request.
|
---|
[043dcc27] | 246 | *
|
---|
| 247 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 248 | */
|
---|
| 249 | int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
|
---|
| 250 | {
|
---|
| 251 | unsigned long keys[3] = { service, 0, 0 };
|
---|
| 252 | link_t *hlp;
|
---|
| 253 | hashed_service_t *hs;
|
---|
[bfd1546] | 254 |
|
---|
[043dcc27] | 255 | hlp = hash_table_find(&ns_hash_table, keys);
|
---|
| 256 | if (!hlp) {
|
---|
| 257 | return ENOENT;
|
---|
| 258 | }
|
---|
| 259 | hs = hash_table_get_instance(hlp, hashed_service_t, link);
|
---|
[b61d47d] | 260 | return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
|
---|
| 261 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
|
---|
[043dcc27] | 262 | }
|
---|
| 263 |
|
---|
[bfd1546] | 264 | /** Register clonable service.
|
---|
| 265 | *
|
---|
| 266 | * @param service Service to be registered.
|
---|
| 267 | * @param phone Phone to be used for connections to the service.
|
---|
| 268 | * @param call Pointer to call structure.
|
---|
| 269 | */
|
---|
| 270 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
|
---|
| 271 | ipc_callid_t callid)
|
---|
| 272 | {
|
---|
| 273 | int rc;
|
---|
| 274 | cs_req_t *csr;
|
---|
| 275 |
|
---|
| 276 | if (list_empty(&cs_req)) {
|
---|
| 277 | /* There was no pending connection request. */
|
---|
| 278 | printf(NAME ": Unexpected clonable server.\n");
|
---|
| 279 | ipc_answer_0(callid, EBUSY);
|
---|
| 280 | return;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | csr = list_get_instance(cs_req.next, cs_req_t, link);
|
---|
| 284 | list_remove(&csr->link);
|
---|
| 285 |
|
---|
| 286 | /* Currently we can only handle a single type of clonable service. */
|
---|
| 287 | assert(csr->service == SERVICE_LOAD);
|
---|
| 288 |
|
---|
| 289 | ipc_answer_0(callid, EOK);
|
---|
| 290 |
|
---|
[76dd25b] | 291 | rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call),
|
---|
| 292 | IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE);
|
---|
[bfd1546] | 293 |
|
---|
| 294 | free(csr);
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /** Connect client to clonable service.
|
---|
| 298 | *
|
---|
| 299 | * @param service Service to be connected to.
|
---|
| 300 | * @param call Pointer to call structure.
|
---|
| 301 | * @param callid Call ID of the request.
|
---|
| 302 | *
|
---|
| 303 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 304 | */
|
---|
| 305 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
|
---|
| 306 | ipc_callid_t callid)
|
---|
| 307 | {
|
---|
| 308 | int rc;
|
---|
| 309 | cs_req_t *csr;
|
---|
| 310 |
|
---|
| 311 | assert(service == SERVICE_LOAD);
|
---|
| 312 |
|
---|
| 313 | csr = malloc(sizeof(cs_req_t));
|
---|
| 314 | if (csr == NULL) {
|
---|
| 315 | ipc_answer_0(callid, ENOMEM);
|
---|
| 316 | return;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /* Spawn a loader. */
|
---|
| 320 | rc = loader_spawn("loader");
|
---|
| 321 |
|
---|
| 322 | if (rc < 0) {
|
---|
| 323 | free(csr);
|
---|
| 324 | ipc_answer_0(callid, rc);
|
---|
| 325 | return;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | csr->service = service;
|
---|
[76dd25b] | 329 | csr->call = *call;
|
---|
[bfd1546] | 330 | csr->callid = callid;
|
---|
| 331 |
|
---|
| 332 | /*
|
---|
| 333 | * We can forward the call only after the server we spawned connects
|
---|
| 334 | * to us. Meanwhile we might need to service more connection requests.
|
---|
| 335 | * Thus we store the call in a queue.
|
---|
| 336 | */
|
---|
| 337 | list_append(&csr->link, &cs_req);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[babe786] | 340 | /** Compute hash index into NS hash table.
|
---|
| 341 | *
|
---|
[043dcc27] | 342 | * @param key Pointer keys. However, only the first key (i.e. service number)
|
---|
| 343 | * is used to compute the hash index.
|
---|
| 344 | * @return Hash index corresponding to key[0].
|
---|
[babe786] | 345 | */
|
---|
| 346 | hash_index_t ns_hash(unsigned long *key)
|
---|
| 347 | {
|
---|
| 348 | assert(key);
|
---|
| 349 | return *key % NS_HASH_TABLE_CHAINS;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /** Compare a key with hashed item.
|
---|
| 353 | *
|
---|
[043dcc27] | 354 | * This compare function always ignores the third key.
|
---|
| 355 | * It exists only to make it possible to remove records
|
---|
| 356 | * originating from connection with key[1] in_phone_hash
|
---|
| 357 | * value. Note that this is close to being classified
|
---|
| 358 | * as a nasty hack.
|
---|
| 359 | *
|
---|
| 360 | * @param key Array of keys.
|
---|
| 361 | * @param keys Must be lesser or equal to 3.
|
---|
[babe786] | 362 | * @param item Pointer to a hash table item.
|
---|
| 363 | * @return Non-zero if the key matches the item, zero otherwise.
|
---|
| 364 | */
|
---|
[043dcc27] | 365 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[babe786] | 366 | {
|
---|
| 367 | hashed_service_t *hs;
|
---|
| 368 |
|
---|
| 369 | assert(key);
|
---|
[043dcc27] | 370 | assert(keys <= 3);
|
---|
[babe786] | 371 | assert(item);
|
---|
| 372 |
|
---|
| 373 | hs = hash_table_get_instance(item, hashed_service_t, link);
|
---|
| 374 |
|
---|
[043dcc27] | 375 | if (keys == 2)
|
---|
| 376 | return key[1] == hs->in_phone_hash;
|
---|
| 377 | else
|
---|
| 378 | return key[0] == hs->service;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | /** Perform actions after removal of item from the hash table.
|
---|
| 382 | *
|
---|
| 383 | * @param item Item that was removed from the hash table.
|
---|
| 384 | */
|
---|
| 385 | void ns_remove(link_t *item)
|
---|
| 386 | {
|
---|
| 387 | assert(item);
|
---|
| 388 | free(hash_table_get_instance(item, hashed_service_t, link));
|
---|
[babe786] | 389 | }
|
---|
[a46da63] | 390 |
|
---|
[ce5bcb4] | 391 | /**
|
---|
| 392 | * @}
|
---|
| 393 | */
|
---|