changeset 4945:9b5c1ee2fe77 draft

Linux: Improve privsep code a little
author Roy Marples <roy@marples.name>
date Wed, 08 Jan 2020 15:58:57 +0000
parents 572ef46ecde3
children c80386966f1f
files src/privsep-linux.c
diffstat 1 files changed, 14 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/privsep-linux.c	Wed Jan 08 14:26:01 2020 +0000
+++ b/src/privsep-linux.c	Wed Jan 08 15:58:57 2020 +0000
@@ -29,6 +29,7 @@
 #include <sys/ioctl.h>
 
 #include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -61,10 +62,10 @@
 ps_root_dowritepathuint(const void *data, size_t len)
 {
 	const char *path = data;
-	FILE *fp;
-	ssize_t r;
 	size_t plen;
 	unsigned int val;
+	int fd;
+	ssize_t r;
 
 	if (len < sizeof(plen)) {
 		errno = EINVAL;
@@ -80,11 +81,12 @@
 
 	memcpy(&val, path + plen, sizeof(val));
 
-	fp = fopen(path, "w");
-	if (fp == NULL)
+	fd = open(path, O_WRONLY);
+	if (fd == -1)
 		return -1;
-	r = fprintf(fp, "%u\n", val);
-	fclose(fp);
+	r = dprintf(fd, "%u", val);
+	close(fd);
+
 	return r;
 }
 
@@ -120,7 +122,7 @@
 ps_root_writepathuint(struct dhcpcd_ctx *ctx, const char *path,
     unsigned int val)
 {
-	char buf[PS_BUFLEN];
+	char buf[PS_BUFLEN], *p = buf;
 	size_t plen = strlen(path) + 1;
 	size_t len = sizeof(plen) + plen + sizeof(val);
 
@@ -129,9 +131,11 @@
 		return -1;
 	}
 
-	memcpy(buf, &plen, sizeof(plen));
-	memcpy(buf + sizeof(plen), path, plen);
-	memcpy(buf + sizeof(plen) + plen, &val, sizeof(val));
+	memcpy(p, &plen, sizeof(plen));
+	p += sizeof(plen);
+	memcpy(p, path, plen);
+	p += plen;
+	memcpy(p, &val, sizeof(val));
 
 	return ps_sendcmd(ctx, ctx->ps_root_fd, PS_WRITEPATHUINT, 0, buf, len);
 }