Subversion Repositories HelenOS

Rev

Rev 4491 | Rev 4508 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4491 Rev 4492
1
/*
1
/*
2
 * Copyright (c) 2006 Jakub Jermar
2
 * Copyright (c) 2006 Jakub Jermar
3
 * Copyright (c) 2008 Jiri Svoboda
3
 * Copyright (c) 2008 Jiri Svoboda
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
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
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.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup libc
30
/** @addtogroup libc
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <task.h>
36
#include <task.h>
37
#include <libc.h>
37
#include <libc.h>
38
#include <stdlib.h>
38
#include <stdlib.h>
39
#include <errno.h>
39
#include <errno.h>
40
#include <loader/loader.h>
40
#include <loader/loader.h>
41
#include <string.h>
41
#include <string.h>
42
#include <ipc/ns.h>
42
#include <ipc/ns.h>
43
#include <macros.h>
43
#include <macros.h>
44
#include <async.h>
44
#include <async.h>
45
 
45
 
46
task_id_t task_get_id(void)
46
task_id_t task_get_id(void)
47
{
47
{
48
    task_id_t task_id;
48
    task_id_t task_id;
49
    (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
49
    (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
50
   
50
   
51
    return task_id;
51
    return task_id;
52
}
52
}
53
 
53
 
54
/** Set the task name.
54
/** Set the task name.
55
 *
55
 *
56
 * @param name The new name, typically the command used to execute the
56
 * @param name The new name, typically the command used to execute the
57
 *             program.
57
 *             program.
58
 *
58
 *
59
 * @return Zero on success or negative error code.
59
 * @return Zero on success or negative error code.
60
 *
60
 *
61
 */
61
 */
62
int task_set_name(const char *name)
62
int task_set_name(const char *name)
63
{
63
{
64
    return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
64
    return __SYSCALL2(SYS_TASK_SET_NAME, (sysarg_t) name, str_size(name));
65
}
65
}
66
 
66
 
67
/** Create a new task by running an executable from the filesystem.
67
/** Create a new task by running an executable from the filesystem.
68
 *
68
 *
69
 * This is really just a convenience wrapper over the more complicated
69
 * This is really just a convenience wrapper over the more complicated
70
 * loader API.
70
 * loader API.
71
 *
71
 *
72
 * @param path pathname of the binary to execute
72
 * @param path pathname of the binary to execute
73
 * @param argv command-line arguments
73
 * @param argv command-line arguments
74
 *
74
 *
75
 * @return ID of the newly created task or zero on error.
75
 * @return ID of the newly created task or zero on error.
76
 *
76
 *
77
 */
77
 */
78
task_id_t task_spawn(const char *path, char *const args[])
78
task_id_t task_spawn(const char *path, char *const args[])
79
{
79
{
80
    /* Connect to a program loader. */
80
    /* Connect to a program loader. */
81
    loader_t *ldr = loader_connect();
81
    loader_t *ldr = loader_connect();
82
    if (ldr == NULL)
82
    if (ldr == NULL)
83
        return 0;
83
        return 0;
84
   
84
   
85
    /* Get task ID. */
85
    /* Get task ID. */
86
    task_id_t task_id;
86
    task_id_t task_id;
87
    int rc = loader_get_task_id(ldr, &task_id);
87
    int rc = loader_get_task_id(ldr, &task_id);
88
    if (rc != EOK)
88
    if (rc != EOK)
89
        goto error;
89
        goto error;
90
   
90
   
91
    /* Send program pathname. */
91
    /* Send program pathname. */
92
    rc = loader_set_pathname(ldr, path);
92
    rc = loader_set_pathname(ldr, path);
93
    if (rc != EOK)
93
    if (rc != EOK)
94
        goto error;
94
        goto error;
95
   
95
   
96
    /* Send arguments. */
96
    /* Send arguments. */
97
    rc = loader_set_args(ldr, args);
97
    rc = loader_set_args(ldr, args);
98
    if (rc != EOK)
98
    if (rc != EOK)
99
        goto error;
99
        goto error;
100
   
100
   
101
   
101
   
102
    /* Send default files */
102
    /* Send default files */
103
    inode_t *files[4];
103
    fdi_node_t *files[4];
104
    inode_t stdin_node;
104
    fdi_node_t stdin_node;
105
    inode_t stdout_node;
105
    fdi_node_t stdout_node;
106
    inode_t stderr_node;
106
    fdi_node_t stderr_node;
107
   
107
   
108
    if ((stdin != NULL) && (stdin != &stdin_null)) {
108
    if ((stdin != NULL) && (stdin != &stdin_null)) {
109
        fnode(stdin, &stdin_node);
109
        fnode(stdin, &stdin_node);
110
        files[0] = &stdin_node;
110
        files[0] = &stdin_node;
111
    } else
111
    } else
112
        files[0] = NULL;
112
        files[0] = NULL;
113
   
113
   
114
    if ((stdout != NULL) && (stdout != &stdout_klog)) {
114
    if ((stdout != NULL) && (stdout != &stdout_klog)) {
115
        fnode(stdout, &stdout_node);
115
        fnode(stdout, &stdout_node);
116
        files[1] = &stdout_node;
116
        files[1] = &stdout_node;
117
    } else
117
    } else
118
        files[1] = NULL;
118
        files[1] = NULL;
119
   
119
   
120
    if ((stderr != NULL) && (stderr != &stdout_klog)) {
120
    if ((stderr != NULL) && (stderr != &stdout_klog)) {
121
        fnode(stderr, &stderr_node);
121
        fnode(stderr, &stderr_node);
122
        files[2] = &stderr_node;
122
        files[2] = &stderr_node;
123
    } else
123
    } else
124
        files[2] = NULL;
124
        files[2] = NULL;
125
   
125
   
126
    files[3] = NULL;
126
    files[3] = NULL;
127
   
127
   
128
    rc = loader_set_files(ldr, files);
128
    rc = loader_set_files(ldr, files);
129
    if (rc != EOK)
129
    if (rc != EOK)
130
        goto error;
130
        goto error;
131
   
131
   
132
    /* Load the program. */
132
    /* Load the program. */
133
    rc = loader_load_program(ldr);
133
    rc = loader_load_program(ldr);
134
    if (rc != EOK)
134
    if (rc != EOK)
135
        goto error;
135
        goto error;
136
   
136
   
137
    /* Run it. */
137
    /* Run it. */
138
    rc = loader_run(ldr);
138
    rc = loader_run(ldr);
139
    if (rc != EOK)
139
    if (rc != EOK)
140
        goto error;
140
        goto error;
141
   
141
   
142
    /* Success */
142
    /* Success */
143
    free(ldr);
143
    free(ldr);
144
    return task_id;
144
    return task_id;
145
   
145
   
146
error:
146
error:
147
    /* Error exit */
147
    /* Error exit */
148
    loader_abort(ldr);
148
    loader_abort(ldr);
149
    free(ldr);
149
    free(ldr);
150
   
150
   
151
    return 0;
151
    return 0;
152
}
152
}
153
 
153
 
154
int task_wait(task_id_t id)
154
int task_wait(task_id_t id)
155
{
155
{
156
    return (int) async_req_2_0(PHONE_NS, NS_TASK_WAIT, LOWER32(id), UPPER32(id));
156
    return (int) async_req_2_0(PHONE_NS, NS_TASK_WAIT, LOWER32(id), UPPER32(id));
157
}
157
}
158
 
158
 
159
/** @}
159
/** @}
160
 */
160
 */
161
 
161