aboutsummaryrefslogtreecommitdiff
path: root/src/rc/rc-logger.c
diff options
context:
space:
mode:
authorChristian Ruppert <idl0r@gentoo.org>2011-07-05 23:37:07 +0200
committerChristian Ruppert <idl0r@gentoo.org>2011-07-05 23:42:21 +0200
commit44dc7db4f89382fe198d1115a20d30ceb10cce02 (patch)
tree2ce23fa2048fff0bdfd8807f8f05e44f10b57856 /src/rc/rc-logger.c
parent003d4ba15be5f43eb868cf89e8c8c7db46e9dc94 (diff)
Improve logging
Add "e" (O_CLOEXEC) to all fopen() calls. Remove system() call and replace it by fopen() and friends. This also fixes bug 368933 X-Gentoo-Bug: 368933 X-Gentoo-Bug-URL: https://bugs.gentoo.org/368933
Diffstat (limited to 'src/rc/rc-logger.c')
-rw-r--r--src/rc/rc-logger.c54
1 files changed, 41 insertions, 13 deletions
diff --git a/src/rc/rc-logger.c b/src/rc/rc-logger.c
index 6178ba9b..593c16f5 100644
--- a/src/rc/rc-logger.c
+++ b/src/rc/rc-logger.c
@@ -57,10 +57,8 @@
#include "rc.h"
#include "rc-misc.h"
-#define LOGFILE RC_SVCDIR "/rc.log"
-#define PERMLOG "/var/log/rc.log"
-#define MOVELOG "cat " LOGFILE " 2>/dev/null >>" PERMLOG " && " \
- "rm -f " LOGFILE
+#define TMPLOG RC_SVCDIR "/rc.log"
+#define DEFAULTLOG "/var/log/rc.log"
static int signal_pipe[2] = { -1, -1 };
static int fd_stdout = -1;
@@ -149,12 +147,15 @@ rc_logger_open(const char *level)
int slave_tty;
struct termios tt;
struct winsize ws;
- char *buffer;
+ char buffer[BUFSIZ];
struct pollfd fd[2];
int s = 0;
size_t bytes;
int i;
FILE *log = NULL;
+ FILE *plog = NULL;
+ const char *logfile;
+ int log_error = 0;
if (!rc_conf_yesno("rc_logger"))
return;
@@ -192,7 +193,7 @@ rc_logger_open(const char *level)
signal_pipe[1] = -1;
runlevel = level;
- if ((log = fopen(LOGFILE, "a")))
+ if ((log = fopen(TMPLOG, "ae")))
write_time(log, "started");
else {
free(logbuf);
@@ -201,7 +202,6 @@ rc_logger_open(const char *level)
logbuf_len = 0;
}
- buffer = xmalloc(sizeof (char) * BUFSIZ);
fd[0].fd = signal_pipe[0];
fd[0].events = fd[1].events = POLLIN;
fd[0].revents = fd[1].revents = 0;
@@ -242,9 +242,8 @@ rc_logger_open(const char *level)
if (fd[0].revents & (POLLIN | POLLHUP))
break;
}
- free(buffer);
if (logbuf) {
- if ((log = fopen(LOGFILE, "a"))) {
+ if ((log = fopen(TMPLOG, "ae"))) {
write_time(log, "started");
write_log(fileno(log), logbuf, logbuf_len);
}
@@ -255,10 +254,39 @@ rc_logger_open(const char *level)
fclose(log);
}
- /* Try and cat our new logfile to a more permament location
- and then punt it */
- if (system(MOVELOG) == -1)
- eerror("system: %s: %s", MOVELOG, strerror(errno));
+ /* Append the temporary log to the real log */
+ logfile = rc_conf_value("rc_logger_path");
+ if (logfile == NULL)
+ logfile = DEFAULTLOG;
+
+ if ((plog = fopen(logfile, "ae"))) {
+ if ((log = fopen(TMPLOG, "re"))) {
+ while ((bytes = fread(buffer, sizeof(*buffer), BUFSIZ, log)) > 0) {
+ if (fwrite(buffer, sizeof(*buffer), bytes, plog) < bytes) {
+ log_error = 1;
+ eerror("Error: write(%s) failed: %s", logfile, strerror(errno));
+ break;
+ }
+ }
+ } else {
+ log_error = 1;
+ eerror("Error: fopen(%s) failed: %s", TMPLOG, strerror(errno));
+ }
+
+ fclose(log);
+ fclose(plog);
+ } else {
+ log_error = 1;
+ eerror("Error: fopen(%s) failed: %s", logfile, strerror(errno));
+ }
+
+ /* Try to keep the temporary log in case of errors */
+ if (!log_error) {
+ if (unlink(TMPLOG) == -1)
+ eerror("Error: unlink(%s) failed: %s", TMPLOG, strerror(errno));
+ } else if (exists(TMPLOG))
+ eerrorx("Warning: temporary logfile left behind: %s", TMPLOG);
+
exit(0);
/* NOTREACHED */