Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Mar 2008 01:51:49 GMT
From:      Aaron Meihm <alm@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 138488 for review
Message-ID:  <200803250151.m2P1pnMx033154@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=138488

Change 138488 by alm@alm_praetorian on 2008/03/25 01:51:35

	First pass at new threaded model.

Affected files ...

.. //depot/projects/trustedbsd/netauditd/Makefile#6 edit
.. //depot/projects/trustedbsd/netauditd/conf.c#8 edit
.. //depot/projects/trustedbsd/netauditd/grammar.y#2 edit
.. //depot/projects/trustedbsd/netauditd/netauditd.c#17 edit
.. //depot/projects/trustedbsd/netauditd/netauditd.h#12 edit
.. //depot/projects/trustedbsd/netauditd/reader.c#1 add
.. //depot/projects/trustedbsd/netauditd/reader.h#1 add
.. //depot/projects/trustedbsd/netauditd/writer.c#1 add
.. //depot/projects/trustedbsd/netauditd/writer.h#1 add

Differences ...

==== //depot/projects/trustedbsd/netauditd/Makefile#6 (text+ko) ====

@@ -1,7 +1,7 @@
 CC = gcc
-CFLAGS = -Wall -g
+CFLAGS = -Wall -g -pthread
 TARGETS = netauditd
-OBJ = conf.o lex.yy.o netauditd.o y.tab.o
+OBJ = conf.o lex.yy.o netauditd.o reader.o y.tab.o writer.o
 
 all: $(TARGETS)
 

==== //depot/projects/trustedbsd/netauditd/conf.c#8 (text+ko) ====

@@ -23,6 +23,8 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+#include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/queue.h>
 
 #include <stdio.h>

==== //depot/projects/trustedbsd/netauditd/grammar.y#2 (text+ko) ====

@@ -36,10 +36,13 @@
 
 #include "conf.h"
 #include "netauditd.h"
+#include "reader.h"
+#include "writer.h"
 
 #define AU_CMPNT_INIT(x)	x = malloc(sizeof(struct au_cmpnt)); \
 				assert (x != NULL); \
-				bzero(x, sizeof(struct au_cmpnt));
+				bzero(x, sizeof(struct au_cmpnt)); \
+				TAILQ_INIT(&x->ac_sbuffers);
 
 static int	ainfo_passive;
 
@@ -115,6 +118,8 @@
 		new->ac_type = COMPONENT_NET;
 		new->ac_name = $3;
 		new->ac_ainfo = $4;
+		new->ac_init_func = writer_init_net;
+		writer_q_init(new);
 		$$ = new;
 	}
 	|
@@ -125,6 +130,8 @@
 		new->ac_type = COMPONENT_TRAIL;
 		new->ac_name = $2;
 		new->ac_path = $3;
+		new->ac_init_func = writer_init_trail;
+		writer_q_init(new);
 		$$ = new;
 	}
 	;
@@ -137,6 +144,8 @@
 		new->ac_type = COMPONENT_PIPE;
 		new->ac_name = $2;
 		new->ac_path = $3;
+		new->ac_init_func = reader_init_pipe;
+		new->ac_read_func = reader_read_pipe;
 		$$ = new;
 	}
 	|
@@ -147,6 +156,8 @@
 		new->ac_type = COMPONENT_NET;
 		new->ac_name = $3;
 		new->ac_ainfo = $4;
+		new->ac_init_func = reader_init_net;
+		new->ac_read_func = reader_accept_client;
 		$$ = new;
 	}
 	;

==== //depot/projects/trustedbsd/netauditd/netauditd.c#17 (text+ko) ====

@@ -30,25 +30,57 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <netdb.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
 
 #include "conf.h"
 #include "netauditd.h"
+#include "reader.h"
+#include "writer.h"
 
+static int		debug_flag;
+pthread_mutex_t		debug_mutex;
 ac_head_t		ac_list_src;
 ac_head_t		ac_list_dst;
 
 extern char		*conf_path;
 
+void
+dprintf(char *fmt, ...)
+{
+	char buf[1024];
+	va_list ap;
+
+	if (!debug_flag)
+		return;
+	if (pthread_mutex_lock(&debug_mutex) != 0)
+		exit(2);
+	va_start(ap, fmt);
+	(void) vsnprintf(buf, sizeof(buf), fmt, ap);
+	va_end(ap);
+	(void) printf("debug: %s\n", buf);
+	if (pthread_mutex_unlock(&debug_mutex) != 0)
+		exit(2);
+}
+
 int
 main(int argc, char *argv[])
 {
+	pthread_t writer_thread;
 	char ch;
 
+	if (pthread_mutex_init(&debug_mutex, NULL) != 0)
+		exit(2);
 	conf_path = DEFAULT_CONF_PATH;
-	while ((ch = getopt(argc, argv, "f:h")) != -1) {
+	while ((ch = getopt(argc, argv, "df:h")) != -1) {
 		switch (ch) {
+		case 'd':
+			debug_flag = 1;
+			break;
 		case 'f':
 			conf_path = optarg;
 			break;
@@ -58,15 +90,33 @@
 			/* Not reached */
 		}
 	}
+	(void) signal(SIGPIPE, SIG_IGN);
 	TAILQ_INIT(&ac_list_src);
 	TAILQ_INIT(&ac_list_dst);
 	conf_load(conf_path);
+	if (pthread_create(&writer_thread, NULL, writer_start, NULL) != 0)
+		exit(2);
+	reader_start();
 	return (0);
 }
 
+int
+nonblock(int fd)
+{
+	int flags;
+
+	flags = fcntl(fd, F_GETFL);
+	if (flags == -1)
+		return (-1);
+	flags |= O_NONBLOCK;
+	if (fcntl(fd, F_SETFL, flags) == -1)
+		return (-1);
+	return (0);
+}
+
 void
 usage()
 {
-	(void) fputs("usage: netauditd [-h] [-f path]\n", stderr);
+	(void) fputs("usage: netauditd [-dh] [-f path]\n", stderr);
 	exit(1);
 }

==== //depot/projects/trustedbsd/netauditd/netauditd.h#12 (text+ko) ====

@@ -32,19 +32,67 @@
 	COMPONENT_TRAIL
 };
 
+#define		FLAG_ONLINE	1		/* Component is online */
+#define		FLAG_CONNECTING	(1 << 1)	/* Component connecting */
+
+struct audit_record {
+	void			*ar_buf;
+	u_int32_t		ar_record_len;
+	int			ar_refcount;
+};
+
+struct au_queue_ent {
+	TAILQ_ENTRY(au_queue_ent)	aq_glue;
+	struct audit_record		*aq_record;
+	u_int32_t			aq_remain;
+};
+
+typedef TAILQ_HEAD(, au_queue_ent)	au_q_t;
+
+struct au_qpair {
+	au_q_t				qp_a, qp_b;
+	int				qp_ready;
+	au_q_t				*qp_read, *qp_write;
+	pthread_mutex_t			qp_lock;
+	pthread_cond_t			qp_cond;
+	u_int32_t			qp_read_size;
+	time_t				qp_time;
+};
+
 struct au_cmpnt {
 	struct addrinfo		*ac_ainfo;
 	struct au_cmpnt		**ac_dsts;
+	time_t			ac_failed;
 	int			ac_fd;
+	int			ac_flags;
 	TAILQ_ENTRY(au_cmpnt)	ac_glue;
 	char			*ac_name;
 	int			ac_ndsts;
 	char			*ac_path;
+	struct au_qpair		ac_q;
 	int			ac_type;
+
+	TAILQ_HEAD(, au_src_buffer)	ac_sbuffers;
+
+	int			(*ac_init_func)(struct au_cmpnt *);
+	int			(*ac_read_func)(struct au_cmpnt *);
+};
+
+struct au_src_buffer {
+	struct sockaddr			as_addr;
+	socklen_t			as_addrlen;
+	int				as_fd;
+	TAILQ_ENTRY(au_src_buffer)	as_glue;
+	u_char				as_header[5];
+	struct au_cmpnt			*as_parent;
+	u_int32_t			as_nread;
+	struct audit_record		*as_record;
 };
 
 typedef TAILQ_HEAD(, au_cmpnt)	ac_head_t;
 extern ac_head_t		ac_list_src;
 extern ac_head_t		ac_list_dst;
 
+void		dprintf(char *, ...);
+int		nonblock(int);
 void		usage(void);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200803250151.m2P1pnMx033154>