OLD | NEW |
| (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 #ifndef CONTENT_BROWSER_ZYGOTE_HOST_LINUX_H_ | |
6 #define CONTENT_BROWSER_ZYGOTE_HOST_LINUX_H_ | |
7 #pragma once | |
8 | |
9 #include <unistd.h> | |
10 | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/global_descriptors_posix.h" | |
15 #include "base/process.h" | |
16 #include "base/process_util.h" | |
17 #include "base/synchronization/lock.h" | |
18 #include "content/common/content_export.h" | |
19 | |
20 template<typename Type> | |
21 struct DefaultSingletonTraits; | |
22 | |
23 static const char kZygoteMagic[] = "ZYGOTE_OK"; | |
24 | |
25 // http://code.google.com/p/chromium/wiki/LinuxZygote | |
26 | |
27 // The zygote host is the interface, in the browser process, to the zygote | |
28 // process. | |
29 class CONTENT_EXPORT ZygoteHost { | |
30 public: | |
31 // Returns the singleton instance. | |
32 static ZygoteHost* GetInstance(); | |
33 | |
34 void Init(const std::string& sandbox_cmd); | |
35 | |
36 // Tries to start a process of type indicated by process_type. | |
37 // Returns its pid on success, otherwise | |
38 // base::kNullProcessHandle; | |
39 pid_t ForkRequest(const std::vector<std::string>& command_line, | |
40 const base::GlobalDescriptors::Mapping& mapping, | |
41 const std::string& process_type); | |
42 void EnsureProcessTerminated(pid_t process); | |
43 | |
44 // Get the termination status (and, optionally, the exit code) of | |
45 // the process. |exit_code| is set to the exit code of the child | |
46 // process. (|exit_code| may be NULL.) | |
47 base::TerminationStatus GetTerminationStatus(base::ProcessHandle handle, | |
48 int* exit_code); | |
49 | |
50 // These are the command codes used on the wire between the browser and the | |
51 // zygote. | |
52 enum { | |
53 kCmdFork = 0, // Fork off a new renderer. | |
54 kCmdReap = 1, // Reap a renderer child. | |
55 kCmdGetTerminationStatus = 2, // Check what happend to a child process. | |
56 kCmdGetSandboxStatus = 3, // Read a bitmask of kSandbox* | |
57 }; | |
58 | |
59 // These form a bitmask which describes the conditions of the sandbox that | |
60 // the zygote finds itself in. | |
61 enum { | |
62 kSandboxSUID = 1 << 0, // SUID sandbox active | |
63 kSandboxPIDNS = 1 << 1, // SUID sandbox is using the PID namespace | |
64 kSandboxNetNS = 1 << 2, // SUID sandbox is using the network namespace | |
65 kSandboxSeccomp = 1 << 3, // seccomp sandbox active. | |
66 }; | |
67 | |
68 pid_t pid() const { return pid_; } | |
69 | |
70 // Returns an int which is a bitmask of kSandbox* values. Only valid after | |
71 // the first render has been forked. | |
72 int sandbox_status() const { | |
73 if (have_read_sandbox_status_word_) | |
74 return sandbox_status_; | |
75 return 0; | |
76 } | |
77 | |
78 // Adjust the OOM score of the given renderer's PID. The allowed | |
79 // range for the score is [0, 1000], where higher values are more | |
80 // likely to be killed by the OOM killer. | |
81 void AdjustRendererOOMScore(base::ProcessHandle process_handle, int score); | |
82 | |
83 private: | |
84 friend struct DefaultSingletonTraits<ZygoteHost>; | |
85 ZygoteHost(); | |
86 ~ZygoteHost(); | |
87 | |
88 ssize_t ReadReply(void* buf, size_t buflen); | |
89 | |
90 int control_fd_; // the socket to the zygote | |
91 // A lock protecting all communication with the zygote. This lock must be | |
92 // acquired before sending a command and released after the result has been | |
93 // received. | |
94 base::Lock control_lock_; | |
95 pid_t pid_; | |
96 bool init_; | |
97 bool using_suid_sandbox_; | |
98 std::string sandbox_binary_; | |
99 bool have_read_sandbox_status_word_; | |
100 int sandbox_status_; | |
101 }; | |
102 | |
103 #endif // CONTENT_BROWSER_ZYGOTE_HOST_LINUX_H_ | |
OLD | NEW |