Subversion Repositories HelenOS-historic

Rev

Rev 1547 | Rev 1649 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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