Skip to content

Commit 4efec35

Browse files
author
cd rubin
committed
support for execution of files within zip
1 parent 117438b commit 4efec35

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

qjs.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,68 @@
4040
#include <malloc_np.h>
4141
#endif
4242

43+
44+
// BEGIN: cdrubin
45+
// exec of files within zipos
46+
47+
#define _COSMO_SOURCE
48+
49+
#include <stdbool.h>
50+
#include "libc/dce.h"
51+
#include "libc/calls/calls.h"
52+
#include "libc/calls/pledge.h"
53+
#include "libc/calls/pledge.internal.h"
54+
#include "libc/calls/syscall-nt.internal.h"
55+
#include "libc/calls/syscall-sysv.internal.h"
56+
#include "libc/intrin/describeflags.h"
57+
#include "libc/intrin/likely.h"
58+
#include "libc/intrin/promises.h"
59+
#include "libc/intrin/strace.h"
60+
#include "libc/intrin/weaken.h"
61+
#include "libc/log/libfatal.internal.h"
62+
#include "libc/runtime/runtime.h"
63+
#include "libc/runtime/zipos.internal.h"
64+
#include "libc/sysv/consts/o.h"
65+
#include "libc/sysv/errfuns.h"
66+
67+
68+
69+
int execz(const char *prog, char *const argv[], char *const envp[]) {
70+
int rc;
71+
struct ZiposUri uri;
72+
if (!prog || !argv || !envp) {
73+
rc = efault();
74+
} else {
75+
STRACE("execz(%#s, %s, %s)", prog, DescribeStringList(argv),
76+
DescribeStringList(envp));
77+
rc = 0;
78+
if (IsLinux() && __execpromises && _weaken(sys_pledge_linux)) {
79+
rc = _weaken(sys_pledge_linux)(__execpromises, __pledge_mode);
80+
}
81+
if (!rc) {
82+
if (_weaken(__zipos_parseuri) &&
83+
(_weaken(__zipos_parseuri)(prog, &uri) != -1)) {
84+
rc = _weaken(__zipos_open)(&uri, O_RDONLY | O_CLOEXEC);
85+
if (rc != -1) {
86+
const int zipFD = rc;
87+
strace_enabled(-1);
88+
rc = fexecve(zipFD, argv, envp);
89+
close(zipFD);
90+
strace_enabled(+1);
91+
}
92+
} else if (!IsWindows()) {
93+
rc = sys_execve(prog, argv, envp);
94+
} else {
95+
rc = sys_execve_nt(prog, argv, envp);
96+
}
97+
}
98+
}
99+
STRACE("execve(%#s) failed %d% m", prog, rc);
100+
return rc;
101+
}
102+
// END: cdrubin
103+
104+
43105
#include "cutils.h"
44106
#include "quickjs-libc.h"
45107

@@ -318,6 +380,27 @@ int main(int argc, char **argv)
318380
#endif
319381
size_t stack_size = 0;
320382

383+
// check value of first parameter
384+
// if it matches the name of an executable at /zip/[executable]
385+
// then run it passing all the rest of the parameters to it
386+
// also think about how to handle if this file was executed
387+
// from a symlink with the name of that executable
388+
389+
int length = snprintf( NULL, 0, "/zip/%s", argv[1] );
390+
char possible_executable_name[ length + 1 ];
391+
snprintf( possible_executable_name, length + 1, "/zip/%s", argv[1] );
392+
393+
//fprintf(stderr, "%s\n", possible_executable_name);
394+
395+
if ( access( possible_executable_name, F_OK ) == 0 ) {
396+
//fprintf(stderr, "possible executable name found!\n");
397+
int ret = execz( possible_executable_name, &argv[1], environ ); //(char *const[]){0} );
398+
399+
fprintf(stderr, "execz failure (returned: %d, errno: %d)?\n", ret, errno );
400+
exit( -10 );
401+
}
402+
//fprintf(stderr, "6\n");
403+
321404
#ifdef CONFIG_BIGNUM
322405
/* load jscalc runtime if invoked as 'qjscalc' */
323406
{

0 commit comments

Comments
 (0)