source: mainline/uspace/lib/c/generic/loader.c@ e796dc8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e796dc8 was 354b642, checked in by Jakub Jermar <jakub@…>, 9 years ago

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5/ revisions 1932-1936

Original commit messages:

1936: Jiri Zarevucky 2013-08-05 Modifications to vfs_rdwr.
1935: Jiri Zarevucky 2013-08-05 Fix a bug in read/write.
1934: Jiri Zarevucky 2013-08-05 Fix a hidden bug in handle passing.
1933: Jiri Zarevucky 2013-08-05 Add VFS_IN_CLONE.
1932: Jiri Zarevucky 2013-08-05 Add functions for passing handles around.

Modifications:

  • New vcl_* interfaces renamed to vfs_*
  • Server-side vfs_pass_handle() and vfs_clone() renamed to vfs_op_* to avoid name conflict with libc
  • 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/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
[45454e9b]149/** Set pathname of the program to load.
150 *
151 * Sets the name of the program file to load. The name can be relative
152 * to the current working directory (it will be absolutized before
153 * sending to the loader).
154 *
[937aeee]155 * @param ldr Loader connection structure.
156 * @param path Pathname of the program file.
157 *
158 * @return Zero on success or negative error code.
159 *
[45454e9b]160 */
161int loader_set_pathname(loader_t *ldr, const char *path)
162{
163 size_t pa_len;
[6afc9d7]164 char *pa = vfs_absolutize(path, &pa_len);
[45454e9b]165 if (!pa)
[79ae36dd]166 return ENOMEM;
[937aeee]167
[45454e9b]168 /* Send program pathname */
[79ae36dd]169 async_exch_t *exch = async_exchange_begin(ldr->sess);
170
[937aeee]171 ipc_call_t answer;
[79ae36dd]172 aid_t req = async_send_0(exch, LOADER_SET_PATHNAME, &answer);
173 sysarg_t rc = async_data_write_start(exch, (void *) pa, pa_len);
174
175 async_exchange_end(exch);
176 free(pa);
177
[45454e9b]178 if (rc != EOK) {
[50b581d]179 async_forget(req);
[79ae36dd]180 return (int) rc;
[45454e9b]181 }
[937aeee]182
[79ae36dd]183 async_wait_for(req, &rc);
184 return (int) rc;
[45454e9b]185}
186
187/** Set command-line arguments for the program.
188 *
189 * Sets the vector of command-line arguments to be passed to the loaded
190 * program. By convention, the very first argument is typically the same as
191 * the command used to execute the program.
192 *
[937aeee]193 * @param ldr Loader connection structure.
194 * @param argv NULL-terminated array of pointers to arguments.
195 *
196 * @return Zero on success or negative error code.
197 *
[45454e9b]198 */
[a000878c]199int loader_set_args(loader_t *ldr, const char *const argv[])
[45454e9b]200{
[937aeee]201 /*
[45454e9b]202 * Serialize the arguments into a single array. First
203 * compute size of the buffer needed.
204 */
[a000878c]205 const char *const *ap = argv;
[937aeee]206 size_t buffer_size = 0;
[45454e9b]207 while (*ap != NULL) {
[9eb3623]208 buffer_size += str_size(*ap) + 1;
[937aeee]209 ap++;
[45454e9b]210 }
[937aeee]211
212 char *arg_buf = malloc(buffer_size);
213 if (arg_buf == NULL)
214 return ENOMEM;
215
[45454e9b]216 /* Now fill the buffer with null-terminated argument strings */
217 ap = argv;
[937aeee]218 char *dp = arg_buf;
219
[45454e9b]220 while (*ap != NULL) {
[6eb2e96]221 str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
[9eb3623]222 dp += str_size(*ap) + 1;
[937aeee]223 ap++;
[45454e9b]224 }
[937aeee]225
[45454e9b]226 /* Send serialized arguments to the loader */
[79ae36dd]227 async_exch_t *exch = async_exchange_begin(ldr->sess);
228
[937aeee]229 ipc_call_t answer;
[79ae36dd]230 aid_t req = async_send_0(exch, LOADER_SET_ARGS, &answer);
231 sysarg_t rc = async_data_write_start(exch, (void *) arg_buf,
232 buffer_size);
233
234 async_exchange_end(exch);
235 free(arg_buf);
236
[45454e9b]237 if (rc != EOK) {
[50b581d]238 async_forget(req);
[79ae36dd]239 return (int) rc;
[45454e9b]240 }
[937aeee]241
[45454e9b]242 async_wait_for(req, &rc);
[79ae36dd]243 return (int) rc;
[937aeee]244}
[45454e9b]245
[937aeee]246/** Set preset files for the program.
247 *
248 * Sets the vector of preset files to be passed to the loaded
249 * program. By convention, the first three files represent stdin,
250 * stdout and stderr respectively.
251 *
252 * @param ldr Loader connection structure.
253 * @param files NULL-terminated array of pointers to files.
254 *
255 * @return Zero on success or negative error code.
256 *
257 */
[7171760]258int loader_set_files(loader_t *ldr, int * const files[])
[937aeee]259{
260 /* Send serialized files to the loader */
[79ae36dd]261 async_exch_t *exch = async_exchange_begin(ldr->sess);
[7171760]262 async_exch_t *vfs_exch = vfs_exchange_begin();
[79ae36dd]263
[7171760]264 int i;
[0fe52ef]265 for (i = 0; files[i]; i++);
[7171760]266
[937aeee]267 ipc_call_t answer;
[7171760]268 aid_t req = async_send_1(exch, LOADER_SET_FILES, i, &answer);
269
[d894fbd]270 sysarg_t rc = EOK;
[79ae36dd]271
[7171760]272 for (i = 0; files[i]; i++) {
[354b642]273 rc = vfs_pass_handle(vfs_exch, *files[i], exch);
[7171760]274 if (rc != EOK)
275 break;
276 }
[79ae36dd]277
[7171760]278 vfs_exchange_end(vfs_exch);
279 async_exchange_end(exch);
280
[937aeee]281 if (rc != EOK) {
[50b581d]282 async_forget(req);
[79ae36dd]283 return (int) rc;
[937aeee]284 }
285
286 async_wait_for(req, &rc);
[79ae36dd]287 return (int) rc;
[45454e9b]288}
289
[4470e26]290/** Instruct loader to load the program.
291 *
292 * If this function succeeds, the program has been successfully loaded
293 * and is ready to be executed.
294 *
[937aeee]295 * @param ldr Loader connection structure.
296 *
297 * @return Zero on success or negative error code.
298 *
[4470e26]299 */
300int loader_load_program(loader_t *ldr)
301{
[79ae36dd]302 async_exch_t *exch = async_exchange_begin(ldr->sess);
303 int rc = async_req_0_0(exch, LOADER_LOAD);
304 async_exchange_end(exch);
305
306 return rc;
[4470e26]307}
308
[45454e9b]309/** Instruct loader to execute the program.
[4470e26]310 *
311 * Note that this function blocks until the loader actually replies
312 * so you cannot expect this function to return if you are debugging
313 * the task and its thread is stopped.
[45454e9b]314 *
[79ae36dd]315 * After using this function, no further operations can be performed
316 * on the loader structure and it is deallocated.
[45454e9b]317 *
[937aeee]318 * @param ldr Loader connection structure.
319 *
320 * @return Zero on success or negative error code.
321 *
[45454e9b]322 */
[4470e26]323int loader_run(loader_t *ldr)
[45454e9b]324{
[79ae36dd]325 async_exch_t *exch = async_exchange_begin(ldr->sess);
326 int rc = async_req_0_0(exch, LOADER_RUN);
327 async_exchange_end(exch);
328
[45454e9b]329 if (rc != EOK)
330 return rc;
[937aeee]331
[79ae36dd]332 async_hangup(ldr->sess);
333 free(ldr);
334
[45454e9b]335 return EOK;
336}
337
338/** Cancel the loader session.
339 *
[79ae36dd]340 * Tell the loader not to load any program and terminate.
341 * After using this function, no further operations can be performed
342 * on the loader structure and it is deallocated.
[45454e9b]343 *
[937aeee]344 * @param ldr Loader connection structure.
345 *
346 * @return Zero on success or negative error code.
347 *
[45454e9b]348 */
349void loader_abort(loader_t *ldr)
350{
[79ae36dd]351 async_hangup(ldr->sess);
352 free(ldr);
[45454e9b]353}
354
355/** @}
356 */
Note: See TracBrowser for help on using the repository browser.