Move bugreport library from crash-worker 81/255881/2 accepted/tizen/unified/20210330.111252 submit/tizen/20210324.085352 submit/tizen/20210325.112955 submit/tizen/20210329.082152 submit/tizen/20210329.184123
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Wed, 24 Mar 2021 13:12:38 +0000 (14:12 +0100)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Wed, 24 Mar 2021 13:32:17 +0000 (14:32 +0100)
Change-Id: I086b83dc5d1eea4d46d874f22bd2f72bde2c5ff9
Signed-off-by: Konrad Kuchciak <k.kuchciak@samsung.com>
CMakeLists.txt
packaging/diagnostics.spec
src/bugreport/CMakeLists.txt [new file with mode: 0644]
src/bugreport/ProcessM4.cmake [new file with mode: 0644]
src/bugreport/bugreport.pc.in [new file with mode: 0644]
src/bugreport/libbugreport-service.c [new file with mode: 0644]
src/bugreport/libbugreport.h [new file with mode: 0644]
src/library/CMakeLists.txt
src/library/diagnostics.c
src/library/log.h
src/test/CMakeLists.txt

index 875ff222bf406f8c3c752764d497e130ec6ef362..b81aad1ee45a3609e40d41e610ae98be080c42ba 100644 (file)
@@ -1,7 +1,7 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
 PROJECT(diagnostics LANGUAGES C)
 SET(target ${PROJECT_NAME})
-SET(dependency "glib-2.0 gio-unix-2.0 dlog dumpsys dumpsys-system bugreport capi-system-info bundle aul")
+SET(dependency "glib-2.0 gio-unix-2.0 dlog dumpsys dumpsys-system capi-system-info bundle aul")
 # ADD_DEFINITIONS(-Wall -Werror -Wextra)
 
 # Options
@@ -16,6 +16,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
 
+ADD_SUBDIRECTORY(src/bugreport)
 ADD_SUBDIRECTORY(src/library)
 
 # cmocka tasts and coverage
index 32ccd81baa5f51b061e7c3ec470ed06f4c3cb269..19b8ac35f1ffbcf6114dc7caadfebffe57558294 100644 (file)
@@ -1,5 +1,5 @@
 Name:       diagnostics
-Version:    1.1.0
+Version:    1.2.0
 Release:    1
 License:    Apache-2.0
 Summary:    Tizen Diagnostics API
@@ -13,7 +13,6 @@ BuildRequires:  pkgconfig(gio-unix-2.0)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(dumpsys)
 BuildRequires:  pkgconfig(dumpsys-system)
-BuildRequires:  pkgconfig(bugreport)
 BuildRequires:  pkgconfig(capi-system-info)
 BuildRequires:  pkgconfig(bundle)
 BuildRequires:  pkgconfig(aul)
@@ -40,6 +39,14 @@ Requires: %{name} = %{version}-%{release}
 %description devel
 Tizen Diagnostics API (Development)
 
+%package -n     libbugreport
+Summary:        libbugreport provides API to communicate with bugreport-service
+%description -n libbugreport
+
+%package -n     libbugreport-devel
+Summary:        package provides headers needed to develop programs using bugreport-service
+%description -n libbugreport-devel
+
 %prep
 %setup -q
 
@@ -56,10 +63,22 @@ make %{?jobs:-j%jobs}
 
 %files
 %manifest packaging/%{name}.manifest
-%{_libdir}/*.so.*
+%{_libdir}/libdiagnostics.so.*
 %license LICENSE.APLv2
 
 %files devel
-%{_includedir}/*.h
-%{_libdir}/*.so
-%{_libdir}/pkgconfig/*.pc
+%{_includedir}/diagnostics.h
+%{_libdir}/libdiagnostics.so
+%{_libdir}/pkgconfig/diagnostics.pc
+
+%files -n libbugreport
+%license LICENSE.APLv2
+%manifest packaging/%{name}.manifest
+%{_libdir}/libbugreport.so.*
+
+%files -n libbugreport-devel
+%license LICENSE.APLv2
+%manifest packaging/%{name}.manifest
+%{_includedir}/libbugreport.h
+%{_libdir}/libbugreport.so
+%{_libdir}/pkgconfig/bugreport.pc
\ No newline at end of file
diff --git a/src/bugreport/CMakeLists.txt b/src/bugreport/CMakeLists.txt
new file mode 100644 (file)
index 0000000..7e5df2e
--- /dev/null
@@ -0,0 +1,41 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(bugreport C)
+
+# ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/../../include include/)
+
+# INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/../../src/ ${CMAKE_SOURCE_DIR}/../../include)
+
+INCLUDE(GNUInstallDirs)
+
+INCLUDE(FindPkgConfig)
+
+pkg_check_modules(bugreport_pkgs REQUIRED
+       dlog
+       gio-2.0
+       gio-unix-2.0
+       )
+
+FOREACH(flag ${bugreport_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIE -Wno-unused-function -Wno-unused-const-variable")
+
+INCLUDE(${CMAKE_SOURCE_DIR}/src/bugreport/ProcessM4.cmake)
+
+# LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/crash-manager)
+
+ADD_LIBRARY(libbugreport SHARED libbugreport-service.c)
+SET_TARGET_PROPERTIES(libbugreport PROPERTIES
+       SOVERSION 1
+       PUBLIC_HEADER libbugreport.h
+       OUTPUT_NAME bugreport)
+
+CONFIGURE_FILE(bugreport.pc.in ${CMAKE_SOURCE_DIR}/bugreport.pc @ONLY)
+
+INSTALL(TARGETS libbugreport LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+       PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/bugreport.pc
+       DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
+
diff --git a/src/bugreport/ProcessM4.cmake b/src/bugreport/ProcessM4.cmake
new file mode 100644 (file)
index 0000000..09ba1c9
--- /dev/null
@@ -0,0 +1,11 @@
+MACRO(PROCESS_M4 _defines _input _output)
+  GET_FILENAME_COMPONENT(_name ${_output} NAME)
+  ADD_CUSTOM_COMMAND(
+    OUTPUT ${_output}
+    COMMAND m4
+    ARGS -P ${_defines} ${_input} > ${_output}
+    DEPENDS ${_input}
+    VERBATIM)
+  ADD_CUSTOM_TARGET(M4_${_name} DEPENDS ${_output})
+  ADD_DEPENDENCIES(${PROJECT_NAME} M4_${_name})
+ENDMACRO(PROCESS_M4)
diff --git a/src/bugreport/bugreport.pc.in b/src/bugreport/bugreport.pc.in
new file mode 100644 (file)
index 0000000..1ccd264
--- /dev/null
@@ -0,0 +1,10 @@
+prefix=/usr
+exec_prefix=${prefix}
+includedir=${prefix}/include
+libdir=${exec_prefix}/lib
+
+Name: bugreport
+Description: The bugreport library
+Version: @VERSION@
+Cflags: -I${includedir}
+Libs: -L${libdir} -lbugreport
diff --git a/src/bugreport/libbugreport-service.c b/src/bugreport/libbugreport-service.c
new file mode 100644 (file)
index 0000000..8ec45ba
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2016-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.
+ */
+
+#include <gio/gio.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define LOG_TAG "LIBBUGREPORT"
+
+#include "libbugreport.h"
+#include "../library/log.h"
+
+#define BUS_NAME "org.tizen.system.crash.livedump"
+#define OBJECT_PATH "/Org/Tizen/System/Crash/Livedump"
+#define INTERFACE_NAME "org.tizen.system.crash.livedump"
+#define METHOD_NAME "livedump_pid"
+
+static GDBusConnection* dbus_init(void)
+{
+       GError *error = NULL;
+       GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+
+       if (!conn || error) {
+               _E("Failed to get dbus: %s", error ? error->message : "");
+               if (error)
+                       g_error_free(error);
+               return NULL;
+       }
+
+       return conn;
+}
+
+int livedump_pid(pid_t pid, const char *dump_reason, char *report_path, size_t report_path_len)
+{
+       int res = TIZEN_ERROR_NONE;
+       GDBusConnection *conn = dbus_init();
+
+       if (!conn)
+               return TIZEN_ERROR_IO_ERROR;
+
+       GVariant *parameters = g_variant_new("(is)", pid, dump_reason);
+
+       GError *error = NULL;
+       GVariant *reply = g_dbus_connection_call_sync(conn,
+                                      BUS_NAME,
+                                      OBJECT_PATH,
+                                      INTERFACE_NAME,
+                                      METHOD_NAME,
+                                      parameters,
+                                      G_VARIANT_TYPE("(s)"),
+                                      G_DBUS_CALL_FLAGS_NONE,
+                                      -1,
+                                      NULL,
+                                      &error);
+       if (!reply || error) {
+               _E("Error while calling livedump_pid via dbus (pid=%d, reason=%s): %s", pid, dump_reason, error ? error->message : "");
+               res = TIZEN_ERROR_IO_ERROR;
+
+               if (error) {
+                       if (error->code == G_DBUS_ERROR_ACCESS_DENIED)
+                               res = TIZEN_ERROR_PERMISSION_DENIED;
+                       if (error->code == CS_ERR_NO_PROC)
+                               res = TIZEN_ERROR_NO_SUCH_PROCESS;
+                       g_error_free(error);
+               }
+               goto exit;
+       }
+
+       if (!g_variant_is_of_type(reply, G_VARIANT_TYPE("(s)"))) {
+               _E("reply is not of the correct type (s)");
+               res = TIZEN_ERROR_IO_ERROR;
+               goto exit;
+       }
+
+       gchar *reply_str;
+       g_variant_get(reply, "(&s)", &reply_str);
+       if (strlen(reply_str) <= (report_path_len + 1)) {
+               strncpy(report_path, reply_str, report_path_len);
+       } else {
+               _E("Report path (%s) is longer than report_path_len", reply_str);
+               res = TIZEN_ERROR_IO_ERROR;
+       }
+exit:
+       g_object_unref(conn);
+       return res;
+}
diff --git a/src/bugreport/libbugreport.h b/src/bugreport/libbugreport.h
new file mode 100644 (file)
index 0000000..570f298
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+#pragma once
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+#define CS_ERROR               1
+#define CS_ERR_NO_ERROR        0
+#define CS_ERR_PARAM           1
+#define CS_ERR_TIMEOUT         2
+#define CS_ERR_READ            3
+#define CS_ERR_INTERNAL        4
+#define CS_ERR_UNSUPPORTED     5
+#define CS_ERR_NO_PROC         6
+
+/**
+ * @brief Sends a request to the bugreport-service to create livedump report of specific process.
+ * @param pid PID of process for which the report should be created.
+ * @param dump_reason the reason that should be included in the raport.
+ * @param report_path pointer to the buffer in which will be saved the report path.
+ * @param report_path_len lenght of buffer for the report path.
+ * @return 0 on success, otherwise a negative error value
+ * @retval #TIZEN_ERROR_NONE Success
+ * @retval #TIZEN_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #TIZEN_ERROR_NO_SUCH_PROCESS No such process
+ * @retval #TIZEN_ERROR_IO_ERROR Internal error occurred
+ */
+extern int livedump_pid(pid_t pid, const char *dump_reason, char *report_path, size_t report_path_len);
index 5091e5093e5816e202203dbaded498f95a09da55..c9b3eec79977aa96d6179039a01e4935eed621bb 100644 (file)
@@ -8,10 +8,10 @@ SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS} -fPIC
 SET_TARGET_PROPERTIES(${target} PROPERTIES LINK_FLAGS "-pie")
 SET_TARGET_PROPERTIES(${target} PROPERTIES SOVERSION ${MAJORVER})
 SET_TARGET_PROPERTIES(${target} PROPERTIES VERSION ${FULLVER})
-TARGET_LINK_LIBRARIES(${target} ${pkgs_LDFLAGS})
+TARGET_LINK_LIBRARIES(${target} ${pkgs_LDFLAGS} libbugreport)
 
-ADD_LIBRARY(${target}_static STATIC ${SOURCES})
-TARGET_INCLUDE_DIRECTORIES(${target}_static PRIVATE ${CMAKE_SOURCE_DIR}/src/library)
+ADD_LIBRARY(${target}_static STATIC ${SOURCES} ${CMAKE_SOURCE_DIR}/src/bugreport/libbugreport-service.c)
+TARGET_INCLUDE_DIRECTORIES(${target}_static PRIVATE ${CMAKE_SOURCE_DIR}/src/bugreport ${CMAKE_SOURCE_DIR}/src/library)
 TARGET_COMPILE_OPTIONS(${target}_static BEFORE PRIVATE -include ../test/test_diagnostics_add_function_defs.h)
 TARGET_COMPILE_DEFINITIONS(${target}_static PUBLIC -DUNIT_TEST)
 if(ENABLE_COVERAGE)
index 2ccb0f2bfa39df9fb36cfa9691e92e0c3e659ea9..739a40873b9db27b8fb2cc6ab34636626a83def2 100644 (file)
 #include <glib.h>
 #include <libdumpsys.h>
 #include <dumpsys-system.h>
-#include <libbugreport.h>
 #include <poll.h>
 #include <bundle.h>
 #include <aul.h>
 #include <limits.h>
 
+#define LOG_TAG "DIAGNOSTICS"
+
 #include "log.h"
 #include "signal.h"
 #include "dbus.h"
+#include "../bugreport/libbugreport.h"
 
 #ifndef STATIC
 #define STATIC static
index b633e309a856f71d6dd6f010507c58421b75200a..465c5bb447996b6e60ab93de0eb5dcd6fbe67dc3 100644 (file)
@@ -19,9 +19,6 @@
 
 #include <dlog.h>
 
-#undef LOG_TAG
-#define LOG_TAG "DIAGNOSTICS"
-
 #define _E(fmt, arg...) LOGE(fmt, ##arg)
 #define _D(fmt, arg...) LOGD(fmt, ##arg)
 #define _W(fmt, arg...) LOGW(fmt, ##arg)
index b8b74b0ed3530d4a23ca521db16e49d49c44fe20..85f216ce5f5a46e082eb6dc5668ca8aea911225b 100644 (file)
@@ -1,7 +1,7 @@
 # ADD_DEFINITIONS(-g -Werror -Wall -Wextra)
 ADD_CUSTOM_TARGET(ctest ALL make CTEST_OUTPUT_ON_FAILURE=1 test WORKING_DIRECTORY ./ USES_TERMINAL)
 
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/library)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/bugreport ${CMAKE_SOURCE_DIR}/src/library)
 
 function(add_mocked_test name wraps)
        add_executable(test_${name} test_${name}.c)