3636#include <stdio.h>
3737#endif
3838
39- #define CTL_MAX_ENTRIES 100
40-
4139#define MAX_CONFIG_FILE_LEN (1 << 20) /* 1 megabyte */
4240
4341#define CTL_STRING_QUERY_SEPARATOR ";"
4947static int ctl_global_first_free = 0 ;
5048static umf_ctl_node_t CTL_NODE (global )[CTL_MAX_ENTRIES ];
5149
52- /*
53- * This is the top level node of the ctl tree structure. Each node can contain
54- * children and leaf nodes.
55- *
56- * Internal nodes simply create a new path in the tree whereas child nodes are
57- * the ones providing the read/write functionality by the means of callbacks.
58- *
59- * Each tree node must be NULL-terminated, CTL_NODE_END macro is provided for
60- * convenience.
61- */
62- struct ctl {
63- umf_ctl_node_t root [CTL_MAX_ENTRIES ];
64- int first_free ;
65- };
66-
6750void * Zalloc (size_t sz ) {
6851 void * ptr = umf_ba_global_alloc (sz );
6952 if (ptr ) {
@@ -81,22 +64,31 @@ char *Strdup(const char *s) {
8164 return p ;
8265}
8366
84- umf_result_t umfCtlGet (const char * name , void * ctx , void * arg ) {
85- if (name == NULL || arg == NULL || ctx == NULL ) {
67+ umf_result_t umfCtlGet (const char * name , void * ctx , void * arg , size_t size ) {
68+ if (name == NULL || arg == NULL ) {
69+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
70+ }
71+ // using ctx is disallowed for default settings
72+ if (ctx && strstr (name , ".default." )) {
8673 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
8774 }
8875 return ctl_query (NULL , ctx , CTL_QUERY_PROGRAMMATIC , name , CTL_QUERY_READ ,
89- arg )
76+ arg , size )
9077 ? UMF_RESULT_ERROR_UNKNOWN
9178 : UMF_RESULT_SUCCESS ;
9279}
9380
94- umf_result_t umfCtlSet (const char * name , void * ctx , void * arg ) {
95- if (name == NULL || arg == NULL || ctx == NULL ) {
81+ umf_result_t umfCtlSet (const char * name , void * ctx , void * arg , size_t size ) {
82+ // Context can be NULL when setting defaults
83+ if (name == NULL || arg == NULL || size == 0 ) {
84+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
85+ }
86+ // using ctx is disallowed for default settings
87+ if (ctx && strstr (name , ".default." )) {
9688 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
9789 }
9890 return ctl_query (NULL , ctx , CTL_QUERY_PROGRAMMATIC , name , CTL_QUERY_WRITE ,
99- arg )
91+ arg , size )
10092 ? UMF_RESULT_ERROR_UNKNOWN
10193 : UMF_RESULT_SUCCESS ;
10294}
@@ -106,7 +98,7 @@ umf_result_t umfCtlExec(const char *name, void *ctx, void *arg) {
10698 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
10799 }
108100 return ctl_query (NULL , ctx , CTL_QUERY_PROGRAMMATIC , name ,
109- CTL_QUERY_RUNNABLE , arg )
101+ CTL_QUERY_RUNNABLE , arg , 0 )
110102 ? UMF_RESULT_ERROR_UNKNOWN
111103 : UMF_RESULT_SUCCESS ;
112104}
@@ -287,10 +279,10 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
287279 */
288280static int ctl_exec_query_read (void * ctx , const umf_ctl_node_t * n ,
289281 umf_ctl_query_source_t source , void * arg ,
290- umf_ctl_index_utlist_t * indexes ,
282+ size_t size , umf_ctl_index_utlist_t * indexes ,
291283 const char * extra_name ,
292284 umf_ctl_query_type_t query_type ) {
293- (void )extra_name , ( void ) query_type ;
285+ (void )query_type ;
294286 assert (n != NULL );
295287 assert (n -> cb [CTL_QUERY_READ ] != NULL );
296288 assert (MAX_CTL_QUERY_TYPE != query_type );
@@ -300,7 +292,7 @@ static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
300292 return -1 ;
301293 }
302294
303- return n -> cb [CTL_QUERY_READ ](ctx , source , arg , indexes , NULL ,
295+ return n -> cb [CTL_QUERY_READ ](ctx , source , arg , size , indexes , extra_name ,
304296 MAX_CTL_QUERY_TYPE );
305297}
306298
@@ -309,10 +301,10 @@ static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
309301 */
310302static int ctl_exec_query_write (void * ctx , const umf_ctl_node_t * n ,
311303 umf_ctl_query_source_t source , void * arg ,
312- umf_ctl_index_utlist_t * indexes ,
304+ size_t size , umf_ctl_index_utlist_t * indexes ,
313305 const char * extra_name ,
314306 umf_ctl_query_type_t query_type ) {
315- (void )extra_name , ( void ) query_type ;
307+ (void )query_type ;
316308 assert (n != NULL );
317309 assert (n -> cb [CTL_QUERY_WRITE ] != NULL );
318310 assert (MAX_CTL_QUERY_TYPE != query_type );
@@ -327,8 +319,8 @@ static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
327319 return -1 ;
328320 }
329321
330- int ret = n -> cb [CTL_QUERY_WRITE ](ctx , source , real_arg , indexes , NULL ,
331- MAX_CTL_QUERY_TYPE );
322+ int ret = n -> cb [CTL_QUERY_WRITE ](ctx , source , real_arg , size , indexes ,
323+ extra_name , MAX_CTL_QUERY_TYPE );
332324 ctl_query_cleanup_real_args (n , real_arg , source );
333325
334326 return ret ;
@@ -339,31 +331,32 @@ static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
339331 */
340332static int ctl_exec_query_runnable (void * ctx , const umf_ctl_node_t * n ,
341333 umf_ctl_query_source_t source , void * arg ,
342- umf_ctl_index_utlist_t * indexes ,
334+ size_t size , umf_ctl_index_utlist_t * indexes ,
343335 const char * extra_name ,
344336 umf_ctl_query_type_t query_type ) {
345337 (void )extra_name , (void )query_type ;
346338 assert (n != NULL );
347339 assert (n -> cb [CTL_QUERY_RUNNABLE ] != NULL );
348340 assert (MAX_CTL_QUERY_TYPE != query_type );
349- return n -> cb [CTL_QUERY_RUNNABLE ](ctx , source , arg , indexes , NULL ,
350- MAX_CTL_QUERY_TYPE );
341+ return n -> cb [CTL_QUERY_RUNNABLE ](ctx , source , arg , size , indexes ,
342+ extra_name , MAX_CTL_QUERY_TYPE );
351343}
352344
353345static int ctl_exec_query_subtree (void * ctx , const umf_ctl_node_t * n ,
354346 umf_ctl_query_source_t source , void * arg ,
355- umf_ctl_index_utlist_t * indexes ,
347+ size_t size , umf_ctl_index_utlist_t * indexes ,
356348 const char * extra_name ,
357349 umf_ctl_query_type_t query_type ) {
358350 assert (n != NULL );
359351 assert (n -> cb [CTL_QUERY_SUBTREE ] != NULL );
360352 assert (MAX_CTL_QUERY_TYPE != query_type );
361- return n -> cb [CTL_QUERY_SUBTREE ](ctx , source , arg , indexes , extra_name ,
353+ return n -> cb [CTL_QUERY_SUBTREE ](ctx , source , arg , size , indexes , extra_name ,
362354 query_type );
363355}
364356
365357typedef int (* umf_ctl_exec_query_t )(void * ctx , const umf_ctl_node_t * n ,
366358 umf_ctl_query_source_t source , void * arg ,
359+ size_t size ,
367360 umf_ctl_index_utlist_t * indexes ,
368361 const char * extra_name ,
369362 umf_ctl_query_type_t query_type );
@@ -380,7 +373,8 @@ static umf_ctl_exec_query_t ctl_exec_query[MAX_CTL_QUERY_TYPE] = {
380373 * from the ctl tree
381374 */
382375int ctl_query (struct ctl * ctl , void * ctx , umf_ctl_query_source_t source ,
383- const char * name , umf_ctl_query_type_t type , void * arg ) {
376+ const char * name , umf_ctl_query_type_t type , void * arg ,
377+ size_t size ) {
384378 if (name == NULL ) {
385379 errno = EINVAL ;
386380 return -1 ;
@@ -416,11 +410,14 @@ int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
416410 errno = EINVAL ;
417411 goto out ;
418412 }
413+ const char * extra_name = & name [0 ];
414+ if (strstr (extra_name , "by_handle" ) != NULL ) {
415+ extra_name = & name [0 ] + name_offset ;
416+ }
419417
420- const char * extra_name = & name [0 ] + name_offset ;
421418 ret =
422419 ctl_exec_query [n -> type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type ](
423- ctx , n , source , arg , indexes , extra_name , type );
420+ ctx , n , source , arg , size , indexes , extra_name , type );
424421out :
425422 ctl_delete_indexes (indexes );
426423
@@ -487,7 +484,7 @@ static int ctl_load_config(struct ctl *ctl, void *ctx, char *buf) {
487484 }
488485
489486 r = ctl_query (ctl , ctx , CTL_QUERY_CONFIG_INPUT , name , CTL_QUERY_WRITE ,
490- value );
487+ value , 0 );
491488
492489 if (r < 0 && ctx != NULL ) {
493490 return -1 ;
0 commit comments