Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 29 Nov 2015 12:33:56 +0000 (UTC)
From:      Edward Tomasz Napierala <trasz@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r291451 - head/usr.bin/rctl
Message-ID:  <201511291233.tATCXuPv078753@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trasz
Date: Sun Nov 29 12:33:56 2015
New Revision: 291451
URL: https://svnweb.freebsd.org/changeset/base/291451

Log:
  Simplify rule retrieval and improve error handling.
  
  MFC after:	1 month
  Sponsored by:	The FreeBSD Foundation

Modified:
  head/usr.bin/rctl/rctl.c

Modified: head/usr.bin/rctl/rctl.c
==============================================================================
--- head/usr.bin/rctl/rctl.c	Sun Nov 29 12:23:08 2015	(r291450)
+++ head/usr.bin/rctl/rctl.c	Sun Nov 29 12:33:56 2015	(r291451)
@@ -387,20 +387,24 @@ show_limits(const char *filter, int hfla
 	char *outbuf = NULL;
 	size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
 
-	do {
+	for (;;) {
 		outbuflen *= 4;
 		outbuf = realloc(outbuf, outbuflen);
 		if (outbuf == NULL)
 			err(1, "realloc");
+		error = rctl_get_limits(filter, strlen(filter) + 1,
+		    outbuf, outbuflen);
+		if (error == 0)
+			break;
+		if (errno == ERANGE)
+			continue;
+		if (errno == ENOSYS)
+			enosys();
+		warn("rctl_get_limits");
+		free(outbuf);
 
-		error = rctl_get_limits(filter, strlen(filter) + 1, outbuf,
-		    outbuflen);
-		if (error && errno != ERANGE) {
-			if (errno == ENOSYS)
-				enosys();
-			warn("rctl_get_limits");
-		}
-	} while (error && errno == ERANGE);
+		return (error);
+	}
 
 	print_rules(outbuf, hflag, nflag);
 	free(outbuf);
@@ -466,20 +470,24 @@ show_usage(const char *filter, int hflag
 	char *copy, *outbuf = NULL, *tmp;
 	size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4;
 
-	do {
+	for (;;) {
 		outbuflen *= 4;
 		outbuf = realloc(outbuf, outbuflen);
 		if (outbuf == NULL)
 			err(1, "realloc");
+		error = rctl_get_racct(filter, strlen(filter) + 1,
+		    outbuf, outbuflen);
+		if (error == 0)
+			break;
+		if (errno == ERANGE)
+			continue;
+		if (errno == ENOSYS)
+			enosys();
+		warn("rctl_get_racct");
+		free(outbuf);
 
-		error = rctl_get_racct(filter, strlen(filter) + 1, outbuf,
-		    outbuflen);
-		if (error && errno != ERANGE) {
-			if (errno == ENOSYS)
-				enosys();
-			warn("rctl_get_racct");
-		}
-	} while (error && errno == ERANGE);
+		return (error);
+	}
 
 	copy = outbuf;
 	while ((tmp = strsep(&copy, ",")) != NULL) {
@@ -512,19 +520,23 @@ show_rules(const char *filter, int hflag
 	else
 		filterlen = 0;
 
-	do {
+	for (;;) {
 		outbuflen *= 4;
 		outbuf = realloc(outbuf, outbuflen);
 		if (outbuf == NULL)
 			err(1, "realloc");
-
 		error = rctl_get_rules(filter, filterlen, outbuf, outbuflen);
-		if (error && errno != ERANGE) {
-			if (errno == ENOSYS)
-				enosys();
-			warn("rctl_get_rules");
-		}
-	} while (error && errno == ERANGE);
+		if (error == 0)
+			break;
+		if (errno == ERANGE)
+			continue;
+		if (errno == ENOSYS)
+			enosys();
+		warn("rctl_get_rules");
+		free(outbuf);
+
+		return (error);
+	}
 
 	print_rules(outbuf, hflag, nflag);
 	free(outbuf);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201511291233.tATCXuPv078753>