]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: remove fgets() from run_buffer()
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 1 Mar 2019 11:00:42 +0000 (12:00 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 1 Mar 2019 11:17:46 +0000 (12:17 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c
src/lxc/utils.h

index c0ac73be2a97ab10d23a228f2dce0fb0985acaf2..95b197e1c45bc6528595eec1fdd26a12a22466b2 100644 (file)
@@ -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) {
index 6314b7985a8477ea77db5a02d9e16470de45aa5e..286baaabcb917a2210c8229772333e1088272c15 100644 (file)
@@ -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;