[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;
|
---|
[984a9ba] | 50 | ipc_call_t call;
|
---|
[40313e4] | 51 | } cs_req_t;
|
---|
| 52 |
|
---|
| 53 | /** List of clonable-service connection requests. */
|
---|
[b72efe8] | 54 | static list_t cs_req;
|
---|
[40313e4] | 55 |
|
---|
[9b1baac] | 56 | errno_t ns_clonable_init(void)
|
---|
[40313e4] | 57 | {
|
---|
| 58 | list_initialize(&cs_req);
|
---|
| 59 | return EOK;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /** Return true if @a service is clonable. */
|
---|
[9b1baac] | 63 | bool ns_service_is_clonable(service_t service, iface_t iface)
|
---|
[40313e4] | 64 | {
|
---|
[9b1baac] | 65 | return (service == SERVICE_LOADER) && (iface == INTERFACE_LOADER);
|
---|
[40313e4] | 66 | }
|
---|
| 67 |
|
---|
| 68 | /** Register clonable service.
|
---|
| 69 | *
|
---|
[9b1baac] | 70 | * @param call Pointer to call structure.
|
---|
[40313e4] | 71 | *
|
---|
| 72 | */
|
---|
[9b1baac] | 73 | void ns_clonable_register(ipc_call_t *call)
|
---|
[40313e4] | 74 | {
|
---|
[9cfbf2f] | 75 | link_t *req_link = list_first(&cs_req);
|
---|
[b72efe8] | 76 | if (req_link == NULL) {
|
---|
[40313e4] | 77 | /* There was no pending connection request. */
|
---|
[c7bbf029] | 78 | printf("%s: Unexpected clonable server.\n", NAME);
|
---|
[984a9ba] | 79 | async_answer_0(call, EBUSY);
|
---|
[40313e4] | 80 | return;
|
---|
| 81 | }
|
---|
[a35b458] | 82 |
|
---|
[b72efe8] | 83 | cs_req_t *csr = list_get_instance(req_link, cs_req_t, link);
|
---|
| 84 | list_remove(req_link);
|
---|
[a35b458] | 85 |
|
---|
[40313e4] | 86 | /* Currently we can only handle a single type of clonable service. */
|
---|
[9b1baac] | 87 | assert(ns_service_is_clonable(csr->service, csr->iface));
|
---|
[a35b458] | 88 |
|
---|
[984a9ba] | 89 | async_answer_0(call, EOK);
|
---|
[a35b458] | 90 |
|
---|
[7b616e2] | 91 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 92 | if (sess == NULL)
|
---|
[984a9ba] | 93 | async_answer_0(call, EIO);
|
---|
[a35b458] | 94 |
|
---|
[7b616e2] | 95 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[4f13e19] | 96 | async_forward_1(&csr->call, exch, csr->iface,
|
---|
[fafb8e5] | 97 | ipc_get_arg3(&csr->call), IPC_FF_NONE);
|
---|
[7b616e2] | 98 | async_exchange_end(exch);
|
---|
[a35b458] | 99 |
|
---|
[40313e4] | 100 | free(csr);
|
---|
[7b616e2] | 101 | async_hangup(sess);
|
---|
[40313e4] | 102 | }
|
---|
| 103 |
|
---|
| 104 | /** Connect client to clonable service.
|
---|
| 105 | *
|
---|
[984a9ba] | 106 | * @param service Service to be connected to.
|
---|
| 107 | * @param iface Interface to be connected to.
|
---|
| 108 | * @param call Pointer to call structure.
|
---|
[40313e4] | 109 | *
|
---|
| 110 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 111 | *
|
---|
| 112 | */
|
---|
[9b1baac] | 113 | void ns_clonable_forward(service_t service, iface_t iface, ipc_call_t *call)
|
---|
[40313e4] | 114 | {
|
---|
[9b1baac] | 115 | assert(ns_service_is_clonable(service, iface));
|
---|
[a35b458] | 116 |
|
---|
[40313e4] | 117 | cs_req_t *csr = malloc(sizeof(cs_req_t));
|
---|
| 118 | if (csr == NULL) {
|
---|
[984a9ba] | 119 | async_answer_0(call, ENOMEM);
|
---|
[40313e4] | 120 | return;
|
---|
| 121 | }
|
---|
[a35b458] | 122 |
|
---|
[40313e4] | 123 | /* Spawn a loader. */
|
---|
[b7fd2a0] | 124 | errno_t rc = loader_spawn("loader");
|
---|
[a35b458] | 125 |
|
---|
[d5c1051] | 126 | if (rc != EOK) {
|
---|
[40313e4] | 127 | free(csr);
|
---|
[984a9ba] | 128 | async_answer_0(call, rc);
|
---|
[40313e4] | 129 | return;
|
---|
| 130 | }
|
---|
[a35b458] | 131 |
|
---|
[007e6efa] | 132 | link_initialize(&csr->link);
|
---|
[40313e4] | 133 | csr->service = service;
|
---|
[9cfbf2f] | 134 | csr->iface = iface;
|
---|
[984a9ba] | 135 | csr->call = *call;
|
---|
[a35b458] | 136 |
|
---|
[40313e4] | 137 | /*
|
---|
| 138 | * We can forward the call only after the server we spawned connects
|
---|
| 139 | * to us. Meanwhile we might need to service more connection requests.
|
---|
| 140 | * Thus we store the call in a queue.
|
---|
| 141 | */
|
---|
| 142 | list_append(&csr->link, &cs_req);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /**
|
---|
| 146 | * @}
|
---|
| 147 | */
|
---|