Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4327 | Rev 4581 | ||
---|---|---|---|
Line 28... | Line 28... | ||
28 | 28 | ||
29 | /** @addtogroup libc |
29 | /** @addtogroup libc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <ipc/ipc.h> |
35 | #include <ipc/ipc.h> |
36 | #include <ipc/loader.h> |
36 | #include <ipc/loader.h> |
37 | #include <ipc/services.h> |
37 | #include <ipc/services.h> |
38 | #include <libc.h> |
38 | #include <libc.h> |
Line 45... | Line 45... | ||
45 | #include <loader/loader.h> |
45 | #include <loader/loader.h> |
46 | 46 | ||
47 | /** Connect to a new program loader. |
47 | /** Connect to a new program loader. |
48 | * |
48 | * |
49 | * Spawns a new program loader task and returns the connection structure. |
49 | * Spawns a new program loader task and returns the connection structure. |
- | 50 | * |
|
50 | * @param name Symbolic name to set on the newly created task. |
51 | * @param name Symbolic name to set on the newly created task. |
- | 52 | * |
|
51 | * @return Pointer to the loader connection structure (should be |
53 | * @return Pointer to the loader connection structure (should be |
52 | * de-allocated using free() after use). |
54 | * deallocated using free() after use). |
- | 55 | * |
|
53 | */ |
56 | */ |
54 | int loader_spawn(const char *name) |
57 | int loader_spawn(const char *name) |
55 | { |
58 | { |
56 | return __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER, |
59 | return __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER, |
57 | (sysarg_t) name, str_size(name)); |
60 | (sysarg_t) name, str_size(name)); |
58 | } |
61 | } |
59 | 62 | ||
60 | loader_t *loader_connect(void) |
63 | loader_t *loader_connect(void) |
61 | { |
64 | { |
62 | loader_t *ldr; |
- | |
63 | int phone_id; |
- | |
64 | - | ||
65 | phone_id = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_LOAD, 0, 0); |
65 | int phone_id = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_LOAD, 0, 0); |
66 | if (phone_id < 0) |
66 | if (phone_id < 0) |
67 | return NULL; |
67 | return NULL; |
68 | 68 | ||
69 | ldr = malloc(sizeof(loader_t)); |
69 | loader_t *ldr = malloc(sizeof(loader_t)); |
70 | if (ldr == NULL) |
70 | if (ldr == NULL) |
71 | return NULL; |
71 | return NULL; |
72 | 72 | ||
73 | ldr->phone_id = phone_id; |
73 | ldr->phone_id = phone_id; |
74 | return ldr; |
74 | return ldr; |
75 | } |
75 | } |
76 | 76 | ||
77 | /** Get ID of the new task. |
77 | /** Get ID of the new task. |
78 | * |
78 | * |
79 | * Retrieves the ID of the new task from the loader. |
79 | * Retrieves the ID of the new task from the loader. |
80 | * |
80 | * |
81 | * @param ldr Loader connection structure. |
81 | * @param ldr Loader connection structure. |
82 | * @param task_id Points to a variable where the ID should be stored. |
82 | * @param task_id Points to a variable where the ID should be stored. |
- | 83 | * |
|
83 | * @return Zero on success or negative error code. |
84 | * @return Zero on success or negative error code. |
- | 85 | * |
|
84 | */ |
86 | */ |
85 | int loader_get_task_id(loader_t *ldr, task_id_t *task_id) |
87 | int loader_get_task_id(loader_t *ldr, task_id_t *task_id) |
86 | { |
88 | { |
87 | ipc_call_t answer; |
- | |
88 | aid_t req; |
- | |
89 | int rc; |
- | |
90 | ipcarg_t retval; |
- | |
91 | - | ||
92 | /* Get task ID. */ |
89 | /* Get task ID. */ |
- | 90 | ipc_call_t answer; |
|
93 | req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer); |
91 | aid_t req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer); |
94 | rc = ipc_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t)); |
92 | int rc = ipc_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t)); |
95 | if (rc != EOK) { |
93 | if (rc != EOK) { |
96 | async_wait_for(req, NULL); |
94 | async_wait_for(req, NULL); |
97 | return rc; |
95 | return rc; |
98 | } |
96 | } |
99 | 97 | ||
- | 98 | ipcarg_t retval; |
|
100 | async_wait_for(req, &retval); |
99 | async_wait_for(req, &retval); |
101 | return (int)retval; |
100 | return (int) retval; |
102 | } |
101 | } |
103 | 102 | ||
104 | /** Set pathname of the program to load. |
103 | /** Set pathname of the program to load. |
105 | * |
104 | * |
106 | * Sets the name of the program file to load. The name can be relative |
105 | * Sets the name of the program file to load. The name can be relative |
107 | * to the current working directory (it will be absolutized before |
106 | * to the current working directory (it will be absolutized before |
108 | * sending to the loader). |
107 | * sending to the loader). |
109 | * |
108 | * |
110 | * @param ldr Loader connection structure. |
109 | * @param ldr Loader connection structure. |
111 | * @param path Pathname of the program file. |
110 | * @param path Pathname of the program file. |
- | 111 | * |
|
112 | * @return Zero on success or negative error code. |
112 | * @return Zero on success or negative error code. |
- | 113 | * |
|
113 | */ |
114 | */ |
114 | int loader_set_pathname(loader_t *ldr, const char *path) |
115 | int loader_set_pathname(loader_t *ldr, const char *path) |
115 | { |
116 | { |
116 | ipc_call_t answer; |
- | |
117 | aid_t req; |
- | |
118 | int rc; |
- | |
119 | ipcarg_t retval; |
- | |
120 | - | ||
121 | char *pa; |
- | |
122 | size_t pa_len; |
117 | size_t pa_len; |
123 | - | ||
124 | pa = absolutize(path, &pa_len); |
118 | char *pa = absolutize(path, &pa_len); |
125 | if (!pa) |
119 | if (!pa) |
126 | return 0; |
120 | return 0; |
127 | 121 | ||
128 | /* Send program pathname */ |
122 | /* Send program pathname */ |
- | 123 | ipc_call_t answer; |
|
129 | req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer); |
124 | aid_t req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer); |
130 | rc = ipc_data_write_start(ldr->phone_id, (void *)pa, pa_len); |
125 | int rc = ipc_data_write_start(ldr->phone_id, (void *) pa, pa_len); |
131 | if (rc != EOK) { |
126 | if (rc != EOK) { |
132 | async_wait_for(req, NULL); |
127 | async_wait_for(req, NULL); |
133 | return rc; |
128 | return rc; |
134 | } |
129 | } |
135 | 130 | ||
136 | free(pa); |
131 | free(pa); |
137 | 132 | ||
- | 133 | ipcarg_t retval; |
|
138 | async_wait_for(req, &retval); |
134 | async_wait_for(req, &retval); |
139 | return (int)retval; |
135 | return (int) retval; |
140 | } |
136 | } |
141 | 137 | ||
142 | - | ||
143 | /** Set command-line arguments for the program. |
138 | /** Set command-line arguments for the program. |
144 | * |
139 | * |
145 | * Sets the vector of command-line arguments to be passed to the loaded |
140 | * Sets the vector of command-line arguments to be passed to the loaded |
146 | * program. By convention, the very first argument is typically the same as |
141 | * program. By convention, the very first argument is typically the same as |
147 | * the command used to execute the program. |
142 | * the command used to execute the program. |
148 | * |
143 | * |
149 | * @param ldr Loader connection structure. |
144 | * @param ldr Loader connection structure. |
150 | * @param argv NULL-terminated array of pointers to arguments. |
145 | * @param argv NULL-terminated array of pointers to arguments. |
- | 146 | * |
|
151 | * @return Zero on success or negative error code. |
147 | * @return Zero on success or negative error code. |
- | 148 | * |
|
152 | */ |
149 | */ |
153 | int loader_set_args(loader_t *ldr, char *const argv[]) |
150 | int loader_set_args(loader_t *ldr, char *const argv[]) |
154 | { |
151 | { |
155 | aid_t req; |
- | |
156 | ipc_call_t answer; |
- | |
157 | ipcarg_t rc; |
- | |
158 | - | ||
159 | char *const *ap; |
- | |
160 | char *dp; |
- | |
161 | char *arg_buf; |
- | |
162 | size_t buffer_size; |
- | |
163 | - | ||
164 | /* |
152 | /* |
165 | * Serialize the arguments into a single array. First |
153 | * Serialize the arguments into a single array. First |
166 | * compute size of the buffer needed. |
154 | * compute size of the buffer needed. |
167 | */ |
155 | */ |
168 | ap = argv; |
156 | char *const *ap = argv; |
169 | buffer_size = 0; |
157 | size_t buffer_size = 0; |
170 | while (*ap != NULL) { |
158 | while (*ap != NULL) { |
171 | buffer_size += str_size(*ap) + 1; |
159 | buffer_size += str_size(*ap) + 1; |
172 | ++ap; |
160 | ap++; |
173 | } |
161 | } |
174 | 162 | ||
175 | arg_buf = malloc(buffer_size); |
163 | char *arg_buf = malloc(buffer_size); |
176 | if (arg_buf == NULL) return ENOMEM; |
164 | if (arg_buf == NULL) |
- | 165 | return ENOMEM; |
|
177 | 166 | ||
178 | /* Now fill the buffer with null-terminated argument strings */ |
167 | /* Now fill the buffer with null-terminated argument strings */ |
179 | ap = argv; |
168 | ap = argv; |
180 | dp = arg_buf; |
169 | char *dp = arg_buf; |
181 | 170 | ||
182 | while (*ap != NULL) { |
171 | while (*ap != NULL) { |
183 | str_cpy(dp, buffer_size - (dp - arg_buf), *ap); |
172 | str_cpy(dp, buffer_size - (dp - arg_buf), *ap); |
184 | dp += str_size(*ap) + 1; |
173 | dp += str_size(*ap) + 1; |
185 | - | ||
186 | ++ap; |
174 | ap++; |
187 | } |
175 | } |
188 | 176 | ||
189 | /* Send serialized arguments to the loader */ |
177 | /* Send serialized arguments to the loader */ |
190 | - | ||
- | 178 | ipc_call_t answer; |
|
191 | req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer); |
179 | aid_t req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer); |
192 | rc = ipc_data_write_start(ldr->phone_id, (void *)arg_buf, buffer_size); |
180 | ipcarg_t rc = ipc_data_write_start(ldr->phone_id, (void *) arg_buf, buffer_size); |
193 | if (rc != EOK) { |
181 | if (rc != EOK) { |
194 | async_wait_for(req, NULL); |
182 | async_wait_for(req, NULL); |
195 | return rc; |
183 | return rc; |
196 | } |
184 | } |
197 | 185 | ||
198 | async_wait_for(req, &rc); |
186 | async_wait_for(req, &rc); |
199 | if (rc != EOK) return rc; |
187 | if (rc != EOK) |
- | 188 | return rc; |
|
200 | 189 | ||
201 | /* Free temporary buffer */ |
190 | /* Free temporary buffer */ |
202 | free(arg_buf); |
191 | free(arg_buf); |
- | 192 | ||
- | 193 | return EOK; |
|
- | 194 | } |
|
203 | 195 | ||
- | 196 | /** Set preset files for the program. |
|
- | 197 | * |
|
- | 198 | * Sets the vector of preset files to be passed to the loaded |
|
- | 199 | * program. By convention, the first three files represent stdin, |
|
- | 200 | * stdout and stderr respectively. |
|
- | 201 | * |
|
- | 202 | * @param ldr Loader connection structure. |
|
- | 203 | * @param files NULL-terminated array of pointers to files. |
|
- | 204 | * |
|
- | 205 | * @return Zero on success or negative error code. |
|
- | 206 | * |
|
- | 207 | */ |
|
- | 208 | int loader_set_files(loader_t *ldr, fdi_node_t *const files[]) |
|
- | 209 | { |
|
- | 210 | /* |
|
- | 211 | * Serialize the arguments into a single array. First |
|
- | 212 | * compute size of the buffer needed. |
|
- | 213 | */ |
|
- | 214 | fdi_node_t *const *ap = files; |
|
- | 215 | size_t count = 0; |
|
- | 216 | while (*ap != NULL) { |
|
- | 217 | count++; |
|
- | 218 | ap++; |
|
- | 219 | } |
|
- | 220 | ||
- | 221 | fdi_node_t *files_buf; |
|
- | 222 | files_buf = (fdi_node_t *) malloc(count * sizeof(fdi_node_t)); |
|
- | 223 | if (files_buf == NULL) |
|
- | 224 | return ENOMEM; |
|
- | 225 | ||
- | 226 | /* Fill the buffer */ |
|
- | 227 | size_t i; |
|
- | 228 | for (i = 0; i < count; i++) |
|
- | 229 | files_buf[i] = *files[i]; |
|
- | 230 | ||
- | 231 | /* Send serialized files to the loader */ |
|
- | 232 | ipc_call_t answer; |
|
- | 233 | aid_t req = async_send_0(ldr->phone_id, LOADER_SET_FILES, &answer); |
|
- | 234 | ipcarg_t rc = ipc_data_write_start(ldr->phone_id, (void *) files_buf, |
|
- | 235 | count * sizeof(fdi_node_t)); |
|
- | 236 | if (rc != EOK) { |
|
- | 237 | async_wait_for(req, NULL); |
|
- | 238 | return rc; |
|
- | 239 | } |
|
- | 240 | ||
- | 241 | async_wait_for(req, &rc); |
|
- | 242 | if (rc != EOK) |
|
- | 243 | return rc; |
|
- | 244 | ||
- | 245 | /* Free temporary buffer */ |
|
- | 246 | free(files_buf); |
|
- | 247 | ||
204 | return EOK; |
248 | return EOK; |
205 | } |
249 | } |
206 | 250 | ||
207 | /** Instruct loader to load the program. |
251 | /** Instruct loader to load the program. |
208 | * |
252 | * |
209 | * If this function succeeds, the program has been successfully loaded |
253 | * If this function succeeds, the program has been successfully loaded |
210 | * and is ready to be executed. |
254 | * and is ready to be executed. |
211 | * |
255 | * |
212 | * @param ldr Loader connection structure. |
256 | * @param ldr Loader connection structure. |
- | 257 | * |
|
213 | * @return Zero on success or negative error code. |
258 | * @return Zero on success or negative error code. |
- | 259 | * |
|
214 | */ |
260 | */ |
215 | int loader_load_program(loader_t *ldr) |
261 | int loader_load_program(loader_t *ldr) |
216 | { |
262 | { |
217 | int rc; |
- | |
218 | - | ||
219 | rc = async_req_0_0(ldr->phone_id, LOADER_LOAD); |
263 | return (int) async_req_0_0(ldr->phone_id, LOADER_LOAD); |
220 | if (rc != EOK) |
- | |
221 | return rc; |
- | |
222 | - | ||
223 | return EOK; |
- | |
224 | } |
264 | } |
225 | 265 | ||
226 | /** Instruct loader to execute the program. |
266 | /** Instruct loader to execute the program. |
227 | * |
267 | * |
228 | * Note that this function blocks until the loader actually replies |
268 | * Note that this function blocks until the loader actually replies |
Line 230... | Line 270... | ||
230 | * the task and its thread is stopped. |
270 | * the task and its thread is stopped. |
231 | * |
271 | * |
232 | * After using this function, no further operations must be performed |
272 | * After using this function, no further operations must be performed |
233 | * on the loader structure. It should be de-allocated using free(). |
273 | * on the loader structure. It should be de-allocated using free(). |
234 | * |
274 | * |
235 | * @param ldr Loader connection structure. |
275 | * @param ldr Loader connection structure. |
- | 276 | * |
|
236 | * @return Zero on success or negative error code. |
277 | * @return Zero on success or negative error code. |
- | 278 | * |
|
237 | */ |
279 | */ |
238 | int loader_run(loader_t *ldr) |
280 | int loader_run(loader_t *ldr) |
239 | { |
281 | { |
240 | int rc; |
- | |
241 | - | ||
242 | rc = async_req_0_0(ldr->phone_id, LOADER_RUN); |
282 | int rc = async_req_0_0(ldr->phone_id, LOADER_RUN); |
243 | if (rc != EOK) |
283 | if (rc != EOK) |
244 | return rc; |
284 | return rc; |
245 | 285 | ||
246 | ipc_hangup(ldr->phone_id); |
286 | ipc_hangup(ldr->phone_id); |
247 | ldr->phone_id = 0; |
287 | ldr->phone_id = 0; |
248 | return EOK; |
288 | return EOK; |
249 | } |
289 | } |
250 | 290 | ||
Line 252... | Line 292... | ||
252 | * |
292 | * |
253 | * Tells the loader not to load any program and terminate. |
293 | * Tells the loader not to load any program and terminate. |
254 | * After using this function, no further operations must be performed |
294 | * After using this function, no further operations must be performed |
255 | * on the loader structure. It should be de-allocated using free(). |
295 | * on the loader structure. It should be de-allocated using free(). |
256 | * |
296 | * |
257 | * @param ldr Loader connection structure. |
297 | * @param ldr Loader connection structure. |
- | 298 | * |
|
258 | * @return Zero on success or negative error code. |
299 | * @return Zero on success or negative error code. |
- | 300 | * |
|
259 | */ |
301 | */ |
260 | void loader_abort(loader_t *ldr) |
302 | void loader_abort(loader_t *ldr) |
261 | { |
303 | { |
262 | ipc_hangup(ldr->phone_id); |
304 | ipc_hangup(ldr->phone_id); |
263 | ldr->phone_id = 0; |
305 | ldr->phone_id = 0; |