masqmail

view 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 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 "masqmail.h"
20 #include <sys/wait.h>
21 #include <sys/types.h>
23 static int volatile sighup_seen = 0;
25 static void
26 sighup_handler(int sig)
27 {
28 sighup_seen = 1;
29 signal(SIGHUP, sighup_handler);
30 }
32 static void
33 sigchld_handler(int sig)
34 {
35 pid_t pid;
36 int status;
38 pid = waitpid(0, &status, 0);
39 if (pid > 0) {
40 if (WEXITSTATUS(status) != EXIT_SUCCESS)
41 logwrite(LOG_WARNING, "process %d exited with %d\n", pid, WEXITSTATUS(status));
42 if (WIFSIGNALED(status))
43 logwrite(LOG_WARNING, "process with pid %d got signal: %d\n", pid, WTERMSIG(status));
44 }
45 signal(SIGCHLD, sigchld_handler);
46 }
48 #ifdef ENABLE_SMTP_SERVER
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(EXIT_SUCCESS);
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 }
94 #endif /*ifdef ENABLE_SMTP_SERVER */
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 #ifdef ENABLE_SMTP_SERVER
111 for (node = g_list_first(iface_list); node; node = node_next) {
112 interface *iface = (interface *) (node->data);
113 int sock;
115 node_next = g_list_next(node);
116 if ((sock = make_server_socket(iface)) < 0) {
117 iface_list = g_list_remove_link(iface_list, node);
118 g_list_free_1(node);
119 continue;
120 }
121 if (listen(sock, 1) < 0) {
122 logwrite(LOG_ALERT, "listen: (terminating): %s\n", strerror(errno));
123 exit(EXIT_FAILURE);
124 }
125 logwrite(LOG_NOTICE, "listening on interface %s:%d\n", iface->address, iface->port);
126 DEBUG(5) debugf("sock = %d\n", sock);
127 FD_SET(sock, &active_fd_set);
128 }
129 #endif
131 /* setup handler for HUP signal: */
132 signal(SIGHUP, sighup_handler);
133 signal(SIGCHLD, sigchld_handler);
135 /* now that we have our socket(s),
136 we can give up root privileges */
137 if (!conf.run_as_user) {
138 if (setegid(conf.mail_gid) != 0) {
139 logwrite(LOG_ALERT, "could not change gid to %d: %s\n", conf.mail_gid, strerror(errno));
140 exit(EXIT_FAILURE);
141 }
142 if (seteuid(conf.mail_uid) != 0) {
143 logwrite(LOG_ALERT, "could not change uid to %d: %s\n", conf.mail_uid, strerror(errno));
144 exit(EXIT_FAILURE);
145 }
146 }
148 /* sel_ret = 0; */
149 time(&time_before);
150 time_before -= qival;
151 sel_ret = -1;
153 while (1) {
155 /* if we were interrupted by an incoming connection (or a signal)
156 we have to recalculate the time until the next queue run should
157 occur. select may put a value into tm, but doc for select() says
158 we should not use it. */
159 if (qival > 0) {
160 time(&time_now);
161 if (sel_ret == 0) { /* we are either just starting or did a queue run */
162 tm.tv_sec = qival;
163 tm.tv_usec = 0;
164 time_before = time_now;
165 } else {
166 tm.tv_sec = qival - (time_now - time_before);
167 tm.tv_usec = 0;
169 /* race condition, very unlikely (but possible): */
170 if (tm.tv_sec < 0)
171 tm.tv_sec = 0;
172 }
173 }
174 /* Block until input arrives on one or more active sockets,
175 or signal arrives,
176 or queuing interval time elapsed (if qival > 0) */
177 read_fd_set = active_fd_set;
178 if ((sel_ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL, qival > 0 ? &tm : NULL)) < 0) {
179 if (errno != EINTR) {
180 logwrite(LOG_ALERT, "select: (terminating): %s\n", strerror(errno));
181 exit(EXIT_FAILURE);
182 } else {
183 if (sighup_seen) {
184 logwrite(LOG_NOTICE, "HUP signal received. Restarting daemon\n");
186 for (i = 0; i < FD_SETSIZE; i++)
187 if (FD_ISSET(i, &active_fd_set))
188 close(i);
190 execv(argv[0], &(argv[0]));
191 logwrite(LOG_ALERT, "restarting failed: %s\n", strerror(errno));
192 exit(EXIT_FAILURE);
193 }
194 }
195 } else if (sel_ret > 0) {
196 #ifdef ENABLE_SMTP_SERVER
197 for (i = 0; i < FD_SETSIZE; i++) {
198 if (FD_ISSET(i, &read_fd_set)) {
199 int sock = i;
200 int new;
201 size = sizeof(clientname);
202 new = accept(sock, (struct sockaddr *) &clientname, &size);
203 if (new < 0) {
204 logwrite(LOG_ALERT, "accept: (ignoring): %s\n", strerror(errno));
205 } else
206 accept_connect(sock, new, &clientname);
207 }
208 }
209 #else
210 ;
211 #endif
212 } else {
213 /* If select returns 0, the interval time has elapsed.
214 We start a new queue runner process */
215 int pid;
216 signal(SIGCHLD, sigchld_handler);
217 if ((pid = fork()) == 0) {
218 queue_run();
220 _exit(EXIT_SUCCESS);
221 } else if (pid < 0) {
222 logwrite(LOG_ALERT, "could not fork for queue run");
223 }
224 }
225 }
226 }