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>
48 #include "bc.h" /*Backup Coordinator structs and defs */
49 #include "bucoord_prototypes.h"
51 int localauth, interact;
55 * Global configuration information for the Backup Coordinator.
57 struct bc_config *bc_globalConfig; /*Ptr to global BC configuration info */
59 struct ubik_client *cstruct; /* Ptr to Ubik client structure */
60 struct ktc_token ttoken; /* The token */
62 static const char *DefaultConfDir; /*Default backup config directory */
63 static int bcInit = 0; /* backupInit called yet ? */
64 char *whoami = "backup";
66 /* dummy routine for the audit work. It should do nothing since audits */
67 /* occur at the server level and bos is not a server. */
75 * Initialize all the error tables that may be used by com_err
81 initialize_ACFG_error_table();
82 initialize_KA_error_table();
83 initialize_RXK_error_table();
84 initialize_CMD_error_table();
85 initialize_VL_error_table();
86 initialize_BUTM_error_table();
87 initialize_VOLS_error_table();
88 initialize_BUTC_error_table();
89 initialize_BUTX_error_table();
90 initialize_BUDB_error_table();
91 initialize_BUCD_error_table();
92 initialize_KTC_error_table();
96 * got to account for the errors which are volume related but
97 * not dealt with by standard errno and com_err stuff.
100 bc_HandleMisc(afs_int32 code)
102 if (((code <= VMOVED) && (code >= VSALVAGE)) || (code < 0)) {
105 fprintf(STDERR, "Possible communication failure\n");
108 fprintf(STDERR, "Volume needs salvage\n");
111 fprintf(STDERR, "Bad vnode number quoted\n");
115 "Volume not attached, does not exist, or not on line\n");
118 fprintf(STDERR, "Volume already exists\n");
121 fprintf(STDERR, "Volume is not in service\n");
124 fprintf(STDERR, "Volume is off line\n");
127 fprintf(STDERR, "Volume is already on line\n");
130 fprintf(STDERR, "Partition is full\n");
133 fprintf(STDERR, "Volume max quota exceeded\n");
136 fprintf(STDERR, "Volume temporarily unavailable\n");
139 fprintf(STDERR, "Volume has moved to another server\n");
148 /* Return true if line is all whitespace */
150 LineIsBlank(char *aline)
154 while ((tc = *aline++))
155 if ((tc != ' ') && (tc != '\t') && (tc != '\n'))
163 * initialize configuration information that is stored as text blocks
167 bc_InitTextConfig(void)
169 udbClientTextP ctPtr;
172 extern struct bc_config *bc_globalConfig;
174 mkdir(DefaultConfDir, 777); /* temporary */
176 /* initialize the client text structures */
177 ctPtr = &bc_globalConfig->configText[0];
179 for (i = 0; i < TB_NUM; i++) {
180 memset(ctPtr, 0, sizeof(*ctPtr));
182 ctPtr->textVersion = -1;
189 /*----------------------------------------------------------------------------
193 * Routine that is called when the backup coordinator is invoked, responsible for
194 * basic initialization and some command line parsing.
197 * Zero (but may exit the entire program on error!)
200 * Nothing interesting.
203 * Initializes this program.
204 *----------------------------------------------------------------------------
210 register afs_int32 code;
211 static int initd = 0; /* ever called? */
213 PROCESS pid; /* LWP process ID */
216 initialize_CMD_error_table();
218 /* don't run more than once */
220 afs_com_err(whoami, 0, "Backup already initialized.");
225 code = bc_InitConfig(DefaultConfDir);
227 afs_com_err(whoami, code,
228 "Can't initialize from config files in directory '%s'",
236 code = LWP_InitializeProcessSupport(LWP_NORMAL_PRIORITY, &pid);
238 afs_com_err(whoami, code, "; Can't initialize LWP");
242 code = rx_Init(htons(0));
244 afs_com_err(whoami, code, "; Can't initialize Rx");
248 rx_SetRxDeadTime(60);
250 /* VLDB initialization */
251 code = vldbClientInit(0, localauth, tcell, &cstruct, &ttoken);
255 /* Backup database initialization */
256 code = udbClientInit(0, localauth, tcell);
260 /* setup status monitoring thread */
263 LWP_CreateProcess(statusWatcher, 20480, LWP_NORMAL_PRIORITY,
264 (void *)2, "statusWatcher", &watcherPid);
266 afs_com_err(whoami, code, "; Can't create status monitor task");
273 /*----------------------------------------------------------------------------
277 * Make sure we mark down when we need to exit the Backup Coordinator.
278 * Specifically, we don't want to continue when our help and apropos
279 * routines are called from the command line.
282 * as : Ptr to the command syntax descriptor.
288 * This routine is called right before each of the Backup Coordinator
289 * opcode routines are invoked.
291 *----------------------------------------------------------------------------
295 MyBeforeProc(register struct cmd_syndesc *as, void *arock)
299 /* Handling the command line opcode */
301 localauth = ((as && as->parms[14].items) ? 1 : 0);
302 if (as && as->parms[15].items)
303 strcpy(tcell, as->parms[15].items->data);
309 afs_com_err(whoami, code, "; Can't initialize backup");
313 /* Get initial information from the database */
314 code = bc_InitTextConfig();
316 afs_com_err(whoami, code,
317 "; Can't obtain configuration text from backup database");
329 #include "AFS_component_version_number.c"
331 #define MAXRECURSION 20
332 extern int dontExecute; /* declared in commands.c */
333 extern char *loadFile; /* declared in commands.c */
334 char lineBuffer[1024]; /* Line typed in by user or read from load file */
337 * This will dispatch a command. It holds a recursive loop for the
338 * "dump -file" option. This option reads backup commands from a file.
340 * Cannot put this code on other side of cmd_Dispatch call (in
341 * commands.c) because when make a dispatch call when in a dispatch
342 * call, environment is mucked up.
344 * To avoid multiple processes stepping on each other in the dispatch code,
345 * put a lock around it so only 1 process gets in at a time.
348 struct Lock dispatchLock; /* lock on the Dispatch call */
349 #define lock_Dispatch() ObtainWriteLock(&dispatchLock)
350 #define unlock_Dispatch() ReleaseWriteLock(&dispatchLock)
353 doDispatch(afs_int32 targc,
355 afs_int32 dispatchCount) /* to prevent infinite recursion */
363 int noExecute; /* local capy of global variable */
364 char *internalLoadFile;
369 code = cmd_Dispatch(targc, targv);
370 internalLoadFile = loadFile;
374 if (internalLoadFile) { /* Load a file in */
375 if (dispatchCount > MAXRECURSION) { /* Beware recursive loops. */
376 afs_com_err(whoami, 0, "Potential recursion: will not load file %s",
382 fd = fopen(internalLoadFile, "r"); /* Open the load file */
384 afs_com_err(whoami, errno, "; Cannot open file %s", internalLoadFile);
389 noExecute = dontExecute;
391 printf("Would have executed the following commands:\n");
394 while (fgets(lineBuffer, sizeof(lineBuffer) - 1, fd)) { /* Get commands from file */
397 i = strlen(lineBuffer) - 1;
398 if (lineBuffer[i] == '\n') /* Drop return at end of line */
399 lineBuffer[i] = '\0';
402 printf(" %s\n", lineBuffer); /* echo */
404 printf("------> %s\n", lineBuffer); /* echo */
406 if (!LineIsBlank(lineBuffer) && /* Skip if blank line */
407 (lineBuffer[0] != '#') && /* or comment */
408 (!noExecute)) { /* or no execute */
409 c = cmd_ParseLine(lineBuffer, sargv, &sargc, MAXV);
411 afs_com_err(whoami, c, "; Can't parse line");
413 doDispatch(sargc, sargv, dispatchCount + 1); /* Recursive - ignore error */
414 cmd_FreeArgv(sargv); /* Free up arguments */
423 if (internalLoadFile)
424 free(internalLoadFile);
429 bc_interactCmd(struct cmd_syndesc *as, void *arock)
436 add_std_args(struct cmd_syndesc *ts)
439 cmd_AddParm(ts, "-localauth", CMD_FLAG, CMD_OPTIONAL,
440 "local authentication");
441 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
445 main(int argc, char **argv)
447 char *targv[MAXV]; /*Ptr to parsed argv stuff */
448 afs_int32 targc; /*Num parsed arguments */
449 afs_int32 code; /*Return code */
450 register struct cmd_syndesc *ts; /*Ptr to parsed command line */
456 * The following signal action for AIX is necessary so that in case of a
457 * crash (i.e. core is generated) we can include the user's data section
458 * in the core dump. Unfortunately, by default, only a partial core is
459 * generated which, in many cases, isn't too useful.
461 struct sigaction nsa;
463 sigemptyset(&nsa.sa_mask);
464 nsa.sa_handler = SIG_DFL;
465 nsa.sa_flags = SA_FULLDUMP;
466 sigaction(SIGSEGV, &nsa, NULL);
468 Lock_Init(&dispatchLock);
469 InitErrTabs(); /* init all the error tables which may be used */
471 /* setup the default backup dir */
472 DefaultConfDir = AFSDIR_SERVER_BACKUP_DIRPATH;
473 /* Get early warning if the command is interacive mode or not */
474 interact = (((argc < 2) || (argv[1][0] == '-')) ? 1 : 0);
476 cmd_SetBeforeProc(MyBeforeProc, NULL);
478 ts = cmd_CreateSyntax("dump", bc_DumpCmd, NULL, "start dump");
479 cmd_AddParm(ts, "-volumeset", CMD_SINGLE, CMD_OPTIONAL,
481 cmd_AddParm(ts, "-dump", CMD_SINGLE, CMD_OPTIONAL, "dump level name");
482 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
484 cmd_AddParm(ts, "-at", CMD_LIST, CMD_OPTIONAL, "Date/time to start dump");
485 cmd_AddParm(ts, "-append", CMD_FLAG, CMD_OPTIONAL,
486 "append to existing dump set");
487 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
488 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "load file");
492 ts = cmd_CreateSyntax("volrestore", bc_VolRestoreCmd, NULL,
494 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED,
495 "destination machine");
496 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED,
497 "destination partition");
498 cmd_AddParm(ts, "-volume", CMD_LIST, CMD_REQUIRED,
499 "volume(s) to restore");
500 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
501 "new volume name extension");
502 cmd_AddParm(ts, "-date", CMD_LIST, CMD_OPTIONAL,
503 "date from which to restore");
504 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offsets");
505 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
506 cmd_AddParm(ts, "-usedump", CMD_SINGLE, CMD_OPTIONAL,
507 "specify the dumpID to restore from");
511 ts = cmd_CreateSyntax("diskrestore", bc_DiskRestoreCmd, NULL,
512 "restore partition");
513 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED,
514 "machine to restore");
515 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED,
516 "partition to restore");
517 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offset");
519 cmd_AddParm(ts, "-newserver", CMD_SINGLE, CMD_OPTIONAL,
520 "destination machine");
521 cmd_AddParm(ts, "-newpartition", CMD_SINGLE, CMD_OPTIONAL,
522 "destination partition");
523 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
524 "new volume name extension");
525 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
529 ts = cmd_CreateSyntax("quit", bc_QuitCmd, NULL, "leave the program");
531 ts = cmd_CreateSyntax("volsetrestore", bc_VolsetRestoreCmd, NULL,
532 "restore a set of volumes");
533 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume set name");
534 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "file name");
535 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offset");
536 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
537 "new volume name extension");
538 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
542 ts = cmd_CreateSyntax("addhost", bc_AddHostCmd, NULL, "add host to config");
543 cmd_AddParm(ts, "-tapehost", CMD_SINGLE, CMD_REQUIRED,
544 "tape machine name");
545 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
550 ts = cmd_CreateSyntax("delhost", bc_DeleteHostCmd, NULL,
551 "delete host to config");
552 cmd_AddParm(ts, "-tapehost", CMD_SINGLE, CMD_REQUIRED,
553 "tape machine name");
554 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
559 ts = cmd_CreateSyntax("listhosts", bc_ListHostsCmd, NULL,
560 "list config hosts");
564 ts = cmd_CreateSyntax("jobs", bc_JobsCmd, NULL, "list running jobs");
566 ts = cmd_CreateSyntax("kill", bc_KillCmd, NULL, "kill running job");
567 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_REQUIRED,
568 "job ID or dump set name");
570 ts = cmd_CreateSyntax("listvolsets", bc_ListVolSetCmd, NULL,
572 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume set name");
576 ts = cmd_CreateSyntax("listdumps", bc_ListDumpScheduleCmd, NULL,
577 "list dump schedules");
581 ts = cmd_CreateSyntax("addvolset", bc_AddVolSetCmd, NULL,
582 "create a new volume set");
583 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
584 cmd_AddParm(ts, "-temporary", CMD_FLAG, CMD_OPTIONAL,
585 "temporary volume set");
589 ts = cmd_CreateSyntax("status", bc_GetTapeStatusCmd, NULL,
590 "get tape coordinator status");
591 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
596 ts = cmd_CreateSyntax("delvolset", bc_DeleteVolSetCmd, NULL,
597 "delete a volume set");
598 cmd_AddParm(ts, "-name", CMD_LIST, CMD_REQUIRED, "volume set name");
602 ts = cmd_CreateSyntax("addvolentry", bc_AddVolEntryCmd, NULL,
603 "add a new volume entry");
604 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
605 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "machine name");
606 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED, "partition name");
607 cmd_AddParm(ts, "-volumes", CMD_SINGLE, CMD_REQUIRED,
608 "volume name (regular expression)");
612 ts = cmd_CreateSyntax("delvolentry", bc_DeleteVolEntryCmd, NULL,
613 "delete a volume set sub-entry");
614 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
615 cmd_AddParm(ts, "-entry", CMD_SINGLE, CMD_REQUIRED, "volume set index");
619 ts = cmd_CreateSyntax("adddump", bc_AddDumpCmd, NULL, "add dump schedule");
620 cmd_AddParm(ts, "-dump", CMD_LIST, CMD_REQUIRED, "dump level name");
621 cmd_AddParm(ts, "-expires", CMD_LIST, CMD_OPTIONAL, "expiration date");
625 ts = cmd_CreateSyntax("deldump", bc_DeleteDumpCmd, NULL,
626 "delete dump schedule");
627 cmd_AddParm(ts, "-dump", CMD_SINGLE, CMD_REQUIRED, "dump level name");
631 ts = cmd_CreateSyntax("labeltape", bc_LabelTapeCmd, NULL, "label a tape");
632 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL,
633 "AFS tape name, defaults to NULL");
634 cmd_AddParm(ts, "-size", CMD_SINGLE, CMD_OPTIONAL,
635 "tape size in Kbytes, defaults to size in tapeconfig");
636 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
638 cmd_AddParm(ts, "-pname", CMD_SINGLE, CMD_OPTIONAL,
639 "permanent tape name");
643 ts = cmd_CreateSyntax("readlabel", bc_ReadLabelCmd, NULL,
644 "read the label on tape");
645 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
650 ts = cmd_CreateSyntax("scantape", bc_ScanDumpsCmd, NULL,
651 "dump information recovery from tape");
652 cmd_AddParm(ts, "-dbadd", CMD_FLAG, CMD_OPTIONAL,
653 "add information to the database");
654 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
659 ts = cmd_CreateSyntax("volinfo", bc_dblookupCmd, NULL,
660 "query the backup database");
661 cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_REQUIRED, "volume name");
665 ts = cmd_CreateSyntax("setexp", bc_SetExpCmd, NULL,
666 "set/clear dump expiration dates");
667 cmd_AddParm(ts, "-dump", CMD_LIST, CMD_REQUIRED, "dump level name");
668 cmd_AddParm(ts, "-expires", CMD_LIST, CMD_OPTIONAL, "expiration date");
672 ts = cmd_CreateSyntax("savedb", bc_saveDbCmd, NULL, "save backup database");
673 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
675 cmd_AddParm(ts, "-archive", CMD_LIST, CMD_OPTIONAL, "date time");
679 ts = cmd_CreateSyntax("restoredb", bc_restoreDbCmd, NULL,
680 "restore backup database");
681 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
686 ts = cmd_CreateSyntax("dumpinfo", bc_dumpInfoCmd, NULL,
687 "provide information about a dump in the database");
688 cmd_AddParm(ts, "-ndumps", CMD_SINGLE, CMD_OPTIONAL, "no. of dumps");
689 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "dump id");
690 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
691 "detailed description");
695 ts = cmd_CreateSyntax("dbverify", bc_dbVerifyCmd, NULL,
696 "check ubik database integrity");
697 cmd_AddParm(ts, "-detail", CMD_FLAG, CMD_OPTIONAL, "additional details");
701 ts = cmd_CreateSyntax("deletedump", bc_deleteDumpCmd, NULL,
702 "delete dumps from the database");
703 cmd_AddParm(ts, "-dumpid", CMD_LIST, CMD_OPTIONAL, "dump id");
704 cmd_AddParm(ts, "-from", CMD_LIST, CMD_OPTIONAL, "date time");
705 cmd_AddParm(ts, "-to", CMD_LIST, CMD_OPTIONAL, "date time");
706 cmd_AddParm(ts, "-port", CMD_SINGLE, CMD_OPTIONAL, "TC port offset");
707 cmd_AddParm(ts, "-groupid", CMD_SINGLE, CMD_OPTIONAL, "group ID");
708 cmd_AddParm(ts, "-dbonly", CMD_FLAG, CMD_OPTIONAL,
709 "delete the dump from the backup database only");
710 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
711 "always delete from backup database");
712 cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL,
713 "Just list the dumps");
718 ts = cmd_CreateSyntax("interactive", bc_interactCmd, NULL,
719 "enter interactive mode");
723 * Now execute the command.
727 targv[targc++] = argv[0];
729 targv[targc++] = "interactive";
730 for (i = 1; i < argc; i++)
731 targv[targc++] = argv[i];
733 code = doDispatch(targc, targv, 1);
735 if (!interact || !bcInit) { /* Non-interactive mode */
739 code = bc_WaitForNoJobs(); /* wait for any jobs to finish */
740 exit(code); /* and exit */
743 /* Iterate on command lines, interpreting user commands (interactive mode) */
751 while ((ret = LWP_GetLine(lineBuffer, sizeof(lineBuffer))) == 0)
752 printf("%s: Command line too long\n", whoami); /* line too long */
755 return 0; /* Got EOF */
757 if (!LineIsBlank(lineBuffer)) {
758 code = cmd_ParseLine(lineBuffer, targv, &targc, MAXV);
760 afs_com_err(whoami, code, "; Can't parse line: '%s'",
761 afs_error_message(code));
763 doDispatch(targc, targv, 1);