Rizin
unix-like reverse engineering framework and cli tools
sys.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_userconf.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #if defined(__NetBSD__)
8 #include <sys/param.h>
9 #include <sys/sysctl.h>
10 #endif
11 #if defined(__FreeBSD__)
12 #include <sys/param.h>
13 #include <sys/sysctl.h>
14 #endif
15 #if defined(__DragonFly__)
16 #include <sys/param.h>
17 #include <sys/sysctl.h>
18 #endif
19 #if defined(__OpenBSD__)
20 #include <sys/sysctl.h>
21 #include <sys/stat.h>
22 #endif
23 #if defined(__HAIKU__)
24 #include <kernel/image.h>
25 #include <sys/param.h>
26 #endif
27 #include <sys/types.h>
28 #include <rz_types.h>
29 #include <rz_util.h>
30 #include <rz_lib.h>
31 
32 static char **env = NULL;
33 
34 #if HAVE_BACKTRACE
35 #include <execinfo.h>
36 #endif
37 #if __APPLE__
38 #include <errno.h>
39 
40 #if HAVE_ENVIRON || __APPLE__
41 #include <execinfo.h>
42 #endif
43 // iOS don't have this we can't hardcode
44 // #include <crt_externs.h>
45 extern char ***_NSGetEnviron(void);
46 #ifndef PROC_PIDPATHINFO_MAXSIZE
47 #define PROC_PIDPATHINFO_MAXSIZE 1024
48 int proc_pidpath(int pid, void *buffer, ut32 buffersize);
49 //# include <libproc.h>
50 #endif
51 #endif
52 #if __UNIX__
53 #include <sys/utsname.h>
54 #include <sys/wait.h>
55 #include <sys/stat.h>
56 #include <errno.h>
57 #include <signal.h>
58 extern char **environ;
59 
60 #ifdef __HAIKU__
61 #define Sleep sleep
62 #endif
63 #endif
64 #if __WINDOWS__
65 #include <io.h>
66 #include <rz_windows.h>
67 #include <VersionHelpers.h>
68 #include <signal.h>
69 #define TMP_BUFSIZE 4096
70 #ifdef _MSC_VER
71 #include <psapi.h>
72 #include <process.h> // to allow getpid under windows msvc compilation
73 #include <direct.h> // to allow getcwd under windows msvc compilation
74 #endif
75 #endif
76 
77 RZ_LIB_VERSION(rz_util);
78 
79 #ifdef __x86_64__
80 #ifdef _MSC_VER
81 #define RZ_SYS_ASM_START_ROP() \
82  eprintf("rz_sys_run_rop: Unsupported arch\n");
83 #else
84 #define RZ_SYS_ASM_START_ROP() \
85  __asm__ __volatile__("leaq %0, %%rsp; ret" \
86  : \
87  : "m"(*bufptr));
88 #endif
89 #elif __i386__
90 #ifdef _MSC_VER
91 #define RZ_SYS_ASM_START_ROP() \
92  __asm { \
93  __asm lea esp, bufptr\
94  __asm ret \
95  }
96 #else
97 #define RZ_SYS_ASM_START_ROP() \
98  __asm__ __volatile__("leal %0, %%esp; ret" \
99  : \
100  : "m"(*bufptr));
101 #endif
102 #else
103 #define RZ_SYS_ASM_START_ROP() \
104  eprintf("rz_sys_run_rop: Unsupported arch\n");
105 #endif
106 
107 static const struct {
108  const char *name;
110 } arch_bit_array[] = {
111  { "x86", RZ_SYS_ARCH_X86 },
112  { "arm", RZ_SYS_ARCH_ARM },
113  { "ppc", RZ_SYS_ARCH_PPC },
114  { "m68k", RZ_SYS_ARCH_M68K },
115  { "java", RZ_SYS_ARCH_JAVA },
116  { "mips", RZ_SYS_ARCH_MIPS },
117  { "sparc", RZ_SYS_ARCH_SPARC },
118  { "xap", RZ_SYS_ARCH_XAP },
119  { "tms320", RZ_SYS_ARCH_TMS320 },
120  { "msil", RZ_SYS_ARCH_MSIL },
121  { "objd", RZ_SYS_ARCH_OBJD },
122  { "bf", RZ_SYS_ARCH_BF },
123  { "sh", RZ_SYS_ARCH_SH },
124  { "avr", RZ_SYS_ARCH_AVR },
125  { "dalvik", RZ_SYS_ARCH_DALVIK },
126  { "z80", RZ_SYS_ARCH_Z80 },
127  { "arc", RZ_SYS_ARCH_ARC },
128  { "i8080", RZ_SYS_ARCH_I8080 },
129  { "rar", RZ_SYS_ARCH_RAR },
130  { "lm32", RZ_SYS_ARCH_LM32 },
131  { "v850", RZ_SYS_ARCH_V850 },
132  { NULL, 0 }
133 };
134 
135 #if HAVE_SIGACTION
136 RZ_API int rz_sys_sigaction(int *sig, void (*handler)(int)) {
137  struct sigaction sigact = {};
138  int ret, i;
139 
140  if (!sig) {
141  return -EINVAL;
142  }
143 
144  sigact.sa_handler = handler;
145  sigemptyset(&sigact.sa_mask);
146 
147  for (i = 0; sig[i] != 0; i++) {
148  sigaddset(&sigact.sa_mask, sig[i]);
149  }
150 
151  for (i = 0; sig[i] != 0; i++) {
152  ret = sigaction(sig[i], &sigact, NULL);
153  if (ret) {
154  eprintf("Failed to set signal handler for signal '%d': %s\n", sig[i], strerror(errno));
155  return ret;
156  }
157  }
158 
159  return 0;
160 }
161 #else
162 RZ_API int rz_sys_sigaction(int *sig, void (*handler)(int)) {
163  if (!sig) {
164  return -EINVAL;
165  }
166  size_t i;
167  for (i = 0; sig[i] != 0; i++) {
168  void (*ret)(int) = signal(sig[i], handler);
169  if (ret == SIG_ERR) {
170  eprintf("Failed to set signal handler for signal '%d': %s\n", sig[i], strerror(errno));
171  return -1;
172  }
173  }
174  return 0;
175 }
176 #endif
177 
178 RZ_API int rz_sys_signal(int sig, void (*handler)(int)) {
179  int s[2] = { sig, 0 };
180  return rz_sys_sigaction(s, handler);
181 }
182 
183 RZ_API void rz_sys_exit(int status, bool nocleanup) {
184  if (nocleanup) {
185  _exit(status);
186  } else {
187  exit(status);
188  }
189 }
190 
191 #if __WINDOWS__
192 static HANDLE sys_opendir(const char *path, WIN32_FIND_DATAW *entry) {
194  wchar_t dir[MAX_PATH];
195  wchar_t *wcpath = 0;
196  static bool win_ver_initialized = false;
197  static bool is_win_7_or_greater = false;
198  if (!win_ver_initialized) {
199  is_win_7_or_greater = IsWindows7OrGreater();
200  win_ver_initialized = true;
201  }
202  if (!(wcpath = rz_utf8_to_utf16(path))) {
203  return NULL;
204  }
205  swprintf(dir, MAX_PATH, L"%ls\\*.*", wcpath);
206  free(wcpath);
207  return FindFirstFileExW(dir, is_win_7_or_greater ? FindExInfoBasic : FindExInfoStandard, entry, FindExSearchNameMatch, NULL, 0);
208 }
209 #else
210 static DIR *sys_opendir(const char *path) {
212  return opendir(path);
213 }
214 #endif
215 
216 RZ_API RzList *rz_sys_dir(const char *path) {
217  RzList *list = NULL;
218 #if __WINDOWS__
219  WIN32_FIND_DATAW entry;
220  char *cfname;
222  if (fh == INVALID_HANDLE_VALUE) {
223  // IFDGB eprintf ("Cannot open directory %ls\n", wcpath);
224  return list;
225  }
227  if (list) {
228  do {
229  if ((cfname = rz_utf16_to_utf8(entry.cFileName))) {
230  rz_list_append(list, cfname);
231  }
232  } while (FindNextFileW(fh, &entry));
233  }
234  FindClose(fh);
235 #else
236  struct dirent *entry;
237  DIR *dir = sys_opendir(path);
238  if (dir) {
239  list = rz_list_new();
240  if (list) {
241  list->free = free;
242  while ((entry = readdir(dir))) {
243  rz_list_append(list, strdup(entry->d_name));
244  }
245  }
246  closedir(dir);
247  }
248 #endif
249  return list;
250 }
251 
252 RZ_API char *rz_sys_cmd_strf(const char *fmt, ...) {
253  char *ret, cmd[4096];
254  va_list ap;
255  va_start(ap, fmt);
256  vsnprintf(cmd, sizeof(cmd), fmt, ap);
257  ret = rz_sys_cmd_str(cmd, NULL, NULL);
258  va_end(ap);
259  return ret;
260 }
261 
266 #if HAVE_BACKTRACE
267  void *array[10];
268  size_t size = backtrace(array, 10);
269  eprintf("Backtrace %zd stack frames.\n", size);
270  backtrace_symbols_fd(array, size, 2);
271 #elif __APPLE__
272  void **fp = (void **)__builtin_frame_address(0);
273  void *saved_pc = __builtin_return_address(0);
274  void *saved_fp = __builtin_frame_address(1);
275  int depth = 0;
276 
277  eprintf("[%d] pc == %p fp == %p\n", depth++, saved_pc, saved_fp);
278  fp = saved_fp;
279  while (fp) {
280  saved_fp = *fp;
281  fp = saved_fp;
282  if (!*fp) {
283  break;
284  }
285  saved_pc = *(fp + 2);
286  eprintf("[%d] pc == %p fp == %p\n", depth++, saved_pc, saved_fp);
287  }
288 #else
289 #ifdef _MSC_VER
290 #pragma message("TODO: rz_sys_bt : unimplemented")
291 #else
292 #warning TODO: rz_sys_bt : unimplemented
293 #endif
294 #endif
295 }
296 
300 RZ_API int rz_sys_sleep(int secs) {
301 #if HAVE_CLOCK_NANOSLEEP && defined(CLOCK_MONOTONIC)
302  struct timespec rqtp;
303  rqtp.tv_sec = secs;
304  rqtp.tv_nsec = 0;
305  return clock_nanosleep(CLOCK_MONOTONIC, 0, &rqtp, NULL);
306 #elif __UNIX__
307  return sleep(secs);
308 #else
309  Sleep(secs * 1000); // W32
310  return 0;
311 #endif
312 }
313 
317 RZ_API int rz_sys_usleep(int usecs) {
318 #if HAVE_CLOCK_NANOSLEEP && defined(CLOCK_MONOTONIC)
319  struct timespec rqtp;
320  rqtp.tv_sec = usecs / 1000000;
321  rqtp.tv_nsec = (usecs - (rqtp.tv_sec * 1000000)) * 1000;
322  return clock_nanosleep(CLOCK_MONOTONIC, 0, &rqtp, NULL);
323 #elif __UNIX__
324 #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 2)
325  // Old versions of GNU libc return void for usleep
326  usleep(usecs);
327  return 0;
328 #else
329  return usleep(usecs);
330 #endif
331 #else
332  // w32 api uses milliseconds
333  usecs /= 1000;
334  Sleep(usecs); // W32
335  return 0;
336 #endif
337 }
338 
347 #if __UNIX__
348 #if __APPLE__ && !HAVE_ENVIRON
349  /* do nothing */
350  if (!env) {
351  rz_sys_env_init();
352  return 0;
353  }
354  char **e = env;
355  while (*e) {
356  *e++ = NULL;
357  }
358 #else
359  if (!environ) {
360  return 0;
361  }
362  while (*environ) {
363  *environ++ = NULL;
364  }
365 #endif
366  return 0;
367 #else
368 #ifdef __WINDOWS__
369  LPWCH env = GetEnvironmentStringsW();
370  LPWCH var = env;
371  while (*var) {
372  wchar_t *eq = wcschr(var, L'=');
373  if (!eq) {
374  FreeEnvironmentStringsW(env);
375  return -1;
376  }
377  const size_t len = eq - var;
378  if (!len) {
379  var += wcslen(var) + 1;
380  continue;
381  }
382  wchar_t *v = RZ_NEWS0(wchar_t, len + 1);
383  if (!v) {
384  return -1;
385  }
386  wcsncpy(v, var, len);
387  if (_wputenv_s(v, L"")) {
388  free(v);
389  break;
390  }
391  free(v);
392  var += wcslen(var) + 1;
393  }
394  FreeEnvironmentStringsW(env);
395 #else
396 #warning rz_sys_clearenv : unimplemented for this platform
397 #endif
398  return 0;
399 #endif
400 }
401 
405 RZ_API int rz_sys_setenv(const char *key, const char *value) {
406  if (!key) {
407  return 0;
408  }
409 #if __UNIX__
410  if (!value) {
411  unsetenv(key);
412  return 0;
413  }
414  return setenv(key, value, 1);
415 #elif __WINDOWS__
416  LPWSTR key_ = rz_utf8_to_utf16(key);
417  LPWSTR value_ = value ? rz_utf8_to_utf16(value) : L"";
418  bool ret = !_wputenv_s(key_, value_);
419  free(key_);
420  if (value) {
421  free(value_);
422  }
423  return ret ? 0 : -1;
424 #else
425 #warning rz_sys_setenv : unimplemented for this platform
426  return 0;
427 #endif
428 }
429 
430 #if __UNIX__
431 static char *crash_handler_cmd = NULL;
432 
433 static void signal_handler(int signum) {
434  char cmd[1024];
435  if (!crash_handler_cmd) {
436  return;
437  }
438  snprintf(cmd, sizeof(cmd) - 1, crash_handler_cmd, getpid());
441 }
442 
443 static int checkcmd(const char *c) {
444  char oc = 0;
445  for (; *c; c++) {
446  if (oc == '%') {
447  if (*c != 'd' && *c != '%') {
448  return 0;
449  }
450  }
451  oc = *c;
452  }
453  return 1;
454 }
455 #endif
456 
457 RZ_API int rz_sys_crash_handler(const char *cmd) {
458 #ifndef __WINDOWS__
459  int sig[] = { SIGINT, SIGSEGV, SIGBUS, SIGQUIT, SIGHUP, 0 };
460 
461  if (!checkcmd(cmd)) {
462  return false;
463  }
464 #if HAVE_BACKTRACE
465  void *array[1];
466  /* call this outside of the signal handler to init it safely */
467  backtrace(array, 1);
468 #endif
469 
470  free(crash_handler_cmd);
471  crash_handler_cmd = strdup(cmd);
472 
474 #else
475 #pragma message("rz_sys_crash_handler : unimplemented for this platform")
476 #endif
477  return true;
478 }
479 
483 RZ_API char *rz_sys_getenv(const char *key) {
484 #if __WINDOWS__
485  if (!key) {
486  return NULL;
487  }
488  wchar_t *val;
489  wchar_t *wkey = rz_utf8_to_utf16(key);
490  if (_wdupenv_s(&val, NULL, wkey) || !val) {
491  free(wkey);
492  return NULL;
493  }
494  free(wkey);
495  char *ret = rz_utf16_to_utf8(val);
496  free(val);
497  return ret;
498 #else
499  char *b;
500  if (!key) {
501  return NULL;
502  }
503  b = getenv(key);
504  return b ? strdup(b) : NULL;
505 #endif
506 }
507 
511 RZ_API bool rz_sys_getenv_asbool(const char *key) {
512  char *env = rz_sys_getenv(key);
513  const bool res = (env && *env == '1');
514  free(env);
515  return res;
516 }
517 
521 RZ_API char *rz_sys_getdir(void) {
522 #if __WINDOWS__
523  return _getcwd(NULL, 0);
524 #else
525  return getcwd(NULL, 0);
526 #endif
527 }
528 
532 RZ_API bool rz_sys_chdir(RZ_NONNULL const char *s) {
533  rz_return_val_if_fail(s, false);
534  char *homepath = rz_path_home_expand(s);
535  if (homepath) {
536  int ret = chdir(homepath);
537  free(homepath);
538  return ret == 0;
539  }
540  return chdir(s) == 0;
541 }
542 
546 RZ_API bool rz_sys_aslr(int val) {
547  bool ret = true;
548 #if __linux__
549  const char *rva = "/proc/sys/kernel/randomize_va_space";
550  char buf[3] = { 0 };
551  snprintf(buf, sizeof(buf), "%d\n", val != 0 ? 2 : 0);
552  int fd = rz_sys_open(rva, O_WRONLY, 0644);
553  if (fd != -1) {
554  if (write(fd, (ut8 *)buf, sizeof(buf)) != sizeof(buf)) {
555  eprintf("Failed to set RVA\n");
556  ret = false;
557  }
558  close(fd);
559  }
560 #elif __FreeBSD__ && __FreeBSD_version >= 1300000
561  size_t vlen = sizeof(val);
562  if (sysctlbyname("kern.elf32.aslr.enable", NULL, 0, &val, vlen) == -1) {
563  eprintf("Failed to set RVA 32 bits\n");
564  return false;
565  }
566 
567 #if __LP64__
568  if (sysctlbyname("kern.elf64.aslr.enable", NULL, 0, &val, vlen) == -1) {
569  eprintf("Failed to set RVA 64 bits\n");
570  ret = false;
571  }
572 #endif
573 #elif __NetBSD__
574  size_t vlen = sizeof(val);
575  if (sysctlbyname("security.pax.aslr.enabled", NULL, 0, &val, vlen) == -1) {
576  eprintf("Failed to set RVA\n");
577  ret = false;
578  }
579 #elif __DragonFly__
580  size_t vlen = sizeof(val);
581  if (sysctlbyname("vm.randomize_mmap", NULL, 0, &val, vlen) == -1) {
582  eprintf("Failed to set RVA\n");
583  ret = false;
584  }
585 #elif __DragonFly__
586 #endif
587  return ret;
588 }
589 
590 RZ_API int rz_sys_cmd_str_full(const char *cmd, const char *input, char **output, int *len, char **sterr) {
591  int argc;
592  char **argv = rz_str_argv(cmd, &argc);
593  if (!argv) {
594  return false;
595  }
596  RzSubprocessOpt opts = {
597  .file = argv[0],
598  .args = (const char **)&argv[1],
599  .args_size = argc - 1,
600  .envvars = NULL,
601  .envvals = NULL,
602  .env_size = 0,
605  .stderr_pipe = sterr ? RZ_SUBPROCESS_PIPE_CREATE : RZ_SUBPROCESS_PIPE_NONE,
606  };
607 
608  if (!rz_subprocess_init()) {
609  RZ_LOG_ERROR("Failed to initialize subprocess\n");
610  return false;
611  }
612  RzSubprocess *subprocess = rz_subprocess_start_opt(&opts);
613  if (!subprocess) {
615  return false;
616  }
617  if (input) {
618  rz_subprocess_stdin_write(subprocess, (ut8 *)input, strlen(input) + 1);
619  }
620  rz_subprocess_wait(subprocess, UT64_MAX);
621  if (output) {
622  *output = (char *)rz_subprocess_out(subprocess, len);
623  }
624  if (sterr) {
625  *sterr = (char *)rz_subprocess_err(subprocess, NULL);
626  }
627  rz_subprocess_free(subprocess);
630  return true;
631 }
632 
633 RZ_API int rz_sys_cmdf(const char *fmt, ...) {
634  int ret;
635  char cmd[4096];
636  va_list ap;
637  va_start(ap, fmt);
638  vsnprintf(cmd, sizeof(cmd), fmt, ap);
639  ret = rz_sys_system(cmd);
640  va_end(ap);
641  return ret;
642 }
643 
644 RZ_API int rz_sys_cmdbg(const char *str) {
645 #if __UNIX__
646  int ret, pid = rz_sys_fork();
647  if (pid == -1) {
648  return -1;
649  }
650  if (pid) {
651  return pid;
652  }
653  char *bin_sh = rz_file_binsh();
654  ret = rz_sys_execl(bin_sh, "sh", "-c", str, (const char *)NULL);
655  free(bin_sh);
656  eprintf("{exit: %d, pid: %d, cmd: \"%s\"}", ret, pid, str);
657  exit(0);
658  return -1;
659 #else
660 #ifdef _MSC_VER
661 #pragma message("rz_sys_cmdbg is not implemented for this platform")
662 #else
663 #warning rz_sys_cmdbg is not implemented for this platform
664 #endif
665  return -1;
666 #endif
667 }
668 
669 RZ_API char *rz_sys_cmd_str(const char *cmd, const char *input, int *len) {
670  char *output = NULL;
672  return output;
673  }
674  free(output);
675  return NULL;
676 }
677 
678 RZ_API bool rz_sys_mkdir(const char *dir) {
679  bool ret;
680 
681 #if __WINDOWS__
682  wchar_t *dir_utf16 = rz_utf8_to_utf16(dir);
683  ret = _wmkdir(dir_utf16) != -1;
684  free(dir_utf16);
685 #else
686  ret = mkdir(dir, 0755) != -1;
687 #endif
688  return ret;
689 }
690 
691 RZ_API bool rz_sys_mkdirp(const char *dir) {
692  bool ret = true;
693  char slash = RZ_SYS_DIR[0];
694  char *path = strdup(dir), *ptr = path;
695  if (!path) {
696  eprintf("rz_sys_mkdirp: Unable to allocate memory\n");
697  return false;
698  }
699  if (*ptr == slash) {
700  ptr++;
701  }
702 #if __WINDOWS__
703  {
704  char *p = strstr(ptr, ":\\");
705  if (p) {
706  ptr = p + 3;
707  }
708  }
709 #endif
710  for (;;) {
711  // find next slash
712  for (; *ptr; ptr++) {
713  if (*ptr == '/' || *ptr == '\\') {
714  slash = *ptr;
715  break;
716  }
717  }
718  if (!*ptr) {
719  break;
720  }
721  *ptr = 0;
723  eprintf("rz_sys_mkdirp: fail '%s' of '%s'\n", path, dir);
724  free(path);
725  return false;
726  }
727  *ptr = slash;
728  ptr++;
729  }
731  ret = false;
732  }
733  free(path);
734  return ret;
735 }
736 
737 RZ_API void rz_sys_perror_str(const char *fun) {
738 #if __UNIX__
739 #pragma push_macro("perror")
740 #undef perror
741  perror(fun);
742 #pragma pop_macro("perror")
743 #elif __WINDOWS__
744  LPTSTR lpMsgBuf;
745  DWORD dw = GetLastError();
746 
747  if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
748  FORMAT_MESSAGE_FROM_SYSTEM |
749  FORMAT_MESSAGE_IGNORE_INSERTS,
750  NULL,
751  dw,
752  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
753  (LPTSTR)&lpMsgBuf,
754  0, NULL)) {
755  char *err = rz_sys_conv_win_to_utf8(lpMsgBuf);
756  if (err) {
757  eprintf("%s: (%#lx) %s%s", fun, dw, err,
758  rz_str_endswith(err, "\n") ? "" : "\n");
759  free(err);
760  }
761  LocalFree(lpMsgBuf);
762  } else {
763  eprintf("%s\n", fun);
764  }
765 #endif
766 }
767 
768 RZ_API bool rz_sys_arch_match(const char *archstr, const char *arch) {
769  char *ptr;
770  if (!archstr || !arch || !*archstr || !*arch) {
771  return true;
772  }
773  if (!strcmp(archstr, "*") || !strcmp(archstr, "any")) {
774  return true;
775  }
776  if (!strcmp(archstr, arch)) {
777  return true;
778  }
779  if ((ptr = strstr(archstr, arch))) {
780  char p = ptr[strlen(arch)];
781  if (!p || p == ',') {
782  return true;
783  }
784  }
785  return false;
786 }
787 
788 RZ_API int rz_sys_arch_id(const char *arch) {
789  int i;
790  for (i = 0; arch_bit_array[i].name; i++) {
791  if (!strcmp(arch, arch_bit_array[i].name)) {
792  return arch_bit_array[i].bit;
793  }
794  }
795  return 0;
796 }
797 
798 RZ_API const char *rz_sys_arch_str(int arch) {
799  int i;
800  for (i = 0; arch_bit_array[i].name; i++) {
801  if (arch & arch_bit_array[i].bit) {
802  return arch_bit_array[i].name;
803  }
804  }
805  return "none";
806 }
807 
808 #define USE_FORK 0
809 RZ_API int rz_sys_run(const ut8 *buf, int len) {
810  const int sz = 4096;
811  int pdelta, ret, (*cb)();
812 #if USE_FORK
813  int st, pid;
814 #endif
815  // TODO: define RZ_SYS_ALIGN_FORWARD in rz_util.h
816  ut8 *ptr, *p = malloc((sz + len) << 1);
817  ptr = p;
818  pdelta = ((size_t)(p)) & (4096 - 1);
819  if (pdelta) {
820  ptr += (4096 - pdelta);
821  }
822  if (!ptr || !buf) {
823  eprintf("rz_sys_run: Cannot run empty buffer\n");
824  free(p);
825  return false;
826  }
827  memcpy(ptr, buf, len);
828  rz_mem_protect(ptr, sz, "rx");
829  // rz_mem_protect (ptr, sz, "rwx"); // try, ignore if fail
830  cb = (int (*)())ptr;
831 #if USE_FORK
832 #if __UNIX__
833  pid = rz_sys_fork();
834 #else
835  pid = -1;
836 #endif
837  if (pid < 0) {
838  return cb();
839  }
840  if (!pid) {
841  ret = cb();
842  exit(ret);
843  return ret;
844  }
845  st = 0;
846  waitpid(pid, &st, 0);
847  if (WIFSIGNALED(st)) {
848  int num = WTERMSIG(st);
849  eprintf("Got signal %d\n", num);
850  ret = num;
851  } else {
852  ret = WEXITSTATUS(st);
853  }
854 #else
855  ret = (*cb)();
856 #endif
857  free(p);
858  return ret;
859 }
860 
861 RZ_API int rz_sys_run_rop(const ut8 *buf, int len) {
862 #if USE_FORK
863  int st;
864 #endif
865  // TODO: define RZ_SYS_ALIGN_FORWARD in rz_util.h
866  ut8 *bufptr = malloc(len);
867  if (!bufptr) {
868  eprintf("rz_sys_run_rop: Cannot allocate buffer\n");
869  return false;
870  }
871 
872  if (!buf) {
873  eprintf("rz_sys_run_rop: Cannot execute empty rop chain\n");
874  free(bufptr);
875  return false;
876  }
877  memcpy(bufptr, buf, len);
878 #if USE_FORK
879 #if __UNIX__
880  pid_t pid = rz_sys_fork();
881 #else
882  pid = -1;
883 #endif
884  if (pid < 0) {
886  } else {
888  exit(0);
889  return 0;
890  }
891  st = 0;
892  if (waitpid(pid, &st, 0) == -1) {
893  eprintf("rz_sys_run_rop: waitpid failed\n");
894  free(bufptr);
895  return -1;
896  }
897  if (WIFSIGNALED(st)) {
898  int num = WTERMSIG(st);
899  eprintf("Got signal %d\n", num);
900  ret = num;
901  } else {
902  ret = WEXITSTATUS(st);
903  }
904 #else
906 #endif
907  free(bufptr);
908  return 0;
909 }
910 
911 RZ_API bool rz_is_heap(void *p) {
912  void *q = malloc(8);
913  ut64 mask = UT64_MAX;
914  mask >>= 16;
915  mask <<= 16;
916  free(q);
917  return (((ut64)(size_t)p) == mask);
918 }
919 
921 #if __WINDOWS__
922  // TODO: add maximum path length support
923  HANDLE processHandle;
924  const DWORD maxlength = MAX_PATH;
925  WCHAR filename[MAX_PATH];
926  char *result = NULL;
927 
928  processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
929  if (!processHandle) {
930  eprintf("rz_sys_pid_to_path: Cannot open process.\n");
931  return NULL;
932  }
933  DWORD length = GetModuleFileNameExW(processHandle, NULL, filename, maxlength);
934  if (length == 0) {
935  // Upon failure fallback to GetProcessImageFileName
936  length = GetProcessImageFileNameW(processHandle, filename, maxlength);
937  CloseHandle(processHandle);
938  if (length == 0) {
939  eprintf("rz_sys_pid_to_path: Error calling GetProcessImageFileName\n");
940  return NULL;
941  }
942  // Convert NT path to win32 path
943  char *name = rz_utf16_to_utf8(filename);
944  if (!name) {
945  eprintf("rz_sys_pid_to_path: Error converting to utf8\n");
946  return NULL;
947  }
948  char *tmp = strchr(name + 1, '\\');
949  if (!tmp) {
950  free(name);
951  eprintf("rz_sys_pid_to_path: Malformed NT path\n");
952  return NULL;
953  }
954  tmp = strchr(tmp + 1, '\\');
955  if (!tmp) {
956  free(name);
957  eprintf("rz_sys_pid_to_path: Malformed NT path\n");
958  return NULL;
959  }
960  length = tmp - name;
961  tmp = malloc(length + 1);
962  if (!tmp) {
963  free(name);
964  eprintf("rz_sys_pid_to_path: Error allocating memory\n");
965  return NULL;
966  }
967  strncpy(tmp, name, length);
968  tmp[length] = '\0';
969  WCHAR device[MAX_PATH];
970  for (WCHAR drv[] = L"A:"; drv[0] <= L'Z'; drv[0]++) {
971  if (QueryDosDeviceW(drv, device, maxlength) > 0) {
972  char *dvc = rz_utf16_to_utf8(device);
973  if (!dvc) {
974  free(name);
975  free(tmp);
976  eprintf("rz_sys_pid_to_path: Error converting to utf8\n");
977  return NULL;
978  }
979  if (!strcmp(tmp, dvc)) {
980  free(tmp);
981  free(dvc);
982  char *d = rz_utf16_to_utf8(drv);
983  if (!d) {
984  free(name);
985  eprintf("rz_sys_pid_to_path: Error converting to utf8\n");
986  return NULL;
987  }
988  tmp = rz_str_newf("%s%s", d, &name[length]);
989  free(d);
990  if (!tmp) {
991  free(name);
992  eprintf("rz_sys_pid_to_path: Error calling rz_str_newf\n");
993  return NULL;
994  }
995  result = strdup(tmp);
996  break;
997  }
998  free(dvc);
999  }
1000  }
1001  free(name);
1002  free(tmp);
1003  } else {
1004  CloseHandle(processHandle);
1005  result = rz_utf16_to_utf8(filename);
1006  }
1007  return result;
1008 #elif __APPLE__
1009  char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
1010  pathbuf[0] = 0;
1011  int ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf));
1012  if (ret <= 0) {
1013  return NULL;
1014  }
1015  return strdup(pathbuf);
1016 #else
1017  int ret;
1018 #if __FreeBSD__ || __DragonFly__
1019  char pathbuf[PATH_MAX];
1020  size_t pathbufl = sizeof(pathbuf);
1021  int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid };
1022  ret = sysctl(mib, 4, pathbuf, &pathbufl, NULL, 0);
1023  if (ret != 0) {
1024  return NULL;
1025  }
1026 #elif __OpenBSD__
1027  // Taken from https://stackoverflow.com/questions/31494901/how-to-get-the-executable-path-on-openbsd
1028  char pathbuf[PATH_MAX];
1029  int mib[4] = { CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ARGV };
1030  size_t len;
1031 
1032  pathbuf[0] = '\0';
1033  ret = sysctl(mib, 4, NULL, &len, NULL, 0);
1034  if (ret < 0) {
1035  return NULL;
1036  }
1037  char **argv = malloc(len);
1038  ret = sysctl(mib, 4, argv, &len, NULL, 0);
1039  if (ret < 0) {
1040  free(argv);
1041  return NULL;
1042  }
1043  const char *comm = argv[0];
1044  int ok = 0;
1045  if (*comm == '/' || *comm == '.') {
1046  if (!realpath(comm, pathbuf)) {
1047  free(argv);
1048  return NULL;
1049  }
1050  } else {
1051  char *sp;
1052  char *xpath = strdup(getenv("PATH"));
1053  char *path = strtok_r(xpath, ":", &sp);
1054  struct stat st;
1055 
1056  if (!xpath) {
1057  free(argv);
1058  return NULL;
1059  }
1060 
1061  while (path) {
1062  snprintf(pathbuf, PATH_MAX, "%s/%s", path, comm);
1063  if (!stat(pathbuf, &st) && (st.st_mode & S_IXUSR)) {
1064  ok = 1;
1065  break;
1066  }
1067  path = strtok_r(NULL, ":", &sp);
1068  }
1069  free(xpath);
1070  }
1071 
1072  if (ok) {
1073  char *p = strrchr(pathbuf, '/');
1074  if (p) {
1075  *p = '\0';
1076  }
1077  }
1078  free(argv);
1079 #elif __HAIKU__
1080  char pathbuf[MAXPATHLEN];
1081  int32_t group = 0;
1082  image_info ii;
1083 
1084  while (get_next_image_info((team_id)pid, &group, &ii) == B_OK) {
1085  if (ii.type == B_APP_IMAGE) {
1086  break;
1087  }
1088  }
1089 
1090  if (ii.type == B_APP_IMAGE) {
1091  rz_str_ncpy(pathbuf, ii.name, MAXPATHLEN);
1092  } else {
1093  pathbuf[0] = '\0';
1094  }
1095 #else
1096  char buf[128], pathbuf[1024];
1097  snprintf(buf, sizeof(buf), "/proc/%d/exe", pid);
1098  ret = readlink(buf, pathbuf, sizeof(pathbuf) - 1);
1099  if (ret < 1) {
1100  return NULL;
1101  }
1102  pathbuf[ret] = 0;
1103 #endif
1104  return strdup(pathbuf);
1105 #endif
1106 }
1107 
1109  char **envp = rz_sys_get_environ();
1110  if (envp) {
1111  env = envp;
1112  }
1113 }
1114 
1116 #if __APPLE__ && !HAVE_ENVIRON
1117  env = *_NSGetEnviron();
1118 #elif HAVE_ENVIRON
1119  env = environ;
1120 #endif
1121  // return environ if available??
1122  if (!env) {
1123  env = rz_lib_dl_sym(NULL, "environ");
1124  }
1125  return env;
1126 }
1127 
1129  env = e;
1130 #if __APPLE__ && !HAVE_ENVIRON
1131  *_NSGetEnviron() = e;
1132 #elif __WINDOWS__
1133  char **oe = e;
1134  rz_sys_clearenv();
1135  while (*e) {
1136  char *var = *e;
1137  char *val = strchr(var, '=');
1138  wchar_t *val_utf16 = NULL;
1139  if (*val) {
1140  *val++ = '\0';
1141  }
1142  rz_sys_setenv(var, val);
1143  free(*e);
1144  e++;
1145  }
1146  free(oe);
1147  env = environ;
1148 #elif HAVE_ENVIRON
1149  environ = e;
1150 #endif
1151 }
1152 
1153 RZ_API char *rz_sys_whoami(char *buf) {
1154  char _buf[32];
1155  int pid = getpid();
1156  int hasbuf = (buf) ? 1 : 0;
1157  if (!hasbuf) {
1158  buf = _buf;
1159  }
1160  sprintf(buf, "pid%d", pid);
1161  return hasbuf ? buf : strdup(buf);
1162 }
1163 
1165 #if __UNIX__
1166  return getpid();
1167 #elif __WINDOWS__
1168  return GetCurrentProcessId();
1169 #else
1170 #warning rz_sys_getpid not implemented for this platform
1171  return -1;
1172 #endif
1173 }
1174 
1176 #if __UNIX__
1177  struct utsname un = { { 0 } };
1178  if (uname(&un) != -1) {
1180  if (si) {
1181  si->sysname = strdup(un.sysname);
1182  si->nodename = strdup(un.nodename);
1183  si->release = strdup(un.release);
1184  si->version = strdup(un.version);
1185  si->machine = strdup(un.machine);
1186  return si;
1187  }
1188  }
1189 #elif __WINDOWS__
1190  HKEY key;
1191  DWORD type;
1192  DWORD size;
1193  DWORD major;
1194  DWORD minor;
1195  char tmp[256] = { 0 };
1197  if (!si) {
1198  return NULL;
1199  }
1200 
1201  if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0,
1202  KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
1203  rz_sys_perror("rz_sys_info/RegOpenKeyExA");
1205  return NULL;
1206  }
1207 
1208  size = sizeof(tmp);
1209  if (RegQueryValueExA(key, "ProductName", NULL, &type,
1210  (LPBYTE)&tmp, &size) != ERROR_SUCCESS ||
1211  type != REG_SZ) {
1212  goto beach;
1213  }
1214  si->sysname = strdup(tmp);
1215 
1216  size = sizeof(major);
1217  if (RegQueryValueExA(key, "CurrentMajorVersionNumber", NULL, &type,
1218  (LPBYTE)&major, &size) != ERROR_SUCCESS ||
1219  type != REG_DWORD) {
1220  goto beach;
1221  }
1222  size = sizeof(minor);
1223  if (RegQueryValueExA(key, "CurrentMinorVersionNumber", NULL, &type,
1224  (LPBYTE)&minor, &size) != ERROR_SUCCESS ||
1225  type != REG_DWORD) {
1226  goto beach;
1227  }
1228 
1229  size = sizeof(tmp);
1230  if (RegQueryValueExA(key, "CurrentBuild", NULL, &type,
1231  (LPBYTE)&tmp, &size) != ERROR_SUCCESS ||
1232  type != REG_SZ) {
1233  goto beach;
1234  }
1235  si->version = rz_str_newf("%lu.%lu.%s", major, minor, tmp);
1236 
1237  size = sizeof(tmp);
1238  if (RegQueryValueExA(key, "ReleaseId", NULL, &type,
1239  (LPBYTE)tmp, &size) != ERROR_SUCCESS ||
1240  type != REG_SZ) {
1241  goto beach;
1242  }
1243  si->release = strdup(tmp);
1244 beach:
1245  RegCloseKey(key);
1246  return si;
1247 #endif
1248  return NULL;
1249 }
1250 
1252  free(si->sysname);
1253  free(si->nodename);
1254  free(si->release);
1255  free(si->version);
1256  free(si->machine);
1257  free(si);
1258 }
1259 
1260 #if __UNIX__ && HAVE_PIPE2
1261 #include <fcntl.h>
1262 #include <unistd.h>
1263 
1264 RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec) {
1265  return pipe2(pipefd, close_on_exec ? O_CLOEXEC : 0);
1266 }
1267 
1268 RZ_API int rz_sys_pipe_close(int fd) {
1269  return close(fd);
1270 }
1271 #elif __UNIX__ && HAVE_PIPE && defined(O_CLOEXEC)
1272 // Use this lock to wraps pipe, close, exec*, system to ensure all pipe file
1273 // descriptors are either created AND set as CLOEXEC or not created at all.
1274 static RzThreadLock *sys_pipe_mutex;
1275 static bool is_child = false;
1276 
1277 #ifdef RZ_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
1278 #pragma RZ_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(sys_pipe_constructor)
1279 #endif
1280 RZ_DEFINE_CONSTRUCTOR(sys_pipe_constructor)
1281 static void sys_pipe_constructor(void) {
1282  sys_pipe_mutex = rz_th_lock_new(true);
1283 }
1284 
1285 #ifdef RZ_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
1286 #pragma RZ_DEFINE_DESTRUCTOR_PRAGMA_ARGS(sys_pipe_destructor)
1287 #endif
1288 RZ_DEFINE_DESTRUCTOR(sys_pipe_destructor)
1289 static void sys_pipe_destructor(void) {
1290  rz_th_lock_free(sys_pipe_mutex);
1291 }
1292 
1293 static bool set_close_on_exec(int fd) {
1294  int flags = fcntl(fd, F_GETFD);
1295  if (flags == -1) {
1296  return false;
1297  }
1298  flags |= FD_CLOEXEC;
1299  return fcntl(fd, F_SETFD, flags) != -1;
1300 }
1301 
1302 static void parent_lock_enter(void) {
1303  if (!is_child) {
1304  rz_th_lock_enter(sys_pipe_mutex);
1305  }
1306 }
1307 
1308 static void parent_lock_leave(void) {
1309  if (!is_child) {
1310  rz_th_lock_leave(sys_pipe_mutex);
1311  }
1312 }
1313 
1321 RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec) {
1322  int res = -1;
1323  parent_lock_enter();
1324  if ((res = pipe(pipefd)) == -1) {
1325  perror("pipe");
1326  goto err;
1327  }
1328  if (close_on_exec && (!set_close_on_exec(pipefd[0]) || !set_close_on_exec(pipefd[1]))) {
1329  perror("close-on-exec");
1330  close(pipefd[0]);
1331  close(pipefd[1]);
1332  goto err;
1333  }
1334 err:
1335  parent_lock_leave();
1336  return res;
1337 }
1338 
1342 RZ_API int rz_sys_pipe_close(int fd) {
1343  return close(fd);
1344 }
1345 
1346 #elif __UNIX__ && HAVE_PIPE
1347 #include <ht_uu.h>
1348 static HtUU *fd2close;
1349 // Use this lock to wraps pipe, close, exec*, system to ensure all pipe file
1350 // descriptors are either created AND added to fd2close or not created at all.
1351 static RzThreadLock *sys_pipe_mutex;
1352 static bool is_child = false;
1353 
1354 #ifdef RZ_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
1355 #pragma RZ_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(sys_pipe_constructor)
1356 #endif
1357 RZ_DEFINE_CONSTRUCTOR(sys_pipe_constructor)
1358 static void sys_pipe_constructor(void) {
1359  sys_pipe_mutex = rz_th_lock_new(false);
1360  fd2close = ht_uu_new0();
1361 }
1362 
1363 #ifdef RZ_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
1364 #pragma RZ_DEFINE_DESTRUCTOR_PRAGMA_ARGS(sys_pipe_destructor)
1365 #endif
1366 RZ_DEFINE_DESTRUCTOR(sys_pipe_destructor)
1367 static void sys_pipe_destructor(void) {
1368  ht_uu_free(fd2close);
1369  rz_th_lock_free(sys_pipe_mutex);
1370 }
1371 
1372 static void parent_lock_enter(void) {
1373  if (!is_child) {
1374  rz_th_lock_enter(sys_pipe_mutex);
1375  }
1376 }
1377 
1378 static void parent_lock_leave(void) {
1379  if (!is_child) {
1380  rz_th_lock_leave(sys_pipe_mutex);
1381  }
1382 }
1383 
1384 static bool set_close_on_exec(int fd, bool close_on_exec) {
1385  bool res = ht_uu_insert(fd2close, fd, close_on_exec);
1386  rz_warn_if_fail(res);
1387  return res;
1388 }
1389 
1390 static bool close_on_exec_fd_cb(void *user, const ut64 key, const ut64 val) {
1391  bool close_on_exec = (bool)val;
1392  if (close_on_exec) {
1393  close((int)key);
1394  }
1395  return true;
1396 }
1397 
1398 static void close_fds(void) {
1399  ht_uu_foreach(fd2close, close_on_exec_fd_cb, NULL);
1400 }
1401 
1409 RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec) {
1410  int res = -1;
1411  parent_lock_enter();
1412  if ((res = pipe(pipefd)) == -1) {
1413  perror("pipe");
1414  goto err;
1415  }
1416  if (!set_close_on_exec(pipefd[0], close_on_exec) || !set_close_on_exec(pipefd[1], close_on_exec)) {
1417  perror("close-on-exec");
1418  close(pipefd[0]);
1419  close(pipefd[1]);
1420  goto err;
1421  }
1422 err:
1423  parent_lock_leave();
1424  return res;
1425 }
1426 
1433 RZ_API int rz_sys_pipe_close(int fd) {
1434  parent_lock_enter();
1435  bool deleted = ht_uu_delete(fd2close, fd);
1436  rz_warn_if_fail(deleted);
1437  int res = close(fd);
1438  parent_lock_leave();
1439  return res;
1440 }
1441 #elif HAVE_PIPE
1442 RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec) {
1443  return pipe(pipefd);
1444 }
1445 
1446 RZ_API int rz_sys_pipe_close(int fd) {
1447  return close(fd);
1448 }
1449 #elif __WINDOWS__
1450 RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec) {
1451  return _pipe(pipefd, 0x1000, O_TEXT);
1452 }
1453 
1454 RZ_API int rz_sys_pipe_close(int fd) {
1455  return _close(fd);
1456 }
1457 #else
1458 RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec) {
1459  return -1;
1460 }
1461 
1463  return -1;
1464 }
1465 #endif
1466 
1467 #if __UNIX__ && HAVE_EXECV && HAVE_PIPE && defined(O_CLOEXEC) && !HAVE_PIPE2
1468 RZ_API int rz_sys_execv(const char *pathname, char *const argv[]) {
1469  parent_lock_enter();
1470  int res = execv(pathname, argv);
1471  parent_lock_leave();
1472  return res;
1473 }
1474 #elif __UNIX__ && HAVE_EXECV && HAVE_PIPE && !HAVE_PIPE2
1475 RZ_API int rz_sys_execv(const char *pathname, char *const argv[]) {
1476  parent_lock_enter();
1477  close_fds();
1478  int res = execv(pathname, argv);
1479  parent_lock_leave();
1480  return res;
1481 }
1482 #elif !HAVE_EXECV
1483 RZ_API int rz_sys_execv(const char *pathname, char *const argv[]) {
1484  return -1;
1485 }
1486 #endif
1487 
1488 #if __UNIX__ && HAVE_EXECVE && HAVE_PIPE && defined(O_CLOEXEC) && !HAVE_PIPE2
1489 RZ_API int rz_sys_execve(const char *pathname, char *const argv[], char *const envp[]) {
1490  parent_lock_enter();
1491  int res = execve(pathname, argv, envp);
1492  parent_lock_leave();
1493  return res;
1494 }
1495 #elif __UNIX__ && HAVE_EXECVE && HAVE_PIPE && !HAVE_PIPE2
1496 RZ_API int rz_sys_execve(const char *pathname, char *const argv[], char *const envp[]) {
1497  parent_lock_enter();
1498  close_fds();
1499  int res = execve(pathname, argv, envp);
1500  parent_lock_leave();
1501  return res;
1502 }
1503 #elif !HAVE_EXECVE
1504 RZ_API int rz_sys_execve(const char *pathname, char *const argv[], char *const envp[]) {
1505  return -1;
1506 }
1507 #endif
1508 
1509 #if __UNIX__ && HAVE_EXECVP && HAVE_PIPE && defined(O_CLOEXEC) && !HAVE_PIPE2
1510 RZ_API int rz_sys_execvp(const char *file, char *const argv[]) {
1511  parent_lock_enter();
1512  int res = execvp(file, argv);
1513  parent_lock_leave();
1514  return res;
1515 }
1516 #elif __UNIX__ && HAVE_EXECVP && HAVE_PIPE && !HAVE_PIPE2
1517 RZ_API int rz_sys_execvp(const char *file, char *const argv[]) {
1518  parent_lock_enter();
1519  close_fds();
1520  int res = execvp(file, argv);
1521  parent_lock_leave();
1522  return res;
1523 }
1524 #elif !HAVE_EXECVP
1525 RZ_API int rz_sys_execvp(const char *file, char *const argv[]) {
1526  return -1;
1527 }
1528 #endif
1529 
1530 #if __UNIX__ && HAVE_EXECL && HAVE_PIPE && defined(O_CLOEXEC) && !HAVE_PIPE2
1531 RZ_API int rz_sys_execl(const char *pathname, const char *arg, ...) {
1532  va_list count_args, args;
1533  va_start(args, arg);
1534  va_copy(count_args, args);
1535  size_t i, argc = 0;
1536  while (va_arg(count_args, char *) != NULL) {
1537  argc++;
1538  }
1539  va_end(count_args);
1540  char **argv = RZ_NEWS0(char *, argc + 2);
1541  argv[0] = strdup(pathname);
1542  for (i = 1; i <= argc; i++) {
1543  argv[i] = va_arg(args, char *);
1544  }
1545  va_end(args);
1546  parent_lock_enter();
1547  int res = execv(pathname, argv);
1548  parent_lock_leave();
1549  return res;
1550 }
1551 #elif __UNIX__ && HAVE_EXECL && HAVE_PIPE && !HAVE_PIPE2
1552 RZ_API int rz_sys_execl(const char *pathname, const char *arg, ...) {
1553  va_list count_args, args;
1554  va_start(args, arg);
1555  va_copy(count_args, args);
1556  size_t i, argc = 0;
1557  while (va_arg(count_args, char *) != NULL) {
1558  argc++;
1559  }
1560  va_end(count_args);
1561  char **argv = RZ_NEWS0(char *, argc + 2);
1562  argv[0] = strdup(pathname);
1563  for (i = 1; i <= argc; i++) {
1564  argv[i] = va_arg(args, char *);
1565  }
1566  va_end(args);
1567 
1568  parent_lock_enter();
1569  close_fds();
1570  int res = execv(pathname, argv);
1571  parent_lock_leave();
1572  return res;
1573 }
1574 #elif !HAVE_EXECL
1575 RZ_API int rz_sys_execl(const char *pathname, const char *arg, ...) {
1576  return -1;
1577 }
1578 #endif
1579 
1580 #if __UNIX__ && HAVE_SYSTEM && HAVE_PIPE && defined(O_CLOEXEC) && !HAVE_PIPE2
1581 RZ_API int rz_sys_system(const char *command) {
1582  parent_lock_enter();
1583  int res = system(command);
1584  parent_lock_leave();
1585  return res;
1586 }
1587 #elif __UNIX__ && HAVE_SYSTEM && HAVE_PIPE && !HAVE_PIPE2
1588 RZ_API int rz_sys_system(const char *command) {
1589  parent_lock_enter();
1590  close_fds();
1591  int res = system(command);
1592  parent_lock_leave();
1593  return res;
1594 }
1595 #elif !HAVE_SYSTEM && APPLE_SDK_IPHONEOS
1596 #include <spawn.h>
1597 RZ_API int rz_sys_system(const char *command) {
1598  int argc;
1599  char *cmd = strdup(command);
1600  char **argv = rz_str_argv(cmd, &argc);
1601  if (argv) {
1602  char *argv0 = rz_file_path(argv[0]);
1603  pid_t pid = 0;
1604  int r = posix_spawn(&pid, argv0, NULL, NULL, argv, NULL);
1605  int status;
1606  int s = waitpid(pid, &status, 0);
1607  return WEXITSTATUS(s);
1608  }
1609 }
1610 #elif !HAVE_SYSTEM && HAVE_FORK
1611 #include <spawn.h>
1612 RZ_API int rz_sys_system(const char *command) {
1613  if (!strchr(command, '|')) {
1614  char **argv, *cmd = strdup(command);
1615  int rc, pid, argc;
1616  char *isbg = strchr(cmd, '&');
1617  // XXX this is hacky
1618  if (isbg) {
1619  *isbg = 0;
1620  }
1621  argv = rz_str_argv(cmd, &argc);
1622  if (argv) {
1623  char *argv0 = rz_file_path(argv[0]);
1624  if (!argv0) {
1625  eprintf("Cannot find '%s'\n", argv[0]);
1626  return -1;
1627  }
1628  pid = 0;
1629  posix_spawn(&pid, argv0, NULL, NULL, argv, NULL);
1630  if (isbg) {
1631  // XXX. wait for children
1632  rc = 0;
1633  } else {
1634  rc = waitpid(pid, NULL, 0);
1635  }
1637  free(argv0);
1638  return rc;
1639  }
1640  eprintf("Error parsing command arguments\n");
1641  return -1;
1642  }
1643  int child = rz_sys_fork();
1644  if (child == -1) {
1645  return -1;
1646  }
1647  if (child) {
1648  return waitpid(child, NULL, 0);
1649  }
1650  char *bin_sh = rz_file_binsh();
1651  if (rz_sys_execl(bin_sh, "sh", "-c", command, (const char *)NULL) == -1) {
1652  perror("execl");
1653  }
1654  free(bin_sh);
1655  exit(1);
1656 }
1657 #elif !HAVE_SYSTEM
1658 RZ_API int rz_sys_system(const char *command) {
1659  return -1;
1660 }
1661 #endif
1662 
1663 #if HAVE_FORK
1664 RZ_API int rz_sys_fork(void) {
1665 #if __UNIX__ && HAVE_PIPE && !HAVE_PIPE2
1666  parent_lock_enter();
1667 #endif
1668  pid_t child = fork();
1669 #if __UNIX__ && HAVE_PIPE && !HAVE_PIPE2
1670  if (child == 0) {
1671  is_child = true;
1672  } else if (child > 0) {
1673  parent_lock_leave();
1674  }
1675 #endif
1676  return child;
1677 }
1678 #else
1679 RZ_API int rz_sys_fork(void) {
1680  return -1;
1681 }
1682 #endif
1683 
1685 #ifdef _MSC_VER
1686  return _chsize_s(fd, length);
1687 #else
1688  return ftruncate(fd, (off_t)length);
1689 #endif
1690 }
1691 
1692 RZ_API int rz_sys_truncate(const char *file, int sz) {
1693 #if __WINDOWS__
1694  int fd = rz_sys_open(file, O_RDWR, 0644);
1695  if (fd == -1) {
1696  return false;
1697  }
1698  int r = rz_sys_truncate_fd(fd, sz);
1699  if (r != 0) {
1700  eprintf("Could not resize '%s' file\n", file);
1701  close(fd);
1702  return false;
1703  }
1704  close(fd);
1705  return true;
1706 #else
1707  return truncate(file, sz) == 0;
1708 #endif
1709 }
1710 
1718 RZ_API int rz_sys_open_perms(int rizin_perms) {
1719  int res = 0;
1720  if ((rizin_perms & RZ_PERM_R) && (rizin_perms & RZ_PERM_W)) {
1721  res |= O_RDWR;
1722  // NOTE: O_CREAT is added here because Rizin for now assumes Write means
1723  // ability to create a file as well.
1724  res |= O_CREAT;
1725  } else if (rizin_perms & RZ_PERM_R) {
1726  res |= O_RDONLY;
1727  } else if (rizin_perms & RZ_PERM_W) {
1728  res |= O_WRONLY;
1729  // NOTE: O_CREAT is added here because Rizin for now assumes Write means
1730  // ability to create a file as well.
1731  res |= O_CREAT;
1732  }
1733  if (rizin_perms & RZ_PERM_CREAT) {
1734  res |= O_CREAT;
1735  }
1736  return res;
1737 }
1738 
1739 /* perm <-> mode */
1740 RZ_API int rz_sys_open(const char *path, int perm, int mode) {
1742  char *epath = rz_path_home_expand(path);
1743  int ret = -1;
1744 #if __WINDOWS__
1745  if (!strcmp(path, "/dev/null")) {
1746  path = "NUL";
1747  }
1748  {
1749  DWORD flags = 0;
1750  DWORD sharing = FILE_SHARE_READ;
1751  if (perm & O_RANDOM) {
1752  flags = FILE_FLAG_RANDOM_ACCESS;
1753  } else if (perm & O_SEQUENTIAL) {
1754  flags = FILE_FLAG_SEQUENTIAL_SCAN;
1755  }
1756  if (perm & O_TEMPORARY) {
1757  flags |= FILE_FLAG_DELETE_ON_CLOSE | FILE_ATTRIBUTE_TEMPORARY;
1758  sharing |= FILE_SHARE_DELETE;
1759  } else if (perm & _O_SHORT_LIVED) {
1760  flags |= FILE_ATTRIBUTE_TEMPORARY;
1761  } else {
1762  flags |= FILE_ATTRIBUTE_NORMAL;
1763  }
1764  DWORD creation = 0;
1765  bool read_only = false;
1766  if (perm & O_CREAT) {
1767  if (perm & O_EXCL) {
1768  creation = CREATE_NEW;
1769  } else {
1770  creation = OPEN_ALWAYS;
1771  }
1772  if (mode & S_IREAD && !(mode & S_IWRITE)) {
1773  flags = FILE_ATTRIBUTE_READONLY;
1774  read_only = true;
1775  }
1776  } else if (perm & O_TRUNC) {
1777  creation = TRUNCATE_EXISTING;
1778  }
1779  if (!creation || !strcasecmp("NUL", path)) {
1780  creation = OPEN_EXISTING;
1781  }
1782  DWORD permission = 0;
1783  if (perm & O_WRONLY) {
1784  permission = GENERIC_WRITE;
1785  } else if (perm & O_RDWR) {
1786  permission = GENERIC_WRITE | GENERIC_READ;
1787  } else {
1788  permission = GENERIC_READ;
1789  }
1790  if (perm & O_APPEND) {
1791  permission |= FILE_APPEND_DATA;
1792  }
1793  if (!read_only) {
1794  sharing |= FILE_SHARE_WRITE;
1795  }
1796 
1797  wchar_t *wepath = rz_utf8_to_utf16(epath);
1798  if (!wepath) {
1799  free(epath);
1800  return -1;
1801  }
1802  HANDLE h = CreateFileW(wepath, permission, sharing, NULL, creation, flags, NULL);
1803  if (h != INVALID_HANDLE_VALUE) {
1804  ret = _open_osfhandle((intptr_t)h, perm);
1805  }
1806  free(wepath);
1807  }
1808 #else // __WINDOWS__
1809  ret = open(epath, perm, mode);
1810 #endif // __WINDOWS__
1811  free(epath);
1812  return ret;
1813 }
1814 
1815 RZ_API FILE *rz_sys_fopen(const char *path, const char *mode) {
1817  FILE *ret = NULL;
1818  char *epath = rz_path_home_expand(path);
1819  if ((strchr(mode, 'w') || strchr(mode, 'a') || rz_file_is_regular(epath))) {
1820 #if __WINDOWS__
1821  wchar_t *wepath = rz_utf8_to_utf16(epath);
1822  if (!wepath) {
1823  free(epath);
1824  return ret;
1825  }
1826  wchar_t *wmode = rz_utf8_to_utf16(mode);
1827  if (!wmode) {
1828  free(wepath);
1829  free(epath);
1830  return ret;
1831  }
1832  ret = _wfopen(wepath, wmode);
1833  free(wmode);
1834  free(wepath);
1835 #else // __WINDOWS__
1836  ret = fopen(epath, mode);
1837 #endif // __WINDOWS__
1838  }
1839  free(epath);
1840  return ret;
1841 }
1842 
1850 RZ_API int rz_sys_kill(int pid, int sig) {
1851  rz_return_val_if_fail(pid != -1, -1);
1852 #if __UNIX__
1853  return kill(pid, sig);
1854 #else
1855  return -1;
1856 #endif
1857 }
1858 
1864 RZ_API bool rz_sys_stop(void) {
1865 #if __UNIX__
1866  return !rz_sys_kill(0, SIGTSTP);
1867 #else
1868  return false;
1869 #endif
1870 }
size_t len
Definition: 6502dis.c:15
si
#define e(frag)
#define mask()
lzma_index ** i
Definition: index.h:629
ut16 val
Definition: armass64_const.h:6
static bool err
Definition: armass.c:435
FILE * fh
Definition: cabinfo.c:52
static ut64 rva(RzBinObject *o, ut64 paddr, ut64 vaddr, int va)
Definition: cbin.c:77
static int value
Definition: cmd_api.c:93
#define O_CLOEXEC
Definition: compat.h:80
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
cs_arch arch
Definition: cstool.c:13
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence ftruncate
Definition: sflib.h:113
static static fork const void static count static fd const char const char static newpath chdir
Definition: sflib.h:33
static static fork write
Definition: sflib.h:33
static static fork const void static count close
Definition: sflib.h:33
static static sync static getppid static getegid const char static filename char static len readlink
Definition: sflib.h:65
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags fcntl
Definition: sflib.h:79
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
uint32_t ut32
const char * v
Definition: dsignal.c:12
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
const char * filename
Definition: ioapi.h:137
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
snprintf
Definition: kernel.h:364
sprintf
Definition: kernel.h:365
vsnprintf
Definition: kernel.h:366
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig mkdir
Definition: sflib.h:66
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset truncate
Definition: sflib.h:117
static static fork const void static count static fd const char const char static newpath execve
Definition: sflib.h:40
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc kill
Definition: sflib.h:64
static stat
Definition: sflib.h:131
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char pathname
Definition: sflib.h:66
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment signal
Definition: sflib.h:79
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path static newfd static getpgrp static euid const sigset_t static mask const char static len const gid_t static list const char const char static newpath const char static library readdir
Definition: sflib.h:120
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname pipe
Definition: sflib.h:73
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment signum
Definition: sflib.h:79
static const char struct stat static buf struct stat static buf static vhangup int struct rusage static rusage struct sysinfo static info unsigned static __unused uname
Definition: sflib.h:153
@ ok
Definition: lz4.c:1706
int args
Definition: mipsasm.c:18
int type
Definition: mipsasm.c:17
#define FALSE
Definition: mybfd.h:102
string FILE
Definition: benchmark.py:21
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API char * rz_file_binsh(void)
Definition: file.c:393
RZ_API bool rz_file_is_regular(const char *str)
Definition: file.c:159
RZ_API char * rz_file_path(const char *bin)
Definition: file.c:354
RZ_API void * rz_lib_dl_sym(void *handler, const char *name)
Definition: lib.c:90
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API int rz_mem_protect(void *ptr, int size, const char *prot)
Definition: mem.c:279
RZ_API RZ_OWN char * rz_path_home_expand(RZ_NULLABLE const char *path)
Return a new path with the ~ char expanded to the home directory.
Definition: path.c:268
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API size_t rz_str_ncpy(char *dst, const char *src, size_t n)
Secure string copy with null terminator.
Definition: str.c:923
RZ_API char ** rz_str_argv(const char *str, int *_argc)
Definition: str.c:2509
RZ_API bool rz_str_endswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string ends with a specifc sequence of characters (case sensitive)
Definition: str.c:3329
RZ_API void rz_str_argv_free(char **argv)
Definition: str.c:2633
RZ_API RzSubprocessWaitReason rz_subprocess_wait(RzSubprocess *proc, ut64 timeout_ms)
Definition: subprocess.c:1185
RZ_API void rz_subprocess_free(RzSubprocess *proc)
Definition: subprocess.c:1273
RZ_API ut8 * rz_subprocess_out(RzSubprocess *proc, int *length)
Definition: subprocess.c:1301
RZ_API RzSubprocess * rz_subprocess_start_opt(RzSubprocessOpt *opt)
Definition: subprocess.c:893
RZ_API void rz_subprocess_fini(void)
Definition: subprocess.c:814
RZ_API ut8 * rz_subprocess_err(RzSubprocess *proc, int *length)
Definition: subprocess.c:1312
RZ_API ssize_t rz_subprocess_stdin_write(RzSubprocess *proc, const ut8 *buf, size_t buf_size)
Definition: subprocess.c:1206
RZ_API bool rz_subprocess_init(void)
Definition: subprocess.c:787
@ RZ_SUBPROCESS_PIPE_NONE
No pipe should be created. It can be used for stdin, stdout and stderr.
Definition: rz_subprocess.h:16
@ RZ_SUBPROCESS_PIPE_CREATE
Re-use the same pipe as stdout. It can be used for stderr only.
Definition: rz_subprocess.h:18
#define rz_sys_mkdir_failed()
Definition: rz_sys.h:91
#define RZ_PERM_CREAT
Definition: rz_types.h:103
#define rz_sys_perror(x)
Definition: rz_types.h:336
#define RZ_SYS_DIR
Definition: rz_types.h:218
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_NEW0(x)
Definition: rz_types.h:284
@ RZ_SYS_ARCH_PPC
Definition: rz_types.h:534
@ RZ_SYS_ARCH_BF
Definition: rz_types.h:542
@ RZ_SYS_ARCH_XAP
Definition: rz_types.h:539
@ RZ_SYS_ARCH_MIPS
Definition: rz_types.h:537
@ RZ_SYS_ARCH_M68K
Definition: rz_types.h:535
@ RZ_SYS_ARCH_JAVA
Definition: rz_types.h:536
@ RZ_SYS_ARCH_ARC
Definition: rz_types.h:547
@ RZ_SYS_ARCH_Z80
Definition: rz_types.h:546
@ RZ_SYS_ARCH_DALVIK
Definition: rz_types.h:545
@ RZ_SYS_ARCH_SH
Definition: rz_types.h:543
@ RZ_SYS_ARCH_MSIL
Definition: rz_types.h:540
@ RZ_SYS_ARCH_OBJD
Definition: rz_types.h:541
@ RZ_SYS_ARCH_I8080
Definition: rz_types.h:548
@ RZ_SYS_ARCH_AVR
Definition: rz_types.h:544
@ RZ_SYS_ARCH_TMS320
Definition: rz_types.h:551
@ RZ_SYS_ARCH_V850
Definition: rz_types.h:555
@ RZ_SYS_ARCH_SPARC
Definition: rz_types.h:538
@ RZ_SYS_ARCH_LM32
Definition: rz_types.h:563
@ RZ_SYS_ARCH_X86
Definition: rz_types.h:532
@ RZ_SYS_ARCH_RAR
Definition: rz_types.h:549
@ RZ_SYS_ARCH_ARM
Definition: rz_types.h:533
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_PERM_W
Definition: rz_types.h:94
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
#define UT64_MAX
Definition: rz_types_base.h:86
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
static int
Definition: sfsocketcall.h:114
#define O_WRONLY
Definition: sftypes.h:487
#define O_CREAT
Definition: sftypes.h:489
#define EINVAL
Definition: sftypes.h:132
int int32_t
Definition: sftypes.h:33
int size_t
Definition: sftypes.h:40
#define O_RDONLY
Definition: sftypes.h:486
#define FD_CLOEXEC
Definition: sftypes.h:522
#define O_EXCL
Definition: sftypes.h:490
int off_t
Definition: sftypes.h:41
int pid_t
Definition: sftypes.h:38
#define O_RDWR
Definition: sftypes.h:488
#define F_GETFD
Definition: sftypes.h:504
#define O_TRUNC
Definition: sftypes.h:492
#define F_SETFD
Definition: sftypes.h:505
#define O_APPEND
Definition: sftypes.h:493
#define d(i)
Definition: sha256.c:44
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define h(i)
Definition: sha256.c:48
_W64 signed int intptr_t
Definition: buffer.h:15
Definition: sftypes.h:48
Definition: zipcmp.c:77
Definition: gzappend.c:170
Definition: z80asm.h:102
const char * file
< Name of the executable to run. It is searched also in PATH
Definition: rz_subprocess.h:59
Definition: sftypes.h:80
long tv_nsec
Definition: sftypes.h:90
time_t tv_sec
Definition: sftypes.h:89
char * getenv()
const char * command
Definition: main.c:7
void signal_handler(uv_signal_t *req, int signum)
Definition: main.c:33
uv_pipe_t stdin_pipe
Definition: main.c:15
uv_pipe_t stdout_pipe
Definition: main.c:16
char ** environ
RZ_API bool rz_sys_mkdirp(const char *dir)
Definition: sys.c:691
RZ_API int rz_sys_execv(const char *pathname, char *const argv[])
Definition: sys.c:1483
RZ_API int rz_sys_execve(const char *pathname, char *const argv[], char *const envp[])
Definition: sys.c:1504
RZ_API bool rz_sys_stop(void)
Send SIGTSTP signal to every process in this process group.
Definition: sys.c:1864
RZ_API int rz_sys_cmdbg(const char *str)
Definition: sys.c:644
#define RZ_SYS_ASM_START_ROP()
Definition: sys.c:103
RZ_API bool rz_sys_aslr(int val)
Enable or disable ASLR for the calling process.
Definition: sys.c:546
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
RZ_API void rz_sys_set_environ(char **e)
Definition: sys.c:1128
RZ_API int rz_sys_execl(const char *pathname, const char *arg,...)
Definition: sys.c:1575
RZ_API int rz_sys_setenv(const char *key, const char *value)
Set an environment variable in the calling process.
Definition: sys.c:405
RZ_API void rz_sys_perror_str(const char *fun)
Definition: sys.c:737
RZ_API char * rz_sys_cmd_strf(const char *fmt,...)
Definition: sys.c:252
static char ** env
Definition: sys.c:32
RZ_API char * rz_sys_whoami(char *buf)
Definition: sys.c:1153
RZ_API int rz_sys_truncate_fd(int fd, ut64 length)
Definition: sys.c:1684
RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec)
Definition: sys.c:1458
RZ_API void rz_sys_exit(int status, bool nocleanup)
Definition: sys.c:183
RZ_API int rz_sys_run_rop(const ut8 *buf, int len)
Definition: sys.c:861
RZ_LIB_VERSION(rz_util)
RZ_API int rz_sys_open(const char *path, int perm, int mode)
Definition: sys.c:1740
RZ_API void rz_sys_env_init(void)
Definition: sys.c:1108
RZ_API bool rz_sys_getenv_asbool(const char *key)
Return true if the environment variable has the value 1, false otherwise.
Definition: sys.c:511
RZ_API RzList * rz_sys_dir(const char *path)
Definition: sys.c:216
static const struct @334 arch_bit_array[]
RZ_API int rz_sys_clearenv(void)
Clean all environment variables in the calling process.
Definition: sys.c:346
RZ_API int rz_sys_fork(void)
Definition: sys.c:1679
RZ_API int rz_sys_pipe_close(int fd)
Definition: sys.c:1462
RZ_API int rz_sys_kill(int pid, int sig)
Send signal sig to process with pid pid.
Definition: sys.c:1850
RZ_API int rz_sys_truncate(const char *file, int sz)
Definition: sys.c:1692
RZ_API int rz_sys_getpid(void)
Definition: sys.c:1164
RZ_API FILE * rz_sys_fopen(const char *path, const char *mode)
Definition: sys.c:1815
RZ_API char * rz_sys_cmd_str(const char *cmd, const char *input, int *len)
Definition: sys.c:669
RZ_API bool rz_sys_mkdir(const char *dir)
Definition: sys.c:678
const char * name
Definition: sys.c:108
RZ_API int rz_sys_execvp(const char *file, char *const argv[])
Definition: sys.c:1525
RZ_API bool rz_sys_chdir(RZ_NONNULL const char *s)
Change current directory to s, taking care of home expansion ~.
Definition: sys.c:532
RZ_API int rz_sys_system(const char *command)
Definition: sys.c:1658
RZ_API void rz_sys_info_free(RSysInfo *si)
Definition: sys.c:1251
RZ_API char * rz_sys_pid_to_path(int pid)
Definition: sys.c:920
RZ_API int rz_sys_usleep(int usecs)
Sleep for usecs microseconds.
Definition: sys.c:317
RZ_API int rz_sys_cmdf(const char *fmt,...)
Definition: sys.c:633
RZ_API bool rz_is_heap(void *p)
Definition: sys.c:911
RZ_API RSysInfo * rz_sys_info(void)
Definition: sys.c:1175
RZ_API int rz_sys_signal(int sig, void(*handler)(int))
Definition: sys.c:178
RZ_API int rz_sys_cmd_str_full(const char *cmd, const char *input, char **output, int *len, char **sterr)
Definition: sys.c:590
RZ_API char * rz_sys_getdir(void)
Get current working directory.
Definition: sys.c:521
RZ_API int rz_sys_run(const ut8 *buf, int len)
Definition: sys.c:809
RZ_API int rz_sys_crash_handler(const char *cmd)
Definition: sys.c:457
RZ_API int rz_sys_sleep(int secs)
Sleep for secs seconds.
Definition: sys.c:300
ut64 bit
Definition: sys.c:109
RZ_API int rz_sys_open_perms(int rizin_perms)
Convert rizin permissions (RZ_PERM_*) to posix permissions that can be passed to rz_sys_open .
Definition: sys.c:1718
RZ_API char ** rz_sys_get_environ(void)
Definition: sys.c:1115
RZ_API bool rz_sys_arch_match(const char *archstr, const char *arch)
Definition: sys.c:768
RZ_API const char * rz_sys_arch_str(int arch)
Definition: sys.c:798
RZ_API void rz_sys_backtrace(void)
Print the backtrace at the point this function is called from.
Definition: sys.c:265
RZ_API int rz_sys_arch_id(const char *arch)
Definition: sys.c:788
static DIR * sys_opendir(const char *path)
Definition: sys.c:210
RZ_API int rz_sys_sigaction(int *sig, void(*handler)(int))
Definition: sys.c:162
#define bool
Definition: sysdefs.h:146
RZ_API void rz_th_lock_leave(RZ_NONNULL RzThreadLock *thl)
Releases a RzThreadLock structure.
Definition: thread_lock.c:75
RZ_API void rz_th_lock_free(RZ_NULLABLE RzThreadLock *thl)
Frees a RzThreadLock structure.
Definition: thread_lock.c:89
RZ_API RZ_OWN RzThreadLock * rz_th_lock_new(bool recursive)
Allocates and initialize a RzThreadLock structure.
Definition: thread_lock.c:14
RZ_API void rz_th_lock_enter(RZ_NONNULL RzThreadLock *thl)
Acquires a RzThreadLock structure.
Definition: thread_lock.c:45
#define SIGHUP
Definition: win.h:86
DWORD LPWSTR
DWORD * HANDLE
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
DWORD
static const z80_opcode fd[]
Definition: z80_tab.h:997
static const char * cb[]
Definition: z80_tab.h:176
static int sp
Definition: z80asm.c:91
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
#define L
Definition: zip_err_str.c:7
diff_output_t output
Definition: zipcmp.c:237