manager: modem_reset: fix file descriptor leek

fd wasn't being closed if the write failed. Theoretically if a system
with an unstable modem ran for long enough, this could bring down
eg25-manager.
This commit is contained in:
Aren Moynihan
2024-10-04 11:21:25 -04:00
parent 1482899888
commit b53702ad0b

View File

@@ -226,24 +226,26 @@ gboolean modem_reset(struct EG25Manager *manager)
g_warning("Unable to open /sys/bus/usb/drivers/usb/unbind"); g_warning("Unable to open /sys/bus/usb/drivers/usb/unbind");
goto error; goto error;
} }
ret = write(fd, manager->modem_usb_id, len); ret = write(fd, manager->modem_usb_id, len);
close(fd);
if (ret < len) { if (ret < len) {
g_warning("Couldn't unbind modem: wrote %d/%d bytes", ret, len); g_warning("Couldn't unbind modem: wrote %d/%d bytes", ret, len);
goto error; goto error;
} }
close(fd);
fd = open("/sys/bus/usb/drivers/usb/bind", O_WRONLY); fd = open("/sys/bus/usb/drivers/usb/bind", O_WRONLY);
if (fd < 0) { if (fd < 0) {
g_warning("Unable to open /sys/bus/usb/drivers/usb/unbind"); g_warning("Unable to open /sys/bus/usb/drivers/usb/bind");
goto error; goto error;
} }
ret = write(fd, manager->modem_usb_id, len); ret = write(fd, manager->modem_usb_id, len);
close(fd);
if (ret < len) { if (ret < len) {
g_warning("Couldn't bind modem: wrote %d/%d bytes", ret, len); g_warning("Couldn't bind modem: wrote %d/%d bytes", ret, len);
goto error; goto error;
} }
close(fd);
g_message("Successfully reset modem's USB connection"); g_message("Successfully reset modem's USB connection");