| 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 |
|
|---|
| 33 | #include <ipc/ipc.h>
|
|---|
| 34 | #include <ipc/services.h>
|
|---|
| 35 | #include <adt/list.h>
|
|---|
| 36 | #include <stdbool.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <assert.h>
|
|---|
| 39 | #include <stdio.h>
|
|---|
| 40 | #include <malloc.h>
|
|---|
| 41 | #include <loader/loader.h>
|
|---|
| 42 | #include "clonable.h"
|
|---|
| 43 | #include "ns.h"
|
|---|
| 44 |
|
|---|
| 45 | /** Request for connection to a clonable service. */
|
|---|
| 46 | typedef struct {
|
|---|
| 47 | link_t link;
|
|---|
| 48 | service_t service;
|
|---|
| 49 | iface_t iface;
|
|---|
| 50 | ipc_callid_t callid;
|
|---|
| 51 | sysarg_t arg3;
|
|---|
| 52 | } cs_req_t;
|
|---|
| 53 |
|
|---|
| 54 | /** List of clonable-service connection requests. */
|
|---|
| 55 | static list_t cs_req;
|
|---|
| 56 |
|
|---|
| 57 | int clonable_init(void)
|
|---|
| 58 | {
|
|---|
| 59 | list_initialize(&cs_req);
|
|---|
| 60 | return EOK;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /** Return true if @a service is clonable. */
|
|---|
| 64 | bool service_clonable(service_t service)
|
|---|
| 65 | {
|
|---|
| 66 | return (service == SERVICE_LOADER);
|
|---|
| 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 | */
|
|---|
| 76 | void register_clonable(service_t service, sysarg_t phone, ipc_call_t *call,
|
|---|
| 77 | ipc_callid_t callid)
|
|---|
| 78 | {
|
|---|
| 79 | link_t *req_link = list_first(&cs_req);
|
|---|
| 80 | if (req_link == NULL) {
|
|---|
| 81 | /* There was no pending connection request. */
|
|---|
| 82 | printf("%s: Unexpected clonable server.\n", NAME);
|
|---|
| 83 | ipc_answer_0(callid, EBUSY);
|
|---|
| 84 | return;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | cs_req_t *csr = list_get_instance(req_link, cs_req_t, link);
|
|---|
| 88 | list_remove(req_link);
|
|---|
| 89 |
|
|---|
| 90 | /* Currently we can only handle a single type of clonable service. */
|
|---|
| 91 | assert(csr->service == SERVICE_LOADER);
|
|---|
| 92 |
|
|---|
| 93 | ipc_answer_0(callid, EOK);
|
|---|
| 94 |
|
|---|
| 95 | ipc_forward_fast(csr->callid, phone, csr->iface, csr->arg3, 0,
|
|---|
| 96 | IPC_FF_NONE);
|
|---|
| 97 |
|
|---|
| 98 | free(csr);
|
|---|
| 99 | ipc_hangup(phone);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Connect client to clonable service.
|
|---|
| 103 | *
|
|---|
| 104 | * @param service Service to be connected to.
|
|---|
| 105 | * @param iface Interface to be connected to.
|
|---|
| 106 | * @param call Pointer to call structure.
|
|---|
| 107 | * @param callid Call ID of the request.
|
|---|
| 108 | *
|
|---|
| 109 | * @return Zero on success or a value from @ref errno.h.
|
|---|
| 110 | *
|
|---|
| 111 | */
|
|---|
| 112 | void connect_to_clonable(service_t service, iface_t iface, ipc_call_t *call,
|
|---|
| 113 | ipc_callid_t callid)
|
|---|
| 114 | {
|
|---|
| 115 | assert(service == SERVICE_LOADER);
|
|---|
| 116 |
|
|---|
| 117 | cs_req_t *csr = malloc(sizeof(cs_req_t));
|
|---|
| 118 | if (csr == NULL) {
|
|---|
| 119 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 120 | return;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /* Spawn a loader. */
|
|---|
| 124 | int rc = loader_spawn("loader");
|
|---|
| 125 |
|
|---|
| 126 | if (rc < 0) {
|
|---|
| 127 | free(csr);
|
|---|
| 128 | ipc_answer_0(callid, rc);
|
|---|
| 129 | return;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | link_initialize(&csr->link);
|
|---|
| 133 | csr->service = service;
|
|---|
| 134 | csr->iface = iface;
|
|---|
| 135 | csr->callid = callid;
|
|---|
| 136 | csr->arg3 = IPC_GET_ARG3(*call);
|
|---|
| 137 |
|
|---|
| 138 | /*
|
|---|
| 139 | * We can forward the call only after the server we spawned connects
|
|---|
| 140 | * to us. Meanwhile we might need to service more connection requests.
|
|---|
| 141 | * Thus we store the call in a queue.
|
|---|
| 142 | */
|
|---|
| 143 | list_append(&csr->link, &cs_req);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * @}
|
|---|
| 148 | */
|
|---|