Rizin
unix-like reverse engineering framework and cli tools
rz_sys.h File Reference
#include <rz_types.h>
#include <rz_list.h>

Go to the source code of this file.

Classes

struct  RSysInfo
 

Macros

#define RZ_SYS_DEVNULL   "/dev/null"
 
#define rz_sys_xsystem(cmd)   RZ_V_NOT(rz_sys_system(cmd), -1)
 
#define rz_sys_mkdir_failed()   (errno != EEXIST)
 
#define __has_builtin(n)   (0)
 
#define rz_sys_trap()   __asm__ __volatile__(".word 0");
 
#define rz_sys_breakpoint()
 

Enumerations

enum  { RZ_SYS_BITS_8 = 1 , RZ_SYS_BITS_16 = 2 , RZ_SYS_BITS_32 = 4 , RZ_SYS_BITS_64 = 8 }
 

Functions

RZ_API RSysInforz_sys_info (void)
 
RZ_API void rz_sys_info_free (RSysInfo *si)
 
RZ_API int rz_sys_sigaction (int *sig, void(*handler)(int))
 
RZ_API int rz_sys_signal (int sig, void(*handler)(int))
 
RZ_API void rz_sys_env_init (void)
 
RZ_API char ** rz_sys_get_environ (void)
 
RZ_API void rz_sys_set_environ (char **e)
 
RZ_API int rz_sys_fork (void)
 
RZ_API int rz_sys_kill (int pid, int sig)
 Send signal sig to process with pid pid. More...
 
RZ_API void rz_sys_exit (int status, bool nocleanup)
 
RZ_API bool rz_is_heap (void *p)
 
RZ_API bool rz_sys_stop (void)
 Send SIGTSTP signal to every process in this process group. More...
 
RZ_API char * rz_sys_pid_to_path (int pid)
 
RZ_API int rz_sys_run (const ut8 *buf, int len)
 
RZ_API int rz_sys_run_rop (const ut8 *buf, int len)
 
RZ_API int rz_sys_getpid (void)
 
RZ_API int rz_sys_pipe (int pipefd[2], bool close_on_exec)
 
RZ_API int rz_sys_pipe_close (int fd)
 
RZ_API int rz_sys_execv (const char *pathname, char *const argv[])
 
RZ_API int rz_sys_execve (const char *pathname, char *const argv[], char *const envp[])
 
RZ_API int rz_sys_execvp (const char *file, char *const argv[])
 
RZ_API int rz_sys_execl (const char *pathname, const char *arg,...)
 
RZ_API int rz_sys_system (const char *command)
 
RZ_API int rz_sys_crash_handler (const char *cmd)
 
RZ_API const char * rz_sys_arch_str (int arch)
 
RZ_API int rz_sys_arch_id (const char *arch)
 
RZ_API bool rz_sys_arch_match (const char *archstr, const char *arch)
 
RZ_API RzListrz_sys_dir (const char *path)
 
RZ_API void rz_sys_perror_str (const char *fun)
 
RZ_API bool rz_sys_mkdir (const char *dir)
 
RZ_API bool rz_sys_mkdirp (const char *dir)
 
RZ_API int rz_sys_sleep (int secs)
 Sleep for secs seconds. More...
 
RZ_API int rz_sys_usleep (int usecs)
 Sleep for usecs microseconds. More...
 
RZ_API char * rz_sys_getenv (const char *key)
 Get the value of an environment variable named key or NULL if none exists. More...
 
RZ_API bool rz_sys_getenv_asbool (const char *key)
 Return true if the environment variable has the value 1, false otherwise. More...
 
RZ_API int rz_sys_setenv (const char *key, const char *value)
 Set an environment variable in the calling process. More...
 
RZ_API int rz_sys_clearenv (void)
 Clean all environment variables in the calling process. More...
 
RZ_API char * rz_sys_whoami (char *buf)
 
RZ_API char * rz_sys_getdir (void)
 Get current working directory. More...
 
RZ_API bool rz_sys_chdir (RZ_NONNULL const char *s)
 Change current directory to s, taking care of home expansion ~. More...
 
RZ_API bool rz_sys_aslr (int val)
 Enable or disable ASLR for the calling process. More...
 
RZ_API int rz_sys_cmd_str_full (const char *cmd, const char *input, char **output, int *len, char **sterr)
 
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 . More...
 
RZ_API int rz_sys_open (const char *path, int perm, int mode)
 
RZ_API FILE * rz_sys_fopen (const char *path, const char *mode)
 
RZ_API int rz_sys_truncate_fd (int fd, ut64 length)
 
RZ_API int rz_sys_truncate (const char *file, int sz)
 
RZ_API int rz_sys_cmdbg (const char *cmd)
 
RZ_API int rz_sys_cmdf (const char *fmt,...) RZ_PRINTF_CHECK(1
 
RZ_API int RZ_API char * rz_sys_cmd_str (const char *cmd, const char *input, int *len)
 
RZ_API char * rz_sys_cmd_strf (const char *cmd,...) RZ_PRINTF_CHECK(1
 
RZ_API char RZ_API void rz_sys_backtrace (void)
 Print the backtrace at the point this function is called from. More...
 
RZ_API RZ_OWN char * rz_syscmd_ls (RZ_NONNULL const char *input)
 
RZ_API RZ_OWN char * rz_syscmd_cat (RZ_NONNULL const char *file)
 
RZ_API RZ_OWN char * rz_syscmd_mkdir (RZ_NONNULL const char *dir)
 
RZ_API RZ_OWN char * rz_syscmd_uniq (RZ_NONNULL const char *file)
 
RZ_API RZ_OWN char * rz_syscmd_head (RZ_NONNULL const char *file, int count)
 
RZ_API RZ_OWN char * rz_syscmd_tail (RZ_NONNULL const char *file, int count)
 
RZ_API RZ_OWN char * rz_syscmd_join (RZ_NONNULL const char *file1, RZ_NONNULL const char *file2)
 
RZ_API RZ_OWN char * rz_syscmd_sort (RZ_NONNULL const char *file)
 

Macro Definition Documentation

◆ __has_builtin

#define __has_builtin (   n)    (0)

Definition at line 140 of file rz_sys.h.

◆ rz_sys_breakpoint

#define rz_sys_breakpoint ( )
Value:
{ \
char *a = NULL; \
*a = 0; \
}
#define NULL
Definition: cris-opc.c:27
#define a(i)
Definition: sha256.c:41

Definition at line 178 of file rz_sys.h.

◆ RZ_SYS_DEVNULL

#define RZ_SYS_DEVNULL   "/dev/null"

Definition at line 10 of file rz_sys.h.

◆ rz_sys_mkdir_failed

#define rz_sys_mkdir_failed ( )    (errno != EEXIST)

Definition at line 91 of file rz_sys.h.

◆ rz_sys_trap

#define rz_sys_trap ( )    __asm__ __volatile__(".word 0");

Definition at line 177 of file rz_sys.h.

◆ rz_sys_xsystem

#define rz_sys_xsystem (   cmd)    RZ_V_NOT(rz_sys_system(cmd), -1)

Definition at line 83 of file rz_sys.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RZ_SYS_BITS_8 
RZ_SYS_BITS_16 
RZ_SYS_BITS_32 
RZ_SYS_BITS_64 

Definition at line 17 of file rz_sys.h.

17  {
18  RZ_SYS_BITS_8 = 1,
19  RZ_SYS_BITS_16 = 2,
20  RZ_SYS_BITS_32 = 4,
21  RZ_SYS_BITS_64 = 8,
22 };
@ RZ_SYS_BITS_32
Definition: rz_sys.h:20
@ RZ_SYS_BITS_64
Definition: rz_sys.h:21
@ RZ_SYS_BITS_16
Definition: rz_sys.h:19
@ RZ_SYS_BITS_8
Definition: rz_sys.h:18

Function Documentation

◆ rz_is_heap()

RZ_API bool rz_is_heap ( void *  p)

Definition at line 911 of file sys.c.

911  {
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 }
#define mask()
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
void * malloc(size_t size)
Definition: malloc.c:123
#define UT64_MAX
Definition: rz_types_base.h:86
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References free(), malloc(), mask, p, ut64(), and UT64_MAX.

◆ rz_sys_arch_id()

RZ_API int rz_sys_arch_id ( const char *  arch)

Definition at line 788 of file sys.c.

788  {
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 }
lzma_index ** i
Definition: index.h:629
cs_arch arch
Definition: cstool.c:13
Definition: z80asm.h:102
static const struct @334 arch_bit_array[]

References arch, arch_bit_array, and i.

Referenced by rz_core_rtr_gdb_run(), rz_debug_gdb_attach(), rz_debug_gdb_reg_profile(), rz_debug_qnx_attach(), and rz_debug_qnx_reg_profile().

◆ rz_sys_arch_match()

RZ_API bool rz_sys_arch_match ( const char *  archstr,
const char *  arch 
)

Definition at line 768 of file sys.c.

768  {
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 }

References arch, and p.

◆ rz_sys_arch_str()

RZ_API const char* rz_sys_arch_str ( int  arch)

Definition at line 798 of file sys.c.

798  {
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 }
ut64 bit
Definition: sys.c:109

References arch, arch_bit_array, bit, and i.

Referenced by info(), and rz_egg_load_file().

◆ rz_sys_aslr()

RZ_API bool rz_sys_aslr ( int  val)

Enable or disable ASLR for the calling process.

Definition at line 546 of file sys.c.

546  {
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 }
ut16 val
Definition: armass64_const.h:6
static ut64 rva(RzBinObject *o, ut64 paddr, ut64 vaddr, int va)
Definition: cbin.c:77
static static fork write
Definition: sflib.h:33
static static fork const void static count close
Definition: sflib.h:33
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
uint8_t ut8
Definition: lh5801.h:11
#define eprintf(x, y...)
Definition: rlcc.c:7
#define O_WRONLY
Definition: sftypes.h:487
RZ_API int rz_sys_open(const char *path, int perm, int mode)
Definition: sys.c:1740
static const z80_opcode fd[]
Definition: z80_tab.h:997

References close, eprintf, fd, NULL, O_WRONLY, rva(), rz_sys_open(), snprintf, val, and write.

Referenced by setASLR().

◆ rz_sys_backtrace()

RZ_API char RZ_API void rz_sys_backtrace ( void  )

Print the backtrace at the point this function is called from.

Definition at line 265 of file sys.c.

265  {
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 }
voidpf void uLong size
Definition: ioapi.h:138

References eprintf.

Referenced by rz_assert_log(), rz_core_is_valid_offset(), rz_io_desc_add(), and winkd_wait_packet().

◆ rz_sys_chdir()

RZ_API bool rz_sys_chdir ( RZ_NONNULL const char *  s)

Change current directory to s, taking care of home expansion ~.

Definition at line 532 of file sys.c.

532  {
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 }
static static fork const void static count static fd const char const char static newpath chdir
Definition: sflib.h:33
static RzSocket * s
Definition: rtr.c:28
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
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

References chdir, free(), rz_path_home_expand(), rz_return_val_if_fail, and s.

Referenced by rz_cmd_shell_cd_handler(), rz_main_rizin(), and rz_test_chdir_fromtest().

◆ rz_sys_clearenv()

RZ_API int rz_sys_clearenv ( void  )

Clean all environment variables in the calling process.

Please note that environment variables should not be used to store sensitive info as they might be kept elsewhere and there is no access control over that data.

Definition at line 346 of file sys.c.

346  {
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 }
size_t len
Definition: 6502dis.c:15
#define e(frag)
const char * v
Definition: dsignal.c:12
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
char ** environ
static char ** env
Definition: sys.c:32
RZ_API void rz_sys_env_init(void)
Definition: sys.c:1108
#define L
Definition: zip_err_str.c:7

References e, env, environ, eq, free(), L, len, NULL, RZ_NEWS0, rz_sys_env_init(), and v.

Referenced by rz_run_parseline(), and rz_sys_set_environ().

◆ rz_sys_cmd_str()

RZ_API int RZ_API char* rz_sys_cmd_str ( const char *  cmd,
const char *  input,
int len 
)

Definition at line 669 of file sys.c.

669  {
670  char *output = NULL;
672  return output;
673  }
674  free(output);
675  return NULL;
676 }
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
RZ_API int rz_sys_cmd_str_full(const char *cmd, const char *input, char **output, int *len, char **sterr)
Definition: sys.c:590
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237

References cmd, free(), input(), len, NULL, output, and rz_sys_cmd_str_full().

Referenced by assemble(), getstr(), rz_core_cmd_command(), rz_sys_cmd_strf(), and socket_http_get_recursive().

◆ rz_sys_cmd_str_full()

RZ_API int rz_sys_cmd_str_full ( const char *  cmd,
const char *  input,
char **  output,
int len,
char **  sterr 
)

Definition at line 590 of file sys.c.

590  {
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 }
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API char ** rz_str_argv(const char *str, int *_argc)
Definition: str.c:2509
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
const char * file
< Name of the executable to run. It is searched also in PATH
Definition: rz_subprocess.h:59
uv_pipe_t stdin_pipe
Definition: main.c:15
uv_pipe_t stdout_pipe
Definition: main.c:16

References argv, cmd, rz_subprocess_opt_t::file, input(), len, NULL, output, RZ_LOG_ERROR, rz_str_argv(), rz_str_argv_free(), rz_subprocess_err(), rz_subprocess_fini(), rz_subprocess_free(), rz_subprocess_init(), rz_subprocess_out(), RZ_SUBPROCESS_PIPE_CREATE, RZ_SUBPROCESS_PIPE_NONE, rz_subprocess_start_opt(), rz_subprocess_stdin_write(), rz_subprocess_wait(), stdin_pipe, stdout_pipe, and UT64_MAX.

Referenced by rz_cons_flush(), rz_core_cmd_pipe_old(), and rz_sys_cmd_str().

◆ rz_sys_cmd_strf()

RZ_API char* rz_sys_cmd_strf ( const char *  cmd,
  ... 
)

◆ rz_sys_cmdbg()

RZ_API int rz_sys_cmdbg ( const char *  cmd)

Definition at line 644 of file sys.c.

644  {
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 }
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
RZ_API char * rz_file_binsh(void)
Definition: file.c:393
RZ_API int rz_sys_execl(const char *pathname, const char *arg,...)
Definition: sys.c:1575
RZ_API int rz_sys_fork(void)
Definition: sys.c:1679

References eprintf, test-lz4-list::exit, free(), NULL, pid, rz_file_binsh(), rz_sys_execl(), rz_sys_fork(), and cmd_descs_generate::str.

Referenced by rz_main_rz_agent().

◆ rz_sys_cmdf()

◆ rz_sys_crash_handler()

RZ_API int rz_sys_crash_handler ( const char *  cmd)

Definition at line 457 of file sys.c.

457  {
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 }
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")
void signal_handler(uv_signal_t *req, int signum)
Definition: main.c:33
RZ_API int rz_sys_sigaction(int *sig, void(*handler)(int))
Definition: sys.c:162
#define SIGHUP
Definition: win.h:86

References cmd, free(), rz_sys_sigaction(), SIGHUP, signal_handler(), and strdup().

Referenced by rz_main_rizin().

◆ rz_sys_dir()

RZ_API RzList* rz_sys_dir ( const char *  path)

Definition at line 216 of file sys.c.

216  {
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 }
FILE * fh
Definition: cabinfo.c:52
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
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
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
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
Definition: sftypes.h:48
Definition: zipcmp.c:77
static DIR * sys_opendir(const char *path)
Definition: sys.c:210
DWORD * HANDLE

References fh, free(), HANDLE, INVALID_HANDLE_VALUE, list(), NULL, path, readdir, rz_list_append(), rz_list_new(), rz_list_newf(), strdup(), and sys_opendir().

Referenced by __clear_layout_cb(), __get_panels_config_file_from_dir(), __init_menu_saved_layout(), autocmplt_cmd_arg_file(), autocomplete_process_path(), cmd_print_format(), database_load(), is_cpu_valid(), list_themes_in_path(), load_scripts_for(), recursive_search_glob(), rtr_dir_files(), rz_cons_hud_path(), rz_core_loadlibs(), rz_core_parse_rizinrc(), rz_syscmd_ls(), rz_test_test_database_load_fuzz(), and rzfind_open_dir().

◆ rz_sys_env_init()

RZ_API void rz_sys_env_init ( void  )

Definition at line 1108 of file sys.c.

1108  {
1109  char **envp = rz_sys_get_environ();
1110  if (envp) {
1111  env = envp;
1112  }
1113 }
RZ_API char ** rz_sys_get_environ(void)
Definition: sys.c:1115

References env, and rz_sys_get_environ().

Referenced by rz_main_rizin(), and rz_sys_clearenv().

◆ rz_sys_execl()

RZ_API int rz_sys_execl ( const char *  pathname,
const char *  arg,
  ... 
)

Definition at line 1575 of file sys.c.

1575  {
1576  return -1;
1577 }

Referenced by bochs_open(), rz_core_cmd_pipe_old(), rz_run_start(), and rz_sys_cmdbg().

◆ rz_sys_execv()

RZ_API int rz_sys_execv ( const char *  pathname,
char *const  argv[] 
)

Definition at line 1483 of file sys.c.

1483  {
1484  return -1;
1485 }

Referenced by rz_run_start(), and rz_socket_proc_open().

◆ rz_sys_execve()

RZ_API int rz_sys_execve ( const char *  pathname,
char *const  argv[],
char *const  envp[] 
)

Definition at line 1504 of file sys.c.

1504  {
1505  return -1;
1506 }

◆ rz_sys_execvp()

RZ_API int rz_sys_execvp ( const char *  file,
char *const  argv[] 
)

Definition at line 1525 of file sys.c.

1525  {
1526  return -1;
1527 }

Referenced by rz_subprocess_start_opt().

◆ rz_sys_exit()

RZ_API void rz_sys_exit ( int  status,
bool  nocleanup 
)

Definition at line 183 of file sys.c.

183  {
184  if (nocleanup) {
185  _exit(status);
186  } else {
187  exit(status);
188  }
189 }
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145

References test-lz4-list::exit, and status.

Referenced by lang_pipe_run(), and rz_subprocess_start_opt().

◆ rz_sys_fopen()

RZ_API FILE* rz_sys_fopen ( const char *  path,
const char *  mode 
)

Definition at line 1815 of file sys.c.

1815  {
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 }
const char int mode
Definition: ioapi.h:137
string FILE
Definition: benchmark.py:21
RZ_API bool rz_file_is_regular(const char *str)
Definition: file.c:159

References benchmark::FILE, free(), NULL, path, rz_file_is_regular(), rz_path_home_expand(), and rz_return_val_if_fail.

Referenced by __write(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), ihex_write(), load_1(), rz_cmd_cmp_file_handler(), rz_cons_flush(), rz_core_cmd_foreach(), rz_core_dump(), rz_file_dump(), rz_file_hexdump(), rz_file_slurp(), rz_file_slurp_hexpairs(), rz_file_slurp_range(), rz_line_hist_load(), rz_line_hist_save(), rz_run_parseline(), rz_save_panels_layout(), and rz_vlog().

◆ rz_sys_fork()

◆ rz_sys_get_environ()

RZ_API char** rz_sys_get_environ ( void  )

Definition at line 1115 of file sys.c.

1115  {
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 }
RZ_API void * rz_lib_dl_sym(void *handler, const char *name)
Definition: lib.c:90

References env, environ, NULL, and rz_lib_dl_sym().

Referenced by autocmplt_cmd_arg_env(), create_child_env(), rz_cmd_shell_env_handler(), rz_main_rizin(), rz_run_start(), and rz_sys_env_init().

◆ rz_sys_getdir()

RZ_API char* rz_sys_getdir ( void  )

Get current working directory.

Definition at line 521 of file sys.c.

521  {
522 #if __WINDOWS__
523  return _getcwd(NULL, 0);
524 #else
525  return getcwd(NULL, 0);
526 #endif
527 }

References NULL.

Referenced by getcommapath(), rz_cmd_shell_cd_handler(), rz_cmd_shell_pwd_handler(), rz_file_abspath(), rz_test_chdir_fromtest(), rz_test_main(), and windbg_gcore().

◆ rz_sys_getenv()

RZ_API char* rz_sys_getenv ( const char *  key)

Get the value of an environment variable named key or NULL if none exists.

Definition at line 483 of file sys.c.

483  {
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 }
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
#define b(i)
Definition: sha256.c:42
char * getenv()

References b, free(), getenv(), key, NULL, strdup(), and val.

Referenced by __load_plugins(), binutils_assemble(), create_cache_bins(), find_include(), gdbr_connect(), hash_load_plugins(), interact_commands(), loadSystemPlugins(), parse_signature(), rz_assert_log(), rz_cmd_shell_env_handler(), rz_cons_get_size(), rz_core_config_init(), rz_core_parse_rizinrc(), rz_core_run_script(), rz_core_sysenv_end(), rz_egg_Cfile_getCompiler(), rz_egg_Cfile_set_cEnv(), rz_egg_lang_include_path(), rz_file_binsh(), rz_file_path(), rz_file_tmpdir(), rz_lib_path(), rz_main_rizin(), rz_main_rz_asm(), rz_main_rz_bin(), rz_main_rzpipe(), rz_path_home(), rz_path_home_prefix(), rz_reg_parse_gdb_profile(), rz_reg_set_profile(), rz_run_config_env(), rz_run_parseline(), rz_str_home(), rzp_open_spawn(), set_color_default(), socket_http_get_recursive(), spp_var_get(), windbg_gcore(), windbg_init(), and windbg_open().

◆ rz_sys_getenv_asbool()

RZ_API bool rz_sys_getenv_asbool ( const char *  key)

Return true if the environment variable has the value 1, false otherwise.

Definition at line 511 of file sys.c.

511  {
512  char *env = rz_sys_getenv(key);
513  const bool res = (env && *env == '1');
514  free(env);
515  return res;
516 }
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

References env, free(), key, and rz_sys_getenv().

Referenced by rz_core_parse_rizinrc(), and rz_main_rizin().

◆ rz_sys_getpid()

RZ_API int rz_sys_getpid ( void  )

Definition at line 1164 of file sys.c.

1164  {
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 }

Referenced by num_callback(), sdb_lock(), and TAG_CALLBACK().

◆ rz_sys_info()

RZ_API RSysInfo* rz_sys_info ( void  )

Definition at line 1175 of file sys.c.

1175  {
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 }
si
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
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
int type
Definition: mipsasm.c:17
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define rz_sys_perror(x)
Definition: rz_types.h:336
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API void rz_sys_info_free(RSysInfo *si)
Definition: sys.c:1251
DWORD

References DWORD, key, major, minor, NULL, RZ_NEW0, rz_str_newf(), rz_sys_info_free(), rz_sys_perror, si, strdup(), autogen_x86imm::tmp, type, and uname.

Referenced by __is_windows_ten(), and rz_cmd_shell_uname_handler().

◆ rz_sys_info_free()

RZ_API void rz_sys_info_free ( RSysInfo si)

Definition at line 1251 of file sys.c.

1251  {
1252  free(si->sysname);
1253  free(si->nodename);
1254  free(si->release);
1255  free(si->version);
1256  free(si->machine);
1257  free(si);
1258 }

References free(), and si.

Referenced by __is_windows_ten(), rz_cmd_shell_uname_handler(), and rz_sys_info().

◆ rz_sys_kill()

RZ_API int rz_sys_kill ( int  pid,
int  sig 
)

Send signal sig to process with pid pid.

Parameters
pidPID of the process to send the signal to
sigSignal to send to the process.
Returns
0 on success, -1 on failure.

Definition at line 1850 of file sys.c.

1850  {
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 }
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

References kill, pid, and rz_return_val_if_fail.

Referenced by rz_debug_signal_send(), rz_main_rz_agent(), and rz_sys_stop().

◆ rz_sys_mkdir()

RZ_API bool rz_sys_mkdir ( const char *  dir)

Definition at line 678 of file sys.c.

678  {
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 }
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

References free(), and mkdir.

Referenced by rz_sys_mkdirp().

◆ rz_sys_mkdirp()

RZ_API bool rz_sys_mkdirp ( const char *  dir)

Definition at line 691 of file sys.c.

691  {
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 }
#define rz_sys_mkdir_failed()
Definition: rz_sys.h:91
#define RZ_SYS_DIR
Definition: rz_types.h:218
RZ_API bool rz_sys_mkdir(const char *dir)
Definition: sys.c:678

References eprintf, free(), p, path, RZ_SYS_DIR, rz_sys_mkdir(), rz_sys_mkdir_failed, and strdup().

Referenced by __create_panels_config_path(), download_and_write(), extract_binobj(), rz_line_hist_save(), rz_syscmd_mkdir(), and sdb_disk_create().

◆ rz_sys_open()

RZ_API int rz_sys_open ( const char *  path,
int  perm,
int  mode 
)

Definition at line 1740 of file sys.c.

1740  {
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 }
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define O_CREAT
Definition: sftypes.h:489
#define O_EXCL
Definition: sftypes.h:490
#define O_RDWR
Definition: sftypes.h:488
#define O_TRUNC
Definition: sftypes.h:492
#define O_APPEND
Definition: sftypes.h:493
#define h(i)
Definition: sha256.c:48
_W64 signed int intptr_t

References DWORD, flags, free(), h, HANDLE, INVALID_HANDLE_VALUE, NULL, O_APPEND, O_CREAT, O_EXCL, O_RDWR, O_TRUNC, O_WRONLY, path, rz_path_home_expand(), and rz_return_val_if_fail.

Referenced by apprentice_compile(), apprentice_map(), binutils_assemble(), buf_file_init(), langFromHashbang(), procfs_pid_slurp(), rz_cons_pipe_open(), rz_core_editor(), rz_file_mkstemp(), rz_file_mmap(), rz_file_mmap_resize(), rz_file_truncate(), rz_sys_aslr(), rz_sys_truncate(), and sp_open().

◆ rz_sys_open_perms()

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 .

rz_sys_open accepts posix permissions for now, not the arch-independent ones provided by RZ_PERM_*. This function is an helper to convert from rizin permissions to posix ones.

Definition at line 1718 of file sys.c.

1718  {
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 }
#define RZ_PERM_CREAT
Definition: rz_types.h:103
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_PERM_W
Definition: rz_types.h:94
#define O_RDONLY
Definition: sftypes.h:486

References O_CREAT, O_RDONLY, O_RDWR, O_WRONLY, RZ_PERM_CREAT, RZ_PERM_R, and RZ_PERM_W.

Referenced by rz_io_ar_open(), rz_io_ar_open_many(), and rz_io_def_mmap_create_new_file().

◆ rz_sys_perror_str()

RZ_API void rz_sys_perror_str ( const char *  fun)

Definition at line 737 of file sys.c.

737  {
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 }
static bool err
Definition: armass.c:435
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

References DWORD, eprintf, err, free(), NULL, and rz_str_endswith().

◆ rz_sys_pid_to_path()

RZ_API char* rz_sys_pid_to_path ( int  pid)

Definition at line 920 of file sys.c.

920  {
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 }
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 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
const char * filename
Definition: ioapi.h:137
static stat
Definition: sflib.h:131
@ ok
Definition: lz4.c:1706
#define FALSE
Definition: mybfd.h:102
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
int int32_t
Definition: sftypes.h:33
#define d(i)
Definition: sha256.c:44
Definition: sftypes.h:80
const char * name
Definition: sys.c:108
static int sp
Definition: z80asm.c:91

References argv, d, DWORD, eprintf, FALSE, free(), getenv(), HANDLE, L, len, length, malloc(), name, NULL, ok, p, path, pid, readlink, rz_str_ncpy(), rz_str_newf(), snprintf, sp, stat, strdup(), and autogen_x86imm::tmp.

Referenced by cmd_debug_pid(), and rz_debug_get_baddr().

◆ rz_sys_pipe()

RZ_API int rz_sys_pipe ( int  pipefd[2],
bool  close_on_exec 
)

◆ rz_sys_pipe_close()

◆ rz_sys_run()

RZ_API int rz_sys_run ( const ut8 buf,
int  len 
)

Definition at line 809 of file sys.c.

809  {
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 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
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
RZ_API int rz_mem_protect(void *ptr, int size, const char *prot)
Definition: mem.c:279
static int
Definition: sfsocketcall.h:114
int size_t
Definition: sftypes.h:40
static const char * cb[]
Definition: z80_tab.h:176

References cb, eprintf, test-lz4-list::exit, free(), int, len, malloc(), memcpy(), num, p, pid, rz_mem_protect(), and rz_sys_fork().

Referenced by rz_egg_run().

◆ rz_sys_run_rop()

RZ_API int rz_sys_run_rop ( const ut8 buf,
int  len 
)

Definition at line 861 of file sys.c.

861  {
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 }
int pid_t
Definition: sftypes.h:38
#define RZ_SYS_ASM_START_ROP()
Definition: sys.c:103

References eprintf, test-lz4-list::exit, free(), len, malloc(), memcpy(), num, pid, RZ_SYS_ASM_START_ROP, and rz_sys_fork().

Referenced by rz_egg_run_rop().

◆ rz_sys_set_environ()

RZ_API void rz_sys_set_environ ( char **  e)

Definition at line 1128 of file sys.c.

1128  {
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 }
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 int rz_sys_clearenv(void)
Clean all environment variables in the calling process.
Definition: sys.c:346

References e, env, environ, free(), NULL, rz_sys_clearenv(), rz_sys_setenv(), and val.

Referenced by rz_subprocess_start_opt().

◆ rz_sys_setenv()

RZ_API int rz_sys_setenv ( const char *  key,
const char *  value 
)

Set an environment variable in the calling process.

Definition at line 405 of file sys.c.

405  {
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 }
static int value
Definition: cmd_api.c:93
DWORD LPWSTR

References free(), key, L, LPWSTR, and value.

Referenced by ARG_CALLBACK(), cb_dirhome(), cb_dirtmp(), env(), rcc_next(), rz_cmd_shell_env_handler(), rz_core_cmd_subst_i(), rz_core_config_init(), rz_core_sysenv_begin(), rz_core_sysenv_end(), rz_egg_lang_include_init(), rz_egg_lang_include_path(), rz_main_rz_bin(), rz_run_config_env(), rz_run_parseline(), rz_test_main(), spp_var_set(), TAG_CALLBACK(), and tmpenvs_free().

◆ rz_sys_sigaction()

RZ_API int rz_sys_sigaction ( int sig,
void(*)(int handler 
)

Definition at line 162 of file sys.c.

162  {
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 }
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
#define EINVAL
Definition: sftypes.h:132

References EINVAL, eprintf, i, int, and signal.

Referenced by rz_sys_crash_handler(), and rz_sys_signal().

◆ rz_sys_signal()

◆ rz_sys_sleep()

RZ_API int rz_sys_sleep ( int  secs)

Sleep for secs seconds.

Definition at line 300 of file sys.c.

300  {
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 }
time_t tv_sec
Definition: sftypes.h:89

References NULL, timespec::tv_nsec, and timespec::tv_sec.

Referenced by qnxr_attach(), rtr_visual(), rz_cmd_shell_sleep_handler(), rz_core_visual_esil(), rz_core_visual_show_char(), rz_core_visual_trackflags(), rz_main_rizin(), rz_run_config_env(), rz_socket_spawn(), and sdb_lock_wait().

◆ rz_sys_stop()

RZ_API bool rz_sys_stop ( void  )

Send SIGTSTP signal to every process in this process group.

Returns
true if at least one signal was sent, false otherwise

Definition at line 1864 of file sys.c.

1864  {
1865 #if __UNIX__
1866  return !rz_sys_kill(0, SIGTSTP);
1867 #else
1868  return false;
1869 #endif
1870 }
RZ_API int rz_sys_kill(int pid, int sig)
Send signal sig to process with pid pid.
Definition: sys.c:1850

References rz_sys_kill().

Referenced by rz_cons_controlz().

◆ rz_sys_system()

RZ_API int rz_sys_system ( const char *  command)

◆ rz_sys_truncate()

RZ_API int rz_sys_truncate ( const char *  file,
int  sz 
)

Definition at line 1692 of file sys.c.

1692  {
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 }
#define r
Definition: crypto_rc6.c:12
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
Definition: gzappend.c:170
RZ_API int rz_sys_truncate_fd(int fd, ut64 length)
Definition: sys.c:1684

References close, eprintf, fd, O_RDWR, r, rz_sys_open(), rz_sys_truncate_fd(), and truncate.

Referenced by rz_core_dump(), rz_file_dump(), rz_file_hexdump(), and rz_file_mmap_resize().

◆ rz_sys_truncate_fd()

RZ_API int rz_sys_truncate_fd ( int  fd,
ut64  length 
)

Definition at line 1684 of file sys.c.

1684  {
1685 #ifdef _MSC_VER
1686  return _chsize_s(fd, length);
1687 #else
1688  return ftruncate(fd, (off_t)length);
1689 #endif
1690 }
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
int off_t
Definition: sftypes.h:41

References fd, ftruncate, and length.

Referenced by buf_file_resize(), and rz_sys_truncate().

◆ rz_sys_usleep()

RZ_API int rz_sys_usleep ( int  usecs)

Sleep for usecs microseconds.

Definition at line 317 of file sys.c.

317  {
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 }

References NULL, timespec::tv_nsec, and timespec::tv_sec.

Referenced by basefind_thread_ui(), run_basic_block_analysis(), rz_cmd_debug(), rz_cons_flush(), rz_core_rtr_cmds_query(), rz_core_rtr_http_run(), rz_core_visual_esil(), rz_diff_draw_tui(), rz_socket_spawn(), rz_th_yield(), and visual_seek_animation().

◆ rz_sys_whoami()

RZ_API char* rz_sys_whoami ( char *  buf)

Definition at line 1153 of file sys.c.

1153  {
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 }
sprintf
Definition: kernel.h:365

References pid, sprintf, and strdup().

Referenced by rz_core_config_init().

◆ rz_syscmd_cat()

RZ_API RZ_OWN char* rz_syscmd_cat ( RZ_NONNULL const char *  file)

Definition at line 442 of file syscmd.c.

442  {
444 
445  const char *p = NULL;
446  if (file) {
447  if ((p = strchr(file, ' '))) {
448  p = p + 1;
449  } else {
450  p = file;
451  }
452  }
453  if (p && *p) {
454  char *filename = strdup(p);
456  char *data = rz_file_slurp(filename, NULL);
457  if (!data) {
458  eprintf("No such file or directory\n");
459  }
460  free(filename);
461  return data;
462  } else {
463  eprintf("Usage: cat [file]\n");
464  }
465  return NULL;
466 }
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str)
Removes whitespace characters (space, tab, newline etc.) from the beginning and end of a string.
Definition: str_trim.c:190
static int file
Definition: z80asm.c:58

References eprintf, file, free(), NULL, p, rz_file_slurp(), rz_return_val_if_fail, rz_str_trim(), and strdup().

Referenced by rz_cmd_shell_cat_handler().

◆ rz_syscmd_head()

RZ_API RZ_OWN char* rz_syscmd_head ( RZ_NONNULL const char *  file,
int  count 
)

Definition at line 284 of file syscmd.c.

284  {
286 
287  const char *p = NULL;
288  if ((p = strchr(file, ' '))) {
289  p = p + 1;
290  } else {
291  p = file;
292  }
293  if (p && *p) {
294  char *filename = strdup(p);
296  char *data = rz_file_slurp_lines(filename, 1, count);
297  if (!data) {
298  eprintf("No such file or directory\n");
299  }
300  free(filename);
301  return data;
302  } else {
303  eprintf("Usage: head 7 [file]\n");
304  }
305  return NULL;
306 }
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 count
Definition: sflib.h:98
RZ_API char * rz_file_slurp_lines(const char *file, int line, int count)
Definition: file.c:736

References count, eprintf, file, free(), NULL, p, rz_file_slurp_lines(), rz_return_val_if_fail, rz_str_trim(), and strdup().

◆ rz_syscmd_join()

RZ_API RZ_OWN char* rz_syscmd_join ( RZ_NONNULL const char *  file1,
RZ_NONNULL const char *  file2 
)

Definition at line 367 of file syscmd.c.

367  {
368  rz_return_val_if_fail(file1 && file2, NULL);
369 
370  const char *p1 = NULL, *p2 = NULL;
371  if (file1) {
372  if ((p1 = strchr(file1, ' '))) {
373  p1 = p1 + 1;
374  } else {
375  p1 = file1;
376  }
377  }
378  if (file2) {
379  if ((p2 = strchr(file2, ' '))) {
380  p2 = p2 + 1;
381  } else {
382  p2 = file2;
383  }
384  }
385  if (p1 && *p1 && p2 && *p2) {
386  char *filename1 = strdup(p1);
387  char *filename2 = strdup(p2);
388  rz_str_trim(filename1);
389  rz_str_trim(filename2);
390  char *data1 = rz_file_slurp(filename1, NULL);
391  char *data2 = rz_file_slurp(filename2, NULL);
392  char *data = NULL;
393  if (!data1 && !data2) {
394  eprintf("No such files or directory\n");
395  } else {
397  RzList *list1 = rz_str_split_list(data1, "\n", 0);
398  RzList *list2 = rz_str_split_list(data2, "\n", 0);
399  if (!list || !list1 || !list2) {
400  rz_list_free(list2);
401  rz_list_free(list1);
403  return NULL;
404  }
405  char *str1, *str2;
406  RzListIter *iter1, *iter2;
407  rz_list_foreach (list1, iter1, str1) {
408  char *field = strdup(str1); // extract comman field
409  char *end = strchr(field, ' ');
410  if (end) {
411  *end = '\0';
412  } else {
413  free(field);
414  continue;
415  }
416  rz_list_foreach (list2, iter2, str2) {
417  if (rz_str_startswith(str2, field)) {
418  char *out = rz_str_new(field);
419  char *first = strchr(str1, ' ');
420  char *second = strchr(str2, ' ');
421  rz_str_append(out, first ? first : " ");
422  rz_str_append(out, second ? second : " ");
424  }
425  }
426  free(field);
427  }
428  data = rz_list_to_str(list, '\n');
429  rz_list_free(list2);
430  rz_list_free(list1);
432  }
433  free(filename1);
434  free(filename2);
435  return data;
436  } else {
437  eprintf("Usage: join file1 file2\n");
438  }
439  return NULL;
440 }
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
RZ_API RZ_OWN char * rz_list_to_str(RZ_NONNULL RzList *list, char ch)
Casts a RzList containg strings into a concatenated string.
Definition: list.c:785
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
RZ_API char * rz_str_append(char *ptr, const char *string)
Definition: str.c:1063
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865
RZ_API bool rz_str_startswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string starts with a specifc sequence of characters (case sensitive)
Definition: str.c:3286
RZ_API RzList * rz_str_split_list(char *str, const char *c, int n)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3429

References test_evm::end, eprintf, free(), list(), NULL, out, rz_file_slurp(), rz_list_append(), rz_list_free(), rz_list_newf(), rz_list_to_str(), rz_return_val_if_fail, rz_str_append(), rz_str_new(), rz_str_split_list(), rz_str_startswith(), rz_str_trim(), and strdup().

◆ rz_syscmd_ls()

RZ_API RZ_OWN char* rz_syscmd_ls ( RZ_NONNULL const char *  input)

Definition at line 121 of file syscmd.c.

121  {
122  char *res = NULL;
123  const char *path = ".";
124  char *d = NULL;
125  char *p = NULL;
126  char *homepath = NULL;
127  char *pattern = NULL;
128  int printfmt = 0;
129  RzListIter *iter;
130  RzList *files;
131  char *name;
132  char *dir;
133  int off;
134  if (!input) {
135  input = "";
136  path = ".";
137  }
138  if (*input == 'q') {
139  printfmt = 'q';
140  input++;
141  }
142  if (*input && input[0] == ' ') {
143  input++;
144  }
145  if (*input) {
146  if ((!strncmp(input, "-h", 2))) {
147  eprintf("Usage: ls ([-e,-l,-j,-q]) ([path]) # long, json, quiet\n");
148  } else if ((!strncmp(input, "-e", 2))) {
149  printfmt = 'e';
151  } else if ((!strncmp(input, "-q", 2))) {
152  printfmt = 'q';
154  } else if ((!strncmp(input, "-l", 2)) || (!strncmp(input, "-j", 2))) {
155  // mode = 'l';
156  if (input[2]) {
157  printfmt = (input[2] == 'j') ? FMT_JSON : FMT_RAW;
159  if (!*path) {
160  path = ".";
161  }
162  } else {
163  printfmt = FMT_RAW;
164  }
165  } else {
166  path = input;
167  }
168  }
169  if (!path || !*path) {
170  path = ".";
171  } else if (rz_str_startswith(path, "~")) {
172  homepath = rz_path_home_expand(path);
173  if (homepath) {
174  path = (const char *)homepath;
175  }
176  } else if (*path == '$') {
177  if (!strncmp(path + 1, "home", 4) || !strncmp(path + 1, "HOME", 4)) {
178  homepath = rz_str_home((strlen(path) > 5) ? path + 6 : NULL);
179  if (homepath) {
180  path = (const char *)homepath;
181  }
182  }
183  }
184  if (!rz_file_is_directory(path)) {
185  p = strrchr(path, '/');
186  if (p) {
187  off = p - path;
188  d = (char *)calloc(1, off + 1);
189  if (!d) {
190  free(homepath);
191  return NULL;
192  }
193  memcpy(d, path, off);
194  path = (const char *)d;
195  pattern = strdup(p + 1);
196  } else {
197  pattern = strdup(path);
198  path = ".";
199  }
200  } else {
201  pattern = strdup("*");
202  }
203  if (rz_file_is_regular(path)) {
204  res = showfile(res, 0, path, path, printfmt);
205  free(homepath);
206  free(pattern);
207  free(d);
208  return res;
209  }
210  files = rz_sys_dir(path);
211 
212  if (path[strlen(path) - 1] == '/') {
213  dir = strdup(path);
214  } else {
215  dir = rz_str_append(strdup(path), "/");
216  }
217  int nth = 0;
218  if (printfmt == FMT_JSON) {
219  res = strdup("[");
220  }
221  needs_newline = 0;
222  rz_list_foreach (files, iter, name) {
223  char *n = rz_str_append(strdup(dir), name);
224  if (!n) {
225  break;
226  }
227  if (rz_str_glob(name, pattern)) {
228  if (*n) {
229  res = showfile(res, nth, n, name, printfmt);
230  }
231  nth++;
232  }
233  free(n);
234  }
235  if (printfmt == FMT_JSON) {
236  res = rz_str_append(res, "]");
237  }
238  if (needs_newline) {
239  res = rz_str_append(res, "\n");
240  }
241  free(dir);
242  free(d);
243  free(homepath);
244  free(pattern);
246  return res;
247 }
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n output MIME type special files
Definition: file_opts.h:46
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
int n
Definition: mipsasm.c:19
const char * name
Definition: op.c:541
int off
Definition: pal.c:13
RZ_API bool rz_file_is_directory(const char *str)
Definition: file.c:167
RZ_API char * rz_str_home(const char *str)
Definition: str.c:354
RZ_API bool rz_str_glob(const char *str, const char *glob)
Definition: str.c:2368
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86
RZ_API RzList * rz_sys_dir(const char *path)
Definition: sys.c:216
#define FMT_RAW
Definition: syscmd.c:8
#define FMT_JSON
Definition: syscmd.c:9
static char * showfile(char *res, const int nth, const char *fpath, const char *name, int printfmt)
Definition: syscmd.c:13
static int needs_newline
Definition: syscmd.c:11

References calloc(), d, eprintf, files, FMT_JSON, FMT_RAW, free(), input(), memcpy(), n, name, needs_newline, NULL, off, p, path, rz_file_is_directory(), rz_file_is_regular(), rz_list_free(), rz_path_home_expand(), rz_str_append(), rz_str_glob(), rz_str_home(), rz_str_startswith(), rz_str_trim_head_ro(), rz_sys_dir(), showfile(), and strdup().

Referenced by rz_cmd_shell_ls_handler().

◆ rz_syscmd_mkdir()

RZ_API RZ_OWN char* rz_syscmd_mkdir ( RZ_NONNULL const char *  dir)

Definition at line 468 of file syscmd.c.

468  {
470 
471  const char *suffix = rz_str_trim_head_ro(strchr(dir, ' '));
472  if (!suffix || !strncmp(suffix, "-p", 3)) {
473  return rz_str_dup(NULL, "Usage: mkdir [-p] [directory]\n");
474  }
475  int ret;
476  char *dirname = (!strncmp(suffix, "-p ", 3))
477  ? strdup(suffix + 3)
478  : strdup(suffix);
479  rz_str_trim(dirname);
480  ret = rz_sys_mkdirp(dirname);
481  if (!ret && rz_sys_mkdir_failed()) {
482  char *res = rz_str_newf("Cannot create \"%s\"\n", dirname);
483  free(dirname);
484  return res;
485  }
486  free(dirname);
487  return NULL;
488 }
unsigned char suffix[65536]
Definition: gun.c:164
RZ_API char * rz_str_dup(char *ptr, const char *string)
Definition: str.c:1021
RZ_API bool rz_sys_mkdirp(const char *dir)
Definition: sys.c:691

References free(), NULL, rz_return_val_if_fail, rz_str_dup(), rz_str_newf(), rz_str_trim(), rz_str_trim_head_ro(), rz_sys_mkdir_failed, rz_sys_mkdirp(), strdup(), and suffix.

Referenced by rz_cmd_shell_mkdir_handler().

◆ rz_syscmd_sort()

RZ_API RZ_OWN char* rz_syscmd_sort ( RZ_NONNULL const char *  file)

Definition at line 254 of file syscmd.c.

254  {
256 
257  const char *p = NULL;
258  RzList *list = NULL;
259  if ((p = strchr(file, ' '))) {
260  p = p + 1;
261  } else {
262  p = file;
263  }
264  if (p && *p) {
265  char *filename = strdup(p);
267  char *data = rz_file_slurp(filename, NULL);
268  if (!data) {
269  eprintf("No such file or directory\n");
270  } else {
271  list = rz_str_split_list(data, "\n", 0);
273  data = rz_list_to_str(list, '\n');
275  }
276  free(filename);
277  return data;
278  } else {
279  eprintf("Usage: sort [file]\n");
280  }
281  return NULL;
282 }
RZ_API void rz_list_sort(RZ_NONNULL RzList *list, RZ_NONNULL RzListComparator cmp)
Sorts via merge sort or via insertion sort a list.
Definition: list.c:743
static int cmpstr(const void *_a, const void *_b)
Definition: syscmd.c:249

References cmpstr(), eprintf, file, free(), list(), NULL, p, rz_file_slurp(), rz_list_free(), rz_list_sort(), rz_list_to_str(), rz_return_val_if_fail, rz_str_split_list(), rz_str_trim(), and strdup().

Referenced by rz_cmd_shell_sort_handler().

◆ rz_syscmd_tail()

RZ_API RZ_OWN char* rz_syscmd_tail ( RZ_NONNULL const char *  file,
int  count 
)

Definition at line 308 of file syscmd.c.

308  {
310 
311  const char *p = NULL;
312  if (file) {
313  if ((p = strchr(file, ' '))) {
314  p = p + 1;
315  } else {
316  p = file;
317  }
318  }
319  if (p && *p) {
320  char *filename = strdup(p);
323  if (!data) {
324  eprintf("No such file or directory\n");
325  }
326  free(filename);
327  return data;
328  } else {
329  eprintf("Usage: tail 7 [file]\n");
330  }
331  return NULL;
332 }
RZ_API RZ_OWN char * rz_file_slurp_lines_from_bottom(const char *file, int line)
Definition: file.c:708

References count, eprintf, file, free(), NULL, p, rz_file_slurp_lines_from_bottom(), rz_return_val_if_fail, rz_str_trim(), and strdup().

◆ rz_syscmd_uniq()

RZ_API RZ_OWN char* rz_syscmd_uniq ( RZ_NONNULL const char *  file)

Definition at line 334 of file syscmd.c.

334  {
336 
337  const char *p = NULL;
338  RzList *list = NULL;
339  if (file) {
340  if ((p = strchr(file, ' '))) {
341  p = p + 1;
342  } else {
343  p = file;
344  }
345  }
346  if (p && *p) {
347  char *filename = strdup(p);
349  char *data = rz_file_slurp(filename, NULL);
350  if (!data) {
351  eprintf("No such file or directory\n");
352  } else {
353  list = rz_str_split_list(data, "\n", 0);
354  RzList *uniq_list = rz_list_uniq(list, cmpstr);
355  data = rz_list_to_str(uniq_list, '\n');
356  rz_list_free(uniq_list);
358  }
359  free(filename);
360  return data;
361  } else {
362  eprintf("Usage: uniq [file]\n");
363  }
364  return NULL;
365 }
RZ_API RZ_OWN RzList * rz_list_uniq(RZ_NONNULL const RzList *list, RZ_NONNULL RzListComparator cmp)
Returns a new RzList which contains only unique values.
Definition: list.c:756

References cmpstr(), eprintf, file, free(), list(), NULL, p, rz_file_slurp(), rz_list_free(), rz_list_to_str(), rz_list_uniq(), rz_return_val_if_fail, rz_str_split_list(), rz_str_trim(), and strdup().

Referenced by rz_cmd_shell_uniq_handler().