From: Anthony Liguori <anthony@codemonkey.ws>
To: Jason Wessel <jason.wessel@windriver.com>
Cc: TJ <linux@tjworld.net>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] Re: [PATCH] Add USB sys file-system support (v6)
Date: Fri, 26 Sep 2008 10:37:58 -0500 [thread overview]
Message-ID: <48DD01D6.50909@codemonkey.ws> (raw)
In-Reply-To: <48DCFC18.5010506@windriver.com>
Jason Wessel wrote:
> Anthony Liguori wrote:
>
> My comment on technical content would be that the several places that
> atoi() and strtoul() the values are not checked for any kind of
> correctness and subsequently pass on. Perhaps no validity check is
> needed because it is dealt with in the later call to the passed in
> function... It just was not immediately obvious.
>
Okay, the patch needs some refactoring. I can commit and refactor or
someone else can refactor and resubmit. Either works for me. Anyway:
> +/*
> + * Use /sys/bus/usb/devices/ directory to determine host's USB
> + * devices.
> + *
> + * This code is taken from Robert Schiele's original patches posted to
> + * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
> + */
> +static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
> +{
> + FILE *f;
> + DIR *dir = 0;
> + char line[1024];
> + int bus_num, addr, speed, class_id, product_id, vendor_id;
> + int ret = 0;
> + char product_name[512];
> + struct dirent* de;
> +
> + dir = opendir(USBSYSBUS_PATH "/devices");
> + if (!dir) {
> + perror("husb: cannot open devices directory");
> + goto the_end;
> + }
> +
> + while ((de = readdir(dir))) {
> + if (de->d_name[0] != '.' && ! strchr(de->d_name, ':')) {
> + char filename[PATH_MAX];
> + char* tmpstr = de->d_name;
> + if (!strncmp(de->d_name, "usb", 3))
> + tmpstr += 3;
> +
> + bus_num = atoi(tmpstr);
> + snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/devnum", de->d_name);
> + f = fopen(filename, "r");
> + if (!f) {
> + term_printf("Could not open %s\n", filename);
> + goto the_end;
> + }
> + fgets(line, sizeof(line), f);
> + fclose(f);
> + addr = atoi(line);
>
This pattern, snprintf() of a filename, open, read from it, then parse
the output is very frequently repeated. It could be collapsed into:
if (usb_host_read_file(buffer, sizeof(buffer), USBSYSBUS_PATH
"/devices/%s/devnum", de->d_name))
goto the_end;
if (sscanf(buffer, "%d", &addr) != 1)
goto the_end;
And it would greatly simplify the code.
> +/*
> + * Determine how to access the host's USB devices and call the
> + * specific support function.
> + */
> +static int usb_host_scan(void *opaque, USBScanFunc *func)
> +{
> + FILE *f = 0;
> + DIR *dir = 0;
> + int ret = 0;
> + const char *devices = "/devices";
> + const char *trying = "husb: trying to open %s%s\n";
> + const char *failed = "husb: could not open %s%s\n";
> + char devpath[PATH_MAX];
> +
> + /* only check the host once */
> + if (!usb_fs_type) {
> + /* test for dev file-system access in /proc/ */
> + dprintf(trying, USBPROCBUS_PATH, devices);
> + f = fopen(USBPROCBUS_PATH "/devices", "r");
> + if (!f) {
>
We really want an affirmative test here, that goto's the switch
statement instead of nesting deeply.
With those changes, the patch is ready to apply.
Regards,
Anthony Liguori
next prev parent reply other threads:[~2008-09-26 15:39 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-04 23:35 [Qemu-devel] [PATCH] Add USB sys file-system support TJ
2008-09-05 2:06 ` [Qemu-devel] [PATCH] Add USB sys file-system support (v2) TJ
2008-09-05 13:30 ` Jason Wessel
2008-09-05 18:51 ` TJ
2008-09-05 19:19 ` Jason Wessel
2008-09-05 20:28 ` TJ
2008-09-05 20:54 ` Jason Wessel
2008-09-05 21:13 ` [Qemu-devel] [PATCH] Add USB sys file-system support (v3) TJ
2008-09-08 14:47 ` [Qemu-devel] " Jason Wessel
2008-09-17 19:31 ` [Qemu-devel] [PATCH] Add USB sys file-system support (v4) TJ
2008-09-17 20:31 ` [Qemu-devel] " Anthony Liguori
2008-09-17 22:47 ` [Qemu-devel] [PATCH] Add USB sys file-system support (v5) TJ
2008-09-22 19:47 ` Rick Vernam
2008-09-22 22:38 ` Anthony Liguori
2008-09-25 17:34 ` Rick Vernam
2008-09-25 17:45 ` Anthony Liguori
2008-09-22 22:37 ` [Qemu-devel] " Anthony Liguori
2008-09-23 1:23 ` TJ
2008-09-23 1:33 ` [Qemu-devel] [PATCH] Add USB sys file-system support (v6) TJ
2008-09-25 17:45 ` [Qemu-devel] " Anthony Liguori
2008-09-26 13:57 ` Jason Wessel
2008-09-26 14:27 ` Anthony Liguori
2008-09-26 14:35 ` Robert Riebisch
2008-09-26 14:37 ` Anthony Liguori
2008-09-26 15:13 ` Jason Wessel
2008-09-26 15:37 ` Anthony Liguori [this message]
2008-09-30 20:53 ` TJ
2008-10-01 21:21 ` [Qemu-devel] [PATCH] Add USB sys file-system support (v7) TJ
2008-10-01 23:19 ` [Qemu-devel] [PATCH] Add USB sys file-system support (v8) TJ
2008-10-06 16:03 ` Rick Vernam
2008-10-07 8:46 ` TJ
2008-10-07 20:09 ` [Qemu-devel] " Anthony Liguori
2008-09-05 19:20 ` [Qemu-devel] [PATCH] Add USB sys file-system support (v2) TJ
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=48DD01D6.50909@codemonkey.ws \
--to=anthony@codemonkey.ws \
--cc=jason.wessel@windriver.com \
--cc=linux@tjworld.net \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).