]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/logger.c
include/pidfd-utils: remove hardcoded syscall fallback
[thirdparty/util-linux.git] / misc-utils / logger.c
index 6d21fcae18fb979b8de0dd3ca7eb5fb93ef8387b..f696287d7a6b8b21c57cc1afe42962a70913e0c7 100644 (file)
@@ -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)
@@ -248,7 +259,7 @@ static int unix_socket(struct logger_ctl *ctl, const char *path, int *socket_typ
                errx(EXIT_FAILURE, _("openlog %s: pathname too long"), path);
 
        s_addr.sun_family = AF_UNIX;
-       strcpy(s_addr.sun_path, path);
+       xstrncpy(s_addr.sun_path, path, sizeof(s_addr.sun_path));
 
        for (i = 2; i; i--) {
                int st = -1;
@@ -343,7 +354,7 @@ static int journald_entry(struct logger_ctl *ctl, FILE *fp)
        int n, lines = 0, vectors = 8, ret = 0, msgline = -1;
        size_t dummy = 0;
 
-       iovec = xmalloc(vectors * sizeof(struct iovec));
+       iovec = xreallocarray(NULL, vectors, sizeof(struct iovec));
        while (1) {
                buf = NULL;
                sz = getline(&buf, &dummy, fp);
@@ -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,
@@ -451,7 +465,7 @@ static void write_output(struct logger_ctl *ctl, const char *const msg)
        if (!ctl->noact && !is_connected(ctl))
                logger_reopen(ctl);
 
-       /* 1) octen count */
+       /* 1) octet count */
        if (ctl->octet_count) {
                size_t len = xasprintf(&octet, "%zu ", strlen(ctl->hdr) + strlen(msg));
                iovec_add_string(iov, iovlen, octet, len);
@@ -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
@@ -1091,8 +1108,8 @@ static void __attribute__((__noreturn__)) usage(void)
 #endif
 
        fputs(USAGE_SEPARATOR, out);
-       printf(USAGE_HELP_OPTIONS(26));
-       printf(USAGE_MAN_TAIL("logger(1)"));
+       fprintf(out, USAGE_HELP_OPTIONS(26));
+       fprintf(out, USAGE_MAN_TAIL("logger(1)"));
 
        exit(EXIT_SUCCESS);
 }