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",
121 strcpy(before[0], argv[0]);
122 strcpy(before[1], argv[1]);
126 strcpy(before[0], argv[0]);
127 strcpy(before[1], argv[1]);
128 strcpy(before[2], argv[2]);
132 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
133 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
134 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
138 id = atoi(before[2]);
139 id_to_name(id, MACHINE_TABLE, &name);
140 strcpy(before[2], name);
141 id = atoi(before[7]);
142 id_to_name(id, USERS_TABLE, &name);
143 strcpy(before[7], name);
144 id = atoi(before[8]);
145 id_to_name(id, LIST_TABLE, &name);
146 strcpy(before[8], name);
151 strcpy(before[0], "?");
152 strcpy(before[1], argv[1]);
153 strcpy(before[2], "?");
154 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
155 "WHERE %s AND fs.filsys_id = q.filsys_id", qual);
157 strcpy(before[2], argv[1]);
161 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
162 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
163 "l.description, l.list_id FROM list l WHERE %s", qual);
169 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
170 "grouplist, gid FROM list WHERE list_id = %d", id);
173 id_to_name(id, LIST_TABLE, &name);
175 strcpy(before[0], name);
176 strcpy(before[1], argv[1]);
179 if (!strcmp(before[1], "USER"))
181 id_to_name(id, USERS_TABLE, &name2);
182 EXEC SQL SELECT status, users_id INTO :before[9], :before[11]
183 FROM users WHERE users_id = :id;
184 EXEC SQL SELECT list_id INTO :before[10] FROM list
188 else if (!strcmp(before[1], "LIST"))
190 id_to_name(id, LIST_TABLE, &name2);
191 EXEC SQL SELECT list_id INTO :before[9] FROM list
193 sprintf(before[10], "%d", id);
196 else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
198 id_to_name(id, STRINGS_TABLE, &name2);
199 EXEC SQL SELECT list_id INTO :before[9] FROM list
202 strcpy(before[2], name2);
208 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
216 void incremental_clear_before(void)
222 /* add an element to the incremental queue for the changed row */
224 void incremental_after(enum tables table, char *qual, char **argv)
227 EXEC SQL BEGIN DECLARE SECTION;
229 EXEC SQL END DECLARE SECTION;
235 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
236 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
237 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
242 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
248 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
249 "c.clu_id FROM clusters c WHERE %s", qual);
253 case CONTAINERS_TABLE:
254 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
255 "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
261 strcpy(after[0], argv[0]);
262 strcpy(after[1], argv[1]);
266 strcpy(after[0], argv[0]);
267 strcpy(after[1], argv[1]);
268 strcpy(after[2], argv[2]);
272 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
273 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
274 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
279 id_to_name(id, MACHINE_TABLE, &name);
280 strcpy(after[2], name);
282 id_to_name(id, USERS_TABLE, &name);
283 strcpy(after[7], name);
285 id_to_name(id, LIST_TABLE, &name);
286 strcpy(after[8], name);
291 strcpy(after[0], "?");
292 strcpy(after[1], argv[1]);
293 strcpy(after[2], "?");
294 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
295 "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
301 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
302 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
303 "l.description, l.list_id FROM list l WHERE %s", qual);
309 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
310 "grouplist, gid FROM list WHERE list_id = %d", id);
313 id_to_name(id, LIST_TABLE, &name);
315 strcpy(after[0], name);
316 strcpy(after[1], argv[1]);
319 if (!strcmp(after[1], "USER"))
321 id_to_name(id, USERS_TABLE, &name2);
322 EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
323 FROM users WHERE users_id = :id;
324 EXEC SQL SELECT list_id INTO :after[10] FROM list
328 else if (!strcmp(after[1], "LIST"))
330 id_to_name(id, LIST_TABLE, &name2);
331 EXEC SQL SELECT list_id INTO :after[9] FROM list
333 sprintf(after[10], "%d", id);
336 else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
338 id_to_name(id, STRINGS_TABLE, &name2);
339 EXEC SQL SELECT list_id INTO :after[9] FROM list
342 strcpy(after[2], name2);
352 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
358 iu = xmalloc(sizeof(struct iupdate));
359 iu->table = table_name[table];
360 iu->beforec = beforec;
361 iu->before = copy_argv(before, beforec);
363 iu->after = copy_argv(after, afterc);
364 sq_save_data(incremental_sq, iu);
367 void incremental_clear_after(void)
369 incremental_after(NO_TABLE, NULL, NULL);
373 /* Called when the current transaction is committed to start any queued
374 * incremental updates. This caches the update table the first time it
379 struct inc_cache *next;
380 char *table, *service;
384 void incremental_update(void)
386 static int inited = 0;
387 static struct inc_cache *cache;
389 EXEC SQL BEGIN DECLARE SECTION;
390 char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
391 EXEC SQL END DECLARE SECTION;
392 struct iupdate *iu, *iu_save;
398 EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
403 EXEC SQL FETCH inc INTO :tab, :serv;
406 c = xmalloc(sizeof(struct inc_cache));
408 c->table = xstrdup(strtrim(tab));
409 c->service = xstrdup(strtrim(serv));
413 EXEC SQL COMMIT WORK;
416 while (sq_remove_data(incremental_sq, &iu))
418 for (c = cache; c; c = c->next)
420 if (!strcmp(c->table, iu->table))
422 iu->service = c->service;
423 iu_save = xmalloc(sizeof(struct iupdate));
424 iu_save->service = iu->service;
425 iu_save->table = iu->table;
426 iu_save->beforec = iu->beforec;
427 iu_save->afterc = iu->afterc;
428 iu_save->before = copy_argv(iu->before, iu->beforec);
429 iu_save->after = copy_argv(iu->after, iu->afterc);
430 sq_save_data(incremental_exec, iu_save);
435 free_argv(iu->before, iu->beforec);
436 free_argv(iu->after, iu->afterc);
440 if (inc_running == 0)
444 /* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
445 We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
446 this function so proc will pass it through without reading it. */
449 void next_incremental(void)
452 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
456 if (!incremental_exec)
459 if (sq_empty(incremental_exec) ||
460 (inc_running && now - inc_started < INC_TIMEOUT))
464 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
466 sq_remove_data(incremental_exec, &iu);
468 sprintf(cbefore, "%d", iu->beforec);
470 sprintf(cafter, "%d", iu->afterc);
472 for (i = 0; i < iu->beforec; i++)
473 argv[4 + i] = iu->before[i];
474 for (i = 0; i < iu->afterc; i++)
475 argv[4 + iu->beforec + i] = iu->after[i];
477 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
479 argv[4 + iu->beforec + iu->afterc] = 0;
482 sigaddset(&sigs, SIGCHLD);
483 sigprocmask(SIG_BLOCK, &sigs, NULL);
491 com_err(whoami, 0, "Failed to start incremental update");
497 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
499 free_argv(iu->before, iu->beforec);
500 free_argv(iu->after, iu->afterc);
505 /* Called when the current transaction is aborted to throw away any queued
506 * incremental updates
509 void incremental_flush(void)
513 while (sq_get_data(incremental_sq, &iu))
515 free_argv(iu->before, iu->beforec);
516 free_argv(iu->after, iu->afterc);
519 sq_destroy(incremental_sq);
520 incremental_sq = sq_create();
524 char **copy_argv(char **argv, int argc)
526 char **ret = xmalloc(sizeof(char *) * argc);
528 ret[argc] = xstrdup(strtrim(argv[argc]));
532 void free_argv(char **argv, int argc)
539 int table_num(char *name)
543 for (i = num_tables - 1; i; i--)
545 if (!strcmp(table_name[i], name))
549 return i; /* 0 = "none" if no match */