source: mainline/uspace/srv/sysman/sm_task.c@ 2df7d824

Last change on this file since 2df7d824 was e8747bd8, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

sysman: Use taskman API to detect successful server start

Conflicts:

uspace/srv/logger/main.c

  • Property mode set to 100644
File size: 3.3 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#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 */
39struct sm_task_event {
40 task_id_t task_id;
41 int flags;
42 task_exit_t texit;
43 int retval;
44};
45
46static void sysman_event_task_event(void *);
47
48/**
49 * @note This function runs in separate fibril (not same as event loop).
50 */
51static 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
68static 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
87static void sysman_event_task_event(void *data)
88{
89 sm_task_event_t *tev = data;
90
91 unit_svc_t *u_svc = sm_task_find_service(tev->task_id);
92 if (u_svc == NULL) {
93 goto finish;
94 }
95
96
97 /* Simple incomplete state automaton */
98 unit_t *u = &u_svc->unit;
99 sysman_log(LVL_DEBUG2, "%s, %s(%i)@%" PRIu64 " %i",
100 __func__, unit_name(u), u->state, tev->task_id, tev->flags);
101 assert(u->state == STATE_STARTING);
102
103 if (tev->flags & TASK_WAIT_EXIT) {
104 // TODO maybe call unit_fail (would be nice to contain reason)
105 u->state = STATE_FAILED;
106 } else {
107 u->state = STATE_STARTED;
108 }
109
110 unit_notify_state(u);
111
112finish:
113 free(tev);
114}
115
116int sm_task_init(void)
117{
118 int rc = task_register_event_handler(&sm_task_event_handler);
119
120 //TODO dump taskman info for boot time tasks
121 return rc;
122}
Note: See TracBrowser for help on using the repository browser.