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 33 22 0a 23 64 ERSION "1.13".#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 09 6c 6f 6e 67 20 6d 61 78 66 64 _id;..long maxfd
4260: 2c 20 69 64 78 3b 0a 0a 09 6d 61 78 66 64 20 3d , idx;...maxfd =
4270: 20 73 79 73 63 6f 6e 66 28 5f 53 43 5f 4f 50 45 sysconf(_SC_OPE
4280: 4e 5f 4d 41 58 29 3b 0a 09 69 66 20 28 6d 61 78 N_MAX);..if (max
4290: 66 64 20 3c 3d 20 30 29 20 7b 0a 09 09 6d 61 78 fd <= 0) {...max
42a0: 66 64 20 3d 20 34 30 39 36 3b 0a 09 7d 0a 0a 09 fd = 4096;..}...
42b0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
42c0: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 20 3d out_sockstatus =
42d0: 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a malloc(sizeof(*
42e0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
42f0: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 29 20 out_sockstatus)
4300: 2a 20 6d 61 78 66 64 29 3b 0a 09 69 66 20 28 66 * maxfd);..if (f
4310: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
4320: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 20 3d 3d ut_sockstatus ==
4330: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 NULL) {...retur
4340: 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 20 n(-1);..}...for
4350: 28 69 64 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 (idx = 0; idx <
4360: 6d 61 78 66 64 3b 20 69 64 78 2b 2b 29 20 7b 0a maxfd; idx++) {.
4370: 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
4380: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 meout_sockstatus
4390: 5b 69 64 78 5d 2e 76 61 6c 69 64 20 3d 20 66 61 [idx].valid = fa
43a0: 6c 73 65 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f lse;..}...filed_
43b0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f sockettimeout_so
43c0: 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 20 ckstatus_length
43d0: 3d 20 6d 61 78 66 64 3b 0a 0a 09 66 69 6c 65 64 = maxfd;...filed
43e0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 64 _sockettimeout_d
43f0: 65 76 6e 75 6c 6c 5f 66 64 20 3d 20 6f 70 65 6e evnull_fd = open
4400: 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f ("/dev/null", O_
4410: 52 44 57 52 29 3b 0a 09 69 66 20 28 66 69 6c 65 RDWR);..if (file
4420: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
4430: 64 65 76 6e 75 6c 6c 5f 66 64 20 3c 20 30 29 20 devnull_fd < 0)
4440: 7b 0a 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a {...return(-1);.
4450: 09 7d 0a 0a 09 70 74 68 72 65 61 64 5f 63 72 65 .}...pthread_cre
4460: 61 74 65 28 26 74 68 72 65 61 64 5f 69 64 2c 20 ate(&thread_id,
4470: 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 73 6f 63 6b NULL, filed_sock
4480: 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 ettimeout_thread
4490: 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 72 65 74 75 72 , NULL);...retur
44a0: 6e 28 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a n(0);.}.#endif..
44b0: 2f 2a 20 46 6f 72 6d 61 74 20 74 69 6d 65 20 70 /* Format time p
44c0: 65 72 20 52 46 43 32 36 31 36 20 2a 2f 0a 73 74 er RFC2616 */.st
44d0: 61 74 69 63 20 63 68 61 72 20 2a 66 69 6c 65 64 atic char *filed
44e0: 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 68 61 _format_time(cha
44f0: 72 20 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f r *buffer, size_
4500: 74 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 63 6f t buffer_len, co
4510: 6e 73 74 20 74 69 6d 65 5f 74 20 74 69 6d 65 69 nst time_t timei
4520: 6e 66 6f 29 20 7b 0a 09 73 74 72 75 63 74 20 74 nfo) {..struct t
4530: 6d 20 74 69 6d 65 69 6e 66 6f 5f 74 6d 2c 20 2a m timeinfo_tm, *
4540: 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a timeinfo_tm_p;..
4550: 09 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d .timeinfo_tm_p =
4560: 20 67 6d 74 69 6d 65 5f 72 28 26 74 69 6d 65 69 gmtime_r(&timei
4570: 6e 66 6f 2c 20 26 74 69 6d 65 69 6e 66 6f 5f 74 nfo, &timeinfo_t
4580: 6d 29 3b 0a 09 69 66 20 28 74 69 6d 65 69 6e 66 m);..if (timeinf
4590: 6f 5f 74 6d 5f 70 20 3d 3d 20 4e 55 4c 4c 29 20 o_tm_p == NULL)
45a0: 7b 0a 09 09 72 65 74 75 72 6e 28 22 75 6e 6b 6e {...return("unkn
45b0: 6f 77 6e 22 29 3b 0a 09 7d 0a 0a 09 62 75 66 66 own");..}...buff
45c0: 65 72 5b 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 er[buffer_len -
45d0: 31 5d 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 1] = '\0';..buff
45e0: 65 72 5f 6c 65 6e 20 3d 20 73 74 72 66 74 69 6d er_len = strftim
45f0: 65 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 e(buffer, buffer
4600: 5f 6c 65 6e 20 2d 20 31 2c 20 22 25 61 2c 20 25 _len - 1, "%a, %
4610: 64 20 25 62 20 25 59 20 25 48 3a 25 4d 3a 25 53 d %b %Y %H:%M:%S
4620: 20 47 4d 54 22 2c 20 74 69 6d 65 69 6e 66 6f 5f GMT", timeinfo_
4630: 74 6d 5f 70 29 3b 0a 0a 09 72 65 74 75 72 6e 28 tm_p);...return(
4640: 62 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 buffer);.}../* h
4650: 61 73 68 20 2a 2f 0a 73 74 61 74 69 63 20 75 6e ash */.static un
4660: 73 69 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 signed int filed
4670: 5f 68 61 73 68 28 63 6f 6e 73 74 20 75 6e 73 69 _hash(const unsi
4680: 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 75 65 gned char *value
4690: 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d , unsigned int m
46a0: 6f 64 75 6c 75 73 29 20 7b 0a 09 75 6e 73 69 67 odulus) {..unsig
46b0: 6e 65 64 20 63 68 61 72 20 63 75 72 72 2c 20 70 ned char curr, p
46c0: 72 65 76 3b 0a 09 69 6e 74 20 64 69 66 66 3b 0a rev;..int diff;.
46d0: 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 65 .unsigned int re
46e0: 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d tval;...retval =
46f0: 20 6d 6f 64 75 6c 75 73 20 2d 20 31 3b 0a 09 70 modulus - 1;..p
4700: 72 65 76 20 3d 20 6d 6f 64 75 6c 75 73 20 25 20 rev = modulus %
4710: 32 35 35 3b 0a 0a 09 77 68 69 6c 65 20 28 28 63 255;...while ((c
4720: 75 72 72 20 3d 20 2a 76 61 6c 75 65 29 29 20 7b urr = *value)) {
4730: 0a 09 09 69 66 20 28 63 75 72 72 20 3c 20 33 32 ...if (curr < 32
4740: 29 20 7b 0a 09 09 09 63 75 72 72 20 3d 20 32 35 ) {....curr = 25
4750: 35 20 2d 20 63 75 72 72 3b 0a 09 09 7d 20 65 6c 5 - curr;...} el
4760: 73 65 20 7b 0a 09 09 09 63 75 72 72 20 2d 3d 20 se {....curr -=
4770: 33 32 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 70 32;...}....if (p
4780: 72 65 76 20 3c 20 63 75 72 72 29 20 7b 0a 09 09 rev < curr) {...
4790: 09 64 69 66 66 20 3d 20 63 75 72 72 20 2d 20 70 .diff = curr - p
47a0: 72 65 76 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a rev;...} else {.
47b0: 09 09 09 64 69 66 66 20 3d 20 70 72 65 76 20 2d ...diff = prev -
47c0: 20 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 70 72 curr;...}....pr
47d0: 65 76 20 3d 20 63 75 72 72 3b 0a 0a 09 09 72 65 ev = curr;....re
47e0: 74 76 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 65 tval <<= 3;...re
47f0: 74 76 61 6c 20 26 3d 20 30 78 46 46 46 46 46 46 tval &= 0xFFFFFF
4800: 46 46 4c 55 3b 0a 09 09 72 65 74 76 61 6c 20 5e FFLU;...retval ^
4810: 3d 20 64 69 66 66 3b 0a 0a 09 09 76 61 6c 75 65 = diff;....value
4820: 2b 2b 3b 0a 09 7d 0a 0a 09 72 65 74 76 61 6c 20 ++;..}...retval
4830: 3d 20 72 65 74 76 61 6c 20 25 20 6d 6f 64 75 6c = retval % modul
4840: 75 73 3b 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 us;...return(ret
4850: 76 61 6c 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 val);.}../* Find
4860: 20 61 20 6d 69 6d 65 2d 74 79 70 65 20 62 61 73 a mime-type bas
4870: 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 6e 61 ed on the filena
4880: 6d 65 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e me */.static con
4890: 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 64 st char *filed_d
48a0: 65 74 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 etermine_mimetyp
48b0: 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 e(const char *pa
48c0: 74 68 29 20 7b 0a 09 63 6f 6e 73 74 20 63 68 61 th) {..const cha
48d0: 72 20 2a 70 3b 0a 0a 09 70 20 3d 20 73 74 72 72 r *p;...p = strr
48e0: 63 68 72 28 70 61 74 68 2c 20 27 2e 27 29 3b 0a chr(path, '.');.
48f0: 09 69 66 20 28 70 20 3d 3d 20 4e 55 4c 4c 29 20 .if (p == NULL)
4900: 7b 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 {...return(FILED
4910: 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a _DEFAULT_TYPE);.
4920: 09 7d 0a 0a 09 70 2b 2b 3b 0a 09 69 66 20 28 2a .}...p++;..if (*
4930: 70 20 3d 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 p == '\0') {...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: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 filed_log_msg_de
4970: 62 75 67 28 22 4c 6f 6f 6b 69 6e 67 20 75 70 20 bug("Looking up
4980: 4d 49 4d 45 20 74 79 70 65 20 66 6f 72 20 25 73 MIME type for %s
4990: 20 28 68 61 73 68 20 3d 20 25 6c 6c 75 29 22 2c (hash = %llu)",
49a0: 20 70 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f p, (unsigned lo
49b0: 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 64 5f 68 ng long) filed_h
49c0: 61 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 ash((const unsig
49d0: 6e 65 64 20 63 68 61 72 20 2a 29 20 70 2c 20 31 ned char *) p, 1
49e0: 36 37 37 37 32 35 39 29 29 3b 0a 0a 23 69 6e 63 6777259));..#inc
49f0: 6c 75 64 65 20 22 66 69 6c 65 64 2d 6d 69 6d 65 lude "filed-mime
4a00: 2d 74 79 70 65 73 2e 68 22 0a 0a 09 72 65 74 75 -types.h"...retu
4a10: 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 rn(FILED_DEFAULT
4a20: 5f 54 59 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 _TYPE);.}../* Ge
4a30: 6e 65 72 61 74 65 20 61 20 75 6e 69 71 75 65 20 nerate a unique
4a40: 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 73 74 identifier */.st
4a50: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f atic void filed_
4a60: 67 65 6e 65 72 61 74 65 5f 65 74 61 67 28 63 68 generate_etag(ch
4a70: 61 72 20 2a 65 74 61 67 2c 20 73 69 7a 65 5f 74 ar *etag, size_t
4a80: 20 6c 65 6e 67 74 68 29 20 7b 0a 09 73 6e 70 72 length) {..snpr
4a90: 69 6e 74 66 28 65 74 61 67 2c 20 6c 65 6e 67 74 intf(etag, lengt
4aa0: 68 2c 20 22 25 6c 6c 78 2d 25 6c 6c 78 25 6c 6c h, "%llx-%llx%ll
4ab0: 78 25 6c 6c 78 25 6c 6c 78 22 2c 0a 09 09 28 75 x%llx%llx",...(u
4ac0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
4ad0: 67 29 20 74 69 6d 65 28 4e 55 4c 4c 29 2c 0a 09 g) time(NULL),..
4ae0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
4af0: 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a long) random(),.
4b00: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
4b10: 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c long) random(),
4b20: 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
4b30: 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 g long) random()
4b40: 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f ,...(unsigned lo
4b50: 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 ng long) random(
4b60: 29 0a 09 29 3b 0a 7d 0a 0a 2f 2a 20 4f 70 65 6e )..);.}../* Open
4b70: 20 61 20 66 69 6c 65 20 61 6e 64 20 72 65 74 75 a file and retu
4b80: 72 6e 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 rn file informat
4b90: 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 ion */.static st
4ba0: 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 ruct filed_filei
4bb0: 6e 66 6f 20 2a 66 69 6c 65 64 5f 6f 70 65 6e 5f nfo *filed_open_
4bc0: 66 69 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 file(const char
4bd0: 2a 70 61 74 68 2c 20 73 74 72 75 63 74 20 66 69 *path, struct fi
4be0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 62 75 led_fileinfo *bu
4bf0: 66 66 65 72 29 20 7b 0a 09 73 74 72 75 63 74 20 ffer) {..struct
4c00: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a filed_fileinfo *
4c10: 63 61 63 68 65 3b 0a 09 75 6e 73 69 67 6e 65 64 cache;..unsigned
4c20: 20 69 6e 74 20 63 61 63 68 65 5f 69 64 78 3b 0a int cache_idx;.
4c30: 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09 69 6e 74 .off_t len;..int
4c40: 20 66 64 3b 0a 0a 09 63 61 63 68 65 5f 69 64 78 fd;...cache_idx
4c50: 20 3d 20 66 69 6c 65 64 5f 68 61 73 68 28 28 63 = filed_hash((c
4c60: 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
4c70: 61 72 20 2a 29 20 70 61 74 68 2c 20 66 69 6c 65 ar *) path, file
4c80: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
4c90: 68 65 5f 73 69 7a 65 29 3b 0a 0a 09 63 61 63 68 he_size);...cach
4ca0: 65 20 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 e = &filed_filei
4cb0: 6e 66 6f 5f 66 64 63 61 63 68 65 5b 63 61 63 68 nfo_fdcache[cach
4cc0: 65 5f 69 64 78 5d 3b 0a 0a 09 66 69 6c 65 64 5f e_idx];...filed_
4cd0: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 4c log_msg_debug("L
4ce0: 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 ocking mutex for
4cf0: 20 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 idx: %lu", (uns
4d00: 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 igned long) cach
4d10: 65 5f 69 64 78 29 3b 0a 0a 09 70 74 68 72 65 61 e_idx);...pthrea
4d20: 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 63 61 d_mutex_lock(&ca
4d30: 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 66 che->mutex);...f
4d40: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
4d50: 75 67 28 22 43 6f 6d 70 6c 65 74 65 64 20 6c 6f ug("Completed lo
4d60: 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20 cking mutex for
4d70: 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69 idx: %lu", (unsi
4d80: 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 gned long) cache
4d90: 5f 69 64 78 29 3b 0a 0a 09 69 66 20 28 73 74 72 _idx);...if (str
4da0: 63 6d 70 28 70 61 74 68 2c 20 63 61 63 68 65 2d cmp(path, cache-
4db0: 3e 70 61 74 68 29 20 21 3d 20 30 29 20 7b 0a 09 >path) != 0) {..
4dc0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 .filed_log_msg_d
4dd0: 65 62 75 67 28 22 43 61 63 68 65 20 6d 69 73 73 ebug("Cache miss
4de0: 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20 4f for idx: %lu: O
4df0: 4c 44 20 5c 22 25 73 5c 22 2c 20 4e 45 57 20 5c LD \"%s\", NEW \
4e00: 22 25 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e 65 "%s\"", (unsigne
4e10: 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 d long) cache_id
4e20: 78 2c 20 63 61 63 68 65 2d 3e 70 61 74 68 2c 20 x, cache->path,
4e30: 70 61 74 68 29 3b 0a 0a 09 09 66 64 20 3d 20 6f path);....fd = o
4e40: 70 65 6e 28 70 61 74 68 2c 20 4f 5f 52 44 4f 4e pen(path, O_RDON
4e50: 4c 59 20 7c 20 4f 5f 4c 41 52 47 45 46 49 4c 45 LY | O_LARGEFILE
4e60: 29 3b 0a 09 09 69 66 20 28 66 64 20 3c 20 30 29 );...if (fd < 0)
4e70: 20 7b 0a 09 09 09 70 74 68 72 65 61 64 5f 6d 75 {....pthread_mu
4e80: 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 tex_unlock(&cach
4e90: 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09 72 e->mutex);.....r
4ea0: 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d eturn(NULL);...}
4eb0: 0a 0a 09 09 69 66 20 28 63 61 63 68 65 2d 3e 66 ....if (cache->f
4ec0: 64 20 3e 3d 20 30 29 20 7b 0a 09 09 09 63 6c 6f d >= 0) {....clo
4ed0: 73 65 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 se(cache->fd);..
4ee0: 09 7d 0a 0a 09 09 6c 65 6e 20 3d 20 6c 73 65 65 .}....len = lsee
4ef0: 6b 28 66 64 2c 20 30 2c 20 53 45 45 4b 5f 45 4e k(fd, 0, SEEK_EN
4f00: 44 29 3b 0a 09 09 6c 73 65 65 6b 28 66 64 2c 20 D);...lseek(fd,
4f10: 30 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 0a 09 0, SEEK_SET);...
4f20: 09 63 61 63 68 65 2d 3e 66 64 20 3d 20 66 64 3b .cache->fd = fd;
4f30: 0a 09 09 63 61 63 68 65 2d 3e 6c 65 6e 20 3d 20 ...cache->len =
4f40: 6c 65 6e 3b 0a 09 09 73 74 72 63 70 79 28 63 61 len;...strcpy(ca
4f50: 63 68 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 che->path, path)
4f60: 3b 0a 09 09 63 61 63 68 65 2d 3e 74 79 70 65 20 ;...cache->type
4f70: 3d 20 66 69 6c 65 64 5f 64 65 74 65 72 6d 69 6e = filed_determin
4f80: 65 5f 6d 69 6d 65 74 79 70 65 28 70 61 74 68 29 e_mimetype(path)
4f90: 3b 0a 09 09 66 69 6c 65 64 5f 67 65 6e 65 72 61 ;...filed_genera
4fa0: 74 65 5f 65 74 61 67 28 63 61 63 68 65 2d 3e 65 te_etag(cache->e
4fb0: 74 61 67 2c 20 73 69 7a 65 6f 66 28 63 61 63 68 tag, sizeof(cach
4fc0: 65 2d 3e 65 74 61 67 29 29 3b 0a 0a 09 09 2f 2a e->etag));..../*
4fd0: 20 58 58 58 3a 54 4f 44 4f 3a 20 44 65 74 65 72 XXX:TODO: Deter
4fe0: 6d 69 6e 65 20 2a 2f 0a 09 09 63 61 63 68 65 2d mine */...cache-
4ff0: 3e 6c 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 64 >lastmod = filed
5000: 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 63 _format_time(cac
5010: 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 he->lastmod_b, s
5020: 69 7a 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 73 izeof(cache->las
5030: 74 6d 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e 55 tmod_b), time(NU
5040: 4c 4c 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 6c LL) - 30);..} el
5050: 73 65 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 se {...filed_log
5060: 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 _msg_debug("Cach
5070: 65 20 68 69 74 20 66 6f 72 20 69 64 78 3a 20 25 e hit for idx: %
5080: 6c 75 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 22 lu: PATH \"%s\""
5090: 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 , (unsigned long
50a0: 29 20 63 61 63 68 65 5f 69 64 78 2c 20 70 61 74 ) cache_idx, pat
50b0: 68 29 3b 0a 09 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 h);..}.../*.. *
50c0: 57 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 We have to make
50d0: 61 20 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20 a duplicate FD,
50e0: 62 65 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20 because once we
50f0: 72 65 6c 65 61 73 65 20 74 68 65 20 63 61 63 68 release the cach
5100: 65 0a 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 65 e.. * mutex, the
5110: 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
5120: 20 6d 61 79 20 62 65 20 63 6c 6f 73 65 64 0a 09 may be closed..
5130: 20 2a 2f 0a 09 66 64 20 3d 20 64 75 70 28 63 61 */..fd = dup(ca
5140: 63 68 65 2d 3e 66 64 29 3b 0a 09 69 66 20 28 66 che->fd);..if (f
5150: 64 20 3c 20 30 29 20 7b 0a 09 09 70 74 68 72 65 d < 0) {...pthre
5160: 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 ad_mutex_unlock(
5170: 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a &cache->mutex);.
5180: 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b ...return(NULL);
5190: 0a 09 7d 0a 0a 09 62 75 66 66 65 72 2d 3e 66 64 ..}...buffer->fd
51a0: 20 3d 20 66 64 3b 0a 09 62 75 66 66 65 72 2d 3e = fd;..buffer->
51b0: 6c 65 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e len = cache->len
51c0: 3b 0a 09 62 75 66 66 65 72 2d 3e 74 79 70 65 20 ;..buffer->type
51d0: 3d 20 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a 09 = cache->type;..
51e0: 6d 65 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 6c memcpy(buffer->l
51f0: 61 73 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d astmod_b, cache-
5200: 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 >lastmod_b, size
5210: 6f 66 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d of(buffer->lastm
5220: 6f 64 5f 62 29 29 3b 0a 09 6d 65 6d 63 70 79 28 od_b));..memcpy(
5230: 62 75 66 66 65 72 2d 3e 65 74 61 67 2c 20 63 61 buffer->etag, ca
5240: 63 68 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f che->etag, sizeo
5250: 66 28 62 75 66 66 65 72 2d 3e 65 74 61 67 29 29 f(buffer->etag))
5260: 3b 0a 09 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d ;..buffer->lastm
5270: 6f 64 20 3d 20 62 75 66 66 65 72 2d 3e 6c 61 73 od = buffer->las
5280: 74 6d 6f 64 5f 62 20 2b 20 28 63 61 63 68 65 2d tmod_b + (cache-
5290: 3e 6c 61 73 74 6d 6f 64 20 2d 20 63 61 63 68 65 >lastmod - cache
52a0: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 3b 0a 0a 09 ->lastmod_b);...
52b0: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e pthread_mutex_un
52c0: 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 lock(&cache->mut
52d0: 65 78 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75 ex);...return(bu
52e0: 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 72 6f ffer);.}../* Pro
52f0: 63 65 73 73 20 61 6e 20 48 54 54 50 20 72 65 71 cess an HTTP req
5300: 75 65 73 74 20 61 6e 64 20 72 65 74 75 72 6e 20 uest and return
5310: 74 68 65 20 70 61 74 68 20 72 65 71 75 65 73 74 the path request
5320: 65 64 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 ed */.static str
5330: 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 uct filed_http_r
5340: 65 71 75 65 73 74 20 2a 66 69 6c 65 64 5f 67 65 equest *filed_ge
5350: 74 5f 68 74 74 70 5f 72 65 71 75 65 73 74 28 46 t_http_request(F
5360: 49 4c 45 20 2a 66 70 2c 20 73 74 72 75 63 74 20 ILE *fp, struct
5370: 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 filed_http_reque
5380: 73 74 20 2a 62 75 66 66 65 72 5f 73 74 2c 20 73 st *buffer_st, s
5390: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 truct filed_opti
53a0: 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a ons *options) {.
53b0: 09 63 68 61 72 20 2a 6d 65 74 68 6f 64 2c 20 2a .char *method, *
53c0: 70 61 74 68 3b 0a 09 63 68 61 72 20 2a 62 75 66 path;..char *buf
53d0: 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72 fer, *workbuffer
53e0: 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 , *workbuffer_ne
53f0: 78 74 3b 0a 09 63 68 61 72 20 2a 66 67 65 74 73 xt;..char *fgets
5400: 5f 72 65 74 3b 0a 09 73 69 7a 65 5f 74 20 62 75 _ret;..size_t bu
5410: 66 66 65 72 5f 6c 65 6e 2c 20 70 61 74 68 5f 6c ffer_len, path_l
5420: 65 6e 3b 0a 09 6f 66 66 5f 74 20 72 61 6e 67 65 en;..off_t range
5430: 5f 73 74 61 72 74 2c 20 72 61 6e 67 65 5f 65 6e _start, range_en
5440: 64 2c 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 3b d, range_length;
5450: 0a 09 69 6e 74 20 72 61 6e 67 65 5f 72 65 71 75 ..int range_requ
5460: 65 73 74 3b 0a 09 69 6e 74 20 73 6e 70 72 69 6e est;..int snprin
5470: 74 66 5f 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a tf_ret;..int i;.
5480: 0a 09 2f 2a 20 53 65 74 20 74 6f 20 64 65 66 61 ../* Set to defa
5490: 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f 0a 09 72 ult values */..r
54a0: 61 6e 67 65 5f 73 74 61 72 74 20 3d 20 30 3b 0a ange_start = 0;.
54b0: 09 72 61 6e 67 65 5f 65 6e 64 20 20 20 3d 20 30 .range_end = 0
54c0: 3b 0a 09 72 61 6e 67 65 5f 72 65 71 75 65 73 74 ;..range_request
54d0: 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 6c 65 6e = 0;..range_len
54e0: 67 74 68 20 3d 20 2d 31 3b 0a 09 62 75 66 66 65 gth = -1;..buffe
54f0: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f r_st->headers.ho
5500: 73 74 2e 70 72 65 73 65 6e 74 20 3d 20 30 3b 0a st.present = 0;.
5510: 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 .buffer_st->head
5520: 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 3d ers.connection =
5530: 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f FILED_CONNECTIO
5540: 4e 5f 43 4c 4f 53 45 3b 0a 0a 09 62 75 66 66 65 N_CLOSE;...buffe
5550: 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 r = buffer_st->t
5560: 6d 70 62 75 66 3b 0a 09 62 75 66 66 65 72 5f 6c mpbuf;..buffer_l
5570: 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 66 en = sizeof(buff
5580: 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b 0a er_st->tmpbuf);.
5590: 0a 09 66 67 65 74 73 5f 72 65 74 20 3d 20 66 67 ..fgets_ret = fg
55a0: 65 74 73 28 62 75 66 66 65 72 2c 20 62 75 66 66 ets(buffer, buff
55b0: 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 69 66 er_len, fp);..if
55c0: 20 28 66 67 65 74 73 5f 72 65 74 20 3d 3d 20 4e (fgets_ret == N
55d0: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 ULL) {...return(
55e0: 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 6d 65 74 68 NULL);..}...meth
55f0: 6f 64 20 3d 20 62 75 66 66 65 72 3b 0a 0a 09 62 od = buffer;...b
5600: 75 66 66 65 72 20 3d 20 73 74 72 63 68 72 28 62 uffer = strchr(b
5610: 75 66 66 65 72 2c 20 27 20 27 29 3b 0a 09 69 66 uffer, ' ');..if
5620: 20 28 62 75 66 66 65 72 20 3d 3d 20 4e 55 4c 4c (buffer == NULL
5630: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c ) {...return(NUL
5640: 4c 29 3b 0a 09 7d 0a 0a 09 2a 62 75 66 66 65 72 L);..}...*buffer
5650: 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 = '\0';..buffer
5660: 2b 2b 3b 0a 0a 09 70 61 74 68 20 3d 20 62 75 66 ++;...path = buf
5670: 66 65 72 3b 0a 0a 09 2f 2a 20 54 65 72 6d 69 6e fer;.../* Termin
5680: 61 74 65 20 70 61 74 68 20 63 6f 6d 70 6f 6e 65 ate path compone
5690: 6e 74 20 2a 2f 0a 09 62 75 66 66 65 72 20 3d 20 nt */..buffer =
56a0: 73 74 72 70 62 72 6b 28 70 61 74 68 2c 20 22 5c strpbrk(path, "\
56b0: 72 5c 6e 20 22 29 3b 0a 09 69 66 20 28 62 75 66 r\n ");..if (buf
56c0: 66 65 72 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 fer != NULL) {..
56d0: 09 2a 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b .*buffer = '\0';
56e0: 0a 09 09 62 75 66 66 65 72 2b 2b 3b 0a 09 7d 0a ...buffer++;..}.
56f0: 0a 09 2f 2a 20 57 65 20 6f 6e 6c 79 20 68 61 6e ../* We only han
5700: 64 6c 65 20 74 68 65 20 22 47 45 54 22 20 61 6e dle the "GET" an
5710: 64 20 22 48 45 41 44 27 20 6d 65 74 68 6f 64 73 d "HEAD' methods
5720: 20 2a 2f 0a 09 69 66 20 28 73 74 72 63 61 73 65 */..if (strcase
5730: 63 6d 70 28 6d 65 74 68 6f 64 2c 20 22 68 65 61 cmp(method, "hea
5740: 64 22 29 20 21 3d 20 30 29 20 7b 0a 09 09 69 66 d") != 0) {...if
5750: 20 28 73 74 72 63 61 73 65 63 6d 70 28 6d 65 74 (strcasecmp(met
5760: 68 6f 64 2c 20 22 67 65 74 22 29 20 21 3d 20 30 hod, "get") != 0
5770: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 ) {....return(NU
5780: 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 47 LL);...}..../* G
5790: 45 54 20 72 65 71 75 65 73 74 20 2a 2f 0a 09 09 ET request */...
57a0: 62 75 66 66 65 72 5f 73 74 2d 3e 6d 65 74 68 6f buffer_st->metho
57b0: 64 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 d = FILED_REQUES
57c0: 54 5f 4d 45 54 48 4f 44 5f 47 45 54 3b 0a 09 7d T_METHOD_GET;..}
57d0: 20 65 6c 73 65 20 7b 0a 09 09 2f 2a 20 48 45 41 else {.../* HEA
57e0: 44 20 72 65 71 75 65 73 74 20 2a 2f 0a 09 09 62 D request */...b
57f0: 75 66 66 65 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 uffer_st->method
5800: 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 = FILED_REQUEST
5810: 5f 4d 45 54 48 4f 44 5f 48 45 41 44 3b 0a 09 7d _METHOD_HEAD;..}
5820: 0a 0a 09 2f 2a 20 4e 6f 74 65 20 70 61 74 68 20 .../* Note path
5830: 2a 2f 0a 09 70 61 74 68 5f 6c 65 6e 20 3d 20 73 */..path_len = s
5840: 74 72 6c 65 6e 28 70 61 74 68 29 3b 0a 09 6d 65 trlen(path);..me
5850: 6d 63 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e mcpy(buffer_st->
5860: 70 61 74 68 2c 20 70 61 74 68 2c 20 70 61 74 68 path, path, path
5870: 5f 6c 65 6e 20 2b 20 31 29 3b 0a 0a 09 2f 2a 20 _len + 1);.../*
5880: 44 65 74 65 72 6d 69 6e 65 20 74 79 70 65 20 6f Determine type o
5890: 66 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 70 f request from p
58a0: 61 74 68 20 2a 2f 0a 09 69 66 20 28 70 61 74 68 ath */..if (path
58b0: 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 62 _len == 0) {...b
58c0: 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d uffer_st->type =
58d0: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 FILED_REQUEST_T
58e0: 59 50 45 5f 44 49 52 45 43 54 4f 52 59 3b 0a 09 YPE_DIRECTORY;..
58f0: 7d 20 65 6c 73 65 20 7b 0a 09 09 69 66 20 28 70 } else {...if (p
5900: 61 74 68 5b 70 61 74 68 5f 6c 65 6e 20 2d 20 31 ath[path_len - 1
5910: 5d 20 3d 3d 20 27 2f 27 29 20 7b 0a 09 09 09 62 ] == '/') {....b
5920: 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d uffer_st->type =
5930: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 FILED_REQUEST_T
5940: 59 50 45 5f 44 49 52 45 43 54 4f 52 59 3b 0a 09 YPE_DIRECTORY;..
5950: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 62 75 66 .} else {....buf
5960: 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 fer_st->type = F
5970: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 ILED_REQUEST_TYP
5980: 45 5f 4f 54 48 45 52 3b 0a 09 09 7d 0a 09 7d 0a E_OTHER;...}..}.
5990: 0a 09 2f 2a 20 52 65 73 65 74 20 62 75 66 66 65 ../* Reset buffe
59a0: 72 20 66 6f 72 20 6c 61 74 65 72 20 75 73 65 20 r for later use
59b0: 2a 2f 0a 09 62 75 66 66 65 72 20 3d 20 62 75 66 */..buffer = buf
59c0: 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a fer_st->tmpbuf;.
59d0: 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 20 ..for (i = 0; i
59e0: 3c 20 31 30 30 3b 20 69 2b 2b 29 20 7b 0a 09 09 < 100; i++) {...
59f0: 66 67 65 74 73 5f 72 65 74 20 3d 20 66 67 65 74 fgets_ret = fget
5a00: 73 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 s(buffer, buffer
5a10: 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 09 69 66 20 _len, fp);...if
5a20: 28 66 67 65 74 73 5f 72 65 74 20 3d 3d 20 4e 55 (fgets_ret == NU
5a30: 4c 4c 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a LL) {....break;.
5a40: 09 09 7d 0a 0a 09 09 69 66 20 28 73 74 72 6e 63 ..}....if (strnc
5a50: 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22 asecmp(buffer, "
5a60: 52 61 6e 67 65 3a 20 22 2c 20 37 29 20 3d 3d 20 Range: ", 7) ==
5a70: 30 29 20 7b 0a 09 09 09 77 6f 72 6b 62 75 66 66 0) {....workbuff
5a80: 65 72 20 3d 20 62 75 66 66 65 72 20 2b 20 37 3b er = buffer + 7;
5a90: 0a 0a 09 09 09 69 66 20 28 73 74 72 6e 63 61 73 .....if (strncas
5aa0: 65 63 6d 70 28 77 6f 72 6b 62 75 66 66 65 72 2c ecmp(workbuffer,
5ab0: 20 22 62 79 74 65 73 3d 22 2c 20 36 29 20 3d 3d "bytes=", 6) ==
5ac0: 20 30 29 20 7b 0a 09 09 09 09 77 6f 72 6b 62 75 0) {.....workbu
5ad0: 66 66 65 72 20 2b 3d 20 36 3b 0a 0a 09 09 09 09 ffer += 6;......
5ae0: 72 61 6e 67 65 5f 72 65 71 75 65 73 74 20 3d 20 range_request =
5af0: 31 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f 73 74 1;......range_st
5b00: 61 72 74 20 3d 20 73 74 72 74 6f 75 6c 6c 28 77 art = strtoull(w
5b10: 6f 72 6b 62 75 66 66 65 72 2c 20 26 77 6f 72 6b orkbuffer, &work
5b20: 62 75 66 66 65 72 5f 6e 65 78 74 2c 20 31 30 29 buffer_next, 10)
5b30: 3b 0a 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 ;......workbuffe
5b40: 72 20 3d 20 77 6f 72 6b 62 75 66 66 65 72 5f 6e r = workbuffer_n
5b50: 65 78 74 3b 0a 0a 09 09 09 09 69 66 20 28 2a 77 ext;......if (*w
5b60: 6f 72 6b 62 75 66 66 65 72 20 3d 3d 20 27 2d 27 orkbuffer == '-'
5b70: 29 20 7b 0a 09 09 09 09 09 77 6f 72 6b 62 75 66 ) {......workbuf
5b80: 66 65 72 2b 2b 3b 0a 0a 09 09 09 09 09 69 66 20 fer++;.......if
5b90: 28 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 (*workbuffer !=
5ba0: 27 5c 72 27 20 26 26 20 2a 77 6f 72 6b 62 75 66 '\r' && *workbuf
5bb0: 66 65 72 20 21 3d 20 27 5c 6e 27 29 20 7b 0a 09 fer != '\n') {..
5bc0: 09 09 09 09 09 72 61 6e 67 65 5f 65 6e 64 20 3d .....range_end =
5bd0: 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 strtoull(workbu
5be0: 66 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 ffer, &workbuffe
5bf0: 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 09 09 09 r_next, 10);....
5c00: 09 09 7d 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 ..}.....}....}..
5c10: 09 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 6e .} else if (strn
5c20: 63 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20 casecmp(buffer,
5c30: 22 48 6f 73 74 3a 20 22 2c 20 35 29 20 3d 3d 20 "Host: ", 5) ==
5c40: 30 29 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 0) {....buffer_s
5c50: 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e t->headers.host.
5c60: 70 72 65 73 65 6e 74 20 3d 20 31 3b 0a 0a 09 09 present = 1;....
5c70: 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 73 74 .workbuffer = st
5c80: 72 70 62 72 6b 28 62 75 66 66 65 72 20 2b 20 35 rpbrk(buffer + 5
5c90: 2c 20 22 5c 72 5c 6e 3a 22 29 3b 0a 09 09 09 69 , "\r\n:");....i
5ca0: 66 20 28 77 6f 72 6b 62 75 66 66 65 72 20 21 3d f (workbuffer !=
5cb0: 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 09 2a 77 6f NULL) {.....*wo
5cc0: 72 6b 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b rkbuffer = '\0';
5cd0: 0a 09 09 09 7d 0a 0a 09 09 09 77 6f 72 6b 62 75 ....}.....workbu
5ce0: 66 66 65 72 20 3d 20 62 75 66 66 65 72 20 2b 20 ffer = buffer +
5cf0: 35 3b 0a 09 09 09 77 68 69 6c 65 20 28 2a 77 6f 5;....while (*wo
5d00: 72 6b 62 75 66 66 65 72 20 3d 3d 20 27 20 27 29 rkbuffer == ' ')
5d10: 20 7b 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 {.....workbuffe
5d20: 72 2b 2b 3b 0a 09 09 09 7d 0a 0a 09 09 09 73 74 r++;....}.....st
5d30: 72 63 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e rcpy(buffer_st->
5d40: 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 68 6f 73 headers.host.hos
5d50: 74 2c 20 77 6f 72 6b 62 75 66 66 65 72 29 3b 0a t, workbuffer);.
5d60: 09 09 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 ..} else if (str
5d70: 6e 63 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c ncasecmp(buffer,
5d80: 20 22 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 65 "Connection: Ke
5d90: 65 70 2d 41 6c 69 76 65 22 2c 20 32 32 29 20 3d ep-Alive", 22) =
5da0: 3d 20 30 29 20 7b 0a 09 09 09 62 75 66 66 65 72 = 0) {....buffer
5db0: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e _st->headers.con
5dc0: 6e 65 63 74 69 6f 6e 20 3d 20 46 49 4c 45 44 5f nection = FILED_
5dd0: 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f CONNECTION_KEEP_
5de0: 41 4c 49 56 45 3b 0a 09 09 7d 0a 0a 09 09 69 66 ALIVE;...}....if
5df0: 20 28 6d 65 6d 63 6d 70 28 62 75 66 66 65 72 2c (memcmp(buffer,
5e00: 20 22 5c 72 5c 6e 22 2c 20 32 29 20 3d 3d 20 30 "\r\n", 2) == 0
5e10: 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 ) {....break;...
5e20: 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 65 74 65 72 6d }..}.../* Determ
5e30: 69 6e 65 20 72 61 6e 67 65 20 2a 2f 0a 09 69 66 ine range */..if
5e40: 20 28 72 61 6e 67 65 5f 65 6e 64 20 21 3d 20 30 (range_end != 0
5e50: 29 20 7b 0a 09 09 69 66 20 28 72 61 6e 67 65 5f ) {...if (range_
5e60: 65 6e 64 20 3c 3d 20 72 61 6e 67 65 5f 73 74 61 end <= range_sta
5e70: 72 74 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 rt) {....return(
5e80: 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 72 61 NULL);...}....ra
5e90: 6e 67 65 5f 6c 65 6e 67 74 68 20 3d 20 72 61 6e nge_length = ran
5ea0: 67 65 5f 65 6e 64 20 2d 20 72 61 6e 67 65 5f 73 ge_end - range_s
5eb0: 74 61 72 74 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c tart;....filed_l
5ec0: 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f og_msg_debug("Co
5ed0: 6d 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 70 mputing length p
5ee0: 61 72 61 6d 65 74 65 72 3a 20 25 6c 6c 75 20 3d arameter: %llu =
5ef0: 20 25 6c 6c 75 20 2d 20 25 6c 6c 75 22 2c 0a 09 %llu - %llu",..
5f00: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
5f10: 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 6c 65 6e long) range_len
5f20: 67 74 68 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 gth,....(unsigne
5f30: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e d long long) ran
5f40: 67 65 5f 65 6e 64 2c 0a 09 09 09 28 75 6e 73 69 ge_end,....(unsi
5f50: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
5f60: 72 61 6e 67 65 5f 73 74 61 72 74 0a 09 09 29 3b range_start...);
5f70: 0a 09 7d 0a 0a 09 2f 2a 20 46 69 6c 6c 20 75 70 ..}.../* Fill up
5f80: 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 72 65 structure to re
5f90: 74 75 72 6e 20 2a 2f 0a 09 62 75 66 66 65 72 5f turn */..buffer_
5fa0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
5fb0: 65 2e 70 72 65 73 65 6e 74 20 3d 20 72 61 6e 67 e.present = rang
5fc0: 65 5f 72 65 71 75 65 73 74 3b 0a 09 62 75 66 66 e_request;..buff
5fd0: 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 er_st->headers.r
5fe0: 61 6e 67 65 2e 6f 66 66 73 65 74 20 20 3d 20 72 ange.offset = r
5ff0: 61 6e 67 65 5f 73 74 61 72 74 3b 0a 09 62 75 66 ange_start;..buf
6000: 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e fer_st->headers.
6010: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 20 3d 20 range.length =
6020: 72 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 0a 09 range_length;...
6030: 2f 2a 20 49 66 20 76 68 6f 73 74 73 20 61 72 65 /* If vhosts are
6040: 20 65 6e 61 62 6c 65 64 2c 20 63 6f 6d 70 75 74 enabled, comput
6050: 65 20 6e 65 77 20 70 61 74 68 20 2a 2f 0a 09 69 e new path */..i
6060: 66 20 28 6f 70 74 69 6f 6e 73 2d 3e 76 68 6f 73 f (options->vhos
6070: 74 73 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 ts_enabled) {...
6080: 69 66 20 28 62 75 66 66 65 72 5f 73 74 2d 3e 68 if (buffer_st->h
6090: 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 eaders.host.pres
60a0: 65 6e 74 20 3d 3d 20 31 29 20 7b 0a 09 09 09 62 ent == 1) {....b
60b0: 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 uffer = buffer_s
60c0: 74 2d 3e 74 6d 70 62 75 66 3b 0a 09 09 09 62 75 t->tmpbuf;....bu
60d0: 66 66 65 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f ffer_len = sizeo
60e0: 66 28 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 f(buffer_st->tmp
60f0: 62 75 66 29 3b 0a 0a 09 09 09 73 6e 70 72 69 6e buf);.....snprin
6100: 74 66 5f 72 65 74 20 3d 20 73 6e 70 72 69 6e 74 tf_ret = snprint
6110: 66 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 f(buffer, buffer
6120: 5f 6c 65 6e 2c 20 22 2f 25 73 25 73 25 73 22 2c _len, "/%s%s%s",
6130: 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e .....buffer_st->
6140: 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 68 6f 73 headers.host.hos
6150: 74 2c 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74 t,.....buffer_st
6160: 2d 3e 70 61 74 68 5b 30 5d 20 3d 3d 20 27 2f 27 ->path[0] == '/'
6170: 20 3f 20 22 22 20 3a 20 22 2f 22 2c 0a 09 09 09 ? "" : "/",....
6180: 09 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 .buffer_st->path
6190: 0a 09 09 09 29 3b 0a 09 09 09 69 66 20 28 73 6e ....);....if (sn
61a0: 70 72 69 6e 74 66 5f 72 65 74 20 3e 3d 20 30 29 printf_ret >= 0)
61b0: 20 7b 0a 09 09 09 09 69 66 20 28 28 28 75 6e 73 {.....if (((uns
61c0: 69 67 6e 65 64 20 69 6e 74 29 20 73 6e 70 72 69 igned int) snpri
61d0: 6e 74 66 5f 72 65 74 29 20 3c 20 62 75 66 66 65 ntf_ret) < buffe
61e0: 72 5f 6c 65 6e 29 20 7b 0a 09 09 09 09 09 73 74 r_len) {......st
61f0: 72 63 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e rcpy(buffer_st->
6200: 70 61 74 68 2c 20 62 75 66 66 65 72 29 3b 0a 09 path, buffer);..
6210: 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a 09 7d ...}....}...}..}
6220: 0a 0a 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 ...return(buffer
6230: 5f 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 _st);.}../* Retu
6240: 72 6e 20 61 6e 20 65 72 72 6f 72 20 70 61 67 65 rn an error page
6250: 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */.static void
6260: 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 filed_error_page
6270: 28 46 49 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 (FILE *fp, const
6280: 20 63 68 61 72 20 2a 64 61 74 65 5f 63 75 72 72 char *date_curr
6290: 65 6e 74 2c 20 69 6e 74 20 65 72 72 6f 72 5f 6e ent, int error_n
62a0: 75 6d 62 65 72 2c 20 69 6e 74 20 6d 65 74 68 6f umber, int metho
62b0: 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 d, const char *r
62c0: 65 61 73 6f 6e 2c 20 73 74 72 75 63 74 20 66 69 eason, struct fi
62d0: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c led_log_entry *l
62e0: 6f 67 29 20 7b 0a 09 63 68 61 72 20 2a 65 72 72 og) {..char *err
62f0: 6f 72 5f 73 74 72 69 6e 67 20 3d 20 22 3c 68 74 or_string = "<ht
6300: 6d 6c 3e 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e ml><head><title>
6310: 45 52 52 4f 52 3c 2f 74 69 74 6c 65 3e 3c 2f 68 ERROR</title></h
6320: 65 61 64 3e 3c 62 6f 64 79 3e 55 6e 61 62 6c 65 ead><body>Unable
6330: 20 74 6f 20 70 72 6f 63 65 73 73 20 72 65 71 75 to process requ
6340: 65 73 74 3c 2f 62 6f 64 79 3e 3c 2f 68 74 6d 6c est</body></html
6350: 3e 22 3b 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 >";...fprintf(fp
6360: 2c 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4e , "HTTP/1.1 %i N
6370: 6f 74 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 ot OK\r\nDate: %
6380: 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c s\r\nServer: fil
6390: 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 ed\r\nLast-Modif
63a0: 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 ied: %s\r\nConte
63b0: 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c nt-Length: %llu\
63c0: 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a r\nContent-Type:
63d0: 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f %s\r\nConnectio
63e0: 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 5c 72 5c 6e n: close\r\n\r\n
63f0: 22 2c 0a 09 09 65 72 72 6f 72 5f 6e 75 6d 62 65 ",...error_numbe
6400: 72 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e r,...date_curren
6410: 74 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e t,...date_curren
6420: 74 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c t,...(unsigned l
6430: 6f 6e 67 20 6c 6f 6e 67 29 20 73 74 72 6c 65 6e ong long) strlen
6440: 28 65 72 72 6f 72 5f 73 74 72 69 6e 67 29 2c 0a (error_string),.
6450: 09 09 22 74 65 78 74 2f 68 74 6d 6c 22 0a 09 29 .."text/html"..)
6460: 3b 0a 0a 09 2f 2a 20 73 69 6c 65 6e 63 65 20 65 ;.../* silence e
6470: 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 rror string for
6480: 48 45 41 44 20 72 65 71 75 65 73 74 73 20 2a 2f HEAD requests */
6490: 0a 09 69 66 20 28 6d 65 74 68 6f 64 20 21 3d 20 ..if (method !=
64a0: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 FILED_REQUEST_ME
64b0: 54 48 4f 44 5f 48 45 41 44 29 20 7b 0a 09 09 66 THOD_HEAD) {...f
64c0: 70 72 69 6e 74 66 28 66 70 2c 20 22 25 73 22 2c printf(fp, "%s",
64d0: 20 65 72 72 6f 72 5f 73 74 72 69 6e 67 29 3b 0a error_string);.
64e0: 09 7d 0a 0a 09 2f 2a 20 4c 6f 67 20 65 72 72 6f .}.../* Log erro
64f0: 72 20 2a 2f 0a 09 2f 2a 2a 20 72 65 61 73 6f 6e r */../** reason
6500: 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 must point to a
6510: 20 67 6c 6f 62 61 6c 6c 79 20 61 6c 6c 6f 63 61 globally alloca
6520: 74 65 64 20 76 61 6c 75 65 20 2a 2a 2f 0a 09 6c ted value **/..l
6530: 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 72 65 61 og->reason = rea
6540: 73 6f 6e 3b 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f son;..log->http_
6550: 63 6f 64 65 20 3d 20 65 72 72 6f 72 5f 6e 75 6d code = error_num
6560: 62 65 72 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 ber;...filed_log
6570: 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 2f _entry(log);.../
6580: 2a 20 43 6c 6f 73 65 20 63 6f 6e 6e 65 63 74 69 * Close connecti
6590: 6f 6e 20 2a 2f 0a 09 66 69 6c 65 64 5f 73 6f 63 on */..filed_soc
65a0: 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 kettimeout_close
65b0: 28 66 69 6c 65 6e 6f 28 66 70 29 29 3b 0a 0a 09 (fileno(fp));...
65c0: 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 fclose(fp);...re
65d0: 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 turn;.}../* Retu
65e0: 72 6e 20 61 20 72 65 64 69 72 65 63 74 20 74 6f rn a redirect to
65f0: 20 69 6e 64 65 78 2e 68 74 6d 6c 20 2a 2f 0a 73 index.html */.s
6600: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 tatic void filed
6610: 5f 72 65 64 69 72 65 63 74 5f 69 6e 64 65 78 28 _redirect_index(
6620: 46 49 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 20 FILE *fp, const
6630: 63 68 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 char *date_curre
6640: 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a nt, const char *
6650: 70 61 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c path, struct fil
6660: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f ed_log_entry *lo
6670: 67 29 20 7b 0a 09 69 6e 74 20 68 74 74 70 5f 63 g) {..int http_c
6680: 6f 64 65 20 3d 20 33 30 31 3b 0a 09 66 70 72 69 ode = 301;..fpri
6690: 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e ntf(fp, "HTTP/1.
66a0: 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 1 %i OK\r\nDate:
66b0: 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 %s\r\nServer: f
66c0: 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 iled\r\nLast-Mod
66d0: 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e ified: %s\r\nCon
66e0: 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 30 5c 72 tent-Length: 0\r
66f0: 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c \nConnection: cl
6700: 6f 73 65 5c 72 5c 6e 4c 6f 63 61 74 69 6f 6e 3a ose\r\nLocation:
6710: 20 25 73 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 %s\r\n\r\n",...
6720: 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64 61 74 http_code,...dat
6730: 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64 61 74 e_current,...dat
6740: 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 22 69 6e e_current,..."in
6750: 64 65 78 2e 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 dex.html"..);...
6760: 2f 2a 20 4c 6f 67 20 72 65 64 69 72 65 63 74 20 /* Log redirect
6770: 2a 2f 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 */..log->reason
6780: 3d 20 22 72 65 64 69 72 65 63 74 22 3b 0a 09 6c = "redirect";..l
6790: 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 og->http_code =
67a0: 68 74 74 70 5f 63 6f 64 65 3b 0a 0a 09 66 69 6c http_code;...fil
67b0: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 ed_log_entry(log
67c0: 29 3b 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20 63 6f );.../* Close co
67d0: 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 66 69 6c nnection */..fil
67e0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
67f0: 5f 63 6c 6f 73 65 28 66 69 6c 65 6e 6f 28 66 70 _close(fileno(fp
6800: 29 29 3b 0a 0a 09 66 63 6c 6f 73 65 28 66 70 29 ));...fclose(fp)
6810: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 0a 09 2f 2a ;...return;.../*
6820: 20 43 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 Currently unuse
6830: 64 3a 20 70 61 74 68 20 2a 2f 0a 09 70 61 74 68 d: path */..path
6840: 20 3d 20 70 61 74 68 3b 0a 7d 0a 0a 2f 2a 20 43 = path;.}../* C
6850: 6f 6e 76 65 72 74 20 61 6e 20 65 6e 75 6d 20 72 onvert an enum r
6860: 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
6870: 22 43 6f 6e 6e 65 63 74 69 6f 6e 22 20 68 65 61 "Connection" hea
6880: 64 65 72 20 76 61 6c 75 65 20 74 6f 20 61 20 73 der value to a s
6890: 74 72 69 6e 67 20 2a 2f 0a 73 74 61 74 69 63 20 tring */.static
68a0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 const char *file
68b0: 64 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 d_connection_str
68c0: 28 69 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f (int connection_
68d0: 76 61 6c 75 65 29 20 7b 0a 09 73 77 69 74 63 68 value) {..switch
68e0: 20 28 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c (connection_val
68f0: 75 65 29 20 7b 0a 09 09 63 61 73 65 20 46 49 4c ue) {...case FIL
6900: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c ED_CONNECTION_CL
6910: 4f 53 45 3a 0a 09 09 09 72 65 74 75 72 6e 28 22 OSE:....return("
6920: 63 6c 6f 73 65 22 29 3b 0a 09 09 63 61 73 65 20 close");...case
6930: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e FILED_CONNECTION
6940: 5f 4b 45 45 50 5f 41 4c 49 56 45 3a 0a 09 09 09 _KEEP_ALIVE:....
6950: 72 65 74 75 72 6e 28 22 6b 65 65 70 2d 61 6c 69 return("keep-ali
6960: 76 65 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 ve");..}...retur
6970: 6e 28 22 63 6c 6f 73 65 22 29 3b 0a 7d 0a 0a 2f n("close");.}../
6980: 2a 20 48 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c * Handle a singl
6990: 65 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 61 e request from a
69a0: 20 63 6c 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 client */.stati
69b0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 68 61 6e 64 c int filed_hand
69c0: 6c 65 5f 63 6c 69 65 6e 74 28 69 6e 74 20 66 64 le_client(int fd
69d0: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 , struct filed_h
69e0: 74 74 70 5f 72 65 71 75 65 73 74 20 2a 72 65 71 ttp_request *req
69f0: 75 65 73 74 2c 20 73 74 72 75 63 74 20 66 69 6c uest, struct fil
6a00: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f ed_log_entry *lo
6a10: 67 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f g, struct filed_
6a20: 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 options *options
6a30: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 ) {..struct file
6a40: 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 d_fileinfo *file
6a50: 69 6e 66 6f 3b 0a 09 73 73 69 7a 65 5f 74 20 73 info;..ssize_t s
6a60: 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 73 69 endfile_ret;..si
6a70: 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f 73 69 ze_t sendfile_si
6a80: 7a 65 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 64 66 ze;..off_t sendf
6a90: 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 ile_offset, send
6aa0: 66 69 6c 65 5f 73 65 6e 74 2c 20 73 65 6e 64 66 file_sent, sendf
6ab0: 69 6c 65 5f 6c 65 6e 3b 0a 09 63 68 61 72 20 2a ile_len;..char *
6ac0: 70 61 74 68 3b 0a 09 63 68 61 72 20 2a 64 61 74 path;..char *dat
6ad0: 65 5f 63 75 72 72 65 6e 74 2c 20 64 61 74 65 5f e_current, date_
6ae0: 63 75 72 72 65 6e 74 5f 62 5b 36 34 5d 3b 0a 09 current_b[64];..
6af0: 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 int http_code;..
6b00: 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 2f 2a 20 44 FILE *fp;.../* D
6b10: 65 74 65 72 6d 69 6e 65 20 63 75 72 72 65 6e 74 etermine current
6b20: 20 74 69 6d 65 20 2a 2f 0a 09 64 61 74 65 5f 63 time */..date_c
6b30: 75 72 72 65 6e 74 20 3d 20 66 69 6c 65 64 5f 66 urrent = filed_f
6b40: 6f 72 6d 61 74 5f 74 69 6d 65 28 64 61 74 65 5f ormat_time(date_
6b50: 63 75 72 72 65 6e 74 5f 62 2c 20 73 69 7a 65 6f current_b, sizeo
6b60: 66 28 64 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 f(date_current_b
6b70: 29 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 29 3b 0a ), time(NULL));.
6b80: 0a 09 2f 2a 20 4f 70 65 6e 20 73 6f 63 6b 65 74 ../* Open socket
6b90: 20 61 73 20 41 4e 53 49 20 49 2f 4f 20 66 6f 72 as ANSI I/O for
6ba0: 20 65 61 73 65 20 6f 66 20 75 73 65 20 2a 2f 0a ease of use */.
6bb0: 09 66 70 20 3d 20 66 64 6f 70 65 6e 28 66 64 2c .fp = fdopen(fd,
6bc0: 20 22 77 2b 62 22 29 3b 0a 09 69 66 20 28 66 70 "w+b");..if (fp
6bd0: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 == NULL) {...fi
6be0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
6bf0: 74 5f 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 t_close(fd);....
6c00: 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 6c 6f close(fd);....lo
6c10: 67 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 g->buffer[0] = '
6c20: 5c 30 27 3b 0a 09 09 6c 6f 67 2d 3e 68 74 74 70 \0';...log->http
6c30: 5f 63 6f 64 65 20 3d 20 2d 31 3b 0a 09 09 6c 6f _code = -1;...lo
6c40: 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 66 64 6f g->reason = "fdo
6c50: 70 65 6e 5f 66 61 69 6c 65 64 22 3b 0a 0a 09 09 pen_failed";....
6c60: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 filed_log_entry(
6c70: 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 log);....return(
6c80: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e FILED_CONNECTION
6c90: 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 72 65 _CLOSE);..}...re
6ca0: 71 75 65 73 74 20 3d 20 66 69 6c 65 64 5f 67 65 quest = filed_ge
6cb0: 74 5f 68 74 74 70 5f 72 65 71 75 65 73 74 28 66 t_http_request(f
6cc0: 70 2c 20 72 65 71 75 65 73 74 2c 20 6f 70 74 69 p, request, opti
6cd0: 6f 6e 73 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 ons);...if (requ
6ce0: 65 73 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 est == NULL) {..
6cf0: 09 6c 6f 67 2d 3e 62 75 66 66 65 72 5b 30 5d 20 .log->buffer[0]
6d00: 3d 20 27 5c 30 27 3b 0a 0a 09 09 66 69 6c 65 64 = '\0';....filed
6d10: 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 _error_page(fp,
6d20: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 date_current, 50
6d30: 30 2c 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 0, FILED_REQUEST
6d40: 5f 4d 45 54 48 4f 44 5f 47 45 54 2c 20 22 66 6f _METHOD_GET, "fo
6d50: 72 6d 61 74 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 rmat", log);....
6d60: 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e return(FILED_CON
6d70: 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a NECTION_CLOSE);.
6d80: 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 .}...filed_socke
6d90: 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 ttimeout_process
6da0: 69 6e 67 5f 73 74 61 72 74 28 66 64 29 3b 0a 0a ing_start(fd);..
6db0: 09 70 61 74 68 20 3d 20 72 65 71 75 65 73 74 2d .path = request-
6dc0: 3e 70 61 74 68 3b 0a 09 73 74 72 63 70 79 28 6c >path;..strcpy(l
6dd0: 6f 67 2d 3e 62 75 66 66 65 72 2c 20 70 61 74 68 og->buffer, path
6de0: 29 3b 0a 09 6c 6f 67 2d 3e 6d 65 74 68 6f 64 20 );..log->method
6df0: 3d 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f = request->metho
6e00: 64 3b 0a 0a 09 2f 2a 20 49 66 20 74 68 65 20 72 d;.../* If the r
6e10: 65 71 75 65 73 74 65 64 20 70 61 74 68 20 69 73 equested path is
6e20: 20 61 20 64 69 72 65 63 74 6f 72 79 2c 20 72 65 a directory, re
6e30: 64 69 72 65 63 74 20 74 6f 20 69 6e 64 65 78 20 direct to index
6e40: 70 61 67 65 20 2a 2f 0a 09 69 66 20 28 72 65 71 page */..if (req
6e50: 75 65 73 74 2d 3e 74 79 70 65 20 3d 3d 20 46 49 uest->type == FI
6e60: 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 LED_REQUEST_TYPE
6e70: 5f 44 49 52 45 43 54 4f 52 59 29 20 7b 0a 09 09 _DIRECTORY) {...
6e80: 66 69 6c 65 64 5f 72 65 64 69 72 65 63 74 5f 69 filed_redirect_i
6e90: 6e 64 65 78 28 66 70 2c 20 64 61 74 65 5f 63 75 ndex(fp, date_cu
6ea0: 72 72 65 6e 74 2c 20 70 61 74 68 2c 20 6c 6f 67 rrent, path, log
6eb0: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c );....return(FIL
6ec0: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c ED_CONNECTION_CL
6ed0: 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 69 OSE);..}...filei
6ee0: 6e 66 6f 20 3d 20 66 69 6c 65 64 5f 6f 70 65 6e nfo = filed_open
6ef0: 5f 66 69 6c 65 28 70 61 74 68 2c 20 26 72 65 71 _file(path, &req
6f00: 75 65 73 74 2d 3e 66 69 6c 65 69 6e 66 6f 29 3b uest->fileinfo);
6f10: 0a 09 69 66 20 28 66 69 6c 65 69 6e 66 6f 20 3d ..if (fileinfo =
6f20: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 = NULL) {...file
6f30: 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c d_error_page(fp,
6f40: 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 date_current, 4
6f50: 30 34 2c 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 04, request->met
6f60: 68 6f 64 2c 20 22 6f 70 65 6e 5f 66 61 69 6c 65 hod, "open_faile
6f70: 64 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 d", log);....ret
6f80: 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 urn(FILED_CONNEC
6f90: 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a TION_CLOSE);..}.
6fa0: 0a 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 ..if (request->h
6fb0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 eaders.range.pre
6fc0: 73 65 6e 74 29 20 7b 0a 09 09 69 66 20 28 72 65 sent) {...if (re
6fd0: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
6fe0: 61 6e 67 65 2e 6f 66 66 73 65 74 20 21 3d 20 30 ange.offset != 0
6ff0: 20 7c 7c 20 72 65 71 75 65 73 74 2d 3e 68 65 61 || request->hea
7000: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 ders.range.lengt
7010: 68 20 3e 3d 20 30 29 20 7b 0a 09 09 09 69 66 20 h >= 0) {....if
7020: 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 (request->header
7030: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3e s.range.offset >
7040: 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 29 = fileinfo->len)
7050: 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f 65 72 72 {.....filed_err
7060: 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 or_page(fp, date
7070: 5f 63 75 72 72 65 6e 74 2c 20 34 31 36 2c 20 72 _current, 416, r
7080: 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c 20 equest->method,
7090: 22 72 61 6e 67 65 5f 69 6e 76 61 6c 69 64 22 2c "range_invalid",
70a0: 20 6c 6f 67 29 3b 0a 0a 09 09 09 09 63 6c 6f 73 log);......clos
70b0: 65 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b e(fileinfo->fd);
70c0: 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 46 49 4c ......return(FIL
70d0: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c ED_CONNECTION_CL
70e0: 4f 53 45 29 3b 0a 09 09 09 7d 0a 0a 09 09 09 69 OSE);....}.....i
70f0: 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 f (request->head
7100: 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 ers.range.length
7110: 20 3d 3d 20 28 28 6f 66 66 5f 74 29 20 2d 31 29 == ((off_t) -1)
7120: 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f 6c 6f ) {.....filed_lo
7130: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d g_msg_debug("Com
7140: 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 74 6f puting length to
7150: 20 66 69 74 20 69 6e 20 62 6f 75 6e 64 73 3a 20 fit in bounds:
7160: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20 3d 20 fileinfo->len =
7170: 25 6c 6c 75 2c 20 72 65 71 75 65 73 74 2d 3e 68 %llu, request->h
7180: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
7190: 73 65 74 20 3d 20 25 6c 6c 75 22 2c 0a 09 09 09 set = %llu",....
71a0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
71b0: 20 6c 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f 2d long) fileinfo-
71c0: 3e 6c 65 6e 2c 0a 09 09 09 09 09 28 75 6e 73 69 >len,......(unsi
71d0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
71e0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
71f0: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 0a 09 09 .range.offset...
7200: 09 09 29 3b 0a 0a 09 09 09 09 72 65 71 75 65 73 ..);......reques
7210: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
7220: 2e 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e .length = filein
7230: 66 6f 2d 3e 6c 65 6e 20 2d 20 72 65 71 75 65 73 fo->len - reques
7240: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
7250: 2e 6f 66 66 73 65 74 3b 0a 09 09 09 7d 0a 0a 09 .offset;....}...
7260: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f ..filed_log_msg_
7270: 64 65 62 75 67 28 22 50 61 72 74 69 61 6c 20 72 debug("Partial r
7280: 65 71 75 65 73 74 2c 20 73 74 61 72 74 69 6e 67 equest, starting
7290: 20 61 74 3a 20 25 6c 6c 75 20 61 6e 64 20 72 75 at: %llu and ru
72a0: 6e 6e 69 6e 67 20 66 6f 72 20 25 6c 6c 69 20 62 nning for %lli b
72b0: 79 74 65 73 22 2c 0a 09 09 09 09 28 75 6e 73 69 ytes",.....(unsi
72c0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
72d0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
72e0: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 .range.offset,..
72f0: 09 09 09 28 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 ...(long long) r
7300: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
7310: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 0a 09 09 09 range.length....
7320: 29 3b 0a 0a 09 09 7d 0a 0a 09 09 68 74 74 70 5f );....}....http_
7330: 63 6f 64 65 20 3d 20 32 30 36 3b 0a 09 7d 20 65 code = 206;..} e
7340: 6c 73 65 20 7b 0a 09 09 68 74 74 70 5f 63 6f 64 lse {...http_cod
7350: 65 20 3d 20 32 30 30 3b 0a 0a 09 09 2f 2a 20 43 e = 200;..../* C
7360: 6f 6d 70 75 74 65 20 66 61 6b 65 20 72 61 6e 67 ompute fake rang
7370: 65 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 e parameters tha
7380: 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 65 t includes the e
7390: 6e 74 69 72 65 20 66 69 6c 65 20 2a 2f 0a 09 09 ntire file */...
73a0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
73b0: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d 20 .range.offset =
73c0: 30 3b 0a 09 09 72 65 71 75 65 73 74 2d 3e 68 65 0;...request->he
73d0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 aders.range.leng
73e0: 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c th = fileinfo->l
73f0: 65 6e 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74 66 en;..}...fprintf
7400: 28 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 25 (fp, "HTTP/1.1 %
7410: 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 73 i OK\r\nDate: %s
7420: 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c 65 \r\nServer: file
7430: 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 d\r\nLast-Modifi
7440: 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e ed: %s\r\nConten
7450: 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 t-Length: %llu\r
7460: 5c 6e 41 63 63 65 70 74 2d 52 61 6e 67 65 73 3a \nAccept-Ranges:
7470: 20 62 79 74 65 73 5c 72 5c 6e 43 6f 6e 74 65 6e bytes\r\nConten
7480: 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e 43 6f t-Type: %s\r\nCo
7490: 6e 6e 65 63 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e nnection: %s\r\n
74a0: 45 54 61 67 3a 20 5c 22 25 73 5c 22 5c 72 5c 6e ETag: \"%s\"\r\n
74b0: 22 2c 0a 09 09 68 74 74 70 5f 63 6f 64 65 2c 0a ",...http_code,.
74c0: 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a ..date_current,.
74d0: 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 6c 61 73 74 ..fileinfo->last
74e0: 6d 6f 64 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 mod,...(unsigned
74f0: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 long long) requ
7500: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e est->headers.ran
7510: 67 65 2e 6c 65 6e 67 74 68 2c 0a 09 09 66 69 6c ge.length,...fil
7520: 65 69 6e 66 6f 2d 3e 74 79 70 65 2c 0a 09 09 66 einfo->type,...f
7530: 69 6c 65 64 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f iled_connection_
7540: 73 74 72 28 72 65 71 75 65 73 74 2d 3e 68 65 61 str(request->hea
7550: 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 29 ders.connection)
7560: 2c 0a 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 65 74 ,...fileinfo->et
7570: 61 67 0a 09 29 3b 0a 0a 09 69 66 20 28 68 74 74 ag..);...if (htt
7580: 70 5f 63 6f 64 65 20 3d 3d 20 32 30 36 29 20 7b p_code == 206) {
7590: 0a 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 ...fprintf(fp, "
75a0: 43 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20 62 Content-Range: b
75b0: 79 74 65 73 20 25 6c 6c 75 2d 25 6c 6c 75 2f 25 ytes %llu-%llu/%
75c0: 6c 6c 75 5c 72 5c 6e 22 2c 0a 09 09 09 28 75 6e llu\r\n",....(un
75d0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
75e0: 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 ) request->heade
75f0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c rs.range.offset,
7600: 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f ....(unsigned lo
7610: 6e 67 20 6c 6f 6e 67 29 20 28 72 65 71 75 65 73 ng long) (reques
7620: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
7630: 2e 6f 66 66 73 65 74 20 2b 20 72 65 71 75 65 73 .offset + reques
7640: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
7650: 2e 6c 65 6e 67 74 68 20 2d 20 31 29 2c 0a 09 09 .length - 1),...
7660: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
7670: 6c 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f 2d 3e long) fileinfo->
7680: 6c 65 6e 0a 09 09 29 3b 0a 09 7d 0a 09 66 70 72 len...);..}..fpr
7690: 69 6e 74 66 28 66 70 2c 20 22 5c 72 5c 6e 22 29 intf(fp, "\r\n")
76a0: 3b 0a 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a ;..fflush(fp);..
76b0: 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 .log->http_code
76c0: 3d 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 6c 6f = http_code;..lo
76d0: 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 4f 4b 22 g->reason = "OK"
76e0: 3b 0a 09 6c 6f 67 2d 3e 73 74 61 72 74 74 69 6d ;..log->starttim
76f0: 65 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a e = time(NULL);.
7700: 09 6c 6f 67 2d 3e 72 65 71 5f 6f 66 66 73 65 74 .log->req_offset
7710: 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 = request->head
7720: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 ers.range.offset
7730: 3b 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6c 65 6e 67 ;..log->req_leng
7740: 74 68 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 th = request->he
7750: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 aders.range.leng
7760: 74 68 3b 0a 09 6c 6f 67 2d 3e 66 69 6c 65 5f 6c th;..log->file_l
7770: 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f ength = fileinfo
7780: 2d 3e 6c 65 6e 3b 0a 0a 23 69 66 64 65 66 20 46 ->len;..#ifdef F
7790: 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 ILED_NONBLOCK_HT
77a0: 54 50 0a 09 69 6e 74 20 73 6f 63 6b 65 74 5f 66 TP..int socket_f
77b0: 6c 61 67 73 3b 0a 09 66 64 5f 73 65 74 20 72 66 lags;..fd_set rf
77c0: 64 2c 20 77 66 64 3b 0a 09 63 68 61 72 20 73 69 d, wfd;..char si
77d0: 6e 6b 62 75 66 5b 38 31 39 32 5d 3b 0a 09 73 73 nkbuf[8192];..ss
77e0: 69 7a 65 5f 74 20 72 65 61 64 5f 72 65 74 3b 0a ize_t read_ret;.
77f0: 0a 09 46 44 5f 5a 45 52 4f 28 26 72 66 64 29 3b ..FD_ZERO(&rfd);
7800: 0a 09 46 44 5f 5a 45 52 4f 28 26 77 66 64 29 3b ..FD_ZERO(&wfd);
7810: 0a 09 46 44 5f 53 45 54 28 66 64 2c 20 26 72 66 ..FD_SET(fd, &rf
7820: 64 29 3b 0a 09 46 44 5f 53 45 54 28 66 64 2c 20 d);..FD_SET(fd,
7830: 26 77 66 64 29 3b 0a 0a 09 73 6f 63 6b 65 74 5f &wfd);...socket_
7840: 66 6c 61 67 73 20 3d 20 66 63 6e 74 6c 28 66 64 flags = fcntl(fd
7850: 2c 20 46 5f 47 45 54 46 4c 29 3b 0a 09 66 63 6e , F_GETFL);..fcn
7860: 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 4c 2c 20 tl(fd, F_SETFL,
7870: 73 6f 63 6b 65 74 5f 66 6c 61 67 73 20 7c 20 4f socket_flags | O
7880: 5f 4e 4f 4e 42 4c 4f 43 4b 29 3b 0a 23 65 6e 64 _NONBLOCK);.#end
7890: 69 66 0a 09 73 65 6e 64 66 69 6c 65 5f 6f 66 66 if..sendfile_off
78a0: 73 65 74 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 set = request->h
78b0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
78c0: 73 65 74 3b 0a 09 73 65 6e 64 66 69 6c 65 5f 6c set;..sendfile_l
78d0: 65 6e 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 en = request->he
78e0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 aders.range.leng
78f0: 74 68 3b 0a 09 73 65 6e 64 66 69 6c 65 5f 73 65 th;..sendfile_se
7900: 6e 74 20 3d 20 30 3b 0a 09 77 68 69 6c 65 20 28 nt = 0;..while (
7910: 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 20 request->method
7920: 3d 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 == FILED_REQUEST
7930: 5f 4d 45 54 48 4f 44 5f 47 45 54 29 20 7b 0a 09 _METHOD_GET) {..
7940: 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c 65 .if (sendfile_le
7950: 6e 20 3e 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 n > FILED_SENDFI
7960: 4c 45 5f 4d 41 58 29 20 7b 0a 09 09 09 73 65 6e LE_MAX) {....sen
7970: 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 46 49 4c dfile_size = FIL
7980: 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41 58 3b ED_SENDFILE_MAX;
7990: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 73 ...} else {....s
79a0: 65 6e 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 73 endfile_size = s
79b0: 65 6e 64 66 69 6c 65 5f 6c 65 6e 3b 0a 09 09 7d endfile_len;...}
79c0: 0a 0a 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 ....sendfile_ret
79d0: 20 3d 20 73 65 6e 64 66 69 6c 65 28 66 64 2c 20 = sendfile(fd,
79e0: 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 2c 20 26 73 fileinfo->fd, &s
79f0: 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 endfile_offset,
7a00: 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 29 3b 0a sendfile_size);.
7a10: 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 72 ..if (sendfile_r
7a20: 65 74 20 3c 3d 20 30 29 20 7b 0a 23 69 66 64 65 et <= 0) {.#ifde
7a30: 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b f FILED_NONBLOCK
7a40: 5f 48 54 54 50 0a 09 09 09 69 66 20 28 65 72 72 _HTTP....if (err
7a50: 6e 6f 20 3d 3d 20 45 41 47 41 49 4e 29 20 7b 0a no == EAGAIN) {.
7a60: 09 09 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 ....sendfile_ret
7a70: 20 3d 20 30 3b 0a 0a 09 09 09 09 77 68 69 6c 65 = 0;......while
7a80: 20 28 31 29 20 7b 0a 09 09 09 09 09 73 65 6c 65 (1) {......sele
7a90: 63 74 28 66 64 20 2b 20 31 2c 20 26 72 66 64 2c ct(fd + 1, &rfd,
7aa0: 20 26 77 66 64 2c 20 4e 55 4c 4c 2c 20 4e 55 4c &wfd, NULL, NUL
7ab0: 4c 29 3b 0a 09 09 09 09 09 69 66 20 28 46 44 5f L);......if (FD_
7ac0: 49 53 53 45 54 28 66 64 2c 20 26 72 66 64 29 29 ISSET(fd, &rfd))
7ad0: 20 7b 0a 09 09 09 09 09 09 72 65 61 64 5f 72 65 {.......read_re
7ae0: 74 20 3d 20 72 65 61 64 28 66 64 2c 20 73 69 6e t = read(fd, sin
7af0: 6b 62 75 66 2c 20 73 69 7a 65 6f 66 28 73 69 6e kbuf, sizeof(sin
7b00: 6b 62 75 66 29 29 3b 0a 0a 09 09 09 09 09 09 69 kbuf));........i
7b10: 66 20 28 72 65 61 64 5f 72 65 74 20 3c 3d 20 30 f (read_ret <= 0
7b20: 29 20 7b 0a 09 09 09 09 09 09 09 62 72 65 61 6b ) {........break
7b30: 3b 0a 09 09 09 09 09 09 7d 0a 09 09 09 09 09 7d ;.......}......}
7b40: 0a 0a 09 09 09 09 09 69 66 20 28 46 44 5f 49 53 .......if (FD_IS
7b50: 53 45 54 28 66 64 2c 20 26 77 66 64 29 29 20 7b SET(fd, &wfd)) {
7b60: 0a 09 09 09 09 09 09 72 65 61 64 5f 72 65 74 20 .......read_ret
7b70: 3d 20 31 3b 0a 0a 09 09 09 09 09 09 62 72 65 61 = 1;........brea
7b80: 6b 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09 7d 0a k;......}.....}.
7b90: 0a 09 09 09 09 69 66 20 28 72 65 61 64 5f 72 65 .....if (read_re
7ba0: 74 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 09 62 t <= 0) {......b
7bb0: 72 65 61 6b 3b 0a 09 09 09 09 7d 0a 09 09 09 7d reak;.....}....}
7bc0: 20 65 6c 73 65 20 7b 0a 09 09 09 09 62 72 65 61 else {.....brea
7bd0: 6b 3b 0a 09 09 09 7d 0a 23 65 6c 73 65 0a 09 09 k;....}.#else...
7be0: 09 62 72 65 61 6b 3b 0a 23 65 6e 64 69 66 0a 09 .break;.#endif..
7bf0: 09 7d 0a 0a 09 09 73 65 6e 64 66 69 6c 65 5f 6c .}....sendfile_l
7c00: 65 6e 20 2d 3d 20 73 65 6e 64 66 69 6c 65 5f 72 en -= sendfile_r
7c10: 65 74 3b 0a 09 09 73 65 6e 64 66 69 6c 65 5f 73 et;...sendfile_s
7c20: 65 6e 74 20 2b 3d 20 73 65 6e 64 66 69 6c 65 5f ent += sendfile_
7c30: 72 65 74 3b 0a 09 09 69 66 20 28 73 65 6e 64 66 ret;...if (sendf
7c40: 69 6c 65 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a ile_len == 0) {.
7c50: 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d ...break;...}..}
7c60: 0a 0a 09 6c 6f 67 2d 3e 65 6e 64 74 69 6d 65 20 ...log->endtime
7c70: 3d 20 28 74 69 6d 65 5f 74 29 20 2d 31 3b 0a 09 = (time_t) -1;..
7c80: 6c 6f 67 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 log->sent_length
7c90: 20 3d 20 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 = sendfile_sent
7ca0: 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e ;...filed_log_en
7cb0: 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 63 6c 6f 73 try(log);...clos
7cc0: 65 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b e(fileinfo->fd);
7cd0: 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e ...if (request->
7ce0: 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 headers.connecti
7cf0: 6f 6e 20 21 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e on != FILED_CONN
7d00: 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 ECTION_KEEP_ALIV
7d10: 45 29 20 7b 0a 09 09 66 69 6c 65 64 5f 73 6f 63 E) {...filed_soc
7d20: 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 kettimeout_close
7d30: 28 66 64 29 3b 0a 0a 09 09 66 63 6c 6f 73 65 28 (fd);....fclose(
7d40: 66 70 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 fp);....return(F
7d50: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f ILED_CONNECTION_
7d60: 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c CLOSE);..}...fil
7d70: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
7d80: 5f 70 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64 28 _processing_end(
7d90: 66 64 29 3b 0a 0a 09 72 65 74 75 72 6e 28 46 49 fd);...return(FI
7da0: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b LED_CONNECTION_K
7db0: 45 45 50 5f 41 4c 49 56 45 29 3b 0a 7d 0a 0a 2f EEP_ALIVE);.}../
7dc0: 2a 20 48 61 6e 64 6c 65 20 69 6e 63 6f 6d 69 6e * Handle incomin
7dd0: 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f g connections */
7de0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 .static void *fi
7df0: 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 led_worker_threa
7e00: 64 28 76 6f 69 64 20 2a 61 72 67 5f 76 29 20 7b d(void *arg_v) {
7e10: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 ..struct filed_w
7e20: 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 orker_thread_arg
7e30: 73 20 2a 61 72 67 3b 0a 09 73 74 72 75 63 74 20 s *arg;..struct
7e40: 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 filed_http_reque
7e50: 73 74 20 72 65 71 75 65 73 74 3b 0a 09 73 74 72 st request;..str
7e60: 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e uct filed_log_en
7e70: 74 72 79 20 2a 6c 6f 67 2c 20 6c 6f 63 61 6c 5f try *log, local_
7e80: 64 75 6d 6d 79 5f 6c 6f 67 3b 0a 09 73 74 72 75 dummy_log;..stru
7e90: 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 ct filed_options
7ea0: 20 2a 6f 70 74 69 6f 6e 73 3b 0a 09 73 74 72 75 *options;..stru
7eb0: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 ct sockaddr_in6
7ec0: 61 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 addr;..socklen_t
7ed0: 20 61 64 64 72 6c 65 6e 3b 0a 09 69 6e 74 20 66 addrlen;..int f
7ee0: 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20 30 ailure_count = 0
7ef0: 2c 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f , max_failure_co
7f00: 75 6e 74 20 3d 20 46 49 4c 45 44 5f 4d 41 58 5f unt = FILED_MAX_
7f10: 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 3b 0a 09 FAILURE_COUNT;..
7f20: 69 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 int connection_s
7f30: 74 61 74 65 20 3d 20 46 49 4c 45 44 5f 43 4f 4e tate = FILED_CON
7f40: 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 3b 0a 09 NECTION_CLOSE;..
7f50: 69 6e 74 20 6d 61 73 74 65 72 5f 66 64 2c 20 66 int master_fd, f
7f60: 64 20 3d 20 2d 31 3b 0a 0a 09 2f 2a 20 52 65 61 d = -1;.../* Rea
7f70: 64 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 d arguments */..
7f80: 61 72 67 20 3d 20 61 72 67 5f 76 3b 0a 0a 09 6d arg = arg_v;...m
7f90: 61 73 74 65 72 5f 66 64 20 3d 20 61 72 67 2d 3e aster_fd = arg->
7fa0: 66 64 3b 0a 09 6f 70 74 69 6f 6e 73 20 3d 20 26 fd;..options = &
7fb0: 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 3b 0a 0a 09 arg->options;...
7fc0: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 2f 2a while (1) {.../*
7fd0: 20 46 61 69 6c 75 72 65 20 6c 6f 6f 70 20 70 72 Failure loop pr
7fe0: 65 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 evention */...if
7ff0: 20 28 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 (failure_count
8000: 3e 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f > max_failure_co
8010: 75 6e 74 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b unt) {....break;
8020: 0a 09 09 7d 0a 0a 09 09 2f 2a 20 41 6c 6c 6f 63 ...}..../* Alloc
8030: 61 74 65 20 61 20 6e 65 77 20 6c 6f 67 20 62 75 ate a new log bu
8040: 66 66 65 72 20 2a 2f 0a 09 09 6c 6f 67 20 3d 20 ffer */...log =
8050: 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 31 29 filed_log_new(1)
8060: 3b 0a 09 09 69 66 20 28 6c 6f 67 20 3d 3d 20 4e ;...if (log == N
8070: 55 4c 4c 29 20 7b 0a 09 09 09 66 69 6c 65 64 5f ULL) {....filed_
8080: 6c 6f 67 5f 6d 73 67 28 22 41 4c 4c 4f 43 41 54 log_msg("ALLOCAT
8090: 45 5f 4c 4f 47 5f 4d 53 47 5f 46 41 49 4c 45 44 E_LOG_MSG_FAILED
80a0: 22 29 3b 0a 0a 09 09 09 62 72 65 61 6b 3b 0a 09 ");.....break;..
80b0: 09 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 79 70 65 20 .}....log->type
80c0: 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 = FILED_LOG_TYPE
80d0: 5f 54 52 41 4e 53 46 45 52 3b 0a 0a 09 09 2f 2a _TRANSFER;..../*
80e0: 20 49 66 20 77 65 20 63 6c 6f 73 65 64 20 74 68 If we closed th
80f0: 65 20 6f 6c 64 20 63 6f 6e 6e 65 63 74 69 6f 6e e old connection
8100: 2c 20 61 63 63 65 70 74 20 61 20 6e 65 77 20 6f , accept a new o
8110: 6e 65 20 2a 2f 0a 09 09 69 66 20 28 63 6f 6e 6e ne */...if (conn
8120: 65 63 74 69 6f 6e 5f 73 74 61 74 65 20 3d 3d 20 ection_state ==
8130: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e FILED_CONNECTION
8140: 5f 43 4c 4f 53 45 29 20 7b 0a 09 09 09 2f 2a 20 _CLOSE) {..../*
8150: 41 63 63 65 70 74 20 61 20 6e 65 77 20 63 6c 69 Accept a new cli
8160: 65 6e 74 20 2a 2f 0a 09 09 09 61 64 64 72 6c 65 ent */....addrle
8170: 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 29 n = sizeof(addr)
8180: 3b 0a 0a 09 09 09 66 64 20 3d 20 61 63 63 65 70 ;.....fd = accep
8190: 74 28 6d 61 73 74 65 72 5f 66 64 2c 20 28 73 74 t(master_fd, (st
81a0: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 ruct sockaddr *)
81b0: 20 26 61 64 64 72 2c 20 26 61 64 64 72 6c 65 6e &addr, &addrlen
81c0: 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 0a 09 09 20 );...}..../*...
81d0: 2a 20 49 66 20 77 65 20 66 61 69 6c 2c 20 6d 61 * If we fail, ma
81e0: 6b 65 20 61 20 6e 6f 74 65 20 6f 66 20 69 74 20 ke a note of it
81f0: 73 6f 20 77 65 20 64 6f 6e 27 74 20 67 6f 20 69 so we don't go i
8200: 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f 66 0a 09 09 nto a loop of...
8210: 20 2a 20 61 63 63 65 70 74 28 29 20 66 61 69 6c * accept() fail
8220: 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 69 66 20 28 ing... */...if (
8230: 66 64 20 3c 20 30 29 20 7b 0a 09 09 09 2f 2a 20 fd < 0) {..../*
8240: 4c 6f 67 20 74 68 65 20 6e 65 77 20 63 6f 6e 6e Log the new conn
8250: 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 09 66 69 6c ection */....fil
8260: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 43 43 45 ed_log_msg("ACCE
8270: 50 54 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 09 PT_FAILED");....
8280: 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 2b 2b .failure_count++
8290: 3b 0a 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f ;.....filed_log_
82a0: 66 72 65 65 28 6c 6f 67 29 3b 0a 0a 09 09 09 63 free(log);.....c
82b0: 6f 6e 74 69 6e 75 65 3b 0a 09 09 7d 0a 0a 09 09 ontinue;...}....
82c0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
82d0: 6f 75 74 5f 61 63 63 65 70 74 28 66 64 29 3b 0a out_accept(fd);.
82e0: 0a 09 09 2f 2a 20 46 69 6c 6c 20 69 6e 20 6c 6f .../* Fill in lo
82f0: 67 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 g structure */..
8300: 09 69 66 20 28 66 69 6c 65 64 5f 6c 6f 67 5f 69 .if (filed_log_i
8310: 70 28 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 p((struct sockad
8320: 64 72 20 2a 29 20 26 61 64 64 72 2c 20 6c 6f 67 dr *) &addr, log
8330: 2d 3e 69 70 2c 20 73 69 7a 65 6f 66 28 6c 6f 67 ->ip, sizeof(log
8340: 2d 3e 69 70 29 29 20 3d 3d 20 4e 55 4c 4c 29 20 ->ip)) == NULL)
8350: 7b 0a 09 09 09 6c 6f 67 2d 3e 69 70 5b 30 5d 20 {....log->ip[0]
8360: 3d 20 27 5c 30 27 3b 0a 09 09 09 6c 6f 67 2d 3e = '\0';....log->
8370: 70 6f 72 74 20 3d 20 30 3b 0a 09 09 7d 20 65 6c port = 0;...} el
8380: 73 65 20 7b 0a 09 09 09 6c 6f 67 2d 3e 70 6f 72 se {....log->por
8390: 74 20 3d 20 61 64 64 72 2e 73 69 6e 36 5f 70 6f t = addr.sin6_po
83a0: 72 74 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 52 65 rt;...}..../* Re
83b0: 73 65 74 20 66 61 69 6c 75 72 65 20 63 6f 75 6e set failure coun
83c0: 74 2a 2f 0a 09 09 66 61 69 6c 75 72 65 5f 63 6f t*/...failure_co
83d0: 75 6e 74 20 3d 20 30 3b 0a 0a 09 09 2f 2a 20 48 unt = 0;..../* H
83e0: 61 6e 64 6c 65 20 73 6f 63 6b 65 74 20 2a 2f 0a andle socket */.
83f0: 09 09 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 ..connection_sta
8400: 74 65 20 3d 20 66 69 6c 65 64 5f 68 61 6e 64 6c te = filed_handl
8410: 65 5f 63 6c 69 65 6e 74 28 66 64 2c 20 26 72 65 e_client(fd, &re
8420: 71 75 65 73 74 2c 20 6c 6f 67 2c 20 6f 70 74 69 quest, log, opti
8430: 6f 6e 73 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65 ons);..}.../* Re
8440: 70 6f 72 74 20 65 72 72 6f 72 20 2a 2f 0a 09 66 port error */..f
8450: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 54 48 iled_log_msg("TH
8460: 52 45 41 44 5f 44 49 45 44 20 41 42 4e 4f 52 4d READ_DIED ABNORM
8470: 41 4c 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 4e AL");...return(N
8480: 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 6c 6f 63 61 6c ULL);.../* local
8490: 5f 64 75 6d 6d 79 5f 6c 6f 67 20 69 73 20 6f 6e _dummy_log is on
84a0: 6c 79 20 75 73 65 64 20 69 66 20 46 49 4c 45 44 ly used if FILED
84b0: 5f 44 4f 4e 54 5f 4c 4f 47 20 69 73 20 65 6e 61 _DONT_LOG is ena
84c0: 62 6c 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20 bled, otherwise
84d0: 69 74 27 73 20 6e 6f 74 20 75 73 65 64 2c 20 62 it's not used, b
84e0: 75 74 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 ut the compiler
84f0: 68 61 74 65 73 20 74 68 61 74 20 69 64 65 61 2e hates that idea.
8500: 20 2a 2f 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d 79 */..local_dummy
8510: 5f 6c 6f 67 2e 74 79 70 65 20 3d 20 30 3b 0a 09 _log.type = 0;..
8520: 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e local_dummy_log.
8530: 74 79 70 65 20 3d 20 6c 6f 63 61 6c 5f 64 75 6d type = local_dum
8540: 6d 79 5f 6c 6f 67 2e 74 79 70 65 3b 0a 7d 0a 0a my_log.type;.}..
8550: 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 6b 65 72 /* Create worker
8560: 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 61 74 threads */.stat
8570: 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 77 6f 72 ic int filed_wor
8580: 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 ker_threads_init
8590: 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 74 68 72 (int fd, int thr
85a0: 65 61 64 5f 63 6f 75 6e 74 2c 20 73 74 72 75 63 ead_count, struc
85b0: 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 t filed_options
85c0: 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 72 *options) {..str
85d0: 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 uct filed_worker
85e0: 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 _thread_args *ar
85f0: 67 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 g;..pthread_t th
8600: 72 65 61 64 69 64 3b 0a 09 69 6e 74 20 70 74 68 readid;..int pth
8610: 72 65 61 64 5f 72 65 74 3b 0a 09 69 6e 74 20 69 read_ret;..int i
8620: 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 ;...for (i = 0;
8630: 69 20 3c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 i < thread_count
8640: 3b 20 69 2b 2b 29 20 7b 0a 09 09 61 72 67 20 3d ; i++) {...arg =
8650: 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a malloc(sizeof(*
8660: 61 72 67 29 29 3b 0a 0a 09 09 61 72 67 2d 3e 66 arg));....arg->f
8670: 64 20 3d 20 66 64 3b 0a 09 09 6d 65 6d 63 70 79 d = fd;...memcpy
8680: 28 26 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 2c 20 (&arg->options,
8690: 6f 70 74 69 6f 6e 73 2c 20 73 69 7a 65 6f 66 28 options, sizeof(
86a0: 2a 6f 70 74 69 6f 6e 73 29 29 3b 0a 0a 09 09 70 *options));....p
86b0: 74 68 72 65 61 64 5f 72 65 74 20 3d 20 70 74 68 thread_ret = pth
86c0: 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 68 72 read_create(&thr
86d0: 65 61 64 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c eadid, NULL, fil
86e0: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 ed_worker_thread
86f0: 2c 20 61 72 67 29 3b 0a 09 09 69 66 20 28 70 74 , arg);...if (pt
8700: 68 72 65 61 64 5f 72 65 74 20 21 3d 20 30 29 20 hread_ret != 0)
8710: 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 31 29 3b {....return(-1);
8720: 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e ...}..}...return
8730: 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 73 70 6c (0);.}../* Displ
8740: 61 79 20 68 65 6c 70 20 2a 2f 0a 73 74 61 74 69 ay help */.stati
8750: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 70 72 69 c void filed_pri
8760: 6e 74 5f 68 65 6c 70 28 46 49 4c 45 20 2a 6f 75 nt_help(FILE *ou
8770: 74 70 75 74 2c 20 69 6e 74 20 6c 6f 6e 67 5f 68 tput, int long_h
8780: 65 6c 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 elp, const char
8790: 2a 65 78 74 72 61 29 20 7b 0a 09 69 66 20 28 65 *extra) {..if (e
87a0: 78 74 72 61 29 20 7b 0a 09 09 66 70 72 69 6e 74 xtra) {...fprint
87b0: 66 28 6f 75 74 70 75 74 2c 20 22 25 73 5c 6e 22 f(output, "%s\n"
87c0: 2c 20 65 78 74 72 61 29 3b 0a 09 7d 0a 0a 09 66 , extra);..}...f
87d0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
87e0: 55 73 61 67 65 3a 20 66 69 6c 65 64 20 5b 3c 6f Usage: filed [<o
87f0: 70 74 69 6f 6e 73 3e 5d 5c 6e 22 29 3b 0a 09 66 ptions>]\n");..f
8800: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
8810: 20 20 4f 70 74 69 6f 6e 73 3a 5c 6e 22 29 3b 0a Options:\n");.
8820: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
8830: 20 22 20 20 20 20 20 20 2d 68 2c 20 2d 2d 68 65 " -h, --he
8840: 6c 70 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 lp\n");..fprintf
8850: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
8860: 2d 64 2c 20 2d 2d 64 61 65 6d 6f 6e 5c 6e 22 29 -d, --daemon\n")
8870: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
8880: 74 2c 20 22 20 20 20 20 20 20 2d 76 2c 20 2d 2d t, " -v, --
8890: 76 65 72 73 69 6f 6e 5c 6e 22 29 3b 0a 09 66 70 version\n");..fp
88a0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
88b0: 20 20 20 20 20 2d 56 2c 20 2d 2d 76 68 6f 73 74 -V, --vhost
88c0: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f \n");..fprintf(o
88d0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 62 utput, " -b
88e0: 20 3c 61 64 64 72 65 73 73 3e 2c 20 2d 2d 62 69 <address>, --bi
88f0: 6e 64 20 3c 61 64 64 72 65 73 73 3e 5c 6e 22 29 nd <address>\n")
8900: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
8910: 74 2c 20 22 20 20 20 20 20 20 2d 70 20 3c 70 6f t, " -p <po
8920: 72 74 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70 6f 72 rt>, --port <por
8930: 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 t>\n");..fprintf
8940: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
8950: 2d 74 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d 74 68 -t <count>, --th
8960: 72 65 61 64 73 20 3c 63 6f 75 6e 74 3e 5c 6e 22 reads <count>\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 63 20 3c 65 ut, " -c <e
8990: 6e 74 72 69 65 73 3e 2c 20 2d 2d 63 61 63 68 65 ntries>, --cache
89a0: 20 3c 65 6e 74 72 69 65 73 3e 5c 6e 22 29 3b 0a <entries>\n");.
89b0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
89c0: 20 22 20 20 20 20 20 20 2d 6c 20 3c 66 69 6c 65 " -l <file
89d0: 3e 2c 20 2d 2d 6c 6f 67 20 3c 66 69 6c 65 3e 5c >, --log <file>\
89e0: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
89f0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 75 20 tput, " -u
8a00: 3c 75 73 65 72 3e 2c 20 2d 2d 75 73 65 72 20 3c <user>, --user <
8a10: 75 73 65 72 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 user>\n");..fpri
8a20: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
8a30: 20 20 20 2d 72 20 3c 64 69 72 65 63 74 6f 72 79 -r <directory
8a40: 3e 2c 20 2d 2d 72 6f 6f 74 20 3c 64 69 72 65 63 >, --root <direc
8a50: 74 6f 72 79 3e 5c 6e 22 29 3b 0a 0a 09 69 66 20 tory>\n");...if
8a60: 28 6c 6f 6e 67 5f 68 65 6c 70 29 20 7b 0a 09 09 (long_help) {...
8a70: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
8a80: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
8a90: 28 6f 75 74 70 75 74 2c 20 22 20 20 55 73 61 67 (output, " Usag
8aa0: 65 3a 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 e:\n");...fprint
8ab0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
8ac0: 20 2d 68 20 28 6f 72 20 2d 2d 68 65 6c 70 29 20 -h (or --help)
8ad0: 70 72 69 6e 74 73 20 74 68 69 73 20 75 73 61 67 prints this usag
8ae0: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 5c 6e e information.\n
8af0: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
8b00: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 tput, "\n");...f
8b10: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
8b20: 20 20 20 20 20 20 2d 64 20 28 6f 72 20 2d 2d 64 -d (or --d
8b30: 61 65 6d 6f 6e 29 20 69 6e 73 74 72 75 63 74 73 aemon) instructs
8b40: 20 66 69 6c 65 64 20 74 6f 20 62 65 63 6f 6d 65 filed to become
8b50: 20 61 20 64 61 65 6d 6f 6e 20 61 66 74 65 72 20 a daemon after
8b60: 69 6e 69 74 69 61 6c 69 7a 69 6e 67 5c 6e 22 29 initializing\n")
8b70: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
8b80: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
8b90: 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 the
8ba0: 6c 69 73 74 65 6e 69 6e 67 20 54 43 50 20 73 6f listening TCP so
8bb0: 63 6b 65 74 20 61 6e 64 20 6c 6f 67 20 66 69 6c cket and log fil
8bc0: 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e es.\n");...fprin
8bd0: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 tf(output, "\n")
8be0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
8bf0: 75 74 2c 20 22 20 20 20 20 20 20 2d 76 20 28 6f ut, " -v (o
8c00: 72 20 2d 2d 76 65 72 73 69 6f 6e 29 20 69 6e 73 r --version) ins
8c10: 74 72 75 63 74 73 20 66 69 6c 65 64 20 70 72 69 tructs filed pri
8c20: 6e 74 20 6f 75 74 20 74 68 65 20 76 65 72 73 69 nt out the versi
8c30: 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 65 78 on number and ex
8c40: 69 74 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e it.\n");...fprin
8c50: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 tf(output, "\n")
8c60: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
8c70: 75 74 2c 20 22 20 20 20 20 20 20 2d 56 20 28 6f ut, " -V (o
8c80: 72 20 2d 2d 76 68 6f 73 74 29 20 69 6e 73 74 72 r --vhost) instr
8c90: 75 63 74 73 20 66 69 6c 65 64 20 74 6f 20 70 72 ucts filed to pr
8ca0: 65 70 65 6e 64 20 61 6c 6c 20 72 65 71 75 65 73 epend all reques
8cb0: 74 73 20 77 69 74 68 20 74 68 65 69 72 20 48 54 ts with their HT
8cc0: 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 TP\n");...fprint
8cd0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
8ce0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8cf0: 20 48 6f 73 74 20 68 65 61 64 65 72 2e 5c 6e 22 Host header.\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 5c 6e 22 29 3b 0a 09 09 66 70 put, "\n");...fp
8d20: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
8d30: 20 20 20 20 20 2d 62 20 28 6f 72 20 2d 2d 62 69 -b (or --bi
8d40: 6e 64 29 20 73 70 65 63 69 66 69 65 73 20 74 68 nd) specifies th
8d50: 65 20 61 64 64 72 65 73 73 20 74 6f 20 6c 69 73 e address to lis
8d60: 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 ten for incoming
8d70: 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 HTTP\n");...fpr
8d80: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
8d90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8da0: 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 requests on.
8db0: 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c The default val
8dc0: 75 65 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 ue is \"%s\".\n"
8dd0: 2c 20 42 49 4e 44 5f 41 44 44 52 29 3b 0a 09 09 , BIND_ADDR);...
8de0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
8df0: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
8e00: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
8e10: 2d 70 20 28 6f 72 20 2d 2d 70 6f 72 74 29 20 73 -p (or --port) s
8e20: 70 65 63 69 66 69 65 73 20 74 68 65 20 54 43 50 pecifies the TCP
8e30: 20 70 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f 20 port number to
8e40: 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d listen for incom
8e50: 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 ing HTTP\n");...
8e60: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
8e70: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
8e80: 20 20 20 20 20 20 72 65 71 75 65 73 74 73 20 6f requests o
8e90: 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 n. The default
8ea0: 69 73 20 25 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 is %u.\n", (unsi
8eb0: 67 6e 65 64 20 69 6e 74 29 20 50 4f 52 54 29 3b gned int) PORT);
8ec0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
8ed0: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t, "\n");...fpri
8ee0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
8ef0: 20 20 20 2d 74 20 28 6f 72 20 2d 2d 74 68 72 65 -t (or --thre
8f00: 61 64 73 29 20 73 70 65 63 69 66 69 65 73 20 74 ads) specifies t
8f10: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 6f 72 he number of wor
8f20: 6b 65 72 20 74 68 72 65 61 64 73 20 74 6f 20 63 ker threads to c
8f30: 72 65 61 74 65 2e 20 45 61 63 68 5c 6e 22 29 3b reate. Each\n");
8f40: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
8f50: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
8f60: 20 20 20 20 20 20 20 20 20 20 20 20 77 6f 72 6b work
8f70: 65 72 20 74 68 72 65 61 64 20 63 61 6e 20 73 65 er thread can se
8f80: 72 76 69 63 65 20 6f 6e 65 20 63 6f 6e 63 75 72 rvice one concur
8f90: 72 65 6e 74 20 48 54 54 50 20 73 65 73 73 69 6f rent HTTP sessio
8fa0: 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 n.\n");...fprint
8fb0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
8fc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8fd0: 20 20 20 54 68 75 73 20 74 68 65 20 6e 75 6d 62 Thus the numb
8fe0: 65 72 20 6f 66 20 74 68 72 65 61 64 73 20 63 72 er of threads cr
8ff0: 65 61 74 65 64 20 77 69 6c 6c 20 64 65 74 65 72 eated will deter
9000: 6d 69 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a 09 09 mine how\n");...
9010: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
9020: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
9030: 20 20 20 20 20 20 20 20 20 6d 61 6e 79 20 73 69 many si
9040: 6d 75 6c 74 61 6e 65 6f 75 73 20 74 72 61 6e 73 multaneous trans
9050: 66 65 72 73 20 77 69 6c 6c 20 62 65 20 70 6f 73 fers will be pos
9060: 73 69 62 6c 65 2e 20 54 68 65 5c 6e 22 29 3b 0a sible. The\n");.
9070: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
9080: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
9090: 20 20 20 20 20 20 20 20 20 20 20 64 65 66 61 75 defau
90a0: 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 lt is %lu.\n", (
90b0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 54 unsigned long) T
90c0: 48 52 45 41 44 5f 43 4f 55 4e 54 29 3b 0a 09 09 HREAD_COUNT);...
90d0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
90e0: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
90f0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
9100: 2d 63 20 28 6f 72 20 2d 2d 63 61 63 68 65 29 20 -c (or --cache)
9110: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 specifies the nu
9120: 6d 62 65 72 20 6f 66 20 66 69 6c 65 20 69 6e 66 mber of file inf
9130: 6f 72 6d 61 74 69 6f 6e 20 63 61 63 68 65 20 65 ormation cache e
9140: 6e 74 72 69 65 73 5c 6e 22 29 3b 0a 09 09 66 70 ntries\n");...fp
9150: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
9160: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9170: 20 20 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74 65 to allocate
9180: 2e 20 20 45 61 63 68 20 63 61 63 68 65 20 65 6e . Each cache en
9190: 74 72 79 20 68 6f 6c 64 73 20 66 69 6c 65 20 69 try holds file i
91a0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 5c 6e 22 nformation as\n"
91b0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
91c0: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
91d0: 20 20 20 20 20 20 20 20 20 20 20 20 77 65 6c 6c well
91e0: 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 as an open file
91f0: 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74 descriptor to t
9200: 68 65 20 66 69 6c 65 2c 20 73 6f 20 72 65 73 6f he file, so reso
9210: 75 72 63 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 urce\n");...fpri
9220: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9230: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9240: 20 20 20 6c 69 6d 69 74 73 20 28 69 2e 65 2e 2c limits (i.e.,
9250: 20 75 6c 69 6d 69 74 29 20 73 68 6f 75 6c 64 20 ulimit) should
9260: 62 65 20 63 6f 6e 73 69 64 65 72 65 64 2e 20 20 be considered.
9270: 54 68 69 73 20 73 68 6f 75 6c 64 5c 6e 22 29 3b This should\n");
9280: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9290: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
92a0: 20 20 20 20 20 20 20 20 20 20 62 65 20 61 20 70 be a p
92b0: 72 69 6d 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 rime number for
92c0: 69 64 65 61 6c 20 75 73 65 20 77 69 74 68 20 74 ideal use with t
92d0: 68 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 6f 64 he lookup method
92e0: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 .\n");...fprintf
92f0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
9300: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9310: 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 The default is %
9320: 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 lu.\n", (unsigne
9330: 64 20 6c 6f 6e 67 29 20 43 41 43 48 45 5f 53 49 d long) CACHE_SI
9340: 5a 45 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f ZE);...fprintf(o
9350: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
9360: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
9370: 22 20 20 20 20 20 20 2d 6c 20 28 6f 72 20 2d 2d " -l (or --
9380: 6c 6f 67 29 20 73 70 65 63 69 66 69 65 73 20 61 log) specifies a
9390: 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 6f 70 65 filename to ope
93a0: 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 20 6c 6f n for writing lo
93b0: 67 20 65 6e 74 72 69 65 73 2e 20 20 4c 6f 67 5c g entries. Log\
93c0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
93d0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
93e0: 20 20 20 20 20 20 20 20 20 20 20 20 65 6e 74 72 entr
93f0: 69 65 73 20 61 72 65 20 6d 61 64 65 20 66 6f 72 ies are made for
9400: 20 76 61 72 69 6f 75 73 20 73 74 61 67 65 73 20 various stages
9410: 69 6e 20 74 72 61 6e 73 66 65 72 69 6e 67 20 66 in transfering f
9420: 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 iles.\n");...fpr
9430: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
9440: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9450: 20 20 54 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 The log file i
9460: 73 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 s opened before
9470: 73 77 69 74 63 68 69 6e 67 20 75 73 65 72 73 20 switching users
9480: 28 73 65 65 20 5c 22 2d 75 5c 22 29 5c 6e 22 29 (see \"-u\")\n")
9490: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
94a0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
94b0: 20 20 20 20 20 20 20 20 20 61 6e 64 20 72 6f 6f and roo
94c0: 74 20 64 69 72 65 63 74 6f 72 69 65 73 20 28 73 t directories (s
94d0: 65 65 20 5c 22 2d 72 5c 22 29 2e 20 20 54 68 65 ee \"-r\"). The
94e0: 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e 65 76 log file is nev
94f0: 65 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 er\n");...fprint
9500: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
9510: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
9520: 6c 6f 73 65 64 20 73 6f 20 6c 6f 67 20 72 6f 74 losed so log rot
9530: 61 74 69 6f 6e 20 77 69 74 68 6f 75 74 20 73 74 ation without st
9540: 6f 70 70 69 6e 67 20 74 68 65 20 64 61 65 6d 6f opping the daemo
9550: 6e 20 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 n is will\n");..
9560: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
9570: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
9580: 20 20 20 20 20 20 6e 6f 74 20 77 6f 72 6b 2e 20 not work.
9590: 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 5c 22 The value of \"
95a0: 2d 5c 22 20 69 6e 64 69 63 61 74 65 73 20 74 68 -\" indicates th
95b0: 61 74 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 at standard outp
95c0: 75 74 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 ut\n");...fprint
95d0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
95e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
95f0: 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f hould be used fo
9600: 72 20 6c 6f 67 67 69 6e 67 2e 20 20 49 66 20 74 r logging. If t
9610: 68 65 20 66 69 6c 65 6e 61 6d 65 20 62 65 67 69 he filename begi
9620: 6e 73 20 77 69 74 68 20 61 5c 6e 22 29 3b 0a 09 ns with a\n");..
9630: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
9640: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
9650: 20 20 20 20 20 20 70 69 70 65 20 28 5c 22 7c 5c pipe (\"|\
9660: 22 29 20 74 68 65 6e 20 61 20 70 72 6f 63 65 73 ") then a proces
9670: 73 20 69 73 20 73 74 61 72 74 65 64 20 61 6e 64 s is started and
9680: 20 75 73 65 64 20 66 6f 72 20 6c 6f 67 67 69 6e used for loggin
9690: 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 g\n");...fprintf
96a0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
96b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
96c0: 73 74 65 61 64 20 6f 66 20 61 20 66 69 6c 65 2e stead of a file.
96d0: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 The default is
96e0: 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 4c 4f 47 \"%s\".\n", LOG
96f0: 5f 46 49 4c 45 29 3b 0a 23 69 66 64 65 66 20 46 _FILE);.#ifdef F
9700: 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 09 09 ILED_DONT_LOG...
9710: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
9720: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
9730: 20 20 20 20 20 4e 6f 74 65 20 74 68 61 74 20 6c Note that l
9740: 6f 67 67 69 6e 67 20 69 73 20 63 6f 6d 70 6c 65 ogging is comple
9750: 74 65 6c 79 20 64 69 73 61 62 6c 65 64 20 73 6f tely disabled so
9760: 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 64 6f 65 this option doe
9770: 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 s\n");...fprintf
9780: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
9790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f no
97a0: 74 68 69 6e 67 20 69 6e 20 74 68 69 73 20 62 75 thing in this bu
97b0: 69 6c 64 2e 5c 6e 22 29 3b 0a 23 65 6e 64 69 66 ild.\n");.#endif
97c0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
97d0: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t, "\n");...fpri
97e0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
97f0: 20 20 20 2d 75 20 28 6f 72 20 2d 2d 75 73 65 72 -u (or --user
9800: 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 ) specifies the
9810: 75 73 65 72 20 74 6f 20 73 77 69 74 63 68 20 75 user to switch u
9820: 73 65 72 20 49 44 73 20 74 6f 20 62 65 66 6f 72 ser IDs to befor
9830: 65 20 73 65 72 76 69 63 69 6e 67 5c 6e 22 29 3b e servicing\n");
9840: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9850: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
9860: 20 20 20 20 20 20 20 20 20 72 65 71 75 65 73 74 request
9870: 73 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 s. The default
9880: 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 75 73 is not change us
9890: 65 72 20 49 44 73 2e 5c 6e 22 29 3b 0a 09 09 66 er IDs.\n");...f
98a0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
98b0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
98c0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
98d0: 72 20 28 6f 72 20 2d 2d 72 6f 6f 74 29 20 73 70 r (or --root) sp
98e0: 65 63 69 66 69 65 73 20 74 68 65 20 64 69 72 65 ecifies the dire
98f0: 63 74 6f 72 79 20 74 6f 20 61 63 74 20 61 73 20 ctory to act as
9900: 74 68 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f the root directo
9910: 72 79 20 66 6f 72 5c 6e 22 29 3b 0a 09 09 66 70 ry for\n");...fp
9920: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
9930: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9940: 20 20 20 20 74 68 65 20 66 69 6c 65 20 73 65 72 the file ser
9950: 76 65 72 2e 20 20 49 66 20 74 68 69 73 20 6f 70 ver. If this op
9960: 74 69 6f 6e 20 69 73 20 73 70 65 63 69 66 69 65 tion is specifie
9970: 64 2c 20 63 68 72 6f 6f 74 28 32 29 5c 6e 22 29 d, chroot(2)\n")
9980: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
9990: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
99a0: 20 20 20 20 20 20 20 20 20 20 69 73 20 63 61 6c is cal
99b0: 6c 65 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c led. The defaul
99c0: 74 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 t is not change
99d0: 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73 root directories
99e0: 2c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 ,\n");...fprintf
99f0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
9a00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
9a10: 68 61 74 20 69 73 2c 20 74 68 65 20 5c 22 2f 5c hat is, the \"/\
9a20: 22 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 73 " directory is s
9a30: 68 61 72 65 64 20 6f 75 74 2e 20 20 54 68 69 73 hared out. This
9a40: 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 72 will\n");...fpr
9a50: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
9a60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a70: 20 20 20 6c 69 6b 65 6c 79 20 62 65 20 61 20 73 likely be a s
9a80: 65 63 75 72 69 74 79 20 69 73 73 75 65 2c 20 73 ecurity issue, s
9a90: 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 73 68 o this option sh
9aa0: 6f 75 6c 64 20 61 6c 77 61 79 73 5c 6e 22 29 3b ould always\n");
9ab0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9ac0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
9ad0: 20 20 20 20 20 20 20 20 20 62 65 20 75 73 65 64 be used
9ae0: 2e 5c 6e 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 .\n");..}...retu
9af0: 72 6e 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 20 rn;.}../* Add a
9b00: 67 65 74 6f 70 74 20 6f 70 74 69 6f 6e 20 2a 2f getopt option */
9b10: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c .static void fil
9b20: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 ed_getopt_long_s
9b30: 65 74 6f 70 74 28 73 74 72 75 63 74 20 6f 70 74 etopt(struct opt
9b40: 69 6f 6e 20 2a 6f 70 74 2c 20 63 6f 6e 73 74 20 ion *opt, const
9b50: 63 68 61 72 20 2a 6e 61 6d 65 2c 20 69 6e 74 20 char *name, int
9b60: 68 61 73 5f 61 72 67 2c 20 69 6e 74 20 76 61 6c has_arg, int val
9b70: 29 20 7b 0a 09 6f 70 74 2d 3e 6e 61 6d 65 20 20 ) {..opt->name
9b80: 20 20 20 3d 20 6e 61 6d 65 3b 0a 09 6f 70 74 2d = name;..opt-
9b90: 3e 68 61 73 5f 61 72 67 20 20 3d 20 68 61 73 5f >has_arg = has_
9ba0: 61 72 67 3b 0a 09 6f 70 74 2d 3e 66 6c 61 67 20 arg;..opt->flag
9bb0: 20 20 20 20 3d 20 4e 55 4c 4c 3b 0a 09 6f 70 74 = NULL;..opt
9bc0: 2d 3e 76 61 6c 20 20 20 20 20 20 3d 20 76 61 6c ->val = val
9bd0: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f ;...return;.}../
9be0: 2a 20 52 65 73 6f 6c 76 65 20 61 20 75 73 65 72 * Resolve a user
9bf0: 6e 61 6d 65 20 74 6f 20 61 20 55 49 44 20 2a 2f name to a UID */
9c00: 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 .static int file
9c10: 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 63 6f d_user_lookup(co
9c20: 6e 73 74 20 63 68 61 72 20 2a 75 73 65 72 2c 20 nst char *user,
9c30: 75 69 64 5f 74 20 2a 75 73 65 72 5f 69 64 29 20 uid_t *user_id)
9c40: 7b 0a 09 63 68 61 72 20 2a 6e 65 78 74 3b 0a 09 {..char *next;..
9c50: 75 69 64 5f 74 20 75 73 65 72 5f 69 64 5f 63 68 uid_t user_id_ch
9c60: 65 63 6b 3b 0a 23 69 66 6e 64 65 66 20 46 49 4c eck;.#ifndef FIL
9c70: 45 44 5f 4e 4f 5f 47 45 54 50 57 4e 41 4d 0a 09 ED_NO_GETPWNAM..
9c80: 73 74 72 75 63 74 20 70 61 73 73 77 64 20 2a 65 struct passwd *e
9c90: 6e 74 3b 0a 0a 09 65 6e 74 20 3d 20 67 65 74 70 nt;...ent = getp
9ca0: 77 6e 61 6d 28 75 73 65 72 29 3b 0a 09 69 66 20 wnam(user);..if
9cb0: 28 65 6e 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a (ent != NULL) {.
9cc0: 09 09 2a 75 73 65 72 5f 69 64 20 3d 20 65 6e 74 ..*user_id = ent
9cd0: 2d 3e 70 77 5f 75 69 64 3b 0a 0a 09 09 72 65 74 ->pw_uid;....ret
9ce0: 75 72 6e 28 30 29 3b 0a 09 7d 0a 23 65 6e 64 69 urn(0);..}.#endi
9cf0: 66 0a 0a 09 75 73 65 72 5f 69 64 5f 63 68 65 63 f...user_id_chec
9d00: 6b 20 3d 20 73 74 72 74 6f 75 6c 6c 28 75 73 65 k = strtoull(use
9d10: 72 2c 20 26 6e 65 78 74 2c 20 31 30 29 3b 0a 09 r, &next, 10);..
9d20: 69 66 20 28 6e 65 78 74 20 3d 3d 20 4e 55 4c 4c if (next == NULL
9d30: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b ) {...return(1);
9d40: 0a 09 7d 0a 0a 09 69 66 20 28 6e 65 78 74 5b 30 ..}...if (next[0
9d50: 5d 20 21 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 ] != '\0') {...r
9d60: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2a eturn(1);..}...*
9d70: 75 73 65 72 5f 69 64 20 3d 20 75 73 65 72 5f 69 user_id = user_i
9d80: 64 5f 63 68 65 63 6b 3b 0a 0a 09 72 65 74 75 72 d_check;...retur
9d90: 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 61 65 6d n(0);.}../* Daem
9da0: 6f 6e 69 7a 65 20 2a 2f 0a 73 74 61 74 69 63 20 onize */.static
9db0: 69 6e 74 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e int filed_daemon
9dc0: 69 7a 65 28 76 6f 69 64 29 20 7b 0a 09 70 69 64 ize(void) {..pid
9dd0: 5f 74 20 73 65 74 73 69 64 5f 72 65 74 2c 20 66 _t setsid_ret, f
9de0: 6f 72 6b 5f 72 65 74 3b 0a 09 69 6e 74 20 63 68 ork_ret;..int ch
9df0: 64 69 72 5f 72 65 74 2c 20 64 75 70 32 5f 72 65 dir_ret, dup2_re
9e00: 74 3b 0a 09 69 6e 74 20 66 64 5f 69 6e 2c 20 66 t;..int fd_in, f
9e10: 64 5f 6f 75 74 3b 0a 0a 09 63 68 64 69 72 5f 72 d_out;...chdir_r
9e20: 65 74 20 3d 20 63 68 64 69 72 28 22 2f 22 29 3b et = chdir("/");
9e30: 0a 09 69 66 20 28 63 68 64 69 72 5f 72 65 74 20 ..if (chdir_ret
9e40: 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e != 0) {...return
9e50: 28 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 6b 5f 72 (1);..}...fork_r
9e60: 65 74 20 3d 20 66 6f 72 6b 28 29 3b 0a 09 69 66 et = fork();..if
9e70: 20 28 66 6f 72 6b 5f 72 65 74 20 3c 20 30 29 20 (fork_ret < 0)
9e80: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 {...return(1);..
9e90: 7d 0a 0a 09 69 66 20 28 66 6f 72 6b 5f 72 65 74 }...if (fork_ret
9ea0: 20 3e 20 30 29 20 7b 0a 09 09 2f 2a 20 50 61 72 > 0) {.../* Par
9eb0: 65 6e 74 20 2a 2f 0a 09 09 77 61 69 74 70 69 64 ent */...waitpid
9ec0: 28 66 6f 72 6b 5f 72 65 74 2c 20 4e 55 4c 4c 2c (fork_ret, NULL,
9ed0: 20 30 29 3b 0a 0a 09 09 65 78 69 74 28 45 58 49 0);....exit(EXI
9ee0: 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a 0a T_SUCCESS);..}..
9ef0: 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 69 66 ./* Child */..if
9f00: 20 28 66 6f 72 6b 28 29 20 21 3d 20 30 29 20 7b (fork() != 0) {
9f10: 0a 09 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 .../* Child */..
9f20: 09 65 78 69 74 28 45 58 49 54 5f 53 55 43 43 45 .exit(EXIT_SUCCE
9f30: 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 47 72 61 SS);..}.../* Gra
9f40: 6e 64 20 63 68 69 6c 64 20 2a 2f 0a 09 73 65 74 nd child */..set
9f50: 73 69 64 5f 72 65 74 20 3d 20 73 65 74 73 69 64 sid_ret = setsid
9f60: 28 29 3b 0a 09 69 66 20 28 73 65 74 73 69 64 5f ();..if (setsid_
9f70: 72 65 74 20 3d 3d 20 28 28 70 69 64 5f 74 29 20 ret == ((pid_t)
9f80: 2d 31 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 -1)) {...return(
9f90: 31 29 3b 0a 09 7d 0a 0a 09 66 64 5f 69 6e 20 3d 1);..}...fd_in =
9fa0: 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c open("/dev/null
9fb0: 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 09 66 ", O_RDONLY);..f
9fc0: 64 5f 6f 75 74 20 3d 20 6f 70 65 6e 28 22 2f 64 d_out = open("/d
9fd0: 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 52 4f 4e ev/null", O_WRON
9fe0: 4c 59 29 3b 0a 09 69 66 20 28 66 64 5f 69 6e 20 LY);..if (fd_in
9ff0: 3c 20 30 20 7c 7c 20 66 64 5f 6f 75 74 20 3c 20 < 0 || fd_out <
a000: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 0) {...return(1)
a010: 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 ;..}...dup2_ret
a020: 3d 20 64 75 70 32 28 66 64 5f 69 6e 2c 20 53 54 = dup2(fd_in, ST
a030: 44 49 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 DIN_FILENO);..if
a040: 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54 (dup2_ret != ST
a050: 44 49 4e 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 DIN_FILENO) {...
a060: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 return(1);..}...
a070: 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 dup2_ret = dup2(
a080: 66 64 5f 6f 75 74 2c 20 53 54 44 4f 55 54 5f 46 fd_out, STDOUT_F
a090: 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 ILENO);..if (dup
a0a0: 32 5f 72 65 74 20 21 3d 20 53 54 44 4f 55 54 5f 2_ret != STDOUT_
a0b0: 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 FILENO) {...retu
a0c0: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 rn(1);..}...dup2
a0d0: 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f _ret = dup2(fd_o
a0e0: 75 74 2c 20 53 54 44 45 52 52 5f 46 49 4c 45 4e ut, STDERR_FILEN
a0f0: 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 O);..if (dup2_re
a100: 74 20 21 3d 20 53 54 44 45 52 52 5f 46 49 4c 45 t != STDERR_FILE
a110: 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 NO) {...return(1
a120: 29 3b 0a 09 7d 0a 0a 09 63 6c 6f 73 65 28 66 64 );..}...close(fd
a130: 5f 69 6e 29 3b 0a 09 63 6c 6f 73 65 28 66 64 5f _in);..close(fd_
a140: 6f 75 74 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 out);...return(0
a150: 29 3b 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 70 72 6f );.}../* Run pro
a160: 63 65 73 73 20 2a 2f 0a 69 6e 74 20 6d 61 69 6e cess */.int main
a170: 28 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 (int argc, char
a180: 2a 2a 61 72 67 76 29 20 7b 0a 09 73 74 72 75 63 **argv) {..struc
a190: 74 20 6f 70 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 t option options
a1a0: 5b 31 32 5d 3b 0a 09 73 74 72 75 63 74 20 66 69 [12];..struct fi
a1b0: 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 74 68 72 65 led_options thre
a1c0: 61 64 5f 6f 70 74 69 6f 6e 73 3b 0a 09 63 6f 6e ad_options;..con
a1d0: 73 74 20 63 68 61 72 20 2a 62 69 6e 64 5f 61 64 st char *bind_ad
a1e0: 64 72 20 3d 20 42 49 4e 44 5f 41 44 44 52 2c 20 dr = BIND_ADDR,
a1f0: 2a 6e 65 77 72 6f 6f 74 20 3d 20 4e 55 4c 4c 2c *newroot = NULL,
a200: 20 2a 6c 6f 67 5f 66 69 6c 65 20 3d 20 4c 4f 47 *log_file = LOG
a210: 5f 46 49 4c 45 3b 0a 09 46 49 4c 45 20 2a 6c 6f _FILE;..FILE *lo
a220: 67 5f 66 70 3b 0a 09 75 69 64 5f 74 20 75 73 65 g_fp;..uid_t use
a230: 72 20 3d 20 30 3b 0a 09 69 6e 74 20 70 6f 72 74 r = 0;..int port
a240: 20 3d 20 50 4f 52 54 2c 20 74 68 72 65 61 64 5f = PORT, thread_
a250: 63 6f 75 6e 74 20 3d 20 54 48 52 45 41 44 5f 43 count = THREAD_C
a260: 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 61 63 68 65 OUNT;..int cache
a270: 5f 73 69 7a 65 20 3d 20 43 41 43 48 45 5f 53 49 _size = CACHE_SI
a280: 5a 45 3b 0a 09 69 6e 74 20 69 6e 69 74 5f 72 65 ZE;..int init_re
a290: 74 2c 20 63 68 72 6f 6f 74 5f 72 65 74 2c 20 73 t, chroot_ret, s
a2a0: 65 74 75 69 64 5f 72 65 74 2c 20 6c 6f 6f 6b 75 etuid_ret, looku
a2b0: 70 5f 72 65 74 2c 20 63 68 64 69 72 5f 72 65 74 p_ret, chdir_ret
a2c0: 3b 0a 09 69 6e 74 20 73 65 74 75 69 64 5f 65 6e ;..int setuid_en
a2d0: 61 62 6c 65 64 20 3d 20 30 2c 20 64 61 65 6d 6f abled = 0, daemo
a2e0: 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 09 n_enabled = 0;..
a2f0: 69 6e 74 20 63 68 3b 0a 09 69 6e 74 20 66 64 3b int ch;..int fd;
a300: 0a 0a 09 2f 2a 20 53 65 74 20 64 65 66 61 75 6c .../* Set defaul
a310: 74 20 76 61 6c 75 65 73 20 2a 2f 0a 09 74 68 72 t values */..thr
a320: 65 61 64 5f 6f 70 74 69 6f 6e 73 2e 76 68 6f 73 ead_options.vhos
a330: 74 73 5f 65 6e 61 62 6c 65 64 20 3d 20 30 3b 0a ts_enabled = 0;.
a340: 0a 09 2f 2a 20 50 72 6f 63 65 73 73 20 61 72 67 ../* Process arg
a350: 75 6d 65 6e 74 73 20 2a 2f 0a 09 66 69 6c 65 64 uments */..filed
a360: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 _getopt_long_set
a370: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 30 5d 2c opt(&options[0],
a380: 20 22 70 6f 72 74 22 2c 20 72 65 71 75 69 72 65 "port", require
a390: 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 70 27 29 d_argument, 'p')
a3a0: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f ;..filed_getopt_
a3b0: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 long_setopt(&opt
a3c0: 69 6f 6e 73 5b 31 5d 2c 20 22 74 68 72 65 61 64 ions[1], "thread
a3d0: 73 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 s", required_arg
a3e0: 75 6d 65 6e 74 2c 20 27 74 27 29 3b 0a 09 66 69 ument, 't');..fi
a3f0: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f led_getopt_long_
a400: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b setopt(&options[
a410: 32 5d 2c 20 22 63 61 63 68 65 22 2c 20 72 65 71 2], "cache", req
a420: 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 uired_argument,
a430: 27 63 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 'c');..filed_get
a440: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 opt_long_setopt(
a450: 26 6f 70 74 69 6f 6e 73 5b 33 5d 2c 20 22 62 69 &options[3], "bi
a460: 6e 64 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 nd", required_ar
a470: 67 75 6d 65 6e 74 2c 20 27 62 27 29 3b 0a 09 66 gument, 'b');..f
a480: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
a490: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
a4a0: 5b 34 5d 2c 20 22 75 73 65 72 22 2c 20 72 65 71 [4], "user", req
a4b0: 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 uired_argument,
a4c0: 27 75 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 'u');..filed_get
a4d0: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 opt_long_setopt(
a4e0: 26 6f 70 74 69 6f 6e 73 5b 35 5d 2c 20 22 72 6f &options[5], "ro
a4f0: 6f 74 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 ot", required_ar
a500: 67 75 6d 65 6e 74 2c 20 27 72 27 29 3b 0a 09 66 gument, 'r');..f
a510: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
a520: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
a530: 5b 36 5d 2c 20 22 68 65 6c 70 22 2c 20 6e 6f 5f [6], "help", no_
a540: 61 72 67 75 6d 65 6e 74 2c 20 27 68 27 29 3b 0a argument, 'h');.
a550: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
a560: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
a570: 6e 73 5b 37 5d 2c 20 22 64 61 65 6d 6f 6e 22 2c ns[7], "daemon",
a580: 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 64 no_argument, 'd
a590: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 ');..filed_getop
a5a0: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f t_long_setopt(&o
a5b0: 70 74 69 6f 6e 73 5b 38 5d 2c 20 22 6c 6f 67 22 ptions[8], "log"
a5c0: 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d , required_argum
a5d0: 65 6e 74 2c 20 27 6c 27 29 3b 0a 09 66 69 6c 65 ent, 'l');..file
a5e0: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 d_getopt_long_se
a5f0: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 39 5d topt(&options[9]
a600: 2c 20 22 76 65 72 73 69 6f 6e 22 2c 20 6e 6f 5f , "version", no_
a610: 61 72 67 75 6d 65 6e 74 2c 20 27 76 27 29 3b 0a argument, 'v');.
a620: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
a630: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
a640: 6e 73 5b 31 30 5d 2c 20 22 76 68 6f 73 74 22 2c ns[10], "vhost",
a650: 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 56 no_argument, 'V
a660: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 ');..filed_getop
a670: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f t_long_setopt(&o
a680: 70 74 69 6f 6e 73 5b 31 31 5d 2c 20 4e 55 4c 4c ptions[11], NULL
a690: 2c 20 30 2c 20 30 29 3b 0a 09 77 68 69 6c 65 20 , 0, 0);..while
a6a0: 28 28 63 68 20 3d 20 67 65 74 6f 70 74 5f 6c 6f ((ch = getopt_lo
a6b0: 6e 67 28 61 72 67 63 2c 20 61 72 67 76 2c 20 22 ng(argc, argv, "
a6c0: 70 3a 74 3a 63 3a 62 3a 75 3a 72 3a 6c 3a 68 64 p:t:c:b:u:r:l:hd
a6d0: 76 56 22 2c 20 6f 70 74 69 6f 6e 73 2c 20 4e 55 vV", options, NU
a6e0: 4c 4c 29 29 20 21 3d 20 2d 31 29 20 7b 0a 09 09 LL)) != -1) {...
a6f0: 73 77 69 74 63 68 28 63 68 29 20 7b 0a 09 09 09 switch(ch) {....
a700: 63 61 73 65 20 27 70 27 3a 0a 09 09 09 09 70 6f case 'p':.....po
a710: 72 74 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 rt = atoi(optarg
a720: 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 );.....break;...
a730: 09 63 61 73 65 20 27 74 27 3a 0a 09 09 09 09 74 .case 't':.....t
a740: 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d 20 61 74 hread_count = at
a750: 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 oi(optarg);.....
a760: 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 break;....case '
a770: 63 27 3a 0a 09 09 09 09 63 61 63 68 65 5f 73 69 c':.....cache_si
a780: 7a 65 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 ze = atoi(optarg
a790: 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 );.....break;...
a7a0: 09 63 61 73 65 20 27 62 27 3a 0a 09 09 09 09 62 .case 'b':.....b
a7b0: 69 6e 64 5f 61 64 64 72 20 3d 20 73 74 72 64 75 ind_addr = strdu
a7c0: 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 p(optarg);.....b
a7d0: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 75 reak;....case 'u
a7e0: 27 3a 0a 09 09 09 09 73 65 74 75 69 64 5f 65 6e ':.....setuid_en
a7f0: 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 6c abled = 1;.....l
a800: 6f 6f 6b 75 70 5f 72 65 74 20 3d 20 66 69 6c 65 ookup_ret = file
a810: 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 6f 70 d_user_lookup(op
a820: 74 61 72 67 2c 20 26 75 73 65 72 29 3b 0a 09 09 targ, &user);...
a830: 09 09 69 66 20 28 6c 6f 6f 6b 75 70 5f 72 65 74 ..if (lookup_ret
a840: 20 21 3d 20 30 29 20 7b 0a 09 09 09 09 09 66 69 != 0) {......fi
a850: 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73 led_print_help(s
a860: 74 64 65 72 72 2c 20 30 2c 20 22 49 6e 76 61 6c tderr, 0, "Inval
a870: 69 64 20 75 73 65 72 6e 61 6d 65 20 73 70 65 63 id username spec
a880: 69 66 69 65 64 22 29 3b 0a 0a 09 09 09 09 09 72 ified");.......r
a890: 65 74 75 72 6e 28 31 29 3b 0a 09 09 09 09 7d 0a eturn(1);.....}.
a8a0: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 ....break;....ca
a8b0: 73 65 20 27 72 27 3a 0a 09 09 09 09 6e 65 77 72 se 'r':.....newr
a8c0: 6f 6f 74 20 3d 20 73 74 72 64 75 70 28 6f 70 74 oot = strdup(opt
a8d0: 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b arg);.....break;
a8e0: 0a 09 09 09 63 61 73 65 20 27 6c 27 3a 0a 09 09 ....case 'l':...
a8f0: 09 09 6c 6f 67 5f 66 69 6c 65 20 3d 20 73 74 72 ..log_file = str
a900: 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 dup(optarg);....
a910: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
a920: 27 64 27 3a 0a 09 09 09 09 64 61 65 6d 6f 6e 5f 'd':.....daemon_
a930: 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 enabled = 1;....
a940: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
a950: 27 56 27 3a 0a 09 09 09 09 74 68 72 65 61 64 5f 'V':.....thread_
a960: 6f 70 74 69 6f 6e 73 2e 76 68 6f 73 74 73 5f 65 options.vhosts_e
a970: 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 0a 09 09 09 nabled = 1;.....
a980: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
a990: 27 76 27 3a 0a 09 09 09 09 70 72 69 6e 74 66 28 'v':.....printf(
a9a0: 22 66 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 25 "filed version %
a9b0: 73 5c 6e 22 2c 20 46 49 4c 45 44 5f 56 45 52 53 s\n", FILED_VERS
a9c0: 49 4f 4e 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 ION);......retur
a9d0: 6e 28 30 29 3b 0a 09 09 09 63 61 73 65 20 27 3f n(0);....case '?
a9e0: 27 3a 0a 09 09 09 63 61 73 65 20 27 3a 27 3a 0a ':....case ':':.
a9f0: 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f ....filed_print_
aa00: 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20 help(stderr, 0,
aa10: 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75 NULL);......retu
aa20: 72 6e 28 31 29 3b 0a 09 09 09 63 61 73 65 20 27 rn(1);....case '
aa30: 68 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 72 h':.....filed_pr
aa40: 69 6e 74 5f 68 65 6c 70 28 73 74 64 6f 75 74 2c int_help(stdout,
aa50: 20 31 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 1, NULL);......
aa60: 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 7d 0a 09 return(0);...}..
aa70: 7d 0a 0a 09 2f 2a 20 4f 70 65 6e 20 6c 6f 67 20 }.../* Open log
aa80: 66 69 6c 65 20 2a 2f 0a 09 6c 6f 67 5f 66 70 20 file */..log_fp
aa90: 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e = filed_log_open
aaa0: 28 6c 6f 67 5f 66 69 6c 65 29 3b 0a 09 69 66 20 (log_file);..if
aab0: 28 6c 6f 67 5f 66 70 20 3d 3d 20 4e 55 4c 4c 29 (log_fp == NULL)
aac0: 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c {...perror("fil
aad0: 65 64 5f 6c 6f 67 5f 6f 70 65 6e 22 29 3b 0a 0a ed_log_open");..
aae0: 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d 0a ..return(4);..}.
aaf0: 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 69 73 74 ../* Create list
ab00: 65 6e 69 6e 67 20 73 6f 63 6b 65 74 20 2a 2f 0a ening socket */.
ab10: 09 66 64 20 3d 20 66 69 6c 65 64 5f 6c 69 73 74 .fd = filed_list
ab20: 65 6e 28 62 69 6e 64 5f 61 64 64 72 2c 20 70 6f en(bind_addr, po
ab30: 72 74 29 3b 0a 09 69 66 20 28 66 64 20 3c 20 30 rt);..if (fd < 0
ab40: 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 ) {...perror("fi
ab50: 6c 65 64 5f 6c 69 73 74 65 6e 22 29 3b 0a 0a 09 led_listen");...
ab60: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a .return(1);..}..
ab70: 09 2f 2a 20 42 65 63 6f 6d 65 20 61 20 64 61 65 ./* Become a dae
ab80: 6d 6f 6e 20 2a 2f 0a 09 69 66 20 28 64 61 65 6d mon */..if (daem
ab90: 6f 6e 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 on_enabled) {...
aba0: 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 init_ret = filed
abb0: 5f 64 61 65 6d 6f 6e 69 7a 65 28 29 3b 0a 09 09 _daemonize();...
abc0: 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 if (init_ret !=
abd0: 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 0) {....perror("
abe0: 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 22 filed_daemonize"
abf0: 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 36 29 );.....return(6)
ac00: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 43 68 ;...}..}.../* Ch
ac10: 72 6f 6f 74 2c 20 69 66 20 61 70 70 72 6f 70 72 root, if appropr
ac20: 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 6e 65 77 iate */..if (new
ac30: 72 6f 6f 74 29 20 7b 0a 09 09 63 68 64 69 72 5f root) {...chdir_
ac40: 72 65 74 20 3d 20 63 68 64 69 72 28 6e 65 77 72 ret = chdir(newr
ac50: 6f 6f 74 29 3b 0a 09 09 69 66 20 28 63 68 64 69 oot);...if (chdi
ac60: 72 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 r_ret != 0) {...
ac70: 09 70 65 72 72 6f 72 28 22 63 68 64 69 72 22 29 .perror("chdir")
ac80: 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b ;.....return(1);
ac90: 0a 09 09 7d 0a 0a 09 09 63 68 72 6f 6f 74 5f 72 ...}....chroot_r
aca0: 65 74 20 3d 20 63 68 72 6f 6f 74 28 22 2e 22 29 et = chroot(".")
acb0: 3b 0a 09 09 69 66 20 28 63 68 72 6f 6f 74 5f 72 ;...if (chroot_r
acc0: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 et != 0) {....pe
acd0: 72 72 6f 72 28 22 63 68 72 6f 6f 74 22 29 3b 0a rror("chroot");.
ace0: 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 ....return(1);..
acf0: 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 72 6f 70 20 .}..}.../* Drop
ad00: 70 72 69 76 69 6c 65 67 65 73 2c 20 69 66 20 61 privileges, if a
ad10: 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09 69 ppropriate */..i
ad20: 66 20 28 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 f (setuid_enable
ad30: 64 29 20 7b 0a 09 09 73 65 74 75 69 64 5f 72 65 d) {...setuid_re
ad40: 74 20 3d 20 73 65 74 75 69 64 28 75 73 65 72 29 t = setuid(user)
ad50: 3b 0a 09 09 69 66 20 28 73 65 74 75 69 64 5f 72 ;...if (setuid_r
ad60: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 et != 0) {....pe
ad70: 72 72 6f 72 28 22 73 65 74 75 69 64 22 29 3b 0a rror("setuid");.
ad80: 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 ....return(1);..
ad90: 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 .}..}.../* Initi
ada0: 61 6c 69 7a 65 20 2a 2f 0a 09 69 6e 69 74 5f 72 alize */..init_r
adb0: 65 74 20 3d 20 66 69 6c 65 64 5f 69 6e 69 74 28 et = filed_init(
adc0: 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 cache_size);..if
add0: 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 (init_ret != 0)
ade0: 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c {...perror("fil
adf0: 65 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 ed_init");....re
ae00: 74 75 72 6e 28 33 29 3b 0a 09 7d 0a 0a 09 2f 2a turn(3);..}.../*
ae10: 20 43 72 65 61 74 65 20 6c 6f 67 67 69 6e 67 20 Create logging
ae20: 74 68 72 65 61 64 20 2a 2f 0a 09 69 6e 69 74 5f thread */..init_
ae30: 72 65 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 67 ret = filed_logg
ae40: 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 ing_thread_init(
ae50: 6c 6f 67 5f 66 70 29 3b 0a 09 69 66 20 28 69 6e log_fp);..if (in
ae60: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 it_ret != 0) {..
ae70: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c .perror("filed_l
ae80: 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e ogging_thread_in
ae90: 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 it");....return(
aea0: 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 4);..}.../* Crea
aeb0: 74 65 20 73 6f 63 6b 65 74 20 74 65 72 6d 69 6e te socket termin
aec0: 61 74 69 6f 6e 20 74 68 72 65 61 64 20 2a 2f 0a ation thread */.
aed0: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 .init_ret = file
aee0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
aef0: 74 68 72 65 61 64 5f 69 6e 69 74 28 29 3b 0a 09 thread_init();..
af00: 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 if (init_ret !=
af10: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 0) {...perror("f
af20: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
af30: 75 74 5f 74 68 72 65 61 64 5f 69 6e 69 74 22 29 ut_thread_init")
af40: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 36 29 3b 0a ;....return(6);.
af50: 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 77 .}.../* Create w
af60: 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a 2f orker threads */
af70: 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c ..init_ret = fil
af80: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 ed_worker_thread
af90: 73 5f 69 6e 69 74 28 66 64 2c 20 74 68 72 65 61 s_init(fd, threa
afa0: 64 5f 63 6f 75 6e 74 2c 20 26 74 68 72 65 61 64 d_count, &thread
afb0: 5f 6f 70 74 69 6f 6e 73 29 3b 0a 09 69 66 20 28 _options);..if (
afc0: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b init_ret != 0) {
afd0: 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 ...perror("filed
afe0: 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f _worker_threads_
aff0: 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 init");....retur
b000: 6e 28 35 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 61 n(5);..}.../* Wa
b010: 69 74 20 66 6f 72 20 74 68 72 65 61 64 73 20 74 it for threads t
b020: 6f 20 65 78 69 74 20 2a 2f 0a 09 2f 2a 20 58 58 o exit */../* XX
b030: 58 3a 54 4f 44 4f 3a 20 4d 6f 6e 69 74 6f 72 20 X:TODO: Monitor
b040: 74 68 72 65 61 64 20 75 73 61 67 65 20 2a 2f 0a thread usage */.
b050: 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 73 .while (1) {...s
b060: 6c 65 65 70 28 36 30 29 3b 0a 09 7d 0a 0a 09 2f leep(60);..}.../
b070: 2a 20 52 65 74 75 72 6e 20 69 6e 20 66 61 69 6c * Return in fail
b080: 75 72 65 20 2a 2f 0a 09 72 65 74 75 72 6e 28 32 ure */..return(2
b090: 29 3b 0a 7d 0a );.}.