Subversion Repositories HelenOS-historic

Rev

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

Rev 1336 Rev 1338
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.h>
34
#include <ipc.h>
35
#include <stdio.h>
35
#include <stdio.h>
36
#include <unistd.h>
36
#include <unistd.h>
37
#include <stdlib.h>
37
#include <stdlib.h>
38
#include <ns.h>
38
#include <ns.h>
39
#include <errno.h>
39
#include <errno.h>
40
#include <assert.h>
40
#include <assert.h>
41
#include <libadt/list.h>
41
#include <libadt/list.h>
42
#include <libadt/hash_table.h>
42
#include <libadt/hash_table.h>
43
 
43
 
44
#define NAME    "NS"
44
#define NAME    "NS"
45
 
45
 
46
#define NS_HASH_TABLE_CHAINS    20
46
#define NS_HASH_TABLE_CHAINS    20
47
 
47
 
48
static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
48
static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
49
static int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid);
49
static int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid);
50
 
50
 
51
/* Static functions implementing NS hash table operations. */
51
/* Static functions implementing NS hash table operations. */
52
static hash_index_t ns_hash(unsigned long *key);
52
static hash_index_t ns_hash(unsigned long *key);
53
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
53
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
54
static void ns_remove(link_t *item);
54
static void ns_remove(link_t *item);
55
 
55
 
56
/** Operations for NS hash table. */
56
/** Operations for NS hash table. */
57
static hash_table_operations_t ns_hash_table_ops = {
57
static hash_table_operations_t ns_hash_table_ops = {
58
    .hash = ns_hash,
58
    .hash = ns_hash,
59
    .compare = ns_compare,
59
    .compare = ns_compare,
60
    .remove_callback = ns_remove
60
    .remove_callback = ns_remove
61
};
61
};
62
 
62
 
63
/** NS hash table structure. */
63
/** NS hash table structure. */
64
static hash_table_t ns_hash_table;
64
static hash_table_t ns_hash_table;
65
 
65
 
66
/** NS hash table item. */
66
/** NS hash table item. */
67
typedef struct {
67
typedef struct {
68
    link_t link;
68
    link_t link;
69
    ipcarg_t service;       /**< Number of the service. */
69
    ipcarg_t service;       /**< Number of the service. */
70
    ipcarg_t phone;         /**< Phone registered with the service. */
70
    ipcarg_t phone;         /**< Phone registered with the service. */
71
    ipcarg_t in_phone_hash;     /**< Incoming phone hash. */
71
    ipcarg_t in_phone_hash;     /**< Incoming phone hash. */
72
} hashed_service_t;
72
} hashed_service_t;
73
 
73
 
74
/*
74
/*
75
irq_cmd_t msim_cmds[1] = {
75
irq_cmd_t msim_cmds[1] = {
76
    { CMD_MEM_READ_1, (void *)0xB0000000, 0 }
76
    { CMD_MEM_READ_1, (void *)0xB0000000, 0 }
77
};
77
};
78
 
78
 
79
irq_code_t msim_kbd = {
79
irq_code_t msim_kbd = {
80
    1,
80
    1,
81
    msim_cmds
81
    msim_cmds
82
};
82
};
83
*/
83
*/
84
/*
84
/*
85
irq_cmd_t i8042_cmds[1] = {
85
irq_cmd_t i8042_cmds[1] = {
86
    { CMD_PORT_READ_1, (void *)0x60, 0 }
86
    { CMD_PORT_READ_1, (void *)0x60, 0 }
87
};
87
};
88
 
88
 
89
irq_code_t i8042_kbd = {
89
irq_code_t i8042_kbd = {
90
    1,
90
    1,
91
    i8042_cmds
91
    i8042_cmds
92
};
92
};
93
*/
93
*/
94
 
94
 
95
 
95
 
96
int static ping_phone;
96
int static ping_phone;
97
 
97
 
98
int main(int argc, char **argv)
98
int main(int argc, char **argv)
99
{
99
{
100
    ipc_call_t call;
100
    ipc_call_t call;
101
    ipc_callid_t callid;
101
    ipc_callid_t callid;
102
    char *as;
102
    char *as;
103
   
103
   
104
    ipcarg_t retval, arg1, arg2;
104
    ipcarg_t retval, arg1, arg2;
105
 
105
 
106
    printf("%s: Name service started.\n", NAME);
106
    printf("%s: Naming service started.\n", NAME);
107
   
107
   
108
    if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, &ns_hash_table_ops)) {
108
    if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, &ns_hash_table_ops)) {
109
        printf("%s: cannot create hash table\n", NAME);
109
        printf("%s: cannot create hash table\n", NAME);
110
        return ENOMEM;
110
        return ENOMEM;
111
    }
111
    }
112
   
112
   
113
   
113
   
114
//  ipc_register_irq(2, &msim_kbd);
114
//  ipc_register_irq(2, &msim_kbd);
115
//  ipc_register_irq(1, &i8042_kbd);
115
//  ipc_register_irq(1, &i8042_kbd);
116
    while (1) {
116
    while (1) {
117
        callid = ipc_wait_for_call(&call, 0);
117
        callid = ipc_wait_for_call(&call, 0);
118
        printf("NS: Call in_phone_hash=%lX...", call.in_phone_hash);
118
        printf("NS: Call in_phone_hash=%lX...", call.in_phone_hash);
119
        switch (IPC_GET_METHOD(call)) {
119
        switch (IPC_GET_METHOD(call)) {
120
        case IPC_M_AS_SEND:
120
        case IPC_M_AS_SEND:
121
            as = (char *)IPC_GET_ARG2(call);
121
            as = (char *)IPC_GET_ARG2(call);
122
            printf("Received as: %P, size:%d\n", as, IPC_GET_ARG3(call));
122
            printf("Received as: %P, size:%d\n", as, IPC_GET_ARG3(call));
123
            retval = ipc_answer(callid, 0,(sysarg_t)(1024*1024), 0);
123
            retval = ipc_answer(callid, 0,(sysarg_t)(1024*1024), 0);
124
            if (!retval) {
124
            if (!retval) {
125
                printf("Reading shared memory...");
125
                printf("Reading shared memory...");
126
                printf("Text: %s", as);
126
                printf("Text: %s", as);
127
            } else
127
            } else
128
                printf("Failed answer: %d\n", retval);
128
                printf("Failed answer: %d\n", retval);
129
            continue;
129
            continue;
130
            break;
130
            break;
131
        case IPC_M_INTERRUPT:
131
        case IPC_M_INTERRUPT:
132
            printf("GOT INTERRUPT: %c\n", IPC_GET_ARG2(call));
132
            printf("GOT INTERRUPT: %c\n", IPC_GET_ARG2(call));
133
            break;
133
            break;
134
        case IPC_M_PHONE_HUNGUP:
134
        case IPC_M_PHONE_HUNGUP:
135
            printf("Phone hung up.\n");
135
            printf("Phone hung up.\n");
136
            retval = 0;
136
            retval = 0;
137
            break;
137
            break;
138
        case IPC_M_CONNECT_TO_ME:
138
        case IPC_M_CONNECT_TO_ME:
139
            /*
139
            /*
140
             * Server requests service registration.
140
             * Server requests service registration.
141
             */
141
             */
142
            retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG3(call), &call);
142
            retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG3(call), &call);
143
            ping_phone = IPC_GET_ARG3(call);
143
            ping_phone = IPC_GET_ARG3(call);
144
            break;
144
            break;
145
        case IPC_M_CONNECT_ME_TO:
145
        case IPC_M_CONNECT_ME_TO:
146
            /*
146
            /*
147
             * Client requests to be connected to a service.
147
             * Client requests to be connected to a service.
148
             */
148
             */
149
            retval = connect_to_service(IPC_GET_ARG1(call), &call, callid);
149
            retval = connect_to_service(IPC_GET_ARG1(call), &call, callid);
150
            break;
150
            break;
-
 
151
        case NS_HANGUP:
-
 
152
            printf("Closing connection.\n");
-
 
153
            retval = EHANGUP;
-
 
154
            break;
151
        case NS_PING:
155
        case NS_PING:
152
            printf("Ping...%P %P\n", IPC_GET_ARG1(call),
156
            printf("Ping...%P %P\n", IPC_GET_ARG1(call),
153
                   IPC_GET_ARG2(call));
157
                   IPC_GET_ARG2(call));
154
            retval = 0;
158
            retval = 0;
155
            arg1 = 0xdead;
159
            arg1 = 0xdead;
156
            arg2 = 0xbeef;
160
            arg2 = 0xbeef;
157
            break;
161
            break;
158
        case NS_HANGUP:
-
 
159
            printf("Closing connection.\n");
-
 
160
            retval = EHANGUP;
-
 
161
            break;
-
 
162
        case NS_PING_SVC:
162
        case NS_PING_SVC:
163
            printf("NS:Pinging service %d\n", ping_phone);
163
            printf("NS:Pinging service %d\n", ping_phone);
164
            ipc_call_sync(ping_phone, NS_PING, 0xbeef, 0);
164
            ipc_call_sync(ping_phone, NS_PING, 0xbeef, 0);
165
            printf("NS:Got pong\n");
165
            printf("NS:Got pong\n");
166
            break;
166
            break;
167
        default:
167
        default:
168
            printf("Unknown method: %zd\n", IPC_GET_METHOD(call));
168
            printf("Unknown method: %zd\n", IPC_GET_METHOD(call));
169
            retval = ENOENT;
169
            retval = ENOENT;
170
            break;
170
            break;
171
        }
171
        }
172
        if (! (callid & IPC_CALLID_NOTIFICATION)) {
172
        if (! (callid & IPC_CALLID_NOTIFICATION)) {
173
            printf("Answering.\n");
173
            printf("Answering.\n");
174
            ipc_answer(callid, retval, arg1, arg2);
174
            ipc_answer(callid, retval, arg1, arg2);
175
        }
175
        }
176
    }
176
    }
177
}
177
}
178
 
178
 
179
/** Register server.
179
/** Register service.
180
 *
180
 *
181
 * @param service Service to be registered.
181
 * @param service Service to be registered.
182
 * @param phone phone Phone to be used for connections to the service.
182
 * @param phone phone Phone to be used for connections to the service.
183
 * @param call Pointer to call structure.
183
 * @param call Pointer to call structure.
184
 *
184
 *
185
 * @return Zero on success or a value from @ref errno.h.
185
 * @return Zero on success or a value from @ref errno.h.
186
 */
186
 */
187
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
187
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
188
{
188
{
189
    unsigned long keys[3] = { service, call->in_phone_hash, 0 };
189
    unsigned long keys[3] = { service, call->in_phone_hash, 0 };
190
    hashed_service_t *hs;
190
    hashed_service_t *hs;
191
           
191
           
192
    printf("Registering service %d on phone %d...", service, phone);
192
    printf("Registering service %d on phone %d...", service, phone);
193
 
193
 
194
    if (hash_table_find(&ns_hash_table, keys)) {
194
    if (hash_table_find(&ns_hash_table, keys)) {
195
        printf("Service %d already registered.\n", service);
195
        printf("Service %d already registered.\n", service);
196
        return EEXISTS;
196
        return EEXISTS;
197
    }
197
    }
198
           
198
           
199
    hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
199
    hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
200
    if (!hs) {
200
    if (!hs) {
201
        printf("Failed to register service %d.\n", service);
201
        printf("Failed to register service %d.\n", service);
202
        return ENOMEM;
202
        return ENOMEM;
203
    }
203
    }
204
           
204
           
205
    link_initialize(&hs->link);
205
    link_initialize(&hs->link);
206
    hs->service = service;
206
    hs->service = service;
207
    hs->phone = phone;
207
    hs->phone = phone;
208
    hs->in_phone_hash = call->in_phone_hash;
208
    hs->in_phone_hash = call->in_phone_hash;
209
    hash_table_insert(&ns_hash_table, keys, &hs->link);
209
    hash_table_insert(&ns_hash_table, keys, &hs->link);
210
           
210
           
211
    return 0;
211
    return 0;
212
}
212
}
213
 
213
 
214
/** Connect client to service.
214
/** Connect client to service.
215
 *
215
 *
216
 * @param service Service to be connected to.
216
 * @param service Service to be connected to.
217
 * @param call Pointer to call structure.
217
 * @param call Pointer to call structure.
218
 * @param callid Call ID of the request.
218
 * @param callid Call ID of the request.
219
 *
219
 *
220
 * @return Zero on success or a value from @ref errno.h.
220
 * @return Zero on success or a value from @ref errno.h.
221
 */
221
 */
222
int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
222
int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
223
{
223
{
224
    unsigned long keys[3] = { service, 0, 0 };
224
    unsigned long keys[3] = { service, 0, 0 };
225
    link_t *hlp;
225
    link_t *hlp;
226
    hashed_service_t *hs;
226
    hashed_service_t *hs;
227
           
227
           
228
    hlp = hash_table_find(&ns_hash_table, keys);
228
    hlp = hash_table_find(&ns_hash_table, keys);
229
    if (!hlp) {
229
    if (!hlp) {
230
        printf("Service %d noty registered.\n", service);
230
        printf("Service %d not registered.\n", service);
231
        return ENOENT;
231
        return ENOENT;
232
    }
232
    }
233
    hs = hash_table_get_instance(hlp, hashed_service_t, link);
233
    hs = hash_table_get_instance(hlp, hashed_service_t, link);
234
    printf("Connecting in_phone_hash=%lX to service at phone %d...", call->in_phone_hash, hs->phone);
234
    printf("Connecting in_phone_hash=%lX to service at phone %d...", call->in_phone_hash, hs->phone);
235
    return ipc_forward_fast(callid, hs->phone, 0, 0);
235
    return ipc_forward_fast(callid, hs->phone, 0, 0);
236
}
236
}
237
 
237
 
238
/** Compute hash index into NS hash table.
238
/** Compute hash index into NS hash table.
239
 *
239
 *
240
 * @param key Pointer keys. However, only the first key (i.e. service number)
240
 * @param key Pointer keys. However, only the first key (i.e. service number)
241
 *        is used to compute the hash index.
241
 *        is used to compute the hash index.
242
 * @return Hash index corresponding to key[0].
242
 * @return Hash index corresponding to key[0].
243
 */
243
 */
244
hash_index_t ns_hash(unsigned long *key)
244
hash_index_t ns_hash(unsigned long *key)
245
{
245
{
246
    assert(key);
246
    assert(key);
247
    return *key % NS_HASH_TABLE_CHAINS;
247
    return *key % NS_HASH_TABLE_CHAINS;
248
}
248
}
249
 
249
 
250
/** Compare a key with hashed item.
250
/** Compare a key with hashed item.
251
 *
251
 *
252
 * This compare function always ignores the third key.
252
 * This compare function always ignores the third key.
253
 * It exists only to make it possible to remove records
253
 * It exists only to make it possible to remove records
254
 * originating from connection with key[1] in_phone_hash
254
 * originating from connection with key[1] in_phone_hash
255
 * value. Note that this is close to being classified
255
 * value. Note that this is close to being classified
256
 * as a nasty hack.
256
 * as a nasty hack.
257
 *
257
 *
258
 * @param key Array of keys.
258
 * @param key Array of keys.
259
 * @param keys Must be lesser or equal to 3.
259
 * @param keys Must be lesser or equal to 3.
260
 * @param item Pointer to a hash table item.
260
 * @param item Pointer to a hash table item.
261
 * @return Non-zero if the key matches the item, zero otherwise.
261
 * @return Non-zero if the key matches the item, zero otherwise.
262
 */
262
 */
263
int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
263
int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
264
{
264
{
265
    hashed_service_t *hs;
265
    hashed_service_t *hs;
266
 
266
 
267
    assert(key);
267
    assert(key);
268
    assert(keys <= 3);
268
    assert(keys <= 3);
269
    assert(item);
269
    assert(item);
270
   
270
   
271
    hs = hash_table_get_instance(item, hashed_service_t, link);
271
    hs = hash_table_get_instance(item, hashed_service_t, link);
272
   
272
   
273
    if (keys == 2)
273
    if (keys == 2)
274
        return key[1] == hs->in_phone_hash;
274
        return key[1] == hs->in_phone_hash;
275
    else
275
    else
276
        return key[0] == hs->service;
276
        return key[0] == hs->service;
277
}
277
}
278
 
278
 
279
/** Perform actions after removal of item from the hash table.
279
/** Perform actions after removal of item from the hash table.
280
 *
280
 *
281
 * @param item Item that was removed from the hash table.
281
 * @param item Item that was removed from the hash table.
282
 */
282
 */
283
void ns_remove(link_t *item)
283
void ns_remove(link_t *item)
284
{
284
{
285
    assert(item);
285
    assert(item);
286
    free(hash_table_get_instance(item, hashed_service_t, link));
286
    free(hash_table_get_instance(item, hashed_service_t, link));
287
}
287
}
288
 
288