Subversion Repositories HelenOS

Rev

Rev 4348 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1175 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2006 Jakub Jermar
3148 svoboda 3
 * Copyright (c) 2008 Jiri Svoboda
1175 jermar 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.
1653 cejka 28
 */
29
 
1866 jermar 30
/** @addtogroup libc
1653 cejka 31
 * @{
32
 */
33
/** @file
4691 svoboda 34
 */
1175 jermar 35
 
36
#include <task.h>
3102 svoboda 37
#include <libc.h>
3174 svoboda 38
#include <stdlib.h>
3004 svoboda 39
#include <errno.h>
3474 svoboda 40
#include <loader/loader.h>
4344 svoboda 41
#include <string.h>
4691 svoboda 42
#include <ipc/ns.h>
43
#include <macros.h>
44
#include <async.h>
1175 jermar 45
 
1228 jermar 46
task_id_t task_get_id(void)
1175 jermar 47
{
48
    task_id_t task_id;
1228 jermar 49
    (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
4691 svoboda 50
 
1175 jermar 51
    return task_id;
52
}
1653 cejka 53
 
4344 svoboda 54
/** Set the task name.
55
 *
4691 svoboda 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
 *
4344 svoboda 61
 */
62
int task_set_name(const char *name)
63
{
4348 svoboda 64
    return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
4344 svoboda 65
}
66
 
3474 svoboda 67
/** Create a new task by running an executable from the filesystem.
3148 svoboda 68
 *
3474 svoboda 69
 * This is really just a convenience wrapper over the more complicated
70
 * loader API.
71
 *
4691 svoboda 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
 *
3148 svoboda 77
 */
4691 svoboda 78
task_id_t task_spawn(const char *path, char *const args[])
3004 svoboda 79
{
4343 svoboda 80
    /* Connect to a program loader. */
4691 svoboda 81
    loader_t *ldr = loader_connect();
3474 svoboda 82
    if (ldr == NULL)
3403 svoboda 83
        return 0;
4691 svoboda 84
 
3474 svoboda 85
    /* Get task ID. */
4691 svoboda 86
    task_id_t task_id;
87
    int rc = loader_get_task_id(ldr, &task_id);
3403 svoboda 88
    if (rc != EOK)
3448 svoboda 89
        goto error;
4691 svoboda 90
 
3474 svoboda 91
    /* Send program pathname. */
92
    rc = loader_set_pathname(ldr, path);
93
    if (rc != EOK)
3448 svoboda 94
        goto error;
4691 svoboda 95
 
3474 svoboda 96
    /* Send arguments. */
4691 svoboda 97
    rc = loader_set_args(ldr, args);
3474 svoboda 98
    if (rc != EOK)
3403 svoboda 99
        goto error;
4691 svoboda 100
 
101
 
102
    /* Send default files */
103
    fdi_node_t *files[4];
104
    fdi_node_t stdin_node;
105
    fdi_node_t stdout_node;
106
    fdi_node_t stderr_node;
107
 
108
    if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
109
        files[0] = &stdin_node;
110
    else
111
        files[0] = NULL;
112
 
113
    if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
114
        files[1] = &stdout_node;
115
    else
116
        files[1] = NULL;
117
 
118
    if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
119
        files[2] = &stderr_node;
120
    else
121
        files[2] = NULL;
122
 
123
    files[3] = NULL;
124
 
125
    rc = loader_set_files(ldr, files);
126
    if (rc != EOK)
127
        goto error;
128
 
3474 svoboda 129
    /* Load the program. */
130
    rc = loader_load_program(ldr);
3403 svoboda 131
    if (rc != EOK)
132
        goto error;
4691 svoboda 133
 
3474 svoboda 134
    /* Run it. */
135
    rc = loader_run(ldr);
3403 svoboda 136
    if (rc != EOK)
137
        goto error;
4691 svoboda 138
 
3174 svoboda 139
    /* Success */
3474 svoboda 140
    free(ldr);
3448 svoboda 141
    return task_id;
4691 svoboda 142
 
143
error:
3174 svoboda 144
    /* Error exit */
3474 svoboda 145
    loader_abort(ldr);
146
    free(ldr);
4691 svoboda 147
 
3174 svoboda 148
    return 0;
3004 svoboda 149
}
150
 
4691 svoboda 151
int task_wait(task_id_t id, task_exit_t *texit, int *retval)
152
{
153
    ipcarg_t te, rv;
154
    int rc;
155
 
156
    rc = (int) async_req_2_2(PHONE_NS, NS_TASK_WAIT, LOWER32(id),
157
        UPPER32(id), &te, &rv);
158
    *texit = te;
159
    *retval = rv;
160
 
161
    return rc;
162
}
163
 
164
int task_retval(int val)
165
{
166
    return (int) async_req_1_0(PHONE_NS, NS_RETVAL, val);
167
}
168
 
1866 jermar 169
/** @}
1653 cejka 170
 */