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 |
| 7 #include "base/files/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "mojo/nacl/nonsfi/irt_mojo_nonsfi.h" |
| 10 #include "mojo/public/cpp/bindings/string.h" |
| 11 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 12 #include "mojo/public/cpp/utility/run_loop.h" |
| 13 #include "native_client/src/untrusted/irt/irt_dev.h" |
| 14 #include "services/nacl/pnacl_link.mojom.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 typedef int (*LinkerCallback)(int, const int*, int); |
| 19 |
| 20 class PexeLinkerImpl : public mojo::nacl::PexeLinker { |
| 21 public: |
| 22 PexeLinkerImpl(mojo::ScopedMessagePipeHandle handle, LinkerCallback func) |
| 23 : func_(func), strong_binding_(this, handle.Pass()) {} |
| 24 void PexeLink(const mojo::String& object_file_name, |
| 25 const mojo::Callback<void(mojo::String)>& callback) |
| 26 override { |
| 27 // Create a temporary .nexe file which will be the result of calling our |
| 28 // linker. |
| 29 base::FilePath nexe_file_name; |
| 30 if (!CreateTemporaryFile(&nexe_file_name)) |
| 31 LOG(FATAL) << "Could not create temporary nexe file"; |
| 32 int nexe_file_fd = open(nexe_file_name.value().c_str(), O_RDWR); |
| 33 if (nexe_file_fd < 0) |
| 34 LOG(FATAL) << "Could not create temp file for linked nexe"; |
| 35 |
| 36 // Open our temporary object file. Additionally, unlink it, since it is a |
| 37 // temporary file that is no longer needed after it is opened. |
| 38 size_t obj_file_fd_count = 1; |
| 39 int obj_file_fd = open(object_file_name.get().c_str(), O_RDONLY); |
| 40 if (unlink(object_file_name.get().c_str())) |
| 41 LOG(FATAL) << "Could not unlink temporary object file"; |
| 42 if (obj_file_fd < 0) |
| 43 LOG(FATAL) << "Could not open object file"; |
| 44 |
| 45 if (func_(nexe_file_fd, &obj_file_fd, obj_file_fd_count)) |
| 46 LOG(FATAL) << "Error calling func on object file"; |
| 47 |
| 48 // Return the name of the nexe file. |
| 49 callback.Run(mojo::String(nexe_file_name.value())); |
| 50 } |
| 51 private: |
| 52 LinkerCallback func_; |
| 53 mojo::StrongBinding<mojo::nacl::PexeLinker> strong_binding_; |
| 54 }; |
| 55 |
| 56 void ServeLinkRequest(LinkerCallback func) { |
| 57 // Acquire the handle -- this is our mechanism to contact the |
| 58 // content handler which called us. |
| 59 MojoHandle handle; |
| 60 nacl::MojoGetInitialHandle(&handle); |
| 61 |
| 62 // Convert the MojoHandle into a ScopedMessagePipeHandle, and use that to |
| 63 // implement the PexeLinker interface. |
| 64 PexeLinkerImpl impl( |
| 65 mojo::ScopedMessagePipeHandle(mojo::MessagePipeHandle(handle)).Pass(), |
| 66 func); |
| 67 mojo::RunLoop::current()->RunUntilIdle(); |
| 68 } |
| 69 |
| 70 } // namespace anonymous |
| 71 |
| 72 namespace nacl { |
| 73 |
| 74 const struct nacl_irt_private_pnacl_translator_link |
| 75 nacl_irt_private_pnacl_translator_link = { |
| 76 ServeLinkRequest |
| 77 }; |
| 78 |
| 79 } // namespace nacl |
OLD | NEW |