Skip to content

Commit 80d2ffc

Browse files
DROID-3097 App | Tech | Catch sigsys signals (#1835)
Co-authored-by: Evgenii Kozlov <[email protected]> Co-authored-by: Evgenii Kozlov <[email protected]>
1 parent c194c05 commit 80d2ffc

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ ktlint
2222
.idea/*.xml
2323
signing.properties
2424
app/release
25-
.kotlin/
25+
.kotlin/
26+
app/.cxx

app/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ android {
5353
buildConfigField "boolean", "LOG_EDITOR_CONTROL_PANEL", localProperties.getProperty("LOG_EDITOR_CONTROL_PANEL", "false")
5454
buildConfigField "boolean", "ENABLE_STRICT_MODE", "false"
5555
resValue "string", "SENTRY_DSN", config.sentryApiKey
56+
externalNativeBuild {
57+
cmake {
58+
// Specify any cpp flags if needed
59+
cppFlags "-std=c++11"
60+
}
61+
}
62+
}
63+
64+
externalNativeBuild {
65+
cmake {
66+
// Provide the path to your CMakeLists.txt file
67+
path "src/main/cpp/CMakeLists.txt"
68+
}
5669
}
5770

5871
packagingOptions {

app/src/main/cpp/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# CMakeLists.txt
2+
cmake_minimum_required(VERSION 3.4.1)
3+
4+
add_library(
5+
signal_handler
6+
SHARED
7+
signal_handler.c
8+
)
9+
10+
find_library(
11+
log-lib
12+
log
13+
)
14+
15+
target_link_libraries(
16+
signal_handler
17+
${log-lib}
18+
)

app/src/main/cpp/signal_handler.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// signal_handler.c
2+
#include <jni.h>
3+
#include <signal.h>
4+
#include <android/log.h>
5+
6+
#define LOG_TAG "SignalHandler"
7+
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
8+
9+
void sigsys_handler(int signum, siginfo_t *info, void *context) {
10+
LOGI("SIGSYS signal received and ignored.");
11+
// Ignore the signal to prevent the app from crashing
12+
}
13+
14+
void setup_sigsys_handler() {
15+
struct sigaction sa;
16+
sa.sa_flags = SA_SIGINFO;
17+
sa.sa_sigaction = sigsys_handler;
18+
sigemptyset(&sa.sa_mask);
19+
if (sigaction(SIGSYS, &sa, NULL) == -1) {
20+
LOGI("Failed to set up SIGSYS handler");
21+
} else {
22+
LOGI("SIGSYS handler set up successfully");
23+
}
24+
}
25+
26+
// Correctly named JNI function
27+
JNIEXPORT void JNICALL
28+
Java_com_anytypeio_anytype_app_AndroidApplication_00024SignalHandler_initSignalHandler(JNIEnv *env, jobject thiz) {
29+
setup_sigsys_handler();
30+
}

app/src/main/java/com/anytypeio/anytype/app/AndroidApplication.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ class AndroidApplication : Application(), HasComponentDependencies {
5050
ComponentManager(main, this)
5151
}
5252

53+
5354
override fun onCreate() {
5455
if (BuildConfig.ENABLE_STRICT_MODE) {
5556
enableStrictMode()
5657
}
5758
super.onCreate()
59+
setupSignalHandler()
5860
main.inject(this)
5961
setupAnalytics()
6062
setupTimber()
@@ -111,10 +113,21 @@ class AndroidApplication : Application(), HasComponentDependencies {
111113
notificationManager.createNotificationChannel(notificationChannel)
112114
}
113115

116+
private fun setupSignalHandler() {
117+
SignalHandler.initSignalHandler()
118+
}
114119

115120
companion object {
116121
const val NOTIFICATION_CHANNEL_ID = "anytype_notification_channel"
117122
const val NOTIFICATION_CHANNEL_NAME = "Local Anytype notifications"
118123
const val NOTIFICATION_CHANNEL_DESCRIPTION = "Important notifications from Anytype, including collaboration events in multiplayer mode"
119124
}
125+
126+
object SignalHandler {
127+
init {
128+
System.loadLibrary(SIGNAL_HANDLER_LIB_NAME)
129+
}
130+
external fun initSignalHandler()
131+
const val SIGNAL_HANDLER_LIB_NAME = "signal_handler"
132+
}
120133
}

0 commit comments

Comments
 (0)