Avoid catch-22 with README.main not being distributed but having the
[gnupg.git] / common / exechelp.h
blob0efee294c63641ed3cb6970e2219c5ecd5841f67
1 /* exechelp.h - Definitions for the fork and exec helpers
2 * Copyright (C) 2004, 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef GNUPG_COMMON_EXECHELP_H
21 #define GNUPG_COMMON_EXECHELP_H
23 /* Return the maximum number of currently allowed file descriptors.
24 Only useful on POSIX systems. */
25 int get_max_fds (void);
28 /* Close all file descriptors starting with descriptor FIRST. If
29 EXCEPT is not NULL, it is expected to be a list of file descriptors
30 which are not to close. This list shall be sorted in ascending
31 order with its end marked by -1. */
32 void close_all_fds (int first, int *except);
35 /* Returns an array with all currently open file descriptors. The end
36 of the array is marked by -1. The caller needs to release this
37 array using the *standard free* and not with xfree. This allow the
38 use of this fucntion right at startup even before libgcrypt has
39 been initialized. Returns NULL on error and sets ERRNO accordingly. */
40 int *get_all_open_fds (void);
43 /* Portable function to create a pipe. Under Windows the write end is
44 inheritable. */
45 gpg_error_t gnupg_create_inbound_pipe (int filedes[2]);
48 /* Fork and exec the PGMNAME, connect the file descriptor of INFILE to
49 stdin, write the output to OUTFILE, return a new stream in
50 STATUSFILE for stderr and the pid of the process in PID. The
51 arguments for the process are expected in the NULL terminated array
52 ARGV. The program name itself should not be included there. If
53 PREEXEC is not NULL, that function will be called right before the
54 exec. FLAGS is currently only useful for W32, see the source for
55 details. Calling gnupg_wait_process is required. Returns 0 on
56 success or an error code. */
57 gpg_error_t gnupg_spawn_process (const char *pgmname, const char *argv[],
58 FILE *infile, FILE *outfile,
59 void (*preexec)(void), unsigned int flags,
60 FILE **statusfile, pid_t *pid);
63 /* Simplified version of gnupg_spawn_process. This function forks and
64 then execs PGMNAME, while connecting INFD to stdin, OUTFD to stdout
65 and ERRFD to stderr (any of them may be -1 to connect them to
66 /dev/null). The arguments for the process are expected in the NULL
67 terminated array ARGV. The program name itself should not be
68 included there. Calling gnupg_wait_process is required. Returns 0
69 on success or an error code. */
70 gpg_error_t gnupg_spawn_process_fd (const char *pgmname,
71 const char *argv[],
72 int infd, int outfd, int errfd,
73 pid_t *pid);
76 /* Wait for the process identified by PID to terminate. PGMNAME should
77 be the same as supplied to the spawn fucntion and is only used for
78 diagnostics. Returns 0 if the process succeded, GPG_ERR_GENERAL
79 for any failures of the spawned program or other error codes. If
80 EXITCODE is not NULL the exit code of the process is stored at this
81 address or -1 if it could not be retrieved. */
82 gpg_error_t gnupg_wait_process (const char *pgmname, pid_t pid, int *exitcode);
85 /* Spawn a new process and immediatley detach from it. The name of
86 the program to exec is PGMNAME and its arguments are in ARGV (the
87 programname is automatically passed as first argument).
88 Environment strings in ENVP are set. An error is returned if
89 pgmname is not executable; to make this work it is necessary to
90 provide an absolute file name. */
91 gpg_error_t gnupg_spawn_process_detached (const char *pgmname,
92 const char *argv[],
93 const char *envp[] );
97 #endif /*GNUPG_COMMON_EXECHELP_H*/