source: mainline/uspace/lib/c/generic/loader.c@ 102f641

Last change on this file since 102f641 was 102f641, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Correcting syntax according to ccheck

  • Property mode set to 100644
File size: 8.7 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
[0a8f070]35#include <async.h>
36#include <errno.h>
[45454e9b]37#include <ipc/loader.h>
[bfd1546]38#include <ipc/services.h>
[0a8f070]39#include <ipc/taskman.h>
[45454e9b]40#include <libc.h>
[0a8f070]41#include <loader/loader.h>
42#include <ns.h>
[45454e9b]43#include <stdlib.h>
[0a8f070]44#include <str.h>
45#include <task.h>
[45454e9b]46#include <vfs/vfs.h>
[012dd8e]47
[79ae36dd]48#include "private/loader.h"
[012dd8e]49#include "private/taskman.h"
[45454e9b]50
51/** Connect to a new program loader.
52 *
53 * Spawns a new program loader task and returns the connection structure.
[937aeee]54 *
55 * @param name Symbolic name to set on the newly created task.
56 *
[f77c1c9]57 * @return Error code.
[45454e9b]58 */
[b7fd2a0]59errno_t loader_spawn(const char *name)
[45454e9b]60{
[b7fd2a0]61 return (errno_t) __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER,
[9eb3623]62 (sysarg_t) name, str_size(name));
[bfd1546]63}
[45454e9b]64
[bfd1546]65loader_t *loader_connect(void)
66{
[937aeee]67 loader_t *ldr = malloc(sizeof(loader_t));
[45454e9b]68 if (ldr == NULL)
69 return NULL;
[102f641]70
[012dd8e]71 async_sess_t *sess = taskman_session_loader();
[79ae36dd]72 if (sess == NULL) {
73 free(ldr);
74 return NULL;
75 }
[a35b458]76
[79ae36dd]77 ldr->sess = sess;
[937aeee]78 return ldr;
[45454e9b]79}
80
81/** Get ID of the new task.
82 *
83 * Retrieves the ID of the new task from the loader.
84 *
[937aeee]85 * @param ldr Loader connection structure.
86 * @param task_id Points to a variable where the ID should be stored.
87 *
[cde999a]88 * @return Zero on success or an error code.
[937aeee]89 *
[45454e9b]90 */
[b7fd2a0]91errno_t loader_get_task_id(loader_t *ldr, task_id_t *task_id)
[45454e9b]92{
93 /* Get task ID. */
[79ae36dd]94 async_exch_t *exch = async_exchange_begin(ldr->sess);
[a35b458]95
[937aeee]96 ipc_call_t answer;
[79ae36dd]97 aid_t req = async_send_0(exch, LOADER_GET_TASKID, &answer);
[b7fd2a0]98 errno_t rc = async_data_read_start(exch, task_id, sizeof(task_id_t));
[a35b458]99
[79ae36dd]100 async_exchange_end(exch);
[a35b458]101
[45454e9b]102 if (rc != EOK) {
[50b581d]103 async_forget(req);
[b7fd2a0]104 return (errno_t) rc;
[45454e9b]105 }
[a35b458]106
[79ae36dd]107 async_wait_for(req, &rc);
[b7fd2a0]108 return (errno_t) rc;
[45454e9b]109}
110
[622cdbe]111/** Set current working directory for the loaded task.
112 *
113 * Sets the current working directory for the loaded task.
114 *
115 * @param ldr Loader connection structure.
116 *
[cde999a]117 * @return Zero on success or an error code.
[622cdbe]118 *
119 */
[b7fd2a0]120errno_t loader_set_cwd(loader_t *ldr)
[622cdbe]121{
[79ae36dd]122 char *cwd = (char *) malloc(MAX_PATH_LEN + 1);
[622cdbe]123 if (!cwd)
124 return ENOMEM;
[a35b458]125
[d96d9bc]126 if (vfs_cwd_get(cwd, MAX_PATH_LEN + 1) != EOK)
[79ae36dd]127 str_cpy(cwd, MAX_PATH_LEN + 1, "/");
[a35b458]128
[79ae36dd]129 size_t len = str_length(cwd);
[a35b458]130
[79ae36dd]131 async_exch_t *exch = async_exchange_begin(ldr->sess);
[a35b458]132
[622cdbe]133 ipc_call_t answer;
[79ae36dd]134 aid_t req = async_send_0(exch, LOADER_SET_CWD, &answer);
[b7fd2a0]135 errno_t rc = async_data_write_start(exch, cwd, len);
[a35b458]136
[79ae36dd]137 async_exchange_end(exch);
[622cdbe]138 free(cwd);
[a35b458]139
[622cdbe]140 if (rc != EOK) {
[50b581d]141 async_forget(req);
[b7fd2a0]142 return (errno_t) rc;
[622cdbe]143 }
[a35b458]144
[79ae36dd]145 async_wait_for(req, &rc);
[b7fd2a0]146 return (errno_t) rc;
[622cdbe]147}
148
[bb9ec2d]149/** Set the program to load.
[45454e9b]150 *
[937aeee]151 * @param ldr Loader connection structure.
[bb9ec2d]152 * @param name Name to set for the spawned program.
153 * @param file Program file.
[937aeee]154 *
[cde999a]155 * @return Zero on success or an error code.
[937aeee]156 *
[45454e9b]157 */
[b7fd2a0]158errno_t loader_set_program(loader_t *ldr, const char *name, int file)
[45454e9b]159{
[79ae36dd]160 async_exch_t *exch = async_exchange_begin(ldr->sess);
[bb9ec2d]161
[937aeee]162 ipc_call_t answer;
[bb9ec2d]163 aid_t req = async_send_0(exch, LOADER_SET_PROGRAM, &answer);
164
[b7fd2a0]165 errno_t rc = async_data_write_start(exch, name, str_size(name) + 1);
[bb9ec2d]166 if (rc == EOK) {
167 async_exch_t *vfs_exch = vfs_exchange_begin();
168 rc = vfs_pass_handle(vfs_exch, file, exch);
169 vfs_exchange_end(vfs_exch);
170 }
171
[79ae36dd]172 async_exchange_end(exch);
[bb9ec2d]173
[45454e9b]174 if (rc != EOK) {
[50b581d]175 async_forget(req);
[b7fd2a0]176 return (errno_t) rc;
[45454e9b]177 }
[bb9ec2d]178
[79ae36dd]179 async_wait_for(req, &rc);
[b7fd2a0]180 return (errno_t) rc;
[45454e9b]181}
182
[bb9ec2d]183/** Set the program to load by path.
184 *
185 * @param ldr Loader connection structure.
186 * @param path Program path.
187 *
[cde999a]188 * @return Zero on success or an error code.
[bb9ec2d]189 *
190 */
[b7fd2a0]191errno_t loader_set_program_path(loader_t *ldr, const char *path)
[bb9ec2d]192{
[9286475]193 size_t abslen;
194 char *abspath = vfs_absolutize(path, &abslen);
195 if (!abspath)
196 return ENOMEM;
[a35b458]197
[f77c1c9]198 int fd;
[b7fd2a0]199 errno_t rc = vfs_lookup(path, 0, &fd);
[f77c1c9]200 if (rc != EOK) {
201 return rc;
[bb9ec2d]202 }
[a35b458]203
[21d3201]204 rc = loader_set_program(ldr, abspath, fd);
[9c4cf0d]205 vfs_put(fd);
[bb9ec2d]206 return rc;
207}
208
[45454e9b]209/** Set command-line arguments for the program.
210 *
211 * Sets the vector of command-line arguments to be passed to the loaded
212 * program. By convention, the very first argument is typically the same as
213 * the command used to execute the program.
214 *
[937aeee]215 * @param ldr Loader connection structure.
216 * @param argv NULL-terminated array of pointers to arguments.
217 *
[cde999a]218 * @return Zero on success or an error code.
[937aeee]219 *
[45454e9b]220 */
[b7fd2a0]221errno_t loader_set_args(loader_t *ldr, const char *const argv[])
[45454e9b]222{
[937aeee]223 /*
[45454e9b]224 * Serialize the arguments into a single array. First
225 * compute size of the buffer needed.
226 */
[a000878c]227 const char *const *ap = argv;
[937aeee]228 size_t buffer_size = 0;
[45454e9b]229 while (*ap != NULL) {
[9eb3623]230 buffer_size += str_size(*ap) + 1;
[937aeee]231 ap++;
[45454e9b]232 }
[a35b458]233
[937aeee]234 char *arg_buf = malloc(buffer_size);
235 if (arg_buf == NULL)
236 return ENOMEM;
[a35b458]237
[45454e9b]238 /* Now fill the buffer with null-terminated argument strings */
239 ap = argv;
[937aeee]240 char *dp = arg_buf;
[a35b458]241
[45454e9b]242 while (*ap != NULL) {
[6eb2e96]243 str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
[9eb3623]244 dp += str_size(*ap) + 1;
[937aeee]245 ap++;
[45454e9b]246 }
[a35b458]247
[45454e9b]248 /* Send serialized arguments to the loader */
[79ae36dd]249 async_exch_t *exch = async_exchange_begin(ldr->sess);
[a35b458]250
[937aeee]251 ipc_call_t answer;
[79ae36dd]252 aid_t req = async_send_0(exch, LOADER_SET_ARGS, &answer);
[b7fd2a0]253 errno_t rc = async_data_write_start(exch, (void *) arg_buf,
[79ae36dd]254 buffer_size);
[a35b458]255
[79ae36dd]256 async_exchange_end(exch);
257 free(arg_buf);
[a35b458]258
[45454e9b]259 if (rc != EOK) {
[50b581d]260 async_forget(req);
[b7fd2a0]261 return (errno_t) rc;
[45454e9b]262 }
[a35b458]263
[45454e9b]264 async_wait_for(req, &rc);
[b7fd2a0]265 return (errno_t) rc;
[937aeee]266}
[45454e9b]267
[bb9ec2d]268/** Add a file to the task's inbox.
[937aeee]269 *
[bb9ec2d]270 * @param ldr Loader connection structure.
271 * @param name Identification of the file.
272 * @param file The file's descriptor.
[937aeee]273 *
[cde999a]274 * @return Zero on success or an error code.
[937aeee]275 *
276 */
[b7fd2a0]277errno_t loader_add_inbox(loader_t *ldr, const char *name, int file)
[937aeee]278{
[79ae36dd]279 async_exch_t *exch = async_exchange_begin(ldr->sess);
[7171760]280 async_exch_t *vfs_exch = vfs_exchange_begin();
[a35b458]281
[bb9ec2d]282 aid_t req = async_send_0(exch, LOADER_ADD_INBOX, NULL);
[a35b458]283
[b7fd2a0]284 errno_t rc = async_data_write_start(exch, name, str_size(name) + 1);
[bb9ec2d]285 if (rc == EOK) {
286 rc = vfs_pass_handle(vfs_exch, file, exch);
[7171760]287 }
[a35b458]288
[bb9ec2d]289 async_exchange_end(vfs_exch);
[7171760]290 async_exchange_end(exch);
[a35b458]291
[bb9ec2d]292 if (rc == EOK) {
293 async_wait_for(req, &rc);
294 } else {
[50b581d]295 async_forget(req);
[937aeee]296 }
[a35b458]297
[b7fd2a0]298 return (errno_t) rc;
[45454e9b]299}
300
[4470e26]301/** Instruct loader to load the program.
302 *
303 * If this function succeeds, the program has been successfully loaded
304 * and is ready to be executed.
305 *
[937aeee]306 * @param ldr Loader connection structure.
307 *
[cde999a]308 * @return Zero on success or an error code.
[937aeee]309 *
[4470e26]310 */
[b7fd2a0]311errno_t loader_load_program(loader_t *ldr)
[4470e26]312{
[79ae36dd]313 async_exch_t *exch = async_exchange_begin(ldr->sess);
[b7fd2a0]314 errno_t rc = async_req_0_0(exch, LOADER_LOAD);
[79ae36dd]315 async_exchange_end(exch);
[a35b458]316
[79ae36dd]317 return rc;
[4470e26]318}
319
[45454e9b]320/** Instruct loader to execute the program.
[4470e26]321 *
322 * Note that this function blocks until the loader actually replies
323 * so you cannot expect this function to return if you are debugging
324 * the task and its thread is stopped.
[45454e9b]325 *
[79ae36dd]326 * After using this function, no further operations can be performed
327 * on the loader structure and it is deallocated.
[45454e9b]328 *
[937aeee]329 * @param ldr Loader connection structure.
330 *
[cde999a]331 * @return Zero on success or an error code.
[937aeee]332 *
[45454e9b]333 */
[b7fd2a0]334errno_t loader_run(loader_t *ldr)
[45454e9b]335{
[79ae36dd]336 async_exch_t *exch = async_exchange_begin(ldr->sess);
[b7fd2a0]337 errno_t rc = async_req_0_0(exch, LOADER_RUN);
[79ae36dd]338 async_exchange_end(exch);
[a35b458]339
[45454e9b]340 if (rc != EOK)
341 return rc;
[a35b458]342
[79ae36dd]343 async_hangup(ldr->sess);
344 free(ldr);
[a35b458]345
[45454e9b]346 return EOK;
347}
348
349/** Cancel the loader session.
350 *
[79ae36dd]351 * Tell the loader not to load any program and terminate.
352 * After using this function, no further operations can be performed
353 * on the loader structure and it is deallocated.
[45454e9b]354 *
[937aeee]355 * @param ldr Loader connection structure.
356 *
[cde999a]357 * @return Zero on success or an error code.
[937aeee]358 *
[45454e9b]359 */
360void loader_abort(loader_t *ldr)
361{
[79ae36dd]362 async_hangup(ldr->sess);
363 free(ldr);
[45454e9b]364}
365
366/** @}
367 */
Note: See TracBrowser for help on using the repository browser.