File 0003-Use-new-standard-I-O-for-reading-writing-sysctl-valu.patch of Package procps

From 97079405b6c7c7c39234d69c548d2f2fea4aa77e Mon Sep 17 00:00:00 2001
From: Werner Fink <werner@suse.de>
Date: Thu, 18 Jan 2018 11:38:02 +0100
Subject: [PATCH 3/3] Use new standard I/O for reading/writing sysctl values

thereby use one allocated buffer for I/O which now might
be increased by the stdio function getline(3) on the
file if required.

Signed-off-by: Werner Fink <werner@suse.de>
---
 sysctl.c |   53 +++++++++++++++++++++++------------------------------
 1 file changed, 23 insertions(+), 30 deletions(-)

--- sysctl.c
+++ sysctl.c	2018-01-30 09:34:51.429936410 +0000
@@ -46,6 +46,7 @@
 #include "fileutils.h"
 #include "nls.h"
 #include "xalloc.h"
+#include "proc/procio.h"
 #include "proc/procps.h"
 #include "proc/version.h"
 
@@ -68,6 +69,10 @@ static bool IgnoreError;
 static bool Quiet;
 static char *pattern;
 
+#define LINELEN 4096
+static char *iobuf;
+static size_t iolen = LINELEN;
+
 /* Function prototypes. */
 static int pattern_match(const char *string, const char *pat);
 static int DisplayAll(const char *restrict const path);
@@ -160,14 +165,12 @@ static char *StripLeadingAndTrailingSpac
 /*
  * Read a sysctl setting
  */
-#define IOBUFSIZ    (128<<10)
-static char *iobuf;
 static int ReadSetting(const char *restrict const name)
 {
 	int rc = 0;
 	char *restrict tmpname;
 	char *restrict outname;
-	char inbuf[1025];
+	ssize_t rlen;
 	FILE *restrict fp;
 	struct stat ts;
 
@@ -222,7 +225,7 @@ static int ReadSetting(const char *restr
 		goto out;
 	}
 
-	fp = fopen(tmpname, "r");
+	fp = fprocopen(tmpname, "r");
 
 	if (!fp) {
 		switch (errno) {
@@ -245,10 +248,8 @@ static int ReadSetting(const char *restr
 			break;
 		}
 	} else {
-		if (iobuf)
-			setvbuf(fp, iobuf, _IOFBF, IOBUFSIZ);
 		errno = 0;
-		if (fgets(inbuf, sizeof inbuf - 1, fp)) {
+		if ((rlen = getline(&iobuf, &iolen, fp)) > 0) {
 			/* this loop is required, see
 			 * /sbin/sysctl -a | egrep -6 dev.cdrom.info
 			 */
@@ -257,23 +258,23 @@ static int ReadSetting(const char *restr
 				if (PrintName) {
 					fprintf(stdout, "%s = ", outname);
 					do {
-						fprintf(stdout, "%s", inbuf);
-						nlptr = &inbuf[strlen(inbuf) - 1];
+						fprintf(stdout, "%s", iobuf);
+						nlptr = &iobuf[strlen(iobuf) - 1];
 						/* already has the \n in it */
 						if (*nlptr == '\n')
 							break;
-					} while (fgets(inbuf, sizeof inbuf - 1, fp));
+					} while ((rlen = getline(&iobuf, &iolen, fp)) > 0);
 					if (*nlptr != '\n')
 						putchar('\n');
 				} else {
 					if (!PrintNewline) {
-						nlptr = strchr(inbuf, '\n');
+						nlptr = strchr(iobuf, '\n');
 						if (nlptr)
 							*nlptr = '\0';
 					}
-					fprintf(stdout, "%s", inbuf);
+					fprintf(stdout, "%s", iobuf);
 				}
-			} while (fgets(inbuf, sizeof inbuf - 1, fp));
+			} while ((rlen = getline(&iobuf, &iolen, fp)) > 0);
 		} else {
 			switch (errno) {
 			case EACCES:
@@ -440,7 +441,7 @@ static int WriteSetting(const char *sett
 		goto out;
 	}
 
-	fp = fopen(tmpname, "w");
+	fp = fprocopen(tmpname, "w");
 
 	if (!fp) {
 		switch (errno) {
@@ -460,8 +461,6 @@ static int WriteSetting(const char *sett
 			break;
 		}
 	} else {
-		if (iobuf)
-			setvbuf(fp, iobuf, _IOFBF, IOBUFSIZ);
 		rc = fprintf(fp, "%s\n", value);
 		if (0 < rc)
 			rc = 0;
@@ -503,20 +502,16 @@ static int pattern_match(const char *str
 	return (1);
 }
 
-#define LINELEN 4096
-
 /*
  * Preload the sysctl's from the conf file.  We parse the file and then
  * reform it (strip out whitespace).
  */
 static int Preload(const char *restrict const filename)
 {
-	char *oneline;
 	FILE *fp;
 	char *t;
 	int n = 0;
 	int rc = 0;
-	size_t blen = LINELEN;
 	ssize_t rlen;
 	char *name, *value;
 	glob_t globbuf;
@@ -524,8 +519,6 @@ static int Preload(const char *restrict
 	int globflg;
 	int j;
 
-	oneline = xmalloc(blen);
-
 	globflg = GLOB_NOCHECK;
 #ifdef GLOB_BRACE
 	globflg |= GLOB_BRACE;
@@ -551,7 +544,7 @@ static int Preload(const char *restrict
 			goto out;
 		}
 
-		while ((rlen =  getline(&oneline, &blen, fp)) != -1) {
+		while ((rlen =  getline(&iobuf, &iolen, fp)) > 0) {
 			size_t offset;
 
 			n++;
@@ -559,7 +552,7 @@ static int Preload(const char *restrict
 			if (rlen < 2)
 				continue;
 
-			t = StripLeadingAndTrailingSpaces(oneline);
+			t = StripLeadingAndTrailingSpaces(iobuf);
 			if (strlen(t) < 2)
 				continue;
 
@@ -579,8 +572,8 @@ static int Preload(const char *restrict
 				continue;
 
 			offset = strlen(name);
-			memmove(&oneline[0], name, offset);
-			oneline[offset++] = '=';
+			memmove(&iobuf[0], name, offset);
+			iobuf[offset++] = '=';
 
 			value = strtok(NULL, "\n\r");
 			if (!value || !*value) {
@@ -593,11 +586,11 @@ static int Preload(const char *restrict
 				value++;
 
 			/* should NameOnly affect this? */
-			memmove(&oneline[offset], value, strlen(value));
+			memmove(&iobuf[offset], value, strlen(value));
 			offset += strlen(value);
-			oneline[offset] = '\0';
+			iobuf[offset] = '\0';
 
-			rc |= WriteSetting(oneline);
+			rc |= WriteSetting(iobuf);
 		}
 
 		fclose(fp);
@@ -835,7 +828,7 @@ int main(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	iobuf = (char*)malloc(IOBUFSIZ);	/* Allow to fail */
+	iobuf = xmalloc(iolen);
 
 	if (DisplayAllOpt)
 		return DisplayAll(PROC_PATH);
openSUSE Build Service is sponsored by