Check-in [158143b222]
Overview
Comment:Use a mutex (for now?) for closing idle sockets
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | remove-c11-atomics
Files: files | file ages | folders
SHA1: 158143b2224ff7846e285f85bf2da31e48c73559
User & Date: rkeene on 2020-03-31 14:19:23
Other Links: branch diff | manifest | tags
Context
2020-03-31
14:20
Log the connect time, which is significant for closed sockets since they have no request time (since no request was made) check-in: 05f8958cc7 user: rkeene tags: remove-c11-atomics
14:19
Use a mutex (for now?) for closing idle sockets check-in: 158143b222 user: rkeene tags: remove-c11-atomics
13:41
Merged in trunk check-in: 1c1d95a764 user: rkeene tags: remove-c11-atomics
Changes

Modified Makefile from [2fd19df44e] to [8ce95f72a7].

1
2
3
4
5
6
7
8
9
CC = gcc
CFLAGS = -I. -std=gnu11 -Wall -W -pthread -O3 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE $(FILED_EXTRA_CFLAGS)
LDFLAGS = -pthread $(FILED_EXTRA_LDFLAGS)
LIBS = -lpthread $(FILED_EXTRA_LIBS)
MIMETYPES = /etc/httpd/mime.types

PREFIX = /usr/local
prefix = $(PREFIX)
bindir = $(prefix)/bin

|







1
2
3
4
5
6
7
8
9
CC = gcc
CFLAGS = -I. -Wall -W -pthread -O3 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE $(FILED_EXTRA_CFLAGS)
LDFLAGS = -pthread $(FILED_EXTRA_LDFLAGS)
LIBS = -lpthread $(FILED_EXTRA_LIBS)
MIMETYPES = /etc/httpd/mime.types

PREFIX = /usr/local
prefix = $(PREFIX)
bindir = $(prefix)/bin

Modified filed.c from [66294af131] to [a328ae775d].

607
608
609
610
611
612
613
614
615
616
617
618
619



620
621
622
623

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639




640
641
642
643
644




645
646
647
648
649
650
651
652
653


654
655
656
657
658


659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687




688




689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706


707
708
709
710

711



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732


733
734
735
736
737
738
739

#ifdef FILED_DONT_TIMEOUT
#define filed_sockettimeout_thread_init() 0
#define filed_sockettimeout_init() 0
#define filed_sockettimeout_accept(x) /**/
#define filed_sockettimeout_processing_start(x) /**/
#define filed_sockettimeout_processing_end(x) /**/
#define filed_sockettimeout_close(x) /**/
#else
time_t filed_sockettimeout_time;
struct {
	time_t expiration_time;
	pthread_t thread_id;



	int valid;
} *filed_sockettimeout_sockstatus;
long filed_sockettimeout_sockstatus_length;
int filed_sockettimeout_devnull_fd;


static int filed_sockettimeout_sockfd_in_range(int sockfd) {
	if (sockfd < 3) {
		return(0);
	}

	if (sockfd > filed_sockettimeout_sockstatus_length) {
		return(0);
	}

	return(1);
}

static void filed_sockettimeout_expire(int sockfd, int length) {
	time_t now, expire;





	now = atomic_load(&filed_sockettimeout_time);

	expire = now + length;

	atomic_store(&filed_sockettimeout_sockstatus[sockfd].expiration_time, expire);





	return;
}

static void filed_sockettimeout_accept(int sockfd) {
	if (!filed_sockettimeout_sockfd_in_range(sockfd)) {
		return;
	}



	filed_sockettimeout_expire(sockfd, 60);

	atomic_store(&filed_sockettimeout_sockstatus[sockfd].thread_id, pthread_self());

	atomic_store(&filed_sockettimeout_sockstatus[sockfd].valid, true);



	return;
}

static void filed_sockettimeout_processing_start(int sockfd) {
	if (!filed_sockettimeout_sockfd_in_range(sockfd)) {
		return;
	}

	filed_sockettimeout_expire(sockfd, 86400);

	return;
}

static void filed_sockettimeout_processing_end(int sockfd) {
	if (!filed_sockettimeout_sockfd_in_range(sockfd)) {
		return;
	}

	filed_sockettimeout_expire(sockfd, 60);

	return;
}

static void filed_sockettimeout_close(int sockfd) {
	if (!filed_sockettimeout_sockfd_in_range(sockfd)) {
		return;
	}





	atomic_store(&filed_sockettimeout_sockstatus[sockfd].valid, false);





	return;
}

static void *filed_sockettimeout_thread(void *arg) {
	struct timespec sleep_time;
	time_t now, expiration_time;
	pthread_t thread_id;
	long idx;
	int count;
	int valid;

	while (1) {
		for (count = 0; count < 10; count++) {
			sleep_time.tv_sec = 30;
			sleep_time.tv_nsec = 0;
			nanosleep(&sleep_time, NULL);



			now = time(NULL);

			atomic_store(&filed_sockettimeout_time, now);
		}





		for (idx = 0; idx < filed_sockettimeout_sockstatus_length; idx++) {
			valid = atomic_load(&filed_sockettimeout_sockstatus[idx].valid);

			if (!valid) {
				continue;
			}

			expiration_time = atomic_load(&filed_sockettimeout_sockstatus[idx].expiration_time);

			thread_id = atomic_load(&filed_sockettimeout_sockstatus[idx].thread_id);

			if (expiration_time > now) {
				continue;
			}

			filed_sockettimeout_close(idx);

			dup2(filed_sockettimeout_devnull_fd, idx);

			pthread_kill(thread_id, SIGPIPE);
		}


	}

	return(NULL);

	/* NOTREACH: We don't actually take any arguments */
	arg = arg;
}







|





>
>
>
|



>













|


>
>
>
>
|



|
>
>
>
>









>
>
|

|

|
>
>









|









|




|




>
>
>
>
|
>
>
>
>


















>
>


|
|
>
|
>
>
>

|

|



|

|





|





>
>







607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771

#ifdef FILED_DONT_TIMEOUT
#define filed_sockettimeout_thread_init() 0
#define filed_sockettimeout_init() 0
#define filed_sockettimeout_accept(x) /**/
#define filed_sockettimeout_processing_start(x) /**/
#define filed_sockettimeout_processing_end(x) /**/
#define filed_sockettimeout_close(x, y) /**/
#else
time_t filed_sockettimeout_time;
struct {
	time_t expiration_time;
	pthread_t thread_id;
	enum {
		filed_sockettimeout_valid,
		filed_sockettimeout_invalid,
	} valid;
} *filed_sockettimeout_sockstatus;
long filed_sockettimeout_sockstatus_length;
int filed_sockettimeout_devnull_fd;
pthread_mutex_t filed_sockettimeout_mutex = PTHREAD_MUTEX_INITIALIZER;

static int filed_sockettimeout_sockfd_in_range(int sockfd) {
	if (sockfd < 3) {
		return(0);
	}

	if (sockfd > filed_sockettimeout_sockstatus_length) {
		return(0);
	}

	return(1);
}

static void filed_sockettimeout_expire(int sockfd, int length, int lockheld) {
	time_t now, expire;

	if (!lockheld) {
		pthread_mutex_lock(&filed_sockettimeout_mutex);
	}

	now = filed_sockettimeout_time;

	expire = now + length;

	filed_sockettimeout_sockstatus[sockfd].expiration_time = expire;

	if (!lockheld) {
		pthread_mutex_unlock(&filed_sockettimeout_mutex);
	}

	return;
}

static void filed_sockettimeout_accept(int sockfd) {
	if (!filed_sockettimeout_sockfd_in_range(sockfd)) {
		return;
	}

	pthread_mutex_lock(&filed_sockettimeout_mutex);

	filed_sockettimeout_expire(sockfd, 60, 1);

	filed_sockettimeout_sockstatus[sockfd].thread_id = pthread_self();

	filed_sockettimeout_sockstatus[sockfd].valid = filed_sockettimeout_valid;

	pthread_mutex_unlock(&filed_sockettimeout_mutex);

	return;
}

static void filed_sockettimeout_processing_start(int sockfd) {
	if (!filed_sockettimeout_sockfd_in_range(sockfd)) {
		return;
	}

	filed_sockettimeout_expire(sockfd, 86400, 0);

	return;
}

static void filed_sockettimeout_processing_end(int sockfd) {
	if (!filed_sockettimeout_sockfd_in_range(sockfd)) {
		return;
	}

	filed_sockettimeout_expire(sockfd, 60, 0);

	return;
}

static void filed_sockettimeout_close(int sockfd, int lockheld) {
	if (!filed_sockettimeout_sockfd_in_range(sockfd)) {
		return;
	}

	if (!lockheld) {
		pthread_mutex_lock(&filed_sockettimeout_mutex);
	}

	filed_sockettimeout_sockstatus[sockfd].valid = filed_sockettimeout_invalid;

	if (!lockheld) {
		pthread_mutex_unlock(&filed_sockettimeout_mutex);
	}

	return;
}

static void *filed_sockettimeout_thread(void *arg) {
	struct timespec sleep_time;
	time_t now, expiration_time;
	pthread_t thread_id;
	long idx;
	int count;
	int valid;

	while (1) {
		for (count = 0; count < 10; count++) {
			sleep_time.tv_sec = 30;
			sleep_time.tv_nsec = 0;
			nanosleep(&sleep_time, NULL);

			pthread_mutex_lock(&filed_sockettimeout_mutex);

			now = time(NULL);

			filed_sockettimeout_time = now;

			pthread_mutex_unlock(&filed_sockettimeout_mutex);
		}

		pthread_mutex_lock(&filed_sockettimeout_mutex);

		for (idx = 0; idx < filed_sockettimeout_sockstatus_length; idx++) {
			valid = filed_sockettimeout_sockstatus[idx].valid;

			if (valid != filed_sockettimeout_valid) {
				continue;
			}

			expiration_time = filed_sockettimeout_sockstatus[idx].expiration_time;

			thread_id = filed_sockettimeout_sockstatus[idx].thread_id;

			if (expiration_time > now) {
				continue;
			}

			filed_sockettimeout_close(idx, 1);

			dup2(filed_sockettimeout_devnull_fd, idx);

			pthread_kill(thread_id, SIGPIPE);
		}

		pthread_mutex_unlock(&filed_sockettimeout_mutex);
	}

	return(NULL);

	/* NOTREACH: We don't actually take any arguments */
	arg = arg;
}
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
	filed_sockettimeout_sockstatus_length = maxfd;
	filed_sockettimeout_sockstatus = malloc(sizeof(*filed_sockettimeout_sockstatus) * filed_sockettimeout_sockstatus_length);
	if (filed_sockettimeout_sockstatus == NULL) {
		return(-1);
	}

	for (idx = 0; idx < maxfd; idx++) {
		filed_sockettimeout_sockstatus[idx].valid = false;
	}

	filed_sockettimeout_devnull_fd = open("/dev/null", O_RDWR);
	if (filed_sockettimeout_devnull_fd < 0) {
		return(-1);
	}








|







789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
	filed_sockettimeout_sockstatus_length = maxfd;
	filed_sockettimeout_sockstatus = malloc(sizeof(*filed_sockettimeout_sockstatus) * filed_sockettimeout_sockstatus_length);
	if (filed_sockettimeout_sockstatus == NULL) {
		return(-1);
	}

	for (idx = 0; idx < maxfd; idx++) {
		filed_sockettimeout_sockstatus[idx].valid = filed_sockettimeout_invalid;
	}

	filed_sockettimeout_devnull_fd = open("/dev/null", O_RDWR);
	if (filed_sockettimeout_devnull_fd < 0) {
		return(-1);
	}

1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
	/** reason must point to a globally allocated value **/
	log->reason = reason;
	log->http_code = error_number;

	filed_log_entry(log);

	/* Close connection */
	filed_sockettimeout_close(fileno(fp));

	fclose(fp);

	return;
}

/* Return a redirect to index.html */







|







1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
	/** reason must point to a globally allocated value **/
	log->reason = reason;
	log->http_code = error_number;

	filed_log_entry(log);

	/* Close connection */
	filed_sockettimeout_close(fileno(fp), 0);

	fclose(fp);

	return;
}

/* Return a redirect to index.html */
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
	/* Log redirect */
	log->reason = "redirect";
	log->http_code = http_code;

	filed_log_entry(log);

	/* Close connection */
	filed_sockettimeout_close(fileno(fp));

	fclose(fp);

	return;

	/* Currently unused: path */
	path = path;







|







1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
	/* Log redirect */
	log->reason = "redirect";
	log->http_code = http_code;

	filed_log_entry(log);

	/* Close connection */
	filed_sockettimeout_close(fileno(fp), 0);

	fclose(fp);

	return;

	/* Currently unused: path */
	path = path;
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252

	/* Determine current time */
	date_current = filed_format_time(date_current_b, sizeof(date_current_b), time(NULL));

	/* Open socket as ANSI I/O for ease of use */
	fp = fdopen(fd, "w+b");
	if (fp == NULL) {
		filed_sockettimeout_close(fd);

		close(fd);

		log->buffer[0] = '\0';
		log->http_code = -1;
		log->reason = "fdopen_failed";








|







1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284

	/* Determine current time */
	date_current = filed_format_time(date_current_b, sizeof(date_current_b), time(NULL));

	/* Open socket as ANSI I/O for ease of use */
	fp = fdopen(fd, "w+b");
	if (fp == NULL) {
		filed_sockettimeout_close(fd, 0);

		close(fd);

		log->buffer[0] = '\0';
		log->http_code = -1;
		log->reason = "fdopen_failed";

1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
	log->sent_length = sendfile_sent;

	filed_log_entry(log);

	close(fileinfo->fd);

	if (request->headers.connection != FILED_CONNECTION_KEEP_ALIVE) {
		filed_sockettimeout_close(fd);

		fclose(fp);

		return(FILED_CONNECTION_CLOSE);
	}

	filed_sockettimeout_processing_end(fd);







|







1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
	log->sent_length = sendfile_sent;

	filed_log_entry(log);

	close(fileinfo->fd);

	if (request->headers.connection != FILED_CONNECTION_KEEP_ALIVE) {
		filed_sockettimeout_close(fd, 0);

		fclose(fp);

		return(FILED_CONNECTION_CLOSE);
	}

	filed_sockettimeout_processing_end(fd);