delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2012/02/16/09:11:50

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
Date: Thu, 16 Feb 2012 15:09:32 +0100
From: Corinna Vinschen <corinna-cygwin AT cygwin DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: STC for libapr1 failure
Message-ID: <20120216140932.GI19092@calimero.vinschen.de>
Reply-To: cygwin AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
References: <20120214144551 DOT GC25918 AT calimero DOT vinschen DOT de> <4F3AA0BB DOT 7000806 AT acm DOT org> <20120214182452 DOT GK25918 AT calimero DOT vinschen DOT de> <4F3AD58A DOT 9040106 AT acm DOT org> <20120215153851 DOT GQ25918 AT calimero DOT vinschen DOT de> <4F3C09D9 DOT 6000406 AT acm DOT org> <20120215204521 DOT GB27454 AT calimero DOT vinschen DOT de> <4F3C208B DOT 2060007 AT acm DOT org> <20120215212010 DOT GA4183 AT calimero DOT vinschen DOT de> <4F3C2E35 DOT 3080308 AT acm DOT org>
MIME-Version: 1.0
In-Reply-To: <4F3C2E35.3080308@acm.org>
User-Agent: Mutt/1.5.21 (2010-09-15)
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

--6c2NcOVqGQ03X4Wi
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline

On Feb 15 14:14, David Rothenberger wrote:
> On 2/15/2012 1:20 PM, Corinna Vinschen wrote:
> > On Feb 15 13:15, David Rothenberger wrote:
> >> On 2/15/2012 12:45 PM, Corinna Vinschen wrote:
> >>> On Feb 15 11:39, David Rothenberger wrote:
> >>>> But... now one of the flock tests is failing. It takes a while to
> >>>> extract a STC from the APR test suite because everything is written in
> >>>> APR-ese and I have to convert every APR call into the base C library
> >>>> calls. I'll work on that over the next day or three.
> >>>>
> >>>> The gist of the test that's failing is this:
> >>>>
> >>>>  * Create a file.
> >>>>  * Get an exclusive flock on it.
> >>>>  * Spawn a child process that attempts to get an exclusive, non-blocking
> >>>>    lock on the file.
> >>>>
> >>>> The test is expecting that the child will not be able to get the lock,
> >>>> but the child is able to.
> >>>[...]
> >>> Does it fork/exec or does it only exec? 
> >>
> >> Looks like fork/exec. execv to be precise.
> >>
> >>> I guess I really need the testcase.
> >> [...]

I read the Linux man page again (http://linux.die.net/man/2/flock)
and I just hacked the following testcase, based on your flock STC.

It creates a lock in the parent, then forks a child.  The child tries to
grab the lock, first using the inherited file descriptor.  This is
supposed to work.  Then it opens the file again and tries to lock the
file using that descriptor.  This is supposed to fail with EWOULDBLOCK.
If it failed to lock the file one way or the other, it tries to unlock
the file using the second descriptor.  In theory this should fail.  If
it doesn't fail, it tries to lock the file again using both descriptors.
The expected result is the same as in the first two tries.  Eventually
the child exec's, and runs the entire set of tests again.  The result
should be the same as for the forked child.

I tried this test on both, Linux and Cygwin (latest from CVS), and it
behaves identically:

Linux$ ./stc-flock-forkexec
funlock from forked child with new descriptor succeeded but shouldn't
funlock from execed child with new descriptor succeeded but shouldn't

Cygwin$ ./stc-flock-forkexec
funlock from forked child with new descriptor succeeded but shouldn't
funlock from execed child with new descriptor succeeded but shouldn't

Funny enough, unlocking always returns success on the descriptor not
holding the lock, even on Linux.  But the second set of tests shows that
the lock is still firm in the hands of the first descriptor.

The testcase is attached.  I'm pretty curious what your test is actually
testing.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--6c2NcOVqGQ03X4Wi
Content-Type: text/plain; charset=utf-8
Content-Disposition: attachment; filename="stc-flock-forkexec.c"

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

/* A temporary file used for flock. */
char tmpfilename[] = "/tmp/flocktstXXXXXX";

void
test_child (int fd, const char *type, const char *fname)
{
  int rc;

  /* First try to lock using fd. */
  do
    {
      rc = flock (fd, LOCK_EX | LOCK_NB);
    }
  while (rc < 0 && errno == EINTR);
  if (rc < 0)
    fprintf (stderr, "flock from %s child with same descriptor: %s\n", type, strerror (errno));

  int fd2 = open (fname, O_RDONLY);
  if (fd2 < 0)
    perror ("child open");
  else
    {
      /* Try another descriptor. */
      do
	{
	  rc = flock (fd2, LOCK_EX | LOCK_NB);
	}
      while (rc < 0 && errno == EINTR);
      if (rc == 0)
	fprintf (stderr, "flock from %s child with new descriptor succeeded but shouldn't\n", type);
      else if (errno != EWOULDBLOCK)
	  fprintf (stderr, "flock from %s child with new descriptor: %s\n", type, strerror (errno));
      if (rc < 0)
	{
	  do
	    {
	      rc = flock (fd2, LOCK_UN);
	    }
	  while (rc < 0 && errno == EINTR);
	  if (rc == 0)
	    {
	      fprintf (stderr, "funlock from %s child with new descriptor succeeded but shouldn't\n", type);
	      do
		{
		  rc = flock (fd2, LOCK_EX | LOCK_NB);
		}
	      while (rc < 0 && errno == EINTR);
	      if (rc == 0)
		fprintf (stderr, "flock from %s child with new descriptor succeeded but shouldn't\n", type);
	      else if (errno != EWOULDBLOCK)
		  fprintf (stderr, "flock from %s child with new descriptor: %s\n", type, strerror (errno));
	      do
		{
		  rc = flock (fd, LOCK_EX | LOCK_NB);
		}
	      while (rc < 0 && errno == EINTR);
	      if (rc < 0)
		fprintf (stderr, "flock from %s child with same descriptor: %s\n", type, strerror (errno));
	    }
	}
      close (fd2);
    }
}

/* Fork and use flock to lock and unlock the file repeatedly in the child. */
void
make_child (int fd, pid_t * pid)
{
  if ((*pid = fork ()) < 0)
    {
      perror ("fork failed");
      exit (1);
    }
  else if (*pid == 0)
    {
      char buf[32];

      test_child (fd, "forked", tmpfilename);
      snprintf (buf, 32, "%d", fd);
      execl (program_invocation_name, program_invocation_name, buf, tmpfilename, NULL);
      perror ("execl");
      exit (1);
    }
}

/* Wait for the child to finish. */
void
await_child (pid_t pid)
{
  pid_t pstatus;
  int exit_int;

  do
    {
      pstatus = waitpid (pid, &exit_int, WUNTRACED);
    }
  while (pstatus < 0 && errno == EINTR);
}

int
main (int argc, const char *const *argv)
{
  pid_t child;
  int rc;
  int fd;

  if (argc > 1)
    {
      test_child (atoi (argv[1]), "execed", argv[2]);
      exit (0);
    }

  /* Create the temporary file. */
  fd = mkstemp (tmpfilename);
  if (fd < 0)
    {
      perror ("open failed");
      exit (1);
    }
  do
    {
      rc = flock (fd, LOCK_EX);
    }
  while (rc < 0 && errno == EINTR);
  if (rc < 0)
    {
      perror ("lock");
      exit (1);
    }

  make_child (fd, &child);

  await_child (child);

  close (fd);

  /* Clean up. */
  unlink (tmpfilename);

  return 0;
}


--6c2NcOVqGQ03X4Wi
Content-Type: text/plain; charset=us-ascii

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
--6c2NcOVqGQ03X4Wi--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019