OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <fcntl.h> |
| 6 #include <iostream> |
| 7 |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/logging.h" |
| 10 #include "mojo/nacl/nonsfi/irt_mojo_nonsfi.h" |
| 11 #include "mojo/public/cpp/bindings/string.h" |
| 12 #include "native_client/src/untrusted/irt/irt_dev.h" |
| 13 |
| 14 void serve_link_request(int (*func)(int nexe_fd, |
| 15 const int* obj_file_fds, |
| 16 int obj_file_fd_count)) { |
| 17 // Acquire the handle -- this is our mechanism to contact the |
| 18 // content handler which called us. |
| 19 MojoHandle handle; |
| 20 nacl::_MojoGetInitialHandle(&handle); |
| 21 |
| 22 // Read / Wait until we can get the object file name. |
| 23 MojoResult result; |
| 24 char object_file_name[PATH_MAX]; |
| 25 uint32_t path_size = sizeof(object_file_name); |
| 26 do { |
| 27 result = MojoReadMessage(handle, |
| 28 object_file_name, |
| 29 &path_size, |
| 30 nullptr, |
| 31 nullptr, |
| 32 MOJO_READ_MESSAGE_FLAG_NONE); |
| 33 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 34 result = MojoWait(handle, |
| 35 MOJO_HANDLE_SIGNAL_READABLE, |
| 36 MOJO_DEADLINE_INDEFINITE, |
| 37 nullptr); |
| 38 if (result != MOJO_RESULT_OK) |
| 39 LOG(FATAL) << "Pexe linker could not wait for object file name"; |
| 40 } else { |
| 41 break; |
| 42 } |
| 43 } while (true); |
| 44 if (result != MOJO_RESULT_OK) |
| 45 LOG(FATAL) << "Pexe linker could not read object file name"; |
| 46 object_file_name[path_size] = '\0'; |
| 47 |
| 48 // Create a temporary .nexe file which will be the result of calling our |
| 49 // linker. |
| 50 base::FilePath nexe_file_name; |
| 51 if (!CreateTemporaryFile(&nexe_file_name)) |
| 52 LOG(FATAL) << "Could not create temporary nexe file"; |
| 53 int nexe_file_fd = open(nexe_file_name.value().c_str(), O_RDWR); |
| 54 if (nexe_file_fd < 0) |
| 55 LOG(FATAL) << "Could not create temp file for linked nexe"; |
| 56 |
| 57 // Open our temporary object file. Additionally, unlink it, since it is a |
| 58 // temporary file that is no longer needed after it is opened. |
| 59 size_t obj_file_fd_count = 1; |
| 60 int obj_file_fds[] = { open(object_file_name, O_RDONLY) }; |
| 61 if (unlink(object_file_name)) |
| 62 LOG(FATAL) << "Could not unlink temporary object file"; |
| 63 if (obj_file_fds[0] < 0) |
| 64 LOG(FATAL) << "Could not open object file"; |
| 65 |
| 66 if (func(nexe_file_fd, obj_file_fds, obj_file_fd_count)) |
| 67 LOG(FATAL) << "Error calling func on object file"; |
| 68 |
| 69 // Give the name of the resultant nexe back to the content handler |
| 70 // so it may be launched. |
| 71 result = MojoWriteMessage(handle, |
| 72 nexe_file_name.value().c_str(), |
| 73 nexe_file_name.value().size(), |
| 74 nullptr, |
| 75 0, |
| 76 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 77 |
| 78 if (result != MOJO_RESULT_OK) |
| 79 LOG(FATAL) << "Could not write message to content handler: " << result; |
| 80 } |
| 81 |
| 82 const struct nacl_irt_private_pnacl_translator_link |
| 83 nacl_irt_private_pnacl_translator_link = { |
| 84 serve_link_request |
| 85 }; |
OLD | NEW |