/*
 *   Copyright (c) 1996-1999 David Parsons. All rights reserved.
 *   
 *   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.
 *  3. All advertising materials mentioning features or use of this
 *     software must display the following acknowledgement:
 *     
 *   This product includes software developed by David Parsons
 *   (orc@pell.chi.il.us)
 *
 *  4. My name may not be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *     
 *  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``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 DAVID
 *  PARSONS 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.
 */

static const char rcsid[] = "$Id: s.logger.c 1.11 00/02/01 22:42:44-00:00 orc $";

/*
 * log kernel messages to /dev/tty7
 */
#include <stdio.h>
#include <unistd.h>
#include <asm/unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>

#include "util.h"

#define __NR_sys_syslog __NR_syslog
_syscall3(int, sys_syslog, int, type, char *, buf, int, len);

#define LOG_CLOSE	0
#define LOG_OPEN	1
#define LOG_READ	2
#define LOG_READALL	3
#define LOG_READCLEAR	4
#define LOG_CLEAR	5
#define LOG_DISABLE	6
#define LOG_ENABLE	7
#define LOG_LOGLEVEL	8

/*
 * stfu() shuts up logging to the console
 */
void
stfu()
{
    if (getpid() == 1) {
	sys_syslog(LOG_LOGLEVEL, NULL, 0);	/* print nothing */
	sys_syslog(LOG_DISABLE, NULL, 0);	/* disable console messages */
    }
}


/*
 * syslogger() fires up a klogd process
 */
pid_t
syslogger(char *tty)
{
    static char bfr[16*1024];
    char line[2048];
    int bufsize;
    int llen;
    int ix;
    enum { NL, PRIO, TEXT } loglinestate;

    pid_t child;

    fprintf(stderr, "starting syslogger\n");
    child = fork();

    if (child == 0) {
	close(0);
	close(1);
	close(2);
	setsid();
	ix = open(tty, O_RDWR);
	dup2(ix, 1);
	dup2(ix, 2);

	strcpy(av0, "mini-logd                  ");
	sys_syslog(LOG_OPEN, NULL, 0);		/* initialize kernel logging */

	loglinestate = NL;

	while (1) {
	    memset(bfr, 0, sizeof bfr);
	    bufsize = sys_syslog(LOG_READ, bfr, sizeof bfr);
	    for (ix=0; ix < bufsize; ix++) {
		switch (loglinestate) {
		case NL:    if (bfr[ix] == '<')
				loglinestate = PRIO;
			    else if (bfr[ix] != '\n') {
				loglinestate = TEXT;
				memset(line, 0, sizeof line);
				llen = 0;
				line[llen++] = bfr[ix];
			    }
			    break;
		case PRIO:  if (bfr[ix] == '>')
				loglinestate = TEXT;
			    else if (bfr[ix] == '\n')
				/* Huh? */
				loglinestate = NL;
			    break;
		case TEXT:  if (bfr[ix] == '\n' || bfr[ix] == '\r') {
				line[llen++] = '\r';
				line[llen++] = '\n';
				write(2, line, llen);
				loglinestate = NL;
			    }
			    else
				line[llen++] = bfr[ix];
			    break;
		}
		if (llen >= sizeof line-6) {
		    line[llen++] = '\r';
		    line[llen++] = '\n';
		    line[llen++] = '.';
		    line[llen++] = '.';
		    line[llen++] = '.';
		    write(2, line, llen);
		    llen = 0;
		    memset(line, 0, sizeof line);
		}
	    }
	}
    }
    else if (child == -1)
	fprintf(stderr, "couldn't start syslogger: %s\n", strerror(errno));
    return (child == -1) ? 0 : child;
} /* syslogger */
