libxtables: Add exit_error cb to xtables_globals
PATCH 1/1 libxtables: Add exit_error cb to xtables_globals
Suggestion from Jan. Building up to compiling most basic app..
cheers,
jamal
commit 2960e6aa4e7d9429aa81eff9636ad3524c9282e2
Author: Jamal Hadi Salim <[email protected]> Date: Mon Feb 9 16:46:18 2009 -0500
Introduce exit_error() as part of xtables_globals structure. When an application registers its xtables_globals definition and does not specify its exit_error() it gets assigned a basic version
include/xtables.h.in +9 -8
@@ -33,14 +33,6 @@ struct in_addr; -struct xtables_globals -{ - unsigned int option_offset; - char *program_version; - char *program_name; - struct option *opts; -}; - /* Include file for additions: new matches and targets. */ struct xtables_match {
@@ -191,6 +183,15 @@ enum xtables_exittype { XTF_ONE_ACTION, }; +struct xtables_globals +{ + unsigned int option_offset; + char *program_version; + char *program_name; + struct option *opts; + void (*exit_error)(enum xtables_exittype status, const char *msg, ...); +}; + extern const char *xtables_program_name; extern const char *xtables_modprobe_program; extern struct xtables_match *xtables_matches;
xtables.c +18 -1
@@ -46,7 +46,20 @@#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"#endif -struct xtables_globals *xt_params; +struct xtables_globals *xt_params =NULL ; + +void basic_exit_error (enum xtables_exittype status,const char *msg, ...) +{ +va_list args; + +va_start (args, msg); +fprintf (stderr,"%s v%s: " , xt_params->program_name, xt_params->program_version); +vfprintf (stderr, msg, args); +va_end (args); +fprintf (stderr,"\n" ); +exit (status); +} +/** * xtables_set_params - set the global parameters used by xtables * @xtp: input xtables_globals structure
@@ -65,6 +78,10 @@ int xtables_set_params(struct xtables_globals *xtp) } xt_params = xtp; + +if (!xt_params->exit_error) + xt_params->exit_error = basic_exit_error; +return 0 ; }
Quoted 2 lines · jamal
Suggestion from Jan. Building up to compiling most basic app..
I looked through this and wondered - how do we want to model the ownership of the structure?
For example, one possibility (which you did) is that xtables_globals is defined in the program, and libxtables takes a pointer/reference to it during xtables_set_params.
Alternatively, libxtables could collect the globals in a struct (defined in xtables.c), and programs override it at will, either by setting variables directly, like it is currently done with xtables_program_name, or by use of a function;
Any preference on which approach to choose?
Quoted 8 lines · Jan Engelhardt
I looked through this and wondered - how do we want to model the ownership of the structure?
For example, one possibility (which you did) is that
xtables_globalsis defined in the program, and libxtables takes a pointer/reference to it duringxtables_set_params.
Quoted 7 lines
Alternatively, libxtables could collect the globals in a struct (defined in
xtables.c), and programs override it at will, either by setting variables directly, like it is currently done withxtables_program_name, or by use of a function;Any preference on which approach to choose?
I think you could go either way.
Quoted 1 line
From a personal taste perspective (and maybe usability as
defined by someone who is lazy like myself), I would pick the option that makes me write the smallest amount of code - which is what the current patches provide.
[I dislike libcurl for example which makes me call all sorts of set() functions].
I would even wanna go further:
unify xtables_init(), xtables_set_nfproto() and xtables_set_params() i.e just have the app call xtables_init(&myxtables_params). If the app only declares myxtables_params and sets nothing in it, then xtables_init() provides it with the default parameters (sort of what i do with basic_exit_error)
cheers,
jamal
Patrick McHardy
AppliedQuoted 1 line · jamal
Suggestion from Jan. Building up to compiling most basic app..
Applied, thanks.
Quoted 14 lines · jamal
include/xtables.h.in +0
-8-struct xtables_globals -{ - unsigned int option_offset; - char *program_version; - char *program_name; - struct option *opts; -}; -/* Include file for additions: new matches and targets. */ struct
xtables_match
{include/xtables.h.in +0
-0@@ -191,6 +183,15 @@ enum xtables_exittype { XTF_ONE_ACTION, };
include/xtables.h.in enum xtables_exittype { · line 194
Quoted 10 lines
include/xtables.h.in +9
-0+struct xtables_globals +{ + unsigned int option_offset; + char *program_version; + char *program_name; + struct option *opts; + void (*exit_error)(enum xtables_exittype status, const char *msg, ...); +}; +
Why move the struct now.. makes it harder to figure out what changed. So right, exit_error.
xtables.c line 49
Quoted 5 lines
xtables.c +1
-1#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"#endif -struct xtables_globals *xt_params; +struct xtables_globals *xt_params =NULL ;
Do not initialize static members - this takes up extra space and adds no benefit. (zeroed anyway even in .bss)
Quoted 16 lines
xtables.c +12
-0+void basic_exit_error (enum xtables_exittype status,const char *msg, ...) +{ +va_list args; + +va_start (args, msg); +fprintf (stderr,"%s v%s: " , xt_params->program_name, xt_params->program_version); +vfprintf (stderr, msg, args); +va_end (args); +fprintf (stderr,"\n" ); +exit (status); +} +/**
*xtables_set_params- set the global parameters used by xtables
* @xtp: inputxtables_globalsstructure
xtables.c int xtables_set_params(struct xtables_globals *xtp) · line 87
Quoted 10 lines
xtables.c +4
-0@@ -65,6 +78,10 @@ int xtables_set_params(struct xtables_globals *xtp) } xt_params = xtp; + +if (!xt_params->exit_error) + xt_params->exit_error = basic_exit_error; +return 0 ; }
When is basic_exit_error invoked? In this patch alone, the users are missing (or the #define).
Quoted 15 lines · Jan Engelhardt
include/xtables.h.in enum xtables_exittype { · line 194
Quoted 10 lines · jamal
include/xtables.h.in +9
-0+struct xtables_globals +{ + unsigned int option_offset; + char *program_version; + char *program_name; + struct option *opts; + void (*exit_error)(enum xtables_exittype status, const char *msg, ...); +}; +Why move the struct now.. makes it harder to figure out what changed. So right,
exit_error.
Right (because of xtables_exittype)
Quoted 8 lines
xtables.c line 49
Quoted 5 lines
#define
PROC_SYS_MODPROBE"/proc/sys/kernel/modprobe" #endifxtables.c +1
-1-struct xtables_globals *xt_params; +struct xtables_globals *xt_params =NULL ;Do not initialize static members - this takes up extra space and adds no benefit. (zeroed anyway even in .bss)
Ok. Do i need to regenerate the patch or send on top?
Quoted 2 lines
When is
basic_exit_errorinvoked? In this patch alone, the users are missing (or the #define).
tc/m_xt.c was at this point - but not any of the native iptables apps. Later on patches iptables-xml uses the basic_exit_error() as well
cheers,
jamal
Quoted 5 lines · jamal
Quoted 3 lines
Why move the struct now.. makes it harder to figure out what changed. So right,
exit_error.Right (because of
xtables_exittype)
Ok, makes sense. (I guess git got in a dilemma and did not figure out the enum was moved ;-)
Quoted 10 lines
Quoted 8 lines
xtables.c line 49
Quoted 5 lines
#define
PROC_SYS_MODPROBE"/proc/sys/kernel/modprobe" #endifxtables.c +1
-1-struct xtables_globals *xt_params; +struct xtables_globals *xt_params =NULL ;Do not initialize static members - this takes up extra space and adds no benefit. (zeroed anyway even in .bss)
Ok. Do i need to regenerate the patch or send on top?
As long as it's not applied, regen seems best.
Quoted 5 lines
Quoted 2 lines
When is
basic_exit_errorinvoked? In this patch alone, the users are missing (or the #define).
tc/m_xt.cwas at this point - but not any of the native iptables apps. Later on patches iptables-xml uses thebasic_exit_error()as well
Ok.