1c09a6d4555b8aac54fb282098b0db38b48e2b88
[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_int32 aserver, port;
50 {
51     register struct rx_connection *tc;
52     struct rx_securityClass *uvclass;
53
54     uvclass = rxnull_NewClientSecurityObject();
55     tc = rx_NewConnection(aserver, htons(port), TCSERVICE_ID, uvclass, 0);
56     return tc;
57 }
58
59
60 /* return host address in network byte order */
61 afs_int32
62 GetServer(aname)
63      char *aname;
64 {
65     register struct hostent *th;
66     afs_int32 addr;
67     char b1, b2, b3, b4;
68     register afs_int32 code;
69
70     code = sscanf(aname, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
71     if (code == 4) {
72         addr = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
73         return htonl(addr);     /* convert to network order (128 in byte 0) */
74     }
75     th = gethostbyname(aname);
76     if (!th)
77         return 0;
78     memcpy(&addr, th->h_addr, sizeof(addr));
79     return addr;
80 }
81
82
83 static int
84 PerformDump(register struct cmd_syndesc *as, void *arock)
85 {
86     struct rx_connection *aconn;
87     afs_int32 server;
88     FILE *fopen(), *fp;
89     struct tc_dumpDesc *ptr;
90     int i;
91     afs_int32 parentDumpID, dumpLevel;
92
93     server = GetServer(SERVERNAME);
94     if (!server) {
95         printf("cant get server id \n");
96         exit(1);
97     }
98     parentDumpID = 1;
99     dumpLevel = 1;
100     strcpy(tdumpSetName, "Test");
101     ttapeSet.id = 1;
102     ttapeSet.maxTapes = 10;
103     fp = fopen("dumpScr", "r");
104     fscanf(fp, "%u %u %u\n", &tdumps.tc_dumpArray_len, &ttapeSet.a,
105            &ttapeSet.b);
106     strcpy(ttapeSet.format, "tapeName%u");
107     strcpy(ttapeSet.tapeServer, "diskTapes");
108     tdumps.tc_dumpArray_val =
109         (struct tc_dumpDesc
110          *)(malloc(tdumps.tc_dumpArray_len * sizeof(struct tc_dumpDesc)));
111     ptr = tdumps.tc_dumpArray_val;
112     for (i = 0; i < tdumps.tc_dumpArray_len; i++) {
113         fscanf(fp, "%s\n", ptr->name);
114         fscanf(fp, "%s\n", ptr->hostAddr);
115         fscanf(fp, "%u %u %u\n", &ptr->vid, &ptr->partition, &ptr->date);
116         ptr++;
117     }
118
119     aconn = UV_Bind(server, TCPORT);
120     code =
121         TC_PerformDump(aconn, tdumpSetName, &ttapeSet, &tdumps, parentDumpID,
122                        dumpLevel, &tdumpID);
123     free(tdumps.tc_dumpArray_val);
124     if (code) {
125         printf("call to TC_PerformDump failed %u\n", code);
126         exit(1);
127     }
128     printf("dumpid returned %u\n", tdumpID);
129
130     return 0;
131 }
132
133 static int
134 PerformRestore(register struct cmd_syndesc *as, void *arock)
135 {
136     struct rx_connection *aconn;
137     afs_int32 server;
138     int i;
139     FILE *fopen(), *fp;
140     struct tc_restoreDesc *ptr;
141
142     server = GetServer(SERVERNAME);
143     if (!server) {
144         printf("cant get server id \n");
145         exit(1);
146     }
147     aconn = UV_Bind(server, TCPORT);
148     strcpy(tdumpSetName, "");
149     strcpy(tdumpSetName, "Test");
150     fp = fopen("restoreScr", "r");
151     fscanf(fp, "%u\n", &trestores.tc_restoreArray_len);
152     trestores.tc_restoreArray_val =
153         (struct tc_restoreDesc *)malloc(trestores.tc_restoreArray_len *
154                                         sizeof(struct tc_restoreDesc));
155     ptr = trestores.tc_restoreArray_val;
156     for (i = 0; i < trestores.tc_restoreArray_len; i++) {
157         fscanf(fp, "%s\n", ptr->oldName);
158         fscanf(fp, "%s\n", ptr->newName);
159         fscanf(fp, "%s\n", ptr->tapeName);
160         fscanf(fp, "%s\n", ptr->hostAddr);
161         fscanf(fp, "%u %u %u %u %d %u\n", &ptr->origVid, &ptr->vid,
162                &ptr->partition, &ptr->flags, &ptr->frag, &ptr->position);
163         ptr++;
164
165     }
166     code = TC_PerformRestore(aconn, tdumpSetName, &trestores, &tdumpID);
167     if (code) {
168         printf("call to TC_PerformRestore failed %u\n", code);
169         exit(1);
170     }
171     printf("dumpid returned %u\n", tdumpID);
172     return 0;
173 }
174
175 static int
176 CheckDump(register struct cmd_syndesc *as, void *arock)
177 {
178     struct rx_connection *aconn;
179     afs_int32 server;
180     server = GetServer(SERVERNAME);
181     if (!server) {
182         printf("cant get server id \n");
183         exit(1);
184     }
185     tdumpID = atol(as->parms[0].items->data);
186     aconn = UV_Bind(server, TCPORT);
187     code = TC_CheckDump(aconn, tdumpID, &tstatus);
188     if (code) {
189         printf("call to TC_CheckDump failed %u\n", code);
190         exit(1);
191     }
192     return 0;
193 }
194
195 static int
196 AbortDump(register struct cmd_syndesc *as, void *arock)
197 {
198     struct rx_connection *aconn;
199     afs_int32 server;
200     server = GetServer(SERVERNAME);
201     if (!server) {
202         printf("cant get server id \n");
203         exit(1);
204     }
205     tdumpID = atol(as->parms[0].items->data);
206     aconn = UV_Bind(server, TCPORT);
207     code = TC_AbortDump(aconn, tdumpID);
208     if (code) {
209         printf("call to TC_AbortDump failed %u\n", code);
210         exit(1);
211     }
212     return 0;
213 }
214
215 static int
216 WaitForDump(register struct cmd_syndesc *as, void *arock)
217 {
218     struct rx_connection *aconn;
219     afs_int32 server;
220     server = GetServer(SERVERNAME);
221     if (!server) {
222         printf("cant get server id \n");
223         exit(1);
224     }
225     tdumpID = atol(as->parms[0].items->data);
226     aconn = UV_Bind(server, TCPORT);
227     code = TC_WaitForDump(aconn, tdumpID);
228     if (code) {
229         printf("call to TC_WaitForDump failed %u\n", code);
230         exit(1);
231     }
232     return 0;
233 }
234
235 static int
236 EndDump(register struct cmd_syndesc *as, void *arock)
237 {
238     struct rx_connection *aconn;
239     afs_int32 server;
240     server = GetServer(SERVERNAME);
241     if (!server) {
242         printf("cant get server id \n");
243         exit(1);
244     }
245     tdumpID = atol(as->parms[0].items->data);
246     aconn = UV_Bind(server, TCPORT);
247     code = TC_EndDump(aconn, tdumpID);
248     if (code) {
249         printf("call to TC_EndDump failed %u\n", code);
250         exit(1);
251     }
252     return 0;
253 }
254
255 static int
256 MyBeforeProc(struct cmd_syndesc *as, void *arock)
257 {
258     afs_int32 code;
259
260     code = rx_Init(0);
261     if (code) {
262         printf("Could not initialize rx.\n");
263         return code;
264     }
265     rxInitDone = 1;
266     rx_SetRxDeadTime(50);
267     return 0;
268 }
269
270 #include "AFS_component_version_number.c"
271
272 main(argc, argv)
273      int argc;
274      char **argv;
275 {
276     register afs_int32 code;
277
278     register struct cmd_syndesc *ts;
279
280 #ifdef  AFS_AIX32_ENV
281     /*
282      * The following signal action for AIX is necessary so that in case of a 
283      * crash (i.e. core is generated) we can include the user's data section 
284      * in the core dump. Unfortunately, by default, only a partial core is
285      * generated which, in many cases, isn't too useful.
286      */
287     struct sigaction nsa;
288
289     sigemptyset(&nsa.sa_mask);
290     nsa.sa_handler = SIG_DFL;
291     nsa.sa_flags = SA_FULLDUMP;
292     sigaction(SIGABRT, &nsa, NULL);
293     sigaction(SIGSEGV, &nsa, NULL);
294 #endif
295     cmd_SetBeforeProc(MyBeforeProc, NULL);
296
297     ts = cmd_CreateSyntax("dump", PerformDump, NULL, "perform a dump");
298
299     ts = cmd_CreateSyntax("restore", PerformRestore, NULL, "perform a restore");
300
301     ts = cmd_CreateSyntax("check", CheckDump, NULL, "check a dump");
302     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "dump id");
303
304     ts = cmd_CreateSyntax("abort", AbortDump, NULL, "abort a dump");
305     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "dump id");
306
307     ts = cmd_CreateSyntax("wait", WaitForDump, NULL, "wait for a dump");
308     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "dump id");
309
310     ts = cmd_CreateSyntax("end", EndDump, NULL, "end a dump");
311     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "dump id");
312
313     code = cmd_Dispatch(argc, argv);
314     if (rxInitDone)
315         rx_Finalize();
316     exit(code);
317 }