Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <string.h> | |
| 8 | |
| 7 #include "native_client/src/trusted/reverse_service/reverse_service.h" | 9 #include "native_client/src/trusted/reverse_service/reverse_service.h" |
| 8 | 10 |
| 9 #include "native_client/src/include/nacl_compiler_annotations.h" | 11 #include "native_client/src/include/nacl_compiler_annotations.h" |
| 10 #include "native_client/src/include/nacl_scoped_ptr.h" | 12 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 11 | 13 #include "native_client/src/include/portability_io.h" |
| 12 #include "native_client/src/shared/platform/nacl_log.h" | 14 #include "native_client/src/shared/platform/nacl_log.h" |
| 13 #include "native_client/src/shared/platform/nacl_sync.h" | 15 #include "native_client/src/shared/platform/nacl_sync.h" |
| 14 #include "native_client/src/shared/platform/nacl_sync_checked.h" | 16 #include "native_client/src/shared/platform/nacl_sync_checked.h" |
| 15 #include "native_client/src/shared/platform/nacl_threads.h" | 17 #include "native_client/src/shared/platform/nacl_threads.h" |
| 16 #include "native_client/src/shared/srpc/nacl_srpc.h" | 18 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 17 | 19 |
| 20 #include "native_client/src/trusted/desc/nacl_desc_invalid.h" | |
| 21 | |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 void Test(NaClSrpcRpc* rpc, | 24 void Test(NaClSrpcRpc* rpc, |
| 21 NaClSrpcArg** in_args, | 25 NaClSrpcArg** in_args, |
| 22 NaClSrpcArg** out_args, | 26 NaClSrpcArg** out_args, |
| 23 NaClSrpcClosure* done) { | 27 NaClSrpcClosure* done) { |
| 24 char *msg = in_args[0]->arrays.str; | 28 char *msg = in_args[0]->arrays.str; |
| 25 UNREFERENCED_PARAMETER(out_args); | 29 UNREFERENCED_PARAMETER(out_args); |
| 26 // use rpc->channel rather than rpc->channel->server_instance_data | 30 // use rpc->channel rather than rpc->channel->server_instance_data |
| 27 // to show that Test RPCs arrive in different channels. | 31 // to show that Test RPCs arrive in different channels. |
| 28 NaClLog(1, "Test: [%"NACL_PRIxPTR"] %s\n", | 32 NaClLog(1, "Test: [%"NACL_PRIxPTR"] %s\n", |
| 29 reinterpret_cast<uintptr_t>(rpc->channel), msg); | 33 reinterpret_cast<uintptr_t>(rpc->channel), msg); |
| 30 rpc->result = NACL_SRPC_RESULT_OK; | 34 rpc->result = NACL_SRPC_RESULT_OK; |
| 31 done->Run(done); | 35 done->Run(done); |
| 32 } | 36 } |
| 33 | 37 |
| 34 void AddChannel(NaClSrpcRpc* rpc, | 38 void AddChannel(NaClSrpcRpc* rpc, |
| 35 NaClSrpcArg** in_args, | 39 NaClSrpcArg** in_args, |
| 36 NaClSrpcArg** out_args, | 40 NaClSrpcArg** out_args, |
| 37 NaClSrpcClosure* done) { | 41 NaClSrpcClosure* done) { |
| 38 nacl::ReverseService* service = reinterpret_cast<nacl::ReverseService*>( | 42 nacl::ReverseService* service = reinterpret_cast<nacl::ReverseService*>( |
| 39 rpc->channel->server_instance_data); | 43 rpc->channel->server_instance_data); |
| 44 | |
| 40 UNREFERENCED_PARAMETER(in_args); | 45 UNREFERENCED_PARAMETER(in_args); |
| 41 UNREFERENCED_PARAMETER(out_args); | 46 |
| 42 NaClLog(4, "Entered AddChannel\n"); | 47 NaClLog(4, "Entered AddChannel\n"); |
| 43 out_args[0]->u.bval = service->Start(); | 48 out_args[0]->u.bval = service->Start(); |
| 44 NaClLog(4, "Leaving AddChannel\n"); | 49 NaClLog(4, "Leaving AddChannel\n"); |
| 45 rpc->result = NACL_SRPC_RESULT_OK; | 50 rpc->result = NACL_SRPC_RESULT_OK; |
| 46 done->Run(done); | 51 done->Run(done); |
| 47 } | 52 } |
| 48 | 53 |
| 49 void RevLog(NaClSrpcRpc* rpc, | 54 void RevLog(NaClSrpcRpc* rpc, |
| 50 NaClSrpcArg** in_args, | 55 NaClSrpcArg** in_args, |
| 51 NaClSrpcArg** out_args, | 56 NaClSrpcArg** out_args, |
| 52 NaClSrpcClosure* done) { | 57 NaClSrpcClosure* done) { |
| 53 nacl::ReverseService* service = reinterpret_cast<nacl::ReverseService*>( | 58 nacl::ReverseService* service = reinterpret_cast<nacl::ReverseService*>( |
| 54 rpc->channel->server_instance_data); | 59 rpc->channel->server_instance_data); |
| 55 UNREFERENCED_PARAMETER(out_args); | 60 UNREFERENCED_PARAMETER(out_args); |
| 56 char* message = in_args[0]->arrays.str; | 61 char* message = in_args[0]->arrays.str; |
| 57 | 62 |
| 58 if (NULL == service->reverse_interface()) { | 63 if (NULL == service->reverse_interface()) { |
| 59 NaClLog(1, "Log RPC, no reverse_interface. Message: %s\n", message); | 64 NaClLog(1, "Log RPC, no reverse_interface. Message: %s\n", message); |
| 60 } else { | 65 } else { |
| 61 service->reverse_interface()->Log(message); | 66 service->reverse_interface()->Log(message); |
| 62 } | 67 } |
| 63 done->Run(done); | 68 done->Run(done); |
| 64 } | 69 } |
| 65 | 70 |
| 71 // Manifest name service, internal APIs. | |
| 72 // | |
| 73 // Manifest file lookups result in read-only file descriptors with a | |
| 74 // handle. When the descriptor is closed, the service runtime must | |
| 75 // inform the plugin of this using the handle, so that the File object | |
| 76 // reference can be closed (thereby allowing the browser to delete or | |
| 77 // otherwise garbage collect the file). Files, being from the | |
| 78 // manifest, cannot be deleted. The manifest is also a read-only | |
| 79 // object, so no new entries can be made to it. | |
| 80 // | |
| 81 // Read-only proxies do not require quota support per se, since we do | |
| 82 // not limit read bandwidth. Quota support is needed for storage | |
| 83 // limits, though could also be used to limit write bandwidth (prevent | |
| 84 // disk output saturation, limit malicious code's ability to cause | |
| 85 // disk failures, especially with flash disks with limited write | |
| 86 // cycles). | |
| 87 | |
| 88 // NACL_MANIFEST_LIST list::C -- enumerate all names in the manifest | |
| 89 void ManifestListRpc(NaClSrpcRpc* rpc, | |
| 90 NaClSrpcArg** in_args, | |
| 91 NaClSrpcArg** out_args, | |
| 92 NaClSrpcClosure* done) { | |
| 93 UNREFERENCED_PARAMETER(in_args); | |
| 94 // temporary test return value. TODO(bsy) hook up to real manifest info | |
| 95 out_args[0]->u.count = SNPRINTF(out_args[0]->arrays.carr, | |
| 96 out_args[0]->u.count, | |
| 97 "This is a reply from the manifest reverse" | |
| 98 " service in the plugin."); | |
| 99 rpc->result = NACL_SRPC_RESULT_OK; | |
| 100 done->Run(done); | |
| 101 } | |
| 102 | |
| 103 // NACL_MANIFEST_LOOKUP lookup:si:ihC -- look up by string name, | |
| 104 // resulting in a handle (if name is in the preimage), a object proxy | |
| 105 // handle, and an error code. | |
| 106 void ManifestLookupRpc(NaClSrpcRpc* rpc, | |
| 107 NaClSrpcArg** in_args, | |
| 108 NaClSrpcArg** out_args, | |
| 109 NaClSrpcClosure* done) { | |
| 110 char* fname = in_args[0]->arrays.str; | |
| 111 int flags = in_args[0]->u.ival; | |
| 112 | |
| 113 NaClLog(0, "ManifestLookupRpc: %s, %d\n", fname, flags); | |
| 114 out_args[0]->u.ival = 0; // ok | |
| 115 out_args[1]->u.hval = (struct NaClDesc*) NaClDescInvalidMake(); | |
| 116 // placeholders. these rpc handlers will be replaced with code that | |
| 117 // actually do lookups/URL fetches. | |
|
polina
2011/06/15 22:50:12
Please capitalize and add a matching comment to th
bsy
2011/06/15 23:26:57
Done.
| |
| 118 out_args[2]->u.count = 10; | |
| 119 strncpy(out_args[2]->arrays.carr, "123456789", 10); | |
| 120 rpc->result = NACL_SRPC_RESULT_OK; | |
| 121 done->Run(done); | |
| 122 } | |
| 123 | |
| 124 // NACL_MANIFEST_UNREF unref:C:i -- dereferences the file by object | |
| 125 // proxy handle. The file descriptor should have been closed. | |
| 126 void ManifestUnrefRpc(NaClSrpcRpc* rpc, | |
| 127 NaClSrpcArg** in_args, | |
| 128 NaClSrpcArg** out_args, | |
| 129 NaClSrpcClosure* done) { | |
| 130 char* proxy_handle = in_args[0]->arrays.carr; | |
| 131 | |
| 132 NaClLog(0, "ManifestUnrefRpc: %s\n", proxy_handle); | |
| 133 out_args[0]->u.ival = 0; // ok | |
| 134 rpc->result = NACL_SRPC_RESULT_OK; | |
| 135 done->Run(done); | |
| 136 } | |
| 137 | |
| 66 } // namespace | 138 } // namespace |
| 67 | 139 |
| 68 namespace nacl { | 140 namespace nacl { |
| 69 | 141 |
| 70 // need NaClThreadIfFactoryFunction that keeps "this" to do counting | 142 // need NaClThreadIfFactoryFunction that keeps "this" to do counting |
| 71 | 143 |
| 72 struct ReverseCountingThreadInterface { | 144 struct ReverseCountingThreadInterface { |
| 73 struct NaClThreadInterface base NACL_IS_REFCOUNT_SUBCLASS; | 145 struct NaClThreadInterface base NACL_IS_REFCOUNT_SUBCLASS; |
| 74 nacl::ReverseService* rev; | 146 nacl::ReverseService* rev; |
| 75 }; | 147 }; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 NaClThreadInterfaceVtbl const kReverseThreadInterfaceVtbl = { | 249 NaClThreadInterfaceVtbl const kReverseThreadInterfaceVtbl = { |
| 178 { | 250 { |
| 179 ReverseThreadIfDtor, | 251 ReverseThreadIfDtor, |
| 180 }, | 252 }, |
| 181 NaClThreadInterfaceStartThread, | 253 NaClThreadInterfaceStartThread, |
| 182 ReverseThreadIfLaunchCallback, | 254 ReverseThreadIfLaunchCallback, |
| 183 ReverseThreadIfExit, | 255 ReverseThreadIfExit, |
| 184 }; | 256 }; |
| 185 | 257 |
| 186 NaClSrpcHandlerDesc const ReverseService::handlers[] = { | 258 NaClSrpcHandlerDesc const ReverseService::handlers[] = { |
| 187 { "test:s:", Test, }, | 259 { NACL_REVERSE_CONTROL_TEST, Test, }, |
| 188 { "revlog:s:", RevLog, }, | 260 { NACL_REVERSE_CONTROL_LOG, RevLog, }, |
| 189 { "add_channel::b", AddChannel, }, | 261 { NACL_REVERSE_CONTROL_ADD_CHANNEL, AddChannel, }, |
| 262 { NACL_MANIFEST_LIST, ManifestListRpc, }, | |
| 263 { NACL_MANIFEST_LOOKUP, ManifestLookupRpc, }, | |
| 264 { NACL_MANIFEST_UNREF, ManifestUnrefRpc, }, | |
| 190 { NULL, NULL, }, | 265 { NULL, NULL, }, |
| 191 }; | 266 }; |
| 192 | 267 |
| 193 ReverseService::ReverseService(nacl::DescWrapper* conn_cap, | 268 ReverseService::ReverseService(nacl::DescWrapper* conn_cap, |
| 194 ReverseInterface* rif) | 269 ReverseInterface* rif) |
| 195 : service_socket_(NULL), | 270 : service_socket_(NULL), |
| 196 reverse_interface_(rif), | 271 reverse_interface_(rif), |
| 197 thread_count_(0) { | 272 thread_count_(0) { |
| 198 /* | 273 /* |
| 199 * We wait for service threads to exit before dtor'ing, so the | 274 * We wait for service threads to exit before dtor'ing, so the |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 213 ReverseService::~ReverseService() { | 288 ReverseService::~ReverseService() { |
| 214 if (thread_count_ != 0) { | 289 if (thread_count_ != 0) { |
| 215 NaClLog(LOG_FATAL, "ReverseService dtor when thread count is nonzero\n"); | 290 NaClLog(LOG_FATAL, "ReverseService dtor when thread count is nonzero\n"); |
| 216 } | 291 } |
| 217 NaClCondVarDtor(&cv_); | 292 NaClCondVarDtor(&cv_); |
| 218 NaClMutexDtor(&mu_); | 293 NaClMutexDtor(&mu_); |
| 219 } | 294 } |
| 220 | 295 |
| 221 | 296 |
| 222 bool ReverseService::Start() { | 297 bool ReverseService::Start() { |
| 298 NaClLog(4, "Entered ReverseService::Start\n"); | |
| 223 return service_socket_->StartService(reinterpret_cast<void*>(this)); | 299 return service_socket_->StartService(reinterpret_cast<void*>(this)); |
| 224 } | 300 } |
| 225 | 301 |
| 226 void ReverseService::WaitForServiceThreadsToExit() { | 302 void ReverseService::WaitForServiceThreadsToExit() { |
| 227 NaClLog(4, "ReverseService::WaitForServiceThreadsToExit\n"); | 303 NaClLog(4, "ReverseService::WaitForServiceThreadsToExit\n"); |
| 228 NaClXMutexLock(&mu_); | 304 NaClXMutexLock(&mu_); |
| 229 while (0 != thread_count_) { | 305 while (0 != thread_count_) { |
| 230 NaClXCondVarWait(&cv_, &mu_); | 306 NaClXCondVarWait(&cv_, &mu_); |
| 231 NaClLog(5, "ReverseService::WaitForServiceThreadsToExit: woke up\n"); | 307 NaClLog(5, "ReverseService::WaitForServiceThreadsToExit: woke up\n"); |
| 232 } | 308 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 252 ("ReverseService::DecrThreadCount:" | 328 ("ReverseService::DecrThreadCount:" |
| 253 " Decrementing thread count when count is zero\n")); | 329 " Decrementing thread count when count is zero\n")); |
| 254 } | 330 } |
| 255 if (0 == --thread_count_) { | 331 if (0 == --thread_count_) { |
| 256 NaClXCondVarBroadcast(&cv_); | 332 NaClXCondVarBroadcast(&cv_); |
| 257 } | 333 } |
| 258 NaClXMutexUnlock(&mu_); | 334 NaClXMutexUnlock(&mu_); |
| 259 } | 335 } |
| 260 | 336 |
| 261 } // namespace nacl | 337 } // namespace nacl |
| OLD | NEW |