~ubuntu-branches/ubuntu/utopic/glib2.0/utopic

« back to all changes in this revision

Viewing changes to glib/gspawn-win32.c

Tags: upstream-2.12.12
ImportĀ upstreamĀ versionĀ 2.12.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gspawn-win32.c - Process launching on Win32
 
2
 *
 
3
 *  Copyright 2000 Red Hat, Inc.
 
4
 *  Copyright 2003 Tor Lillqvist
 
5
 *
 
6
 * GLib is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public License as
 
8
 * published by the Free Software Foundation; either version 2 of the
 
9
 * License, or (at your option) any later version.
 
10
 *
 
11
 * GLib is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with GLib; see the file COPYING.LIB.  If not, write
 
18
 * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
 * Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
/*
 
23
 * Implementation details on Win32.
 
24
 *
 
25
 * - There is no way to set the no-inherit flag for
 
26
 *   a "file descriptor" in the MS C runtime. The flag is there,
 
27
 *   and the dospawn() function uses it, but unfortunately
 
28
 *   this flag can only be set when opening the file.
 
29
 * - As there is no fork(), we cannot reliably change directory
 
30
 *   before starting the child process. (There might be several threads
 
31
 *   running, and the current directory is common for all threads.)
 
32
 *
 
33
 * Thus, we must in most cases use a helper program to handle closing
 
34
 * of (inherited) file descriptors and changing of directory. The
 
35
 * helper process is also needed if the standard input, standard
 
36
 * output, or standard error of the process to be run are supposed to
 
37
 * be redirected somewhere.
 
38
 *
 
39
 * The structure of the source code in this file is a mess, I know.
 
40
 */
 
41
 
 
42
/* Define this to get some logging all the time */
 
43
/* #define G_SPAWN_WIN32_DEBUG */
 
44
 
 
45
#include "config.h"
 
46
 
 
47
#include "glib.h"
 
48
#include "gprintfint.h"
 
49
#include "glibintl.h"
 
50
#include "galias.h"
 
51
 
 
52
#include <string.h>
 
53
#include <stdlib.h>
 
54
#include <stdio.h>
 
55
 
 
56
#include <windows.h>
 
57
#include <errno.h>
 
58
#include <fcntl.h>
 
59
#include <io.h>
 
60
#include <process.h>
 
61
#include <direct.h>
 
62
 
 
63
#ifdef __MINGW32__
 
64
/* Mingw doesn't have prototypes for these */
 
65
int _wspawnvpe (int, const wchar_t *, const wchar_t **, const wchar_t **);
 
66
int _wspawnvp (int, const wchar_t *, const wchar_t **);
 
67
int _wspawnve (int, const wchar_t *, const wchar_t **, const wchar_t **);
 
68
int _wspawnv (int, const wchar_t *, const wchar_t **);
 
69
#endif
 
70
 
 
71
#ifdef G_SPAWN_WIN32_DEBUG
 
72
  static int debug = 1;
 
73
  #define SETUP_DEBUG() /* empty */
 
74
#else
 
75
  static int debug = -1;
 
76
  #define SETUP_DEBUG()                                 \
 
77
    G_STMT_START                                        \
 
78
      {                                                 \
 
79
        if (debug == -1)                                \
 
80
          {                                             \
 
81
            if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
 
82
              debug = 1;                                \
 
83
            else                                        \
 
84
              debug = 0;                                \
 
85
          }                                             \
 
86
      }                                                 \
 
87
    G_STMT_END
 
88
#endif
 
89
 
 
90
enum
 
91
{
 
92
  CHILD_NO_ERROR,
 
93
  CHILD_CHDIR_FAILED,
 
94
  CHILD_SPAWN_FAILED,
 
95
};
 
96
 
 
97
enum {
 
98
  ARG_CHILD_ERR_REPORT = 1,
 
99
  ARG_STDIN,
 
100
  ARG_STDOUT,
 
101
  ARG_STDERR,
 
102
  ARG_WORKING_DIRECTORY,
 
103
  ARG_CLOSE_DESCRIPTORS,
 
104
  ARG_USE_PATH,
 
105
  ARG_WAIT,
 
106
  ARG_PROGRAM,
 
107
  ARG_COUNT = ARG_PROGRAM
 
108
};
 
109
 
 
110
static gchar *
 
111
protect_argv_string (const gchar *string)
 
112
{
 
113
  const gchar *p = string;
 
114
  gchar *retval, *q;
 
115
  gint len = 0;
 
116
  gboolean need_dblquotes = FALSE;
 
117
  while (*p)
 
118
    {
 
119
      if (*p == ' ' || *p == '\t')
 
120
        need_dblquotes = TRUE;
 
121
      else if (*p == '"')
 
122
        len++;
 
123
      else if (*p == '\\')
 
124
        {
 
125
          const gchar *pp = p;
 
126
          while (*pp && *pp == '\\')
 
127
            pp++;
 
128
          if (*pp == '"')
 
129
            len++;
 
130
        }
 
131
      len++;
 
132
      p++;
 
133
    }
 
134
  
 
135
  q = retval = g_malloc (len + need_dblquotes*2 + 1);
 
136
  p = string;
 
137
 
 
138
  if (need_dblquotes)
 
139
    *q++ = '"';
 
140
  
 
141
  while (*p)
 
142
    {
 
143
      if (*p == '"')
 
144
        *q++ = '\\';
 
145
      else if (*p == '\\')
 
146
        {
 
147
          const gchar *pp = p;
 
148
          while (*pp && *pp == '\\')
 
149
            pp++;
 
150
          if (*pp == '"')
 
151
            *q++ = '\\';
 
152
        }
 
153
      *q++ = *p;
 
154
      p++;
 
155
    }
 
156
  
 
157
  if (need_dblquotes)
 
158
    *q++ = '"';
 
159
  *q++ = '\0';
 
160
 
 
161
  return retval;
 
162
}
 
163
 
 
164
static gint
 
165
protect_argv (gchar  **argv,
 
166
              gchar ***new_argv)
 
167
{
 
168
  gint i;
 
169
  gint argc = 0;
 
170
  
 
171
  while (argv[argc])
 
172
    ++argc;
 
173
  *new_argv = g_new (gchar *, argc+1);
 
174
 
 
175
  /* Quote each argv element if necessary, so that it will get
 
176
   * reconstructed correctly in the C runtime startup code.  Note that
 
177
   * the unquoting algorithm in the C runtime is really weird, and
 
178
   * rather different than what Unix shells do. See stdargv.c in the C
 
179
   * runtime sources (in the Platform SDK, in src/crt).
 
180
   *
 
181
   * Note that an new_argv[0] constructed by this function should
 
182
   * *not* be passed as the filename argument to a spawn* or exec*
 
183
   * family function. That argument should be the real file name
 
184
   * without any quoting.
 
185
   */
 
186
  for (i = 0; i < argc; i++)
 
187
    (*new_argv)[i] = protect_argv_string (argv[i]);
 
188
 
 
189
  (*new_argv)[argc] = NULL;
 
190
 
 
191
  return argc;
 
192
}
 
193
 
 
194
#ifndef GSPAWN_HELPER
 
195
 
 
196
#define HELPER_PROCESS "gspawn-win32-helper"
 
197
 
 
198
GQuark
 
199
g_spawn_error_quark (void)
 
200
{
 
201
  return g_quark_from_static_string ("g-exec-error-quark");
 
202
}
 
203
 
 
204
gboolean
 
205
g_spawn_async_utf8 (const gchar          *working_directory,
 
206
                    gchar               **argv,
 
207
                    gchar               **envp,
 
208
                    GSpawnFlags           flags,
 
209
                    GSpawnChildSetupFunc  child_setup,
 
210
                    gpointer              user_data,
 
211
                    GPid                 *child_handle,
 
212
                    GError              **error)
 
213
{
 
214
  g_return_val_if_fail (argv != NULL, FALSE);
 
215
  
 
216
  return g_spawn_async_with_pipes_utf8 (working_directory,
 
217
                                        argv, envp,
 
218
                                        flags,
 
219
                                        child_setup,
 
220
                                        user_data,
 
221
                                        child_handle,
 
222
                                        NULL, NULL, NULL,
 
223
                                        error);
 
224
}
 
225
 
 
226
/* Avoids a danger in threaded situations (calling close()
 
227
 * on a file descriptor twice, and another thread has
 
228
 * re-opened it since the first close)
 
229
 */
 
230
static void
 
231
close_and_invalidate (gint *fd)
 
232
{
 
233
  if (*fd < 0)
 
234
    return;
 
235
 
 
236
  close (*fd);
 
237
  *fd = -1;
 
238
}
 
239
 
 
240
typedef enum
 
241
{
 
242
  READ_FAILED = 0, /* FALSE */
 
243
  READ_OK,
 
244
  READ_EOF
 
245
} ReadResult;
 
246
 
 
247
static ReadResult
 
248
read_data (GString     *str,
 
249
           GIOChannel  *iochannel,
 
250
           GError     **error)
 
251
{
 
252
  GIOStatus giostatus;
 
253
  gssize bytes;
 
254
  gchar buf[4096];
 
255
 
 
256
 again:
 
257
  
 
258
  giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
 
259
 
 
260
  if (bytes == 0)
 
261
    return READ_EOF;
 
262
  else if (bytes > 0)
 
263
    {
 
264
      g_string_append_len (str, buf, bytes);
 
265
      return READ_OK;
 
266
    }
 
267
  else if (giostatus == G_IO_STATUS_AGAIN)
 
268
    goto again;
 
269
  else if (giostatus == G_IO_STATUS_ERROR)
 
270
    {
 
271
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
 
272
                   _("Failed to read data from child process"));
 
273
      
 
274
      return READ_FAILED;
 
275
    }
 
276
  else
 
277
    return READ_OK;
 
278
}
 
279
 
 
280
static gboolean
 
281
make_pipe (gint     p[2],
 
282
           GError **error)
 
283
{
 
284
  if (pipe (p) < 0)
 
285
    {
 
286
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
287
                   _("Failed to create pipe for communicating with child process (%s)"),
 
288
                   g_strerror (errno));
 
289
      return FALSE;
 
290
    }
 
291
  else
 
292
    return TRUE;
 
293
}
 
294
 
 
295
/* The helper process writes a status report back to us, through a
 
296
 * pipe, consisting of two ints.
 
297
 */
 
298
static gboolean
 
299
read_helper_report (int      fd,
 
300
                    gint     report[2],
 
301
                    GError **error)
 
302
{
 
303
  gint bytes = 0;
 
304
  
 
305
  while (bytes < sizeof(gint)*2)
 
306
    {
 
307
      gint chunk;
 
308
 
 
309
      if (debug)
 
310
        g_print ("%s:read_helper_report: read %d...\n",
 
311
                 __FILE__,
 
312
                 sizeof(gint)*2 - bytes);
 
313
 
 
314
      chunk = read (fd, ((gchar*)report) + bytes,
 
315
                    sizeof(gint)*2 - bytes);
 
316
 
 
317
      if (debug)
 
318
        g_print ("...got %d bytes\n", chunk);
 
319
          
 
320
      if (chunk < 0)
 
321
        {
 
322
          /* Some weird shit happened, bail out */
 
323
              
 
324
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
325
                       _("Failed to read from child pipe (%s)"),
 
326
                       g_strerror (errno));
 
327
 
 
328
          return FALSE;
 
329
        }
 
330
      else if (chunk == 0)
 
331
        break; /* EOF */
 
332
      else
 
333
        bytes += chunk;
 
334
    }
 
335
 
 
336
  if (bytes < sizeof(gint)*2)
 
337
    return FALSE;
 
338
 
 
339
  return TRUE;
 
340
}
 
341
 
 
342
static void
 
343
set_child_error (gint         report[2],
 
344
                 const gchar *working_directory,
 
345
                 GError     **error)
 
346
{
 
347
  switch (report[0])
 
348
    {
 
349
    case CHILD_CHDIR_FAILED:
 
350
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
 
351
                   _("Failed to change to directory '%s' (%s)"),
 
352
                   working_directory,
 
353
                   g_strerror (report[1]));
 
354
      break;
 
355
    case CHILD_SPAWN_FAILED:
 
356
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
357
                   _("Failed to execute child process (%s)"),
 
358
                   g_strerror (report[1]));
 
359
      break;
 
360
    default:
 
361
      g_assert_not_reached ();
 
362
    }
 
363
}
 
364
 
 
365
static gboolean
 
366
utf8_charv_to_wcharv (char     **utf8_charv,
 
367
                      wchar_t ***wcharv,
 
368
                      int       *error_index,
 
369
                      GError   **error)
 
370
{
 
371
  wchar_t **retval = NULL;
 
372
 
 
373
  *wcharv = NULL;
 
374
  if (utf8_charv != NULL)
 
375
    {
 
376
      int n = 0, i;
 
377
 
 
378
      while (utf8_charv[n])
 
379
        n++;
 
380
      retval = g_new (wchar_t *, n + 1);
 
381
 
 
382
      for (i = 0; i < n; i++)
 
383
        {
 
384
          retval[i] = g_utf8_to_utf16 (utf8_charv[i], -1, NULL, NULL, error);
 
385
          if (retval[i] == NULL)
 
386
            {
 
387
              if (error_index)
 
388
                *error_index = i;
 
389
              while (i)
 
390
                g_free (retval[--i]);
 
391
              g_free (retval);
 
392
              return FALSE;
 
393
            }
 
394
        }
 
395
            
 
396
      retval[n] = NULL;
 
397
    }
 
398
  *wcharv = retval;
 
399
  return TRUE;
 
400
}
 
401
 
 
402
static gboolean
 
403
utf8_charv_to_cp_charv (char   **utf8_charv,
 
404
                        gchar ***cp_charv,
 
405
                        int     *error_index,
 
406
                        GError **error)
 
407
{
 
408
  char **retval = NULL;
 
409
 
 
410
  *cp_charv = NULL;
 
411
  if (utf8_charv != NULL)
 
412
    {
 
413
      int n = 0, i;
 
414
 
 
415
      while (utf8_charv[n])
 
416
        n++;
 
417
      retval = g_new (char *, n + 1);
 
418
 
 
419
      for (i = 0; i < n; i++)
 
420
        {
 
421
          retval[i] = g_locale_from_utf8 (utf8_charv[i], -1, NULL, NULL, error);
 
422
          if (retval[i] == NULL)
 
423
            {
 
424
              if (error_index)
 
425
                *error_index = i;
 
426
              while (i)
 
427
                g_free (retval[--i]);
 
428
              g_free (retval);
 
429
              return FALSE;
 
430
            }
 
431
        }
 
432
      retval[n] = NULL;
 
433
    }
 
434
 
 
435
  *cp_charv = retval;
 
436
  return TRUE;
 
437
}
 
438
 
 
439
static gboolean
 
440
do_spawn_directly (gint                 *exit_status,
 
441
                   gboolean              do_return_handle,
 
442
                   GSpawnFlags           flags,
 
443
                   gchar               **argv,
 
444
                   char                **envp,
 
445
                   char                **protected_argv,
 
446
                   GSpawnChildSetupFunc  child_setup,
 
447
                   gpointer              user_data,
 
448
                   GPid                 *child_handle,
 
449
                   GError              **error)     
 
450
{
 
451
  const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
 
452
  char **new_argv;
 
453
  int rc = -1;
 
454
  int saved_errno;
 
455
  GError *conv_error = NULL;
 
456
  gint conv_error_index;
 
457
 
 
458
  new_argv = (flags & G_SPAWN_FILE_AND_ARGV_ZERO) ? protected_argv + 1 : protected_argv;
 
459
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
460
    {
 
461
      wchar_t *wargv0, **wargv, **wenvp;
 
462
      
 
463
      wargv0 = g_utf8_to_utf16 (argv[0], -1, NULL, NULL, &conv_error);
 
464
      if (wargv0 == NULL)
 
465
        {
 
466
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
467
                       _("Invalid program name: %s"),
 
468
                       conv_error->message);
 
469
          g_error_free (conv_error);
 
470
          
 
471
          return FALSE;
 
472
        }
 
473
      
 
474
      if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
 
475
        {
 
476
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
477
                       _("Invalid string in argument vector at %d: %s"),
 
478
                       conv_error_index, conv_error->message);
 
479
          g_error_free (conv_error);
 
480
          g_free (wargv0);
 
481
          
 
482
          return FALSE;
 
483
        }
 
484
      
 
485
      if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
 
486
        {
 
487
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
488
                       _("Invalid string in environment: %s"),
 
489
                       conv_error->message);
 
490
          g_error_free (conv_error);
 
491
          g_free (wargv0);
 
492
          g_strfreev ((gchar **) wargv);
 
493
          
 
494
          return FALSE;
 
495
        }
 
496
      
 
497
      if (child_setup)
 
498
        (* child_setup) (user_data);
 
499
      
 
500
      if (flags & G_SPAWN_SEARCH_PATH)
 
501
        if (wenvp != NULL)
 
502
          rc = _wspawnvpe (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
 
503
        else
 
504
          rc = _wspawnvp (mode, wargv0, (const wchar_t **) wargv);
 
505
      else
 
506
        if (wenvp != NULL)
 
507
          rc = _wspawnve (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
 
508
        else
 
509
          rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
 
510
      
 
511
      g_free (wargv0);
 
512
      g_strfreev ((gchar **) wargv);
 
513
      g_strfreev ((gchar **) wenvp);
 
514
    }
 
515
  else
 
516
    {
 
517
      char *cpargv0, **cpargv, **cpenvp;
 
518
      
 
519
      cpargv0 = g_locale_from_utf8 (argv[0], -1, NULL, NULL, &conv_error);
 
520
      if (cpargv0 == NULL)
 
521
        {
 
522
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
523
                       _("Invalid program name: %s"),
 
524
                       conv_error->message);
 
525
          g_error_free (conv_error);
 
526
 
 
527
          return FALSE;
 
528
        }
 
529
 
 
530
      if  (!utf8_charv_to_cp_charv (new_argv, &cpargv, &conv_error_index, &conv_error))
 
531
        {
 
532
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
533
                       _("Invalid string in argument vector at %d: %s"),
 
534
                       conv_error_index, conv_error->message);
 
535
          g_error_free (conv_error);
 
536
          g_free (cpargv0);
 
537
 
 
538
          return FALSE;
 
539
        }
 
540
 
 
541
      if (!utf8_charv_to_cp_charv (envp, &cpenvp, NULL, &conv_error))
 
542
        {
 
543
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
544
                       _("Invalid string in environment: %s"),
 
545
                       conv_error->message);
 
546
          g_error_free (conv_error);
 
547
          g_free (cpargv0);
 
548
          g_strfreev (cpargv);
 
549
 
 
550
          return FALSE;
 
551
        }
 
552
 
 
553
      if (child_setup)
 
554
        (* child_setup) (user_data);
 
555
 
 
556
      if (flags & G_SPAWN_SEARCH_PATH)
 
557
        if (cpenvp != NULL)
 
558
          rc = spawnvpe (mode, cpargv0, (const char **) cpargv, (const char **) cpenvp);
 
559
        else
 
560
          rc = spawnvp (mode, cpargv0, (const char **) cpargv);
 
561
      else
 
562
        if (envp != NULL)
 
563
          rc = spawnve (mode, cpargv0, (const char **) cpargv, (const char **) cpenvp);
 
564
        else
 
565
          rc = spawnv (mode, cpargv0, (const char **) cpargv);
 
566
 
 
567
      g_free (cpargv0);
 
568
      g_strfreev (cpargv);
 
569
      g_strfreev (cpenvp);
 
570
    }
 
571
 
 
572
  saved_errno = errno;
 
573
 
 
574
  if (rc == -1 && saved_errno != 0)
 
575
    {
 
576
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
577
                   _("Failed to execute child process (%s)"),
 
578
                   g_strerror (saved_errno));
 
579
      return FALSE;
 
580
    }
 
581
 
 
582
  if (exit_status == NULL)
 
583
    {
 
584
      if (child_handle && do_return_handle)
 
585
        *child_handle = (GPid) rc;
 
586
      else
 
587
        {
 
588
          CloseHandle ((HANDLE) rc);
 
589
          if (child_handle)
 
590
            *child_handle = 0;
 
591
        }
 
592
    }
 
593
  else
 
594
    *exit_status = rc;
 
595
 
 
596
  return TRUE;
 
597
}
 
598
 
 
599
static gboolean
 
600
do_spawn_with_pipes (gint                 *exit_status,
 
601
                     gboolean              do_return_handle,
 
602
                     const gchar          *working_directory,
 
603
                     gchar               **argv,
 
604
                     char                **envp,
 
605
                     GSpawnFlags           flags,
 
606
                     GSpawnChildSetupFunc  child_setup,
 
607
                     gpointer              user_data,
 
608
                     GPid                 *child_handle,
 
609
                     gint                 *standard_input,
 
610
                     gint                 *standard_output,
 
611
                     gint                 *standard_error,
 
612
                     gint                 *err_report,
 
613
                     GError              **error)     
 
614
{
 
615
  char **protected_argv;
 
616
  char args[ARG_COUNT][10];
 
617
  char **new_argv;
 
618
  int i;
 
619
  int rc = -1;
 
620
  int saved_errno;
 
621
  int argc;
 
622
  int stdin_pipe[2] = { -1, -1 };
 
623
  int stdout_pipe[2] = { -1, -1 };
 
624
  int stderr_pipe[2] = { -1, -1 };
 
625
  int child_err_report_pipe[2] = { -1, -1 };
 
626
  int helper_report[2];
 
627
  static gboolean warned_about_child_setup = FALSE;
 
628
  GError *conv_error = NULL;
 
629
  gint conv_error_index;
 
630
  gchar *helper_process;
 
631
  CONSOLE_CURSOR_INFO cursor_info;
 
632
  
 
633
  SETUP_DEBUG();
 
634
 
 
635
  if (child_setup && !warned_about_child_setup)
 
636
    {
 
637
      warned_about_child_setup = TRUE;
 
638
      g_warning ("passing a child setup function to the g_spawn functions is pointless and dangerous on Win32");
 
639
    }
 
640
 
 
641
  argc = protect_argv (argv, &protected_argv);
 
642
 
 
643
  if (!standard_input && !standard_output && !standard_error &&
 
644
      (flags & G_SPAWN_CHILD_INHERITS_STDIN) &&
 
645
      !(flags & G_SPAWN_STDOUT_TO_DEV_NULL) &&
 
646
      !(flags & G_SPAWN_STDERR_TO_DEV_NULL) &&
 
647
      (working_directory == NULL || !*working_directory) &&
 
648
      (flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
 
649
    {
 
650
      /* We can do without the helper process */
 
651
      gboolean retval =
 
652
        do_spawn_directly (exit_status, do_return_handle, flags,
 
653
                           argv, envp, protected_argv,
 
654
                           child_setup, user_data, child_handle,
 
655
                           error);
 
656
      g_strfreev (protected_argv);
 
657
      return retval;
 
658
    }
 
659
 
 
660
  if (standard_input && !make_pipe (stdin_pipe, error))
 
661
    goto cleanup_and_fail;
 
662
  
 
663
  if (standard_output && !make_pipe (stdout_pipe, error))
 
664
    goto cleanup_and_fail;
 
665
  
 
666
  if (standard_error && !make_pipe (stderr_pipe, error))
 
667
    goto cleanup_and_fail;
 
668
  
 
669
  if (!make_pipe (child_err_report_pipe, error))
 
670
    goto cleanup_and_fail;
 
671
  
 
672
  new_argv = g_new (char *, argc + 1 + ARG_COUNT);
 
673
  if (GetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info))
 
674
    helper_process = HELPER_PROCESS "-console.exe";
 
675
  else
 
676
    helper_process = HELPER_PROCESS ".exe";
 
677
  new_argv[0] = helper_process;
 
678
  _g_sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_pipe[1]);
 
679
  new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
 
680
  
 
681
  if (flags & G_SPAWN_FILE_AND_ARGV_ZERO)
 
682
    {
 
683
      /* Overload ARG_CHILD_ERR_REPORT to also encode the
 
684
       * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
 
685
       */
 
686
      strcat (args[ARG_CHILD_ERR_REPORT], "#");
 
687
    }
 
688
  
 
689
  if (standard_input)
 
690
    {
 
691
      _g_sprintf (args[ARG_STDIN], "%d", stdin_pipe[0]);
 
692
      new_argv[ARG_STDIN] = args[ARG_STDIN];
 
693
    }
 
694
  else if (flags & G_SPAWN_CHILD_INHERITS_STDIN)
 
695
    {
 
696
      /* Let stdin be alone */
 
697
      new_argv[ARG_STDIN] = "-";
 
698
    }
 
699
  else
 
700
    {
 
701
      /* Keep process from blocking on a read of stdin */
 
702
      new_argv[ARG_STDIN] = "z";
 
703
    }
 
704
  
 
705
  if (standard_output)
 
706
    {
 
707
      _g_sprintf (args[ARG_STDOUT], "%d", stdout_pipe[1]);
 
708
      new_argv[ARG_STDOUT] = args[ARG_STDOUT];
 
709
    }
 
710
  else if (flags & G_SPAWN_STDOUT_TO_DEV_NULL)
 
711
    {
 
712
      new_argv[ARG_STDOUT] = "z";
 
713
    }
 
714
  else
 
715
    {
 
716
      new_argv[ARG_STDOUT] = "-";
 
717
    }
 
718
  
 
719
  if (standard_error)
 
720
    {
 
721
      _g_sprintf (args[ARG_STDERR], "%d", stderr_pipe[1]);
 
722
      new_argv[ARG_STDERR] = args[ARG_STDERR];
 
723
    }
 
724
  else if (flags & G_SPAWN_STDERR_TO_DEV_NULL)
 
725
    {
 
726
      new_argv[ARG_STDERR] = "z";
 
727
    }
 
728
  else
 
729
    {
 
730
      new_argv[ARG_STDERR] = "-";
 
731
    }
 
732
  
 
733
  if (working_directory && *working_directory)
 
734
    new_argv[ARG_WORKING_DIRECTORY] = protect_argv_string (working_directory);
 
735
  else
 
736
    new_argv[ARG_WORKING_DIRECTORY] = g_strdup ("-");
 
737
  
 
738
  if (!(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
 
739
    new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
 
740
  else
 
741
    new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
 
742
 
 
743
  if (flags & G_SPAWN_SEARCH_PATH)
 
744
    new_argv[ARG_USE_PATH] = "y";
 
745
  else
 
746
    new_argv[ARG_USE_PATH] = "-";
 
747
 
 
748
  if (exit_status == NULL)
 
749
    new_argv[ARG_WAIT] = "-";
 
750
  else
 
751
    new_argv[ARG_WAIT] = "w";
 
752
 
 
753
  for (i = 0; i <= argc; i++)
 
754
    new_argv[ARG_PROGRAM + i] = protected_argv[i];
 
755
 
 
756
  if (debug)
 
757
    {
 
758
      g_print ("calling %s with argv:\n", helper_process);
 
759
      for (i = 0; i < argc + 1 + ARG_COUNT; i++)
 
760
        g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
 
761
    }
 
762
 
 
763
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
764
    {
 
765
      wchar_t *whelper = g_utf8_to_utf16 (helper_process, -1, NULL, NULL, NULL);
 
766
      wchar_t **wargv, **wenvp;
 
767
 
 
768
      if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
 
769
        {
 
770
          if (conv_error_index == ARG_WORKING_DIRECTORY)
 
771
            g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
 
772
                         _("Invalid working directory: %s"),
 
773
                         conv_error->message);
 
774
          else
 
775
            g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
776
                         _("Invalid string in argument vector at %d: %s"),
 
777
                         conv_error_index - ARG_PROGRAM, conv_error->message);
 
778
          g_error_free (conv_error);
 
779
          g_strfreev (protected_argv);
 
780
          g_free (new_argv[ARG_WORKING_DIRECTORY]);
 
781
          g_free (new_argv);
 
782
          g_free (whelper);
 
783
          
 
784
          return FALSE;
 
785
        }
 
786
      
 
787
      if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
 
788
        {
 
789
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
790
                       _("Invalid string in environment: %s"),
 
791
                       conv_error->message);
 
792
          g_error_free (conv_error);
 
793
          g_strfreev (protected_argv);
 
794
          g_free (new_argv[ARG_WORKING_DIRECTORY]);
 
795
          g_free (new_argv);
 
796
          g_free (whelper);
 
797
          g_strfreev ((gchar **) wargv);
 
798
          
 
799
          return FALSE;
 
800
        }
 
801
 
 
802
      if (child_setup)
 
803
        (* child_setup) (user_data);
 
804
 
 
805
      if (wenvp != NULL)
 
806
        /* Let's hope envp hasn't mucked with PATH so that
 
807
         * gspawn-win32-helper.exe isn't found.
 
808
         */
 
809
        rc = _wspawnvpe (P_NOWAIT, whelper, (const wchar_t **) wargv, (const wchar_t **) wenvp);
 
810
      else
 
811
        rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
 
812
 
 
813
      saved_errno = errno;
 
814
 
 
815
      g_free (whelper);
 
816
      g_strfreev ((gchar **) wargv);
 
817
      g_strfreev ((gchar **) wenvp);
 
818
    }
 
819
  else
 
820
    {
 
821
      char **cpargv, **cpenvp;
 
822
 
 
823
      if (!utf8_charv_to_cp_charv (new_argv, &cpargv, &conv_error_index, &conv_error))
 
824
        {
 
825
          if (conv_error_index == ARG_WORKING_DIRECTORY)
 
826
            g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
 
827
                         _("Invalid working directory: %s"),
 
828
                         conv_error->message);
 
829
          else
 
830
            g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
831
                         _("Invalid string in argument vector at %d: %s"),
 
832
                         conv_error_index - ARG_PROGRAM, conv_error->message);
 
833
          g_error_free (conv_error);
 
834
          g_strfreev (protected_argv);
 
835
          g_free (new_argv[ARG_WORKING_DIRECTORY]);
 
836
          g_free (new_argv);
 
837
          
 
838
          return FALSE;
 
839
        }
 
840
        
 
841
      if (!utf8_charv_to_cp_charv (envp, &cpenvp, NULL, &conv_error))
 
842
        {
 
843
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
844
                       _("Invalid string in environment: %s"),
 
845
                       conv_error->message);
 
846
          g_error_free (conv_error);
 
847
          g_strfreev (protected_argv);
 
848
          g_free (new_argv[ARG_WORKING_DIRECTORY]);
 
849
          g_free (new_argv);
 
850
          g_strfreev (cpargv);
 
851
          
 
852
          return FALSE;
 
853
        }
 
854
 
 
855
      if (child_setup)
 
856
        (* child_setup) (user_data);
 
857
 
 
858
      if (cpenvp != NULL)
 
859
        rc = spawnvpe (P_NOWAIT, helper_process, (const char **) cpargv, (const char **) cpenvp);
 
860
      else
 
861
        rc = spawnvp (P_NOWAIT, helper_process, (const char **) cpargv);
 
862
 
 
863
      saved_errno = errno;
 
864
 
 
865
      g_strfreev (cpargv);
 
866
      g_strfreev (cpenvp);
 
867
    }
 
868
 
 
869
  /* Close the other process's ends of the pipes in this process,
 
870
   * otherwise the reader will never get EOF.
 
871
   */
 
872
  close_and_invalidate (&child_err_report_pipe[1]);
 
873
  close_and_invalidate (&stdin_pipe[0]);
 
874
  close_and_invalidate (&stdout_pipe[1]);
 
875
  close_and_invalidate (&stderr_pipe[1]);
 
876
 
 
877
  g_strfreev (protected_argv);
 
878
 
 
879
  g_free (new_argv[ARG_WORKING_DIRECTORY]);
 
880
  g_free (new_argv);
 
881
 
 
882
  /* Check if gspawn-win32-helper couldn't be run */
 
883
  if (rc == -1 && saved_errno != 0)
 
884
    {
 
885
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
886
                   _("Failed to execute helper program (%s)"),
 
887
                   g_strerror (saved_errno));
 
888
      goto cleanup_and_fail;
 
889
    }
 
890
 
 
891
  if (exit_status != NULL)
 
892
    {
 
893
      /* Synchronous case. Pass helper's report pipe back to caller,
 
894
       * which takes care of reading it after the grandchild has
 
895
       * finished.
 
896
       */
 
897
      g_assert (err_report != NULL);
 
898
      *err_report = child_err_report_pipe[0];
 
899
    }
 
900
  else
 
901
    {
 
902
      /* Asynchronous case. We read the helper's report right away. */
 
903
      if (!read_helper_report (child_err_report_pipe[0], helper_report, error))
 
904
        goto cleanup_and_fail;
 
905
        
 
906
      close (child_err_report_pipe[0]);
 
907
 
 
908
      switch (helper_report[0])
 
909
        {
 
910
        case CHILD_NO_ERROR:
 
911
          if (child_handle && do_return_handle)
 
912
            {
 
913
              /* rc is our HANDLE for gspawn-win32-helper. It has
 
914
               * told us the HANDLE of its child. Duplicate that into
 
915
               * a HANDLE valid in this process.
 
916
               */
 
917
              if (!DuplicateHandle ((HANDLE) rc, (HANDLE) helper_report[1],
 
918
                                    GetCurrentProcess (), (LPHANDLE) child_handle,
 
919
                                    0, TRUE, DUPLICATE_SAME_ACCESS))
 
920
                *child_handle = 0;
 
921
            }
 
922
          else if (child_handle)
 
923
            *child_handle = 0;
 
924
          break;
 
925
          
 
926
        default:
 
927
          set_child_error (helper_report, working_directory, error);
 
928
          goto cleanup_and_fail;
 
929
        }
 
930
    }
 
931
 
 
932
  /* Success against all odds! return the information */
 
933
      
 
934
  if (standard_input)
 
935
    *standard_input = stdin_pipe[1];
 
936
  if (standard_output)
 
937
    *standard_output = stdout_pipe[0];
 
938
  if (standard_error)
 
939
    *standard_error = stderr_pipe[0];
 
940
  if (rc != -1)
 
941
    CloseHandle ((HANDLE) rc);
 
942
  
 
943
  return TRUE;
 
944
 
 
945
 cleanup_and_fail:
 
946
  if (rc != -1)
 
947
    CloseHandle ((HANDLE) rc);
 
948
  if (child_err_report_pipe[0] != -1)
 
949
    close (child_err_report_pipe[0]);
 
950
  if (child_err_report_pipe[1] != -1)
 
951
    close (child_err_report_pipe[1]);
 
952
  if (stdin_pipe[0] != -1)
 
953
    close (stdin_pipe[0]);
 
954
  if (stdin_pipe[1] != -1)
 
955
    close (stdin_pipe[1]);
 
956
  if (stdout_pipe[0] != -1)
 
957
    close (stdout_pipe[0]);
 
958
  if (stdout_pipe[1] != -1)
 
959
    close (stdout_pipe[1]);
 
960
  if (stderr_pipe[0] != -1)
 
961
    close (stderr_pipe[0]);
 
962
  if (stderr_pipe[1] != -1)
 
963
    close (stderr_pipe[1]);
 
964
 
 
965
  return FALSE;
 
966
}
 
967
 
 
968
gboolean
 
969
g_spawn_sync_utf8 (const gchar          *working_directory,
 
970
                   gchar               **argv,
 
971
                   gchar               **envp,
 
972
                   GSpawnFlags           flags,
 
973
                   GSpawnChildSetupFunc  child_setup,
 
974
                   gpointer              user_data,
 
975
                   gchar               **standard_output,
 
976
                   gchar               **standard_error,
 
977
                   gint                 *exit_status,
 
978
                   GError              **error)     
 
979
{
 
980
  gint outpipe = -1;
 
981
  gint errpipe = -1;
 
982
  gint reportpipe = -1;
 
983
  GIOChannel *outchannel = NULL;
 
984
  GIOChannel *errchannel = NULL;
 
985
  GPollFD outfd, errfd;
 
986
  GPollFD fds[2];
 
987
  gint nfds;
 
988
  gint outindex = -1;
 
989
  gint errindex = -1;
 
990
  gint ret;
 
991
  GString *outstr = NULL;
 
992
  GString *errstr = NULL;
 
993
  gboolean failed;
 
994
  gint status;
 
995
  
 
996
  g_return_val_if_fail (argv != NULL, FALSE);
 
997
  g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
 
998
  g_return_val_if_fail (standard_output == NULL ||
 
999
                        !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
 
1000
  g_return_val_if_fail (standard_error == NULL ||
 
1001
                        !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
 
1002
  
 
1003
  /* Just to ensure segfaults if callers try to use
 
1004
   * these when an error is reported.
 
1005
   */
 
1006
  if (standard_output)
 
1007
    *standard_output = NULL;
 
1008
 
 
1009
  if (standard_error)
 
1010
    *standard_error = NULL;
 
1011
  
 
1012
  if (!do_spawn_with_pipes (&status,
 
1013
                            FALSE,
 
1014
                            working_directory,
 
1015
                            argv,
 
1016
                            envp,
 
1017
                            flags,
 
1018
                            child_setup,
 
1019
                            user_data,
 
1020
                            NULL,
 
1021
                            NULL,
 
1022
                            standard_output ? &outpipe : NULL,
 
1023
                            standard_error ? &errpipe : NULL,
 
1024
                            &reportpipe,
 
1025
                            error))
 
1026
    return FALSE;
 
1027
 
 
1028
  /* Read data from child. */
 
1029
  
 
1030
  failed = FALSE;
 
1031
 
 
1032
  if (outpipe >= 0)
 
1033
    {
 
1034
      outstr = g_string_new (NULL);
 
1035
      outchannel = g_io_channel_win32_new_fd (outpipe);
 
1036
      g_io_channel_set_encoding (outchannel, NULL, NULL);
 
1037
      g_io_channel_set_buffered (outchannel, FALSE);
 
1038
      g_io_channel_win32_make_pollfd (outchannel,
 
1039
                                      G_IO_IN | G_IO_ERR | G_IO_HUP,
 
1040
                                      &outfd);
 
1041
      if (debug)
 
1042
        g_print ("outfd=%x\n", outfd.fd);
 
1043
    }
 
1044
      
 
1045
  if (errpipe >= 0)
 
1046
    {
 
1047
      errstr = g_string_new (NULL);
 
1048
      errchannel = g_io_channel_win32_new_fd (errpipe);
 
1049
      g_io_channel_set_encoding (errchannel, NULL, NULL);
 
1050
      g_io_channel_set_buffered (errchannel, FALSE);
 
1051
      g_io_channel_win32_make_pollfd (errchannel,
 
1052
                                      G_IO_IN | G_IO_ERR | G_IO_HUP,
 
1053
                                      &errfd);
 
1054
      if (debug)
 
1055
        g_print ("errfd=%x\n", errfd.fd);
 
1056
    }
 
1057
 
 
1058
  /* Read data until we get EOF on all pipes. */
 
1059
  while (!failed && (outpipe >= 0 || errpipe >= 0))
 
1060
    {
 
1061
      nfds = 0;
 
1062
      if (outpipe >= 0)
 
1063
        {
 
1064
          fds[nfds] = outfd;
 
1065
          outindex = nfds;
 
1066
          nfds++;
 
1067
        }
 
1068
      if (errpipe >= 0)
 
1069
        {
 
1070
          fds[nfds] = errfd;
 
1071
          errindex = nfds;
 
1072
          nfds++;
 
1073
        }
 
1074
 
 
1075
      if (debug)
 
1076
        g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
 
1077
                 nfds);
 
1078
 
 
1079
      ret = g_io_channel_win32_poll (fds, nfds, -1);
 
1080
 
 
1081
      if (ret < 0)
 
1082
        {
 
1083
          failed = TRUE;
 
1084
 
 
1085
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
 
1086
                       _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
 
1087
          
 
1088
          break;
 
1089
        }
 
1090
 
 
1091
      if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
 
1092
        {
 
1093
          switch (read_data (outstr, outchannel, error))
 
1094
            {
 
1095
            case READ_FAILED:
 
1096
              if (debug)
 
1097
                g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
 
1098
              failed = TRUE;
 
1099
              break;
 
1100
            case READ_EOF:
 
1101
              if (debug)
 
1102
                g_print ("g_spawn_sync: outchannel: READ_EOF\n");
 
1103
              g_io_channel_unref (outchannel);
 
1104
              outchannel = NULL;
 
1105
              close_and_invalidate (&outpipe);
 
1106
              break;
 
1107
            default:
 
1108
              if (debug)
 
1109
                g_print ("g_spawn_sync: outchannel: OK\n");
 
1110
              break;
 
1111
            }
 
1112
 
 
1113
          if (failed)
 
1114
            break;
 
1115
        }
 
1116
 
 
1117
      if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
 
1118
        {
 
1119
          switch (read_data (errstr, errchannel, error))
 
1120
            {
 
1121
            case READ_FAILED:
 
1122
              if (debug)
 
1123
                g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
 
1124
              failed = TRUE;
 
1125
              break;
 
1126
            case READ_EOF:
 
1127
              if (debug)
 
1128
                g_print ("g_spawn_sync: errchannel: READ_EOF\n");
 
1129
              g_io_channel_unref (errchannel);
 
1130
              errchannel = NULL;
 
1131
              close_and_invalidate (&errpipe);
 
1132
              break;
 
1133
            default:
 
1134
              if (debug)
 
1135
                g_print ("g_spawn_sync: errchannel: OK\n");
 
1136
              break;
 
1137
            }
 
1138
 
 
1139
          if (failed)
 
1140
            break;
 
1141
        }
 
1142
    }
 
1143
 
 
1144
  if (reportpipe == -1)
 
1145
    {
 
1146
      /* No helper process, exit status of actual spawned process
 
1147
       * already available.
 
1148
       */
 
1149
      if (exit_status)
 
1150
        *exit_status = status;
 
1151
    }
 
1152
  else
 
1153
    {
 
1154
      /* Helper process was involved. Read its report now after the
 
1155
       * grandchild has finished.
 
1156
       */
 
1157
      gint helper_report[2];
 
1158
 
 
1159
      if (!read_helper_report (reportpipe, helper_report, error))
 
1160
        failed = TRUE;
 
1161
      else
 
1162
        {
 
1163
          switch (helper_report[0])
 
1164
            {
 
1165
            case CHILD_NO_ERROR:
 
1166
              if (exit_status)
 
1167
                *exit_status = helper_report[1];
 
1168
              break;
 
1169
            default:
 
1170
              set_child_error (helper_report, working_directory, error);
 
1171
              failed = TRUE;
 
1172
              break;
 
1173
            }
 
1174
        }
 
1175
      close_and_invalidate (&reportpipe);
 
1176
    }
 
1177
 
 
1178
 
 
1179
  /* These should only be open still if we had an error.  */
 
1180
  
 
1181
  if (outchannel != NULL)
 
1182
    g_io_channel_unref (outchannel);
 
1183
  if (errchannel != NULL)
 
1184
    g_io_channel_unref (errchannel);
 
1185
  if (outpipe >= 0)
 
1186
    close_and_invalidate (&outpipe);
 
1187
  if (errpipe >= 0)
 
1188
    close_and_invalidate (&errpipe);
 
1189
  
 
1190
  if (failed)
 
1191
    {
 
1192
      if (outstr)
 
1193
        g_string_free (outstr, TRUE);
 
1194
      if (errstr)
 
1195
        g_string_free (errstr, TRUE);
 
1196
 
 
1197
      return FALSE;
 
1198
    }
 
1199
  else
 
1200
    {
 
1201
      if (standard_output)        
 
1202
        *standard_output = g_string_free (outstr, FALSE);
 
1203
 
 
1204
      if (standard_error)
 
1205
        *standard_error = g_string_free (errstr, FALSE);
 
1206
 
 
1207
      return TRUE;
 
1208
    }
 
1209
}
 
1210
 
 
1211
gboolean
 
1212
g_spawn_async_with_pipes_utf8 (const gchar          *working_directory,
 
1213
                               gchar               **argv,
 
1214
                               gchar               **envp,
 
1215
                               GSpawnFlags           flags,
 
1216
                               GSpawnChildSetupFunc  child_setup,
 
1217
                               gpointer              user_data,
 
1218
                               GPid                 *child_handle,
 
1219
                               gint                 *standard_input,
 
1220
                               gint                 *standard_output,
 
1221
                               gint                 *standard_error,
 
1222
                               GError              **error)
 
1223
{
 
1224
  g_return_val_if_fail (argv != NULL, FALSE);
 
1225
  g_return_val_if_fail (standard_output == NULL ||
 
1226
                        !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
 
1227
  g_return_val_if_fail (standard_error == NULL ||
 
1228
                        !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
 
1229
  /* can't inherit stdin if we have an input pipe. */
 
1230
  g_return_val_if_fail (standard_input == NULL ||
 
1231
                        !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
 
1232
  
 
1233
  return do_spawn_with_pipes (NULL,
 
1234
                              (flags & G_SPAWN_DO_NOT_REAP_CHILD),
 
1235
                              working_directory,
 
1236
                              argv,
 
1237
                              envp,
 
1238
                              flags,
 
1239
                              child_setup,
 
1240
                              user_data,
 
1241
                              child_handle,
 
1242
                              standard_input,
 
1243
                              standard_output,
 
1244
                              standard_error,
 
1245
                              NULL,
 
1246
                              error);
 
1247
}
 
1248
 
 
1249
gboolean
 
1250
g_spawn_command_line_sync_utf8 (const gchar  *command_line,
 
1251
                                gchar       **standard_output,
 
1252
                                gchar       **standard_error,
 
1253
                                gint         *exit_status,
 
1254
                                GError      **error)
 
1255
{
 
1256
  gboolean retval;
 
1257
  gchar **argv = 0;
 
1258
 
 
1259
  g_return_val_if_fail (command_line != NULL, FALSE);
 
1260
  
 
1261
  if (!g_shell_parse_argv (command_line,
 
1262
                           NULL, &argv,
 
1263
                           error))
 
1264
    return FALSE;
 
1265
  
 
1266
  retval = g_spawn_sync_utf8 (NULL,
 
1267
                              argv,
 
1268
                              NULL,
 
1269
                              G_SPAWN_SEARCH_PATH,
 
1270
                              NULL,
 
1271
                              NULL,
 
1272
                              standard_output,
 
1273
                              standard_error,
 
1274
                              exit_status,
 
1275
                              error);
 
1276
  g_strfreev (argv);
 
1277
 
 
1278
  return retval;
 
1279
}
 
1280
 
 
1281
gboolean
 
1282
g_spawn_command_line_async_utf8 (const gchar *command_line,
 
1283
                                 GError     **error)
 
1284
{
 
1285
  gboolean retval;
 
1286
  gchar **argv = 0;
 
1287
 
 
1288
  g_return_val_if_fail (command_line != NULL, FALSE);
 
1289
 
 
1290
  if (!g_shell_parse_argv (command_line,
 
1291
                           NULL, &argv,
 
1292
                           error))
 
1293
    return FALSE;
 
1294
  
 
1295
  retval = g_spawn_async_utf8 (NULL,
 
1296
                               argv,
 
1297
                               NULL,
 
1298
                               G_SPAWN_SEARCH_PATH,
 
1299
                               NULL,
 
1300
                               NULL,
 
1301
                               NULL,
 
1302
                               error);
 
1303
  g_strfreev (argv);
 
1304
 
 
1305
  return retval;
 
1306
}
 
1307
 
 
1308
void
 
1309
g_spawn_close_pid (GPid pid)
 
1310
{
 
1311
    CloseHandle (pid);
 
1312
}
 
1313
 
 
1314
/* Binary compatibility versions that take system codepage pathnames,
 
1315
 * argument vectors and environments. These get used only by code
 
1316
 * built against 2.8.1 or earlier. Code built against 2.8.2 or later
 
1317
 * will use the _utf8 versions above (see the #defines in gspawn.h).
 
1318
 */
 
1319
 
 
1320
#undef g_spawn_async
 
1321
#undef g_spawn_async_with_pipes
 
1322
#undef g_spawn_sync
 
1323
#undef g_spawn_command_line_sync
 
1324
#undef g_spawn_command_line_async
 
1325
 
 
1326
static gboolean
 
1327
setup_utf8_copies (const gchar *working_directory,
 
1328
                   gchar      **utf8_working_directory,
 
1329
                   gchar      **argv,
 
1330
                   gchar     ***utf8_argv,
 
1331
                   gchar      **envp,
 
1332
                   gchar     ***utf8_envp,
 
1333
                   GError     **error)
 
1334
{
 
1335
  gint i, argc, envc;
 
1336
 
 
1337
  if (working_directory == NULL)
 
1338
    *utf8_working_directory = NULL;
 
1339
  else
 
1340
    {
 
1341
      GError *conv_error = NULL;
 
1342
      
 
1343
      *utf8_working_directory = g_locale_to_utf8 (working_directory, -1, NULL, NULL, &conv_error);
 
1344
      if (*utf8_working_directory == NULL)
 
1345
        {
 
1346
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
 
1347
                       _("Invalid working directory: %s"),
 
1348
                       conv_error->message);
 
1349
          g_error_free (conv_error);
 
1350
          return FALSE;
 
1351
        }
 
1352
    }
 
1353
 
 
1354
  argc = 0;
 
1355
  while (argv[argc])
 
1356
    ++argc;
 
1357
  *utf8_argv = g_new (gchar *, argc + 1);
 
1358
  for (i = 0; i < argc; i++)
 
1359
    {
 
1360
      GError *conv_error = NULL;
 
1361
 
 
1362
      (*utf8_argv)[i] = g_locale_to_utf8 (argv[i], -1, NULL, NULL, &conv_error);
 
1363
      if ((*utf8_argv)[i] == NULL)
 
1364
        {
 
1365
          g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
1366
                       _("Invalid string in argument vector at %d: %s"),
 
1367
                       i, conv_error->message);
 
1368
          g_error_free (conv_error);
 
1369
          
 
1370
          g_strfreev (*utf8_argv);
 
1371
          *utf8_argv = NULL;
 
1372
 
 
1373
          g_free (*utf8_working_directory);
 
1374
          *utf8_working_directory = NULL;
 
1375
 
 
1376
          return FALSE;
 
1377
        }
 
1378
    }
 
1379
  (*utf8_argv)[argc] = NULL;
 
1380
 
 
1381
  if (envp == NULL)
 
1382
    {
 
1383
      *utf8_envp = NULL;
 
1384
    }
 
1385
  else
 
1386
    {
 
1387
      envc = 0;
 
1388
      while (envp[envc])
 
1389
        ++envc;
 
1390
      *utf8_envp = g_new (gchar *, envc + 1);
 
1391
      for (i = 0; i < envc; i++)
 
1392
        {
 
1393
          GError *conv_error = NULL;
 
1394
 
 
1395
          (*utf8_envp)[i] = g_locale_to_utf8 (envp[i], -1, NULL, NULL, &conv_error);
 
1396
          if ((*utf8_envp)[i] == NULL)
 
1397
            {   
 
1398
              g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
1399
                           _("Invalid string in environment: %s"),
 
1400
                           conv_error->message);
 
1401
              g_error_free (conv_error);
 
1402
 
 
1403
              g_strfreev (*utf8_envp);
 
1404
              *utf8_envp = NULL;
 
1405
 
 
1406
              g_strfreev (*utf8_argv);
 
1407
              *utf8_argv = NULL;
 
1408
 
 
1409
              g_free (*utf8_working_directory);
 
1410
              *utf8_working_directory = NULL;
 
1411
 
 
1412
              return FALSE;
 
1413
            }
 
1414
        }
 
1415
      (*utf8_envp)[envc] = NULL;
 
1416
    }
 
1417
  return TRUE;
 
1418
}
 
1419
 
 
1420
static void
 
1421
free_utf8_copies (gchar  *utf8_working_directory,
 
1422
                  gchar **utf8_argv,
 
1423
                  gchar **utf8_envp)
 
1424
{
 
1425
  g_free (utf8_working_directory);
 
1426
  g_strfreev (utf8_argv);
 
1427
  g_strfreev (utf8_envp);
 
1428
}
 
1429
 
 
1430
gboolean
 
1431
g_spawn_async_with_pipes (const gchar          *working_directory,
 
1432
                          gchar               **argv,
 
1433
                          gchar               **envp,
 
1434
                          GSpawnFlags           flags,
 
1435
                          GSpawnChildSetupFunc  child_setup,
 
1436
                          gpointer              user_data,
 
1437
                          GPid                 *child_handle,
 
1438
                          gint                 *standard_input,
 
1439
                          gint                 *standard_output,
 
1440
                          gint                 *standard_error,
 
1441
                          GError              **error)
 
1442
{
 
1443
  gchar *utf8_working_directory;
 
1444
  gchar **utf8_argv;
 
1445
  gchar **utf8_envp;
 
1446
  gboolean retval;
 
1447
 
 
1448
  if (!setup_utf8_copies (working_directory, &utf8_working_directory,
 
1449
                          argv, &utf8_argv,
 
1450
                          envp, &utf8_envp,
 
1451
                          error))
 
1452
    return FALSE;
 
1453
 
 
1454
  retval = g_spawn_async_with_pipes_utf8 (utf8_working_directory,
 
1455
                                          utf8_argv, utf8_envp,
 
1456
                                          flags, child_setup, user_data,
 
1457
                                          child_handle,
 
1458
                                          standard_input, standard_output, standard_error,
 
1459
                                          error);
 
1460
 
 
1461
  free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
 
1462
 
 
1463
  return retval;
 
1464
}
 
1465
 
 
1466
gboolean
 
1467
g_spawn_async (const gchar          *working_directory,
 
1468
               gchar               **argv,
 
1469
               gchar               **envp,
 
1470
               GSpawnFlags           flags,
 
1471
               GSpawnChildSetupFunc  child_setup,
 
1472
               gpointer              user_data,
 
1473
               GPid                 *child_handle,
 
1474
               GError              **error)
 
1475
{
 
1476
  return g_spawn_async_with_pipes (working_directory,
 
1477
                                   argv, envp,
 
1478
                                   flags,
 
1479
                                   child_setup,
 
1480
                                   user_data,
 
1481
                                   child_handle,
 
1482
                                   NULL, NULL, NULL,
 
1483
                                   error);
 
1484
}
 
1485
 
 
1486
gboolean
 
1487
g_spawn_sync (const gchar          *working_directory,
 
1488
              gchar               **argv,
 
1489
              gchar               **envp,
 
1490
              GSpawnFlags           flags,
 
1491
              GSpawnChildSetupFunc  child_setup,
 
1492
              gpointer              user_data,
 
1493
              gchar               **standard_output,
 
1494
              gchar               **standard_error,
 
1495
              gint                 *exit_status,
 
1496
              GError              **error)     
 
1497
{
 
1498
  gchar *utf8_working_directory;
 
1499
  gchar **utf8_argv;
 
1500
  gchar **utf8_envp;
 
1501
  gboolean retval;
 
1502
 
 
1503
  if (!setup_utf8_copies (working_directory, &utf8_working_directory,
 
1504
                          argv, &utf8_argv,
 
1505
                          envp, &utf8_envp,
 
1506
                          error))
 
1507
    return FALSE;
 
1508
 
 
1509
  retval = g_spawn_sync_utf8 (utf8_working_directory,
 
1510
                              utf8_argv, utf8_envp,
 
1511
                              flags, child_setup, user_data,
 
1512
                              standard_output, standard_error, exit_status,
 
1513
                              error);
 
1514
 
 
1515
  free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
 
1516
 
 
1517
  return retval;
 
1518
}
 
1519
 
 
1520
gboolean
 
1521
g_spawn_command_line_sync (const gchar  *command_line,
 
1522
                           gchar       **standard_output,
 
1523
                           gchar       **standard_error,
 
1524
                           gint         *exit_status,
 
1525
                           GError      **error)
 
1526
{
 
1527
  gboolean retval;
 
1528
  gchar **argv = 0;
 
1529
 
 
1530
  g_return_val_if_fail (command_line != NULL, FALSE);
 
1531
  
 
1532
  if (!g_shell_parse_argv (command_line,
 
1533
                           NULL, &argv,
 
1534
                           error))
 
1535
    return FALSE;
 
1536
  
 
1537
  retval = g_spawn_sync (NULL,
 
1538
                         argv,
 
1539
                         NULL,
 
1540
                         G_SPAWN_SEARCH_PATH,
 
1541
                         NULL,
 
1542
                         NULL,
 
1543
                         standard_output,
 
1544
                         standard_error,
 
1545
                         exit_status,
 
1546
                         error);
 
1547
  g_strfreev (argv);
 
1548
 
 
1549
  return retval;
 
1550
}
 
1551
 
 
1552
gboolean
 
1553
g_spawn_command_line_async (const gchar *command_line,
 
1554
                            GError     **error)
 
1555
{
 
1556
  gboolean retval;
 
1557
  gchar **argv = 0;
 
1558
 
 
1559
  g_return_val_if_fail (command_line != NULL, FALSE);
 
1560
 
 
1561
  if (!g_shell_parse_argv (command_line,
 
1562
                           NULL, &argv,
 
1563
                           error))
 
1564
    return FALSE;
 
1565
  
 
1566
  retval = g_spawn_async (NULL,
 
1567
                          argv,
 
1568
                          NULL,
 
1569
                          G_SPAWN_SEARCH_PATH,
 
1570
                          NULL,
 
1571
                          NULL,
 
1572
                          NULL,
 
1573
                          error);
 
1574
  g_strfreev (argv);
 
1575
 
 
1576
  return retval;
 
1577
}
 
1578
 
 
1579
#endif /* !GSPAWN_HELPER */
 
1580
 
 
1581
#define __G_SPAWN_C__
 
1582
#include "galiasdef.c"