xref: /csrg-svn/usr.bin/uucp/libuu/logent.c (revision 18620)
113661Ssam #ifndef lint
2*18620Sralph static char sccsid[] = "@(#)logent.c	5.4 (Berkeley) 04/10/85";
313661Ssam #endif
413661Ssam 
513661Ssam #include "uucp.h"
617835Sralph #ifdef BSD4_2
713706Ssam #include <sys/time.h>
817835Sralph #else
917835Sralph #include <time.h>
1017835Sralph #endif
1117835Sralph #if defined(USG) || defined(BSD4_2)
1217835Sralph #include <fcntl.h>
1317835Sralph #endif
1413661Ssam 
1513661Ssam extern	time_t	time();
1613661Ssam 
1717835Sralph static FILE *Lp = NULL;
1817835Sralph static FILE *Sp = NULL;
1913661Ssam static Ltried = 0;
2013661Ssam static Stried = 0;
2113661Ssam 
22*18620Sralph /*
23*18620Sralph  *	make log entry
2413661Ssam  */
2513661Ssam logent(text, status)
2613661Ssam char *text, *status;
2713661Ssam {
28*18620Sralph #ifdef LOGBYSITE
29*18620Sralph 	char lfile[MAXFULLNAME];
30*18620Sralph 	static char LogRmtname[64];
31*18620Sralph #endif LOGBYSITE
32*18620Sralph 	if (Rmtname[0] == '\0')
33*18620Sralph 		strcpy(Rmtname, Myname);
3413661Ssam 	/* Open the log file if necessary */
35*18620Sralph #ifdef LOGBYSITE
36*18620Sralph 	if (strcmp(Rmtname, LogRmtname)) {
37*18620Sralph 		if (Lp != NULL)
38*18620Sralph 			fclose(Lp);
39*18620Sralph 		Lp = NULL;
40*18620Sralph 		Ltried = 0;
41*18620Sralph 	}
42*18620Sralph #endif LOGBYSITE
4313661Ssam 	if (Lp == NULL) {
4413661Ssam 		if (!Ltried) {
4513661Ssam 			int savemask;
46*18620Sralph #ifdef F_SETFL
4717835Sralph 			int flags;
4817835Sralph #endif
4913661Ssam 			savemask = umask(LOGMASK);
50*18620Sralph #ifdef LOGBYSITE
51*18620Sralph 			(void) sprintf(lfile, "%s/%s/%s", LOGBYSITE, Progname, Rmtname);
52*18620Sralph 			strcpy(LogRmtname, Rmtname);
53*18620Sralph 			Lp = fopen (lfile, "a");
54*18620Sralph #else !LOGBYSITE
5513661Ssam 			Lp = fopen (LOGFILE, "a");
56*18620Sralph #endif LOGBYSITE
5713661Ssam 			umask(savemask);
58*18620Sralph #ifdef F_SETFL
5917835Sralph 			flags = fcntl(fileno(Lp), F_GETFL, 0);
6017835Sralph 			fcntl(fileno(Lp), F_SETFL, flags|O_APPEND);
6117835Sralph #endif
6213661Ssam 		}
6313661Ssam 		Ltried = 1;
6413661Ssam 		if (Lp == NULL)
6513661Ssam 			return;
6613661Ssam 		fioclex(fileno(Lp));
6713661Ssam 	}
6813661Ssam 
6913661Ssam 	/*  make entry in existing temp log file  */
7013661Ssam 	mlogent(Lp, status, text);
7113661Ssam }
7213661Ssam 
73*18620Sralph /*
74*18620Sralph  *	make a log entry
7513661Ssam  */
7613661Ssam 
7713661Ssam mlogent(fp, status, text)
7813661Ssam char *text, *status;
7913661Ssam register FILE *fp;
8013661Ssam {
8113661Ssam 	static pid = 0;
8213661Ssam 	register struct tm *tp;
8313661Ssam 	extern struct tm *localtime();
8413661Ssam 	time_t clock;
8513661Ssam 
8617835Sralph 	if (text == NULL)
8717835Sralph 		text = "";
8817835Sralph 	if (status == NULL)
8917835Sralph 		status = "";
9013661Ssam 	if (!pid)
9113661Ssam 		pid = getpid();
9213661Ssam 	time(&clock);
9313661Ssam 	tp = localtime(&clock);
9413661Ssam 	fprintf(fp, "%s %s ", User, Rmtname);
9517835Sralph #ifdef USG
9617835Sralph 	fprintf(fp, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1,
9713661Ssam 		tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
98*18620Sralph #else !USG
9917835Sralph 	fprintf(fp, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1,
10017835Sralph 		tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
101*18620Sralph #endif !USG
10213661Ssam 	fprintf(fp, "%s (%s)\n", status, text);
10313661Ssam 
10413661Ssam 	/* Since it's buffered */
105*18620Sralph #ifndef F_SETFL
10613661Ssam 	lseek (fileno(fp), (long)0, 2);
107*18620Sralph #endif !F_SETFL
10813661Ssam 	fflush (fp);
10917835Sralph 	if (Debug) {
11013661Ssam 		fprintf(stderr, "%s %s ", User, Rmtname);
11117835Sralph #ifdef USG
11217835Sralph 		fprintf(stderr, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1,
11313661Ssam 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
114*18620Sralph #else !USG
11517835Sralph 		fprintf(stderr, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1,
11617835Sralph 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
117*18620Sralph #endif !USG
11813661Ssam 		fprintf(stderr, "%s (%s)\n", status, text);
11913661Ssam 	}
12013661Ssam }
12113661Ssam 
122*18620Sralph /*
123*18620Sralph  *	close log file
12413661Ssam  */
12513661Ssam logcls()
12613661Ssam {
12713661Ssam 	if (Lp != NULL)
12813661Ssam 		fclose(Lp);
12913661Ssam 	Lp = NULL;
13013661Ssam 	Ltried = 0;
13113661Ssam 
13213661Ssam 	if (Sp != NULL)
13313661Ssam 		fclose (Sp);
13413661Ssam 	Sp = NULL;
13513661Ssam 	Stried = 0;
13613661Ssam }
13713661Ssam 
13813661Ssam 
139*18620Sralph /*
140*18620Sralph  *	make system log entry
14113661Ssam  */
14213661Ssam syslog(text)
14313661Ssam char *text;
14413661Ssam {
14513661Ssam 	register struct tm *tp;
14613661Ssam 	extern struct tm *localtime();
147*18620Sralph 	struct timeb clock;
148*18620Sralph #ifdef LOGBYSITE
149*18620Sralph 	char lfile[MAXFULLNAME];
150*18620Sralph 	static char SLogRmtname[64];
15113661Ssam 
152*18620Sralph 	if (strcmp(Rmtname, SLogRmtname)) {
153*18620Sralph 		if (Sp != NULL)
154*18620Sralph 			fclose(Sp);
155*18620Sralph 		Sp = NULL;
156*18620Sralph 		Stried = 0;
157*18620Sralph 	}
158*18620Sralph #endif LOGBYSITE
15913661Ssam 	if (Sp == NULL) {
16013661Ssam 		if (!Stried) {
16113661Ssam 			int savemask;
162*18620Sralph #ifdef F_SETFL
16317835Sralph 			int flags;
164*18620Sralph #endif F_SETFL
16513661Ssam 			savemask = umask(LOGMASK);
166*18620Sralph #ifdef LOGBYSITE
167*18620Sralph 			(void) sprintf(lfile, "%s/xferstats/%s", LOGBYSITE, Rmtname);
168*18620Sralph 			strcpy(SLogRmtname, Rmtname);
169*18620Sralph 			Sp = fopen (lfile, "a");
170*18620Sralph #else !LOGBYSITE
171*18620Sralph 			Sp = fopen (SYSLOG, "a");
172*18620Sralph #endif LOGBYSITE
17313661Ssam 			umask(savemask);
174*18620Sralph #ifdef F_SETFL
17517835Sralph 			flags = fcntl(fileno(Sp), F_GETFL, 0);
17617835Sralph 			fcntl(fileno(Sp), F_SETFL, flags|O_APPEND);
177*18620Sralph #endif F_SETFL
178*18620Sralph 
17913661Ssam 		}
18013661Ssam 		Stried = 1;
18113661Ssam 		if (Sp == NULL)
18213661Ssam 			return;
18313661Ssam 		fioclex(fileno(Sp));
18413661Ssam 	}
18517835Sralph 
186*18620Sralph #ifdef USG
187*18620Sralph 	time(&clock.time);
188*18620Sralph 	clock.millitm = 0;
189*18620Sralph #else !USG
190*18620Sralph 	ftime(&clock);
191*18620Sralph #endif !USG
192*18620Sralph 	tp = localtime(&clock.time);
19313661Ssam 
19413661Ssam 	fprintf(Sp, "%s %s ", User, Rmtname);
19517835Sralph #ifdef USG
19617835Sralph 	fprintf(Sp, "(%d/%d-%2.2d:%2.2d) ", tp->tm_mon + 1,
19713661Ssam 		tp->tm_mday, tp->tm_hour, tp->tm_min);
198*18620Sralph 	fprintf(Sp, "(%ld) %s\n", clock.time, text);
199*18620Sralph #else !USG
20017835Sralph 	fprintf(Sp, "(%d/%d-%02d:%02d) ", tp->tm_mon + 1,
20117835Sralph 		tp->tm_mday, tp->tm_hour, tp->tm_min);
202*18620Sralph 	fprintf(Sp, "(%ld.%02d) %s\n", clock.time, clock.millitm/10, text);
203*18620Sralph #endif !USG
20413661Ssam 
20513661Ssam 	/* Position at end and flush */
206*18620Sralph #ifndef F_SETFL
20713661Ssam 	lseek (fileno(Sp), (long)0, 2);
208*18620Sralph #endif F_SETFL
20913661Ssam 	fflush (Sp);
21013661Ssam }
21113661Ssam 
21213661Ssam /*
21313661Ssam  * Arrange to close fd on exec(II).
21413661Ssam  * Otherwise unwanted file descriptors are inherited
21513661Ssam  * by other programs.  And that may be a security hole.
21613661Ssam  */
21717835Sralph #ifndef	USG
21813661Ssam #include <sgtty.h>
21913661Ssam #endif
22013661Ssam 
22113661Ssam fioclex(fd)
22213661Ssam int fd;
22313661Ssam {
22413661Ssam 	register int ret;
22513661Ssam 
22617835Sralph #if defined(USG) || defined(BSD4_2)
22713661Ssam 	ret = fcntl(fd, F_SETFD, 1);	/* Steve Bellovin says this does it */
22817835Sralph #else
22913661Ssam 	ret = ioctl(fd, FIOCLEX, STBNULL);
23013661Ssam #endif
23113661Ssam 	if (ret)
23213661Ssam 		DEBUG(2, "CAN'T FIOCLEX %d\n", fd);
23313661Ssam }
234