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