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