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