public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Valentina Manea <valentina.manea.m@gmail.com>
Cc: gregkh@linuxfoundation.org, devel@driverdev.osuosl.org,
	shuah.kh@samsung.com, linux-usb@vger.kernel.org,
	ly80toro@cip.cs.fau.de, tobias.polzer@fau.de,
	linux-kernel@vger.kernel.org, firefly@lists.rosedu.org,
	dominik.paulus@fau.de, ihadzic@research.bell-labs.com
Subject: Re: [PATCH 01/12] staging: usbip: userspace: migrate usbip_bind to libudev
Date: Wed, 5 Mar 2014 12:56:19 +0300	[thread overview]
Message-ID: <20140305095619.GY26722@mwanda> (raw)
In-Reply-To: <1393960252-21247-2-git-send-email-valentina.manea.m@gmail.com>

On Tue, Mar 04, 2014 at 09:10:41PM +0200, Valentina Manea wrote:

> +int write_sysfs_attribute(const char *attr_path, const char *new_value,
> +			  size_t len)
> +{
> +	int fd;
> +	int length;
> +
> +	if (attr_path == NULL || new_value == NULL || len == 0) {

Can any of these conditions be true?


> +		dbg("Invalid values provided for attribute %s.", attr_path);
> +		errno = EINVAL;
> +		return -1;
> +	}
> +
> +	if ((fd = open(attr_path, O_WRONLY)) < 0) {

Move the assignment out of the condition.

> +		dbg("Error opening attribute %s.", attr_path);
> +		return -1;
> +	}
> +
> +	length = write(fd, new_value, len);
> +	if (length < 0) {
> +		dbg("Error writing to attribute %s.", attr_path);
> +		close(fd);
> +		return -1;
> +	}
> +
> +	close(fd);
> +
> +	return 0;
> +}
> diff --git a/drivers/staging/usbip/userspace/src/sysfs_utils.h b/drivers/staging/usbip/userspace/src/sysfs_utils.h
> new file mode 100644
> index 0000000..32ac1d1
> --- /dev/null
> +++ b/drivers/staging/usbip/userspace/src/sysfs_utils.h
> @@ -0,0 +1,8 @@
> +
> +#ifndef __SYSFS_UTILS_H
> +#define __SYSFS_UTILS_H
> +
> +int write_sysfs_attribute(const char *attr_path, const char *new_value,
> +			  size_t len);
> +
> +#endif
> diff --git a/drivers/staging/usbip/userspace/src/usbip_bind.c b/drivers/staging/usbip/userspace/src/usbip_bind.c
> index 8cfd2db..d122089 100644
> --- a/drivers/staging/usbip/userspace/src/usbip_bind.c
> +++ b/drivers/staging/usbip/userspace/src/usbip_bind.c
> @@ -16,7 +16,7 @@
>   * along with this program. If not, see <http://www.gnu.org/licenses/>.
>   */
>  
> -#include <sysfs/libsysfs.h>
> +#include <libudev.h>
>  
>  #include <errno.h>
>  #include <stdio.h>
> @@ -28,6 +28,7 @@
>  #include "usbip_common.h"
>  #include "utils.h"
>  #include "usbip.h"
> +#include "sysfs_utils.h"
>  
>  enum unbind_status {
>  	UNBIND_ST_OK,
> @@ -48,135 +49,94 @@ void usbip_bind_usage(void)
>  /* call at unbound state */
>  static int bind_usbip(char *busid)
>  {
> -	char bus_type[] = "usb";

Ok.  This code was already really stupid before this patch but now it's
even more idiotic.  Just put these things things directly into the
string instead of abstracting the abastractions of the abstract
abstractions.  Sheesh.

>  	char attr_name[] = "bind";
> -	char sysfs_mntpath[SYSFS_PATH_MAX];
>  	char bind_attr_path[SYSFS_PATH_MAX];
> -	struct sysfs_attribute *bind_attr;
> -	int failed = 0;
> -	int rc, ret = -1;
> -
> -	rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
> -	if (rc < 0) {
> -		err("sysfs must be mounted: %s", strerror(errno));
> -		return -1;
> -	}
> +	int rc = -1;

Don't do pointless assignments.  The compiler has builtin checks for
uninitialized variables but now you are turning the checks off and the
compiler can't do its job.

>  
>  	snprintf(bind_attr_path, sizeof(bind_attr_path), "%s/%s/%s/%s/%s/%s",
> -		 sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
> -		 USBIP_HOST_DRV_NAME, attr_name);
> +		 SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE,
> +		 SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME, attr_name);
> +	dbg("bind attribute path: %s", bind_attr_path);

These dbg statements are totally pointless.  Just use strace.

>  
> -	bind_attr = sysfs_open_attribute(bind_attr_path);
> -	if (!bind_attr) {
> -		dbg("problem getting bind attribute: %s", strerror(errno));
> -		return -1;
> -	}
> -
> -	rc = sysfs_write_attribute(bind_attr, busid, SYSFS_BUS_ID_SIZE);
> +	rc = write_sysfs_attribute(bind_attr_path, busid, strlen(busid));
>  	if (rc < 0) {
> -		dbg("bind driver at %s failed", busid);
> -		failed = 1;
> +		dbg("Error binding device %s to driver: %s", busid,
> +		    strerror(errno));

The temptation here would be to change the debug statement to an on by
default print statement.  The other option would be to just remove all
the debug statements.

Etc...  For the rest of this patch.  Remove abstraction levels and dead
code.

regards,
dan carpenter


  parent reply	other threads:[~2014-03-05  9:57 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-04 19:10 [PATCH 00/12] Migrate usbip-utils to libudev Valentina Manea
2014-03-04 19:10 ` [PATCH 01/12] staging: usbip: userspace: migrate usbip_bind " Valentina Manea
2014-03-05  9:42   ` Dan Carpenter
2014-03-05 10:15     ` Dan Carpenter
2014-03-06  6:17       ` Valentina Manea
2014-03-05  9:56   ` Dan Carpenter [this message]
2014-03-06 16:15   ` Shuah Khan
2014-03-06 18:19     ` Dan Carpenter
2014-03-04 19:10 ` [PATCH 02/12] staging: usbip: userspace: remove useless libsysfs includes Valentina Manea
2014-03-04 19:10 ` [PATCH 03/12] staging: usbip: userspace: migrate usbip_unbind to libudev Valentina Manea
2014-03-06 16:31   ` Shuah Khan
2014-03-04 19:10 ` [PATCH 04/12] staging: usbip: userspace: migrate usbip_list " Valentina Manea
2014-03-06 20:57   ` Shuah Khan
2014-03-04 19:10 ` [PATCH 05/12] staging: usbip: userspace: re-add interface information listing Valentina Manea
2014-03-06 21:11   ` Shuah Khan
2014-03-04 19:10 ` [PATCH 06/12] staging: usbip: userspace: add new list API Valentina Manea
2014-03-05  6:35   ` Greg KH
2014-03-05 10:16     ` Dan Carpenter
     [not found]       ` <CAByK=5bzS4R0sGj5w5x8gc8DcKXvnr58WskoShuq6G30YTsOgQ@mail.gmail.com>
2014-03-06 19:55         ` [firefly] " Greg KH
2014-03-04 19:10 ` [PATCH 07/12] staging: usbip: userspace: move sysfs_utils to libsrc Valentina Manea
2014-03-04 19:10 ` [PATCH 08/12] staging: usbip: userspace: migrate usbip_host_driver to libudev Valentina Manea
2014-03-06 21:43   ` Shuah Khan
2014-03-04 19:10 ` [PATCH 09/12] staging: usbip: userspace: remove class device infrastructure in vhci_driver Valentina Manea
2014-03-06 21:59   ` Shuah Khan
2014-03-04 19:10 ` [PATCH 10/12] staging: usbip: userspace: migrate vhci_driver to libudev Valentina Manea
2014-03-06 22:07   ` Shuah Khan
2014-03-04 19:10 ` [PATCH 11/12] staging: usbip: userspace: remove libsysfs flag and autoconf check Valentina Manea
2014-03-04 19:10 ` [PATCH 12/12] staging: usbip: userspace: update dependencies in README Valentina Manea
2014-03-05  6:37 ` [PATCH 00/12] Migrate usbip-utils to libudev Greg KH

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=20140305095619.GY26722@mwanda \
    --to=dan.carpenter@oracle.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=dominik.paulus@fau.de \
    --cc=firefly@lists.rosedu.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=ihadzic@research.bell-labs.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=ly80toro@cip.cs.fau.de \
    --cc=shuah.kh@samsung.com \
    --cc=tobias.polzer@fau.de \
    --cc=valentina.manea.m@gmail.com \
    /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