]> andersk Git - moira.git/blame - server/increment.dc
*** empty log message ***
[moira.git] / server / increment.dc
CommitLineData
32cfe906 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
13static 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"
20EXEC SQL INCLUDE sqlca
21
22extern char *whoami;
23char *malloc();
24
25int inc_pid = 0;
26int inc_running = 0;
27time_t inc_started;
28
29#define MAXARGC 15
30
31/* structures to save before args */
32static char beforeb[MAXARGC][ARGLEN];
33static char *before[MAXARGC];
34EXEC SQL BEGIN DECLARE SECTION;
35char *barg0, *barg1, *barg2, *barg3, *barg4;
36char *barg5, *barg6, *barg7, *barg8, *barg9;
37char *barg10, *barg11, *barg12, *barg13, *barg14;
38EXEC SQL END DECLARE SECTION;
39static int beforec;
40static char *beforetable;
41
42/* structures to save after args */
43static char afterb[MAXARGC][ARGLEN];
44static char *after[MAXARGC];
45EXEC SQL BEGIN DECLARE SECTION;
46char *aarg0, *aarg1, *aarg2, *aarg3, *aarg4;
47char *aarg5, *aarg6, *aarg7, *aarg8, *aarg9;
48char *aarg10, *aarg11, *aarg12, *aarg13, *aarg14;
49EXEC SQL END DECLARE SECTION;
50static int afterc;
51
52/* structures to save entire sets of incremental changes */
53struct save_queue *incremental_sq = NULL;
54struct save_queue *incremental_exec = NULL;
55struct iupdate {
56 char *table;
57 int beforec;
58 char **before;
59 int afterc;
60 char **after;
61 char *service;
62};
63
64
65incremental_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
110incremental_before(table, qual, argv)
111char *table;
112char *qual;
113char **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 FROM users WHERE :qual;
134 beforec = 9;
135 } else if (!strcmp(table, "machine")) {
136## retrieve (barg0 = m.name, barg1 = m.type) where qual
137 beforec = 2;
138 } else if (!strcmp(table, "cluster")) {
139## retrieve (barg0 = c.name, barg1 = c.desc, barg2 = c.location)
140## where qual
141 beforec = 3;
142 } else if (!strcmp(table, "mcmap")) {
143 strcpy(barg0, argv[0]);
144 strcpy(barg1, argv[1]);
145 beforec = 2;
146 } else if (!strcmp(table, "svc")) {
147 strcpy(barg0, argv[0]);
148 strcpy(barg1, argv[1]);
149 strcpy(barg2, argv[2]);
150 beforec = 3;
151 } else if (!strcmp(table, "filesys")) {
152## range of fs is filesys
153## retrieve (barg0 = fs.label, barg1 = fs.type, barg2 = text(fs.mach_id),
154## barg3 = fs.name, barg4 = fs.mount, barg5 = fs.access,
155## barg6 = fs.comments, barg7 = text(fs.owner),
156## barg8 = text(fs.owners), barg9 = text(fs.createflg),
157## barg10 = fs.lockertype)
158## where qual
159 beforec = 11;
160 } else if (!strcmp(table, "quota")) {
161 strcpy(barg0, "?");
162 strcpy(barg1, argv[1]);
163 strcpy(barg2, "?");
164 sprintf(buffer, "%s and filesys.filsys_id = q.filsys_id", qual);
165 qual = buffer;
166## range of q is quota
167## retrieve (barg3 = text(q.quota), barg4 = filesys.name) where qual
168 beforec = 5;
169 } else if (!strcmp(table, "list")) {
170## retrieve (barg0 = l.name, barg1 = text(l.active),
171## barg2 = text(l.public), barg3 = text(l.hidden),
172## barg4 = text(l.maillist), barg5 = text(l.group),
173## barg6 = text(l.gid), barg7 = l.acl_type,
174## barg8 = text(l.acl_id), barg9 = l.desc)
175## where qual
176 beforec = 10;
177 } else if (!strcmp(table, "members")) {
178 id = (int) argv[0];
179## retrieve (barg3 = text(list.group)) where list.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
202incremental_clear_before()
203{
204 beforec = 0;
205}
206
207incremental_clear_after()
208{
209 incremental_after("clear", 0);
210}
211
212
213
214##incremental_after(table, qual, argv)
215##char *table;
216##char *qual;
217char **argv;
218##{
219 char buffer[2048], *name;
220## int id, i;
221 struct iupdate *iu;
222 char **copy_argv();
223
224 if (!strcmp(table, "users")) {
225## retrieve (aarg0 = u.login, aarg1 = text(u.uid),
226## aarg2 = u.shell, aarg3 = u.last, aarg4 = u.first,
227## aarg5 = u.middle, aarg6 = text(u.status),
228## aarg7 = u.mit_id, aarg8 = u.mit_year)
229## where qual
230 afterc = 9;
231 } else if (!strcmp(table, "machine")) {
232## retrieve (aarg0 = m.name, aarg1 = m.type) where qual
233 afterc = 2;
234 } else if (!strcmp(table, "cluster")) {
235## retrieve (aarg0 = c.name, aarg1 = c.desc, aarg2 = c.location)
236## where qual
237 afterc = 3;
238 } else if (!strcmp(table, "mcmap")) {
239 strcpy(aarg0, argv[0]);
240 strcpy(aarg1, argv[1]);
241 afterc = 2;
242 } else if (!strcmp(table, "svc")) {
243 strcpy(aarg0, argv[0]);
244 strcpy(aarg1, argv[1]);
245 strcpy(aarg2, argv[2]);
246 afterc = 3;
247 } else if (!strcmp(table, "filesys")) {
248## range of fs is filesys
249## retrieve (aarg0 = fs.label, aarg1 = fs.type,
250## aarg2 = text(fs.mach_id),
251## aarg3 = fs.name, aarg4 = fs.mount, aarg5 = fs.access,
252## aarg6 = fs.comments, aarg7 = text(fs.owner),
253## aarg8 = text(fs.owners), aarg9 = text(fs.createflg),
254## aarg10 = fs.lockertype)
255## where qual
256 afterc = 11;
257 } else if (!strcmp(table, "quota")) {
258 strcpy(aarg0, "?");
259 strcpy(aarg1, argv[1]);
260 strcpy(aarg2, "?");
261 sprintf(buffer, "%s and filesys.filsys_id = q.filsys_id", qual);
262 qual = buffer;
263## range of q is quota
264## retrieve (aarg3 = text(q.quota), aarg4 = filesys.name) where qual
265 afterc = 5;
266 } else if (!strcmp(table, "list")) {
267## retrieve (aarg0 = l.name, aarg1 = text(l.active),
268## aarg2 = text(l.public), aarg3 = text(l.hidden),
269## aarg4 = text(l.maillist), aarg5 = text(l.group),
270## aarg6 = text(l.gid), aarg7 = l.acl_type,
271## aarg8 = text(l.acl_id), aarg9 = l.desc)
272## where qual
273 afterc = 10;
274 } else if (!strcmp(table, "members")) {
275 id = (int) argv[0];
276## retrieve (aarg3 = text(list.group)) where list.list_id = id
277 name = malloc(0);
278 id_to_name(id, "LIST", &name);
279 strcpy(aarg0, name);
280 strcpy(aarg1, argv[1]);
281 id = (int) argv[2];
282 if (!strcmp(aarg1, "USER")) {
283 id_to_name(id, aarg1, &name);
284 } else if (!strcmp(aarg1, "LIST")) {
285 id_to_name(id, aarg1, &name);
286 } else if (!strcmp(aarg1, "STRING")) {
287 id_to_name(id, aarg1, &name);
288 } else if (!strcmp(aarg1, "KERBEROS")) {
289 id_to_name(id, "STRING", &name);
290 }
291 strcpy(aarg2, name);
292 free(name);
293 afterc = 4;
294 } else if (!strcmp(table, "clear")) {
295 afterc = 0;
296 table = beforetable;
297 } /* else
298 com_err(whoami, 0, "unknown table in incremental_after"); */
299
300 iu = (struct iupdate *) malloc(sizeof(struct iupdate));
301 iu->table = strsave(table);
302 iu->beforec = beforec;
303 iu->before = copy_argv(before, beforec);
304 iu->afterc = afterc;
305 iu->after = copy_argv(after, afterc);
306 sq_save_data(incremental_sq, iu);
307
308#ifdef DEBUG
309 sprintf(buffer, "INCREMENTAL(%s, [", table);
310 for (i = 0; i < beforec; i++) {
311 if (i == 0)
312 strcat(buffer, strtrim(before[0]));
313 else {
314 strcat(buffer, ", ");
315 strcat(buffer, strtrim(before[i]));
316 }
317 }
318 strcat(buffer, "], [");
319 for (i = 0; i < afterc; i++) {
320 if (i == 0)
321 strcat(buffer, strtrim(after[0]));
322 else {
323 strcat(buffer, ", ");
324 strcat(buffer, strtrim(after[i]));
325 }
326 }
327 strcat(buffer, "])");
328 com_err(whoami, 0, buffer);
329#endif DEBUG
330##}
331
332
333/* Called when the current transaction is committed to start any queued
334 * incremental updates. This caches the update table the first time it
335 * is called.
336 */
337
338struct inc_cache {
339 struct inc_cache *next;
340 char *table;
341 char *service;
342};
343
344
345incremental_update()
346{
347 static int inited = 0;
348 static struct inc_cache *cache;
349 struct inc_cache *c;
350 EXEC SQL BEGIN DECLARE SECTION;
351 char tab[17], serv[17];
352 EXEC SQL END DECLARE SECTION;
353 struct iupdate *iu;
354
355 if (!inited) {
356 inited++;
357
358 EXEC SQL DECLARE inc CURSOR FOR SELECT table, service FROM incremental;
359 EXEC SQL OPEN inc;
360 while (1) {
361 EXEC SQL FETCH inc INTO :tab, :serv;
362 if (sqlca.sqlcode != 0) break;
363 c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
364 c->next = cache;
365 c->table = strsave(strtrim(tab));
366 c->service = strsave(strtrim(serv));
367 cache = c;
368 }
369 EXEC SQL CLOSE inc;
370 }
371
372 while (sq_remove_data(incremental_sq, &iu)) {
373 for (c = cache; c; c = c->next) {
374 if (!strcmp(c->table, iu->table)) {
375 iu->service = c->service;
376 sq_save_data(incremental_exec, iu);
377 }
378 }
379 }
380 if (inc_running == 0)
381 next_incremental();
382}
383
384
385next_incremental()
386{
387 struct iupdate *iu;
388 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
389 int i;
390
391 if (incremental_exec == NULL)
392 incremental_init();
393
394 if (sq_empty(incremental_exec) ||
395 (inc_running && now - inc_started < INC_TIMEOUT))
396 return;
397
398 if (inc_running)
399 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
400
401 sq_remove_data(incremental_exec, &iu);
402 argv[1] = iu->table;
403 sprintf(cbefore, "%d", iu->beforec);
404 argv[2] = cbefore;
405 sprintf(cafter, "%d", iu->afterc);
406 argv[3] = cafter;
407 for (i = 0; i < iu->beforec; i++)
408 argv[4 + i] = iu->before[i];
409 for (i = 0; i < iu->afterc; i++)
410 argv[4 + iu->beforec + i] = iu->after[i];
411
412 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
413#ifdef DEBUG
414 com_err(whoami, 0, "forking %s", prog);
415#endif
416 argv[0] = prog;
417 argv[4 + iu->beforec + iu->afterc] = 0;
418 inc_pid = vfork();
419 switch (inc_pid) {
420 case 0:
421 execv(prog, argv);
422 exit(1);
423 case -1:
424 com_err(whoami, 0, "Failed to start incremental update");
425 break;
426 default:
427 inc_running = 1;
428 inc_started = now;
429 }
430
431 free_argv(iu->before, iu->beforec);
432 free_argv(iu->after, iu->afterc);
433 free(iu->table);
434 free(iu);
435
436}
437
438
439/* Called when the current transaction is aborted to throw away any queued
440 * incremental updates
441 */
442
443incremental_flush()
444{
445 struct iupdate *iu;
446
447 while (sq_get_data(incremental_sq, &iu)) {
448 free_argv(iu->before, iu->beforec);
449 free_argv(iu->after, iu->afterc);
450 free(iu->table);
451 free(iu);
452 }
453 sq_destroy(incremental_sq);
454 incremental_sq = sq_create();
455}
456
457
458char **copy_argv(argv, argc)
459char **argv;
460int argc;
461{
462 char **ret = (char **)malloc(sizeof(char *) * argc);
463 while (--argc >= 0)
464 ret[argc] = strsave(strtrim(argv[argc]));
465 return(ret);
466}
467
468free_argv(argv, argc)
469char **argv;
470int argc;
471{
472 while (--argc >= 0)
473 free(argv[argc]);
474 free(argv);
475}
This page took 0.115618 seconds and 5 git commands to generate.