masqmail

view src/listen.c @ 363:02bc0331e390

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