3 * Deal with incremental updates
5 * Copyright (C) 1989-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
11 #include "mr_server.h"
21 EXEC SQL INCLUDE sqlca;
26 extern char *table_name[];
27 extern int num_tables;
35 EXEC SQL WHENEVER SQLERROR DO dbmserr();
37 EXEC SQL BEGIN DECLARE SECTION;
38 /* structures to save before args */
39 static char *before[MAXARGC];
41 static enum tables beforetable;
43 /* structures to save after args */
44 static char *after[MAXARGC];
46 EXEC SQL END DECLARE SECTION;
48 /* structures to save entire sets of incremental changes */
49 struct save_queue *incremental_sq = NULL;
50 struct save_queue *incremental_exec = NULL;
60 void next_incremental(void);
61 char **copy_argv(char **argv, int argc);
62 void free_argv(char **argv, int argc);
63 int table_num(char *table);
65 void incremental_init(void)
70 incremental_sq = sq_create();
71 if (!incremental_exec)
72 incremental_exec = sq_create();
74 for (i = 0; i < MAXARGC; i++)
76 before[i] = xmalloc(MAX_FIELD_WIDTH);
77 after[i] = xmalloc(MAX_FIELD_WIDTH);
82 /* record the state of a table row before it is changed */
84 void incremental_before(enum tables table, char *qual, char **argv)
86 EXEC SQL BEGIN DECLARE SECTION;
88 EXEC SQL END DECLARE SECTION;
97 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
98 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
99 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
104 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
110 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
111 "c.clu_id FROM clusters c WHERE %s", qual);
115 case CONTAINERS_TABLE:
116 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
117 "c.acl_type, c.acl_id, c.cnt_id, c.list_id FROM containers c "
122 id = atoi(before[5]);
123 if (!strncmp(before[4], "USER", 4))
125 id_to_name(id, USERS_TABLE, &name);
126 strcpy(before[5], name);
128 else if (!strncmp(before[4], "LIST", 4))
130 id_to_name(id, LIST_TABLE, &name);
131 strcpy(before[5], name);
133 else if (!strncmp(before[4], "KERBEROS", 8))
135 id_to_name(id, STRINGS_TABLE, &name);
136 strcpy(before[5], name);
138 id = atoi(before[7]);
139 id_to_name(id, LIST_TABLE, &name);
140 strcpy(before[7], name);
143 strcpy(before[0], argv[0]);
144 strcpy(before[1], argv[1]);
148 strcpy(before[0], argv[0]);
149 strcpy(before[1], argv[1]);
150 name_to_id(before[0], MACHINE_TABLE, &id);
151 sprintf(before[2], "%d", id);
152 name_to_id(before[1], CONTAINERS_TABLE, &id);
153 sprintf(before[3], "%d", id);
155 EXEC SQL SELECT list_id INTO :before[4] FROM containers
157 id = atoi(before[4]);
158 id_to_name(id, LIST_TABLE, &name);
159 strcpy(before[4], name);
163 strcpy(before[0], argv[0]);
164 strcpy(before[1], argv[1]);
165 strcpy(before[2], argv[2]);
169 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
170 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
171 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
175 id = atoi(before[2]);
176 id_to_name(id, MACHINE_TABLE, &name);
177 strcpy(before[2], name);
178 id = atoi(before[7]);
179 id_to_name(id, USERS_TABLE, &name);
180 strcpy(before[7], name);
181 id = atoi(before[8]);
182 id_to_name(id, LIST_TABLE, &name);
183 strcpy(before[8], name);
188 strcpy(before[0], "?");
189 strcpy(before[1], argv[1]);
190 strcpy(before[2], "?");
191 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
192 "WHERE %s AND fs.filsys_id = q.filsys_id", qual);
194 strcpy(before[2], argv[1]);
198 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
199 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
200 "l.description, l.list_id FROM list l WHERE %s", qual);
206 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
207 "grouplist, gid FROM list WHERE list_id = %d", id);
210 id_to_name(id, LIST_TABLE, &name);
212 strcpy(before[0], name);
213 strcpy(before[1], argv[1]);
216 if (!strcmp(before[1], "USER"))
218 id_to_name(id, USERS_TABLE, &name2);
219 EXEC SQL SELECT status, users_id INTO :before[9], :before[11]
220 FROM users WHERE users_id = :id;
221 EXEC SQL SELECT list_id INTO :before[10] FROM list
225 else if (!strcmp(before[1], "LIST"))
227 id_to_name(id, LIST_TABLE, &name2);
228 EXEC SQL SELECT list_id INTO :before[9] FROM list
230 sprintf(before[10], "%d", id);
233 else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
235 id_to_name(id, STRINGS_TABLE, &name2);
236 EXEC SQL SELECT list_id INTO :before[9] FROM list
239 else if (!strcmp(before[1], "MACHINE"))
241 id_to_name(id, MACHINE_TABLE, &name2);
242 EXEC SQL SELECT list_id INTO :before[9] FROM list
244 sprintf(before[10], "%d", id);
247 strcpy(before[2], name2);
253 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
261 void incremental_clear_before(void)
267 /* add an element to the incremental queue for the changed row */
269 void incremental_after(enum tables table, char *qual, char **argv)
272 EXEC SQL BEGIN DECLARE SECTION;
274 EXEC SQL END DECLARE SECTION;
280 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
281 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
282 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
287 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
293 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
294 "c.clu_id FROM clusters c WHERE %s", qual);
298 case CONTAINERS_TABLE:
299 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
300 "c.acl_type, c.acl_id, c.cnt_id, c.list_id FROM containers c "
306 if (!strncmp(after[4], "USER", 4))
308 id_to_name(id, USERS_TABLE, &name);
309 strcpy(after[5], name);
311 else if (!strncmp(after[4], "LIST", 4))
313 id_to_name(id, LIST_TABLE, &name);
314 strcpy(after[5], name);
316 else if (!strncmp(after[4], "KERBEROS", 8))
318 id_to_name(id, STRINGS_TABLE, &name);
319 strcpy(after[5], name);
322 id_to_name(id, LIST_TABLE, &name);
323 strcpy(after[7], name);
326 strcpy(after[0], argv[0]);
327 strcpy(after[1], argv[1]);
331 strcpy(after[0], argv[0]);
332 strcpy(after[1], argv[1]);
333 name_to_id(after[0], MACHINE_TABLE, &id);
334 sprintf(after[2], "%d", id);
335 name_to_id(after[1], CONTAINERS_TABLE, &id);
336 sprintf(after[3], "%d", id);
338 EXEC SQL SELECT list_id INTO :after[4] FROM containers
341 id_to_name(id, LIST_TABLE, &name);
342 strcpy(after[4], name);
346 strcpy(after[0], argv[0]);
347 strcpy(after[1], argv[1]);
348 strcpy(after[2], argv[2]);
352 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
353 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
354 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
359 id_to_name(id, MACHINE_TABLE, &name);
360 strcpy(after[2], name);
362 id_to_name(id, USERS_TABLE, &name);
363 strcpy(after[7], name);
365 id_to_name(id, LIST_TABLE, &name);
366 strcpy(after[8], name);
371 strcpy(after[0], "?");
372 strcpy(after[1], argv[1]);
373 strcpy(after[2], "?");
374 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
375 "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
381 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
382 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
383 "l.description, l.list_id FROM list l WHERE %s", qual);
389 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
390 "grouplist, gid FROM list WHERE list_id = %d", id);
393 id_to_name(id, LIST_TABLE, &name);
395 strcpy(after[0], name);
396 strcpy(after[1], argv[1]);
399 if (!strcmp(after[1], "USER"))
401 id_to_name(id, USERS_TABLE, &name2);
402 EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
403 FROM users WHERE users_id = :id;
404 EXEC SQL SELECT list_id INTO :after[10] FROM list
408 else if (!strcmp(after[1], "LIST"))
410 id_to_name(id, LIST_TABLE, &name2);
411 EXEC SQL SELECT list_id INTO :after[9] FROM list
413 sprintf(after[10], "%d", id);
416 else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
418 id_to_name(id, STRINGS_TABLE, &name2);
419 EXEC SQL SELECT list_id INTO :after[9] FROM list
422 else if (!strcmp(after[1], "MACHINE"))
424 id_to_name(id, MACHINE_TABLE, &name2);
425 EXEC SQL SELECT list_id INTO :after[9] FROM list
427 sprintf(after[10], "%d", id);
430 strcpy(after[2], name2);
440 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
446 iu = xmalloc(sizeof(struct iupdate));
447 iu->table = table_name[table];
448 iu->beforec = beforec;
449 iu->before = copy_argv(before, beforec);
451 iu->after = copy_argv(after, afterc);
452 sq_save_data(incremental_sq, iu);
455 void incremental_clear_after(void)
457 incremental_after(NO_TABLE, NULL, NULL);
461 /* Called when the current transaction is committed to start any queued
462 * incremental updates. This caches the update table the first time it
467 struct inc_cache *next;
468 char *table, *service;
472 void incremental_update(void)
474 static int inited = 0;
475 static struct inc_cache *cache;
477 EXEC SQL BEGIN DECLARE SECTION;
478 char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
479 EXEC SQL END DECLARE SECTION;
480 struct iupdate *iu, *iu_save;
486 EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
491 EXEC SQL FETCH inc INTO :tab, :serv;
494 c = xmalloc(sizeof(struct inc_cache));
496 c->table = xstrdup(strtrim(tab));
497 c->service = xstrdup(strtrim(serv));
501 EXEC SQL COMMIT WORK;
504 while (sq_remove_data(incremental_sq, &iu))
506 for (c = cache; c; c = c->next)
508 if (!strcmp(c->table, iu->table))
510 iu->service = c->service;
511 iu_save = xmalloc(sizeof(struct iupdate));
512 iu_save->service = iu->service;
513 iu_save->table = iu->table;
514 iu_save->beforec = iu->beforec;
515 iu_save->afterc = iu->afterc;
516 iu_save->before = copy_argv(iu->before, iu->beforec);
517 iu_save->after = copy_argv(iu->after, iu->afterc);
518 sq_save_data(incremental_exec, iu_save);
523 free_argv(iu->before, iu->beforec);
524 free_argv(iu->after, iu->afterc);
528 if (inc_running == 0)
532 /* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
533 We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
534 this function so proc will pass it through without reading it. */
537 void next_incremental(void)
540 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
544 if (!incremental_exec)
547 if (sq_empty(incremental_exec) ||
548 (inc_running && now - inc_started < INC_TIMEOUT))
552 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
554 sq_remove_data(incremental_exec, &iu);
556 sprintf(cbefore, "%d", iu->beforec);
558 sprintf(cafter, "%d", iu->afterc);
560 for (i = 0; i < iu->beforec; i++)
561 argv[4 + i] = iu->before[i];
562 for (i = 0; i < iu->afterc; i++)
563 argv[4 + iu->beforec + i] = iu->after[i];
565 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
567 argv[4 + iu->beforec + iu->afterc] = 0;
570 sigaddset(&sigs, SIGCHLD);
571 sigprocmask(SIG_BLOCK, &sigs, NULL);
579 com_err(whoami, 0, "Failed to start incremental update");
585 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
587 free_argv(iu->before, iu->beforec);
588 free_argv(iu->after, iu->afterc);
593 /* Called when the current transaction is aborted to throw away any queued
594 * incremental updates
597 void incremental_flush(void)
601 while (sq_get_data(incremental_sq, &iu))
603 free_argv(iu->before, iu->beforec);
604 free_argv(iu->after, iu->afterc);
607 sq_destroy(incremental_sq);
608 incremental_sq = sq_create();
612 char **copy_argv(char **argv, int argc)
614 char **ret = xmalloc(sizeof(char *) * argc);
616 ret[argc] = xstrdup(strtrim(argv[argc]));
620 void free_argv(char **argv, int argc)
627 int table_num(char *name)
631 for (i = num_tables - 1; i; i--)
633 if (!strcmp(table_name[i], name))
637 return i; /* 0 = "none" if no match */