Subversion Repositories HelenOS

Rev

Rev 3148 | Rev 3174 | 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>
3004 svoboda 41
#include <async.h>
42
#include <errno.h>
1175 jermar 43
 
1228 jermar 44
task_id_t task_get_id(void)
1175 jermar 45
{
46
    task_id_t task_id;
47
 
1228 jermar 48
    (void) __SYSCALL1(SYS_TASK_GET_ID, (sysarg_t) &task_id);
1175 jermar 49
 
50
    return task_id;
51
}
1653 cejka 52
 
3004 svoboda 53
static int task_spawn_loader(void)
54
{
55
    int phone_id, rc;
56
 
3150 svoboda 57
    rc = __SYSCALL1(SYS_TASK_SPAWN_LOADER, (sysarg_t) &phone_id);
3004 svoboda 58
    if (rc != 0)
59
        return rc;
60
 
61
    return phone_id;
62
}
3148 svoboda 63
 
64
/** Create a new task by running an executable from VFS.
65
 *
66
 * @param path  pathname of the binary to execute
67
 * @param argv  command-line arguments
68
 * @return  ID of the newly created task or zero on error.
69
 */
3150 svoboda 70
task_id_t task_spawn_ex(const char *path, const char *argv[])
3004 svoboda 71
{
72
    int phone_id;
73
    ipc_call_t answer;
74
    aid_t req;
75
    int rc;
3148 svoboda 76
 
77
    /* Spawn a program loader */   
3004 svoboda 78
    phone_id = task_spawn_loader();
79
    if (phone_id < 0) return 0;
80
 
3148 svoboda 81
    /*
82
     * Say hello so that the loader knows the incoming connection's
83
     * phone hash.
84
     */
85
    rc = async_req_0_0(phone_id, LOADER_HELLO);
86
    if (rc != EOK) return 0;
3004 svoboda 87
 
3148 svoboda 88
    /* Send program pathname */
89
    req = async_send_0(phone_id, LOADER_SET_PATHNAME, &answer);
3004 svoboda 90
    rc = ipc_data_write_start(phone_id, (void *)path, strlen(path));
91
    if (rc != EOK) {
3148 svoboda 92
        async_wait_for(req, NULL);
3004 svoboda 93
        return 1;
94
    }
95
 
3148 svoboda 96
    async_wait_for(req, &rc);
3004 svoboda 97
    if (rc != EOK) return 0;
98
 
3148 svoboda 99
    /* Request loader to start the program */  
100
    rc = async_req_0_0(phone_id, LOADER_RUN);
101
    if (rc != EOK) return 0;
3004 svoboda 102
 
3148 svoboda 103
    ipc_hangup(phone_id);
104
 
3004 svoboda 105
    return 1;
106
}
107
 
3150 svoboda 108
int task_spawn(void *image, size_t size)
109
{
110
    return __SYSCALL2(SYS_TASK_SPAWN, (sysarg_t) image, (sysarg_t) size);
111
}
112
 
1866 jermar 113
/** @}
1653 cejka 114
 */