12 #define STDERRSTR_CONST(m) write(2,m,sizeof(m)-1)
13 #define STDERRSTR_STRING(m) write(2,m,strlen(m))
15 #define QUIS "libjwrap"
17 typedef void anyfn_type(void);
19 execvefn_type(const char *path, char *const argv[], char *const envp[]);
21 static execvefn_type find_execve, *old_execve= find_execve;
23 static anyfn_type *find_any(const char *name) {
24 static const char *dlerr;
27 kv= dlsym(RTLD_NEXT,name); if (kv) return kv;
28 dlerr= dlerror(); if (!dlerr) dlerr= "dlsym() failed for no reason";
29 STDERRSTR_CONST(QUIS ": error finding original version of ");
30 STDERRSTR_STRING(name);
31 STDERRSTR_CONST(": ");
32 STDERRSTR_STRING(dlerr);
33 STDERRSTR_CONST("\n");
38 static int find_execve(const char *path,
39 char *const argv[], char *const envp[]) {
41 anyfn= find_any("execve"); if (!anyfn) return -1;
42 old_execve= (execvefn_type*)anyfn;
43 return old_execve(path, argv, envp);
46 static const char javatail[]= "/bin/java";
48 int execve(const char *path, char *const oargv[], char *const envp[]) {
50 for (oargc=0; oargv[oargc]; oargc++);
52 if (!strcmp(path,"/dev/jwrap/bypass")) {
53 //STDERRSTR_CONST(QUIS ": bypass\n");
55 char **nargv= malloc(sizeof(*nargv) * nargc); if (!nargv) return -1;
57 memcpy(nargv+1, oargv+2, sizeof(*nargv) * nargc-1);
58 return old_execve(oargv[1], nargv, envp);
61 int pathlen= strlen(path);
63 pathlen < (sizeof(javatail)-1) ||
64 strcmp(javatail, path + pathlen-(sizeof(javatail)-1))) {
65 //STDERRSTR_CONST(QUIS ": non-java\n");
66 return old_execve(path, oargv, envp);
69 //STDERRSTR_CONST(QUIS ": ours\n");
72 if (stat(path,&stab)) return -1;
75 char **nargv= malloc(sizeof(*nargv) * nargc); if (!nargv) return -1;
77 const char *jwrap_script= getenv("JWRAP_SCRIPT");
79 STDERRSTR_CONST(QUIS ": no JWRAP_SCRIPT specified!\n");
84 nargv[0]= (char*)jwrap_script;
85 nargv[1]= (char*)path;
86 memcpy(nargv+2, oargv, sizeof(*nargv) * (oargc+1));
88 old_execve(nargv[0], nargv, envp);
91 const char *errstr= strerror(e);
92 STDERRSTR_CONST(QUIS ": failed to execute ");
93 STDERRSTR_STRING(jwrap_script);
94 STDERRSTR_CONST(": ");
95 STDERRSTR_STRING(errstr);
96 STDERRSTR_CONST("\n");