source: mainline/uspace/srv/ns/service.c@ 6a8ce16e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6a8ce16e was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

  • Property mode set to 100644
File size: 5.4 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 <stdio.h>
38#include <malloc.h>
39#include "service.h"
40#include "ns.h"
41
42
43/** Service hash table item. */
44typedef struct {
45 ht_link_t link;
46 sysarg_t service; /**< Service ID. */
47 sysarg_t phone; /**< Phone registered with the service. */
48 sysarg_t in_phone_hash; /**< Incoming phone hash. */
49} hashed_service_t;
50
51
52static size_t service_key_hash(void *key)
53{
54 return *(sysarg_t*)key;
55}
56
57static size_t service_hash(const ht_link_t *item)
58{
59 hashed_service_t *hs = hash_table_get_inst(item, hashed_service_t, link);
60 return hs->service;
61}
62
63static bool service_key_equal(void *key, const ht_link_t *item)
64{
65 hashed_service_t *hs = hash_table_get_inst(item, hashed_service_t, link);
66 return hs->service == *(sysarg_t*)key;
67}
68
69/** Operations for service hash table. */
70static hash_table_ops_t service_hash_table_ops = {
71 .hash = service_hash,
72 .key_hash = service_key_hash,
73 .key_equal = service_key_equal,
74 .equal = NULL,
75 .remove_callback = NULL
76};
77
78/** Service hash table structure. */
79static hash_table_t service_hash_table;
80
81/** Pending connection structure. */
82typedef struct {
83 link_t link;
84 sysarg_t service; /**< Number of the service. */
85 ipc_callid_t callid; /**< Call ID waiting for the connection */
86 sysarg_t arg2; /**< Second argument */
87 sysarg_t arg3; /**< Third argument */
88} pending_conn_t;
89
90static list_t pending_conn;
91
92int service_init(void)
93{
94 if (!hash_table_create(&service_hash_table, 0, 0, &service_hash_table_ops)) {
95 printf(NAME ": No memory available for services\n");
96 return ENOMEM;
97 }
98
99 list_initialize(&pending_conn);
100
101 return EOK;
102}
103
104/** Process pending connection requests */
105void process_pending_conn(void)
106{
107loop:
108 list_foreach(pending_conn, link, pending_conn_t, pr) {
109 ht_link_t *link = hash_table_find(&service_hash_table, &pr->service);
110 if (!link)
111 continue;
112
113 hashed_service_t *hs = hash_table_get_inst(link, hashed_service_t, link);
114 (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2,
115 pr->arg3, 0, IPC_FF_NONE);
116
117 list_remove(&pr->link);
118 free(pr);
119 goto loop;
120 }
121}
122
123/** Register service.
124 *
125 * @param service Service to be registered.
126 * @param phone Phone to be used for connections to the service.
127 * @param call Pointer to call structure.
128 *
129 * @return Zero on success or a value from @ref errno.h.
130 *
131 */
132int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call)
133{
134 if (hash_table_find(&service_hash_table, &service))
135 return EEXISTS;
136
137 hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
138 if (!hs)
139 return ENOMEM;
140
141 hs->service = service;
142 hs->phone = phone;
143 hs->in_phone_hash = call->in_phone_hash;
144 hash_table_insert(&service_hash_table, &hs->link);
145
146 return EOK;
147}
148
149/** Connect client to service.
150 *
151 * @param service Service to be connected to.
152 * @param call Pointer to call structure.
153 * @param callid Call ID of the request.
154 *
155 * @return Zero on success or a value from @ref errno.h.
156 *
157 */
158void connect_to_service(sysarg_t service, ipc_call_t *call, ipc_callid_t callid)
159{
160 sysarg_t retval;
161
162 ht_link_t *link = hash_table_find(&service_hash_table, &service);
163 if (!link) {
164 if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
165 /* Blocking connection, add to pending list */
166 pending_conn_t *pr =
167 (pending_conn_t *) malloc(sizeof(pending_conn_t));
168 if (!pr) {
169 retval = ENOMEM;
170 goto out;
171 }
172
173 link_initialize(&pr->link);
174 pr->service = service;
175 pr->callid = callid;
176 pr->arg2 = IPC_GET_ARG2(*call);
177 pr->arg3 = IPC_GET_ARG3(*call);
178 list_append(&pr->link, &pending_conn);
179 return;
180 }
181 retval = ENOENT;
182 goto out;
183 }
184
185 hashed_service_t *hs = hash_table_get_inst(link, hashed_service_t, link);
186 (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
187 IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
188 return;
189
190out:
191 if (!(callid & IPC_CALLID_NOTIFICATION))
192 ipc_answer_0(callid, retval);
193}
194
195/**
196 * @}
197 */
Note: See TracBrowser for help on using the repository browser.