]> andersk Git - moira.git/blame - server/increment.pc
Give version 2 queries their own validate struct so they still work.
[moira.git] / server / increment.pc
CommitLineData
7ac48069 1/* $Id$
32cfe906 2 *
7ac48069 3 * Deal with incremental updates
5eaef520 4 *
7ac48069 5 * Copyright (C) 1989-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
32cfe906 8 */
9
32cfe906 10#include <mit-copyright.h>
32cfe906 11#include "mr_server.h"
03c05291 12#include "query.h"
13#include "qrtn.h"
7ac48069 14
15#include <signal.h>
85330553 16#include <stdio.h>
7ac48069 17#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
20
45bf7573 21EXEC SQL INCLUDE sqlca;
32cfe906 22
7ac48069 23RCSID("$Header$");
24
32cfe906 25extern char *whoami;
03c05291 26extern char *table_name[];
27extern int num_tables;
32cfe906 28
29int inc_pid = 0;
30int inc_running = 0;
31time_t inc_started;
32
33#define MAXARGC 15
34
03c05291 35EXEC SQL WHENEVER SQLERROR DO dbmserr();
45bf7573 36
32cfe906 37/* structures to save before args */
32cfe906 38static char *before[MAXARGC];
32cfe906 39static int beforec;
03c05291 40static enum tables beforetable;
32cfe906 41
42/* structures to save after args */
32cfe906 43static char *after[MAXARGC];
32cfe906 44static int afterc;
45
46/* structures to save entire sets of incremental changes */
47struct save_queue *incremental_sq = NULL;
48struct save_queue *incremental_exec = NULL;
49struct iupdate {
5eaef520 50 char *table;
51 int beforec;
52 char **before;
53 int afterc;
54 char **after;
55 char *service;
32cfe906 56};
57
03c05291 58void next_incremental(void);
fc053494 59char **copy_argv(char **argv, int argc);
03c05291 60void free_argv(char **argv, int argc);
61int table_num(char *table);
32cfe906 62
03c05291 63void incremental_init(void)
32cfe906 64{
5eaef520 65 int i;
32cfe906 66
5eaef520 67 if (!incremental_sq)
68 incremental_sq = sq_create();
69 if (!incremental_exec)
70 incremental_exec = sq_create();
03c05291 71
5eaef520 72 for (i = 0; i < MAXARGC; i++)
73 {
e688520a 74 before[i] = xmalloc(MAX_FIELD_WIDTH);
75 after[i] = xmalloc(MAX_FIELD_WIDTH);
03c05291 76 }
32cfe906 77}
78
79
03c05291 80/* record the state of a table row before it is changed */
81
5eaef520 82void incremental_before(enum tables table, char *qual, char **argv)
32cfe906 83{
5eaef520 84 EXEC SQL BEGIN DECLARE SECTION;
85 int id;
86 EXEC SQL END DECLARE SECTION;
87
88acd4d3 88 char *name, *name2;
5eaef520 89
90 beforetable = table;
91
92 switch (table)
93 {
94 case USERS_TABLE:
d6759517 95 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
96 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
88acd4d3 97 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
5eaef520 98 dosql(before);
88acd4d3 99 beforec = 11;
5eaef520 100 break;
101 case MACHINE_TABLE:
88acd4d3 102 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
5eaef520 103 "WHERE %s", qual);
104 dosql(before);
88acd4d3 105 beforec = 3;
5eaef520 106 break;
e688520a 107 case CLUSTERS_TABLE:
88acd4d3 108 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
109 "c.clu_id FROM clusters c WHERE %s", qual);
5eaef520 110 dosql(before);
88acd4d3 111 beforec = 4;
5eaef520 112 break;
bb5c9457 113 case CONTAINERS_TABLE:
114 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
115 "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
116 qual);
117 dosql(before);
118 beforec = 7;
429aec78 119 name = xmalloc(0);
120 id = atoi(before[5]);
121 if (!strncmp(before[4], "USER", 4))
122 {
123 id_to_name(id, USERS_TABLE, &name);
124 strcpy(before[5], name);
125 }
126 else if (!strncmp(before[4], "LIST", 4))
127 {
128 id_to_name(id, LIST_TABLE, &name);
129 strcpy(before[5], name);
130 }
131 else if (!strncmp(before[4], "KERBEROS", 8))
132 {
133 id_to_name(id, STRINGS_TABLE, &name);
134 strcpy(before[5], name);
135 }
bb5c9457 136 break;
5eaef520 137 case MCMAP_TABLE:
138 strcpy(before[0], argv[0]);
139 strcpy(before[1], argv[1]);
140 beforec = 2;
141 break;
142 case SVC_TABLE:
143 strcpy(before[0], argv[0]);
144 strcpy(before[1], argv[1]);
145 strcpy(before[2], argv[2]);
146 beforec = 3;
147 break;
148 case FILESYS_TABLE:
149 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
150 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
88acd4d3 151 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
152 "WHERE %s", qual);
5eaef520 153 dosql(before);
88acd4d3 154 name = xmalloc(0);
5eaef520 155 id = atoi(before[2]);
156 id_to_name(id, MACHINE_TABLE, &name);
157 strcpy(before[2], name);
158 id = atoi(before[7]);
159 id_to_name(id, USERS_TABLE, &name);
160 strcpy(before[7], name);
161 id = atoi(before[8]);
162 id_to_name(id, LIST_TABLE, &name);
163 strcpy(before[8], name);
164 free(name);
88acd4d3 165 beforec = 12;
5eaef520 166 break;
167 case QUOTA_TABLE:
168 strcpy(before[0], "?");
169 strcpy(before[1], argv[1]);
170 strcpy(before[2], "?");
171 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
172 "WHERE %s AND fs.filsys_id = q.filsys_id", qual);
173 dosql(&(before[3]));
174 strcpy(before[2], argv[1]);
175 beforec = 5;
176 break;
177 case LIST_TABLE:
178 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
179 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
88acd4d3 180 "l.description, l.list_id FROM list l WHERE %s", qual);
5eaef520 181 dosql(before);
88acd4d3 182 beforec = 11;
5eaef520 183 break;
184 case IMEMBERS_TABLE:
185 id = (int) argv[0];
186 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
187 "grouplist, gid FROM list WHERE list_id = %d", id);
188 dosql(&(before[3]));
88acd4d3 189 name = xmalloc(0);
5eaef520 190 id_to_name(id, LIST_TABLE, &name);
88acd4d3 191 name2 = xmalloc(0);
5eaef520 192 strcpy(before[0], name);
193 strcpy(before[1], argv[1]);
194 id = (int) argv[2];
88acd4d3 195 beforec = 10;
5eaef520 196 if (!strcmp(before[1], "USER"))
197 {
88acd4d3 198 id_to_name(id, USERS_TABLE, &name2);
199 EXEC SQL SELECT status, users_id INTO :before[9], :before[11]
200 FROM users WHERE users_id = :id;
201 EXEC SQL SELECT list_id INTO :before[10] FROM list
202 WHERE name = :name;
203 beforec = 12;
5eaef520 204 }
205 else if (!strcmp(before[1], "LIST"))
88acd4d3 206 {
207 id_to_name(id, LIST_TABLE, &name2);
208 EXEC SQL SELECT list_id INTO :before[9] FROM list
209 WHERE name = :name;
210 sprintf(before[10], "%d", id);
211 beforec = 11;
212 }
5eaef520 213 else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
88acd4d3 214 {
215 id_to_name(id, STRINGS_TABLE, &name2);
216 EXEC SQL SELECT list_id INTO :before[9] FROM list
217 WHERE name = :name;
218 }
219 strcpy(before[2], name2);
5eaef520 220 free(name);
88acd4d3 221 free(name2);
5eaef520 222 break;
223 default:
03c05291 224 /*
225 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
226 table_name[table]);
227 */
5eaef520 228 break;
229 }
a313cad2 230}
32cfe906 231
232
03c05291 233void incremental_clear_before(void)
32cfe906 234{
5eaef520 235 beforec = 0;
32cfe906 236}
237
32cfe906 238
03c05291 239/* add an element to the incremental queue for the changed row */
32cfe906 240
5eaef520 241void incremental_after(enum tables table, char *qual, char **argv)
a313cad2 242{
88acd4d3 243 char *name, *name2;
5eaef520 244 EXEC SQL BEGIN DECLARE SECTION;
245 int id;
246 EXEC SQL END DECLARE SECTION;
247 struct iupdate *iu;
248
249 switch (table)
250 {
251 case USERS_TABLE:
d6759517 252 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
253 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
88acd4d3 254 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
5eaef520 255 dosql(after);
88acd4d3 256 afterc = 11;
5eaef520 257 break;
258 case MACHINE_TABLE:
88acd4d3 259 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
5eaef520 260 "WHERE %s", qual);
261 dosql(after);
88acd4d3 262 afterc = 3;
5eaef520 263 break;
e688520a 264 case CLUSTERS_TABLE:
88acd4d3 265 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
266 "c.clu_id FROM clusters c WHERE %s", qual);
5eaef520 267 dosql(after);
88acd4d3 268 afterc = 4;
5eaef520 269 break;
bb5c9457 270 case CONTAINERS_TABLE:
271 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
272 "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
273 qual);
274 dosql(after);
275 afterc = 7;
429aec78 276 name = xmalloc(0);
277 id = atoi(after[5]);
278 if (!strncmp(after[4], "USER", 4))
279 {
280 id_to_name(id, USERS_TABLE, &name);
281 strcpy(after[5], name);
282 }
283 else if (!strncmp(after[4], "LIST", 4))
284 {
285 id_to_name(id, LIST_TABLE, &name);
286 strcpy(after[5], name);
287 }
288 else if (!strncmp(after[4], "KERBEROS", 8))
289 {
290 id_to_name(id, STRINGS_TABLE, &name);
291 strcpy(after[5], name);
292 }
bb5c9457 293 break;
5eaef520 294 case MCMAP_TABLE:
295 strcpy(after[0], argv[0]);
296 strcpy(after[1], argv[1]);
297 afterc = 2;
298 break;
299 case SVC_TABLE:
300 strcpy(after[0], argv[0]);
301 strcpy(after[1], argv[1]);
302 strcpy(after[2], argv[2]);
303 afterc = 3;
304 break;
305 case FILESYS_TABLE:
306 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
307 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
88acd4d3 308 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
309 "WHERE %s", qual);
5eaef520 310 dosql(after);
88acd4d3 311 name = xmalloc(0);
5eaef520 312 id = atoi(after[2]);
313 id_to_name(id, MACHINE_TABLE, &name);
314 strcpy(after[2], name);
315 id = atoi(after[7]);
316 id_to_name(id, USERS_TABLE, &name);
317 strcpy(after[7], name);
318 id = atoi(after[8]);
319 id_to_name(id, LIST_TABLE, &name);
320 strcpy(after[8], name);
321 free(name);
88acd4d3 322 afterc = 12;
5eaef520 323 break;
324 case QUOTA_TABLE:
325 strcpy(after[0], "?");
326 strcpy(after[1], argv[1]);
327 strcpy(after[2], "?");
328 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
329 "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
330 qual, argv[1]);
331 dosql(&(after[3]));
332 afterc = 5;
333 break;
334 case LIST_TABLE:
335 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
336 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
88acd4d3 337 "l.description, l.list_id FROM list l WHERE %s", qual);
5eaef520 338 dosql(after);
88acd4d3 339 afterc = 11;
5eaef520 340 break;
341 case IMEMBERS_TABLE:
342 id = (int) argv[0];
343 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
344 "grouplist, gid FROM list WHERE list_id = %d", id);
345 dosql(&(after[3]));
88acd4d3 346 name = xmalloc(0);
5eaef520 347 id_to_name(id, LIST_TABLE, &name);
88acd4d3 348 name2 = xmalloc(0);
5eaef520 349 strcpy(after[0], name);
350 strcpy(after[1], argv[1]);
351 id = (int) argv[2];
88acd4d3 352 afterc = 10;
5eaef520 353 if (!strcmp(after[1], "USER"))
354 {
88acd4d3 355 id_to_name(id, USERS_TABLE, &name2);
356 EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
357 FROM users WHERE users_id = :id;
358 EXEC SQL SELECT list_id INTO :after[10] FROM list
359 WHERE name = :name;
360 afterc = 12;
32cfe906 361 }
5eaef520 362 else if (!strcmp(after[1], "LIST"))
88acd4d3 363 {
364 id_to_name(id, LIST_TABLE, &name2);
365 EXEC SQL SELECT list_id INTO :after[9] FROM list
366 WHERE name = :name;
367 sprintf(after[10], "%d", id);
368 afterc = 11;
369 }
5eaef520 370 else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
88acd4d3 371 {
372 id_to_name(id, STRINGS_TABLE, &name2);
373 EXEC SQL SELECT list_id INTO :after[9] FROM list
374 WHERE name = :name;
375 }
376 strcpy(after[2], name2);
5eaef520 377 free(name);
88acd4d3 378 free(name2);
5eaef520 379 break;
380 case NO_TABLE:
381 afterc = 0;
382 table = beforetable;
383 break;
384 default:
03c05291 385 /*
386 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
387 table_name[table]);
388 */
5eaef520 389 break;
32cfe906 390 }
5eaef520 391
e688520a 392 iu = xmalloc(sizeof(struct iupdate));
5eaef520 393 iu->table = table_name[table];
394 iu->beforec = beforec;
fc053494 395 iu->before = copy_argv(before, beforec);
5eaef520 396 iu->afterc = afterc;
fc053494 397 iu->after = copy_argv(after, afterc);
5eaef520 398 sq_save_data(incremental_sq, iu);
a313cad2 399}
32cfe906 400
03c05291 401void incremental_clear_after(void)
402{
5eaef520 403 incremental_after(NO_TABLE, NULL, NULL);
03c05291 404}
405
32cfe906 406
407/* Called when the current transaction is committed to start any queued
408 * incremental updates. This caches the update table the first time it
409 * is called.
410 */
411
412struct inc_cache {
5eaef520 413 struct inc_cache *next;
414 char *table, *service;
32cfe906 415};
416
417
03c05291 418void incremental_update(void)
32cfe906 419{
5eaef520 420 static int inited = 0;
421 static struct inc_cache *cache;
422 struct inc_cache *c;
423 EXEC SQL BEGIN DECLARE SECTION;
e688520a 424 char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
5eaef520 425 EXEC SQL END DECLARE SECTION;
772c26b3 426 struct iupdate *iu, *iu_save;
5eaef520 427
428 if (!inited)
429 {
430 inited++;
431
432 EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
433 FROM incremental;
434 EXEC SQL OPEN inc;
435 while (1)
436 {
437 EXEC SQL FETCH inc INTO :tab, :serv;
438 if (sqlca.sqlcode)
439 break;
e688520a 440 c = xmalloc(sizeof(struct inc_cache));
5eaef520 441 c->next = cache;
e688520a 442 c->table = xstrdup(strtrim(tab));
443 c->service = xstrdup(strtrim(serv));
5eaef520 444 cache = c;
32cfe906 445 }
5eaef520 446 EXEC SQL CLOSE inc;
447 EXEC SQL COMMIT WORK;
32cfe906 448 }
449
5eaef520 450 while (sq_remove_data(incremental_sq, &iu))
451 {
452 for (c = cache; c; c = c->next)
453 {
454 if (!strcmp(c->table, iu->table))
455 {
456 iu->service = c->service;
772c26b3 457 iu_save = xmalloc(sizeof(struct iupdate));
458 iu_save->service = iu->service;
459 iu_save->table = iu->table;
460 iu_save->beforec = iu->beforec;
461 iu_save->afterc = iu->afterc;
462 iu_save->before = copy_argv(iu->before, iu->beforec);
463 iu_save->after = copy_argv(iu->after, iu->afterc);
464 sq_save_data(incremental_exec, iu_save);
32cfe906 465 }
466 }
e688520a 467 if (!c)
468 {
469 free_argv(iu->before, iu->beforec);
470 free_argv(iu->after, iu->afterc);
471 free(iu);
472 }
32cfe906 473 }
5eaef520 474 if (inc_running == 0)
475 next_incremental();
32cfe906 476}
477
f18a32ee 478/* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
479 We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
480 this function so proc will pass it through without reading it. */
32cfe906 481
f18a32ee 482#ifndef _PROC_
03c05291 483void next_incremental(void)
32cfe906 484{
5eaef520 485 struct iupdate *iu;
e688520a 486 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
5eaef520 487 int i;
488 sigset_t sigs;
489
490 if (!incremental_exec)
491 incremental_init();
492
493 if (sq_empty(incremental_exec) ||
494 (inc_running && now - inc_started < INC_TIMEOUT))
495 return;
496
497 if (inc_running)
498 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
499
500 sq_remove_data(incremental_exec, &iu);
501 argv[1] = iu->table;
502 sprintf(cbefore, "%d", iu->beforec);
503 argv[2] = cbefore;
504 sprintf(cafter, "%d", iu->afterc);
505 argv[3] = cafter;
506 for (i = 0; i < iu->beforec; i++)
507 argv[4 + i] = iu->before[i];
508 for (i = 0; i < iu->afterc; i++)
509 argv[4 + iu->beforec + i] = iu->after[i];
510
511 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
512 argv[0] = prog;
513 argv[4 + iu->beforec + iu->afterc] = 0;
514
515 sigemptyset(&sigs);
516 sigaddset(&sigs, SIGCHLD);
517 sigprocmask(SIG_BLOCK, &sigs, NULL);
518 inc_pid = vfork();
519 switch (inc_pid)
520 {
32cfe906 521 case 0:
5eaef520 522 execv(prog, argv);
523 _exit(1);
32cfe906 524 case -1:
5eaef520 525 com_err(whoami, 0, "Failed to start incremental update");
526 break;
32cfe906 527 default:
5eaef520 528 inc_running = 1;
529 inc_started = now;
32cfe906 530 }
5eaef520 531 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
32cfe906 532
5eaef520 533 free_argv(iu->before, iu->beforec);
534 free_argv(iu->after, iu->afterc);
535 free(iu);
32cfe906 536}
f18a32ee 537#endif
32cfe906 538
539/* Called when the current transaction is aborted to throw away any queued
540 * incremental updates
541 */
542
03c05291 543void incremental_flush(void)
32cfe906 544{
5eaef520 545 struct iupdate *iu;
32cfe906 546
5eaef520 547 while (sq_get_data(incremental_sq, &iu))
548 {
549 free_argv(iu->before, iu->beforec);
550 free_argv(iu->after, iu->afterc);
551 free(iu);
32cfe906 552 }
5eaef520 553 sq_destroy(incremental_sq);
554 incremental_sq = sq_create();
32cfe906 555}
556
557
fc053494 558char **copy_argv(char **argv, int argc)
559{
560 char **ret = xmalloc(sizeof(char *) * argc);
561 while (--argc >= 0)
562 ret[argc] = xstrdup(strtrim(argv[argc]));
563 return ret;
564}
565
5eaef520 566void free_argv(char **argv, int argc)
32cfe906 567{
5eaef520 568 while (--argc >= 0)
569 free(argv[argc]);
570 free(argv);
32cfe906 571}
03c05291 572
573int table_num(char *name)
574{
575 int i;
576
5eaef520 577 for (i = num_tables - 1; i; i--)
578 {
579 if (!strcmp(table_name[i], name))
580 break;
581 }
03c05291 582
583 return i; /* 0 = "none" if no match */
584}
This page took 0.176259 seconds and 5 git commands to generate.