]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Changes to support netsyslog in addition to msyslog.
authorDanny Mayer <mayer@ntp.org>
Sun, 4 May 2003 14:51:16 +0000 (10:51 -0400)
committerDanny Mayer <mayer@ntp.org>
Sun, 4 May 2003 14:51:16 +0000 (10:51 -0400)
the libntp/log.c was in the wrong place. It should have been under winnt/libntp.

bk: 3eb528e4Cw-L-3s535FIbKrCQ9IH0A

include/ntp_stdlib.h
libntp/msyslog.c
ntpd/ntp_io.c
ports/winnt/libntp/log.c [new file with mode: 0644]

index cfcfa1473f7511f039afaa4462379c2826d2b4cf..9d1d4c9bdcc02849f5c7beb05953f4f9a7dfba2c 100644 (file)
 # include <stdarg.h>
 extern void    msyslog         P((int, const char *, ...))
                                __attribute__((__format__(__printf__, 2, 3)));
+extern void    netsyslog       P((int, const char *, ...))
+                               __attribute__((__format__(__printf__, 2, 3)));
 #else
 # include <varargs.h>
 extern void msyslog            P(());
+extern void netsyslog          P(());
 #endif
 
 extern void    auth_delkeys    P((void));
index 1aad4475239e43f45162bbc14411e88ae4eccd74..16ce442f55f560f1e60db07dabffeac195b0dd8c 100644 (file)
@@ -42,6 +42,82 @@ static char separator = '/';
 #endif /* SYS_WINNT */
 extern char *progname;
 
+/*
+ * This routine adds the contents of a buffer to the log
+ */
+void
+addto_syslog(int level, char * buf)
+{
+       char *prog;
+       FILE *out_file;
+
+#if !defined(VMS) && !defined (SYS_VXWORKS)
+       if (syslogit)
+           syslog(level, "%s", buf);
+       else
+#endif /* VMS  && SYS_VXWORKS*/
+       {
+               out_file = syslog_file ? syslog_file: level <= LOG_ERR ? stderr : stdout;
+               /* syslog() provides the timestamp, so if we're not using
+                  syslog, we must provide it. */
+               prog = strrchr(progname, separator);
+               if (prog == NULL)
+                   prog = progname;
+               else
+                   prog++;
+               (void) fprintf(out_file, "%s ", humanlogtime ());
+               (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf);
+               fflush (out_file);
+       }
+}
+void
+format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
+{
+       register char c;
+       register char *n, *prog;
+       register const char *f;
+
+       char *err;
+
+       n = nfmt;
+       f = fmt;
+       while ((c = *f++) != '\0' && n < (nfmt+lennfmt - 2)) {
+               if (c != '%') {
+                       *n++ = c;
+                       continue;
+               }
+               if ((c = *f++) != 'm') {
+                       *n++ = '%';
+                       *n++ = c;
+                       continue;
+               }
+               err = 0;
+               err = strerror(errval);
+               /* Make sure we have enough space for the error message */
+               if ((n + strlen(err)) < (nfmt + lennfmt -2)) {
+                       strcpy(n, err);
+                       n += strlen(err);
+               }
+       }
+#if !defined(VMS)
+       if (!syslogit)
+#endif /* VMS */
+           *n++ = '\n';
+       *n = '\0';
+}
+
+/*
+ * The externally called functions are defined here
+ * but share the internal function above to fetch
+ * any error message strings, This is done so that we can
+ * have two different functions to perform the logging
+ * since Windows gets it's error information from different
+ * places depending on whether or not it's network I/O.
+ * msyslog() is for general use while netsyslog() is for
+ * network I/O functions. They are virtually identical
+ * in implementation.
+ */
+
 #if defined(__STDC__) || defined(HAVE_STDARG_H)
 void msyslog(int level, const char *fmt, ...)
 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
@@ -57,17 +133,15 @@ void msyslog(int level, const char *fmt, ...)
 #endif
        va_list ap;
        char buf[1025], nfmt[256];
-       register char c;
-       register char *n, *prog;
-       register const char *f;
+       register char *prog;
 
        /*
         * Save the error value as soon as possible
         */
 #ifdef SYS_WINNT
-       int olderrno = GetLastError();
+       int errval = GetLastError();
 #else
-       int olderrno = errno;
+       int errval = errno;
 #endif
        char *err;
 
@@ -79,55 +153,58 @@ void msyslog(int level, const char *fmt, ...)
        level = va_arg(ap, int);
        fmt = va_arg(ap, char *);
 #endif
-
-       n = nfmt;
-       f = fmt;
-       while ((c = *f++) != '\0' && c != '\n' && n < &nfmt[252]) {
-               if (c != '%') {
-                       *n++ = c;
-                       continue;
-               }
-               if ((c = *f++) != 'm') {
-                       *n++ = '%';
-                       *n++ = c;
-                       continue;
-               }
-               err = 0;
-               err = strerror(olderrno);
-               if ((n + strlen(err)) < &nfmt[254]) {
-                       strcpy(n, err);
-                       n += strlen(err);
-               }
-       }
-#if !defined(VMS)
-       if (!syslogit)
-#endif /* VMS */
-           *n++ = '\n';
-       *n = '\0';
+       format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
 
        vsnprintf(buf, sizeof(buf), nfmt, ap);
 #if DEBUG
        if (debug)
                printf("msyslog: %s\n", buf);
 #endif
-#if !defined(VMS) && !defined (SYS_VXWORKS)
-       if (syslogit)
-           syslog(level, "%s", buf);
-       else
-#endif /* VMS  && SYS_VXWORKS*/
-       {
-               FILE *out_file = syslog_file ? syslog_file
-                       : level <= LOG_ERR ? stderr : stdout;
-               /* syslog() provides the timestamp, so if we're not using
-                  syslog, we must provide it. */
-               prog = strrchr(progname, separator);
-               if (prog == NULL)
-                   prog = progname;
-               else
-                   prog++;
-               (void) fprintf(out_file, "%s ", humanlogtime ());
-               (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf);
-               fflush (out_file);
-       }
+       addto_syslog(level, buf);
+       va_end(ap);
+}
+#if defined(__STDC__) || defined(HAVE_STDARG_H)
+void netsyslog(int level, const char *fmt, ...)
+#else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
+     /*VARARGS*/
+     void netsyslog(va_alist)
+     va_dcl
+#endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
+{
+#if defined(__STDC__) || defined(HAVE_STDARG_H)
+#else
+       int level;
+       const char *fmt;
+#endif
+       va_list ap;
+       char buf[1025], nfmt[256];
+       register char *prog;
+
+       /*
+        * Save the error value as soon as possible
+        */
+#ifdef SYS_WINNT
+       int errval = WSAGetLastError();
+#else
+       int errval = errno;
+#endif
+       char *err;
+
+#if defined(__STDC__) || defined(HAVE_STDARG_H)
+       va_start(ap, fmt);
+#else
+       va_start(ap);
+
+       level = va_arg(ap, int);
+       fmt = va_arg(ap, char *);
+#endif
+       format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
+
+       vsnprintf(buf, sizeof(buf), nfmt, ap);
+#if DEBUG
+       if (debug)
+               printf("netsyslog: %s\n", buf);
+#endif
+       addto_syslog(level, buf);
        va_end(ap);
 }
index c9cb51fe07a8807a86c0710f665a5f3de0e57fd2..8800298bdd796406c9a75c076a03d942415e8fee 100644 (file)
@@ -58,9 +58,6 @@ extern int listen_to_virtual_ips;
 #include <transmitbuff.h>
 #endif
 
-/* Temporarily define netsyslog as an alias to msyslog */
-#define netsyslog msyslog
-
 /*
  * We do asynchronous input using the SIGIO facility.  A number of
  * recvbuf buffers are preallocated for input. In the signal
@@ -402,7 +399,7 @@ create_sockets(
                        continue;
 
                /* Check to see if we are going to use the interface */
-               if (address_okay(&isc_if)) {
+               if (address_okay(&isc_if) == ISC_TRUE) {
                        convert_isc_if(&isc_if, &inter_list[idx], port);
                        inter_list[idx].fd = INVALID_SOCKET;
                        inter_list[idx].bfd = INVALID_SOCKET;
diff --git a/ports/winnt/libntp/log.c b/ports/winnt/libntp/log.c
new file mode 100644 (file)
index 0000000..991b498
--- /dev/null
@@ -0,0 +1,161 @@
+/* Microsoft Developer Support Copyright (c) 1993 Microsoft Corporation. */
+
+/* Skip asynch rpc inclusion */
+#ifndef __RPCASYNC_H__
+#define __RPCASYNC_H__
+#endif
+
+#include <windows.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "messages.h"
+#include "log.h"
+
+#define PERR(bSuccess, api) {if(!(bSuccess)) printf("%s: Error %d from %s \
+    on line %d\n", __FILE__, GetLastError(), api, __LINE__);}
+
+
+/*********************************************************************
+* FUNCTION: addSourceToRegistry(LPSTR pszAppname, LPSTR pszMsgDLL)   *
+*                                                                    *
+* PURPOSE: Add a source name key, message DLL name value, and        *
+*          message type supported value to the registry              *
+*                                                                    *
+* INPUT: source name, path of message DLL                            *
+*                                                                    *
+* RETURNS: none                                                      *
+*********************************************************************/
+
+void addSourceToRegistry(LPSTR pszAppname, LPSTR pszMsgDLL)
+{
+  HKEY hk;                      /* registry key handle */
+  DWORD dwData;
+  BOOL bSuccess;
+  char   regarray[200];
+  char *lpregarray = regarray;
+
+  /* When an application uses the RegisterEventSource or OpenEventLog
+     function to get a handle of an event log, the event loggging service
+     searches for the specified source name in the registry. You can add a
+     new source name to the registry by opening a new registry subkey
+     under the Application key and adding registry values to the new
+     subkey. */
+
+  strcpy(lpregarray, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\");
+  strcat(lpregarray, pszAppname);
+
+  /* Create a new key for our application */
+  bSuccess = RegCreateKey(HKEY_LOCAL_MACHINE,lpregarray, &hk);
+  PERR(bSuccess == ERROR_SUCCESS, "RegCreateKey");
+
+  /* Add the Event-ID message-file name to the subkey. */
+  bSuccess = RegSetValueEx(hk,  /* subkey handle         */
+      "EventMessageFile",       /* value name            */
+      0,                        /* must be zero          */
+      REG_EXPAND_SZ,            /* value type            */
+      (LPBYTE) pszMsgDLL,       /* address of value data */
+      strlen(pszMsgDLL) + 1);   /* length of value data  */
+  PERR(bSuccess == ERROR_SUCCESS, "RegSetValueEx");
+
+  /* Set the supported types flags and addit to the subkey. */
+  dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
+      EVENTLOG_INFORMATION_TYPE;
+  bSuccess = RegSetValueEx(hk,  /* subkey handle                */
+      "TypesSupported",         /* value name                   */
+      0,                        /* must be zero                 */
+      REG_DWORD,                /* value type                   */
+      (LPBYTE) &dwData,         /* address of value data        */
+      sizeof(DWORD));           /* length of value data         */
+  PERR(bSuccess == ERROR_SUCCESS, "RegSetValueEx");
+  RegCloseKey(hk);
+  return;
+}
+
+/*********************************************************************
+* FUNCTION: reportAnEvent(DWORD dwIdEvent, WORD cStrings,            *
+*                         LPTSTR *ppszStrings);                      *
+*                                                                    *
+* PURPOSE: add the event to the event log                            *
+*                                                                    *
+* INPUT: the event ID to report in the log, the number of insert     *
+*        strings, and an array of null-terminated insert strings     *
+*                                                                    *
+* RETURNS: none                                                      *
+*********************************************************************/
+
+void reportAnIEvent(DWORD dwIdEvent, WORD cStrings, LPTSTR *pszStrings)
+{
+  HANDLE hAppLog;
+  BOOL bSuccess;
+
+  /* Get a handle to the Application event log */
+  hAppLog = RegisterEventSource(NULL,   /* use local machine      */
+      "NTP");                   /* source name                 */
+  PERR(hAppLog, "RegisterEventSource");
+
+  /* Now report the event, which will add this event to the event log */
+  bSuccess = ReportEvent(hAppLog,       /* event-log handle            */
+      EVENTLOG_INFORMATION_TYPE,      /* event type                  */
+      0,                        /* category zero               */
+      dwIdEvent,                /* event ID                    */
+      NULL,                     /* no user SID                 */
+      cStrings,                 /* number of substitution strings     */
+      0,                        /* no binary data              */
+      pszStrings,               /* string array                */
+      NULL);                    /* address of data             */
+  PERR(bSuccess, "ReportEvent");
+  DeregisterEventSource(hAppLog);
+  return;
+}
+
+void reportAnWEvent(DWORD dwIdEvent, WORD cStrings, LPTSTR *pszStrings)
+{
+  HANDLE hAppLog;
+  BOOL bSuccess;
+
+  /* Get a handle to the Application event log */
+  hAppLog = RegisterEventSource(NULL,   /* use local machine      */
+      "NTP");                   /* source name                 */
+  PERR(hAppLog, "RegisterEventSource");
+
+  /* Now report the event, which will add this event to the event log */
+  bSuccess = ReportEvent(hAppLog,       /* event-log handle            */
+      EVENTLOG_WARNING_TYPE,      /* event type                  */
+      0,                        /* category zero               */
+      dwIdEvent,                /* event ID                    */
+      NULL,                     /* no user SID                 */
+      cStrings,                 /* number of substitution strings     */
+      0,                        /* no binary data              */
+      pszStrings,               /* string array                */
+      NULL);                    /* address of data             */
+  PERR(bSuccess, "ReportEvent");
+  DeregisterEventSource(hAppLog);
+  return;
+}
+
+void reportAnEEvent(DWORD dwIdEvent, WORD cStrings, LPTSTR *pszStrings)
+{
+  HANDLE hAppLog;
+  BOOL bSuccess;
+
+  /* Get a handle to the Application event log */
+  hAppLog = RegisterEventSource(NULL,   /* use local machine      */
+      "NTP");                   /* source name                 */
+  PERR(hAppLog, "RegisterEventSource");
+
+  /* Now report the event, which will add this event to the event log */
+  bSuccess = ReportEvent(hAppLog,       /* event-log handle            */
+      EVENTLOG_ERROR_TYPE,      /* event type                  */
+      0,                        /* category zero               */
+      dwIdEvent,                /* event ID                    */
+      NULL,                     /* no user SID                 */
+      cStrings,                 /* number of substitution strings     */
+      0,                        /* no binary data              */
+      pszStrings,               /* string array                */
+      NULL);                    /* address of data             */
+  PERR(bSuccess, "ReportEvent");
+  DeregisterEventSource(hAppLog);
+  return;
+}