]> mj.ucw.cz Git - libucw.git/commitdiff
Logging: Added log_drop_stderr().
authorMartin Mares <mj@ucw.cz>
Thu, 12 Feb 2015 15:41:26 +0000 (16:41 +0100)
committerMartin Mares <mj@ucw.cz>
Thu, 12 Feb 2015 15:41:26 +0000 (16:41 +0100)
When writing the daemonization module, I assumed that it is not
necessary to close stderr, because it will be replaced by a log
file later. Alas, this is not true for general log streams.

I have added a function, which should be called by the daemon
when it is done with initialization of logging.

maint/libucw.abi
ucw/doc/daemon.txt
ucw/log-file.c
ucw/log.h

index e81ac9c0b15c6fab3c141288036925096101dfea..813398f259507c581894fe4a0a77f4e3c544a64a 100644 (file)
@@ -304,6 +304,7 @@ log_new_fd
 log_switch_disable
 log_switch_enable
 log_switch
+log_drop_stderr
 log_new_syslog
 log_syslog_facility_exists
 log_new_configured
index 58d550f2d4b7130f732106738565519c70050182..f6037b60b004301285d3d6be565a9451ec383caf 100644 (file)
@@ -14,7 +14,8 @@ helper which performs the necessary actions. Namely:
   set according to `/etc/passwd` and `/etc/group`.
 * Redirecting standard input and output from `/dev/null`. Standard error
   output is left open, so that error messages can be printed before you
-  set up proper <<log:,logging>>.
+  set up proper <<log:,logging>>. If you are not sure that your log streams
+  replaced stderr, you can call <<log:log_drop_stderr()>> to do that.
 * Setting the `umask()` to a fixed value (022).
 * Switching from the current directory to `/`, so that it is not kept busy.
 * Writing a PID file. While the daemon is running, the PID file is kept locked
index 40f1f3964e487e21e7d85834f194d3b3bdd67d39..40871418d7399fc92fff7b618fd9ad72f7a9004e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- Logging to Files
  *
- *     (c) 1997--2009 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2015 Martin Mares <mj@ucw.cz>
  *     (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
@@ -32,6 +32,7 @@ struct file_stream {
 #define MAX_EXPAND 64          // Maximum size of expansion of strftime escapes
 
 static int log_switch_nest;
+static bool log_stderr_replaced;
 
 static void
 do_log_reopen(struct file_stream *fs, const char *name)
@@ -43,7 +44,10 @@ do_log_reopen(struct file_stream *fs, const char *name)
     close(fs->fd);
   fs->fd = fd;
   if (fs->flags & FF_FD2_FOLLOWS)
-    dup2(fd, 2);
+    {
+      dup2(fd, 2);
+      log_stderr_replaced = 1;
+    }
   if (fs->ls.name)
     {
       xfree(fs->ls.name);
@@ -181,6 +185,17 @@ log_file(const char *name)
   log_set_default_stream(log_new_file(name, FF_FD2_FOLLOWS));
 }
 
+void
+log_drop_stderr(void)
+{
+  if (!log_stderr_replaced)
+    {
+      if (dup2(1, 2) < 0)
+       die("Cannot get rid of stderr: %m");
+      log_stderr_replaced = 1;
+    }
+}
+
 #ifdef TEST
 
 int main(int argc, char **argv)
index 6ff2d4e56b8d82669dfbadf4848c0691cf3227de..7724c6c07f3bbbe68e83c77183be18e1bf9dfc69 100644 (file)
--- a/ucw/log.h
+++ b/ucw/log.h
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- Logging
  *
- *     (c) 1997--2009 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2015 Martin Mares <mj@ucw.cz>
  *     (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
@@ -19,6 +19,7 @@
 #define log_close_all ucw_log_close_all
 #define log_close_stream ucw_log_close_stream
 #define log_configured ucw_log_configured
+#define log_drop_stderr ucw_log_drop_stderr
 #define log_find_type ucw_log_find_type
 #define log_new_configured ucw_log_new_configured
 #define log_new_fd ucw_log_new_fd
@@ -258,6 +259,15 @@ void log_switch_disable(void);
 void log_switch_enable(void);          /** Negate the effect of log_switch_disable(). **/
 int log_switch(void);                  /** Switch log files manually. **/
 
+/**
+ * Drop stderr if it is not already redirected to a log file.
+ * This is usually needed in daemons to make sure that the original
+ * stderr does not stay open (stdin and stdout are dropped by our
+ * <<daemon:,daemon setup functions>> automatically). More specifically,
+ * it makes stderr a clone of stdout.
+ **/
+void log_drop_stderr(void);
+
 /***
  * === Logging to syslog
  *