xref: /csrg-svn/usr.bin/uucp/libuu/logent.c (revision 23611)
113661Ssam #ifndef lint
2*23611Sbloom static char sccsid[] = "@(#)logent.c	5.5 (Berkeley) 06/20/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 
1517835Sralph static FILE *Lp = NULL;
1617835Sralph static FILE *Sp = NULL;
1713661Ssam static Ltried = 0;
1813661Ssam static Stried = 0;
1913661Ssam 
20*23611Sbloom /*LINTLIBRARY*/
21*23611Sbloom 
2218620Sralph /*
2318620Sralph  *	make log entry
2413661Ssam  */
2513661Ssam logent(text, status)
2613661Ssam char *text, *status;
2713661Ssam {
2818620Sralph #ifdef LOGBYSITE
2918620Sralph 	char lfile[MAXFULLNAME];
3018620Sralph 	static char LogRmtname[64];
3118620Sralph #endif LOGBYSITE
3218620Sralph 	if (Rmtname[0] == '\0')
3318620Sralph 		strcpy(Rmtname, Myname);
3413661Ssam 	/* Open the log file if necessary */
3518620Sralph #ifdef LOGBYSITE
3618620Sralph 	if (strcmp(Rmtname, LogRmtname)) {
3718620Sralph 		if (Lp != NULL)
3818620Sralph 			fclose(Lp);
3918620Sralph 		Lp = NULL;
4018620Sralph 		Ltried = 0;
4118620Sralph 	}
4218620Sralph #endif LOGBYSITE
4313661Ssam 	if (Lp == NULL) {
4413661Ssam 		if (!Ltried) {
4513661Ssam 			int savemask;
4618620Sralph #ifdef F_SETFL
4717835Sralph 			int flags;
4817835Sralph #endif
4913661Ssam 			savemask = umask(LOGMASK);
5018620Sralph #ifdef LOGBYSITE
5118620Sralph 			(void) sprintf(lfile, "%s/%s/%s", LOGBYSITE, Progname, Rmtname);
5218620Sralph 			strcpy(LogRmtname, Rmtname);
5318620Sralph 			Lp = fopen (lfile, "a");
5418620Sralph #else !LOGBYSITE
5513661Ssam 			Lp = fopen (LOGFILE, "a");
5618620Sralph #endif LOGBYSITE
5713661Ssam 			umask(savemask);
5818620Sralph #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 
7318620Sralph /*
7418620Sralph  *	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);
9818620Sralph #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);
10118620Sralph #endif !USG
10213661Ssam 	fprintf(fp, "%s (%s)\n", status, text);
10313661Ssam 
10413661Ssam 	/* Since it's buffered */
10518620Sralph #ifndef F_SETFL
10613661Ssam 	lseek (fileno(fp), (long)0, 2);
10718620Sralph #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);
11418620Sralph #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);
11718620Sralph #endif !USG
11813661Ssam 		fprintf(stderr, "%s (%s)\n", status, text);
11913661Ssam 	}
12013661Ssam }
12113661Ssam 
12218620Sralph /*
12318620Sralph  *	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 
13918620Sralph /*
14018620Sralph  *	make system log entry
14113661Ssam  */
14213661Ssam syslog(text)
14313661Ssam char *text;
14413661Ssam {
14513661Ssam 	register struct tm *tp;
14613661Ssam 	extern struct tm *localtime();
14718620Sralph 	struct timeb clock;
14818620Sralph #ifdef LOGBYSITE
14918620Sralph 	char lfile[MAXFULLNAME];
15018620Sralph 	static char SLogRmtname[64];
15113661Ssam 
15218620Sralph 	if (strcmp(Rmtname, SLogRmtname)) {
15318620Sralph 		if (Sp != NULL)
15418620Sralph 			fclose(Sp);
15518620Sralph 		Sp = NULL;
15618620Sralph 		Stried = 0;
15718620Sralph 	}
15818620Sralph #endif LOGBYSITE
15913661Ssam 	if (Sp == NULL) {
16013661Ssam 		if (!Stried) {
16113661Ssam 			int savemask;
16218620Sralph #ifdef F_SETFL
16317835Sralph 			int flags;
16418620Sralph #endif F_SETFL
16513661Ssam 			savemask = umask(LOGMASK);
16618620Sralph #ifdef LOGBYSITE
16718620Sralph 			(void) sprintf(lfile, "%s/xferstats/%s", LOGBYSITE, Rmtname);
16818620Sralph 			strcpy(SLogRmtname, Rmtname);
16918620Sralph 			Sp = fopen (lfile, "a");
17018620Sralph #else !LOGBYSITE
17118620Sralph 			Sp = fopen (SYSLOG, "a");
17218620Sralph #endif LOGBYSITE
17313661Ssam 			umask(savemask);
17418620Sralph #ifdef F_SETFL
17517835Sralph 			flags = fcntl(fileno(Sp), F_GETFL, 0);
17617835Sralph 			fcntl(fileno(Sp), F_SETFL, flags|O_APPEND);
17718620Sralph #endif F_SETFL
17818620Sralph 
17913661Ssam 		}
18013661Ssam 		Stried = 1;
18113661Ssam 		if (Sp == NULL)
18213661Ssam 			return;
18313661Ssam 		fioclex(fileno(Sp));
18413661Ssam 	}
18517835Sralph 
18618620Sralph #ifdef USG
18718620Sralph 	time(&clock.time);
18818620Sralph 	clock.millitm = 0;
18918620Sralph #else !USG
19018620Sralph 	ftime(&clock);
19118620Sralph #endif !USG
19218620Sralph 	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);
19818620Sralph 	fprintf(Sp, "(%ld) %s\n", clock.time, text);
19918620Sralph #else !USG
20017835Sralph 	fprintf(Sp, "(%d/%d-%02d:%02d) ", tp->tm_mon + 1,
20117835Sralph 		tp->tm_mday, tp->tm_hour, tp->tm_min);
202*23611Sbloom 	fprintf(Sp, "(%ld.%02u) %s\n", clock.time, clock.millitm/10, text);
20318620Sralph #endif !USG
20413661Ssam 
20513661Ssam 	/* Position at end and flush */
20618620Sralph #ifndef F_SETFL
20713661Ssam 	lseek (fileno(Sp), (long)0, 2);
20818620Sralph #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