Subversion Repositories HelenOS

Rev

Rev 3424 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3424 Rev 4377
Line 26... Line 26...
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 <bool.h>
43
#include <unistd.h>
44
#include <unistd.h>
44
#include <stdlib.h>
45
#include <stdlib.h>
45
#include <errno.h>
46
#include <errno.h>
46
#include <assert.h>
47
#include <assert.h>
47
#include <libadt/list.h>
48
#include <libadt/list.h>
48
#include <libadt/hash_table.h>
49
#include <libadt/hash_table.h>
49
#include <sysinfo.h>
50
#include <sysinfo.h>
-
 
51
#include <loader/loader.h>
50
#include <ddi.h>
52
#include <ddi.h>
51
#include <as.h>
53
#include <as.h>
52
 
54
 
53
#define NAME    "ns"
55
#define NAME  "ns"
54
 
56
 
55
#define NS_HASH_TABLE_CHAINS    20
57
#define NS_HASH_TABLE_CHAINS  20
56
 
58
 
57
static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
59
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,
60
static void connect_to_service(ipcarg_t service, ipc_call_t *call,
-
 
61
    ipc_callid_t callid);
-
 
62
 
-
 
63
void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
-
 
64
    ipc_callid_t callid);
-
 
65
void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
59
    ipc_callid_t callid);
66
    ipc_callid_t callid);
60
 
67
 
-
 
68
 
61
/* Static functions implementing NS hash table operations. */
69
/* Static functions implementing NS hash table operations. */
62
static hash_index_t ns_hash(unsigned long *key);
70
static hash_index_t ns_hash(unsigned long *key);
63
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
71
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
64
static void ns_remove(link_t *item);
72
static void ns_remove(link_t *item);
65
 
73
 
Line 74... Line 82...
74
static hash_table_t ns_hash_table;
82
static hash_table_t ns_hash_table;
75
 
83
 
76
/** NS hash table item. */
84
/** NS hash table item. */
77
typedef struct {
85
typedef struct {
78
    link_t link;
86
    link_t link;
79
    ipcarg_t service;       /**< Number of the service. */
87
    ipcarg_t service;        /**< Number of the service. */
80
    ipcarg_t phone;         /**< Phone registered with the service. */
88
    ipcarg_t phone;          /**< Phone registered with the service. */
81
    ipcarg_t in_phone_hash;     /**< Incoming phone hash. */
89
    ipcarg_t in_phone_hash;  /**< Incoming phone hash. */
82
} hashed_service_t;
90
} hashed_service_t;
83
 
91
 
-
 
92
/** Pending connection structure. */
-
 
93
typedef struct {
-
 
94
    link_t link;
-
 
95
    ipcarg_t service;        /**< Number of the service. */
-
 
96
    ipc_callid_t callid;     /**< Call ID waiting for the connection */
-
 
97
    ipcarg_t arg2;           /**< Second argument */
-
 
98
    ipcarg_t arg3;           /**< Third argument */
-
 
99
} pending_req_t;
-
 
100
 
-
 
101
static link_t pending_req;
-
 
102
 
-
 
103
/** Request for connection to a clonable service. */
-
 
104
typedef struct {
-
 
105
    link_t link;
-
 
106
    ipcarg_t service;
-
 
107
    ipc_call_t call;
-
 
108
    ipc_callid_t callid;
-
 
109
} cs_req_t;
-
 
110
 
-
 
111
/** List of clonable-service connection requests. */
-
 
112
static link_t cs_req;
-
 
113
 
84
static void *clockaddr = NULL;
114
static void *clockaddr = NULL;
85
static void *klogaddr = NULL;
115
static void *klogaddr = NULL;
86
 
116
 
87
static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name,
117
/** Return true if @a service is clonable. */
88
    void **addr)
118
static bool service_clonable(int service)
89
{
119
{
90
    void *ph_addr;
120
    return (service == SERVICE_LOAD);
-
 
121
}
91
 
122
 
-
 
123
static void get_as_area(ipc_callid_t callid, ipc_call_t *call, void *ph_addr, count_t pages, void **addr)
-
 
124
{
-
 
125
    if (ph_addr == NULL) {
-
 
126
        ipc_answer_0(callid, ENOENT);
-
 
127
        return;
-
 
128
    }
-
 
129
   
92
    if (!*addr) {
130
    if (*addr == NULL) {
93
        ph_addr = (void *) sysinfo_value(name);
131
        *addr = as_get_mappable_page(pages * PAGE_SIZE);
-
 
132
       
94
        if (!ph_addr) {
133
        if (*addr == NULL) {
-
 
134
            ipc_answer_0(callid, ENOENT);
-
 
135
            return;
-
 
136
        }
-
 
137
       
-
 
138
        if (physmem_map(ph_addr, *addr, pages,
-
 
139
            AS_AREA_READ | AS_AREA_CACHEABLE) != 0) {
95
            ipc_answer_0(callid, ENOENT);
140
            ipc_answer_0(callid, ENOENT);
96
            return;
141
            return;
97
        }
142
        }
98
        *addr = as_get_mappable_page(PAGE_SIZE);
-
 
99
        physmem_map(ph_addr, *addr, 1,
-
 
100
            AS_AREA_READ | AS_AREA_CACHEABLE);
-
 
101
    }
143
    }
-
 
144
   
102
    ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ);
145
    ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ);
103
}
146
}
104
 
147
 
-
 
148
/** Process pending connection requests */
-
 
149
static void process_pending_req()
-
 
150
{
-
 
151
    link_t *cur;
-
 
152
   
-
 
153
loop:
-
 
154
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
-
 
155
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
-
 
156
       
-
 
157
        unsigned long keys[3] = {
-
 
158
            pr->service,
-
 
159
            0,
-
 
160
            0
-
 
161
        };
-
 
162
       
-
 
163
        link_t *link = hash_table_find(&ns_hash_table, keys);
-
 
164
        if (!link)
-
 
165
            continue;
-
 
166
       
-
 
167
        hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
-
 
168
        ipcarg_t retval = ipc_forward_fast(pr->callid, hs->phone,
-
 
169
            pr->arg2, pr->arg3, 0, IPC_FF_NONE);
-
 
170
       
-
 
171
        if (!(pr->callid & IPC_CALLID_NOTIFICATION))
-
 
172
            ipc_answer_0(pr->callid, retval);
-
 
173
       
-
 
174
        list_remove(cur);
-
 
175
        free(pr);
-
 
176
        goto loop;
-
 
177
    }
-
 
178
}
-
 
179
 
105
int main(int argc, char **argv)
180
int main(int argc, char **argv)
106
{
181
{
107
    printf(NAME ": HelenOS IPC Naming Service\n");
182
    printf(NAME ": HelenOS IPC Naming Service\n");
108
   
183
   
109
    ipc_call_t call;
-
 
110
    ipc_callid_t callid;
-
 
111
   
-
 
112
    ipcarg_t retval;
-
 
113
 
-
 
114
    if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
184
    if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
115
        &ns_hash_table_ops)) {
185
        &ns_hash_table_ops)) {
116
        printf(NAME ": No memory available\n");
186
        printf(NAME ": No memory available for services\n");
117
        return ENOMEM;
187
        return ENOMEM;
118
    }
188
    }
119
   
189
   
-
 
190
    list_initialize(&pending_req);
-
 
191
    list_initialize(&cs_req);
-
 
192
   
120
    printf(NAME ": Accepting connections\n");
193
    printf(NAME ": Accepting connections\n");
121
    while (1) {
194
    while (true) {
-
 
195
        process_pending_req();
-
 
196
       
-
 
197
        ipc_call_t call;
122
        callid = ipc_wait_for_call(&call);
198
        ipc_callid_t callid = ipc_wait_for_call(&call);
-
 
199
        ipcarg_t retval;
-
 
200
       
123
        switch (IPC_GET_METHOD(call)) {
201
        switch (IPC_GET_METHOD(call)) {
124
        case IPC_M_SHARE_IN:
202
        case IPC_M_SHARE_IN:
125
            switch (IPC_GET_ARG3(call)) {
203
            switch (IPC_GET_ARG3(call)) {
126
            case SERVICE_MEM_REALTIME:
204
            case SERVICE_MEM_REALTIME:
127
                get_as_area(callid, &call, "clock.faddr",
205
                get_as_area(callid, &call, sysinfo_value("clock.faddr"), 1, &clockaddr);
128
                    &clockaddr);
-
 
129
                break;
206
                break;
130
            case SERVICE_MEM_KLOG:
207
            case SERVICE_MEM_KLOG:
131
                get_as_area(callid, &call, "klog.faddr",
208
                get_as_area(callid, &call, sysinfo_value("klog.faddr"), sysinfo_value("klog.pages"), &klogaddr);
132
                    &klogaddr);
-
 
133
                break;
209
                break;
134
            default:
210
            default:
135
                ipc_answer_0(callid, ENOENT);
211
                ipc_answer_0(callid, ENOENT);
136
            }
212
            }
137
            continue;
213
            continue;
Line 140... Line 216...
140
            break;
216
            break;
141
        case IPC_M_CONNECT_TO_ME:
217
        case IPC_M_CONNECT_TO_ME:
142
            /*
218
            /*
143
             * Server requests service registration.
219
             * Server requests service registration.
144
             */
220
             */
-
 
221
            if (service_clonable(IPC_GET_ARG1(call))) {
-
 
222
                register_clonable(IPC_GET_ARG1(call),
-
 
223
                    IPC_GET_ARG5(call), &call, callid);
-
 
224
                continue;
-
 
225
            } else {
145
            retval = register_service(IPC_GET_ARG1(call),
226
                retval = register_service(IPC_GET_ARG1(call),
146
                IPC_GET_ARG5(call), &call);
227
                    IPC_GET_ARG5(call), &call);
-
 
228
            }
147
            break;
229
            break;
148
        case IPC_M_CONNECT_ME_TO:
230
        case IPC_M_CONNECT_ME_TO:
149
            /*
231
            /*
150
             * Client requests to be connected to a service.
232
             * Client requests to be connected to a service.
151
             */
233
             */
-
 
234
            if (service_clonable(IPC_GET_ARG1(call))) {
-
 
235
                connect_to_clonable(IPC_GET_ARG1(call),
-
 
236
                    &call, callid);
-
 
237
                continue;
-
 
238
            } else {
152
            retval = connect_to_service(IPC_GET_ARG1(call), &call,
239
                connect_to_service(IPC_GET_ARG1(call), &call,
153
                callid);
240
                    callid);
-
 
241
                continue;
-
 
242
            }
154
            break;
243
            break;
155
        default:
244
        default:
156
            retval = ENOENT;
245
            retval = ENOENT;
157
            break;
246
            break;
158
        }
247
        }
-
 
248
       
159
        if (!(callid & IPC_CALLID_NOTIFICATION)) {
249
        if (!(callid & IPC_CALLID_NOTIFICATION))
160
            ipc_answer_0(callid, retval);
250
            ipc_answer_0(callid, retval);
161
        }
-
 
162
    }
251
    }
163
   
252
   
164
    /* Not reached */
253
    /* Not reached */
165
    return 0;
254
    return 0;
166
}
255
}
167
 
256
 
168
/** Register service.
257
/** Register service.
169
 *
258
 *
170
 * @param service Service to be registered.
259
 * @param service Service to be registered.
171
 * @param phone Phone to be used for connections to the service.
260
 * @param phone   Phone to be used for connections to the service.
172
 * @param call Pointer to call structure.
261
 * @param call    Pointer to call structure.
173
 *
262
 *
174
 * @return Zero on success or a value from @ref errno.h.
263
 * @return Zero on success or a value from @ref errno.h.
-
 
264
 *
175
 */
265
 */
176
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
266
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
177
{
267
{
178
    unsigned long keys[3] = {
268
    unsigned long keys[3] = {
179
        service,
269
        service,
180
        call->in_phone_hash,
270
        call->in_phone_hash,
181
        0
271
        0
182
    };
272
    };
183
    hashed_service_t *hs;
-
 
184
           
273
   
185
    if (hash_table_find(&ns_hash_table, keys)) {
274
    if (hash_table_find(&ns_hash_table, keys))
186
        return EEXISTS;
275
        return EEXISTS;
187
    }
276
   
188
           
-
 
189
    hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
277
    hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
190
    if (!hs) {
278
    if (!hs)
191
        return ENOMEM;
279
        return ENOMEM;
192
    }
280
   
193
           
-
 
194
    link_initialize(&hs->link);
281
    link_initialize(&hs->link);
195
    hs->service = service;
282
    hs->service = service;
196
    hs->phone = phone;
283
    hs->phone = phone;
197
    hs->in_phone_hash = call->in_phone_hash;
284
    hs->in_phone_hash = call->in_phone_hash;
198
    hash_table_insert(&ns_hash_table, keys, &hs->link);
285
    hash_table_insert(&ns_hash_table, keys, &hs->link);
199
           
286
   
200
    return 0;
287
    return 0;
201
}
288
}
202
 
289
 
203
/** Connect client to service.
290
/** Connect client to service.
204
 *
291
 *
205
 * @param service Service to be connected to.
292
 * @param service Service to be connected to.
206
 * @param call Pointer to call structure.
293
 * @param call    Pointer to call structure.
207
 * @param callid Call ID of the request.
294
 * @param callid  Call ID of the request.
208
 *
295
 *
209
 * @return Zero on success or a value from @ref errno.h.
296
 * @return Zero on success or a value from @ref errno.h.
-
 
297
 *
210
 */
298
 */
211
int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
299
void connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
212
{
300
{
-
 
301
    ipcarg_t retval;
213
    unsigned long keys[3] = { service, 0, 0 };
302
    unsigned long keys[3] = {
-
 
303
        service,
-
 
304
        0,
-
 
305
        0
-
 
306
    };
-
 
307
   
-
 
308
    link_t *link = hash_table_find(&ns_hash_table, keys);
-
 
309
    if (!link) {
-
 
310
        if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
-
 
311
            /* Blocking connection, add to pending list */
-
 
312
            pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
214
    link_t *hlp;
313
            if (!pr) {
215
    hashed_service_t *hs;
314
                retval = ENOMEM;
-
 
315
                goto out;
-
 
316
            }
216
           
317
           
-
 
318
            pr->service = service;
-
 
319
            pr->callid = callid;
-
 
320
            pr->arg2 = IPC_GET_ARG2(*call);
-
 
321
            pr->arg3 = IPC_GET_ARG3(*call);
217
    hlp = hash_table_find(&ns_hash_table, keys);
322
            list_append(&pr->link, &pending_req);
218
    if (!hlp) {
323
            return;
-
 
324
        }
219
        return ENOENT;
325
        retval = ENOENT;
-
 
326
        goto out;
220
    }
327
    }
-
 
328
   
221
    hs = hash_table_get_instance(hlp, hashed_service_t, link);
329
    hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
222
    return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
330
    retval = ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
223
        IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
331
        IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
-
 
332
out:
-
 
333
    if (!(callid & IPC_CALLID_NOTIFICATION))
-
 
334
        ipc_answer_0(callid, retval);
-
 
335
}
-
 
336
 
-
 
337
/** Register clonable service.
-
 
338
 *
-
 
339
 * @param service Service to be registered.
-
 
340
 * @param phone   Phone to be used for connections to the service.
-
 
341
 * @param call    Pointer to call structure.
-
 
342
 *
-
 
343
 */
-
 
344
void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
-
 
345
    ipc_callid_t callid)
-
 
346
{
-
 
347
    if (list_empty(&cs_req)) {
-
 
348
        /* There was no pending connection request. */
-
 
349
        printf(NAME ": Unexpected clonable server.\n");
-
 
350
        ipc_answer_0(callid, EBUSY);
-
 
351
        return;
-
 
352
    }
-
 
353
   
-
 
354
    cs_req_t *csr = list_get_instance(cs_req.next, cs_req_t, link);
-
 
355
    list_remove(&csr->link);
-
 
356
   
-
 
357
    /* Currently we can only handle a single type of clonable service. */
-
 
358
    assert(csr->service == SERVICE_LOAD);
-
 
359
   
-
 
360
    ipc_answer_0(callid, EOK);
-
 
361
   
-
 
362
    int rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call),
-
 
363
        IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE);
-
 
364
 
-
 
365
    free(csr);
-
 
366
    ipc_hangup(phone);
-
 
367
}
-
 
368
 
-
 
369
/** Connect client to clonable service.
-
 
370
 *
-
 
371
 * @param service Service to be connected to.
-
 
372
 * @param call    Pointer to call structure.
-
 
373
 * @param callid  Call ID of the request.
-
 
374
 *
-
 
375
 * @return Zero on success or a value from @ref errno.h.
-
 
376
 *
-
 
377
 */
-
 
378
void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
-
 
379
    ipc_callid_t callid)
-
 
380
{
-
 
381
    assert(service == SERVICE_LOAD);
-
 
382
   
-
 
383
    cs_req_t *csr = malloc(sizeof(cs_req_t));
-
 
384
    if (csr == NULL) {
-
 
385
        ipc_answer_0(callid, ENOMEM);
-
 
386
        return;
-
 
387
    }
-
 
388
   
-
 
389
    /* Spawn a loader. */
-
 
390
    int rc = loader_spawn("loader");
-
 
391
   
-
 
392
    if (rc < 0) {
-
 
393
        free(csr);
-
 
394
        ipc_answer_0(callid, rc);
-
 
395
        return;
-
 
396
    }
-
 
397
   
-
 
398
    csr->service = service;
-
 
399
    csr->call = *call;
-
 
400
    csr->callid = callid;
-
 
401
   
-
 
402
    /*
-
 
403
     * We can forward the call only after the server we spawned connects
-
 
404
     * to us. Meanwhile we might need to service more connection requests.
-
 
405
     * Thus we store the call in a queue.
-
 
406
     */
-
 
407
    list_append(&csr->link, &cs_req);
224
}
408
}
225
 
409
 
226
/** Compute hash index into NS hash table.
410
/** Compute hash index into NS hash table.
227
 *
411
 *
228
 * @param key Pointer keys. However, only the first key (i.e. service number)
412
 * @param key Pointer keys. However, only the first key (i.e. service number)
229
 *        is used to compute the hash index.
413
 *            is used to compute the hash index.
-
 
414
 *
230
 * @return Hash index corresponding to key[0].
415
 * @return Hash index corresponding to key[0].
-
 
416
 *
231
 */
417
 */
232
hash_index_t ns_hash(unsigned long *key)
418
hash_index_t ns_hash(unsigned long *key)
233
{
419
{
234
    assert(key);
420
    assert(key);
235
    return *key % NS_HASH_TABLE_CHAINS;
421
    return (*key % NS_HASH_TABLE_CHAINS);
236
}
422
}
237
 
423
 
238
/** Compare a key with hashed item.
424
/** Compare a key with hashed item.
239
 *
425
 *
240
 * This compare function always ignores the third key.
426
 * This compare function always ignores the third key.
241
 * It exists only to make it possible to remove records
427
 * It exists only to make it possible to remove records
242
 * originating from connection with key[1] in_phone_hash
428
 * originating from connection with key[1] in_phone_hash
243
 * value. Note that this is close to being classified
429
 * value. Note that this is close to being classified
244
 * as a nasty hack.
430
 * as a nasty hack.
245
 *
431
 *
246
 * @param key Array of keys.
432
 * @param key  Array of keys.
247
 * @param keys Must be lesser or equal to 3.
433
 * @param keys Must be lesser or equal to 3.
248
 * @param item Pointer to a hash table item.
434
 * @param item Pointer to a hash table item.
-
 
435
 *
249
 * @return Non-zero if the key matches the item, zero otherwise.
436
 * @return Non-zero if the key matches the item, zero otherwise.
-
 
437
 *
250
 */
438
 */
251
int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
439
int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
252
{
440
{
253
    hashed_service_t *hs;
-
 
254
 
-
 
255
    assert(key);
441
    assert(key);
256
    assert(keys <= 3);
442
    assert(keys <= 3);
257
    assert(item);
443
    assert(item);
258
   
444
   
259
    hs = hash_table_get_instance(item, hashed_service_t, link);
445
    hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
260
   
446
   
261
    if (keys == 2)
447
    if (keys == 2)
262
        return key[1] == hs->in_phone_hash;
448
        return key[1] == hs->in_phone_hash;
263
    else
449
    else
264
        return key[0] == hs->service;
450
        return key[0] == hs->service;
265
}
451
}
266
 
452
 
267
/** Perform actions after removal of item from the hash table.
453
/** Perform actions after removal of item from the hash table.
268
 *
454
 *
269
 * @param item Item that was removed from the hash table.
455
 * @param item Item that was removed from the hash table.
-
 
456
 *
270
 */
457
 */
271
void ns_remove(link_t *item)
458
void ns_remove(link_t *item)
272
{
459
{
273
    assert(item);
460
    assert(item);
274
    free(hash_table_get_instance(item, hashed_service_t, link));
461
    free(hash_table_get_instance(item, hashed_service_t, link));
275
}
462
}
276
 
463
 
277
/**
464
/**
278
 * @}
465
 * @}
279
 */
466
 */