source: mainline/uspace/srv/ns/service.c@ 164c653

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 164c653 was 007e6efa, checked in by Martin Decky <martin@…>, 14 years ago
  • libc routines for registering services and connecting to services via NS
  • async_connect_to_me()
  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Copyright (c) 2009 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup ns
30 * @{
31 */
32
33#include <ipc/ipc.h>
34#include <adt/hash_table.h>
35#include <assert.h>
36#include <errno.h>
37#include "service.h"
38#include "ns.h"
39
40#define SERVICE_HASH_TABLE_CHAINS 20
41
42/** Service hash table item. */
43typedef struct {
44 link_t link;
45 sysarg_t service; /**< Service ID. */
46 sysarg_t phone; /**< Phone registered with the service. */
47 sysarg_t in_phone_hash; /**< Incoming phone hash. */
48} hashed_service_t;
49
50/** Compute hash index into service hash table.
51 *
52 * @param key Pointer keys. However, only the first key (i.e. service number)
53 * is used to compute the hash index.
54 *
55 * @return Hash index corresponding to key[0].
56 *
57 */
58static hash_index_t service_hash(unsigned long key[])
59{
60 assert(key);
61 return (key[0] % SERVICE_HASH_TABLE_CHAINS);
62}
63
64/** Compare a key with hashed item.
65 *
66 * This compare function always ignores the third key.
67 * It exists only to make it possible to remove records
68 * originating from connection with key[1] in_phone_hash
69 * value. Note that this is close to being classified
70 * as a nasty hack.
71 *
72 * @param key Array of keys.
73 * @param keys Must be lesser or equal to 3.
74 * @param item Pointer to a hash table item.
75 *
76 * @return Non-zero if the key matches the item, zero otherwise.
77 *
78 */
79static int service_compare(unsigned long key[], hash_count_t keys, link_t *item)
80{
81 assert(key);
82 assert(keys <= 3);
83 assert(item);
84
85 hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
86
87 if (keys == 2)
88 return ((key[0] == hs->service) && (key[1] == hs->in_phone_hash));
89 else
90 return (key[0] == hs->service);
91}
92
93/** Perform actions after removal of item from the hash table.
94 *
95 * @param item Item that was removed from the hash table.
96 *
97 */
98static void service_remove(link_t *item)
99{
100 assert(item);
101 free(hash_table_get_instance(item, hashed_service_t, link));
102}
103
104/** Operations for service hash table. */
105static hash_table_operations_t service_hash_table_ops = {
106 .hash = service_hash,
107 .compare = service_compare,
108 .remove_callback = service_remove
109};
110
111/** Service hash table structure. */
112static hash_table_t service_hash_table;
113
114/** Pending connection structure. */
115typedef struct {
116 link_t link;
117 sysarg_t service; /**< Number of the service. */
118 ipc_callid_t callid; /**< Call ID waiting for the connection */
119 sysarg_t arg2; /**< Second argument */
120 sysarg_t arg3; /**< Third argument */
121} pending_conn_t;
122
123static link_t pending_conn;
124
125int service_init(void)
126{
127 if (!hash_table_create(&service_hash_table, SERVICE_HASH_TABLE_CHAINS,
128 3, &service_hash_table_ops)) {
129 printf(NAME ": No memory available for services\n");
130 return ENOMEM;
131 }
132
133 list_initialize(&pending_conn);
134
135 return EOK;
136}
137
138/** Process pending connection requests */
139void process_pending_conn(void)
140{
141 link_t *cur;
142
143loop:
144 for (cur = pending_conn.next; cur != &pending_conn; cur = cur->next) {
145 pending_conn_t *pr = list_get_instance(cur, pending_conn_t, link);
146
147 unsigned long keys[3] = {
148 pr->service,
149 0,
150 0
151 };
152
153 link_t *link = hash_table_find(&service_hash_table, keys);
154 if (!link)
155 continue;
156
157 hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
158 (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2,
159 pr->arg3, 0, IPC_FF_NONE);
160
161 list_remove(cur);
162 free(pr);
163 goto loop;
164 }
165}
166
167/** Register service.
168 *
169 * @param service Service to be registered.
170 * @param phone Phone to be used for connections to the service.
171 * @param call Pointer to call structure.
172 *
173 * @return Zero on success or a value from @ref errno.h.
174 *
175 */
176int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call)
177{
178 unsigned long keys[3] = {
179 service,
180 call->in_phone_hash,
181 0
182 };
183
184 if (hash_table_find(&service_hash_table, keys))
185 return EEXISTS;
186
187 hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
188 if (!hs)
189 return ENOMEM;
190
191 link_initialize(&hs->link);
192 hs->service = service;
193 hs->phone = phone;
194 hs->in_phone_hash = call->in_phone_hash;
195 hash_table_insert(&service_hash_table, keys, &hs->link);
196
197 return EOK;
198}
199
200/** Connect client to service.
201 *
202 * @param service Service to be connected to.
203 * @param call Pointer to call structure.
204 * @param callid Call ID of the request.
205 *
206 * @return Zero on success or a value from @ref errno.h.
207 *
208 */
209void connect_to_service(sysarg_t service, ipc_call_t *call, ipc_callid_t callid)
210{
211 sysarg_t retval;
212 unsigned long keys[3] = {
213 service,
214 0,
215 0
216 };
217
218 link_t *link = hash_table_find(&service_hash_table, keys);
219 if (!link) {
220 if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
221 /* Blocking connection, add to pending list */
222 pending_conn_t *pr =
223 (pending_conn_t *) malloc(sizeof(pending_conn_t));
224 if (!pr) {
225 retval = ENOMEM;
226 goto out;
227 }
228
229 link_initialize(&pr->link);
230 pr->service = service;
231 pr->callid = callid;
232 pr->arg2 = IPC_GET_ARG2(*call);
233 pr->arg3 = IPC_GET_ARG3(*call);
234 list_append(&pr->link, &pending_conn);
235 return;
236 }
237 retval = ENOENT;
238 goto out;
239 }
240
241 hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
242 (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
243 IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
244 return;
245
246out:
247 if (!(callid & IPC_CALLID_NOTIFICATION))
248 ipc_answer_0(callid, retval);
249}
250
251/**
252 * @}
253 */
Note: See TracBrowser for help on using the repository browser.