Subversion Repositories HelenOS

Rev

Rev 3448 | Rev 3569 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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