summaryrefslogtreecommitdiff
path: root/src/rc/rc-logger.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-03-16 17:00:56 +0000
committerRoy Marples <roy@marples.name>2008-03-16 17:00:56 +0000
commitcb9da6a262b60255cd037f20b4cde3ab2c8a1e6a (patch)
treec5c57d5adedf009fdb02b53677e2cdf940bdb47c /src/rc/rc-logger.c
parent40e12f6ba026af9c24d5c3d8e36512719ed5faee (diff)
downloadopenrc-cb9da6a262b60255cd037f20b4cde3ab2c8a1e6a.tar.gz
openrc-cb9da6a262b60255cd037f20b4cde3ab2c8a1e6a.tar.bz2
openrc-cb9da6a262b60255cd037f20b4cde3ab2c8a1e6a.tar.xz
Remove null terminated char ** lists in favour of RC_STRINGLIST, using TAILQ from queue(3). Refactor code style around the BSD KNF.
Diffstat (limited to 'src/rc/rc-logger.c')
-rw-r--r--src/rc/rc-logger.c255
1 files changed, 129 insertions, 126 deletions
diff --git a/src/rc/rc-logger.c b/src/rc/rc-logger.c
index 3e02a1a..b4e4e35 100644
--- a/src/rc/rc-logger.c
+++ b/src/rc/rc-logger.c
@@ -33,6 +33,7 @@
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
+
#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
@@ -76,67 +77,67 @@ pid_t rc_logger_pid = -1;
int rc_logger_tty = -1;
bool rc_in_logger = false;
-static void write_log (int logfd, const char *buffer, size_t bytes)
+static void write_log(int logfd, const char *buffer, size_t bytes)
{
const char *p = buffer;
while ((size_t) (p - buffer) < bytes) {
switch (*p) {
- 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;
+ 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;
}
if (! in_escape) {
- write (logfd, p++, 1);
+ write(logfd, p++, 1);
continue;
}
- if (! in_term || isalpha ((int) *p))
+ if (! in_term || isalpha((int) *p))
in_escape = in_term = false;
cont:
p++;
}
}
-
-static void write_time (FILE *f, const char *s)
+static void write_time(FILE *f, const char *s)
{
- time_t now = time (NULL);
- struct tm *tm = localtime (&now);
+ time_t now = time(NULL);
+ struct tm *tm = localtime(&now);
- fprintf (f, "\nrc %s logging %s at %s\n", runlevel, s, asctime (tm));
- fflush (f);
+ fprintf(f, "\nrc %s logging %s at %s\n", runlevel, s, asctime(tm));
+ fflush(f);
}
-void rc_logger_close ()
+void rc_logger_close(void)
{
+ int sig = SIGTERM;
+
if (signal_pipe[1] > -1) {
- int sig = SIGTERM;
- write (signal_pipe[1], &sig, sizeof (sig));
- close (signal_pipe[1]);
+ write(signal_pipe[1], &sig, sizeof(sig));
+ close(signal_pipe[1]);
signal_pipe[1] = -1;
}
if (rc_logger_pid > 0)
- waitpid (rc_logger_pid, 0, 0);
+ waitpid(rc_logger_pid, 0, 0);
if (fd_stdout > -1)
- dup2 (fd_stdout, STDOUT_FILENO);
+ dup2(fd_stdout, STDOUT_FILENO);
if (fd_stderr > -1)
- dup2 (fd_stderr, STDERR_FILENO);
+ dup2(fd_stderr, STDERR_FILENO);
}
-void rc_logger_open (const char *level)
+void rc_logger_open(const char *level)
{
int slave_tty;
struct termios tt;
@@ -149,125 +150,127 @@ void rc_logger_open (const char *level)
int i;
FILE *log = NULL;
- if (! isatty (STDOUT_FILENO))
+ if (! isatty(STDOUT_FILENO))
return;
- if (! rc_conf_yesno ("rc_logger"))
+ if (! rc_conf_yesno("rc_logger"))
return;
- if (pipe (signal_pipe) == -1)
- eerrorx ("pipe: %s", strerror (errno));
+ if (pipe(signal_pipe) == -1)
+ eerrorx("pipe: %s", strerror(errno));
for (i = 0; i < 2; i++)
if ((s = fcntl (signal_pipe[i], F_GETFD, 0) == -1 ||
fcntl (signal_pipe[i], F_SETFD, s | FD_CLOEXEC) == -1))
- eerrorx ("fcntl: %s", strerror (errno));
+ eerrorx("fcntl: %s", strerror (errno));
- tcgetattr (STDOUT_FILENO, &tt);
- ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws);
+ tcgetattr(STDOUT_FILENO, &tt);
+ ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
/* /dev/pts may not be available yet */
- if (openpty (&rc_logger_tty, &slave_tty, NULL, &tt, &ws))
+ if (openpty(&rc_logger_tty, &slave_tty, NULL, &tt, &ws))
return;
- if ((s = fcntl (rc_logger_tty, F_GETFD, 0)) == 0)
- fcntl (rc_logger_tty, F_SETFD, s | FD_CLOEXEC);
+ if ((s = fcntl(rc_logger_tty, F_GETFD, 0)) == 0)
+ fcntl(rc_logger_tty, F_SETFD, s | FD_CLOEXEC);
- if ((s = fcntl (slave_tty, F_GETFD, 0)) == 0)
- fcntl (slave_tty, F_SETFD, s | FD_CLOEXEC);
+ if ((s = fcntl(slave_tty, F_GETFD, 0)) == 0)
+ fcntl(slave_tty, F_SETFD, s | FD_CLOEXEC);
- rc_logger_pid = fork ();
+ rc_logger_pid = fork();
switch (rc_logger_pid) {
- case -1:
- eerror ("forkpty: %s", strerror (errno));
- break;
- case 0:
- rc_in_logger = true;
- close (signal_pipe[1]);
- signal_pipe[1] = -1;
-
- runlevel = level;
- if ((log = fopen (LOGFILE, "a")))
- write_time (log, "started");
- else {
- free (logbuf);
- logbuf_size = BUFSIZ * 10;
- logbuf = xmalloc (sizeof (char) * logbuf_size);
- logbuf_len = 0;
- }
+ case -1:
+ eerror("fork: %s", strerror(errno));
+ break;
+ case 0:
+ rc_in_logger = true;
+ close(signal_pipe[1]);
+ signal_pipe[1] = -1;
- buffer = xmalloc (sizeof (char) * BUFSIZ);
- selfd = rc_logger_tty > signal_pipe[0] ? rc_logger_tty : signal_pipe[0];
- for (;;) {
- FD_ZERO (&rset);
- FD_SET (rc_logger_tty, &rset);
- FD_SET (signal_pipe[0], &rset);
+ runlevel = level;
+ if ((log = fopen(LOGFILE, "a")))
+ write_time(log, "started");
+ else {
+ free(logbuf);
+ logbuf_size = BUFSIZ * 10;
+ logbuf = xmalloc(sizeof (char) * logbuf_size);
+ logbuf_len = 0;
+ }
- if ((s = select (selfd + 1, &rset, NULL, NULL, NULL)) == -1) {
- eerror ("select: %s", strerror (errno));
- break;
- }
+ buffer = xmalloc(sizeof (char) * BUFSIZ);
+ selfd = rc_logger_tty > signal_pipe[0] ? rc_logger_tty : signal_pipe[0];
+ for (;;) {
+ FD_ZERO(&rset);
+ FD_SET(rc_logger_tty, &rset);
+ FD_SET(signal_pipe[0], &rset);
- if (s > 0) {
- if (FD_ISSET (rc_logger_tty, &rset)) {
- memset (buffer, 0, BUFSIZ);
- bytes = read (rc_logger_tty, buffer, BUFSIZ);
- write (STDOUT_FILENO, buffer, bytes);
-
- 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);
- }
-
- memcpy (logbuf + logbuf_len, buffer, bytes);
- logbuf_len += bytes;
+ if ((s = select(selfd + 1, &rset, NULL, NULL, NULL)) == -1) {
+ eerror("select: %s", strerror(errno));
+ break;
+ }
+
+ if (s > 0) {
+ if (FD_ISSET(rc_logger_tty, &rset)) {
+ memset(buffer, 0, BUFSIZ);
+ bytes = read(rc_logger_tty, buffer, BUFSIZ);
+ write(STDOUT_FILENO, buffer, bytes);
+
+ 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);
}
- }
- /* Only SIGTERMS signals come down this pipe */
- if (FD_ISSET (signal_pipe[0], &rset))
- break;
- }
- }
- free (buffer);
- if (logbuf) {
- if ((log = fopen (LOGFILE, "a"))) {
- write_time (log, "started");
- write_log (fileno (log), logbuf, logbuf_len);
+ memcpy(logbuf + logbuf_len, buffer, bytes);
+ logbuf_len += bytes;
+ }
}
- free (logbuf);
+
+ /* Only SIGTERMS signals come down this pipe */
+ if (FD_ISSET(signal_pipe[0], &rset))
+ break;
}
- if (log) {
- write_time (log, "stopped");
- fclose (log);
+ }
+ free(buffer);
+ if (logbuf) {
+ if ((log = fopen(LOGFILE, "a"))) {
+ write_time(log, "started");
+ write_log(fileno(log), logbuf, logbuf_len);
}
+ free(logbuf);
+ }
+ if (log) {
+ write_time(log, "stopped");
+ fclose(log);
+ }
- /* Try and cat our new logfile to a more permament location and then
- * punt it */
- system (MOVELOG);
-
- 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;
+ /* Try and cat our new logfile to a more permament location and then
+ * punt it */
+ system(MOVELOG);
+
+ 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;
}
}