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);
114 strcpy(before[0], argv[0]);
115 strcpy(before[1], argv[1]);
119 strcpy(before[0], argv[0]);
120 strcpy(before[1], argv[1]);
121 strcpy(before[2], argv[2]);
125 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
126 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
127 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
131 id = atoi(before[2]);
132 id_to_name(id, MACHINE_TABLE, &name);
133 strcpy(before[2], name);
134 id = atoi(before[7]);
135 id_to_name(id, USERS_TABLE, &name);
136 strcpy(before[7], name);
137 id = atoi(before[8]);
138 id_to_name(id, LIST_TABLE, &name);
139 strcpy(before[8], name);
144 strcpy(before[0], "?");
145 strcpy(before[1], argv[1]);
146 strcpy(before[2], "?");
147 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
148 "WHERE %s AND fs.filsys_id = q.filsys_id", qual);
150 strcpy(before[2], argv[1]);
154 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
155 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
156 "l.description, l.list_id FROM list l WHERE %s", qual);
162 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
163 "grouplist, gid FROM list WHERE list_id = %d", id);
166 id_to_name(id, LIST_TABLE, &name);
168 strcpy(before[0], name);
169 strcpy(before[1], argv[1]);
172 if (!strcmp(before[1], "USER"))
174 id_to_name(id, USERS_TABLE, &name2);
175 EXEC SQL SELECT status, users_id INTO :before[9], :before[11]
176 FROM users WHERE users_id = :id;
177 EXEC SQL SELECT list_id INTO :before[10] FROM list
181 else if (!strcmp(before[1], "LIST"))
183 id_to_name(id, LIST_TABLE, &name2);
184 EXEC SQL SELECT list_id INTO :before[9] FROM list
186 sprintf(before[10], "%d", id);
189 else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
191 id_to_name(id, STRINGS_TABLE, &name2);
192 EXEC SQL SELECT list_id INTO :before[9] FROM list
195 strcpy(before[2], name2);
201 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
209 void incremental_clear_before(void)
215 /* add an element to the incremental queue for the changed row */
217 void incremental_after(enum tables table, char *qual, char **argv)
220 EXEC SQL BEGIN DECLARE SECTION;
222 EXEC SQL END DECLARE SECTION;
228 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
229 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
230 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
235 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
241 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
242 "c.clu_id FROM clusters c WHERE %s", qual);
247 strcpy(after[0], argv[0]);
248 strcpy(after[1], argv[1]);
252 strcpy(after[0], argv[0]);
253 strcpy(after[1], argv[1]);
254 strcpy(after[2], argv[2]);
258 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
259 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
260 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
265 id_to_name(id, MACHINE_TABLE, &name);
266 strcpy(after[2], name);
268 id_to_name(id, USERS_TABLE, &name);
269 strcpy(after[7], name);
271 id_to_name(id, LIST_TABLE, &name);
272 strcpy(after[8], name);
277 strcpy(after[0], "?");
278 strcpy(after[1], argv[1]);
279 strcpy(after[2], "?");
280 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
281 "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
287 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
288 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
289 "l.description, l.list_id FROM list l WHERE %s", qual);
295 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
296 "grouplist, gid FROM list WHERE list_id = %d", id);
299 id_to_name(id, LIST_TABLE, &name);
301 strcpy(after[0], name);
302 strcpy(after[1], argv[1]);
305 if (!strcmp(after[1], "USER"))
307 id_to_name(id, USERS_TABLE, &name2);
308 EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
309 FROM users WHERE users_id = :id;
310 EXEC SQL SELECT list_id INTO :after[10] FROM list
314 else if (!strcmp(after[1], "LIST"))
316 id_to_name(id, LIST_TABLE, &name2);
317 EXEC SQL SELECT list_id INTO :after[9] FROM list
319 sprintf(after[10], "%d", id);
322 else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
324 id_to_name(id, STRINGS_TABLE, &name2);
325 EXEC SQL SELECT list_id INTO :after[9] FROM list
328 strcpy(after[2], name2);
338 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
344 iu = xmalloc(sizeof(struct iupdate));
345 iu->table = table_name[table];
346 iu->beforec = beforec;
347 iu->before = copy_argv(before, beforec);
349 iu->after = copy_argv(after, afterc);
350 sq_save_data(incremental_sq, iu);
353 void incremental_clear_after(void)
355 incremental_after(NO_TABLE, NULL, NULL);
359 /* Called when the current transaction is committed to start any queued
360 * incremental updates. This caches the update table the first time it
365 struct inc_cache *next;
366 char *table, *service;
370 void incremental_update(void)
372 static int inited = 0;
373 static struct inc_cache *cache;
375 EXEC SQL BEGIN DECLARE SECTION;
376 char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
377 EXEC SQL END DECLARE SECTION;
378 struct iupdate *iu, *iu_save;
384 EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
389 EXEC SQL FETCH inc INTO :tab, :serv;
392 c = xmalloc(sizeof(struct inc_cache));
394 c->table = xstrdup(strtrim(tab));
395 c->service = xstrdup(strtrim(serv));
399 EXEC SQL COMMIT WORK;
402 while (sq_remove_data(incremental_sq, &iu))
404 for (c = cache; c; c = c->next)
406 if (!strcmp(c->table, iu->table))
408 iu->service = c->service;
409 iu_save = xmalloc(sizeof(struct iupdate));
410 iu_save->service = iu->service;
411 iu_save->table = iu->table;
412 iu_save->beforec = iu->beforec;
413 iu_save->afterc = iu->afterc;
414 iu_save->before = copy_argv(iu->before, iu->beforec);
415 iu_save->after = copy_argv(iu->after, iu->afterc);
416 sq_save_data(incremental_exec, iu_save);
421 free_argv(iu->before, iu->beforec);
422 free_argv(iu->after, iu->afterc);
426 if (inc_running == 0)
430 /* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
431 We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
432 this function so proc will pass it through without reading it. */
435 void next_incremental(void)
438 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
442 if (!incremental_exec)
445 if (sq_empty(incremental_exec) ||
446 (inc_running && now - inc_started < INC_TIMEOUT))
450 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
452 sq_remove_data(incremental_exec, &iu);
454 sprintf(cbefore, "%d", iu->beforec);
456 sprintf(cafter, "%d", iu->afterc);
458 for (i = 0; i < iu->beforec; i++)
459 argv[4 + i] = iu->before[i];
460 for (i = 0; i < iu->afterc; i++)
461 argv[4 + iu->beforec + i] = iu->after[i];
463 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
465 argv[4 + iu->beforec + iu->afterc] = 0;
468 sigaddset(&sigs, SIGCHLD);
469 sigprocmask(SIG_BLOCK, &sigs, NULL);
477 com_err(whoami, 0, "Failed to start incremental update");
483 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
485 free_argv(iu->before, iu->beforec);
486 free_argv(iu->after, iu->afterc);
491 /* Called when the current transaction is aborted to throw away any queued
492 * incremental updates
495 void incremental_flush(void)
499 while (sq_get_data(incremental_sq, &iu))
501 free_argv(iu->before, iu->beforec);
502 free_argv(iu->after, iu->afterc);
505 sq_destroy(incremental_sq);
506 incremental_sq = sq_create();
510 char **copy_argv(char **argv, int argc)
512 char **ret = xmalloc(sizeof(char *) * argc);
514 ret[argc] = xstrdup(strtrim(argv[argc]));
518 void free_argv(char **argv, int argc)
525 int table_num(char *name)
529 for (i = num_tables - 1; i; i--)
531 if (!strcmp(table_name[i], name))
535 return i; /* 0 = "none" if no match */