xref: /original-bsd/usr.bin/uucp/libuu/logent.c (revision 60c3b96a)
1 #ifndef lint
2 static char sccsid[] = "@(#)logent.c	5.9	(Berkeley) 05/04/88";
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 int errno;
16 extern int sys_nerr;
17 extern char *sys_errlist[];
18 
19 static FILE *Lp = NULL;
20 static FILE *Sp = NULL;
21 #ifndef USE_SYSLOG
22 static FILE *Ep = NULL;
23 #endif /* !USE_SYSLOG */
24 static int pid = 0;
25 
26 /*LINTLIBRARY*/
27 
28 /*
29  *	make log entry
30  */
31 FILE *
32 get_logfd(pname, logfilename)
33 char *pname;
34 char *logfilename;
35 {
36 	FILE *fp;
37 	int savemask;
38 #ifdef LOGBYSITE
39 	char lfile[MAXFULLNAME];
40 #endif LOGBYSITE
41 
42 	savemask = umask(LOGMASK);
43 #ifdef LOGBYSITE
44 	if (pname != NULL) {
45 		(void) sprintf(lfile, "%s/%s/%s", LOGBYSITE, pname, Rmtname);
46 		logfilename = lfile;
47 	}
48 #endif LOGBYSITE
49 	fp = fopen(logfilename, "a");
50 	umask(savemask);
51 	if (fp) {
52 #ifdef		F_SETFL
53 		int flags;
54 		flags = fcntl(fileno(fp), F_GETFL, 0);
55 		fcntl(fileno(Lp), F_SETFL, flags|O_APPEND);
56 #endif		/* F_SETFL */
57 		fioclex(fileno(fp));
58 	} else /* we really want to log this, but it's the logging that failed*/
59 		perror(logfilename);
60 	return fp;
61 }
62 
63 /*
64  *	make a log entry
65  */
66 mlogent(fp, status, text)
67 char *text, *status;
68 register FILE *fp;
69 {
70 	register struct tm *tp;
71 	extern struct tm *localtime();
72 
73 	if (text == NULL)
74 		text = "";
75 	if (status == NULL)
76 		status = "";
77 	if (pid == 0)
78 		pid = getpid();
79 #ifdef USG
80 	time(&Now.time);
81 	Now.millitm = 0;
82 #else !USG
83 	ftime(&Now);
84 #endif !USG
85 	tp = localtime(&Now.time);
86 #ifdef USG
87 	fprintf(fp, "%s %s (%d/%d-%2.2d:%2.2d-%d) ",
88 #else !USG
89 	fprintf(fp, "%s %s (%d/%d-%02d:%02d-%d) ",
90 #endif !USG
91 		User, Rmtname, tp->tm_mon + 1, tp->tm_mday,
92 		tp->tm_hour, tp->tm_min, pid);
93 	fprintf(fp, "%s %s\n", status, text);
94 
95 	/* Since it's buffered */
96 #ifndef F_SETFL
97 	lseek (fileno(fp), (long)0, 2);
98 #endif !F_SETFL
99 	fflush (fp);
100 	if (Debug) {
101 		fprintf(stderr, "%s %s ", User, Rmtname);
102 #ifdef USG
103 		fprintf(stderr, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1,
104 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
105 #else !USG
106 		fprintf(stderr, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1,
107 			tp->tm_mday, tp->tm_hour, tp->tm_min, pid);
108 #endif !USG
109 		fprintf(stderr, "%s %s\n", status, text);
110 	}
111 }
112 
113 /*
114  *	close log file
115  */
116 logcls()
117 {
118 	if (Lp != NULL)
119 		fclose(Lp);
120 	Lp = NULL;
121 
122 	if (Sp != NULL)
123 		fclose (Sp);
124 	Sp = NULL;
125 #ifndef USE_SYSLOG
126 	if (Ep != NULL)
127 		fclose (Ep);
128 	Ep = NULL;
129 #endif /* !USE_SYSLOG */
130 }
131 
132 /*
133  * Arrange to close fd on exec(II).
134  * Otherwise unwanted file descriptors are inherited
135  * by other programs.  And that may be a security hole.
136  */
137 #ifndef	USG
138 #include <sgtty.h>
139 #endif
140 
141 fioclex(fd)
142 int fd;
143 {
144 	register int ret;
145 
146 #if defined(USG) || defined(BSD4_2)
147 	ret = fcntl(fd, F_SETFD, 1);	/* Steve Bellovin says this does it */
148 #else
149 	ret = ioctl(fd, FIOCLEX, STBNULL);
150 #endif
151 	if (ret)
152 		DEBUG(2, "CAN'T FIOCLEX %d\n", fd);
153 }
154 
155 logent(text, status)
156 char *text, *status;
157 {
158 	if (Lp == NULL)
159 		Lp = get_logfd(Progname, LOGFILE);
160 
161 	mlogent(Lp, status, text);
162 }
163 
164 /*
165  *	make system log entry
166  */
167 log_xferstats(text)
168 char *text;
169 {
170 	char tbuf[BUFSIZ];
171 	if (Sp == NULL)
172 		Sp = get_logfd("xferstats", SYSLOG);
173 	sprintf(tbuf, "(%ld.%02u)", Now.time, Now.millitm/10);
174 	mlogent(Sp, tbuf, text);
175 }
176 
177 #ifndef USE_SYSLOG
178 /*
179  * This is for sites that don't have a decent syslog() in their library
180  * This routine would be a lot simpler if syslog() didn't permit %m
181  * (or if printf did!)
182  */
183 syslog(priority, format, p0, p1, p2, p3, p4)
184 int priority;
185 char *format;
186 {
187 	char nformat[BUFSIZ], sbuf[BUFSIZ];
188 	register char *s, *d;
189 	register int c;
190 	long now;
191 
192 	s = format;
193 	d = nformat;
194 	while ((c = *s++) != '\0' && c != '\n' && d < &nformat[BUFSIZ]) {
195 		if (c != '%') {
196 			*d++ = c;
197 			continue;
198 		}
199 		if ((c = *s++) != 'm') {
200 			*d++ = '%';
201 			*d++ = c;
202 			continue;
203 		}
204 		if ((unsigned)errno > sys_nerr)
205 			sprintf(d, "error %d", errno);
206 		else
207 			strcpy(d, sys_errlist[errno]);
208 		d += strlen(d);
209 	}
210 	*d = '\0';
211 
212 	if (Ep == NULL)
213 		Ep = get_logfd(NULL, ERRLOG);
214 	sprintf(sbuf, nformat, p0, p1, p2, p3, p4);
215 	mlogent(Ep, sbuf, "");
216 }
217 #endif /* !USE_SYSLOG */
218