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

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

Remove unistd.h

  • Rename usleep() and sleep() to thread_usleep() and thread_sleep() and move to thread.[hc].
  • Include stddef.h in order to provide NULL.
  • Move getpagesize() to libposix.
  • Sync uspace/dist/src/c/demos with originals.
  • Property mode set to 100644
File size: 10.7 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 */
[5126f80]179 int root = vfs_root();
180 if (root >= 0) {
181 rc = loader_add_inbox(ldr, "root", root);
[9c4cf0d]182 vfs_put(root);
[5126f80]183 if (rc != EOK)
184 goto error;
185 }
186
[bb9ec2d]187 if (fd_stdin >= 0) {
188 rc = loader_add_inbox(ldr, "stdin", fd_stdin);
189 if (rc != EOK)
190 goto error;
191 }
192
193 if (fd_stdout >= 0) {
194 rc = loader_add_inbox(ldr, "stdout", fd_stdout);
195 if (rc != EOK)
196 goto error;
197 }
198
199 if (fd_stderr >= 0) {
200 rc = loader_add_inbox(ldr, "stderr", fd_stderr);
201 if (rc != EOK)
202 goto error;
203 }
[ee369f3]204
[4470e26]205 /* Load the program. */
206 rc = loader_load_program(ldr);
207 if (rc != EOK)
208 goto error;
[ee369f3]209
[1c635d6]210 /* Setup waiting for return value if needed */
211 if (wait) {
212 rc = task_setup_wait(task_id, wait);
213 if (rc != EOK)
214 goto error;
215 wait_initialized = true;
216 }
217
[4470e26]218 /* Run it. */
219 rc = loader_run(ldr);
[17b2aac]220 if (rc != EOK)
221 goto error;
[ee369f3]222
[c98e6ee]223 /* Success */
[0485135]224 if (id != NULL)
225 *id = task_id;
[d9fae235]226
[0485135]227 return EOK;
[3815efb]228
[c98e6ee]229error:
[1c635d6]230 if (wait_initialized)
231 task_cancel_wait(wait);
232
[ee369f3]233 /* Error exit */
[45454e9b]234 loader_abort(ldr);
[0485135]235 return rc;
236}
237
[9f5cf68]238/** Create a new task by running an executable from the filesystem.
239 *
240 * This is really just a convenience wrapper over the more complicated
241 * loader API. Arguments are passed in a va_list.
242 *
243 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]244 * @param wait If not NULL, setup waiting for task's return value and store
245 * the information necessary for waiting here on success.
[9f5cf68]246 * @param path Pathname of the binary to execute.
247 * @param cnt Number of arguments.
248 * @param ap Command-line arguments.
249 *
250 * @return Zero on success or negative error code.
251 *
252 */
[1c635d6]253int task_spawn(task_id_t *task_id, task_wait_t *wait, const char *path,
254 int cnt, va_list ap)
[9f5cf68]255{
256 /* Allocate argument list. */
257 const char **arglist = malloc(cnt * sizeof(const char *));
258 if (arglist == NULL)
259 return ENOMEM;
260
261 /* Fill in arguments. */
262 const char *arg;
263 cnt = 0;
264 do {
265 arg = va_arg(ap, const char *);
266 arglist[cnt++] = arg;
267 } while (arg != NULL);
268
269 /* Spawn task. */
[1c635d6]270 int rc = task_spawnv(task_id, wait, path, arglist);
[9f5cf68]271
272 /* Free argument list. */
273 free(arglist);
274 return rc;
275}
276
[0485135]277/** Create a new task by running an executable from the filesystem.
278 *
279 * This is really just a convenience wrapper over the more complicated
280 * loader API. Arguments are passed as a null-terminated list of arguments.
281 *
[79ae36dd]282 * @param id If not NULL, the ID of the task is stored here on success.
[1c635d6]283 * @param wait If not NULL, setup waiting for task's return value and store
284 * the information necessary for waiting here on success.
[79ae36dd]285 * @param path Pathname of the binary to execute.
286 * @param ... Command-line arguments.
287 *
288 * @return Zero on success or negative error code.
[0485135]289 *
290 */
[1c635d6]291int task_spawnl(task_id_t *task_id, task_wait_t *wait, const char *path, ...)
[0485135]292{
[79ae36dd]293 /* Count the number of arguments. */
294
[0485135]295 va_list ap;
296 const char *arg;
[79ae36dd]297 int cnt = 0;
298
[0485135]299 va_start(ap, path);
300 do {
301 arg = va_arg(ap, const char *);
302 cnt++;
303 } while (arg != NULL);
304 va_end(ap);
[79ae36dd]305
[0485135]306 va_start(ap, path);
[1c635d6]307 int rc = task_spawn(task_id, wait, path, cnt, ap);
[0485135]308 va_end(ap);
[79ae36dd]309
[0485135]310 return rc;
[adb4d6c2]311}
312
[1c635d6]313/** Setup waiting for a task.
314 *
315 * If the task finishes after this call succeeds, it is guaranteed that
316 * task_wait(wait, &texit, &retval) will return correct return value for
317 * the task.
318 *
319 * @param id ID of the task to setup waiting for.
320 * @param wait Information necessary for the later task_wait call is stored here.
321 *
322 * @return EOK on success, else error code.
323 */
324int task_setup_wait(task_id_t id, task_wait_t *wait)
[ee369f3]325{
[79ae36dd]326 async_exch_t *exch = async_exchange_begin(session_ns);
[1c635d6]327 wait->aid = async_send_2(exch, NS_TASK_WAIT, LOWER32(id), UPPER32(id),
328 &wait->result);
[79ae36dd]329 async_exchange_end(exch);
[1c635d6]330
331 return EOK;
332}
333
334/** Cancel waiting for a task.
335 *
336 * This can be called *instead of* task_wait if the caller is not interested
337 * in waiting for the task anymore.
338 *
339 * This function cannot be called if the task_wait was already called.
340 *
341 * @param wait task_wait_t previously initialized by task_setup_wait.
342 */
343void task_cancel_wait(task_wait_t *wait) {
344 async_forget(wait->aid);
345}
346
347/** Wait for a task to finish.
348 *
349 * This function returns correct values even if the task finished in
350 * between task_setup_wait and this task_wait call.
351 *
352 * This function cannot be called more than once with the same task_wait_t
353 * (it can be reused, but must be reinitialized with task_setup_wait first)
354 *
355 * @param wait task_wait_t previously initialized by task_setup_wait.
356 * @param texit Store type of task exit here.
357 * @param retval Store return value of the task here.
358 *
359 * @return EOK on success, else error code.
360 */
361int task_wait(task_wait_t *wait, task_exit_t *texit, int *retval)
362{
363 assert(texit);
364 assert(retval);
365
366 sysarg_t rc;
367 async_wait_for(wait->aid, &rc);
368
369 if (rc == EOK) {
370 *texit = IPC_GET_ARG1(wait->result);
371 *retval = IPC_GET_ARG2(wait->result);
372 }
373
[7114d83]374 return rc;
375}
376
[1c635d6]377/** Wait for a task to finish by its id.
378 *
379 * Note that this will fail with ENOENT if the task id is not registered in ns
380 * (e.g. if the task finished). If you are spawning a task and need to wait
381 * for its completion, use wait parameter of the task_spawn* functions instead
382 * to prevent a race where the task exits before you may have a chance to wait
383 * wait for it.
384 *
385 * @param id ID of the task to wait for.
386 * @param texit Store type of task exit here.
387 * @param retval Store return value of the task here.
388 *
389 * @return EOK on success, else error code.
390 */
391int task_wait_task_id(task_id_t id, task_exit_t *texit, int *retval)
392{
393 task_wait_t wait;
394 int rc = task_setup_wait(id, &wait);
395 if (rc != EOK)
396 return rc;
397
398 return task_wait(&wait, texit, retval);
399}
400
[7114d83]401int task_retval(int val)
402{
[79ae36dd]403 async_exch_t *exch = async_exchange_begin(session_ns);
404 int rc = (int) async_req_1_0(exch, NS_RETVAL, val);
405 async_exchange_end(exch);
406
407 return rc;
[ee369f3]408}
409
[fadd381]410/** @}
[b2951e2]411 */
Note: See TracBrowser for help on using the repository browser.