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