masqmail

changeset 401:885e3d886199

Various minor refactoring.
author markus schnalke <meillo@marmaro.de>
date Tue, 21 Feb 2012 16:11:00 +0100
parents 6500db550a03
children eedc23877cd5
files src/child.c src/connect.c
diffstat 2 files changed, 46 insertions(+), 40 deletions(-) [+]
line diff
     1.1 --- a/src/child.c	Tue Feb 21 15:44:55 2012 +0100
     1.2 +++ b/src/child.c	Tue Feb 21 16:11:00 2012 +0100
     1.3 @@ -41,26 +41,28 @@
     1.4  		pid = fork();
     1.5  		if (pid == -1) {
     1.6  			return -1;
     1.7 +
     1.8  		} else if (pid == 0) {
     1.9  			/* child */
    1.10 +			char *argv[] = { "/bin/sh", "-c", (char *)command,
    1.11 +					NULL };
    1.12  			int i, max_fd = sysconf(_SC_OPEN_MAX);
    1.13 +
    1.14  			dup2(pipe[0], 0);
    1.15  			dup2(pipe[0], 1);
    1.16  			dup2(pipe[0], 2);
    1.17  
    1.18 -			if (max_fd <= 0)
    1.19 +			if (max_fd <= 0) {
    1.20  				max_fd = 64;
    1.21 -			for (i = 3; i < max_fd; i++)
    1.22 +			}
    1.23 +			for (i = 3; i < max_fd; i++) {
    1.24  				close(i);
    1.25 -
    1.26 -			{
    1.27 -				char *argv[] = { "/bin/sh", "-c",
    1.28 -						(char *) command, NULL };
    1.29 -				execve(*argv, argv, NULL);
    1.30  			}
    1.31 +			execve(*argv, argv, NULL);
    1.32  			logwrite(LOG_ALERT, "execve failed: %s\n",
    1.33  					strerror(errno));
    1.34  			_exit(1);
    1.35 +
    1.36  		} else {
    1.37  			/* parent */
    1.38  			close(pipe[0]);
     2.1 --- a/src/connect.c	Tue Feb 21 15:44:55 2012 +0100
     2.2 +++ b/src/connect.c	Tue Feb 21 16:11:00 2012 +0100
     2.3 @@ -39,39 +39,42 @@
     2.4  {
     2.5  	GList *addr_node;
     2.6  	struct sockaddr_in saddr;
     2.7 +	int saved_errno;
     2.8  
     2.9  	DEBUG(5) debugf("connect_hostlist entered\n");
    2.10  
    2.11 -	for (addr_node = g_list_first(addr_list); addr_node; addr_node = g_list_next(addr_node)) {
    2.12 +	for (addr_node = g_list_first(addr_list); addr_node;
    2.13 +			addr_node = g_list_next(addr_node)) {
    2.14  		mxip_addr *addr = (mxip_addr *) (addr_node->data);
    2.15 -
    2.16  		*psockfd = socket(PF_INET, SOCK_STREAM, 0);
    2.17  
    2.18  		memset(&saddr, 0, sizeof(saddr));
    2.19 -
    2.20  		saddr.sin_family = AF_INET;
    2.21  		saddr.sin_port = htons(port);
    2.22 -
    2.23  		/* clumsy, but makes compiler happy: */
    2.24  		saddr.sin_addr = *(struct in_addr *) (&(addr->ip));
    2.25 -		DEBUG(5) debugf("  trying ip %s port %d\n", inet_ntoa(saddr.sin_addr), port);
    2.26 -		if (connect(*psockfd, (struct sockaddr *) (&saddr), sizeof(saddr)) == 0) {
    2.27 -			DEBUG(5) debugf("  connected to %s\n", inet_ntoa(saddr.sin_addr));
    2.28 +
    2.29 +		DEBUG(5) debugf("  trying ip %s port %d\n",
    2.30 +				inet_ntoa(saddr.sin_addr), port);
    2.31 +
    2.32 +		if (connect(*psockfd, (struct sockaddr *) &saddr,
    2.33 +				sizeof(saddr))==0) {
    2.34 +			DEBUG(5) debugf("  connected to %s\n",
    2.35 +					inet_ntoa(saddr.sin_addr));
    2.36  			return addr;
    2.37 -		} else {
    2.38 -			int saved_errno = errno;
    2.39 +		}
    2.40  
    2.41 -			close(*psockfd);
    2.42 +		saved_errno = errno;
    2.43 +		close(*psockfd);
    2.44 +		logwrite(LOG_WARNING, "connection to %s failed: %s\n",
    2.45 +				inet_ntoa(saddr.sin_addr), strerror(errno));
    2.46 +		errno = saved_errno;
    2.47  
    2.48 -			logwrite(LOG_WARNING, "connection to %s failed: %s\n", inet_ntoa(saddr.sin_addr), strerror(errno));
    2.49 -
    2.50 -			errno = saved_errno;
    2.51 -
    2.52 -			if ((saved_errno != ECONNREFUSED)
    2.53 -			    && (saved_errno != ETIMEDOUT)
    2.54 -			    && (saved_errno != ENETUNREACH)
    2.55 -			    && (saved_errno != EHOSTUNREACH))
    2.56 -				return NULL;
    2.57 +		if ((saved_errno != ECONNREFUSED) &&
    2.58 +				(saved_errno != ETIMEDOUT) &&
    2.59 +				(saved_errno != ENETUNREACH) &&
    2.60 +				(saved_errno != EHOSTUNREACH)) {
    2.61 +			return NULL;
    2.62  		}
    2.63  	}
    2.64  	return NULL;
    2.65 @@ -87,7 +90,8 @@
    2.66  **  if attempt failed for one it should not be tried again.
    2.67  */
    2.68  mxip_addr*
    2.69 -connect_resolvelist(int *psockfd, gchar *host, guint port, GList *res_func_list)
    2.70 +connect_resolvelist(int *psockfd, gchar *host, guint port,
    2.71 +		GList *res_func_list)
    2.72  {
    2.73  	GList *res_node;
    2.74  	GList *addr_list;
    2.75 @@ -95,12 +99,12 @@
    2.76  	DEBUG(5) debugf("connect_resolvelist entered\n");
    2.77  
    2.78  	h_errno = 0;
    2.79 -
    2.80  	if (isdigit(*host)) {
    2.81  		mxip_addr *addr;
    2.82  
    2.83  		if ((addr_list = resolve_ip(host))) {
    2.84 -			addr = connect_hostlist(psockfd, host, port, addr_list);
    2.85 +			addr = connect_hostlist(psockfd, host, port,
    2.86 +					addr_list);
    2.87  			g_list_free(addr_list);
    2.88  			return addr;
    2.89  		}
    2.90 @@ -110,18 +114,18 @@
    2.91  		*/
    2.92  	}
    2.93  
    2.94 -	if (res_func_list == NULL) {
    2.95 -		logwrite(LOG_ALERT, "res_funcs == NULL !!!\n");
    2.96 +	if (!res_func_list) {
    2.97 +		logwrite(LOG_ALERT, "res_funcs not set!\n");
    2.98  		exit(1);
    2.99  	}
   2.100  
   2.101  	foreach(res_func_list, res_node) {
   2.102  		resolve_func res_func;
   2.103  		DEBUG(6) debugf("  foreach() body\n");
   2.104 -		res_func = (resolve_func) (res_node->data);
   2.105  
   2.106 -		if (res_func == NULL) {
   2.107 -			logwrite(LOG_ALERT, "res_func == NULL !!!\n");
   2.108 +		res_func = (resolve_func) res_node->data;
   2.109 +		if (!res_func) {
   2.110 +			logwrite(LOG_ALERT, "Empty res_func!\n");
   2.111  			exit(1);
   2.112  		}
   2.113  
   2.114 @@ -129,16 +133,16 @@
   2.115  		if ((addr_list = res_func(NULL, host))) {
   2.116  
   2.117  			mxip_addr *addr;
   2.118 -			if ((addr = connect_hostlist(psockfd, host, port, addr_list)))
   2.119 +			if ((addr = connect_hostlist(psockfd, host, port,
   2.120 +					addr_list))) {
   2.121  				return addr;
   2.122 -
   2.123 -			DEBUG(5) {
   2.124 -				debugf("connect_hostlist failed: %s\n", strerror(errno));
   2.125  			}
   2.126 -
   2.127 +			DEBUG(5) debugf("connect_hostlist failed: %s\n",
   2.128 +					strerror(errno));
   2.129  			g_list_free(addr_list);
   2.130  		} else if (!g_list_next(res_node)) {
   2.131 -			logwrite(LOG_ALERT, "could not resolve %s: %s\n", host, hstrerror(h_errno));
   2.132 +			logwrite(LOG_ALERT, "could not resolve %s: %s\n",
   2.133 +					host, hstrerror(h_errno));
   2.134  		}
   2.135  	}
   2.136  	return NULL;