Changeset bb154c6 in mainline for uspace/srv/sysman/unit.c
- Timestamp:
- 2019-08-03T08:15:25Z (5 years ago)
- Children:
- 09a8006
- Parents:
- 6006f35
- git-author:
- Michal Koutný <xm.koutny+hos@…> (2015-04-15 15:14:58)
- git-committer:
- Matthieu Riolo <matthieu.riolo@…> (2019-08-03 08:15:25)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/sysman/unit.c
r6006f35 rbb154c6 2 2 #include <errno.h> 3 3 #include <fibril_synch.h> 4 #include <conf/configuration.h> 5 #include <conf/ini.h> 4 6 #include <mem.h> 7 #include <stddef.h> 5 8 #include <stdio.h> 6 9 #include <stdlib.h> 10 #include <str.h> 7 11 12 #include "dep.h" 8 13 #include "log.h" 9 14 #include "unit.h" 10 15 11 16 /** Virtual method table for each unit type */ 12 static unit_ops_t *unit_type_vmts[] = {17 unit_vmt_t *unit_type_vmts[] = { 13 18 [UNIT_TARGET] = &unit_tgt_ops, 14 19 [UNIT_MOUNT] = &unit_mnt_ops, … … 16 21 }; 17 22 23 static const char *section_name = "Unit"; 24 25 static config_item_t unit_configuration[] = { 26 {"After", &unit_parse_unit_list, 0, ""}, 27 CONFIGURATION_ITEM_SENTINEL 28 }; 29 30 18 31 static void unit_init(unit_t *unit, unit_type_t type) 19 32 { 20 33 assert(unit); 21 34 35 // TODO is this necessary? 22 36 memset(unit, 0, sizeof(unit_t)); 23 link_initialize(&unit->units);24 37 25 38 unit->type = type; 39 unit->name = NULL; 40 26 41 unit->state = STATE_EMBRYO; 27 42 fibril_mutex_initialize(&unit->state_mtx); … … 31 46 list_initialize(&unit->dependencies); 32 47 33 unit_type_vmts[unit->type]->init(unit);48 UNIT_VMT(unit)->init(unit); 34 49 } 35 50 36 51 unit_t *unit_create(unit_type_t type) 37 52 { 38 unit_t *unit = malloc(sizeof(unit_t)); 53 size_t size = unit_type_vmts[type]->size; 54 unit_t *unit = malloc(size); 39 55 if (unit != NULL) { 40 56 unit_init(unit, type); … … 50 66 return; 51 67 52 unit_type_vmts[unit->type]->destroy(unit);68 UNIT_VMT(unit)->destroy(unit); 53 69 /* TODO: 54 70 * edges, … … 56 72 * other resources to come 57 73 */ 74 free(unit->name); 58 75 free(unit); 59 76 unit_ptr = NULL; … … 75 92 int unit_start(unit_t *unit) 76 93 { 77 sysman_log(LVL_DEBUG, "%s( %p)", __func__, unit);78 return unit_type_vmts[unit->type]->start(unit);94 sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit)); 95 return UNIT_VMT(unit)->start(unit); 79 96 } 97 98 int unit_load(unit_t *unit, ini_configuration_t *ini_conf, 99 text_parse_t *text_parse) 100 { 101 sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit)); 102 103 int rc = EOK; 104 ini_section_t *unit_section = ini_get_section(ini_conf, section_name); 105 if (unit_section) { 106 rc = config_load_ini_section(unit_configuration, 107 unit_section, unit, text_parse); 108 } 109 110 if (rc != EOK) { 111 return rc; 112 } else { 113 return UNIT_VMT(unit)->load(unit, ini_conf, text_parse); 114 } 115 } 116 117 unit_type_t unit_type_name_to_type(const char *type_name) 118 { 119 if (str_cmp(type_name, "cfg") == 0) 120 return UNIT_CONFIGURATION; 121 122 else if (str_cmp(type_name, "mnt") == 0) 123 return UNIT_MOUNT; 124 125 else if (str_cmp(type_name, "tgt") == 0) 126 return UNIT_TARGET; 127 128 else 129 return UNIT_TYPE_INVALID; 130 } 131 132 /** Format unit name to be presented to user */ 133 const char *unit_name(const unit_t *unit) 134 { 135 return unit->name ? unit->name : ""; 136 } 137 138 139 140 141 bool unit_parse_unit_list(const char *value, void *dst, text_parse_t *parse, 142 size_t lineno) 143 { 144 unit_t *unit = dst; 145 bool result; 146 char *my_value = str_dup(value); 147 148 if (!my_value) { 149 result = false; 150 goto finish; 151 } 152 153 char *to_split = my_value; 154 char *cur_tok; 155 156 while ((cur_tok = str_tok(to_split, " ", &to_split))) { 157 if (dep_sprout_dependency(unit, cur_tok) != EOK) { 158 result = false; 159 goto finish; 160 } 161 } 162 163 result = true; 164 165 finish: 166 free(my_value); 167 return result; 168 }
Note:
See TracChangeset
for help on using the changeset viewer.