| Index: src/untrusted/init/irt_dev_process.c
|
| diff --git a/src/untrusted/init/irt_dev_process.c b/src/untrusted/init/irt_dev_process.c
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..af19ed3523bfada9eee4976e16fd563a7b27a4ad
|
| --- /dev/null
|
| +++ b/src/untrusted/init/irt_dev_process.c
|
| @@ -0,0 +1,292 @@
|
| +/*
|
| + * Copyright (c) 2013 The Native Client Authors. All rights reserved.
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + */
|
| +
|
| +#include <errno.h>
|
| +#include <stdlib.h>
|
| +#include <string.h>
|
| +#include <unistd.h>
|
| +
|
| +#include "native_client/src/untrusted/irt/irt.h"
|
| +#include "native_client/src/untrusted/nacl/syscall_bindings_trampoline.h"
|
| +
|
| +#include "native_client/src/public/imc_syscalls.h"
|
| +#include "native_client/src/public/imc_types.h"
|
| +#include "native_client/src/public/name_service.h"
|
| +
|
| +#include "native_client/src/shared/srpc/nacl_srpc.h"
|
| +#include "native_client/src/shared/ldr/nacl_ldr.h"
|
| +
|
| +#include "native_client/src/trusted/service_runtime/include/bits/nacl_syscalls.h"
|
| +#include "native_client/src/trusted/service_runtime/include/sys/nacl_kernel_service.h"
|
| +
|
| +
|
| +static void print_error(const char *message) {
|
| + write(2, message, strlen(message));
|
| +}
|
| +
|
| +/* Mutex to guard kernel channel initialization. */
|
| +static pthread_mutex_t kernel_service_mutex = PTHREAD_MUTEX_INITIALIZER;
|
| +static int kernel_channel_initialized = 0;
|
| +static struct NaClSrpcChannel kernel_channel;
|
| +
|
| +static int get_kernel_channel_locked(struct NaClSrpcChannel **result) {
|
| + int status;
|
| + int kernel;
|
| + int kernel_conn;
|
| +
|
| + if (kernel_channel_initialized) {
|
| + *result = &manifest_channel;
|
| + return 0;
|
| + }
|
| + *result = NULL;
|
| +
|
| + status = irt_nameservice_lookup("KernelService", O_RDWR, &kernel);
|
| + if (NACL_NAME_SERVICE_SUCCESS != status) {
|
| + print_error("Kernel service lookup failed\n");
|
| + return EIO;
|
| + }
|
| +
|
| + if (-1 == kernel) {
|
| + print_error("Kernel service descriptor is invalid\n");
|
| + return EIO;
|
| + }
|
| + kernel_conn = imc_connect(kernel);
|
| + close(kernel);
|
| + if (-1 == kernel_conn) {
|
| + print_error("Error connecting to Kernel service\n");
|
| + return EIO;
|
| + }
|
| + if (!NaClSrpcClientCtor(&kernel_channel, kernel_conn)) {
|
| + print_error("Error establishing Kernel SRPC channel\n");
|
| + return EIO;
|
| + }
|
| + *result = &kernel_channel;
|
| + kernel_channel_initialized = 1;
|
| + return 0;
|
| +}
|
| +
|
| +int get_kernel_channel(struct NaClSrpcChannel **result) {
|
| + int error;
|
| + pthread_mutex_lock(&kernel_service_mutex);
|
| + error = get_kernel_channel_locked(result);
|
| + pthread_mutex_unlock(&kernel_service_mutex);
|
| + return error;
|
| +}
|
| +
|
| +int create_process(struct NaClSrpcChannel *kernel_channel) {
|
| + int status;
|
| +
|
| + if (NACL_SRPC_RESULT_OK !=
|
| + NaClSrpcInvokeBySignature(kern_chan, NACL_KERNEL_SERVICE_CREATE_PROCESS,
|
| + &status, &g_sock_addr, &g_app_addr)) {
|
| + fprintf(stderr, "create process failed, status %d\n", status);
|
| + return 0;
|
| + }
|
| + assert(0 == status);
|
| +
|
| + if (!NaClLdrSetupCommandChannel(g_sock_addr, &g_command_channel)) {
|
| + fprintf(stderr, "failed to setup command channel\n");
|
| + return 0;
|
| + }
|
| +
|
| + return 1;
|
| +}
|
| +
|
| +static int nacl_irt_getpid(pid_t *pid) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_getppid(pid_t *ppid) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_setpgid(pid_t pid, pid_t pgid) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_getpgid(pid_t *pid) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_getsid(pid_t pid, pid_t *sid) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_setsid(pid_t *sid) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_kill(pid_t pid, int sig) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_posix_spawn(pid_t *pid, const char *path,
|
| + const posix_spawn_file_actions_t *file_actions,
|
| + const posix_spawnattr_t *attrp,
|
| + char *const argv[], char *const envp[]) {
|
| + SerializationBuffer buffer;
|
| + if (!buffer.Serialize(argv)) {
|
| +
|
| + }
|
| + if (!buffer.Serialize(envp)) {
|
| + }
|
| +
|
| + uint32_t num_bytes;
|
| + if (buf.num_bytes() > static_cast<size_t>(~static_cast<uint32_t>(0))) {
|
| + NaClLog(4, "ZygotePosix::SpawnNaClSubprocess: arglist too large\n");
|
| + return false;
|
| + }
|
| + num_bytes = static_cast<uint32_t>(buf.num_bytes());
|
| +
|
| + rpc_result = NaClSrpcInvokeBySignature(&app_channel,
|
| + "posix_spawn:sC:i",
|
| + num_bytes,
|
| + reinterpret_cast<char const *>(buf.data()),
|
| + pid);
|
| + if (NACL_SRPC_RESULT_OK != rpc_result) {
|
| + NaClSrpcDtor(&app_channel);
|
| + return EINVAL;
|
| + }
|
| +
|
| + return 0;
|
| +}
|
| +
|
| +static int nacl_irt_posix_spawn(pid_t *pid, const char *path,
|
| + const posix_spawn_file_actions_t *file_actions,
|
| + const posix_spawnattr_t *attrp,
|
| + char *const argv[], char *const envp[]) {
|
| + int status;
|
| + struct NaClSrpcChannel *kernel_channel;
|
| + int sock_addr;
|
| + int app_addr;
|
| + int nexe_fd;
|
| + struct NaClSrpcChannel command_channel;
|
| + int app_conn;
|
| + struct NaClSrpcChannel app_channel;
|
| +
|
| + status = get_kernel_channel(&kernel_channel);
|
| + if (0 != status) {
|
| + return status;
|
| + }
|
| +
|
| + pthread_mutex_lock(&kernel_service_mutex);
|
| + if (NACL_SRPC_RESULT_OK == NaClSrpcInvokeBySignature(
|
| + kernel_channel, NACL_KERNEL_SERVICE_CREATE_PROCESS,
|
| + &status, &sock_addr, &app_addr)) {
|
| + status = 0;
|
| + }
|
| + pthread_mutex_unlock(&kernel_service_mutex);
|
| +
|
| + if (!NaClLdrSetupCommandChannel(sock_addr, &command_channel)) {
|
| + print_error(stderr, "Failed to setup command channel\n");
|
| + return 0;
|
| + }
|
| +
|
| + status = nacl_irt_resource_open.open_resource(path, &nexe_fd);
|
| + if (0 != status) {
|
| + print_error(stderr, "Failed to open module nexe\n");
|
| + goto cleanup_channel;
|
| + }
|
| +
|
| + if (!NaClLdrLoadModule(&command_channel, nexe_fd)) {
|
| + print_error(stderr, "Failed to load module\n");
|
| + goto cleanup_channel;
|
| + }
|
| +
|
| + if (!NaClLdrStartModule(&command_channel)) {
|
| + print_error(stderr, "Failed to start module\n");
|
| + goto cleanup_channel;
|
| + }
|
| +
|
| + app_conn = imc_connect(app_addr);
|
| + close(app_addr);
|
| + if (-1 == app_conn) {
|
| + print_error("Error connecting to application\n");
|
| + return EIO;
|
| + }
|
| + if (!NaClSrpcClientCtor(&app_channel, app_conn)) {
|
| + fprintf(stderr, "Application SRPC channel ctor failed\n");
|
| + return 1;
|
| + }
|
| +
|
| +cleanup_channel:
|
| + NaClSrpcDtor(&command_channel);
|
| + return status;
|
| +}
|
| +
|
| +static sighandler_t nacl_irt_signal(int sig, sighandler_t handler) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_sigprocmask(int how, const sigset_t *set, siegset_t *oldset) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_wait(int *pid, int *status) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +static int nacl_irt_waitpid(int *pid, int *status, int options) {
|
| + return ENOSYS;
|
| +}
|
| +
|
| +const struct nacl_irt_dev_process nacl_irt_dev_process = {
|
| + nacl_irt_getpid,
|
| + nacl_irt_getppid,
|
| + nacl_irt_setpgid,
|
| + nacl_irt_getpgid,
|
| + nacl_irt_getsid,
|
| + nacl_irt_setsid,
|
| + nacl_irt_kill,
|
| + nacl_irt_posix_spawn,
|
| + nacl_irt_signal,
|
| + nacl_irt_sigprocmask,
|
| + nacl_irt_wait,
|
| + nacl_irt_waitpid,
|
| +};
|
| +
|
| +void SignalHandler(struct NaClSrpcRpc *rpc,
|
| + struct NaClSrpcArg *args[],
|
| + struct NaClSrpcArg *rets[],
|
| + struct NaClSrpcClosure *done) {
|
| + int signal = args[0]->u.ival;
|
| +
|
| +
|
| + fflush(stdout);
|
| + (*done->Run)(done);
|
| +}
|
| +
|
| +struct NaClSrpcHandlerDesc const kProcessHandlers[] = {
|
| + { "signal:i:", SignalHandler, },
|
| + { (char const *) NULL, (NaClSrpcMethod) NULL, },
|
| +};
|
| +
|
| +static void *RpcHandler(void *arg) {
|
| + int desc = (int) arg;
|
| + if (!NaClSrpcServerLoop(desc, kProcessHandlers, NULL)) {
|
| + print_error(stderr, "NaClSrpcServerLoop failed\n");
|
| + abort();
|
| + }
|
| + (void) close(desc);
|
| + return (void *) NULL;
|
| +}
|
| +
|
| +static void irt_process_new(int argc, char **args) {
|
| + int d;
|
| +
|
| + NaClSrpcModuleInit();
|
| + while ((d = imc_accept(3)) != -1) {
|
| + pthread_t thread;
|
| + if (0 != pthread_create(&thread, (pthread_attr_t *) NULL,
|
| + RpcHandler, (void *) d)) {
|
| + print_error("pthread_create for RpcHandler");
|
| + abort();
|
| + }
|
| + }
|
| + NaClSrpcModuleFini();
|
| +}
|
| +
|
| +NACL_IRT_REGISTER_MODULE(irt_process, irt_process_new)
|
|
|