728ee3a3bff796eb23cd457608f669242d9177c3
[debian/amanda] / server-src / amlogroll.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: amlogroll.c,v 1.1.2.1.4.1.2.3 2003/10/27 17:55:39 martinea Exp $
29  *
30  * rename a live log file to the datestamped name.
31  */
32
33 #include "amanda.h"
34 #include "conffile.h"
35 #include "logfile.h"
36 #include "version.h"
37
38 char *datestamp;
39
40 void handle_start P((void));
41
42 int main(argc, argv)
43 int argc;
44 char **argv;
45 {
46     char *conffile;
47     char *logfname;
48     char *conf_logdir;
49     FILE *logfile;
50     int fd;
51     unsigned long malloc_hist_1, malloc_size_1;
52     unsigned long malloc_hist_2, malloc_size_2;
53     char my_cwd[STR_SIZE];
54
55     for(fd = 3; fd < FD_SETSIZE; fd++) {
56         /*
57          * Make sure nobody spoofs us with a lot of extra open files
58          * that would cause an open we do to get a very high file
59          * descriptor, which in turn might be used as an index into
60          * an array (e.g. an fd_set).
61          */
62         close(fd);
63     }
64
65     set_pname("amlogroll");
66
67     malloc_size_1 = malloc_inuse(&malloc_hist_1);
68
69     /* Process options */
70     
71     erroutput_type = ERR_INTERACTIVE;
72
73     if (getcwd(my_cwd, sizeof(my_cwd)) == NULL) {
74         error("cannot determine current working directory");
75     }
76
77     if (argc < 2) {
78         config_dir = stralloc2(my_cwd, "/");
79         if ((config_name = strrchr(my_cwd, '/')) != NULL) {
80             config_name = stralloc(config_name + 1);
81         }
82     } else {
83         config_name = stralloc(argv[1]);
84         config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
85     }
86
87     safe_cd();
88
89     /* read configuration files */
90
91     conffile = stralloc2(config_dir, CONFFILE_NAME);
92     if(read_conffile(conffile)) {
93         error("errors processing config file \"%s\"", conffile);
94     }
95     amfree(conffile);
96
97     conf_logdir = getconf_str(CNF_LOGDIR);
98     if (*conf_logdir == '/') {
99         conf_logdir = stralloc(conf_logdir);
100     } else {
101         conf_logdir = stralloc2(config_dir, conf_logdir);
102     }
103     logfname = vstralloc(conf_logdir, "/", "log", NULL);
104     amfree(conf_logdir);
105
106     if((logfile = fopen(logfname, "r")) == NULL) {
107         error("could not open log %s: %s", logfname, strerror(errno));
108     }
109     amfree(logfname);
110
111     erroutput_type |= ERR_AMANDALOG;
112     set_logerror(logerror);
113
114     while(get_logline(logfile)) {
115         if(curlog == L_START) {
116             handle_start();
117             if(datestamp != NULL) {
118                 break;
119             }
120         }
121     }
122     afclose(logfile);
123  
124     log_rename(datestamp);
125
126     amfree(datestamp);
127     amfree(config_dir);
128     amfree(config_name);
129
130     malloc_size_2 = malloc_inuse(&malloc_hist_2);
131
132     if(malloc_size_1 != malloc_size_2) {
133         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
134     }
135
136     return 0;
137 }
138
139 void handle_start()
140 {
141     static int started = 0;
142     char *s, *fp;
143     int ch;
144
145     if(!started) {
146         s = curstr;
147         ch = *s++;
148
149         skip_whitespace(s, ch);
150 #define sc "date"
151         if(ch == '\0' || strncmp(s - 1, sc, sizeof(sc)-1) != 0) {
152             return;                             /* ignore bogus line */
153         }
154         s += sizeof(sc)-1;
155         ch = s[-1];
156 #undef sc
157         skip_whitespace(s, ch);
158         if(ch == '\0') {
159             return;
160         }
161         fp = s - 1;
162         skip_non_whitespace(s, ch);
163         s[-1] = '\0';
164         datestamp = newstralloc(datestamp, fp);
165         s[-1] = ch;
166
167         started = 1;
168     }
169 }