~ubuntu-branches/debian/sid/glib2.0/sid

« back to all changes in this revision

Viewing changes to glib/gspawn-win32-helper.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-05-08 06:25:57 UTC
  • mfrom: (1.27.14) (3.1.181 experimental)
  • Revision ID: package-import@ubuntu.com-20130508062557-i7gbku66mls70gi2
Tags: 2.36.1-2
Merge experimental branch, upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include <fcntl.h>
25
25
 
 
26
/* For _CrtSetReportMode, we don't want Windows CRT (2005 and later)
 
27
 * to terminate the process if a bad file descriptor is passed into
 
28
 * _get_osfhandle.  The newer MS CRT's are picky
 
29
 * on double close()'s and bad file descriptors.
 
30
 */
 
31
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
 
32
#include <crtdbg.h>
 
33
#endif
 
34
 
26
35
#undef G_LOG_DOMAIN
27
36
#include "glib.h"
28
37
#define GSPAWN_HELPER
147
156
  return argc;
148
157
}
149
158
 
 
159
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
 
160
/*
 
161
 * This is the (empty) invalid parameter handler
 
162
 * that is used for Visual C++ 2005 (and later) builds
 
163
 * so that we can use this instead of the system automatically
 
164
 * aborting the process, as the newer MS CRTs are more picky
 
165
 * about double close()'s and bad/invalid file descriptors.
 
166
 *
 
167
 * This is necessary as we use _get_oshandle() to check the validity
 
168
 * of the file descriptors as we close them, so when an invalid file
 
169
 * descriptor is passed into that function as we check on it, we get
 
170
 * -1 as the result, instead of the gspawn helper program aborting.
 
171
 */
 
172
void myInvalidParameterHandler(
 
173
   const wchar_t * expression,
 
174
   const wchar_t * function,
 
175
   const wchar_t * file,
 
176
   unsigned int line,
 
177
   uintptr_t pReserved
 
178
)
 
179
{
 
180
  return;
 
181
}
 
182
#endif
 
183
 
 
184
 
150
185
#ifndef HELPER_CONSOLE
151
186
int _stdcall
152
187
WinMain (struct HINSTANCE__ *hInstance,
173
208
  _startupinfo si = { 0 };
174
209
  char c;
175
210
 
 
211
  /* store up the file descriptors to close */
 
212
  GSList *fd_toclose = NULL;
 
213
  GSList *last_item = NULL;
 
214
 
 
215
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
 
216
  /* set up our empty invalid parameter handler */
 
217
  _invalid_parameter_handler oldHandler, newHandler;
 
218
  newHandler = myInvalidParameterHandler;
 
219
  oldHandler = _set_invalid_parameter_handler(newHandler);
 
220
 
 
221
  /* Disable the message box for assertions. */
 
222
  _CrtSetReportMode(_CRT_ASSERT, 0);
 
223
#endif
 
224
 
 
225
 
176
226
  g_assert (__argc >= ARG_COUNT);
177
227
 
178
228
  /* Fetch the wide-char argument vector */
217
267
      if (fd != 0)
218
268
        {
219
269
          dup2 (fd, 0);
220
 
          close (fd);
 
270
          fd_toclose = g_slist_append (fd_toclose, GINT_TO_POINTER (fd));
221
271
        }
222
272
    }
223
273
  else
226
276
      if (fd != 0)
227
277
        {
228
278
          dup2 (fd, 0);
229
 
          close (fd);
 
279
          fd_toclose = g_slist_append (fd_toclose, GINT_TO_POINTER (fd));
230
280
        }
231
281
    }
232
282
 
238
288
      if (fd != 1)
239
289
        {
240
290
          dup2 (fd, 1);
241
 
          close (fd);
 
291
          fd_toclose = g_slist_append (fd_toclose, GINT_TO_POINTER (fd));
242
292
        }
243
293
    }
244
294
  else
247
297
      if (fd != 1)
248
298
        {
249
299
          dup2 (fd, 1);
250
 
          close (fd);
 
300
          fd_toclose = g_slist_append (fd_toclose, GINT_TO_POINTER (fd));
251
301
        }
252
302
    }
253
303
 
259
309
      if (fd != 2)
260
310
        {
261
311
          dup2 (fd, 2);
262
 
          close (fd);
 
312
          fd_toclose = g_slist_append (fd_toclose, GINT_TO_POINTER (fd));
263
313
        }
264
314
    }
265
315
  else
268
318
      if (fd != 2)
269
319
        {
270
320
          dup2 (fd, 2);
271
 
          close (fd);
 
321
          fd_toclose = g_slist_append (fd_toclose, GINT_TO_POINTER (fd));
272
322
        }
273
323
    }
274
324
 
287
337
  if (__argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
288
338
    for (i = 3; i < 1000; i++)  /* FIXME real limit? */
289
339
      if (i != child_err_report_fd && i != helper_sync_fd)
290
 
        close (i);
 
340
        if (_get_osfhandle (i) != -1)
 
341
          fd_toclose = g_slist_append (fd_toclose, GINT_TO_POINTER (i));
 
342
 
 
343
  /* ...so we won't get the nasty off-by-1 file descriptor leak */
 
344
  g_slist_append (fd_toclose, NULL);
 
345
  last_item = g_slist_last (fd_toclose);
 
346
 
 
347
  /* now close all the file descriptors as necessary */
 
348
  if (fd_toclose != NULL && last_item != NULL)
 
349
  {
 
350
    for ( ; fd_toclose != last_item; fd_toclose = fd_toclose->next)
 
351
      close (GPOINTER_TO_INT(fd_toclose->data));
 
352
  }
 
353
  g_slist_free (last_item);
 
354
  g_slist_free (fd_toclose);
 
355
 
291
356
 
292
357
  /* We don't want our child to inherit the error report and
293
358
   * helper sync fds.