From 2de0b4dddb98fa70874eb96a4a9dc33c12037db4 Mon Sep 17 00:00:00 2001
From: chegJH <hejunjie10@huawei.com>
Date: Tue, 15 Feb 2022 16:13:56 +0800
Subject: [PATCH] changes for compile in android env

Signed-off-by: chegJH <hejunjie10@huawei.com>
---
 configure.ac               |  3 ++-
 src/lxc/Makefile.am        |  3 ++-
 src/lxc/commands_utils.c   |  8 +++++++-
 src/lxc/confile.c          |  6 +++---
 src/lxc/json/read-file.c   |  2 +-
 src/lxc/log.c              |  2 +-
 src/lxc/lxclock.c          |  1 +
 src/lxc/syscall_wrappers.h |  2 +-
 src/lxc/utils.c            | 21 +++++++--------------
 9 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/configure.ac b/configure.ac
index d1d793b..7766638 100644
--- a/configure.ac
+++ b/configure.ac
@@ -771,7 +771,8 @@ AX_CHECK_LINK_FLAG([-z relro], [LDFLAGS="$LDFLAGS -z relro"],,[])
 AX_CHECK_LINK_FLAG([-z now], [LDFLAGS="$LDFLAGS -z now"],,[])
 AX_CHECK_LINK_FLAG([-z noexecstack], [LDFLAGS="$LDFLAGS -z noexecstack"],,[])
 
-CFLAGS="$CFLAGS -Wvla -std=gnu11 -D_FORTIFY_SOURCE=2 -Wall -fPIC -fPIE -pie"
+CFLAGS="$CFLAGS -Wvla -std=gnu11 -D_FORTIFY_SOURCE=2 -Wall -fPIC -fPIE"
+LDFLAGS="$LDFLAGS -pie"
 if test "x$enable_werror" = "xyes"; then
 	CFLAGS="$CFLAGS -Werror"
 fi
diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index dc49c7e..2686e24 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -361,7 +361,8 @@ LDADD = liblxc.la \
 	@OPENSSL_LIBS@ \
 	@SECCOMP_LIBS@ \
 	@SELINUX_LIBS@ \
-	@DLOG_LIBS@
+	@DLOG_LIBS@ \
+	@YAJL_LIBS@
 
 if ENABLE_TOOLS
 lxc_attach_SOURCES = tools/lxc_attach.c \
diff --git a/src/lxc/commands_utils.c b/src/lxc/commands_utils.c
index 7dfefa5..54ba26e 100644
--- a/src/lxc/commands_utils.c
+++ b/src/lxc/commands_utils.c
@@ -141,9 +141,15 @@ int lxc_make_abstract_socket_name(char *path, size_t pathlen,
 char *generate_named_unix_sock_dir(const char *name)
 {
 	__do_free char *exec_sock_dir = NULL;
+	__do_free char *rundir = NULL;
 
-	if (asprintf(&exec_sock_dir, "/var/run/lxc/%s", name) < 0)
+	rundir = get_rundir();
+	if (!rundir)
+		rundir = strdup("/var/run");
+
+	if (asprintf(&exec_sock_dir, "%s/lxc/%s", rundir, name) < 0) {
 		return log_error_errno(NULL, errno, "Failed to allocate memory");
+	}
 
 	return move_ptr(exec_sock_dir);
 }
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index e298ce9..cc53148 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -6239,21 +6239,21 @@ static int set_config_init_args(const char *key, const char *value,
 				 struct lxc_conf *lxc_conf, void *data)
 {
 	int ret = 0;
-	char *tmp = NULL;
+	char **tmp = NULL;
 	char *new_value = NULL;
 
 	ret = set_config_string_item(&new_value, value);
 	if (ret || !new_value)
 		return ret;
 
-	tmp = realloc(lxc_conf->init_argv, (lxc_conf->init_argc + 1) * sizeof(char *));
+	tmp = (char **)realloc(lxc_conf->init_argv, (lxc_conf->init_argc + 1) * sizeof(char *));
 	if (!tmp) {
 		ERROR("Out of memory");
 		free(new_value);
 		return -1;
 	}
 
-	lxc_conf->init_argv = (char **)tmp;
+	lxc_conf->init_argv = tmp;
 
 	lxc_conf->init_argv[lxc_conf->init_argc] = new_value;
 	lxc_conf->init_argc++;
diff --git a/src/lxc/json/read-file.c b/src/lxc/json/read-file.c
index 70e73e5..34ebeed 100644
--- a/src/lxc/json/read-file.c
+++ b/src/lxc/json/read-file.c
@@ -76,7 +76,7 @@ char *read_file(const char *path, size_t *length)
 		return NULL;
 	}
 
-	fd = open(rpath, O_RDONLY | O_CLOEXEC, 0640);
+	fd = open(rpath, O_RDONLY | O_CLOEXEC);
 	if (fd < 0) {
 		return NULL;
 	}
diff --git a/src/lxc/log.c b/src/lxc/log.c
index 79caa2c..a04f78e 100644
--- a/src/lxc/log.c
+++ b/src/lxc/log.c
@@ -71,7 +71,7 @@ static int isulad_open_fifo(const char *file_path)
 #define LOG_FIFO_SIZE (1024 * 1024)
 	int fd;
 
-	fd = lxc_unpriv(open(file_path, O_RDWR | O_NONBLOCK | O_CLOEXEC, 0640));
+	fd = lxc_unpriv(open(file_path, O_RDWR | O_NONBLOCK | O_CLOEXEC));
 	if (fd == -1) {
 		fprintf(stderr, "Open fifo %s failed: %s\n", file_path, strerror(errno));
 		return -1;
diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c
index bb0dca0..d65c614 100644
--- a/src/lxc/lxclock.c
+++ b/src/lxc/lxclock.c
@@ -179,6 +179,7 @@ struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name)
 	l->u.f.fd = -1;
 
 on_error:
+	fprintf(stderr, "Failed to create lock for %s, path %s\n", name, lxcpath);
 	return l;
 }
 
diff --git a/src/lxc/syscall_wrappers.h b/src/lxc/syscall_wrappers.h
index 1cef215..1c8e652 100644
--- a/src/lxc/syscall_wrappers.h
+++ b/src/lxc/syscall_wrappers.h
@@ -62,7 +62,7 @@ extern int memfd_create(const char *name, unsigned int flags);
 #endif
 
 #ifndef HAVE_PIVOT_ROOT
-static int pivot_root(const char *new_root, const char *put_old)
+static inline int pivot_root(const char *new_root, const char *put_old)
 {
 	return syscall(__NR_pivot_root, new_root, put_old);
 }
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 95c00cf..b39b6a8 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -2081,7 +2081,10 @@ void lxc_write_error_message(int errfd, const char *format, ...)
 		return;
 
 	va_start(argp, format);
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
 	ret = vsnprintf(errbuf, BUFSIZ, format, argp);
+#pragma GCC diagnostic pop
 	va_end(argp);
 	if (ret < 0 || ret >= BUFSIZ)
 		SYSERROR("Failed to call vsnprintf");
@@ -2210,30 +2213,20 @@ out:
 // isulad: set env home in container
 int lxc_setup_env_home(uid_t uid)
 {
-#define __PASSWD_FILE__ "/etc/passwd"
 	char *homedir = "/"; // default home dir is /
-	FILE *stream = NULL;
 	struct passwd pw, *pwbufp = NULL;
 	char buf[BUFSIZ];
+	int ret;
 
-	stream = fopen_cloexec(__PASSWD_FILE__, "r");
-	if (stream == NULL) {
-		SYSWARN("Failed to open %s", __PASSWD_FILE__);
+	ret = getpwuid_r(uid, &pw, buf, sizeof(buf), &pwbufp);
+	if ((ret == 0) && (pwbufp != NULL) && (pwbufp->pw_uid == uid)) {
+		homedir = pwbufp->pw_dir;
 		goto set_env;
 	}
 
-	while (fgetpwent_r(stream, &pw, buf, sizeof(buf), &pwbufp) == 0 && pwbufp != NULL) {
-		if (pwbufp->pw_uid == uid) {
-			homedir = pwbufp->pw_dir;
-			goto set_env;
-		}
-	}
 	WARN("User invalid, can not find user '%u'", uid);
 
 set_env:
-	if (stream)
-		fclose(stream);
-
 	// if we didn't configure HOME, set it based on uid
 	if (setenv("HOME", homedir, 0) < 0) {
 		SYSERROR("Unable to set env 'HOME'");
-- 
2.32.0 (Apple Git-132)

