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