xref: /csrg-svn/usr.bin/uucp/libuu/logent.c (revision 13706)
113661Ssam #ifndef lint
2*13706Ssam static char sccsid[] = "@(#)logent.c	5.2 (Berkeley) 07/02/83";
313661Ssam #endif
413661Ssam 
513661Ssam #include "uucp.h"
613661Ssam #include <sys/types.h>
7*13706Ssam #include <sys/time.h>
813661Ssam 
913661Ssam extern	time_t	time();
1013661Ssam 
1113661Ssam /* This logfile stuff was awful -- it did output to an
1213661Ssam  * unbuffered stream.
1313661Ssam  *
1413661Ssam  * This new version just open the single logfile and writes
1513661Ssam  * the record in the stdio buffer.  Once that's done, it
1613661Ssam  * positions itself at the end of the file (lseek), and
1713661Ssam  * writes the buffer out.  This could mangle things but
1813661Ssam  * it isn't likely. -- ittvax!swatt
1913661Ssam  *
2013661Ssam  * If the files could be opened with "guaranteed append to end",
2113661Ssam  * the lseeks could be removed.
2213661Ssam  * Using fseek would be slightly cleaner,
2313661Ssam  * but would mangle things slightly more often.
2413661Ssam  */
2513661Ssam 
2613661Ssam 
2713661Ssam FILE *Lp = NULL;
2813661Ssam FILE *Sp = NULL;
2913661Ssam static Ltried = 0;
3013661Ssam static Stried = 0;
3113661Ssam 
3213661Ssam /*******
3313661Ssam  *	logent(text, status)	make log entry
3413661Ssam  *	char *text, *status;
3513661Ssam  *
3613661Ssam  *	return code - none
3713661Ssam  */
3813661Ssam 
3913661Ssam logent(text, status)
4013661Ssam char *text, *status;
4113661Ssam {
4213661Ssam 	/* Open the log file if necessary */
4313661Ssam 	if (Lp == NULL) {
4413661Ssam 		if (!Ltried) {
4513661Ssam 			int savemask;
4613661Ssam 			savemask = umask(LOGMASK);
4713661Ssam 			Lp = fopen (LOGFILE, "a");
4813661Ssam 			umask(savemask);
4913661Ssam 		}
5013661Ssam 		Ltried = 1;
5113661Ssam 		if (Lp == NULL)
5213661Ssam 			return;
5313661Ssam 		fioclex(fileno(Lp));
5413661Ssam 	}
5513661Ssam 
5613661Ssam 	/*  make entry in existing temp log file  */
5713661Ssam 	mlogent(Lp, status, text);
5813661Ssam }
5913661Ssam 
6013661Ssam /***
6113661Ssam  *	mlogent(fp, status, text)  - make a log entry
6213661Ssam  */
6313661Ssam 
6413661Ssam mlogent(fp, status, text)
6513661Ssam char *text, *status;
6613661Ssam register FILE *fp;
6713661Ssam {
6813661Ssam 	static pid = 0;
6913661Ssam 	register struct tm *tp;
7013661Ssam 	extern struct tm *localtime();
7113661Ssam 	time_t clock;
7213661Ssam 
7313661Ssam 	if (!pid)
7413661Ssam 		pid = getpid();
7513661Ssam 	time(&clock);
7613661Ssam 	tp = localtime(&clock);
7713661Ssam 	fprintf(fp, "%s %s ", User, Rmtname);
7813661Ssam 	fprintf(fp, "(%d/%d-%d:%02d-%d) ", tp->tm_mon + 1,
7913661Ssam 		tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
8013661Ssam 	fprintf(fp, "%s (%s)\n", status, text);
8113661Ssam 
8213661Ssam 	/* Since it's buffered */
8313661Ssam 	lseek (fileno(fp), (long)0, 2);
8413661Ssam 	fflush (fp);
8513661Ssam 	if (Debug > 0) {
8613661Ssam 		fprintf(stderr, "%s %s ", User, Rmtname);
8713661Ssam 		fprintf(stderr, "(%d/%d-%d:%02d-%d) ", tp->tm_mon + 1,
8813661Ssam 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
8913661Ssam 		fprintf(stderr, "%s (%s)\n", status, text);
9013661Ssam 	}
9113661Ssam }
9213661Ssam 
9313661Ssam /***
9413661Ssam  *	logcls()	close log file
9513661Ssam  *
9613661Ssam  *	return codes:  none
9713661Ssam  */
9813661Ssam 
9913661Ssam logcls()
10013661Ssam {
10113661Ssam 	if (Lp != NULL)
10213661Ssam 		fclose(Lp);
10313661Ssam 	Lp = NULL;
10413661Ssam 	Ltried = 0;
10513661Ssam 
10613661Ssam 	if (Sp != NULL)
10713661Ssam 		fclose (Sp);
10813661Ssam 	Sp = NULL;
10913661Ssam 	Stried = 0;
11013661Ssam }
11113661Ssam 
11213661Ssam 
11313661Ssam /***
11413661Ssam  *	syslog(text)	make system log entry
11513661Ssam  *	char *text;
11613661Ssam  *
11713661Ssam  *	return codes - none
11813661Ssam  */
11913661Ssam 
12013661Ssam syslog(text)
12113661Ssam char *text;
12213661Ssam {
12313661Ssam 	register struct tm *tp;
12413661Ssam 	extern struct tm *localtime();
12513661Ssam 	time_t clock;
12613661Ssam 
12713661Ssam 	if (Sp == NULL) {
12813661Ssam 		if (!Stried) {
12913661Ssam 			int savemask;
13013661Ssam 			savemask = umask(LOGMASK);
13113661Ssam 			Sp = fopen(SYSLOG, "a");
13213661Ssam 			umask(savemask);
13313661Ssam 		}
13413661Ssam 		Stried = 1;
13513661Ssam 		if (Sp == NULL)
13613661Ssam 			return;
13713661Ssam 		fioclex(fileno(Sp));
13813661Ssam 	}
13913661Ssam 
14013661Ssam 	time(&clock);
14113661Ssam 	tp = localtime(&clock);
14213661Ssam 
14313661Ssam 	fprintf(Sp, "%s %s ", User, Rmtname);
14413661Ssam 	fprintf(Sp, "(%d/%d-%d:%02d) ", tp->tm_mon + 1,
14513661Ssam 		tp->tm_mday, tp->tm_hour, tp->tm_min);
14613661Ssam 	fprintf(Sp, "(%ld) %s\n", clock, text);
14713661Ssam 
14813661Ssam 	/* Position at end and flush */
14913661Ssam 	lseek (fileno(Sp), (long)0, 2);
15013661Ssam 	fflush (Sp);
15113661Ssam }
15213661Ssam 
15313661Ssam /*
15413661Ssam  * Arrange to close fd on exec(II).
15513661Ssam  * Otherwise unwanted file descriptors are inherited
15613661Ssam  * by other programs.  And that may be a security hole.
15713661Ssam  */
15813661Ssam #ifdef SYSIII
15913661Ssam #include <fcntl.h>
16013661Ssam #endif
16113661Ssam #ifndef	SYSIII
16213661Ssam #include <sgtty.h>
16313661Ssam #endif
16413661Ssam 
16513661Ssam fioclex(fd)
16613661Ssam int fd;
16713661Ssam {
16813661Ssam 	register int ret;
16913661Ssam 
17013661Ssam #ifdef	SYSIII
17113661Ssam 	ret = fcntl(fd, F_SETFD, 1);	/* Steve Bellovin says this does it */
17213661Ssam #endif
17313661Ssam #ifndef	SYSIII
17413661Ssam 	ret = ioctl(fd, FIOCLEX, STBNULL);
17513661Ssam #endif
17613661Ssam 	if (ret)
17713661Ssam 		DEBUG(2, "CAN'T FIOCLEX %d\n", fd);
17813661Ssam }
179