2007-10-31 21:16:56 +05:30
|
|
|
/*
|
2009-04-24 03:01:22 +05:30
|
|
|
rc-logger.c
|
|
|
|
Spawns a logging daemon to capture stdout and stderr so we can log
|
|
|
|
them to a buffer and/or files.
|
|
|
|
*/
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2008-01-14 10:35:22 +05:30
|
|
|
/*
|
2009-05-01 19:41:40 +05:30
|
|
|
* Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
|
2011-06-30 05:16:31 +05:30
|
|
|
*
|
2007-11-14 20:52:04 +05:30
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-10-31 21:16:56 +05:30
|
|
|
#include <sys/types.h>
|
2008-01-10 04:52:04 +05:30
|
|
|
#include <sys/ioctl.h>
|
2007-10-31 21:16:56 +05:30
|
|
|
#include <sys/wait.h>
|
2008-03-16 22:30:56 +05:30
|
|
|
|
2007-10-31 21:16:56 +05:30
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
2008-11-28 02:44:43 +05:30
|
|
|
#include <poll.h>
|
2007-10-31 21:16:56 +05:30
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef __linux__
|
2009-04-24 03:01:22 +05:30
|
|
|
# include <pty.h>
|
2008-01-10 04:52:04 +05:30
|
|
|
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
2009-04-24 03:01:22 +05:30
|
|
|
# include <util.h>
|
2007-10-31 21:16:56 +05:30
|
|
|
#else
|
2009-04-24 03:01:22 +05:30
|
|
|
# include <libutil.h>
|
2007-10-31 21:16:56 +05:30
|
|
|
#endif
|
|
|
|
|
2008-01-06 19:17:39 +05:30
|
|
|
#include "einfo.h"
|
2007-10-31 21:16:56 +05:30
|
|
|
#include "rc-logger.h"
|
|
|
|
#include "rc.h"
|
2008-07-03 04:50:04 +05:30
|
|
|
#include "rc-misc.h"
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2011-07-06 03:07:07 +05:30
|
|
|
#define TMPLOG RC_SVCDIR "/rc.log"
|
|
|
|
#define DEFAULTLOG "/var/log/rc.log"
|
2007-10-31 21:16:56 +05:30
|
|
|
|
|
|
|
static int signal_pipe[2] = { -1, -1 };
|
|
|
|
static int fd_stdout = -1;
|
|
|
|
static int fd_stderr = -1;
|
|
|
|
static const char *runlevel = NULL;
|
|
|
|
static bool in_escape = false;
|
|
|
|
static bool in_term = false;
|
|
|
|
|
|
|
|
static char *logbuf = NULL;
|
|
|
|
static size_t logbuf_size = 0;
|
|
|
|
static size_t logbuf_len = 0;
|
|
|
|
|
|
|
|
pid_t rc_logger_pid = -1;
|
|
|
|
int rc_logger_tty = -1;
|
|
|
|
bool rc_in_logger = false;
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static void
|
|
|
|
write_log(int logfd, const char *buffer, size_t bytes)
|
2007-10-31 21:16:56 +05:30
|
|
|
{
|
|
|
|
const char *p = buffer;
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
while ((size_t)(p - buffer) < bytes) {
|
2007-10-31 21:16:56 +05:30
|
|
|
switch (*p) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case '\r':
|
|
|
|
goto cont;
|
|
|
|
case '\033':
|
|
|
|
in_escape = true;
|
|
|
|
in_term = false;
|
|
|
|
goto cont;
|
|
|
|
case '\n':
|
|
|
|
in_escape = in_term = false;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
if (in_escape)
|
|
|
|
in_term = true;
|
|
|
|
break;
|
2007-10-31 21:16:56 +05:30
|
|
|
}
|
2008-01-11 21:21:40 +05:30
|
|
|
|
2009-05-01 00:26:43 +05:30
|
|
|
if (!in_escape) {
|
|
|
|
if (write(logfd, p++, 1) == -1)
|
|
|
|
eerror("write: %s", strerror(errno));
|
2007-10-31 21:16:56 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-04-17 15:49:58 +05:30
|
|
|
if (! in_term || isalpha((unsigned char)*p))
|
2007-10-31 21:16:56 +05:30
|
|
|
in_escape = in_term = false;
|
|
|
|
cont:
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
2009-04-24 03:01:22 +05:30
|
|
|
|
|
|
|
static void
|
|
|
|
write_time(FILE *f, const char *s)
|
2007-10-31 21:16:56 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
time_t now = time(NULL);
|
|
|
|
struct tm *tm = localtime(&now);
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(f, "\nrc %s logging %s at %s\n", runlevel, s, asctime(tm));
|
|
|
|
fflush(f);
|
2007-10-31 21:16:56 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
void
|
|
|
|
rc_logger_close(void)
|
2007-10-31 21:16:56 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
int sig = SIGTERM;
|
|
|
|
|
2007-10-31 21:16:56 +05:30
|
|
|
if (signal_pipe[1] > -1) {
|
2009-05-01 00:26:43 +05:30
|
|
|
if (write(signal_pipe[1], &sig, sizeof(sig)) == -1)
|
|
|
|
eerror("write: %s", strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
close(signal_pipe[1]);
|
2007-10-31 21:16:56 +05:30
|
|
|
signal_pipe[1] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc_logger_pid > 0)
|
2008-03-16 22:30:56 +05:30
|
|
|
waitpid(rc_logger_pid, 0, 0);
|
2007-10-31 21:16:56 +05:30
|
|
|
|
|
|
|
if (fd_stdout > -1)
|
2008-03-16 22:30:56 +05:30
|
|
|
dup2(fd_stdout, STDOUT_FILENO);
|
2007-10-31 21:16:56 +05:30
|
|
|
if (fd_stderr > -1)
|
2008-03-16 22:30:56 +05:30
|
|
|
dup2(fd_stderr, STDERR_FILENO);
|
2007-10-31 21:16:56 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
void
|
|
|
|
rc_logger_open(const char *level)
|
2007-10-31 21:16:56 +05:30
|
|
|
{
|
|
|
|
int slave_tty;
|
|
|
|
struct termios tt;
|
|
|
|
struct winsize ws;
|
2011-07-06 03:07:07 +05:30
|
|
|
char buffer[BUFSIZ];
|
2008-11-28 02:44:43 +05:30
|
|
|
struct pollfd fd[2];
|
2007-11-04 15:43:48 +05:30
|
|
|
int s = 0;
|
2007-10-31 21:16:56 +05:30
|
|
|
size_t bytes;
|
|
|
|
int i;
|
|
|
|
FILE *log = NULL;
|
2011-07-06 03:07:07 +05:30
|
|
|
FILE *plog = NULL;
|
|
|
|
const char *logfile;
|
|
|
|
int log_error = 0;
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2008-11-28 16:17:17 +05:30
|
|
|
if (!rc_conf_yesno("rc_logger"))
|
2007-10-31 21:16:56 +05:30
|
|
|
return;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (pipe(signal_pipe) == -1)
|
|
|
|
eerrorx("pipe: %s", strerror(errno));
|
2007-10-31 21:16:56 +05:30
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
if ((s = fcntl (signal_pipe[i], F_GETFD, 0) == -1 ||
|
2009-04-24 03:01:22 +05:30
|
|
|
fcntl (signal_pipe[i], F_SETFD, s | FD_CLOEXEC) == -1))
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("fcntl: %s", strerror (errno));
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2009-04-03 05:25:19 +05:30
|
|
|
if (isatty(STDOUT_FILENO)) {
|
|
|
|
tcgetattr(STDOUT_FILENO, &tt);
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
|
|
|
|
if (openpty(&rc_logger_tty, &slave_tty, NULL, &tt, &ws))
|
|
|
|
return;
|
2011-01-17 13:58:43 +05:30
|
|
|
} else
|
2009-04-03 05:25:19 +05:30
|
|
|
if (openpty(&rc_logger_tty, &slave_tty, NULL, NULL, NULL))
|
|
|
|
return;
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((s = fcntl(rc_logger_tty, F_GETFD, 0)) == 0)
|
|
|
|
fcntl(rc_logger_tty, F_SETFD, s | FD_CLOEXEC);
|
2008-02-29 03:17:45 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((s = fcntl(slave_tty, F_GETFD, 0)) == 0)
|
|
|
|
fcntl(slave_tty, F_SETFD, s | FD_CLOEXEC);
|
2008-02-29 03:17:45 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_logger_pid = fork();
|
2007-10-31 21:16:56 +05:30
|
|
|
switch (rc_logger_pid) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case -1:
|
|
|
|
eerror("fork: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
rc_in_logger = true;
|
|
|
|
close(signal_pipe[1]);
|
|
|
|
signal_pipe[1] = -1;
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
runlevel = level;
|
2011-07-06 03:07:07 +05:30
|
|
|
if ((log = fopen(TMPLOG, "ae")))
|
2008-03-16 22:30:56 +05:30
|
|
|
write_time(log, "started");
|
|
|
|
else {
|
|
|
|
free(logbuf);
|
|
|
|
logbuf_size = BUFSIZ * 10;
|
|
|
|
logbuf = xmalloc(sizeof (char) * logbuf_size);
|
|
|
|
logbuf_len = 0;
|
|
|
|
}
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2008-11-28 02:44:43 +05:30
|
|
|
fd[0].fd = signal_pipe[0];
|
|
|
|
fd[0].events = fd[1].events = POLLIN;
|
|
|
|
fd[0].revents = fd[1].revents = 0;
|
|
|
|
if (rc_logger_tty >= 0)
|
|
|
|
fd[1].fd = rc_logger_tty;
|
2008-03-16 22:30:56 +05:30
|
|
|
for (;;) {
|
2009-04-24 03:01:22 +05:30
|
|
|
if ((s = poll(fd,
|
|
|
|
rc_logger_tty >= 0 ? 2 : 1, -1)) == -1)
|
|
|
|
{
|
2008-11-28 02:44:43 +05:30
|
|
|
eerror("poll: %s", strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2009-04-24 03:01:22 +05:30
|
|
|
} else if (s == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fd[1].revents & (POLLIN | POLLHUP)) {
|
|
|
|
memset(buffer, 0, BUFSIZ);
|
|
|
|
bytes = read(rc_logger_tty, buffer, BUFSIZ);
|
2009-05-01 00:26:43 +05:30
|
|
|
if (write(STDOUT_FILENO, buffer, bytes) == -1)
|
|
|
|
eerror("write: %s", strerror(errno));
|
2009-04-24 03:01:22 +05:30
|
|
|
|
|
|
|
if (log)
|
|
|
|
write_log(fileno (log), buffer, bytes);
|
|
|
|
else {
|
|
|
|
if (logbuf_size - logbuf_len < bytes) {
|
|
|
|
logbuf_size += BUFSIZ * 10;
|
|
|
|
logbuf = xrealloc(logbuf,
|
|
|
|
sizeof(char ) *
|
|
|
|
logbuf_size);
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
memcpy(logbuf + logbuf_len,
|
|
|
|
buffer, bytes);
|
|
|
|
logbuf_len += bytes;
|
|
|
|
}
|
2007-10-31 21:16:56 +05:30
|
|
|
}
|
2009-04-24 03:01:22 +05:30
|
|
|
|
|
|
|
/* Only SIGTERMS signals come down this pipe */
|
|
|
|
if (fd[0].revents & (POLLIN | POLLHUP))
|
|
|
|
break;
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
|
|
|
if (logbuf) {
|
2011-07-06 03:07:07 +05:30
|
|
|
if ((log = fopen(TMPLOG, "ae"))) {
|
2008-03-16 22:30:56 +05:30
|
|
|
write_time(log, "started");
|
|
|
|
write_log(fileno(log), logbuf, logbuf_len);
|
2007-10-31 21:16:56 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
free(logbuf);
|
|
|
|
}
|
|
|
|
if (log) {
|
|
|
|
write_time(log, "stopped");
|
|
|
|
fclose(log);
|
|
|
|
}
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2011-07-06 03:07:07 +05:30
|
|
|
/* Append the temporary log to the real log */
|
2011-07-07 00:45:48 +05:30
|
|
|
logfile = rc_conf_value("rc_log_path");
|
2011-07-06 03:07:07 +05:30
|
|
|
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 {
|
2011-12-29 07:15:04 +05:30
|
|
|
/*
|
|
|
|
* logfile or its basedir may be read-only during shutdown so skip
|
|
|
|
* the error in this case
|
|
|
|
*/
|
2011-12-29 07:46:53 +05:30
|
|
|
if (strcmp(level, RC_LEVEL_SHUTDOWN) != 0) {
|
2011-12-29 07:15:04 +05:30
|
|
|
log_error = 1;
|
|
|
|
eerror("Error: fopen(%s) failed: %s", logfile, strerror(errno));
|
|
|
|
}
|
2011-07-06 03:07:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
exit(0);
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
default:
|
|
|
|
setpgid(rc_logger_pid, 0);
|
|
|
|
fd_stdout = dup(STDOUT_FILENO);
|
|
|
|
fd_stderr = dup(STDERR_FILENO);
|
|
|
|
if ((s = fcntl(fd_stdout, F_GETFD, 0)) == 0)
|
|
|
|
fcntl(fd_stdout, F_SETFD, s | FD_CLOEXEC);
|
|
|
|
|
|
|
|
if ((s = fcntl(fd_stderr, F_GETFD, 0)) == 0)
|
|
|
|
fcntl(fd_stderr, F_SETFD, s | FD_CLOEXEC);
|
|
|
|
dup2(slave_tty, STDOUT_FILENO);
|
|
|
|
dup2(slave_tty, STDERR_FILENO);
|
|
|
|
if (slave_tty != STDIN_FILENO &&
|
|
|
|
slave_tty != STDOUT_FILENO &&
|
|
|
|
slave_tty != STDERR_FILENO)
|
|
|
|
close(slave_tty);
|
|
|
|
close(signal_pipe[0]);
|
|
|
|
signal_pipe[0] = -1;
|
|
|
|
break;
|
2007-10-31 21:16:56 +05:30
|
|
|
}
|
|
|
|
}
|