Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include <errno.h> | |
|
pasko-google - do not use
2011/08/15 16:23:04
system headers first, then nacl system headers, af
halyavin
2011/08/16 09:05:37
Done.
| |
| 2 #include <nacl/nacl_srpc.h> | |
| 3 #include <stdio.h> | |
| 4 #include <string.h> | |
| 5 #include <sys/fcntl.h> | |
| 6 #include <sys/nacl_name_service.h> | |
| 7 #include <sys/nacl_syscalls.h> | |
| 8 | |
| 9 #include "native_client/src/untrusted/irt/irt.h" | |
| 10 #include "native_client/src/untrusted/irt/irt_interfaces.h" | |
| 11 | |
| 12 static void print_error(const char *message) { | |
| 13 write(2, message, strlen(message)); | |
| 14 } | |
| 15 | |
| 16 /* | |
| 17 * TODO(halyavin): move to separate file because name service channel can be | |
| 18 * usefull for other APIs. | |
|
pasko-google - do not use
2011/08/15 16:23:04
s/usefull/useful/
frankly, I do not see the reaso
halyavin
2011/08/16 09:05:37
I want to remember to do this.
| |
| 19 */ | |
| 20 /* Mutex to guard name service channel initialization. */ | |
| 21 static int name_service_mutex; | |
| 22 static int ns_channel_initialized = 0; | |
| 23 static struct NaClSrpcChannel ns_channel; | |
| 24 | |
| 25 /* IRT is static, we don't need destructor. */ | |
| 26 __attribute__((constructor)) void init_ns_mutex(void) { | |
| 27 nacl_irt_mutex.mutex_create(&name_service_mutex); | |
| 28 } | |
| 29 | |
| 30 int get_nameservice_channel_locked(struct NaClSrpcChannel **result) { | |
|
pasko-google - do not use
2011/08/15 16:23:04
should be static
halyavin
2011/08/16 09:05:37
Done.
| |
| 31 int ns; | |
| 32 int connected_socket; | |
| 33 if (ns_channel_initialized) { | |
| 34 *result = &ns_channel; | |
| 35 return 0; | |
| 36 } | |
| 37 *result = 0; | |
| 38 ns = -1; | |
| 39 nacl_nameservice(&ns); | |
| 40 if (-1 == ns) { | |
| 41 print_error("Can't get name service descriptor\n"); | |
| 42 return -EIO; | |
|
pasko-google - do not use
2011/08/15 16:23:04
the convention is to return the *same* value as er
halyavin
2011/08/16 09:05:37
Done.
| |
| 43 } | |
| 44 connected_socket = imc_connect(ns); | |
| 45 if (-1 == connected_socket) { | |
| 46 print_error("Can't connect to name service\n"); | |
| 47 return -EIO; | |
| 48 } | |
| 49 close(ns); | |
| 50 if (!NaClSrpcClientCtor(&ns_channel, connected_socket)) { | |
| 51 print_error("Srpc client channel ctor failed\n"); | |
| 52 return -EIO; | |
| 53 } | |
| 54 *result = &ns_channel; | |
| 55 ns_channel_initialized = 1; | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 /* | |
| 60 * Get name service channel. | |
| 61 * If successfull, function sets pointer to name service channel and returns 0. | |
| 62 * In case of error, function sets pointer to zero and returns error code. | |
| 63 */ | |
| 64 int get_nameservice_channel(struct NaClSrpcChannel **result) { | |
|
pasko-google - do not use
2011/08/15 16:23:04
should be static
halyavin
2011/08/16 09:05:37
This function may be extracted to the separate fil
| |
| 65 int error; | |
| 66 nacl_irt_mutex.mutex_lock(name_service_mutex); | |
| 67 error = get_nameservice_channel_locked(result); | |
| 68 nacl_irt_mutex.mutex_unlock(name_service_mutex); | |
| 69 return error; | |
| 70 } | |
| 71 | |
| 72 /* | |
| 73 * We use separate mutex so that nameservice initialization can be moved to | |
| 74 * another file. In this case name_service_mutex would not be public. | |
| 75 */ | |
| 76 /* Mutex to guard manifest channel initialization. */ | |
| 77 static int manifest_service_mutex; | |
| 78 static int manifest_channel_initialized = 0; | |
| 79 static struct NaClSrpcChannel manifest_channel; | |
| 80 | |
| 81 __attribute__((constructor)) void init_manifest_mutex(void) { | |
| 82 nacl_irt_mutex.mutex_create(&manifest_service_mutex); | |
| 83 } | |
| 84 | |
| 85 int get_manifest_channel_locked(struct NaClSrpcChannel **result) { | |
|
pasko-google - do not use
2011/08/15 16:23:04
should be static
halyavin
2011/08/16 09:05:37
Done.
| |
| 86 int status; | |
| 87 int manifest; | |
| 88 int manifest_conn; | |
| 89 struct NaClSrpcChannel* ns_channel; | |
| 90 if (manifest_channel_initialized) { | |
| 91 *result = &manifest_channel; | |
| 92 return 0; | |
| 93 } | |
| 94 *result = 0; | |
| 95 status = get_nameservice_channel(&ns_channel); | |
| 96 if (0 != status) { | |
| 97 return status; | |
| 98 } | |
| 99 if (NACL_SRPC_RESULT_OK != NaClSrpcInvokeBySignature( | |
| 100 ns_channel, NACL_NAME_SERVICE_LOOKUP, "ManifestNameService", O_RDWR, | |
| 101 &status, &manifest)) { | |
| 102 print_error("Nameservice lookup failed, status\n"); | |
| 103 return -EIO; | |
| 104 } | |
| 105 if (-1 == manifest) { | |
| 106 print_error("Manifest descriptor is invalid\n"); | |
| 107 return -EIO; | |
| 108 } | |
| 109 manifest_conn = imc_connect(manifest); | |
| 110 if (-1 == manifest_conn) { | |
| 111 print_error("Can't connect to manifest service\n"); | |
| 112 return -EIO; | |
| 113 } | |
| 114 close(manifest); | |
| 115 if (!NaClSrpcClientCtor(&manifest_channel, manifest_conn)) { | |
| 116 print_error("Can't create manifest srpc channel\n"); | |
| 117 return -EIO; | |
| 118 } | |
| 119 *result = &manifest_channel; | |
| 120 manifest_channel_initialized = 1; | |
| 121 return 0; | |
| 122 } | |
| 123 | |
| 124 int get_manifest_channel(struct NaClSrpcChannel **result) { | |
| 125 int error; | |
| 126 nacl_irt_mutex.mutex_lock(manifest_service_mutex); | |
| 127 error = get_manifest_channel_locked(result); | |
| 128 nacl_irt_mutex.mutex_unlock(manifest_service_mutex); | |
| 129 return error; | |
| 130 } | |
| 131 | |
| 132 /* | |
| 133 * Returns file descriptor or negative error code. | |
|
pasko-google - do not use
2011/08/15 16:23:04
again, according to the common IRT convention, err
halyavin
2011/08/16 09:05:37
Done.
| |
| 134 */ | |
| 135 int irt_open_resource(const char *file, int *fd) { | |
|
pasko-google - do not use
2011/08/15 16:23:04
should be static
halyavin
2011/08/16 09:05:37
Done.
| |
| 136 int status; | |
| 137 struct NaClSrpcChannel* manifest_channel; | |
| 138 status = get_manifest_channel(&manifest_channel); | |
| 139 if (0 != status) { | |
| 140 return status; | |
| 141 } | |
| 142 *fd = 0; | |
|
pasko-google - do not use
2011/08/15 16:23:04
touching a value when an error occurred is awkward
halyavin
2011/08/16 09:05:37
Done.
| |
| 143 if (NACL_SRPC_RESULT_OK != NaClSrpcInvokeBySignature( | |
| 144 manifest_channel, NACL_NAME_SERVICE_LOOKUP, file, O_RDONLY, | |
| 145 &status, fd)) { | |
| 146 return status; | |
| 147 } | |
| 148 return 0; | |
| 149 } | |
| 150 | |
| 151 const struct nacl_irt_resource_open nacl_irt_resource_open = { | |
| 152 irt_open_resource | |
| 153 }; | |
| OLD | NEW |