Commit 95ef1b8b authored by Christian Kellner's avatar Christian Kellner
Browse files

device+sysfs: new device ident struct

Introduce a new BoltIdent struct that holds the name and vendor
information for a thunderbolt device together with a reference
to the 'struct udev_device' that provides those information.
This binds the lifetime of the char pointers directly to the
struct where they are contained and thus prevent accidental
bugs where the strings are used after the udev_device is not
valid anymore (got un-referenced).
Since the struct holds a reference, define a clear method for
it and the corresponding auto-cleanup method.
Use the new BoltIdent struct for 'bolt_device_ident'. Re-factor
it to not use a loop anymore.

The read_sysattr_name helper, use in 'bolt_device_ident' now
uses the recently introduced sysattr value getter.
parent 1adb3d00
......@@ -1172,10 +1172,9 @@ bolt_device_new_for_udev (struct udev_device *udev,
BoltDomain *domain,
GError **error)
{
BoltDevInfo info;
g_auto(BoltIdent) id = BOLT_IDENT_INIT;
const char *uid;
const char *name;
const char *vendor;
BoltDevInfo info;
BoltStatus status;
BoltAuthFlags aflags;
BoltDeviceType type;
......@@ -1195,7 +1194,7 @@ bolt_device_new_for_udev (struct udev_device *udev,
if (!ok)
return NULL;
ok = bolt_sysfs_device_ident (udev, &name, &vendor, error);
ok = bolt_sysfs_device_ident (udev, &id, error);
if (!ok)
return NULL;
......@@ -1212,8 +1211,8 @@ bolt_device_new_for_udev (struct udev_device *udev,
dev = g_object_new (BOLT_TYPE_DEVICE,
"uid", uid,
"name", name,
"vendor", vendor,
"name", id.name,
"vendor", id.vendor,
"type", type,
"generation", info.generation,
"status", status,
......
......@@ -30,8 +30,19 @@
#include <errno.h>
#include <libudev.h>
#include <string.h>
#include <sys/stat.h>
void
bolt_ident_clear (BoltIdent *id)
{
if (id->udev == NULL)
return;
udev_device_unref (id->udev);
memset (id, 0, sizeof (BoltIdent));
}
static const char *
sysfs_get_sysattr_value (struct udev_device *dev,
const char *attr,
......@@ -187,7 +198,8 @@ bolt_sysfs_security_for_device (struct udev_device *udev,
static const char *
read_sysattr_name (struct udev_device *udev,
const char *attr)
const char *attr,
GError **error)
{
g_autofree char *s = NULL;
const char *v;
......@@ -198,48 +210,32 @@ read_sysattr_name (struct udev_device *udev,
if (v != NULL)
return v;
return udev_device_get_sysattr_value (udev, attr);
return sysfs_get_sysattr_value (udev, attr, error);
}
gboolean
bolt_sysfs_device_ident (struct udev_device *udev,
const char **name,
const char **vendor,
BoltIdent *id,
GError **error)
{
const char *val;
struct
{
const char *attr;
const char **ptr;
} fields[] = {
{"device", name},
{"vendor", vendor}
};
const char *name;
const char *vendor;
g_return_val_if_fail (udev != NULL, FALSE);
g_return_val_if_fail (name != NULL, FALSE);
g_return_val_if_fail (vendor != NULL, FALSE);
g_return_val_if_fail (id != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
for (size_t i = 0; i < G_N_ELEMENTS (fields); i++)
{
const char *key = fields[i].attr;
val = read_sysattr_name (udev, key);
if (val == NULL)
{
g_set_error (error,
BOLT_ERROR, BOLT_ERROR_UDEV,
"'%s' information missing",
key);
vendor = read_sysattr_name (udev, "vendor", error);
if (vendor == NULL)
return FALSE;
return FALSE;
}
name = read_sysattr_name (udev, "device", error);
if (name == NULL)
return FALSE;
*(fields[i].ptr) = val;
}
id->udev = udev_device_ref (udev);
id->name = name;
id->vendor = vendor;
return TRUE;
}
......
......@@ -31,6 +31,22 @@ struct udev_device;
G_BEGIN_DECLS
/* Device identification */
typedef struct _BoltIdent BoltIdent;
struct _BoltIdent
{
struct udev_device *udev;
const char *name;
const char *vendor;
};
#define BOLT_IDENT_INIT {NULL, NULL, NULL}
void bolt_ident_clear (BoltIdent *id);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (BoltIdent, bolt_ident_clear);
const char * bolt_sysfs_device_get_unique_id (struct udev_device *dev,
GError **error);
......@@ -53,8 +69,7 @@ BoltSecurity bolt_sysfs_security_for_device (struct udev_device *udev,
GError **error);
gboolean bolt_sysfs_device_ident (struct udev_device *udev,
const char **name,
const char **vendor,
BoltIdent *id,
GError **error);
int bolt_sysfs_count_hosts (struct udev *udev,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment