]> andersk Git - moira.git/blob - server/increment.dc
cdf816e2f0507181909b6ef3f26e0737912f7909
[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 /* structures to save before args */
32 static char beforeb[MAXARGC][ARGLEN];
33 static char *before[MAXARGC];
34 EXEC SQL BEGIN DECLARE SECTION;
35 char *barg0, *barg1, *barg2, *barg3, *barg4;
36 char *barg5, *barg6, *barg7, *barg8, *barg9;
37 char *barg10, *barg11, *barg12, *barg13, *barg14;
38 EXEC SQL END DECLARE SECTION;
39 static int beforec;
40 static char *beforetable;
41
42 /* structures to save after args */
43 static char afterb[MAXARGC][ARGLEN];
44 static char *after[MAXARGC];
45 EXEC SQL BEGIN DECLARE SECTION;
46 char *aarg0, *aarg1, *aarg2, *aarg3, *aarg4;
47 char *aarg5, *aarg6, *aarg7, *aarg8, *aarg9;
48 char *aarg10, *aarg11, *aarg12, *aarg13, *aarg14;
49 EXEC SQL END DECLARE SECTION;
50 static int afterc;
51
52 /* structures to save entire sets of incremental changes */
53 struct save_queue *incremental_sq = NULL;
54 struct save_queue *incremental_exec = NULL;
55 struct iupdate {
56     char *table;
57     int beforec;
58     char **before;
59     int afterc;
60     char **after;
61     char *service;
62 };
63
64
65 incremental_init()
66 {
67     int i;
68
69     for (i = 0; i < MAXARGC; i++) {
70         before[i] = &beforeb[i][0];
71         after[i] = &afterb[i][0];
72     }
73     barg0 = before[0];
74     barg1 = before[1];
75     barg2 = before[2];
76     barg3 = before[3];
77     barg4 = before[4];
78     barg5 = before[5];
79     barg6 = before[6];
80     barg7 = before[7];
81     barg8 = before[8];
82     barg9 = before[9];
83     barg10 = before[10];
84     barg11 = before[11];
85     barg12 = before[12];
86     barg13 = before[13];
87     barg14 = before[14];
88     aarg0 = after[0];
89     aarg1 = after[1];
90     aarg2 = after[2];
91     aarg3 = after[3];
92     aarg4 = after[4];
93     aarg5 = after[5];
94     aarg6 = after[6];
95     aarg7 = after[7];
96     aarg8 = after[8];
97     aarg9 = after[9];
98     aarg10 = after[10];
99     aarg11 = after[11];
100     aarg12 = after[12];
101     aarg13 = after[13];
102     aarg14 = after[14];
103     if (incremental_sq == NULL)
104       incremental_sq = sq_create();
105     if (incremental_exec == NULL)
106       incremental_exec = sq_create();
107 }
108
109
110 incremental_before(table, qual, argv)
111 char *table;
112 char *qual;
113 char **argv;
114 {
115     EXEC SQL BEGIN DECLARE SECTION;
116     int id;
117     EXEC SQL END DECLARE SECTION;
118
119     char buffer[512], *name;
120
121     beforetable = table;
122
123     if (!strcmp(table, "users")) {
124 /*
125  *      retrieve (barg0 = u.login, barg1 = text(u.uid),
126  *                barg2 = u.shell, barg3 = u.last, barg4 = u.first,
127  *                barg5 = u.middle, barg6 = text(u.status),
128  *                barg7 = u.mit_id, barg8 = u.mit_year)
129  *              where qual
130  */
131         EXEC SQL SELECT login, uid, shell, last, first, middle, 
132             text(u.status), mit_id, mit_year
133           INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
134             :barg7, :barg8
135           FROM users WHERE :qual;
136         beforec = 9;
137     } else if (!strcmp(table, "machine")) {
138 /*
139  *      retrieve (barg0 = m.name, barg1 = m.type) where qual
140  */
141         EXEC SQL SELECT name, type INTO :barg0, :barg1 FROM machine
142           WHERE :qual;
143         beforec = 2;
144     } else if (!strcmp(table, "cluster")) {
145 /*
146  *      retrieve (barg0 = c.name, barg1 = c.desc, barg2 = c.location)
147  *              where qual
148  */
149         EXEC SQL SELECT name, desc, location INTO :barg0, :barg1, :barg2
150           FROM cluster WHERE :qual;
151         beforec = 3;
152     } else if (!strcmp(table, "mcmap")) {
153         strcpy(barg0, argv[0]);
154         strcpy(barg1, argv[1]);
155         beforec = 2;
156     } else if (!strcmp(table, "svc")) {
157         strcpy(barg0, argv[0]);
158         strcpy(barg1, argv[1]);
159         strcpy(barg2, argv[2]);
160         beforec = 3;
161     } else if (!strcmp(table, "filesys")) {
162 /*
163  *      range of fs is filesys
164  *      retrieve (barg0 = fs.label, barg1 = fs.type, barg2 = text(fs.mach_id),
165  *                barg3 = fs.name, barg4 = fs.mount, barg5 = fs.access,
166  *                barg6 = fs.comments, barg7 = text(fs.owner),
167  *                barg8 = text(fs.owners), barg9 = text(fs.createflg),
168  *                barg10 = fs.lockertype)
169  *        where qual
170  */
171         EXEC SQL SELECT label, type, text(mach_id), name, mount, access,
172             comments, text(owner), text(owners), text(createflag), lockertype
173           INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6, 
174             :barg7, :barg8, :barg9, :barg10
175           FROM filesys WHERE :qual;
176         beforec = 11;
177     } else if (!strcmp(table, "quota")) {
178         strcpy(barg0, "?");
179         strcpy(barg1, argv[1]);
180         strcpy(barg2, "?");
181         sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
182         qual = buffer;
183 /*
184  *      range of q is quota
185  *      retrieve (barg3 = text(q.quota), barg4 = filesys.name) where qual
186  */
187         EXEC SQL SELECT q.quota, fs.name INTO :barg3, :barg4
188           FROM quota q, filesys fs WHERE :qual;
189         beforec = 5;
190     } else if (!strcmp(table, "list")) {
191 /*
192  *      retrieve (barg0 = l.name, barg1 = text(l.active),
193  *                barg2 = text(l.public), barg3 = text(l.hidden),
194  *                barg4 = text(l.maillist), barg5 = text(l.group),
195  *                barg6 = text(l.gid), barg7 = l.acl_type,
196  *                barg8 = text(l.acl_id), barg9 = l.desc)
197  *        where qual
198  */
199         EXEC SQL SELECT name, text(active), text(public), text(hidden),
200             text(maillist), text(grouplist), text(gid), acl_type,
201             text(acl_id), desc
202           INTO :barg0, :barg1, :barg2, :barg3, :barg4, :barg5, :barg6,
203              :barg7, :barg8, :barg9
204           FROM list WHERE :qual;
205         beforec = 10;
206     } else if (!strcmp(table, "members")) {
207         id = (int) argv[0];
208 /*
209  *      retrieve (barg3 = text(list.group)) where list.list_id = id
210  */
211         EXEC SQL SELECT text(list.group) INTO :barg3 FROM list 
212           WHERE list_id = :id;
213         name = malloc(0);
214         id_to_name(id, "LIST", &name);
215         strcpy(barg0, name);
216         strcpy(barg1, argv[1]);
217         id = (int) argv[2];
218         if (!strcmp(barg1, "USER")) {
219             id_to_name(id, barg1, &name);
220         } else if (!strcmp(barg1, "LIST")) {
221             id_to_name(id, barg1, &name);
222         } else if (!strcmp(barg1, "STRING")) {
223             id_to_name(id, barg1, &name);
224         } else if (!strcmp(barg1, "KERBEROS")) {
225             id_to_name(id, "STRING", &name);
226         }
227         strcpy(barg2, name);
228         free(name);
229         beforec = 4;
230     } /* else
231       com_err(whoami, 0, "unknown table in incremental_before"); */
232 }
233
234
235 incremental_clear_before()
236 {
237     beforec = 0;
238 }
239
240 incremental_clear_after()
241 {
242     incremental_after("clear", 0);
243 }
244
245
246
247 incremental_after(table, qual, argv)
248 EXEC SQL BEGIN DECLARE SECTION; 
249 char *table;
250 char *qual;
251 EXEC SQL END DECLARE SECTION; 
252 char **argv;
253 {
254     char buffer[2048], *name;
255 EXEC SQL BEGIN DECLARE SECTION; 
256     int id, i;
257 EXEC SQL END DECLARE SECTION; 
258     struct iupdate *iu;
259     char **copy_argv();
260
261     if (!strcmp(table, "users")) {
262 /*
263  *      retrieve (aarg0 = u.login, aarg1 = text(u.uid),
264  *                aarg2 = u.shell, aarg3 = u.last, aarg4 = u.first,
265  *                aarg5 = u.middle, aarg6 = text(u.status),
266  *                aarg7 = u.mit_id, aarg8 = u.mit_year)
267  *              where qual
268  */
269         EXEC SQL SELECT login, uid, shell, last, first, middle,
270             text(status), mit_id, mit_year
271           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5,
272             :aarg6, :aarg7, :aarg8
273           FROM users WHERE :qual;
274         afterc = 9;
275     } else if (!strcmp(table, "machine")) {
276 /*
277  *      retrieve (aarg0 = m.name, aarg1 = m.type) where qual
278  */
279         EXEC SQL SELECT name, type INTO :aarg0, :aarg1
280           FROM machine WHERE :qual;
281         afterc = 2;
282     } else if (!strcmp(table, "cluster")) {
283 /*
284  *      retrieve (aarg0 = c.name, aarg1 = c.desc, aarg2 = c.location)
285  *              where qual
286  */
287         EXEC SQL SELECT name, desc, location INTO :aarg0, :aarg1, :aarg2
288           FROM cluster WHERE :qual;
289         afterc = 3;
290     } else if (!strcmp(table, "mcmap")) {
291         strcpy(aarg0, argv[0]);
292         strcpy(aarg1, argv[1]);
293         afterc = 2;
294     } else if (!strcmp(table, "svc")) {
295         strcpy(aarg0, argv[0]);
296         strcpy(aarg1, argv[1]);
297         strcpy(aarg2, argv[2]);
298         afterc = 3;
299     } else if (!strcmp(table, "filesys")) {
300 /*
301  *      range of fs is filesys
302  *      retrieve (aarg0 = fs.label, aarg1 = fs.type,
303  *                aarg2 = text(fs.mach_id),
304  *                aarg3 = fs.name, aarg4 = fs.mount, aarg5 = fs.access,
305  *                aarg6 = fs.comments, aarg7 = text(fs.owner),
306  *                aarg8 = text(fs.owners), aarg9 = text(fs.createflg),
307  *                aarg10 = fs.lockertype)
308  *        where qual
309  */
310         EXEC SQL SELECT label, type, text(mach_id), name, mount, access,
311             comments, text(owner), text(owners), text(createflag), lockertype
312           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
313               :aarg7, :aarg8, :aarg9, :aarg10
314           FROM filesys fs WHERE :qual;
315         afterc = 11;
316     } else if (!strcmp(table, "quota")) {
317         strcpy(aarg0, "?");
318         strcpy(aarg1, argv[1]);
319         strcpy(aarg2, "?");
320         sprintf(buffer, "%s and fs.filsys_id = q.filsys_id", qual);
321         qual = buffer;
322 /*
323  *      range of q is quota
324  *      retrieve (aarg3 = text(q.quota), aarg4 = filesys.name) where qual
325  */
326         EXEC SQL SELECT text(q.quota), fs.name INTO :aarg3, :aarg4
327           FROM quota q, filesys fs WHERE :qual;
328         afterc = 5;
329     } else if (!strcmp(table, "list")) {
330 /*
331  *      retrieve (aarg0 = l.name, aarg1 = text(l.active),
332  *                aarg2 = text(l.public), aarg3 = text(l.hidden),
333  *                aarg4 = text(l.maillist), aarg5 = text(l.group),
334  *                aarg6 = text(l.gid), aarg7 = l.acl_type,
335  *                aarg8 = text(l.acl_id), aarg9 = l.desc)
336  *        where qual
337  */
338         EXEC SQL SELECT name, text(active), text(public), text(hidden),
339             text(maillist), text(grouplist), text(gid), acl_type,
340             text(acl_id), desc
341           INTO :aarg0, :aarg1, :aarg2, :aarg3, :aarg4, :aarg5, :aarg6,
342              :aarg7, :aarg8, :aarg9
343           FROM list WHERE :qual;
344         afterc = 10;
345     } else if (!strcmp(table, "members")) {
346         id = (int) argv[0];
347 /*
348  *      retrieve (aarg3 = text(list.group)) where list.list_id = id
349  */
350         EXEC SQL SELECT text(list.group) INTO :aarg3 FROM list
351           WHERE list_id = :id;
352         name = malloc(0);
353         id_to_name(id, "LIST", &name);
354         strcpy(aarg0, name);
355         strcpy(aarg1, argv[1]);
356         id = (int) argv[2];
357         if (!strcmp(aarg1, "USER")) {
358             id_to_name(id, aarg1, &name);
359         } else if (!strcmp(aarg1, "LIST")) {
360             id_to_name(id, aarg1, &name);
361         } else if (!strcmp(aarg1, "STRING")) {
362             id_to_name(id, aarg1, &name);
363         } else if (!strcmp(aarg1, "KERBEROS")) {
364             id_to_name(id, "STRING", &name);
365         }
366         strcpy(aarg2, name);
367         free(name);
368         afterc = 4;
369     } else if (!strcmp(table, "clear")) {
370         afterc = 0;
371         table = beforetable;
372     } /* else
373       com_err(whoami, 0, "unknown table in incremental_after"); */
374
375     iu = (struct iupdate *) malloc(sizeof(struct iupdate));
376     iu->table = strsave(table);
377     iu->beforec = beforec;
378     iu->before = copy_argv(before, beforec);
379     iu->afterc = afterc;
380     iu->after = copy_argv(after, afterc);
381     sq_save_data(incremental_sq, iu);
382
383 #ifdef DEBUG
384     sprintf(buffer, "INCREMENTAL(%s, [", table);
385     for (i = 0; i < beforec; i++) {
386         if (i == 0)
387           strcat(buffer, strtrim(before[0]));
388         else {
389             strcat(buffer, ", ");
390             strcat(buffer, strtrim(before[i]));
391         }
392     }
393     strcat(buffer, "], [");
394     for (i = 0; i < afterc; i++) {
395         if (i == 0)
396           strcat(buffer, strtrim(after[0]));
397         else {
398             strcat(buffer, ", ");
399             strcat(buffer, strtrim(after[i]));
400         }
401     }
402     strcat(buffer, "])");
403     com_err(whoami, 0, buffer);
404 #endif DEBUG
405 }
406
407
408 /* Called when the current transaction is committed to start any queued
409  * incremental updates.  This caches the update table the first time it
410  * is called.
411  */
412
413 struct inc_cache {
414     struct inc_cache *next;
415     char *table;
416     char *service;
417 };
418
419
420 incremental_update()
421 {
422     static int inited = 0;
423     static struct inc_cache *cache;
424     struct inc_cache *c;
425     EXEC SQL BEGIN DECLARE SECTION;
426     char tab[17], serv[17];
427     EXEC SQL END DECLARE SECTION;
428     struct iupdate *iu;
429
430     if (!inited) {
431         inited++;
432
433         EXEC SQL DECLARE inc CURSOR FOR SELECT table, service FROM incremental;
434         EXEC SQL OPEN inc;
435         while (1) {
436             EXEC SQL FETCH inc INTO :tab, :serv;
437             if (sqlca.sqlcode != 0) break;
438             c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
439             c->next = cache;
440             c->table = strsave(strtrim(tab));
441             c->service = strsave(strtrim(serv));
442             cache = c;
443         }
444         EXEC SQL CLOSE inc;
445     }
446
447     while (sq_remove_data(incremental_sq, &iu)) {
448         for (c = cache; c; c = c->next) {
449             if (!strcmp(c->table, iu->table)) {
450                 iu->service = c->service;
451                 sq_save_data(incremental_exec, iu);
452             }
453         }
454     }
455     if (inc_running == 0)
456       next_incremental();
457 }
458
459
460 next_incremental()
461 {
462     struct iupdate *iu;
463     char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
464     int i;
465
466     if (incremental_exec == NULL)
467       incremental_init();
468
469     if (sq_empty(incremental_exec) ||
470         (inc_running && now - inc_started < INC_TIMEOUT))
471       return;
472
473     if (inc_running)
474       com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
475
476     sq_remove_data(incremental_exec, &iu);
477     argv[1] = iu->table;
478     sprintf(cbefore, "%d", iu->beforec);
479     argv[2] = cbefore;
480     sprintf(cafter, "%d", iu->afterc);
481     argv[3] = cafter;
482     for (i = 0; i < iu->beforec; i++)
483       argv[4 + i] = iu->before[i];
484     for (i = 0; i < iu->afterc; i++)
485       argv[4 + iu->beforec + i] = iu->after[i];
486
487     sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
488 #ifdef DEBUG
489     com_err(whoami, 0, "forking %s", prog);
490 #endif
491     argv[0] = prog;
492     argv[4 + iu->beforec + iu->afterc] = 0;
493     inc_pid = vfork();
494     switch (inc_pid) {
495     case 0:
496         execv(prog, argv);
497         exit(1);
498     case -1:
499         com_err(whoami, 0, "Failed to start incremental update");
500         break;
501     default:
502         inc_running = 1;
503         inc_started = now;
504     }
505
506     free_argv(iu->before, iu->beforec);
507     free_argv(iu->after, iu->afterc);
508     free(iu->table);
509     free(iu);
510
511 }
512
513
514 /* Called when the current transaction is aborted to throw away any queued
515  * incremental updates
516  */
517
518 incremental_flush()
519 {
520     struct iupdate *iu;
521
522     while (sq_get_data(incremental_sq, &iu)) {
523         free_argv(iu->before, iu->beforec);
524         free_argv(iu->after, iu->afterc);
525         free(iu->table);
526         free(iu);
527     }
528     sq_destroy(incremental_sq);
529     incremental_sq = sq_create();
530 }
531
532
533 char **copy_argv(argv, argc)
534 char **argv;
535 int argc;
536 {
537     char **ret = (char **)malloc(sizeof(char *) * argc);
538     while (--argc >= 0)
539       ret[argc] = strsave(strtrim(argv[argc]));
540     return(ret);
541 }
542
543 free_argv(argv, argc)
544 char **argv;
545 int argc;
546 {
547     while (--argc >= 0)
548       free(argv[argc]);
549     free(argv);
550 }
This page took 0.295794 seconds and 3 git commands to generate.