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 name_to_id(before[0], MACHINE_TABLE, &id);
146 sprintf(before[2], "%d", id);
147 name_to_id(before[1], CONTAINERS_TABLE, &id);
148 sprintf(before[3], "%d", id);
152 strcpy(before[0], argv[0]);
153 strcpy(before[1], argv[1]);
154 strcpy(before[2], argv[2]);
158 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
159 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
160 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
164 id = atoi(before[2]);
165 id_to_name(id, MACHINE_TABLE, &name);
166 strcpy(before[2], name);
167 id = atoi(before[7]);
168 id_to_name(id, USERS_TABLE, &name);
169 strcpy(before[7], name);
170 id = atoi(before[8]);
171 id_to_name(id, LIST_TABLE, &name);
172 strcpy(before[8], name);
177 strcpy(before[0], "?");
178 strcpy(before[1], argv[1]);
179 strcpy(before[2], "?");
180 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
181 "WHERE %s AND fs.filsys_id = q.filsys_id", qual);
183 strcpy(before[2], argv[1]);
187 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
188 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
189 "l.description, l.list_id FROM list l WHERE %s", qual);
195 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
196 "grouplist, gid FROM list WHERE list_id = %d", id);
199 id_to_name(id, LIST_TABLE, &name);
201 strcpy(before[0], name);
202 strcpy(before[1], argv[1]);
205 if (!strcmp(before[1], "USER"))
207 id_to_name(id, USERS_TABLE, &name2);
208 EXEC SQL SELECT status, users_id INTO :before[9], :before[11]
209 FROM users WHERE users_id = :id;
210 EXEC SQL SELECT list_id INTO :before[10] FROM list
214 else if (!strcmp(before[1], "LIST"))
216 id_to_name(id, LIST_TABLE, &name2);
217 EXEC SQL SELECT list_id INTO :before[9] FROM list
219 sprintf(before[10], "%d", id);
222 else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
224 id_to_name(id, STRINGS_TABLE, &name2);
225 EXEC SQL SELECT list_id INTO :before[9] FROM list
228 else if (!strcmp(before[1], "MACHINE"))
230 id_to_name(id, MACHINE_TABLE, &name2);
231 EXEC SQL SELECT list_id INTO :before[9] FROM list
233 sprintf(before[10], "%d", id);
236 strcpy(before[2], name2);
242 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
250 void incremental_clear_before(void)
256 /* add an element to the incremental queue for the changed row */
258 void incremental_after(enum tables table, char *qual, char **argv)
261 EXEC SQL BEGIN DECLARE SECTION;
263 EXEC SQL END DECLARE SECTION;
269 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
270 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
271 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
276 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
282 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
283 "c.clu_id FROM clusters c WHERE %s", qual);
287 case CONTAINERS_TABLE:
288 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
289 "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
295 if (!strncmp(after[4], "USER", 4))
297 id_to_name(id, USERS_TABLE, &name);
298 strcpy(after[5], name);
300 else if (!strncmp(after[4], "LIST", 4))
302 id_to_name(id, LIST_TABLE, &name);
303 strcpy(after[5], name);
305 else if (!strncmp(after[4], "KERBEROS", 8))
307 id_to_name(id, STRINGS_TABLE, &name);
308 strcpy(after[5], name);
312 strcpy(after[0], argv[0]);
313 strcpy(after[1], argv[1]);
317 strcpy(after[0], argv[0]);
318 strcpy(after[1], argv[1]);
319 name_to_id(after[0], MACHINE_TABLE, &id);
320 sprintf(after[2], "%d", id);
321 name_to_id(after[1], CONTAINERS_TABLE, &id);
322 sprintf(after[3], "%d", id);
326 strcpy(after[0], argv[0]);
327 strcpy(after[1], argv[1]);
328 strcpy(after[2], argv[2]);
332 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
333 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
334 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
339 id_to_name(id, MACHINE_TABLE, &name);
340 strcpy(after[2], name);
342 id_to_name(id, USERS_TABLE, &name);
343 strcpy(after[7], name);
345 id_to_name(id, LIST_TABLE, &name);
346 strcpy(after[8], name);
351 strcpy(after[0], "?");
352 strcpy(after[1], argv[1]);
353 strcpy(after[2], "?");
354 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
355 "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
361 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
362 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
363 "l.description, l.list_id FROM list l WHERE %s", qual);
369 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
370 "grouplist, gid FROM list WHERE list_id = %d", id);
373 id_to_name(id, LIST_TABLE, &name);
375 strcpy(after[0], name);
376 strcpy(after[1], argv[1]);
379 if (!strcmp(after[1], "USER"))
381 id_to_name(id, USERS_TABLE, &name2);
382 EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
383 FROM users WHERE users_id = :id;
384 EXEC SQL SELECT list_id INTO :after[10] FROM list
388 else if (!strcmp(after[1], "LIST"))
390 id_to_name(id, LIST_TABLE, &name2);
391 EXEC SQL SELECT list_id INTO :after[9] FROM list
393 sprintf(after[10], "%d", id);
396 else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
398 id_to_name(id, STRINGS_TABLE, &name2);
399 EXEC SQL SELECT list_id INTO :after[9] FROM list
402 else if (!strcmp(after[1], "MACHINE"))
404 id_to_name(id, MACHINE_TABLE, &name2);
405 EXEC SQL SELECT list_id INTO :after[9] FROM list
407 sprintf(after[10], "%d", id);
410 strcpy(after[2], name2);
420 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
426 iu = xmalloc(sizeof(struct iupdate));
427 iu->table = table_name[table];
428 iu->beforec = beforec;
429 iu->before = copy_argv(before, beforec);
431 iu->after = copy_argv(after, afterc);
432 sq_save_data(incremental_sq, iu);
435 void incremental_clear_after(void)
437 incremental_after(NO_TABLE, NULL, NULL);
441 /* Called when the current transaction is committed to start any queued
442 * incremental updates. This caches the update table the first time it
447 struct inc_cache *next;
448 char *table, *service;
452 void incremental_update(void)
454 static int inited = 0;
455 static struct inc_cache *cache;
457 EXEC SQL BEGIN DECLARE SECTION;
458 char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
459 EXEC SQL END DECLARE SECTION;
460 struct iupdate *iu, *iu_save;
466 EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
471 EXEC SQL FETCH inc INTO :tab, :serv;
474 c = xmalloc(sizeof(struct inc_cache));
476 c->table = xstrdup(strtrim(tab));
477 c->service = xstrdup(strtrim(serv));
481 EXEC SQL COMMIT WORK;
484 while (sq_remove_data(incremental_sq, &iu))
486 for (c = cache; c; c = c->next)
488 if (!strcmp(c->table, iu->table))
490 iu->service = c->service;
491 iu_save = xmalloc(sizeof(struct iupdate));
492 iu_save->service = iu->service;
493 iu_save->table = iu->table;
494 iu_save->beforec = iu->beforec;
495 iu_save->afterc = iu->afterc;
496 iu_save->before = copy_argv(iu->before, iu->beforec);
497 iu_save->after = copy_argv(iu->after, iu->afterc);
498 sq_save_data(incremental_exec, iu_save);
503 free_argv(iu->before, iu->beforec);
504 free_argv(iu->after, iu->afterc);
508 if (inc_running == 0)
512 /* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
513 We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
514 this function so proc will pass it through without reading it. */
517 void next_incremental(void)
520 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
524 if (!incremental_exec)
527 if (sq_empty(incremental_exec) ||
528 (inc_running && now - inc_started < INC_TIMEOUT))
532 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
534 sq_remove_data(incremental_exec, &iu);
536 sprintf(cbefore, "%d", iu->beforec);
538 sprintf(cafter, "%d", iu->afterc);
540 for (i = 0; i < iu->beforec; i++)
541 argv[4 + i] = iu->before[i];
542 for (i = 0; i < iu->afterc; i++)
543 argv[4 + iu->beforec + i] = iu->after[i];
545 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
547 argv[4 + iu->beforec + iu->afterc] = 0;
550 sigaddset(&sigs, SIGCHLD);
551 sigprocmask(SIG_BLOCK, &sigs, NULL);
559 com_err(whoami, 0, "Failed to start incremental update");
565 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
567 free_argv(iu->before, iu->beforec);
568 free_argv(iu->after, iu->afterc);
573 /* Called when the current transaction is aborted to throw away any queued
574 * incremental updates
577 void incremental_flush(void)
581 while (sq_get_data(incremental_sq, &iu))
583 free_argv(iu->before, iu->beforec);
584 free_argv(iu->after, iu->afterc);
587 sq_destroy(incremental_sq);
588 incremental_sq = sq_create();
592 char **copy_argv(char **argv, int argc)
594 char **ret = xmalloc(sizeof(char *) * argc);
596 ret[argc] = xstrdup(strtrim(argv[argc]));
600 void free_argv(char **argv, int argc)
607 int table_num(char *name)
611 for (i = num_tables - 1; i; i--)
613 if (!strcmp(table_name[i], name))
617 return i; /* 0 = "none" if no match */