exec(args[, options]) -> exitcode */
| 3569 | |
| 3570 | /* exec(args[, options]) -> exitcode */ |
| 3571 | static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val, |
| 3572 | int argc, JSValueConst *argv) |
| 3573 | { |
| 3574 | JSValueConst options, args = argv[0]; |
| 3575 | JSValue val, ret_val; |
| 3576 | const char **exec_argv, *file = NULL, *str, *cwd = NULL; |
| 3577 | char **envp = environ; |
| 3578 | uint32_t exec_argc, i; |
| 3579 | int ret, pid, status; |
| 3580 | bool block_flag = true, use_path = true; |
| 3581 | static const char *std_name[3] = { "stdin", "stdout", "stderr" }; |
| 3582 | int std_fds[3]; |
| 3583 | uint32_t uid = -1, gid = -1; |
| 3584 | int ngroups = -1; |
| 3585 | gid_t groups[64]; |
| 3586 | |
| 3587 | val = JS_GetPropertyStr(ctx, args, "length"); |
| 3588 | if (JS_IsException(val)) |
| 3589 | return JS_EXCEPTION; |
| 3590 | ret = JS_ToUint32(ctx, &exec_argc, val); |
| 3591 | JS_FreeValue(ctx, val); |
| 3592 | if (ret) |
| 3593 | return JS_EXCEPTION; |
| 3594 | /* arbitrary limit to avoid overflow */ |
| 3595 | if (exec_argc < 1 || exec_argc > 65535) { |
| 3596 | return JS_ThrowTypeError(ctx, "invalid number of arguments"); |
| 3597 | } |
| 3598 | exec_argv = js_mallocz(ctx, sizeof(exec_argv[0]) * (exec_argc + 1)); |
| 3599 | if (!exec_argv) |
| 3600 | return JS_EXCEPTION; |
| 3601 | for(i = 0; i < exec_argc; i++) { |
| 3602 | val = JS_GetPropertyUint32(ctx, args, i); |
| 3603 | if (JS_IsException(val)) |
| 3604 | goto exception; |
| 3605 | str = JS_ToCString(ctx, val); |
| 3606 | JS_FreeValue(ctx, val); |
| 3607 | if (!str) |
| 3608 | goto exception; |
| 3609 | exec_argv[i] = str; |
| 3610 | } |
| 3611 | exec_argv[exec_argc] = NULL; |
| 3612 | |
| 3613 | for(i = 0; i < 3; i++) |
| 3614 | std_fds[i] = i; |
| 3615 | |
| 3616 | /* get the options, if any */ |
| 3617 | if (argc >= 2) { |
| 3618 | options = argv[1]; |
| 3619 | |
| 3620 | if (get_bool_option(ctx, &block_flag, options, "block")) |
| 3621 | goto exception; |
| 3622 | if (get_bool_option(ctx, &use_path, options, "usePath")) |
| 3623 | goto exception; |
| 3624 | |
| 3625 | val = JS_GetPropertyStr(ctx, options, "file"); |
| 3626 | if (JS_IsException(val)) |
| 3627 | goto exception; |
| 3628 | if (!JS_IsUndefined(val)) { |
nothing calls this directly
no test coverage detected