Subversion Repositories HelenOS

Rev

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

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