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