Skip to contentSkip to conversation

ip: return correct status from help command

PATCH In review Rose Wright 2 participants 4 messages Jun 21 – Jun 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ip: return correct status from help command

Currently, "ip help" or "ip -help" always returns an error code because usage() is used as a fall through on "ip" and defaults to stderr with -1.

This is a minor bug that breaks "ip help | grep" and other scripts that rely on standard exit codes. The fix is to pass the status code as a parameter into usage() and change stderr to stdout when needed.

---
 ip/ip.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
ip/ip.c +8 -7
index e4b71bde..ee4595e8 100644
@@ -52,11 +52,12 @@ const char *get_ip_lib_dir(void)
 	return lib_dir;
 }
 
-static void usage(void) __attribute__((noreturn));
+static void usage(int status) __attribute__((noreturn));
 
-static void usage(void)
+static void usage(int status)
 {
-	fprintf(stderr,
+	FILE *out = status == 0 ? stdout : stderr;
+	fprintf(out,
 		"Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
 		"       ip [ -force ] -batch filename\n"
 		"where  OBJECT := { address | addrlabel | fou | help | ila | ioam | l2tp | link |\n"
@@ -72,12 +73,12 @@ static void usage(void)
 		"                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
 		"                    -rc[vbuf] [size] | -n[etns] name | -N[umeric] | -a[ll] |\n"
 		"                    -c[olor]}\n");
-	exit(-1);
+	exit(status);
 }
 
 static int do_help(int argc, char **argv)
 {
-	usage();
+	usage(0);
 	return 0;
 }
 
@@ -279,7 +280,7 @@ int main(int argc, char **argv)
 			rcvbuf = size;
 		} else if (matches_color(opt, &color)) {
 		} else if (matches(opt, "-help") == 0) {
-			usage();
+			usage(0);
 		} else if (matches(opt, "-netns") == 0) {
 			NEXT_ARG();
 			if (netns_switch(argv[1]))
@@ -321,5 +322,5 @@ int main(int argc, char **argv)
 		return do_cmd(argv[1], argc-1, argv+1, true);
 
 	rtnl_close(&rth);
-	usage();
+	usage(-1);
 }
Signature · 1 line
2.54.0
#1 lore raw
Quoted 6 lines · On Sun, 21 Jun 2026 18:03:11 +0000 Rose Wright <[email protected]>…

Currently, "ip help" or "ip -help" always returns an error code because usage() is used as a fall through on "ip" and defaults to stderr with -1.

This is a minor bug that breaks "ip help | grep" and other scripts that rely on standard exit codes. The fix is to pass the status code as a parameter into usage() and change stderr to stdout when needed.

---

This is the closest of the three submissions, but there are way more commands in iproute2 than just ip. Need to address all the commands. Looks like perfect trivial job for AI coding tools. I am looking into it now.

author
#2 lore raw

Hi Stephen,

Thanks for the feedback. I'd love to finish this up. I can apply the same fix to the other commands in iproute2 sometime after work, around 17:30 UTC today.

Thanks again,
Rose

Quoted 15 lines · Stephen Hemminger
Quoted 6 lines · On Sun, 21 Jun 2026 18:03:11 +0000 Rose Wright <[email protected]>…

Currently, "ip help" or "ip -help" always returns an error code because usage() is used as a fall through on "ip" and defaults to stderr with -1.

This is a minor bug that breaks "ip help | grep" and other scripts that rely on standard exit codes. The fix is to pass the status code as a parameter into usage() and change stderr to stdout when needed.

---

This is the closest of the three submissions, but there are way more commands in iproute2 than just ip. Need to address all the commands. Looks like perfect trivial job for AI coding tools. I am looking into it now.

#3 lore raw

PATCH v2 1/1 iproute2: return correct status from help commands across all utilities

In reply to Stephen Hemminger

Currently, help commands ("help", "-h", "-help") in utilities always return error codes.

This is a bug which breaks piping, grep operations, and scripts that rely on standard exit codes. The fix parameterizes the usage/help functions in these utilities to dynamically write to stdout when help is explicitly requested.

This patch standardizes the behavior of help commands and usage errors across all utilities: ip, bridge, tc, devlink, dcb, rdma, vdpa, dpll, genl, tipc, and netshaper.

Netlink socket initialization is bypassed when a help command is used to prevent commands from failing in isolated test/container environments.

Test added to confirm these changes: testsuite/tests/ip/help.t

---
 bridge/bridge.c           |  22 +++---
 dcb/dcb.c                 |  38 +++++++---
 devlink/devlink.c         |  42 ++++++++---
 dpll/dpll.c               |  31 +++++---
 genl/genl.c               |  14 ++--
 netshaper/netshaper.c     |  52 ++++++++++---
 rdma/rdma.c               |  39 +++++++---
 tc/tc.c                   |  14 ++--
 testsuite/tests/ip/help.t | 151 ++++++++++++++++++++++++++++++++++++++
 tipc/bearer.c             | 142 +++++++++++++++++++----------------
 tipc/bearer.h             |   8 +-
 tipc/cmdl.c               |   9 ++-
 tipc/cmdl.h               |   6 +-
 tipc/link.c               | 108 ++++++++++++++-------------
 tipc/link.h               |   4 +-
 tipc/media.c              |  24 +++---
 tipc/media.h              |   4 +-
 tipc/nametable.c          |   8 +-
 tipc/nametable.h          |   4 +-
 tipc/node.c               |  54 ++++++++------
 tipc/node.h               |   4 +-
 tipc/peer.c               |  26 +++----
 tipc/peer.h               |   4 +-
 tipc/socket.c             |   8 +-
 tipc/socket.h             |   4 +-
 tipc/tipc.c               |  26 ++++---
 vdpa/vdpa.c               |  40 +++++++---
 27 files changed, 598 insertions(+), 288 deletions(-)
 create mode 100755 testsuite/tests/ip/help.t
bridge/bridge.c +12 -10
index d993ba19..7bc08b82 100644
@@ -29,23 +29,25 @@ int timestamp;
 static const char *batch_file;
 int force;
 
-static void usage(void) __attribute__((noreturn));
+static void usage(int status) __attribute__((noreturn));
 
-static void usage(void)
+static void usage(int status)
 {
-	fprintf(stderr,
+	FILE *out = status == 0 ? stdout : stderr;
+
+	fprintf(out,
 "Usage: bridge [ OPTIONS ] OBJECT { COMMAND | help }\n"
 "       bridge [ -force ] -batch filename\n"
 "where  OBJECT := { link | fdb | mdb | mst | vlan | vni | monitor }\n"
 "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n"
 "                    -o[neline] | -t[imestamp] | -n[etns] name |\n"
 "                    -com[pressvlans] -c[olor] -p[retty] -j[son] }\n");
-	exit(-1);
+	exit(status);
 }
 
 static int do_help(int argc, char **argv)
 {
-	usage();
+	usage(0);
 }
 
 
@@ -118,7 +120,7 @@ main(int argc, char **argv)
 			opt++;
 
 		if (matches(opt, "-help") == 0) {
-			usage();
+			usage(0);
 		} else if (matches(opt, "-Version") == 0) {
 			printf("bridge utility, %s\n", version);
 			exit(0);
@@ -135,13 +137,13 @@ main(int argc, char **argv)
 			argc--;
 			argv++;
 			if (argc <= 1)
-				usage();
+				usage(-1);
 			if (strcmp(argv[1], "inet") == 0)
 				preferred_family = AF_INET;
 			else if (strcmp(argv[1], "inet6") == 0)
 				preferred_family = AF_INET6;
 			else if (strcmp(argv[1], "help") == 0)
-				usage();
+				usage(0);
 			else
 				invarg("invalid protocol family", argv[1]);
 		} else if (strcmp(opt, "-4") == 0) {
@@ -165,7 +167,7 @@ main(int argc, char **argv)
 			argc--;
 			argv++;
 			if (argc <= 1)
-				usage();
+				usage(-1);
 			batch_file = argv[1];
 		} else {
 			fprintf(stderr,
@@ -192,5 +194,5 @@ main(int argc, char **argv)
 		return do_cmd(argv[1], argc-1, argv+1);
 
 	rtnl_close(&rth);
-	usage();
+	usage(-1);
 }
dcb/dcb.c +27 -11
index fe0a0f04..131005fe 100644
@@ -465,9 +465,9 @@ int dcb_cmd_parse_dev(struct dcb *dcb, int argc, char **argv,
 	}
 }
 
-static void dcb_help(void)
+static void dcb_help(FILE *out)
 {
-	fprintf(stderr,
+	fprintf(out,
 		"Usage: dcb [ OPTIONS ] OBJECT { COMMAND | help }\n"
 		"       dcb [ -f | --force ] { -b | --batch } filename [ -n | --netns ] netnsname\n"
 		"where  OBJECT := { app | apptrust | buffer | dcbx | ets | maxrate | pfc | rewr }\n"
@@ -478,8 +478,11 @@ static void dcb_help(void)
 
 static int dcb_cmd(struct dcb *dcb, int argc, char **argv)
 {
-	if (!argc || matches(*argv, "help") == 0) {
-		dcb_help();
+	if (!argc) {
+		dcb_help(stderr);
+		return -EINVAL;
+	} else if (matches(*argv, "help") == 0) {
+		dcb_help(stdout);
 		return 0;
 	} else if (matches(*argv, "app") == 0) {
 		return dcb_cmd_app(dcb, argc - 1, argv + 1);
@@ -533,6 +536,7 @@ int main(int argc, char **argv)
 	const char *batch_file = NULL;
 	bool force = false;
 	struct dcb *dcb;
+	bool need_nl = true;
 	int opt;
 	int err;
 	int ret;
@@ -579,12 +583,12 @@ int main(int argc, char **argv)
 			dcb->use_iec = true;
 			break;
 		case 'h':
-			dcb_help();
+			dcb_help(stdout);
 			ret = EXIT_SUCCESS;
 			goto dcb_free;
 		default:
 			fprintf(stderr, "Unknown option.\n");
-			dcb_help();
+			dcb_help(stderr);
 			ret = EXIT_FAILURE;
 			goto dcb_free;
 		}
@@ -593,10 +597,21 @@ int main(int argc, char **argv)
 	argc -= optind;
 	argv += optind;
 
-	err = dcb_init(dcb);
-	if (err) {
-		ret = EXIT_FAILURE;
-		goto dcb_free;
+	if (argc > 0 && (strcmp(argv[0], "help") == 0 ||
+			 strcmp(argv[0], "-h") == 0 ||
+			 strcmp(argv[0], "--help") == 0))
+		need_nl = false;
+	if (argc > 1 && (strcmp(argv[1], "help") == 0 ||
+			 strcmp(argv[1], "-h") == 0 ||
+			 strcmp(argv[1], "--help") == 0))
+		need_nl = false;
+
+	if (need_nl) {
+		err = dcb_init(dcb);
+		if (err) {
+			ret = EXIT_FAILURE;
+			goto dcb_free;
+		}
 	}
 
 	if (batch_file)
@@ -612,7 +627,8 @@ int main(int argc, char **argv)
 	ret = EXIT_SUCCESS;
 
 dcb_fini:
-	dcb_fini(dcb);
+	if (need_nl)
+		dcb_fini(dcb);
 dcb_free:
 	dcb_free(dcb);
 
devlink/devlink.c +31 -11

88 lines not on this page · raw message

dpll/dpll.c +21 -10
index 60404e13..e0afb4fe 100644
@@ -575,9 +575,9 @@ static void dpll_pr_freq_range(__u64 freq_min, __u64 freq_max)
 	close_json_object();
 }
 
-static void help(void)
+static void help(FILE *out)
 {
-	pr_err("Usage: dpll [ OPTIONS ] OBJECT { COMMAND | help }\n"
+	fprintf(out, "Usage: dpll [ OPTIONS ] OBJECT { COMMAND | help }\n"
 	       "where  OBJECT := { device | pin | monitor }\n"
 	       "       OPTIONS := { -V | --Version | -j | --json | -p | --pretty |\n"
 	       "                    -t | --timestamp | --tshort }\n");
@@ -592,9 +592,12 @@ static int dpll_cmd(struct dpll *dpll, int argc, char **argv)
 	dpll->argc = argc;
 	dpll->argv = argv;
 
-	if (dpll_argv_match(dpll, "help") || dpll_no_arg(dpll)) {
-		help();
+	if (dpll_argv_match(dpll, "help")) {
+		help(stdout);
 		return 0;
+	} else if (dpll_no_arg(dpll)) {
+		help(stderr);
+		return -EINVAL;
 	} else if (dpll_argv_match_inc(dpll, "device")) {
 		return cmd_device(dpll);
 	} else if (dpll_argv_match_inc(dpll, "pin")) {
@@ -646,10 +649,12 @@ int main(int argc, char **argv)
 		{ "pretty", no_argument, NULL, 'p' },
 		{ "timestamp", no_argument, NULL, 't' },
 		{ "tshort", no_argument, NULL, OPT_TSHORT },
+		{ "help", no_argument, NULL, 'h' },
 		{ NULL, 0, NULL, 0 }
 	};
-	const char *opt_short = "Vjpt";
+	const char *opt_short = "Vjpth";
 	struct dpll *dpll;
+	bool need_nl = true;
 	int err, opt, ret;
 
 	dpll = dpll_alloc();
@@ -678,9 +683,13 @@ int main(int argc, char **argv)
 			timestamp = 1;
 			timestamp_short = 1;
 			break;
+		case 'h':
+			help(stdout);
+			ret = EXIT_SUCCESS;
+			goto dpll_free;
 		default:
 			pr_err("Unknown option.\n");
-			help();
+			help(stderr);
 			ret = EXIT_FAILURE;
 			goto dpll_free;
 		}
@@ -700,11 +709,13 @@ int main(int argc, char **argv)
 	}
 
 	/* Skip netlink init for help commands */
-	bool need_nl = true;
-
-	if (argc > 0 && strcmp(argv[0], "help") == 0)
+	if (argc > 0 && (strcmp(argv[0], "help") == 0 ||
+			 strcmp(argv[0], "-h") == 0 ||
+			 strcmp(argv[0], "--help") == 0))
 		need_nl = false;
-	if (argc > 1 && strcmp(argv[1], "help") == 0)
+	if (argc > 1 && (strcmp(argv[1], "help") == 0 ||
+			 strcmp(argv[1], "-h") == 0 ||
+			 strcmp(argv[1], "--help") == 0))
 		need_nl = false;
 
 	if (need_nl) {
genl/genl.c +8 -6
index b497a3ad..2c304a57 100644
@@ -90,16 +90,18 @@ noexist:
 	return f;
 }
 
-static void usage(void) __attribute__((noreturn));
+static void usage(int status) __attribute__((noreturn));
 
-static void usage(void)
+static void usage(int status)
 {
-	fprintf(stderr,
+	FILE *out = status == 0 ? stdout : stderr;
+
+	fprintf(out,
 		"Usage: genl [ OPTIONS ] OBJECT [help] }\n"
 		"where  OBJECT := { ctrl etc }\n"
 		"       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n"
 		"                    -j[son] | -p[retty] }\n");
-	exit(-1);
+	exit(status);
 }
 
 int main(int argc, char **argv)
@@ -122,7 +124,7 @@ int main(int argc, char **argv)
 		} else if (matches(argv[1], "-pretty") == 0) {
 			++pretty;
 		} else if (matches(argv[1], "-help") == 0) {
-			usage();
+			usage(0);
 		} else {
 			fprintf(stderr,
 				"Option \"%s\" is unknown, try \"genl -help\".\n",
@@ -146,5 +148,5 @@ int main(int argc, char **argv)
 		return ret;
 	}
 
-	usage();
+	usage(-1);
 }
netshaper/netshaper.c +40 -12

130 lines not on this page · raw message

rdma/rdma.c +29 -10
index 253ac58b..3bdb76f0 100644
@@ -11,9 +11,9 @@
 /* Global utils flags */
 int json;
 
-static void help(char *name)
+static void help(FILE *out, char *name)
 {
-	pr_out("Usage: %s [ OPTIONS ] OBJECT { COMMAND | help }\n"
+	fprintf(out, "Usage: %s [ OPTIONS ] OBJECT { COMMAND | help }\n"
 	       "       %s [ -f[orce] ] -b[atch] filename\n"
 	       "where  OBJECT := { dev | link | resource | monitor | system | statistic | help }\n"
 	       "       OPTIONS := { -V[ersion] | -d[etails] | -j[son] | -p[retty] | -r[aw]}\n", name, name);
@@ -21,14 +21,20 @@ static void help(char *name)
 
 static int cmd_help(struct rd *rd)
 {
-	help(rd->filename);
+	help(stdout, rd->filename);
 	return 0;
 }
 
+static int cmd_no_arg(struct rd *rd)
+{
+	help(stderr, rd->filename);
+	return -EINVAL;
+}
+
 static int rd_cmd(struct rd *rd, int argc, char **argv)
 {
 	const struct rd_cmd cmds[] = {
-		{ NULL,		cmd_help },
+		{ NULL,		cmd_no_arg },
 		{ "help",	cmd_help },
 		{ "dev",	cmd_dev },
 		{ "link",	cmd_link },
@@ -104,6 +110,7 @@ int main(int argc, char **argv)
 	bool show_raw = false;
 	bool force = false;
 	bool oneline = false;
+	bool need_nl = true;
 	struct rd rd = {};
 	char *filename;
 	int opt;
@@ -142,14 +149,14 @@ int main(int argc, char **argv)
 			batch_file = optarg;
 			break;
 		case 'h':
-			help(filename);
+			help(stdout, filename);
 			return EXIT_SUCCESS;
 		case ':':
 			pr_err("-%c option requires an argument\n", optopt);
 			return EXIT_FAILURE;
 		default:
 			pr_err("Unknown option.\n");
-			help(filename);
+			help(stderr, filename);
 			return EXIT_FAILURE;
 		}
 	}
@@ -163,9 +170,20 @@ int main(int argc, char **argv)
 	rd.show_driver_details = show_driver_details;
 	rd.show_raw = show_raw;
 
-	err = rd_init(&rd, filename);
-	if (err)
-		goto out;
+	if (argc > 0 && (strcmp(argv[0], "help") == 0 ||
+			 strcmp(argv[0], "-h") == 0 ||
+			 strcmp(argv[0], "--help") == 0))
+		need_nl = false;
+	if (argc > 1 && (strcmp(argv[1], "help") == 0 ||
+			 strcmp(argv[1], "-h") == 0 ||
+			 strcmp(argv[1], "--help") == 0))
+		need_nl = false;
+
+	if (need_nl) {
+		err = rd_init(&rd, filename);
+		if (err)
+			goto out;
+	}
 
 	if (batch_file)
 		err = rd_batch(&rd, batch_file, force);
@@ -173,6 +191,7 @@ int main(int argc, char **argv)
 		err = rd_cmd(&rd, argc, argv);
 out:
 	/* Always cleanup */
-	rd_cleanup(&rd);
+	if (need_nl)
+		rd_cleanup(&rd);
 	return err ? EXIT_FAILURE : EXIT_SUCCESS;
 }
tc/tc.c +8 -6
index 7d69e4d5..057541d2 100644
@@ -186,9 +186,11 @@ noexist:
 	return q;
 }
 
-static void usage(void)
+static void usage(int status)
 {
-	fprintf(stderr,
+	FILE *out = status == 0 ? stdout : stderr;
+	
+	fprintf(out,
 		"Usage:	tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
 		"	tc [-force] -batch filename\n"
 		"where  OBJECT := { qdisc | class | filter | chain |\n"
@@ -217,7 +219,7 @@ static int do_cmd(int argc, char **argv)
 	if (matches(*argv, "exec") == 0)
 		return do_exec(argc-1, argv+1);
 	if (matches(*argv, "help") == 0) {
-		usage();
+		usage(0);
 		return 0;
 	}
 
@@ -282,7 +284,7 @@ int main(int argc, char **argv)
 		} else if (matches(argv[1], "-iec") == 0) {
 			++use_iec;
 		} else if (matches(argv[1], "-help") == 0) {
-			usage();
+			usage(0);
 			return 0;
 		} else if (matches(argv[1], "-force") == 0) {
 			++force;
@@ -335,8 +337,8 @@ int main(int argc, char **argv)
 		return batch(batch_file);
 
 	if (argc <= 1) {
-		usage();
-		return 0;
+		usage(-1);
+		return -1;
 	}
 
 	tc_core_init();
testsuite/tests/ip/help.t new file +151 -0

151 lines not on this page · raw message

tipc/bearer.c +78 -64

331 lines not on this page · raw message

tipc/bearer.h +5 -3
index a9344659..2389f811 100644
@@ -12,11 +12,13 @@
 
 extern int help_flag;
 
+#include <stdio.h>
+
 int cmd_bearer(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl, void *data);
-void cmd_bearer_help(struct cmdl *cmdl);
+void cmd_bearer_help(FILE *out, struct cmdl *cmdl);
 
-void print_bearer_media(void);
-int cmd_get_unique_bearer_name(const struct cmd *cmd, struct cmdl *cmdl,
+void print_bearer_media(FILE *out);
+int cmd_get_unique_bearer_name(FILE *out, const struct cmd *cmd, struct cmdl *cmdl,
 			       struct opt *opts, char *bname,
 			       const struct tipc_sup_media *sup_media);
 #endif
tipc/cmdl.c +7 -2
index 152ddb51..ee9e822c 100644
@@ -108,7 +108,7 @@ int run_cmd(struct nlmsghdr *nlh, const struct cmd *caller,
 
 	if ((cmdl->optind) >= cmdl->argc) {
 		if (caller->help)
-			(caller->help)(cmdl);
+			(caller->help)(help_flag ? stdout : stderr, cmdl);
 		return -EINVAL;
 	}
 	name = cmdl->argv[cmdl->optind];
@@ -118,7 +118,7 @@ int run_cmd(struct nlmsghdr *nlh, const struct cmd *caller,
 	if (!cmd) {
 		/* Show help about last command if we don't find this one */
 		if (help_flag && caller->help) {
-			(caller->help)(cmdl);
+			(caller->help)(stdout, cmdl);
 		} else {
 			fprintf(stderr, "error, invalid command \"%s\"\n", name);
 			fprintf(stderr, "use --help for command help\n");
@@ -126,5 +126,10 @@ int run_cmd(struct nlmsghdr *nlh, const struct cmd *caller,
 		return -EINVAL;
 	}
 
+	if (help_flag && cmdl->optind >= cmdl->argc && cmd->help) {
+		(cmd->help)(stdout, cmdl);
+		return 0;
+	}
+
 	return (cmd->func)(nlh, cmd, cmdl, data);
 }
tipc/cmdl.h +4 -2
index 18fe51bf..fd8f02d1 100644
@@ -12,6 +12,8 @@
 
 extern int help_flag;
 
+#include <stdio.h>
+
 enum {
 	OPT_KEY			= (1 << 0),
 	OPT_KEYVAL		= (1 << 1),
@@ -26,14 +28,14 @@ struct cmdl {
 struct tipc_sup_media {
 	char *media;
 	char *identifier;
-	void (*help)(struct cmdl *cmdl, char *media);
+	void (*help)(FILE *out, struct cmdl *cmdl, char *media);
 };
 
 struct cmd {
 	const char *cmd;
 	int (*func)(struct nlmsghdr *nlh, const struct cmd *cmd,
 		    struct cmdl *cmdl, void *data);
-	void (*help)(struct cmdl *cmdl);
+	void (*help)(FILE *out, struct cmdl *cmdl);
 };
 
 struct opt {
tipc/link.c +56 -52

288 lines not on this page · raw message

tipc/link.h +2 -2
index a0d46035..09e47b89 100644
@@ -8,10 +8,10 @@
 #ifndef _TIPC_LINK_H
 #define _TIPC_LINK_H
 
-extern int help_flag;
+#include <stdio.h>
 
 int cmd_link(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
 	     void *data);
-void cmd_link_help(struct cmdl *cmdl);
+void cmd_link_help(FILE *out, struct cmdl *cmdl);
 
 #endif
tipc/media.c +12 -12
index 5ff0c8c4..82ce5145 100644
@@ -40,8 +40,8 @@ static int cmd_media_list(struct nlmsghdr *nlh, const struct cmd *cmd,
 			 struct cmdl *cmdl, void *data)
 {
 	if (help_flag) {
-		fprintf(stderr, "Usage: %s media list\n", cmdl->argv[0]);
-		return -EINVAL;
+		fprintf(stdout, "Usage: %s media list\n", cmdl->argv[0]);
+		return 0;
 	}
 
 	nlh = msg_init(TIPC_NL_MEDIA_GET);
@@ -101,8 +101,8 @@ static int cmd_media_get_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
 		return -EINVAL;
 
 	if (help_flag) {
-		(cmd->help)(cmdl);
-		return -EINVAL;
+		(cmd->help)(stdout, cmdl);
+		return 0;
 	}
 
 	if (parse_opts(opts, cmdl) < 0)
@@ -131,9 +131,9 @@ static int cmd_media_get_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
 	return msg_doit(nlh, media_get_cb, &prop);
 }
 
-static void cmd_media_get_help(struct cmdl *cmdl)
+static void cmd_media_get_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr, "Usage: %s media get PPROPERTY media MEDIA\n\n"
+	fprintf(out, "Usage: %s media get PPROPERTY media MEDIA\n\n"
 		"PROPERTIES\n"
 		" tolerance             - Get media tolerance\n"
 		" priority              - Get media priority\n"
@@ -156,9 +156,9 @@ static int cmd_media_get(struct nlmsghdr *nlh, const struct cmd *cmd,
 	return run_cmd(nlh, cmd, cmds, cmdl, NULL);
 }
 
-static void cmd_media_set_help(struct cmdl *cmdl)
+static void cmd_media_set_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr, "Usage: %s media set PPROPERTY media MEDIA\n\n"
+	fprintf(out, "Usage: %s media set PPROPERTY media MEDIA\n\n"
 		"PROPERTIES\n"
 		" tolerance TOLERANCE   - Set media tolerance\n"
 		" priority PRIORITY     - Set media priority\n"
@@ -192,8 +192,8 @@ static int cmd_media_set_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
 		return -EINVAL;
 
 	if (help_flag) {
-		(cmd->help)(cmdl);
-		return -EINVAL;
+		(cmd->help)(stdout, cmdl);
+		return 0;
 	}
 
 	if (cmdl->optind >= cmdl->argc) {
@@ -247,9 +247,9 @@ static int cmd_media_set(struct nlmsghdr *nlh, const struct cmd *cmd,
 	return run_cmd(nlh, cmd, cmds, cmdl, NULL);
 }
 
-void cmd_media_help(struct cmdl *cmdl)
+void cmd_media_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr,
+	fprintf(out,
 		"Usage: %s media COMMAND [ARGS] ...\n"
 		"\n"
 		"Commands:\n"
tipc/media.h +2 -2
index f1b4b540..eb4374dd 100644
@@ -8,10 +8,10 @@
 #ifndef _TIPC_MEDIA_H
 #define _TIPC_MEDIA_H
 
-extern int help_flag;
+#include <stdio.h>
 
 int cmd_media(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
 	     void *data);
-void cmd_media_help(struct cmdl *cmdl);
+void cmd_media_help(FILE *out, struct cmdl *cmdl);
 
 #endif
tipc/nametable.c +4 -4
index 5162f7fc..1ede1024 100644
@@ -80,8 +80,8 @@ static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
 	int rc = 0;
 
 	if (help_flag) {
-		fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]);
-		return -EINVAL;
+		fprintf(stdout, "Usage: %s nametable show\n", cmdl->argv[0]);
+		return 0;
 	}
 
 	nlh = msg_init(TIPC_NL_NAME_TABLE_GET);
@@ -97,9 +97,9 @@ static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
 	return rc;
 }
 
-void cmd_nametable_help(struct cmdl *cmdl)
+void cmd_nametable_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr,
+	fprintf(out,
 		"Usage: %s nametable COMMAND\n\n"
 		"COMMANDS\n"
 		" show                  - Show nametable\n",
tipc/nametable.h +2 -2
index c4df8d9d..e84227ed 100644
@@ -8,9 +8,9 @@
 #ifndef _TIPC_NAMETABLE_H
 #define _TIPC_NAMETABLE_H
 
-extern int help_flag;
+#include <stdio.h>
 
-void cmd_nametable_help(struct cmdl *cmdl);
+void cmd_nametable_help(FILE *out, struct cmdl *cmdl);
 int cmd_nametable(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
 		  void *data);
 
tipc/node.c +32 -22

137 lines not on this page · raw message

tipc/node.h +2 -2
index 4a986d07..7d310897 100644
@@ -8,10 +8,10 @@
 #ifndef _TIPC_NODE_H
 #define _TIPC_NODE_H
 
-extern int help_flag;
+#include <stdio.h>
 
 int cmd_node(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
 	     void *data);
-void cmd_node_help(struct cmdl *cmdl);
+void cmd_node_help(FILE *out, struct cmdl *cmdl);
 
 #endif
tipc/peer.c +13 -13
index 5a583fb9..6d12fb19 100644
@@ -27,9 +27,9 @@ static int cmd_peer_rm_addr(struct nlmsghdr *nlh, const struct cmd *cmd,
 	struct nlattr *nest;
 
 	if ((cmdl->argc != cmdl->optind + 1) || help_flag) {
-		fprintf(stderr, "Usage: %s peer remove address ADDRESS\n",
+		fprintf(help_flag ? stdout : stderr, "Usage: %s peer remove address ADDRESS\n",
 			cmdl->argv[0]);
-		return -EINVAL;
+		return help_flag ? 0 : -EINVAL;
 	}
 
 	str = shift_cmdl(cmdl);
@@ -63,10 +63,10 @@ static int cmd_peer_rm_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
 	struct nlattr *nest;
 	char *str;
 
-	if (cmdl->argc != cmdl->optind + 1) {
-		fprintf(stderr, "Usage: %s peer remove identity NODEID\n",
+	if ((cmdl->argc != cmdl->optind + 1) || help_flag) {
+		fprintf(help_flag ? stdout : stderr, "Usage: %s peer remove identity NODEID\n",
 			cmdl->argv[0]);
-		return -EINVAL;
+		return help_flag ? 0 : -EINVAL;
 	}
 
 	str = shift_cmdl(cmdl);
@@ -89,23 +89,23 @@ static int cmd_peer_rm_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
 	return msg_doit(nlh, NULL, NULL);
 }
 
-static void cmd_peer_rm_help(struct cmdl *cmdl)
+static void cmd_peer_rm_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr, "Usage: %s peer remove PROPERTY\n\n"
+	fprintf(out, "Usage: %s peer remove PROPERTY\n\n"
 		"PROPERTIES\n"
 		" identity NODEID         - Remove peer node identity\n",
 		cmdl->argv[0]);
 }
 
-static void cmd_peer_rm_addr_help(struct cmdl *cmdl)
+static void cmd_peer_rm_addr_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr, "Usage: %s peer remove address ADDRESS\n",
+	fprintf(out, "Usage: %s peer remove address ADDRESS\n",
 		cmdl->argv[0]);
 }
 
-static void cmd_peer_rm_nodeid_help(struct cmdl *cmdl)
+static void cmd_peer_rm_nodeid_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr, "Usage: %s peer remove identity NODEID\n",
+	fprintf(out, "Usage: %s peer remove identity NODEID\n",
 		cmdl->argv[0]);
 }
 
@@ -121,9 +121,9 @@ static int cmd_peer_rm(struct nlmsghdr *nlh, const struct cmd *cmd,
 	return run_cmd(nlh, cmd, cmds, cmdl, NULL);
 }
 
-void cmd_peer_help(struct cmdl *cmdl)
+void cmd_peer_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr,
+	fprintf(out,
 		"Usage: %s peer COMMAND [ARGS] ...\n\n"
 		"COMMANDS\n"
 		" remove                - Remove an offline peer node\n",
tipc/peer.h +2 -2
index 2bd0a2a3..8a2374cf 100644
@@ -8,10 +8,10 @@
 #ifndef _TIPC_PEER_H
 #define _TIPC_PEER_H
 
-extern int help_flag;
+#include <stdio.h>
 
 int cmd_peer(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
 	     void *data);
-void cmd_peer_help(struct cmdl *cmdl);
+void cmd_peer_help(FILE *out, struct cmdl *cmdl);
 
 #endif
tipc/socket.c +4 -4
index 4d376e07..bf2147cb 100644
@@ -112,8 +112,8 @@ static int cmd_socket_list(struct nlmsghdr *nlh, const struct cmd *cmd,
 			   struct cmdl *cmdl, void *data)
 {
 	if (help_flag) {
-		fprintf(stderr, "Usage: %s socket list\n", cmdl->argv[0]);
-		return -EINVAL;
+		fprintf(stdout, "Usage: %s socket list\n", cmdl->argv[0]);
+		return 0;
 	}
 
 	nlh = msg_init(TIPC_NL_SOCK_GET);
@@ -125,9 +125,9 @@ static int cmd_socket_list(struct nlmsghdr *nlh, const struct cmd *cmd,
 	return msg_dumpit(nlh, sock_list_cb, NULL);
 }
 
-void cmd_socket_help(struct cmdl *cmdl)
+void cmd_socket_help(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr,
+	fprintf(out,
 		"Usage: %s socket COMMAND\n\n"
 		"Commands:\n"
 		" list                  - List sockets (ports)\n",
tipc/socket.h +2 -2
index c4341bb2..207b8666 100644
@@ -8,9 +8,9 @@
 #ifndef _TIPC_SOCKET_H
 #define _TIPC_SOCKET_H
 
-extern int help_flag;
+#include <stdio.h>
 
-void cmd_socket_help(struct cmdl *cmdl);
+void cmd_socket_help(FILE *out, struct cmdl *cmdl);
 int cmd_socket(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
 		  void *data);
 
tipc/tipc.c +15 -11
index 56af052c..733acb50 100644
@@ -28,9 +28,9 @@ int help_flag;
 int json;
 struct mnlu_gen_socket tipc_nlg;
 
-static void about(struct cmdl *cmdl)
+static void about(FILE *out, struct cmdl *cmdl)
 {
-	fprintf(stderr,
+	fprintf(out,
 		"Transparent Inter-Process Communication Protocol\n"
 		"Usage: %s [OPTIONS] COMMAND [ARGS] ...\n"
 		"\n"
@@ -111,20 +111,24 @@ int main(int argc, char *argv[])
 	cmdl.argc = argc;
 	cmdl.argv = argv;
 
-	res = mnlu_gen_socket_open(&tipc_nlg, TIPC_GENL_V2_NAME,
-				   TIPC_GENL_V2_VERSION);
-	if (res) {
-		fprintf(stderr,
-			"Unable to get TIPC nl family id (module loaded?)\n");
-		return -1;
+	if (!help_flag) {
+		res = mnlu_gen_socket_open(&tipc_nlg, TIPC_GENL_V2_NAME,
+					   TIPC_GENL_V2_VERSION);
+		if (res) {
+			fprintf(stderr,
+				"Unable to get TIPC nl family id (module loaded?)\n");
+			return -1;
+		}
 	}
 
 	res = run_cmd(NULL, &cmd, cmds, &cmdl, &tipc_nlg);
 	if (res != 0) {
-		mnlu_gen_socket_close(&tipc_nlg);
-		return -1;
+		if (!help_flag)
+			mnlu_gen_socket_close(&tipc_nlg);
+		return help_flag ? 0 : -1;
 	}
 
-	mnlu_gen_socket_close(&tipc_nlg);
+	if (!help_flag)
+		mnlu_gen_socket_close(&tipc_nlg);
 	return 0;
 }
vdpa/vdpa.c +29 -11
index e2b0a5b1..550443e6 100644
@@ -1051,9 +1051,9 @@ static int cmd_dev(struct vdpa *vdpa, int argc, char **argv)
 	return -ENOENT;
 }
 
-static void help(void)
+static void help(FILE *out)
 {
-	fprintf(stderr,
+	fprintf(out,
 		"Usage: vdpa [ OPTIONS ] OBJECT { COMMAND | help }\n"
 		"where  OBJECT := { mgmtdev | dev }\n"
 		"       OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] }\n");
@@ -1061,8 +1061,11 @@ static void help(void)
 
 static int vdpa_cmd(struct vdpa *vdpa, int argc, char **argv)
 {
-	if (!argc || matches(*argv, "help") == 0) {
-		help();
+	if (!argc) {
+		help(stderr);
+		return -EINVAL;
+	} else if (matches(*argv, "help") == 0) {
+		help(stdout);
 		return 0;
 	} else if (matches(*argv, "mgmtdev") == 0) {
 		return cmd_mgmtdev(vdpa, argc - 1, argv + 1);
@@ -1127,6 +1130,7 @@ int main(int argc, char **argv)
 		{ NULL, 0, NULL, 0 }
 	};
 	struct vdpa *vdpa;
+	bool need_nl = true;
 	int opt;
 	int err;
 	int ret;
@@ -1150,12 +1154,12 @@ int main(int argc, char **argv)
 			pretty = true;
 			break;
 		case 'h':
-			help();
+			help(stdout);
 			ret = EXIT_SUCCESS;
 			goto vdpa_free;
 		default:
 			fprintf(stderr, "Unknown option.\n");
-			help();
+			help(stderr);
 			ret = EXIT_FAILURE;
 			goto vdpa_free;
 		}
@@ -1164,10 +1168,23 @@ int main(int argc, char **argv)
 	argc -= optind;
 	argv += optind;
 
-	err = vdpa_init(vdpa);
-	if (err) {
-		ret = EXIT_FAILURE;
-		goto vdpa_free;
+	if (argc > 0 && (strcmp(argv[0], "help") == 0 ||
+			 strcmp(argv[0], "-h") == 0 ||
+			 strcmp(argv[0], "--help") == 0))
+		need_nl = false;
+	
+	if (argc > 1 && (strcmp(argv[1], "help") == 0 ||
+			 strcmp(argv[1], "-h") == 0 ||
+			 strcmp(argv[1], "--help") == 0))
+		need_nl = false;
+
+
+	if (need_nl) {
+		err = vdpa_init(vdpa);
+		if (err) {
+			ret = EXIT_FAILURE;
+			goto vdpa_free;
+		}
 	}
 
 	err = vdpa_cmd(vdpa, argc, argv);
@@ -1179,7 +1196,8 @@ int main(int argc, char **argv)
 	ret = EXIT_SUCCESS;
 
 vdpa_fini:
-	vdpa_fini(vdpa);
+	if (need_nl)
+		vdpa_fini(vdpa);
 vdpa_free:
 	vdpa_free(vdpa);
 	return ret;
Signature · 1 line
2.54.0