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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b538ca5c was 79ae36dd, checked in by Martin Decky <martin@…>, 14 years ago

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 9.0 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(EXCHANGE_SERIALIZE, SERVICE_LOAD, 0, 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_wait_for(req, NULL);
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))
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_wait_for(req, NULL);
142 return (int) rc;
143 }
144
145 async_wait_for(req, &rc);
146 return (int) rc;
147}
148
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 *
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 *
160 */
161int loader_set_pathname(loader_t *ldr, const char *path)
162{
163 size_t pa_len;
164 char *pa = absolutize(path, &pa_len);
165 if (!pa)
166 return ENOMEM;
167
168 /* Send program pathname */
169 async_exch_t *exch = async_exchange_begin(ldr->sess);
170
171 ipc_call_t answer;
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
178 if (rc != EOK) {
179 async_wait_for(req, NULL);
180 return (int) rc;
181 }
182
183 async_wait_for(req, &rc);
184 return (int) rc;
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 *
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 *
198 */
199int loader_set_args(loader_t *ldr, const char *const argv[])
200{
201 /*
202 * Serialize the arguments into a single array. First
203 * compute size of the buffer needed.
204 */
205 const char *const *ap = argv;
206 size_t buffer_size = 0;
207 while (*ap != NULL) {
208 buffer_size += str_size(*ap) + 1;
209 ap++;
210 }
211
212 char *arg_buf = malloc(buffer_size);
213 if (arg_buf == NULL)
214 return ENOMEM;
215
216 /* Now fill the buffer with null-terminated argument strings */
217 ap = argv;
218 char *dp = arg_buf;
219
220 while (*ap != NULL) {
221 str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
222 dp += str_size(*ap) + 1;
223 ap++;
224 }
225
226 /* Send serialized arguments to the loader */
227 async_exch_t *exch = async_exchange_begin(ldr->sess);
228
229 ipc_call_t answer;
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
237 if (rc != EOK) {
238 async_wait_for(req, NULL);
239 return (int) rc;
240 }
241
242 async_wait_for(req, &rc);
243 return (int) rc;
244}
245
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 */
258int loader_set_files(loader_t *ldr, fdi_node_t *const files[])
259{
260 /*
261 * Serialize the arguments into a single array. First
262 * compute size of the buffer needed.
263 */
264 fdi_node_t *const *ap = files;
265 size_t count = 0;
266 while (*ap != NULL) {
267 count++;
268 ap++;
269 }
270
271 fdi_node_t *files_buf;
272 files_buf = (fdi_node_t *) malloc(count * sizeof(fdi_node_t));
273 if (files_buf == NULL)
274 return ENOMEM;
275
276 /* Fill the buffer */
277 size_t i;
278 for (i = 0; i < count; i++)
279 files_buf[i] = *files[i];
280
281 /* Send serialized files to the loader */
282 async_exch_t *exch = async_exchange_begin(ldr->sess);
283
284 ipc_call_t answer;
285 aid_t req = async_send_0(exch, LOADER_SET_FILES, &answer);
286 sysarg_t rc = async_data_write_start(exch, (void *) files_buf,
287 count * sizeof(fdi_node_t));
288
289 async_exchange_end(exch);
290 free(files_buf);
291
292 if (rc != EOK) {
293 async_wait_for(req, NULL);
294 return (int) rc;
295 }
296
297 async_wait_for(req, &rc);
298 return (int) rc;
299}
300
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 *
306 * @param ldr Loader connection structure.
307 *
308 * @return Zero on success or negative error code.
309 *
310 */
311int loader_load_program(loader_t *ldr)
312{
313 async_exch_t *exch = async_exchange_begin(ldr->sess);
314 int rc = async_req_0_0(exch, LOADER_LOAD);
315 async_exchange_end(exch);
316
317 return rc;
318}
319
320/** Instruct loader to execute the program.
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.
325 *
326 * After using this function, no further operations can be performed
327 * on the loader structure and it is deallocated.
328 *
329 * @param ldr Loader connection structure.
330 *
331 * @return Zero on success or negative error code.
332 *
333 */
334int loader_run(loader_t *ldr)
335{
336 async_exch_t *exch = async_exchange_begin(ldr->sess);
337 int rc = async_req_0_0(exch, LOADER_RUN);
338 async_exchange_end(exch);
339
340 if (rc != EOK)
341 return rc;
342
343 async_hangup(ldr->sess);
344 free(ldr);
345
346 return EOK;
347}
348
349/** Cancel the loader session.
350 *
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.
354 *
355 * @param ldr Loader connection structure.
356 *
357 * @return Zero on success or negative error code.
358 *
359 */
360void loader_abort(loader_t *ldr)
361{
362 async_hangup(ldr->sess);
363 free(ldr);
364}
365
366/** @}
367 */
Note: See TracBrowser for help on using the repository browser.