]> andersk Git - moira.git/blob - gen/hosts.dc
Fix allocation strategy in setup_ahst(); shouldn't be redefining the argv
[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     gettimeofday(&now, NULL);
72     fprintf(out, "; Host table generated %s;\n", ctime(&now.tv_sec));
73
74     EXEC SQL DECLARE y CURSOR FOR SELECT
75       mach_id, name FROM hostalias;
76     EXEC SQL OPEN y;
77     aliases = create_hash(1001);
78     while (1) {
79         EXEC SQL FETCH y INTO :id, :name;
80         if (sqlca.sqlcode != 0) break;
81         if (id == 0) continue;
82         if (*strtrim(name) == 0) continue;
83         if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
84             fprintf(stderr, "Name %s not in MIT domain\n", name);
85             continue;
86         } else {
87             name[i-8] = 0;
88         }
89         if (p = hash_lookup(aliases, id)) {
90             sprintf(buf, "%s,%s", p, name);
91             hash_update(aliases, id, strsave(buf));
92             free(p);
93         } else
94           hash_store(aliases, id, strsave(name));
95     }
96
97     EXEC SQL DECLARE x CURSOR FOR SELECT 
98       name, mach_id, vendor, model, os, address
99       FROM machine WHERE status=1 ORDER BY address;
100     EXEC SQL OPEN x;
101     while (1) {
102         EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
103         if (sqlca.sqlcode != 0) break;
104         if (id == 0) continue;
105         if (*strtrim(name) == 0) continue;
106         if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
107             fprintf(stderr, "Name %s not in MIT domain\n", name);
108             continue;
109         } else {
110             name[i-8] = 0;
111         }
112         strtrim(vendor);
113         strtrim(model);
114         strtrim(os);
115         strtrim(addr);
116         if (*addr == 0 || inet_addr(addr) == -1)
117           continue;
118         if (p = hash_lookup(aliases, id))
119           sprintf(buf, "%s,%s", name, p);
120         else
121           strcpy(buf, name);
122         fprintf(out, "HOST : %s : %s : ", addr, buf);
123         if ((*vendor || *model) && *os) {
124             if (*vendor && *model)
125               fprintf(out, "%s/%s : %s :\n", vendor, model, os);
126             else
127               fprintf(out, "%s%s : %s :\n", vendor, model, os);
128         } else
129           fputs("\n", out);
130     }
131
132     EXEC SQL CLOSE x;
133 #ifsql INGRES
134     EXEC SQL DISCONNECT;
135 #endsql
136 #ifsql INFORMIX
137     EXEC SQL CLOSE DATABASE;
138 #endsql
139
140     fprintf(out, "; End of automatically generated host table\n");
141     if (fclose(out)) {
142         perror("close failed");
143         exit(MR_CCONFIG);
144     }
145     if (outf)
146       fix_file(outf);
147     exit(MR_SUCCESS);
148
149  sqlerr:
150     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
151 #ifsql INGRES
152     if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000)
153       exit(MR_DEADLOCK);
154 #endsql
155     critical_alert("DCM", "Hosts build encountered INGRES ERROR %d",
156                    sqlca.sqlcode);
157     exit(MR_INGRES_ERR);
158 }
This page took 0.193533 seconds and 5 git commands to generate.