delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/11/05/11:26:15

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT sourceware DOT cygnus DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sourceware DOT cygnus DOT com>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>, <http://sourceware.cygnus.com/ml/#faqs>
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-ID: <779F20BCCE5AD31186A50008C75D997917167E@SILLDN_MAIL1>
From: "Fifer, Eric" <EFifer AT sanwaint DOT com>
To: "'Steve Jorgensen'" <steve AT khoral DOT com>, cygwin AT sourceware DOT cygnus DOT com
Subject: RE: DLL's and environ again
Date: Fri, 5 Nov 1999 16:22:05 -0000
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.2448.0)

Steve,

I think I saw this before and I posted something to the list, but never
got a response.  My original message is below.  The way I ended
up working around this was to add a function to the dll that is initialized
by the main exe, the function passes the address of environ from the
exe to the dll.  The dll then assigns it's local environ to the
main exe's environ.  Also a realloc can occur in setenv (in the main
context) so after using setenv in the dll, you should assign the local
environ to the main exe's environ again.  See perl5_005.62's util.c for
the working code. 

Hope this helps.

Eric Fifer


----

Subject: Environ not shared between exe and dlls
Date: Wed 5/26/99 3:17 PM

This is a problem I found while fixing some problems with
the latest dynamic build of perl.

When I call setenv enough times to force a realloc of
the global environ variable, the value of environ only
changes in the exe and not in any of the dlls.

Note that environ shows as defined in the exe *and* the dll:

    nm envbug.exe | grep environ
    00402008 D _environ

    nm libenv.dll | grep environ
    6dc02004 D _environ

Should environ be in "struct _reent" or am I going about this
in the wrong way?

Thanks in advance.

Eric Fifer

----------------------------------------

Just to be clear, here's an example ...

output:
	exe: environ=a030008 environ[0]=!S:=S:\EFifer
	dll: environ=a030008 environ[0]=!S:=S:\EFifer
	exe: environ should be realloced
	exe: environ=a031330 environ[0]=!S:=S:\EFifer
	             ^^^^^^^ environ realloced in exe
	dll: environ=a030008 environ[0]=`'^Fa`'^Fa
	             ^^^^^^^ but not changed in dll, points to garbage
	dll: environ in dll reset
	dll: environ=a031330 environ[0]=!S:=S:\EFifer
	             ^^^^^^^ after overriding environ everything is fine

envbug.c:

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

	#define DLLIMPORT __declspec (dllimport)
	extern DLLIMPORT void dll_env_init(char ***penviron);
	extern DLLIMPORT void dll_env();

	int
	main()
	{
	    dll_env_init(&environ);

	    printf("exe: environ=%x environ[0]=%s\n", environ, environ[0]);

	    dll_env();

	    /* force the realloc of environ - see winsup/environ.cc */
	    setenv("foo1", "bar", 1); setenv("foo2", "bar", 1);
	    setenv("foo3", "bar", 1); setenv("foo4", "bar", 1);
	    setenv("foo5", "bar", 1); setenv("foo6", "bar", 1);
	    setenv("foo7", "bar", 1); setenv("foo8", "bar", 1);
	    printf("exe: environ should be realloced\n");

	    printf("exe: environ=%x environ[0]=%s\n", environ, environ[0]);

	    dll_env();

	    return 0;
	}

libenv.c:

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

	#define DLLIMPORT __declspec (dllexport)

	static char ***dll_env_environ;

	DLLIMPORT void
	dll_env_init(char ***penviron)
	{
	    dll_env_environ = penviron;
	}

	DLLIMPORT void
	dll_env()
	{
	    printf("dll: environ=%x environ[0]=%s\n", environ, environ[0]);

	    if(environ != *dll_env_environ) {
		environ = *dll_env_environ;
		printf("dll: environ in dll reset\n");
		printf("dll: environ=%x environ[0]=%s\n", environ,
environ[0]);
	    }
	}

Makefile:

	CFLAGS	= -g -Wall
	LDFLAGS = -g

	all: libenv.dll envbug

	envbug: envbug.o
		$(CC) $(LDFLAGS) -o envbug envbug.o -L. -lenv

	%.dll: %.o
		dllwrap --export-all --output-def $*.def \
			--output-lib $*.a -o $@ $<

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com

- Raw text -


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