source: mainline/uspace/srv/sysman/unit.c@ 01e68af

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

sysman: Remove stale TODOs

  • Property mode set to 100644
File size: 5.6 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 <assert.h>
30#include <errno.h>
31#include <fibril_synch.h>
32#include <conf/configuration.h>
33#include <conf/ini.h>
34#include <mem.h>
35#include <stddef.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <str.h>
39#include <sysman/unit.h>
40
41#include "edge.h"
42#include "log.h"
43#include "sysman.h"
44#include "unit.h"
45
46/** Virtual method table for each unit type */
47unit_vmt_t *unit_type_vmts[] = {
48 [UNIT_CONFIGURATION] = &unit_cfg_vmt,
49 [UNIT_MOUNT] = &unit_mnt_vmt,
50 [UNIT_TARGET] = &unit_tgt_vmt,
51 [UNIT_SERVICE] = &unit_svc_vmt
52};
53
54static const char *section_name = "Unit";
55
56static config_item_t unit_configuration[] = {
57 {"After", &unit_parse_unit_list, 0, ""},
58 CONFIGURATION_ITEM_SENTINEL
59};
60
61
62static void unit_init(unit_t *unit, unit_type_t type)
63{
64 assert(unit);
65
66 size_t size = unit_type_vmts[type]->size;
67 memset(unit, 0, size);
68
69 unit->type = type;
70 unit->state = STATE_STOPPED;
71 unit->repo_state = REPO_EMBRYO;
72
73 link_initialize(&unit->units);
74 link_initialize(&unit->bfs_link);
75 list_initialize(&unit->edges_in);
76 list_initialize(&unit->edges_out);
77
78 UNIT_VMT(unit)->init(unit);
79}
80
81unit_t *unit_create(unit_type_t type)
82{
83 size_t size = unit_type_vmts[type]->size;
84 unit_t *unit = malloc(size);
85 if (unit != NULL) {
86 unit_init(unit, type);
87 }
88 return unit;
89}
90
91/** Release resources used by unit structure */
92void unit_destroy(unit_t **unit_ptr)
93{
94 unit_t *unit = *unit_ptr;
95 if (unit == NULL)
96 return;
97
98 UNIT_VMT(unit)->destroy(unit);
99 /* TODO:
100 * edges
101 */
102 free(unit->name);
103 free(unit);
104 unit_ptr = NULL;
105}
106
107int unit_load(unit_t *unit, ini_configuration_t *ini_conf,
108 text_parse_t *text_parse)
109{
110 sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
111
112 int rc = EOK;
113 ini_section_t *unit_section = ini_get_section(ini_conf, section_name);
114 if (unit_section) {
115 rc = config_load_ini_section(unit_configuration,
116 unit_section, unit, text_parse);
117 }
118
119 if (rc != EOK) {
120 return rc;
121 } else {
122 return UNIT_VMT(unit)->load(unit, ini_conf, text_parse);
123 }
124}
125
126/** Issue request to restarter to start a unit
127 *
128 * Ideally this function is non-blocking synchronous, however, some units
129 * cannot be started synchronously and thus return from this function generally
130 * means that start was requested.
131 *
132 * Check state of the unit for actual result, start method can end in states:
133 * - STATE_STARTED, (succesful synchronous start)
134 * - STATE_STARTING, (succesful asynchronous start request)
135 * - STATE_FAILED. (unit state changed and error occured)
136 */
137int unit_start(unit_t *unit)
138{
139 sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
140 return UNIT_VMT(unit)->start(unit);
141}
142
143/** Issue request to restarter to stop a unit
144 *
145 * Same semantics like for unit_start applies.
146 */
147int unit_stop(unit_t *unit)
148{
149 sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
150 return UNIT_VMT(unit)->stop(unit);
151}
152
153void unit_exposee_created(unit_t *unit)
154{
155 sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
156 return UNIT_VMT(unit)->exposee_created(unit);
157}
158
159void unit_fail(unit_t *unit)
160{
161 sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
162 return UNIT_VMT(unit)->fail(unit);
163}
164
165void unit_notify_state(unit_t *unit)
166{
167 sysman_raise_event(&sysman_event_unit_state_changed, unit);
168}
169
170// TODO move to libsysman
171unit_type_t unit_type_name_to_type(const char *type_name)
172{
173 if (str_cmp(type_name, UNIT_CFG_TYPE_NAME) == 0)
174 return UNIT_CONFIGURATION;
175
176 else if (str_cmp(type_name, UNIT_MNT_TYPE_NAME) == 0)
177 return UNIT_MOUNT;
178
179 else if (str_cmp(type_name, UNIT_TGT_TYPE_NAME) == 0)
180 return UNIT_TARGET;
181
182 else if (str_cmp(type_name, UNIT_SVC_TYPE_NAME) == 0)
183 return UNIT_SERVICE;
184
185 else
186 return UNIT_TYPE_INVALID;
187}
188
189/** Format unit name to be presented to user */
190const char *unit_name(const unit_t *unit)
191{
192 return unit->name ? unit->name : "";
193}
194
195bool unit_parse_unit_list(const char *string, void *dst, text_parse_t *parse,
196 size_t lineno)
197{
198 unit_t *unit = dst;
199 bool result;
200 char *my_string = str_dup(string);
201
202 if (!my_string) {
203 result = false;
204 goto finish;
205 }
206
207 char *to_split = my_string;
208 char *cur_tok;
209
210 while ((cur_tok = str_tok(to_split, " ", &to_split))) {
211 if (edge_sprout_out(unit, cur_tok) != EOK) {
212 result = false;
213 goto finish;
214 }
215 }
216
217 result = true;
218
219finish:
220 free(my_string);
221 return result;
222}
Note: See TracBrowser for help on using the repository browser.