On Fri, Mar 17, 2017 at 02:09:35AM +0100, Alexander Bluhm wrote:
> This is the next step for refactoring internal syslogd(8) logging.

As we did not have nice log functions before, ttymsg() had to return
the error string.  Log the message where the error happens and make
the function void.

ok?

bluhm

Index: usr.sbin/syslogd/syslogd.c
===================================================================
RCS file: /cvs/src/usr.sbin/syslogd/syslogd.c,v
retrieving revision 1.238
diff -u -p -r1.238 syslogd.c
--- usr.sbin/syslogd/syslogd.c  5 Apr 2017 21:30:04 -0000       1.238
+++ usr.sbin/syslogd/syslogd.c  5 Apr 2017 21:41:31 -0000
@@ -2068,7 +2068,7 @@ void
 wallmsg(struct filed *f, struct iovec *iov)
 {
        struct utmp ut;
-       char utline[sizeof(ut.ut_line) + 1], *p;
+       char utline[sizeof(ut.ut_line) + 1];
        static int reenter;                     /* avoid calling ourselves */
        FILE *uf;
        int i;
@@ -2087,8 +2087,7 @@ wallmsg(struct filed *f, struct iovec *i
                strncpy(utline, ut.ut_line, sizeof(utline) - 1);
                utline[sizeof(utline) - 1] = '\0';
                if (f->f_type == F_WALL) {
-                       if ((p = ttymsg(iov, 6, utline)) != NULL)
-                               log_warnx("%s", p);
+                       ttymsg(iov, 6, utline);
                        continue;
                }
                /* should we send the message to this user? */
@@ -2097,8 +2096,7 @@ wallmsg(struct filed *f, struct iovec *i
                                break;
                        if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
                            UT_NAMESIZE)) {
-                               if ((p = ttymsg(iov, 6, utline)) != NULL)
-                                       log_warnx("%s", p);
+                               ttymsg(iov, 6, utline);
                                break;
                        }
                }
Index: usr.sbin/syslogd/syslogd.h
===================================================================
RCS file: /cvs/src/usr.sbin/syslogd/syslogd.h,v
retrieving revision 1.28
diff -u -p -r1.28 syslogd.h
--- usr.sbin/syslogd/syslogd.h  5 Apr 2017 11:31:45 -0000       1.28
+++ usr.sbin/syslogd/syslogd.h  5 Apr 2017 21:41:31 -0000
@@ -37,7 +37,7 @@ int   priv_getnameinfo(struct sockaddr *
 /* Terminal message */
 #define TTYMSGTIME     1               /* timeout used by ttymsg */
 #define TTYMAXDELAY    256             /* max events in ttymsg */
-char *ttymsg(struct iovec *, int, char *);
+void ttymsg(struct iovec *, int, char *);
 
 /* File descriptor send/recv */
 void send_fd(int, int);
Index: usr.sbin/syslogd/ttymsg.c
===================================================================
RCS file: /cvs/src/usr.sbin/syslogd/ttymsg.c,v
retrieving revision 1.14
diff -u -p -r1.14 ttymsg.c
--- usr.sbin/syslogd/ttymsg.c   5 Apr 2017 11:31:45 -0000       1.14
+++ usr.sbin/syslogd/ttymsg.c   5 Apr 2017 21:41:31 -0000
@@ -60,37 +60,35 @@ void ttycb(int, short, void *);
 /*
  * Display the contents of a uio structure on a terminal.
  * Schedules an event if write would block, waiting up to TTYMSGTIME
- * seconds.  Returns pointer to error string on unexpected error;
- * string is not newline-terminated.  Various "normal" errors are ignored
- * (exclusive-use, lack of permission, etc.).
+ * seconds.
  */
-char *
+void
 ttymsg(struct iovec *iov, int iovcnt, char *utline)
 {
        static char device[MAXNAMLEN] = _PATH_DEV;
-       static char ebuf[ERRBUFSIZE];
        int cnt, fd;
        size_t left;
        ssize_t wret;
        struct iovec localiov[6];
 
-       if (iovcnt < 0 || (size_t)iovcnt > nitems(localiov))
-               return ("too many iov's (change code in syslogd/ttymsg.c)");
+       if (iovcnt < 0 || (size_t)iovcnt > nitems(localiov)) {
+               log_warnx("too many iov's (change code in syslogd/ttymsg.c)");
+               return;
+       }
 
        /*
         * Ignore lines that start with "ftp" or "uucp".
         */
        if ((strncmp(utline, "ftp", 3) == 0) ||
            (strncmp(utline, "uucp", 4) == 0))
-               return (NULL);
+               return;
 
        (void) strlcpy(device + sizeof(_PATH_DEV) - 1, utline,
            sizeof(device) - (sizeof(_PATH_DEV) - 1));
        if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
                /* A slash is an attempt to break security... */
-               (void) snprintf(ebuf, sizeof(ebuf), "'/' in \"%s\"",
-                   device);
-               return (ebuf);
+               log_warnx("'/' in tty device \"%s\"", device);
+               return;
        }
 
        /*
@@ -98,11 +96,9 @@ ttymsg(struct iovec *iov, int iovcnt, ch
         * if not running as root; not an error.
         */
        if ((fd = priv_open_tty(device)) < 0) {
-               if (errno == EBUSY || errno == EACCES)
-                       return (NULL);
-               (void) snprintf(ebuf, sizeof(ebuf),
-                   "%s: %s", device, strerror(errno));
-               return (ebuf);
+               if (errno != EBUSY && errno != EACCES)
+                       log_warn("priv_open_tty device \"%s\"", device);
+               return;
        }
 
        left = 0;
@@ -136,9 +132,9 @@ ttymsg(struct iovec *iov, int iovcnt, ch
                        struct timeval           to;
 
                        if (tty_delayed >= TTYMAXDELAY) {
-                               (void) snprintf(ebuf, sizeof(ebuf),
-                                   "%s: too many delayed writes", device);
-                               goto error;
+                               log_warnx("tty device \"%s\": %s",
+                                   device, "too many delayed writes");
+                               break;
                        }
                        log_debug("ttymsg delayed write");
                        if (iov != localiov) {
@@ -147,9 +143,9 @@ ttymsg(struct iovec *iov, int iovcnt, ch
                                iov = localiov;
                        }
                        if ((td = malloc(sizeof(*td))) == NULL) {
-                               (void) snprintf(ebuf, sizeof(ebuf),
-                                   "%s: malloc: %s", device, strerror(errno));
-                               goto error;
+                               log_warn("allocate delay tty device \"%s\"",
+                                   device);
+                               break;
                        }
                        td->td_length = 0;
                        if (left > MAXLINE)
@@ -169,23 +165,19 @@ ttymsg(struct iovec *iov, int iovcnt, ch
                        to.tv_sec = TTYMSGTIME;
                        to.tv_usec = 0;
                        event_add(&td->td_event, &to);
-                       return (NULL);
+                       return;
                }
                /*
                 * We get ENODEV on a slip line if we're running as root,
                 * and EIO if the line just went away.
                 */
-               if (errno == ENODEV || errno == EIO)
-                       break;
-               (void) snprintf(ebuf, sizeof(ebuf),
-                   "%s: %s", device, strerror(errno));
- error:
-               (void) close(fd);
-               return (ebuf);
+               if (errno != ENODEV && errno != EIO)
+                       log_warn("writev tty device \"%s\"", device);
+               break;
        }
 
        (void) close(fd);
-       return (NULL);
+       return;
 }
 
 void

Reply via email to