Check-in [c7e703e4e5]
Overview
Comment:Added support for a "no logging" compile-time option (for now) and updated to support sendfile restarts
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:c7e703e4e5c6dcb188f7b97b9b7220309dfb951d
User & Date: rkeene on 2014-02-05 09:01:12
Other Links: manifest | tags
Context
2014-02-05
09:04
Additional improvements check-in: a00671c11b user: rkeene tags: trunk
09:01
Added support for a "no logging" compile-time option (for now) and updated to support sendfile restarts check-in: c7e703e4e5 user: rkeene tags: trunk
08:49
Updated to cache file descriptors check-in: 565d4b51c2 user: rkeene tags: trunk
Changes

Modified filed.c from [af10919239] to [f27d202156].

    10     10   #include <string.h>
    11     11   #include <fcntl.h>
    12     12   #include <stdio.h>
    13     13   #include <time.h>
    14     14   
    15     15   /* Default values */
    16     16   #define MAX_FAILURE_COUNT 30
    17         -#define PORT 8080
           17  +#define PORT 8081
    18     18   #define THREAD_COUNT 10
    19     19   #define BIND_ADDR "::"
    20     20   
    21     21   /* Arguments for worker threads */
    22     22   struct filed_worker_thread_args {
    23     23   	int fd;
    24     24   };
................................................................................
    98     98   
    99     99   		return(-1);
   100    100   	}
   101    101   
   102    102   	return(fd);
   103    103   }
   104    104   
          105  +/* Log a message */
          106  +#define FILED_DONT_LOG
          107  +#ifdef FILED_DONT_LOG
          108  +#  define filed_logging_thread_init() 0
          109  +#  define filed_log_msg_debug(x, ...) /**/
          110  +#  define filed_log_msg(x) /**/
          111  +#else
   105    112   /* Initialize logging thread */
   106    113   static int filed_logging_thread_init(void) {
   107    114   	/* XXX:TODO: Unimplemented */
   108    115   	return(0);
   109    116   }
   110    117   
   111         -/* Log a message */
   112    118   #define filed_log_msg_debug(x, ...) { fprintf(stderr, x, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }
   113    119   static void filed_log_msg(const char *buffer) {
   114    120   	/* XXX:TODO: Unimplemented */
   115    121   	fprintf(stderr, "%s\n", buffer);
   116    122   
   117    123   	return;
   118    124   }
   119    125   
          126  +#endif
   120    127   /* Format time per RFC2616 */
   121    128   static char *filed_format_time(char *buffer, size_t buffer_len, const time_t timeinfo) {
   122    129   	struct tm timeinfo_tm, *timeinfo_tm_p;
   123    130   
   124    131   	timeinfo_tm_p = gmtime_r(&timeinfo, &timeinfo_tm);
   125    132   	if (timeinfo_tm_p == NULL) {
   126    133   		return("unknown");
................................................................................
   292    299   	);
   293    300   }
   294    301   
   295    302   /* Handle a single request from a client */
   296    303   static void filed_handle_client(int fd) {
   297    304   	struct filed_fileinfo *fileinfo, fileinfo_b;
   298    305   	ssize_t sendfile_ret;
          306  +	size_t sendfile_len;
   299    307   	off_t sendfile_offset;
   300    308   	char *path, path_b[1010];
   301    309   	char *date_current, date_current_b[64];
   302    310   	FILE *fp;
   303    311   
   304    312   	/* Determine current time */
   305    313   	date_current = filed_format_time(date_current_b, sizeof(date_current_b), time(NULL));
................................................................................
   341    349   		fflush(fp);
   342    350   
   343    351   		filed_log_msg("PROCESS_REPLY_COMPLETE FD=... STATUS=200");
   344    352   
   345    353   		filed_log_msg("SEND_START IFD=... OFD=... BYTES=...");
   346    354   
   347    355   		sendfile_offset = 0;
   348         -		sendfile_ret = sendfile(fd, fileinfo->fd, &sendfile_offset, fileinfo->len);
   349         -		if (sendfile_ret < 0 || ((size_t) sendfile_ret) != fileinfo->len) {
   350         -			filed_log_msg("SEND_COMPLETE STATUS=ERROR IFD=... OFD=... BYTES=... BYTES_SENT=...");
   351         -		} else {
   352         -			filed_log_msg("SEND_COMPLETE STATUS=OK IFD=... OFD=... BYTES=...");
          356  +		sendfile_len = fileinfo->len;
          357  +		while (1) {
          358  +			sendfile_ret = sendfile(fd, fileinfo->fd, &sendfile_offset, sendfile_len);
          359  +			if (sendfile_ret <= 0) {
          360  +				break;
          361  +			}
          362  +
          363  +			sendfile_len -= sendfile_ret;
          364  +			if (sendfile_len == 0) {
          365  +				break;
          366  +			}
   353    367   		}
          368  +
          369  +		filed_log_msg("SEND_COMPLETE STATUS=... IFD=... OFD=... BYTES=... BYTES_SENT=...");
   354    370   
   355    371   		close(fileinfo->fd);
   356    372   
   357    373   		filed_log_msg("CLOSE_FILE FD=...");
   358    374   	}
   359    375   
   360    376   	filed_log_msg("CLOSE_CONNECTION FD=...");
................................................................................
   462    478   	if (init_ret != 0) {
   463    479   		perror("filed_init");
   464    480   
   465    481   		return(3);
   466    482   	}
   467    483   
   468    484   	/* Create logging thread */
   469         -	/* XXX:TODO: Check for errors */
   470         -	filed_logging_thread_init();
          485  +	init_ret = filed_logging_thread_init();
          486  +	if (init_ret != 0) {
          487  +		perror("filed_logging_thread_init");
          488  +
          489  +		return(4);
          490  +	}
   471    491   
   472    492   	/* Create worker threads */
   473         -	/* XXX:TODO: Check for errors */
   474         -	filed_worker_threads_init(fd, thread_count);
          493  +	init_ret = filed_worker_threads_init(fd, thread_count);
          494  +	if (init_ret != 0) {
          495  +		perror("filed_worker_threads_init");
          496  +
          497  +		return(4);
          498  +	}
   475    499   
   476    500   	/* Wait for threads to exit */
   477    501   	/* XXX:TODO: Monitor thread usage */
   478    502   	while (1) {
   479    503   		sleep(60);
   480    504   	}
   481    505   
   482    506   	/* Return in failure */
   483    507   	return(2);
   484    508   }