Fix fd leaks
When you open a file, you should remember to close it.
bug:2639464
Change-Id: I90d25dba2a262b620373270832eb3189616c720d
diff --git a/TetherController.cpp b/TetherController.cpp
index 30ff165..2fc2138 100644
--- a/TetherController.cpp
+++ b/TetherController.cpp
@@ -61,6 +61,7 @@
if (write(fd, (enable ? "1" : "0"), 1) != 1) {
LOGE("Failed to write ip_forward (%s)", strerror(errno));
+ close(fd);
return -1;
}
close(fd);
@@ -78,11 +79,11 @@
char enabled;
if (read(fd, &enabled, 1) != 1) {
LOGE("Failed to read ip_forward (%s)", strerror(errno));
+ close(fd);
return -1;
}
close(fd);
-
return (enabled == '1' ? true : false);
}
diff --git a/UsbController.cpp b/UsbController.cpp
index 0c1fb58..1def0e6 100644
--- a/UsbController.cpp
+++ b/UsbController.cpp
@@ -49,6 +49,7 @@
int fd = open("/sys/class/usb_composite/rndis/enable", O_RDWR);
int count = snprintf(value, sizeof(value), "%d\n", (enable ? 1 : 0));
write(fd, value, count);
+ close(fd);
return 0;
}
@@ -56,5 +57,6 @@
char value=0;
int fd = open("/sys/class/usb_composite/rndis/enable", O_RDWR);
read(fd, &value, 1);
+ close(fd);
return (value == '1' ? true : false);
}
diff --git a/logwrapper.c b/logwrapper.c
index e42d00a..96d9431 100644
--- a/logwrapper.c
+++ b/logwrapper.c
@@ -120,21 +120,23 @@
if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
+ close(parent_ptty);
LOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx");
return -1;
}
pid = fork();
if (pid < 0) {
+ close(parent_ptty);
LOG(LOG_ERROR, "logwrapper", "Failed to fork");
return -errno;
} else if (pid == 0) {
child_ptty = open(child_devname, O_RDWR);
if (child_ptty < 0) {
+ close(parent_ptty);
LOG(LOG_ERROR, "logwrapper", "Problem with child ptty");
return -errno;
}
-
// redirect stdout and stderr
close(parent_ptty);
dup2(child_ptty, 1);
@@ -159,10 +161,11 @@
"Unable to background process (%s)", strerror(errno));
}
}
-
child(argc, argv);
} else {
- return parent(argv[0], parent_ptty);
+ int retValue = parent(argv[0], parent_ptty);
+ close(parent_ptty);
+ return retValue;
}
return 0;