xref: /original-bsd/usr.bin/uucp/libuu/logent.c (revision 43bfbc1c)
1 #ifndef lint
2 static char sccsid[] = "@(#)logent.c	5.6 (Berkeley) 10/09/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 static FILE *Lp = NULL;
16 static FILE *Sp = NULL;
17 static Ltried = 0;
18 static Stried = 0;
19 
20 /*LINTLIBRARY*/
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 
85 	if (text == NULL)
86 		text = "";
87 	if (status == NULL)
88 		status = "";
89 	if (!pid)
90 		pid = getpid();
91 #ifdef USG
92 	time(&Now.time);
93 	Now.millitm = 0;
94 #else !USG
95 	ftime(&Now);
96 #endif !USG
97 	tp = localtime(&Now.time);
98 #ifdef USG
99 	fprintf(fp, "%s %s (%d/%d-%2.2d:%2.2d-%d) ",
100 #else !USG
101 	fprintf(fp, "%s %s (%d/%d-%02d:%02d-%d) ",
102 #endif !USG
103 		User, Rmtname, tp->tm_mon + 1, tp->tm_mday,
104 		tp->tm_hour, tp->tm_min, pid);
105 	fprintf(fp, "%s (%s)\n", status, text);
106 
107 	/* Since it's buffered */
108 #ifndef F_SETFL
109 	lseek (fileno(fp), (long)0, 2);
110 #endif !F_SETFL
111 	fflush (fp);
112 	if (Debug) {
113 		fprintf(stderr, "%s %s ", User, Rmtname);
114 #ifdef USG
115 		fprintf(stderr, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1,
116 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
117 #else !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 !USG
121 		fprintf(stderr, "%s (%s)\n", status, text);
122 	}
123 }
124 
125 /*
126  *	close log file
127  */
128 logcls()
129 {
130 	if (Lp != NULL)
131 		fclose(Lp);
132 	Lp = NULL;
133 	Ltried = 0;
134 
135 	if (Sp != NULL)
136 		fclose (Sp);
137 	Sp = NULL;
138 	Stried = 0;
139 }
140 
141 
142 /*
143  *	make system log entry
144  */
145 syslog(text)
146 char *text;
147 {
148 	register struct tm *tp;
149 	extern struct tm *localtime();
150 #ifdef LOGBYSITE
151 	char lfile[MAXFULLNAME];
152 	static char SLogRmtname[64];
153 
154 	if (strcmp(Rmtname, SLogRmtname)) {
155 		if (Sp != NULL)
156 			fclose(Sp);
157 		Sp = NULL;
158 		Stried = 0;
159 	}
160 #endif LOGBYSITE
161 	if (Sp == NULL) {
162 		if (!Stried) {
163 			int savemask;
164 #ifdef F_SETFL
165 			int flags;
166 #endif F_SETFL
167 			savemask = umask(LOGMASK);
168 #ifdef LOGBYSITE
169 			(void) sprintf(lfile, "%s/xferstats/%s", LOGBYSITE, Rmtname);
170 			strcpy(SLogRmtname, Rmtname);
171 			Sp = fopen (lfile, "a");
172 #else !LOGBYSITE
173 			Sp = fopen (SYSLOG, "a");
174 #endif LOGBYSITE
175 			umask(savemask);
176 #ifdef F_SETFL
177 			flags = fcntl(fileno(Sp), F_GETFL, 0);
178 			fcntl(fileno(Sp), F_SETFL, flags|O_APPEND);
179 #endif F_SETFL
180 
181 		}
182 		Stried = 1;
183 		if (Sp == NULL)
184 			return;
185 		fioclex(fileno(Sp));
186 	}
187 
188 #ifdef USG
189 	time(&Now.time);
190 	Now.millitm = 0;
191 #else !USG
192 	ftime(&Now);
193 #endif !USG
194 	tp = localtime(&Now.time);
195 
196 	fprintf(Sp, "%s %s ", User, Rmtname);
197 #ifdef USG
198 	fprintf(Sp, "(%d/%d-%2.2d:%2.2d) ", tp->tm_mon + 1,
199 		tp->tm_mday, tp->tm_hour, tp->tm_min);
200 	fprintf(Sp, "(%ld) %s\n", Now.time, text);
201 #else !USG
202 	fprintf(Sp, "(%d/%d-%02d:%02d) ", tp->tm_mon + 1,
203 		tp->tm_mday, tp->tm_hour, tp->tm_min);
204 	fprintf(Sp, "(%ld.%02u) %s\n", Now.time, Now.millitm/10, text);
205 #endif !USG
206 
207 	/* Position at end and flush */
208 #ifndef F_SETFL
209 	lseek (fileno(Sp), (long)0, 2);
210 #endif F_SETFL
211 	fflush (Sp);
212 }
213 
214 /*
215  * Arrange to close fd on exec(II).
216  * Otherwise unwanted file descriptors are inherited
217  * by other programs.  And that may be a security hole.
218  */
219 #ifndef	USG
220 #include <sgtty.h>
221 #endif
222 
223 fioclex(fd)
224 int fd;
225 {
226 	register int ret;
227 
228 #if defined(USG) || defined(BSD4_2)
229 	ret = fcntl(fd, F_SETFD, 1);	/* Steve Bellovin says this does it */
230 #else
231 	ret = ioctl(fd, FIOCLEX, STBNULL);
232 #endif
233 	if (ret)
234 		DEBUG(2, "CAN'T FIOCLEX %d\n", fd);
235 }
236