source: mainline/ns/ns.c@ 51dbadf3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 51dbadf3 was 51dbadf3, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Added basic klog.
Added ipc tester.
TODO add serializing functions to psthread, so that the lines in klog won't intermix.

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[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>
[51dbadf3]36#include <ipc/services.h>
[69cdeec]37#include <stdio.h>
38#include <unistd.h>
39#include <stdlib.h>
40#include <errno.h>
[babe786]41#include <assert.h>
42#include <libadt/list.h>
43#include <libadt/hash_table.h>
[0b99e40]44#include <sysinfo.h>
45#include <ddi.h>
46#include <as.h>
[babe786]47
48#define NAME "NS"
49
50#define NS_HASH_TABLE_CHAINS 20
51
[043dcc27]52static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
53static int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid);
54
[babe786]55/* Static functions implementing NS hash table operations. */
56static hash_index_t ns_hash(unsigned long *key);
57static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
[043dcc27]58static void ns_remove(link_t *item);
[babe786]59
60/** Operations for NS hash table. */
61static hash_table_operations_t ns_hash_table_ops = {
62 .hash = ns_hash,
63 .compare = ns_compare,
[043dcc27]64 .remove_callback = ns_remove
[babe786]65};
66
67/** NS hash table structure. */
68static hash_table_t ns_hash_table;
69
70/** NS hash table item. */
71typedef struct {
72 link_t link;
73 ipcarg_t service; /**< Number of the service. */
74 ipcarg_t phone; /**< Phone registered with the service. */
[043dcc27]75 ipcarg_t in_phone_hash; /**< Incoming phone hash. */
[babe786]76} hashed_service_t;
77
78int static ping_phone;
[69cdeec]79
[51dbadf3]80static void *clockaddr = NULL;
81static void *klogaddr = NULL;
82
83static void get_as(ipc_callid_t callid, ipc_call_t *call, char *name, void **addr)
[0b99e40]84{
85 void *ph_addr;
86
[51dbadf3]87 if (!*addr) {
88 ph_addr = (void *)sysinfo_value(name);
[0b99e40]89 if (!ph_addr) {
90 ipc_answer_fast(callid, ENOENT, 0, 0);
91 return;
92 }
[51dbadf3]93 *addr = as_get_mappable_page(PAGE_SIZE);
94 map_physmem(ph_addr, *addr, 1, AS_AREA_READ | AS_AREA_CACHEABLE);
[0b99e40]95 }
[51dbadf3]96 ipc_answer_fast(callid, 0, (ipcarg_t)*addr, AS_AREA_READ);
[0b99e40]97}
98
[69cdeec]99int main(int argc, char **argv)
100{
101 ipc_call_t call;
102 ipc_callid_t callid;
[6efe0ddf]103 char *as_area;
[69cdeec]104
105 ipcarg_t retval, arg1, arg2;
106
[043dcc27]107 if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, &ns_hash_table_ops)) {
[babe786]108 return ENOMEM;
109 }
[250717cc]110
[69cdeec]111 while (1) {
[04a73cdf]112 callid = ipc_wait_for_call(&call);
[4c61e60]113 switch (IPC_GET_METHOD(call)) {
[0b99e40]114 case IPC_M_AS_AREA_RECV:
[51dbadf3]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 }
[0b99e40]125 continue;
[7048773]126 case IPC_M_PHONE_HUNGUP:
127 retval = 0;
128 break;
[043dcc27]129 case IPC_M_CONNECT_TO_ME:
[babe786]130 /*
[043dcc27]131 * Server requests service registration.
[babe786]132 */
[043dcc27]133 retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG3(call), &call);
[69cdeec]134 break;
[7048773]135 case IPC_M_CONNECT_ME_TO:
[043dcc27]136 /*
137 * Client requests to be connected to a service.
138 */
139 retval = connect_to_service(IPC_GET_ARG1(call), &call, callid);
[11eae82]140 break;
[69cdeec]141 default:
142 retval = ENOENT;
143 break;
144 }
[602ca36b]145 if (! (callid & IPC_CALLID_NOTIFICATION)) {
[250717cc]146 ipc_answer_fast(callid, retval, arg1, arg2);
[602ca36b]147 }
[69cdeec]148 }
149}
[babe786]150
[108602e]151/** Register service.
[043dcc27]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 */
159int 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 */
190int 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
[babe786]204/** Compute hash index into NS hash table.
205 *
[043dcc27]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].
[babe786]209 */
210hash_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 *
[043dcc27]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.
[babe786]226 * @param item Pointer to a hash table item.
227 * @return Non-zero if the key matches the item, zero otherwise.
228 */
[043dcc27]229int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
[babe786]230{
231 hashed_service_t *hs;
232
233 assert(key);
[043dcc27]234 assert(keys <= 3);
[babe786]235 assert(item);
236
237 hs = hash_table_get_instance(item, hashed_service_t, link);
238
[043dcc27]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 */
249void ns_remove(link_t *item)
250{
251 assert(item);
252 free(hash_table_get_instance(item, hashed_service_t, link));
[babe786]253}
Note: See TracBrowser for help on using the repository browser.