Rev 3896 | Rev 3908 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3896 | Rev 3904 | ||
---|---|---|---|
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 <bool.h> |
43 | #include <bool.h> |
44 | #include <unistd.h> |
44 | #include <unistd.h> |
45 | #include <stdlib.h> |
45 | #include <stdlib.h> |
46 | #include <errno.h> |
46 | #include <errno.h> |
47 | #include <assert.h> |
47 | #include <assert.h> |
48 | #include <libadt/list.h> |
48 | #include <libadt/list.h> |
49 | #include <libadt/hash_table.h> |
49 | #include <libadt/hash_table.h> |
50 | #include <sysinfo.h> |
50 | #include <sysinfo.h> |
51 | #include <loader/loader.h> |
51 | #include <loader/loader.h> |
52 | #include <ddi.h> |
52 | #include <ddi.h> |
53 | #include <as.h> |
53 | #include <as.h> |
54 | 54 | ||
55 | #define NAME "ns" |
55 | #define NAME "ns" |
56 | 56 | ||
57 | #define NS_HASH_TABLE_CHAINS 20 |
57 | #define NS_HASH_TABLE_CHAINS 20 |
58 | 58 | ||
59 | static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call); |
59 | static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call); |
60 | static int connect_to_service(ipcarg_t service, ipc_call_t *call, |
60 | static int connect_to_service(ipcarg_t service, ipc_call_t *call, |
61 | ipc_callid_t callid); |
61 | ipc_callid_t callid); |
62 | 62 | ||
63 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
63 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
64 | ipc_callid_t callid); |
64 | ipc_callid_t callid); |
65 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
65 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
66 | ipc_callid_t callid); |
66 | ipc_callid_t callid); |
67 | 67 | ||
68 | /* Static functions implementing NS hash table operations. */ |
68 | /* Static functions implementing NS hash table operations. */ |
69 | static hash_index_t ns_hash(unsigned long *key); |
69 | static hash_index_t ns_hash(unsigned long *key); |
70 | static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item); |
70 | static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item); |
71 | static void ns_remove(link_t *item); |
71 | static void ns_remove(link_t *item); |
72 | 72 | ||
73 | /** Operations for NS hash table. */ |
73 | /** Operations for NS hash table. */ |
74 | static hash_table_operations_t ns_hash_table_ops = { |
74 | static hash_table_operations_t ns_hash_table_ops = { |
75 | .hash = ns_hash, |
75 | .hash = ns_hash, |
76 | .compare = ns_compare, |
76 | .compare = ns_compare, |
77 | .remove_callback = ns_remove |
77 | .remove_callback = ns_remove |
78 | }; |
78 | }; |
79 | 79 | ||
80 | /** NS hash table structure. */ |
80 | /** NS hash table structure. */ |
81 | static hash_table_t ns_hash_table; |
81 | static hash_table_t ns_hash_table; |
82 | 82 | ||
83 | /** NS hash table item. */ |
83 | /** NS hash table item. */ |
84 | typedef struct { |
84 | typedef struct { |
85 | link_t link; |
85 | link_t link; |
86 | ipcarg_t service; /**< Number of the service. */ |
86 | ipcarg_t service; /**< Number of the service. */ |
87 | ipcarg_t phone; /**< Phone registered with the service. */ |
87 | ipcarg_t phone; /**< Phone registered with the service. */ |
88 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
88 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
89 | } hashed_service_t; |
89 | } hashed_service_t; |
90 | 90 | ||
91 | static void *clockaddr = NULL; |
91 | static void *clockaddr = NULL; |
92 | static void *klogaddr = NULL; |
92 | static void *klogaddr = NULL; |
93 | 93 | ||
94 | /** Request for connection to a clonable service. */ |
94 | /** Request for connection to a clonable service. */ |
95 | typedef struct { |
95 | typedef struct { |
96 | link_t link; |
96 | link_t link; |
97 | ipcarg_t service; |
97 | ipcarg_t service; |
98 | ipc_call_t *call; |
98 | ipc_call_t call; |
99 | ipc_callid_t callid; |
99 | ipc_callid_t callid; |
100 | } cs_req_t; |
100 | } cs_req_t; |
101 | 101 | ||
102 | /** List of clonable-service connection requests. */ |
102 | /** List of clonable-service connection requests. */ |
103 | static link_t cs_req; |
103 | static link_t cs_req; |
104 | 104 | ||
105 | /** Return true if @a service is clonable. */ |
105 | /** Return true if @a service is clonable. */ |
106 | static bool service_clonable(int service) |
106 | static bool service_clonable(int service) |
107 | { |
107 | { |
108 | return service == SERVICE_LOAD; |
108 | return service == SERVICE_LOAD; |
109 | } |
109 | } |
110 | 110 | ||
111 | static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name, |
111 | static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name, |
112 | void **addr) |
112 | void **addr) |
113 | { |
113 | { |
114 | void *ph_addr; |
114 | void *ph_addr; |
115 | 115 | ||
116 | if (!*addr) { |
116 | if (!*addr) { |
117 | ph_addr = (void *) sysinfo_value(name); |
117 | ph_addr = (void *) sysinfo_value(name); |
118 | if (!ph_addr) { |
118 | if (!ph_addr) { |
119 | ipc_answer_0(callid, ENOENT); |
119 | ipc_answer_0(callid, ENOENT); |
120 | return; |
120 | return; |
121 | } |
121 | } |
122 | *addr = as_get_mappable_page(PAGE_SIZE); |
122 | *addr = as_get_mappable_page(PAGE_SIZE); |
123 | physmem_map(ph_addr, *addr, 1, |
123 | physmem_map(ph_addr, *addr, 1, |
124 | AS_AREA_READ | AS_AREA_CACHEABLE); |
124 | AS_AREA_READ | AS_AREA_CACHEABLE); |
125 | } |
125 | } |
126 | ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ); |
126 | ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ); |
127 | } |
127 | } |
128 | 128 | ||
129 | int main(int argc, char **argv) |
129 | int main(int argc, char **argv) |
130 | { |
130 | { |
131 | printf(NAME ": HelenOS IPC Naming Service\n"); |
131 | printf(NAME ": HelenOS IPC Naming Service\n"); |
132 | 132 | ||
133 | ipc_call_t call; |
133 | ipc_call_t call; |
134 | ipc_callid_t callid; |
134 | ipc_callid_t callid; |
135 | 135 | ||
136 | ipcarg_t retval; |
136 | ipcarg_t retval; |
137 | 137 | ||
138 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, |
138 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, |
139 | &ns_hash_table_ops)) { |
139 | &ns_hash_table_ops)) { |
140 | printf(NAME ": No memory available\n"); |
140 | printf(NAME ": No memory available\n"); |
141 | return ENOMEM; |
141 | return ENOMEM; |
142 | } |
142 | } |
143 | 143 | ||
144 | list_initialize(&cs_req); |
144 | list_initialize(&cs_req); |
145 | 145 | ||
146 | printf(NAME ": Accepting connections\n"); |
146 | printf(NAME ": Accepting connections\n"); |
147 | while (1) { |
147 | while (1) { |
148 | callid = ipc_wait_for_call(&call); |
148 | callid = ipc_wait_for_call(&call); |
149 | switch (IPC_GET_METHOD(call)) { |
149 | switch (IPC_GET_METHOD(call)) { |
150 | case IPC_M_SHARE_IN: |
150 | case IPC_M_SHARE_IN: |
151 | switch (IPC_GET_ARG3(call)) { |
151 | switch (IPC_GET_ARG3(call)) { |
152 | case SERVICE_MEM_REALTIME: |
152 | case SERVICE_MEM_REALTIME: |
153 | get_as_area(callid, &call, "clock.faddr", |
153 | get_as_area(callid, &call, "clock.faddr", |
154 | &clockaddr); |
154 | &clockaddr); |
155 | break; |
155 | break; |
156 | case SERVICE_MEM_KLOG: |
156 | case SERVICE_MEM_KLOG: |
157 | get_as_area(callid, &call, "klog.faddr", |
157 | get_as_area(callid, &call, "klog.faddr", |
158 | &klogaddr); |
158 | &klogaddr); |
159 | break; |
159 | break; |
160 | default: |
160 | default: |
161 | ipc_answer_0(callid, ENOENT); |
161 | ipc_answer_0(callid, ENOENT); |
162 | } |
162 | } |
163 | continue; |
163 | continue; |
164 | case IPC_M_PHONE_HUNGUP: |
164 | case IPC_M_PHONE_HUNGUP: |
165 | retval = EOK; |
165 | retval = EOK; |
166 | break; |
166 | break; |
167 | case IPC_M_CONNECT_TO_ME: |
167 | case IPC_M_CONNECT_TO_ME: |
168 | /* |
168 | /* |
169 | * Server requests service registration. |
169 | * Server requests service registration. |
170 | */ |
170 | */ |
171 | if (service_clonable(IPC_GET_ARG1(call))) { |
171 | if (service_clonable(IPC_GET_ARG1(call))) { |
172 | register_clonable(IPC_GET_ARG1(call), |
172 | register_clonable(IPC_GET_ARG1(call), |
173 | IPC_GET_ARG5(call), &call, callid); |
173 | IPC_GET_ARG5(call), &call, callid); |
174 | continue; |
174 | continue; |
175 | } else { |
175 | } else { |
176 | retval = register_service(IPC_GET_ARG1(call), |
176 | retval = register_service(IPC_GET_ARG1(call), |
177 | IPC_GET_ARG5(call), &call); |
177 | IPC_GET_ARG5(call), &call); |
178 | } |
178 | } |
179 | break; |
179 | break; |
180 | case IPC_M_CONNECT_ME_TO: |
180 | case IPC_M_CONNECT_ME_TO: |
181 | /* |
181 | /* |
182 | * Client requests to be connected to a service. |
182 | * Client requests to be connected to a service. |
183 | */ |
183 | */ |
184 | if (service_clonable(IPC_GET_ARG1(call))) { |
184 | if (service_clonable(IPC_GET_ARG1(call))) { |
185 | connect_to_clonable(IPC_GET_ARG1(call), |
185 | connect_to_clonable(IPC_GET_ARG1(call), |
186 | &call, callid); |
186 | &call, callid); |
187 | continue; |
187 | continue; |
188 | } else { |
188 | } else { |
189 | retval = connect_to_service(IPC_GET_ARG1(call), |
189 | retval = connect_to_service(IPC_GET_ARG1(call), |
190 | &call, callid); |
190 | &call, callid); |
191 | } |
191 | } |
192 | break; |
192 | break; |
193 | default: |
193 | default: |
194 | retval = ENOENT; |
194 | retval = ENOENT; |
195 | break; |
195 | break; |
196 | } |
196 | } |
197 | if (!(callid & IPC_CALLID_NOTIFICATION)) { |
197 | if (!(callid & IPC_CALLID_NOTIFICATION)) { |
198 | ipc_answer_0(callid, retval); |
198 | ipc_answer_0(callid, retval); |
199 | } |
199 | } |
200 | } |
200 | } |
201 | 201 | ||
202 | /* Not reached */ |
202 | /* Not reached */ |
203 | return 0; |
203 | return 0; |
204 | } |
204 | } |
205 | 205 | ||
206 | /** Register service. |
206 | /** Register service. |
207 | * |
207 | * |
208 | * @param service Service to be registered. |
208 | * @param service Service to be registered. |
209 | * @param phone Phone to be used for connections to the service. |
209 | * @param phone Phone to be used for connections to the service. |
210 | * @param call Pointer to call structure. |
210 | * @param call Pointer to call structure. |
211 | * |
211 | * |
212 | * @return Zero on success or a value from @ref errno.h. |
212 | * @return Zero on success or a value from @ref errno.h. |
213 | */ |
213 | */ |
214 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call) |
214 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call) |
215 | { |
215 | { |
216 | unsigned long keys[3] = { |
216 | unsigned long keys[3] = { |
217 | service, |
217 | service, |
218 | call->in_phone_hash, |
218 | call->in_phone_hash, |
219 | 0 |
219 | 0 |
220 | }; |
220 | }; |
221 | hashed_service_t *hs; |
221 | hashed_service_t *hs; |
222 | 222 | ||
223 | if (hash_table_find(&ns_hash_table, keys)) { |
223 | if (hash_table_find(&ns_hash_table, keys)) { |
224 | return EEXISTS; |
224 | return EEXISTS; |
225 | } |
225 | } |
226 | 226 | ||
227 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
227 | hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
228 | if (!hs) { |
228 | if (!hs) { |
229 | return ENOMEM; |
229 | return ENOMEM; |
230 | } |
230 | } |
231 | 231 | ||
232 | link_initialize(&hs->link); |
232 | link_initialize(&hs->link); |
233 | hs->service = service; |
233 | hs->service = service; |
234 | hs->phone = phone; |
234 | hs->phone = phone; |
235 | hs->in_phone_hash = call->in_phone_hash; |
235 | hs->in_phone_hash = call->in_phone_hash; |
236 | hash_table_insert(&ns_hash_table, keys, &hs->link); |
236 | hash_table_insert(&ns_hash_table, keys, &hs->link); |
237 | 237 | ||
238 | return 0; |
238 | return 0; |
239 | } |
239 | } |
240 | 240 | ||
241 | /** Connect client to service. |
241 | /** Connect client to service. |
242 | * |
242 | * |
243 | * @param service Service to be connected to. |
243 | * @param service Service to be connected to. |
244 | * @param call Pointer to call structure. |
244 | * @param call Pointer to call structure. |
245 | * @param callid Call ID of the request. |
245 | * @param callid Call ID of the request. |
246 | * |
246 | * |
247 | * @return Zero on success or a value from @ref errno.h. |
247 | * @return Zero on success or a value from @ref errno.h. |
248 | */ |
248 | */ |
249 | int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid) |
249 | int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid) |
250 | { |
250 | { |
251 | unsigned long keys[3] = { service, 0, 0 }; |
251 | unsigned long keys[3] = { service, 0, 0 }; |
252 | link_t *hlp; |
252 | link_t *hlp; |
253 | hashed_service_t *hs; |
253 | hashed_service_t *hs; |
254 | 254 | ||
255 | hlp = hash_table_find(&ns_hash_table, keys); |
255 | hlp = hash_table_find(&ns_hash_table, keys); |
256 | if (!hlp) { |
256 | if (!hlp) { |
257 | return ENOENT; |
257 | return ENOENT; |
258 | } |
258 | } |
259 | hs = hash_table_get_instance(hlp, hashed_service_t, link); |
259 | hs = hash_table_get_instance(hlp, hashed_service_t, link); |
260 | return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call), |
260 | return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call), |
261 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE); |
261 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE); |
262 | } |
262 | } |
263 | 263 | ||
264 | /** Register clonable service. |
264 | /** Register clonable service. |
265 | * |
265 | * |
266 | * @param service Service to be registered. |
266 | * @param service Service to be registered. |
267 | * @param phone Phone to be used for connections to the service. |
267 | * @param phone Phone to be used for connections to the service. |
268 | * @param call Pointer to call structure. |
268 | * @param call Pointer to call structure. |
269 | */ |
269 | */ |
270 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
270 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
271 | ipc_callid_t callid) |
271 | ipc_callid_t callid) |
272 | { |
272 | { |
273 | int rc; |
273 | int rc; |
274 | cs_req_t *csr; |
274 | cs_req_t *csr; |
275 | 275 | ||
276 | if (list_empty(&cs_req)) { |
276 | if (list_empty(&cs_req)) { |
277 | /* There was no pending connection request. */ |
277 | /* There was no pending connection request. */ |
278 | printf(NAME ": Unexpected clonable server.\n"); |
278 | printf(NAME ": Unexpected clonable server.\n"); |
279 | ipc_answer_0(callid, EBUSY); |
279 | ipc_answer_0(callid, EBUSY); |
280 | return; |
280 | return; |
281 | } |
281 | } |
282 | 282 | ||
283 | csr = list_get_instance(cs_req.next, cs_req_t, link); |
283 | csr = list_get_instance(cs_req.next, cs_req_t, link); |
284 | list_remove(&csr->link); |
284 | list_remove(&csr->link); |
285 | 285 | ||
286 | /* Currently we can only handle a single type of clonable service. */ |
286 | /* Currently we can only handle a single type of clonable service. */ |
287 | assert(csr->service == SERVICE_LOAD); |
287 | assert(csr->service == SERVICE_LOAD); |
288 | 288 | ||
289 | ipc_answer_0(callid, EOK); |
289 | ipc_answer_0(callid, EOK); |
290 | 290 | ||
291 | rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(*csr->call), |
291 | rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call), |
292 | IPC_GET_ARG3(*csr->call), 0, IPC_FF_NONE); |
292 | IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE); |
293 | 293 | ||
294 | free(csr); |
294 | free(csr); |
295 | } |
295 | } |
296 | 296 | ||
297 | /** Connect client to clonable service. |
297 | /** Connect client to clonable service. |
298 | * |
298 | * |
299 | * @param service Service to be connected to. |
299 | * @param service Service to be connected to. |
300 | * @param call Pointer to call structure. |
300 | * @param call Pointer to call structure. |
301 | * @param callid Call ID of the request. |
301 | * @param callid Call ID of the request. |
302 | * |
302 | * |
303 | * @return Zero on success or a value from @ref errno.h. |
303 | * @return Zero on success or a value from @ref errno.h. |
304 | */ |
304 | */ |
305 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
305 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
306 | ipc_callid_t callid) |
306 | ipc_callid_t callid) |
307 | { |
307 | { |
308 | int rc; |
308 | int rc; |
309 | cs_req_t *csr; |
309 | cs_req_t *csr; |
310 | 310 | ||
311 | assert(service == SERVICE_LOAD); |
311 | assert(service == SERVICE_LOAD); |
312 | 312 | ||
313 | csr = malloc(sizeof(cs_req_t)); |
313 | csr = malloc(sizeof(cs_req_t)); |
314 | if (csr == NULL) { |
314 | if (csr == NULL) { |
315 | ipc_answer_0(callid, ENOMEM); |
315 | ipc_answer_0(callid, ENOMEM); |
316 | return; |
316 | return; |
317 | } |
317 | } |
318 | 318 | ||
319 | /* Spawn a loader. */ |
319 | /* Spawn a loader. */ |
320 | rc = loader_spawn("loader"); |
320 | rc = loader_spawn("loader"); |
321 | 321 | ||
322 | if (rc < 0) { |
322 | if (rc < 0) { |
323 | free(csr); |
323 | free(csr); |
324 | ipc_answer_0(callid, rc); |
324 | ipc_answer_0(callid, rc); |
325 | return; |
325 | return; |
326 | } |
326 | } |
327 | 327 | ||
328 | csr->service = service; |
328 | csr->service = service; |
329 | csr->call = call; |
329 | csr->call = *call; |
330 | csr->callid = callid; |
330 | csr->callid = callid; |
331 | 331 | ||
332 | /* |
332 | /* |
333 | * We can forward the call only after the server we spawned connects |
333 | * We can forward the call only after the server we spawned connects |
334 | * to us. Meanwhile we might need to service more connection requests. |
334 | * to us. Meanwhile we might need to service more connection requests. |
335 | * Thus we store the call in a queue. |
335 | * Thus we store the call in a queue. |
336 | */ |
336 | */ |
337 | list_append(&csr->link, &cs_req); |
337 | list_append(&csr->link, &cs_req); |
338 | } |
338 | } |
339 | 339 | ||
340 | /** Compute hash index into NS hash table. |
340 | /** Compute hash index into NS hash table. |
341 | * |
341 | * |
342 | * @param key Pointer keys. However, only the first key (i.e. service number) |
342 | * @param key Pointer keys. However, only the first key (i.e. service number) |
343 | * is used to compute the hash index. |
343 | * is used to compute the hash index. |
344 | * @return Hash index corresponding to key[0]. |
344 | * @return Hash index corresponding to key[0]. |
345 | */ |
345 | */ |
346 | hash_index_t ns_hash(unsigned long *key) |
346 | hash_index_t ns_hash(unsigned long *key) |
347 | { |
347 | { |
348 | assert(key); |
348 | assert(key); |
349 | return *key % NS_HASH_TABLE_CHAINS; |
349 | return *key % NS_HASH_TABLE_CHAINS; |
350 | } |
350 | } |
351 | 351 | ||
352 | /** Compare a key with hashed item. |
352 | /** Compare a key with hashed item. |
353 | * |
353 | * |
354 | * This compare function always ignores the third key. |
354 | * This compare function always ignores the third key. |
355 | * It exists only to make it possible to remove records |
355 | * It exists only to make it possible to remove records |
356 | * originating from connection with key[1] in_phone_hash |
356 | * originating from connection with key[1] in_phone_hash |
357 | * value. Note that this is close to being classified |
357 | * value. Note that this is close to being classified |
358 | * as a nasty hack. |
358 | * as a nasty hack. |
359 | * |
359 | * |
360 | * @param key Array of keys. |
360 | * @param key Array of keys. |
361 | * @param keys Must be lesser or equal to 3. |
361 | * @param keys Must be lesser or equal to 3. |
362 | * @param item Pointer to a hash table item. |
362 | * @param item Pointer to a hash table item. |
363 | * @return Non-zero if the key matches the item, zero otherwise. |
363 | * @return Non-zero if the key matches the item, zero otherwise. |
364 | */ |
364 | */ |
365 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item) |
365 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item) |
366 | { |
366 | { |
367 | hashed_service_t *hs; |
367 | hashed_service_t *hs; |
368 | 368 | ||
369 | assert(key); |
369 | assert(key); |
370 | assert(keys <= 3); |
370 | assert(keys <= 3); |
371 | assert(item); |
371 | assert(item); |
372 | 372 | ||
373 | hs = hash_table_get_instance(item, hashed_service_t, link); |
373 | hs = hash_table_get_instance(item, hashed_service_t, link); |
374 | 374 | ||
375 | if (keys == 2) |
375 | if (keys == 2) |
376 | return key[1] == hs->in_phone_hash; |
376 | return key[1] == hs->in_phone_hash; |
377 | else |
377 | else |
378 | return key[0] == hs->service; |
378 | return key[0] == hs->service; |
379 | } |
379 | } |
380 | 380 | ||
381 | /** Perform actions after removal of item from the hash table. |
381 | /** Perform actions after removal of item from the hash table. |
382 | * |
382 | * |
383 | * @param item Item that was removed from the hash table. |
383 | * @param item Item that was removed from the hash table. |
384 | */ |
384 | */ |
385 | void ns_remove(link_t *item) |
385 | void ns_remove(link_t *item) |
386 | { |
386 | { |
387 | assert(item); |
387 | assert(item); |
388 | free(hash_table_get_instance(item, hashed_service_t, link)); |
388 | free(hash_table_get_instance(item, hashed_service_t, link)); |
389 | } |
389 | } |
390 | 390 | ||
391 | /** |
391 | /** |
392 | * @} |
392 | * @} |
393 | */ |
393 | */ |
394 | 394 |