]> andersk Git - moira.git/blob - gen/hosts.dc
Diane Delgado's changes for a fixed table-locking order
[moira.git] / gen / hosts.dc
1 /* $Header$
2  *
3  * This generates the hstath.txt hosttable.
4  *
5  *  (c) Copyright 1993 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 <moira.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 EXEC SQL INCLUDE sqlca;
17
18 extern int errno;
19 char *whoami = "hosts.gen";
20
21
22 main(argc, argv)
23 int argc;
24 char **argv;
25 {
26     FILE *out = stdout;
27     char *outf = NULL, outft[64], *p, buf[256];
28     struct stat sb;
29     struct timeval now;
30     int flag1, flag2, i;
31     struct hash *aliases;
32     EXEC SQL BEGIN DECLARE SECTION;
33     int id;
34     char name[65], vendor[33], model[33], os[33], addr[17];
35     EXEC SQL END DECLARE SECTION;
36
37 #ifsql INGRES
38     EXEC SQL CONNECT moira;
39     EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
40 #endsql
41 #ifsql INFORMIX
42     EXEC SQL DATABASE moira;
43 #endsql
44
45     if (argc == 2) {
46         if (stat(argv[1], &sb) == 0) {
47             if (ModDiff(&flag1, "machine", sb.st_mtime) ||
48                 ModDiff(&flag2, "subnet", sb.st_mtime))
49               exit(MR_DATE);
50             if (flag1 < 0 && flag2 < 0) {
51                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
52                         argv[1]);
53                 exit(MR_NO_CHANGE);
54             }
55         }
56         outf = argv[1];
57         sprintf(outft, "%s~", outf);
58         if ((out = fopen(outft, "w")) == NULL) {
59             fprintf(stderr, "unable to open %s for output\n", outf);
60             exit(MR_OCONFIG);
61         }
62     } else if (argc != 1) {
63         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
64         exit(MR_ARGS);
65     } else {
66         outf = NULL;
67     }
68
69     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
70
71     /* Acquire the required locks in the appropriate order */
72
73     EXEC SQL SELECT modtime INTO :name FROM machine WHERE mach_id = 0;
74     EXEC SQL SELECT modtime INTO :name FROM hostalias WHERE mach_id = 0;
75
76     gettimeofday(&now, NULL);
77     
78     fprintf(out, "; MIT Network Host Table\n;\n");
79     fprintf(out, "; \t%cAuthor: $\n", '$');
80     fprintf(out, "; \t%cDate: $\n", '$');
81     fprintf(out, "; \t%cRevision: $\n;\n", '$');
82     fprintf(out, "; Host table generated by Moira at %s;\n",
83             ctime(&now.tv_sec));
84
85     EXEC SQL DECLARE y CURSOR FOR SELECT
86       mach_id, name FROM hostalias;
87     EXEC SQL OPEN y;
88     aliases = create_hash(1001);
89     while (1) {
90         EXEC SQL FETCH y INTO :id, :name;
91         if (sqlca.sqlcode != 0) break;
92         if (id == 0) continue;
93         if (*strtrim(name) == 0) continue;
94         if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
95             fprintf(stderr, "Name %s not in MIT domain\n", name);
96             continue;
97         } else {
98             name[i-8] = 0;
99         }
100         if (p = hash_lookup(aliases, id)) {
101             sprintf(buf, "%s,%s", p, name);
102             hash_update(aliases, id, strsave(buf));
103             free(p);
104         } else
105           hash_store(aliases, id, strsave(name));
106     }
107
108     EXEC SQL DECLARE x CURSOR FOR SELECT 
109       name, mach_id, vendor, model, os, address
110       FROM machine WHERE status=1 ORDER BY address;
111     EXEC SQL OPEN x;
112     while (1) {
113         EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
114         if (sqlca.sqlcode != 0) break;
115         if (id == 0) continue;
116         if (*strtrim(name) == 0) continue;
117         if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
118             continue;
119         } else {
120             name[i-8] = 0;
121         }
122         strtrim(vendor);
123         strtrim(model);
124         strtrim(os);
125         strtrim(addr);
126         if (*addr == 0 || inet_addr(addr) == -1)
127           continue;
128         if (p = hash_lookup(aliases, id))
129           sprintf(buf, "%s,%s", name, p);
130         else
131           strcpy(buf, name);
132         fprintf(out, "HOST : %s : %s : ", addr, buf);
133         if ((*vendor || *model) && *os) {
134             if (*vendor && *model)
135               fprintf(out, "%s/%s : %s :\n", vendor, model, os);
136             else
137               fprintf(out, "%s%s : %s :\n", vendor, model, os);
138         } else
139           fputs("\n", out);
140     }
141
142     EXEC SQL CLOSE x;
143 #ifsql INGRES
144     EXEC SQL DISCONNECT;
145 #endsql
146 #ifsql INFORMIX
147     EXEC SQL CLOSE DATABASE;
148 #endsql
149
150     fprintf(out, "; End of automatically generated host table\n");
151     if (fclose(out)) {
152         perror("close failed");
153         exit(MR_CCONFIG);
154     }
155     if (outf)
156       fix_file(outf);
157     exit(MR_SUCCESS);
158
159  sqlerr:
160     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
161 #ifsql INGRES
162     if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000)
163       exit(MR_DEADLOCK);
164 #endsql
165     critical_alert("DCM", "Hosts build encountered INGRES ERROR %d",
166                    sqlca.sqlcode);
167     exit(MR_INGRES_ERR);
168 }
This page took 0.066713 seconds and 5 git commands to generate.