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