]> andersk Git - moira.git/blame - dbck/dbck.pc
Check for uniqueness of subnet, filesys, and printer names. (These
[moira.git] / dbck / dbck.pc
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>
23a784eb 12#include <string.h>
68bbc9c3 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
23a784eb 35extern SQLDA *sqlald(int,int,int);
68bbc9c3 36
37main(argc, argv)
38int argc;
39char **argv;
40{
41 char **arg = argv;
208a4f4a 42EXEC SQL BEGIN DECLARE SECTION;
43 char *database;
44EXEC SQL END DECLARE SECTION;
68bbc9c3 45 int ingerr();
46 int countonly = 0;
47
208a4f4a 48 database = "moira";
4b9e5c72 49 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
68bbc9c3 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
68bbc9c3 99 signal(SIGHUP, interrupt);
100 signal(SIGQUIT, interrupt);
101 signal(SIGINT, interrupt);
4b9e5c72 102
68bbc9c3 103 modtables = sq_create();
4b9e5c72 104 mr_sqlda = sqlald(1, 255, 0);
68bbc9c3 105
4b9e5c72 106 EXEC SQL WHENEVER SQLERROR DO dbmserr();
68bbc9c3 107 printf("Opening database %s...", database);
108 fflush(stdout);
4b9e5c72 109 EXEC SQL CONNECT :database IDENTIFIED BY :database;
68bbc9c3 110 printf("done\n");
208a4f4a 111 EXEC SQL SELECT value INTO :dcmenable FROM numvalues
112 WHERE name='dcm_enable';
68bbc9c3 113 dprintf("DCM disabled (was %d)\n", dcmenable);
208a4f4a 114 EXEC SQL UPDATE numvalues SET value=0 WHERE name='dcm_enable';
68bbc9c3 115
208a4f4a 116 /* Begin transaction here. */
68bbc9c3 117
118 if (!countonly) {
119 phase1();
37229b80 120 EXEC SQL COMMIT WORK;
68bbc9c3 121 phase2();
37229b80 122 EXEC SQL COMMIT WORK;
68bbc9c3 123 phase3();
37229b80 124 EXEC SQL COMMIT WORK;
68bbc9c3 125 } else {
126 count_only_setup();
37229b80 127 EXEC SQL COMMIT WORK;
68bbc9c3 128 }
129 phase4();
208a4f4a 130 EXEC SQL COMMIT WORK;
131
68bbc9c3 132 cleanup();
133 printf("Done.\n");
134 exit(0);
135}
136
4b9e5c72 137dbmserr()
68bbc9c3 138{
ab05f33a 139EXEC SQL BEGIN DECLARE SECTION;
140 char buf[512];
141EXEC SQL END DECLARE SECTION;
4b9e5c72 142 int bufsize=256, msglength=0;
ab05f33a 143
4b9e5c72 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;
ab05f33a 148 printf("%s\n", buf);
68bbc9c3 149 printf("Aborting...\n");
208a4f4a 150 if (!abort_p) {
151 abort_p++;
152 EXEC SQL ROLLBACK WORK;
68bbc9c3 153 }
154 exit(1);
155}
156
157
4b9e5c72 158void interrupt()
208a4f4a 159{
68bbc9c3 160 printf("Signal caught\n");
161 if (prompt("Save database changes")) {
208a4f4a 162 EXEC SQL COMMIT WORK;
68bbc9c3 163 cleanup();
164 exit(0);
165 }
166 printf("Aborting transaction\n");
208a4f4a 167 if (!abort_p) {
168 abort_p++;
208a4f4a 169 EXEC SQL ROLLBACK WORK;
68bbc9c3 170 }
208a4f4a 171
208a4f4a 172 EXEC SQL UPDATE numvalues SET value=:dcmenable
173 WHERE name='dcm_enable';
174
68bbc9c3 175 exit(0);
208a4f4a 176}
68bbc9c3 177
178
179modified(table)
180char *table;
181{
182 sq_save_unique_string(modtables, table);
183}
184
185cleanup()
208a4f4a 186{
187 EXEC SQL BEGIN DECLARE SECTION;
188 char *tab;
189 EXEC SQL END DECLARE SECTION;
68bbc9c3 190
191 while (sq_get_data(modtables, &tab)) {
4b9e5c72 192 EXEC SQL UPDATE tblstats SET modtime=SYSDATE
208a4f4a 193 WHERE table_name = :tab;
68bbc9c3 194 }
208a4f4a 195 EXEC SQL UPDATE numvalues SET value = :dcmenable
196 WHERE name='dcm_enable';
208a4f4a 197}
68bbc9c3 198
199
200out_of_mem(msg)
201char *msg;
202{
203 fprintf(stderr, "Out of memory while %s\n", msg);
f0888cca 204 if (prompt("Save database changes")) {
f0888cca 205 EXEC SQL COMMIT WORK;
206 cleanup();
207 exit(1);
208 }
209 printf("Aborting transaction\n");
f0888cca 210 EXEC SQL ROLLBACK WORK;
68bbc9c3 211 exit(1);
212}
This page took 0.09207 seconds and 5 git commands to generate.