How can I catch SIGSEGV (segmentation fault) and get a stack trace under JNI on Android?

Edit: From Jelly Bean onwards you can’t get the stack trace, because READ_LOGS went away. 🙁 I actually got a signal handler working without doing anything too exotic, and have released code using it, which you can see on github (edit: linking to historical release; I removed the crash handler since then). Here’s how: Use … Read more

How to avoid using printf in a signal handler?

The primary problem is that if the signal interrupts malloc() or some similar function, the internal state may be temporarily inconsistent while it is moving blocks of memory between the free and used list, or other similar operations. If the code in the signal handler calls a function that then invokes malloc(), this may completely … Read more

In what order should I send signals to gracefully shutdown processes?

SIGTERM tells an application to terminate. The other signals tell the application other things which are unrelated to shutdown but may sometimes have the same result. Don’t use those. If you want an application to shut down, tell it to. Don’t give it misleading signals. Some people believe the smart standard way of terminating a … Read more

Signal handling with multiple threads in Linux

pthreads(7) describes that POSIX.1 requires all threads in a process share attributes, including: signal dispositions POSIX.1 also requires some attributes to be distinct for each thread, including: signal mask (pthread_sigmask(3)) alternate signal stack (sigaltstack(2)) The Linux kernel’s complete_signal routine has the following code block — the comments are quite useful: /* * Now find a … Read more

What is the difference between sigaction and signal?

Use sigaction() unless you’ve got very compelling reasons not to do so. The signal() interface has antiquity (and hence availability) in its favour, and it is defined in the C standard. Nevertheless, it has a number of undesirable characteristics that sigaction() avoids – unless you use the flags explicitly added to sigaction() to allow it … Read more

How can I catch a ctrl-c event?

signal isn’t the most reliable way as it differs in implementations. I would recommend using sigaction. Tom’s code would now look like this : #include <signal.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> void my_handler(int s){ printf(“Caught signal %d\n”,s); exit(1); } int main(int argc,char** argv) { struct sigaction sigIntHandler; sigIntHandler.sa_handler = my_handler; sigemptyset(&sigIntHandler.sa_mask); sigIntHandler.sa_flags = 0; … Read more

Catch Ctrl-C in C

With a signal handler. Here is a simple example flipping a bool used in main(): #include <signal.h> static volatile int keepRunning = 1; void intHandler(int dummy) { keepRunning = 0; } // … int main(void) { signal(SIGINT, intHandler); while (keepRunning) { // … Edit in June 2017: To whom it may concern, particularly those with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)