lomiri-location-service  ..
An aggregating location service providing positioning and geocoding capabilities to applications.
skeleton.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012-2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 #ifndef LOCATION_SERVICE_COM_LOMIRI_LOCATION_SERVICE_SKELETON_H_
19 #define LOCATION_SERVICE_COM_LOMIRI_LOCATION_SERVICE_SKELETON_H_
20 
24 
25 #include <core/dbus/dbus.h>
26 #include <core/dbus/object.h>
27 #include <core/dbus/property.h>
28 #include <core/dbus/service_watcher.h>
29 #include <core/dbus/skeleton.h>
30 
31 #include <core/dbus/interfaces/properties.h>
32 
33 #include <functional>
34 
35 namespace com
36 {
37 namespace lomiri
38 {
39 namespace location
40 {
41 namespace service
42 {
43 class Skeleton
44  : public core::dbus::Skeleton<com::lomiri::location::service::Interface>,
45  public std::enable_shared_from_this<Skeleton>
46 {
47 public:
48  typedef std::shared_ptr<Skeleton> Ptr;
49 
50  // Models resolution of an incoming dbus message to the credentials of the message sender.
52  {
53  typedef std::shared_ptr<CredentialsResolver> Ptr;
54 
55  CredentialsResolver() = default;
56  virtual ~CredentialsResolver() = default;
57 
58  // Resolves the sender of msg to the respective credentials.
59  virtual Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr& msg) = 0;
60  };
61 
62  // Implements CredentialsResolver by reaching out to the dbus daemon and
63  // invoking:
64  // * GetConnectionUnixProcessID
65  // * GetConnectionUnixUser
67  {
68  // Functor for resolving a process id to an app-armor profile name.
69  typedef std::function<std::string(pid_t)> AppArmorProfileResolver;
70 
71  // Returns an AppArmorProfileResolver leveraging libapparmor.
73 
74  // Sets up a new instance for the given bus connection.
75  DBusDaemonCredentialsResolver(const core::dbus::Bus::Ptr& bus,
77 
78  // Resolves the sender of msg to pid, uid by calling out to the dbus daemon.
79  Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr& msg);
80 
81  // Stub for accessing the dbus daemon.
82  core::dbus::DBus daemon;
83 
84  // Helper to resolve an application's pid to an app-armor profile name.
86  };
87 
88  // Models the generation of stable and unique object paths for client-specific sessions.
89  // The requirements for the resulting object path are:
90  // * Unique for the entire system over its complete lifetime
91  // * Stable with respect to an app. That is, one app is always assigned the same object path.
93  {
94  typedef std::shared_ptr<ObjectPathGenerator> Ptr;
95 
96  ObjectPathGenerator() = default;
97  virtual ~ObjectPathGenerator() = default;
98 
99  // Calculates an object path from pid and uid. The default implementation
100  // creates the path according to the following steps:
101  // [1.] Query the AppArmor profile name for pid in credentials.
102  // [1.1] If the process is running unconfined, rely on a counter to assemble the session name.
103  // [1.2] If the process is confined, use the AppArmor profile name to generate the path.
104  virtual core::dbus::types::ObjectPath object_path_for_caller_credentials(const Credentials& credentials);
105  };
106 
108  {
109  // DBus connection set up for handling requests to the service.
110  core::dbus::Bus::Ptr incoming;
111  // DBus connection for reaching out to other services in a non-blocking way.
112  core::dbus::Bus::Ptr outgoing;
113  // An implementation of CredentialsResolver for resolving incoming message sender
114  // to Credentials = uid, pid.
116  // An implementation of ObjectPathGenerator for generating session names.
118  // Permission manager implementation for verifying incoming requests.
120  };
121 
122  Skeleton(const Configuration& configuration);
123  ~Skeleton() noexcept;
124 
125  // From com::lomiri::location::service::Interface
126  const core::Property<State>& state() const;
127  core::Property<bool>& does_satellite_based_positioning();
128  core::Property<bool>& does_report_cell_and_wifi_ids();
129  core::Property<bool>& is_online();
130  core::Property<std::map<SpaceVehicle::Key, SpaceVehicle>>& visible_space_vehicles();
131  core::Property<std::vector<std::string>>& client_applications();
132 
133 protected:
134  // Enable subclasses to alter the state.
135  core::Property<State>& mutable_state();
136 private:
137  // Handles incoming message calls for create_session_for_criteria.
138  // Dispatches to the actual implementation, and manages object lifetimes.
139  void handle_create_session_for_criteria(const core::dbus::Message::Ptr& msg);
140 
141  // Tries to register the given session under the given path in the session store.
142  // Returns true iff the session has been added to the store.
143  bool add_to_session_store_for_path(
144  const core::dbus::types::ObjectPath& path,
145  std::unique_ptr<core::dbus::ServiceWatcher> watcher,
146  const session::Interface::Ptr& session);
147 
148  // Removes the session with the given path from the session store.
149  void remove_from_session_store_for_path(const core::dbus::types::ObjectPath& path);
150 
151  void add_client_application(const std::string& app_id);
152  void remove_client_application(const std::string& app_id);
153 
154  // Called whenever the overall state of the service changes.
155  void on_state_changed(State state);
156  // Called whenever the value of the respective property changes.
157  void on_does_satellite_based_positioning_changed(bool value);
158  // Called whenever the value of the respective property changes.
159  void on_does_report_cell_and_wifi_ids_changed(bool value);
160  // Called whenever the value of the respective property changes.
161  void on_is_online_changed(bool value);
162  void on_client_applications_changed(const std::vector<std::string>& value);
163 
164  // Stores the configuration passed in at creation time.
165  Configuration configuration;
166  // We observe sessions if they have died and resigned from the bus.
167  core::dbus::DBus daemon;
168  // The skeleton object representing com.lomiri.location.service.Interface on the bus.
169  core::dbus::Object::Ptr object;
170  // We emit property changes manually.
171  core::dbus::Signal
172  <
173  core::dbus::interfaces::Properties::Signals::PropertiesChanged,
174  core::dbus::interfaces::Properties::Signals::PropertiesChanged::ArgumentType
175  >::Ptr properties_changed;
176 
177  // DBus properties as exposed on the bus for com.lomiri.location.service.Interface
178  struct
179  {
180  std::shared_ptr< core::dbus::Property<Interface::Properties::State> > state;
181  std::shared_ptr< core::dbus::Property<Interface::Properties::DoesSatelliteBasedPositioning> > does_satellite_based_positioning;
182  std::shared_ptr< core::dbus::Property<Interface::Properties::DoesReportCellAndWifiIds> > does_report_cell_and_wifi_ids;
183  std::shared_ptr< core::dbus::Property<Interface::Properties::IsOnline> > is_online;
184  std::shared_ptr< core::dbus::Property<Interface::Properties::VisibleSpaceVehicles> > visible_space_vehicles;
185  std::shared_ptr< core::dbus::Property<Interface::Properties::ClientApplications> > client_applications;
186  } properties;
187  // We sign up to property changes here, to be able to report them to the bus
188  struct
189  {
190  core::ScopedConnection state;
191  core::ScopedConnection does_satellite_based_positioning;
192  core::ScopedConnection does_report_cell_and_wifi_ids;
193  core::ScopedConnection is_online;
194  core::ScopedConnection client_applications;
195  } connections;
196  // Guards the session store.
197  std::mutex guard;
198  // We track sessions and their respective watchers.
199  struct Element
200  {
201  std::unique_ptr<core::dbus::ServiceWatcher> watcher;
202  std::shared_ptr<session::Interface> session;
203  };
204  // Keeps track of running sessions, keying them by their unique object path.
205  std::map<dbus::types::ObjectPath, Element> session_store;
206 };
207 }
208 }
209 }
210 }
211 #endif // LOCATION_SERVICE_COM_LOMIRI_LOCATION_SERVICE_SKELETON_H_
com::lomiri::location::service::Skeleton::mutable_state
core::Property< State > & mutable_state()
com::lomiri::location::service::Credentials
Credentials of a remote session.
Definition: permission_manager.h:36
com::lomiri::location::service::Skeleton::ObjectPathGenerator::Ptr
std::shared_ptr< ObjectPathGenerator > Ptr
Definition: skeleton.h:94
core::dbus
Definition: codec.h:40
com::lomiri::location::service::Skeleton::Ptr
std::shared_ptr< Skeleton > Ptr
Definition: skeleton.h:48
com::lomiri::location::service::Skeleton::client_applications
std::shared_ptr< core::dbus::Property< Interface::Properties::ClientApplications > > client_applications
Definition: skeleton.h:185
com::lomiri::location::service::Skeleton::Configuration::outgoing
core::dbus::Bus::Ptr outgoing
Definition: skeleton.h:112
com::lomiri::location::service::Skeleton::ObjectPathGenerator::object_path_for_caller_credentials
virtual core::dbus::types::ObjectPath object_path_for_caller_credentials(const Credentials &credentials)
com::lomiri::location::service::PermissionManager::Ptr
std::shared_ptr< PermissionManager > Ptr
Definition: permission_manager.h:54
com::lomiri::location::service::Skeleton::Configuration::permission_manager
PermissionManager::Ptr permission_manager
Definition: skeleton.h:119
com::lomiri::location::service::Skeleton::CredentialsResolver::~CredentialsResolver
virtual ~CredentialsResolver()=default
com::lomiri::location::service::Skeleton::does_report_cell_and_wifi_ids
core::ScopedConnection does_report_cell_and_wifi_ids
Definition: skeleton.h:192
com::lomiri::location::service::Skeleton::DBusDaemonCredentialsResolver::app_armor_profile_resolver
AppArmorProfileResolver app_armor_profile_resolver
Definition: skeleton.h:85
com::lomiri::location::service::Skeleton::Configuration::credentials_resolver
CredentialsResolver::Ptr credentials_resolver
Definition: skeleton.h:115
interface.h
com::lomiri::location::service::Interface
The Interface class models the primary interface to the location service.
Definition: interface.h:48
com::lomiri::location::service::Skeleton::ObjectPathGenerator::ObjectPathGenerator
ObjectPathGenerator()=default
com::lomiri::location::service::Skeleton::ObjectPathGenerator::~ObjectPathGenerator
virtual ~ObjectPathGenerator()=default
com::lomiri::location::service::Skeleton::Configuration
Definition: skeleton.h:107
com::lomiri::location::service::Skeleton::does_satellite_based_positioning
core::ScopedConnection does_satellite_based_positioning
Definition: skeleton.h:191
com::lomiri::location::service::Skeleton::ObjectPathGenerator
Definition: skeleton.h:92
com::lomiri::location::service::Skeleton::~Skeleton
~Skeleton() noexcept
com::lomiri::location::service::Skeleton::DBusDaemonCredentialsResolver::AppArmorProfileResolver
std::function< std::string(pid_t)> AppArmorProfileResolver
Definition: skeleton.h:69
com::lomiri::location::service::Skeleton::does_report_cell_and_wifi_ids
std::shared_ptr< core::dbus::Property< Interface::Properties::DoesReportCellAndWifiIds > > does_report_cell_and_wifi_ids
Definition: skeleton.h:182
com::lomiri::location::service::State
State
State enumerates the known states of the service.
Definition: state.h:26
com::lomiri::location::service::Skeleton::state
core::ScopedConnection state
Definition: skeleton.h:190
com::lomiri::location::service::Skeleton::is_online
core::ScopedConnection is_online
Definition: skeleton.h:193
com::lomiri::location::service::Skeleton::Configuration::object_path_generator
ObjectPathGenerator::Ptr object_path_generator
Definition: skeleton.h:117
com::lomiri::location::service::Skeleton::DBusDaemonCredentialsResolver::DBusDaemonCredentialsResolver
DBusDaemonCredentialsResolver(const core::dbus::Bus::Ptr &bus, AppArmorProfileResolver app_armor_profile_resolver)
com::lomiri::location::service::Skeleton::CredentialsResolver::resolve_credentials_for_incoming_message
virtual Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr &msg)=0
Skeleton
com::lomiri::location::service::Skeleton::DBusDaemonCredentialsResolver::daemon
core::dbus::DBus daemon
Definition: skeleton.h:82
core
Definition: codec.h:38
com::lomiri::location::service::Skeleton::Skeleton
Skeleton(const Configuration &configuration)
com::lomiri::location::service::Skeleton::Configuration::incoming
core::dbus::Bus::Ptr incoming
Definition: skeleton.h:110
com::lomiri::location::service::Skeleton::DBusDaemonCredentialsResolver::resolve_credentials_for_incoming_message
Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr &msg)
com::lomiri::location::service::Skeleton::client_applications
core::ScopedConnection client_applications
Definition: skeleton.h:194
com::lomiri::location::service::Skeleton::visible_space_vehicles
std::shared_ptr< core::dbus::Property< Interface::Properties::VisibleSpaceVehicles > > visible_space_vehicles
Definition: skeleton.h:184
com::lomiri::location::service::Skeleton::is_online
std::shared_ptr< core::dbus::Property< Interface::Properties::IsOnline > > is_online
Definition: skeleton.h:183
com::lomiri::location::service::Skeleton
Definition: skeleton.h:43
com
Definition: accuracy.h:23
interface.h
com::lomiri::location::service::Skeleton::CredentialsResolver
Definition: skeleton.h:51
com::lomiri::location::service::Skeleton::CredentialsResolver::CredentialsResolver
CredentialsResolver()=default
com::lomiri::location::service::Skeleton::does_satellite_based_positioning
std::shared_ptr< core::dbus::Property< Interface::Properties::DoesSatelliteBasedPositioning > > does_satellite_based_positioning
Definition: skeleton.h:181
com::lomiri::location::service::Skeleton::CredentialsResolver::Ptr
std::shared_ptr< CredentialsResolver > Ptr
Definition: skeleton.h:53
permission_manager.h
com::lomiri::location::service::Skeleton::state
std::shared_ptr< core::dbus::Property< Interface::Properties::State > > state
Definition: skeleton.h:180
com::lomiri::location::SpaceVehicle
A space-vehicle as visible to providers.
Definition: space_vehicle.h:33
com::lomiri::location::service::Skeleton::DBusDaemonCredentialsResolver
Definition: skeleton.h:66
com::lomiri::location::service::Skeleton::DBusDaemonCredentialsResolver::libapparmor_profile_resolver
static AppArmorProfileResolver libapparmor_profile_resolver()