Remove mips dependency on alpha.
[jlayton/glibc.git] / misc / syslog.c
index 6916356da717a0f58aa48db5dbac687452b6a8e2..70daa9e1f3b7b74534ee2c269a3a1f563d8335ab 100644 (file)
@@ -35,6 +35,7 @@ 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>
@@ -50,14 +51,14 @@ static char sccsid[] = "@(#)syslog.c        8.4 (Berkeley) 3/18/94";
 #include <signal.h>
 #include <locale.h>
 
-#if __STDC__
 #include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
 
 #include <libio/iolibio.h>
-#define ftell(s) INTUSE(_IO_ftell) (s)
+#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,8 +74,13 @@ __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);
+#endif
 
+#ifndef send_flags
+# define send_flags 0
+#endif
 
 struct cleanup_arg
 {
@@ -85,11 +91,13 @@ struct cleanup_arg
 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);
@@ -101,32 +109,29 @@ cancel_handler (void *ptr)
  *     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;
@@ -134,9 +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;
        int sigpipe;
+#endif
        int saved_errno = errno;
        char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"];
 
@@ -183,14 +190,14 @@ vsyslog(pri, fmt, ap)
        else
          {
            __fsetlocking (f, FSETLOCKING_BYCALLER);
-           prioff = fprintf (f, "<%d>", pri);
+           fprintf (f, "<%d>", pri);
            (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);
+                                             _nl_C_locobj_ptr);
            msgoff = ftell (f);
            if (LogTag == NULL)
              LogTag = __progname;
@@ -209,7 +216,10 @@ vsyslog(pri, fmt, ap)
 
            /* 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.  */
@@ -219,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;
@@ -247,6 +257,7 @@ vsyslog(pri, fmt, ap)
        __libc_cleanup_push (cancel_handler, &clarg);
        __libc_lock_lock (syslog_lock);
 
+#ifndef NO_SIGPIPE
        /* Prepare for a broken connection.  */
        memset (&action, 0, sizeof (action));
        action.sa_handler = sigpipe_handler;
@@ -254,6 +265,7 @@ vsyslog(pri, fmt, ap)
        sigpipe = __sigaction (SIGPIPE, &action, &oldaction);
        if (sigpipe == 0)
          clarg.oldaction = &oldaction;
+#endif
 
        /* Get connected, output the message to the local logger. */
        if (!connected)
@@ -264,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)
              {
@@ -274,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 */
                /*
@@ -292,8 +304,10 @@ vsyslog(pri, fmt, ap)
              }
          }
 
+#ifndef NO_SIGPIPE
        if (sigpipe == 0)
                __sigaction (SIGPIPE, &oldaction, (struct sigaction *) NULL);
+#endif
 
        /* End of critical section.  */
        __libc_cleanup_pop (0);
@@ -302,9 +316,17 @@ vsyslog(pri, fmt, ap)
        if (buf != failbuf)
                free (buf);
 }
-libc_hidden_def (vsyslog)
+libc_hidden_def (__vsyslog_chk)
 
-static struct sockaddr SyslogAddr;     /* AF_UNIX address of local logger */
+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 void
@@ -320,14 +342,40 @@ openlog_internal(const char *ident, int logstat, int logfac)
        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)
@@ -368,14 +416,16 @@ openlog (const char *ident, int logstat, int logfac)
   __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;
@@ -386,7 +436,7 @@ closelog_internal()
 }
 
 void
-closelog ()
+closelog (void)
 {
   /* Protect against multiple users and cancellation.  */
   __libc_cleanup_push (cancel_handler, NULL);