Rev 1418 | Rev 1462 | Go to most recent revision | 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 | |||
29 | /** |
||
30 | * @file ns.c |
||
31 | * @brief Naming service for HelenOS IPC. |
||
32 | */ |
||
33 | |||
1353 | jermar | 34 | #include <ipc/ipc.h> |
35 | #include <ipc/ns.h> |
||
1030 | palkovsky | 36 | #include <stdio.h> |
37 | #include <unistd.h> |
||
38 | #include <stdlib.h> |
||
39 | #include <errno.h> |
||
1335 | jermar | 40 | #include <assert.h> |
41 | #include <libadt/list.h> |
||
42 | #include <libadt/hash_table.h> |
||
1435 | palkovsky | 43 | #include <sysinfo.h> |
44 | #include <ddi.h> |
||
45 | #include <as.h> |
||
1335 | jermar | 46 | |
47 | #define NAME "NS" |
||
48 | |||
49 | #define NS_HASH_TABLE_CHAINS 20 |
||
50 | |||
1336 | jermar | 51 | static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call); |
52 | static int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid); |
||
53 | |||
1335 | jermar | 54 | /* Static functions implementing NS hash table operations. */ |
55 | static hash_index_t ns_hash(unsigned long *key); |
||
56 | static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item); |
||
1336 | jermar | 57 | static void ns_remove(link_t *item); |
1335 | jermar | 58 | |
59 | /** Operations for NS hash table. */ |
||
60 | static hash_table_operations_t ns_hash_table_ops = { |
||
61 | .hash = ns_hash, |
||
62 | .compare = ns_compare, |
||
1336 | jermar | 63 | .remove_callback = ns_remove |
1335 | jermar | 64 | }; |
65 | |||
66 | /** NS hash table structure. */ |
||
67 | static hash_table_t ns_hash_table; |
||
68 | |||
69 | /** NS hash table item. */ |
||
70 | typedef struct { |
||
71 | link_t link; |
||
72 | ipcarg_t service; /**< Number of the service. */ |
||
73 | ipcarg_t phone; /**< Phone registered with the service. */ |
||
1336 | jermar | 74 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
1335 | jermar | 75 | } hashed_service_t; |
76 | |||
77 | int static ping_phone; |
||
78 | |||
1435 | palkovsky | 79 | static void get_realtime_as(ipc_callid_t callid, ipc_call_t *call) |
80 | { |
||
81 | static void *addr = NULL; |
||
82 | void *ph_addr; |
||
83 | |||
84 | if (IPC_GET_ARG3(*call) != (AS_AREA_READ | AS_AREA_CACHEABLE)) { |
||
85 | ipc_answer_fast(callid, EPERM, 0, 0); |
||
86 | return; |
||
87 | } |
||
88 | if (!addr) { |
||
89 | ph_addr = (void *)sysinfo_value("clock.faddr"); |
||
90 | if (!ph_addr) { |
||
91 | ipc_answer_fast(callid, ENOENT, 0, 0); |
||
92 | return; |
||
93 | } |
||
94 | addr = (void *)(200*1024*1024); /* TODO: intelligent freemem space */ |
||
95 | map_physmem(task_get_id(), ph_addr, addr, 1, |
||
96 | AS_AREA_READ | AS_AREA_CACHEABLE); |
||
97 | } |
||
98 | ipc_answer_fast(callid, 0, (ipcarg_t)addr, 0); |
||
99 | } |
||
100 | |||
1030 | palkovsky | 101 | int main(int argc, char **argv) |
102 | { |
||
103 | ipc_call_t call; |
||
104 | ipc_callid_t callid; |
||
1360 | jermar | 105 | char *as_area; |
1030 | palkovsky | 106 | |
107 | ipcarg_t retval, arg1, arg2; |
||
108 | |||
1363 | vana | 109 | // printf("%s: Naming service started.\n", NAME); |
1335 | jermar | 110 | |
1336 | jermar | 111 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, &ns_hash_table_ops)) { |
1363 | vana | 112 | // printf("%s: cannot create hash table\n", NAME); |
1335 | jermar | 113 | return ENOMEM; |
114 | } |
||
1343 | jermar | 115 | |
1030 | palkovsky | 116 | while (1) { |
1365 | jermar | 117 | callid = ipc_wait_for_call(&call); |
1343 | jermar | 118 | // printf("NS: Call in_phone_hash=%lX...", call.in_phone_hash); |
1091 | palkovsky | 119 | switch (IPC_GET_METHOD(call)) { |
1360 | jermar | 120 | case IPC_M_AS_AREA_SEND: |
1418 | jermar | 121 | as_area = (char *)IPC_GET_ARG1(call); |
122 | // printf("Received as_area: %P, size:%d\n", as_area, IPC_GET_ARG2(call)); |
||
1343 | jermar | 123 | retval = ipc_answer_fast(callid, 0,(sysarg_t)(1024*1024), 0); |
1330 | palkovsky | 124 | if (!retval) { |
1363 | vana | 125 | // printf("Reading shared memory..."); |
126 | // printf("Text: %s", as_area); |
||
1330 | palkovsky | 127 | } else |
1363 | vana | 128 | // printf("Failed answer: %d\n", retval); |
1330 | palkovsky | 129 | continue; |
1435 | palkovsky | 130 | case IPC_M_AS_AREA_RECV: |
131 | get_realtime_as(callid, &call); |
||
132 | continue; |
||
1259 | palkovsky | 133 | case IPC_M_INTERRUPT: |
134 | break; |
||
1089 | palkovsky | 135 | case IPC_M_PHONE_HUNGUP: |
136 | retval = 0; |
||
137 | break; |
||
1336 | jermar | 138 | case IPC_M_CONNECT_TO_ME: |
1335 | jermar | 139 | /* |
1336 | jermar | 140 | * Server requests service registration. |
1335 | jermar | 141 | */ |
1336 | jermar | 142 | retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG3(call), &call); |
143 | ping_phone = IPC_GET_ARG3(call); |
||
1030 | palkovsky | 144 | break; |
1089 | palkovsky | 145 | case IPC_M_CONNECT_ME_TO: |
1336 | jermar | 146 | /* |
147 | * Client requests to be connected to a service. |
||
148 | */ |
||
149 | retval = connect_to_service(IPC_GET_ARG1(call), &call, callid); |
||
1061 | palkovsky | 150 | break; |
1338 | jermar | 151 | case NS_HANGUP: |
1363 | vana | 152 | // printf("Closing connection.\n"); |
1338 | jermar | 153 | retval = EHANGUP; |
154 | break; |
||
1030 | palkovsky | 155 | case NS_PING: |
1363 | vana | 156 | // printf("Ping...%P %P\n", IPC_GET_ARG1(call), |
157 | // IPC_GET_ARG2(call)); |
||
1030 | palkovsky | 158 | retval = 0; |
159 | arg1 = 0xdead; |
||
160 | arg2 = 0xbeef; |
||
161 | break; |
||
162 | case NS_PING_SVC: |
||
1363 | vana | 163 | // printf("NS:Pinging service %d\n", ping_phone); |
1335 | jermar | 164 | ipc_call_sync(ping_phone, NS_PING, 0xbeef, 0); |
1363 | vana | 165 | // printf("NS:Got pong\n"); |
1030 | palkovsky | 166 | break; |
167 | default: |
||
1363 | vana | 168 | // printf("Unknown method: %zd\n", IPC_GET_METHOD(call)); |
1030 | palkovsky | 169 | retval = ENOENT; |
170 | break; |
||
171 | } |
||
1282 | palkovsky | 172 | if (! (callid & IPC_CALLID_NOTIFICATION)) { |
1343 | jermar | 173 | // printf("Answering.\n"); |
174 | ipc_answer_fast(callid, retval, arg1, arg2); |
||
1282 | palkovsky | 175 | } |
1030 | palkovsky | 176 | } |
177 | } |
||
1335 | jermar | 178 | |
1338 | jermar | 179 | /** Register service. |
1336 | jermar | 180 | * |
181 | * @param service Service to be registered. |
||
182 | * @param phone phone Phone to be used for connections to the service. |
||
183 | * @param call Pointer to call structure. |
||
184 | * |
||
185 | * @return Zero on success or a value from @ref errno.h. |
||
186 | */ |
||
187 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call) |
||
188 | { |
||
189 | unsigned long keys[3] = { service, call->in_phone_hash, 0 }; |
||
190 | hashed_service_t *hs; |
||
191 | |||
1363 | vana | 192 | // printf("Registering service %d on phone %d...", service, phone); |
1336 | jermar | 193 | |
194 | if (hash_table_find(&ns_hash_table, keys)) { |
||
1363 | vana | 195 | // printf("Service %d already registered.\n", service); |
1336 | jermar | 196 | return EEXISTS; |
197 | } |
||
198 | |||
199 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
||
200 | if (!hs) { |
||
1363 | vana | 201 | // printf("Failed to register service %d.\n", service); |
1336 | jermar | 202 | return ENOMEM; |
203 | } |
||
204 | |||
205 | link_initialize(&hs->link); |
||
206 | hs->service = service; |
||
207 | hs->phone = phone; |
||
208 | hs->in_phone_hash = call->in_phone_hash; |
||
209 | hash_table_insert(&ns_hash_table, keys, &hs->link); |
||
210 | |||
211 | return 0; |
||
212 | } |
||
213 | |||
214 | /** Connect client to service. |
||
215 | * |
||
216 | * @param service Service to be connected to. |
||
217 | * @param call Pointer to call structure. |
||
218 | * @param callid Call ID of the request. |
||
219 | * |
||
220 | * @return Zero on success or a value from @ref errno.h. |
||
221 | */ |
||
222 | int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid) |
||
223 | { |
||
224 | unsigned long keys[3] = { service, 0, 0 }; |
||
225 | link_t *hlp; |
||
226 | hashed_service_t *hs; |
||
227 | |||
228 | hlp = hash_table_find(&ns_hash_table, keys); |
||
229 | if (!hlp) { |
||
1343 | jermar | 230 | // printf("Service %d not registered.\n", service); |
1336 | jermar | 231 | return ENOENT; |
232 | } |
||
233 | hs = hash_table_get_instance(hlp, hashed_service_t, link); |
||
1363 | vana | 234 | // printf("Connecting in_phone_hash=%lX to service at phone %d...", call->in_phone_hash, hs->phone); |
1336 | jermar | 235 | return ipc_forward_fast(callid, hs->phone, 0, 0); |
236 | } |
||
237 | |||
1335 | jermar | 238 | /** Compute hash index into NS hash table. |
239 | * |
||
1336 | jermar | 240 | * @param key Pointer keys. However, only the first key (i.e. service number) |
241 | * is used to compute the hash index. |
||
242 | * @return Hash index corresponding to key[0]. |
||
1335 | jermar | 243 | */ |
244 | hash_index_t ns_hash(unsigned long *key) |
||
245 | { |
||
246 | assert(key); |
||
247 | return *key % NS_HASH_TABLE_CHAINS; |
||
248 | } |
||
249 | |||
250 | /** Compare a key with hashed item. |
||
251 | * |
||
1336 | jermar | 252 | * This compare function always ignores the third key. |
253 | * It exists only to make it possible to remove records |
||
254 | * originating from connection with key[1] in_phone_hash |
||
255 | * value. Note that this is close to being classified |
||
256 | * as a nasty hack. |
||
257 | * |
||
258 | * @param key Array of keys. |
||
259 | * @param keys Must be lesser or equal to 3. |
||
1335 | jermar | 260 | * @param item Pointer to a hash table item. |
261 | * @return Non-zero if the key matches the item, zero otherwise. |
||
262 | */ |
||
1336 | jermar | 263 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item) |
1335 | jermar | 264 | { |
265 | hashed_service_t *hs; |
||
266 | |||
267 | assert(key); |
||
1336 | jermar | 268 | assert(keys <= 3); |
1335 | jermar | 269 | assert(item); |
270 | |||
271 | hs = hash_table_get_instance(item, hashed_service_t, link); |
||
272 | |||
1336 | jermar | 273 | if (keys == 2) |
274 | return key[1] == hs->in_phone_hash; |
||
275 | else |
||
276 | return key[0] == hs->service; |
||
1335 | jermar | 277 | } |
1336 | jermar | 278 | |
279 | /** Perform actions after removal of item from the hash table. |
||
280 | * |
||
281 | * @param item Item that was removed from the hash table. |
||
282 | */ |
||
283 | void ns_remove(link_t *item) |
||
284 | { |
||
285 | assert(item); |
||
286 | free(hash_table_get_instance(item, hashed_service_t, link)); |
||
287 | } |