source: mainline/uspace/srv/ns/service.c@ 2133e02

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2133e02 was 2133e02, checked in by Martin Decky <martin@…>, 10 years ago

canonically reserve the first argument of IPC_M_CONNECT_ME_TO for interface type
naming service: service handle in IPC_M_CONNECT_ME_TO needs to be shifted to the second argument
service connections that use the untyped (fallback) port can only provide one additional argument now

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