From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QeEUD-0005HI-W6 for garchives@archives.gentoo.org; Tue, 05 Jul 2011 22:52:58 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id E253821C042; Tue, 5 Jul 2011 22:52:35 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 984F021C02F for ; Tue, 5 Jul 2011 22:52:35 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 2196E1B4006 for ; Tue, 5 Jul 2011 22:52:35 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 6E8A18003D for ; Tue, 5 Jul 2011 22:52:34 +0000 (UTC) From: "Christian Ruppert" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Christian Ruppert" Message-ID: <44dc7db4f89382fe198d1115a20d30ceb10cce02.idl0r@gentoo> Subject: [gentoo-commits] proj/openrc:master commit in: src/rc/ X-VCS-Repository: proj/openrc X-VCS-Files: src/rc/rc-logger.c X-VCS-Directories: src/rc/ X-VCS-Committer: idl0r X-VCS-Committer-Name: Christian Ruppert X-VCS-Revision: 44dc7db4f89382fe198d1115a20d30ceb10cce02 Date: Tue, 5 Jul 2011 22:52:34 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: ece1bb6e90643db48d4010827e4da290 commit: 44dc7db4f89382fe198d1115a20d30ceb10cce02 Author: Christian Ruppert gentoo org> AuthorDate: Tue Jul 5 21:37:07 2011 +0000 Commit: Christian Ruppert gentoo org> CommitDate: Tue Jul 5 21:42:21 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/openrc.git;a=3D= commit;h=3D44dc7db4 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 --- src/rc/rc-logger.c | 54 +++++++++++++++++++++++++++++++++++++++-------= ----- 1 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/rc/rc-logger.c b/src/rc/rc-logger.c index 6178ba9..593c16f 100644 --- a/src/rc/rc-logger.c +++ b/src/rc/rc-logger.c @@ -57,10 +57,8 @@ #include "rc.h" #include "rc-misc.h" =20 -#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" =20 static int signal_pipe[2] =3D { -1, -1 }; static int fd_stdout =3D -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 =3D 0; size_t bytes; int i; FILE *log =3D NULL; + FILE *plog =3D NULL; + const char *logfile; + int log_error =3D 0; =20 if (!rc_conf_yesno("rc_logger")) return; @@ -192,7 +193,7 @@ rc_logger_open(const char *level) signal_pipe[1] =3D -1; =20 runlevel =3D level; - if ((log =3D fopen(LOGFILE, "a"))) + if ((log =3D fopen(TMPLOG, "ae"))) write_time(log, "started"); else { free(logbuf); @@ -201,7 +202,6 @@ rc_logger_open(const char *level) logbuf_len =3D 0; } =20 - buffer =3D xmalloc(sizeof (char) * BUFSIZ); fd[0].fd =3D signal_pipe[0]; fd[0].events =3D fd[1].events =3D POLLIN; fd[0].revents =3D fd[1].revents =3D 0; @@ -242,9 +242,8 @@ rc_logger_open(const char *level) if (fd[0].revents & (POLLIN | POLLHUP)) break; } - free(buffer); if (logbuf) { - if ((log =3D fopen(LOGFILE, "a"))) { + if ((log =3D 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); } =20 - /* Try and cat our new logfile to a more permament location - and then punt it */ - if (system(MOVELOG) =3D=3D -1) - eerror("system: %s: %s", MOVELOG, strerror(errno)); + /* Append the temporary log to the real log */ + logfile =3D rc_conf_value("rc_logger_path"); + if (logfile =3D=3D NULL) + logfile =3D DEFAULTLOG; + + if ((plog =3D fopen(logfile, "ae"))) { + if ((log =3D fopen(TMPLOG, "re"))) { + while ((bytes =3D fread(buffer, sizeof(*buffer), BUFSIZ, log)) > 0) = { + if (fwrite(buffer, sizeof(*buffer), bytes, plog) < bytes) { + log_error =3D 1; + eerror("Error: write(%s) failed: %s", logfile, strerror(errno)); + break; + } + } + } else { + log_error =3D 1; + eerror("Error: fopen(%s) failed: %s", TMPLOG, strerror(errno)); + } + + fclose(log); + fclose(plog); + } else { + log_error =3D 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) =3D=3D -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 */ =20