glib/glib/gspawn-symbian.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* gspawn-spawn.c - Process launching on Symbian
       
     2  *
       
     3  *  Copyright 2000 Red Hat, Inc.
       
     4  *  Copyright 2003 Tor Lillqvist
       
     5  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
       
     6  *
       
     7  * GLib is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Lesser General Public License as
       
     9  * published by the Free Software Foundation; either version 2 of the
       
    10  * License, or (at your option) any later version.
       
    11  *
       
    12  * GLib is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  * Lesser General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU Lesser General Public
       
    18  * License along with GLib; see the file COPYING.LIB.  If not, write
       
    19  * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    20  * Boston, MA 02111-1307, USA.
       
    21  */
       
    22 /*
       
    23  * Implementation of g_spawn family of functions on Symbian.
       
    24 
       
    25  */
       
    26 /* Define this to get some logging all the time */
       
    27 /* #define G_SPAWN_SYMBIAN_DEBUG */
       
    28 
       
    29 
       
    30 
       
    31 /*This program is adapted to be used in the Symbian OS scenario*/
       
    32 
       
    33 #include "config.h"
       
    34 
       
    35 #include "glib.h"
       
    36 #include "gprintfint.h"
       
    37 #include "galias.h"
       
    38 
       
    39 #include <string.h>
       
    40 #include <stdlib.h>
       
    41 #include <stdio.h>
       
    42 
       
    43 #include <errno.h>
       
    44 #include <fcntl.h>
       
    45 
       
    46 #include "glibintl.h"
       
    47 
       
    48 #ifdef __SYMBIAN32__
       
    49 #include <sys/select.h>
       
    50 #include "glibbackend.h"
       
    51 #include "glib_wsd.h"
       
    52 #endif//__SYMBIAN32__
       
    53 
       
    54 #if EMULATOR
       
    55 
       
    56 PLS(debug,gspawn_symbian,int)
       
    57 #define debug  (*FUNCTION_NAME(debug,gspawn_symbian)())
       
    58 
       
    59 #endif /* EMULATOR */
       
    60 
       
    61 #if (defined G_SPAWN_WIN32_DEBUG || defined G_SPAWN_SYMBIAN_DEBUG)
       
    62 #if !(EMULATOR)
       
    63   static int debug = 1;
       
    64 #endif /* EMULATOR */
       
    65   #define SETUP_DEBUG() /* empty */
       
    66 #else
       
    67 #if !(EMULATOR)
       
    68   static int debug = -1;
       
    69 #endif /* EMULATOR */
       
    70   #define SETUP_DEBUG()					\
       
    71     G_STMT_START					\
       
    72       {							\
       
    73 	if (debug == -1)				\
       
    74 	  {						\
       
    75 	    if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL)	\
       
    76 	      debug = 1;				\
       
    77 	    else					\
       
    78 	      debug = 0;				\
       
    79 	  }						\
       
    80       }							\
       
    81     G_STMT_END
       
    82 #endif
       
    83 
       
    84 enum
       
    85 {
       
    86   CHILD_NO_ERROR,
       
    87   CHILD_CHDIR_FAILED,
       
    88   CHILD_SPAWN_FAILED,
       
    89 };
       
    90 
       
    91 enum {
       
    92   ARG_CHILD_ERR_REPORT = 1,
       
    93   ARG_STDIN,
       
    94   ARG_STDOUT,
       
    95   ARG_STDERR,
       
    96   ARG_WORKING_DIRECTORY,
       
    97   ARG_CLOSE_DESCRIPTORS,
       
    98   ARG_USE_PATH,
       
    99   ARG_WAIT,
       
   100   ARG_PROGRAM,
       
   101   ARG_COUNT = ARG_PROGRAM
       
   102 };
       
   103 
       
   104 
       
   105 static gchar *
       
   106 protect_argv_string (const gchar *string)
       
   107 {
       
   108   const gchar *p = string;
       
   109   gchar *retval, *q;
       
   110   gint len = 0;
       
   111   gboolean need_dblquotes = FALSE;
       
   112   while (*p)
       
   113     {
       
   114       if (*p == ' ' || *p == '\t')
       
   115 	need_dblquotes = TRUE;
       
   116       else if (*p == '"')
       
   117 	len++;
       
   118       else if (*p == '\\')
       
   119 	{
       
   120 	  const gchar *pp = p;
       
   121 	  while (*pp && *pp == '\\')
       
   122 	    pp++;
       
   123 	  if (*pp == '"')
       
   124 	    len++;
       
   125 	}
       
   126       len++;
       
   127       p++;
       
   128     }
       
   129   q = retval = g_malloc (len + need_dblquotes*2 + 1);
       
   130   p = string;
       
   131 
       
   132   if (need_dblquotes)
       
   133     *q++ = '"';
       
   134   
       
   135   while (*p)
       
   136     {
       
   137       if (*p == '"')
       
   138 	*q++ = '\\';
       
   139       else if (*p == '\\')
       
   140 	{
       
   141 	  const gchar *pp = p;
       
   142 	  while (*pp && *pp == '\\')
       
   143 	    pp++;
       
   144 	  if (*pp == '"')
       
   145 	    *q++ = '\\';
       
   146 	}
       
   147       *q++ = *p;
       
   148       p++;
       
   149     }
       
   150   
       
   151   if (need_dblquotes)
       
   152     *q++ = '"';
       
   153   *q++ = '\0';
       
   154 
       
   155   return retval;
       
   156 }
       
   157 
       
   158 static gint
       
   159 protect_argv (gchar  **argv,
       
   160 	      gchar ***new_argv)
       
   161 {
       
   162   gint i;
       
   163   gint argc = 0;
       
   164   
       
   165   while (argv[argc])
       
   166     ++argc;
       
   167   *new_argv = g_new (gchar *, argc+1);
       
   168 
       
   169   /* Quote each argv element if necessary, so that it will get
       
   170    * reconstructed correctly in the C runtime startup code.  Note that
       
   171    * the unquoting algorithm in the C runtime is really weird, and
       
   172    * rather different than what Unix shells do. See stdargv.c in the C
       
   173    * runtime sources (in the Platform SDK, in src/crt).
       
   174    *
       
   175    * Note that an new_argv[0] constructed by this function should
       
   176    * *not* be passed as the filename argument to a spawn* or exec*
       
   177    * family function. That argument should be the real file name
       
   178    * without any quoting.
       
   179    */
       
   180   for (i = 0; i < argc; i++)
       
   181     (*new_argv)[i] = protect_argv_string (argv[i]);
       
   182 
       
   183   (*new_argv)[argc] = NULL;
       
   184 
       
   185   return argc;
       
   186 }
       
   187 
       
   188 EXPORT_C GQuark
       
   189 g_spawn_error_quark (void)
       
   190 {
       
   191   return g_quark_from_static_string ("g-exec-error-quark");
       
   192 }
       
   193 
       
   194 EXPORT_C gboolean
       
   195 g_spawn_async (const gchar          *working_directory,
       
   196 		    gchar               **argv,
       
   197 		    gchar               **envp,
       
   198 		    GSpawnFlags           flags,
       
   199 		    GSpawnChildSetupFunc  child_setup,
       
   200 		    gpointer              user_data,
       
   201 		    GPid                 *child_handle,
       
   202 		    GError              **error)
       
   203 {
       
   204   g_return_val_if_fail (argv != NULL, FALSE);
       
   205   
       
   206   return g_spawn_async_with_pipes (working_directory,
       
   207 					argv, envp,
       
   208 					flags,
       
   209 					child_setup,
       
   210 					user_data,
       
   211 					child_handle,
       
   212 					NULL, NULL, NULL,
       
   213 					error);
       
   214 }
       
   215 
       
   216 static gboolean
       
   217 utf8_charv_to_wcharv (char     **utf8_charv,
       
   218 		      wchar_t ***wcharv,
       
   219 		      int       *error_index,
       
   220 		      GError   **error)
       
   221 {
       
   222   wchar_t **retval = NULL;
       
   223 
       
   224   *wcharv = NULL;
       
   225   if (utf8_charv != NULL)
       
   226     {
       
   227       int n = 0, i;
       
   228 
       
   229       while (utf8_charv[n])
       
   230 		n++;
       
   231       retval = g_new (wchar_t *, n + 1);
       
   232             
       
   233 
       
   234       for (i = 0; i < n; i++)
       
   235 	{
       
   236 	  retval[i] = g_utf8_to_utf16 (utf8_charv[i], -1, NULL, NULL, error);
       
   237 	  if (retval[i] == NULL)
       
   238 	    {
       
   239 	      if (error_index)
       
   240 		*error_index = i;
       
   241 	      while (i)
       
   242 		g_free (retval[--i]);
       
   243 	      g_free (retval);
       
   244 	      return FALSE;
       
   245 	    }
       
   246 	}
       
   247 	    
       
   248       retval[n] = NULL;
       
   249     }
       
   250   *wcharv = retval;
       
   251   return TRUE;
       
   252 }
       
   253 
       
   254 static gboolean
       
   255 utf8_charv_to_cp_charv (char   **utf8_charv,
       
   256 			gchar ***cp_charv,
       
   257 			int     *error_index,
       
   258 			GError **error)
       
   259 {
       
   260   char **retval = NULL;
       
   261 
       
   262   *cp_charv = NULL;
       
   263   if (utf8_charv != NULL)
       
   264     {
       
   265       int n = 0, i;
       
   266 
       
   267       while (utf8_charv[n])
       
   268 		n++;
       
   269       retval = g_new (char *, n + 1);
       
   270 
       
   271       for (i = 0; i < n; i++)
       
   272 	{
       
   273 	  retval[i] = g_locale_from_utf8 (utf8_charv[i], -1, NULL, NULL, error);
       
   274 	  if (retval[i] == NULL)
       
   275 	    {
       
   276 	      if (error_index)
       
   277 		*error_index = i;
       
   278 	      while (i)
       
   279 		g_free (retval[--i]);
       
   280 	      g_free (retval);
       
   281 	      return FALSE;
       
   282 	    }
       
   283 	}
       
   284       retval[n] = NULL;
       
   285     }
       
   286 
       
   287   *cp_charv = retval;
       
   288   return TRUE;
       
   289 }
       
   290 
       
   291 static gboolean
       
   292 do_spawn_directly (gint                 *exit_status,
       
   293 		   gboolean		 do_return_handle,
       
   294 		   GSpawnFlags           flags,
       
   295 		   gchar               **argv,
       
   296 		   char                **envp,
       
   297 		   char                **protected_argv,
       
   298 		   GSpawnChildSetupFunc  child_setup,
       
   299 		   gpointer              user_data,
       
   300 		   GPid                 *child_handle,
       
   301 		   GError              **error)     
       
   302 {
       
   303   const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
       
   304   char **new_argv;
       
   305   int rc = -1;
       
   306   int saved_errno;
       
   307   GError *conv_error = NULL;
       
   308   gint conv_error_index;
       
   309   
       
   310   char *cpargv0, **cpargv, **cpenvp;
       
   311 
       
   312   /*
       
   313   if G_SPAWN_FILE_AND_ARGV_ZERO is specified all the argv except the first
       
   314   ie argv[0] is passed to the child. if the flag is not specified all the 
       
   315   arguements are passed to the child including the the argv[0]
       
   316   */
       
   317   new_argv = (flags & G_SPAWN_FILE_AND_ARGV_ZERO) ? protected_argv + 1 : protected_argv; 
       
   318       
       
   319   cpargv0 = g_locale_from_utf8 (argv[0], -1, NULL, NULL, &conv_error);
       
   320   if (cpargv0 == NULL)
       
   321   {
       
   322   	g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
       
   323 		       "Invalid program name: %s",
       
   324 		       conv_error->message);
       
   325 	g_error_free (conv_error);
       
   326 
       
   327 	return FALSE;
       
   328   }
       
   329 
       
   330   if  (!utf8_charv_to_cp_charv (new_argv, &cpargv, &conv_error_index, &conv_error))
       
   331   {
       
   332 	g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
       
   333 		       "Invalid string in argument vector at %d: %s",
       
   334 		       conv_error_index, conv_error->message);
       
   335 	g_error_free (conv_error);
       
   336 	g_free (cpargv0);
       
   337 
       
   338 	return FALSE;
       
   339   }
       
   340 
       
   341   if (!utf8_charv_to_cp_charv (envp, &cpenvp, NULL, &conv_error))
       
   342   {
       
   343 	g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
       
   344 		       "Invalid string in environment: %s",
       
   345 		       conv_error->message);
       
   346 	g_error_free (conv_error);
       
   347 	g_free (cpargv0);
       
   348 	g_strfreev (cpargv);
       
   349 
       
   350 	return FALSE;
       
   351   }
       
   352 
       
   353   if (child_setup)
       
   354   	(* child_setup) (user_data);
       
   355   
       
   356   if (flags & G_SPAWN_SEARCH_PATH)
       
   357 	if (cpenvp != NULL)
       
   358 	  rc = spawnvpe (mode, cpargv0, (const char **) cpargv, (const char **) cpenvp);
       
   359 	else
       
   360 	  rc = spawnvp (mode, cpargv0, (const char **) cpargv);
       
   361   else
       
   362 	if (envp != NULL)
       
   363 	  rc = spawnve (mode, cpargv0, (const char **) cpargv, (const char **) cpenvp);
       
   364 	else
       
   365 	  rc = spawnv (mode, cpargv0, (const char **) cpargv);
       
   366 
       
   367   g_free (cpargv0);
       
   368   g_strfreev (cpargv);
       
   369   g_strfreev (cpenvp);
       
   370   
       
   371 
       
   372   saved_errno = errno;
       
   373 
       
   374   if (rc == -1 && saved_errno != 0)
       
   375     {
       
   376       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
       
   377 		   _("Failed to execute child process (%s)"),
       
   378 		   g_strerror (saved_errno));
       
   379       return FALSE;
       
   380     }
       
   381 
       
   382   if (exit_status == NULL)
       
   383     {
       
   384       if (child_handle && do_return_handle)
       
   385 	  	*child_handle = (GPid) rc;
       
   386       else
       
   387       	{
       
   388 	  	CloseHandle ((HANDLE) rc);
       
   389 	  	if (child_handle)
       
   390 	    	*child_handle = 0;
       
   391 	  	}
       
   392     }
       
   393   else
       
   394     *exit_status = rc;
       
   395 
       
   396   return TRUE;
       
   397 }
       
   398 
       
   399 #if EMULATOR
       
   400 
       
   401 PLS(warned_about_child_setup ,do_spawn_with_pipes,gboolean)
       
   402 #define warned_about_child_setup (*FUNCTION_NAME(warned_about_child_setup,do_spawn_with_pipes)())
       
   403 
       
   404 #endif /* EMULATOR */
       
   405 
       
   406 static gboolean
       
   407 do_spawn_with_pipes (gint                 *exit_status,
       
   408 		     gboolean		   do_return_handle,
       
   409 		     const gchar          *working_directory,
       
   410 		     gchar               **argv,
       
   411 		     char                **envp,
       
   412 		     GSpawnFlags           flags,
       
   413 		     GSpawnChildSetupFunc  child_setup,
       
   414 		     gpointer              user_data,
       
   415 		     GPid                 *child_handle,
       
   416 		     gint                 *standard_input,
       
   417 		     gint                 *standard_output,
       
   418 		     gint                 *standard_error,
       
   419 		     gint		  *err_report,
       
   420 		     GError              **error)     
       
   421 {
       
   422   char **protected_argv;
       
   423   int rc = -1;
       
   424   int argc;
       
   425   gboolean retval;
       
   426 
       
   427   #if !(EMULATOR)
       
   428   static gboolean warned_about_child_setup = FALSE;
       
   429   #endif /* EMULATOR */
       
   430   
       
   431   GError *conv_error = NULL;
       
   432     
       
   433   SETUP_DEBUG();
       
   434 
       
   435   if (child_setup && !warned_about_child_setup)
       
   436     {
       
   437       warned_about_child_setup = TRUE;
       
   438       g_warning ("passing a child setup function to the g_spawn functions is pointless and dangerous on Win32");
       
   439     }
       
   440 
       
   441   argc = protect_argv (argv, &protected_argv);
       
   442 
       
   443   retval =
       
   444 	do_spawn_directly (exit_status, do_return_handle, flags,
       
   445 			   argv, envp, protected_argv,
       
   446 			   child_setup, user_data, child_handle,
       
   447 			   error);
       
   448       g_strfreev (protected_argv);
       
   449    
       
   450    if(standard_input)
       
   451    	*standard_input = -1;
       
   452    if(standard_output)
       
   453    	*standard_output = -1;
       
   454    if(standard_error)
       
   455    	*standard_error = -1;
       
   456    
       
   457    return retval;  
       
   458 }
       
   459 
       
   460 #if EMULATOR
       
   461 #undef warned_about_child_setup 
       
   462 #endif /* EMULATOR */
       
   463 
       
   464 EXPORT_C gboolean
       
   465 g_spawn_sync (const gchar          *working_directory,
       
   466 		   gchar               **argv,
       
   467 		   gchar               **envp,
       
   468 		   GSpawnFlags           flags,
       
   469 		   GSpawnChildSetupFunc  child_setup,
       
   470 		   gpointer              user_data,
       
   471 		   gchar               **standard_output,
       
   472 		   gchar               **standard_error,
       
   473 		   gint                 *exit_status,
       
   474 		   GError              **error)     
       
   475 {
       
   476   gint outpipe = -1;
       
   477   gint errpipe = -1;
       
   478   gint reportpipe = -1;
       
   479   gint outindex = -1;
       
   480   gint errindex = -1;
       
   481   GString *outstr = NULL;
       
   482   GString *errstr = NULL;
       
   483   gint status;
       
   484   
       
   485   g_return_val_if_fail (argv != NULL, FALSE);
       
   486   g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
       
   487   g_return_val_if_fail (standard_output == NULL ||
       
   488                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
       
   489   g_return_val_if_fail (standard_error == NULL ||
       
   490                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
       
   491   
       
   492   /* Just to ensure segfaults if callers try to use
       
   493    * these when an error is reported.
       
   494    */
       
   495   if (standard_output)
       
   496     *standard_output = NULL;
       
   497 
       
   498   if (standard_error)
       
   499     *standard_error = NULL;
       
   500   
       
   501   if (!do_spawn_with_pipes (&status,
       
   502 			    FALSE,
       
   503 			    working_directory,
       
   504 			    argv,
       
   505 			    envp,
       
   506 			    flags,
       
   507 			    child_setup,
       
   508 			    user_data,
       
   509 			    NULL,
       
   510 			    NULL,
       
   511 			    standard_output ? &outpipe : NULL,
       
   512 			    standard_error ? &errpipe : NULL,
       
   513 			    &reportpipe,
       
   514 			    error))
       
   515     return FALSE;
       
   516 
       
   517     if (exit_status)
       
   518     	*exit_status = status;
       
   519     return TRUE;
       
   520 }
       
   521 
       
   522 EXPORT_C gboolean
       
   523 g_spawn_async_with_pipes (const gchar          *working_directory,
       
   524 			       gchar               **argv,
       
   525 			       gchar               **envp,
       
   526 			       GSpawnFlags           flags,
       
   527 			       GSpawnChildSetupFunc  child_setup,
       
   528 			       gpointer              user_data,
       
   529 			       GPid                 *child_handle,
       
   530 			       gint                 *standard_input,
       
   531 			       gint                 *standard_output,
       
   532 			       gint                 *standard_error,
       
   533 			       GError              **error)
       
   534 {
       
   535   g_return_val_if_fail (argv != NULL, FALSE);
       
   536   g_return_val_if_fail (standard_output == NULL ||
       
   537                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
       
   538   g_return_val_if_fail (standard_error == NULL ||
       
   539                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
       
   540   /* can't inherit stdin if we have an input pipe. */
       
   541   g_return_val_if_fail (standard_input == NULL ||
       
   542                         !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
       
   543   
       
   544   return do_spawn_with_pipes (NULL,
       
   545 			      (flags & G_SPAWN_DO_NOT_REAP_CHILD),
       
   546 			      working_directory,
       
   547 			      argv,
       
   548 			      envp,
       
   549 			      flags,
       
   550 			      child_setup,
       
   551 			      user_data,
       
   552 			      child_handle,
       
   553 			      standard_input,
       
   554 			      standard_output,
       
   555 			      standard_error,
       
   556 			      NULL,
       
   557 			      error);
       
   558 }
       
   559 
       
   560 EXPORT_C gboolean
       
   561 g_spawn_command_line_sync (const gchar  *command_line,
       
   562 				gchar       **standard_output,
       
   563 				gchar       **standard_error,
       
   564 				gint         *exit_status,
       
   565 				GError      **error)
       
   566 {
       
   567   gboolean retval;
       
   568   gchar **argv = 0;
       
   569 
       
   570   g_return_val_if_fail (command_line != NULL, FALSE);
       
   571   
       
   572   if (!g_shell_parse_argv (command_line,
       
   573                            NULL, &argv,
       
   574                            error))
       
   575     return FALSE;
       
   576   
       
   577   retval = g_spawn_sync (NULL,
       
   578 			      argv,
       
   579 			      NULL,
       
   580 			      G_SPAWN_SEARCH_PATH,
       
   581 			      NULL,
       
   582 			      NULL,
       
   583 			      standard_output,
       
   584 			      standard_error,
       
   585 			      exit_status,
       
   586 			      error);
       
   587   g_strfreev (argv);
       
   588 
       
   589   return retval;
       
   590 }
       
   591 
       
   592 EXPORT_C gboolean
       
   593 g_spawn_command_line_async (const gchar *command_line,
       
   594 				 GError     **error)
       
   595 {
       
   596   gboolean retval;
       
   597   gchar **argv = 0;
       
   598 
       
   599   g_return_val_if_fail (command_line != NULL, FALSE);
       
   600 
       
   601   if (!g_shell_parse_argv (command_line,
       
   602                            NULL, &argv,
       
   603                            error))
       
   604     return FALSE;
       
   605   
       
   606   retval = g_spawn_async (NULL,
       
   607 			       argv,
       
   608 			       NULL,
       
   609 			       G_SPAWN_SEARCH_PATH,
       
   610 			       NULL,
       
   611 			       NULL,
       
   612 			       NULL,
       
   613 			       error);
       
   614   g_strfreev (argv);
       
   615 
       
   616   return retval;
       
   617 }
       
   618 
       
   619 EXPORT_C void
       
   620 g_spawn_close_pid (GPid pid)
       
   621 {
       
   622     CloseHandle (pid);
       
   623 }
       
   624 
       
   625 #define __G_SPAWN_SYMBIAN_C__
       
   626 #include "galiasdef.c"