blob: a586793228fc896c18797ccb950836be3f973b36 [file] [log] [blame]
Andrew Tridgell0b76cd61998-05-09 14:00:55 +00001/*
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
Andrew Tridgell4f6325c1998-09-09 05:51:42 +000026static FILE *logfile;
27
Andrew Tridgelle0414f41998-09-09 06:06:08 +000028
29/****************************************************************************
30 return the date and time as a string
31****************************************************************************/
32static 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') {
Andrew Tridgell35bdd141998-09-09 07:06:44 +000045 TimeBuf[strlen(TimeBuf)-1] = 0;
Andrew Tridgelle0414f41998-09-09 06:06:08 +000046 }
47
48 return(TimeBuf);
49}
50
Andrew Tridgell4f6325c1998-09-09 05:51:42 +000051static void logit(int priority, char *buf)
52{
53 if (logfile) {
Andrew Tridgelle0414f41998-09-09 06:06:08 +000054 fprintf(logfile,"%s %s", timestring(), buf);
Andrew Tridgell4f6325c1998-09-09 05:51:42 +000055 fflush(logfile);
56 } else {
57 syslog(priority, "%s", buf);
58 }
59}
Andrew Tridgelle42c9451998-05-15 09:26:01 +000060
Andrew Tridgell1a016bf1998-05-15 10:34:07 +000061void log_open(void)
62{
63 static int initialised;
64 int options = LOG_PID;
Andrew Tridgellbcf5b131998-07-02 10:57:20 +000065 time_t t;
Andrew Tridgell1a016bf1998-05-15 10:34:07 +000066
67 if (initialised) return;
68 initialised = 1;
69
Andrew Tridgell4f6325c1998-09-09 05:51:42 +000070 if (lp_log_file()) {
71 logfile = fopen(lp_log_file(), "a");
72 return;
73 }
74
Andrew Tridgell1a016bf1998-05-15 10:34:07 +000075#ifdef LOG_NDELAY
76 options |= LOG_NDELAY;
77#endif
78
79#ifdef LOG_DAEMON
80 openlog("rsyncd", options, lp_syslog_facility());
81#else
82 openlog("rsyncd", options);
83#endif
84
85#ifndef LOG_NDELAY
Andrew Tridgell4f6325c1998-09-09 05:51:42 +000086 logit(LOG_INFO,"rsyncd started\n");
Andrew Tridgell1a016bf1998-05-15 10:34:07 +000087#endif
Andrew Tridgellbcf5b131998-07-02 10:57:20 +000088
89 /* this looks pointless, but it is needed in order for the
90 C library on some systems to fetch the timezone info
91 before the chroot */
92 t = time(NULL);
93 localtime(&t);
Andrew Tridgell1a016bf1998-05-15 10:34:07 +000094}
95
96
Andrew Tridgell0b76cd61998-05-09 14:00:55 +000097/* this is the rsync debugging function. Call it with FINFO or FERROR */
98void rprintf(int fd, const char *format, ...)
99{
100 va_list ap;
101 char buf[1024];
102 int len;
103 FILE *f=NULL;
104 extern int am_daemon;
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000105 /* recursion can happen with certain fatal conditions */
106 static int depth;
107
108 if (depth) return;
109
110 depth++;
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000111
112 va_start(ap, format);
Andrew Tridgelle42c9451998-05-15 09:26:01 +0000113 len = vslprintf(buf, sizeof(buf)-1, format, ap);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000114 va_end(ap);
115
116 if (len < 0) exit_cleanup(1);
117
Andrew Tridgell45ccc5c1998-05-10 08:31:50 +0000118 if (len > sizeof(buf)-1) exit_cleanup(1);
119
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000120 buf[len] = 0;
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000121
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000122 if (fd == FLOG) {
123 if (am_daemon) logit(LOG_INFO, buf);
124 depth--;
125 return;
126 }
127
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000128 if (am_daemon) {
129 int priority = LOG_INFO;
130 if (fd == FERROR) priority = LOG_WARNING;
Andrew Tridgell45ccc5c1998-05-10 08:31:50 +0000131
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000132 log_open();
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000133 if (!io_multiplex_write(fd, buf, strlen(buf))) {
Andrew Tridgell4f6325c1998-09-09 05:51:42 +0000134 logit(priority, buf);
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000135 }
136
137 depth--;
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000138 return;
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000139 }
140
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000141 if (fd == FERROR) {
142 f = stderr;
143 }
144
145 if (fd == FINFO) {
146 extern int am_server;
147 if (am_server)
148 f = stderr;
149 else
150 f = stdout;
151 }
152
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000153 if (!f) exit_cleanup(1);
154
155 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000156
Andrew Tridgellbcf5b131998-07-02 10:57:20 +0000157 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
Andrew Tridgelleb86d661998-07-02 00:47:13 +0000158
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000159 depth--;
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000160}
161
162void rflush(int fd)
163{
164 FILE *f = NULL;
165 extern int am_daemon;
166
167 if (am_daemon) {
168 return;
169 }
170
171 if (fd == FERROR) {
172 f = stderr;
173 }
174
175 if (fd == FINFO) {
176 extern int am_server;
177 if (am_server)
178 f = stderr;
179 else
180 f = stdout;
181 }
182
183 if (!f) exit_cleanup(1);
184 fflush(f);
185}
186
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000187
188/* log the outgoing transfer of a file */
189void log_send(struct file_struct *file)
190{
191 extern int module_id;
192 if (lp_transfer_logging(module_id)) {
193 rprintf(FLOG,"Sending %s [%s] %.0f %s\n",
194 client_name(0), client_addr(0),
195 (double)file->length, f_name(file));
196 }
197}
198
199/* log the incoming transfer of a file */
200void log_recv(struct file_struct *file)
201{
202 extern int module_id;
203 if (lp_transfer_logging(module_id)) {
204 rprintf(FLOG,"Receiving %s [%s] %.0f %s\n",
205 client_name(0), client_addr(0),
206 (double)file->length, f_name(file));
207 }
208}
209
210/* log the incoming transfer of a file for interactive use, this
211 will be called at the end where the client was run */
212void log_transfer(struct file_struct *file, char *fname)
213{
214 extern int verbose;
215
216 if (!verbose) return;
217
218 rprintf(FINFO,"%s\n", fname);
219}