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

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

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

Original commit messages:

1944: Jiri Zarevucky 2013-08-06 Replace legacy file descriptor presetting with inbox.
1943: Jiri Zarevucky 2013-08-06 Do not preserve open state when passing file descriptor to another task. Allow receiver to specify, whether the descriptor is low or high.
1942: Jiri Zarevucky 2013-08-06 C style.
1941: Jiri Zarevucky 2013-08-06 Make loader accept file reference instead of a pathname.

Modifications:

  • Keep version of elf_load_file() that accepts file name
  • Changes required for loading dynamically linked executables
  • Update to newer list_foreach
  • Property mode set to 100644
File size: 8.8 KB
Line 
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 Pointer to the loader connection structure (should be
55 * deallocated using free() after use).
56 *
57 */
58int loader_spawn(const char *name)
59{
60 return __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER,
61 (sysarg_t) name, str_size(name));
62}
63
64loader_t *loader_connect(void)
65{
66 loader_t *ldr = malloc(sizeof(loader_t));
67 if (ldr == NULL)
68 return NULL;
69
70 async_sess_t *sess =
71 service_connect_blocking(SERVICE_LOADER, INTERFACE_LOADER, 0);
72 if (sess == NULL) {
73 free(ldr);
74 return NULL;
75 }
76
77 ldr->sess = sess;
78 return ldr;
79}
80
81/** Get ID of the new task.
82 *
83 * Retrieves the ID of the new task from the loader.
84 *
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 *
90 */
91int loader_get_task_id(loader_t *ldr, task_id_t *task_id)
92{
93 /* Get task ID. */
94 async_exch_t *exch = async_exchange_begin(ldr->sess);
95
96 ipc_call_t answer;
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
102 if (rc != EOK) {
103 async_forget(req);
104 return (int) rc;
105 }
106
107 async_wait_for(req, &rc);
108 return (int) rc;
109}
110
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{
122 char *cwd = (char *) malloc(MAX_PATH_LEN + 1);
123 if (!cwd)
124 return ENOMEM;
125
126 if (getcwd(cwd, MAX_PATH_LEN + 1) == NULL)
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);
132
133 ipc_call_t answer;
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);
138 free(cwd);
139
140 if (rc != EOK) {
141 async_forget(req);
142 return (int) rc;
143 }
144
145 async_wait_for(req, &rc);
146 return (int) rc;
147}
148
149/** Set the program to load.
150 *
151 * @param ldr Loader connection structure.
152 * @param name Name to set for the spawned program.
153 * @param file Program file.
154 *
155 * @return Zero on success or negative error code.
156 *
157 */
158int loader_set_program(loader_t *ldr, const char *name, int file)
159{
160 async_exch_t *exch = async_exchange_begin(ldr->sess);
161
162 ipc_call_t answer;
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
172 async_exchange_end(exch);
173
174 if (rc != EOK) {
175 async_forget(req);
176 return (int) rc;
177 }
178
179 async_wait_for(req, &rc);
180 return (int) rc;
181}
182
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
200 int fd = vfs_lookup(path);
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
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 *
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 *
222 */
223int loader_set_args(loader_t *ldr, const char *const argv[])
224{
225 /*
226 * Serialize the arguments into a single array. First
227 * compute size of the buffer needed.
228 */
229 const char *const *ap = argv;
230 size_t buffer_size = 0;
231 while (*ap != NULL) {
232 buffer_size += str_size(*ap) + 1;
233 ap++;
234 }
235
236 char *arg_buf = malloc(buffer_size);
237 if (arg_buf == NULL)
238 return ENOMEM;
239
240 /* Now fill the buffer with null-terminated argument strings */
241 ap = argv;
242 char *dp = arg_buf;
243
244 while (*ap != NULL) {
245 str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
246 dp += str_size(*ap) + 1;
247 ap++;
248 }
249
250 /* Send serialized arguments to the loader */
251 async_exch_t *exch = async_exchange_begin(ldr->sess);
252
253 ipc_call_t answer;
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
261 if (rc != EOK) {
262 async_forget(req);
263 return (int) rc;
264 }
265
266 async_wait_for(req, &rc);
267 return (int) rc;
268}
269
270/** Add a file to the task's inbox.
271 *
272 * @param ldr Loader connection structure.
273 * @param name Identification of the file.
274 * @param file The file's descriptor.
275 *
276 * @return Zero on success or negative error code.
277 *
278 */
279int loader_add_inbox(loader_t *ldr, const char *name, int file)
280{
281 async_exch_t *exch = async_exchange_begin(ldr->sess);
282 async_exch_t *vfs_exch = vfs_exchange_begin();
283
284 aid_t req = async_send_0(exch, LOADER_ADD_INBOX, NULL);
285
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);
289 }
290
291 async_exchange_end(vfs_exch);
292 async_exchange_end(exch);
293
294 if (rc == EOK) {
295 async_wait_for(req, &rc);
296 } else {
297 async_forget(req);
298 }
299
300 return (int) rc;
301}
302
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 *
308 * @param ldr Loader connection structure.
309 *
310 * @return Zero on success or negative error code.
311 *
312 */
313int loader_load_program(loader_t *ldr)
314{
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;
320}
321
322/** Instruct loader to execute the program.
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.
327 *
328 * After using this function, no further operations can be performed
329 * on the loader structure and it is deallocated.
330 *
331 * @param ldr Loader connection structure.
332 *
333 * @return Zero on success or negative error code.
334 *
335 */
336int loader_run(loader_t *ldr)
337{
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
342 if (rc != EOK)
343 return rc;
344
345 async_hangup(ldr->sess);
346 free(ldr);
347
348 return EOK;
349}
350
351/** Cancel the loader session.
352 *
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.
356 *
357 * @param ldr Loader connection structure.
358 *
359 * @return Zero on success or negative error code.
360 *
361 */
362void loader_abort(loader_t *ldr)
363{
364 async_hangup(ldr->sess);
365 free(ldr);
366}
367
368/** @}
369 */
Note: See TracBrowser for help on using the repository browser.