]> andersk Git - moira.git/blobdiff - gen/nfs.qc
make sure we sum up duplicate quotas correctly
[moira.git] / gen / nfs.qc
index b910205e02502a2bed46cd847c6abe1416ea5b31..324827fc3177911e5cac6451cdaa72f2a0cf30eb 100644 (file)
@@ -1,8 +1,13 @@
 /* $Header$
  *
  * This generates the files necessary to load an nfs server.
+ *
+ *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
+ *  For copying and distribution information, please see the file
+ *  <mit-copyright.h>.
  */
 
+#include <mit-copyright.h>
 #include <stdio.h>
 #include <sms.h>
 #include <sms_app.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 
-#define NFS_DIR        "/u1/sms/dcm/nfs"
 
 #define min(x,y)       ((x) < (y) ? (x) : (y))
 
-struct np {
-    char mach[33];
-    char dev[33];
-    char list[33];
-};
-
-struct fs {
-    int nfsphys;
-    char *dir;
-    int owner;
-    int owners;
-    struct qt *quota;
-};
-
-
+char *whoami = "nfs.gen";
 char *malloc(), *strsave();
 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
+char nfs_dir[64];
 
 main(argc, argv)
 int argc;
@@ -39,14 +30,19 @@ char **argv;
     char cmd[64];
     struct stat sb;
     int changed = 0;
+    int ingerr();
 
     if (argc > 2) {
        fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
        exit(SMS_ARGS);
     }
 
+    IIseterr(ingerr);
+    initialize_sms_error_table();
+    sprintf(nfs_dir, "%s/nfs", DCM_DIR);
+
 ##  ingres sms
-##  set lockmode session where readlock = nolock
+##  set lockmode session where level = table
 
     changed = do_nfs();
 
@@ -59,7 +55,8 @@ char **argv;
     }
 
     if (argc == 2) {
-       sprintf(cmd, "cd %s; tar cf %s .", NFS_DIR, argv[1]);
+       sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
+               nfs_dir, SMS_DIR, argv[1]);
        if (system(cmd))
          exit(SMS_TAR_FAIL);
     }
@@ -68,6 +65,32 @@ char **argv;
 }
 
 
+/*
+ * ingerr: (supposedly) called when Ingres indicates an error.
+ * I have not yet been able to get this to work to intercept a
+ * database open error.
+ */
+#define INGRES_DEADLOCK 4700
+
+static int ingerr(num)
+    int *num;
+{
+    char buf[256];
+    int ingres_errno;
+
+    switch (*num) {
+    case INGRES_DEADLOCK:
+       ingres_errno = SMS_DEADLOCK;
+       break;
+    default:
+       ingres_errno = SMS_INGRES_ERR;
+    }
+    com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
+    critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
+    exit(ingres_errno);
+}
+
+
 /* Generate the files.  Returns zero if nothing changed, non-zero otherwise. */
 
 int do_nfs()
@@ -97,7 +120,6 @@ int do_nfs()
 int do_lists(lists)
 struct save_queue *lists;
 ##{
-    struct save_queue *sq;
     char file[64], *u;
     struct hash *users, *do_everyone();
     struct stat sb;
@@ -105,7 +127,7 @@ struct save_queue *lists;
 ##  char *listname, *lsname, lname[33], uname[9], *filetime;
 ##  int uid, id, flag1, flag2, flag3, flag4;
 
-    sprintf(file, "%s/list-", NFS_DIR);
+    sprintf(file, "%s/list-", nfs_dir);
     if (stat(file, &sb) == 0) {
        filetime = ingres_date_and_time(sb.st_mtime);
 ##     retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
@@ -113,7 +135,7 @@ struct save_queue *lists;
 ##     retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
 ##             where tblstats.table = "list"
 ##     retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
-##             where tblstats.table = "members"
+##             where tblstats.table = "imembers"
 ##     retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
 ##             where tblstats.table = "serverhosts"
        if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
@@ -122,6 +144,11 @@ struct save_queue *lists;
        }
     }
 
+##  begin transaction
+    /* get locks */
+##  retrieve (lname = list.modtime) where list.list_id = 0
+##  retrieve (lname = users.modtime) where users.users_id = 0
+
     /* build the list of everyone, and store it in a file whose name
      * corresponds to the empty list.
      */
@@ -131,39 +158,32 @@ struct save_queue *lists;
     /* now do each of the lists used by an NFS server */
 ##  range of l is list
 ##  range of l1 is list
-##  range of m is members
+##  range of m is imembers
 ##  range of u is users
     while (sq_get_data(lists, &listname)) {
        if (strlen(listname) == 0)
          continue;
-       sprintf(file, "%s/list-%s", NFS_DIR, listname);
+       sprintf(file, "%s/list-%s", nfs_dir, listname);
        fd = fopen(file, "w");
        if (!fd) {
            fprintf(stderr, "cannot open %s for output\n", file);
            exit(SMS_OCONFIG);
        }
-       sq = sq_create();
-       sq_save_data(sq, listname);
-       while (sq_get_data(sq, &lsname)) {
-##         repeat retrieve (lname = trim(l.name))
-##             where m.list_id = l1.list_id and l1.name = @lsname and
-##                   m.member_type = "LIST" and m.member_id = l.list_id {
-             sq_save_unique_string(sq, lname);
-##         }
-##         repeat retrieve (id = m.member_id)
-##             where m.list_id = l1.list_id and l1.name = @lsname and
-##                   m.member_type = "USER" {
-             if (u = hash_lookup(users, id)) 
-               fprintf(fd, "%s\n", u);
-##         }
-       }
-       sq_destroy(sq);
+##     repeat retrieve (id = m.member_id)
+##         where m.list_id = l1.list_id and l1.name = @listname and
+##         m.member_type = "USER" {
+           if (u = hash_lookup(users, id))
+             fprintf(fd, "%s\n", u);
+##     }
        if (fclose(fd)) {
            fprintf(stderr, "error closing %s\n", file);
            exit(SMS_CCONFIG);
        }
     }
+/* don't free here either
     sq_destroy(lists);
+ */
+##  end transaction
     return(1);
 ##}
 
@@ -189,11 +209,11 @@ struct hash *do_everyone()
 ##  char name[33];
 ##  int gid, id, lid, maxid, uid;
     FILE *fd;
-    int first;
+    int i;
     struct save_queue *sq;
 
     fprintf(stderr, "Building the list of everybody\n");
-    sprintf(buf, "%s/list-", NFS_DIR);
+    sprintf(buf, "%s/list-", nfs_dir);
     fd = fopen(buf, "w");
     if (!fd) {
        fprintf(stderr, "cannot open %s for output\n", buf);
@@ -211,39 +231,6 @@ struct hash *do_everyone()
       hash_store(groups, lid, strsave(buf));
 ##  }
 
-    /* get special cases: lists that aren't groups themselves but are
-     * members of groups. */
-    sq = sq_create();
-##  range of m is members
-##  retrieve (gid = list.#gid, lid = l.list_id)
-##     where l.group = 0 and l.active != 0 and m.member_type = "LIST" and
-##             m.member_id = l.list_id and m.list_id = list.list_id and
-##             list.group != 0 {
-      if (l = hash_lookup(groups, lid)) {
-         sprintf(buf, "%s:%d", l, gid);
-         free(l);
-      } else {
-         sprintf(buf, ":%d", gid);
-      }
-      hash_store(groups, lid, strsave(buf));
-      sq_save_data(sq, lid);
-##  }
-
-    while (sq_get_data(sq, &id)) {
-##     repeat retrieve (lid = l.list_id, gid = l.#gid)
-##         where l.group = 0 and l.active != 0 and m.member_type = "LIST" and
-##             m.member_id = l.list_id and m.list_id = @id {
-         if (l = hash_lookup(groups, lid)) {
-             sprintf(buf, "%s%s", l, hash_lookup(groups, id));
-             free(l);
-         } else {
-             strcpy(buf, hash_lookup(groups, id));
-         }
-         sq_save_unique_data(sq, lid);
-##      }
-    }
-    sq_destroy(sq);
-
     /* now do grplists */
     users = create_hash(10000);
 ##  range of u is users
@@ -256,9 +243,10 @@ struct hash *do_everyone()
       hash_store(users, id, u);
 ##  }
 
-##  repeat retrieve (lid = m.list_id, id = m.member_id)
-##     where m.member_type = "USER" {
-      if (u = (struct user *) hash_lookup(users, id)) {
+##  range of m is imembers
+##  retrieve (lid = m.list_id, id = m.member_id) where m.member_type = "USER" {
+      if ((u = (struct user *) hash_lookup(users, id)) &&
+         (hash_lookup(groups, lid) != NULL)) {
          g = (struct grp *) malloc(sizeof(struct grp));
          g->next = u->lists;
          u->lists = g;
@@ -268,12 +256,18 @@ struct hash *do_everyone()
 
     for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
        for (b = *p; b; b = b->next) {
+           i = 0;
            u = (struct user *)b->data;
            sprintf(buf, "%s:%d", u->name, u->uid);
            for (g = u->lists; g; g = g->next)
-             if (l = hash_lookup(groups, g->id))
-               strcat(buf, l);
-           /* should free stuff here... */
+             if (l = hash_lookup(groups, g->id)) {
+                 i++;
+                 strcat(buf, l);
+             }
+           /* should free stuff here...
+           if (i > 1) {
+               remove_duplicate_groups(buf);
+           } */
            b->data = strsave(buf);
            fprintf(fd, "%s\n", buf);
        }
@@ -284,6 +278,36 @@ struct hash *do_everyone()
     return(users);
 ##}
 
+#define MAXGROUPS 256
+
+remove_duplicate_groups(p)
+register char *p;
+{
+    register char *pp;
+    char *index();
+    register int *mp;
+    int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
+
+    bzero(memory, MAXGROUPS * sizeof(int));
+    p = index(p, ':');
+    p++;
+    while (p = index(p, ':')) {
+       i = atoi(++p);
+       pp = p;
+       for (mp = memory; *mp && mp < mg; mp++)
+         if (*mp == i) {
+             if (p = index(p, ':'))
+               strcpy(pp, ++p);
+             else {
+                 *(--pp) = 0;
+             }
+             p = pp - 2;
+             break;
+         }
+       *mp = i;
+    }
+}
+
 
 /* Now do each of the servers, linking the credentials list file and 
  * compiling the quota and dirs files.
@@ -302,7 +326,7 @@ struct save_queue *machs;
     struct save_queue *sq;
 
 /*
-    sprintf(file, "%s/list-", NFS_DIR);
+    sprintf(file, "%s/list-", nfs_dir);
     if (stat(file, &sb) == 0) {
        filetime = ingres_date_and_time(sb.st_mtime);
  #     retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
@@ -329,12 +353,20 @@ struct save_queue *machs;
 ##  range of f is filesys
 ##  range of u is users
 ##  range of l is list
+
+/* acquire locks on users, lists, machines & filesystems */
+##  begin transaction
+##  retrieve (listname = u.modtime) where u.users_id = 0
+##  retrieve (listname = l.modtime) where l.list_id = 0
+##  retrieve (listname = m.modtime) where m.mach_id = 0
+##  retrieve (listname = f.modtime) where f.filsys_id = 0
+
     while (sq_get_data(machs, &machname)) {
 ##     repeat retrieve (listname = trim(s.value3))
 ##         where s.mach_id = m.mach_id and m.name = @machname
        strtrim(machname);
-       sprintf(f1, "%s/list-%s", NFS_DIR, listname);
-       sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
+       sprintf(f1, "%s/list-%s", nfs_dir, listname);
+       sprintf(f2, "%s/%s.cred", nfs_dir, machname);
        unlink(f2); /* ignore errors on this unlink */
        if (link(f1, f2)) {
            fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
@@ -349,7 +381,7 @@ struct save_queue *machs;
 ##         repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
 ##             m.#name = @machname and n.#device = @device
            while (cp = index(device, '/')) *cp = '@';
-           sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
+           sprintf(file, "%s/%s.%s.quotas", nfs_dir, machname, device);
            fd = fopen(file, "w");
            if (!fd) {
                fprintf(stderr, "cannot open %s for output\n", file);
@@ -357,9 +389,9 @@ struct save_queue *machs;
            }
            prevuid = -1;
            quotasum = 0;
-##         repeat retrieve (uid = u.#uid, quota = q.#quota)
-##             where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
-##                   f.phys_id = @id and u.status != 0 sort by #uid {
+##         repeat retrieve (uid = u.#uid, quota = q.#quota, flag1=q.filsys_id)
+##             where q.users_id = u.users_id and q.phys_id = @id and
+##                   u.status != 0 sort by #uid {
                if (uid != prevuid) {
                    if (quotasum)
                      fprintf(fd, "%d %d\n", prevuid, quotasum);
@@ -375,7 +407,7 @@ struct save_queue *machs;
                fprintf(stderr, "error closing %s", file);
                exit(SMS_CCONFIG);
            }
-           sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
+           sprintf(file, "%s/%s.%s.dirs", nfs_dir, machname, device);
            fd = fopen(file, "w");
            if (!fd) {
                fprintf(stderr, "cannot open %s for output\n", file);
@@ -394,5 +426,6 @@ struct save_queue *machs;
        }
        sq_destroy(sq);
     }
+##  end transaction
     return(1);
 ##}
This page took 0.046662 seconds and 4 git commands to generate.