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