Fix fd leaks
When you open a file, you should remember to close it.
bug:2639464
Change-Id: I90d25dba2a262b620373270832eb3189616c720d
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;