xref: /original-bsd/usr.bin/uucp/libuu/logent.c (revision 8216efb6)
19677bed1Sbostic /*-
2*8216efb6Sbostic  * Copyright (c) 1985, 1993
3*8216efb6Sbostic  *	The Regents of the University of California.  All rights reserved.
49677bed1Sbostic  *
59677bed1Sbostic  * %sccs.include.proprietary.c%
69677bed1Sbostic  */
79677bed1Sbostic 
87b72b34eSsam #ifndef lint
9*8216efb6Sbostic static char sccsid[] = "@(#)logent.c	8.1 (Berkeley) 06/06/93";
109677bed1Sbostic #endif /* not lint */
117b72b34eSsam 
127b72b34eSsam #include "uucp.h"
13c9dd95d8Sralph #ifdef BSD4_2
140d244a74Ssam #include <sys/time.h>
15c9dd95d8Sralph #else
16c9dd95d8Sralph #include <time.h>
17c9dd95d8Sralph #endif
18c9dd95d8Sralph #if defined(USG) || defined(BSD4_2)
19c9dd95d8Sralph #include <fcntl.h>
20c9dd95d8Sralph #endif
217b72b34eSsam 
22df7947e8Srick extern int errno;
23df7947e8Srick extern int sys_nerr;
2439408173Storek extern const char *const sys_errlist[];
25df7947e8Srick 
26c9dd95d8Sralph static FILE *Lp = NULL;
27c9dd95d8Sralph static FILE *Sp = NULL;
28df7947e8Srick #ifndef USE_SYSLOG
29df7947e8Srick static FILE *Ep = NULL;
30df7947e8Srick #endif /* !USE_SYSLOG */
31df7947e8Srick static int pid = 0;
327b72b34eSsam 
33574bec65Sbloom /*LINTLIBRARY*/
34574bec65Sbloom 
35e90e093fSralph /*
36e90e093fSralph  *	make log entry
377b72b34eSsam  */
38df7947e8Srick FILE *
get_logfd(pname,logfilename)39df7947e8Srick get_logfd(pname, logfilename)
40df7947e8Srick char *pname;
41df7947e8Srick char *logfilename;
427b72b34eSsam {
43df7947e8Srick 	FILE *fp;
44df7947e8Srick 	int savemask;
45e90e093fSralph #ifdef LOGBYSITE
46e90e093fSralph 	char lfile[MAXFULLNAME];
47e90e093fSralph #endif LOGBYSITE
48df7947e8Srick 
497b72b34eSsam 	savemask = umask(LOGMASK);
50e90e093fSralph #ifdef LOGBYSITE
51df7947e8Srick 	if (pname != NULL) {
52df7947e8Srick 		(void) sprintf(lfile, "%s/%s/%s", LOGBYSITE, pname, Rmtname);
53df7947e8Srick 		logfilename = lfile;
54df7947e8Srick 	}
55e90e093fSralph #endif LOGBYSITE
56df7947e8Srick 	fp = fopen(logfilename, "a");
577b72b34eSsam 	umask(savemask);
58df7947e8Srick 	if (fp) {
59e90e093fSralph #ifdef		F_SETFL
60df7947e8Srick 		int flags;
61df7947e8Srick 		flags = fcntl(fileno(fp), F_GETFL, 0);
62c9dd95d8Sralph 		fcntl(fileno(Lp), F_SETFL, flags|O_APPEND);
63df7947e8Srick #endif		/* F_SETFL */
64df7947e8Srick 		fioclex(fileno(fp));
65df7947e8Srick 	} else /* we really want to log this, but it's the logging that failed*/
66df7947e8Srick 		perror(logfilename);
67df7947e8Srick 	return fp;
687b72b34eSsam }
697b72b34eSsam 
70e90e093fSralph /*
71e90e093fSralph  *	make a log entry
727b72b34eSsam  */
mlogent(fp,status,text)737b72b34eSsam mlogent(fp, status, text)
747b72b34eSsam char *text, *status;
757b72b34eSsam register FILE *fp;
767b72b34eSsam {
777b72b34eSsam 	register struct tm *tp;
787b72b34eSsam 	extern struct tm *localtime();
797b72b34eSsam 
80c9dd95d8Sralph 	if (text == NULL)
81c9dd95d8Sralph 		text = "";
82c9dd95d8Sralph 	if (status == NULL)
83c9dd95d8Sralph 		status = "";
841721ddfdSrick 	if (pid == 0)
857b72b34eSsam 		pid = getpid();
86c9dd95d8Sralph #ifdef USG
87255e2b26Sbloom 	time(&Now.time);
88255e2b26Sbloom 	Now.millitm = 0;
89e90e093fSralph #else !USG
90255e2b26Sbloom 	ftime(&Now);
91e90e093fSralph #endif !USG
92255e2b26Sbloom 	tp = localtime(&Now.time);
93255e2b26Sbloom #ifdef USG
94255e2b26Sbloom 	fprintf(fp, "%s %s (%d/%d-%2.2d:%2.2d-%d) ",
95255e2b26Sbloom #else !USG
96255e2b26Sbloom 	fprintf(fp, "%s %s (%d/%d-%02d:%02d-%d) ",
97255e2b26Sbloom #endif !USG
98255e2b26Sbloom 		User, Rmtname, tp->tm_mon + 1, tp->tm_mday,
99255e2b26Sbloom 		tp->tm_hour, tp->tm_min, pid);
100df7947e8Srick 	fprintf(fp, "%s %s\n", status, text);
1017b72b34eSsam 
1027b72b34eSsam 	/* Since it's buffered */
103e90e093fSralph #ifndef F_SETFL
1047b72b34eSsam 	lseek (fileno(fp), (long)0, 2);
105e90e093fSralph #endif !F_SETFL
1067b72b34eSsam 	fflush (fp);
107c9dd95d8Sralph 	if (Debug) {
1087b72b34eSsam 		fprintf(stderr, "%s %s ", User, Rmtname);
109c9dd95d8Sralph #ifdef USG
110c9dd95d8Sralph 		fprintf(stderr, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1,
1117b72b34eSsam 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
112e90e093fSralph #else !USG
113c9dd95d8Sralph 		fprintf(stderr, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1,
114c9dd95d8Sralph 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
115e90e093fSralph #endif !USG
116df7947e8Srick 		fprintf(stderr, "%s %s\n", status, text);
1177b72b34eSsam 	}
1187b72b34eSsam }
1197b72b34eSsam 
120e90e093fSralph /*
121e90e093fSralph  *	close log file
1227b72b34eSsam  */
logcls()1237b72b34eSsam logcls()
1247b72b34eSsam {
1257b72b34eSsam 	if (Lp != NULL)
1267b72b34eSsam 		fclose(Lp);
1277b72b34eSsam 	Lp = NULL;
1287b72b34eSsam 
1297b72b34eSsam 	if (Sp != NULL)
1307b72b34eSsam 		fclose (Sp);
1317b72b34eSsam 	Sp = NULL;
132df7947e8Srick #ifndef USE_SYSLOG
133df7947e8Srick 	if (Ep != NULL)
134df7947e8Srick 		fclose (Ep);
135df7947e8Srick 	Ep = NULL;
136df7947e8Srick #endif /* !USE_SYSLOG */
1377b72b34eSsam }
1387b72b34eSsam 
1397b72b34eSsam /*
1407b72b34eSsam  * Arrange to close fd on exec(II).
1417b72b34eSsam  * Otherwise unwanted file descriptors are inherited
1427b72b34eSsam  * by other programs.  And that may be a security hole.
1437b72b34eSsam  */
144c9dd95d8Sralph #ifndef	USG
1457b72b34eSsam #include <sgtty.h>
1467b72b34eSsam #endif
1477b72b34eSsam 
fioclex(fd)1487b72b34eSsam fioclex(fd)
1497b72b34eSsam int fd;
1507b72b34eSsam {
1517b72b34eSsam 	register int ret;
1527b72b34eSsam 
153c9dd95d8Sralph #if defined(USG) || defined(BSD4_2)
1547b72b34eSsam 	ret = fcntl(fd, F_SETFD, 1);	/* Steve Bellovin says this does it */
155c9dd95d8Sralph #else
1567b72b34eSsam 	ret = ioctl(fd, FIOCLEX, STBNULL);
1577b72b34eSsam #endif
1587b72b34eSsam 	if (ret)
1597b72b34eSsam 		DEBUG(2, "CAN'T FIOCLEX %d\n", fd);
1607b72b34eSsam }
161df7947e8Srick 
logent(text,status)162df7947e8Srick logent(text, status)
163df7947e8Srick char *text, *status;
164df7947e8Srick {
165df7947e8Srick 	if (Lp == NULL)
166df7947e8Srick 		Lp = get_logfd(Progname, LOGFILE);
167df7947e8Srick 
168df7947e8Srick 	mlogent(Lp, status, text);
169df7947e8Srick }
170df7947e8Srick 
171df7947e8Srick /*
172df7947e8Srick  *	make system log entry
173df7947e8Srick  */
log_xferstats(text)174df7947e8Srick log_xferstats(text)
175df7947e8Srick char *text;
176df7947e8Srick {
177df7947e8Srick 	char tbuf[BUFSIZ];
178df7947e8Srick 	if (Sp == NULL)
179df7947e8Srick 		Sp = get_logfd("xferstats", SYSLOG);
180df7947e8Srick 	sprintf(tbuf, "(%ld.%02u)", Now.time, Now.millitm/10);
181df7947e8Srick 	mlogent(Sp, tbuf, text);
182df7947e8Srick }
183df7947e8Srick 
184df7947e8Srick #ifndef USE_SYSLOG
185df7947e8Srick /*
186df7947e8Srick  * This is for sites that don't have a decent syslog() in their library
187df7947e8Srick  * This routine would be a lot simpler if syslog() didn't permit %m
188df7947e8Srick  * (or if printf did!)
189df7947e8Srick  */
syslog(priority,format,p0,p1,p2,p3,p4)190df7947e8Srick syslog(priority, format, p0, p1, p2, p3, p4)
191df7947e8Srick int priority;
192df7947e8Srick char *format;
193df7947e8Srick {
194df7947e8Srick 	char nformat[BUFSIZ], sbuf[BUFSIZ];
195df7947e8Srick 	register char *s, *d;
196df7947e8Srick 	register int c;
197df7947e8Srick 	long now;
198df7947e8Srick 
199df7947e8Srick 	s = format;
200df7947e8Srick 	d = nformat;
201df7947e8Srick 	while ((c = *s++) != '\0' && c != '\n' && d < &nformat[BUFSIZ]) {
202df7947e8Srick 		if (c != '%') {
203df7947e8Srick 			*d++ = c;
204df7947e8Srick 			continue;
205df7947e8Srick 		}
206df7947e8Srick 		if ((c = *s++) != 'm') {
207df7947e8Srick 			*d++ = '%';
208df7947e8Srick 			*d++ = c;
209df7947e8Srick 			continue;
210df7947e8Srick 		}
211df7947e8Srick 		if ((unsigned)errno > sys_nerr)
212df7947e8Srick 			sprintf(d, "error %d", errno);
213df7947e8Srick 		else
214df7947e8Srick 			strcpy(d, sys_errlist[errno]);
215df7947e8Srick 		d += strlen(d);
216df7947e8Srick 	}
217df7947e8Srick 	*d = '\0';
218df7947e8Srick 
219df7947e8Srick 	if (Ep == NULL)
220df7947e8Srick 		Ep = get_logfd(NULL, ERRLOG);
221df7947e8Srick 	sprintf(sbuf, nformat, p0, p1, p2, p3, p4);
222df7947e8Srick 	mlogent(Ep, sbuf, "");
223df7947e8Srick }
224df7947e8Srick #endif /* !USE_SYSLOG */
225