Subversion Repositories HelenOS-historic

Rev

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

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