]> andersk Git - moira.git/blame_incremental - dbck/dbck.pc
Document new blanche exit status semantics. (Don't claim success
[moira.git] / dbck / dbck.pc
... / ...
CommitLineData
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"
15EXEC SQL INCLUDE sqlca;
16EXEC SQL INCLUDE sqlda;
17
18static char dbck_qc_rcsid[] = "$Header$";
19
20
21int debug = 0;
22int mode = MODE_ASK;
23int fast = 0;
24int warn = 1;
25int abort_p = 0;
26struct hash *users, *machines, *clusters, *lists, *filesys, *nfsphys;
27struct hash *strings, *members, *subnets, *string_dups;
28EXEC SQL BEGIN DECLARE SECTION;
29int dcmenable;
30EXEC SQL END DECLARE SECTION;
31struct save_queue *modtables, *sq_create();
32void interrupt();
33SQLDA *mr_sqlda;
34
35extern SQLDA *sqlald(int,int,int);
36
37main(argc, argv)
38int argc;
39char **argv;
40{
41 char **arg = argv;
42EXEC SQL BEGIN DECLARE SECTION;
43 char *database;
44EXEC 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
137dbmserr()
138{
139EXEC SQL BEGIN DECLARE SECTION;
140 char buf[512];
141EXEC 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
158void 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
179modified(table)
180char *table;
181{
182 sq_save_unique_string(modtables, table);
183}
184
185cleanup()
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
200out_of_mem(msg)
201char *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.036579 seconds and 5 git commands to generate.