source: mainline/uspace/lib/c/generic/loader.c@ 0d35511

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0d35511 was 5126f80, checked in by Jakub Jermar <jakub@…>, 8 years ago

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5/ revision 1946

Original commit messages:

1946: Jiri Zarevucky 2013-08-06 Relativize mount, add root handle to libc and remove root from VFS server. This wraps up the "relativization" phase.

Breakage:

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