sysfs: sysfs_attr_get_value() writes to a buffer instead
PATCH 2/3 sysfs: sysfs_attr_get_value() writes to a buffer instead
This avoids lifetime issues with the returned string, for subsequent changes to caching in sysfs. All callers are changed accordingly. Many callers copied the result to a buffer already, so this actually simplifies some code.
extras/usb_id/usb_id.c +49 -30
@@ -224,9 +224,11 @@ static int usb_id(const char *devpath)struct sysfs_device *dev;struct sysfs_device *dev_interface;struct sysfs_device *dev_usb; -const char *if_class, *if_subclass; +char if_class[NAME_SIZE ]; +char if_subclass[NAME_SIZE ];int if_class_num;int protocol =0 ; +int status;dbg ("devpath %s\n" , devpath);
@@ -244,15 +246,17 @@ static int usb_id(const char *devpath)return 1 ; } - if_class =sysfs_attr_get_value (dev_interface->devpath,"bInterfaceClass" ); -if (!if_class) { + status =sysfs_attr_get_value (dev_interface->devpath,"bInterfaceClass" , + if_class,sizeof (if_class)); +if (!status) {info ("%s: cannot get bInterfaceClass attribute\n" , dev_interface->kernel);return 1 ; } if_class_num =strtoul (if_class,NULL ,16 );if (if_class_num =8 ) { - if_subclass =sysfs_attr_get_value (dev_interface->devpath,"bInterfaceSubClass" ); -if (if_subclass !=NULL ) + status =sysfs_attr_get_value (dev_interface->devpath,"bInterfaceSubClass" , + if_subclass,sizeof (if_subclass)); +if (!status) protocol =set_usb_mass_storage_ifsubtype (type_str, if_subclass,sizeof (type_str)-1 ); }else set_usb_iftype (type_str, if_class_num,sizeof (type_str)-1 );
@@ -269,7 +273,10 @@ static int usb_id(const char *devpath)/* mass storage */ if (protocol =6 && !use_usb_info) {struct sysfs_device *dev_scsi; -const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev; +char scsi_model[NAME_SIZE ]; +char scsi_vendor[NAME_SIZE ]; +char scsi_type[NAME_SIZE ]; +char scsi_rev[NAME_SIZE ];int host, bus, target, lun;/* get scsi device */
@@ -284,29 +291,33 @@ static int usb_id(const char *devpath) }/* Generic SPC-2 device */ - scsi_vendor =sysfs_attr_get_value (dev_scsi->devpath,"vendor" ); -if (!scsi_vendor) { + status =sysfs_attr_get_value (dev_scsi->devpath,"vendor" , + scsi_vendor,sizeof (scsi_vendor)); +if (!status) {info ("%s: cannot get SCSI vendor attribute\n" , dev_scsi->kernel); goto fallback; }set_str (vendor_str, scsi_vendor,sizeof (vendor_str)-1 ); - scsi_model =sysfs_attr_get_value (dev_scsi->devpath,"model" ); -if (!scsi_model) { + status =sysfs_attr_get_value (dev_scsi->devpath,"model" , + scsi_model,sizeof (scsi_model)); +if (!status) {info ("%s: cannot get SCSI model attribute\n" , dev_scsi->kernel); goto fallback; }set_str (model_str, scsi_model,sizeof (model_str)-1 ); - scsi_type =sysfs_attr_get_value (dev_scsi->devpath,"type" ); -if (!scsi_type) { + status =sysfs_attr_get_value (dev_scsi->devpath,"type" , + scsi_type,sizeof (scsi_type)); +if (!status) {info ("%s: cannot get SCSI type attribute\n" , dev_scsi->kernel); goto fallback; }set_scsi_type (type_str, scsi_type,sizeof (type_str)-1 ); - scsi_rev =sysfs_attr_get_value (dev_scsi->devpath,"rev" ); -if (!scsi_rev) { + status =sysfs_attr_get_value (dev_scsi->devpath,"rev" , + scsi_rev,sizeof (scsi_rev)); +if (!status) {info ("%s: cannot get SCSI revision attribute\n" , dev_scsi->kernel); goto fallback; }
@@ -322,15 +333,18 @@ static int usb_id(const char *devpath) fallback:/* fallback to USB vendor & device */ if (vendor_str[0 ] ='\0' ) { -const char *usb_vendor =NULL ; +char usb_vendor[NAME_SIZE ]; + status =0 ;if (!use_num_info) - usb_vendor =sysfs_attr_get_value (dev_usb->devpath,"manufacturer" ); + status =sysfs_attr_get_value (dev_usb->devpath,"manufacturer" , + usb_vendor,sizeof (usb_vendor)); -if (!usb_vendor) - usb_vendor =sysfs_attr_get_value (dev_usb->devpath,"idVendor" ); +if (!status) + status =sysfs_attr_get_value (dev_usb->devpath,"idVendor" , + usb_vendor,sizeof (usb_vendor)); -if (!usb_vendor) { +if (!status) {info ("No USB vendor information available\n" );return 1 ; }
@@ -338,15 +352,18 @@ fallback: }if (model_str[0 ] ='\0' ) { -const char *usb_model =NULL ; +char usb_model[NAME_SIZE ]; + status =0 ;if (!use_num_info) - usb_model =sysfs_attr_get_value (dev_usb->devpath,"product" ); + status =sysfs_attr_get_value (dev_usb->devpath,"product" , + usb_model,sizeof (usb_model)); -if (!usb_model) - usb_model =sysfs_attr_get_value (dev_usb->devpath,"idProduct" ); +if (!status) + status =sysfs_attr_get_value (dev_usb->devpath,"idProduct" , + usb_model,sizeof (usb_model)); -if (!usb_model) { +if (!status) {dbg ("No USB model information available\n" );return 1 ; }
@@ -354,18 +371,20 @@ fallback: }if (revision_str[0 ] ='\0' ) { -const char *usb_rev; +char usb_rev[NAME_SIZE ]; - usb_rev =sysfs_attr_get_value (dev_usb->devpath,"bcdDevice" ); -if (usb_rev) + status =sysfs_attr_get_value (dev_usb->devpath,"bcdDevice" , + usb_rev,sizeof (usb_rev)); +if (status)set_str (revision_str, usb_rev,sizeof (revision_str)-1 ); }if (serial_str[0 ] ='\0' ) { -const char *usb_serial; +char usb_serial[NAME_SIZE ]; - usb_serial =sysfs_attr_get_value (dev_usb->devpath,"serial" ); -if (usb_serial) + status =sysfs_attr_get_value (dev_usb->devpath,"serial" , + usb_serial,sizeof (usb_serial)); +if (status)set_str (serial_str, usb_serial,sizeof (serial_str)-1 ); }return 0 ;
udev/udev.h +1 -1
@@ -117,7 +117,7 @@ extern void sysfs_device_set_values(struct sysfs_device *dev, const char *devpatextern struct sysfs_device *sysfs_device_get (const char *devpath);extern struct sysfs_device *sysfs_device_get_parent (struct sysfs_device *dev);extern struct sysfs_device *sysfs_device_get_parent_with_subsystem (struct sysfs_device *dev,const char *subsystem); -extern char *sysfs_attr_get_value (const char *devpath,const char *attr_name); +extern int sysfs_attr_get_value (const char *devpath,const char *attr_name,char *value,size_t len);extern int sysfs_resolve_link (char *path,size_t size);extern int sysfs_lookup_devpath_by_subsys_id (char *devpath,size_t len,const char *subsystem,const char *id);
udev/udev_device.c +2 -3
@@ -71,12 +71,11 @@ void udev_device_cleanup(struct udevice *udev)dev_t udev_device_get_devt (struct udevice *udev) { -const char *attr; +char attr[NAME_SIZE ];unsigned int maj, min;/* read it from sysfs */ - attr =sysfs_attr_get_value (udev->dev->devpath,"dev" ); -if (attr !=NULL ) { +if (sysfs_attr_get_value (udev->dev->devpath,"dev" , attr,sizeof (attr))) {if (sscanf (attr,"%u:%u" , &maj, &min) =2 )return makedev (maj, min); }
udev/udev_node.c +2 -3
@@ -376,12 +376,11 @@ int udev_node_add(struct udevice *udev)/* create all_partitions if requested */ if (udev->partitions) {char partitionname[PATH_SIZE ]; -char *attr; +char attr[NAME_SIZE ];int range;/* take the maximum registered minor range */ - attr =sysfs_attr_get_value (udev->dev->devpath,"range" ); -if (attr !=NULL ) { +if (sysfs_attr_get_value (udev->dev->devpath,"range" , attr,sizeof (attr))) { range =atoi (attr);if (range >1 ) udev->partitions = range-1 ;
udev/udev_rules.c +27 -25
104 lines not on this page · raw message
udev/udev_sysfs.c +56 -47
166 lines not on this page · raw message
udev/udevinfo.c +2 -6
@@ -47,7 +47,6 @@ static void print_all_attributes(const char *devpath, const char *key)for (dent =readdir (dir); dent !=NULL ; dent =readdir (dir)) {struct stat statbuf;char filename[PATH_SIZE ]; -char *attr_value;char value[NAME_SIZE ];size_t len;
@@ -67,12 +66,9 @@ static void print_all_attributes(const char *devpath, const char *key)if (S_ISLNK (statbuf.st_mode))continue ; - attr_value =sysfs_attr_get_value (devpath, dent->d_name); -if (attr_value =NULL ) +if (!sysfs_attr_get_value (devpath, dent->d_name, value,sizeof (value)))continue ; - len =strlcpy (value, attr_value,sizeof (value)); -if (len >=sizeof (value)) - len =sizeof (value) -1 ; + len =strlen (value);dbg ("attr '%s'='%s'(%zi)\n" , dent->d_name, value, len);/* skip nonprintable attributes */
Re: [PATCH 2/3] sysfs: sysfs_attr_get_value() writes to a buffer
Quoted 4 lines · Alan Jenkins
This avoids lifetime issues with the returned string, for subsequent changes to caching in sysfs. All callers are changed accordingly. Many callers copied the result to a buffer already, so this actually simplifies some code.
Sorry, my re-implementation of sysfs_attr_get_value() was defective. It didn't return cached values correctly.
I suggest you drop these two patches. I'll fix the bug and update them to apply on top of the recent libudev/logging changes.
Thanks
Alan
Re: [PATCH 2/3] sysfs: sysfs_attr_get_value() writes to a buffer
In reply to Alan JenkinsQuoted 11 lines · Alan Jenkins
Quoted 4 lines · Alan Jenkins
This avoids lifetime issues with the returned string, for subsequent changes to caching in sysfs. All callers are changed accordingly. Many callers copied the result to a buffer already, so this actually simplifies some code.
Sorry, my re-implementation of
sysfs_attr_get_value()was defective. It didn't return cached values correctly.I suggest you drop these two patches. I'll fix the bug and update them to apply on top of the recent libudev/logging changes.
I didn't apply them, as I was busy with other stuff in libudev. We should move the "sysfs cache" to the lib, hanging off of the the udev_device, I guess. For this we will need to add parent device relations in the udev_device. The current global device and attribute cache will just go away then.
We will get there after the current work on the lib, which means converting more stuff to libudev, and use it in the udev code. We will also soon get rid of all dependencies on udev code in libudev, which will remove the current udev sysfs code from libudev.
Thanks,
Kay
Re: [PATCH 2/3] sysfs: sysfs_attr_get_value() writes to a buffer instead of returning a string
In reply to Alan JenkinsQuoted 19 lines · Kay Sievers
Quoted 11 lines · Alan Jenkins
Quoted 4 lines · Alan Jenkins
This avoids lifetime issues with the returned string, for subsequent changes to caching in sysfs. All callers are changed accordingly. Many callers copied the result to a buffer already, so this actually simplifies some code.
Sorry, my re-implementation of
sysfs_attr_get_value()was defective. It didn't return cached values correctly.I suggest you drop these two patches. I'll fix the bug and update them to apply on top of the recent libudev/logging changes.
I didn't apply them, as I was busy with other stuff in libudev. We should move the "sysfs cache" to the lib, hanging off of the the
udev_device, I guess. For this we will need to add parent device relations in theudev_device. The current global device and attribute cache will just go away then.
Quoted 4 lines
We will get there after the current work on the lib, which means converting more stuff to libudev, and use it in the udev code. We will also soon get rid of all dependencies on udev code in libudev, which will remove the current udev sysfs code from libudev.
We are getting closer. Current libudev does not use any main udev code anymore. Libudev's "udev_device" does parent-device and sysfs-attribute value "caching" now.
Kay