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 /* structures to save before args */
38 static char *before[MAXARGC];
40 static enum tables beforetable;
42 /* structures to save after args */
43 static char *after[MAXARGC];
46 /* structures to save entire sets of incremental changes */
47 struct save_queue *incremental_sq = NULL;
48 struct save_queue *incremental_exec = NULL;
58 void next_incremental(void);
59 char **copy_argv(char **argv, int argc);
60 void free_argv(char **argv, int argc);
61 int table_num(char *table);
63 void incremental_init(void)
68 incremental_sq = sq_create();
69 if (!incremental_exec)
70 incremental_exec = sq_create();
72 for (i = 0; i < MAXARGC; i++)
74 before[i] = xmalloc(MAX_FIELD_WIDTH);
75 after[i] = xmalloc(MAX_FIELD_WIDTH);
80 /* record the state of a table row before it is changed */
82 void incremental_before(enum tables table, char *qual, char **argv)
84 EXEC SQL BEGIN DECLARE SECTION;
86 EXEC SQL END DECLARE SECTION;
95 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
96 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
97 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
102 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
108 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
109 "c.clu_id FROM clusters c WHERE %s", qual);
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",
120 id = atoi(before[5]);
121 if (!strncmp(before[4], "USER", 4))
123 id_to_name(id, USERS_TABLE, &name);
124 strcpy(before[5], name);
126 else if (!strncmp(before[4], "LIST", 4))
128 id_to_name(id, LIST_TABLE, &name);
129 strcpy(before[5], name);
131 else if (!strncmp(before[4], "KERBEROS", 8))
133 id_to_name(id, STRINGS_TABLE, &name);
134 strcpy(before[5], name);
138 strcpy(before[0], argv[0]);
139 strcpy(before[1], argv[1]);
143 strcpy(before[0], argv[0]);
144 strcpy(before[1], argv[1]);
145 strcpy(before[2], argv[2]);
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, "
151 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
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);
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);
174 strcpy(before[2], argv[1]);
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, "
180 "l.description, l.list_id FROM list l WHERE %s", qual);
186 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
187 "grouplist, gid FROM list WHERE list_id = %d", id);
190 id_to_name(id, LIST_TABLE, &name);
192 strcpy(before[0], name);
193 strcpy(before[1], argv[1]);
196 if (!strcmp(before[1], "USER"))
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
205 else if (!strcmp(before[1], "LIST"))
207 id_to_name(id, LIST_TABLE, &name2);
208 EXEC SQL SELECT list_id INTO :before[9] FROM list
210 sprintf(before[10], "%d", id);
213 else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
215 id_to_name(id, STRINGS_TABLE, &name2);
216 EXEC SQL SELECT list_id INTO :before[9] FROM list
219 else if (!strcmp(before[1], "MACHINE"))
221 id_to_name(id, MACHINE_TABLE, &name2);
222 EXEC SQL SELECT list_id INTO :before[9] FROM list
224 sprintf(before[10], "%d", id);
227 strcpy(before[2], name2);
233 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
241 void incremental_clear_before(void)
247 /* add an element to the incremental queue for the changed row */
249 void incremental_after(enum tables table, char *qual, char **argv)
252 EXEC SQL BEGIN DECLARE SECTION;
254 EXEC SQL END DECLARE SECTION;
260 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
261 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
262 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
267 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
273 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
274 "c.clu_id FROM clusters c WHERE %s", qual);
278 case CONTAINERS_TABLE:
279 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
280 "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
286 if (!strncmp(after[4], "USER", 4))
288 id_to_name(id, USERS_TABLE, &name);
289 strcpy(after[5], name);
291 else if (!strncmp(after[4], "LIST", 4))
293 id_to_name(id, LIST_TABLE, &name);
294 strcpy(after[5], name);
296 else if (!strncmp(after[4], "KERBEROS", 8))
298 id_to_name(id, STRINGS_TABLE, &name);
299 strcpy(after[5], name);
303 strcpy(after[0], argv[0]);
304 strcpy(after[1], argv[1]);
308 strcpy(after[0], argv[0]);
309 strcpy(after[1], argv[1]);
310 strcpy(after[2], argv[2]);
314 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
315 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
316 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
321 id_to_name(id, MACHINE_TABLE, &name);
322 strcpy(after[2], name);
324 id_to_name(id, USERS_TABLE, &name);
325 strcpy(after[7], name);
327 id_to_name(id, LIST_TABLE, &name);
328 strcpy(after[8], name);
333 strcpy(after[0], "?");
334 strcpy(after[1], argv[1]);
335 strcpy(after[2], "?");
336 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
337 "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
343 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
344 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
345 "l.description, l.list_id FROM list l WHERE %s", qual);
351 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
352 "grouplist, gid FROM list WHERE list_id = %d", id);
355 id_to_name(id, LIST_TABLE, &name);
357 strcpy(after[0], name);
358 strcpy(after[1], argv[1]);
361 if (!strcmp(after[1], "USER"))
363 id_to_name(id, USERS_TABLE, &name2);
364 EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
365 FROM users WHERE users_id = :id;
366 EXEC SQL SELECT list_id INTO :after[10] FROM list
370 else if (!strcmp(after[1], "LIST"))
372 id_to_name(id, LIST_TABLE, &name2);
373 EXEC SQL SELECT list_id INTO :after[9] FROM list
375 sprintf(after[10], "%d", id);
378 else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
380 id_to_name(id, STRINGS_TABLE, &name2);
381 EXEC SQL SELECT list_id INTO :after[9] FROM list
384 else if (!strcmp(after[1], "MACHINE"))
386 id_to_name(id, MACHINE_TABLE, &name2);
387 EXEC SQL SELECT list_id INTO :after[9] FROM list
389 sprintf(after[10], "%d", id);
392 strcpy(after[2], name2);
402 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
408 iu = xmalloc(sizeof(struct iupdate));
409 iu->table = table_name[table];
410 iu->beforec = beforec;
411 iu->before = copy_argv(before, beforec);
413 iu->after = copy_argv(after, afterc);
414 sq_save_data(incremental_sq, iu);
417 void incremental_clear_after(void)
419 incremental_after(NO_TABLE, NULL, NULL);
423 /* Called when the current transaction is committed to start any queued
424 * incremental updates. This caches the update table the first time it
429 struct inc_cache *next;
430 char *table, *service;
434 void incremental_update(void)
436 static int inited = 0;
437 static struct inc_cache *cache;
439 EXEC SQL BEGIN DECLARE SECTION;
440 char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
441 EXEC SQL END DECLARE SECTION;
442 struct iupdate *iu, *iu_save;
448 EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
453 EXEC SQL FETCH inc INTO :tab, :serv;
456 c = xmalloc(sizeof(struct inc_cache));
458 c->table = xstrdup(strtrim(tab));
459 c->service = xstrdup(strtrim(serv));
463 EXEC SQL COMMIT WORK;
466 while (sq_remove_data(incremental_sq, &iu))
468 for (c = cache; c; c = c->next)
470 if (!strcmp(c->table, iu->table))
472 iu->service = c->service;
473 iu_save = xmalloc(sizeof(struct iupdate));
474 iu_save->service = iu->service;
475 iu_save->table = iu->table;
476 iu_save->beforec = iu->beforec;
477 iu_save->afterc = iu->afterc;
478 iu_save->before = copy_argv(iu->before, iu->beforec);
479 iu_save->after = copy_argv(iu->after, iu->afterc);
480 sq_save_data(incremental_exec, iu_save);
485 free_argv(iu->before, iu->beforec);
486 free_argv(iu->after, iu->afterc);
490 if (inc_running == 0)
494 /* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
495 We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
496 this function so proc will pass it through without reading it. */
499 void next_incremental(void)
502 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
506 if (!incremental_exec)
509 if (sq_empty(incremental_exec) ||
510 (inc_running && now - inc_started < INC_TIMEOUT))
514 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
516 sq_remove_data(incremental_exec, &iu);
518 sprintf(cbefore, "%d", iu->beforec);
520 sprintf(cafter, "%d", iu->afterc);
522 for (i = 0; i < iu->beforec; i++)
523 argv[4 + i] = iu->before[i];
524 for (i = 0; i < iu->afterc; i++)
525 argv[4 + iu->beforec + i] = iu->after[i];
527 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
529 argv[4 + iu->beforec + iu->afterc] = 0;
532 sigaddset(&sigs, SIGCHLD);
533 sigprocmask(SIG_BLOCK, &sigs, NULL);
541 com_err(whoami, 0, "Failed to start incremental update");
547 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
549 free_argv(iu->before, iu->beforec);
550 free_argv(iu->after, iu->afterc);
555 /* Called when the current transaction is aborted to throw away any queued
556 * incremental updates
559 void incremental_flush(void)
563 while (sq_get_data(incremental_sq, &iu))
565 free_argv(iu->before, iu->beforec);
566 free_argv(iu->after, iu->afterc);
569 sq_destroy(incremental_sq);
570 incremental_sq = sq_create();
574 char **copy_argv(char **argv, int argc)
576 char **ret = xmalloc(sizeof(char *) * argc);
578 ret[argc] = xstrdup(strtrim(argv[argc]));
582 void free_argv(char **argv, int argc)
589 int table_num(char *name)
593 for (i = num_tables - 1; i; i--)
595 if (!strcmp(table_name[i], name))
599 return i; /* 0 = "none" if no match */