clonable.c

00001 /*
00002  * Copyright (c) 2009 Martin Decky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00033 #include <ipc/ipc.h>
00034 #include <ipc/services.h>
00035 #include <adt/list.h>
00036 #include <bool.h>
00037 #include <errno.h>
00038 #include <assert.h>
00039 #include <stdio.h>
00040 #include <malloc.h>
00041 #include <loader/loader.h>
00042 #include "clonable.h"
00043 #include "ns.h"
00044 
00046 typedef struct {
00047         link_t link;
00048         sysarg_t service;
00049         ipc_call_t call;
00050         ipc_callid_t callid;
00051 } cs_req_t;
00052 
00054 static link_t cs_req;
00055 
00056 int clonable_init(void)
00057 {
00058         list_initialize(&cs_req);
00059         return EOK;
00060 }
00061 
00063 bool service_clonable(int service)
00064 {
00065         return (service == SERVICE_LOAD);
00066 }
00067 
00075 void register_clonable(sysarg_t service, sysarg_t phone, ipc_call_t *call,
00076     ipc_callid_t callid)
00077 {
00078         if (list_empty(&cs_req)) {
00079                 /* There was no pending connection request. */
00080                 printf("%s: Unexpected clonable server.\n", NAME);
00081                 ipc_answer_0(callid, EBUSY);
00082                 return;
00083         }
00084         
00085         cs_req_t *csr = list_get_instance(cs_req.next, cs_req_t, link);
00086         list_remove(&csr->link);
00087         
00088         /* Currently we can only handle a single type of clonable service. */
00089         assert(csr->service == SERVICE_LOAD);
00090         
00091         ipc_answer_0(callid, EOK);
00092         
00093         ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call),
00094             IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE);
00095         
00096         free(csr);
00097         ipc_hangup(phone);
00098 }
00099 
00109 void connect_to_clonable(sysarg_t service, ipc_call_t *call,
00110     ipc_callid_t callid)
00111 {
00112         assert(service == SERVICE_LOAD);
00113         
00114         cs_req_t *csr = malloc(sizeof(cs_req_t));
00115         if (csr == NULL) {
00116                 ipc_answer_0(callid, ENOMEM);
00117                 return;
00118         }
00119         
00120         /* Spawn a loader. */
00121         int rc = loader_spawn("loader");
00122         
00123         if (rc < 0) {
00124                 free(csr);
00125                 ipc_answer_0(callid, rc);
00126                 return;
00127         }
00128         
00129         link_initialize(&csr->link);
00130         csr->service = service;
00131         csr->call = *call;
00132         csr->callid = callid;
00133         
00134         /*
00135          * We can forward the call only after the server we spawned connects
00136          * to us. Meanwhile we might need to service more connection requests.
00137          * Thus we store the call in a queue.
00138          */
00139         list_append(&csr->link, &cs_req);
00140 }
00141 

Generated on Thu Jun 2 07:45:50 2011 for HelenOS/USB by  doxygen 1.4.7