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