Rev 1493 | Rev 1531 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1493 | Rev 1501 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** |
29 | /** |
30 | * @file ns.c |
30 | * @file ns.c |
31 | * @brief Naming service for HelenOS IPC. |
31 | * @brief Naming service for HelenOS IPC. |
32 | */ |
32 | */ |
33 | 33 | ||
34 | #include <ipc/ipc.h> |
34 | #include <ipc/ipc.h> |
35 | #include <ipc/ns.h> |
35 | #include <ipc/ns.h> |
36 | #include <stdio.h> |
36 | #include <stdio.h> |
37 | #include <unistd.h> |
37 | #include <unistd.h> |
38 | #include <stdlib.h> |
38 | #include <stdlib.h> |
39 | #include <errno.h> |
39 | #include <errno.h> |
40 | #include <assert.h> |
40 | #include <assert.h> |
41 | #include <libadt/list.h> |
41 | #include <libadt/list.h> |
42 | #include <libadt/hash_table.h> |
42 | #include <libadt/hash_table.h> |
43 | #include <sysinfo.h> |
43 | #include <sysinfo.h> |
44 | #include <ddi.h> |
44 | #include <ddi.h> |
45 | #include <as.h> |
45 | #include <as.h> |
46 | 46 | ||
47 | #define NAME "NS" |
47 | #define NAME "NS" |
48 | 48 | ||
49 | #define NS_HASH_TABLE_CHAINS 20 |
49 | #define NS_HASH_TABLE_CHAINS 20 |
50 | 50 | ||
51 | static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call); |
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); |
52 | static int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid); |
53 | 53 | ||
54 | /* Static functions implementing NS hash table operations. */ |
54 | /* Static functions implementing NS hash table operations. */ |
55 | static hash_index_t ns_hash(unsigned long *key); |
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); |
56 | static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item); |
57 | static void ns_remove(link_t *item); |
57 | static void ns_remove(link_t *item); |
58 | 58 | ||
59 | /** Operations for NS hash table. */ |
59 | /** Operations for NS hash table. */ |
60 | static hash_table_operations_t ns_hash_table_ops = { |
60 | static hash_table_operations_t ns_hash_table_ops = { |
61 | .hash = ns_hash, |
61 | .hash = ns_hash, |
62 | .compare = ns_compare, |
62 | .compare = ns_compare, |
63 | .remove_callback = ns_remove |
63 | .remove_callback = ns_remove |
64 | }; |
64 | }; |
65 | 65 | ||
66 | /** NS hash table structure. */ |
66 | /** NS hash table structure. */ |
67 | static hash_table_t ns_hash_table; |
67 | static hash_table_t ns_hash_table; |
68 | 68 | ||
69 | /** NS hash table item. */ |
69 | /** NS hash table item. */ |
70 | typedef struct { |
70 | typedef struct { |
71 | link_t link; |
71 | link_t link; |
72 | ipcarg_t service; /**< Number of the service. */ |
72 | ipcarg_t service; /**< Number of the service. */ |
73 | ipcarg_t phone; /**< Phone registered with the service. */ |
73 | ipcarg_t phone; /**< Phone registered with the service. */ |
74 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
74 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
75 | } hashed_service_t; |
75 | } hashed_service_t; |
76 | 76 | ||
77 | int static ping_phone; |
77 | int static ping_phone; |
78 | 78 | ||
79 | static void get_realtime_as(ipc_callid_t callid, ipc_call_t *call) |
79 | static void get_realtime_as(ipc_callid_t callid, ipc_call_t *call) |
80 | { |
80 | { |
81 | static void *addr = NULL; |
81 | static void *addr = NULL; |
82 | void *ph_addr; |
82 | void *ph_addr; |
83 | 83 | ||
84 | if (!addr) { |
84 | if (!addr) { |
85 | ph_addr = (void *)sysinfo_value("clock.faddr"); |
85 | ph_addr = (void *)sysinfo_value("clock.faddr"); |
86 | if (!ph_addr) { |
86 | if (!ph_addr) { |
87 | ipc_answer_fast(callid, ENOENT, 0, 0); |
87 | ipc_answer_fast(callid, ENOENT, 0, 0); |
88 | return; |
88 | return; |
89 | } |
89 | } |
90 | addr = (void *)(200*1024*1024); /* TODO: intelligent freemem space */ |
90 | addr = as_get_mappable_page(PAGE_SIZE); |
91 | map_physmem(ph_addr, addr, 1, AS_AREA_READ | AS_AREA_CACHEABLE); |
91 | map_physmem(ph_addr, addr, 1, AS_AREA_READ | AS_AREA_CACHEABLE); |
92 | } |
92 | } |
93 | ipc_answer_fast(callid, 0, (ipcarg_t)addr, AS_AREA_READ | AS_AREA_CACHEABLE); |
93 | ipc_answer_fast(callid, 0, (ipcarg_t)addr, AS_AREA_READ | AS_AREA_CACHEABLE); |
94 | } |
94 | } |
95 | 95 | ||
96 | int main(int argc, char **argv) |
96 | int main(int argc, char **argv) |
97 | { |
97 | { |
98 | ipc_call_t call; |
98 | ipc_call_t call; |
99 | ipc_callid_t callid; |
99 | ipc_callid_t callid; |
100 | char *as_area; |
100 | char *as_area; |
101 | 101 | ||
102 | ipcarg_t retval, arg1, arg2; |
102 | ipcarg_t retval, arg1, arg2; |
103 | 103 | ||
104 | // printf("%s: Naming service started.\n", NAME); |
104 | // printf("%s: Naming service started.\n", NAME); |
105 | 105 | ||
106 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, &ns_hash_table_ops)) { |
106 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, &ns_hash_table_ops)) { |
107 | // printf("%s: cannot create hash table\n", NAME); |
107 | // printf("%s: cannot create hash table\n", NAME); |
108 | return ENOMEM; |
108 | return ENOMEM; |
109 | } |
109 | } |
110 | 110 | ||
111 | while (1) { |
111 | while (1) { |
112 | callid = ipc_wait_for_call(&call); |
112 | callid = ipc_wait_for_call(&call); |
113 | // printf("NS: Call in_phone_hash=%lX...", call.in_phone_hash); |
113 | // printf("NS: Call in_phone_hash=%lX...", call.in_phone_hash); |
114 | switch (IPC_GET_METHOD(call)) { |
114 | switch (IPC_GET_METHOD(call)) { |
115 | case IPC_M_AS_AREA_SEND: |
115 | case IPC_M_AS_AREA_SEND: |
116 | as_area = (char *)IPC_GET_ARG1(call); |
116 | as_area = (char *)IPC_GET_ARG1(call); |
117 | // printf("Received as_area: %P, size:%d\n", as_area, IPC_GET_ARG2(call)); |
117 | // printf("Received as_area: %P, size:%d\n", as_area, IPC_GET_ARG2(call)); |
118 | retval = ipc_answer_fast(callid, 0,(sysarg_t)(1024*1024), 0); |
118 | retval = ipc_answer_fast(callid, 0,(sysarg_t)(1024*1024), 0); |
119 | if (!retval) { |
119 | if (!retval) { |
120 | // printf("Reading shared memory..."); |
120 | // printf("Reading shared memory..."); |
121 | // printf("Text: %s", as_area); |
121 | // printf("Text: %s", as_area); |
122 | } else |
122 | } else |
123 | // printf("Failed answer: %d\n", retval); |
123 | // printf("Failed answer: %d\n", retval); |
124 | continue; |
124 | continue; |
125 | case IPC_M_AS_AREA_RECV: |
125 | case IPC_M_AS_AREA_RECV: |
126 | get_realtime_as(callid, &call); |
126 | get_realtime_as(callid, &call); |
127 | continue; |
127 | continue; |
128 | case IPC_M_INTERRUPT: |
128 | case IPC_M_INTERRUPT: |
129 | break; |
129 | break; |
130 | case IPC_M_PHONE_HUNGUP: |
130 | case IPC_M_PHONE_HUNGUP: |
131 | retval = 0; |
131 | retval = 0; |
132 | break; |
132 | break; |
133 | case IPC_M_CONNECT_TO_ME: |
133 | case IPC_M_CONNECT_TO_ME: |
134 | /* |
134 | /* |
135 | * Server requests service registration. |
135 | * Server requests service registration. |
136 | */ |
136 | */ |
137 | retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG3(call), &call); |
137 | retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG3(call), &call); |
138 | ping_phone = IPC_GET_ARG3(call); |
138 | ping_phone = IPC_GET_ARG3(call); |
139 | break; |
139 | break; |
140 | case IPC_M_CONNECT_ME_TO: |
140 | case IPC_M_CONNECT_ME_TO: |
141 | /* |
141 | /* |
142 | * Client requests to be connected to a service. |
142 | * Client requests to be connected to a service. |
143 | */ |
143 | */ |
144 | retval = connect_to_service(IPC_GET_ARG1(call), &call, callid); |
144 | retval = connect_to_service(IPC_GET_ARG1(call), &call, callid); |
145 | break; |
145 | break; |
146 | case NS_HANGUP: |
146 | case NS_HANGUP: |
147 | // printf("Closing connection.\n"); |
147 | // printf("Closing connection.\n"); |
148 | retval = EHANGUP; |
148 | retval = EHANGUP; |
149 | break; |
149 | break; |
150 | case NS_PING: |
150 | case NS_PING: |
151 | // printf("Ping...%P %P\n", IPC_GET_ARG1(call), |
151 | // printf("Ping...%P %P\n", IPC_GET_ARG1(call), |
152 | // IPC_GET_ARG2(call)); |
152 | // IPC_GET_ARG2(call)); |
153 | retval = 0; |
153 | retval = 0; |
154 | arg1 = 0xdead; |
154 | arg1 = 0xdead; |
155 | arg2 = 0xbeef; |
155 | arg2 = 0xbeef; |
156 | break; |
156 | break; |
157 | case NS_PING_SVC: |
157 | case NS_PING_SVC: |
158 | // printf("NS:Pinging service %d\n", ping_phone); |
158 | // printf("NS:Pinging service %d\n", ping_phone); |
159 | ipc_call_sync(ping_phone, NS_PING, 0xbeef, 0); |
159 | ipc_call_sync(ping_phone, NS_PING, 0xbeef, 0); |
160 | // printf("NS:Got pong\n"); |
160 | // printf("NS:Got pong\n"); |
161 | break; |
161 | break; |
162 | default: |
162 | default: |
163 | // printf("Unknown method: %zd\n", IPC_GET_METHOD(call)); |
163 | // printf("Unknown method: %zd\n", IPC_GET_METHOD(call)); |
164 | retval = ENOENT; |
164 | retval = ENOENT; |
165 | break; |
165 | break; |
166 | } |
166 | } |
167 | if (! (callid & IPC_CALLID_NOTIFICATION)) { |
167 | if (! (callid & IPC_CALLID_NOTIFICATION)) { |
168 | // printf("Answering.\n"); |
168 | // printf("Answering.\n"); |
169 | ipc_answer_fast(callid, retval, arg1, arg2); |
169 | ipc_answer_fast(callid, retval, arg1, arg2); |
170 | } |
170 | } |
171 | } |
171 | } |
172 | } |
172 | } |
173 | 173 | ||
174 | /** Register service. |
174 | /** Register service. |
175 | * |
175 | * |
176 | * @param service Service to be registered. |
176 | * @param service Service to be registered. |
177 | * @param phone phone Phone to be used for connections to the service. |
177 | * @param phone phone Phone to be used for connections to the service. |
178 | * @param call Pointer to call structure. |
178 | * @param call Pointer to call structure. |
179 | * |
179 | * |
180 | * @return Zero on success or a value from @ref errno.h. |
180 | * @return Zero on success or a value from @ref errno.h. |
181 | */ |
181 | */ |
182 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call) |
182 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call) |
183 | { |
183 | { |
184 | unsigned long keys[3] = { service, call->in_phone_hash, 0 }; |
184 | unsigned long keys[3] = { service, call->in_phone_hash, 0 }; |
185 | hashed_service_t *hs; |
185 | hashed_service_t *hs; |
186 | 186 | ||
187 | // printf("Registering service %d on phone %d...", service, phone); |
187 | // printf("Registering service %d on phone %d...", service, phone); |
188 | 188 | ||
189 | if (hash_table_find(&ns_hash_table, keys)) { |
189 | if (hash_table_find(&ns_hash_table, keys)) { |
190 | // printf("Service %d already registered.\n", service); |
190 | // printf("Service %d already registered.\n", service); |
191 | return EEXISTS; |
191 | return EEXISTS; |
192 | } |
192 | } |
193 | 193 | ||
194 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
194 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
195 | if (!hs) { |
195 | if (!hs) { |
196 | // printf("Failed to register service %d.\n", service); |
196 | // printf("Failed to register service %d.\n", service); |
197 | return ENOMEM; |
197 | return ENOMEM; |
198 | } |
198 | } |
199 | 199 | ||
200 | link_initialize(&hs->link); |
200 | link_initialize(&hs->link); |
201 | hs->service = service; |
201 | hs->service = service; |
202 | hs->phone = phone; |
202 | hs->phone = phone; |
203 | hs->in_phone_hash = call->in_phone_hash; |
203 | hs->in_phone_hash = call->in_phone_hash; |
204 | hash_table_insert(&ns_hash_table, keys, &hs->link); |
204 | hash_table_insert(&ns_hash_table, keys, &hs->link); |
205 | 205 | ||
206 | return 0; |
206 | return 0; |
207 | } |
207 | } |
208 | 208 | ||
209 | /** Connect client to service. |
209 | /** Connect client to service. |
210 | * |
210 | * |
211 | * @param service Service to be connected to. |
211 | * @param service Service to be connected to. |
212 | * @param call Pointer to call structure. |
212 | * @param call Pointer to call structure. |
213 | * @param callid Call ID of the request. |
213 | * @param callid Call ID of the request. |
214 | * |
214 | * |
215 | * @return Zero on success or a value from @ref errno.h. |
215 | * @return Zero on success or a value from @ref errno.h. |
216 | */ |
216 | */ |
217 | int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid) |
217 | int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid) |
218 | { |
218 | { |
219 | unsigned long keys[3] = { service, 0, 0 }; |
219 | unsigned long keys[3] = { service, 0, 0 }; |
220 | link_t *hlp; |
220 | link_t *hlp; |
221 | hashed_service_t *hs; |
221 | hashed_service_t *hs; |
222 | 222 | ||
223 | hlp = hash_table_find(&ns_hash_table, keys); |
223 | hlp = hash_table_find(&ns_hash_table, keys); |
224 | if (!hlp) { |
224 | if (!hlp) { |
225 | // printf("Service %d not registered.\n", service); |
225 | // printf("Service %d not registered.\n", service); |
226 | return ENOENT; |
226 | return ENOENT; |
227 | } |
227 | } |
228 | hs = hash_table_get_instance(hlp, hashed_service_t, link); |
228 | hs = hash_table_get_instance(hlp, hashed_service_t, link); |
229 | // printf("Connecting in_phone_hash=%lX to service at phone %d...", call->in_phone_hash, hs->phone); |
229 | // printf("Connecting in_phone_hash=%lX to service at phone %d...", call->in_phone_hash, hs->phone); |
230 | return ipc_forward_fast(callid, hs->phone, 0, 0); |
230 | return ipc_forward_fast(callid, hs->phone, 0, 0); |
231 | } |
231 | } |
232 | 232 | ||
233 | /** Compute hash index into NS hash table. |
233 | /** Compute hash index into NS hash table. |
234 | * |
234 | * |
235 | * @param key Pointer keys. However, only the first key (i.e. service number) |
235 | * @param key Pointer keys. However, only the first key (i.e. service number) |
236 | * is used to compute the hash index. |
236 | * is used to compute the hash index. |
237 | * @return Hash index corresponding to key[0]. |
237 | * @return Hash index corresponding to key[0]. |
238 | */ |
238 | */ |
239 | hash_index_t ns_hash(unsigned long *key) |
239 | hash_index_t ns_hash(unsigned long *key) |
240 | { |
240 | { |
241 | assert(key); |
241 | assert(key); |
242 | return *key % NS_HASH_TABLE_CHAINS; |
242 | return *key % NS_HASH_TABLE_CHAINS; |
243 | } |
243 | } |
244 | 244 | ||
245 | /** Compare a key with hashed item. |
245 | /** Compare a key with hashed item. |
246 | * |
246 | * |
247 | * This compare function always ignores the third key. |
247 | * This compare function always ignores the third key. |
248 | * It exists only to make it possible to remove records |
248 | * It exists only to make it possible to remove records |
249 | * originating from connection with key[1] in_phone_hash |
249 | * originating from connection with key[1] in_phone_hash |
250 | * value. Note that this is close to being classified |
250 | * value. Note that this is close to being classified |
251 | * as a nasty hack. |
251 | * as a nasty hack. |
252 | * |
252 | * |
253 | * @param key Array of keys. |
253 | * @param key Array of keys. |
254 | * @param keys Must be lesser or equal to 3. |
254 | * @param keys Must be lesser or equal to 3. |
255 | * @param item Pointer to a hash table item. |
255 | * @param item Pointer to a hash table item. |
256 | * @return Non-zero if the key matches the item, zero otherwise. |
256 | * @return Non-zero if the key matches the item, zero otherwise. |
257 | */ |
257 | */ |
258 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item) |
258 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item) |
259 | { |
259 | { |
260 | hashed_service_t *hs; |
260 | hashed_service_t *hs; |
261 | 261 | ||
262 | assert(key); |
262 | assert(key); |
263 | assert(keys <= 3); |
263 | assert(keys <= 3); |
264 | assert(item); |
264 | assert(item); |
265 | 265 | ||
266 | hs = hash_table_get_instance(item, hashed_service_t, link); |
266 | hs = hash_table_get_instance(item, hashed_service_t, link); |
267 | 267 | ||
268 | if (keys == 2) |
268 | if (keys == 2) |
269 | return key[1] == hs->in_phone_hash; |
269 | return key[1] == hs->in_phone_hash; |
270 | else |
270 | else |
271 | return key[0] == hs->service; |
271 | return key[0] == hs->service; |
272 | } |
272 | } |
273 | 273 | ||
274 | /** Perform actions after removal of item from the hash table. |
274 | /** Perform actions after removal of item from the hash table. |
275 | * |
275 | * |
276 | * @param item Item that was removed from the hash table. |
276 | * @param item Item that was removed from the hash table. |
277 | */ |
277 | */ |
278 | void ns_remove(link_t *item) |
278 | void ns_remove(link_t *item) |
279 | { |
279 | { |
280 | assert(item); |
280 | assert(item); |
281 | free(hash_table_get_instance(item, hashed_service_t, link)); |
281 | free(hash_table_get_instance(item, hashed_service_t, link)); |
282 | } |
282 | } |
283 | 283 |