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>
26 #include <afs/afs_Admin.h>
27 #include <afs/afs_utilAdmin.h>
28 #include <afs/afs_clientAdmin.h>
29 #include <afs/afs_cfgAdmin.h>
31 #include <afs/cellconfig.h>
35 /* define some globals */
36 static char myCellName[MAXCELLCHARS];
37 static void *myTokenHandle;
38 static void *myCellHandle;
42 /* ------------------- Utility functions ---------------------- */
45 GetErrorText(afs_status_t errorCode)
48 const char *errorCodeText;
49 static const char *failedLookupText = "unable to translate error code";
51 if (util_AdminErrorCodeTranslate(errorCode, 0, &errorCodeText, &st)) {
54 return failedLookupText;
59 /* ------------------- CellServDB functions ------------------------ */
61 #define CSDBCallBackId 42
62 static pthread_mutex_t CSDBCallBackMutex;
63 static pthread_cond_t CSDBCallBackCond;
64 static int CSDBCallBackDone;
67 CellServDbCallBack(void *callBackId, cfg_cellServDbStatus_t * statusItemP,
70 if (statusItemP != NULL) {
71 printf("Updated %s with result '%s' (tid = %u)\n",
72 statusItemP->fsDbHost, GetErrorText(statusItemP->status),
73 (unsigned)pthread_self());
75 printf("Update termination (tid = %u)\n", (unsigned)pthread_self());
77 (void)pthread_mutex_lock(&CSDBCallBackMutex);
79 (void)pthread_mutex_unlock(&CSDBCallBackMutex);
80 (void)pthread_cond_signal(&CSDBCallBackCond);
83 if (callBackId != (void *)CSDBCallBackId) {
84 printf("Update call back ID invalid (tid = %u)\n",
85 (unsigned)pthread_self());
91 DoCellServDbAddHost(struct cmd_syndesc *as, void *arock)
96 char *cfgHost = as->parms[0].items->data;
99 if (as->parms[1].items) {
100 sysHost = as->parms[1].items->data;
103 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
104 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
106 CSDBCallBackDone = 0;
108 if (!cfg_CellServDbAddHost
109 (hostHandle, sysHost, CellServDbCallBack, (void *)CSDBCallBackId,
111 printf("cfg_CellServDbAddHost failed (%s)\n", GetErrorText(st));
112 CSDBCallBackDone = 1;
115 printf("cfg_CellServDbAddHost succeeded (maxUpdates = %d)\n",
119 (void)pthread_mutex_lock(&CSDBCallBackMutex);
121 while (!CSDBCallBackDone) {
122 (void)pthread_cond_wait(&CSDBCallBackCond, &CSDBCallBackMutex);
125 (void)pthread_mutex_unlock(&CSDBCallBackMutex);
127 if (!cfg_HostClose(hostHandle, &st)) {
128 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
131 return (st == 0 ? 0 : 1);
136 DoCellServDbRemoveHost(struct cmd_syndesc *as, void *arock)
141 char *cfgHost = as->parms[0].items->data;
142 char *sysHost = NULL;
144 if (as->parms[1].items) {
145 sysHost = as->parms[1].items->data;
148 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
149 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
151 CSDBCallBackDone = 0;
153 if (!cfg_CellServDbRemoveHost
154 (hostHandle, sysHost, CellServDbCallBack, (void *)CSDBCallBackId,
156 printf("cfg_CellServDbRemoveHost failed (%s)\n",
158 CSDBCallBackDone = 1;
161 printf("cfg_CellServDbRemoveHost succeeded (maxUpdates = %d)\n",
165 (void)pthread_mutex_lock(&CSDBCallBackMutex);
167 while (!CSDBCallBackDone) {
168 (void)pthread_cond_wait(&CSDBCallBackCond, &CSDBCallBackMutex);
171 (void)pthread_mutex_unlock(&CSDBCallBackMutex);
173 if (!cfg_HostClose(hostHandle, &st)) {
174 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
177 return (st == 0 ? 0 : 1);
182 DoCellServDbEnumerate(struct cmd_syndesc *as, void *arock)
185 char *fsDbHost = as->parms[0].items->data;
189 if (!cfg_CellServDbEnumerate(fsDbHost, &cellName, &cellDbHosts, &st)) {
190 printf("cfg_CellServDbEnumerate failed (%s)\n", GetErrorText(st));
194 printf("%s is in cell %s\n", fsDbHost, cellName);
196 for (mstrp = cellDbHosts; *mstrp != '\0'; mstrp += strlen(mstrp) + 1) {
197 printf("\t%s\n", mstrp);
200 if (!cfg_StringDeallocate(cellName, &st)) {
201 printf("cfg_StringDeallocate failed (%s)\n", GetErrorText(st));
204 if (!cfg_StringDeallocate(cellDbHosts, &st)) {
205 printf("cfg_StringDeallocate failed (%s)\n", GetErrorText(st));
208 return (st == 0 ? 0 : 1);
213 SetupCellServDbCmd(void)
215 struct cmd_syndesc *ts;
217 ts = cmd_CreateSyntax("CellServDbAddHost", DoCellServDbAddHost, NULL,
218 "add configuration target to server CellServDB");
219 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
220 "configuration host");
221 cmd_AddParm(ts, "-syshost", CMD_SINGLE, CMD_OPTIONAL,
222 "system control host");
224 ts = cmd_CreateSyntax("CellServDbRemoveHost", DoCellServDbRemoveHost, NULL,
225 "remove configuration target from server CellServDB");
226 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
227 "configuration host");
228 cmd_AddParm(ts, "-syshost", CMD_SINGLE, CMD_OPTIONAL,
229 "system control host");
231 ts = cmd_CreateSyntax("CellServDbEnumerate", DoCellServDbEnumerate, NULL,
232 "enumerate server CellServDB from specified host");
233 cmd_AddParm(ts, "-host", CMD_SINGLE, CMD_REQUIRED, "host name");
236 (void)pthread_mutex_init(&CSDBCallBackMutex, NULL);
237 (void)pthread_cond_init(&CSDBCallBackCond, NULL);
242 /* ------------------- Server functions ------------------------ */
246 DoDbServersWaitForQuorum(struct cmd_syndesc *as, void *arock)
250 char *cfgHost = as->parms[0].items->data;
251 unsigned int timeout = 180;
253 if (as->parms[1].items) {
254 timeout = strtoul(as->parms[1].items->data, NULL, 10);
257 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
258 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
260 if (!cfg_DbServersWaitForQuorum(hostHandle, timeout, &st)) {
261 printf("cfg_DbServersWaitForQuorum failed (%s)\n",
265 if (!cfg_HostClose(hostHandle, &st)) {
266 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
269 return (st == 0 ? 0 : 1);
274 DoFileServerStop(struct cmd_syndesc *as, void *arock)
278 char *cfgHost = as->parms[0].items->data;
280 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
281 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
283 if (!cfg_FileServerStop(hostHandle, &st)) {
284 printf("cfg_FileServerStop failed (%s)\n", GetErrorText(st));
287 if (!cfg_HostClose(hostHandle, &st)) {
288 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
291 return (st == 0 ? 0 : 1);
295 DoFileServerStart(struct cmd_syndesc *as, void *arock)
299 char *cfgHost = as->parms[0].items->data;
301 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
302 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
304 if (!cfg_FileServerStart(hostHandle, &st)) {
305 printf("cfg_FileServerStart failed (%s)\n", GetErrorText(st));
308 if (!cfg_HostClose(hostHandle, &st)) {
309 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
312 return (st == 0 ? 0 : 1);
319 struct cmd_syndesc *ts;
321 ts = cmd_CreateSyntax("DbServersWaitForQuorum", DoDbServersWaitForQuorum,
322 NULL, "wait for database servers to achieve quorum");
323 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
324 "configuration host");
325 cmd_AddParm(ts, "-timeout", CMD_SINGLE, CMD_OPTIONAL,
326 "timeout in seconds");
328 ts = cmd_CreateSyntax("FileServerStop", DoFileServerStop, NULL,
329 "stop and unconfigure fileserver on specified host");
330 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
331 "configuration host");
333 ts = cmd_CreateSyntax("FileServerStart", DoFileServerStart, NULL,
334 "start the fileserver on specified host");
335 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
336 "configuration host");
341 /* ------------------- Host functions ------------------------ */
345 DoHostPartitionTableEnumerate(struct cmd_syndesc *as, void *arock)
348 cfg_partitionEntry_t *vptable;
351 char *cfgHost = as->parms[0].items->data;
353 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
354 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
356 if (!cfg_HostPartitionTableEnumerate
357 (hostHandle, &vptable, &tableCount, &st)) {
358 printf("cfg_HostPartitionTableEnumerate failed (%s)\n",
361 for (i = 0; i < tableCount; i++) {
362 printf("Partition: %s Device: %s\n",
363 vptable[i].partitionName, vptable[i].deviceName);
366 if (!cfg_PartitionListDeallocate(vptable, &st)) {
367 printf("cfg_PartitionListDeallocate failed (%s)\n",
372 if (!cfg_HostClose(hostHandle, &st)) {
373 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
376 return (st == 0 ? 0 : 1);
383 struct cmd_syndesc *ts;
385 ts = cmd_CreateSyntax("HostPartitionTableEnumerate",
386 DoHostPartitionTableEnumerate, NULL,
387 "enumerate vice partition table");
388 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
389 "configuration host");
395 /* ------------------- Client functions ------------------------ */
399 DoClientCellServDbAdd(struct cmd_syndesc *as, void *arock)
403 char *cfgHost = as->parms[0].items->data;
404 char *cellName = as->parms[1].items->data;
405 char *dbHost = as->parms[2].items->data;
407 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
408 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
410 if (!cfg_ClientCellServDbAdd(hostHandle, cellName, dbHost, &st)) {
411 printf("cfg_ClientCellServDbAdd failed (%s)\n", GetErrorText(st));
414 if (!cfg_HostClose(hostHandle, &st)) {
415 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
418 return (st == 0 ? 0 : 1);
423 DoClientCellServDbRemove(struct cmd_syndesc *as, void *arock)
427 char *cfgHost = as->parms[0].items->data;
428 char *cellName = as->parms[1].items->data;
429 char *dbHost = as->parms[2].items->data;
431 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
432 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
434 if (!cfg_ClientCellServDbRemove(hostHandle, cellName, dbHost, &st)) {
435 printf("cfg_ClientCellServDbRemove failed (%s)\n",
439 if (!cfg_HostClose(hostHandle, &st)) {
440 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
443 return (st == 0 ? 0 : 1);
448 DoClientStart(struct cmd_syndesc *as, void *arock)
452 char *cfgHost = as->parms[0].items->data;
453 char *timeoutStr = as->parms[1].items->data;
454 unsigned int timeoutVal;
456 timeoutVal = strtoul(timeoutStr, NULL, 10);
458 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
459 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
461 if (!cfg_ClientStart(hostHandle, timeoutVal, &st)) {
462 printf("cfg_ClientStart failed (%s)\n", GetErrorText(st));
465 if (!cfg_HostClose(hostHandle, &st)) {
466 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
469 return (st == 0 ? 0 : 1);
475 DoClientStop(struct cmd_syndesc *as, void *arock)
479 char *cfgHost = as->parms[0].items->data;
480 char *timeoutStr = as->parms[1].items->data;
481 unsigned int timeoutVal;
483 timeoutVal = strtoul(timeoutStr, NULL, 10);
485 if (!cfg_HostOpen(myCellHandle, cfgHost, &hostHandle, &st)) {
486 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
488 if (!cfg_ClientStop(hostHandle, timeoutVal, &st)) {
489 printf("cfg_ClientStop failed (%s)\n", GetErrorText(st));
492 if (!cfg_HostClose(hostHandle, &st)) {
493 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
496 return (st == 0 ? 0 : 1);
502 DoClientSetCell(struct cmd_syndesc *as, void *arock)
506 char *cfgHost = as->parms[0].items->data;
507 char *cellName = as->parms[1].items->data;
508 struct cmd_item *citem;
509 char cellDbHosts[1024];
510 char *dbHost = cellDbHosts;
511 void *nullCellHandle;
513 /* setup multistring of database hosts */
514 for (citem = as->parms[2].items; citem != NULL; citem = citem->next) {
515 strcpy(dbHost, citem->data);
516 dbHost += strlen(citem->data) + 1;
520 /* use a null cell handle to avoid "cell mismatch" */
521 if (!afsclient_NullCellOpen(&nullCellHandle, &st)) {
522 printf("afsclient_NullCellOpen failed (%s)\n", GetErrorText(st));
524 if (!cfg_HostOpen(nullCellHandle, cfgHost, &hostHandle, &st)) {
525 printf("cfg_HostOpen failed (%s)\n", GetErrorText(st));
527 if (!cfg_ClientSetCell(hostHandle, cellName, cellDbHosts, &st)) {
528 printf("cfg_ClientSetCell failed (%s)\n", GetErrorText(st));
531 if (!cfg_HostClose(hostHandle, &st)) {
532 printf("cfg_HostClose failed (%s)\n", GetErrorText(st));
536 if (!afsclient_CellClose(nullCellHandle, &st)) {
537 printf("afsclient_CellClose failed (%s)\n", GetErrorText(st));
540 return (st == 0 ? 0 : 1);
545 DoClientQueryStatus(struct cmd_syndesc *as, void *arock)
548 char *cfgHost = as->parms[0].items->data;
551 afs_status_t clientSt;
554 if (!cfg_ClientQueryStatus
555 (cfgHost, &cmInstalled, &cmVersion, &clientSt, &clientCell, &st)) {
556 printf("cfg_ClientQueryStatus failed (%s)\n", GetErrorText(st));
559 printf("Client (CM) version %u is installed.\n", cmVersion);
561 printf("Client (CM) is not installed.\n");
565 printf("Client configuration is valid; default cell is %s.\n",
568 printf("Client configuration is not valid (%s).\n",
569 GetErrorText(clientSt));
573 return (st == 0 ? 0 : 1);
579 DoHostQueryStatus(struct cmd_syndesc *as, void *arock)
582 char *cfgHost = as->parms[0].items->data;
583 afs_status_t serverSt;
586 if (!cfg_HostQueryStatus(cfgHost, &serverSt, &serverCell, &st)) {
587 printf("cfg_HostQueryStatus failed (%s)\n", GetErrorText(st));
590 printf("Server configuration is valid; cell is %s.\n",
593 printf("Server configuration is not valid (%s).\n",
594 GetErrorText(serverSt));
598 return (st == 0 ? 0 : 1);
609 struct cmd_syndesc *ts;
611 ts = cmd_CreateSyntax("ClientCellServDbAdd", DoClientCellServDbAdd, NULL,
612 "add host entry to client CellServDB");
613 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
614 "configuration host");
615 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_REQUIRED, "cell name");
616 cmd_AddParm(ts, "-dbhost", CMD_SINGLE, CMD_REQUIRED, "host to add");
618 ts = cmd_CreateSyntax("ClientCellServDbRemove", DoClientCellServDbRemove,
619 NULL, "remove host entry from client CellServDB");
620 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
621 "configuration host");
622 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_REQUIRED, "cell name");
623 cmd_AddParm(ts, "-dbhost", CMD_SINGLE, CMD_REQUIRED, "host to remove");
625 ts = cmd_CreateSyntax("ClientSetCell", DoClientSetCell, NULL,
626 "set default client cell");
627 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
628 "configuration host");
629 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_REQUIRED, "cell name");
630 cmd_AddParm(ts, "-dbhosts", CMD_LIST, CMD_REQUIRED, "database hosts");
632 ts = cmd_CreateSyntax("ClientQueryStatus", DoClientQueryStatus, NULL,
633 "query status of client on host");
634 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
635 "configuration host");
637 ts = cmd_CreateSyntax("HostQueryStatus", DoHostQueryStatus, NULL,
638 "query status of server on host");
639 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
640 "configuration host");
643 ts = cmd_CreateSyntax("ClientStart", DoClientStart, NULL,
645 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
646 "configuration host");
647 cmd_AddParm(ts, "-timeout", CMD_SINGLE, CMD_REQUIRED, "wait timeout");
650 ts = cmd_CreateSyntax("ClientStop", DoClientStop, NULL, "stop the client");
651 cmd_AddParm(ts, "-cfghost", CMD_SINGLE, CMD_REQUIRED,
652 "configuration host");
653 cmd_AddParm(ts, "-timeout", CMD_SINGLE, CMD_REQUIRED, "wait timeout");
660 main(int argc, char *argv[])
664 char *whoami = argv[0];
666 /* perform client initialization */
668 if (!afsclient_Init(&st)) {
669 printf("afsclient_Init failed (%s)\n", GetErrorText(st));
673 if (!afsclient_LocalCellGet(myCellName, &st)) {
674 printf("afsclient_LocalCellGet failed (%s)\n", GetErrorText(st));
677 printf("%s running in cell %s\n\n", whoami, myCellName);
680 if (!afsclient_TokenGetExisting(myCellName, &myTokenHandle, &st)) {
681 printf("afsclient_TokenGetExisting failed (%s)\n", GetErrorText(st));
682 printf("Test will run unauthenticated\n\n");
684 if (!afsclient_TokenGetNew
685 (myCellName, NULL, NULL, &myTokenHandle, &st)) {
686 printf("afsclient_TokenGetNew failed (%s)\n", GetErrorText(st));
691 if (!afsclient_CellOpen(myCellName, myTokenHandle, &myCellHandle, &st)) {
692 printf("afsclient_CellOpen failed (%s)\n", GetErrorText(st));
696 /* initialize command syntax and globals */
698 SetupCellServDbCmd();
703 /* execute command */
705 code = cmd_Dispatch(argc, argv);
707 /* release handles */
709 if (!afsclient_CellClose(myCellHandle, &st)) {
710 printf("afsclient_CellClose failed (%s)\n", GetErrorText(st));
714 if (!afsclient_TokenClose(myTokenHandle, &st)) {
715 printf("afsclient_TokenClose failed (%s)\n", GetErrorText(st));