xref: /csrg-svn/usr.bin/uucp/libuu/logent.c (revision 13661)
1*13661Ssam #ifndef lint
2*13661Ssam static char sccsid[] = "@(#)logent.c	5.1 (Berkeley) 07/02/83";
3*13661Ssam #endif
4*13661Ssam 
5*13661Ssam #include "uucp.h"
6*13661Ssam #include <sys/types.h>
7*13661Ssam #include <time.h>
8*13661Ssam 
9*13661Ssam extern	time_t	time();
10*13661Ssam 
11*13661Ssam /* This logfile stuff was awful -- it did output to an
12*13661Ssam  * unbuffered stream.
13*13661Ssam  *
14*13661Ssam  * This new version just open the single logfile and writes
15*13661Ssam  * the record in the stdio buffer.  Once that's done, it
16*13661Ssam  * positions itself at the end of the file (lseek), and
17*13661Ssam  * writes the buffer out.  This could mangle things but
18*13661Ssam  * it isn't likely. -- ittvax!swatt
19*13661Ssam  *
20*13661Ssam  * If the files could be opened with "guaranteed append to end",
21*13661Ssam  * the lseeks could be removed.
22*13661Ssam  * Using fseek would be slightly cleaner,
23*13661Ssam  * but would mangle things slightly more often.
24*13661Ssam  */
25*13661Ssam 
26*13661Ssam 
27*13661Ssam FILE *Lp = NULL;
28*13661Ssam FILE *Sp = NULL;
29*13661Ssam static Ltried = 0;
30*13661Ssam static Stried = 0;
31*13661Ssam 
32*13661Ssam /*******
33*13661Ssam  *	logent(text, status)	make log entry
34*13661Ssam  *	char *text, *status;
35*13661Ssam  *
36*13661Ssam  *	return code - none
37*13661Ssam  */
38*13661Ssam 
39*13661Ssam logent(text, status)
40*13661Ssam char *text, *status;
41*13661Ssam {
42*13661Ssam 	/* Open the log file if necessary */
43*13661Ssam 	if (Lp == NULL) {
44*13661Ssam 		if (!Ltried) {
45*13661Ssam 			int savemask;
46*13661Ssam 			savemask = umask(LOGMASK);
47*13661Ssam 			Lp = fopen (LOGFILE, "a");
48*13661Ssam 			umask(savemask);
49*13661Ssam 		}
50*13661Ssam 		Ltried = 1;
51*13661Ssam 		if (Lp == NULL)
52*13661Ssam 			return;
53*13661Ssam 		fioclex(fileno(Lp));
54*13661Ssam 	}
55*13661Ssam 
56*13661Ssam 	/*  make entry in existing temp log file  */
57*13661Ssam 	mlogent(Lp, status, text);
58*13661Ssam }
59*13661Ssam 
60*13661Ssam /***
61*13661Ssam  *	mlogent(fp, status, text)  - make a log entry
62*13661Ssam  */
63*13661Ssam 
64*13661Ssam mlogent(fp, status, text)
65*13661Ssam char *text, *status;
66*13661Ssam register FILE *fp;
67*13661Ssam {
68*13661Ssam 	static pid = 0;
69*13661Ssam 	register struct tm *tp;
70*13661Ssam 	extern struct tm *localtime();
71*13661Ssam 	time_t clock;
72*13661Ssam 
73*13661Ssam 	if (!pid)
74*13661Ssam 		pid = getpid();
75*13661Ssam 	time(&clock);
76*13661Ssam 	tp = localtime(&clock);
77*13661Ssam 	fprintf(fp, "%s %s ", User, Rmtname);
78*13661Ssam 	fprintf(fp, "(%d/%d-%d:%02d-%d) ", tp->tm_mon + 1,
79*13661Ssam 		tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
80*13661Ssam 	fprintf(fp, "%s (%s)\n", status, text);
81*13661Ssam 
82*13661Ssam 	/* Since it's buffered */
83*13661Ssam 	lseek (fileno(fp), (long)0, 2);
84*13661Ssam 	fflush (fp);
85*13661Ssam 	if (Debug > 0) {
86*13661Ssam 		fprintf(stderr, "%s %s ", User, Rmtname);
87*13661Ssam 		fprintf(stderr, "(%d/%d-%d:%02d-%d) ", tp->tm_mon + 1,
88*13661Ssam 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
89*13661Ssam 		fprintf(stderr, "%s (%s)\n", status, text);
90*13661Ssam 	}
91*13661Ssam }
92*13661Ssam 
93*13661Ssam /***
94*13661Ssam  *	logcls()	close log file
95*13661Ssam  *
96*13661Ssam  *	return codes:  none
97*13661Ssam  */
98*13661Ssam 
99*13661Ssam logcls()
100*13661Ssam {
101*13661Ssam 	if (Lp != NULL)
102*13661Ssam 		fclose(Lp);
103*13661Ssam 	Lp = NULL;
104*13661Ssam 	Ltried = 0;
105*13661Ssam 
106*13661Ssam 	if (Sp != NULL)
107*13661Ssam 		fclose (Sp);
108*13661Ssam 	Sp = NULL;
109*13661Ssam 	Stried = 0;
110*13661Ssam }
111*13661Ssam 
112*13661Ssam 
113*13661Ssam /***
114*13661Ssam  *	syslog(text)	make system log entry
115*13661Ssam  *	char *text;
116*13661Ssam  *
117*13661Ssam  *	return codes - none
118*13661Ssam  */
119*13661Ssam 
120*13661Ssam syslog(text)
121*13661Ssam char *text;
122*13661Ssam {
123*13661Ssam 	register struct tm *tp;
124*13661Ssam 	extern struct tm *localtime();
125*13661Ssam 	time_t clock;
126*13661Ssam 
127*13661Ssam 	if (Sp == NULL) {
128*13661Ssam 		if (!Stried) {
129*13661Ssam 			int savemask;
130*13661Ssam 			savemask = umask(LOGMASK);
131*13661Ssam 			Sp = fopen(SYSLOG, "a");
132*13661Ssam 			umask(savemask);
133*13661Ssam 		}
134*13661Ssam 		Stried = 1;
135*13661Ssam 		if (Sp == NULL)
136*13661Ssam 			return;
137*13661Ssam 		fioclex(fileno(Sp));
138*13661Ssam 	}
139*13661Ssam 
140*13661Ssam 	time(&clock);
141*13661Ssam 	tp = localtime(&clock);
142*13661Ssam 
143*13661Ssam 	fprintf(Sp, "%s %s ", User, Rmtname);
144*13661Ssam 	fprintf(Sp, "(%d/%d-%d:%02d) ", tp->tm_mon + 1,
145*13661Ssam 		tp->tm_mday, tp->tm_hour, tp->tm_min);
146*13661Ssam 	fprintf(Sp, "(%ld) %s\n", clock, text);
147*13661Ssam 
148*13661Ssam 	/* Position at end and flush */
149*13661Ssam 	lseek (fileno(Sp), (long)0, 2);
150*13661Ssam 	fflush (Sp);
151*13661Ssam }
152*13661Ssam 
153*13661Ssam /*
154*13661Ssam  * Arrange to close fd on exec(II).
155*13661Ssam  * Otherwise unwanted file descriptors are inherited
156*13661Ssam  * by other programs.  And that may be a security hole.
157*13661Ssam  */
158*13661Ssam #ifdef SYSIII
159*13661Ssam #include <fcntl.h>
160*13661Ssam #endif
161*13661Ssam #ifndef	SYSIII
162*13661Ssam #include <sgtty.h>
163*13661Ssam #endif
164*13661Ssam 
165*13661Ssam fioclex(fd)
166*13661Ssam int fd;
167*13661Ssam {
168*13661Ssam 	register int ret;
169*13661Ssam 
170*13661Ssam #ifdef	SYSIII
171*13661Ssam 	ret = fcntl(fd, F_SETFD, 1);	/* Steve Bellovin says this does it */
172*13661Ssam #endif
173*13661Ssam #ifndef	SYSIII
174*13661Ssam 	ret = ioctl(fd, FIOCLEX, STBNULL);
175*13661Ssam #endif
176*13661Ssam 	if (ret)
177*13661Ssam 		DEBUG(2, "CAN'T FIOCLEX %d\n", fd);
178*13661Ssam }
179