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