diff options
-rw-r--r-- | agent/ChangeLog | 5 | ||||
-rw-r--r-- | agent/gpg-agent.c | 24 | ||||
-rw-r--r-- | jnlib/ChangeLog | 10 | ||||
-rw-r--r-- | jnlib/logging.c | 77 | ||||
-rw-r--r-- | jnlib/logging.h | 8 |
5 files changed, 91 insertions, 33 deletions
diff --git a/agent/ChangeLog b/agent/ChangeLog index b20ee42e0..43bdb7ab5 100644 --- a/agent/ChangeLog +++ b/agent/ChangeLog @@ -1,3 +1,8 @@ +2004-04-16 Werner Koch <wk@gnupg.org> + + * gpg-agent.c (main): Tell the logging code taht we are runnign + detached. + 2004-04-06 Werner Koch <wk@gnupg.org> * gpg-agent.c (main): Use new libgcrypt thread library register diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c index 0fc1bb8bf..18a456f19 100644 --- a/agent/gpg-agent.c +++ b/agent/gpg-agent.c @@ -389,7 +389,7 @@ main (int argc, char **argv ) /* Please note that we may running SUID(ROOT), so be very CAREFUL when adding any stuff between here and the call to INIT_SECMEM() somewhere after the option parsing */ - log_set_prefix ("gpg-agent", 1|4); + log_set_prefix ("gpg-agent", JNLIB_LOG_WITH_PREFIX|JNLIB_LOG_WITH_PID); /* Try to auto set the character set. */ set_native_charset (NULL); @@ -652,11 +652,13 @@ main (int argc, char **argv ) bind_textdomain_codeset (PACKAGE_GT, "UTF-8"); #endif - /* now start with logging to a file if this is desired */ + /* Now start with logging to a file if this is desired. */ if (logfile) { log_set_file (logfile); - log_set_prefix (NULL, 1|2|4); + log_set_prefix (NULL, (JNLIB_LOG_WITH_PREFIX + |JNLIB_LOG_WITH_TIME + |JNLIB_LOG_WITH_PID)); } /* Make sure that we have a default ttyname. */ @@ -754,7 +756,7 @@ main (int argc, char **argv ) exit (1); } else if (pid) - { /* we are the parent */ + { /* We are the parent */ char *infostr; close (fd); @@ -803,17 +805,20 @@ main (int argc, char **argv ) } /* end parent */ - /* this is the child */ + /* + This is the child + */ - /* detach from tty and put process into a new session */ + /* Detach from tty and put process into a new session */ if (!nodetach ) { int i; + unsigned int oldflags; - /* close stdin, stdout and stderr unless it is the log stream */ + /* Close stdin, stdout and stderr unless it is the log stream */ for (i=0; i <= 2; i++) { - if ( log_get_fd () != i) + if (!log_test_fd (i) ) close (i); } if (setsid() == -1) @@ -822,6 +827,9 @@ main (int argc, char **argv ) cleanup (); exit (1); } + + log_get_prefix (&oldflags); + log_set_prefix (NULL, oldflags | JNLIB_LOG_RUN_DETACHED); opt.running_detached = 1; } diff --git a/jnlib/ChangeLog b/jnlib/ChangeLog index 6867b4fb9..99c9177b5 100644 --- a/jnlib/ChangeLog +++ b/jnlib/ChangeLog @@ -1,3 +1,11 @@ +2004-04-16 Werner Koch <wk@gnupg.org> + + * logging.h (JNLIB_LOG_WITH_PREFIX): Add constants for the flag + values. + * logging.c (log_set_prefix): New flag DETACHED. + (fun_writer): Take care of this flag. + (log_test_fd): New. + 2004-02-18 Werner Koch <wk@gnupg.org> * stringhelp.c (print_sanitized_buffer): Don't care about @@ -181,7 +189,7 @@ Mon Jan 24 13:04:28 CET 2000 Werner Koch <wk@gnupg.de> * You may find it source-copied in other packages. * *********************************************************** - Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc. + Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. This file is free software; as a special exception the author gives unlimited permission to copy and/or distribute it, with or without diff --git a/jnlib/logging.c b/jnlib/logging.c index 6a12d8771..fdf2d7fcb 100644 --- a/jnlib/logging.c +++ b/jnlib/logging.c @@ -1,5 +1,6 @@ /* logging.c - useful logging functions - * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc. + * Copyright (C) 1998, 1999, 2000, 2001, 2003, + * 2004 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -46,10 +47,12 @@ static FILE *logstream; +static int log_socket = -1; static char prefix_buffer[80]; static int with_time; static int with_prefix; static int with_pid; +static int running_detached; static int force_prefixes; static int missing_lf; @@ -125,9 +128,14 @@ fun_writer (void *cookie_arg, const char *buffer, size_t size) { struct fun_cookie_s *cookie = cookie_arg; - /* Note that we always try to reconnect to the socket but print error - messages only the first time an error occured. */ - if (cookie->fd == -1 ) + /* Note that we always try to reconnect to the socket but print + error messages only the first time an error occured. IF + RUNNING_DETACHED is set we don't fall back to stderr and even do + not print any error messages. This is needed becuase detached + processes often close stderr and my printing to fiel descriptor 2 + we might send the log message to a file not intended for logging + (e.g. a pipe or network connection). */ + if (cookie->fd == -1) { /* Note yet open or meanwhile closed due to an error. */ struct sockaddr_un addr; @@ -136,11 +144,12 @@ fun_writer (void *cookie_arg, const char *buffer, size_t size) cookie->fd = socket (PF_LOCAL, SOCK_STREAM, 0); if (cookie->fd == -1) { - if (!cookie->quiet) + if (!cookie->quiet && !running_detached) fprintf (stderr, "failed to create socket for logging: %s\n", strerror(errno)); goto failure; } + log_socket = cookie->fd; memset (&addr, 0, sizeof addr); addr.sun_family = PF_LOCAL; @@ -151,7 +160,8 @@ fun_writer (void *cookie_arg, const char *buffer, size_t size) if (connect (cookie->fd, (struct sockaddr *) &addr, addrlen) == -1) { - if (!cookie->quiet) + log_socket = -1; + if (!cookie->quiet && !running_detached) fprintf (stderr, "can't connect to `%s': %s\n", cookie->name, strerror(errno)); close (cookie->fd); @@ -165,19 +175,24 @@ fun_writer (void *cookie_arg, const char *buffer, size_t size) if (!writen (cookie->fd, buffer, size)) return size; /* Okay. */ - fprintf (stderr, "error writing to `%s': %s\n", - cookie->name, strerror(errno)); + log_socket = -1; + if (!running_detached) + fprintf (stderr, "error writing to `%s': %s\n", + cookie->name, strerror(errno)); close (cookie->fd); cookie->fd = -1; failure: - if (!cookie->quiet) + if (!running_detached) { - fputs ("switching logging to stderr\n", stderr); - cookie->quiet = 1; + if (!cookie->quiet) + { + fputs ("switching logging to stderr\n", stderr); + cookie->quiet = 1; + } + + fwrite (buffer, size, 1, stderr); } - - fwrite (buffer, size, 1, stderr); return size; } @@ -297,9 +312,10 @@ log_set_prefix (const char *text, unsigned int flags) prefix_buffer[sizeof (prefix_buffer)-1] = 0; } - with_prefix = (flags & 1); - with_time = (flags & 2); - with_pid = (flags & 4); + with_prefix = (flags & JNLIB_LOG_WITH_PREFIX); + with_time = (flags & JNLIB_LOG_WITH_TIME); + with_pid = (flags & JNLIB_LOG_WITH_PID); + running_detached = (flags & JNLIB_LOG_RUN_DETACHED); } @@ -310,30 +326,45 @@ log_get_prefix (unsigned int *flags) { *flags = 0; if (with_prefix) - *flags |= 1; + *flags |= JNLIB_LOG_WITH_PREFIX; if (with_time) - *flags |= 2; + *flags |= JNLIB_LOG_WITH_TIME; if (with_pid) - *flags |=4; + *flags |= JNLIB_LOG_WITH_PID; + if (running_detached) + *flags |= JNLIB_LOG_RUN_DETACHED; } return prefix_buffer; } +/* This function returns true if the file descriptor FD is in use for + logging. This is preferable over a test using log_get_fd in that + it allows the logging code to use more then one file descriptor. */ +int +log_test_fd (int fd) +{ + if (fileno (logstream?logstream:stderr) == fd) + return 1; + if (log_socket == fd) + return 1; + return 0; +} + int -log_get_fd() +log_get_fd () { - return fileno(logstream?logstream:stderr); + return fileno(logstream?logstream:stderr); } FILE * log_get_stream () { - return logstream?logstream:stderr; + return logstream?logstream:stderr; } static void -do_logv( int level, const char *fmt, va_list arg_ptr ) +do_logv (int level, const char *fmt, va_list arg_ptr) { if (!logstream) logstream = stderr; diff --git a/jnlib/logging.h b/jnlib/logging.h index 78d2b020d..b5c0bd741 100644 --- a/jnlib/logging.h +++ b/jnlib/logging.h @@ -1,5 +1,5 @@ /* logging.h - * Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. + * Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -24,6 +24,11 @@ #include <stdio.h> #include "mischelp.h" +/* Flag values for log_set_prefix. */ +#define JNLIB_LOG_WITH_PREFIX 1 +#define JNLIB_LOG_WITH_TIME 2 +#define JNLIB_LOG_WITH_PID 4 +#define JNLIB_LOG_RUN_DETACHED 256 int log_get_errorcount (int clear); void log_inc_errorcount (void); @@ -31,6 +36,7 @@ void log_set_file( const char *name ); void log_set_fd (int fd); void log_set_prefix (const char *text, unsigned int flags); const char *log_get_prefix (unsigned int *flags); +int log_test_fd (int fd); int log_get_fd(void); FILE *log_get_stream (void); |