2004-12-06 18:28:56 +00:00
|
|
|
/* exechelp.h - Definitions for the fork and exec helpers
|
2010-03-24 12:15:30 +00:00
|
|
|
* Copyright (C) 2004, 2009, 2010 Free Software Foundation, Inc.
|
2004-12-06 18:28:56 +00:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2004-12-06 18:28:56 +00:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2004-12-06 18:28:56 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2004-12-06 18:28:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GNUPG_COMMON_EXECHELP_H
|
|
|
|
#define GNUPG_COMMON_EXECHELP_H
|
|
|
|
|
2010-03-08 12:18:19 +00:00
|
|
|
|
2009-03-19 07:09:31 +00:00
|
|
|
/* Return the maximum number of currently allowed file descriptors.
|
|
|
|
Only useful on POSIX systems. */
|
|
|
|
int get_max_fds (void);
|
|
|
|
|
|
|
|
|
|
|
|
/* Close all file descriptors starting with descriptor FIRST. If
|
|
|
|
EXCEPT is not NULL, it is expected to be a list of file descriptors
|
|
|
|
which are not to close. This list shall be sorted in ascending
|
|
|
|
order with its end marked by -1. */
|
|
|
|
void close_all_fds (int first, int *except);
|
|
|
|
|
|
|
|
|
|
|
|
/* Returns an array with all currently open file descriptors. The end
|
|
|
|
of the array is marked by -1. The caller needs to release this
|
|
|
|
array using the *standard free* and not with xfree. This allow the
|
2015-11-16 12:41:46 +01:00
|
|
|
use of this function right at startup even before libgcrypt has
|
2009-03-19 07:09:31 +00:00
|
|
|
been initialized. Returns NULL on error and sets ERRNO accordingly. */
|
|
|
|
int *get_all_open_fds (void);
|
|
|
|
|
2004-12-06 18:28:56 +00:00
|
|
|
|
2007-08-29 16:59:20 +00:00
|
|
|
/* Portable function to create a pipe. Under Windows the write end is
|
2016-05-27 15:41:55 +02:00
|
|
|
inheritable. If R_FP is not NULL, an estream is created for the
|
|
|
|
write end and stored at R_FP. */
|
|
|
|
gpg_error_t gnupg_create_inbound_pipe (int filedes[2],
|
|
|
|
estream_t *r_fp, int nonblock);
|
2007-08-29 16:59:20 +00:00
|
|
|
|
2009-09-30 15:28:38 +00:00
|
|
|
/* Portable function to create a pipe. Under Windows the read end is
|
2016-05-27 15:41:55 +02:00
|
|
|
inheritable. If R_FP is not NULL, an estream is created for the
|
|
|
|
write end and stored at R_FP. */
|
|
|
|
gpg_error_t gnupg_create_outbound_pipe (int filedes[2],
|
|
|
|
estream_t *r_fp, int nonblock);
|
2009-09-30 15:28:38 +00:00
|
|
|
|
2016-01-14 14:14:25 +01:00
|
|
|
/* Portable function to create a pipe. Under Windows both ends are
|
|
|
|
inheritable. */
|
|
|
|
gpg_error_t gnupg_create_pipe (int filedes[2]);
|
|
|
|
|
|
|
|
|
2015-09-28 18:10:21 +02:00
|
|
|
#define GNUPG_SPAWN_NONBLOCK 16
|
|
|
|
#define GNUPG_SPAWN_RUN_ASFW 64
|
|
|
|
#define GNUPG_SPAWN_DETACHED 128
|
2004-12-06 18:28:56 +00:00
|
|
|
|
2015-09-28 18:10:21 +02:00
|
|
|
|
|
|
|
/* Fork and exec the program PGMNAME.
|
|
|
|
|
|
|
|
If R_INFP is NULL connect stdin of the new process to /dev/null; if
|
|
|
|
it is not NULL store the address of a pointer to a new estream
|
|
|
|
there. If R_OUTFP is NULL connect stdout of the new process to
|
|
|
|
/dev/null; if it is not NULL store the address of a pointer to a
|
|
|
|
new estream there. If R_ERRFP is NULL connect stderr of the new
|
|
|
|
process to /dev/null; if it is not NULL store the address of a
|
|
|
|
pointer to a new estream there. On success the pid of the new
|
|
|
|
process is stored at PID. On error -1 is stored at PID and if
|
|
|
|
R_OUTFP or R_ERRFP are not NULL, NULL is stored there.
|
2010-08-20 12:18:38 +00:00
|
|
|
|
|
|
|
The arguments for the process are expected in the NULL terminated
|
|
|
|
array ARGV. The program name itself should not be included there.
|
|
|
|
If PREEXEC is not NULL, the given function will be called right
|
2011-02-04 12:57:53 +01:00
|
|
|
before the exec.
|
2010-08-20 12:18:38 +00:00
|
|
|
|
2016-05-27 22:48:04 +02:00
|
|
|
IF EXCEPT is not NULL, it is expected to be an ordered list of file
|
|
|
|
descriptors, terminated by an entry with the value (-1). These
|
|
|
|
file descriptors won't be closed before spawning a new program.
|
|
|
|
|
2010-08-20 12:18:38 +00:00
|
|
|
Returns 0 on success or an error code. Calling gnupg_wait_process
|
|
|
|
and gnupg_release_process is required if the function succeeded.
|
2010-03-24 12:15:30 +00:00
|
|
|
|
|
|
|
FLAGS is a bit vector:
|
|
|
|
|
2015-09-28 18:10:21 +02:00
|
|
|
GNUPG_SPAWN_NONBLOCK
|
|
|
|
If set the two output streams are created in non-blocking
|
|
|
|
mode and the input stream is switched to non-blocking mode.
|
|
|
|
This is merely a convenience feature because the caller
|
|
|
|
could do the same with gpgrt_set_nonblock. Does not yet
|
|
|
|
work for Windows.
|
|
|
|
|
|
|
|
GNUPG_SPAWN_DETACHED
|
|
|
|
If set the process will be started as a background process.
|
2010-03-24 12:15:30 +00:00
|
|
|
This flag is only useful under W32 (but not W32CE) systems,
|
|
|
|
so that no new console is created and pops up a console
|
|
|
|
window when starting the server. Does not work on W32CE.
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2015-09-28 18:10:21 +02:00
|
|
|
GNUPG_SPAWN_RUN_ASFW
|
|
|
|
On W32 (but not on W32CE) run AllowSetForegroundWindow for
|
2010-03-24 12:15:30 +00:00
|
|
|
the child. Note that due to unknown problems this actually
|
2017-04-28 10:06:33 +09:00
|
|
|
allows SetForegroundWindow for all children of this process.
|
2010-03-24 12:15:30 +00:00
|
|
|
|
|
|
|
*/
|
2010-08-20 12:18:38 +00:00
|
|
|
gpg_error_t
|
|
|
|
gnupg_spawn_process (const char *pgmname, const char *argv[],
|
2016-05-27 22:48:04 +02:00
|
|
|
int *execpt, void (*preexec)(void), unsigned int flags,
|
2015-09-28 18:10:21 +02:00
|
|
|
estream_t *r_infp,
|
2010-08-20 12:18:38 +00:00
|
|
|
estream_t *r_outfp,
|
|
|
|
estream_t *r_errfp,
|
|
|
|
pid_t *pid);
|
2004-12-06 18:28:56 +00:00
|
|
|
|
2007-08-27 18:10:27 +00:00
|
|
|
|
|
|
|
/* Simplified version of gnupg_spawn_process. This function forks and
|
|
|
|
then execs PGMNAME, while connecting INFD to stdin, OUTFD to stdout
|
|
|
|
and ERRFD to stderr (any of them may be -1 to connect them to
|
|
|
|
/dev/null). The arguments for the process are expected in the NULL
|
|
|
|
terminated array ARGV. The program name itself should not be
|
2010-06-09 16:53:51 +00:00
|
|
|
included there. Calling gnupg_wait_process and
|
|
|
|
gnupg_release_process is required. Returns 0 on success or an
|
|
|
|
error code. */
|
2011-02-04 12:57:53 +01:00
|
|
|
gpg_error_t gnupg_spawn_process_fd (const char *pgmname,
|
2007-08-27 18:10:27 +00:00
|
|
|
const char *argv[],
|
|
|
|
int infd, int outfd, int errfd,
|
|
|
|
pid_t *pid);
|
|
|
|
|
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
/* If HANG is true, waits for the process identified by PID to exit;
|
|
|
|
if HANG is false, checks whether the process has terminated.
|
|
|
|
PGMNAME should be the same as supplied to the spawn function and is
|
|
|
|
only used for diagnostics. Return values:
|
|
|
|
|
|
|
|
0
|
|
|
|
The process exited successful. 0 is stored at R_EXITCODE.
|
|
|
|
|
|
|
|
GPG_ERR_GENERAL
|
|
|
|
The process exited without success. The exit code of process
|
|
|
|
is then stored at R_EXITCODE. An exit code of -1 indicates
|
|
|
|
that the process terminated abnormally (e.g. due to a signal).
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
GPG_ERR_TIMEOUT
|
2010-06-09 16:53:51 +00:00
|
|
|
The process is still running (returned only if HANG is false).
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
GPG_ERR_INV_VALUE
|
|
|
|
An invalid PID has been specified.
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
Other error codes may be returned as well. Unless otherwise noted,
|
|
|
|
-1 will be stored at R_EXITCODE. R_EXITCODE may be passed as NULL
|
2017-04-28 10:06:33 +09:00
|
|
|
if the exit code is not required (in that case an error message will
|
2010-06-09 16:53:51 +00:00
|
|
|
be printed). Note that under Windows PID is not the process id but
|
|
|
|
the handle of the process. */
|
|
|
|
gpg_error_t gnupg_wait_process (const char *pgmname, pid_t pid, int hang,
|
|
|
|
int *r_exitcode);
|
2004-12-06 18:28:56 +00:00
|
|
|
|
2016-01-14 18:20:14 +01:00
|
|
|
/* Like gnupg_wait_process, but for COUNT processes. */
|
|
|
|
gpg_error_t gnupg_wait_processes (const char **pgmnames, pid_t *pids,
|
|
|
|
size_t count, int hang, int *r_exitcodes);
|
|
|
|
|
2004-12-06 18:28:56 +00:00
|
|
|
|
2009-10-13 19:17:24 +00:00
|
|
|
/* Kill a process; that is send an appropriate signal to the process.
|
|
|
|
gnupg_wait_process must be called to actually remove the process
|
|
|
|
from the system. An invalid PID is ignored. */
|
|
|
|
void gnupg_kill_process (pid_t pid);
|
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
/* Release the process identified by PID. This function is actually
|
|
|
|
only required for Windows but it does not harm to always call it.
|
|
|
|
It is a nop if PID is invalid. */
|
|
|
|
void gnupg_release_process (pid_t pid);
|
|
|
|
|
2009-10-13 19:17:24 +00:00
|
|
|
|
2015-11-16 12:41:46 +01:00
|
|
|
/* Spawn a new process and immediately detach from it. The name of
|
2006-09-07 15:13:33 +00:00
|
|
|
the program to exec is PGMNAME and its arguments are in ARGV (the
|
|
|
|
programname is automatically passed as first argument).
|
|
|
|
Environment strings in ENVP are set. An error is returned if
|
|
|
|
pgmname is not executable; to make this work it is necessary to
|
|
|
|
provide an absolute file name. */
|
|
|
|
gpg_error_t gnupg_spawn_process_detached (const char *pgmname,
|
|
|
|
const char *argv[],
|
|
|
|
const char *envp[] );
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-06 18:28:56 +00:00
|
|
|
#endif /*GNUPG_COMMON_EXECHELP_H*/
|