about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ngircd/Makefile.am10
-rw-r--r--src/ngircd/conf.c2
-rw-r--r--src/ngircd/conf.h4
-rw-r--r--src/ngircd/conn.c23
-rw-r--r--src/ngircd/conn.h4
-rw-r--r--src/ngircd/io.c3
-rw-r--r--src/ngircd/proc.c102
-rw-r--r--src/ngircd/proc.h35
-rw-r--r--src/ngircd/resolve.c99
-rw-r--r--src/ngircd/resolve.h30
10 files changed, 181 insertions, 131 deletions
diff --git a/src/ngircd/Makefile.am b/src/ngircd/Makefile.am
index f94f8d92..2bce2924 100644
--- a/src/ngircd/Makefile.am
+++ b/src/ngircd/Makefile.am
@@ -1,6 +1,6 @@
 #
 # ngIRCd -- The Next Generation IRC Daemon
-# Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
+# Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -8,8 +8,6 @@
 # (at your option) any later version.
 # Please read the file COPYING, README and AUTHORS for more information.
 #
-# $Id: Makefile.am,v 1.51 2008/02/26 22:04:17 fw Exp $
-#
 
 AUTOMAKE_OPTIONS = ../portab/ansi2knr
 
@@ -23,7 +21,7 @@ sbin_PROGRAMS = ngircd
 ngircd_SOURCES = ngircd.c array.c channel.c client.c conf.c conn.c conn-func.c \
 	conn-ssl.c conn-zip.c hash.c io.c irc.c irc-channel.c irc-info.c irc-login.c \
 	irc-mode.c irc-op.c irc-oper.c irc-server.c irc-write.c lists.c log.c \
-	match.c op.c numeric.c parse.c rendezvous.c resolve.c
+	match.c op.c numeric.c parse.c proc.c rendezvous.c resolve.c
 
 ngircd_LDFLAGS = -L../portab -L../tool -L../ipaddr
 
@@ -32,8 +30,8 @@ ngircd_LDADD = -lngportab -lngtool -lngipaddr
 noinst_HEADERS = ngircd.h array.h channel.h client.h conf.h conf-ssl.h conn.h \
 	conn-func.h conn-ssl.h conn-zip.h hash.h io.h irc.h irc-channel.h \
 	irc-info.h irc-login.h irc-mode.h irc-op.h irc-oper.h irc-server.h \
-	irc-write.h lists.h log.h match.h numeric.h op.h parse.h rendezvous.h \
-	resolve.h defines.h messages.h
+	irc-write.h lists.h log.h match.h numeric.h op.h parse.h proc.h \
+	rendezvous.h resolve.h defines.h messages.h
 
 clean-local:
 	rm -f check-version check-help lint.out
diff --git a/src/ngircd/conf.c b/src/ngircd/conf.c
index f9db5c75..43b07b73 100644
--- a/src/ngircd/conf.c
+++ b/src/ngircd/conf.c
@@ -1481,7 +1481,7 @@ Init_Server_Struct( CONF_SERVER *Server )
 
 	if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
 
-	Resolve_Init(&Server->res_stat);
+	Proc_InitStruct(&Server->res_stat);
 	Server->conn_id = NONE;
 	memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
 } /* Init_Server_Struct */
diff --git a/src/ngircd/conf.h b/src/ngircd/conf.h
index 5764d0f3..8e397faf 100644
--- a/src/ngircd/conf.h
+++ b/src/ngircd/conf.h
@@ -22,7 +22,7 @@
 #include "portab.h"
 #include "tool.h"
 #include "ng_ipaddr.h"
-#include "resolve.h"
+#include "proc.h"
 #include "conf-ssl.h"
 
 
@@ -41,7 +41,7 @@ typedef struct _Conf_Server
 	UINT16 port;			/* Server port */
 	int group;			/* Group of server */
 	time_t lasttry;			/* Last connect attempt */
-	RES_STAT res_stat;		/* Status of the resolver */
+	PROC_STAT res_stat;		/* Status of the resolver */
 	int flags;			/* Flags */
 	CONN_ID conn_id;		/* ID of server connection or NONE */
 	ng_ipaddr_t bind_addr;		/* source address to use for outgoing
diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c
index bfcb8d4b..495c54f1 100644
--- a/src/ngircd/conn.c
+++ b/src/ngircd/conn.c
@@ -21,9 +21,9 @@
 #include "imp.h"
 #include <assert.h>
 #ifdef PROTOTYPES
-#	include <stdarg.h>
+# include <stdarg.h>
 #else
-#	include <varargs.h>
+# include <varargs.h>
 #endif
 #include <stdio.h>
 #include <stdlib.h>
@@ -53,20 +53,23 @@
 
 #include "array.h"
 #include "defines.h"
-#include "resolve.h"
 
 #include "exp.h"
 #include "conn.h"
 
 #include "imp.h"
 #include "ngircd.h"
+#include "array.h"
 #include "client.h"
 #include "conf.h"
 #include "conn-ssl.h"
 #include "conn-zip.h"
 #include "conn-func.h"
 #include "log.h"
+#include "ng_ipaddr.h"
 #include "parse.h"
+#include "proc.h"
+#include "resolve.h"
 #include "tool.h"
 
 #ifdef ZEROCONF
@@ -752,7 +755,7 @@ Conn_Handler(void)
 			if (SSL_WantWrite(&My_Connections[i]))
 				continue; /* TLS/SSL layer needs to write data; deal with this first */
 #endif
-			if (Resolve_INPROGRESS(&My_Connections[i].res_stat)) {
+			if (Proc_InProgress(&My_Connections[i].res_stat)) {
 				/* Wait for completion of resolver sub-process ... */
 				io_event_del(My_Connections[i].sock,
 					     IO_WANTREAD);
@@ -1071,8 +1074,8 @@ Conn_Close( CONN_ID Idx, const char *LogMsg, const char *FwdMsg, bool InformClie
 	}
 
 	/* cancel running resolver */
-	if (Resolve_INPROGRESS(&My_Connections[Idx].res_stat))
-		Resolve_Shutdown(&My_Connections[Idx].res_stat);
+	if (Proc_InProgress(&My_Connections[Idx].res_stat))
+		Proc_Kill(&My_Connections[Idx].res_stat);
 
 	/* Servers: Modify time of next connect attempt? */
 	Conf_UnsetServer( Idx );
@@ -1763,7 +1766,7 @@ Check_Servers( void )
 		/* Okay, try to connect now */
 		Conf_Server[i].lasttry = time_now;
 		Conf_Server[i].conn_id = SERVER_WAIT;
-		assert(Resolve_Getfd(&Conf_Server[i].res_stat) < 0);
+		assert(Proc_GetPipeFd(&Conf_Server[i].res_stat) < 0);
 		Resolve_Name(&Conf_Server[i].res_stat, Conf_Server[i].host, cb_Connect_to_Server);
 	}
 } /* Check_Servers */
@@ -1886,7 +1889,7 @@ Init_Conn_Struct(CONN_ID Idx)
 	My_Connections[Idx].signon = now;
 	My_Connections[Idx].lastdata = now;
 	My_Connections[Idx].lastprivmsg = now;
-	Resolve_Init(&My_Connections[Idx].res_stat);
+	Proc_InitStruct(&My_Connections[Idx].res_stat);
 } /* Init_Conn_Struct */
 
 
@@ -1941,7 +1944,7 @@ cb_Connect_to_Server(int fd, UNUSED short events)
 	LogDebug("Resolver: Got forward lookup callback on fd %d, events %d", fd, events);
 
 	for (i=0; i < MAX_SERVERS; i++) {
-		  if (Resolve_Getfd(&Conf_Server[i].res_stat) == fd )
+		  if (Proc_GetPipeFd(&Conf_Server[i].res_stat) == fd )
 			  break;
 	}
 
@@ -2000,7 +2003,7 @@ cb_Read_Resolver_Result( int r_fd, UNUSED short events )
 	/* Search associated connection ... */
 	for( i = 0; i < Pool_Size; i++ ) {
 		if(( My_Connections[i].sock != NONE )
-		  && ( Resolve_Getfd(&My_Connections[i].res_stat) == r_fd ))
+		  && (Proc_GetPipeFd(&My_Connections[i].res_stat) == r_fd))
 			break;
 	}
 	if( i >= Pool_Size ) {
diff --git a/src/ngircd/conn.h b/src/ngircd/conn.h
index 96d1f33b..2ca0d628 100644
--- a/src/ngircd/conn.h
+++ b/src/ngircd/conn.h
@@ -48,7 +48,7 @@ typedef long CONN_ID;
 #ifdef CONN_MODULE
 
 #include "defines.h"
-#include "resolve.h"
+#include "proc.h"
 #include "array.h"
 #include "tool.h"
 #include "ng_ipaddr.h"
@@ -69,7 +69,7 @@ typedef struct _Connection
 {
 	int sock;			/* Socket handle */
 	ng_ipaddr_t addr;		/* Client address */
-	RES_STAT res_stat;		/* Status of resolver process */
+	PROC_STAT res_stat;		/* Status of resolver process */
 	char host[HOST_LEN];		/* Hostname */
 	array rbuf;			/* Read buffer */
 	array wbuf;			/* Write buffer */
diff --git a/src/ngircd/io.c b/src/ngircd/io.c
index 18a29aa6..e811e81b 100644
--- a/src/ngircd/io.c
+++ b/src/ngircd/io.c
@@ -104,8 +104,7 @@ static bool io_event_change_devpoll(int fd, short what);
 
 #ifdef IO_USE_SELECT
 #include "defines.h"	/* for conn.h */
-#include "conn.h"	/* for CONN_IDX (needed by resolve.h) */
-#include "resolve.h"	/* for RES_STAT (needed by conf.h) */
+#include "proc.h"	/* for PROC_STAT (needed by conf.h) */
 #include "conf.h"	/* for Conf_MaxConnections */
 
 static fd_set readers;
diff --git a/src/ngircd/proc.c b/src/ngircd/proc.c
new file mode 100644
index 00000000..5f3cadeb
--- /dev/null
+++ b/src/ngircd/proc.c
@@ -0,0 +1,102 @@
+/*
+ * ngIRCd -- The Next Generation IRC Daemon
+ * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * Please read the file COPYING, README and AUTHORS for more information.
+ *
+ * Process management
+ */
+
+#include "portab.h"
+
+#include "imp.h"
+#include <assert.h>
+#include <errno.h>
+#include <signal.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "log.h"
+#include "io.h"
+
+#include "exp.h"
+#include "proc.h"
+
+/**
+ * Initialize process structure.
+ */
+GLOBAL void
+Proc_InitStruct (PROC_STAT *proc)
+{
+	assert(proc != NULL);
+	proc->pid = 0;
+	proc->pipe_fd = -1;
+}
+
+/**
+ * Fork a child process.
+ */
+GLOBAL pid_t
+Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short))
+{
+	pid_t pid;
+
+	assert(proc != NULL);
+	assert(pipefds != NULL);
+	assert(cbfunc != NULL);
+
+	if (pipe(pipefds) != 0) {
+		Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
+		    strerror(errno));
+		return -1;
+	}
+
+	pid = fork();
+	switch (pid) {
+	case -1:
+		/* Error on fork: */
+		Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
+		close(pipefds[0]);
+		close(pipefds[1]);
+		return -1;
+	case 0:
+		/* New child process: */
+		close(pipefds[0]);
+		return 0;
+	}
+
+	/* Old parent process: */
+	close(pipefds[1]);
+
+	if (!io_setnonblock(pipefds[0])
+	 || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
+		Log(LOG_CRIT, "Can't register callback for child process: %s!",
+		    strerror(errno));
+		close(pipefds[0]);
+		return -1;
+	}
+
+	proc->pid = pid;
+	proc->pipe_fd = pipefds[0];
+	return pid;
+}
+
+/**
+ * Kill forked child process.
+ */
+GLOBAL void
+Proc_Kill(PROC_STAT *proc)
+{
+	assert(proc != NULL);
+	assert(proc->pipe_fd >= 0);
+
+	io_close(proc->pipe_fd);
+	kill(proc->pid, SIGTERM);
+	Proc_InitStruct(proc);
+}
+
+/* -eof- */
diff --git a/src/ngircd/proc.h b/src/ngircd/proc.h
new file mode 100644
index 00000000..c342e1df
--- /dev/null
+++ b/src/ngircd/proc.h
@@ -0,0 +1,35 @@
+/*
+ * ngIRCd -- The Next Generation IRC Daemon
+ * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * Please read the file COPYING, README and AUTHORS for more information.
+ *
+ * Process management (header)
+ */
+
+#ifndef __proc_h__
+#define __proc_h__
+
+/* This struct must not be accessed directly! */
+typedef struct _Proc_Stat {
+	pid_t pid;	/* PID of the child process or 0 if none */
+	int pipe_fd;	/* Pipe file descriptor or -1 if none */
+} PROC_STAT;
+
+#define Proc_InProgress(x)	((x)->pid != 0)
+#define Proc_GetPipeFd(x)	((x)->pipe_fd)
+
+GLOBAL void Proc_InitStruct PARAMS((PROC_STAT *proc));
+
+GLOBAL pid_t Proc_Fork PARAMS((PROC_STAT *proc, int *pipefds,
+			       void (*cbfunc)(int, short)));
+
+GLOBAL void Proc_Kill PARAMS((PROC_STAT *proc));
+
+#endif
+
+/* -eof- */
diff --git a/src/ngircd/resolve.c b/src/ngircd/resolve.c
index bccf5180..e64688ec 100644
--- a/src/ngircd/resolve.c
+++ b/src/ngircd/resolve.c
@@ -30,9 +30,12 @@
 #endif
 #endif
 
+#include "array.h"
 #include "conn.h"
 #include "defines.h"
 #include "log.h"
+#include "ng_ipaddr.h"
+#include "proc.h"
 
 #include "exp.h"
 #include "resolve.h"
@@ -41,46 +44,18 @@
 
 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
-static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short)));
 
 #ifdef WANT_IPV6
 extern bool Conf_ConnectIPv4;
 extern bool Conf_ConnectIPv6;
 #endif
 
-static pid_t
-Resolver_fork(int *pipefds)
-{
-	pid_t pid;
-
-	if (pipe(pipefds) != 0) {
-                Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
-                return -1;
-	}
-
-	pid = fork();
-	switch(pid) {
-		case -1:
-			Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
-			close(pipefds[0]);
-			close(pipefds[1]);
-			return -1;
-		case 0: /* child */
-			close(pipefds[0]);
-			Log_Init_Resolver( );
-			return 0;
-	}
-	/* parent */
-	close(pipefds[1]);
-	return pid; 
-}
-
 
 /**
  * Resolve IP (asynchronous!).
  */
 GLOBAL bool
-Resolve_Addr(RES_STAT * s, const ng_ipaddr_t *Addr, int identsock,
+Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
 	     void (*cbfunc) (int, short))
 {
 	int pipefd[2];
@@ -88,15 +63,13 @@ Resolve_Addr(RES_STAT * s, const ng_ipaddr_t *Addr, int identsock,
 
 	assert(s != NULL);
 
-	pid = Resolver_fork(pipefd);
+	pid = Proc_Fork(s, pipefd, cbfunc);
 	if (pid > 0) {
 		LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
-
-		s->pid = pid;
-		s->resolver_fd = pipefd[0];
-		return register_callback(s, cbfunc);
+		return true;
 	} else if( pid == 0 ) {
 		/* Sub process */
+		Log_Init_Resolver();
 		Do_ResolveAddr( Addr, identsock, pipefd[1]);
 		Log_Exit_Resolver( );
 		exit(0);
@@ -109,24 +82,23 @@ Resolve_Addr(RES_STAT * s, const ng_ipaddr_t *Addr, int identsock,
  * Resolve hostname (asynchronous!).
  */
 GLOBAL bool
-Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
+Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
 {
 	int pipefd[2];
 	pid_t pid;
 
 	assert(s != NULL);
 
-	pid = Resolver_fork(pipefd);
+	pid = Proc_Fork(s, pipefd, cbfunc);
 	if (pid > 0) {
 		/* Main process */
 #ifdef DEBUG
 		Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
 #endif
-		s->pid = pid;
-		s->resolver_fd = pipefd[0];
-		return register_callback(s, cbfunc);
+		return true;
 	} else if( pid == 0 ) {
 		/* Sub process */
+		Log_Init_Resolver();
 		Do_ResolveName(Host, pipefd[1]);
 		Log_Exit_Resolver( );
 		exit(0);
@@ -135,15 +107,6 @@ Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
 } /* Resolve_Name */
 
 
-GLOBAL void
-Resolve_Init(RES_STAT *s)
-{
-	assert(s != NULL);
-	s->resolver_fd = -1;
-	s->pid = 0;
-}
-
-
 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
 #if !defined(WANT_IPV6) && defined(h_errno)
 static char *
@@ -482,52 +445,18 @@ Do_ResolveName( const char *Host, int w_fd )
 } /* Do_ResolveName */
 
 
-static bool
-register_callback( RES_STAT *s, void (*cbfunc)(int, short))
-{
-	assert(cbfunc != NULL);
-	assert(s != NULL);
-	assert(s->resolver_fd >= 0);
-
-	if (io_setnonblock(s->resolver_fd) &&
-		io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc))
-			return true;
-
-	Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno));
-	close(s->resolver_fd);
-	Resolve_Init(s);
-	return false;
-}
-
-
-GLOBAL bool
-Resolve_Shutdown( RES_STAT *s)
-{
-	bool ret = false;
-
-	assert(s != NULL);
-	assert(s->resolver_fd >= 0);
-
-	if (s->resolver_fd >= 0)
-		ret = io_close(s->resolver_fd);
-
-	Resolve_Init(s);
-	return ret;
-}
-
-
 /**
  * Read result of resolver sub-process from pipe
  */
 GLOBAL size_t
-Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
+Resolve_Read( PROC_STAT *s, void* readbuf, size_t buflen)
 {
 	ssize_t bytes_read;
 
 	assert(buflen > 0);
 
 	/* Read result from pipe */
-	bytes_read = read(s->resolver_fd, readbuf, buflen);
+	bytes_read = read(Proc_GetPipeFd(s), readbuf, buflen);
 	if (bytes_read < 0) {
 		if (errno == EAGAIN)
 			return 0;
@@ -539,7 +468,7 @@ Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
 	else if (bytes_read == 0)
 		Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
 #endif
-	Resolve_Shutdown(s);
+	Proc_Kill(s);
 	return (size_t)bytes_read;
 }
 
diff --git a/src/ngircd/resolve.h b/src/ngircd/resolve.h
index 8ff88c8b..9759a2c4 100644
--- a/src/ngircd/resolve.h
+++ b/src/ngircd/resolve.h
@@ -1,6 +1,6 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2010 by Alexander Barton (alex@barton.de)
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -8,34 +8,18 @@
  * (at your option) any later version.
  * Please read the file COPYING, README and AUTHORS for more information.
  *
- * $Id: resolve.h,v 1.14 2008/02/26 22:04:17 fw Exp $
- *
  * Asynchronous resolver (header)
  */
 
-
 #ifndef __resolve_h__
 #define __resolve_h__
 
-#include "array.h"
-#include "tool.h"
-#include "ng_ipaddr.h"
-
-/* This struct must not be accessed directly */
-typedef struct _Res_Stat {
-	pid_t pid;			/* PID of resolver process */
-	int resolver_fd;		/* pipe fd for lookup result. */
-} RES_STAT;
-
-
-#define Resolve_Getfd(x)		((x)->resolver_fd)
-#define Resolve_INPROGRESS(x)		((x)->resolver_fd >= 0)
-
-GLOBAL bool Resolve_Addr PARAMS(( RES_STAT *s, const ng_ipaddr_t *Addr, int identsock, void (*cbfunc)(int, short)));
-GLOBAL bool Resolve_Name PARAMS(( RES_STAT *s, const char *Host, void (*cbfunc)(int, short) ));
-GLOBAL size_t Resolve_Read PARAMS(( RES_STAT *s, void *buf, size_t buflen));
-GLOBAL void Resolve_Init PARAMS(( RES_STAT *s));
-GLOBAL bool Resolve_Shutdown PARAMS(( RES_STAT *s));
+GLOBAL bool Resolve_Addr PARAMS((PROC_STAT * s, const ng_ipaddr_t * Addr,
+				 int identsock, void (*cbfunc) (int, short)));
+GLOBAL bool Resolve_Name PARAMS((PROC_STAT * s, const char *Host,
+				 void (*cbfunc) (int, short)));
+GLOBAL size_t Resolve_Read PARAMS((PROC_STAT * s, void *buf, size_t buflen));
 
 #endif
+
 /* -eof- */