Improved handling of very long host or program names, or no strftime

This commit is contained in:
Gerhard Rieger 2022-08-12 10:54:25 +02:00
parent 5edcb9b308
commit 9be423ceea
2 changed files with 30 additions and 16 deletions

View file

@ -56,6 +56,9 @@ Porting:
recognize if this function does not exist, e.g. on Musl libc. recognize if this function does not exist, e.g. on Musl libc.
Thanks to Alexander Kanavin and Baruch Siach for sending patches. Thanks to Alexander Kanavin and Baruch Siach for sending patches.
Corrected message format when no strftime() is available; improved
handling of very long host or program names
Testing: Testing:
Unset SOCAT_MAIN_WAIT on informational Socat calls Unset SOCAT_MAIN_WAIT on informational Socat calls

43
error.c
View file

@ -317,8 +317,8 @@ void msg2(
#if HAVE_STRFTIME #if HAVE_STRFTIME
struct tm struct_tm; struct tm struct_tm;
#endif #endif
#define BUFLEN 512 #define MSGLEN 512
char buff[BUFLEN+1], *bufp, *syslp; char buff[MSGLEN+2], *bufp = buff, *syslp;
size_t bytes; size_t bytes;
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
@ -328,12 +328,14 @@ void msg2(
#else #else
epoch = *now; epoch = *now;
#endif #endif
/*! consider caching instead of recalculating many times per second */
#if HAVE_STRFTIME #if HAVE_STRFTIME
bytes = strftime(buff, 20, "%Y/%m/%d %H:%M:%S", localtime_r(&epoch, &struct_tm)); bytes = strftime(bufp, 20, "%Y/%m/%d %H:%M:%S", localtime_r(&epoch, &struct_tm));
buff[bytes] = '\0';
#else #else
bytes = snprintf(buff, 11, F_time, epoch); bytes = snprintf(bufp, 11, F_time, epoch);
#endif #endif
bufp += bytes;
*bufp = '\0';
if (diagopts.micros) { if (diagopts.micros) {
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
micros = now->tv_nsec/1000; micros = now->tv_nsec/1000;
@ -342,29 +344,38 @@ void msg2(
#else #else
micros = 0; micros = 0;
#endif #endif
bytes += sprintf(buff+19, ".%06lu ", micros); bufp += sprintf(bufp, ".%06lu ", micros);
} else { } else {
buff[19] = ' '; buff[20] = '\0'; *bufp++ = ' ';
*bufp = '\0';
} }
bytes = strlen(buff);
bufp = buff + bytes;
if (diagopts.withhostname) { if (diagopts.withhostname) {
bytes = sprintf(bufp, "%s ", diagopts.hostname), bufp+=bytes; bytes = snprintf(bufp, MSGLEN-(bufp-buff), "%s ", diagopts.hostname);
if (bytes >= MSGLEN-(bufp-buff))
bytes = MSGLEN-(bufp-buff)-1;
bufp += bytes;
} }
bytes = sprintf(bufp, "%s["F_pid"] ", diagopts.progname, getpid()); bytes = snprintf(bufp, MSGLEN-(bufp-buff), "%s["F_pid"] ", diagopts.progname, getpid());
if (bytes >= MSGLEN-(bufp-buff))
bytes = MSGLEN-(bufp-buff)-1;
bufp += bytes; bufp += bytes;
syslp = bufp; syslp = bufp; /* syslog prefixes with time etc.itself */
*bufp++ = "DINWEF"[level]; if (bufp < buff+MSGLEN)
*bufp++ = "DINWEF"[level];
#if 0 /* only for debugging socat */ #if 0 /* only for debugging socat */
if (handler) bufp[-1] = tolower((unsigned char)bufp[-1]); /* for debugging, low chars indicate messages from signal handlers */ if (handler) bufp[-1] = tolower((unsigned char)bufp[-1]); /* for debugging, low chars indicate messages from signal handlers */
#endif #endif
*bufp++ = ' '; if (bufp < buff+MSGLEN)
strncpy(bufp, text, BUFLEN-(bufp-buff)-1); *bufp++ = ' ';
strcat(bufp, "\n"); strncpy(bufp, text, MSGLEN-(bufp-buff));
bufp = strchr(bufp, '\0');
strcpy(bufp, "\n");
_msg(level, buff, syslp); _msg(level, buff, syslp);
if (level >= diagopts.exitlevel) { if (level >= diagopts.exitlevel) {
if (E_NOTICE >= diagopts.msglevel) { if (E_NOTICE >= diagopts.msglevel) {
if ((syslp - buff) + 16 > MSGLEN+1)
syslp = buff + MSGLEN - 15;
snprintf_r(syslp, 16, "N exit(%d)\n", exitcode?exitcode:(diagopts.exitstatus?diagopts.exitstatus:1)); snprintf_r(syslp, 16, "N exit(%d)\n", exitcode?exitcode:(diagopts.exitstatus?diagopts.exitstatus:1));
_msg(E_NOTICE, buff, syslp); _msg(E_NOTICE, buff, syslp);
} }