source: mainline/uspace/lib/c/generic/task.c@ 7907cf9

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

32/64 ABI split for SYS_TASK_GET_ID

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
3 * Copyright (c) 2008 Jiri Svoboda
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.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33/** @file
34 */
35
36#include <task.h>
37#include <libc.h>
38#include <stdlib.h>
39#include <errno.h>
40#include <loader/loader.h>
41#include <stdarg.h>
42#include <str.h>
43#include <ipc/ns.h>
44#include <macros.h>
45#include <async.h>
46
47task_id_t task_get_id(void)
48{
49#ifdef __32_BITS__
50 task_id_t task_id;
51 (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
52
53 return task_id;
54#endif /* __32_BITS__ */
55
56#ifdef __64_BITS__
57 return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
58#endif /* __64_BITS__ */
59}
60
61/** Set the task name.
62 *
63 * @param name The new name, typically the command used to execute the
64 * program.
65 *
66 * @return Zero on success or negative error code.
67 *
68 */
69int task_set_name(const char *name)
70{
71 return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
72}
73
74/** Create a new task by running an executable from the filesystem.
75 *
76 * This is really just a convenience wrapper over the more complicated
77 * loader API. Arguments are passed as a null-terminated array of strings.
78 *
79 * @param id If not NULL, the ID of the task is stored here on success.
80 * @param path Pathname of the binary to execute.
81 * @param argv Command-line arguments.
82 *
83 * @return Zero on success or negative error code.
84 */
85int task_spawnv(task_id_t *id, const char *path, const char *const args[])
86{
87 loader_t *ldr;
88 task_id_t task_id;
89 int rc;
90
91 /* Connect to a program loader. */
92 ldr = loader_connect();
93 if (ldr == NULL)
94 return EREFUSED;
95
96 /* Get task ID. */
97 rc = loader_get_task_id(ldr, &task_id);
98 if (rc != EOK)
99 goto error;
100
101 /* Send spawner's current working directory. */
102 rc = loader_set_cwd(ldr);
103 if (rc != EOK)
104 goto error;
105
106 /* Send program pathname. */
107 rc = loader_set_pathname(ldr, path);
108 if (rc != EOK)
109 goto error;
110
111 /* Send arguments. */
112 rc = loader_set_args(ldr, args);
113 if (rc != EOK)
114 goto error;
115
116 /* Send default files */
117 fdi_node_t *files[4];
118 fdi_node_t stdin_node;
119 fdi_node_t stdout_node;
120 fdi_node_t stderr_node;
121
122 if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
123 files[0] = &stdin_node;
124 else
125 files[0] = NULL;
126
127 if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
128 files[1] = &stdout_node;
129 else
130 files[1] = NULL;
131
132 if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
133 files[2] = &stderr_node;
134 else
135 files[2] = NULL;
136
137 files[3] = NULL;
138
139 rc = loader_set_files(ldr, files);
140 if (rc != EOK)
141 goto error;
142
143 /* Load the program. */
144 rc = loader_load_program(ldr);
145 if (rc != EOK)
146 goto error;
147
148 /* Run it. */
149 rc = loader_run(ldr);
150 if (rc != EOK)
151 goto error;
152
153 /* Success */
154 free(ldr);
155
156 if (id != NULL)
157 *id = task_id;
158
159 return EOK;
160
161error:
162 /* Error exit */
163 loader_abort(ldr);
164 free(ldr);
165 return rc;
166}
167
168/** Create a new task by running an executable from the filesystem.
169 *
170 * This is really just a convenience wrapper over the more complicated
171 * loader API. Arguments are passed as a null-terminated list of arguments.
172 *
173 * @param id If not NULL, the ID of the task is stored here on success.
174 * @param path Pathname of the binary to execute.
175 * @param ... Command-line arguments.
176 *
177 * @return Zero on success or negative error code.
178 */
179int task_spawnl(task_id_t *task_id, const char *path, ...)
180{
181 va_list ap;
182 int rc, cnt;
183 const char *arg;
184 const char **arglist;
185
186 /* Count the number of arguments. */
187 cnt = 0;
188 va_start(ap, path);
189 do {
190 arg = va_arg(ap, const char *);
191 cnt++;
192 } while (arg != NULL);
193 va_end(ap);
194
195 /* Allocate argument list. */
196 arglist = malloc(cnt * sizeof(const char *));
197 if (arglist == NULL)
198 return ENOMEM;
199
200 /* Fill in arguments. */
201 cnt = 0;
202 va_start(ap, path);
203 do {
204 arg = va_arg(ap, const char *);
205 arglist[cnt++] = arg;
206 } while (arg != NULL);
207 va_end(ap);
208
209 /* Spawn task. */
210 rc = task_spawnv(task_id, path, arglist);
211
212 /* Free argument list. */
213 free(arglist);
214 return rc;
215}
216
217int task_wait(task_id_t id, task_exit_t *texit, int *retval)
218{
219 sysarg_t te, rv;
220 int rc;
221
222 rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
223 UPPER32(id), &te, &rv);
224 *texit = te;
225 *retval = rv;
226
227 return rc;
228}
229
230int task_retval(int val)
231{
232 return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
233}
234
235/** @}
236 */
Note: See TracBrowser for help on using the repository browser.