]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/rexec.c
github: Update for main branch
[mirror_lxc.git] / src / lxc / rexec.c
index cd76efb3c4c5f94d9f6e40bfd3356af02678662f..6a017da9c62cb51575b7ac84e4008c8766e33b36 100644 (file)
@@ -1,24 +1,23 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE 1
-#endif
+#include "config.h"
+
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-#include "config.h"
 #include "file_utils.h"
 #include "macro.h"
 #include "memory_utils.h"
-#include "raw_syscalls.h"
+#include "process_utils.h"
+#include "rexec.h"
 #include "string_utils.h"
 #include "syscall_wrappers.h"
 
-#if IS_BIONIC
-#include "../include/fexecve.h"
+#if IS_BIONIC && !HAVE_FEXECVE
+#include "fexecve.h"
 #endif
 
 #define LXC_MEMFD_REXEC_SEALS \
@@ -97,9 +96,9 @@ static void lxc_rexec_as_memfd(char **argv, char **envp, const char *memfd_name)
        if (memfd < 0) {
                char template[PATH_MAX];
 
-               ret = snprintf(template, sizeof(template),
-                              P_tmpdir "/.%s_XXXXXX", memfd_name);
-               if (ret < 0 || (size_t)ret >= sizeof(template))
+               ret = strnprintf(template, sizeof(template),
+                                P_tmpdir "/.%s_XXXXXX", memfd_name);
+               if (ret < 0)
                        return;
 
                tmpfd = lxc_make_tmpfile(template, true);
@@ -126,10 +125,13 @@ static void lxc_rexec_as_memfd(char **argv, char **envp, const char *memfd_name)
                sent = lxc_sendfile_nointr(memfd >= 0 ? memfd : tmpfd, fd, NULL,
                                           st.st_size - bytes_sent);
                if (sent < 0) {
-                       /* Fallback to shoveling data between kernel- and
+                       /*
+                        * Fallback to shoveling data between kernel- and
                         * userspace.
                         */
-                       lseek(fd, 0, SEEK_SET);
+                       if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
+                               fprintf(stderr, "Failed to seek to beginning of file");
+
                        if (fd_to_fd(fd, memfd >= 0 ? memfd : tmpfd))
                                break;
 
@@ -147,8 +149,8 @@ static void lxc_rexec_as_memfd(char **argv, char **envp, const char *memfd_name)
        } else {
                char procfd[LXC_PROC_PID_FD_LEN];
 
-               ret = snprintf(procfd, sizeof(procfd), "/proc/self/fd/%d", tmpfd);
-               if (ret < 0 || (size_t)ret >= sizeof(procfd))
+               ret = strnprintf(procfd, sizeof(procfd), "/proc/self/fd/%d", tmpfd);
+               if (ret < 0)
                        return;
 
                execfd = open(procfd, O_PATH | O_CLOEXEC);