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/win/elevated_controller.h" | 5 #include "remoting/host/win/elevated_controller.h" |
6 | 6 |
7 #include "base/file_version_info.h" | 7 #include "base/file_version_info.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 NULL)); | 107 NULL)); |
108 | 108 |
109 if (!file.IsValid()) { | 109 if (!file.IsValid()) { |
110 DWORD error = GetLastError(); | 110 DWORD error = GetLastError(); |
111 PLOG(ERROR) << "Failed to open '" << filename.value() << "'"; | 111 PLOG(ERROR) << "Failed to open '" << filename.value() << "'"; |
112 return HRESULT_FROM_WIN32(error); | 112 return HRESULT_FROM_WIN32(error); |
113 } | 113 } |
114 | 114 |
115 scoped_ptr<char[]> buffer(new char[kMaxConfigFileSize]); | 115 scoped_ptr<char[]> buffer(new char[kMaxConfigFileSize]); |
116 DWORD size = kMaxConfigFileSize; | 116 DWORD size = kMaxConfigFileSize; |
117 if (!::ReadFile(file, &buffer[0], size, &size, NULL)) { | 117 if (!::ReadFile(file.Get(), &buffer[0], size, &size, NULL)) { |
118 DWORD error = GetLastError(); | 118 DWORD error = GetLastError(); |
119 PLOG(ERROR) << "Failed to read '" << filename.value() << "'"; | 119 PLOG(ERROR) << "Failed to read '" << filename.value() << "'"; |
120 return HRESULT_FROM_WIN32(error); | 120 return HRESULT_FROM_WIN32(error); |
121 } | 121 } |
122 | 122 |
123 // Parse the JSON configuration, expecting it to contain a dictionary. | 123 // Parse the JSON configuration, expecting it to contain a dictionary. |
124 std::string file_content(buffer.get(), size); | 124 std::string file_content(buffer.get(), size); |
125 scoped_ptr<base::Value> value( | 125 scoped_ptr<base::Value> value( |
126 base::JSONReader::Read(file_content, base::JSON_ALLOW_TRAILING_COMMAS)); | 126 base::JSONReader::Read(file_content, base::JSON_ALLOW_TRAILING_COMMAS)); |
127 | 127 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 FILE_FLAG_SEQUENTIAL_SCAN, | 170 FILE_FLAG_SEQUENTIAL_SCAN, |
171 NULL)); | 171 NULL)); |
172 | 172 |
173 if (!file.IsValid()) { | 173 if (!file.IsValid()) { |
174 DWORD error = GetLastError(); | 174 DWORD error = GetLastError(); |
175 PLOG(ERROR) << "Failed to create '" << filename.value() << "'"; | 175 PLOG(ERROR) << "Failed to create '" << filename.value() << "'"; |
176 return HRESULT_FROM_WIN32(error); | 176 return HRESULT_FROM_WIN32(error); |
177 } | 177 } |
178 | 178 |
179 DWORD written; | 179 DWORD written; |
180 if (!WriteFile(file, content, static_cast<DWORD>(length), &written, NULL)) { | 180 if (!WriteFile(file.Get(), content, static_cast<DWORD>(length), &written, |
| 181 NULL)) { |
181 DWORD error = GetLastError(); | 182 DWORD error = GetLastError(); |
182 PLOG(ERROR) << "Failed to write to '" << filename.value() << "'"; | 183 PLOG(ERROR) << "Failed to write to '" << filename.value() << "'"; |
183 return HRESULT_FROM_WIN32(error); | 184 return HRESULT_FROM_WIN32(error); |
184 } | 185 } |
185 | 186 |
186 return S_OK; | 187 return S_OK; |
187 } | 188 } |
188 | 189 |
189 // Moves a config file from its temporary location to its permanent location. | 190 // Moves a config file from its temporary location to its permanent location. |
190 HRESULT MoveConfigFileFromTemp(const base::FilePath& filename) { | 191 HRESULT MoveConfigFileFromTemp(const base::FilePath& filename) { |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 } | 366 } |
366 | 367 |
367 STDMETHODIMP ElevatedController::StartDaemon() { | 368 STDMETHODIMP ElevatedController::StartDaemon() { |
368 ScopedScHandle service; | 369 ScopedScHandle service; |
369 HRESULT hr = OpenService(&service); | 370 HRESULT hr = OpenService(&service); |
370 if (FAILED(hr)) { | 371 if (FAILED(hr)) { |
371 return hr; | 372 return hr; |
372 } | 373 } |
373 | 374 |
374 // Change the service start type to 'auto'. | 375 // Change the service start type to 'auto'. |
375 if (!::ChangeServiceConfigW(service, | 376 if (!::ChangeServiceConfigW(service.Get(), |
376 SERVICE_NO_CHANGE, | 377 SERVICE_NO_CHANGE, |
377 SERVICE_AUTO_START, | 378 SERVICE_AUTO_START, |
378 SERVICE_NO_CHANGE, | 379 SERVICE_NO_CHANGE, |
379 NULL, | 380 NULL, |
380 NULL, | 381 NULL, |
381 NULL, | 382 NULL, |
382 NULL, | 383 NULL, |
383 NULL, | 384 NULL, |
384 NULL, | 385 NULL, |
385 NULL)) { | 386 NULL)) { |
386 DWORD error = GetLastError(); | 387 DWORD error = GetLastError(); |
387 PLOG(ERROR) << "Failed to change the '" << kWindowsServiceName | 388 PLOG(ERROR) << "Failed to change the '" << kWindowsServiceName |
388 << "'service start type to 'auto'"; | 389 << "'service start type to 'auto'"; |
389 return HRESULT_FROM_WIN32(error); | 390 return HRESULT_FROM_WIN32(error); |
390 } | 391 } |
391 | 392 |
392 // Start the service. | 393 // Start the service. |
393 if (!StartService(service, 0, NULL)) { | 394 if (!StartService(service.Get(), 0, NULL)) { |
394 DWORD error = GetLastError(); | 395 DWORD error = GetLastError(); |
395 if (error != ERROR_SERVICE_ALREADY_RUNNING) { | 396 if (error != ERROR_SERVICE_ALREADY_RUNNING) { |
396 PLOG(ERROR) << "Failed to start the '" << kWindowsServiceName | 397 PLOG(ERROR) << "Failed to start the '" << kWindowsServiceName |
397 << "'service"; | 398 << "'service"; |
398 | 399 |
399 return HRESULT_FROM_WIN32(error); | 400 return HRESULT_FROM_WIN32(error); |
400 } | 401 } |
401 } | 402 } |
402 | 403 |
403 return S_OK; | 404 return S_OK; |
404 } | 405 } |
405 | 406 |
406 STDMETHODIMP ElevatedController::StopDaemon() { | 407 STDMETHODIMP ElevatedController::StopDaemon() { |
407 ScopedScHandle service; | 408 ScopedScHandle service; |
408 HRESULT hr = OpenService(&service); | 409 HRESULT hr = OpenService(&service); |
409 if (FAILED(hr)) { | 410 if (FAILED(hr)) { |
410 return hr; | 411 return hr; |
411 } | 412 } |
412 | 413 |
413 // Change the service start type to 'manual'. | 414 // Change the service start type to 'manual'. |
414 if (!::ChangeServiceConfigW(service, | 415 if (!::ChangeServiceConfigW(service.Get(), |
415 SERVICE_NO_CHANGE, | 416 SERVICE_NO_CHANGE, |
416 SERVICE_DEMAND_START, | 417 SERVICE_DEMAND_START, |
417 SERVICE_NO_CHANGE, | 418 SERVICE_NO_CHANGE, |
418 NULL, | 419 NULL, |
419 NULL, | 420 NULL, |
420 NULL, | 421 NULL, |
421 NULL, | 422 NULL, |
422 NULL, | 423 NULL, |
423 NULL, | 424 NULL, |
424 NULL)) { | 425 NULL)) { |
425 DWORD error = GetLastError(); | 426 DWORD error = GetLastError(); |
426 PLOG(ERROR) << "Failed to change the '" << kWindowsServiceName | 427 PLOG(ERROR) << "Failed to change the '" << kWindowsServiceName |
427 << "'service start type to 'manual'"; | 428 << "'service start type to 'manual'"; |
428 return HRESULT_FROM_WIN32(error); | 429 return HRESULT_FROM_WIN32(error); |
429 } | 430 } |
430 | 431 |
431 // Stop the service. | 432 // Stop the service. |
432 SERVICE_STATUS status; | 433 SERVICE_STATUS status; |
433 if (!ControlService(service, SERVICE_CONTROL_STOP, &status)) { | 434 if (!ControlService(service.Get(), SERVICE_CONTROL_STOP, &status)) { |
434 DWORD error = GetLastError(); | 435 DWORD error = GetLastError(); |
435 if (error != ERROR_SERVICE_NOT_ACTIVE) { | 436 if (error != ERROR_SERVICE_NOT_ACTIVE) { |
436 PLOG(ERROR) << "Failed to stop the '" << kWindowsServiceName | 437 PLOG(ERROR) << "Failed to stop the '" << kWindowsServiceName |
437 << "'service"; | 438 << "'service"; |
438 return HRESULT_FROM_WIN32(error); | 439 return HRESULT_FROM_WIN32(error); |
439 } | 440 } |
440 } | 441 } |
441 | 442 |
442 return S_OK; | 443 return S_OK; |
443 } | 444 } |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 if (!scmanager.IsValid()) { | 507 if (!scmanager.IsValid()) { |
507 error = GetLastError(); | 508 error = GetLastError(); |
508 PLOG(ERROR) << "Failed to connect to the service control manager"; | 509 PLOG(ERROR) << "Failed to connect to the service control manager"; |
509 | 510 |
510 return HRESULT_FROM_WIN32(error); | 511 return HRESULT_FROM_WIN32(error); |
511 } | 512 } |
512 | 513 |
513 DWORD desired_access = SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | | 514 DWORD desired_access = SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | |
514 SERVICE_START | SERVICE_STOP; | 515 SERVICE_START | SERVICE_STOP; |
515 ScopedScHandle service( | 516 ScopedScHandle service( |
516 ::OpenServiceW(scmanager, kWindowsServiceName, desired_access)); | 517 ::OpenServiceW(scmanager.Get(), kWindowsServiceName, desired_access)); |
517 if (!service.IsValid()) { | 518 if (!service.IsValid()) { |
518 error = GetLastError(); | 519 error = GetLastError(); |
519 PLOG(ERROR) << "Failed to open to the '" << kWindowsServiceName | 520 PLOG(ERROR) << "Failed to open to the '" << kWindowsServiceName |
520 << "' service"; | 521 << "' service"; |
521 | 522 |
522 return HRESULT_FROM_WIN32(error); | 523 return HRESULT_FROM_WIN32(error); |
523 } | 524 } |
524 | 525 |
525 service_out->Set(service.Take()); | 526 service_out->Set(service.Take()); |
526 return S_OK; | 527 return S_OK; |
527 } | 528 } |
528 | 529 |
529 } // namespace remoting | 530 } // namespace remoting |
OLD | NEW |