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