@@ -75,49 +75,57 @@ int epfd = -1;
7575
7676BBIO_err gpio_export (unsigned int gpio )
7777{
78- int fd , len ;
78+ int fd = 0 , len = 0 , ret = BBIO_GEN ;
7979 char str_gpio [10 ];
8080
8181 // already exported by us?
8282 if (exported_gpios [gpio ] != GPIO_NOT_EXPORTED ) {
8383 syslog (LOG_DEBUG , "gpio_export: %u already exported" , gpio );
84- return BBIO_OK ;
84+ ret = BBIO_OK ;
85+ goto exit ;
8586 }
8687
8788 // already exported by someone else?
8889 char gpio_path [64 ];
8990 snprintf (gpio_path , sizeof (gpio_path ), "/sys/class/gpio/gpio%d" , gpio );
9091
91- if (access (gpio_path , R_OK |W_OK |X_OK ) != -1 )
92- {
92+ if (access (gpio_path , R_OK |W_OK |X_OK ) != -1 ) {
9393 exported_gpios [gpio ] = GPIO_ALREADY_EXPORTED ;
9494 syslog (LOG_DEBUG , "gpio_export: %u already exported" , gpio );
95- return BBIO_OK ;
95+ ret = BBIO_OK ;
96+ goto exit ;
9697 }
9798
98- #define GPIO_EXPORT "/sys/class/gpio/export"
99+ const char gpio_export [] = "/sys/class/gpio/export" ;
99100
100- if ((fd = open (GPIO_EXPORT , O_WRONLY )) < 0 )
101- {
102- syslog ( LOG_ERR , "gpio_export: %u couldn't open '" GPIO_EXPORT "': %i-%s" ,
103- gpio , errno , strerror ( errno )) ;
104- return BBIO_SYSFS ;
101+ if ((fd = open (gpio_export , O_WRONLY )) < 0 ) {
102+ syslog ( LOG_ERR , "gpio_export: %u couldn't open \"%s\": %i-%s" ,
103+ gpio , gpio_export , errno , strerror ( errno ));
104+ ret = BBIO_SYSFS ;
105+ goto exit ;
105106 }
106107
107108 len = snprintf (str_gpio , sizeof (str_gpio ), "%d" , gpio );
108- int ret = write (fd , str_gpio , len );
109- close (fd );
110- if (ret < 0 ) {
111- syslog (LOG_ERR , "gpio_export: %u couldn't write '" GPIO_EXPORT "': %i-%s" ,
112- gpio , errno , strerror (errno ));
113- return BBIO_SYSFS ;
109+ if (write (fd , str_gpio , len ) < 0 ) {
110+ syslog (LOG_ERR , "gpio_export: %u couldn't write \"%s\": %i-%s" ,
111+ gpio , gpio_export , errno , strerror (errno ));
112+ ret = BBIO_SYSFS ;
113+ goto exit ;
114114 }
115115
116116 // add to list
117117 exported_gpios [gpio ] = GPIO_EXPORTED ;
118118
119119 syslog (LOG_DEBUG , "gpio_export: %u OK" , gpio );
120- return BBIO_OK ;
120+ ret = BBIO_OK ;
121+
122+ exit :
123+ if (fd && (ret = close (fd ))) {
124+ syslog (LOG_ERR , "gpio_export: %u couldn't close \"%s\": %i-%s" ,
125+ gpio , gpio_export , errno , strerror (errno ));
126+ ret = BBIO_SYSFS ;
127+ }
128+ return ret ;
121129}
122130
123131void close_value_fd (unsigned int gpio )
0 commit comments