| 1 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2020 Tomas Bzatek <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <src/udisksdaemon.h> |
| 24 |
#include <src/udiskslogging.h> |
| 25 |
#include <src/udiskslinuxdevice.h> |
| 26 |
#include <src/udisksmodulemanager.h> |
| 27 |
#include <src/udisksmodule.h> |
| 28 |
#include <src/udisksmoduleobject.h> |
| 29 |
#include <src/udiskslinuxdriveobject.h> |
| 30 |
|
| 31 |
#include "udiskslinuxmodulelsm.h" |
| 32 |
#include "lsm_types.h" |
| 33 |
#include "lsm_linux_drive.h" |
| 34 |
#include "lsm_linux_drive_local.h" |
| 35 |
#include "lsm_data.h" |
| 36 |
|
| 37 |
/** |
| 38 |
* SECTION:udiskslinuxmodulelsm |
| 39 |
* @title: UDisksLinuxModuleLSM |
| 40 |
* @short_description: libstoragemgmt module. |
| 41 |
* |
| 42 |
* The libstoragemgmt module. |
| 43 |
*/ |
| 44 |
|
| 45 |
/** |
| 46 |
* UDisksLinuxModuleLSM: |
| 47 |
* |
| 48 |
* The #UDisksLinuxModuleLSM structure contains only private data |
| 49 |
* and should only be accessed using the provided API. |
| 50 |
*/ |
| 51 |
struct _UDisksLinuxModuleLSM { |
| 52 |
UDisksModule parent_instance; |
| 53 |
}; |
| 54 |
|
| 55 |
typedef struct _UDisksLinuxModuleLSMClass UDisksLinuxModuleLSMClass; |
| 56 |
|
| 57 |
struct _UDisksLinuxModuleLSMClass { |
| 58 |
UDisksModuleClass parent_class; |
| 59 |
}; |
| 60 |
|
| 61 |
static void initable_iface_init (GInitableIface *initable_iface); |
| 62 |
|
| 63 |
G_DEFINE_TYPE_WITH_CODE (UDisksLinuxModuleLSM, udisks_linux_module_lsm, UDISKS_TYPE_MODULE, |
| 64 |
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)); |
| 65 |
|
| 66 |
|
| 67 |
static void |
| 68 |
udisks_linux_module_lsm_init (UDisksLinuxModuleLSM *module) |
| 69 |
{ |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
udisks_linux_module_lsm_constructed (GObject *object) |
| 74 |
{ |
| 75 |
if (G_OBJECT_CLASS (udisks_linux_module_lsm_parent_class)->constructed) |
| 76 |
G_OBJECT_CLASS (udisks_linux_module_lsm_parent_class)->constructed (object); |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
udisks_linux_module_lsm_finalize (GObject *object) |
| 81 |
{ |
| 82 |
std_lsm_data_teardown (); |
| 83 |
|
| 84 |
if (G_OBJECT_CLASS (udisks_linux_module_lsm_parent_class)->finalize) |
| 85 |
G_OBJECT_CLASS (udisks_linux_module_lsm_parent_class)->finalize (object); |
| 86 |
} |
| 87 |
|
| 88 |
gchar * |
| 89 |
udisks_module_id (void) |
| 90 |
{ |
| 91 |
return g_strdup (LSM_MODULE_NAME); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* udisks_module_lsm_new: |
| 96 |
* @daemon: A #UDisksDaemon. |
| 97 |
* @cancellable: (nullable): A #GCancellable or %NULL |
| 98 |
* @error: Return location for error or %NULL. |
| 99 |
* |
| 100 |
* Creates new #UDisksLinuxModuleLSM object. |
| 101 |
* |
| 102 |
* Returns: (transfer full) (type UDisksLinuxModuleLSM): A |
| 103 |
* #UDisksLinuxModuleLSM object or %NULL if @error is set. Free |
| 104 |
* with g_object_unref(). |
| 105 |
*/ |
| 106 |
UDisksModule * |
| 107 |
udisks_module_lsm_new (UDisksDaemon *daemon, |
| 108 |
GCancellable *cancellable, |
| 109 |
GError **error) |
| 110 |
{ |
| 111 |
GInitable *initable; |
| 112 |
|
| 113 |
g_return_val_if_fail (UDISKS_IS_DAEMON (daemon), NULL); |
| 114 |
g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 115 |
|
| 116 |
initable = g_initable_new (UDISKS_TYPE_LINUX_MODULE_LSM, |
| 117 |
cancellable, |
| 118 |
error, |
| 119 |
"daemon", daemon, |
| 120 |
"name", LSM_MODULE_NAME, |
| 121 |
NULL); |
| 122 |
|
| 123 |
if (initable == NULL) |
| 124 |
return NULL; |
| 125 |
else |
| 126 |
return UDISKS_MODULE (initable); |
| 127 |
} |
| 128 |
|
| 129 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 130 |
|
| 131 |
static gboolean |
| 132 |
initable_init (GInitable *initable, |
| 133 |
GCancellable *cancellable, |
| 134 |
GError **error) |
| 135 |
{ |
| 136 |
UDisksLinuxModuleLSM *module = UDISKS_LINUX_MODULE_LSM (initable); |
| 137 |
UDisksDaemon *daemon; |
| 138 |
|
| 139 |
daemon = udisks_module_get_daemon (UDISKS_MODULE (module)); |
| 140 |
if (! std_lsm_data_init (daemon, error)) |
| 141 |
return FALSE; |
| 142 |
|
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
initable_iface_init (GInitableIface *initable_iface) |
| 148 |
{ |
| 149 |
initable_iface->init = initable_init; |
| 150 |
} |
| 151 |
|
| 152 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 153 |
|
| 154 |
gboolean |
| 155 |
udisks_linux_module_lsm_drive_check (UDisksLinuxModuleLSM *module, |
| 156 |
UDisksLinuxDriveObject *drive_object) |
| 157 |
{ |
| 158 |
gboolean is_managed; |
| 159 |
UDisksLinuxDevice *device; |
| 160 |
gboolean rc = FALSE; |
| 161 |
const gchar *wwn; |
| 162 |
|
| 163 |
udisks_debug ("LSM: _drive_check"); |
| 164 |
|
| 165 |
device = udisks_linux_drive_object_get_device (drive_object, TRUE); |
| 166 |
if (device == NULL) |
| 167 |
goto out; |
| 168 |
|
| 169 |
if (g_udev_device_get_property_as_boolean (device->udev_device, "ID_CDROM")) |
| 170 |
goto out; |
| 171 |
|
| 172 |
wwn = g_udev_device_get_property (device->udev_device, "ID_WWN_WITH_EXTENSION"); |
| 173 |
if (! wwn || strlen (wwn) < 2) |
| 174 |
goto out; |
| 175 |
|
| 176 |
/* udev ID_WWN is started with 0x. */ |
| 177 |
is_managed = std_lsm_vpd83_is_managed (wwn + 2); |
| 178 |
if (is_managed == FALSE) |
| 179 |
{ |
| 180 |
/* Refresh and try again. */ |
| 181 |
std_lsm_vpd83_list_refresh (); |
| 182 |
is_managed = std_lsm_vpd83_is_managed (wwn + 2); |
| 183 |
} |
| 184 |
|
| 185 |
if (is_managed == FALSE) |
| 186 |
{ |
| 187 |
udisks_debug ("LSM: VPD %s is not managed by LibstorageMgmt", wwn + 2); |
| 188 |
goto out; |
| 189 |
} |
| 190 |
else |
| 191 |
rc = TRUE; |
| 192 |
|
| 193 |
out: |
| 194 |
g_clear_object (&device); |
| 195 |
|
| 196 |
return rc; |
| 197 |
} |
| 198 |
|
| 199 |
gboolean |
| 200 |
udisks_linux_module_lsm_drive_local_check (UDisksLinuxModuleLSM *module, |
| 201 |
UDisksLinuxDriveObject *drive_object) |
| 202 |
{ |
| 203 |
/* TODO: attach only to drives libstoragemgmt can handle */ |
| 204 |
|
| 205 |
/* The LsmLocalDisk interface is designated available on all disk drives as |
| 206 |
* there is no reliable way to determine whether LED control is properly |
| 207 |
* supported. Client code can only invoke the appropriate procedures for |
| 208 |
* controlling the lights and check for errors that may indicate failure. |
| 209 |
*/ |
| 210 |
|
| 211 |
return TRUE; |
| 212 |
} |
| 213 |
|
| 214 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 215 |
|
| 216 |
static GType * |
| 217 |
udisks_linux_module_lsm_get_drive_object_interface_types (UDisksModule *module) |
| 218 |
{ |
| 219 |
static GType drive_object_interface_types[3]; |
| 220 |
|
| 221 |
g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_LSM (module), NULL); |
| 222 |
|
| 223 |
if (g_once_init_enter (&drive_object_interface_types[0])) |
| 224 |
{ |
| 225 |
/* FIXME: not entirely atomic way, but should work. */ |
| 226 |
drive_object_interface_types[1] = UDISKS_TYPE_LINUX_DRIVE_LSM_LOCAL; |
| 227 |
g_once_init_leave (&drive_object_interface_types[0], UDISKS_TYPE_LINUX_DRIVE_LSM); |
| 228 |
} |
| 229 |
|
| 230 |
return drive_object_interface_types; |
| 231 |
} |
| 232 |
|
| 233 |
static GDBusInterfaceSkeleton * |
| 234 |
udisks_linux_module_lsm_new_drive_object_interface (UDisksModule *module, |
| 235 |
UDisksLinuxDriveObject *object, |
| 236 |
GType interface_type) |
| 237 |
{ |
| 238 |
GDBusInterfaceSkeleton *interface = NULL; |
| 239 |
|
| 240 |
g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_LSM (module), NULL); |
| 241 |
|
| 242 |
if (interface_type == UDISKS_TYPE_LINUX_DRIVE_LSM) |
| 243 |
{ |
| 244 |
if (udisks_linux_module_lsm_drive_check (UDISKS_LINUX_MODULE_LSM (module), object)) |
| 245 |
{ |
| 246 |
interface = G_DBUS_INTERFACE_SKELETON (udisks_linux_drive_lsm_new (UDISKS_LINUX_MODULE_LSM (module), object)); |
| 247 |
} |
| 248 |
} |
| 249 |
else |
| 250 |
if (interface_type == UDISKS_TYPE_LINUX_DRIVE_LSM_LOCAL) |
| 251 |
{ |
| 252 |
if (udisks_linux_module_lsm_drive_local_check (UDISKS_LINUX_MODULE_LSM (module), object)) |
| 253 |
{ |
| 254 |
interface = G_DBUS_INTERFACE_SKELETON (udisks_linux_drive_lsm_local_new (UDISKS_LINUX_MODULE_LSM (module), object)); |
| 255 |
} |
| 256 |
} |
| 257 |
else |
| 258 |
{ |
| 259 |
udisks_error ("Invalid interface type"); |
| 260 |
} |
| 261 |
|
| 262 |
return interface; |
| 263 |
} |
| 264 |
|
| 265 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 266 |
|
| 267 |
static void |
| 268 |
udisks_linux_module_lsm_class_init (UDisksLinuxModuleLSMClass *klass) |
| 269 |
{ |
| 270 |
GObjectClass *gobject_class; |
| 271 |
UDisksModuleClass *module_class; |
| 272 |
|
| 273 |
gobject_class = G_OBJECT_CLASS (klass); |
| 274 |
gobject_class->constructed = udisks_linux_module_lsm_constructed; |
| 275 |
gobject_class->finalize = udisks_linux_module_lsm_finalize; |
| 276 |
|
| 277 |
module_class = UDISKS_MODULE_CLASS (klass); |
| 278 |
module_class->get_drive_object_interface_types = udisks_linux_module_lsm_get_drive_object_interface_types; |
| 279 |
module_class->new_drive_object_interface = udisks_linux_module_lsm_new_drive_object_interface; |
| 280 |
} |