Skip to content

Instantly share code, notes, and snippets.

@flankerhqd
Created June 5, 2014 13:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save flankerhqd/175f71e4aadfa1bb6cd9 to your computer and use it in GitHub Desktop.
Save flankerhqd/175f71e4aadfa1bb6cd9 to your computer and use it in GitHub Desktop.
Patch on mksh to enable shell command logging into logcat. Rememeber to replace ash to mksh on emulator builds.
diff --git a/Android.mk b/Android.mk
index e53b863..1d3854e 100644
--- a/Android.mk
+++ b/Android.mk
@@ -8,7 +8,6 @@ LOCAL_PATH:= $(call my-dir)
# /system/etc/mkshrc
include $(CLEAR_VARS)
-
LOCAL_MODULE:= mkshrc
LOCAL_MODULE_TAGS:= shell_mksh
LOCAL_MODULE_CLASS:= ETC
@@ -20,10 +19,9 @@ include $(BUILD_PREBUILT)
# /system/bin/mksh
include $(CLEAR_VARS)
-
LOCAL_MODULE:= mksh
LOCAL_MODULE_TAGS:= shell_mksh
-
+LOCAL_STATIC_LIBRARIES:= liblog
# mksh source files
LOCAL_SRC_FILES:= src/lalloc.c src/edit.c src/eval.c src/exec.c \
src/expr.c src/funcs.c src/histrap.c src/jobs.c \
@@ -59,6 +57,5 @@ LOCAL_CFLAGS:= -DMKSH_DEFAULT_EXECSHELL=\"/system/bin/sh\" \
-DHAVE_SETGROUPS=1 -DHAVE_STRCASESTR=1 \
-DHAVE_STRLCPY=1 -DHAVE_FLOCK_DECL=1 \
-DHAVE_REVOKE_DECL=1 -DHAVE_SYS_SIGLIST_DECL=1 \
- -DHAVE_PERSISTENT_HISTORY=0
-
+ -DHAVE_PERSISTENT_HISTORY=1
include $(BUILD_EXECUTABLE)
diff --git a/mkmf.sh b/mkmf.sh
index 0372d62..005368e 100644
--- a/mkmf.sh
+++ b/mkmf.sh
@@ -112,7 +112,9 @@ export HAVE_CAN_FNOSTRICTALIASING HAVE_CAN_FSTACKPROTECTORALL HAVE_CAN_WALL
HAVE_MKNOD=0; export HAVE_MKNOD
# even the idea of persistent history on a phone is funny
-HAVE_PERSISTENT_HISTORY=0; export HAVE_PERSISTENT_HISTORY
+# FUCKYOU_BEGIN
+HAVE_PERSISTENT_HISTORY=1; export HAVE_PERSISTENT_HISTORY
+# FUCKYOU_END
# ... and run it!
export CC CPPFLAGS CFLAGS LDFLAGS LIBS TARGET_OS
diff --git a/mkshrc b/mkshrc
index 0da5ea6..24ee1bb 100644
--- a/mkshrc
+++ b/mkshrc
@@ -25,5 +25,5 @@ for p in ~/.bin; do
done
unset p
-
+export HISTFILE=/data/local/tmp/mksh.log
: place customisations above this line
diff --git a/src/main.c b/src/main.c
index b78965e..4c2d8c9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,7 +25,7 @@
#define EXTERN
#include "sh.h"
-
+#include <android/log.h>
#if HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif
@@ -34,7 +34,7 @@
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.200 2011/10/07 19:51:28 tg Exp $");
-
+#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"TEST" ,__VA_ARGS__)
extern char **environ;
#ifndef MKSHRC_PATH
@@ -159,6 +159,23 @@ int
main(int argc, const char *argv[])
{
int argi, i;
+ char shbuf[1000]={0};
+ int remainN = 1000;
+ //SH_BEGIN
+ for(i=0;i<argc;i++)
+ {
+ strncat(shbuf, argv[i], remainN);
+ remainN -= strlen(argv[i]);
+ if(remainN < 0)
+ break;
+ strncat(shbuf, " ", remainN);
+ remainN -= 1;
+ if(remainN < 0)
+ break;
+ }
+ LOGD("command executed from args: %s", shbuf);
+ //SH_END
+
Source *s = NULL;
struct block *l;
unsigned char restricted, errexit, utf_flag;
@@ -596,7 +613,7 @@ main(int argc, const char *argv[])
if (Flag(FAS_BUILTIN))
return (shcomexec(l->argv));
-
+
/* doesn't return */
shell(s, true);
/* NOTREACHED */
@@ -676,7 +693,6 @@ int
command(const char *comm, int line)
{
Source *s;
-
s = pushs(SSTRING, ATEMP);
s->start = s->str = comm;
s->line = line;
@@ -696,7 +712,25 @@ shell(Source * volatile s, volatile int toplevel)
volatile bool sfirst = true;
Source *volatile old_source = source;
int i;
-
+
+ //SH_BEGIN
+ if(s->xs.len != 256){
+ char *p, *q;
+ for (p = s->xs.beg; p; p = q) {
+ if ((q = strchr(p, '\n'))) {
+ /* kill the newline */
+ *q++ = '\0';
+ if (!*q)
+ /* ignore trailing newline */
+ q = NULL;
+ }
+ LOGD("command executed: %s", p);
+ if (q)
+ /* restore \n (trailing \n not restored) */
+ q[-1] = '\n';
+ }
+ }
+ //SH_END
newenv(E_PARSE);
if (interactive)
really_exit = 0;
@@ -759,6 +793,24 @@ shell(Source * volatile s, volatile int toplevel)
set_prompt(PS1, s);
}
t = compile(s, sfirst);
+ //SH_BEGIN
+ if(s->xs.len != 256){
+ char *p, *q;
+ for (p = s->xs.beg; p; p = q) {
+ if ((q = strchr(p, '\n'))) {
+ /* kill the newline */
+ *q++ = '\0';
+ if (!*q)
+ /* ignore trailing newline */
+ q = NULL;
+ }
+ LOGD("command executed: %s", p);
+ if (q)
+ /* restore \n (trailing \n not restored) */
+ q[-1] = '\n';
+ }
+ }
+ //SH_END
sfirst = false;
if (t != NULL && t->type == TEOF) {
if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment