source: mainline/uspace/srv/ns/service.c@ 32c2c8f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 32c2c8f was 4e00f87, checked in by Jakub Jermar <jakub@…>, 13 years ago

Use NULL instead of 0 as a hash_table_ops_t member initializer.

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