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