]> andersk Git - moira.git/blame - server/increment.dc
Version prior to beginning synchronization with opssrc version.
[moira.git] / server / increment.dc
CommitLineData
32cfe906 1/*
2 * $Source$
3 * $Author$
4 * $Header$
5 *
6 * Copyright (C) 1989 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
9 *
10 */
11
12#ifndef lint
13static char *rcsid_increment_dc = "$Header$";
14#endif lint
15
16#include <mit-copyright.h>
17#include <moira.h>
18#include "query.h"
19#include "mr_server.h"
45bf7573 20EXEC SQL INCLUDE sqlca;
32cfe906 21
22extern char *whoami;
23char *malloc();
24
25int inc_pid = 0;
26int inc_running = 0;
27time_t inc_started;
28
29#define MAXARGC 15
30
45bf7573 31EXEC SQL WHENEVER SQLERROR CALL ingerr;
32
32cfe906 33/* structures to save before args */
34static char beforeb[MAXARGC][ARGLEN];
35static char *before[MAXARGC];
36EXEC SQL BEGIN DECLARE SECTION;
37char *barg0, *barg1, *barg2, *barg3, *barg4;
38char *barg5, *barg6, *barg7, *barg8, *barg9;
39char *barg10, *barg11, *barg12, *barg13, *barg14;
40EXEC SQL END DECLARE SECTION;
41static int beforec;
42static char *beforetable;
43
44/* structures to save after args */
45static char afterb[MAXARGC][ARGLEN];
46static char *after[MAXARGC];
47EXEC SQL BEGIN DECLARE SECTION;
48char *aarg0, *aarg1, *aarg2, *aarg3, *aarg4;
49char *aarg5, *aarg6, *aarg7, *aarg8, *aarg9;
50char *aarg10, *aarg11, *aarg12, *aarg13, *aarg14;
51EXEC SQL END DECLARE SECTION;
52static int afterc;
53
54/* structures to save entire sets of incremental changes */
55struct save_queue *incremental_sq = NULL;
56struct save_queue *incremental_exec = NULL;
57struct iupdate {
58 char *table;
59 int beforec;
60 char **before;
61 int afterc;
62 char **after;
63 char *service;
64};
65
66
67incremental_init()
68{
69 int i;
70
71 for (i = 0; i < MAXARGC; i++) {
72 before[i] = &beforeb[i][0];
73 after[i] = &afterb[i][0];
74 }
75 barg0 = before[0];
76 barg1 = before[1];
77 barg2 = before[2];
78 barg3 = before[3];
79 barg4 = before[4];
80 barg5 = before[5];
81 barg6 = before[6];
82 barg7 = before[7];
83 barg8 = before[8];
84 barg9 = before[9];
85 barg10 = before[10];
86 barg11 = before[11];
87 barg12 = before[12];
88 barg13 = before[13];
89 barg14 = before[14];
90 aarg0 = after[0];
91 aarg1 = after[1];
92 aarg2 = after[2];
93 aarg3 = after[3];
94 aarg4 = after[4];
95 aarg5 = after[5];
96 aarg6 = after[6];
97 aarg7 = after[7];
98 aarg8 = after[8];
99 aarg9 = after[9];
100 aarg10 = after[10];
101 aarg11 = after[11];
102 aarg12 = after[12];
103 aarg13 = after[13];
104 aarg14 = after[14];
105 if (incremental_sq == NULL)
106 incremental_sq = sq_create();
107 if (incremental_exec == NULL)
108 incremental_exec = sq_create();
109}
110
111
112incremental_before(table, qual, argv)
113char *table;
45bf7573 114EXEC SQL BEGIN DECLARE SECTION;
32cfe906 115char *qual;
45bf7573 116EXEC SQL END DECLARE SECTION;
32cfe906 117char **argv;
118{
119 EXEC SQL BEGIN DECLARE SECTION;
120 int id;
121 EXEC SQL END DECLARE SECTION;
122
123 char buffer[512], *name;
124
125 beforetable = table;
126
127 if (!strcmp(table, "users")) {
45bf7573 128 EXEC SQL SELECT login, CHAR(uid), shell, last, first, middle,
129 CHAR(status), clearid, type
a313cad2 130 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
131 :barg7, :barg8
32cfe906 132 FROM users WHERE :qual;
133 beforec = 9;
134 } else if (!strcmp(table, "machine")) {
a313cad2 135 EXEC SQL SELECT name, type INTO :barg0, :barg1 FROM machine
136 WHERE :qual;
32cfe906 137 beforec = 2;
138 } else if (!strcmp(table, "cluster")) {
45bf7573 139 EXEC SQL SELECT name, description, location
140 INTO :barg0, :barg1, :barg2
a313cad2 141 FROM cluster WHERE :qual;
32cfe906 142 beforec = 3;
143 } else if (!strcmp(table, "mcmap")) {
144 strcpy(barg0, argv[0]);
145 strcpy(barg1, argv[1]);
146 beforec = 2;
147 } else if (!strcmp(table, "svc")) {
148 strcpy(barg0, argv[0]);
149 strcpy(barg1, argv[1]);
150 strcpy(barg2, argv[2]);
151 beforec = 3;
152 } else if (!strcmp(table, "filesys")) {
45bf7573 153 EXEC SQL SELECT label, type, CHAR(mach_id), name, mount, access,
99e09b48 154 comments, CHAR(owner), CHAR(owners), CHAR(createflg), lockertype
a313cad2 155 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
156 :barg7, :barg8, :barg9, :barg10
157 FROM filesys WHERE :qual;
32cfe906 158 beforec = 11;
159 } else if (!strcmp(table, "quota")) {
160 strcpy(barg0, "?");
161 strcpy(barg1, argv[1]);
162 strcpy(barg2, "?");
a313cad2 163 sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
32cfe906 164 qual = buffer;
a313cad2 165 EXEC SQL SELECT q.quota, fs.name INTO :barg3, :barg4
166 FROM quota q, filesys fs WHERE :qual;
32cfe906 167 beforec = 5;
168 } else if (!strcmp(table, "list")) {
45bf7573 169 EXEC SQL SELECT name, CHAR(active), CHAR(publicflg), CHAR(hidden),
170 CHAR(maillist), CHAR(grouplist), CHAR(gid), acl_type,
171 CHAR(acl_id), description
a313cad2 172 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
173 :barg7, :barg8, :barg9
174 FROM list WHERE :qual;
32cfe906 175 beforec = 10;
176 } else if (!strcmp(table, "members")) {
177 id = (int) argv[0];
45bf7573 178 EXEC SQL SELECT CHAR(grouplist) INTO :barg3 FROM list
a313cad2 179 WHERE list_id = :id;
32cfe906 180 name = malloc(0);
181 id_to_name(id, "LIST", &name);
182 strcpy(barg0, name);
183 strcpy(barg1, argv[1]);
184 id = (int) argv[2];
185 if (!strcmp(barg1, "USER")) {
186 id_to_name(id, barg1, &name);
187 } else if (!strcmp(barg1, "LIST")) {
188 id_to_name(id, barg1, &name);
189 } else if (!strcmp(barg1, "STRING")) {
190 id_to_name(id, barg1, &name);
191 } else if (!strcmp(barg1, "KERBEROS")) {
192 id_to_name(id, "STRING", &name);
193 }
194 strcpy(barg2, name);
195 free(name);
196 beforec = 4;
197 } /* else
198 com_err(whoami, 0, "unknown table in incremental_before"); */
a313cad2 199}
32cfe906 200
201
202incremental_clear_before()
203{
204 beforec = 0;
205}
206
207incremental_clear_after()
208{
209 incremental_after("clear", 0);
210}
211
212
213
a313cad2 214incremental_after(table, qual, argv)
a313cad2 215char *table;
45bf7573 216EXEC SQL BEGIN DECLARE SECTION;
a313cad2 217char *qual;
218EXEC SQL END DECLARE SECTION;
32cfe906 219char **argv;
a313cad2 220{
32cfe906 221 char buffer[2048], *name;
a313cad2 222EXEC SQL BEGIN DECLARE SECTION;
223 int id, i;
224EXEC SQL END DECLARE SECTION;
32cfe906 225 struct iupdate *iu;
226 char **copy_argv();
227
228 if (!strcmp(table, "users")) {
45bf7573 229 EXEC SQL SELECT login, CHAR(uid), shell, last, first, middle,
230 CHAR(status), clearid, type
a313cad2 231 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5,
232 :aarg6, :aarg7, :aarg8
233 FROM users WHERE :qual;
32cfe906 234 afterc = 9;
235 } else if (!strcmp(table, "machine")) {
a313cad2 236 EXEC SQL SELECT name, type INTO :aarg0, :aarg1
237 FROM machine WHERE :qual;
32cfe906 238 afterc = 2;
239 } else if (!strcmp(table, "cluster")) {
45bf7573 240 EXEC SQL SELECT name, description, location
241 INTO :aarg0, :aarg1, :aarg2
a313cad2 242 FROM cluster WHERE :qual;
32cfe906 243 afterc = 3;
244 } else if (!strcmp(table, "mcmap")) {
245 strcpy(aarg0, argv[0]);
246 strcpy(aarg1, argv[1]);
247 afterc = 2;
248 } else if (!strcmp(table, "svc")) {
249 strcpy(aarg0, argv[0]);
250 strcpy(aarg1, argv[1]);
251 strcpy(aarg2, argv[2]);
252 afterc = 3;
253 } else if (!strcmp(table, "filesys")) {
99e09b48 254 EXEC SQL SELECT CHAR(label), type, CHAR(mach_id), name, mount, access,
255 comments, CHAR(owner), CHAR(owners), CHAR(createflg), lockertype
a313cad2 256 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
257 :aarg7, :aarg8, :aarg9, :aarg10
258 FROM filesys fs WHERE :qual;
32cfe906 259 afterc = 11;
260 } else if (!strcmp(table, "quota")) {
261 strcpy(aarg0, "?");
262 strcpy(aarg1, argv[1]);
263 strcpy(aarg2, "?");
a313cad2 264 sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
32cfe906 265 qual = buffer;
45bf7573 266 EXEC SQL SELECT CHAR(q.quota), fs.name INTO :aarg3, :aarg4
a313cad2 267 FROM quota q, filesys fs WHERE :qual;
32cfe906 268 afterc = 5;
269 } else if (!strcmp(table, "list")) {
45bf7573 270 EXEC SQL SELECT name, CHAR(active), CHAR(publicflg), CHAR(hidden),
271 CHAR(maillist), CHAR(grouplist), CHAR(gid), acl_type,
272 CHAR(acl_id), description
a313cad2 273 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
274 :aarg7, :aarg8, :aarg9
275 FROM list WHERE :qual;
32cfe906 276 afterc = 10;
277 } else if (!strcmp(table, "members")) {
278 id = (int) argv[0];
45bf7573 279 EXEC SQL SELECT CHAR(grouplist) INTO :aarg3 FROM list
a313cad2 280 WHERE list_id = :id;
32cfe906 281 name = malloc(0);
282 id_to_name(id, "LIST", &name);
283 strcpy(aarg0, name);
284 strcpy(aarg1, argv[1]);
285 id = (int) argv[2];
286 if (!strcmp(aarg1, "USER")) {
287 id_to_name(id, aarg1, &name);
288 } else if (!strcmp(aarg1, "LIST")) {
289 id_to_name(id, aarg1, &name);
290 } else if (!strcmp(aarg1, "STRING")) {
291 id_to_name(id, aarg1, &name);
292 } else if (!strcmp(aarg1, "KERBEROS")) {
293 id_to_name(id, "STRING", &name);
294 }
295 strcpy(aarg2, name);
296 free(name);
297 afterc = 4;
298 } else if (!strcmp(table, "clear")) {
299 afterc = 0;
300 table = beforetable;
301 } /* else
302 com_err(whoami, 0, "unknown table in incremental_after"); */
303
304 iu = (struct iupdate *) malloc(sizeof(struct iupdate));
305 iu->table = strsave(table);
306 iu->beforec = beforec;
307 iu->before = copy_argv(before, beforec);
308 iu->afterc = afterc;
309 iu->after = copy_argv(after, afterc);
310 sq_save_data(incremental_sq, iu);
311
312#ifdef DEBUG
313 sprintf(buffer, "INCREMENTAL(%s, [", table);
314 for (i = 0; i < beforec; i++) {
315 if (i == 0)
316 strcat(buffer, strtrim(before[0]));
317 else {
318 strcat(buffer, ", ");
319 strcat(buffer, strtrim(before[i]));
320 }
321 }
322 strcat(buffer, "], [");
323 for (i = 0; i < afterc; i++) {
324 if (i == 0)
325 strcat(buffer, strtrim(after[0]));
326 else {
327 strcat(buffer, ", ");
328 strcat(buffer, strtrim(after[i]));
329 }
330 }
331 strcat(buffer, "])");
332 com_err(whoami, 0, buffer);
333#endif DEBUG
a313cad2 334}
32cfe906 335
336
337/* Called when the current transaction is committed to start any queued
338 * incremental updates. This caches the update table the first time it
339 * is called.
340 */
341
342struct inc_cache {
343 struct inc_cache *next;
344 char *table;
345 char *service;
346};
347
348
349incremental_update()
350{
351 static int inited = 0;
352 static struct inc_cache *cache;
353 struct inc_cache *c;
354 EXEC SQL BEGIN DECLARE SECTION;
355 char tab[17], serv[17];
356 EXEC SQL END DECLARE SECTION;
357 struct iupdate *iu;
358
359 if (!inited) {
360 inited++;
361
45bf7573 362 EXEC SQL DECLARE inc CURSOR FOR SELECT tablename, service FROM incremental;
32cfe906 363 EXEC SQL OPEN inc;
364 while (1) {
365 EXEC SQL FETCH inc INTO :tab, :serv;
366 if (sqlca.sqlcode != 0) break;
367 c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
368 c->next = cache;
369 c->table = strsave(strtrim(tab));
370 c->service = strsave(strtrim(serv));
371 cache = c;
372 }
373 EXEC SQL CLOSE inc;
374 }
375
376 while (sq_remove_data(incremental_sq, &iu)) {
377 for (c = cache; c; c = c->next) {
378 if (!strcmp(c->table, iu->table)) {
379 iu->service = c->service;
380 sq_save_data(incremental_exec, iu);
381 }
382 }
383 }
384 if (inc_running == 0)
385 next_incremental();
386}
387
388
389next_incremental()
390{
391 struct iupdate *iu;
392 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
393 int i;
394
395 if (incremental_exec == NULL)
396 incremental_init();
397
398 if (sq_empty(incremental_exec) ||
399 (inc_running && now - inc_started < INC_TIMEOUT))
400 return;
401
402 if (inc_running)
403 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
404
405 sq_remove_data(incremental_exec, &iu);
406 argv[1] = iu->table;
407 sprintf(cbefore, "%d", iu->beforec);
408 argv[2] = cbefore;
409 sprintf(cafter, "%d", iu->afterc);
410 argv[3] = cafter;
411 for (i = 0; i < iu->beforec; i++)
412 argv[4 + i] = iu->before[i];
413 for (i = 0; i < iu->afterc; i++)
414 argv[4 + iu->beforec + i] = iu->after[i];
415
416 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
417#ifdef DEBUG
418 com_err(whoami, 0, "forking %s", prog);
419#endif
420 argv[0] = prog;
421 argv[4 + iu->beforec + iu->afterc] = 0;
422 inc_pid = vfork();
423 switch (inc_pid) {
424 case 0:
425 execv(prog, argv);
426 exit(1);
427 case -1:
428 com_err(whoami, 0, "Failed to start incremental update");
429 break;
430 default:
431 inc_running = 1;
432 inc_started = now;
433 }
434
435 free_argv(iu->before, iu->beforec);
436 free_argv(iu->after, iu->afterc);
437 free(iu->table);
438 free(iu);
439
440}
441
442
443/* Called when the current transaction is aborted to throw away any queued
444 * incremental updates
445 */
446
447incremental_flush()
448{
449 struct iupdate *iu;
450
451 while (sq_get_data(incremental_sq, &iu)) {
452 free_argv(iu->before, iu->beforec);
453 free_argv(iu->after, iu->afterc);
454 free(iu->table);
455 free(iu);
456 }
457 sq_destroy(incremental_sq);
458 incremental_sq = sq_create();
459}
460
461
462char **copy_argv(argv, argc)
463char **argv;
464int argc;
465{
466 char **ret = (char **)malloc(sizeof(char *) * argc);
467 while (--argc >= 0)
468 ret[argc] = strsave(strtrim(argv[argc]));
469 return(ret);
470}
471
472free_argv(argv, argc)
473char **argv;
474int argc;
475{
476 while (--argc >= 0)
477 free(argv[argc]);
478 free(argv);
479}
This page took 0.109979 seconds and 5 git commands to generate.