common_service.h revision 5b5fa8b412312a41cfd4d7ab475b54d4f730ed2a
1a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
2a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// Copyright (C) 2016 The Android Open Source Project
3a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
4a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// Licensed under the Apache License, Version 2.0 (the "License");
5a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// you may not use this file except in compliance with the License.
6a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// You may obtain a copy of the License at
7a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
8a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//      http://www.apache.org/licenses/LICENSE-2.0
9a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
10a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// Unless required by applicable law or agreed to in writing, software
11a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// distributed under the License is distributed on an "AS IS" BASIS,
12a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// See the License for the specific language governing permissions and
14a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// limitations under the License.
15a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
16a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
17a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#ifndef UPDATE_ENGINE_COMMON_SERVICE_H_
18a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#define UPDATE_ENGINE_COMMON_SERVICE_H_
19a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
20a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <inttypes.h>
21a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
22a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <string>
23a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
24a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <base/memory/ref_counted.h>
25a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <brillo/errors/error.h>
26a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
27a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/system_state.h"
28a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
29a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinnamespace chromeos_update_engine {
30a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
31a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinclass UpdateEngineService {
32a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin public:
33a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Flags used in the AttemptUpdateWithFlags() D-Bus method.
34a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  typedef enum {
35a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    kAttemptUpdateFlagNonInteractive = (1<<0)
36a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  } AttemptUpdateFlags;
37a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
38a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Error domain for all the service errors.
39a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  static const char* const kErrorDomain;
40a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
41a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Generic service error.
42a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  static const char* const kErrorFailed;
43a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
44a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  explicit UpdateEngineService(SystemState* system_state);
45a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  virtual ~UpdateEngineService() = default;
46a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
47a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool AttemptUpdate(brillo::ErrorPtr* error,
48a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                     const std::string& in_app_version,
49a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                     const std::string& in_omaha_url,
50a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                     int32_t in_flags_as_int);
51a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
52a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool AttemptRollback(brillo::ErrorPtr* error, bool in_powerwash);
53a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
54a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Checks if the system rollback is available by verifying if the secondary
55a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // system partition is valid and bootable.
56a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool CanRollback(brillo::ErrorPtr* error, bool* out_can_rollback);
57a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
58a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Resets the status of the update_engine to idle, ignoring any applied
59a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // update. This is used for development only.
60a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool ResetStatus(brillo::ErrorPtr* error);
61a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
62a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Returns the current status of the Update Engine. If an update is in
63a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // progress, the number of operations, size to download and overall progress
64a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // is reported.
65a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool GetStatus(brillo::ErrorPtr* error,
66a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                 int64_t* out_last_checked_time,
67a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                 double* out_progress,
68a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                 std::string* out_current_operation,
69a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                 std::string* out_new_version,
70a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                 int64_t* out_new_size);
71a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
72a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Reboots the device if an update is applied and a reboot is required.
73a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool RebootIfNeeded(brillo::ErrorPtr* error);
74a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
75a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Changes the current channel of the device to the target channel. If the
76a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // target channel is a less stable channel than the current channel, then the
77a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // channel change happens immediately (at the next update check).  If the
78a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // target channel is a more stable channel, then if is_powerwash_allowed is
79a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // set to true, then also the change happens immediately but with a powerwash
80a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // if required. Otherwise, the change takes effect eventually (when the
81a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // version on the target channel goes above the version number of what the
82a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // device currently has).
83a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool SetChannel(brillo::ErrorPtr* error,
84a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                  const std::string& in_target_channel,
85a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                  bool in_is_powerwash_allowed);
86a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
87a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // If get_current_channel is set to true, populates |channel| with the name of
88a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // the channel that the device is currently on. Otherwise, it populates it
89a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // with the name of the channel the device is supposed to be (in case of a
90a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // pending channel change).
91a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool GetChannel(brillo::ErrorPtr* error,
92a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                  bool in_get_current_channel,
93a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                  std::string* out_channel);
94a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
955b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo  // Sets the current "cohort hint" value to |in_cohort_hint|. The cohort hint
965b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo  // is sent back to Omaha on every request and can be used as a hint of what
975b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo  // cohort should we be put on.
985b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo  bool SetCohortHint(brillo::ErrorPtr* error, std::string in_cohort_hint);
995b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo
1005b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo  // Return the current cohort hint. This value can be set with SetCohortHint()
1015b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo  // and can also be updated from Omaha on every update check request.
1025b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo  bool GetCohortHint(brillo::ErrorPtr* error, std::string* out_cohort_hint);
1035b5fa8b412312a41cfd4d7ab475b54d4f730ed2aAlex Deymo
104a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Enables or disables the sharing and consuming updates over P2P feature
105a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // according to the |enabled| argument passed.
106a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool SetP2PUpdatePermission(brillo::ErrorPtr* error, bool in_enabled);
107a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
108a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Returns the current value for the P2P enabled setting. This involves both
109a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // sharing and consuming updates over P2P.
110a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool GetP2PUpdatePermission(brillo::ErrorPtr* error, bool* out_enabled);
111a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
112a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // If there's no device policy installed, sets the update over cellular
113a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // networks permission to the |allowed| value. Otherwise, this method returns
114a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // with an error since this setting is overridden by the applied policy.
115a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool SetUpdateOverCellularPermission(brillo::ErrorPtr* error,
116a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                       bool in_allowed);
117a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
118a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Returns the current value of the update over cellular network setting,
119a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // either forced by the device policy if the device is enrolled or the current
120a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // user preference otherwise.
121a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool GetUpdateOverCellularPermission(brillo::ErrorPtr* error,
122a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                       bool* out_allowed);
123a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
124a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Returns the duration since the last successful update, as the
125a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // duration on the wallclock. Returns an error if the device has not
126a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // updated.
127a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool GetDurationSinceUpdate(brillo::ErrorPtr* error,
128a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                              int64_t* out_usec_wallclock);
129a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
130a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Returns the version string of OS that was used before the last reboot
131a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // into an updated version. This is available only when rebooting into an
132a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // update from previous version, otherwise an empty string is returned.
133a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool GetPrevVersion(brillo::ErrorPtr* error, std::string* out_prev_version);
134a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
135a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Returns the name of kernel partition that can be rolled back into.
136a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool GetRollbackPartition(brillo::ErrorPtr* error,
137a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                            std::string* out_rollback_partition_name);
138a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
1392997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao  // Returns the last UpdateAttempt error.
1402997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao  bool GetLastAttemptError(brillo::ErrorPtr* error,
1412997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao                           int32_t* out_last_attempt_error);
1422997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao
143b3fa53bf760bd0a6fd1ef9df28cb425586d733f6Alex Deymo  // Returns the current end-of-life status of the device. This value is updated
144b3fa53bf760bd0a6fd1ef9df28cb425586d733f6Alex Deymo  // on every update check and persisted on disk across reboots.
145b3fa53bf760bd0a6fd1ef9df28cb425586d733f6Alex Deymo  bool GetEolStatus(brillo::ErrorPtr* error, int32_t* out_eol_status);
146b3fa53bf760bd0a6fd1ef9df28cb425586d733f6Alex Deymo
147a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin private:
148a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  SystemState* system_state_;
149a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin};
150a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
151a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}  // namespace chromeos_update_engine
152a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
153a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#endif  // UPDATE_ENGINE_COMMON_SERVICE_H_
154