Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/service_process_util_posix.h" | 5 #include "chrome/common/service_process_util_posix.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/posix/eintr_wrapper.h" | 10 #include "base/posix/eintr_wrapper.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 // not, but we don't ever expect it to be called. | 70 // not, but we don't ever expect it to be called. |
| 71 static void SigTermHandler(int sig, siginfo_t* info, void* uap) { | 71 static void SigTermHandler(int sig, siginfo_t* info, void* uap) { |
| 72 // TODO(dmaclach): add security here to make sure that we are being shut | 72 // TODO(dmaclach): add security here to make sure that we are being shut |
| 73 // down by an appropriate process. | 73 // down by an appropriate process. |
| 74 int message = ServiceProcessTerminateMonitor::kTerminateMessage; | 74 int message = ServiceProcessTerminateMonitor::kTerminateMessage; |
| 75 if (write(g_signal_socket, &message, sizeof(message)) < 0) { | 75 if (write(g_signal_socket, &message, sizeof(message)) < 0) { |
| 76 DPLOG(ERROR) << "write"; | 76 DPLOG(ERROR) << "write"; |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 ServiceProcessState::StateData::StateData() : set_action_(false) { | 80 ServiceProcessState::StateData::StateData() : set_action(false) { |
| 81 memset(sockets_, -1, sizeof(sockets_)); | 81 memset(sockets, -1, sizeof(sockets)); |
| 82 memset(&old_action_, 0, sizeof(old_action_)); | 82 memset(&old_action, 0, sizeof(old_action)); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void ServiceProcessState::StateData::SignalReady(base::WaitableEvent* signal, | 85 void ServiceProcessState::StateData::SignalReady(base::WaitableEvent* signal, |
| 86 bool* success) { | 86 bool* success) { |
| 87 DCHECK_EQ(g_signal_socket, -1); | 87 DCHECK_EQ(g_signal_socket, -1); |
| 88 DCHECK(!signal->IsSignaled()); | 88 DCHECK(!signal->IsSignaled()); |
| 89 *success = base::MessageLoopForIO::current()->WatchFileDescriptor( | 89 *success = base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 90 sockets_[0], | 90 sockets[0], true, base::MessageLoopForIO::WATCH_READ, &watcher, |
|
James Hawkins
2015/03/02 15:36:28
Please don't change the style of the original file
qi1988.yang
2015/03/03 05:59:20
Done.
| |
| 91 true, | 91 terminate_monitor.get()); |
| 92 base::MessageLoopForIO::WATCH_READ, | |
| 93 &watcher_, | |
| 94 terminate_monitor_.get()); | |
| 95 if (!*success) { | 92 if (!*success) { |
| 96 DLOG(ERROR) << "WatchFileDescriptor"; | 93 DLOG(ERROR) << "WatchFileDescriptor"; |
| 97 signal->Signal(); | 94 signal->Signal(); |
| 98 return; | 95 return; |
| 99 } | 96 } |
| 100 g_signal_socket = sockets_[1]; | 97 g_signal_socket = sockets[1]; |
| 101 | 98 |
| 102 // Set up signal handler for SIGTERM. | 99 // Set up signal handler for SIGTERM. |
| 103 struct sigaction action; | 100 struct sigaction action; |
| 104 memset(&action, 0, sizeof(action)); | 101 memset(&action, 0, sizeof(action)); |
| 105 action.sa_sigaction = SigTermHandler; | 102 action.sa_sigaction = SigTermHandler; |
| 106 sigemptyset(&action.sa_mask); | 103 sigemptyset(&action.sa_mask); |
| 107 action.sa_flags = SA_SIGINFO; | 104 action.sa_flags = SA_SIGINFO; |
| 108 *success = sigaction(SIGTERM, &action, &old_action_) == 0; | 105 *success = sigaction(SIGTERM, &action, &old_action) == 0; |
| 109 if (!*success) { | 106 if (!*success) { |
| 110 DPLOG(ERROR) << "sigaction"; | 107 DPLOG(ERROR) << "sigaction"; |
| 111 signal->Signal(); | 108 signal->Signal(); |
| 112 return; | 109 return; |
| 113 } | 110 } |
| 114 | 111 |
| 115 // If the old_action is not default, somebody else has installed a | 112 // If the old_action is not default, somebody else has installed a |
| 116 // a competing handler. Our handler is going to override it so it | 113 // a competing handler. Our handler is going to override it so it |
| 117 // won't be called. If this occurs it needs to be fixed. | 114 // won't be called. If this occurs it needs to be fixed. |
| 118 DCHECK_EQ(old_action_.sa_handler, SIG_DFL); | 115 DCHECK_EQ(old_action.sa_handler, SIG_DFL); |
| 119 set_action_ = true; | 116 set_action = true; |
| 120 | 117 |
| 121 #if defined(OS_MACOSX) | 118 #if defined(OS_MACOSX) |
| 122 *success = WatchExecutable(); | 119 *success = WatchExecutable(); |
| 123 if (!*success) { | 120 if (!*success) { |
| 124 DLOG(ERROR) << "WatchExecutable"; | 121 DLOG(ERROR) << "WatchExecutable"; |
| 125 signal->Signal(); | 122 signal->Signal(); |
| 126 return; | 123 return; |
| 127 } | 124 } |
| 128 #elif defined(OS_POSIX) | 125 #elif defined(OS_POSIX) |
| 129 initializing_lock_.reset(); | 126 initializing_lock.reset(); |
| 130 #endif // OS_POSIX | 127 #endif // OS_POSIX |
| 131 signal->Signal(); | 128 signal->Signal(); |
| 132 } | 129 } |
| 133 | 130 |
| 134 ServiceProcessState::StateData::~StateData() { | 131 ServiceProcessState::StateData::~StateData() { |
| 135 if (sockets_[0] != -1) { | 132 if (sockets[0] != -1) { |
| 136 if (IGNORE_EINTR(close(sockets_[0]))) { | 133 if (IGNORE_EINTR(close(sockets[0]))) { |
| 137 DPLOG(ERROR) << "close"; | 134 DPLOG(ERROR) << "close"; |
| 138 } | 135 } |
| 139 } | 136 } |
| 140 if (sockets_[1] != -1) { | 137 if (sockets[1] != -1) { |
| 141 if (IGNORE_EINTR(close(sockets_[1]))) { | 138 if (IGNORE_EINTR(close(sockets[1]))) { |
| 142 DPLOG(ERROR) << "close"; | 139 DPLOG(ERROR) << "close"; |
| 143 } | 140 } |
| 144 } | 141 } |
| 145 if (set_action_) { | 142 if (set_action) { |
| 146 if (sigaction(SIGTERM, &old_action_, NULL) < 0) { | 143 if (sigaction(SIGTERM, &old_action, NULL) < 0) { |
| 147 DPLOG(ERROR) << "sigaction"; | 144 DPLOG(ERROR) << "sigaction"; |
| 148 } | 145 } |
| 149 } | 146 } |
| 150 g_signal_socket = -1; | 147 g_signal_socket = -1; |
| 151 } | 148 } |
| 152 | 149 |
| 153 void ServiceProcessState::CreateState() { | 150 void ServiceProcessState::CreateState() { |
| 154 DCHECK(!state_); | 151 DCHECK(!state_); |
| 155 state_ = new StateData; | 152 state_ = new StateData; |
| 156 | 153 |
| 157 // Explicitly adding a reference here (and removing it in TearDownState) | 154 // Explicitly adding a reference here (and removing it in TearDownState) |
| 158 // because StateData is refcounted on Mac and Linux so that methods can | 155 // because StateData is refcounted on Mac and Linux so that methods can |
| 159 // be called on other threads. | 156 // be called on other threads. |
| 160 // It is not refcounted on Windows at this time. | 157 // It is not refcounted on Windows at this time. |
| 161 state_->AddRef(); | 158 state_->AddRef(); |
| 162 } | 159 } |
| 163 | 160 |
| 164 bool ServiceProcessState::SignalReady( | 161 bool ServiceProcessState::SignalReady( |
| 165 base::MessageLoopProxy* message_loop_proxy, | 162 base::MessageLoopProxy* message_loop_proxy, |
| 166 const base::Closure& terminate_task) { | 163 const base::Closure& terminate_task) { |
| 167 DCHECK(state_); | 164 DCHECK(state_); |
| 168 | 165 |
| 169 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 166 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 170 state_->running_lock_.reset(TakeServiceRunningLock(true)); | 167 state_->running_lock.reset(TakeServiceRunningLock(true)); |
| 171 if (state_->running_lock_.get() == NULL) { | 168 if (state_->running_lock.get() == NULL) { |
| 172 return false; | 169 return false; |
| 173 } | 170 } |
| 174 #endif | 171 #endif |
| 175 state_->terminate_monitor_.reset( | 172 state_->terminate_monitor.reset( |
| 176 new ServiceProcessTerminateMonitor(terminate_task)); | 173 new ServiceProcessTerminateMonitor(terminate_task)); |
| 177 if (pipe(state_->sockets_) < 0) { | 174 if (pipe(state_->sockets) < 0) { |
| 178 DPLOG(ERROR) << "pipe"; | 175 DPLOG(ERROR) << "pipe"; |
| 179 return false; | 176 return false; |
| 180 } | 177 } |
| 181 base::WaitableEvent signal_ready(true, false); | 178 base::WaitableEvent signal_ready(true, false); |
| 182 bool success = false; | 179 bool success = false; |
| 183 | 180 |
| 184 message_loop_proxy->PostTask(FROM_HERE, | 181 message_loop_proxy->PostTask(FROM_HERE, |
| 185 base::Bind(&ServiceProcessState::StateData::SignalReady, | 182 base::Bind(&ServiceProcessState::StateData::SignalReady, |
| 186 state_, | 183 state_, |
| 187 &signal_ready, | 184 &signal_ready, |
| 188 &success)); | 185 &success)); |
| 189 signal_ready.Wait(); | 186 signal_ready.Wait(); |
| 190 return success; | 187 return success; |
| 191 } | 188 } |
| 192 | 189 |
| 193 void ServiceProcessState::TearDownState() { | 190 void ServiceProcessState::TearDownState() { |
| 194 if (state_) { | 191 if (state_) { |
| 195 state_->Release(); | 192 state_->Release(); |
| 196 state_ = NULL; | 193 state_ = NULL; |
| 197 } | 194 } |
| 198 } | 195 } |
| OLD | NEW |