source: mainline/uspace/srv/ns/ns.c@ e623197

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

service banner

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
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/**
34 * @file ns.c
35 * @brief Naming service for HelenOS IPC.
36 */
37
38
39#include <ipc/ipc.h>
40#include <ipc/ns.h>
41#include <ipc/services.h>
42#include <stdio.h>
43#include <unistd.h>
44#include <stdlib.h>
45#include <errno.h>
46#include <assert.h>
47#include <libadt/list.h>
48#include <libadt/hash_table.h>
49#include <sysinfo.h>
50#include <ddi.h>
51#include <as.h>
52
53#define NAME "ns"
54
55#define NS_HASH_TABLE_CHAINS 20
56
57static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
58static int connect_to_service(ipcarg_t service, ipc_call_t *call,
59 ipc_callid_t callid);
60
61/* Static functions implementing NS hash table operations. */
62static hash_index_t ns_hash(unsigned long *key);
63static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
64static void ns_remove(link_t *item);
65
66/** Operations for NS hash table. */
67static hash_table_operations_t ns_hash_table_ops = {
68 .hash = ns_hash,
69 .compare = ns_compare,
70 .remove_callback = ns_remove
71};
72
73/** NS hash table structure. */
74static hash_table_t ns_hash_table;
75
76/** NS hash table item. */
77typedef struct {
78 link_t link;
79 ipcarg_t service; /**< Number of the service. */
80 ipcarg_t phone; /**< Phone registered with the service. */
81 ipcarg_t in_phone_hash; /**< Incoming phone hash. */
82} hashed_service_t;
83
84static void *clockaddr = NULL;
85static void *klogaddr = NULL;
86
87static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name,
88 void **addr)
89{
90 void *ph_addr;
91
92 if (!*addr) {
93 ph_addr = (void *) sysinfo_value(name);
94 if (!ph_addr) {
95 ipc_answer_0(callid, ENOENT);
96 return;
97 }
98 *addr = as_get_mappable_page(PAGE_SIZE);
99 physmem_map(ph_addr, *addr, 1,
100 AS_AREA_READ | AS_AREA_CACHEABLE);
101 }
102 ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ);
103}
104
105int main(int argc, char **argv)
106{
107 printf(NAME ": HelenOS IPC Naming Service\n");
108
109 ipc_call_t call;
110 ipc_callid_t callid;
111
112 ipcarg_t retval;
113
114 if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
115 &ns_hash_table_ops)) {
116 printf(NAME ": No memory available\n");
117 return ENOMEM;
118 }
119
120 printf(NAME ": Accepting connections\n");
121 while (1) {
122 callid = ipc_wait_for_call(&call);
123 switch (IPC_GET_METHOD(call)) {
124 case IPC_M_SHARE_IN:
125 switch (IPC_GET_ARG3(call)) {
126 case SERVICE_MEM_REALTIME:
127 get_as_area(callid, &call, "clock.faddr",
128 &clockaddr);
129 break;
130 case SERVICE_MEM_KLOG:
131 get_as_area(callid, &call, "klog.faddr",
132 &klogaddr);
133 break;
134 default:
135 ipc_answer_0(callid, ENOENT);
136 }
137 continue;
138 case IPC_M_PHONE_HUNGUP:
139 retval = EOK;
140 break;
141 case IPC_M_CONNECT_TO_ME:
142 /*
143 * Server requests service registration.
144 */
145 retval = register_service(IPC_GET_ARG1(call),
146 IPC_GET_ARG5(call), &call);
147 break;
148 case IPC_M_CONNECT_ME_TO:
149 /*
150 * Client requests to be connected to a service.
151 */
152 retval = connect_to_service(IPC_GET_ARG1(call), &call,
153 callid);
154 break;
155 default:
156 retval = ENOENT;
157 break;
158 }
159 if (!(callid & IPC_CALLID_NOTIFICATION)) {
160 ipc_answer_0(callid, retval);
161 }
162 }
163
164 /* Not reached */
165 return 0;
166}
167
168/** Register service.
169 *
170 * @param service Service to be registered.
171 * @param phone Phone to be used for connections to the service.
172 * @param call Pointer to call structure.
173 *
174 * @return Zero on success or a value from @ref errno.h.
175 */
176int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
177{
178 unsigned long keys[3] = {
179 service,
180 call->in_phone_hash,
181 0
182 };
183 hashed_service_t *hs;
184
185 if (hash_table_find(&ns_hash_table, keys)) {
186 return EEXISTS;
187 }
188
189 hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
190 if (!hs) {
191 return ENOMEM;
192 }
193
194 link_initialize(&hs->link);
195 hs->service = service;
196 hs->phone = phone;
197 hs->in_phone_hash = call->in_phone_hash;
198 hash_table_insert(&ns_hash_table, keys, &hs->link);
199
200 return 0;
201}
202
203/** Connect client to service.
204 *
205 * @param service Service to be connected to.
206 * @param call Pointer to call structure.
207 * @param callid Call ID of the request.
208 *
209 * @return Zero on success or a value from @ref errno.h.
210 */
211int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
212{
213 unsigned long keys[3] = { service, 0, 0 };
214 link_t *hlp;
215 hashed_service_t *hs;
216
217 hlp = hash_table_find(&ns_hash_table, keys);
218 if (!hlp) {
219 return ENOENT;
220 }
221 hs = hash_table_get_instance(hlp, hashed_service_t, link);
222 return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
223 IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
224}
225
226/** Compute hash index into NS hash table.
227 *
228 * @param key Pointer keys. However, only the first key (i.e. service number)
229 * is used to compute the hash index.
230 * @return Hash index corresponding to key[0].
231 */
232hash_index_t ns_hash(unsigned long *key)
233{
234 assert(key);
235 return *key % NS_HASH_TABLE_CHAINS;
236}
237
238/** Compare a key with hashed item.
239 *
240 * This compare function always ignores the third key.
241 * It exists only to make it possible to remove records
242 * originating from connection with key[1] in_phone_hash
243 * value. Note that this is close to being classified
244 * as a nasty hack.
245 *
246 * @param key Array of keys.
247 * @param keys Must be lesser or equal to 3.
248 * @param item Pointer to a hash table item.
249 * @return Non-zero if the key matches the item, zero otherwise.
250 */
251int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
252{
253 hashed_service_t *hs;
254
255 assert(key);
256 assert(keys <= 3);
257 assert(item);
258
259 hs = hash_table_get_instance(item, hashed_service_t, link);
260
261 if (keys == 2)
262 return key[1] == hs->in_phone_hash;
263 else
264 return key[0] == hs->service;
265}
266
267/** Perform actions after removal of item from the hash table.
268 *
269 * @param item Item that was removed from the hash table.
270 */
271void ns_remove(link_t *item)
272{
273 assert(item);
274 free(hash_table_get_instance(item, hashed_service_t, link));
275}
276
277/**
278 * @}
279 */
Note: See TracBrowser for help on using the repository browser.