source: mainline/uspace/lib/c/generic/task.c@ a737667e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a737667e 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: 10.6 KB
RevLine 
[f30e6a0b]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[c98e6ee]3 * Copyright (c) 2008 Jiri Svoboda
[1c635d6]4 * Copyright (c) 2014 Martin Sucha
[f30e6a0b]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[b2951e2]29 */
30
[fadd381]31/** @addtogroup libc
[b2951e2]32 * @{
33 */
34/** @file
[ee369f3]35 */
[f30e6a0b]36
37#include <task.h>
[45454e9b]38#include <loader/loader.h>
[0485135]39#include <stdarg.h>
[19f857a]40#include <str.h>
[ee369f3]41#include <ipc/ns.h>
42#include <macros.h>
[79ae36dd]43#include <assert.h>
[ee369f3]44#include <async.h>
[79ae36dd]45#include <errno.h>
46#include <malloc.h>
47#include <libc.h>
48#include "private/ns.h"
[df02460]49#include <vfs/vfs.h>
[f30e6a0b]50
[d3b8c1f]51task_id_t task_get_id(void)
[f30e6a0b]52{
[dd8d5a7]53#ifdef __32_BITS__
[f30e6a0b]54 task_id_t task_id;
[d3b8c1f]55 (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
[ee369f3]56
[f30e6a0b]57 return task_id;
[dd8d5a7]58#endif /* __32_BITS__ */
59
60#ifdef __64_BITS__
61 return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
62#endif /* __64_BITS__ */
[f30e6a0b]63}
[b2951e2]64
[bc18d63]65/** Set the task name.
66 *
[ee369f3]67 * @param name The new name, typically the command used to execute the
68 * program.
69 *
70 * @return Zero on success or negative error code.
[bc18d63]71 */
72int task_set_name(const char *name)
73{
[79ae36dd]74 assert(name);
75
[9eb3623]76 return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
[bc18d63]77}
78
[1e9f8ab]79/** Kill a task.
80 *
81 * @param task_id ID of task to kill.
82 *
83 * @return Zero on success or negative error code.
84 */
85
86int task_kill(task_id_t task_id)
87{
88 return (int) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
89}
90
[45454e9b]91/** Create a new task by running an executable from the filesystem.
92 *
93 * This is really just a convenience wrapper over the more complicated
[0485135]94 * loader API. Arguments are passed as a null-terminated array of strings.
[c98e6ee]95 *
[79ae36dd]96 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]97 * @param wait If not NULL, setup waiting for task's return value and store
98 * the information necessary for waiting here on success.
[79ae36dd]99 * @param path Pathname of the binary to execute.
100 * @param argv Command-line arguments.
101 *
102 * @return Zero on success or negative error code.
[ee369f3]103 *
[c98e6ee]104 */
[1c635d6]105int task_spawnv(task_id_t *id, task_wait_t *wait, const char *path,
106 const char *const args[])
[ae45201]107{
108 /* Send default files */
109
[bb9ec2d]110 int fd_stdin = -1;
111 int fd_stdout = -1;
112 int fd_stderr = -1;
[ae45201]113
[bb9ec2d]114 if (stdin != NULL) {
115 (void) vfs_fhandle(stdin, &fd_stdin);
116 }
[ae45201]117
[bb9ec2d]118 if (stdout != NULL) {
119 (void) vfs_fhandle(stdout, &fd_stdout);
120 }
121
122 if (stderr != NULL) {
123 (void) vfs_fhandle(stderr, &fd_stderr);
124 }
[ae45201]125
[bb9ec2d]126 return task_spawnvf(id, wait, path, args, fd_stdin, fd_stdout,
127 fd_stderr);
[ae45201]128}
129
130/** Create a new task by running an executable from the filesystem.
131 *
132 * This is really just a convenience wrapper over the more complicated
133 * loader API. Arguments are passed as a null-terminated array of strings.
134 * Files are passed as null-terminated array of pointers to fdi_node_t.
135 *
[bb9ec2d]136 * @param id If not NULL, the ID of the task is stored here on success.
137 * @param wait If not NULL, setup waiting for task's return value and store
138 * @param path Pathname of the binary to execute.
139 * @param argv Command-line arguments.
140 * @param std_in File to use as stdin.
141 * @param std_out File to use as stdout.
142 * @param std_err File to use as stderr.
[ae45201]143 *
144 * @return Zero on success or negative error code.
145 *
146 */
[1c635d6]147int task_spawnvf(task_id_t *id, task_wait_t *wait, const char *path,
[bb9ec2d]148 const char *const args[], int fd_stdin, int fd_stdout, int fd_stderr)
[c98e6ee]149{
[bfd1546]150 /* Connect to a program loader. */
[79ae36dd]151 loader_t *ldr = loader_connect();
[0485135]152 if (ldr == NULL)
153 return EREFUSED;
[ee369f3]154
[1c635d6]155 bool wait_initialized = false;
156
[47e0a05b]157 /* Get task ID. */
[79ae36dd]158 task_id_t task_id;
159 int rc = loader_get_task_id(ldr, &task_id);
[45454e9b]160 if (rc != EOK)
[47e0a05b]161 goto error;
[ee369f3]162
[622cdbe]163 /* Send spawner's current working directory. */
164 rc = loader_set_cwd(ldr);
165 if (rc != EOK)
166 goto error;
167
[bb9ec2d]168 /* Send program binary. */
169 rc = loader_set_program_path(ldr, path);
[45454e9b]170 if (rc != EOK)
[17b2aac]171 goto error;
[ee369f3]172
[4470e26]173 /* Send arguments. */
[ee369f3]174 rc = loader_set_args(ldr, args);
[17b2aac]175 if (rc != EOK)
176 goto error;
[ee369f3]177
[ae45201]178 /* Send files */
[bb9ec2d]179 if (fd_stdin >= 0) {
180 rc = loader_add_inbox(ldr, "stdin", fd_stdin);
181 if (rc != EOK)
182 goto error;
183 }
184
185 if (fd_stdout >= 0) {
186 rc = loader_add_inbox(ldr, "stdout", fd_stdout);
187 if (rc != EOK)
188 goto error;
189 }
190
191 if (fd_stderr >= 0) {
192 rc = loader_add_inbox(ldr, "stderr", fd_stderr);
193 if (rc != EOK)
194 goto error;
195 }
[ee369f3]196
[4470e26]197 /* Load the program. */
198 rc = loader_load_program(ldr);
199 if (rc != EOK)
200 goto error;
[ee369f3]201
[1c635d6]202 /* Setup waiting for return value if needed */
203 if (wait) {
204 rc = task_setup_wait(task_id, wait);
205 if (rc != EOK)
206 goto error;
207 wait_initialized = true;
208 }
209
[4470e26]210 /* Run it. */
211 rc = loader_run(ldr);
[17b2aac]212 if (rc != EOK)
213 goto error;
[ee369f3]214
[c98e6ee]215 /* Success */
[0485135]216 if (id != NULL)
217 *id = task_id;
[d9fae235]218
[0485135]219 return EOK;
[3815efb]220
[c98e6ee]221error:
[1c635d6]222 if (wait_initialized)
223 task_cancel_wait(wait);
224
[ee369f3]225 /* Error exit */
[45454e9b]226 loader_abort(ldr);
[0485135]227 return rc;
228}
229
[9f5cf68]230/** Create a new task by running an executable from the filesystem.
231 *
232 * This is really just a convenience wrapper over the more complicated
233 * loader API. Arguments are passed in a va_list.
234 *
235 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]236 * @param wait If not NULL, setup waiting for task's return value and store
237 * the information necessary for waiting here on success.
[9f5cf68]238 * @param path Pathname of the binary to execute.
239 * @param cnt Number of arguments.
240 * @param ap Command-line arguments.
241 *
242 * @return Zero on success or negative error code.
243 *
244 */
[1c635d6]245int task_spawn(task_id_t *task_id, task_wait_t *wait, const char *path,
246 int cnt, va_list ap)
[9f5cf68]247{
248 /* Allocate argument list. */
249 const char **arglist = malloc(cnt * sizeof(const char *));
250 if (arglist == NULL)
251 return ENOMEM;
252
253 /* Fill in arguments. */
254 const char *arg;
255 cnt = 0;
256 do {
257 arg = va_arg(ap, const char *);
258 arglist[cnt++] = arg;
259 } while (arg != NULL);
260
261 /* Spawn task. */
[1c635d6]262 int rc = task_spawnv(task_id, wait, path, arglist);
[9f5cf68]263
264 /* Free argument list. */
265 free(arglist);
266 return rc;
267}
268
[0485135]269/** Create a new task by running an executable from the filesystem.
270 *
271 * This is really just a convenience wrapper over the more complicated
272 * loader API. Arguments are passed as a null-terminated list of arguments.
273 *
[79ae36dd]274 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]275 * @param wait If not NULL, setup waiting for task's return value and store
276 * the information necessary for waiting here on success.
[79ae36dd]277 * @param path Pathname of the binary to execute.
278 * @param ... Command-line arguments.
279 *
280 * @return Zero on success or negative error code.
[0485135]281 *
282 */
[1c635d6]283int task_spawnl(task_id_t *task_id, task_wait_t *wait, const char *path, ...)
[0485135]284{
[79ae36dd]285 /* Count the number of arguments. */
286
[0485135]287 va_list ap;
288 const char *arg;
[79ae36dd]289 int cnt = 0;
290
[0485135]291 va_start(ap, path);
292 do {
293 arg = va_arg(ap, const char *);
294 cnt++;
295 } while (arg != NULL);
296 va_end(ap);
[79ae36dd]297
[0485135]298 va_start(ap, path);
[1c635d6]299 int rc = task_spawn(task_id, wait, path, cnt, ap);
[0485135]300 va_end(ap);
[79ae36dd]301
[0485135]302 return rc;
[adb4d6c2]303}
304
[1c635d6]305/** Setup waiting for a task.
306 *
307 * If the task finishes after this call succeeds, it is guaranteed that
308 * task_wait(wait, &texit, &retval) will return correct return value for
309 * the task.
310 *
311 * @param id ID of the task to setup waiting for.
312 * @param wait Information necessary for the later task_wait call is stored here.
313 *
314 * @return EOK on success, else error code.
315 */
316int task_setup_wait(task_id_t id, task_wait_t *wait)
[ee369f3]317{
[79ae36dd]318 async_exch_t *exch = async_exchange_begin(session_ns);
[1c635d6]319 wait->aid = async_send_2(exch, NS_TASK_WAIT, LOWER32(id), UPPER32(id),
320 &wait->result);
[79ae36dd]321 async_exchange_end(exch);
[1c635d6]322
323 return EOK;
324}
325
326/** Cancel waiting for a task.
327 *
328 * This can be called *instead of* task_wait if the caller is not interested
329 * in waiting for the task anymore.
330 *
331 * This function cannot be called if the task_wait was already called.
332 *
333 * @param wait task_wait_t previously initialized by task_setup_wait.
334 */
335void task_cancel_wait(task_wait_t *wait) {
336 async_forget(wait->aid);
337}
338
339/** Wait for a task to finish.
340 *
341 * This function returns correct values even if the task finished in
342 * between task_setup_wait and this task_wait call.
343 *
344 * This function cannot be called more than once with the same task_wait_t
345 * (it can be reused, but must be reinitialized with task_setup_wait first)
346 *
347 * @param wait task_wait_t previously initialized by task_setup_wait.
348 * @param texit Store type of task exit here.
349 * @param retval Store return value of the task here.
350 *
351 * @return EOK on success, else error code.
352 */
353int task_wait(task_wait_t *wait, task_exit_t *texit, int *retval)
354{
355 assert(texit);
356 assert(retval);
357
358 sysarg_t rc;
359 async_wait_for(wait->aid, &rc);
360
361 if (rc == EOK) {
362 *texit = IPC_GET_ARG1(wait->result);
363 *retval = IPC_GET_ARG2(wait->result);
364 }
365
[7114d83]366 return rc;
367}
368
[1c635d6]369/** Wait for a task to finish by its id.
370 *
371 * Note that this will fail with ENOENT if the task id is not registered in ns
372 * (e.g. if the task finished). If you are spawning a task and need to wait
373 * for its completion, use wait parameter of the task_spawn* functions instead
374 * to prevent a race where the task exits before you may have a chance to wait
375 * wait for it.
376 *
377 * @param id ID of the task to wait for.
378 * @param texit Store type of task exit here.
379 * @param retval Store return value of the task here.
380 *
381 * @return EOK on success, else error code.
382 */
383int task_wait_task_id(task_id_t id, task_exit_t *texit, int *retval)
384{
385 task_wait_t wait;
386 int rc = task_setup_wait(id, &wait);
387 if (rc != EOK)
388 return rc;
389
390 return task_wait(&wait, texit, retval);
391}
392
[7114d83]393int task_retval(int val)
394{
[79ae36dd]395 async_exch_t *exch = async_exchange_begin(session_ns);
396 int rc = (int) async_req_1_0(exch, NS_RETVAL, val);
397 async_exchange_end(exch);
398
399 return rc;
[ee369f3]400}
401
[fadd381]402/** @}
[b2951e2]403 */
Note: See TracBrowser for help on using the repository browser.