Add dummy fs.io plugin, r_io_system now returns char*

This commit is contained in:
pancake 2017-10-22 23:52:07 +02:00
parent 4a58713c56
commit ca1b44e64d
14 changed files with 208 additions and 98 deletions

View File

@ -23,6 +23,15 @@ static void set_options(RConfigNode *node, ...) {
va_end (argp);
}
static bool isGdbPlugin(RCore *core) {
if (core->io && core->io->desc && core->io->desc->plugin) {
if (core->io->desc->plugin->name && !strcmp (core->io->desc->plugin->name, "gdb")) {
return true;
}
}
return false;
}
static void print_node_options(RConfigNode *node) {
RListIter *iter;
char *option;
@ -1016,12 +1025,10 @@ static int cb_dbg_gdb_page_size(void *user, void *data) {
if (node->i_value < 64) { // 64 is hardcoded min packet size
return false;
}
if (core->io && core->io->desc && core->io->desc->plugin
&& core->io->desc->plugin->name
&& !strcmp (core->io->desc->plugin->name, "gdb")) {
if (isGdbPlugin (core)) {
char cmd[64];
snprintf (cmd, sizeof (cmd), "page_size %"PFMT64d, node->i_value);
r_io_system (core->io, cmd);
free (r_io_system (core->io, cmd));
}
return true;
}
@ -1032,12 +1039,10 @@ static int cb_dbg_gdb_retries(void *user, void *data) {
if (node->i_value <= 0) {
return false;
}
if (core->io && core->io->desc && core->io->desc->plugin
&& core->io->desc->plugin->name
&& !strcmp (core->io->desc->plugin->name, "gdb")) {
if (isGdbPlugin (core)) {
char cmd[64];
snprintf (cmd, sizeof (cmd), "retries %"PFMT64d, node->i_value);
r_io_system (core->io, cmd);
free (r_io_system (core->io, cmd));
}
return true;
}

View File

@ -470,7 +470,11 @@ static int cmd_rap(void *data, const char *input) {
core->cmdremote = input[2]? 1: 0;
r_cons_println (r_str_bool (core->cmdremote));
} else {
r_io_system (core->io, input + 1);
char *res = r_io_system (core->io, input + 1);
if (res) {
r_cons_printf ("%s\n", res);
free (res);
}
}
break;
case '$': // "=$"
@ -513,7 +517,13 @@ static int cmd_rap(void *data, const char *input) {
static int cmd_rap_run(void *data, const char *input) {
RCore *core = (RCore *)data;
return r_io_system (core->io, input);
char *res = r_io_system (core->io, input);
if (res) {
int ret = atoi (res);
free (res);
return ret;
}
return false;
}
static int cmd_yank(void *data, const char *input) {
@ -1830,7 +1840,12 @@ static int r_core_cmd_subst_i(RCore *core, char *cmd, char *colon) {
if (*cmd) {
r_core_cmd_pipe (core, cmd, ptr + 1);
} else {
r_io_system (core->io, ptr + 1);
char *res = r_io_system (core->io, ptr + 1);
if (res) {
r_cons_printf ("%s\n", res);
free (res);
}
free (res);
}
core->num->value = value;
return 0;
@ -2968,7 +2983,11 @@ R_API int r_core_cmd(RCore *core, const char *cstr, int log) {
}
if (core->cmdremote) {
if (*cstr != '=' && *cstr != 'q' && strncmp (cstr, "!=", 2)) {
r_io_system (core->io, cstr);
char *res = r_io_system (core->io, cstr);
if (res) {
r_cons_printf ("%s\n", res);
free (res);
}
goto beach; // false
}
}

87
libr/fs/p/fs_io.c Normal file
View File

@ -0,0 +1,87 @@
/* radare - LGPL - Copyright 2017 - pancake */
#include <r_fs.h>
#include <r_lib.h>
#include <sys/stat.h>
static RFSFile* fs_io_open(RFSRoot *root, const char *path) {
FILE *fd;
RFSFile *file = r_fs_file_new (root, path);
if (!file) {
return NULL;
}
file->ptr = NULL;
file->p = root->p;
fd = r_sandbox_fopen (path, "r");
if (fd) {
fseek (fd, 0, SEEK_END);
file->size = ftell (fd);
fclose (fd);
} else {
r_fs_file_free (file);
file = NULL;
}
return file;
}
static bool fs_io_read(RFSFile *file, ut64 addr, int len) {
free (file->data);
file->data = (void*)r_file_slurp_range (file->name, 0, len, NULL);
return false;
}
static void fs_io_close(RFSFile *file) {
//fclose (file->ptr);
}
static void append_file(RList *list, const char *name, int type, int time, ut64 size) {
RFSFile *fsf = r_fs_file_new (NULL, name);
if (!fsf) {
return;
}
fsf->type = type;
fsf->time = time;
fsf->size = size;
r_list_append (list, fsf);
}
static RList *fs_io_dir(RFSRoot *root, const char *path, int view /*ignored*/) {
RList *list = r_list_new ();
if (!list) {
return NULL;
}
{
// snprintf (fullpath, sizeof (fullpath)-1, "%s/%s", path, de->d_name);
append_file (list, "file-a", 'f', 0, 0);
append_file (list, "file-b", 'f', 0, 12);
}
return list;
}
static int fs_io_mount(RFSRoot *root) {
root->ptr = NULL; // XXX: TODO
return true;
}
static void fs_io_umount(RFSRoot *root) {
root->ptr = NULL;
}
RFSPlugin r_fs_plugin_io = {
.name = "io",
.desc = "r_io based filesystem",
.open = fs_io_open,
.read = fs_io_read,
.close = fs_io_close,
.dir = &fs_io_dir,
.mount = fs_io_mount,
.umount = fs_io_umount,
};
#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_FS,
.data = &r_fs_plugin_io,
.version = R2_VERSION
};
#endif

9
libr/fs/p/io.mk Normal file
View File

@ -0,0 +1,9 @@
OBJ_IO=fs_io.o
STATIC_OBJ+=${OBJ_IO}
TARGET_IO=fs_io.${EXT_SO}
ALL_TARGETS+=${TARGET_IO}
${TARGET_IO}: ${OBJ_IO}
${CC} $(call libname,fs_io) ${LDFLAGS} ${CFLAGS} -o ${TARGET_IO} ${OBJ_IO} ${EXTRA}

View File

@ -126,6 +126,7 @@ R_API const char *r_fs_partition_type_get(int n);
R_API int r_fs_partition_get_size(void); // WTF. wrong function name
/* plugins */
extern RFSPlugin r_fs_plugin_io;
extern RFSPlugin r_fs_plugin_ext2;
extern RFSPlugin r_fs_plugin_fat;
extern RFSPlugin r_fs_plugin_ntfs;

View File

@ -141,7 +141,7 @@ typedef struct r_io_plugin_t {
RIOUndo undo;
bool isdbg;
// int (*is_file_opened)(RIO *io, RIODesc *fd, const char *);
int (*system)(RIO *io, RIODesc *fd, const char *);
char *(*system)(RIO *io, RIODesc *fd, const char *);
RIODesc* (*open)(RIO *io, const char *, int rw, int mode);
RList* /*RIODesc* */ (*open_many)(RIO *io, const char *, int rw, int mode);
int (*read)(RIO *io, RIODesc *fd, ut8 *buf, int count);
@ -338,7 +338,7 @@ R_API bool r_io_read (RIO *io, ut8 *buf, int len);
R_API bool r_io_write (RIO *io, ut8 *buf, int len);
R_API ut64 r_io_size (RIO *io);
R_API bool r_io_is_listener (RIO *io);
R_API int r_io_system (RIO *io, const char* cmd);
R_API char *r_io_system (RIO *io, const char* cmd);
R_API bool r_io_resize (RIO *io, ut64 newsize);
R_API int r_io_extend_at (RIO *io, ut64 addr, ut64 size);
R_API bool r_io_set_write_mask (RIO *io, const ut8 *mask, int len);

View File

@ -558,11 +558,11 @@ R_API bool r_io_is_listener(RIO* io) {
return false;
}
R_API int r_io_system(RIO* io, const char* cmd) {
R_API char *r_io_system(RIO* io, const char* cmd) {
if (io && io->desc && io->desc->plugin && io->desc->plugin->system) {
return io->desc->plugin->system (io, io->desc, cmd);
}
return -1;
return NULL;
}
R_API bool r_io_resize(RIO* io, ut64 newsize) {

View File

@ -1,4 +1,4 @@
// Copyright (c) 2016 - LGPL, SkUaTeR, All rights reserved.
// Copyright (c) 2016-2017 - LGPL, SkUaTeR, All rights reserved.
#include <r_io.h>
#include <r_lib.h>
@ -90,7 +90,7 @@ static int __close(RIODesc *fd) {
return true;
}
static int __system(RIO *io, RIODesc *fd, const char *cmd) {
static char *__system(RIO *io, RIODesc *fd, const char *cmd) {
lprintf ("system command (%s)\n", cmd);
if (!strcmp (cmd, "help")) {
lprintf ("Usage: =!cmd args\n"
@ -99,13 +99,11 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
lprintf ("io_system: Enviando comando bochs\n");
bochs_send_cmd (desc, &cmd[1], true);
io->cb_printf ("%s\n", desc->data);
return 1;
} else if (!strncmp (cmd, "dobreak", 7)) {
bochs_cmd_stop (desc);
io->cb_printf ("%s\n", desc->data);
return 1;
}
return true;
return NULL;
}
RIOPlugin r_io_plugin_bochs = {

View File

@ -203,9 +203,9 @@ static int __gettid(RIODesc *fd) {
int send_msg(libgdbr_t* g, const char* command);
int read_packet(libgdbr_t* instance);
static int __system(RIO *io, RIODesc *fd, const char *cmd) {
static char *__system(RIO *io, RIODesc *fd, const char *cmd) {
if (!desc) {
return true;
return NULL;
}
if (!cmd[0] || cmd[0] == '?' || !strcmp (cmd, "help")) {
eprintf ("Usage: =!cmd args\n"
@ -219,37 +219,42 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
" =!pktsz bytes - set max. packet size as 'bytes' bytes\n"
" =!exec_file [pid] - get file which was executed for"
" current/specified pid\n");
return true;
return NULL;
}
if (r_str_startswith (cmd, "pktsz")) {
const char *ptr = r_str_chop_ro (cmd + 5);
if (!isdigit (*ptr)) {
io->cb_printf ("packet size: %u bytes\n",
desc->stub_features.pkt_sz);
return true;
return NULL;
}
ut32 pktsz;
if (!(pktsz = (ut32) strtoul (ptr, NULL, 10))) {
// pktsz = 0 doesn't make sense
return false;
return NULL;
}
desc->stub_features.pkt_sz = R_MAX (pktsz, 8); // min = 64
return true;
return NULL;
}
if (r_str_startswith (cmd, "detach")) {
int pid;
int res;
if (!isspace (cmd[6]) || !desc->stub_features.multiprocess) {
return gdbr_detach (desc) >= 0;
res = gdbr_detach (desc) >= 0;
} else {
int pid = 0;
cmd = r_str_chop_ro (cmd + 6);
if (!*cmd || !(pid = strtoul (cmd, NULL, 10))) {
res = gdbr_detach (desc) >= 0;
} else {
res = gdbr_detach_pid (desc, pid) >= 0;
}
}
cmd = r_str_chop_ro (cmd + 6);
if (!*cmd || !(pid = strtoul (cmd, NULL, 10))) {
return gdbr_detach (desc) >= 0;
}
return gdbr_detach_pid (desc, pid) >= 0;
eprintf ("%d\n", res);
return NULL;
}
if (r_str_startswith (cmd, "pkt ")) {
if (send_msg (desc, cmd + 4) == -1) {
return false;
return NULL;
}
int r = read_packet (desc);
desc->data[desc->data_len] = '\0';
@ -257,14 +262,15 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
if (!desc->no_ack) {
eprintf ("[waiting for ack]\n");
}
return r >= 0;
// return r >= 0;
return NULL;
}
if (r_str_startswith (cmd, "pid")) {
int pid = desc ? desc->pid : -1;
if (!cmd[3]) {
io->cb_printf ("%d\n", pid);
}
return pid;
return r_str_newf ("%d", pid);
}
if (r_str_startswith (cmd, "monitor")) {
const char *qrcmd = cmd + 8;
@ -273,40 +279,35 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
}
if (gdbr_send_qRcmd (desc, qrcmd, io->cb_printf) < 0) {
eprintf ("remote error\n");
return false;
return NULL;
}
return true;
return NULL;
}
if (r_str_startswith (cmd, "inv.reg")) {
gdbr_invalidate_reg_cache ();
return true;
return NULL;
}
if (r_str_startswith (cmd, "exec_file")) {
const char *ptr = cmd + strlen ("exec_file");
char *file;
int pid;
if (!isspace (*ptr)) {
if (!(file = gdbr_exec_file_read (desc, 0))) {
return false;
}
file = gdbr_exec_file_read (desc, 0);
} else {
while (isspace (*ptr)) {
ptr++;
}
if (isdigit (*ptr)) {
pid = atoi (ptr);
if (!(file = gdbr_exec_file_read (desc, pid))) {
return false;
}
int pid = atoi (ptr);
file = gdbr_exec_file_read (desc, pid);
} else {
if (!(file = gdbr_exec_file_read (desc, 0))) {
return false;
}
file = gdbr_exec_file_read (desc, 0);
}
}
if (!file) {
return NULL;
}
io->cb_printf ("%s\n", file);
free (file);
return true;
return file;
}
// These are internal, not available to user directly
if (r_str_startswith (cmd, "retries")) {
@ -315,10 +316,10 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
if ((num_retries = atoi (cmd + 8)) >= 1) {
desc->num_retries = num_retries;
}
return true;
return NULL;
}
io->cb_printf ("num_retries: %d bytes\n", desc->page_size);
return true;
return NULL;
}
if (r_str_startswith (cmd, "page_size")) {
int page_size;
@ -326,18 +327,18 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
if ((page_size = atoi (cmd + 10)) >= 64) {
desc->page_size = page_size;
}
return true;
return NULL;
}
io->cb_printf ("page size: %d bytes\n", desc->page_size);
return true;
return NULL;
}
// Sets a flag that next call to get memmap will be for getting baddr
if (!strcmp (cmd, "baddr")) {
desc->get_baddr = true;
return true;
return NULL;
}
eprintf ("Try: '=!?'\n");
return true;
return NULL;
}
RIOPlugin r_io_plugin_gdb = {

View File

@ -463,13 +463,13 @@ static int __close(RIODesc *fd) {
return kr == KERN_SUCCESS;
}
static int __system(RIO *io, RIODesc *fd, const char *cmd) {
static char *__system(RIO *io, RIODesc *fd, const char *cmd) {
if (!io || !fd || !cmd || !fd->data) {
return 0;
return NULL;
}
RIODescData *iodd = fd->data;
if (iodd->magic != R_MACH_MAGIC) {
return false;
return NULL;
}
task_t task = pid_to_task (iodd->tid);
@ -482,20 +482,22 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
} else {
eprintf ("Usage: =!perm [rwx]\n");
}
return 0;
return NULL;
}
if (!strncmp (cmd, "pid", 3)) {
const char *pidstr = cmd + 3;
int pid = -1;
if (*pidstr) {
// int pid = __get_pid (fd);
return 0;
return NULL;
}
if (!strcmp (pidstr, "0")) {
pid = 0;
} else {
pid = atoi (pidstr);
if (!pid) pid = -1;
if (!pid) {
pid = -1;
}
}
if (pid != -1) {
task_t task = pid_to_task (pid);
@ -504,14 +506,14 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
eprintf ("TODO: must set the pid in io here\n");
// riom->pid = pid;
// riom->task = task;
return 0;
return NULL;
}
}
eprintf ("io_mach_system: Invalid pid %d\n", pid);
} else {
eprintf ("Try: '=!pid' or '=!perm'\n");
}
return 1;
return NULL;
}
static int __get_pid (RIODesc *desc) {

View File

@ -84,19 +84,20 @@ static bool r2k__plugin_open(RIO *io, const char *pathname, bool many) {
return (!strncmp (pathname, "r2k://", 6));
}
static int r2k__system(RIO *io, RIODesc *fd, const char *cmd) {
static char *r2k__system(RIO *io, RIODesc *fd, const char *cmd) {
if (!strncmp (cmd, "mod", 3)) {
#if __WINDOWS__
GetSystemModules (io);
#endif
} else {
#if defined (__linux__) && !defined (__GNU__)
return run_ioctl_command (io, fd, cmd);
(void)run_ioctl_command (io, fd, cmd);
return NULL;
#else
eprintf ("Try: '=!mod'\n '.=!mod'\n");
#endif
}
return -1;
return NULL;
}
static RIODesc *r2k__open(RIO *io, const char *pathname, int rw, int mode) {

View File

@ -188,16 +188,16 @@ static void got_alarm(int sig) {
#endif
}
static int __system(RIO *io, RIODesc *fd, const char *cmd) {
static char *__system(RIO *io, RIODesc *fd, const char *cmd) {
if (!strcmp (cmd, "pid")) {
eprintf ("%d\n", fd->fd);
return r_str_newf ("%d", fd->fd);
} else if (!strncmp (cmd, "pid", 3)) {
/* do nothing here */
#if (!defined(__WINDOWS__)) || defined(__CYGWIN__)
} else if (!strncmp (cmd, "kill", 4)) {
if (r_sandbox_enable (false)) {
eprintf ("This is unsafe, so disabled by the sandbox\n");
return 1;
return NULL;
}
/* do nothing here */
kill (getpid (), 9);
@ -206,7 +206,7 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
size_t cbptr = 0;
if (r_sandbox_enable (false)) {
eprintf ("This is unsafe, so disabled by the sandbox\n");
return 1;
return NULL;
}
ut64 result = 0;
char *argv = strdup (cmd + 5);
@ -214,7 +214,7 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
if (argc == 0) {
eprintf ("Usage: =!call [fcnptr] [a0] [a1] ...\n");
free (argv);
return 0;
return NULL;
}
const char *sym = r_str_word_get0 (argv, 0);
if (sym) {
@ -344,7 +344,7 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
eprintf ("| =!call [sym] [...] nativelly call a function\n");
eprintf ("| =!mameio enter mame IO mode\n");
}
return 0;
return NULL;
}
RIOPlugin r_io_plugin_self = {

View File

@ -164,7 +164,7 @@ static struct winedbg_x86_32 regState() {
return r;
}
static int __system(RIO *io, RIODesc *fd, const char *cmd) {
static char *__system(RIO *io, RIODesc *fd, const char *cmd) {
if (!strncmp (cmd, "?", 1)) {
eprintf ("dr : show registers\n");
eprintf ("dr* : show registers as flags\n");
@ -175,16 +175,14 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
eprintf ("dc : continue\n");
eprintf ("dm : show maps\n");
eprintf ("pid : show current process id\n");
return 0;
} else if (!strncmp (cmd, "dr8", 3)) {
struct winedbg_x86_32 r = regState ();
ut8 *arena = (ut8*)calloc (sizeof (struct winedbg_x86_32), 3);
if (!arena) {
return 0;
if (arena) {
r_hex_bin2str ((ut8*)&r, sizeof (r), (char *)arena);
io->cb_printf ("%s\n", arena);
free (arena);
}
r_hex_bin2str ((ut8*)&r, sizeof (r), (char *)arena);
io->cb_printf ("%s\n", arena);
free (arena);
} else if (!strncmp (cmd, "drp", 3)) {
const char *msg =
"=PC eip\n"\
@ -230,41 +228,29 @@ const char *msg =
"flg nt .1 .201 0\n"\
"flg rf .1 .202 0\n"\
"flg vm .1 .203 0\n";
io->cb_printf ("%s", msg);
return 0;
return strdup (msg);
} else if (!strncmp (cmd, "dr", 2)) {
printcmd (io, "info reg");
return 0;
} else if (!strncmp (cmd, "db ", 3)) {
free (runcmd (sdb_fmt (0, "break *%"PFMT64x, r_num_get (NULL, cmd + 3) || io->off)));
return 0;
} else if (!strncmp (cmd, "ds", 2)) {
free (runcmd ("stepi"));
return 0;
} else if (!strncmp (cmd, "dc", 2)) {
free (runcmd ("cont"));
return 0;
} else if (!strncmp (cmd, "dso", 3)) {
eprintf ("TODO: dso\n");
return 0;
} else if (!strncmp (cmd, "dp", 3)) {
printcmd (io, "info thread");
return 0;
} else if (!strncmp (cmd, "dm", 3)) {
printcmd (io, "info maps");
return 0;
} else if (!strncmp (cmd, "pid", 3)) {
int pid = fd->fd;
if (!cmd[3]) {
io->cb_printf ("%d\n", pid);
}
return pid;
return r_str_newf ("%d", fd->fd);
} else {
char *res = runcmd (cmd);
io->cb_printf ("%s\n", res);
free (res);
}
return 0;
return NULL;
}
RIOPlugin r_io_plugin_winedbg = {

View File

@ -179,6 +179,7 @@ debug.bochs
debug.null
egg.exec
egg.xor
fs.io
fs.ext2
fs.fat
fs.fb