[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] | 47 | unit_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] | 54 | static const char *section_name = "Unit";
|
---|
| 55 |
|
---|
| 56 | static config_item_t unit_configuration[] = {
|
---|
| 57 | {"After", &unit_parse_unit_list, 0, ""},
|
---|
| 58 | CONFIGURATION_ITEM_SENTINEL
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 |
|
---|
[f42ee6f] | 62 | static 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;
|
---|
| 70 | unit->state = STATE_EMBRYO;
|
---|
[694253c] | 71 |
|
---|
[dda2602] | 72 | link_initialize(&unit->units);
|
---|
| 73 | link_initialize(&unit->bfs_link);
|
---|
[9532981] | 74 | list_initialize(&unit->edges_in);
|
---|
| 75 | list_initialize(&unit->edges_out);
|
---|
[6efec7e3] | 76 |
|
---|
[bb154c6] | 77 | UNIT_VMT(unit)->init(unit);
|
---|
[f42ee6f] | 78 | }
|
---|
| 79 |
|
---|
| 80 | unit_t *unit_create(unit_type_t type)
|
---|
| 81 | {
|
---|
[bb154c6] | 82 | size_t size = unit_type_vmts[type]->size;
|
---|
| 83 | unit_t *unit = malloc(size);
|
---|
[f42ee6f] | 84 | if (unit != NULL) {
|
---|
| 85 | unit_init(unit, type);
|
---|
| 86 | }
|
---|
| 87 | return unit;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /** Release resources used by unit structure */
|
---|
[6efec7e3] | 91 | void unit_destroy(unit_t **unit_ptr)
|
---|
[f42ee6f] | 92 | {
|
---|
[6efec7e3] | 93 | unit_t *unit = *unit_ptr;
|
---|
| 94 | if (unit == NULL)
|
---|
[f42ee6f] | 95 | return;
|
---|
[6efec7e3] | 96 |
|
---|
[bb154c6] | 97 | UNIT_VMT(unit)->destroy(unit);
|
---|
[f42ee6f] | 98 | /* TODO:
|
---|
| 99 | * edges,
|
---|
| 100 | * check it's not an active unit,
|
---|
| 101 | * other resources to come
|
---|
| 102 | */
|
---|
[bb154c6] | 103 | free(unit->name);
|
---|
[6efec7e3] | 104 | free(unit);
|
---|
| 105 | unit_ptr = NULL;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[5559712] | 108 | int unit_load(unit_t *unit, ini_configuration_t *ini_conf,
|
---|
| 109 | text_parse_t *text_parse)
|
---|
| 110 | {
|
---|
| 111 | sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
|
---|
| 112 |
|
---|
| 113 | int rc = EOK;
|
---|
| 114 | ini_section_t *unit_section = ini_get_section(ini_conf, section_name);
|
---|
| 115 | if (unit_section) {
|
---|
| 116 | rc = config_load_ini_section(unit_configuration,
|
---|
| 117 | unit_section, unit, text_parse);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | if (rc != EOK) {
|
---|
| 121 | return rc;
|
---|
| 122 | } else {
|
---|
| 123 | return UNIT_VMT(unit)->load(unit, ini_conf, text_parse);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[694253c] | 126 |
|
---|
| 127 | /** Issue request to restarter to start a unit
|
---|
| 128 | *
|
---|
[3f7e1f24] | 129 | * Ideally this function is non-blocking synchronous, however, some units
|
---|
| 130 | * cannot be started synchronously and thus return from this function generally
|
---|
| 131 | * means that start was requested.
|
---|
| 132 | *
|
---|
| 133 | * Check state of the unit for actual result, start method can end in states:
|
---|
| 134 | * - STATE_STARTED, (succesful synchronous start)
|
---|
| 135 | * - STATE_STARTING, (succesful asynchronous start request)
|
---|
[5559712] | 136 | * - STATE_FAILED. (unit state changed and error occured)
|
---|
[694253c] | 137 | */
|
---|
| 138 | int unit_start(unit_t *unit)
|
---|
| 139 | {
|
---|
[bb154c6] | 140 | sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
|
---|
| 141 | return UNIT_VMT(unit)->start(unit);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[5559712] | 144 | void unit_exposee_created(unit_t *unit)
|
---|
[bb154c6] | 145 | {
|
---|
| 146 | sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
|
---|
[5559712] | 147 | return UNIT_VMT(unit)->exposee_created(unit);
|
---|
| 148 | }
|
---|
[bb154c6] | 149 |
|
---|
[5559712] | 150 | void unit_fail(unit_t *unit)
|
---|
| 151 | {
|
---|
| 152 | sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
|
---|
| 153 | return UNIT_VMT(unit)->fail(unit);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | void unit_notify_state(unit_t *unit)
|
---|
| 157 | {
|
---|
| 158 | sysman_raise_event(&sysman_event_unit_state_changed, unit);
|
---|
[bb154c6] | 159 | }
|
---|
| 160 |
|
---|
[9532981] | 161 | // TODO move to libsysman
|
---|
[bb154c6] | 162 | unit_type_t unit_type_name_to_type(const char *type_name)
|
---|
| 163 | {
|
---|
[5559712] | 164 | if (str_cmp(type_name, UNIT_CFG_TYPE_NAME) == 0)
|
---|
[bb154c6] | 165 | return UNIT_CONFIGURATION;
|
---|
| 166 |
|
---|
[5559712] | 167 | else if (str_cmp(type_name, UNIT_MNT_TYPE_NAME) == 0)
|
---|
[bb154c6] | 168 | return UNIT_MOUNT;
|
---|
| 169 |
|
---|
[5559712] | 170 | else if (str_cmp(type_name, UNIT_TGT_TYPE_NAME) == 0)
|
---|
[bb154c6] | 171 | return UNIT_TARGET;
|
---|
| 172 |
|
---|
[5559712] | 173 | else if (str_cmp(type_name, UNIT_SVC_TYPE_NAME) == 0)
|
---|
| 174 | return UNIT_SERVICE;
|
---|
| 175 |
|
---|
[bb154c6] | 176 | else
|
---|
| 177 | return UNIT_TYPE_INVALID;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /** Format unit name to be presented to user */
|
---|
| 181 | const char *unit_name(const unit_t *unit)
|
---|
| 182 | {
|
---|
| 183 | return unit->name ? unit->name : "";
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[dda2602] | 186 | bool unit_parse_unit_list(const char *string, void *dst, text_parse_t *parse,
|
---|
[bb154c6] | 187 | size_t lineno)
|
---|
| 188 | {
|
---|
| 189 | unit_t *unit = dst;
|
---|
| 190 | bool result;
|
---|
[dda2602] | 191 | char *my_string = str_dup(string);
|
---|
[bb154c6] | 192 |
|
---|
[dda2602] | 193 | if (!my_string) {
|
---|
[bb154c6] | 194 | result = false;
|
---|
| 195 | goto finish;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[dda2602] | 198 | char *to_split = my_string;
|
---|
[bb154c6] | 199 | char *cur_tok;
|
---|
| 200 |
|
---|
| 201 | while ((cur_tok = str_tok(to_split, " ", &to_split))) {
|
---|
[9532981] | 202 | if (edge_sprout_out(unit, cur_tok) != EOK) {
|
---|
[bb154c6] | 203 | result = false;
|
---|
| 204 | goto finish;
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | result = true;
|
---|
| 209 |
|
---|
| 210 | finish:
|
---|
[dda2602] | 211 | free(my_string);
|
---|
[bb154c6] | 212 | return result;
|
---|
[694253c] | 213 | }
|
---|