strange glib spawn problem



I've recently noticed a strange problem with the glib function,
g_spawn_command_line_sync().  I think this problem has arisen since I
updated from Linux kernel 2.6.1 to 2.6.2, though I have no idea how
that could be relevant.

The problem is that bash builtins are not working.  For instance, if I
try to spawn a command that starts with "cd" I'm getting 'Failed to
execute child process "cd"'.  I have checked, and the regular C
'system' command works OK.  Below is a little test program that tries
to cd to /tmp and write a little file, using both system() and glib.
The 'system' version writes its file in /tmp OK, while the glib
version reports

glib_spawn: 'Failed to execute child process "cd" (No such file or
directory)'

/* test program */

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

int glib_spawn (const char *cmdline)
{
    GError *error = NULL;
    gchar *errout = NULL, *sout = NULL;
    int ok, status;
    int ret = 0;

    ok = g_spawn_command_line_sync (cmdline,
                                    &sout,
                                    &errout,
                                    &status,
                                    &error);

    if (!ok) {
        fprintf(stderr, "glib_spawn: '%s'\n", error->message);
        g_error_free(error);
        ret = 1;
    } else if (errout && *errout) {
        fprintf(stderr, "stderr: '%s'\n", errout);
        ret = 1;
    } else if (status != 0) {
        fprintf(stderr, "status=%d: '%s'\n", status, sout);
        ret = 1;
    }

    if (errout != NULL) g_free(errout);
    if (sout != NULL) g_free(sout);

    if (ret) {
        fprintf(stderr, "Failed command: '%s'\n", cmdline);
    }

    return ret;
}

int system_spawn (const char *cmdline)
{
    int err;

    err = system(cmdline);
    if (err) {
        fprintf(stderr, "Failed command: '%s'\n", cmdline);
        perror(NULL);
    }

    return err;
}

int main (void)
{
    system_spawn("cd /tmp && echo foo > SYSTEM_HERE_I_AM");
    glib_spawn("cd /tmp && echo foo > GLIB_HERE_I_AM");

    return 0;
}

/* end of test program */

Linux kernel 2.6.2, bash 2.05b; /bin/sh is a hard link to bash.

-- 
Allin Cottrell
Department of Economics
Wake Forest University, NC



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]