Rizin
unix-like reverse engineering framework and cli tools
rz_sys.h
Go to the documentation of this file.
1 #ifndef RZ_SYS_H
2 #define RZ_SYS_H
3 
4 #include <rz_types.h>
5 #include <rz_list.h>
6 
7 #if __WINDOWS__
8 #define RZ_SYS_DEVNULL "nul"
9 #else
10 #define RZ_SYS_DEVNULL "/dev/null"
11 #endif
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 enum {
22 };
23 
24 typedef struct {
25  char *sysname;
26  char *nodename;
27  char *release;
28  char *version;
29  char *machine;
30 } RSysInfo;
31 
34 
35 RZ_API int rz_sys_sigaction(int *sig, void (*handler)(int));
36 RZ_API int rz_sys_signal(int sig, void (*handler)(int));
37 RZ_API void rz_sys_env_init(void);
38 RZ_API char **rz_sys_get_environ(void);
39 RZ_API void rz_sys_set_environ(char **e);
40 
41 RZ_API int rz_sys_fork(void);
42 RZ_API int rz_sys_kill(int pid, int sig);
43 // nocleanup = false => exit(); true => _exit()
44 RZ_API void rz_sys_exit(int status, bool nocleanup);
45 RZ_API bool rz_is_heap(void *p);
46 RZ_API bool rz_sys_stop(void);
47 RZ_API char *rz_sys_pid_to_path(int pid);
48 RZ_API int rz_sys_run(const ut8 *buf, int len);
49 RZ_API int rz_sys_run_rop(const ut8 *buf, int len);
50 RZ_API int rz_sys_getpid(void);
51 #if !HAVE_PIPE || (__UNIX__ && HAVE_PIPE)
52 RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec);
53 RZ_API int rz_sys_pipe_close(int fd);
54 #else
55 #define rz_sys_pipe pipe
56 #define rz_sys_pipe_close close
57 #endif
58 #if !HAVE_EXECV || (__UNIX__ && HAVE_EXECV && HAVE_PIPE && !HAVE_PIPE2)
59 RZ_API int rz_sys_execv(const char *pathname, char *const argv[]);
60 #else
61 #define rz_sys_execv execv
62 #endif
63 #if !HAVE_EXECVE || (__UNIX__ && HAVE_EXECVE && HAVE_PIPE && !HAVE_PIPE2)
64 RZ_API int rz_sys_execve(const char *pathname, char *const argv[], char *const envp[]);
65 #else
66 #define rz_sys_execve execve
67 #endif
68 #if !HAVE_EXECVP || (__UNIX__ && HAVE_EXECVP && HAVE_PIPE && !HAVE_PIPE2)
69 RZ_API int rz_sys_execvp(const char *file, char *const argv[]);
70 #else
71 #define rz_sys_execvp execvp
72 #endif
73 #if !HAVE_EXECL || (__UNIX__ && HAVE_EXECL && HAVE_PIPE && !HAVE_PIPE2)
74 RZ_API int rz_sys_execl(const char *pathname, const char *arg, ...);
75 #else
76 #define rz_sys_execl execl
77 #endif
78 #if !HAVE_SYSTEM || (__UNIX__ && HAVE_SYSTEM && HAVE_PIPE && !HAVE_PIPE2)
79 RZ_API int rz_sys_system(const char *command);
80 #else
81 #define rz_sys_system system
82 #endif
83 #define rz_sys_xsystem(cmd) RZ_V_NOT(rz_sys_system(cmd), -1)
84 
85 RZ_API int rz_sys_crash_handler(const char *cmd);
86 RZ_API const char *rz_sys_arch_str(int arch);
87 RZ_API int rz_sys_arch_id(const char *arch);
88 RZ_API bool rz_sys_arch_match(const char *archstr, const char *arch);
89 RZ_API RzList *rz_sys_dir(const char *path);
90 RZ_API void rz_sys_perror_str(const char *fun);
91 #define rz_sys_mkdir_failed() (errno != EEXIST)
92 RZ_API bool rz_sys_mkdir(const char *dir);
93 RZ_API bool rz_sys_mkdirp(const char *dir);
94 RZ_API int rz_sys_sleep(int secs);
95 RZ_API int rz_sys_usleep(int usecs);
96 RZ_API char *rz_sys_getenv(const char *key);
97 RZ_API bool rz_sys_getenv_asbool(const char *key);
98 RZ_API int rz_sys_setenv(const char *key, const char *value);
99 RZ_API int rz_sys_clearenv(void);
100 RZ_API char *rz_sys_whoami(char *buf);
101 RZ_API char *rz_sys_getdir(void);
102 RZ_API bool rz_sys_chdir(RZ_NONNULL const char *s);
103 RZ_API bool rz_sys_aslr(int val);
104 RZ_API int rz_sys_cmd_str_full(const char *cmd, const char *input, char **output, int *len, char **sterr);
105 #if __WINDOWS__
106 #if UNICODE
107 #define W32_TCHAR_FSTR "%S"
108 #define W32_TCALL(name) name "W"
109 #define rz_sys_conv_utf8_to_win(buf) rz_utf8_to_utf16(buf)
110 #define rz_sys_conv_utf8_to_win_l(buf, len) rz_utf8_to_utf16_l(buf, len)
111 #define rz_sys_conv_win_to_utf8(buf) rz_utf16_to_utf8(buf)
112 #define rz_sys_conv_win_to_utf8_l(buf, len) rz_utf16_to_utf8_l((wchar_t *)buf, len)
113 #else
114 #define W32_TCHAR_FSTR "%s"
115 #define W32_TCALL(name) name "A"
116 #define rz_sys_conv_utf8_to_win(buf) rz_utf8_to_acp(buf)
117 #define rz_sys_conv_utf8_to_win_l(buf, len) rz_utf8_to_acp_l(buf, len)
118 #define rz_sys_conv_win_to_utf8(buf) rz_acp_to_utf8(buf)
119 #define rz_sys_conv_win_to_utf8_l(buf, len) rz_acp_to_utf8_l(buf, len)
120 #endif
121 typedef void *HANDLE;
122 RZ_API char *rz_sys_get_src_dir_w32(void);
123 RZ_API bool rz_sys_create_child_proc_w32(const char *cmdline, HANDLE in, HANDLE out, HANDLE err);
124 RZ_API char **rz_sys_utf8_argv_new(int argc, const wchar_t **argv);
125 RZ_API void rz_sys_utf8_argv_free(int argc, char **utf8_argv);
126 #endif
127 RZ_API int rz_sys_open_perms(int rizin_perms);
128 RZ_API int rz_sys_open(const char *path, int perm, int mode);
129 RZ_API FILE *rz_sys_fopen(const char *path, const char *mode);
131 RZ_API int rz_sys_truncate(const char *file, int sz);
132 RZ_API int rz_sys_cmdbg(const char *cmd);
133 RZ_API int rz_sys_cmdf(const char *fmt, ...) RZ_PRINTF_CHECK(1, 2);
134 RZ_API char *rz_sys_cmd_str(const char *cmd, const char *input, int *len);
136 //#define rz_sys_cmd_str(cmd, input, len) rz_sys_cmd_str_full(cmd, input, len, 0)
137 RZ_API void rz_sys_backtrace(void);
138 
139 #ifndef __has_builtin
140 #define __has_builtin(n) (0)
141 #endif
142 
143 #if __has_builtin(__builtin_debugtrap)
144 #define rz_sys_breakpoint() __builtin_debugtrap()
145 #endif
146 
147 #ifndef rz_sys_breakpoint
148 #if __WINDOWS__
149 #define rz_sys_breakpoint() \
150  { __debugbreak(); }
151 #else
152 #if __GNUC__
153 #define rz_sys_breakpoint() __builtin_trap()
154 #elif __i386__ || __x86_64__
155 #define rz_sys_breakpoint() __asm__ volatile("int3");
156 #elif __arm64__ || __aarch64__
157 #define rz_sys_breakpoint() __asm__ volatile("brk 0");
158 // #define rz_sys_breakpoint() __asm__ volatile ("brk #1");
159 #elif (__arm__ || __thumb__)
160 #if __ARM_ARCH > 5
161 #define rz_sys_breakpoint() __asm__ volatile("bkpt $0");
162 #else
163 #define rz_sys_breakpoint() __asm__ volatile("svc $1");
164 #endif
165 #elif __mips__
166 #define rz_sys_breakpoint() __asm__ volatile("break");
167 // # define rz_sys_breakpoint() __asm__ volatile ("teq $0, $0");
168 #elif __EMSCRIPTEN__
169 // TODO: cannot find a better way to breakpoint in wasm/asm.js
170 #define rz_sys_breakpoint() \
171  { \
172  char *a = NULL; \
173  *a = 0; \
174  }
175 #else
176 #warning rz_sys_breakpoint not implemented for this platform
177 #define rz_sys_trap() __asm__ __volatile__(".word 0");
178 #define rz_sys_breakpoint() \
179  { \
180  char *a = NULL; \
181  *a = 0; \
182  }
183 #endif
184 #endif
185 #endif
186 
187 /* syscmd */
188 RZ_API RZ_OWN char *rz_syscmd_ls(RZ_NONNULL const char *input);
189 RZ_API RZ_OWN char *rz_syscmd_cat(RZ_NONNULL const char *file);
190 RZ_API RZ_OWN char *rz_syscmd_mkdir(RZ_NONNULL const char *dir);
191 RZ_API RZ_OWN char *rz_syscmd_uniq(RZ_NONNULL const char *file);
192 RZ_API RZ_OWN char *rz_syscmd_head(RZ_NONNULL const char *file, int count);
193 RZ_API RZ_OWN char *rz_syscmd_tail(RZ_NONNULL const char *file, int count);
194 RZ_API RZ_OWN char *rz_syscmd_join(RZ_NONNULL const char *file1, RZ_NONNULL const char *file2);
195 RZ_API RZ_OWN char *rz_syscmd_sort(RZ_NONNULL const char *file);
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif // RZ_SYS_H
size_t len
Definition: 6502dis.c:15
si
#define e(frag)
ut16 val
Definition: armass64_const.h:6
static bool err
Definition: armass.c:435
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
static int value
Definition: cmd_api.c:93
#define RZ_API
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 count
Definition: sflib.h:98
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 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 int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
#define const
Definition: ansidecl.h:240
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
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
string FILE
Definition: benchmark.py:21
static RzSocket * s
Definition: rtr.c:28
RZ_API RZ_OWN char * rz_syscmd_join(RZ_NONNULL const char *file1, RZ_NONNULL const char *file2)
Definition: syscmd.c:367
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_cmdf(const char *fmt,...) RZ_PRINTF_CHECK(1
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 RZ_OWN char * rz_syscmd_cat(RZ_NONNULL const char *file)
Definition: syscmd.c:442
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 RZ_OWN char * rz_syscmd_sort(RZ_NONNULL const char *file)
Definition: syscmd.c:254
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 char * rz_sys_cmd_strf(const char *cmd,...) RZ_PRINTF_CHECK(1
RZ_API void rz_sys_exit(int status, bool nocleanup)
Definition: sys.c:183
RZ_API char 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_run_rop(const ut8 *buf, int len)
Definition: sys.c:861
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
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 RZ_OWN char * rz_syscmd_mkdir(RZ_NONNULL const char *dir)
Definition: syscmd.c:468
RZ_API bool rz_sys_mkdir(const char *dir)
Definition: sys.c:678
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_cmdbg(const char *cmd)
Definition: sys.c:644
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_API char * rz_sys_cmd_str(const char *cmd, const char *input, int *len)
Definition: sys.c:669
RZ_API RZ_OWN char * rz_syscmd_tail(RZ_NONNULL const char *file, int count)
Definition: syscmd.c:308
RZ_API RZ_OWN char * rz_syscmd_head(RZ_NONNULL const char *file, int count)
Definition: syscmd.c:284
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
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 RZ_OWN char * rz_syscmd_uniq(RZ_NONNULL const char *file)
Definition: syscmd.c:334
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 RZ_OWN char * rz_syscmd_ls(RZ_NONNULL const char *input)
Definition: syscmd.c:121
@ 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
RZ_API int rz_sys_arch_id(const char *arch)
Definition: sys.c:788
RZ_API int rz_sys_sigaction(int *sig, void(*handler)(int))
Definition: sys.c:162
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_PRINTF_CHECK(fmt, dots)
Definition: rz_types.h:192
char * sysname
Definition: rz_sys.h:25
char * version
Definition: rz_sys.h:28
char * machine
Definition: rz_sys.h:29
char * nodename
Definition: rz_sys.h:26
char * release
Definition: rz_sys.h:27
Definition: gzappend.c:170
const char * command
Definition: main.c:7
DWORD * HANDLE
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237