Commit | Line | Data |
---|---|---|
cc767c2b | 1 | #ifndef lint |
44c9962f | 2 | static char sccsid[] = "@(#)logent.c 5.5 (Berkeley) %G%"; |
cc767c2b SL |
3 | #endif |
4 | ||
5 | #include "uucp.h" | |
46b15d8a | 6 | #ifdef BSD4_2 |
62ba4a08 | 7 | #include <sys/time.h> |
46b15d8a RC |
8 | #else |
9 | #include <time.h> | |
10 | #endif | |
11 | #if defined(USG) || defined(BSD4_2) | |
12 | #include <fcntl.h> | |
13 | #endif | |
cc767c2b | 14 | |
46b15d8a RC |
15 | static FILE *Lp = NULL; |
16 | static FILE *Sp = NULL; | |
cc767c2b SL |
17 | static Ltried = 0; |
18 | static Stried = 0; | |
19 | ||
44c9962f JB |
20 | /*LINTLIBRARY*/ |
21 | ||
1a85e9d2 RC |
22 | /* |
23 | * make log entry | |
cc767c2b | 24 | */ |
cc767c2b SL |
25 | logent(text, status) |
26 | char *text, *status; | |
27 | { | |
1a85e9d2 RC |
28 | #ifdef LOGBYSITE |
29 | char lfile[MAXFULLNAME]; | |
30 | static char LogRmtname[64]; | |
31 | #endif LOGBYSITE | |
32 | if (Rmtname[0] == '\0') | |
33 | strcpy(Rmtname, Myname); | |
cc767c2b | 34 | /* Open the log file if necessary */ |
1a85e9d2 RC |
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 | |
cc767c2b SL |
43 | if (Lp == NULL) { |
44 | if (!Ltried) { | |
45 | int savemask; | |
1a85e9d2 | 46 | #ifdef F_SETFL |
46b15d8a RC |
47 | int flags; |
48 | #endif | |
cc767c2b | 49 | savemask = umask(LOGMASK); |
1a85e9d2 RC |
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 | |
cc767c2b | 55 | Lp = fopen (LOGFILE, "a"); |
1a85e9d2 | 56 | #endif LOGBYSITE |
cc767c2b | 57 | umask(savemask); |
1a85e9d2 | 58 | #ifdef F_SETFL |
46b15d8a RC |
59 | flags = fcntl(fileno(Lp), F_GETFL, 0); |
60 | fcntl(fileno(Lp), F_SETFL, flags|O_APPEND); | |
61 | #endif | |
cc767c2b SL |
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 | ||
1a85e9d2 RC |
73 | /* |
74 | * make a log entry | |
cc767c2b SL |
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 | ||
46b15d8a RC |
86 | if (text == NULL) |
87 | text = ""; | |
88 | if (status == NULL) | |
89 | status = ""; | |
cc767c2b SL |
90 | if (!pid) |
91 | pid = getpid(); | |
92 | time(&clock); | |
93 | tp = localtime(&clock); | |
94 | fprintf(fp, "%s %s ", User, Rmtname); | |
46b15d8a RC |
95 | #ifdef USG |
96 | fprintf(fp, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1, | |
cc767c2b | 97 | tp->tm_mday, tp->tm_hour, tp->tm_min, pid); |
1a85e9d2 | 98 | #else !USG |
46b15d8a RC |
99 | fprintf(fp, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1, |
100 | tp->tm_mday, tp->tm_hour, tp->tm_min, pid); | |
1a85e9d2 | 101 | #endif !USG |
cc767c2b SL |
102 | fprintf(fp, "%s (%s)\n", status, text); |
103 | ||
104 | /* Since it's buffered */ | |
1a85e9d2 | 105 | #ifndef F_SETFL |
cc767c2b | 106 | lseek (fileno(fp), (long)0, 2); |
1a85e9d2 | 107 | #endif !F_SETFL |
cc767c2b | 108 | fflush (fp); |
46b15d8a | 109 | if (Debug) { |
cc767c2b | 110 | fprintf(stderr, "%s %s ", User, Rmtname); |
46b15d8a RC |
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); | |
1a85e9d2 | 114 | #else !USG |
46b15d8a | 115 | fprintf(stderr, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1, |
cc767c2b | 116 | tp->tm_mday, tp->tm_hour, tp->tm_min, pid); |
1a85e9d2 | 117 | #endif !USG |
cc767c2b SL |
118 | fprintf(stderr, "%s (%s)\n", status, text); |
119 | } | |
120 | } | |
121 | ||
1a85e9d2 RC |
122 | /* |
123 | * close log file | |
cc767c2b | 124 | */ |
cc767c2b SL |
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 | ||
1a85e9d2 RC |
139 | /* |
140 | * make system log entry | |
cc767c2b | 141 | */ |
cc767c2b SL |
142 | syslog(text) |
143 | char *text; | |
144 | { | |
145 | register struct tm *tp; | |
146 | extern struct tm *localtime(); | |
1a85e9d2 RC |
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 | |
cc767c2b SL |
159 | if (Sp == NULL) { |
160 | if (!Stried) { | |
161 | int savemask; | |
1a85e9d2 | 162 | #ifdef F_SETFL |
46b15d8a | 163 | int flags; |
1a85e9d2 | 164 | #endif F_SETFL |
cc767c2b | 165 | savemask = umask(LOGMASK); |
1a85e9d2 RC |
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 | |
cc767c2b | 173 | umask(savemask); |
1a85e9d2 | 174 | #ifdef F_SETFL |
46b15d8a RC |
175 | flags = fcntl(fileno(Sp), F_GETFL, 0); |
176 | fcntl(fileno(Sp), F_SETFL, flags|O_APPEND); | |
1a85e9d2 RC |
177 | #endif F_SETFL |
178 | ||
cc767c2b SL |
179 | } |
180 | Stried = 1; | |
181 | if (Sp == NULL) | |
182 | return; | |
183 | fioclex(fileno(Sp)); | |
184 | } | |
46b15d8a | 185 | |
1a85e9d2 RC |
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); | |
cc767c2b SL |
193 | |
194 | fprintf(Sp, "%s %s ", User, Rmtname); | |
46b15d8a RC |
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); | |
1a85e9d2 RC |
198 | fprintf(Sp, "(%ld) %s\n", clock.time, text); |
199 | #else !USG | |
46b15d8a | 200 | fprintf(Sp, "(%d/%d-%02d:%02d) ", tp->tm_mon + 1, |
cc767c2b | 201 | tp->tm_mday, tp->tm_hour, tp->tm_min); |
44c9962f | 202 | fprintf(Sp, "(%ld.%02u) %s\n", clock.time, clock.millitm/10, text); |
1a85e9d2 | 203 | #endif !USG |
cc767c2b SL |
204 | |
205 | /* Position at end and flush */ | |
1a85e9d2 | 206 | #ifndef F_SETFL |
cc767c2b | 207 | lseek (fileno(Sp), (long)0, 2); |
1a85e9d2 | 208 | #endif F_SETFL |
cc767c2b SL |
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 | */ | |
46b15d8a | 217 | #ifndef USG |
cc767c2b SL |
218 | #include <sgtty.h> |
219 | #endif | |
220 | ||
221 | fioclex(fd) | |
222 | int fd; | |
223 | { | |
224 | register int ret; | |
225 | ||
46b15d8a | 226 | #if defined(USG) || defined(BSD4_2) |
cc767c2b | 227 | ret = fcntl(fd, F_SETFD, 1); /* Steve Bellovin says this does it */ |
46b15d8a | 228 | #else |
cc767c2b SL |
229 | ret = ioctl(fd, FIOCLEX, STBNULL); |
230 | #endif | |
231 | if (ret) | |
232 | DEBUG(2, "CAN'T FIOCLEX %d\n", fd); | |
233 | } |