Index: mojo/nacl/nonsfi/irt_pnacl_translator_link.cc |
diff --git a/mojo/nacl/nonsfi/irt_pnacl_translator_link.cc b/mojo/nacl/nonsfi/irt_pnacl_translator_link.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..607445ee503475681d9df467bf3455df4b20351d |
--- /dev/null |
+++ b/mojo/nacl/nonsfi/irt_pnacl_translator_link.cc |
@@ -0,0 +1,85 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <fcntl.h> |
+#include <iostream> |
+ |
+#include "base/files/file_util.h" |
+#include "base/logging.h" |
+#include "mojo/nacl/nonsfi/irt_mojo_nonsfi.h" |
+#include "mojo/public/cpp/bindings/string.h" |
+#include "native_client/src/untrusted/irt/irt_dev.h" |
+ |
+void serve_link_request(int (*func)(int nexe_fd, |
+ const int* obj_file_fds, |
+ int obj_file_fd_count)) { |
+ // Acquire the handle -- this is our mechanism to contact the |
+ // content handler which called us. |
+ MojoHandle handle; |
+ nacl::_MojoGetInitialHandle(&handle); |
+ |
+ // Read / Wait until we can get the object file name. |
+ MojoResult result; |
+ char object_file_name[PATH_MAX]; |
+ uint32_t path_size = sizeof(object_file_name); |
+ do { |
+ result = MojoReadMessage(handle, |
+ object_file_name, |
+ &path_size, |
+ nullptr, |
+ nullptr, |
+ MOJO_READ_MESSAGE_FLAG_NONE); |
+ if (result == MOJO_RESULT_SHOULD_WAIT) { |
+ result = MojoWait(handle, |
+ MOJO_HANDLE_SIGNAL_READABLE, |
+ MOJO_DEADLINE_INDEFINITE, |
+ nullptr); |
+ if (result != MOJO_RESULT_OK) |
+ LOG(FATAL) << "Pexe linker could not wait for object file name"; |
+ } else { |
+ break; |
+ } |
+ } while (true); |
+ if (result != MOJO_RESULT_OK) |
+ LOG(FATAL) << "Pexe linker could not read object file name"; |
+ object_file_name[path_size] = '\0'; |
+ |
+ // Create a temporary .nexe file which will be the result of calling our |
+ // linker. |
+ base::FilePath nexe_file_name; |
+ if (!CreateTemporaryFile(&nexe_file_name)) |
+ LOG(FATAL) << "Could not create temporary nexe file"; |
+ int nexe_file_fd = open(nexe_file_name.value().c_str(), O_RDWR); |
+ if (nexe_file_fd < 0) |
+ LOG(FATAL) << "Could not create temp file for linked nexe"; |
+ |
+ // Open our temporary object file. Additionally, unlink it, since it is a |
+ // temporary file that is no longer needed after it is opened. |
+ size_t obj_file_fd_count = 1; |
+ int obj_file_fds[] = { open(object_file_name, O_RDONLY) }; |
+ if (unlink(object_file_name)) |
+ LOG(FATAL) << "Could not unlink temporary object file"; |
+ if (obj_file_fds[0] < 0) |
+ LOG(FATAL) << "Could not open object file"; |
+ |
+ if (func(nexe_file_fd, obj_file_fds, obj_file_fd_count)) |
+ LOG(FATAL) << "Error calling func on object file"; |
+ |
+ // Give the name of the resultant nexe back to the content handler |
+ // so it may be launched. |
+ result = MojoWriteMessage(handle, |
+ nexe_file_name.value().c_str(), |
+ nexe_file_name.value().size(), |
+ nullptr, |
+ 0, |
+ MOJO_WRITE_MESSAGE_FLAG_NONE); |
+ |
+ if (result != MOJO_RESULT_OK) |
+ LOG(FATAL) << "Could not write message to content handler: " << result; |
+} |
+ |
+const struct nacl_irt_private_pnacl_translator_link |
+ nacl_irt_private_pnacl_translator_link = { |
+ serve_link_request |
+}; |