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.
7 #include "native_client/tests/subprocess/process_lib.h"
14 // TODO(bsy) get rid of debug printfs
16 // The header files below access private, not-guaranteed-to-be-stable
18 #include "native_client/src/public/imc_syscalls.h"
19 #include "native_client/src/public/name_service.h"
20 #include "native_client/src/public/secure_service.h"
21 #include "native_client/src/trusted/service_runtime/include/bits/nacl_syscalls.h"
22 #include "native_client/src/trusted/service_runtime/include/sys/nacl_kernel_service.h"
23 // get NACL_SYSCALL_BLOCK_SIZE, NACL_SYSCALL_START_ADDR
24 #include "native_client/src/trusted/service_runtime/nacl_config.h"
26 namespace NaClProcessLib {
28 SrpcClientConnection::SrpcClientConnection() : initialized_(false) {}
30 SrpcClientConnection::~SrpcClientConnection() {
37 bool SrpcClientConnection::InitializeFromConnectedDesc(int desc) {
38 if (!NaClSrpcClientCtor(&chan_, desc)) {
40 "SrpcClientConnection::InitializeFromConnectedDesc failed\n");
48 bool SrpcClientConnection::InitializeFromConnectionCapability(int cap) {
49 int conn = imc_connect(cap);
52 "SrpcClientConnection:InitializeFromConnectionCapability"
53 " imc_connect failed, error %d\n", errno);
56 return InitializeFromConnectedDesc(conn);
59 int NameServiceClient::Resolve(std::string name) {
60 NaClSrpcResultCodes result;
66 result = NaClSrpcInvokeBySignature(chan(), NACL_NAME_SERVICE_LOOKUP,
67 name.c_str(), O_RDONLY,
69 if (NACL_SRPC_RESULT_OK != result) {
70 fprintf(stderr, "Service lookup RPC failed (%d): %s\n", result,
71 NaClSrpcErrorString(result));
74 if (NACL_NAME_SERVICE_SUCCESS != status) {
75 fprintf(stderr, "Resolve failed for \"%s\"; error %d\n", name.c_str(),
83 int NameServiceFactory::name_service_cap;
84 NameServiceFactory *NameServiceFactory::singleton = NULL;
86 NameServiceFactory *NameServiceFactory::NameServiceFactorySingleton() {
90 NameServiceFactory::NameServiceFactory() {}
92 typedef int (*nameservice_tramp_t)(int *desc);
94 bool NameServiceFactory::Init() {
95 if (NULL != singleton) {
98 nameservice_tramp_t nameservice_tramp =
99 reinterpret_cast<nameservice_tramp_t>(
100 (NACL_SYSCALL_START_ADDR +
101 NACL_SYSCALL_BLOCK_SIZE * NACL_sys_nameservice));
103 name_service_cap = -1;
104 if (-1 == (*nameservice_tramp)(&name_service_cap)) {
107 singleton = new NameServiceFactory();
108 return (NULL != singleton);
111 NameServiceClient *NameServiceFactory::NameService() {
112 NameServiceClient *client = new NameServiceClient();
113 if (!client->InitializeFromConnectionCapability(name_service_cap)) {
120 bool KernelServiceClient::CreateProcess(int *child_sockaddr,
122 if (!initialized()) {
125 NaClSrpcResultCodes result;
127 result = NaClSrpcInvokeBySignature(chan(), NACL_KERNEL_SERVICE_CREATE_PROCESS,
128 &status, child_sockaddr, app_sockaddr);
129 if (NACL_SRPC_RESULT_OK != result) {
130 fprintf(stderr, "create process failed (%d): %s\n", result,
131 NaClSrpcErrorString(result));
137 ServiceRuntimeClient* KernelServiceClient::ServiceRuntimeClientFactory(
138 int child_sockaddr) {
139 ServiceRuntimeClient* src = new ServiceRuntimeClient;
140 if (!src->InitializeFromConnectionCapability(child_sockaddr)) {
147 NaClErrorCode ServiceRuntimeClient::RunNaClModule(int module_descriptor) {
148 NaClSrpcResultCodes result;
150 if (!initialized()) {
151 return LOAD_INTERNAL;
153 printf("Entered RunNaClModule\n");
154 printf("chan: %p\n", (void*) chan());
155 result = NaClSrpcInvokeBySignature(chan(), NACL_SECURE_SERVICE_LOAD_MODULE,
157 printf("load_module result %d\n", result);
158 if (NACL_SRPC_RESULT_OK != result) {
159 fprintf(stderr, "load_module RPC failed (%d): %s\n",
160 result, NaClSrpcErrorString(result));
161 return LOAD_INTERNAL;
163 result = NaClSrpcInvokeBySignature(chan(), "start_module::i", &status);
164 printf("start_module result %d\n", result);
165 if (NACL_SRPC_RESULT_OK != result) {
166 fprintf(stderr, "start_module RPC failed (%d): %s\n",
167 result, NaClSrpcErrorString(result));
168 return LOAD_INTERNAL;
170 printf("start_module status: %d\n", status);
171 return static_cast<NaClErrorCode>(status);
174 } // namespace NaClProcessLib