2005-05-08 Michael Tokarev Dmitry V. Levin Use ctime_r(3) instead of strftime_r(3) in syslog(3) so that month names will not depend on current locale settings. This patch also cleans up syslog.c a bit as a result. But see note in code below. --- glibc-2.5.orig/misc/syslog.c +++ glibc-2.5/misc/syslog.c @@ -57,11 +57,8 @@ #include #endif -#include #include -#define ftell(s) INTUSE(_IO_ftell) (s) - static int LogType = SOCK_DGRAM; /* type of socket connection */ static int LogFile = -1; /* fd for log */ static int connected; /* have done connect */ @@ -182,19 +179,22 @@ void __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap) { - struct tm now_tm; time_t now; int fd; FILE *f; char *buf = 0; size_t bufsize = 0; - size_t prioff, msgoff; + size_t msgoff; #ifndef NO_SIGPIPE struct sigaction action, oldaction; int sigpipe; #endif int saved_errno = errno; - char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; + /* we use failbuf for 2 things: to hold message in case of + * OOM condition, and for ctime_r() buffer */ +#define FAILMSGSZ (3 * sizeof (pid_t) + sizeof "out of memory []") +#define CTIMESZ 26 + char failbuf[FAILMSGSZ > CTIMESZ ? FAILMSGSZ : CTIMESZ]; #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID /* Check for invalid bits. */ @@ -239,15 +239,21 @@ __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap) else { __fsetlocking (f, FSETLOCKING_BYCALLER); - prioff = fprintf (f, "<%d>", pri); + /* + * XXX: syslogd may know better what the current time is. + * User program may be running chrooted w/o proper localtime + * description, or a user may prefer their own timezone. syslogd + * is able to construct timestamp if there is no timestamp given + * in a message. So it may be waay better to NOT construct ANY + * timestamp here and allow syslogd to figure out proper time + * for us. When working with local syslogd (and it IS local + * since we're using either /dev/log or localhost), message + * should be picked up by syslogd very shortly. And at least + * timestamps in system logs will be in chronological order... + * -- mjt. + */ (void) time (&now); - f->_IO_write_ptr += __strftime_l (f->_IO_write_ptr, - f->_IO_write_end - - f->_IO_write_ptr, - "%h %e %T ", - __localtime_r (&now, &now_tm), - _nl_C_locobj_ptr); - msgoff = ftell (f); + msgoff = fprintf (f, "<%d>%.15s ", pri, ctime_r(&now, failbuf) + 4); /* Protect against multiple users and cancellation. */ __libc_cleanup_push (cancel_handler, NULL);