[syslog-ng] [PATCH] afuser: Use utmpx when available, instead of utmp

Gergely Nagy algernon at balabit.hu
Fri Jun 8 13:07:40 CEST 2012


FreeBSD 9 removed support for utmp, and one must use the
POSIX-specified utmpx instead. The same utmpx is available on Linux
too (and it is the same as utmp there).

The patch below converts afuser to use utmpx when available, utmp
otherwise. It is based on the post-build sed magic applied to
syslog-ng within the FreeBSD ports collection, with other bits based
on a patch from Alex Zimnitsky.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 configure.in            |    4 ++--
 lib/utils.c             |    4 +++-
 lib/utils.h             |    7 ++++++-
 modules/afuser/afuser.c |   20 +++++++++++++++++++-
 4 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/configure.in b/configure.in
index aafb980..c76d7d9 100644
--- a/configure.in
+++ b/configure.in
@@ -383,7 +383,7 @@ dnl ***************************************************************************
 
 AC_HEADER_STDC
 AC_CHECK_HEADER(dmalloc.h)
-AC_CHECK_HEADERS(strings.h getopt.h stropts.h sys/strlog.h door.h sys/capability.h sys/prctl.h)
+AC_CHECK_HEADERS(strings.h getopt.h stropts.h sys/strlog.h door.h sys/capability.h sys/prctl.h utmpx.h)
 AC_CHECK_HEADERS(tcpd.h)
 
 
@@ -479,7 +479,7 @@ if test "x$enable_linux_caps" = "xyes" -o "x$enable_linux_caps" = "xauto"; then
 	AC_CHECK_LIB(cap, cap_set_proc, LIBCAP_LIBS="-lcap")
 fi
 
-AC_CHECK_FUNCS(strdup strtol strtoll strtoimax inet_aton inet_ntoa getopt_long getaddrinfo getutent pread pwrite strcasestr memrchr localtime_r gmtime_r)
+AC_CHECK_FUNCS(strdup strtol strtoll strtoimax inet_aton inet_ntoa getopt_long getaddrinfo getutent getutxent pread pwrite strcasestr memrchr localtime_r gmtime_r)
 old_LIBS=$LIBS
 LIBS=$BASE_LIBS
 AC_CHECK_FUNCS(clock_gettime)
diff --git a/lib/utils.c b/lib/utils.c
index 2b5c525..1f06e12 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -33,7 +33,9 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <fcntl.h>
+#if HAVE_UTMP_H
 #include <utmp.h>
+#endif
 
 #include "utils.h"
 
@@ -47,7 +49,7 @@ int inet_aton(const char *cp, struct in_addr *addr)
 }
 #endif
 
-#ifndef HAVE_GETUTENT
+#if !defined(HAVE_GETUTENT) && !defined(HAVE_GETUTXENT) && defined(HAVE_UTMP_H)
 
 static int utent_fd = -1;
 
diff --git a/lib/utils.h b/lib/utils.h
index 86e3a7f..a0f3dcc 100644
--- a/lib/utils.h
+++ b/lib/utils.h
@@ -28,13 +28,18 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+
+#ifdef HAVE_UTMPX_H
+#include <utmpx.h>
+#else
 #include <utmp.h>
+#endif
 
 #ifndef HAVE_INET_ATON
 int inet_aton(const char *cp, struct in_addr *addr);
 #endif
 
-#ifndef HAVE_GETUTENT
+#if !defined(HAVE_GETUTENT) && !defined(HAVE_GETUTXENT)
 struct utmp *getutent(void);
 void endutent(void);
 #endif
diff --git a/modules/afuser/afuser.c b/modules/afuser/afuser.c
index 8f170e5..7d082b2 100644
--- a/modules/afuser/afuser.c
+++ b/modules/afuser/afuser.c
@@ -25,7 +25,13 @@
 #include "alarms.h"
 #include "messages.h"
 
+#ifdef HAVE_UTMPX_H
+#include <utmpx.h>
+#define ut_name ut_user
+#else
 #include <utmp.h>
+#endif
+
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -46,7 +52,11 @@ afuser_dd_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options,
 {
   AFUserDestDriver *self = (AFUserDestDriver *) s;
   gchar buf[8192];
+#ifdef HAVE_UTMPX_H
+  struct utmpx *ut;
+#else
   struct utmp *ut;
+#endif
   GString *timestamp;
   time_t now;
   
@@ -63,7 +73,11 @@ afuser_dd_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options,
   g_string_free(timestamp, TRUE);
   
   /* NOTE: there's a private implementations of getutent in utils.c on Systems which do not provide one. */
-  while ((ut = getutent())) 
+#ifdef HAVE_GETUTXENT
+  while ((ut = getutxent()))
+#else
+  while ((ut = getutent()))
+#endif
     {
 #if HAVE_MODERN_UTMP
       if (ut->ut_type == USER_PROCESS &&
@@ -106,7 +120,11 @@ afuser_dd_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options,
             }
         }
     }
+#if HAVE_UTMPX_H
+  endutxent();
+#else
   endutent();
+#endif
 finish:
   log_msg_ack(msg, path_options);
   log_msg_unref(msg);
-- 
1.7.9




More information about the syslog-ng mailing list