Skip to contentSkip to conversation

Allow specifying bridge port STP state by name rather than number.

PATCH In review Alex Pilon 2 participants 4 messages Feb 19, 2015 – Feb 20, 2015

Subsystems (2)
lore
Share & tools
lore

Alex Pilon

author
#0 lore raw

PATCH 1/1 Allow specifying bridge port STP state by name rather than number.

The existing behaviour forces one to memorize the integer constants for STP port states.

    # bridge link set dev dummy0 state 3

This patch makes it possible to use the lowercased port state name.

    # bridge link set dev dummy0 state forwarding

Invalid non-integer inputs now cause exit with status -1.

---
 bridge/link.c     | 14 +++++++++++++-
 man/man8/bridge.8 |  4 +++-
 2 files changed, 16 insertions(+), 2 deletions(-)
bridge/link.c +13 -1
index c8555f8..a7bd85f 100644
@@ -316,7 +316,19 @@ static int brlink_modify(int argc, char **argv)
 			priority = atoi(*argv);
 		} else if (strcmp(*argv, "state") == 0) {
 			NEXT_ARG();
-			state = atoi(*argv);
+			char *endptr;
+			size_t nstates = sizeof(port_states) / sizeof(*port_states);
+			state = strtol(*argv, &endptr, 10);
+			if (!(**argv != '\0' && *endptr == '\0')) {
+				for (state = 0; state < nstates; state++)
+					if (strcmp(port_states[state], *argv) == 0)
+						break;
+				if (state == nstates) {
+					fprintf(stderr,
+						"Error: invalid STP port state\n");
+					exit(-1);
+				}
+			}
 		} else if (strcmp(*argv, "hwmode") == 0) {
 			NEXT_ARG();
 			flags = BRIDGE_FLAGS_SELF;
man/man8/bridge.8 +3 -1
index e344db2..68ad71e 100644
@@ -207,7 +207,9 @@ droot port selectio algorithms.
 .TP
 .BI state " STATE "
 the operation state of the port.  This is primarily used by user space STP/RSTP
-implementation.  The following is a list of valid values:
+implementation.  One may enter a lowercased port state name, or one of the
+numbers below.  Negative inputs are ignored, and unrecognized names return an
+error.
 
 .B 0
 - port is DISABLED.  Make this port completely inactive.
Signature · 1 line
2.3.0
#1 lore raw

Please don't pass -1 to exit(). It is outside the acceptable range (0 to 255) of exit status values:

https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html

Most programs exit(1) when presented with invalid user input.

Quoted 64 lines · Alex Pilon

The existing behaviour forces one to memorize the integer constants for STP port states.

    # bridge link set dev dummy0 state 3

This patch makes it possible to use the lowercased port state name.

    # bridge link set dev dummy0 state forwarding

Invalid non-integer inputs now cause exit with status -1.

---
 bridge/link.c     | 14 +++++++++++++-
 man/man8/bridge.8 |  4 +++-
 2 files changed, 16 insertions(+), 2 deletions(-)
bridge/link.c +13 -1
index c8555f8..a7bd85f 100644
@@ -316,7 +316,19 @@ static int brlink_modify(int argc, char **argv)
                        priority = atoi(*argv);
                } else if (strcmp(*argv, "state") == 0) {
                        NEXT_ARG();
-                       state = atoi(*argv);
+                       char *endptr;
+                       size_t nstates = sizeof(port_states) / sizeof(*port_states);
+                       state = strtol(*argv, &endptr, 10);
+                       if (!(**argv != '\0' && *endptr == '\0')) {
+                               for (state = 0; state < nstates; state++)
+                                       if (strcmp(port_states[state], *argv) == 0)
+                                               break;
+                               if (state == nstates) {
+                                       fprintf(stderr,
+                                               "Error: invalid STP port state\n");
+                                       exit(-1);
+                               }
+                       }
                } else if (strcmp(*argv, "hwmode") == 0) {
                        NEXT_ARG();
                        flags = BRIDGE_FLAGS_SELF;
man/man8/bridge.8 +3 -2
index e344db2..68ad71e 100644
@@ -207,7 +207,9 @@ droot port selectio algorithms.
 .TP
 .BI state " STATE "
 the operation state of the port.  This is primarily used by user space STP/RSTP
-implementation.  The following is a list of valid values:
+implementation.  One may enter a lowercased port state name, or one of the
+numbers below.  Negative inputs are ignored, and unrecognized names return an
+error.
 
 .B 0
 - port is DISABLED.  Make this port completely inactive.
--

2.3.0

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#2 lore raw
Quoted 6 lines · Jonathon Reinhart

Please don't pass -1 to exit(). It is outside the acceptable range (0 to 255) of exit status values:

https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html

Most programs exit(1) when presented with invalid user input.

I was being consistent with nearby code, and the dominant style. But if that's the case, then there's a few hundred such mistakes that need correcting. Should I fix all of them, and if so, should that be a single patch, or multiple?

There's also only a few instances of EXIT_{SUCCESS,FAILURE}, so I took it wasn't the style to use that.

Regards,

Alex Pilon

#3 lore raw

Sorry, I only looked at your patch, and not the surrounding code. In that case, I would probably stick with the existing style.

I'm just a casual passer-by, but I would like to hear others' opinion on this, as I find exit(-1) to always be incorrect. The problem is that, per the man page:

Quoted 2 lines

The exit() function causes normal process termination and the value of status & 0377 is returned to the parent (see wait(2)).

So when -1 is passed to exit(), the parent will see it as 255. Why not return a value that could be found in the source by grepping for it?

Quoted 19 lines · Alex Pilon
Quoted 6 lines · Jonathon Reinhart

Please don't pass -1 to exit(). It is outside the acceptable range (0 to 255) of exit status values:

https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html

Most programs exit(1) when presented with invalid user input.

I was being consistent with nearby code, and the dominant style. But if that's the case, then there's a few hundred such mistakes that need correcting. Should I fix all of them, and if so, should that be a single patch, or multiple?

There's also only a few instances of EXIT_{SUCCESS,FAILURE}, so I took it wasn't the style to use that.

Regards,

Alex Pilon

Signature · 4 lines
Computers are incredibly fast, accurate and stupid. Human beings are
incredibly slow, inaccurate and brilliant. Together they are powerful
beyond imagination.
                                    A. Einstein