]> andersk Git - moira.git/blame - dbck/dbck.dc
Diane Delgado's changes for a fixed table-locking order
[moira.git] / dbck / dbck.dc
CommitLineData
68bbc9c3 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 <strings.h>
13#include <signal.h>
14#include "dbck.h"
15
16
17static char dbck_qc_rcsid[] = "$Header$";
18
19
20int debug = 0;
21int mode = MODE_ASK;
22int fast = 0;
23int warn = 1;
208a4f4a 24int abort_p = 0;
68bbc9c3 25struct hash *users, *machines, *clusters, *lists, *filesys, *nfsphys;
772b7afc 26struct hash *strings, *members, *subnets;
208a4f4a 27EXEC SQL BEGIN DECLARE SECTION;
28int dcmenable;
29EXEC SQL END DECLARE SECTION;
68bbc9c3 30struct save_queue *modtables, *sq_create();
31int interrupt();
32
33
34main(argc, argv)
35int argc;
36char **argv;
37{
38 char **arg = argv;
208a4f4a 39EXEC SQL BEGIN DECLARE SECTION;
40 char *database;
41EXEC SQL END DECLARE SECTION;
68bbc9c3 42 int ingerr();
43 int countonly = 0;
44
208a4f4a 45 database = "moira";
68bbc9c3 46
47 while (++arg - argv < argc) {
48 if (**arg == '-')
49 switch ((*arg)[1]) {
50 case 'd':
51 debug = atoi((*arg)[2] ? *arg+2 : *++arg);
52 break;
53 case 'n':
54 mode = MODE_NO;
55 break;
56 case 'y':
57 mode = MODE_YES;
58 break;
59 case 'p':
60 mode = MODE_PREEN;
61 break;
62 case 'a':
63 mode = MODE_ASK;
64 break;
65 case 'c':
66 countonly++;
67 break;
68 case 'f':
69 fast++;
70 break;
71 case 'w':
72 warn = 0;
73 break;
74 default:
75 printf("Usage: %s [-d level] [-n] [-y] [-p] [-a] [-c] [-f] [-w] [database]\n",
76 argv[0]);
77 exit(1);
78 }
79 else
80 database = *arg;
81 }
82 if (countonly)
83 printf("Only doing counts\n");
84 else if (fast)
85 printf("Doing fast version (skipping some checks)\n");
86 if (mode == MODE_NO)
87 printf("Will NOT modify the database\n");
88 else if (mode == MODE_PREEN)
89 printf("Will fix simple things without asking\n");
90 else if (mode == MODE_YES)
91 printf("Will fix everything without asking\n");
92 if (debug)
93 printf("Debug level is %d\n", debug);
94
95 setlinebuf(stdout);
96
97 signal(SIGHUP, interrupt);
98 signal(SIGQUIT, interrupt);
99 signal(SIGINT, interrupt);
100 modtables = sq_create();
101
102 IIseterr(ingerr);
103 printf("Opening database %s...", database);
104 fflush(stdout);
208a4f4a 105 EXEC SQL CONNECT :database;
68bbc9c3 106 printf("done\n");
208a4f4a 107 EXEC SQL SELECT value INTO :dcmenable FROM numvalues
108 WHERE name='dcm_enable';
68bbc9c3 109 dprintf("DCM disabled (was %d)\n", dcmenable);
208a4f4a 110 EXEC SQL UPDATE numvalues SET value=0 WHERE name='dcm_enable';
68bbc9c3 111
208a4f4a 112 /* Begin transaction here. */
68bbc9c3 113
114 if (!countonly) {
115 phase1();
37229b80 116 EXEC SQL COMMIT WORK;
68bbc9c3 117 phase2();
37229b80 118 EXEC SQL COMMIT WORK;
68bbc9c3 119 phase3();
37229b80 120 EXEC SQL COMMIT WORK;
68bbc9c3 121 } else {
122 count_only_setup();
37229b80 123 EXEC SQL COMMIT WORK;
68bbc9c3 124 }
125 phase4();
126
208a4f4a 127 EXEC SQL COMMIT WORK;
128
68bbc9c3 129 cleanup();
130 printf("Done.\n");
131 exit(0);
132}
133
134ingerr(num)
135int *num;
136{
f0888cca 137 if (*num == 100) return;
68bbc9c3 138 printf("An ingres error occuurred, code %d\n", *num);
139 printf("Aborting...\n");
208a4f4a 140 if (!abort_p) {
141 abort_p++;
142 EXEC SQL ROLLBACK WORK;
68bbc9c3 143 }
144 exit(1);
145}
146
147
148int interrupt()
208a4f4a 149{
68bbc9c3 150 printf("Signal caught\n");
151 if (prompt("Save database changes")) {
152 /* break out of a retrieve loop */
153 IIbreak();
208a4f4a 154
155 EXEC SQL COMMIT WORK;
68bbc9c3 156 cleanup();
157 exit(0);
158 }
159 printf("Aborting transaction\n");
208a4f4a 160 if (!abort_p) {
161 abort_p++;
68bbc9c3 162 /* break out of a retrieve loop */
163 IIbreak();
208a4f4a 164 EXEC SQL ROLLBACK WORK;
68bbc9c3 165 }
208a4f4a 166
208a4f4a 167 EXEC SQL UPDATE numvalues SET value=:dcmenable
168 WHERE name='dcm_enable';
169
68bbc9c3 170 exit(0);
208a4f4a 171}
68bbc9c3 172
173
174modified(table)
175char *table;
176{
177 sq_save_unique_string(modtables, table);
178}
179
180cleanup()
208a4f4a 181{
182 EXEC SQL BEGIN DECLARE SECTION;
183 char *tab;
184 EXEC SQL END DECLARE SECTION;
68bbc9c3 185
186 while (sq_get_data(modtables, &tab)) {
208a4f4a 187 EXEC SQL REPEATED UPDATE tblstats SET modtime='now'
188 WHERE table_name = :tab;
68bbc9c3 189 }
208a4f4a 190 EXEC SQL UPDATE numvalues SET value = :dcmenable
191 WHERE name='dcm_enable';
208a4f4a 192}
68bbc9c3 193
194
195out_of_mem(msg)
196char *msg;
197{
198 fprintf(stderr, "Out of memory while %s\n", msg);
f0888cca 199 if (prompt("Save database changes")) {
200 /* break out of a retrieve loop */
201 IIbreak();
202
203 EXEC SQL COMMIT WORK;
204 cleanup();
205 exit(1);
206 }
207 printf("Aborting transaction\n");
208 IIbreak();
209 EXEC SQL ROLLBACK WORK;
68bbc9c3 210 exit(1);
211}
This page took 0.200796 seconds and 5 git commands to generate.