Subversion Repositories HelenOS

Rev

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

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