[lxc-devel] [lxc/master] tree-wide: kill fgets()
brauner on Github
lxc-bot at linuxcontainers.org
Fri Mar 1 11:18:37 UTC 2019
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190301/6202da10/attachment.bin>
-------------- next part --------------
From ebf3a6af232ed85c24a691c3da52676980930e3c Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 1 Mar 2019 12:00:42 +0100
Subject: [PATCH 1/3] conf: remove fgets() from run_buffer()
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/conf.c | 23 ++++++++++++++++++++---
src/lxc/utils.h | 8 ++++----
2 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index c0ac73be2a..95b197e1c4 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -307,7 +307,7 @@ static struct limit_opt limit_opt[] = {
static int run_buffer(char *buffer)
{
__do_free char *output = NULL;
- int ret;
+ int fd, ret;
struct lxc_popen_FILE *f;
f = lxc_popen(buffer);
@@ -323,8 +323,25 @@ static int run_buffer(char *buffer)
return -1;
}
- while (fgets(output, LXC_LOG_BUFFER_SIZE, f->f))
- DEBUG("Script %s with output: %s", buffer, output);
+ fd = fileno(f->f);
+ if (fd < 0) {
+ SYSERROR("Failed to retrieve underlying file descriptor");
+ lxc_pclose(f);
+ return -1;
+ }
+
+ for (int i = 0; i < 10; i++) {
+ ssize_t bytes_read;
+
+ bytes_read = lxc_read_nointr(fd, output, LXC_LOG_BUFFER_SIZE - 1);
+ if (bytes_read > 0) {
+ output[bytes_read] = '\0';
+ DEBUG("Script %s produced output: %s", buffer, output);
+ continue;
+ }
+
+ break;
+ }
ret = lxc_pclose(f);
if (ret == -1) {
diff --git a/src/lxc/utils.h b/src/lxc/utils.h
index 6314b7985a..286baaabcb 100644
--- a/src/lxc/utils.h
+++ b/src/lxc/utils.h
@@ -61,10 +61,10 @@ static inline int lxc_set_cloexec(int fd)
return fcntl(fd, F_SETFD, FD_CLOEXEC);
}
-/* Struct to carry child pid from lxc_popen() to lxc_pclose().
- * Not an opaque struct to allow direct access to the underlying FILE *
- * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f))
- * without additional wrappers.
+/*
+ * Struct to carry child pid from lxc_popen() to lxc_pclose(). Not an opaque
+ * struct to allow direct access to the underlying FILE without additional
+ * wrappers.
*/
struct lxc_popen_FILE {
int pipe;
From c9c7b842a0035011d2a719c1bafa2bd6b5505838 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 1 Mar 2019 12:12:15 +0100
Subject: [PATCH 2/3] conf: remove fgets() from lxc_chroot()
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/conf.c | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 95b197e1c4..9797848b8f 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -1364,8 +1364,6 @@ int lxc_chroot(const struct lxc_rootfs *rootfs)
{
__do_free char *nroot = NULL;
int i, ret;
- char *p, *p2;
- char buf[LXC_LINELEN];
char *root = rootfs->mount;
nroot = realpath(root, NULL);
@@ -1405,7 +1403,10 @@ int lxc_chroot(const struct lxc_rootfs *rootfs)
*/
for (;;) {
__do_fclose FILE *f = NULL;
+ char *slider1, *slider2;
int progress = 0;
+ size_t len = 0;
+ char *line = NULL;
f = fopen("./proc/self/mountinfo", "r");
if (!f) {
@@ -1413,27 +1414,27 @@ int lxc_chroot(const struct lxc_rootfs *rootfs)
return -1;
}
- while (fgets(buf, LXC_LINELEN, f)) {
- for (p = buf, i=0; p && i < 4; i++)
- p = strchr(p+1, ' ');
+ while (getline(&line, &len, f) > 0) {
+ for (slider1 = line, i = 0; slider1 && i < 4; i++)
+ slider1 = strchr(slider1 + 1, ' ');
- if (!p)
+ if (!slider1)
continue;
- p2 = strchr(p+1, ' ');
- if (!p2)
+ slider2 = strchr(slider1 + 1, ' ');
+ if (!slider2)
continue;
- *p2 = '\0';
- *p = '.';
+ *slider2 = '\0';
+ *slider1 = '.';
- if (strcmp(p + 1, "/") == 0)
+ if (strcmp(slider1 + 1, "/") == 0)
continue;
- if (strcmp(p + 1, "/proc") == 0)
+ if (strcmp(slider1 + 1, "/proc") == 0)
continue;
- ret = umount2(p, MNT_DETACH);
+ ret = umount2(slider1, MNT_DETACH);
if (ret == 0)
progress++;
}
From ec903c1fe406382dd00ce2a666f947b67553e7e6 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 1 Mar 2019 12:15:28 +0100
Subject: [PATCH 3/3] utils: remove fgets() from is_shared_mountpoint()
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/utils.c | 33 +++++++++++++++++----------------
1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index be81727c3c..928dd26220 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -49,6 +49,7 @@
#include "config.h"
#include "log.h"
#include "lxclock.h"
+#include "memory_utils.h"
#include "namespace.h"
#include "parse.h"
#include "raw_syscalls.h"
@@ -641,37 +642,37 @@ uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
bool is_shared_mountpoint(const char *path)
{
- char buf[LXC_LINELEN];
- FILE *f;
+ __do_fclose FILE *f = NULL;
int i;
- char *p, *p2;
+ char *line = NULL;
+ size_t len = 0;
f = fopen("/proc/self/mountinfo", "r");
if (!f)
return 0;
- while (fgets(buf, LXC_LINELEN, f)) {
- for (p = buf, i = 0; p && i < 4; i++)
- p = strchr(p + 1, ' ');
- if (!p)
+ while (getline(&line, &len, f) > 0) {
+ char *slider1, *slider2;
+
+ for (slider1 = line, i = 0; slider1 && i < 4; i++)
+ slider1 = strchr(slider1 + 1, ' ');
+
+ if (!slider1)
continue;
- p2 = strchr(p + 1, ' ');
- if (!p2)
+ slider2 = strchr(slider1 + 1, ' ');
+ if (!slider2)
continue;
- *p2 = '\0';
- if (strcmp(p + 1, path) == 0) {
+ *slider2 = '\0';
+ if (strcmp(slider1 + 1, path) == 0) {
/* This is the path. Is it shared? */
- p = strchr(p2 + 1, ' ');
- if (p && strstr(p, "shared:")) {
- fclose(f);
+ slider1 = strchr(slider2 + 1, ' ');
+ if (slider1 && strstr(slider1, "shared:"))
return true;
- }
}
}
- fclose(f);
return false;
}
More information about the lxc-devel
mailing list