Subversion Repositories HelenOS

Rev

Rev 3216 | Rev 3448 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1175 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2006 Jakub Jermar
3148 svoboda 3
 * Copyright (c) 2008 Jiri Svoboda
1175 jermar 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
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
16
 *   derived from this software without specific prior written permission.
17
 *
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
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1653 cejka 28
 */
29
 
1866 jermar 30
/** @addtogroup libc
1653 cejka 31
 * @{
32
 */
33
/** @file
1175 jermar 34
 */
35
 
36
#include <task.h>
3004 svoboda 37
#include <ipc/ipc.h>
3148 svoboda 38
#include <ipc/loader.h>
3102 svoboda 39
#include <libc.h>
3148 svoboda 40
#include <string.h>
3174 svoboda 41
#include <stdlib.h>
3004 svoboda 42
#include <async.h>
43
#include <errno.h>
3403 svoboda 44
#include <vfs/vfs.h>
1175 jermar 45
 
1228 jermar 46
task_id_t task_get_id(void)
1175 jermar 47
{
48
    task_id_t task_id;
49
 
1228 jermar 50
    (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
1175 jermar 51
 
52
    return task_id;
53
}
1653 cejka 54
 
3004 svoboda 55
static int task_spawn_loader(void)
56
{
57
    int phone_id, rc;
58
 
3203 svoboda 59
    rc = __SYSCALL1(SYS_PROGRAM_SPAWN_LOADER, (sysarg_t) &phone_id);
3004 svoboda 60
    if (rc != 0)
61
        return rc;
62
 
63
    return phone_id;
64
}
3148 svoboda 65
 
3174 svoboda 66
static int loader_set_args(int phone_id, const char *argv[])
67
{
68
    aid_t req;
69
    ipc_call_t answer;
3202 svoboda 70
    ipcarg_t rc;
3174 svoboda 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
80
     * compute size of the buffer needed.
81
     */
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 */
93
    ap = argv;
94
    dp = arg_buf;
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
 
3148 svoboda 120
/** Create a new task by running an executable from VFS.
121
 *
122
 * @param path  pathname of the binary to execute
123
 * @param argv  command-line arguments
124
 * @return  ID of the newly created task or zero on error.
125
 */
3190 svoboda 126
task_id_t task_spawn(const char *path, const char *argv[])
3004 svoboda 127
{
128
    int phone_id;
129
    ipc_call_t answer;
130
    aid_t req;
131
    int rc;
3216 svoboda 132
    ipcarg_t retval;
3148 svoboda 133
 
3403 svoboda 134
    char *pa;
135
    size_t pa_len;
136
 
137
    pa = absolutize(path, &pa_len);
138
    if (!pa)
139
        return 0;
140
 
3148 svoboda 141
    /* Spawn a program loader */   
3004 svoboda 142
    phone_id = task_spawn_loader();
3403 svoboda 143
    if (phone_id < 0)
144
        return 0;
3004 svoboda 145
 
3148 svoboda 146
    /*
147
     * Say hello so that the loader knows the incoming connection's
148
     * phone hash.
149
     */
150
    rc = async_req_0_0(phone_id, LOADER_HELLO);
3403 svoboda 151
    if (rc != EOK)
152
        return 0;
3004 svoboda 153
 
3148 svoboda 154
    /* Send program pathname */
155
    req = async_send_0(phone_id, LOADER_SET_PATHNAME, &answer);
3403 svoboda 156
    rc = ipc_data_write_start(phone_id, (void *)pa, pa_len);
3004 svoboda 157
    if (rc != EOK) {
3148 svoboda 158
        async_wait_for(req, NULL);
3004 svoboda 159
        return 1;
160
    }
161
 
3216 svoboda 162
    async_wait_for(req, &retval);
3403 svoboda 163
    if (retval != EOK)
164
        goto error;
3004 svoboda 165
 
3174 svoboda 166
    /* Send arguments */
167
    rc = loader_set_args(phone_id, argv);
3403 svoboda 168
    if (rc != EOK)
169
        goto error;
3174 svoboda 170
 
3148 svoboda 171
    /* Request loader to start the program */  
172
    rc = async_req_0_0(phone_id, LOADER_RUN);
3403 svoboda 173
    if (rc != EOK)
174
        goto error;
3004 svoboda 175
 
3174 svoboda 176
    /* Success */
3148 svoboda 177
    ipc_hangup(phone_id);
3174 svoboda 178
    return 1;
3148 svoboda 179
 
3174 svoboda 180
    /* Error exit */
181
error:
182
    ipc_hangup(phone_id);
183
    return 0;
3004 svoboda 184
}
185
 
1866 jermar 186
/** @}
1653 cejka 187
 */