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