task.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2006 Jakub Jermar
00003  * Copyright (c) 2008 Jiri Svoboda
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * - Redistributions of source code must retain the above copyright
00011  *   notice, this list of conditions and the following disclaimer.
00012  * - Redistributions in binary form must reproduce the above copyright
00013  *   notice, this list of conditions and the following disclaimer in the
00014  *   documentation and/or other materials provided with the distribution.
00015  * - The name of the author may not be used to endorse or promote products
00016  *   derived from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00019  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00020  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00021  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00023  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00024  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00025  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00027  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  /* __32_BITS__ */
00055         
00056 #ifdef __64_BITS__
00057         return (task_id_t) __SYSCALL0(SYS_TASK_GET_ID);
00058 #endif  /* __64_BITS__ */
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         /* Connect to a program loader. */
00103         ldr = loader_connect();
00104         if (ldr == NULL)
00105                 return EREFUSED;
00106         
00107         /* Get task ID. */
00108         rc = loader_get_task_id(ldr, &task_id);
00109         if (rc != EOK)
00110                 goto error;
00111         
00112         /* Send spawner's current working directory. */
00113         rc = loader_set_cwd(ldr);
00114         if (rc != EOK)
00115                 goto error;
00116         
00117         /* Send program pathname. */
00118         rc = loader_set_pathname(ldr, path);
00119         if (rc != EOK)
00120                 goto error;
00121         
00122         /* Send arguments. */
00123         rc = loader_set_args(ldr, args);
00124         if (rc != EOK)
00125                 goto error;
00126         
00127         /* Send default files */
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         /* Load the program. */
00155         rc = loader_load_program(ldr);
00156         if (rc != EOK)
00157                 goto error;
00158         
00159         /* Run it. */
00160         rc = loader_run(ldr);
00161         if (rc != EOK)
00162                 goto error;
00163         
00164         /* Success */
00165         free(ldr);
00166         
00167         if (id != NULL)
00168                 *id = task_id;
00169         
00170         return EOK;
00171         
00172 error:
00173         /* Error exit */
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         /* Count the number of arguments. */
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         /* Allocate argument list. */
00207         arglist = malloc(cnt * sizeof(const char *));
00208         if (arglist == NULL)
00209                 return ENOMEM;
00210 
00211         /* Fill in arguments. */
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         /* Spawn task. */
00221         rc = task_spawnv(task_id, path, arglist);
00222 
00223         /* Free argument list. */
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 

Generated on Thu Jun 2 07:45:46 2011 for HelenOS/USB by  doxygen 1.4.7