1
1
/* exechelp.c - fork and exec helpers
2
* Copyright (C) 2004, 2007, 2008 Free Software Foundation, Inc.
2
* Copyright (C) 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
4
4
* This file is part of GnuPG.
40
40
#include <sys/wait.h>
45
#include <sys/resource.h>
46
#endif /*HAVE_GETRLIMIT*/
49
# include <sys/stat.h>
45
56
#include "exechelp.h"
47
58
/* Define to 1 do enable debugging. */
48
59
#define DEBUG_W32_SPAWN 1
51
#ifdef _POSIX_OPEN_MAX
52
#define MAX_OPEN_FDS _POSIX_OPEN_MAX
54
#define MAX_OPEN_FDS 20
57
62
/* We have the usual problem here: Some modules are linked against pth
58
63
and some are not. However we want to use pth_fork and pth_waitpid
59
64
here. Using a weak symbol works but is not portable - we should
93
/* Return the maximum number of currently allowed open file
94
descriptors. Only useful on POSIX systems but returns a value on
100
#ifdef HAVE_GETRLIMIT
103
# ifdef RLIMIT_NOFILE
104
if (!getrlimit (RLIMIT_NOFILE, &rl))
105
max_fds = rl.rlim_max;
109
if (max_fds == -1 && !getrlimit (RLIMIT_OFILE, &rl))
110
max_fds = rl.rlim_max;
113
#endif /*HAVE_GETRLIMIT*/
118
long int scres = sysconf (_SC_OPEN_MAX);
124
#ifdef _POSIX_OPEN_MAX
126
max_fds = _POSIX_OPEN_MAX;
135
max_fds = 256; /* Arbitrary limit. */
141
/* Close all file descriptors starting with descriptor FIRST. If
142
EXCEPT is not NULL, it is expected to be a list of file descriptors
143
which shall not be closed. This list shall be sorted in ascending
144
order with the end marked by -1. */
146
close_all_fds (int first, int *except)
148
int max_fd = get_max_fds ();
149
int fd, i, except_start;
154
for (fd=first; fd < max_fd; fd++)
156
for (i=except_start; except[i] != -1; i++)
160
/* If we found the descriptor in the exception list
161
we can start the next compare run at the next
162
index because the exception list is ordered. */
163
except_start = i + 1;
173
for (fd=first; fd < max_fd; fd++)
181
/* Returns an array with all currently open file descriptors. The end
182
of the array is marked by -1. The caller needs to release this
183
array using the *standard free* and not with xfree. This allow the
184
use of this fucntion right at startup even before libgcrypt has
185
been initialized. Returns NULL on error and sets ERRNO
188
get_all_open_fds (void)
194
array = calloc (1, sizeof *array);
200
max_fd = get_max_fds ();
201
narray = 32; /* If you change this change also t-exechelp.c. */
202
array = calloc (narray, sizeof *array);
206
/* Note: The list we return is ordered. */
207
for (idx=0, fd=0; fd < max_fd; fd++)
208
if (!(fstat (fd, &statbuf) == -1 && errno == EBADF))
214
narray += (narray < 256)? 32:256;
215
tmp = realloc (array, narray * sizeof *array);
88
232
#ifdef HAVE_W32_SYSTEM
89
233
/* Helper function to build_w32_commandline. */
333
472
This flag is only useful under W32 systems, so that no new
334
473
console is created and pops up a console window when
335
474
starting the server
476
Bit 6: On W32 run AllowSetForegroundWindow for the child. Due to
477
error problems this actually allows SetForegroundWindow for
478
childs of this process.
337
480
Returns 0 on success or an error code. */
430
573
/* " dwProcessID=%d dwThreadId=%d\n", */
431
574
/* pi.hProcess, pi.hThread, */
432
575
/* (int) pi.dwProcessId, (int) pi.dwThreadId); */
577
/* Fixme: For unknown reasons AllowSetForegroundWindow returns an
578
invalid argument error if we pass the correct processID to
579
it. As a workaround we use -1 (ASFW_ANY). */
581
gnupg_allow_set_foregound_window ((pid_t)(-1)/*pi.dwProcessId*/);
434
583
/* Process has been created suspended; resume it now. */
435
584
ResumeThread (pi.hThread);