Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include <nacl/nacl_srpc.h> | |
| 2 #include <stdio.h> | |
| 3 #include <string.h> | |
| 4 #include <sys/fcntl.h> | |
| 5 #include <sys/nacl_name_service.h> | |
| 6 #include <sys/nacl_syscalls.h> | |
| 7 | |
| 8 #include "native_client/src/untrusted/irt/irt.h" | |
| 9 #include "native_client/src/untrusted/irt/irt_interfaces.h" | |
| 10 | |
| 11 static void print_error(const char *message) { | |
| 12 write(2, message, strlen(message)); | |
| 13 } | |
| 14 | |
| 15 /* Mutex to guard name service channel initialization. */ | |
| 16 static int name_service_mutex; | |
| 17 static int ns_channel_initialized = 0; | |
| 18 static struct NaClSrpcChannel ns_channel; | |
| 19 | |
| 20 /* IRT is static, we don't need destructor. */ | |
| 21 __attribute__((constructor)) void init_mutex() { | |
| 22 nacl_irt_mutex.mutex_create(&name_service_mutex); | |
| 23 } | |
| 24 | |
| 25 /* | |
| 26 * Returns pointer to ns_channel or 0 if lazy initialization of name service | |
| 27 * channel has failed. | |
| 28 */ | |
| 29 struct NaClSrpcChannel *get_nameservice_channel() { | |
|
Roland McGrath
2011/08/12 16:44:56
C functions with no arguments have a prototype of
halyavin
2011/08/15 10:35:52
Done.
| |
| 30 int ns; | |
| 31 int connected_socket; | |
| 32 struct NaClSrpcChannel *result = 0; | |
| 33 nacl_irt_mutex.mutex_lock(name_service_mutex); | |
| 34 do { | |
|
Roland McGrath
2011/08/12 16:44:56
Using do {...} while (0) this way is no better tha
halyavin
2011/08/15 10:35:52
I extracted subfunction here.
| |
| 35 if (ns_channel_initialized) { | |
| 36 result = &ns_channel; | |
| 37 break; | |
| 38 } | |
| 39 ns = -1; | |
| 40 nacl_nameservice(&ns); | |
| 41 if (-1 == ns) { | |
| 42 print_error("Can't get name service descriptor\n"); | |
| 43 break; | |
| 44 } | |
| 45 connected_socket = imc_connect(ns); | |
| 46 if (-1 == connected_socket) { | |
| 47 print_error("Can't connect to name service\n"); | |
| 48 break; | |
| 49 } | |
| 50 close(ns); | |
| 51 if (!NaClSrpcClientCtor(&ns_channel, connected_socket)) { | |
| 52 print_error("Srpc client channel ctor failed\n"); | |
| 53 break; | |
| 54 } | |
| 55 result = &ns_channel; | |
| 56 ns_channel_initialized = 1; | |
| 57 } while (0); | |
| 58 nacl_irt_mutex.mutex_unlock(name_service_mutex); | |
| 59 return result; | |
| 60 } | |
| 61 | |
| 62 /* | |
| 63 * Returns file descriptor or -1 if some error happened. | |
|
Roland McGrath
2011/08/12 16:44:56
The norm in IRT interfaces is to return an errno c
halyavin
2011/08/15 10:46:03
Done.
| |
| 64 */ | |
| 65 int irt_open_file_in_manifest(const char *file) { | |
| 66 struct NaClSrpcChannel* ns_channel; | |
| 67 int status; | |
| 68 int desc; | |
| 69 int manifest; | |
| 70 int manifest_conn; | |
| 71 struct NaClSrpcChannel manifest_channel; | |
| 72 ns_channel = get_nameservice_channel(); | |
| 73 if (0 == ns_channel) { | |
| 74 return -1; | |
| 75 } | |
| 76 if (NACL_SRPC_RESULT_OK != NaClSrpcInvokeBySignature( | |
|
Roland McGrath
2011/08/12 16:44:56
Why repeat this work in every call? Shouldn't we
halyavin
2011/08/15 10:35:52
Done.
| |
| 77 ns_channel, NACL_NAME_SERVICE_LOOKUP, "ManifestNameService", O_RDWR, | |
| 78 &status, &manifest)) { | |
| 79 print_error("Nameservice lookup failed, status\n"); | |
| 80 return -1; | |
| 81 } | |
| 82 if (-1 == manifest) { | |
| 83 print_error("Manifest descriptor is invalid\n"); | |
| 84 return -1; | |
| 85 } | |
| 86 manifest_conn = imc_connect(manifest); | |
| 87 if (-1 == manifest_conn) { | |
| 88 print_error("Can't connect to manifest service\n"); | |
| 89 return -1; | |
| 90 } | |
| 91 close(manifest); | |
| 92 if (!NaClSrpcClientCtor(&manifest_channel, manifest_conn)) { | |
|
Roland McGrath
2011/08/12 16:44:56
We can also cache the manifest channel struct, no?
halyavin
2011/08/15 10:35:52
Done.
| |
| 93 print_error("Can't create manifest srpc channel\n"); | |
| 94 return -1; | |
| 95 } | |
| 96 desc = 0; | |
| 97 if (NACL_SRPC_RESULT_OK != NaClSrpcInvokeBySignature( | |
| 98 &manifest_channel, NACL_NAME_SERVICE_LOOKUP, file, O_RDONLY, | |
| 99 &status, &desc)) { | |
| 100 print_error("Manifest lookup RPC failed\n"); | |
|
Roland McGrath
2011/08/12 16:44:56
This error case should not print. The others are
halyavin
2011/08/15 10:35:52
Done.
| |
| 101 NaClSrpcDtor(&manifest_channel); | |
| 102 return -1; | |
| 103 } | |
| 104 NaClSrpcDtor(&manifest_channel); | |
| 105 return desc; | |
| 106 } | |
| 107 | |
| 108 const struct nacl_irt_manifest_open nacl_irt_manifest_open = { | |
| 109 irt_open_file_in_manifest | |
| 110 }; | |
| OLD | NEW |