]> andersk Git - moira.git/blob - dbck/dbck.pc
Fix another freeing-memory-we-didn't-malloc bug in AddMachine. This
[moira.git] / dbck / dbck.pc
1 /* $Header$
2  *
3  * Moira database consistency checker
4  *
5  *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
6  *  For copying and distribution information, please see the file
7  *  <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <signal.h>
14 #include "dbck.h"
15 EXEC SQL INCLUDE sqlca;
16 EXEC SQL INCLUDE sqlda;
17
18 static char dbck_qc_rcsid[] = "$Header$";
19
20
21 int debug = 0;
22 int mode = MODE_ASK;
23 int fast = 0;
24 int warn = 1;
25 int abort_p = 0;
26 struct hash *users, *machines, *clusters, *lists, *filesys, *nfsphys;
27 struct hash *strings, *members, *subnets, *string_dups;
28 EXEC SQL BEGIN DECLARE SECTION; 
29 int dcmenable;
30 EXEC SQL END DECLARE SECTION; 
31 struct save_queue *modtables, *sq_create();
32 void interrupt();
33 SQLDA *mr_sqlda;
34
35 extern SQLDA *sqlald(int,int,int);
36
37 main(argc, argv)
38 int argc;
39 char **argv;
40 {
41     char **arg = argv;
42 EXEC SQL BEGIN DECLARE SECTION; 
43     char *database;
44 EXEC SQL END DECLARE SECTION; 
45     int ingerr();
46     int countonly = 0;
47
48     database = "moira";
49     setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
50
51     while (++arg - argv < argc) {
52         if  (**arg == '-')
53           switch ((*arg)[1]) {
54           case 'd':
55               debug = atoi((*arg)[2] ? *arg+2 : *++arg);
56               break;
57           case 'n':
58               mode = MODE_NO;
59               break;
60           case 'y':
61               mode = MODE_YES;
62               break;
63           case 'p':
64               mode = MODE_PREEN;
65               break;
66           case 'a':
67               mode = MODE_ASK;
68               break;
69           case 'c':
70               countonly++;
71               break;
72           case 'f':
73               fast++;
74               break;
75           case 'w':
76               warn = 0;
77               break;
78           default:
79               printf("Usage: %s [-d level] [-n] [-y] [-p] [-a] [-c] [-f] [-w] [database]\n",
80                      argv[0]);
81               exit(1);
82           }
83         else
84           database = *arg;
85     }
86     if (countonly)
87       printf("Only doing counts\n");
88     else if (fast)
89       printf("Doing fast version (skipping some checks)\n");
90     if (mode == MODE_NO)
91       printf("Will NOT modify the database\n");
92     else if (mode == MODE_PREEN)
93       printf("Will fix simple things without asking\n");
94     else if (mode == MODE_YES)
95       printf("Will fix everything without asking\n");
96     if (debug)
97       printf("Debug level is %d\n", debug);
98
99     signal(SIGHUP, interrupt);
100     signal(SIGQUIT, interrupt);
101     signal(SIGINT, interrupt);
102
103     modtables = sq_create();
104     mr_sqlda = sqlald(1, 255, 0);
105
106     EXEC SQL WHENEVER SQLERROR DO dbmserr();
107     printf("Opening database %s...", database);
108     fflush(stdout);
109     EXEC SQL CONNECT :database IDENTIFIED BY :database;
110     printf("done\n");
111     EXEC SQL SELECT value INTO :dcmenable FROM numvalues 
112         WHERE name='dcm_enable'; 
113     dprintf("DCM disabled (was %d)\n", dcmenable);
114     EXEC SQL UPDATE numvalues SET value=0 WHERE name='dcm_enable';
115
116     /* Begin transaction here. */
117
118     if (!countonly) {
119         phase1();
120         EXEC SQL COMMIT WORK;
121         phase2();
122         EXEC SQL COMMIT WORK;
123         phase3();
124         EXEC SQL COMMIT WORK;
125     } else {
126         count_only_setup();
127         EXEC SQL COMMIT WORK;
128     }
129     phase4();
130     EXEC SQL COMMIT WORK;
131
132     cleanup();
133     printf("Done.\n");
134     exit(0);
135 }
136
137 dbmserr()
138 {
139 EXEC SQL BEGIN DECLARE SECTION;
140     char buf[512];
141 EXEC SQL END DECLARE SECTION;
142     int bufsize=256, msglength=0;
143
144     if (sqlca.sqlcode==1403) return;
145     printf("A DBMS error occurred, code %d\n", sqlca.sqlcode);
146     sqlglm(buf, &bufsize, &msglength);
147     buf[msglength]=0;
148     printf("%s\n", buf);
149     printf("Aborting...\n");
150     if (!abort_p) {
151         abort_p++;
152         EXEC SQL ROLLBACK WORK;
153     }
154     exit(1);
155 }
156
157
158 void interrupt()
159 {
160     printf("Signal caught\n");
161     if (prompt("Save database changes")) {
162         EXEC SQL COMMIT WORK;
163         cleanup();
164         exit(0);
165     }
166     printf("Aborting transaction\n");
167     if (!abort_p) {
168         abort_p++;
169         EXEC SQL ROLLBACK WORK;
170     }
171
172     EXEC SQL UPDATE numvalues SET value=:dcmenable
173         WHERE name='dcm_enable';
174
175     exit(0);
176 }
177
178
179 modified(table)
180 char *table;
181 {
182     sq_save_unique_string(modtables, table);
183 }
184
185 cleanup()
186 {
187     EXEC SQL BEGIN DECLARE SECTION; 
188     char *tab;
189     EXEC SQL END DECLARE SECTION; 
190
191     while (sq_get_data(modtables, &tab)) {
192         EXEC SQL UPDATE tblstats SET modtime=SYSDATE
193             WHERE table_name = :tab;
194     }
195     EXEC SQL UPDATE numvalues SET value = :dcmenable
196         WHERE name='dcm_enable';
197 }
198
199
200 out_of_mem(msg)
201 char *msg;
202 {
203     fprintf(stderr, "Out of memory while %s\n", msg);
204     if (prompt("Save database changes")) {
205         EXEC SQL COMMIT WORK;
206         cleanup();
207         exit(1);
208     }
209     printf("Aborting transaction\n");
210     EXEC SQL ROLLBACK WORK;
211     exit(1);
212 }
This page took 0.176563 seconds and 5 git commands to generate.