static void parseline (char *b) { char *e = strchr (b, '='); if (!e) return; if (*b=='#') return; *e++ = 0; if (*e=='$') e = r_sys_getenv (e); if (e == NULL) return; if (!strcmp (b, "program")) _args[0] = _program = strdup (e); else if (!strcmp (b, "connect")) _connect = strdup (e); else if (!strcmp (b, "listen")) _listen = strdup (e); else if (!strcmp (b, "stdout")) _stdout = strdup (e); else if (!strcmp (b, "stdin")) _stdin = strdup (e); else if (!strcmp (b, "input")) _input = strdup (e); else if (!strcmp (b, "chdir")) _chgdir = strdup (e); else if (!strcmp (b, "chroot")) _chroot = strdup (e); else if (!strcmp (b, "preload")) _preload = strdup (e); else if (!strcmp (b, "setuid")) _setuid = strdup (e); else if (!strcmp (b, "seteuid")) _seteuid = strdup (e); else if (!strcmp (b, "setgid")) _setgid = strdup (e); else if (!strcmp (b, "setegid")) _setegid = strdup (e); else if (!memcmp (b, "arg", 3)) { int n = atoi (b+3); if (n>=0 && n<NARGS) { _args[n] = strdup (e); r_str_escape (_args[n]); } else eprintf ("Out of bounds args index: %d\n", n); } else if (!strcmp (b, "timeout")) _timeout = atoi (e); else if (!strcmp (b, "setenv")) { char *v = strchr (e, '='); if (v) { *v++ = 0; r_sys_setenv (e, v); } } }
R_API void r_egg_lang_include_path (REgg *egg, const char *path) { char *env = r_sys_getenv (R_EGG_INCDIR_ENV); if (!env || !*env) { r_egg_lang_include_init (egg); env = r_sys_getenv (R_EGG_INCDIR_ENV); } env = r_str_concatf (NULL, "%s:%s", path, env); r_sys_setenv (R_EGG_INCDIR_ENV, env); free (env); }
static void _libwrap_init() { char *web; signal (SIGUSR1, sigusr1); signal (SIGUSR2, sigusr2); printf ("libr2 initialized. send SIGUSR1 to %d in order to reach the r2 prompt\n", getpid ()); printf ("kill -USR1 %d\n", getpid ()); fflush (stdout); web = r_sys_getenv ("RARUN2_WEB"); core = r_core_new (); r_core_loadlibs (core, R_CORE_LOADLIBS_ALL, NULL); if (web) { r_core_cmd0 (core, "=H&"); r_sys_setenv ("RARUN2_WEB", NULL); free (web); } // TODO: maybe reopen every time a signal is spawned to reload memory regions information // TODO: open io_self }
static int lang_cpipe_file(RLang *lang, const char *file) { char *a, *cc, *p, name[512]; const char *libpath, *libname; if (strlen (file) > (sizeof (name)-10)) return false; if (!strstr (file, ".c")) sprintf (name, "%s.c", file); else strcpy (name, file); if (!r_file_exists (name)) { eprintf ("file not found (%s)\n", name); return false; } a = (char*)r_str_lchr (name, '/'); if (a) { *a = 0; libpath = name; libname = a + 1; } else { libpath = "."; libname = name; } r_sys_setenv ("PKG_CONFIG_PATH", R2_LIBDIR"/pkgconfig"); p = strstr (name, ".c"); if (p) *p = 0; cc = r_sys_getenv ("CC"); if (!cc || !*cc) { free (cc); cc = strdup ("gcc"); } char *buf = r_str_newf ("%s %s -o %s/bin%s" " $(pkg-config --cflags --libs r_socket)", cc, file, libpath, libname); free (cc); if (r_sandbox_system (buf, 1) == 0) { char *binfile = r_str_newf ("%s/bin%s", libpath, libname); lang_pipe_run (lang, binfile, -1); r_file_rm (binfile); free (binfile); } free (buf); return 0; }
static int cmd_eval(void *data, const char *input) { char *p; RCore *core = (RCore *)data; switch (input[0]) { case 't': // env if (input[1]==' ' && input[2]) { RConfigNode *node = r_config_node_get (core->config, input+2); if (node) { const char *type = r_config_node_type (node); if (type && *type) { r_cons_printf ("%s\n", type); } } } else { eprintf ("Usage: et [varname] ; show type of eval var\n"); } break; case 'n': // env if (!strchr (input, '=')) { char *var, *p; var = strchr (input, ' '); if (var) while (*var==' ') var++; p = r_sys_getenv (var); if (p) { r_cons_printf ("%s\n", p); free (p); } else { char **e = r_sys_get_environ (); while (e && *e) { r_cons_printf ("%s\n", *e); e++; } } } else if (strlen (input)>3) { char *v, *k = strdup (input+3); if (!k) break; v = strchr (k, '='); if (v) { *v++ = 0; r_sys_setenv (k, v); } free (k); } return true; case 'x': // exit return cmd_quit (data, ""); case 'j': r_config_list (core->config, NULL, 'j'); break; case '\0': r_config_list (core->config, NULL, 0); break; case 'c': switch (input[1]) { case 'h': // echo if (( p = strchr (input, ' ') )) { r_cons_strcat (p+1); r_cons_newline (); } break; case 'd': r_cons_pal_init (NULL); break; case '?': { const char *helpmsg[] = { "Usage ec[s?] [key][[=| ]fg] [bg]","","", "ec","","list all color keys", "ec*","","same as above, but using r2 commands", "ecd","","set default palette", "ecr","","set random palette", "ecs","","show a colorful palette", "ecj","","show palette in JSON", "ecc","","show palette in CSS", "eco"," dark|white","load white color scheme template", "ecn","","load next color theme", "ec"," prompt red","change color of prompt", "ec"," prompt red blue","change color and background of prompt", ""," ","", "colors:","","rgb:000, red, green, blue, ...", "e scr.rgbcolor","=1|0","for 256 color cube (boolean)", "e scr.truecolor","=1|0","for 256*256*256 colors (boolean)", "$DATADIR/radare2/cons","","~/.config/radare2/cons ./", NULL}; r_core_cmd_help (core, helpmsg); } break; case 'o': // "eco" if (input[2] == ' ') { bool failed = false; char *home, path[512]; snprintf (path, sizeof (path), ".config/radare2/cons/%s", input+3); home = r_str_home (path); snprintf (path, sizeof (path), R2_DATDIR"/radare2/" R2_VERSION"/cons/%s", input+3); if (!r_core_cmd_file (core, home)) { if (r_core_cmd_file (core, path)) { //curtheme = r_str_dup (curtheme, path); curtheme = r_str_dup (curtheme, input + 3); } else { if (r_core_cmd_file (core, input+3)) { curtheme = r_str_dup (curtheme, input + 3); } else { eprintf ("eco: cannot open colorscheme profile (%s)\n", path); failed = true; } } } free (home); } else { nextpal (core, 'l'); } break; case 's': r_cons_pal_show (); break; case '*': r_cons_pal_list (1); break; case 'j': r_cons_pal_list ('j'); break; case 'c': r_cons_pal_list ('c'); break; case '\0': r_cons_pal_list (0); break; case 'r': // "ecr" r_cons_pal_random (); break; case 'n': // "ecn" nextpal (core, 'n'); break; default: { char *p = strdup (input + 2); char *q = strchr (p, '='); if (!q) q = strchr (p, ' '); if (q) { // set *q++ = 0; r_cons_pal_set (p, q); } else { const char *k = r_cons_pal_get (p); if (k) eprintf ("(%s)(%sCOLOR"Color_RESET")\n", p, k); } free (p); } } break; case 'e': if (input[1]==' ') { char *p; const char *val, *input2 = strchr (input+2, ' '); if (input2) input2++; else input2 = input+2; val = r_config_get (core->config, input2); p = r_core_editor (core, NULL, val); if (p) { r_str_replace_char (p, '\n', ';'); r_config_set (core->config, input2, p); } } else eprintf ("Usage: ee varname\n"); break; case '!': input = r_str_chop_ro (input+1); if (!r_config_toggle (core->config, input)) eprintf ("r_config: '%s' is not a boolean variable.\n", input); break; case '-': r_core_config_init (core); //eprintf ("BUG: 'e-' command locks the eval hashtable. patches are welcome :)\n"); break; case 'v': eprintf ("Invalid command '%s'. Use 'e?'\n", input); break; case '*': r_config_list (core->config, NULL, 1); break; case '?': switch (input[1]) { case '?': r_config_list (core->config, input+2, 2); break; default: r_config_list (core->config, input+1, 2); break; case 0:{ const char* help_msg[] = { "Usage:", "e[?] [var[=value]]", "Evaluable vars", "e","?asm.bytes", "show description", "e", "??", "list config vars with description", "e", "", "list config vars", "e-", "", "reset config vars", "e*", "", "dump config vars in r commands", "e!", "a", "invert the boolean value of 'a' var", "ee", "var", "open editor to change the value of var", "er", " [key]", "set config key as readonly. no way back", "ec", " [k] [color]", "set color for given key (prompt, offset, ...)", "et", " [key]", "show type of given config variable", "e", " a", "get value of var 'a'", "e", " a=b", "set var 'a' the 'b' value", "env", " [k[=v]]", "get/set environment variable", NULL}; r_core_cmd_help (core, help_msg); } } break; case 'r': if (input[1]) { const char *key = input+((input[1]==' ')?2:1); if (!r_config_readonly (core->config, key)) eprintf ("cannot find key '%s'\n", key); } else eprintf ("Usage: er [key]\n"); break; case ' ': r_config_eval (core->config, input+1); break; default: r_config_eval (core->config, input); break; } return 0; }
/* TODO: split this function into several ones..quite long fun */ static void rcc_next(REgg *egg) { const char *ocn; REggEmit *e = egg->remit; char *str = NULL, *p, *ptr, buf[64]; int i; if (setenviron) { elem[elem_n - 1] = 0; r_sys_setenv (setenviron, elem); R_FREE (setenviron); return; } if (includefile) { char *p, *q, *path; // TODO: add support for directories elem[elem_n-1] = 0; path = find_include (elem, includefile); if (!path) { eprintf ("Cannot find include file '%s'\n", elem); return; } free (includefile); includefile = NULL; rcc_reset_callname (); p = q = r_file_slurp (path, NULL); if (p) { int oline = ++line; elem[0] = 0; // TODO: this must be a separate function elem_n = 0; line = 0; for (; *p; p++) r_egg_lang_parsechar (egg, *p); free (q); line = oline; } else { eprintf ("Cannot find '%s'\n", path); } free (path); return; } docall = 1; if (callname) { if (!strcmp (callname, "goto")) { if (nargs != 1) { eprintf ("Invalid number of arguments for goto()\n"); return; } e->jmp (egg, ctxpush[CTX], 0); rcc_reset_callname (); return; } if (!strcmp (callname, "break")) { e->trap (egg); rcc_reset_callname (); return; } ptr = strchr (callname, '='); if (ptr) { *ptr = '\0'; //ocn = ptr+1; // what is the point of this? } ocn = skipspaces (callname); if (!ocn) return; str = r_egg_mkvar (egg, buf, ocn, 0); if (!str) { eprintf ("Cannot mkvar\n"); return; } if (*ocn=='.') e->call (egg, str, 1); if (!strcmp (str, "while")) { char var[128]; if (lastctxdelta >= 0) exit (eprintf ("ERROR: Unsupported while syntax\n")); sprintf (var, "__begin_%d_%d_%d\n", nfunctions, CTX, nestedi[CTX-1]); e->while_end (egg, var); //get_frame_label (1)); #if 0 eprintf ("------------------------------------------ lastctx: %d\n", lastctxdelta); // TODO: the pushvar is required for the if(){}while(); constructions //char *pushvar = ctxpush[context+nbrackets-1]; /* TODO: support to compare more than one expression (LOGICAL OR) */ rcc_printf (" pop %%eax\n"); rcc_printf (" cmp $0, %%eax\n"); // XXX MUST SUPPORT != 0 COMPARE HERE /* TODO : Simplify!! */ //if (pushvar) // printf(" push %s /* wihle push */\n", pushvar); if (lastctxdelta<0) rcc_printf (" jnz %s\n", get_frame_label (1)); else rcc_printf (" jnz %s\n", get_frame_label (0)); //if (pushvar) // printf(" pop %%"R_AX" /* while pop */\n"); #endif nargs = 0; } else { for (i = 0; i < nsyscalls; i++) { if (!strcmp (str, syscalls[i].name)) { p = syscallbody; e->comment (egg, "set syscall args"); e->syscall_args (egg, nargs); docall = 0; e->comment (egg, "syscall"); r_egg_lang_parsechar (egg, '\n'); /* FIX parsing issue */ if (p) { for (; *p; p++) r_egg_lang_parsechar (egg, *p); } else { char *q, *s = e->syscall (egg, nargs); if (s) { for (q=s; *q; q++) r_egg_lang_parsechar (egg, *q); free (s); } else eprintf ("Cannot get @syscall payload\n"); } docall = 0; break; } } if (docall) for (i = 0; i < ninlines; i++) { if (!strcmp (str, inlines[i].name)) { p = inlines[i].body; docall = 0; e->comment (egg, "inline"); r_egg_lang_parsechar (egg, '\n'); /* FIX parsing issue */ for (; *p; p++) r_egg_lang_parsechar (egg, *p); docall = 0; break; } } if (docall) { e->comment (egg, "call in mode %d", mode); e->call (egg, str, 0); } } if (nargs > 0) e->restore_stack (egg, nargs*e->size); if (ocn) { // Used to call .var0() /* XXX: Probably buggy and wrong */ *buf = 0; free (str); str = r_egg_mkvar (egg, buf, ocn, 0); if (*buf) e->get_result (egg, buf); //else { eprintf("external symbol %s\n", ocn); } } /* store result of call */ if (dstvar) { if (mode != NAKED) { *buf = 0; free (str); str = r_egg_mkvar (egg, buf, dstvar, 0); if (*buf == 0) eprintf ("Cannot resolve variable '%s'\n", dstvar); else e->get_result (egg, buf); } R_FREE (dstvar); } rcc_reset_callname (); } else { int vs = 'l'; char type, *eq, *ptr = elem; elem[elem_n] = '\0'; ptr = (char*)skipspaces (ptr); if (*ptr) { eq = strchr (ptr, '='); if (eq) { char str2[64], *p, ch = *(eq-1); *eq = '\0'; eq = (char*) skipspaces (eq+1); p = r_egg_mkvar (egg, str2, ptr, 0); vs = varsize; if (is_var (eq)) { eq = r_egg_mkvar (egg, buf, eq, 0); if (varxs=='*') e->load (egg, eq, varsize); else /* XXX this is a hack .. must be integrated with pusharg */ if (varxs=='&') e->load_ptr (egg, eq); if (eq) { free (eq); eq = NULL; } type = ' '; } else type = '$'; vs = 'l'; // XXX: add support for != 'l' size e->mathop (egg, ch, vs, type, eq, p); free(p); } else { if (!strcmp (ptr, "break")) { // handle 'break;' e->trap (egg); rcc_reset_callname (); } else { e->mathop (egg, '=', vs, '$', ptr, NULL); } } } } free (str); }
R_API void r_egg_lang_include_init (REgg *egg) { r_sys_setenv (R_EGG_INCDIR_ENV, ".:"R_EGG_INCDIR_PATH); }
static void parseline (char *b) { char *e = strchr (b, '='); if (!e) return; if (*b=='#') return; *e++ = 0; if (*e=='$') e = r_sys_getenv (e); if (e == NULL) return; if (!strcmp (b, "program")) _args[0] = _program = strdup (e); else if (!strcmp (b, "system")) _system = strdup (e); else if (!strcmp (b, "connect")) _connect = strdup (e); else if (!strcmp (b, "listen")) _listen = strdup (e); else if (!strcmp (b, "stdout")) _stdout = strdup (e); else if (!strcmp (b, "stdio")) { _stdout = _stderr = _stdin = strdup (e); } else if (!strcmp (b, "stdin")) _stdin = strdup (e); else if (!strcmp (b, "stderr")) _stderr = strdup (e); else if (!strcmp (b, "input")) _input = strdup (e); else if (!strcmp (b, "chdir")) _chgdir = strdup (e); else if (!strcmp (b, "chroot")) _chroot = strdup (e); else if (!strcmp (b, "libpath")) _libpath = strdup (e); else if (!strcmp (b, "preload")) _preload = strdup (e); else if (!strcmp (b, "r2preload")) _r2preload = \ (strcmp (e, "yes")? (strcmp (e, "true")? (strcmp (e, "1")? 0: 1): 1): 1); else if (!strcmp (b, "setuid")) _setuid = strdup (e); else if (!strcmp (b, "seteuid")) _seteuid = strdup (e); else if (!strcmp (b, "setgid")) _setgid = strdup (e); else if (!strcmp (b, "setegid")) _setegid = strdup (e); else if (!memcmp (b, "arg", 3)) { int n = atoi (b+3); if (n>=0 && n<NARGS) { _args[n] = getstr (e); } else eprintf ("Out of bounds args index: %d\n", n); } else if (!strcmp (b, "timeout")) { _timeout = atoi (e); } else if (!strcmp (b, "envfile")) { char *p, buf[1024]; FILE *fd = fopen (e, "r"); if (!fd) { eprintf ("Cannot open '%s'\n", e); return; } for (;;) { fgets (buf, sizeof (buf)-1, fd); if (feof (fd)) break; p = strchr (buf, '='); if (p) { *p = 0; r_sys_setenv (buf, p+1); } } fclose (fd); } else if (!strcmp (b, "unsetenv")) { r_sys_setenv (e, NULL); } else if (!strcmp (b, "setenv")) { char *v = strchr (e, '='); if (v) { *v++ = 0; r_sys_setenv (e, v); } } }
static int runfile () { if (!_program && !_system) { printf ("No program or system rule defined\n"); return 1; } if (_stdin) { int f = open (_stdin, O_RDONLY); close (0); dup2 (f, 0); } if (_stdout) { int f = open (_stdout, O_WRONLY); close (1); dup2 (f, 1); } if (_stderr) { int f = open (_stderr, O_WRONLY); close (2); dup2 (f, 2); } if (_connect) { char *p = strchr (_connect, ':'); if (p) { RSocket *fd = r_socket_new (0); *p=0; if (!r_socket_connect_tcp (fd, _connect, p+1, 30)) { eprintf ("Cannot connect\n"); return 1; } eprintf ("connected\n"); close (0); close (1); close (2); dup2 (fd->fd, 0); dup2 (fd->fd, 1); dup2 (fd->fd, 2); } else { eprintf ("Invalid format for connect. missing ':'\n"); return 1; } } if (_listen) { RSocket *child, *fd = r_socket_new (0); if (!r_socket_listen (fd, _listen, NULL)) { eprintf ("rarun2: cannot listen\n"); return 1; } child = r_socket_accept (fd); if (child) { eprintf ("connected\n"); close (0); close (1); close (2); dup2 (child->fd, 0); dup2 (child->fd, 1); dup2 (child->fd, 2); } } if (_chgdir) chdir (_chgdir); if (_chroot) chdir (_chroot); #if __UNIX__ if (_chroot) { if (chroot (".")) { eprintf ("rarun2: cannot chroot\n"); return 1; } } if (_setuid) setuid (atoi (_setuid)); if (_seteuid) seteuid (atoi (_seteuid)); if (_setgid) setgid (atoi (_setgid)); if (_input) { int f2[2]; pipe (f2); close (0); dup2 (f2[0], 0); parseinput (_input); write (f2[1], _input, strlen (_input)); } #endif if (_r2preload) { if (_preload) { eprintf ("WARNING: Only one library can be opened at a time\n"); } _preload = R2_LIBDIR"/libr2."R_LIB_EXT; } if (_libpath) { #if __WINDOWS__ eprintf ("rarun2: libpath unsupported for this platform\n"); #elif __APPLE__ r_sys_setenv ("DYLD_LIBRARY_PATH", _libpath); #else r_sys_setenv ("LD_LIBRARY_PATH", _libpath); #endif } if (_preload) { #if __APPLE__ // 10.6 r_sys_setenv ("DYLD_PRELOAD", _preload); r_sys_setenv ("DYLD_INSERT_LIBRARIES", _preload); // 10.8 r_sys_setenv ("DYLD_FORCE_FLAT_NAMESPACE", "1"); #else r_sys_setenv ("LD_PRELOAD", _preload); #endif } if (_timeout) { #if __UNIX__ int mypid = getpid (); if (!fork ()) { sleep (_timeout); if (!kill (mypid, 0)) eprintf ("\nrarun2: Interrupted by timeout\n"); kill (mypid, SIGKILL); exit (0); } #else eprintf ("timeout not supported for this platform\n"); #endif } if (_system) { exit (r_sys_cmd (_system)); } if (!r_file_exists (_program)) { eprintf ("rarun2: %s: file not found\n", _program); return 1; } exit (execv (_program, _args)); }
R_API int r_run_start(RRunProfile *p) { #if __APPLE__ posix_spawnattr_t attr = {0}; pid_t pid = -1; #endif int ret; if (!p->_program && !p->_system) { printf ("No program or system rule defined\n"); return 1; } if (p->_stdin) { int f = open (p->_stdin, O_RDONLY); if (f < 0) return 1; close (0); dup2 (f, 0); } if (p->_stdout) { int f = open (p->_stdout, O_WRONLY); if (f < 0) return 1; close (1); dup2 (f, 1); } if (p->_stderr) { int f = open (p->_stderr, O_WRONLY); if (f < 0) return 1; close (2); dup2 (f, 2); } if (p->_aslr != -1) setASLR (p->_aslr); #if __UNIX__ set_limit (p->_docore, RLIMIT_CORE, RLIM_INFINITY); if (p->_maxfd) set_limit (p->_maxfd, RLIMIT_NOFILE, p->_maxfd); #ifdef RLIMIT_NPROC if (p->_maxproc) set_limit (p->_maxproc, RLIMIT_NPROC, p->_maxproc); #endif if (p->_maxstack) set_limit (p->_maxstack, RLIMIT_STACK, p->_maxstack); #else if (p->_docore || p->_maxfd || p->_maxproc || p->_maxstack) eprintf ("Warning: setrlimits not supported for this platform\n"); #endif if (p->_connect) { char *q = strchr (p->_connect, ':'); if (q) { RSocket *fd = r_socket_new (0); *q = 0; if (!r_socket_connect_tcp (fd, p->_connect, q+1, 30)) { eprintf ("Cannot connect\n"); return 1; } eprintf ("connected\n"); close (0); close (1); close (2); dup2 (fd->fd, 0); dup2 (fd->fd, 1); dup2 (fd->fd, 2); } else { eprintf ("Invalid format for connect. missing ':'\n"); return 1; } } if (p->_listen) { RSocket *child, *fd = r_socket_new (0); if (!r_socket_listen (fd, p->_listen, NULL)) { eprintf ("rarun2: cannot listen\n"); r_socket_free (fd); return 1; } child = r_socket_accept (fd); if (child) { eprintf ("connected\n"); close (0); close (1); close (2); dup2 (child->fd, 0); dup2 (child->fd, 1); dup2 (child->fd, 2); } } if (p->_r2sleep != 0) { r_sys_sleep (p->_r2sleep); } if (p->_chgdir) { ret = chdir (p->_chgdir); if (ret < 0) return 1; } if (p->_chroot) { ret = chdir (p->_chroot); if (ret < 0) return 1; } #if __UNIX__ if (p->_chroot) { if (chroot (p->_chroot)) { eprintf ("rarun2: cannot chroot\n"); return 1; } chdir("/"); } if (p->_setuid) { ret = setgroups(0, NULL); if (ret < 0) return 1; ret = setuid (atoi (p->_setuid)); if (ret < 0) return 1; } if (p->_seteuid) { ret = seteuid (atoi (p->_seteuid)); if (ret < 0) return 1; } if (p->_setgid) { ret = setgid (atoi (p->_setgid)); if (ret < 0) return 1; } if (p->_input) { int f2[2]; pipe (f2); close (0); dup2 (f2[0], 0); parseinput (p->_input); write (f2[1], p->_input, strlen (p->_input)); } #endif if (p->_r2preload) { if (p->_preload) { eprintf ("WARNING: Only one library can be opened at a time\n"); } p->_preload = R2_LIBDIR"/libr2."R_LIB_EXT; } if (p->_libpath) { #if __WINDOWS__ eprintf ("rarun2: libpath unsupported for this platform\n"); #elif __HAIKU__ r_sys_setenv ("LIBRARY_PATH", p->_libpath); #elif __APPLE__ r_sys_setenv ("DYLD_LIBRARY_PATH", p->_libpath); #else r_sys_setenv ("LD_LIBRARY_PATH", p->_libpath); #endif } if (p->_preload) { #if __APPLE__ // 10.6 r_sys_setenv ("DYLD_PRELOAD", p->_preload); r_sys_setenv ("DYLD_INSERT_LIBRARIES", p->_preload); // 10.8 r_sys_setenv ("DYLD_FORCE_FLAT_NAMESPACE", "1"); #else r_sys_setenv ("LD_PRELOAD", p->_preload); #endif } if (p->_timeout) { #if __UNIX__ int mypid = getpid (); if (!fork ()) { sleep (p->_timeout); if (!kill (mypid, 0)) eprintf ("\nrarun2: Interrupted by timeout\n"); kill (mypid, SIGKILL); exit (0); } #else eprintf ("timeout not supported for this platform\n"); #endif } #if __APPLE__ posix_spawnattr_init (&attr); if (p->_args[0]) { char **envp = r_sys_get_environ(); ut32 spflags = 0; //POSIX_SPAWN_START_SUSPENDED; spflags |= POSIX_SPAWN_SETEXEC; if (p->_aslr == 0) { #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 spflags |= _POSIX_SPAWN_DISABLE_ASLR; } (void)posix_spawnattr_setflags (&attr, spflags); if (p->_bits) { size_t copied = 1; cpu_type_t cpu; #if __i386__ || __x86_64__ cpu = CPU_TYPE_I386; if (p->_bits == 64) cpu |= CPU_ARCH_ABI64; #else cpu = CPU_TYPE_ANY; #endif posix_spawnattr_setbinpref_np ( &attr, 1, &cpu, &copied); } ret = posix_spawnp (&pid, p->_args[0], NULL, &attr, p->_args, envp); switch (ret) { case 0: break; case 22: eprintf ("posix_spawnp: Invalid argument\n"); break; case 86: eprintf ("posix_spawnp: Unsupported architecture\n"); break; default: eprintf ("posix_spawnp: unknown error %d\n", ret); perror ("posix_spawnp"); break; } exit (ret); } #endif if (p->_system) { if (p->_pid) { eprintf ("PID: Cannot determine pid with 'system' directive. Use 'program'.\n"); } exit (r_sys_cmd (p->_system)); } if (p->_program) { if (!r_file_exists (p->_program)) { char *progpath = r_file_path (p->_program); if (progpath && *progpath) { free (p->_program); p->_program = progpath; } else { free (progpath); eprintf ("rarun2: %s: file not found\n", p->_program); return 1; } } #if __UNIX__ // XXX HACK close all non-tty fds { int i; for (i=3; i<10; i++) close (i); } // TODO: use posix_spawn if (p->_setgid) { ret = setgid (atoi (p->_setgid)); if (ret < 0) return 1; } if (p->_pid) { eprintf ("PID: %d\n", getpid ()); } if (p->_pidfile) { char pidstr[32]; snprintf (pidstr, sizeof (pidstr), "%d\n", getpid ()); r_file_dump (p->_pidfile, (const ut8*)pidstr, strlen (pidstr), 0); } #endif if (p->_nice) { #if __UNIX__ errno = 0; ret = nice(p->_nice); if (ret == -1) { if (errno != 0) { return 1; } } #else eprintf ("nice not supported for this platform\n"); #endif } exit (execv (p->_program, (char* const*)p->_args)); } return 0; }
static int cmd_eval(void *data, const char *input) { char *p; RCore *core = (RCore *)data; switch (input[0]) { case 't': // env if (input[1] == 'a') { r_cons_printf ("%s\n", (r_num_rand (10) % 2)? "wen": "son"); } else if (input[1]==' ' && input[2]) { RConfigNode *node = r_config_node_get (core->config, input+2); if (node) { const char *type = r_config_node_type (node); if (type && *type) { r_cons_println (type); } } } else { eprintf ("Usage: et [varname] ; show type of eval var\n"); } break; case 'n': // env if (!strchr (input, '=')) { char *var, *p; var = strchr (input, ' '); if (var) while (*var==' ') var++; p = r_sys_getenv (var); if (p) { r_cons_println (p); free (p); } else { char **e = r_sys_get_environ (); while (e && *e) { r_cons_println (*e); e++; } } } else if (strlen (input)>3) { char *v, *k = strdup (input+3); if (!k) break; v = strchr (k, '='); if (v) { *v++ = 0; r_sys_setenv (k, v); } free (k); } return true; case 'x': // exit // XXX we need headers for the cmd_xxx files. return cmd_quit (data, ""); case 'j': // json r_config_list (core->config, NULL, 'j'); break; case 'v': // verbose r_config_list (core->config, input + 1, 'v'); break; case 'q': // quiet list of eval keys r_config_list (core->config, NULL, 'q'); break; case '\0': // "e" r_config_list (core->config, NULL, 0); break; case 'c': // "ec" switch (input[1]) { case 'd': r_cons_pal_init (NULL); break; case '?': { const char *helpmsg[] = { "Usage ec[s?] [key][[=| ]fg] [bg]","","", "ec","","list all color keys", "ec*","","same as above, but using r2 commands", "ecd","","set default palette", "ecr","","set random palette (see also scr.randpal)", "ecs","","show a colorful palette", "ecj","","show palette in JSON", "ecc"," [prefix]","show palette in CSS", "eco"," dark|white","load white color scheme template", "ecp","","load previous color theme", "ecn","","load next color theme", "ecH","[?]","highlight word or instruction", "ec"," prompt red","change color of prompt", "ec"," prompt red blue","change color and background of prompt", ""," ","", "colors:","","rgb:000, red, green, blue, ...", "e scr.rgbcolor","=1|0","for 256 color cube (boolean)", "e scr.truecolor","=1|0","for 256*256*256 colors (boolean)", "$DATADIR/radare2/cons","","~/.config/radare2/cons ./", NULL}; r_core_cmd_help (core, helpmsg); } break; case 'o': // "eco" if (input[2] == 'j') { nextpal (core, 'j'); } else if (input[2] == ' ') { bool failed = false; char *home, path[512]; snprintf (path, sizeof (path), ".config/radare2/cons/%s", input + 3); home = r_str_home (path); snprintf (path, sizeof (path), R2_DATDIR"/radare2/" R2_VERSION"/cons/%s", input + 3); if (!load_theme (core, home)) { if (load_theme (core, path)) { //curtheme = r_str_dup (curtheme, path); curtheme = r_str_dup (curtheme, input + 3); } else { if (load_theme (core, input + 3)) { curtheme = r_str_dup (curtheme, input + 3); } else { char *absfile = r_file_abspath (input + 3); eprintf ("eco: cannot open colorscheme profile (%s)\n", absfile); free (absfile); failed = true; } } } free (home); if (failed) { eprintf ("Something went wrong\n"); } } else if (input[2] == '?') { eprintf ("Usage: eco [themename] ;load theme from "R2_DATDIR"/radare2/"R2_VERSION"/cons/\n"); } else { nextpal (core, 'l'); } break; case 's': r_cons_pal_show (); break; // "ecs" case '*': r_cons_pal_list (1, NULL); break; // "ec*" case 'h': // echo if (( p = strchr (input, ' ') )) { r_cons_strcat (p+1); r_cons_newline (); } else { // "ech" r_cons_pal_list ('h', NULL); } break; case 'j': // "ecj" r_cons_pal_list ('j', NULL); break; case 'c': // "ecc" r_cons_pal_list ('c', input + 2); break; case '\0': // "ec" r_cons_pal_list (0, NULL); break; case 'r': // "ecr" r_cons_pal_random (); break; case 'n': // "ecn" nextpal (core, 'n'); break; case 'p': // "ecp" nextpal (core, 'p'); break; case 'H': { // "ecH" char *color_code = NULL; char *word = NULL; int argc = 0; char** argv = r_str_argv (input + 4, &argc); switch (input[2]) { case '?': { const char *helpmsg[] = { "Usage ecH[iw-?]","","", "ecHi","[color]","highlight current instruction with 'color' background", "ecHw","[word] [color]","highlight 'word ' in current instruction with 'color' background", "ecH-","","remove all highlights on current instruction", NULL }; r_core_cmd_help (core, helpmsg); } break; case '-': r_meta_set_string (core->anal, R_META_TYPE_HIGHLIGHT, core->offset, ""); return false; case '\0': case 'i': // "ecHi if (argc) { char *dup = r_str_newf ("bgonly %s", argv[0]); color_code = r_cons_pal_parse (dup); R_FREE (dup); } break; case 'w': // "ecHw" if (!argc) { eprintf ("Usage: echw word [color]\n"); r_str_argv_free (argv); return true; } word = strdup (argv[0]); if (argc > 1) { char *dup = r_str_newf ("bgonly %s", argv[1]); color_code = r_cons_pal_parse (dup); if (!color_code) { eprintf ("Unknown color %s\n", argv[1]); r_str_argv_free (argv); free (dup); free (word); return true; } R_FREE (dup); } break; default: eprintf ("See ecH?\n"); r_str_argv_free (argv); return true; } char *str = r_meta_get_string (core->anal, R_META_TYPE_HIGHLIGHT, core->offset); char *dup = r_str_newf ("%s \"%s%s\"", str?str:"", word?word:"", color_code?color_code:r_cons_pal_get ("highlight")); r_meta_set_string (core->anal, R_META_TYPE_HIGHLIGHT, core->offset, dup); r_str_argv_free (argv); R_FREE (word); R_FREE (dup); break; } default: { char *p = strdup (input + 2); char *q = strchr (p, '='); if (!q) { q = strchr (p, ' '); } if (q) { // set *q++ = 0; r_cons_pal_set (p, q); } else { const char *k = r_cons_pal_get (p); if (k) { eprintf ("(%s)(%sCOLOR"Color_RESET")\n", p, k); } } free (p); } } break; case 'e': if (input[1] == ' ') { char *p; const char *val, *input2 = strchr (input+2, ' '); if (input2) input2++; else input2 = input+2; val = r_config_get (core->config, input2); p = r_core_editor (core, NULL, val); if (p) { r_str_replace_char (p, '\n', ';'); r_config_set (core->config, input2, p); } } else { eprintf ("Usage: ee varname\n"); } break; case '!': input = r_str_chop_ro (input+1); if (!r_config_toggle (core->config, input)) eprintf ("r_config: '%s' is not a boolean variable.\n", input); break; case 's': r_config_list (core->config, (input[1])? input + 1: NULL, 's'); break; case '-': r_core_config_init (core); //eprintf ("BUG: 'e-' command locks the eval hashtable. patches are welcome :)\n"); break; case '*': r_config_list (core->config, NULL, 1); break; case '?': switch (input[1]) { case '?': r_config_list (core->config, input+2, 2); break; default: r_config_list (core->config, input+1, 2); break; case 0: r_core_cmd_help (core, help_msg_e); } break; case 'r': if (input[1]) { const char *key = input+((input[1]==' ')?2:1); if (!r_config_readonly (core->config, key)) { eprintf ("cannot find key '%s'\n", key); } } else { eprintf ("Usage: er [key]\n"); } break; case ' ': r_config_eval (core->config, input+1); break; default: r_config_eval (core->config, input); break; } return 0; }
R_API int r_run_config_env(RRunProfile *p) { int ret; if (!p->_program && !p->_system && !p->_runlib) { printf ("No program, system or runlib rule defined\n"); return 1; } // when IO is redirected to a process, handle them together if (handle_redirection (p->_stdio, true, true, false) != 0) { return 1; } if (handle_redirection (p->_stdin, true, false, false) != 0) { return 1; } if (handle_redirection (p->_stdout, false, true, false) != 0) { return 1; } if (handle_redirection (p->_stderr, false, false, true) != 0) { return 1; } if (p->_aslr != -1) { setASLR (p, p->_aslr); } #if __UNIX__ set_limit (p->_docore, RLIMIT_CORE, RLIM_INFINITY); if (p->_maxfd) { set_limit (p->_maxfd, RLIMIT_NOFILE, p->_maxfd); } #ifdef RLIMIT_NPROC if (p->_maxproc) { set_limit (p->_maxproc, RLIMIT_NPROC, p->_maxproc); } #endif if (p->_maxstack) { set_limit (p->_maxstack, RLIMIT_STACK, p->_maxstack); } #else if (p->_docore || p->_maxfd || p->_maxproc || p->_maxstack) eprintf ("Warning: setrlimits not supported for this platform\n"); #endif if (p->_connect) { char *q = strchr (p->_connect, ':'); if (q) { RSocket *fd = r_socket_new (0); *q = 0; if (!r_socket_connect_tcp (fd, p->_connect, q+1, 30)) { eprintf ("Cannot connect\n"); return 1; } if (p->_pty) { if (redirect_socket_to_pty (fd) != 0) { eprintf ("socket redirection failed\n"); r_socket_free (fd); return 1; } } else { redirect_socket_to_stdio (fd); } } else { eprintf ("Invalid format for connect. missing ':'\n"); return 1; } } if (p->_listen) { RSocket *child, *fd = r_socket_new (0); bool is_child = false; if (!r_socket_listen (fd, p->_listen, NULL)) { eprintf ("rarun2: cannot listen\n"); r_socket_free (fd); return 1; } while (true) { child = r_socket_accept (fd); if (child) { is_child = true; if (p->_dofork && !p->_dodebug) { #ifdef _MSC_VER int child_pid = r_sys_fork (); #else pid_t child_pid = r_sys_fork (); #endif if (child_pid == -1) { eprintf("rarun2: cannot fork\n"); r_socket_free (child); r_socket_free (fd); return 1; } else if (child_pid != 0){ // parent code is_child = false; } } if (is_child) { r_socket_close_fd (fd); eprintf ("connected\n"); if (p->_pty) { if (redirect_socket_to_pty (child) != 0) { eprintf ("socket redirection failed\n"); r_socket_free (child); r_socket_free (fd); return 1; } } else { redirect_socket_to_stdio (child); } break; } else { r_socket_close_fd (child); } } } if (!is_child) { r_socket_free (child); } r_socket_free (fd); } if (p->_r2sleep != 0) { r_sys_sleep (p->_r2sleep); } #if __UNIX__ if (p->_chroot) { if (chdir (p->_chroot) == -1) { eprintf ("Cannot chdir to chroot in %s\n", p->_chroot); return 1; } else { if (chroot (".") == -1) { eprintf ("Cannot chroot to %s\n", p->_chroot); return 1; } else { // Silenting pedantic meson flags... if (chdir ("/") == -1) { eprintf ("Cannot chdir to /\n"); return 1; } if (p->_chgdir) { if (chdir (p->_chgdir) == -1) { eprintf ("Cannot chdir after chroot to %s\n", p->_chgdir); return 1; } } } } } else if (p->_chgdir) { if (chdir (p->_chgdir) == -1) { eprintf ("Cannot chdir after chroot to %s\n", p->_chgdir); return 1; } } #else if (p->_chgdir) { ret = chdir (p->_chgdir); if (ret < 0) { return 1; } } if (p->_chroot) { ret = chdir (p->_chroot); if (ret < 0) { return 1; } } #endif #if __UNIX__ if (p->_setuid) { ret = setgroups (0, NULL); if (ret < 0) { return 1; } ret = setuid (atoi (p->_setuid)); if (ret < 0) { return 1; } } if (p->_seteuid) { ret = seteuid (atoi (p->_seteuid)); if (ret < 0) { return 1; } } if (p->_setgid) { ret = setgid (atoi (p->_setgid)); if (ret < 0) { return 1; } } if (p->_input) { char *inp; int f2[2]; if (pipe (f2) != -1) { close (0); dup2 (f2[0], 0); } else { eprintf ("[ERROR] rarun2: Cannot create pipe\n"); return 1; } inp = getstr (p->_input); if (inp) { size_t inpl = strlen (inp); if (write (f2[1], inp, inpl) != inpl) { eprintf ("[ERROR] rarun2: Cannot write to the pipe\n"); } close (f2[1]); free (inp); } else { eprintf ("Invalid input\n"); } } #endif if (p->_r2preload) { if (p->_preload) { eprintf ("WARNING: Only one library can be opened at a time\n"); } p->_preload = R2_LIBDIR"/libr2."R_LIB_EXT; } if (p->_libpath) { #if __WINDOWS__ eprintf ("rarun2: libpath unsupported for this platform\n"); #elif __HAIKU__ r_sys_setenv ("LIBRARY_PATH", p->_libpath); #elif __APPLE__ r_sys_setenv ("DYLD_LIBRARY_PATH", p->_libpath); #else r_sys_setenv ("LD_LIBRARY_PATH", p->_libpath); #endif } if (p->_preload) { #if __APPLE__ // 10.6 #ifndef __MAC_10_7 r_sys_setenv ("DYLD_PRELOAD", p->_preload); #endif r_sys_setenv ("DYLD_INSERT_LIBRARIES", p->_preload); // 10.8 r_sys_setenv ("DYLD_FORCE_FLAT_NAMESPACE", "1"); #else r_sys_setenv ("LD_PRELOAD", p->_preload); #endif } if (p->_timeout) { #if __UNIX__ int mypid = getpid (); if (!r_sys_fork ()) { int use_signal = p->_timeout_sig; if (use_signal < 1) { use_signal = SIGKILL; } sleep (p->_timeout); if (!kill (mypid, 0)) { eprintf ("\nrarun2: Interrupted by timeout\n"); } kill (mypid, use_signal); exit (0); } #else eprintf ("timeout not supported for this platform\n"); #endif } return 0; }
R_API bool r_run_parseline(RRunProfile *p, char *b) { int must_free = false; char *e = strchr (b, '='); if (!e || *b == '#') { return 0; } *e++ = 0; if (*e=='$') { must_free = true; e = r_sys_getenv (e); } if (!e) { return 0; } if (!strcmp (b, "program")) { p->_args[0] = p->_program = strdup (e); } else if (!strcmp (b, "system")) { p->_system = strdup (e); } else if (!strcmp (b, "runlib")) { p->_runlib = strdup (e); } else if (!strcmp (b, "runlib.fcn")) { p->_runlib_fcn = strdup (e); } else if (!strcmp (b, "aslr")) { p->_aslr = parseBool (e); } else if (!strcmp (b, "pid")) { p->_pid = atoi (e); } else if (!strcmp (b, "pidfile")) { p->_pidfile = strdup (e); } else if (!strcmp (b, "connect")) { p->_connect = strdup (e); } else if (!strcmp (b, "listen")) { p->_listen = strdup (e); } else if (!strcmp (b, "pty")) { p->_pty = parseBool (e); } else if (!strcmp (b, "stdio")) { if (e[0] == '!') { p->_stdio = strdup (e); } else { p->_stdout = strdup (e); p->_stderr = strdup (e); p->_stdin = strdup (e); } } else if (!strcmp (b, "stdout")) { p->_stdout = strdup (e); } else if (!strcmp (b, "stdin")) { p->_stdin = strdup (e); } else if (!strcmp (b, "stderr")) { p->_stderr = strdup (e); } else if (!strcmp (b, "input")) { p->_input = strdup (e); } else if (!strcmp (b, "chdir")) { p->_chgdir = strdup (e); } else if (!strcmp (b, "core")) { p->_docore = parseBool (e); } else if (!strcmp (b, "fork")) { p->_dofork = parseBool (e); } else if (!strcmp (b, "sleep")) { p->_r2sleep = atoi (e); } else if (!strcmp (b, "maxstack")) { p->_maxstack = atoi (e); } else if (!strcmp (b, "maxproc")) { p->_maxproc = atoi (e); } else if (!strcmp (b, "maxfd")) { p->_maxfd = atoi (e); } else if (!strcmp (b, "bits")) { p->_bits = atoi (e); } else if (!strcmp (b, "chroot")) { p->_chroot = strdup (e); } else if (!strcmp (b, "libpath")) { p->_libpath = strdup (e); } else if (!strcmp (b, "preload")) { p->_preload = strdup (e); } else if (!strcmp (b, "r2preload")) { p->_r2preload = parseBool (e); } else if (!strcmp (b, "r2preweb")) { r_sys_setenv ("RARUN2_WEB", "yes"); } else if (!strcmp (b, "setuid")) { p->_setuid = strdup (e); } else if (!strcmp (b, "seteuid")) { p->_seteuid = strdup (e); } else if (!strcmp (b, "setgid")) { p->_setgid = strdup (e); } else if (!strcmp (b, "setegid")) { p->_setegid = strdup (e); } else if (!strcmp (b, "nice")) { p->_nice = atoi (e); } else if (!strcmp (b, "timeout")) { p->_timeout = atoi (e); } else if (!strcmp (b, "timeoutsig")) { p->_timeout_sig = r_signal_from_string (e); } else if (!memcmp (b, "arg", 3)) { int n = atoi (b + 3); if (n >= 0 && n < R_RUN_PROFILE_NARGS) { p->_args[n] = getstr (e); p->_argc++; } else { eprintf ("Out of bounds args index: %d\n", n); } } else if (!strcmp (b, "envfile")) { char *p, buf[1024]; size_t len; FILE *fd = fopen (e, "r"); if (!fd) { eprintf ("Cannot open '%s'\n", e); if (must_free == true) { free (e); } return false; } for (;;) { if (!fgets (buf, sizeof (buf) - 1, fd)) { break; } if (feof (fd)) { break; } p = strchr (buf, '='); if (p) { *p++ = 0; len = strlen(p); if (p[len - 1] == '\n') { p[len - 1] = 0; } if (p[len - 2] == '\r') { p[len - 2] = 0; } r_sys_setenv (buf, p); } } fclose (fd); } else if (!strcmp (b, "unsetenv")) { r_sys_setenv (e, NULL); } else if (!strcmp (b, "setenv")) { char *V, *v = strchr (e, '='); if (v) { *v++ = 0; V = getstr (v); r_sys_setenv (e, V); free (V); } } else if (!strcmp(b, "clearenv")) { r_sys_clearenv (); } if (must_free == true) { free (e); } return true; }
static int lang_vala_file(RLang *lang, const char *file, bool silent) { void *lib; char *p, name[512], buf[512]; char *vapidir, *srcdir, *libname; if (strlen (file)>500) return false; if (!strstr (file, ".vala")) sprintf (name, "%s.vala", file); else strcpy (name, file); if (!r_file_exists (name)) { eprintf ("file not found (%s)\n", name); return false; } srcdir = strdup (file); p = (char*)r_str_lchr (srcdir, '/'); if (p) { *p = 0; libname = strdup (p+1); if (*file!='/') { strcpy (srcdir, "."); } } else { libname = strdup (file); strcpy (srcdir, "."); } r_sys_setenv ("PKG_CONFIG_PATH", R2_LIBDIR"/pkgconfig"); vapidir = r_sys_getenv ("VAPIDIR"); char *tail = silent? " > /dev/null 2>&1": ""; if (vapidir) { if (*vapidir) { snprintf (buf, sizeof (buf)-1, "valac -d %s --vapidir=%s --pkg r_core -C %s %s", srcdir, vapidir, name, tail); } free (vapidir); } else { snprintf (buf, sizeof (buf) - 1, "valac -d %s --pkg r_core -C %s %s", srcdir, name, tail); } free (srcdir); if (r_sandbox_system (buf, 1) != 0) { free (libname); return false; } p = strstr (name, ".vala"); if (p) *p=0; p = strstr (name, ".gs"); if (p) *p=0; // TODO: use CC environ if possible snprintf (buf, sizeof (buf), "gcc -fPIC -shared %s.c -o lib%s."R_LIB_EXT " $(pkg-config --cflags --libs r_core gobject-2.0)", name, libname); if (r_sandbox_system (buf, 1) != 0) { free (libname); return false; } snprintf (buf, sizeof (buf), "./lib%s."R_LIB_EXT, libname); free (libname); lib = r_lib_dl_open (buf); if (lib != NULL) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) fcn (lang->user); else eprintf ("Cannot find 'entry' symbol in library\n"); r_lib_dl_close (lib); } else eprintf ("Cannot open library\n"); r_file_rm (buf); // remove lib sprintf (buf, "%s.c", name); // remove .c r_file_rm (buf); return 0; }
static int runfile () { if (!_program) { printf ("No program rule defined\n"); return 1; } if (_stdin) { int f = open (_stdin, O_RDONLY); close (0); dup2 (f, 0); } if (_stdout) { int f = open (_stdout, O_RDONLY); close (1); dup2 (f, 1); } if (_stderr) { int f = open (_stderr, O_RDONLY); close (2); dup2 (f, 2); } if (_connect) { char *p = strchr (_connect, ':'); if (p) { RSocket *fd = r_socket_new (0); *p=0; if (!r_socket_connect_tcp (fd, _connect, p+1, 30)) { eprintf ("Cannot connect\n"); return 1; } eprintf ("connected\n"); close (0); close (1); close (2); dup2 (fd->fd, 0); dup2 (fd->fd, 1); dup2 (fd->fd, 2); } else { eprintf ("Invalid format for connect. missing ':'\n"); return 1; } } if (_listen) { RSocket *child, *fd = r_socket_new (0); if (!r_socket_listen (fd, _listen, NULL)) { eprintf ("rarun2: cannot listen\n"); return 1; } child = r_socket_accept (fd); if (child) { eprintf ("connected\n"); close (0); close (1); close (2); dup2 (child->fd, 0); dup2 (child->fd, 1); dup2 (child->fd, 2); } } if (_chgdir) chdir (_chgdir); if (_chroot) chdir (_chroot); #if __UNIX__ if (_chroot) { if (chroot (".")) { eprintf ("rarun2: cannot chroot\n"); return 1; } } if (_setuid) setuid (atoi (_setuid)); if (_seteuid) seteuid (atoi (_seteuid)); if (_setgid) setgid (atoi (_setgid)); if (_input) { int f2[2]; pipe (f2); close (0); dup2 (f2[0], 0); parseinput (_input); write (f2[1], _input, strlen (_input)); } #endif if (_preload) { #if __APPLE__ r_sys_setenv ("DYLD_PRELOAD", _preload); #else r_sys_setenv ("LD_PRELOAD", _preload); #endif } if (_timeout) { #if __UNIX__ int mypid = getpid (); if (!fork ()) { sleep (_timeout); if (!kill (mypid, 0)) fprintf (stderr, "\nrarun2: Interrupted by timeout\n"); kill (mypid, SIGKILL); exit (0); } #else eprintf ("timeout not supported for this platform\n"); #endif } if (!r_file_exists (_program)) { eprintf ("rarun2: %s: file not found\n", _program); return 1; } exit (execv (_program, _args)); }
R_API int r_run_parseline (RRunProfile *p, char *b) { int must_free = R_FALSE; char *e = strchr (b, '='); if (!e) return 0; if (*b=='#') return 0; *e++ = 0; if (*e=='$') { must_free = R_TRUE; e = r_sys_getenv (e); } if (e == NULL) return 0; if (!strcmp (b, "program")) p->_args[0] = p->_program = strdup (e); else if (!strcmp (b, "system")) p->_system = strdup (e); else if (!strcmp (b, "aslr")) p->_aslr = parseBool (e); else if (!strcmp (b, "pid")) p->_pid = atoi (e); else if (!strcmp (b, "pidfile")) p->_pidfile = strdup (e); else if (!strcmp (b, "connect")) p->_connect = strdup (e); else if (!strcmp (b, "listen")) p->_listen = strdup (e); else if (!strcmp (b, "stdout")) p->_stdout = strdup (e); else if (!strcmp (b, "stdio")) { p->_stdout = p->_stderr = p->_stdin = strdup (e); } else if (!strcmp (b, "stdin")) p->_stdin = strdup (e); else if (!strcmp (b, "stderr")) p->_stderr = strdup (e); else if (!strcmp (b, "input")) p->_input = strdup (e); else if (!strcmp (b, "chdir")) p->_chgdir = strdup (e); else if (!strcmp (b, "core")) p->_docore = parseBool (e); else if (!strcmp (b, "sleep")) p->_r2sleep = atoi (e); else if (!strcmp (b, "maxstack")) p->_maxstack = atoi (e); else if (!strcmp (b, "maxproc")) p->_maxproc = atoi (e); else if (!strcmp (b, "maxfd")) p->_maxfd = atoi (e); else if (!strcmp (b, "bits")) p->_bits = atoi (e); else if (!strcmp (b, "chroot")) p->_chroot = strdup (e); else if (!strcmp (b, "libpath")) p->_libpath = strdup (e); else if (!strcmp (b, "preload")) p->_preload = strdup (e); else if (!strcmp (b, "r2preload")) p->_r2preload = parseBool (e); else if (!strcmp (b, "setuid")) p->_setuid = strdup (e); else if (!strcmp (b, "seteuid")) p->_seteuid = strdup (e); else if (!strcmp (b, "setgid")) p->_setgid = strdup (e); else if (!strcmp (b, "setegid")) p->_setegid = strdup (e); else if (!strcmp (b, "nice")) p->_nice = atoi (e); else if (!memcmp (b, "arg", 3)) { int n = atoi (b+3); if (n>=0 && n<R_RUN_PROFILE_NARGS) { p->_args[n] = getstr (e); } else eprintf ("Out of bounds args index: %d\n", n); } else if (!strcmp (b, "timeout")) { p->_timeout = atoi (e); } else if (!strcmp (b, "envfile")) { char *p, buf[1024]; FILE *fd = fopen (e, "r"); if (!fd) { eprintf ("Cannot open '%s'\n", e); if (must_free == R_TRUE) free (e); return 0; } for (;;) { fgets (buf, sizeof (buf)-1, fd); if (feof (fd)) break; p = strchr (buf, '='); if (p) { *p = 0; r_sys_setenv (buf, p+1); } } fclose (fd); } else if (!strcmp (b, "unsetenv")) { r_sys_setenv (e, NULL); } else if (!strcmp (b, "setenv")) { char *v = strchr (e, '='); if (v) { *v++ = 0; r_sys_setenv (e, v); } } if (must_free == R_TRUE) free (e); return 1; }
static void env(const char *s, int f) { char *a = r_str_newf ("%d", f); r_sys_setenv (s, a); free (a); }
static int cmd_eval(void *data, const char *input) { RCore *core = (RCore *)data; switch (input[0]) { case '\0': // "e" r_config_list (core->config, NULL, 0); break; case '?': // "e?" default: switch (input[1]) { case '\0': r_core_cmd_help (core, help_msg_e); break; case '?': r_config_list (core->config, input + 2, 2); break; default: r_config_list (core->config, input + 1, 2); break; } break; case 't': // "et" if (input[1] == 'a') { r_cons_printf ("%s\n", (r_num_rand (10) % 2)? "wen": "son"); } else if (input[1]==' ' && input[2]) { RConfigNode *node = r_config_node_get (core->config, input+2); if (node) { const char *type = r_config_node_type (node); if (type && *type) { r_cons_println (type); } } } else { eprintf ("Usage: et [varname] ; show type of eval var\n"); } break; case 'n': // "en" if (!strchr (input, '=')) { char *var, *p; var = strchr (input, ' '); if (var) while (*var==' ') var++; p = r_sys_getenv (var); if (p) { r_cons_println (p); free (p); } else { char **e = r_sys_get_environ (); while (e && *e) { r_cons_println (*e); e++; } } } else if (strlen (input)>3) { char *v, *k = strdup (input+3); if (!k) break; v = strchr (k, '='); if (v) { *v++ = 0; r_sys_setenv (k, v); } free (k); } return true; case 'x': // exit // XXX we need headers for the cmd_xxx files. return cmd_quit (data, ""); case 'j': // json r_config_list (core->config, NULL, 'j'); break; case 'v': // verbose r_config_list (core->config, input + 1, 'v'); break; case 'q': // quiet list of eval keys r_config_list (core->config, NULL, 'q'); break; case 'c': // "ec" switch (input[1]) { case 'd': // "ecd" r_cons_pal_init (core->cons->context); break; case '?': r_core_cmd_help (core, help_msg_ec); break; case 'o': // "eco" if (input[2] == 'j') { nextpal (core, 'j'); } else if (input[2] == ' ') { cmd_load_theme (core, input + 3); } else if (input[2] == 'o') { cmd_load_theme (core, r_core_get_theme ()); } else if (input[2] == 'c' || input[2] == '.') { r_cons_printf ("%s\n", r_core_get_theme ()); } else if (input[2] == '?') { r_core_cmd_help (core, help_msg_eco); } else if (input[2] == 'q') { RList *themes_list = r_core_list_themes (core); RListIter *th_iter; const char *th; r_list_foreach (themes_list, th_iter, th) { r_cons_printf ("%s\n", th); } } else {