source: mainline/uspace/lib/libc/generic/task.c@ 958de16

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

make sure that all statically allocated strings are declared as "const char *"
and are treated as read-only

  • Property mode set to 100644
File size: 4.1 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 <string.h>
42#include <ipc/ns.h>
43#include <macros.h>
44#include <async.h>
45
46task_id_t task_get_id(void)
47{
48 task_id_t task_id;
49 (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
50
51 return task_id;
52}
53
54/** Set the task name.
55 *
56 * @param name The new name, typically the command used to execute the
57 * program.
58 *
59 * @return Zero on success or negative error code.
60 *
61 */
62int task_set_name(const char *name)
63{
64 return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
65}
66
67/** Create a new task by running an executable from the filesystem.
68 *
69 * This is really just a convenience wrapper over the more complicated
70 * loader API.
71 *
72 * @param path pathname of the binary to execute
73 * @param argv command-line arguments
74 *
75 * @return ID of the newly created task or zero on error.
76 *
77 */
78task_id_t task_spawn(const char *path, const char *const args[])
79{
80 /* Connect to a program loader. */
81 loader_t *ldr = loader_connect();
82 if (ldr == NULL)
83 return 0;
84
85 /* Get task ID. */
86 task_id_t task_id;
87 int rc = loader_get_task_id(ldr, &task_id);
88 if (rc != EOK)
89 goto error;
90
91 /* Send spawner's current working directory. */
92 rc = loader_set_cwd(ldr);
93 if (rc != EOK)
94 goto error;
95
96 /* Send program pathname. */
97 rc = loader_set_pathname(ldr, path);
98 if (rc != EOK)
99 goto error;
100
101 /* Send arguments. */
102 rc = loader_set_args(ldr, args);
103 if (rc != EOK)
104 goto error;
105
106 /* Send default files */
107 fdi_node_t *files[4];
108 fdi_node_t stdin_node;
109 fdi_node_t stdout_node;
110 fdi_node_t stderr_node;
111
112 if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
113 files[0] = &stdin_node;
114 else
115 files[0] = NULL;
116
117 if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
118 files[1] = &stdout_node;
119 else
120 files[1] = NULL;
121
122 if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
123 files[2] = &stderr_node;
124 else
125 files[2] = NULL;
126
127 files[3] = NULL;
128
129 rc = loader_set_files(ldr, files);
130 if (rc != EOK)
131 goto error;
132
133 /* Load the program. */
134 rc = loader_load_program(ldr);
135 if (rc != EOK)
136 goto error;
137
138 /* Run it. */
139 rc = loader_run(ldr);
140 if (rc != EOK)
141 goto error;
142
143 /* Success */
144 free(ldr);
145 return task_id;
146
147error:
148 /* Error exit */
149 loader_abort(ldr);
150 free(ldr);
151
152 return 0;
153}
154
155int task_wait(task_id_t id, task_exit_t *texit, int *retval)
156{
157 ipcarg_t te, rv;
158 int rc;
159
160 rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
161 UPPER32(id), &te, &rv);
162 *texit = te;
163 *retval = rv;
164
165 return rc;
166}
167
168int task_retval(int val)
169{
170 return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
171}
172
173/** @}
174 */
Note: See TracBrowser for help on using the repository browser.