butc: initialize startTime before it is used
[openafs.git] / src / butc / lwps.c
index c4fded6..f479b78 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <afs/procmgmt.h>
+#include <roken.h>
 
-#include <sys/types.h>
-#include <string.h>
 #ifdef AFS_NT40_ENV
-#include <winsock2.h>
 #include <conio.h>
-#else
-#include <sys/file.h>
-#include <netinet/in.h>
-#include <netdb.h>
 #endif
 
-#include <afs/procmgmt.h>
 #include <rx/xdr.h>
 #include <rx/rx.h>
-#include <time.h>
 #include <lwp.h>
 #include <lock.h>
 #include <afs/tcdata.h>
 #include <afs/bubasics.h>      /* PA */
 #include <afs/budb_client.h>
 #include <afs/bucoord_prototypes.h>
+#include <afs/butm_prototypes.h>
 #include <afs/volser.h>
+#include <afs/volser_prototypes.h>
 #include <afs/com_err.h>
-#include "error_macros.h"
 #include <afs/afsutil.h>
-#include <errno.h>
+
+#include "error_macros.h"
 #include "butc_xbsa.h"
 #include "butc_internal.h"
 
@@ -121,7 +116,6 @@ extern struct tapeConfig globalTapeConfig;
 extern struct deviceSyncNode *deviceLatch;
 extern char globalCellName[];
 struct timeval tp;
-struct timezone tzp;
 
 /* forward declaration */
 afs_int32 readVolumeHeader(char *, afs_int32, struct volumeHeader *);
@@ -138,14 +132,11 @@ static struct TapeBlock tapeBlock;
 char tapeVolumeHT[sizeof(struct volumeHeader) + 2 * sizeof(char)];
 
 void
-PrintLog(log, error1, error2, str, a, b, c, d, e, f, g, h, i, j)
-     FILE *log;
-     afs_int32 error1, error2;
-     char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
+PrintLogStr(FILE *log, afs_int32 error1, afs_int32 error2, char *str)
 {
     char *err1, *err2;
 
-    fprintf(log, str, a, b, c, d, e, f, g, h, i, j);
+    fprintf(log, "%s", str);
     if (error1) {
        err2 = "vols";
        switch (error1) {
@@ -199,74 +190,110 @@ PrintLog(log, error1, error2, str, a, b, c, d, e, f, g, h, i, j)
 }
 
 void
-TapeLog(debug, task, error1, error2, str, a, b, c, d, e, f, g, h, i, j)
-     int debug;
-     afs_int32 task, error1, error2;
-     char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
+TapeLogStr(int debug, afs_int32 task, afs_int32 error1, afs_int32 error2,
+          char *str)
 {
     time_t now;
-    char tbuffer[32], *timestr;
+    char tbuffer[32];
+    struct tm tm;
 
     now = time(0);
-    timestr = afs_ctime(&now, tbuffer, sizeof(tbuffer));
-    timestr[24] = '\0';
+    if (strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y",
+                localtime_r(&now, &tm)) != 0)
+       fprintf(logIO, "%s: ", tbuffer);
 
-    fprintf(logIO, "%s: ", timestr);
     if (task)
        fprintf(logIO, "Task %u: ", task);
-    PrintLog(logIO, error1, error2, str, a, b, c, d, e, f, g, h, i, j);
+    PrintLogStr(logIO, error1, error2, str);
 
     if (lastPass && lastLogIO) {
-       fprintf(lastLogIO, "%s: ", timestr);
+       fprintf(lastLogIO, "%s: ", tbuffer);
        if (task)
            fprintf(lastLogIO, "Task %u: ", task);
-       PrintLog(lastLogIO, error1, error2, str, a, b, c, d, e, f, g, h, i,
-                j);
+       PrintLogStr(lastLogIO, error1, error2, str);
     }
 
     /* Now print to the screen if debug level requires */
     if (debug <= debugLevel)
-       PrintLog(stdout, error1, error2, str, a, b, c, d, e, f, g, h, i, j);
+       PrintLogStr(stdout, error1, error2, str);
+}
+
+void
+TapeLog(int debug, afs_int32 task, afs_int32 error1, afs_int32 error2,
+       char *fmt, ...)
+{
+    char tmp[1024];
+    va_list ap;
+
+    va_start(ap, fmt);
+    vsnprintf(tmp, sizeof(tmp), fmt, ap);
+    va_end(ap);
+
+    TapeLogStr(debug, task, error1, error2, tmp);
 }
 
 void
-TLog(task, str, a, b, c, d, e, f, g, h, i, j)
-     afs_int32 task;
-     char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
+TLog(afs_int32 task, char *fmt, ...)
 {
+    char tmp[1024];
+    va_list ap;
+
+    va_start(ap, fmt);
+    vsnprintf(tmp, sizeof(tmp), fmt, ap);
+    va_end(ap);
+
     /* Sends message to TapeLog and stdout */
-    TapeLog(0, task, 0, 0, str, a, b, c, d, e, f, g, h, i, j);
+    TapeLogStr(0, task, 0, 0, tmp);
 }
 
 void
-ErrorLog(debug, task, error1, error2, str, a, b, c, d, e, f, g, h, i, j)
-     int debug;
-     afs_int32 task, error1, error2;
-     char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
+ErrorLogStr(int debug, afs_int32 task, afs_int32 error1, afs_int32 error2,
+           char *errStr)
 {
     time_t now;
-    char tbuffer[32], *timestr;
+    char tbuffer[32];
+    struct tm tm;
 
     now = time(0);
-    timestr = afs_ctime(&now, tbuffer, sizeof(tbuffer));
-    timestr[24] = '\0';
-    fprintf(ErrorlogIO, "%s: ", timestr);
+    if (strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y",
+                localtime_r(&now, &tm)) != 0)
+       fprintf(ErrorlogIO, "%s: ", tbuffer);
 
     /* Print the time and task number */
     if (task)
        fprintf(ErrorlogIO, "Task %u: ", task);
-    PrintLog(ErrorlogIO, error1, error2, str, a, b, c, d, e, f, g, h, i, j);
 
-    TapeLog(debug, task, error1, error2, str, a, b, c, d, e, f, g, h, i, j);
+    PrintLogStr(ErrorlogIO, error1, error2, errStr);
+    TapeLogStr(debug, task, error1, error2, errStr);
 }
 
 void
-ELog(task, str, a, b, c, d, e, f, g, h, i, j)
-     afs_int32 task;
-     char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
+ErrorLog(int debug, afs_int32 task, afs_int32 error1, afs_int32 error2,
+        char *fmt, ...)
 {
+    char tmp[1024];
+    va_list ap;
+
+    va_start(ap, fmt);
+    vsnprintf(tmp, sizeof(tmp), fmt, ap);
+    va_end(ap);
+
+    ErrorLogStr(debug, task, error1, error2, tmp);
+
+}
+
+void
+ELog(afs_int32 task, char *fmt, ...)
+{
+    char tmp[1024];
+    va_list ap;
+
+    va_start(ap, fmt);
+    vsnprintf(tmp, sizeof(tmp), fmt, ap);
+    va_end(ap);
+
     /* Sends message to ErrorLog, TapeLog and stdout */
-    ErrorLog(0, task, 0, 0, str, a, b, c, d, e, f, g, h, i, j);
+    ErrorLog(0, task, 0, 0, "%s", tmp);
 }
 
 /* first proc called by anybody who intends to use the device */
@@ -288,122 +315,6 @@ LeaveDeviceQueue(struct deviceSyncNode *devLatch)
 #define BELLTIME 60            /* 60 seconds before a bell rings */
 #define BELLCHAR 7             /* ascii for bell */
 
-
-#ifdef AFS_PTHREAD_ENV
-#ifdef AFS_NT40_ENV
-/* WaitForKeystroke : Wait until a key has been struck or time (secconds)
- * runs out and return to caller. The NT version of this function will return
- * immediately after a key has been pressed (doesn't wait for cr).
- * Input:
- *   seconds: wait for <seconds> seconds before returning. If seconds < 0,
- *            wait infinitely.
- * Return Value:
- *    1:  Keyboard input available
- *    0:  seconds elapsed. Timeout.
- *
- * STOLEN FROM LWP_WaitForKeystroke()
- */
-int
-WaitForKeystroke(int seconds)
-{
-    time_t startTime, nowTime;
-    double timeleft = 1;
-    struct timeval twait;
-
-    time(&startTime);
-    twait.tv_sec = 0;
-    twait.tv_usec = 250;
-    if (seconds >= 0)
-       timeleft = seconds;
-
-    do {
-       /* check if we have a keystroke */
-       if (_kbhit())
-           return 1;
-       if (timeleft == 0)
-           break;
-
-       /* sleep for  LWP_KEYSTROKE_DELAY ms and let other
-        * process run some*/
-       select(0, 0, 0, 0, &twait);
-
-       if (seconds > 0) {      /* we only worry about elapsed time if 
-                                * not looping forever (seconds < 0) */
-           time(&nowTime);
-           timeleft = seconds - difftime(nowTime, startTime);
-       }
-    } while (timeleft > 0);
-    return 0;
-}
-#else /* AFS_NT40)ENV */
-extern int WaitForKeystroke(int);
-/*
- *      STOLEN FROM LWP_WaitForKeystroke()
- */
-int
-WaitForKeystroke(int seconds)
-{
-    fd_set rdfds;
-    int code;
-    struct timeval twait;
-    struct timeval *tp = NULL;
-
-#ifdef AFS_LINUX20_ENV
-    if (stdin->_IO_read_ptr < stdin->_IO_read_end)
-       return 1;
-#else
-    if (stdin->_cnt > 0)
-       return 1;
-#endif
-    FD_ZERO(&rdfds);
-    FD_SET(fileno(stdin), &rdfds);
-
-    if (seconds >= 0) {
-       twait.tv_sec = seconds;
-       twait.tv_usec = 0;
-       tp = &twait;
-    }
-    code = select(1 + fileno(stdin), &rdfds, NULL, NULL, tp);
-    return (code == 1) ? 1 : 0;
-}
-#endif
-
-/* GetResponseKey() - Waits for a specified period of time and
- * returns a char when one has been typed by the user.
- * Input:
- *    seconds - how long to wait for a key press.
- *    *key    - char entered by user
- * Return Values: 
- *    0 - Time ran out before the user typed a key.
- *    1 - Valid char is being returned.
- *
- *    STOLEN FROM LWP_GetResponseKey();
- */
-int
-GetResponseKey(int seconds, char *key)
-{
-    int rc;
-
-    if (key == NULL)
-       return 0;               /* need space to store char */
-    fflush(stdin);             /* flush all existing data and start anew */
-
-    rc = WaitForKeystroke(seconds);
-    if (rc == 0) {             /* time ran out */
-       *key = 0;
-       return rc;
-    }
-
-    /* now read the char. */
-#ifdef AFS_NT40_ENV
-    *key = getche();           /* get char and echo it to screen */
-#else
-    *key = getchar();
-#endif
-    return rc;
-}
-#endif /* AFS_PTHREAD_ENV */
-
 /*
  * FFlushInput
  *     flush all input
@@ -418,12 +329,7 @@ FFlushInput(void)
     fflush(stdin);
 
     while (1) {
-#ifdef AFS_PTHREAD_ENV
-       w = WaitForKeystroke(0);
-#else
        w = LWP_WaitForKeystroke(0);
-#endif /* AFS_PTHREAD_ENV */
-
        if (w) {
 #ifdef AFS_NT40_ENV
            getche();
@@ -658,7 +564,7 @@ PrintPrompt(int flag, char *name, int dumpid)
        break;
 
     case SAVEDBOPCODE:         /* Mount for savedb */
-       printf("Please insert a writeable tape %s for the database dump",
+       printf("Please insert a writable tape %s for the database dump",
               tapename);
        break;
 
@@ -669,7 +575,7 @@ PrintPrompt(int flag, char *name, int dumpid)
 }
 
 /* PromptForTape
- *     Prompt the operator to change the tape. 
+ *     Prompt the operator to change the tape.
  *      Use to be a void routine but now returns an error. Some calls
  *      don't use the error code.
  * notes:
@@ -679,7 +585,7 @@ afs_int32
 PromptForTape(int flag, char *name, afs_uint32 dbDumpId, afs_uint32 taskId,
              int tapecount)
 {
-    register afs_int32 code = 0;
+    afs_int32 code = 0;
     afs_int32 wcode;
     afs_int32 start = 0;
     char inchr;
@@ -771,11 +677,7 @@ PromptForTape(int flag, char *name, afs_uint32 dbDumpId, afs_uint32 taskId,
                putchar(BELLCHAR);
                fflush(stdout);
            }
-#ifdef AFS_PTHREAD_ENV
-           wcode = GetResponseKey(5, &inchr);  /* inchr stores key read */
-#else
            wcode = LWP_GetResponseKey(5, &inchr);      /* inchr stores key read */
-#endif
            if (wcode == 1) {   /* keyboard input is available */
 
                if ((inchr == 'a') || (inchr == 'A')) {
@@ -1069,10 +971,11 @@ GetRestoreTape(afs_int32 taskId, struct butm_tapeInfo *tapeInfoPtr,
 }
 
 afs_int32
-xbsaRestoreVolumeData(struct rx_call *call, struct restoreParams *rparamsPtr)
+xbsaRestoreVolumeData(struct rx_call *call, void *rock)
 {
     afs_int32 code = 0;
 #ifdef xbsa
+    struct restoreParams *rparamsPtr = (struct restoreParams *)rock;
     afs_int32 curChunk, rc;
     afs_uint32 totalWritten;
     afs_int32 headBytes, tailBytes, w;
@@ -1220,8 +1123,9 @@ xbsaRestoreVolumeData(struct rx_call *call, struct restoreParams *rparamsPtr)
  */
 
 afs_int32
-restoreVolumeData(struct rx_call *call, struct restoreParams *rparamsPtr)
+restoreVolumeData(struct rx_call *call, void *rock)
 {
+    struct restoreParams *rparamsPtr = (struct restoreParams *)rock;
     afs_int32 curChunk;
     afs_uint32 totalWritten = 0;
     afs_int32 code = 0;
@@ -1324,7 +1228,7 @@ restoreVolumeData(struct rx_call *call, struct restoreParams *rparamsPtr)
        }
 
        /* lastbuf is last block read and it has nbytes of data
-        * startWbuf is the 2nd to last block read 
+        * startWbuf is the 2nd to last block read
         * Seach for the volume trailer in these two blocks.
         */
        if (lastbuf == startWbuf)
@@ -1366,7 +1270,7 @@ restoreVolumeData(struct rx_call *call, struct restoreParams *rparamsPtr)
        if (!tapeVolTrailer.contd)
            break;              /* We've read the entire volume */
 
-       /* Volume is continued on next tape. 
+       /* Volume is continued on next tape.
         * Step to the next volume fragment and prompt for its tape.
         * When a volume has multiple frags, those frags are on different
         * tapes. So we know that we need to prompt for a tape.
@@ -1734,6 +1638,7 @@ Restorer(void *param) {
     time_t startTime, endTime;
     afs_int32 goodrestore = 0;
 
+    afs_pthread_setname_self("restorer");
     taskId = newNode->taskID;
     setStatus(taskId, DRIVE_WAIT);
     EnterDeviceQueue(deviceLatch);
@@ -1742,6 +1647,7 @@ Restorer(void *param) {
     printf("\n\n");
     TLog(taskId, "Restore\n");
 
+    startTime = time(0);
     memset(&tapeInfo, 0, sizeof(tapeInfo));
     if (!CONF_XBSA) {
        tapeInfo.structVersion = BUTM_MAJORVERSION;
@@ -1777,7 +1683,6 @@ Restorer(void *param) {
        ERROR_EXIT(TC_NOMEMORY);
     memset(bufferBlock, 0, allocbufferSize);
 
-    startTime = time(0);
     for (rparams.frag = 0; (rparams.frag < newNode->arraySize);
         rparams.frag++) {
        RestoreDesc = &Restore[rparams.frag];
@@ -1810,7 +1715,7 @@ Restorer(void *param) {
 
        /* restoreVolume function takes care of all the related fragments
         * spanning various tapes. On return the complete volume has been
-        * restored 
+        * restored
         */
        if (CONF_XBSA) {
            tcode = xbsaRestoreVolume(taskId, RestoreDesc, &rparams);
@@ -1896,7 +1801,7 @@ Restorer(void *param) {
 
     FreeNode(taskId);
     LeaveDeviceQueue(deviceLatch);
-    return (void *)(code);
+    return (void *)(intptr_t)(code);
 }
 
 /* this is just scaffolding, creates new tape label with name <tapeName> */
@@ -1906,7 +1811,6 @@ GetNewLabel(struct butm_tapeInfo *tapeInfoPtr, char *pName, char *AFSName,
            struct butm_tapeLabel *tapeLabel)
 {
     struct timeval tp;
-    struct timezone tzp;
     afs_uint32 size;
 
     memset(tapeLabel, 0, sizeof(struct butm_tapeLabel));
@@ -1918,7 +1822,7 @@ GetNewLabel(struct butm_tapeInfo *tapeInfoPtr, char *pName, char *AFSName,
     } else {
        size = 0;               /* no tape size */
     }
-    gettimeofday(&tp, &tzp);
+    gettimeofday(&tp, NULL);
 
     tapeLabel->structVersion = CUR_TAPE_VERSION;
     tapeLabel->creationTime = tp.tv_sec;
@@ -2066,19 +1970,18 @@ tapeExpired(struct butm_tapeLabel *tapeLabelPtr)
 {
     Date expiration;
     struct timeval tp;
-    struct timezone tzp;
 
     expiration = ExpirationDate(tapeLabelPtr->dumpid);
     if (!expiration)
        expiration = tapeLabelPtr->expirationDate;
 
-    gettimeofday(&tp, &tzp);
+    gettimeofday(&tp, NULL);
     return ((expiration < tp.tv_sec) ? 1 : 0);
 }
 
 /* updateTapeLabel
  *     given the label on the tape, delete any old information from the
- *     database. 
+ *     database.
 
  * Deletes all entries that match the volset.dumpnode
  *     and the dump path.
@@ -2123,7 +2026,7 @@ updateTapeLabel(struct labelTapeIf *labelIfPtr,
 
            if ((strcmp(newLabelPtr->AFSName, "") != 0)
                && (strcmp(oldLabel.pName, "") != 0)) {
-               /* We are setting the AFS name, yet tape 
+               /* We are setting the AFS name, yet tape
                 * has a permanent name (not allowed).
                 */
                TLog(taskId, "Can't label. Tape has permanent label '%s'\n",
@@ -2202,6 +2105,7 @@ Labeller(void *param)
     afs_uint32 taskId;
     afs_int32 code = 0;
 
+    afs_pthread_setname_self("labeller");
     taskId = labelIfPtr->taskId;
     setStatus(taskId, DRIVE_WAIT);
     EnterDeviceQueue(deviceLatch);
@@ -2245,7 +2149,7 @@ Labeller(void *param)
 
     free(labelIfPtr);
     LeaveDeviceQueue(deviceLatch);
-    return (void *)(code);
+    return (void *)(intptr_t)(code);
 }
 
 /* PrintTapeLabel
@@ -2281,7 +2185,7 @@ PrintTapeLabel(struct butm_tapeLabel *labelptr)
     printf("-- End of tape label --\n\n");
 }
 
-/* ReadLabel 
+/* ReadLabel
  *     Read the label from a tape.
  *     Currently prints out a "detailed" summary of the label but passes
  *     back only selected fields.
@@ -2387,7 +2291,7 @@ readVolumeHeader(char *buffer,            /* in - buffer to read header from */
     int nextSplice = sizeof(struct volumeHeader) - firstSplice - padLen;
 
     /* Four cases are to be handled
-     * 
+     *
      * Volume Header (byte alignment)
      * -----------------------
      * Tape   In Core
@@ -2397,7 +2301,7 @@ readVolumeHeader(char *buffer,            /* in - buffer to read header from */
      * Case 3:  1       1
      * Case 4:  1       4
      * -----------------------
-     * 
+     *
      * Case 2 and Case 3 are identical cases and handled the same way.
      * Case 1 and Case 4 are separate cases. In one case the pad needs
      * to be removed and in the other, it needs to be spliced in. The
@@ -2420,7 +2324,9 @@ readVolumeHeader(char *buffer,            /* in - buffer to read header from */
        /* Handle Case 1 */
        memset(&vhptr, 0, sizeof(struct volumeHeader));
        memcpy(&vhptr, buffer + bufloc, firstSplice);
-       memcpy(&vhptr + firstSplice, buffer + bufloc + firstSplice + padLen,
+       /* probably GCC bug 37060; however, no guarantee on length of buffer */
+       tempvhptr = (struct volumeHeader *)(buffer + firstSplice);
+       memcpy(tempvhptr, buffer + bufloc + firstSplice + padLen,
               nextSplice);
        HEADER_CHECKS(vhptr, header);