proxmark3 icon indicating copy to clipboard operation
proxmark3 copied to clipboard

command history not save when ctrl-c exiting client

Open iceman1001 opened this issue 5 years ago • 9 comments
trafficstars

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

  1. enter client.,
  2. run a command you didn't have in your history.
  3. press ctrl-c
  4. enter client again
  5. 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.

iceman1001 avatar Jun 12 '20 16:06 iceman1001

we should be able to trap the ctrl-c and save before exit I would avoid writing the file at each cmd

doegox avatar Jun 12 '20 17:06 doegox

#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);
...
}

doegox avatar Jun 12 '20 17:06 doegox

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.

iceman1001 avatar Jun 25 '20 07:06 iceman1001

this might help https://stackoverflow.com/a/34675886

doegox avatar Jun 25 '20 12:06 doegox

if you do, you loose ctrl-r , ctrl-u etc...

iceman1001 avatar Jun 25 '20 12:06 iceman1001

I had a quick play and got a rough solution that seems to work, but not fully tested in any way.

My approach was

  1. 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);
    }
}
  1. 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 ?

mwalker33 avatar Oct 14 '21 08:10 mwalker33

Note that we've already similar SIGINT handler: https://github.com/RfidResearchGroup/proxmark3/blob/e72fbb4983ac1775968081c763c3b1dde36eb90e/client/src/proxmark3.c#L164-L168

doegox avatar Oct 14 '21 08:10 doegox

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.

iceman1001 avatar Oct 14 '21 19:10 iceman1001

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.

doegox avatar Oct 16 '21 10:10 doegox