| 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 | |
| 26 | static FILE *logfile; |
| 27 | static int log_error_fd = -1; |
| 28 | |
| 29 | static void logit(int priority, char *buf) |
| 30 | { |
| 31 | if (logfile) { |
| 32 | fprintf(logfile,"%s [%d] %s", |
| 33 | timestring(time(NULL)), (int)getpid(), buf); |
| 34 | fflush(logfile); |
| 35 | } else { |
| 36 | syslog(priority, "%s", buf); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void log_open(void) |
| 41 | { |
| 42 | static int initialised; |
| 43 | int options = LOG_PID; |
| 44 | time_t t; |
| 45 | char *logf; |
| 46 | |
| 47 | if (initialised) return; |
| 48 | initialised = 1; |
| 49 | |
| 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 */ |
| 57 | logf = lp_log_file(); |
| 58 | if (logf && *logf) { |
| 59 | extern int orig_umask; |
| 60 | int old_umask = umask(022 | orig_umask); |
| 61 | logfile = fopen(logf, "a"); |
| 62 | umask(old_umask); |
| 63 | return; |
| 64 | } |
| 65 | |
| 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 |
| 77 | logit(LOG_INFO,"rsyncd started\n"); |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | /* setup the error file descriptor - used when we are a server |
| 82 | that is receiving files */ |
| 83 | void set_error_fd(int fd) |
| 84 | { |
| 85 | log_error_fd = fd; |
| 86 | } |
| 87 | |
| 88 | /* this is the underlying (unformatted) rsync debugging function. Call |
| 89 | it with FINFO, FERROR or FLOG */ |
| 90 | void rwrite(int fd, char *buf, int len) |
| 91 | { |
| 92 | FILE *f=NULL; |
| 93 | extern int am_daemon; |
| 94 | extern int quiet; |
| 95 | /* recursion can happen with certain fatal conditions */ |
| 96 | |
| 97 | if (quiet != 0 && fd == FINFO) return; |
| 98 | |
| 99 | if (len < 0) exit_cleanup(RERR_MESSAGEIO); |
| 100 | |
| 101 | buf[len] = 0; |
| 102 | |
| 103 | if (fd == FLOG) { |
| 104 | if (am_daemon) logit(LOG_INFO, buf); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if (am_daemon) { |
| 109 | static int depth; |
| 110 | int priority = LOG_INFO; |
| 111 | if (fd == FERROR) priority = LOG_WARNING; |
| 112 | |
| 113 | if (depth) return; |
| 114 | |
| 115 | depth++; |
| 116 | |
| 117 | log_open(); |
| 118 | |
| 119 | if (!io_error_write(log_error_fd, buf, strlen(buf)) && |
| 120 | !io_multiplex_write(fd, buf, strlen(buf))) { |
| 121 | logit(priority, buf); |
| 122 | } |
| 123 | |
| 124 | depth--; |
| 125 | return; |
| 126 | } |
| 127 | |
| 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 | |
| 140 | if (!f) exit_cleanup(RERR_MESSAGEIO); |
| 141 | |
| 142 | if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO); |
| 143 | |
| 144 | if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */ |
| 149 | void rprintf(int fd, const char *format, ...) |
| 150 | { |
| 151 | va_list ap; |
| 152 | char buf[1024]; |
| 153 | int len; |
| 154 | |
| 155 | va_start(ap, format); |
| 156 | len = vslprintf(buf, sizeof(buf), format, ap); |
| 157 | va_end(ap); |
| 158 | |
| 159 | if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO); |
| 160 | |
| 161 | rwrite(fd, buf, len); |
| 162 | } |
| 163 | |
| 164 | void rflush(int fd) |
| 165 | { |
| 166 | FILE *f = NULL; |
| 167 | extern int am_daemon; |
| 168 | |
| 169 | if (am_daemon) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (fd == FLOG) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if (fd == FERROR) { |
| 178 | f = stderr; |
| 179 | } |
| 180 | |
| 181 | if (fd == FINFO) { |
| 182 | extern int am_server; |
| 183 | if (am_server) |
| 184 | f = stderr; |
| 185 | else |
| 186 | f = stdout; |
| 187 | } |
| 188 | |
| 189 | if (!f) exit_cleanup(RERR_MESSAGEIO); |
| 190 | fflush(f); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | /* a generic logging routine for send/recv, with parameter |
| 196 | substitiution */ |
| 197 | static void log_formatted(int fd, |
| 198 | char *format, char *op, struct file_struct *file, |
| 199 | struct stats *initial_stats) |
| 200 | { |
| 201 | extern int module_id; |
| 202 | extern char *auth_user; |
| 203 | char buf[1024]; |
| 204 | char buf2[1024]; |
| 205 | char *p, *s, *n; |
| 206 | int l; |
| 207 | extern struct stats stats; |
| 208 | extern int am_sender; |
| 209 | extern int am_daemon; |
| 210 | int64 b; |
| 211 | |
| 212 | strlcpy(buf, format, sizeof(buf)); |
| 213 | |
| 214 | for (s=&buf[0]; |
| 215 | s && (p=strchr(s,'%')); ) { |
| 216 | n = NULL; |
| 217 | s = p + 1; |
| 218 | |
| 219 | switch (p[1]) { |
| 220 | case 'h': if (am_daemon) n = client_name(0); break; |
| 221 | case 'a': if (am_daemon) n = client_addr(0); break; |
| 222 | case 'l': |
| 223 | slprintf(buf2,sizeof(buf2),"%.0f", |
| 224 | (double)file->length); |
| 225 | n = buf2; |
| 226 | break; |
| 227 | case 'p': |
| 228 | slprintf(buf2,sizeof(buf2),"%d", |
| 229 | (int)getpid()); |
| 230 | n = buf2; |
| 231 | break; |
| 232 | case 'o': n = op; break; |
| 233 | case 'f': |
| 234 | slprintf(buf2, sizeof(buf2), "%s/%s", |
| 235 | file->basedir?file->basedir:"", |
| 236 | f_name(file)); |
| 237 | clean_fname(buf2); |
| 238 | n = buf2; |
| 239 | if (*n == '/') n++; |
| 240 | break; |
| 241 | case 'm': n = lp_name(module_id); break; |
| 242 | case 't': n = timestring(time(NULL)); break; |
| 243 | case 'P': n = lp_path(module_id); break; |
| 244 | case 'u': n = auth_user; break; |
| 245 | case 'b': |
| 246 | if (am_sender) { |
| 247 | b = stats.total_written - |
| 248 | initial_stats->total_written; |
| 249 | } else { |
| 250 | b = stats.total_read - |
| 251 | initial_stats->total_read; |
| 252 | } |
| 253 | slprintf(buf2,sizeof(buf2),"%.0f", (double)b); |
| 254 | n = buf2; |
| 255 | break; |
| 256 | case 'c': |
| 257 | if (!am_sender) { |
| 258 | b = stats.total_written - |
| 259 | initial_stats->total_written; |
| 260 | } else { |
| 261 | b = stats.total_read - |
| 262 | initial_stats->total_read; |
| 263 | } |
| 264 | slprintf(buf2,sizeof(buf2),"%.0f", (double)b); |
| 265 | n = buf2; |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | if (!n) continue; |
| 270 | |
| 271 | l = strlen(n); |
| 272 | |
| 273 | if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) { |
| 274 | rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n", |
| 275 | p[0]); |
| 276 | exit_cleanup(RERR_MESSAGEIO); |
| 277 | } |
| 278 | |
| 279 | if (l != 2) { |
| 280 | memmove(s+(l-1), s+1, strlen(s+1)+1); |
| 281 | } |
| 282 | memcpy(p, n, l); |
| 283 | |
| 284 | s = p+l; |
| 285 | } |
| 286 | |
| 287 | rprintf(fd,"%s\n", buf); |
| 288 | } |
| 289 | |
| 290 | /* log the outgoing transfer of a file */ |
| 291 | void log_send(struct file_struct *file, struct stats *initial_stats) |
| 292 | { |
| 293 | extern int module_id; |
| 294 | extern int am_server; |
| 295 | extern char *log_format; |
| 296 | |
| 297 | if (lp_transfer_logging(module_id)) { |
| 298 | log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats); |
| 299 | } else if (log_format && !am_server) { |
| 300 | log_formatted(FINFO, log_format, "send", file, initial_stats); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /* log the incoming transfer of a file */ |
| 305 | void log_recv(struct file_struct *file, struct stats *initial_stats) |
| 306 | { |
| 307 | extern int module_id; |
| 308 | extern int am_server; |
| 309 | extern char *log_format; |
| 310 | |
| 311 | if (lp_transfer_logging(module_id)) { |
| 312 | log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats); |
| 313 | } else if (log_format && !am_server) { |
| 314 | log_formatted(FINFO, log_format, "recv", file, initial_stats); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* called when the transfer is interrupted for some reason */ |
| 319 | void log_exit(int code, const char *file, int line) |
| 320 | { |
| 321 | if (code == 0) { |
| 322 | extern struct stats stats; |
| 323 | rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n", |
| 324 | (double)stats.total_written, |
| 325 | (double)stats.total_read, |
| 326 | (double)stats.total_size); |
| 327 | } else { |
| 328 | rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n", |
| 329 | code, file, line); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* log the incoming transfer of a file for interactive use, this |
| 334 | will be called at the end where the client was run |
| 335 | |
| 336 | it i called when a file starts to be transferred |
| 337 | */ |
| 338 | void log_transfer(struct file_struct *file, const char *fname) |
| 339 | { |
| 340 | extern int verbose; |
| 341 | |
| 342 | if (!verbose) return; |
| 343 | |
| 344 | rprintf(FINFO,"%s\n", fname); |
| 345 | } |
| 346 | |