Revamp pacman signal handler
* All errors now go to stderr, so do the same here and simplify the writing of the error message. * Add SIGHUP to the handled signal list, and don't repeat code. * Attempt to release the transaction (e.g. remove the lock file) for all of HUP, INT, and TERM. Signals HUP and INT respects transaction state, TERM will immediately terminate the process. Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
2517ba3303
commit
d26d50e664
1 changed files with 23 additions and 23 deletions
|
@ -299,26 +299,29 @@ static void handler(int signum)
|
||||||
{
|
{
|
||||||
int out = fileno(stdout);
|
int out = fileno(stdout);
|
||||||
int err = fileno(stderr);
|
int err = fileno(stderr);
|
||||||
|
const char *msg;
|
||||||
if(signum == SIGSEGV) {
|
if(signum == SIGSEGV) {
|
||||||
const char *msg1 = "error: segmentation fault\n";
|
msg = "\nerror: segmentation fault\n"
|
||||||
const char *msg2 = "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(err, msg, strlen(msg));
|
||||||
xwrite(out, msg1, strlen(msg1));
|
|
||||||
xwrite(err, msg2, strlen(msg2));
|
|
||||||
exit(signum);
|
exit(signum);
|
||||||
} else if(signum == SIGINT) {
|
} else if(signum == SIGINT || signum == SIGHUP) {
|
||||||
const char *msg = "\nInterrupt signal received\n";
|
if(signum == SIGINT) {
|
||||||
|
msg = "\nInterrupt signal received\n";
|
||||||
|
} else {
|
||||||
|
msg = "\nHangup signal received\n";
|
||||||
|
}
|
||||||
xwrite(err, msg, strlen(msg));
|
xwrite(err, msg, strlen(msg));
|
||||||
if(alpm_trans_interrupt(config->handle) == 0) {
|
if(alpm_trans_interrupt(config->handle) == 0) {
|
||||||
/* a transaction is being interrupted, don't exit pacman yet. */
|
/* a transaction is being interrupted, don't exit pacman yet. */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* no commiting transaction, we can release it now and then exit pacman */
|
|
||||||
alpm_trans_release(config->handle);
|
|
||||||
/* output a newline to be sure we clear any line we may be on */
|
|
||||||
xwrite(out, "\n", 1);
|
|
||||||
}
|
}
|
||||||
|
/* SIGINT: no commiting transaction, release it now and then exit pacman
|
||||||
|
* SIGHUP, SIGTERM: release no matter what */
|
||||||
|
alpm_trans_release(config->handle);
|
||||||
|
/* output a newline to be sure we clear any line we may be on */
|
||||||
|
xwrite(out, "\n", 1);
|
||||||
cleanup(128 + signum);
|
cleanup(128 + signum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -757,8 +760,9 @@ static void cl_to_log(int argc, char* argv[])
|
||||||
*/
|
*/
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int i, ret = 0;
|
||||||
struct sigaction new_action, old_action;
|
struct sigaction new_action, old_action;
|
||||||
|
const int signals[] = { SIGHUP, SIGINT, SIGTERM, SIGSEGV };
|
||||||
#if defined(HAVE_GETEUID) && !defined(CYGWIN)
|
#if defined(HAVE_GETEUID) && !defined(CYGWIN)
|
||||||
/* geteuid undefined in CYGWIN */
|
/* geteuid undefined in CYGWIN */
|
||||||
uid_t myuid = geteuid();
|
uid_t myuid = geteuid();
|
||||||
|
@ -775,17 +779,13 @@ int main(int argc, char *argv[])
|
||||||
sigemptyset(&new_action.sa_mask);
|
sigemptyset(&new_action.sa_mask);
|
||||||
new_action.sa_flags = 0;
|
new_action.sa_flags = 0;
|
||||||
|
|
||||||
sigaction(SIGINT, NULL, &old_action);
|
/* assign our handler to any signals we care about */
|
||||||
if(old_action.sa_handler != SIG_IGN) {
|
for(i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) {
|
||||||
sigaction(SIGINT, &new_action, NULL);
|
int signal = signals[i];
|
||||||
}
|
sigaction(signal, NULL, &old_action);
|
||||||
sigaction(SIGTERM, NULL, &old_action);
|
if(old_action.sa_handler != SIG_IGN) {
|
||||||
if(old_action.sa_handler != SIG_IGN) {
|
sigaction(signal, &new_action, NULL);
|
||||||
sigaction(SIGTERM, &new_action, NULL);
|
}
|
||||||
}
|
|
||||||
sigaction(SIGSEGV, NULL, &old_action);
|
|
||||||
if(old_action.sa_handler != SIG_IGN) {
|
|
||||||
sigaction(SIGSEGV, &new_action, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* i18n init */
|
/* i18n init */
|
||||||
|
|
Loading…
Add table
Reference in a new issue