1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
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 | #include <adt/prodcons.h>
|
---|
30 | #include <assert.h>
|
---|
31 | #include <async.h>
|
---|
32 | #include <errno.h>
|
---|
33 | #include <ipc/services.h>
|
---|
34 | #include <ipc/taskman.h>
|
---|
35 | #include <loader/loader.h>
|
---|
36 | #include <ns.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 |
|
---|
40 | #define NAME "taskman"
|
---|
41 |
|
---|
42 | //TODO move to appropriate header file
|
---|
43 | extern async_sess_t *session_primary;
|
---|
44 |
|
---|
45 | typedef struct {
|
---|
46 | link_t link;
|
---|
47 | async_sess_t *sess;
|
---|
48 | } sess_ref_t;
|
---|
49 |
|
---|
50 | static prodcons_t sess_queue;
|
---|
51 |
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Static functions
|
---|
55 | */
|
---|
56 | static void connect_to_loader(ipc_callid_t iid, ipc_call_t *icall)
|
---|
57 | {
|
---|
58 | /* Spawn a loader. */
|
---|
59 | int rc = loader_spawn("loader");
|
---|
60 |
|
---|
61 | if (rc != EOK) {
|
---|
62 | async_answer_0(iid, rc);
|
---|
63 | return;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* Wait until spawned task presents itself to us. */
|
---|
67 | link_t *link = prodcons_consume(&sess_queue);
|
---|
68 | sess_ref_t *sess_ref = list_get_instance(link, sess_ref_t, link);
|
---|
69 |
|
---|
70 | /* Forward the connection request (strip interface arg). */
|
---|
71 | async_exch_t *exch = async_exchange_begin(sess_ref->sess);
|
---|
72 | rc = async_forward_fast(iid, exch,
|
---|
73 | IPC_GET_ARG2(*icall),
|
---|
74 | IPC_GET_ARG3(*icall),
|
---|
75 | 0, IPC_FF_NONE);
|
---|
76 | async_exchange_end(exch);
|
---|
77 |
|
---|
78 | free(sess_ref);
|
---|
79 |
|
---|
80 | if (rc != EOK) {
|
---|
81 | async_answer_0(iid, rc);
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* Everything OK. */
|
---|
86 | }
|
---|
87 |
|
---|
88 | static void loader_to_ns(ipc_callid_t iid, ipc_call_t *icall)
|
---|
89 | {
|
---|
90 | /* Do no accept connection request, forward it instead. */
|
---|
91 | async_exch_t *exch = async_exchange_begin(session_primary);
|
---|
92 | int rc = async_forward_fast(iid, exch, 0, 0, 0, IPC_FF_NONE);
|
---|
93 | async_exchange_end(exch);
|
---|
94 |
|
---|
95 | if (rc != EOK) {
|
---|
96 | async_answer_0(iid, rc);
|
---|
97 | return;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | static void loader_callback(ipc_callid_t iid, ipc_call_t *icall)
|
---|
102 | {
|
---|
103 | // TODO check that loader is expected, would probably discard prodcons
|
---|
104 | // scheme
|
---|
105 |
|
---|
106 | /* Preallocate session container */
|
---|
107 | sess_ref_t *sess_ref = malloc(sizeof(sess_ref_t));
|
---|
108 | if (sess_ref == NULL) {
|
---|
109 | async_answer_0(iid, ENOMEM);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /* Create callback connection */
|
---|
114 | sess_ref->sess = async_callback_receive_start(EXCHANGE_ATOMIC, icall);
|
---|
115 | if (sess_ref->sess == NULL) {
|
---|
116 | //TODO different error code?
|
---|
117 | async_answer_0(iid, EINVAL);
|
---|
118 | return;
|
---|
119 | }
|
---|
120 | async_answer_0(iid, EOK);
|
---|
121 |
|
---|
122 | link_initialize(&sess_ref->link);
|
---|
123 | prodcons_produce(&sess_queue, &sess_ref->link);
|
---|
124 | }
|
---|
125 |
|
---|
126 | static void taskman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
127 | {
|
---|
128 | taskman_interface_t iface = IPC_GET_ARG1(*icall);
|
---|
129 | switch (iface) {
|
---|
130 | case TASKMAN_CONNECT_TO_LOADER:
|
---|
131 | connect_to_loader(iid, icall);
|
---|
132 | break;
|
---|
133 | case TASKMAN_LOADER_TO_NS:
|
---|
134 | loader_to_ns(iid, icall);
|
---|
135 | break;
|
---|
136 | default:
|
---|
137 | /* Unknown interface */
|
---|
138 | async_answer_0(iid, ENOENT);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | static void implicit_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
143 | {
|
---|
144 | taskman_interface_t iface = IPC_GET_ARG1(*icall);
|
---|
145 | switch (iface) {
|
---|
146 | case TASKMAN_LOADER_CALLBACK:
|
---|
147 | loader_callback(iid, icall);
|
---|
148 | break;
|
---|
149 | default:
|
---|
150 | /* Unknown interface on implicit connection */
|
---|
151 | async_answer_0(iid, EHANGUP);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Build hard coded configuration */
|
---|
156 |
|
---|
157 |
|
---|
158 | int main(int argc, char *argv[])
|
---|
159 | {
|
---|
160 | printf(NAME ": HelenOS task manager\n");
|
---|
161 |
|
---|
162 | prodcons_initialize(&sess_queue);
|
---|
163 |
|
---|
164 | /* We're service too */
|
---|
165 | int rc = service_register(SERVICE_TASKMAN);
|
---|
166 | if (rc != EOK) {
|
---|
167 | printf("Cannot register at naming service (%i).", rc);
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* Start sysman server */
|
---|
172 | async_set_client_connection(taskman_connection);
|
---|
173 | async_set_implicit_connection(implicit_connection);
|
---|
174 |
|
---|
175 | printf(NAME ": Accepting connections\n");
|
---|
176 | //TODO task_retval(EOK);
|
---|
177 | async_manager();
|
---|
178 |
|
---|
179 | /* not reached */
|
---|
180 | return 0;
|
---|
181 | }
|
---|