Changeset b0f00a9 in mainline for uspace/lib/c/generic/task.c


Ignore:
Timestamp:
2011-11-06T22:21:05Z (15 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/fix-logger-deadlock, topic/msim-upgrade, topic/simplify-dev-export
Children:
898e847
Parents:
2bdf8313 (diff), 7b5f4c9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/task.c

    r2bdf8313 rb0f00a9  
    3535
    3636#include <task.h>
    37 #include <libc.h>
    38 #include <stdlib.h>
    39 #include <errno.h>
    4037#include <loader/loader.h>
    4138#include <stdarg.h>
     
    4340#include <ipc/ns.h>
    4441#include <macros.h>
     42#include <assert.h>
    4543#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>
    4649
    4750task_id_t task_get_id(void)
     
    6871int task_set_name(const char *name)
    6972{
     73        assert(name);
     74       
    7075        return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
    7176}
     
    8893 * loader API. Arguments are passed as a null-terminated array of strings.
    8994 *
    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 * @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 *
    95101 */
    96102int task_spawnv(task_id_t *id, const char *path, const char *const args[])
    97103{
    98         loader_t *ldr;
    99         task_id_t task_id;
    100         int rc;
    101 
     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 */
     144int task_spawnvf(task_id_t *id, const char *path, const char *const args[],
     145    int *const files[])
     146{
    102147        /* Connect to a program loader. */
    103         ldr = loader_connect();
     148        loader_t *ldr = loader_connect();
    104149        if (ldr == NULL)
    105150                return EREFUSED;
    106151       
    107152        /* Get task ID. */
    108         rc = loader_get_task_id(ldr, &task_id);
     153        task_id_t task_id;
     154        int rc = loader_get_task_id(ldr, &task_id);
    109155        if (rc != EOK)
    110156                goto error;
     
    125171                goto error;
    126172       
    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        
     173        /* Send files */
    150174        rc = loader_set_files(ldr, files);
    151175        if (rc != EOK)
     
    163187       
    164188        /* Success */
    165         free(ldr);
    166        
    167189        if (id != NULL)
    168190                *id = task_id;
     
    173195        /* Error exit */
    174196        loader_abort(ldr);
    175         free(ldr);
    176197        return rc;
    177198}
     
    182203 * loader API. Arguments are passed as a null-terminated list of arguments.
    183204 *
    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.
     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 *
    189211 */
    190212int task_spawnl(task_id_t *task_id, const char *path, ...)
    191213{
     214        /* Count the number of arguments. */
     215       
    192216        va_list ap;
    193         int rc, cnt;
    194217        const char *arg;
    195218        const char **arglist;
    196 
    197         /* Count the number of arguments. */
    198         cnt = 0;
     219        int cnt = 0;
     220       
    199221        va_start(ap, path);
    200222        do {
     
    203225        } while (arg != NULL);
    204226        va_end(ap);
    205 
     227       
    206228        /* Allocate argument list. */
    207229        arglist = malloc(cnt * sizeof(const char *));
    208230        if (arglist == NULL)
    209231                return ENOMEM;
    210 
     232       
    211233        /* Fill in arguments. */
    212234        cnt = 0;
     
    217239        } while (arg != NULL);
    218240        va_end(ap);
    219 
     241       
    220242        /* Spawn task. */
    221         rc = task_spawnv(task_id, path, arglist);
    222 
     243        int rc = task_spawnv(task_id, path, arglist);
     244       
    223245        /* Free argument list. */
    224246        free(arglist);
     
    228250int task_wait(task_id_t id, task_exit_t *texit, int *retval)
    229251{
     252        assert(texit);
     253        assert(retval);
     254       
     255        async_exch_t *exch = async_exchange_begin(session_ns);
    230256        sysarg_t te, rv;
    231         int rc;
    232 
    233         rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
     257        int rc = (int) async_req_2_2(exch, NS_TASK_WAIT, LOWER32(id),
    234258            UPPER32(id), &te, &rv);
     259        async_exchange_end(exch);
     260       
    235261        *texit = te;
    236262        *retval = rv;
    237 
     263       
    238264        return rc;
    239265}
     
    241267int task_retval(int val)
    242268{
    243         return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
     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;
    244274}
    245275
Note: See TracChangeset for help on using the changeset viewer.