Subversion Repositories HelenOS-historic

Rev

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