]> andersk Git - moira.git/blob - server/increment.dc
Many bug-fixes. At least 30 queries work (but still no wildcards.)
[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 <mit-copyright.h>
17 #include <moira.h>
18 #include "query.h"
19 #include "mr_server.h"
20 EXEC SQL INCLUDE sqlca;
21
22 extern char *whoami;
23 char *malloc();
24
25 int inc_pid = 0;
26 int inc_running = 0;
27 time_t inc_started;
28
29 #define MAXARGC 15
30
31 EXEC SQL WHENEVER SQLERROR CALL ingerr;
32
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;
41 static int beforec;
42 static char *beforetable;
43
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;
52 static int afterc;
53
54 /* structures to save entire sets of incremental changes */
55 struct save_queue *incremental_sq = NULL;
56 struct save_queue *incremental_exec = NULL;
57 struct iupdate {
58     char *table;
59     int beforec;
60     char **before;
61     int afterc;
62     char **after;
63     char *service;
64 };
65
66
67 incremental_init()
68 {
69     int i;
70
71     for (i = 0; i < MAXARGC; i++) {
72         before[i] = &beforeb[i][0];
73         after[i] = &afterb[i][0];
74     }
75     barg0 = before[0];
76     barg1 = before[1];
77     barg2 = before[2];
78     barg3 = before[3];
79     barg4 = before[4];
80     barg5 = before[5];
81     barg6 = before[6];
82     barg7 = before[7];
83     barg8 = before[8];
84     barg9 = before[9];
85     barg10 = before[10];
86     barg11 = before[11];
87     barg12 = before[12];
88     barg13 = before[13];
89     barg14 = before[14];
90     aarg0 = after[0];
91     aarg1 = after[1];
92     aarg2 = after[2];
93     aarg3 = after[3];
94     aarg4 = after[4];
95     aarg5 = after[5];
96     aarg6 = after[6];
97     aarg7 = after[7];
98     aarg8 = after[8];
99     aarg9 = after[9];
100     aarg10 = after[10];
101     aarg11 = after[11];
102     aarg12 = after[12];
103     aarg13 = after[13];
104     aarg14 = after[14];
105     if (incremental_sq == NULL)
106       incremental_sq = sq_create();
107     if (incremental_exec == NULL)
108       incremental_exec = sq_create();
109 }
110
111
112 incremental_before(table, qual, argv)
113 char *table;
114 EXEC SQL BEGIN DECLARE SECTION; 
115 char *qual;
116 EXEC SQL END DECLARE SECTION; 
117 char **argv;
118 {
119     EXEC SQL BEGIN DECLARE SECTION;
120     int id;
121     EXEC SQL END DECLARE SECTION;
122
123     char buffer[512], *name;
124
125     beforetable = table;
126
127     if (!strcmp(table, "users")) {
128 /*
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)
133  *              where qual
134  */
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,
138             :barg7, :barg8
139           FROM users WHERE :qual;
140         beforec = 9;
141     } else if (!strcmp(table, "machine")) {
142 /*
143  *      retrieve (barg0 = m.name, barg1 = m.type) where qual
144  */
145         EXEC SQL SELECT name, type INTO :barg0, :barg1 FROM machine
146           WHERE :qual;
147         beforec = 2;
148     } else if (!strcmp(table, "cluster")) {
149 /*
150  *      retrieve (barg0 = c.name, barg1 = c.desc, barg2 = c.location)
151  *              where qual
152  */
153         EXEC SQL SELECT name, description, location 
154           INTO :barg0, :barg1, :barg2
155           FROM cluster WHERE :qual;
156         beforec = 3;
157     } else if (!strcmp(table, "mcmap")) {
158         strcpy(barg0, argv[0]);
159         strcpy(barg1, argv[1]);
160         beforec = 2;
161     } else if (!strcmp(table, "svc")) {
162         strcpy(barg0, argv[0]);
163         strcpy(barg1, argv[1]);
164         strcpy(barg2, argv[2]);
165         beforec = 3;
166     } else if (!strcmp(table, "filesys")) {
167 /*
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)
174  *        where qual
175  */
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;
181         beforec = 11;
182     } else if (!strcmp(table, "quota")) {
183         strcpy(barg0, "?");
184         strcpy(barg1, argv[1]);
185         strcpy(barg2, "?");
186         sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
187         qual = buffer;
188 /*
189  *      range of q is quota
190  *      retrieve (barg3 = text(q.quota), barg4 = filesys.name) where qual
191  */
192         EXEC SQL SELECT q.quota, fs.name INTO :barg3, :barg4
193           FROM quota q, filesys fs WHERE :qual;
194         beforec = 5;
195     } else if (!strcmp(table, "list")) {
196 /*
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)
202  *        where qual
203  */
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;
210         beforec = 10;
211     } else if (!strcmp(table, "members")) {
212         id = (int) argv[0];
213 /*
214  *      retrieve (barg3 = text(list.group)) where list.list_id = id
215  */
216         EXEC SQL SELECT CHAR(grouplist) INTO :barg3 FROM list 
217           WHERE list_id = :id;
218         name = malloc(0);
219         id_to_name(id, "LIST", &name);
220         strcpy(barg0, name);
221         strcpy(barg1, argv[1]);
222         id = (int) argv[2];
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);
231         }
232         strcpy(barg2, name);
233         free(name);
234         beforec = 4;
235     } /* else
236       com_err(whoami, 0, "unknown table in incremental_before"); */
237 }
238
239
240 incremental_clear_before()
241 {
242     beforec = 0;
243 }
244
245 incremental_clear_after()
246 {
247     incremental_after("clear", 0);
248 }
249
250
251
252 incremental_after(table, qual, argv)
253 char *table;
254 EXEC SQL BEGIN DECLARE SECTION; 
255 char *qual;
256 EXEC SQL END DECLARE SECTION; 
257 char **argv;
258 {
259     char buffer[2048], *name;
260 EXEC SQL BEGIN DECLARE SECTION; 
261     int id, i;
262 EXEC SQL END DECLARE SECTION; 
263     struct iupdate *iu;
264     char **copy_argv();
265
266     if (!strcmp(table, "users")) {
267 /*
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)
272  *              where qual
273  */
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;
279         afterc = 9;
280     } else if (!strcmp(table, "machine")) {
281 /*
282  *      retrieve (aarg0 = m.name, aarg1 = m.type) where qual
283  */
284         EXEC SQL SELECT name, type INTO :aarg0, :aarg1
285           FROM machine WHERE :qual;
286         afterc = 2;
287     } else if (!strcmp(table, "cluster")) {
288 /*
289  *      retrieve (aarg0 = c.name, aarg1 = c.desc, aarg2 = c.location)
290  *              where qual
291  */
292         EXEC SQL SELECT name, description, location 
293           INTO :aarg0, :aarg1, :aarg2
294           FROM cluster WHERE :qual;
295         afterc = 3;
296     } else if (!strcmp(table, "mcmap")) {
297         strcpy(aarg0, argv[0]);
298         strcpy(aarg1, argv[1]);
299         afterc = 2;
300     } else if (!strcmp(table, "svc")) {
301         strcpy(aarg0, argv[0]);
302         strcpy(aarg1, argv[1]);
303         strcpy(aarg2, argv[2]);
304         afterc = 3;
305     } else if (!strcmp(table, "filesys")) {
306 /*
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)
314  *        where qual
315  */
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;
321         afterc = 11;
322     } else if (!strcmp(table, "quota")) {
323         strcpy(aarg0, "?");
324         strcpy(aarg1, argv[1]);
325         strcpy(aarg2, "?");
326         sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
327         qual = buffer;
328 /*
329  *      range of q is quota
330  *      retrieve (aarg3 = text(q.quota), aarg4 = filesys.name) where qual
331  */
332         EXEC SQL SELECT CHAR(q.quota), fs.name INTO :aarg3, :aarg4
333           FROM quota q, filesys fs WHERE :qual;
334         afterc = 5;
335     } else if (!strcmp(table, "list")) {
336 /*
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)
342  *        where qual
343  */
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;
350         afterc = 10;
351     } else if (!strcmp(table, "members")) {
352         id = (int) argv[0];
353 /*
354  *      retrieve (aarg3 = text(list.group)) where list.list_id = id
355  */
356         EXEC SQL SELECT CHAR(grouplist) INTO :aarg3 FROM list
357           WHERE list_id = :id;
358         name = malloc(0);
359         id_to_name(id, "LIST", &name);
360         strcpy(aarg0, name);
361         strcpy(aarg1, argv[1]);
362         id = (int) argv[2];
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);
371         }
372         strcpy(aarg2, name);
373         free(name);
374         afterc = 4;
375     } else if (!strcmp(table, "clear")) {
376         afterc = 0;
377         table = beforetable;
378     } /* else
379       com_err(whoami, 0, "unknown table in incremental_after"); */
380
381     iu = (struct iupdate *) malloc(sizeof(struct iupdate));
382     iu->table = strsave(table);
383     iu->beforec = beforec;
384     iu->before = copy_argv(before, beforec);
385     iu->afterc = afterc;
386     iu->after = copy_argv(after, afterc);
387     sq_save_data(incremental_sq, iu);
388
389 #ifdef DEBUG
390     sprintf(buffer, "INCREMENTAL(%s, [", table);
391     for (i = 0; i < beforec; i++) {
392         if (i == 0)
393           strcat(buffer, strtrim(before[0]));
394         else {
395             strcat(buffer, ", ");
396             strcat(buffer, strtrim(before[i]));
397         }
398     }
399     strcat(buffer, "], [");
400     for (i = 0; i < afterc; i++) {
401         if (i == 0)
402           strcat(buffer, strtrim(after[0]));
403         else {
404             strcat(buffer, ", ");
405             strcat(buffer, strtrim(after[i]));
406         }
407     }
408     strcat(buffer, "])");
409     com_err(whoami, 0, buffer);
410 #endif DEBUG
411 }
412
413
414 /* Called when the current transaction is committed to start any queued
415  * incremental updates.  This caches the update table the first time it
416  * is called.
417  */
418
419 struct inc_cache {
420     struct inc_cache *next;
421     char *table;
422     char *service;
423 };
424
425
426 incremental_update()
427 {
428     static int inited = 0;
429     static struct inc_cache *cache;
430     struct inc_cache *c;
431     EXEC SQL BEGIN DECLARE SECTION;
432     char tab[17], serv[17];
433     EXEC SQL END DECLARE SECTION;
434     struct iupdate *iu;
435
436     if (!inited) {
437         inited++;
438
439         EXEC SQL DECLARE inc CURSOR FOR SELECT tablename, service FROM incremental;
440         EXEC SQL OPEN inc;
441         while (1) {
442             EXEC SQL FETCH inc INTO :tab, :serv;
443             if (sqlca.sqlcode != 0) break;
444             c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
445             c->next = cache;
446             c->table = strsave(strtrim(tab));
447             c->service = strsave(strtrim(serv));
448             cache = c;
449         }
450         EXEC SQL CLOSE inc;
451     }
452
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);
458             }
459         }
460     }
461     if (inc_running == 0)
462       next_incremental();
463 }
464
465
466 next_incremental()
467 {
468     struct iupdate *iu;
469     char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
470     int i;
471
472     if (incremental_exec == NULL)
473       incremental_init();
474
475     if (sq_empty(incremental_exec) ||
476         (inc_running && now - inc_started < INC_TIMEOUT))
477       return;
478
479     if (inc_running)
480       com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
481
482     sq_remove_data(incremental_exec, &iu);
483     argv[1] = iu->table;
484     sprintf(cbefore, "%d", iu->beforec);
485     argv[2] = cbefore;
486     sprintf(cafter, "%d", iu->afterc);
487     argv[3] = cafter;
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];
492
493     sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
494 #ifdef DEBUG
495     com_err(whoami, 0, "forking %s", prog);
496 #endif
497     argv[0] = prog;
498     argv[4 + iu->beforec + iu->afterc] = 0;
499     inc_pid = vfork();
500     switch (inc_pid) {
501     case 0:
502         execv(prog, argv);
503         exit(1);
504     case -1:
505         com_err(whoami, 0, "Failed to start incremental update");
506         break;
507     default:
508         inc_running = 1;
509         inc_started = now;
510     }
511
512     free_argv(iu->before, iu->beforec);
513     free_argv(iu->after, iu->afterc);
514     free(iu->table);
515     free(iu);
516
517 }
518
519
520 /* Called when the current transaction is aborted to throw away any queued
521  * incremental updates
522  */
523
524 incremental_flush()
525 {
526     struct iupdate *iu;
527
528     while (sq_get_data(incremental_sq, &iu)) {
529         free_argv(iu->before, iu->beforec);
530         free_argv(iu->after, iu->afterc);
531         free(iu->table);
532         free(iu);
533     }
534     sq_destroy(incremental_sq);
535     incremental_sq = sq_create();
536 }
537
538
539 char **copy_argv(argv, argc)
540 char **argv;
541 int argc;
542 {
543     char **ret = (char **)malloc(sizeof(char *) * argc);
544     while (--argc >= 0)
545       ret[argc] = strsave(strtrim(argv[argc]));
546     return(ret);
547 }
548
549 free_argv(argv, argc)
550 char **argv;
551 int argc;
552 {
553     while (--argc >= 0)
554       free(argv[argc]);
555     free(argv);
556 }
This page took 0.087376 seconds and 5 git commands to generate.