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 #include "sandbox/linux/syscall_broker/broker_process.h"
10 #include <sys/syscall.h>
11 #include <sys/types.h>
19 #include "base/callback.h"
20 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/posix/eintr_wrapper.h"
23 #include "base/process/process_metrics.h"
24 #include "build/build_config.h"
25 #include "sandbox/linux/syscall_broker/broker_channel.h"
26 #include "sandbox/linux/syscall_broker/broker_client.h"
27 #include "sandbox/linux/syscall_broker/broker_host.h"
31 namespace syscall_broker
{
33 BrokerProcess::BrokerProcess(
35 const std::vector
<syscall_broker::BrokerFilePermission
>& permissions
,
36 bool fast_check_in_client
,
37 bool quiet_failures_for_tests
)
38 : initialized_(false),
39 fast_check_in_client_(fast_check_in_client
),
40 quiet_failures_for_tests_(quiet_failures_for_tests
),
42 policy_(denied_errno
, permissions
) {
45 BrokerProcess::~BrokerProcess() {
47 if (broker_client_
.get()) {
48 // Closing the socket should be enough to notify the child to die,
49 // unless it has been duplicated.
52 PCHECK(0 == kill(broker_pid_
, SIGKILL
));
53 siginfo_t process_info
;
55 int ret
= HANDLE_EINTR(waitid(P_PID
, broker_pid_
, &process_info
, WEXITED
));
60 bool BrokerProcess::Init(
61 const base::Callback
<bool(void)>& broker_process_init_callback
) {
63 BrokerChannel::EndPoint ipc_reader
;
64 BrokerChannel::EndPoint ipc_writer
;
65 BrokerChannel::CreatePair(&ipc_reader
, &ipc_writer
);
67 #if !defined(THREAD_SANITIZER)
68 DCHECK_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
70 int child_pid
= fork();
71 if (child_pid
== -1) {
75 // We are the parent and we have just forked our broker process.
77 broker_pid_
= child_pid
;
78 broker_client_
.reset(new BrokerClient(policy_
, ipc_writer
.Pass(),
79 fast_check_in_client_
,
80 quiet_failures_for_tests_
));
84 // We are the broker process. Make sure to close the writer's end so that
85 // we get notified if the client disappears.
87 CHECK(broker_process_init_callback
.Run());
88 BrokerHost
broker_host(policy_
, ipc_reader
.Pass());
90 switch (broker_host
.HandleRequest()) {
91 case BrokerHost::RequestStatus::LOST_CLIENT
:
93 case BrokerHost::RequestStatus::SUCCESS
:
94 case BrokerHost::RequestStatus::FAILURE
:
104 void BrokerProcess::CloseChannel() {
105 broker_client_
.reset();
108 int BrokerProcess::Access(const char* pathname
, int mode
) const {
109 RAW_CHECK(initialized_
);
110 return broker_client_
->Access(pathname
, mode
);
113 int BrokerProcess::Open(const char* pathname
, int flags
) const {
114 RAW_CHECK(initialized_
);
115 return broker_client_
->Open(pathname
, flags
);
118 } // namespace syscall_broker
120 } // namespace sandbox.