libhugetlbfs/SOURCES/0008-hugeutils-Make-writing-a-ulong-to-a-file-more-reliab.patch
2021-09-09 20:20:27 +00:00

54 lines
1.6 KiB
Diff

From 4ba60a2f5c3f5405c599caddc5a124c5781c9beb Mon Sep 17 00:00:00 2001
Message-Id: <4ba60a2f5c3f5405c599caddc5a124c5781c9beb.1566225007.git.aquini@redhat.com>
In-Reply-To: <d42f467a923dfc09309acb7a83b42e3285fbd8f4.1566225007.git.aquini@redhat.com>
References: <d42f467a923dfc09309acb7a83b42e3285fbd8f4.1566225007.git.aquini@redhat.com>
From: Sandipan Das <sandipan@linux.ibm.com>
Date: Thu, 15 Aug 2019 13:08:35 +0530
Subject: [RHEL7 PATCH 08/31] hugeutils: Make writing a ulong to a file more
reliable
This makes file_write_ulong() more reliable in terms of error
detection for certain cases like writing an invalid value to
a file under procfs or sysfs. Also, using fprintf() does not
guarantee that errno would be set under such circumstances.
Signed-off-by: Sandipan Das <sandipan@linux.ibm.com>
Signed-off-by: Eric B Munson <emunson@mgebm.net>
Signed-off-by: Rafael Aquini <aquini@redhat.com>
---
hugeutils.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/hugeutils.c b/hugeutils.c
index 60488e8..fc64946 100644
--- a/hugeutils.c
+++ b/hugeutils.c
@@ -219,17 +219,18 @@ long file_read_ulong(char *file, const char *tag)
int file_write_ulong(char *file, unsigned long val)
{
- FILE *f;
- int ret;
+ int fd, ret, buflen;
+ char buf[20];
- f = fopen(file, "w");
- if (!f) {
+ fd = open(file, O_WRONLY);
+ if (fd < 0) {
ERROR("Couldn't open %s: %s\n", file, strerror(errno));
return -1;
}
- ret = fprintf(f, "%lu", val);
- fclose(f);
+ buflen = sprintf(buf, "%lu", val);
+ ret = write(fd, buf, buflen);
+ close(fd);
return ret > 0 ? 0 : -1;
}
--
1.8.3.1