2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* Test driver for configuration functions. */
12 #include <afsconfig.h>
13 #include <afs/param.h>
20 #include <afs/afs_Admin.h>
21 #include <afs/afs_utilAdmin.h>
22 #include <afs/afs_clientAdmin.h>
23 #include <afs/afs_cfgAdmin.h>
25 #include <afs/cellconfig.h>
29 /* define some globals */
30 static char myCellName
[MAXCELLCHARS
];
31 static void *myTokenHandle
;
32 static void *myCellHandle
;
36 /* ------------------- Utility functions ---------------------- */
39 GetErrorText(afs_status_t errorCode
)
42 const char *errorCodeText
;
43 static const char *failedLookupText
= "unable to translate error code";
45 if (util_AdminErrorCodeTranslate(errorCode
, 0, &errorCodeText
, &st
)) {
48 return failedLookupText
;
53 /* ------------------- CellServDB functions ------------------------ */
55 #define CSDBCallBackId 42
56 static pthread_mutex_t CSDBCallBackMutex
;
57 static pthread_cond_t CSDBCallBackCond
;
58 static int CSDBCallBackDone
;
61 CellServDbCallBack(void *callBackId
, cfg_cellServDbStatus_t
* statusItemP
,
64 if (statusItemP
!= NULL
) {
65 printf("Updated %s with result '%s' (tid = %u)\n",
66 statusItemP
->fsDbHost
, GetErrorText(statusItemP
->status
),
67 (unsigned)pthread_self());
69 printf("Update termination (tid = %u)\n", (unsigned)pthread_self());
71 (void)pthread_mutex_lock(&CSDBCallBackMutex
);
73 (void)pthread_mutex_unlock(&CSDBCallBackMutex
);
74 (void)pthread_cond_signal(&CSDBCallBackCond
);
77 if (callBackId
!= (void *)CSDBCallBackId
) {
78 printf("Update call back ID invalid (tid = %u)\n",
79 (unsigned)pthread_self());
85 DoCellServDbAddHost(struct cmd_syndesc
*as
, void *arock
)
90 char *cfgHost
= as
->parms
[0].items
->data
;
93 if (as
->parms
[1].items
) {
94 sysHost
= as
->parms
[1].items
->data
;
97 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
98 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
100 CSDBCallBackDone
= 0;
102 if (!cfg_CellServDbAddHost
103 (hostHandle
, sysHost
, CellServDbCallBack
, (void *)CSDBCallBackId
,
105 printf("cfg_CellServDbAddHost failed (%s)\n", GetErrorText(st
));
106 CSDBCallBackDone
= 1;
109 printf("cfg_CellServDbAddHost succeeded (maxUpdates = %d)\n",
113 (void)pthread_mutex_lock(&CSDBCallBackMutex
);
115 while (!CSDBCallBackDone
) {
116 (void)pthread_cond_wait(&CSDBCallBackCond
, &CSDBCallBackMutex
);
119 (void)pthread_mutex_unlock(&CSDBCallBackMutex
);
121 if (!cfg_HostClose(hostHandle
, &st
)) {
122 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
125 return (st
== 0 ? 0 : 1);
130 DoCellServDbRemoveHost(struct cmd_syndesc
*as
, void *arock
)
135 char *cfgHost
= as
->parms
[0].items
->data
;
136 char *sysHost
= NULL
;
138 if (as
->parms
[1].items
) {
139 sysHost
= as
->parms
[1].items
->data
;
142 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
143 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
145 CSDBCallBackDone
= 0;
147 if (!cfg_CellServDbRemoveHost
148 (hostHandle
, sysHost
, CellServDbCallBack
, (void *)CSDBCallBackId
,
150 printf("cfg_CellServDbRemoveHost failed (%s)\n",
152 CSDBCallBackDone
= 1;
155 printf("cfg_CellServDbRemoveHost succeeded (maxUpdates = %d)\n",
159 (void)pthread_mutex_lock(&CSDBCallBackMutex
);
161 while (!CSDBCallBackDone
) {
162 (void)pthread_cond_wait(&CSDBCallBackCond
, &CSDBCallBackMutex
);
165 (void)pthread_mutex_unlock(&CSDBCallBackMutex
);
167 if (!cfg_HostClose(hostHandle
, &st
)) {
168 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
171 return (st
== 0 ? 0 : 1);
176 DoCellServDbEnumerate(struct cmd_syndesc
*as
, void *arock
)
179 char *fsDbHost
= as
->parms
[0].items
->data
;
183 if (!cfg_CellServDbEnumerate(fsDbHost
, &cellName
, &cellDbHosts
, &st
)) {
184 printf("cfg_CellServDbEnumerate failed (%s)\n", GetErrorText(st
));
188 printf("%s is in cell %s\n", fsDbHost
, cellName
);
190 for (mstrp
= cellDbHosts
; *mstrp
!= '\0'; mstrp
+= strlen(mstrp
) + 1) {
191 printf("\t%s\n", mstrp
);
194 if (!cfg_StringDeallocate(cellName
, &st
)) {
195 printf("cfg_StringDeallocate failed (%s)\n", GetErrorText(st
));
198 if (!cfg_StringDeallocate(cellDbHosts
, &st
)) {
199 printf("cfg_StringDeallocate failed (%s)\n", GetErrorText(st
));
202 return (st
== 0 ? 0 : 1);
207 SetupCellServDbCmd(void)
209 struct cmd_syndesc
*ts
;
211 ts
= cmd_CreateSyntax("CellServDbAddHost", DoCellServDbAddHost
, NULL
, 0,
212 "add configuration target to server CellServDB");
213 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
214 "configuration host");
215 cmd_AddParm(ts
, "-syshost", CMD_SINGLE
, CMD_OPTIONAL
,
216 "system control host");
218 ts
= cmd_CreateSyntax("CellServDbRemoveHost", DoCellServDbRemoveHost
, NULL
, 0,
219 "remove configuration target from server CellServDB");
220 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
221 "configuration host");
222 cmd_AddParm(ts
, "-syshost", CMD_SINGLE
, CMD_OPTIONAL
,
223 "system control host");
225 ts
= cmd_CreateSyntax("CellServDbEnumerate", DoCellServDbEnumerate
, NULL
, 0,
226 "enumerate server CellServDB from specified host");
227 cmd_AddParm(ts
, "-host", CMD_SINGLE
, CMD_REQUIRED
, "host name");
230 (void)pthread_mutex_init(&CSDBCallBackMutex
, NULL
);
231 (void)pthread_cond_init(&CSDBCallBackCond
, NULL
);
236 /* ------------------- Server functions ------------------------ */
240 DoDbServersWaitForQuorum(struct cmd_syndesc
*as
, void *arock
)
244 char *cfgHost
= as
->parms
[0].items
->data
;
245 unsigned int timeout
= 180;
247 if (as
->parms
[1].items
) {
248 timeout
= strtoul(as
->parms
[1].items
->data
, NULL
, 10);
251 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
252 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
254 if (!cfg_DbServersWaitForQuorum(hostHandle
, timeout
, &st
)) {
255 printf("cfg_DbServersWaitForQuorum failed (%s)\n",
259 if (!cfg_HostClose(hostHandle
, &st
)) {
260 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
263 return (st
== 0 ? 0 : 1);
268 DoFileServerStop(struct cmd_syndesc
*as
, void *arock
)
272 char *cfgHost
= as
->parms
[0].items
->data
;
274 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
275 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
277 if (!cfg_FileServerStop(hostHandle
, &st
)) {
278 printf("cfg_FileServerStop failed (%s)\n", GetErrorText(st
));
281 if (!cfg_HostClose(hostHandle
, &st
)) {
282 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
285 return (st
== 0 ? 0 : 1);
289 DoFileServerStart(struct cmd_syndesc
*as
, void *arock
)
293 char *cfgHost
= as
->parms
[0].items
->data
;
295 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
296 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
298 if (!cfg_FileServerStart(hostHandle
, &st
)) {
299 printf("cfg_FileServerStart failed (%s)\n", GetErrorText(st
));
302 if (!cfg_HostClose(hostHandle
, &st
)) {
303 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
306 return (st
== 0 ? 0 : 1);
313 struct cmd_syndesc
*ts
;
315 ts
= cmd_CreateSyntax("DbServersWaitForQuorum", DoDbServersWaitForQuorum
,
316 NULL
, 0, "wait for database servers to achieve quorum");
317 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
318 "configuration host");
319 cmd_AddParm(ts
, "-timeout", CMD_SINGLE
, CMD_OPTIONAL
,
320 "timeout in seconds");
322 ts
= cmd_CreateSyntax("FileServerStop", DoFileServerStop
, NULL
, 0,
323 "stop and unconfigure fileserver on specified host");
324 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
325 "configuration host");
327 ts
= cmd_CreateSyntax("FileServerStart", DoFileServerStart
, NULL
, 0,
328 "start the fileserver on specified host");
329 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
330 "configuration host");
335 /* ------------------- Host functions ------------------------ */
339 DoHostPartitionTableEnumerate(struct cmd_syndesc
*as
, void *arock
)
342 cfg_partitionEntry_t
*vptable
;
345 char *cfgHost
= as
->parms
[0].items
->data
;
347 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
348 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
350 if (!cfg_HostPartitionTableEnumerate
351 (hostHandle
, &vptable
, &tableCount
, &st
)) {
352 printf("cfg_HostPartitionTableEnumerate failed (%s)\n",
355 for (i
= 0; i
< tableCount
; i
++) {
356 printf("Partition: %s Device: %s\n",
357 vptable
[i
].partitionName
, vptable
[i
].deviceName
);
360 if (!cfg_PartitionListDeallocate(vptable
, &st
)) {
361 printf("cfg_PartitionListDeallocate failed (%s)\n",
366 if (!cfg_HostClose(hostHandle
, &st
)) {
367 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
370 return (st
== 0 ? 0 : 1);
377 struct cmd_syndesc
*ts
;
379 ts
= cmd_CreateSyntax("HostPartitionTableEnumerate",
380 DoHostPartitionTableEnumerate
, NULL
, 0,
381 "enumerate vice partition table");
382 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
383 "configuration host");
389 /* ------------------- Client functions ------------------------ */
393 DoClientCellServDbAdd(struct cmd_syndesc
*as
, void *arock
)
397 char *cfgHost
= as
->parms
[0].items
->data
;
398 char *cellName
= as
->parms
[1].items
->data
;
399 char *dbHost
= as
->parms
[2].items
->data
;
401 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
402 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
404 if (!cfg_ClientCellServDbAdd(hostHandle
, cellName
, dbHost
, &st
)) {
405 printf("cfg_ClientCellServDbAdd failed (%s)\n", GetErrorText(st
));
408 if (!cfg_HostClose(hostHandle
, &st
)) {
409 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
412 return (st
== 0 ? 0 : 1);
417 DoClientCellServDbRemove(struct cmd_syndesc
*as
, void *arock
)
421 char *cfgHost
= as
->parms
[0].items
->data
;
422 char *cellName
= as
->parms
[1].items
->data
;
423 char *dbHost
= as
->parms
[2].items
->data
;
425 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
426 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
428 if (!cfg_ClientCellServDbRemove(hostHandle
, cellName
, dbHost
, &st
)) {
429 printf("cfg_ClientCellServDbRemove failed (%s)\n",
433 if (!cfg_HostClose(hostHandle
, &st
)) {
434 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
437 return (st
== 0 ? 0 : 1);
442 DoClientStart(struct cmd_syndesc
*as
, void *arock
)
446 char *cfgHost
= as
->parms
[0].items
->data
;
447 char *timeoutStr
= as
->parms
[1].items
->data
;
448 unsigned int timeoutVal
;
450 timeoutVal
= strtoul(timeoutStr
, NULL
, 10);
452 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
453 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
455 if (!cfg_ClientStart(hostHandle
, timeoutVal
, &st
)) {
456 printf("cfg_ClientStart failed (%s)\n", GetErrorText(st
));
459 if (!cfg_HostClose(hostHandle
, &st
)) {
460 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
463 return (st
== 0 ? 0 : 1);
469 DoClientStop(struct cmd_syndesc
*as
, void *arock
)
473 char *cfgHost
= as
->parms
[0].items
->data
;
474 char *timeoutStr
= as
->parms
[1].items
->data
;
475 unsigned int timeoutVal
;
477 timeoutVal
= strtoul(timeoutStr
, NULL
, 10);
479 if (!cfg_HostOpen(myCellHandle
, cfgHost
, &hostHandle
, &st
)) {
480 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
482 if (!cfg_ClientStop(hostHandle
, timeoutVal
, &st
)) {
483 printf("cfg_ClientStop failed (%s)\n", GetErrorText(st
));
486 if (!cfg_HostClose(hostHandle
, &st
)) {
487 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
490 return (st
== 0 ? 0 : 1);
496 DoClientSetCell(struct cmd_syndesc
*as
, void *arock
)
500 char *cfgHost
= as
->parms
[0].items
->data
;
501 char *cellName
= as
->parms
[1].items
->data
;
502 struct cmd_item
*citem
;
503 char cellDbHosts
[1024];
504 char *dbHost
= cellDbHosts
;
505 void *nullCellHandle
;
507 /* setup multistring of database hosts */
508 for (citem
= as
->parms
[2].items
; citem
!= NULL
; citem
= citem
->next
) {
509 strcpy(dbHost
, citem
->data
);
510 dbHost
+= strlen(citem
->data
) + 1;
514 /* use a null cell handle to avoid "cell mismatch" */
515 if (!afsclient_NullCellOpen(&nullCellHandle
, &st
)) {
516 printf("afsclient_NullCellOpen failed (%s)\n", GetErrorText(st
));
518 if (!cfg_HostOpen(nullCellHandle
, cfgHost
, &hostHandle
, &st
)) {
519 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st
));
521 if (!cfg_ClientSetCell(hostHandle
, cellName
, cellDbHosts
, &st
)) {
522 printf("cfg_ClientSetCell failed (%s)\n", GetErrorText(st
));
525 if (!cfg_HostClose(hostHandle
, &st
)) {
526 printf("cfg_HostClose failed (%s)\n", GetErrorText(st
));
530 if (!afsclient_CellClose(nullCellHandle
, &st
)) {
531 printf("afsclient_CellClose failed (%s)\n", GetErrorText(st
));
534 return (st
== 0 ? 0 : 1);
539 DoClientQueryStatus(struct cmd_syndesc
*as
, void *arock
)
542 char *cfgHost
= as
->parms
[0].items
->data
;
545 afs_status_t clientSt
;
548 if (!cfg_ClientQueryStatus
549 (cfgHost
, &cmInstalled
, &cmVersion
, &clientSt
, &clientCell
, &st
)) {
550 printf("cfg_ClientQueryStatus failed (%s)\n", GetErrorText(st
));
553 printf("Client (CM) version %u is installed.\n", cmVersion
);
555 printf("Client (CM) is not installed.\n");
559 printf("Client configuration is valid; default cell is %s.\n",
562 printf("Client configuration is not valid (%s).\n",
563 GetErrorText(clientSt
));
567 return (st
== 0 ? 0 : 1);
573 DoHostQueryStatus(struct cmd_syndesc
*as
, void *arock
)
576 char *cfgHost
= as
->parms
[0].items
->data
;
577 afs_status_t serverSt
;
580 if (!cfg_HostQueryStatus(cfgHost
, &serverSt
, &serverCell
, &st
)) {
581 printf("cfg_HostQueryStatus failed (%s)\n", GetErrorText(st
));
584 printf("Server configuration is valid; cell is %s.\n",
587 printf("Server configuration is not valid (%s).\n",
588 GetErrorText(serverSt
));
592 return (st
== 0 ? 0 : 1);
603 struct cmd_syndesc
*ts
;
605 ts
= cmd_CreateSyntax("ClientCellServDbAdd", DoClientCellServDbAdd
, NULL
, 0,
606 "add host entry to client CellServDB");
607 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
608 "configuration host");
609 cmd_AddParm(ts
, "-cell", CMD_SINGLE
, CMD_REQUIRED
, "cell name");
610 cmd_AddParm(ts
, "-dbhost", CMD_SINGLE
, CMD_REQUIRED
, "host to add");
612 ts
= cmd_CreateSyntax("ClientCellServDbRemove", DoClientCellServDbRemove
,
613 NULL
, 0, "remove host entry from client CellServDB");
614 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
615 "configuration host");
616 cmd_AddParm(ts
, "-cell", CMD_SINGLE
, CMD_REQUIRED
, "cell name");
617 cmd_AddParm(ts
, "-dbhost", CMD_SINGLE
, CMD_REQUIRED
, "host to remove");
619 ts
= cmd_CreateSyntax("ClientSetCell", DoClientSetCell
, NULL
, 0,
620 "set default client cell");
621 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
622 "configuration host");
623 cmd_AddParm(ts
, "-cell", CMD_SINGLE
, CMD_REQUIRED
, "cell name");
624 cmd_AddParm(ts
, "-dbhosts", CMD_LIST
, CMD_REQUIRED
, "database hosts");
626 ts
= cmd_CreateSyntax("ClientQueryStatus", DoClientQueryStatus
, NULL
, 0,
627 "query status of client on host");
628 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
629 "configuration host");
631 ts
= cmd_CreateSyntax("HostQueryStatus", DoHostQueryStatus
, NULL
, 0,
632 "query status of server on host");
633 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
634 "configuration host");
637 ts
= cmd_CreateSyntax("ClientStart", DoClientStart
, NULL
, 0,
639 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
640 "configuration host");
641 cmd_AddParm(ts
, "-timeout", CMD_SINGLE
, CMD_REQUIRED
, "wait timeout");
644 ts
= cmd_CreateSyntax("ClientStop", DoClientStop
, NULL
, 0, "stop the client");
645 cmd_AddParm(ts
, "-cfghost", CMD_SINGLE
, CMD_REQUIRED
,
646 "configuration host");
647 cmd_AddParm(ts
, "-timeout", CMD_SINGLE
, CMD_REQUIRED
, "wait timeout");
654 main(int argc
, char *argv
[])
658 char *whoami
= argv
[0];
660 /* perform client initialization */
662 if (!afsclient_Init(&st
)) {
663 printf("afsclient_Init failed (%s)\n", GetErrorText(st
));
667 if (!afsclient_LocalCellGet(myCellName
, &st
)) {
668 printf("afsclient_LocalCellGet failed (%s)\n", GetErrorText(st
));
671 printf("%s running in cell %s\n\n", whoami
, myCellName
);
674 if (!afsclient_TokenGetExisting(myCellName
, &myTokenHandle
, &st
)) {
675 printf("afsclient_TokenGetExisting failed (%s)\n", GetErrorText(st
));
676 printf("Test will run unauthenticated\n\n");
678 if (!afsclient_TokenGetNew
679 (myCellName
, NULL
, NULL
, &myTokenHandle
, &st
)) {
680 printf("afsclient_TokenGetNew failed (%s)\n", GetErrorText(st
));
685 if (!afsclient_CellOpen(myCellName
, myTokenHandle
, &myCellHandle
, &st
)) {
686 printf("afsclient_CellOpen failed (%s)\n", GetErrorText(st
));
690 /* initialize command syntax and globals */
692 SetupCellServDbCmd();
697 /* execute command */
699 code
= cmd_Dispatch(argc
, argv
);
701 /* release handles */
703 if (!afsclient_CellClose(myCellHandle
, &st
)) {
704 printf("afsclient_CellClose failed (%s)\n", GetErrorText(st
));
708 if (!afsclient_TokenClose(myTokenHandle
, &st
)) {
709 printf("afsclient_TokenClose failed (%s)\n", GetErrorText(st
));