Rev 2637 | Rev 3896 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1335 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Ondrej Palkovsky |
1335 | jermar | 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); |
2476 | jermar | 58 | static int connect_to_service(ipcarg_t service, ipc_call_t *call, |
59 | ipc_callid_t callid); |
||
1336 | jermar | 60 | |
1335 | jermar | 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); |
||
1336 | jermar | 64 | static void ns_remove(link_t *item); |
1335 | jermar | 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, |
||
1336 | jermar | 70 | .remove_callback = ns_remove |
1335 | jermar | 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. */ |
||
1336 | jermar | 81 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
1335 | jermar | 82 | } hashed_service_t; |
83 | |||
1596 | palkovsky | 84 | static void *clockaddr = NULL; |
85 | static void *klogaddr = NULL; |
||
86 | |||
2476 | jermar | 87 | static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name, |
88 | void **addr) |
||
1435 | palkovsky | 89 | { |
90 | void *ph_addr; |
||
91 | |||
1596 | palkovsky | 92 | if (!*addr) { |
2015 | jermar | 93 | ph_addr = (void *) sysinfo_value(name); |
1435 | palkovsky | 94 | if (!ph_addr) { |
2619 | jermar | 95 | ipc_answer_0(callid, ENOENT); |
1435 | palkovsky | 96 | return; |
97 | } |
||
2141 | jermar | 98 | *addr = as_get_mappable_page(PAGE_SIZE); |
2588 | jermar | 99 | physmem_map(ph_addr, *addr, 1, |
100 | AS_AREA_READ | AS_AREA_CACHEABLE); |
||
1435 | palkovsky | 101 | } |
2619 | jermar | 102 | ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ); |
1435 | palkovsky | 103 | } |
104 | |||
1030 | palkovsky | 105 | int main(int argc, char **argv) |
106 | { |
||
107 | ipc_call_t call; |
||
108 | ipc_callid_t callid; |
||
109 | |||
1719 | decky | 110 | ipcarg_t retval; |
1030 | palkovsky | 111 | |
2476 | jermar | 112 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, |
113 | &ns_hash_table_ops)) { |
||
1335 | jermar | 114 | return ENOMEM; |
115 | } |
||
1343 | jermar | 116 | |
1030 | palkovsky | 117 | while (1) { |
1365 | jermar | 118 | callid = ipc_wait_for_call(&call); |
1091 | palkovsky | 119 | switch (IPC_GET_METHOD(call)) { |
2677 | jermar | 120 | case IPC_M_SHARE_IN: |
1596 | palkovsky | 121 | switch (IPC_GET_ARG3(call)) { |
122 | case SERVICE_MEM_REALTIME: |
||
2015 | jermar | 123 | get_as_area(callid, &call, "clock.faddr", |
2141 | jermar | 124 | &clockaddr); |
1596 | palkovsky | 125 | break; |
126 | case SERVICE_MEM_KLOG: |
||
2015 | jermar | 127 | get_as_area(callid, &call, "klog.faddr", |
2141 | jermar | 128 | &klogaddr); |
1596 | palkovsky | 129 | break; |
130 | default: |
||
2619 | jermar | 131 | ipc_answer_0(callid, ENOENT); |
1596 | palkovsky | 132 | } |
1435 | palkovsky | 133 | continue; |
1089 | palkovsky | 134 | case IPC_M_PHONE_HUNGUP: |
2588 | jermar | 135 | retval = EOK; |
1089 | palkovsky | 136 | break; |
1336 | jermar | 137 | case IPC_M_CONNECT_TO_ME: |
1335 | jermar | 138 | /* |
1336 | jermar | 139 | * Server requests service registration. |
1335 | jermar | 140 | */ |
2476 | jermar | 141 | retval = register_service(IPC_GET_ARG1(call), |
2637 | cejka | 142 | IPC_GET_ARG5(call), &call); |
1030 | palkovsky | 143 | break; |
1089 | palkovsky | 144 | case IPC_M_CONNECT_ME_TO: |
1336 | jermar | 145 | /* |
146 | * Client requests to be connected to a service. |
||
147 | */ |
||
2476 | jermar | 148 | retval = connect_to_service(IPC_GET_ARG1(call), &call, |
149 | callid); |
||
1061 | palkovsky | 150 | break; |
1030 | palkovsky | 151 | default: |
152 | retval = ENOENT; |
||
153 | break; |
||
154 | } |
||
2476 | jermar | 155 | if (!(callid & IPC_CALLID_NOTIFICATION)) { |
2619 | jermar | 156 | ipc_answer_0(callid, retval); |
1282 | palkovsky | 157 | } |
1030 | palkovsky | 158 | } |
159 | } |
||
1335 | jermar | 160 | |
1338 | jermar | 161 | /** Register service. |
1336 | jermar | 162 | * |
163 | * @param service Service to be registered. |
||
2471 | jermar | 164 | * @param phone Phone to be used for connections to the service. |
1336 | jermar | 165 | * @param call Pointer to call structure. |
166 | * |
||
167 | * @return Zero on success or a value from @ref errno.h. |
||
168 | */ |
||
169 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call) |
||
170 | { |
||
2476 | jermar | 171 | unsigned long keys[3] = { |
172 | service, |
||
173 | call->in_phone_hash, |
||
174 | |||
175 | }; |
||
1336 | jermar | 176 | hashed_service_t *hs; |
177 | |||
178 | if (hash_table_find(&ns_hash_table, keys)) { |
||
179 | return EEXISTS; |
||
180 | } |
||
181 | |||
182 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
||
183 | if (!hs) { |
||
184 | return ENOMEM; |
||
185 | } |
||
186 | |||
187 | link_initialize(&hs->link); |
||
188 | hs->service = service; |
||
189 | hs->phone = phone; |
||
190 | hs->in_phone_hash = call->in_phone_hash; |
||
191 | hash_table_insert(&ns_hash_table, keys, &hs->link); |
||
192 | |||
193 | return 0; |
||
194 | } |
||
195 | |||
196 | /** Connect client to service. |
||
197 | * |
||
198 | * @param service Service to be connected to. |
||
199 | * @param call Pointer to call structure. |
||
200 | * @param callid Call ID of the request. |
||
201 | * |
||
202 | * @return Zero on success or a value from @ref errno.h. |
||
203 | */ |
||
204 | int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid) |
||
205 | { |
||
206 | unsigned long keys[3] = { service, 0, 0 }; |
||
207 | link_t *hlp; |
||
208 | hashed_service_t *hs; |
||
209 | |||
210 | hlp = hash_table_find(&ns_hash_table, keys); |
||
211 | if (!hlp) { |
||
212 | return ENOENT; |
||
213 | } |
||
214 | hs = hash_table_get_instance(hlp, hashed_service_t, link); |
||
2635 | cejka | 215 | return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call), |
216 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE); |
||
1336 | jermar | 217 | } |
218 | |||
1335 | jermar | 219 | /** Compute hash index into NS hash table. |
220 | * |
||
1336 | jermar | 221 | * @param key Pointer keys. However, only the first key (i.e. service number) |
222 | * is used to compute the hash index. |
||
223 | * @return Hash index corresponding to key[0]. |
||
1335 | jermar | 224 | */ |
225 | hash_index_t ns_hash(unsigned long *key) |
||
226 | { |
||
227 | assert(key); |
||
228 | return *key % NS_HASH_TABLE_CHAINS; |
||
229 | } |
||
230 | |||
231 | /** Compare a key with hashed item. |
||
232 | * |
||
1336 | jermar | 233 | * This compare function always ignores the third key. |
234 | * It exists only to make it possible to remove records |
||
235 | * originating from connection with key[1] in_phone_hash |
||
236 | * value. Note that this is close to being classified |
||
237 | * as a nasty hack. |
||
238 | * |
||
239 | * @param key Array of keys. |
||
240 | * @param keys Must be lesser or equal to 3. |
||
1335 | jermar | 241 | * @param item Pointer to a hash table item. |
242 | * @return Non-zero if the key matches the item, zero otherwise. |
||
243 | */ |
||
1336 | jermar | 244 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item) |
1335 | jermar | 245 | { |
246 | hashed_service_t *hs; |
||
247 | |||
248 | assert(key); |
||
1336 | jermar | 249 | assert(keys <= 3); |
1335 | jermar | 250 | assert(item); |
251 | |||
252 | hs = hash_table_get_instance(item, hashed_service_t, link); |
||
253 | |||
1336 | jermar | 254 | if (keys == 2) |
255 | return key[1] == hs->in_phone_hash; |
||
256 | else |
||
257 | return key[0] == hs->service; |
||
1335 | jermar | 258 | } |
1336 | jermar | 259 | |
260 | /** Perform actions after removal of item from the hash table. |
||
261 | * |
||
262 | * @param item Item that was removed from the hash table. |
||
263 | */ |
||
264 | void ns_remove(link_t *item) |
||
265 | { |
||
266 | assert(item); |
||
267 | free(hash_table_get_instance(item, hashed_service_t, link)); |
||
268 | } |
||
1719 | decky | 269 | |
1649 | cejka | 270 | /** |
271 | * @} |
||
272 | */ |