chiark / gitweb /
dirmngr: Drop useless housekeeping.
[gnupg2.git] / common / exechelp.h
1 /* exechelp.h - Definitions for the fork and exec helpers
2  * Copyright (C) 2004, 2009, 2010 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * This file is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, see <https://www.gnu.org/licenses/>.
28  */
29
30 #ifndef GNUPG_COMMON_EXECHELP_H
31 #define GNUPG_COMMON_EXECHELP_H
32
33
34 /* Return the maximum number of currently allowed file descriptors.
35    Only useful on POSIX systems.  */
36 int get_max_fds (void);
37
38
39 /* Close all file descriptors starting with descriptor FIRST.  If
40    EXCEPT is not NULL, it is expected to be a list of file descriptors
41    which are not to close.  This list shall be sorted in ascending
42    order with its end marked by -1.  */
43 void close_all_fds (int first, int *except);
44
45
46 /* Returns an array with all currently open file descriptors.  The end
47    of the array is marked by -1.  The caller needs to release this
48    array using the *standard free* and not with xfree.  This allow the
49    use of this function right at startup even before libgcrypt has
50    been initialized.  Returns NULL on error and sets ERRNO accordingly.  */
51 int *get_all_open_fds (void);
52
53
54 /* Portable function to create a pipe.  Under Windows the write end is
55    inheritable.  If R_FP is not NULL, an estream is created for the
56    write end and stored at R_FP.  */
57 gpg_error_t gnupg_create_inbound_pipe (int filedes[2],
58                                        estream_t *r_fp, int nonblock);
59
60 /* Portable function to create a pipe.  Under Windows the read end is
61    inheritable.  If R_FP is not NULL, an estream is created for the
62    write end and stored at R_FP.  */
63 gpg_error_t gnupg_create_outbound_pipe (int filedes[2],
64                                         estream_t *r_fp, int nonblock);
65
66 /* Portable function to create a pipe.  Under Windows both ends are
67    inheritable.  */
68 gpg_error_t gnupg_create_pipe (int filedes[2]);
69
70
71 #define GNUPG_SPAWN_NONBLOCK   16
72 #define GNUPG_SPAWN_RUN_ASFW   64
73 #define GNUPG_SPAWN_DETACHED  128
74
75
76 /* Fork and exec the program PGMNAME.
77
78    If R_INFP is NULL connect stdin of the new process to /dev/null; if
79    it is not NULL store the address of a pointer to a new estream
80    there. If R_OUTFP is NULL connect stdout of the new process to
81    /dev/null; if it is not NULL store the address of a pointer to a
82    new estream there.  If R_ERRFP is NULL connect stderr of the new
83    process to /dev/null; if it is not NULL store the address of a
84    pointer to a new estream there.  On success the pid of the new
85    process is stored at PID.  On error -1 is stored at PID and if
86    R_OUTFP or R_ERRFP are not NULL, NULL is stored there.
87
88    The arguments for the process are expected in the NULL terminated
89    array ARGV.  The program name itself should not be included there.
90    If PREEXEC is not NULL, the given function will be called right
91    before the exec.
92
93    IF EXCEPT is not NULL, it is expected to be an ordered list of file
94    descriptors, terminated by an entry with the value (-1).  These
95    file descriptors won't be closed before spawning a new program.
96
97    Returns 0 on success or an error code.  Calling gnupg_wait_process
98    and gnupg_release_process is required if the function succeeded.
99
100    FLAGS is a bit vector:
101
102    GNUPG_SPAWN_NONBLOCK
103           If set the two output streams are created in non-blocking
104           mode and the input stream is switched to non-blocking mode.
105           This is merely a convenience feature because the caller
106           could do the same with gpgrt_set_nonblock.  Does not yet
107           work for Windows.
108
109    GNUPG_SPAWN_DETACHED
110           If set the process will be started as a background process.
111           This flag is only useful under W32 (but not W32CE) systems,
112           so that no new console is created and pops up a console
113           window when starting the server.  Does not work on W32CE.
114
115    GNUPG_SPAWN_RUN_ASFW
116           On W32 (but not on W32CE) run AllowSetForegroundWindow for
117           the child.  Note that due to unknown problems this actually
118           allows SetForegroundWindow for all childs of this process.
119
120  */
121 gpg_error_t
122 gnupg_spawn_process (const char *pgmname, const char *argv[],
123                      int *execpt, void (*preexec)(void), unsigned int flags,
124                      estream_t *r_infp,
125                      estream_t *r_outfp,
126                      estream_t *r_errfp,
127                      pid_t *pid);
128
129
130 /* Simplified version of gnupg_spawn_process.  This function forks and
131    then execs PGMNAME, while connecting INFD to stdin, OUTFD to stdout
132    and ERRFD to stderr (any of them may be -1 to connect them to
133    /dev/null).  The arguments for the process are expected in the NULL
134    terminated array ARGV.  The program name itself should not be
135    included there.  Calling gnupg_wait_process and
136    gnupg_release_process is required.  Returns 0 on success or an
137    error code. */
138 gpg_error_t gnupg_spawn_process_fd (const char *pgmname,
139                                     const char *argv[],
140                                     int infd, int outfd, int errfd,
141                                     pid_t *pid);
142
143
144 /* If HANG is true, waits for the process identified by PID to exit;
145    if HANG is false, checks whether the process has terminated.
146    PGMNAME should be the same as supplied to the spawn function and is
147    only used for diagnostics.  Return values:
148
149    0
150        The process exited successful.  0 is stored at R_EXITCODE.
151
152    GPG_ERR_GENERAL
153        The process exited without success.  The exit code of process
154        is then stored at R_EXITCODE.  An exit code of -1 indicates
155        that the process terminated abnormally (e.g. due to a signal).
156
157    GPG_ERR_TIMEOUT
158        The process is still running (returned only if HANG is false).
159
160    GPG_ERR_INV_VALUE
161        An invalid PID has been specified.
162
163    Other error codes may be returned as well.  Unless otherwise noted,
164    -1 will be stored at R_EXITCODE.  R_EXITCODE may be passed as NULL
165    if the exit code is not required (in that case an error messge will
166    be printed).  Note that under Windows PID is not the process id but
167    the handle of the process.  */
168 gpg_error_t gnupg_wait_process (const char *pgmname, pid_t pid, int hang,
169                                 int *r_exitcode);
170
171 /* Like gnupg_wait_process, but for COUNT processes.  */
172 gpg_error_t gnupg_wait_processes (const char **pgmnames, pid_t *pids,
173                                   size_t count, int hang, int *r_exitcodes);
174
175
176 /* Kill a process; that is send an appropriate signal to the process.
177    gnupg_wait_process must be called to actually remove the process
178    from the system.  An invalid PID is ignored.  */
179 void gnupg_kill_process (pid_t pid);
180
181 /* Release the process identified by PID.  This function is actually
182    only required for Windows but it does not harm to always call it.
183    It is a nop if PID is invalid.  */
184 void gnupg_release_process (pid_t pid);
185
186
187 /* Spawn a new process and immediately detach from it.  The name of
188    the program to exec is PGMNAME and its arguments are in ARGV (the
189    programname is automatically passed as first argument).
190    Environment strings in ENVP are set.  An error is returned if
191    pgmname is not executable; to make this work it is necessary to
192    provide an absolute file name.  */
193 gpg_error_t gnupg_spawn_process_detached (const char *pgmname,
194                                           const char *argv[],
195                                           const char *envp[] );
196
197
198
199 #endif /*GNUPG_COMMON_EXECHELP_H*/