Skip to content
Snippets Groups Projects
mailsrvr.c 188 KiB
Newer Older
				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,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 %u bytes of memory for smtp_t"
							,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 %u bytes of memory for pop3_t"
deuce's avatar
deuce committed
							,client_socket,sizeof(pop3_t));
						sockprintf(client_socket,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)) {
deuce's avatar
deuce committed
			lprintf(LOG_DEBUG,"Waiting for %d active clients to disconnect..."
				, 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));
			sem_post(&sendmail_wakeup_sem);
			mswait(100);
deuce's avatar
deuce committed
			lprintf(LOG_DEBUG,"Waiting for SendMail thread to terminate...");
			start=time(NULL);
			while(sendmail_running) {
				if(time(NULL)-start>TIMEOUT_THREAD_WAIT) {
deuce's avatar
deuce committed
					lprintf(LOG_WARNING,"!TIMEOUT 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);