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