OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2013 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 #include <errno.h> |
| 8 #include <stdlib.h> |
| 9 #include <string.h> |
| 10 #include <unistd.h> |
| 11 |
| 12 #include "native_client/src/untrusted/irt/irt.h" |
| 13 #include "native_client/src/untrusted/nacl/syscall_bindings_trampoline.h" |
| 14 |
| 15 #include "native_client/src/public/imc_syscalls.h" |
| 16 #include "native_client/src/public/imc_types.h" |
| 17 #include "native_client/src/public/name_service.h" |
| 18 |
| 19 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 20 #include "native_client/src/shared/ldr/nacl_ldr.h" |
| 21 |
| 22 #include "native_client/src/trusted/service_runtime/include/bits/nacl_syscalls.h
" |
| 23 #include "native_client/src/trusted/service_runtime/include/sys/nacl_kernel_serv
ice.h" |
| 24 |
| 25 |
| 26 static void print_error(const char *message) { |
| 27 write(2, message, strlen(message)); |
| 28 } |
| 29 |
| 30 /* Mutex to guard kernel channel initialization. */ |
| 31 static pthread_mutex_t kernel_service_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 32 static int kernel_channel_initialized = 0; |
| 33 static struct NaClSrpcChannel kernel_channel; |
| 34 |
| 35 static int get_kernel_channel_locked(struct NaClSrpcChannel **result) { |
| 36 int status; |
| 37 int kernel; |
| 38 int kernel_conn; |
| 39 |
| 40 if (kernel_channel_initialized) { |
| 41 *result = &manifest_channel; |
| 42 return 0; |
| 43 } |
| 44 *result = NULL; |
| 45 |
| 46 status = irt_nameservice_lookup("KernelService", O_RDWR, &kernel); |
| 47 if (NACL_NAME_SERVICE_SUCCESS != status) { |
| 48 print_error("Kernel service lookup failed\n"); |
| 49 return EIO; |
| 50 } |
| 51 |
| 52 if (-1 == kernel) { |
| 53 print_error("Kernel service descriptor is invalid\n"); |
| 54 return EIO; |
| 55 } |
| 56 kernel_conn = imc_connect(kernel); |
| 57 close(kernel); |
| 58 if (-1 == kernel_conn) { |
| 59 print_error("Error connecting to Kernel service\n"); |
| 60 return EIO; |
| 61 } |
| 62 if (!NaClSrpcClientCtor(&kernel_channel, kernel_conn)) { |
| 63 print_error("Error establishing Kernel SRPC channel\n"); |
| 64 return EIO; |
| 65 } |
| 66 *result = &kernel_channel; |
| 67 kernel_channel_initialized = 1; |
| 68 return 0; |
| 69 } |
| 70 |
| 71 int get_kernel_channel(struct NaClSrpcChannel **result) { |
| 72 int error; |
| 73 pthread_mutex_lock(&kernel_service_mutex); |
| 74 error = get_kernel_channel_locked(result); |
| 75 pthread_mutex_unlock(&kernel_service_mutex); |
| 76 return error; |
| 77 } |
| 78 |
| 79 int create_process(struct NaClSrpcChannel *kernel_channel) { |
| 80 int status; |
| 81 |
| 82 if (NACL_SRPC_RESULT_OK != |
| 83 NaClSrpcInvokeBySignature(kern_chan, NACL_KERNEL_SERVICE_CREATE_PROCESS, |
| 84 &status, &g_sock_addr, &g_app_addr)) { |
| 85 fprintf(stderr, "create process failed, status %d\n", status); |
| 86 return 0; |
| 87 } |
| 88 assert(0 == status); |
| 89 |
| 90 if (!NaClLdrSetupCommandChannel(g_sock_addr, &g_command_channel)) { |
| 91 fprintf(stderr, "failed to setup command channel\n"); |
| 92 return 0; |
| 93 } |
| 94 |
| 95 return 1; |
| 96 } |
| 97 |
| 98 static int nacl_irt_getpid(pid_t *pid) { |
| 99 return ENOSYS; |
| 100 } |
| 101 |
| 102 static int nacl_irt_getppid(pid_t *ppid) { |
| 103 return ENOSYS; |
| 104 } |
| 105 |
| 106 static int nacl_irt_setpgid(pid_t pid, pid_t pgid) { |
| 107 return ENOSYS; |
| 108 } |
| 109 |
| 110 static int nacl_irt_getpgid(pid_t *pid) { |
| 111 return ENOSYS; |
| 112 } |
| 113 |
| 114 static int nacl_irt_getsid(pid_t pid, pid_t *sid) { |
| 115 return ENOSYS; |
| 116 } |
| 117 |
| 118 static int nacl_irt_setsid(pid_t *sid) { |
| 119 return ENOSYS; |
| 120 } |
| 121 |
| 122 static int nacl_irt_kill(pid_t pid, int sig) { |
| 123 return ENOSYS; |
| 124 } |
| 125 |
| 126 static int nacl_irt_posix_spawn(pid_t *pid, const char *path, |
| 127 const posix_spawn_file_actions_t *file_actions, |
| 128 const posix_spawnattr_t *attrp, |
| 129 char *const argv[], char *const envp[]) { |
| 130 SerializationBuffer buffer; |
| 131 if (!buffer.Serialize(argv)) { |
| 132 |
| 133 } |
| 134 if (!buffer.Serialize(envp)) { |
| 135 } |
| 136 |
| 137 uint32_t num_bytes; |
| 138 if (buf.num_bytes() > static_cast<size_t>(~static_cast<uint32_t>(0))) { |
| 139 NaClLog(4, "ZygotePosix::SpawnNaClSubprocess: arglist too large\n"); |
| 140 return false; |
| 141 } |
| 142 num_bytes = static_cast<uint32_t>(buf.num_bytes()); |
| 143 |
| 144 rpc_result = NaClSrpcInvokeBySignature(&app_channel, |
| 145 "posix_spawn:sC:i", |
| 146 num_bytes, |
| 147 reinterpret_cast<char const *>(buf.data
()), |
| 148 pid); |
| 149 if (NACL_SRPC_RESULT_OK != rpc_result) { |
| 150 NaClSrpcDtor(&app_channel); |
| 151 return EINVAL; |
| 152 } |
| 153 |
| 154 return 0; |
| 155 } |
| 156 |
| 157 static int nacl_irt_posix_spawn(pid_t *pid, const char *path, |
| 158 const posix_spawn_file_actions_t *file_actions, |
| 159 const posix_spawnattr_t *attrp, |
| 160 char *const argv[], char *const envp[]) { |
| 161 int status; |
| 162 struct NaClSrpcChannel *kernel_channel; |
| 163 int sock_addr; |
| 164 int app_addr; |
| 165 int nexe_fd; |
| 166 struct NaClSrpcChannel command_channel; |
| 167 int app_conn; |
| 168 struct NaClSrpcChannel app_channel; |
| 169 |
| 170 status = get_kernel_channel(&kernel_channel); |
| 171 if (0 != status) { |
| 172 return status; |
| 173 } |
| 174 |
| 175 pthread_mutex_lock(&kernel_service_mutex); |
| 176 if (NACL_SRPC_RESULT_OK == NaClSrpcInvokeBySignature( |
| 177 kernel_channel, NACL_KERNEL_SERVICE_CREATE_PROCESS, |
| 178 &status, &sock_addr, &app_addr)) { |
| 179 status = 0; |
| 180 } |
| 181 pthread_mutex_unlock(&kernel_service_mutex); |
| 182 |
| 183 if (!NaClLdrSetupCommandChannel(sock_addr, &command_channel)) { |
| 184 print_error(stderr, "Failed to setup command channel\n"); |
| 185 return 0; |
| 186 } |
| 187 |
| 188 status = nacl_irt_resource_open.open_resource(path, &nexe_fd); |
| 189 if (0 != status) { |
| 190 print_error(stderr, "Failed to open module nexe\n"); |
| 191 goto cleanup_channel; |
| 192 } |
| 193 |
| 194 if (!NaClLdrLoadModule(&command_channel, nexe_fd)) { |
| 195 print_error(stderr, "Failed to load module\n"); |
| 196 goto cleanup_channel; |
| 197 } |
| 198 |
| 199 if (!NaClLdrStartModule(&command_channel)) { |
| 200 print_error(stderr, "Failed to start module\n"); |
| 201 goto cleanup_channel; |
| 202 } |
| 203 |
| 204 app_conn = imc_connect(app_addr); |
| 205 close(app_addr); |
| 206 if (-1 == app_conn) { |
| 207 print_error("Error connecting to application\n"); |
| 208 return EIO; |
| 209 } |
| 210 if (!NaClSrpcClientCtor(&app_channel, app_conn)) { |
| 211 fprintf(stderr, "Application SRPC channel ctor failed\n"); |
| 212 return 1; |
| 213 } |
| 214 |
| 215 cleanup_channel: |
| 216 NaClSrpcDtor(&command_channel); |
| 217 return status; |
| 218 } |
| 219 |
| 220 static sighandler_t nacl_irt_signal(int sig, sighandler_t handler) { |
| 221 return ENOSYS; |
| 222 } |
| 223 |
| 224 static int nacl_irt_sigprocmask(int how, const sigset_t *set, siegset_t *oldset)
{ |
| 225 return ENOSYS; |
| 226 } |
| 227 |
| 228 static int nacl_irt_wait(int *pid, int *status) { |
| 229 return ENOSYS; |
| 230 } |
| 231 |
| 232 static int nacl_irt_waitpid(int *pid, int *status, int options) { |
| 233 return ENOSYS; |
| 234 } |
| 235 |
| 236 const struct nacl_irt_dev_process nacl_irt_dev_process = { |
| 237 nacl_irt_getpid, |
| 238 nacl_irt_getppid, |
| 239 nacl_irt_setpgid, |
| 240 nacl_irt_getpgid, |
| 241 nacl_irt_getsid, |
| 242 nacl_irt_setsid, |
| 243 nacl_irt_kill, |
| 244 nacl_irt_posix_spawn, |
| 245 nacl_irt_signal, |
| 246 nacl_irt_sigprocmask, |
| 247 nacl_irt_wait, |
| 248 nacl_irt_waitpid, |
| 249 }; |
| 250 |
| 251 void SignalHandler(struct NaClSrpcRpc *rpc, |
| 252 struct NaClSrpcArg *args[], |
| 253 struct NaClSrpcArg *rets[], |
| 254 struct NaClSrpcClosure *done) { |
| 255 int signal = args[0]->u.ival; |
| 256 |
| 257 |
| 258 fflush(stdout); |
| 259 (*done->Run)(done); |
| 260 } |
| 261 |
| 262 struct NaClSrpcHandlerDesc const kProcessHandlers[] = { |
| 263 { "signal:i:", SignalHandler, }, |
| 264 { (char const *) NULL, (NaClSrpcMethod) NULL, }, |
| 265 }; |
| 266 |
| 267 static void *RpcHandler(void *arg) { |
| 268 int desc = (int) arg; |
| 269 if (!NaClSrpcServerLoop(desc, kProcessHandlers, NULL)) { |
| 270 print_error(stderr, "NaClSrpcServerLoop failed\n"); |
| 271 abort(); |
| 272 } |
| 273 (void) close(desc); |
| 274 return (void *) NULL; |
| 275 } |
| 276 |
| 277 static void irt_process_new(int argc, char **args) { |
| 278 int d; |
| 279 |
| 280 NaClSrpcModuleInit(); |
| 281 while ((d = imc_accept(3)) != -1) { |
| 282 pthread_t thread; |
| 283 if (0 != pthread_create(&thread, (pthread_attr_t *) NULL, |
| 284 RpcHandler, (void *) d)) { |
| 285 print_error("pthread_create for RpcHandler"); |
| 286 abort(); |
| 287 } |
| 288 } |
| 289 NaClSrpcModuleFini(); |
| 290 } |
| 291 |
| 292 NACL_IRT_REGISTER_MODULE(irt_process, irt_process_new) |
OLD | NEW |