From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on sa.local.altlinux.org X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683402311; x=1685994311; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=v85rqs45SuKyDzrfAC0JPRtcK3yiVAfUvWPD8YdLNtg=; b=Y6ycBvL+sUteFe3Gqst6ehn4F5VwXlnLipci8UIdhklDC8bNxgi6sK5pUN1USqz5XD tCYeZ0hZbXbZNJTYHuUqJkClUUtIxxWDDiQy/+s9MOsT0Y0E9eSGLSylC5KgOZiHFyaT 2lfM7eAoTEBUaxFygAWuIH933KIQopaSH62iIeGzUQmM+bPdbH05zJarzNwCGpN4CmXE lkapHjhCM3/TcecLNsg3copW+OyWC2cYUKmYvTatJnXIQcgPU0/beAV5G8JRjUhvm9q2 DL1OGCS9um9oo8KboWRFLFaGB5L+22OJhyYWChDiGdlfPKAWVamTvL8KjJKeY3HjzWMq ecHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683402311; x=1685994311; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v85rqs45SuKyDzrfAC0JPRtcK3yiVAfUvWPD8YdLNtg=; b=g1tXXowLG2fYn/955kPMRnVerI57b8uPjdDdDViq5IlwidBW70UXkZ0D9sAChNiCtm +GPrhgLJzg3in0BzEKK4hPxLPJsv42Gq3fD86lgYd/tNgq24dsc3VEODEBAc4mAso78b iRojNCYdxoPbraODtMzaoj0J+1xrCNnJNiDa3a9QeuYtZ+PexUQ4/4bn4D5oE8YMWBXt MPQSkfgJ+4gGtlNBPtNovqK+ZCDOHXq+4pZ5HVymde04+c+lp07KNUl5urTGyVlyVlz3 A/OCGkhsnSNBuY9sa0k53eujSL4+UWGVhbM7hVzpGJWBlzH1zQCwD/RhBDAeFMyuBO+N 6g5A== X-Gm-Message-State: AC+VfDyJslBBl1fHMcQ/7hFz3e5xymgTlqXdbeA3EzF2sj/6i+yH4PLm WgqhjaYJujnZ0wS64yShOK/GoEylja0= X-Google-Smtp-Source: ACHHUZ7ms0oC5nyZWnSsnl2u9dnHhN6zPzsx0v5gPX0IXTiDabKP/IRmJhHsN8ijVKEXljqzdkVouw== X-Received: by 2002:a05:600c:24ce:b0:3f2:51e7:f1f4 with SMTP id 14-20020a05600c24ce00b003f251e7f1f4mr3631962wmu.10.1683402311342; Sat, 06 May 2023 12:45:11 -0700 (PDT) From: Alexey Gladkov To: make-initrd@lists.altlinux.org Date: Sat, 6 May 2023 21:45:04 +0200 Message-Id: <20230506194504.1471267-4-gladkov.alexey@gmail.com> X-Mailer: git-send-email 2.33.7 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [make-initrd] ueventd: Rewrite logging function to make it more atomic X-BeenThere: make-initrd@lists.altlinux.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: make-initrd@lists.altlinux.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 May 2023 19:45:15 -0000 Archived-At: List-Archive: Signed-off-by: Alexey Gladkov --- datasrc/ueventd/logging.c | 107 +++++++++++++++++++++++++++++++++----- datasrc/ueventd/ueventd.h | 17 +++--- 2 files changed, 104 insertions(+), 20 deletions(-) diff --git a/datasrc/ueventd/logging.c b/datasrc/ueventd/logging.c index ac316aab..14539752 100644 --- a/datasrc/ueventd/logging.c +++ b/datasrc/ueventd/logging.c @@ -1,8 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ +#include #include #include -#include +#include #include #include @@ -11,6 +12,7 @@ int log_priority = LOG_INFO; int log_fd = STDERR_FILENO; const char *log_progname = NULL; +size_t log_progname_len = 0; int rd_logging_level(const char *name) { @@ -25,27 +27,108 @@ void rd_logging_init(int fd, int loglevel, const char *progname) { log_priority = loglevel; log_fd = fd; - log_progname = progname; + if (progname) { + log_progname = progname; + log_progname_len = strlen(progname); + } } void rd_logging_close(void) { } -void rd_message(int priority, const char *fmt, ...) +enum { + LOG_FMT_DATETIME, + LOG_FMT_DELIM_1, + LOG_FMT_PROGNAME, + LOG_FMT_DELIM_2, + LOG_FMT_MESSAGE, + LOG_FMT_EOL, + LOG_FMT_MAX, +}; + +void rd_vmessage(const char *fmt, va_list ap) +{ + char stack_date[38]; // "[1970-01-01 00:00:00]" + char stack_msg[80]; + struct iovec iov[LOG_FMT_MAX]; + + memset(iov, 0, sizeof(iov)); + + int used; + struct tm t; + time_t now = time(NULL); + + if (localtime_r(&now, &t) == &t) { + used = snprintf(stack_date, sizeof(stack_date), + "[%04d-%02d-%02d %02d:%02d:%02d]", + t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, + t.tm_hour, t.tm_min, t.tm_sec); + + iov[LOG_FMT_DATETIME].iov_base = stack_date; + iov[LOG_FMT_DATETIME].iov_len = (size_t) used; + + iov[LOG_FMT_DELIM_1].iov_base = (char *) " "; + iov[LOG_FMT_DELIM_1].iov_len = 1; + } + + if (log_progname) { + iov[LOG_FMT_PROGNAME].iov_base = (char *) log_progname; + iov[LOG_FMT_PROGNAME].iov_len = log_progname_len; + + iov[LOG_FMT_DELIM_2].iov_base = (char *) ": "; + iov[LOG_FMT_DELIM_2].iov_len = 2; + } + + iov[LOG_FMT_EOL].iov_base = (char *) "\n"; + iov[LOG_FMT_EOL].iov_len = 1; + + va_list ap2; + size_t size; + char *buf = NULL; + + va_copy(ap2, ap); + used = vsnprintf(NULL, 0, fmt, ap2); + va_end(ap2); + + if (used >= (int) sizeof(stack_msg)) { + size = (size_t) used + 1; + buf = malloc(size); + } + + if (!buf) { + size = sizeof(stack_msg); + buf = stack_msg; + } + + va_copy(ap2, ap); + used = vsnprintf(buf, size, fmt, ap2); + va_end(ap2); + + iov[LOG_FMT_MESSAGE].iov_base = buf; + iov[LOG_FMT_MESSAGE].iov_len = (size_t) used; + + fsync(log_fd); + TEMP_FAILURE_RETRY(writev(log_fd, iov, LOG_FMT_MAX)); + + if (buf != stack_msg) + free(buf); + + return; +} + +void rd_log_vmessage(int priority, const char *fmt, va_list ap) +{ + if (priority <= log_priority) + rd_vmessage(fmt, ap); +} + +void rd_log_message(int priority, const char *fmt, ...) { va_list ap; va_start(ap, fmt); if (priority <= log_priority) { - time_t ts = time(NULL); - struct tm *t = localtime(&ts); - dprintf(log_fd, "[%04d-%02d-%02d %02d:%02d:%02d] ", - t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, - t->tm_hour, t->tm_min, t->tm_sec); - if (log_progname) - dprintf(log_fd, "%s: ", log_progname); - vdprintf(log_fd, fmt, ap); - dprintf(log_fd, "\n"); + rd_vmessage(fmt, ap); } va_end(ap); } diff --git a/datasrc/ueventd/ueventd.h b/datasrc/ueventd/ueventd.h index ddba98bf..732d0c61 100644 --- a/datasrc/ueventd/ueventd.h +++ b/datasrc/ueventd/ueventd.h @@ -55,22 +55,23 @@ extern int is_dot_dir(struct dirent *ent) __attribute__((nonnull(1))); #include #include #include +#include extern void rd_logging_init(int log_fd, int level, const char *progname); extern void rd_logging_close(void); -extern int rd_logging_level(const char *lvl) __attribute__((nonnull(1))); -extern void rd_message(int priority, const char *fmt, ...) __attribute__((format(printf, 2, 3))); - -#define __rd_message(level, format, arg...) rd_message(level, format, ##arg) +extern int rd_logging_level(const char *lvl) __attribute__((nonnull(1))); +extern void rd_vmessage(const char *fmt, va_list ap) __attribute__((format(printf, 1, 0))); +extern void rd_log_vmessage(int priority, const char *fmt, va_list ap) __attribute__((format(printf, 2, 0))); +extern void rd_log_message(int priority, const char *fmt, ...) __attribute__((format(printf, 2, 3))); #define rd_fatal(format, arg...) \ do { \ - rd_message(LOG_CRIT, "%s:%d: " format, __FILE__, __LINE__, ##arg); \ + rd_log_message(LOG_CRIT, "%s:%d: " format, __FILE__, __LINE__, ##arg); \ _exit(EXIT_FAILURE); \ } while (0) -#define rd_err(format, arg...) __rd_message(LOG_ERR, format, ##arg) -#define rd_info(format, arg...) __rd_message(LOG_INFO, format, ##arg) -#define rd_dbg(format, arg...) __rd_message(LOG_DEBUG, format, ##arg) +#define rd_err(format, arg...) rd_log_message(LOG_ERR, format, ##arg) +#define rd_info(format, arg...) rd_log_message(LOG_INFO, format, ##arg) +#define rd_dbg(format, arg...) rd_log_message(LOG_DEBUG, format, ##arg) #endif /* __UEVENTD_H__ */ -- 2.33.7