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, u.winhomedir, u.winprofiledir, "
100 "u.potype FROM users u WHERE %s", qual);
105 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
111 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
112 "c.clu_id FROM clusters c WHERE %s", qual);
116 case CONTAINERS_TABLE:
117 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
118 "c.acl_type, c.acl_id, c.cnt_id, c.list_id FROM containers c "
123 id = atoi(before[5]);
124 if (!strncmp(before[4], "USER", 4))
126 id_to_name(id, USERS_TABLE, &name);
127 strcpy(before[5], name);
129 else if (!strncmp(before[4], "LIST", 4))
131 id_to_name(id, LIST_TABLE, &name);
132 strcpy(before[5], name);
134 else if (!strncmp(before[4], "KERBEROS", 8))
136 id_to_name(id, STRINGS_TABLE, &name);
137 strcpy(before[5], name);
139 id = atoi(before[7]);
140 id_to_name(id, LIST_TABLE, &name);
141 strcpy(before[7], name);
144 strcpy(before[0], argv[0]);
145 strcpy(before[1], argv[1]);
149 strcpy(before[0], argv[0]);
150 strcpy(before[1], argv[1]);
151 name_to_id(before[0], MACHINE_TABLE, &id);
152 sprintf(before[2], "%d", id);
153 name_to_id(before[1], CONTAINERS_TABLE, &id);
154 sprintf(before[3], "%d", id);
156 EXEC SQL SELECT list_id INTO :before[4] FROM containers
158 id = atoi(before[4]);
159 id_to_name(id, LIST_TABLE, &name);
160 strcpy(before[4], name);
164 strcpy(before[0], argv[0]);
165 strcpy(before[1], argv[1]);
166 strcpy(before[2], argv[2]);
170 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
171 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
172 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
176 id = atoi(before[2]);
177 id_to_name(id, MACHINE_TABLE, &name);
178 strcpy(before[2], name);
179 id = atoi(before[7]);
180 id_to_name(id, USERS_TABLE, &name);
181 strcpy(before[7], name);
182 id = atoi(before[8]);
183 id_to_name(id, LIST_TABLE, &name);
184 strcpy(before[8], name);
189 strcpy(before[0], "?");
190 strcpy(before[1], argv[1]);
191 strcpy(before[2], "?");
192 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
193 "WHERE %s AND fs.filsys_id = q.filsys_id", qual);
195 strcpy(before[2], argv[1]);
199 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
200 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
201 "l.description, l.list_id FROM list l WHERE %s", qual);
207 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
208 "grouplist, gid FROM list WHERE list_id = %d", id);
211 id_to_name(id, LIST_TABLE, &name);
213 strcpy(before[0], name);
214 strcpy(before[1], argv[1]);
217 if (!strcmp(before[1], "USER"))
219 id_to_name(id, USERS_TABLE, &name2);
220 EXEC SQL SELECT status, users_id INTO :before[9], :before[11]
221 FROM users WHERE users_id = :id;
222 EXEC SQL SELECT list_id INTO :before[10] FROM list
226 else if (!strcmp(before[1], "LIST"))
228 id_to_name(id, LIST_TABLE, &name2);
229 EXEC SQL SELECT list_id INTO :before[9] FROM list
231 sprintf(before[10], "%d", id);
234 else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
236 id_to_name(id, STRINGS_TABLE, &name2);
237 EXEC SQL SELECT list_id INTO :before[9] FROM list
240 else if (!strcmp(before[1], "MACHINE"))
242 id_to_name(id, MACHINE_TABLE, &name2);
243 EXEC SQL SELECT list_id INTO :before[9] FROM list
245 sprintf(before[10], "%d", id);
248 strcpy(before[2], name2);
254 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
262 void incremental_clear_before(void)
268 /* add an element to the incremental queue for the changed row */
270 void incremental_after(enum tables table, char *qual, char **argv)
273 EXEC SQL BEGIN DECLARE SECTION;
275 EXEC SQL END DECLARE SECTION;
281 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
282 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
283 "u.clearid, u.type, u.users_id, u.winhomedir, u.winprofiledir, "
284 "u.potype FROM users u WHERE %s", qual);
289 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
295 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
296 "c.clu_id FROM clusters c WHERE %s", qual);
300 case CONTAINERS_TABLE:
301 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
302 "c.acl_type, c.acl_id, c.cnt_id, c.list_id FROM containers c "
308 if (!strncmp(after[4], "USER", 4))
310 id_to_name(id, USERS_TABLE, &name);
311 strcpy(after[5], name);
313 else if (!strncmp(after[4], "LIST", 4))
315 id_to_name(id, LIST_TABLE, &name);
316 strcpy(after[5], name);
318 else if (!strncmp(after[4], "KERBEROS", 8))
320 id_to_name(id, STRINGS_TABLE, &name);
321 strcpy(after[5], name);
324 id_to_name(id, LIST_TABLE, &name);
325 strcpy(after[7], name);
328 strcpy(after[0], argv[0]);
329 strcpy(after[1], argv[1]);
333 strcpy(after[0], argv[0]);
334 strcpy(after[1], argv[1]);
335 name_to_id(after[0], MACHINE_TABLE, &id);
336 sprintf(after[2], "%d", id);
337 name_to_id(after[1], CONTAINERS_TABLE, &id);
338 sprintf(after[3], "%d", id);
340 EXEC SQL SELECT list_id INTO :after[4] FROM containers
343 id_to_name(id, LIST_TABLE, &name);
344 strcpy(after[4], name);
348 strcpy(after[0], argv[0]);
349 strcpy(after[1], argv[1]);
350 strcpy(after[2], argv[2]);
354 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
355 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
356 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
361 id_to_name(id, MACHINE_TABLE, &name);
362 strcpy(after[2], name);
364 id_to_name(id, USERS_TABLE, &name);
365 strcpy(after[7], name);
367 id_to_name(id, LIST_TABLE, &name);
368 strcpy(after[8], name);
373 strcpy(after[0], "?");
374 strcpy(after[1], argv[1]);
375 strcpy(after[2], "?");
376 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
377 "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
383 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
384 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
385 "l.description, l.list_id FROM list l WHERE %s", qual);
391 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
392 "grouplist, gid FROM list WHERE list_id = %d", id);
395 id_to_name(id, LIST_TABLE, &name);
397 strcpy(after[0], name);
398 strcpy(after[1], argv[1]);
401 if (!strcmp(after[1], "USER"))
403 id_to_name(id, USERS_TABLE, &name2);
404 EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
405 FROM users WHERE users_id = :id;
406 EXEC SQL SELECT list_id INTO :after[10] FROM list
410 else if (!strcmp(after[1], "LIST"))
412 id_to_name(id, LIST_TABLE, &name2);
413 EXEC SQL SELECT list_id INTO :after[9] FROM list
415 sprintf(after[10], "%d", id);
418 else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
420 id_to_name(id, STRINGS_TABLE, &name2);
421 EXEC SQL SELECT list_id INTO :after[9] FROM list
424 else if (!strcmp(after[1], "MACHINE"))
426 id_to_name(id, MACHINE_TABLE, &name2);
427 EXEC SQL SELECT list_id INTO :after[9] FROM list
429 sprintf(after[10], "%d", id);
432 strcpy(after[2], name2);
442 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
448 iu = xmalloc(sizeof(struct iupdate));
449 iu->table = table_name[table];
450 iu->beforec = beforec;
451 iu->before = copy_argv(before, beforec);
453 iu->after = copy_argv(after, afterc);
454 sq_save_data(incremental_sq, iu);
457 void incremental_clear_after(void)
459 incremental_after(NO_TABLE, NULL, NULL);
463 /* Called when the current transaction is committed to start any queued
464 * incremental updates. This caches the update table the first time it
469 struct inc_cache *next;
470 char *table, *service;
474 void incremental_update(void)
476 static int inited = 0;
477 static struct inc_cache *cache;
479 EXEC SQL BEGIN DECLARE SECTION;
480 char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
481 EXEC SQL END DECLARE SECTION;
482 struct iupdate *iu, *iu_save;
488 EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
493 EXEC SQL FETCH inc INTO :tab, :serv;
496 c = xmalloc(sizeof(struct inc_cache));
498 c->table = xstrdup(strtrim(tab));
499 c->service = xstrdup(strtrim(serv));
503 EXEC SQL COMMIT WORK;
506 while (sq_remove_data(incremental_sq, &iu))
508 for (c = cache; c; c = c->next)
510 if (!strcmp(c->table, iu->table))
512 iu->service = c->service;
513 iu_save = xmalloc(sizeof(struct iupdate));
514 iu_save->service = iu->service;
515 iu_save->table = iu->table;
516 iu_save->beforec = iu->beforec;
517 iu_save->afterc = iu->afterc;
518 iu_save->before = copy_argv(iu->before, iu->beforec);
519 iu_save->after = copy_argv(iu->after, iu->afterc);
520 sq_save_data(incremental_exec, iu_save);
525 free_argv(iu->before, iu->beforec);
526 free_argv(iu->after, iu->afterc);
530 if (inc_running == 0)
534 /* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
535 We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
536 this function so proc will pass it through without reading it. */
539 void next_incremental(void)
542 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
546 if (!incremental_exec)
549 if (sq_empty(incremental_exec) ||
550 (inc_running && now - inc_started < INC_TIMEOUT))
554 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
556 sq_remove_data(incremental_exec, &iu);
558 sprintf(cbefore, "%d", iu->beforec);
560 sprintf(cafter, "%d", iu->afterc);
562 for (i = 0; i < iu->beforec; i++)
563 argv[4 + i] = iu->before[i];
564 for (i = 0; i < iu->afterc; i++)
565 argv[4 + iu->beforec + i] = iu->after[i];
567 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
569 argv[4 + iu->beforec + iu->afterc] = 0;
572 sigaddset(&sigs, SIGCHLD);
573 sigprocmask(SIG_BLOCK, &sigs, NULL);
581 com_err(whoami, 0, "Failed to start incremental update");
587 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
589 free_argv(iu->before, iu->beforec);
590 free_argv(iu->after, iu->afterc);
595 /* Called when the current transaction is aborted to throw away any queued
596 * incremental updates
599 void incremental_flush(void)
603 while (sq_get_data(incremental_sq, &iu))
605 free_argv(iu->before, iu->beforec);
606 free_argv(iu->after, iu->afterc);
609 sq_destroy(incremental_sq);
610 incremental_sq = sq_create();
614 char **copy_argv(char **argv, int argc)
616 char **ret = xmalloc(sizeof(char *) * argc);
618 ret[argc] = xstrdup(strtrim(argv[argc]));
622 void free_argv(char **argv, int argc)
629 int table_num(char *name)
633 for (i = num_tables - 1; i; i--)
635 if (!strcmp(table_name[i], name))
639 return i; /* 0 = "none" if no match */