linux-raid.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: NeilBrown <neilb@suse.de>
To: Maciej Patelczyk <maciej.patelczyk@intel.com>
Cc: linux-raid@vger.kernel.org
Subject: Re: [PATCH] mdadm: close parent file descriptors when starting mdmon.
Date: Tue, 30 Aug 2011 09:31:57 +1000	[thread overview]
Message-ID: <20110830093157.1ae34f8c@notabene.brown> (raw)
In-Reply-To: <20110829154949.15993.32383.stgit@gklab-128-044.igk.intel.com>

On Mon, 29 Aug 2011 17:49:49 +0200 Maciej Patelczyk
<maciej.patelczyk@intel.com> wrote:

> When mdadm is invoked by fork-and-exec it inherits all open file
> descriptors and when mdadm forks to exec mdmon those file descriptors
> are passed to mdmon. Mdmon closes only first 97 fd and that in some
> cases is not enough.

Can you describe and actual can when it is not enough?  Maybe there is some
other problem where mdadm is not closing things as it should.

> This commit adds function which looks at the '/proc/<pid>/fd' directory
> and closes all inherited file descriptors except the standard ones (0-2).

I'm not thrilled by this approach.
For a start, the loop could close the fd that is being used to
read /proc/<pid>/fd, so subsequent fds won't get seen or closed.

I would much rather do as the comment suggests and use O_CLOEXEC on all opens
so that everything gets closed when we do an exec.

About the most I  would be willing to do in closing more fds before the exec
is to keep closing until we get too many failures.
e.g.
  fd = 3;
  failed = 0;
  while (failed < 20) {
      if (close(fd) < 0)
          failed ++;
      else
          failed = 0;
      fd++;
  }

NeilBrown


> 
> Signed-off-by: Maciej Patelczyk <maciej.patelczyk@intel.com>
> ---
>  util.c |   41 +++++++++++++++++++++++++++++++++++++----
>  1 files changed, 37 insertions(+), 4 deletions(-)
> 
> diff --git a/util.c b/util.c
> index ce03239..b844447 100644
> --- a/util.c
> +++ b/util.c
> @@ -30,7 +30,12 @@
>  #include	<sys/un.h>
>  #include	<ctype.h>
>  #include	<dirent.h>
> +#include	<sys/types.h>
>  #include	<signal.h>
> +#include	<stdlib.h>
> +#include	<string.h>
> +#include	<errno.h>
> +#include	<unistd.h>
>  
>  /*
>   * following taken from linux/blkpg.h because they aren't
> @@ -1571,11 +1576,41 @@ int mdmon_running(int devnum)
>  	return 0;
>  }
>  
> +static void close_parent_fds(void)
> +{
> +	pid_t pid;
> +	char buf[128];
> +	DIR *dirp;
> +	struct dirent *d_entry;
> +	int fd;
> +
> +	pid = getpid();
> +	if (snprintf(buf, sizeof(buf), "/proc/%d/fd", (int)pid) < 0)
> +		return;
> +
> +	dirp = opendir((const char *)buf);
> +	if (!dirp)
> +		return;
> +
> +	while((d_entry = readdir(dirp)) != NULL) {
> +		if (!strcmp(d_entry->d_name, ".") ||
> +		    !strcmp(d_entry->d_name, ".."))
> +			continue;
> +		errno = 0;
> +		fd = (int)strtol(d_entry->d_name, NULL, 10);
> +		if (errno)
> +			continue;
> +		if (fd > 2)
> +			close(fd);
> +	}
> +	closedir(dirp);
> +}
> +
>  int start_mdmon(int devnum)
>  {
>  	int i;
>  	int len;
> -	pid_t pid;	
> +	pid_t pid;
>  	int status;
>  	char pathbuf[1024];
>  	char *paths[4] = {
> @@ -1603,9 +1638,7 @@ int start_mdmon(int devnum)
>  
>  	switch(fork()) {
>  	case 0:
> -		/* FIXME yuk. CLOSE_EXEC?? */
> -		for (i=3; i < 100; i++)
> -			close(i);
> +		close_parent_fds();
>  		for (i=0; paths[i]; i++)
>  			if (paths[i][0])
>  				execl(paths[i], "mdmon",


  reply	other threads:[~2011-08-29 23:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-29 15:49 [PATCH] mdadm: close parent file descriptors when starting mdmon Maciej Patelczyk
2011-08-29 23:31 ` NeilBrown [this message]
2011-08-30 13:30   ` Patelczyk, Maciej
2011-09-07  2:55     ` NeilBrown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110830093157.1ae34f8c@notabene.brown \
    --to=neilb@suse.de \
    --cc=linux-raid@vger.kernel.org \
    --cc=maciej.patelczyk@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).