Exit with an error code if Ctrl+C is pressed

This commit is contained in:
Andrea Mazzoleni 2025-10-17 08:49:11 +02:00
parent b4c96e5afa
commit e00342557e
4 changed files with 30 additions and 46 deletions

View file

@ -407,7 +407,7 @@ bail:
return 0;
}
void state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount)
int state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount)
{
block_off_t blockmax;
int ret;
@ -467,11 +467,8 @@ void state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t
}
}
/* abort if required */
if (error != 0) {
/* LCOV_EXCL_START */
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (error != 0)
return -1;
return 0;
}

View file

@ -326,16 +326,13 @@ int main(int argc, char* argv[])
sa.sa_flags = SA_RESTART; /* restart interrupted system calls */
/* install forwarding handler for signals */
sigaction(SIGINT, &sa, NULL); /* Ctrl+C */
sigaction(SIGQUIT, &sa, NULL); /* Ctrl+\ */
sigaction(SIGTERM, &sa, NULL); /* termination */
sigaction(SIGHUP, &sa, NULL); /* hangup */
sigaction(SIGPIPE, &sa, NULL); /* broken pipe */
sigaction(SIGALRM, &sa, NULL); /* alarm/timer */
sigaction(SIGUSR1, &sa, NULL); /* user-defined 1 */
sigaction(SIGUSR2, &sa, NULL); /* user-defined 2 */
sigaction(SIGTSTP, &sa, NULL); /* Ctrl+Z */
sigaction(SIGINT, &sa, NULL); /* Ctrl+C */
sigaction(SIGQUIT, &sa, NULL); /* Ctrl+\ */
sigaction(SIGTERM, &sa, NULL); /* termination */
sigaction(SIGHUP, &sa, NULL); /* hangup */
sigaction(SIGUSR1, &sa, NULL); /* user-defined 1 */
sigaction(SIGUSR2, &sa, NULL); /* user-defined 2 */
pid_t pid = fork();
if (pid == -1) {
perror("Failed to fork SnapRAID");
@ -350,11 +347,8 @@ int main(int argc, char* argv[])
signal(SIGQUIT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGHUP, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
signal(SIGALRM, SIG_DFL);
signal(SIGUSR1, SIG_DFL);
signal(SIGUSR2, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
execvp(get_argv0(argv[0]), argv);
@ -385,11 +379,8 @@ int main(int argc, char* argv[])
signal(SIGQUIT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGHUP, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
signal(SIGALRM, SIG_DFL);
signal(SIGUSR1, SIG_DFL);
signal(SIGUSR2, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);

View file

@ -1347,6 +1347,7 @@ int snapraid_main(int argc, char* argv[])
(void)lock;
#endif
ret = 0;
if (operation == OPERATION_DIFF) {
state_read(&state);
@ -1419,13 +1420,6 @@ int snapraid_main(int argc, char* argv[])
sleep(2);
ret = state_sync(&state, blockstart, blockcount);
/* abort if required */
if (ret != 0) {
/* LCOV_EXCL_START */
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
} else if (operation == OPERATION_DRY) {
state_read(&state);
@ -1438,7 +1432,7 @@ int snapraid_main(int argc, char* argv[])
/* intercept signals while operating */
signal_init();
state_dry(&state, blockstart, blockcount);
ret = state_dry(&state, blockstart, blockcount);
} else if (operation == OPERATION_REHASH) {
state_read(&state);
@ -1459,13 +1453,6 @@ int snapraid_main(int argc, char* argv[])
signal_init();
ret = state_scrub(&state, plan, olderthan);
/* abort if required */
if (ret != 0) {
/* LCOV_EXCL_START */
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
} else if (operation == OPERATION_REWRITE) {
state_read(&state);
@ -1548,13 +1535,6 @@ int snapraid_main(int argc, char* argv[])
} else { /* it's fix */
ret = state_check(&state, 1, blockstart, blockcount);
}
/* abort if required */
if (ret != 0) {
/* LCOV_EXCL_START */
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
}
/* close log file */
@ -1578,5 +1558,21 @@ int snapraid_main(int argc, char* argv[])
os_done();
lock_done();
/* abort if required */
if (ret != 0) {
/* LCOV_EXCL_START */
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (global_interrupt) {
/* LCOV_EXCL_START */
#ifdef _WIN32
exit(STATUS_CONTROL_C_EXIT);
#else
exit(128 + SIGINT);
#endif
/* LCOV_EXCL_STOP */
}
return EXIT_SUCCESS;
}

View file

@ -259,7 +259,7 @@ int state_check(struct snapraid_state* state, int fix, block_off_t blockstart, b
/**
* Dry the files.
*/
void state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount);
int state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount);
/**
* Rehash the files.