Subversion Repositories HelenOS

Rev

Rev 3593 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3593 Rev 3745
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
 * @param name  Symbolic name to set on the newly created task.
50
 * @return  Pointer to the loader connection structure (should be
50
 * @return  Pointer to the loader connection structure (should be
51
 *      de-allocated using free() after use).
51
 *      de-allocated using free() after use).
52
 */
52
 */
53
loader_t *loader_spawn(char *name)
53
loader_t *loader_spawn(const char *name)
54
{
54
{
55
    int phone_id, rc;
55
    int phone_id, rc;
56
    loader_t *ldr;
56
    loader_t *ldr;
57
 
57
 
58
    /*
58
    /*
59
     * Ask kernel to spawn a new loader task.
59
     * Ask kernel to spawn a new loader task.
60
     */
60
     */
61
    rc = __SYSCALL3(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));
62
        (sysarg_t) name, strlen(name));
63
    if (rc != 0)
63
    if (rc != 0)
64
        return NULL;
64
        return NULL;
65
 
65
 
66
    /*
66
    /*
67
     * Say hello so that the loader knows the incoming connection's
67
     * Say hello so that the loader knows the incoming connection's
68
     * phone hash.
68
     * phone hash.
69
     */
69
     */
70
    rc = async_req_0_0(phone_id, LOADER_HELLO);
70
    rc = async_req_0_0(phone_id, LOADER_HELLO);
71
    if (rc != EOK)
71
    if (rc != EOK)
72
        return NULL;
72
        return NULL;
73
 
73
 
74
    ldr = malloc(sizeof(loader_t));
74
    ldr = malloc(sizeof(loader_t));
75
    if (ldr == NULL)
75
    if (ldr == NULL)
76
        return NULL;
76
        return NULL;
77
 
77
 
78
    ldr->phone_id = phone_id;
78
    ldr->phone_id = phone_id;
79
    return ldr;
79
    return ldr;
80
}
80
}
81
 
81
 
82
/** Get ID of the new task.
82
/** Get ID of the new task.
83
 *
83
 *
84
 * Retrieves the ID of the new task from the loader.
84
 * Retrieves the ID of the new task from the loader.
85
 *
85
 *
86
 * @param ldr       Loader connection structure.
86
 * @param ldr       Loader connection structure.
87
 * @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.
88
 * @return      Zero on success or negative error code.
88
 * @return      Zero on success or negative error code.
89
 */
89
 */
90
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)
91
{
91
{
92
    ipc_call_t answer;
92
    ipc_call_t answer;
93
    aid_t req;
93
    aid_t req;
94
    int rc;
94
    int rc;
95
    ipcarg_t retval;
95
    ipcarg_t retval;
96
 
96
 
97
    /* Get task ID. */
97
    /* Get task ID. */
98
    req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer);
98
    req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer);
99
    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));
100
    if (rc != EOK) {
100
    if (rc != EOK) {
101
        async_wait_for(req, NULL);
101
        async_wait_for(req, NULL);
102
        return rc;
102
        return rc;
103
    }
103
    }
104
 
104
 
105
    async_wait_for(req, &retval);
105
    async_wait_for(req, &retval);
106
    return (int)retval;
106
    return (int)retval;
107
}
107
}
108
 
108
 
109
/** Set pathname of the program to load.
109
/** Set pathname of the program to load.
110
 *
110
 *
111
 * 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
112
 * to the current working directory (it will be absolutized before
112
 * to the current working directory (it will be absolutized before
113
 * sending to the loader).
113
 * sending to the loader).
114
 *
114
 *
115
 * @param ldr       Loader connection structure.
115
 * @param ldr       Loader connection structure.
116
 * @param path      Pathname of the program file.
116
 * @param path      Pathname of the program file.
117
 * @return      Zero on success or negative error code.
117
 * @return      Zero on success or negative error code.
118
 */
118
 */
119
int loader_set_pathname(loader_t *ldr, const char *path)
119
int loader_set_pathname(loader_t *ldr, const char *path)
120
{
120
{
121
    ipc_call_t answer;
121
    ipc_call_t answer;
122
    aid_t req;
122
    aid_t req;
123
    int rc;
123
    int rc;
124
    ipcarg_t retval;
124
    ipcarg_t retval;
125
 
125
 
126
    char *pa;
126
    char *pa;
127
    size_t pa_len;
127
    size_t pa_len;
128
 
128
 
129
    pa = absolutize(path, &pa_len);
129
    pa = absolutize(path, &pa_len);
130
    if (!pa)
130
    if (!pa)
131
        return 0;
131
        return 0;
132
 
132
 
133
    /* Send program pathname */
133
    /* Send program pathname */
134
    req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer);
134
    req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer);
135
    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);
136
    if (rc != EOK) {
136
    if (rc != EOK) {
137
        async_wait_for(req, NULL);
137
        async_wait_for(req, NULL);
138
        return rc;
138
        return rc;
139
    }
139
    }
140
 
140
 
141
    free(pa);
141
    free(pa);
142
 
142
 
143
    async_wait_for(req, &retval);
143
    async_wait_for(req, &retval);
144
    return (int)retval;
144
    return (int)retval;
145
}
145
}
146
 
146
 
147
 
147
 
148
/** Set command-line arguments for the program.
148
/** Set command-line arguments for the program.
149
 *
149
 *
150
 * 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
151
 * 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
152
 * the command used to execute the program.
152
 * the command used to execute the program.
153
 *
153
 *
154
 * @param ldr       Loader connection structure.
154
 * @param ldr       Loader connection structure.
155
 * @param argv      NULL-terminated array of pointers to arguments.
155
 * @param argv      NULL-terminated array of pointers to arguments.
156
 * @return      Zero on success or negative error code.
156
 * @return      Zero on success or negative error code.
157
 */
157
 */
158
int loader_set_args(loader_t *ldr, char *const argv[])
158
int loader_set_args(loader_t *ldr, char *const argv[])
159
{
159
{
160
    aid_t req;
160
    aid_t req;
161
    ipc_call_t answer;
161
    ipc_call_t answer;
162
    ipcarg_t rc;
162
    ipcarg_t rc;
163
 
163
 
164
    char *const *ap;
164
    char *const *ap;
165
    char *dp;
165
    char *dp;
166
    char *arg_buf;
166
    char *arg_buf;
167
    size_t buffer_size;
167
    size_t buffer_size;
168
 
168
 
169
    /*
169
    /*
170
     * Serialize the arguments into a single array. First
170
     * Serialize the arguments into a single array. First
171
     * compute size of the buffer needed.
171
     * compute size of the buffer needed.
172
     */
172
     */
173
    ap = argv;
173
    ap = argv;
174
    buffer_size = 0;
174
    buffer_size = 0;
175
    while (*ap != NULL) {
175
    while (*ap != NULL) {
176
        buffer_size += strlen(*ap) + 1;
176
        buffer_size += strlen(*ap) + 1;
177
        ++ap;
177
        ++ap;
178
    }
178
    }
179
 
179
 
180
    arg_buf = malloc(buffer_size);
180
    arg_buf = malloc(buffer_size);
181
    if (arg_buf == NULL) return ENOMEM;
181
    if (arg_buf == NULL) return ENOMEM;
182
 
182
 
183
    /* Now fill the buffer with null-terminated argument strings */
183
    /* Now fill the buffer with null-terminated argument strings */
184
    ap = argv;
184
    ap = argv;
185
    dp = arg_buf;
185
    dp = arg_buf;
186
    while (*ap != NULL) {
186
    while (*ap != NULL) {
187
        strcpy(dp, *ap);
187
        strcpy(dp, *ap);
188
        dp += strlen(*ap) + 1;
188
        dp += strlen(*ap) + 1;
189
 
189
 
190
        ++ap;
190
        ++ap;
191
    }
191
    }
192
 
192
 
193
    /* Send serialized arguments to the loader */
193
    /* Send serialized arguments to the loader */
194
 
194
 
195
    req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer);
195
    req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer);
196
    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);
197
    if (rc != EOK) {
197
    if (rc != EOK) {
198
        async_wait_for(req, NULL);
198
        async_wait_for(req, NULL);
199
        return rc;
199
        return rc;
200
    }
200
    }
201
 
201
 
202
    async_wait_for(req, &rc);
202
    async_wait_for(req, &rc);
203
    if (rc != EOK) return rc;
203
    if (rc != EOK) return rc;
204
 
204
 
205
    /* Free temporary buffer */
205
    /* Free temporary buffer */
206
    free(arg_buf);
206
    free(arg_buf);
207
 
207
 
208
    return EOK;
208
    return EOK;
209
}
209
}
210
 
210
 
211
/** Instruct loader to load the program.
211
/** Instruct loader to load the program.
212
 *
212
 *
213
 * If this function succeeds, the program has been successfully loaded
213
 * If this function succeeds, the program has been successfully loaded
214
 * and is ready to be executed.
214
 * and is ready to be executed.
215
 *
215
 *
216
 * @param ldr       Loader connection structure.
216
 * @param ldr       Loader connection structure.
217
 * @return      Zero on success or negative error code.
217
 * @return      Zero on success or negative error code.
218
 */
218
 */
219
int loader_load_program(loader_t *ldr)
219
int loader_load_program(loader_t *ldr)
220
{
220
{
221
    int rc;
221
    int rc;
222
 
222
 
223
    rc = async_req_0_0(ldr->phone_id, LOADER_LOAD);
223
    rc = async_req_0_0(ldr->phone_id, LOADER_LOAD);
224
    if (rc != EOK)
224
    if (rc != EOK)
225
        return rc;
225
        return rc;
226
 
226
 
227
    return EOK;
227
    return EOK;
228
}
228
}
229
 
229
 
230
/** Instruct loader to execute the program.
230
/** Instruct loader to execute the program.
231
 *
231
 *
232
 * Note that this function blocks until the loader actually replies
232
 * Note that this function blocks until the loader actually replies
233
 * 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
234
 * the task and its thread is stopped.
234
 * the task and its thread is stopped.
235
 *
235
 *
236
 * After using this function, no further operations must be performed
236
 * After using this function, no further operations must be performed
237
 * on the loader structure. It should be de-allocated using free().
237
 * on the loader structure. It should be de-allocated using free().
238
 *
238
 *
239
 * @param ldr       Loader connection structure.
239
 * @param ldr       Loader connection structure.
240
 * @return      Zero on success or negative error code.
240
 * @return      Zero on success or negative error code.
241
 */
241
 */
242
int loader_run(loader_t *ldr)
242
int loader_run(loader_t *ldr)
243
{
243
{
244
    int rc;
244
    int rc;
245
 
245
 
246
    rc = async_req_0_0(ldr->phone_id, LOADER_RUN);
246
    rc = async_req_0_0(ldr->phone_id, LOADER_RUN);
247
    if (rc != EOK)
247
    if (rc != EOK)
248
        return rc;
248
        return rc;
249
 
249
 
250
    return EOK;
250
    return EOK;
251
}
251
}
252
 
252
 
253
/** Cancel the loader session.
253
/** Cancel the loader session.
254
 *
254
 *
255
 * Tells the loader not to load any program and terminate.
255
 * Tells the loader not to load any program and terminate.
256
 * After using this function, no further operations must be performed
256
 * After using this function, no further operations must be performed
257
 * on the loader structure. It should be de-allocated using free().
257
 * on the loader structure. It should be de-allocated using free().
258
 *
258
 *
259
 * @param ldr       Loader connection structure.
259
 * @param ldr       Loader connection structure.
260
 * @return      Zero on success or negative error code.
260
 * @return      Zero on success or negative error code.
261
 */
261
 */
262
void loader_abort(loader_t *ldr)
262
void loader_abort(loader_t *ldr)
263
{
263
{
264
    ipc_hangup(ldr->phone_id);
264
    ipc_hangup(ldr->phone_id);
265
    ldr->phone_id = 0;
265
    ldr->phone_id = 0;
266
}
266
}
267
 
267
 
268
/** @}
268
/** @}
269
 */
269
 */
270
 
270