| 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 |
|
|---|
| 56 | typedef struct {
|
|---|
| 57 | link_t link;
|
|---|
| 58 | async_sess_t *sess;
|
|---|
| 59 | } sess_ref_t;
|
|---|
| 60 |
|
|---|
| 61 | static prodcons_t sess_queue;
|
|---|
| 62 |
|
|---|
| 63 | /** We keep session to NS on our own in taskman */
|
|---|
| 64 | static async_sess_t *session_ns = NULL;
|
|---|
| 65 |
|
|---|
| 66 | static FIBRIL_MUTEX_INITIALIZE(session_ns_mtx);
|
|---|
| 67 | static FIBRIL_CONDVAR_INITIALIZE(session_ns_cv);
|
|---|
| 68 |
|
|---|
| 69 | /*
|
|---|
| 70 | * Static functions
|
|---|
| 71 | */
|
|---|
| 72 | static 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 |
|
|---|
| 110 | static 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 |
|
|---|
| 130 | static 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 |
|
|---|
| 141 | static 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);
|
|---|
| 161 | finish:
|
|---|
| 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 |
|
|---|
| 171 | static 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 |
|
|---|
| 181 | static 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 |
|
|---|
| 193 | static 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 |
|
|---|
| 209 | static 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 |
|
|---|
| 217 | static 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 |
|
|---|
| 224 | static void loader_callback(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 | }
|
|---|
| 235 |
|
|---|
| 236 | /* Create callback connection */
|
|---|
| 237 | sess_ref->sess = async_callback_receive_start(EXCHANGE_ATOMIC, icall);
|
|---|
| 238 | if (sess_ref->sess == NULL) {
|
|---|
| 239 | async_answer_0(icall, EINVAL);
|
|---|
| 240 | return;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | async_answer_0(icall, EOK);
|
|---|
| 244 |
|
|---|
| 245 | /* Notify spawners */
|
|---|
| 246 | link_initialize(&sess_ref->link);
|
|---|
| 247 | prodcons_produce(&sess_queue, &sess_ref->link);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | static void taskman_connection(ipc_call_t *icall, void *arg)
|
|---|
| 251 | {
|
|---|
| 252 | /* handle new incoming calls */
|
|---|
| 253 | if (ipc_get_imethod(icall) == IPC_M_CONNECT_ME_TO) {
|
|---|
| 254 | switch (ipc_get_arg2(icall)) {
|
|---|
| 255 | case TASKMAN_NEW_TASK:
|
|---|
| 256 | taskman_new_task(icall);
|
|---|
| 257 | break;
|
|---|
| 258 | case TASKMAN_CONNECT_TO_NS:
|
|---|
| 259 | connect_to_ns(icall);
|
|---|
| 260 | return;
|
|---|
| 261 | case TASKMAN_CONNECT_TO_LOADER:
|
|---|
| 262 | connect_to_loader(icall);
|
|---|
| 263 | return;
|
|---|
| 264 | default:
|
|---|
| 265 | DPRINTF("%s:%d from %" PRIu64 "/%" SCNuPTR "/%" SCNuPTR "/%" SCNuPTR "\n",
|
|---|
| 266 | __func__, __LINE__,
|
|---|
| 267 | icall->task_id, ipc_get_imethod(icall),
|
|---|
| 268 | ipc_get_arg1(icall), ipc_get_arg2(icall));
|
|---|
| 269 | async_answer_0(icall, ENOTSUP);
|
|---|
| 270 | return;
|
|---|
| 271 | }
|
|---|
| 272 | } else if (ipc_get_imethod(icall) == IPC_M_CONNECT_TO_ME) {
|
|---|
| 273 | switch (ipc_get_arg2(icall)) {
|
|---|
| 274 | case TASKMAN_LOADER_CALLBACK:
|
|---|
| 275 | loader_callback(icall);
|
|---|
| 276 | return;
|
|---|
| 277 | default:
|
|---|
| 278 | DPRINTF("%s:%d from %" PRIu64 "/%" SCNuPTR "/%" SCNuPTR "/%" SCNuPTR "\n",
|
|---|
| 279 | __func__, __LINE__,
|
|---|
| 280 | icall->task_id, ipc_get_imethod(icall),
|
|---|
| 281 | ipc_get_arg1(icall), ipc_get_arg2(icall));
|
|---|
| 282 | async_answer_0(icall, ENOTSUP);
|
|---|
| 283 | return;
|
|---|
| 284 | }
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /* handle accepted calls */
|
|---|
| 288 | while (true) {
|
|---|
| 289 | ipc_call_t call;
|
|---|
| 290 |
|
|---|
| 291 | if (!async_get_call(&call)) {
|
|---|
| 292 | /* Client disconnected */
|
|---|
| 293 | DPRINTF("%s:%d client disconnected\n", __func__, __LINE__);
|
|---|
| 294 | return;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | switch (ipc_get_imethod(&call)) {
|
|---|
| 298 | case TASKMAN_I_AM_NS:
|
|---|
| 299 | taskman_i_am_ns(&call);
|
|---|
| 300 | break;
|
|---|
| 301 | case TASKMAN_WAIT:
|
|---|
| 302 | taskman_ctl_wait(&call);
|
|---|
| 303 | break;
|
|---|
| 304 | case TASKMAN_RETVAL:
|
|---|
| 305 | taskman_ctl_retval(&call);
|
|---|
| 306 | break;
|
|---|
| 307 | case TASKMAN_EVENT_CALLBACK:
|
|---|
| 308 | taskman_ctl_ev_callback(&call);
|
|---|
| 309 | break;
|
|---|
| 310 | default:
|
|---|
| 311 | DPRINTF("%s:%d from %" PRIu64 "/%" SCNuPTR "/%" SCNuPTR "/%" SCNuPTR "\n",
|
|---|
| 312 | __func__, __LINE__,
|
|---|
| 313 | call.task_id, ipc_get_imethod(&call),
|
|---|
| 314 | ipc_get_arg1(&call), ipc_get_arg2(&call));
|
|---|
| 315 | async_answer_0(&call, ENOTSUP);
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | int main(int argc, char *argv[])
|
|---|
| 321 | {
|
|---|
| 322 | printf(NAME ": HelenOS task manager\n");
|
|---|
| 323 |
|
|---|
| 324 | /* Initialization */
|
|---|
| 325 | prodcons_initialize(&sess_queue);
|
|---|
| 326 | errno_t rc = tasks_init();
|
|---|
| 327 | if (rc != EOK) {
|
|---|
| 328 | return rc;
|
|---|
| 329 | }
|
|---|
| 330 | rc = event_init();
|
|---|
| 331 | if (rc != EOK) {
|
|---|
| 332 | return rc;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | rc = async_event_subscribe(EVENT_EXIT, task_exit_event, NULL);
|
|---|
| 336 | if (rc != EOK) {
|
|---|
| 337 | printf(NAME ": Cannot register for exit events (%i).\n", rc);
|
|---|
| 338 | return rc;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | rc = async_event_subscribe(EVENT_FAULT, task_fault_event, NULL);
|
|---|
| 342 | if (rc != EOK) {
|
|---|
| 343 | printf(NAME ": Cannot register for fault events (%i).\n", rc);
|
|---|
| 344 | return rc;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | task_id_t self_id = task_get_id();
|
|---|
| 348 | rc = task_intro(self_id);
|
|---|
| 349 | if (rc != EOK) {
|
|---|
| 350 | printf(NAME ": Cannot register self as task (%i).\n", rc);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /* Start sysman server */
|
|---|
| 354 | async_set_fallback_port_handler(taskman_connection, NULL);
|
|---|
| 355 |
|
|---|
| 356 | printf(NAME ": Accepting connections\n");
|
|---|
| 357 | (void)task_set_retval(self_id, EOK, false);
|
|---|
| 358 | async_manager();
|
|---|
| 359 |
|
|---|
| 360 | /* not reached */
|
|---|
| 361 | return 0;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | /**
|
|---|
| 365 | * @}
|
|---|
| 366 | */
|
|---|