glib/gspawn-win32.c Implement G_SPAWN_FILE_AND_ARGV_ZERO. (#136792, Bruce
[platform/upstream/glib.git] / glib / gspawn-win32-helper.c
1 /* gspawn-win32-helper.c - Helper program for process launching on Win32.
2  *
3  *  Copyright 2000 Red Hat, Inc.
4  *  Copyright 2000 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 #include "config.h"
23
24 #undef G_LOG_DOMAIN
25 #include "glib.h"
26 #define GSPAWN_HELPER
27 #include "gspawn-win32.c"       /* For shared definitions */
28
29
30 static GString *debugstring;
31
32 static void
33 write_err_and_exit (gint fd,
34                     gint msg)
35 {
36   gint en = errno;
37   
38   if (debug)
39     {
40       debugstring = g_string_new (NULL);
41       g_string_append (debugstring,
42                        g_strdup_printf ("writing error code %d and errno %d",
43                                         msg, en));
44       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
45     }
46
47   write (fd, &msg, sizeof(msg));
48   write (fd, &en, sizeof(en));
49   
50   _exit (1);
51 }
52
53 #ifdef __GNUC__
54 #  ifndef _stdcall
55 #    define _stdcall  __attribute__((stdcall))
56 #  endif
57 #endif
58
59 /* We build gspawn-win32-helper.exe as a Windows GUI application
60  * to avoid any temporarily flashing console windows in case
61  * the gspawn function is invoked by a GUI program. Thus, no main()
62  * but a WinMain(). We do, however, still use argc and argv tucked
63  * away in the global __argc and __argv by the C runtime startup code.
64  */
65
66 int _stdcall
67 WinMain (struct HINSTANCE__ *hInstance,
68          struct HINSTANCE__ *hPrevInstance,
69          char               *lpszCmdLine,
70          int                 nCmdShow)
71 {
72   int child_err_report_fd;
73   int i;
74   int fd;
75   int mode;
76   int handle;
77   int no_error = CHILD_NO_ERROR;
78   int zero = 0;
79   gint file_and_argv_zero = 0;
80   gchar **new_argv;
81
82   SETUP_DEBUG();
83
84   if (debug)
85     {
86       debugstring = g_string_new (NULL);
87
88       g_string_append (debugstring,
89                        g_strdup_printf ("g-spawn-win32-helper: "
90                                         "argc = %d, argv: ",
91                                         __argc));
92       for (i = 0; i < __argc; i++)
93         {
94           if (i > 0)
95             g_string_append (debugstring, " ");
96           g_string_append (debugstring, __argv[i]);
97         }
98       
99       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
100     }
101
102   g_assert (__argc >= ARG_COUNT);
103
104   /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor onto which
105    * write error messages.
106    */
107   child_err_report_fd = atoi (__argv[ARG_CHILD_ERR_REPORT]);
108
109   /* Hack to implement G_SPAWN_FILE_AND_ARGV_ZERO */
110   if (__argv[ARG_CHILD_ERR_REPORT][strlen (__argv[ARG_CHILD_ERR_REPORT]) - 1] == '#')
111     file_and_argv_zero = 1;
112
113   /* argv[ARG_STDIN..ARG_STDERR] are the file descriptors that should
114    * be dup2'd to stdin, stdout and stderr, '-' if the corresponding
115    * std* should be let alone, and 'z' if it should be connected to
116    * the bit bucket NUL:.
117    */
118   if (__argv[ARG_STDIN][0] == '-')
119     ; /* Nothing */
120   else if (__argv[ARG_STDIN][0] == 'z')
121     {
122       fd = open ("NUL:", O_RDONLY);
123       if (fd != 0)
124         {
125           dup2 (fd, 0);
126           close (fd);
127         }
128     }
129   else
130     {
131       fd = atoi (__argv[ARG_STDIN]);
132       if (fd != 0)
133         {
134           dup2 (fd, 0);
135           close (fd);
136         }
137     }
138
139   if (__argv[ARG_STDOUT][0] == '-')
140     ; /* Nothing */
141   else if (__argv[ARG_STDOUT][0] == 'z')
142     {
143       fd = open ("NUL:", O_WRONLY);
144       if (fd != 1)
145         {
146           dup2 (fd, 1);
147           close (fd);
148         }
149     }
150   else
151     {
152       fd = atoi (__argv[ARG_STDOUT]);
153       if (fd != 1)
154         {
155           dup2 (fd, 1);
156           close (fd);
157         }
158     }
159
160   if (__argv[ARG_STDERR][0] == '-')
161     ; /* Nothing */
162   else if (__argv[ARG_STDERR][0] == 'z')
163     {
164       fd = open ("NUL:", O_WRONLY);
165       if (fd != 2)
166         {
167           dup2 (fd, 2);
168           close (fd);
169         }
170     }
171   else
172     {
173       fd = atoi (__argv[ARG_STDERR]);
174       if (fd != 2)
175         {
176           dup2 (fd, 2);
177           close (fd);
178         }
179     }
180
181   /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
182    * process.  If "-", don't change directory.
183    */
184   if (__argv[ARG_WORKING_DIRECTORY][0] == '-' &&
185       __argv[ARG_WORKING_DIRECTORY][1] == 0)
186     ; /* Nothing */
187   else if (chdir (__argv[ARG_WORKING_DIRECTORY]) < 0)
188     write_err_and_exit (child_err_report_fd,
189                         CHILD_CHDIR_FAILED);
190
191   /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
192    *  upwards should be closed
193    */
194
195   if (__argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
196     for (i = 3; i < 1000; i++)  /* FIXME real limit? */
197       if (i != child_err_report_fd)
198         close (i);
199
200   /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
201
202   if (__argv[ARG_WAIT][0] == 'w')
203     mode = P_WAIT;
204   else
205     mode = P_NOWAIT;
206
207   /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
208
209   /* __argv[ARG_PROGRAM] is program file to run,
210    * __argv[ARG_PROGRAM+1]... is its __argv.
211    */
212
213   protect_argv (__argv, &new_argv);
214
215   /* For the program name passed to spawnv(), don't use the quoted
216    * version. */
217
218   if (debug)
219     {
220       debugstring = g_string_new (NULL);
221       g_string_append (debugstring,
222                        g_strdup_printf ("calling %s %s mode=%s argv: ",
223                                         (__argv[ARG_USE_PATH][0] == 'y' ?
224                                          "spawnvp" : "spawnv"),
225                                         __argv[ARG_PROGRAM],
226                                         (mode == P_WAIT ?
227                                          "P_WAIT" : "P_NOWAIT")));
228       i = ARG_PROGRAM + 1 + file_and_argv_zero;
229       while (new_argv[i])
230         {
231           g_string_append (debugstring, new_argv[i++]);
232           if (new_argv[i])
233             g_string_append (debugstring, " ");
234         }
235       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
236     }
237
238   if (new_argv[ARG_USE_PATH][0] == 'y')
239     handle = spawnvp (mode, __argv[ARG_PROGRAM], new_argv + ARG_PROGRAM + file_and_argv_zero);
240   else
241     handle = spawnv (mode, __argv[ARG_PROGRAM], new_argv + ARG_PROGRAM + file_and_argv_zero);
242
243   if (debug)
244     {
245       debugstring = g_string_new (NULL);
246       g_string_append (debugstring,
247                        g_strdup_printf ("%s returned %#x",
248                                         (__argv[ARG_USE_PATH][0] == 'y' ?
249                                          "spawnvp" : "spawnv"),
250                                         handle));
251       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
252     }
253
254   if (handle < 0)
255     write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
256
257   write (child_err_report_fd, &no_error, sizeof (no_error));
258   if (mode == P_NOWAIT)
259     write (child_err_report_fd, &handle, sizeof (handle));
260   else
261     write (child_err_report_fd, &zero, sizeof (zero));
262   return 0;
263 }
264