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