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