improve cleanup; release 2
[ginge.git] / loader / override.c
index 5a10193..75b8eeb 100644 (file)
@@ -1,6 +1,7 @@
 // vim:shiftwidth=2:expandtab
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -149,6 +150,42 @@ static UNUSED int w_system(const char *command)
   return ret;
 }
 
+// 4 functions bellow are efforts to prevent gp2xmenu from being started..
+static UNUSED int w_execl(const char *path, const char *arg, ...)
+{
+  // don't allow exec (for now)
+  strace("execl(%s, %s, ...) = ?\n", path, arg);
+  exit(0);
+}
+
+static UNUSED int w_execlp(const char *file, const char *arg, ...)
+{
+  strace("execlp(%s, %s, ...) = ?\n", file, arg);
+  exit(0);
+}
+
+// static note: this can't safely return because of the way it's patched in
+// static note2: can't be used, execve hangs?
+static UNUSED int w_execve(const char *filename, char *const argv[],
+                  char *const envp[])
+{
+  strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
+  if (filename != NULL && strstr(filename, "/gp2xmenu") != NULL)
+    exit(0);
+  return execve(filename, argv, envp);
+}
+
+static int w_chdir(const char *path)
+{
+  int ret;
+  if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
+    ret = 0;
+  else
+    ret = chdir(path);
+  strace("chdir(%s) = %d\n", path, ret);
+  return ret;
+}
+
 #undef open
 #undef fopen
 #undef mmap
@@ -158,15 +195,23 @@ static UNUSED int w_system(const char *command)
 #undef tcgetattr
 #undef tcsetattr
 #undef system
+#undef execl
+#undef execlp
+#undef execve
+#undef chdir
 
 #ifdef DL
 
-#define MAKE_WRAP_SYM(sym) \
+#define MAKE_WRAP_SYM_N(sym) \
   /* alias wrap symbols to real names */ \
-  typeof(sym) sym __attribute__((alias("w_" #sym))); \
+  typeof(sym) sym __attribute__((alias("w_" #sym)))
+
+#define MAKE_WRAP_SYM(sym) \
+  MAKE_WRAP_SYM_N(sym); \
   /* wrapper to real functions, to be set up on load */ \
   static typeof(sym) *p_real_##sym
 
+// note: update symver too
 MAKE_WRAP_SYM(open);
 MAKE_WRAP_SYM(fopen);
 MAKE_WRAP_SYM(mmap);
@@ -176,6 +221,10 @@ MAKE_WRAP_SYM(sigaction);
 MAKE_WRAP_SYM(tcgetattr);
 MAKE_WRAP_SYM(tcsetattr);
 MAKE_WRAP_SYM(system);
+MAKE_WRAP_SYM_N(execl);
+MAKE_WRAP_SYM_N(execlp);
+MAKE_WRAP_SYM(execve);
+MAKE_WRAP_SYM(chdir);
 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
 
 #define REAL_FUNC_NP(name) \
@@ -194,6 +243,9 @@ static const struct {
   REAL_FUNC_NP(tcgetattr),
   REAL_FUNC_NP(tcsetattr),
   REAL_FUNC_NP(system),
+  // exec* - skipped
+  REAL_FUNC_NP(execve),
+  REAL_FUNC_NP(chdir),
 };
 
 #define open p_real_open
@@ -205,6 +257,8 @@ static const struct {
 #define tcgetattr p_real_tcgetattr
 #define tcsetattr p_real_tcsetattr
 #define system p_real_system
+#define execve p_real_execve
+#define chdir p_real_chdir
 
 #undef MAKE_WRAP_SYM
 #undef REAL_FUNC_NP
@@ -212,8 +266,14 @@ static const struct {
 #endif
 
 // just call real funcs for static, pointer for dynamic
-int real_open(const char *pathname, int flags, mode_t mode)
+int real_open(const char *pathname, int flags, ...)
 {
+  va_list ap;
+  mode_t mode;
+
+  va_start(ap, flags);
+  mode = va_arg(ap, mode_t);
+  va_end(ap);
   return open(pathname, flags, mode);
 }
 
@@ -257,3 +317,16 @@ int real_system(const char *command)
 {
   return system(command);
 }
+
+// real_exec* is missing intentionally - we don't need them
+
+int real_execve(const char *filename, char *const argv[],
+                  char *const envp[])
+{
+  return execve(filename, argv, envp);
+}
+
+int real_chdir(const char *path)
+{
+  return chdir(path);
+}