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