Subversion Repositories HelenOS

Rev

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

Rev 3174 Rev 3190
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 <ipc/ipc.h>
37
#include <ipc/ipc.h>
38
#include <ipc/loader.h>
38
#include <ipc/loader.h>
39
#include <libc.h>
39
#include <libc.h>
40
#include <string.h>
40
#include <string.h>
41
#include <stdlib.h>
41
#include <stdlib.h>
42
#include <async.h>
42
#include <async.h>
43
#include <errno.h>
43
#include <errno.h>
44
 
44
 
45
task_id_t task_get_id(void)
45
task_id_t task_get_id(void)
46
{
46
{
47
    task_id_t task_id;
47
    task_id_t task_id;
48
 
48
 
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
static int task_spawn_loader(void)
54
static int task_spawn_loader(void)
55
{
55
{
56
    int phone_id, rc;
56
    int phone_id, rc;
57
 
57
 
58
    rc = __SYSCALL1(SYS_TASK_SPAWN_LOADER, (sysarg_t) &phone_id);
58
    rc = __SYSCALL1(SYS_TASK_SPAWN_LOADER, (sysarg_t) &phone_id);
59
    if (rc != 0)
59
    if (rc != 0)
60
        return rc;
60
        return rc;
61
 
61
 
62
    return phone_id;
62
    return phone_id;
63
}
63
}
64
 
64
 
65
static int loader_set_args(int phone_id, const char *argv[])
65
static int loader_set_args(int phone_id, const char *argv[])
66
{
66
{
67
    aid_t req;
67
    aid_t req;
68
    ipc_call_t answer;
68
    ipc_call_t answer;
69
    int rc;
69
    int rc;
70
 
70
 
71
    const char **ap;
71
    const char **ap;
72
    char *dp;
72
    char *dp;
73
    char *arg_buf;
73
    char *arg_buf;
74
    size_t buffer_size;
74
    size_t buffer_size;
75
    size_t len;
75
    size_t len;
76
 
76
 
77
    /*
77
    /*
78
     * Serialize the arguments into a single array. First
78
     * Serialize the arguments into a single array. First
79
     * compute size of the buffer needed.
79
     * compute size of the buffer needed.
80
     */
80
     */
81
    ap = argv;
81
    ap = argv;
82
    buffer_size = 0;
82
    buffer_size = 0;
83
    while (*ap != NULL) {
83
    while (*ap != NULL) {
84
        buffer_size += strlen(*ap) + 1;
84
        buffer_size += strlen(*ap) + 1;
85
        ++ap;
85
        ++ap;
86
    }
86
    }
87
 
87
 
88
    arg_buf = malloc(buffer_size);
88
    arg_buf = malloc(buffer_size);
89
    if (arg_buf == NULL) return ENOMEM;
89
    if (arg_buf == NULL) return ENOMEM;
90
 
90
 
91
    /* Now fill the buffer with null-terminated argument strings */
91
    /* Now fill the buffer with null-terminated argument strings */
92
    ap = argv;
92
    ap = argv;
93
    dp = arg_buf;
93
    dp = arg_buf;
94
    while (*ap != NULL) {
94
    while (*ap != NULL) {
95
        strcpy(dp, *ap);
95
        strcpy(dp, *ap);
96
        dp += strlen(*ap) + 1;
96
        dp += strlen(*ap) + 1;
97
 
97
 
98
        ++ap;
98
        ++ap;
99
    }
99
    }
100
 
100
 
101
    /* Send serialized arguments to the loader */
101
    /* Send serialized arguments to the loader */
102
 
102
 
103
    req = async_send_0(phone_id, LOADER_SET_ARGS, &answer);
103
    req = async_send_0(phone_id, LOADER_SET_ARGS, &answer);
104
    rc = ipc_data_write_start(phone_id, (void *)arg_buf, buffer_size);
104
    rc = ipc_data_write_start(phone_id, (void *)arg_buf, buffer_size);
105
    if (rc != EOK) {
105
    if (rc != EOK) {
106
        async_wait_for(req, NULL);
106
        async_wait_for(req, NULL);
107
        return rc;
107
        return rc;
108
    }
108
    }
109
 
109
 
110
    async_wait_for(req, &rc);
110
    async_wait_for(req, &rc);
111
    if (rc != EOK) return rc;
111
    if (rc != EOK) return rc;
112
 
112
 
113
    /* Free temporary buffer */
113
    /* Free temporary buffer */
114
    free(arg_buf);
114
    free(arg_buf);
115
 
115
 
116
    return EOK;
116
    return EOK;
117
}
117
}
118
 
118
 
119
/** Create a new task by running an executable from VFS.
119
/** Create a new task by running an executable from VFS.
120
 *
120
 *
121
 * @param path  pathname of the binary to execute
121
 * @param path  pathname of the binary to execute
122
 * @param argv  command-line arguments
122
 * @param argv  command-line arguments
123
 * @return  ID of the newly created task or zero on error.
123
 * @return  ID of the newly created task or zero on error.
124
 */
124
 */
125
task_id_t task_spawn_ex(const char *path, const char *argv[])
125
task_id_t task_spawn(const char *path, const char *argv[])
126
{
126
{
127
    int phone_id;
127
    int phone_id;
128
    ipc_call_t answer;
128
    ipc_call_t answer;
129
    aid_t req;
129
    aid_t req;
130
    int rc;
130
    int rc;
131
 
131
 
132
    /* Spawn a program loader */   
132
    /* Spawn a program loader */   
133
    phone_id = task_spawn_loader();
133
    phone_id = task_spawn_loader();
134
    if (phone_id < 0) return 0;
134
    if (phone_id < 0) return 0;
135
 
135
 
136
    /*
136
    /*
137
     * Say hello so that the loader knows the incoming connection's
137
     * Say hello so that the loader knows the incoming connection's
138
     * phone hash.
138
     * phone hash.
139
     */
139
     */
140
    rc = async_req_0_0(phone_id, LOADER_HELLO);
140
    rc = async_req_0_0(phone_id, LOADER_HELLO);
141
    if (rc != EOK) return 0;
141
    if (rc != EOK) return 0;
142
 
142
 
143
    /* Send program pathname */
143
    /* Send program pathname */
144
    req = async_send_0(phone_id, LOADER_SET_PATHNAME, &answer);
144
    req = async_send_0(phone_id, LOADER_SET_PATHNAME, &answer);
145
    rc = ipc_data_write_start(phone_id, (void *)path, strlen(path));
145
    rc = ipc_data_write_start(phone_id, (void *)path, strlen(path));
146
    if (rc != EOK) {
146
    if (rc != EOK) {
147
        async_wait_for(req, NULL);
147
        async_wait_for(req, NULL);
148
        return 1;
148
        return 1;
149
    }
149
    }
150
 
150
 
151
    async_wait_for(req, &rc);
151
    async_wait_for(req, &rc);
152
    if (rc != EOK) goto error;
152
    if (rc != EOK) goto error;
153
 
153
 
154
    /* Send arguments */
154
    /* Send arguments */
155
    rc = loader_set_args(phone_id, argv);
155
    rc = loader_set_args(phone_id, argv);
156
    if (rc != EOK) goto error;
156
    if (rc != EOK) goto error;
157
 
157
 
158
    /* Request loader to start the program */  
158
    /* Request loader to start the program */  
159
    rc = async_req_0_0(phone_id, LOADER_RUN);
159
    rc = async_req_0_0(phone_id, LOADER_RUN);
160
    if (rc != EOK) goto error;
160
    if (rc != EOK) goto error;
161
 
161
 
162
    /* Success */
162
    /* Success */
163
    ipc_hangup(phone_id);
163
    ipc_hangup(phone_id);
164
    return 1;
164
    return 1;
165
 
165
 
166
    /* Error exit */
166
    /* Error exit */
167
error:
167
error:
168
    ipc_hangup(phone_id);
168
    ipc_hangup(phone_id);
169
    return 0;
169
    return 0;
170
}
170
}
171
 
-
 
172
int task_spawn(void *image, size_t size)
-
 
173
{
-
 
174
    return __SYSCALL2(SYS_TASK_SPAWN, (sysarg_t) image, (sysarg_t) size);
-
 
175
}
-
 
176
 
171
 
177
/** @}
172
/** @}
178
 */
173
 */
179
 
174