1 // Copyright 2016 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 BASE_MAC_MACH_PORT_BROKER_H_
6 #define BASE_MAC_MACH_PORT_BROKER_H_
7 
8 #include <mach/mach.h>
9 
10 #include <map>
11 #include <memory>
12 #include <string>
13 
14 #include "base/base_export.h"
15 #include "base/mac/dispatch_source_mach.h"
16 #include "base/mac/scoped_mach_port.h"
17 #include "base/macros.h"
18 #include "base/process/port_provider_mac.h"
19 #include "base/process/process_handle.h"
20 #include "base/synchronization/lock.h"
21 
22 namespace base {
23 
24 // On OS X, the task port of a process is required to collect metrics about the
25 // process, and to insert Mach ports into the process. Running |task_for_pid()|
26 // is only allowed for privileged code. However, a process has port rights to
27 // all its subprocesses, so let the child processes send their Mach port to the
28 // parent over IPC.
29 //
30 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that
31 // the regular IPC system uses. Hence, the child processes opens a Mach
32 // connection shortly after launching and ipc their mach data to the parent
33 // process. A single |MachPortBroker| with a given name is expected to exist in
34 // the parent process.
35 //
36 // Since this data arrives over a separate channel, it is not available
37 // immediately after a child process has been started.
38 class BASE_EXPORT MachPortBroker : public base::PortProvider {
39  public:
40   // For use in child processes. This will send the task port of the current
41   // process over Mach IPC to the port registered by name (via this class) in
42   // the parent process. Returns true if the message was sent successfully
43   // and false if otherwise.
44   static bool ChildSendTaskPortToParent(const std::string& name);
45 
46   // Returns the Mach port name to use when sending or receiving messages.
47   // Does the Right Thing in the browser and in child processes.
48   static std::string GetMachPortName(const std::string& name, bool is_child);
49 
50   MachPortBroker(const std::string& name);
51   ~MachPortBroker() override;
52 
53   // Performs any initialization work.
54   bool Init();
55 
56   // Adds a placeholder to the map for the given pid with MACH_PORT_NULL.
57   // Callers are expected to later update the port with FinalizePid(). Callers
58   // MUST acquire the lock given by GetLock() before calling this method (and
59   // release the lock afterwards).
60   void AddPlaceholderForPid(base::ProcessHandle pid);
61 
62   // Removes |pid| from the task port map. Callers MUST acquire the lock given
63   // by GetLock() before calling this method (and release the lock afterwards).
64   void InvalidatePid(base::ProcessHandle pid);
65 
66   // The lock that protects this MachPortBroker object. Callers MUST acquire
67   // and release this lock around calls to AddPlaceholderForPid(),
68   // InvalidatePid(), and FinalizePid();
GetLock()69   base::Lock& GetLock() { return lock_; }
70 
71   // Implement |base::PortProvider|.
72   mach_port_t TaskForPid(base::ProcessHandle process) const override;
73 
74  private:
75   friend class MachPortBrokerTest;
76 
77   // Message handler that is invoked on |dispatch_source_| when an
78   // incoming message needs to be received.
79   void HandleRequest();
80 
81   // Updates the mapping for |pid| to include the given |mach_info|.  Does
82   // nothing if PlaceholderForPid() has not already been called for the given
83   // |pid|. Callers MUST acquire the lock given by GetLock() before calling
84   // this method (and release the lock afterwards).
85   void FinalizePid(base::ProcessHandle pid, mach_port_t task_port);
86 
87   // Name used to identify a particular port broker.
88   const std::string name_;
89 
90   // The Mach port on which the server listens.
91   base::mac::ScopedMachReceiveRight server_port_;
92 
93   // The dispatch source and queue on which Mach messages will be received.
94   std::unique_ptr<base::DispatchSourceMach> dispatch_source_;
95 
96   // Stores mach info for every process in the broker.
97   typedef std::map<base::ProcessHandle, mach_port_t> MachMap;
98   MachMap mach_map_;
99 
100   // Mutex that guards |mach_map_|.
101   mutable base::Lock lock_;
102 
103   DISALLOW_COPY_AND_ASSIGN(MachPortBroker);
104 };
105 
106 }  // namespace base
107 
108 #endif  // BASE_MAC_MACH_PORT_BROKER_H_
109