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 <loader/loader.h>
|
---|
38 | #include <stdarg.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <ipc/ns.h>
|
---|
41 | #include <macros.h>
|
---|
42 | #include <assert.h>
|
---|
43 | #include <async.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <malloc.h>
|
---|
46 | #include <libc.h>
|
---|
47 | #include "private/ns.h"
|
---|
48 |
|
---|
49 | task_id_t task_get_id(void)
|
---|
50 | {
|
---|
51 | #ifdef __32_BITS__
|
---|
52 | task_id_t task_id;
|
---|
53 | (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
|
---|
54 |
|
---|
55 | return task_id;
|
---|
56 | #endif /* __32_BITS__ */
|
---|
57 |
|
---|
58 | #ifdef __64_BITS__
|
---|
59 | return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
|
---|
60 | #endif /* __64_BITS__ */
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Set the task name.
|
---|
64 | *
|
---|
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.
|
---|
69 | */
|
---|
70 | int task_set_name(const char *name)
|
---|
71 | {
|
---|
72 | assert(name);
|
---|
73 |
|
---|
74 | return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
|
---|
75 | }
|
---|
76 |
|
---|
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 |
|
---|
84 | int task_kill(task_id_t task_id)
|
---|
85 | {
|
---|
86 | return (int) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
|
---|
87 | }
|
---|
88 |
|
---|
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
|
---|
92 | * loader API. Arguments are passed as a null-terminated array of strings.
|
---|
93 | *
|
---|
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.
|
---|
99 | *
|
---|
100 | */
|
---|
101 | int task_spawnv(task_id_t *id, const char *path, const char *const args[])
|
---|
102 | {
|
---|
103 | /* Send default files */
|
---|
104 | fdi_node_t *files[4];
|
---|
105 | fdi_node_t stdin_node;
|
---|
106 | fdi_node_t stdout_node;
|
---|
107 | fdi_node_t stderr_node;
|
---|
108 |
|
---|
109 | if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
|
---|
110 | files[0] = &stdin_node;
|
---|
111 | else
|
---|
112 | files[0] = NULL;
|
---|
113 |
|
---|
114 | if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
|
---|
115 | files[1] = &stdout_node;
|
---|
116 | else
|
---|
117 | files[1] = NULL;
|
---|
118 |
|
---|
119 | if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
|
---|
120 | files[2] = &stderr_node;
|
---|
121 | else
|
---|
122 | files[2] = NULL;
|
---|
123 |
|
---|
124 | files[3] = NULL;
|
---|
125 |
|
---|
126 | return task_spawnvf(id, path, args, files);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Create a new task by running an executable from the filesystem.
|
---|
130 | *
|
---|
131 | * This is really just a convenience wrapper over the more complicated
|
---|
132 | * loader API. Arguments are passed as a null-terminated array of strings.
|
---|
133 | * Files are passed as null-terminated array of pointers to fdi_node_t.
|
---|
134 | *
|
---|
135 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
136 | * @param path Pathname of the binary to execute.
|
---|
137 | * @param argv Command-line arguments.
|
---|
138 | * @param files Standard files to use.
|
---|
139 | *
|
---|
140 | * @return Zero on success or negative error code.
|
---|
141 | *
|
---|
142 | */
|
---|
143 | int task_spawnvf(task_id_t *id, const char *path, const char *const args[],
|
---|
144 | fdi_node_t *const files[])
|
---|
145 | {
|
---|
146 | /* Connect to a program loader. */
|
---|
147 | loader_t *ldr = loader_connect();
|
---|
148 | if (ldr == NULL)
|
---|
149 | return EREFUSED;
|
---|
150 |
|
---|
151 | /* Get task ID. */
|
---|
152 | task_id_t task_id;
|
---|
153 | int rc = loader_get_task_id(ldr, &task_id);
|
---|
154 | if (rc != EOK)
|
---|
155 | goto error;
|
---|
156 |
|
---|
157 | /* Send spawner's current working directory. */
|
---|
158 | rc = loader_set_cwd(ldr);
|
---|
159 | if (rc != EOK)
|
---|
160 | goto error;
|
---|
161 |
|
---|
162 | /* Send program pathname. */
|
---|
163 | rc = loader_set_pathname(ldr, path);
|
---|
164 | if (rc != EOK)
|
---|
165 | goto error;
|
---|
166 |
|
---|
167 | /* Send arguments. */
|
---|
168 | rc = loader_set_args(ldr, args);
|
---|
169 | if (rc != EOK)
|
---|
170 | goto error;
|
---|
171 |
|
---|
172 | /* Send files */
|
---|
173 | rc = loader_set_files(ldr, files);
|
---|
174 | if (rc != EOK)
|
---|
175 | goto error;
|
---|
176 |
|
---|
177 | /* Load the program. */
|
---|
178 | rc = loader_load_program(ldr);
|
---|
179 | if (rc != EOK)
|
---|
180 | goto error;
|
---|
181 |
|
---|
182 | /* Run it. */
|
---|
183 | rc = loader_run(ldr);
|
---|
184 | if (rc != EOK)
|
---|
185 | goto error;
|
---|
186 |
|
---|
187 | /* Success */
|
---|
188 | if (id != NULL)
|
---|
189 | *id = task_id;
|
---|
190 |
|
---|
191 | return EOK;
|
---|
192 | error:
|
---|
193 | /* Error exit */
|
---|
194 | loader_abort(ldr);
|
---|
195 | return rc;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** Create a new task by running an executable from the filesystem.
|
---|
199 | *
|
---|
200 | * This is really just a convenience wrapper over the more complicated
|
---|
201 | * loader API. Arguments are passed as a null-terminated list of arguments.
|
---|
202 | *
|
---|
203 | * @param id If not NULL, the ID of the task is stored here on success.
|
---|
204 | * @param path Pathname of the binary to execute.
|
---|
205 | * @param ... Command-line arguments.
|
---|
206 | *
|
---|
207 | * @return Zero on success or negative error code.
|
---|
208 | *
|
---|
209 | */
|
---|
210 | int task_spawnl(task_id_t *task_id, const char *path, ...)
|
---|
211 | {
|
---|
212 | /* Count the number of arguments. */
|
---|
213 |
|
---|
214 | va_list ap;
|
---|
215 | const char *arg;
|
---|
216 | const char **arglist;
|
---|
217 | int cnt = 0;
|
---|
218 |
|
---|
219 | va_start(ap, path);
|
---|
220 | do {
|
---|
221 | arg = va_arg(ap, const char *);
|
---|
222 | cnt++;
|
---|
223 | } while (arg != NULL);
|
---|
224 | va_end(ap);
|
---|
225 |
|
---|
226 | /* Allocate argument list. */
|
---|
227 | arglist = malloc(cnt * sizeof(const char *));
|
---|
228 | if (arglist == NULL)
|
---|
229 | return ENOMEM;
|
---|
230 |
|
---|
231 | /* Fill in arguments. */
|
---|
232 | cnt = 0;
|
---|
233 | va_start(ap, path);
|
---|
234 | do {
|
---|
235 | arg = va_arg(ap, const char *);
|
---|
236 | arglist[cnt++] = arg;
|
---|
237 | } while (arg != NULL);
|
---|
238 | va_end(ap);
|
---|
239 |
|
---|
240 | /* Spawn task. */
|
---|
241 | int rc = task_spawnv(task_id, path, arglist);
|
---|
242 |
|
---|
243 | /* Free argument list. */
|
---|
244 | free(arglist);
|
---|
245 | return rc;
|
---|
246 | }
|
---|
247 |
|
---|
248 | int task_wait(task_id_t id, task_exit_t *texit, int *retval)
|
---|
249 | {
|
---|
250 | assert(texit);
|
---|
251 | assert(retval);
|
---|
252 |
|
---|
253 | async_exch_t *exch = async_exchange_begin(session_ns);
|
---|
254 | sysarg_t te, rv;
|
---|
255 | int rc = (int) async_req_2_2(exch, NS_TASK_WAIT, LOWER32(id),
|
---|
256 | UPPER32(id), &te, &rv);
|
---|
257 | async_exchange_end(exch);
|
---|
258 |
|
---|
259 | *texit = te;
|
---|
260 | *retval = rv;
|
---|
261 |
|
---|
262 | return rc;
|
---|
263 | }
|
---|
264 |
|
---|
265 | int task_retval(int val)
|
---|
266 | {
|
---|
267 | async_exch_t *exch = async_exchange_begin(session_ns);
|
---|
268 | int rc = (int) async_req_1_0(exch, NS_RETVAL, val);
|
---|
269 | async_exchange_end(exch);
|
---|
270 |
|
---|
271 | return rc;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** @}
|
---|
275 | */
|
---|