iw: fix "iw reg get" double output

Axel reports that running "iw reg get" results in the (global)
output being printed twice. The reason for this is that we try
to use the dump facility, and if that succeeds we also do the
get command.

To prevent this, allow handlers to return HANDLER_RET_DONE, in
which case the command will be treated as successful but will
not actually execute another netlink command.

Reported-by: Axel Köllhofer <AxelKoellhofer@web.de>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
diff --git a/iw.c b/iw.c
index 0f511d9..fe394b2 100644
--- a/iw.c
+++ b/iw.c
@@ -575,6 +575,8 @@
 			usage_cmd(cmd);
 		else
 			usage(0, NULL);
+	} else if (err == HANDLER_RET_DONE) {
+		err = 0;
 	} else if (err < 0)
 		fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
 
diff --git a/iw.h b/iw.h
index d91a33e..b78e21b 100644
--- a/iw.h
+++ b/iw.h
@@ -38,6 +38,8 @@
 	II_WDEV,
 };
 
+#define HANDLER_RET_DONE 3
+
 struct cmd {
 	const char *name;
 	const char *args;
diff --git a/reg.c b/reg.c
index 1dca13a..cee0b5e 100644
--- a/reg.c
+++ b/reg.c
@@ -244,14 +244,14 @@
 	int err;
 
 	err = handle_cmd(state, CIB_NONE, 2, dump_args);
-	/* dump might fail since it's not supported on older kernels */
-	if (err == -EOPNOTSUPP) {
-		register_handler(print_reg_handler,
-			  NULL);
+	/*
+	 * dump might fail since it's not supported on older kernels,
+	 * in that case the handler is still registered already
+	 */
+	if (err == -EOPNOTSUPP)
 		return 0;
-	}
 
-	return err;
+	return err ?: HANDLER_RET_DONE;
 }
 COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get,
 	"Print out the kernel's current regulatory domain information.");