From 47732e9742ae1f898f161b4244e7faed74c98756 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Tue, 29 Apr 2014 13:54:53 -0700 Subject: logging: use duplicate stderr, instead of re-opening The special filename "-" is supposed to log to stderr. Instead of trying to explictly open "/dev/stderr" again (which may not be possible as permissions might have changed by then), dup the stderr and use the copy. It is not a good idea to use @stderr global variable directly, as ctx->log.logfile is fclose()d in glfs_fini() (was fixed in http://review.gluster.org/6452) Cherry picked from commit 15ea78fffd63756fecf2f15887d3cad6a13d2a34: > BUG: 1088589 > Signed-off-by: Anand Avati > Reviewed-on: http://review.gluster.org/7607 > Tested-by: Gluster Build System > Reviewed-by: Vijay Bellur Change-Id: Ia6c538fe363905588dcf4fc4783804073956a586 BUG: 1103413 Signed-off-by: Niels de Vos Reviewed-on: http://review.gluster.org/7938 Tested-by: Gluster Build System Reviewed-by: Harshavardhana --- libglusterfs/src/logging.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'libglusterfs/src/logging.c') diff --git a/libglusterfs/src/logging.c b/libglusterfs/src/logging.c index 4a905a24def..bb1b9884ae6 100644 --- a/libglusterfs/src/logging.c +++ b/libglusterfs/src/logging.c @@ -366,7 +366,29 @@ gf_log_init (void *data, const char *file, const char *ident) } if (strcmp (file, "-") == 0) { - file = "/dev/stderr"; + int dupfd = -1; + + ctx->log.filename = gf_strdup ("/dev/stderr"); + if (!ctx->log.filename) { + fprintf (stderr, "ERROR: strdup failed\n"); + return -1; + } + + dupfd = dup (fileno (stderr)); + if (dupfd == -1) { + fprintf (stderr, "ERROR: could not dup %d (%s)\n", + fileno (stderr), strerror (errno)); + return -1; + } + + ctx->log.logfile = fdopen (dupfd, "a"); + if (!ctx->log.logfile) { + fprintf (stderr, "ERROR: could not fdopen on %d (%s)\n", + dupfd, strerror (errno)); + return -1; + } + + goto out; } ctx->log.filename = gf_strdup (file); @@ -390,7 +412,7 @@ gf_log_init (void *data, const char *file, const char *ident) file, strerror (errno)); return -1; } - +out: ctx->log.gf_log_logfile = ctx->log.logfile; return 0; -- cgit