0000: 2f 2a 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20 /*. * Copyright
0010: 28 63 29 20 32 30 31 34 20 2d 20 32 30 31 36 2c (c) 2014 - 2016,
0020: 20 52 6f 79 20 4b 65 65 6e 65 0a 20 2a 20 41 6c Roy Keene. * Al
0030: 6c 20 72 69 67 68 74 73 20 72 65 73 65 72 76 65 l rights reserve
0040: 64 2e 0a 20 2a 20 0a 20 2a 20 52 65 64 69 73 74 d.. * . * Redist
0050: 72 69 62 75 74 69 6f 6e 20 61 6e 64 20 75 73 65 ribution and use
0060: 20 69 6e 20 73 6f 75 72 63 65 20 61 6e 64 20 62 in source and b
0070: 69 6e 61 72 79 20 66 6f 72 6d 73 2c 20 77 69 74 inary forms, wit
0080: 68 20 6f 72 20 77 69 74 68 6f 75 74 0a 20 2a 20 h or without. *
0090: 6d 6f 64 69 66 69 63 61 74 69 6f 6e 2c 20 61 72 modification, ar
00a0: 65 20 70 65 72 6d 69 74 74 65 64 20 70 72 6f 76 e permitted prov
00b0: 69 64 65 64 20 74 68 61 74 20 74 68 65 20 66 6f ided that the fo
00c0: 6c 6c 6f 77 69 6e 67 20 63 6f 6e 64 69 74 69 6f llowing conditio
00d0: 6e 73 20 61 72 65 20 6d 65 74 3a 0a 20 2a 20 09 ns are met:. * .
00e0: 31 2e 20 52 65 64 69 73 74 72 69 62 75 74 69 6f 1. Redistributio
00f0: 6e 73 20 6f 66 20 73 6f 75 72 63 65 20 63 6f 64 ns of source cod
0100: 65 20 6d 75 73 74 20 72 65 74 61 69 6e 20 74 68 e must retain th
0110: 65 20 61 62 6f 76 65 20 63 6f 70 79 72 69 67 68 e above copyrigh
0120: 74 0a 20 2a 20 09 20 20 20 6e 6f 74 69 63 65 2c t. * . notice,
0130: 20 74 68 69 73 20 6c 69 73 74 20 6f 66 20 63 6f this list of co
0140: 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nditions and the
0150: 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 69 73 63 6c following discl
0160: 61 69 6d 65 72 2e 0a 20 2a 20 09 32 2e 20 52 65 aimer.. * .2. Re
0170: 64 69 73 74 72 69 62 75 74 69 6f 6e 73 20 69 6e distributions in
0180: 20 62 69 6e 61 72 79 20 66 6f 72 6d 20 6d 75 73 binary form mus
0190: 74 20 72 65 70 72 6f 64 75 63 65 20 74 68 65 20 t reproduce the
01a0: 61 62 6f 76 65 20 63 6f 70 79 72 69 67 68 74 0a above copyright.
01b0: 20 2a 20 09 20 20 20 6e 6f 74 69 63 65 2c 20 74 * . notice, t
01c0: 68 69 73 20 6c 69 73 74 20 6f 66 20 63 6f 6e 64 his list of cond
01d0: 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 66 itions and the f
01e0: 6f 6c 6c 6f 77 69 6e 67 20 64 69 73 63 6c 61 69 ollowing disclai
01f0: 6d 65 72 20 69 6e 20 74 68 65 0a 20 2a 20 09 20 mer in the. * .
0200: 20 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 documentation
0210: 61 6e 64 2f 6f 72 20 6f 74 68 65 72 20 6d 61 74 and/or other mat
0220: 65 72 69 61 6c 73 20 70 72 6f 76 69 64 65 64 20 erials provided
0230: 77 69 74 68 20 74 68 65 20 64 69 73 74 72 69 62 with the distrib
0240: 75 74 69 6f 6e 2e 0a 20 2a 20 0a 20 2a 20 54 48 ution.. * . * TH
0250: 49 53 20 53 4f 46 54 57 41 52 45 20 49 53 20 50 IS SOFTWARE IS P
0260: 52 4f 56 49 44 45 44 20 42 59 20 54 48 45 20 43 ROVIDED BY THE C
0270: 4f 50 59 52 49 47 48 54 20 48 4f 4c 44 45 52 53 OPYRIGHT HOLDERS
0280: 20 41 4e 44 20 43 4f 4e 54 52 49 42 55 54 4f 52 AND CONTRIBUTOR
0290: 53 20 22 41 53 20 49 53 22 20 0a 20 2a 20 41 4e S "AS IS" . * AN
02a0: 44 20 41 4e 59 20 45 58 50 52 45 53 53 20 4f 52 D ANY EXPRESS OR
02b0: 20 49 4d 50 4c 49 45 44 20 57 41 52 52 41 4e 54 IMPLIED WARRANT
02c0: 49 45 53 2c 20 49 4e 43 4c 55 44 49 4e 47 2c 20 IES, INCLUDING,
02d0: 42 55 54 20 4e 4f 54 20 4c 49 4d 49 54 45 44 20 BUT NOT LIMITED
02e0: 54 4f 2c 20 54 48 45 20 0a 20 2a 20 49 4d 50 4c TO, THE . * IMPL
02f0: 49 45 44 20 57 41 52 52 41 4e 54 49 45 53 20 4f IED WARRANTIES O
0300: 46 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 F MERCHANTABILIT
0310: 59 20 41 4e 44 20 46 49 54 4e 45 53 53 20 46 4f Y AND FITNESS FO
0320: 52 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 R A PARTICULAR P
0330: 55 52 50 4f 53 45 20 0a 20 2a 20 41 52 45 20 44 URPOSE . * ARE D
0340: 49 53 43 4c 41 49 4d 45 44 2e 20 20 49 4e 20 4e ISCLAIMED. IN N
0350: 4f 20 45 56 45 4e 54 20 53 48 41 4c 4c 20 54 48 O EVENT SHALL TH
0360: 45 20 43 4f 50 59 52 49 47 48 54 20 48 4f 4c 44 E COPYRIGHT HOLD
0370: 45 52 20 4f 52 20 43 4f 4e 54 52 49 42 55 54 4f ER OR CONTRIBUTO
0380: 52 53 20 42 45 20 0a 20 2a 20 4c 49 41 42 4c 45 RS BE . * LIABLE
0390: 20 46 4f 52 20 41 4e 59 20 44 49 52 45 43 54 2c FOR ANY DIRECT,
03a0: 20 49 4e 44 49 52 45 43 54 2c 20 49 4e 43 49 44 INDIRECT, INCID
03b0: 45 4e 54 41 4c 2c 20 53 50 45 43 49 41 4c 2c 20 ENTAL, SPECIAL,
03c0: 45 58 45 4d 50 4c 41 52 59 2c 20 4f 52 20 0a 20 EXEMPLARY, OR .
03d0: 2a 20 43 4f 4e 53 45 51 55 45 4e 54 49 41 4c 20 * CONSEQUENTIAL
03e0: 44 41 4d 41 47 45 53 20 28 49 4e 43 4c 55 44 49 DAMAGES (INCLUDI
03f0: 4e 47 2c 20 42 55 54 20 4e 4f 54 20 4c 49 4d 49 NG, BUT NOT LIMI
0400: 54 45 44 20 54 4f 2c 20 50 52 4f 43 55 52 45 4d TED TO, PROCUREM
0410: 45 4e 54 20 4f 46 20 0a 20 2a 20 53 55 42 53 54 ENT OF . * SUBST
0420: 49 54 55 54 45 20 47 4f 4f 44 53 20 4f 52 20 53 ITUTE GOODS OR S
0430: 45 52 56 49 43 45 53 3b 20 4c 4f 53 53 20 4f 46 ERVICES; LOSS OF
0440: 20 55 53 45 2c 20 44 41 54 41 2c 20 4f 52 20 50 USE, DATA, OR P
0450: 52 4f 46 49 54 53 3b 20 4f 52 20 42 55 53 49 4e ROFITS; OR BUSIN
0460: 45 53 53 20 0a 20 2a 20 49 4e 54 45 52 52 55 50 ESS . * INTERRUP
0470: 54 49 4f 4e 29 20 48 4f 57 45 56 45 52 20 43 41 TION) HOWEVER CA
0480: 55 53 45 44 20 41 4e 44 20 4f 4e 20 41 4e 59 20 USED AND ON ANY
0490: 54 48 45 4f 52 59 20 4f 46 20 4c 49 41 42 49 4c THEORY OF LIABIL
04a0: 49 54 59 2c 20 57 48 45 54 48 45 52 20 49 4e 20 ITY, WHETHER IN
04b0: 0a 20 2a 20 43 4f 4e 54 52 41 43 54 2c 20 53 54 . * CONTRACT, ST
04c0: 52 49 43 54 20 4c 49 41 42 49 4c 49 54 59 2c 20 RICT LIABILITY,
04d0: 4f 52 20 54 4f 52 54 20 28 49 4e 43 4c 55 44 49 OR TORT (INCLUDI
04e0: 4e 47 20 4e 45 47 4c 49 47 45 4e 43 45 20 4f 52 NG NEGLIGENCE OR
04f0: 20 4f 54 48 45 52 57 49 53 45 29 20 0a 20 2a 20 OTHERWISE) . *
0500: 41 52 49 53 49 4e 47 20 49 4e 20 41 4e 59 20 57 ARISING IN ANY W
0510: 41 59 20 4f 55 54 20 4f 46 20 54 48 45 20 55 53 AY OUT OF THE US
0520: 45 20 4f 46 20 54 48 49 53 20 53 4f 46 54 57 41 E OF THIS SOFTWA
0530: 52 45 2c 20 45 56 45 4e 20 49 46 20 41 44 56 49 RE, EVEN IF ADVI
0540: 53 45 44 20 4f 46 20 54 48 45 20 0a 20 2a 20 50 SED OF THE . * P
0550: 4f 53 53 49 42 49 4c 49 54 59 20 4f 46 20 53 55 OSSIBILITY OF SU
0560: 43 48 20 44 41 4d 41 47 45 2e 0a 20 2a 2f 0a 23 CH DAMAGE.. */.#
0570: 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 65 6e include <sys/sen
0580: 64 66 69 6c 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 dfile.h>.#includ
0590: 65 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68 3e e <sys/socket.h>
05a0: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 .#include <sys/t
05b0: 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 ypes.h>.#include
05c0: 20 3c 61 72 70 61 2f 69 6e 65 74 2e 68 3e 0a 23 <arpa/inet.h>.#
05d0: 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d 61 include <sys/mma
05e0: 6e 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 n.h>.#include <s
05f0: 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 6c ys/stat.h>.#incl
0600: 75 64 65 20 3c 73 79 73 2f 77 61 69 74 2e 68 3e ude <sys/wait.h>
0610: 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65 .#include <pthre
0620: 61 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c ad.h>.#include <
0630: 73 74 72 69 6e 67 73 2e 68 3e 0a 23 69 6e 63 6c strings.h>.#incl
0640: 75 64 65 20 3c 73 69 67 6e 61 6c 2e 68 3e 0a 23 ude <signal.h>.#
0650: 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e include <stdlib.
0660: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 h>.#include <uni
0670: 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 std.h>.#include
0680: 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c <string.h>.#incl
0690: 75 64 65 20 3c 67 65 74 6f 70 74 2e 68 3e 0a 23 ude <getopt.h>.#
06a0: 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e include <stdarg.
06b0: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e h>.#include <fcn
06c0: 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c tl.h>.#include <
06d0: 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 stdio.h>.#includ
06e0: 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 23 69 6e 63 e <errno.h>.#inc
06f0: 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 23 69 lude <time.h>.#i
0700: 6e 63 6c 75 64 65 20 3c 70 77 64 2e 68 3e 0a 0a nclude <pwd.h>..
0710: 2f 2a 0a 20 2a 20 44 65 74 65 72 6d 69 6e 65 20 /*. * Determine
0720: 69 66 20 74 68 65 20 43 20 63 6f 6d 70 69 6c 65 if the C compile
0730: 72 20 73 75 70 70 6f 72 74 73 20 43 31 31 20 61 r supports C11 a
0740: 74 6f 6d 69 63 73 0a 20 2a 2f 0a 23 69 66 20 5f tomics. */.#if _
0750: 5f 53 54 44 43 5f 56 45 52 53 49 4f 4e 5f 5f 20 _STDC_VERSION__
0760: 3e 3d 20 32 30 31 31 31 32 4c 0a 23 20 20 69 66 >= 201112L.# if
0770: 6e 64 65 66 20 5f 5f 53 54 44 43 5f 4e 4f 5f 41 ndef __STDC_NO_A
0780: 54 4f 4d 49 43 53 5f 5f 0a 23 20 20 20 20 64 65 TOMICS__.# de
0790: 66 69 6e 65 20 46 49 4c 45 44 5f 46 45 41 54 55 fine FILED_FEATU
07a0: 52 45 5f 43 31 31 5f 41 54 4f 4d 49 43 53 20 31 RE_C11_ATOMICS 1
07b0: 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 .# endif.#endif
07c0: 0a 0a 2f 2a 0a 20 2a 20 49 66 20 74 68 65 20 43 ../*. * If the C
07d0: 20 63 6f 6d 70 69 6c 65 72 20 64 6f 65 73 20 6e compiler does n
07e0: 6f 74 20 73 75 70 70 6f 72 74 20 43 31 31 20 61 ot support C11 a
07f0: 74 6f 6d 69 63 73 2c 20 64 69 73 61 62 6c 65 20 tomics, disable
0800: 54 49 4d 45 4f 55 54 20 73 75 70 70 6f 72 74 0a TIMEOUT support.
0810: 20 2a 20 73 69 6e 63 65 20 69 74 20 72 65 6c 69 * since it reli
0820: 65 73 20 75 70 6f 6e 20 69 74 0a 20 2a 2f 0a 23 es upon it. */.#
0830: 69 66 6e 64 65 66 20 46 49 4c 45 44 5f 46 45 41 ifndef FILED_FEA
0840: 54 55 52 45 5f 43 31 31 5f 41 54 4f 4d 49 43 53 TURE_C11_ATOMICS
0850: 0a 23 20 20 77 61 72 6e 69 6e 67 20 22 41 75 74 .# warning "Aut
0860: 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 66 69 6e omatically defin
0870: 69 6e 67 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 54 ing FILED_DONT_T
0880: 49 4d 45 4f 55 54 20 73 69 6e 63 65 20 79 6f 75 IMEOUT since you
0890: 72 20 43 20 63 6f 6d 70 69 6c 65 72 20 6c 61 63 r C compiler lac
08a0: 6b 73 20 43 31 31 20 61 74 6f 6d 69 63 73 22 0a ks C11 atomics".
08b0: 23 20 20 64 65 66 69 6e 65 20 46 49 4c 45 44 5f # define FILED_
08c0: 44 4f 4e 54 5f 54 49 4d 45 4f 55 54 20 31 0a 23 DONT_TIMEOUT 1.#
08d0: 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68 65 endif../*. * The
08e0: 73 65 20 68 65 61 64 65 72 73 20 61 72 65 20 6f se headers are o
08f0: 6e 6c 79 20 72 65 71 75 69 72 65 64 20 66 6f 72 nly required for
0900: 20 54 49 4d 45 4f 55 54 20 73 75 70 70 6f 72 74 TIMEOUT support
0910: 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 20 46 49 4c . */.#ifndef FIL
0920: 45 44 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55 54 0a ED_DONT_TIMEOUT.
0930: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 74 6f #include <stdato
0940: 6d 69 63 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 mic.h>.#include
0950: 3c 73 74 64 62 6f 6f 6c 2e 68 3e 0a 23 65 6e 64 <stdbool.h>.#end
0960: 69 66 0a 0a 2f 2a 20 43 6f 6d 70 69 6c 65 20 74 if../* Compile t
0970: 69 6d 65 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f ime constants */
0980: 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 56 .#define FILED_V
0990: 45 52 53 49 4f 4e 20 22 31 2e 31 35 22 0a 23 64 ERSION "1.15".#d
09a0: 65 66 69 6e 65 20 46 49 4c 45 44 5f 53 45 4e 44 efine FILED_SEND
09b0: 46 49 4c 45 5f 4d 41 58 20 31 36 37 37 37 32 31 FILE_MAX 1677721
09c0: 35 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 5.#define FILED_
09d0: 4d 41 58 5f 46 41 49 4c 55 52 45 5f 43 4f 55 4e MAX_FAILURE_COUN
09e0: 54 20 33 30 0a 23 64 65 66 69 6e 65 20 46 49 4c T 30.#define FIL
09f0: 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 20 ED_DEFAULT_TYPE
0a00: 22 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 "application/oct
0a10: 65 74 2d 73 74 72 65 61 6d 22 0a 23 64 65 66 69 et-stream".#defi
0a20: 6e 65 20 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 ne FILED_PATH_BU
0a30: 46 46 45 52 5f 53 49 5a 45 20 31 30 31 30 0a 0a FFER_SIZE 1010..
0a40: 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 /* Default value
0a50: 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 4f 52 s */.#define POR
0a60: 54 20 38 30 0a 23 64 65 66 69 6e 65 20 54 48 52 T 80.#define THR
0a70: 45 41 44 5f 43 4f 55 4e 54 20 35 0a 23 64 65 66 EAD_COUNT 5.#def
0a80: 69 6e 65 20 42 49 4e 44 5f 41 44 44 52 20 22 3a ine BIND_ADDR ":
0a90: 3a 22 0a 23 64 65 66 69 6e 65 20 43 41 43 48 45 :".#define CACHE
0aa0: 5f 53 49 5a 45 20 38 32 30 39 0a 23 64 65 66 69 _SIZE 8209.#defi
0ab0: 6e 65 20 4c 4f 47 5f 46 49 4c 45 20 22 2d 22 0a ne LOG_FILE "-".
0ac0: 0a 2f 2a 20 46 75 7a 7a 69 6e 67 20 54 65 73 74 ./* Fuzzing Test
0ad0: 20 43 6f 64 65 20 2a 2f 0a 23 69 66 64 65 66 20 Code */.#ifdef
0ae0: 46 49 4c 45 44 5f 54 45 53 54 5f 41 46 4c 0a 23 FILED_TEST_AFL.#
0af0: 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 44 4f 4e define FILED_DON
0b00: 54 5f 4c 4f 47 20 31 0a 23 64 65 66 69 6e 65 20 T_LOG 1.#define
0b10: 46 49 4c 45 44 5f 44 4f 4e 54 5f 54 49 4d 45 4f FILED_DONT_TIMEO
0b20: 55 54 20 31 0a 23 64 65 66 69 6e 65 20 70 74 68 UT 1.#define pth
0b30: 72 65 61 64 5f 63 72 65 61 74 65 28 61 2c 20 78 read_create(a, x
0b40: 2c 20 79 2c 20 7a 29 20 61 66 6c 5f 70 74 68 72 , y, z) afl_pthr
0b50: 65 61 64 5f 63 72 65 61 74 65 28 61 2c 20 78 2c ead_create(a, x,
0b60: 20 79 2c 20 7a 29 0a 23 64 65 66 69 6e 65 20 62 y, z).#define b
0b70: 69 6e 64 28 78 2c 20 79 2c 20 7a 29 20 61 66 6c ind(x, y, z) afl
0b80: 5f 62 69 6e 64 28 78 2c 20 79 2c 20 7a 29 0a 23 _bind(x, y, z).#
0b90: 64 65 66 69 6e 65 20 73 6f 63 6b 65 74 28 78 2c define socket(x,
0ba0: 20 79 2c 20 7a 29 20 38 31 39 33 0a 23 64 65 66 y, z) 8193.#def
0bb0: 69 6e 65 20 6c 69 73 74 65 6e 28 78 2c 20 79 29 ine listen(x, y)
0bc0: 20 30 0a 23 64 65 66 69 6e 65 20 61 63 63 65 70 0.#define accep
0bd0: 74 28 78 2c 20 79 2c 20 7a 29 20 61 66 6c 5f 61 t(x, y, z) afl_a
0be0: 63 63 65 70 74 28 78 2c 20 79 2c 20 7a 29 0a 23 ccept(x, y, z).#
0bf0: 64 65 66 69 6e 65 20 63 6c 6f 73 65 28 78 29 20 define close(x)
0c00: 7b 20 69 66 20 28 73 74 72 63 6d 70 28 23 78 2c { if (strcmp(#x,
0c10: 20 22 72 61 6e 64 6f 6d 5f 66 64 22 29 20 3d 3d "random_fd") ==
0c20: 20 30 29 20 7b 20 63 6c 6f 73 65 28 78 29 3b 20 0) { close(x);
0c30: 7d 20 65 6c 73 65 20 7b 20 65 78 69 74 28 30 29 } else { exit(0)
0c40: 3b 20 7d 20 7d 0a 23 64 65 66 69 6e 65 20 66 63 ; } }.#define fc
0c50: 6c 6f 73 65 28 78 29 20 65 78 69 74 28 30 29 0a lose(x) exit(0).
0c60: 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 6c 5f .static int afl_
0c70: 61 63 63 65 70 74 28 69 6e 74 20 78 2c 20 76 6f accept(int x, vo
0c80: 69 64 20 2a 61 64 64 72 2c 20 76 6f 69 64 20 2a id *addr, void *
0c90: 7a 29 20 7b 0a 09 28 28 73 74 72 75 63 74 20 73 z) {..((struct s
0ca0: 6f 63 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20 61 ockaddr_in6 *) a
0cb0: 64 64 72 29 2d 3e 73 69 6e 36 5f 66 61 6d 69 6c ddr)->sin6_famil
0cc0: 79 20 3d 20 41 46 5f 49 4e 45 54 20 2b 20 41 46 y = AF_INET + AF
0cd0: 5f 49 4e 45 54 36 20 2b 20 31 3b 0a 09 72 65 74 _INET6 + 1;..ret
0ce0: 75 72 6e 28 53 54 44 49 4e 5f 46 49 4c 45 4e 4f urn(STDIN_FILENO
0cf0: 29 3b 0a 09 78 20 3d 20 78 3b 0a 09 7a 20 3d 20 );..x = x;..z =
0d00: 7a 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 z;.}..static int
0d10: 20 61 66 6c 5f 62 69 6e 64 28 69 6e 74 20 78 2c afl_bind(int x,
0d20: 20 76 6f 69 64 20 2a 79 2c 20 73 6f 63 6b 6c 65 void *y, sockle
0d30: 6e 5f 74 20 7a 29 20 7b 0a 09 72 65 74 75 72 6e n_t z) {..return
0d40: 28 38 31 39 34 29 3b 0a 09 78 20 3d 20 78 3b 0a (8194);..x = x;.
0d50: 09 79 20 3d 20 79 3b 0a 09 7a 20 3d 20 7a 3b 0a .y = y;..z = z;.
0d60: 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 }..static int af
0d70: 6c 5f 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 l_pthread_create
0d80: 28 70 74 68 72 65 61 64 5f 74 20 2a 74 68 72 65 (pthread_t *thre
0d90: 61 64 2c 20 63 6f 6e 73 74 20 70 74 68 72 65 61 ad, const pthrea
0da0: 64 5f 61 74 74 72 5f 74 20 2a 61 74 74 72 2c 20 d_attr_t *attr,
0db0: 76 6f 69 64 20 2a 28 2a 73 74 61 72 74 5f 72 6f void *(*start_ro
0dc0: 75 74 69 6e 65 29 20 28 76 6f 69 64 20 2a 29 2c utine) (void *),
0dd0: 20 76 6f 69 64 20 2a 61 72 67 29 20 7b 0a 09 73 void *arg) {..s
0de0: 74 61 72 74 5f 72 6f 75 74 69 6e 65 28 61 72 67 tart_routine(arg
0df0: 29 3b 0a 09 65 78 69 74 28 33 29 3b 0a 09 74 68 );..exit(3);..th
0e00: 72 65 61 64 20 3d 20 74 68 72 65 61 64 3b 0a 09 read = thread;..
0e10: 61 74 74 72 20 3d 20 61 74 74 72 3b 0a 7d 0a 23 attr = attr;.}.#
0e20: 65 6e 64 69 66 0a 0a 2f 2a 20 43 6f 6e 66 69 67 endif../* Config
0e30: 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 uration options
0e40: 74 68 61 74 20 77 6f 72 6b 20 74 68 72 65 61 64 that work thread
0e50: 73 20 6e 65 65 64 20 74 6f 20 62 65 20 61 77 61 s need to be awa
0e60: 72 65 20 6f 66 20 2a 2f 0a 73 74 72 75 63 74 20 re of */.struct
0e70: 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 7b 0a filed_options {.
0e80: 09 69 6e 74 20 76 68 6f 73 74 73 5f 65 6e 61 62 .int vhosts_enab
0e90: 6c 65 64 3b 0a 7d 3b 0a 0a 2f 2a 20 41 72 67 75 led;.};../* Argu
0ea0: 6d 65 6e 74 73 20 66 6f 72 20 77 6f 72 6b 65 72 ments for worker
0eb0: 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 72 75 threads */.stru
0ec0: 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f ct filed_worker_
0ed0: 74 68 72 65 61 64 5f 61 72 67 73 20 7b 0a 09 69 thread_args {..i
0ee0: 6e 74 20 66 64 3b 0a 09 73 74 72 75 63 74 20 66 nt fd;..struct f
0ef0: 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 6f 70 74 iled_options opt
0f00: 69 6f 6e 73 3b 0a 7d 3b 0a 0a 2f 2a 20 41 72 67 ions;.};../* Arg
0f10: 75 6d 65 6e 74 73 20 66 6f 72 20 6c 6f 67 67 69 uments for loggi
0f20: 6e 67 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 ng threads */.st
0f30: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 ruct filed_loggi
0f40: 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 73 20 7b ng_thread_args {
0f50: 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 7d 3b 0a 0a ..FILE *fp;.};..
0f60: 2f 2a 20 46 69 6c 65 20 69 6e 66 6f 72 6d 61 74 /* File informat
0f70: 69 6f 6e 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 ion */.struct fi
0f80: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 7b 0a 09 led_fileinfo {..
0f90: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 pthread_mutex_t
0fa0: 6d 75 74 65 78 3b 0a 09 63 68 61 72 20 70 61 74 mutex;..char pat
0fb0: 68 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 h[FILED_PATH_BUF
0fc0: 46 45 52 5f 53 49 5a 45 5d 3b 0a 09 69 6e 74 20 FER_SIZE];..int
0fd0: 66 64 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a fd;..off_t len;.
0fe0: 09 63 68 61 72 20 2a 6c 61 73 74 6d 6f 64 3b 0a .char *lastmod;.
0ff0: 09 63 68 61 72 20 6c 61 73 74 6d 6f 64 5f 62 5b .char lastmod_b[
1000: 36 34 5d 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 64];..const char
1010: 20 2a 74 79 70 65 3b 0a 09 63 68 61 72 20 65 74 *type;..char et
1020: 61 67 5b 36 34 5d 3b 0a 7d 3b 0a 0a 2f 2a 20 52 ag[64];.};../* R
1030: 65 71 75 65 73 74 20 76 61 72 69 61 62 6c 65 73 equest variables
1040: 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 */.struct filed
1050: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 7b 0a _http_request {.
1060: 09 2f 2a 2a 20 42 75 66 66 65 72 73 20 2a 2a 2f ./** Buffers **/
1070: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 ..struct filed_f
1080: 69 6c 65 69 6e 66 6f 20 66 69 6c 65 69 6e 66 6f ileinfo fileinfo
1090: 3b 0a 09 63 68 61 72 20 74 6d 70 62 75 66 5b 46 ;..char tmpbuf[F
10a0: 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 ILED_PATH_BUFFER
10b0: 5f 53 49 5a 45 5d 3b 0a 0a 09 2f 2a 2a 20 48 54 _SIZE];.../** HT
10c0: 54 50 20 52 65 71 75 65 73 74 20 69 6e 66 6f 72 TP Request infor
10d0: 6d 61 74 69 6f 6e 20 2a 2a 2f 0a 09 2f 2a 2a 2a mation **/../***
10e0: 20 54 79 70 65 20 6f 66 20 72 65 71 75 65 73 74 Type of request
10f0: 20 28 48 45 41 44 20 6f 72 20 47 45 54 29 20 2a (HEAD or GET) *
1100: 2a 2a 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 **/..enum {...FI
1110: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 LED_REQUEST_METH
1120: 4f 44 5f 47 45 54 2c 0a 09 09 46 49 4c 45 44 5f OD_GET,...FILED_
1130: 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48 REQUEST_METHOD_H
1140: 45 41 44 0a 09 7d 20 6d 65 74 68 6f 64 3b 0a 0a EAD..} method;..
1150: 09 2f 2a 2a 2a 20 50 61 74 68 20 62 65 69 6e 67 ./*** Path being
1160: 20 72 65 71 75 65 73 74 65 64 20 2a 2a 2a 2f 0a requested ***/.
1170: 09 63 68 61 72 20 70 61 74 68 5b 46 49 4c 45 44 .char path[FILED
1180: 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a _PATH_BUFFER_SIZ
1190: 45 5d 3b 20 0a 0a 09 2f 2a 2a 2a 20 50 61 74 68 E]; .../*** Path
11a0: 20 74 79 70 65 20 2a 2a 2a 2f 0a 09 65 6e 75 6d type ***/..enum
11b0: 20 7b 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45 {...FILED_REQUE
11c0: 53 54 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 ST_TYPE_DIRECTOR
11d0: 59 2c 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45 Y,...FILED_REQUE
11e0: 53 54 5f 54 59 50 45 5f 4f 54 48 45 52 0a 09 7d ST_TYPE_OTHER..}
11f0: 20 74 79 70 65 3b 0a 0a 09 73 74 72 75 63 74 20 type;...struct
1200: 7b 0a 09 09 73 74 72 75 63 74 20 7b 0a 09 09 09 {...struct {....
1210: 69 6e 74 20 70 72 65 73 65 6e 74 3b 0a 09 09 09 int present;....
1220: 6f 66 66 5f 74 20 6f 66 66 73 65 74 3b 20 20 20 off_t offset;
1230: 2f 2a 2a 2a 20 52 61 6e 67 65 20 73 74 61 72 74 /*** Range start
1240: 20 2a 2a 2a 2f 0a 09 09 09 6f 66 66 5f 74 20 6c ***/....off_t l
1250: 65 6e 67 74 68 3b 20 20 20 2f 2a 2a 2a 20 52 61 ength; /*** Ra
1260: 6e 67 65 20 6c 65 6e 67 74 68 20 2a 2a 2a 2f 0a nge length ***/.
1270: 09 09 7d 20 72 61 6e 67 65 3b 0a 0a 09 09 73 74 ..} range;....st
1280: 72 75 63 74 20 7b 0a 09 09 09 69 6e 74 20 70 72 ruct {....int pr
1290: 65 73 65 6e 74 3b 0a 09 09 09 63 68 61 72 20 68 esent;....char h
12a0: 6f 73 74 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 ost[FILED_PATH_B
12b0: 55 46 46 45 52 5f 53 49 5a 45 5d 3b 0a 09 09 7d UFFER_SIZE];...}
12c0: 20 68 6f 73 74 3b 0a 0a 09 09 65 6e 75 6d 20 7b host;....enum {
12d0: 0a 09 09 09 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 ....FILED_CONNEC
12e0: 54 49 4f 4e 5f 43 4c 4f 53 45 2c 0a 09 09 09 46 TION_CLOSE,....F
12f0: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f ILED_CONNECTION_
1300: 4b 45 45 50 5f 41 4c 49 56 45 0a 09 09 7d 20 63 KEEP_ALIVE...} c
1310: 6f 6e 6e 65 63 74 69 6f 6e 3b 0a 09 7d 20 68 65 onnection;..} he
1320: 61 64 65 72 73 3b 0a 7d 3b 0a 0a 2f 2a 20 4c 6f aders;.};../* Lo
1330: 67 20 72 65 63 6f 72 64 20 2a 2f 0a 73 74 72 75 g record */.stru
1340: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ct filed_log_ent
1350: 72 79 20 7b 0a 09 2f 2a 20 54 79 70 65 20 6f 66 ry {../* Type of
1360: 20 6c 6f 67 20 65 6e 74 72 79 20 2a 2f 0a 09 65 log entry */..e
1370: 6e 75 6d 20 7b 0a 09 09 46 49 4c 45 44 5f 4c 4f num {...FILED_LO
1380: 47 5f 54 59 50 45 5f 4d 45 53 53 41 47 45 2c 0a G_TYPE_MESSAGE,.
1390: 09 09 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 ..FILED_LOG_TYPE
13a0: 5f 54 52 41 4e 53 46 45 52 0a 09 7d 20 74 79 70 _TRANSFER..} typ
13b0: 65 3b 0a 0a 09 2f 2a 20 4c 69 6e 6b 65 64 20 6c e;.../* Linked l
13c0: 69 73 74 20 68 65 61 64 2f 74 61 69 6c 20 2a 2f ist head/tail */
13d0: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c ..struct filed_l
13e0: 6f 67 5f 65 6e 74 72 79 20 2a 5f 6e 65 78 74 3b og_entry *_next;
13f0: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c ..struct filed_l
1400: 6f 67 5f 65 6e 74 72 79 20 2a 5f 70 72 65 76 3b og_entry *_prev;
1410: 0a 0a 09 2f 2a 20 54 68 72 65 61 64 20 66 72 6f .../* Thread fro
1420: 6d 20 77 68 69 63 68 20 74 68 69 73 20 6c 6f 67 m which this log
1430: 20 65 6e 74 72 79 20 65 6d 69 6e 61 74 65 73 20 entry eminates
1440: 2a 2f 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 */..pthread_t th
1450: 72 65 61 64 3b 0a 0a 09 2f 2a 20 4d 65 73 73 61 read;.../* Messa
1460: 67 65 20 62 75 66 66 65 72 20 66 6f 72 20 74 79 ge buffer for ty
1470: 70 65 20 3d 20 4d 45 53 53 41 47 45 20 2a 2f 0a pe = MESSAGE */.
1480: 09 2f 2a 20 50 61 74 68 20 62 75 66 66 65 72 20 ./* Path buffer
1490: 66 6f 72 20 74 79 70 65 20 3d 20 54 52 41 4e 53 for type = TRANS
14a0: 46 45 52 20 2a 2f 0a 09 63 68 61 72 20 62 75 66 FER */..char buf
14b0: 66 65 72 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 fer[FILED_PATH_B
14c0: 55 46 46 45 52 5f 53 49 5a 45 5d 3b 0a 0a 09 2f UFFER_SIZE];.../
14d0: 2a 20 49 74 65 6d 73 20 66 6f 72 20 74 79 70 65 * Items for type
14e0: 20 3d 20 54 52 41 4e 53 46 45 52 20 2a 2f 0a 09 = TRANSFER */..
14f0: 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 int http_code;..
1500: 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 61 73 const char *reas
1510: 6f 6e 3b 0a 09 74 69 6d 65 5f 74 20 73 74 61 72 on;..time_t star
1520: 74 74 69 6d 65 3b 0a 09 74 69 6d 65 5f 74 20 65 ttime;..time_t e
1530: 6e 64 74 69 6d 65 3b 0a 09 6f 66 66 5f 74 20 72 ndtime;..off_t r
1540: 65 71 5f 6f 66 66 73 65 74 3b 0a 09 6f 66 66 5f eq_offset;..off_
1550: 74 20 72 65 71 5f 6c 65 6e 67 74 68 3b 0a 09 6f t req_length;..o
1560: 66 66 5f 74 20 73 65 6e 74 5f 6c 65 6e 67 74 68 ff_t sent_length
1570: 3b 0a 09 6f 66 66 5f 74 20 66 69 6c 65 5f 6c 65 ;..off_t file_le
1580: 6e 67 74 68 3b 0a 09 63 68 61 72 20 69 70 5b 31 ngth;..char ip[1
1590: 32 38 5d 3b 0a 09 69 6e 74 20 70 6f 72 74 3b 0a 28];..int port;.
15a0: 09 69 6e 74 20 6d 65 74 68 6f 64 3b 0a 7d 3b 0a .int method;.};.
15b0: 0a 2f 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 ./* Global varia
15c0: 62 6c 65 73 20 2a 2f 0a 2f 2a 2a 20 4f 70 65 6e bles */./** Open
15d0: 20 46 69 6c 65 20 63 61 63 68 65 20 2a 2a 2f 0a File cache **/.
15e0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c struct filed_fil
15f0: 65 69 6e 66 6f 20 2a 66 69 6c 65 64 5f 66 69 6c einfo *filed_fil
1600: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d 20 einfo_fdcache =
1610: 4e 55 4c 4c 3b 0a 75 6e 73 69 67 6e 65 64 20 69 NULL;.unsigned i
1620: 6e 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 nt filed_fileinf
1630: 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 3d o_fdcache_size =
1640: 20 30 3b 0a 0a 2f 2a 2a 20 4c 6f 67 67 69 6e 67 0;../** Logging
1650: 20 2a 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 **/.struct file
1660: 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 66 69 6c d_log_entry *fil
1670: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b ed_log_msg_list;
1680: 0a 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 .pthread_mutex_t
1690: 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c filed_log_msg_l
16a0: 69 73 74 5f 6d 75 74 65 78 3b 0a 70 74 68 72 65 ist_mutex;.pthre
16b0: 61 64 5f 63 6f 6e 64 5f 74 20 66 69 6c 65 64 5f ad_cond_t filed_
16c0: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 log_msg_list_rea
16d0: 64 79 3b 0a 0a 2f 2a 20 53 69 67 6e 61 6c 20 48 dy;../* Signal H
16e0: 61 6e 64 6c 65 72 20 2a 2f 0a 73 74 61 74 69 63 andler */.static
16f0: 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 69 67 6e void filed_sign
1700: 61 6c 5f 68 61 6e 64 6c 65 72 28 69 6e 74 20 73 al_handler(int s
1710: 69 67 6e 61 6c 5f 6e 75 6d 62 65 72 29 20 7b 0a ignal_number) {.
1720: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 .struct filed_fi
1730: 6c 65 69 6e 66 6f 20 2a 63 61 63 68 65 3b 0a 09 leinfo *cache;..
1740: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 64 78 unsigned int idx
1750: 3b 0a 0a 09 73 77 69 74 63 68 20 28 73 69 67 6e ;...switch (sign
1760: 61 6c 5f 6e 75 6d 62 65 72 29 20 7b 0a 09 09 63 al_number) {...c
1770: 61 73 65 20 53 49 47 48 55 50 3a 0a 09 09 09 66 ase SIGHUP:....f
1780: 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 69 64 78 or (idx = 0; idx
1790: 20 3c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 < filed_fileinf
17a0: 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 3b 20 o_fdcache_size;
17b0: 69 64 78 2b 2b 29 20 7b 0a 09 09 09 09 63 61 63 idx++) {.....cac
17c0: 68 65 20 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 he = &filed_file
17d0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 info_fdcache[idx
17e0: 5d 3b 0a 0a 09 09 09 09 70 74 68 72 65 61 64 5f ];......pthread_
17f0: 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 mutex_lock(&cach
1800: 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09 09 e->mutex);......
1810: 63 61 63 68 65 2d 3e 70 61 74 68 5b 30 5d 20 3d cache->path[0] =
1820: 20 27 5c 30 27 3b 0a 09 09 09 09 69 66 20 28 63 '\0';.....if (c
1830: 61 63 68 65 2d 3e 66 64 20 3e 3d 20 30 29 20 7b ache->fd >= 0) {
1840: 0a 09 09 09 09 09 63 6c 6f 73 65 28 63 61 63 68 ......close(cach
1850: 65 2d 3e 66 64 29 3b 0a 0a 09 09 09 09 09 63 61 e->fd);.......ca
1860: 63 68 65 2d 3e 66 64 20 3d 20 2d 31 3b 0a 09 09 che->fd = -1;...
1870: 09 09 7d 0a 0a 09 09 09 09 63 61 63 68 65 2d 3e ..}......cache->
1880: 6c 61 73 74 6d 6f 64 20 3d 20 22 22 3b 0a 09 09 lastmod = "";...
1890: 09 09 63 61 63 68 65 2d 3e 74 79 70 65 20 3d 20 ..cache->type =
18a0: 22 22 3b 0a 0a 09 09 09 09 70 74 68 72 65 61 64 "";......pthread
18b0: 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 _mutex_unlock(&c
18c0: 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 09 09 ache->mutex);...
18d0: 09 7d 0a 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a .}....break;..}.
18e0: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 ..return;.}../*
18f0: 49 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 Initialize cache
1900: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 */.static int f
1910: 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 65 28 iled_init_cache(
1920: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63 unsigned int cac
1930: 68 65 5f 73 69 7a 65 29 20 7b 0a 09 75 6e 73 69 he_size) {..unsi
1940: 67 6e 65 64 20 69 6e 74 20 69 64 78 3b 0a 09 69 gned int idx;..i
1950: 6e 74 20 6d 75 74 65 78 5f 69 6e 69 74 5f 72 65 nt mutex_init_re
1960: 74 3b 0a 0a 09 2f 2a 20 43 61 63 68 65 20 6d 61 t;.../* Cache ma
1970: 79 20 6e 6f 74 20 62 65 20 72 65 2d 69 6e 69 74 y not be re-init
1980: 69 61 6c 69 7a 65 64 20 2a 2f 0a 09 69 66 20 28 ialized */..if (
1990: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
19a0: 64 63 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 30 dcache_size != 0
19b0: 20 7c 7c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e || filed_filein
19c0: 66 6f 5f 66 64 63 61 63 68 65 20 21 3d 20 4e 55 fo_fdcache != NU
19d0: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 LL) {...return(1
19e0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 41 6c 6c 6f 63 );..}.../* Alloc
19f0: 61 74 65 20 63 61 63 68 65 20 2a 2f 0a 09 66 69 ate cache */..fi
1a00: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
1a10: 61 63 68 65 5f 73 69 7a 65 20 3d 20 63 61 63 68 ache_size = cach
1a20: 65 5f 73 69 7a 65 3b 0a 09 66 69 6c 65 64 5f 66 e_size;..filed_f
1a30: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 ileinfo_fdcache
1a40: 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 = malloc(sizeof(
1a50: 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f *filed_fileinfo_
1a60: 66 64 63 61 63 68 65 29 20 2a 20 66 69 6c 65 64 fdcache) * filed
1a70: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 _fileinfo_fdcach
1a80: 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 66 69 e_size);..if (fi
1a90: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
1aa0: 61 63 68 65 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a ache == NULL) {.
1ab0: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
1ac0: 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ../* Initialize
1ad0: 63 61 63 68 65 20 65 6e 74 72 69 65 73 20 2a 2f cache entries */
1ae0: 0a 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 ..for (idx = 0;
1af0: 69 64 78 20 3c 20 66 69 6c 65 64 5f 66 69 6c 65 idx < filed_file
1b00: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a info_fdcache_siz
1b10: 65 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09 6d 75 e; idx++) {...mu
1b20: 74 65 78 5f 69 6e 69 74 5f 72 65 74 20 3d 20 70 tex_init_ret = p
1b30: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 thread_mutex_ini
1b40: 74 28 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 t(&filed_fileinf
1b50: 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 6d o_fdcache[idx].m
1b60: 75 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a 09 09 69 utex, NULL);...i
1b70: 66 20 28 6d 75 74 65 78 5f 69 6e 69 74 5f 72 65 f (mutex_init_re
1b80: 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 74 t != 0) {....ret
1b90: 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 66 urn(1);...}....f
1ba0: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
1bb0: 63 61 63 68 65 5b 69 64 78 5d 2e 70 61 74 68 5b cache[idx].path[
1bc0: 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 66 69 6c 0] = '\0';...fil
1bd0: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 ed_fileinfo_fdca
1be0: 63 68 65 5b 69 64 78 5d 2e 66 64 20 3d 20 2d 31 che[idx].fd = -1
1bf0: 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e ;...filed_filein
1c00: 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e fo_fdcache[idx].
1c10: 6c 61 73 74 6d 6f 64 20 3d 20 22 22 3b 0a 09 09 lastmod = "";...
1c20: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
1c30: 64 63 61 63 68 65 5b 69 64 78 5d 2e 74 79 70 65 dcache[idx].type
1c40: 20 3d 20 22 22 3b 0a 09 7d 0a 0a 09 72 65 74 75 = "";..}...retu
1c50: 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69 rn(0);.}../* Ini
1c60: 74 69 61 6c 69 7a 65 20 70 72 6f 63 65 73 73 20 tialize process
1c70: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
1c80: 6c 65 64 5f 69 6e 69 74 28 75 6e 73 69 67 6e 65 led_init(unsigne
1c90: 64 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 d int cache_size
1ca0: 29 20 7b 0a 09 73 74 61 74 69 63 20 69 6e 74 20 ) {..static int
1cb0: 63 61 6c 6c 65 64 20 3d 20 30 3b 0a 09 73 73 69 called = 0;..ssi
1cc0: 7a 65 5f 74 20 72 65 61 64 5f 72 65 74 20 3d 20 ze_t read_ret =
1cd0: 30 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 0;..unsigned int
1ce0: 20 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 3d 20 random_value =
1cf0: 30 3b 0a 09 69 6e 74 20 63 61 63 68 65 5f 72 65 0;..int cache_re
1d00: 74 3b 0a 09 69 6e 74 20 72 61 6e 64 6f 6d 5f 66 t;..int random_f
1d10: 64 3b 0a 0a 09 69 66 20 28 63 61 6c 6c 65 64 29 d;...if (called)
1d20: 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a {...return(0);.
1d30: 09 7d 0a 0a 09 63 61 6c 6c 65 64 20 3d 20 31 3b .}...called = 1;
1d40: 0a 0a 09 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f .../* Attempt to
1d50: 20 6c 6f 63 6b 20 61 6c 6c 20 6d 65 6d 6f 72 79 lock all memory
1d60: 20 74 6f 20 70 68 79 73 69 63 61 6c 20 52 41 4d to physical RAM
1d70: 20 28 62 75 74 20 64 6f 6e 27 74 20 63 61 72 65 (but don't care
1d80: 20 69 66 20 77 65 20 63 61 6e 27 74 29 20 2a 2f if we can't) */
1d90: 0a 09 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 4c 5f 43 ..mlockall(MCL_C
1da0: 55 52 52 45 4e 54 20 7c 20 4d 43 4c 5f 46 55 54 URRENT | MCL_FUT
1db0: 55 52 45 29 3b 0a 0a 09 2f 2a 20 49 67 6e 6f 72 URE);.../* Ignor
1dc0: 65 20 53 49 47 50 49 50 45 20 2a 2f 0a 09 73 69 e SIGPIPE */..si
1dd0: 67 6e 61 6c 28 53 49 47 50 49 50 45 2c 20 53 49 gnal(SIGPIPE, SI
1de0: 47 5f 49 47 4e 29 3b 0a 0a 09 2f 2a 20 48 61 6e G_IGN);.../* Han
1df0: 64 6c 65 20 53 49 47 48 55 50 20 74 6f 20 72 65 dle SIGHUP to re
1e00: 6c 65 61 73 65 20 61 6c 6c 20 63 61 63 68 65 73 lease all caches
1e10: 20 2a 2f 0a 09 73 69 67 6e 61 6c 28 53 49 47 48 */..signal(SIGH
1e20: 55 50 2c 20 66 69 6c 65 64 5f 73 69 67 6e 61 6c UP, filed_signal
1e30: 5f 68 61 6e 64 6c 65 72 29 3b 0a 0a 09 2f 2a 20 _handler);.../*
1e40: 49 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 Initialize cache
1e50: 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 63 structure */..c
1e60: 61 63 68 65 5f 72 65 74 20 3d 20 66 69 6c 65 64 ache_ret = filed
1e70: 5f 69 6e 69 74 5f 63 61 63 68 65 28 63 61 63 68 _init_cache(cach
1e80: 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 63 61 e_size);..if (ca
1e90: 63 68 65 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a che_ret != 0) {.
1ea0: 09 09 72 65 74 75 72 6e 28 63 61 63 68 65 5f 72 ..return(cache_r
1eb0: 65 74 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 et);..}.../* Ini
1ec0: 74 69 61 6c 69 7a 65 20 72 61 6e 64 6f 6d 20 6e tialize random n
1ed0: 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
1ee0: 2a 2f 0a 09 72 61 6e 64 6f 6d 5f 66 64 20 3d 20 */..random_fd =
1ef0: 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 open("/dev/urand
1f00: 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a om", O_RDONLY);.
1f10: 09 69 66 20 28 72 61 6e 64 6f 6d 5f 66 64 20 3e .if (random_fd >
1f20: 3d 20 30 29 20 7b 0a 09 09 72 65 61 64 5f 72 65 = 0) {...read_re
1f30: 74 20 3d 20 72 65 61 64 28 72 61 6e 64 6f 6d 5f t = read(random_
1f40: 66 64 2c 20 26 72 61 6e 64 6f 6d 5f 76 61 6c 75 fd, &random_valu
1f50: 65 2c 20 73 69 7a 65 6f 66 28 72 61 6e 64 6f 6d e, sizeof(random
1f60: 5f 76 61 6c 75 65 29 29 3b 0a 0a 09 09 63 6c 6f _value));....clo
1f70: 73 65 28 72 61 6e 64 6f 6d 5f 66 64 29 3b 0a 09 se(random_fd);..
1f80: 7d 0a 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 }...random_value
1f90: 20 5e 3d 20 67 65 74 70 69 64 28 29 3b 0a 09 72 ^= getpid();..r
1fa0: 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 67 andom_value ^= g
1fb0: 65 74 75 69 64 28 29 3b 0a 09 72 61 6e 64 6f 6d etuid();..random
1fc0: 5f 76 61 6c 75 65 20 5e 3d 20 74 69 6d 65 28 4e _value ^= time(N
1fd0: 55 4c 4c 29 3b 0a 0a 09 73 72 61 6e 64 6f 6d 28 ULL);...srandom(
1fe0: 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 29 3b 0a 0a random_value);..
1ff0: 09 72 65 74 75 72 6e 28 30 29 3b 0a 0a 09 2f 2a .return(0);.../*
2000: 20 4e 4f 54 52 45 41 43 48 3a 20 52 65 61 64 20 NOTREACH: Read
2010: 6d 61 79 20 66 61 69 6c 20 6f 72 20 73 75 63 63 may fail or succ
2020: 65 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 61 63 eed, we don't ac
2030: 74 75 61 6c 6c 79 20 63 61 72 65 20 2a 2f 0a 09 tually care */..
2040: 72 65 61 64 5f 72 65 74 20 3d 20 72 65 61 64 5f read_ret = read_
2050: 72 65 74 3b 0a 7d 0a 0a 2f 2a 20 4c 69 73 74 65 ret;.}../* Liste
2060: 6e 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 n on a particula
2070: 72 20 61 64 64 72 65 73 73 2f 70 6f 72 74 20 2a r address/port *
2080: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c /.static int fil
2090: 65 64 5f 6c 69 73 74 65 6e 28 63 6f 6e 73 74 20 ed_listen(const
20a0: 63 68 61 72 20 2a 61 64 64 72 65 73 73 2c 20 75 char *address, u
20b0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 70 6f 72 74 nsigned int port
20c0: 29 20 7b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b ) {..struct sock
20d0: 61 64 64 72 5f 69 6e 36 20 61 64 64 72 5f 76 36 addr_in6 addr_v6
20e0: 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 ;..struct sockad
20f0: 64 72 5f 69 6e 20 61 64 64 72 5f 76 34 3b 0a 09 dr_in addr_v4;..
2100: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 struct sockaddr
2110: 2a 61 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f *addr;..socklen_
2120: 74 20 61 64 64 72 5f 6c 65 6e 3b 0a 09 69 6e 74 t addr_len;..int
2130: 20 70 74 6f 6e 5f 72 65 74 2c 20 62 69 6e 64 5f pton_ret, bind_
2140: 72 65 74 2c 20 6c 69 73 74 65 6e 5f 72 65 74 3b ret, listen_ret;
2150: 0a 09 69 6e 74 20 66 61 6d 69 6c 79 3b 0a 09 69 ..int family;..i
2160: 6e 74 20 66 64 3b 0a 0a 09 66 61 6d 69 6c 79 20 nt fd;...family
2170: 3d 20 41 46 5f 49 4e 45 54 36 3b 0a 09 70 74 6f = AF_INET6;..pto
2180: 6e 5f 72 65 74 20 3d 20 69 6e 65 74 5f 70 74 6f n_ret = inet_pto
2190: 6e 28 66 61 6d 69 6c 79 2c 20 61 64 64 72 65 73 n(family, addres
21a0: 73 2c 20 26 61 64 64 72 5f 76 36 2e 73 69 6e 36 s, &addr_v6.sin6
21b0: 5f 61 64 64 72 2e 73 36 5f 61 64 64 72 29 3b 0a _addr.s6_addr);.
21c0: 09 69 66 20 28 70 74 6f 6e 5f 72 65 74 20 21 3d .if (pton_ret !=
21d0: 20 31 29 20 7b 0a 09 09 66 61 6d 69 6c 79 20 3d 1) {...family =
21e0: 20 41 46 5f 49 4e 45 54 3b 0a 09 09 70 74 6f 6e AF_INET;...pton
21f0: 5f 72 65 74 20 3d 20 69 6e 65 74 5f 70 74 6f 6e _ret = inet_pton
2200: 28 66 61 6d 69 6c 79 2c 20 61 64 64 72 65 73 73 (family, address
2210: 2c 20 26 61 64 64 72 5f 76 34 2e 73 69 6e 5f 61 , &addr_v4.sin_a
2220: 64 64 72 2e 73 5f 61 64 64 72 29 3b 0a 09 09 69 ddr.s_addr);...i
2230: 66 20 28 70 74 6f 6e 5f 72 65 74 20 21 3d 20 31 f (pton_ret != 1
2240: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 31 ) {....return(-1
2250: 29 3b 0a 09 09 7d 0a 0a 09 09 61 64 64 72 5f 76 );...}....addr_v
2260: 34 2e 73 69 6e 5f 66 61 6d 69 6c 79 20 3d 20 66 4.sin_family = f
2270: 61 6d 69 6c 79 3b 0a 09 09 61 64 64 72 5f 76 34 amily;...addr_v4
2280: 2e 73 69 6e 5f 70 6f 72 74 20 3d 20 68 74 6f 6e .sin_port = hton
2290: 73 28 70 6f 72 74 29 3b 0a 0a 09 09 61 64 64 72 s(port);....addr
22a0: 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 = (struct socka
22b0: 64 64 72 20 2a 29 20 26 61 64 64 72 5f 76 34 3b ddr *) &addr_v4;
22c0: 0a 09 09 61 64 64 72 5f 6c 65 6e 20 3d 20 73 69 ...addr_len = si
22d0: 7a 65 6f 66 28 61 64 64 72 5f 76 34 29 3b 0a 09 zeof(addr_v4);..
22e0: 7d 20 65 6c 73 65 20 7b 0a 09 09 61 64 64 72 5f } else {...addr_
22f0: 76 36 2e 73 69 6e 36 5f 66 61 6d 69 6c 79 20 3d v6.sin6_family =
2300: 20 41 46 5f 49 4e 45 54 36 3b 0a 09 09 61 64 64 AF_INET6;...add
2310: 72 5f 76 36 2e 73 69 6e 36 5f 66 6c 6f 77 69 6e r_v6.sin6_flowin
2320: 66 6f 20 3d 20 30 3b 0a 09 09 61 64 64 72 5f 76 fo = 0;...addr_v
2330: 36 2e 73 69 6e 36 5f 73 63 6f 70 65 5f 69 64 20 6.sin6_scope_id
2340: 3d 20 30 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 = 0;...addr_v6.s
2350: 69 6e 36 5f 70 6f 72 74 20 3d 20 68 74 6f 6e 73 in6_port = htons
2360: 28 70 6f 72 74 29 3b 0a 0a 09 09 61 64 64 72 20 (port);....addr
2370: 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 = (struct sockad
2380: 64 72 20 2a 29 20 26 61 64 64 72 5f 76 36 3b 0a dr *) &addr_v6;.
2390: 09 09 61 64 64 72 5f 6c 65 6e 20 3d 20 73 69 7a ..addr_len = siz
23a0: 65 6f 66 28 61 64 64 72 5f 76 36 29 3b 0a 09 7d eof(addr_v6);..}
23b0: 0a 0a 09 66 64 20 3d 20 73 6f 63 6b 65 74 28 66 ...fd = socket(f
23c0: 61 6d 69 6c 79 2c 20 53 4f 43 4b 5f 53 54 52 45 amily, SOCK_STRE
23d0: 41 4d 2c 20 30 29 3b 0a 09 69 66 20 28 66 64 20 AM, 0);..if (fd
23e0: 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 < 0) {...return(
23f0: 66 64 29 3b 0a 09 7d 0a 0a 09 62 69 6e 64 5f 72 fd);..}...bind_r
2400: 65 74 20 3d 20 62 69 6e 64 28 66 64 2c 20 61 64 et = bind(fd, ad
2410: 64 72 2c 20 61 64 64 72 5f 6c 65 6e 29 3b 0a 09 dr, addr_len);..
2420: 69 66 20 28 62 69 6e 64 5f 72 65 74 20 3c 20 30 if (bind_ret < 0
2430: 29 20 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b ) {...close(fd);
2440: 0a 0a 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a ....return(-1);.
2450: 09 7d 0a 0a 09 6c 69 73 74 65 6e 5f 72 65 74 20 .}...listen_ret
2460: 3d 20 6c 69 73 74 65 6e 28 66 64 2c 20 31 32 38 = listen(fd, 128
2470: 29 3b 0a 09 69 66 20 28 6c 69 73 74 65 6e 5f 72 );..if (listen_r
2480: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 63 6c 6f et != 0) {...clo
2490: 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 se(fd);....retur
24a0: 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 n(-1);..}...retu
24b0: 72 6e 28 66 64 29 3b 0a 7d 0a 0a 2f 2a 20 4c 6f rn(fd);.}../* Lo
24c0: 67 20 61 20 6d 65 73 73 61 67 65 20 2a 2f 0a 23 g a message */.#
24d0: 69 66 64 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 ifdef FILED_DONT
24e0: 5f 4c 4f 47 0a 23 20 20 64 65 66 69 6e 65 20 66 _LOG.# define f
24f0: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 iled_logging_thr
2500: 65 61 64 5f 69 6e 69 74 28 78 29 20 30 0a 23 20 ead_init(x) 0.#
2510: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f define filed_lo
2520: 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c 20 2e g_msg_debug(x, .
2530: 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 ..) /**/.# defi
2540: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ne filed_log_msg
2550: 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 (x, ...) /**/.#
2560: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f define filed_lo
2570: 67 5f 65 6e 74 72 79 28 78 29 20 2f 2a 2a 2f 0a g_entry(x) /**/.
2580: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f # define filed_
2590: 6c 6f 67 5f 69 70 28 78 2c 20 2e 2e 2e 29 20 4e log_ip(x, ...) N
25a0: 55 4c 4c 0a 23 20 20 64 65 66 69 6e 65 20 66 69 ULL.# define fi
25b0: 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 78 29 20 26 led_log_new(x) &
25c0: 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 0a local_dummy_log.
25d0: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f # define filed_
25e0: 6c 6f 67 5f 66 72 65 65 28 78 29 20 2f 2a 2a 2f log_free(x) /**/
25f0: 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 6c 6f 67 67 ../* Return logg
2600: 69 6e 67 20 68 61 6e 64 6c 65 20 2a 2f 0a 73 74 ing handle */.st
2610: 61 74 69 63 20 46 49 4c 45 20 2a 66 69 6c 65 64 atic FILE *filed
2620: 5f 6c 6f 67 5f 6f 70 65 6e 28 63 6f 6e 73 74 20 _log_open(const
2630: 63 68 61 72 20 2a 66 69 6c 65 29 20 7b 0a 09 72 char *file) {..r
2640: 65 74 75 72 6e 28 73 74 64 6f 75 74 29 3b 0a 09 eturn(stdout);..
2650: 66 69 6c 65 20 3d 20 66 69 6c 65 3b 0a 7d 0a 23 file = file;.}.#
2660: 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 66 else.# define f
2670: 69 6c 65 64 5f 6c 6f 67 5f 66 72 65 65 28 78 29 iled_log_free(x)
2680: 20 66 72 65 65 28 78 29 0a 23 20 20 69 66 64 65 free(x).# ifde
2690: 66 20 46 49 4c 45 44 5f 44 45 42 55 47 0a 23 20 f FILED_DEBUG.#
26a0: 20 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f define filed_
26b0: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c log_msg_debug(x,
26c0: 20 2e 2e 2e 29 20 7b 20 66 70 72 69 6e 74 66 28 ...) { fprintf(
26d0: 73 74 64 65 72 72 2c 20 78 2c 20 5f 5f 56 41 5f stderr, x, __VA_
26e0: 41 52 47 53 5f 5f 29 3b 20 66 70 72 69 6e 74 66 ARGS__); fprintf
26f0: 28 73 74 64 65 72 72 2c 20 22 5c 6e 22 29 3b 20 (stderr, "\n");
2700: 66 66 6c 75 73 68 28 73 74 64 65 72 72 29 3b 20 fflush(stderr);
2710: 7d 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 }.# else.# d
2720: 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f efine filed_log_
2730: 6d 73 67 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e msg_debug(x, ...
2740: 29 20 2f 2a 2a 2f 0a 23 20 20 65 6e 64 69 66 0a ) /**/.# endif.
2750: 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6c ./* Initialize l
2760: 6f 67 67 69 6e 67 20 74 68 72 65 61 64 20 2a 2f ogging thread */
2770: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 .static void *fi
2780: 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 led_logging_thre
2790: 61 64 28 76 6f 69 64 20 2a 61 72 67 5f 70 29 20 ad(void *arg_p)
27a0: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f {..struct filed_
27b0: 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 logging_thread_a
27c0: 72 67 73 20 2a 61 72 67 3b 0a 09 73 74 72 75 63 rgs *arg;..struc
27d0: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
27e0: 79 20 2a 63 75 72 72 2c 20 2a 70 72 65 76 3b 0a y *curr, *prev;.
27f0: 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 6d 65 74 .const char *met
2800: 68 6f 64 3b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 hod;..time_t now
2810: 3b 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 61 ;..FILE *fp;...a
2820: 72 67 20 3d 20 61 72 67 5f 70 3b 0a 0a 09 66 70 rg = arg_p;...fp
2830: 20 3d 20 61 72 67 2d 3e 66 70 3b 0a 0a 09 77 68 = arg->fp;...wh
2840: 69 6c 65 20 28 31 29 20 7b 0a 09 09 70 74 68 72 ile (1) {...pthr
2850: 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 ead_mutex_lock(&
2860: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
2870: 73 74 5f 6d 75 74 65 78 29 3b 0a 09 09 70 74 68 st_mutex);...pth
2880: 72 65 61 64 5f 63 6f 6e 64 5f 77 61 69 74 28 26 read_cond_wait(&
2890: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
28a0: 73 74 5f 72 65 61 64 79 2c 20 26 66 69 6c 65 64 st_ready, &filed
28b0: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 _log_msg_list_mu
28c0: 74 65 78 29 3b 0a 0a 09 09 63 75 72 72 20 3d 20 tex);....curr =
28d0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
28e0: 73 74 3b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f st;...filed_log_
28f0: 6d 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b msg_list = NULL;
2900: 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 ....pthread_mute
2910: 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f x_unlock(&filed_
2920: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 log_msg_list_mut
2930: 65 78 29 3b 0a 0a 09 09 6e 6f 77 20 3d 20 74 69 ex);....now = ti
2940: 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 09 70 72 65 me(NULL);....pre
2950: 76 20 3d 20 4e 55 4c 4c 3b 0a 09 09 66 6f 72 20 v = NULL;...for
2960: 28 3b 20 63 75 72 72 3b 20 63 75 72 72 20 3d 20 (; curr; curr =
2970: 63 75 72 72 2d 3e 5f 6e 65 78 74 29 20 7b 0a 09 curr->_next) {..
2980: 09 09 63 75 72 72 2d 3e 5f 70 72 65 76 20 3d 20 ..curr->_prev =
2990: 70 72 65 76 3b 0a 0a 09 09 09 70 72 65 76 20 3d prev;.....prev =
29a0: 20 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 63 75 curr;...}....cu
29b0: 72 72 20 3d 20 70 72 65 76 3b 0a 09 09 77 68 69 rr = prev;...whi
29c0: 6c 65 20 28 63 75 72 72 29 20 7b 0a 09 09 09 73 le (curr) {....s
29d0: 77 69 74 63 68 20 28 63 75 72 72 2d 3e 74 79 70 witch (curr->typ
29e0: 65 29 20 7b 0a 09 09 09 09 63 61 73 65 20 46 49 e) {.....case FI
29f0: 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 LED_LOG_TYPE_MES
2a00: 53 41 47 45 3a 0a 09 09 09 09 09 66 70 72 69 6e SAGE:......fprin
2a10: 74 66 28 66 70 2c 20 22 25 73 22 2c 20 63 75 72 tf(fp, "%s", cur
2a20: 72 2d 3e 62 75 66 66 65 72 29 3b 0a 0a 09 09 09 r->buffer);.....
2a30: 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 63 61 73 ..break;.....cas
2a40: 65 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 e FILED_LOG_TYPE
2a50: 5f 54 52 41 4e 53 46 45 52 3a 0a 09 09 09 09 09 _TRANSFER:......
2a60: 73 77 69 74 63 68 20 28 63 75 72 72 2d 3e 6d 65 switch (curr->me
2a70: 74 68 6f 64 29 20 7b 0a 09 09 09 09 09 09 63 61 thod) {.......ca
2a80: 73 65 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 se FILED_REQUEST
2a90: 5f 4d 45 54 48 4f 44 5f 47 45 54 3a 0a 09 09 09 _METHOD_GET:....
2aa0: 09 09 09 09 6d 65 74 68 6f 64 3d 22 47 45 54 22 ....method="GET"
2ab0: 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a ;........break;.
2ac0: 09 09 09 09 09 09 63 61 73 65 20 46 49 4c 45 44 ......case FILED
2ad0: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f _REQUEST_METHOD_
2ae0: 48 45 41 44 3a 0a 09 09 09 09 09 09 09 6d 65 74 HEAD:........met
2af0: 68 6f 64 3d 22 48 45 41 44 22 3b 0a 09 09 09 09 hod="HEAD";.....
2b00: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 ...break;.......
2b10: 64 65 66 61 75 6c 74 3a 0a 09 09 09 09 09 09 09 default:........
2b20: 6d 65 74 68 6f 64 3d 22 3c 75 6e 6b 6e 6f 77 6e method="<unknown
2b30: 3e 22 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b >";........break
2b40: 3b 0a 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 69 ;......}.......i
2b50: 66 20 28 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 f (curr->endtime
2b60: 20 3d 3d 20 28 28 74 69 6d 65 5f 74 29 20 2d 31 == ((time_t) -1
2b70: 29 29 20 7b 0a 09 09 09 09 09 09 63 75 72 72 2d )) {.......curr-
2b80: 3e 65 6e 64 74 69 6d 65 20 3d 20 6e 6f 77 3b 0a >endtime = now;.
2b90: 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 66 70 72 .....}.......fpr
2ba0: 69 6e 74 66 28 66 70 2c 20 22 54 52 41 4e 53 46 intf(fp, "TRANSF
2bb0: 45 52 20 4d 45 54 48 4f 44 3d 25 73 20 50 41 54 ER METHOD=%s PAT
2bc0: 48 3d 25 73 20 53 52 43 3d 25 73 3a 25 69 20 54 H=%s SRC=%s:%i T
2bd0: 49 4d 45 2e 53 54 41 52 54 3d 25 6c 6c 75 20 54 IME.START=%llu T
2be0: 49 4d 45 2e 45 4e 44 3d 25 6c 6c 75 20 43 4f 44 IME.END=%llu COD
2bf0: 45 2e 56 41 4c 55 45 3d 25 75 20 43 4f 44 45 2e E.VALUE=%u CODE.
2c00: 52 45 41 53 4f 4e 3d 25 73 20 52 45 51 55 45 53 REASON=%s REQUES
2c10: 54 2e 4f 46 46 53 45 54 3d 25 6c 6c 75 20 52 45 T.OFFSET=%llu RE
2c20: 51 55 45 53 54 2e 4c 45 4e 47 54 48 3d 25 6c 6c QUEST.LENGTH=%ll
2c30: 75 20 46 49 4c 45 2e 4c 45 4e 47 54 48 3d 25 6c u FILE.LENGTH=%l
2c40: 6c 75 20 54 52 41 4e 53 46 45 52 2e 4c 45 4e 47 lu TRANSFER.LENG
2c50: 54 48 3d 25 6c 6c 75 22 2c 0a 09 09 09 09 09 09 TH=%llu",.......
2c60: 6d 65 74 68 6f 64 2c 0a 09 09 09 09 09 09 63 75 method,.......cu
2c70: 72 72 2d 3e 62 75 66 66 65 72 2c 0a 09 09 09 09 rr->buffer,.....
2c80: 09 09 63 75 72 72 2d 3e 69 70 2c 20 63 75 72 72 ..curr->ip, curr
2c90: 2d 3e 70 6f 72 74 2c 0a 09 09 09 09 09 09 28 75 ->port,.......(u
2ca0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
2cb0: 67 29 20 63 75 72 72 2d 3e 73 74 61 72 74 74 69 g) curr->startti
2cc0: 6d 65 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 me,.......(unsig
2cd0: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 ned long long) c
2ce0: 75 72 72 2d 3e 65 6e 64 74 69 6d 65 2c 0a 09 09 urr->endtime,...
2cf0: 09 09 09 09 63 75 72 72 2d 3e 68 74 74 70 5f 63 ....curr->http_c
2d00: 6f 64 65 2c 20 63 75 72 72 2d 3e 72 65 61 73 6f ode, curr->reaso
2d10: 6e 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e n,.......(unsign
2d20: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 ed long long) cu
2d30: 72 72 2d 3e 72 65 71 5f 6f 66 66 73 65 74 2c 0a rr->req_offset,.
2d40: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ......(unsigned
2d50: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d long long) curr-
2d60: 3e 72 65 71 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 >req_length,....
2d70: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
2d80: 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 66 69 g long) curr->fi
2d90: 6c 65 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09 le_length,......
2da0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
2db0: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 73 65 6e 74 long) curr->sent
2dc0: 5f 6c 65 6e 67 74 68 0a 09 09 09 09 09 29 3b 0a _length......);.
2dd0: 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 ......break;....
2de0: 7d 0a 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c }....fprintf(fp,
2df0: 20 22 20 54 48 52 45 41 44 3d 25 6c 6c 75 20 54 " THREAD=%llu T
2e00: 49 4d 45 3d 25 6c 6c 75 5c 6e 22 2c 0a 09 09 09 IME=%llu\n",....
2e10: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
2e20: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 74 68 72 65 long) curr->thre
2e30: 61 64 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 ad,.....(unsigne
2e40: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 6e 6f 77 d long long) now
2e50: 0a 09 09 09 29 3b 0a 09 09 09 66 66 6c 75 73 68 ....);....fflush
2e60: 28 66 70 29 3b 0a 0a 09 09 09 70 72 65 76 20 3d (fp);.....prev =
2e70: 20 63 75 72 72 3b 0a 09 09 09 63 75 72 72 20 3d curr;....curr =
2e80: 20 63 75 72 72 2d 3e 5f 70 72 65 76 3b 0a 0a 09 curr->_prev;...
2e90: 09 09 66 72 65 65 28 70 72 65 76 29 3b 0a 09 09 ..free(prev);...
2ea0: 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 }..}...return(NU
2eb0: 4c 4c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 LL);.}..static v
2ec0: 6f 69 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e oid filed_log_en
2ed0: 74 72 79 28 73 74 72 75 63 74 20 66 69 6c 65 64 try(struct filed
2ee0: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 _log_entry *entr
2ef0: 79 29 20 7b 0a 09 65 6e 74 72 79 2d 3e 74 68 72 y) {..entry->thr
2f00: 65 61 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 ead = pthread_se
2f10: 6c 66 28 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f lf();...pthread_
2f20: 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 mutex_lock(&file
2f30: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d d_log_msg_list_m
2f40: 75 74 65 78 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e utex);...entry->
2f50: 5f 6e 65 78 74 20 3d 20 66 69 6c 65 64 5f 6c 6f _next = filed_lo
2f60: 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a 09 66 69 6c g_msg_list;..fil
2f70: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 ed_log_msg_list
2f80: 3d 20 65 6e 74 72 79 3b 0a 0a 09 70 74 68 72 65 = entry;...pthre
2f90: 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 ad_mutex_unlock(
2fa0: 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c &filed_log_msg_l
2fb0: 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 70 74 ist_mutex);...pt
2fc0: 68 72 65 61 64 5f 63 6f 6e 64 5f 73 69 67 6e 61 hread_cond_signa
2fd0: 6c 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 l(&filed_log_msg
2fe0: 5f 6c 69 73 74 5f 72 65 61 64 79 29 3b 0a 0a 09 _list_ready);...
2ff0: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 return;.}..stati
3000: 63 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c c struct filed_l
3010: 6f 67 5f 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f og_entry *filed_
3020: 6c 6f 67 5f 6e 65 77 28 69 6e 74 20 69 6e 69 74 log_new(int init
3030: 69 61 6c 69 7a 65 29 20 7b 0a 09 73 74 72 75 63 ialize) {..struc
3040: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
3050: 79 20 2a 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 y *retval;...ret
3060: 76 61 6c 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a val = malloc(siz
3070: 65 6f 66 28 2a 72 65 74 76 61 6c 29 29 3b 0a 0a eof(*retval));..
3080: 09 69 66 20 28 69 6e 69 74 69 61 6c 69 7a 65 29 .if (initialize)
3090: 20 7b 0a 09 09 72 65 74 76 61 6c 2d 3e 62 75 66 {...retval->buf
30a0: 66 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 fer[0] = '\0';..
30b0: 09 72 65 74 76 61 6c 2d 3e 68 74 74 70 5f 63 6f .retval->http_co
30c0: 64 65 20 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 de = -1;...retva
30d0: 6c 2d 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 30 l->starttime = 0
30e0: 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 65 6e 64 74 ;...retval->endt
30f0: 69 6d 65 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 ime = 0;...retva
3100: 6c 2d 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 l->req_offset =
3110: 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 0;...retval->req
3120: 5f 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 _length = 0;...r
3130: 65 74 76 61 6c 2d 3e 73 65 6e 74 5f 6c 65 6e 67 etval->sent_leng
3140: 74 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c th = 0;...retval
3150: 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 ->file_length =
3160: 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 69 70 5b 0;...retval->ip[
3170: 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 72 65 74 0] = '\0';...ret
3180: 76 61 6c 2d 3e 70 6f 72 74 20 3d 20 2d 31 3b 0a val->port = -1;.
3190: 09 09 72 65 74 76 61 6c 2d 3e 6d 65 74 68 6f 64 ..retval->method
31a0: 20 3d 20 2d 31 3b 0a 09 7d 0a 0a 09 72 65 74 75 = -1;..}...retu
31b0: 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 rn(retval);.}..s
31c0: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 tatic void filed
31d0: 5f 6c 6f 67 5f 6d 73 67 28 63 6f 6e 73 74 20 63 _log_msg(const c
31e0: 68 61 72 20 2a 66 6d 74 2c 20 2e 2e 2e 29 20 7b har *fmt, ...) {
31f0: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c ..struct filed_l
3200: 6f 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 3b og_entry *entry;
3210: 0a 09 76 61 5f 6c 69 73 74 20 61 72 67 73 3b 0a ..va_list args;.
3220: 0a 09 65 6e 74 72 79 20 3d 20 66 69 6c 65 64 5f ..entry = filed_
3230: 6c 6f 67 5f 6e 65 77 28 30 29 3b 0a 0a 09 76 61 log_new(0);...va
3240: 5f 73 74 61 72 74 28 61 72 67 73 2c 20 66 6d 74 _start(args, fmt
3250: 29 3b 0a 0a 09 76 73 6e 70 72 69 6e 74 66 28 65 );...vsnprintf(e
3260: 6e 74 72 79 2d 3e 62 75 66 66 65 72 2c 20 73 69 ntry->buffer, si
3270: 7a 65 6f 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 zeof(entry->buff
3280: 65 72 29 2c 20 66 6d 74 2c 20 61 72 67 73 29 3b er), fmt, args);
3290: 0a 0a 09 76 61 5f 65 6e 64 28 61 72 67 73 29 3b ...va_end(args);
32a0: 0a 0a 09 65 6e 74 72 79 2d 3e 74 79 70 65 20 3d ...entry->type =
32b0: 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f FILED_LOG_TYPE_
32c0: 4d 45 53 53 41 47 45 3b 0a 0a 09 66 69 6c 65 64 MESSAGE;...filed
32d0: 5f 6c 6f 67 5f 65 6e 74 72 79 28 65 6e 74 72 79 _log_entry(entry
32e0: 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a );...return;.}..
32f0: 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
3300: 72 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 r *filed_log_ip(
3310: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 struct sockaddr
3320: 2a 61 64 64 72 2c 20 63 68 61 72 20 2a 62 75 66 *addr, char *buf
3330: 66 65 72 2c 20 73 69 7a 65 5f 74 20 62 75 66 66 fer, size_t buff
3340: 65 72 6c 65 6e 29 20 7b 0a 09 73 74 72 75 63 74 erlen) {..struct
3350: 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 2a 61 64 sockaddr_in *ad
3360: 64 72 5f 76 34 3b 0a 09 73 74 72 75 63 74 20 73 dr_v4;..struct s
3370: 6f 63 6b 61 64 64 72 5f 69 6e 36 20 2a 61 64 64 ockaddr_in6 *add
3380: 72 5f 76 36 3b 0a 09 63 6f 6e 73 74 20 63 68 61 r_v6;..const cha
3390: 72 20 2a 72 65 74 76 61 6c 20 3d 20 4e 55 4c 4c r *retval = NULL
33a0: 3b 0a 0a 09 61 64 64 72 5f 76 36 20 3d 20 28 73 ;...addr_v6 = (s
33b0: 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 truct sockaddr_i
33c0: 6e 36 20 2a 29 20 61 64 64 72 3b 0a 0a 09 73 77 n6 *) addr;...sw
33d0: 69 74 63 68 20 28 61 64 64 72 5f 76 36 2d 3e 73 itch (addr_v6->s
33e0: 69 6e 36 5f 66 61 6d 69 6c 79 29 20 7b 0a 09 09 in6_family) {...
33f0: 63 61 73 65 20 41 46 5f 49 4e 45 54 3a 0a 09 09 case AF_INET:...
3400: 09 61 64 64 72 5f 76 34 20 3d 20 28 73 74 72 75 .addr_v4 = (stru
3410: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 2a ct sockaddr_in *
3420: 29 20 61 64 64 72 3b 0a 09 09 09 72 65 74 76 61 ) addr;....retva
3430: 6c 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 41 46 l = inet_ntop(AF
3440: 5f 49 4e 45 54 2c 20 26 61 64 64 72 5f 76 34 2d _INET, &addr_v4-
3450: 3e 73 69 6e 5f 61 64 64 72 2c 20 62 75 66 66 65 >sin_addr, buffe
3460: 72 2c 20 62 75 66 66 65 72 6c 65 6e 29 3b 0a 09 r, bufferlen);..
3470: 09 09 62 72 65 61 6b 3b 0a 09 09 63 61 73 65 20 ..break;...case
3480: 41 46 5f 49 4e 45 54 36 3a 0a 09 09 09 72 65 74 AF_INET6:....ret
3490: 76 61 6c 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 val = inet_ntop(
34a0: 41 46 5f 49 4e 45 54 36 2c 20 26 61 64 64 72 5f AF_INET6, &addr_
34b0: 76 36 2d 3e 73 69 6e 36 5f 61 64 64 72 2c 20 62 v6->sin6_addr, b
34c0: 75 66 66 65 72 2c 20 62 75 66 66 65 72 6c 65 6e uffer, bufferlen
34d0: 29 3b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a );....break;..}.
34e0: 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 ..return(retval)
34f0: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 46 49 4c 45 ;.}..static FILE
3500: 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e *filed_log_open
3510: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c (const char *fil
3520: 65 29 20 7b 0a 09 46 49 4c 45 20 2a 72 65 74 76 e) {..FILE *retv
3530: 61 6c 3b 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 al;...if (strcmp
3540: 28 66 69 6c 65 2c 20 22 2d 22 29 20 3d 3d 20 30 (file, "-") == 0
3550: 29 20 7b 0a 09 09 72 65 74 76 61 6c 20 3d 20 73 ) {...retval = s
3560: 74 64 6f 75 74 3b 0a 09 7d 20 65 6c 73 65 20 69 tdout;..} else i
3570: 66 20 28 66 69 6c 65 5b 30 5d 20 3d 3d 20 27 7c f (file[0] == '|
3580: 27 29 20 7b 0a 09 09 66 69 6c 65 2b 2b 3b 0a 09 ') {...file++;..
3590: 09 72 65 74 76 61 6c 20 3d 20 70 6f 70 65 6e 28 .retval = popen(
35a0: 66 69 6c 65 2c 20 22 77 22 29 3b 0a 09 7d 20 65 file, "w");..} e
35b0: 6c 73 65 20 7b 0a 09 09 72 65 74 76 61 6c 20 3d lse {...retval =
35c0: 20 66 6f 70 65 6e 28 66 69 6c 65 2c 20 22 61 2b fopen(file, "a+
35d0: 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 ");..}...return(
35e0: 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 retval);.}..stat
35f0: 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 6c 6f 67 ic int filed_log
3600: 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 ging_thread_init
3610: 28 46 49 4c 45 20 2a 6c 6f 67 66 70 29 20 7b 0a (FILE *logfp) {.
3620: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f .struct filed_lo
3630: 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 gging_thread_arg
3640: 73 20 2a 61 72 67 73 3b 0a 09 70 74 68 72 65 61 s *args;..pthrea
3650: 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b 0a 0a d_t thread_id;..
3660: 09 61 72 67 73 20 3d 20 6d 61 6c 6c 6f 63 28 73 .args = malloc(s
3670: 69 7a 65 6f 66 28 2a 61 72 67 73 29 29 3b 0a 09 izeof(*args));..
3680: 61 72 67 73 2d 3e 66 70 20 3d 20 6c 6f 67 66 70 args->fp = logfp
3690: 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 ;...filed_log_ms
36a0: 67 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a 0a g_list = NULL;..
36b0: 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 .pthread_mutex_i
36c0: 6e 69 74 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d nit(&filed_log_m
36d0: 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 2c 20 4e sg_list_mutex, N
36e0: 55 4c 4c 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f ULL);...pthread_
36f0: 63 72 65 61 74 65 28 26 74 68 72 65 61 64 5f 69 create(&thread_i
3700: 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 6c d, NULL, filed_l
3710: 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 2c 20 61 ogging_thread, a
3720: 72 67 73 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f rgs);...filed_lo
3730: 67 5f 6d 73 67 28 22 53 54 41 52 54 22 29 3b 0a g_msg("START");.
3740: 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 23 ..return(0);.}.#
3750: 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 46 49 endif..#ifdef FI
3760: 4c 45 44 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55 54 LED_DONT_TIMEOUT
3770: 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 .#define filed_s
3780: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 ockettimeout_thr
3790: 65 61 64 5f 69 6e 69 74 28 29 20 30 0a 23 64 65 ead_init() 0.#de
37a0: 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 fine filed_socke
37b0: 74 74 69 6d 65 6f 75 74 5f 61 63 63 65 70 74 28 ttimeout_accept(
37c0: 78 29 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 x) /**/.#define
37d0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
37e0: 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 73 out_processing_s
37f0: 74 61 72 74 28 78 29 20 2f 2a 2a 2f 0a 23 64 65 tart(x) /**/.#de
3800: 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 fine filed_socke
3810: 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 ttimeout_process
3820: 69 6e 67 5f 65 6e 64 28 78 29 20 2f 2a 2a 2f 0a ing_end(x) /**/.
3830: 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f #define filed_so
3840: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 ckettimeout_clos
3850: 65 28 78 29 20 2f 2a 2a 2f 0a 23 65 6c 73 65 0a e(x) /**/.#else.
3860: 5f 41 74 6f 6d 69 63 20 74 69 6d 65 5f 74 20 66 _Atomic time_t f
3870: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
3880: 75 74 5f 74 69 6d 65 3b 0a 73 74 72 75 63 74 20 ut_time;.struct
3890: 7b 0a 09 5f 41 74 6f 6d 69 63 20 74 69 6d 65 5f {.._Atomic time_
38a0: 74 20 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d t expiration_tim
38b0: 65 3b 0a 09 5f 41 74 6f 6d 69 63 20 70 74 68 72 e;.._Atomic pthr
38c0: 65 61 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b ead_t thread_id;
38d0: 0a 09 62 6f 6f 6c 20 76 61 6c 69 64 3b 0a 7d 2a ..bool valid;.}*
38e0: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d filed_sockettim
38f0: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 3b eout_sockstatus;
3900: 0a 6c 6f 6e 67 20 66 69 6c 65 64 5f 73 6f 63 6b .long filed_sock
3910: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 ettimeout_sockst
3920: 61 74 75 73 5f 6c 65 6e 67 74 68 3b 0a 69 6e 74 atus_length;.int
3930: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d filed_sockettim
3940: 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 3b eout_devnull_fd;
3950: 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c ..static int fil
3960: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
3970: 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 _sockfd_in_range
3980: 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 (int sockfd) {..
3990: 69 66 20 28 73 6f 63 6b 66 64 20 3c 20 33 29 20 if (sockfd < 3)
39a0: 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 {...return(0);..
39b0: 7d 0a 0a 09 69 66 20 28 73 6f 63 6b 66 64 20 3e }...if (sockfd >
39c0: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d filed_sockettim
39d0: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f eout_sockstatus_
39e0: 6c 65 6e 67 74 68 29 20 7b 0a 09 09 72 65 74 75 length) {...retu
39f0: 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 rn(0);..}...retu
3a00: 72 6e 28 31 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 rn(1);.}..static
3a10: 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b void filed_sock
3a20: 65 74 74 69 6d 65 6f 75 74 5f 65 78 70 69 72 65 ettimeout_expire
3a30: 28 69 6e 74 20 73 6f 63 6b 66 64 2c 20 69 6e 74 (int sockfd, int
3a40: 20 6c 65 6e 67 74 68 29 20 7b 0a 09 74 69 6d 65 length) {..time
3a50: 5f 74 20 6e 6f 77 2c 20 65 78 70 69 72 65 3b 0a _t now, expire;.
3a60: 0a 09 6e 6f 77 20 3d 20 61 74 6f 6d 69 63 5f 6c ..now = atomic_l
3a70: 6f 61 64 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 oad(&filed_socke
3a80: 74 74 69 6d 65 6f 75 74 5f 74 69 6d 65 29 3b 0a ttimeout_time);.
3a90: 0a 09 65 78 70 69 72 65 20 3d 20 6e 6f 77 20 2b ..expire = now +
3aa0: 20 6c 65 6e 67 74 68 3b 0a 0a 09 61 74 6f 6d 69 length;...atomi
3ab0: 63 5f 73 74 6f 72 65 28 26 66 69 6c 65 64 5f 73 c_store(&filed_s
3ac0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
3ad0: 6b 73 74 61 74 75 73 5b 73 6f 63 6b 66 64 5d 2e kstatus[sockfd].
3ae0: 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 2c expiration_time,
3af0: 20 65 78 70 69 72 65 29 3b 0a 0a 09 72 65 74 75 expire);...retu
3b00: 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f rn;.}..static vo
3b10: 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 id filed_sockett
3b20: 69 6d 65 6f 75 74 5f 61 63 63 65 70 74 28 69 6e imeout_accept(in
3b30: 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 t sockfd) {..if
3b40: 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 (!filed_socketti
3b50: 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f meout_sockfd_in_
3b60: 72 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b range(sockfd)) {
3b70: 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 ...return;..}...
3b80: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
3b90: 6f 75 74 5f 65 78 70 69 72 65 28 73 6f 63 6b 66 out_expire(sockf
3ba0: 64 2c 20 36 30 29 3b 0a 0a 09 61 74 6f 6d 69 63 d, 60);...atomic
3bb0: 5f 73 74 6f 72 65 28 26 66 69 6c 65 64 5f 73 6f _store(&filed_so
3bc0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b ckettimeout_sock
3bd0: 73 74 61 74 75 73 5b 73 6f 63 6b 66 64 5d 2e 74 status[sockfd].t
3be0: 68 72 65 61 64 5f 69 64 2c 20 70 74 68 72 65 61 hread_id, pthrea
3bf0: 64 5f 73 65 6c 66 28 29 29 3b 0a 0a 09 61 74 6f d_self());...ato
3c00: 6d 69 63 5f 73 74 6f 72 65 28 26 66 69 6c 65 64 mic_store(&filed
3c10: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
3c20: 6f 63 6b 73 74 61 74 75 73 5b 73 6f 63 6b 66 64 ockstatus[sockfd
3c30: 5d 2e 76 61 6c 69 64 2c 20 74 72 75 65 29 3b 0a ].valid, true);.
3c40: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 ..return;.}..sta
3c50: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 tic void filed_s
3c60: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f ockettimeout_pro
3c70: 63 65 73 73 69 6e 67 5f 73 74 61 72 74 28 69 6e cessing_start(in
3c80: 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 t sockfd) {..if
3c90: 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 (!filed_socketti
3ca0: 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f meout_sockfd_in_
3cb0: 72 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b range(sockfd)) {
3cc0: 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 ...return;..}...
3cd0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
3ce0: 6f 75 74 5f 65 78 70 69 72 65 28 73 6f 63 6b 66 out_expire(sockf
3cf0: 64 2c 20 38 36 34 30 30 29 3b 0a 0a 09 72 65 74 d, 86400);...ret
3d00: 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 urn;.}..static v
3d10: 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 oid filed_socket
3d20: 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 timeout_processi
3d30: 6e 67 5f 65 6e 64 28 69 6e 74 20 73 6f 63 6b 66 ng_end(int sockf
3d40: 64 29 20 7b 0a 09 69 66 20 28 21 66 69 6c 65 64 d) {..if (!filed
3d50: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
3d60: 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28 73 ockfd_in_range(s
3d70: 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 65 74 75 ockfd)) {...retu
3d80: 72 6e 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 rn;..}...filed_s
3d90: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78 70 ockettimeout_exp
3da0: 69 72 65 28 73 6f 63 6b 66 64 2c 20 36 30 29 3b ire(sockfd, 60);
3db0: 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 ...return;.}..st
3dc0: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f atic void filed_
3dd0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c sockettimeout_cl
3de0: 6f 73 65 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 ose(int sockfd)
3df0: 7b 0a 09 69 66 20 28 21 66 69 6c 65 64 5f 73 6f {..if (!filed_so
3e00: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b ckettimeout_sock
3e10: 66 64 5f 69 6e 5f 72 61 6e 67 65 28 73 6f 63 6b fd_in_range(sock
3e20: 66 64 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 3b fd)) {...return;
3e30: 0a 09 7d 0a 0a 09 61 74 6f 6d 69 63 5f 73 74 6f ..}...atomic_sto
3e40: 72 65 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 re(&filed_socket
3e50: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
3e60: 75 73 5b 73 6f 63 6b 66 64 5d 2e 76 61 6c 69 64 us[sockfd].valid
3e70: 2c 20 66 61 6c 73 65 29 3b 0a 0a 09 72 65 74 75 , false);...retu
3e80: 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f rn;.}..static vo
3e90: 69 64 20 2a 66 69 6c 65 64 5f 73 6f 63 6b 65 74 id *filed_socket
3ea0: 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 28 76 timeout_thread(v
3eb0: 6f 69 64 20 2a 61 72 67 29 20 7b 0a 09 74 69 6d oid *arg) {..tim
3ec0: 65 5f 74 20 6e 6f 77 2c 20 65 78 70 69 72 61 74 e_t now, expirat
3ed0: 69 6f 6e 5f 74 69 6d 65 3b 0a 09 70 74 68 72 65 ion_time;..pthre
3ee0: 61 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b 0a ad_t thread_id;.
3ef0: 09 6c 6f 6e 67 20 69 64 78 3b 0a 09 69 6e 74 20 .long idx;..int
3f00: 63 6f 75 6e 74 3b 0a 09 62 6f 6f 6c 20 76 61 6c count;..bool val
3f10: 69 64 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 id;...while (1)
3f20: 7b 0a 09 09 66 6f 72 20 28 63 6f 75 6e 74 20 3d {...for (count =
3f30: 20 30 3b 20 63 6f 75 6e 74 20 3c 20 31 30 3b 20 0; count < 10;
3f40: 63 6f 75 6e 74 2b 2b 29 20 7b 0a 09 09 09 75 73 count++) {....us
3f50: 6c 65 65 70 28 33 30 30 30 30 30 30 30 29 3b 0a leep(30000000);.
3f60: 0a 09 09 09 6e 6f 77 20 3d 20 74 69 6d 65 28 4e ....now = time(N
3f70: 55 4c 4c 29 3b 0a 0a 09 09 09 61 74 6f 6d 69 63 ULL);.....atomic
3f80: 5f 73 74 6f 72 65 28 26 66 69 6c 65 64 5f 73 6f _store(&filed_so
3f90: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 69 6d 65 ckettimeout_time
3fa0: 2c 20 6e 6f 77 29 3b 0a 09 09 7d 0a 0a 09 09 66 , now);...}....f
3fb0: 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 69 64 78 or (idx = 0; idx
3fc0: 20 3c 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 < filed_sockett
3fd0: 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 imeout_sockstatu
3fe0: 73 5f 6c 65 6e 67 74 68 3b 20 69 64 78 2b 2b 29 s_length; idx++)
3ff0: 20 7b 0a 09 09 09 76 61 6c 69 64 20 3d 20 61 74 {....valid = at
4000: 6f 6d 69 63 5f 6c 6f 61 64 28 26 66 69 6c 65 64 omic_load(&filed
4010: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
4020: 6f 63 6b 73 74 61 74 75 73 5b 69 64 78 5d 2e 76 ockstatus[idx].v
4030: 61 6c 69 64 29 3b 0a 0a 09 09 09 69 66 20 28 21 alid);.....if (!
4040: 76 61 6c 69 64 29 20 7b 0a 09 09 09 09 63 6f 6e valid) {.....con
4050: 74 69 6e 75 65 3b 0a 09 09 09 7d 0a 0a 09 09 09 tinue;....}.....
4060: 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 20 expiration_time
4070: 3d 20 61 74 6f 6d 69 63 5f 6c 6f 61 64 28 26 66 = atomic_load(&f
4080: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
4090: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 69 64 ut_sockstatus[id
40a0: 78 5d 2e 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 x].expiration_ti
40b0: 6d 65 29 3b 0a 0a 09 09 09 74 68 72 65 61 64 5f me);.....thread_
40c0: 69 64 20 3d 20 61 74 6f 6d 69 63 5f 6c 6f 61 64 id = atomic_load
40d0: 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 (&filed_socketti
40e0: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 meout_sockstatus
40f0: 5b 69 64 78 5d 2e 74 68 72 65 61 64 5f 69 64 29 [idx].thread_id)
4100: 3b 0a 0a 09 09 09 69 66 20 28 65 78 70 69 72 61 ;.....if (expira
4110: 74 69 6f 6e 5f 74 69 6d 65 20 3e 20 6e 6f 77 29 tion_time > now)
4120: 20 7b 0a 09 09 09 09 63 6f 6e 74 69 6e 75 65 3b {.....continue;
4130: 0a 09 09 09 7d 0a 0a 09 09 09 66 69 6c 65 64 5f ....}.....filed_
4140: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c sockettimeout_cl
4150: 6f 73 65 28 69 64 78 29 3b 0a 0a 09 09 09 64 75 ose(idx);.....du
4160: 70 32 28 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 p2(filed_sockett
4170: 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 imeout_devnull_f
4180: 64 2c 20 69 64 78 29 3b 0a 0a 09 09 09 70 74 68 d, idx);.....pth
4190: 72 65 61 64 5f 6b 69 6c 6c 28 74 68 72 65 61 64 read_kill(thread
41a0: 5f 69 64 2c 20 53 49 47 50 49 50 45 29 3b 0a 09 _id, SIGPIPE);..
41b0: 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 4e .}..}...return(N
41c0: 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 4e 4f 54 52 45 ULL);.../* NOTRE
41d0: 41 43 48 3a 20 57 65 20 64 6f 6e 27 74 20 61 63 ACH: We don't ac
41e0: 74 75 61 6c 6c 79 20 74 61 6b 65 20 61 6e 79 20 tually take any
41f0: 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 72 arguments */..ar
4200: 67 20 3d 20 61 72 67 3b 0a 7d 0a 0a 73 74 61 74 g = arg;.}..stat
4210: 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 ic int filed_soc
4220: 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 kettimeout_threa
4230: 64 5f 69 6e 69 74 28 76 6f 69 64 29 20 7b 0a 09 d_init(void) {..
4240: 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 pthread_t thread
4250: 5f 69 64 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 _id;...pthread_c
4260: 72 65 61 74 65 28 26 74 68 72 65 61 64 5f 69 64 reate(&thread_id
4270: 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 73 6f , NULL, filed_so
4280: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 ckettimeout_thre
4290: 61 64 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 72 65 74 ad, NULL);...ret
42a0: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 73 74 61 74 69 urn(0);.}..stati
42b0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b c int filed_sock
42c0: 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69 74 28 76 ettimeout_init(v
42d0: 6f 69 64 29 20 7b 0a 09 6c 6f 6e 67 20 6d 61 78 oid) {..long max
42e0: 66 64 2c 20 69 64 78 3b 0a 0a 09 6d 61 78 66 64 fd, idx;...maxfd
42f0: 20 3d 20 73 79 73 63 6f 6e 66 28 5f 53 43 5f 4f = sysconf(_SC_O
4300: 50 45 4e 5f 4d 41 58 29 3b 0a 09 69 66 20 28 6d PEN_MAX);..if (m
4310: 61 78 66 64 20 3c 3d 20 30 29 20 7b 0a 09 09 6d axfd <= 0) {...m
4320: 61 78 66 64 20 3d 20 34 30 39 36 3b 0a 09 7d 0a axfd = 4096;..}.
4330: 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
4340: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 meout_sockstatus
4350: 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 = malloc(sizeof
4360: 28 2a 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 (*filed_socketti
4370: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 meout_sockstatus
4380: 29 20 2a 20 6d 61 78 66 64 29 3b 0a 09 69 66 20 ) * maxfd);..if
4390: 28 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d (filed_sockettim
43a0: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 20 eout_sockstatus
43b0: 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 == NULL) {...ret
43c0: 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 66 6f urn(-1);..}...fo
43d0: 72 20 28 69 64 78 20 3d 20 30 3b 20 69 64 78 20 r (idx = 0; idx
43e0: 3c 20 6d 61 78 66 64 3b 20 69 64 78 2b 2b 29 20 < maxfd; idx++)
43f0: 7b 0a 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 {...filed_socket
4400: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
4410: 75 73 5b 69 64 78 5d 2e 76 61 6c 69 64 20 3d 20 us[idx].valid =
4420: 66 61 6c 73 65 3b 0a 09 7d 0a 0a 09 66 69 6c 65 false;..}...file
4430: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
4440: 73 6f 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 sockstatus_lengt
4450: 68 20 3d 20 6d 61 78 66 64 3b 0a 09 66 69 6c 65 h = maxfd;..file
4460: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
4470: 64 65 76 6e 75 6c 6c 5f 66 64 20 3d 20 6f 70 65 devnull_fd = ope
4480: 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f n("/dev/null", O
4490: 5f 52 44 57 52 29 3b 0a 09 69 66 20 28 66 69 6c _RDWR);..if (fil
44a0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
44b0: 5f 64 65 76 6e 75 6c 6c 5f 66 64 20 3c 20 30 29 _devnull_fd < 0)
44c0: 20 7b 0a 09 09 72 65 74 75 72 6e 28 2d 31 29 3b {...return(-1);
44d0: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b ..}...return(0);
44e0: 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f .}.#endif../* Fo
44f0: 72 6d 61 74 20 74 69 6d 65 20 70 65 72 20 52 46 rmat time per RF
4500: 43 32 36 31 36 20 2a 2f 0a 73 74 61 74 69 63 20 C2616 */.static
4510: 63 68 61 72 20 2a 66 69 6c 65 64 5f 66 6f 72 6d char *filed_form
4520: 61 74 5f 74 69 6d 65 28 63 68 61 72 20 2a 62 75 at_time(char *bu
4530: 66 66 65 72 2c 20 73 69 7a 65 5f 74 20 62 75 66 ffer, size_t buf
4540: 66 65 72 5f 6c 65 6e 2c 20 63 6f 6e 73 74 20 74 fer_len, const t
4550: 69 6d 65 5f 74 20 74 69 6d 65 69 6e 66 6f 29 20 ime_t timeinfo)
4560: 7b 0a 09 73 74 72 75 63 74 20 74 6d 20 74 69 6d {..struct tm tim
4570: 65 69 6e 66 6f 5f 74 6d 2c 20 2a 74 69 6d 65 69 einfo_tm, *timei
4580: 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a 09 74 69 6d 65 nfo_tm_p;...time
4590: 69 6e 66 6f 5f 74 6d 5f 70 20 3d 20 67 6d 74 69 info_tm_p = gmti
45a0: 6d 65 5f 72 28 26 74 69 6d 65 69 6e 66 6f 2c 20 me_r(&timeinfo,
45b0: 26 74 69 6d 65 69 6e 66 6f 5f 74 6d 29 3b 0a 09 &timeinfo_tm);..
45c0: 69 66 20 28 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f if (timeinfo_tm_
45d0: 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 p == NULL) {...r
45e0: 65 74 75 72 6e 28 22 75 6e 6b 6e 6f 77 6e 22 29 eturn("unknown")
45f0: 3b 0a 09 7d 0a 0a 09 62 75 66 66 65 72 5b 62 75 ;..}...buffer[bu
4600: 66 66 65 72 5f 6c 65 6e 20 2d 20 31 5d 20 3d 20 ffer_len - 1] =
4610: 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 5f 6c 65 '\0';..buffer_le
4620: 6e 20 3d 20 73 74 72 66 74 69 6d 65 28 62 75 66 n = strftime(buf
4630: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 20 fer, buffer_len
4640: 2d 20 31 2c 20 22 25 61 2c 20 25 64 20 25 62 20 - 1, "%a, %d %b
4650: 25 59 20 25 48 3a 25 4d 3a 25 53 20 47 4d 54 22 %Y %H:%M:%S GMT"
4660: 2c 20 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 29 , timeinfo_tm_p)
4670: 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75 66 66 65 ;...return(buffe
4680: 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 61 73 68 20 2a r);.}../* hash *
4690: 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 /.static unsigne
46a0: 64 20 69 6e 74 20 66 69 6c 65 64 5f 68 61 73 68 d int filed_hash
46b0: 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
46c0: 63 68 61 72 20 2a 76 61 6c 75 65 2c 20 75 6e 73 char *value, uns
46d0: 69 67 6e 65 64 20 69 6e 74 20 6d 6f 64 75 6c 75 igned int modulu
46e0: 73 29 20 7b 0a 09 75 6e 73 69 67 6e 65 64 20 63 s) {..unsigned c
46f0: 68 61 72 20 63 75 72 72 2c 20 70 72 65 76 3b 0a har curr, prev;.
4700: 09 69 6e 74 20 64 69 66 66 3b 0a 09 75 6e 73 69 .int diff;..unsi
4710: 67 6e 65 64 20 69 6e 74 20 72 65 74 76 61 6c 3b gned int retval;
4720: 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d 6f 64 75 ...retval = modu
4730: 6c 75 73 20 2d 20 31 3b 0a 09 70 72 65 76 20 3d lus - 1;..prev =
4740: 20 6d 6f 64 75 6c 75 73 20 25 20 32 35 35 3b 0a modulus % 255;.
4750: 0a 09 77 68 69 6c 65 20 28 28 63 75 72 72 20 3d ..while ((curr =
4760: 20 2a 76 61 6c 75 65 29 29 20 7b 0a 09 09 69 66 *value)) {...if
4770: 20 28 63 75 72 72 20 3c 20 33 32 29 20 7b 0a 09 (curr < 32) {..
4780: 09 09 63 75 72 72 20 3d 20 32 35 35 20 2d 20 63 ..curr = 255 - c
4790: 75 72 72 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a urr;...} else {.
47a0: 09 09 09 63 75 72 72 20 2d 3d 20 33 32 3b 0a 09 ...curr -= 32;..
47b0: 09 7d 0a 0a 09 09 69 66 20 28 70 72 65 76 20 3c .}....if (prev <
47c0: 20 63 75 72 72 29 20 7b 0a 09 09 09 64 69 66 66 curr) {....diff
47d0: 20 3d 20 63 75 72 72 20 2d 20 70 72 65 76 3b 0a = curr - prev;.
47e0: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 64 69 ..} else {....di
47f0: 66 66 20 3d 20 70 72 65 76 20 2d 20 63 75 72 72 ff = prev - curr
4800: 3b 0a 09 09 7d 0a 0a 09 09 70 72 65 76 20 3d 20 ;...}....prev =
4810: 63 75 72 72 3b 0a 0a 09 09 72 65 74 76 61 6c 20 curr;....retval
4820: 3c 3c 3d 20 33 3b 0a 09 09 72 65 74 76 61 6c 20 <<= 3;...retval
4830: 26 3d 20 30 78 46 46 46 46 46 46 46 46 4c 55 3b &= 0xFFFFFFFFLU;
4840: 0a 09 09 72 65 74 76 61 6c 20 5e 3d 20 64 69 66 ...retval ^= dif
4850: 66 3b 0a 0a 09 09 76 61 6c 75 65 2b 2b 3b 0a 09 f;....value++;..
4860: 7d 0a 0a 09 72 65 74 76 61 6c 20 3d 20 72 65 74 }...retval = ret
4870: 76 61 6c 20 25 20 6d 6f 64 75 6c 75 73 3b 0a 0a val % modulus;..
4880: 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b .return(retval);
4890: 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 20 61 20 6d 69 .}../* Find a mi
48a0: 6d 65 2d 74 79 70 65 20 62 61 73 65 64 20 6f 6e me-type based on
48b0: 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 2a 2f the filename */
48c0: 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 .static const ch
48d0: 61 72 20 2a 66 69 6c 65 64 5f 64 65 74 65 72 6d ar *filed_determ
48e0: 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 63 6f 6e ine_mimetype(con
48f0: 73 74 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b st char *path) {
4900: 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 3b ..const char *p;
4910: 0a 0a 09 70 20 3d 20 73 74 72 72 63 68 72 28 70 ...p = strrchr(p
4920: 61 74 68 2c 20 27 2e 27 29 3b 0a 09 69 66 20 28 ath, '.');..if (
4930: 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 p == NULL) {...r
4940: 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 eturn(FILED_DEFA
4950: 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 ULT_TYPE);..}...
4960: 70 2b 2b 3b 0a 09 69 66 20 28 2a 70 20 3d 3d 20 p++;..if (*p ==
4970: 27 5c 30 27 29 20 7b 0a 09 09 72 65 74 75 72 6e '\0') {...return
4980: 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 (FILED_DEFAULT_T
4990: 59 50 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 YPE);..}...filed
49a0: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 _log_msg_debug("
49b0: 4c 6f 6f 6b 69 6e 67 20 75 70 20 4d 49 4d 45 20 Looking up MIME
49c0: 74 79 70 65 20 66 6f 72 20 25 73 20 28 68 61 73 type for %s (has
49d0: 68 20 3d 20 25 6c 6c 75 29 22 2c 20 70 2c 20 28 h = %llu)", p, (
49e0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
49f0: 6e 67 29 20 66 69 6c 65 64 5f 68 61 73 68 28 28 ng) filed_hash((
4a00: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
4a10: 68 61 72 20 2a 29 20 70 2c 20 31 36 37 37 37 32 har *) p, 167772
4a20: 35 39 29 29 3b 0a 0a 23 69 6e 63 6c 75 64 65 20 59));..#include
4a30: 22 66 69 6c 65 64 2d 6d 69 6d 65 2d 74 79 70 65 "filed-mime-type
4a40: 73 2e 68 22 0a 0a 09 72 65 74 75 72 6e 28 46 49 s.h"...return(FI
4a50: 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 LED_DEFAULT_TYPE
4a60: 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 6e 65 72 61 74 );.}../* Generat
4a70: 65 20 61 20 75 6e 69 71 75 65 20 69 64 65 6e 74 e a unique ident
4a80: 69 66 69 65 72 20 2a 2f 0a 73 74 61 74 69 63 20 ifier */.static
4a90: 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 6e 65 72 void filed_gener
4aa0: 61 74 65 5f 65 74 61 67 28 63 68 61 72 20 2a 65 ate_etag(char *e
4ab0: 74 61 67 2c 20 73 69 7a 65 5f 74 20 6c 65 6e 67 tag, size_t leng
4ac0: 74 68 29 20 7b 0a 09 73 6e 70 72 69 6e 74 66 28 th) {..snprintf(
4ad0: 65 74 61 67 2c 20 6c 65 6e 67 74 68 2c 20 22 25 etag, length, "%
4ae0: 6c 6c 78 2d 25 6c 6c 78 25 6c 6c 78 25 6c 6c 78 llx-%llx%llx%llx
4af0: 25 6c 6c 78 22 2c 0a 09 09 28 75 6e 73 69 67 6e %llx",...(unsign
4b00: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 74 69 ed long long) ti
4b10: 6d 65 28 4e 55 4c 4c 29 2c 0a 09 09 28 75 6e 73 me(NULL),...(uns
4b20: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
4b30: 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09 28 75 6e random(),...(un
4b40: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
4b50: 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09 28 75 ) random(),...(u
4b60: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
4b70: 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09 28 g) random(),...(
4b80: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
4b90: 6e 67 29 20 72 61 6e 64 6f 6d 28 29 0a 09 29 3b ng) random()..);
4ba0: 0a 7d 0a 0a 2f 2a 20 4f 70 65 6e 20 61 20 66 69 .}../* Open a fi
4bb0: 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 66 69 le and return fi
4bc0: 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a le information *
4bd0: 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
4be0: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a filed_fileinfo *
4bf0: 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 28 filed_open_file(
4c00: 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 const char *path
4c10: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 , struct filed_f
4c20: 69 6c 65 69 6e 66 6f 20 2a 62 75 66 66 65 72 29 ileinfo *buffer)
4c30: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 {..struct filed
4c40: 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 63 68 65 _fileinfo *cache
4c50: 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 ;..unsigned int
4c60: 63 61 63 68 65 5f 69 64 78 3b 0a 09 6f 66 66 5f cache_idx;..off_
4c70: 74 20 6c 65 6e 3b 0a 09 69 6e 74 20 66 64 3b 0a t len;..int fd;.
4c80: 0a 09 63 61 63 68 65 5f 69 64 78 20 3d 20 66 69 ..cache_idx = fi
4c90: 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e 73 74 20 led_hash((const
4ca0: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
4cb0: 20 70 61 74 68 2c 20 66 69 6c 65 64 5f 66 69 6c path, filed_fil
4cc0: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 einfo_fdcache_si
4cd0: 7a 65 29 3b 0a 0a 09 63 61 63 68 65 20 3d 20 26 ze);...cache = &
4ce0: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
4cf0: 64 63 61 63 68 65 5b 63 61 63 68 65 5f 69 64 78 dcache[cache_idx
4d00: 5d 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d ];...filed_log_m
4d10: 73 67 5f 64 65 62 75 67 28 22 4c 6f 63 6b 69 6e sg_debug("Lockin
4d20: 67 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a g mutex for idx:
4d30: 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 %lu", (unsigned
4d40: 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 long) cache_idx
4d50: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 );...pthread_mut
4d60: 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e ex_lock(&cache->
4d70: 6d 75 74 65 78 29 3b 0a 0a 09 66 69 6c 65 64 5f mutex);...filed_
4d80: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 log_msg_debug("C
4d90: 6f 6d 70 6c 65 74 65 64 20 6c 6f 63 6b 69 6e 67 ompleted locking
4da0: 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a 20 mutex for idx:
4db0: 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 %lu", (unsigned
4dc0: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 29 long) cache_idx)
4dd0: 3b 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 28 70 ;...if (strcmp(p
4de0: 61 74 68 2c 20 63 61 63 68 65 2d 3e 70 61 74 68 ath, cache->path
4df0: 29 20 21 3d 20 30 29 20 7b 0a 09 09 66 69 6c 65 ) != 0) {...file
4e00: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 d_log_msg_debug(
4e10: 22 43 61 63 68 65 20 6d 69 73 73 20 66 6f 72 20 "Cache miss for
4e20: 69 64 78 3a 20 25 6c 75 3a 20 4f 4c 44 20 5c 22 idx: %lu: OLD \"
4e30: 25 73 5c 22 2c 20 4e 45 57 20 5c 22 25 73 5c 22 %s\", NEW \"%s\"
4e40: 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ", (unsigned lon
4e50: 67 29 20 63 61 63 68 65 5f 69 64 78 2c 20 63 61 g) cache_idx, ca
4e60: 63 68 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 che->path, path)
4e70: 3b 0a 0a 09 09 66 64 20 3d 20 6f 70 65 6e 28 70 ;....fd = open(p
4e80: 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 20 7c 20 ath, O_RDONLY |
4e90: 4f 5f 4c 41 52 47 45 46 49 4c 45 29 3b 0a 09 09 O_LARGEFILE);...
4ea0: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 if (fd < 0) {...
4eb0: 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 .pthread_mutex_u
4ec0: 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 nlock(&cache->mu
4ed0: 74 65 78 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e tex);.....return
4ee0: 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 69 (NULL);...}....i
4ef0: 66 20 28 63 61 63 68 65 2d 3e 66 64 20 3e 3d 20 f (cache->fd >=
4f00: 30 29 20 7b 0a 09 09 09 63 6c 6f 73 65 28 63 61 0) {....close(ca
4f10: 63 68 65 2d 3e 66 64 29 3b 0a 09 09 7d 0a 0a 09 che->fd);...}...
4f20: 09 6c 65 6e 20 3d 20 6c 73 65 65 6b 28 66 64 2c .len = lseek(fd,
4f30: 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29 3b 0a 09 0, SEEK_END);..
4f40: 09 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 53 45 .lseek(fd, 0, SE
4f50: 45 4b 5f 53 45 54 29 3b 0a 0a 09 09 63 61 63 68 EK_SET);....cach
4f60: 65 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 09 63 61 e->fd = fd;...ca
4f70: 63 68 65 2d 3e 6c 65 6e 20 3d 20 6c 65 6e 3b 0a che->len = len;.
4f80: 09 09 73 74 72 63 70 79 28 63 61 63 68 65 2d 3e ..strcpy(cache->
4f90: 70 61 74 68 2c 20 70 61 74 68 29 3b 0a 09 09 63 path, path);...c
4fa0: 61 63 68 65 2d 3e 74 79 70 65 20 3d 20 66 69 6c ache->type = fil
4fb0: 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f 6d 69 6d ed_determine_mim
4fc0: 65 74 79 70 65 28 70 61 74 68 29 3b 0a 09 09 66 etype(path);...f
4fd0: 69 6c 65 64 5f 67 65 6e 65 72 61 74 65 5f 65 74 iled_generate_et
4fe0: 61 67 28 63 61 63 68 65 2d 3e 65 74 61 67 2c 20 ag(cache->etag,
4ff0: 73 69 7a 65 6f 66 28 63 61 63 68 65 2d 3e 65 74 sizeof(cache->et
5000: 61 67 29 29 3b 0a 0a 09 09 2f 2a 20 58 58 58 3a ag));..../* XXX:
5010: 54 4f 44 4f 3a 20 44 65 74 65 72 6d 69 6e 65 20 TODO: Determine
5020: 2a 2f 0a 09 09 63 61 63 68 65 2d 3e 6c 61 73 74 */...cache->last
5030: 6d 6f 64 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d mod = filed_form
5040: 61 74 5f 74 69 6d 65 28 63 61 63 68 65 2d 3e 6c at_time(cache->l
5050: 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 astmod_b, sizeof
5060: 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f (cache->lastmod_
5070: 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 20 2d b), time(NULL) -
5080: 20 33 30 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 30);..} else {.
5090: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f ..filed_log_msg_
50a0: 64 65 62 75 67 28 22 43 61 63 68 65 20 68 69 74 debug("Cache hit
50b0: 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20 50 for idx: %lu: P
50c0: 41 54 48 20 5c 22 25 73 5c 22 22 2c 20 28 75 6e ATH \"%s\"", (un
50d0: 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 signed long) cac
50e0: 68 65 5f 69 64 78 2c 20 70 61 74 68 29 3b 0a 09 he_idx, path);..
50f0: 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 57 65 20 68 61 }.../*.. * We ha
5100: 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 64 75 70 ve to make a dup
5110: 6c 69 63 61 74 65 20 46 44 2c 20 62 65 63 61 75 licate FD, becau
5120: 73 65 20 6f 6e 63 65 20 77 65 20 72 65 6c 65 61 se once we relea
5130: 73 65 20 74 68 65 20 63 61 63 68 65 0a 09 20 2a se the cache.. *
5140: 20 6d 75 74 65 78 2c 20 74 68 65 20 66 69 6c 65 mutex, the file
5150: 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79 20 descriptor may
5160: 62 65 20 63 6c 6f 73 65 64 0a 09 20 2a 2f 0a 09 be closed.. */..
5170: 66 64 20 3d 20 64 75 70 28 63 61 63 68 65 2d 3e fd = dup(cache->
5180: 66 64 29 3b 0a 09 69 66 20 28 66 64 20 3c 20 30 fd);..if (fd < 0
5190: 29 20 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 ) {...pthread_mu
51a0: 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 tex_unlock(&cach
51b0: 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 72 65 e->mutex);....re
51c0: 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a turn(NULL);..}..
51d0: 09 62 75 66 66 65 72 2d 3e 66 64 20 3d 20 66 64 .buffer->fd = fd
51e0: 3b 0a 09 62 75 66 66 65 72 2d 3e 6c 65 6e 20 3d ;..buffer->len =
51f0: 20 63 61 63 68 65 2d 3e 6c 65 6e 3b 0a 09 62 75 cache->len;..bu
5200: 66 66 65 72 2d 3e 74 79 70 65 20 3d 20 63 61 63 ffer->type = cac
5210: 68 65 2d 3e 74 79 70 65 3b 0a 09 6d 65 6d 63 70 he->type;..memcp
5220: 79 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f y(buffer->lastmo
5230: 64 5f 62 2c 20 63 61 63 68 65 2d 3e 6c 61 73 74 d_b, cache->last
5240: 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 28 62 75 mod_b, sizeof(bu
5250: 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 ffer->lastmod_b)
5260: 29 3b 0a 09 6d 65 6d 63 70 79 28 62 75 66 66 65 );..memcpy(buffe
5270: 72 2d 3e 65 74 61 67 2c 20 63 61 63 68 65 2d 3e r->etag, cache->
5280: 65 74 61 67 2c 20 73 69 7a 65 6f 66 28 62 75 66 etag, sizeof(buf
5290: 66 65 72 2d 3e 65 74 61 67 29 29 3b 0a 09 62 75 fer->etag));..bu
52a0: 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20 ffer->lastmod =
52b0: 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 5f buffer->lastmod_
52c0: 62 20 2b 20 28 63 61 63 68 65 2d 3e 6c 61 73 74 b + (cache->last
52d0: 6d 6f 64 20 2d 20 63 61 63 68 65 2d 3e 6c 61 73 mod - cache->las
52e0: 74 6d 6f 64 5f 62 29 3b 0a 0a 09 70 74 68 72 65 tmod_b);...pthre
52f0: 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 ad_mutex_unlock(
5300: 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a &cache->mutex);.
5310: 0a 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 29 ..return(buffer)
5320: 3b 0a 7d 0a 0a 2f 2a 20 50 72 6f 63 65 73 73 20 ;.}../* Process
5330: 61 6e 20 48 54 54 50 20 72 65 71 75 65 73 74 20 an HTTP request
5340: 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 70 and return the p
5350: 61 74 68 20 72 65 71 75 65 73 74 65 64 20 2a 2f ath requested */
5360: 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 66 .static struct f
5370: 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 iled_http_reques
5380: 74 20 2a 66 69 6c 65 64 5f 67 65 74 5f 68 74 74 t *filed_get_htt
5390: 70 5f 72 65 71 75 65 73 74 28 46 49 4c 45 20 2a p_request(FILE *
53a0: 66 70 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 fp, struct filed
53b0: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 62 _http_request *b
53c0: 75 66 66 65 72 5f 73 74 2c 20 73 74 72 75 63 74 uffer_st, struct
53d0: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a filed_options *
53e0: 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 63 68 61 72 options) {..char
53f0: 20 2a 6d 65 74 68 6f 64 2c 20 2a 70 61 74 68 3b *method, *path;
5400: 0a 09 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 ..char *buffer,
5410: 2a 77 6f 72 6b 62 75 66 66 65 72 2c 20 2a 77 6f *workbuffer, *wo
5420: 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 09 rkbuffer_next;..
5430: 63 68 61 72 20 2a 66 67 65 74 73 5f 72 65 74 3b char *fgets_ret;
5440: 0a 09 73 69 7a 65 5f 74 20 62 75 66 66 65 72 5f ..size_t buffer_
5450: 6c 65 6e 2c 20 70 61 74 68 5f 6c 65 6e 3b 0a 09 len, path_len;..
5460: 6f 66 66 5f 74 20 72 61 6e 67 65 5f 73 74 61 72 off_t range_star
5470: 74 2c 20 72 61 6e 67 65 5f 65 6e 64 2c 20 72 61 t, range_end, ra
5480: 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 09 69 6e 74 nge_length;..int
5490: 20 72 61 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a range_request;.
54a0: 09 69 6e 74 20 73 6e 70 72 69 6e 74 66 5f 72 65 .int snprintf_re
54b0: 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 2f 2a 20 t;..int i;.../*
54c0: 53 65 74 20 74 6f 20 64 65 66 61 75 6c 74 20 76 Set to default v
54d0: 61 6c 75 65 73 20 2a 2f 0a 09 72 61 6e 67 65 5f alues */..range_
54e0: 73 74 61 72 74 20 3d 20 30 3b 0a 09 72 61 6e 67 start = 0;..rang
54f0: 65 5f 65 6e 64 20 20 20 3d 20 30 3b 0a 09 72 61 e_end = 0;..ra
5500: 6e 67 65 5f 72 65 71 75 65 73 74 20 3d 20 30 3b nge_request = 0;
5510: 0a 09 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d ..range_length =
5520: 20 2d 31 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d -1;..buffer_st-
5530: 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 >headers.host.pr
5540: 65 73 65 6e 74 20 3d 20 30 3b 0a 09 62 75 66 66 esent = 0;..buff
5550: 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 63 er_st->headers.c
5560: 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 46 49 4c 45 onnection = FILE
5570: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f D_CONNECTION_CLO
5580: 53 45 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 62 SE;...buffer = b
5590: 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 uffer_st->tmpbuf
55a0: 3b 0a 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 ;..buffer_len =
55b0: 73 69 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 74 sizeof(buffer_st
55c0: 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 66 67 65 ->tmpbuf);...fge
55d0: 74 73 5f 72 65 74 20 3d 20 66 67 65 74 73 28 62 ts_ret = fgets(b
55e0: 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 uffer, buffer_le
55f0: 6e 2c 20 66 70 29 3b 0a 09 69 66 20 28 66 67 65 n, fp);..if (fge
5600: 74 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 ts_ret == NULL)
5610: 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 {...return(NULL)
5620: 3b 0a 09 7d 0a 0a 09 6d 65 74 68 6f 64 20 3d 20 ;..}...method =
5630: 62 75 66 66 65 72 3b 0a 0a 09 62 75 66 66 65 72 buffer;...buffer
5640: 20 3d 20 73 74 72 63 68 72 28 62 75 66 66 65 72 = strchr(buffer
5650: 2c 20 27 20 27 29 3b 0a 09 69 66 20 28 62 75 66 , ' ');..if (buf
5660: 66 65 72 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 fer == NULL) {..
5670: 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 .return(NULL);..
5680: 7d 0a 0a 09 2a 62 75 66 66 65 72 20 3d 20 27 5c }...*buffer = '\
5690: 30 27 3b 0a 09 62 75 66 66 65 72 2b 2b 3b 0a 0a 0';..buffer++;..
56a0: 09 70 61 74 68 20 3d 20 62 75 66 66 65 72 3b 0a .path = buffer;.
56b0: 0a 09 2f 2a 20 54 65 72 6d 69 6e 61 74 65 20 70 ../* Terminate p
56c0: 61 74 68 20 63 6f 6d 70 6f 6e 65 6e 74 20 2a 2f ath component */
56d0: 0a 09 62 75 66 66 65 72 20 3d 20 73 74 72 70 62 ..buffer = strpb
56e0: 72 6b 28 70 61 74 68 2c 20 22 5c 72 5c 6e 20 22 rk(path, "\r\n "
56f0: 29 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20 21 );..if (buffer !
5700: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 62 75 66 = NULL) {...*buf
5710: 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 62 75 fer = '\0';...bu
5720: 66 66 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 2f 2a 20 ffer++;..}.../*
5730: 57 65 20 6f 6e 6c 79 20 68 61 6e 64 6c 65 20 74 We only handle t
5740: 68 65 20 22 47 45 54 22 20 61 6e 64 20 22 48 45 he "GET" and "HE
5750: 41 44 27 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 09 AD' methods */..
5760: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 28 6d if (strcasecmp(m
5770: 65 74 68 6f 64 2c 20 22 68 65 61 64 22 29 20 21 ethod, "head") !
5780: 3d 20 30 29 20 7b 0a 09 09 69 66 20 28 73 74 72 = 0) {...if (str
5790: 63 61 73 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 casecmp(method,
57a0: 22 67 65 74 22 29 20 21 3d 20 30 29 20 7b 0a 09 "get") != 0) {..
57b0: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a ..return(NULL);.
57c0: 09 09 7d 0a 0a 09 09 2f 2a 20 47 45 54 20 72 65 ..}..../* GET re
57d0: 71 75 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 quest */...buffe
57e0: 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 r_st->method = F
57f0: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 ILED_REQUEST_MET
5800: 48 4f 44 5f 47 45 54 3b 0a 09 7d 20 65 6c 73 65 HOD_GET;..} else
5810: 20 7b 0a 09 09 2f 2a 20 48 45 41 44 20 72 65 71 {.../* HEAD req
5820: 75 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 72 uest */...buffer
5830: 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 49 _st->method = FI
5840: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 LED_REQUEST_METH
5850: 4f 44 5f 48 45 41 44 3b 0a 09 7d 0a 0a 09 2f 2a OD_HEAD;..}.../*
5860: 20 4e 6f 74 65 20 70 61 74 68 20 2a 2f 0a 09 70 Note path */..p
5870: 61 74 68 5f 6c 65 6e 20 3d 20 73 74 72 6c 65 6e ath_len = strlen
5880: 28 70 61 74 68 29 3b 0a 09 6d 65 6d 63 70 79 28 (path);..memcpy(
5890: 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c buffer_st->path,
58a0: 20 70 61 74 68 2c 20 70 61 74 68 5f 6c 65 6e 20 path, path_len
58b0: 2b 20 31 29 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 + 1);.../* Deter
58c0: 6d 69 6e 65 20 74 79 70 65 20 6f 66 20 72 65 71 mine type of req
58d0: 75 65 73 74 20 66 72 6f 6d 20 70 61 74 68 20 2a uest from path *
58e0: 2f 0a 09 69 66 20 28 70 61 74 68 5f 6c 65 6e 20 /..if (path_len
58f0: 3d 3d 20 30 29 20 7b 0a 09 09 62 75 66 66 65 72 == 0) {...buffer
5900: 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 _st->type = FILE
5910: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 D_REQUEST_TYPE_D
5920: 49 52 45 43 54 4f 52 59 3b 0a 09 7d 20 65 6c 73 IRECTORY;..} els
5930: 65 20 7b 0a 09 09 69 66 20 28 70 61 74 68 5b 70 e {...if (path[p
5940: 61 74 68 5f 6c 65 6e 20 2d 20 31 5d 20 3d 3d 20 ath_len - 1] ==
5950: 27 2f 27 29 20 7b 0a 09 09 09 62 75 66 66 65 72 '/') {....buffer
5960: 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 _st->type = FILE
5970: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 D_REQUEST_TYPE_D
5980: 49 52 45 43 54 4f 52 59 3b 0a 09 09 7d 20 65 6c IRECTORY;...} el
5990: 73 65 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 se {....buffer_s
59a0: 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f t->type = FILED_
59b0: 52 45 51 55 45 53 54 5f 54 59 50 45 5f 4f 54 48 REQUEST_TYPE_OTH
59c0: 45 52 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 ER;...}..}.../*
59d0: 52 65 73 65 74 20 62 75 66 66 65 72 20 66 6f 72 Reset buffer for
59e0: 20 6c 61 74 65 72 20 75 73 65 20 2a 2f 0a 09 62 later use */..b
59f0: 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 uffer = buffer_s
5a00: 74 2d 3e 74 6d 70 62 75 66 3b 0a 0a 09 66 6f 72 t->tmpbuf;...for
5a10: 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 31 30 30 (i = 0; i < 100
5a20: 3b 20 69 2b 2b 29 20 7b 0a 09 09 66 67 65 74 73 ; i++) {...fgets
5a30: 5f 72 65 74 20 3d 20 66 67 65 74 73 28 62 75 66 _ret = fgets(buf
5a40: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c fer, buffer_len,
5a50: 20 66 70 29 3b 0a 09 09 69 66 20 28 66 67 65 74 fp);...if (fget
5a60: 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b s_ret == NULL) {
5a70: 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a ....break;...}..
5a80: 09 09 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d ..if (strncasecm
5a90: 70 28 62 75 66 66 65 72 2c 20 22 52 61 6e 67 65 p(buffer, "Range
5aa0: 3a 20 22 2c 20 37 29 20 3d 3d 20 30 29 20 7b 0a : ", 7) == 0) {.
5ab0: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 ...workbuffer =
5ac0: 62 75 66 66 65 72 20 2b 20 37 3b 0a 0a 09 09 09 buffer + 7;.....
5ad0: 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 if (strncasecmp(
5ae0: 77 6f 72 6b 62 75 66 66 65 72 2c 20 22 62 79 74 workbuffer, "byt
5af0: 65 73 3d 22 2c 20 36 29 20 3d 3d 20 30 29 20 7b es=", 6) == 0) {
5b00: 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 .....workbuffer
5b10: 2b 3d 20 36 3b 0a 0a 09 09 09 09 72 61 6e 67 65 += 6;......range
5b20: 5f 72 65 71 75 65 73 74 20 3d 20 31 3b 0a 0a 09 _request = 1;...
5b30: 09 09 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d ...range_start =
5b40: 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 strtoull(workbu
5b50: 66 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 ffer, &workbuffe
5b60: 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 0a 09 09 r_next, 10);....
5b70: 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 77 ..workbuffer = w
5b80: 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a orkbuffer_next;.
5b90: 0a 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 .....if (*workbu
5ba0: 66 66 65 72 20 3d 3d 20 27 2d 27 29 20 7b 0a 09 ffer == '-') {..
5bb0: 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b ....workbuffer++
5bc0: 3b 0a 0a 09 09 09 09 09 69 66 20 28 2a 77 6f 72 ;.......if (*wor
5bd0: 6b 62 75 66 66 65 72 20 21 3d 20 27 5c 72 27 20 kbuffer != '\r'
5be0: 26 26 20 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 && *workbuffer !
5bf0: 3d 20 27 5c 6e 27 29 20 7b 0a 09 09 09 09 09 09 = '\n') {.......
5c00: 72 61 6e 67 65 5f 65 6e 64 20 3d 20 73 74 72 74 range_end = strt
5c10: 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c oull(workbuffer,
5c20: 20 26 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 &workbuffer_nex
5c30: 74 2c 20 31 30 29 3b 0a 09 09 09 09 09 7d 0a 09 t, 10);......}..
5c40: 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 20 65 6c ...}....}...} el
5c50: 73 65 20 69 66 20 28 73 74 72 6e 63 61 73 65 63 se if (strncasec
5c60: 6d 70 28 62 75 66 66 65 72 2c 20 22 48 6f 73 74 mp(buffer, "Host
5c70: 3a 20 22 2c 20 35 29 20 3d 3d 20 30 29 20 7b 0a : ", 5) == 0) {.
5c80: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 ...buffer_st->he
5c90: 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65 aders.host.prese
5ca0: 6e 74 20 3d 20 31 3b 0a 0a 09 09 09 77 6f 72 6b nt = 1;.....work
5cb0: 62 75 66 66 65 72 20 3d 20 73 74 72 70 62 72 6b buffer = strpbrk
5cc0: 28 62 75 66 66 65 72 20 2b 20 35 2c 20 22 5c 72 (buffer + 5, "\r
5cd0: 5c 6e 3a 22 29 3b 0a 09 09 09 69 66 20 28 77 6f \n:");....if (wo
5ce0: 72 6b 62 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c rkbuffer != NULL
5cf0: 29 20 7b 0a 09 09 09 09 2a 77 6f 72 6b 62 75 66 ) {.....*workbuf
5d00: 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 09 7d fer = '\0';....}
5d10: 0a 0a 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 .....workbuffer
5d20: 3d 20 62 75 66 66 65 72 20 2b 20 35 3b 0a 09 09 = buffer + 5;...
5d30: 09 77 68 69 6c 65 20 28 2a 77 6f 72 6b 62 75 66 .while (*workbuf
5d40: 66 65 72 20 3d 3d 20 27 20 27 29 20 7b 0a 09 09 fer == ' ') {...
5d50: 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a ..workbuffer++;.
5d60: 09 09 09 7d 0a 0a 09 09 09 73 74 72 63 70 79 28 ...}.....strcpy(
5d70: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
5d80: 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c 20 77 6f rs.host.host, wo
5d90: 72 6b 62 75 66 66 65 72 29 3b 0a 09 09 7d 20 65 rkbuffer);...} e
5da0: 6c 73 65 20 69 66 20 28 73 74 72 6e 63 61 73 65 lse if (strncase
5db0: 63 6d 70 28 62 75 66 66 65 72 2c 20 22 43 6f 6e cmp(buffer, "Con
5dc0: 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c nection: Keep-Al
5dd0: 69 76 65 22 2c 20 32 32 29 20 3d 3d 20 30 29 20 ive", 22) == 0)
5de0: 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e {....buffer_st->
5df0: 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 headers.connecti
5e00: 6f 6e 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 on = FILED_CONNE
5e10: 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 CTION_KEEP_ALIVE
5e20: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 6d 65 6d ;...}....if (mem
5e30: 63 6d 70 28 62 75 66 66 65 72 2c 20 22 5c 72 5c cmp(buffer, "\r\
5e40: 6e 22 2c 20 32 29 20 3d 3d 20 30 29 20 7b 0a 09 n", 2) == 0) {..
5e50: 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a ..break;...}..}.
5e60: 0a 09 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 72 ../* Determine r
5e70: 61 6e 67 65 20 2a 2f 0a 09 69 66 20 28 72 61 6e ange */..if (ran
5e80: 67 65 5f 65 6e 64 20 21 3d 20 30 29 20 7b 0a 09 ge_end != 0) {..
5e90: 09 69 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 3c .if (range_end <
5ea0: 3d 20 72 61 6e 67 65 5f 73 74 61 72 74 29 20 7b = range_start) {
5eb0: 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 ....return(NULL)
5ec0: 3b 0a 09 09 7d 0a 0a 09 09 72 61 6e 67 65 5f 6c ;...}....range_l
5ed0: 65 6e 67 74 68 20 3d 20 72 61 6e 67 65 5f 65 6e ength = range_en
5ee0: 64 20 2d 20 72 61 6e 67 65 5f 73 74 61 72 74 3b d - range_start;
5ef0: 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 ....filed_log_ms
5f00: 67 5f 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 g_debug("Computi
5f10: 6e 67 20 6c 65 6e 67 74 68 20 70 61 72 61 6d 65 ng length parame
5f20: 74 65 72 3a 20 25 6c 6c 75 20 3d 20 25 6c 6c 75 ter: %llu = %llu
5f30: 20 2d 20 25 6c 6c 75 22 2c 0a 09 09 09 28 75 6e - %llu",....(un
5f40: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
5f50: 29 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 2c 0a ) range_length,.
5f60: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
5f70: 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 65 6e g long) range_en
5f80: 64 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 d,....(unsigned
5f90: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 long long) range
5fa0: 5f 73 74 61 72 74 0a 09 09 29 3b 0a 09 7d 0a 0a _start...);..}..
5fb0: 09 2f 2a 20 46 69 6c 6c 20 75 70 20 73 74 72 75 ./* Fill up stru
5fc0: 63 74 75 72 65 20 74 6f 20 72 65 74 75 72 6e 20 cture to return
5fd0: 2a 2f 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 */..buffer_st->h
5fe0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 eaders.range.pre
5ff0: 73 65 6e 74 20 3d 20 72 61 6e 67 65 5f 72 65 71 sent = range_req
6000: 75 65 73 74 3b 0a 09 62 75 66 66 65 72 5f 73 74 uest;..buffer_st
6010: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
6020: 6f 66 66 73 65 74 20 20 3d 20 72 61 6e 67 65 5f offset = range_
6030: 73 74 61 72 74 3b 0a 09 62 75 66 66 65 72 5f 73 start;..buffer_s
6040: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
6050: 2e 6c 65 6e 67 74 68 20 20 3d 20 72 61 6e 67 65 .length = range
6060: 5f 6c 65 6e 67 74 68 3b 0a 0a 09 2f 2a 20 49 66 _length;.../* If
6070: 20 76 68 6f 73 74 73 20 61 72 65 20 65 6e 61 62 vhosts are enab
6080: 6c 65 64 2c 20 63 6f 6d 70 75 74 65 20 6e 65 77 led, compute new
6090: 20 70 61 74 68 20 2a 2f 0a 09 69 66 20 28 6f 70 path */..if (op
60a0: 74 69 6f 6e 73 2d 3e 76 68 6f 73 74 73 5f 65 6e tions->vhosts_en
60b0: 61 62 6c 65 64 29 20 7b 0a 09 09 69 66 20 28 62 abled) {...if (b
60c0: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 uffer_st->header
60d0: 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d s.host.present =
60e0: 3d 20 31 29 20 7b 0a 09 09 09 62 75 66 66 65 72 = 1) {....buffer
60f0: 20 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d = buffer_st->tm
6100: 70 62 75 66 3b 0a 09 09 09 62 75 66 66 65 72 5f pbuf;....buffer_
6110: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 len = sizeof(buf
6120: 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b fer_st->tmpbuf);
6130: 0a 0a 09 09 09 73 6e 70 72 69 6e 74 66 5f 72 65 .....snprintf_re
6140: 74 20 3d 20 73 6e 70 72 69 6e 74 66 28 62 75 66 t = snprintf(buf
6150: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c fer, buffer_len,
6160: 20 22 2f 25 73 25 73 25 73 22 2c 0a 09 09 09 09 "/%s%s%s",.....
6170: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
6180: 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c 0a 09 09 rs.host.host,...
6190: 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 ..buffer_st->pat
61a0: 68 5b 30 5d 20 3d 3d 20 27 2f 27 20 3f 20 22 22 h[0] == '/' ? ""
61b0: 20 3a 20 22 2f 22 2c 0a 09 09 09 09 62 75 66 66 : "/",.....buff
61c0: 65 72 5f 73 74 2d 3e 70 61 74 68 0a 09 09 09 29 er_st->path....)
61d0: 3b 0a 09 09 09 69 66 20 28 73 6e 70 72 69 6e 74 ;....if (snprint
61e0: 66 5f 72 65 74 20 3e 3d 20 30 29 20 7b 0a 09 09 f_ret >= 0) {...
61f0: 09 09 69 66 20 28 28 28 75 6e 73 69 67 6e 65 64 ..if (((unsigned
6200: 20 69 6e 74 29 20 73 6e 70 72 69 6e 74 66 5f 72 int) snprintf_r
6210: 65 74 29 20 3c 20 62 75 66 66 65 72 5f 6c 65 6e et) < buffer_len
6220: 29 20 7b 0a 09 09 09 09 09 73 74 72 63 70 79 28 ) {......strcpy(
6230: 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c buffer_st->path,
6240: 20 62 75 66 66 65 72 29 3b 0a 09 09 09 09 7d 0a buffer);.....}.
6250: 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 ...}...}..}...re
6260: 74 75 72 6e 28 62 75 66 66 65 72 5f 73 74 29 3b turn(buffer_st);
6270: 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 6e .}../* Return an
6280: 20 65 72 72 6f 72 20 70 61 67 65 20 2a 2f 0a 73 error page */.s
6290: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 tatic void filed
62a0: 5f 65 72 72 6f 72 5f 70 61 67 65 28 46 49 4c 45 _error_page(FILE
62b0: 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 *fp, const char
62c0: 20 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 *date_current,
62d0: 69 6e 74 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 int error_number
62e0: 2c 20 69 6e 74 20 6d 65 74 68 6f 64 2c 20 63 6f , int method, co
62f0: 6e 73 74 20 63 68 61 72 20 2a 72 65 61 73 6f 6e nst char *reason
6300: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c , struct filed_l
6310: 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 29 20 7b og_entry *log) {
6320: 0a 09 63 68 61 72 20 2a 65 72 72 6f 72 5f 73 74 ..char *error_st
6330: 72 69 6e 67 20 3d 20 22 3c 68 74 6d 6c 3e 3c 68 ring = "<html><h
6340: 65 61 64 3e 3c 74 69 74 6c 65 3e 45 52 52 4f 52 ead><title>ERROR
6350: 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 3e 3c </title></head><
6360: 62 6f 64 79 3e 55 6e 61 62 6c 65 20 74 6f 20 70 body>Unable to p
6370: 72 6f 63 65 73 73 20 72 65 71 75 65 73 74 3c 2f rocess request</
6380: 62 6f 64 79 3e 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a body></html>";..
6390: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54 .fprintf(fp, "HT
63a0: 54 50 2f 31 2e 31 20 25 69 20 4e 6f 74 20 4f 4b TP/1.1 %i Not OK
63b0: 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e \r\nDate: %s\r\n
63c0: 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c Server: filed\r\
63d0: 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 nLast-Modified:
63e0: 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 %s\r\nContent-Le
63f0: 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 43 6f ngth: %llu\r\nCo
6400: 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 ntent-Type: %s\r
6410: 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c \nConnection: cl
6420: 6f 73 65 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 ose\r\n\r\n",...
6430: 65 72 72 6f 72 5f 6e 75 6d 62 65 72 2c 0a 09 09 error_number,...
6440: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 date_current,...
6450: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 date_current,...
6460: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
6470: 6f 6e 67 29 20 73 74 72 6c 65 6e 28 65 72 72 6f ong) strlen(erro
6480: 72 5f 73 74 72 69 6e 67 29 2c 0a 09 09 22 74 65 r_string),..."te
6490: 78 74 2f 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f xt/html"..);.../
64a0: 2a 20 73 69 6c 65 6e 63 65 20 65 72 72 6f 72 20 * silence error
64b0: 73 74 72 69 6e 67 20 66 6f 72 20 48 45 41 44 20 string for HEAD
64c0: 72 65 71 75 65 73 74 73 20 2a 2f 0a 09 69 66 20 requests */..if
64d0: 28 6d 65 74 68 6f 64 20 21 3d 20 46 49 4c 45 44 (method != FILED
64e0: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f _REQUEST_METHOD_
64f0: 48 45 41 44 29 20 7b 0a 09 09 66 70 72 69 6e 74 HEAD) {...fprint
6500: 66 28 66 70 2c 20 22 25 73 22 2c 20 65 72 72 6f f(fp, "%s", erro
6510: 72 5f 73 74 72 69 6e 67 29 3b 0a 09 7d 0a 0a 09 r_string);..}...
6520: 2f 2a 20 4c 6f 67 20 65 72 72 6f 72 20 2a 2f 0a /* Log error */.
6530: 09 2f 2a 2a 20 72 65 61 73 6f 6e 20 6d 75 73 74 ./** reason must
6540: 20 70 6f 69 6e 74 20 74 6f 20 61 20 67 6c 6f 62 point to a glob
6550: 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 76 ally allocated v
6560: 61 6c 75 65 20 2a 2a 2f 0a 09 6c 6f 67 2d 3e 72 alue **/..log->r
6570: 65 61 73 6f 6e 20 3d 20 72 65 61 73 6f 6e 3b 0a eason = reason;.
6580: 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 .log->http_code
6590: 3d 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 3b 0a = error_number;.
65a0: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 ..filed_log_entr
65b0: 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f y(log);.../* Clo
65c0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
65d0: 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
65e0: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 69 6c 65 meout_close(file
65f0: 6e 6f 28 66 70 29 29 3b 0a 0a 09 66 63 6c 6f 73 no(fp));...fclos
6600: 65 28 66 70 29 3b 0a 0a 09 72 65 74 75 72 6e 3b e(fp);...return;
6610: 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 .}../* Return a
6620: 72 65 64 69 72 65 63 74 20 74 6f 20 69 6e 64 65 redirect to inde
6630: 78 2e 68 74 6d 6c 20 2a 2f 0a 73 74 61 74 69 63 x.html */.static
6640: 20 76 6f 69 64 20 66 69 6c 65 64 5f 72 65 64 69 void filed_redi
6650: 72 65 63 74 5f 69 6e 64 65 78 28 46 49 4c 45 20 rect_index(FILE
6660: 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *fp, const char
6670: 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 63 *date_current, c
6680: 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c onst char *path,
6690: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f struct filed_lo
66a0: 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 29 20 7b 0a g_entry *log) {.
66b0: 09 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 20 3d .int http_code =
66c0: 20 33 30 31 3b 0a 09 66 70 72 69 6e 74 66 28 66 301;..fprintf(f
66d0: 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20 p, "HTTP/1.1 %i
66e0: 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 OK\r\nDate: %s\r
66f0: 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c \nServer: filed\
6700: 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 r\nLast-Modified
6710: 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d : %s\r\nContent-
6720: 4c 65 6e 67 74 68 3a 20 30 5c 72 5c 6e 43 6f 6e Length: 0\r\nCon
6730: 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c 72 nection: close\r
6740: 5c 6e 4c 6f 63 61 74 69 6f 6e 3a 20 25 73 5c 72 \nLocation: %s\r
6750: 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 68 74 74 70 5f \n\r\n",...http_
6760: 63 6f 64 65 2c 0a 09 09 64 61 74 65 5f 63 75 72 code,...date_cur
6770: 72 65 6e 74 2c 0a 09 09 64 61 74 65 5f 63 75 72 rent,...date_cur
6780: 72 65 6e 74 2c 0a 09 09 22 69 6e 64 65 78 2e 68 rent,..."index.h
6790: 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f 2a 20 4c 6f tml"..);.../* Lo
67a0: 67 20 72 65 64 69 72 65 63 74 20 2a 2f 0a 09 6c g redirect */..l
67b0: 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 72 65 og->reason = "re
67c0: 64 69 72 65 63 74 22 3b 0a 09 6c 6f 67 2d 3e 68 direct";..log->h
67d0: 74 74 70 5f 63 6f 64 65 20 3d 20 68 74 74 70 5f ttp_code = http_
67e0: 63 6f 64 65 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f code;...filed_lo
67f0: 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 g_entry(log);...
6800: 2f 2a 20 43 6c 6f 73 65 20 63 6f 6e 6e 65 63 74 /* Close connect
6810: 69 6f 6e 20 2a 2f 0a 09 66 69 6c 65 64 5f 73 6f ion */..filed_so
6820: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 ckettimeout_clos
6830: 65 28 66 69 6c 65 6e 6f 28 66 70 29 29 3b 0a 0a e(fileno(fp));..
6840: 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72 .fclose(fp);...r
6850: 65 74 75 72 6e 3b 0a 0a 09 2f 2a 20 43 75 72 72 eturn;.../* Curr
6860: 65 6e 74 6c 79 20 75 6e 75 73 65 64 3a 20 70 61 ently unused: pa
6870: 74 68 20 2a 2f 0a 09 70 61 74 68 20 3d 20 70 61 th */..path = pa
6880: 74 68 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 72 th;.}../* Conver
6890: 74 20 61 6e 20 65 6e 75 6d 20 72 65 70 72 65 73 t an enum repres
68a0: 65 6e 74 69 6e 67 20 74 68 65 20 22 43 6f 6e 6e enting the "Conn
68b0: 65 63 74 69 6f 6e 22 20 68 65 61 64 65 72 20 76 ection" header v
68c0: 61 6c 75 65 20 74 6f 20 61 20 73 74 72 69 6e 67 alue to a string
68d0: 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 */.static const
68e0: 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 63 6f 6e char *filed_con
68f0: 6e 65 63 74 69 6f 6e 5f 73 74 72 28 69 6e 74 20 nection_str(int
6900: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c 75 65 connection_value
6910: 29 20 7b 0a 09 73 77 69 74 63 68 20 28 63 6f 6e ) {..switch (con
6920: 6e 65 63 74 69 6f 6e 5f 76 61 6c 75 65 29 20 7b nection_value) {
6930: 0a 09 09 63 61 73 65 20 46 49 4c 45 44 5f 43 4f ...case FILED_CO
6940: 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 3a 0a NNECTION_CLOSE:.
6950: 09 09 09 72 65 74 75 72 6e 28 22 63 6c 6f 73 65 ...return("close
6960: 22 29 3b 0a 09 09 63 61 73 65 20 46 49 4c 45 44 ");...case FILED
6970: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 _CONNECTION_KEEP
6980: 5f 41 4c 49 56 45 3a 0a 09 09 09 72 65 74 75 72 _ALIVE:....retur
6990: 6e 28 22 6b 65 65 70 2d 61 6c 69 76 65 22 29 3b n("keep-alive");
69a0: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 22 63 6c ..}...return("cl
69b0: 6f 73 65 22 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e ose");.}../* Han
69c0: 64 6c 65 20 61 20 73 69 6e 67 6c 65 20 72 65 71 dle a single req
69d0: 75 65 73 74 20 66 72 6f 6d 20 61 20 63 6c 69 65 uest from a clie
69e0: 6e 74 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 nt */.static int
69f0: 20 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c filed_handle_cl
6a00: 69 65 6e 74 28 69 6e 74 20 66 64 2c 20 73 74 72 ient(int fd, str
6a10: 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 uct filed_http_r
6a20: 65 71 75 65 73 74 20 2a 72 65 71 75 65 73 74 2c equest *request,
6a30: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f struct filed_lo
6a40: 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 2c 20 73 74 g_entry *log, st
6a50: 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f ruct filed_optio
6a60: 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 ns *options) {..
6a70: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c struct filed_fil
6a80: 65 69 6e 66 6f 20 2a 66 69 6c 65 69 6e 66 6f 3b einfo *fileinfo;
6a90: 0a 09 73 73 69 7a 65 5f 74 20 73 65 6e 64 66 69 ..ssize_t sendfi
6aa0: 6c 65 5f 72 65 74 3b 0a 09 73 69 7a 65 5f 74 20 le_ret;..size_t
6ab0: 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 3b 0a 09 sendfile_size;..
6ac0: 6f 66 66 5f 74 20 73 65 6e 64 66 69 6c 65 5f 6f off_t sendfile_o
6ad0: 66 66 73 65 74 2c 20 73 65 6e 64 66 69 6c 65 5f ffset, sendfile_
6ae0: 73 65 6e 74 2c 20 73 65 6e 64 66 69 6c 65 5f 6c sent, sendfile_l
6af0: 65 6e 3b 0a 09 63 68 61 72 20 2a 70 61 74 68 3b en;..char *path;
6b00: 0a 09 63 68 61 72 20 2a 64 61 74 65 5f 63 75 72 ..char *date_cur
6b10: 72 65 6e 74 2c 20 64 61 74 65 5f 63 75 72 72 65 rent, date_curre
6b20: 6e 74 5f 62 5b 36 34 5d 3b 0a 09 69 6e 74 20 68 nt_b[64];..int h
6b30: 74 74 70 5f 63 6f 64 65 3b 0a 09 46 49 4c 45 20 ttp_code;..FILE
6b40: 2a 66 70 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 6d *fp;.../* Determ
6b50: 69 6e 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 ine current time
6b60: 20 2a 2f 0a 09 64 61 74 65 5f 63 75 72 72 65 6e */..date_curren
6b70: 74 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d 61 74 t = filed_format
6b80: 5f 74 69 6d 65 28 64 61 74 65 5f 63 75 72 72 65 _time(date_curre
6b90: 6e 74 5f 62 2c 20 73 69 7a 65 6f 66 28 64 61 74 nt_b, sizeof(dat
6ba0: 65 5f 63 75 72 72 65 6e 74 5f 62 29 2c 20 74 69 e_current_b), ti
6bb0: 6d 65 28 4e 55 4c 4c 29 29 3b 0a 0a 09 2f 2a 20 me(NULL));.../*
6bc0: 4f 70 65 6e 20 73 6f 63 6b 65 74 20 61 73 20 41 Open socket as A
6bd0: 4e 53 49 20 49 2f 4f 20 66 6f 72 20 65 61 73 65 NSI I/O for ease
6be0: 20 6f 66 20 75 73 65 20 2a 2f 0a 09 66 70 20 3d of use */..fp =
6bf0: 20 66 64 6f 70 65 6e 28 66 64 2c 20 22 77 2b 62 fdopen(fd, "w+b
6c00: 22 29 3b 0a 09 69 66 20 28 66 70 20 3d 3d 20 4e ");..if (fp == N
6c10: 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 73 ULL) {...filed_s
6c20: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f ockettimeout_clo
6c30: 73 65 28 66 64 29 3b 0a 0a 09 09 63 6c 6f 73 65 se(fd);....close
6c40: 28 66 64 29 3b 0a 0a 09 09 6c 6f 67 2d 3e 62 75 (fd);....log->bu
6c50: 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a ffer[0] = '\0';.
6c60: 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 ..log->http_code
6c70: 20 3d 20 2d 31 3b 0a 09 09 6c 6f 67 2d 3e 72 65 = -1;...log->re
6c80: 61 73 6f 6e 20 3d 20 22 66 64 6f 70 65 6e 5f 66 ason = "fdopen_f
6c90: 61 69 6c 65 64 22 3b 0a 0a 09 09 66 69 6c 65 64 ailed";....filed
6ca0: 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b _log_entry(log);
6cb0: 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 ....return(FILED
6cc0: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 _CONNECTION_CLOS
6cd0: 45 29 3b 0a 09 7d 0a 0a 09 72 65 71 75 65 73 74 E);..}...request
6ce0: 20 3d 20 66 69 6c 65 64 5f 67 65 74 5f 68 74 74 = filed_get_htt
6cf0: 70 5f 72 65 71 75 65 73 74 28 66 70 2c 20 72 65 p_request(fp, re
6d00: 71 75 65 73 74 2c 20 6f 70 74 69 6f 6e 73 29 3b quest, options);
6d10: 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74 20 3d ...if (request =
6d20: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 6c 6f 67 2d = NULL) {...log-
6d30: 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 >buffer[0] = '\0
6d40: 27 3b 0a 0a 09 09 66 69 6c 65 64 5f 65 72 72 6f ';....filed_erro
6d50: 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f r_page(fp, date_
6d60: 63 75 72 72 65 6e 74 2c 20 35 30 30 2c 20 46 49 current, 500, FI
6d70: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 LED_REQUEST_METH
6d80: 4f 44 5f 47 45 54 2c 20 22 66 6f 72 6d 61 74 22 OD_GET, "format"
6d90: 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 , log);....retur
6da0: 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 n(FILED_CONNECTI
6db0: 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 ON_CLOSE);..}...
6dc0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
6dd0: 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 73 out_processing_s
6de0: 74 61 72 74 28 66 64 29 3b 0a 0a 09 70 61 74 68 tart(fd);...path
6df0: 20 3d 20 72 65 71 75 65 73 74 2d 3e 70 61 74 68 = request->path
6e00: 3b 0a 09 73 74 72 63 70 79 28 6c 6f 67 2d 3e 62 ;..strcpy(log->b
6e10: 75 66 66 65 72 2c 20 70 61 74 68 29 3b 0a 09 6c uffer, path);..l
6e20: 6f 67 2d 3e 6d 65 74 68 6f 64 20 3d 20 72 65 71 og->method = req
6e30: 75 65 73 74 2d 3e 6d 65 74 68 6f 64 3b 0a 0a 09 uest->method;...
6e40: 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 /* If the reques
6e50: 74 65 64 20 70 61 74 68 20 69 73 20 61 20 64 69 ted path is a di
6e60: 72 65 63 74 6f 72 79 2c 20 72 65 64 69 72 65 63 rectory, redirec
6e70: 74 20 74 6f 20 69 6e 64 65 78 20 70 61 67 65 20 t to index page
6e80: 2a 2f 0a 09 69 66 20 28 72 65 71 75 65 73 74 2d */..if (request-
6e90: 3e 74 79 70 65 20 3d 3d 20 46 49 4c 45 44 5f 52 >type == FILED_R
6ea0: 45 51 55 45 53 54 5f 54 59 50 45 5f 44 49 52 45 EQUEST_TYPE_DIRE
6eb0: 43 54 4f 52 59 29 20 7b 0a 09 09 66 69 6c 65 64 CTORY) {...filed
6ec0: 5f 72 65 64 69 72 65 63 74 5f 69 6e 64 65 78 28 _redirect_index(
6ed0: 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 fp, date_current
6ee0: 2c 20 70 61 74 68 2c 20 6c 6f 67 29 3b 0a 0a 09 , path, log);...
6ef0: 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f .return(FILED_CO
6f00: 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b NNECTION_CLOSE);
6f10: 0a 09 7d 0a 0a 09 66 69 6c 65 69 6e 66 6f 20 3d ..}...fileinfo =
6f20: 20 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 filed_open_file
6f30: 28 70 61 74 68 2c 20 26 72 65 71 75 65 73 74 2d (path, &request-
6f40: 3e 66 69 6c 65 69 6e 66 6f 29 3b 0a 09 69 66 20 >fileinfo);..if
6f50: 28 66 69 6c 65 69 6e 66 6f 20 3d 3d 20 4e 55 4c (fileinfo == NUL
6f60: 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65 72 72 L) {...filed_err
6f70: 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 or_page(fp, date
6f80: 5f 63 75 72 72 65 6e 74 2c 20 34 30 34 2c 20 72 _current, 404, r
6f90: 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c 20 equest->method,
6fa0: 22 6f 70 65 6e 5f 66 61 69 6c 65 64 22 2c 20 6c "open_failed", l
6fb0: 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 og);....return(F
6fc0: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f ILED_CONNECTION_
6fd0: 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 69 66 20 CLOSE);..}...if
6fe0: 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 (request->header
6ff0: 73 2e 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 29 s.range.present)
7000: 20 7b 0a 09 09 69 66 20 28 72 65 71 75 65 73 74 {...if (request
7010: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
7020: 6f 66 66 73 65 74 20 21 3d 20 30 20 7c 7c 20 72 offset != 0 || r
7030: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
7040: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3e 3d 20 range.length >=
7050: 30 29 20 7b 0a 09 09 09 69 66 20 28 72 65 71 75 0) {....if (requ
7060: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e est->headers.ran
7070: 67 65 2e 6f 66 66 73 65 74 20 3e 3d 20 66 69 6c ge.offset >= fil
7080: 65 69 6e 66 6f 2d 3e 6c 65 6e 29 20 7b 0a 09 09 einfo->len) {...
7090: 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 ..filed_error_pa
70a0: 67 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 ge(fp, date_curr
70b0: 65 6e 74 2c 20 34 31 36 2c 20 72 65 71 75 65 73 ent, 416, reques
70c0: 74 2d 3e 6d 65 74 68 6f 64 2c 20 22 72 61 6e 67 t->method, "rang
70d0: 65 5f 69 6e 76 61 6c 69 64 22 2c 20 6c 6f 67 29 e_invalid", log)
70e0: 3b 0a 0a 09 09 09 09 63 6c 6f 73 65 28 66 69 6c ;......close(fil
70f0: 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 09 09 einfo->fd);.....
7100: 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f .return(FILED_CO
7110: 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b NNECTION_CLOSE);
7120: 0a 09 09 09 7d 0a 0a 09 09 09 69 66 20 28 72 65 ....}.....if (re
7130: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
7140: 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 3d 20 28 ange.length == (
7150: 28 6f 66 66 5f 74 29 20 2d 31 29 29 20 7b 0a 09 (off_t) -1)) {..
7160: 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ...filed_log_msg
7170: 5f 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 6e _debug("Computin
7180: 67 20 6c 65 6e 67 74 68 20 74 6f 20 66 69 74 20 g length to fit
7190: 69 6e 20 62 6f 75 6e 64 73 3a 20 66 69 6c 65 69 in bounds: filei
71a0: 6e 66 6f 2d 3e 6c 65 6e 20 3d 20 25 6c 6c 75 2c nfo->len = %llu,
71b0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
71c0: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d s.range.offset =
71d0: 20 25 6c 6c 75 22 2c 0a 09 09 09 09 09 28 75 6e %llu",......(un
71e0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
71f0: 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 2c ) fileinfo->len,
7200: 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ......(unsigned
7210: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 long long) reque
7220: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
7230: 65 2e 6f 66 66 73 65 74 0a 09 09 09 09 29 3b 0a e.offset.....);.
7240: 0a 09 09 09 09 72 65 71 75 65 73 74 2d 3e 68 65 .....request->he
7250: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 aders.range.leng
7260: 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c th = fileinfo->l
7270: 65 6e 20 2d 20 72 65 71 75 65 73 74 2d 3e 68 65 en - request->he
7280: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
7290: 65 74 3b 0a 09 09 09 7d 0a 0a 09 09 09 66 69 6c et;....}.....fil
72a0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
72b0: 28 22 50 61 72 74 69 61 6c 20 72 65 71 75 65 73 ("Partial reques
72c0: 74 2c 20 73 74 61 72 74 69 6e 67 20 61 74 3a 20 t, starting at:
72d0: 25 6c 6c 75 20 61 6e 64 20 72 75 6e 6e 69 6e 67 %llu and running
72e0: 20 66 6f 72 20 25 6c 6c 69 20 62 79 74 65 73 22 for %lli bytes"
72f0: 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ,.....(unsigned
7300: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 long long) reque
7310: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
7320: 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 09 28 6c e.offset,.....(l
7330: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 ong long) reques
7340: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
7350: 2e 6c 65 6e 67 74 68 0a 09 09 09 29 3b 0a 0a 09 .length....);...
7360: 09 7d 0a 0a 09 09 68 74 74 70 5f 63 6f 64 65 20 .}....http_code
7370: 3d 20 32 30 36 3b 0a 09 7d 20 65 6c 73 65 20 7b = 206;..} else {
7380: 0a 09 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 ...http_code = 2
7390: 30 30 3b 0a 0a 09 09 2f 2a 20 43 6f 6d 70 75 74 00;..../* Comput
73a0: 65 20 66 61 6b 65 20 72 61 6e 67 65 20 70 61 72 e fake range par
73b0: 61 6d 65 74 65 72 73 20 74 68 61 74 20 69 6e 63 ameters that inc
73c0: 6c 75 64 65 73 20 74 68 65 20 65 6e 74 69 72 65 ludes the entire
73d0: 20 66 69 6c 65 20 2a 2f 0a 09 09 72 65 71 75 65 file */...reque
73e0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
73f0: 65 2e 6f 66 66 73 65 74 20 3d 20 30 3b 0a 09 09 e.offset = 0;...
7400: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
7410: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20 .range.length =
7420: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 09 fileinfo->len;..
7430: 7d 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20 }...fprintf(fp,
7440: 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c "HTTP/1.1 %i OK\
7450: 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 r\nDate: %s\r\nS
7460: 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e erver: filed\r\n
7470: 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 Last-Modified: %
7480: 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e s\r\nContent-Len
7490: 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 41 63 63 gth: %llu\r\nAcc
74a0: 65 70 74 2d 52 61 6e 67 65 73 3a 20 62 79 74 65 ept-Ranges: byte
74b0: 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 70 s\r\nContent-Typ
74c0: 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74 e: %s\r\nConnect
74d0: 69 6f 6e 3a 20 25 73 5c 72 5c 6e 45 54 61 67 3a ion: %s\r\nETag:
74e0: 20 5c 22 25 73 5c 22 5c 72 5c 6e 22 2c 0a 09 09 \"%s\"\r\n",...
74f0: 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64 61 74 http_code,...dat
7500: 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 66 69 6c e_current,...fil
7510: 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d 6f 64 2c 0a einfo->lastmod,.
7520: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
7530: 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e long) request->
7540: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 headers.range.le
7550: 6e 67 74 68 2c 0a 09 09 66 69 6c 65 69 6e 66 6f ngth,...fileinfo
7560: 2d 3e 74 79 70 65 2c 0a 09 09 66 69 6c 65 64 5f ->type,...filed_
7570: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 28 72 connection_str(r
7580: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
7590: 63 6f 6e 6e 65 63 74 69 6f 6e 29 2c 0a 09 09 66 connection),...f
75a0: 69 6c 65 69 6e 66 6f 2d 3e 65 74 61 67 0a 09 29 ileinfo->etag..)
75b0: 3b 0a 0a 09 69 66 20 28 68 74 74 70 5f 63 6f 64 ;...if (http_cod
75c0: 65 20 3d 3d 20 32 30 36 29 20 7b 0a 09 09 66 70 e == 206) {...fp
75d0: 72 69 6e 74 66 28 66 70 2c 20 22 43 6f 6e 74 65 rintf(fp, "Conte
75e0: 6e 74 2d 52 61 6e 67 65 3a 20 62 79 74 65 73 20 nt-Range: bytes
75f0: 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c 6c 75 5c 72 %llu-%llu/%llu\r
7600: 5c 6e 22 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 \n",....(unsigne
7610: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 d long long) req
7620: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
7630: 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 28 nge.offset,....(
7640: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
7650: 6e 67 29 20 28 72 65 71 75 65 73 74 2d 3e 68 65 ng) (request->he
7660: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
7670: 65 74 20 2b 20 72 65 71 75 65 73 74 2d 3e 68 65 et + request->he
7680: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 aders.range.leng
7690: 74 68 20 2d 20 31 29 2c 0a 09 09 09 28 75 6e 73 th - 1),....(uns
76a0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
76b0: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 0a 09 fileinfo->len..
76c0: 09 29 3b 0a 09 7d 0a 09 66 70 72 69 6e 74 66 28 .);..}..fprintf(
76d0: 66 70 2c 20 22 5c 72 5c 6e 22 29 3b 0a 09 66 66 fp, "\r\n");..ff
76e0: 6c 75 73 68 28 66 70 29 3b 0a 0a 09 6c 6f 67 2d lush(fp);...log-
76f0: 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 68 74 74 >http_code = htt
7700: 70 5f 63 6f 64 65 3b 0a 09 6c 6f 67 2d 3e 72 65 p_code;..log->re
7710: 61 73 6f 6e 20 3d 20 22 4f 4b 22 3b 0a 09 6c 6f ason = "OK";..lo
7720: 67 2d 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 74 g->starttime = t
7730: 69 6d 65 28 4e 55 4c 4c 29 3b 0a 09 6c 6f 67 2d ime(NULL);..log-
7740: 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 72 65 >req_offset = re
7750: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
7760: 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 6c 6f ange.offset;..lo
7770: 67 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20 3d 20 g->req_length =
7780: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
7790: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09 .range.length;..
77a0: 6c 6f 67 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 68 log->file_length
77b0: 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e = fileinfo->len
77c0: 3b 0a 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f ;..#ifdef FILED_
77d0: 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 69 NONBLOCK_HTTP..i
77e0: 6e 74 20 73 6f 63 6b 65 74 5f 66 6c 61 67 73 3b nt socket_flags;
77f0: 0a 09 66 64 5f 73 65 74 20 72 66 64 2c 20 77 66 ..fd_set rfd, wf
7800: 64 3b 0a 09 63 68 61 72 20 73 69 6e 6b 62 75 66 d;..char sinkbuf
7810: 5b 38 31 39 32 5d 3b 0a 09 73 73 69 7a 65 5f 74 [8192];..ssize_t
7820: 20 72 65 61 64 5f 72 65 74 3b 0a 0a 09 46 44 5f read_ret;...FD_
7830: 5a 45 52 4f 28 26 72 66 64 29 3b 0a 09 46 44 5f ZERO(&rfd);..FD_
7840: 5a 45 52 4f 28 26 77 66 64 29 3b 0a 09 46 44 5f ZERO(&wfd);..FD_
7850: 53 45 54 28 66 64 2c 20 26 72 66 64 29 3b 0a 09 SET(fd, &rfd);..
7860: 46 44 5f 53 45 54 28 66 64 2c 20 26 77 66 64 29 FD_SET(fd, &wfd)
7870: 3b 0a 0a 09 73 6f 63 6b 65 74 5f 66 6c 61 67 73 ;...socket_flags
7880: 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 = fcntl(fd, F_G
7890: 45 54 46 4c 29 3b 0a 09 66 63 6e 74 6c 28 66 64 ETFL);..fcntl(fd
78a0: 2c 20 46 5f 53 45 54 46 4c 2c 20 73 6f 63 6b 65 , F_SETFL, socke
78b0: 74 5f 66 6c 61 67 73 20 7c 20 4f 5f 4e 4f 4e 42 t_flags | O_NONB
78c0: 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 0a 09 73 LOCK);.#endif..s
78d0: 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 20 3d endfile_offset =
78e0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
78f0: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a s.range.offset;.
7900: 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 20 .sendfile_len =
7910: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
7920: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09 .range.length;..
7930: 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 3d 20 sendfile_sent =
7940: 30 3b 0a 09 77 68 69 6c 65 20 28 72 65 71 75 65 0;..while (reque
7950: 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 3d 20 46 49 st->method == FI
7960: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 LED_REQUEST_METH
7970: 4f 44 5f 47 45 54 29 20 7b 0a 09 09 69 66 20 28 OD_GET) {...if (
7980: 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3e 20 46 sendfile_len > F
7990: 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41 ILED_SENDFILE_MA
79a0: 58 29 20 7b 0a 09 09 09 73 65 6e 64 66 69 6c 65 X) {....sendfile
79b0: 5f 73 69 7a 65 20 3d 20 46 49 4c 45 44 5f 53 45 _size = FILED_SE
79c0: 4e 44 46 49 4c 45 5f 4d 41 58 3b 0a 09 09 7d 20 NDFILE_MAX;...}
79d0: 65 6c 73 65 20 7b 0a 09 09 09 73 65 6e 64 66 69 else {....sendfi
79e0: 6c 65 5f 73 69 7a 65 20 3d 20 73 65 6e 64 66 69 le_size = sendfi
79f0: 6c 65 5f 6c 65 6e 3b 0a 09 09 7d 0a 0a 09 09 73 le_len;...}....s
7a00: 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20 73 65 endfile_ret = se
7a10: 6e 64 66 69 6c 65 28 66 64 2c 20 66 69 6c 65 69 ndfile(fd, filei
7a20: 6e 66 6f 2d 3e 66 64 2c 20 26 73 65 6e 64 66 69 nfo->fd, &sendfi
7a30: 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 66 le_offset, sendf
7a40: 69 6c 65 5f 73 69 7a 65 29 3b 0a 09 09 69 66 20 ile_size);...if
7a50: 28 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3c 3d (sendfile_ret <=
7a60: 20 30 29 20 7b 0a 23 69 66 64 65 66 20 46 49 4c 0) {.#ifdef FIL
7a70: 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50 ED_NONBLOCK_HTTP
7a80: 0a 09 09 09 69 66 20 28 65 72 72 6e 6f 20 3d 3d ....if (errno ==
7a90: 20 45 41 47 41 49 4e 29 20 7b 0a 09 09 09 09 73 EAGAIN) {.....s
7aa0: 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20 30 3b endfile_ret = 0;
7ab0: 0a 0a 09 09 09 09 77 68 69 6c 65 20 28 31 29 20 ......while (1)
7ac0: 7b 0a 09 09 09 09 09 73 65 6c 65 63 74 28 66 64 {......select(fd
7ad0: 20 2b 20 31 2c 20 26 72 66 64 2c 20 26 77 66 64 + 1, &rfd, &wfd
7ae0: 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 09 , NULL, NULL);..
7af0: 09 09 09 09 69 66 20 28 46 44 5f 49 53 53 45 54 ....if (FD_ISSET
7b00: 28 66 64 2c 20 26 72 66 64 29 29 20 7b 0a 09 09 (fd, &rfd)) {...
7b10: 09 09 09 09 72 65 61 64 5f 72 65 74 20 3d 20 72 ....read_ret = r
7b20: 65 61 64 28 66 64 2c 20 73 69 6e 6b 62 75 66 2c ead(fd, sinkbuf,
7b30: 20 73 69 7a 65 6f 66 28 73 69 6e 6b 62 75 66 29 sizeof(sinkbuf)
7b40: 29 3b 0a 0a 09 09 09 09 09 09 69 66 20 28 72 65 );........if (re
7b50: 61 64 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 ad_ret <= 0) {..
7b60: 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 ......break;....
7b70: 09 09 09 7d 0a 09 09 09 09 09 7d 0a 0a 09 09 09 ...}......}.....
7b80: 09 09 69 66 20 28 46 44 5f 49 53 53 45 54 28 66 ..if (FD_ISSET(f
7b90: 64 2c 20 26 77 66 64 29 29 20 7b 0a 09 09 09 09 d, &wfd)) {.....
7ba0: 09 09 72 65 61 64 5f 72 65 74 20 3d 20 31 3b 0a ..read_ret = 1;.
7bb0: 0a 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 .......break;...
7bc0: 09 09 09 7d 0a 09 09 09 09 7d 0a 0a 09 09 09 09 ...}.....}......
7bd0: 69 66 20 28 72 65 61 64 5f 72 65 74 20 3c 3d 20 if (read_ret <=
7be0: 30 29 20 7b 0a 09 09 09 09 09 62 72 65 61 6b 3b 0) {......break;
7bf0: 0a 09 09 09 09 7d 0a 09 09 09 7d 20 65 6c 73 65 .....}....} else
7c00: 20 7b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 {.....break;...
7c10: 09 7d 0a 23 65 6c 73 65 0a 09 09 09 62 72 65 61 .}.#else....brea
7c20: 6b 3b 0a 23 65 6e 64 69 66 0a 09 09 7d 0a 0a 09 k;.#endif...}...
7c30: 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 2d 3d .sendfile_len -=
7c40: 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 sendfile_ret;..
7c50: 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 2b .sendfile_sent +
7c60: 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a = sendfile_ret;.
7c70: 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c ..if (sendfile_l
7c80: 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72 en == 0) {....br
7c90: 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 6c 6f eak;...}..}...lo
7ca0: 67 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 28 74 69 g->endtime = (ti
7cb0: 6d 65 5f 74 29 20 2d 31 3b 0a 09 6c 6f 67 2d 3e me_t) -1;..log->
7cc0: 73 65 6e 74 5f 6c 65 6e 67 74 68 20 3d 20 73 65 sent_length = se
7cd0: 6e 64 66 69 6c 65 5f 73 65 6e 74 3b 0a 0a 09 66 ndfile_sent;...f
7ce0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c iled_log_entry(l
7cf0: 6f 67 29 3b 0a 0a 09 63 6c 6f 73 65 28 66 69 6c og);...close(fil
7d00: 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 69 66 einfo->fd);...if
7d10: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 (request->heade
7d20: 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 21 3d rs.connection !=
7d30: 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f FILED_CONNECTIO
7d40: 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 29 20 7b 0a N_KEEP_ALIVE) {.
7d50: 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
7d60: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 64 29 3b meout_close(fd);
7d70: 0a 0a 09 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a ....fclose(fp);.
7d80: 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f ...return(FILED_
7d90: 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 CONNECTION_CLOSE
7da0: 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f );..}...filed_so
7db0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 ckettimeout_proc
7dc0: 65 73 73 69 6e 67 5f 65 6e 64 28 66 64 29 3b 0a essing_end(fd);.
7dd0: 0a 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 ..return(FILED_C
7de0: 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 ONNECTION_KEEP_A
7df0: 4c 49 56 45 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e LIVE);.}../* Han
7e00: 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 6e dle incoming con
7e10: 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 nections */.stat
7e20: 69 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f 77 ic void *filed_w
7e30: 6f 72 6b 65 72 5f 74 68 72 65 61 64 28 76 6f 69 orker_thread(voi
7e40: 64 20 2a 61 72 67 5f 76 29 20 7b 0a 09 73 74 72 d *arg_v) {..str
7e50: 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 uct filed_worker
7e60: 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 _thread_args *ar
7e70: 67 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 g;..struct filed
7e80: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 72 65 _http_request re
7e90: 71 75 65 73 74 3b 0a 09 73 74 72 75 63 74 20 66 quest;..struct f
7ea0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
7eb0: 6c 6f 67 2c 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 log, local_dummy
7ec0: 5f 6c 6f 67 3b 0a 09 73 74 72 75 63 74 20 66 69 _log;..struct fi
7ed0: 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 led_options *opt
7ee0: 69 6f 6e 73 3b 0a 09 73 74 72 75 63 74 20 73 6f ions;..struct so
7ef0: 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 64 72 3b ckaddr_in6 addr;
7f00: 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 72 ..socklen_t addr
7f10: 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 69 6c 75 72 len;..int failur
7f20: 65 5f 63 6f 75 6e 74 20 3d 20 30 2c 20 6d 61 78 e_count = 0, max
7f30: 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d _failure_count =
7f40: 20 46 49 4c 45 44 5f 4d 41 58 5f 46 41 49 4c 55 FILED_MAX_FAILU
7f50: 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 RE_COUNT;..int c
7f60: 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65 20 onnection_state
7f70: 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 = FILED_CONNECTI
7f80: 4f 4e 5f 43 4c 4f 53 45 3b 0a 09 69 6e 74 20 6d ON_CLOSE;..int m
7f90: 61 73 74 65 72 5f 66 64 2c 20 66 64 20 3d 20 2d aster_fd, fd = -
7fa0: 31 3b 0a 0a 09 2f 2a 20 52 65 61 64 20 61 72 67 1;.../* Read arg
7fb0: 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 72 67 20 3d uments */..arg =
7fc0: 20 61 72 67 5f 76 3b 0a 0a 09 6d 61 73 74 65 72 arg_v;...master
7fd0: 5f 66 64 20 3d 20 61 72 67 2d 3e 66 64 3b 0a 09 _fd = arg->fd;..
7fe0: 6f 70 74 69 6f 6e 73 20 3d 20 26 61 72 67 2d 3e options = &arg->
7ff0: 6f 70 74 69 6f 6e 73 3b 0a 0a 09 77 68 69 6c 65 options;...while
8000: 20 28 31 29 20 7b 0a 09 09 2f 2a 20 46 61 69 6c (1) {.../* Fail
8010: 75 72 65 20 6c 6f 6f 70 20 70 72 65 76 65 6e 74 ure loop prevent
8020: 69 6f 6e 20 2a 2f 0a 09 09 69 66 20 28 66 61 69 ion */...if (fai
8030: 6c 75 72 65 5f 63 6f 75 6e 74 20 3e 20 6d 61 78 lure_count > max
8040: 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 29 20 _failure_count)
8050: 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a {....break;...}.
8060: 0a 09 09 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 .../* Allocate a
8070: 20 6e 65 77 20 6c 6f 67 20 62 75 66 66 65 72 20 new log buffer
8080: 2a 2f 0a 09 09 6c 6f 67 20 3d 20 66 69 6c 65 64 */...log = filed
8090: 5f 6c 6f 67 5f 6e 65 77 28 31 29 3b 0a 09 09 69 _log_new(1);...i
80a0: 66 20 28 6c 6f 67 20 3d 3d 20 4e 55 4c 4c 29 20 f (log == NULL)
80b0: 7b 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d {....filed_log_m
80c0: 73 67 28 22 41 4c 4c 4f 43 41 54 45 5f 4c 4f 47 sg("ALLOCATE_LOG
80d0: 5f 4d 53 47 5f 46 41 49 4c 45 44 22 29 3b 0a 0a _MSG_FAILED");..
80e0: 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09 ...break;...}...
80f0: 09 6c 6f 67 2d 3e 74 79 70 65 20 3d 20 46 49 4c .log->type = FIL
8100: 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54 52 41 4e ED_LOG_TYPE_TRAN
8110: 53 46 45 52 3b 0a 0a 09 09 2f 2a 20 49 66 20 77 SFER;..../* If w
8120: 65 20 63 6c 6f 73 65 64 20 74 68 65 20 6f 6c 64 e closed the old
8130: 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 61 63 63 connection, acc
8140: 65 70 74 20 61 20 6e 65 77 20 6f 6e 65 20 2a 2f ept a new one */
8150: 0a 09 09 69 66 20 28 63 6f 6e 6e 65 63 74 69 6f ...if (connectio
8160: 6e 5f 73 74 61 74 65 20 3d 3d 20 46 49 4c 45 44 n_state == FILED
8170: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 _CONNECTION_CLOS
8180: 45 29 20 7b 0a 09 09 09 2f 2a 20 41 63 63 65 70 E) {..../* Accep
8190: 74 20 61 20 6e 65 77 20 63 6c 69 65 6e 74 20 2a t a new client *
81a0: 2f 0a 09 09 09 61 64 64 72 6c 65 6e 20 3d 20 73 /....addrlen = s
81b0: 69 7a 65 6f 66 28 61 64 64 72 29 3b 0a 0a 09 09 izeof(addr);....
81c0: 09 66 64 20 3d 20 61 63 63 65 70 74 28 6d 61 73 .fd = accept(mas
81d0: 74 65 72 5f 66 64 2c 20 28 73 74 72 75 63 74 20 ter_fd, (struct
81e0: 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64 sockaddr *) &add
81f0: 72 2c 20 26 61 64 64 72 6c 65 6e 29 3b 0a 09 09 r, &addrlen);...
8200: 7d 0a 0a 09 09 2f 2a 0a 09 09 20 2a 20 49 66 20 }..../*... * If
8210: 77 65 20 66 61 69 6c 2c 20 6d 61 6b 65 20 61 20 we fail, make a
8220: 6e 6f 74 65 20 6f 66 20 69 74 20 73 6f 20 77 65 note of it so we
8230: 20 64 6f 6e 27 74 20 67 6f 20 69 6e 74 6f 20 61 don't go into a
8240: 20 6c 6f 6f 70 20 6f 66 0a 09 09 20 2a 20 61 63 loop of... * ac
8250: 63 65 70 74 28 29 20 66 61 69 6c 69 6e 67 0a 09 cept() failing..
8260: 09 20 2a 2f 0a 09 09 69 66 20 28 66 64 20 3c 20 . */...if (fd <
8270: 30 29 20 7b 0a 09 09 09 2f 2a 20 4c 6f 67 20 74 0) {..../* Log t
8280: 68 65 20 6e 65 77 20 63 6f 6e 6e 65 63 74 69 6f he new connectio
8290: 6e 20 2a 2f 0a 09 09 09 66 69 6c 65 64 5f 6c 6f n */....filed_lo
82a0: 67 5f 6d 73 67 28 22 41 43 43 45 50 54 5f 46 41 g_msg("ACCEPT_FA
82b0: 49 4c 45 44 22 29 3b 0a 0a 09 09 09 66 61 69 6c ILED");.....fail
82c0: 75 72 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 0a 09 09 ure_count++;....
82d0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 66 72 65 65 28 .filed_log_free(
82e0: 6c 6f 67 29 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e log);.....contin
82f0: 75 65 3b 0a 09 09 7d 0a 0a 09 09 66 69 6c 65 64 ue;...}....filed
8300: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 61 _sockettimeout_a
8310: 63 63 65 70 74 28 66 64 29 3b 0a 0a 09 09 2f 2a ccept(fd);..../*
8320: 20 46 69 6c 6c 20 69 6e 20 6c 6f 67 20 73 74 72 Fill in log str
8330: 75 63 74 75 72 65 20 2a 2f 0a 09 09 69 66 20 28 ucture */...if (
8340: 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 28 73 74 filed_log_ip((st
8350: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 ruct sockaddr *)
8360: 20 26 61 64 64 72 2c 20 6c 6f 67 2d 3e 69 70 2c &addr, log->ip,
8370: 20 73 69 7a 65 6f 66 28 6c 6f 67 2d 3e 69 70 29 sizeof(log->ip)
8380: 29 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 ) == NULL) {....
8390: 6c 6f 67 2d 3e 69 70 5b 30 5d 20 3d 20 27 5c 30 log->ip[0] = '\0
83a0: 27 3b 0a 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 20 ';....log->port
83b0: 3d 20 30 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a = 0;...} else {.
83c0: 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 20 3d 20 61 ...log->port = a
83d0: 64 64 72 2e 73 69 6e 36 5f 70 6f 72 74 3b 0a 09 ddr.sin6_port;..
83e0: 09 7d 0a 0a 09 09 2f 2a 20 52 65 73 65 74 20 66 .}..../* Reset f
83f0: 61 69 6c 75 72 65 20 63 6f 75 6e 74 2a 2f 0a 09 ailure count*/..
8400: 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d .failure_count =
8410: 20 30 3b 0a 0a 09 09 2f 2a 20 48 61 6e 64 6c 65 0;..../* Handle
8420: 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 09 63 6f 6e socket */...con
8430: 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65 20 3d 20 nection_state =
8440: 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69 filed_handle_cli
8450: 65 6e 74 28 66 64 2c 20 26 72 65 71 75 65 73 74 ent(fd, &request
8460: 2c 20 6c 6f 67 2c 20 6f 70 74 69 6f 6e 73 29 3b , log, options);
8470: 0a 09 7d 0a 0a 09 2f 2a 20 52 65 70 6f 72 74 20 ..}.../* Report
8480: 65 72 72 6f 72 20 2a 2f 0a 09 66 69 6c 65 64 5f error */..filed_
8490: 6c 6f 67 5f 6d 73 67 28 22 54 48 52 45 41 44 5f log_msg("THREAD_
84a0: 44 49 45 44 20 41 42 4e 4f 52 4d 41 4c 22 29 3b DIED ABNORMAL");
84b0: 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b ...return(NULL);
84c0: 0a 0a 09 2f 2a 20 6c 6f 63 61 6c 5f 64 75 6d 6d .../* local_dumm
84d0: 79 5f 6c 6f 67 20 69 73 20 6f 6e 6c 79 20 75 73 y_log is only us
84e0: 65 64 20 69 66 20 46 49 4c 45 44 5f 44 4f 4e 54 ed if FILED_DONT
84f0: 5f 4c 4f 47 20 69 73 20 65 6e 61 62 6c 65 64 2c _LOG is enabled,
8500: 20 6f 74 68 65 72 77 69 73 65 20 69 74 27 73 20 otherwise it's
8510: 6e 6f 74 20 75 73 65 64 2c 20 62 75 74 20 74 68 not used, but th
8520: 65 20 63 6f 6d 70 69 6c 65 72 20 68 61 74 65 73 e compiler hates
8530: 20 74 68 61 74 20 69 64 65 61 2e 20 2a 2f 0a 09 that idea. */..
8540: 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e local_dummy_log.
8550: 74 79 70 65 20 3d 20 30 3b 0a 09 6c 6f 63 61 6c type = 0;..local
8560: 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 20 _dummy_log.type
8570: 3d 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f = local_dummy_lo
8580: 67 2e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 43 72 g.type;.}../* Cr
8590: 65 61 74 65 20 77 6f 72 6b 65 72 20 74 68 72 65 eate worker thre
85a0: 61 64 73 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e ads */.static in
85b0: 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 t filed_worker_t
85c0: 68 72 65 61 64 73 5f 69 6e 69 74 28 69 6e 74 20 hreads_init(int
85d0: 66 64 2c 20 69 6e 74 20 74 68 72 65 61 64 5f 63 fd, int thread_c
85e0: 6f 75 6e 74 2c 20 73 74 72 75 63 74 20 66 69 6c ount, struct fil
85f0: 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 ed_options *opti
8600: 6f 6e 73 29 20 7b 0a 09 73 74 72 75 63 74 20 66 ons) {..struct f
8610: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 iled_worker_thre
8620: 61 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 70 ad_args *arg;..p
8630: 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 69 thread_t threadi
8640: 64 3b 0a 09 69 6e 74 20 70 74 68 72 65 61 64 5f d;..int pthread_
8650: 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 66 ret;..int i;...f
8660: 6f 72 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 74 or (i = 0; i < t
8670: 68 72 65 61 64 5f 63 6f 75 6e 74 3b 20 69 2b 2b hread_count; i++
8680: 29 20 7b 0a 09 09 61 72 67 20 3d 20 6d 61 6c 6c ) {...arg = mall
8690: 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72 67 29 29 oc(sizeof(*arg))
86a0: 3b 0a 0a 09 09 61 72 67 2d 3e 66 64 20 3d 20 66 ;....arg->fd = f
86b0: 64 3b 0a 09 09 6d 65 6d 63 70 79 28 26 61 72 67 d;...memcpy(&arg
86c0: 2d 3e 6f 70 74 69 6f 6e 73 2c 20 6f 70 74 69 6f ->options, optio
86d0: 6e 73 2c 20 73 69 7a 65 6f 66 28 2a 6f 70 74 69 ns, sizeof(*opti
86e0: 6f 6e 73 29 29 3b 0a 0a 09 09 70 74 68 72 65 61 ons));....pthrea
86f0: 64 5f 72 65 74 20 3d 20 70 74 68 72 65 61 64 5f d_ret = pthread_
8700: 63 72 65 61 74 65 28 26 74 68 72 65 61 64 69 64 create(&threadid
8710: 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 77 6f , NULL, filed_wo
8720: 72 6b 65 72 5f 74 68 72 65 61 64 2c 20 61 72 67 rker_thread, arg
8730: 29 3b 0a 09 09 69 66 20 28 70 74 68 72 65 61 64 );...if (pthread
8740: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 _ret != 0) {....
8750: 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a return(-1);...}.
8760: 09 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a .}...return(0);.
8770: 7d 0a 0a 2f 2a 20 44 69 73 70 6c 61 79 20 68 65 }../* Display he
8780: 6c 70 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 lp */.static voi
8790: 64 20 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 d filed_print_he
87a0: 6c 70 28 46 49 4c 45 20 2a 6f 75 74 70 75 74 2c lp(FILE *output,
87b0: 20 69 6e 74 20 6c 6f 6e 67 5f 68 65 6c 70 2c 20 int long_help,
87c0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 65 78 74 72 const char *extr
87d0: 61 29 20 7b 0a 09 69 66 20 28 65 78 74 72 61 29 a) {..if (extra)
87e0: 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 {...fprintf(out
87f0: 70 75 74 2c 20 22 25 73 5c 6e 22 2c 20 65 78 74 put, "%s\n", ext
8800: 72 61 29 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74 ra);..}...fprint
8810: 66 28 6f 75 74 70 75 74 2c 20 22 55 73 61 67 65 f(output, "Usage
8820: 3a 20 66 69 6c 65 64 20 5b 3c 6f 70 74 69 6f 6e : filed [<option
8830: 73 3e 5d 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 s>]\n");..fprint
8840: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 4f 70 74 f(output, " Opt
8850: 69 6f 6e 73 3a 5c 6e 22 29 3b 0a 09 66 70 72 69 ions:\n");..fpri
8860: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
8870: 20 20 20 2d 68 2c 20 2d 2d 68 65 6c 70 5c 6e 22 -h, --help\n"
8880: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
8890: 75 74 2c 20 22 20 20 20 20 20 20 2d 64 2c 20 2d ut, " -d, -
88a0: 2d 64 61 65 6d 6f 6e 5c 6e 22 29 3b 0a 09 66 70 -daemon\n");..fp
88b0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
88c0: 20 20 20 20 20 2d 76 2c 20 2d 2d 76 65 72 73 69 -v, --versi
88d0: 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 on\n");..fprintf
88e0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
88f0: 2d 56 2c 20 2d 2d 76 68 6f 73 74 5c 6e 22 29 3b -V, --vhost\n");
8900: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
8910: 2c 20 22 20 20 20 20 20 20 2d 62 20 3c 61 64 64 , " -b <add
8920: 72 65 73 73 3e 2c 20 2d 2d 62 69 6e 64 20 3c 61 ress>, --bind <a
8930: 64 64 72 65 73 73 3e 5c 6e 22 29 3b 0a 09 66 70 ddress>\n");..fp
8940: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
8950: 20 20 20 20 20 2d 70 20 3c 70 6f 72 74 3e 2c 20 -p <port>,
8960: 2d 2d 70 6f 72 74 20 3c 70 6f 72 74 3e 5c 6e 22 --port <port>\n"
8970: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
8980: 75 74 2c 20 22 20 20 20 20 20 20 2d 74 20 3c 63 ut, " -t <c
8990: 6f 75 6e 74 3e 2c 20 2d 2d 74 68 72 65 61 64 73 ount>, --threads
89a0: 20 3c 63 6f 75 6e 74 3e 5c 6e 22 29 3b 0a 09 66 <count>\n");..f
89b0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
89c0: 20 20 20 20 20 20 2d 63 20 3c 65 6e 74 72 69 65 -c <entrie
89d0: 73 3e 2c 20 2d 2d 63 61 63 68 65 20 3c 65 6e 74 s>, --cache <ent
89e0: 72 69 65 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 ries>\n");..fpri
89f0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
8a00: 20 20 20 2d 6c 20 3c 66 69 6c 65 3e 2c 20 2d 2d -l <file>, --
8a10: 6c 6f 67 20 3c 66 69 6c 65 3e 5c 6e 22 29 3b 0a log <file>\n");.
8a20: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
8a30: 20 22 20 20 20 20 20 20 2d 75 20 3c 75 73 65 72 " -u <user
8a40: 3e 2c 20 2d 2d 75 73 65 72 20 3c 75 73 65 72 3e >, --user <user>
8a50: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f \n");..fprintf(o
8a60: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 72 utput, " -r
8a70: 20 3c 64 69 72 65 63 74 6f 72 79 3e 2c 20 2d 2d <directory>, --
8a80: 72 6f 6f 74 20 3c 64 69 72 65 63 74 6f 72 79 3e root <directory>
8a90: 5c 6e 22 29 3b 0a 0a 09 69 66 20 28 6c 6f 6e 67 \n");...if (long
8aa0: 5f 68 65 6c 70 29 20 7b 0a 09 09 66 70 72 69 6e _help) {...fprin
8ab0: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 tf(output, "\n")
8ac0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
8ad0: 75 74 2c 20 22 20 20 55 73 61 67 65 3a 5c 6e 22 ut, " Usage:\n"
8ae0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
8af0: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 68 20 28 put, " -h (
8b00: 6f 72 20 2d 2d 68 65 6c 70 29 20 70 72 69 6e 74 or --help) print
8b10: 73 20 74 68 69 73 20 75 73 61 67 65 20 69 6e 66 s this usage inf
8b20: 6f 72 6d 61 74 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 ormation.\n");..
8b30: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
8b40: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 "\n");...fprint
8b50: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
8b60: 20 2d 64 20 28 6f 72 20 2d 2d 64 61 65 6d 6f 6e -d (or --daemon
8b70: 29 20 69 6e 73 74 72 75 63 74 73 20 66 69 6c 65 ) instructs file
8b80: 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 64 61 d to become a da
8b90: 65 6d 6f 6e 20 61 66 74 65 72 20 69 6e 69 74 69 emon after initi
8ba0: 61 6c 69 7a 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 alizing\n");...f
8bb0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
8bc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8bd0: 20 20 20 20 20 20 20 74 68 65 20 6c 69 73 74 65 the liste
8be0: 6e 69 6e 67 20 54 43 50 20 73 6f 63 6b 65 74 20 ning TCP socket
8bf0: 61 6e 64 20 6c 6f 67 20 66 69 6c 65 73 2e 5c 6e and log files.\n
8c00: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
8c10: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 tput, "\n");...f
8c20: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
8c30: 20 20 20 20 20 20 2d 76 20 28 6f 72 20 2d 2d 76 -v (or --v
8c40: 65 72 73 69 6f 6e 29 20 69 6e 73 74 72 75 63 74 ersion) instruct
8c50: 73 20 66 69 6c 65 64 20 70 72 69 6e 74 20 6f 75 s filed print ou
8c60: 74 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 t the version nu
8c70: 6d 62 65 72 20 61 6e 64 20 65 78 69 74 2e 5c 6e mber and exit.\n
8c80: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
8c90: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 tput, "\n");...f
8ca0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
8cb0: 20 20 20 20 20 20 2d 56 20 28 6f 72 20 2d 2d 76 -V (or --v
8cc0: 68 6f 73 74 29 20 69 6e 73 74 72 75 63 74 73 20 host) instructs
8cd0: 66 69 6c 65 64 20 74 6f 20 70 72 65 70 65 6e 64 filed to prepend
8ce0: 20 61 6c 6c 20 72 65 71 75 65 73 74 73 20 77 69 all requests wi
8cf0: 74 68 20 74 68 65 69 72 20 48 54 54 50 5c 6e 22 th their HTTP\n"
8d00: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
8d10: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
8d20: 20 20 20 20 20 20 20 20 20 20 20 20 48 6f 73 74 Host
8d30: 20 68 65 61 64 65 72 2e 5c 6e 22 29 3b 0a 09 09 header.\n");...
8d40: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
8d50: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
8d60: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
8d70: 2d 62 20 28 6f 72 20 2d 2d 62 69 6e 64 29 20 73 -b (or --bind) s
8d80: 70 65 63 69 66 69 65 73 20 74 68 65 20 61 64 64 pecifies the add
8d90: 72 65 73 73 20 74 6f 20 6c 69 73 74 65 6e 20 66 ress to listen f
8da0: 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 48 54 54 50 or incoming HTTP
8db0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
8dc0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
8dd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
8de0: 71 75 65 73 74 73 20 6f 6e 2e 20 20 54 68 65 20 quests on. The
8df0: 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 default value is
8e00: 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 42 49 4e \"%s\".\n", BIN
8e10: 44 5f 41 44 44 52 29 3b 0a 09 09 66 70 72 69 6e D_ADDR);...fprin
8e20: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 tf(output, "\n")
8e30: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
8e40: 75 74 2c 20 22 20 20 20 20 20 20 2d 70 20 28 6f ut, " -p (o
8e50: 72 20 2d 2d 70 6f 72 74 29 20 73 70 65 63 69 66 r --port) specif
8e60: 69 65 73 20 74 68 65 20 54 43 50 20 70 6f 72 74 ies the TCP port
8e70: 20 6e 75 6d 62 65 72 20 74 6f 20 6c 69 73 74 65 number to liste
8e80: 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 48 n for incoming H
8e90: 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e TTP\n");...fprin
8ea0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
8eb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8ec0: 20 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 20 54 requests on. T
8ed0: 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 75 he default is %u
8ee0: 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 .\n", (unsigned
8ef0: 69 6e 74 29 20 50 4f 52 54 29 3b 0a 09 09 66 70 int) PORT);...fp
8f00: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
8f10: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
8f20: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 74 utput, " -t
8f30: 20 28 6f 72 20 2d 2d 74 68 72 65 61 64 73 29 20 (or --threads)
8f40: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 specifies the nu
8f50: 6d 62 65 72 20 6f 66 20 77 6f 72 6b 65 72 20 74 mber of worker t
8f60: 68 72 65 61 64 73 20 74 6f 20 63 72 65 61 74 65 hreads to create
8f70: 2e 20 45 61 63 68 5c 6e 22 29 3b 0a 09 09 66 70 . Each\n");...fp
8f80: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
8f90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8fa0: 20 20 20 20 20 20 20 77 6f 72 6b 65 72 20 74 68 worker th
8fb0: 72 65 61 64 20 63 61 6e 20 73 65 72 76 69 63 65 read can service
8fc0: 20 6f 6e 65 20 63 6f 6e 63 75 72 72 65 6e 74 20 one concurrent
8fd0: 48 54 54 50 20 73 65 73 73 69 6f 6e 2e 5c 6e 22 HTTP session.\n"
8fe0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
8ff0: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
9000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 Th
9010: 75 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 us the number of
9020: 20 74 68 72 65 61 64 73 20 63 72 65 61 74 65 64 threads created
9030: 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 20 will determine
9040: 68 6f 77 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e how\n");...fprin
9050: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
9060: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9070: 20 20 20 20 6d 61 6e 79 20 73 69 6d 75 6c 74 61 many simulta
9080: 6e 65 6f 75 73 20 74 72 61 6e 73 66 65 72 73 20 neous transfers
9090: 77 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c 65 will be possible
90a0: 2e 20 54 68 65 5c 6e 22 29 3b 0a 09 09 66 70 72 . The\n");...fpr
90b0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
90c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
90d0: 20 20 20 20 20 20 64 65 66 61 75 6c 74 20 69 73 default is
90e0: 20 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 %lu.\n", (unsig
90f0: 6e 65 64 20 6c 6f 6e 67 29 20 54 48 52 45 41 44 ned long) THREAD
9100: 5f 43 4f 55 4e 54 29 3b 0a 09 09 66 70 72 69 6e _COUNT);...fprin
9110: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 tf(output, "\n")
9120: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
9130: 75 74 2c 20 22 20 20 20 20 20 20 2d 63 20 28 6f ut, " -c (o
9140: 72 20 2d 2d 63 61 63 68 65 29 20 73 70 65 63 69 r --cache) speci
9150: 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 fies the number
9160: 6f 66 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 of file informat
9170: 69 6f 6e 20 63 61 63 68 65 20 65 6e 74 72 69 65 ion cache entrie
9180: 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 s\n");...fprintf
9190: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
91a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
91b0: 74 6f 20 61 6c 6c 6f 63 61 74 65 2e 20 20 45 61 to allocate. Ea
91c0: 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 68 ch cache entry h
91d0: 6f 6c 64 73 20 66 69 6c 65 20 69 6e 66 6f 72 6d olds file inform
91e0: 61 74 69 6f 6e 20 61 73 5c 6e 22 29 3b 0a 09 09 ation as\n");...
91f0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
9200: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
9210: 20 20 20 20 20 20 20 77 65 6c 6c 20 61 73 20 61 well as a
9220: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 n open file desc
9230: 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20 66 69 riptor to the fi
9240: 6c 65 2c 20 73 6f 20 72 65 73 6f 75 72 63 65 5c le, so resource\
9250: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
9260: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
9270: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69 li
9280: 6d 69 74 73 20 28 69 2e 65 2e 2c 20 75 6c 69 6d mits (i.e., ulim
9290: 69 74 29 20 73 68 6f 75 6c 64 20 62 65 20 63 6f it) should be co
92a0: 6e 73 69 64 65 72 65 64 2e 20 20 54 68 69 73 20 nsidered. This
92b0: 73 68 6f 75 6c 64 5c 6e 22 29 3b 0a 09 09 66 70 should\n");...fp
92c0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
92d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
92e0: 20 20 20 20 20 62 65 20 61 20 70 72 69 6d 65 20 be a prime
92f0: 6e 75 6d 62 65 72 20 66 6f 72 20 69 64 65 61 6c number for ideal
9300: 20 75 73 65 20 77 69 74 68 20 74 68 65 20 6c 6f use with the lo
9310: 6f 6b 75 70 20 6d 65 74 68 6f 64 2e 5c 6e 22 29 okup method.\n")
9320: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
9330: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
9340: 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 64 The d
9350: 65 66 61 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e efault is %lu.\n
9360: 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ", (unsigned lon
9370: 67 29 20 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a g) CACHE_SIZE);.
9380: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
9390: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e , "\n");...fprin
93a0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
93b0: 20 20 2d 6c 20 28 6f 72 20 2d 2d 6c 6f 67 29 20 -l (or --log)
93c0: 73 70 65 63 69 66 69 65 73 20 61 20 66 69 6c 65 specifies a file
93d0: 6e 61 6d 65 20 74 6f 20 6f 70 65 6e 20 66 6f 72 name to open for
93e0: 20 77 72 69 74 69 6e 67 20 6c 6f 67 20 65 6e 74 writing log ent
93f0: 72 69 65 73 2e 20 20 4c 6f 67 5c 6e 22 29 3b 0a ries. Log\n");.
9400: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
9410: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
9420: 20 20 20 20 20 20 20 65 6e 74 72 69 65 73 20 61 entries a
9430: 72 65 20 6d 61 64 65 20 66 6f 72 20 76 61 72 69 re made for vari
9440: 6f 75 73 20 73 74 61 67 65 73 20 69 6e 20 74 72 ous stages in tr
9450: 61 6e 73 66 65 72 69 6e 67 20 66 69 6c 65 73 2e ansfering files.
9460: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
9470: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
9480: 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 The
9490: 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 6f 70 65 log file is ope
94a0: 6e 65 64 20 62 65 66 6f 72 65 20 73 77 69 74 63 ned before switc
94b0: 68 69 6e 67 20 75 73 65 72 73 20 28 73 65 65 20 hing users (see
94c0: 5c 22 2d 75 5c 22 29 5c 6e 22 29 3b 0a 09 09 66 \"-u\")\n");...f
94d0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
94e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
94f0: 20 20 20 20 61 6e 64 20 72 6f 6f 74 20 64 69 72 and root dir
9500: 65 63 74 6f 72 69 65 73 20 28 73 65 65 20 5c 22 ectories (see \"
9510: 2d 72 5c 22 29 2e 20 20 54 68 65 20 6c 6f 67 20 -r\"). The log
9520: 66 69 6c 65 20 69 73 20 6e 65 76 65 72 5c 6e 22 file is never\n"
9530: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
9540: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
9550: 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 64 closed
9560: 20 73 6f 20 6c 6f 67 20 72 6f 74 61 74 69 6f 6e so log rotation
9570: 20 77 69 74 68 6f 75 74 20 73 74 6f 70 70 69 6e without stoppin
9580: 67 20 74 68 65 20 64 61 65 6d 6f 6e 20 69 73 20 g the daemon is
9590: 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 will\n");...fpri
95a0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
95b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
95c0: 20 6e 6f 74 20 77 6f 72 6b 2e 20 20 54 68 65 20 not work. The
95d0: 76 61 6c 75 65 20 6f 66 20 5c 22 2d 5c 22 20 69 value of \"-\" i
95e0: 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 73 74 ndicates that st
95f0: 61 6e 64 61 72 64 20 6f 75 74 70 75 74 5c 6e 22 andard output\n"
9600: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
9610: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
9620: 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64 should
9630: 20 62 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 67 be used for log
9640: 67 69 6e 67 2e 20 20 49 66 20 74 68 65 20 66 69 ging. If the fi
9650: 6c 65 6e 61 6d 65 20 62 65 67 69 6e 73 20 77 69 lename begins wi
9660: 74 68 20 61 5c 6e 22 29 3b 0a 09 09 66 70 72 69 th a\n");...fpri
9670: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9690: 20 70 69 70 65 20 28 5c 22 7c 5c 22 29 20 74 68 pipe (\"|\") th
96a0: 65 6e 20 61 20 70 72 6f 63 65 73 73 20 69 73 20 en a process is
96b0: 73 74 61 72 74 65 64 20 61 6e 64 20 75 73 65 64 started and used
96c0: 20 66 6f 72 20 6c 6f 67 67 69 6e 67 5c 6e 22 29 for logging\n")
96d0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
96e0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
96f0: 20 20 20 20 20 20 20 20 20 69 6e 73 74 65 61 64 instead
9700: 20 6f 66 20 61 20 66 69 6c 65 2e 20 20 54 68 65 of a file. The
9710: 20 64 65 66 61 75 6c 74 20 69 73 20 5c 22 25 73 default is \"%s
9720: 5c 22 2e 5c 6e 22 2c 20 4c 4f 47 5f 46 49 4c 45 \".\n", LOG_FILE
9730: 29 3b 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f );.#ifdef FILED_
9740: 44 4f 4e 54 5f 4c 4f 47 0a 09 09 66 70 72 69 6e DONT_LOG...fprin
9750: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
9760: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9770: 4e 6f 74 65 20 74 68 61 74 20 6c 6f 67 67 69 6e Note that loggin
9780: 67 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 g is completely
9790: 64 69 73 61 62 6c 65 64 20 73 6f 20 74 68 69 73 disabled so this
97a0: 20 6f 70 74 69 6f 6e 20 64 6f 65 73 5c 6e 22 29 option does\n")
97b0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
97c0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
97d0: 20 20 20 20 20 20 20 20 20 6e 6f 74 68 69 6e 67 nothing
97e0: 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2e 5c in this build.\
97f0: 6e 22 29 3b 0a 23 65 6e 64 69 66 0a 09 09 66 70 n");.#endif...fp
9800: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
9810: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
9820: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 75 utput, " -u
9830: 20 28 6f 72 20 2d 2d 75 73 65 72 29 20 73 70 65 (or --user) spe
9840: 63 69 66 69 65 73 20 74 68 65 20 75 73 65 72 20 cifies the user
9850: 74 6f 20 73 77 69 74 63 68 20 75 73 65 72 20 49 to switch user I
9860: 44 73 20 74 6f 20 62 65 66 6f 72 65 20 73 65 72 Ds to before ser
9870: 76 69 63 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 vicing\n");...fp
9880: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
9890: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
98a0: 20 20 20 20 72 65 71 75 65 73 74 73 2e 20 20 54 requests. T
98b0: 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 6e 6f he default is no
98c0: 74 20 63 68 61 6e 67 65 20 75 73 65 72 20 49 44 t change user ID
98d0: 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 s.\n");...fprint
98e0: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
98f0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9900: 74 2c 20 22 20 20 20 20 20 20 2d 72 20 28 6f 72 t, " -r (or
9910: 20 2d 2d 72 6f 6f 74 29 20 73 70 65 63 69 66 69 --root) specifi
9920: 65 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 es the directory
9930: 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72 to act as the r
9940: 6f 6f 74 20 64 69 72 65 63 74 6f 72 79 20 66 6f oot directory fo
9950: 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 r\n");...fprintf
9960: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
9970: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
9980: 68 65 20 66 69 6c 65 20 73 65 72 76 65 72 2e 20 he file server.
9990: 20 49 66 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 If this option
99a0: 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 63 68 is specified, ch
99b0: 72 6f 6f 74 28 32 29 5c 6e 22 29 3b 0a 09 09 66 root(2)\n");...f
99c0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
99d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
99e0: 20 20 20 20 20 69 73 20 63 61 6c 6c 65 64 2e 20 is called.
99f0: 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 The default is
9a00: 6e 6f 74 20 63 68 61 6e 67 65 20 72 6f 6f 74 20 not change root
9a10: 64 69 72 65 63 74 6f 72 69 65 73 2c 5c 6e 22 29 directories,\n")
9a20: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
9a30: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
9a40: 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 that i
9a50: 73 2c 20 74 68 65 20 5c 22 2f 5c 22 20 64 69 72 s, the \"/\" dir
9a60: 65 63 74 6f 72 79 20 69 73 20 73 68 61 72 65 64 ectory is shared
9a70: 20 6f 75 74 2e 20 20 54 68 69 73 20 77 69 6c 6c out. This will
9a80: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
9a90: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
9aa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69 li
9ab0: 6b 65 6c 79 20 62 65 20 61 20 73 65 63 75 72 69 kely be a securi
9ac0: 74 79 20 69 73 73 75 65 2c 20 73 6f 20 74 68 69 ty issue, so thi
9ad0: 73 20 6f 70 74 69 6f 6e 20 73 68 6f 75 6c 64 20 s option should
9ae0: 61 6c 77 61 79 73 5c 6e 22 29 3b 0a 09 09 66 70 always\n");...fp
9af0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
9b00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b10: 20 20 20 20 62 65 20 75 73 65 64 2e 5c 6e 22 29 be used.\n")
9b20: 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d ;..}...return;.}
9b30: 0a 0a 2f 2a 20 41 64 64 20 61 20 67 65 74 6f 70 ../* Add a getop
9b40: 74 20 6f 70 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 t option */.stat
9b50: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 ic void filed_ge
9b60: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 topt_long_setopt
9b70: 28 73 74 72 75 63 74 20 6f 70 74 69 6f 6e 20 2a (struct option *
9b80: 6f 70 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 opt, const char
9b90: 2a 6e 61 6d 65 2c 20 69 6e 74 20 68 61 73 5f 61 *name, int has_a
9ba0: 72 67 2c 20 69 6e 74 20 76 61 6c 29 20 7b 0a 09 rg, int val) {..
9bb0: 6f 70 74 2d 3e 6e 61 6d 65 20 20 20 20 20 3d 20 opt->name =
9bc0: 6e 61 6d 65 3b 0a 09 6f 70 74 2d 3e 68 61 73 5f name;..opt->has_
9bd0: 61 72 67 20 20 3d 20 68 61 73 5f 61 72 67 3b 0a arg = has_arg;.
9be0: 09 6f 70 74 2d 3e 66 6c 61 67 20 20 20 20 20 3d .opt->flag =
9bf0: 20 4e 55 4c 4c 3b 0a 09 6f 70 74 2d 3e 76 61 6c NULL;..opt->val
9c00: 20 20 20 20 20 20 3d 20 76 61 6c 3b 0a 0a 09 72 = val;...r
9c10: 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 73 eturn;.}../* Res
9c20: 6f 6c 76 65 20 61 20 75 73 65 72 6e 61 6d 65 20 olve a username
9c30: 74 6f 20 61 20 55 49 44 20 2a 2f 0a 73 74 61 74 to a UID */.stat
9c40: 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 75 73 65 ic int filed_use
9c50: 72 5f 6c 6f 6f 6b 75 70 28 63 6f 6e 73 74 20 63 r_lookup(const c
9c60: 68 61 72 20 2a 75 73 65 72 2c 20 75 69 64 5f 74 har *user, uid_t
9c70: 20 2a 75 73 65 72 5f 69 64 29 20 7b 0a 09 63 68 *user_id) {..ch
9c80: 61 72 20 2a 6e 65 78 74 3b 0a 09 75 69 64 5f 74 ar *next;..uid_t
9c90: 20 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 3b 0a user_id_check;.
9ca0: 23 69 66 6e 64 65 66 20 46 49 4c 45 44 5f 4e 4f #ifndef FILED_NO
9cb0: 5f 47 45 54 50 57 4e 41 4d 0a 09 73 74 72 75 63 _GETPWNAM..struc
9cc0: 74 20 70 61 73 73 77 64 20 2a 65 6e 74 3b 0a 0a t passwd *ent;..
9cd0: 09 65 6e 74 20 3d 20 67 65 74 70 77 6e 61 6d 28 .ent = getpwnam(
9ce0: 75 73 65 72 29 3b 0a 09 69 66 20 28 65 6e 74 20 user);..if (ent
9cf0: 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 75 73 != NULL) {...*us
9d00: 65 72 5f 69 64 20 3d 20 65 6e 74 2d 3e 70 77 5f er_id = ent->pw_
9d10: 75 69 64 3b 0a 0a 09 09 72 65 74 75 72 6e 28 30 uid;....return(0
9d20: 29 3b 0a 09 7d 0a 23 65 6e 64 69 66 0a 0a 09 75 );..}.#endif...u
9d30: 73 65 72 5f 69 64 5f 63 68 65 63 6b 20 3d 20 73 ser_id_check = s
9d40: 74 72 74 6f 75 6c 6c 28 75 73 65 72 2c 20 26 6e trtoull(user, &n
9d50: 65 78 74 2c 20 31 30 29 3b 0a 09 69 66 20 28 6e ext, 10);..if (n
9d60: 65 78 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 ext == NULL) {..
9d70: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a .return(1);..}..
9d80: 09 69 66 20 28 6e 65 78 74 5b 30 5d 20 21 3d 20 .if (next[0] !=
9d90: 27 5c 30 27 29 20 7b 0a 09 09 72 65 74 75 72 6e '\0') {...return
9da0: 28 31 29 3b 0a 09 7d 0a 0a 09 2a 75 73 65 72 5f (1);..}...*user_
9db0: 69 64 20 3d 20 75 73 65 72 5f 69 64 5f 63 68 65 id = user_id_che
9dc0: 63 6b 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b ck;...return(0);
9dd0: 0a 7d 0a 0a 2f 2a 20 44 61 65 6d 6f 6e 69 7a 65 .}../* Daemonize
9de0: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 */.static int f
9df0: 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 76 iled_daemonize(v
9e00: 6f 69 64 29 20 7b 0a 09 70 69 64 5f 74 20 73 65 oid) {..pid_t se
9e10: 74 73 69 64 5f 72 65 74 2c 20 66 6f 72 6b 5f 72 tsid_ret, fork_r
9e20: 65 74 3b 0a 09 69 6e 74 20 63 68 64 69 72 5f 72 et;..int chdir_r
9e30: 65 74 2c 20 64 75 70 32 5f 72 65 74 3b 0a 09 69 et, dup2_ret;..i
9e40: 6e 74 20 66 64 5f 69 6e 2c 20 66 64 5f 6f 75 74 nt fd_in, fd_out
9e50: 3b 0a 0a 09 63 68 64 69 72 5f 72 65 74 20 3d 20 ;...chdir_ret =
9e60: 63 68 64 69 72 28 22 2f 22 29 3b 0a 09 69 66 20 chdir("/");..if
9e70: 28 63 68 64 69 72 5f 72 65 74 20 21 3d 20 30 29 (chdir_ret != 0)
9e80: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a {...return(1);.
9e90: 09 7d 0a 0a 09 66 6f 72 6b 5f 72 65 74 20 3d 20 .}...fork_ret =
9ea0: 66 6f 72 6b 28 29 3b 0a 09 69 66 20 28 66 6f 72 fork();..if (for
9eb0: 6b 5f 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 72 k_ret < 0) {...r
9ec0: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 eturn(1);..}...i
9ed0: 66 20 28 66 6f 72 6b 5f 72 65 74 20 3e 20 30 29 f (fork_ret > 0)
9ee0: 20 7b 0a 09 09 2f 2a 20 50 61 72 65 6e 74 20 2a {.../* Parent *
9ef0: 2f 0a 09 09 77 61 69 74 70 69 64 28 66 6f 72 6b /...waitpid(fork
9f00: 5f 72 65 74 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a _ret, NULL, 0);.
9f10: 0a 09 09 65 78 69 74 28 45 58 49 54 5f 53 55 43 ...exit(EXIT_SUC
9f20: 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 CESS);..}.../* C
9f30: 68 69 6c 64 20 2a 2f 0a 09 69 66 20 28 66 6f 72 hild */..if (for
9f40: 6b 28 29 20 21 3d 20 30 29 20 7b 0a 09 09 2f 2a k() != 0) {.../*
9f50: 20 43 68 69 6c 64 20 2a 2f 0a 09 09 65 78 69 74 Child */...exit
9f60: 28 45 58 49 54 5f 53 55 43 43 45 53 53 29 3b 0a (EXIT_SUCCESS);.
9f70: 09 7d 0a 0a 09 2f 2a 20 47 72 61 6e 64 20 63 68 .}.../* Grand ch
9f80: 69 6c 64 20 2a 2f 0a 09 73 65 74 73 69 64 5f 72 ild */..setsid_r
9f90: 65 74 20 3d 20 73 65 74 73 69 64 28 29 3b 0a 09 et = setsid();..
9fa0: 69 66 20 28 73 65 74 73 69 64 5f 72 65 74 20 3d if (setsid_ret =
9fb0: 3d 20 28 28 70 69 64 5f 74 29 20 2d 31 29 29 20 = ((pid_t) -1))
9fc0: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 {...return(1);..
9fd0: 7d 0a 0a 09 66 64 5f 69 6e 20 3d 20 6f 70 65 6e }...fd_in = open
9fe0: 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f ("/dev/null", O_
9ff0: 52 44 4f 4e 4c 59 29 3b 0a 09 66 64 5f 6f 75 74 RDONLY);..fd_out
a000: 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 = open("/dev/nu
a010: 6c 6c 22 2c 20 4f 5f 57 52 4f 4e 4c 59 29 3b 0a ll", O_WRONLY);.
a020: 09 69 66 20 28 66 64 5f 69 6e 20 3c 20 30 20 7c .if (fd_in < 0 |
a030: 7c 20 66 64 5f 6f 75 74 20 3c 20 30 29 20 7b 0a | fd_out < 0) {.
a040: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
a050: 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70 ..dup2_ret = dup
a060: 32 28 66 64 5f 69 6e 2c 20 53 54 44 49 4e 5f 46 2(fd_in, STDIN_F
a070: 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 ILENO);..if (dup
a080: 32 5f 72 65 74 20 21 3d 20 53 54 44 49 4e 5f 46 2_ret != STDIN_F
a090: 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 ILENO) {...retur
a0a0: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f n(1);..}...dup2_
a0b0: 72 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f 75 ret = dup2(fd_ou
a0c0: 74 2c 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f t, STDOUT_FILENO
a0d0: 29 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74 );..if (dup2_ret
a0e0: 20 21 3d 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e != STDOUT_FILEN
a0f0: 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 O) {...return(1)
a100: 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 ;..}...dup2_ret
a110: 3d 20 64 75 70 32 28 66 64 5f 6f 75 74 2c 20 53 = dup2(fd_out, S
a120: 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29 3b 0a 09 TDERR_FILENO);..
a130: 69 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 if (dup2_ret !=
a140: 53 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29 20 7b STDERR_FILENO) {
a150: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d ...return(1);..}
a160: 0a 0a 09 63 6c 6f 73 65 28 66 64 5f 69 6e 29 3b ...close(fd_in);
a170: 0a 09 63 6c 6f 73 65 28 66 64 5f 6f 75 74 29 3b ..close(fd_out);
a180: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a ...return(0);.}.
a190: 0a 2f 2a 20 52 75 6e 20 70 72 6f 63 65 73 73 20 ./* Run process
a1a0: 2a 2f 0a 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20 */.int main(int
a1b0: 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 argc, char **arg
a1c0: 76 29 20 7b 0a 09 73 74 72 75 63 74 20 6f 70 74 v) {..struct opt
a1d0: 69 6f 6e 20 6f 70 74 69 6f 6e 73 5b 31 32 5d 3b ion options[12];
a1e0: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f ..struct filed_o
a1f0: 70 74 69 6f 6e 73 20 74 68 72 65 61 64 5f 6f 70 ptions thread_op
a200: 74 69 6f 6e 73 3b 0a 09 63 6f 6e 73 74 20 63 68 tions;..const ch
a210: 61 72 20 2a 62 69 6e 64 5f 61 64 64 72 20 3d 20 ar *bind_addr =
a220: 42 49 4e 44 5f 41 44 44 52 2c 20 2a 6e 65 77 72 BIND_ADDR, *newr
a230: 6f 6f 74 20 3d 20 4e 55 4c 4c 2c 20 2a 6c 6f 67 oot = NULL, *log
a240: 5f 66 69 6c 65 20 3d 20 4c 4f 47 5f 46 49 4c 45 _file = LOG_FILE
a250: 3b 0a 09 46 49 4c 45 20 2a 6c 6f 67 5f 66 70 3b ;..FILE *log_fp;
a260: 0a 09 75 69 64 5f 74 20 75 73 65 72 20 3d 20 30 ..uid_t user = 0
a270: 3b 0a 09 69 6e 74 20 70 6f 72 74 20 3d 20 50 4f ;..int port = PO
a280: 52 54 2c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 RT, thread_count
a290: 20 3d 20 54 48 52 45 41 44 5f 43 4f 55 4e 54 3b = THREAD_COUNT;
a2a0: 0a 09 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 ..int cache_size
a2b0: 20 3d 20 43 41 43 48 45 5f 53 49 5a 45 3b 0a 09 = CACHE_SIZE;..
a2c0: 69 6e 74 20 69 6e 69 74 5f 72 65 74 2c 20 63 68 int init_ret, ch
a2d0: 72 6f 6f 74 5f 72 65 74 2c 20 73 65 74 75 69 64 root_ret, setuid
a2e0: 5f 72 65 74 2c 20 6c 6f 6f 6b 75 70 5f 72 65 74 _ret, lookup_ret
a2f0: 2c 20 63 68 64 69 72 5f 72 65 74 3b 0a 09 69 6e , chdir_ret;..in
a300: 74 20 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 t setuid_enabled
a310: 20 3d 20 30 2c 20 64 61 65 6d 6f 6e 5f 65 6e 61 = 0, daemon_ena
a320: 62 6c 65 64 20 3d 20 30 3b 0a 09 69 6e 74 20 63 bled = 0;..int c
a330: 68 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 09 2f 2a h;..int fd;.../*
a340: 20 53 65 74 20 64 65 66 61 75 6c 74 20 76 61 6c Set default val
a350: 75 65 73 20 2a 2f 0a 09 74 68 72 65 61 64 5f 6f ues */..thread_o
a360: 70 74 69 6f 6e 73 2e 76 68 6f 73 74 73 5f 65 6e ptions.vhosts_en
a370: 61 62 6c 65 64 20 3d 20 30 3b 0a 0a 09 2f 2a 20 abled = 0;.../*
a380: 50 72 6f 63 65 73 73 20 61 72 67 75 6d 65 6e 74 Process argument
a390: 73 20 2a 2f 0a 09 66 69 6c 65 64 5f 67 65 74 6f s */..filed_geto
a3a0: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
a3b0: 6f 70 74 69 6f 6e 73 5b 30 5d 2c 20 22 70 6f 72 options[0], "por
a3c0: 74 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 t", required_arg
a3d0: 75 6d 65 6e 74 2c 20 27 70 27 29 3b 0a 09 66 69 ument, 'p');..fi
a3e0: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f led_getopt_long_
a3f0: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b setopt(&options[
a400: 31 5d 2c 20 22 74 68 72 65 61 64 73 22 2c 20 72 1], "threads", r
a410: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 equired_argument
a420: 2c 20 27 74 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 't');..filed_g
a430: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
a440: 74 28 26 6f 70 74 69 6f 6e 73 5b 32 5d 2c 20 22 t(&options[2], "
a450: 63 61 63 68 65 22 2c 20 72 65 71 75 69 72 65 64 cache", required
a460: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 63 27 29 3b _argument, 'c');
a470: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c ..filed_getopt_l
a480: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 ong_setopt(&opti
a490: 6f 6e 73 5b 33 5d 2c 20 22 62 69 6e 64 22 2c 20 ons[3], "bind",
a4a0: 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e required_argumen
a4b0: 74 2c 20 27 62 27 29 3b 0a 09 66 69 6c 65 64 5f t, 'b');..filed_
a4c0: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f getopt_long_seto
a4d0: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 34 5d 2c 20 pt(&options[4],
a4e0: 22 75 73 65 72 22 2c 20 72 65 71 75 69 72 65 64 "user", required
a4f0: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 75 27 29 3b _argument, 'u');
a500: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c ..filed_getopt_l
a510: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 ong_setopt(&opti
a520: 6f 6e 73 5b 35 5d 2c 20 22 72 6f 6f 74 22 2c 20 ons[5], "root",
a530: 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e required_argumen
a540: 74 2c 20 27 72 27 29 3b 0a 09 66 69 6c 65 64 5f t, 'r');..filed_
a550: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f getopt_long_seto
a560: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 36 5d 2c 20 pt(&options[6],
a570: 22 68 65 6c 70 22 2c 20 6e 6f 5f 61 72 67 75 6d "help", no_argum
a580: 65 6e 74 2c 20 27 68 27 29 3b 0a 09 66 69 6c 65 ent, 'h');..file
a590: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 d_getopt_long_se
a5a0: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 37 5d topt(&options[7]
a5b0: 2c 20 22 64 61 65 6d 6f 6e 22 2c 20 6e 6f 5f 61 , "daemon", no_a
a5c0: 72 67 75 6d 65 6e 74 2c 20 27 64 27 29 3b 0a 09 rgument, 'd');..
a5d0: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
a5e0: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
a5f0: 73 5b 38 5d 2c 20 22 6c 6f 67 22 2c 20 72 65 71 s[8], "log", req
a600: 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 uired_argument,
a610: 27 6c 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 'l');..filed_get
a620: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 opt_long_setopt(
a630: 26 6f 70 74 69 6f 6e 73 5b 39 5d 2c 20 22 76 65 &options[9], "ve
a640: 72 73 69 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d rsion", no_argum
a650: 65 6e 74 2c 20 27 76 27 29 3b 0a 09 66 69 6c 65 ent, 'v');..file
a660: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 d_getopt_long_se
a670: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 30 topt(&options[10
a680: 5d 2c 20 22 76 68 6f 73 74 22 2c 20 6e 6f 5f 61 ], "vhost", no_a
a690: 72 67 75 6d 65 6e 74 2c 20 27 56 27 29 3b 0a 09 rgument, 'V');..
a6a0: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
a6b0: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
a6c0: 73 5b 31 31 5d 2c 20 4e 55 4c 4c 2c 20 30 2c 20 s[11], NULL, 0,
a6d0: 30 29 3b 0a 09 77 68 69 6c 65 20 28 28 63 68 20 0);..while ((ch
a6e0: 3d 20 67 65 74 6f 70 74 5f 6c 6f 6e 67 28 61 72 = getopt_long(ar
a6f0: 67 63 2c 20 61 72 67 76 2c 20 22 70 3a 74 3a 63 gc, argv, "p:t:c
a700: 3a 62 3a 75 3a 72 3a 6c 3a 68 64 76 56 22 2c 20 :b:u:r:l:hdvV",
a710: 6f 70 74 69 6f 6e 73 2c 20 4e 55 4c 4c 29 29 20 options, NULL))
a720: 21 3d 20 2d 31 29 20 7b 0a 09 09 73 77 69 74 63 != -1) {...switc
a730: 68 28 63 68 29 20 7b 0a 09 09 09 63 61 73 65 20 h(ch) {....case
a740: 27 70 27 3a 0a 09 09 09 09 70 6f 72 74 20 3d 20 'p':.....port =
a750: 61 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 atoi(optarg);...
a760: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
a770: 20 27 74 27 3a 0a 09 09 09 09 74 68 72 65 61 64 't':.....thread
a780: 5f 63 6f 75 6e 74 20 3d 20 61 74 6f 69 28 6f 70 _count = atoi(op
a790: 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b targ);.....break
a7a0: 3b 0a 09 09 09 63 61 73 65 20 27 63 27 3a 0a 09 ;....case 'c':..
a7b0: 09 09 09 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 ...cache_size =
a7c0: 61 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 atoi(optarg);...
a7d0: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
a7e0: 20 27 62 27 3a 0a 09 09 09 09 62 69 6e 64 5f 61 'b':.....bind_a
a7f0: 64 64 72 20 3d 20 73 74 72 64 75 70 28 6f 70 74 ddr = strdup(opt
a800: 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b arg);.....break;
a810: 0a 09 09 09 63 61 73 65 20 27 75 27 3a 0a 09 09 ....case 'u':...
a820: 09 09 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 ..setuid_enabled
a830: 20 3d 20 31 3b 0a 09 09 09 09 6c 6f 6f 6b 75 70 = 1;.....lookup
a840: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 75 73 65 _ret = filed_use
a850: 72 5f 6c 6f 6f 6b 75 70 28 6f 70 74 61 72 67 2c r_lookup(optarg,
a860: 20 26 75 73 65 72 29 3b 0a 09 09 09 09 69 66 20 &user);.....if
a870: 28 6c 6f 6f 6b 75 70 5f 72 65 74 20 21 3d 20 30 (lookup_ret != 0
a880: 29 20 7b 0a 09 09 09 09 09 66 69 6c 65 64 5f 70 ) {......filed_p
a890: 72 69 6e 74 5f 68 65 6c 70 28 73 74 64 65 72 72 rint_help(stderr
a8a0: 2c 20 30 2c 20 22 49 6e 76 61 6c 69 64 20 75 73 , 0, "Invalid us
a8b0: 65 72 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64 ername specified
a8c0: 22 29 3b 0a 0a 09 09 09 09 09 72 65 74 75 72 6e ");.......return
a8d0: 28 31 29 3b 0a 09 09 09 09 7d 0a 09 09 09 09 62 (1);.....}.....b
a8e0: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 72 reak;....case 'r
a8f0: 27 3a 0a 09 09 09 09 6e 65 77 72 6f 6f 74 20 3d ':.....newroot =
a900: 20 73 74 72 64 75 70 28 6f 70 74 61 72 67 29 3b strdup(optarg);
a910: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 .....break;....c
a920: 61 73 65 20 27 6c 27 3a 0a 09 09 09 09 6c 6f 67 ase 'l':.....log
a930: 5f 66 69 6c 65 20 3d 20 73 74 72 64 75 70 28 6f _file = strdup(o
a940: 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 ptarg);.....brea
a950: 6b 3b 0a 09 09 09 63 61 73 65 20 27 64 27 3a 0a k;....case 'd':.
a960: 09 09 09 09 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c ....daemon_enabl
a970: 65 64 20 3d 20 31 3b 0a 09 09 09 09 62 72 65 61 ed = 1;.....brea
a980: 6b 3b 0a 09 09 09 63 61 73 65 20 27 56 27 3a 0a k;....case 'V':.
a990: 09 09 09 09 74 68 72 65 61 64 5f 6f 70 74 69 6f ....thread_optio
a9a0: 6e 73 2e 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 ns.vhosts_enable
a9b0: 64 20 3d 20 31 3b 0a 0a 09 09 09 09 62 72 65 61 d = 1;......brea
a9c0: 6b 3b 0a 09 09 09 63 61 73 65 20 27 76 27 3a 0a k;....case 'v':.
a9d0: 09 09 09 09 70 72 69 6e 74 66 28 22 66 69 6c 65 ....printf("file
a9e0: 64 20 76 65 72 73 69 6f 6e 20 25 73 5c 6e 22 2c d version %s\n",
a9f0: 20 46 49 4c 45 44 5f 56 45 52 53 49 4f 4e 29 3b FILED_VERSION);
aa00: 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 30 29 3b ......return(0);
aa10: 0a 09 09 09 63 61 73 65 20 27 3f 27 3a 0a 09 09 ....case '?':...
aa20: 09 63 61 73 65 20 27 3a 27 3a 0a 09 09 09 09 66 .case ':':.....f
aa30: 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 iled_print_help(
aa40: 73 74 64 65 72 72 2c 20 30 2c 20 4e 55 4c 4c 29 stderr, 0, NULL)
aa50: 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 31 29 ;......return(1)
aa60: 3b 0a 09 09 09 63 61 73 65 20 27 68 27 3a 0a 09 ;....case 'h':..
aa70: 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 ...filed_print_h
aa80: 65 6c 70 28 73 74 64 6f 75 74 2c 20 31 2c 20 4e elp(stdout, 1, N
aa90: 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 ULL);......retur
aaa0: 6e 28 30 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f n(0);...}..}.../
aab0: 2a 20 4f 70 65 6e 20 6c 6f 67 20 66 69 6c 65 20 * Open log file
aac0: 2a 2f 0a 09 6c 6f 67 5f 66 70 20 3d 20 66 69 6c */..log_fp = fil
aad0: 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 6c 6f 67 5f ed_log_open(log_
aae0: 66 69 6c 65 29 3b 0a 09 69 66 20 28 6c 6f 67 5f file);..if (log_
aaf0: 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 fp == NULL) {...
ab00: 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 6f perror("filed_lo
ab10: 67 5f 6f 70 65 6e 22 29 3b 0a 0a 09 09 72 65 74 g_open");....ret
ab20: 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 urn(4);..}.../*
ab30: 43 72 65 61 74 65 20 6c 69 73 74 65 6e 69 6e 67 Create listening
ab40: 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 66 64 20 3d socket */..fd =
ab50: 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 62 69 filed_listen(bi
ab60: 6e 64 5f 61 64 64 72 2c 20 70 6f 72 74 29 3b 0a nd_addr, port);.
ab70: 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 .if (fd < 0) {..
ab80: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c .perror("filed_l
ab90: 69 73 74 65 6e 22 29 3b 0a 0a 09 09 72 65 74 75 isten");....retu
aba0: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 rn(1);..}.../* I
abb0: 6e 69 74 69 61 6c 69 7a 65 20 74 69 6d 65 6f 75 nitialize timeou
abc0: 74 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a t structures */.
abd0: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 .init_ret = file
abe0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
abf0: 69 6e 69 74 28 29 3b 0a 09 69 66 20 28 69 6e 69 init();..if (ini
ac00: 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 t_ret != 0) {...
ac10: 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 73 6f perror("filed_so
ac20: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69 74 ckettimeout_init
ac30: 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 38 29 ");....return(8)
ac40: 3b 0a 09 7d 0a 0a 09 2f 2a 20 42 65 63 6f 6d 65 ;..}.../* Become
ac50: 20 61 20 64 61 65 6d 6f 6e 20 2a 2f 0a 09 69 66 a daemon */..if
ac60: 20 28 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 (daemon_enabled
ac70: 29 20 7b 0a 09 09 69 6e 69 74 5f 72 65 74 20 3d ) {...init_ret =
ac80: 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 filed_daemonize
ac90: 28 29 3b 0a 09 09 69 66 20 28 69 6e 69 74 5f 72 ();...if (init_r
aca0: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 et != 0) {....pe
acb0: 72 72 6f 72 28 22 66 69 6c 65 64 5f 64 61 65 6d rror("filed_daem
acc0: 6f 6e 69 7a 65 22 29 3b 0a 0a 09 09 09 72 65 74 onize");.....ret
acd0: 75 72 6e 28 36 29 3b 0a 09 09 7d 0a 09 7d 0a 0a urn(6);...}..}..
ace0: 09 2f 2a 20 43 68 72 6f 6f 74 2c 20 69 66 20 61 ./* Chroot, if a
acf0: 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09 69 ppropriate */..i
ad00: 66 20 28 6e 65 77 72 6f 6f 74 29 20 7b 0a 09 09 f (newroot) {...
ad10: 63 68 64 69 72 5f 72 65 74 20 3d 20 63 68 64 69 chdir_ret = chdi
ad20: 72 28 6e 65 77 72 6f 6f 74 29 3b 0a 09 09 69 66 r(newroot);...if
ad30: 20 28 63 68 64 69 72 5f 72 65 74 20 21 3d 20 30 (chdir_ret != 0
ad40: 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 63 ) {....perror("c
ad50: 68 64 69 72 22 29 3b 0a 0a 09 09 09 72 65 74 75 hdir");.....retu
ad60: 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 63 68 rn(1);...}....ch
ad70: 72 6f 6f 74 5f 72 65 74 20 3d 20 63 68 72 6f 6f root_ret = chroo
ad80: 74 28 22 2e 22 29 3b 0a 09 09 69 66 20 28 63 68 t(".");...if (ch
ad90: 72 6f 6f 74 5f 72 65 74 20 21 3d 20 30 29 20 7b root_ret != 0) {
ada0: 0a 09 09 09 70 65 72 72 6f 72 28 22 63 68 72 6f ....perror("chro
adb0: 6f 74 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e ot");.....return
adc0: 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a (1);...}..}.../*
add0: 20 44 72 6f 70 20 70 72 69 76 69 6c 65 67 65 73 Drop privileges
ade0: 2c 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 , if appropriate
adf0: 20 2a 2f 0a 09 69 66 20 28 73 65 74 75 69 64 5f */..if (setuid_
ae00: 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 73 65 74 enabled) {...set
ae10: 75 69 64 5f 72 65 74 20 3d 20 73 65 74 75 69 64 uid_ret = setuid
ae20: 28 75 73 65 72 29 3b 0a 09 09 69 66 20 28 73 65 (user);...if (se
ae30: 74 75 69 64 5f 72 65 74 20 21 3d 20 30 29 20 7b tuid_ret != 0) {
ae40: 0a 09 09 09 70 65 72 72 6f 72 28 22 73 65 74 75 ....perror("setu
ae50: 69 64 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e id");.....return
ae60: 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a (1);...}..}.../*
ae70: 20 49 6e 69 74 69 61 6c 69 7a 65 20 2a 2f 0a 09 Initialize */..
ae80: 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 init_ret = filed
ae90: 5f 69 6e 69 74 28 63 61 63 68 65 5f 73 69 7a 65 _init(cache_size
aea0: 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 );..if (init_ret
aeb0: 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f != 0) {...perro
aec0: 72 28 22 66 69 6c 65 64 5f 69 6e 69 74 22 29 3b r("filed_init");
aed0: 0a 0a 09 09 72 65 74 75 72 6e 28 33 29 3b 0a 09 ....return(3);..
aee0: 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 6f }.../* Create lo
aef0: 67 67 69 6e 67 20 74 68 72 65 61 64 20 2a 2f 0a gging thread */.
af00: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 .init_ret = file
af10: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 d_logging_thread
af20: 5f 69 6e 69 74 28 6c 6f 67 5f 66 70 29 3b 0a 09 _init(log_fp);..
af30: 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 if (init_ret !=
af40: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 0) {...perror("f
af50: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 iled_logging_thr
af60: 65 61 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 ead_init");....r
af70: 65 74 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f eturn(4);..}.../
af80: 2a 20 43 72 65 61 74 65 20 73 6f 63 6b 65 74 20 * Create socket
af90: 74 65 72 6d 69 6e 61 74 69 6f 6e 20 74 68 72 65 termination thre
afa0: 61 64 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 ad */..init_ret
afb0: 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 = filed_socketti
afc0: 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e 69 meout_thread_ini
afd0: 74 28 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 t();..if (init_r
afe0: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 et != 0) {...per
aff0: 72 6f 72 28 22 66 69 6c 65 64 5f 73 6f 63 6b 65 ror("filed_socke
b000: 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f ttimeout_thread_
b010: 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 init");....retur
b020: 6e 28 37 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 n(7);..}.../* Cr
b030: 65 61 74 65 20 77 6f 72 6b 65 72 20 74 68 72 65 eate worker thre
b040: 61 64 73 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 ads */..init_ret
b050: 20 3d 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f = filed_worker_
b060: 74 68 72 65 61 64 73 5f 69 6e 69 74 28 66 64 2c threads_init(fd,
b070: 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 2c 20 26 thread_count, &
b080: 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73 29 3b thread_options);
b090: 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 ..if (init_ret !
b0a0: 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 = 0) {...perror(
b0b0: 22 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 "filed_worker_th
b0c0: 72 65 61 64 73 5f 69 6e 69 74 22 29 3b 0a 0a 09 reads_init");...
b0d0: 09 72 65 74 75 72 6e 28 35 29 3b 0a 09 7d 0a 0a .return(5);..}..
b0e0: 09 2f 2a 20 57 61 69 74 20 66 6f 72 20 74 68 72 ./* Wait for thr
b0f0: 65 61 64 73 20 74 6f 20 65 78 69 74 20 2a 2f 0a eads to exit */.
b100: 09 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 4d 6f ./* XXX:TODO: Mo
b110: 6e 69 74 6f 72 20 74 68 72 65 61 64 20 75 73 61 nitor thread usa
b120: 67 65 20 2a 2f 0a 09 77 68 69 6c 65 20 28 31 29 ge */..while (1)
b130: 20 7b 0a 09 09 73 6c 65 65 70 28 36 30 29 3b 0a {...sleep(60);.
b140: 09 7d 0a 0a 09 2f 2a 20 52 65 74 75 72 6e 20 69 .}.../* Return i
b150: 6e 20 66 61 69 6c 75 72 65 20 2a 2f 0a 09 72 65 n failure */..re
b160: 74 75 72 6e 28 32 29 3b 0a 7d 0a turn(2);.}.