| Martin Pool | a039749 | 2000-10-26 07:24:18 +0000 | [diff] [blame] | 1 | /* -*- c-file-style: "linux"; -*- |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 2 | |
| Martin Pool | 0c5a792 | 2001-03-19 08:19:11 +0000 | [diff] [blame] | 3 | Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org> |
| Martin Pool | 18c71e9 | 2001-02-23 01:45:46 +0000 | [diff] [blame] | 4 | Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org> |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 5 | |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 10 | |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 15 | |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| Martin Pool | 3e3dcd6 | 2001-02-21 07:18:50 +0000 | [diff] [blame] | 22 | Logging and utility functions. |
| Martin Pool | 0c5a792 | 2001-03-19 08:19:11 +0000 | [diff] [blame] | 23 | tridge, May 1998 |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 24 | |
| Martin Pool | 3e3dcd6 | 2001-02-21 07:18:50 +0000 | [diff] [blame] | 25 | Mapping to human-readable messages added by Martin Pool |
| 26 | <mbp@samba.org>, Oct 2000. |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 27 | */ |
| 28 | #include "rsync.h" |
| 29 | |
| Wayne Davison | 548abf9 | 2004-01-13 04:50:45 +0000 | [diff] [blame] | 30 | extern int am_daemon; |
| 31 | extern int am_server; |
| 32 | extern int am_sender; |
| 33 | extern int quiet; |
| 34 | extern int module_id; |
| Wayne Davison | a644fc3 | 2004-01-15 07:42:19 +0000 | [diff] [blame] | 35 | extern int msg_fd_out; |
| Wayne Davison | 548abf9 | 2004-01-13 04:50:45 +0000 | [diff] [blame] | 36 | extern char *auth_user; |
| 37 | extern char *log_format; |
| 38 | |
| David Dykstra | 30e8c8e | 2002-08-30 23:27:26 +0000 | [diff] [blame] | 39 | static int log_initialised; |
| David Dykstra | 45a8354 | 2000-10-24 18:50:08 +0000 | [diff] [blame] | 40 | static char *logfname; |
| Andrew Tridgell | 4f6325c | 1998-09-09 05:51:42 +0000 | [diff] [blame] | 41 | static FILE *logfile; |
| Martin Pool | b35d0d8 | 2002-04-08 04:10:20 +0000 | [diff] [blame] | 42 | struct stats stats; |
| Andrew Tridgell | e0414f4 | 1998-09-09 06:06:08 +0000 | [diff] [blame] | 43 | |
| Wayne Davison | 8fcdc44 | 2004-05-15 19:31:16 +0000 | [diff] [blame] | 44 | int log_got_error = 0; |
| Martin Pool | af642a6 | 2000-11-10 03:28:15 +0000 | [diff] [blame] | 45 | |
| 46 | struct { |
| 47 | int code; |
| 48 | char const *name; |
| 49 | } const rerr_names[] = { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 50 | { RERR_SYNTAX , "syntax or usage error" }, |
| 51 | { RERR_PROTOCOL , "protocol incompatibility" }, |
| 52 | { RERR_FILESELECT , "errors selecting input/output files, dirs" }, |
| 53 | { RERR_UNSUPPORTED, "requested action not supported" }, |
| 54 | { RERR_STARTCLIENT, "error starting client-server protocol" }, |
| 55 | { RERR_SOCKETIO , "error in socket IO" }, |
| 56 | { RERR_FILEIO , "error in file IO" }, |
| 57 | { RERR_STREAMIO , "error in rsync protocol data stream" }, |
| 58 | { RERR_MESSAGEIO , "errors with program diagnostics" }, |
| 59 | { RERR_IPC , "error in IPC code" }, |
| 60 | { RERR_SIGNAL , "received SIGUSR1 or SIGINT" }, |
| 61 | { RERR_WAITCHILD , "some error returned by waitpid()" }, |
| 62 | { RERR_MALLOC , "error allocating core memory buffers" }, |
| 63 | { RERR_PARTIAL , "some files could not be transferred" }, |
| Wayne Davison | 584ba4e | 2004-09-05 21:30:00 +0000 | [diff] [blame^] | 64 | { RERR_VANISHED , "some files vanished before they could be transferred" }, |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 65 | { RERR_TIMEOUT , "timeout in data send/receive" }, |
| Andrew Tridgell | 19b27a4 | 2001-03-23 01:26:04 +0000 | [diff] [blame] | 66 | { RERR_CMD_FAILED , "remote shell failed" }, |
| 67 | { RERR_CMD_KILLED , "remote shell killed" }, |
| 68 | { RERR_CMD_RUN, "remote command could not be run" }, |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 69 | { RERR_CMD_NOTFOUND, "remote command not found" }, |
| 70 | { 0, NULL } |
| Martin Pool | af642a6 | 2000-11-10 03:28:15 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | |
| 74 | |
| 75 | /* |
| 76 | * Map from rsync error code to name, or return NULL. |
| 77 | */ |
| 78 | static char const *rerr_name(int code) |
| 79 | { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 80 | int i; |
| 81 | for (i = 0; rerr_names[i].name; i++) { |
| 82 | if (rerr_names[i].code == code) |
| 83 | return rerr_names[i].name; |
| 84 | } |
| 85 | return NULL; |
| Martin Pool | af642a6 | 2000-11-10 03:28:15 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| Andrew Tridgell | 4f6325c | 1998-09-09 05:51:42 +0000 | [diff] [blame] | 89 | static void logit(int priority, char *buf) |
| 90 | { |
| David Dykstra | 45a8354 | 2000-10-24 18:50:08 +0000 | [diff] [blame] | 91 | if (logfname) { |
| David Dykstra | 15b84e1 | 2000-10-25 19:57:42 +0000 | [diff] [blame] | 92 | if (!logfile) |
| 93 | log_open(); |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 94 | fprintf(logfile,"%s [%d] %s", |
| Andrew Tridgell | f7632fc | 1998-11-02 00:52:01 +0000 | [diff] [blame] | 95 | timestring(time(NULL)), (int)getpid(), buf); |
| Andrew Tridgell | 4f6325c | 1998-09-09 05:51:42 +0000 | [diff] [blame] | 96 | fflush(logfile); |
| 97 | } else { |
| 98 | syslog(priority, "%s", buf); |
| 99 | } |
| 100 | } |
| Andrew Tridgell | e42c945 | 1998-05-15 09:26:01 +0000 | [diff] [blame] | 101 | |
| David Dykstra | 45a8354 | 2000-10-24 18:50:08 +0000 | [diff] [blame] | 102 | void log_init(void) |
| Andrew Tridgell | 1a016bf | 1998-05-15 10:34:07 +0000 | [diff] [blame] | 103 | { |
| Andrew Tridgell | 1a016bf | 1998-05-15 10:34:07 +0000 | [diff] [blame] | 104 | int options = LOG_PID; |
| Andrew Tridgell | bcf5b13 | 1998-07-02 10:57:20 +0000 | [diff] [blame] | 105 | time_t t; |
| Andrew Tridgell | 1a016bf | 1998-05-15 10:34:07 +0000 | [diff] [blame] | 106 | |
| Wayne Davison | 0bb4d17 | 2004-06-07 22:51:14 +0000 | [diff] [blame] | 107 | if (log_initialised) |
| 108 | return; |
| David Dykstra | 30e8c8e | 2002-08-30 23:27:26 +0000 | [diff] [blame] | 109 | log_initialised = 1; |
| Andrew Tridgell | 1a016bf | 1998-05-15 10:34:07 +0000 | [diff] [blame] | 110 | |
| Andrew Tridgell | 958f373 | 1998-10-30 11:18:38 +0000 | [diff] [blame] | 111 | /* this looks pointless, but it is needed in order for the |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 112 | * C library on some systems to fetch the timezone info |
| 113 | * before the chroot */ |
| Andrew Tridgell | 958f373 | 1998-10-30 11:18:38 +0000 | [diff] [blame] | 114 | t = time(NULL); |
| 115 | localtime(&t); |
| 116 | |
| 117 | /* optionally use a log file instead of syslog */ |
| David Dykstra | 45a8354 | 2000-10-24 18:50:08 +0000 | [diff] [blame] | 118 | logfname = lp_log_file(); |
| 119 | if (logfname) { |
| David Dykstra | 15b84e1 | 2000-10-25 19:57:42 +0000 | [diff] [blame] | 120 | if (*logfname) { |
| 121 | log_open(); |
| David Dykstra | 45a8354 | 2000-10-24 18:50:08 +0000 | [diff] [blame] | 122 | return; |
| David Dykstra | 15b84e1 | 2000-10-25 19:57:42 +0000 | [diff] [blame] | 123 | } |
| David Dykstra | 45a8354 | 2000-10-24 18:50:08 +0000 | [diff] [blame] | 124 | logfname = NULL; |
| Andrew Tridgell | 4f6325c | 1998-09-09 05:51:42 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| Andrew Tridgell | 1a016bf | 1998-05-15 10:34:07 +0000 | [diff] [blame] | 127 | #ifdef LOG_NDELAY |
| 128 | options |= LOG_NDELAY; |
| 129 | #endif |
| 130 | |
| 131 | #ifdef LOG_DAEMON |
| 132 | openlog("rsyncd", options, lp_syslog_facility()); |
| 133 | #else |
| 134 | openlog("rsyncd", options); |
| 135 | #endif |
| 136 | |
| 137 | #ifndef LOG_NDELAY |
| Andrew Tridgell | 4f6325c | 1998-09-09 05:51:42 +0000 | [diff] [blame] | 138 | logit(LOG_INFO,"rsyncd started\n"); |
| Andrew Tridgell | 1a016bf | 1998-05-15 10:34:07 +0000 | [diff] [blame] | 139 | #endif |
| 140 | } |
| Andrew Tridgell | 1a016bf | 1998-05-15 10:34:07 +0000 | [diff] [blame] | 141 | |
| Jos Backus | 1e34e4b | 2002-12-24 07:42:04 +0000 | [diff] [blame] | 142 | void log_open(void) |
| David Dykstra | 15b84e1 | 2000-10-25 19:57:42 +0000 | [diff] [blame] | 143 | { |
| 144 | if (logfname && !logfile) { |
| 145 | extern int orig_umask; |
| 146 | int old_umask = umask(022 | orig_umask); |
| 147 | logfile = fopen(logfname, "a"); |
| 148 | umask(old_umask); |
| 149 | } |
| 150 | } |
| 151 | |
| Jos Backus | 1e34e4b | 2002-12-24 07:42:04 +0000 | [diff] [blame] | 152 | void log_close(void) |
| David Dykstra | 45a8354 | 2000-10-24 18:50:08 +0000 | [diff] [blame] | 153 | { |
| 154 | if (logfile) { |
| 155 | fclose(logfile); |
| 156 | logfile = NULL; |
| 157 | } |
| 158 | } |
| 159 | |
| Andrew Tridgell | 554e0a8 | 2000-01-23 07:36:56 +0000 | [diff] [blame] | 160 | /* this is the underlying (unformatted) rsync debugging function. Call |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 161 | * it with FINFO, FERROR or FLOG */ |
| Andrew Tridgell | ff41a59 | 2000-01-28 15:29:59 +0000 | [diff] [blame] | 162 | void rwrite(enum logcode code, char *buf, int len) |
| Andrew Tridgell | 554e0a8 | 2000-01-23 07:36:56 +0000 | [diff] [blame] | 163 | { |
| Wayne Davison | 8fcdc44 | 2004-05-15 19:31:16 +0000 | [diff] [blame] | 164 | FILE *f = NULL; |
| Andrew Tridgell | 8d9dc9f | 1998-05-18 08:20:26 +0000 | [diff] [blame] | 165 | /* recursion can happen with certain fatal conditions */ |
| Andrew Tridgell | 8d9dc9f | 1998-05-18 08:20:26 +0000 | [diff] [blame] | 166 | |
| Wayne Davison | a644fc3 | 2004-01-15 07:42:19 +0000 | [diff] [blame] | 167 | if (quiet && code == FINFO) |
| 168 | return; |
| David Dykstra | b86f0ce | 1999-02-09 17:25:35 +0000 | [diff] [blame] | 169 | |
| Wayne Davison | a644fc3 | 2004-01-15 07:42:19 +0000 | [diff] [blame] | 170 | if (len < 0) |
| 171 | exit_cleanup(RERR_MESSAGEIO); |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 172 | |
| Andrew Tridgell | ff8b29b | 1998-05-10 08:24:38 +0000 | [diff] [blame] | 173 | buf[len] = 0; |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 174 | |
| Wayne Davison | 0bb4d17 | 2004-06-07 22:51:14 +0000 | [diff] [blame] | 175 | if (am_server && msg_fd_out >= 0) { |
| 176 | /* Pass the message to our sibling. */ |
| 177 | send_msg((enum msgcode)code, buf, len); |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| Wayne Davison | 0bb4d17 | 2004-06-07 22:51:14 +0000 | [diff] [blame] | 181 | if (am_daemon) { |
| 182 | static int in_block; |
| 183 | char msg[2048]; |
| 184 | int priority = code == FERROR ? LOG_WARNING : LOG_INFO; |
| 185 | |
| 186 | if (in_block) |
| 187 | return; |
| 188 | in_block = 1; |
| 189 | if (!log_initialised) |
| 190 | log_init(); |
| 191 | strlcpy(msg, buf, MIN((int)sizeof msg, len + 1)); |
| 192 | logit(priority, msg); |
| 193 | in_block = 0; |
| 194 | |
| 195 | if (code == FLOG || !am_server) |
| 196 | return; |
| 197 | } else if (code == FLOG) |
| 198 | return; |
| 199 | |
| Wayne Davison | a644fc3 | 2004-01-15 07:42:19 +0000 | [diff] [blame] | 200 | if (am_server) { |
| Wayne Davison | 0bb4d17 | 2004-06-07 22:51:14 +0000 | [diff] [blame] | 201 | /* Pass the message to the non-server side. */ |
| 202 | if (io_multiplex_write((enum msgcode)code, buf, len)) |
| 203 | return; |
| 204 | if (am_daemon) { |
| 205 | /* TODO: can we send the error to the user somehow? */ |
| Wayne Davison | a644fc3 | 2004-01-15 07:42:19 +0000 | [diff] [blame] | 206 | return; |
| 207 | } |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| Andrew Tridgell | ff41a59 | 2000-01-28 15:29:59 +0000 | [diff] [blame] | 210 | if (code == FERROR) { |
| Andrew Tridgell | ff81e80 | 2001-03-22 07:36:51 +0000 | [diff] [blame] | 211 | log_got_error = 1; |
| Andrew Tridgell | ff8b29b | 1998-05-10 08:24:38 +0000 | [diff] [blame] | 212 | f = stderr; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 213 | } |
| Andrew Tridgell | ff8b29b | 1998-05-10 08:24:38 +0000 | [diff] [blame] | 214 | |
| Wayne Davison | 0bb4d17 | 2004-06-07 22:51:14 +0000 | [diff] [blame] | 215 | if (code == FINFO) |
| 216 | f = am_server ? stderr : stdout; |
| Andrew Tridgell | ff8b29b | 1998-05-10 08:24:38 +0000 | [diff] [blame] | 217 | |
| Wayne Davison | 0bb4d17 | 2004-06-07 22:51:14 +0000 | [diff] [blame] | 218 | if (!f) |
| 219 | exit_cleanup(RERR_MESSAGEIO); |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 220 | |
| Wayne Davison | 0bb4d17 | 2004-06-07 22:51:14 +0000 | [diff] [blame] | 221 | if (fwrite(buf, len, 1, f) != 1) |
| 222 | exit_cleanup(RERR_MESSAGEIO); |
| Andrew Tridgell | 8d9dc9f | 1998-05-18 08:20:26 +0000 | [diff] [blame] | 223 | |
| Wayne Davison | 0bb4d17 | 2004-06-07 22:51:14 +0000 | [diff] [blame] | 224 | if (buf[len-1] == '\r' || buf[len-1] == '\n') |
| 225 | fflush(f); |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 226 | } |
| Andrew Tridgell | 554e0a8 | 2000-01-23 07:36:56 +0000 | [diff] [blame] | 227 | |
| 228 | |
| Martin Pool | a039749 | 2000-10-26 07:24:18 +0000 | [diff] [blame] | 229 | /* This is the rsync debugging function. Call it with FINFO, FERROR or |
| 230 | * FLOG. */ |
| 231 | void rprintf(enum logcode code, const char *format, ...) |
| Andrew Tridgell | 554e0a8 | 2000-01-23 07:36:56 +0000 | [diff] [blame] | 232 | { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 233 | va_list ap; |
| Wayne Davison | 8fcdc44 | 2004-05-15 19:31:16 +0000 | [diff] [blame] | 234 | char buf[MAXPATHLEN+512]; |
| 235 | size_t len; |
| Andrew Tridgell | 554e0a8 | 2000-01-23 07:36:56 +0000 | [diff] [blame] | 236 | |
| 237 | va_start(ap, format); |
| Andrew Tridgell | 8950ac0 | 2001-05-07 06:59:37 +0000 | [diff] [blame] | 238 | len = vsnprintf(buf, sizeof(buf), format, ap); |
| Andrew Tridgell | 554e0a8 | 2000-01-23 07:36:56 +0000 | [diff] [blame] | 239 | va_end(ap); |
| 240 | |
| Martin Pool | b2f0246 | 2001-09-04 03:12:55 +0000 | [diff] [blame] | 241 | /* Deal with buffer overruns. Instead of panicking, just |
| Wayne Davison | 8fcdc44 | 2004-05-15 19:31:16 +0000 | [diff] [blame] | 242 | * truncate the resulting string. (Note that configure ensures |
| 243 | * that we have a vsnprintf() that doesn't ever return -1.) */ |
| 244 | if (len > sizeof buf - 1) { |
| Martin Pool | b2f0246 | 2001-09-04 03:12:55 +0000 | [diff] [blame] | 245 | const char ellipsis[] = "[...]"; |
| 246 | |
| 247 | /* Reset length, and zero-terminate the end of our buffer */ |
| 248 | len = sizeof(buf)-1; |
| 249 | buf[len] = '\0'; |
| 250 | |
| 251 | /* Copy the ellipsis to the end of the string, but give |
| 252 | * us one extra character: |
| 253 | * |
| 254 | * v--- null byte at buf[sizeof(buf)-1] |
| 255 | * abcdefghij0 |
| 256 | * -> abcd[...]00 <-- now two null bytes at end |
| 257 | * |
| 258 | * If the input format string has a trailing newline, |
| 259 | * we copy it into that extra null; if it doesn't, well, |
| 260 | * all we lose is one byte. */ |
| 261 | strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis)); |
| 262 | if (format[strlen(format)-1] == '\n') { |
| 263 | buf[len-1] = '\n'; |
| 264 | } |
| 265 | } |
| Andrew Tridgell | 554e0a8 | 2000-01-23 07:36:56 +0000 | [diff] [blame] | 266 | |
| Andrew Tridgell | ff41a59 | 2000-01-28 15:29:59 +0000 | [diff] [blame] | 267 | rwrite(code, buf, len); |
| Andrew Tridgell | 554e0a8 | 2000-01-23 07:36:56 +0000 | [diff] [blame] | 268 | } |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 269 | |
| Martin Pool | a039749 | 2000-10-26 07:24:18 +0000 | [diff] [blame] | 270 | |
| 271 | /* This is like rprintf, but it also tries to print some |
| 272 | * representation of the error code. Normally errcode = errno. |
| 273 | * |
| 274 | * Unlike rprintf, this always adds a newline and there should not be |
| 275 | * one in the format string. |
| 276 | * |
| 277 | * Note that since strerror might involve dynamically loading a |
| 278 | * message catalog we need to call it once before chroot-ing. */ |
| 279 | void rsyserr(enum logcode code, int errcode, const char *format, ...) |
| 280 | { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 281 | va_list ap; |
| Wayne Davison | 8fcdc44 | 2004-05-15 19:31:16 +0000 | [diff] [blame] | 282 | char buf[MAXPATHLEN+512]; |
| 283 | size_t len; |
| 284 | |
| 285 | strcpy(buf, RSYNC_NAME ": "); |
| 286 | len = (sizeof RSYNC_NAME ": ") - 1; |
| Martin Pool | a039749 | 2000-10-26 07:24:18 +0000 | [diff] [blame] | 287 | |
| 288 | va_start(ap, format); |
| Wayne Davison | 8fcdc44 | 2004-05-15 19:31:16 +0000 | [diff] [blame] | 289 | len += vsnprintf(buf + len, sizeof buf - len, format, ap); |
| Martin Pool | a039749 | 2000-10-26 07:24:18 +0000 | [diff] [blame] | 290 | va_end(ap); |
| 291 | |
| Wayne Davison | 8fcdc44 | 2004-05-15 19:31:16 +0000 | [diff] [blame] | 292 | if (len < sizeof buf) { |
| 293 | len += snprintf(buf + len, sizeof buf - len, |
| 294 | ": %s (%d)\n", strerror(errcode), errcode); |
| 295 | } |
| 296 | if (len >= sizeof buf) |
| Martin Pool | 59ee743 | 2002-01-23 08:04:54 +0000 | [diff] [blame] | 297 | exit_cleanup(RERR_MESSAGEIO); |
| Martin Pool | a039749 | 2000-10-26 07:24:18 +0000 | [diff] [blame] | 298 | |
| Martin Pool | a039749 | 2000-10-26 07:24:18 +0000 | [diff] [blame] | 299 | rwrite(code, buf, len); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| Andrew Tridgell | ff41a59 | 2000-01-28 15:29:59 +0000 | [diff] [blame] | 304 | void rflush(enum logcode code) |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 305 | { |
| 306 | FILE *f = NULL; |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 307 | |
| 308 | if (am_daemon) { |
| 309 | return; |
| 310 | } |
| 311 | |
| Andrew Tridgell | ff41a59 | 2000-01-28 15:29:59 +0000 | [diff] [blame] | 312 | if (code == FLOG) { |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 313 | return; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 314 | } |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 315 | |
| Andrew Tridgell | ff41a59 | 2000-01-28 15:29:59 +0000 | [diff] [blame] | 316 | if (code == FERROR) { |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 317 | f = stderr; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 318 | } |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 319 | |
| Andrew Tridgell | ff41a59 | 2000-01-28 15:29:59 +0000 | [diff] [blame] | 320 | if (code == FINFO) { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 321 | if (am_server) |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 322 | f = stderr; |
| 323 | else |
| 324 | f = stdout; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 325 | } |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 326 | |
| Andrew Tridgell | 6541757 | 1998-11-03 07:08:27 +0000 | [diff] [blame] | 327 | if (!f) exit_cleanup(RERR_MESSAGEIO); |
| Andrew Tridgell | 0b76cd6 | 1998-05-09 14:00:55 +0000 | [diff] [blame] | 328 | fflush(f); |
| 329 | } |
| 330 | |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 331 | |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 332 | |
| 333 | /* a generic logging routine for send/recv, with parameter |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 334 | * substitiution */ |
| Andrew Tridgell | 0f3203c | 2000-01-29 02:49:03 +0000 | [diff] [blame] | 335 | static void log_formatted(enum logcode code, |
| Andrew Tridgell | b606265 | 1998-11-03 03:48:47 +0000 | [diff] [blame] | 336 | char *format, char *op, struct file_struct *file, |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 337 | struct stats *initial_stats) |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 338 | { |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 339 | char buf[1024]; |
| Andrew Tridgell | ab7104d | 1998-11-02 06:40:11 +0000 | [diff] [blame] | 340 | char buf2[1024]; |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 341 | char *p, *s, *n; |
| Martin Pool | 59ee743 | 2002-01-23 08:04:54 +0000 | [diff] [blame] | 342 | size_t l; |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 343 | int64 b; |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 344 | |
| Martin Pool | aa12697 | 2002-01-24 02:41:38 +0000 | [diff] [blame] | 345 | /* We expand % codes one by one in place in buf. We don't |
| 346 | * copy in the terminating nul of the inserted strings, but |
| 347 | * rather keep going until we reach the nul of the format. |
| 348 | * Just to make sure we don't clobber that nul and therefore |
| 349 | * accidentally keep going, we zero the buffer now. */ |
| Wayne Davison | 8fcdc44 | 2004-05-15 19:31:16 +0000 | [diff] [blame] | 350 | l = strlcpy(buf, format, sizeof buf); |
| 351 | if (l < sizeof buf) |
| 352 | memset(buf + l, 0, sizeof buf - l); |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 353 | |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 354 | for (s = &buf[0]; s && (p = strchr(s,'%')); ) { |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 355 | n = NULL; |
| 356 | s = p + 1; |
| 357 | |
| 358 | switch (p[1]) { |
| Andrew Tridgell | af77cc6 | 1998-11-16 23:50:28 +0000 | [diff] [blame] | 359 | case 'h': if (am_daemon) n = client_name(0); break; |
| 360 | case 'a': if (am_daemon) n = client_addr(0); break; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 361 | case 'l': |
| 362 | snprintf(buf2,sizeof(buf2),"%.0f", |
| 363 | (double)file->length); |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 364 | n = buf2; |
| 365 | break; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 366 | case 'p': |
| 367 | snprintf(buf2,sizeof(buf2),"%d", |
| 368 | (int)getpid()); |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 369 | n = buf2; |
| 370 | break; |
| 371 | case 'o': n = op; break; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 372 | case 'f': |
| Wayne Davison | 81d2b0e | 2004-01-20 05:15:14 +0000 | [diff] [blame] | 373 | pathjoin(buf2, sizeof buf2, |
| 374 | file->basedir ? file->basedir : "", |
| Andrew Tridgell | ab7104d | 1998-11-02 06:40:11 +0000 | [diff] [blame] | 375 | f_name(file)); |
| Wayne Davison | 58b1999 | 2004-08-12 18:20:07 +0000 | [diff] [blame] | 376 | clean_fname(buf2, 0); |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 377 | n = buf2; |
| Andrew Tridgell | b606265 | 1998-11-03 03:48:47 +0000 | [diff] [blame] | 378 | if (*n == '/') n++; |
| Andrew Tridgell | ab7104d | 1998-11-02 06:40:11 +0000 | [diff] [blame] | 379 | break; |
| Andrew Tridgell | 97cb8dc | 1998-10-30 05:53:05 +0000 | [diff] [blame] | 380 | case 'm': n = lp_name(module_id); break; |
| Andrew Tridgell | b606265 | 1998-11-03 03:48:47 +0000 | [diff] [blame] | 381 | case 't': n = timestring(time(NULL)); break; |
| Andrew Tridgell | 97cb8dc | 1998-10-30 05:53:05 +0000 | [diff] [blame] | 382 | case 'P': n = lp_path(module_id); break; |
| 383 | case 'u': n = auth_user; break; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 384 | case 'b': |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 385 | if (am_sender) { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 386 | b = stats.total_written - |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 387 | initial_stats->total_written; |
| 388 | } else { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 389 | b = stats.total_read - |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 390 | initial_stats->total_read; |
| 391 | } |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 392 | snprintf(buf2,sizeof(buf2),"%.0f", (double)b); |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 393 | n = buf2; |
| 394 | break; |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 395 | case 'c': |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 396 | if (!am_sender) { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 397 | b = stats.total_written - |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 398 | initial_stats->total_written; |
| 399 | } else { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 400 | b = stats.total_read - |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 401 | initial_stats->total_read; |
| 402 | } |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 403 | snprintf(buf2,sizeof(buf2),"%.0f", (double)b); |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 404 | n = buf2; |
| 405 | break; |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| Martin Pool | aa12697 | 2002-01-24 02:41:38 +0000 | [diff] [blame] | 408 | /* n is the string to be inserted in place of this % |
| 409 | * code; l is its length not including the trailing |
| 410 | * NUL */ |
| 411 | if (!n) |
| 412 | continue; |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 413 | |
| 414 | l = strlen(n); |
| 415 | |
| Martin Pool | 9dd891b | 2002-01-23 04:57:18 +0000 | [diff] [blame] | 416 | if (l + ((int)(s - &buf[0])) >= sizeof(buf)) { |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 417 | rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n", |
| 418 | p[0]); |
| Andrew Tridgell | 6541757 | 1998-11-03 07:08:27 +0000 | [diff] [blame] | 419 | exit_cleanup(RERR_MESSAGEIO); |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| Martin Pool | aa12697 | 2002-01-24 02:41:38 +0000 | [diff] [blame] | 422 | /* Shuffle the rest of the string along to make space for n */ |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 423 | if (l != 2) { |
| Andrew Tridgell | 3472009 | 1998-10-30 02:36:05 +0000 | [diff] [blame] | 424 | memmove(s+(l-1), s+1, strlen(s+1)+1); |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 425 | } |
| Martin Pool | aa12697 | 2002-01-24 02:41:38 +0000 | [diff] [blame] | 426 | |
| 427 | /* Copy in n but NOT its nul, because the format sting |
| 428 | * probably continues after this. */ |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 429 | memcpy(p, n, l); |
| 430 | |
| Martin Pool | aa12697 | 2002-01-24 02:41:38 +0000 | [diff] [blame] | 431 | /* Skip over inserted string; continue looking */ |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 432 | s = p+l; |
| 433 | } |
| 434 | |
| Andrew Tridgell | 0f3203c | 2000-01-29 02:49:03 +0000 | [diff] [blame] | 435 | rprintf(code,"%s\n", buf); |
| Andrew Tridgell | e08bfe1 | 1998-10-30 02:24:47 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 438 | /* log the outgoing transfer of a file */ |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 439 | void log_send(struct file_struct *file, struct stats *initial_stats) |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 440 | { |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 441 | if (lp_transfer_logging(module_id)) { |
| Andrew Tridgell | b606265 | 1998-11-03 03:48:47 +0000 | [diff] [blame] | 442 | log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats); |
| 443 | } else if (log_format && !am_server) { |
| 444 | log_formatted(FINFO, log_format, "send", file, initial_stats); |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | /* log the incoming transfer of a file */ |
| Andrew Tridgell | 1b7c47c | 1998-11-02 04:17:56 +0000 | [diff] [blame] | 449 | void log_recv(struct file_struct *file, struct stats *initial_stats) |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 450 | { |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 451 | if (lp_transfer_logging(module_id)) { |
| David Dykstra | 117af10 | 1999-01-05 20:08:45 +0000 | [diff] [blame] | 452 | log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats); |
| Andrew Tridgell | b606265 | 1998-11-03 03:48:47 +0000 | [diff] [blame] | 453 | } else if (log_format && !am_server) { |
| David Dykstra | 117af10 | 1999-01-05 20:08:45 +0000 | [diff] [blame] | 454 | log_formatted(FINFO, log_format, "recv", file, initial_stats); |
| Andrew Tridgell | 11a5a3c | 1998-10-28 03:28:30 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| Martin Pool | af642a6 | 2000-11-10 03:28:15 +0000 | [diff] [blame] | 458 | |
| 459 | |
| 460 | |
| 461 | /* |
| 462 | * Called when the transfer is interrupted for some reason. |
| 463 | * |
| 464 | * Code is one of the RERR_* codes from errcode.h, or terminating |
| 465 | * successfully. |
| 466 | */ |
| Andrew Tridgell | a9766ef | 1998-11-15 01:21:42 +0000 | [diff] [blame] | 467 | void log_exit(int code, const char *file, int line) |
| Andrew Tridgell | 9b73d1c | 1998-10-29 11:11:38 +0000 | [diff] [blame] | 468 | { |
| 469 | if (code == 0) { |
| Andrew Tridgell | 67ea0d4 | 1998-10-29 11:16:51 +0000 | [diff] [blame] | 470 | rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n", |
| Andrew Tridgell | 9b73d1c | 1998-10-29 11:11:38 +0000 | [diff] [blame] | 471 | (double)stats.total_written, |
| 472 | (double)stats.total_read, |
| 473 | (double)stats.total_size); |
| 474 | } else { |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 475 | const char *name; |
| Martin Pool | af642a6 | 2000-11-10 03:28:15 +0000 | [diff] [blame] | 476 | |
| Wayne Davison | 4a7319b | 2003-12-15 01:45:01 +0000 | [diff] [blame] | 477 | name = rerr_name(code); |
| 478 | if (!name) |
| 479 | name = "unexplained error"; |
| 480 | |
| Wayne Davison | 1bca1de | 2003-12-15 08:06:52 +0000 | [diff] [blame] | 481 | /* VANISHED is not an error, only a warning */ |
| 482 | if (code == RERR_VANISHED) { |
| 483 | rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n", |
| 484 | name, code, file, line); |
| 485 | } else { |
| 486 | rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n", |
| 487 | name, code, file, line); |
| 488 | } |
| Andrew Tridgell | 9b73d1c | 1998-10-29 11:11:38 +0000 | [diff] [blame] | 489 | } |
| 490 | } |