|
From: | Joshua Neuheisel |
Subject: | [RP] Close-on-exec bug patch |
Date: | Thu Nov 6 10:04:18 2003 |
I noticed a subtle problem the other day: all my
processes had an open file descriptor to ~/.ratpoisonrc. I traced the
problem to a bug in ratpoison. RP opens files in two cases: when reading
in the rc file, and when performing a 'source' command. In either case,
when an 'exec' command is found in the file, it's executed immediately without
waiting to close the file descriptor. As a result, the file
descriptor gets passed to the child process.
In my case, I have an 'exec xterm' line in my
~/.ratpoisonrc file. As such, the xterm would inherit the file descriptor,
as would the shell and any other process started from that shell. It
didn't take too long to have a couple dozen open file descriptors to
it.
There is an easy fix. RP should set the
close-on-exec flag on the open file descriptor immediately after opening
it.
Joshua
Index:
src/actions.c
=================================================================== RCS file: /cvsroot/ratpoison/ratpoison/src/actions.c,v retrieving revision 1.184 diff -u -r1.184 actions.c --- src/actions.c 4 Sep 2003 09:55:08 -0000 1.184 +++ src/actions.c 6 Nov 2003 16:32:34 -0000 @@ -736,6 +736,9 @@ marked_message_printf (0, 0, " source: %s : %s ", data, strerror(errno)); else { + int flags = fcntl (fileno (fileptr), F_GETFD); + if (flags >= 0) + fcntl (fileno (fileptr), F_SETFD, flags | FD_CLOEXEC); read_rc_file (fileptr); fclose (fileptr); } Index: src/main.c =================================================================== RCS file: /cvsroot/ratpoison/ratpoison/src/main.c,v retrieving revision 1.91 diff -u -r1.91 main.c --- src/main.c 3 Nov 2003 03:17:59 -0000 1.91 +++ src/main.c 6 Nov 2003 16:32:34 -0000 @@ -384,6 +384,9 @@ if (fileptr) { + int flags = fcntl (fileno (fileptr), F_GETFD); + if (flags >= 0) + fcntl (fileno (fileptr), F_SETFD, flags | FD_CLOEXEC); read_rc_file (fileptr); fclose (fileptr); } Index: src/ratpoison.h =================================================================== RCS file: /cvsroot/ratpoison/ratpoison/src/ratpoison.h,v retrieving revision 1.25 diff -u -r1.25 ratpoison.h --- src/ratpoison.h 3 Nov 2003 03:17:59 -0000 1.25 +++ src/ratpoison.h 6 Nov 2003 16:32:34 -0000 @@ -30,6 +30,12 @@ #include <stdio.h> #include <stdarg.h> #include <X11/Xlib.h> +#include <fcntl.h> + +/* Some systems don't define the close-on-exec flag in fcntl.h */ +#ifndef FD_CLOEXEC +# define FD_CLOEXEC 1 +#endif /* Helper macro for error and debug reporting. */ #define PRINT_LINE(type) printf (PACKAGE ":%s:%d: %s: ",__FILE__, __LINE__, #type) |
[Prev in Thread] | Current Thread | [Next in Thread] |