chg: added the mutex lock to printing in communications thread.

This commit is contained in:
iceman1001 2018-02-18 10:36:15 +01:00
commit e6f51451aa

View file

@ -40,8 +40,10 @@ static UsbCommand txcmd;
static char comport[255]; static char comport[255];
byte_t rx[sizeof(UsbCommand)]; byte_t rx[sizeof(UsbCommand)];
byte_t* prx = rx; byte_t* prx = rx;
volatile static bool txcmd_pending = false; volatile static bool txcmd_pending = false;
struct receiver_arg {
int run;
};
void SendCommand(UsbCommand *c) { void SendCommand(UsbCommand *c) {
#if 0 #if 0
@ -65,9 +67,6 @@ void SendCommand(UsbCommand *c) {
__atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST); __atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST);
} }
struct receiver_arg {
int run;
};
#if defined(__linux__) || (__APPLE__) #if defined(__linux__) || (__APPLE__)
static void showBanner(void){ static void showBanner(void){
@ -87,19 +86,23 @@ static void showBanner(void){
bool hookUpPM3() { bool hookUpPM3() {
bool ret = false; bool ret = false;
sp = uart_open( comport ); sp = uart_open( comport );
pthread_mutex_lock(&print_lock);
if (sp == INVALID_SERIAL_PORT) { if (sp == INVALID_SERIAL_PORT) {
printf("Reconnect failed, retrying... (reason: invalid serial port)\n"); printf("[!] Reconnect failed, retrying... (reason: invalid serial port)\n");
ret = false; ret = false;
offline = 1; offline = 1;
} else if (sp == CLAIMED_SERIAL_PORT) { } else if (sp == CLAIMED_SERIAL_PORT) {
printf("Reconnect failed, retrying... (reason: serial port is claimed by another process)\n"); printf("[!] Reconnect failed, retrying... (reason: serial port is claimed by another process)\n");
ret = false; ret = false;
offline = 1; offline = 1;
} else { } else {
printf("Proxmark reconnected\n"); printf("[+] Proxmark reconnected\n");
ret = true; ret = true;
offline = 0; offline = 0;
} }
pthread_mutex_unlock(&print_lock);
return ret; return ret;
} }
@ -137,7 +140,7 @@ __attribute__((force_align_arg_pointer))
bool res = uart_send(sp, (byte_t*) &txcmd, sizeof(UsbCommand)); bool res = uart_send(sp, (byte_t*) &txcmd, sizeof(UsbCommand));
if (!res) { if (!res) {
counter_to_offline++; counter_to_offline++;
PrintAndLog("Sending bytes to proxmark failed"); PrintAndLog("[!] sending bytes to proxmark failed");
} }
__atomic_clear(&txcmd_pending, __ATOMIC_SEQ_CST); __atomic_clear(&txcmd_pending, __ATOMIC_SEQ_CST);
@ -185,8 +188,11 @@ main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
if (script_cmds_file) { if (script_cmds_file) {
sf = fopen(script_cmds_file, "r"); sf = fopen(script_cmds_file, "r");
if (sf) if (sf) {
pthread_mutex_lock(&print_lock);
printf("[+] executing commands from file: %s\n", script_cmds_file); printf("[+] executing commands from file: %s\n", script_cmds_file);
pthread_mutex_unlock(&print_lock);
}
} }
read_history(".history"); read_history(".history");
@ -223,14 +229,18 @@ main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
strcleanrn(script_cmd_buf, sizeof(script_cmd_buf)); strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
if ((cmd = strmcopy(script_cmd_buf)) != NULL) { if ((cmd = strmcopy(script_cmd_buf)) != NULL) {
pthread_mutex_lock(&print_lock);
printf(PROXPROMPT"%s\n", cmd); printf(PROXPROMPT"%s\n", cmd);
pthread_mutex_unlock(&print_lock);
} }
} }
} else { } else {
// If there is a script command // If there is a script command
if (execCommand){ if (execCommand){
if ((cmd = strmcopy(script_cmd)) != NULL) { if ((cmd = strmcopy(script_cmd)) != NULL) {
pthread_mutex_lock(&print_lock);
printf(PROXPROMPT"%s\n", cmd); printf(PROXPROMPT"%s\n", cmd);
pthread_mutex_unlock(&print_lock);
} }
execCommand = false; execCommand = false;
@ -243,13 +253,17 @@ main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
if (stdinOnPipe) { if (stdinOnPipe) {
memset(script_cmd_buf, 0, sizeof(script_cmd_buf)); memset(script_cmd_buf, 0, sizeof(script_cmd_buf));
if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), stdin)) { if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), stdin)) {
pthread_mutex_lock(&print_lock);
printf("\n[!] stdin end, exit...\n"); printf("\n[!] stdin end, exit...\n");
pthread_mutex_unlock(&print_lock);
break; break;
} }
strcleanrn(script_cmd_buf, sizeof(script_cmd_buf)); strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
if ((cmd = strmcopy(script_cmd_buf)) != NULL) { if ((cmd = strmcopy(script_cmd_buf)) != NULL) {
pthread_mutex_lock(&print_lock);
printf(PROXPROMPT"%s\n", cmd); printf(PROXPROMPT"%s\n", cmd);
pthread_mutex_unlock(&print_lock);
} }
} else { } else {