Skip to contentSkip to conversation

Documentation/watchdog: Fix the file descriptor leak when no cmdline arg given

PATCH In review Devendra Naga 2 participants 3 messages May 17, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Documentation/watchdog: Fix the file descriptor leak when no cmdline arg given

we start a infinite loop when user gives ./watchdog-test, and when user ctrl + c's the program, we just exit immeadiately with out closing the filedescriptor of the watchdog device. a signal handler is used to do the job of closing the filedescriptor and exiting the program.

---
 Documentation/watchdog/src/watchdog-test.c |   11 +++++++++++
 1 file changed, 11 insertions(+)
Documentation/watchdog/src/watchdog-test.c +11 -0
index 23084f2..73ff5cc 100644
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <signal.h>
 #include <sys/ioctl.h>
 #include <linux/types.h>
 #include <linux/watchdog.h>
@@ -29,6 +30,14 @@ static void keep_alive(void)
  * The main program.  Run the program with "-d" to disable the card,
  * or "-e" to enable the card.
  */
+
+void term(int sig)
+{
+    close(fd);
+    fprintf(stderr, "Stopping watchdog ticks...\n");
+    exit(0);
+}
+
 int main(int argc, char *argv[])
 {
     int flags;
@@ -65,6 +74,8 @@ int main(int argc, char *argv[])
 	fflush(stderr);
     }
 
+    signal(SIGINT, term);
+
     while(1) {
 	keep_alive();
 	sleep(1);
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 4 lines · Devendra Naga

we start a infinite loop when user gives ./watchdog-test, and when user ctrl + c's the program, we just exit immeadiately with out closing the filedescriptor of the watchdog device. a signal handler is used to do the job of closing the filedescriptor and exiting the program.

Doesn't exiting a process close all its filehandles already?

Rob

Signature · 2 lines
GNU/Linux isn't: Linux=GPLv2, GNU=GPLv3+, they can't share code.
Either it's "mere aggregation", or a license violation.  Pick one.
#2 lore raw
Quoted 7 lines · Rob Landley
Quoted 4 lines · Devendra Naga

we start a infinite loop when user gives ./watchdog-test, and when user ctrl + c's the program, we just exit immeadiately with out closing the filedescriptor of the watchdog device. a signal handler is used to do the job of closing the filedescriptor and exiting the program.

Doesn't exiting a process close all its filehandles already?

I think kernel will take care of releasing process resources. i am not so sure of it.
but running valgrind --track-fds=yes ./a.out as root shows a descriptor leak.

BTW, there is another miss, if the user is non-root we still leak a file descriptor.

Quoted 5 lines

Rob
--
GNU/Linux isn't: Linux=GPLv2, GNU=GPLv3+, they can't share code. Either it's "mere aggregation", or a license violation.  Pick one.

Thanks,
Devendra.
--
To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html