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