Remove mips dependency on alpha.
[jlayton/glibc.git] / misc / syslog.c
index c1fdf5b6b98273f5aa070a13f669825e03e0e362..70daa9e1f3b7b74534ee2c269a3a1f563d8335ab 100644 (file)
@@ -35,29 +35,30 @@ static char sccsid[] = "@(#)syslog.c        8.4 (Berkeley) 3/18/94";
 #include <sys/socket.h>
 #include <sys/syslog.h>
 #include <sys/uio.h>
+#include <sys/un.h>
 #include <netdb.h>
 
 #include <errno.h>
 #include <fcntl.h>
 #include <paths.h>
 #include <stdio.h>
+#include <stdio_ext.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <bits/libc-lock.h>
 #include <signal.h>
+#include <locale.h>
 
-#if __STDC__
 #include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
 
-#ifdef USE_IN_LIBIO
-# include <libio/iolibio.h>
-# define ftell(s) INTUSE(_IO_ftell) (s)
-#endif
+#include <libio/iolibio.h>
+#include <math_ldbl_opt.h>
+
+#include <kernel-features.h>
+
+#define ftell(s) _IO_ftell (s)
 
 static int     LogType = SOCK_DGRAM;   /* type of socket connection */
 static int     LogFile = -1;           /* fd for log */
@@ -73,42 +74,64 @@ __libc_lock_define_initialized (static, syslog_lock)
 
 static void openlog_internal(const char *, int, int) internal_function;
 static void closelog_internal(void);
+#ifndef NO_SIGPIPE
 static void sigpipe_handler (int);
-#ifdef _LIBC_REENTRANT
-static void cancel_handler (void *);
 #endif
 
+#ifndef send_flags
+# define send_flags 0
+#endif
+
+struct cleanup_arg
+{
+  void *buf;
+  struct sigaction *oldaction;
+};
+
+static void
+cancel_handler (void *ptr)
+{
+#ifndef NO_SIGPIPE
+  /* Restore the old signal handler.  */
+  struct cleanup_arg *clarg = (struct cleanup_arg *) ptr;
+
+  if (clarg != NULL && clarg->oldaction != NULL)
+    __sigaction (SIGPIPE, clarg->oldaction, NULL);
+#endif
+
+  /* Free the lock.  */
+  __libc_lock_unlock (syslog_lock);
+}
+
+
 /*
  * syslog, vsyslog --
  *     print message on log file; output is intended for syslogd(8).
  */
 void
-#if __STDC__
-syslog(int pri, const char *fmt, ...)
-#else
-syslog(pri, fmt, va_alist)
-       int pri;
-       char *fmt;
-       va_dcl
-#endif
+__syslog(int pri, const char *fmt, ...)
 {
        va_list ap;
 
-#if __STDC__
        va_start(ap, fmt);
-#else
-       va_start(ap);
-#endif
-       vsyslog(pri, fmt, ap);
+       __vsyslog_chk(pri, -1, fmt, ap);
        va_end(ap);
 }
-libc_hidden_def (syslog)
+ldbl_hidden_def (__syslog, syslog)
+ldbl_strong_alias (__syslog, syslog)
 
 void
-vsyslog(pri, fmt, ap)
-       int pri;
-       register const char *fmt;
+__syslog_chk(int pri, int flag, const char *fmt, ...)
+{
        va_list ap;
+
+       va_start(ap, fmt);
+       __vsyslog_chk(pri, flag, fmt, ap);
+       va_end(ap);
+}
+
+void
+__vsyslog_chk(int pri, int flag, const char *fmt, va_list ap)
 {
        struct tm now_tm;
        time_t now;
@@ -116,10 +139,11 @@ vsyslog(pri, fmt, ap)
        FILE *f;
        char *buf = 0;
        size_t bufsize = 0;
-       size_t prioff, msgoff;
+       size_t msgoff;
+#ifndef NO_SIGPIPE
        struct sigaction action, oldaction;
-       struct sigaction *oldaction_ptr = NULL;
        int sigpipe;
+#endif
        int saved_errno = errno;
        char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"];
 
@@ -156,7 +180,7 @@ vsyslog(pri, fmt, ap)
              *--nump = '0' + pid % 10;
            while ((pid /= 10) != 0);
 
-           endp = __mempcpy (endp, nump, (nump + sizeof (numbuf)) - nump);
+           endp = __mempcpy (endp, nump, (numbuf + sizeof (numbuf)) - nump);
            *endp++ = ']';
            *endp = '\0';
            buf = failbuf;
@@ -165,33 +189,37 @@ vsyslog(pri, fmt, ap)
          }
        else
          {
-           prioff = fprintf (f, "<%d>", pri);
+           __fsetlocking (f, FSETLOCKING_BYCALLER);
+           fprintf (f, "<%d>", pri);
            (void) time (&now);
-#ifdef USE_IN_LIBIO
-           f->_IO_write_ptr += strftime (f->_IO_write_ptr,
-                                         f->_IO_write_end - f->_IO_write_ptr,
-                                         "%h %e %T ",
-                                         __localtime_r (&now, &now_tm));
-#else
-           f->__bufp += strftime (f->__bufp, f->__put_limit - f->__bufp,
-                                  "%h %e %T ", __localtime_r (&now, &now_tm));
-#endif
+           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);
            if (LogTag == NULL)
              LogTag = __progname;
            if (LogTag != NULL)
              fputs_unlocked (LogTag, f);
            if (LogStat & LOG_PID)
-             fprintf (f, "[%d]", __getpid ());
+             fprintf (f, "[%d]", (int) __getpid ());
            if (LogTag != NULL)
-             putc_unlocked (':', f), putc_unlocked (' ', f);
+             {
+               putc_unlocked (':', f);
+               putc_unlocked (' ', f);
+             }
 
            /* Restore errno for %m format.  */
            __set_errno (saved_errno);
 
            /* We have the header.  Print the user's format into the
                buffer.  */
-           vfprintf (f, fmt, ap);
+           if (flag == -1)
+             vfprintf (f, fmt, ap);
+           else
+             __vfprintf_chk (f, flag, fmt, ap);
 
            /* Close the memory stream; this will finalize the data
               into a malloc'd buffer in BUF.  */
@@ -201,7 +229,7 @@ vsyslog(pri, fmt, ap)
        /* Output to stderr if requested. */
        if (LogStat & LOG_PERROR) {
                struct iovec iov[2];
-               register struct iovec *v = iov;
+               struct iovec *v = iov;
 
                v->iov_base = buf + msgoff;
                v->iov_len = bufsize - msgoff;
@@ -212,24 +240,32 @@ vsyslog(pri, fmt, ap)
                    v->iov_base = (char *) "\n";
                    v->iov_len = 1;
                  }
+
+               __libc_cleanup_push (free, buf == failbuf ? NULL : buf);
+
+               /* writev is a cancellation point.  */
                (void)__writev(STDERR_FILENO, iov, v - iov + 1);
+
+               __libc_cleanup_pop (0);
        }
 
-#ifdef _LIBC_REENTRANT
        /* Prepare for multiple users.  We have to take care: open and
           write are cancellation points.  */
-       __libc_cleanup_region_start (1, (void (*) (void *)) cancel_handler,
-                                    &oldaction_ptr);
+       struct cleanup_arg clarg;
+       clarg.buf = buf;
+       clarg.oldaction = NULL;
+       __libc_cleanup_push (cancel_handler, &clarg);
        __libc_lock_lock (syslog_lock);
-#endif
 
+#ifndef NO_SIGPIPE
        /* Prepare for a broken connection.  */
        memset (&action, 0, sizeof (action));
        action.sa_handler = sigpipe_handler;
        sigemptyset (&action.sa_mask);
        sigpipe = __sigaction (SIGPIPE, &action, &oldaction);
        if (sigpipe == 0)
-         oldaction_ptr = &oldaction;
+         clarg.oldaction = &oldaction;
+#endif
 
        /* Get connected, output the message to the local logger. */
        if (!connected)
@@ -240,7 +276,7 @@ vsyslog(pri, fmt, ap)
        if (LogType == SOCK_STREAM)
          ++bufsize;
 
-       if (!connected || __send(LogFile, buf, bufsize, 0) < 0)
+       if (!connected || __send(LogFile, buf, bufsize, send_flags) < 0)
          {
            if (connected)
              {
@@ -250,7 +286,7 @@ vsyslog(pri, fmt, ap)
                openlog_internal(LogTag, LogStat | LOG_NDELAY, 0);
              }
 
-           if (!connected || __send(LogFile, buf, bufsize, 0) < 0)
+           if (!connected || __send(LogFile, buf, bufsize, send_flags) < 0)
              {
                closelog_internal ();   /* attempt re-open next time */
                /*
@@ -268,20 +304,30 @@ vsyslog(pri, fmt, ap)
              }
          }
 
+#ifndef NO_SIGPIPE
        if (sigpipe == 0)
                __sigaction (SIGPIPE, &oldaction, (struct sigaction *) NULL);
+#endif
 
-#ifdef _LIBC_REENTRANT
        /* End of critical section.  */
-       __libc_cleanup_region_end (0);
+       __libc_cleanup_pop (0);
        __libc_lock_unlock (syslog_lock);
-#endif
 
-       free (buf);
+       if (buf != failbuf)
+               free (buf);
 }
-libc_hidden_def (vsyslog)
+libc_hidden_def (__vsyslog_chk)
+
+void
+__vsyslog(int pri, const char *fmt, va_list ap)
+{
+  __vsyslog_chk (pri, -1, fmt, ap);
+}
+ldbl_hidden_def (__vsyslog, vsyslog)
+ldbl_strong_alias (__vsyslog, vsyslog)
+
+static struct sockaddr_un SyslogAddr;  /* AF_UNIX address of local logger */
 
-static struct sockaddr SyslogAddr;     /* AF_UNIX address of local logger */
 
 static void
 internal_function
@@ -293,16 +339,43 @@ openlog_internal(const char *ident, int logstat, int logfac)
        if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
                LogFacility = logfac;
 
-       while (1) {
+       int retry = 0;
+       while (retry < 2) {
                if (LogFile == -1) {
-                       SyslogAddr.sa_family = AF_UNIX;
-                       (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
-                                     sizeof(SyslogAddr.sa_data));
+                       SyslogAddr.sun_family = AF_UNIX;
+                       (void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
+                                     sizeof(SyslogAddr.sun_path));
                        if (LogStat & LOG_NDELAY) {
-                               if ((LogFile = __socket(AF_UNIX, LogType, 0))
-                                   == -1)
+#ifdef SOCK_CLOEXEC
+# ifndef __ASSUME_SOCK_CLOEXEC
+                               if (__have_sock_cloexec >= 0) {
+# endif
+                                       LogFile = __socket(AF_UNIX,
+                                                          LogType
+                                                          | SOCK_CLOEXEC, 0);
+# ifndef __ASSUME_SOCK_CLOEXEC
+                                       if (__have_sock_cloexec == 0)
+                                               __have_sock_cloexec
+                                                 = ((LogFile != -1
+                                                     || errno != EINVAL)
+                                                    ? 1 : -1);
+                               }
+# endif
+#endif
+#ifndef __ASSUME_SOCK_CLOEXEC
+# ifdef SOCK_CLOEXEC
+                               if (__have_sock_cloexec < 0)
+# endif
+                                 LogFile = __socket(AF_UNIX, LogType, 0);
+#endif
+                               if (LogFile == -1)
                                        return;
-                               (void)__fcntl(LogFile, F_SETFD, 1);
+#ifndef __ASSUME_SOCK_CLOEXEC
+# ifdef SOCK_CLOEXEC
+                               if (__have_sock_cloexec < 0)
+# endif
+                                       __fcntl(LogFile, F_SETFD, FD_CLOEXEC);
+#endif
                        }
                }
                if (LogFile != -1 && !connected)
@@ -312,14 +385,16 @@ openlog_internal(const char *ident, int logstat, int logfac)
                            == -1)
                        {
                                int saved_errno = errno;
-                               (void)__close(LogFile);
+                               int fd = LogFile;
                                LogFile = -1;
-                               if (LogType == SOCK_DGRAM
-                                   && saved_errno == EPROTOTYPE)
+                               (void)__close(fd);
+                               __set_errno (old_errno);
+                               if (saved_errno == EPROTOTYPE)
                                {
-                                       /* retry with next SOCK_STREAM: */
-                                       LogType = SOCK_STREAM;
-                                       __set_errno (old_errno);
+                                       /* retry with the other type: */
+                                       LogType = (LogType == SOCK_DGRAM
+                                                  ? SOCK_STREAM : SOCK_DGRAM);
+                                       ++retry;
                                        continue;
                                }
                        } else
@@ -329,38 +404,28 @@ openlog_internal(const char *ident, int logstat, int logfac)
        }
 }
 
-
-static void
-log_cleanup (void *arg)
-{
-  __libc_lock_unlock (syslog_lock);
-}
-
 void
 openlog (const char *ident, int logstat, int logfac)
 {
-#ifdef _LIBC_REENTRANT
-  /* Protect against multiple users.  */
-  __libc_cleanup_region_start (1, log_cleanup, NULL);
+  /* Protect against multiple users and cancellation.  */
+  __libc_cleanup_push (cancel_handler, NULL);
   __libc_lock_lock (syslog_lock);
-#endif
 
   openlog_internal (ident, logstat, logfac);
 
-#ifdef _LIBC_REENTRANT
-  /* Free the lock.  */
-  __libc_cleanup_region_end (1);
-#endif
+  __libc_cleanup_pop (1);
 }
 
+#ifndef NO_SIGPIPE
 static void
 sigpipe_handler (int signo)
 {
   closelog_internal ();
 }
+#endif
 
 static void
-closelog_internal()
+closelog_internal (void)
 {
   if (!connected)
     return;
@@ -371,38 +436,19 @@ closelog_internal()
 }
 
 void
-closelog ()
+closelog (void)
 {
-#ifdef _LIBC_REENTRANT
-  /* Protect against multiple users.  */
-  __libc_cleanup_region_start (1, log_cleanup, NULL);
+  /* Protect against multiple users and cancellation.  */
+  __libc_cleanup_push (cancel_handler, NULL);
   __libc_lock_lock (syslog_lock);
-#endif
 
   closelog_internal ();
   LogTag = NULL;
   LogType = SOCK_DGRAM; /* this is the default */
 
-#ifdef _LIBC_REENTRANT
-  /* Free the lock.  */
-  __libc_cleanup_region_end (1);
-#endif
-}
-
-#ifdef _LIBC_REENTRANT
-static void
-cancel_handler (void *ptr)
-{
-  /* Restore the old signal handler.  */
-  struct sigaction *oldaction = *((struct sigaction **) ptr);
-
-  if (oldaction != (struct sigaction *) NULL)
-    __sigaction (SIGPIPE, oldaction, (struct sigaction *) NULL);
-
   /* Free the lock.  */
-  __libc_lock_unlock (syslog_lock);
+  __libc_cleanup_pop (1);
 }
-#endif
 
 /* setlogmask -- set the log mask level */
 int