/usr/lib/python3/dist-packages/systemimage/dbus.py is in system-image-common 2.2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | # Copyright (C) 2013-2014 Canonical Ltd.
# Author: Barry Warsaw <barry@ubuntu.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""DBus service."""
__all__ = [
'Loop',
'Service',
]
import os
import sys
import logging
import traceback
from dbus.service import Object, method, signal
from gi.repository import GLib
from systemimage.api import Mediator
from systemimage.config import config
from systemimage.helpers import last_update_date, version_detail
from systemimage.settings import Settings
from threading import Lock
EMPTYSTRING = ''
log = logging.getLogger('systemimage')
class Loop:
"""Keep track of the main loop."""
def __init__(self):
self._loop = GLib.MainLoop()
self._quitter = None
def keepalive(self):
if self._quitter is not None:
GLib.source_remove(self._quitter)
self._quitter = None
self._quitter = GLib.timeout_add_seconds(
config.dbus.lifetime.total_seconds(),
self.quit)
def quit(self):
if self._quitter is not None:
GLib.source_remove(self._quitter)
self._quitter = None
self._loop.quit()
def run(self):
self._loop.run()
class Service(Object):
"""Main dbus service."""
def __init__(self, bus, object_path, loop):
super().__init__(bus, object_path)
self._loop = loop
self._api = Mediator(self._progress_callback)
log.info('Mediator created {}', self._api)
self._checking = Lock()
self._update = None
self._downloading = False
self._paused = False
self._rebootable = False
self._failure_count = 0
self._last_error = ''
def _check_for_update(self):
# Asynchronous method call.
log.info('Enter _check_for_update()')
self._update = self._api.check_for_update()
# Do we have an update and can we auto-download it?
downloading = False
if self._update.is_available:
settings = Settings()
auto = settings.get('auto_download')
log.info('Update available; auto-download: {}', auto)
if auto in ('1', '2'):
# XXX When we have access to the download service, we can
# check if we're on the wifi (auto == '1').
GLib.timeout_add(50, self._download)
downloading = True
self.UpdateAvailableStatus(
self._update.is_available,
downloading,
self._update.version,
self._update.size,
self._update.last_update_date,
# XXX 2013-08-22 - the u/i cannot currently currently handle the
# array of dictionaries data type. LP: #1215586
#self._update.descriptions,
self._update.error)
# Stop GLib from calling this method again.
return False
# 2013-07-25 BAW: should we use the rather underdocumented async_callbacks
# argument to @method?
@method('com.canonical.SystemImage')
def CheckForUpdate(self):
"""Find out whether an update is available.
This method is used to explicitly check whether an update is
available, by communicating with the server and calculating an
upgrade path from the current build number to a later build
available on the server.
This method runs asynchronously and thus does not return a result.
Instead, an `UpdateAvailableStatus` signal is triggered when the check
completes. The argument to that signal is a boolean indicating
whether the update is available or not.
"""
self._loop.keepalive()
# Check-and-acquire the lock.
log.info('test and acquire checking lock')
if not self._checking.acquire(blocking=False):
# Check is already in progress, so there's nothing more to do. If
# there's status available (i.e. we are in the auto-downloading
# phase of the last CFU), then send the status.
if self._update is not None:
self.UpdateAvailableStatus(
self._update.is_available,
self._downloading,
self._update.version,
self._update.size,
self._update.last_update_date,
# XXX 2013-08-22 - the u/i cannot currently currently
# handle the array of dictionaries data type. LP:
# #1215586 self._update.descriptions,
"")
log.info('checking lock not acquired')
return
log.info('checking lock acquired')
# We've now acquired the lock. Reset any failure or in-progress
# state. Get a new mediator to reset any of its state.
self._api = Mediator(self._progress_callback)
log.info('Mediator recreated {}', self._api)
self._failure_count = 0
self._last_error = ''
# Arrange for the actual check to happen in a little while, so that
# this method can return immediately.
GLib.timeout_add(50, self._check_for_update)
def _progress_callback(self, received, total):
# Plumb the progress through our own D-Bus API. Our API is defined as
# signalling a percentage and an eta. We can calculate the percentage
# easily, but the eta is harder. For now, we just send 0 as the eta.
percentage = received * 100 // total
eta = 0
self.UpdateProgress(percentage, eta)
def _download(self):
if self._downloading and self._paused:
self._api.resume()
self._paused = False
log.info('Download previously paused')
return
if (self._downloading # Already in progress.
or self._update is None # Not yet checked.
or not self._update.is_available # No update available.
):
log.info('Download already in progress or not available')
return
if self._failure_count > 0:
self._failure_count += 1
self.UpdateFailed(self._failure_count, self._last_error)
log.info('Update failures: {}; last error: {}',
self._failure_count, self._last_error)
return
self._downloading = True
log.info('Update is downloading')
try:
# Always start by sending a UpdateProgress(0, 0). This is
# enough to get the u/i's attention.
self.UpdateProgress(0, 0)
self._api.download()
except Exception:
self._failure_count += 1
# This will return both the exception name and the exception
# value, but not the traceback.
self._last_error = EMPTYSTRING.join(
traceback.format_exception_only(*sys.exc_info()[:2]))
log.info('Update failed: {}', self._last_error)
self.UpdateFailed(self._failure_count, self._last_error)
else:
log.info('Update downloaded')
self.UpdateDownloaded()
self._failure_count = 0
self._last_error = ''
self._rebootable = True
self._downloading = False
log.info('release checking lock from _download()')
self._checking.release()
# Stop GLib from calling this method again.
return False
@method('com.canonical.SystemImage')
def DownloadUpdate(self):
"""Download the available update.
The download may be canceled during this time.
"""
# Arrange for the update to happen in a little while, so that this
# method can return immediately.
self._loop.keepalive()
GLib.timeout_add(50, self._download)
@method('com.canonical.SystemImage', out_signature='s')
def PauseDownload(self):
"""Pause a downloading update."""
self._loop.keepalive()
if self._downloading:
self._api.pause()
self._paused = True
error_message = ''
else:
error_message = 'not downloading'
return error_message
@method('com.canonical.SystemImage', out_signature='s')
def CancelUpdate(self):
"""Cancel a download."""
self._loop.keepalive()
self._api.cancel()
# If we're holding the checking lock, release it.
try:
self._checking.release()
log.info('release checking lock from CancelUpdate()')
except RuntimeError:
# We're not holding the lock.
pass
# We're now in a failure state until the next CheckForUpdate.
self._failure_count += 1
self._last_error = 'Canceled'
log.info('CancelUpdate() called')
# Only send this signal if we were in the middle of downloading.
if self._downloading:
self.UpdateFailed(self._failure_count, self._last_error)
# XXX 2013-08-22: If we can't cancel the current download, return the
# reason in this string.
return ''
def _apply_update(self):
self._loop.keepalive()
if not self._rebootable:
command_file = os.path.join(
config.updater.cache_partition, 'ubuntu_command')
if not os.path.exists(command_file):
# Not enough has been downloaded to allow for a reboot.
self.Rebooting(False)
return
self._api.reboot()
# This code may or may not run. We're racing against the system
# reboot procedure.
self._rebootable = False
self.Rebooting(True)
@method('com.canonical.SystemImage')
def ApplyUpdate(self):
"""Apply the update, rebooting the device."""
GLib.timeout_add(50, self._apply_update)
return ''
@method('com.canonical.SystemImage', out_signature='isssa{ss}')
def Info(self):
self._loop.keepalive()
return (config.build_number,
config.device,
config.channel,
last_update_date(),
version_detail())
@method('com.canonical.SystemImage', in_signature='ss')
def SetSetting(self, key, value):
"""Set a key/value setting.
Some values are special, e.g. min_battery and auto_downloads.
Implement these special semantics here.
"""
self._loop.keepalive()
if key == 'min_battery':
try:
as_int = int(value)
except ValueError:
return
if as_int < 0 or as_int > 100:
return
if key == 'auto_download':
try:
as_int = int(value)
except ValueError:
return
if as_int not in (0, 1, 2):
return
settings = Settings()
old_value = settings.get(key)
settings.set(key, value)
if value != old_value:
# Send the signal.
self.SettingChanged(key, value)
@method('com.canonical.SystemImage', in_signature='s', out_signature='s')
def GetSetting(self, key):
"""Get a setting."""
self._loop.keepalive()
return Settings().get(key)
@method('com.canonical.SystemImage')
def Exit(self):
"""Quit the daemon immediately."""
self._loop.quit()
# XXX 2013-08-22 The u/i cannot currently handle the array of dictionaries
# data type for the descriptions. LP: #1215586
#@signal('com.canonical.SystemImage', signature='bbsisaa{ss}s')
@signal('com.canonical.SystemImage', signature='bbsiss')
def UpdateAvailableStatus(self,
is_available, downloading,
available_version, update_size,
last_update_date,
#descriptions,
error_reason):
"""Signal sent in response to a CheckForUpdate()."""
log.debug('EMIT UpdateAvailableStatus({}, {}, {}, {}, {}, {})',
is_available, downloading, available_version, update_size,
last_update_date, repr(error_reason))
self._loop.keepalive()
@signal('com.canonical.SystemImage', signature='id')
def UpdateProgress(self, percentage, eta):
"""Download progress."""
log.debug('EMIT UpdateProgress({}, {})', percentage, eta)
self._loop.keepalive()
@signal('com.canonical.SystemImage')
def UpdateDownloaded(self):
"""The update has been successfully downloaded."""
log.debug('EMIT UpdateDownloaded()')
self._loop.keepalive()
@signal('com.canonical.SystemImage', signature='is')
def UpdateFailed(self, consecutive_failure_count, last_reason):
"""The update failed for some reason."""
log.debug('EMIT UpdateFailed({}, {})',
consecutive_failure_count, repr(last_reason))
self._loop.keepalive()
@signal('com.canonical.SystemImage', signature='i')
def UpdatePaused(self, percentage):
"""The download got paused."""
log.debug('EMIT UpdatePaused({})', percentage)
self._loop.keepalive()
@signal('com.canonical.SystemImage', signature='ss')
def SettingChanged(self, key, new_value):
"""A setting value has change."""
log.debug('EMIT SettingChanged({}, {})', repr(key), repr(new_value))
self._loop.keepalive()
@signal('com.canonical.SystemImage', signature='b')
def Rebooting(self, status):
"""The system is rebooting."""
# We don't need to keep the loop alive since we're probably just going
# to shutdown anyway.
log.debug('EMIT Rebooting({})', status)
|