masqmail

view src/listen.c @ 383:a2909de1818b

Port number are unsigned. This corrects the logfile output.
author markus schnalke <meillo@marmaro.de>
date Thu, 16 Feb 2012 11:12:39 +0100
parents 5781ba87df95
children 4848c16ed1c1
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999/2000 Oliver Kurth
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include <sys/wait.h>
21 #include <sys/types.h>
23 #include "masqmail.h"
25 static int volatile sighup_seen = 0;
27 static void
28 sighup_handler(int sig)
29 {
30 sighup_seen = 1;
31 signal(SIGHUP, sighup_handler);
32 }
34 static void
35 sigchld_handler(int sig)
36 {
37 pid_t pid;
38 int status;
40 pid = waitpid(0, &status, 0);
41 if (pid > 0) {
42 if (WEXITSTATUS(status) != 0)
43 logwrite(LOG_WARNING, "process %d exited with %d\n", pid, WEXITSTATUS(status));
44 if (WIFSIGNALED(status))
45 logwrite(LOG_WARNING, "process with pid %d got signal: %d\n", pid, WTERMSIG(status));
46 }
47 signal(SIGCHLD, sigchld_handler);
48 }
50 void
51 accept_connect(int listen_sock, int sock, struct sockaddr_in *sock_addr)
52 {
53 pid_t pid;
54 int dup_sock = dup(sock);
55 FILE *out, *in;
56 gchar *rem_host;
57 gchar *ident = NULL;
59 rem_host = g_strdup(inet_ntoa(sock_addr->sin_addr));
60 logwrite(LOG_NOTICE, "connect from host %s, port %hu\n",
61 rem_host, ntohs(sock_addr->sin_port));
63 /* start child for connection: */
64 signal(SIGCHLD, sigchld_handler);
65 pid = fork();
66 if (pid == 0) {
67 close(listen_sock);
68 out = fdopen(sock, "w");
69 in = fdopen(dup_sock, "r");
71 smtp_in(in, out, rem_host, ident);
73 _exit(0);
74 } else if (pid < 0) {
75 logwrite(LOG_WARNING, "could not fork for incoming smtp connection: %s\n", strerror(errno));
76 }
78 close(sock);
79 close(dup_sock);
80 }
82 void
83 listen_port(GList *iface_list, gint qival, char *argv[])
84 {
85 int i;
86 fd_set active_fd_set, read_fd_set;
87 struct timeval tm;
88 time_t time_before, time_now;
89 struct sockaddr_in clientname;
90 size_t size;
91 GList *node, *node_next;
92 int sel_ret;
94 /* Create the sockets and set them up to accept connections. */
95 FD_ZERO(&active_fd_set);
96 for (node = g_list_first(iface_list); node; node = node_next) {
97 interface *iface = (interface *) (node->data);
98 int sock;
100 node_next = g_list_next(node);
101 if ((sock = make_server_socket(iface)) < 0) {
102 iface_list = g_list_remove_link(iface_list, node);
103 g_list_free_1(node);
104 continue;
105 }
106 if (listen(sock, 1) < 0) {
107 logwrite(LOG_ALERT, "listen: (terminating): %s\n", strerror(errno));
108 exit(1);
109 }
110 logwrite(LOG_NOTICE, "listening on interface %s:%d\n", iface->address, iface->port);
111 DEBUG(5) debugf("sock = %d\n", sock);
112 FD_SET(sock, &active_fd_set);
113 }
115 /* setup handler for HUP signal: */
116 signal(SIGHUP, sighup_handler);
117 signal(SIGCHLD, sigchld_handler);
119 /* now that we have our socket(s), we can give up root privileges */
120 if (!conf.run_as_user) {
121 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
122 }
124 /* sel_ret = 0; */
125 time(&time_before);
126 time_before -= qival;
127 sel_ret = -1;
129 while (1) {
131 /*
132 ** if we were interrupted by an incoming connection (or a
133 ** signal) we have to recalculate the time until the next
134 ** queue run should occur. select may put a value into tm,
135 ** but doc for select() says we should not use it.
136 */
137 if (qival > 0) {
138 time(&time_now);
139 if (sel_ret == 0) { /* we are either just starting or did a queue run */
140 tm.tv_sec = qival;
141 tm.tv_usec = 0;
142 time_before = time_now;
143 } else {
144 tm.tv_sec = qival - (time_now - time_before);
145 tm.tv_usec = 0;
147 /* race condition, very unlikely (but possible): */
148 if (tm.tv_sec < 0)
149 tm.tv_sec = 0;
150 }
151 }
152 /*
153 ** Block until input arrives on one or more active sockets,
154 ** or signal arrives, or queuing interval time elapsed
155 ** (if qival > 0)
156 */
157 read_fd_set = active_fd_set;
158 if ((sel_ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL, qival > 0 ? &tm : NULL)) < 0) {
159 if (errno != EINTR) {
160 logwrite(LOG_ALERT, "select: (terminating): %s\n", strerror(errno));
161 exit(1);
162 } else {
163 if (sighup_seen) {
164 logwrite(LOG_NOTICE, "HUP signal received. Restarting daemon\n");
166 for (i = 0; i < FD_SETSIZE; i++)
167 if (FD_ISSET(i, &active_fd_set))
168 close(i);
170 execv(argv[0], &(argv[0]));
171 logwrite(LOG_ALERT, "restarting failed: %s\n", strerror(errno));
172 exit(1);
173 }
174 }
175 } else if (sel_ret > 0) {
176 for (i = 0; i < FD_SETSIZE; i++) {
177 if (FD_ISSET(i, &read_fd_set)) {
178 int sock = i;
179 int new;
180 size = sizeof(clientname);
181 new = accept(sock, (struct sockaddr *) &clientname, &size);
182 if (new < 0) {
183 logwrite(LOG_ALERT, "accept: (ignoring): %s\n", strerror(errno));
184 } else
185 accept_connect(sock, new, &clientname);
186 }
187 }
188 } else {
189 /*
190 ** If select returns 0, the interval time has elapsed.
191 ** We start a new queue runner process
192 */
193 int pid;
194 signal(SIGCHLD, sigchld_handler);
195 if ((pid = fork()) == 0) {
196 queue_run();
198 _exit(0);
199 } else if (pid < 0) {
200 logwrite(LOG_ALERT, "could not fork for queue run");
201 }
202 }
203 }
204 }