Subversion Repositories HelenOS-historic

Rev

Rev 1649 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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