6 * Copyright (C) 1989 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
13 static char *rcsid_increment_dc = "$Header$";
16 #include <mit-copyright.h>
19 #include "mr_server.h"
20 EXEC SQL INCLUDE sqlca;
31 EXEC SQL WHENEVER SQLERROR CALL ingerr;
33 /* structures to save before args */
34 static char beforeb[MAXARGC][ARGLEN];
35 static char *before[MAXARGC];
36 EXEC SQL BEGIN DECLARE SECTION;
37 char *barg0, *barg1, *barg2, *barg3, *barg4;
38 char *barg5, *barg6, *barg7, *barg8, *barg9;
39 char *barg10, *barg11, *barg12, *barg13, *barg14;
40 EXEC SQL END DECLARE SECTION;
42 static char *beforetable;
44 /* structures to save after args */
45 static char afterb[MAXARGC][ARGLEN];
46 static char *after[MAXARGC];
47 EXEC SQL BEGIN DECLARE SECTION;
48 char *aarg0, *aarg1, *aarg2, *aarg3, *aarg4;
49 char *aarg5, *aarg6, *aarg7, *aarg8, *aarg9;
50 char *aarg10, *aarg11, *aarg12, *aarg13, *aarg14;
51 EXEC SQL END DECLARE SECTION;
54 /* structures to save entire sets of incremental changes */
55 struct save_queue *incremental_sq = NULL;
56 struct save_queue *incremental_exec = NULL;
71 for (i = 0; i < MAXARGC; i++) {
72 before[i] = &beforeb[i][0];
73 after[i] = &afterb[i][0];
105 if (incremental_sq == NULL)
106 incremental_sq = sq_create();
107 if (incremental_exec == NULL)
108 incremental_exec = sq_create();
112 incremental_before(table, qual, argv)
114 EXEC SQL BEGIN DECLARE SECTION;
116 EXEC SQL END DECLARE SECTION;
119 EXEC SQL BEGIN DECLARE SECTION;
121 EXEC SQL END DECLARE SECTION;
123 char buffer[512], *name;
127 if (!strcmp(table, "users")) {
129 * retrieve (barg0 = u.login, barg1 = text(u.uid),
130 * barg2 = u.shell, barg3 = u.last, barg4 = u.first,
131 * barg5 = u.middle, barg6 = text(u.status),
132 * barg7 = u.mit_id, barg8 = u.mit_year)
135 EXEC SQL SELECT login, CHAR(uid), shell, last, first, middle,
136 CHAR(status), clearid, type
137 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
139 FROM users WHERE :qual;
141 } else if (!strcmp(table, "machine")) {
143 * retrieve (barg0 = m.name, barg1 = m.type) where qual
145 EXEC SQL SELECT name, type INTO :barg0, :barg1 FROM machine
148 } else if (!strcmp(table, "cluster")) {
150 * retrieve (barg0 = c.name, barg1 = c.desc, barg2 = c.location)
153 EXEC SQL SELECT name, description, location
154 INTO :barg0, :barg1, :barg2
155 FROM cluster WHERE :qual;
157 } else if (!strcmp(table, "mcmap")) {
158 strcpy(barg0, argv[0]);
159 strcpy(barg1, argv[1]);
161 } else if (!strcmp(table, "svc")) {
162 strcpy(barg0, argv[0]);
163 strcpy(barg1, argv[1]);
164 strcpy(barg2, argv[2]);
166 } else if (!strcmp(table, "filesys")) {
168 * range of fs is filesys
169 * retrieve (barg0 = fs.label, barg1 = fs.type, barg2 = text(fs.mach_id),
170 * barg3 = fs.name, barg4 = fs.mount, barg5 = fs.access,
171 * barg6 = fs.comments, barg7 = text(fs.owner),
172 * barg8 = text(fs.owners), barg9 = text(fs.createflg),
173 * barg10 = fs.lockertype)
176 EXEC SQL SELECT label, type, CHAR(mach_id), name, mount, access,
177 comments, CHAR(owner), CHAR(owners), CHAR(createflag), lockertype
178 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
179 :barg7, :barg8, :barg9, :barg10
180 FROM filesys WHERE :qual;
182 } else if (!strcmp(table, "quota")) {
184 strcpy(barg1, argv[1]);
186 sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
189 * range of q is quota
190 * retrieve (barg3 = text(q.quota), barg4 = filesys.name) where qual
192 EXEC SQL SELECT q.quota, fs.name INTO :barg3, :barg4
193 FROM quota q, filesys fs WHERE :qual;
195 } else if (!strcmp(table, "list")) {
197 * retrieve (barg0 = l.name, barg1 = text(l.active),
198 * barg2 = text(l.public), barg3 = text(l.hidden),
199 * barg4 = text(l.maillist), barg5 = text(l.group),
200 * barg6 = text(l.gid), barg7 = l.acl_type,
201 * barg8 = text(l.acl_id), barg9 = l.desc)
204 EXEC SQL SELECT name, CHAR(active), CHAR(publicflg), CHAR(hidden),
205 CHAR(maillist), CHAR(grouplist), CHAR(gid), acl_type,
206 CHAR(acl_id), description
207 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
208 :barg7, :barg8, :barg9
209 FROM list WHERE :qual;
211 } else if (!strcmp(table, "members")) {
214 * retrieve (barg3 = text(list.group)) where list.list_id = id
216 EXEC SQL SELECT CHAR(grouplist) INTO :barg3 FROM list
219 id_to_name(id, "LIST", &name);
221 strcpy(barg1, argv[1]);
223 if (!strcmp(barg1, "USER")) {
224 id_to_name(id, barg1, &name);
225 } else if (!strcmp(barg1, "LIST")) {
226 id_to_name(id, barg1, &name);
227 } else if (!strcmp(barg1, "STRING")) {
228 id_to_name(id, barg1, &name);
229 } else if (!strcmp(barg1, "KERBEROS")) {
230 id_to_name(id, "STRING", &name);
236 com_err(whoami, 0, "unknown table in incremental_before"); */
240 incremental_clear_before()
245 incremental_clear_after()
247 incremental_after("clear", 0);
252 incremental_after(table, qual, argv)
254 EXEC SQL BEGIN DECLARE SECTION;
256 EXEC SQL END DECLARE SECTION;
259 char buffer[2048], *name;
260 EXEC SQL BEGIN DECLARE SECTION;
262 EXEC SQL END DECLARE SECTION;
266 if (!strcmp(table, "users")) {
268 * retrieve (aarg0 = u.login, aarg1 = text(u.uid),
269 * aarg2 = u.shell, aarg3 = u.last, aarg4 = u.first,
270 * aarg5 = u.middle, aarg6 = text(u.status),
271 * aarg7 = u.mit_id, aarg8 = u.mit_year)
274 EXEC SQL SELECT login, CHAR(uid), shell, last, first, middle,
275 CHAR(status), clearid, type
276 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5,
277 :aarg6, :aarg7, :aarg8
278 FROM users WHERE :qual;
280 } else if (!strcmp(table, "machine")) {
282 * retrieve (aarg0 = m.name, aarg1 = m.type) where qual
284 EXEC SQL SELECT name, type INTO :aarg0, :aarg1
285 FROM machine WHERE :qual;
287 } else if (!strcmp(table, "cluster")) {
289 * retrieve (aarg0 = c.name, aarg1 = c.desc, aarg2 = c.location)
292 EXEC SQL SELECT name, description, location
293 INTO :aarg0, :aarg1, :aarg2
294 FROM cluster WHERE :qual;
296 } else if (!strcmp(table, "mcmap")) {
297 strcpy(aarg0, argv[0]);
298 strcpy(aarg1, argv[1]);
300 } else if (!strcmp(table, "svc")) {
301 strcpy(aarg0, argv[0]);
302 strcpy(aarg1, argv[1]);
303 strcpy(aarg2, argv[2]);
305 } else if (!strcmp(table, "filesys")) {
307 * range of fs is filesys
308 * retrieve (aarg0 = fs.label, aarg1 = fs.type,
309 * aarg2 = text(fs.mach_id),
310 * aarg3 = fs.name, aarg4 = fs.mount, aarg5 = fs.access,
311 * aarg6 = fs.comments, aarg7 = text(fs.owner),
312 * aarg8 = text(fs.owners), aarg9 = text(fs.createflg),
313 * aarg10 = fs.lockertype)
316 EXEC SQL SELECT label, type, CHAR(mach_id), name, mount, access,
317 comments, CHAR(owner), CHAR(owners), CHAR(createflag), lockertype
318 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
319 :aarg7, :aarg8, :aarg9, :aarg10
320 FROM filesys fs WHERE :qual;
322 } else if (!strcmp(table, "quota")) {
324 strcpy(aarg1, argv[1]);
326 sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
329 * range of q is quota
330 * retrieve (aarg3 = text(q.quota), aarg4 = filesys.name) where qual
332 EXEC SQL SELECT CHAR(q.quota), fs.name INTO :aarg3, :aarg4
333 FROM quota q, filesys fs WHERE :qual;
335 } else if (!strcmp(table, "list")) {
337 * retrieve (aarg0 = l.name, aarg1 = text(l.active),
338 * aarg2 = text(l.public), aarg3 = text(l.hidden),
339 * aarg4 = text(l.maillist), aarg5 = text(l.group),
340 * aarg6 = text(l.gid), aarg7 = l.acl_type,
341 * aarg8 = text(l.acl_id), aarg9 = l.desc)
344 EXEC SQL SELECT name, CHAR(active), CHAR(publicflg), CHAR(hidden),
345 CHAR(maillist), CHAR(grouplist), CHAR(gid), acl_type,
346 CHAR(acl_id), description
347 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
348 :aarg7, :aarg8, :aarg9
349 FROM list WHERE :qual;
351 } else if (!strcmp(table, "members")) {
354 * retrieve (aarg3 = text(list.group)) where list.list_id = id
356 EXEC SQL SELECT CHAR(grouplist) INTO :aarg3 FROM list
359 id_to_name(id, "LIST", &name);
361 strcpy(aarg1, argv[1]);
363 if (!strcmp(aarg1, "USER")) {
364 id_to_name(id, aarg1, &name);
365 } else if (!strcmp(aarg1, "LIST")) {
366 id_to_name(id, aarg1, &name);
367 } else if (!strcmp(aarg1, "STRING")) {
368 id_to_name(id, aarg1, &name);
369 } else if (!strcmp(aarg1, "KERBEROS")) {
370 id_to_name(id, "STRING", &name);
375 } else if (!strcmp(table, "clear")) {
379 com_err(whoami, 0, "unknown table in incremental_after"); */
381 iu = (struct iupdate *) malloc(sizeof(struct iupdate));
382 iu->table = strsave(table);
383 iu->beforec = beforec;
384 iu->before = copy_argv(before, beforec);
386 iu->after = copy_argv(after, afterc);
387 sq_save_data(incremental_sq, iu);
390 sprintf(buffer, "INCREMENTAL(%s, [", table);
391 for (i = 0; i < beforec; i++) {
393 strcat(buffer, strtrim(before[0]));
395 strcat(buffer, ", ");
396 strcat(buffer, strtrim(before[i]));
399 strcat(buffer, "], [");
400 for (i = 0; i < afterc; i++) {
402 strcat(buffer, strtrim(after[0]));
404 strcat(buffer, ", ");
405 strcat(buffer, strtrim(after[i]));
408 strcat(buffer, "])");
409 com_err(whoami, 0, buffer);
414 /* Called when the current transaction is committed to start any queued
415 * incremental updates. This caches the update table the first time it
420 struct inc_cache *next;
428 static int inited = 0;
429 static struct inc_cache *cache;
431 EXEC SQL BEGIN DECLARE SECTION;
432 char tab[17], serv[17];
433 EXEC SQL END DECLARE SECTION;
439 EXEC SQL DECLARE inc CURSOR FOR SELECT tablename, service FROM incremental;
442 EXEC SQL FETCH inc INTO :tab, :serv;
443 if (sqlca.sqlcode != 0) break;
444 c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
446 c->table = strsave(strtrim(tab));
447 c->service = strsave(strtrim(serv));
453 while (sq_remove_data(incremental_sq, &iu)) {
454 for (c = cache; c; c = c->next) {
455 if (!strcmp(c->table, iu->table)) {
456 iu->service = c->service;
457 sq_save_data(incremental_exec, iu);
461 if (inc_running == 0)
469 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
472 if (incremental_exec == NULL)
475 if (sq_empty(incremental_exec) ||
476 (inc_running && now - inc_started < INC_TIMEOUT))
480 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
482 sq_remove_data(incremental_exec, &iu);
484 sprintf(cbefore, "%d", iu->beforec);
486 sprintf(cafter, "%d", iu->afterc);
488 for (i = 0; i < iu->beforec; i++)
489 argv[4 + i] = iu->before[i];
490 for (i = 0; i < iu->afterc; i++)
491 argv[4 + iu->beforec + i] = iu->after[i];
493 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
495 com_err(whoami, 0, "forking %s", prog);
498 argv[4 + iu->beforec + iu->afterc] = 0;
505 com_err(whoami, 0, "Failed to start incremental update");
512 free_argv(iu->before, iu->beforec);
513 free_argv(iu->after, iu->afterc);
520 /* Called when the current transaction is aborted to throw away any queued
521 * incremental updates
528 while (sq_get_data(incremental_sq, &iu)) {
529 free_argv(iu->before, iu->beforec);
530 free_argv(iu->after, iu->afterc);
534 sq_destroy(incremental_sq);
535 incremental_sq = sq_create();
539 char **copy_argv(argv, argc)
543 char **ret = (char **)malloc(sizeof(char *) * argc);
545 ret[argc] = strsave(strtrim(argv[argc]));
549 free_argv(argv, argc)