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; | |
8d9dc9f9 | 90 | /* recursion can happen with certain fatal conditions */ |
8d9dc9f9 | 91 | |
0b76cd63 | 92 | va_start(ap, format); |
e42c9458 | 93 | len = vslprintf(buf, sizeof(buf)-1, format, ap); |
0b76cd63 AT |
94 | va_end(ap); |
95 | ||
96 | if (len < 0) exit_cleanup(1); | |
97 | ||
45ccc5c0 AT |
98 | if (len > sizeof(buf)-1) exit_cleanup(1); |
99 | ||
ff8b29b8 AT |
100 | buf[len] = 0; |
101 | ||
11a5a3c7 AT |
102 | if (fd == FLOG) { |
103 | if (am_daemon) logit(LOG_INFO, buf); | |
11a5a3c7 AT |
104 | return; |
105 | } | |
106 | ||
ff8b29b8 | 107 | if (am_daemon) { |
b24203b3 | 108 | static int depth; |
ff8b29b8 AT |
109 | int priority = LOG_INFO; |
110 | if (fd == FERROR) priority = LOG_WARNING; | |
45ccc5c0 | 111 | |
b24203b3 AT |
112 | if (depth) return; |
113 | ||
f27b53f5 AT |
114 | depth++; |
115 | ||
1a016bfd | 116 | log_open(); |
8d9dc9f9 | 117 | if (!io_multiplex_write(fd, buf, strlen(buf))) { |
4f6325c3 | 118 | logit(priority, buf); |
8d9dc9f9 AT |
119 | } |
120 | ||
121 | depth--; | |
ff8b29b8 | 122 | return; |
0b76cd63 AT |
123 | } |
124 | ||
ff8b29b8 AT |
125 | if (fd == FERROR) { |
126 | f = stderr; | |
127 | } | |
128 | ||
129 | if (fd == FINFO) { | |
130 | extern int am_server; | |
131 | if (am_server) | |
132 | f = stderr; | |
133 | else | |
134 | f = stdout; | |
135 | } | |
136 | ||
0b76cd63 AT |
137 | if (!f) exit_cleanup(1); |
138 | ||
139 | if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1); | |
8d9dc9f9 | 140 | |
bcf5b133 | 141 | if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f); |
0b76cd63 AT |
142 | } |
143 | ||
144 | void rflush(int fd) | |
145 | { | |
146 | FILE *f = NULL; | |
147 | extern int am_daemon; | |
148 | ||
149 | if (am_daemon) { | |
150 | return; | |
151 | } | |
152 | ||
e08bfe12 AT |
153 | if (fd == FLOG) { |
154 | return; | |
155 | } | |
156 | ||
0b76cd63 AT |
157 | if (fd == FERROR) { |
158 | f = stderr; | |
159 | } | |
160 | ||
161 | if (fd == FINFO) { | |
162 | extern int am_server; | |
163 | if (am_server) | |
164 | f = stderr; | |
165 | else | |
166 | f = stdout; | |
167 | } | |
168 | ||
169 | if (!f) exit_cleanup(1); | |
170 | fflush(f); | |
171 | } | |
172 | ||
11a5a3c7 | 173 | |
e08bfe12 AT |
174 | |
175 | /* a generic logging routine for send/recv, with parameter | |
176 | substitiution */ | |
1b7c47cb AT |
177 | static void log_formatted(char *op, struct file_struct *file, |
178 | struct stats *initial_stats) | |
e08bfe12 AT |
179 | { |
180 | extern int module_id; | |
97cb8dc2 | 181 | extern char *auth_user; |
e08bfe12 AT |
182 | char buf[1024]; |
183 | char *p, *s, *n; | |
184 | char buf2[100]; | |
185 | int l; | |
1b7c47cb AT |
186 | extern struct stats stats; |
187 | extern int am_sender; | |
188 | int64 b; | |
e08bfe12 AT |
189 | |
190 | strlcpy(buf, lp_log_format(module_id), sizeof(buf)-1); | |
191 | ||
192 | for (s=&buf[0]; | |
193 | s && (p=strchr(s,'%')); ) { | |
194 | n = NULL; | |
195 | s = p + 1; | |
196 | ||
197 | switch (p[1]) { | |
198 | case 'h': n = client_name(0); break; | |
199 | case 'a': n = client_addr(0); break; | |
200 | case 'l': | |
201 | slprintf(buf2,sizeof(buf2)-1,"%.0f", | |
202 | (double)file->length); | |
203 | n = buf2; | |
204 | break; | |
205 | case 'p': | |
206 | slprintf(buf2,sizeof(buf2)-1,"%d", | |
207 | (int)getpid()); | |
208 | n = buf2; | |
209 | break; | |
210 | case 'o': n = op; break; | |
211 | case 'f': n = f_name(file); break; | |
97cb8dc2 AT |
212 | case 'm': n = lp_name(module_id); break; |
213 | case 'P': n = lp_path(module_id); break; | |
214 | case 'u': n = auth_user; break; | |
1b7c47cb AT |
215 | case 'b': |
216 | if (am_sender) { | |
217 | b = stats.total_written - | |
218 | initial_stats->total_written; | |
219 | } else { | |
220 | b = stats.total_read - | |
221 | initial_stats->total_read; | |
222 | } | |
223 | slprintf(buf2,sizeof(buf2)-1,"%.0f", (double)b); | |
224 | n = buf2; | |
225 | break; | |
226 | case 'c': | |
227 | if (!am_sender) { | |
228 | b = stats.total_written - | |
229 | initial_stats->total_written; | |
230 | } else { | |
231 | b = stats.total_read - | |
232 | initial_stats->total_read; | |
233 | } | |
234 | slprintf(buf2,sizeof(buf2)-1,"%.0f", (double)b); | |
235 | n = buf2; | |
236 | break; | |
e08bfe12 AT |
237 | } |
238 | ||
239 | if (!n) continue; | |
240 | ||
241 | l = strlen(n); | |
242 | ||
243 | if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) { | |
244 | rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n", | |
245 | p[0]); | |
246 | exit_cleanup(1); | |
247 | } | |
248 | ||
249 | if (l != 2) { | |
34720097 | 250 | memmove(s+(l-1), s+1, strlen(s+1)+1); |
e08bfe12 AT |
251 | } |
252 | memcpy(p, n, l); | |
253 | ||
254 | s = p+l; | |
255 | } | |
256 | ||
257 | rprintf(FLOG,"%s\n", buf); | |
258 | } | |
259 | ||
11a5a3c7 | 260 | /* log the outgoing transfer of a file */ |
1b7c47cb | 261 | void log_send(struct file_struct *file, struct stats *initial_stats) |
11a5a3c7 AT |
262 | { |
263 | extern int module_id; | |
264 | if (lp_transfer_logging(module_id)) { | |
1b7c47cb | 265 | log_formatted("send", file, initial_stats); |
11a5a3c7 AT |
266 | } |
267 | } | |
268 | ||
269 | /* log the incoming transfer of a file */ | |
1b7c47cb | 270 | void log_recv(struct file_struct *file, struct stats *initial_stats) |
11a5a3c7 AT |
271 | { |
272 | extern int module_id; | |
273 | if (lp_transfer_logging(module_id)) { | |
1b7c47cb | 274 | log_formatted("recv", file, initial_stats); |
11a5a3c7 AT |
275 | } |
276 | } | |
277 | ||
9b73d1c0 AT |
278 | /* called when the transfer is interrupted for some reason */ |
279 | void log_exit(int code) | |
280 | { | |
281 | if (code == 0) { | |
282 | extern struct stats stats; | |
67ea0d48 | 283 | rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n", |
9b73d1c0 AT |
284 | (double)stats.total_written, |
285 | (double)stats.total_read, | |
286 | (double)stats.total_size); | |
287 | } else { | |
67ea0d48 | 288 | rprintf(FLOG,"transfer interrupted\n"); |
9b73d1c0 AT |
289 | } |
290 | } | |
291 | ||
11a5a3c7 AT |
292 | /* log the incoming transfer of a file for interactive use, this |
293 | will be called at the end where the client was run */ | |
294 | void log_transfer(struct file_struct *file, char *fname) | |
295 | { | |
296 | extern int verbose; | |
297 | ||
298 | if (!verbose) return; | |
299 | ||
300 | rprintf(FINFO,"%s\n", fname); | |
301 | } | |
9b73d1c0 | 302 |