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