Subversion Repositories HelenOS-historic

Rev

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

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