xref: /aosp_15_r20/external/cronet/components/nacl/zygote/nacl_fork_delegate_linux.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 COMPONENTS_NACL_ZYGOTE_NACL_FORK_DELEGATE_LINUX_H_
6 #define COMPONENTS_NACL_ZYGOTE_NACL_FORK_DELEGATE_LINUX_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "content/public/common/zygote/zygote_fork_delegate_linux.h"
15 
16 namespace base {
17 struct LaunchOptions;
18 }
19 
20 namespace nacl {
21 
22 // Appends any ZygoteForkDelegate instances needed by NaCl to |*delegates|.
23 void AddNaClZygoteForkDelegates(
24     std::vector<std::unique_ptr<content::ZygoteForkDelegate>>* delegates);
25 
26 // The NaClForkDelegate is created during Chrome linux zygote initialization,
27 // and provides "fork()" functionality with NaCl specific process
28 // characteristics (specifically address space layout) as an alternative to
29 // forking the zygote. A new delegate is passed in as an argument to
30 // ZygoteMain().
31 class NaClForkDelegate : public content::ZygoteForkDelegate {
32  public:
33   NaClForkDelegate();
34   NaClForkDelegate(const NaClForkDelegate&) = delete;
35   NaClForkDelegate& operator=(const NaClForkDelegate&) = delete;
36   ~NaClForkDelegate() override;
37 
38   void Init(int sandboxdesc, bool enable_layer1_sandbox) override;
39   void InitialUMA(std::string* uma_name,
40                   int* uma_sample,
41                   int* uma_boundary_value) override;
42   bool CanHelp(const std::string& process_type,
43                std::string* uma_name,
44                int* uma_sample,
45                int* uma_boundary_value) override;
46   pid_t Fork(const std::string& process_type,
47              const std::vector<std::string>& args,
48              const std::vector<int>& fds,
49              const std::string& channel_id) override;
50   bool GetTerminationStatus(pid_t pid,
51                             bool known_dead,
52                             base::TerminationStatus* status,
53                             int* exit_code) override;
54 
55  private:
56   static void AddPassthroughEnvToOptions(base::LaunchOptions* options);
57 
58   // These values are reported via UMA and hence they become permanent
59   // constants.  Old values cannot be reused, only new ones added.
60   enum NaClHelperStatus {
61     kNaClHelperUnused = 0,
62     kNaClHelperMissing = 1,
63     kNaClHelperBootstrapMissing = 2,
64     // kNaClHelperValgrind = 3,  // Running in valgrind no longer supported.
65     kNaClHelperLaunchFailed = 4,
66     kNaClHelperAckFailed = 5,
67     kNaClHelperSuccess = 6,
68     kNaClHelperStatusBoundary  // Must be one greater than highest value used.
69   };
70 
71   NaClHelperStatus status_;
72   int fd_;
73 
74   FRIEND_TEST_ALL_PREFIXES(NaClForkDelegateLinuxTest, EnvPassthrough);
75 };
76 
77 }  // namespace nacl
78 
79 #endif  // COMPONENTS_NACL_ZYGOTE_NACL_FORK_DELEGATE_LINUX_H_
80