1 | /*
|
---|
2 | * Copyright (c) 2008 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
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
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <ipc/loader.h>
|
---|
36 | #include <ipc/services.h>
|
---|
37 | #include <ns.h>
|
---|
38 | #include <libc.h>
|
---|
39 | #include <task.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <vfs/vfs.h>
|
---|
45 | #include <loader/loader.h>
|
---|
46 | #include "private/loader.h"
|
---|
47 |
|
---|
48 | /** Connect to a new program loader.
|
---|
49 | *
|
---|
50 | * Spawns a new program loader task and returns the connection structure.
|
---|
51 | *
|
---|
52 | * @param name Symbolic name to set on the newly created task.
|
---|
53 | *
|
---|
54 | * @return Error code.
|
---|
55 | */
|
---|
56 | int loader_spawn(const char *name)
|
---|
57 | {
|
---|
58 | return __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER,
|
---|
59 | (sysarg_t) name, str_size(name));
|
---|
60 | }
|
---|
61 |
|
---|
62 | loader_t *loader_connect(void)
|
---|
63 | {
|
---|
64 | loader_t *ldr = malloc(sizeof(loader_t));
|
---|
65 | if (ldr == NULL)
|
---|
66 | return NULL;
|
---|
67 |
|
---|
68 | async_sess_t *sess =
|
---|
69 | service_connect_blocking(SERVICE_LOADER, INTERFACE_LOADER, 0);
|
---|
70 | if (sess == NULL) {
|
---|
71 | free(ldr);
|
---|
72 | return NULL;
|
---|
73 | }
|
---|
74 |
|
---|
75 | ldr->sess = sess;
|
---|
76 | return ldr;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Get ID of the new task.
|
---|
80 | *
|
---|
81 | * Retrieves the ID of the new task from the loader.
|
---|
82 | *
|
---|
83 | * @param ldr Loader connection structure.
|
---|
84 | * @param task_id Points to a variable where the ID should be stored.
|
---|
85 | *
|
---|
86 | * @return Zero on success or negative error code.
|
---|
87 | *
|
---|
88 | */
|
---|
89 | int loader_get_task_id(loader_t *ldr, task_id_t *task_id)
|
---|
90 | {
|
---|
91 | /* Get task ID. */
|
---|
92 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
93 |
|
---|
94 | ipc_call_t answer;
|
---|
95 | aid_t req = async_send_0(exch, LOADER_GET_TASKID, &answer);
|
---|
96 | int rc = async_data_read_start(exch, task_id, sizeof(task_id_t));
|
---|
97 |
|
---|
98 | async_exchange_end(exch);
|
---|
99 |
|
---|
100 | if (rc != EOK) {
|
---|
101 | async_forget(req);
|
---|
102 | return (int) rc;
|
---|
103 | }
|
---|
104 |
|
---|
105 | async_wait_for(req, &rc);
|
---|
106 | return (int) rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Set current working directory for the loaded task.
|
---|
110 | *
|
---|
111 | * Sets the current working directory for the loaded task.
|
---|
112 | *
|
---|
113 | * @param ldr Loader connection structure.
|
---|
114 | *
|
---|
115 | * @return Zero on success or negative error code.
|
---|
116 | *
|
---|
117 | */
|
---|
118 | int loader_set_cwd(loader_t *ldr)
|
---|
119 | {
|
---|
120 | char *cwd = (char *) malloc(MAX_PATH_LEN + 1);
|
---|
121 | if (!cwd)
|
---|
122 | return ENOMEM;
|
---|
123 |
|
---|
124 | if (vfs_cwd_get(cwd, MAX_PATH_LEN + 1) != EOK)
|
---|
125 | str_cpy(cwd, MAX_PATH_LEN + 1, "/");
|
---|
126 |
|
---|
127 | size_t len = str_length(cwd);
|
---|
128 |
|
---|
129 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
130 |
|
---|
131 | ipc_call_t answer;
|
---|
132 | aid_t req = async_send_0(exch, LOADER_SET_CWD, &answer);
|
---|
133 | int rc = async_data_write_start(exch, cwd, len);
|
---|
134 |
|
---|
135 | async_exchange_end(exch);
|
---|
136 | free(cwd);
|
---|
137 |
|
---|
138 | if (rc != EOK) {
|
---|
139 | async_forget(req);
|
---|
140 | return (int) rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 | async_wait_for(req, &rc);
|
---|
144 | return (int) rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /** Set the program to load.
|
---|
148 | *
|
---|
149 | * @param ldr Loader connection structure.
|
---|
150 | * @param name Name to set for the spawned program.
|
---|
151 | * @param file Program file.
|
---|
152 | *
|
---|
153 | * @return Zero on success or negative error code.
|
---|
154 | *
|
---|
155 | */
|
---|
156 | int loader_set_program(loader_t *ldr, const char *name, int file)
|
---|
157 | {
|
---|
158 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
159 |
|
---|
160 | ipc_call_t answer;
|
---|
161 | aid_t req = async_send_0(exch, LOADER_SET_PROGRAM, &answer);
|
---|
162 |
|
---|
163 | int rc = async_data_write_start(exch, name, str_size(name) + 1);
|
---|
164 | if (rc == EOK) {
|
---|
165 | async_exch_t *vfs_exch = vfs_exchange_begin();
|
---|
166 | rc = vfs_pass_handle(vfs_exch, file, exch);
|
---|
167 | vfs_exchange_end(vfs_exch);
|
---|
168 | }
|
---|
169 |
|
---|
170 | async_exchange_end(exch);
|
---|
171 |
|
---|
172 | if (rc != EOK) {
|
---|
173 | async_forget(req);
|
---|
174 | return (int) rc;
|
---|
175 | }
|
---|
176 |
|
---|
177 | async_wait_for(req, &rc);
|
---|
178 | return (int) rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Set the program to load by path.
|
---|
182 | *
|
---|
183 | * @param ldr Loader connection structure.
|
---|
184 | * @param path Program path.
|
---|
185 | *
|
---|
186 | * @return Zero on success or negative error code.
|
---|
187 | *
|
---|
188 | */
|
---|
189 | int loader_set_program_path(loader_t *ldr, const char *path)
|
---|
190 | {
|
---|
191 | const char *name = str_rchr(path, '/');
|
---|
192 | if (name == NULL) {
|
---|
193 | name = path;
|
---|
194 | } else {
|
---|
195 | name++;
|
---|
196 | }
|
---|
197 |
|
---|
198 | int fd;
|
---|
199 | int rc = vfs_lookup(path, 0, &fd);
|
---|
200 | if (rc != EOK) {
|
---|
201 | return rc;
|
---|
202 | }
|
---|
203 |
|
---|
204 | rc = loader_set_program(ldr, name, fd);
|
---|
205 | vfs_put(fd);
|
---|
206 | return rc;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /** Set command-line arguments for the program.
|
---|
211 | *
|
---|
212 | * Sets the vector of command-line arguments to be passed to the loaded
|
---|
213 | * program. By convention, the very first argument is typically the same as
|
---|
214 | * the command used to execute the program.
|
---|
215 | *
|
---|
216 | * @param ldr Loader connection structure.
|
---|
217 | * @param argv NULL-terminated array of pointers to arguments.
|
---|
218 | *
|
---|
219 | * @return Zero on success or negative error code.
|
---|
220 | *
|
---|
221 | */
|
---|
222 | int loader_set_args(loader_t *ldr, const char *const argv[])
|
---|
223 | {
|
---|
224 | /*
|
---|
225 | * Serialize the arguments into a single array. First
|
---|
226 | * compute size of the buffer needed.
|
---|
227 | */
|
---|
228 | const char *const *ap = argv;
|
---|
229 | size_t buffer_size = 0;
|
---|
230 | while (*ap != NULL) {
|
---|
231 | buffer_size += str_size(*ap) + 1;
|
---|
232 | ap++;
|
---|
233 | }
|
---|
234 |
|
---|
235 | char *arg_buf = malloc(buffer_size);
|
---|
236 | if (arg_buf == NULL)
|
---|
237 | return ENOMEM;
|
---|
238 |
|
---|
239 | /* Now fill the buffer with null-terminated argument strings */
|
---|
240 | ap = argv;
|
---|
241 | char *dp = arg_buf;
|
---|
242 |
|
---|
243 | while (*ap != NULL) {
|
---|
244 | str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
|
---|
245 | dp += str_size(*ap) + 1;
|
---|
246 | ap++;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /* Send serialized arguments to the loader */
|
---|
250 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
251 |
|
---|
252 | ipc_call_t answer;
|
---|
253 | aid_t req = async_send_0(exch, LOADER_SET_ARGS, &answer);
|
---|
254 | int rc = async_data_write_start(exch, (void *) arg_buf,
|
---|
255 | buffer_size);
|
---|
256 |
|
---|
257 | async_exchange_end(exch);
|
---|
258 | free(arg_buf);
|
---|
259 |
|
---|
260 | if (rc != EOK) {
|
---|
261 | async_forget(req);
|
---|
262 | return (int) rc;
|
---|
263 | }
|
---|
264 |
|
---|
265 | async_wait_for(req, &rc);
|
---|
266 | return (int) rc;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /** Add a file to the task's inbox.
|
---|
270 | *
|
---|
271 | * @param ldr Loader connection structure.
|
---|
272 | * @param name Identification of the file.
|
---|
273 | * @param file The file's descriptor.
|
---|
274 | *
|
---|
275 | * @return Zero on success or negative error code.
|
---|
276 | *
|
---|
277 | */
|
---|
278 | int loader_add_inbox(loader_t *ldr, const char *name, int file)
|
---|
279 | {
|
---|
280 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
281 | async_exch_t *vfs_exch = vfs_exchange_begin();
|
---|
282 |
|
---|
283 | aid_t req = async_send_0(exch, LOADER_ADD_INBOX, NULL);
|
---|
284 |
|
---|
285 | int rc = async_data_write_start(exch, name, str_size(name) + 1);
|
---|
286 | if (rc == EOK) {
|
---|
287 | rc = vfs_pass_handle(vfs_exch, file, exch);
|
---|
288 | }
|
---|
289 |
|
---|
290 | async_exchange_end(vfs_exch);
|
---|
291 | async_exchange_end(exch);
|
---|
292 |
|
---|
293 | if (rc == EOK) {
|
---|
294 | async_wait_for(req, &rc);
|
---|
295 | } else {
|
---|
296 | async_forget(req);
|
---|
297 | }
|
---|
298 |
|
---|
299 | return (int) rc;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /** Instruct loader to load the program.
|
---|
303 | *
|
---|
304 | * If this function succeeds, the program has been successfully loaded
|
---|
305 | * and is ready to be executed.
|
---|
306 | *
|
---|
307 | * @param ldr Loader connection structure.
|
---|
308 | *
|
---|
309 | * @return Zero on success or negative error code.
|
---|
310 | *
|
---|
311 | */
|
---|
312 | int loader_load_program(loader_t *ldr)
|
---|
313 | {
|
---|
314 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
315 | int rc = async_req_0_0(exch, LOADER_LOAD);
|
---|
316 | async_exchange_end(exch);
|
---|
317 |
|
---|
318 | return rc;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /** Instruct loader to execute the program.
|
---|
322 | *
|
---|
323 | * Note that this function blocks until the loader actually replies
|
---|
324 | * so you cannot expect this function to return if you are debugging
|
---|
325 | * the task and its thread is stopped.
|
---|
326 | *
|
---|
327 | * After using this function, no further operations can be performed
|
---|
328 | * on the loader structure and it is deallocated.
|
---|
329 | *
|
---|
330 | * @param ldr Loader connection structure.
|
---|
331 | *
|
---|
332 | * @return Zero on success or negative error code.
|
---|
333 | *
|
---|
334 | */
|
---|
335 | int loader_run(loader_t *ldr)
|
---|
336 | {
|
---|
337 | async_exch_t *exch = async_exchange_begin(ldr->sess);
|
---|
338 | int rc = async_req_0_0(exch, LOADER_RUN);
|
---|
339 | async_exchange_end(exch);
|
---|
340 |
|
---|
341 | if (rc != EOK)
|
---|
342 | return rc;
|
---|
343 |
|
---|
344 | async_hangup(ldr->sess);
|
---|
345 | free(ldr);
|
---|
346 |
|
---|
347 | return EOK;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** Cancel the loader session.
|
---|
351 | *
|
---|
352 | * Tell the loader not to load any program and terminate.
|
---|
353 | * After using this function, no further operations can be performed
|
---|
354 | * on the loader structure and it is deallocated.
|
---|
355 | *
|
---|
356 | * @param ldr Loader connection structure.
|
---|
357 | *
|
---|
358 | * @return Zero on success or negative error code.
|
---|
359 | *
|
---|
360 | */
|
---|
361 | void loader_abort(loader_t *ldr)
|
---|
362 | {
|
---|
363 | async_hangup(ldr->sess);
|
---|
364 | free(ldr);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /** @}
|
---|
368 | */
|
---|