xref: /csrg-svn/usr.bin/uucp/libuu/logent.c (revision 34168)
113661Ssam #ifndef lint
2*34168Srick static char sccsid[] = "@(#)logent.c	5.9	(Berkeley) 05/04/88";
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 
15*34168Srick extern int errno;
16*34168Srick extern int sys_nerr;
17*34168Srick extern char *sys_errlist[];
18*34168Srick 
1917835Sralph static FILE *Lp = NULL;
2017835Sralph static FILE *Sp = NULL;
21*34168Srick #ifndef USE_SYSLOG
22*34168Srick static FILE *Ep = NULL;
23*34168Srick #endif /* !USE_SYSLOG */
24*34168Srick static int pid = 0;
2513661Ssam 
2623611Sbloom /*LINTLIBRARY*/
2723611Sbloom 
2818620Sralph /*
2918620Sralph  *	make log entry
3013661Ssam  */
31*34168Srick FILE *
32*34168Srick get_logfd(pname, logfilename)
33*34168Srick char *pname;
34*34168Srick char *logfilename;
3513661Ssam {
36*34168Srick 	FILE *fp;
37*34168Srick 	int savemask;
3818620Sralph #ifdef LOGBYSITE
3918620Sralph 	char lfile[MAXFULLNAME];
4018620Sralph #endif LOGBYSITE
41*34168Srick 
42*34168Srick 	savemask = umask(LOGMASK);
4318620Sralph #ifdef LOGBYSITE
44*34168Srick 	if (pname != NULL) {
45*34168Srick 		(void) sprintf(lfile, "%s/%s/%s", LOGBYSITE, pname, Rmtname);
46*34168Srick 		logfilename = lfile;
4718620Sralph 	}
4818620Sralph #endif LOGBYSITE
49*34168Srick 	fp = fopen(logfilename, "a");
50*34168Srick 	umask(savemask);
51*34168Srick 	if (fp) {
52*34168Srick #ifdef		F_SETFL
53*34168Srick 		int flags;
54*34168Srick 		flags = fcntl(fileno(fp), F_GETFL, 0);
55*34168Srick 		fcntl(fileno(Lp), F_SETFL, flags|O_APPEND);
56*34168Srick #endif		/* F_SETFL */
57*34168Srick 		fioclex(fileno(fp));
58*34168Srick 	} else /* we really want to log this, but it's the logging that failed*/
59*34168Srick 		perror(logfilename);
60*34168Srick 	return fp;
6113661Ssam }
6213661Ssam 
6318620Sralph /*
6418620Sralph  *	make a log entry
6513661Ssam  */
6613661Ssam mlogent(fp, status, text)
6713661Ssam char *text, *status;
6813661Ssam register FILE *fp;
6913661Ssam {
7013661Ssam 	register struct tm *tp;
7113661Ssam 	extern struct tm *localtime();
7213661Ssam 
7317835Sralph 	if (text == NULL)
7417835Sralph 		text = "";
7517835Sralph 	if (status == NULL)
7617835Sralph 		status = "";
7733566Srick 	if (pid == 0)
7813661Ssam 		pid = getpid();
7917835Sralph #ifdef USG
8025134Sbloom 	time(&Now.time);
8125134Sbloom 	Now.millitm = 0;
8218620Sralph #else !USG
8325134Sbloom 	ftime(&Now);
8418620Sralph #endif !USG
8525134Sbloom 	tp = localtime(&Now.time);
8625134Sbloom #ifdef USG
8725134Sbloom 	fprintf(fp, "%s %s (%d/%d-%2.2d:%2.2d-%d) ",
8825134Sbloom #else !USG
8925134Sbloom 	fprintf(fp, "%s %s (%d/%d-%02d:%02d-%d) ",
9025134Sbloom #endif !USG
9125134Sbloom 		User, Rmtname, tp->tm_mon + 1, tp->tm_mday,
9225134Sbloom 		tp->tm_hour, tp->tm_min, pid);
93*34168Srick 	fprintf(fp, "%s %s\n", status, text);
9413661Ssam 
9513661Ssam 	/* Since it's buffered */
9618620Sralph #ifndef F_SETFL
9713661Ssam 	lseek (fileno(fp), (long)0, 2);
9818620Sralph #endif !F_SETFL
9913661Ssam 	fflush (fp);
10017835Sralph 	if (Debug) {
10113661Ssam 		fprintf(stderr, "%s %s ", User, Rmtname);
10217835Sralph #ifdef USG
10317835Sralph 		fprintf(stderr, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1,
10413661Ssam 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
10518620Sralph #else !USG
10617835Sralph 		fprintf(stderr, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1,
10717835Sralph 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
10818620Sralph #endif !USG
109*34168Srick 		fprintf(stderr, "%s %s\n", status, text);
11013661Ssam 	}
11113661Ssam }
11213661Ssam 
11318620Sralph /*
11418620Sralph  *	close log file
11513661Ssam  */
11613661Ssam logcls()
11713661Ssam {
11813661Ssam 	if (Lp != NULL)
11913661Ssam 		fclose(Lp);
12013661Ssam 	Lp = NULL;
12113661Ssam 
12213661Ssam 	if (Sp != NULL)
12313661Ssam 		fclose (Sp);
12413661Ssam 	Sp = NULL;
125*34168Srick #ifndef USE_SYSLOG
126*34168Srick 	if (Ep != NULL)
127*34168Srick 		fclose (Ep);
128*34168Srick 	Ep = NULL;
129*34168Srick #endif /* !USE_SYSLOG */
13013661Ssam }
13113661Ssam 
13218620Sralph /*
13313661Ssam  * Arrange to close fd on exec(II).
13413661Ssam  * Otherwise unwanted file descriptors are inherited
13513661Ssam  * by other programs.  And that may be a security hole.
13613661Ssam  */
13717835Sralph #ifndef	USG
13813661Ssam #include <sgtty.h>
13913661Ssam #endif
14013661Ssam 
14113661Ssam fioclex(fd)
14213661Ssam int fd;
14313661Ssam {
14413661Ssam 	register int ret;
14513661Ssam 
14617835Sralph #if defined(USG) || defined(BSD4_2)
14713661Ssam 	ret = fcntl(fd, F_SETFD, 1);	/* Steve Bellovin says this does it */
14817835Sralph #else
14913661Ssam 	ret = ioctl(fd, FIOCLEX, STBNULL);
15013661Ssam #endif
15113661Ssam 	if (ret)
15213661Ssam 		DEBUG(2, "CAN'T FIOCLEX %d\n", fd);
15313661Ssam }
154*34168Srick 
155*34168Srick logent(text, status)
156*34168Srick char *text, *status;
157*34168Srick {
158*34168Srick 	if (Lp == NULL)
159*34168Srick 		Lp = get_logfd(Progname, LOGFILE);
160*34168Srick 
161*34168Srick 	mlogent(Lp, status, text);
162*34168Srick }
163*34168Srick 
164*34168Srick /*
165*34168Srick  *	make system log entry
166*34168Srick  */
167*34168Srick log_xferstats(text)
168*34168Srick char *text;
169*34168Srick {
170*34168Srick 	char tbuf[BUFSIZ];
171*34168Srick 	if (Sp == NULL)
172*34168Srick 		Sp = get_logfd("xferstats", SYSLOG);
173*34168Srick 	sprintf(tbuf, "(%ld.%02u)", Now.time, Now.millitm/10);
174*34168Srick 	mlogent(Sp, tbuf, text);
175*34168Srick }
176*34168Srick 
177*34168Srick #ifndef USE_SYSLOG
178*34168Srick /*
179*34168Srick  * This is for sites that don't have a decent syslog() in their library
180*34168Srick  * This routine would be a lot simpler if syslog() didn't permit %m
181*34168Srick  * (or if printf did!)
182*34168Srick  */
183*34168Srick syslog(priority, format, p0, p1, p2, p3, p4)
184*34168Srick int priority;
185*34168Srick char *format;
186*34168Srick {
187*34168Srick 	char nformat[BUFSIZ], sbuf[BUFSIZ];
188*34168Srick 	register char *s, *d;
189*34168Srick 	register int c;
190*34168Srick 	long now;
191*34168Srick 
192*34168Srick 	s = format;
193*34168Srick 	d = nformat;
194*34168Srick 	while ((c = *s++) != '\0' && c != '\n' && d < &nformat[BUFSIZ]) {
195*34168Srick 		if (c != '%') {
196*34168Srick 			*d++ = c;
197*34168Srick 			continue;
198*34168Srick 		}
199*34168Srick 		if ((c = *s++) != 'm') {
200*34168Srick 			*d++ = '%';
201*34168Srick 			*d++ = c;
202*34168Srick 			continue;
203*34168Srick 		}
204*34168Srick 		if ((unsigned)errno > sys_nerr)
205*34168Srick 			sprintf(d, "error %d", errno);
206*34168Srick 		else
207*34168Srick 			strcpy(d, sys_errlist[errno]);
208*34168Srick 		d += strlen(d);
209*34168Srick 	}
210*34168Srick 	*d = '\0';
211*34168Srick 
212*34168Srick 	if (Ep == NULL)
213*34168Srick 		Ep = get_logfd(NULL, ERRLOG);
214*34168Srick 	sprintf(sbuf, nformat, p0, p1, p2, p3, p4);
215*34168Srick 	mlogent(Ep, sbuf, "");
216*34168Srick }
217*34168Srick #endif /* !USE_SYSLOG */
218