Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: chrome/nacl/nacl_helper_linux.cc

Issue 6995121: New NaCl zygote implementation 2 (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: NaCl helper for Chrome Linux zygote Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 // A mini-zygote specifically for Native Client.
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <unistd.h>
13
14 #include <vector>
15
16 #include "base/at_exit.h"
17 #include "base/command_line.h"
18 #include "base/eintr_wrapper.h"
19 #include "base/mac/scoped_nsautorelease_pool.h"
20 #include "chrome/nacl/nacl_helper_linux.h"
21 #include "content/common/main_function_params.h"
22 #include "content/common/unix_domain_socket_posix.h"
23 #include "webkit/glue/webkit_glue.h"
24
25 #ifdef NDEBUG
agl 2011/06/10 17:28:03 Deserves a comment about why it's not using the us
agl 2011/06/10 17:28:03 Optional nit: Chrome code typically uses #if defi
Brad Chen 2011/06/14 00:16:01 Done.
26 #define DCHECK(condition) {}
27 #else
28 #define DCHECK(condition) \
29 { if (!condition) fprintf(stderr, "Check failed: %s.\n", #condition); }
30 #endif
31
32 #ifndef NDEBUG
agl 2011/06/10 17:28:03 optional nit: #if !defined(NDEBUG)
Brad Chen 2011/06/14 00:16:01 Done.
33 // These two symbols from webkit_glue are being referenced someplace
34 // in debug builds but apparently are not being called. I do not know
35 // from where they are referenced, but as long as it's not in release
36 // builds it's okay.
37 namespace webkit_glue {
38
39 bool FindProxyForUrl(const GURL& url, std::string* proxy_list) {
40 fprintf(stderr, "Who is calling FindProxyForUrl?");
41 _exit(-1);
42 return false;
43 }
44
45 std::string GetProductVersion() {
46 static std::string version("What is the product version?");
47 fprintf(stderr, "Who is calling GetProductVersion?");
48 _exit(-1);
49 return version;
agl 2011/06/10 17:28:03 Seems superfluous to return anything but an empty
Brad Chen 2011/06/14 00:16:01 Done.
50 }
51
52 } // namespace webkit_glue
53 #endif // NDEBUG
54
55 const bool g_suid_sandbox_active;
agl 2011/06/10 17:28:03 should either be extern, static or needs a comment
Brad Chen 2011/06/14 00:16:01 Done.
56 extern int NaClMain(const MainFunctionParams&);
57
58 // The child must mimic the behavior of zygote_main_linux.c on the child
59 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from
60 // if (!child) {
61 // Note this code doesn't attempt to support SELINUX or the SECCOMP sandbox.
agl 2011/06/10 17:28:03 nit: Either "Note: " or "Note that "
62 void BeLoader(const int zygote_fd, const std::vector<int> child_fds) {
agl 2011/06/10 17:28:03 static
agl 2011/06/10 17:28:03 I think you should consider renaming this function
Brad Chen 2011/06/14 00:16:01 Done.
Brad Chen 2011/06/14 00:16:01 Done.
63 // NaCl main expects the child_fd to be on kZygoteDescriptor
64 DCHECK(zygote_fd == kZygoteDescriptor);
65 close(zygote_fd);
66 DCHECK(kZygoteDescriptor == dup(child_fds[0]));
67
68 base::AtExitManager exit_manager;
69 base::mac::ScopedNSAutoreleasePool autorelease_pool;
70 SandboxInitWrapper sandbox_wrapper;
71 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
72 const MainFunctionParams mainparams(command_line,
73 sandbox_wrapper,
74 &autorelease_pool);
75
76 NaClMain(mainparams);
77 _exit(0);
78 }
79
80 // Some of this code was lifted from
81 // content/browser/zygote_main_linux.cc:ForkWithRealPid()
82 void HandleForkRequest(const int zygote_fd, const std::vector<int> child_fds) {
agl 2011/06/10 17:28:03 static
agl 2011/06/10 17:28:03 ampersand after "const std::vector<int>", I suspec
Brad Chen 2011/06/14 00:16:01 Done.
83 pid_t childpid = fork();
84 if (childpid < 0) {
85 perror("fork");
86 fprintf(stderr, "*** HandleForkRequest failed\n");
87 // fall through to parent case below
88 } else if (childpid == 0) { // In the child process.
89 if (g_suid_sandbox_active) {
90 char buffer[1];
91 // Wait until the parent process has discovered our PID. We
92 // should not fork any child processes (which the seccomp
93 // sandbox does) until then, because that can interfere with the
94 // parent's discovery of our PID.
95 int nread = HANDLE_EINTR(read(child_fds[2], buffer, 1));
96 if (nread != 1 || buffer[0] != 'x') {
97 perror("read");
98 fprintf(stderr, "Failed to synch with zygote (%d:%d)\n",
99 nread, buffer[0]);
100 } else {
101 fprintf(stderr, "NaCl loader is synchronised with Chrome zygote");
102 }
103 }
104 BeLoader(zygote_fd, child_fds);
105 // NOTREACHED
106 return;
107 }
108 // I am the parent.
109 // First, close the dummy_fd so the sandbox won't find me when
110 // looking for the child's pid in /proc. Also close other fds.
111 for (size_t i = 0; i < child_fds.size(); i++) close(child_fds[i]);
agl 2011/06/10 17:28:03 Body of loop on its own line.
Brad Chen 2011/06/14 00:16:01 Done.
112 // Now tell childpid to the Chrome zygote.
113 if (send(zygote_fd, &childpid, sizeof(childpid), MSG_EOR)
114 != sizeof(childpid)) {
115 fprintf(stderr, "*** send() to zygote failed");
116 }
117 }
118
119 int main(int argc, char *argv[]) {
120 pid_t mypid = getpid();
121 CommandLine::Init(argc, argv);
122
123 g_suid_sandbox_active = (NULL != getenv("SBX_D"));
124
125 if (send(kZygoteDescriptor, kNaClHelperMagic,
126 sizeof(kNaClHelperMagic), MSG_EOR) != 4) {
127 perror("send");
128 }
129
130 while (1) {
agl 2011/06/10 17:28:03 nit: while (true) is more typical of Chrome code.
Brad Chen 2011/06/14 00:16:01 Done.
131 int badpid = -1;
132 std::vector<int> fds;
133 static const unsigned kMaxMessageLength = 2048;
134 char buf[kMaxMessageLength];
135 const ssize_t msglen = (UnixDomainSocket::RecvMsg(kZygoteDescriptor,
agl 2011/06/10 17:28:03 parentheses around RHS are superfluous.
Brad Chen 2011/06/14 00:16:01 Done.
136 &buf, sizeof(buf), &fds));
137 if (0 == msglen || (-1 == msglen && ECONNRESET == errno)) {
138 // EOF from zygote. Rest in peace...
139 _exit(0);
140 return 0;
141 }
142 if (msglen == -1) {
143 fprintf(stderr, "NaCl helper: error reading message from zygote\n");
144 continue;
145 }
146 // fork request
147 if (0 == strcmp(buf, kNaClForkRequest)) {
agl 2011/06/10 17:28:03 if (msglen == sizeof(kNaClForkRequest) - 1 && memc
Brad Chen 2011/06/14 00:16:01 Done.
148 if (1 == fds.size() || 3 == fds.size()) {
149 HandleForkRequest(kZygoteDescriptor, fds);
150 // child does not return
151 } else {
152 fprintf(stderr, "NaCl helper expected 1 or 3 fds, got %d\n",
153 int(fds.size()));
154 send(kZygoteDescriptor, &badpid, sizeof(badpid), MSG_EOR);
155 }
156 } else {
157 fprintf(stderr, "NaCl helper unrecognized request: %s\n", buf);
158 send(kZygoteDescriptor, &badpid, sizeof(badpid), MSG_EOR);
159 }
160 }
161 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698