Use unsigned IP addresses in bu*
[openafs.git] / src / butc / test.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <sys/types.h>
11 #include <sys/file.h>
12 #include <sys/stat.h>
13 #ifdef AFS_AIX_ENV
14 #include <sys/statfs.h>
15 #endif
16 #include <netdb.h>
17 #include <sys/errno.h>
18 #include <lock.h>
19 #include <netinet/in.h>
20 #include <rx/xdr.h>
21 #include <rx/rx.h>
22 #include <rx/rx_globals.h>
23 #include <afs/nfs.h>
24 #include <afs/vlserver.h>
25 #include <afs/cellconfig.h>
26 #include <afs/keys.h>
27 #include <ubik.h>
28 #include <afs/afsint.h>
29 #include <afs/cmd.h>
30 #include <rx/rxkad.h>
31 #include <afs/tcdata.h>
32 #ifdef  AFS_AIX32_ENV
33 #include <signal.h>
34 #endif
35
36 #define SERVERNAME "server1"
37
38 afs_int32 code = 0;
39 struct tc_tapeSet ttapeSet;
40 char tdumpSetName[TC_MAXNAMELEN];
41 tc_dumpArray tdumps;            /*defined by rxgen */
42 tc_restoreArray trestores;      /*defined by rxgen */
43 afs_int32 tdumpID;
44 struct tc_dumpStat tstatus;
45 int rxInitDone = 0;
46
47 struct rx_connection *
48 UV_Bind(aserver, port)
49      afs_uint32 aserver;
50      afs_int32 port;
51 {
52     register struct rx_connection *tc;
53     struct rx_securityClass *uvclass;
54
55     uvclass = rxnull_NewClientSecurityObject();
56     tc = rx_NewConnection(aserver, htons(port), TCSERVICE_ID, uvclass, 0);
57     return tc;
58 }
59
60
61 /* return host address in network byte order */
62 afs_uint32
63 GetServer(aname)
64      char *aname;
65 {
66     register struct hostent *th;
67     afs_uint32 addr;
68     char b1, b2, b3, b4;
69     register afs_int32 code;
70
71     code = sscanf(aname, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
72     if (code == 4) {
73         addr = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
74         return htonl(addr);     /* convert to network order (128 in byte 0) */
75     }
76     th = gethostbyname(aname);
77     if (!th)
78         return 0;
79     memcpy(&addr, th->h_addr, sizeof(addr));
80     return addr;
81 }
82
83
84 static int
85 PerformDump(register struct cmd_syndesc *as, void *arock)
86 {
87     struct rx_connection *aconn;
88     afs_uint32 server;
89     FILE *fopen(), *fp;
90     struct tc_dumpDesc *ptr;
91     int i;
92     afs_int32 parentDumpID, dumpLevel;
93
94     server = GetServer(SERVERNAME);
95     if (!server) {
96         printf("cant get server id \n");
97         exit(1);
98     }
99     parentDumpID = 1;
100     dumpLevel = 1;
101     strcpy(tdumpSetName, "Test");
102     ttapeSet.id = 1;
103     ttapeSet.maxTapes = 10;
104     fp = fopen("dumpScr", "r");
105     fscanf(fp, "%u %u %u\n", &tdumps.tc_dumpArray_len, &ttapeSet.a,
106            &ttapeSet.b);
107     strcpy(ttapeSet.format, "tapeName%u");
108     strcpy(ttapeSet.tapeServer, "diskTapes");
109     tdumps.tc_dumpArray_val =
110         (struct tc_dumpDesc
111          *)(malloc(tdumps.tc_dumpArray_len * sizeof(struct tc_dumpDesc)));
112     ptr = tdumps.tc_dumpArray_val;
113     for (i = 0; i < tdumps.tc_dumpArray_len; i++) {
114         fscanf(fp, "%s\n", ptr->name);
115         fscanf(fp, "%s\n", ptr->hostAddr);
116         fscanf(fp, "%u %u %u\n", &ptr->vid, &ptr->partition, &ptr->date);
117         ptr++;
118     }
119
120     aconn = UV_Bind(server, TCPORT);
121     code =
122         TC_PerformDump(aconn, tdumpSetName, &ttapeSet, &tdumps, parentDumpID,
123                        dumpLevel, &tdumpID);
124     free(tdumps.tc_dumpArray_val);
125     if (code) {
126         printf("call to TC_PerformDump failed %u\n", code);
127         exit(1);
128     }
129     printf("dumpid returned %u\n", tdumpID);
130
131     return 0;
132 }
133
134 static int
135 PerformRestore(register struct cmd_syndesc *as, void *arock)
136 {
137     struct rx_connection *aconn;
138     afs_uint32 server;
139     int i;
140     FILE *fopen(), *fp;
141     struct tc_restoreDesc *ptr;
142
143     server = GetServer(SERVERNAME);
144     if (!server) {
145         printf("cant get server id \n");
146         exit(1);
147     }
148     aconn = UV_Bind(server, TCPORT);
149     strcpy(tdumpSetName, "");
150     strcpy(tdumpSetName, "Test");
151     fp = fopen("restoreScr", "r");
152     fscanf(fp, "%u\n", &trestores.tc_restoreArray_len);
153     trestores.tc_restoreArray_val =
154         (struct tc_restoreDesc *)malloc(trestores.tc_restoreArray_len *
155                                         sizeof(struct tc_restoreDesc));
156     ptr = trestores.tc_restoreArray_val;
157     for (i = 0; i < trestores.tc_restoreArray_len; i++) {
158         fscanf(fp, "%s\n", ptr->oldName);
159         fscanf(fp, "%s\n", ptr->newName);
160         fscanf(fp, "%s\n", ptr->tapeName);
161         fscanf(fp, "%s\n", ptr->hostAddr);
162         fscanf(fp, "%u %u %u %u %d %u\n", &ptr->origVid, &ptr->vid,
163                &ptr->partition, &ptr->flags, &ptr->frag, &ptr->position);
164         ptr++;
165
166     }
167     code = TC_PerformRestore(aconn, tdumpSetName, &trestores, &tdumpID);
168     if (code) {
169         printf("call to TC_PerformRestore failed %u\n", code);
170         exit(1);
171     }
172     printf("dumpid returned %u\n", tdumpID);
173     return 0;
174 }
175
176 static int
177 CheckDump(register struct cmd_syndesc *as, void *arock)
178 {
179     struct rx_connection *aconn;
180     afs_uint32 server;
181     server = GetServer(SERVERNAME);
182     if (!server) {
183         printf("cant get server id \n");
184         exit(1);
185     }
186     tdumpID = atol(as->parms[0].items->data);
187     aconn = UV_Bind(server, TCPORT);
188     code = TC_CheckDump(aconn, tdumpID, &tstatus);
189     if (code) {
190         printf("call to TC_CheckDump failed %u\n", code);
191         exit(1);
192     }
193     return 0;
194 }
195
196 static int
197 AbortDump(register struct cmd_syndesc *as, void *arock)
198 {
199     struct rx_connection *aconn;
200     afs_uint32 server;
201     server = GetServer(SERVERNAME);
202     if (!server) {
203         printf("cant get server id \n");
204         exit(1);
205     }
206     tdumpID = atol(as->parms[0].items->data);
207     aconn = UV_Bind(server, TCPORT);
208     code = TC_AbortDump(aconn, tdumpID);
209     if (code) {
210         printf("call to TC_AbortDump failed %u\n", code);
211         exit(1);
212     }
213     return 0;
214 }
215
216 static int
217 WaitForDump(register struct cmd_syndesc *as, void *arock)
218 {
219     struct rx_connection *aconn;
220     afs_uint32 server;
221     server = GetServer(SERVERNAME);
222     if (!server) {
223         printf("cant get server id \n");
224         exit(1);
225     }
226     tdumpID = atol(as->parms[0].items->data);
227     aconn = UV_Bind(server, TCPORT);
228     code = TC_WaitForDump(aconn, tdumpID);
229     if (code) {
230         printf("call to TC_WaitForDump failed %u\n", code);
231         exit(1);
232     }
233     return 0;
234 }
235
236 static int
237 EndDump(register struct cmd_syndesc *as, void *arock)
238 {
239     struct rx_connection *aconn;
240     afs_uint32 server;
241     server = GetServer(SERVERNAME);
242     if (!server) {
243         printf("cant get server id \n");
244         exit(1);
245     }
246     tdumpID = atol(as->parms[0].items->data);
247     aconn = UV_Bind(server, TCPORT);
248     code = TC_EndDump(aconn, tdumpID);
249     if (code) {
250         printf("call to TC_EndDump failed %u\n", code);
251         exit(1);
252     }
253     return 0;
254 }
255
256 static int
257 MyBeforeProc(struct cmd_syndesc *as, void *arock)
258 {
259     afs_int32 code;
260
261     code = rx_Init(0);
262     if (code) {
263         printf("Could not initialize rx.\n");
264         return code;
265     }
266     rxInitDone = 1;
267     rx_SetRxDeadTime(50);
268     return 0;
269 }
270
271 #include "AFS_component_version_number.c"
272
273 main(argc, argv)
274      int argc;
275      char **argv;
276 {
277     register afs_int32 code;
278
279     register struct cmd_syndesc *ts;
280
281 #ifdef  AFS_AIX32_ENV
282     /*
283      * The following signal action for AIX is necessary so that in case of a 
284      * crash (i.e. core is generated) we can include the user's data section 
285      * in the core dump. Unfortunately, by default, only a partial core is
286      * generated which, in many cases, isn't too useful.
287      */
288     struct sigaction nsa;
289
290     sigemptyset(&nsa.sa_mask);
291     nsa.sa_handler = SIG_DFL;
292     nsa.sa_flags = SA_FULLDUMP;
293     sigaction(SIGABRT, &nsa, NULL);
294     sigaction(SIGSEGV, &nsa, NULL);
295 #endif
296     cmd_SetBeforeProc(MyBeforeProc, NULL);
297
298     ts = cmd_CreateSyntax("dump", PerformDump, NULL, "perform a dump");
299
300     ts = cmd_CreateSyntax("restore", PerformRestore, NULL, "perform a restore");
301
302     ts = cmd_CreateSyntax("check", CheckDump, NULL, "check a dump");
303     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "dump id");
304
305     ts = cmd_CreateSyntax("abort", AbortDump, NULL, "abort a dump");
306     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "dump id");
307
308     ts = cmd_CreateSyntax("wait", WaitForDump, NULL, "wait for a dump");
309     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "dump id");
310
311     ts = cmd_CreateSyntax("end", EndDump, NULL, "end a dump");
312     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "dump id");
313
314     code = cmd_Dispatch(argc, argv);
315     if (rxInitDone)
316         rx_Finalize();
317     exit(code);
318 }