]> andersk Git - moira.git/blob - server/increment.pc
Support MACHINE members of lists.
[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 mach_id INTO :before[9] FROM machine
223             WHERE name = :name;
224         }
225       strcpy(before[2], name2);
226       free(name);
227       free(name2);
228       break;
229     default:
230         /*
231         com_err(whoami, 0, "requested incremental on unexpected table `%s'",
232                 table_name[table]);
233         */
234       break;
235     }
236 }
237
238
239 void incremental_clear_before(void)
240 {
241   beforec = 0;
242 }
243
244
245 /* add an element to the incremental queue for the changed row */
246
247 void incremental_after(enum tables table, char *qual, char **argv)
248 {
249   char *name, *name2;
250   EXEC SQL BEGIN DECLARE SECTION;
251   int id;
252   EXEC SQL END DECLARE SECTION;
253   struct iupdate *iu;
254
255   switch (table)
256     {
257     case USERS_TABLE:
258       sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
259               "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
260               "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
261       dosql(after);
262       afterc = 11;
263       break;
264     case MACHINE_TABLE:
265       sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
266               "WHERE %s", qual);
267       dosql(after);
268       afterc = 3;
269       break;
270     case CLUSTERS_TABLE:
271       sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
272               "c.clu_id FROM clusters c WHERE %s", qual);
273       dosql(after);
274       afterc = 4;
275       break;
276     case CONTAINERS_TABLE:
277       sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
278               "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
279               qual);
280       dosql(after);
281       afterc = 7;
282       name = xmalloc(0);
283       id = atoi(after[5]);
284       if (!strncmp(after[4], "USER", 4))
285         {
286           id_to_name(id, USERS_TABLE, &name);
287           strcpy(after[5], name);
288         }
289       else if (!strncmp(after[4], "LIST", 4))
290         {
291           id_to_name(id, LIST_TABLE, &name);
292           strcpy(after[5], name);
293         }
294       else if (!strncmp(after[4], "KERBEROS", 8))
295         {
296           id_to_name(id, STRINGS_TABLE, &name);
297           strcpy(after[5], name);
298         }
299       break;
300     case MCMAP_TABLE:
301       strcpy(after[0], argv[0]);
302       strcpy(after[1], argv[1]);
303       afterc = 2;
304       break;
305     case SVC_TABLE:
306       strcpy(after[0], argv[0]);
307       strcpy(after[1], argv[1]);
308       strcpy(after[2], argv[2]);
309       afterc = 3;
310       break;
311     case FILESYS_TABLE:
312       sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
313               "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
314               "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
315               "WHERE %s", qual);
316       dosql(after);
317       name = xmalloc(0);
318       id = atoi(after[2]);
319       id_to_name(id, MACHINE_TABLE, &name);
320       strcpy(after[2], name);
321       id = atoi(after[7]);
322       id_to_name(id, USERS_TABLE, &name);
323       strcpy(after[7], name);
324       id = atoi(after[8]);
325       id_to_name(id, LIST_TABLE, &name);
326       strcpy(after[8], name);
327       free(name);
328       afterc = 12;
329       break;
330     case QUOTA_TABLE:
331       strcpy(after[0], "?");
332       strcpy(after[1], argv[1]);
333       strcpy(after[2], "?");
334       sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
335               "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
336               qual, argv[1]);
337       dosql(&(after[3]));
338       afterc = 5;
339       break;
340     case LIST_TABLE:
341       sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
342               "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
343               "l.description, l.list_id FROM list l WHERE %s", qual);
344       dosql(after);
345       afterc = 11;
346       break;
347     case IMEMBERS_TABLE:
348       id = (int) argv[0];
349       sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
350               "grouplist, gid FROM list WHERE list_id = %d", id);
351       dosql(&(after[3]));
352       name = xmalloc(0);
353       id_to_name(id, LIST_TABLE, &name);
354       name2 = xmalloc(0);
355       strcpy(after[0], name);
356       strcpy(after[1], argv[1]);
357       id = (int) argv[2];
358       afterc = 10;
359       if (!strcmp(after[1], "USER"))
360         {
361           id_to_name(id, USERS_TABLE, &name2);
362           EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
363             FROM users WHERE users_id = :id;
364           EXEC SQL SELECT list_id INTO :after[10] FROM list
365             WHERE name = :name;
366           afterc = 12;
367         }
368       else if (!strcmp(after[1], "LIST"))
369         {
370           id_to_name(id, LIST_TABLE, &name2);
371           EXEC SQL SELECT list_id INTO :after[9] FROM list
372             WHERE name = :name;
373           sprintf(after[10], "%d", id);
374           afterc = 11;
375         }
376       else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
377         {
378           id_to_name(id, STRINGS_TABLE, &name2);
379           EXEC SQL SELECT list_id INTO :after[9] FROM list
380             WHERE name = :name;
381         }
382       else if (!strcmp(after[1], "MACHINE"))
383         {
384           id_to_name(id, MACHINE_TABLE, &name2);
385           EXEC SQL SELECT mach_id INTO :after[9] FROM machine
386             WHERE name = :name;
387         }
388       strcpy(after[2], name2);
389       free(name);
390       free(name2);
391       break;
392     case NO_TABLE:
393       afterc = 0;
394       table = beforetable;
395       break;
396     default:
397         /*
398         com_err(whoami, 0, "requested incremental on unexpected table `%s'",
399                 table_name[table]);
400         */
401       break;
402     }
403
404   iu = xmalloc(sizeof(struct iupdate));
405   iu->table = table_name[table];
406   iu->beforec = beforec;
407   iu->before = copy_argv(before, beforec);
408   iu->afterc = afterc;
409   iu->after = copy_argv(after, afterc);
410   sq_save_data(incremental_sq, iu);
411 }
412
413 void incremental_clear_after(void)
414 {
415   incremental_after(NO_TABLE, NULL, NULL);
416 }
417
418
419 /* Called when the current transaction is committed to start any queued
420  * incremental updates.  This caches the update table the first time it
421  * is called.
422  */
423
424 struct inc_cache {
425   struct inc_cache *next;
426   char *table, *service;
427 };
428
429
430 void incremental_update(void)
431 {
432   static int inited = 0;
433   static struct inc_cache *cache;
434   struct inc_cache *c;
435   EXEC SQL BEGIN DECLARE SECTION;
436   char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
437   EXEC SQL END DECLARE SECTION;
438   struct iupdate *iu, *iu_save;
439
440   if (!inited)
441     {
442       inited++;
443
444       EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
445         FROM incremental;
446       EXEC SQL OPEN inc;
447       while (1)
448         {
449           EXEC SQL FETCH inc INTO :tab, :serv;
450           if (sqlca.sqlcode)
451             break;
452           c = xmalloc(sizeof(struct inc_cache));
453           c->next = cache;
454           c->table = xstrdup(strtrim(tab));
455           c->service = xstrdup(strtrim(serv));
456           cache = c;
457         }
458       EXEC SQL CLOSE inc;
459       EXEC SQL COMMIT WORK;
460     }
461
462   while (sq_remove_data(incremental_sq, &iu))
463     {
464       for (c = cache; c; c = c->next)
465         {
466           if (!strcmp(c->table, iu->table))
467             {
468               iu->service = c->service;
469               iu_save = xmalloc(sizeof(struct iupdate));
470               iu_save->service = iu->service;
471               iu_save->table = iu->table;
472               iu_save->beforec = iu->beforec;
473               iu_save->afterc = iu->afterc;
474               iu_save->before = copy_argv(iu->before, iu->beforec);
475               iu_save->after = copy_argv(iu->after, iu->afterc);
476               sq_save_data(incremental_exec, iu_save);
477             }
478         }
479       if (!c)
480         {
481           free_argv(iu->before, iu->beforec);
482           free_argv(iu->after, iu->afterc);
483           free(iu);
484         }
485     }
486   if (inc_running == 0)
487     next_incremental();
488 }
489
490 /* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
491    We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
492    this function so proc will pass it through without reading it. */
493
494 #ifndef _PROC_
495 void next_incremental(void)
496 {
497   struct iupdate *iu;
498   char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
499   int i;
500   sigset_t sigs;
501
502   if (!incremental_exec)
503     incremental_init();
504
505   if (sq_empty(incremental_exec) ||
506       (inc_running && now - inc_started < INC_TIMEOUT))
507     return;
508
509   if (inc_running)
510     com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
511
512   sq_remove_data(incremental_exec, &iu);
513   argv[1] = iu->table;
514   sprintf(cbefore, "%d", iu->beforec);
515   argv[2] = cbefore;
516   sprintf(cafter, "%d", iu->afterc);
517   argv[3] = cafter;
518   for (i = 0; i < iu->beforec; i++)
519     argv[4 + i] = iu->before[i];
520   for (i = 0; i < iu->afterc; i++)
521     argv[4 + iu->beforec + i] = iu->after[i];
522
523   sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
524   argv[0] = prog;
525   argv[4 + iu->beforec + iu->afterc] = 0;
526
527   sigemptyset(&sigs);
528   sigaddset(&sigs, SIGCHLD);
529   sigprocmask(SIG_BLOCK, &sigs, NULL);
530   inc_pid = vfork();
531   switch (inc_pid)
532     {
533     case 0:
534       execv(prog, argv);
535       _exit(1);
536     case -1:
537       com_err(whoami, 0, "Failed to start incremental update");
538       break;
539     default:
540       inc_running = 1;
541       inc_started = now;
542     }
543   sigprocmask(SIG_UNBLOCK, &sigs, NULL);
544
545   free_argv(iu->before, iu->beforec);
546   free_argv(iu->after, iu->afterc);
547   free(iu);
548 }
549 #endif
550
551 /* Called when the current transaction is aborted to throw away any queued
552  * incremental updates
553  */
554
555 void incremental_flush(void)
556 {
557   struct iupdate *iu;
558
559   while (sq_get_data(incremental_sq, &iu))
560     {
561       free_argv(iu->before, iu->beforec);
562       free_argv(iu->after, iu->afterc);
563       free(iu);
564     }
565   sq_destroy(incremental_sq);
566   incremental_sq = sq_create();
567 }
568
569
570 char **copy_argv(char **argv, int argc)
571 {
572   char **ret = xmalloc(sizeof(char *) * argc);
573   while (--argc >= 0)
574     ret[argc] = xstrdup(strtrim(argv[argc]));
575   return ret;
576 }
577
578 void free_argv(char **argv, int argc)
579 {
580   while (--argc >= 0)
581     free(argv[argc]);
582   free(argv);
583 }
584
585 int table_num(char *name)
586 {
587   int i;
588
589   for (i = num_tables - 1; i; i--)
590     {
591       if (!strcmp(table_name[i], name))
592         break;
593     }
594
595   return i; /* 0 = "none" if no match */
596 }
This page took 0.267579 seconds and 5 git commands to generate.