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