| 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 "remoting/host/elevated_controller_win.h" | 5 #include "remoting/host/elevated_controller_win.h" |
| 6 | 6 |
| 7 #include <sddl.h> | 7 #include <sddl.h> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 file_content.size()); | 221 file_content.size()); |
| 222 } | 222 } |
| 223 | 223 |
| 224 STDMETHODIMP ElevatedControllerWin::StartDaemon() { | 224 STDMETHODIMP ElevatedControllerWin::StartDaemon() { |
| 225 ScopedScHandle service; | 225 ScopedScHandle service; |
| 226 HRESULT hr = OpenService(&service); | 226 HRESULT hr = OpenService(&service); |
| 227 if (FAILED(hr)) { | 227 if (FAILED(hr)) { |
| 228 return hr; | 228 return hr; |
| 229 } | 229 } |
| 230 | 230 |
| 231 // Change the service start type to 'auto'. |
| 232 if (!::ChangeServiceConfigW(service, |
| 233 SERVICE_NO_CHANGE, |
| 234 SERVICE_AUTO_START, |
| 235 SERVICE_NO_CHANGE, |
| 236 NULL, |
| 237 NULL, |
| 238 NULL, |
| 239 NULL, |
| 240 NULL, |
| 241 NULL, |
| 242 NULL)) { |
| 243 DWORD error = GetLastError(); |
| 244 LOG_GETLASTERROR(ERROR) |
| 245 << "Failed to change the '" << kWindowsServiceName |
| 246 << "'service start type to 'auto'"; |
| 247 return HRESULT_FROM_WIN32(error); |
| 248 } |
| 249 |
| 250 // Start the service. |
| 231 if (!StartService(service, 0, NULL)) { | 251 if (!StartService(service, 0, NULL)) { |
| 232 DWORD error = GetLastError(); | 252 DWORD error = GetLastError(); |
| 233 if (error != ERROR_SERVICE_ALREADY_RUNNING) { | 253 if (error != ERROR_SERVICE_ALREADY_RUNNING) { |
| 234 LOG_GETLASTERROR(ERROR) | 254 LOG_GETLASTERROR(ERROR) |
| 235 << "Failed to start the '" << kWindowsServiceName << "'service"; | 255 << "Failed to start the '" << kWindowsServiceName << "'service"; |
| 236 | 256 |
| 237 return HRESULT_FROM_WIN32(error); | 257 return HRESULT_FROM_WIN32(error); |
| 238 } | 258 } |
| 239 } | 259 } |
| 240 | 260 |
| 241 return S_OK; | 261 return S_OK; |
| 242 } | 262 } |
| 243 | 263 |
| 244 STDMETHODIMP ElevatedControllerWin::StopDaemon() { | 264 STDMETHODIMP ElevatedControllerWin::StopDaemon() { |
| 245 ScopedScHandle service; | 265 ScopedScHandle service; |
| 246 HRESULT hr = OpenService(&service); | 266 HRESULT hr = OpenService(&service); |
| 247 if (FAILED(hr)) { | 267 if (FAILED(hr)) { |
| 248 return hr; | 268 return hr; |
| 249 } | 269 } |
| 250 | 270 |
| 271 // Change the service start type to 'manual'. |
| 272 if (!::ChangeServiceConfigW(service, |
| 273 SERVICE_NO_CHANGE, |
| 274 SERVICE_DEMAND_START, |
| 275 SERVICE_NO_CHANGE, |
| 276 NULL, |
| 277 NULL, |
| 278 NULL, |
| 279 NULL, |
| 280 NULL, |
| 281 NULL, |
| 282 NULL)) { |
| 283 DWORD error = GetLastError(); |
| 284 LOG_GETLASTERROR(ERROR) |
| 285 << "Failed to change the '" << kWindowsServiceName |
| 286 << "'service start type to 'manual'"; |
| 287 return HRESULT_FROM_WIN32(error); |
| 288 } |
| 289 |
| 290 // Stop the service. |
| 251 SERVICE_STATUS status; | 291 SERVICE_STATUS status; |
| 252 if (!ControlService(service, SERVICE_CONTROL_STOP, &status)) { | 292 if (!ControlService(service, SERVICE_CONTROL_STOP, &status)) { |
| 253 DWORD error = GetLastError(); | 293 DWORD error = GetLastError(); |
| 254 if (error != ERROR_SERVICE_NOT_ACTIVE) { | 294 if (error != ERROR_SERVICE_NOT_ACTIVE) { |
| 255 LOG_GETLASTERROR(ERROR) | 295 LOG_GETLASTERROR(ERROR) |
| 256 << "Failed to stop the '" << kWindowsServiceName << "'service"; | 296 << "Failed to stop the '" << kWindowsServiceName << "'service"; |
| 257 return HRESULT_FROM_WIN32(error); | 297 return HRESULT_FROM_WIN32(error); |
| 258 } | 298 } |
| 259 } | 299 } |
| 260 | 300 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 272 ::OpenSCManagerW(NULL, SERVICES_ACTIVE_DATABASE, | 312 ::OpenSCManagerW(NULL, SERVICES_ACTIVE_DATABASE, |
| 273 SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE)); | 313 SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE)); |
| 274 if (!scmanager.IsValid()) { | 314 if (!scmanager.IsValid()) { |
| 275 error = GetLastError(); | 315 error = GetLastError(); |
| 276 LOG_GETLASTERROR(ERROR) | 316 LOG_GETLASTERROR(ERROR) |
| 277 << "Failed to connect to the service control manager"; | 317 << "Failed to connect to the service control manager"; |
| 278 | 318 |
| 279 return HRESULT_FROM_WIN32(error); | 319 return HRESULT_FROM_WIN32(error); |
| 280 } | 320 } |
| 281 | 321 |
| 322 DWORD desired_access = SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | |
| 323 SERVICE_START | SERVICE_STOP; |
| 282 ScopedScHandle service( | 324 ScopedScHandle service( |
| 283 ::OpenServiceW(scmanager, UTF8ToUTF16(kWindowsServiceName).c_str(), | 325 ::OpenServiceW(scmanager, UTF8ToUTF16(kWindowsServiceName).c_str(), |
| 284 SERVICE_QUERY_STATUS | SERVICE_START | SERVICE_STOP)); | 326 desired_access)); |
| 285 if (!service.IsValid()) { | 327 if (!service.IsValid()) { |
| 286 error = GetLastError(); | 328 error = GetLastError(); |
| 287 LOG_GETLASTERROR(ERROR) | 329 LOG_GETLASTERROR(ERROR) |
| 288 << "Failed to open to the '" << kWindowsServiceName << "' service"; | 330 << "Failed to open to the '" << kWindowsServiceName << "' service"; |
| 289 | 331 |
| 290 return HRESULT_FROM_WIN32(error); | 332 return HRESULT_FROM_WIN32(error); |
| 291 } | 333 } |
| 292 | 334 |
| 293 service_out->Set(service.Take()); | 335 service_out->Set(service.Take()); |
| 294 return S_OK; | 336 return S_OK; |
| 295 } | 337 } |
| 296 | 338 |
| 297 } // namespace remoting | 339 } // namespace remoting |
| OLD | NEW |