delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2011/10/20/04:22:35

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-0.3 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD
X-Spam-Check-By: sourceware.org
From: Manuel Wienand <Manuel DOT Wienand AT ubitronix DOT com>
To: "cygwin AT cygwin DOT com" <cygwin AT cygwin DOT com>
Date: Thu, 20 Oct 2011 10:19:50 +0200
Subject: 1.7.9: spawn brakes reopening of serial port
Message-ID: <0C11C5BF0B29FD43A8D0250F711D497F89DEDBFEE2@ex01-ubitronix.ubitronix.local>
MIME-Version: 1.0
X-IsSubscribed: yes
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
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id p9K8MO5g022384

Hallo,

it seems that spawning a process brakes the reopening of a serial port (when done during the execution of that process). The result of the open() function is "Permission denied (13)" in that case.

How to test:
Run the following code ;)
TestSharedMemProc is the process that should be spawned by TestSharedMem (yes, the name is misleading, but it's just a test...).

First I had shm_open under investigation for my problem, because sometimes I got a "Bad Address" (EFAULT, 14) error, but I couldn't reproduce it with a small program and I'm not sure if the problem is somehow connected to the serial port problem. In my original program I got it when opening the shared memory in the spawned (child) process. The shared memory was created in the parent process first and did exist in /dev/shm.
shm_open() returning EFAULT seems to be cygwin specific; what could have caused it? Maybe with a hint in the right direction I might be able to reproduce the error as well.

Thanks,
Manuel

/*
 ============================================================================
 Name        : TestSharedMem.c
 ============================================================================
*/

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

#define COMPORT_NAME      "/dev/com1"
#define SPAWN_FILE_NAME   "./TestSharedMemProc.exe"

int serialportHandle = -1;

int SC_Close()
{
  if( serialportHandle >= 0 )
  {
    fprintf(stdout, "Closing handle %d.\n", serialportHandle);
    if( close(serialportHandle) < 0 )
    {
      fprintf(stdout, "Couldn't close COM-Port: %s :%d \n",sys_errlist[errno],errno);
      return 3;
    }
  }
  serialportHandle = -1;

  return 0;
}

int SC_Open()
{

  // Close any already open connections first
  if( serialportHandle >= 0)
  {
    fprintf(stdout, "Open called before closing the port. Closing the port now.\n");
    SC_Close();
  }

  // Call open to open a port
  serialportHandle = open(COMPORT_NAME, O_RDWR | O_NONBLOCK );
  if( serialportHandle < 0 )
  {
    fprintf(stdout, "Couldn't open COM-Port (%s): %s :%d \n",COMPORT_NAME,sys_errlist[errno],errno);
    return 4;
  }

  fprintf(stdout, "Opened port %s (handle %d).\n", COMPORT_NAME, serialportHandle);

  return 0;
}

int main(void) {
	puts("TestSharedMem");
	int error;

  error = SC_Open();
  if (error != 0)
  {
    printf("first open(COM port %s) failed.\n", COMPORT_NAME);
  }

  // Spawn
  //int pid = spawnl(_P_WAIT, SPAWN_FILE_NAME, "TestSharedMemProc", NULL);
  int pid = spawnl(_P_NOWAITO, SPAWN_FILE_NAME, "TestSharedMemProc", NULL);
  if (pid == -1)
  {
    printf( "Failed to create process: %s %d\n", sys_errlist[errno],errno);
  }
  //sleep(2); // If the process closes before the next SC_Open, it works.

  error = SC_Open();
  if (error != 0)
  {
    printf("second open(COM port %s) failed.\n", COMPORT_NAME);
  }

  SC_Close();

  puts("Done (TestSharedMem).");

	return EXIT_SUCCESS;
}

/*
 ============================================================================
 Name        : TestSharedMemProc.c
 ============================================================================
*/

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

int main(void) {
	puts("TestSharedMemProc");

  sleep(1); // This process must be running when
            // opening the COM port the second time
            // -> maybe you need to increase the time

  puts("Done (TestSharedMemProc).");

	return 0;
}

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