From 484852b127dab5f5548ed34d5bb668b18e4dc99e Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Fri, 9 Dec 2022 07:01:31 +0000
Subject: [PATCH 58/65] !1757 add adaption code for musl. * add adaption code
 for musl.

---
 cmake/checker.cmake                                 |  2 +-
 cmake/options.cmake                                 | 12 ++++++++++++
 cmake/set_build_flags.cmake                         |  5 +++++
 src/CMakeLists.txt                                  |  6 +++---
 src/daemon/common/selinux_label.c                   |  2 +-
 src/daemon/common/sysinfo.c                         |  4 ++--
 src/daemon/executor/container_cb/execution_stream.c |  2 +-
 src/daemon/modules/image/CMakeLists.txt             |  2 +-
 src/daemon/modules/image/image.c                    |  6 ++++--
 src/daemon/modules/image/image_rootfs_handler.c     | 12 ++++++------
 src/utils/cutils/CMakeLists.txt                     |  2 +-
 src/utils/cutils/utils.c                            | 13 +++++++++----
 src/utils/cutils/utils.h                            |  2 +-
 src/utils/cutils/utils_verify.c                     |  8 ++++----
 14 files changed, 51 insertions(+), 27 deletions(-)

diff --git a/cmake/checker.cmake b/cmake/checker.cmake
index cbcfc929..fea4f925 100644
--- a/cmake/checker.cmake
+++ b/cmake/checker.cmake
@@ -54,7 +54,7 @@ find_library(CRYPTO_LIBRARY crypto
     HINTS ${PC_CRYPTO_LIBDIR} ${PC_LIBCRYPTO_LIBRARY_DIRS})
 _CHECK(CRYPTO_LIBRARY "CRYPTO_LIBRARY-NOTFOUND" "libcrypto.so")
 
-if (ANDROID)
+if (ANDROID OR MUSL)
     # check libssl
     find_library(LIBSSL_LIBRARY ssl)
     _CHECK(CRYPTO_LIBRARY "LIBSSL_LIBRARY-NOTFOUND" "libssl.so")
diff --git a/cmake/options.cmake b/cmake/options.cmake
index 7a141fef..0d44868a 100644
--- a/cmake/options.cmake
+++ b/cmake/options.cmake
@@ -115,6 +115,18 @@ if (ENABLE_SUP_GROUPS)
     message("${Green}--  Enable sup groups${ColourReset}")
 endif()
 
+option(MUSL "available for musl" OFF)
+if (MUSL)
+    add_definitions(-D__MUSL__)
+    message("${Green}--  Available for MUSL${ColourReset}")
+endif()
+
+option(ANDROID "available for android" OFF)
+if (ANDROID)
+    add_definitions(-D__ANDROID__)
+    message("${Green}--  Available for ANDROID${ColourReset}")
+endif()
+
 if (NOT RUNPATH)
     set(RUNPATH "/var/run")
 endif()
diff --git a/cmake/set_build_flags.cmake b/cmake/set_build_flags.cmake
index fa6f38c0..89c9468c 100644
--- a/cmake/set_build_flags.cmake
+++ b/cmake/set_build_flags.cmake
@@ -9,6 +9,11 @@ endif()
 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -shared -pthread")
 set(CMAKE_EXE_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -pie -rdynamic")
 
+if (NOT DISABLE_WERROR)
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
+endif()
+
 if (ISULAD_GCOV)
     set(CMAKE_C_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage")
     set(CMAKE_CXX_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 65bcb978..f3dd3c19 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -102,7 +102,7 @@ add_executable(isula
     )
 target_include_directories(isula PUBLIC ${ISULA_INCS} ${SHARED_INCS})
 target_link_libraries(isula libisula ${LIBYAJL_LIBRARY})
-if (ANDROID)
+if (ANDROID OR MUSL)
     target_link_libraries(isula ${LIBSSL_LIBRARY})
 else()
     target_link_libraries(isula -lpthread)
@@ -115,7 +115,7 @@ add_executable(isulad-shim
     )
 target_include_directories(isulad-shim PUBLIC ${ISULAD_SHIM_INCS} ${SHARED_INCS})
 target_link_libraries(isulad-shim libisulad_tools)
-if (ANDROID)
+if (ANDROID OR MUSL)
     target_link_libraries(isulad-shim ${LIBSSL_LIBRARY} ${LIBYAJL_LIBRARY})
 else()
     target_link_libraries(isulad-shim -lpthread)
@@ -181,7 +181,7 @@ else()
 endif()
 
 target_link_libraries(isulad libisulad_tools libhttpclient -ldl)
-if (ANDROID)
+if (ANDROID OR MUSL)
     target_link_libraries(isulad ${LIBSSL_LIBRARY} ${LIBYAJL_LIBRARY})
 else()
     target_link_libraries(isulad -lpthread)
diff --git a/src/daemon/common/selinux_label.c b/src/daemon/common/selinux_label.c
index 145e4b6e..24294780 100644
--- a/src/daemon/common/selinux_label.c
+++ b/src/daemon/common/selinux_label.c
@@ -592,7 +592,7 @@ static int container_label(char **process_label, char **file_label)
         return 0;
     }
 
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
     lxc_path = ISULAD_DAEMON_CONTAINER_CONTEXTS;
 #else
     lxc_path = selinux_lxc_contexts_path();
diff --git a/src/daemon/common/sysinfo.c b/src/daemon/common/sysinfo.c
index d52f8767..e7b3807b 100644
--- a/src/daemon/common/sysinfo.c
+++ b/src/daemon/common/sysinfo.c
@@ -1308,7 +1308,7 @@ out:
     return ret;
 }
 
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
 static bool cgroup2_no_controller()
 {
     char *controllers_str = NULL;
@@ -1336,7 +1336,7 @@ static int make_sure_cgroup2_isulad_path_exist()
         return -1;
     }
 
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
     if (cgroup2_no_controller()) {
         DEBUG("no cgroup controller found");
         return 0;
diff --git a/src/daemon/executor/container_cb/execution_stream.c b/src/daemon/executor/container_cb/execution_stream.c
index 9af6fb5c..ebb9ee2b 100644
--- a/src/daemon/executor/container_cb/execution_stream.c
+++ b/src/daemon/executor/container_cb/execution_stream.c
@@ -63,7 +63,7 @@
 #include "utils_file.h"
 #include "utils_verify.h"
 
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
 #define SIG_CANCEL_SIGNAL     SIGUSR1
 #define PTHREAD_CANCEL_ENABLE    1
 #define PTHREAD_CANCEL_DISABLE    0
diff --git a/src/daemon/modules/image/CMakeLists.txt b/src/daemon/modules/image/CMakeLists.txt
index 329d2937..6f9e9936 100644
--- a/src/daemon/modules/image/CMakeLists.txt
+++ b/src/daemon/modules/image/CMakeLists.txt
@@ -117,7 +117,7 @@ target_link_libraries(${LIB_ISULAD_IMG}
     ${ZLIB_LIBRARY}
     libhttpclient)
 
-if (NOT ANDROID)
+if ((NOT ANDROID) AND (NOT MUSL))
     target_link_libraries(${LIB_ISULAD_IMG} -lpthread)
 endif()
 
diff --git a/src/daemon/modules/image/image.c b/src/daemon/modules/image/image.c
index fb0db361..8fb226aa 100644
--- a/src/daemon/modules/image/image.c
+++ b/src/daemon/modules/image/image.c
@@ -1773,7 +1773,8 @@ int im_container_export(const im_export_request *request)
 #endif
 
 #ifdef ENABLE_OCI_IMAGE
-char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
+char *im_get_rootfs_dir(const im_get_rf_dir_request *request)
+{
     char *dir = NULL;
     struct bim *bim = NULL;
 
@@ -1801,7 +1802,8 @@ out:
     return dir;
 }
 #else
-char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
+char *im_get_rootfs_dir(const im_get_rf_dir_request *request)
+{
     return NULL;
 }
 #endif
diff --git a/src/daemon/modules/image/image_rootfs_handler.c b/src/daemon/modules/image/image_rootfs_handler.c
index a76363d0..c3964b2c 100644
--- a/src/daemon/modules/image/image_rootfs_handler.c
+++ b/src/daemon/modules/image/image_rootfs_handler.c
@@ -87,7 +87,7 @@ static int proc_by_fpasswd(FILE *f_passwd, const char *user, defs_process_user *
     struct passwd *pwbufp = NULL;
 
     if (f_passwd != NULL) {
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__) 
         errval = util_getpwent_r(f_passwd, &pw, buf, sizeof(buf), &pwbufp);
 #else
         errval = fgetpwent_r(f_passwd, &pw, buf, sizeof(buf), &pwbufp);
@@ -105,7 +105,7 @@ static int proc_by_fpasswd(FILE *f_passwd, const char *user, defs_process_user *
                 *matched_username = util_strdup_s(pwbufp->pw_name);
                 break;
             }
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__) 
             errval = util_getpwent_r(f_passwd, &pw, buf, sizeof(buf), &pwbufp);
 #else
             errval = fgetpwent_r(f_passwd, &pw, buf, sizeof(buf), &pwbufp);
@@ -215,7 +215,7 @@ static int do_proc_by_froup(FILE *f_group, const char *group, defs_process_user
         return 0;
     }
 
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__) 
     errval = util_getgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
 #else
     errval = fgetgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
@@ -226,7 +226,7 @@ static int do_proc_by_froup(FILE *f_group, const char *group, defs_process_user
             if (search_group_list(gbufp, matched_username, puser) != 0) {
                 return -1;
             }
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__) 
             errval = util_getgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
 #else
             errval = fgetgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
@@ -240,7 +240,7 @@ static int do_proc_by_froup(FILE *f_group, const char *group, defs_process_user
             puser->gid = gbufp->gr_gid;
             *groupcnt = 1;
         }
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__) 
         errval = util_getgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
 #else
         errval = fgetgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
@@ -378,7 +378,7 @@ static int get_additional_groups(char **additional_groups, size_t additional_gro
     struct group *gbufp = NULL;
     struct group *groups = NULL;
 
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__) 
     while (f_group != NULL && util_getgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp) == 0) {
 #else
     while (f_group != NULL && fgetgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp) == 0) {
diff --git a/src/utils/cutils/CMakeLists.txt b/src/utils/cutils/CMakeLists.txt
index 30414d91..50a17f60 100644
--- a/src/utils/cutils/CMakeLists.txt
+++ b/src/utils/cutils/CMakeLists.txt
@@ -2,7 +2,7 @@
 aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} local_cutils_srcs)
 add_subdirectory(map)
 
-if (NOT ANDROID)
+if ((NOT ANDROID) AND (NOT MUSL))
     list(REMOVE_ITEM local_cutils_srcs "${CMAKE_CURRENT_SOURCE_DIR}/utils_pwgr.c")
 endif()
 
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index a154c52a..b1db045a 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -16,7 +16,7 @@
 #define _GNU_SOURCE
 #include "utils.h"
 #include <errno.h>
-#ifndef __ANDROID__
+#if !defined (__ANDROID__) && !defined(__MUSL__)
 #include <execinfo.h>
 #endif
 #include <stdint.h>
@@ -47,7 +47,7 @@
 #include "utils_string.h"
 #include "utils_verify.h"
 
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
 int mallopt(int param, int value)
 {
     return 1;
@@ -427,7 +427,12 @@ proc_t *util_stat2proc(const char *s, size_t len)
 
     /* parse these two strings separately, skipping the leading "(". */
     /* comm[16] in kernel */
+    /* https://www.openwall.com/lists/musl/2013/11/15/5: musl's sscanf("%15c",cmd) requires exactly 15 characters; anything shorter is a matching failure. */
+#ifdef __MUSL__
+    num = sscanf(s, "%d (%15s", &p->pid, p->cmd);
+#else
     num = sscanf(s, "%d (%15c", &p->pid, p->cmd);
+#endif
     if (num != 2) {
         ERROR("Call sscanf error: %s", errno ? strerror(errno) : "");
         free(p);
@@ -851,8 +856,8 @@ out:
 
 char **util_get_backtrace(void)
 {
-#ifdef __ANDROID__
-    /* android has no backtrace */
+#if defined (__ANDROID__) || defined(__MUSL__)
+    /* android and musl has no backtrace */
     return NULL;
 #else
 #define BACKTRACE_SIZE 16
diff --git a/src/utils/cutils/utils.h b/src/utils/cutils/utils.h
index 4518e3ac..72cab9f2 100644
--- a/src/utils/cutils/utils.h
+++ b/src/utils/cutils/utils.h
@@ -47,7 +47,7 @@
 extern "C" {
 #endif
 
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
 #define M_TRIM_THRESHOLD    -1
 #define M_TOP_PAD           -2
 #define M_MMAP_THRESHOLD    -3
diff --git a/src/utils/cutils/utils_verify.c b/src/utils/cutils/utils_verify.c
index 9ed33bf3..713e72c3 100644
--- a/src/utils/cutils/utils_verify.c
+++ b/src/utils/cutils/utils_verify.c
@@ -563,7 +563,7 @@ bool util_valid_value_false(const char *value)
 
 bool util_valid_rw_mode(const char *mode)
 {
-    if (mode == NULL){
+    if (mode == NULL) {
         return false;
     }
 
@@ -572,7 +572,7 @@ bool util_valid_rw_mode(const char *mode)
 
 bool util_valid_label_mode(const char *mode)
 {
-    if (mode == NULL){
+    if (mode == NULL) {
         return false;
     }
 
@@ -581,7 +581,7 @@ bool util_valid_label_mode(const char *mode)
 
 bool util_valid_copy_mode(const char *mode)
 {
-    if (mode == NULL){
+    if (mode == NULL) {
         return false;
     }
     return !strcmp(mode, "nocopy");
@@ -708,7 +708,7 @@ int util_valid_env(const char *env, char **dst)
     int ret = 0;
     char *value = NULL;
 
-    if (dst == NULL){
+    if (dst == NULL) {
         ERROR("NULL dst");
         return -1;
     }
-- 
2.25.1

