delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2020/04/18/11:25:21

X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2EBD4385C426
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1587223475;
bh=krU8rHPsIoDj92GGk0YEn2g/DBbyw8WINNqzxPEKPxk=;
h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post:
List-Help:List-Subscribe:From:Reply-To:From;
b=Vm40ijDL1+thPe/zbmG42eK3BAh2UJquxb1oTcRgrBTJ3imHF/aNSR1A8F99p/8TT
Cew3MIUHcASfg+V9pCYwlhU5p0cRAR1Wj5HHNlaC+EGCaPNwNI6FxV4uvlsw4NQvA8
WLFg2YGCw1WEOnHD4XgpWd6sgZO2NQb6m/e1YvAg=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D7049385B835
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version
:content-language:thread-index;
bh=01Hvgnu6hbnSH6csgtHPuYeA/9hX2erOQt+5q3M+Z1o=;
b=D0h2EfZZ80wJ6b8ywZCzezE5wmmRB5sZpwj/8UctWApxa3JdGn7drclytULqRgdNuI
05b+ZzToXJRohAvKWQ089iZhyOVYWWsnV7qkWxTfGQjw4q6uopWNqIxj3X2ea5tjENbr
dB1D8l185EPoORyVCUW/b9Kt+PlqIgDcXJ3rWa8YPIz2Zj6DYRnkTdX7lpBLTGcovpnL
e87mzQxGUWCJ4h9UfhoWIplQWoe6Z7m0fkaVpcmfL5VfmaH1Vv/zcA7z6tlmqSPuV3/+
gcv3j790h9QDYcpk6l9hodlF3f3RYDAClQ+OtG1YTQ8kNbQCNcluFlfvkFNLPnyma0Io
mlCA==
X-Gm-Message-State: AGi0Pub65wbc1j5wztHqXfMTEPjToMSz1Bw8lF6ZwQar6zkEQjw9Xpn0
hq9xWM5fUfEA0y8svnWTJjbrKRth
X-Google-Smtp-Source: APiQypJIC9SbmUUp/xhuJ7tMhcNSZxnUYIHtstJewNZNDVAISh53HiksXIxwKqOLY+zlZsI3nqhgOg==
X-Received: by 2002:a05:6512:3b0:: with SMTP id
v16mr5246665lfp.213.1587223470380;
Sat, 18 Apr 2020 08:24:30 -0700 (PDT)
To: <cygwin AT cygwin DOT com>
Subject: open write descriptor on named pipe sometime results in ENOENT
Date: Sat, 18 Apr 2020 17:24:28 +0200
Message-ID: <005101d61595$741a0120$5c4e0360$@gmail.com>
MIME-Version: 1.0
X-Mailer: Microsoft Outlook 16.0
Thread-Index: AdYVkihqibrCGzwjTaKuc3FImwqzOA==
X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, DKIM_SIGNED,
DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_1,
RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS,
TXREP autolearn=ham autolearn_force=no version=3.4.2
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on
server2.sourceware.org
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.29
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <http://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Kristian Ivarsson via Cygwin <cygwin AT cygwin DOT com>
Reply-To: sten DOT kristian DOT ivarsson AT gmail DOT com
Sender: "Cygwin" <cygwin-bounces AT cygwin DOT com>

This is a multipart message in MIME format.

------=_NextPart_000_0052_01D615A6.37A34650
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hey all


We're trying to nail down some issues with using named pipes

The issue we're getting is deterministic (ENXIO) but it is not this one, but
we think this issue is worth reporting anyway


We're using the branch topic/fifo



The program explained in short is:


- One main (parent) pipe that lives through the whole execution

- The main process forks 'children' child-processes that creates their own
(unique) named pipes

- Each child forks 'children' grans-child-processes that just writes some
bogus messages back to the unique child pipe

- Each child writes a bogus message back to the main process

- Every process creates a write and a read descriptor, but the write
descriptor is just a dummy descriptor (to somehow keep the pipe alive
without being bombarded with signals)

- This iterates a few times


Some of the constructs may be a bit confusing and maybe not relevant to this
issue, but I left them in the test-program anyway 




Issue #1 sometimes occurs in line 35 (printed as 36) we get ENOENT (No such
file or directory) despite that the pipe was just created and the read
descriptor successfully was opened

   *wfd = open(name, O_WRONLY);


Issue #2 sometimes occurs in line 73 (printed as 74) we get EBUSY (Device or
resource busy) when attempting to open a non blocking descriptor

   const int wfd = open(name, O_WRONLY | O_NONBLOCK);


Issue #3 sometimes occurs somewhere unknown and the main process just get
stuck (I've failed to reproduced that with strace or so) and to not have any
more input so maybe this should be left out ?



I hope this is well described and hopefully it's enough to reproduce the
issue(s) and hopefully is not due to a fault test case ;-)



Kristian


------=_NextPart_000_0052_01D615A6.37A34650
Content-Type: text/plain;
	name="family.cpp"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="family.cpp"

#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>

void print_error(const int line, const int error)
{
   printf("%d\tline %d\t%s\n", getpid(), line, strerror(error));
}

#define HANDLE_ERROR(result) do {if (result < 0){print_error(__LINE__, =
errno);return result;}} while (0);

#define HANDLE_RESULT(result) do {if (result < 0){return result;}} while =
(0);

int make_reader(const char *const name, int *const rfd, int *const wfd)
{
   //printf("%d\tmake\t%s\n", getpid(), name);

   /* In case it exists */
   remove(name);

   const int mode =3D O_CREAT | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | =
S_IROTH | S_IWOTH;

   const int fifo =3D mkfifo(name, mode) ? -1 : 0;
   HANDLE_ERROR(fifo);

   *rfd =3D open(name, O_RDONLY | O_NONBLOCK);
   HANDLE_ERROR(*rfd);

   *wfd =3D open(name, O_WRONLY);
   HANDLE_ERROR(*wfd);

   return 0;
}

int stop_reader(const char *const name, const int *const rfd, const int =
*const wfd)
{
   //printf("%d\tstop\t%s\n", getpid(), name);

   HANDLE_ERROR(close(*wfd));
   HANDLE_ERROR(close(*rfd));

   HANDLE_ERROR(unlink(name));

   return 0;
}

int wait_child(const int pid)
{
   int status =3D 0;
   HANDLE_ERROR(waitpid(pid, &status, 0));
   if (WIFEXITED(status))
   {
      //printf("%d\tchild %d exited\n", getpid(), pid);
   }
   return 0;
}

int send_messages(const char *const name, const int messages)
{
   //printf("%d\tsend\t%s\n", getpid(), name);

   char message[PIPE_BUF];
   memset(message, ' ', sizeof message);

   for (int index =3D 0; index < messages; ++index)
   {
      const int wfd =3D open(name, O_WRONLY | O_NONBLOCK);
      HANDLE_ERROR(wfd);
      if (::write(wfd, message, sizeof message) =3D=3D -1 && errno =
=3D=3D EAGAIN)
      {
         //printf("%d\tsend\t%s\t[%d]\n", getpid(), name, index);
         HANDLE_ERROR(fcntl(wfd, F_SETFL, ~O_NONBLOCK & fcntl(wfd, =
F_GETFL)));
         HANDLE_ERROR(::write(wfd, message, sizeof message));
      }
      HANDLE_ERROR(close(wfd));
   }

   return 0;
}

int read_messages(const char *const name, const int rfd, const int =
messages)
{
   //printf("%d\tread\t%s\n", getpid(), name);

   char message[PIPE_BUF] =3D {};

   for (int index =3D 0; index < messages; ++index)
   {
      HANDLE_ERROR(fcntl(rfd, F_SETFL, O_NONBLOCK | fcntl(rfd, =
F_GETFL)));
      if (::read(rfd, message, sizeof message) =3D=3D -1 && errno =3D=3D =
EAGAIN)
      {
         //printf("%d\tread\t%s\t[%d]\n", getpid(), name, index);
         HANDLE_ERROR(fcntl(rfd, F_SETFL, ~O_NONBLOCK & fcntl(rfd, =
F_GETFL)));
         HANDLE_ERROR(::read(rfd, &message, sizeof(message)));
      }
   }

   return 0;
}

int main()
{
   const int messages =3D 5;
   const int children =3D 25;

   const char *name_parent =3D "/tmp/pipe_parent";

   int rfd, wfd;
   HANDLE_RESULT(make_reader(name_parent, &rfd, &wfd));

   for (int lap =3D 1; lap < 5; ++lap)
   {
      printf("lap %d\n", lap);

      int first_generation_pids[children];

      for (int idx =3D 0; idx < children; ++idx)
      {
         const int first_generation_pid =3D fork();
         HANDLE_ERROR(first_generation_pid);

         if (first_generation_pid =3D=3D 0)
         {
            char name_child[32];
            snprintf(name_child, sizeof name_child, =
"/tmp/pipe_child_%d", idx);
            int rfd, wfd;
            HANDLE_RESULT(make_reader(name_child, &rfd, &wfd));

            int second_generation_pids[children];

            for (int idx =3D 0; idx < children; ++idx)
            {
               const int second_generation_pid =3D fork();
               HANDLE_ERROR(second_generation_pid);

               if (second_generation_pid =3D=3D 0)
               {
                  return send_messages(name_child, messages);
               }

               second_generation_pids[idx] =3D second_generation_pid;
            }

            HANDLE_RESULT(read_messages(name_child, rfd, children * =
messages));

            for (int idx =3D 0; idx < children; ++idx)
            {
               wait_child(second_generation_pids[idx]);
            }

            HANDLE_RESULT(send_messages(name_parent, messages));

            HANDLE_RESULT(stop_reader(name_child, &rfd, &wfd));

            return 0;
         }

         first_generation_pids[idx] =3D first_generation_pid;
      }

      HANDLE_RESULT(read_messages(name_parent, rfd, children * =
messages));

      for (int idx =3D 0; idx < children; ++idx)
      {
         wait_child(first_generation_pids[idx]);
      }
   }

   HANDLE_RESULT(stop_reader(name_parent, &rfd, &wfd));

   return 0;
}
------=_NextPart_000_0052_01D615A6.37A34650
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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

------=_NextPart_000_0052_01D615A6.37A34650--

- Raw text -


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