masqmail

changeset 384:4848c16ed1c1

Beautifying.
author markus schnalke <meillo@marmaro.de>
date Thu, 16 Feb 2012 11:22:31 +0100
parents a2909de1818b
children 01769f722a18
files src/listen.c
diffstat 1 files changed, 52 insertions(+), 36 deletions(-) [+]
line diff
     1.1 --- a/src/listen.c	Thu Feb 16 11:12:39 2012 +0100
     1.2 +++ b/src/listen.c	Thu Feb 16 11:22:31 2012 +0100
     1.3 @@ -39,10 +39,14 @@
     1.4  
     1.5  	pid = waitpid(0, &status, 0);
     1.6  	if (pid > 0) {
     1.7 -		if (WEXITSTATUS(status) != 0)
     1.8 -			logwrite(LOG_WARNING, "process %d exited with %d\n", pid, WEXITSTATUS(status));
     1.9 -		if (WIFSIGNALED(status))
    1.10 -			logwrite(LOG_WARNING, "process with pid %d got signal: %d\n", pid, WTERMSIG(status));
    1.11 +		if (WEXITSTATUS(status) != 0) {
    1.12 +			logwrite(LOG_WARNING, "process %d exited with %d\n",
    1.13 +					pid, WEXITSTATUS(status));
    1.14 +		}
    1.15 +		if (WIFSIGNALED(status)) {
    1.16 +			logwrite(LOG_WARNING, "process %d got signal: %d\n",
    1.17 +					pid, WTERMSIG(status));
    1.18 +		}
    1.19  	}
    1.20  	signal(SIGCHLD, sigchld_handler);
    1.21  }
    1.22 @@ -63,16 +67,16 @@
    1.23  	/* start child for connection: */
    1.24  	signal(SIGCHLD, sigchld_handler);
    1.25  	pid = fork();
    1.26 -	if (pid == 0) {
    1.27 +	if (pid < 0) {
    1.28 +		logwrite(LOG_WARNING, "could not fork for incoming smtp "
    1.29 +				"connection: %s\n", strerror(errno));
    1.30 +	} else if (pid == 0) {
    1.31 +		/* child */
    1.32  		close(listen_sock);
    1.33  		out = fdopen(sock, "w");
    1.34  		in = fdopen(dup_sock, "r");
    1.35 -
    1.36  		smtp_in(in, out, rem_host, ident);
    1.37 -
    1.38  		_exit(0);
    1.39 -	} else if (pid < 0) {
    1.40 -		logwrite(LOG_WARNING, "could not fork for incoming smtp connection: %s\n", strerror(errno));
    1.41  	}
    1.42  
    1.43  	close(sock);
    1.44 @@ -104,10 +108,12 @@
    1.45  			continue;
    1.46  		}
    1.47  		if (listen(sock, 1) < 0) {
    1.48 -			logwrite(LOG_ALERT, "listen: (terminating): %s\n", strerror(errno));
    1.49 +			logwrite(LOG_ALERT, "listen: (terminating): %s\n",
    1.50 +					strerror(errno));
    1.51  			exit(1);
    1.52  		}
    1.53 -		logwrite(LOG_NOTICE, "listening on interface %s:%d\n", iface->address, iface->port);
    1.54 +		logwrite(LOG_NOTICE, "listening on interface %s:%d\n",
    1.55 +				iface->address, iface->port);
    1.56  		DEBUG(5) debugf("sock = %d\n", sock);
    1.57  		FD_SET(sock, &active_fd_set);
    1.58  	}
    1.59 @@ -136,7 +142,8 @@
    1.60  		*/
    1.61  		if (qival > 0) {
    1.62  			time(&time_now);
    1.63 -			if (sel_ret == 0) {  /* we are either just starting or did a queue run */
    1.64 +			if (!sel_ret) {
    1.65 +				/* either just starting or after a queue run */
    1.66  				tm.tv_sec = qival;
    1.67  				tm.tv_usec = 0;
    1.68  				time_before = time_now;
    1.69 @@ -144,9 +151,10 @@
    1.70  				tm.tv_sec = qival - (time_now - time_before);
    1.71  				tm.tv_usec = 0;
    1.72  
    1.73 -				/* race condition, very unlikely (but possible): */
    1.74 -				if (tm.tv_sec < 0)
    1.75 +				/* race condition, unlikely (but possible): */
    1.76 +				if (tm.tv_sec < 0) {
    1.77  					tm.tv_sec = 0;
    1.78 +				}
    1.79  			}
    1.80  		}
    1.81  		/*
    1.82 @@ -155,34 +163,41 @@
    1.83  		**  (if qival > 0)
    1.84  		*/
    1.85  		read_fd_set = active_fd_set;
    1.86 -		if ((sel_ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL, qival > 0 ? &tm : NULL)) < 0) {
    1.87 +		if ((sel_ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL,
    1.88 +				qival > 0 ? &tm : NULL)) < 0) {
    1.89  			if (errno != EINTR) {
    1.90 -				logwrite(LOG_ALERT, "select: (terminating): %s\n", strerror(errno));
    1.91 +				logwrite(LOG_ALERT, "select: (terminating): "
    1.92 +						"%s\n", strerror(errno));
    1.93  				exit(1);
    1.94 -			} else {
    1.95 -				if (sighup_seen) {
    1.96 -					logwrite(LOG_NOTICE, "HUP signal received. Restarting daemon\n");
    1.97 +			} else if (sighup_seen) {
    1.98 +				logwrite(LOG_NOTICE, "HUP signal received. "
    1.99 +						"Restarting daemon\n");
   1.100  
   1.101 -					for (i = 0; i < FD_SETSIZE; i++)
   1.102 -						if (FD_ISSET(i, &active_fd_set))
   1.103 -							close(i);
   1.104 +				for (i = 0; i < FD_SETSIZE; i++)
   1.105 +					if (FD_ISSET(i, &active_fd_set))
   1.106 +						close(i);
   1.107  
   1.108 -					execv(argv[0], &(argv[0]));
   1.109 -					logwrite(LOG_ALERT, "restarting failed: %s\n", strerror(errno));
   1.110 -					exit(1);
   1.111 -				}
   1.112 +				execv(argv[0], &(argv[0]));
   1.113 +				logwrite(LOG_ALERT, "restarting failed: %s\n",
   1.114 +						strerror(errno));
   1.115 +				exit(1);
   1.116  			}
   1.117  		} else if (sel_ret > 0) {
   1.118  			for (i = 0; i < FD_SETSIZE; i++) {
   1.119 -				if (FD_ISSET(i, &read_fd_set)) {
   1.120 -					int sock = i;
   1.121 -					int new;
   1.122 -					size = sizeof(clientname);
   1.123 -					new = accept(sock, (struct sockaddr *) &clientname, &size);
   1.124 -					if (new < 0) {
   1.125 -						logwrite(LOG_ALERT, "accept: (ignoring): %s\n", strerror(errno));
   1.126 -					} else
   1.127 -						accept_connect(sock, new, &clientname);
   1.128 +				int sock = i;
   1.129 +				int new;
   1.130 +
   1.131 +				if (!FD_ISSET(i, &read_fd_set)) {
   1.132 +					continue;
   1.133 +				}
   1.134 +				size = sizeof(clientname);
   1.135 +				new = accept(sock, (struct sockaddr *)
   1.136 +						&clientname, &size);
   1.137 +				if (new < 0) {
   1.138 +					logwrite(LOG_ALERT, "accept: (ignoring): %s\n", strerror(errno));
   1.139 +				} else {
   1.140 +					accept_connect(sock, new,
   1.141 +							&clientname);
   1.142  				}
   1.143  			}
   1.144  		} else {
   1.145 @@ -197,7 +212,8 @@
   1.146  
   1.147  				_exit(0);
   1.148  			} else if (pid < 0) {
   1.149 -				logwrite(LOG_ALERT, "could not fork for queue run");
   1.150 +				logwrite(LOG_ALERT, "could not fork for "
   1.151 +						"queue run");
   1.152  			}
   1.153  		}
   1.154  	}