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]

Environ not shared between exe and dlls



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]