copy-blockdev: Make binary fully static 14/318914/3
authorAntoni <a.adaszkiewi@samsung.com>
Fri, 24 Jan 2025 12:16:55 +0000 (13:16 +0100)
committerMateusz Moscicki <m.moscicki2@samsung.com>
Wed, 19 Feb 2025 13:43:09 +0000 (14:43 +0100)
This change ensures that `copy-blockdev` binary provided in a delta
will run on a system with an older GLib version.

hal-api-device dependecy is removed from copy-blockedv.
`device_board_set_upgrade_progress_status` is called directly instead.
This is done to save us the trouble of linking to hal-* libs
statically.

Change-Id: I03b7c4d645f1ce68d841f4699c90d8492f5324df

src/copy-blockdev/CMakeLists.txt
src/copy-blockdev/lib.c
src/copy-blockdev/lib.h

index 6a9d08f8fe1a30026cfbae6ef38bef978a916826..fb9528e9e45cd49be4d7abdd97997ed85a003f78 100644 (file)
@@ -1,13 +1,3 @@
-IF(NOT DEFINE_HOST_BUILD)
-SET(CMAKE_C_FLAGS ENV${CFLAGS})
-INCLUDE(FindPkgConfig)
-pkg_check_modules(REQUIRED_PKGS REQUIRED hal-api-device)
-FOREACH(flag ${REQUIRED_PKGS_CFLAGS})
-       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
-ENDFOREACH(flag)
-SET(CMAKE_C_FLAGS ${EXTRA_CFLAGS})
-ENDIF()
-
 set(COPY_BLOCKDEVSRCS ../upgrade-apply/sha1/sha1.c main.c lib.c)
 include_directories(../upgrade-apply/sha1)
 set(COPY_BLOCKDEVEXENAME "copy-blockdev")
@@ -17,7 +7,11 @@ target_compile_options(${COPY_BLOCKDEVEXENAME} PRIVATE -Wall -Wextra -pedantic -
 target_compile_definitions(${COPY_BLOCKDEVEXENAME} PRIVATE -D_FILE_OFFSET_BITS=64)
 
 IF(NOT DEFINE_HOST_BUILD)
-target_link_libraries(${COPY_BLOCKDEVEXENAME} ${REQUIRED_PKGS_LDFLAGS})
+target_link_options(${COPY_BLOCKDEVEXENAME} PRIVATE "-static")
+set_target_properties(${COPY_BLOCKDEVEXENAME} PROPERTIES
+               LINK_SEARCH_START_STATIC ON
+               LINK_SEARCH_END_STATIC ON
+       )
 ELSE()
 target_compile_definitions(${COPY_BLOCKDEVEXENAME} PRIVATE -DHOST_BUILD)
 ENDIF()
index 04b71154e0be23dba9f28efb6aa7fb5d4af01d0c..bbd6a97451f00badee85c77c80c4b57b243fa2a3 100644 (file)
 #include <sys/statvfs.h>
 #include <sys/types.h>
 #include <sys/vfs.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #include <upgrade-apply/sha1/sha1.h>
 
-#ifndef HOST_BUILD
-#include <hal/hal-device-board.h>
-#endif
-
 void usage(char *name) {
     fprintf(stderr, "Usage:\n"
                     "\n"
@@ -244,6 +241,37 @@ void fd_cleanup(int *fd)
        close(*fd);
 }
 
+#ifndef HOST_BUILD
+static int set_upgrade_progress_status(int progress)
+{
+    int child_ret = 0;
+    pid_t pid = fork();
+
+    if (pid == -1) {
+        fprintf(stderr, "Failed to fork\n");
+        return -1;
+    }
+
+    if (pid == 0) {
+        // Max value for progress is 100
+        char buf[4];
+        snprintf(buf, sizeof(buf), "%d", progress);
+        if (execl(DEVICE_BOARD_SET_UPGRADE_PROGRESS_STATUS_BIN,
+            DEVICE_BOARD_SET_UPGRADE_PROGRESS_STATUS_BIN, buf, (char *)NULL) < 0) {
+                fprintf(stderr, "Failed to exec %s\n", DEVICE_BOARD_SET_UPGRADE_PROGRESS_STATUS_BIN);
+                exit(EXIT_FAILURE);
+            }
+    }
+
+    if (waitpid(pid, &child_ret, 0) < 0) {
+        fprintf(stderr, "Failed to wait child\n");
+        return -1;
+    }
+
+    return WEXITSTATUS(child_ret) == 0 ? 0 : -1;
+}
+#endif
+
 int copy_and_update_progress(struct params_t *params) {
     int prev_progress = params->progress_from;
     int progress = params->progress_from;
@@ -301,7 +329,7 @@ int copy_and_update_progress(struct params_t *params) {
 
         if (progress - prev_progress > 0) {
             #ifndef HOST_BUILD
-                if (params->update && hal_device_board_set_upgrade_progress_status(progress) < 0) {
+                if (params->update && set_upgrade_progress_status(progress) < 0) {
                     fprintf(stderr, "Cannot set upgrade progress status.\n");
                     return ERR_STATUS;
                 };
@@ -313,7 +341,7 @@ int copy_and_update_progress(struct params_t *params) {
     }
 
     #ifndef HOST_BUILD
-        if (params->update && hal_device_board_set_upgrade_progress_status(params->progress_to) < 0) {
+        if (params->update && set_upgrade_progress_status(params->progress_to) < 0) {
             fprintf(stderr, "Cannot set upgrade progress status.\n");
             return ERR_STATUS;
         }
index fefb190c47cc003b5520b2dfd9786a58d2dd0c8f..36997400a083f940318d8c4424b6f9a3d32d7d77 100644 (file)
@@ -26,6 +26,8 @@
 #define ERR_SHA1_COMP       14
 #define ERR_STATUS          15
 
+#define DEVICE_BOARD_SET_UPGRADE_PROGRESS_STATUS_BIN "/usr/bin/device_board_set_upgrade_progress_status"
+
 /*
  * Rationale for using long long values and strtoll instead of
  * unsigned long long and strtoull to parse ranges which can't