Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/external/bsd/dhcpcd/dist/src/logerr.c,v rcsdiff: /ftp/cvs/cvsroot/src/external/bsd/dhcpcd/dist/src/logerr.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.1.1.2.4.1 retrieving revision 1.1.1.2.4.2 diff -u -p -r1.1.1.2.4.1 -r1.1.1.2.4.2 --- src/external/bsd/dhcpcd/dist/src/logerr.c 2019/06/10 21:44:44 1.1.1.2.4.1 +++ src/external/bsd/dhcpcd/dist/src/logerr.c 2020/04/13 07:45:59 1.1.1.2.4.2 @@ -1,6 +1,7 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ /* * logerr: errx with logging - * Copyright (c) 2006-2019 Roy Marples + * Copyright (c) 2006-2020 Roy Marples * All rights reserved * Redistribution and use in source and binary forms, with or without @@ -91,7 +92,7 @@ getprogname(void) #ifndef SMALL /* Write the time, syslog style. month day time - */ -static void +static int logprintdate(FILE *stream) { struct timeval tv; @@ -100,19 +101,22 @@ logprintdate(FILE *stream) char buf[32]; if (gettimeofday(&tv, NULL) == -1) - return; + return -1; now = tv.tv_sec; tzset(); - localtime_r(&now, &tmnow); - strftime(buf, sizeof(buf), "%b %d %T ", &tmnow); - fprintf(stream, "%s", buf); + if (localtime_r(&now, &tmnow) == NULL) + return -1; + if (strftime(buf, sizeof(buf), "%b %d %T ", &tmnow) == 0) + return -1; + return fprintf(stream, "%s", buf); } #endif -__printflike(3, 0) static void +__printflike(3, 0) static int vlogprintf_r(struct logctx *ctx, FILE *stream, const char *fmt, va_list args) { + int len = 0, e; va_list a; #ifndef SMALL bool log_pid; @@ -122,7 +126,11 @@ vlogprintf_r(struct logctx *ctx, FILE *s if ((stream == stderr && ctx->log_opts & LOGERR_ERR_DATE) || (stream != stderr && ctx->log_opts & LOGERR_LOG_DATE)) - logprintdate(stream); + { + if ((e = logprintdate(stream)) == -1) + return -1; + len += e; + } #ifdef LOGERR_TAG log_tag = ((stream == stderr && ctx->log_opts & LOGERR_ERR_TAG) || @@ -130,29 +138,43 @@ vlogprintf_r(struct logctx *ctx, FILE *s if (log_tag) { if (ctx->log_tag == NULL) ctx->log_tag = getprogname(); - fprintf(stream, "%s", ctx->log_tag); + if ((e = fprintf(stream, "%s", ctx->log_tag)) == -1) + return -1; + len += e; } #endif log_pid = ((stream == stderr && ctx->log_opts & LOGERR_ERR_PID) || (stream != stderr && ctx->log_opts & LOGERR_LOG_PID)); - if (log_pid) - fprintf(stream, "[%d]", getpid()); + if (log_pid) { + if ((e = fprintf(stream, "[%d]", getpid())) == -1) + return -1; + len += e; + } #ifdef LOGERR_TAG if (log_tag || log_pid) #else if (log_pid) #endif - fprintf(stream, ": "); + { + if ((e = fprintf(stream, ": ")) == -1) + return -1; + len += e; + } #else UNUSED(ctx); #endif va_copy(a, args); - vfprintf(stream, fmt, a); - fputc('\n', stream); + e = vfprintf(stream, fmt, a); + if (fputc('\n', stream) == EOF) + e = -1; + else if (e != -1) + e++; va_end(a); + + return e == -1 ? -1 : len + e; } /* @@ -170,37 +192,35 @@ vlogprintf_r(struct logctx *ctx, FILE *s #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmissing-format-attribute" #endif -__printflike(2, 0) static void +__printflike(2, 0) static int vlogmessage(int pri, const char *fmt, va_list args) { struct logctx *ctx = &_logctx; + int len = 0; if (ctx->log_opts & LOGERR_ERR && (pri <= LOG_ERR || (!(ctx->log_opts & LOGERR_QUIET) && pri <= LOG_INFO) || (ctx->log_opts & LOGERR_DEBUG && pri <= LOG_DEBUG))) - vlogprintf_r(ctx, stderr, fmt, args); + len = vlogprintf_r(ctx, stderr, fmt, args); if (!(ctx->log_opts & LOGERR_LOG)) - return; + return len; -#ifdef SMALL - vsyslog(pri, fmt, args); -#else - if (ctx->log_file == NULL) { - vsyslog(pri, fmt, args); - return; - } - if (pri == LOG_DEBUG && !(ctx->log_opts & LOGERR_DEBUG)) - return; - vlogprintf_r(ctx, ctx->log_file, fmt, args); +#ifndef SMALL + if (ctx->log_file != NULL && + (pri != LOG_DEBUG || (ctx->log_opts & LOGERR_DEBUG))) + len = vlogprintf_r(ctx, ctx->log_file, fmt, args); #endif + + vsyslog(pri, fmt, args); + return len; } #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)) #pragma GCC diagnostic pop #endif -__printflike(2, 3) static void +__printflike(2, 3) void logmessage(int pri, const char *fmt, ...) { va_list args; @@ -220,8 +240,18 @@ vlogerrmessage(int pri, const char *fmt, logmessage(pri, "%s: %s", buf, strerror(_errno)); } +__printflike(2, 3) void +logerrmessage(int pri, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vlogerrmessage(pri, fmt, args); + va_end(args); +} + void -logdebug(const char *fmt, ...) +log_debug(const char *fmt, ...) { va_list args; @@ -231,7 +261,7 @@ logdebug(const char *fmt, ...) } void -logdebugx(const char *fmt, ...) +log_debugx(const char *fmt, ...) { va_list args; @@ -241,7 +271,7 @@ logdebugx(const char *fmt, ...) } void -loginfo(const char *fmt, ...) +log_info(const char *fmt, ...) { va_list args; @@ -251,7 +281,7 @@ loginfo(const char *fmt, ...) } void -loginfox(const char *fmt, ...) +log_infox(const char *fmt, ...) { va_list args; @@ -261,7 +291,7 @@ loginfox(const char *fmt, ...) } void -logwarn(const char *fmt, ...) +log_warn(const char *fmt, ...) { va_list args; @@ -271,7 +301,7 @@ logwarn(const char *fmt, ...) } void -logwarnx(const char *fmt, ...) +log_warnx(const char *fmt, ...) { va_list args; @@ -281,7 +311,7 @@ logwarnx(const char *fmt, ...) } void -logerr(const char *fmt, ...) +log_err(const char *fmt, ...) { va_list args; @@ -291,7 +321,7 @@ logerr(const char *fmt, ...) } void -logerrx(const char *fmt, ...) +log_errx(const char *fmt, ...) { va_list args; @@ -300,6 +330,14 @@ logerrx(const char *fmt, ...) va_end(args); } +unsigned int +loggetopts(void) +{ + struct logctx *ctx = &_logctx; + + return ctx->log_opts; +} + void logsetopts(unsigned int opts) {