about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2022-09-08 09:59:58 +0200
committerptitSeb <sebastien.chev@gmail.com>2022-09-08 09:59:58 +0200
commit532b1697a60ed9a684b5f82b752208317aaaf1d6 (patch)
treeccb81b39e9f4e09e1c8f73e2e14d5c000aa93bc2 /src
parenta3f63a12f9102794ce1d94c972064f10a25e944b (diff)
downloadbox64-532b1697a60ed9a684b5f82b752208317aaaf1d6.tar.gz
box64-532b1697a60ed9a684b5f82b752208317aaaf1d6.zip
When running x86_64 bash, using x86_64 bash to run script automaticaly (for #402)
Diffstat (limited to 'src')
-rwxr-xr-xsrc/include/debug.h1
-rwxr-xr-xsrc/include/fileutils.h1
-rwxr-xr-xsrc/main.c10
-rwxr-xr-xsrc/tools/fileutils.c22
-rwxr-xr-xsrc/wrapped/wrappedlibc.c15
5 files changed, 41 insertions, 8 deletions
diff --git a/src/include/debug.h b/src/include/debug.h
index 5f228f58..057d7898 100755
--- a/src/include/debug.h
+++ b/src/include/debug.h
@@ -41,6 +41,7 @@ extern int box64_prefer_wrapped;
 extern int box64_prefer_emulated;
 extern int box64_steam;
 extern int box64_wine;
+extern int box64_bash;
 extern int box64_musl;
 extern int box64_nopulse;   // disabling the use of wrapped pulseaudio
 extern int box64_nogtk; // disabling the use of wrapped gtk
diff --git a/src/include/fileutils.h b/src/include/fileutils.h
index 8332681e..79da42bc 100755
--- a/src/include/fileutils.h
+++ b/src/include/fileutils.h
@@ -16,6 +16,7 @@ char* ResolveFile(const char* filename, path_collection_t* paths);
 // 1: if file is an x86 elf, 0: if not (or not found)
 int FileIsX86ELF(const char* filename);
 int FileIsX64ELF(const char* filename);
+int FileIsShell(const char* filename);
 
 #if defined(RPI) || defined(RK3399) || defined(RK3326)
 void sanitize_mojosetup_gtk_background();
diff --git a/src/main.c b/src/main.c
index 4929b769..ac04917c 100755
--- a/src/main.c
+++ b/src/main.c
@@ -84,6 +84,7 @@ int box64_mapclean = 0;
 int box64_zoom = 0;
 int box64_steam = 0;
 int box64_wine = 0;
+int box64_bash = 0;
 int box64_musl = 0;
 int box64_nopulse = 0;
 int box64_nogtk = 0;
@@ -1235,6 +1236,15 @@ int main(int argc, const char **argv, char **env) {
         printf_log(LOG_INFO, "Zoom detected, trying to use system libturbojpeg if possible\n");
         box64_zoom = 1;
     }
+    // special case for bash (add BOX86_NOBANNER=1 if not there)
+    if(!strcmp(prgname, "bash")) {
+        printf_log(LOG_INFO, "bash detected, disabling banner\n");
+        box64_bash = 1;
+        if (!box64_nobanner) {
+            setenv("BOX86_NOBANNER", "1", 0);
+            setenv("BOX64_NOBANNER", "1", 0);
+        }
+    }
     if(strstr(prgname, "pressure-vessel-wrap")==prgname) {
         printf_log(LOG_INFO, "pressure-vessel-wrap detected, disabling GTK\n");
         box64_nogtk = 1;
diff --git a/src/tools/fileutils.c b/src/tools/fileutils.c
index 80a0ec60..06299fc0 100755
--- a/src/tools/fileutils.c
+++ b/src/tools/fileutils.c
@@ -15,10 +15,10 @@
 #include "debug.h"
 #include "fileutils.h"
 
-static const char* x86sign = "\x7f" "ELF" "\x01" "\x01" "\x01" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x02" "\x00" "\x03" "\x00";
-static const char* x64sign = "\x7f" "ELF" "\x02" "\x01" "\x01" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x02" "\x00" "\x3e" "\x00";
 static const char* x86lib  = "\x7f" "ELF" "\x01" "\x01" "\x01" "\x03" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x02" "\x00" "\x03" "\x00";
 static const char* x64lib  = "\x7f" "ELF" "\x02" "\x01" "\x01" "\x03" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x00" "\x02" "\x00" "\x3e" "\x00";
+static const char* bashsign= "#!/bin/bash";
+static const char* shsign  = "#!/bin/sh";
 
 int FileExist(const char* filename, int flags)
 {
@@ -99,6 +99,24 @@ int FileIsX86ELF(const char* filename)
     return 0;
 }
 
+int FileIsShell(const char* filename)
+{
+    FILE *f = fopen(filename, "rb");
+    if(!f)
+        return 0;
+    char head[20] = {0};
+    int sz = fread(head, strlen(bashsign), 1, f);
+    fclose(f);
+    if(sz!=1) {
+        return 0;
+    if(memcmp(head, bashsign, strlen(bashsign))==0)
+        return 1;
+    }
+    if(memcmp(head, shsign, strlen(shsign))==0)
+        return 1;
+    return 0;
+}
+
 #if defined(RPI) || defined(RK3399) || defined(RK3326)
 void sanitize_mojosetup_gtk_background()
 {
diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c
index 32aad1f8..ec20ae2c 100755
--- a/src/wrapped/wrappedlibc.c
+++ b/src/wrapped/wrappedlibc.c
@@ -1716,24 +1716,27 @@ EXPORT int32_t my_execve(x64emu_t* emu, const char* path, char* const argv[], ch
     int self = isProcSelf(path, "exe");
     int x64 = FileIsX64ELF(path);
     int x86 = my_context->box86path?FileIsX86ELF(path):0;
-    printf_log(LOG_INFO/*LOG_DEBUG*/, "execve(\"%s\", %p, %p) is x64=%d x86=%d (my_context->envv=%p, environ=%p\n", path, argv, envp, x64, x86, my_context->envv, environ);
+    int script = (box64_bash && FileIsShell(path))?1:0;
+    printf_log(LOG_DEBUG, "execve(\"%s\", %p, %p) is x64=%d x86=%d script=%d (my_context->envv=%p, environ=%p\n", path, argv, envp, x64, x86, script, my_context->envv, environ);
     // hack to update the environ var if needed
     if(envp == my_context->envv && environ) {
         envp = environ;
     }
     #if 1
-    if (x64 || x86 || self) {
+    if (x64 || x86 || self || script) {
         int skip_first = 0;
         if(strlen(path)>=strlen("wine64-preloader") && strcmp(path+strlen(path)-strlen("wine64-preloader"), "wine64-preloader")==0)
             skip_first++;
         // count argv...
         int n=skip_first;
         while(argv[n]) ++n;
-        const char** newargv = (const char**)box_calloc(n+2, sizeof(char*));
+        int toadd = script?2:1;
+        const char** newargv = (const char**)box_calloc(n+1+toadd, sizeof(char*));
         newargv[0] = x86?emu->context->box86path:emu->context->box64path;
-        memcpy(newargv+1, argv+skip_first, sizeof(char*)*(n+1));
-        if(self) newargv[1] = emu->context->fullpath;
-        printf_log(LOG_DEBUG, " => execve(\"%s\", %p [\"%s\", \"%s\", \"%s\"...:%d], %p)\n", newargv[0], newargv, newargv[0], n?newargv[1]:"", (n>1)?newargv[2]:"",n, envp);
+        if(script) newargv[1] = emu->context->fullpath; // script needs to be launched with bash
+        memcpy(newargv+toadd, argv+skip_first, sizeof(char*)*(n+toadd));
+        if(self) newargv[toadd] = emu->context->fullpath;
+        printf_log(LOG_DEBUG, " => execve(\"%s\", %p [\"%s\", \"%s\", \"%s\"...:%d], %p)\n", newargv[0], newargv, newargv[0], (n+toadd)?newargv[1]:"", ((n+toadd)>1)?newargv[2]:"",n, envp);
         int ret = execve(newargv[0], (char* const*)newargv, envp);
         box_free(newargv);
         return ret;