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>
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <task.h>
|
---|
32 |
|
---|
33 | #include "configuration.h"
|
---|
34 | #include "log.h"
|
---|
35 | #include "sysman.h"
|
---|
36 | #include "sm_task.h"
|
---|
37 |
|
---|
38 | /** Structure for boxing task event */
|
---|
39 | struct sm_task_event {
|
---|
40 | task_id_t task_id;
|
---|
41 | int flags;
|
---|
42 | task_exit_t texit;
|
---|
43 | int retval;
|
---|
44 | };
|
---|
45 |
|
---|
46 | static void sysman_event_task_event(void *);
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * @note This function runs in separate fibril (not same as event loop).
|
---|
50 | */
|
---|
51 | static void sm_task_event_handler(task_id_t tid, int flags, task_exit_t texit,
|
---|
52 | int retval)
|
---|
53 | {
|
---|
54 | sm_task_event_t *tev = malloc(sizeof(sm_task_event_t));
|
---|
55 | if (tev == NULL) {
|
---|
56 | sysman_log(LVL_FATAL,
|
---|
57 | "Unable to process event of task %" PRIu64 ".", tid);
|
---|
58 | return;
|
---|
59 | }
|
---|
60 | tev->task_id = tid;
|
---|
61 | tev->flags = flags;
|
---|
62 | tev->texit = texit;
|
---|
63 | tev->retval = retval;
|
---|
64 |
|
---|
65 | sysman_raise_event(&sysman_event_task_event, tev);
|
---|
66 | }
|
---|
67 |
|
---|
68 | static unit_svc_t *sm_task_find_service(task_id_t tid)
|
---|
69 | {
|
---|
70 | /*
|
---|
71 | * Unit to task is about to be developed, so use plain linear search
|
---|
72 | * instead of specialized structures.
|
---|
73 | */
|
---|
74 | list_foreach(units, units, unit_t, u) {
|
---|
75 | if (u->type != UNIT_SERVICE) {
|
---|
76 | continue;
|
---|
77 | }
|
---|
78 | if (CAST_SVC(u)->main_task_id == tid) {
|
---|
79 | return CAST_SVC(u);
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|
83 |
|
---|
84 | return NULL;
|
---|
85 | }
|
---|
86 |
|
---|
87 | static void sysman_event_task_event(void *data)
|
---|
88 | {
|
---|
89 | sm_task_event_t *tev = data;
|
---|
90 |
|
---|
91 | sysman_log(LVL_DEBUG2, "%s, %" PRIu64 " %i",
|
---|
92 | __func__, tev->task_id, tev->flags);
|
---|
93 | unit_svc_t *u_svc = sm_task_find_service(tev->task_id);
|
---|
94 | if (u_svc == NULL) {
|
---|
95 | goto finish;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /* Simple incomplete state automaton */
|
---|
100 | unit_t *u = &u_svc->unit;
|
---|
101 | sysman_log(LVL_DEBUG2, "%s, %s(%i)@%" PRIu64 " %i",
|
---|
102 | __func__, unit_name(u), u->state, tev->task_id, tev->flags);
|
---|
103 | assert(u->state == STATE_STARTING);
|
---|
104 |
|
---|
105 | if (tev->flags & TASK_WAIT_EXIT) {
|
---|
106 | // TODO maybe call unit_fail (would be nice to contain reason)
|
---|
107 | u->state = STATE_FAILED;
|
---|
108 | } else {
|
---|
109 | u->state = STATE_STARTED;
|
---|
110 | }
|
---|
111 |
|
---|
112 | unit_notify_state(u);
|
---|
113 |
|
---|
114 | finish:
|
---|
115 | free(tev);
|
---|
116 | }
|
---|
117 |
|
---|
118 | int sm_task_init(void)
|
---|
119 | {
|
---|
120 | int rc = task_register_event_handler(&sm_task_event_handler);
|
---|
121 |
|
---|
122 | //TODO dump taskman info for boot time tasks
|
---|
123 | return rc;
|
---|
124 | }
|
---|