#define SIGHUP	1
#define SIGINT	2
#define SIGQIT	3
#define SIGCLK	14
#define SIGTRM	15

#define SU	0
#define MSGSZ	106
#define NOACK	64

struct minfo {
	int	mpid;
	int	mtype;
} minfo;

	int	mtext[MSGSZ];
	int	getsig();
	int	stop;
	int	efno;
	char	*errfile	"/usr/adm/errfile";

main(argc,argv)
	char **argv;
{
	register int n;
	int fil[2];

	signal(SIGHUP,1);
	signal(SIGINT,1);
	signal(SIGQIT,1);
	if(argc > 1)
		errfile = *++argv;
	if(pipe(fil) < 0)
		error("Can't pipe");
	if((n = fork()) > 0) {
		close(fil[1]);
		while((n = read(fil[0],mtext,sizeof(mtext))) > 0)
			write(1,mtext,n);
		exit(0);
	}
	else if(n < 0) {
		error("Can't fork");
	}

	/* Error Log Daemon */

	close(fil[0]);
	close(0);
	open("/",0);		/* guarantee fd 0 in use */
	close(1);
	dup(fil[1]);
	close(fil[1]);
	setuid(SU);
	if(getuid()&0377)
		error("Not super-user");
	signal(SIGTRM,&getsig);
	if((efno = open(errfile,1)) < 0)
		if((efno = creat(errfile,0666)) < 0)
			error("Can't open/create %s",errfile);
	seek(efno,0,2);
	if(msgenab() < 0)
		error("Can't enable messages");
	if(errlog(1) < 0)
		error("Can't start logging");
	for(n = 0; n < 20; n++)
		if(n != efno)
			close(n);
	while(!stop) {
		if((n = recvw(mtext,sizeof(mtext),&minfo,0)) > 0)
			wrtmsg(n);
	}
	errlog(0);
	while((n = recv(mtext,sizeof(mtext),&minfo,0)) > 0)
		wrtmsg(n);
	exit(0);
}
wrtmsg(n)
{
	if(minfo.mpid == 0 && minfo.mtype == NOACK) {
		write(efno,mtext,n);
	}
}
getsig(n)
{
	signal(n, &getsig);
	if(n == SIGTRM)
		stop++;
}
error(s1,s2)
{
	printf(s1,s2);
	putchar('\n');
	exit(16);
}
