syslog support in rsync daemon has been broken since I added the "log
[rsync/rsync.git] / log.c
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  %s", timestring(), buf);
55                 fflush(logfile);
56         } else {
57                 syslog(priority, "%s", buf);
58         }
59 }
60
61 void log_open(void)
62 {
63         static int initialised;
64         int options = LOG_PID;
65         time_t t;
66         char *logf;
67
68         if (initialised) return;
69         initialised = 1;
70
71         logf = lp_log_file();
72         if (logf && *logf) {
73                 logfile = fopen(logf, "a");
74                 return;
75         }
76
77 #ifdef LOG_NDELAY
78         options |= LOG_NDELAY;
79 #endif
80
81 #ifdef LOG_DAEMON
82         openlog("rsyncd", options, lp_syslog_facility());
83 #else
84         openlog("rsyncd", options);
85 #endif
86
87 #ifndef LOG_NDELAY
88         logit(LOG_INFO,"rsyncd started\n");
89 #endif
90
91         /* this looks pointless, but it is needed in order for the
92            C library on some systems to fetch the timezone info
93            before the chroot */
94         t = time(NULL);
95         localtime(&t);
96 }
97                 
98
99 /* this is the rsync debugging function. Call it with FINFO or FERROR */
100 void rprintf(int fd, const char *format, ...)
101 {
102         va_list ap;  
103         char buf[1024];
104         int len;
105         FILE *f=NULL;
106         extern int am_daemon;
107         /* recursion can happen with certain fatal conditions */
108         static int depth;
109
110         if (depth) return;
111
112         depth++;
113
114         va_start(ap, format);
115         len = vslprintf(buf, sizeof(buf)-1, format, ap);
116         va_end(ap);
117
118         if (len < 0) exit_cleanup(1);
119
120         if (len > sizeof(buf)-1) exit_cleanup(1);
121
122         buf[len] = 0;
123
124         if (fd == FLOG) {
125                 if (am_daemon) logit(LOG_INFO, buf);
126                 depth--;
127                 return;
128         }
129
130         if (am_daemon) {
131                 int priority = LOG_INFO;
132                 if (fd == FERROR) priority = LOG_WARNING;
133
134                 log_open();
135                 if (!io_multiplex_write(fd, buf, strlen(buf))) {
136                         logit(priority, buf);
137                 }
138
139                 depth--;
140                 return;
141         }
142
143         if (fd == FERROR) {
144                 f = stderr;
145         } 
146
147         if (fd == FINFO) {
148                 extern int am_server;
149                 if (am_server) 
150                         f = stderr;
151                 else
152                         f = stdout;
153         } 
154
155         if (!f) exit_cleanup(1);
156
157         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
158
159         if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
160
161         depth--;
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 == FERROR) {
174                 f = stderr;
175         } 
176
177         if (fd == FINFO) {
178                 extern int am_server;
179                 if (am_server) 
180                         f = stderr;
181                 else
182                         f = stdout;
183         } 
184
185         if (!f) exit_cleanup(1);
186         fflush(f);
187 }
188
189
190 /* log the outgoing transfer of a file */
191 void log_send(struct file_struct *file)
192 {
193         extern int module_id;
194         if (lp_transfer_logging(module_id)) {
195                 rprintf(FLOG,"Sending %s [%s] %.0f %s\n",
196                         client_name(0), client_addr(0),
197                         (double)file->length, f_name(file));
198         }
199 }
200
201 /* log the incoming transfer of a file */
202 void log_recv(struct file_struct *file)
203 {
204         extern int module_id;
205         if (lp_transfer_logging(module_id)) {
206                 rprintf(FLOG,"Receiving %s [%s] %.0f %s\n",
207                         client_name(0), client_addr(0),
208                         (double)file->length, f_name(file));
209         }
210 }
211
212 /* log the incoming transfer of a file for interactive use, this
213    will be called at the end where the client was run */
214 void log_transfer(struct file_struct *file, char *fname)
215 {
216         extern int verbose;
217
218         if (!verbose) return;
219
220         rprintf(FINFO,"%s\n", fname);
221 }