Linux Netfilter development
 help / color / mirror / Atom feed
From: Soren Hansen <soren@ubuntu.com>
To: netfilter-devel@vger.kernel.org
Subject: Automatically load modules in iptables-save
Date: Mon, 16 Mar 2009 12:42:24 +0100	[thread overview]
Message-ID: <20090316114224.GE31952@ralph.linux2go.dk> (raw)

[-- Attachment #1: Type: text/plain, Size: 2741 bytes --]

If the iptables modules are not loaded when iptables-save is run,
iptables-save will fail, because it can't open the relevant files in
/proc.  This patch makes iptables-save attempt to load the modules, and
then retries.


Index: iptables-1.4.1.1/iptables-save.c
===================================================================
--- iptables-1.4.1.1.orig/iptables-save.c	2009-03-16 11:19:25.233429856 +0100
+++ iptables-1.4.1.1/iptables-save.c	2009-03-16 12:42:10.200735326 +0100
@@ -28,17 +28,22 @@
 	{.name = "counters", .has_arg = false, .val = 'c'},
 	{.name = "dump",     .has_arg = false, .val = 'd'},
 	{.name = "table",    .has_arg = true,  .val = 't'},
+	{.name = "modprobe", .has_arg = true,  .val = 'M'},
 	{NULL},
 };
 
 /* Debugging prototype. */
-static int for_each_table(int (*func)(const char *tablename))
+static int for_each_table(int (*func)(const char *tablename), const char *modprobe)
 {
 	int ret = 1;
 	FILE *procfile = NULL;
 	char tablename[IPT_TABLE_MAXNAMELEN+1];
 
 	procfile = fopen("/proc/net/ip_tables_names", "r");
+	if (!procfile) {
+		load_xtables_ko(modprobe, 0);
+		procfile = fopen("/proc/net/ip_tables_names", "r");
+	}
 	if (!procfile)
 		exit_error(OTHER_PROBLEM,
 			   "Unable to open /proc/net/ip_tables_names: %s\n",
@@ -57,15 +62,20 @@
 }
 
 
-static int do_output(const char *tablename)
+static int do_output(const char *tablename, const char *modprobe)
 {
 	iptc_handle_t h;
 	const char *chain = NULL;
 
 	if (!tablename)
-		return for_each_table(&do_output);
+		return for_each_table(&do_output, modprobe);
 
 	h = iptc_init(tablename);
+	if (!h) {
+		load_xtables_ko(modprobe, 0);
+		h = iptc_init(tablename);
+	}
+
 	if (!h)
 		exit_error(OTHER_PROBLEM, "Can't initialize: %s\n",
 			   iptc_strerror(errno));
@@ -134,6 +144,7 @@
 #endif
 {
 	const char *tablename = NULL;
+	const char *modprobe = NULL;
 	int c;
 
 	program_name = "iptables-save";
@@ -152,7 +163,7 @@
 	init_extensions();
 #endif
 
-	while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
+	while ((c = getopt_long(argc, argv, "bcdt:M:", options, NULL)) != -1) {
 		switch (c) {
 		case 'b':
 			show_binary = 1;
@@ -166,8 +177,11 @@
 			/* Select specific table. */
 			tablename = optarg;
 			break;
+		case 'M':
+			modprobe = optarg;
+			break;
 		case 'd':
-			do_output(tablename);
+			do_output(tablename, modprobe);
 			exit(0);
 		}
 	}
@@ -177,5 +191,5 @@
 		exit(1);
 	}
 
-	return !do_output(tablename);
+	return !do_output(tablename, modprobe);
 }


-- 
Soren Hansen                 | 
Lead Virtualisation Engineer | Ubuntu Server Team
Canonical Ltd.               | http://www.ubuntu.com/

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 315 bytes --]

             reply	other threads:[~2009-03-16 11:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-16 11:42 Soren Hansen [this message]
  -- strict thread matches above, loose matches on Subject: below --
2009-03-16 17:10 Automatically load modules in iptables-save Soren Hansen
2009-03-19 12:55 ` Soren Hansen
2009-03-19 13:05   ` Patrick McHardy
2009-03-19 13:50     ` Soren Hansen
2009-03-19 13:55       ` Jan Engelhardt
2009-03-19 13:58         ` Patrick McHardy
2009-03-19 14:29         ` Soren Hansen
2009-03-19 14:31           ` Jan Engelhardt
2009-03-19 15:43             ` Soren Hansen
2009-03-19 16:02               ` Jan Engelhardt
2009-03-19 17:00                 ` Soren Hansen
2009-03-20 18:42                 ` Patrick McHardy
2009-03-19 16:43               ` Soren Hansen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090316114224.GE31952@ralph.linux2go.dk \
    --to=soren@ubuntu.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox