]> andersk Git - moira.git/blob - server/increment.dc
e516c1486782dd17137990699571f53f34c8314b
[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 m.name, m.vendor 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(active), CHAR(publicflg), CHAR(hidden),
192                 CHAR(maillist), CHAR(grouplist), CHAR(gid)
193                 INTO :barg3, :barg4, :barg5, :barg6, :barg7, :barg8
194                 FROM list WHERE list_id = :id;
195         name = malloc(0);
196         id_to_name(id, "LIST", &name);
197         strcpy(barg0, name);
198         strcpy(barg1, argv[1]);
199         id = (int) argv[2];
200         if (!strcmp(barg1, "USER")) {
201             id_to_name(id, barg1, &name);
202         } else if (!strcmp(barg1, "LIST")) {
203             id_to_name(id, barg1, &name);
204         } else if (!strcmp(barg1, "STRING")) {
205             id_to_name(id, barg1, &name);
206         } else if (!strcmp(barg1, "KERBEROS")) {
207             id_to_name(id, "STRING", &name);
208         }
209         strcpy(barg2, name);
210         free(name);
211         beforec = 9;
212     } /* else
213       com_err(whoami, 0, "unknown table in incremental_before"); */
214 }
215
216
217 incremental_clear_before()
218 {
219     beforec = 0;
220 }
221
222 incremental_clear_after()
223 {
224     incremental_after("clear", 0);
225 }
226
227
228
229 incremental_after(table, qual, argv)
230 char *table;
231 EXEC SQL BEGIN DECLARE SECTION; 
232 char *qual;
233 EXEC SQL END DECLARE SECTION; 
234 char **argv;
235 {
236     char buffer[2048], *name;
237 EXEC SQL BEGIN DECLARE SECTION; 
238     int id, i;
239 EXEC SQL END DECLARE SECTION; 
240     struct iupdate *iu;
241     char **copy_argv();
242
243     if (!strcmp(table, "users")) {
244         EXEC SQL SELECT u.login, CHAR(u.uid), u.shell, u.last, u.first, 
245             u.middle, CHAR(u.status), u.clearid, u.type
246           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5,
247             :aarg6, :aarg7, :aarg8
248           FROM users u WHERE :qual;
249         afterc = 9;
250     } else if (!strcmp(table, "machine")) {
251         EXEC SQL SELECT m.name, m.vendor INTO :aarg0, :aarg1
252           FROM machine m WHERE :qual;
253         afterc = 2;
254     } else if (!strcmp(table, "cluster")) {
255         EXEC SQL SELECT c.name, c.description, c.location 
256           INTO :aarg0, :aarg1, :aarg2
257           FROM cluster c WHERE :qual;
258         afterc = 3;
259     } else if (!strcmp(table, "mcmap")) {
260         strcpy(aarg0, argv[0]);
261         strcpy(aarg1, argv[1]);
262         afterc = 2;
263     } else if (!strcmp(table, "svc")) {
264         strcpy(aarg0, argv[0]);
265         strcpy(aarg1, argv[1]);
266         strcpy(aarg2, argv[2]);
267         afterc = 3;
268     } else if (!strcmp(table, "filesys")) {
269         EXEC SQL SELECT CHAR(fs.label), fs.type, CHAR(fs.mach_id), fs.name, 
270             fs.mount, fs.access, fs.comments, CHAR(fs.owner), CHAR(fs.owners),
271             CHAR(fs.createflg), fs.lockertype
272           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
273               :aarg7, :aarg8, :aarg9, :aarg10
274           FROM filesys fs WHERE :qual;
275         name = malloc(0);
276         id = atoi(aarg2);
277         id_to_name(id, "MACHINE", &name);
278         strcpy(aarg2, name);
279         id = atoi(aarg7);
280         id_to_name(id, "USER", &name);
281         strcpy(aarg7, name);
282         id = atoi(aarg8);
283         id_to_name(id, "LIST", &name);
284         strcpy(aarg8, name);
285         free(name);
286         afterc = 11;
287     } else if (!strcmp(table, "quota")) {
288         strcpy(aarg0, "?");
289         strcpy(aarg1, argv[1]);
290         strcpy(aarg2, "?");
291         sprintf(buffer, "%s and fs.filsys_id = q.filsys_id and q.type = '%s'",
292                 qual, argv[1]);
293         qual = buffer;
294         EXEC SQL SELECT CHAR(q.quota), fs.name INTO :aarg3, :aarg4
295           FROM quota q, filesys fs WHERE :qual;
296         afterc = 5;
297     } else if (!strcmp(table, "list")) {
298         EXEC SQL SELECT l.name, CHAR(l.active), CHAR(l.publicflg), 
299             CHAR(l.hidden), CHAR(l.maillist), CHAR(l.grouplist), CHAR(l.gid), 
300             l.acl_type, CHAR(l.acl_id), l.description
301           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
302              :aarg7, :aarg8, :aarg9
303           FROM list l WHERE :qual;
304         afterc = 10;
305     } else if (!strcmp(table, "members")) {
306         id = (int) argv[0];
307         EXEC SQL SELECT CHAR(active), CHAR(publicflg), CHAR(hidden),
308                 CHAR(maillist), CHAR(grouplist), CHAR(gid)
309                 INTO :aarg3, :aarg4, :aarg5, :aarg6, :aarg7, :aarg8
310                 FROM list WHERE list_id = :id;
311         name = malloc(0);
312         id_to_name(id, "LIST", &name);
313         strcpy(aarg0, name);
314         strcpy(aarg1, argv[1]);
315         id = (int) argv[2];
316         if (!strcmp(aarg1, "USER")) {
317             id_to_name(id, aarg1, &name);
318         } else if (!strcmp(aarg1, "LIST")) {
319             id_to_name(id, aarg1, &name);
320         } else if (!strcmp(aarg1, "STRING")) {
321             id_to_name(id, aarg1, &name);
322         } else if (!strcmp(aarg1, "KERBEROS")) {
323             id_to_name(id, "STRING", &name);
324         }
325         strcpy(aarg2, name);
326         free(name);
327         afterc = 9;
328     } else if (!strcmp(table, "clear")) {
329         afterc = 0;
330         table = beforetable;
331     } /* else
332       com_err(whoami, 0, "unknown table in incremental_after"); */
333
334     iu = (struct iupdate *) malloc(sizeof(struct iupdate));
335     iu->table = strsave(table);
336     iu->beforec = beforec;
337     iu->before = copy_argv(before, beforec);
338     iu->afterc = afterc;
339     iu->after = copy_argv(after, afterc);
340     sq_save_data(incremental_sq, iu);
341
342 #ifdef DEBUG
343     sprintf(buffer, "INCREMENTAL(%s, [", table);
344     for (i = 0; i < beforec; i++) {
345         if (i == 0)
346           strcat(buffer, strtrim(before[0]));
347         else {
348             strcat(buffer, ", ");
349             strcat(buffer, strtrim(before[i]));
350         }
351     }
352     strcat(buffer, "], [");
353     for (i = 0; i < afterc; i++) {
354         if (i == 0)
355           strcat(buffer, strtrim(after[0]));
356         else {
357             strcat(buffer, ", ");
358             strcat(buffer, strtrim(after[i]));
359         }
360     }
361     strcat(buffer, "])");
362     com_err(whoami, 0, buffer);
363 #endif DEBUG
364 }
365
366
367 /* Called when the current transaction is committed to start any queued
368  * incremental updates.  This caches the update table the first time it
369  * is called.
370  */
371
372 struct inc_cache {
373     struct inc_cache *next;
374     char *table;
375     char *service;
376 };
377
378
379 incremental_update()
380 {
381     static int inited = 0;
382     static struct inc_cache *cache;
383     struct inc_cache *c;
384     EXEC SQL BEGIN DECLARE SECTION;
385     char tab[17], serv[17];
386     EXEC SQL END DECLARE SECTION;
387     struct iupdate *iu;
388
389     if (!inited) {
390         inited++;
391
392         EXEC SQL DECLARE inc CURSOR FOR SELECT tablename, service FROM incremental;
393         EXEC SQL OPEN inc;
394         while (1) {
395             EXEC SQL FETCH inc INTO :tab, :serv;
396             if (sqlca.sqlcode != 0) break;
397             c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
398             c->next = cache;
399             c->table = strsave(strtrim(tab));
400             c->service = strsave(strtrim(serv));
401             cache = c;
402         }
403         EXEC SQL CLOSE inc;
404         EXEC SQL COMMIT WORK;
405     }
406
407     while (sq_remove_data(incremental_sq, &iu)) {
408         for (c = cache; c; c = c->next) {
409             if (!strcmp(c->table, iu->table)) {
410                 iu->service = c->service;
411                 sq_save_data(incremental_exec, iu);
412             }
413         }
414     }
415     if (inc_running == 0)
416       next_incremental();
417 }
418
419
420 next_incremental()
421 {
422     struct iupdate *iu;
423     char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
424     int i;
425
426     if (incremental_exec == NULL)
427       incremental_init();
428
429     if (sq_empty(incremental_exec) ||
430         (inc_running && now - inc_started < INC_TIMEOUT))
431       return;
432
433     if (inc_running)
434       com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
435
436     sq_remove_data(incremental_exec, &iu);
437     argv[1] = iu->table;
438     sprintf(cbefore, "%d", iu->beforec);
439     argv[2] = cbefore;
440     sprintf(cafter, "%d", iu->afterc);
441     argv[3] = cafter;
442     for (i = 0; i < iu->beforec; i++)
443       argv[4 + i] = iu->before[i];
444     for (i = 0; i < iu->afterc; i++)
445       argv[4 + iu->beforec + i] = iu->after[i];
446
447     sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
448 #ifdef DEBUG
449     com_err(whoami, 0, "forking %s", prog);
450 #endif
451     argv[0] = prog;
452     argv[4 + iu->beforec + iu->afterc] = 0;
453     inc_pid = vfork();
454     switch (inc_pid) {
455     case 0:
456         execv(prog, argv);
457         _exit(1);
458     case -1:
459         com_err(whoami, 0, "Failed to start incremental update");
460         break;
461     default:
462         inc_running = 1;
463         inc_started = now;
464     }
465
466     free_argv(iu->before, iu->beforec);
467     free_argv(iu->after, iu->afterc);
468     free(iu->table);
469     free(iu);
470
471 }
472
473
474 /* Called when the current transaction is aborted to throw away any queued
475  * incremental updates
476  */
477
478 incremental_flush()
479 {
480     struct iupdate *iu;
481
482     while (sq_get_data(incremental_sq, &iu)) {
483         free_argv(iu->before, iu->beforec);
484         free_argv(iu->after, iu->afterc);
485         free(iu->table);
486         free(iu);
487     }
488     sq_destroy(incremental_sq);
489     incremental_sq = sq_create();
490 }
491
492
493 char **copy_argv(argv, argc)
494 char **argv;
495 int argc;
496 {
497     char **ret = (char **)malloc(sizeof(char *) * argc);
498     while (--argc >= 0)
499       ret[argc] = strsave(strtrim(argv[argc]));
500     return(ret);
501 }
502
503 free_argv(argv, argc)
504 char **argv;
505 int argc;
506 {
507     while (--argc >= 0)
508       free(argv[argc]);
509     free(argv);
510 }
This page took 0.37448 seconds and 3 git commands to generate.