source: mainline/uspace/lib/c/generic/task.c@ 9934f7d

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

fix double free

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[f30e6a0b]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[c98e6ee]3 * Copyright (c) 2008 Jiri Svoboda
[f30e6a0b]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[b2951e2]28 */
29
[fadd381]30/** @addtogroup libc
[b2951e2]31 * @{
32 */
33/** @file
[ee369f3]34 */
[f30e6a0b]35
36#include <task.h>
[45454e9b]37#include <loader/loader.h>
[0485135]38#include <stdarg.h>
[19f857a]39#include <str.h>
[ee369f3]40#include <ipc/ns.h>
41#include <macros.h>
[79ae36dd]42#include <assert.h>
[ee369f3]43#include <async.h>
[79ae36dd]44#include <errno.h>
45#include <malloc.h>
46#include <libc.h>
47#include "private/ns.h"
[f30e6a0b]48
[d3b8c1f]49task_id_t task_get_id(void)
[f30e6a0b]50{
[dd8d5a7]51#ifdef __32_BITS__
[f30e6a0b]52 task_id_t task_id;
[d3b8c1f]53 (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
[ee369f3]54
[f30e6a0b]55 return task_id;
[dd8d5a7]56#endif /* __32_BITS__ */
57
58#ifdef __64_BITS__
59 return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
60#endif /* __64_BITS__ */
[f30e6a0b]61}
[b2951e2]62
[bc18d63]63/** Set the task name.
64 *
[ee369f3]65 * @param name The new name, typically the command used to execute the
66 * program.
67 *
68 * @return Zero on success or negative error code.
[bc18d63]69 */
70int task_set_name(const char *name)
71{
[79ae36dd]72 assert(name);
73
[9eb3623]74 return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
[bc18d63]75}
76
[1e9f8ab]77/** Kill a task.
78 *
79 * @param task_id ID of task to kill.
80 *
81 * @return Zero on success or negative error code.
82 */
83
84int task_kill(task_id_t task_id)
85{
86 return (int) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
87}
88
[45454e9b]89/** Create a new task by running an executable from the filesystem.
90 *
91 * This is really just a convenience wrapper over the more complicated
[0485135]92 * loader API. Arguments are passed as a null-terminated array of strings.
[c98e6ee]93 *
[79ae36dd]94 * @param id If not NULL, the ID of the task is stored here on success.
95 * @param path Pathname of the binary to execute.
96 * @param argv Command-line arguments.
97 *
98 * @return Zero on success or negative error code.
[ee369f3]99 *
[c98e6ee]100 */
[0485135]101int task_spawnv(task_id_t *id, const char *path, const char *const args[])
[c98e6ee]102{
[bfd1546]103 /* Connect to a program loader. */
[79ae36dd]104 loader_t *ldr = loader_connect();
[0485135]105 if (ldr == NULL)
106 return EREFUSED;
[ee369f3]107
[47e0a05b]108 /* Get task ID. */
[79ae36dd]109 task_id_t task_id;
110 int rc = loader_get_task_id(ldr, &task_id);
[45454e9b]111 if (rc != EOK)
[47e0a05b]112 goto error;
[ee369f3]113
[622cdbe]114 /* Send spawner's current working directory. */
115 rc = loader_set_cwd(ldr);
116 if (rc != EOK)
117 goto error;
118
[4470e26]119 /* Send program pathname. */
[45454e9b]120 rc = loader_set_pathname(ldr, path);
121 if (rc != EOK)
[17b2aac]122 goto error;
[ee369f3]123
[4470e26]124 /* Send arguments. */
[ee369f3]125 rc = loader_set_args(ldr, args);
[17b2aac]126 if (rc != EOK)
127 goto error;
[ee369f3]128
129 /* Send default files */
[99272a3]130 fdi_node_t *files[4];
131 fdi_node_t stdin_node;
132 fdi_node_t stdout_node;
133 fdi_node_t stderr_node;
[ee369f3]134
[a68f737]135 if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
[ee369f3]136 files[0] = &stdin_node;
[a68f737]137 else
[ee369f3]138 files[0] = NULL;
139
[a68f737]140 if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
[ee369f3]141 files[1] = &stdout_node;
[a68f737]142 else
[ee369f3]143 files[1] = NULL;
144
[a68f737]145 if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
[ee369f3]146 files[2] = &stderr_node;
[a68f737]147 else
[ee369f3]148 files[2] = NULL;
149
150 files[3] = NULL;
151
152 rc = loader_set_files(ldr, files);
153 if (rc != EOK)
154 goto error;
155
[4470e26]156 /* Load the program. */
157 rc = loader_load_program(ldr);
158 if (rc != EOK)
159 goto error;
[ee369f3]160
[4470e26]161 /* Run it. */
162 rc = loader_run(ldr);
[17b2aac]163 if (rc != EOK)
164 goto error;
[ee369f3]165
[c98e6ee]166 /* Success */
[0485135]167 if (id != NULL)
168 *id = task_id;
[d9fae235]169
[0485135]170 return EOK;
[ee369f3]171
[c98e6ee]172error:
[ee369f3]173 /* Error exit */
[45454e9b]174 loader_abort(ldr);
[0485135]175 return rc;
176}
177
178/** Create a new task by running an executable from the filesystem.
179 *
180 * This is really just a convenience wrapper over the more complicated
181 * loader API. Arguments are passed as a null-terminated list of arguments.
182 *
[79ae36dd]183 * @param id If not NULL, the ID of the task is stored here on success.
184 * @param path Pathname of the binary to execute.
185 * @param ... Command-line arguments.
186 *
187 * @return Zero on success or negative error code.
[0485135]188 *
189 */
190int task_spawnl(task_id_t *task_id, const char *path, ...)
191{
[79ae36dd]192 /* Count the number of arguments. */
193
[0485135]194 va_list ap;
195 const char *arg;
196 const char **arglist;
[79ae36dd]197 int cnt = 0;
198
[0485135]199 va_start(ap, path);
200 do {
201 arg = va_arg(ap, const char *);
202 cnt++;
203 } while (arg != NULL);
204 va_end(ap);
[79ae36dd]205
[0485135]206 /* Allocate argument list. */
207 arglist = malloc(cnt * sizeof(const char *));
208 if (arglist == NULL)
209 return ENOMEM;
[79ae36dd]210
[0485135]211 /* Fill in arguments. */
212 cnt = 0;
213 va_start(ap, path);
214 do {
215 arg = va_arg(ap, const char *);
216 arglist[cnt++] = arg;
217 } while (arg != NULL);
218 va_end(ap);
[79ae36dd]219
[0485135]220 /* Spawn task. */
[79ae36dd]221 int rc = task_spawnv(task_id, path, arglist);
222
[0485135]223 /* Free argument list. */
224 free(arglist);
225 return rc;
[adb4d6c2]226}
227
[adb49f58]228int task_wait(task_id_t id, task_exit_t *texit, int *retval)
[ee369f3]229{
[79ae36dd]230 assert(texit);
231 assert(retval);
232
233 async_exch_t *exch = async_exchange_begin(session_ns);
[96b02eb9]234 sysarg_t te, rv;
[79ae36dd]235 int rc = (int) async_req_2_2(exch, NS_TASK_WAIT, LOWER32(id),
[adb49f58]236 UPPER32(id), &te, &rv);
[79ae36dd]237 async_exchange_end(exch);
238
[adb49f58]239 *texit = te;
[7114d83]240 *retval = rv;
[79ae36dd]241
[7114d83]242 return rc;
243}
244
245int task_retval(int val)
246{
[79ae36dd]247 async_exch_t *exch = async_exchange_begin(session_ns);
248 int rc = (int) async_req_1_0(exch, NS_RETVAL, val);
249 async_exchange_end(exch);
250
251 return rc;
[ee369f3]252}
253
[fadd381]254/** @}
[b2951e2]255 */
Note: See TracBrowser for help on using the repository browser.