File syslog-ng-add-log-sockets.dif of Package syslog-ng

--- src/add_socket.c
+++ src/add_socket.c	2008/02/26 11:16:31
@@ -0,0 +1,282 @@
+#ifndef  _GNU_SOURCE
+#define  _GNU_SOURCE
+#endif
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <glib.h>
+
+#include "afunix.h"
+#include "sgroup.h"
+#include "messages.h"
+#include "add_socket.h"
+
+#ifndef DEV_LOG_SOCKET
+#define DEV_LOG_SOCKET     "/dev/log"
+#endif
+
+struct additional_socket
+{
+	gint   skip;
+	gchar *path;
+};
+
+static struct additional_socket *
+make_additional_socket (const gchar *path, gint skip)
+{
+	struct additional_socket *sock;
+	sock = g_new(struct additional_socket, 1);
+	if(sock)
+	{
+		sock->skip = skip;
+		sock->path = g_strdup(path);
+		if( !sock->path)
+		{
+			g_free(sock);
+			sock = NULL;
+		}
+	}
+	return sock;
+}
+
+static void
+free_additional_socket (struct additional_socket *sock)
+{
+	if(sock)
+	{
+		g_free(sock->path);
+		g_free(sock);
+	}
+}
+
+static void
+add_sockets_skip_reset (GSList      *socket_list)
+{
+	GSList                   *ptr;
+	struct additional_socket *sock;
+
+	for(ptr = socket_list; ptr; ptr = g_slist_next(ptr))
+	{
+		sock = (struct additional_socket *)ptr->data;
+		if(sock)
+		{
+			sock->skip = 0;
+		}
+	}
+}
+
+static void
+add_sockets_skip_mark  (GSList      *socket_list,
+                        const gchar *socket_path)
+{
+	GSList                   *ptr;
+	struct additional_socket *sock;
+
+	for(ptr = socket_list; ptr; ptr = g_slist_next(ptr))
+	{
+		sock = (struct additional_socket *)ptr->data;
+		if(sock && socket_path && !strcmp(sock->path, socket_path))
+		{
+			sock->skip = 1;
+		}
+	}
+}
+
+static LogDriver *
+add_sockets_clone_unix_source(LogDriver *log_drv, char *path)
+{
+	LogDriver            *new_drv;
+	AFUnixSourceDriver   *log_u_drv;
+	AFUnixSourceDriver   *new_u_drv;
+	AFSocketSourceDriver *log_s_drv;
+	AFSocketSourceDriver *new_s_drv;
+
+	log_u_drv = (AFUnixSourceDriver *)log_drv;
+	log_s_drv = (AFSocketSourceDriver *)log_drv;
+
+	new_drv  = afunix_sd_new(path, log_u_drv->super.flags);
+	if( !new_drv)
+		return NULL;
+
+	new_drv->optional = log_drv->optional;
+
+	/* unix source driver params */
+	new_u_drv = (AFUnixSourceDriver *)new_drv;
+	new_u_drv->owner = log_u_drv->owner;
+	new_u_drv->group = log_u_drv->group;
+	new_u_drv->perm  = log_u_drv->perm;
+
+	/* socket options */
+	new_u_drv->sock_options.sndbuf    = log_u_drv->sock_options.sndbuf;
+	new_u_drv->sock_options.rcvbuf    = log_u_drv->sock_options.rcvbuf;
+	new_u_drv->sock_options.broadcast = log_u_drv->sock_options.broadcast;
+	new_u_drv->sock_options.keepalive = log_u_drv->sock_options.keepalive;
+
+	/* socket source params */
+	new_s_drv = (AFSocketSourceDriver *)new_drv;
+	afsocket_sd_set_keep_alive(new_drv,
+		log_s_drv->flags & AFSOCKET_KEEP_ALIVE);
+	afsocket_sd_set_max_connections(new_drv,
+		log_s_drv->max_connections);
+
+	/* source reader options */
+	new_s_drv->reader_options.options =
+	log_s_drv->reader_options.options;
+
+	new_s_drv->reader_options.msg_size =
+	log_s_drv->reader_options.msg_size;
+
+	new_s_drv->reader_options.source_opts.init_window_size =
+	log_s_drv->reader_options.source_opts.init_window_size;
+
+	new_s_drv->reader_options.fetch_limit =
+	log_s_drv->reader_options.fetch_limit;
+
+	new_s_drv->reader_options.prefix =
+	g_strdup(log_s_drv->reader_options.prefix);
+
+	new_s_drv->reader_options.padding =
+	log_s_drv->reader_options.padding;
+
+	new_s_drv->reader_options.follow_freq =
+	log_s_drv->reader_options.follow_freq;
+
+	new_s_drv->reader_options.keep_timestamp =
+	log_s_drv->reader_options.keep_timestamp;
+
+	new_s_drv->reader_options.zone_offset =
+	log_s_drv->reader_options.zone_offset;
+
+	return new_drv;
+}
+
+static void
+add_sockets_foreach_src(gpointer key, gpointer value, gpointer user_data)
+{
+	GSList         *socket_list  = (GSList *)user_data;
+	LogSourceGroup *source_group = (LogSourceGroup *)value;
+	LogDriver      *dev_log_drv  = NULL;
+	LogDriver      *drv;
+
+	if(!socket_list || !source_group)
+		return;
+
+
+	/*
+	** find /dev/log and set skip mark to all
+	** sockets that are already in the config.
+	*/
+	for(drv = source_group->drivers; drv; drv = drv->drv_next)
+	{
+		LogPipe *pipe = &drv->super;
+
+		if(pipe->free_fn == afunix_sd_free)
+		{
+			AFUnixSourceDriver *udrv;
+			udrv = (AFUnixSourceDriver *)drv;
+
+			if(strcmp(udrv->filename, DEV_LOG_SOCKET) == 0)
+			{
+				dev_log_drv = drv;
+			}
+			else
+			{
+				add_sockets_skip_mark(socket_list,
+				                      udrv->filename);
+			}
+		}
+	}
+
+	if(dev_log_drv)
+	{
+		GSList                   *ptr;
+		struct additional_socket *sock;
+
+		for(ptr = socket_list; ptr; ptr = g_slist_next(ptr))
+		{
+			sock = (struct additional_socket *)ptr->data;
+			if(sock && sock->skip == 0)
+			{
+				drv = add_sockets_clone_unix_source(
+					dev_log_drv, sock->path
+				);
+
+				if(drv)
+				{
+					drv->drv_next = source_group->drivers;
+					source_group->drivers = drv;
+				}
+			}
+		}
+	}
+}
+
+void
+add_sockets_to_sources (GSList       *socket_list,
+                        GHashTable   *source_groups)
+{
+	if( !socket_list || !source_groups)
+		return;
+
+	/* reset additional log socket skip flag */
+	add_sockets_skip_reset(socket_list);
+
+	g_hash_table_foreach(
+		source_groups, add_sockets_foreach_src, socket_list
+	);
+}
+
+void
+add_sockets_free_list  (GSList      **socket_list)
+{
+	if( !socket_list)
+		return;
+
+	GSList                   *ptr;
+	struct additional_socket *sock;
+	for(ptr = *socket_list; ptr; ptr = g_slist_next(ptr))
+	{
+		sock = (struct additional_socket *)ptr->data;
+		free_additional_socket(sock);
+	}
+	g_slist_free(*socket_list);
+	socket_list = NULL;
+}
+
+gboolean
+add_sockets_append_new (GSList      **socket_list,
+                        const gchar  *socket_path,
+		        int           debug_flag)
+{
+	struct additional_socket *sock;
+
+	if( !socket_list)
+		return FALSE;
+	if( !socket_path || *socket_path == '\0')
+		return FALSE;
+	if( strcmp(socket_path, DEV_LOG_SOCKET) == 0)
+		return FALSE;
+
+	if( *socket_list)
+	{
+		GSList *ptr;
+		/* just return ok in case we already have it */
+		for(ptr = *socket_list; ptr; ptr = g_slist_next(ptr))
+		{
+			sock = (struct additional_socket *)ptr->data;
+			if(sock && strcmp(sock->path, socket_path) == 0)
+				return TRUE;
+		}
+	}
+
+	sock = make_additional_socket(socket_path, 0);
+	if( sock)
+	{
+		*socket_list = g_slist_append(*socket_list, sock);
+		return TRUE;
+	}
+	return FALSE;
+}
+
+
+/* vim: set tw=80 ts=8 sts=8 sw=8 ai noet: */
--- src/add_socket.h
+++ src/add_socket.h	2008/02/25 10:47:33
@@ -0,0 +1,19 @@
+#ifndef   ADD_SOCKET_H
+#define   ADD_SOCKET_H
+
+#include <glib.h>
+
+gboolean
+add_sockets_append_new (GSList      **socket_list,
+                        const gchar  *socket_path,
+		        int           debug_flag);
+
+void
+add_sockets_to_sources (GSList       *socket_list,
+                        GHashTable   *source_groups);
+
+void
+add_sockets_free_list  (GSList      **socket_list);
+
+#endif /* ADD_SOCKET_H */
+/* vim: set tw=80 ts=8 sts=8 sw=8 ai noet: */
--- src/afunix.c
+++ src/afunix.c	2008/02/25 13:41:38
@@ -77,7 +77,7 @@ afunix_sd_init(LogPipe *s, GlobalConfig 
   return FALSE;
 }
 
-static void 
+void 
 afunix_sd_free(LogPipe *s)
 {
   AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
--- src/afunix.h
+++ src/afunix.h	2008/02/25 13:41:08
@@ -42,6 +42,7 @@ void afunix_sd_set_gid(LogDriver *self, 
 void afunix_sd_set_perm(LogDriver *self, mode_t perm);
 
 LogDriver *afunix_sd_new(gchar *filename, guint32 flags);
+void       afunix_sd_free(LogPipe *s);
 
 typedef struct _AFUnixDestDriver
 {
--- src/cfg.c
+++ src/cfg.c	2008/02/25 15:30:12
@@ -31,6 +31,7 @@
 #include "misc.h"
 #include "logmsg.h"
 #include "dnscache.h"
+#include "add_socket.h"
 
 #include <stdio.h>
 #include <ctype.h>
@@ -364,7 +365,7 @@ cfg_free(GlobalConfig *self)
 }
 
 GlobalConfig *
-cfg_reload_config(gchar *fname, GlobalConfig *cfg)
+cfg_reload_config(gchar *fname, GlobalConfig *cfg, GSList *additional_sockets)
 {
   PersistentConfig *persist;
   GlobalConfig *new_cfg;
@@ -378,6 +379,7 @@ cfg_reload_config(gchar *fname, GlobalCo
                 NULL);
       return cfg;
     }
+  add_sockets_to_sources(additional_sockets, new_cfg->sources);
 
   persist = persist_config_new();
   cfg_deinit(cfg, persist);
--- src/cfg.h
+++ src/cfg.h	2008/02/25 15:13:29
@@ -129,7 +129,7 @@ GlobalConfig *cfg_new(gchar *fname);
 void cfg_free(GlobalConfig *self);
 gboolean cfg_init(GlobalConfig *cfg, PersistentConfig *persist);
 gboolean cfg_deinit(GlobalConfig *cfg, PersistentConfig *persist);
-GlobalConfig *cfg_reload_config(gchar *fname, GlobalConfig *cfg);
+GlobalConfig *cfg_reload_config(gchar *fname, GlobalConfig *cfg, GSList *additional_sockets);
 
 PersistentConfig *persist_config_new(void);
 void persist_config_add(PersistentConfig *self, gchar *name, gpointer store, GDestroyNotify destroy);
--- src/main.c
+++ src/main.c	2008/02/26 11:26:13
@@ -30,6 +30,7 @@
 #include "stats.h"
 #include "dnscache.h"
 #include "alarms.h"
+#include "add_socket.h"
 
 #include <sys/types.h>
 #include <stdio.h>
@@ -59,7 +60,7 @@ static gchar *chroot_dir = NULL;
 static gchar *run_as_user = NULL;
 static uid_t uid = 0;
 static gid_t gid = 0;
-
+static GSList *additional_sockets = NULL;
 
 void usage(void)
 {
@@ -81,6 +82,8 @@ void usage(void)
 #ifdef YYDEBUG
 	 "  -y, --yydebug                    Turn on yacc debug messages\n"
 #endif
+	 "  -a, --add-socket=<path>          Add an additional (chroot) log socket with same\n"
+	 "                                   type and to the same log source group as /dev/log.\n"
 	 );
 
   exit(0);
@@ -166,7 +169,7 @@ main_loop_run(GlobalConfig **cfg)
       if (sig_hup_received)
         {
           msg_notice("Configuration reload request received, reloading configuration", NULL);
-          (*cfg) = cfg_reload_config(cfgfilename, (*cfg));
+          (*cfg) = cfg_reload_config(cfgfilename, (*cfg), additional_sockets);
           sig_hup_received = FALSE;
           if ((*cfg)->stats_freq > 0)
             {
@@ -322,6 +325,7 @@ main(int argc, char *argv[])
 #ifdef YYDEBUG
       { "yydebug", no_argument, NULL, 'y' },
 #endif
+      { "add-socket", required_argument, NULL, 'a'},
       { NULL, 0, NULL, 0 }
     };
 #endif
@@ -333,9 +337,9 @@ main(int argc, char *argv[])
   
 
 #if HAVE_GETOPT_LONG
-  while ((opt = getopt_long(argc, argv, "sFf:p:dvhyVC:u:g:eR:", syslog_ng_options, NULL)) != -1)
+  while ((opt = getopt_long(argc, argv, "sFf:p:dvhyVC:u:g:eR:a:", syslog_ng_options, NULL)) != -1)
 #else
-  while ((opt = getopt(argc, argv, "sFf:p:dvhyVC:u:g:eR:")) != -1)
+  while ((opt = getopt(argc, argv, "sFf:p:dvhyVC:u:g:eR:a:")) != -1)
 #endif
     {
       switch (opt) 
@@ -384,6 +388,14 @@ main(int argc, char *argv[])
         case 'R':
           persist_file = optarg;
           break;
+	case 'a':
+	  if( !add_sockets_append_new(&additional_sockets, optarg,
+		                      (debug_flag && log_to_stderr)))
+	  {
+	    add_sockets_free_list(&additional_sockets);
+	    return 1;
+	  }
+	break;
 	case 'h':
 	default:
 	  usage();
@@ -404,11 +416,14 @@ main(int argc, char *argv[])
   cfg = cfg_new(cfgfilename);
   if (!cfg)
     {
+      add_sockets_free_list(&additional_sockets);
       return 1;
     }
+  add_sockets_to_sources(additional_sockets, cfg->sources);
 
   if (syntax_only)
     {
+      add_sockets_free_list(&additional_sockets);
       return 0;
     }
 
@@ -417,11 +432,13 @@ main(int argc, char *argv[])
 
   if (!cfg_init(cfg, persist))
     {
+      add_sockets_free_list(&additional_sockets);
       return 2;
     }
 
   if (!daemonize())
     {
+      add_sockets_free_list(&additional_sockets);
       return 2;
     }
   /* from now on internal messages are written to the system log as well */
@@ -441,6 +458,7 @@ main(int argc, char *argv[])
     }
 
   cfg_free(cfg);
+  add_sockets_free_list(&additional_sockets);
   
   child_manager_deinit();
 
--- src/Makefile.am
+++ src/Makefile.am	2008/02/22 15:04:28
@@ -17,7 +17,7 @@ libsyslog_ng_a_SOURCES = \
 	afsocket.c afsocket.h afunix.c afunix.h afinet.c afinet.h afinter.c afinter.h \
 	children.c children.h stats.c stats.h \
 	gsockaddr.c gsockaddr.h fdwrite.c fdwrite.h fdread.c fdread.h memtrace.c memtrace.h \
-	dnscache.c dnscache.h alarms.c alarms.h
+	dnscache.c dnscache.h alarms.c alarms.h add_socket.c add_socket.h
 
 cfg-lex.c: cfg-grammar.c
 
openSUSE Build Service is sponsored by