]> andersk Git - moira.git/blob - gen/nfs.qc
improve locking; don't free some stuff in the interests of speed
[moira.git] / gen / nfs.qc
1 /* $Header$
2  *
3  * This generates the files necessary to load an nfs server.
4  *
5  *  (c) Copyright 1988 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 <stdio.h>
12 #include <sms.h>
13 #include <sms_app.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17
18 #define NFS_DIR "/u1/sms/dcm/nfs"
19
20 #define min(x,y)        ((x) < (y) ? (x) : (y))
21
22 char *whoami = "nfs.gen";
23 char *malloc(), *strsave();
24 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
25
26 main(argc, argv)
27 int argc;
28 char **argv;
29 {
30     char cmd[64];
31     struct stat sb;
32     int changed = 0;
33     int ingerr();
34
35     if (argc > 2) {
36         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
37         exit(SMS_ARGS);
38     }
39
40     IIseterr(ingerr);
41 ##  ingres sms
42 ##  set lockmode session where level = table
43
44     changed = do_nfs();
45
46 ##  exit
47
48     if (!changed) {
49         fprintf(stderr, "No files updated.\n");
50         if (argc == 2 && stat(argv[1], &sb) == 0)
51           exit(SMS_NO_CHANGE);
52     }
53
54     if (argc == 2) {
55         sprintf(cmd, "cd %s; cp /u1/sms/bin/install_* .; tar cf %s .", NFS_DIR, argv[1]);
56         if (system(cmd))
57           exit(SMS_TAR_FAIL);
58     }
59
60     exit(SMS_SUCCESS);
61 }
62
63
64 /*
65  * ingerr: (supposedly) called when Ingres indicates an error.
66  * I have not yet been able to get this to work to intercept a
67  * database open error.
68  */
69 #define INGRES_DEADLOCK 4700
70
71 static int ingerr(num)
72     int *num;
73 {
74     char buf[256];
75     int ingres_errno;
76
77     switch (*num) {
78     case INGRES_DEADLOCK:
79         ingres_errno = SMS_DEADLOCK;
80         break;
81     default:
82         ingres_errno = SMS_INGRES_ERR;
83     }
84     com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
85     critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
86     exit(ingres_errno);
87 }
88
89
90 /* Generate the files.  Returns zero if nothing changed, non-zero otherwise. */
91
92 int do_nfs()
93 ##{
94 ##  char machname[33], listname[33];
95     struct save_queue *machs, *lists;
96     int changed;
97
98     machs = sq_create();
99     lists = sq_create();
100 ##  range of s is serverhosts
101 ##  retrieve (machname = trim(machine.name), listname = trim(s.value3))
102 ##      where machine.mach_id = s.mach_id and s.enable != 0 {
103       sq_save_unique_string(machs, strsave(machname));
104       sq_save_unique_string(lists, strsave(listname));
105 ##  }
106
107     changed = do_lists(lists);
108     changed += do_machs(machs);
109     return(changed);
110 ##}
111
112
113 /* Make all of the credentials lists that will be needed.  Returns 0 if
114  * no files were actually changed */
115
116 int do_lists(lists)
117 struct save_queue *lists;
118 ##{
119     struct save_queue *sq, *members;
120     char file[64], *u;
121     struct hash *users, *do_everyone();
122     struct stat sb;
123     FILE *fd;
124 ##  char *listname, *lsname, lname[33], uname[9], *filetime;
125 ##  int uid, id, flag1, flag2, flag3, flag4;
126
127     sprintf(file, "%s/list-", NFS_DIR);
128     if (stat(file, &sb) == 0) {
129         filetime = ingres_date_and_time(sb.st_mtime);
130 ##      retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
131 ##              where tblstats.table = "users"
132 ##      retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
133 ##              where tblstats.table = "list"
134 ##      retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
135 ##              where tblstats.table = "members"
136 ##      retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
137 ##              where tblstats.table = "serverhosts"
138         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
139             fprintf(stderr, "The lists do not need to be rebuilt.\n");
140             return(0);
141         }
142     }
143
144 ##  begin transaction
145     /* get locks */
146 ##  retrieve (lname = list.modtime) where list.list_id = 0
147 ##  retrieve (lname = users.modtime) where users.users_id = 0
148
149     /* build the list of everyone, and store it in a file whose name
150      * corresponds to the empty list.
151      */
152     users = do_everyone();
153
154     fprintf(stderr, "Building specific lists\n");
155     /* now do each of the lists used by an NFS server */
156 ##  range of l is list
157 ##  range of l1 is list
158 ##  range of m is members
159 ##  range of u is users
160     while (sq_get_data(lists, &listname)) {
161         if (strlen(listname) == 0)
162           continue;
163         sprintf(file, "%s/list-%s", NFS_DIR, listname);
164         fd = fopen(file, "w");
165         if (!fd) {
166             fprintf(stderr, "cannot open %s for output\n", file);
167             exit(SMS_OCONFIG);
168         }
169         sq = sq_create();
170         sq_save_data(sq, listname);
171         members = sq_create();
172         while (sq_get_data(sq, &lsname)) {
173 ##          repeat retrieve (lname = trim(l.name))
174 ##              where m.list_id = l1.list_id and l1.name = @lsname and
175 ##                    m.member_type = "LIST" and m.member_id = l.list_id {
176               sq_save_unique_string(sq, strsave(lname));
177 ##          }
178 ##          repeat retrieve (id = m.member_id)
179 ##              where m.list_id = l1.list_id and l1.name = @lsname and
180 ##                    m.member_type = "USER" {
181               if (u = hash_lookup(users, id)) {
182                   sq_save_unique_data(members, u);
183               }
184 ##          }
185         }
186         while (sq_get_data(members, &lsname))
187           fprintf(fd, "%s\n", lsname);
188 /* test to see if this speeds things up; don't free anything!
189         sq_destroy(members);
190         sq_destroy(sq);
191  */     if (fclose(fd)) {
192             fprintf(stderr, "error closing %s\n", file);
193             exit(SMS_CCONFIG);
194         }
195     }
196 /* don't free here either
197     sq_destroy(lists);
198  */
199 ##  end transaction
200     return(1);
201 ##}
202
203
204 /*  Build the list of everybody. */
205 struct grp {
206     struct grp *next;
207     int id;
208 };
209 struct user {
210     char name[9];
211     int uid;
212     struct grp *lists;
213 };
214
215 struct hash *do_everyone()
216 ##{
217     char buf[BUFSIZ], *l;
218     struct hash *groups, *users;
219     struct user *u;
220     struct grp *g;
221     struct bucket *b, **p;
222 ##  char name[33];
223 ##  int gid, id, lid, maxid, uid;
224     FILE *fd;
225     int i;
226     struct save_queue *sq;
227
228     fprintf(stderr, "Building the list of everybody\n");
229     sprintf(buf, "%s/list-", NFS_DIR);
230     fd = fopen(buf, "w");
231     if (!fd) {
232         fprintf(stderr, "cannot open %s for output\n", buf);
233         exit(SMS_OCONFIG);
234     }
235
236     /* make space for group list */
237     groups = create_hash(15000);
238
239     /* retrieve simple groups */
240 ##  range of l is list
241 ##  retrieve (gid = l.#gid, lid = l.list_id)
242 ##      where l.group != 0 and l.active != 0 {
243       sprintf(buf, ":%d", gid);
244       hash_store(groups, lid, strsave(buf));
245 ##  }
246
247     /* get special cases: lists that aren't groups themselves but are
248      * members of groups. */
249     sq = sq_create();
250 ##  range of m is members
251 ##  retrieve (gid = list.#gid, lid = l.list_id)
252 ##      where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
253 ##              m.member_id = l.list_id and m.list_id = list.list_id and
254 ##              list.group != 0 {
255       if (l = hash_lookup(groups, lid)) {
256           sprintf(buf, "%s:%d", l, gid);
257           free(l);
258       } else {
259           sprintf(buf, ":%d", gid);
260       }
261       hash_store(groups, lid, strsave(buf));
262       sq_save_data(sq, lid);
263 ##  }
264
265     while (sq_get_data(sq, &id)) {
266 ##      repeat retrieve (lid = l.list_id) where l.active != 0 and
267 ##              m.member_type = "LIST" and m.member_id = l.list_id and
268 ##              m.list_id = @id {
269           if (l = hash_lookup(groups, lid)) {
270               sprintf(buf, "%s%s", l, hash_lookup(groups, id));
271               free(l);
272               l = strsave(buf);
273           } else {
274               l = hash_lookup(groups, id);
275           }
276           hash_store(groups, lid, l);
277           sq_save_unique_data(sq, lid);
278 ##      }
279     }
280     sq_destroy(sq);
281
282     /* now do grplists */
283     users = create_hash(10000);
284 ##  range of u is users
285 ##  retrieve (id = u.users_id, name = u.login, uid = u.#uid)
286 ##       where u.status = 1 {
287       u = (struct user *) malloc(sizeof(struct user));
288       strcpy(u->name, strtrim(name));
289       u->uid = uid;
290       u->lists = NULL;
291       hash_store(users, id, u);
292 ##  }
293
294 ##  repeat retrieve (lid = m.list_id, id = m.member_id)
295 ##      where m.member_type = "USER" {
296       if ((u = (struct user *) hash_lookup(users, id)) &&
297           (hash_lookup(groups, lid) != NULL)) {
298           g = (struct grp *) malloc(sizeof(struct grp));
299           g->next = u->lists;
300           u->lists = g;
301           g->id = lid;
302       }
303 ##  }
304
305     for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
306         for (b = *p; b; b = b->next) {
307             i = 0;
308             u = (struct user *)b->data;
309             sprintf(buf, "%s:%d", u->name, u->uid);
310             for (g = u->lists; g; g = g->next)
311               if (l = hash_lookup(groups, g->id)) {
312                   i++;
313                   strcat(buf, l);
314               }
315             /* should free stuff here... */
316             if (i > 1) {
317                 remove_duplicate_groups(buf);
318             }
319             b->data = strsave(buf);
320             fprintf(fd, "%s\n", buf);
321         }
322     }
323
324     fclose(fd);
325     free(groups);
326     return(users);
327 ##}
328
329 #define MAXGROUPS 256
330
331 remove_duplicate_groups(p)
332 register char *p;
333 {
334     register char *pp;
335     char *index();
336     register int *mp;
337     int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
338
339     bzero(memory, MAXGROUPS * sizeof(int));
340     p = index(p, ':');
341     p++;
342     while (p = index(p, ':')) {
343         i = atoi(++p);
344         pp = p;
345         for (mp = memory; *mp && mp < mg; mp++)
346           if (*mp == i) {
347               if (p = index(p, ':'))
348                 strcpy(pp, ++p);
349               else {
350                   *(--pp) = 0;
351               }
352               p = pp - 2;
353               break;
354           }
355         *mp = i;
356     }
357 }
358
359
360 /* Now do each of the servers, linking the credentials list file and 
361  * compiling the quota and dirs files.
362  */
363
364 int do_machs(machs)
365 struct save_queue *machs;
366 ##{
367 ##  char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
368 ##  char *filetime;
369 ##  int uid, quota, id, gid, flag1, flag2, flag3, flag4;
370     char file[64], f1[64], f2[64], *cp, *index();
371     int prevuid, quotasum;
372     FILE *fd;
373     struct stat sb;
374     struct save_queue *sq;
375
376 /*
377     sprintf(file, "%s/list-", NFS_DIR);
378     if (stat(file, &sb) == 0) {
379         filetime = ingres_date_and_time(sb.st_mtime);
380  #      retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
381  #              where tblstats.table = "serverhosts"
382  #      retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
383  #              where tblstats.table = "filesys"
384  #      retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
385  #              where tblstats.table = "nfsquota"
386  #      retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
387  #              where tblstats.table = "nfsphys"
388         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
389             fprintf(stderr, "The machine files do not need to be rebuilt.\n");
390             return(0);
391         }
392     }
393 */
394
395     fprintf(stderr, "Building machine files\n");
396
397 ##  begin transaction
398 ##  range of s is serverhosts
399 ##  range of m is machine
400 ##  range of n is nfsphys
401 ##  range of q is nfsquota
402 ##  range of f is filesys
403 ##  range of u is users
404 ##  range of l is list
405     while (sq_get_data(machs, &machname)) {
406 ##      repeat retrieve (listname = trim(s.value3))
407 ##          where s.mach_id = m.mach_id and m.name = @machname
408         strtrim(machname);
409         sprintf(f1, "%s/list-%s", NFS_DIR, listname);
410         sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
411         unlink(f2); /* ignore errors on this unlink */
412         if (link(f1, f2)) {
413             fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
414             exit(SMS_OCONFIG);
415         }
416         sq = sq_create();
417 ##      repeat retrieve (dev = trim(n.#device))
418 ##          where n.mach_id = m.mach_id and m.name = @machname {
419           sq_save_data(sq, strsave(dev));
420 ##      }
421         while (sq_get_data(sq, &device)) {
422 ##          repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
423 ##              m.#name = @machname and n.#device = @device
424             while (cp = index(device, '/')) *cp = '@';
425             sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
426             fd = fopen(file, "w");
427             if (!fd) {
428                 fprintf(stderr, "cannot open %s for output\n", file);
429                 exit(SMS_OCONFIG);
430             }
431             prevuid = -1;
432             quotasum = 0;
433 ##          repeat retrieve (uid = u.#uid, quota = q.#quota)
434 ##              where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
435 ##                    f.phys_id = @id and u.status != 0 sort by #uid {
436                 if (uid != prevuid) {
437                     if (quotasum)
438                       fprintf(fd, "%d %d\n", prevuid, quotasum);
439                     prevuid = uid;
440                     quotasum = quota;
441                 } else {
442                     quotasum += quota;
443                 }
444 ##          }
445             if (quotasum)
446               fprintf(fd, "%d %d\n", prevuid, quotasum);
447             if (fclose(fd)) {
448                 fprintf(stderr, "error closing %s", file);
449                 exit(SMS_CCONFIG);
450             }
451             sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
452             fd = fopen(file, "w");
453             if (!fd) {
454                 fprintf(stderr, "cannot open %s for output\n", file);
455                 exit(SMS_OCONFIG);
456             }
457 ##          repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
458 ##                           uid = u.#uid, gid = l.#gid)
459 ##              where f.phys_id = @id and f.owner = u.users_id and 
460 ##                    f.owners = l.list_id and f.createflg != 0 {
461               fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
462 ##          }
463             if (fclose(fd)) {
464                 fprintf(stderr, "error closing %s", file);
465                 exit(SMS_CCONFIG);
466             }
467         }
468         sq_destroy(sq);
469     }
470 ##  end transaction
471     return(1);
472 ##}
This page took 0.074459 seconds and 5 git commands to generate.