Subversion Repositories HelenOS

Rev

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

Rev 3492 Rev 3593
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup libc
29
/** @addtogroup libc
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <ipc/ipc.h>
35
#include <ipc/ipc.h>
36
#include <ipc/loader.h>
36
#include <ipc/loader.h>
37
#include <libc.h>
37
#include <libc.h>
38
#include <task.h>
38
#include <task.h>
39
#include <string.h>
39
#include <string.h>
40
#include <stdlib.h>
40
#include <stdlib.h>
41
#include <async.h>
41
#include <async.h>
42
#include <errno.h>
42
#include <errno.h>
43
#include <vfs/vfs.h>
43
#include <vfs/vfs.h>
44
#include <loader/loader.h>
44
#include <loader/loader.h>
45
 
45
 
46
/** Connect to a new program loader.
46
/** Connect to a new program loader.
47
 *
47
 *
48
 * Spawns a new program loader task and returns the connection structure.
48
 * Spawns a new program loader task and returns the connection structure.
-
 
49
 * @param name  Symbolic name to set on the newly created task.
49
 * @return  Pointer to the loader connection structure (should be
50
 * @return  Pointer to the loader connection structure (should be
50
 *      de-allocated using free() after use).
51
 *      de-allocated using free() after use).
51
 */
52
 */
52
loader_t *loader_spawn(void)
53
loader_t *loader_spawn(char *name)
53
{
54
{
54
    int phone_id, rc;
55
    int phone_id, rc;
55
    loader_t *ldr;
56
    loader_t *ldr;
56
 
57
 
57
    /*
58
    /*
58
     * Ask kernel to spawn a new loader task.
59
     * Ask kernel to spawn a new loader task.
59
     */
60
     */
60
    rc = __SYSCALL1(SYS_PROGRAM_SPAWN_LOADER, (sysarg_t) &phone_id);
61
    rc = __SYSCALL3(SYS_PROGRAM_SPAWN_LOADER, (sysarg_t) &phone_id,
-
 
62
        (sysarg_t) name, strlen(name));
61
    if (rc != 0)
63
    if (rc != 0)
62
        return NULL;
64
        return NULL;
63
 
65
 
64
    /*
66
    /*
65
     * Say hello so that the loader knows the incoming connection's
67
     * Say hello so that the loader knows the incoming connection's
66
     * phone hash.
68
     * phone hash.
67
     */
69
     */
68
    rc = async_req_0_0(phone_id, LOADER_HELLO);
70
    rc = async_req_0_0(phone_id, LOADER_HELLO);
69
    if (rc != EOK)
71
    if (rc != EOK)
70
        return NULL;
72
        return NULL;
71
 
73
 
72
    ldr = malloc(sizeof(loader_t));
74
    ldr = malloc(sizeof(loader_t));
73
    if (ldr == NULL)
75
    if (ldr == NULL)
74
        return NULL;
76
        return NULL;
75
 
77
 
76
    ldr->phone_id = phone_id;
78
    ldr->phone_id = phone_id;
77
    return ldr;
79
    return ldr;
78
}
80
}
79
 
81
 
80
/** Get ID of the new task.
82
/** Get ID of the new task.
81
 *
83
 *
82
 * Retrieves the ID of the new task from the loader.
84
 * Retrieves the ID of the new task from the loader.
83
 *
85
 *
84
 * @param ldr       Loader connection structure.
86
 * @param ldr       Loader connection structure.
85
 * @param task_id   Points to a variable where the ID should be stored.
87
 * @param task_id   Points to a variable where the ID should be stored.
86
 * @return      Zero on success or negative error code.
88
 * @return      Zero on success or negative error code.
87
 */
89
 */
88
int loader_get_task_id(loader_t *ldr, task_id_t *task_id)
90
int loader_get_task_id(loader_t *ldr, task_id_t *task_id)
89
{
91
{
90
    ipc_call_t answer;
92
    ipc_call_t answer;
91
    aid_t req;
93
    aid_t req;
92
    int rc;
94
    int rc;
93
    ipcarg_t retval;
95
    ipcarg_t retval;
94
 
96
 
95
    /* Get task ID. */
97
    /* Get task ID. */
96
    req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer);
98
    req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer);
97
    rc = ipc_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t));
99
    rc = ipc_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t));
98
    if (rc != EOK) {
100
    if (rc != EOK) {
99
        async_wait_for(req, NULL);
101
        async_wait_for(req, NULL);
100
        return rc;
102
        return rc;
101
    }
103
    }
102
 
104
 
103
    async_wait_for(req, &retval);
105
    async_wait_for(req, &retval);
104
    return (int)retval;
106
    return (int)retval;
105
}
107
}
106
 
108
 
107
/** Set pathname of the program to load.
109
/** Set pathname of the program to load.
108
 *
110
 *
109
 * Sets the name of the program file to load. The name can be relative
111
 * Sets the name of the program file to load. The name can be relative
110
 * to the current working directory (it will be absolutized before
112
 * to the current working directory (it will be absolutized before
111
 * sending to the loader).
113
 * sending to the loader).
112
 *
114
 *
113
 * @param ldr       Loader connection structure.
115
 * @param ldr       Loader connection structure.
114
 * @param path      Pathname of the program file.
116
 * @param path      Pathname of the program file.
115
 * @return      Zero on success or negative error code.
117
 * @return      Zero on success or negative error code.
116
 */
118
 */
117
int loader_set_pathname(loader_t *ldr, const char *path)
119
int loader_set_pathname(loader_t *ldr, const char *path)
118
{
120
{
119
    ipc_call_t answer;
121
    ipc_call_t answer;
120
    aid_t req;
122
    aid_t req;
121
    int rc;
123
    int rc;
122
    ipcarg_t retval;
124
    ipcarg_t retval;
123
 
125
 
124
    char *pa;
126
    char *pa;
125
    size_t pa_len;
127
    size_t pa_len;
126
 
128
 
127
    pa = absolutize(path, &pa_len);
129
    pa = absolutize(path, &pa_len);
128
    if (!pa)
130
    if (!pa)
129
        return 0;
131
        return 0;
130
 
132
 
131
    /* Send program pathname */
133
    /* Send program pathname */
132
    req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer);
134
    req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer);
133
    rc = ipc_data_write_start(ldr->phone_id, (void *)pa, pa_len);
135
    rc = ipc_data_write_start(ldr->phone_id, (void *)pa, pa_len);
134
    if (rc != EOK) {
136
    if (rc != EOK) {
135
        async_wait_for(req, NULL);
137
        async_wait_for(req, NULL);
136
        return rc;
138
        return rc;
137
    }
139
    }
138
 
140
 
139
    free(pa);
141
    free(pa);
140
 
142
 
141
    async_wait_for(req, &retval);
143
    async_wait_for(req, &retval);
142
    return (int)retval;
144
    return (int)retval;
143
}
145
}
144
 
146
 
145
 
147
 
146
/** Set command-line arguments for the program.
148
/** Set command-line arguments for the program.
147
 *
149
 *
148
 * Sets the vector of command-line arguments to be passed to the loaded
150
 * Sets the vector of command-line arguments to be passed to the loaded
149
 * program. By convention, the very first argument is typically the same as
151
 * program. By convention, the very first argument is typically the same as
150
 * the command used to execute the program.
152
 * the command used to execute the program.
151
 *
153
 *
152
 * @param ldr       Loader connection structure.
154
 * @param ldr       Loader connection structure.
153
 * @param argv      NULL-terminated array of pointers to arguments.
155
 * @param argv      NULL-terminated array of pointers to arguments.
154
 * @return      Zero on success or negative error code.
156
 * @return      Zero on success or negative error code.
155
 */
157
 */
156
int loader_set_args(loader_t *ldr, char *const argv[])
158
int loader_set_args(loader_t *ldr, char *const argv[])
157
{
159
{
158
    aid_t req;
160
    aid_t req;
159
    ipc_call_t answer;
161
    ipc_call_t answer;
160
    ipcarg_t rc;
162
    ipcarg_t rc;
161
 
163
 
162
    char *const *ap;
164
    char *const *ap;
163
    char *dp;
165
    char *dp;
164
    char *arg_buf;
166
    char *arg_buf;
165
    size_t buffer_size;
167
    size_t buffer_size;
166
 
168
 
167
    /*
169
    /*
168
     * Serialize the arguments into a single array. First
170
     * Serialize the arguments into a single array. First
169
     * compute size of the buffer needed.
171
     * compute size of the buffer needed.
170
     */
172
     */
171
    ap = argv;
173
    ap = argv;
172
    buffer_size = 0;
174
    buffer_size = 0;
173
    while (*ap != NULL) {
175
    while (*ap != NULL) {
174
        buffer_size += strlen(*ap) + 1;
176
        buffer_size += strlen(*ap) + 1;
175
        ++ap;
177
        ++ap;
176
    }
178
    }
177
 
179
 
178
    arg_buf = malloc(buffer_size);
180
    arg_buf = malloc(buffer_size);
179
    if (arg_buf == NULL) return ENOMEM;
181
    if (arg_buf == NULL) return ENOMEM;
180
 
182
 
181
    /* Now fill the buffer with null-terminated argument strings */
183
    /* Now fill the buffer with null-terminated argument strings */
182
    ap = argv;
184
    ap = argv;
183
    dp = arg_buf;
185
    dp = arg_buf;
184
    while (*ap != NULL) {
186
    while (*ap != NULL) {
185
        strcpy(dp, *ap);
187
        strcpy(dp, *ap);
186
        dp += strlen(*ap) + 1;
188
        dp += strlen(*ap) + 1;
187
 
189
 
188
        ++ap;
190
        ++ap;
189
    }
191
    }
190
 
192
 
191
    /* Send serialized arguments to the loader */
193
    /* Send serialized arguments to the loader */
192
 
194
 
193
    req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer);
195
    req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer);
194
    rc = ipc_data_write_start(ldr->phone_id, (void *)arg_buf, buffer_size);
196
    rc = ipc_data_write_start(ldr->phone_id, (void *)arg_buf, buffer_size);
195
    if (rc != EOK) {
197
    if (rc != EOK) {
196
        async_wait_for(req, NULL);
198
        async_wait_for(req, NULL);
197
        return rc;
199
        return rc;
198
    }
200
    }
199
 
201
 
200
    async_wait_for(req, &rc);
202
    async_wait_for(req, &rc);
201
    if (rc != EOK) return rc;
203
    if (rc != EOK) return rc;
202
 
204
 
203
    /* Free temporary buffer */
205
    /* Free temporary buffer */
204
    free(arg_buf);
206
    free(arg_buf);
205
 
207
 
206
    return EOK;
208
    return EOK;
207
}
209
}
208
 
210
 
209
/** Instruct loader to load the program.
211
/** Instruct loader to load the program.
210
 *
212
 *
211
 * If this function succeeds, the program has been successfully loaded
213
 * If this function succeeds, the program has been successfully loaded
212
 * and is ready to be executed.
214
 * and is ready to be executed.
213
 *
215
 *
214
 * @param ldr       Loader connection structure.
216
 * @param ldr       Loader connection structure.
215
 * @return      Zero on success or negative error code.
217
 * @return      Zero on success or negative error code.
216
 */
218
 */
217
int loader_load_program(loader_t *ldr)
219
int loader_load_program(loader_t *ldr)
218
{
220
{
219
    int rc;
221
    int rc;
220
 
222
 
221
    rc = async_req_0_0(ldr->phone_id, LOADER_LOAD);
223
    rc = async_req_0_0(ldr->phone_id, LOADER_LOAD);
222
    if (rc != EOK)
224
    if (rc != EOK)
223
        return rc;
225
        return rc;
224
 
226
 
225
    return EOK;
227
    return EOK;
226
}
228
}
227
 
229
 
228
/** Instruct loader to execute the program.
230
/** Instruct loader to execute the program.
229
 *
231
 *
230
 * Note that this function blocks until the loader actually replies
232
 * Note that this function blocks until the loader actually replies
231
 * so you cannot expect this function to return if you are debugging
233
 * so you cannot expect this function to return if you are debugging
232
 * the task and its thread is stopped.
234
 * the task and its thread is stopped.
233
 *
235
 *
234
 * After using this function, no further operations must be performed
236
 * After using this function, no further operations must be performed
235
 * on the loader structure. It should be de-allocated using free().
237
 * on the loader structure. It should be de-allocated using free().
236
 *
238
 *
237
 * @param ldr       Loader connection structure.
239
 * @param ldr       Loader connection structure.
238
 * @return      Zero on success or negative error code.
240
 * @return      Zero on success or negative error code.
239
 */
241
 */
240
int loader_run(loader_t *ldr)
242
int loader_run(loader_t *ldr)
241
{
243
{
242
    int rc;
244
    int rc;
243
 
245
 
244
    rc = async_req_0_0(ldr->phone_id, LOADER_RUN);
246
    rc = async_req_0_0(ldr->phone_id, LOADER_RUN);
245
    if (rc != EOK)
247
    if (rc != EOK)
246
        return rc;
248
        return rc;
247
 
249
 
248
    return EOK;
250
    return EOK;
249
}
251
}
250
 
252
 
251
/** Cancel the loader session.
253
/** Cancel the loader session.
252
 *
254
 *
253
 * Tells the loader not to load any program and terminate.
255
 * Tells the loader not to load any program and terminate.
254
 * After using this function, no further operations must be performed
256
 * After using this function, no further operations must be performed
255
 * on the loader structure. It should be de-allocated using free().
257
 * on the loader structure. It should be de-allocated using free().
256
 *
258
 *
257
 * @param ldr       Loader connection structure.
259
 * @param ldr       Loader connection structure.
258
 * @return      Zero on success or negative error code.
260
 * @return      Zero on success or negative error code.
259
 */
261
 */
260
void loader_abort(loader_t *ldr)
262
void loader_abort(loader_t *ldr)
261
{
263
{
262
    ipc_hangup(ldr->phone_id);
264
    ipc_hangup(ldr->phone_id);
263
    ldr->phone_id = 0;
265
    ldr->phone_id = 0;
264
}
266
}
265
 
267
 
266
/** @}
268
/** @}
267
 */
269
 */
268
 
270