00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00036 #include <task.h>
00037 #include <libc.h>
00038 #include <stdlib.h>
00039 #include <errno.h>
00040 #include <loader/loader.h>
00041 #include <stdarg.h>
00042 #include <str.h>
00043 #include <ipc/ns.h>
00044 #include <macros.h>
00045 #include <async.h>
00046
00047 task_id_t task_get_id(void)
00048 {
00049 #ifdef __32_BITS__
00050 task_id_t task_id;
00051 (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
00052
00053 return task_id;
00054 #endif
00055
00056 #ifdef __64_BITS__
00057 return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
00058 #endif
00059 }
00060
00068 int task_set_name(const char *name)
00069 {
00070 return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
00071 }
00072
00080 int task_kill(task_id_t task_id)
00081 {
00082 return (int) __SYSCALL1(SYS_TASK_KILL, (sysarg_t) &task_id);
00083 }
00084
00096 int task_spawnv(task_id_t *id, const char *path, const char *const args[])
00097 {
00098 loader_t *ldr;
00099 task_id_t task_id;
00100 int rc;
00101
00102
00103 ldr = loader_connect();
00104 if (ldr == NULL)
00105 return EREFUSED;
00106
00107
00108 rc = loader_get_task_id(ldr, &task_id);
00109 if (rc != EOK)
00110 goto error;
00111
00112
00113 rc = loader_set_cwd(ldr);
00114 if (rc != EOK)
00115 goto error;
00116
00117
00118 rc = loader_set_pathname(ldr, path);
00119 if (rc != EOK)
00120 goto error;
00121
00122
00123 rc = loader_set_args(ldr, args);
00124 if (rc != EOK)
00125 goto error;
00126
00127
00128 fdi_node_t *files[4];
00129 fdi_node_t stdin_node;
00130 fdi_node_t stdout_node;
00131 fdi_node_t stderr_node;
00132
00133 if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
00134 files[0] = &stdin_node;
00135 else
00136 files[0] = NULL;
00137
00138 if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
00139 files[1] = &stdout_node;
00140 else
00141 files[1] = NULL;
00142
00143 if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
00144 files[2] = &stderr_node;
00145 else
00146 files[2] = NULL;
00147
00148 files[3] = NULL;
00149
00150 rc = loader_set_files(ldr, files);
00151 if (rc != EOK)
00152 goto error;
00153
00154
00155 rc = loader_load_program(ldr);
00156 if (rc != EOK)
00157 goto error;
00158
00159
00160 rc = loader_run(ldr);
00161 if (rc != EOK)
00162 goto error;
00163
00164
00165 free(ldr);
00166
00167 if (id != NULL)
00168 *id = task_id;
00169
00170 return EOK;
00171
00172 error:
00173
00174 loader_abort(ldr);
00175 free(ldr);
00176 return rc;
00177 }
00178
00190 int task_spawnl(task_id_t *task_id, const char *path, ...)
00191 {
00192 va_list ap;
00193 int rc, cnt;
00194 const char *arg;
00195 const char **arglist;
00196
00197
00198 cnt = 0;
00199 va_start(ap, path);
00200 do {
00201 arg = va_arg(ap, const char *);
00202 cnt++;
00203 } while (arg != NULL);
00204 va_end(ap);
00205
00206
00207 arglist = malloc(cnt * sizeof(const char *));
00208 if (arglist == NULL)
00209 return ENOMEM;
00210
00211
00212 cnt = 0;
00213 va_start(ap, path);
00214 do {
00215 arg = va_arg(ap, const char *);
00216 arglist[cnt++] = arg;
00217 } while (arg != NULL);
00218 va_end(ap);
00219
00220
00221 rc = task_spawnv(task_id, path, arglist);
00222
00223
00224 free(arglist);
00225 return rc;
00226 }
00227
00228 int task_wait(task_id_t id, task_exit_t *texit, int *retval)
00229 {
00230 sysarg_t te, rv;
00231 int rc;
00232
00233 rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
00234 UPPER32(id), &te, &rv);
00235 *texit = te;
00236 *retval = rv;
00237
00238 return rc;
00239 }
00240
00241 int task_retval(int val)
00242 {
00243 return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
00244 }
00245