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