Subversion Repositories HelenOS

Rev

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

Rev 3150 Rev 4343
Line 38... Line 38...
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
 
Line 56... Line 58...
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 int connect_to_service(ipcarg_t service, ipc_call_t *call,
59
    ipc_callid_t callid);
61
    ipc_callid_t callid);
60
 
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,
-
 
66
    ipc_callid_t callid);
-
 
67
 
61
/* Static functions implementing NS hash table operations. */
68
/* Static functions implementing NS hash table operations. */
62
static hash_index_t ns_hash(unsigned long *key);
69
static hash_index_t ns_hash(unsigned long *key);
63
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
70
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
64
static void ns_remove(link_t *item);
71
static void ns_remove(link_t *item);
65
 
72
 
Line 82... Line 89...
82
} hashed_service_t;
89
} hashed_service_t;
83
 
90
 
84
static void *clockaddr = NULL;
91
static void *clockaddr = NULL;
85
static void *klogaddr = NULL;
92
static void *klogaddr = NULL;
86
 
93
 
87
static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name,
94
/** Request for connection to a clonable service. */
-
 
95
typedef struct {
-
 
96
    link_t link;
-
 
97
    ipcarg_t service;
-
 
98
    ipc_call_t call;
88
    void **addr)
99
    ipc_callid_t callid;
-
 
100
} cs_req_t;
-
 
101
 
-
 
102
/** List of clonable-service connection requests. */
-
 
103
static link_t cs_req;
-
 
104
 
-
 
105
/** Return true if @a service is clonable. */
-
 
106
static bool service_clonable(int service)
89
{
107
{
90
    void *ph_addr;
108
    return service == SERVICE_LOAD;
-
 
109
}
91
 
110
 
-
 
111
static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name, void **addr)
-
 
112
{
-
 
113
    void *ph_addr;
-
 
114
   
92
    if (!*addr) {
115
    if (!*addr) {
93
        ph_addr = (void *) sysinfo_value(name);
116
        ph_addr = (void *) sysinfo_value(name);
94
        if (!ph_addr) {
117
        if (!ph_addr) {
95
            ipc_answer_0(callid, ENOENT);
118
            ipc_answer_0(callid, ENOENT);
96
            return;
119
            return;
97
        }
120
        }
98
        *addr = as_get_mappable_page(PAGE_SIZE);
121
        *addr = as_get_mappable_page(PAGE_SIZE);
99
        physmem_map(ph_addr, *addr, 1,
122
        if (physmem_map(ph_addr, *addr, 1,
100
            AS_AREA_READ | AS_AREA_CACHEABLE);
123
            AS_AREA_READ | AS_AREA_CACHEABLE) != 0) {
-
 
124
            ipc_answer_0(callid, ENOENT);
-
 
125
            return;
-
 
126
        }
101
    }
127
    }
102
    ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ);
128
    ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ);
103
}
129
}
104
 
130
 
105
int main(int argc, char **argv)
131
int main(int argc, char **argv)
Line 114... Line 140...
114
    if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
140
    if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
115
        &ns_hash_table_ops)) {
141
        &ns_hash_table_ops)) {
116
        printf(NAME ": No memory available\n");
142
        printf(NAME ": No memory available\n");
117
        return ENOMEM;
143
        return ENOMEM;
118
    }
144
    }
-
 
145
 
-
 
146
    list_initialize(&cs_req);
119
   
147
   
120
    printf(NAME ": Accepting connections\n");
148
    printf(NAME ": Accepting connections\n");
121
    while (1) {
149
    while (1) {
122
        callid = ipc_wait_for_call(&call);
150
        callid = ipc_wait_for_call(&call);
123
        switch (IPC_GET_METHOD(call)) {
151
        switch (IPC_GET_METHOD(call)) {
124
        case IPC_M_SHARE_IN:
152
        case IPC_M_SHARE_IN:
125
            switch (IPC_GET_ARG3(call)) {
153
            switch (IPC_GET_ARG3(call)) {
126
            case SERVICE_MEM_REALTIME:
154
            case SERVICE_MEM_REALTIME:
127
                get_as_area(callid, &call, "clock.faddr",
155
                get_as_area(callid, &call, "clock.faddr", &clockaddr);
128
                    &clockaddr);
-
 
129
                break;
156
                break;
130
            case SERVICE_MEM_KLOG:
157
            case SERVICE_MEM_KLOG:
131
                get_as_area(callid, &call, "klog.faddr",
158
                get_as_area(callid, &call, "klog.faddr", &klogaddr);
132
                    &klogaddr);
-
 
133
                break;
159
                break;
134
            default:
160
            default:
135
                ipc_answer_0(callid, ENOENT);
161
                ipc_answer_0(callid, ENOENT);
136
            }
162
            }
137
            continue;
163
            continue;
Line 140... Line 166...
140
            break;
166
            break;
141
        case IPC_M_CONNECT_TO_ME:
167
        case IPC_M_CONNECT_TO_ME:
142
            /*
168
            /*
143
             * Server requests service registration.
169
             * Server requests service registration.
144
             */
170
             */
-
 
171
            if (service_clonable(IPC_GET_ARG1(call))) {
-
 
172
                register_clonable(IPC_GET_ARG1(call),
-
 
173
                    IPC_GET_ARG5(call), &call, callid);
-
 
174
                continue;
-
 
175
            } else {
145
            retval = register_service(IPC_GET_ARG1(call),
176
                retval = register_service(IPC_GET_ARG1(call),
146
                IPC_GET_ARG5(call), &call);
177
                    IPC_GET_ARG5(call), &call);
-
 
178
            }
147
            break;
179
            break;
148
        case IPC_M_CONNECT_ME_TO:
180
        case IPC_M_CONNECT_ME_TO:
149
            /*
181
            /*
150
             * Client requests to be connected to a service.
182
             * Client requests to be connected to a service.
151
             */
183
             */
-
 
184
            if (service_clonable(IPC_GET_ARG1(call))) {
-
 
185
                connect_to_clonable(IPC_GET_ARG1(call),
-
 
186
                    &call, callid);
-
 
187
                continue;
-
 
188
            } else {
152
            retval = connect_to_service(IPC_GET_ARG1(call), &call,
189
                retval = connect_to_service(IPC_GET_ARG1(call),
153
                callid);
190
                    &call, callid);
-
 
191
            }
154
            break;
192
            break;
155
        default:
193
        default:
156
            retval = ENOENT;
194
            retval = ENOENT;
157
            break;
195
            break;
158
        }
196
        }
Line 165... Line 203...
165
    return 0;
203
    return 0;
166
}
204
}
167
 
205
 
168
/** Register service.
206
/** Register service.
169
 *
207
 *
170
 * @param service Service to be registered.
208
 * @param service   Service to be registered.
171
 * @param phone Phone to be used for connections to the service.
209
 * @param phone     Phone to be used for connections to the service.
172
 * @param call Pointer to call structure.
210
 * @param call      Pointer to call structure.
173
 *
211
 *
174
 * @return Zero on success or a value from @ref errno.h.
212
 * @return      Zero on success or a value from @ref errno.h.
175
 */
213
 */
176
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
214
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
177
{
215
{
178
    unsigned long keys[3] = {
216
    unsigned long keys[3] = {
179
        service,
217
        service,
180
        call->in_phone_hash,
218
        call->in_phone_hash,
181
        0
219
        0
182
    };
220
    };
183
    hashed_service_t *hs;
221
    hashed_service_t *hs;
184
           
222
 
185
    if (hash_table_find(&ns_hash_table, keys)) {
223
    if (hash_table_find(&ns_hash_table, keys)) {
186
        return EEXISTS;
224
        return EEXISTS;
187
    }
225
    }
188
           
226
           
189
    hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
227
    hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
Line 200... Line 238...
200
    return 0;
238
    return 0;
201
}
239
}
202
 
240
 
203
/** Connect client to service.
241
/** Connect client to service.
204
 *
242
 *
205
 * @param service Service to be connected to.
243
 * @param service   Service to be connected to.
206
 * @param call Pointer to call structure.
244
 * @param call      Pointer to call structure.
207
 * @param callid Call ID of the request.
245
 * @param callid    Call ID of the request.
208
 *
246
 *
209
 * @return Zero on success or a value from @ref errno.h.
247
 * @return Zero on success or a value from @ref errno.h.
210
 */
248
 */
211
int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
249
int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
212
{
250
{
213
    unsigned long keys[3] = { service, 0, 0 };
251
    unsigned long keys[3] = { service, 0, 0 };
214
    link_t *hlp;
252
    link_t *hlp;
215
    hashed_service_t *hs;
253
    hashed_service_t *hs;
216
           
254
 
217
    hlp = hash_table_find(&ns_hash_table, keys);
255
    hlp = hash_table_find(&ns_hash_table, keys);
218
    if (!hlp) {
256
    if (!hlp) {
219
        return ENOENT;
257
        return ENOENT;
220
    }
258
    }
221
    hs = hash_table_get_instance(hlp, hashed_service_t, link);
259
    hs = hash_table_get_instance(hlp, hashed_service_t, link);
222
    return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
260
    return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
223
        IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
261
        IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
224
}
262
}
225
 
263
 
-
 
264
/** Register clonable service.
-
 
265
 *
-
 
266
 * @param service   Service to be registered.
-
 
267
 * @param phone     Phone to be used for connections to the service.
-
 
268
 * @param call      Pointer to call structure.
-
 
269
 */
-
 
270
void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
-
 
271
    ipc_callid_t callid)
-
 
272
{
-
 
273
    int rc;
-
 
274
    cs_req_t *csr;
-
 
275
 
-
 
276
    if (list_empty(&cs_req)) {
-
 
277
        /* There was no pending connection request. */
-
 
278
        printf(NAME ": Unexpected clonable server.\n");
-
 
279
        ipc_answer_0(callid, EBUSY);
-
 
280
        return;
-
 
281
    }
-
 
282
 
-
 
283
    csr = list_get_instance(cs_req.next, cs_req_t, link);
-
 
284
    list_remove(&csr->link);
-
 
285
 
-
 
286
    /* Currently we can only handle a single type of clonable service. */
-
 
287
    assert(csr->service == SERVICE_LOAD);
-
 
288
 
-
 
289
    ipc_answer_0(callid, EOK);
-
 
290
 
-
 
291
    rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call),
-
 
292
        IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE);
-
 
293
 
-
 
294
    free(csr);
-
 
295
}
-
 
296
 
-
 
297
/** Connect client to clonable service.
-
 
298
 *
-
 
299
 * @param service   Service to be connected to.
-
 
300
 * @param call      Pointer to call structure.
-
 
301
 * @param callid    Call ID of the request.
-
 
302
 *
-
 
303
 * @return      Zero on success or a value from @ref errno.h.
-
 
304
 */
-
 
305
void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
-
 
306
    ipc_callid_t callid)
-
 
307
{
-
 
308
    int rc;
-
 
309
    cs_req_t *csr;
-
 
310
 
-
 
311
    assert(service == SERVICE_LOAD);
-
 
312
 
-
 
313
    csr = malloc(sizeof(cs_req_t));
-
 
314
    if (csr == NULL) {
-
 
315
        ipc_answer_0(callid, ENOMEM);
-
 
316
        return;
-
 
317
    }
-
 
318
 
-
 
319
    /* Spawn a loader. */
-
 
320
    rc = loader_spawn("loader");
-
 
321
 
-
 
322
    if (rc < 0) {
-
 
323
        free(csr);
-
 
324
        ipc_answer_0(callid, rc);
-
 
325
        return;
-
 
326
    }
-
 
327
 
-
 
328
    csr->service = service;
-
 
329
    csr->call = *call;
-
 
330
    csr->callid = callid;
-
 
331
 
-
 
332
    /*
-
 
333
     * We can forward the call only after the server we spawned connects
-
 
334
     * to us. Meanwhile we might need to service more connection requests.
-
 
335
     * Thus we store the call in a queue.
-
 
336
     */
-
 
337
    list_append(&csr->link, &cs_req);
-
 
338
}
-
 
339
 
226
/** Compute hash index into NS hash table.
340
/** Compute hash index into NS hash table.
227
 *
341
 *
228
 * @param key Pointer keys. However, only the first key (i.e. service number)
342
 * @param key Pointer keys. However, only the first key (i.e. service number)
229
 *        is used to compute the hash index.
343
 *        is used to compute the hash index.
230
 * @return Hash index corresponding to key[0].
344
 * @return Hash index corresponding to key[0].