Subversion Repositories HelenOS

Rev

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

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