proxmark3
proxmark3 copied to clipboard
command history not save when ctrl-c exiting client
Describe the bug When you press ctrl-c to exit / interupt the client (it happens) the command history isn't saved. So you loose the commands you tried.
To Reproduce
- enter client.,
- run a command you didn't have in your history.
- press ctrl-c
- enter client again
- up arrow, doesn't show the command you ran.
Expected behavior I expect the client to save the history after each run?
Desktop (please complete the following information): latest source from repo.
we should be able to trap the ctrl-c and save before exit I would avoid writing the file at each cmd
#include <signal.h>
#include <stdlib.h>
void intHandler(int) {
savehist...;
}
int main(int argc, char *argv[]) {
struct sigaction act;
act.sa_handler = intHandler;
sigaction(SIGINT, &act, NULL);
...
}
I have experimented with this one.
Easy to add signal but the readline call uses its own signals.
So even if we press ctrl-c , we still need to "finish" readlines waiting for user to press enter, before we can exit loop.
or we exit direct, but then we don't have access to the history log file.
meaning we would need to put it in a global session.
this might help https://stackoverflow.com/a/34675886
if you do, you loose ctrl-r , ctrl-u etc...
I had a quick play and got a rough solution that seems to work, but not fully tested in any way.
My approach was
- Setup a signal handler to catch the ctrl-c If ctrl-c is seen, write out the history and exit (Key note: since this is an abrupt exit it may not be ideal)
void handle_signals(int signo) {
if (signo == SIGINT) {
printf("You pressed Ctrl+C\n");
if (g_session.history_path) {
write_history(g_session.history_path);
free(g_session.history_path);
}
exit (0);
}
}
- near the top of main_loop register the signal handler
if (signal(SIGINT, handle_signals) == SIG_ERR) {
printf("failed to register interrupts with kernel\n");
}
thoughts ?
Note that we've already similar SIGINT handler: https://github.com/RfidResearchGroup/proxmark3/blob/e72fbb4983ac1775968081c763c3b1dde36eb90e/client/src/proxmark3.c#L164-L168
yeah, Dunno if I remember it correct, but I looked into this too a year ago, and I think I needed to guard the calls for signal handling and for the log history which is readline related.
What is the status exactly today ? On Linux it seems to always save the history properly on ctrl-c. I seldom encountered history files saved with corrupted filenames in the current workdir (but never managed to reproduce it when I wanted), that's why I pushed now a history_path=NULL to avoid to be used after free. On Windows (PS) the signal handling code is commented.