]> andersk Git - moira.git/blobdiff - server/increment.qc
missing include of ctype.h
[moira.git] / server / increment.qc
index 7f682a9d14a3bfcfaf6288d15703089173bc3942..c7b3b720f1d218b23e8d92bf6f7e1e89b0cb284b 100644 (file)
@@ -14,11 +14,16 @@ static char *rcsid_qrtn_qc = "$Header$";
 #endif lint
 
 #include <mit-copyright.h>
-#include <sms.h>
+#include <moira.h>
 #include "query.h"
-#include "sms_server.h"
+#include "mr_server.h"
 
 extern char *whoami;
+char *malloc();
+
+int inc_pid = 0;
+int inc_running = 0;
+time_t inc_started;
 
 #define MAXARGC 15
 
@@ -40,13 +45,15 @@ static char *after[MAXARGC];
 static int afterc;
 
 /* structures to save entire sets of incremental changes */
-struct save_queue *incremental_sq;
+struct save_queue *incremental_sq = NULL;
+struct save_queue *incremental_exec = NULL;
 struct iupdate {
     char *table;
     int beforec;
     char **before;
     int afterc;
     char **after;
+    char *service;
 };
 
 
@@ -88,7 +95,10 @@ incremental_init()
     aarg12 = after[12];
     aarg13 = after[13];
     aarg14 = after[14];
-    incremental_sq = sq_create();
+    if (incremental_sq == NULL)
+      incremental_sq = sq_create();
+    if (incremental_exec == NULL)
+      incremental_exec = sq_create();
 }
 
 
@@ -98,7 +108,7 @@ incremental_init()
 char **argv;
 ##{
 ##  int id;
-    char buffer[512];
+    char buffer[512], *name;
 
     beforetable = table;
 
@@ -134,14 +144,15 @@ char **argv;
 ##               barg10 = fs.lockertype)
 ##       where qual
        beforec = 11;
-    } else if (!strcmp(table, "nfsquota")) {
-       strcpy(barg0, argv[0]);
+    } else if (!strcmp(table, "quota")) {
+       strcpy(barg0, "?");
        strcpy(barg1, argv[1]);
-       sprintf(buffer, "%s and filesys.filsys_id = nq.filsys_id", qual);
+       strcpy(barg2, "?");
+       sprintf(buffer, "%s and filesys.filsys_id = q.filsys_id", qual);
        qual = buffer;
-##     range of nq is nfsquota
-##     retrieve (barg2 = text(nq.quota), barg3 = filesys.name) where qual
-       beforec = 4;
+##     range of q is quota
+##     retrieve (barg3 = text(q.quota), barg4 = filesys.name) where qual
+       beforec = 5;
     } else if (!strcmp(table, "list")) {
 ##     retrieve (barg0 = l.name, barg1 = text(l.active),
 ##               barg2 = text(l.public), barg3 = text(l.hidden),
@@ -152,21 +163,24 @@ char **argv;
        beforec = 10;
     } else if (!strcmp(table, "members")) {
        id = (int) argv[0];
-##     repeat retrieve (barg0 = list.name) where list.list_id = @id
+##     retrieve (barg3 = text(list.group)) where list.list_id = id
+       name = malloc(0);
+       id_to_name(id, "LIST", &name);
+       strcpy(barg0, name);
        strcpy(barg1, argv[1]);
        id = (int) argv[2];
        if (!strcmp(barg1, "USER")) {
-##         repeat retrieve (barg2 = users.login) where users.users_id = @id
+           id_to_name(id, barg1, &name);
        } else if (!strcmp(barg1, "LIST")) {
-##         repeat retrieve (barg2 = list.name) where list.list_id = @id
+           id_to_name(id, barg1, &name);
        } else if (!strcmp(barg1, "STRING")) {
-##         repeat retrieve (barg2 = strings.string)
-##             where strings.string_id = @id
+           id_to_name(id, barg1, &name);
        } else if (!strcmp(barg1, "KERBEROS")) {
-##         repeat retrieve (barg2 = strings.string)
-##             where strings.string_id = @id
+           id_to_name(id, "STRING", &name);
        }
-       beforec = 3;
+       strcpy(barg2, name);
+       free(name);
+       beforec = 4;
     } /* else
       com_err(whoami, 0, "unknown table in incremental_before"); */
 ##}
@@ -189,7 +203,7 @@ incremental_clear_after()
 ##char *qual;
 char **argv;
 ##{
-    char buffer[2048];
+    char buffer[2048], *name;
 ##  int id, i;
     struct iupdate *iu;
     char **copy_argv();
@@ -227,14 +241,15 @@ char **argv;
 ##               aarg10 = fs.lockertype)
 ##       where qual
        afterc = 11;
-    } else if (!strcmp(table, "nfsquota")) {
-       strcpy(aarg0, argv[0]);
+    } else if (!strcmp(table, "quota")) {
+       strcpy(aarg0, "?");
        strcpy(aarg1, argv[1]);
-       sprintf(buffer, "%s and filesys.filsys_id = nq.filsys_id", qual);
+       strcpy(aarg2, "?");
+       sprintf(buffer, "%s and filesys.filsys_id = q.filsys_id", qual);
        qual = buffer;
-##     range of nq is nfsquota
-##     retrieve (aarg2 = text(nq.quota), aarg3 = filesys.name) where qual
-       afterc = 4;
+##     range of q is quota
+##     retrieve (aarg3 = text(q.quota), aarg4 = filesys.name) where qual
+       afterc = 5;
     } else if (!strcmp(table, "list")) {
 ##     retrieve (aarg0 = l.name, aarg1 = text(l.active),
 ##               aarg2 = text(l.public), aarg3 = text(l.hidden),
@@ -245,21 +260,24 @@ char **argv;
        afterc = 10;
     } else if (!strcmp(table, "members")) {
        id = (int) argv[0];
-##     repeat retrieve (aarg0 = list.name) where list.list_id = @id
+##     retrieve (aarg3 = text(list.group)) where list.list_id = id
+       name = malloc(0);
+       id_to_name(id, "LIST", &name);
+       strcpy(aarg0, name);
        strcpy(aarg1, argv[1]);
        id = (int) argv[2];
        if (!strcmp(aarg1, "USER")) {
-##         repeat retrieve (aarg2 = users.login) where users.users_id = @id
+           id_to_name(id, aarg1, &name);
        } else if (!strcmp(aarg1, "LIST")) {
-##         repeat retrieve (aarg2 = list.name) where list.list_id = @id
+           id_to_name(id, aarg1, &name);
        } else if (!strcmp(aarg1, "STRING")) {
-##         repeat retrieve (aarg2 = strings.string)
-##             where strings.string_id = @id
+           id_to_name(id, aarg1, &name);
        } else if (!strcmp(aarg1, "KERBEROS")) {
-##         repeat retrieve (aarg2 = strings.string)
-##             where strings.string_id = @id
+           id_to_name(id, "STRING", &name);
        }
-       afterc = 3;
+       strcpy(aarg2, name);
+       free(name);
+       afterc = 4;
     } else if (!strcmp(table, "clear")) {
        afterc = 0;
        table = beforetable;
@@ -267,7 +285,7 @@ char **argv;
       com_err(whoami, 0, "unknown table in incremental_after"); */
 
     iu = (struct iupdate *) malloc(sizeof(struct iupdate));
-    iu->table = table;
+    iu->table = strsave(table);
     iu->beforec = beforec;
     iu->before = copy_argv(before, beforec);
     iu->afterc = afterc;
@@ -300,62 +318,101 @@ char **argv;
 
 
 /* Called when the current transaction is committed to start any queued
- * incremental updates
+ * incremental updates.  This caches the update table the first time it
+ * is called.
  */
 
+struct inc_cache {
+    struct inc_cache *next;
+    char *table;
+    char *service;
+};
+
+
 incremental_update()
 {
+    static int inited = 0;
+    static struct inc_cache *cache;
+    struct inc_cache *c;
+##  char tab[17], serv[17];
     struct iupdate *iu;
-    char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3];
-    int i;
 
-    while (sq_get_data(incremental_sq, &iu)) {
-       argv[1] = iu->table;
-       sprintf(cbefore, "%d", iu->beforec);
-       argv[2] = cbefore;
-       sprintf(cafter, "%d", iu->afterc);
-       argv[3] = cafter;
-       for (i = 0; i < iu->beforec; i++)
-         argv[4 + i] = before[i];
-       for (i = 0; i < iu->afterc; i++)
-         argv[4 + iu->beforec + i] = after[i];
-       invoke_updates(argv, 4 + iu->beforec + iu->afterc);
-       free_argv(iu->before, iu->beforec);
-       free_argv(iu->after, iu->afterc);
-       free(iu);
+    if (!inited) {
+       inited++;
+
+##     retrieve (tab = incremental.table, serv = incremental.service) {
+           c = (struct inc_cache *)malloc(sizeof(struct inc_cache));
+           c->next = cache;
+           c->table = strsave(strtrim(tab));
+           c->service = strsave(strtrim(serv));
+           cache = c;
+##     }
     }
-    sq_destroy(incremental_sq);
-    incremental_sq = sq_create();
-}
 
+    while (sq_remove_data(incremental_sq, &iu)) {
+       for (c = cache; c; c = c->next) {
+           if (!strcmp(c->table, iu->table)) {
+               iu->service = c->service;
+               sq_save_data(incremental_exec, iu);
+           }
+       }
+    }
+    if (inc_running == 0)
+      next_incremental();
+}
 
-/* THIS IS WRONG.  We should keep a table of services getting incremental
- * updates.  But for now, just assume AFS
- */
 
-invoke_updates(argv, argc)
-char **argv;
-int argc;
+next_incremental()
 {
-    int pid, i;
-    char *prog;
+    struct iupdate *iu;
+    char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[BUFSIZ];
+    int i;
+
+    if (incremental_exec == NULL)
+      incremental_init();
+
+    if (sq_empty(incremental_exec) ||
+       (inc_running && now - inc_started < INC_TIMEOUT))
+      return;
+
+    if (inc_running)
+      com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
 
-    prog = "/u1/sms/bin/afs.incr";
+    sq_remove_data(incremental_exec, &iu);
+    argv[1] = iu->table;
+    sprintf(cbefore, "%d", iu->beforec);
+    argv[2] = cbefore;
+    sprintf(cafter, "%d", iu->afterc);
+    argv[3] = cafter;
+    for (i = 0; i < iu->beforec; i++)
+      argv[4 + i] = iu->before[i];
+    for (i = 0; i < iu->afterc; i++)
+      argv[4 + iu->beforec + i] = iu->after[i];
+
+    sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
+#ifdef DEBUG
+    com_err(whoami, 0, "forking %s", prog);
+#endif
     argv[0] = prog;
-    argv[argc] = 0;
-    pid = vfork();
-    switch (pid) {
+    argv[4 + iu->beforec + iu->afterc] = 0;
+    inc_pid = vfork();
+    switch (inc_pid) {
     case 0:
-       for (i = getdtablesize() - 1; i > 2; i--)
-         close(i);
        execv(prog, argv);
        exit(1);
     case -1:
        com_err(whoami, 0, "Failed to start incremental update");
-       return;
+       break;
     default:
-       return;
+       inc_running = 1;
+       inc_started = now;
     }
+
+    free_argv(iu->before, iu->beforec);
+    free_argv(iu->after, iu->afterc);
+    free(iu->table);
+    free(iu);
+
 }
 
 
@@ -370,6 +427,7 @@ incremental_flush()
     while (sq_get_data(incremental_sq, &iu)) {
        free_argv(iu->before, iu->beforec);
        free_argv(iu->after, iu->afterc);
+       free(iu->table);
        free(iu);
     }
     sq_destroy(incremental_sq);
This page took 0.079605 seconds and 4 git commands to generate.