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