Newer
Older
if(((p=semfile_list_check(&initialized,shutdown_semfiles))!=NULL
&& lprintf(LOG_INFO,"Shutdown semaphore file (%s) detected",p))
|| (startup->shutdown_now==TRUE
startup->shutdown_now=FALSE;
terminate_server=TRUE;
break;
}
/* now wait for connection */
client_addr_len = sizeof(client_addr);
client_socket = xpms_accept(mail_set,&client_addr, &client_addr_len, startup->sem_chk_freq*1000, &cbdata);
if(client_socket != INVALID_SOCKET) {
if(startup->socket_open!=NULL)
startup->socket_open(startup->cbdata,TRUE);
stats.sockets++;
inet_addrtop(&client_addr, host_ip, sizeof(host_ip));
if(trashcan(&scfg,host_ip,"ip-silent")) {
mail_close_socket(&client_socket, &session);
stats.connections_ignored++;
continue;
}
if(protected_uint32_value(active_clients)>=startup->max_clients) {
lprintf(LOG_WARNING,"%04d %s !MAXIMUM CLIENTS (%u) reached, access denied (%lu total)"
,client_socket, (char *)cbdata, startup->max_clients, ++stats.connections_refused);
sockprintf(client_socket, cbdata, session,"-ERR Maximum active clients reached, please try again later.");
mswait(3000);
mail_close_socket(&client_socket, &session);
continue;
}
l=1;
if((i=ioctlsocket(client_socket, FIONBIO, &l))!=0) {
lprintf(LOG_CRIT,"%04d %s !ERROR %d (%d) disabling blocking on socket"
,client_socket, (char *)cbdata, i, ERROR_VALUE);
sockprintf(client_socket, cbdata, session,"-ERR System error, please try again later.");
mswait(3000);
mail_close_socket(&client_socket, &session);
continue;
}
if(strcmp((char *)cbdata, "pop3") && strcmp((char *)cbdata, "pop3s")) { /* Not POP3 */
lprintf(LOG_CRIT,"%04d SMTP !ERROR allocating %lu bytes of memory for smtp_t"
mail_close_socket(&client_socket, &session);
continue;
}
smtp->socket=client_socket;
memcpy(&smtp->client_addr, &client_addr, client_addr_len);
smtp->client_addr_len=client_addr_len;
smtp->tls_port = (strcmp((char *)cbdata, "submissions") == 0);
protected_uint32_adjust(&thread_count,1);
_beginthread(smtp_thread, 0, smtp);
stats.connections_served++;
lprintf(LOG_CRIT,"%04d !POP3 ERROR allocating %lu bytes of memory for pop3_t"
sockprintf(client_socket, "POP3", session,"-ERR System error, please try again later.");
mail_close_socket(&client_socket, &session);
pop3->socket=client_socket;
memcpy(&pop3->client_addr, &client_addr, client_addr_len);
pop3->client_addr_len=client_addr_len;
pop3->tls_port = (strcmp((char *)cbdata, "pop3s") == 0);
protected_uint32_adjust(&thread_count,1);
_beginthread(pop3_thread, 0, pop3);
stats.connections_served++;
}
if(protected_uint32_value(active_clients)) {
lprintf(LOG_INFO,"Waiting for %d active clients to disconnect..."
start=time(NULL);
while(protected_uint32_value(active_clients)) {
if(startup->max_inactivity && time(NULL)-start>startup->max_inactivity) {
lprintf(LOG_WARNING,"!TIMEOUT (%u seconds) waiting for %d active clients"
, startup->max_inactivity, protected_uint32_value(active_clients));
break;
}
mswait(100);
lprintf(LOG_INFO, "Done waiting for active clients to disconnect");
if(sendmail_running) {
terminate_sendmail=TRUE;
sem_post(&sendmail_wakeup_sem);
mswait(100);
}
if(sendmail_running) {
lprintf(LOG_INFO, "Waiting for SendMail thread to terminate...");
start=time(NULL);
while(sendmail_running) {
if(time(NULL)-start>TIMEOUT_THREAD_WAIT) {
lprintf(LOG_WARNING, "!TIMEOUT waiting for SendMail thread to terminate");
break;
}
mswait(500);
lprintf(LOG_INFO, "Done waiting for SendMail thread to terminate");
if(!sendmail_running) {
while(sem_destroy(&sendmail_wakeup_sem)==-1 && errno==EBUSY) {
mswait(1);
cleanup(0);
if(!terminate_server) {
lprintf(LOG_INFO,"Recycling server...");
if(startup->recycle!=NULL)
startup->recycle(startup->cbdata);
} while(!terminate_server);
protected_uint32_destroy(thread_count);