source: mainline/uspace/srv/taskman/main.c@ 3529f148

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

Adding types task_wait_flag_t and ipc_start_flag_t which replaces makros

  • Property mode set to 100644
File size: 9.0 KB
Line 
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/**
30 * Locking order:
31 * - task_hash_table_lock (task.c),
32 * - pending_wait_lock (event.c),
33 * - listeners_lock (event.c).
34 *
35 * @addtogroup taskman
36 * @{
37 */
38#include <abi/ipc/methods.h>
39#include <adt/prodcons.h>
40#include <assert.h>
41#include <async.h>
42#include <errno.h>
43#include <fibril_synch.h>
44#include <ipc/services.h>
45#include <ipc/taskman.h>
46#include <loader/loader.h>
47#include <macros.h>
48#include <ns.h>
49#include <stdio.h>
50#include <stdlib.h>
51
52#include "event.h"
53#include "task.h"
54#include "taskman.h"
55
56typedef struct {
57 link_t link;
58 async_sess_t *sess;
59} sess_ref_t;
60
61static prodcons_t sess_queue;
62
63/** We keep session to NS on our own in taskman */
64static async_sess_t *session_ns = NULL;
65
66static FIBRIL_MUTEX_INITIALIZE(session_ns_mtx);
67static FIBRIL_CONDVAR_INITIALIZE(session_ns_cv);
68
69/*
70 * Static functions
71 */
72static void connect_to_loader(ipc_call_t *icall)
73{
74 DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
75 /*
76 * We don't accept the connection request, we forward it instead to
77 * freshly spawned loader.
78 */
79 errno_t rc = loader_spawn("loader");
80
81 if (rc != EOK) {
82 async_answer_0(icall, rc);
83 return;
84 }
85
86 /* Wait until spawned task presents itself to us. */
87 link_t *link = prodcons_consume(&sess_queue);
88 sess_ref_t *sess_ref = list_get_instance(link, sess_ref_t, link);
89
90 /* Forward the connection request (strip interface arg). */
91 async_exch_t *exch = async_exchange_begin(sess_ref->sess);
92 rc = async_forward_1(icall, exch,
93 ipc_get_arg2(icall),
94 ipc_get_arg3(icall),
95 IPC_FF_NONE);
96 async_exchange_end(exch);
97
98 /* After forward we can dispose all session-related resources */
99 //async_hangup(sess_ref->sess);
100 free(sess_ref);
101
102 if (rc != EOK) {
103 async_answer_0(icall, rc);
104 return;
105 }
106
107 /* Everything OK. */
108}
109
110static void connect_to_ns(ipc_call_t *icall)
111{
112 DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
113
114 /* Wait until we know NS */
115 fibril_mutex_lock(&session_ns_mtx);
116 while (session_ns == NULL) {
117 fibril_condvar_wait(&session_ns_cv, &session_ns_mtx);
118 }
119 fibril_mutex_unlock(&session_ns_mtx);
120
121 /* Do not accept connection, forward it */
122 async_exch_t *exch = async_exchange_begin(session_ns);
123 errno_t rc = async_forward_0(icall, exch, 0, IPC_FF_NONE);
124 async_exchange_end(exch);
125
126 if (rc != EOK)
127 async_answer_0(icall, rc);
128}
129
130static void taskman_new_task(ipc_call_t *icall)
131{
132 DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
133 errno_t rc = task_intro(icall->task_id);
134 if (rc == EOK) {
135 async_accept_0(icall);
136 } else {
137 async_answer_0(icall, rc);
138 }
139}
140
141static void taskman_i_am_ns(ipc_call_t *icall)
142{
143 DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
144 errno_t rc = EOK;
145
146 fibril_mutex_lock(&session_ns_mtx);
147 if (session_ns != NULL) {
148 rc = EEXIST;
149 goto finish;
150 }
151
152 /* Used only for connection forwarding -- atomic */
153 session_ns = async_callback_receive(EXCHANGE_ATOMIC);
154
155 if (session_ns == NULL) {
156 rc = ENOENT;
157 printf("%s: Cannot connect to NS\n", NAME);
158 }
159
160 fibril_condvar_broadcast(&session_ns_cv);
161finish:
162 fibril_mutex_unlock(&session_ns_mtx);
163
164 if (rc == EOK) {
165 async_accept_0(icall);
166 } else {
167 async_answer_0(icall, rc);
168 }
169}
170
171static void taskman_ctl_wait(ipc_call_t *icall)
172{
173 task_id_t id = (task_id_t)
174 MERGE_LOUP32(ipc_get_arg1(icall), ipc_get_arg2(icall));
175 int flags = ipc_get_arg3(icall);
176 task_id_t waiter_id = icall->task_id;
177
178 wait_for_task(id, flags, icall, waiter_id);
179}
180
181static void taskman_ctl_retval(ipc_call_t *icall)
182{
183 task_id_t sender = icall->task_id;
184 int retval = ipc_get_arg1(icall);
185 bool wait_for_exit = ipc_get_arg2(icall);
186
187 DPRINTF("%s:%d from %" PRIu64 "/%i\n", __func__, __LINE__, sender, retval);
188
189 errno_t rc = task_set_retval(sender, retval, wait_for_exit);
190 async_answer_0(icall, rc);
191}
192
193static void taskman_ctl_ev_callback(ipc_call_t *icall)
194{
195 DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
196
197 bool past_events = ipc_get_arg1(icall);
198
199 /* Atomic -- will be used for notifications only */
200 async_sess_t *sess = async_callback_receive(EXCHANGE_ATOMIC);
201 if (sess == NULL) {
202 async_answer_0(icall, ENOMEM);
203 return;
204 }
205
206 event_register_listener(icall->task_id, past_events, sess, icall);
207}
208
209static void task_exit_event(ipc_call_t *icall, void *arg)
210{
211 task_id_t id = MERGE_LOUP32(ipc_get_arg1(icall), ipc_get_arg2(icall));
212 exit_reason_t exit_reason = ipc_get_arg3(icall);
213 DPRINTF("%s:%d from %" PRIu64 "/%i\n", __func__, __LINE__, id, exit_reason);
214 task_terminated(id, exit_reason);
215}
216
217static void task_fault_event(ipc_call_t *icall, void *arg)
218{
219 task_id_t id = MERGE_LOUP32(ipc_get_arg1(icall), ipc_get_arg2(icall));
220 DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, id);
221 task_failed(id);
222}
223
224static void taskman_i_am_loader(ipc_call_t *icall)
225{
226 DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
227 // TODO check that loader is expected, would probably discard prodcons
228 // scheme
229
230 /* Preallocate session container */
231 sess_ref_t *sess_ref = malloc(sizeof(sess_ref_t));
232 if (sess_ref == NULL) {
233 async_answer_0(icall, ENOMEM);
234 return;
235 }
236
237 /* Create callback connection */
238 sess_ref->sess = async_callback_receive(EXCHANGE_ATOMIC);
239
240 /* Notify spawners */
241 link_initialize(&sess_ref->link);
242 prodcons_produce(&sess_queue, &sess_ref->link);
243}
244
245static void taskman_connection(ipc_call_t *icall, void *arg)
246{
247 /* handle new incoming IPC_M_CONNECT_ME_TO calls */
248 switch (ipc_get_arg2(icall)) {
249 case TASKMAN_NEW_TASK:
250 taskman_new_task(icall);
251 break;
252 case TASKMAN_CONNECT_TO_NS:
253 connect_to_ns(icall);
254 break;
255 case TASKMAN_CONNECT_TO_LOADER:
256 connect_to_loader(icall);
257 break;
258 default:
259 DPRINTF("%s:%d from %" PRIu64 "/%" SCNuPTR "/%" SCNuPTR "/%" SCNuPTR "\n",
260 __func__, __LINE__,
261 icall->task_id, ipc_get_imethod(icall),
262 ipc_get_arg1(icall), ipc_get_arg2(icall));
263 async_answer_0(icall, ENOTSUP);
264 return;
265 }
266
267 /* handle accepted calls */
268 while (true) {
269 ipc_call_t call;
270
271 if (!async_get_call(&call) || !ipc_get_imethod(&call)) {
272 /* Client disconnected */
273 DPRINTF("%s:%d client disconnected\n", __func__, __LINE__);
274 break;
275 }
276
277 switch (ipc_get_imethod(&call)) {
278 case TASKMAN_I_AM_NS:
279 taskman_i_am_ns(&call);
280 break;
281 case TASKMAN_I_AM_LOADER:
282 taskman_i_am_loader(&call);
283 break;
284 case TASKMAN_WAIT:
285 taskman_ctl_wait(&call);
286 break;
287 case TASKMAN_RETVAL:
288 taskman_ctl_retval(&call);
289 break;
290 case TASKMAN_EVENT_CALLBACK:
291 taskman_ctl_ev_callback(&call);
292 break;
293 default:
294 DPRINTF("%s:%d from %" PRIu64 "/%" SCNuPTR "/%" SCNuPTR "/%" SCNuPTR "\n",
295 __func__, __LINE__,
296 call.task_id, ipc_get_imethod(&call),
297 ipc_get_arg1(&call), ipc_get_arg2(&call));
298 async_answer_0(&call, ENOTSUP);
299 }
300 }
301}
302
303int main(int argc, char *argv[])
304{
305 printf(NAME ": HelenOS task manager\n");
306
307 /* Initialization */
308 prodcons_initialize(&sess_queue);
309 errno_t rc = tasks_init();
310 if (rc != EOK) {
311 return rc;
312 }
313 rc = event_init();
314 if (rc != EOK) {
315 return rc;
316 }
317
318 rc = async_event_subscribe(EVENT_EXIT, task_exit_event, NULL);
319 if (rc != EOK) {
320 printf(NAME ": Cannot register for exit events (%i).\n", rc);
321 return rc;
322 }
323
324 rc = async_event_subscribe(EVENT_FAULT, task_fault_event, NULL);
325 if (rc != EOK) {
326 printf(NAME ": Cannot register for fault events (%i).\n", rc);
327 return rc;
328 }
329
330 task_id_t self_id = task_get_id();
331 rc = task_intro(self_id);
332 if (rc != EOK) {
333 printf(NAME ": Cannot register self as task (%i).\n", rc);
334 }
335
336 /* Start sysman server */
337 async_set_fallback_port_handler(taskman_connection, NULL);
338
339 printf(NAME ": Accepting connections\n");
340 (void)task_set_retval(self_id, EOK, false);
341 async_manager();
342
343 /* not reached */
344 return 0;
345}
346
347/**
348 * @}
349 */
Note: See TracBrowser for help on using the repository browser.