I have code that calls g_spawn_sync and sets an environment variable.  The 
program that I am spawning simply dumps out the environment and exits.  I found 
some odd behavior when running this test app on Cygwin, if I set any random 
variable, like "HELLO=world" it is not passed to the child.  However, if I set 
PATH, I get it set along with 2 other variables.  Attached is my test code that 
demonstrates the issue.

Environ.c - child program that dumps the environment
Gspawn_env_test.cpp - program that spawns environ.c and reads the output
Make - simple shell script that builds the test apps

Cygwin 1.7.9 results:

$ ./gspawn_env_test.exe
** (process:5080): DEBUG: Calling my_environ with envp set to HELLO=world
** (process:5080): DEBUG: result:

** (process:5080): DEBUG: ------------------------------------
** (process:5080): DEBUG: Calling my_environ with envp set to PATH=/usr/bin
** (process:5080): DEBUG: result:
PATH=/usr/bin
SYSTEMROOT=C:\Windows
WINDIR=C:\Windows

** (process:5080): DEBUG: ------------------------------------

Ubuntu 10.04 results:

[mfisch@toaster ~/gspawn_issue]$ ./gspawn_env_test
** (process:29580): DEBUG: Calling my_environ with envp set to HELLO=world
** (process:29580): DEBUG: result:
HELLO=world

** (process:29580): DEBUG: ------------------------------------
** (process:29580): DEBUG: Calling my_environ with envp set to PATH=/usr/bin
** (process:29580): DEBUG: result:
PATH=/usr/bin

** (process:29580): DEBUG: ------------------------------------

Attachment: cygcheck.out
Description: cygcheck.out

#include <stdio.h>

int main(void)
{
        extern char **environ;
        int i = 0;
        for(i; environ[i]!=NULL; i++)
                printf("%s\n",environ[i]); 
}
#include <glib.h>
#include <string>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>

enum envpOption {
        ENVP_NULL,
        ENVP_PATH,
        ENVP_MISC
}; 

void spawnproc(envpOption opt)
{
    gchar * argv[2];
    gchar * envp[2];
    gchar * g_stdoutBuffer = NULL;
    gchar * g_stderrBuffer = NULL;
        gboolean resultStatus;

    int exit_status;
    GError * gerr = NULL;

    GSpawnFlags flags = (GSpawnFlags)(G_SPAWN_SEARCH_PATH);

    argv[0] = (gchar*)"./my_environ.exe";
    argv[1] = NULL;


        if (opt == ENVP_MISC)
        {
                envp[0] = (gchar *)"HELLO=world";
                envp[1] = NULL;

                resultStatus = g_spawn_sync("/home/mfisch/gspawn_issue",
                                argv,
                                envp,
                                flags,
                                NULL,
                                NULL,
                                &g_stdoutBuffer,
                                &g_stderrBuffer,
                                &exit_status,
                                &gerr);
        }
        else if (opt == ENVP_PATH)
        {
                envp[0] = (gchar *)"PATH=/usr/bin";
                envp[1] = NULL;

                resultStatus = g_spawn_sync("/home/mfisch/gspawn_issue",
                                argv,
                                envp,
                                flags,
                                NULL,
                                NULL,
                                &g_stdoutBuffer,
                                &g_stderrBuffer,
                                &exit_status,
                                &gerr);
        }
        else if (opt == ENVP_NULL)
        {
                resultStatus = g_spawn_sync("/home/mfisch/gspawn_issue",
                                argv,
                                NULL,
                                flags,
                                NULL,
                                NULL,
                                &g_stdoutBuffer,
                                &g_stderrBuffer,
                                &exit_status,
                                &gerr);

        }


    if (gerr) 
        {
        g_warning("error: %s",gerr->message);
    }

    if (g_stdoutBuffer)
    {
                g_debug("result:\n%s",g_stdoutBuffer);
        }

    if (!resultStatus) 
        {
        g_warning("error: spawn failed");
    }
}

int main(void)
{
        g_debug("Calling my_environ with envp set to HELLO=world");
        spawnproc(ENVP_MISC);
        g_debug("------------------------------------");
        g_debug("Calling my_environ with envp set to PATH=/usr/bin");
        spawnproc(ENVP_PATH);
        g_debug("------------------------------------");
#if 0
        g_debug("Calling my_environ with envp Disabled");
        spawnproc(ENVP_NULL);
        g_debug("------------------------------------");
#endif
}

Attachment: make
Description: make

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

Reply via email to