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