| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2015 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 16 |  | 
|  | 17 | #include "update_engine/daemon.h" | 
|  | 18 |  | 
|  | 19 | #include <sysexits.h> | 
|  | 20 |  | 
|  | 21 | #include <base/bind.h> | 
|  | 22 | #include <base/location.h> | 
| Alex Deymo | dc066f1 | 2017-03-27 13:01:29 -0700 | [diff] [blame] | 23 | #if USE_BINDER | 
| Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 24 | #include <binderwrapper/binder_wrapper.h> | 
| Alex Deymo | dc066f1 | 2017-03-27 13:01:29 -0700 | [diff] [blame] | 25 | #endif  // USE_BINDER | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 26 |  | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 27 | #if USE_OMAHA | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 28 | #include "update_engine/real_system_state.h" | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 29 | #else  // !USE_OMAHA | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 30 | #include "update_engine/daemon_state_android.h" | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 31 | #endif  // USE_OMAHA | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 32 |  | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 33 | namespace chromeos_update_engine { | 
|  | 34 |  | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 35 | int UpdateEngineDaemon::OnInit() { | 
|  | 36 | // Register the |subprocess_| singleton with this Daemon as the signal | 
|  | 37 | // handler. | 
|  | 38 | subprocess_.Init(this); | 
|  | 39 |  | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 40 | int exit_code = Daemon::OnInit(); | 
|  | 41 | if (exit_code != EX_OK) | 
|  | 42 | return exit_code; | 
|  | 43 |  | 
| Alex Deymo | dc066f1 | 2017-03-27 13:01:29 -0700 | [diff] [blame] | 44 | #if USE_BINDER | 
| Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 45 | android::BinderWrapper::Create(); | 
|  | 46 | binder_watcher_.Init(); | 
| Alex Deymo | dc066f1 | 2017-03-27 13:01:29 -0700 | [diff] [blame] | 47 | #endif  // USE_BINDER | 
| Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 48 |  | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 49 | #if USE_OMAHA | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 50 | // Initialize update engine global state but continue if something fails. | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 51 | // TODO(deymo): Move the daemon_state_ initialization to a factory method | 
|  | 52 | // avoiding the explicit re-usage of the |bus| instance, shared between | 
|  | 53 | // D-Bus service and D-Bus client calls. | 
| Sen Jiang | 299128e | 2016-06-03 17:48:18 -0700 | [diff] [blame] | 54 | RealSystemState* real_system_state = new RealSystemState(); | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 55 | daemon_state_.reset(real_system_state); | 
|  | 56 | LOG_IF(ERROR, !real_system_state->Initialize()) | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 57 | << "Failed to initialize system state."; | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 58 | #else  // !USE_OMAHA | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 59 | DaemonStateAndroid* daemon_state_android = new DaemonStateAndroid(); | 
|  | 60 | daemon_state_.reset(daemon_state_android); | 
|  | 61 | LOG_IF(ERROR, !daemon_state_android->Initialize()) | 
|  | 62 | << "Failed to initialize system state."; | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 63 | #endif  // USE_OMAHA | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 64 |  | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 65 | #if USE_BINDER | 
| Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 66 | // Create the Binder Service. | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 67 | #if USE_OMAHA | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 68 | binder_service_ = new BinderUpdateEngineBrilloService{real_system_state}; | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 69 | #else  // !USE_OMAHA | 
| Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 70 | binder_service_ = new BinderUpdateEngineAndroidService{ | 
|  | 71 | daemon_state_android->service_delegate()}; | 
| Sen Jiang | 192a86a | 2016-05-19 17:21:24 -0700 | [diff] [blame] | 72 | #endif  // USE_OMAHA | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 73 | auto binder_wrapper = android::BinderWrapper::Get(); | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 74 | if (!binder_wrapper->RegisterService(binder_service_->ServiceName(), | 
|  | 75 | binder_service_)) { | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 76 | LOG(ERROR) << "Failed to register binder service."; | 
|  | 77 | } | 
| Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 78 |  | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 79 | daemon_state_->AddObserver(binder_service_.get()); | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 80 | #endif  // USE_BINDER | 
|  | 81 |  | 
| Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 82 | #if USE_DBUS | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 83 | // Create the DBus service. | 
| Sen Jiang | 299128e | 2016-06-03 17:48:18 -0700 | [diff] [blame] | 84 | dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state)); | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 85 | daemon_state_->AddObserver(dbus_adaptor_.get()); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 86 |  | 
|  | 87 | dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered, | 
|  | 88 | base::Unretained(this))); | 
|  | 89 | LOG(INFO) << "Waiting for DBus object to be registered."; | 
| Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 90 | #else  // !USE_DBUS | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 91 | daemon_state_->StartUpdater(); | 
|  | 92 | #endif  // USE_DBUS | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 93 | return EX_OK; | 
|  | 94 | } | 
|  | 95 |  | 
| Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 96 | #if USE_DBUS | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 97 | void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) { | 
|  | 98 | if (!succeeded) { | 
|  | 99 | LOG(ERROR) << "Registering the UpdateEngineAdaptor"; | 
|  | 100 | QuitWithExitCode(1); | 
|  | 101 | return; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | // Take ownership of the service now that everything is initialized. We need | 
|  | 105 | // to this now and not before to avoid exposing a well known DBus service | 
|  | 106 | // path that doesn't have the service it is supposed to implement. | 
|  | 107 | if (!dbus_adaptor_->RequestOwnership()) { | 
|  | 108 | LOG(ERROR) << "Unable to take ownership of the DBus service, is there " | 
|  | 109 | << "other update_engine daemon running?"; | 
|  | 110 | QuitWithExitCode(1); | 
|  | 111 | return; | 
|  | 112 | } | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 113 | daemon_state_->StartUpdater(); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 114 | } | 
| Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 115 | #endif  // USE_DBUS | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 116 |  | 
|  | 117 | }  // namespace chromeos_update_engine |