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 #include <afsconfig.h>
11 #include <afs/param.h>
15 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
32 #include <rx/rx_globals.h>
34 #include <afs/bubasics.h>
36 #include <afs/afsutil.h>
38 #include <afs/cellconfig.h>
43 #include <afs/volser.h> /*VLDB_MAXSERVERS */
44 #include <afs/com_err.h>
47 #include <afs/kautils.h>
48 #include <afs/vlserver.h>
52 #include "bc.h" /*Backup Coordinator structs and defs */
53 #include "bucoord_internal.h"
55 int localauth, interact;
59 * Global configuration information for the Backup Coordinator.
61 struct bc_config *bc_globalConfig; /*Ptr to global BC configuration info */
63 struct ubik_client *cstruct; /* Ptr to Ubik client structure */
64 struct ktc_token ttoken; /* The token */
66 static const char *DefaultConfDir; /*Default backup config directory */
67 static int bcInit = 0; /* backupInit called yet ? */
68 char *whoami = "backup";
70 /* dummy routine for the audit work. It should do nothing since audits */
71 /* occur at the server level and bos is not a server. */
79 * Initialize all the error tables that may be used by com_err
85 initialize_ACFG_error_table();
86 initialize_KA_error_table();
87 initialize_RXK_error_table();
88 initialize_CMD_error_table();
89 initialize_VL_error_table();
90 initialize_BUTM_error_table();
91 initialize_VOLS_error_table();
92 initialize_BUTC_error_table();
93 initialize_BUTX_error_table();
94 initialize_BUDB_error_table();
95 initialize_BUCD_error_table();
96 initialize_KTC_error_table();
100 * got to account for the errors which are volume related but
101 * not dealt with by standard errno and com_err stuff.
104 bc_HandleMisc(afs_int32 code)
106 if (((code <= VMOVED) && (code >= VSALVAGE)) || (code < 0)) {
109 fprintf(STDERR, "Possible communication failure\n");
112 fprintf(STDERR, "Volume needs salvage\n");
115 fprintf(STDERR, "Bad vnode number quoted\n");
119 "Volume not attached, does not exist, or not on line\n");
122 fprintf(STDERR, "Volume already exists\n");
125 fprintf(STDERR, "Volume is not in service\n");
128 fprintf(STDERR, "Volume is off line\n");
131 fprintf(STDERR, "Volume is already on line\n");
134 fprintf(STDERR, "Partition is full\n");
137 fprintf(STDERR, "Volume max quota exceeded\n");
140 fprintf(STDERR, "Volume temporarily unavailable\n");
143 fprintf(STDERR, "Volume has moved to another server\n");
152 /* Return true if line is all whitespace */
154 LineIsBlank(char *aline)
158 while ((tc = *aline++))
159 if ((tc != ' ') && (tc != '\t') && (tc != '\n'))
167 * initialize configuration information that is stored as text blocks
171 bc_InitTextConfig(void)
173 udbClientTextP ctPtr;
176 extern struct bc_config *bc_globalConfig;
178 mkdir(DefaultConfDir, 777); /* temporary */
180 /* initialize the client text structures */
181 ctPtr = &bc_globalConfig->configText[0];
183 for (i = 0; i < TB_NUM; i++) {
184 memset(ctPtr, 0, sizeof(*ctPtr));
186 ctPtr->textVersion = -1;
193 /*----------------------------------------------------------------------------
197 * Routine that is called when the backup coordinator is invoked, responsible for
198 * basic initialization and some command line parsing.
201 * Zero (but may exit the entire program on error!)
204 * Nothing interesting.
207 * Initializes this program.
208 *----------------------------------------------------------------------------
214 register afs_int32 code;
215 static int initd = 0; /* ever called? */
217 PROCESS pid; /* LWP process ID */
220 initialize_CMD_error_table();
222 /* don't run more than once */
224 afs_com_err(whoami, 0, "Backup already initialized.");
229 code = bc_InitConfig(DefaultConfDir);
231 afs_com_err(whoami, code,
232 "Can't initialize from config files in directory '%s'",
240 code = LWP_InitializeProcessSupport(LWP_NORMAL_PRIORITY, &pid);
242 afs_com_err(whoami, code, "; Can't initialize LWP");
246 code = rx_Init(htons(0));
248 afs_com_err(whoami, code, "; Can't initialize Rx");
252 rx_SetRxDeadTime(60);
254 /* VLDB initialization */
255 code = vldbClientInit(0, localauth, tcell, &cstruct, &ttoken);
259 /* Backup database initialization */
260 code = udbClientInit(0, localauth, tcell);
264 /* setup status monitoring thread */
267 LWP_CreateProcess(statusWatcher, 20480, LWP_NORMAL_PRIORITY,
268 (void *)2, "statusWatcher", &watcherPid);
270 afs_com_err(whoami, code, "; Can't create status monitor task");
277 /*----------------------------------------------------------------------------
281 * Make sure we mark down when we need to exit the Backup Coordinator.
282 * Specifically, we don't want to continue when our help and apropos
283 * routines are called from the command line.
286 * as : Ptr to the command syntax descriptor.
292 * This routine is called right before each of the Backup Coordinator
293 * opcode routines are invoked.
295 *----------------------------------------------------------------------------
299 MyBeforeProc(register struct cmd_syndesc *as, void *arock)
303 /* Handling the command line opcode */
305 localauth = ((as && as->parms[14].items) ? 1 : 0);
306 if (as && as->parms[15].items)
307 strcpy(tcell, as->parms[15].items->data);
313 afs_com_err(whoami, code, "; Can't initialize backup");
317 /* Get initial information from the database */
318 code = bc_InitTextConfig();
320 afs_com_err(whoami, code,
321 "; Can't obtain configuration text from backup database");
333 #include "AFS_component_version_number.c"
335 #define MAXRECURSION 20
336 extern int dontExecute; /* declared in commands.c */
337 extern char *loadFile; /* declared in commands.c */
338 char lineBuffer[1024]; /* Line typed in by user or read from load file */
341 * This will dispatch a command. It holds a recursive loop for the
342 * "dump -file" option. This option reads backup commands from a file.
344 * Cannot put this code on other side of cmd_Dispatch call (in
345 * commands.c) because when make a dispatch call when in a dispatch
346 * call, environment is mucked up.
348 * To avoid multiple processes stepping on each other in the dispatch code,
349 * put a lock around it so only 1 process gets in at a time.
352 struct Lock dispatchLock; /* lock on the Dispatch call */
353 #define lock_Dispatch() ObtainWriteLock(&dispatchLock)
354 #define unlock_Dispatch() ReleaseWriteLock(&dispatchLock)
357 doDispatch(afs_int32 targc,
359 afs_int32 dispatchCount) /* to prevent infinite recursion */
367 int noExecute; /* local capy of global variable */
368 char *internalLoadFile;
373 code = cmd_Dispatch(targc, targv);
374 internalLoadFile = loadFile;
378 if (internalLoadFile) { /* Load a file in */
379 if (dispatchCount > MAXRECURSION) { /* Beware recursive loops. */
380 afs_com_err(whoami, 0, "Potential recursion: will not load file %s",
386 fd = fopen(internalLoadFile, "r"); /* Open the load file */
388 afs_com_err(whoami, errno, "; Cannot open file %s", internalLoadFile);
393 noExecute = dontExecute;
395 printf("Would have executed the following commands:\n");
398 while (fgets(lineBuffer, sizeof(lineBuffer) - 1, fd)) { /* Get commands from file */
401 i = strlen(lineBuffer) - 1;
402 if (lineBuffer[i] == '\n') /* Drop return at end of line */
403 lineBuffer[i] = '\0';
406 printf(" %s\n", lineBuffer); /* echo */
408 printf("------> %s\n", lineBuffer); /* echo */
410 if (!LineIsBlank(lineBuffer) && /* Skip if blank line */
411 (lineBuffer[0] != '#') && /* or comment */
412 (!noExecute)) { /* or no execute */
413 c = cmd_ParseLine(lineBuffer, sargv, &sargc, MAXV);
415 afs_com_err(whoami, c, "; Can't parse line");
417 doDispatch(sargc, sargv, dispatchCount + 1); /* Recursive - ignore error */
418 cmd_FreeArgv(sargv); /* Free up arguments */
427 if (internalLoadFile)
428 free(internalLoadFile);
433 bc_interactCmd(struct cmd_syndesc *as, void *arock)
440 add_std_args(struct cmd_syndesc *ts)
443 cmd_AddParm(ts, "-localauth", CMD_FLAG, CMD_OPTIONAL,
444 "local authentication");
445 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
449 main(int argc, char **argv)
451 char *targv[MAXV]; /*Ptr to parsed argv stuff */
452 afs_int32 targc; /*Num parsed arguments */
453 afs_int32 code; /*Return code */
454 register struct cmd_syndesc *ts; /*Ptr to parsed command line */
460 * The following signal action for AIX is necessary so that in case of a
461 * crash (i.e. core is generated) we can include the user's data section
462 * in the core dump. Unfortunately, by default, only a partial core is
463 * generated which, in many cases, isn't too useful.
465 struct sigaction nsa;
467 sigemptyset(&nsa.sa_mask);
468 nsa.sa_handler = SIG_DFL;
469 nsa.sa_flags = SA_FULLDUMP;
470 sigaction(SIGSEGV, &nsa, NULL);
472 Lock_Init(&dispatchLock);
473 InitErrTabs(); /* init all the error tables which may be used */
475 /* setup the default backup dir */
476 DefaultConfDir = AFSDIR_SERVER_BACKUP_DIRPATH;
477 /* Get early warning if the command is interacive mode or not */
478 interact = (((argc < 2) || (argv[1][0] == '-')) ? 1 : 0);
480 cmd_SetBeforeProc(MyBeforeProc, NULL);
482 ts = cmd_CreateSyntax("dump", bc_DumpCmd, NULL, "start dump");
483 cmd_AddParm(ts, "-volumeset", CMD_SINGLE, CMD_OPTIONAL,
485 cmd_AddParm(ts, "-dump", CMD_SINGLE, CMD_OPTIONAL, "dump level name");
486 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
488 cmd_AddParm(ts, "-at", CMD_LIST, CMD_OPTIONAL, "Date/time to start dump");
489 cmd_AddParm(ts, "-append", CMD_FLAG, CMD_OPTIONAL,
490 "append to existing dump set");
491 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
492 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "load file");
496 ts = cmd_CreateSyntax("volrestore", bc_VolRestoreCmd, NULL,
498 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED,
499 "destination machine");
500 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED,
501 "destination partition");
502 cmd_AddParm(ts, "-volume", CMD_LIST, CMD_REQUIRED,
503 "volume(s) to restore");
504 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
505 "new volume name extension");
506 cmd_AddParm(ts, "-date", CMD_LIST, CMD_OPTIONAL,
507 "date from which to restore");
508 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offsets");
509 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
510 cmd_AddParm(ts, "-usedump", CMD_SINGLE, CMD_OPTIONAL,
511 "specify the dumpID to restore from");
515 ts = cmd_CreateSyntax("diskrestore", bc_DiskRestoreCmd, NULL,
516 "restore partition");
517 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED,
518 "machine to restore");
519 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED,
520 "partition to restore");
521 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offset");
523 cmd_AddParm(ts, "-newserver", CMD_SINGLE, CMD_OPTIONAL,
524 "destination machine");
525 cmd_AddParm(ts, "-newpartition", CMD_SINGLE, CMD_OPTIONAL,
526 "destination partition");
527 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
528 "new volume name extension");
529 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
533 ts = cmd_CreateSyntax("quit", bc_QuitCmd, NULL, "leave the program");
535 ts = cmd_CreateSyntax("volsetrestore", bc_VolsetRestoreCmd, NULL,
536 "restore a set of volumes");
537 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume set name");
538 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "file name");
539 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offset");
540 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
541 "new volume name extension");
542 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
546 ts = cmd_CreateSyntax("addhost", bc_AddHostCmd, NULL, "add host to config");
547 cmd_AddParm(ts, "-tapehost", CMD_SINGLE, CMD_REQUIRED,
548 "tape machine name");
549 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
554 ts = cmd_CreateSyntax("delhost", bc_DeleteHostCmd, NULL,
555 "delete host to config");
556 cmd_AddParm(ts, "-tapehost", CMD_SINGLE, CMD_REQUIRED,
557 "tape machine name");
558 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
563 ts = cmd_CreateSyntax("listhosts", bc_ListHostsCmd, NULL,
564 "list config hosts");
568 ts = cmd_CreateSyntax("jobs", bc_JobsCmd, NULL, "list running jobs");
570 ts = cmd_CreateSyntax("kill", bc_KillCmd, NULL, "kill running job");
571 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_REQUIRED,
572 "job ID or dump set name");
574 ts = cmd_CreateSyntax("listvolsets", bc_ListVolSetCmd, NULL,
576 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume set name");
580 ts = cmd_CreateSyntax("listdumps", bc_ListDumpScheduleCmd, NULL,
581 "list dump schedules");
585 ts = cmd_CreateSyntax("addvolset", bc_AddVolSetCmd, NULL,
586 "create a new volume set");
587 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
588 cmd_AddParm(ts, "-temporary", CMD_FLAG, CMD_OPTIONAL,
589 "temporary volume set");
593 ts = cmd_CreateSyntax("status", bc_GetTapeStatusCmd, NULL,
594 "get tape coordinator status");
595 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
600 ts = cmd_CreateSyntax("delvolset", bc_DeleteVolSetCmd, NULL,
601 "delete a volume set");
602 cmd_AddParm(ts, "-name", CMD_LIST, CMD_REQUIRED, "volume set name");
606 ts = cmd_CreateSyntax("addvolentry", bc_AddVolEntryCmd, NULL,
607 "add a new volume entry");
608 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
609 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "machine name");
610 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED, "partition name");
611 cmd_AddParm(ts, "-volumes", CMD_SINGLE, CMD_REQUIRED,
612 "volume name (regular expression)");
616 ts = cmd_CreateSyntax("delvolentry", bc_DeleteVolEntryCmd, NULL,
617 "delete a volume set sub-entry");
618 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
619 cmd_AddParm(ts, "-entry", CMD_SINGLE, CMD_REQUIRED, "volume set index");
623 ts = cmd_CreateSyntax("adddump", bc_AddDumpCmd, NULL, "add dump schedule");
624 cmd_AddParm(ts, "-dump", CMD_LIST, CMD_REQUIRED, "dump level name");
625 cmd_AddParm(ts, "-expires", CMD_LIST, CMD_OPTIONAL, "expiration date");
629 ts = cmd_CreateSyntax("deldump", bc_DeleteDumpCmd, NULL,
630 "delete dump schedule");
631 cmd_AddParm(ts, "-dump", CMD_SINGLE, CMD_REQUIRED, "dump level name");
635 ts = cmd_CreateSyntax("labeltape", bc_LabelTapeCmd, NULL, "label a tape");
636 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL,
637 "AFS tape name, defaults to NULL");
638 cmd_AddParm(ts, "-size", CMD_SINGLE, CMD_OPTIONAL,
639 "tape size in Kbytes, defaults to size in tapeconfig");
640 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
642 cmd_AddParm(ts, "-pname", CMD_SINGLE, CMD_OPTIONAL,
643 "permanent tape name");
647 ts = cmd_CreateSyntax("readlabel", bc_ReadLabelCmd, NULL,
648 "read the label on tape");
649 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
654 ts = cmd_CreateSyntax("scantape", bc_ScanDumpsCmd, NULL,
655 "dump information recovery from tape");
656 cmd_AddParm(ts, "-dbadd", CMD_FLAG, CMD_OPTIONAL,
657 "add information to the database");
658 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
663 ts = cmd_CreateSyntax("volinfo", bc_dblookupCmd, NULL,
664 "query the backup database");
665 cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_REQUIRED, "volume name");
669 ts = cmd_CreateSyntax("setexp", bc_SetExpCmd, NULL,
670 "set/clear dump expiration dates");
671 cmd_AddParm(ts, "-dump", CMD_LIST, CMD_REQUIRED, "dump level name");
672 cmd_AddParm(ts, "-expires", CMD_LIST, CMD_OPTIONAL, "expiration date");
676 ts = cmd_CreateSyntax("savedb", bc_saveDbCmd, NULL, "save backup database");
677 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
679 cmd_AddParm(ts, "-archive", CMD_LIST, CMD_OPTIONAL, "date time");
683 ts = cmd_CreateSyntax("restoredb", bc_restoreDbCmd, NULL,
684 "restore backup database");
685 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
690 ts = cmd_CreateSyntax("dumpinfo", bc_dumpInfoCmd, NULL,
691 "provide information about a dump in the database");
692 cmd_AddParm(ts, "-ndumps", CMD_SINGLE, CMD_OPTIONAL, "no. of dumps");
693 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "dump id");
694 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
695 "detailed description");
699 ts = cmd_CreateSyntax("dbverify", bc_dbVerifyCmd, NULL,
700 "check ubik database integrity");
701 cmd_AddParm(ts, "-detail", CMD_FLAG, CMD_OPTIONAL, "additional details");
705 ts = cmd_CreateSyntax("deletedump", bc_deleteDumpCmd, NULL,
706 "delete dumps from the database");
707 cmd_AddParm(ts, "-dumpid", CMD_LIST, CMD_OPTIONAL, "dump id");
708 cmd_AddParm(ts, "-from", CMD_LIST, CMD_OPTIONAL, "date time");
709 cmd_AddParm(ts, "-to", CMD_LIST, CMD_OPTIONAL, "date time");
710 cmd_AddParm(ts, "-port", CMD_SINGLE, CMD_OPTIONAL, "TC port offset");
711 cmd_AddParm(ts, "-groupid", CMD_SINGLE, CMD_OPTIONAL, "group ID");
712 cmd_AddParm(ts, "-dbonly", CMD_FLAG, CMD_OPTIONAL,
713 "delete the dump from the backup database only");
714 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
715 "always delete from backup database");
716 cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL,
717 "Just list the dumps");
722 ts = cmd_CreateSyntax("interactive", bc_interactCmd, NULL,
723 "enter interactive mode");
727 * Now execute the command.
731 targv[targc++] = argv[0];
733 targv[targc++] = "interactive";
734 for (i = 1; i < argc; i++)
735 targv[targc++] = argv[i];
737 code = doDispatch(targc, targv, 1);
739 if (!interact || !bcInit) { /* Non-interactive mode */
743 code = bc_WaitForNoJobs(); /* wait for any jobs to finish */
744 exit(code); /* and exit */
747 /* Iterate on command lines, interpreting user commands (interactive mode) */
755 while ((ret = LWP_GetLine(lineBuffer, sizeof(lineBuffer))) == 0)
756 printf("%s: Command line too long\n", whoami); /* line too long */
759 return 0; /* Got EOF */
761 if (!LineIsBlank(lineBuffer)) {
762 code = cmd_ParseLine(lineBuffer, targv, &targc, MAXV);
764 afs_com_err(whoami, code, "; Can't parse line: '%s'",
765 afs_error_message(code));
767 doDispatch(targc, targv, 1);