masqmail

view src/listen.c @ 372:b0708fac99dd

Fixed a problem with psb->remote_host, introduced in changeset 412385b57dc4
author markus schnalke <meillo@marmaro.de>
date Tue, 25 Oct 2011 14:31:28 +0200
parents 41958685480d
children 5781ba87df95
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 #ifdef ENABLE_IDENT
61 {
62 gchar *id = NULL;
63 if ((id = (gchar *) ident_id(sock, 60))) {
64 ident = g_strdup(id);
65 }
66 logwrite(LOG_NOTICE, "connect from host %s, port %hd ident=%s\n", rem_host,
67 ntohs(sock_addr->sin_port), ident ? ident : "(unknown)");
68 }
69 #else
70 logwrite(LOG_NOTICE, "connect from host %s, port %hd\n", rem_host, ntohs(sock_addr->sin_port));
71 #endif
73 /* start child for connection: */
74 signal(SIGCHLD, sigchld_handler);
75 pid = fork();
76 if (pid == 0) {
77 close(listen_sock);
78 out = fdopen(sock, "w");
79 in = fdopen(dup_sock, "r");
81 smtp_in(in, out, rem_host, ident);
83 _exit(0);
84 } else if (pid < 0) {
85 logwrite(LOG_WARNING, "could not fork for incoming smtp connection: %s\n", strerror(errno));
86 }
87 #ifdef ENABLE_IDENT
88 if (ident != NULL)
89 g_free(ident);
90 #endif
92 close(sock);
93 close(dup_sock);
94 }
96 void
97 listen_port(GList *iface_list, gint qival, char *argv[])
98 {
99 int i;
100 fd_set active_fd_set, read_fd_set;
101 struct timeval tm;
102 time_t time_before, time_now;
103 struct sockaddr_in clientname;
104 size_t size;
105 GList *node, *node_next;
106 int sel_ret;
108 /* Create the sockets and set them up to accept connections. */
109 FD_ZERO(&active_fd_set);
110 for (node = g_list_first(iface_list); node; node = node_next) {
111 interface *iface = (interface *) (node->data);
112 int sock;
114 node_next = g_list_next(node);
115 if ((sock = make_server_socket(iface)) < 0) {
116 iface_list = g_list_remove_link(iface_list, node);
117 g_list_free_1(node);
118 continue;
119 }
120 if (listen(sock, 1) < 0) {
121 logwrite(LOG_ALERT, "listen: (terminating): %s\n", strerror(errno));
122 exit(1);
123 }
124 logwrite(LOG_NOTICE, "listening on interface %s:%d\n", iface->address, iface->port);
125 DEBUG(5) debugf("sock = %d\n", sock);
126 FD_SET(sock, &active_fd_set);
127 }
129 /* setup handler for HUP signal: */
130 signal(SIGHUP, sighup_handler);
131 signal(SIGCHLD, sigchld_handler);
133 /* now that we have our socket(s), we can give up root privileges */
134 if (!conf.run_as_user) {
135 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
136 }
138 /* sel_ret = 0; */
139 time(&time_before);
140 time_before -= qival;
141 sel_ret = -1;
143 while (1) {
145 /*
146 ** if we were interrupted by an incoming connection (or a
147 ** signal) we have to recalculate the time until the next
148 ** queue run should occur. select may put a value into tm,
149 ** but doc for select() says we should not use it.
150 */
151 if (qival > 0) {
152 time(&time_now);
153 if (sel_ret == 0) { /* we are either just starting or did a queue run */
154 tm.tv_sec = qival;
155 tm.tv_usec = 0;
156 time_before = time_now;
157 } else {
158 tm.tv_sec = qival - (time_now - time_before);
159 tm.tv_usec = 0;
161 /* race condition, very unlikely (but possible): */
162 if (tm.tv_sec < 0)
163 tm.tv_sec = 0;
164 }
165 }
166 /*
167 ** Block until input arrives on one or more active sockets,
168 ** or signal arrives, or queuing interval time elapsed
169 ** (if qival > 0)
170 */
171 read_fd_set = active_fd_set;
172 if ((sel_ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL, qival > 0 ? &tm : NULL)) < 0) {
173 if (errno != EINTR) {
174 logwrite(LOG_ALERT, "select: (terminating): %s\n", strerror(errno));
175 exit(1);
176 } else {
177 if (sighup_seen) {
178 logwrite(LOG_NOTICE, "HUP signal received. Restarting daemon\n");
180 for (i = 0; i < FD_SETSIZE; i++)
181 if (FD_ISSET(i, &active_fd_set))
182 close(i);
184 execv(argv[0], &(argv[0]));
185 logwrite(LOG_ALERT, "restarting failed: %s\n", strerror(errno));
186 exit(1);
187 }
188 }
189 } else if (sel_ret > 0) {
190 for (i = 0; i < FD_SETSIZE; i++) {
191 if (FD_ISSET(i, &read_fd_set)) {
192 int sock = i;
193 int new;
194 size = sizeof(clientname);
195 new = accept(sock, (struct sockaddr *) &clientname, &size);
196 if (new < 0) {
197 logwrite(LOG_ALERT, "accept: (ignoring): %s\n", strerror(errno));
198 } else
199 accept_connect(sock, new, &clientname);
200 }
201 }
202 } else {
203 /*
204 ** If select returns 0, the interval time has elapsed.
205 ** We start a new queue runner process
206 */
207 int pid;
208 signal(SIGCHLD, sigchld_handler);
209 if ((pid = fork()) == 0) {
210 queue_run();
212 _exit(0);
213 } else if (pid < 0) {
214 logwrite(LOG_ALERT, "could not fork for queue run");
215 }
216 }
217 }
218 }