1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-22 07:48:50 -05:00

extract soft interrupt handlers

Delays handler setup until after config is set to a valid
value to avoid a segmentation fault.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Andrew Gregory 2015-11-30 13:07:11 -05:00 committed by Allan McRae
parent 8089081ef9
commit b8a7277061
3 changed files with 17 additions and 22 deletions

View File

@ -1092,7 +1092,6 @@ int main(int argc, char *argv[])
uid_t myuid = getuid(); uid_t myuid = getuid();
install_segv_handler(); install_segv_handler();
install_signal_handlers();
/* i18n init */ /* i18n init */
#if defined(ENABLE_NLS) #if defined(ENABLE_NLS)
@ -1108,6 +1107,8 @@ int main(int argc, char *argv[])
cleanup(1); cleanup(1);
} }
install_soft_interrupt_handler();
if(!isatty(fileno(stdout))) { if(!isatty(fileno(stdout))) {
/* disable progressbar if the output is redirected */ /* disable progressbar if the output is redirected */
config->noprogressbar = 1; config->noprogressbar = 1;

View File

@ -42,7 +42,7 @@ static ssize_t xwrite(int fd, const void *buf, size_t count)
* in a consistent state. * in a consistent state.
* @param signum the thrown signal * @param signum the thrown signal
*/ */
static void handler(int signum) static void soft_interrupt_handler(int signum)
{ {
if(signum == SIGINT) { if(signum == SIGINT) {
const char msg[] = "\nInterrupt signal received\n"; const char msg[] = "\nInterrupt signal received\n";
@ -55,13 +55,25 @@ static void handler(int signum)
/* a transaction is being interrupted, don't exit pacman yet. */ /* a transaction is being interrupted, don't exit pacman yet. */
return; return;
} }
/* SIGINT/SIGHUP: no committing transaction, release it now and then exit pacman */
alpm_unlock(config->handle); alpm_unlock(config->handle);
/* 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 */
xwrite(STDOUT_FILENO, "\n", 1); xwrite(STDOUT_FILENO, "\n", 1);
_Exit(128 + signum); _Exit(128 + signum);
} }
void install_soft_interrupt_handler(void)
{
struct sigaction new_action;
new_action.sa_handler = soft_interrupt_handler;
new_action.sa_flags = SA_RESTART;
sigemptyset(&new_action.sa_mask);
sigaddset(&new_action.sa_mask, SIGINT);
sigaddset(&new_action.sa_mask, SIGHUP);
sigaction(SIGINT, &new_action, NULL);
sigaction(SIGHUP, &new_action, NULL);
}
static void segv_handler(int signum) static void segv_handler(int signum)
{ {
const char msg[] = "\nerror: segmentation fault\n" const char msg[] = "\nerror: segmentation fault\n"
@ -94,22 +106,4 @@ void install_winch_handler(void)
sigaction(SIGWINCH, &new_action, NULL); sigaction(SIGWINCH, &new_action, NULL);
} }
void install_signal_handlers(void)
{
struct sigaction new_action;
const int signals[] = { SIGHUP, SIGINT };
size_t i;
/* Set signal handlers */
/* Set up the structure to specify the new action. */
new_action.sa_handler = handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = SA_RESTART;
/* assign our handler to any signals we care about */
for(i = 0; i < ARRAYSIZE(signals); i++) {
sigaction(signals[i], &new_action, NULL);
}
}
/* vim: set noet: */ /* vim: set noet: */

View File

@ -22,7 +22,7 @@
void install_segv_handler(void); void install_segv_handler(void);
void install_winch_handler(void); void install_winch_handler(void);
void install_signal_handlers(void); void install_soft_interrupt_handler(void);
#endif /* _PM_SIGHANDLER_H */ #endif /* _PM_SIGHANDLER_H */