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