eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

module.c (4302B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <errno.h>
     28 #include <ell/ell.h>
     29 
     30 #include "src/module.h"
     31 
     32 struct dependency {
     33 	struct dependency *next;
     34 	struct module *module;
     35 };
     36 
     37 struct module {
     38 	struct iwd_module_desc *desc;
     39 	struct dependency *depends;
     40 	bool visited : 1;
     41 	bool processed : 1;
     42 };
     43 
     44 extern struct iwd_module_desc __start___iwd_module[];
     45 extern struct iwd_module_desc __stop___iwd_module[];
     46 extern struct iwd_module_depends __start___iwd_module_dep[];
     47 extern struct iwd_module_depends __stop___iwd_module_dep[];
     48 
     49 static struct iwd_module_desc **modules_sorted;
     50 
     51 static struct module *module_find(struct module *modules, size_t count,
     52 						const char *name)
     53 {
     54 	unsigned int i;
     55 
     56 	for (i = 0; i < count; i++)
     57 		if (!strcmp(modules[i].desc->name, name))
     58 			return &modules[i];
     59 
     60 	return NULL;
     61 }
     62 
     63 static int module_topological_order(struct module *module,
     64 					struct iwd_module_desc **sorted,
     65 					size_t *offset)
     66 {
     67 	struct dependency *d;
     68 	int r;
     69 
     70 	module->visited = true;
     71 
     72 	for (d = module->depends; d; d = d->next) {
     73 		if (d->module->processed)
     74 			continue;
     75 
     76 		if (d->module->visited) {
     77 			l_error("Circular dependency between %s and %s",
     78 					module->desc->name,
     79 					d->module->desc->name);
     80 			return -EINVAL;
     81 		}
     82 
     83 		r = module_topological_order(d->module, sorted, offset);
     84 		if (r < 0)
     85 			return r;
     86 	}
     87 
     88 	module->processed = true;
     89 	sorted[*offset] = module->desc;
     90 	*offset += 1;
     91 	return 0;
     92 }
     93 
     94 int iwd_modules_init(void)
     95 {
     96 	struct iwd_module_desc *desc;
     97 	struct iwd_module_depends *dep;
     98 	L_AUTO_FREE_VAR(struct module *, modules) = NULL;
     99 	L_AUTO_FREE_VAR(struct dependency *, deps) = NULL;
    100 	L_AUTO_FREE_VAR(struct iwd_module_desc **, sorted) = NULL;
    101 	unsigned int i = 0;
    102 	size_t n_modules;
    103 	size_t n_deps;
    104 	size_t offset;
    105 	int r;
    106 
    107 	l_debug("");
    108 
    109 	n_modules = (__stop___iwd_module - __start___iwd_module);
    110 	modules = l_new(struct module, n_modules);
    111 
    112 	for (desc = __start___iwd_module; desc < __stop___iwd_module; desc++)
    113 		modules[i++].desc = desc;
    114 
    115 	n_deps = (__stop___iwd_module_dep - __start___iwd_module_dep);
    116 	deps = l_new(struct dependency, n_deps);
    117 
    118 	for (dep = __start___iwd_module_dep, i = 0;
    119 				dep < __stop___iwd_module_dep; dep++, i++) {
    120 		struct module *src;
    121 		struct module *dst;
    122 
    123 		src = module_find(modules, n_modules, dep->self);
    124 		dst = module_find(modules, n_modules, dep->target);
    125 		if (!src || !dst) {
    126 			l_error("Module dependency %s->%s not found",
    127 					dep->self, dep->target);
    128 			return -EINVAL;
    129 		}
    130 
    131 		deps[i].next = src->depends;
    132 		deps[i].module = dst;
    133 		src->depends = &deps[i];
    134 	}
    135 
    136 	sorted = l_new(struct iwd_module_desc *, n_modules);
    137 
    138 	for (i = 0, offset = 0; i < n_modules; i++) {
    139 		if (modules[i].processed)
    140 			continue;
    141 
    142 		if (module_topological_order(&modules[i], sorted,
    143 							&offset) < 0)
    144 			return -EINVAL;
    145 	}
    146 
    147 	modules_sorted = sorted;
    148 	sorted = NULL;
    149 
    150 	for (i = 0; i < n_modules; i++) {
    151 		desc = modules_sorted[i];
    152 		r = desc->init();
    153 
    154 		if (r < 0) {
    155 			l_error("Module %s failed to start: %d", desc->name, r);
    156 			return r;
    157 		}
    158 
    159 		desc->active = true;
    160 	}
    161 
    162 	return 0;
    163 }
    164 
    165 void iwd_modules_exit(void)
    166 {
    167 	struct iwd_module_desc *desc;
    168 	unsigned int i;
    169 	size_t n_modules = (__stop___iwd_module - __start___iwd_module);
    170 
    171 	l_debug("");
    172 
    173 	if (!modules_sorted)
    174 		return;
    175 
    176 	for (i = 0; i < n_modules; i++) {
    177 		desc = modules_sorted[n_modules - 1 - i];
    178 		if (!desc->active)
    179 			continue;
    180 
    181 		desc->exit();
    182 		desc->active = false;
    183 	}
    184 
    185 	l_free(modules_sorted);
    186 	modules_sorted = NULL;
    187 }