log_switch_disable
log_switch_enable
log_switch
+log_drop_stderr
log_new_syslog
log_syslog_facility_exists
log_new_configured
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
/*
* 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
#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)
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);
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)
/*
* 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
#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
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
*