delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2015/03/27/05:30:35

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:from:to:subject:date:message-id:content-type
:content-transfer-encoding:mime-version; q=dns; s=default; b=EdZ
xqmXUMgoLhCw0bQdiBeq6uaAiCPqKUqO5zXIsPNQ48Du4N8Dns83IZKWeUKWzWCi
+DmOAtVkJ9N5u5r7U3NTCiMMLpjXapLmSunUuVfkdQt5l9bqMxtvlV4cIqPVvvgU
qKeLLU4S6G8v27+c3hXZO5xnNMH7kIUpD1oWfDZw=
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:from:to:subject:date:message-id:content-type
:content-transfer-encoding:mime-version; s=default; bh=Nysnc7TUR
FBw21Uqu9srBGx3V6k=; b=m+hZDB3sGsig2V2Sh6EgMd8/1mwYxZBEcGzIpfW9d
r73GHG+R7KS+pNaNfmGikjSz7Wu6kBKij6XlZLRfolBdfMVpYXsVk7Muz3PV8uP4
uoW/VgOq2J9ATSZc2x63zbsmYsRnRlMWzOqSoWdZlEViB8RKlXWArcmJBuTz8hTZ
8w=
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: Yes, score=5.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=no version=3.3.2
X-HELO: mail-out.astrium.eads.net
From: "KEREP, Mladen" <mladen DOT kerep AT airbus DOT com>
To: "cygwin AT cygwin DOT com" <cygwin AT cygwin DOT com>
Subject: Handles returned by mq_open are not valid file descriptors as supposed to be under native linux distributions
Date: Fri, 27 Mar 2015 09:30:05 +0000
Message-ID: <556891A1F0F6154CB3DD7811A49431534D3B8027@FOWEXMC003.de.astrium.corp>
MIME-Version: 1.0
X-TM-AS-GCONF: 00
X-IsSubscribed: yes
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id t2R9USKc024288

We're using POSIX message queues to pass messages between processes.
For this we've build a library layer to be able to use message queues on different platforms. Basically linux (debian, Ubuntu, archlinux, rasbian) is the development platform, but also vxworks platforms are supported.

Several message queues are opened through mq_open calls and the returned handles are organized in file descriptor sets (fd_set). The set is then passed over to a select(2) call, which blocks processing and returns as soon as any message in any of the queues arrives.

This works under linux, since the handles returned are valid file descriptors and the macros FD_ZERO / FD_SET are able to handle that handles.

However, under Cygwin, mq_open does not directly return file handles, but pointers to (unknown) data structures in memory (ref. http://sourceware.org/ml/cygwin/2013-07/msg00179.html), which cannot be used with FD_ZERO, FD_SET, so not with select(2). I know from the man pages (mq_open):
"Polling message queue descriptors
On Linux, a message queue descriptor is actually a file descriptor, and can be monitored using select(2), poll(2), or epoll(7).  This is not portable."

mq_open() and select() conform to POSIX.1-2001, at least under linux, but also under Cygwin ?
How can this be modified, so that it works under Cygwin as well ?

Here's a small code example, showing a segmentation fault in macros FD_ZERO/FD_SET:

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

int main(void)
{
  int res;
  int i = 0;
  char msgQueueTestName[] = "/testQueue";
  mqd_t testQueue;
  struct mq_attr  attrQueue;
  fd_set readfds;
  int highest_fd;
  int numMessages;

  memset(&attrQueue,0x00,sizeof(attrQueue));
  attrQueue.mq_maxmsg  = 10;  /* room for X messages in the queue */
  attrQueue.mq_msgsize = 20;  /* maximum size of a message */

  /* this example has only one message queue; usually several message queues are opened by different processes */
  testQueue = mq_open(msgQueueTestName, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG, &attrQueue);
  if( testQueue == (mqd_t)-1 )
  {
      printf("Failed to open msg queue %s: %s %d\n", msgQueueTestName, sys_errlist[errno],errno);
  }
  else
  {
      printf("msg queue descriptor %d (0x%x)\n", testQueue, testQueue);
  }

  highest_fd = 0;

  /* here, usually we scan all opened message queues for highest fd */
  if (testQueue > highest_fd)
	highest_fd = testQueue;
  highest_fd++;
  printf("highest fd %d (0x%x)\n", highest_fd, highest_fd);

  /* add all file descriptors to be checked */
  FD_ZERO(&readfds);
  FD_SET(testQueue, &readfds);
  printf("readfds entries %d\n", sizeof(readfds.fds_bits) / sizeof(fd_mask));

  numMessages = select(highest_fd, &readfds, NULL, NULL, (struct timeval *) NULL);
  printf("messages pending %d\n", numMessages);


  return EXIT_SUCCESS;
}

Any help will be appreciated.
Mladen


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