delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2014/02/09/13:26:02

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:reply-to:from:to:subject:date:message-id
:mime-version:content-type:content-transfer-encoding; q=dns; s=
default; b=eM+vOdw1puEWFEHj9MgMcjbiqLa2n18kV/qBfALh2Dej9NbhondbW
a/kAkbUUEwi0KFtwKtxzwynVKavHli828Z3IXJSdF5SYCkDc//zlBMsHUIpeuAOr
OidKzGGZXDUQTpGO9E4zfS7GfpbyBngdmFQs03ZtQa7R9mpciVJVGg=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:reply-to:from:to:subject:date:message-id
:mime-version:content-type:content-transfer-encoding; s=default;
bh=H/4Q/NkXaKL6VgCkGHE9K+JSRO8=; b=iT/cRUOnSx13qvTsT9Pioq/sK7mB
tURVurYuQ2Ous67VxT5AAD2ymp1AGDthDh0uU7KvpkpqQWo6UvKha9OPgDEf9i4+
P2TN4NKWHLrNhCXakP4yxMarUL5zIyTJ8+lE31/qNr92EyEhGSDgJ35q89xTo+Xd
0Lp5K1YSU701mT0=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.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
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=0.3 required=5.0 tests=AWL,BAYES_05,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2
X-HELO: p3plsmtpa11-03.prod.phx3.secureserver.net
Reply-To: <sbardwell AT lbmsys DOT com>
From: "Steven Bardwell" <SBardwell AT lbmsys DOT com>
To: <cygwin AT cygwin DOT com>
Subject: spawnv() unlocks files in the calling program
Date: Sun, 9 Feb 2014 13:25:30 -0500
Message-ID: <040201cf25c4$54e9cd20$febd6760$@lbmsys.com>
MIME-Version: 1.0
X-IsSubscribed: yes

I have a simple programs that show the following issue:

1) program locks a file (in my test /tmp/yyy)
2) program then calls spawnv() (in my test    "/bin/sh  -c  /bin/touch
/tmp/xxx").
3) after the spawnv(), the file /tmp/yyy is no longer locked.

It seems to me that this is wrong. Perhaps I am misunderstanding how file 
locking works. On the other hand, the file lock is preserved if the child
task is started with posix_spawn() rather than spawnv().

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <process.h>
#include <fcntl.h>
#include <time.h>
#include <spawn.h>

extern char **environ;

main()
{
  int status;
  pid_t pid, task_pid;
  struct flock lock;
  const char * argv[] = {"/bin/sh", "-c", "/usr/bin/touch /tmp/xxx", (char
*) 0};
  pid = open("/tmp/yyy", O_WRONLY|O_CREAT,0666);  // create file to be
locked
  if (pid==(pid_t)-1) {
    fprintf(stderr, "Error creating /tmp/yyy - %s\n", strerror(errno));
    exit(1);
  }
  lock.l_type = F_WRLCK;                    // lock /tmp/yyy
  lock.l_start = 0;
  lock.l_whence = 0;
  lock.l_len = 0;
  errno = 0;
  if (fcntl(pid, F_SETLKW, &lock) < 0) {
    fprintf(stderr, "Error locking /tmp/yyy - %s\n", strerror(errno));
    exit(1);
  }

  write_status("/tmp/yyy is locked ... sleeping for 10 seconds\n");

  sleep(10);

  status = spawnv(_P_NOWAIT, "/bin/sh", argv);  // spawn some random program
/*  status = posix_spawn(&task_pid, "/bin/sh", NULL, NULL, argv, environ);
*/
  if (errno != 0)
    fprintf(stderr, "Error starting %s (%s)\n", argv[2], strerror(errno));
  else {
    write_status("spawnv() succeeded\n");
  }
  if (access("/tmp/xxx", F_OK)) {               //  check to make sure our
random program worked
    write_status("spawned program did not complete\n");
  } else {
    write_status("spawned program completed\n");
  }

  while (1) {
    write_status("main program idle\n");
    sleep(2);
  }
  exit(0);
}
write_status(char *message)
{
  char ctbuf[90];
  time_t t,time(time_t *);
  t=time((time_t*)0);         /* make a time stamp */
  strcpy(ctbuf,(char *)ctime(&t));
  if (ctbuf[strlen(ctbuf)-1]=='\n') ctbuf[strlen(ctbuf)-1]=(char)0;
  strcat(ctbuf, ": ");
  strcat(ctbuf, message);
  fprintf(stderr, ctbuf);
}

Steve Bardwell





--
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

- Raw text -


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