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
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 <adt/hash.h>
30#include <adt/hash_table.h>
31#include <adt/list.h>
32#include <assert.h>
33#include <errno.h>
34#include <fibril_synch.h>
35
36#include "repo.h"
37#include "dep.h"
38#include "log.h"
39
40// TODO rename to repository (dynamic nature of units storage, and do not name it godlike Manager :-)
41
42LIST_INITIALIZE(units);
43
44static hash_table_t units_by_name;
45static hash_table_t units_by_handle;
46
47/* Hash table functions */
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
81static size_t units_by_name_ht_hash(const ht_link_t *item)
82{
83 unit_t *unit =
84 hash_table_get_inst(item, unit_t, units_by_name);
85 return hash_string(unit->name);
86}
87
88static size_t units_by_name_ht_key_hash(void *key)
89{
90 return hash_string((const char *)key);
91}
92
93static bool units_by_name_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
94{
95 return
96 hash_table_get_inst(item1, unit_t, units_by_handle) ==
97 hash_table_get_inst(item2, unit_t, units_by_handle);
98}
99
100static bool units_by_name_ht_key_equal(void *key, const ht_link_t *item)
101{
102 return str_cmp((const char *)key,
103 hash_table_get_inst(item, unit_t, units_by_name)->name) == 0;
104}
105
106
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,
112 .remove_callback = NULL // TODO realy unneeded?
113};
114
115/* Repository functions */
116
117void repo_init(void)
118{
119 hash_table_create(&units_by_name, 0, 0, &units_by_name_ht_ops);
120 hash_table_create(&units_by_handle, 0, 0, &units_by_handle_ht_ops);
121}
122
123int repo_add_unit(unit_t *unit)
124{
125 assert(unit);
126 assert(unit->state == STATE_EMBRYO);
127 assert(unit->handle == 0);
128 assert(unit->name != NULL);
129 sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
130
131 if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
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);
136 list_append(&unit->units, &units);
137 return EOK;
138 } else {
139 return EEXISTS;
140 }
141}
142
143void repo_begin_update(void) {
144 sysman_log(LVL_DEBUG2, "%s", __func__);
145}
146
147static bool repo_commit_unit(ht_link_t *ht_link, void *arg)
148{
149 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
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;
161}
162
163/** Marks newly added units_by_name as usable (via state change) */
164void repo_commit(void)
165{
166 sysman_log(LVL_DEBUG2, "%s", __func__);
167
168 /*
169 * Apply commit to all units_by_name, each commited unit commits its outgoing
170 * deps, thus eventually commiting all embryo deps as well.
171 */
172 hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
173}
174
175static bool repo_rollback_unit(ht_link_t *ht_link, void *arg)
176{
177 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
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) {
188 hash_table_remove_item(&units_by_name, ht_link);
189 list_remove(&unit->units);
190 unit_destroy(&unit);
191 }
192
193 return true;
194}
195
196/** Remove all uncommited units_by_name and edges from configuratio
197 *
198 * Memory used by removed object is released.
199 */
200void repo_rollback(void)
201{
202 sysman_log(LVL_DEBUG2, "%s", __func__);
203
204 hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
205}
206
207static bool repo_resolve_unit(ht_link_t *ht_link, void *arg)
208{
209 bool *has_error_ptr = arg;
210 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
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 =
220 repo_find_unit_by_name(dep->dependency_name);
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);
229 }
230 }
231
232 return true;
233}
234
235/** Resolve unresolved dependencies between any pair of units_by_name
236 *
237 * @return EOK on success
238 * @return ENOENT when one or more resolution fails, information is logged
239 */
240int repo_resolve_dependecies(void)
241{
242 sysman_log(LVL_DEBUG2, "%s", __func__);
243
244 bool has_error = false;
245 hash_table_apply(&units_by_name, &repo_resolve_unit, &has_error);
246
247 return has_error ? ENOENT : EOK;
248}
249
250unit_t *repo_find_unit_by_name(const char *name)
251{
252 ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
253 if (ht_link != NULL) {
254 return hash_table_get_inst(ht_link, unit_t, units_by_name);
255 } else {
256 return NULL;
257 }
258}
259
260unit_t *repo_find_unit_by_handle(unit_handle_t handle)
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.