Skip to contentSkip to conversation

libxtables: Add exit_error cb to xtables_globals

PATCH Applied jamal 3 participants 7 messages Feb 10, 2009 – Feb 11, 2009 netfilter

lore
Share & tools
lore

jamal

author
#0 lore raw

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
index 1d33394..3a16651 100644
@@ -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
index 95be5f8..d0fc478 100644
@@ -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;
 }
 
#1 lore raw
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?

author
#2 lore raw
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_globals is defined in the program, and libxtables takes a pointer/reference to it during xtables_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 with xtables_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

#4 lore raw
In reply to jamal
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: input xtables_globals structure

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).

author
#5 lore raw
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" #endif

xtables.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_error invoked? 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

#6 lore raw
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" #endif

xtables.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_error invoked? 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

Ok.