Blame glib/gspawn-win32-helper.c

Packit ae235b
/* gspawn-win32-helper.c - Helper program for process launching on Win32.
Packit ae235b
 *
Packit ae235b
 *  Copyright 2000 Red Hat, Inc.
Packit ae235b
 *  Copyright 2000 Tor Lillqvist
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public License
Packit ae235b
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <fcntl.h>
Packit ae235b
Packit ae235b
/* For _CrtSetReportMode, we don't want Windows CRT (2005 and later)
Packit ae235b
 * to terminate the process if a bad file descriptor is passed into
Packit ae235b
 * _get_osfhandle().  This is necessary because we use _get_osfhandle()
Packit ae235b
 * to check the validity of the fd before we try to call close() on
Packit ae235b
 * it as attempting to close an invalid fd will cause the Windows CRT
Packit ae235b
 * to abort() this program internally.
Packit ae235b
 *
Packit ae235b
 * Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
Packit ae235b
 * for an explanation on this.
Packit ae235b
 */
Packit ae235b
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
Packit ae235b
#include <crtdbg.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
#include "glib.h"
Packit ae235b
#define GSPAWN_HELPER
Packit ae235b
#include "gspawn-win32.c"	/* For shared definitions */
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_err_and_exit (gint    fd,
Packit ae235b
		    gintptr msg)
Packit ae235b
{
Packit ae235b
  gintptr en = errno;
Packit ae235b
  
Packit ae235b
  write (fd, &msg, sizeof(gintptr));
Packit ae235b
  write (fd, &en, sizeof(gintptr));
Packit ae235b
  
Packit ae235b
  _exit (1);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef __GNUC__
Packit ae235b
#  ifndef _stdcall
Packit ae235b
#    define _stdcall  __attribute__((stdcall))
Packit ae235b
#  endif
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* We build gspawn-win32-helper.exe as a Windows GUI application
Packit ae235b
 * to avoid any temporarily flashing console windows in case
Packit ae235b
 * the gspawn function is invoked by a GUI program. Thus, no main()
Packit ae235b
 * but a WinMain().
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* Copy of protect_argv that handles wchar_t strings */
Packit ae235b
Packit ae235b
static gint
Packit ae235b
protect_wargv (gint       argc,
Packit ae235b
	       wchar_t  **wargv,
Packit ae235b
	       wchar_t ***new_wargv)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
  
Packit ae235b
  *new_wargv = g_new (wchar_t *, argc+1);
Packit ae235b
Packit ae235b
  /* Quote each argv element if necessary, so that it will get
Packit ae235b
   * reconstructed correctly in the C runtime startup code.  Note that
Packit ae235b
   * the unquoting algorithm in the C runtime is really weird, and
Packit ae235b
   * rather different than what Unix shells do. See stdargv.c in the C
Packit ae235b
   * runtime sources (in the Platform SDK, in src/crt).
Packit ae235b
   *
Packit ae235b
   * Note that an new_wargv[0] constructed by this function should
Packit ae235b
   * *not* be passed as the filename argument to a _wspawn* or _wexec*
Packit ae235b
   * family function. That argument should be the real file name
Packit ae235b
   * without any quoting.
Packit ae235b
   */
Packit ae235b
  for (i = 0; i < argc; i++)
Packit ae235b
    {
Packit ae235b
      wchar_t *p = wargv[i];
Packit ae235b
      wchar_t *q;
Packit ae235b
      gint len = 0;
Packit ae235b
      gboolean need_dblquotes = FALSE;
Packit ae235b
      while (*p)
Packit ae235b
	{
Packit ae235b
	  if (*p == ' ' || *p == '\t')
Packit ae235b
	    need_dblquotes = TRUE;
Packit ae235b
	  else if (*p == '"')
Packit ae235b
	    len++;
Packit ae235b
	  else if (*p == '\\')
Packit ae235b
	    {
Packit ae235b
	      wchar_t *pp = p;
Packit ae235b
	      while (*pp && *pp == '\\')
Packit ae235b
		pp++;
Packit ae235b
	      if (*pp == '"')
Packit ae235b
		len++;
Packit ae235b
	    }
Packit ae235b
	  len++;
Packit ae235b
	  p++;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      q = (*new_wargv)[i] = g_new (wchar_t, len + need_dblquotes*2 + 1);
Packit ae235b
      p = wargv[i];
Packit ae235b
Packit ae235b
      if (need_dblquotes)
Packit ae235b
	*q++ = '"';
Packit ae235b
Packit ae235b
      while (*p)
Packit ae235b
	{
Packit ae235b
	  if (*p == '"')
Packit ae235b
	    *q++ = '\\';
Packit ae235b
	  else if (*p == '\\')
Packit ae235b
	    {
Packit ae235b
	      wchar_t *pp = p;
Packit ae235b
	      while (*pp && *pp == '\\')
Packit ae235b
		pp++;
Packit ae235b
	      if (*pp == '"')
Packit ae235b
		*q++ = '\\';
Packit ae235b
	    }
Packit ae235b
	  *q++ = *p;
Packit ae235b
	  p++;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (need_dblquotes)
Packit ae235b
	*q++ = '"';
Packit ae235b
      *q++ = '\0';
Packit ae235b
    }
Packit ae235b
  (*new_wargv)[argc] = NULL;
Packit ae235b
Packit ae235b
  return argc;
Packit ae235b
}
Packit ae235b
Packit ae235b
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
Packit ae235b
/*
Packit ae235b
 * This is the (empty) invalid parameter handler
Packit ae235b
 * that is used for Visual C++ 2005 (and later) builds
Packit ae235b
 * so that we can use this instead of the system automatically
Packit ae235b
 * aborting the process.
Packit ae235b
 *
Packit ae235b
 * This is necessary as we use _get_oshandle() to check the validity
Packit ae235b
 * of the file descriptors as we close them, so when an invalid file
Packit ae235b
 * descriptor is passed into that function as we check on it, we get
Packit ae235b
 * -1 as the result, instead of the gspawn helper program aborting.
Packit ae235b
 *
Packit ae235b
 * Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
Packit ae235b
 * for an explanation on this.
Packit ae235b
 */
Packit ae235b
extern void
Packit ae235b
myInvalidParameterHandler(const wchar_t *expression,
Packit ae235b
                          const wchar_t *function,
Packit ae235b
                          const wchar_t *file,
Packit ae235b
                          unsigned int   line,
Packit ae235b
                          uintptr_t      pReserved);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
#ifndef HELPER_CONSOLE
Packit ae235b
int _stdcall
Packit ae235b
WinMain (struct HINSTANCE__ *hInstance,
Packit ae235b
	 struct HINSTANCE__ *hPrevInstance,
Packit ae235b
	 char               *lpszCmdLine,
Packit ae235b
	 int                 nCmdShow)
Packit ae235b
#else
Packit ae235b
int
Packit ae235b
main (int ignored_argc, char **ignored_argv)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
  int child_err_report_fd = -1;
Packit ae235b
  int helper_sync_fd = -1;
Packit ae235b
  int i;
Packit ae235b
  int fd;
Packit ae235b
  int mode;
Packit ae235b
  gintptr handle;
Packit ae235b
  int saved_errno;
Packit ae235b
  gintptr no_error = CHILD_NO_ERROR;
Packit ae235b
  gint argv_zero_offset = ARG_PROGRAM;
Packit ae235b
  wchar_t **new_wargv;
Packit ae235b
  int argc;
Packit ae235b
  char **argv;
Packit ae235b
  wchar_t **wargv;
Packit ae235b
  char c;
Packit ae235b
Packit ae235b
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
Packit ae235b
  /* set up our empty invalid parameter handler */
Packit ae235b
  _invalid_parameter_handler oldHandler, newHandler;
Packit ae235b
  newHandler = myInvalidParameterHandler;
Packit ae235b
  oldHandler = _set_invalid_parameter_handler(newHandler);
Packit ae235b
Packit ae235b
  /* Disable the message box for assertions. */
Packit ae235b
  _CrtSetReportMode(_CRT_ASSERT, 0);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  /* Fetch the wide-char argument vector */
Packit ae235b
  wargv = CommandLineToArgvW (GetCommandLineW(), &argc);
Packit ae235b
Packit ae235b
  g_assert (argc >= ARG_COUNT);
Packit ae235b
Packit ae235b
  /* Convert unicode wargs to utf8 */
Packit ae235b
  argv = g_new(char *, argc + 1);
Packit ae235b
  for (i = 0; i < argc; i++)
Packit ae235b
    argv[i] = g_utf16_to_utf8(wargv[i], -1, NULL, NULL, NULL);
Packit ae235b
  argv[i] = NULL;
Packit ae235b
Packit ae235b
  /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor number onto
Packit ae235b
   * which write error messages.
Packit ae235b
   */
Packit ae235b
  child_err_report_fd = atoi (argv[ARG_CHILD_ERR_REPORT]);
Packit ae235b
Packit ae235b
  /* Hack to implement G_SPAWN_FILE_AND_ARGV_ZERO. If
Packit ae235b
   * argv[ARG_CHILD_ERR_REPORT] is suffixed with a '#' it means we get
Packit ae235b
   * the program to run and its argv[0] separately.
Packit ae235b
   */
Packit ae235b
  if (argv[ARG_CHILD_ERR_REPORT][strlen (argv[ARG_CHILD_ERR_REPORT]) - 1] == '#')
Packit ae235b
    argv_zero_offset++;
Packit ae235b
Packit ae235b
  /* argv[ARG_HELPER_SYNC] is the file descriptor number we read a
Packit ae235b
   * byte that tells us it is OK to exit. We have to wait until the
Packit ae235b
   * parent allows us to exit, so that the parent has had time to
Packit ae235b
   * duplicate the process handle we sent it. Duplicating a handle
Packit ae235b
   * from another process works only if that other process exists.
Packit ae235b
   */
Packit ae235b
  helper_sync_fd = atoi (argv[ARG_HELPER_SYNC]);
Packit ae235b
Packit ae235b
  /* argv[ARG_STDIN..ARG_STDERR] are the file descriptor numbers that
Packit ae235b
   * should be dup2'd to 0, 1 and 2. '-' if the corresponding fd
Packit ae235b
   * should be left alone, and 'z' if it should be connected to the
Packit ae235b
   * bit bucket NUL:.
Packit ae235b
   */
Packit ae235b
  if (argv[ARG_STDIN][0] == '-')
Packit ae235b
    ; /* Nothing */
Packit ae235b
  else if (argv[ARG_STDIN][0] == 'z')
Packit ae235b
    {
Packit ae235b
      fd = open ("NUL:", O_RDONLY);
Packit ae235b
      if (fd != 0)
Packit ae235b
	{
Packit ae235b
	  dup2 (fd, 0);
Packit ae235b
	  close (fd);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      fd = atoi (argv[ARG_STDIN]);
Packit ae235b
      if (fd != 0)
Packit ae235b
	{
Packit ae235b
	  dup2 (fd, 0);
Packit ae235b
	  close (fd);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (argv[ARG_STDOUT][0] == '-')
Packit ae235b
    ; /* Nothing */
Packit ae235b
  else if (argv[ARG_STDOUT][0] == 'z')
Packit ae235b
    {
Packit ae235b
      fd = open ("NUL:", O_WRONLY);
Packit ae235b
      if (fd != 1)
Packit ae235b
	{
Packit ae235b
	  dup2 (fd, 1);
Packit ae235b
	  close (fd);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      fd = atoi (argv[ARG_STDOUT]);
Packit ae235b
      if (fd != 1)
Packit ae235b
	{
Packit ae235b
	  dup2 (fd, 1);
Packit ae235b
	  close (fd);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (argv[ARG_STDERR][0] == '-')
Packit ae235b
    ; /* Nothing */
Packit ae235b
  else if (argv[ARG_STDERR][0] == 'z')
Packit ae235b
    {
Packit ae235b
      fd = open ("NUL:", O_WRONLY);
Packit ae235b
      if (fd != 2)
Packit ae235b
	{
Packit ae235b
	  dup2 (fd, 2);
Packit ae235b
	  close (fd);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      fd = atoi (argv[ARG_STDERR]);
Packit ae235b
      if (fd != 2)
Packit ae235b
	{
Packit ae235b
	  dup2 (fd, 2);
Packit ae235b
	  close (fd);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
Packit ae235b
   * process.  If "-", don't change directory.
Packit ae235b
   */
Packit ae235b
  if (argv[ARG_WORKING_DIRECTORY][0] == '-' &&
Packit ae235b
      argv[ARG_WORKING_DIRECTORY][1] == 0)
Packit ae235b
    ; /* Nothing */
Packit ae235b
  else if (_wchdir (wargv[ARG_WORKING_DIRECTORY]) < 0)
Packit ae235b
    write_err_and_exit (child_err_report_fd, CHILD_CHDIR_FAILED);
Packit ae235b
Packit ae235b
  /* argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
Packit ae235b
   *  upwards should be closed
Packit ae235b
   */
Packit ae235b
  if (argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
Packit ae235b
    for (i = 3; i < 1000; i++)	/* FIXME real limit? */
Packit ae235b
      if (i != child_err_report_fd && i != helper_sync_fd)
Packit ae235b
        if (_get_osfhandle (i) != -1)
Packit ae235b
          close (i);
Packit ae235b
Packit ae235b
  /* We don't want our child to inherit the error report and
Packit ae235b
   * helper sync fds.
Packit ae235b
   */
Packit ae235b
  child_err_report_fd = dup_noninherited (child_err_report_fd, _O_WRONLY);
Packit ae235b
  helper_sync_fd = dup_noninherited (helper_sync_fd, _O_RDONLY);
Packit ae235b
Packit ae235b
  /* argv[ARG_WAIT] is "w" to wait for the program to exit */
Packit ae235b
  if (argv[ARG_WAIT][0] == 'w')
Packit ae235b
    mode = P_WAIT;
Packit ae235b
  else
Packit ae235b
    mode = P_NOWAIT;
Packit ae235b
Packit ae235b
  /* argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
Packit ae235b
Packit ae235b
  /* argv[ARG_PROGRAM] is executable file to run,
Packit ae235b
   * argv[argv_zero_offset]... is its argv. argv_zero_offset equals
Packit ae235b
   * ARG_PROGRAM unless G_SPAWN_FILE_AND_ARGV_ZERO was used, in which
Packit ae235b
   * case we have a separate executable name and argv[0].
Packit ae235b
   */
Packit ae235b
Packit ae235b
  /* For the program name passed to spawnv(), don't use the quoted
Packit ae235b
   * version.
Packit ae235b
   */
Packit ae235b
  protect_wargv (argc - argv_zero_offset, wargv + argv_zero_offset, &new_wargv);
Packit ae235b
Packit ae235b
  if (argv[ARG_USE_PATH][0] == 'y')
Packit ae235b
    handle = _wspawnvp (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
Packit ae235b
  else
Packit ae235b
    handle = _wspawnv (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
Packit ae235b
Packit ae235b
  saved_errno = errno;
Packit ae235b
Packit ae235b
  if (handle == -1 && saved_errno != 0)
Packit ae235b
    write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
Packit ae235b
Packit ae235b
  write (child_err_report_fd, &no_error, sizeof (no_error));
Packit ae235b
  write (child_err_report_fd, &handle, sizeof (handle));
Packit ae235b
Packit ae235b
  read (helper_sync_fd, &c, 1);
Packit ae235b
Packit ae235b
  LocalFree (wargv);
Packit ae235b
  g_strfreev (argv);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}