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
3 // found in the LICENSE file.
5 #ifndef SANDBOX_WIN_SRC_BROKER_SERVICES_H_
6 #define SANDBOX_WIN_SRC_BROKER_SERVICES_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/win/scoped_handle.h"
14 #include "sandbox/win/src/crosscall_server.h"
15 #include "sandbox/win/src/job.h"
16 #include "sandbox/win/src/sandbox.h"
17 #include "sandbox/win/src/sharedmem_ipc_server.h"
18 #include "sandbox/win/src/win2k_threadpool.h"
19 #include "sandbox/win/src/win_utils.h"
32 // BrokerServicesBase ---------------------------------------------------------
33 // Broker implementation version 0
35 // This is an implementation of the interface BrokerServices and
36 // of the associated TargetProcess interface. In this implementation
37 // TargetProcess is a friend of BrokerServices where the later manages a
38 // collection of the former.
39 class BrokerServicesBase
: public BrokerServices
,
40 public SingletonBase
<BrokerServicesBase
> {
44 ~BrokerServicesBase();
46 // BrokerServices interface.
47 virtual ResultCode
Init() OVERRIDE
;
48 virtual TargetPolicy
* CreatePolicy() OVERRIDE
;
49 virtual ResultCode
SpawnTarget(const wchar_t* exe_path
,
50 const wchar_t* command_line
,
52 PROCESS_INFORMATION
* target
) OVERRIDE
;
53 virtual ResultCode
WaitForAllTargets() OVERRIDE
;
54 virtual ResultCode
AddTargetPeer(HANDLE peer_process
) OVERRIDE
;
55 virtual ResultCode
InstallAppContainer(const wchar_t* sid
,
56 const wchar_t* name
) OVERRIDE
;
57 virtual ResultCode
UninstallAppContainer(const wchar_t* sid
) OVERRIDE
;
59 // Checks if the supplied process ID matches one of the broker's active
62 // true if there is an active target process for this ID, otherwise false.
63 bool IsActiveTarget(DWORD process_id
);
66 // Releases the Job and notifies the associated Policy object to its
68 static void FreeResources(JobTracker
* tracker
);
70 // The routine that the worker thread executes. It is in charge of
71 // notifications and cleanup-related tasks.
72 static DWORD WINAPI
TargetEventsThread(PVOID param
);
74 // Removes a target peer from the process list if it expires.
75 static VOID CALLBACK
RemovePeer(PVOID parameter
, BOOLEAN timeout
);
77 // The completion port used by the job objects to communicate events to
81 // Handle to a manual-reset event that is signaled when the total target
82 // process count reaches zero.
85 // Handle to the worker thread that reacts to job notifications.
88 // Lock used to protect the list of targets from being modified by 2
89 // threads at the same time.
90 CRITICAL_SECTION lock_
;
92 // provides a pool of threads that are used to wait on the IPC calls.
93 ThreadProvider
* thread_pool_
;
95 // List of the trackers for closing and cleanup purposes.
96 typedef std::list
<JobTracker
*> JobTrackerList
;
97 JobTrackerList tracker_list_
;
99 // Maps peer process IDs to the saved handle and wait event.
100 // Prevents peer callbacks from accessing the broker after destruction.
101 typedef std::map
<DWORD
, PeerTracker
*> PeerTrackerMap
;
102 PeerTrackerMap peer_map_
;
104 // Provides a fast lookup to identify sandboxed processes that belong to a
105 // job. Consult |jobless_process_handles_| for handles of pocess without job.
106 std::set
<DWORD
> child_process_ids_
;
108 DISALLOW_COPY_AND_ASSIGN(BrokerServicesBase
);
111 } // namespace sandbox
114 #endif // SANDBOX_WIN_SRC_BROKER_SERVICES_H_