diff --git a/libc/Makefile b/libc/Makefile index 372af5f6..a0630801 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -560,7 +560,6 @@ sys/dnsconfig/setdnsconfig.o \ sys/ioctl/ioctl.o \ sys/kernelinfo/kernelinfo.o \ syslog/closelog.o \ -syslog/connectlog.o \ sys/futex/futex.o \ syslog/openlog.o \ syslog/setlogmask.o \ diff --git a/libc/include/syslog.h b/libc/include/syslog.h index 9d77dec3..fd7b7b94 100644 --- a/libc/include/syslog.h +++ b/libc/include/syslog.h @@ -85,13 +85,11 @@ extern "C" { #if defined(__is_sortix_libc) extern char* __syslog_identity; extern int __syslog_facility; -extern int __syslog_fd; extern int __syslog_mask; extern int __syslog_option; #endif void closelog(void); -int connectlog(void); void openlog(const char*, int, int); int setlogmask(int); __attribute__ ((__format__ (__printf__, 2, 3))) diff --git a/libc/syslog/closelog.c b/libc/syslog/closelog.c index 5c521bd6..9a0f0ad2 100644 --- a/libc/syslog/closelog.c +++ b/libc/syslog/closelog.c @@ -22,9 +22,4 @@ void closelog(void) { - if ( 0 <= __syslog_fd ) - { - close(__syslog_fd); - __syslog_fd = -1; - } } diff --git a/libc/syslog/connectlog.c b/libc/syslog/connectlog.c deleted file mode 100644 index 922eabe9..00000000 --- a/libc/syslog/connectlog.c +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2014 Jonas 'Sortie' Termansen. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * syslog/connectlog.c - * Returns a file descriptor to the system log. - */ - -#include -#include - -int connectlog(void) -{ - return open("/dev/tty", O_WRONLY | O_CLOEXEC); -} diff --git a/libc/syslog/openlog.c b/libc/syslog/openlog.c index 12adf5bf..447d1bc0 100644 --- a/libc/syslog/openlog.c +++ b/libc/syslog/openlog.c @@ -30,8 +30,4 @@ void openlog(const char* identity, int option, int facility) // Remember the option and facility parameters for later use. __syslog_option = option; __syslog_facility = facility; - - // Connect to the system more immediately if we are asked to and need to. - if ( (option & LOG_NDELAY) && __syslog_fd < 0 ) - __syslog_fd = connectlog(); } diff --git a/libc/syslog/vsyslog.c b/libc/syslog/vsyslog.c index 1397a7e0..1ef33fe4 100644 --- a/libc/syslog/vsyslog.c +++ b/libc/syslog/vsyslog.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Jonas 'Sortie' Termansen. + * Copyright (c) 2014, 2022 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,60 +14,70 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * syslog/vsyslog.c - * Logs a condition to the system log. + * Logs an event to the system log. */ +#include + +#include #include -#include #include #include #include #include +// TODO: The Sortix doesn't expose this at the moment. +#if !defined(HOST_NAME_MAX) && defined(__sortix__) +#include +#endif + char* __syslog_identity = NULL; int __syslog_facility = LOG_USER; -int __syslog_fd = -1; int __syslog_mask = LOG_UPTO(LOG_DEBUG); int __syslog_option = 0; -// TODO: The transmitted string might be delivered in chunks using this dprintf -// approach (plus there are multiple dprintf calls). We should buffer the -// string fully before transmitting it. - -// TODO: The log entry format here is just whatever some other system does. We -// should work out the Sortix system log semantics and adapt this to that. - void vsyslog(int priority, const char* format, va_list ap) { // Drop the event if it doesn't fit the current priority mask. if ( !(LOG_MASK(LOG_PRI(priority)) & __syslog_mask) ) return; - // Connect to the system log if a connection hasn't yet been established. - if ( __syslog_fd < 0 && (__syslog_fd = connectlog()) < 0 ) - return; - // If no facility is given we'll use the default facility from openlog. if ( !LOG_FAC(priority) ) priority |= __syslog_facility; - // Prepare a timestamp for the log event. + // Gather the log event metadata. + int version = 1; // RFC 5424 struct timespec now; clock_gettime(CLOCK_REALTIME, &now); struct tm tm; gmtime_r(&now.tv_sec, &tm); - char timestamp[32]; - strftime(timestamp, sizeof(timestamp), "%b %e %T", &tm); - - // Include the process id of the current process if requested. - pid_t pid = (__syslog_option & LOG_PID) ? getpid() : 0; + char timeformat[64]; + snprintf(timeformat, sizeof(timeformat), + "%%FT%%T.%06liZ", now.tv_nsec / 1000); + char timestamp[64]; + strftime(timestamp, sizeof(timestamp), timeformat, &tm); + char hostname[HOST_NAME_MAX + 1] = "-"; + gethostname(hostname, sizeof(hostname)); + const char* identity = __syslog_identity ? __syslog_identity : "-"; + char pidstr[3 * sizeof(pid_t)] = "-"; + if ( __syslog_option & LOG_PID ) + snprintf(pidstr, sizeof(pidstr), "%"PRIdPID, getpid()); + const char* msgid = "-"; + const char* structured_data = "-"; // Transmit the event to the system log. - dprintf(__syslog_fd, "<%d>%s %s%s%.0jd%s: ", - priority, - timestamp, - __syslog_identity ? __syslog_identity : "", - "[" + !pid, (intmax_t) pid, "]" + !pid); - vdprintf(__syslog_fd, format, ap); - dprintf(__syslog_fd, "\n"); + flockfile(stderr); + fprintf(stderr, "<%d>%d %s %s %s %s %s %s ", + priority, + version, + timestamp, + hostname, + identity, + pidstr, + msgid, + structured_data); + vfprintf(stderr, format, ap); + fputc('\n', stderr); + funlockfile(stderr); }