[e8747bd8] | 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/list.h>
|
---|
[8ae8262] | 30 | #include <errno.h>
|
---|
[e8747bd8] | 31 | #include <stdlib.h>
|
---|
| 32 | #include <task.h>
|
---|
[8ae8262] | 33 | #include <sysman/unit.h>
|
---|
[e8747bd8] | 34 |
|
---|
[af92309] | 35 | #include "repo.h"
|
---|
[e8747bd8] | 36 | #include "log.h"
|
---|
| 37 | #include "sysman.h"
|
---|
| 38 | #include "sm_task.h"
|
---|
| 39 |
|
---|
[8ae8262] | 40 |
|
---|
[e8747bd8] | 41 | /** Structure for boxing task event */
|
---|
| 42 | struct sm_task_event {
|
---|
| 43 | task_id_t task_id;
|
---|
| 44 | int flags;
|
---|
| 45 | task_exit_t texit;
|
---|
| 46 | int retval;
|
---|
| 47 | };
|
---|
| 48 |
|
---|
| 49 | static void sysman_event_task_event(void *);
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * @note This function runs in separate fibril (not same as event loop).
|
---|
| 53 | */
|
---|
| 54 | static void sm_task_event_handler(task_id_t tid, int flags, task_exit_t texit,
|
---|
| 55 | int retval)
|
---|
| 56 | {
|
---|
| 57 | sm_task_event_t *tev = malloc(sizeof(sm_task_event_t));
|
---|
| 58 | if (tev == NULL) {
|
---|
| 59 | sysman_log(LVL_FATAL,
|
---|
| 60 | "Unable to process event of task %" PRIu64 ".", tid);
|
---|
| 61 | return;
|
---|
| 62 | }
|
---|
| 63 | tev->task_id = tid;
|
---|
| 64 | tev->flags = flags;
|
---|
| 65 | tev->texit = texit;
|
---|
| 66 | tev->retval = retval;
|
---|
| 67 |
|
---|
| 68 | sysman_raise_event(&sysman_event_task_event, tev);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | static unit_svc_t *sm_task_find_service(task_id_t tid)
|
---|
| 72 | {
|
---|
| 73 | /*
|
---|
| 74 | * Unit to task is about to be developed, so use plain linear search
|
---|
| 75 | * instead of specialized structures.
|
---|
| 76 | */
|
---|
[015b147] | 77 | repo_foreach_t(UNIT_SERVICE, u) {
|
---|
[e8747bd8] | 78 | if (CAST_SVC(u)->main_task_id == tid) {
|
---|
| 79 | return CAST_SVC(u);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return NULL;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[8ae8262] | 87 | static unit_svc_t *sm_task_create_service(task_id_t tid)
|
---|
| 88 | {
|
---|
| 89 | unit_t *u_svc = unit_create(UNIT_SERVICE);
|
---|
| 90 | bool in_repo_update = false;
|
---|
| 91 | int rc = EOK;
|
---|
| 92 |
|
---|
| 93 | if (u_svc == NULL) {
|
---|
| 94 | goto fail;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | rc = asprintf(&u_svc->name, ANONYMOUS_SERVICE_MASK "%c%s", tid,
|
---|
| 98 | UNIT_NAME_SEPARATOR, UNIT_SVC_TYPE_NAME);
|
---|
| 99 | if (rc < 0) {
|
---|
| 100 | goto fail;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | CAST_SVC(u_svc)->main_task_id = tid;
|
---|
| 104 | CAST_SVC(u_svc)->anonymous = true;
|
---|
| 105 | /* exec_start is left undefined, maybe could be hinted by kernel's task
|
---|
| 106 | * name */
|
---|
| 107 |
|
---|
[8d74fdd] | 108 | /*
|
---|
| 109 | * Temporary workaround to avoid killing ourselves during shutdown,
|
---|
| 110 | * eventually should be captured by dependencies.
|
---|
| 111 | */
|
---|
| 112 | if (tid == task_get_id() || tid == 2 /*taskman*/) {
|
---|
| 113 | CAST_SVC(u_svc)->critical = true;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[8ae8262] | 116 | repo_begin_update();
|
---|
| 117 | in_repo_update = true;
|
---|
| 118 |
|
---|
| 119 | rc = repo_add_unit(u_svc);
|
---|
| 120 | if (rc != EOK) {
|
---|
| 121 | goto fail;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | repo_commit();
|
---|
| 125 |
|
---|
| 126 | return CAST_SVC(u_svc);
|
---|
| 127 |
|
---|
| 128 | fail:
|
---|
| 129 | if (in_repo_update) {
|
---|
| 130 | repo_rollback();
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | unit_destroy(&u_svc);
|
---|
| 134 | return NULL;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | static void sm_task_delete_service(unit_svc_t *u_svc)
|
---|
| 138 | {
|
---|
| 139 | repo_begin_update();
|
---|
| 140 | int rc = repo_remove_unit(&u_svc->unit);
|
---|
| 141 | if (rc != EOK) {
|
---|
| 142 | sysman_log(LVL_WARN, "Can't remove unit %s (%i).",
|
---|
| 143 | unit_name(&u_svc->unit), rc);
|
---|
| 144 | repo_rollback();
|
---|
| 145 | return;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | repo_commit();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[e8747bd8] | 151 | static void sysman_event_task_event(void *data)
|
---|
| 152 | {
|
---|
| 153 | sm_task_event_t *tev = data;
|
---|
| 154 |
|
---|
[b55f62a] | 155 | sysman_log(LVL_DEBUG2, "%s, %" PRIu64 " %i",
|
---|
| 156 | __func__, tev->task_id, tev->flags);
|
---|
[e8747bd8] | 157 | unit_svc_t *u_svc = sm_task_find_service(tev->task_id);
|
---|
[8ae8262] | 158 |
|
---|
[e8747bd8] | 159 | if (u_svc == NULL) {
|
---|
[8ae8262] | 160 | if (tev->flags & TASK_WAIT_EXIT) {
|
---|
| 161 | /* Non-service task exited, ignore. */
|
---|
| 162 | goto finish;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | u_svc = sm_task_create_service(tev->task_id);
|
---|
| 166 | if (u_svc == NULL) {
|
---|
| 167 | sysman_log(LVL_WARN,
|
---|
| 168 | "Unable to create anonymous service for task %" PRIu64 ".",
|
---|
| 169 | tev->task_id);
|
---|
| 170 | goto finish;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | sysman_log(LVL_DEBUG, "Created anonymous service %s.",
|
---|
| 174 | unit_name(&u_svc->unit));
|
---|
| 175 |
|
---|
| 176 | /* Inject state so that further processing makes sense */
|
---|
| 177 | u_svc->unit.state = STATE_STARTING;
|
---|
[e8747bd8] | 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | /* Simple incomplete state automaton */
|
---|
| 182 | unit_t *u = &u_svc->unit;
|
---|
| 183 | sysman_log(LVL_DEBUG2, "%s, %s(%i)@%" PRIu64 " %i",
|
---|
| 184 | __func__, unit_name(u), u->state, tev->task_id, tev->flags);
|
---|
| 185 |
|
---|
| 186 | if (tev->flags & TASK_WAIT_EXIT) {
|
---|
| 187 | // TODO maybe call unit_fail (would be nice to contain reason)
|
---|
[ed5367b] | 188 | // or move this whole logic to unit_svc.c
|
---|
| 189 | if (u->state == STATE_STOPPING) {
|
---|
| 190 | u->state = STATE_STOPPED;
|
---|
| 191 | } else {
|
---|
| 192 | // if it has also retval == 0 then it's not fail
|
---|
| 193 | u->state = STATE_FAILED;
|
---|
| 194 | }
|
---|
[8ae8262] | 195 |
|
---|
[c7b9db03] | 196 | } else if (tev->flags & TASK_WAIT_RETVAL) {
|
---|
[ed5367b] | 197 | assert(u->state == STATE_STARTING);
|
---|
[e8747bd8] | 198 | u->state = STATE_STARTED;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | unit_notify_state(u);
|
---|
| 202 |
|
---|
[8ae8262] | 203 | if ((tev->flags & TASK_WAIT_EXIT) && u_svc->anonymous) {
|
---|
| 204 | sysman_log(LVL_DEBUG, "Deleted anonymous service %s.",
|
---|
| 205 | unit_name(&u_svc->unit));
|
---|
| 206 | sm_task_delete_service(u_svc);
|
---|
| 207 | }
|
---|
[e8747bd8] | 208 | finish:
|
---|
| 209 | free(tev);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[4ff66ae] | 212 | int sm_task_start(void)
|
---|
[e8747bd8] | 213 | {
|
---|
[4ff66ae] | 214 | int rc = task_register_event_handler(&sm_task_event_handler, true);
|
---|
[e8747bd8] | 215 | return rc;
|
---|
| 216 | }
|
---|