www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2025/04/09/18:53:44

DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 539Mrid12960077
Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com
Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com
DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 539Mrid12960077
Authentication-Results: delorie.com;
dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=R9NJm+gy
X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E43F3840C1F
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1744239222;
bh=6mIjYJdOnTtTCvBAZWqz00uGi1ekHE0zFePoaaILIAI=;
h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:
List-Help:List-Subscribe:From:Reply-To:From;
b=R9NJm+gyIIAjvHZmq2r22+dRkFBENen6KprSM81W48SVIGhgOUHtOHrW0DeTZhJQy
9mlqRxMVJsJuzlop55kcYcaXeyGl8VEZ9PQ04HEcSeNerTBZRFYxHCeQzhmdBkdbkG
qu53ToAgdljvVMfAwqe8KrmMfbY2IiF8UhRbfQII=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C0C6385AC1E
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2C0C6385AC1E
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1744239199; cv=none;
b=XbbPHUeU914RZlJ+/NJYsSbcaz3RY653rEdr2q1Gg6MH5DbzjGHTF8+4F/T46EMjX4HVSLCqhNlPQ+SajFSC87bmOU16GTOlQRt06kJUoBt1skqRZ2p7M88KBhtDhdWOUrQ30fKqj+1/MeEaC0mZsvH4j1s9GuElaaOMp2qSZmY=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
t=1744239199; c=relaxed/simple;
bh=XxDqIsbE8Rf5iT37J92XTmaWac/YDofiLF/85LWg6Do=;
h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version;
b=jsOo5z6haklqEvoY7WLQyb+mMdE4KKuTRtuTttOAgKj71EmgVc0bEqLvDVJcb8jvO+TeAT/TkpUmaJ2IAD9B84JSaD2evqYAxG91hJwTvY9r/xFZwT35cRPv+6GU5Kl80mb/7UYLKiddjDJZ88CnGgwkqKBBNaKDglPNETWDN+s=
ARC-Authentication-Results: i=1; server2.sourceware.org
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C0C6385AC1E
Date: Wed, 9 Apr 2025 15:53:17 -0700 (PDT)
X-X-Sender: jeremyd AT resin DOT csoft DOT net
To: cygwin AT cygwin DOT com
Subject: pthread_atfork vs dlopen/dlclose
Message-ID: <07665688-8199-2e80-cdfe-abeaa6f56ba7@jdrake.com>
MIME-Version: 1.0
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.30
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: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Jeremy Drake via Cygwin <cygwin AT cygwin DOT com>
Reply-To: Jeremy Drake <cygwin AT jdrake DOT com>
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com>

The recent issue with pthread_atfork handlers reminded me of a scenario
that I know glibc handles, but it seems that Cygwin does not.  Test case:

== test.c ==
#include <dlfcn.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

typedef void(*func_type)(void);

int main(void)
{
  int wstatus = 0;
  func_type func = NULL;
  void *handle = dlopen("./testlib.so", RTLD_LAZY);
  if (!handle)
  {
    fprintf(stderr, "dlopen error: %s\n", dlerror());
    return 1;
  }

  func = (func_type)dlsym(handle, "func");
  if (!func)
  {
    fprintf(stderr, "dlsym error: %s\n", dlerror());
    return 1;
  }

  func();

  if (dlclose(handle))
  {
    fprintf(stderr, "dlclose error: %s\n", dlerror());
    return 1;
  }

  switch (fork())
  {
  case -1:
    perror("fork");
    return 1;
  case 0:
    printf("Hi from child!\n");
    return 0;
  default:
    if (wait(&wstatus) == -1)
    {
      perror("wait");
      return 1;
    }
    printf ("wstatus %d\n", wstatus);
    return WIFSIGNALED(wstatus) ? 128 + WTERMSIG(wstatus) : WEXITSTATUS(wstatus);
  }

  /* should not get here */
  return 123;
}

== testlib.c ==
#include <pthread.h>
#include <stdio.h>

static void prepare(void)
{
  printf("in prepare\n");
}

static void parent(void)
{
  printf("in parent\n");
}

static void child(void)
{
  printf("in child\n");
}

void func(void)
{
  int ret;
  if (ret = pthread_atfork(prepare, parent, child))
    fprintf(stderr, "pthread_atfork returned %d\n", ret);
}

== build commands ==
gcc -fPIC -shared -o testlib.so testlib.c
gcc -o test test.c
./test

== linux output ==
$ ./test
Hi from child!
wstatus 0

== Cygwin 3.6.1 output ==
$ ./test
$ echo $?
0
$ # WTF?
$ gdb ./test
...
(gdb) r

Thread 1 "test" received signal SIGSEGV, Segmentation fault.
0x00000004219b1030 in ?? ()
(gdb) bt
#0  0x00000004219b1030 in ?? ()
#1  0x00000001800ad1b2 in pthread::atforkprepare ()
    at ../../.././winsup/cygwin/thread.cc:2091
#2  0x000000018006bdcd in lock_pthread::lock_pthread (this=<synthetic pointer>)
    at /d/a/msys2-runtime/msys2-runtime/winsup/cygwin/local_includes/sigproc.h:136
#3  hold_everything::hold_everything (x=<synthetic pointer>: <optimized out>,
    this=<synthetic pointer>)
    at /d/a/msys2-runtime/msys2-runtime/winsup/cygwin/local_includes/sigproc.h:168
#4  dofork (proc=proc AT entry=0x0,
    with_forkables=with_forkables AT entry=0x7ffffcb9f)
    at ../../.././winsup/cygwin/fork.cc:640
#5  0x000000018006c239 in fork () at ../../.././winsup/cygwin/fork.cc:590
#6  0x000000018019f564 in _sigfe () at sigfe.s:35
#7  0x000000010040117e in main ()

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

- Raw text -


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