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