Imported Upstream version 2.5.1p1
[debian/amanda] / server-src / logfile.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * $Id: logfile.c,v 1.31.2.1 2006/09/22 11:02:13 martinea Exp $
29  *
30  * common log file writing routine
31  */
32 #include "amanda.h"
33 #include "arglist.h"
34 #include "util.h"
35 #include "conffile.h"
36
37 #include "logfile.h"
38
39 char *logtype_str[] = {
40     "BOGUS",
41     "FATAL",            /* program died for some reason, used by error() */
42     "ERROR", "WARNING", "INFO", "SUMMARY",       /* information messages */
43     "START", "FINISH",                             /* start/end of a run */
44     "DISK",                                                      /* disk */
45     "SUCCESS", "PARTIAL", "FAIL", "STRANGE",        /* the end of a dump */
46     "CHUNK", "CHUNKSUCCESS",                            /* ... continued */
47     "STATS",                                               /* statistics */
48     "MARKER",                                     /* marker for reporter */
49     "CONT"                                 /* continuation line; special */
50 };
51
52 char *program_str[] = {
53     "UNKNOWN", "planner", "driver", "amreport", "dumper", "chunker",
54     "taper", "amflush"
55 };
56
57 int curlinenum;
58 logtype_t curlog;
59 program_t curprog;
60 char *curstr;
61
62 int multiline = -1;
63 static char *logfile;
64 static int logfd = -1;
65
66  /*
67   * Note that technically we could use two locks, a read lock
68   * from 0-EOF and a write-lock from EOF-EOF, thus leaving the
69   * beginning of the file open for read-only access.  Doing so
70   * would open us up to some race conditions unless we're pretty
71   * careful, and on top of that the functions here are so far
72   * the only accesses to the logfile, so keep things simple.
73   */
74
75 /* local functions */
76 static void open_log(void);
77 static void close_log(void);
78
79 void
80 logerror(
81     char *      msg)
82 {
83     log_add(L_FATAL, "%s", msg);
84 }
85
86
87 printf_arglist_function2(char *log_genstring, logtype_t, typ, char *, pname, char *, format)
88 {
89     va_list argp;
90     char *leader = NULL;
91     char linebuf[STR_SIZE];
92
93
94     /* format error message */
95
96     if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
97
98     if(multiline > 0) {
99         leader = stralloc("  ");                /* continuation line */
100     } else {
101         leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
102     }
103
104     arglist_start(argp, format);
105     vsnprintf(linebuf, SIZEOF(linebuf)-1, format, argp);
106                                                 /* -1 to allow for '\n' */
107     arglist_end(argp);
108     return(vstralloc(leader, linebuf, "\n", NULL));
109 }
110
111 printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
112 {
113     va_list argp;
114     int saved_errout;
115     char *leader = NULL;
116     char linebuf[STR_SIZE];
117     size_t n;
118
119
120     /* format error message */
121
122     if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
123
124     if(multiline > 0) {
125         leader = stralloc("  ");                /* continuation line */
126     } else {
127         leader = vstralloc(logtype_str[(int)typ], " ", get_pname(), " ", NULL);
128     }
129
130     arglist_start(argp, format);
131     vsnprintf(linebuf, SIZEOF(linebuf)-1, format, argp);
132                                                 /* -1 to allow for '\n' */
133     arglist_end(argp);
134
135     /* avoid recursive call from error() */
136
137     saved_errout = erroutput_type;
138     erroutput_type &= ~ERR_AMANDALOG;
139
140     /* append message to the log file */
141
142     if(multiline == -1) open_log();
143
144     if (fullwrite(logfd, leader, strlen(leader)) < 0) {
145         error("log file write error: %s", strerror(errno));
146         /*NOTREACHED*/
147     }
148
149     amfree(leader);
150
151     n = strlen(linebuf);
152     if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n';
153     linebuf[n] = '\0';
154
155     if (fullwrite(logfd, linebuf, n) < 0) {
156         error("log file write error: %s", strerror(errno));
157         /*NOTREACHED*/
158     }
159
160     if(multiline != -1) multiline++;
161     else close_log();
162
163     erroutput_type = saved_errout;
164 }
165
166 void
167 log_start_multiline(void)
168 {
169     assert(multiline == -1);
170
171     multiline = 0;
172     open_log();
173 }
174
175
176 void
177 log_end_multiline(void)
178 {
179     assert(multiline != -1);
180     multiline = -1;
181     close_log();
182 }
183
184
185 void
186 log_rename(
187     char *      datestamp)
188 {
189     char *conf_logdir;
190     char *logfile;
191     char *fname = NULL;
192     char seq_str[NUM_STR_SIZE];
193     unsigned int seq;
194     struct stat statbuf;
195
196     if(datestamp == NULL) datestamp = "error";
197
198     conf_logdir = getconf_str(CNF_LOGDIR);
199     if (*conf_logdir == '/') {
200         conf_logdir = stralloc(conf_logdir);
201     } else {
202         conf_logdir = stralloc2(config_dir, conf_logdir);
203     }
204     logfile = vstralloc(conf_logdir, "/log", NULL);
205
206     for(seq = 0; 1; seq++) {    /* if you've got MAXINT files in your dir... */
207         snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
208         fname = newvstralloc(fname,
209                              logfile,
210                              ".", datestamp,
211                              ".", seq_str,
212                              NULL);
213         if(stat(fname, &statbuf) == -1 && errno == ENOENT) break;
214     }
215
216     if(rename(logfile, fname) == -1) {
217         error("could not rename \"%s\" to \"%s\": %s",
218               logfile, fname, strerror(errno));
219         /*NOTREACHED*/
220     }
221
222     amfree(fname);
223     amfree(logfile);
224     amfree(conf_logdir);
225 }
226
227
228 static void
229 open_log(void)
230 {
231     char *conf_logdir;
232
233     conf_logdir = getconf_str(CNF_LOGDIR);
234     if (*conf_logdir == '/') {
235         conf_logdir = stralloc(conf_logdir);
236     } else {
237         conf_logdir = stralloc2(config_dir, conf_logdir);
238     }
239     logfile = vstralloc(conf_logdir, "/log", NULL);
240     amfree(conf_logdir);
241
242     logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600);
243
244     if(logfd == -1) {
245         error("could not open log file %s: %s", logfile, strerror(errno));
246         /*NOTREACHED*/
247     }
248
249     if(amflock(logfd, "log") == -1) {
250         error("could not lock log file %s: %s", logfile, strerror(errno));
251         /*NOTREACHED*/
252     }
253 }
254
255
256 static void
257 close_log(void)
258 {
259     if(amfunlock(logfd, "log") == -1) {
260         error("could not unlock log file %s: %s", logfile, strerror(errno));
261         /*NOTREACHED*/
262     }
263
264     if(close(logfd) == -1) {
265         error("close log file: %s", strerror(errno));
266         /*NOTREACHED*/
267     }
268
269     logfd = -1;
270     amfree(logfile);
271 }
272
273
274 int
275 get_logline(
276     FILE *      logf)
277 {
278     static char *logline = NULL;
279     char *logstr, *progstr;
280     char *s;
281     int ch;
282
283     amfree(logline);
284     while ((logline = agets(logf)) != NULL) {
285         if (logline[0] != '\0')
286             break;
287         amfree(logline);
288     }
289     if (logline == NULL) return 0;
290     curlinenum++;
291     s = logline;
292     ch = *s++;
293
294     /* continuation lines are special */
295
296     if(logline[0] == ' ' && logline[1] == ' ') {
297         curlog = L_CONT;
298         /* curprog stays the same */
299         skip_whitespace(s, ch);
300         curstr = s-1;
301         return 1;
302     }
303
304     /* isolate logtype field */
305
306     skip_whitespace(s, ch);
307     logstr = s - 1;
308     skip_non_whitespace(s, ch);
309     s[-1] = '\0';
310
311     /* isolate program name field */
312
313     skip_whitespace(s, ch);
314     progstr = s - 1;
315     skip_non_whitespace(s, ch);
316     s[-1] = '\0';
317
318     /* rest of line is logtype dependent string */
319
320     skip_whitespace(s, ch);
321     curstr = s - 1;
322
323     /* lookup strings */
324
325     for(curlog = L_MARKER; curlog != L_BOGUS; curlog--)
326         if(strcmp(logtype_str[curlog], logstr) == 0) break;
327
328     for(curprog = P_LAST; curprog != P_UNKNOWN; curprog--)
329         if(strcmp(program_str[curprog], progstr) == 0) break;
330
331     return 1;
332 }