source: mainline/uspace/lib/c/generic/loader.c@ 7907cf9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7907cf9 was 96b02eb9, checked in by Martin Decky <martin@…>, 15 years ago

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

  • Property mode set to 100644
File size: 8.6 KB
RevLine 
[45454e9b]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
[937aeee]33 */
[45454e9b]34
35#include <ipc/ipc.h>
36#include <ipc/loader.h>
[bfd1546]37#include <ipc/services.h>
[45454e9b]38#include <libc.h>
[0993087]39#include <task.h>
[19f857a]40#include <str.h>
[45454e9b]41#include <stdlib.h>
42#include <async.h>
43#include <errno.h>
44#include <vfs/vfs.h>
45#include <loader/loader.h>
46
47/** Connect to a new program loader.
48 *
49 * Spawns a new program loader task and returns the connection structure.
[937aeee]50 *
51 * @param name Symbolic name to set on the newly created task.
52 *
53 * @return Pointer to the loader connection structure (should be
54 * deallocated using free() after use).
55 *
[45454e9b]56 */
[bfd1546]57int loader_spawn(const char *name)
[45454e9b]58{
[bfd1546]59 return __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER,
[9eb3623]60 (sysarg_t) name, str_size(name));
[bfd1546]61}
[45454e9b]62
[bfd1546]63loader_t *loader_connect(void)
64{
[937aeee]65 int phone_id = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_LOAD, 0, 0);
[bfd1546]66 if (phone_id < 0)
[45454e9b]67 return NULL;
[937aeee]68
69 loader_t *ldr = malloc(sizeof(loader_t));
[45454e9b]70 if (ldr == NULL)
71 return NULL;
[937aeee]72
[45454e9b]73 ldr->phone_id = phone_id;
[937aeee]74 return ldr;
[45454e9b]75}
76
77/** Get ID of the new task.
78 *
79 * Retrieves the ID of the new task from the loader.
80 *
[937aeee]81 * @param ldr Loader connection structure.
82 * @param task_id Points to a variable where the ID should be stored.
83 *
84 * @return Zero on success or negative error code.
85 *
[45454e9b]86 */
87int loader_get_task_id(loader_t *ldr, task_id_t *task_id)
88{
89 /* Get task ID. */
[937aeee]90 ipc_call_t answer;
91 aid_t req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer);
[0da4e41]92 int rc = async_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t));
[45454e9b]93 if (rc != EOK) {
94 async_wait_for(req, NULL);
95 return rc;
96 }
[937aeee]97
[96b02eb9]98 sysarg_t retval;
[45454e9b]99 async_wait_for(req, &retval);
[937aeee]100 return (int) retval;
[45454e9b]101}
102
[622cdbe]103/** Set current working directory for the loaded task.
104 *
105 * Sets the current working directory for the loaded task.
106 *
107 * @param ldr Loader connection structure.
108 *
109 * @return Zero on success or negative error code.
110 *
111 */
112int loader_set_cwd(loader_t *ldr)
113{
114 char *cwd;
115 size_t len;
116
117 cwd = (char *) malloc(MAX_PATH_LEN + 1);
118 if (!cwd)
119 return ENOMEM;
120 if (!getcwd(cwd, MAX_PATH_LEN + 1))
121 str_cpy(cwd, MAX_PATH_LEN + 1, "/");
122 len = str_length(cwd);
123
124 ipc_call_t answer;
125 aid_t req = async_send_0(ldr->phone_id, LOADER_SET_CWD, &answer);
126 int rc = async_data_write_start(ldr->phone_id, cwd, len);
127 free(cwd);
128 if (rc != EOK) {
129 async_wait_for(req, NULL);
130 return rc;
131 }
132
[96b02eb9]133 sysarg_t retval;
[622cdbe]134 async_wait_for(req, &retval);
135 return (int) retval;
136}
137
[45454e9b]138/** Set pathname of the program to load.
139 *
140 * Sets the name of the program file to load. The name can be relative
141 * to the current working directory (it will be absolutized before
142 * sending to the loader).
143 *
[937aeee]144 * @param ldr Loader connection structure.
145 * @param path Pathname of the program file.
146 *
147 * @return Zero on success or negative error code.
148 *
[45454e9b]149 */
150int loader_set_pathname(loader_t *ldr, const char *path)
151{
152 size_t pa_len;
[937aeee]153 char *pa = absolutize(path, &pa_len);
[45454e9b]154 if (!pa)
155 return 0;
[937aeee]156
[45454e9b]157 /* Send program pathname */
[937aeee]158 ipc_call_t answer;
159 aid_t req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer);
[0da4e41]160 int rc = async_data_write_start(ldr->phone_id, (void *) pa, pa_len);
[45454e9b]161 if (rc != EOK) {
162 async_wait_for(req, NULL);
163 return rc;
164 }
[937aeee]165
[45454e9b]166 free(pa);
[937aeee]167
[96b02eb9]168 sysarg_t retval;
[45454e9b]169 async_wait_for(req, &retval);
[937aeee]170 return (int) retval;
[45454e9b]171}
172
173/** Set command-line arguments for the program.
174 *
175 * Sets the vector of command-line arguments to be passed to the loaded
176 * program. By convention, the very first argument is typically the same as
177 * the command used to execute the program.
178 *
[937aeee]179 * @param ldr Loader connection structure.
180 * @param argv NULL-terminated array of pointers to arguments.
181 *
182 * @return Zero on success or negative error code.
183 *
[45454e9b]184 */
[a000878c]185int loader_set_args(loader_t *ldr, const char *const argv[])
[45454e9b]186{
[937aeee]187 /*
[45454e9b]188 * Serialize the arguments into a single array. First
189 * compute size of the buffer needed.
190 */
[a000878c]191 const char *const *ap = argv;
[937aeee]192 size_t buffer_size = 0;
[45454e9b]193 while (*ap != NULL) {
[9eb3623]194 buffer_size += str_size(*ap) + 1;
[937aeee]195 ap++;
[45454e9b]196 }
[937aeee]197
198 char *arg_buf = malloc(buffer_size);
199 if (arg_buf == NULL)
200 return ENOMEM;
201
[45454e9b]202 /* Now fill the buffer with null-terminated argument strings */
203 ap = argv;
[937aeee]204 char *dp = arg_buf;
205
[45454e9b]206 while (*ap != NULL) {
[6eb2e96]207 str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
[9eb3623]208 dp += str_size(*ap) + 1;
[937aeee]209 ap++;
[45454e9b]210 }
[937aeee]211
[45454e9b]212 /* Send serialized arguments to the loader */
[937aeee]213 ipc_call_t answer;
214 aid_t req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer);
[96b02eb9]215 sysarg_t rc = async_data_write_start(ldr->phone_id, (void *) arg_buf, buffer_size);
[45454e9b]216 if (rc != EOK) {
217 async_wait_for(req, NULL);
218 return rc;
219 }
[937aeee]220
[45454e9b]221 async_wait_for(req, &rc);
[937aeee]222 if (rc != EOK)
223 return rc;
224
[45454e9b]225 /* Free temporary buffer */
226 free(arg_buf);
[937aeee]227
228 return EOK;
229}
[45454e9b]230
[937aeee]231/** Set preset files for the program.
232 *
233 * Sets the vector of preset files to be passed to the loaded
234 * program. By convention, the first three files represent stdin,
235 * stdout and stderr respectively.
236 *
237 * @param ldr Loader connection structure.
238 * @param files NULL-terminated array of pointers to files.
239 *
240 * @return Zero on success or negative error code.
241 *
242 */
[99272a3]243int loader_set_files(loader_t *ldr, fdi_node_t *const files[])
[937aeee]244{
245 /*
246 * Serialize the arguments into a single array. First
247 * compute size of the buffer needed.
248 */
[99272a3]249 fdi_node_t *const *ap = files;
[937aeee]250 size_t count = 0;
251 while (*ap != NULL) {
252 count++;
253 ap++;
254 }
255
[99272a3]256 fdi_node_t *files_buf;
257 files_buf = (fdi_node_t *) malloc(count * sizeof(fdi_node_t));
[937aeee]258 if (files_buf == NULL)
259 return ENOMEM;
260
261 /* Fill the buffer */
262 size_t i;
263 for (i = 0; i < count; i++)
264 files_buf[i] = *files[i];
265
266 /* Send serialized files to the loader */
267 ipc_call_t answer;
268 aid_t req = async_send_0(ldr->phone_id, LOADER_SET_FILES, &answer);
[96b02eb9]269 sysarg_t rc = async_data_write_start(ldr->phone_id, (void *) files_buf,
[99272a3]270 count * sizeof(fdi_node_t));
[937aeee]271 if (rc != EOK) {
272 async_wait_for(req, NULL);
273 return rc;
274 }
275
276 async_wait_for(req, &rc);
277 if (rc != EOK)
278 return rc;
279
280 /* Free temporary buffer */
281 free(files_buf);
282
[45454e9b]283 return EOK;
284}
285
[4470e26]286/** Instruct loader to load the program.
287 *
288 * If this function succeeds, the program has been successfully loaded
289 * and is ready to be executed.
290 *
[937aeee]291 * @param ldr Loader connection structure.
292 *
293 * @return Zero on success or negative error code.
294 *
[4470e26]295 */
296int loader_load_program(loader_t *ldr)
297{
[937aeee]298 return (int) async_req_0_0(ldr->phone_id, LOADER_LOAD);
[4470e26]299}
300
[45454e9b]301/** Instruct loader to execute the program.
[4470e26]302 *
303 * Note that this function blocks until the loader actually replies
304 * so you cannot expect this function to return if you are debugging
305 * the task and its thread is stopped.
[45454e9b]306 *
307 * After using this function, no further operations must be performed
308 * on the loader structure. It should be de-allocated using free().
309 *
[937aeee]310 * @param ldr Loader connection structure.
311 *
312 * @return Zero on success or negative error code.
313 *
[45454e9b]314 */
[4470e26]315int loader_run(loader_t *ldr)
[45454e9b]316{
[937aeee]317 int rc = async_req_0_0(ldr->phone_id, LOADER_RUN);
[45454e9b]318 if (rc != EOK)
319 return rc;
[937aeee]320
[482c86f]321 ipc_hangup(ldr->phone_id);
322 ldr->phone_id = 0;
[45454e9b]323 return EOK;
324}
325
326/** Cancel the loader session.
327 *
328 * Tells the loader not to load any program and terminate.
329 * After using this function, no further operations must be performed
330 * on the loader structure. It should be de-allocated using free().
331 *
[937aeee]332 * @param ldr Loader connection structure.
333 *
334 * @return Zero on success or negative error code.
335 *
[45454e9b]336 */
337void loader_abort(loader_t *ldr)
338{
339 ipc_hangup(ldr->phone_id);
340 ldr->phone_id = 0;
341}
342
343/** @}
344 */
Note: See TracBrowser for help on using the repository browser.