Move code to the shared library 47/212547/11
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Tue, 20 Aug 2019 11:39:42 +0000 (13:39 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Mon, 23 Sep 2019 08:01:15 +0000 (10:01 +0200)
Change-Id: I05f3bc5902bd7b13b4f50dac32622e7f3cbaedab

packaging/crash-worker.spec
src/crash-manager/CMakeLists.txt
src/crash-manager/crash-manager.c
src/crash-manager/crash-manager.h [new file with mode: 0644]
src/crash-manager/crash-manager.pc.in [new file with mode: 0644]
src/crash-manager/main.c [new file with mode: 0644]

index 2f38a78..a597a4b 100644 (file)
@@ -54,6 +54,11 @@ Requires: %{_bindir}/buxton2ctl
 %description
 crash-manager
 
+%package devel
+Summary:    Crash-manager development package
+%description devel
+This package provides library and header files.
+
 %if %{with doc}
 %package doc
 Summary:       Documentation package for crash-worker
@@ -136,7 +141,8 @@ export CFLAGS+=" -Werror"
           -DLOG_DUMP=%{on_off logdump} \
           -DLIVEDUMPER=%{on_off livedumper} \
           -DUPGRADE_SCRIPT_PATH=%{upgrade_script_path} \
-          -DLOGGER=dlog
+          -DLOGGER=dlog \
+          -DVERSION=%{version}
 
 make %{?jobs:-j%jobs}
 %if %{with doc}
@@ -207,6 +213,7 @@ sed -i "/${pattern}/D" %{_sysconfdir}/ld.so.preload
 %{_libexecdir}/crash-stack
 %{_libexecdir}/crash-popup-launch
 %{_libexecdir}/crash-notify-send
+%{_libdir}/libcrash-manager.so.*
 
 %if %{with logdump}
 %dir %{crash_all_log}
@@ -225,6 +232,11 @@ sed -i "/${pattern}/D" %{_sysconfdir}/ld.so.preload
 #upgrade script
 %attr(-,root,root) %{upgrade_script_path}/500.crash-manager-upgrade.sh
 
+%files devel
+%{_includedir}/crash-manager.h
+%{_libdir}/libcrash-manager.so
+%{_datadir}/pkgconfig/*.pc
+
 %if %{with doc}
 %files doc
 %{_datadir}/doc/crash-worker
@@ -251,3 +263,4 @@ sed -i "/${pattern}/D" %{_sysconfdir}/ld.so.preload
 %manifest %{name}.manifest
 %{_bindir}/livedumper
 %endif
+
index ce803d8..b935621 100644 (file)
@@ -2,7 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 PROJECT(crash-manager C)
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
-SET(CRASH_MANAGER_SRCS
+SET(LIB_CRASH_MANAGER_SRCS
        crash-manager.c
        so-info.c
        ${CMAKE_SOURCE_DIR}/src/shared/util.c
@@ -10,6 +10,10 @@ SET(CRASH_MANAGER_SRCS
        ${CMAKE_SOURCE_DIR}/src/shared/config.c
    )
 
+SET(CRASH_MANAGER_SRCS
+        main.c
+   )
+
 INCLUDE(FindPkgConfig)
 pkg_check_modules(crash-manager_pkgs REQUIRED
        dlog
@@ -30,8 +34,14 @@ ENDFOREACH(flag)
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIE")
 
+ADD_LIBRARY(libcrash-manager SHARED ${LIB_CRASH_MANAGER_SRCS})
+SET_TARGET_PROPERTIES(libcrash-manager PROPERTIES SOVERSION 1)
+SET_TARGET_PROPERTIES(libcrash-manager PROPERTIES PUBLIC_HEADER crash-manager.h)
+SET_TARGET_PROPERTIES(libcrash-manager PROPERTIES OUTPUT_NAME crash-manager)
+TARGET_LINK_LIBRARIES(libcrash-manager ${crash-manager_pkgs_LDFLAGS})
+
 ADD_EXECUTABLE(${PROJECT_NAME} ${CRASH_MANAGER_SRCS})
-TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${crash-manager_pkgs_LDFLAGS} -pie -lrt)
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${crash-manager_pkgs_LDFLAGS} -pie -lrt libcrash-manager)
 
 set(CRASH_POPUP crash-popup-launch)
 ADD_EXECUTABLE(${CRASH_POPUP} ${CRASH_POPUP}.c)
@@ -43,17 +53,24 @@ ADD_EXECUTABLE(${CRASH_NOTIFY} dbus_notify.c)
 TARGET_LINK_LIBRARIES(${CRASH_NOTIFY} ${helper_pkgs_LDFLAGS} -pie)
 install(TARGETS ${CRASH_NOTIFY} DESTINATION libexec)
 
+INSTALL(TARGETS libcrash-manager LIBRARY DESTINATION lib PUBLIC_HEADER DESTINATION include)
+
 INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin
                PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
                GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 
 CONFIGURE_FILE(500.${PROJECT_NAME}-upgrade.sh.in 500.${PROJECT_NAME}-upgrade.sh @ONLY)
 CONFIGURE_FILE(70-${PROJECT_NAME}.conf.in 70-${PROJECT_NAME}.conf @ONLY)
+CONFIGURE_FILE(crash-manager.pc.in crash-manager.pc @ONLY)
 
 INSTALL(FILES ${CMAKE_SOURCE_DIR}/src/${PROJECT_NAME}/crash-manager.conf
                DESTINATION /etc
                PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
 
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/src/${PROJECT_NAME}/crash-manager.pc
+               DESTINATION share/pkgconfig
+               PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
 INSTALL(FILES ${CMAKE_SOURCE_DIR}/src/${PROJECT_NAME}/70-${PROJECT_NAME}.conf
                DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/sysctl.d
                PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
index 7d30c92..be79b5b 100644 (file)
 #include "shared/spawn.h"
 #include "shared/util.h"
 #include "so-info.h"
+#include "crash-manager.h"
 
 /* Parsing */
 #define KEY_MAX              255
-#define APPID_MAX                   128
-#define PKGNAME_MAX                 128
 
 #define CRASH_TEMP_SUBDIR    "/temp/"
 #define CRASH_PATH_SUBDIR    "/dump/"
@@ -86,36 +85,6 @@ config_t config;
 static char* crash_dump_path;
 static char* crash_temp_path;
 
-/* Paths and variables */
-struct crash_info {
-       pid_t pid_info;
-       pid_t tid_info;
-       int uid_info;
-       int gid_info;
-       int sig_info;
-       char *cmd_line;
-       char *cmd_path;
-       time_t time_info;
-       char *temp_dir;
-       char *name;
-       char *result_path;
-       char *pfx;
-       char *info_path;
-       char *core_path;
-       char *log_path;
-       char appid[APPID_MAX];
-       char pkgid[PKGNAME_MAX];
-       char *output_path;
-       bool livedump;
-       bool kill;
-       bool print_result_path;
-#ifdef SYS_ASSERT
-       char *sysassert_cs_path;
-       bool have_sysassert_report;
-#endif
-       int prstatus_fd;
-};
-
 /* pkgmgrinfo filter list function for getting application ID */
 static int appinfo_get_appid_func(pkgmgrinfo_appinfo_h handle,
                void *user_data)
@@ -339,125 +308,7 @@ close_fd:
        return -1;
 }
 
-static void print_help(const char *name)
-{
-       printf("Syntax: %s [OPTIONS]\n"
-              "\n"
-              "    -p  --pid=PID           PID of dumped process\n"
-              "    -u  --uid=UID           real UID of dumped process\n"
-              "    -g  --gid=GID           real GID of dumped process\n"
-              "    -i  --tid=TID           TID of thread that triggered core dump\n"
-              "    -s  --signal=SIG        number of signal causing dump\n"
-              "    -t  --time=TIME         time of dump, expressed as seconds since the Epoch\n"
-              "    -l  --live              get coredump of running process\n"
-              "    -k  --kill-after-dump   kill after dump (only with --live option)\n"
-              "    -r  --print             print report path to stdout\n"
-              "    -o  --output            output directory\n"
-              "    -h  --help              this message\n"
-              "\n"
-              "for --live option only --pid is required\n"
-              "\n", name);
-}
-
-static bool parse_args(struct crash_info *cinfo, int argc, char *argv[])
-{
-#define QUOTE(member) #member
-#define GET_NUMBER(member) \
-       errno = 0; \
-       cinfo->member##_info = strtol(optarg, NULL, 10); \
-       if (errno != 0) { \
-               _D("%s argument error\n", QUOTE(member)); \
-               printf("%s argument error\n", QUOTE(member)); \
-               return false; \
-       }
-
-       bool result = true;
-       int opt;
-       bool pid_set = false;
-       bool uid_set = false;
-       bool gid_set = false;
-       bool sig_set = false;
-
-       struct option long_options[] = {
-               {"pid",             required_argument, NULL, 'p'},
-               {"uid",             required_argument, NULL, 'u'},
-               {"gid",             required_argument, NULL, 'g'},
-               {"tid",             required_argument, NULL, 'i'},
-               {"signal",          required_argument, NULL, 's'},
-               {"time",            required_argument, NULL, 't'},
-               {"live",                  no_argument, NULL, 'l'},
-               {"kill-after-dump",       no_argument, NULL, 'k'},
-               {"print",                 no_argument, NULL, 'r'},
-               {"output",          required_argument, NULL, 'o'},
-               {"help",                  no_argument, NULL, 'h'},
-       };
-
-       while ((opt = getopt_long(argc, argv, "p:u:g:i:s:t:hlkro:", long_options, NULL)) != -1) {
-               switch (opt) {
-               case 'p':
-                       GET_NUMBER(pid)
-                       pid_set = true;
-                       break;
-               case 'u':
-                       GET_NUMBER(uid)
-                       uid_set = true;
-                       break;
-               case 'g':
-                       GET_NUMBER(gid)
-                       gid_set = true;
-                       break;
-               case 'i':
-                       GET_NUMBER(tid)
-                       break;
-               case 's':
-                       GET_NUMBER(sig)
-                       sig_set = true;
-                       break;
-               case 't':
-                       GET_NUMBER(time)
-                       break;
-               case 'l':
-                       cinfo->livedump = true;
-                       break;
-               case 'k':
-                       cinfo->kill = true;
-                       break;
-               case 'r':
-                       cinfo->print_result_path = true;
-                       break;
-               case 'o':
-                       cinfo->output_path = optarg;
-                       _D("output path: %s\n", optarg);
-                       break;
-               case 'h':
-               default:
-                       print_help(argv[0]);
-                       return false;
-               }
-       }
-
-       if (!pid_set || (!cinfo->livedump && (!gid_set || !uid_set || !sig_set))) {
-               printf("Not enough parameters.\n\n");
-               print_help(argv[0]);
-               return false;
-       }
-
-       if (cinfo->livedump && sig_set) {
-               printf("--sig can not be used with --live option\n\n");
-               print_help(argv[0]);
-               return false;
-       }
-
-       if (!cinfo->livedump && cinfo->kill) {
-               printf("Option --kill-after-dump can be used only with --live\n");
-               return false;
-       }
-       return result;
-#undef QUOTE
-#undef GET_NUMBER
-}
-
-static bool set_crash_info(struct crash_info *cinfo)
+bool set_crash_info(struct crash_info *cinfo)
 {
        int ret;
        char *temp_dir_ret = NULL;
@@ -1244,7 +1095,7 @@ static void free_crash_info(struct crash_info *cinfo)
 #endif
 }
 
-static void crash_info_init(struct crash_info *cinfo)
+void crash_info_init(struct crash_info *cinfo)
 {
        cinfo->prstatus_fd = -1;
        cinfo->livedump = false;
@@ -1253,6 +1104,13 @@ static void crash_info_init(struct crash_info *cinfo)
        cinfo->tid_info = -1;
        cinfo->time_info = 0;
        cinfo->output_path = NULL;
+       cinfo->cmd_line = NULL;
+       cinfo->cmd_path = NULL;
+       cinfo->temp_dir = NULL;
+       cinfo->pfx =  NULL;
+       cinfo->result_path = NULL;
+       cinfo->info_path = NULL;
+       cinfo->core_path = NULL;
 }
 
 static bool run(struct crash_info *cinfo)
@@ -1337,7 +1195,8 @@ static bool run(struct crash_info *cinfo)
        return true;
 }
 
-bool crash_manager_prepare(struct crash_info *cinfo) {
+static bool crash_manager_prepare(struct crash_info *cinfo)
+{
        if (!config_init(&config, CRASH_MANAGER_CONFIG_PATH))
                return false;
 
@@ -1357,7 +1216,7 @@ bool crash_manager_prepare(struct crash_info *cinfo) {
        return true;
 }
 
-static void crash_manager_free(struct crash_info *cinfo)
+void crash_manager_free(struct crash_info *cinfo)
 {
        if (cinfo->prstatus_fd >= 0)
                close(cinfo->prstatus_fd);
@@ -1368,32 +1227,11 @@ static void crash_manager_free(struct crash_info *cinfo)
        free_crash_info(cinfo);
 }
 
-int main(int argc, char *argv[])
+bool crash_manager_direct(struct crash_info *cinfo)
 {
-       int res = EXIT_SUCCESS;
-       struct crash_info cinfo;
-
-       /*
-        * prctl(PR_SET_DUMPABLE, 0) is not neccessary. Kernel runs the
-        * crash-manager and sets RLIMIT_CORE to 1 for the process. This is special
-        * value that prevents from running crash-manager recursively.
-        */
-
-       crash_info_init(&cinfo);
-
-       /* Parse args */
-       if (!parse_args(&cinfo, argc, argv))
-               return EXIT_FAILURE;
-
-       if (!crash_manager_prepare(&cinfo)) {
-               res = EXIT_FAILURE;
-               goto exit;
-       }
+       if (!crash_manager_prepare(cinfo))
+               return false;
 
-       if (!run(&cinfo))
-               res = EXIT_FAILURE;
-exit:
-       crash_manager_free(&cinfo);
-       _I("Exiting with exit code %d", res);
-       return res;
+       return run(cinfo);
 }
+
diff --git a/src/crash-manager/crash-manager.h b/src/crash-manager/crash-manager.h
new file mode 100644 (file)
index 0000000..244b37a
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * crash-manager
+ *
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CRASH_MANAGER_H__
+#define __CRASH_MANAGER_H__
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#define APPID_MAX                   128
+#define PKGNAME_MAX                 128
+
+/* Paths and variables */
+struct crash_info {
+       pid_t pid_info;
+       pid_t tid_info;
+       int uid_info;
+       int gid_info;
+       int sig_info;
+       char *cmd_line;
+       char *cmd_path;
+       time_t time_info;
+       char *temp_dir;
+       char *name;
+       char *result_path;
+       char *pfx;
+       char *info_path;
+       char *core_path;
+       char *log_path;
+       char appid[APPID_MAX];
+       char pkgid[PKGNAME_MAX];
+       char *output_path;
+       bool livedump;
+       bool kill;
+       bool print_result_path;
+#ifdef SYS_ASSERT
+       char *sysassert_cs_path;
+       bool have_sysassert_report;
+#endif
+       int prstatus_fd;
+};
+
+bool crash_manager_direct(struct crash_info *cinfo);
+void crash_info_init(struct crash_info *cinfo);
+void crash_manager_free(struct crash_info *cinfo);
+#endif
diff --git a/src/crash-manager/crash-manager.pc.in b/src/crash-manager/crash-manager.pc.in
new file mode 100644 (file)
index 0000000..3f9b266
--- /dev/null
@@ -0,0 +1,10 @@
+prefix=/usr
+exec_prefix=${prefix}
+includedir=${prefix}/include
+libdir=${exec_prefix}/lib
+
+Name: crash-manager
+Description: The crash-manager library
+Version: @VERSION@
+Cflags: -I${includedir}
+Libs: -L${libdir} -lcrash-manager
diff --git a/src/crash-manager/main.c b/src/crash-manager/main.c
new file mode 100644 (file)
index 0000000..53211e8
--- /dev/null
@@ -0,0 +1,149 @@
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "shared/log.h"
+#include "crash-manager.h"
+
+static void print_help(const char *name)
+{
+       printf("Syntax: %s [OPTIONS]\n"
+              "\n"
+              "    -p  --pid=PID           PID of dumped process\n"
+              "    -u  --uid=UID           real UID of dumped process\n"
+              "    -g  --gid=GID           real GID of dumped process\n"
+              "    -i  --tid=TID           TID of thread that triggered core dump\n"
+              "    -s  --signal=SIG        number of signal causing dump\n"
+              "    -t  --time=TIME         time of dump, expressed as seconds since the Epoch\n"
+              "    -l  --live              get coredump of running process\n"
+              "    -k  --kill-after-dump   kill after dump (only with --live option)\n"
+              "    -r  --print             print report path to stdout\n"
+              "    -o  --output            output directory\n"
+              "    -h  --help              this message\n"
+              "\n"
+              "for --live option only --pid is required\n"
+              "\n", name);
+}
+
+static bool parse_args(struct crash_info *cinfo, int argc, char *argv[])
+{
+#define QUOTE(member) #member
+#define GET_NUMBER(member) \
+       do {\
+               errno = 0; \
+               cinfo->member##_info = strtol(optarg, NULL, 10); \
+               if (errno != 0) { \
+                       _D("%s argument error\n", QUOTE(member)); \
+                       printf("%s argument error\n", QUOTE(member)); \
+                       return false; \
+               } \
+       } while(0)
+
+       bool result = true;
+       int opt;
+       bool pid_set = false;
+       bool uid_set = false;
+       bool gid_set = false;
+       bool sig_set = false;
+
+       struct option long_options[] = {
+               {"pid",             required_argument, NULL, 'p'},
+               {"uid",             required_argument, NULL, 'u'},
+               {"gid",             required_argument, NULL, 'g'},
+               {"tid",             required_argument, NULL, 'i'},
+               {"signal",          required_argument, NULL, 's'},
+               {"time",            required_argument, NULL, 't'},
+               {"live",                  no_argument, NULL, 'l'},
+               {"kill-after-dump",       no_argument, NULL, 'k'},
+               {"print",                 no_argument, NULL, 'r'},
+               {"output",          required_argument, NULL, 'o'},
+               {"help",                  no_argument, NULL, 'h'},
+       };
+
+       while ((opt = getopt_long(argc, argv, "p:u:g:i:s:t:hlkro:", long_options, NULL)) != -1) {
+               switch (opt) {
+               case 'p':
+                       GET_NUMBER(pid);
+                       pid_set = true;
+                       break;
+               case 'u':
+                       GET_NUMBER(uid);
+                       uid_set = true;
+                       break;
+               case 'g':
+                       GET_NUMBER(gid);
+                       gid_set = true;
+                       break;
+               case 'i':
+                       GET_NUMBER(tid);
+                       break;
+               case 's':
+                       GET_NUMBER(sig);
+                       sig_set = true;
+                       break;
+               case 't':
+                       GET_NUMBER(time);
+                       break;
+               case 'l':
+                       cinfo->livedump = true;
+                       break;
+               case 'k':
+                       cinfo->kill = true;
+                       break;
+               case 'r':
+                       cinfo->print_result_path = true;
+                       break;
+               case 'o':
+                       cinfo->output_path = optarg;
+                       _D("output path: %s\n", optarg);
+                       break;
+               case 'h':
+               default:
+                       print_help(argv[0]);
+                       return false;
+               }
+       }
+
+       if (!pid_set || (!cinfo->livedump && (!gid_set || !uid_set || !sig_set))) {
+               printf("Not enough parameters.\n\n");
+               print_help(argv[0]);
+               return false;
+       }
+
+       if (cinfo->livedump && sig_set) {
+               printf("--sig can not be used with --live option\n\n");
+               print_help(argv[0]);
+               return false;
+       }
+
+       if (!cinfo->livedump && cinfo->kill) {
+               printf("Option --kill-after-dump can be used only with --live\n");
+               return false;
+       }
+       return result;
+#undef QUOTE
+#undef GET_NUMBER
+}
+
+
+int main(int argc, char *argv[])
+{
+       struct crash_info cinfo;
+
+       /*
+        * prctl(PR_SET_DUMPABLE, 0) is not neccessary. Kernel runs the
+        * crash-manager and sets RLIMIT_CORE to 1 for the process. This is special
+        * value that prevents from running crash-manager recursively.
+        */
+
+       crash_info_init(&cinfo);
+
+       /* Parse args */
+       if (!parse_args(&cinfo, argc, argv))
+               return EXIT_FAILURE;
+
+       int res = crash_manager_direct(&cinfo) ? EXIT_SUCCESS : EXIT_FAILURE;
+
+       crash_manager_free(&cinfo);
+       _I("Exiting with exit code %d", res);
+       return res;
+}