From 49cb30aa5b3cb92e28b1219b276420ee3b4f2946 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 14 Jun 2007 16:06:25 +0000 Subject: [PATCH] Redo the long syslog line splitting based on a patch from Eygene Ryabinkin. Include memrchr() for systems without it. --- logging.c | 43 +++++++++++++++++++++---------------------- sudo.h | 6 ++++++ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/logging.c b/logging.c index aafc77c70..07c83d372 100644 --- a/logging.c +++ b/logging.c @@ -128,46 +128,45 @@ do_syslog(pri, msg) int pri; char *msg; { - size_t count; - char *p; - char *tmp; - char save; + size_t len, maxlen; + char *p, *tmp, save; + const char *fmt; + const char fmt_first[] = "%8s : %s"; + const char fmt_contd[] = "%8s : (command continued) %s"; /* * Log the full line, breaking into multiple syslog(3) calls if necessary */ - for (p = msg, count = 0; *p && count < strlen(msg) / MAXSYSLOGLEN + 1; - count++) { - if (strlen(p) > MAXSYSLOGLEN) { + fmt = fmt_first; + maxlen = MAXSYSLOGLEN - (sizeof(fmt_first) - 6 + strlen(user_name)); + for (p = msg; *p != '\0'; ) { + len = strlen(p); + if (len > maxlen) { /* * Break up the line into what will fit on one syslog(3) line - * Try to break on a word boundary if possible. + * Try to avoid breaking words into several lines if possible. */ - for (tmp = p + MAXSYSLOGLEN; tmp > p && *tmp != ' '; tmp--) - ; - if (tmp <= p) - tmp = p + MAXSYSLOGLEN; + tmp = memrchr(p, ' ', maxlen); + if (tmp == NULL) + tmp = p + maxlen; /* NULL terminate line, but save the char to restore later */ save = *tmp; *tmp = '\0'; - if (count == 0) - mysyslog(pri, "%8s : %s", user_name, p); - else - mysyslog(pri, "%8s : (command continued) %s", user_name, p); + mysyslog(pri, fmt, user_name, p); *tmp = save; /* restore saved character */ - /* Eliminate leading whitespace */ - for (p = tmp; *p != ' ' && *p !='\0'; p++) + /* Advance p and eliminate leading whitespace */ + for (p = tmp; *p == ' '; p++) ; } else { - if (count == 0) - mysyslog(pri, "%8s : %s", user_name, p); - else - mysyslog(pri, "%8s : (command continued) %s", user_name, p); + mysyslog(pri, fmt, user_name, p); + p += len; } + fmt = fmt_contd; + maxlen = MAXSYSLOGLEN - (sizeof(fmt_contd) - 6 + strlen(user_name)); } } diff --git a/sudo.h b/sudo.h index f30b1d0b0..da514026c 100644 --- a/sudo.h +++ b/sudo.h @@ -207,6 +207,12 @@ size_t strlcat __P((char *, const char *, size_t)); #ifndef HAVE_STRLCPY size_t strlcpy __P((char *, const char *, size_t)); #endif +#ifndef HAVE_MEMRCHR +VOID *memrchr __P((const VOID *, int, size_t)); +#endif +#ifndef HAVE_MKSTEMP +int mkstemp __P((char *)); +#endif char *sudo_goodpath __P((const char *, struct stat *)); char *tgetpass __P((const char *, int, int)); int find_path __P((char *, char **, struct stat *, char *)); -- 2.40.0