add tty handling for FreeBSD
[hashcat.git] / src / shared.c
index 026af29..5033dae 100644 (file)
@@ -6,10 +6,15 @@
  * License.....: MIT
  */
 
-#ifdef OSX
+#ifdef DARWIN
 #include <stdio.h>
 #endif
 
+#ifdef __FreeBSD__
+#include <stdio.h>
+#include <pthread_np.h>
+#endif
+
 #include <shared.h>
 #include <limits.h>
 
@@ -2334,14 +2339,14 @@ void drupal7_encode (u8 digest[64], u8 buf[43])
  */
 
 #ifdef LINUX
-static struct termio savemodes;
+static struct termios savemodes;
 static int havemodes = 0;
 
 int tty_break()
 {
-  struct termio modmodes;
+  struct termios modmodes;
 
-  if (ioctl (fileno (stdin), TCGETA, &savemodes) < 0) return -1;
+  if (tcgetattr (fileno (stdin), &savemodes) < 0) return -1;
 
   havemodes = 1;
 
@@ -2350,7 +2355,7 @@ int tty_break()
   modmodes.c_cc[VMIN] = 1;
   modmodes.c_cc[VTIME] = 0;
 
-  return ioctl (fileno (stdin), TCSETAW, &modmodes);
+  return tcsetattr (fileno (stdin), TCSANOW, &modmodes);
 }
 
 int tty_getchar()
@@ -2378,11 +2383,11 @@ int tty_fix()
 {
   if (!havemodes) return 0;
 
-  return ioctl (fileno (stdin), TCSETAW, &savemodes);
+  return tcsetattr (fileno (stdin), TCSADRAIN, &savemodes);
 }
 #endif
 
-#ifdef OSX
+#if defined(DARWIN) || defined(__FreeBSD__)
 static struct termios savemodes;
 static int havemodes = 0;
 
@@ -4358,7 +4363,7 @@ char *get_exec_path ()
 
   const int len = GetModuleFileName (NULL, exec_path, exec_path_len - 1);
 
-  #elif OSX
+  #elif DARWIN
 
   uint size = exec_path_len;
 
@@ -4371,6 +4376,20 @@ char *get_exec_path ()
 
   const int len = strlen (exec_path);
 
+  #elif __FreeBSD__
+
+  #include <sys/sysctl.h>
+
+  int mib[4];
+  mib[0] = CTL_KERN;
+  mib[1] = KERN_PROC;
+  mib[2] = KERN_PROC_PATHNAME;
+  mib[3] = -1;
+
+  size_t size = sizeof(exec_path);
+
+  const int len = sysctl(mib, 4, exec_path, &size, NULL, 0);
+
   #else
   #error Your Operating System is not supported or detected
   #endif
@@ -4495,7 +4514,7 @@ void truecrypt_crc32 (const char *filename, u8 keytab[64])
   myfree (buf);
 }
 
-#ifdef OSX
+#ifdef DARWIN
 int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
 {
   int core;
@@ -4523,6 +4542,9 @@ void set_cpu_affinity (char *cpu_affinity)
 {
   #ifdef _WIN
   DWORD_PTR aff_mask = 0;
+  #elif __FreeBSD__
+  cpuset_t cpuset;
+  CPU_ZERO (&cpuset);
   #elif _POSIX
   cpu_set_t cpuset;
   CPU_ZERO (&cpuset);
@@ -4570,6 +4592,9 @@ void set_cpu_affinity (char *cpu_affinity)
   #ifdef _WIN
   SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
   SetThreadAffinityMask (GetCurrentThread (), aff_mask);
+  #elif __FreeBSD__
+  pthread_t thread = pthread_self ();
+  pthread_setaffinity_np (thread, sizeof (cpuset_t), &cpuset);
   #elif _POSIX
   pthread_t thread = pthread_self ();
   pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
@@ -5763,7 +5788,7 @@ char **scan_directory (const char *path)
 
   if ((d = opendir (tmp_path)) != NULL)
   {
-    #ifdef OSX
+    #ifdef DARWIN
     struct dirent e;
 
     for (;;) {
@@ -9162,36 +9187,32 @@ void to_hccap_t (hccap_t *hccap, uint salt_pos, uint digest_pos)
 
 void SuspendThreads ()
 {
-  if (data.devices_status == STATUS_RUNNING)
-  {
-    hc_timer_set (&data.timer_paused);
+  if (data.devices_status != STATUS_RUNNING) return;
 
-    data.devices_status = STATUS_PAUSED;
+  hc_timer_set (&data.timer_paused);
 
-    log_info ("Paused");
-  }
+  data.devices_status = STATUS_PAUSED;
+
+  log_info ("Paused");
 }
 
 void ResumeThreads ()
 {
-  if (data.devices_status == STATUS_PAUSED)
-  {
-    double ms_paused;
+  if (data.devices_status != STATUS_PAUSED) return;
 
-    hc_timer_get (data.timer_paused, ms_paused);
+  double ms_paused;
 
-    data.ms_paused += ms_paused;
+  hc_timer_get (data.timer_paused, ms_paused);
 
-    data.devices_status = STATUS_RUNNING;
+  data.ms_paused += ms_paused;
 
-    log_info ("Resumed");
-  }
+  data.devices_status = STATUS_RUNNING;
+
+  log_info ("Resumed");
 }
 
 void bypass ()
 {
-  if (data.devices_status != STATUS_RUNNING) return;
-
   data.devices_status = STATUS_BYPASS;
 
   log_info ("Next dictionary / mask in queue selected, bypassing current one");
@@ -9239,17 +9260,11 @@ void stop_at_checkpoint ()
 
 void myabort ()
 {
-  //if (data.devices_status == STATUS_INIT)     return;
-  //if (data.devices_status == STATUS_STARTING) return;
-
   data.devices_status = STATUS_ABORTED;
 }
 
 void myquit ()
 {
-  //if (data.devices_status == STATUS_INIT)     return;
-  //if (data.devices_status == STATUS_STARTING) return;
-
   data.devices_status = STATUS_QUIT;
 }
 
@@ -20737,7 +20752,7 @@ BOOL WINAPI sigHandler_benchmark (DWORD sig)
   {
     case CTRL_CLOSE_EVENT:
 
-      myabort ();
+      myquit ();
 
       SetConsoleCtrlHandler (NULL, TRUE);
 
@@ -20806,7 +20821,7 @@ void *thread_keypress (void *p)
 
   tty_break();
 
-  while ((data.devices_status != STATUS_EXHAUSTED) && (data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT))
+  while (data.shutdown_outer == 0)
   {
     int ch = tty_getchar();