Rev 4509 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4509 | Rev 4581 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Martin Decky |
2 | * Copyright (c) 2009 Martin Decky |
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 | /** @addtogroup ns |
29 | /** @addtogroup ns |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | #include <ipc/ipc.h> |
33 | #include <ipc/ipc.h> |
34 | #include <ipc/services.h> |
34 | #include <ipc/services.h> |
35 | #include <adt/list.h> |
35 | #include <adt/list.h> |
36 | #include <bool.h> |
36 | #include <bool.h> |
37 | #include <errno.h> |
37 | #include <errno.h> |
38 | #include <assert.h> |
38 | #include <assert.h> |
39 | #include <stdio.h> |
39 | #include <stdio.h> |
40 | #include <malloc.h> |
40 | #include <malloc.h> |
41 | #include <loader/loader.h> |
41 | #include <loader/loader.h> |
42 | #include "clonable.h" |
42 | #include "clonable.h" |
43 | #include "ns.h" |
43 | #include "ns.h" |
44 | 44 | ||
45 | /** Request for connection to a clonable service. */ |
45 | /** Request for connection to a clonable service. */ |
46 | typedef struct { |
46 | typedef struct { |
47 | link_t link; |
47 | link_t link; |
48 | ipcarg_t service; |
48 | ipcarg_t service; |
49 | ipc_call_t call; |
49 | ipc_call_t call; |
50 | ipc_callid_t callid; |
50 | ipc_callid_t callid; |
51 | } cs_req_t; |
51 | } cs_req_t; |
52 | 52 | ||
53 | /** List of clonable-service connection requests. */ |
53 | /** List of clonable-service connection requests. */ |
54 | static link_t cs_req; |
54 | static link_t cs_req; |
55 | 55 | ||
56 | int clonable_init(void) |
56 | int clonable_init(void) |
57 | { |
57 | { |
58 | list_initialize(&cs_req); |
58 | list_initialize(&cs_req); |
59 | return EOK; |
59 | return EOK; |
60 | } |
60 | } |
61 | 61 | ||
62 | /** Return true if @a service is clonable. */ |
62 | /** Return true if @a service is clonable. */ |
63 | bool service_clonable(int service) |
63 | bool service_clonable(int service) |
64 | { |
64 | { |
65 | return (service == SERVICE_LOAD); |
65 | return (service == SERVICE_LOAD); |
66 | } |
66 | } |
67 | 67 | ||
68 | /** Register clonable service. |
68 | /** Register clonable service. |
69 | * |
69 | * |
70 | * @param service Service to be registered. |
70 | * @param service Service to be registered. |
71 | * @param phone Phone to be used for connections to the service. |
71 | * @param phone Phone to be used for connections to the service. |
72 | * @param call Pointer to call structure. |
72 | * @param call Pointer to call structure. |
73 | * |
73 | * |
74 | */ |
74 | */ |
75 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
75 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
76 | ipc_callid_t callid) |
76 | ipc_callid_t callid) |
77 | { |
77 | { |
78 | if (list_empty(&cs_req)) { |
78 | if (list_empty(&cs_req)) { |
79 | /* There was no pending connection request. */ |
79 | /* There was no pending connection request. */ |
80 | printf(NAME ": Unexpected clonable server.\n"); |
80 | printf(NAME ": Unexpected clonable server.\n"); |
81 | ipc_answer_0(callid, EBUSY); |
81 | ipc_answer_0(callid, EBUSY); |
82 | return; |
82 | return; |
83 | } |
83 | } |
84 | 84 | ||
85 | cs_req_t *csr = list_get_instance(cs_req.next, cs_req_t, link); |
85 | cs_req_t *csr = list_get_instance(cs_req.next, cs_req_t, link); |
86 | list_remove(&csr->link); |
86 | list_remove(&csr->link); |
87 | 87 | ||
88 | /* Currently we can only handle a single type of clonable service. */ |
88 | /* Currently we can only handle a single type of clonable service. */ |
89 | assert(csr->service == SERVICE_LOAD); |
89 | assert(csr->service == SERVICE_LOAD); |
90 | 90 | ||
91 | ipc_answer_0(callid, EOK); |
91 | ipc_answer_0(callid, EOK); |
92 | 92 | ||
93 | ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call), |
93 | ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call), |
94 | IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE); |
94 | IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE); |
95 | 95 | ||
96 | free(csr); |
96 | free(csr); |
97 | ipc_hangup(phone); |
97 | ipc_hangup(phone); |
98 | } |
98 | } |
99 | 99 | ||
100 | /** Connect client to clonable service. |
100 | /** Connect client to clonable service. |
101 | * |
101 | * |
102 | * @param service Service to be connected to. |
102 | * @param service Service to be connected to. |
103 | * @param call Pointer to call structure. |
103 | * @param call Pointer to call structure. |
104 | * @param callid Call ID of the request. |
104 | * @param callid Call ID of the request. |
105 | * |
105 | * |
106 | * @return Zero on success or a value from @ref errno.h. |
106 | * @return Zero on success or a value from @ref errno.h. |
107 | * |
107 | * |
108 | */ |
108 | */ |
109 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
109 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
110 | ipc_callid_t callid) |
110 | ipc_callid_t callid) |
111 | { |
111 | { |
112 | assert(service == SERVICE_LOAD); |
112 | assert(service == SERVICE_LOAD); |
113 | 113 | ||
114 | cs_req_t *csr = malloc(sizeof(cs_req_t)); |
114 | cs_req_t *csr = malloc(sizeof(cs_req_t)); |
115 | if (csr == NULL) { |
115 | if (csr == NULL) { |
116 | ipc_answer_0(callid, ENOMEM); |
116 | ipc_answer_0(callid, ENOMEM); |
117 | return; |
117 | return; |
118 | } |
118 | } |
119 | 119 | ||
120 | /* Spawn a loader. */ |
120 | /* Spawn a loader. */ |
121 | int rc = loader_spawn("loader"); |
121 | int rc = loader_spawn("loader"); |
122 | 122 | ||
123 | if (rc < 0) { |
123 | if (rc < 0) { |
124 | free(csr); |
124 | free(csr); |
125 | ipc_answer_0(callid, rc); |
125 | ipc_answer_0(callid, rc); |
126 | return; |
126 | return; |
127 | } |
127 | } |
128 | 128 | ||
129 | csr->service = service; |
129 | csr->service = service; |
130 | csr->call = *call; |
130 | csr->call = *call; |
131 | csr->callid = callid; |
131 | csr->callid = callid; |
132 | 132 | ||
133 | /* |
133 | /* |
134 | * We can forward the call only after the server we spawned connects |
134 | * We can forward the call only after the server we spawned connects |
135 | * to us. Meanwhile we might need to service more connection requests. |
135 | * to us. Meanwhile we might need to service more connection requests. |
136 | * Thus we store the call in a queue. |
136 | * Thus we store the call in a queue. |
137 | */ |
137 | */ |
138 | list_append(&csr->link, &cs_req); |
138 | list_append(&csr->link, &cs_req); |
139 | } |
139 | } |
140 | 140 | ||
141 | /** |
141 | /** |
142 | * @} |
142 | * @} |
143 | */ |
143 | */ |
144 | 144 |