Changeset bfd1546 in mainline


Ignore:
Timestamp:
2009-02-15T00:01:06Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
08b777e
Parents:
4cac212c
Message:

More elegant way of invoking the loader - as a 'cloneable' service. Task names functionality lost for the time being.

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/proc/program.h

    r4cac212c rbfd1546  
    6060extern void program_ready(program_t *p);
    6161
    62 extern unative_t sys_program_spawn_loader(int *uspace_phone_id,
    63     char *uspace_name, size_t name_len);
     62extern unative_t sys_program_spawn_loader(char *uspace_name, size_t name_len);
    6463
    6564#endif
  • kernel/generic/src/proc/program.c

    r4cac212c rbfd1546  
    191191/** Syscall for creating a new loader instance from userspace.
    192192 *
    193  * Creates a new task from the program loader image, connects a phone
    194  * to it and stores the phone id into the provided buffer.
    195  *
    196  * @param uspace_phone_id       Userspace address where to store the phone id.
     193 * Creates a new task from the program loader image and sets
     194 * the task name.
     195 *
    197196 * @param name                  Name to set on the new task (typically the same
    198197 *                              as the command used to execute it).
     
    200199 * @return 0 on success or an error code from @ref errno.h.
    201200 */
    202 unative_t sys_program_spawn_loader(int *uspace_phone_id, char *uspace_name,
    203     size_t name_len)
     201unative_t sys_program_spawn_loader(char *uspace_name, size_t name_len)
    204202{
    205203        program_t p;
    206         int fake_id;
    207204        int rc;
    208         int phone_id;
    209205        char namebuf[TASK_NAME_BUFLEN];
    210 
    211         fake_id = 0;
    212 
    213         /* Before we even try creating the task, see if we can write the id */
    214         rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id,
    215             sizeof(fake_id));
    216         if (rc != 0)
    217                 return rc;
    218206
    219207        /* Cap length of name and copy it from userspace. */
     
    228216        namebuf[name_len] = '\0';
    229217
    230         /* Allocate the phone for communicating with the new task. */
    231 
    232         phone_id = phone_alloc();
    233         if (phone_id < 0)
    234                 return ELIMIT;
    235 
    236218        /* Spawn the new task. */
    237219
     
    240222                return rc;
    241223
    242         phone_connect(phone_id, &p.task->answerbox);
    243 
    244         /* No need to aquire lock before task_ready() */
    245         rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id,
    246             sizeof(phone_id));
    247         if (rc != 0) {
    248                 /* Ooops */
    249                 ipc_phone_hangup(&TASK->phones[phone_id]);
    250                 task_kill(p.task->taskid);
    251                 return rc;
    252         }
    253 
    254224        // FIXME: control the capabilities
    255225        cap_set(p.task, cap_get(TASK));
  • uspace/lib/libc/generic/loader.c

    r4cac212c rbfd1546  
    3535#include <ipc/ipc.h>
    3636#include <ipc/loader.h>
     37#include <ipc/services.h>
    3738#include <libc.h>
    3839#include <task.h>
     
    5152 *              de-allocated using free() after use).
    5253 */
    53 loader_t *loader_spawn(const char *name)
    54 {
    55         int phone_id, rc;
     54int loader_spawn(const char *name)
     55{
     56        return __SYSCALL2(SYS_PROGRAM_SPAWN_LOADER,
     57            (sysarg_t) name, strlen(name));
     58}
     59
     60loader_t *loader_connect(void)
     61{
    5662        loader_t *ldr;
    57 
    58         /*
    59          * Ask kernel to spawn a new loader task.
    60          */
    61         rc = __SYSCALL3(SYS_PROGRAM_SPAWN_LOADER, (sysarg_t) &phone_id,
    62                 (sysarg_t) name, strlen(name));
    63         if (rc != 0)
    64                 return NULL;
    65 
    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);
    71         if (rc != EOK)
     63        int phone_id;
     64
     65        phone_id = ipc_connect_me_to(PHONE_NS, SERVICE_LOAD, 0, 0);
     66        if (phone_id < 0)
    7267                return NULL;
    7368
     
    7772
    7873        ldr->phone_id = phone_id;
    79         return ldr;
     74        return ldr;     
    8075}
    8176
  • uspace/lib/libc/generic/task.c

    r4cac212c rbfd1546  
    6464        int rc;
    6565
    66         /* Spawn a program loader. */   
    67         ldr = loader_spawn(path);
     66        /* Connect to a program loader. */
     67        ldr = loader_connect();
    6868        if (ldr == NULL)
    6969                return 0;
     
    9090
    9191        /* Run it. */
    92         /* Load the program. */
    9392        rc = loader_run(ldr);
    9493        if (rc != EOK)
  • uspace/lib/libc/include/ipc/services.h

    r4cac212c rbfd1546  
    3939
    4040typedef enum {
    41         SERVICE_PCI = 1,
     41        SERVICE_LOAD = 1,
     42        SERVICE_PCI,
    4243        SERVICE_KEYBOARD,
    4344        SERVICE_VIDEO,
  • uspace/lib/libc/include/loader/loader.h

    r4cac212c rbfd1546  
    4545} loader_t;
    4646
    47 extern loader_t *loader_spawn(const char *name);
     47extern int loader_spawn(const char *);
     48extern loader_t *loader_connect(void);
    4849extern int loader_get_task_id(loader_t *, task_id_t *);
    4950extern int loader_set_pathname(loader_t *, const char *);
  • uspace/srv/loader/main.c

    r4cac212c rbfd1546  
    5151#include <sys/types.h>
    5252#include <ipc/ipc.h>
     53#include <ipc/services.h>
    5354#include <ipc/loader.h>
    5455#include <loader/pcb.h>
     
    8081static bool is_dyn_linked;
    8182
     83/** Used to limit number of connections to one. */
     84static bool connected;
    8285
    8386static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request)
     
    297300        ipc_call_t call;
    298301        int retval;
     302
     303        /* Already have a connection? */
     304        if (connected) {
     305                ipc_answer_0(iid, ELIMIT);
     306                return;
     307        }
     308
     309        connected = true;
     310       
     311        /* Accept the connection */
     312        ipc_answer_0(iid, EOK);
    299313
    300314        /* Ignore parameters, the connection is already open */
     
    339353int main(int argc, char *argv[])
    340354{
    341         ipc_callid_t callid;
    342         ipc_call_t call;
    343         ipcarg_t phone_hash;
    344 
    345         /* The first call only communicates the incoming phone hash */
    346         callid = ipc_wait_for_call(&call);
    347 
    348         if (IPC_GET_METHOD(call) != LOADER_HELLO) {
    349                 if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
    350                         ipc_answer_0(callid, EINVAL);
    351                 return 1;
    352         }
    353 
    354         ipc_answer_0(callid, EOK);
    355         phone_hash = call.in_phone_hash;
    356 
    357         /*
    358          * Up until now async must not be used as it couldn't
    359          * handle incoming requests. (Which means e.g. printf()
    360          * cannot be used)
    361          */
    362         async_new_connection(phone_hash, 0, NULL, loader_connection);
     355        ipcarg_t phonead;
     356
     357        connected = false;
     358       
     359        /* Set a handler of incomming connections. */
     360        async_set_client_connection(loader_connection);
     361
     362        /* Register at naming service. */
     363        if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
     364                return -1;
     365       
    363366        async_manager();
    364367
    365         /* not reached */
     368        /* Never reached */
    366369        return 0;
    367370}
  • uspace/srv/ns/ns.c

    r4cac212c rbfd1546  
    4141#include <ipc/services.h>
    4242#include <stdio.h>
     43#include <bool.h>
    4344#include <unistd.h>
    4445#include <stdlib.h>
     
    4849#include <libadt/hash_table.h>
    4950#include <sysinfo.h>
     51#include <loader/loader.h>
    5052#include <ddi.h>
    5153#include <as.h>
     
    5759static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
    5860static int connect_to_service(ipcarg_t service, ipc_call_t *call,
     61    ipc_callid_t callid);
     62
     63void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
     64    ipc_callid_t callid);
     65void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
    5966    ipc_callid_t callid);
    6067
     
    8491static void *clockaddr = NULL;
    8592static void *klogaddr = NULL;
     93
     94/** Request for connection to a clonable service. */
     95typedef struct {
     96        link_t link;
     97        ipcarg_t service;
     98        ipc_call_t *call;
     99        ipc_callid_t callid;
     100} cs_req_t;
     101
     102/** List of clonable-service connection requests. */
     103static link_t cs_req;
     104
     105/** Return true if @a service is clonable. */
     106static bool service_clonable(int service)
     107{
     108        return service == SERVICE_LOAD;
     109}
    86110
    87111static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name,
     
    117141                return ENOMEM;
    118142        }
     143
     144        list_initialize(&cs_req);
    119145       
    120146        printf(NAME ": Accepting connections\n");
     
    143169                         * Server requests service registration.
    144170                         */
    145                         retval = register_service(IPC_GET_ARG1(call),
    146                             IPC_GET_ARG5(call), &call);
     171                        if (service_clonable(IPC_GET_ARG1(call))) {
     172                                register_clonable(IPC_GET_ARG1(call),
     173                                    IPC_GET_ARG5(call), &call, callid);
     174                                continue;
     175                        } else {
     176                                retval = register_service(IPC_GET_ARG1(call),
     177                                    IPC_GET_ARG5(call), &call);
     178                        }
    147179                        break;
    148180                case IPC_M_CONNECT_ME_TO:
     
    150182                         * Client requests to be connected to a service.
    151183                         */
    152                         retval = connect_to_service(IPC_GET_ARG1(call), &call,
    153                             callid);
     184                        if (service_clonable(IPC_GET_ARG1(call))) {
     185                                connect_to_clonable(IPC_GET_ARG1(call),
     186                                    &call, callid);
     187                                continue;
     188                        } else {
     189                                retval = connect_to_service(IPC_GET_ARG1(call),
     190                                    &call, callid);
     191                        }
    154192                        break;
    155193                default:
     
    168206/** Register service.
    169207 *
    170  * @param service Service to be registered.
    171  * @param phone Phone to be used for connections to the service.
    172  * @param call Pointer to call structure.
    173  *
    174  * @return Zero on success or a value from @ref errno.h.
     208 * @param service       Service to be registered.
     209 * @param phone         Phone to be used for connections to the service.
     210 * @param call          Pointer to call structure.
     211 *
     212 * @return              Zero on success or a value from @ref errno.h.
    175213 */
    176214int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
     
    182220        };
    183221        hashed_service_t *hs;
    184                        
     222
    185223        if (hash_table_find(&ns_hash_table, keys)) {
    186224                return EEXISTS;
     
    203241/** Connect client to service.
    204242 *
    205  * @param service Service to be connected to.
    206  * @param call Pointer to call structure.
    207  * @param callid Call ID of the request.
     243 * @param service       Service to be connected to.
     244 * @param call          Pointer to call structure.
     245 * @param callid        Call ID of the request.
    208246 *
    209247 * @return Zero on success or a value from @ref errno.h.
     
    214252        link_t *hlp;
    215253        hashed_service_t *hs;
    216                        
     254
    217255        hlp = hash_table_find(&ns_hash_table, keys);
    218256        if (!hlp) {
     
    222260        return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
    223261                IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
     262}
     263
     264/** Register clonable service.
     265 *
     266 * @param service       Service to be registered.
     267 * @param phone         Phone to be used for connections to the service.
     268 * @param call          Pointer to call structure.
     269 */
     270void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
     271    ipc_callid_t callid)
     272{
     273        int rc;
     274        cs_req_t *csr;
     275
     276        if (list_empty(&cs_req)) {
     277                /* There was no pending connection request. */
     278                printf(NAME ": Unexpected clonable server.\n");
     279                ipc_answer_0(callid, EBUSY);
     280                return;
     281        }
     282
     283        csr = list_get_instance(cs_req.next, cs_req_t, link);
     284        list_remove(&csr->link);
     285
     286        /* Currently we can only handle a single type of clonable service. */
     287        assert(csr->service == SERVICE_LOAD);
     288
     289        ipc_answer_0(callid, EOK);
     290
     291        rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(*csr->call),
     292                IPC_GET_ARG3(*csr->call), 0, IPC_FF_NONE);
     293
     294        free(csr);
     295}
     296
     297/** Connect client to clonable service.
     298 *
     299 * @param service       Service to be connected to.
     300 * @param call          Pointer to call structure.
     301 * @param callid        Call ID of the request.
     302 *
     303 * @return              Zero on success or a value from @ref errno.h.
     304 */
     305void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
     306    ipc_callid_t callid)
     307{
     308        int rc;
     309        cs_req_t *csr;
     310
     311        assert(service == SERVICE_LOAD);
     312
     313        csr = malloc(sizeof(cs_req_t));
     314        if (csr == NULL) {
     315                ipc_answer_0(callid, ENOMEM);
     316                return;
     317        }
     318
     319        /* Spawn a loader. */
     320        rc = loader_spawn("loader");
     321
     322        if (rc < 0) {
     323                free(csr);
     324                ipc_answer_0(callid, rc);
     325                return;
     326        }
     327
     328        csr->service = service;
     329        csr->call = call;
     330        csr->callid = callid;
     331
     332        /*
     333         * We can forward the call only after the server we spawned connects
     334         * to us. Meanwhile we might need to service more connection requests.
     335         * Thus we store the call in a queue.
     336         */
     337        list_append(&csr->link, &cs_req);
    224338}
    225339
Note: See TracChangeset for help on using the changeset viewer.