Patch from Sven Neumann to make the include order consistent. (#71704)
[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 ("");
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
80   SETUP_DEBUG();
81
82   if (debug)
83     {
84       debugstring = g_string_new ("");
85
86       g_string_append (debugstring,
87                        g_strdup_printf ("g-spawn-win32-helper: "
88                                         "argc = %d, argv: ",
89                                         __argc));
90       for (i = 0; i < __argc; i++)
91         {
92           if (i > 0)
93             g_string_append (debugstring, " ");
94           g_string_append (debugstring, __argv[i]);
95         }
96       
97       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
98     }
99
100   g_assert (__argc >= ARG_COUNT);
101
102   /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor onto which
103    * write error messages.
104    */
105   child_err_report_fd = atoi (__argv[ARG_CHILD_ERR_REPORT]);
106
107   /* argv[ARG_STDIN..ARG_STDERR] are the file descriptors that should
108    * be dup2'd to stdin, stdout and stderr, '-' if the corresponding
109    * std* should be let alone, and 'z' if it should be connected to
110    * the bit bucket NUL:.
111    */
112   if (__argv[ARG_STDIN][0] == '-')
113     ; /* Nothing */
114   else if (__argv[ARG_STDIN][0] == 'z')
115     {
116       fd = open ("NUL:", O_RDONLY);
117       if (fd != 0)
118         {
119           dup2 (fd, 0);
120           close (fd);
121         }
122     }
123   else
124     {
125       fd = atoi (__argv[ARG_STDIN]);
126       if (fd != 0)
127         {
128           dup2 (fd, 0);
129           close (fd);
130         }
131     }
132
133   if (__argv[ARG_STDOUT][0] == '-')
134     ; /* Nothing */
135   else if (__argv[ARG_STDOUT][0] == 'z')
136     {
137       fd = open ("NUL:", O_WRONLY);
138       if (fd != 1)
139         {
140           dup2 (fd, 1);
141           close (fd);
142         }
143     }
144   else
145     {
146       fd = atoi (__argv[ARG_STDOUT]);
147       if (fd != 1)
148         {
149           dup2 (fd, 1);
150           close (fd);
151         }
152     }
153
154   if (__argv[ARG_STDERR][0] == '-')
155     ; /* Nothing */
156   else if (__argv[ARG_STDERR][0] == 'z')
157     {
158       fd = open ("NUL:", O_WRONLY);
159       if (fd != 2)
160         {
161           dup2 (fd, 2);
162           close (fd);
163         }
164     }
165   else
166     {
167       fd = atoi (__argv[ARG_STDERR]);
168       if (fd != 2)
169         {
170           dup2 (fd, 2);
171           close (fd);
172         }
173     }
174
175   /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
176    * process.  If "-", don't change directory.
177    */
178   if (__argv[ARG_WORKING_DIRECTORY][0] == '-' &&
179       __argv[ARG_WORKING_DIRECTORY][1] == 0)
180     ; /* Nothing */
181   else if (chdir (__argv[ARG_WORKING_DIRECTORY]) < 0)
182     write_err_and_exit (child_err_report_fd,
183                         CHILD_CHDIR_FAILED);
184
185   /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
186    *  upwards should be closed
187    */
188
189   if (__argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
190     for (i = 3; i < 1000; i++)  /* FIXME real limit? */
191       if (i != child_err_report_fd)
192         close (i);
193
194   /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
195
196   if (__argv[ARG_WAIT][0] == 'w')
197     mode = P_WAIT;
198   else
199     mode = P_NOWAIT;
200
201   /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
202
203   /* __argv[ARG_PROGRAM] is program file to run,
204    * __argv[ARG_PROGRAM+1]... is its __argv.
205    */
206
207   if (debug)
208     {
209       debugstring = g_string_new ("");
210       g_string_append (debugstring,
211                        g_strdup_printf ("calling %s %s mode=%s argv: ",
212                                         (__argv[ARG_USE_PATH][0] == 'y' ?
213                                          "spawnvp" : "spawnv"),
214                                         __argv[ARG_PROGRAM],
215                                         (mode == P_WAIT ?
216                                          "P_WAIT" : "P_NOWAIT")));
217       i = ARG_PROGRAM+1;
218       while (__argv[i])
219         {
220           g_string_append (debugstring, __argv[i++]);
221           if (__argv[i])
222             g_string_append (debugstring, " ");
223         }
224       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
225     }
226
227   if (__argv[ARG_USE_PATH][0] == 'y')
228     handle = spawnvp (mode, __argv[ARG_PROGRAM], __argv+ARG_PROGRAM);
229   else
230     handle = spawnv (mode, __argv[ARG_PROGRAM], __argv+ARG_PROGRAM);
231
232   if (debug)
233     {
234       debugstring = g_string_new ("");
235       g_string_append (debugstring,
236                        g_strdup_printf ("%s returned %#x",
237                                         (__argv[ARG_USE_PATH][0] == 'y' ?
238                                          "spawnvp" : "spawnv"),
239                                         handle));
240       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
241     }
242
243   if (handle < 0)
244     write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
245
246   write (child_err_report_fd, &no_error, sizeof (no_error));
247   if (mode == P_NOWAIT)
248     write (child_err_report_fd, &handle, sizeof (handle));
249   else
250     write (child_err_report_fd, &zero, sizeof (zero));
251   return 0;
252 }
253