source: mainline/uspace/srv/taskman/main.c@ 5cd2290

Last change on this file since 5cd2290 was 5cd2290, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

taskman: Make use of EVENT_EXIT exit_reason parameter

  • Also improved handling of EVENT_FAULT — it only marks task as failed, waiters are notified after EVENT_EXIT happens.
  • Property mode set to 100644
File size: 7.5 KB
RevLine 
[0a8f070]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>
[1be7bee]36#include <macros.h>
[0a8f070]37#include <ns.h>
38#include <stdio.h>
39#include <stdlib.h>
40
[1be7bee]41#include "task.h"
42#include "taskman.h"
[0a8f070]43
44//TODO move to appropriate header file
45extern async_sess_t *session_primary;
46
47typedef struct {
48 link_t link;
49 async_sess_t *sess;
50} sess_ref_t;
51
52static prodcons_t sess_queue;
53
54
55/*
56 * Static functions
57 */
58static void connect_to_loader(ipc_callid_t iid, ipc_call_t *icall)
59{
[2f44fafd]60 /* We don't accept the connection request, we forward it instead to
61 * freshly spawned loader. */
[0a8f070]62 int rc = loader_spawn("loader");
63
64 if (rc != EOK) {
65 async_answer_0(iid, rc);
66 return;
67 }
68
69 /* Wait until spawned task presents itself to us. */
70 link_t *link = prodcons_consume(&sess_queue);
71 sess_ref_t *sess_ref = list_get_instance(link, sess_ref_t, link);
72
73 /* Forward the connection request (strip interface arg). */
74 async_exch_t *exch = async_exchange_begin(sess_ref->sess);
75 rc = async_forward_fast(iid, exch,
76 IPC_GET_ARG2(*icall),
77 IPC_GET_ARG3(*icall),
78 0, IPC_FF_NONE);
79 async_exchange_end(exch);
80
[2f44fafd]81 /* After forward we can dispose all session-related resources */
82 async_hangup(sess_ref->sess);
[0a8f070]83 free(sess_ref);
84
85 if (rc != EOK) {
86 async_answer_0(iid, rc);
87 return;
88 }
89
90 /* Everything OK. */
91}
92
93static void loader_to_ns(ipc_callid_t iid, ipc_call_t *icall)
94{
95 /* Do no accept connection request, forward it instead. */
96 async_exch_t *exch = async_exchange_begin(session_primary);
97 int rc = async_forward_fast(iid, exch, 0, 0, 0, IPC_FF_NONE);
98 async_exchange_end(exch);
99
100 if (rc != EOK) {
101 async_answer_0(iid, rc);
102 return;
103 }
104}
105
[1be7bee]106static void taskman_ctl_wait(ipc_callid_t iid, ipc_call_t *icall)
107{
108 task_id_t id = (task_id_t)
109 MERGE_LOUP32(IPC_GET_ARG1(*icall), IPC_GET_ARG2(*icall));
110 int flags = IPC_GET_ARG3(*icall);
111
112 wait_for_task(id, flags, iid, icall);
113}
114
115static void taskman_ctl_retval(ipc_callid_t iid, ipc_call_t *icall)
116{
[d4ec49e]117 printf("%s:%i from %llu\n", __func__, __LINE__, icall->in_task_id);
[1be7bee]118 int rc = task_set_retval(icall);
119 async_answer_0(iid, rc);
120}
121
[62273d1]122static void task_exit_event(ipc_callid_t iid, ipc_call_t *icall, void *arg)
123{
124 task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*icall), IPC_GET_ARG2(*icall));
[5cd2290]125 exit_reason_t exit_reason = IPC_GET_ARG3(*icall);
126 printf("%s:%i from %llu/%i\n", __func__, __LINE__, id, exit_reason);
127 task_terminated(id, exit_reason);
128}
129
130static void task_fault_event(ipc_callid_t iid, ipc_call_t *icall, void *arg)
131{
132 task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*icall), IPC_GET_ARG2(*icall));
133 printf("%s:%i from %llu\n", __func__, __LINE__, id);
134 task_failed(id);
[62273d1]135}
136
[1be7bee]137static void control_connection_loop(void)
138{
139 while (true) {
140 ipc_call_t call;
141 ipc_callid_t callid = async_get_call(&call);
142
143 if (!IPC_GET_IMETHOD(call)) {
144 /* Client disconnected */
145 break;
146 }
147
148 switch (IPC_GET_IMETHOD(call)) {
149 case TASKMAN_WAIT:
150 taskman_ctl_wait(callid, &call);
151 break;
152 case TASKMAN_RETVAL:
153 taskman_ctl_retval(callid, &call);
154 break;
155 default:
156 async_answer_0(callid, ENOENT);
157 }
158 }
159}
160
161static void control_connection(ipc_callid_t iid, ipc_call_t *icall)
162{
[2f44fafd]163 /* TODO remove/redesign the workaround
164 * Call task_intro here for boot-time tasks,
165 * probably they should announce themselves explicitly
166 * or taskman should detect them from kernel's list of tasks.
167 */
168 int rc = task_intro(icall, false);
169
[1be7bee]170 /* First, accept connection */
[2f44fafd]171 async_answer_0(iid, rc);
172
173 if (rc != EOK) {
174 return;
175 }
[1be7bee]176
177 control_connection_loop();
178}
179
[0a8f070]180static void loader_callback(ipc_callid_t iid, ipc_call_t *icall)
181{
182 // TODO check that loader is expected, would probably discard prodcons
183 // scheme
184
185 /* Preallocate session container */
186 sess_ref_t *sess_ref = malloc(sizeof(sess_ref_t));
187 if (sess_ref == NULL) {
188 async_answer_0(iid, ENOMEM);
189 }
190
191 /* Create callback connection */
192 sess_ref->sess = async_callback_receive_start(EXCHANGE_ATOMIC, icall);
193 if (sess_ref->sess == NULL) {
194 async_answer_0(iid, EINVAL);
195 return;
196 }
[62273d1]197
198 /* Remember task_id */
[2f44fafd]199 int rc = task_intro(icall, true);
[62273d1]200
201 if (rc != EOK) {
202 async_answer_0(iid, rc);
203 free(sess_ref);
204 return;
205 }
[0a8f070]206 async_answer_0(iid, EOK);
207
[62273d1]208 /* Notify spawners */
[0a8f070]209 link_initialize(&sess_ref->link);
210 prodcons_produce(&sess_queue, &sess_ref->link);
211}
212
213static void taskman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
214{
215 taskman_interface_t iface = IPC_GET_ARG1(*icall);
216 switch (iface) {
217 case TASKMAN_CONNECT_TO_LOADER:
218 connect_to_loader(iid, icall);
219 break;
220 case TASKMAN_LOADER_TO_NS:
221 loader_to_ns(iid, icall);
222 break;
[1be7bee]223 case TASKMAN_CONTROL:
224 control_connection(iid, icall);
225 // ---- interrupt here ----
226 // implement control connection body (setup wait)
227 // ------------------------
228 break;
[0a8f070]229 default:
230 /* Unknown interface */
231 async_answer_0(iid, ENOENT);
232 }
233}
234
235static void implicit_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
236{
237 taskman_interface_t iface = IPC_GET_ARG1(*icall);
238 switch (iface) {
239 case TASKMAN_LOADER_CALLBACK:
240 loader_callback(iid, icall);
[1be7bee]241 control_connection_loop();
[0a8f070]242 break;
243 default:
244 /* Unknown interface on implicit connection */
245 async_answer_0(iid, EHANGUP);
246 }
247}
248
249/** Build hard coded configuration */
250
251
252int main(int argc, char *argv[])
253{
254 printf(NAME ": HelenOS task manager\n");
255
[62273d1]256 /* Initialization */
[0a8f070]257 prodcons_initialize(&sess_queue);
[1be7bee]258 int rc = task_init();
259 if (rc != EOK) {
260 return rc;
261 }
[0a8f070]262
[5cd2290]263 rc = async_event_subscribe(EVENT_EXIT, task_exit_event, NULL);
[62273d1]264 if (rc != EOK) {
265 printf("Cannot register for exit events (%i).\n", rc);
266 return rc;
267 }
268
[5cd2290]269 rc = async_event_subscribe(EVENT_FAULT, task_fault_event, NULL);
[62273d1]270 if (rc != EOK) {
271 printf("Cannot register for fault events (%i).\n", rc);
272 return rc;
273 }
274
[0a8f070]275 /* We're service too */
[1be7bee]276 rc = service_register(SERVICE_TASKMAN);
[0a8f070]277 if (rc != EOK) {
[62273d1]278 printf("Cannot register at naming service (%i).\n", rc);
[0a8f070]279 return rc;
280 }
281
282 /* Start sysman server */
283 async_set_client_connection(taskman_connection);
284 async_set_implicit_connection(implicit_connection);
285
286 printf(NAME ": Accepting connections\n");
287 //TODO task_retval(EOK);
288 async_manager();
289
290 /* not reached */
291 return 0;
292}
Note: See TracBrowser for help on using the repository browser.