linux-usb-gadgets / libusbgx

C library encapsulating the Linux kernel USB gadget configfs userspace API functionality
GNU General Public License v2.0
216 stars 72 forks source link

usbg_write_buf() can fail without reporting it #12

Closed notro closed 7 years ago

notro commented 7 years ago

I had a problem where usbg_enable_gadget() reported success, but it should have failed:

[869503.937381] configfs-gadget 20980000.usb: failed to start g1: -19

I haven't got time to make a PR, but the following worked for me, now returning USBG_ERROR_NO_DEV.

diff --git a/src/usbg_common.c b/src/usbg_common.c
index de9e40d..6321993 100644
--- a/src/usbg_common.c
+++ b/src/usbg_common.c
@@ -172,7 +172,9 @@ int usbg_write_buf(const char *path, const char *name,
                        ret = USBG_ERROR_IO;
        }

-       fclose(fp);
+       if (fclose(fp) == -1 && ret > 0) /* don't mask previous error */
+               ret = usbg_translate_error(errno);
+
 out:
        return ret;
 }
@@ -192,7 +194,7 @@ int usbg_write_int(const char *path, const char *name, const char *file,
        if (ret > 0)
                ret = 0;

-       return 0;
+       return ret;
 }

 int usbg_write_string(const char *path, const char *name,
@@ -204,7 +206,7 @@ int usbg_write_string(const char *path, const char *name,
        if (ret > 0)
                ret = 0;

-       return 0;
+       return ret;
 }

 int ubsg_rm_file(const char *path, const char *name)
kopasiak commented 7 years ago

Could you please check if #13 fixes the issue for you?

notro commented 7 years ago

Yes it worked, thanks.

kopasiak commented 7 years ago

ok. merged. Thank you for reporting this issue:)