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