Skip to contentSkip to conversation

nfsdctl: check for listeners before starting threads

PATCH Superseded Olga Kornievskaia 3 participants 4 messages Mar 23

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Olga Kornievskaia

author reviewer
#0 lore raw

PATCH 1/1 nfsdctl: check for listeners before starting threads

When a thread command is executed and yet no listeners have been added prior to it, instead of failing with EIO error print an informative error. Also, "thread 0" command should not error regardless of the listener status.

---
 utils/nfsdctl/nfsdctl.c | 52 ++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 11 deletions(-)
utils/nfsdctl/nfsdctl.c +41 -11
index 6a20d180..d03e2c9f 100644
@@ -162,6 +162,9 @@ static const char *nfsd4_ops[] = {
 	[OP_REMOVEXATTR]	= "OP_REMOVEXATTR",
 };
 
+static int fetch_current_listeners(struct nl_sock *sock);
+static int print_listeners(int output);
+
 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
 			 void *arg)
 {
@@ -720,7 +723,7 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)
 	uint8_t cmd = NFSD_CMD_THREADS_GET;
 	int *pool_threads = NULL;
 	int minthreads = -1;
-	int opt, pools = 0;
+	int opt, pools = 0, zero_threads = 0;
 
 	optind = 1;
 	while ((opt = getopt_long(argc, argv, "hm:", threads_options, NULL)) != -1) {
@@ -762,12 +765,31 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)
 			}
 
 			pool_threads[i] = strtol(targv[i], &endptr, 0);
+			if (!pool_threads[i])
+				zero_threads++;
 			if (!endptr || *endptr != '\0') {
 				xlog(L_ERROR, "Invalid threads value %s.", argv[1]);
 				return 1;
 			}
 		}
 	}
+	/* check if there are listeners added */
+	if (fetch_current_listeners(sock)) {
+		xlog(L_ERROR, "Unable to determine if listeners were added\n");
+		return 1;
+	}
+	if (!print_listeners(0)) {
+		if (zero_threads && zero_threads == pools) {
+			/* Note: we can never have a server with threads and no
+			 * listeners. If we ever add functionality to remove
+			 * listeners on an active server, we need to revisit this.
+			 */
+			return 0;
+		}
+		xlog(L_ERROR, "No listeners added, not starting threads\n");
+		return 1;
+	}
+
 	return threads_doit(sock, cmd, 0, 0, pools, pool_threads, NULL, minthreads);
 }
 
@@ -1077,9 +1099,9 @@ out:
 	return ret;
 }
 
-static void print_listeners(void)
+static int print_listeners(int output)
 {
-	int i;
+	int i, count = 0;
 	const char *res;
 
 	for (i = 0; i < MAX_NFSD_SOCKETS; ++i) {
@@ -1098,26 +1120,34 @@ static void print_listeners(void)
 			res = inet_ntop(AF_INET, &((struct sockaddr_in *)(&sock->ss))->sin_addr,
 					addr, INET6_ADDRSTRLEN);
 			port = ((struct sockaddr_in *)(&sock->ss))->sin_port;
-			if (res == NULL)
+			if (res == NULL && output)
 				perror("inet_ntop");
-			else
-				printf("%s:%s:%hu\n", sock->name, addr, ntohs(port));
+			else {
+				count++;
+				if (output)
+					printf("%s:%s:%hu\n", sock->name, addr, ntohs(port));
+			}
 			break;
 		case AF_INET6:
 			res = inet_ntop(AF_INET6, &((struct sockaddr_in6 *)(&sock->ss))->sin6_addr,
 					addr, INET6_ADDRSTRLEN);
 			port = ((struct sockaddr_in6 *)(&sock->ss))->sin6_port;
-			if (res == NULL)
+			if (res == NULL && output)
 				perror("inet_ntop");
-			else
-				printf("%s:[%s]:%hu\n", sock->name, addr, ntohs(port));
+			else {
+				count++;
+				if (output)
+					printf("%s:[%s]:%hu\n", sock->name, addr, ntohs(port));
+			}
 			break;
 		default:
-			snprintf(addr, INET6_ADDRSTRLEN, "Unknown address family: %d\n",
+			if (output)
+				snprintf(addr, INET6_ADDRSTRLEN, "Unknown address family: %d\n",
 					sock->ss.ss_family);
 			addr[INET6_ADDRSTRLEN - 1] = '\0';
 		}
 	}
+	return count;
 }
 
 static bool ipv6_is_enabled(void)
@@ -1394,7 +1424,7 @@ static int listener_func(struct nl_sock *sock, int argc, char ** argv)
 		return set_listeners(sock);
 	}
 
-	print_listeners();
+	print_listeners(1);
 	return 0;
 }
 
Signature · 1 line
2.52.0
maintainer
#1 lore raw
Quoted 66 lines · Olga Kornievskaia

When a thread command is executed and yet no listeners have been added prior to it, instead of failing with EIO error print an informative error. Also, "thread 0" command should not error regardless of the listener status.

---
 utils/nfsdctl/nfsdctl.c | 52 ++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 11 deletions(-)
utils/nfsdctl/nfsdctl.c +23 -1
index 6a20d180..d03e2c9f 100644
@@ -162,6 +162,9 @@ static const char *nfsd4_ops[] = {
 	[OP_REMOVEXATTR]	= "OP_REMOVEXATTR",
 };
 
+static int fetch_current_listeners(struct nl_sock *sock);
+static int print_listeners(int output);
+
 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
 			 void *arg)
 {
@@ -720,7 +723,7 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)
 	uint8_t cmd = NFSD_CMD_THREADS_GET;
 	int *pool_threads = NULL;
 	int minthreads = -1;
-	int opt, pools = 0;
+	int opt, pools = 0, zero_threads = 0;
 
 	optind = 1;
 	while ((opt = getopt_long(argc, argv, "hm:", threads_options, NULL)) != -1) {
@@ -762,12 +765,31 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)
 			}
 
 			pool_threads[i] = strtol(targv[i], &endptr, 0);
+			if (!pool_threads[i])
+				zero_threads++;
 			if (!endptr || *endptr != '\0') {
 				xlog(L_ERROR, "Invalid threads value %s.", argv[1]);
 				return 1;
 			}
 		}
 	}
+	/* check if there are listeners added */
+	if (fetch_current_listeners(sock)) {
+		xlog(L_ERROR, "Unable to determine if listeners were added\n");
+		return 1;
+	}
+	if (!print_listeners(0)) {
+		if (zero_threads && zero_threads == pools) {
+			/* Note: we can never have a server with threads and no
+			 * listeners. If we ever add functionality to remove
+			 * listeners on an active server, we need to revisit this.
+			 */
+			return 0;
+		}
+		xlog(L_ERROR, "No listeners added, not starting threads\n");
+		return 1;
+	}
+
 	return threads_doit(sock, cmd, 0, 0, pools, pool_threads, NULL, minthreads);
 }
 

utils/nfsdctl/nfsdctl.c out: · line 1102

Quoted 5 lines
utils/nfsdctl/nfsdctl.c +1 -1
@@ -1077,9 +1099,9 @@ out:
 	return ret;
 }
 
-static void print_listeners(void)
+static int print_listeners(int output)

Maybe instead of adding the "output" parameter, you could just add a count_listeners() function?

Ack on the basic idea though!

Quoted 57 lines
utils/nfsdctl/nfsdctl.c +17 -9
 {
-	int i;
+	int i, count = 0;
 	const char *res;
 
 	for (i = 0; i < MAX_NFSD_SOCKETS; ++i) {
@@ -1098,26 +1120,34 @@ static void print_listeners(void)
 			res = inet_ntop(AF_INET, &((struct sockaddr_in *)(&sock->ss))->sin_addr,
 					addr, INET6_ADDRSTRLEN);
 			port = ((struct sockaddr_in *)(&sock->ss))->sin_port;
-			if (res == NULL)
+			if (res == NULL && output)
 				perror("inet_ntop");
-			else
-				printf("%s:%s:%hu\n", sock->name, addr, ntohs(port));
+			else {
+				count++;
+				if (output)
+					printf("%s:%s:%hu\n", sock->name, addr, ntohs(port));
+			}
 			break;
 		case AF_INET6:
 			res = inet_ntop(AF_INET6, &((struct sockaddr_in6 *)(&sock->ss))->sin6_addr,
 					addr, INET6_ADDRSTRLEN);
 			port = ((struct sockaddr_in6 *)(&sock->ss))->sin6_port;
-			if (res == NULL)
+			if (res == NULL && output)
 				perror("inet_ntop");
-			else
-				printf("%s:[%s]:%hu\n", sock->name, addr, ntohs(port));
+			else {
+				count++;
+				if (output)
+					printf("%s:[%s]:%hu\n", sock->name, addr, ntohs(port));
+			}
 			break;
 		default:
-			snprintf(addr, INET6_ADDRSTRLEN, "Unknown address family: %d\n",
+			if (output)
+				snprintf(addr, INET6_ADDRSTRLEN, "Unknown address family: %d\n",
 					sock->ss.ss_family);
 			addr[INET6_ADDRSTRLEN - 1] = '\0';
 		}
 	}
+	return count;
 }
 
 static bool ipv6_is_enabled(void)
@@ -1394,7 +1424,7 @@ static int listener_func(struct nl_sock *sock, int argc, char ** argv)
 		return set_listeners(sock);
 	}
 
-	print_listeners();
+	print_listeners(1);
 	return 0;
 }
 
Signature · 1 line
Jeff Layton <[email protected]>
#2 lore raw
Quoted 71 lines · Jeff Layton
Quoted 69 lines · Olga Kornievskaia

When a thread command is executed and yet no listeners have been added prior to it, instead of failing with EIO error print an informative error. Also, "thread 0" command should not error regardless of the listener status.

---
 utils/nfsdctl/nfsdctl.c | 52 ++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 11 deletions(-)
utils/nfsdctl/nfsdctl.c +23 -1
index 6a20d180..d03e2c9f 100644
@@ -162,6 +162,9 @@ static const char *nfsd4_ops[] = {
      [OP_REMOVEXATTR]        = "OP_REMOVEXATTR",
 };
 
+static int fetch_current_listeners(struct nl_sock *sock);
+static int print_listeners(int output);
+
 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
                       void *arg)
 {
@@ -720,7 +723,7 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)
      uint8_t cmd = NFSD_CMD_THREADS_GET;
      int *pool_threads = NULL;
      int minthreads = -1;
-     int opt, pools = 0;
+     int opt, pools = 0, zero_threads = 0;
 
      optind = 1;
      while ((opt = getopt_long(argc, argv, "hm:", threads_options, NULL)) != -1) {
@@ -762,12 +765,31 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)
                      }
 
                      pool_threads[i] = strtol(targv[i], &endptr, 0);
+                     if (!pool_threads[i])
+                             zero_threads++;
                      if (!endptr || *endptr != '\0') {
                              xlog(L_ERROR, "Invalid threads value %s.", argv[1]);
                              return 1;
                      }
              }
      }
+     /* check if there are listeners added */
+     if (fetch_current_listeners(sock)) {
+             xlog(L_ERROR, "Unable to determine if listeners were added\n");
+             return 1;
+     }
+     if (!print_listeners(0)) {
+             if (zero_threads && zero_threads == pools) {
+                     /* Note: we can never have a server with threads and no
+                      * listeners. If we ever add functionality to remove
+                      * listeners on an active server, we need to revisit this.
+                      */
+                     return 0;
+             }
+             xlog(L_ERROR, "No listeners added, not starting threads\n");
+             return 1;
+     }
+
      return threads_doit(sock, cmd, 0, 0, pools, pool_threads, NULL, minthreads);
 }
 
@@ -1077,9 +1099,9 @@ out:
      return ret;
 }
 
Quoted 5 lines

utils/nfsdctl/nfsdctl.c out: · line 1102

Quoted 2 lines
utils/nfsdctl/nfsdctl.c +1 -1
-static void print_listeners(void)
+static int print_listeners(int output)

Maybe instead of adding the "output" parameter, you could just add a count_listeners() function?

Well, I thought would need to do all the work of print_listener(), I would assume we'd want to do all the checks done by print_listener before it prints the value or in my case count it as a listener. And thus I opted for re-using the function but adding the count.

If you think it's sufficient to just check if sock->name is non-empty and active and do no further checking, I can.

Quoted 63 lines

Ack on the basic idea though!

Quoted 49 lines
utils/nfsdctl/nfsdctl.c +16 -8
 {
-     int i;
+     int i, count = 0;
      const char *res;
 
      for (i = 0; i < MAX_NFSD_SOCKETS; ++i) {
@@ -1098,26 +1120,34 @@ static void print_listeners(void)
                      res = inet_ntop(AF_INET, &((struct sockaddr_in *)(&sock->ss))->sin_addr,
                                      addr, INET6_ADDRSTRLEN);
                      port = ((struct sockaddr_in *)(&sock->ss))->sin_port;
-                     if (res == NULL)
+                     if (res == NULL && output)
                              perror("inet_ntop");
-                     else
-                             printf("%s:%s:%hu\n", sock->name, addr, ntohs(port));
+                     else {
+                             count++;
+                             if (output)
+                                     printf("%s:%s:%hu\n", sock->name, addr, ntohs(port));
+                     }
                      break;
              case AF_INET6:
                      res = inet_ntop(AF_INET6, &((struct sockaddr_in6 *)(&sock->ss))->sin6_addr,
                                      addr, INET6_ADDRSTRLEN);
                      port = ((struct sockaddr_in6 *)(&sock->ss))->sin6_port;
-                     if (res == NULL)
+                     if (res == NULL && output)
                              perror("inet_ntop");
-                     else
-                             printf("%s:[%s]:%hu\n", sock->name, addr, ntohs(port));
+                     else {
+                             count++;
+                             if (output)
+                                     printf("%s:[%s]:%hu\n", sock->name, addr, ntohs(port));
+                     }
                      break;
              default:
-                     snprintf(addr, INET6_ADDRSTRLEN, "Unknown address family: %d\n",
+                     if (output)
+                             snprintf(addr, INET6_ADDRSTRLEN, "Unknown address family: %d\n",
                                      sock->ss.ss_family);
                      addr[INET6_ADDRSTRLEN - 1] = '\0';
              }
      }
+     return count;
 }
 
 static bool ipv6_is_enabled(void)

utils/nfsdctl/nfsdctl.c static int listener_func(struct nl_sock *sock, int argc, char ** argv) · line 1430

Quoted 8 lines
utils/nfsdctl/nfsdctl.c +1 -1
@@ -1394,7 +1424,7 @@ static int listener_func(struct nl_sock *sock, int argc, char ** argv)
              return set_listeners(sock);
      }
 
-     print_listeners();
+     print_listeners(1);
      return 0;
 }
 

--
Jeff Layton <[email protected]>

maintainer
#3 lore raw
Quoted 78 lines · Olga Kornievskaia
Quoted 71 lines · Jeff Layton
Quoted 69 lines · Olga Kornievskaia

When a thread command is executed and yet no listeners have been added prior to it, instead of failing with EIO error print an informative error. Also, "thread 0" command should not error regardless of the listener status.

---
 utils/nfsdctl/nfsdctl.c | 52 ++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 11 deletions(-)
utils/nfsdctl/nfsdctl.c +23 -1
index 6a20d180..d03e2c9f 100644
@@ -162,6 +162,9 @@ static const char *nfsd4_ops[] = {
      [OP_REMOVEXATTR]        = "OP_REMOVEXATTR",
 };
 
+static int fetch_current_listeners(struct nl_sock *sock);
+static int print_listeners(int output);
+
 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
                       void *arg)
 {
@@ -720,7 +723,7 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)
      uint8_t cmd = NFSD_CMD_THREADS_GET;
      int *pool_threads = NULL;
      int minthreads = -1;
-     int opt, pools = 0;
+     int opt, pools = 0, zero_threads = 0;
 
      optind = 1;
      while ((opt = getopt_long(argc, argv, "hm:", threads_options, NULL)) != -1) {
@@ -762,12 +765,31 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)
                      }
 
                      pool_threads[i] = strtol(targv[i], &endptr, 0);
+                     if (!pool_threads[i])
+                             zero_threads++;
                      if (!endptr || *endptr != '\0') {
                              xlog(L_ERROR, "Invalid threads value %s.", argv[1]);
                              return 1;
                      }
              }
      }
+     /* check if there are listeners added */
+     if (fetch_current_listeners(sock)) {
+             xlog(L_ERROR, "Unable to determine if listeners were added\n");
+             return 1;
+     }
+     if (!print_listeners(0)) {
+             if (zero_threads && zero_threads == pools) {
+                     /* Note: we can never have a server with threads and no
+                      * listeners. If we ever add functionality to remove
+                      * listeners on an active server, we need to revisit this.
+                      */
+                     return 0;
+             }
+             xlog(L_ERROR, "No listeners added, not starting threads\n");
+             return 1;
+     }
+
      return threads_doit(sock, cmd, 0, 0, pools, pool_threads, NULL, minthreads);
 }
 
@@ -1077,9 +1099,9 @@ out:
      return ret;
 }
 
Quoted 5 lines

utils/nfsdctl/nfsdctl.c out: · line 1102

Quoted 2 lines
utils/nfsdctl/nfsdctl.c +1 -1
-static void print_listeners(void)
+static int print_listeners(int output)

Maybe instead of adding the "output" parameter, you could just add a count_listeners() function?

Quoted 8 lines

Well, I thought would need to do all the work of print_listener(), I would assume we'd want to do all the checks done by print_listener before it prints the value or in my case count it as a listener. And thus I opted for re-using the function but adding the count.

If you think it's sufficient to just check if sock->name is non-empty and active and do no further checking, I can.

The "active" flag is only used on the set side of things. Anything read by "get" is considered active. If you fetch_current_listeners() first, then I think you can just check that nfsd_socket_count is non-zero.

Signature · 1 line
Jeff Layton <[email protected]>