Make interrupt handler async-safe

Calling printf() in a signal handler can be dangerous, so avoid it by
writing directly which is guaranteed to be safe according to signal(7).

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2008-10-12 21:07:49 -05:00
parent f0e1846b51
commit 30851a24ff

View file

@ -36,6 +36,7 @@
#include <sys/utsname.h> /* uname */ #include <sys/utsname.h> /* uname */
#include <locale.h> /* setlocale */ #include <locale.h> /* setlocale */
#include <time.h> /* time_t */ #include <time.h> /* time_t */
#include <errno.h>
#if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H) #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
#include <mcheck.h> /* debug tracing (mtrace) */ #include <mcheck.h> /* debug tracing (mtrace) */
#endif #endif
@ -211,21 +212,34 @@ static void cleanup(int ret) {
exit(ret); exit(ret);
} }
/** Write function that correctly handles EINTR.
*/
static ssize_t xwrite(int fd, const void *buf, size_t count)
{
ssize_t ret;
while((ret = write(fd, buf, count)) == -1 && errno == EINTR);
return(ret);
}
/** Catches thrown signals. Performs necessary cleanup to ensure database is /** Catches thrown signals. Performs necessary cleanup to ensure database is
* in a consistant state. * in a consistant state.
* @param signum the thrown signal * @param signum the thrown signal
*/ */
static RETSIGTYPE handler(int signum) static RETSIGTYPE handler(int signum)
{ {
if(signum==SIGSEGV) int out = fileno(stdout);
{ int err = fileno(stderr);
/* write a log message and write to stderr */ if(signum == SIGSEGV) {
pm_printf(PM_LOG_ERROR, _("segmentation fault\n")); const char *msg1 = "error: segmentation fault\n";
pm_fprintf(stderr, PM_LOG_ERROR, const char *msg2 = "Internal pacman error: Segmentation fault.\n"
_("Internal pacman error: Segmentation fault.\n" "Please submit a full bug report with --debug if appropriate.\n";
"Please submit a full bug report with --debug if appropriate.\n")); /* write a error message to out, the rest to err */
xwrite(out, msg1, strlen(msg1));
xwrite(err, msg2, strlen(msg2));
exit(signum); exit(signum);
} else if((signum == SIGINT)) { } else if((signum == SIGINT)) {
const char *msg = "\nInterrupt signal received\n";
xwrite(err, msg, strlen(msg));
if(alpm_trans_interrupt() == 0) { if(alpm_trans_interrupt() == 0) {
/* a transaction is being interrupted, don't exit pacman yet. */ /* a transaction is being interrupted, don't exit pacman yet. */
return; return;
@ -233,7 +247,7 @@ static RETSIGTYPE handler(int signum)
/* no commiting transaction, we can release it now and then exit pacman */ /* no commiting transaction, we can release it now and then exit pacman */
alpm_trans_release(); alpm_trans_release();
/* output a newline to be sure we clear any line we may be on */ /* output a newline to be sure we clear any line we may be on */
printf("\n"); xwrite(out, "\n", 1);
} }
cleanup(signum); cleanup(signum);
} }