masqmail-0.2

diff src/listen.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
line diff
     1.1 --- a/src/listen.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/listen.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -22,222 +22,205 @@
     1.4  
     1.5  static int volatile sighup_seen = 0;
     1.6  
     1.7 -static
     1.8 -void sighup_handler(int sig)
     1.9 +static void
    1.10 +sighup_handler(int sig)
    1.11  {
    1.12 -  sighup_seen = 1;
    1.13 -  signal(SIGHUP, sighup_handler);
    1.14 +	sighup_seen = 1;
    1.15 +	signal(SIGHUP, sighup_handler);
    1.16  }
    1.17  
    1.18 -static
    1.19 -void sigchld_handler(int sig)
    1.20 +static void
    1.21 +sigchld_handler(int sig)
    1.22  {
    1.23 -  pid_t pid;
    1.24 -  int status;
    1.25 -  
    1.26 -  pid = waitpid(0, &status, 0);
    1.27 -  if(pid > 0){
    1.28 -    if(WEXITSTATUS(status) != EXIT_SUCCESS)
    1.29 -      logwrite(LOG_WARNING, "process %d exited with %d\n",
    1.30 -	       pid, WEXITSTATUS(status));
    1.31 -    if(WIFSIGNALED(status))
    1.32 -      logwrite(LOG_WARNING,
    1.33 -	       "process with pid %d got signal: %d\n",
    1.34 -	       pid, WTERMSIG(status));
    1.35 -  }
    1.36 -  signal(SIGCHLD, sigchld_handler);
    1.37 +	pid_t pid;
    1.38 +	int status;
    1.39 +
    1.40 +	pid = waitpid(0, &status, 0);
    1.41 +	if (pid > 0) {
    1.42 +		if (WEXITSTATUS(status) != EXIT_SUCCESS)
    1.43 +			logwrite(LOG_WARNING, "process %d exited with %d\n", pid, WEXITSTATUS(status));
    1.44 +		if (WIFSIGNALED(status))
    1.45 +			logwrite(LOG_WARNING, "process with pid %d got signal: %d\n", pid, WTERMSIG(status));
    1.46 +	}
    1.47 +	signal(SIGCHLD, sigchld_handler);
    1.48  }
    1.49  
    1.50  #ifdef ENABLE_SMTP_SERVER
    1.51 -void accept_connect(int listen_sock, int sock, struct sockaddr_in* sock_addr)
    1.52 +void
    1.53 +accept_connect(int listen_sock, int sock, struct sockaddr_in *sock_addr)
    1.54  {
    1.55 -  pid_t pid;
    1.56 -  int dup_sock = dup(sock);
    1.57 -  FILE *out, *in;
    1.58 -  gchar *rem_host;
    1.59 -  gchar *ident = NULL;
    1.60 +	pid_t pid;
    1.61 +	int dup_sock = dup(sock);
    1.62 +	FILE *out, *in;
    1.63 +	gchar *rem_host;
    1.64 +	gchar *ident = NULL;
    1.65  
    1.66 -  rem_host = g_strdup(inet_ntoa(sock_addr->sin_addr));
    1.67 +	rem_host = g_strdup(inet_ntoa(sock_addr->sin_addr));
    1.68  #ifdef ENABLE_IDENT
    1.69 -  {
    1.70 -    gchar *id = NULL;
    1.71 -    if((id = (gchar *)ident_id(sock, 60))){
    1.72 -      ident = g_strdup(id);
    1.73 -    }
    1.74 -    logwrite(LOG_NOTICE, "connect from host %s, port %hd ident=%s\n",
    1.75 -	     rem_host,
    1.76 -	     ntohs (sock_addr->sin_port),
    1.77 -	     ident ? ident : "(unknown)");
    1.78 -  }
    1.79 +	{
    1.80 +		gchar *id = NULL;
    1.81 +		if ((id = (gchar *) ident_id(sock, 60))) {
    1.82 +			ident = g_strdup(id);
    1.83 +		}
    1.84 +		logwrite(LOG_NOTICE, "connect from host %s, port %hd ident=%s\n", rem_host,
    1.85 +		         ntohs(sock_addr->sin_port), ident ? ident : "(unknown)");
    1.86 +	}
    1.87  #else
    1.88 -  logwrite(LOG_NOTICE, "connect from host %s, port %hd\n",
    1.89 -	 rem_host,
    1.90 -	 ntohs (sock_addr->sin_port));
    1.91 +	logwrite(LOG_NOTICE, "connect from host %s, port %hd\n", rem_host, ntohs(sock_addr->sin_port));
    1.92  #endif
    1.93  
    1.94 -  // start child for connection:
    1.95 -  signal(SIGCHLD, sigchld_handler);
    1.96 -  pid = fork();
    1.97 -  if(pid == 0){
    1.98 -    close(listen_sock);
    1.99 -    out = fdopen(sock, "w");
   1.100 -    in = fdopen(dup_sock, "r");
   1.101 -    
   1.102 -    smtp_in(in, out, rem_host, ident);
   1.103 +	// start child for connection:
   1.104 +	signal(SIGCHLD, sigchld_handler);
   1.105 +	pid = fork();
   1.106 +	if (pid == 0) {
   1.107 +		close(listen_sock);
   1.108 +		out = fdopen(sock, "w");
   1.109 +		in = fdopen(dup_sock, "r");
   1.110  
   1.111 -    _exit(EXIT_SUCCESS);
   1.112 -  }else if(pid < 0){
   1.113 -    logwrite(LOG_WARNING, "could not fork for incoming smtp connection: %s\n",
   1.114 -	     strerror(errno));
   1.115 -  }
   1.116 +		smtp_in(in, out, rem_host, ident);
   1.117  
   1.118 +		_exit(EXIT_SUCCESS);
   1.119 +	} else if (pid < 0) {
   1.120 +		logwrite(LOG_WARNING, "could not fork for incoming smtp connection: %s\n", strerror(errno));
   1.121 +	}
   1.122  #ifdef ENABLE_IDENT
   1.123 -  if(ident != NULL) g_free(ident);
   1.124 +	if (ident != NULL)
   1.125 +		g_free(ident);
   1.126  #endif
   1.127  
   1.128 -  close(sock);
   1.129 -  close(dup_sock);
   1.130 +	close(sock);
   1.131 +	close(dup_sock);
   1.132  }
   1.133 -#endif /*ifdef ENABLE_SMTP_SERVER*/
   1.134 +#endif  /*ifdef ENABLE_SMTP_SERVER */
   1.135  
   1.136 -void listen_port(GList *iface_list, gint qival, char *argv[])
   1.137 +void
   1.138 +listen_port(GList * iface_list, gint qival, char *argv[])
   1.139  {
   1.140 -  int i;
   1.141 -  fd_set active_fd_set, read_fd_set;
   1.142 -  struct timeval tm;
   1.143 -  time_t time_before, time_now;
   1.144 -  struct sockaddr_in clientname;
   1.145 -  size_t size;
   1.146 -  GList *node, *node_next;
   1.147 -  int sel_ret;
   1.148 +	int i;
   1.149 +	fd_set active_fd_set, read_fd_set;
   1.150 +	struct timeval tm;
   1.151 +	time_t time_before, time_now;
   1.152 +	struct sockaddr_in clientname;
   1.153 +	size_t size;
   1.154 +	GList *node, *node_next;
   1.155 +	int sel_ret;
   1.156  
   1.157 -  /* Create the sockets and set them up to accept connections. */
   1.158 -  FD_ZERO (&active_fd_set);
   1.159 +	/* Create the sockets and set them up to accept connections. */
   1.160 +	FD_ZERO(&active_fd_set);
   1.161  #ifdef ENABLE_SMTP_SERVER
   1.162 -  for(node = g_list_first(iface_list);
   1.163 -      node;
   1.164 -      node = node_next){
   1.165 -    interface *iface = (interface *)(node->data);
   1.166 -    int sock;
   1.167 +	for (node = g_list_first(iface_list); node; node = node_next) {
   1.168 +		interface *iface = (interface *) (node->data);
   1.169 +		int sock;
   1.170  
   1.171 -    node_next=g_list_next(node);
   1.172 -    if ((sock = make_server_socket (iface))<0){
   1.173 -      iface_list= g_list_remove_link(iface_list, node);
   1.174 -      g_list_free_1(node);
   1.175 -      continue;
   1.176 -    }
   1.177 -    if (listen (sock, 1) < 0){
   1.178 -      logwrite(LOG_ALERT, "listen: (terminating): %s\n", strerror(errno));
   1.179 -      exit (EXIT_FAILURE);
   1.180 -    }
   1.181 -    logwrite(LOG_NOTICE, "listening on interface %s:%d\n",
   1.182 -	     iface->address, iface->port);
   1.183 -    DEBUG(5) debugf("sock = %d\n", sock);
   1.184 -    FD_SET (sock, &active_fd_set);
   1.185 -  }
   1.186 +		node_next = g_list_next(node);
   1.187 +		if ((sock = make_server_socket(iface)) < 0) {
   1.188 +			iface_list = g_list_remove_link(iface_list, node);
   1.189 +			g_list_free_1(node);
   1.190 +			continue;
   1.191 +		}
   1.192 +		if (listen(sock, 1) < 0) {
   1.193 +			logwrite(LOG_ALERT, "listen: (terminating): %s\n", strerror(errno));
   1.194 +			exit(EXIT_FAILURE);
   1.195 +		}
   1.196 +		logwrite(LOG_NOTICE, "listening on interface %s:%d\n", iface->address, iface->port);
   1.197 +		DEBUG(5) debugf("sock = %d\n", sock);
   1.198 +		FD_SET(sock, &active_fd_set);
   1.199 +	}
   1.200  #endif
   1.201  
   1.202 -  /* setup handler for HUP signal: */
   1.203 -  signal(SIGHUP, sighup_handler);
   1.204 -  signal(SIGCHLD, sigchld_handler);
   1.205 +	/* setup handler for HUP signal: */
   1.206 +	signal(SIGHUP, sighup_handler);
   1.207 +	signal(SIGCHLD, sigchld_handler);
   1.208  
   1.209 -  /* now that we have our socket(s),
   1.210 -     we can give up root privileges */
   1.211 -  if(!conf.run_as_user){
   1.212 -    if(setegid(conf.mail_gid) != 0){
   1.213 -      logwrite(LOG_ALERT, "could not change gid to %d: %s\n",
   1.214 -	       conf.mail_gid, strerror(errno));
   1.215 -      exit(EXIT_FAILURE);
   1.216 -    }
   1.217 -    if(seteuid(conf.mail_uid) != 0){
   1.218 -      logwrite(LOG_ALERT, "could not change uid to %d: %s\n",
   1.219 -	       conf.mail_uid, strerror(errno));
   1.220 -      exit(EXIT_FAILURE);
   1.221 -    }
   1.222 -  }
   1.223 +	/* now that we have our socket(s),
   1.224 +	   we can give up root privileges */
   1.225 +	if (!conf.run_as_user) {
   1.226 +		if (setegid(conf.mail_gid) != 0) {
   1.227 +			logwrite(LOG_ALERT, "could not change gid to %d: %s\n", conf.mail_gid, strerror(errno));
   1.228 +			exit(EXIT_FAILURE);
   1.229 +		}
   1.230 +		if (seteuid(conf.mail_uid) != 0) {
   1.231 +			logwrite(LOG_ALERT, "could not change uid to %d: %s\n", conf.mail_uid, strerror(errno));
   1.232 +			exit(EXIT_FAILURE);
   1.233 +		}
   1.234 +	}
   1.235  
   1.236 -  /*  sel_ret = 0;*/
   1.237 -  time(&time_before);
   1.238 -  time_before -= qival;
   1.239 -  sel_ret = -1;
   1.240 +	/*  sel_ret = 0; */
   1.241 +	time(&time_before);
   1.242 +	time_before -= qival;
   1.243 +	sel_ret = -1;
   1.244  
   1.245 -  while (1){
   1.246 +	while (1) {
   1.247  
   1.248 -    /* if we were interrupted by an incoming connection (or a signal)
   1.249 -       we have to recalculate the time until the next queue run should
   1.250 -       occur. select may put a value into tm, but doc for select() says
   1.251 -       we should not use it.*/
   1.252 -    if(qival > 0){
   1.253 -      time(&time_now);
   1.254 -      if(sel_ret == 0){ /* we are either just starting or did a queue run */
   1.255 -	tm.tv_sec = qival;
   1.256 -	tm.tv_usec = 0;
   1.257 -	time_before = time_now;
   1.258 -      }else{
   1.259 -	tm.tv_sec = qival - (time_now - time_before);
   1.260 -	tm.tv_usec = 0;
   1.261 +		/* if we were interrupted by an incoming connection (or a signal)
   1.262 +		   we have to recalculate the time until the next queue run should
   1.263 +		   occur. select may put a value into tm, but doc for select() says
   1.264 +		   we should not use it. */
   1.265 +		if (qival > 0) {
   1.266 +			time(&time_now);
   1.267 +			if (sel_ret == 0) {  /* we are either just starting or did a queue run */
   1.268 +				tm.tv_sec = qival;
   1.269 +				tm.tv_usec = 0;
   1.270 +				time_before = time_now;
   1.271 +			} else {
   1.272 +				tm.tv_sec = qival - (time_now - time_before);
   1.273 +				tm.tv_usec = 0;
   1.274  
   1.275 -	/* race condition, very unlikely (but possible): */
   1.276 -	if(tm.tv_sec < 0)
   1.277 -	  tm.tv_sec = 0;
   1.278 -      }
   1.279 -    }
   1.280 -    /* Block until input arrives on one or more active sockets,
   1.281 -       or signal arrives,
   1.282 -       or queuing interval time elapsed (if qival > 0) */
   1.283 -    read_fd_set = active_fd_set;
   1.284 -    if ((sel_ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL,
   1.285 -			  qival > 0 ? &tm : NULL)) < 0){
   1.286 -      if(errno != EINTR){
   1.287 -	logwrite(LOG_ALERT, "select: (terminating): %s\n", strerror(errno));
   1.288 -	exit (EXIT_FAILURE);
   1.289 -      }else{
   1.290 -	if(sighup_seen){
   1.291 -	  logwrite(LOG_NOTICE, "HUP signal received. Restarting daemon\n");
   1.292 +				/* race condition, very unlikely (but possible): */
   1.293 +				if (tm.tv_sec < 0)
   1.294 +					tm.tv_sec = 0;
   1.295 +			}
   1.296 +		}
   1.297 +		/* Block until input arrives on one or more active sockets,
   1.298 +		   or signal arrives,
   1.299 +		   or queuing interval time elapsed (if qival > 0) */
   1.300 +		read_fd_set = active_fd_set;
   1.301 +		if ((sel_ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL, qival > 0 ? &tm : NULL)) < 0) {
   1.302 +			if (errno != EINTR) {
   1.303 +				logwrite(LOG_ALERT, "select: (terminating): %s\n", strerror(errno));
   1.304 +				exit(EXIT_FAILURE);
   1.305 +			} else {
   1.306 +				if (sighup_seen) {
   1.307 +					logwrite(LOG_NOTICE, "HUP signal received. Restarting daemon\n");
   1.308  
   1.309 -	  for(i = 0; i < FD_SETSIZE; i++)
   1.310 -	    if(FD_ISSET(i, &active_fd_set))
   1.311 -	      close(i);
   1.312 +					for (i = 0; i < FD_SETSIZE; i++)
   1.313 +						if (FD_ISSET(i, &active_fd_set))
   1.314 +							close(i);
   1.315  
   1.316 -	  execv(argv[0], &(argv[0]));
   1.317 -	  logwrite(LOG_ALERT, "restarting failed: %s\n", strerror(errno));
   1.318 -	  exit(EXIT_FAILURE);
   1.319 +					execv(argv[0], &(argv[0]));
   1.320 +					logwrite(LOG_ALERT, "restarting failed: %s\n", strerror(errno));
   1.321 +					exit(EXIT_FAILURE);
   1.322 +				}
   1.323 +			}
   1.324 +		} else if (sel_ret > 0) {
   1.325 +#ifdef ENABLE_SMTP_SERVER
   1.326 +			for (i = 0; i < FD_SETSIZE; i++) {
   1.327 +				if (FD_ISSET(i, &read_fd_set)) {
   1.328 +					int sock = i;
   1.329 +					int new;
   1.330 +					size = sizeof(clientname);
   1.331 +					new = accept(sock, (struct sockaddr *) &clientname, &size);
   1.332 +					if (new < 0) {
   1.333 +						logwrite(LOG_ALERT, "accept: (ignoring): %s\n", strerror(errno));
   1.334 +					} else
   1.335 +						accept_connect(sock, new, &clientname);
   1.336 +				}
   1.337 +			}
   1.338 +#else
   1.339 +			;
   1.340 +#endif
   1.341 +		} else {
   1.342 +			/* If select returns 0, the interval time has elapsed.
   1.343 +			   We start a new queue runner process */
   1.344 +			int pid;
   1.345 +			signal(SIGCHLD, sigchld_handler);
   1.346 +			if ((pid = fork()) == 0) {
   1.347 +				queue_run();
   1.348 +
   1.349 +				_exit(EXIT_SUCCESS);
   1.350 +			} else if (pid < 0) {
   1.351 +				logwrite(LOG_ALERT, "could not fork for queue run");
   1.352 +			}
   1.353 +		}
   1.354  	}
   1.355 -      }
   1.356 -    }
   1.357 -    else if(sel_ret > 0){
   1.358 -#ifdef ENABLE_SMTP_SERVER
   1.359 -      for(i = 0; i < FD_SETSIZE; i++){
   1.360 -	if (FD_ISSET (i, &read_fd_set)){
   1.361 -	  int sock = i;
   1.362 -	  int new;
   1.363 -	  size = sizeof (clientname);
   1.364 -	  new = accept (sock,
   1.365 -			(struct sockaddr *) &clientname,
   1.366 -			&size);
   1.367 -	  if (new < 0){
   1.368 -	    logwrite(LOG_ALERT, "accept: (ignoring): %s\n",
   1.369 -		     strerror(errno));
   1.370 -	  }else
   1.371 -	    accept_connect(sock, new, &clientname);
   1.372 -	}
   1.373 -      }
   1.374 -#else
   1.375 -      ;
   1.376 -#endif
   1.377 -    }else{
   1.378 -      /* If select returns 0, the interval time has elapsed.
   1.379 -	 We start a new queue runner process */
   1.380 -      int pid;
   1.381 -      signal(SIGCHLD, sigchld_handler);
   1.382 -      if((pid = fork()) == 0){
   1.383 -	queue_run();
   1.384 -
   1.385 -	_exit(EXIT_SUCCESS);
   1.386 -      }
   1.387 -      else if(pid < 0){
   1.388 -	logwrite(LOG_ALERT, "could not fork for queue run");
   1.389 -      }
   1.390 -    }
   1.391 -  }
   1.392  }