The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
module.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 39bfcc8cc6bd8af6df7f652d45f2ff938a14674e $
19 *
20 * @file src/lib/server/module.c
21 * @brief Defines functions for module initialisation
22 *
23 * @copyright 2016,2024 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 * @copyright 2003,2006,2016 The FreeRADIUS server project
25 * @copyright 2000 Alan DeKok (aland@freeradius.org)
26 * @copyright 2000 Alan Curry (pacman@world.std.com)
27 */
28
29RCSID("$Id: 39bfcc8cc6bd8af6df7f652d45f2ff938a14674e $")
30
31#include <freeradius-devel/server/base.h>
32#include <freeradius-devel/server/cf_file.h>
33#include <freeradius-devel/server/global_lib.h>
34#include <freeradius-devel/server/modpriv.h>
35#include <freeradius-devel/server/module_rlm.h>
36#include <freeradius-devel/server/radmin.h>
37#include <freeradius-devel/server/request_data.h>
38#include <freeradius-devel/server/module.h>
39#include <freeradius-devel/util/strerror.h>
40#include <freeradius-devel/util/talloc.h>
41#include <freeradius-devel/unlang/xlat_func.h>
42
43#include <talloc.h>
44#include <sys/mman.h>
45
47
48/** Heap of all lists/modules used to get a common index with mlg_thread->inst_list
49 */
51
52/** An array of thread-local module lists
53*
54* The indexes in this array are identical to module_list_global, allowing
55* O(1) lookups. Arrays are used here as there's no performance penalty
56* once they're populated.
57*/
59
60static int cmd_show_module_config(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info);
61static int module_name_tab_expand(UNUSED TALLOC_CTX *talloc_ctx, UNUSED void *uctx, fr_cmd_info_t *info, int max_expansions, char const **expansions);
62static int cmd_show_module_list(FILE *fp, UNUSED FILE *fp_err, UNUSED void *uctx, UNUSED fr_cmd_info_t const *info);
63static int cmd_show_module_status(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info);
64static int cmd_set_module_status(UNUSED FILE *fp, FILE *fp_err, void *ctx, fr_cmd_info_t const *info);
65
67 {
68 .parent = "show module",
69 .add_name = true,
70 .name = "status",
72 .help = "Show the status of a particular module.",
73 .read_only = true,
74 },
75
76 {
77 .parent = "show module",
78 .add_name = true,
79 .name = "config",
81 .help = "Show configuration for a module",
82 // @todo - do tab expand, by walking over the whole module list...
83 .read_only = true,
84 },
85
86 {
87 .parent = "set module",
88 .add_name = true,
89 .name = "status",
90 .syntax = "(alive|disallow|fail|reject|handled|invalid|notfound|noop|ok|updated)",
92 .help = "Change module status to fixed value.",
93 .read_only = false,
94 },
95
97};
98
100 {
101 .parent = "show",
102 .name = "module",
103 .help = "Show information about modules.",
104 .tab_expand = module_name_tab_expand,
105 .read_only = true,
106 },
107
108 // @todo - what if there's a module called "list" ?
109 {
110 .parent = "show module",
111 .name = "list",
112 .func = cmd_show_module_list,
113 .help = "Show the list of modules loaded in the server.",
114 .read_only = true,
115 },
116
117 {
118 .parent = "set",
119 .name = "module",
120 .help = "Change module settings.",
121 .tab_expand = module_name_tab_expand,
122 .read_only = false,
123 },
124
125
127};
128
129static int cmd_show_module_config(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
130{
131 module_instance_t *mi = ctx;
132
133 fr_assert(mi->conf != NULL);
134
135 (void) cf_section_write(fp, mi->conf, 0);
136
137 return 0;
138}
139
140static int module_name_tab_expand(UNUSED TALLOC_CTX *talloc_ctx, UNUSED void *uctx,
141 fr_cmd_info_t *info, int max_expansions, char const **expansions)
142{
143 char const *text;
144 int count;
145
146 if (info->argc <= 0) return 0;
147
148 text = info->argv[info->argc - 1];
149 count = 0;
150
152 module_instance_t *mi = talloc_get_type_abort(instance, module_instance_t);
153
154 if (count >= max_expansions) {
155 break;
156 }
157 if (fr_command_strncmp(text, mi->name)) {
158 expansions[count] = strdup(mi->name);
159 count++;
160 }
161 }}
162
163 return count;
164}
165
166static int cmd_show_module_list(FILE *fp, UNUSED FILE *fp_err, UNUSED void *uctx, UNUSED fr_cmd_info_t const *info)
167{
169 module_instance_t *mi = talloc_get_type_abort(instance, module_instance_t);
170
171 fprintf(fp, "\t%s\n", mi->name);
172 }}
173
174 return 0;
175}
176
177static int cmd_show_module_status(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
178{
179 module_instance_t *mi = ctx;
180
181 if (!mi->force) {
182 fprintf(fp, "alive\n");
183 return 0;
184 }
185
186 fprintf(fp, "%s\n", fr_table_str_by_value(rcode_table, mi->code, "<invalid>"));
187
188 return 0;
189}
190
191static int cmd_set_module_status(UNUSED FILE *fp, FILE *fp_err, void *ctx, fr_cmd_info_t const *info)
192{
193 module_instance_t *mi = ctx;
194 rlm_rcode_t rcode;
195
196 if (strcmp(info->argv[0], "alive") == 0) {
197 mi->force = false;
198 return 0;
199 }
200
202 if (rcode == RLM_MODULE_NOT_SET) {
203 fprintf(fp_err, "Unknown status '%s'\n", info->argv[0]);
204 return -1;
205 }
206
207 mi->code = rcode;
208 mi->force = true;
209
210 return 0;
211}
212
213/** Chars that are allowed in a module instance name
214 *
215 */
217 ['-'] = true, ['/'] = true, ['_'] = true, ['.'] = true,
218 ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true,
219 ['5'] = true, ['6'] = true, ['7'] = true, ['8'] = true, ['9'] = true,
220 ['A'] = true, ['B'] = true, ['C'] = true, ['D'] = true, ['E'] = true,
221 ['F'] = true, ['G'] = true, ['H'] = true, ['I'] = true, ['J'] = true,
222 ['K'] = true, ['L'] = true, ['M'] = true, ['N'] = true, ['O'] = true,
223 ['P'] = true, ['Q'] = true, ['R'] = true, ['S'] = true, ['T'] = true,
224 ['U'] = true, ['V'] = true, ['W'] = true, ['X'] = true, ['Y'] = true,
225 ['Z'] = true,
226 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true,
227 ['f'] = true, ['g'] = true, ['h'] = true, ['i'] = true, ['j'] = true,
228 ['k'] = true, ['l'] = true, ['m'] = true, ['n'] = true, ['o'] = true,
229 ['p'] = true, ['q'] = true, ['r'] = true, ['s'] = true, ['t'] = true,
230 ['u'] = true, ['v'] = true, ['w'] = true, ['x'] = true, ['y'] = true,
231 ['z'] = true
232};
233
234/** dl module tracking
235 *
236 * This is used by all module lists, irrespecitve of their type, and is thread safe.
237 */
239
240/** Callback to initialise any global structures required for the module list
241 *
242 * @param[in] ml to initialise global data for.
243 * @return
244 * - 0 on success.
245 * - -1 on failure.
246 */
248
249/** Callback to free any global structures associated with the module list
250 *
251 * @param[in] ml to free.
252 */
254
255/** Callback to add data for a module
256 *
257 * @param[in] mi to add data for.
258 * Use mi->ml for the module list.
259 * Use mi->data to access the data.
260 * @return
261 * - 0 on success.
262 * - -1 on failure.
263 */
265
266/** Callback to del data for a module
267 *
268 * @param[in] mi to add data to (use mi->ml for the module list).
269 *
270 */
272
273/** Callback to initialise a list for thread-local data, called once per thread
274 *
275 * @param[in] ctx talloc context for thread-local data.
276 * May be modified by the init function if the
277 * module_thread_instance_t need to be parented
278 * by another ctx.
279 * @param[in] ml to initialise thread-local data for.
280 * @return
281 * - 0 on success.
282 * - -1 on failure.
283 */
284typedef int (*module_list_thread_init_t)(TALLOC_CTX **ctx, module_list_t const *ml);
285
286/** Callback to free thread-local structures, called once per thread as the thread is being destroyed
287 *
288 * @param[in] ml to free thread-local data for.
289 */
291
292/** Callback to add thread-local data for a module
293 *
294 * @param[in] ti to add data for.
295 * Use `ti->mi->ml` for the module list.
296 * Use `ti->mi` for the module instance.
297 * Use `ti->data` for the thread specific data.
298 * @return
299 * - 0 on success.
300 * - -1 on failure.
301 */
303
304/** Callback to remove thread-local data for a module
305 *
306 * @param[in] ti to del data for.
307 * Use `ti->mi->ml` for the module list.
308 * Use `ti->mi` for the module instance.
309 * Use `ti->data` for the thread specific data.
310 */
312
313/** Structure to hold callbacks for a module list type
314 *
315 * We care about performance for module lists, as they're used heavily at runtime.
316 *
317 * As much as possible we try to avoid jumping through unecessary functions and
318 * unecessary switch statements.
319 *
320 * This structure contains callbacks which change how the module list operates,
321 * making it either a global module list, or a thread-local module list, i.e. one
322 * which only be used by a single thread.
323 *
324 * Instances of this structure are created in this compilation unit, and exported
325 * for the caller to pass into module_list_alloc().
326 */
328 size_t list_size; //!< Size of talloc_chunk to allocate for the module_list_t.
329
330 module_list_init_t init; //!< Initialise any global structures required for thread-local lookups.
331 module_list_free_t free; //!< Free any global structures required for thread-local lookups.
332
333 size_t inst_size; //!< Size of talloc chunk to allocate for the module_instance_t.
334 ///< allows over-allocation if list types want to append fields.
335 module_list_data_add_t data_add; //!< Record that module data has been added.
336 module_list_data_del_t data_del; //!< Record that module data has been removed.
337
338 /** Callbacks to manage thread-local data
339 */
340 struct {
341 module_list_thread_init_t init; //!< Initialise any thread-local structures required for thread-local lookups.
342 module_list_thread_free_t free; //!< Free any thread-local structures.
343
344 module_list_thread_data_add_t data_add; //!< Add thread-local data for a module.
345 module_list_thread_data_get_t data_get; //!< Retrieve thread local-data for a module.
346 module_list_thread_data_del_t data_del; //!< Remove (but not free) thread-local data for a module.
347
348 void *data; //!< Pointer to hold any global resources for the thread-local implementation.
350};
351
352typedef struct {
353 module_instance_t mi; //!< Common module instance fields. Must come first.
354
355 fr_heap_index_t inst_idx; //!< Entry in the bootstrap/instantiation heap.
356 //!< should be an identical value to the thread-specific
357 ///< data for this module.
359
360/** Sort module instance data first by list then by number
361 *
362 * The module's position in the global instance heap informs of us
363 * of its position in the thread-specific heap, which allows for
364 * O(1) lookups.
365 */
366static int8_t _mlg_module_instance_cmp(void const *one, void const *two)
367{
370 int8_t ret;
371
372 fr_assert(a->ml && b->ml);
373
374 ret = CMP(a->ml, b->ml);
375 if (ret != 0) return 0;
376
377 return CMP(a->number, b->number);
378}
379
380/** Free the global module index
381 *
382 */
383static int _mlg_global_free(UNUSED void *uctx)
384{
385 return talloc_free(mlg_index);
386}
387
388/** Initialise the global module index
389 *
390 */
391static int _mlg_global_init(UNUSED void *uctx)
392{
394 return 0;
395}
396
397/** Global initialisation for index heap and module array
398 *
399 */
401{
402 /*
403 * Create the global module heap we use for
404 * common indexes in the thread-specific
405 * heaps.
406 */
408
409 return 0;
410}
411
412/** Add the unique index value so we can do thread local lookups
413 *
414 */
416{
417 /*
418 * Insert the module into the global heap so
419 * we can get common thread-local indexes.
420 */
421 if (fr_heap_insert(&mlg_index, mi) < 0) {
422 ERROR("Failed inserting into global module index");
423 return -1;
424 }
425
426 return 0;
427}
428
430{
431 mlg_module_instance_t *mlg_mi = (mlg_module_instance_t *)talloc_get_type_abort(mi, module_instance_t);
432
433 if (!fr_heap_entry_inserted(mlg_mi->inst_idx)) return;
434
435 if (fr_heap_extract(&mlg_index, mi) == 0) return;
436
437 fr_assert(0);
438}
439
440/** Free the thread local heap on exit
441 *
442 * All thread local module lists should have been destroyed by this point
443 */
444static int _module_thread_inst_list_free(void *tilp)
445{
446 module_thread_instance_t **til = talloc_get_type_abort(tilp, module_thread_instance_t *);
447 size_t i, len = talloc_array_length(til);
448 unsigned int found = 0;
449
450 for (i = 0; i < len; i++) if (til[i]) found++;
451
452 if (!fr_cond_assert_msg(found == 0,
453 "Thread local array has %u non-null elements remaining on exit. This is a leak",
454 found)) {
455 return -1;
456 }
457
458 return talloc_free(til);
459}
460
461/** Allocate a thread-local array to hold thread data for each module thats been instantiated
462 *
463 * @param[in] ctx Talloc context for the thread-local data.
464 * Mutated by this function so that thread local data is allocated
465 * beneath the array.
466 * @param[in] ml Module list to initialise the thread-local data for.
467 */
468static int mlg_thread_init(UNUSED TALLOC_CTX **ctx, UNUSED module_list_t const *ml)
469{
470 /*
471 * Initialise the thread specific tree if this is the
472 * first time through or if everything else was
473 * de-initialised.
474 */
477
478 MEM(arr = talloc_zero_array(NULL, module_thread_instance_t *, fr_heap_num_elements(mlg_index)));
479
481 }
482
483 return 0;
484}
485
486/** Retrieve the thread-specific data for a module from the thread-local array of instance data
487 *
488 * This looks complex, but it's just asserts for sanity. This is really only returning an array offset.
489 *
490 * @param[in] mi Module instance to get the thread-specific data for.
491 */
493{
496 void *ti_p;
497
498 fr_assert_msg(mlg_mi->inst_idx <= talloc_array_length(mlg_thread_inst_list),
499 "module instance index %u must be <= thread local array %zu",
500 mlg_mi->inst_idx, talloc_array_length(mlg_thread_inst_list));
501
503 "mismatch between global module heap (%u entries) and thread local (%zu entries)",
505
506 /*
507 * Check for a NULL entry. This can happen when a module's
508 * thread instantiate callback fails, and we try and cleanup
509 * a partially instantiated thread.
510 */
511 ti_p = mlg_thread_inst_list[mlg_mi->inst_idx - 1];
512 if (unlikely(!ti_p)) return NULL;
513
514 ti = talloc_get_type_abort(ti_p, module_thread_instance_t);
515 fr_assert_msg(ti->mi == mi, "thread/module mismatch thread %s (%p), module %s (%p)",
516 ti->mi->name, ti->mi, mi->name, mi);
517
518 return ti;
519}
520
527
533
534/** Callbacks for a global module list
535 */
537 .init = mlg_init,
538
539 .inst_size = sizeof(mlg_module_instance_t),
540 .data_add = mlg_data_add,
541 .data_del = mlg_data_del,
542
543 .thread = {
544 .init = mlg_thread_init,
545 .data_add = mlg_thread_data_add,
546 .data_get = mlg_thread_data_get,
547 .data_del = mlg_thread_data_del
548 }
549};
550
551/** A slightly larger module_instance structure to hold the module instance and thread instance
552 */
553typedef struct {
554 module_instance_t mi; //!< Common module instance fields. Must come first.
555 module_thread_instance_t *ti; //!< Thread-specific data. Still in its own structure
556 ///< for talloc reasons.
558
560{
561 mltl_module_instance_t *mltl_mi = (mltl_module_instance_t *)talloc_get_type_abort(mi, module_instance_t);
562
563 /*
564 * Only free thread instance data we allocated...
565 */
566 if (mltl_mi->ti) module_thread_detach(mltl_mi->ti);
567}
568
574
576{
577 mltl_module_instance_t *mltl_mi = (mltl_module_instance_t *)talloc_get_type_abort(ti->mi, module_instance_t);
578 mltl_mi->ti = ti;
579 return 0;
580}
581
583{
584 mltl_module_instance_t *mltl_mi = (mltl_module_instance_t *)talloc_get_type_abort(ti->mi, module_instance_t);
585 mltl_mi->ti = NULL;
586}
587
588/** Callbacks for a thread local list
589 */
592 .data_del = mltl_mlg_data_del,
593
594 .thread = {
595 .data_add = mltl_thread_data_add,
596 .data_get = mltl_thread_data_get,
597 .data_del = mltl_thread_data_del
598 }
599};
600
601/** Print debugging information for a module
602 *
603 * @param[in] mi Module instance to print.
604 */
606{
607 FR_FAULT_LOG("%s (%p) {", mi->name, mi);
608 FR_FAULT_LOG(" type : %s", fr_table_str_by_value(dl_module_type_prefix, mi->module->type, "<invalid>"));
609 if (mi->parent) {
610 FR_FAULT_LOG(" parent : \"%s\" (%p)", mi->parent->name, mi->parent);
611 }
612 FR_FAULT_LOG(" bootstrapped : %s", mi->state & MODULE_INSTANCE_BOOTSTRAPPED ? "yes" : "no");
613 FR_FAULT_LOG(" instantiated : %s", mi->state & MODULE_INSTANCE_INSTANTIATED ? "yes" : "no");
614 FR_FAULT_LOG(" boot : %p", mi->boot);
615 FR_FAULT_LOG(" data : %p", mi->data);
616 FR_FAULT_LOG(" conf : %p", mi->conf);
617 FR_FAULT_LOG("}");
618}
619
620/** Print the contents of a module list
621 *
622 */
624{
625 module_instance_t const *inst;
627
628 FR_FAULT_LOG("Module list \"%s\" (%p) {", ml->name, ml);
629 FR_FAULT_LOG(" phase masked:");
630 FR_FAULT_LOG(" bootstrap : %s", ml->mask & MODULE_INSTANCE_BOOTSTRAPPED ? "yes" : "no");
631 FR_FAULT_LOG(" instantiate : %s", ml->mask & MODULE_INSTANCE_INSTANTIATED ? "yes" : "no");
632 FR_FAULT_LOG(" thread : %s", ml->mask & MODULE_INSTANCE_INSTANTIATED ? "yes" : "no");
633 FR_FAULT_LOG("}");
634 /*
635 * Modules are printed in the same order
636 * they would be bootstrapped or inserted
637 * into the tree.
638 */
639 for (inst = fr_rb_iter_init_inorder(&iter, ml->name_tree);
640 inst;
641 inst = fr_rb_iter_next_inorder(&iter)) {
643 }
644}
645
646/** Protect module data
647 *
648 * @param[in] mi module instance.
649 * @param[in] pool to protect
650 * @return
651 * - 0 on success.
652 * - -1 on failure.
653 */
654static inline CC_HINT(always_inline)
656{
657 if ((pool->start == NULL) || !mi->ml->write_protect) return 0; /* noop */
658
659 DEBUG3("Protecting data for module \"%s\" %p-%p",
660 mi->name, pool->start, ((uint8_t *)pool->start + pool->len - 1));
661
662 if (unlikely(mprotect(pool->start, pool->len, PROT_READ) < 0)) {
663 fr_strerror_printf("Protecting \"%s\" module data failed: %s", mi->name, fr_syserror(errno));
664 return -1;
665 }
666
667 return 0;
668}
669
670/** Unprotect module data
671 *
672 * @param[in] mi module instance.
673 * @param[in] pool to protect
674 * @return
675 * - 0 on success.
676 * - -1 on failure.
677 */
678static inline CC_HINT(always_inline)
680{
681 if ((pool->start == NULL) || !mi->ml->write_protect) return 0; /* noop */
682
683 DEBUG3("Unprotecting data for module \"%s\" %p-%p",
684 mi->name, pool->start, ((uint8_t *)pool->start + pool->len - 1));
685
686 if (unlikely(mprotect(pool->start, pool->len, PROT_READ | PROT_WRITE) < 0)) {
687 fr_strerror_printf("Unprotecting \"%s\" data failed: %s", mi->name, fr_syserror(errno));
688 return -1;
689 }
690
691 return 0;
692}
693
694/** Mark module data as read only
695 *
696 * @param[in] mi Instance data to protect (mark as read only).
697 * @return
698 * - 0 on success.
699 * - -1 on failure.
700 */
705
706/** Mark module data as read/write
707 *
708 * @param[in] mi Instance data to unprotect (mark as read/write).
709 * @return
710 * - 0 on success.
711 * - -1 on failure.
712 */
717
718/** Return the prefix string for the deepest module
719 *
720 * This is useful for submodules which don't have a prefix of their own.
721 * In this case we need to use the prefix of the shallowest module, which
722 * will be a proto or rlm module.
723 *
724 * @param[in] mi Instance to get the prefix for.
725 * @return The prefix string for the shallowest module.
726 */
728{
729 module_instance_t const *root = module_instance_root(mi);
730
731 return fr_table_str_by_value(dl_module_type_prefix, root->module->type, "<INVALID>");
732}
733
734/** Avoid boilerplate when setting the module instance name
735 *
736 */
738{
739 char const *name2;
740 char const *inst_name;
741 fr_slen_t slen;
742
743 name2 = cf_section_name2(conf);
744 if (name2) {
745 inst_name = name2;
746 goto done;
747 }
748
749 inst_name = cf_section_name1(conf);
750done:
751 slen = module_instance_name_valid(inst_name);
752 if (slen < 0) {
753 cf_log_perr(conf, "Invalid module configuration");
754 *name = NULL;
755 return slen;
756 }
757
758 *name = inst_name;
759
760 return 0;
761}
762
763/** Covert a CONF_SECTION into parsed module instance data
764 *
765 */
767{
768 /*
769 * Associate the module instance with the conf section
770 * *before* executing any parse rules that might need it.
771 */
772 cf_data_add(conf, mi, mi->module->dl->name, false);
773 mi->conf = conf;
774
775 if (mi->exported->config && mi->conf) {
776 if ((cf_section_rules_push(mi->conf, mi->exported->config)) < 0 ||
777 (cf_section_parse(mi->data, mi->data, mi->conf) < 0)) {
778 cf_log_err(mi->conf, "Failed evaluating configuration for module \"%s\"",
779 mi->module->dl->name);
780 return -1;
781 }
782 }
783
784 return 0;
785}
786
787/** Compare module instances by parent and name
788 *
789 * The reason why we need parent, is because we could have submodules with names
790 * that conflict with their parent.
791 */
792static int8_t module_instance_name_cmp(void const *one, void const *two)
793{
794 module_instance_t const *a = one;
795 module_instance_t const *b = two;
796 module_instance_t const *mi;
797 int a_depth = 0, b_depth = 0;
798 int ret;
799
800#ifdef STATIC_ANALYZER
801 if (!fr_cond_assert(a)) return +1;
802 if (!fr_cond_assert(b)) return -1;
803#endif
804
805 /*
806 * Sort by depth, so for tree walking we start
807 * at the shallowest node, and finish with
808 * the deepest child.
809 */
810 for (mi = a; mi; mi = mi->parent) a_depth++;
811 for (mi = b; mi; mi = mi->parent) b_depth++;
812
813 ret = CMP(a_depth, b_depth);
814 if (ret != 0) return ret;
815
816 ret = CMP(a->parent, b->parent);
817 if (ret != 0) return ret;
818
819 ret = strcmp(a->name, b->name);
820 return CMP(ret, 0);
821}
822
823/** Compare module's by their private instance data
824 *
825 */
826static int8_t module_instance_data_cmp(void const *one, void const *two)
827{
828 void const *a = ((module_instance_t const *)one)->data;
829 void const *b = ((module_instance_t const *)two)->data;
830
831 return CMP(a, b);
832}
833
834/** Generic callback for conf_parser_t to load a submodule
835 *
836 * conf_parser_t entry should point to a module_instance_t field in the instance data
837 *
838 * @param[in] ctx unused.
839 * @param[out] out A pointer to a pointer to a module_instance_t.
840 * @param[in] parent This _must_ point to the instance data of the parent
841 * module.
842 * @param[in] ci The CONF_PAIR containing the name of the submodule to load.
843 * @param[in] rule uctx pointer must be a pointer to a module_list_t **
844 * containing the list to search in.
845 * @return
846 * - 0 on success.
847 * - -1 if we failed to load the submodule.
848 */
849int module_submodule_parse(UNUSED TALLOC_CTX *ctx, void *out, void *parent,
850 CONF_ITEM *ci, conf_parser_t const *rule)
851{
852 char const *name = cf_pair_value(cf_item_to_pair(ci));
854 CONF_SECTION *submodule_cs;
856 module_list_t *ml = talloc_get_type_abort(*((void * const *)rule->uctx), module_list_t);
857
858 /*
859 * We assume the submodule's config is the
860 * in a section with the same name as
861 * the submodule.
862 */
863 submodule_cs = cf_section_find(cs, name, NULL);
864
865 /*
866 * Allocate an empty section if one doesn't exist
867 * this is so defaults get parsed.
868 */
869 if (!submodule_cs) submodule_cs = cf_section_alloc(cs, cs, name, NULL);
870
871 /*
872 * The submodule name dictates the module loaded
873 * the instance name is always the submodule name
874 * and will be appended to the parent's instance
875 * name.
876 */
878 if (unlikely(mi == NULL)) {
879 cf_log_err(submodule_cs, "Failed loading submodule");
880 return -1;
881 }
882
883 if (unlikely(module_instance_conf_parse(mi, submodule_cs) < 0)) {
884 cf_log_err(submodule_cs, "Failed parsing submodule config");
885 talloc_free(mi);
886 return -1;
887 }
888
889 *((module_instance_t **)out) = mi;
890
891 return 0;
892}
893
894/** Find an existing module instance by its name and parent
895 *
896 * @param[in] ml to search in.
897 * @param[in] parent to qualify search with.
898 * @param[in] asked_name The name of the module we're attempting to find.
899 * May include '-' which indicates that it's ok for
900 * the module not to be loaded.
901 * @return
902 * - Module instance matching name.
903 * - NULL if no such module exists.
904 */
906{
907 char const *inst_name;
908 void *inst;
909
910 if (!ml->name_tree) return NULL;
911
912 /*
913 * Look for the real name. Ignore the first character,
914 * which tells the server "it's OK for this module to not
915 * exist."
916 */
917 inst_name = asked_name;
918 if (inst_name[0] == '-') inst_name++;
919
922 .parent = UNCONST(module_instance_t *, parent),
923 .name = inst_name
924 });
925 if (!inst) return NULL;
926
927 return talloc_get_type_abort(inst, module_instance_t);
928}
929
930/** Find the module's shallowest parent
931 *
932 * @param[in] child to locate the root for.
933 * @return
934 * - The module's shallowest parent.
935 * - NULL on error.
936 */
938{
939 module_instance_t const *next;
940
941 for (;;) {
942 next = child->parent;
943 if (!next) break;
944
945 child = next;
946 }
947
948 return UNCONST(module_instance_t *, child);
949}
950
951/** Find an existing module instance by its private instance data
952 *
953 * @param[in] ml to search in.
954 * @param[in] data to resolve to module_instance_t.
955 * @return
956 * - Module instance matching data.
957 * - NULL if no such module exists.
958 */
960{
962
963 mi = fr_rb_find(ml->data_tree,
965 .data = UNCONST(void *, data)
966 });
967 if (!mi) return NULL;
968
969 return talloc_get_type_abort(mi, module_instance_t);
970}
971
972/** Retrieve module/thread specific instance data for a module
973 *
974 * @param[in] ml Module list module belongs to.
975 * @param[in] data Private instance data of the module.
976 * Same as what would be provided by
977 * #module_instance_by_data.
978 * @return
979 * - Thread specific instance data on success.
980 * - NULL if module has no thread instance data.
981 */
983{
985
986 if (!mi) return NULL;
987
988 return module_thread(mi);
989}
990
992{
993 module_list_t *ml;
994
995 /*
996 * This can happen when a module's thread instantiate
997 * callback fails, and we try and cleanup a partially
998 * instantiated thread.
999 */
1000 if (unlikely(!ti)) return;
1001
1002 ml = ti->mi->ml;
1003 ml->type->thread.data_del(ti);
1004 talloc_free(ti);
1005}
1006
1007/** Remove thread-specific data for a given module list
1008 *
1009 * Removes all module thread data for the
1010 */
1012{
1014 void *inst;
1015
1016 /*
1017 * Loop over all the modules in the module list
1018 * finding and extracting their thread specific
1019 * data, and calling their detach methods.
1020 */
1021 for (inst = fr_rb_iter_init_inorder(&iter, ml->name_tree);
1022 inst;
1023 inst = fr_rb_iter_next_inorder(&iter)) {
1024 module_instance_t *mi = talloc_get_type_abort(inst, module_instance_t);
1026
1028 }
1029
1030 /*
1031 * Cleanup any lists the module list added to this thread
1032 */
1033 if (ml->type->thread.free) ml->type->thread.free(ml);
1034}
1035
1036/** Callback to free thread local data
1037 *
1038 * ti->data is allocated in the context of ti, so will be freed too.
1039 *
1040 * Calls the detach function for thread local data, and removes the data from the
1041 * thread local list.
1042 *
1043 * @param[in] ti to free.
1044 */
1046{
1047 module_instance_t const *mi = ti->mi;
1048
1049 /*
1050 * Never allocated a thread instance, so we don't need
1051 * to clean it up...
1052 */
1053 if (mi->state & MODULE_INSTANCE_NO_THREAD_INSTANTIATE) return 0;
1054
1055 DEBUG4("Cleaning up %s thread instance data (%p/%p)",
1056 mi->exported->name, ti, ti->data);
1057
1058 if (mi->exported->thread_detach) {
1060 .mi = ti->mi,
1061 .thread = ti->data,
1062 .el = ti->el
1063 });
1064 }
1065
1066 ti->mi->ml->type->thread.data_del(ti);
1067
1068 return 0;
1069}
1070
1071/** Allocate thread-local instance data for a module
1072 *
1073 * The majority of modules will have a single set of thread-specific instance data.
1074 *
1075 * An exception is dynamic modules, which may have multiple sets of thread-specific instance data tied to
1076 * a specific dynamic use of that module.
1077 *
1078 * @param[in] ctx Talloc ctx to bind thread specific data to.
1079 * @param[in] mi Module instance to perform thread instantiation for.
1080 * @param[in] el Event list serviced by this thread.
1081 * @return
1082 * - 0 on success.
1083 * - -1 on failure.
1084 */
1086{
1087 module_list_t *ml = mi->ml;
1089
1090 /*
1091 * Allows the caller of module_instance_alloc to
1092 * skip thread instantiation for certain modules instances
1093 * whilst allowing modules to still register thread
1094 * instantiation callbacks.
1095 *
1096 * This is mainly there for the single global instance of
1097 * a module, which will only have run-time thread-specific
1098 * instances, like dynamic/keyed modules.
1099 */
1101
1102 /*
1103 * Check the list pointers are ok
1104 */
1105 (void)talloc_get_type_abort(mi->ml, module_list_t);
1106
1107 MEM(ti = talloc_zero(ctx, module_thread_instance_t));
1108 talloc_set_destructor(ti, _module_thread_inst_free);
1109 ti->el = el;
1110 ti->mi = mi;
1111
1112 if (mi->exported->thread_inst_size) {
1113 MEM(ti->data = talloc_zero_array(ti, uint8_t, mi->exported->thread_inst_size));
1114
1115 /*
1116 * Fixup the type name, in case something calls
1117 * talloc_get_type_abort() on it...
1118 */
1119 if (!mi->exported->thread_inst_type) {
1120 talloc_set_name(ti->data, "%s_%s_thread_t",
1122 mi->exported->name);
1123 } else {
1124 talloc_set_name_const(ti->data, mi->exported->thread_inst_type);
1125 }
1126 }
1127
1128 if (ml->type->thread.data_add(ti) < 0) {
1129 PERROR("Failed adding thread data for module \"%s\"", mi->name);
1130 error:
1131 ml->type->thread.data_del(ti);
1132 talloc_free(ti);
1133 return -1;
1134 }
1135
1136 /*
1137 * So we don't get spurious errors
1138 */
1140
1141 DEBUG4("Alloced %s thread instance data (%p/%p)", ti->mi->exported->name, ti, ti->data);
1142 if (mi->exported->thread_instantiate &&
1144 PERROR("Thread instantiation failed for module \"%s\"", mi->name);
1145 goto error;
1146 }
1147
1148 return 0;
1149}
1150
1151/** Creates per-thread instance data for modules which need it
1152 *
1153 * Must be called by any new threads before attempting to execute unlang sections.
1154 *
1155 * @param[in] ctx Talloc ctx to bind thread specific data to.
1156 * @param[in] ml Module list to perform thread instantiation for.
1157 * @param[in] el Event list serviced by this thread.
1158 * @return
1159 * - 0 on success.
1160 * - -1 on failure.
1161 */
1163{
1164 void *inst;
1166 int ret;
1167
1168 /*
1169 * Do any thread-local instantiation necessary
1170 */
1171 if (ml->type->thread.init) {
1172 ret = ml->type->thread.init(&ctx, ml);
1173 if (unlikely(ret < 0)) return ret;
1174 }
1175
1176 for (inst = fr_rb_iter_init_inorder(&iter, ml->name_tree);
1177 inst;
1178 inst = fr_rb_iter_next_inorder(&iter)) {
1179 module_instance_t *mi = talloc_get_type_abort(inst, module_instance_t); /* Sanity check*/
1180
1181 if (module_thread_instantiate(ctx, mi, el) < 0) {
1183 return -1;
1184 }
1185 }
1186
1187 return 0;
1188}
1189
1190/** Manually complete module setup by calling its instantiate function
1191 *
1192 * @param[in] instance of module to complete instantiation for.
1193 * @return
1194 * - 0 on success.
1195 * - -1 on failure.
1196 */
1198{
1199 module_instance_t *mi = talloc_get_type_abort(instance, module_instance_t);
1200 CONF_SECTION *cs = mi->conf;
1201
1202 /*
1203 * If we're instantiating, then nothing should be able to
1204 * modify the boot data for this module.
1205 *
1206 * mprotect is thread-safe, so we don't need to worry about
1207 * synchronisation. There is the overhead of a system call
1208 * but dynamic module instantiation is relatively rare.
1209 *
1210 * We need to wait until all modules have registered things
1211 * like xlat functions, as the xlat functions themselves may
1212 * end up being allocated in boot pool data, and have inline
1213 * rbtree node structures, which may be modified as additional
1214 * xlat functions are registered.
1215 */
1216 if (unlikely(module_data_protect(mi, &mi->boot_pool) < 0)) {
1217 cf_log_perr(mi->conf, "\"%s\"", mi->name);
1218 return -1;
1219 }
1220
1221 /*
1222 * We only instantiate modules in the bootstrapped state
1223 */
1224 if (module_instance_skip_instantiate(mi)) return 0;
1225
1226 if (mi->module->type == DL_MODULE_TYPE_MODULE) {
1227 if (fr_command_register_hook(NULL, mi->name, mi, module_cmd_table) < 0) {
1228 PERROR("Failed registering radmin commands for module %s", mi->name);
1229 return -1;
1230 }
1231 }
1232
1233 /*
1234 * Now that ALL modules are instantiated, and ALL xlats
1235 * are defined, go compile the config items marked as XLAT.
1236 */
1237 if (mi->exported->config && (cf_section_parse_pass2(mi->data,
1238 mi->conf) < 0)) return -1;
1239
1240 /*
1241 * Call the instantiate method, if any.
1242 */
1243 if (mi->exported->instantiate) {
1244 cf_log_debug(cs, "Instantiating %s_%s \"%s\"",
1246 mi->module->exported->name,
1247 mi->name);
1248
1249 /*
1250 * Call the module's instantiation routine.
1251 */
1252 if (mi->exported->instantiate(MODULE_INST_CTX(mi)) < 0) {
1253 cf_log_err(mi->conf, "Instantiation failed for module \"%s\"", mi->name);
1254
1255 return -1;
1256 }
1257 }
1258
1259 /*
1260 * Instantiate shouldn't modify any global resources
1261 * so we can protect the data now without the side
1262 * effects we might see with boot data.
1263 */
1264 if (unlikely(module_data_protect(mi, &mi->inst_pool) < 0)) {
1265 cf_log_perr(mi->conf, "\"%s\"", mi->name);
1266 return -1;
1267 }
1269
1270 return 0;
1271}
1272
1273/** Completes instantiation of modules
1274 *
1275 * Allows the module to initialise connection pools, and complete any registrations that depend on
1276 * attributes created during the bootstrap phase.
1277 *
1278 * @param[in] ml containing modules to instantiate.
1279 * @return
1280 * - 0 on success.
1281 * - -1 on failure.
1282 */
1284{
1285 void *inst;
1287
1288 DEBUG2("#### Instantiating %s modules ####", ml->name);
1289
1290 for (inst = fr_rb_iter_init_inorder(&iter, ml->name_tree);
1291 inst;
1292 inst = fr_rb_iter_next_inorder(&iter)) {
1293 module_instance_t *mi = talloc_get_type_abort(inst, module_instance_t);
1294 if (module_instantiate(mi) < 0) return -1;
1295 }
1296
1297 return 0;
1298}
1299
1300/** Manually complete module bootstrap by calling its instantiate function
1301 *
1302 * - Parse the module configuration.
1303 * - Call the modules "bootstrap" method.
1304 *
1305 * @param[in] mi Module instance to bootstrap.
1306 * @return
1307 * - 0 on success.
1308 * - -1 on failure.
1309 */
1311{
1312 /*
1313 * We only bootstrap modules in the init state
1314 */
1315 if (module_instance_skip_bootstrap(mi)) return 0;
1316
1317 /*
1318 * Bootstrap the module.
1319 * This must be done last so that the
1320 * module can find its module_instance_t
1321 * in the trees if it needs to bootstrap
1322 * submodules.
1323 */
1324 if (mi->exported->bootstrap) {
1325 CONF_SECTION *cs = mi->conf;
1326
1327 cf_log_debug(cs, "Bootstrapping %s_%s \"%s\"",
1329 mi->module->exported->name,
1330 mi->name);
1331
1332 /*
1333 * Modules MUST NOT modify their instance data during
1334 * bootstrap. This is because dynamic (runtime) modules
1335 * don't run their boostrap callbacks, and MUST re-resolve
1336 * any resources added during bootstrap in the
1337 * instantiate callback.
1338 *
1339 * Bootstrap is ONLY there for adding global,
1340 * module-specific resources.
1341 *
1342 * If the module has MODULE_TYPE_DYNAMIC_UNSAFE is set,
1343 * then we don't need the restriction.
1344 */
1345 if ((!(mi->exported->flags & MODULE_TYPE_DYNAMIC_UNSAFE)) &&
1346 unlikely(module_data_protect(mi, &mi->inst_pool) < 0)) {
1347 cf_log_perr(cs, "\"%s\"", mi->name);
1348 return -1;
1349 }
1350 if (mi->exported->bootstrap(MODULE_INST_CTX(mi)) < 0) {
1351 cf_log_err(cs, "Bootstrap failed for module \"%s\"", mi->name);
1352 return -1;
1353 }
1354 if (unlikely(module_data_unprotect(mi, &mi->inst_pool) < 0)) {
1355 cf_log_perr(cs, "\"%s\"", mi->name);
1356 return -1;
1357 }
1358 }
1360
1361 return 0;
1362}
1363
1364/** Bootstrap any modules which have not been bootstrapped already
1365 *
1366 * Allows the module to initialise connection pools, and complete any registrations that depend on
1367 * attributes created during the bootstrap phase.
1368 *
1369 * @param[in] ml containing modules to bootstrap.
1370 * @return
1371 * - 0 on success.
1372 * - -1 on failure.
1373 */
1375{
1376 void *instance;
1378
1379 DEBUG2("#### Bootstrapping %s modules ####", ml->name);
1380
1381 for (instance = fr_rb_iter_init_inorder(&iter, ml->name_tree);
1382 instance;
1383 instance = fr_rb_iter_next_inorder(&iter)) {
1384 module_instance_t *mi = talloc_get_type_abort(instance, module_instance_t);
1385 if (module_bootstrap(mi) < 0) return -1;
1386 }
1387
1388 return 0;
1389}
1390
1391/** Generate a module name from the module's name and its parents
1392 *
1393 * @param[in] ctx Where to allocate the module name.
1394 * @param[out] out Where to write a pointer to the instance name.
1395 * @param[in] parent of the module.
1396 * @param[in] inst_name module's instance name.
1397 */
1398static fr_slen_t module_instance_name(TALLOC_CTX *ctx, char **out,
1399 module_instance_t const *parent, char const *inst_name)
1400{
1401 fr_sbuff_t *agg;
1402
1404
1405 /*
1406 * Parent has all of the qualifiers of its ancestors
1407 * already in the name, so we just need to concatenate.
1408 */
1409 if (parent) {
1411 FR_SBUFF_IN_CHAR_RETURN(agg, '.');
1412 }
1413 FR_SBUFF_IN_STRCPY_RETURN(agg, inst_name);
1414
1415 MEM(*out = talloc_bstrndup(ctx, fr_sbuff_start(agg), fr_sbuff_used(agg)));
1416
1417 return fr_sbuff_used(agg);
1418}
1419
1420/** Detach the shallowest parent first
1421 *
1422 * This ensures that the module's parent is detached before it is.
1423 *
1424 * Generally parents reach into their children and not the other way
1425 * around. Calling the parent's detach method first ensures that
1426 * there's no code that access the child module's instance data or
1427 * reach into its symbol space if it's being unloaded.
1428 *
1429 * @note If you don't want to detach the parent, maybe because its children
1430 * are ephemeral, consider using a seaprate thread-local module list
1431 * to hold the children instead.
1432 *
1433 * @param[in] mi to detach.
1434 */
1436{
1438
1440
1442 if (mi->exported && mi->exported->detach) {
1444 }
1446 }
1447
1449 if (mi->exported && mi->exported->unstrap) {
1451 }
1453 }
1454}
1455
1456/** Free module's instance data, and any xlats or paircmps
1457 *
1458 * @param[in] mi to free.
1459 * @return 0
1460 */
1462{
1463 module_list_t *ml = mi->ml;
1464
1465 DEBUG3("Freeing %s (%p)", mi->name, mi);
1466
1467 /*
1468 * Allow writing to instance and bootstrap data again
1469 * so we can clean up without segving.
1470 */
1471 if (unlikely(module_data_unprotect(mi, &mi->inst_pool) < 0)) {
1472 cf_log_perr(mi->conf, "\"%s\"", mi->name);
1473 return -1;
1474 }
1475 if (unlikely(module_data_unprotect(mi, &mi->boot_pool) < 0)) {
1476 cf_log_perr(mi->conf, "\"%s\"", mi->name);
1477 return -1;
1478 }
1479
1482 if (ml->type->data_del) ml->type->data_del(mi);
1483
1484 /*
1485 * mi->exported may be NULL if we failed loading the module
1486 */
1487 if (mi->exported && ((mi->exported->flags & MODULE_TYPE_THREAD_UNSAFE) != 0)) {
1488#ifndef NDEBUG
1489 int ret;
1490
1491 /*
1492 * If the mutex is locked that means
1493 * the server exited without cleaning
1494 * up requests.
1495 *
1496 * Assert that the mutex is not held.
1497 */
1498 ret = pthread_mutex_trylock(&mi->mutex);
1499 fr_assert_msg(ret == 0, "Failed locking module mutex during exit: %s", fr_syserror(ret));
1500 pthread_mutex_unlock(&mi->mutex);
1501#endif
1502 pthread_mutex_destroy(&mi->mutex);
1503 }
1504
1505 /*
1506 * Remove all xlat's registered to module instance.
1507 */
1508 if (mi->data) {
1511 }
1512
1514
1515 /*
1516 * We need to explicitly free all children, so the module instance
1517 * destructors get executed before we unload the bytecode for the
1518 * module.
1519 *
1520 * If we don't do this, we get a SEGV deep inside the talloc code
1521 * when it tries to call a destructor that no longer exists.
1522 */
1523 talloc_free_children(mi);
1524
1525 dl_module_free(mi->module);
1526
1527 return 0;
1528}
1529
1530/** Duplicate a module instance, placing it in a new module list
1531 *
1532 * @param[in] dst list to place the new module instance in.
1533 * @param[in] src to duplicate.
1534 * @param[in] inst_name new instance name. If null, src->name will be used.
1535 */
1537{
1538 module_instance_t *mi = module_instance_alloc(dst, src->parent, src->module->type,
1539 src->module->name,
1540 inst_name ? inst_name : src->name, 0);
1541 if (!mi) return NULL;
1542
1543 return mi;
1544}
1545
1546/** Allocate module instance data
1547 *
1548 * @param[in] ctx talloc context to allocate data in.
1549 * @param[out] pool_out where to write pool details.
1550 * @param[out] out where to write data pointer.
1551 * @param[in] mi module instance.
1552 * @param[in] size of data to allocate.
1553 * @param[in] type talloc type to assign.
1554 */
1555static inline CC_HINT(always_inline)
1556void module_instance_data_alloc(TALLOC_CTX *ctx, module_data_pool_t *pool_out, void **out,
1557 module_instance_t *mi, size_t size, char const *type)
1558{
1559 dl_module_t const *module = mi->module;
1560 void *data;
1561
1562 /*
1563 * If there is supposed to be instance data, allocate it now.
1564 *
1565 * If the structure is zero length then allocation will still
1566 * succeed, and will create a talloc chunk header.
1567 *
1568 * This is needed so we can resolve instance data back to
1569 * module_instance_t/dl_module_t/dl_t.
1570 */
1571 pool_out->ctx = talloc_page_aligned_pool(ctx,
1572 &pool_out->start, &pool_out->len,
1573 1, size);
1574 MEM(data = talloc_zero_array(pool_out->ctx, uint8_t, size));
1575 if (!type) {
1576 talloc_set_name(data, "%s_t", module->dl->name ? module->dl->name : "config");
1577 } else {
1578 talloc_set_name_const(data, type);
1579 }
1580 *out = data;
1581}
1582
1583/** Check to see if a module instance name is valid
1584 *
1585 * @note On failure the error message may be retrieved with fr_strerror().
1586 *
1587 * @param[in] inst_name to check.
1588 *
1589 * @return
1590 * - 0 on success.
1591 * - Negative value on error indicating the position of the bad char.
1592 */
1594{
1595 /*
1596 * [] are used for dynamic module selection.
1597 * . is used as a method and submodule separator.
1598 * Quoting and other characters would just confuse the parser in too many
1599 * instances so they're disallowed too.
1600 */
1601 {
1602 size_t len = strlen(inst_name);
1603
1604 for (size_t i = 0; i < len; i++) {
1605 if (!module_instance_allowed_chars[(uint8_t)inst_name[i]]) {
1606 fr_strerror_printf("Instance name \"%s\" contains an invalid character. "
1607 "Valid characters are [0-9a-zA-Z/_-]", inst_name);
1608 return -(i + 1);
1609 }
1610 }
1611 }
1612
1613 return 0;
1614}
1615
1616/** Set the uctx pointer for a module instance
1617 *
1618 * @param[in] mi to set the uctx for.
1619 * @param[in] uctx to set.
1620 */
1622{
1623 mi->uctx = uctx;
1624}
1625
1626/** Allocate a new module and add it to a module list for later bootstrap/instantiation
1627 *
1628 * - Load the module shared library.
1629 * - Allocate instance data for it.
1630 *
1631 * @param[in] ml To add module to.
1632 * @param[in] parent of the module being bootstrapped, if this is a submodule.
1633 * If this is not a submodule parent must be NULL.
1634 * @param[in] type What type of module we're loading. Determines the prefix
1635 * added to the library name. Should be one of:
1636 * - DL_MODULE_TYPE_MODULE - Standard backend module.
1637 * - DL_MODULE_TYPE_SUBMODULE - Usually a driver for a backend module.
1638 * - DL_MODULE_TYPE_PROTO - A module associated with a listen section.
1639 * - DL_MODULE_TYPE_PROCESS - Protocol state machine bound to a virtual server.
1640 * @param[in] mod_name The name of this module, i.e. 'redis' for 'rlm_redis'.
1641 * @param[in] inst_name Instance name for this module, i.e. "aws_redis_01".
1642 * The notable exception is if this is a submodule, in which case
1643 * inst_name is usually the mod_name.
1644 * @param[in] init_state The state the module "starts" in. Can be used to prevent
1645 * bootstrapping, instantiation, or thread instantiation of the module,
1646 * by passing one or more of the MODULE_INSTANCE_* flags.
1647 * Should usually be 0, unless special behaviour is required.
1648 * @return
1649 * - A new module instance handle, containing the module's public interface,
1650 * and private instance data.
1651 * - NULL on error.
1652 */
1655 dl_module_type_t type, char const *mod_name, char const *inst_name,
1656 module_instance_state_t init_state)
1657{
1658 char *qual_inst_name = NULL;
1660
1665
1666 /*
1667 * Takes the inst_name and adds qualifiers
1668 * if this is a submodule.
1669 */
1670 if (module_instance_name(NULL, &qual_inst_name, parent, inst_name) < 0) {
1671 ERROR("Module name too long");
1672 return NULL;
1673 }
1674
1675 /*
1676 * See if the module already exists.
1677 */
1678 mi = module_instance_by_name(ml, parent, qual_inst_name);
1679 if (mi) {
1680 /*
1681 * We may not have configuration data yet
1682 * for the duplicate module.
1683 */
1684 if (mi->conf) {
1685 ERROR("Duplicate %s_%s instance \"%s\", previous instance defined at %s[%d]",
1686 fr_table_str_by_value(dl_module_type_prefix, mi->module->type, "<INVALID>"),
1687 mi->module->exported->name,
1688 qual_inst_name,
1689 cf_filename(mi->conf),
1690 cf_lineno(mi->conf));
1691
1692 } else {
1693 ERROR("Duplicate %s_%s instance \"%s\"",
1694 fr_table_str_by_value(dl_module_type_prefix, mi->module->type, "<INVALID>"),
1695 mi->module->exported->name,
1696 qual_inst_name);
1697 }
1698 talloc_free(qual_inst_name);
1699 return NULL;
1700 }
1701
1702 /*
1703 * Overallocate the module instance, so we can add
1704 * some module list type specific data to it.
1705 */
1706 MEM(mi = (module_instance_t *)talloc_zero_array(parent ? (void const *)parent : (void const *)ml, uint8_t, ml->type->inst_size));
1707 talloc_set_name_const(mi, "module_instance_t");
1708 mi->name = talloc_typed_strdup(mi, qual_inst_name);
1709 talloc_free(qual_inst_name); /* Avoid stealing */
1710
1711 mi->ml = ml;
1712 mi->parent = parent;
1713 mi->state = init_state;
1714
1715 /*
1716 * Increment the reference count on an already loaded module,
1717 * or load the .so or .dylib, and run all the global callbacks.
1718 */
1719 mi->module = dl_module_alloc(parent ? parent->module : NULL, mod_name, type);
1720 if (!mi->module) {
1721 error:
1722 talloc_free(mi);
1723 return NULL;
1724 }
1725
1726 /*
1727 * We have no way of checking if this is correct... so we hope...
1728 */
1729 mi->exported = (module_t *)mi->module->exported;
1730 if (unlikely(mi->exported == NULL)) {
1731 ERROR("Missing public structure for \"%s\"", qual_inst_name);
1732 goto error;
1733 }
1734
1735 /*
1736 * Allocate bootstrap data.
1737 */
1738 if (mi->exported->bootstrap) {
1740 mi, mi->exported->boot_size, mi->exported->boot_type);
1741 }
1742 /*
1743 * Allocate the module instance data. We always allocate
1744 * this so the module can use it for lookup.
1745 */
1747 mi, mi->exported->inst_size, mi->exported->inst_type);
1748 /*
1749 * If we're threaded, check if the module is thread-safe.
1750 *
1751 * If it isn't, we init the mutex.
1752 *
1753 * Do this here so the destructor can trylock the mutex
1754 * correctly even if bootstrap/instantiation fails.
1755 */
1756 if ((mi->exported->flags & MODULE_TYPE_THREAD_UNSAFE) != 0) pthread_mutex_init(&mi->mutex, NULL);
1757 talloc_set_destructor(mi, _module_instance_free); /* Set late intentionally */
1758 mi->number = ml->last_number++;
1759
1760 /*
1761 * Remember the module for later.
1762 */
1763 if (!fr_cond_assert(fr_rb_insert(ml->name_tree, mi))) goto error;
1764 if (!fr_cond_assert(fr_rb_insert(ml->data_tree, mi))) goto error;
1765 if (ml->type->data_add && unlikely(ml->type->data_add(mi) < 0)) goto error;
1766
1767 return mi;
1768}
1769
1770/** Free all modules loaded by the server
1771 *
1772 * @param[in] ml Module list being freed.
1773 * @return 0
1774 */
1776{
1779
1780 /*
1781 * Re-initialize the iterator after freeing each module.
1782 * The module may have children which are also in the
1783 * tree. It can cause problems when we delete children
1784 * without the iterator knowing about it.
1785 */
1786 while ((mi = fr_rb_iter_init_inorder(&iter, ml->name_tree)) != NULL) {
1787 fr_rb_iter_delete_inorder(&iter); /* Keeps the iterator sane */
1788 talloc_free(mi);
1789 }
1790
1791 if (ml->type->free) ml->type->free(ml);
1792
1793 return 0;
1794}
1795
1796/** Should we bootstrap this module instance?
1797 *
1798 * @param[in] mi to check.
1799 * @return
1800 * - true if the module instance should be bootstrapped.
1801 * - false if the module instance has already been bootstrapped.
1802 */
1807
1808/** Should we instantiate this module instance?
1809 *
1810 * @param[in] mi to check.
1811 * @return
1812 * - true if the module instance should be instantiated.
1813 * - false if the module instance has already been instantiated.
1814 */
1819
1820/** Should we instantiate this module instance in a new thread?
1821 *
1822 * @param[in] mi to check.
1823 * @return
1824 * - true if the module instance should be instantiated in a new thread.
1825 * - false if the module instance has already been instantiated in a new thread.
1826 */
1831
1832/** Set a new bootstrap/instantiate state for a list
1833 *
1834 * @param[in] ml To set the state for.
1835 * @param[in] mask New state.
1836 */
1841
1842/** Allocate a new module list
1843 *
1844 * This is used to instantiate and destroy modules in distinct phases
1845 * for example, we may need to load all proto modules before rlm modules.
1846 *
1847 * If the list is freed all module instance data will be freed.
1848 * If no more instances of the module exist the module be unloaded.
1849 *
1850 * @param[in] ctx To allocate the list in.
1851 * @param[in] type of the list. Controls whether this is a global
1852 * module list, or a per-thread list containing
1853 * variants of an existing module.
1854 * @param[in] name of the list. Used for debugging.
1855 * @param[in] write_protect Whether to write protect the module data
1856 * after instantiation and bootstrapping.
1857 * @return A new module list.
1858 */
1860 char const *name, bool write_protect)
1861{
1862 module_list_t *ml;
1863
1864 /*
1865 * These callbacks are NOT optional, the rest are.
1866 */
1867 fr_assert(type->thread.data_add);
1868 fr_assert(type->thread.data_get);
1869 fr_assert(type->thread.data_del);
1870
1871 MEM(ml = talloc_zero(ctx, module_list_t));
1872 ml->type = type;
1873
1874 ml->thread_data_get = type->thread.data_get; /* Cache for access outside of the compilation unit */
1875 MEM(ml->name = talloc_typed_strdup(ml, name));
1878 talloc_set_destructor(ml, _module_list_free);
1879
1880 if (ml->type->init && (ml->type->init(ml) < 0)) {
1881 talloc_free(ml);
1882 return NULL;
1883 }
1884 ml->write_protect = write_protect;
1885
1886 return ml;
1887}
1888
1889static int _module_dl_loader_init(void *uctx)
1890{
1892
1893 /*
1894 * Ensure the common library tracking
1895 * tree is in place...
1896 */
1898
1899 return 0;
1900}
1901
1902static int _module_dl_loader_free(UNUSED void *uctx)
1903{
1904 if (talloc_free(dl_modules) < 0) return -1;
1905 dl_modules = NULL;
1906 return 0;
1907}
1908
1909/** Perform global initialisation for modules
1910 *
1911 */
1912void modules_init(char const *lib_dir)
1913{
1914 /*
1915 * Create the global module heap we use for
1916 * common indexes in the thread-specific
1917 * heaps.
1918 */
1920}
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:221
#define fr_atexit_global_once(_init, _free, _uctx)
Definition atexit.h:211
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:483
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:112
#define unlikely(_x)
Definition build.h:381
#define UNUSED
Definition build.h:315
int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth)
Definition cf_file.c:3554
int cf_section_parse(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs)
Parse a configuration section into user-supplied variables.
Definition cf_parse.c:1151
int cf_section_parse_pass2(void *base, CONF_SECTION *cs)
Fixup xlat expansions and attributes.
Definition cf_parse.c:1259
void const * uctx
User data accessible by the cf_parse_t func.
Definition cf_parse.h:618
#define cf_section_rules_push(_cs, _rule)
Definition cf_parse.h:690
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:595
Common header for all CONF_* types.
Definition cf_priv.h:49
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1185
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1171
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1028
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:684
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:664
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1594
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#define cf_lineno(_cf)
Definition cf_util.h:104
#define cf_data_add(_cf, _data, _name, _free)
Definition cf_util.h:255
#define cf_parent(_cf)
Definition cf_util.h:101
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:296
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:140
#define cf_filename(_cf)
Definition cf_util.h:107
#define cf_log_debug(_cf, _fmt,...)
Definition cf_util.h:292
fr_command_register_hook_t fr_command_register_hook
Definition command.c:42
bool fr_command_strncmp(const char *word, const char *name)
Definition command.c:2906
int argc
current argument count
Definition command.h:39
char const * parent
e.g. "show module"
Definition command.h:52
#define CMD_TABLE_END
Definition command.h:62
char const ** argv
text version of commands
Definition command.h:42
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:210
#define FR_FAULT_LOG(_fmt,...)
Definition debug.h:49
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:156
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:41
fr_table_num_sorted_t const dl_module_type_prefix[]
Name prefixes matching the types of loadable module.
Definition dl_module.c:57
dl_module_t * dl_module_alloc(dl_module_t const *parent, char const *name, dl_module_type_t type)
Load a module library using dlopen() or return a previously loaded module from the cache.
Definition dl_module.c:315
int dl_module_free(dl_module_t *dl_module)
Free a dl_module (when there are no more references to it)
Definition dl_module.c:281
dl_module_loader_t * dl_module_loader_init(char const *lib_dir)
Initialise structures needed by the dynamic linker.
Definition dl_module.c:532
Wrapper struct around dl_loader_t.
Definition dl_module.c:47
dl_module_type_t
Definition dl_module.h:65
@ DL_MODULE_TYPE_PROTO
Protocol module.
Definition dl_module.h:67
@ DL_MODULE_TYPE_SUBMODULE
Driver (or method in the case of EAP)
Definition dl_module.h:69
@ DL_MODULE_TYPE_MODULE
Standard loadable module.
Definition dl_module.h:66
@ DL_MODULE_TYPE_PROCESS
protocol processor.
Definition dl_module.h:68
int global_lib_init(void)
Initialise the global list of external libraries.
Definition global_lib.c:204
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
Definition heap.c:146
int fr_heap_extract(fr_heap_t **hp, void *data)
Remove a node from the heap.
Definition heap.c:239
unsigned int fr_heap_index_t
Definition heap.h:80
#define fr_heap_alloc(_ctx, _cmp, _type, _field, _init)
Creates a heap that can be used with non-talloced elements.
Definition heap.h:100
static bool fr_heap_entry_inserted(fr_heap_index_t heap_idx)
Check if an entry is inserted into a heap.
Definition heap.h:124
static unsigned int fr_heap_num_elements(fr_heap_t *h)
Return the number of elements in the heap.
Definition heap.h:179
#define fr_heap_foreach(_heap, _type, _data)
Iterate over the contents of a heap.
Definition heap.h:205
The main heap structure.
Definition heap.h:66
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG3(_fmt,...)
Definition log.h:266
#define DEBUG4(_fmt,...)
Definition log.h:267
talloc_free(reap)
Stores all information relating to an event list.
Definition event.c:411
static char const * mod_name(fr_listen_t *li)
Definition master.c:2750
unsigned char uint8_t
ssize_t fr_slen_t
#define UINT8_MAX
#define MODULE_DETACH_CTX(_mi)
Wrapper to create a module_detach_ctx_t as a compound literal.
Definition module_ctx.h:164
#define MODULE_THREAD_INST_CTX(_mi, _thread, _el)
Wrapper to create a module_thread_inst_ctx_t as a compound literal.
Definition module_ctx.h:176
#define MODULE_INST_CTX(_mi)
Wrapper to create a module_inst_ctx_t as a compound literal.
Definition module_ctx.h:158
Temporary structure to hold arguments for thread_instantiation calls.
Definition module_ctx.h:63
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:80
#define DEBUG2(fmt,...)
Definition radclient.h:43
static rs_t * conf
Definition radsniff.c:53
void * fr_rb_iter_init_inorder(fr_rb_iter_inorder_t *iter, fr_rb_tree_t *tree)
Initialise an in-order iterator.
Definition rb.c:824
void fr_rb_iter_delete_inorder(fr_rb_iter_inorder_t *iter)
Remove the current node from the tree.
Definition rb.c:898
void * fr_rb_iter_next_inorder(fr_rb_iter_inorder_t *iter)
Return the next node.
Definition rb.c:850
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
bool fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
Definition rb.c:741
#define fr_rb_inline_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black tree.
Definition rb.h:271
static bool fr_rb_node_inline_in_tree(fr_rb_node_t const *node)
Check to see if an item is in a tree by examining its inline fr_rb_node_t.
Definition rb.h:314
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:321
static uint32_t mask
Definition rbmonkey.c:39
fr_table_num_sorted_t const rcode_table[]
Definition rcode.c:35
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:51
static char const * name
#define fr_sbuff_start(_sbuff_or_marker)
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_used(_sbuff_or_marker)
#define FR_SBUFF_IN_STRCPY_RETURN(...)
#define FR_SBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
module_instance_t * mi
As opposed to the thread local inst.
Definition module.h:356
char const * name
Instance name e.g. user_database.
Definition module.h:335
@ MODULE_TYPE_DYNAMIC_UNSAFE
Instances of this module cannot be created at runtime.
Definition module.h:52
@ MODULE_TYPE_THREAD_UNSAFE
Module is not threadsafe.
Definition module.h:48
module_flags_t flags
Flags that control how a module starts up and how a module is called.
Definition module.h:227
fr_rb_node_t name_node
Entry in the name tree.
Definition module.h:310
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:329
size_t inst_size
Size of the module's instance data.
Definition module.h:203
module_detach_t detach
Clean up module resources from the instantiation pahses.
Definition module.h:223
bool force
Force the module to return a specific code.
Definition module.h:297
void * data
Module's instance data.
Definition module.h:271
module_instance_state_t state
What's been done with this module so far.
Definition module.h:328
module_instance_t const * parent
Parent module's instance (if any).
Definition module.h:337
module_thread_instantiate_t thread_instantiate
Callback to populate a new module thread instance data.
Definition module.h:230
module_instantiate_t instantiate
Callback to allow the module to register any per-instance resources like sockets and file handles.
Definition module.h:218
void * boot
Data allocated during the boostrap phase.
Definition module.h:274
TALLOC_CTX * ctx
ctx data is allocated in.
Definition module.h:254
module_instance_state_t mask
Prevent phases from being executed.
Definition module.h:387
module_thread_instance_t *(* module_list_thread_data_get_t)(module_instance_t const *mi)
Callback to retrieve thread-local data for a module.
Definition module.h:372
#define MODULE_INSTANCE_LEN_MAX
The maximum size of a module instance.
Definition module.h:147
void * data
Thread specific instance data.
Definition module.h:352
module_data_pool_t inst_pool
Data to allow mprotect state toggling for instance data.
Definition module.h:319
bool write_protect
If true, pages containing module boot or instance data will be write protected after bootstrapping an...
Definition module.h:394
rlm_rcode_t code
Code module will return when 'force' has has been set to true.
Definition module.h:300
char const * boot_type
talloc type to assign to bootstrap data.
Definition module.h:201
size_t len
How much data we need mprotect to protect.
Definition module.h:256
char const * inst_type
talloc type to assign to instance data.
Definition module.h:204
module_data_pool_t boot_pool
Data to allow mprotect state toggling for bootstrap data.
Definition module.h:321
module_detach_t unstrap
Clean up module resources from both the bootstrap phase.
Definition module.h:225
static module_thread_instance_t * module_thread(module_instance_t const *mi)
Retrieve module/thread specific instance for a module.
Definition module.h:481
module_instance_state_t
What state the module instance is currently in.
Definition module.h:243
@ MODULE_INSTANCE_INSTANTIATED
Module instance has been bootstrapped and instantiated.
Definition module.h:246
@ MODULE_INSTANCE_NO_THREAD_INSTANTIATE
Not set internally, but can be used to prevent thread instantiation for certain modules.
Definition module.h:248
@ MODULE_INSTANCE_BOOTSTRAPPED
Module instance has been bootstrapped, but not yet instantiated.
Definition module.h:244
fr_rb_tree_t * data_tree
Modules indexed by data.
Definition module.h:391
uint32_t number
Unique module number.
Definition module.h:312
module_list_thread_data_get_t thread_data_get
Callback to get thread-specific data.
Definition module.h:413
char const * thread_inst_type
talloc type to assign to thread instance data.
Definition module.h:236
module_instantiate_t bootstrap
Callback to allow the module to register any global resources like xlat functions and attributes.
Definition module.h:206
fr_rb_node_t data_node
Entry in the data tree.
Definition module.h:311
module_thread_detach_t thread_detach
Callback to free thread-specific resources associated < with a module.
Definition module.h:232
void * uctx
Extra data passed to module_instance_alloc.
Definition module.h:339
void * start
Start address which may be passed to mprotect.
Definition module.h:255
size_t boot_size
Size of the module's bootstrap data.
Definition module.h:200
module_list_t * ml
Module list this instance belongs to.
Definition module.h:309
size_t thread_inst_size
Size of the module's thread-specific instance data.
Definition module.h:235
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
Definition module.h:198
char const * name
Friendly list identifier.
Definition module.h:386
fr_event_list_t * el
Event list associated with this thread.
Definition module.h:354
module_list_type_t const * type
Type of module list.
Definition module.h:412
uint32_t last_number
Last identifier assigned to a module instance.
Definition module.h:389
fr_rb_tree_t * name_tree
Modules indexed by name.
Definition module.h:390
module_t * exported
Public module structure.
Definition module.h:276
pthread_mutex_t mutex
Used prevent multiple threads entering a thread unsafe module simultaneously.
Definition module.h:283
Module instance data.
Definition module.h:265
A list of modules.
Definition module.h:385
Struct exported by a rlm_* module.
Definition module.h:195
Per thread per instance data.
Definition module.h:347
static int mltl_thread_data_add(module_thread_instance_t *ti)
Definition module.c:575
int module_instance_data_protect(module_instance_t const *mi)
Mark module data as read only.
Definition module.c:701
static void module_thread_detach(module_thread_instance_t *ti)
Definition module.c:991
void module_list_debug(module_list_t const *ml)
Print the contents of a module list.
Definition module.c:623
module_list_type_t const module_list_type_thread_local
Callbacks for a thread local list.
Definition module.c:590
void modules_init(char const *lib_dir)
Perform global initialisation for modules.
Definition module.c:1912
bool module_instance_skip_thread_instantiate(module_instance_t *mi)
Should we instantiate this module instance in a new thread?
Definition module.c:1827
static int module_data_unprotect(module_instance_t const *mi, module_data_pool_t const *pool)
Unprotect module data.
Definition module.c:679
static int mlg_init(UNUSED module_list_t *ml)
Global initialisation for index heap and module array.
Definition module.c:400
static int _module_dl_loader_free(UNUSED void *uctx)
Definition module.c:1902
module_list_free_t free
Free any global structures required for thread-local lookups.
Definition module.c:331
static int _module_list_free(module_list_t *ml)
Free all modules loaded by the server.
Definition module.c:1775
module_thread_instance_t * ti
Thread-specific data.
Definition module.c:555
static int _module_instance_free(module_instance_t *mi)
Free module's instance data, and any xlats or paircmps.
Definition module.c:1461
module_instance_t * module_instance_by_name(module_list_t const *ml, module_instance_t const *parent, char const *asked_name)
Find an existing module instance by its name and parent.
Definition module.c:905
static int module_data_protect(module_instance_t *mi, module_data_pool_t *pool)
Protect module data.
Definition module.c:655
static void module_detach_parent(module_instance_t *mi)
Detach the shallowest parent first.
Definition module.c:1435
static int _module_thread_inst_list_free(void *tilp)
Free the thread local heap on exit.
Definition module.c:444
void(* module_list_free_t)(module_list_t *ml)
Callback to free any global structures associated with the module list.
Definition module.c:253
void module_list_mask_set(module_list_t *ml, module_instance_state_t mask)
Set a new bootstrap/instantiate state for a list.
Definition module.c:1837
module_instance_t * module_instance_copy(module_list_t *dst, module_instance_t const *src, char const *inst_name)
Duplicate a module instance, placing it in a new module list.
Definition module.c:1536
module_instance_t * module_instance_alloc(module_list_t *ml, module_instance_t const *parent, dl_module_type_t type, char const *mod_name, char const *inst_name, module_instance_state_t init_state)
Allocate a new module and add it to a module list for later bootstrap/instantiation.
Definition module.c:1653
static int cmd_show_module_status(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
Definition module.c:177
static int8_t module_instance_data_cmp(void const *one, void const *two)
Compare module's by their private instance data.
Definition module.c:826
struct module_list_type_s::@65 thread
Callbacks to manage thread-local data.
fr_slen_t module_instance_name_from_conf(char const **name, CONF_SECTION *conf)
Avoid boilerplate when setting the module instance name.
Definition module.c:737
static int8_t module_instance_name_cmp(void const *one, void const *two)
Compare module instances by parent and name.
Definition module.c:792
void modules_thread_detach(module_list_t *ml)
Remove thread-specific data for a given module list.
Definition module.c:1011
static int mlg_data_add(module_instance_t *mi)
Add the unique index value so we can do thread local lookups.
Definition module.c:415
module_list_data_add_t data_add
Record that module data has been added.
Definition module.c:335
module_list_init_t init
Initialise any global structures required for thread-local lookups.
Definition module.c:330
int modules_thread_instantiate(TALLOC_CTX *ctx, module_list_t const *ml, fr_event_list_t *el)
Creates per-thread instance data for modules which need it.
Definition module.c:1162
bool module_instance_skip_instantiate(module_instance_t *mi)
Should we instantiate this module instance?
Definition module.c:1815
static int cmd_show_module_list(FILE *fp, UNUSED FILE *fp_err, UNUSED void *uctx, UNUSED fr_cmd_info_t const *info)
Definition module.c:166
static int module_name_tab_expand(UNUSED TALLOC_CTX *talloc_ctx, UNUSED void *uctx, fr_cmd_info_t *info, int max_expansions, char const **expansions)
Definition module.c:140
void(* module_list_data_del_t)(module_instance_t *mi)
Callback to del data for a module.
Definition module.c:271
size_t inst_size
Size of talloc chunk to allocate for the module_instance_t.
Definition module.c:333
int(* module_list_thread_data_add_t)(module_thread_instance_t *ti)
Callback to add thread-local data for a module.
Definition module.c:302
module_instance_t * module_instance_by_data(module_list_t const *ml, void const *data)
Find an existing module instance by its private instance data.
Definition module.c:959
static void module_instance_data_alloc(TALLOC_CTX *ctx, module_data_pool_t *pool_out, void **out, module_instance_t *mi, size_t size, char const *type)
Allocate module instance data.
Definition module.c:1556
int modules_instantiate(module_list_t const *ml)
Completes instantiation of modules.
Definition module.c:1283
int(* module_list_init_t)(module_list_t *ml)
Callback to initialise any global structures required for the module list.
Definition module.c:247
static module_thread_instance_t * mltl_thread_data_get(module_instance_t const *mi)
Definition module.c:569
module_list_t * module_list_alloc(TALLOC_CTX *ctx, module_list_type_t const *type, char const *name, bool write_protect)
Allocate a new module list.
Definition module.c:1859
static void mlg_thread_data_del(module_thread_instance_t *ti)
Definition module.c:528
static int mlg_thread_data_add(module_thread_instance_t *ti)
Definition module.c:521
static int cmd_set_module_status(UNUSED FILE *fp, FILE *fp_err, void *ctx, fr_cmd_info_t const *info)
Definition module.c:191
static int _module_dl_loader_init(void *uctx)
Definition module.c:1889
void(* module_list_thread_free_t)(module_list_t *ml)
Callback to free thread-local structures, called once per thread as the thread is being destroyed.
Definition module.c:290
void module_instance_uctx_set(module_instance_t *mi, void *uctx)
Set the uctx pointer for a module instance.
Definition module.c:1621
static fr_slen_t module_instance_name(TALLOC_CTX *ctx, char **out, module_instance_t const *parent, char const *inst_name)
Generate a module name from the module's name and its parents.
Definition module.c:1398
static int cmd_show_module_config(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
Definition module.c:129
void(* module_list_thread_data_del_t)(module_thread_instance_t *ti)
Callback to remove thread-local data for a module.
Definition module.c:311
fr_slen_t module_instance_name_valid(char const *inst_name)
Check to see if a module instance name is valid.
Definition module.c:1593
return count
Definition module.c:163
int module_instance_data_unprotect(module_instance_t const *mi)
Mark module data as read/write.
Definition module.c:713
static int _module_thread_inst_free(module_thread_instance_t *ti)
Callback to free thread local data.
Definition module.c:1045
static int _mlg_global_free(UNUSED void *uctx)
Free the global module index.
Definition module.c:383
int module_thread_instantiate(TALLOC_CTX *ctx, module_instance_t *mi, fr_event_list_t *el)
Allocate thread-local instance data for a module.
Definition module.c:1085
module_list_data_del_t data_del
Record that module data has been removed.
Definition module.c:336
module_list_type_t const module_list_type_global
Callbacks for a global module list.
Definition module.c:536
static dl_module_loader_t * dl_modules
dl module tracking
Definition module.c:238
fr_cmd_table_t module_cmd_list_table[]
Definition module.c:99
static module_thread_instance_t * mlg_thread_data_get(module_instance_t const *mi)
Retrieve the thread-specific data for a module from the thread-local array of instance data.
Definition module.c:492
fr_heap_index_t inst_idx
Entry in the bootstrap/instantiation heap.
Definition module.c:355
int(* module_list_thread_init_t)(TALLOC_CTX **ctx, module_list_t const *ml)
Callback to initialise a list for thread-local data, called once per thread.
Definition module.c:284
module_thread_instance_t * module_thread_by_data(module_list_t const *ml, void const *data)
Retrieve module/thread specific instance data for a module.
Definition module.c:982
int modules_bootstrap(module_list_t const *ml)
Bootstrap any modules which have not been bootstrapped already.
Definition module.c:1374
fr_cmd_table_t module_cmd_table[]
Definition module.c:66
module_instance_t mi
Common module instance fields. Must come first.
Definition module.c:353
static _Thread_local module_thread_instance_t ** mlg_thread_inst_list
An array of thread-local module lists.
Definition module.c:58
int module_instantiate(module_instance_t *instance)
Manually complete module setup by calling its instantiate function.
Definition module.c:1197
static int mlg_thread_init(UNUSED TALLOC_CTX **ctx, UNUSED module_list_t const *ml)
Allocate a thread-local array to hold thread data for each module thats been instantiated.
Definition module.c:468
bool module_instance_skip_bootstrap(module_instance_t *mi)
Should we bootstrap this module instance?
Definition module.c:1803
module_instance_t mi
Common module instance fields. Must come first.
Definition module.c:554
static int _mlg_global_init(UNUSED void *uctx)
Initialise the global module index.
Definition module.c:391
int module_submodule_parse(UNUSED TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic callback for conf_parser_t to load a submodule.
Definition module.c:849
int(* module_list_data_add_t)(module_instance_t *mi)
Callback to add data for a module.
Definition module.c:264
module_instance_t * module_instance_root(module_instance_t const *child)
Find the module's shallowest parent.
Definition module.c:937
size_t list_size
Size of talloc_chunk to allocate for the module_list_t.
Definition module.c:328
static void mlg_data_del(module_instance_t *mi)
Definition module.c:429
static void mltl_thread_data_del(module_thread_instance_t *ti)
Definition module.c:582
bool const module_instance_allowed_chars[UINT8_MAX+1]
Chars that are allowed in a module instance name.
Definition module.c:216
static void mltl_mlg_data_del(module_instance_t *mi)
Definition module.c:559
int module_instance_conf_parse(module_instance_t *mi, CONF_SECTION *conf)
Covert a CONF_SECTION into parsed module instance data.
Definition module.c:766
char const * module_instance_root_prefix_str(module_instance_t const *mi)
Return the prefix string for the deepest module.
Definition module.c:727
static fr_heap_t * mlg_index
Heap of all lists/modules used to get a common index with mlg_thread->inst_list.
Definition module.c:50
static int8_t _mlg_module_instance_cmp(void const *one, void const *two)
Sort module instance data first by list then by number.
Definition module.c:366
int module_bootstrap(module_instance_t *mi)
Manually complete module bootstrap by calling its instantiate function.
Definition module.c:1310
void module_instance_debug(module_instance_t const *mi)
Print debugging information for a module.
Definition module.c:605
A slightly larger module_instance structure to hold the module instance and thread instance.
Definition module.c:553
Structure to hold callbacks for a module list type.
Definition module.c:327
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:564
TALLOC_CTX * talloc_page_aligned_pool(TALLOC_CTX *ctx, void **start, size_t *end_len, unsigned int headers, size_t size)
Return a page aligned talloc memory pool.
Definition talloc.c:312
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:445
Functions which we wish were included in the standard talloc distribution.
#define talloc_get_type_abort_const
Definition talloc.h:282
static fr_event_list_t * el
static fr_slen_t parent
Definition pair.h:851
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:577
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
static fr_slen_t data
Definition value.h:1265
static size_t char ** out
Definition value.h:997
void xlat_func_unregister_module(module_instance_t const *inst)
Definition xlat_func.c:533
void xlat_func_unregister(char const *name)
Unregister an xlat function.
Definition xlat_func.c:519