source: mainline/uspace/srv/sysman/repo.c@ af92309

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

sysman: Rename configuration to repo(sitory)

It's better suited for dynamic nature of the storage.

  • Property mode set to 100644
File size: 7.5 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
[bb154c6]29#include <adt/hash.h>
30#include <adt/hash_table.h>
[f42ee6f]31#include <adt/list.h>
32#include <assert.h>
33#include <errno.h>
34#include <fibril_synch.h>
35
[af92309]36#include "repo.h"
[bb154c6]37#include "dep.h"
[6efec7e3]38#include "log.h"
[f42ee6f]39
[e8747bd8]40// TODO rename to repository (dynamic nature of units storage, and do not name it godlike Manager :-)
41
[dda2602]42LIST_INITIALIZE(units);
43
44static hash_table_t units_by_name;
[b55f62a]45static hash_table_t units_by_handle;
[bb154c6]46
47/* Hash table functions */
[b55f62a]48static size_t units_by_handle_ht_hash(const ht_link_t *item)
49{
50 unit_t *unit =
51 hash_table_get_inst(item, unit_t, units_by_handle);
52 return unit->handle;
53}
54
55static size_t units_by_handle_ht_key_hash(void *key)
56{
57 return *(unit_handle_t *)key;
58}
59
60static bool units_by_handle_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
61{
62 return
63 hash_table_get_inst(item1, unit_t, units_by_handle) ==
64 hash_table_get_inst(item2, unit_t, units_by_handle);
65}
66
67static bool units_by_handle_ht_key_equal(void *key, const ht_link_t *item)
68{
69 return *(unit_handle_t *)key ==
70 hash_table_get_inst(item, unit_t, units_by_handle)->handle;
71}
72
73static hash_table_ops_t units_by_handle_ht_ops = {
74 .hash = &units_by_handle_ht_hash,
75 .key_hash = &units_by_handle_ht_key_hash,
76 .equal = &units_by_handle_ht_equal,
77 .key_equal = &units_by_handle_ht_key_equal,
78 .remove_callback = NULL // TODO realy unneeded?
79};
80
[dda2602]81static size_t units_by_name_ht_hash(const ht_link_t *item)
[bb154c6]82{
83 unit_t *unit =
[dda2602]84 hash_table_get_inst(item, unit_t, units_by_name);
[bb154c6]85 return hash_string(unit->name);
86}
87
[dda2602]88static size_t units_by_name_ht_key_hash(void *key)
[bb154c6]89{
90 return hash_string((const char *)key);
91}
92
[dda2602]93static bool units_by_name_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
[bb154c6]94{
[b55f62a]95 return
96 hash_table_get_inst(item1, unit_t, units_by_handle) ==
97 hash_table_get_inst(item2, unit_t, units_by_handle);
[bb154c6]98}
99
[dda2602]100static bool units_by_name_ht_key_equal(void *key, const ht_link_t *item)
[bb154c6]101{
102 return str_cmp((const char *)key,
[dda2602]103 hash_table_get_inst(item, unit_t, units_by_name)->name) == 0;
[bb154c6]104}
105
106
[dda2602]107static hash_table_ops_t units_by_name_ht_ops = {
108 .hash = &units_by_name_ht_hash,
109 .key_hash = &units_by_name_ht_key_hash,
110 .equal = &units_by_name_ht_equal,
111 .key_equal = &units_by_name_ht_key_equal,
[bb154c6]112 .remove_callback = NULL // TODO realy unneeded?
113};
114
[af92309]115/* Repository functions */
[f42ee6f]116
[af92309]117void repo_init(void)
[f42ee6f]118{
[dda2602]119 hash_table_create(&units_by_name, 0, 0, &units_by_name_ht_ops);
[b55f62a]120 hash_table_create(&units_by_handle, 0, 0, &units_by_handle_ht_ops);
[f42ee6f]121}
122
[af92309]123int repo_add_unit(unit_t *unit)
[f42ee6f]124{
125 assert(unit);
126 assert(unit->state == STATE_EMBRYO);
[b55f62a]127 assert(unit->handle == 0);
[bb154c6]128 assert(unit->name != NULL);
129 sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
[f42ee6f]130
[dda2602]131 if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
[b55f62a]132 /* Pointers are same size as unit_handle_t both on 32b and 64b */
133 unit->handle = (unit_handle_t)unit;
134
135 hash_table_insert(&units_by_handle, &unit->units_by_handle);
[dda2602]136 list_append(&unit->units, &units);
[bb154c6]137 return EOK;
138 } else {
139 return EEXISTS;
140 }
141}
142
[af92309]143void repo_begin_update(void) {
[bb154c6]144 sysman_log(LVL_DEBUG2, "%s", __func__);
145}
146
[af92309]147static bool repo_commit_unit(ht_link_t *ht_link, void *arg)
[bb154c6]148{
[dda2602]149 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]150 // TODO state locking?
151 if (unit->state == STATE_EMBRYO) {
152 unit->state = STATE_STOPPED;
153 }
154
155 list_foreach(unit->dependencies, dependencies, unit_dependency_t, dep) {
156 if (dep->state == DEP_EMBRYO) {
157 dep->state = DEP_VALID;
158 }
159 }
160 return true;
[f42ee6f]161}
162
[dda2602]163/** Marks newly added units_by_name as usable (via state change) */
[af92309]164void repo_commit(void)
[f42ee6f]165{
[6efec7e3]166 sysman_log(LVL_DEBUG2, "%s", __func__);
167
[bb154c6]168 /*
[dda2602]169 * Apply commit to all units_by_name, each commited unit commits its outgoing
[bb154c6]170 * deps, thus eventually commiting all embryo deps as well.
171 */
[af92309]172 hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
[bb154c6]173}
174
[af92309]175static bool repo_rollback_unit(ht_link_t *ht_link, void *arg)
[bb154c6]176{
[dda2602]177 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]178
179 list_foreach_safe(unit->dependencies, cur_link, next_link) {
180 unit_dependency_t *dep =
181 list_get_instance(cur_link, unit_dependency_t, dependencies);
182 if (dep->state == DEP_EMBRYO) {
183 dep_remove_dependency(&dep);
184 }
185 }
186
187 if (unit->state == STATE_EMBRYO) {
[dda2602]188 hash_table_remove_item(&units_by_name, ht_link);
189 list_remove(&unit->units);
[bb154c6]190 unit_destroy(&unit);
191 }
192
193 return true;
194}
195
[dda2602]196/** Remove all uncommited units_by_name and edges from configuratio
[5559712]197 *
198 * Memory used by removed object is released.
199 */
[af92309]200void repo_rollback(void)
[bb154c6]201{
202 sysman_log(LVL_DEBUG2, "%s", __func__);
203
[af92309]204 hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
[bb154c6]205}
206
[af92309]207static bool repo_resolve_unit(ht_link_t *ht_link, void *arg)
[bb154c6]208{
209 bool *has_error_ptr = arg;
[dda2602]210 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]211
212 list_foreach(unit->dependencies, dependencies, unit_dependency_t, dep) {
213 assert(dep->dependant == unit);
214 assert((dep->dependency != NULL) != (dep->dependency_name != NULL));
215 if (dep->dependency) {
216 continue;
217 }
218
219 unit_t *dependency =
[af92309]220 repo_find_unit_by_name(dep->dependency_name);
[bb154c6]221 if (dependency == NULL) {
222 sysman_log(LVL_ERROR,
223 "Cannot resolve dependency of '%s' to unit '%s'",
224 unit_name(unit), dep->dependency_name);
225 *has_error_ptr = true;
226 // TODO should we just leave the sprout untouched?
227 } else {
228 dep_resolve_dependency(dep, dependency);
[f42ee6f]229 }
230 }
231
[bb154c6]232 return true;
233}
234
[dda2602]235/** Resolve unresolved dependencies between any pair of units_by_name
[bb154c6]236 *
237 * @return EOK on success
[5559712]238 * @return ENOENT when one or more resolution fails, information is logged
[bb154c6]239 */
[af92309]240int repo_resolve_dependecies(void)
[bb154c6]241{
242 sysman_log(LVL_DEBUG2, "%s", __func__);
243
244 bool has_error = false;
[af92309]245 hash_table_apply(&units_by_name, &repo_resolve_unit, &has_error);
[bb154c6]246
247 return has_error ? ENOENT : EOK;
[f42ee6f]248}
[bb154c6]249
[af92309]250unit_t *repo_find_unit_by_name(const char *name)
[bb154c6]251{
[dda2602]252 ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
[bb154c6]253 if (ht_link != NULL) {
[dda2602]254 return hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]255 } else {
256 return NULL;
257 }
258}
259
[af92309]260unit_t *repo_find_unit_by_handle(unit_handle_t handle)
[b55f62a]261{
262 ht_link_t *ht_link = hash_table_find(&units_by_handle, &handle);
263 if (ht_link != NULL) {
264 return hash_table_get_inst(ht_link, unit_t, units_by_handle);
265 } else {
266 return NULL;
267 }
268}
269
Note: See TracBrowser for help on using the repository browser.