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 class PexeLinkerImpl : public mojo::nacl::PexeLinker { | |
19 public: | |
20 PexeLinkerImpl(mojo::ScopedMessagePipeHandle handle, | |
21 int (*func)(int nexe_fd, const int* obj_file_fds, | |
Mark Seaborn
2015/10/27 17:30:20
It might be worth typedef'ing this in this file to
Sean Klein
2015/10/28 17:02:41
Done.
| |
22 int obj_file_fd_count)) | |
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_fds[] = { open(object_file_name.get().c_str(), O_RDONLY) }; | |
Mark Seaborn
2015/10/27 17:30:20
Can you use "int obj_file_fds = open(...)" here to
Sean Klein
2015/10/28 17:02:41
Done.
| |
40 if (unlink(object_file_name.get().c_str())) | |
41 LOG(FATAL) << "Could not unlink temporary object file"; | |
42 if (obj_file_fds[0] < 0) | |
43 LOG(FATAL) << "Could not open object file"; | |
44 | |
45 if (func_(nexe_file_fd, obj_file_fds, 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 int (*func_)(int nexe_fd, const int* obj_file_fds, int obj_file_fd_count); | |
53 mojo::StrongBinding<mojo::nacl::PexeLinker> strong_binding_; | |
54 }; | |
55 | |
56 void ServeLinkRequest(int (*func)(int nexe_fd, | |
57 const int* obj_file_fds, | |
58 int obj_file_fd_count)) { | |
59 // Acquire the handle -- this is our mechanism to contact the | |
60 // content handler which called us. | |
61 MojoHandle handle; | |
62 nacl::MojoGetInitialHandle(&handle); | |
63 | |
64 // Convert the MojoHandle into a ScopedMessagePipeHandle, and use that to | |
65 // implement the PexeLinker interface. | |
66 PexeLinkerImpl impl( | |
67 mojo::ScopedMessagePipeHandle(mojo::MessagePipeHandle(handle)).Pass(), | |
68 func); | |
69 mojo::RunLoop::current()->RunUntilIdle(); | |
70 } | |
71 | |
72 } // namespace anonymous | |
73 | |
74 namespace nacl { | |
75 | |
76 const struct nacl_irt_private_pnacl_translator_link | |
77 nacl_irt_private_pnacl_translator_link = { | |
78 ServeLinkRequest | |
79 }; | |
80 | |
81 } // namespace nacl | |
OLD | NEW |