]> andersk Git - moira.git/blob - server/increment.dc
Canonicalized correlation names.
[moira.git] / server / increment.dc
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1989 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  * 
10  */
11
12 #ifndef lint
13 static char *rcsid_increment_dc = "$Header$";
14 #endif lint
15
16 #include <fcntl.h>
17 #include <mit-copyright.h>
18 #include <moira.h>
19 #include "query.h"
20 #include "mr_server.h"
21 EXEC SQL INCLUDE sqlca;
22
23 extern char *whoami;
24 char *malloc();
25
26 int inc_pid = 0;
27 int inc_running = 0;
28 time_t inc_started;
29
30 #define MAXARGC 15
31
32 EXEC SQL WHENEVER SQLERROR CALL ingerr;
33
34 /* structures to save before args */
35 static char beforeb[MAXARGC][ARGLEN];
36 static char *before[MAXARGC];
37 EXEC SQL BEGIN DECLARE SECTION;
38 char *barg0, *barg1, *barg2, *barg3, *barg4;
39 char *barg5, *barg6, *barg7, *barg8, *barg9;
40 char *barg10, *barg11, *barg12, *barg13, *barg14;
41 EXEC SQL END DECLARE SECTION;
42 static int beforec;
43 static char *beforetable;
44
45 /* structures to save after args */
46 static char afterb[MAXARGC][ARGLEN];
47 static char *after[MAXARGC];
48 EXEC SQL BEGIN DECLARE SECTION;
49 char *aarg0, *aarg1, *aarg2, *aarg3, *aarg4;
50 char *aarg5, *aarg6, *aarg7, *aarg8, *aarg9;
51 char *aarg10, *aarg11, *aarg12, *aarg13, *aarg14;
52 EXEC SQL END DECLARE SECTION;
53 static int afterc;
54
55 /* structures to save entire sets of incremental changes */
56 struct save_queue *incremental_sq = NULL;
57 struct save_queue *incremental_exec = NULL;
58 struct iupdate {
59     char *table;
60     int beforec;
61     char **before;
62     int afterc;
63     char **after;
64     char *service;
65 };
66
67
68 incremental_init()
69 {
70     int i;
71
72     for (i = 0; i < MAXARGC; i++) {
73         before[i] = &beforeb[i][0];
74         after[i] = &afterb[i][0];
75     }
76     barg0 = before[0];
77     barg1 = before[1];
78     barg2 = before[2];
79     barg3 = before[3];
80     barg4 = before[4];
81     barg5 = before[5];
82     barg6 = before[6];
83     barg7 = before[7];
84     barg8 = before[8];
85     barg9 = before[9];
86     barg10 = before[10];
87     barg11 = before[11];
88     barg12 = before[12];
89     barg13 = before[13];
90     barg14 = before[14];
91     aarg0 = after[0];
92     aarg1 = after[1];
93     aarg2 = after[2];
94     aarg3 = after[3];
95     aarg4 = after[4];
96     aarg5 = after[5];
97     aarg6 = after[6];
98     aarg7 = after[7];
99     aarg8 = after[8];
100     aarg9 = after[9];
101     aarg10 = after[10];
102     aarg11 = after[11];
103     aarg12 = after[12];
104     aarg13 = after[13];
105     aarg14 = after[14];
106     if (incremental_sq == NULL)
107       incremental_sq = sq_create();
108     if (incremental_exec == NULL)
109       incremental_exec = sq_create();
110 }
111
112
113 incremental_before(table, qual, argv)
114 char *table;
115 EXEC SQL BEGIN DECLARE SECTION; 
116 char *qual;
117 EXEC SQL END DECLARE SECTION; 
118 char **argv;
119 {
120     EXEC SQL BEGIN DECLARE SECTION;
121     int id;
122     EXEC SQL END DECLARE SECTION;
123
124     char buffer[512], *name;
125
126     beforetable = table;
127
128     if (!strcmp(table, "users")) {
129         EXEC SQL SELECT u.login, CHAR(u.uid), u.shell, u.last, u.first, u.middle, 
130             CHAR(u.status), u.clearid, u.type
131           INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
132             :barg7, :barg8
133           FROM users u WHERE :qual;
134         beforec = 9;
135     } else if (!strcmp(table, "machine")) {
136         EXEC SQL SELECT n.name, n.type INTO :barg0, :barg1 FROM machine m
137           WHERE :qual;
138         beforec = 2;
139     } else if (!strcmp(table, "cluster")) {
140         EXEC SQL SELECT c.name, c.description, c.location 
141           INTO :barg0, :barg1, :barg2
142           FROM cluster c WHERE :qual;
143         beforec = 3;
144     } else if (!strcmp(table, "mcmap")) {
145         strcpy(barg0, argv[0]);
146         strcpy(barg1, argv[1]);
147         beforec = 2;
148     } else if (!strcmp(table, "svc")) {
149         strcpy(barg0, argv[0]);
150         strcpy(barg1, argv[1]);
151         strcpy(barg2, argv[2]);
152         beforec = 3;
153     } else if (!strcmp(table, "filesys")) {
154         EXEC SQL SELECT fs.label, fs.type, CHAR(fs.mach_id), fs.name, 
155             fs.mount, fs.access, fs.comments, CHAR(fs.owner), CHAR(fs.owners),
156             CHAR(fs.createflg), fs.lockertype
157           INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6, 
158             :barg7, :barg8, :barg9, :barg10
159           FROM filesys fs WHERE :qual;
160         name = malloc(0);
161         id = atoi(barg2);
162         id_to_name(id, "MACHINE", &name);
163         strcpy(barg2, name);
164         id = atoi(barg7);
165         id_to_name(id, "USER", &name);
166         strcpy(barg7, name);
167         id = atoi(barg8);
168         id_to_name(id, "LIST", &name);
169         strcpy(barg8, name);
170         free(name);
171         beforec = 11;
172     } else if (!strcmp(table, "quota")) {
173         strcpy(barg0, "?");
174         strcpy(barg1, argv[1]);
175         strcpy(barg2, "?");
176         sprintf(buffer, "%s AND fs.filsys_id = q.filsys_id", qual);
177         qual = buffer;
178         EXEC SQL SELECT CHAR(q.quota), fs.name INTO :barg3, :barg4
179           FROM quota q, filesys fs WHERE :qual;
180         beforec = 5;
181     } else if (!strcmp(table, "list")) {
182         EXEC SQL SELECT l.name, CHAR(l.active), CHAR(l.publicflg), 
183             CHAR(l.hidden), CHAR(l.maillist), CHAR(l.grouplist), CHAR(l.gid), 
184             l.acl_type, CHAR(l.acl_id), l.description
185           INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
186             :barg7, :barg8, :barg9
187           FROM list l WHERE :qual;
188         beforec = 10;
189     } else if (!strcmp(table, "members")) {
190         id = (int) argv[0];
191         EXEC SQL SELECT CHAR(grouplist) INTO :barg3 FROM list 
192           WHERE list_id = :id;
193         name = malloc(0);
194         id_to_name(id, "LIST", &name);
195         strcpy(barg0, name);
196         strcpy(barg1, argv[1]);
197         id = (int) argv[2];
198         if (!strcmp(barg1, "USER")) {
199             id_to_name(id, barg1, &name);
200         } else if (!strcmp(barg1, "LIST")) {
201             id_to_name(id, barg1, &name);
202         } else if (!strcmp(barg1, "STRING")) {
203             id_to_name(id, barg1, &name);
204         } else if (!strcmp(barg1, "KERBEROS")) {
205             id_to_name(id, "STRING", &name);
206         }
207         strcpy(barg2, name);
208         free(name);
209         beforec = 4;
210     } /* else
211       com_err(whoami, 0, "unknown table in incremental_before"); */
212 }
213
214
215 incremental_clear_before()
216 {
217     beforec = 0;
218 }
219
220 incremental_clear_after()
221 {
222     incremental_after("clear", 0);
223 }
224
225
226
227 incremental_after(table, qual, argv)
228 char *table;
229 EXEC SQL BEGIN DECLARE SECTION; 
230 char *qual;
231 EXEC SQL END DECLARE SECTION; 
232 char **argv;
233 {
234     char buffer[2048], *name;
235 EXEC SQL BEGIN DECLARE SECTION; 
236     int id, i;
237 EXEC SQL END DECLARE SECTION; 
238     struct iupdate *iu;
239     char **copy_argv();
240
241     if (!strcmp(table, "users")) {
242         EXEC SQL SELECT u.login, CHAR(u.uid), u.shell, u.last, u.first, 
243             u.middle, CHAR(u.status), u.clearid, u.type
244           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5,
245             :aarg6, :aarg7, :aarg8
246           FROM users u WHERE :qual;
247         afterc = 9;
248     } else if (!strcmp(table, "machine")) {
249         EXEC SQL SELECT m.name, m.type INTO :aarg0, :aarg1
250           FROM machine m WHERE :qual;
251         afterc = 2;
252     } else if (!strcmp(table, "cluster")) {
253         EXEC SQL SELECT c.name, c.description, c.location 
254           INTO :aarg0, :aarg1, :aarg2
255           FROM cluster c WHERE :qual;
256         afterc = 3;
257     } else if (!strcmp(table, "mcmap")) {
258         strcpy(aarg0, argv[0]);
259         strcpy(aarg1, argv[1]);
260         afterc = 2;
261     } else if (!strcmp(table, "svc")) {
262         strcpy(aarg0, argv[0]);
263         strcpy(aarg1, argv[1]);
264         strcpy(aarg2, argv[2]);
265         afterc = 3;
266     } else if (!strcmp(table, "filesys")) {
267         EXEC SQL SELECT CHAR(fs.label), fs.type, CHAR(fs.mach_id), fs.name, 
268             fs.mount, fs.access, fs.comments, CHAR(fs.owner), CHAR(fs.owners),
269             CHAR(fs.createflg), fs.lockertype
270           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
271               :aarg7, :aarg8, :aarg9, :aarg10
272           FROM filesys fs WHERE :qual;
273         name = malloc(0);
274         id = atoi(aarg2);
275         id_to_name(id, "MACHINE", &name);
276         strcpy(aarg2, name);
277         id = atoi(aarg7);
278         id_to_name(id, "USER", &name);
279         strcpy(aarg7, name);
280         id = atoi(aarg8);
281         id_to_name(id, "LIST", &name);
282         strcpy(aarg8, name);
283         free(name);
284         afterc = 11;
285     } else if (!strcmp(table, "quota")) {
286         strcpy(aarg0, "?");
287         strcpy(aarg1, argv[1]);
288         strcpy(aarg2, "?");
289         sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
290         qual = buffer;
291         EXEC SQL SELECT CHAR(q.quota), fs.name INTO :aarg3, :aarg4
292           FROM quota q, filesys fs WHERE :qual;
293         afterc = 5;
294     } else if (!strcmp(table, "list")) {
295         EXEC SQL SELECT l.name, CHAR(l.active), CHAR(l.publicflg), 
296             CHAR(l.hidden), CHAR(l.maillist), CHAR(l.grouplist), CHAR(l.gid), 
297             l.acl_type, CHAR(l.acl_id), l.description
298           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
299              :aarg7, :aarg8, :aarg9
300           FROM list l WHERE :qual;
301         afterc = 10;
302     } else if (!strcmp(table, "members")) {
303         id = (int) argv[0];
304         EXEC SQL SELECT CHAR(grouplist) INTO :aarg3 FROM list
305           WHERE list_id = :id;
306         name = malloc(0);
307         id_to_name(id, "LIST", &name);
308         strcpy(aarg0, name);
309         strcpy(aarg1, argv[1]);
310         id = (int) argv[2];
311         if (!strcmp(aarg1, "USER")) {
312             id_to_name(id, aarg1, &name);
313         } else if (!strcmp(aarg1, "LIST")) {
314             id_to_name(id, aarg1, &name);
315         } else if (!strcmp(aarg1, "STRING")) {
316             id_to_name(id, aarg1, &name);
317         } else if (!strcmp(aarg1, "KERBEROS")) {
318             id_to_name(id, "STRING", &name);
319         }
320         strcpy(aarg2, name);
321         free(name);
322         afterc = 4;
323     } else if (!strcmp(table, "clear")) {
324         afterc = 0;
325         table = beforetable;
326     } /* else
327       com_err(whoami, 0, "unknown table in incremental_after"); */
328
329     iu = (struct iupdate *) malloc(sizeof(struct iupdate));
330     iu->table = strsave(table);
331     iu->beforec = beforec;
332     iu->before = copy_argv(before, beforec);
333     iu->afterc = afterc;
334     iu->after = copy_argv(after, afterc);
335     sq_save_data(incremental_sq, iu);
336
337 #ifdef DEBUG
338     sprintf(buffer, "INCREMENTAL(%s, [", table);
339     for (i = 0; i < beforec; i++) {
340         if (i == 0)
341           strcat(buffer, strtrim(before[0]));
342         else {
343             strcat(buffer, ", ");
344             strcat(buffer, strtrim(before[i]));
345         }
346     }
347     strcat(buffer, "], [");
348     for (i = 0; i < afterc; i++) {
349         if (i == 0)
350           strcat(buffer, strtrim(after[0]));
351         else {
352             strcat(buffer, ", ");
353             strcat(buffer, strtrim(after[i]));
354         }
355     }
356     strcat(buffer, "])");
357     com_err(whoami, 0, buffer);
358 #endif DEBUG
359 }
360
361
362 /* Called when the current transaction is committed to start any queued
363  * incremental updates.  This caches the update table the first time it
364  * is called.
365  */
366
367 struct inc_cache {
368     struct inc_cache *next;
369     char *table;
370     char *service;
371 };
372
373
374 incremental_update()
375 {
376     static int inited = 0;
377     static struct inc_cache *cache;
378     struct inc_cache *c;
379     EXEC SQL BEGIN DECLARE SECTION;
380     char tab[17], serv[17];
381     EXEC SQL END DECLARE SECTION;
382     struct iupdate *iu;
383
384     if (!inited) {
385         inited++;
386
387         EXEC SQL DECLARE inc CURSOR FOR SELECT tablename, service FROM incremental;
388         EXEC SQL OPEN inc;
389         while (1) {
390             EXEC SQL FETCH inc INTO :tab, :serv;
391             if (sqlca.sqlcode != 0) break;
392             c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
393             c->next = cache;
394             c->table = strsave(strtrim(tab));
395             c->service = strsave(strtrim(serv));
396             cache = c;
397         }
398         EXEC SQL CLOSE inc;
399     }
400
401     while (sq_remove_data(incremental_sq, &iu)) {
402         for (c = cache; c; c = c->next) {
403             if (!strcmp(c->table, iu->table)) {
404                 iu->service = c->service;
405                 sq_save_data(incremental_exec, iu);
406             }
407         }
408     }
409     if (inc_running == 0)
410       next_incremental();
411 }
412
413
414 next_incremental()
415 {
416     struct iupdate *iu;
417     char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
418     int i;
419
420     if (incremental_exec == NULL)
421       incremental_init();
422
423     if (sq_empty(incremental_exec) ||
424         (inc_running && now - inc_started < INC_TIMEOUT))
425       return;
426
427     if (inc_running)
428       com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
429
430     sq_remove_data(incremental_exec, &iu);
431     argv[1] = iu->table;
432     sprintf(cbefore, "%d", iu->beforec);
433     argv[2] = cbefore;
434     sprintf(cafter, "%d", iu->afterc);
435     argv[3] = cafter;
436     for (i = 0; i < iu->beforec; i++)
437       argv[4 + i] = iu->before[i];
438     for (i = 0; i < iu->afterc; i++)
439       argv[4 + iu->beforec + i] = iu->after[i];
440
441     sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
442 #ifdef DEBUG
443     com_err(whoami, 0, "forking %s", prog);
444 #endif
445     argv[0] = prog;
446     argv[4 + iu->beforec + iu->afterc] = 0;
447     inc_pid = vfork();
448     switch (inc_pid) {
449     case 0:
450         execv(prog, argv);
451         _exit(1);
452     case -1:
453         com_err(whoami, 0, "Failed to start incremental update");
454         break;
455     default:
456         inc_running = 1;
457         inc_started = now;
458     }
459
460     free_argv(iu->before, iu->beforec);
461     free_argv(iu->after, iu->afterc);
462     free(iu->table);
463     free(iu);
464
465 }
466
467
468 /* Called when the current transaction is aborted to throw away any queued
469  * incremental updates
470  */
471
472 incremental_flush()
473 {
474     struct iupdate *iu;
475
476     while (sq_get_data(incremental_sq, &iu)) {
477         free_argv(iu->before, iu->beforec);
478         free_argv(iu->after, iu->afterc);
479         free(iu->table);
480         free(iu);
481     }
482     sq_destroy(incremental_sq);
483     incremental_sq = sq_create();
484 }
485
486
487 char **copy_argv(argv, argc)
488 char **argv;
489 int argc;
490 {
491     char **ret = (char **)malloc(sizeof(char *) * argc);
492     while (--argc >= 0)
493       ret[argc] = strsave(strtrim(argv[argc]));
494     return(ret);
495 }
496
497 free_argv(argv, argc)
498 char **argv;
499 int argc;
500 {
501     while (--argc >= 0)
502       free(argv[argc]);
503     free(argv);
504 }
This page took 0.086376 seconds and 5 git commands to generate.