Subversion Repositories HelenOS

Rev

Rev 2471 | Rev 2479 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2471 Rev 2476
1
/*
1
/*
2
 * Copyright (c) 2006 Ondrej Palkovsky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup ns
29
/** @addtogroup ns
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    ns.c
34
 * @file    ns.c
35
 * @brief   Naming service for HelenOS IPC.
35
 * @brief   Naming service for HelenOS IPC.
36
 */
36
 */
37
 
37
 
38
 
38
 
39
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
40
#include <ipc/ns.h>
40
#include <ipc/ns.h>
41
#include <ipc/services.h>
41
#include <ipc/services.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <unistd.h>
43
#include <unistd.h>
44
#include <stdlib.h>
44
#include <stdlib.h>
45
#include <errno.h>
45
#include <errno.h>
46
#include <assert.h>
46
#include <assert.h>
47
#include <libadt/list.h>
47
#include <libadt/list.h>
48
#include <libadt/hash_table.h>
48
#include <libadt/hash_table.h>
49
#include <sysinfo.h>
49
#include <sysinfo.h>
50
#include <ddi.h>
50
#include <ddi.h>
51
#include <as.h>
51
#include <as.h>
52
 
52
 
53
#define NAME    "NS"
53
#define NAME    "NS"
54
 
54
 
55
#define NS_HASH_TABLE_CHAINS    20
55
#define NS_HASH_TABLE_CHAINS    20
56
 
56
 
57
static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
57
static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
58
static int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid);
58
static int connect_to_service(ipcarg_t service, ipc_call_t *call,
-
 
59
    ipc_callid_t callid);
59
 
60
 
60
/* Static functions implementing NS hash table operations. */
61
/* Static functions implementing NS hash table operations. */
61
static hash_index_t ns_hash(unsigned long *key);
62
static hash_index_t ns_hash(unsigned long *key);
62
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
63
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
63
static void ns_remove(link_t *item);
64
static void ns_remove(link_t *item);
64
 
65
 
65
/** Operations for NS hash table. */
66
/** Operations for NS hash table. */
66
static hash_table_operations_t ns_hash_table_ops = {
67
static hash_table_operations_t ns_hash_table_ops = {
67
    .hash = ns_hash,
68
    .hash = ns_hash,
68
    .compare = ns_compare,
69
    .compare = ns_compare,
69
    .remove_callback = ns_remove
70
    .remove_callback = ns_remove
70
};
71
};
71
 
72
 
72
/** NS hash table structure. */
73
/** NS hash table structure. */
73
static hash_table_t ns_hash_table;
74
static hash_table_t ns_hash_table;
74
 
75
 
75
/** NS hash table item. */
76
/** NS hash table item. */
76
typedef struct {
77
typedef struct {
77
    link_t link;
78
    link_t link;
78
    ipcarg_t service;       /**< Number of the service. */
79
    ipcarg_t service;       /**< Number of the service. */
79
    ipcarg_t phone;         /**< Phone registered with the service. */
80
    ipcarg_t phone;         /**< Phone registered with the service. */
80
    ipcarg_t in_phone_hash;     /**< Incoming phone hash. */
81
    ipcarg_t in_phone_hash;     /**< Incoming phone hash. */
81
} hashed_service_t;
82
} hashed_service_t;
82
 
83
 
83
static void *clockaddr = NULL;
84
static void *clockaddr = NULL;
84
static void *klogaddr = NULL;
85
static void *klogaddr = NULL;
85
 
86
 
86
static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name, void **addr)
87
static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name,
-
 
88
    void **addr)
87
{
89
{
88
    void *ph_addr;
90
    void *ph_addr;
89
 
91
 
90
    if (!*addr) {
92
    if (!*addr) {
91
        ph_addr = (void *) sysinfo_value(name);
93
        ph_addr = (void *) sysinfo_value(name);
92
        if (!ph_addr) {
94
        if (!ph_addr) {
93
            ipc_answer_fast(callid, ENOENT, 0, 0);
95
            ipc_answer_fast(callid, ENOENT, 0, 0);
94
            return;
96
            return;
95
        }
97
        }
96
        *addr = as_get_mappable_page(PAGE_SIZE);
98
        *addr = as_get_mappable_page(PAGE_SIZE);
97
        physmem_map(ph_addr, *addr, 1, AS_AREA_READ | AS_AREA_CACHEABLE);
99
        physmem_map(ph_addr, *addr, 1, AS_AREA_READ | AS_AREA_CACHEABLE);
98
    }
100
    }
99
    ipc_answer_fast(callid, 0, (ipcarg_t) *addr, AS_AREA_READ);
101
    ipc_answer_fast(callid, 0, (ipcarg_t) *addr, AS_AREA_READ);
100
}
102
}
101
 
103
 
102
int main(int argc, char **argv)
104
int main(int argc, char **argv)
103
{
105
{
104
    ipc_call_t call;
106
    ipc_call_t call;
105
    ipc_callid_t callid;
107
    ipc_callid_t callid;
106
   
108
   
107
    ipcarg_t retval;
109
    ipcarg_t retval;
108
 
110
 
109
    if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, &ns_hash_table_ops)) {
111
    if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
-
 
112
        &ns_hash_table_ops)) {
110
        return ENOMEM;
113
        return ENOMEM;
111
    }
114
    }
112
       
115
       
113
    while (1) {
116
    while (1) {
114
        callid = ipc_wait_for_call(&call);
117
        callid = ipc_wait_for_call(&call);
115
        switch (IPC_GET_METHOD(call)) {
118
        switch (IPC_GET_METHOD(call)) {
116
        case IPC_M_AS_AREA_RECV:
119
        case IPC_M_AS_AREA_RECV:
117
            switch (IPC_GET_ARG3(call)) {
120
            switch (IPC_GET_ARG3(call)) {
118
            case SERVICE_MEM_REALTIME:
121
            case SERVICE_MEM_REALTIME:
119
                get_as_area(callid, &call, "clock.faddr",
122
                get_as_area(callid, &call, "clock.faddr",
120
                    &clockaddr);
123
                    &clockaddr);
121
                break;
124
                break;
122
            case SERVICE_MEM_KLOG:
125
            case SERVICE_MEM_KLOG:
123
                get_as_area(callid, &call, "klog.faddr",
126
                get_as_area(callid, &call, "klog.faddr",
124
                    &klogaddr);
127
                    &klogaddr);
125
                break;
128
                break;
126
            default:
129
            default:
127
                ipc_answer_fast(callid, ENOENT, 0, 0);
130
                ipc_answer_fast(callid, ENOENT, 0, 0);
128
            }
131
            }
129
            continue;
132
            continue;
130
        case IPC_M_PHONE_HUNGUP:
133
        case IPC_M_PHONE_HUNGUP:
131
            retval = 0;
134
            retval = 0;
132
            break;
135
            break;
133
        case IPC_M_CONNECT_TO_ME:
136
        case IPC_M_CONNECT_TO_ME:
134
            /*
137
            /*
135
             * Server requests service registration.
138
             * Server requests service registration.
136
             */
139
             */
137
            retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG3(call), &call);
140
            retval = register_service(IPC_GET_ARG1(call),
-
 
141
                IPC_GET_ARG3(call), &call);
138
            break;
142
            break;
139
        case IPC_M_CONNECT_ME_TO:
143
        case IPC_M_CONNECT_ME_TO:
140
            /*
144
            /*
141
             * Client requests to be connected to a service.
145
             * Client requests to be connected to a service.
142
             */
146
             */
143
            retval = connect_to_service(IPC_GET_ARG1(call), &call, callid);
147
            retval = connect_to_service(IPC_GET_ARG1(call), &call,
-
 
148
                callid);
144
            break;
149
            break;
145
        default:
150
        default:
146
            retval = ENOENT;
151
            retval = ENOENT;
147
            break;
152
            break;
148
        }
153
        }
149
        if (! (callid & IPC_CALLID_NOTIFICATION)) {
154
        if (!(callid & IPC_CALLID_NOTIFICATION)) {
150
            ipc_answer_fast(callid, retval, 0, 0);
155
            ipc_answer_fast(callid, retval, 0, 0);
151
        }
156
        }
152
    }
157
    }
153
}
158
}
154
 
159
 
155
/** Register service.
160
/** Register service.
156
 *
161
 *
157
 * @param service Service to be registered.
162
 * @param service Service to be registered.
158
 * @param phone Phone to be used for connections to the service.
163
 * @param phone Phone to be used for connections to the service.
159
 * @param call Pointer to call structure.
164
 * @param call Pointer to call structure.
160
 *
165
 *
161
 * @return Zero on success or a value from @ref errno.h.
166
 * @return Zero on success or a value from @ref errno.h.
162
 */
167
 */
163
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
168
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
164
{
169
{
165
    unsigned long keys[3] = { service, call->in_phone_hash, 0 };
170
    unsigned long keys[3] = {
-
 
171
        service,
-
 
172
        call->in_phone_hash,
-
 
173
        0
-
 
174
    };
166
    hashed_service_t *hs;
175
    hashed_service_t *hs;
167
           
176
           
168
    if (hash_table_find(&ns_hash_table, keys)) {
177
    if (hash_table_find(&ns_hash_table, keys)) {
169
        return EEXISTS;
178
        return EEXISTS;
170
    }
179
    }
171
           
180
           
172
    hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
181
    hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
173
    if (!hs) {
182
    if (!hs) {
174
        return ENOMEM;
183
        return ENOMEM;
175
    }
184
    }
176
           
185
           
177
    link_initialize(&hs->link);
186
    link_initialize(&hs->link);
178
    hs->service = service;
187
    hs->service = service;
179
    hs->phone = phone;
188
    hs->phone = phone;
180
    hs->in_phone_hash = call->in_phone_hash;
189
    hs->in_phone_hash = call->in_phone_hash;
181
    hash_table_insert(&ns_hash_table, keys, &hs->link);
190
    hash_table_insert(&ns_hash_table, keys, &hs->link);
182
           
191
           
183
    return 0;
192
    return 0;
184
}
193
}
185
 
194
 
186
/** Connect client to service.
195
/** Connect client to service.
187
 *
196
 *
188
 * @param service Service to be connected to.
197
 * @param service Service to be connected to.
189
 * @param call Pointer to call structure.
198
 * @param call Pointer to call structure.
190
 * @param callid Call ID of the request.
199
 * @param callid Call ID of the request.
191
 *
200
 *
192
 * @return Zero on success or a value from @ref errno.h.
201
 * @return Zero on success or a value from @ref errno.h.
193
 */
202
 */
194
int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
203
int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
195
{
204
{
196
    unsigned long keys[3] = { service, 0, 0 };
205
    unsigned long keys[3] = { service, 0, 0 };
197
    link_t *hlp;
206
    link_t *hlp;
198
    hashed_service_t *hs;
207
    hashed_service_t *hs;
199
           
208
           
200
    hlp = hash_table_find(&ns_hash_table, keys);
209
    hlp = hash_table_find(&ns_hash_table, keys);
201
    if (!hlp) {
210
    if (!hlp) {
202
        return ENOENT;
211
        return ENOENT;
203
    }
212
    }
204
    hs = hash_table_get_instance(hlp, hashed_service_t, link);
213
    hs = hash_table_get_instance(hlp, hashed_service_t, link);
205
    return ipc_forward_fast(callid, hs->phone, 0, 0);
214
    return ipc_forward_fast(callid, hs->phone, 0, 0);
206
}
215
}
207
 
216
 
208
/** Compute hash index into NS hash table.
217
/** Compute hash index into NS hash table.
209
 *
218
 *
210
 * @param key Pointer keys. However, only the first key (i.e. service number)
219
 * @param key Pointer keys. However, only the first key (i.e. service number)
211
 *        is used to compute the hash index.
220
 *        is used to compute the hash index.
212
 * @return Hash index corresponding to key[0].
221
 * @return Hash index corresponding to key[0].
213
 */
222
 */
214
hash_index_t ns_hash(unsigned long *key)
223
hash_index_t ns_hash(unsigned long *key)
215
{
224
{
216
    assert(key);
225
    assert(key);
217
    return *key % NS_HASH_TABLE_CHAINS;
226
    return *key % NS_HASH_TABLE_CHAINS;
218
}
227
}
219
 
228
 
220
/** Compare a key with hashed item.
229
/** Compare a key with hashed item.
221
 *
230
 *
222
 * This compare function always ignores the third key.
231
 * This compare function always ignores the third key.
223
 * It exists only to make it possible to remove records
232
 * It exists only to make it possible to remove records
224
 * originating from connection with key[1] in_phone_hash
233
 * originating from connection with key[1] in_phone_hash
225
 * value. Note that this is close to being classified
234
 * value. Note that this is close to being classified
226
 * as a nasty hack.
235
 * as a nasty hack.
227
 *
236
 *
228
 * @param key Array of keys.
237
 * @param key Array of keys.
229
 * @param keys Must be lesser or equal to 3.
238
 * @param keys Must be lesser or equal to 3.
230
 * @param item Pointer to a hash table item.
239
 * @param item Pointer to a hash table item.
231
 * @return Non-zero if the key matches the item, zero otherwise.
240
 * @return Non-zero if the key matches the item, zero otherwise.
232
 */
241
 */
233
int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
242
int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
234
{
243
{
235
    hashed_service_t *hs;
244
    hashed_service_t *hs;
236
 
245
 
237
    assert(key);
246
    assert(key);
238
    assert(keys <= 3);
247
    assert(keys <= 3);
239
    assert(item);
248
    assert(item);
240
   
249
   
241
    hs = hash_table_get_instance(item, hashed_service_t, link);
250
    hs = hash_table_get_instance(item, hashed_service_t, link);
242
   
251
   
243
    if (keys == 2)
252
    if (keys == 2)
244
        return key[1] == hs->in_phone_hash;
253
        return key[1] == hs->in_phone_hash;
245
    else
254
    else
246
        return key[0] == hs->service;
255
        return key[0] == hs->service;
247
}
256
}
248
 
257
 
249
/** Perform actions after removal of item from the hash table.
258
/** Perform actions after removal of item from the hash table.
250
 *
259
 *
251
 * @param item Item that was removed from the hash table.
260
 * @param item Item that was removed from the hash table.
252
 */
261
 */
253
void ns_remove(link_t *item)
262
void ns_remove(link_t *item)
254
{
263
{
255
    assert(item);
264
    assert(item);
256
    free(hash_table_get_instance(item, hashed_service_t, link));
265
    free(hash_table_get_instance(item, hashed_service_t, link));
257
}
266
}
258
 
267
 
259
/**
268
/**
260
 * @}
269
 * @}
261
 */
270
 */
262
 
271