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