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