OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // A mini-zygote specifically for Native Client. | 5 // A mini-zygote specifically for Native Client. |
6 | 6 |
7 #include "components/nacl/common/nacl_helper_linux.h" | 7 #include "components/nacl/common/nacl_helper_linux.h" |
8 | 8 |
9 #include <errno.h> | 9 #include <errno.h> |
10 #include <fcntl.h> | 10 #include <fcntl.h> |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // Start the NaCl loader in a child created by the NaCl loader Zygote. | 70 // Start the NaCl loader in a child created by the NaCl loader Zygote. |
71 void ChildNaClLoaderInit(const std::vector<int>& child_fds, | 71 void ChildNaClLoaderInit(const std::vector<int>& child_fds, |
72 const NaClLoaderSystemInfo& system_info) { | 72 const NaClLoaderSystemInfo& system_info) { |
73 bool validack = false; | 73 bool validack = false; |
74 const size_t kMaxReadSize = 1024; | 74 const size_t kMaxReadSize = 1024; |
75 char buffer[kMaxReadSize]; | 75 char buffer[kMaxReadSize]; |
76 // Wait until the parent process has discovered our PID. We | 76 // Wait until the parent process has discovered our PID. We |
77 // should not fork any child processes (which the seccomp | 77 // should not fork any child processes (which the seccomp |
78 // sandbox does) until then, because that can interfere with the | 78 // sandbox does) until then, because that can interfere with the |
79 // parent's discovery of our PID. | 79 // parent's discovery of our PID. |
80 const int nread = HANDLE_EINTR(read(child_fds[kNaClParentFDIndex], buffer, | 80 const int nread = HANDLE_EINTR( |
81 kMaxReadSize)); | 81 read(child_fds[kNaClParentFDIndex(child_fds.size())], buffer, |
| 82 kMaxReadSize)); |
82 const std::string switch_prefix = std::string("--") + | 83 const std::string switch_prefix = std::string("--") + |
83 switches::kProcessChannelID + std::string("="); | 84 switches::kProcessChannelID + std::string("="); |
84 const size_t len = switch_prefix.length(); | 85 const size_t len = switch_prefix.length(); |
85 | 86 |
86 if (nread < 0) { | 87 if (nread < 0) { |
87 perror("read"); | 88 perror("read"); |
88 LOG(ERROR) << "read returned " << nread; | 89 LOG(ERROR) << "read returned " << nread; |
89 } else if (nread > static_cast<int>(len)) { | 90 } else if (nread > static_cast<int>(len)) { |
90 if (switch_prefix.compare(0, len, buffer, 0, len) == 0) { | 91 if (switch_prefix.compare(0, len, buffer, 0, len) == 0) { |
91 VLOG(1) << "NaCl loader is synchronised with Chrome zygote"; | 92 VLOG(1) << "NaCl loader is synchronised with Chrome zygote"; |
92 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 93 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
93 switches::kProcessChannelID, | 94 switches::kProcessChannelID, |
94 std::string(&buffer[len], nread - len)); | 95 std::string(&buffer[len], nread - len)); |
95 validack = true; | 96 validack = true; |
96 } | 97 } |
97 } | 98 } |
98 if (HANDLE_EINTR(close(child_fds[kNaClDummyFDIndex])) != 0) | 99 if (HANDLE_EINTR(close(child_fds[kNaClDummyFDIndex(child_fds.size())])) != 0) |
99 LOG(ERROR) << "close(child_fds[kNaClDummyFDIndex]) failed"; | 100 LOG(ERROR) << "close(child_fds[kNaClDummyFDIndex]) failed"; |
100 if (HANDLE_EINTR(close(child_fds[kNaClParentFDIndex])) != 0) | 101 if (HANDLE_EINTR(close(child_fds[kNaClParentFDIndex(child_fds.size())])) != 0) |
101 LOG(ERROR) << "close(child_fds[kNaClParentFDIndex]) failed"; | 102 LOG(ERROR) << "close(child_fds[kNaClParentFDIndex]) failed"; |
102 if (validack) { | 103 if (validack) { |
103 BecomeNaClLoader(child_fds, system_info); | 104 BecomeNaClLoader(child_fds, system_info); |
104 } else { | 105 } else { |
105 LOG(ERROR) << "Failed to synch with zygote"; | 106 LOG(ERROR) << "Failed to synch with zygote"; |
106 } | 107 } |
107 _exit(1); | 108 _exit(1); |
108 } | 109 } |
109 | 110 |
110 // Handle a fork request from the Zygote. | 111 // Handle a fork request from the Zygote. |
111 // Some of this code was lifted from | 112 // Some of this code was lifted from |
112 // content/browser/zygote_main_linux.cc:ForkWithRealPid() | 113 // content/browser/zygote_main_linux.cc:ForkWithRealPid() |
113 bool HandleForkRequest(const std::vector<int>& child_fds, | 114 bool HandleForkRequest(const std::vector<int>& child_fds, |
114 const NaClLoaderSystemInfo& system_info, | 115 const NaClLoaderSystemInfo& system_info, |
115 Pickle* output_pickle) { | 116 Pickle* output_pickle) { |
116 if (kNaClParentFDIndex + 1 != child_fds.size()) { | 117 if (kNaClParentFDIndex(child_fds.size()) + 1 != child_fds.size()) { |
117 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " | 118 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " |
118 << child_fds.size(); | 119 << child_fds.size(); |
119 return false; | 120 return false; |
120 } | 121 } |
121 | 122 |
122 VLOG(1) << "nacl_helper: forking"; | 123 VLOG(1) << "nacl_helper: forking"; |
123 pid_t child_pid = fork(); | 124 pid_t child_pid = fork(); |
124 if (child_pid < 0) { | 125 if (child_pid < 0) { |
125 PLOG(ERROR) << "*** fork() failed."; | 126 PLOG(ERROR) << "*** fork() failed."; |
126 } | 127 } |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 // Now handle requests from the Zygote. | 386 // Now handle requests from the Zygote. |
386 while (true) { | 387 while (true) { |
387 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, | 388 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, |
388 system_info); | 389 system_info); |
389 // Do not turn this into a CHECK() without thinking about robustness | 390 // Do not turn this into a CHECK() without thinking about robustness |
390 // against malicious IPC requests. | 391 // against malicious IPC requests. |
391 DCHECK(request_handled); | 392 DCHECK(request_handled); |
392 } | 393 } |
393 NOTREACHED(); | 394 NOTREACHED(); |
394 } | 395 } |
OLD | NEW |