Imported Upstream version 2.5.1
[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 2006/06/01 14:54:39 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     return(vstralloc(leader, linebuf, "\n", NULL));
108 }
109
110 printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
111 {
112     va_list argp;
113     int saved_errout;
114     char *leader = NULL;
115     char linebuf[STR_SIZE];
116     size_t n;
117
118
119     /* format error message */
120
121     if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
122
123     if(multiline > 0) {
124         leader = stralloc("  ");                /* continuation line */
125     } else {
126         leader = vstralloc(logtype_str[(int)typ], " ", get_pname(), " ", NULL);
127     }
128
129     arglist_start(argp, format);
130     vsnprintf(linebuf, SIZEOF(linebuf)-1, format, argp);
131                                                 /* -1 to allow for '\n' */
132     arglist_end(argp);
133
134     /* avoid recursive call from error() */
135
136     saved_errout = erroutput_type;
137     erroutput_type &= ~ERR_AMANDALOG;
138
139     /* append message to the log file */
140
141     if(multiline == -1) open_log();
142
143     if (fullwrite(logfd, leader, strlen(leader)) < 0) {
144         error("log file write error: %s", strerror(errno));
145         /*NOTREACHED*/
146     }
147
148     amfree(leader);
149
150     n = strlen(linebuf);
151     if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n';
152     linebuf[n] = '\0';
153
154     if (fullwrite(logfd, linebuf, n) < 0) {
155         error("log file write error: %s", strerror(errno));
156         /*NOTREACHED*/
157     }
158
159     if(multiline != -1) multiline++;
160     else close_log();
161
162     erroutput_type = saved_errout;
163 }
164
165 void
166 log_start_multiline(void)
167 {
168     assert(multiline == -1);
169
170     multiline = 0;
171     open_log();
172 }
173
174
175 void
176 log_end_multiline(void)
177 {
178     assert(multiline != -1);
179     multiline = -1;
180     close_log();
181 }
182
183
184 void
185 log_rename(
186     char *      datestamp)
187 {
188     char *conf_logdir;
189     char *logfile;
190     char *fname = NULL;
191     char seq_str[NUM_STR_SIZE];
192     unsigned int seq;
193     struct stat statbuf;
194
195     if(datestamp == NULL) datestamp = "error";
196
197     conf_logdir = getconf_str(CNF_LOGDIR);
198     if (*conf_logdir == '/') {
199         conf_logdir = stralloc(conf_logdir);
200     } else {
201         conf_logdir = stralloc2(config_dir, conf_logdir);
202     }
203     logfile = vstralloc(conf_logdir, "/log", NULL);
204
205     for(seq = 0; 1; seq++) {    /* if you've got MAXINT files in your dir... */
206         snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
207         fname = newvstralloc(fname,
208                              logfile,
209                              ".", datestamp,
210                              ".", seq_str,
211                              NULL);
212         if(stat(fname, &statbuf) == -1 && errno == ENOENT) break;
213     }
214
215     if(rename(logfile, fname) == -1) {
216         error("could not rename \"%s\" to \"%s\": %s",
217               logfile, fname, strerror(errno));
218         /*NOTREACHED*/
219     }
220
221     amfree(fname);
222     amfree(logfile);
223     amfree(conf_logdir);
224 }
225
226
227 static void
228 open_log(void)
229 {
230     char *conf_logdir;
231
232     conf_logdir = getconf_str(CNF_LOGDIR);
233     if (*conf_logdir == '/') {
234         conf_logdir = stralloc(conf_logdir);
235     } else {
236         conf_logdir = stralloc2(config_dir, conf_logdir);
237     }
238     logfile = vstralloc(conf_logdir, "/log", NULL);
239     amfree(conf_logdir);
240
241     logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600);
242
243     if(logfd == -1) {
244         error("could not open log file %s: %s", logfile, strerror(errno));
245         /*NOTREACHED*/
246     }
247
248     if(amflock(logfd, "log") == -1) {
249         error("could not lock log file %s: %s", logfile, strerror(errno));
250         /*NOTREACHED*/
251     }
252 }
253
254
255 static void
256 close_log(void)
257 {
258     if(amfunlock(logfd, "log") == -1) {
259         error("could not unlock log file %s: %s", logfile, strerror(errno));
260         /*NOTREACHED*/
261     }
262
263     if(close(logfd) == -1) {
264         error("close log file: %s", strerror(errno));
265         /*NOTREACHED*/
266     }
267
268     logfd = -1;
269     amfree(logfile);
270 }
271
272
273 int
274 get_logline(
275     FILE *      logf)
276 {
277     static char *logline = NULL;
278     char *logstr, *progstr;
279     char *s;
280     int ch;
281
282     amfree(logline);
283     while ((logline = agets(logf)) != NULL) {
284         if (logline[0] != '\0')
285             break;
286         amfree(logline);
287     }
288     if (logline == NULL) return 0;
289     curlinenum++;
290     s = logline;
291     ch = *s++;
292
293     /* continuation lines are special */
294
295     if(logline[0] == ' ' && logline[1] == ' ') {
296         curlog = L_CONT;
297         /* curprog stays the same */
298         skip_whitespace(s, ch);
299         curstr = s-1;
300         return 1;
301     }
302
303     /* isolate logtype field */
304
305     skip_whitespace(s, ch);
306     logstr = s - 1;
307     skip_non_whitespace(s, ch);
308     s[-1] = '\0';
309
310     /* isolate program name field */
311
312     skip_whitespace(s, ch);
313     progstr = s - 1;
314     skip_non_whitespace(s, ch);
315     s[-1] = '\0';
316
317     /* rest of line is logtype dependent string */
318
319     skip_whitespace(s, ch);
320     curstr = s - 1;
321
322     /* lookup strings */
323
324     for(curlog = L_MARKER; curlog != L_BOGUS; curlog--)
325         if(strcmp(logtype_str[curlog], logstr) == 0) break;
326
327     for(curprog = P_LAST; curprog != P_UNKNOWN; curprog--)
328         if(strcmp(program_str[curprog], progstr) == 0) break;
329
330     return 1;
331 }