source: mainline/uspace/srv/sysman/unit.c@ 24b30e1

Last change on this file since 24b30e1 was 8ae8262, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

sysman: Support for anonymous services

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