]> andersk Git - moira.git/blob - server/increment.pc
bb1f26bea27e7d0b6639f1f83e2ab5326cb63caa
[moira.git] / server / increment.pc
1 /* $Id$
2  *
3  * Deal with incremental updates
4  *
5  * Copyright (C) 1989-1998 by the Massachusetts Institute of Technology
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include "mr_server.h"
12 #include "query.h"
13 #include "qrtn.h"
14
15 #include <signal.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 EXEC SQL INCLUDE sqlca;
22
23 RCSID("$Header$");
24
25 extern char *whoami;
26 extern char *table_name[];
27 extern int num_tables;
28
29 int inc_pid = 0;
30 int inc_running = 0;
31 time_t inc_started;
32
33 #define MAXARGC 15
34
35 EXEC SQL WHENEVER SQLERROR DO dbmserr();
36
37 /* structures to save before args */
38 static char *before[MAXARGC];
39 static int beforec;
40 static enum tables beforetable;
41
42 /* structures to save after args */
43 static char *after[MAXARGC];
44 static int afterc;
45
46 /* structures to save entire sets of incremental changes */
47 struct save_queue *incremental_sq = NULL;
48 struct save_queue *incremental_exec = NULL;
49 struct iupdate {
50   char *table;
51   int beforec;
52   char **before;
53   int afterc;
54   char **after;
55   char *service;
56 };
57
58 void next_incremental(void);
59 char **copy_argv(char **argv, int argc);
60 void free_argv(char **argv, int argc);
61 int table_num(char *table);
62
63 void incremental_init(void)
64 {
65   int i;
66
67   if (!incremental_sq)
68     incremental_sq = sq_create();
69   if (!incremental_exec)
70     incremental_exec = sq_create();
71
72   for (i = 0; i < MAXARGC; i++)
73     {
74       before[i] = xmalloc(MAX_FIELD_WIDTH);
75       after[i] = xmalloc(MAX_FIELD_WIDTH);
76     }
77 }
78
79
80 /* record the state of a table row before it is changed */
81
82 void incremental_before(enum tables table, char *qual, char **argv)
83 {
84   EXEC SQL BEGIN DECLARE SECTION;
85   int id;
86   EXEC SQL END DECLARE SECTION;
87
88   char *name, *name2;
89
90   beforetable = table;
91
92   switch (table)
93     {
94     case USERS_TABLE:
95       sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
96               "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
97               "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
98       dosql(before);
99       beforec = 11;
100       break;
101     case MACHINE_TABLE:
102       sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
103               "WHERE %s", qual);
104       dosql(before);
105       beforec = 3;
106       break;
107     case CLUSTERS_TABLE:
108       sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
109               "c.clu_id FROM clusters c WHERE %s", qual);
110       dosql(before);
111       beforec = 4;
112       break;
113     case CONTAINERS_TABLE:
114       sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
115               "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s", 
116               qual);
117       dosql(before);
118       beforec = 7;
119       name = xmalloc(0);
120       id = atoi(before[5]);
121       if (!strncmp(before[4], "USER", 4))
122         {
123           id_to_name(id, USERS_TABLE, &name);
124           strcpy(before[5], name);
125         }
126       else if (!strncmp(before[4], "LIST", 4))
127         {
128           id_to_name(id, LIST_TABLE, &name);
129           strcpy(before[5], name);
130         }
131       else if (!strncmp(before[4], "KERBEROS", 8))
132         {
133           id_to_name(id, STRINGS_TABLE, &name);
134           strcpy(before[5], name);
135         }
136       break;
137     case MCMAP_TABLE:
138       strcpy(before[0], argv[0]);
139       strcpy(before[1], argv[1]);
140       beforec = 2;
141       break;
142     case SVC_TABLE:
143       strcpy(before[0], argv[0]);
144       strcpy(before[1], argv[1]);
145       strcpy(before[2], argv[2]);
146       beforec = 3;
147       break;
148     case FILESYS_TABLE:
149       sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
150               "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
151               "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
152               "WHERE %s", qual);
153       dosql(before);
154       name = xmalloc(0);
155       id = atoi(before[2]);
156       id_to_name(id, MACHINE_TABLE, &name);
157       strcpy(before[2], name);
158       id = atoi(before[7]);
159       id_to_name(id, USERS_TABLE, &name);
160       strcpy(before[7], name);
161       id = atoi(before[8]);
162       id_to_name(id, LIST_TABLE, &name);
163       strcpy(before[8], name);
164       free(name);
165       beforec = 12;
166       break;
167     case QUOTA_TABLE:
168       strcpy(before[0], "?");
169       strcpy(before[1], argv[1]);
170       strcpy(before[2], "?");
171       sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
172               "WHERE %s AND fs.filsys_id = q.filsys_id", qual);
173       dosql(&(before[3]));
174       strcpy(before[2], argv[1]);
175       beforec = 5;
176       break;
177     case LIST_TABLE:
178       sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
179               "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
180               "l.description, l.list_id FROM list l WHERE %s", qual);
181       dosql(before);
182       beforec = 11;
183       break;
184     case IMEMBERS_TABLE:
185       id = (int) argv[0];
186       sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
187               "grouplist, gid FROM list WHERE list_id = %d", id);
188       dosql(&(before[3]));
189       name = xmalloc(0);
190       id_to_name(id, LIST_TABLE, &name);
191       name2 = xmalloc(0);
192       strcpy(before[0], name);
193       strcpy(before[1], argv[1]);
194       id = (int) argv[2];
195       beforec = 10;
196       if (!strcmp(before[1], "USER"))
197         {
198           id_to_name(id, USERS_TABLE, &name2);
199           EXEC SQL SELECT status, users_id INTO :before[9], :before[11] 
200             FROM users WHERE users_id = :id;
201           EXEC SQL SELECT list_id INTO :before[10] FROM list
202             WHERE name = :name;
203           beforec = 12;
204       }
205       else if (!strcmp(before[1], "LIST"))
206         {
207           id_to_name(id, LIST_TABLE, &name2);
208           EXEC SQL SELECT list_id INTO :before[9] FROM list
209             WHERE name = :name;
210           sprintf(before[10], "%d", id);
211           beforec = 11;
212         }
213       else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
214         {
215           id_to_name(id, STRINGS_TABLE, &name2);
216           EXEC SQL SELECT list_id INTO :before[9] FROM list
217             WHERE name = :name;
218         }
219       else if (!strcmp(before[1], "MACHINE"))
220         {
221           id_to_name(id, MACHINE_TABLE, &name2);
222           EXEC SQL SELECT list_id INTO :before[9] FROM list
223             WHERE name = :name;
224           sprintf(before[10], "%d", id);
225           beforec = 11;
226         }
227       strcpy(before[2], name2);
228       free(name);
229       free(name2);
230       break;
231     default:
232         /*
233         com_err(whoami, 0, "requested incremental on unexpected table `%s'",
234                 table_name[table]);
235         */
236       break;
237     }
238 }
239
240
241 void incremental_clear_before(void)
242 {
243   beforec = 0;
244 }
245
246
247 /* add an element to the incremental queue for the changed row */
248
249 void incremental_after(enum tables table, char *qual, char **argv)
250 {
251   char *name, *name2;
252   EXEC SQL BEGIN DECLARE SECTION;
253   int id;
254   EXEC SQL END DECLARE SECTION;
255   struct iupdate *iu;
256
257   switch (table)
258     {
259     case USERS_TABLE:
260       sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
261               "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
262               "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
263       dosql(after);
264       afterc = 11;
265       break;
266     case MACHINE_TABLE:
267       sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
268               "WHERE %s", qual);
269       dosql(after);
270       afterc = 3;
271       break;
272     case CLUSTERS_TABLE:
273       sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
274               "c.clu_id FROM clusters c WHERE %s", qual);
275       dosql(after);
276       afterc = 4;
277       break;
278     case CONTAINERS_TABLE:
279       sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
280               "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
281               qual);
282       dosql(after);
283       afterc = 7;
284       name = xmalloc(0);
285       id = atoi(after[5]);
286       if (!strncmp(after[4], "USER", 4))
287         {
288           id_to_name(id, USERS_TABLE, &name);
289           strcpy(after[5], name);
290         }
291       else if (!strncmp(after[4], "LIST", 4))
292         {
293           id_to_name(id, LIST_TABLE, &name);
294           strcpy(after[5], name);
295         }
296       else if (!strncmp(after[4], "KERBEROS", 8))
297         {
298           id_to_name(id, STRINGS_TABLE, &name);
299           strcpy(after[5], name);
300         }
301       break;
302     case MCMAP_TABLE:
303       strcpy(after[0], argv[0]);
304       strcpy(after[1], argv[1]);
305       afterc = 2;
306       break;
307     case SVC_TABLE:
308       strcpy(after[0], argv[0]);
309       strcpy(after[1], argv[1]);
310       strcpy(after[2], argv[2]);
311       afterc = 3;
312       break;
313     case FILESYS_TABLE:
314       sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
315               "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
316               "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
317               "WHERE %s", qual);
318       dosql(after);
319       name = xmalloc(0);
320       id = atoi(after[2]);
321       id_to_name(id, MACHINE_TABLE, &name);
322       strcpy(after[2], name);
323       id = atoi(after[7]);
324       id_to_name(id, USERS_TABLE, &name);
325       strcpy(after[7], name);
326       id = atoi(after[8]);
327       id_to_name(id, LIST_TABLE, &name);
328       strcpy(after[8], name);
329       free(name);
330       afterc = 12;
331       break;
332     case QUOTA_TABLE:
333       strcpy(after[0], "?");
334       strcpy(after[1], argv[1]);
335       strcpy(after[2], "?");
336       sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
337               "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
338               qual, argv[1]);
339       dosql(&(after[3]));
340       afterc = 5;
341       break;
342     case LIST_TABLE:
343       sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
344               "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
345               "l.description, l.list_id FROM list l WHERE %s", qual);
346       dosql(after);
347       afterc = 11;
348       break;
349     case IMEMBERS_TABLE:
350       id = (int) argv[0];
351       sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
352               "grouplist, gid FROM list WHERE list_id = %d", id);
353       dosql(&(after[3]));
354       name = xmalloc(0);
355       id_to_name(id, LIST_TABLE, &name);
356       name2 = xmalloc(0);
357       strcpy(after[0], name);
358       strcpy(after[1], argv[1]);
359       id = (int) argv[2];
360       afterc = 10;
361       if (!strcmp(after[1], "USER"))
362         {
363           id_to_name(id, USERS_TABLE, &name2);
364           EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
365             FROM users WHERE users_id = :id;
366           EXEC SQL SELECT list_id INTO :after[10] FROM list
367             WHERE name = :name;
368           afterc = 12;
369         }
370       else if (!strcmp(after[1], "LIST"))
371         {
372           id_to_name(id, LIST_TABLE, &name2);
373           EXEC SQL SELECT list_id INTO :after[9] FROM list
374             WHERE name = :name;
375           sprintf(after[10], "%d", id);
376           afterc = 11;
377         }
378       else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
379         {
380           id_to_name(id, STRINGS_TABLE, &name2);
381           EXEC SQL SELECT list_id INTO :after[9] FROM list
382             WHERE name = :name;
383         }
384       else if (!strcmp(after[1], "MACHINE"))
385         {
386           id_to_name(id, MACHINE_TABLE, &name2);
387           EXEC SQL SELECT list_id INTO :after[9] FROM list
388             WHERE name = :name;
389           sprintf(after[10], "%d", id);
390           afterc = 11;
391         }
392       strcpy(after[2], name2);
393       free(name);
394       free(name2);
395       break;
396     case NO_TABLE:
397       afterc = 0;
398       table = beforetable;
399       break;
400     default:
401         /*
402         com_err(whoami, 0, "requested incremental on unexpected table `%s'",
403                 table_name[table]);
404         */
405       break;
406     }
407
408   iu = xmalloc(sizeof(struct iupdate));
409   iu->table = table_name[table];
410   iu->beforec = beforec;
411   iu->before = copy_argv(before, beforec);
412   iu->afterc = afterc;
413   iu->after = copy_argv(after, afterc);
414   sq_save_data(incremental_sq, iu);
415 }
416
417 void incremental_clear_after(void)
418 {
419   incremental_after(NO_TABLE, NULL, NULL);
420 }
421
422
423 /* Called when the current transaction is committed to start any queued
424  * incremental updates.  This caches the update table the first time it
425  * is called.
426  */
427
428 struct inc_cache {
429   struct inc_cache *next;
430   char *table, *service;
431 };
432
433
434 void incremental_update(void)
435 {
436   static int inited = 0;
437   static struct inc_cache *cache;
438   struct inc_cache *c;
439   EXEC SQL BEGIN DECLARE SECTION;
440   char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
441   EXEC SQL END DECLARE SECTION;
442   struct iupdate *iu, *iu_save;
443
444   if (!inited)
445     {
446       inited++;
447
448       EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
449         FROM incremental;
450       EXEC SQL OPEN inc;
451       while (1)
452         {
453           EXEC SQL FETCH inc INTO :tab, :serv;
454           if (sqlca.sqlcode)
455             break;
456           c = xmalloc(sizeof(struct inc_cache));
457           c->next = cache;
458           c->table = xstrdup(strtrim(tab));
459           c->service = xstrdup(strtrim(serv));
460           cache = c;
461         }
462       EXEC SQL CLOSE inc;
463       EXEC SQL COMMIT WORK;
464     }
465
466   while (sq_remove_data(incremental_sq, &iu))
467     {
468       for (c = cache; c; c = c->next)
469         {
470           if (!strcmp(c->table, iu->table))
471             {
472               iu->service = c->service;
473               iu_save = xmalloc(sizeof(struct iupdate));
474               iu_save->service = iu->service;
475               iu_save->table = iu->table;
476               iu_save->beforec = iu->beforec;
477               iu_save->afterc = iu->afterc;
478               iu_save->before = copy_argv(iu->before, iu->beforec);
479               iu_save->after = copy_argv(iu->after, iu->afterc);
480               sq_save_data(incremental_exec, iu_save);
481             }
482         }
483       if (!c)
484         {
485           free_argv(iu->before, iu->beforec);
486           free_argv(iu->after, iu->afterc);
487           free(iu);
488         }
489     }
490   if (inc_running == 0)
491     next_incremental();
492 }
493
494 /* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
495    We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
496    this function so proc will pass it through without reading it. */
497
498 #ifndef _PROC_
499 void next_incremental(void)
500 {
501   struct iupdate *iu;
502   char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
503   int i;
504   sigset_t sigs;
505
506   if (!incremental_exec)
507     incremental_init();
508
509   if (sq_empty(incremental_exec) ||
510       (inc_running && now - inc_started < INC_TIMEOUT))
511     return;
512
513   if (inc_running)
514     com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
515
516   sq_remove_data(incremental_exec, &iu);
517   argv[1] = iu->table;
518   sprintf(cbefore, "%d", iu->beforec);
519   argv[2] = cbefore;
520   sprintf(cafter, "%d", iu->afterc);
521   argv[3] = cafter;
522   for (i = 0; i < iu->beforec; i++)
523     argv[4 + i] = iu->before[i];
524   for (i = 0; i < iu->afterc; i++)
525     argv[4 + iu->beforec + i] = iu->after[i];
526
527   sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
528   argv[0] = prog;
529   argv[4 + iu->beforec + iu->afterc] = 0;
530
531   sigemptyset(&sigs);
532   sigaddset(&sigs, SIGCHLD);
533   sigprocmask(SIG_BLOCK, &sigs, NULL);
534   inc_pid = vfork();
535   switch (inc_pid)
536     {
537     case 0:
538       execv(prog, argv);
539       _exit(1);
540     case -1:
541       com_err(whoami, 0, "Failed to start incremental update");
542       break;
543     default:
544       inc_running = 1;
545       inc_started = now;
546     }
547   sigprocmask(SIG_UNBLOCK, &sigs, NULL);
548
549   free_argv(iu->before, iu->beforec);
550   free_argv(iu->after, iu->afterc);
551   free(iu);
552 }
553 #endif
554
555 /* Called when the current transaction is aborted to throw away any queued
556  * incremental updates
557  */
558
559 void incremental_flush(void)
560 {
561   struct iupdate *iu;
562
563   while (sq_get_data(incremental_sq, &iu))
564     {
565       free_argv(iu->before, iu->beforec);
566       free_argv(iu->after, iu->afterc);
567       free(iu);
568     }
569   sq_destroy(incremental_sq);
570   incremental_sq = sq_create();
571 }
572
573
574 char **copy_argv(char **argv, int argc)
575 {
576   char **ret = xmalloc(sizeof(char *) * argc);
577   while (--argc >= 0)
578     ret[argc] = xstrdup(strtrim(argv[argc]));
579   return ret;
580 }
581
582 void free_argv(char **argv, int argc)
583 {
584   while (--argc >= 0)
585     free(argv[argc]);
586   free(argv);
587 }
588
589 int table_num(char *name)
590 {
591   int i;
592
593   for (i = num_tables - 1; i; i--)
594     {
595       if (!strcmp(table_name[i], name))
596         break;
597     }
598
599   return i; /* 0 = "none" if no match */
600 }
This page took 0.075311 seconds and 3 git commands to generate.