aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2024-04-09 12:16:21 +0200
committerKarel Zak <kzak@redhat.com>2024-04-09 12:16:21 +0200
commit985e7a748ae66c92bc4793b4de9946c6e1ba0e24 (patch)
tree8b1e4de587bb5c59468eb66124cdf826edb1854b
parent4a5b4cfccadbc687c69497f892aea496cc4c9209 (diff)
parent6227b2b0585ee2ccf224cf70c7144296a814a4ab (diff)
downloadutil-linux-985e7a748ae66c92bc4793b4de9946c6e1ba0e24.tar.gz
Merge branch 'logger/fixes' of https://github.com/t-8ch/util-linux
* 'logger/fixes' of https://github.com/t-8ch/util-linux: logger: correctly format tv_usec logger: rework error handling in logger_gettimeofday() logger: handle failures of gettimeofday()
-rw-r--r--misc-utils/logger.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index e1d270de8d..f696287d7a 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -154,13 +154,24 @@ static inline int logger_gettimeofday(struct timeval *tv, struct timezone *tz)
char *str = getenv("LOGGER_TEST_TIMEOFDAY");
uintmax_t sec, usec;
- if (str && sscanf(str, "%ju.%ju", &sec, &usec) == 2) {
+ if (str) {
+ if (sscanf(str, "%ju.%ju", &sec, &usec) != 2)
+ goto err;
+
tv->tv_sec = sec;
tv->tv_usec = usec;
- return tv->tv_sec >= 0 && tv->tv_usec >= 0 ? 0 : -EINVAL;
+
+ if (tv->tv_sec >= 0 && tv->tv_usec >= 0)
+ return 0;
+ else
+ goto err;
}
return gettimeofday(tv, tz);
+
+err:
+ errno = EINVAL;
+ return -1;
}
static inline char *logger_xgethostname(void)
@@ -406,12 +417,15 @@ static char const *rfc3164_current_time(void)
static char time[32];
struct timeval tv;
struct tm tm;
+ int ret;
static char const * const monthnames[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
};
- logger_gettimeofday(&tv, NULL);
+ ret = logger_gettimeofday(&tv, NULL);
+ if (ret == -1)
+ err(EXIT_FAILURE, _("gettimeofday() failed"));
localtime_r(&tv.tv_sec, &tm);
snprintf(time, sizeof(time),"%s %2d %2.2d:%2.2d:%2.2d",
monthnames[tm.tm_mon], tm.tm_mday,
@@ -782,6 +796,7 @@ static int valid_structured_data_id(const char *str)
*/
static void syslog_rfc5424_header(struct logger_ctl *const ctl)
{
+ int ret;
char *time;
char *hostname;
char const *app_name = ctl->tag;
@@ -794,16 +809,18 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
struct timeval tv;
struct tm tm;
- logger_gettimeofday(&tv, NULL);
+ ret = logger_gettimeofday(&tv, NULL);
+ if (ret == -1)
+ err(EXIT_FAILURE, _("gettimeofday() failed"));
if (localtime_r(&tv.tv_sec, &tm) != NULL) {
char fmt[64];
const size_t i = strftime(fmt, sizeof(fmt),
- "%Y-%m-%dT%H:%M:%S.%%06u%z ", &tm);
+ "%Y-%m-%dT%H:%M:%S.%%06jd%z ", &tm);
/* patch TZ info to comply with RFC3339 (we left SP at end) */
fmt[i - 1] = fmt[i - 2];
fmt[i - 2] = fmt[i - 3];
fmt[i - 3] = ':';
- xasprintf(&time, fmt, tv.tv_usec);
+ xasprintf(&time, fmt, (intmax_t) tv.tv_usec);
} else
err(EXIT_FAILURE, _("localtime() failed"));
} else