Check-in [c6f6825723]
Overview
Comment:Started branch to replace C11 atomics with simpler (but hopefully lock-free) implementation
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | remove-c11-atomics
Files: files | file ages | folders
SHA1:c6f68257232a7086d58f36317f895a20107520d8
User & Date: rkeene on 2016-09-22 17:35:54
Other Links: manifest | tags
Context
2020-03-31
13:41
Merged in trunk check-in: 1c1d95a764 user: rkeene tags: remove-c11-atomics
2016-09-22
17:35
Started branch to replace C11 atomics with simpler (but hopefully lock-free) implementation check-in: c6f6825723 user: rkeene tags: remove-c11-atomics
2016-08-17
16:23
Post-release version increment check-in: 1233e63987 user: rkeene tags: trunk
Changes

Modified README from [8ec76224c9] to [280019e35b].

    51     51   
    52     52      1. Logging (CFLAGS, -DFILED_DONT_LOG=1)
    53     53   	It is possible to disable ALL logging from filed.  When logging is
    54     54   	completely disabled interlocks (mutexes) for the logging pointer are
    55     55   	not engaged and the logging functions are not compiled at all.
    56     56   	This results in a slightly smaller and faster binary
    57     57   
    58         -   2. Kill idle connections (CFLAGS, -DFILED_DONT_TIMEOUT=1)
    59         -        Killing idle connections relies heavily upon C11 atomics.  This
    60         -        requires a relatively new version of GCC (4.9+) or other C compiler
    61         -        that implements this aspect of C11 and so it can be disabled at
    62         -        compile time (which is the only time it makes sense).  One day an
    63         -        alternate implementation might be present that uses a mutex instead
    64         -        of atomics at which point this documentation will be updated.
    65         -
    66         -   3. Debugging (CFLAGS, -DFILED_DEBUG=1)
           58  +   2. Debugging (CFLAGS, -DFILED_DEBUG=1)
    67     59   	This is an internal option and should only be used during development.
    68     60   
    69         -   4. Differing HTTP semantics (CFLAGS, -DFILED_NONBLOCK_HTTP=1)
           61  +   3. Differing HTTP semantics (CFLAGS, -DFILED_NONBLOCK_HTTP=1)
    70     62   	It is possible that some HTTP clients may not process the HTTP stream
    71     63   	being delivered if they cannot write to the HTTP stream itself.  This
    72     64   	has not been observed yet, but it is possible.  If these semantics are
    73     65   	needed (and they should not be) then they can be enabled with this
    74     66   	flag at the cost of performance.
    75     67   
    76         -   5. Differing chroot() semantics (CFLAGS, -DFILED_FAKE_CHROOT=1)
           68  +   4. Differing chroot() semantics (CFLAGS, -DFILED_FAKE_CHROOT=1)
    77     69           In some cases it is desirable to mangle paths with a path prefix
    78     70           rather than call chroot() at startup.  This is less secure and slower
    79     71           and should be generally avoided -- however it may be necessary to do.
    80     72           In these cases the executable may be compiled with the
    81     73           FILED_FAKE_CHROOT C preprocessor macro defined and instead of calling
    82     74           chroot() all HTTP requests will have the root suffix specified as the
    83     75           argument to the "-r" or "--root" option prepended to them.
    84     76   
    85         -   6. MIME Types (MIMETYPES)
           77  +   5. MIME Types (MIMETYPES)
    86     78   	For single-file convenience "filed" compiles the mapping of file
    87     79   	extensions (the string in the filename following its last dot ("."))
    88     80   	into the executable.  This mapping comes from a file in the format of
    89     81   		type1   type1_extension1 type1_extension2...
    90     82   		type2   type2_extension1 type2_extension2...
    91     83   		...
    92     84   	However it may not be desirable to include this mapping, or it may be
................................................................................
    99     91   Because "filed" relies on chroot(2) and setuid(2), log files cannot reliably
   100     92   be re-opened.  If you need log rotation then a second process, which can close
   101     93   and re-open log files, must be used.  Any process may be used for writing logs
   102     94   but if the process does not support log rotation then it will not provide that
   103     95   benefit.  For example, if you wish to write logs to syslogd(8) you can use
   104     96   logger(1), such as:
   105     97   	# ./filed --root /www --user nobody --log '|logger -t filed' --daemon
   106         -
   107         -Troubleshooting
   108         ----------------
   109         -   1. It won't compile, something about stdatomic.h not found or _Atomic not
   110         -      a valid type.
   111         -
   112         -      => This is a bug in your compiler:
   113         -            https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58016
   114         -
   115         -         GCC 4.7.x and 4.8.x define the macro indicating that they have C11
   116         -         support and do not define the macro that C11 requires to indicate
   117         -         that C11 atomics are not available.  They should define that macro.
   118         -
   119         -         You can disable the features in "filed" that require C11 atomics by
   120         -         defining FILED_DONT_TIMEOUT in the Makefile.

Modified filed.c from [73ad6aa47c] to [a92c173e7c].

    39     39   #include <stdarg.h>
    40     40   #include <fcntl.h>
    41     41   #include <stdio.h>
    42     42   #include <errno.h>
    43     43   #include <time.h>
    44     44   #include <pwd.h>
    45     45   
    46         -/*
    47         - * Determine if the C compiler supports C11 atomics
    48         - */
    49         -#if __STDC_VERSION__ >= 201112L
    50         -#  ifndef __STDC_NO_ATOMICS__
    51         -#    define FILED_FEATURE_C11_ATOMICS 1
    52         -#  endif
    53         -#endif
    54         -
    55         -/*
    56         - * If the C compiler does not support C11 atomics, disable TIMEOUT support
    57         - * since it relies upon it
    58         - */
    59         -#ifndef FILED_FEATURE_C11_ATOMICS
    60         -#  define FILED_DONT_TIMEOUT 1
    61         -#endif
    62         -
    63         -/*
    64         - * These headers are only required for TIMEOUT support
    65         - */
    66         -#ifndef FILED_DONT_TIMEOUT
    67         -#include <stdatomic.h>
    68         -#include <stdbool.h>
    69         -#endif
    70         -
    71     46   /* Compile time constants */
    72     47   #define FILED_VERSION "1.19"
    73     48   #define FILED_SENDFILE_MAX 16777215
    74     49   #define FILED_MAX_FAILURE_COUNT 30
    75     50   #define FILED_DEFAULT_TYPE "application/octet-stream"
    76     51   #define FILED_PATH_BUFFER_SIZE 1010
    77     52   
................................................................................
    81     56   #define BIND_ADDR "::"
    82     57   #define CACHE_SIZE 8209
    83     58   #define LOG_FILE "-"
    84     59   
    85     60   /* Fuzzing Test Code */
    86     61   #ifdef FILED_TEST_AFL
    87     62   #define FILED_DONT_LOG 1
    88         -#define FILED_DONT_TIMEOUT 1
    89     63   #define pthread_create(a, x, y, z) afl_pthread_create(a, x, y, z)
    90     64   #define bind(x, y, z) afl_bind(x, y, z)
    91     65   #define socket(x, y, z) 8193
    92     66   #define listen(x, y) 0
    93     67   #define accept(x, y, z) afl_accept(x, y, z)
    94     68   #define close(x) { if (strcmp(#x, "random_fd") == 0) { close(x); } else { exit(0); } }
    95     69   #define fclose(x) exit(0)
................................................................................
   624    598   #define filed_sockettimeout_thread_init() 0
   625    599   #define filed_sockettimeout_init() 0
   626    600   #define filed_sockettimeout_accept(x) /**/
   627    601   #define filed_sockettimeout_processing_start(x) /**/
   628    602   #define filed_sockettimeout_processing_end(x) /**/
   629    603   #define filed_sockettimeout_close(x) /**/
   630    604   #else
   631         -_Atomic time_t filed_sockettimeout_time;
          605  +time_t filed_sockettimeout_time;
   632    606   struct {
   633         -	_Atomic time_t expiration_time;
   634         -	_Atomic pthread_t thread_id;
   635         -	bool valid;
          607  +	time_t expiration_time;
          608  +	pthread_t thread_id;
          609  +	int valid;
   636    610   }* filed_sockettimeout_sockstatus;
   637    611   long filed_sockettimeout_sockstatus_length;
   638    612   int filed_sockettimeout_devnull_fd;
   639    613   
   640    614   static int filed_sockettimeout_sockfd_in_range(int sockfd) {
   641    615   	if (sockfd < 3) {
   642    616   		return(0);
................................................................................
   706    680   }
   707    681   
   708    682   static void *filed_sockettimeout_thread(void *arg) {
   709    683   	time_t now, expiration_time;
   710    684   	pthread_t thread_id;
   711    685   	long idx;
   712    686   	int count;
   713         -	bool valid;
          687  +	int valid;
   714    688   
   715    689   	while (1) {
   716    690   		for (count = 0; count < 10; count++) {
   717    691   			usleep(30000000);
   718    692   
   719    693   			now = time(NULL);
   720    694