blob: fe347941c1aa3fbb751151724e7a8289f44c4b31 [file] [log] [blame]
// Copyright 2016 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <signal.h>
#include <base/callback.h>
#include <base/cancelable_callback.h>
#include <base/memory/weak_ptr.h>
#include <brillo/daemons/dbus_daemon.h>
#include <brillo/errors/error.h>
#include <brillo/process_reaper.h>
#include "dbus_adaptors/org.chromium.ImageLoaderInterface.h"
#include "helper_process.h"
#include "imageloader_impl.h"
namespace imageloader {
// This is a utility that handles mounting and unmounting of
// verified filesystem images that might include binaries intended
// to be run as read only.
class ImageLoader : public brillo::DBusServiceDaemon,
public org::chromium::ImageLoaderInterfaceInterface {
// User and group to run imageloader as.
static const char kImageLoaderGroupName[];
static const char kImageLoaderUserName[];
ImageLoader(ImageLoaderConfig config, std::unique_ptr<HelperProcess> helper);
// Implementations of the public methods interface.
// Register a component.
bool RegisterComponent(brillo::ErrorPtr* err, const std::string& name,
const std::string& version,
const std::string& component_folder_abs_path,
bool* out_success) override;
// TODO(kerrnel): errors should probably be returned using the err object.
// Get component version given component name.
bool GetComponentVersion(brillo::ErrorPtr* err, const std::string& name,
std::string* out_version) override;
// Load and mount a component.
bool LoadComponent(brillo::ErrorPtr* err, const std::string& name,
std::string* out_mount_point) override;
// Sandboxes the runtime environment, using minijail. This is publicly exposed
// so that can sandbox when not running as a daemon.
static void EnterSandbox();
int OnInit() override;
void RegisterDBusObjectsAsync(
brillo::dbus_utils::AsyncEventSequencer* sequencer) override;
void OnShutdown(int* return_code) override;
// Callback from ProcessReaper to notify ImageLoader that one of the
// subprocesses died.
void OnSubprocessExited(pid_t pid, const siginfo_t& info);
// ImageLoader exits after 20 seconds of inactivity. This function restarts
// the timer.
void PostponeShutdown();
// Daemon will automatically shutdown after this length of idle time.
static const int kShutdownTimeoutMilliseconds;
std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
ImageLoaderImpl impl_;
std::unique_ptr<HelperProcess> helper_process_;
brillo::ProcessReaper process_reaper_;
base::CancelableClosure shutdown_callback_;
org::chromium::ImageLoaderInterfaceAdaptor dbus_adaptor_{this};
base::WeakPtrFactory<ImageLoader> weak_factory_{this};
} // namespace imageloader