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 /* structures to save before args */
32 static char beforeb[MAXARGC][ARGLEN];
33 static char *before[MAXARGC];
34 EXEC SQL BEGIN DECLARE SECTION;
35 char *barg0, *barg1, *barg2, *barg3, *barg4;
36 char *barg5, *barg6, *barg7, *barg8, *barg9;
37 char *barg10, *barg11, *barg12, *barg13, *barg14;
38 EXEC SQL END DECLARE SECTION;
40 static char *beforetable;
42 /* structures to save after args */
43 static char afterb[MAXARGC][ARGLEN];
44 static char *after[MAXARGC];
45 EXEC SQL BEGIN DECLARE SECTION;
46 char *aarg0, *aarg1, *aarg2, *aarg3, *aarg4;
47 char *aarg5, *aarg6, *aarg7, *aarg8, *aarg9;
48 char *aarg10, *aarg11, *aarg12, *aarg13, *aarg14;
49 EXEC SQL END DECLARE SECTION;
52 /* structures to save entire sets of incremental changes */
53 struct save_queue *incremental_sq = NULL;
54 struct save_queue *incremental_exec = NULL;
69 for (i = 0; i < MAXARGC; i++) {
70 before[i] = &beforeb[i][0];
71 after[i] = &afterb[i][0];
103 if (incremental_sq == NULL)
104 incremental_sq = sq_create();
105 if (incremental_exec == NULL)
106 incremental_exec = sq_create();
110 incremental_before(table, qual, argv)
115 EXEC SQL BEGIN DECLARE SECTION;
117 EXEC SQL END DECLARE SECTION;
119 char buffer[512], *name;
123 if (!strcmp(table, "users")) {
125 * retrieve (barg0 = u.login, barg1 = text(u.uid),
126 * barg2 = u.shell, barg3 = u.last, barg4 = u.first,
127 * barg5 = u.middle, barg6 = text(u.status),
128 * barg7 = u.mit_id, barg8 = u.mit_year)
131 EXEC SQL SELECT login, uid, shell, last, first, middle,
132 text(u.status), mit_id, mit_year
133 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
135 FROM users WHERE :qual;
137 } else if (!strcmp(table, "machine")) {
139 * retrieve (barg0 = m.name, barg1 = m.type) where qual
141 EXEC SQL SELECT name, type INTO :barg0, :barg1 FROM machine
144 } else if (!strcmp(table, "cluster")) {
146 * retrieve (barg0 = c.name, barg1 = c.desc, barg2 = c.location)
149 EXEC SQL SELECT name, desc, location INTO :barg0, :barg1, :barg2
150 FROM cluster WHERE :qual;
152 } else if (!strcmp(table, "mcmap")) {
153 strcpy(barg0, argv[0]);
154 strcpy(barg1, argv[1]);
156 } else if (!strcmp(table, "svc")) {
157 strcpy(barg0, argv[0]);
158 strcpy(barg1, argv[1]);
159 strcpy(barg2, argv[2]);
161 } else if (!strcmp(table, "filesys")) {
163 * range of fs is filesys
164 * retrieve (barg0 = fs.label, barg1 = fs.type, barg2 = text(fs.mach_id),
165 * barg3 = fs.name, barg4 = fs.mount, barg5 = fs.access,
166 * barg6 = fs.comments, barg7 = text(fs.owner),
167 * barg8 = text(fs.owners), barg9 = text(fs.createflg),
168 * barg10 = fs.lockertype)
171 EXEC SQL SELECT label, type, text(mach_id), name, mount, access,
172 comments, text(owner), text(owners), text(createflag), lockertype
173 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
174 :barg7, :barg8, :barg9, :barg10
175 FROM filesys WHERE :qual;
177 } else if (!strcmp(table, "quota")) {
179 strcpy(barg1, argv[1]);
181 sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
184 * range of q is quota
185 * retrieve (barg3 = text(q.quota), barg4 = filesys.name) where qual
187 EXEC SQL SELECT q.quota, fs.name INTO :barg3, :barg4
188 FROM quota q, filesys fs WHERE :qual;
190 } else if (!strcmp(table, "list")) {
192 * retrieve (barg0 = l.name, barg1 = text(l.active),
193 * barg2 = text(l.public), barg3 = text(l.hidden),
194 * barg4 = text(l.maillist), barg5 = text(l.group),
195 * barg6 = text(l.gid), barg7 = l.acl_type,
196 * barg8 = text(l.acl_id), barg9 = l.desc)
199 EXEC SQL SELECT name, text(active), text(public), text(hidden),
200 text(maillist), text(grouplist), text(gid), acl_type,
202 INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
203 :barg7, :barg8, :barg9
204 FROM list WHERE :qual;
206 } else if (!strcmp(table, "members")) {
209 * retrieve (barg3 = text(list.group)) where list.list_id = id
211 EXEC SQL SELECT text(list.group) INTO :barg3 FROM list
214 id_to_name(id, "LIST", &name);
216 strcpy(barg1, argv[1]);
218 if (!strcmp(barg1, "USER")) {
219 id_to_name(id, barg1, &name);
220 } else if (!strcmp(barg1, "LIST")) {
221 id_to_name(id, barg1, &name);
222 } else if (!strcmp(barg1, "STRING")) {
223 id_to_name(id, barg1, &name);
224 } else if (!strcmp(barg1, "KERBEROS")) {
225 id_to_name(id, "STRING", &name);
231 com_err(whoami, 0, "unknown table in incremental_before"); */
235 incremental_clear_before()
240 incremental_clear_after()
242 incremental_after("clear", 0);
247 incremental_after(table, qual, argv)
248 EXEC SQL BEGIN DECLARE SECTION;
251 EXEC SQL END DECLARE SECTION;
254 char buffer[2048], *name;
255 EXEC SQL BEGIN DECLARE SECTION;
257 EXEC SQL END DECLARE SECTION;
261 if (!strcmp(table, "users")) {
263 * retrieve (aarg0 = u.login, aarg1 = text(u.uid),
264 * aarg2 = u.shell, aarg3 = u.last, aarg4 = u.first,
265 * aarg5 = u.middle, aarg6 = text(u.status),
266 * aarg7 = u.mit_id, aarg8 = u.mit_year)
269 EXEC SQL SELECT login, uid, shell, last, first, middle,
270 text(status), mit_id, mit_year
271 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5,
272 :aarg6, :aarg7, :aarg8
273 FROM users WHERE :qual;
275 } else if (!strcmp(table, "machine")) {
277 * retrieve (aarg0 = m.name, aarg1 = m.type) where qual
279 EXEC SQL SELECT name, type INTO :aarg0, :aarg1
280 FROM machine WHERE :qual;
282 } else if (!strcmp(table, "cluster")) {
284 * retrieve (aarg0 = c.name, aarg1 = c.desc, aarg2 = c.location)
287 EXEC SQL SELECT name, desc, location INTO :aarg0, :aarg1, :aarg2
288 FROM cluster WHERE :qual;
290 } else if (!strcmp(table, "mcmap")) {
291 strcpy(aarg0, argv[0]);
292 strcpy(aarg1, argv[1]);
294 } else if (!strcmp(table, "svc")) {
295 strcpy(aarg0, argv[0]);
296 strcpy(aarg1, argv[1]);
297 strcpy(aarg2, argv[2]);
299 } else if (!strcmp(table, "filesys")) {
301 * range of fs is filesys
302 * retrieve (aarg0 = fs.label, aarg1 = fs.type,
303 * aarg2 = text(fs.mach_id),
304 * aarg3 = fs.name, aarg4 = fs.mount, aarg5 = fs.access,
305 * aarg6 = fs.comments, aarg7 = text(fs.owner),
306 * aarg8 = text(fs.owners), aarg9 = text(fs.createflg),
307 * aarg10 = fs.lockertype)
310 EXEC SQL SELECT label, type, text(mach_id), name, mount, access,
311 comments, text(owner), text(owners), text(createflag), lockertype
312 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
313 :aarg7, :aarg8, :aarg9, :aarg10
314 FROM filesys fs WHERE :qual;
316 } else if (!strcmp(table, "quota")) {
318 strcpy(aarg1, argv[1]);
320 sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
323 * range of q is quota
324 * retrieve (aarg3 = text(q.quota), aarg4 = filesys.name) where qual
326 EXEC SQL SELECT text(q.quota), fs.name INTO :aarg3, :aarg4
327 FROM quota q, filesys fs WHERE :qual;
329 } else if (!strcmp(table, "list")) {
331 * retrieve (aarg0 = l.name, aarg1 = text(l.active),
332 * aarg2 = text(l.public), aarg3 = text(l.hidden),
333 * aarg4 = text(l.maillist), aarg5 = text(l.group),
334 * aarg6 = text(l.gid), aarg7 = l.acl_type,
335 * aarg8 = text(l.acl_id), aarg9 = l.desc)
338 EXEC SQL SELECT name, text(active), text(public), text(hidden),
339 text(maillist), text(grouplist), text(gid), acl_type,
341 INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
342 :aarg7, :aarg8, :aarg9
343 FROM list WHERE :qual;
345 } else if (!strcmp(table, "members")) {
348 * retrieve (aarg3 = text(list.group)) where list.list_id = id
350 EXEC SQL SELECT text(list.group) INTO :aarg3 FROM list
353 id_to_name(id, "LIST", &name);
355 strcpy(aarg1, argv[1]);
357 if (!strcmp(aarg1, "USER")) {
358 id_to_name(id, aarg1, &name);
359 } else if (!strcmp(aarg1, "LIST")) {
360 id_to_name(id, aarg1, &name);
361 } else if (!strcmp(aarg1, "STRING")) {
362 id_to_name(id, aarg1, &name);
363 } else if (!strcmp(aarg1, "KERBEROS")) {
364 id_to_name(id, "STRING", &name);
369 } else if (!strcmp(table, "clear")) {
373 com_err(whoami, 0, "unknown table in incremental_after"); */
375 iu = (struct iupdate *) malloc(sizeof(struct iupdate));
376 iu->table = strsave(table);
377 iu->beforec = beforec;
378 iu->before = copy_argv(before, beforec);
380 iu->after = copy_argv(after, afterc);
381 sq_save_data(incremental_sq, iu);
384 sprintf(buffer, "INCREMENTAL(%s, [", table);
385 for (i = 0; i < beforec; i++) {
387 strcat(buffer, strtrim(before[0]));
389 strcat(buffer, ", ");
390 strcat(buffer, strtrim(before[i]));
393 strcat(buffer, "], [");
394 for (i = 0; i < afterc; i++) {
396 strcat(buffer, strtrim(after[0]));
398 strcat(buffer, ", ");
399 strcat(buffer, strtrim(after[i]));
402 strcat(buffer, "])");
403 com_err(whoami, 0, buffer);
408 /* Called when the current transaction is committed to start any queued
409 * incremental updates. This caches the update table the first time it
414 struct inc_cache *next;
422 static int inited = 0;
423 static struct inc_cache *cache;
425 EXEC SQL BEGIN DECLARE SECTION;
426 char tab[17], serv[17];
427 EXEC SQL END DECLARE SECTION;
433 EXEC SQL DECLARE inc CURSOR FOR SELECT table, service FROM incremental;
436 EXEC SQL FETCH inc INTO :tab, :serv;
437 if (sqlca.sqlcode != 0) break;
438 c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
440 c->table = strsave(strtrim(tab));
441 c->service = strsave(strtrim(serv));
447 while (sq_remove_data(incremental_sq, &iu)) {
448 for (c = cache; c; c = c->next) {
449 if (!strcmp(c->table, iu->table)) {
450 iu->service = c->service;
451 sq_save_data(incremental_exec, iu);
455 if (inc_running == 0)
463 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
466 if (incremental_exec == NULL)
469 if (sq_empty(incremental_exec) ||
470 (inc_running && now - inc_started < INC_TIMEOUT))
474 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
476 sq_remove_data(incremental_exec, &iu);
478 sprintf(cbefore, "%d", iu->beforec);
480 sprintf(cafter, "%d", iu->afterc);
482 for (i = 0; i < iu->beforec; i++)
483 argv[4 + i] = iu->before[i];
484 for (i = 0; i < iu->afterc; i++)
485 argv[4 + iu->beforec + i] = iu->after[i];
487 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
489 com_err(whoami, 0, "forking %s", prog);
492 argv[4 + iu->beforec + iu->afterc] = 0;
499 com_err(whoami, 0, "Failed to start incremental update");
506 free_argv(iu->before, iu->beforec);
507 free_argv(iu->after, iu->afterc);
514 /* Called when the current transaction is aborted to throw away any queued
515 * incremental updates
522 while (sq_get_data(incremental_sq, &iu)) {
523 free_argv(iu->before, iu->beforec);
524 free_argv(iu->after, iu->afterc);
528 sq_destroy(incremental_sq);
529 incremental_sq = sq_create();
533 char **copy_argv(argv, argc)
537 char **ret = (char **)malloc(sizeof(char *) * argc);
539 ret[argc] = strsave(strtrim(argv[argc]));
543 free_argv(argv, argc)