gpio: unexport sysfs gpio on close

This commit is contained in:
Vanya A. Sergeev
2019-09-30 22:19:44 -05:00
parent 503c7e4e90
commit 79c3930cd5
+15
View File
@@ -197,6 +197,9 @@ static const char *gpio_sysfs_edge_to_string[] = {
};
static int gpio_sysfs_close(gpio_t *gpio) {
char buf[16];
int fd;
if (gpio->line_fd < 0)
return 0;
@@ -206,6 +209,18 @@ static int gpio_sysfs_close(gpio_t *gpio) {
gpio->line_fd = -1;
/* Unexport the GPIO */
snprintf(buf, sizeof(buf), "%d", gpio->line);
if ((fd = open("/sys/class/gpio/unexport", O_WRONLY)) < 0)
return _gpio_error(gpio, GPIO_ERROR_CLOSE, errno, "Closing GPIO: opening 'unexport'");
if (write(fd, buf, strlen(buf)+1) < 0) {
int errsv = errno;
close(fd);
return _gpio_error(gpio, GPIO_ERROR_CLOSE, errsv, "Closing GPIO: writing 'unexport'");
}
if (close(fd) < 0)
return _gpio_error(gpio, GPIO_ERROR_CLOSE, errno, "Closing GPIO: closing 'unexport'");
return 0;
}