[40313e4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup ns
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[7b616e2] | 33 | #include <async.h>
|
---|
[40313e4] | 34 | #include <ipc/services.h>
|
---|
[d9c8c81] | 35 | #include <adt/list.h>
|
---|
[3e6a98c5] | 36 | #include <stdbool.h>
|
---|
[40313e4] | 37 | #include <errno.h>
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <stdio.h>
|
---|
[38d150e] | 40 | #include <stdlib.h>
|
---|
[3bf907a] | 41 | #include <loader/loader.h>
|
---|
[40313e4] | 42 | #include "clonable.h"
|
---|
| 43 | #include "ns.h"
|
---|
| 44 |
|
---|
| 45 | /** Request for connection to a clonable service. */
|
---|
| 46 | typedef struct {
|
---|
| 47 | link_t link;
|
---|
[9cfbf2f] | 48 | service_t service;
|
---|
| 49 | iface_t iface;
|
---|
[3be9d10] | 50 | cap_call_handle_t callid;
|
---|
[9cfbf2f] | 51 | sysarg_t arg3;
|
---|
[40313e4] | 52 | } cs_req_t;
|
---|
| 53 |
|
---|
| 54 | /** List of clonable-service connection requests. */
|
---|
[b72efe8] | 55 | static list_t cs_req;
|
---|
[40313e4] | 56 |
|
---|
[b7fd2a0] | 57 | errno_t clonable_init(void)
|
---|
[40313e4] | 58 | {
|
---|
| 59 | list_initialize(&cs_req);
|
---|
| 60 | return EOK;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /** Return true if @a service is clonable. */
|
---|
[9cfbf2f] | 64 | bool service_clonable(service_t service)
|
---|
[40313e4] | 65 | {
|
---|
[566992e1] | 66 | return (service == SERVICE_LOADER);
|
---|
[40313e4] | 67 | }
|
---|
| 68 |
|
---|
| 69 | /** Register clonable service.
|
---|
| 70 | *
|
---|
| 71 | * @param service Service to be registered.
|
---|
| 72 | * @param phone Phone to be used for connections to the service.
|
---|
| 73 | * @param call Pointer to call structure.
|
---|
| 74 | *
|
---|
| 75 | */
|
---|
[9cfbf2f] | 76 | void register_clonable(service_t service, sysarg_t phone, ipc_call_t *call,
|
---|
[3be9d10] | 77 | cap_call_handle_t callid)
|
---|
[40313e4] | 78 | {
|
---|
[9cfbf2f] | 79 | link_t *req_link = list_first(&cs_req);
|
---|
[b72efe8] | 80 | if (req_link == NULL) {
|
---|
[40313e4] | 81 | /* There was no pending connection request. */
|
---|
[c7bbf029] | 82 | printf("%s: Unexpected clonable server.\n", NAME);
|
---|
[7b616e2] | 83 | async_answer_0(callid, EBUSY);
|
---|
[40313e4] | 84 | return;
|
---|
| 85 | }
|
---|
[a35b458] | 86 |
|
---|
[b72efe8] | 87 | cs_req_t *csr = list_get_instance(req_link, cs_req_t, link);
|
---|
| 88 | list_remove(req_link);
|
---|
[a35b458] | 89 |
|
---|
[40313e4] | 90 | /* Currently we can only handle a single type of clonable service. */
|
---|
[566992e1] | 91 | assert(csr->service == SERVICE_LOADER);
|
---|
[a35b458] | 92 |
|
---|
[7b616e2] | 93 | async_answer_0(callid, EOK);
|
---|
[a35b458] | 94 |
|
---|
[7b616e2] | 95 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 96 | if (sess == NULL)
|
---|
| 97 | async_answer_0(callid, EIO);
|
---|
[a35b458] | 98 |
|
---|
[7b616e2] | 99 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 100 | async_forward_fast(csr->callid, exch, csr->iface, csr->arg3, 0,
|
---|
[9cfbf2f] | 101 | IPC_FF_NONE);
|
---|
[7b616e2] | 102 | async_exchange_end(exch);
|
---|
[a35b458] | 103 |
|
---|
[40313e4] | 104 | free(csr);
|
---|
[7b616e2] | 105 | async_hangup(sess);
|
---|
[40313e4] | 106 | }
|
---|
| 107 |
|
---|
| 108 | /** Connect client to clonable service.
|
---|
| 109 | *
|
---|
| 110 | * @param service Service to be connected to.
|
---|
[9cfbf2f] | 111 | * @param iface Interface to be connected to.
|
---|
[40313e4] | 112 | * @param call Pointer to call structure.
|
---|
| 113 | * @param callid Call ID of the request.
|
---|
| 114 | *
|
---|
| 115 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 116 | *
|
---|
| 117 | */
|
---|
[9cfbf2f] | 118 | void connect_to_clonable(service_t service, iface_t iface, ipc_call_t *call,
|
---|
[3be9d10] | 119 | cap_call_handle_t callid)
|
---|
[40313e4] | 120 | {
|
---|
[566992e1] | 121 | assert(service == SERVICE_LOADER);
|
---|
[a35b458] | 122 |
|
---|
[40313e4] | 123 | cs_req_t *csr = malloc(sizeof(cs_req_t));
|
---|
| 124 | if (csr == NULL) {
|
---|
[7b616e2] | 125 | async_answer_0(callid, ENOMEM);
|
---|
[40313e4] | 126 | return;
|
---|
| 127 | }
|
---|
[a35b458] | 128 |
|
---|
[40313e4] | 129 | /* Spawn a loader. */
|
---|
[b7fd2a0] | 130 | errno_t rc = loader_spawn("loader");
|
---|
[a35b458] | 131 |
|
---|
[d5c1051] | 132 | if (rc != EOK) {
|
---|
[40313e4] | 133 | free(csr);
|
---|
[7b616e2] | 134 | async_answer_0(callid, rc);
|
---|
[40313e4] | 135 | return;
|
---|
| 136 | }
|
---|
[a35b458] | 137 |
|
---|
[007e6efa] | 138 | link_initialize(&csr->link);
|
---|
[40313e4] | 139 | csr->service = service;
|
---|
[9cfbf2f] | 140 | csr->iface = iface;
|
---|
[40313e4] | 141 | csr->callid = callid;
|
---|
[9cfbf2f] | 142 | csr->arg3 = IPC_GET_ARG3(*call);
|
---|
[a35b458] | 143 |
|
---|
[40313e4] | 144 | /*
|
---|
| 145 | * We can forward the call only after the server we spawned connects
|
---|
| 146 | * to us. Meanwhile we might need to service more connection requests.
|
---|
| 147 | * Thus we store the call in a queue.
|
---|
| 148 | */
|
---|
| 149 | list_append(&csr->link, &cs_req);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * @}
|
---|
| 154 | */
|
---|