Skip to content
Snippets Groups Projects
mailsrvr.c 196 KiB
Newer Older
				if(((p=semfile_list_check(&initialized,shutdown_semfiles))!=NULL
deuce's avatar
deuce committed
						&& lprintf(LOG_INFO,"Shutdown semaphore file (%s) detected",p))
					|| (startup->shutdown_now==TRUE
deuce's avatar
deuce committed
						&& lprintf(LOG_INFO,"Shutdown semaphore signaled"))) {
					startup->shutdown_now=FALSE;
					terminate_server=TRUE;
deuce's avatar
deuce committed
			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);
deuce's avatar
deuce committed
				inet_addrtop(&client_addr, host_ip, sizeof(host_ip));
				if(trashcan(&scfg,host_ip,"ip-silent")) {
					mail_close_socket(&client_socket, &session);
				if(protected_uint32_value(active_clients)>=startup->max_clients) {
					lprintf(LOG_WARNING,"%04d %s !MAXIMUM CLIENTS (%u) reached, access denied (%lu total)"
deuce's avatar
deuce committed
						,client_socket, (char *)cbdata, startup->max_clients, ++stats.connections_refused);
					sockprintf(client_socket, cbdata, session,"-ERR Maximum active clients reached, please try again later.");
					mail_close_socket(&client_socket, &session);
				l=1;

				if((i=ioctlsocket(client_socket, FIONBIO, &l))!=0) {
deuce's avatar
deuce committed
					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.");
					mail_close_socket(&client_socket, &session);
				if(strcmp((char *)cbdata, "pop3") && strcmp((char *)cbdata, "pop3s")) { /* Not POP3 */
deuce's avatar
deuce committed
					if((smtp=malloc(sizeof(smtp_t)))==NULL) {
						lprintf(LOG_CRIT,"%04d SMTP !ERROR allocating %lu bytes of memory for smtp_t"
deuce's avatar
deuce committed
							,client_socket, sizeof(smtp_t));
						mail_close_socket(&client_socket, &session);
deuce's avatar
deuce committed
						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);
deuce's avatar
deuce committed
					protected_uint32_adjust(&thread_count,1);
					_beginthread(smtp_thread, 0, smtp);
					stats.connections_served++;
deuce's avatar
deuce committed
				else {
					if((pop3=malloc(sizeof(pop3_t)))==NULL) {
						lprintf(LOG_CRIT,"%04d !POP3 ERROR allocating %lu bytes of memory for pop3_t"
deuce's avatar
deuce committed
							,client_socket,sizeof(pop3_t));
						sockprintf(client_socket, "POP3", session,"-ERR System error, please try again later.");
deuce's avatar
deuce committed
						mswait(3000);
						mail_close_socket(&client_socket, &session);
deuce's avatar
deuce committed
						continue;
					}
deuce's avatar
deuce committed
					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);
deuce's avatar
deuce committed
					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..."
deuce's avatar
deuce committed
				, protected_uint32_value(active_clients));
			while(protected_uint32_value(active_clients)) {
				if(startup->max_inactivity && time(NULL)-start>startup->max_inactivity) {
deuce's avatar
deuce committed
					lprintf(LOG_WARNING,"!TIMEOUT (%u seconds) waiting for %d active clients"
						, startup->max_inactivity, protected_uint32_value(active_clients));
			lprintf(LOG_INFO, "Done waiting for active clients to disconnect");
			sem_post(&sendmail_wakeup_sem);
			mswait(100);
			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");
			lprintf(LOG_INFO, "Done waiting for SendMail thread to terminate");
deuce's avatar
deuce committed
		if(!sendmail_running) {
			while(sem_destroy(&sendmail_wakeup_sem)==-1 && errno==EBUSY) {
				mswait(1);
deuce's avatar
deuce committed
				sem_post(&sendmail_wakeup_sem);
deuce's avatar
deuce committed
			}
		}
			lprintf(LOG_INFO,"Recycling server...");
			mswait(2000);
			if(startup->recycle!=NULL)
				startup->recycle(startup->cbdata);
	protected_uint32_destroy(thread_count);