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 | 28 | |
4f6325c3 AT |
29 | static void logit(int priority, char *buf) |
30 | { | |
31 | if (logfile) { | |
67ea0d48 | 32 | fprintf(logfile,"%s [%d] %s", |
f7632fc6 | 33 | timestring(time(NULL)), (int)getpid(), buf); |
4f6325c3 AT |
34 | fflush(logfile); |
35 | } else { | |
36 | syslog(priority, "%s", buf); | |
37 | } | |
38 | } | |
e42c9458 | 39 | |
1a016bfd AT |
40 | void log_open(void) |
41 | { | |
42 | static int initialised; | |
43 | int options = LOG_PID; | |
bcf5b133 | 44 | time_t t; |
27d3cdbc | 45 | char *logf; |
1a016bfd AT |
46 | |
47 | if (initialised) return; | |
48 | initialised = 1; | |
49 | ||
958f3735 AT |
50 | /* this looks pointless, but it is needed in order for the |
51 | C library on some systems to fetch the timezone info | |
52 | before the chroot */ | |
53 | t = time(NULL); | |
54 | localtime(&t); | |
55 | ||
56 | /* optionally use a log file instead of syslog */ | |
27d3cdbc AT |
57 | logf = lp_log_file(); |
58 | if (logf && *logf) { | |
e8030905 AT |
59 | extern int orig_umask; |
60 | int old_umask = umask(022 | orig_umask); | |
27d3cdbc | 61 | logfile = fopen(logf, "a"); |
6265551a | 62 | umask(old_umask); |
4f6325c3 AT |
63 | return; |
64 | } | |
65 | ||
1a016bfd AT |
66 | #ifdef LOG_NDELAY |
67 | options |= LOG_NDELAY; | |
68 | #endif | |
69 | ||
70 | #ifdef LOG_DAEMON | |
71 | openlog("rsyncd", options, lp_syslog_facility()); | |
72 | #else | |
73 | openlog("rsyncd", options); | |
74 | #endif | |
75 | ||
76 | #ifndef LOG_NDELAY | |
4f6325c3 | 77 | logit(LOG_INFO,"rsyncd started\n"); |
1a016bfd AT |
78 | #endif |
79 | } | |
80 | ||
81 | ||
e08bfe12 AT |
82 | /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */ |
83 | void rprintf(int fd, const char *format, ...) | |
0b76cd63 AT |
84 | { |
85 | va_list ap; | |
86 | char buf[1024]; | |
87 | int len; | |
88 | FILE *f=NULL; | |
89 | extern int am_daemon; | |
b86f0cef | 90 | extern int quiet; |
8d9dc9f9 | 91 | /* recursion can happen with certain fatal conditions */ |
8d9dc9f9 | 92 | |
b86f0cef DD |
93 | if (quiet != 0 && fd == FINFO) return; |
94 | ||
0b76cd63 | 95 | va_start(ap, format); |
37f9805d | 96 | len = vslprintf(buf, sizeof(buf), format, ap); |
0b76cd63 AT |
97 | va_end(ap); |
98 | ||
65417579 | 99 | if (len < 0) exit_cleanup(RERR_MESSAGEIO); |
0b76cd63 | 100 | |
65417579 | 101 | if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO); |
45ccc5c0 | 102 | |
ff8b29b8 AT |
103 | buf[len] = 0; |
104 | ||
11a5a3c7 AT |
105 | if (fd == FLOG) { |
106 | if (am_daemon) logit(LOG_INFO, buf); | |
11a5a3c7 AT |
107 | return; |
108 | } | |
109 | ||
ff8b29b8 | 110 | if (am_daemon) { |
b24203b3 | 111 | static int depth; |
ff8b29b8 AT |
112 | int priority = LOG_INFO; |
113 | if (fd == FERROR) priority = LOG_WARNING; | |
45ccc5c0 | 114 | |
b24203b3 AT |
115 | if (depth) return; |
116 | ||
f27b53f5 AT |
117 | depth++; |
118 | ||
1a016bfd | 119 | log_open(); |
8d9dc9f9 | 120 | if (!io_multiplex_write(fd, buf, strlen(buf))) { |
4f6325c3 | 121 | logit(priority, buf); |
8d9dc9f9 AT |
122 | } |
123 | ||
124 | depth--; | |
ff8b29b8 | 125 | return; |
0b76cd63 AT |
126 | } |
127 | ||
ff8b29b8 AT |
128 | if (fd == FERROR) { |
129 | f = stderr; | |
130 | } | |
131 | ||
132 | if (fd == FINFO) { | |
133 | extern int am_server; | |
134 | if (am_server) | |
135 | f = stderr; | |
136 | else | |
137 | f = stdout; | |
138 | } | |
139 | ||
65417579 | 140 | if (!f) exit_cleanup(RERR_MESSAGEIO); |
0b76cd63 | 141 | |
65417579 | 142 | if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO); |
8d9dc9f9 | 143 | |
bcf5b133 | 144 | if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f); |
0b76cd63 AT |
145 | } |
146 | ||
147 | void rflush(int fd) | |
148 | { | |
149 | FILE *f = NULL; | |
150 | extern int am_daemon; | |
151 | ||
152 | if (am_daemon) { | |
153 | return; | |
154 | } | |
155 | ||
e08bfe12 AT |
156 | if (fd == FLOG) { |
157 | return; | |
158 | } | |
159 | ||
0b76cd63 AT |
160 | if (fd == FERROR) { |
161 | f = stderr; | |
162 | } | |
163 | ||
164 | if (fd == FINFO) { | |
165 | extern int am_server; | |
166 | if (am_server) | |
167 | f = stderr; | |
168 | else | |
169 | f = stdout; | |
170 | } | |
171 | ||
65417579 | 172 | if (!f) exit_cleanup(RERR_MESSAGEIO); |
0b76cd63 AT |
173 | fflush(f); |
174 | } | |
175 | ||
11a5a3c7 | 176 | |
e08bfe12 AT |
177 | |
178 | /* a generic logging routine for send/recv, with parameter | |
179 | substitiution */ | |
b6062654 AT |
180 | static void log_formatted(int fd, |
181 | char *format, char *op, struct file_struct *file, | |
1b7c47cb | 182 | struct stats *initial_stats) |
e08bfe12 AT |
183 | { |
184 | extern int module_id; | |
97cb8dc2 | 185 | extern char *auth_user; |
e08bfe12 | 186 | char buf[1024]; |
ab7104da | 187 | char buf2[1024]; |
e08bfe12 | 188 | char *p, *s, *n; |
e08bfe12 | 189 | int l; |
1b7c47cb AT |
190 | extern struct stats stats; |
191 | extern int am_sender; | |
af77cc6b | 192 | extern int am_daemon; |
1b7c47cb | 193 | int64 b; |
e08bfe12 | 194 | |
37f9805d | 195 | strlcpy(buf, format, sizeof(buf)); |
e08bfe12 AT |
196 | |
197 | for (s=&buf[0]; | |
198 | s && (p=strchr(s,'%')); ) { | |
199 | n = NULL; | |
200 | s = p + 1; | |
201 | ||
202 | switch (p[1]) { | |
af77cc6b AT |
203 | case 'h': if (am_daemon) n = client_name(0); break; |
204 | case 'a': if (am_daemon) n = client_addr(0); break; | |
e08bfe12 | 205 | case 'l': |
37f9805d | 206 | slprintf(buf2,sizeof(buf2),"%.0f", |
e08bfe12 AT |
207 | (double)file->length); |
208 | n = buf2; | |
209 | break; | |
210 | case 'p': | |
37f9805d | 211 | slprintf(buf2,sizeof(buf2),"%d", |
e08bfe12 AT |
212 | (int)getpid()); |
213 | n = buf2; | |
214 | break; | |
215 | case 'o': n = op; break; | |
ab7104da | 216 | case 'f': |
37f9805d | 217 | slprintf(buf2, sizeof(buf2), "%s/%s", |
ab7104da AT |
218 | file->basedir?file->basedir:"", |
219 | f_name(file)); | |
220 | clean_fname(buf2); | |
221 | n = buf2; | |
b6062654 | 222 | if (*n == '/') n++; |
ab7104da | 223 | break; |
97cb8dc2 | 224 | case 'm': n = lp_name(module_id); break; |
b6062654 | 225 | case 't': n = timestring(time(NULL)); break; |
97cb8dc2 AT |
226 | case 'P': n = lp_path(module_id); break; |
227 | case 'u': n = auth_user; break; | |
1b7c47cb AT |
228 | case 'b': |
229 | if (am_sender) { | |
230 | b = stats.total_written - | |
231 | initial_stats->total_written; | |
232 | } else { | |
233 | b = stats.total_read - | |
234 | initial_stats->total_read; | |
235 | } | |
37f9805d | 236 | slprintf(buf2,sizeof(buf2),"%.0f", (double)b); |
1b7c47cb AT |
237 | n = buf2; |
238 | break; | |
239 | case 'c': | |
240 | if (!am_sender) { | |
241 | b = stats.total_written - | |
242 | initial_stats->total_written; | |
243 | } else { | |
244 | b = stats.total_read - | |
245 | initial_stats->total_read; | |
246 | } | |
37f9805d | 247 | slprintf(buf2,sizeof(buf2),"%.0f", (double)b); |
1b7c47cb AT |
248 | n = buf2; |
249 | break; | |
e08bfe12 AT |
250 | } |
251 | ||
252 | if (!n) continue; | |
253 | ||
254 | l = strlen(n); | |
255 | ||
256 | if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) { | |
257 | rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n", | |
258 | p[0]); | |
65417579 | 259 | exit_cleanup(RERR_MESSAGEIO); |
e08bfe12 AT |
260 | } |
261 | ||
262 | if (l != 2) { | |
34720097 | 263 | memmove(s+(l-1), s+1, strlen(s+1)+1); |
e08bfe12 AT |
264 | } |
265 | memcpy(p, n, l); | |
266 | ||
267 | s = p+l; | |
268 | } | |
269 | ||
b6062654 | 270 | rprintf(fd,"%s\n", buf); |
e08bfe12 AT |
271 | } |
272 | ||
11a5a3c7 | 273 | /* log the outgoing transfer of a file */ |
1b7c47cb | 274 | void log_send(struct file_struct *file, struct stats *initial_stats) |
11a5a3c7 AT |
275 | { |
276 | extern int module_id; | |
b6062654 AT |
277 | extern int am_server; |
278 | extern char *log_format; | |
279 | ||
11a5a3c7 | 280 | if (lp_transfer_logging(module_id)) { |
b6062654 AT |
281 | log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats); |
282 | } else if (log_format && !am_server) { | |
283 | log_formatted(FINFO, log_format, "send", file, initial_stats); | |
11a5a3c7 AT |
284 | } |
285 | } | |
286 | ||
287 | /* log the incoming transfer of a file */ | |
1b7c47cb | 288 | void log_recv(struct file_struct *file, struct stats *initial_stats) |
11a5a3c7 AT |
289 | { |
290 | extern int module_id; | |
b6062654 AT |
291 | extern int am_server; |
292 | extern char *log_format; | |
293 | ||
11a5a3c7 | 294 | if (lp_transfer_logging(module_id)) { |
117af102 | 295 | log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats); |
b6062654 | 296 | } else if (log_format && !am_server) { |
117af102 | 297 | log_formatted(FINFO, log_format, "recv", file, initial_stats); |
11a5a3c7 AT |
298 | } |
299 | } | |
300 | ||
9b73d1c0 | 301 | /* called when the transfer is interrupted for some reason */ |
a9766ef1 | 302 | void log_exit(int code, const char *file, int line) |
9b73d1c0 AT |
303 | { |
304 | if (code == 0) { | |
305 | extern struct stats stats; | |
67ea0d48 | 306 | rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n", |
9b73d1c0 AT |
307 | (double)stats.total_written, |
308 | (double)stats.total_read, | |
309 | (double)stats.total_size); | |
310 | } else { | |
a9766ef1 AT |
311 | rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n", |
312 | code, file, line); | |
9b73d1c0 AT |
313 | } |
314 | } | |
315 | ||
11a5a3c7 | 316 | /* log the incoming transfer of a file for interactive use, this |
b6062654 AT |
317 | will be called at the end where the client was run |
318 | ||
319 | it i called when a file starts to be transferred | |
320 | */ | |
a9766ef1 | 321 | void log_transfer(struct file_struct *file, const char *fname) |
11a5a3c7 AT |
322 | { |
323 | extern int verbose; | |
324 | ||
325 | if (!verbose) return; | |
326 | ||
327 | rprintf(FINFO,"%s\n", fname); | |
328 | } | |
9b73d1c0 | 329 |