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