diff options
author | Werner Koch <wk@gnupg.org> | 2007-12-03 14:05:15 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2007-12-03 14:05:15 +0100 |
commit | 033a2c0bc96c406bf324bff51891cfdefe42183e (patch) | |
tree | c15e16d8e55368f814d5011c4e84202c46509fa4 /common | |
parent | Fixed make distcheck (diff) | |
download | gnupg2-033a2c0bc96c406bf324bff51891cfdefe42183e.tar.xz gnupg2-033a2c0bc96c406bf324bff51891cfdefe42183e.zip |
Try to make sure that the standard descriptors are connected when calling
gpgsm.
Diffstat (limited to 'common')
-rw-r--r-- | common/ChangeLog | 4 | ||||
-rw-r--r-- | common/sysutils.c | 76 | ||||
-rw-r--r-- | common/sysutils.h | 1 |
3 files changed, 80 insertions, 1 deletions
diff --git a/common/ChangeLog b/common/ChangeLog index 1ebfe2984..867eeb57d 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,3 +1,7 @@ +2007-12-03 Werner Koch <wk@g10code.com> + + * sysutils.c (gnupg_reopen_std): New. Taken from ../g10/gpg.c. + 2007-11-27 Werner Koch <wk@g10code.com> * Makefile.am (CLEANFILES): New. diff --git a/common/sysutils.c b/common/sysutils.c index 7c8a3be2e..869dc2a10 100644 --- a/common/sysutils.c +++ b/common/sysutils.c @@ -48,6 +48,7 @@ #ifdef HAVE_PTH # include <pth.h> #endif +#include <fcntl.h> #include "util.h" #include "i18n.h" @@ -119,7 +120,7 @@ enable_core_dumps (void) return 1; limit.rlim_cur = limit.rlim_max; setrlimit (RLIMIT_CORE, &limit); - return 1; /* We always return true because trhis function is + return 1; /* We always return true because this function is merely a debugging aid. */ # endif return 1; @@ -397,3 +398,76 @@ gnupg_tmpfile (void) return tmpfile (); #endif /*!HAVE_W32_SYSTEM*/ } + + +/* Make sure that the standard file descriptors are opened. Obviously + some folks close them before an exec and the next file we open will + get one of them assigned and thus any output (i.e. diagnostics) end + up in that file (e.g. the trustdb). Not actually a gpg problem as + this will hapen with almost all utilities when called in a wrong + way. However we try to minimize the damage here and raise + awareness of the problem. + + Must be called before we open any files! */ +void +gnupg_reopen_std (const char *pgmname) +{ +#if defined(HAVE_STAT) && !defined(HAVE_W32_SYSTEM) + struct stat statbuf; + int did_stdin = 0; + int did_stdout = 0; + int did_stderr = 0; + FILE *complain; + + if (fstat (STDIN_FILENO, &statbuf) == -1 && errno ==EBADF) + { + if (open ("/dev/null",O_RDONLY) == STDIN_FILENO) + did_stdin = 1; + else + did_stdin = 2; + } + + if (fstat (STDOUT_FILENO, &statbuf) == -1 && errno == EBADF) + { + if (open ("/dev/null",O_WRONLY) == STDOUT_FILENO) + did_stdout = 1; + else + did_stdout = 2; + } + + if (fstat (STDERR_FILENO, &statbuf)==-1 && errno==EBADF) + { + if (open ("/dev/null", O_WRONLY) == STDERR_FILENO) + did_stderr = 1; + else + did_stderr = 2; + } + + /* It's hard to log this sort of thing since the filehandle we would + complain to may be closed... */ + if (!did_stderr) + complain = stderr; + else if (!did_stdout) + complain = stdout; + else + complain = NULL; + + if (complain) + { + if (did_stdin == 1) + fprintf (complain, "%s: WARNING: standard input reopened\n", pgmname); + if (did_stdout == 1) + fprintf (complain, "%s: WARNING: standard output reopened\n", pgmname); + if (did_stderr == 1) + fprintf (complain, "%s: WARNING: standard error reopened\n", pgmname); + + if (did_stdin == 2 || did_stdout == 2 || did_stderr == 2) + fprintf(complain,"%s: fatal: unable to reopen standard input," + " output, or error\n", pgmname); + } + + if (did_stdin == 2 || did_stdout == 2 || did_stderr == 2) + exit (3); +#endif /* HAVE_STAT && !HAVE_W32_SYSTEM */ +} + diff --git a/common/sysutils.h b/common/sysutils.h index c053e8fb9..44f7ca68c 100644 --- a/common/sysutils.h +++ b/common/sysutils.h @@ -46,6 +46,7 @@ void gnupg_sleep (unsigned int seconds); int translate_sys2libc_fd (gnupg_fd_t fd, int for_write); int translate_sys2libc_fd_int (int fd, int for_write); FILE *gnupg_tmpfile (void); +void gnupg_reopen_std (const char *pgmname); #ifdef HAVE_W32_SYSTEM |