[news.software.nntp] using fakesyslog

nagel@PARIS.ICS.UCI.EDU (Mark Nagel) (03/20/89)

I have to use fakesyslog on one of our news servers since the syslog
routines do not work (Sequent Symmetry Dynix 3.0.12).  Anyhow, they
work OK, except there is no file locking done on the fakesyslog file.
I've tried to do this, once just by using flock on the open fakesyslog
file descriptor; other times I've tried more complex methods.  In any event,
I can't seem to get the *%&^ thing to lock.  This causes log messages
to get interspersed with one another.  I can just turn off logging,
but it is nice to get those reports once a week.  If anyone has fixed
this (or if anyone knows how to get the syslog routines to function
properly on the Symmetry), I'd appreciate it.  Thanks in advance!

Mark Nagel @ UC Irvine, Department of Information and Computer Science
                            +----------------------------------------+
ARPA: nagel@ics.uci.edu     | Six plus six equals fourteen for large |
UUCP: ucbvax!ucivax!nagel   | values of six -- Dave Ackerman         |

loverso@Xylogics.COM (John Robert LoVerso) (03/23/89)

In article <29391.606340366@paris.ics.uci.edu> nagel@ics.UCI.EDU writes:
> I have to use fakesyslog on one of our news servers since the syslog
> routines do not work (Sequent Symmetry Dynix 3.0.12).
...
> This causes log messages to get interspersed with one another.

Here are some diffs and a new fakesyslog.c that fix this problem by open()ing
the logfile using the O_APPEND flag, and then using fdopen() to get a stdio
stream.  I also changed the log output of fakesyslog() to be a lot more like
that of 4.3BSD.  Finally, I liked to HUP syslogd after doing my daily
rotation of "log"->"log.0", and so I included a quick hack (fixlog) that
accomlishes that, even if you're using FAKESYSLOG (it HUPs all your running
nntpd's).

I originally did these changes to use NNTP on an Encore Multimax running
Umax4.2.  I sent them in to Phil and Stan at that time.  When I came here
to Xylogics, I just installed a 4.3BSD syslogd on our Multimax and did
away with this nonsense.

John
-- 
John Robert LoVerso			Xylogics, Inc.  617/272-8140
loverso%Xylogics.COM@Encore.COM		Annex Terminal Server Development Group
encore!xylogics!loverso			[formerly of Encore Computer Corp]

--
# This is a shell archive.  Remove anything before this line, then
# unpack it by saving it in a file and typing "sh file".  (Files
# unpacked will be owned by you and have default permissions.)
#
# This archive contains:
# conf.h_diffs fakesyslog.h_diffs main.c_diffs serve.c_diffs fakesyslog.c fixlog.c

echo x - conf.h_diffs
sed -e 's/^X//' > "conf.h_diffs" << '//E*O*F conf.h_diffs//'
X*** ../common/conf.h_prefake	Thu Dec 15 11:31:12 1988
X--- ../common/conf.h	Thu Dec 15 11:33:25 1988
X***************
X*** 36,42 ****
X  
X  /*
X   * If you have the syslog library routine, define SYSLOG to
X!  * be thef syslog facility name under which stats should be
X   * logged.  Newer 4.3 systems might choose LOG_NEWS;
X   * LOG_LOCAL7 is an acceptable substitute.
X   *
X--- 36,42 ----
X  
X  /*
X   * If you have the syslog library routine, define SYSLOG to
X!  * be the syslog facility name under which stats should be
X   * logged.  Newer 4.3 systems might choose LOG_NEWS;
X   * LOG_LOCAL7 is an acceptable substitute.
X   *
X***************
X*** 46,56 ****
X   *
X   *	#define	FAKESYSLOG	"/usr/lib/news/nntplog"
X   *
X   * If you don't want any syslog-type activity, #undef SYSLOG.
X   * Obviously, this means that you can't define LOG, either.
X   */
X  
X! #undef	FAKESYSLOG
X  
X  #define	SYSLOG	LOG_NEWS
X  
X--- 46,61 ----
X   *
X   *	#define	FAKESYSLOG	"/usr/lib/news/nntplog"
X   *
X+  * If your host supports the BSD fdopen() function and the O_APPEND flag
X+  * to open(), you should define FAKEAPPEND with FAKESYSLOG so that
X+  * multiple copies of nntpd don't trash the log with buffered fprintf's.
X+  *
X   * If you don't want any syslog-type activity, #undef SYSLOG.
X   * Obviously, this means that you can't define LOG, either.
X   */
X  
X! #define	FAKESYSLOG	"/usr/lib/news/nntp/log"
X! #define	FAKEAPPEND
X  
X  #define	SYSLOG	LOG_NEWS
//E*O*F conf.h_diffs//

echo x - fakesyslog.h_diffs
sed -e 's/^X//' > "fakesyslog.h_diffs" << '//E*O*F fakesyslog.h_diffs//'
X*** fakesyslog.h_orig	Fri Dec 18 18:15:25 1987
X--- fakesyslog.h	Wed Dec 14 14:16:13 1988
X***************
X*** 56,62 ****
X   *  Option flags for openlog.
X   */
X  
X! #define	LOG_PID		0
X  #define	LOG_CONS	0
X  #define	LOG_ODELAY	0
X  #define LOG_NDELAY	0
X--- 56,62 ----
X   *  Option flags for openlog.
X   */
X  
X! #define	LOG_PID		1
X  #define	LOG_CONS	0
X  #define	LOG_ODELAY	0
X  #define LOG_NDELAY	0
//E*O*F fakesyslog.h_diffs//

echo x - main.c_diffs
sed -e 's/^X//' > "main.c_diffs" << '//E*O*F main.c_diffs//'
X*** main.c_orig	Mon Sep 19 22:33:30 1988
X--- main.c	Thu Dec 15 11:22:41 1988
X***************
X*** 37,49 ****
X  	for(sockt = 3; sockt < 40; sockt++)
X  		(void) close(sockt);
X  
X- #ifndef FAKESYSLOG
X  #ifdef SYSLOG
X  #ifdef BSD_42
X  	openlog("nntpd", LOG_PID);			/* fd 3 */
X  #else
X  	openlog("nntpd", LOG_PID, SYSLOG);		/* fd 3 */
X- #endif
X  #endif
X  #endif
X  
X--- 37,47 ----
//E*O*F main.c_diffs//

echo x - serve.c_diffs
sed -e 's/^X//' > "serve.c_diffs" << '//E*O*F serve.c_diffs//'
X*** serve.c_orig	Mon Sep 19 22:33:37 1988
X--- serve.c	Thu Dec 15 11:22:26 1988
X***************
X*** 105,118 ****
X  
X  	/* If we're ALONE, then we've already opened syslog */
X  
X! #ifndef FAKESYSLOG
X! # ifndef ALONE
X! #  ifdef SYSLOG
X! #   ifdef BSD_42
X  	openlog("nntpd", LOG_PID);
X! #   else
X  	openlog("nntpd", LOG_PID, SYSLOG);
X- #   endif
X  #  endif
X  # endif
X  #endif
X--- 105,116 ----
X  
X  	/* If we're ALONE, then we've already opened syslog */
X  
X! #ifndef ALONE
X! # ifdef SYSLOG
X! #  ifdef BSD_42
X  	openlog("nntpd", LOG_PID);
X! #  else
X  	openlog("nntpd", LOG_PID, SYSLOG);
X  #  endif
X  # endif
X  #endif
//E*O*F serve.c_diffs//

echo x - fakesyslog.c
sed -e 's/^X//' > "fakesyslog.c" << '//E*O*F fakesyslog.c//'
X#ifndef lint
Xstatic char	*sccsid = "@(#)fakesyslog.c	1.3	(Berkeley) 2/6/88 jlv";
X#endif
X
X/*
X * Fake syslog routines for systems that don't have syslog.
X * Taken from an idea by Paul McKenny, <mckenny@sri-unix.arpa>.
X * (Unfortunately, Paul, I can't distribute the real syslog code
X * as you suggested ... sigh.)
X *
X * Warning: this file contains joe code that may offend you.
X */
X
X#include "../common/conf.h"
X
X#ifdef FAKESYSLOG
X
X#include "fakesyslog.h"
X
X#include <stdio.h>
X#include <sys/signal.h>
X#include <sys/types.h>
X
X#ifdef FAKEAPPEND
X#include <sys/file.h>
X#endif
X
Xextern	int	errno;
Xextern	int	sys_nerr;
Xextern	char	*sys_errlist[];
X
Xstatic FILE	*logfp;
Xstatic int	failed = 0;
Xstatic char	*ident = "syslog";
Xstatic int 	opt = 0;
X#ifndef BSD_42
Xstatic int	fac = 0;
X#endif
X
Xextern char	*strcpy(), *strcat(), *ctime(), *sprintf();
Xextern time_t	time();
X
Xresetlog()
X{
X	closelog();
X	failed = 0;
X	if (logfp == NULL) {
X#ifdef BSD_42
X		openlog(ident, opt);
X#else
X		openlog(ident, opt, fac);
X#endif
X		if (logfp == NULL) {
X			failed = 1;
X			return;
X		}
X	}
X}
X
X#ifdef BSD_42
Xopenlog(newident,logopt)
X	char *newident;
X	int logopt;
X#else
Xopenlog(newident,logopt,facility)
X	char *newident;
X	int logopt, facility;
X#endif
X{
X#ifdef FAKEAPPEND
X/*
X * why can't stdio give us the capability of O_APPEND?
X */
X	int fd;
X
X	fd = open(FAKESYSLOG, O_WRONLY|O_APPEND, 0664);
X	if (fd < 0)
X		logfp = NULL;
X	else
X		logfp = fdopen(fd, "a");
X#else
X	logfp = fopen(FAKESYSLOG, "a");
X#endif
X
X	(void)signal(SIGHUP, resetlog);
X
X	if (newident && *newident)
X		ident = newident;
X	opt = logopt;
X#ifndef BSD_42
X	fac = facility;
X#endif
X}
X
Xcloselog()
X{
X	if (logfp) {
X		(void)fclose(logfp);
X		failed = 0;
X		logfp = NULL;
X	}
X}
X
X/*ARGSUSED*/
Xsetlogmask(maskpri)
X	int maskpri;
X{
X}
X
Xsyslog(pri, msg, x1, x2, x3, x4, x5, x6)
X	int	pri;
X	char	*msg, *x1, *x2, *x3, *x4, *x5, *x6;
X{
X	char		buf[1024];
X	char		*cp, *bp;
X	time_t		clock;
X
X	if (failed)
X		return;
X
X	if (logfp == NULL) {
X#ifdef BSD_42
X		openlog(ident, opt);
X#else
X		openlog(ident, opt, fac);
X#endif
X		if (logfp == NULL) {
X			failed = 1;
X			return;
X		}
X	}
X
X	(void) time(&clock);
X	(void) strcpy(buf, ctime(&clock)+4);
X	*(bp = buf + 16) = '\0';
X
X	(void) sprintf(bp, "localhost %s", ident ? ident : "");
X	bp += strlen(bp);
X
X	if (opt&LOG_PID) {
X		/* don't cache getpid() - who knows when we'll fork() */
X		(void) sprintf(bp, "[%d]", getpid());
X		bp += strlen(bp);
X	}
X
X	if (ident) {
X		(void) strcat(bp, ": ");
X		bp += 2;
X	} else {
X		(void) strcat(bp, " ");
X		bp ++;
X	}
X
X	for (cp = msg; *cp; cp++) {
X		if (*cp == '%' && cp[1] == 'm') {
X			*bp = '\0';
X			if (errno >= sys_nerr || errno < 0) {
X				char	work[32];
X				(void)sprintf(work, "unknown error #%d", errno);
X				(void)strcat(bp, work);
X			} else
X				(void)strcat(bp, sys_errlist[errno]);
X			bp = buf + strlen(buf);
X			cp++;
X		} else {
X			*bp++ = *cp;
X		}
X	}
X	*bp = '\0';
X	/* Ah, the semantic security of C ... */
X	if (bp[-1] != '\n')
X		(void) strcat(bp, "\n");
X
X	fprintf(logfp, buf, x1, x2, x3, x4, x5, x6);
X	(void) fflush(logfp);
X}
X
X#endif
//E*O*F fakesyslog.c//

echo x - fixlog.c
sed -e 's/^X//' > "fixlog.c" << '//E*O*F fixlog.c//'
X/*
X * Notify syslogd/nntpd that log file has been moved (after daily cleanup)
X *
X * install this setuid root as /usr/lib/news/nntp/fixlog
X *
X * for 4.3 systems (nntpd using 4.3 syslog), run "fixlog syslog"
X * for 4.2 systems (nntpd using FAKESYSLOG), run "fixlog fakelog"
X */
X
X#define SYSLOGPID "etc/syslog.pid"
X#define NNTPDPROCS "/bin/ps auxww | /bin/grep nntpd"
X
X#include <stdio.h>
X
Xextern int errno;
X
Xenum mode_e { SYSLOG, FAKELOG};
X
XFILE *popen();
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X	FILE *f;
X	char buf[BUFSIZ];
X	int i, pid;
X	enum mode_e mode=SYSLOG;
X
X	if (argc == 2 && !strcmp(argv[1],"syslog")) {
X		mode=SYSLOG;
X		f = fopen(SYSLOGPID, "r");
X	} else if (argc == 2 && !strcmp(argv[1],"fakelog")) {
X		mode=FAKELOG;
X		f = popen(NNTPDPROCS, "r");
X	} else {
X		fprintf(stderr, "Usage: logfix {syslog|fakelog}\n");
X		exit(1);
X	}
X
X
X	if (f == NULL) {
X		perror("[fp]open");
X		exit(2);
X	}
X
X	printf("kill -HUP");
X	while (!feof(f) && fgets(buf,BUFSIZ,f)) {
X		i = sscanf(buf, mode == FAKELOG ? "%*s %d" : "%d", &pid);
X		if (i > 0) {
X			printf(" %d", pid);
X			if (kill(pid, 1)) {
X				printf("\n");
X				perror("kill");
X				exit(1);
X			}
X		}
X	}
X
X	printf("\n");
X
X	if (mode == FAKELOG)
X		(void)pclose(f);
X	else
X		(void)fclose(f);
X	exit(0);
X}
//E*O*F fixlog.c//

exit 0