source: mainline/uspace/lib/c/generic/task.c@ 3c22f70

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3c22f70 was 1e9f8ab, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Add kill command to forcefully terminate a task.

  • Property mode set to 100644
File size: 5.8 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 */
68int task_set_name(const char *name)
69{
70 return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
71}
72
73/** Kill a task.
74 *
75 * @param task_id ID of task to kill.
76 *
77 * @return Zero on success or negative error code.
78 */
79
80int task_kill(task_id_t task_id)
81{
82 return (int) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
83}
84
85/** Create a new task by running an executable from the filesystem.
86 *
87 * This is really just a convenience wrapper over the more complicated
88 * loader API. Arguments are passed as a null-terminated array of strings.
89 *
90 * @param id If not NULL, the ID of the task is stored here on success.
91 * @param path Pathname of the binary to execute.
92 * @param argv Command-line arguments.
93 *
94 * @return Zero on success or negative error code.
95 */
96int task_spawnv(task_id_t *id, const char *path, const char *const args[])
97{
98 loader_t *ldr;
99 task_id_t task_id;
100 int rc;
101
102 /* Connect to a program loader. */
103 ldr = loader_connect();
104 if (ldr == NULL)
105 return EREFUSED;
106
107 /* Get task ID. */
108 rc = loader_get_task_id(ldr, &task_id);
109 if (rc != EOK)
110 goto error;
111
112 /* Send spawner's current working directory. */
113 rc = loader_set_cwd(ldr);
114 if (rc != EOK)
115 goto error;
116
117 /* Send program pathname. */
118 rc = loader_set_pathname(ldr, path);
119 if (rc != EOK)
120 goto error;
121
122 /* Send arguments. */
123 rc = loader_set_args(ldr, args);
124 if (rc != EOK)
125 goto error;
126
127 /* Send default files */
128 fdi_node_t *files[4];
129 fdi_node_t stdin_node;
130 fdi_node_t stdout_node;
131 fdi_node_t stderr_node;
132
133 if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
134 files[0] = &stdin_node;
135 else
136 files[0] = NULL;
137
138 if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
139 files[1] = &stdout_node;
140 else
141 files[1] = NULL;
142
143 if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
144 files[2] = &stderr_node;
145 else
146 files[2] = NULL;
147
148 files[3] = NULL;
149
150 rc = loader_set_files(ldr, files);
151 if (rc != EOK)
152 goto error;
153
154 /* Load the program. */
155 rc = loader_load_program(ldr);
156 if (rc != EOK)
157 goto error;
158
159 /* Run it. */
160 rc = loader_run(ldr);
161 if (rc != EOK)
162 goto error;
163
164 /* Success */
165 free(ldr);
166
167 if (id != NULL)
168 *id = task_id;
169
170 return EOK;
171
172error:
173 /* Error exit */
174 loader_abort(ldr);
175 free(ldr);
176 return rc;
177}
178
179/** Create a new task by running an executable from the filesystem.
180 *
181 * This is really just a convenience wrapper over the more complicated
182 * loader API. Arguments are passed as a null-terminated list of arguments.
183 *
184 * @param id If not NULL, the ID of the task is stored here on success.
185 * @param path Pathname of the binary to execute.
186 * @param ... Command-line arguments.
187 *
188 * @return Zero on success or negative error code.
189 */
190int task_spawnl(task_id_t *task_id, const char *path, ...)
191{
192 va_list ap;
193 int rc, cnt;
194 const char *arg;
195 const char **arglist;
196
197 /* Count the number of arguments. */
198 cnt = 0;
199 va_start(ap, path);
200 do {
201 arg = va_arg(ap, const char *);
202 cnt++;
203 } while (arg != NULL);
204 va_end(ap);
205
206 /* Allocate argument list. */
207 arglist = malloc(cnt * sizeof(const char *));
208 if (arglist == NULL)
209 return ENOMEM;
210
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);
219
220 /* Spawn task. */
221 rc = task_spawnv(task_id, path, arglist);
222
223 /* Free argument list. */
224 free(arglist);
225 return rc;
226}
227
228int task_wait(task_id_t id, task_exit_t *texit, int *retval)
229{
230 sysarg_t te, rv;
231 int rc;
232
233 rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
234 UPPER32(id), &te, &rv);
235 *texit = te;
236 *retval = rv;
237
238 return rc;
239}
240
241int task_retval(int val)
242{
243 return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
244}
245
246/** @}
247 */
Note: See TracBrowser for help on using the repository browser.