| [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 |
|
|---|
| [780c8ce] | 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 | */
|
|---|
| [16d748ee] | 38 | #include <abi/ipc/methods.h>
|
|---|
| [0a8f070] | 39 | #include <adt/prodcons.h>
|
|---|
| 40 | #include <assert.h>
|
|---|
| 41 | #include <async.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| [012dd8e] | 43 | #include <fibril_synch.h>
|
|---|
| [0a8f070] | 44 | #include <ipc/services.h>
|
|---|
| 45 | #include <ipc/taskman.h>
|
|---|
| 46 | #include <loader/loader.h>
|
|---|
| [1be7bee] | 47 | #include <macros.h>
|
|---|
| [0a8f070] | 48 | #include <ns.h>
|
|---|
| 49 | #include <stdio.h>
|
|---|
| 50 | #include <stdlib.h>
|
|---|
| 51 |
|
|---|
| [035d7d8] | 52 | #include "event.h"
|
|---|
| [1be7bee] | 53 | #include "task.h"
|
|---|
| 54 | #include "taskman.h"
|
|---|
| [0a8f070] | 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 |
|
|---|
| [012dd8e] | 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);
|
|---|
| [0a8f070] | 68 |
|
|---|
| 69 | /*
|
|---|
| 70 | * Static functions
|
|---|
| 71 | */
|
|---|
| [241f1985] | 72 | static void connect_to_loader(ipc_call_t *icall)
|
|---|
| [0a8f070] | 73 | {
|
|---|
| [66b1075] | 74 | DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
|
|---|
| [102f641] | 75 | /*
|
|---|
| 76 | * We don't accept the connection request, we forward it instead to
|
|---|
| 77 | * freshly spawned loader.
|
|---|
| 78 | */
|
|---|
| [241f1985] | 79 | errno_t rc = loader_spawn("loader");
|
|---|
| [102f641] | 80 |
|
|---|
| [0a8f070] | 81 | if (rc != EOK) {
|
|---|
| [241f1985] | 82 | async_answer_0(icall, rc);
|
|---|
| [0a8f070] | 83 | return;
|
|---|
| 84 | }
|
|---|
| [102f641] | 85 |
|
|---|
| [0a8f070] | 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);
|
|---|
| [241f1985] | 92 | rc = async_forward_1(icall, exch,
|
|---|
| 93 | ipc_get_arg2(icall),
|
|---|
| 94 | ipc_get_arg3(icall),
|
|---|
| 95 | IPC_FF_NONE);
|
|---|
| [0a8f070] | 96 | async_exchange_end(exch);
|
|---|
| 97 |
|
|---|
| [012dd8e] | 98 | /* After forward we can dispose all session-related resources */
|
|---|
| [2f44fafd] | 99 | async_hangup(sess_ref->sess);
|
|---|
| [0a8f070] | 100 | free(sess_ref);
|
|---|
| 101 |
|
|---|
| 102 | if (rc != EOK) {
|
|---|
| [241f1985] | 103 | async_answer_0(icall, rc);
|
|---|
| [0a8f070] | 104 | return;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /* Everything OK. */
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| [241f1985] | 110 | static void connect_to_ns(ipc_call_t *icall)
|
|---|
| [0a8f070] | 111 | {
|
|---|
| [66b1075] | 112 | DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
|
|---|
| [012dd8e] | 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);
|
|---|
| [241f1985] | 123 | errno_t rc = async_forward_0(icall, exch, 0, IPC_FF_NONE);
|
|---|
| [0a8f070] | 124 | async_exchange_end(exch);
|
|---|
| 125 |
|
|---|
| [16d748ee] | 126 | if (rc != EOK)
|
|---|
| [241f1985] | 127 | async_answer_0(icall, rc);
|
|---|
| [0a8f070] | 128 | }
|
|---|
| 129 |
|
|---|
| [241f1985] | 130 | static void taskman_new_task(ipc_call_t *icall)
|
|---|
| [012dd8e] | 131 | {
|
|---|
| [66b1075] | 132 | DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
|
|---|
| [241f1985] | 133 | errno_t rc = task_intro(icall->task_id);
|
|---|
| [16d748ee] | 134 | if (rc == EOK) {
|
|---|
| 135 | async_accept_0(icall);
|
|---|
| 136 | } else {
|
|---|
| 137 | async_answer_0(icall, rc);
|
|---|
| 138 | }
|
|---|
| [012dd8e] | 139 | }
|
|---|
| 140 |
|
|---|
| [241f1985] | 141 | static void taskman_i_am_ns(ipc_call_t *icall)
|
|---|
| [012dd8e] | 142 | {
|
|---|
| [66b1075] | 143 | DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
|
|---|
| [241f1985] | 144 | errno_t rc = EOK;
|
|---|
| [012dd8e] | 145 |
|
|---|
| 146 | fibril_mutex_lock(&session_ns_mtx);
|
|---|
| 147 | if (session_ns != NULL) {
|
|---|
| [241f1985] | 148 | rc = EEXIST;
|
|---|
| [012dd8e] | 149 | goto finish;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* Used only for connection forwarding -- atomic */
|
|---|
| 153 | session_ns = async_callback_receive(EXCHANGE_ATOMIC);
|
|---|
| [102f641] | 154 |
|
|---|
| [012dd8e] | 155 | if (session_ns == NULL) {
|
|---|
| 156 | rc = ENOENT;
|
|---|
| 157 | printf("%s: Cannot connect to NS\n", NAME);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| [918ac9b] | 160 | fibril_condvar_broadcast(&session_ns_cv);
|
|---|
| [012dd8e] | 161 | finish:
|
|---|
| 162 | fibril_mutex_unlock(&session_ns_mtx);
|
|---|
| [16d748ee] | 163 |
|
|---|
| 164 | if (rc == EOK) {
|
|---|
| 165 | async_accept_0(icall);
|
|---|
| 166 | } else {
|
|---|
| 167 | async_answer_0(icall, rc);
|
|---|
| 168 | }
|
|---|
| [012dd8e] | 169 | }
|
|---|
| 170 |
|
|---|
| [241f1985] | 171 | static void taskman_ctl_wait(ipc_call_t *icall)
|
|---|
| [1be7bee] | 172 | {
|
|---|
| 173 | task_id_t id = (task_id_t)
|
|---|
| [241f1985] | 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;
|
|---|
| [1be7bee] | 177 |
|
|---|
| [241f1985] | 178 | wait_for_task(id, flags, icall, waiter_id);
|
|---|
| [1be7bee] | 179 | }
|
|---|
| 180 |
|
|---|
| [241f1985] | 181 | static void taskman_ctl_retval(ipc_call_t *icall)
|
|---|
| [1be7bee] | 182 | {
|
|---|
| [241f1985] | 183 | task_id_t sender = icall->task_id;
|
|---|
| 184 | int retval = ipc_get_arg1(icall);
|
|---|
| 185 | bool wait_for_exit = ipc_get_arg2(icall);
|
|---|
| [780c8ce] | 186 |
|
|---|
| [66b1075] | 187 | DPRINTF("%s:%d from %" PRIu64 "/%i\n", __func__, __LINE__, sender, retval);
|
|---|
| [012dd8e] | 188 |
|
|---|
| [241f1985] | 189 | errno_t rc = task_set_retval(sender, retval, wait_for_exit);
|
|---|
| 190 | async_answer_0(icall, rc);
|
|---|
| [1be7bee] | 191 | }
|
|---|
| 192 |
|
|---|
| [241f1985] | 193 | static void taskman_ctl_ev_callback(ipc_call_t *icall)
|
|---|
| [b8341bc] | 194 | {
|
|---|
| [66b1075] | 195 | DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
|
|---|
| [012dd8e] | 196 |
|
|---|
| [241f1985] | 197 | bool past_events = ipc_get_arg1(icall);
|
|---|
| [4ff66ae] | 198 |
|
|---|
| [035d7d8] | 199 | /* Atomic -- will be used for notifications only */
|
|---|
| 200 | async_sess_t *sess = async_callback_receive(EXCHANGE_ATOMIC);
|
|---|
| 201 | if (sess == NULL) {
|
|---|
| [241f1985] | 202 | async_answer_0(icall, ENOMEM);
|
|---|
| [035d7d8] | 203 | return;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| [241f1985] | 206 | event_register_listener(icall->task_id, past_events, sess, icall);
|
|---|
| [4667b5c] | 207 | }
|
|---|
| 208 |
|
|---|
| [241f1985] | 209 | static void task_exit_event(ipc_call_t *icall, void *arg)
|
|---|
| [62273d1] | 210 | {
|
|---|
| [241f1985] | 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);
|
|---|
| [66b1075] | 213 | DPRINTF("%s:%d from %" PRIu64 "/%i\n", __func__, __LINE__, id, exit_reason);
|
|---|
| [5cd2290] | 214 | task_terminated(id, exit_reason);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| [241f1985] | 217 | static void task_fault_event(ipc_call_t *icall, void *arg)
|
|---|
| [5cd2290] | 218 | {
|
|---|
| [241f1985] | 219 | task_id_t id = MERGE_LOUP32(ipc_get_arg1(icall), ipc_get_arg2(icall));
|
|---|
| [66b1075] | 220 | DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, id);
|
|---|
| [5cd2290] | 221 | task_failed(id);
|
|---|
| [62273d1] | 222 | }
|
|---|
| 223 |
|
|---|
| [241f1985] | 224 | static void loader_callback(ipc_call_t *icall)
|
|---|
| [0a8f070] | 225 | {
|
|---|
| [66b1075] | 226 | DPRINTF("%s:%d from %" PRIu64 "\n", __func__, __LINE__, icall->task_id);
|
|---|
| [0a8f070] | 227 | // TODO check that loader is expected, would probably discard prodcons
|
|---|
| 228 | // scheme
|
|---|
| [102f641] | 229 |
|
|---|
| [0a8f070] | 230 | /* Preallocate session container */
|
|---|
| 231 | sess_ref_t *sess_ref = malloc(sizeof(sess_ref_t));
|
|---|
| 232 | if (sess_ref == NULL) {
|
|---|
| [241f1985] | 233 | async_answer_0(icall, ENOMEM);
|
|---|
| [0a8f070] | 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /* Create callback connection */
|
|---|
| 237 | sess_ref->sess = async_callback_receive_start(EXCHANGE_ATOMIC, icall);
|
|---|
| 238 | if (sess_ref->sess == NULL) {
|
|---|
| [241f1985] | 239 | async_answer_0(icall, EINVAL);
|
|---|
| [0a8f070] | 240 | return;
|
|---|
| 241 | }
|
|---|
| [62273d1] | 242 |
|
|---|
| [241f1985] | 243 | async_answer_0(icall, EOK);
|
|---|
| [0a8f070] | 244 |
|
|---|
| [62273d1] | 245 | /* Notify spawners */
|
|---|
| [0a8f070] | 246 | link_initialize(&sess_ref->link);
|
|---|
| 247 | prodcons_produce(&sess_queue, &sess_ref->link);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| [16d748ee] | 250 | static void taskman_connection(ipc_call_t *icall, void *arg)
|
|---|
| [012dd8e] | 251 | {
|
|---|
| [16d748ee] | 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;
|
|---|
| [012dd8e] | 258 | case TASKMAN_CONNECT_TO_NS:
|
|---|
| [241f1985] | 259 | connect_to_ns(icall);
|
|---|
| [16d748ee] | 260 | return;
|
|---|
| [012dd8e] | 261 | case TASKMAN_CONNECT_TO_LOADER:
|
|---|
| [241f1985] | 262 | connect_to_loader(icall);
|
|---|
| [16d748ee] | 263 | return;
|
|---|
| 264 | default:
|
|---|
| [66b1075] | 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));
|
|---|
| [16d748ee] | 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)) {
|
|---|
| [012dd8e] | 274 | case TASKMAN_LOADER_CALLBACK:
|
|---|
| [241f1985] | 275 | loader_callback(icall);
|
|---|
| [16d748ee] | 276 | return;
|
|---|
| [012dd8e] | 277 | default:
|
|---|
| [66b1075] | 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));
|
|---|
| [16d748ee] | 282 | async_answer_0(icall, ENOTSUP);
|
|---|
| 283 | return;
|
|---|
| [012dd8e] | 284 | }
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| [16d748ee] | 287 | /* handle accepted calls */
|
|---|
| [012dd8e] | 288 | while (true) {
|
|---|
| 289 | ipc_call_t call;
|
|---|
| 290 |
|
|---|
| [16d748ee] | 291 | if (!async_get_call(&call)) {
|
|---|
| [012dd8e] | 292 | /* Client disconnected */
|
|---|
| [66b1075] | 293 | DPRINTF("%s:%d client disconnected\n", __func__, __LINE__);
|
|---|
| [16d748ee] | 294 | return;
|
|---|
| [012dd8e] | 295 | }
|
|---|
| 296 |
|
|---|
| [16d748ee] | 297 | switch (ipc_get_imethod(&call)) {
|
|---|
| 298 | case TASKMAN_I_AM_NS:
|
|---|
| 299 | taskman_i_am_ns(&call);
|
|---|
| [012dd8e] | 300 | break;
|
|---|
| [16d748ee] | 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:
|
|---|
| [66b1075] | 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));
|
|---|
| [16d748ee] | 315 | async_answer_0(&call, ENOTSUP);
|
|---|
| [012dd8e] | 316 | }
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| [0a8f070] | 320 | int main(int argc, char *argv[])
|
|---|
| 321 | {
|
|---|
| 322 | printf(NAME ": HelenOS task manager\n");
|
|---|
| 323 |
|
|---|
| [62273d1] | 324 | /* Initialization */
|
|---|
| [0a8f070] | 325 | prodcons_initialize(&sess_queue);
|
|---|
| [241f1985] | 326 | errno_t rc = tasks_init();
|
|---|
| [1be7bee] | 327 | if (rc != EOK) {
|
|---|
| 328 | return rc;
|
|---|
| 329 | }
|
|---|
| [035d7d8] | 330 | rc = event_init();
|
|---|
| 331 | if (rc != EOK) {
|
|---|
| 332 | return rc;
|
|---|
| 333 | }
|
|---|
| [0a8f070] | 334 |
|
|---|
| [5cd2290] | 335 | rc = async_event_subscribe(EVENT_EXIT, task_exit_event, NULL);
|
|---|
| [62273d1] | 336 | if (rc != EOK) {
|
|---|
| [012dd8e] | 337 | printf(NAME ": Cannot register for exit events (%i).\n", rc);
|
|---|
| [62273d1] | 338 | return rc;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| [5cd2290] | 341 | rc = async_event_subscribe(EVENT_FAULT, task_fault_event, NULL);
|
|---|
| [62273d1] | 342 | if (rc != EOK) {
|
|---|
| [012dd8e] | 343 | printf(NAME ": Cannot register for fault events (%i).\n", rc);
|
|---|
| [62273d1] | 344 | return rc;
|
|---|
| 345 | }
|
|---|
| [102f641] | 346 |
|
|---|
| [012dd8e] | 347 | task_id_t self_id = task_get_id();
|
|---|
| 348 | rc = task_intro(self_id);
|
|---|
| [0a8f070] | 349 | if (rc != EOK) {
|
|---|
| [012dd8e] | 350 | printf(NAME ": Cannot register self as task (%i).\n", rc);
|
|---|
| [0a8f070] | 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /* Start sysman server */
|
|---|
| [241f1985] | 354 | async_set_fallback_port_handler(taskman_connection, NULL);
|
|---|
| [0a8f070] | 355 |
|
|---|
| 356 | printf(NAME ": Accepting connections\n");
|
|---|
| [012dd8e] | 357 | (void)task_set_retval(self_id, EOK, false);
|
|---|
| [0a8f070] | 358 | async_manager();
|
|---|
| 359 |
|
|---|
| 360 | /* not reached */
|
|---|
| 361 | return 0;
|
|---|
| 362 | }
|
|---|
| [780c8ce] | 363 |
|
|---|
| 364 | /**
|
|---|
| 365 | * @}
|
|---|
| 366 | */
|
|---|