Commit | Line | Data |
---|---|---|
0b76cd63 AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1998 | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | */ | |
18 | ||
19 | /* | |
20 | logging and utility functions | |
21 | ||
22 | tridge, May 1998 | |
23 | */ | |
24 | #include "rsync.h" | |
25 | ||
4f6325c3 AT |
26 | static FILE *logfile; |
27 | ||
e0414f42 AT |
28 | |
29 | /**************************************************************************** | |
30 | return the date and time as a string | |
31 | ****************************************************************************/ | |
32 | static char *timestring(void ) | |
33 | { | |
34 | static char TimeBuf[200]; | |
35 | time_t t = time(NULL); | |
36 | struct tm *tm = localtime(&t); | |
37 | ||
38 | #ifdef HAVE_STRFTIME | |
39 | strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm); | |
40 | #else | |
41 | strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf)-1); | |
42 | #endif | |
43 | ||
44 | if (TimeBuf[strlen(TimeBuf)-1] == '\n') { | |
35bdd146 | 45 | TimeBuf[strlen(TimeBuf)-1] = 0; |
e0414f42 AT |
46 | } |
47 | ||
48 | return(TimeBuf); | |
49 | } | |
50 | ||
4f6325c3 AT |
51 | static void logit(int priority, char *buf) |
52 | { | |
53 | if (logfile) { | |
67ea0d48 AT |
54 | fprintf(logfile,"%s [%d] %s", |
55 | timestring(), (int)getpid(), buf); | |
4f6325c3 AT |
56 | fflush(logfile); |
57 | } else { | |
58 | syslog(priority, "%s", buf); | |
59 | } | |
60 | } | |
e42c9458 | 61 | |
1a016bfd AT |
62 | void log_open(void) |
63 | { | |
64 | static int initialised; | |
65 | int options = LOG_PID; | |
bcf5b133 | 66 | time_t t; |
27d3cdbc | 67 | char *logf; |
1a016bfd AT |
68 | |
69 | if (initialised) return; | |
70 | initialised = 1; | |
71 | ||
958f3735 AT |
72 | /* this looks pointless, but it is needed in order for the |
73 | C library on some systems to fetch the timezone info | |
74 | before the chroot */ | |
75 | t = time(NULL); | |
76 | localtime(&t); | |
77 | ||
78 | /* optionally use a log file instead of syslog */ | |
27d3cdbc AT |
79 | logf = lp_log_file(); |
80 | if (logf && *logf) { | |
81 | logfile = fopen(logf, "a"); | |
4f6325c3 AT |
82 | return; |
83 | } | |
84 | ||
1a016bfd AT |
85 | #ifdef LOG_NDELAY |
86 | options |= LOG_NDELAY; | |
87 | #endif | |
88 | ||
89 | #ifdef LOG_DAEMON | |
90 | openlog("rsyncd", options, lp_syslog_facility()); | |
91 | #else | |
92 | openlog("rsyncd", options); | |
93 | #endif | |
94 | ||
95 | #ifndef LOG_NDELAY | |
4f6325c3 | 96 | logit(LOG_INFO,"rsyncd started\n"); |
1a016bfd AT |
97 | #endif |
98 | } | |
99 | ||
100 | ||
e08bfe12 AT |
101 | /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */ |
102 | void rprintf(int fd, const char *format, ...) | |
0b76cd63 AT |
103 | { |
104 | va_list ap; | |
105 | char buf[1024]; | |
106 | int len; | |
107 | FILE *f=NULL; | |
108 | extern int am_daemon; | |
8d9dc9f9 | 109 | /* recursion can happen with certain fatal conditions */ |
8d9dc9f9 | 110 | |
0b76cd63 | 111 | va_start(ap, format); |
e42c9458 | 112 | len = vslprintf(buf, sizeof(buf)-1, format, ap); |
0b76cd63 AT |
113 | va_end(ap); |
114 | ||
115 | if (len < 0) exit_cleanup(1); | |
116 | ||
45ccc5c0 AT |
117 | if (len > sizeof(buf)-1) exit_cleanup(1); |
118 | ||
ff8b29b8 AT |
119 | buf[len] = 0; |
120 | ||
11a5a3c7 AT |
121 | if (fd == FLOG) { |
122 | if (am_daemon) logit(LOG_INFO, buf); | |
11a5a3c7 AT |
123 | return; |
124 | } | |
125 | ||
ff8b29b8 | 126 | if (am_daemon) { |
b24203b3 | 127 | static int depth; |
ff8b29b8 AT |
128 | int priority = LOG_INFO; |
129 | if (fd == FERROR) priority = LOG_WARNING; | |
45ccc5c0 | 130 | |
b24203b3 AT |
131 | if (depth) return; |
132 | ||
f27b53f5 AT |
133 | depth++; |
134 | ||
1a016bfd | 135 | log_open(); |
8d9dc9f9 | 136 | if (!io_multiplex_write(fd, buf, strlen(buf))) { |
4f6325c3 | 137 | logit(priority, buf); |
8d9dc9f9 AT |
138 | } |
139 | ||
140 | depth--; | |
ff8b29b8 | 141 | return; |
0b76cd63 AT |
142 | } |
143 | ||
ff8b29b8 AT |
144 | if (fd == FERROR) { |
145 | f = stderr; | |
146 | } | |
147 | ||
148 | if (fd == FINFO) { | |
149 | extern int am_server; | |
150 | if (am_server) | |
151 | f = stderr; | |
152 | else | |
153 | f = stdout; | |
154 | } | |
155 | ||
0b76cd63 AT |
156 | if (!f) exit_cleanup(1); |
157 | ||
158 | if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1); | |
8d9dc9f9 | 159 | |
bcf5b133 | 160 | if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f); |
0b76cd63 AT |
161 | } |
162 | ||
163 | void rflush(int fd) | |
164 | { | |
165 | FILE *f = NULL; | |
166 | extern int am_daemon; | |
167 | ||
168 | if (am_daemon) { | |
169 | return; | |
170 | } | |
171 | ||
e08bfe12 AT |
172 | if (fd == FLOG) { |
173 | return; | |
174 | } | |
175 | ||
0b76cd63 AT |
176 | if (fd == FERROR) { |
177 | f = stderr; | |
178 | } | |
179 | ||
180 | if (fd == FINFO) { | |
181 | extern int am_server; | |
182 | if (am_server) | |
183 | f = stderr; | |
184 | else | |
185 | f = stdout; | |
186 | } | |
187 | ||
188 | if (!f) exit_cleanup(1); | |
189 | fflush(f); | |
190 | } | |
191 | ||
11a5a3c7 | 192 | |
e08bfe12 AT |
193 | |
194 | /* a generic logging routine for send/recv, with parameter | |
195 | substitiution */ | |
196 | static void log_formatted(char *op, struct file_struct *file) | |
197 | { | |
198 | extern int module_id; | |
97cb8dc2 | 199 | extern char *auth_user; |
e08bfe12 AT |
200 | char buf[1024]; |
201 | char *p, *s, *n; | |
202 | char buf2[100]; | |
203 | int l; | |
204 | ||
205 | strlcpy(buf, lp_log_format(module_id), sizeof(buf)-1); | |
206 | ||
207 | for (s=&buf[0]; | |
208 | s && (p=strchr(s,'%')); ) { | |
209 | n = NULL; | |
210 | s = p + 1; | |
211 | ||
212 | switch (p[1]) { | |
213 | case 'h': n = client_name(0); break; | |
214 | case 'a': n = client_addr(0); break; | |
215 | case 'l': | |
216 | slprintf(buf2,sizeof(buf2)-1,"%.0f", | |
217 | (double)file->length); | |
218 | n = buf2; | |
219 | break; | |
220 | case 'p': | |
221 | slprintf(buf2,sizeof(buf2)-1,"%d", | |
222 | (int)getpid()); | |
223 | n = buf2; | |
224 | break; | |
225 | case 'o': n = op; break; | |
226 | case 'f': n = f_name(file); break; | |
97cb8dc2 AT |
227 | case 'm': n = lp_name(module_id); break; |
228 | case 'P': n = lp_path(module_id); break; | |
229 | case 'u': n = auth_user; break; | |
e08bfe12 AT |
230 | } |
231 | ||
232 | if (!n) continue; | |
233 | ||
234 | l = strlen(n); | |
235 | ||
236 | if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) { | |
237 | rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n", | |
238 | p[0]); | |
239 | exit_cleanup(1); | |
240 | } | |
241 | ||
242 | if (l != 2) { | |
34720097 | 243 | memmove(s+(l-1), s+1, strlen(s+1)+1); |
e08bfe12 AT |
244 | } |
245 | memcpy(p, n, l); | |
246 | ||
247 | s = p+l; | |
248 | } | |
249 | ||
250 | rprintf(FLOG,"%s\n", buf); | |
251 | } | |
252 | ||
11a5a3c7 AT |
253 | /* log the outgoing transfer of a file */ |
254 | void log_send(struct file_struct *file) | |
255 | { | |
256 | extern int module_id; | |
257 | if (lp_transfer_logging(module_id)) { | |
e08bfe12 | 258 | log_formatted("send", file); |
11a5a3c7 AT |
259 | } |
260 | } | |
261 | ||
262 | /* log the incoming transfer of a file */ | |
263 | void log_recv(struct file_struct *file) | |
264 | { | |
265 | extern int module_id; | |
266 | if (lp_transfer_logging(module_id)) { | |
e08bfe12 | 267 | log_formatted("recv", file); |
11a5a3c7 AT |
268 | } |
269 | } | |
270 | ||
9b73d1c0 AT |
271 | /* called when the transfer is interrupted for some reason */ |
272 | void log_exit(int code) | |
273 | { | |
274 | if (code == 0) { | |
275 | extern struct stats stats; | |
67ea0d48 | 276 | rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n", |
9b73d1c0 AT |
277 | (double)stats.total_written, |
278 | (double)stats.total_read, | |
279 | (double)stats.total_size); | |
280 | } else { | |
67ea0d48 | 281 | rprintf(FLOG,"transfer interrupted\n"); |
9b73d1c0 AT |
282 | } |
283 | } | |
284 | ||
11a5a3c7 AT |
285 | /* log the incoming transfer of a file for interactive use, this |
286 | will be called at the end where the client was run */ | |
287 | void log_transfer(struct file_struct *file, char *fname) | |
288 | { | |
289 | extern int verbose; | |
290 | ||
291 | if (!verbose) return; | |
292 | ||
293 | rprintf(FINFO,"%s\n", fname); | |
294 | } | |
9b73d1c0 | 295 |