]> andersk Git - moira.git/blob - server/increment.dc
increase Argv[] size to match QMAXARGS
[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.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 and q.type = '%s'",
290                 qual, argv[1]);
291         qual = buffer;
292         EXEC SQL SELECT CHAR(q.quota), fs.name INTO :aarg3, :aarg4
293           FROM quota q, filesys fs WHERE :qual;
294         afterc = 5;
295     } else if (!strcmp(table, "list")) {
296         EXEC SQL SELECT l.name, CHAR(l.active), CHAR(l.publicflg), 
297             CHAR(l.hidden), CHAR(l.maillist), CHAR(l.grouplist), CHAR(l.gid), 
298             l.acl_type, CHAR(l.acl_id), l.description
299           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
300              :aarg7, :aarg8, :aarg9
301           FROM list l WHERE :qual;
302         afterc = 10;
303     } else if (!strcmp(table, "members")) {
304         id = (int) argv[0];
305         EXEC SQL SELECT CHAR(grouplist) INTO :aarg3 FROM list
306           WHERE list_id = :id;
307         name = malloc(0);
308         id_to_name(id, "LIST", &name);
309         strcpy(aarg0, name);
310         strcpy(aarg1, argv[1]);
311         id = (int) argv[2];
312         if (!strcmp(aarg1, "USER")) {
313             id_to_name(id, aarg1, &name);
314         } else if (!strcmp(aarg1, "LIST")) {
315             id_to_name(id, aarg1, &name);
316         } else if (!strcmp(aarg1, "STRING")) {
317             id_to_name(id, aarg1, &name);
318         } else if (!strcmp(aarg1, "KERBEROS")) {
319             id_to_name(id, "STRING", &name);
320         }
321         strcpy(aarg2, name);
322         free(name);
323         afterc = 4;
324     } else if (!strcmp(table, "clear")) {
325         afterc = 0;
326         table = beforetable;
327     } /* else
328       com_err(whoami, 0, "unknown table in incremental_after"); */
329
330     iu = (struct iupdate *) malloc(sizeof(struct iupdate));
331     iu->table = strsave(table);
332     iu->beforec = beforec;
333     iu->before = copy_argv(before, beforec);
334     iu->afterc = afterc;
335     iu->after = copy_argv(after, afterc);
336     sq_save_data(incremental_sq, iu);
337
338 #ifdef DEBUG
339     sprintf(buffer, "INCREMENTAL(%s, [", table);
340     for (i = 0; i < beforec; i++) {
341         if (i == 0)
342           strcat(buffer, strtrim(before[0]));
343         else {
344             strcat(buffer, ", ");
345             strcat(buffer, strtrim(before[i]));
346         }
347     }
348     strcat(buffer, "], [");
349     for (i = 0; i < afterc; i++) {
350         if (i == 0)
351           strcat(buffer, strtrim(after[0]));
352         else {
353             strcat(buffer, ", ");
354             strcat(buffer, strtrim(after[i]));
355         }
356     }
357     strcat(buffer, "])");
358     com_err(whoami, 0, buffer);
359 #endif DEBUG
360 }
361
362
363 /* Called when the current transaction is committed to start any queued
364  * incremental updates.  This caches the update table the first time it
365  * is called.
366  */
367
368 struct inc_cache {
369     struct inc_cache *next;
370     char *table;
371     char *service;
372 };
373
374
375 incremental_update()
376 {
377     static int inited = 0;
378     static struct inc_cache *cache;
379     struct inc_cache *c;
380     EXEC SQL BEGIN DECLARE SECTION;
381     char tab[17], serv[17];
382     EXEC SQL END DECLARE SECTION;
383     struct iupdate *iu;
384
385     if (!inited) {
386         inited++;
387
388         EXEC SQL DECLARE inc CURSOR FOR SELECT tablename, service FROM incremental;
389         EXEC SQL OPEN inc;
390         while (1) {
391             EXEC SQL FETCH inc INTO :tab, :serv;
392             if (sqlca.sqlcode != 0) break;
393             c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
394             c->next = cache;
395             c->table = strsave(strtrim(tab));
396             c->service = strsave(strtrim(serv));
397             cache = c;
398         }
399         EXEC SQL CLOSE inc;
400         EXEC SQL COMMIT WORK;
401     }
402
403     while (sq_remove_data(incremental_sq, &iu)) {
404         for (c = cache; c; c = c->next) {
405             if (!strcmp(c->table, iu->table)) {
406                 iu->service = c->service;
407                 sq_save_data(incremental_exec, iu);
408             }
409         }
410     }
411     if (inc_running == 0)
412       next_incremental();
413 }
414
415
416 next_incremental()
417 {
418     struct iupdate *iu;
419     char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
420     int i;
421
422     if (incremental_exec == NULL)
423       incremental_init();
424
425     if (sq_empty(incremental_exec) ||
426         (inc_running && now - inc_started < INC_TIMEOUT))
427       return;
428
429     if (inc_running)
430       com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
431
432     sq_remove_data(incremental_exec, &iu);
433     argv[1] = iu->table;
434     sprintf(cbefore, "%d", iu->beforec);
435     argv[2] = cbefore;
436     sprintf(cafter, "%d", iu->afterc);
437     argv[3] = cafter;
438     for (i = 0; i < iu->beforec; i++)
439       argv[4 + i] = iu->before[i];
440     for (i = 0; i < iu->afterc; i++)
441       argv[4 + iu->beforec + i] = iu->after[i];
442
443     sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
444 #ifdef DEBUG
445     com_err(whoami, 0, "forking %s", prog);
446 #endif
447     argv[0] = prog;
448     argv[4 + iu->beforec + iu->afterc] = 0;
449     inc_pid = vfork();
450     switch (inc_pid) {
451     case 0:
452         execv(prog, argv);
453         _exit(1);
454     case -1:
455         com_err(whoami, 0, "Failed to start incremental update");
456         break;
457     default:
458         inc_running = 1;
459         inc_started = now;
460     }
461
462     free_argv(iu->before, iu->beforec);
463     free_argv(iu->after, iu->afterc);
464     free(iu->table);
465     free(iu);
466
467 }
468
469
470 /* Called when the current transaction is aborted to throw away any queued
471  * incremental updates
472  */
473
474 incremental_flush()
475 {
476     struct iupdate *iu;
477
478     while (sq_get_data(incremental_sq, &iu)) {
479         free_argv(iu->before, iu->beforec);
480         free_argv(iu->after, iu->afterc);
481         free(iu->table);
482         free(iu);
483     }
484     sq_destroy(incremental_sq);
485     incremental_sq = sq_create();
486 }
487
488
489 char **copy_argv(argv, argc)
490 char **argv;
491 int argc;
492 {
493     char **ret = (char **)malloc(sizeof(char *) * argc);
494     while (--argc >= 0)
495       ret[argc] = strsave(strtrim(argv[argc]));
496     return(ret);
497 }
498
499 free_argv(argv, argc)
500 char **argv;
501 int argc;
502 {
503     while (--argc >= 0)
504       free(argv[argc]);
505     free(argv);
506 }
This page took 0.091836 seconds and 5 git commands to generate.