[CRIU] [scripts 1/2] stopexec: Log failed execvp() calls

Christopher Covington cov at codeaurora.org
Thu Nov 3 20:05:13 PDT 2016


From: Aaron Lindsay <alindsay at codeaurora.org>

Also move to returning errno instead of the return code from the
execvp() call itself, which is always -1 if it returns.

Signed-off-by: Christopher Covington <cov at codeaurora.org>
---
 stopexec.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/stopexec.c b/stopexec.c
index 329f601..c85e02e 100644
--- a/stopexec.c
+++ b/stopexec.c
@@ -16,13 +16,15 @@
  */
 
 #include <errno.h>
+#include <fcntl.h>
 #include <signal.h>
 #include <stdio.h>
+#include <string.h>
 #include <unistd.h>
 
 int main(int argc, char *argv[])
 {
-	FILE *fp;
+	int fd, err;
 
 	if (argc < 3) {
 		printf("Typical usage: %s logfile -- program [args...] &\n",
@@ -30,17 +32,23 @@ int main(int argc, char *argv[])
 		return -1;
 	}
 
-	fp = fopen(argv[1], "w");
-	if (fp == NULL) {
+	fd = open(argv[1], O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+	if (fd < 0) {
 		printf("Error opening %s.\n", argv[1]);
 		return errno;
 	}
 
 	/* Set session ID to make process easily `criu dump`-able */
 	setsid();
-	fprintf(fp, "Stopping pid %d before exec().\n", getpid());
-	fclose(fp);
+	dprintf(fd, "Stopping pid %d before exec().\n", getpid());
 	raise(SIGSTOP);
 
-	return execvp(argv[3], &argv[3]);
+	/* Run application */
+	execvp(argv[3], &argv[3]);
+	err = errno;
+
+	/* Log and return error if execvp failed */
+	dprintf(fd, "Error executing binary: %s (errno: %d)\n", strerror(err), err);
+	close(fd);
+	return err;
 }
-- 
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora
Forum, a Linux Foundation Collaborative Project.



More information about the CRIU mailing list