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