This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: DLL's and environ again



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@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]