From 4e723e6d9a422f5130c9d185918a088612027e62 Mon Sep 17 00:00:00 2001 From: Simon Wilkinson Date: Sat, 2 Apr 2011 13:33:36 +0100 Subject: [PATCH] Replace afs_ctime with strftime and friends Replace our local afs_ctime() hack with strftime and localtime_r, which roken can provide for us if necessary. This avoids the compatibility problems inherent with ctime_r, and removes another bit of platform compatibility goo from libutil. Change-Id: I18ed36cc4dce9aa354ad1398710ab7db83c814a2 Reviewed-on: http://gerrit.openafs.org/4426 Tested-by: BuildBot Reviewed-by: Jeffrey Altman --- src/audit/audit.c | 9 ++++----- src/butc/lwps.c | 20 +++++++++++--------- src/kauth/kautils.c | 16 ++++++++++------ src/rxdebug/NTMakefile | 3 ++- src/ubik/NTMakefile | 3 ++- src/util/afsutil.h | 27 --------------------------- src/util/assert.c | 8 ++++---- src/util/serverLog.c | 10 +++++----- src/viced/host.c | 22 +++++++++++++++------- src/viced/viced.c | 39 +++++++++++++++++++++------------------ src/vlserver/vlclient.c | 5 +++-- 11 files changed, 77 insertions(+), 85 deletions(-) diff --git a/src/audit/audit.c b/src/audit/audit.c index 5f74906..3bb5f76 100644 --- a/src/audit/audit.c +++ b/src/audit/audit.c @@ -170,16 +170,15 @@ printbuf(int rec, char *audEvent, char *afsName, afs_int32 hostId, int num = LogThreadNum(); struct in_addr hostAddr; time_t currenttime; - char *timeStamp; char tbuffer[26]; + struct tm tm; /* Don't print the timestamp or thread id if we recursed */ if (rec == 0) { currenttime = time(0); - timeStamp = afs_ctime(¤ttime, tbuffer, - sizeof(tbuffer)); - timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */ - audit_ops->append_msg(timeStamp); + if (strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y ", + localtime_r(¤ttime, &tm)) !=0) + audit_ops->append_msg(tbuffer); if (num > -1) audit_ops->append_msg("[%d] ", num); diff --git a/src/butc/lwps.c b/src/butc/lwps.c index 1124f11..cce1bc7 100644 --- a/src/butc/lwps.c +++ b/src/butc/lwps.c @@ -195,19 +195,20 @@ 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); PrintLogStr(logIO, error1, error2, str); if (lastPass && lastLogIO) { - fprintf(lastLogIO, "%s: ", timestr); + fprintf(lastLogIO, "%s: ", tbuffer); if (task) fprintf(lastLogIO, "Task %u: ", task); PrintLogStr(lastLogIO, error1, error2, str); @@ -251,12 +252,13 @@ 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) diff --git a/src/kauth/kautils.c b/src/kauth/kautils.c index ddf92ee..b17b1e2 100644 --- a/src/kauth/kautils.c +++ b/src/kauth/kautils.c @@ -167,14 +167,18 @@ ka_timestr(afs_int32 time, char *tstr, afs_int32 tlen) { char tbuffer[32]; /* need at least 26 bytes */ time_t passtime; /* modern systems have 64 bit time */ + struct tm tm; - if (!time) - strcpy(tstr, "no date"); /* special case this */ - else if (time == NEVERDATE) + passtime = time; + + if (time == NEVERDATE) strcpy(tstr, "never"); else { - passtime = time; - strncpy(tstr, afs_ctime(&passtime, tbuffer, sizeof(tbuffer)), tlen); - tstr[strlen(tstr) - 1] = '\0'; /* punt the newline character */ + if (!time || strftime(tbuffer, sizeof(tbuffer), "%c", + localtime_r(&passtime, &tm)) == 0) + strcpy(tstr, "no date"); + else { + strncpy(tstr, tbuffer, tlen); + } } } diff --git a/src/rxdebug/NTMakefile b/src/rxdebug/NTMakefile index 63ba5e8..96bfeec 100644 --- a/src/rxdebug/NTMakefile +++ b/src/rxdebug/NTMakefile @@ -24,7 +24,8 @@ RXDLIBS = $(LIBDIR)\afs\afscmd.lib \ $(LIBDIR)\afsrx.lib \ $(LIBDIR)\afslwp.lib \ $(LIBDIR)\afs\afsutil.lib \ - $(LIBDIR)\afs\afsreg.lib + $(LIBDIR)\afs\afsreg.lib \ + $(LIBDIR)\afsroken.lib $(OUT)\rxdebug.res: rxdebug.rc $(VERSFILE).h $(RC) /Fo$*.RES $(*F).rc diff --git a/src/ubik/NTMakefile b/src/ubik/NTMakefile index cc1ee69..05170c3 100644 --- a/src/ubik/NTMakefile +++ b/src/ubik/NTMakefile @@ -135,7 +135,8 @@ UDBG_EXELIBS =\ $(DESTDIR)\lib\afslwp.lib \ $(DESTDIR)\lib\afs\afscom_err.lib \ $(DESTDIR)\lib\afs\afscmd.lib \ - $(DESTDIR)\lib\afs\afsutil.lib + $(DESTDIR)\lib\afs\afsutil.lib \ + $(DESTDIR)\lib\afsroken.lib $(RS_UDBG_EXEFILE): $(UDBG_EXEOBJS) $(UDBG_EXELIBS) diff --git a/src/util/afsutil.h b/src/util/afsutil.h index 6a06510..fe4ad9d 100644 --- a/src/util/afsutil.h +++ b/src/util/afsutil.h @@ -55,33 +55,6 @@ extern int OpenLog(const char *filename); extern int ReOpenLog(const char *fileName); extern void SetupLogSignals(void); -/* special version of ctime that clobbers a *different static variable, so - * that ViceLog can call ctime and not cause buffer confusion. - */ -extern char *vctime(const time_t * atime); - -/* Need a thead safe ctime for pthread builds. Use std ctime for LWP */ -#if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV) -#if defined(AFS_SUN5_ENV) && !defined(_POSIX_PTHREAD_SEMANTICS) && (_POSIX_C_SOURCE - 0 < 199506L) -#define afs_ctime(C, B, L) ctime_r(C, B, L) -#else -/* Cast is for platforms which do not prototype ctime_r */ -#define afs_ctime(C, B, L) (char*)ctime_r(C, B) -#endif /* AFS_SUN5_ENV */ -#else /* AFS_PTHREAD_ENV && !AFS_NT40_ENV */ -static_inline char * -afs_ctime(const time_t *C, char *B, size_t S) { -#if !defined(AFS_NT40_ENV) || (_MSC_VER < 1400) - strncpy(B, ctime(C), (S-1)); - B[S-1] = '\0'; -#else - ctime_s(B, S, C); -#endif - return B; -} -#endif /* AFS_PTHREAD_ENV && !AFS_NT40_ENV */ - - /* abort the current process. */ #ifdef AFS_NT40_ENV #define afs_abort() afs_NTAbort() diff --git a/src/util/assert.c b/src/util/assert.c index 3ef2c79..03af01b 100644 --- a/src/util/assert.c +++ b/src/util/assert.c @@ -24,17 +24,17 @@ afs_NTAbort(void) #endif #define TIMESTAMP_BUFFER_SIZE 26 /* including the null */ -#define TIMESTAMP_NEWLINE_POS 24 /* offset to the newline placed by ctime */ void AssertionFailed(char *file, int line) { char tdate[TIMESTAMP_BUFFER_SIZE]; time_t when; + struct tm tm; - time(&when); - (void)afs_ctime(&when, tdate, sizeof(tdate)); - tdate[TIMESTAMP_NEWLINE_POS] = ' '; + when = time(NULL); + strftime(tdate, sizeof(tdate), "%a %b %d %T %Y", + localtime_r(&when, &tm)); fprintf(stderr, "%sAssertion failed! file %s, line %d.\n", tdate, file, line); fflush(stderr); diff --git a/src/util/serverLog.c b/src/util/serverLog.c index 06c8db2..710502c 100644 --- a/src/util/serverLog.c +++ b/src/util/serverLog.c @@ -100,16 +100,16 @@ void vFSLog(const char *format, va_list args) { time_t currenttime; - char *timeStamp; char tbuffer[1024]; char *info; size_t len; + struct tm tm; int num; - currenttime = time(0); - timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer)); - timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */ - info = &timeStamp[25]; + currenttime = time(NULL); + len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y ", + localtime_r(¤ttime, &tm)); + info = &tbuffer[len+1]; if (mrafsStyleLogs || threadIdLogs) { num = (*threadNumProgram) (); diff --git a/src/viced/host.c b/src/viced/host.c index 53330a3..7d12aa3 100644 --- a/src/viced/host.c +++ b/src/viced/host.c @@ -2733,6 +2733,7 @@ h_PrintClient(struct host *host, int flags, void *rock) char tbuffer[32]; char hoststr[16]; time_t LastCall, expTime; + struct tm tm; H_LOCK; LastCall = host->LastCall; @@ -2740,22 +2741,24 @@ h_PrintClient(struct host *host, int flags, void *rock) H_UNLOCK; return flags; } + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&LastCall, &tm)); snprintf(tmpStr, sizeof tmpStr, "Host %s:%d down = %d, LastCall %s", afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), (host->hostFlags & VENUSDOWN), - afs_ctime(&LastCall, tbuffer, sizeof(tbuffer))); + tbuffer); (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file); for (client = host->FirstClient; client; client = client->next) { if (!client->deleted) { expTime = client->expTime; + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&expTime, &tm)); snprintf(tmpStr, sizeof tmpStr, " user id=%d, name=%s, sl=%s till %s", client->ViceId, h_UserName(client), client->authClass ? "Authenticated" : "Not authenticated", - client->authClass ? afs_ctime(&expTime, tbuffer, - sizeof(tbuffer)) - : "No Limit\n"); + client->authClass ? tbuffer : "No Limit\n"); (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file); snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [", client->CPS.prlist_len); @@ -2788,6 +2791,7 @@ h_PrintClients(void) time_t now; char tmpStr[256]; char tbuffer[32]; + struct tm tm; StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w"); @@ -2798,8 +2802,10 @@ h_PrintClients(void) return; } now = FT_ApproxTime(); + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&now, &tm)); snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n", - afs_ctime(&now, tbuffer, sizeof(tbuffer))); + tbuffer); (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file); h_Enumerate(h_PrintClient, (char *)file); STREAM_REALLYCLOSE(file); @@ -2861,6 +2867,7 @@ h_DumpHosts(void) StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w"); char tmpStr[256]; char tbuffer[32]; + struct tm tm; if (file == NULL) { ViceLog(0, @@ -2869,8 +2876,9 @@ h_DumpHosts(void) return; } now = FT_ApproxTime(); - snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n", - afs_ctime(&now, tbuffer, sizeof(tbuffer))); + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&now, &tm)); + snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n", tbuffer); (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file); h_Enumerate(h_DumpHost, (char *)file); STREAM_REALLYCLOSE(file); diff --git a/src/viced/viced.c b/src/viced/viced.c index 7565e62..fa7e3c6 100644 --- a/src/viced/viced.c +++ b/src/viced/viced.c @@ -474,9 +474,11 @@ FiveMinuteCheckLWP(void *unused) #endif if (printBanner && (++msg & 1)) { /* Every 10 minutes */ time_t now = FT_ApproxTime(); + struct tm tm; + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&now, &tm)); ViceLog(2, - ("File server is running at %s\n", - afs_ctime(&now, tbuffer, sizeof(tbuffer)))); + ("File server is running at %s\n", tbuffer)); } #ifdef AFS_DEMAND_ATTACH_FS FS_STATE_WRLOCK; @@ -689,15 +691,16 @@ PrintCounters(void) int workstations, activeworkstations, delworkstations; int processSize = 0; char tbuffer[32]; + struct tm tm; #ifdef AFS_DEMAND_ATTACH_FS int stats_flags = 0; #endif FT_GetTimeOfDay(&tpl, 0); Statistics = 1; - ViceLog(0, - ("Vice was last started at %s\n", - afs_ctime(&StartTime, tbuffer, sizeof(tbuffer)))); + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&StartTime, &tm)); + ViceLog(0, ("Vice was last started at %s\n", tbuffer)); #ifdef AFS_DEMAND_ATTACH_FS if (LogLevel >= 125) { @@ -771,7 +774,8 @@ ShutdownWatchdogLWP(void *unused) void ShutDownAndCore(int dopanic) { - time_t now = time(0); + time_t now = time(NULL); + struct tm tm; char tbuffer[32]; if (dopanic) { @@ -809,9 +813,9 @@ ShutDownAndCore(int dopanic) FS_STATE_UNLOCK; #endif - ViceLog(0, - ("Shutting down file server at %s", - afs_ctime(&now, tbuffer, sizeof(tbuffer)))); + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&now, &tm)); + ViceLog(0, ("Shutting down file server at %s", tbuffer)); if (dopanic) ViceLog(0, ("ABNORMAL SHUTDOWN, see core file.\n")); DFlush(); @@ -859,14 +863,12 @@ ShutDownAndCore(int dopanic) fflush(debugFile); } now = time(0); + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&now, &tm)); if (dopanic) { - ViceLog(0, - ("File server has terminated abnormally at %s\n", - afs_ctime(&now, tbuffer, sizeof(tbuffer)))); + ViceLog(0, ("File server has terminated abnormally at %s\n", tbuffer)); } else { - ViceLog(0, - ("File server has terminated normally at %s\n", - afs_ctime(&now, tbuffer, sizeof(tbuffer)))); + ViceLog(0, ("File server has terminated normally at %s\n", tbuffer)); } if (dopanic) /* XXX pass in file and line? */ @@ -1972,6 +1974,7 @@ main(int argc, char *argv[]) #endif int curLimit; time_t t; + struct tm tm; afs_uint32 rx_bindhost; VolumePackageOptions opts; @@ -2380,9 +2383,9 @@ main(int argc, char *argv[]) } t = tp.tv_sec; - ViceLog(0, - ("File Server started %s", - afs_ctime(&t, tbuffer, sizeof(tbuffer)))); + strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y", + localtime_r(&t, &tm)); + ViceLog(0, ("File Server started %s", tbuffer)); #if FS_STATS_DETAILED afs_FullPerfStats.det.epoch.tv_sec = StartTime = tp.tv_sec; #endif diff --git a/src/vlserver/vlclient.c b/src/vlserver/vlclient.c index 8c41d18..4860c00 100644 --- a/src/vlserver/vlclient.c +++ b/src/vlserver/vlclient.c @@ -1274,9 +1274,10 @@ dump_stats(vldstats *stats, vital_vlheader *vital_header) int i; char strg[30]; time_t start_time = stats->start_time; + struct tm tm; - afs_ctime(&start_time, strg, sizeof(strg)); - strg[strlen(strg) - 1] = 0; + strftime(strg, sizeof(strg), "%a %b %d %T %Y", + localtime_r(&start_time, &tm)); printf("Dynamic statistics stats (starting time: %s):\n", strg); printf("OpcodeName\t# Requests\t# Aborts\n"); for (i = 0; i < VL_NUMBER_OPCODESX; i++) -- 1.9.4