Hex Artifact Content

Artifact f1e3a815c82f26a54ecf49a7770f04d2c0f4117f:


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 64 65 66 69 6e 65 20 46 49 4c 45 44  .#  define FILED
0860: 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55 54 20 31 0a  _DONT_TIMEOUT 1.
0870: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68  #endif../*. * Th
0880: 65 73 65 20 68 65 61 64 65 72 73 20 61 72 65 20  ese headers are 
0890: 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 66 6f  only required fo
08a0: 72 20 54 49 4d 45 4f 55 54 20 73 75 70 70 6f 72  r TIMEOUT suppor
08b0: 74 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 20 46 49  t. */.#ifndef FI
08c0: 4c 45 44 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55 54  LED_DONT_TIMEOUT
08d0: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 74  .#include <stdat
08e0: 6f 6d 69 63 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  omic.h>.#include
08f0: 20 3c 73 74 64 62 6f 6f 6c 2e 68 3e 0a 23 65 6e   <stdbool.h>.#en
0900: 64 69 66 0a 0a 2f 2a 20 43 6f 6d 70 69 6c 65 20  dif../* Compile 
0910: 74 69 6d 65 20 63 6f 6e 73 74 61 6e 74 73 20 2a  time constants *
0920: 2f 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f  /.#define FILED_
0930: 56 45 52 53 49 4f 4e 20 22 31 2e 32 30 22 0a 23  VERSION "1.20".#
0940: 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 53 45 4e  define FILED_SEN
0950: 44 46 49 4c 45 5f 4d 41 58 20 31 36 37 37 37 32  DFILE_MAX 167772
0960: 31 35 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44  15.#define FILED
0970: 5f 4d 41 58 5f 46 41 49 4c 55 52 45 5f 43 4f 55  _MAX_FAILURE_COU
0980: 4e 54 20 33 30 0a 23 64 65 66 69 6e 65 20 46 49  NT 30.#define FI
0990: 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45  LED_DEFAULT_TYPE
09a0: 20 22 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6f 63   "application/oc
09b0: 74 65 74 2d 73 74 72 65 61 6d 22 0a 23 64 65 66  tet-stream".#def
09c0: 69 6e 65 20 46 49 4c 45 44 5f 50 41 54 48 5f 42  ine FILED_PATH_B
09d0: 55 46 46 45 52 5f 53 49 5a 45 20 31 30 31 30 0a  UFFER_SIZE 1010.
09e0: 0a 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75  ./* Default valu
09f0: 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 4f  es */.#define PO
0a00: 52 54 20 38 30 0a 23 64 65 66 69 6e 65 20 54 48  RT 80.#define TH
0a10: 52 45 41 44 5f 43 4f 55 4e 54 20 35 0a 23 64 65  READ_COUNT 5.#de
0a20: 66 69 6e 65 20 42 49 4e 44 5f 41 44 44 52 20 22  fine BIND_ADDR "
0a30: 3a 3a 22 0a 23 64 65 66 69 6e 65 20 43 41 43 48  ::".#define CACH
0a40: 45 5f 53 49 5a 45 20 38 32 30 39 0a 23 64 65 66  E_SIZE 8209.#def
0a50: 69 6e 65 20 4c 4f 47 5f 46 49 4c 45 20 22 2d 22  ine LOG_FILE "-"
0a60: 0a 0a 2f 2a 20 46 75 7a 7a 69 6e 67 20 54 65 73  ../* Fuzzing Tes
0a70: 74 20 43 6f 64 65 20 2a 2f 0a 23 69 66 64 65 66  t Code */.#ifdef
0a80: 20 46 49 4c 45 44 5f 54 45 53 54 5f 41 46 4c 0a   FILED_TEST_AFL.
0a90: 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 44 4f  #define FILED_DO
0aa0: 4e 54 5f 4c 4f 47 20 31 0a 23 64 65 66 69 6e 65  NT_LOG 1.#define
0ab0: 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 54 49 4d 45   FILED_DONT_TIME
0ac0: 4f 55 54 20 31 0a 23 64 65 66 69 6e 65 20 70 74  OUT 1.#define pt
0ad0: 68 72 65 61 64 5f 63 72 65 61 74 65 28 61 2c 20  hread_create(a, 
0ae0: 78 2c 20 79 2c 20 7a 29 20 61 66 6c 5f 70 74 68  x, y, z) afl_pth
0af0: 72 65 61 64 5f 63 72 65 61 74 65 28 61 2c 20 78  read_create(a, x
0b00: 2c 20 79 2c 20 7a 29 0a 23 64 65 66 69 6e 65 20  , y, z).#define 
0b10: 62 69 6e 64 28 78 2c 20 79 2c 20 7a 29 20 61 66  bind(x, y, z) af
0b20: 6c 5f 62 69 6e 64 28 78 2c 20 79 2c 20 7a 29 0a  l_bind(x, y, z).
0b30: 23 64 65 66 69 6e 65 20 73 6f 63 6b 65 74 28 78  #define socket(x
0b40: 2c 20 79 2c 20 7a 29 20 38 31 39 33 0a 23 64 65  , y, z) 8193.#de
0b50: 66 69 6e 65 20 6c 69 73 74 65 6e 28 78 2c 20 79  fine listen(x, y
0b60: 29 20 30 0a 23 64 65 66 69 6e 65 20 61 63 63 65  ) 0.#define acce
0b70: 70 74 28 78 2c 20 79 2c 20 7a 29 20 61 66 6c 5f  pt(x, y, z) afl_
0b80: 61 63 63 65 70 74 28 78 2c 20 79 2c 20 7a 29 0a  accept(x, y, z).
0b90: 23 64 65 66 69 6e 65 20 63 6c 6f 73 65 28 78 29  #define close(x)
0ba0: 20 7b 20 69 66 20 28 73 74 72 63 6d 70 28 23 78   { if (strcmp(#x
0bb0: 2c 20 22 72 61 6e 64 6f 6d 5f 66 64 22 29 20 3d  , "random_fd") =
0bc0: 3d 20 30 29 20 7b 20 63 6c 6f 73 65 28 78 29 3b  = 0) { close(x);
0bd0: 20 7d 20 65 6c 73 65 20 7b 20 65 78 69 74 28 30   } else { exit(0
0be0: 29 3b 20 7d 20 7d 0a 23 64 65 66 69 6e 65 20 66  ); } }.#define f
0bf0: 63 6c 6f 73 65 28 78 29 20 65 78 69 74 28 30 29  close(x) exit(0)
0c00: 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 6c  ..static int afl
0c10: 5f 61 63 63 65 70 74 28 69 6e 74 20 78 2c 20 76  _accept(int x, v
0c20: 6f 69 64 20 2a 61 64 64 72 2c 20 76 6f 69 64 20  oid *addr, void 
0c30: 2a 7a 29 20 7b 0a 09 28 28 73 74 72 75 63 74 20  *z) {..((struct 
0c40: 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20  sockaddr_in6 *) 
0c50: 61 64 64 72 29 2d 3e 73 69 6e 36 5f 66 61 6d 69  addr)->sin6_fami
0c60: 6c 79 20 3d 20 41 46 5f 49 4e 45 54 20 2b 20 41  ly = AF_INET + A
0c70: 46 5f 49 4e 45 54 36 20 2b 20 31 3b 0a 09 72 65  F_INET6 + 1;..re
0c80: 74 75 72 6e 28 53 54 44 49 4e 5f 46 49 4c 45 4e  turn(STDIN_FILEN
0c90: 4f 29 3b 0a 09 78 20 3d 20 78 3b 0a 09 7a 20 3d  O);..x = x;..z =
0ca0: 20 7a 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e   z;.}..static in
0cb0: 74 20 61 66 6c 5f 62 69 6e 64 28 69 6e 74 20 78  t afl_bind(int x
0cc0: 2c 20 76 6f 69 64 20 2a 79 2c 20 73 6f 63 6b 6c  , void *y, sockl
0cd0: 65 6e 5f 74 20 7a 29 20 7b 0a 09 72 65 74 75 72  en_t z) {..retur
0ce0: 6e 28 38 31 39 34 29 3b 0a 09 78 20 3d 20 78 3b  n(8194);..x = x;
0cf0: 0a 09 79 20 3d 20 79 3b 0a 09 7a 20 3d 20 7a 3b  ..y = y;..z = z;
0d00: 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .}..static int a
0d10: 66 6c 5f 70 74 68 72 65 61 64 5f 63 72 65 61 74  fl_pthread_creat
0d20: 65 28 70 74 68 72 65 61 64 5f 74 20 2a 74 68 72  e(pthread_t *thr
0d30: 65 61 64 2c 20 63 6f 6e 73 74 20 70 74 68 72 65  ead, const pthre
0d40: 61 64 5f 61 74 74 72 5f 74 20 2a 61 74 74 72 2c  ad_attr_t *attr,
0d50: 20 76 6f 69 64 20 2a 28 2a 73 74 61 72 74 5f 72   void *(*start_r
0d60: 6f 75 74 69 6e 65 29 20 28 76 6f 69 64 20 2a 29  outine) (void *)
0d70: 2c 20 76 6f 69 64 20 2a 61 72 67 29 20 7b 0a 09  , void *arg) {..
0d80: 73 74 61 72 74 5f 72 6f 75 74 69 6e 65 28 61 72  start_routine(ar
0d90: 67 29 3b 0a 09 65 78 69 74 28 33 29 3b 0a 09 74  g);..exit(3);..t
0da0: 68 72 65 61 64 20 3d 20 74 68 72 65 61 64 3b 0a  hread = thread;.
0db0: 09 61 74 74 72 20 3d 20 61 74 74 72 3b 0a 7d 0a  .attr = attr;.}.
0dc0: 23 65 6e 64 69 66 0a 0a 2f 2a 20 43 6f 6e 66 69  #endif../* Confi
0dd0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  guration options
0de0: 20 74 68 61 74 20 77 6f 72 6b 20 74 68 72 65 61   that work threa
0df0: 64 73 20 6e 65 65 64 20 74 6f 20 62 65 20 61 77  ds need to be aw
0e00: 61 72 65 20 6f 66 20 2a 2f 0a 73 74 72 75 63 74  are of */.struct
0e10: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 7b   filed_options {
0e20: 0a 09 69 6e 74 20 76 68 6f 73 74 73 5f 65 6e 61  ..int vhosts_ena
0e30: 62 6c 65 64 3b 0a 09 63 6f 6e 73 74 20 63 68 61  bled;..const cha
0e40: 72 20 2a 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 3b  r *fake_newroot;
0e50: 0a 7d 3b 0a 0a 2f 2a 20 41 72 67 75 6d 65 6e 74  .};../* Argument
0e60: 73 20 66 6f 72 20 77 6f 72 6b 65 72 20 74 68 72  s for worker thr
0e70: 65 61 64 73 20 2a 2f 0a 73 74 72 75 63 74 20 66  eads */.struct f
0e80: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65  iled_worker_thre
0e90: 61 64 5f 61 72 67 73 20 7b 0a 09 69 6e 74 20 66  ad_args {..int f
0ea0: 64 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64  d;..struct filed
0eb0: 5f 6f 70 74 69 6f 6e 73 20 6f 70 74 69 6f 6e 73  _options options
0ec0: 3b 0a 7d 3b 0a 0a 2f 2a 20 41 72 67 75 6d 65 6e  ;.};../* Argumen
0ed0: 74 73 20 66 6f 72 20 6c 6f 67 67 69 6e 67 20 74  ts for logging t
0ee0: 68 72 65 61 64 73 20 2a 2f 0a 73 74 72 75 63 74  hreads */.struct
0ef0: 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74   filed_logging_t
0f00: 68 72 65 61 64 5f 61 72 67 73 20 7b 0a 09 46 49  hread_args {..FI
0f10: 4c 45 20 2a 66 70 3b 0a 7d 3b 0a 0a 2f 2a 20 46  LE *fp;.};../* F
0f20: 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ile information 
0f30: 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f  */.struct filed_
0f40: 66 69 6c 65 69 6e 66 6f 20 7b 0a 09 70 74 68 72  fileinfo {..pthr
0f50: 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65  ead_mutex_t mute
0f60: 78 3b 0a 09 63 68 61 72 20 70 61 74 68 5b 46 49  x;..char path[FI
0f70: 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f  LED_PATH_BUFFER_
0f80: 53 49 5a 45 5d 3b 0a 09 69 6e 74 20 66 64 3b 0a  SIZE];..int fd;.
0f90: 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09 63 68 61  .off_t len;..cha
0fa0: 72 20 2a 6c 61 73 74 6d 6f 64 3b 0a 09 63 68 61  r *lastmod;..cha
0fb0: 72 20 6c 61 73 74 6d 6f 64 5f 62 5b 36 34 5d 3b  r lastmod_b[64];
0fc0: 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 79  ..const char *ty
0fd0: 70 65 3b 0a 09 63 68 61 72 20 65 74 61 67 5b 36  pe;..char etag[6
0fe0: 34 5d 3b 0a 7d 3b 0a 0a 2f 2a 20 52 65 71 75 65  4];.};../* Reque
0ff0: 73 74 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a  st variables */.
1000: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74  struct filed_htt
1010: 70 5f 72 65 71 75 65 73 74 20 7b 0a 09 2f 2a 2a  p_request {../**
1020: 20 42 75 66 66 65 72 73 20 2a 2a 2f 0a 09 73 74   Buffers **/..st
1030: 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69  ruct filed_filei
1040: 6e 66 6f 20 66 69 6c 65 69 6e 66 6f 3b 0a 09 63  nfo fileinfo;..c
1050: 68 61 72 20 74 6d 70 62 75 66 5b 46 49 4c 45 44  har tmpbuf[FILED
1060: 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a  _PATH_BUFFER_SIZ
1070: 45 5d 3b 0a 0a 09 2f 2a 2a 20 48 54 54 50 20 52  E];.../** HTTP R
1080: 65 71 75 65 73 74 20 69 6e 66 6f 72 6d 61 74 69  equest informati
1090: 6f 6e 20 2a 2a 2f 0a 09 2f 2a 2a 2a 20 54 79 70  on **/../*** Typ
10a0: 65 20 6f 66 20 72 65 71 75 65 73 74 20 28 48 45  e of request (HE
10b0: 41 44 20 6f 72 20 47 45 54 29 20 2a 2a 2a 2f 0a  AD or GET) ***/.
10c0: 09 65 6e 75 6d 20 7b 0a 09 09 46 49 4c 45 44 5f  .enum {...FILED_
10d0: 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47  REQUEST_METHOD_G
10e0: 45 54 2c 0a 09 09 46 49 4c 45 44 5f 52 45 51 55  ET,...FILED_REQU
10f0: 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 0a  EST_METHOD_HEAD.
1100: 09 7d 20 6d 65 74 68 6f 64 3b 0a 0a 09 2f 2a 2a  .} method;.../**
1110: 2a 20 50 61 74 68 20 62 65 69 6e 67 20 72 65 71  * Path being req
1120: 75 65 73 74 65 64 20 2a 2a 2a 2f 0a 09 63 68 61  uested ***/..cha
1130: 72 20 70 61 74 68 5b 46 49 4c 45 44 5f 50 41 54  r path[FILED_PAT
1140: 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b 20  H_BUFFER_SIZE]; 
1150: 0a 0a 09 2f 2a 2a 2a 20 50 61 74 68 20 74 79 70  .../*** Path typ
1160: 65 20 2a 2a 2a 2f 0a 09 65 6e 75 6d 20 7b 0a 09  e ***/..enum {..
1170: 09 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54  .FILED_REQUEST_T
1180: 59 50 45 5f 44 49 52 45 43 54 4f 52 59 2c 0a 09  YPE_DIRECTORY,..
1190: 09 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54  .FILED_REQUEST_T
11a0: 59 50 45 5f 4f 54 48 45 52 0a 09 7d 20 74 79 70  YPE_OTHER..} typ
11b0: 65 3b 0a 0a 09 73 74 72 75 63 74 20 7b 0a 09 09  e;...struct {...
11c0: 73 74 72 75 63 74 20 7b 0a 09 09 09 69 6e 74 20  struct {....int 
11d0: 70 72 65 73 65 6e 74 3b 0a 09 09 09 6f 66 66 5f  present;....off_
11e0: 74 20 6f 66 66 73 65 74 3b 20 20 20 2f 2a 2a 2a  t offset;   /***
11f0: 20 52 61 6e 67 65 20 73 74 61 72 74 20 2a 2a 2a   Range start ***
1200: 2f 0a 09 09 09 6f 66 66 5f 74 20 6c 65 6e 67 74  /....off_t lengt
1210: 68 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67 65 20  h;   /*** Range 
1220: 6c 65 6e 67 74 68 20 2a 2a 2a 2f 0a 09 09 7d 20  length ***/...} 
1230: 72 61 6e 67 65 3b 0a 0a 09 09 73 74 72 75 63 74  range;....struct
1240: 20 7b 0a 09 09 09 69 6e 74 20 70 72 65 73 65 6e   {....int presen
1250: 74 3b 0a 09 09 09 63 68 61 72 20 68 6f 73 74 5b  t;....char host[
1260: 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45  FILED_PATH_BUFFE
1270: 52 5f 53 49 5a 45 5d 3b 0a 09 09 7d 20 68 6f 73  R_SIZE];...} hos
1280: 74 3b 0a 0a 09 09 65 6e 75 6d 20 7b 0a 09 09 09  t;....enum {....
1290: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e  FILED_CONNECTION
12a0: 5f 43 4c 4f 53 45 2c 0a 09 09 09 46 49 4c 45 44  _CLOSE,....FILED
12b0: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50  _CONNECTION_KEEP
12c0: 5f 41 4c 49 56 45 0a 09 09 7d 20 63 6f 6e 6e 65  _ALIVE...} conne
12d0: 63 74 69 6f 6e 3b 0a 09 7d 20 68 65 61 64 65 72  ction;..} header
12e0: 73 3b 0a 7d 3b 0a 0a 2f 2a 20 4c 6f 67 20 72 65  s;.};../* Log re
12f0: 63 6f 72 64 20 2a 2f 0a 73 74 72 75 63 74 20 66  cord */.struct f
1300: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 7b  iled_log_entry {
1310: 0a 09 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 67  ../* Type of log
1320: 20 65 6e 74 72 79 20 2a 2f 0a 09 65 6e 75 6d 20   entry */..enum 
1330: 7b 0a 09 09 46 49 4c 45 44 5f 4c 4f 47 5f 54 59  {...FILED_LOG_TY
1340: 50 45 5f 4d 45 53 53 41 47 45 2c 0a 09 09 46 49  PE_MESSAGE,...FI
1350: 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54 52 41  LED_LOG_TYPE_TRA
1360: 4e 53 46 45 52 0a 09 7d 20 74 79 70 65 3b 0a 0a  NSFER..} type;..
1370: 09 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20  ./* Linked list 
1380: 68 65 61 64 2f 74 61 69 6c 20 2a 2f 0a 09 73 74  head/tail */..st
1390: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65  ruct filed_log_e
13a0: 6e 74 72 79 20 2a 5f 6e 65 78 74 3b 0a 09 73 74  ntry *_next;..st
13b0: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65  ruct filed_log_e
13c0: 6e 74 72 79 20 2a 5f 70 72 65 76 3b 0a 0a 09 2f  ntry *_prev;.../
13d0: 2a 20 54 68 72 65 61 64 20 66 72 6f 6d 20 77 68  * Thread from wh
13e0: 69 63 68 20 74 68 69 73 20 6c 6f 67 20 65 6e 74  ich this log ent
13f0: 72 79 20 65 6d 69 6e 61 74 65 73 20 2a 2f 0a 09  ry eminates */..
1400: 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64  pthread_t thread
1410: 3b 0a 0a 09 2f 2a 20 4d 65 73 73 61 67 65 20 62  ;.../* Message b
1420: 75 66 66 65 72 20 66 6f 72 20 74 79 70 65 20 3d  uffer for type =
1430: 20 4d 45 53 53 41 47 45 20 2a 2f 0a 09 2f 2a 20   MESSAGE */../* 
1440: 50 61 74 68 20 62 75 66 66 65 72 20 66 6f 72 20  Path buffer for 
1450: 74 79 70 65 20 3d 20 54 52 41 4e 53 46 45 52 20  type = TRANSFER 
1460: 2a 2f 0a 09 63 68 61 72 20 62 75 66 66 65 72 5b  */..char buffer[
1470: 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45  FILED_PATH_BUFFE
1480: 52 5f 53 49 5a 45 5d 3b 0a 0a 09 2f 2a 20 49 74  R_SIZE];.../* It
1490: 65 6d 73 20 66 6f 72 20 74 79 70 65 20 3d 20 54  ems for type = T
14a0: 52 41 4e 53 46 45 52 20 2a 2f 0a 09 69 6e 74 20  RANSFER */..int 
14b0: 68 74 74 70 5f 63 6f 64 65 3b 0a 09 63 6f 6e 73  http_code;..cons
14c0: 74 20 63 68 61 72 20 2a 72 65 61 73 6f 6e 3b 0a  t char *reason;.
14d0: 09 74 69 6d 65 5f 74 20 73 74 61 72 74 74 69 6d  .time_t starttim
14e0: 65 3b 0a 09 74 69 6d 65 5f 74 20 65 6e 64 74 69  e;..time_t endti
14f0: 6d 65 3b 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6f  me;..off_t req_o
1500: 66 66 73 65 74 3b 0a 09 6f 66 66 5f 74 20 72 65  ffset;..off_t re
1510: 71 5f 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 5f 74  q_length;..off_t
1520: 20 73 65 6e 74 5f 6c 65 6e 67 74 68 3b 0a 09 6f   sent_length;..o
1530: 66 66 5f 74 20 66 69 6c 65 5f 6c 65 6e 67 74 68  ff_t file_length
1540: 3b 0a 09 63 68 61 72 20 69 70 5b 31 32 38 5d 3b  ;..char ip[128];
1550: 0a 09 69 6e 74 20 70 6f 72 74 3b 0a 09 69 6e 74  ..int port;..int
1560: 20 6d 65 74 68 6f 64 3b 0a 7d 3b 0a 0a 2f 2a 20   method;.};../* 
1570: 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73  Global variables
1580: 20 2a 2f 0a 2f 2a 2a 20 4f 70 65 6e 20 46 69 6c   */./** Open Fil
1590: 65 20 63 61 63 68 65 20 2a 2a 2f 0a 73 74 72 75  e cache **/.stru
15a0: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  ct filed_fileinf
15b0: 6f 20 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  o *filed_fileinf
15c0: 6f 5f 66 64 63 61 63 68 65 20 3d 20 4e 55 4c 4c  o_fdcache = NULL
15d0: 3b 0a 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 66  ;.unsigned int f
15e0: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64  iled_fileinfo_fd
15f0: 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 30 3b 0a  cache_size = 0;.
1600: 0a 2f 2a 2a 20 4c 6f 67 67 69 6e 67 20 2a 2a 2f  ./** Logging **/
1610: 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f  .struct filed_lo
1620: 67 5f 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c  g_entry *filed_l
1630: 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a 70 74 68  og_msg_list;.pth
1640: 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 66 69 6c  read_mutex_t fil
1650: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f  ed_log_msg_list_
1660: 6d 75 74 65 78 3b 0a 70 74 68 72 65 61 64 5f 63  mutex;.pthread_c
1670: 6f 6e 64 5f 74 20 66 69 6c 65 64 5f 6c 6f 67 5f  ond_t filed_log_
1680: 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 79 3b 0a  msg_list_ready;.
1690: 0a 2f 2a 20 53 69 67 6e 61 6c 20 48 61 6e 64 6c  ./* Signal Handl
16a0: 65 72 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  er */.static voi
16b0: 64 20 66 69 6c 65 64 5f 73 69 67 6e 61 6c 5f 68  d filed_signal_h
16c0: 61 6e 64 6c 65 72 28 69 6e 74 20 73 69 67 6e 61  andler(int signa
16d0: 6c 5f 6e 75 6d 62 65 72 29 20 7b 0a 09 73 74 72  l_number) {..str
16e0: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e  uct filed_filein
16f0: 66 6f 20 2a 63 61 63 68 65 3b 0a 09 75 6e 73 69  fo *cache;..unsi
1700: 67 6e 65 64 20 69 6e 74 20 69 64 78 3b 0a 0a 09  gned int idx;...
1710: 73 77 69 74 63 68 20 28 73 69 67 6e 61 6c 5f 6e  switch (signal_n
1720: 75 6d 62 65 72 29 20 7b 0a 09 09 63 61 73 65 20  umber) {...case 
1730: 53 49 47 48 55 50 3a 0a 09 09 09 66 6f 72 20 28  SIGHUP:....for (
1740: 69 64 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 66  idx = 0; idx < f
1750: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64  iled_fileinfo_fd
1760: 63 61 63 68 65 5f 73 69 7a 65 3b 20 69 64 78 2b  cache_size; idx+
1770: 2b 29 20 7b 0a 09 09 09 09 63 61 63 68 65 20 3d  +) {.....cache =
1780: 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f   &filed_fileinfo
1790: 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 3b 0a 0a  _fdcache[idx];..
17a0: 09 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65  ....pthread_mute
17b0: 78 5f 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d  x_lock(&cache->m
17c0: 75 74 65 78 29 3b 0a 0a 09 09 09 09 63 61 63 68  utex);......cach
17d0: 65 2d 3e 70 61 74 68 5b 30 5d 20 3d 20 27 5c 30  e->path[0] = '\0
17e0: 27 3b 0a 09 09 09 09 69 66 20 28 63 61 63 68 65  ';.....if (cache
17f0: 2d 3e 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09 09  ->fd >= 0) {....
1800: 09 09 63 6c 6f 73 65 28 63 61 63 68 65 2d 3e 66  ..close(cache->f
1810: 64 29 3b 0a 0a 09 09 09 09 09 63 61 63 68 65 2d  d);.......cache-
1820: 3e 66 64 20 3d 20 2d 31 3b 0a 09 09 09 09 7d 0a  >fd = -1;.....}.
1830: 0a 09 09 09 09 63 61 63 68 65 2d 3e 6c 61 73 74  .....cache->last
1840: 6d 6f 64 20 3d 20 22 22 3b 0a 09 09 09 09 63 61  mod = "";.....ca
1850: 63 68 65 2d 3e 74 79 70 65 20 3d 20 22 22 3b 0a  che->type = "";.
1860: 0a 09 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74  .....pthread_mut
1870: 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65  ex_unlock(&cache
1880: 2d 3e 6d 75 74 65 78 29 3b 0a 09 09 09 7d 0a 09  ->mutex);....}..
1890: 09 09 62 72 65 61 6b 3b 0a 09 7d 0a 0a 09 72 65  ..break;..}...re
18a0: 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69 74  turn;.}../* Init
18b0: 69 61 6c 69 7a 65 20 63 61 63 68 65 20 2a 2f 0a  ialize cache */.
18c0: 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64  static int filed
18d0: 5f 69 6e 69 74 5f 63 61 63 68 65 28 75 6e 73 69  _init_cache(unsi
18e0: 67 6e 65 64 20 69 6e 74 20 63 61 63 68 65 5f 73  gned int cache_s
18f0: 69 7a 65 29 20 7b 0a 09 75 6e 73 69 67 6e 65 64  ize) {..unsigned
1900: 20 69 6e 74 20 69 64 78 3b 0a 09 69 6e 74 20 6d   int idx;..int m
1910: 75 74 65 78 5f 69 6e 69 74 5f 72 65 74 3b 0a 0a  utex_init_ret;..
1920: 09 2f 2a 20 43 61 63 68 65 20 6d 61 79 20 6e 6f  ./* Cache may no
1930: 74 20 62 65 20 72 65 2d 69 6e 69 74 69 61 6c 69  t be re-initiali
1940: 7a 65 64 20 2a 2f 0a 09 69 66 20 28 66 69 6c 65  zed */..if (file
1950: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63  d_fileinfo_fdcac
1960: 68 65 5f 73 69 7a 65 20 21 3d 20 30 20 7c 7c 20  he_size != 0 || 
1970: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66  filed_fileinfo_f
1980: 64 63 61 63 68 65 20 21 3d 20 4e 55 4c 4c 29 20  dcache != NULL) 
1990: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  {...return(1);..
19a0: 7d 0a 0a 09 2f 2a 20 43 61 63 68 65 20 64 6f 65  }.../* Cache doe
19b0: 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  s not need to be
19c0: 20 61 6c 6c 6f 63 61 74 65 64 20 69 66 20 63 61   allocated if ca
19d0: 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c  che is not enabl
19e0: 65 64 20 2a 2f 0a 09 69 66 20 28 63 61 63 68 65  ed */..if (cache
19f0: 5f 73 69 7a 65 20 3d 3d 20 30 29 20 7b 0a 09 09  _size == 0) {...
1a00: 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09  return(0);..}...
1a10: 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 63 61 63 68  /* Allocate cach
1a20: 65 20 2a 2f 0a 09 66 69 6c 65 64 5f 66 69 6c 65  e */..filed_file
1a30: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a  info_fdcache_siz
1a40: 65 20 3d 20 63 61 63 68 65 5f 73 69 7a 65 3b 0a  e = cache_size;.
1a50: 09 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 20 3d 20 6d 61 6c 6c 6f 63  fdcache = malloc
1a70: 28 73 69 7a 65 6f 66 28 2a 66 69 6c 65 64 5f 66  (sizeof(*filed_f
1a80: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 29  ileinfo_fdcache)
1a90: 20 2a 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66   * filed_fileinf
1aa0: 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 29 3b  o_fdcache_size);
1ab0: 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65  ..if (filed_file
1ac0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d 3d 20  info_fdcache == 
1ad0: 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e  NULL) {...return
1ae0: 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69  (1);..}.../* Ini
1af0: 74 69 61 6c 69 7a 65 20 63 61 63 68 65 20 65 6e  tialize cache en
1b00: 74 72 69 65 73 20 2a 2f 0a 09 66 6f 72 20 28 69  tries */..for (i
1b10: 64 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 66 69  dx = 0; idx < fi
1b20: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
1b30: 61 63 68 65 5f 73 69 7a 65 3b 20 69 64 78 2b 2b  ache_size; idx++
1b40: 29 20 7b 0a 09 09 6d 75 74 65 78 5f 69 6e 69 74  ) {...mutex_init
1b50: 5f 72 65 74 20 3d 20 70 74 68 72 65 61 64 5f 6d  _ret = pthread_m
1b60: 75 74 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 64  utex_init(&filed
1b70: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68  _fileinfo_fdcach
1b80: 65 5b 69 64 78 5d 2e 6d 75 74 65 78 2c 20 4e 55  e[idx].mutex, NU
1b90: 4c 4c 29 3b 0a 09 09 69 66 20 28 6d 75 74 65 78  LL);...if (mutex
1ba0: 5f 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20  _init_ret != 0) 
1bb0: 7b 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a  {....return(1);.
1bc0: 09 09 7d 0a 0a 09 09 66 69 6c 65 64 5f 66 69 6c  ..}....filed_fil
1bd0: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64  einfo_fdcache[id
1be0: 78 5d 2e 70 61 74 68 5b 30 5d 20 3d 20 27 5c 30  x].path[0] = '\0
1bf0: 27 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69  ';...filed_filei
1c00: 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d  nfo_fdcache[idx]
1c10: 2e 66 64 20 3d 20 2d 31 3b 0a 09 09 66 69 6c 65  .fd = -1;...file
1c20: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63  d_fileinfo_fdcac
1c30: 68 65 5b 69 64 78 5d 2e 6c 61 73 74 6d 6f 64 20  he[idx].lastmod 
1c40: 3d 20 22 22 3b 0a 09 09 66 69 6c 65 64 5f 66 69  = "";...filed_fi
1c50: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69  leinfo_fdcache[i
1c60: 64 78 5d 2e 74 79 70 65 20 3d 20 22 22 3b 0a 09  dx].type = "";..
1c70: 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d  }...return(0);.}
1c80: 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ../* Initialize 
1c90: 70 72 6f 63 65 73 73 20 2a 2f 0a 73 74 61 74 69  process */.stati
1ca0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74  c int filed_init
1cb0: 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61  (unsigned int ca
1cc0: 63 68 65 5f 73 69 7a 65 29 20 7b 0a 09 73 74 61  che_size) {..sta
1cd0: 74 69 63 20 69 6e 74 20 63 61 6c 6c 65 64 20 3d  tic int called =
1ce0: 20 30 3b 0a 09 73 74 72 75 63 74 20 73 69 67 61   0;..struct siga
1cf0: 63 74 69 6f 6e 20 73 69 67 6e 61 6c 5f 68 61 6e  ction signal_han
1d00: 64 6c 65 72 5f 69 6e 66 6f 3b 0a 09 73 69 67 73  dler_info;..sigs
1d10: 65 74 5f 74 20 73 69 67 6e 61 6c 5f 68 61 6e 64  et_t signal_hand
1d20: 6c 65 72 5f 6d 61 73 6b 3b 0a 09 73 73 69 7a 65  ler_mask;..ssize
1d30: 5f 74 20 72 65 61 64 5f 72 65 74 20 3d 20 30 3b  _t read_ret = 0;
1d40: 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 72  ..unsigned int r
1d50: 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 3d 20 30 3b  andom_value = 0;
1d60: 0a 09 69 6e 74 20 63 61 63 68 65 5f 72 65 74 3b  ..int cache_ret;
1d70: 0a 09 69 6e 74 20 72 61 6e 64 6f 6d 5f 66 64 3b  ..int random_fd;
1d80: 0a 0a 09 69 66 20 28 63 61 6c 6c 65 64 29 20 7b  ...if (called) {
1d90: 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d  ...return(0);..}
1da0: 0a 0a 09 63 61 6c 6c 65 64 20 3d 20 31 3b 0a 0a  ...called = 1;..
1db0: 09 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c  ./* Attempt to l
1dc0: 6f 63 6b 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74  ock all memory t
1dd0: 6f 20 70 68 79 73 69 63 61 6c 20 52 41 4d 20 28  o physical RAM (
1de0: 62 75 74 20 64 6f 6e 27 74 20 63 61 72 65 20 69  but don't care i
1df0: 66 20 77 65 20 63 61 6e 27 74 29 20 2a 2f 0a 09  f we can't) */..
1e00: 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 4c 5f 43 55 52  mlockall(MCL_CUR
1e10: 52 45 4e 54 20 7c 20 4d 43 4c 5f 46 55 54 55 52  RENT | MCL_FUTUR
1e20: 45 29 3b 0a 0a 09 2f 2a 20 45 73 74 61 62 6c 69  E);.../* Establi
1e30: 73 68 20 73 69 67 6e 61 6c 20 68 61 6e 64 6c 65  sh signal handle
1e40: 72 73 20 2a 2f 0a 09 2f 2a 20 53 49 47 50 49 50  rs */../* SIGPIP
1e50: 45 20 73 68 6f 75 6c 64 20 69 6e 74 65 72 72 75  E should interru
1e60: 70 74 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20  pt system calls 
1e70: 2a 2f 0a 09 73 69 67 66 69 6c 6c 73 65 74 28 26  */..sigfillset(&
1e80: 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 6d  signal_handler_m
1e90: 61 73 6b 29 3b 0a 09 73 69 67 6e 61 6c 5f 68 61  ask);..signal_ha
1ea0: 6e 64 6c 65 72 5f 69 6e 66 6f 2e 73 61 5f 68 61  ndler_info.sa_ha
1eb0: 6e 64 6c 65 72 20 3d 20 66 69 6c 65 64 5f 73 69  ndler = filed_si
1ec0: 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 3b 0a 09 73  gnal_handler;..s
1ed0: 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e  ignal_handler_in
1ee0: 66 6f 2e 73 61 5f 6d 61 73 6b 20 3d 20 73 69 67  fo.sa_mask = sig
1ef0: 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 6d 61 73 6b  nal_handler_mask
1f00: 3b 0a 09 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65  ;..signal_handle
1f10: 72 5f 69 6e 66 6f 2e 73 61 5f 66 6c 61 67 73 20  r_info.sa_flags 
1f20: 3d 20 53 41 5f 52 45 53 54 41 52 54 3b 0a 09 73  = SA_RESTART;..s
1f30: 69 67 61 63 74 69 6f 6e 28 53 49 47 50 49 50 45  igaction(SIGPIPE
1f40: 2c 20 26 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65  , &signal_handle
1f50: 72 5f 69 6e 66 6f 2c 20 4e 55 4c 4c 29 3b 0a 0a  r_info, NULL);..
1f60: 09 2f 2a 20 48 61 6e 64 6c 65 20 53 49 47 48 55  ./* Handle SIGHU
1f70: 50 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c  P to release all
1f80: 20 63 61 63 68 65 73 20 2a 2f 0a 09 73 69 67 66   caches */..sigf
1f90: 69 6c 6c 73 65 74 28 26 73 69 67 6e 61 6c 5f 68  illset(&signal_h
1fa0: 61 6e 64 6c 65 72 5f 6d 61 73 6b 29 3b 0a 09 73  andler_mask);..s
1fb0: 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e  ignal_handler_in
1fc0: 66 6f 2e 73 61 5f 68 61 6e 64 6c 65 72 20 3d 20  fo.sa_handler = 
1fd0: 66 69 6c 65 64 5f 73 69 67 6e 61 6c 5f 68 61 6e  filed_signal_han
1fe0: 64 6c 65 72 3b 0a 09 73 69 67 6e 61 6c 5f 68 61  dler;..signal_ha
1ff0: 6e 64 6c 65 72 5f 69 6e 66 6f 2e 73 61 5f 6d 61  ndler_info.sa_ma
2000: 73 6b 20 3d 20 73 69 67 6e 61 6c 5f 68 61 6e 64  sk = signal_hand
2010: 6c 65 72 5f 6d 61 73 6b 3b 0a 09 73 69 67 6e 61  ler_mask;..signa
2020: 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 6f 2e 73  l_handler_info.s
2030: 61 5f 66 6c 61 67 73 20 3d 20 30 3b 0a 09 73 69  a_flags = 0;..si
2040: 67 61 63 74 69 6f 6e 28 53 49 47 48 55 50 2c 20  gaction(SIGHUP, 
2050: 26 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f  &signal_handler_
2060: 69 6e 66 6f 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 2f  info, NULL);.../
2070: 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 63 61 63  * Initialize cac
2080: 68 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  he structure */.
2090: 09 63 61 63 68 65 5f 72 65 74 20 3d 20 66 69 6c  .cache_ret = fil
20a0: 65 64 5f 69 6e 69 74 5f 63 61 63 68 65 28 63 61  ed_init_cache(ca
20b0: 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28  che_size);..if (
20c0: 63 61 63 68 65 5f 72 65 74 20 21 3d 20 30 29 20  cache_ret != 0) 
20d0: 7b 0a 09 09 72 65 74 75 72 6e 28 63 61 63 68 65  {...return(cache
20e0: 5f 72 65 74 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49  _ret);..}.../* I
20f0: 6e 69 74 69 61 6c 69 7a 65 20 72 61 6e 64 6f 6d  nitialize random
2100: 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f   number generato
2110: 72 20 2a 2f 0a 09 72 61 6e 64 6f 6d 5f 66 64 20  r */..random_fd 
2120: 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61  = open("/dev/ura
2130: 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29  ndom", O_RDONLY)
2140: 3b 0a 09 69 66 20 28 72 61 6e 64 6f 6d 5f 66 64  ;..if (random_fd
2150: 20 3e 3d 20 30 29 20 7b 0a 09 09 72 65 61 64 5f   >= 0) {...read_
2160: 72 65 74 20 3d 20 72 65 61 64 28 72 61 6e 64 6f  ret = read(rando
2170: 6d 5f 66 64 2c 20 26 72 61 6e 64 6f 6d 5f 76 61  m_fd, &random_va
2180: 6c 75 65 2c 20 73 69 7a 65 6f 66 28 72 61 6e 64  lue, sizeof(rand
2190: 6f 6d 5f 76 61 6c 75 65 29 29 3b 0a 0a 09 09 63  om_value));....c
21a0: 6c 6f 73 65 28 72 61 6e 64 6f 6d 5f 66 64 29 3b  lose(random_fd);
21b0: 0a 09 7d 0a 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c  ..}...random_val
21c0: 75 65 20 5e 3d 20 67 65 74 70 69 64 28 29 3b 0a  ue ^= getpid();.
21d0: 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d  .random_value ^=
21e0: 20 67 65 74 75 69 64 28 29 3b 0a 09 72 61 6e 64   getuid();..rand
21f0: 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 74 69 6d 65  om_value ^= time
2200: 28 4e 55 4c 4c 29 3b 0a 0a 09 73 72 61 6e 64 6f  (NULL);...srando
2210: 6d 28 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 29 3b  m(random_value);
2220: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 0a 09  ...return(0);...
2230: 2f 2a 20 4e 4f 54 52 45 41 43 48 3a 20 52 65 61  /* NOTREACH: Rea
2240: 64 20 6d 61 79 20 66 61 69 6c 20 6f 72 20 73 75  d may fail or su
2250: 63 63 65 65 64 2c 20 77 65 20 64 6f 6e 27 74 20  cceed, we don't 
2260: 61 63 74 75 61 6c 6c 79 20 63 61 72 65 20 2a 2f  actually care */
2270: 0a 09 72 65 61 64 5f 72 65 74 20 3d 20 72 65 61  ..read_ret = rea
2280: 64 5f 72 65 74 3b 0a 7d 0a 0a 2f 2a 20 4c 69 73  d_ret;.}../* Lis
2290: 74 65 6e 20 6f 6e 20 61 20 70 61 72 74 69 63 75  ten on a particu
22a0: 6c 61 72 20 61 64 64 72 65 73 73 2f 70 6f 72 74  lar address/port
22b0: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
22c0: 69 6c 65 64 5f 6c 69 73 74 65 6e 28 63 6f 6e 73  iled_listen(cons
22d0: 74 20 63 68 61 72 20 2a 61 64 64 72 65 73 73 2c  t char *address,
22e0: 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 70 6f   unsigned int po
22f0: 72 74 29 20 7b 0a 09 73 74 72 75 63 74 20 73 6f  rt) {..struct so
2300: 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 64 72 5f  ckaddr_in6 addr_
2310: 76 36 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b  v6;..struct sock
2320: 61 64 64 72 5f 69 6e 20 61 64 64 72 5f 76 34 3b  addr_in addr_v4;
2330: 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64  ..struct sockadd
2340: 72 20 2a 61 64 64 72 3b 0a 09 73 6f 63 6b 6c 65  r *addr;..sockle
2350: 6e 5f 74 20 61 64 64 72 5f 6c 65 6e 3b 0a 09 69  n_t addr_len;..i
2360: 6e 74 20 70 74 6f 6e 5f 72 65 74 2c 20 62 69 6e  nt pton_ret, bin
2370: 64 5f 72 65 74 2c 20 6c 69 73 74 65 6e 5f 72 65  d_ret, listen_re
2380: 74 3b 0a 09 69 6e 74 20 66 61 6d 69 6c 79 3b 0a  t;..int family;.
2390: 09 69 6e 74 20 66 64 3b 0a 0a 09 66 61 6d 69 6c  .int fd;...famil
23a0: 79 20 3d 20 41 46 5f 49 4e 45 54 36 3b 0a 09 70  y = AF_INET6;..p
23b0: 74 6f 6e 5f 72 65 74 20 3d 20 69 6e 65 74 5f 70  ton_ret = inet_p
23c0: 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 61 64 64 72  ton(family, addr
23d0: 65 73 73 2c 20 26 61 64 64 72 5f 76 36 2e 73 69  ess, &addr_v6.si
23e0: 6e 36 5f 61 64 64 72 2e 73 36 5f 61 64 64 72 29  n6_addr.s6_addr)
23f0: 3b 0a 09 69 66 20 28 70 74 6f 6e 5f 72 65 74 20  ;..if (pton_ret 
2400: 21 3d 20 31 29 20 7b 0a 09 09 66 61 6d 69 6c 79  != 1) {...family
2410: 20 3d 20 41 46 5f 49 4e 45 54 3b 0a 09 09 70 74   = AF_INET;...pt
2420: 6f 6e 5f 72 65 74 20 3d 20 69 6e 65 74 5f 70 74  on_ret = inet_pt
2430: 6f 6e 28 66 61 6d 69 6c 79 2c 20 61 64 64 72 65  on(family, addre
2440: 73 73 2c 20 26 61 64 64 72 5f 76 34 2e 73 69 6e  ss, &addr_v4.sin
2450: 5f 61 64 64 72 2e 73 5f 61 64 64 72 29 3b 0a 09  _addr.s_addr);..
2460: 09 69 66 20 28 70 74 6f 6e 5f 72 65 74 20 21 3d  .if (pton_ret !=
2470: 20 31 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28   1) {....return(
2480: 2d 31 29 3b 0a 09 09 7d 0a 0a 09 09 61 64 64 72  -1);...}....addr
2490: 5f 76 34 2e 73 69 6e 5f 66 61 6d 69 6c 79 20 3d  _v4.sin_family =
24a0: 20 66 61 6d 69 6c 79 3b 0a 09 09 61 64 64 72 5f   family;...addr_
24b0: 76 34 2e 73 69 6e 5f 70 6f 72 74 20 3d 20 68 74  v4.sin_port = ht
24c0: 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a 09 09 61 64  ons(port);....ad
24d0: 64 72 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63  dr = (struct soc
24e0: 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 5f 76  kaddr *) &addr_v
24f0: 34 3b 0a 09 09 61 64 64 72 5f 6c 65 6e 20 3d 20  4;...addr_len = 
2500: 73 69 7a 65 6f 66 28 61 64 64 72 5f 76 34 29 3b  sizeof(addr_v4);
2510: 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 61 64 64  ..} else {...add
2520: 72 5f 76 36 2e 73 69 6e 36 5f 66 61 6d 69 6c 79  r_v6.sin6_family
2530: 20 3d 20 41 46 5f 49 4e 45 54 36 3b 0a 09 09 61   = AF_INET6;...a
2540: 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 6c 6f 77  ddr_v6.sin6_flow
2550: 69 6e 66 6f 20 3d 20 30 3b 0a 09 09 61 64 64 72  info = 0;...addr
2560: 5f 76 36 2e 73 69 6e 36 5f 73 63 6f 70 65 5f 69  _v6.sin6_scope_i
2570: 64 20 3d 20 30 3b 0a 09 09 61 64 64 72 5f 76 36  d = 0;...addr_v6
2580: 2e 73 69 6e 36 5f 70 6f 72 74 20 3d 20 68 74 6f  .sin6_port = hto
2590: 6e 73 28 70 6f 72 74 29 3b 0a 0a 09 09 61 64 64  ns(port);....add
25a0: 72 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b  r = (struct sock
25b0: 61 64 64 72 20 2a 29 20 26 61 64 64 72 5f 76 36  addr *) &addr_v6
25c0: 3b 0a 09 09 61 64 64 72 5f 6c 65 6e 20 3d 20 73  ;...addr_len = s
25d0: 69 7a 65 6f 66 28 61 64 64 72 5f 76 36 29 3b 0a  izeof(addr_v6);.
25e0: 09 7d 0a 0a 09 66 64 20 3d 20 73 6f 63 6b 65 74  .}...fd = socket
25f0: 28 66 61 6d 69 6c 79 2c 20 53 4f 43 4b 5f 53 54  (family, SOCK_ST
2600: 52 45 41 4d 2c 20 30 29 3b 0a 09 69 66 20 28 66  REAM, 0);..if (f
2610: 64 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72  d < 0) {...retur
2620: 6e 28 66 64 29 3b 0a 09 7d 0a 0a 09 62 69 6e 64  n(fd);..}...bind
2630: 5f 72 65 74 20 3d 20 62 69 6e 64 28 66 64 2c 20  _ret = bind(fd, 
2640: 61 64 64 72 2c 20 61 64 64 72 5f 6c 65 6e 29 3b  addr, addr_len);
2650: 0a 09 69 66 20 28 62 69 6e 64 5f 72 65 74 20 3c  ..if (bind_ret <
2660: 20 30 29 20 7b 0a 09 09 63 6c 6f 73 65 28 66 64   0) {...close(fd
2670: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 2d 31 29  );....return(-1)
2680: 3b 0a 09 7d 0a 0a 09 6c 69 73 74 65 6e 5f 72 65  ;..}...listen_re
2690: 74 20 3d 20 6c 69 73 74 65 6e 28 66 64 2c 20 31  t = listen(fd, 1
26a0: 32 38 29 3b 0a 09 69 66 20 28 6c 69 73 74 65 6e  28);..if (listen
26b0: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 63  _ret != 0) {...c
26c0: 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74  lose(fd);....ret
26d0: 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 72 65  urn(-1);..}...re
26e0: 74 75 72 6e 28 66 64 29 3b 0a 7d 0a 0a 2f 2a 20  turn(fd);.}../* 
26f0: 4c 6f 67 20 61 20 6d 65 73 73 61 67 65 20 2a 2f  Log a message */
2700: 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f 44 4f  .#ifdef FILED_DO
2710: 4e 54 5f 4c 4f 47 0a 23 20 20 64 65 66 69 6e 65  NT_LOG.#  define
2720: 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74   filed_logging_t
2730: 68 72 65 61 64 5f 69 6e 69 74 28 78 29 20 30 0a  hread_init(x) 0.
2740: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f  #  define filed_
2750: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c  log_msg_debug(x,
2760: 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 64 65   ...) /**/.#  de
2770: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d  fine filed_log_m
2780: 73 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a  sg(x, ...) /**/.
2790: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f  #  define filed_
27a0: 6c 6f 67 5f 65 6e 74 72 79 28 78 29 20 2f 2a 2a  log_entry(x) /**
27b0: 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65  /.#  define file
27c0: 64 5f 6c 6f 67 5f 69 70 28 78 2c 20 2e 2e 2e 29  d_log_ip(x, ...)
27d0: 20 4e 55 4c 4c 0a 23 20 20 64 65 66 69 6e 65 20   NULL.#  define 
27e0: 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 78 29  filed_log_new(x)
27f0: 20 26 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f   &local_dummy_lo
2800: 67 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65  g.#  define file
2810: 64 5f 6c 6f 67 5f 66 72 65 65 28 78 29 20 2f 2a  d_log_free(x) /*
2820: 2a 2f 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 6c 6f  */../* Return lo
2830: 67 67 69 6e 67 20 68 61 6e 64 6c 65 20 2a 2f 0a  gging handle */.
2840: 73 74 61 74 69 63 20 46 49 4c 45 20 2a 66 69 6c  static FILE *fil
2850: 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 63 6f 6e 73  ed_log_open(cons
2860: 74 20 63 68 61 72 20 2a 66 69 6c 65 29 20 7b 0a  t char *file) {.
2870: 09 72 65 74 75 72 6e 28 73 74 64 6f 75 74 29 3b  .return(stdout);
2880: 0a 09 66 69 6c 65 20 3d 20 66 69 6c 65 3b 0a 7d  ..file = file;.}
2890: 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  .#else.#  define
28a0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 66 72 65 65 28   filed_log_free(
28b0: 78 29 20 66 72 65 65 28 78 29 0a 23 20 20 69 66  x) free(x).#  if
28c0: 64 65 66 20 46 49 4c 45 44 5f 44 45 42 55 47 0a  def FILED_DEBUG.
28d0: 23 20 20 20 20 64 65 66 69 6e 65 20 66 69 6c 65  #    define file
28e0: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28  d_log_msg_debug(
28f0: 78 2c 20 2e 2e 2e 29 20 7b 20 66 70 72 69 6e 74  x, ...) { fprint
2900: 66 28 73 74 64 65 72 72 2c 20 78 2c 20 5f 5f 56  f(stderr, x, __V
2910: 41 5f 41 52 47 53 5f 5f 29 3b 20 66 70 72 69 6e  A_ARGS__); fprin
2920: 74 66 28 73 74 64 65 72 72 2c 20 22 5c 6e 22 29  tf(stderr, "\n")
2930: 3b 20 66 66 6c 75 73 68 28 73 74 64 65 72 72 29  ; fflush(stderr)
2940: 3b 20 7d 0a 23 20 20 65 6c 73 65 0a 23 20 20 20  ; }.#  else.#   
2950: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f   define filed_lo
2960: 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c 20 2e  g_msg_debug(x, .
2970: 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 65 6e 64 69  ..) /**/.#  endi
2980: 66 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  f../* Initialize
2990: 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64 20   logging thread 
29a0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
29b0: 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68  filed_logging_th
29c0: 72 65 61 64 28 76 6f 69 64 20 2a 61 72 67 5f 70  read(void *arg_p
29d0: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65  ) {..struct file
29e0: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64  d_logging_thread
29f0: 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 73 74 72  _args *arg;..str
2a00: 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e  uct filed_log_en
2a10: 74 72 79 20 2a 63 75 72 72 2c 20 2a 70 72 65 76  try *curr, *prev
2a20: 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 6d  ;..const char *m
2a30: 65 74 68 6f 64 3b 0a 09 74 69 6d 65 5f 74 20 6e  ethod;..time_t n
2a40: 6f 77 3b 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a  ow;..FILE *fp;..
2a50: 09 61 72 67 20 3d 20 61 72 67 5f 70 3b 0a 0a 09  .arg = arg_p;...
2a60: 66 70 20 3d 20 61 72 67 2d 3e 66 70 3b 0a 0a 09  fp = arg->fp;...
2a70: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 70 74  while (1) {...pt
2a80: 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b  hread_mutex_lock
2a90: 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  (&filed_log_msg_
2aa0: 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 09 09 70  list_mutex);...p
2ab0: 74 68 72 65 61 64 5f 63 6f 6e 64 5f 77 61 69 74  thread_cond_wait
2ac0: 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  (&filed_log_msg_
2ad0: 6c 69 73 74 5f 72 65 61 64 79 2c 20 26 66 69 6c  list_ready, &fil
2ae0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f  ed_log_msg_list_
2af0: 6d 75 74 65 78 29 3b 0a 0a 09 09 63 75 72 72 20  mutex);....curr 
2b00: 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  = filed_log_msg_
2b10: 6c 69 73 74 3b 0a 09 09 66 69 6c 65 64 5f 6c 6f  list;...filed_lo
2b20: 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c  g_msg_list = NUL
2b30: 4c 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d 75  L;....pthread_mu
2b40: 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65  tex_unlock(&file
2b50: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d  d_log_msg_list_m
2b60: 75 74 65 78 29 3b 0a 0a 09 09 6e 6f 77 20 3d 20  utex);....now = 
2b70: 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 09 70  time(NULL);....p
2b80: 72 65 76 20 3d 20 4e 55 4c 4c 3b 0a 09 09 66 6f  rev = NULL;...fo
2b90: 72 20 28 3b 20 63 75 72 72 3b 20 63 75 72 72 20  r (; curr; curr 
2ba0: 3d 20 63 75 72 72 2d 3e 5f 6e 65 78 74 29 20 7b  = curr->_next) {
2bb0: 0a 09 09 09 63 75 72 72 2d 3e 5f 70 72 65 76 20  ....curr->_prev 
2bc0: 3d 20 70 72 65 76 3b 0a 0a 09 09 09 70 72 65 76  = prev;.....prev
2bd0: 20 3d 20 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09   = curr;...}....
2be0: 63 75 72 72 20 3d 20 70 72 65 76 3b 0a 09 09 77  curr = prev;...w
2bf0: 68 69 6c 65 20 28 63 75 72 72 29 20 7b 0a 09 09  hile (curr) {...
2c00: 09 73 77 69 74 63 68 20 28 63 75 72 72 2d 3e 74  .switch (curr->t
2c10: 79 70 65 29 20 7b 0a 09 09 09 09 63 61 73 65 20  ype) {.....case 
2c20: 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d  FILED_LOG_TYPE_M
2c30: 45 53 53 41 47 45 3a 0a 09 09 09 09 09 66 70 72  ESSAGE:......fpr
2c40: 69 6e 74 66 28 66 70 2c 20 22 25 73 22 2c 20 63  intf(fp, "%s", c
2c50: 75 72 72 2d 3e 62 75 66 66 65 72 29 3b 0a 0a 09  urr->buffer);...
2c60: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 63  ....break;.....c
2c70: 61 73 65 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59  ase FILED_LOG_TY
2c80: 50 45 5f 54 52 41 4e 53 46 45 52 3a 0a 09 09 09  PE_TRANSFER:....
2c90: 09 09 73 77 69 74 63 68 20 28 63 75 72 72 2d 3e  ..switch (curr->
2ca0: 6d 65 74 68 6f 64 29 20 7b 0a 09 09 09 09 09 09  method) {.......
2cb0: 63 61 73 65 20 46 49 4c 45 44 5f 52 45 51 55 45  case FILED_REQUE
2cc0: 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 3a 0a 09  ST_METHOD_GET:..
2cd0: 09 09 09 09 09 09 6d 65 74 68 6f 64 3d 22 47 45  ......method="GE
2ce0: 54 22 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b  T";........break
2cf0: 3b 0a 09 09 09 09 09 09 63 61 73 65 20 46 49 4c  ;.......case FIL
2d00: 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f  ED_REQUEST_METHO
2d10: 44 5f 48 45 41 44 3a 0a 09 09 09 09 09 09 09 6d  D_HEAD:........m
2d20: 65 74 68 6f 64 3d 22 48 45 41 44 22 3b 0a 09 09  ethod="HEAD";...
2d30: 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09  .....break;.....
2d40: 09 09 64 65 66 61 75 6c 74 3a 0a 09 09 09 09 09  ..default:......
2d50: 09 09 6d 65 74 68 6f 64 3d 22 3c 75 6e 6b 6e 6f  ..method="<unkno
2d60: 77 6e 3e 22 3b 0a 09 09 09 09 09 09 09 62 72 65  wn>";........bre
2d70: 61 6b 3b 0a 09 09 09 09 09 7d 0a 0a 09 09 09 09  ak;......}......
2d80: 09 69 66 20 28 63 75 72 72 2d 3e 65 6e 64 74 69  .if (curr->endti
2d90: 6d 65 20 3d 3d 20 28 28 74 69 6d 65 5f 74 29 20  me == ((time_t) 
2da0: 2d 31 29 29 20 7b 0a 09 09 09 09 09 09 63 75 72  -1)) {.......cur
2db0: 72 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 6e 6f 77  r->endtime = now
2dc0: 3b 0a 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 66  ;......}.......f
2dd0: 70 72 69 6e 74 66 28 66 70 2c 20 22 54 52 41 4e  printf(fp, "TRAN
2de0: 53 46 45 52 20 4d 45 54 48 4f 44 3d 25 73 20 50  SFER METHOD=%s P
2df0: 41 54 48 3d 25 73 20 53 52 43 3d 25 73 3a 25 69  ATH=%s SRC=%s:%i
2e00: 20 54 49 4d 45 2e 53 54 41 52 54 3d 25 6c 6c 75   TIME.START=%llu
2e10: 20 54 49 4d 45 2e 45 4e 44 3d 25 6c 6c 75 20 43   TIME.END=%llu C
2e20: 4f 44 45 2e 56 41 4c 55 45 3d 25 75 20 43 4f 44  ODE.VALUE=%u COD
2e30: 45 2e 52 45 41 53 4f 4e 3d 25 73 20 52 45 51 55  E.REASON=%s REQU
2e40: 45 53 54 2e 4f 46 46 53 45 54 3d 25 6c 6c 75 20  EST.OFFSET=%llu 
2e50: 52 45 51 55 45 53 54 2e 4c 45 4e 47 54 48 3d 25  REQUEST.LENGTH=%
2e60: 6c 6c 75 20 46 49 4c 45 2e 4c 45 4e 47 54 48 3d  llu FILE.LENGTH=
2e70: 25 6c 6c 75 20 54 52 41 4e 53 46 45 52 2e 4c 45  %llu TRANSFER.LE
2e80: 4e 47 54 48 3d 25 6c 6c 75 22 2c 0a 09 09 09 09  NGTH=%llu",.....
2e90: 09 09 6d 65 74 68 6f 64 2c 0a 09 09 09 09 09 09  ..method,.......
2ea0: 63 75 72 72 2d 3e 62 75 66 66 65 72 2c 0a 09 09  curr->buffer,...
2eb0: 09 09 09 09 63 75 72 72 2d 3e 69 70 2c 20 63 75  ....curr->ip, cu
2ec0: 72 72 2d 3e 70 6f 72 74 2c 0a 09 09 09 09 09 09  rr->port,.......
2ed0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
2ee0: 6f 6e 67 29 20 63 75 72 72 2d 3e 73 74 61 72 74  ong) curr->start
2ef0: 74 69 6d 65 2c 0a 09 09 09 09 09 09 28 75 6e 73  time,.......(uns
2f00: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
2f10: 20 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 2c 0a   curr->endtime,.
2f20: 09 09 09 09 09 09 63 75 72 72 2d 3e 68 74 74 70  ......curr->http
2f30: 5f 63 6f 64 65 2c 20 63 75 72 72 2d 3e 72 65 61  _code, curr->rea
2f40: 73 6f 6e 2c 0a 09 09 09 09 09 09 28 75 6e 73 69  son,.......(unsi
2f50: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
2f60: 63 75 72 72 2d 3e 72 65 71 5f 6f 66 66 73 65 74  curr->req_offset
2f70: 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65  ,.......(unsigne
2f80: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72  d long long) cur
2f90: 72 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 2c 0a 09  r->req_length,..
2fa0: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c  .....(unsigned l
2fb0: 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e  ong long) curr->
2fc0: 66 69 6c 65 5f 6c 65 6e 67 74 68 2c 0a 09 09 09  file_length,....
2fd0: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
2fe0: 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 73 65  g long) curr->se
2ff0: 6e 74 5f 6c 65 6e 67 74 68 0a 09 09 09 09 09 29  nt_length......)
3000: 3b 0a 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09  ;.......break;..
3010: 09 09 7d 0a 09 09 09 66 70 72 69 6e 74 66 28 66  ..}....fprintf(f
3020: 70 2c 20 22 20 54 48 52 45 41 44 3d 25 6c 6c 75  p, " THREAD=%llu
3030: 20 54 49 4d 45 3d 25 6c 6c 75 5c 6e 22 2c 0a 09   TIME=%llu\n",..
3040: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
3050: 67 20 6c 6f 6e 67 29 20 28 28 69 6e 74 70 74 72  g long) ((intptr
3060: 5f 74 29 20 63 75 72 72 2d 3e 74 68 72 65 61 64  _t) curr->thread
3070: 29 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64  ),.....(unsigned
3080: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 6e 6f 77 0a   long long) now.
3090: 09 09 09 29 3b 0a 09 09 09 66 66 6c 75 73 68 28  ...);....fflush(
30a0: 66 70 29 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20  fp);.....prev = 
30b0: 63 75 72 72 3b 0a 09 09 09 63 75 72 72 20 3d 20  curr;....curr = 
30c0: 63 75 72 72 2d 3e 5f 70 72 65 76 3b 0a 0a 09 09  curr->_prev;....
30d0: 09 66 72 65 65 28 70 72 65 76 29 3b 0a 09 09 7d  .free(prev);...}
30e0: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c  ..}...return(NUL
30f0: 4c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  L);.}..static vo
3100: 69 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  id filed_log_ent
3110: 72 79 28 73 74 72 75 63 74 20 66 69 6c 65 64 5f  ry(struct filed_
3120: 6c 6f 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79  log_entry *entry
3130: 29 20 7b 0a 09 65 6e 74 72 79 2d 3e 74 68 72 65  ) {..entry->thre
3140: 61 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  ad = pthread_sel
3150: 66 28 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d  f();...pthread_m
3160: 75 74 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64  utex_lock(&filed
3170: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75  _log_msg_list_mu
3180: 74 65 78 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 5f  tex);...entry->_
3190: 6e 65 78 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67  next = filed_log
31a0: 5f 6d 73 67 5f 6c 69 73 74 3b 0a 09 66 69 6c 65  _msg_list;..file
31b0: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d  d_log_msg_list =
31c0: 20 65 6e 74 72 79 3b 0a 0a 09 70 74 68 72 65 61   entry;...pthrea
31d0: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26  d_mutex_unlock(&
31e0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69  filed_log_msg_li
31f0: 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 70 74 68  st_mutex);...pth
3200: 72 65 61 64 5f 63 6f 6e 64 5f 73 69 67 6e 61 6c  read_cond_signal
3210: 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  (&filed_log_msg_
3220: 6c 69 73 74 5f 72 65 61 64 79 29 3b 0a 0a 09 72  list_ready);...r
3230: 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63  eturn;.}..static
3240: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f   struct filed_lo
3250: 67 5f 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c  g_entry *filed_l
3260: 6f 67 5f 6e 65 77 28 69 6e 74 20 69 6e 69 74 69  og_new(int initi
3270: 61 6c 69 7a 65 29 20 7b 0a 09 73 74 72 75 63 74  alize) {..struct
3280: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79   filed_log_entry
3290: 20 2a 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76   *retval;...retv
32a0: 61 6c 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65  al = malloc(size
32b0: 6f 66 28 2a 72 65 74 76 61 6c 29 29 3b 0a 0a 09  of(*retval));...
32c0: 69 66 20 28 69 6e 69 74 69 61 6c 69 7a 65 29 20  if (initialize) 
32d0: 7b 0a 09 09 72 65 74 76 61 6c 2d 3e 62 75 66 66  {...retval->buff
32e0: 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09  er[0] = '\0';...
32f0: 72 65 74 76 61 6c 2d 3e 68 74 74 70 5f 63 6f 64  retval->http_cod
3300: 65 20 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c  e = -1;...retval
3310: 2d 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 30 3b  ->starttime = 0;
3320: 0a 09 09 72 65 74 76 61 6c 2d 3e 65 6e 64 74 69  ...retval->endti
3330: 6d 65 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c  me = 0;...retval
3340: 2d 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 30  ->req_offset = 0
3350: 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f  ;...retval->req_
3360: 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65  length = 0;...re
3370: 74 76 61 6c 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74  tval->sent_lengt
3380: 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d  h = 0;...retval-
3390: 3e 66 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 30  >file_length = 0
33a0: 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 69 70 5b 30  ;...retval->ip[0
33b0: 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 72 65 74 76  ] = '\0';...retv
33c0: 61 6c 2d 3e 70 6f 72 74 20 3d 20 2d 31 3b 0a 09  al->port = -1;..
33d0: 09 72 65 74 76 61 6c 2d 3e 6d 65 74 68 6f 64 20  .retval->method 
33e0: 3d 20 2d 31 3b 0a 09 7d 0a 0a 09 72 65 74 75 72  = -1;..}...retur
33f0: 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74  n(retval);.}..st
3400: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f  atic void filed_
3410: 6c 6f 67 5f 6d 73 67 28 63 6f 6e 73 74 20 63 68  log_msg(const ch
3420: 61 72 20 2a 66 6d 74 2c 20 2e 2e 2e 29 20 7b 0a  ar *fmt, ...) {.
3430: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f  .struct filed_lo
3440: 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 3b 0a  g_entry *entry;.
3450: 09 76 61 5f 6c 69 73 74 20 61 72 67 73 3b 0a 0a  .va_list args;..
3460: 09 65 6e 74 72 79 20 3d 20 66 69 6c 65 64 5f 6c  .entry = filed_l
3470: 6f 67 5f 6e 65 77 28 30 29 3b 0a 0a 09 76 61 5f  og_new(0);...va_
3480: 73 74 61 72 74 28 61 72 67 73 2c 20 66 6d 74 29  start(args, fmt)
3490: 3b 0a 0a 09 76 73 6e 70 72 69 6e 74 66 28 65 6e  ;...vsnprintf(en
34a0: 74 72 79 2d 3e 62 75 66 66 65 72 2c 20 73 69 7a  try->buffer, siz
34b0: 65 6f 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 65  eof(entry->buffe
34c0: 72 29 2c 20 66 6d 74 2c 20 61 72 67 73 29 3b 0a  r), fmt, args);.
34d0: 0a 09 76 61 5f 65 6e 64 28 61 72 67 73 29 3b 0a  ..va_end(args);.
34e0: 0a 09 65 6e 74 72 79 2d 3e 74 79 70 65 20 3d 20  ..entry->type = 
34f0: 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d  FILED_LOG_TYPE_M
3500: 45 53 53 41 47 45 3b 0a 0a 09 66 69 6c 65 64 5f  ESSAGE;...filed_
3510: 6c 6f 67 5f 65 6e 74 72 79 28 65 6e 74 72 79 29  log_entry(entry)
3520: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73  ;...return;.}..s
3530: 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
3540: 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 73   *filed_log_ip(s
3550: 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a  truct sockaddr *
3560: 61 64 64 72 2c 20 63 68 61 72 20 2a 62 75 66 66  addr, char *buff
3570: 65 72 2c 20 73 69 7a 65 5f 74 20 62 75 66 66 65  er, size_t buffe
3580: 72 6c 65 6e 29 20 7b 0a 09 73 74 72 75 63 74 20  rlen) {..struct 
3590: 73 6f 63 6b 61 64 64 72 5f 69 6e 20 2a 61 64 64  sockaddr_in *add
35a0: 72 5f 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f  r_v4;..struct so
35b0: 63 6b 61 64 64 72 5f 69 6e 36 20 2a 61 64 64 72  ckaddr_in6 *addr
35c0: 5f 76 36 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72  _v6;..const char
35d0: 20 2a 72 65 74 76 61 6c 20 3d 20 4e 55 4c 4c 3b   *retval = NULL;
35e0: 0a 0a 09 61 64 64 72 5f 76 36 20 3d 20 28 73 74  ...addr_v6 = (st
35f0: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e  ruct sockaddr_in
3600: 36 20 2a 29 20 61 64 64 72 3b 0a 0a 09 73 77 69  6 *) addr;...swi
3610: 74 63 68 20 28 61 64 64 72 5f 76 36 2d 3e 73 69  tch (addr_v6->si
3620: 6e 36 5f 66 61 6d 69 6c 79 29 20 7b 0a 09 09 63  n6_family) {...c
3630: 61 73 65 20 41 46 5f 49 4e 45 54 3a 0a 09 09 09  ase AF_INET:....
3640: 61 64 64 72 5f 76 34 20 3d 20 28 73 74 72 75 63  addr_v4 = (struc
3650: 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 2a 29  t sockaddr_in *)
3660: 20 61 64 64 72 3b 0a 09 09 09 72 65 74 76 61 6c   addr;....retval
3670: 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 41 46 5f   = inet_ntop(AF_
3680: 49 4e 45 54 2c 20 26 61 64 64 72 5f 76 34 2d 3e  INET, &addr_v4->
3690: 73 69 6e 5f 61 64 64 72 2c 20 62 75 66 66 65 72  sin_addr, buffer
36a0: 2c 20 62 75 66 66 65 72 6c 65 6e 29 3b 0a 09 09  , bufferlen);...
36b0: 09 62 72 65 61 6b 3b 0a 09 09 63 61 73 65 20 41  .break;...case A
36c0: 46 5f 49 4e 45 54 36 3a 0a 09 09 09 72 65 74 76  F_INET6:....retv
36d0: 61 6c 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 41  al = inet_ntop(A
36e0: 46 5f 49 4e 45 54 36 2c 20 26 61 64 64 72 5f 76  F_INET6, &addr_v
36f0: 36 2d 3e 73 69 6e 36 5f 61 64 64 72 2c 20 62 75  6->sin6_addr, bu
3700: 66 66 65 72 2c 20 62 75 66 66 65 72 6c 65 6e 29  ffer, bufferlen)
3710: 3b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a 0a  ;....break;..}..
3720: 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b  .return(retval);
3730: 0a 7d 0a 0a 73 74 61 74 69 63 20 46 49 4c 45 20  .}..static FILE 
3740: 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28  *filed_log_open(
3750: 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65  const char *file
3760: 29 20 7b 0a 09 46 49 4c 45 20 2a 72 65 74 76 61  ) {..FILE *retva
3770: 6c 3b 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 28  l;...if (strcmp(
3780: 66 69 6c 65 2c 20 22 2d 22 29 20 3d 3d 20 30 29  file, "-") == 0)
3790: 20 7b 0a 09 09 72 65 74 76 61 6c 20 3d 20 73 74   {...retval = st
37a0: 64 6f 75 74 3b 0a 09 7d 20 65 6c 73 65 20 69 66  dout;..} else if
37b0: 20 28 66 69 6c 65 5b 30 5d 20 3d 3d 20 27 7c 27   (file[0] == '|'
37c0: 29 20 7b 0a 09 09 66 69 6c 65 2b 2b 3b 0a 09 09  ) {...file++;...
37d0: 72 65 74 76 61 6c 20 3d 20 70 6f 70 65 6e 28 66  retval = popen(f
37e0: 69 6c 65 2c 20 22 77 22 29 3b 0a 09 7d 20 65 6c  ile, "w");..} el
37f0: 73 65 20 7b 0a 09 09 72 65 74 76 61 6c 20 3d 20  se {...retval = 
3800: 66 6f 70 65 6e 28 66 69 6c 65 2c 20 22 61 2b 22  fopen(file, "a+"
3810: 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 72  );..}...return(r
3820: 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69  etval);.}..stati
3830: 63 20 69 6e 74 20 66 69 6c 65 64 5f 6c 6f 67 67  c int filed_logg
3840: 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28  ing_thread_init(
3850: 46 49 4c 45 20 2a 6c 6f 67 66 70 29 20 7b 0a 09  FILE *logfp) {..
3860: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
3870: 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 73  ging_thread_args
3880: 20 2a 61 72 67 73 3b 0a 09 70 74 68 72 65 61 64   *args;..pthread
3890: 5f 74 20 74 68 72 65 61 64 5f 69 64 3b 0a 0a 09  _t thread_id;...
38a0: 61 72 67 73 20 3d 20 6d 61 6c 6c 6f 63 28 73 69  args = malloc(si
38b0: 7a 65 6f 66 28 2a 61 72 67 73 29 29 3b 0a 09 61  zeof(*args));..a
38c0: 72 67 73 2d 3e 66 70 20 3d 20 6c 6f 67 66 70 3b  rgs->fp = logfp;
38d0: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  ...filed_log_msg
38e0: 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a 0a 09  _list = NULL;...
38f0: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e  pthread_mutex_in
3900: 69 74 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  it(&filed_log_ms
3910: 67 5f 6c 69 73 74 5f 6d 75 74 65 78 2c 20 4e 55  g_list_mutex, NU
3920: 4c 4c 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63  LL);...pthread_c
3930: 72 65 61 74 65 28 26 74 68 72 65 61 64 5f 69 64  reate(&thread_id
3940: 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 6c 6f  , NULL, filed_lo
3950: 67 67 69 6e 67 5f 74 68 72 65 61 64 2c 20 61 72  gging_thread, ar
3960: 67 73 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67  gs);...filed_log
3970: 5f 6d 73 67 28 22 53 54 41 52 54 22 29 3b 0a 0a  _msg("START");..
3980: 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 23 65  .return(0);.}.#e
3990: 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 46 49 4c  ndif..#ifdef FIL
39a0: 45 44 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55 54 0a  ED_DONT_TIMEOUT.
39b0: 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f  #define filed_so
39c0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65  ckettimeout_thre
39d0: 61 64 5f 69 6e 69 74 28 29 20 30 0a 23 64 65 66  ad_init() 0.#def
39e0: 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74  ine filed_socket
39f0: 74 69 6d 65 6f 75 74 5f 69 6e 69 74 28 29 20 30  timeout_init() 0
3a00: 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73  .#define filed_s
3a10: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 61 63 63  ockettimeout_acc
3a20: 65 70 74 28 78 29 20 2f 2a 2a 2f 0a 23 64 65 66  ept(x) /**/.#def
3a30: 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74  ine filed_socket
3a40: 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69  timeout_processi
3a50: 6e 67 5f 73 74 61 72 74 28 78 29 20 2f 2a 2a 2f  ng_start(x) /**/
3a60: 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73  .#define filed_s
3a70: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f  ockettimeout_pro
3a80: 63 65 73 73 69 6e 67 5f 65 6e 64 28 78 29 20 2f  cessing_end(x) /
3a90: 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6c 65  **/.#define file
3aa0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
3ab0: 63 6c 6f 73 65 28 78 29 20 2f 2a 2a 2f 0a 23 65  close(x) /**/.#e
3ac0: 6c 73 65 0a 5f 41 74 6f 6d 69 63 20 74 69 6d 65  lse._Atomic time
3ad0: 5f 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  _t filed_sockett
3ae0: 69 6d 65 6f 75 74 5f 74 69 6d 65 3b 0a 73 74 72  imeout_time;.str
3af0: 75 63 74 20 7b 0a 09 5f 41 74 6f 6d 69 63 20 74  uct {.._Atomic t
3b00: 69 6d 65 5f 74 20 65 78 70 69 72 61 74 69 6f 6e  ime_t expiration
3b10: 5f 74 69 6d 65 3b 0a 09 5f 41 74 6f 6d 69 63 20  _time;.._Atomic 
3b20: 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64  pthread_t thread
3b30: 5f 69 64 3b 0a 09 62 6f 6f 6c 20 76 61 6c 69 64  _id;..bool valid
3b40: 3b 0a 7d 2a 20 66 69 6c 65 64 5f 73 6f 63 6b 65  ;.}* filed_socke
3b50: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61  ttimeout_socksta
3b60: 74 75 73 3b 0a 6c 6f 6e 67 20 66 69 6c 65 64 5f  tus;.long filed_
3b70: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f  sockettimeout_so
3b80: 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 3b  ckstatus_length;
3b90: 0a 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65  .int filed_socke
3ba0: 74 74 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c 6c  ttimeout_devnull
3bb0: 5f 66 64 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  _fd;..static int
3bc0: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d   filed_sockettim
3bd0: 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72  eout_sockfd_in_r
3be0: 61 6e 67 65 28 69 6e 74 20 73 6f 63 6b 66 64 29  ange(int sockfd)
3bf0: 20 7b 0a 09 69 66 20 28 73 6f 63 6b 66 64 20 3c   {..if (sockfd <
3c00: 20 33 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 30   3) {...return(0
3c10: 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 73 6f 63 6b  );..}...if (sock
3c20: 66 64 20 3e 20 66 69 6c 65 64 5f 73 6f 63 6b 65  fd > filed_socke
3c30: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61  ttimeout_socksta
3c40: 74 75 73 5f 6c 65 6e 67 74 68 29 20 7b 0a 09 09  tus_length) {...
3c50: 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09  return(0);..}...
3c60: 72 65 74 75 72 6e 28 31 29 3b 0a 7d 0a 0a 73 74  return(1);.}..st
3c70: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f  atic void filed_
3c80: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78  sockettimeout_ex
3c90: 70 69 72 65 28 69 6e 74 20 73 6f 63 6b 66 64 2c  pire(int sockfd,
3ca0: 20 69 6e 74 20 6c 65 6e 67 74 68 29 20 7b 0a 09   int length) {..
3cb0: 74 69 6d 65 5f 74 20 6e 6f 77 2c 20 65 78 70 69  time_t now, expi
3cc0: 72 65 3b 0a 0a 09 6e 6f 77 20 3d 20 61 74 6f 6d  re;...now = atom
3cd0: 69 63 5f 6c 6f 61 64 28 26 66 69 6c 65 64 5f 73  ic_load(&filed_s
3ce0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 69 6d  ockettimeout_tim
3cf0: 65 29 3b 0a 0a 09 65 78 70 69 72 65 20 3d 20 6e  e);...expire = n
3d00: 6f 77 20 2b 20 6c 65 6e 67 74 68 3b 0a 0a 09 61  ow + length;...a
3d10: 74 6f 6d 69 63 5f 73 74 6f 72 65 28 26 66 69 6c  tomic_store(&fil
3d20: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
3d30: 5f 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f 63 6b  _sockstatus[sock
3d40: 66 64 5d 2e 65 78 70 69 72 61 74 69 6f 6e 5f 74  fd].expiration_t
3d50: 69 6d 65 2c 20 65 78 70 69 72 65 29 3b 0a 0a 09  ime, expire);...
3d60: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69  return;.}..stati
3d70: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63  c void filed_soc
3d80: 6b 65 74 74 69 6d 65 6f 75 74 5f 61 63 63 65 70  kettimeout_accep
3d90: 74 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a  t(int sockfd) {.
3da0: 09 69 66 20 28 21 66 69 6c 65 64 5f 73 6f 63 6b  .if (!filed_sock
3db0: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64  ettimeout_sockfd
3dc0: 5f 69 6e 5f 72 61 6e 67 65 28 73 6f 63 6b 66 64  _in_range(sockfd
3dd0: 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 3b 0a 09  )) {...return;..
3de0: 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74  }...filed_socket
3df0: 74 69 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 73  timeout_expire(s
3e00: 6f 63 6b 66 64 2c 20 36 30 29 3b 0a 0a 09 61 74  ockfd, 60);...at
3e10: 6f 6d 69 63 5f 73 74 6f 72 65 28 26 66 69 6c 65  omic_store(&file
3e20: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
3e30: 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f 63 6b 66  sockstatus[sockf
3e40: 64 5d 2e 74 68 72 65 61 64 5f 69 64 2c 20 70 74  d].thread_id, pt
3e50: 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3b 0a 0a  hread_self());..
3e60: 09 61 74 6f 6d 69 63 5f 73 74 6f 72 65 28 26 66  .atomic_store(&f
3e70: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3e80: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f  ut_sockstatus[so
3e90: 63 6b 66 64 5d 2e 76 61 6c 69 64 2c 20 74 72 75  ckfd].valid, tru
3ea0: 65 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a  e);...return;.}.
3eb0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
3ec0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
3ed0: 5f 70 72 6f 63 65 73 73 69 6e 67 5f 73 74 61 72  _processing_star
3ee0: 74 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a  t(int sockfd) {.
3ef0: 09 69 66 20 28 21 66 69 6c 65 64 5f 73 6f 63 6b  .if (!filed_sock
3f00: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64  ettimeout_sockfd
3f10: 5f 69 6e 5f 72 61 6e 67 65 28 73 6f 63 6b 66 64  _in_range(sockfd
3f20: 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 3b 0a 09  )) {...return;..
3f30: 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74  }...filed_socket
3f40: 74 69 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 73  timeout_expire(s
3f50: 6f 63 6b 66 64 2c 20 38 36 34 30 30 29 3b 0a 0a  ockfd, 86400);..
3f60: 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74  .return;.}..stat
3f70: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f  ic void filed_so
3f80: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63  ckettimeout_proc
3f90: 65 73 73 69 6e 67 5f 65 6e 64 28 69 6e 74 20 73  essing_end(int s
3fa0: 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 21 66  ockfd) {..if (!f
3fb0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3fc0: 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e  ut_sockfd_in_ran
3fd0: 67 65 28 73 6f 63 6b 66 64 29 29 20 7b 0a 09 09  ge(sockfd)) {...
3fe0: 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 66 69 6c  return;..}...fil
3ff0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
4000: 5f 65 78 70 69 72 65 28 73 6f 63 6b 66 64 2c 20  _expire(sockfd, 
4010: 36 30 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d  60);...return;.}
4020: 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69  ..static void fi
4030: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
4040: 74 5f 63 6c 6f 73 65 28 69 6e 74 20 73 6f 63 6b  t_close(int sock
4050: 66 64 29 20 7b 0a 09 69 66 20 28 21 66 69 6c 65  fd) {..if (!file
4060: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
4070: 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28  sockfd_in_range(
4080: 73 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 65 74  sockfd)) {...ret
4090: 75 72 6e 3b 0a 09 7d 0a 0a 09 61 74 6f 6d 69 63  urn;..}...atomic
40a0: 5f 73 74 6f 72 65 28 26 66 69 6c 65 64 5f 73 6f  _store(&filed_so
40b0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b  ckettimeout_sock
40c0: 73 74 61 74 75 73 5b 73 6f 63 6b 66 64 5d 2e 76  status[sockfd].v
40d0: 61 6c 69 64 2c 20 66 61 6c 73 65 29 3b 0a 0a 09  alid, false);...
40e0: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69  return;.}..stati
40f0: 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f 73 6f  c void *filed_so
4100: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65  ckettimeout_thre
4110: 61 64 28 76 6f 69 64 20 2a 61 72 67 29 20 7b 0a  ad(void *arg) {.
4120: 09 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63  .struct timespec
4130: 20 73 6c 65 65 70 5f 74 69 6d 65 3b 0a 09 74 69   sleep_time;..ti
4140: 6d 65 5f 74 20 6e 6f 77 2c 20 65 78 70 69 72 61  me_t now, expira
4150: 74 69 6f 6e 5f 74 69 6d 65 3b 0a 09 70 74 68 72  tion_time;..pthr
4160: 65 61 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b  ead_t thread_id;
4170: 0a 09 6c 6f 6e 67 20 69 64 78 3b 0a 09 69 6e 74  ..long idx;..int
4180: 20 63 6f 75 6e 74 3b 0a 09 62 6f 6f 6c 20 76 61   count;..bool va
4190: 6c 69 64 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29  lid;...while (1)
41a0: 20 7b 0a 09 09 66 6f 72 20 28 63 6f 75 6e 74 20   {...for (count 
41b0: 3d 20 30 3b 20 63 6f 75 6e 74 20 3c 20 31 30 3b  = 0; count < 10;
41c0: 20 63 6f 75 6e 74 2b 2b 29 20 7b 0a 09 09 09 73   count++) {....s
41d0: 6c 65 65 70 5f 74 69 6d 65 2e 74 76 5f 73 65 63  leep_time.tv_sec
41e0: 20 3d 20 33 30 3b 0a 09 09 09 73 6c 65 65 70 5f   = 30;....sleep_
41f0: 74 69 6d 65 2e 74 76 5f 6e 73 65 63 20 3d 20 30  time.tv_nsec = 0
4200: 3b 0a 09 09 09 6e 61 6e 6f 73 6c 65 65 70 28 26  ;....nanosleep(&
4210: 73 6c 65 65 70 5f 74 69 6d 65 2c 20 4e 55 4c 4c  sleep_time, NULL
4220: 29 3b 0a 0a 09 09 09 6e 6f 77 20 3d 20 74 69 6d  );.....now = tim
4230: 65 28 4e 55 4c 4c 29 3b 0a 0a 09 09 09 61 74 6f  e(NULL);.....ato
4240: 6d 69 63 5f 73 74 6f 72 65 28 26 66 69 6c 65 64  mic_store(&filed
4250: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74  _sockettimeout_t
4260: 69 6d 65 2c 20 6e 6f 77 29 3b 0a 09 09 7d 0a 0a  ime, now);...}..
4270: 09 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20  ..for (idx = 0; 
4280: 69 64 78 20 3c 20 66 69 6c 65 64 5f 73 6f 63 6b  idx < filed_sock
4290: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74  ettimeout_sockst
42a0: 61 74 75 73 5f 6c 65 6e 67 74 68 3b 20 69 64 78  atus_length; idx
42b0: 2b 2b 29 20 7b 0a 09 09 09 76 61 6c 69 64 20 3d  ++) {....valid =
42c0: 20 61 74 6f 6d 69 63 5f 6c 6f 61 64 28 26 66 69   atomic_load(&fi
42d0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
42e0: 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 69 64 78  t_sockstatus[idx
42f0: 5d 2e 76 61 6c 69 64 29 3b 0a 0a 09 09 09 69 66  ].valid);.....if
4300: 20 28 21 76 61 6c 69 64 29 20 7b 0a 09 09 09 09   (!valid) {.....
4310: 63 6f 6e 74 69 6e 75 65 3b 0a 09 09 09 7d 0a 0a  continue;....}..
4320: 09 09 09 65 78 70 69 72 61 74 69 6f 6e 5f 74 69  ...expiration_ti
4330: 6d 65 20 3d 20 61 74 6f 6d 69 63 5f 6c 6f 61 64  me = atomic_load
4340: 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  (&filed_socketti
4350: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73  meout_sockstatus
4360: 5b 69 64 78 5d 2e 65 78 70 69 72 61 74 69 6f 6e  [idx].expiration
4370: 5f 74 69 6d 65 29 3b 0a 0a 09 09 09 74 68 72 65  _time);.....thre
4380: 61 64 5f 69 64 20 3d 20 61 74 6f 6d 69 63 5f 6c  ad_id = atomic_l
4390: 6f 61 64 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65  oad(&filed_socke
43a0: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61  ttimeout_socksta
43b0: 74 75 73 5b 69 64 78 5d 2e 74 68 72 65 61 64 5f  tus[idx].thread_
43c0: 69 64 29 3b 0a 0a 09 09 09 69 66 20 28 65 78 70  id);.....if (exp
43d0: 69 72 61 74 69 6f 6e 5f 74 69 6d 65 20 3e 20 6e  iration_time > n
43e0: 6f 77 29 20 7b 0a 09 09 09 09 63 6f 6e 74 69 6e  ow) {.....contin
43f0: 75 65 3b 0a 09 09 09 7d 0a 0a 09 09 09 66 69 6c  ue;....}.....fil
4400: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
4410: 5f 63 6c 6f 73 65 28 69 64 78 29 3b 0a 0a 09 09  _close(idx);....
4420: 09 64 75 70 32 28 66 69 6c 65 64 5f 73 6f 63 6b  .dup2(filed_sock
4430: 65 74 74 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c  ettimeout_devnul
4440: 6c 5f 66 64 2c 20 69 64 78 29 3b 0a 0a 09 09 09  l_fd, idx);.....
4450: 70 74 68 72 65 61 64 5f 6b 69 6c 6c 28 74 68 72  pthread_kill(thr
4460: 65 61 64 5f 69 64 2c 20 53 49 47 50 49 50 45 29  ead_id, SIGPIPE)
4470: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72  ;...}..}...retur
4480: 6e 28 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 4e 4f  n(NULL);.../* NO
4490: 54 52 45 41 43 48 3a 20 57 65 20 64 6f 6e 27 74  TREACH: We don't
44a0: 20 61 63 74 75 61 6c 6c 79 20 74 61 6b 65 20 61   actually take a
44b0: 6e 79 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  ny arguments */.
44c0: 09 61 72 67 20 3d 20 61 72 67 3b 0a 7d 0a 0a 73  .arg = arg;.}..s
44d0: 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f  tatic int filed_
44e0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68  sockettimeout_th
44f0: 72 65 61 64 5f 69 6e 69 74 28 76 6f 69 64 29 20  read_init(void) 
4500: 7b 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72  {..pthread_t thr
4510: 65 61 64 5f 69 64 3b 0a 0a 09 70 74 68 72 65 61  ead_id;...pthrea
4520: 64 5f 63 72 65 61 74 65 28 26 74 68 72 65 61 64  d_create(&thread
4530: 5f 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64  _id, NULL, filed
4540: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74  _sockettimeout_t
4550: 68 72 65 61 64 2c 20 4e 55 4c 4c 29 3b 0a 0a 09  hread, NULL);...
4560: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 73 74  return(0);.}..st
4570: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 73  atic int filed_s
4580: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69  ockettimeout_ini
4590: 74 28 76 6f 69 64 29 20 7b 0a 09 6c 6f 6e 67 20  t(void) {..long 
45a0: 6d 61 78 66 64 2c 20 69 64 78 3b 0a 0a 09 6d 61  maxfd, idx;...ma
45b0: 78 66 64 20 3d 20 73 79 73 63 6f 6e 66 28 5f 53  xfd = sysconf(_S
45c0: 43 5f 4f 50 45 4e 5f 4d 41 58 29 3b 0a 09 69 66  C_OPEN_MAX);..if
45d0: 20 28 6d 61 78 66 64 20 3c 3d 20 30 29 20 7b 0a   (maxfd <= 0) {.
45e0: 09 09 6d 61 78 66 64 20 3d 20 34 30 39 36 3b 0a  ..maxfd = 4096;.
45f0: 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65  .}...filed_socke
4600: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61  ttimeout_socksta
4610: 74 75 73 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a  tus = malloc(siz
4620: 65 6f 66 28 2a 66 69 6c 65 64 5f 73 6f 63 6b 65  eof(*filed_socke
4630: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61  ttimeout_socksta
4640: 74 75 73 29 20 2a 20 6d 61 78 66 64 29 3b 0a 09  tus) * maxfd);..
4650: 69 66 20 28 66 69 6c 65 64 5f 73 6f 63 6b 65 74  if (filed_socket
4660: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74  timeout_sockstat
4670: 75 73 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09  us == NULL) {...
4680: 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a  return(-1);..}..
4690: 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 69  .for (idx = 0; i
46a0: 64 78 20 3c 20 6d 61 78 66 64 3b 20 69 64 78 2b  dx < maxfd; idx+
46b0: 2b 29 20 7b 0a 09 09 66 69 6c 65 64 5f 73 6f 63  +) {...filed_soc
46c0: 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73  kettimeout_socks
46d0: 74 61 74 75 73 5b 69 64 78 5d 2e 76 61 6c 69 64  tatus[idx].valid
46e0: 20 3d 20 66 61 6c 73 65 3b 0a 09 7d 0a 0a 09 66   = false;..}...f
46f0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
4700: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c 65  ut_sockstatus_le
4710: 6e 67 74 68 20 3d 20 6d 61 78 66 64 3b 0a 09 66  ngth = maxfd;..f
4720: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
4730: 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 20 3d 20  ut_devnull_fd = 
4740: 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22  open("/dev/null"
4750: 2c 20 4f 5f 52 44 57 52 29 3b 0a 09 69 66 20 28  , O_RDWR);..if (
4760: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
4770: 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 20 3c  out_devnull_fd <
4780: 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 2d   0) {...return(-
4790: 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28  1);..}...return(
47a0: 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  0);.}.#endif../*
47b0: 20 46 6f 72 6d 61 74 20 74 69 6d 65 20 70 65 72   Format time per
47c0: 20 52 46 43 32 36 31 36 20 2a 2f 0a 73 74 61 74   RFC2616 */.stat
47d0: 69 63 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 66  ic char *filed_f
47e0: 6f 72 6d 61 74 5f 74 69 6d 65 28 63 68 61 72 20  ormat_time(char 
47f0: 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f 74 20  *buffer, size_t 
4800: 62 75 66 66 65 72 5f 6c 65 6e 2c 20 63 6f 6e 73  buffer_len, cons
4810: 74 20 74 69 6d 65 5f 74 20 74 69 6d 65 69 6e 66  t time_t timeinf
4820: 6f 29 20 7b 0a 09 73 74 72 75 63 74 20 74 6d 20  o) {..struct tm 
4830: 74 69 6d 65 69 6e 66 6f 5f 74 6d 2c 20 2a 74 69  timeinfo_tm, *ti
4840: 6d 65 69 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a 09 74  meinfo_tm_p;...t
4850: 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d 20 67  imeinfo_tm_p = g
4860: 6d 74 69 6d 65 5f 72 28 26 74 69 6d 65 69 6e 66  mtime_r(&timeinf
4870: 6f 2c 20 26 74 69 6d 65 69 6e 66 6f 5f 74 6d 29  o, &timeinfo_tm)
4880: 3b 0a 09 69 66 20 28 74 69 6d 65 69 6e 66 6f 5f  ;..if (timeinfo_
4890: 74 6d 5f 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  tm_p == NULL) {.
48a0: 09 09 72 65 74 75 72 6e 28 22 75 6e 6b 6e 6f 77  ..return("unknow
48b0: 6e 22 29 3b 0a 09 7d 0a 0a 09 62 75 66 66 65 72  n");..}...buffer
48c0: 5b 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 31 5d  [buffer_len - 1]
48d0: 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72   = '\0';..buffer
48e0: 5f 6c 65 6e 20 3d 20 73 74 72 66 74 69 6d 65 28  _len = strftime(
48f0: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c  buffer, buffer_l
4900: 65 6e 20 2d 20 31 2c 20 22 25 61 2c 20 25 64 20  en - 1, "%a, %d 
4910: 25 62 20 25 59 20 25 48 3a 25 4d 3a 25 53 20 47  %b %Y %H:%M:%S G
4920: 4d 54 22 2c 20 74 69 6d 65 69 6e 66 6f 5f 74 6d  MT", timeinfo_tm
4930: 5f 70 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75  _p);...return(bu
4940: 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 61 73  ffer);.}../* has
4950: 68 20 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69  h */.static unsi
4960: 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 5f 68  gned int filed_h
4970: 61 73 68 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ash(const unsign
4980: 65 64 20 63 68 61 72 20 2a 76 61 6c 75 65 2c 20  ed char *value, 
4990: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 6f 64  unsigned int mod
49a0: 75 6c 75 73 29 20 7b 0a 09 75 6e 73 69 67 6e 65  ulus) {..unsigne
49b0: 64 20 63 68 61 72 20 63 75 72 72 2c 20 70 72 65  d char curr, pre
49c0: 76 3b 0a 09 69 6e 74 20 64 69 66 66 3b 0a 09 75  v;..int diff;..u
49d0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 65 74 76  nsigned int retv
49e0: 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d  al;...retval = m
49f0: 6f 64 75 6c 75 73 20 2d 20 31 3b 0a 09 70 72 65  odulus - 1;..pre
4a00: 76 20 3d 20 6d 6f 64 75 6c 75 73 20 25 20 32 35  v = modulus % 25
4a10: 35 3b 0a 0a 09 77 68 69 6c 65 20 28 28 63 75 72  5;...while ((cur
4a20: 72 20 3d 20 2a 76 61 6c 75 65 29 29 20 7b 0a 09  r = *value)) {..
4a30: 09 69 66 20 28 63 75 72 72 20 3c 20 33 32 29 20  .if (curr < 32) 
4a40: 7b 0a 09 09 09 63 75 72 72 20 3d 20 32 35 35 20  {....curr = 255 
4a50: 2d 20 63 75 72 72 3b 0a 09 09 7d 20 65 6c 73 65  - curr;...} else
4a60: 20 7b 0a 09 09 09 63 75 72 72 20 2d 3d 20 33 32   {....curr -= 32
4a70: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 70 72 65  ;...}....if (pre
4a80: 76 20 3c 20 63 75 72 72 29 20 7b 0a 09 09 09 64  v < curr) {....d
4a90: 69 66 66 20 3d 20 63 75 72 72 20 2d 20 70 72 65  iff = curr - pre
4aa0: 76 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09  v;...} else {...
4ab0: 09 64 69 66 66 20 3d 20 70 72 65 76 20 2d 20 63  .diff = prev - c
4ac0: 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 70 72 65 76  urr;...}....prev
4ad0: 20 3d 20 63 75 72 72 3b 0a 0a 09 09 72 65 74 76   = curr;....retv
4ae0: 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 65 74 76  al <<= 3;...retv
4af0: 61 6c 20 26 3d 20 30 78 46 46 46 46 46 46 46 46  al &= 0xFFFFFFFF
4b00: 4c 55 3b 0a 09 09 72 65 74 76 61 6c 20 5e 3d 20  LU;...retval ^= 
4b10: 64 69 66 66 3b 0a 0a 09 09 76 61 6c 75 65 2b 2b  diff;....value++
4b20: 3b 0a 09 7d 0a 0a 09 72 65 74 76 61 6c 20 3d 20  ;..}...retval = 
4b30: 72 65 74 76 61 6c 20 25 20 6d 6f 64 75 6c 75 73  retval % modulus
4b40: 3b 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61  ;...return(retva
4b50: 6c 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 20 61  l);.}../* Find a
4b60: 20 6d 69 6d 65 2d 74 79 70 65 20 62 61 73 65 64   mime-type based
4b70: 20 6f 6e 20 74 68 65 20 66 69 6c 65 6e 61 6d 65   on the filename
4b80: 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
4b90: 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 64 65 74   char *filed_det
4ba0: 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28  ermine_mimetype(
4bb0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68  const char *path
4bc0: 29 20 7b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20  ) {..const char 
4bd0: 2a 70 3b 0a 0a 09 70 20 3d 20 73 74 72 72 63 68  *p;...p = strrch
4be0: 72 28 70 61 74 68 2c 20 27 2e 27 29 3b 0a 09 69  r(path, '.');..i
4bf0: 66 20 28 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  f (p == NULL) {.
4c00: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 44  ..return(FILED_D
4c10: 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d  EFAULT_TYPE);..}
4c20: 0a 0a 09 70 2b 2b 3b 0a 09 69 66 20 28 2a 70 20  ...p++;..if (*p 
4c30: 3d 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 65 74  == '\0') {...ret
4c40: 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c  urn(FILED_DEFAUL
4c50: 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 66 69  T_TYPE);..}...fi
4c60: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75  led_log_msg_debu
4c70: 67 28 22 4c 6f 6f 6b 69 6e 67 20 75 70 20 4d 49  g("Looking up MI
4c80: 4d 45 20 74 79 70 65 20 66 6f 72 20 25 73 20 28  ME type for %s (
4c90: 68 61 73 68 20 3d 20 25 6c 6c 75 29 22 2c 20 70  hash = %llu)", p
4ca0: 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  , (unsigned long
4cb0: 20 6c 6f 6e 67 29 20 66 69 6c 65 64 5f 68 61 73   long) filed_has
4cc0: 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  h((const unsigne
4cd0: 64 20 63 68 61 72 20 2a 29 20 70 2c 20 31 36 37  d char *) p, 167
4ce0: 37 37 32 35 39 29 29 3b 0a 0a 23 69 6e 63 6c 75  77259));..#inclu
4cf0: 64 65 20 22 66 69 6c 65 64 2d 6d 69 6d 65 2d 74  de "filed-mime-t
4d00: 79 70 65 73 2e 68 22 0a 0a 09 72 65 74 75 72 6e  ypes.h"...return
4d10: 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54  (FILED_DEFAULT_T
4d20: 59 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 6e 65  YPE);.}../* Gene
4d30: 72 61 74 65 20 61 20 75 6e 69 71 75 65 20 69 64  rate a unique id
4d40: 65 6e 74 69 66 69 65 72 20 2a 2f 0a 73 74 61 74  entifier */.stat
4d50: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67 65  ic void filed_ge
4d60: 6e 65 72 61 74 65 5f 65 74 61 67 28 63 68 61 72  nerate_etag(char
4d70: 20 2a 65 74 61 67 2c 20 73 69 7a 65 5f 74 20 6c   *etag, size_t l
4d80: 65 6e 67 74 68 29 20 7b 0a 09 73 6e 70 72 69 6e  ength) {..snprin
4d90: 74 66 28 65 74 61 67 2c 20 6c 65 6e 67 74 68 2c  tf(etag, length,
4da0: 20 22 25 6c 6c 78 2d 25 6c 6c 78 25 6c 6c 78 25   "%llx-%llx%llx%
4db0: 6c 6c 78 25 6c 6c 78 22 2c 0a 09 09 28 75 6e 73  llx%llx",...(uns
4dc0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
4dd0: 20 74 69 6d 65 28 4e 55 4c 4c 29 2c 0a 09 09 28   time(NULL),...(
4de0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
4df0: 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09  ng) random(),...
4e00: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
4e10: 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09  ong) random(),..
4e20: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
4e30: 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a  long) random(),.
4e40: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
4e50: 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 0a   long) random().
4e60: 09 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 46 49  .);.}..#ifdef FI
4e70: 4c 45 44 5f 46 41 4b 45 5f 43 48 52 4f 4f 54 0a  LED_FAKE_CHROOT.
4e80: 2f 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 70  /* Translate a p
4e90: 61 74 68 20 69 6e 74 6f 20 61 20 66 61 6b 65 20  ath into a fake 
4ea0: 63 68 72 6f 6f 74 20 70 61 74 68 20 2a 2f 0a 73  chroot path */.s
4eb0: 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
4ec0: 20 2a 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 61   *filed_path_tra
4ed0: 6e 73 6c 61 74 65 28 63 6f 6e 73 74 20 63 68 61  nslate(const cha
4ee0: 72 20 2a 70 61 74 68 2c 20 73 74 72 75 63 74 20  r *path, struct 
4ef0: 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f  filed_options *o
4f00: 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 61 74 69  ptions) {..stati
4f10: 63 20 5f 5f 74 68 72 65 61 64 20 63 68 61 72 20  c __thread char 
4f20: 70 61 74 68 42 75 66 66 65 72 5b 38 31 39 32 5d  pathBuffer[8192]
4f30: 3b 0a 09 69 6e 74 20 73 6e 70 72 69 6e 74 66 5f  ;..int snprintf_
4f40: 72 65 74 3b 0a 0a 09 2f 2a 20 49 66 20 6e 6f 20  ret;.../* If no 
4f50: 61 6c 74 65 72 6e 61 74 69 76 65 20 72 6f 6f 74  alternative root
4f60: 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 72   is specified, r
4f70: 65 74 75 72 6e 20 74 68 65 20 75 6e 61 64 6f 72  eturn the unador
4f80: 6e 65 64 20 70 61 74 68 20 2a 2f 0a 09 69 66 20  ned path */..if 
4f90: 28 21 6f 70 74 69 6f 6e 73 2d 3e 66 61 6b 65 5f  (!options->fake_
4fa0: 6e 65 77 72 6f 6f 74 29 20 7b 0a 09 09 72 65 74  newroot) {...ret
4fb0: 75 72 6e 28 70 61 74 68 29 3b 0a 09 7d 0a 0a 09  urn(path);..}...
4fc0: 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
4fd0: 68 69 73 20 72 65 71 75 65 73 74 20 77 69 6c 6c  his request will
4fe0: 20 6e 6f 74 20 67 6f 20 6f 75 74 73 69 64 65 20   not go outside 
4ff0: 6f 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64  of the specified
5000: 20 72 6f 6f 74 20 2a 2f 0a 09 69 66 20 28 73 74   root */..if (st
5010: 72 73 74 72 28 70 61 74 68 2c 20 22 2f 2e 2e 2f  rstr(path, "/../
5020: 22 29 20 21 3d 20 4e 55 4c 4c 20 7c 7c 20 70 61  ") != NULL || pa
5030: 74 68 5b 30 5d 20 21 3d 20 27 2f 27 29 20 7b 0a  th[0] != '/') {.
5040: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  ..filed_log_msg_
5050: 64 65 62 75 67 28 22 55 6e 61 62 6c 65 20 74 6f  debug("Unable to
5060: 20 74 72 61 6e 73 6c 61 74 65 20 70 61 74 68 20   translate path 
5070: 5c 22 25 73 5c 22 2c 20 63 6f 6e 74 61 69 6e 73  \"%s\", contains
5080: 20 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74   invalid charact
5090: 65 72 73 22 2c 20 70 61 74 68 29 3b 0a 0a 09 09  ers", path);....
50a0: 72 65 74 75 72 6e 28 6f 70 74 69 6f 6e 73 2d 3e  return(options->
50b0: 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 29 3b 0a 09  fake_newroot);..
50c0: 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 74 68  }.../* Create th
50d0: 65 20 6e 65 77 20 70 61 74 68 20 69 6e 74 6f 20  e new path into 
50e0: 6f 75 72 20 6c 6f 63 61 6c 20 28 54 4c 53 29 20  our local (TLS) 
50f0: 73 74 61 74 69 63 20 62 75 66 66 65 72 20 2a 2f  static buffer */
5100: 0a 09 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3d  ..snprintf_ret =
5110: 20 73 6e 70 72 69 6e 74 66 28 70 61 74 68 42 75   snprintf(pathBu
5120: 66 66 65 72 2c 20 73 69 7a 65 6f 66 28 70 61 74  ffer, sizeof(pat
5130: 68 42 75 66 66 65 72 29 2c 20 22 25 73 2f 25 73  hBuffer), "%s/%s
5140: 22 2c 20 6f 70 74 69 6f 6e 73 2d 3e 66 61 6b 65  ", options->fake
5150: 5f 6e 65 77 72 6f 6f 74 2c 20 70 61 74 68 29 3b  _newroot, path);
5160: 0a 09 69 66 20 28 73 6e 70 72 69 6e 74 66 5f 72  ..if (snprintf_r
5170: 65 74 20 3c 20 30 20 7c 7c 20 28 28 75 6e 73 69  et < 0 || ((unsi
5180: 67 6e 65 64 20 69 6e 74 29 20 73 6e 70 72 69 6e  gned int) snprin
5190: 74 66 5f 72 65 74 29 20 3e 3d 20 73 69 7a 65 6f  tf_ret) >= sizeo
51a0: 66 28 70 61 74 68 42 75 66 66 65 72 29 29 20 7b  f(pathBuffer)) {
51b0: 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  ...filed_log_msg
51c0: 5f 64 65 62 75 67 28 22 55 6e 61 62 6c 65 20 74  _debug("Unable t
51d0: 6f 20 74 72 61 6e 73 6c 61 74 65 20 70 61 74 68  o translate path
51e0: 20 5c 22 25 73 5c 22 2c 20 77 69 6c 6c 20 6e 6f   \"%s\", will no
51f0: 74 20 66 69 74 20 69 6e 74 6f 20 6e 65 77 20 62  t fit into new b
5200: 75 66 66 65 72 22 2c 20 70 61 74 68 29 3b 0a 0a  uffer", path);..
5210: 09 09 72 65 74 75 72 6e 28 6f 70 74 69 6f 6e 73  ..return(options
5220: 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 29 3b  ->fake_newroot);
5230: 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f  ..}...filed_log_
5240: 6d 73 67 5f 64 65 62 75 67 28 22 54 72 61 6e 73  msg_debug("Trans
5250: 6c 61 74 69 6e 67 20 70 61 74 68 20 5c 22 25 73  lating path \"%s
5260: 5c 22 20 69 6e 74 6f 20 5c 22 25 73 5c 22 22 2c  \" into \"%s\"",
5270: 20 70 61 74 68 2c 20 70 61 74 68 42 75 66 66 65   path, pathBuffe
5280: 72 29 3b 0a 0a 09 2f 2a 20 52 65 74 75 72 6e 20  r);.../* Return 
5290: 74 68 65 20 6e 65 77 20 70 61 74 68 20 2a 2f 0a  the new path */.
52a0: 09 72 65 74 75 72 6e 28 70 61 74 68 42 75 66 66  .return(pathBuff
52b0: 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  er);.}..static v
52c0: 6f 69 64 20 66 69 6c 65 64 5f 70 61 74 68 5f 74  oid filed_path_t
52d0: 72 61 6e 73 6c 61 74 65 5f 73 65 74 5f 72 6f 6f  ranslate_set_roo
52e0: 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 76 61  t(const char *va
52f0: 72 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f  r, struct filed_
5300: 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73  options *options
5310: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 76 61  , const char *va
5320: 6c 29 20 7b 0a 09 6f 70 74 69 6f 6e 73 2d 3e 66  l) {..options->f
5330: 61 6b 65 5f 6e 65 77 72 6f 6f 74 20 3d 20 73 74  ake_newroot = st
5340: 72 64 75 70 28 76 61 6c 29 3b 0a 0a 09 72 65 74  rdup(val);...ret
5350: 75 72 6e 3b 0a 0a 09 2f 2a 20 76 61 72 20 69 73  urn;.../* var is
5360: 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 74 68   only used in th
5370: 65 20 6d 61 63 72 6f 20 2d 2d 20 64 69 73 63 61  e macro -- disca
5380: 72 64 20 69 74 20 68 65 72 65 20 2a 2f 0a 09 76  rd it here */..v
5390: 61 72 20 3d 20 76 61 72 3b 0a 7d 0a 23 65 6c 73  ar = var;.}.#els
53a0: 65 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f  e.#define filed_
53b0: 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 28 70  path_translate(p
53c0: 61 74 68 2c 20 6f 70 74 69 6f 6e 73 29 20 70 61  ath, options) pa
53d0: 74 68 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64  th.#define filed
53e0: 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 5f  _path_translate_
53f0: 73 65 74 5f 72 6f 6f 74 28 76 61 72 2c 20 6f 70  set_root(var, op
5400: 74 69 6f 6e 73 2c 20 76 61 6c 29 20 76 61 72 20  tions, val) var 
5410: 3d 20 73 74 72 64 75 70 28 76 61 6c 29 0a 23 65  = strdup(val).#e
5420: 6e 64 69 66 0a 0a 2f 2a 20 4f 70 65 6e 20 61 20  ndif../* Open a 
5430: 66 69 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20  file and return 
5440: 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  file information
5450: 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63   */.static struc
5460: 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  t filed_fileinfo
5470: 20 2a 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c   *filed_open_fil
5480: 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61  e(const char *pa
5490: 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64  th, struct filed
54a0: 5f 66 69 6c 65 69 6e 66 6f 20 2a 62 75 66 66 65  _fileinfo *buffe
54b0: 72 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f  r, struct filed_
54c0: 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73  options *options
54d0: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65  ) {..struct file
54e0: 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 63 68  d_fileinfo *cach
54f0: 65 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74  e;..unsigned int
5500: 20 63 61 63 68 65 5f 69 64 78 3b 0a 09 6f 66 66   cache_idx;..off
5510: 5f 74 20 6c 65 6e 3b 0a 09 69 6e 74 20 66 64 3b  _t len;..int fd;
5520: 0a 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c  ...if (filed_fil
5530: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69  einfo_fdcache_si
5540: 7a 65 20 21 3d 20 30 29 20 7b 0a 09 09 63 61 63  ze != 0) {...cac
5550: 68 65 5f 69 64 78 20 3d 20 66 69 6c 65 64 5f 68  he_idx = filed_h
5560: 61 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67  ash((const unsig
5570: 6e 65 64 20 63 68 61 72 20 2a 29 20 70 61 74 68  ned char *) path
5580: 2c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  , filed_fileinfo
5590: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  _fdcache_size);.
55a0: 0a 09 09 63 61 63 68 65 20 3d 20 26 66 69 6c 65  ...cache = &file
55b0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63  d_fileinfo_fdcac
55c0: 68 65 5b 63 61 63 68 65 5f 69 64 78 5d 3b 0a 0a  he[cache_idx];..
55d0: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  ..filed_log_msg_
55e0: 64 65 62 75 67 28 22 4c 6f 63 6b 69 6e 67 20 6d  debug("Locking m
55f0: 75 74 65 78 20 66 6f 72 20 69 64 78 3a 20 25 6c  utex for idx: %l
5600: 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f  u", (unsigned lo
5610: 6e 67 29 20 63 61 63 68 65 5f 69 64 78 29 3b 0a  ng) cache_idx);.
5620: 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78  ...pthread_mutex
5630: 5f 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75  _lock(&cache->mu
5640: 74 65 78 29 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c  tex);....filed_l
5650: 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f  og_msg_debug("Co
5660: 6d 70 6c 65 74 65 64 20 6c 6f 63 6b 69 6e 67 20  mpleted locking 
5670: 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a 20 25  mutex for idx: %
5680: 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c  lu", (unsigned l
5690: 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 29 3b  ong) cache_idx);
56a0: 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 63 61 63  ..} else {...cac
56b0: 68 65 5f 69 64 78 20 3d 20 30 3b 0a 09 09 63 61  he_idx = 0;...ca
56c0: 63 68 65 20 3d 20 62 75 66 66 65 72 3b 0a 09 09  che = buffer;...
56d0: 63 61 63 68 65 2d 3e 70 61 74 68 5b 30 5d 20 3d  cache->path[0] =
56e0: 20 27 5c 30 27 3b 0a 09 09 63 61 63 68 65 2d 3e   '\0';...cache->
56f0: 66 64 20 3d 20 2d 31 3b 0a 09 7d 0a 0a 09 69 66  fd = -1;..}...if
5700: 20 28 73 74 72 63 6d 70 28 70 61 74 68 2c 20 63   (strcmp(path, c
5710: 61 63 68 65 2d 3e 70 61 74 68 29 20 21 3d 20 30  ache->path) != 0
5720: 29 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f  ) {...filed_log_
5730: 6d 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65  msg_debug("Cache
5740: 20 6d 69 73 73 20 66 6f 72 20 69 64 78 3a 20 25   miss for idx: %
5750: 6c 75 3a 20 4f 4c 44 20 5c 22 25 73 5c 22 2c 20  lu: OLD \"%s\", 
5760: 4e 45 57 20 5c 22 25 73 5c 22 22 2c 20 28 75 6e  NEW \"%s\"", (un
5770: 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63  signed long) cac
5780: 68 65 5f 69 64 78 2c 20 63 61 63 68 65 2d 3e 70  he_idx, cache->p
5790: 61 74 68 2c 20 70 61 74 68 29 3b 0a 0a 09 09 66  ath, path);....f
57a0: 64 20 3d 20 6f 70 65 6e 28 66 69 6c 65 64 5f 70  d = open(filed_p
57b0: 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 28 70 61  ath_translate(pa
57c0: 74 68 2c 20 6f 70 74 69 6f 6e 73 29 2c 20 4f 5f  th, options), O_
57d0: 52 44 4f 4e 4c 59 20 7c 20 4f 5f 4c 41 52 47 45  RDONLY | O_LARGE
57e0: 46 49 4c 45 29 3b 0a 09 09 69 66 20 28 66 64 20  FILE);...if (fd 
57f0: 3c 20 30 29 20 7b 0a 09 09 09 69 66 20 28 66 69  < 0) {....if (fi
5800: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
5810: 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 30 29 20  ache_size != 0) 
5820: 7b 0a 09 09 09 09 70 74 68 72 65 61 64 5f 6d 75  {.....pthread_mu
5830: 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68  tex_unlock(&cach
5840: 65 2d 3e 6d 75 74 65 78 29 3b 0a 09 09 09 7d 0a  e->mutex);....}.
5850: 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29  ....return(NULL)
5860: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 63 61 63  ;...}....if (cac
5870: 68 65 2d 3e 66 64 20 3e 3d 20 30 29 20 7b 0a 09  he->fd >= 0) {..
5880: 09 09 63 6c 6f 73 65 28 63 61 63 68 65 2d 3e 66  ..close(cache->f
5890: 64 29 3b 0a 09 09 7d 0a 0a 09 09 6c 65 6e 20 3d  d);...}....len =
58a0: 20 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 53 45   lseek(fd, 0, SE
58b0: 45 4b 5f 45 4e 44 29 3b 0a 09 09 6c 73 65 65 6b  EK_END);...lseek
58c0: 28 66 64 2c 20 30 2c 20 53 45 45 4b 5f 53 45 54  (fd, 0, SEEK_SET
58d0: 29 3b 0a 0a 09 09 63 61 63 68 65 2d 3e 66 64 20  );....cache->fd 
58e0: 3d 20 66 64 3b 0a 09 09 63 61 63 68 65 2d 3e 6c  = fd;...cache->l
58f0: 65 6e 20 3d 20 6c 65 6e 3b 0a 09 09 73 74 72 63  en = len;...strc
5900: 70 79 28 63 61 63 68 65 2d 3e 70 61 74 68 2c 20  py(cache->path, 
5910: 70 61 74 68 29 3b 0a 09 09 63 61 63 68 65 2d 3e  path);...cache->
5920: 74 79 70 65 20 3d 20 66 69 6c 65 64 5f 64 65 74  type = filed_det
5930: 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28  ermine_mimetype(
5940: 70 61 74 68 29 3b 0a 09 09 66 69 6c 65 64 5f 67  path);...filed_g
5950: 65 6e 65 72 61 74 65 5f 65 74 61 67 28 63 61 63  enerate_etag(cac
5960: 68 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f 66  he->etag, sizeof
5970: 28 63 61 63 68 65 2d 3e 65 74 61 67 29 29 3b 0a  (cache->etag));.
5980: 0a 09 09 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20  .../* XXX:TODO: 
5990: 44 65 74 65 72 6d 69 6e 65 20 2a 2f 0a 09 09 63  Determine */...c
59a0: 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20  ache->lastmod = 
59b0: 66 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d  filed_format_tim
59c0: 65 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64  e(cache->lastmod
59d0: 5f 62 2c 20 73 69 7a 65 6f 66 28 63 61 63 68 65  _b, sizeof(cache
59e0: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 2c 20 74 69  ->lastmod_b), ti
59f0: 6d 65 28 4e 55 4c 4c 29 20 2d 20 33 30 29 3b 0a  me(NULL) - 30);.
5a00: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 66 69 6c 65  .} else {...file
5a10: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28  d_log_msg_debug(
5a20: 22 43 61 63 68 65 20 68 69 74 20 66 6f 72 20 69  "Cache hit for i
5a30: 64 78 3a 20 25 6c 75 3a 20 50 41 54 48 20 5c 22  dx: %lu: PATH \"
5a40: 25 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e 65 64  %s\"", (unsigned
5a50: 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78   long) cache_idx
5a60: 2c 20 70 61 74 68 29 3b 0a 09 7d 0a 0a 09 69 66  , path);..}...if
5a70: 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f   (filed_fileinfo
5a80: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 21 3d  _fdcache_size !=
5a90: 20 30 29 20 7b 0a 09 09 2f 2a 0a 09 09 20 2a 20   0) {.../*... * 
5aa0: 57 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20  We have to make 
5ab0: 61 20 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20  a duplicate FD, 
5ac0: 62 65 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20  because once we 
5ad0: 72 65 6c 65 61 73 65 20 74 68 65 20 63 61 63 68  release the cach
5ae0: 65 0a 09 09 20 2a 20 6d 75 74 65 78 2c 20 74 68  e... * mutex, th
5af0: 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
5b00: 72 20 6d 61 79 20 62 65 20 63 6c 6f 73 65 64 0a  r may be closed.
5b10: 09 09 20 2a 2f 0a 09 09 66 64 20 3d 20 64 75 70  .. */...fd = dup
5b20: 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 09 69  (cache->fd);...i
5b30: 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 09  f (fd < 0) {....
5b40: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e  pthread_mutex_un
5b50: 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74  lock(&cache->mut
5b60: 65 78 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28  ex);.....return(
5b70: 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 62 75  NULL);...}....bu
5b80: 66 66 65 72 2d 3e 66 64 20 3d 20 66 64 3b 0a 09  ffer->fd = fd;..
5b90: 09 62 75 66 66 65 72 2d 3e 6c 65 6e 20 3d 20 63  .buffer->len = c
5ba0: 61 63 68 65 2d 3e 6c 65 6e 3b 0a 09 09 62 75 66  ache->len;...buf
5bb0: 66 65 72 2d 3e 74 79 70 65 20 3d 20 63 61 63 68  fer->type = cach
5bc0: 65 2d 3e 74 79 70 65 3b 0a 09 09 6d 65 6d 63 70  e->type;...memcp
5bd0: 79 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f  y(buffer->lastmo
5be0: 64 5f 62 2c 20 63 61 63 68 65 2d 3e 6c 61 73 74  d_b, cache->last
5bf0: 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 28 62 75  mod_b, sizeof(bu
5c00: 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29  ffer->lastmod_b)
5c10: 29 3b 0a 09 09 6d 65 6d 63 70 79 28 62 75 66 66  );...memcpy(buff
5c20: 65 72 2d 3e 65 74 61 67 2c 20 63 61 63 68 65 2d  er->etag, cache-
5c30: 3e 65 74 61 67 2c 20 73 69 7a 65 6f 66 28 62 75  >etag, sizeof(bu
5c40: 66 66 65 72 2d 3e 65 74 61 67 29 29 3b 0a 09 09  ffer->etag));...
5c50: 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 20  buffer->lastmod 
5c60: 3d 20 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f  = buffer->lastmo
5c70: 64 5f 62 20 2b 20 28 63 61 63 68 65 2d 3e 6c 61  d_b + (cache->la
5c80: 73 74 6d 6f 64 20 2d 20 63 61 63 68 65 2d 3e 6c  stmod - cache->l
5c90: 61 73 74 6d 6f 64 5f 62 29 3b 0a 0a 09 09 70 74  astmod_b);....pt
5ca0: 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f  hread_mutex_unlo
5cb0: 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78  ck(&cache->mutex
5cc0: 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 62  );..}...return(b
5cd0: 75 66 66 65 72 29 3b 0a 0a 09 2f 2a 20 6f 70 74  uffer);.../* opt
5ce0: 69 6f 6e 73 20 69 73 20 6f 6e 6c 79 20 75 73 65  ions is only use
5cf0: 64 20 69 66 20 66 61 6b 65 20 63 68 72 6f 6f 74  d if fake chroot
5d00: 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 63 6f 6e   is enabled, con
5d10: 66 75 73 65 20 74 68 65 20 63 6f 6d 70 69 6c 65  fuse the compile
5d20: 72 20 2a 2f 0a 09 6f 70 74 69 6f 6e 73 20 3d 20  r */..options = 
5d30: 6f 70 74 69 6f 6e 73 3b 0a 7d 0a 0a 2f 2a 20 50  options;.}../* P
5d40: 72 6f 63 65 73 73 20 61 6e 20 48 54 54 50 20 72  rocess an HTTP r
5d50: 65 71 75 65 73 74 20 61 6e 64 20 72 65 74 75 72  equest and retur
5d60: 6e 20 74 68 65 20 70 61 74 68 20 72 65 71 75 65  n the path reque
5d70: 73 74 65 64 20 2a 2f 0a 73 74 61 74 69 63 20 73  sted */.static s
5d80: 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70  truct filed_http
5d90: 5f 72 65 71 75 65 73 74 20 2a 66 69 6c 65 64 5f  _request *filed_
5da0: 67 65 74 5f 68 74 74 70 5f 72 65 71 75 65 73 74  get_http_request
5db0: 28 46 49 4c 45 20 2a 66 70 2c 20 73 74 72 75 63  (FILE *fp, struc
5dc0: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71  t filed_http_req
5dd0: 75 65 73 74 20 2a 62 75 66 66 65 72 5f 73 74 2c  uest *buffer_st,
5de0: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70   struct filed_op
5df0: 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20  tions *options) 
5e00: 7b 0a 09 63 68 61 72 20 2a 6d 65 74 68 6f 64 2c  {..char *method,
5e10: 20 2a 70 61 74 68 3b 0a 09 63 68 61 72 20 2a 62   *path;..char *b
5e20: 75 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66  uffer, *workbuff
5e30: 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72 5f  er, *workbuffer_
5e40: 6e 65 78 74 3b 0a 09 63 68 61 72 20 2a 66 67 65  next;..char *fge
5e50: 74 73 5f 72 65 74 3b 0a 09 73 69 7a 65 5f 74 20  ts_ret;..size_t 
5e60: 62 75 66 66 65 72 5f 6c 65 6e 2c 20 70 61 74 68  buffer_len, path
5e70: 5f 6c 65 6e 3b 0a 09 6f 66 66 5f 74 20 72 61 6e  _len;..off_t ran
5e80: 67 65 5f 73 74 61 72 74 2c 20 72 61 6e 67 65 5f  ge_start, range_
5e90: 65 6e 64 2c 20 72 61 6e 67 65 5f 6c 65 6e 67 74  end, range_lengt
5ea0: 68 3b 0a 09 69 6e 74 20 72 61 6e 67 65 5f 72 65  h;..int range_re
5eb0: 71 75 65 73 74 3b 0a 09 69 6e 74 20 73 6e 70 72  quest;..int snpr
5ec0: 69 6e 74 66 5f 72 65 74 3b 0a 09 69 6e 74 20 69  intf_ret;..int i
5ed0: 3b 0a 0a 09 2f 2a 20 53 65 74 20 74 6f 20 64 65  ;.../* Set to de
5ee0: 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f 0a  fault values */.
5ef0: 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d 20 30  .range_start = 0
5f00: 3b 0a 09 72 61 6e 67 65 5f 65 6e 64 20 20 20 3d  ;..range_end   =
5f10: 20 30 3b 0a 09 72 61 6e 67 65 5f 72 65 71 75 65   0;..range_reque
5f20: 73 74 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 6c  st = 0;..range_l
5f30: 65 6e 67 74 68 20 3d 20 2d 31 3b 0a 09 62 75 66  ength = -1;..buf
5f40: 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e  fer_st->headers.
5f50: 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d 20 30  host.present = 0
5f60: 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65  ;..buffer_st->he
5f70: 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e  aders.connection
5f80: 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54   = FILED_CONNECT
5f90: 49 4f 4e 5f 43 4c 4f 53 45 3b 0a 0a 09 62 75 66  ION_CLOSE;...buf
5fa0: 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d  fer = buffer_st-
5fb0: 3e 74 6d 70 62 75 66 3b 0a 09 62 75 66 66 65 72  >tmpbuf;..buffer
5fc0: 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75  _len = sizeof(bu
5fd0: 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29  ffer_st->tmpbuf)
5fe0: 3b 0a 0a 09 66 67 65 74 73 5f 72 65 74 20 3d 20  ;...fgets_ret = 
5ff0: 66 67 65 74 73 28 62 75 66 66 65 72 2c 20 62 75  fgets(buffer, bu
6000: 66 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09  ffer_len, fp);..
6010: 69 66 20 28 66 67 65 74 73 5f 72 65 74 20 3d 3d  if (fgets_ret ==
6020: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72   NULL) {...retur
6030: 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 6d 65  n(NULL);..}...me
6040: 74 68 6f 64 20 3d 20 62 75 66 66 65 72 3b 0a 0a  thod = buffer;..
6050: 09 62 75 66 66 65 72 20 3d 20 73 74 72 63 68 72  .buffer = strchr
6060: 28 62 75 66 66 65 72 2c 20 27 20 27 29 3b 0a 09  (buffer, ' ');..
6070: 69 66 20 28 62 75 66 66 65 72 20 3d 3d 20 4e 55  if (buffer == NU
6080: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 4e  LL) {...return(N
6090: 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 2a 62 75 66 66  ULL);..}...*buff
60a0: 65 72 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66  er = '\0';..buff
60b0: 65 72 2b 2b 3b 0a 0a 09 70 61 74 68 20 3d 20 62  er++;...path = b
60c0: 75 66 66 65 72 3b 0a 0a 09 2f 2a 20 54 65 72 6d  uffer;.../* Term
60d0: 69 6e 61 74 65 20 70 61 74 68 20 63 6f 6d 70 6f  inate path compo
60e0: 6e 65 6e 74 20 2a 2f 0a 09 62 75 66 66 65 72 20  nent */..buffer 
60f0: 3d 20 73 74 72 70 62 72 6b 28 70 61 74 68 2c 20  = strpbrk(path, 
6100: 22 5c 72 5c 6e 20 22 29 3b 0a 09 69 66 20 28 62  "\r\n ");..if (b
6110: 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c 29 20 7b  uffer != NULL) {
6120: 0a 09 09 2a 62 75 66 66 65 72 20 3d 20 27 5c 30  ...*buffer = '\0
6130: 27 3b 0a 09 09 62 75 66 66 65 72 2b 2b 3b 0a 09  ';...buffer++;..
6140: 7d 0a 0a 09 2f 2a 20 57 65 20 6f 6e 6c 79 20 68  }.../* We only h
6150: 61 6e 64 6c 65 20 74 68 65 20 22 47 45 54 22 20  andle the "GET" 
6160: 61 6e 64 20 22 48 45 41 44 27 20 6d 65 74 68 6f  and "HEAD' metho
6170: 64 73 20 2a 2f 0a 09 69 66 20 28 73 74 72 63 61  ds */..if (strca
6180: 73 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 22 68  secmp(method, "h
6190: 65 61 64 22 29 20 21 3d 20 30 29 20 7b 0a 09 09  ead") != 0) {...
61a0: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 28 6d  if (strcasecmp(m
61b0: 65 74 68 6f 64 2c 20 22 67 65 74 22 29 20 21 3d  ethod, "get") !=
61c0: 20 30 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28   0) {....return(
61d0: 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a  NULL);...}..../*
61e0: 20 47 45 54 20 72 65 71 75 65 73 74 20 2a 2f 0a   GET request */.
61f0: 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 6d 65 74  ..buffer_st->met
6200: 68 6f 64 20 3d 20 46 49 4c 45 44 5f 52 45 51 55  hod = FILED_REQU
6210: 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 3b 0a  EST_METHOD_GET;.
6220: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 2f 2a 20 48  .} else {.../* H
6230: 45 41 44 20 72 65 71 75 65 73 74 20 2a 2f 0a 09  EAD request */..
6240: 09 62 75 66 66 65 72 5f 73 74 2d 3e 6d 65 74 68  .buffer_st->meth
6250: 6f 64 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45  od = FILED_REQUE
6260: 53 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 3b 0a  ST_METHOD_HEAD;.
6270: 09 7d 0a 0a 09 2f 2a 20 4e 6f 74 65 20 70 61 74  .}.../* Note pat
6280: 68 20 2a 2f 0a 09 70 61 74 68 5f 6c 65 6e 20 3d  h */..path_len =
6290: 20 73 74 72 6c 65 6e 28 70 61 74 68 29 3b 0a 09   strlen(path);..
62a0: 6d 65 6d 63 70 79 28 62 75 66 66 65 72 5f 73 74  memcpy(buffer_st
62b0: 2d 3e 70 61 74 68 2c 20 70 61 74 68 2c 20 70 61  ->path, path, pa
62c0: 74 68 5f 6c 65 6e 20 2b 20 31 29 3b 0a 0a 09 2f  th_len + 1);.../
62d0: 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 79 70 65  * Determine type
62e0: 20 6f 66 20 72 65 71 75 65 73 74 20 66 72 6f 6d   of request from
62f0: 20 70 61 74 68 20 2a 2f 0a 09 69 66 20 28 70 61   path */..if (pa
6300: 74 68 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09  th_len == 0) {..
6310: 09 62 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65  .buffer_st->type
6320: 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54   = FILED_REQUEST
6330: 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 59 3b  _TYPE_DIRECTORY;
6340: 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 69 66 20  ..} else {...if 
6350: 28 70 61 74 68 5b 70 61 74 68 5f 6c 65 6e 20 2d  (path[path_len -
6360: 20 31 5d 20 3d 3d 20 27 2f 27 29 20 7b 0a 09 09   1] == '/') {...
6370: 09 62 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65  .buffer_st->type
6380: 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54   = FILED_REQUEST
6390: 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 59 3b  _TYPE_DIRECTORY;
63a0: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 62  ...} else {....b
63b0: 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d  uffer_st->type =
63c0: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54   FILED_REQUEST_T
63d0: 59 50 45 5f 4f 54 48 45 52 3b 0a 09 09 7d 0a 09  YPE_OTHER;...}..
63e0: 7d 0a 0a 09 2f 2a 20 52 65 73 65 74 20 62 75 66  }.../* Reset buf
63f0: 66 65 72 20 66 6f 72 20 6c 61 74 65 72 20 75 73  fer for later us
6400: 65 20 2a 2f 0a 09 62 75 66 66 65 72 20 3d 20 62  e */..buffer = b
6410: 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66  uffer_st->tmpbuf
6420: 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20  ;...for (i = 0; 
6430: 69 20 3c 20 31 30 30 3b 20 69 2b 2b 29 20 7b 0a  i < 100; i++) {.
6440: 09 09 66 67 65 74 73 5f 72 65 74 20 3d 20 66 67  ..fgets_ret = fg
6450: 65 74 73 28 62 75 66 66 65 72 2c 20 62 75 66 66  ets(buffer, buff
6460: 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 09 69  er_len, fp);...i
6470: 66 20 28 66 67 65 74 73 5f 72 65 74 20 3d 3d 20  f (fgets_ret == 
6480: 4e 55 4c 4c 29 20 7b 0a 09 09 09 62 72 65 61 6b  NULL) {....break
6490: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 73 74 72  ;...}....if (str
64a0: 6e 63 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c  ncasecmp(buffer,
64b0: 20 22 52 61 6e 67 65 3a 20 22 2c 20 37 29 20 3d   "Range: ", 7) =
64c0: 3d 20 30 29 20 7b 0a 09 09 09 77 6f 72 6b 62 75  = 0) {....workbu
64d0: 66 66 65 72 20 3d 20 62 75 66 66 65 72 20 2b 20  ffer = buffer + 
64e0: 37 3b 0a 0a 09 09 09 69 66 20 28 73 74 72 6e 63  7;.....if (strnc
64f0: 61 73 65 63 6d 70 28 77 6f 72 6b 62 75 66 66 65  asecmp(workbuffe
6500: 72 2c 20 22 62 79 74 65 73 3d 22 2c 20 36 29 20  r, "bytes=", 6) 
6510: 3d 3d 20 30 29 20 7b 0a 09 09 09 09 77 6f 72 6b  == 0) {.....work
6520: 62 75 66 66 65 72 20 2b 3d 20 36 3b 0a 0a 09 09  buffer += 6;....
6530: 09 09 72 61 6e 67 65 5f 72 65 71 75 65 73 74 20  ..range_request 
6540: 3d 20 31 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f  = 1;......range_
6550: 73 74 61 72 74 20 3d 20 73 74 72 74 6f 75 6c 6c  start = strtoull
6560: 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 26 77 6f  (workbuffer, &wo
6570: 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 2c 20 31  rkbuffer_next, 1
6580: 30 29 3b 0a 0a 09 09 09 09 77 6f 72 6b 62 75 66  0);......workbuf
6590: 66 65 72 20 3d 20 77 6f 72 6b 62 75 66 66 65 72  fer = workbuffer
65a0: 5f 6e 65 78 74 3b 0a 0a 09 09 09 09 69 66 20 28  _next;......if (
65b0: 2a 77 6f 72 6b 62 75 66 66 65 72 20 3d 3d 20 27  *workbuffer == '
65c0: 2d 27 29 20 7b 0a 09 09 09 09 09 77 6f 72 6b 62  -') {......workb
65d0: 75 66 66 65 72 2b 2b 3b 0a 0a 09 09 09 09 09 69  uffer++;.......i
65e0: 66 20 28 2a 77 6f 72 6b 62 75 66 66 65 72 20 21  f (*workbuffer !
65f0: 3d 20 27 5c 72 27 20 26 26 20 2a 77 6f 72 6b 62  = '\r' && *workb
6600: 75 66 66 65 72 20 21 3d 20 27 5c 6e 27 29 20 7b  uffer != '\n') {
6610: 0a 09 09 09 09 09 09 72 61 6e 67 65 5f 65 6e 64  .......range_end
6620: 20 3d 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b   = strtoull(work
6630: 62 75 66 66 65 72 2c 20 26 77 6f 72 6b 62 75 66  buffer, &workbuf
6640: 66 65 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 09  fer_next, 10);..
6650: 09 09 09 09 7d 0a 09 09 09 09 7d 0a 09 09 09 7d  ....}.....}....}
6660: 0a 09 09 7d 20 65 6c 73 65 20 69 66 20 28 73 74  ...} else if (st
6670: 72 6e 63 61 73 65 63 6d 70 28 62 75 66 66 65 72  rncasecmp(buffer
6680: 2c 20 22 48 6f 73 74 3a 20 22 2c 20 35 29 20 3d  , "Host: ", 5) =
6690: 3d 20 30 29 20 7b 0a 09 09 09 62 75 66 66 65 72  = 0) {....buffer
66a0: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73  _st->headers.hos
66b0: 74 2e 70 72 65 73 65 6e 74 20 3d 20 31 3b 0a 0a  t.present = 1;..
66c0: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20  ...workbuffer = 
66d0: 73 74 72 70 62 72 6b 28 62 75 66 66 65 72 20 2b  strpbrk(buffer +
66e0: 20 35 2c 20 22 5c 72 5c 6e 3a 22 29 3b 0a 09 09   5, "\r\n:");...
66f0: 09 69 66 20 28 77 6f 72 6b 62 75 66 66 65 72 20  .if (workbuffer 
6700: 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 09 2a  != NULL) {.....*
6710: 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 27 5c 30  workbuffer = '\0
6720: 27 3b 0a 09 09 09 7d 0a 0a 09 09 09 77 6f 72 6b  ';....}.....work
6730: 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 20  buffer = buffer 
6740: 2b 20 35 3b 0a 09 09 09 77 68 69 6c 65 20 28 2a  + 5;....while (*
6750: 77 6f 72 6b 62 75 66 66 65 72 20 3d 3d 20 27 20  workbuffer == ' 
6760: 27 29 20 7b 0a 09 09 09 09 77 6f 72 6b 62 75 66  ') {.....workbuf
6770: 66 65 72 2b 2b 3b 0a 09 09 09 7d 0a 0a 09 09 09  fer++;....}.....
6780: 73 74 72 63 70 79 28 62 75 66 66 65 72 5f 73 74  strcpy(buffer_st
6790: 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 68  ->headers.host.h
67a0: 6f 73 74 2c 20 77 6f 72 6b 62 75 66 66 65 72 29  ost, workbuffer)
67b0: 3b 0a 09 09 7d 20 65 6c 73 65 20 69 66 20 28 73  ;...} else if (s
67c0: 74 72 6e 63 61 73 65 63 6d 70 28 62 75 66 66 65  trncasecmp(buffe
67d0: 72 2c 20 22 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20  r, "Connection: 
67e0: 4b 65 65 70 2d 41 6c 69 76 65 22 2c 20 32 32 29  Keep-Alive", 22)
67f0: 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 75 66 66   == 0) {....buff
6800: 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 63  er_st->headers.c
6810: 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 46 49 4c 45  onnection = FILE
6820: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45  D_CONNECTION_KEE
6830: 50 5f 41 4c 49 56 45 3b 0a 09 09 7d 0a 0a 09 09  P_ALIVE;...}....
6840: 69 66 20 28 6d 65 6d 63 6d 70 28 62 75 66 66 65  if (memcmp(buffe
6850: 72 2c 20 22 5c 72 5c 6e 22 2c 20 32 29 20 3d 3d  r, "\r\n", 2) ==
6860: 20 30 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a   0) {....break;.
6870: 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 65 74 65  ..}..}.../* Dete
6880: 72 6d 69 6e 65 20 72 61 6e 67 65 20 2a 2f 0a 09  rmine range */..
6890: 69 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 21 3d  if (range_end !=
68a0: 20 30 29 20 7b 0a 09 09 69 66 20 28 72 61 6e 67   0) {...if (rang
68b0: 65 5f 65 6e 64 20 3c 3d 20 72 61 6e 67 65 5f 73  e_end <= range_s
68c0: 74 61 72 74 29 20 7b 0a 09 09 09 72 65 74 75 72  tart) {....retur
68d0: 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09  n(NULL);...}....
68e0: 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d 20 72  range_length = r
68f0: 61 6e 67 65 5f 65 6e 64 20 2d 20 72 61 6e 67 65  ange_end - range
6900: 5f 73 74 61 72 74 3b 0a 0a 09 09 66 69 6c 65 64  _start;....filed
6910: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22  _log_msg_debug("
6920: 43 6f 6d 70 75 74 69 6e 67 20 6c 65 6e 67 74 68  Computing length
6930: 20 70 61 72 61 6d 65 74 65 72 3a 20 25 6c 6c 75   parameter: %llu
6940: 20 3d 20 25 6c 6c 75 20 2d 20 25 6c 6c 75 22 2c   = %llu - %llu",
6950: 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f  ....(unsigned lo
6960: 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 6c  ng long) range_l
6970: 65 6e 67 74 68 2c 0a 09 09 09 28 75 6e 73 69 67  ength,....(unsig
6980: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72  ned long long) r
6990: 61 6e 67 65 5f 65 6e 64 2c 0a 09 09 09 28 75 6e  ange_end,....(un
69a0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
69b0: 29 20 72 61 6e 67 65 5f 73 74 61 72 74 0a 09 09  ) range_start...
69c0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 46 69 6c 6c 20  );..}.../* Fill 
69d0: 75 70 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  up structure to 
69e0: 72 65 74 75 72 6e 20 2a 2f 0a 09 62 75 66 66 65  return */..buffe
69f0: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  r_st->headers.ra
6a00: 6e 67 65 2e 70 72 65 73 65 6e 74 20 3d 20 72 61  nge.present = ra
6a10: 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a 09 62 75  nge_request;..bu
6a20: 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73  ffer_st->headers
6a30: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 20 3d  .range.offset  =
6a40: 20 72 61 6e 67 65 5f 73 74 61 72 74 3b 0a 09 62   range_start;..b
6a50: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72  uffer_st->header
6a60: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 20  s.range.length  
6a70: 3d 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a  = range_length;.
6a80: 0a 09 2f 2a 20 49 66 20 76 68 6f 73 74 73 20 61  ../* If vhosts a
6a90: 72 65 20 65 6e 61 62 6c 65 64 2c 20 63 6f 6d 70  re enabled, comp
6aa0: 75 74 65 20 6e 65 77 20 70 61 74 68 20 2a 2f 0a  ute new path */.
6ab0: 09 69 66 20 28 6f 70 74 69 6f 6e 73 2d 3e 76 68  .if (options->vh
6ac0: 6f 73 74 73 5f 65 6e 61 62 6c 65 64 29 20 7b 0a  osts_enabled) {.
6ad0: 09 09 69 66 20 28 62 75 66 66 65 72 5f 73 74 2d  ..if (buffer_st-
6ae0: 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72  >headers.host.pr
6af0: 65 73 65 6e 74 20 3d 3d 20 31 29 20 7b 0a 09 09  esent == 1) {...
6b00: 09 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 72  .buffer = buffer
6b10: 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a 09 09 09  _st->tmpbuf;....
6b20: 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 69 7a  buffer_len = siz
6b30: 65 6f 66 28 62 75 66 66 65 72 5f 73 74 2d 3e 74  eof(buffer_st->t
6b40: 6d 70 62 75 66 29 3b 0a 0a 09 09 09 73 6e 70 72  mpbuf);.....snpr
6b50: 69 6e 74 66 5f 72 65 74 20 3d 20 73 6e 70 72 69  intf_ret = snpri
6b60: 6e 74 66 28 62 75 66 66 65 72 2c 20 62 75 66 66  ntf(buffer, buff
6b70: 65 72 5f 6c 65 6e 2c 20 22 2f 25 73 25 73 25 73  er_len, "/%s%s%s
6b80: 22 2c 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74  ",.....buffer_st
6b90: 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 68  ->headers.host.h
6ba0: 6f 73 74 2c 0a 09 09 09 09 62 75 66 66 65 72 5f  ost,.....buffer_
6bb0: 73 74 2d 3e 70 61 74 68 5b 30 5d 20 3d 3d 20 27  st->path[0] == '
6bc0: 2f 27 20 3f 20 22 22 20 3a 20 22 2f 22 2c 0a 09  /' ? "" : "/",..
6bd0: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 70 61  ...buffer_st->pa
6be0: 74 68 0a 09 09 09 29 3b 0a 09 09 09 69 66 20 28  th....);....if (
6bf0: 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3e 3d 20  snprintf_ret >= 
6c00: 30 29 20 7b 0a 09 09 09 09 69 66 20 28 28 28 75  0) {.....if (((u
6c10: 6e 73 69 67 6e 65 64 20 69 6e 74 29 20 73 6e 70  nsigned int) snp
6c20: 72 69 6e 74 66 5f 72 65 74 29 20 3c 20 62 75 66  rintf_ret) < buf
6c30: 66 65 72 5f 6c 65 6e 29 20 7b 0a 09 09 09 09 09  fer_len) {......
6c40: 73 74 72 63 70 79 28 62 75 66 66 65 72 5f 73 74  strcpy(buffer_st
6c50: 2d 3e 70 61 74 68 2c 20 62 75 66 66 65 72 29 3b  ->path, buffer);
6c60: 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a  .....}....}...}.
6c70: 09 7d 0a 0a 09 72 65 74 75 72 6e 28 62 75 66 66  .}...return(buff
6c80: 65 72 5f 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 52 65  er_st);.}../* Re
6c90: 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 70 61  turn an error pa
6ca0: 67 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ge */.static voi
6cb0: 64 20 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61  d filed_error_pa
6cc0: 67 65 28 46 49 4c 45 20 2a 66 70 2c 20 63 6f 6e  ge(FILE *fp, con
6cd0: 73 74 20 63 68 61 72 20 2a 64 61 74 65 5f 63 75  st char *date_cu
6ce0: 72 72 65 6e 74 2c 20 69 6e 74 20 65 72 72 6f 72  rrent, int error
6cf0: 5f 6e 75 6d 62 65 72 2c 20 69 6e 74 20 6d 65 74  _number, int met
6d00: 68 6f 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  hod, const char 
6d10: 2a 72 65 61 73 6f 6e 2c 20 73 74 72 75 63 74 20  *reason, struct 
6d20: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20  filed_log_entry 
6d30: 2a 6c 6f 67 29 20 7b 0a 09 63 68 61 72 20 2a 65  *log) {..char *e
6d40: 72 72 6f 72 5f 73 74 72 69 6e 67 20 3d 20 22 3c  rror_string = "<
6d50: 68 74 6d 6c 3e 3c 68 65 61 64 3e 3c 74 69 74 6c  html><head><titl
6d60: 65 3e 45 52 52 4f 52 3c 2f 74 69 74 6c 65 3e 3c  e>ERROR</title><
6d70: 2f 68 65 61 64 3e 3c 62 6f 64 79 3e 55 6e 61 62  /head><body>Unab
6d80: 6c 65 20 74 6f 20 70 72 6f 63 65 73 73 20 72 65  le to process re
6d90: 71 75 65 73 74 3c 2f 62 6f 64 79 3e 3c 2f 68 74  quest</body></ht
6da0: 6d 6c 3e 22 3b 0a 0a 09 66 70 72 69 6e 74 66 28  ml>";...fprintf(
6db0: 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 25 69  fp, "HTTP/1.1 %i
6dc0: 20 4e 6f 74 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a   Not OK\r\nDate:
6dd0: 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66   %s\r\nServer: f
6de0: 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64  iled\r\nLast-Mod
6df0: 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e  ified: %s\r\nCon
6e00: 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c  tent-Length: %ll
6e10: 75 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 70  u\r\nContent-Typ
6e20: 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74  e: %s\r\nConnect
6e30: 69 6f 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 5c 72  ion: close\r\n\r
6e40: 5c 6e 22 2c 0a 09 09 65 72 72 6f 72 5f 6e 75 6d  \n",...error_num
6e50: 62 65 72 2c 0a 09 09 64 61 74 65 5f 63 75 72 72  ber,...date_curr
6e60: 65 6e 74 2c 0a 09 09 64 61 74 65 5f 63 75 72 72  ent,...date_curr
6e70: 65 6e 74 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64  ent,...(unsigned
6e80: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 73 74 72 6c   long long) strl
6e90: 65 6e 28 65 72 72 6f 72 5f 73 74 72 69 6e 67 29  en(error_string)
6ea0: 2c 0a 09 09 22 74 65 78 74 2f 68 74 6d 6c 22 0a  ,..."text/html".
6eb0: 09 29 3b 0a 0a 09 2f 2a 20 73 69 6c 65 6e 63 65  .);.../* silence
6ec0: 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f   error string fo
6ed0: 72 20 48 45 41 44 20 72 65 71 75 65 73 74 73 20  r HEAD requests 
6ee0: 2a 2f 0a 09 69 66 20 28 6d 65 74 68 6f 64 20 21  */..if (method !
6ef0: 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f  = FILED_REQUEST_
6f00: 4d 45 54 48 4f 44 5f 48 45 41 44 29 20 7b 0a 09  METHOD_HEAD) {..
6f10: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 25 73  .fprintf(fp, "%s
6f20: 22 2c 20 65 72 72 6f 72 5f 73 74 72 69 6e 67 29  ", error_string)
6f30: 3b 0a 09 7d 0a 0a 09 2f 2a 20 4c 6f 67 20 65 72  ;..}.../* Log er
6f40: 72 6f 72 20 2a 2f 0a 09 2f 2a 2a 20 72 65 61 73  ror */../** reas
6f50: 6f 6e 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f  on must point to
6f60: 20 61 20 67 6c 6f 62 61 6c 6c 79 20 61 6c 6c 6f   a globally allo
6f70: 63 61 74 65 64 20 76 61 6c 75 65 20 2a 2a 2f 0a  cated value **/.
6f80: 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 72  .log->reason = r
6f90: 65 61 73 6f 6e 3b 0a 09 6c 6f 67 2d 3e 68 74 74  eason;..log->htt
6fa0: 70 5f 63 6f 64 65 20 3d 20 65 72 72 6f 72 5f 6e  p_code = error_n
6fb0: 75 6d 62 65 72 3b 0a 0a 09 66 69 6c 65 64 5f 6c  umber;...filed_l
6fc0: 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a  og_entry(log);..
6fd0: 09 2f 2a 20 43 6c 6f 73 65 20 63 6f 6e 6e 65 63  ./* Close connec
6fe0: 74 69 6f 6e 20 2a 2f 0a 09 66 69 6c 65 64 5f 73  tion */..filed_s
6ff0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f  ockettimeout_clo
7000: 73 65 28 66 69 6c 65 6e 6f 28 66 70 29 29 3b 0a  se(fileno(fp));.
7010: 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09  ..fclose(fp);...
7020: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65  return;.}../* Re
7030: 74 75 72 6e 20 61 20 72 65 64 69 72 65 63 74 20  turn a redirect 
7040: 74 6f 20 69 6e 64 65 78 2e 68 74 6d 6c 20 2a 2f  to index.html */
7050: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
7060: 65 64 5f 72 65 64 69 72 65 63 74 5f 69 6e 64 65  ed_redirect_inde
7070: 78 28 46 49 4c 45 20 2a 66 70 2c 20 63 6f 6e 73  x(FILE *fp, cons
7080: 74 20 63 68 61 72 20 2a 64 61 74 65 5f 63 75 72  t char *date_cur
7090: 72 65 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  rent, const char
70a0: 20 2a 70 61 74 68 2c 20 73 74 72 75 63 74 20 66   *path, struct f
70b0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a  iled_log_entry *
70c0: 6c 6f 67 29 20 7b 0a 09 69 6e 74 20 68 74 74 70  log) {..int http
70d0: 5f 63 6f 64 65 20 3d 20 33 30 31 3b 0a 09 66 70  _code = 301;..fp
70e0: 72 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f  rintf(fp, "HTTP/
70f0: 31 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74  1.1 %i OK\r\nDat
7100: 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a  e: %s\r\nServer:
7110: 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d   filed\r\nLast-M
7120: 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43  odified: %s\r\nC
7130: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 30  ontent-Length: 0
7140: 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20  \r\nConnection: 
7150: 63 6c 6f 73 65 5c 72 5c 6e 4c 6f 63 61 74 69 6f  close\r\nLocatio
7160: 6e 3a 20 25 73 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a  n: %s\r\n\r\n",.
7170: 09 09 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64  ..http_code,...d
7180: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64  ate_current,...d
7190: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 22  ate_current,..."
71a0: 69 6e 64 65 78 2e 68 74 6d 6c 22 0a 09 29 3b 0a  index.html"..);.
71b0: 0a 09 2f 2a 20 4c 6f 67 20 72 65 64 69 72 65 63  ../* Log redirec
71c0: 74 20 2a 2f 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f  t */..log->reaso
71d0: 6e 20 3d 20 22 72 65 64 69 72 65 63 74 22 3b 0a  n = "redirect";.
71e0: 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20  .log->http_code 
71f0: 3d 20 68 74 74 70 5f 63 6f 64 65 3b 0a 0a 09 66  = http_code;...f
7200: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c  iled_log_entry(l
7210: 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20  og);.../* Close 
7220: 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 66  connection */..f
7230: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
7240: 75 74 5f 63 6c 6f 73 65 28 66 69 6c 65 6e 6f 28  ut_close(fileno(
7250: 66 70 29 29 3b 0a 0a 09 66 63 6c 6f 73 65 28 66  fp));...fclose(f
7260: 70 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 0a 09  p);...return;...
7270: 2f 2a 20 43 75 72 72 65 6e 74 6c 79 20 75 6e 75  /* Currently unu
7280: 73 65 64 3a 20 70 61 74 68 20 2a 2f 0a 09 70 61  sed: path */..pa
7290: 74 68 20 3d 20 70 61 74 68 3b 0a 7d 0a 0a 2f 2a  th = path;.}../*
72a0: 20 43 6f 6e 76 65 72 74 20 61 6e 20 65 6e 75 6d   Convert an enum
72b0: 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
72c0: 65 20 22 43 6f 6e 6e 65 63 74 69 6f 6e 22 20 68  e "Connection" h
72d0: 65 61 64 65 72 20 76 61 6c 75 65 20 74 6f 20 61  eader value to a
72e0: 20 73 74 72 69 6e 67 20 2a 2f 0a 73 74 61 74 69   string */.stati
72f0: 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69  c const char *fi
7300: 6c 65 64 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73  led_connection_s
7310: 74 72 28 69 6e 74 20 63 6f 6e 6e 65 63 74 69 6f  tr(int connectio
7320: 6e 5f 76 61 6c 75 65 29 20 7b 0a 09 73 77 69 74  n_value) {..swit
7330: 63 68 20 28 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76  ch (connection_v
7340: 61 6c 75 65 29 20 7b 0a 09 09 63 61 73 65 20 46  alue) {...case F
7350: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f  ILED_CONNECTION_
7360: 43 4c 4f 53 45 3a 0a 09 09 09 72 65 74 75 72 6e  CLOSE:....return
7370: 28 22 63 6c 6f 73 65 22 29 3b 0a 09 09 63 61 73  ("close");...cas
7380: 65 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49  e FILED_CONNECTI
7390: 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 3a 0a 09  ON_KEEP_ALIVE:..
73a0: 09 09 72 65 74 75 72 6e 28 22 6b 65 65 70 2d 61  ..return("keep-a
73b0: 6c 69 76 65 22 29 3b 0a 09 7d 0a 0a 09 72 65 74  live");..}...ret
73c0: 75 72 6e 28 22 63 6c 6f 73 65 22 29 3b 0a 7d 0a  urn("close");.}.
73d0: 0a 2f 2a 20 48 61 6e 64 6c 65 20 61 20 73 69 6e  ./* Handle a sin
73e0: 67 6c 65 20 72 65 71 75 65 73 74 20 66 72 6f 6d  gle request from
73f0: 20 61 20 63 6c 69 65 6e 74 20 2a 2f 0a 73 74 61   a client */.sta
7400: 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 68 61  tic int filed_ha
7410: 6e 64 6c 65 5f 63 6c 69 65 6e 74 28 69 6e 74 20  ndle_client(int 
7420: 66 64 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64  fd, struct filed
7430: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 72  _http_request *r
7440: 65 71 75 65 73 74 2c 20 73 74 72 75 63 74 20 66  equest, struct f
7450: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a  iled_log_entry *
7460: 6c 6f 67 2c 20 73 74 72 75 63 74 20 66 69 6c 65  log, struct file
7470: 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f  d_options *optio
7480: 6e 73 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69  ns) {..struct fi
7490: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69  led_fileinfo *fi
74a0: 6c 65 69 6e 66 6f 3b 0a 09 73 73 69 7a 65 5f 74  leinfo;..ssize_t
74b0: 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09   sendfile_ret;..
74c0: 73 69 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f  size_t sendfile_
74d0: 73 69 7a 65 3b 0a 09 6f 66 66 5f 74 20 73 65 6e  size;..off_t sen
74e0: 64 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65  dfile_offset, se
74f0: 6e 64 66 69 6c 65 5f 73 65 6e 74 2c 20 73 65 6e  ndfile_sent, sen
7500: 64 66 69 6c 65 5f 6c 65 6e 3b 0a 09 63 68 61 72  dfile_len;..char
7510: 20 2a 70 61 74 68 3b 0a 09 63 68 61 72 20 2a 64   *path;..char *d
7520: 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 64 61 74  ate_current, dat
7530: 65 5f 63 75 72 72 65 6e 74 5f 62 5b 36 34 5d 3b  e_current_b[64];
7540: 0a 09 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b  ..int http_code;
7550: 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 2f 2a  ..FILE *fp;.../*
7560: 20 44 65 74 65 72 6d 69 6e 65 20 63 75 72 72 65   Determine curre
7570: 6e 74 20 74 69 6d 65 20 2a 2f 0a 09 64 61 74 65  nt time */..date
7580: 5f 63 75 72 72 65 6e 74 20 3d 20 66 69 6c 65 64  _current = filed
7590: 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 64 61 74  _format_time(dat
75a0: 65 5f 63 75 72 72 65 6e 74 5f 62 2c 20 73 69 7a  e_current_b, siz
75b0: 65 6f 66 28 64 61 74 65 5f 63 75 72 72 65 6e 74  eof(date_current
75c0: 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 29  _b), time(NULL))
75d0: 3b 0a 0a 09 2f 2a 20 4f 70 65 6e 20 73 6f 63 6b  ;.../* Open sock
75e0: 65 74 20 61 73 20 41 4e 53 49 20 49 2f 4f 20 66  et as ANSI I/O f
75f0: 6f 72 20 65 61 73 65 20 6f 66 20 75 73 65 20 2a  or ease of use *
7600: 2f 0a 09 66 70 20 3d 20 66 64 6f 70 65 6e 28 66  /..fp = fdopen(f
7610: 64 2c 20 22 77 2b 62 22 29 3b 0a 09 69 66 20 28  d, "w+b");..if (
7620: 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09  fp == NULL) {...
7630: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
7640: 6f 75 74 5f 63 6c 6f 73 65 28 66 64 29 3b 0a 0a  out_close(fd);..
7650: 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09  ..close(fd);....
7660: 6c 6f 67 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d  log->buffer[0] =
7670: 20 27 5c 30 27 3b 0a 09 09 6c 6f 67 2d 3e 68 74   '\0';...log->ht
7680: 74 70 5f 63 6f 64 65 20 3d 20 2d 31 3b 0a 09 09  tp_code = -1;...
7690: 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 66  log->reason = "f
76a0: 64 6f 70 65 6e 5f 66 61 69 6c 65 64 22 3b 0a 0a  dopen_failed";..
76b0: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72  ..filed_log_entr
76c0: 79 28 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72  y(log);....retur
76d0: 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49  n(FILED_CONNECTI
76e0: 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09  ON_CLOSE);..}...
76f0: 72 65 71 75 65 73 74 20 3d 20 66 69 6c 65 64 5f  request = filed_
7700: 67 65 74 5f 68 74 74 70 5f 72 65 71 75 65 73 74  get_http_request
7710: 28 66 70 2c 20 72 65 71 75 65 73 74 2c 20 6f 70  (fp, request, op
7720: 74 69 6f 6e 73 29 3b 0a 0a 09 69 66 20 28 72 65  tions);...if (re
7730: 71 75 65 73 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b  quest == NULL) {
7740: 0a 09 09 6c 6f 67 2d 3e 62 75 66 66 65 72 5b 30  ...log->buffer[0
7750: 5d 20 3d 20 27 5c 30 27 3b 0a 0a 09 09 66 69 6c  ] = '\0';....fil
7760: 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70  ed_error_page(fp
7770: 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20  , date_current, 
7780: 35 30 30 2c 20 46 49 4c 45 44 5f 52 45 51 55 45  500, FILED_REQUE
7790: 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 2c 20 22  ST_METHOD_GET, "
77a0: 66 6f 72 6d 61 74 22 2c 20 6c 6f 67 29 3b 0a 0a  format", log);..
77b0: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43  ..return(FILED_C
77c0: 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29  ONNECTION_CLOSE)
77d0: 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63  ;..}...filed_soc
77e0: 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65  kettimeout_proce
77f0: 73 73 69 6e 67 5f 73 74 61 72 74 28 66 64 29 3b  ssing_start(fd);
7800: 0a 0a 09 70 61 74 68 20 3d 20 72 65 71 75 65 73  ...path = reques
7810: 74 2d 3e 70 61 74 68 3b 0a 09 73 74 72 63 70 79  t->path;..strcpy
7820: 28 6c 6f 67 2d 3e 62 75 66 66 65 72 2c 20 70 61  (log->buffer, pa
7830: 74 68 29 3b 0a 09 6c 6f 67 2d 3e 6d 65 74 68 6f  th);..log->metho
7840: 64 20 3d 20 72 65 71 75 65 73 74 2d 3e 6d 65 74  d = request->met
7850: 68 6f 64 3b 0a 0a 09 2f 2a 20 49 66 20 74 68 65  hod;.../* If the
7860: 20 72 65 71 75 65 73 74 65 64 20 70 61 74 68 20   requested path 
7870: 69 73 20 61 20 64 69 72 65 63 74 6f 72 79 2c 20  is a directory, 
7880: 72 65 64 69 72 65 63 74 20 74 6f 20 69 6e 64 65  redirect to inde
7890: 78 20 70 61 67 65 20 2a 2f 0a 09 69 66 20 28 72  x page */..if (r
78a0: 65 71 75 65 73 74 2d 3e 74 79 70 65 20 3d 3d 20  equest->type == 
78b0: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59  FILED_REQUEST_TY
78c0: 50 45 5f 44 49 52 45 43 54 4f 52 59 29 20 7b 0a  PE_DIRECTORY) {.
78d0: 09 09 66 69 6c 65 64 5f 72 65 64 69 72 65 63 74  ..filed_redirect
78e0: 5f 69 6e 64 65 78 28 66 70 2c 20 64 61 74 65 5f  _index(fp, date_
78f0: 63 75 72 72 65 6e 74 2c 20 70 61 74 68 2c 20 6c  current, path, l
7900: 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46  og);....return(F
7910: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f  ILED_CONNECTION_
7920: 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c  CLOSE);..}...fil
7930: 65 69 6e 66 6f 20 3d 20 66 69 6c 65 64 5f 6f 70  einfo = filed_op
7940: 65 6e 5f 66 69 6c 65 28 70 61 74 68 2c 20 26 72  en_file(path, &r
7950: 65 71 75 65 73 74 2d 3e 66 69 6c 65 69 6e 66 6f  equest->fileinfo
7960: 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a 09 69 66 20  , options);..if 
7970: 28 66 69 6c 65 69 6e 66 6f 20 3d 3d 20 4e 55 4c  (fileinfo == NUL
7980: 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65 72 72  L) {...filed_err
7990: 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65  or_page(fp, date
79a0: 5f 63 75 72 72 65 6e 74 2c 20 34 30 34 2c 20 72  _current, 404, r
79b0: 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c 20  equest->method, 
79c0: 22 6f 70 65 6e 5f 66 61 69 6c 65 64 22 2c 20 6c  "open_failed", l
79d0: 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46  og);....return(F
79e0: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f  ILED_CONNECTION_
79f0: 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 69 66 20  CLOSE);..}...if 
7a00: 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72  (request->header
7a10: 73 2e 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 29  s.range.present)
7a20: 20 7b 0a 09 09 69 66 20 28 72 65 71 75 65 73 74   {...if (request
7a30: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
7a40: 6f 66 66 73 65 74 20 21 3d 20 30 20 7c 7c 20 72  offset != 0 || r
7a50: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
7a60: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3e 3d 20  range.length >= 
7a70: 30 29 20 7b 0a 09 09 09 69 66 20 28 72 65 71 75  0) {....if (requ
7a80: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
7a90: 67 65 2e 6f 66 66 73 65 74 20 3e 3d 20 66 69 6c  ge.offset >= fil
7aa0: 65 69 6e 66 6f 2d 3e 6c 65 6e 29 20 7b 0a 09 09  einfo->len) {...
7ab0: 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61  ..filed_error_pa
7ac0: 67 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72  ge(fp, date_curr
7ad0: 65 6e 74 2c 20 34 31 36 2c 20 72 65 71 75 65 73  ent, 416, reques
7ae0: 74 2d 3e 6d 65 74 68 6f 64 2c 20 22 72 61 6e 67  t->method, "rang
7af0: 65 5f 69 6e 76 61 6c 69 64 22 2c 20 6c 6f 67 29  e_invalid", log)
7b00: 3b 0a 0a 09 09 09 09 63 6c 6f 73 65 28 66 69 6c  ;......close(fil
7b10: 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 09 09  einfo->fd);.....
7b20: 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f  .return(FILED_CO
7b30: 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b  NNECTION_CLOSE);
7b40: 0a 09 09 09 7d 0a 0a 09 09 09 69 66 20 28 72 65  ....}.....if (re
7b50: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
7b60: 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 3d 20 28  ange.length == (
7b70: 28 6f 66 66 5f 74 29 20 2d 31 29 29 20 7b 0a 09  (off_t) -1)) {..
7b80: 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  ...filed_log_msg
7b90: 5f 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 6e  _debug("Computin
7ba0: 67 20 6c 65 6e 67 74 68 20 74 6f 20 66 69 74 20  g length to fit 
7bb0: 69 6e 20 62 6f 75 6e 64 73 3a 20 66 69 6c 65 69  in bounds: filei
7bc0: 6e 66 6f 2d 3e 6c 65 6e 20 3d 20 25 6c 6c 75 2c  nfo->len = %llu,
7bd0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72   request->header
7be0: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d  s.range.offset =
7bf0: 20 25 6c 6c 75 22 2c 0a 09 09 09 09 09 28 75 6e   %llu",......(un
7c00: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
7c10: 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 2c  ) fileinfo->len,
7c20: 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ......(unsigned 
7c30: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65  long long) reque
7c40: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
7c50: 65 2e 6f 66 66 73 65 74 0a 09 09 09 09 29 3b 0a  e.offset.....);.
7c60: 0a 09 09 09 09 72 65 71 75 65 73 74 2d 3e 68 65  .....request->he
7c70: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
7c80: 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c  th = fileinfo->l
7c90: 65 6e 20 2d 20 72 65 71 75 65 73 74 2d 3e 68 65  en - request->he
7ca0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73  aders.range.offs
7cb0: 65 74 3b 0a 09 09 09 7d 0a 0a 09 09 09 66 69 6c  et;....}.....fil
7cc0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67  ed_log_msg_debug
7cd0: 28 22 50 61 72 74 69 61 6c 20 72 65 71 75 65 73  ("Partial reques
7ce0: 74 2c 20 73 74 61 72 74 69 6e 67 20 61 74 3a 20  t, starting at: 
7cf0: 25 6c 6c 75 20 61 6e 64 20 72 75 6e 6e 69 6e 67  %llu and running
7d00: 20 66 6f 72 20 25 6c 6c 69 20 62 79 74 65 73 22   for %lli bytes"
7d10: 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ,.....(unsigned 
7d20: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65  long long) reque
7d30: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
7d40: 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 09 28 6c  e.offset,.....(l
7d50: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73  ong long) reques
7d60: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65  t->headers.range
7d70: 2e 6c 65 6e 67 74 68 0a 09 09 09 29 3b 0a 0a 09  .length....);...
7d80: 09 7d 0a 0a 09 09 68 74 74 70 5f 63 6f 64 65 20  .}....http_code 
7d90: 3d 20 32 30 36 3b 0a 09 7d 20 65 6c 73 65 20 7b  = 206;..} else {
7da0: 0a 09 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 32  ...http_code = 2
7db0: 30 30 3b 0a 0a 09 09 2f 2a 20 43 6f 6d 70 75 74  00;..../* Comput
7dc0: 65 20 66 61 6b 65 20 72 61 6e 67 65 20 70 61 72  e fake range par
7dd0: 61 6d 65 74 65 72 73 20 74 68 61 74 20 69 6e 63  ameters that inc
7de0: 6c 75 64 65 73 20 74 68 65 20 65 6e 74 69 72 65  ludes the entire
7df0: 20 66 69 6c 65 20 2a 2f 0a 09 09 72 65 71 75 65   file */...reque
7e00: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
7e10: 65 2e 6f 66 66 73 65 74 20 3d 20 30 3b 0a 09 09  e.offset = 0;...
7e20: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
7e30: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20  .range.length = 
7e40: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 09  fileinfo->len;..
7e50: 7d 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20  }...fprintf(fp, 
7e60: 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c  "HTTP/1.1 %i OK\
7e70: 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53  r\nDate: %s\r\nS
7e80: 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e  erver: filed\r\n
7e90: 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25  Last-Modified: %
7ea0: 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e  s\r\nContent-Len
7eb0: 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 41 63 63  gth: %llu\r\nAcc
7ec0: 65 70 74 2d 52 61 6e 67 65 73 3a 20 62 79 74 65  ept-Ranges: byte
7ed0: 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 70  s\r\nContent-Typ
7ee0: 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74  e: %s\r\nConnect
7ef0: 69 6f 6e 3a 20 25 73 5c 72 5c 6e 45 54 61 67 3a  ion: %s\r\nETag:
7f00: 20 5c 22 25 73 5c 22 5c 72 5c 6e 22 2c 0a 09 09   \"%s\"\r\n",...
7f10: 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64 61 74  http_code,...dat
7f20: 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 66 69 6c  e_current,...fil
7f30: 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d 6f 64 2c 0a  einfo->lastmod,.
7f40: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
7f50: 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e   long) request->
7f60: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65  headers.range.le
7f70: 6e 67 74 68 2c 0a 09 09 66 69 6c 65 69 6e 66 6f  ngth,...fileinfo
7f80: 2d 3e 74 79 70 65 2c 0a 09 09 66 69 6c 65 64 5f  ->type,...filed_
7f90: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 28 72  connection_str(r
7fa0: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
7fb0: 63 6f 6e 6e 65 63 74 69 6f 6e 29 2c 0a 09 09 66  connection),...f
7fc0: 69 6c 65 69 6e 66 6f 2d 3e 65 74 61 67 0a 09 29  ileinfo->etag..)
7fd0: 3b 0a 0a 09 69 66 20 28 68 74 74 70 5f 63 6f 64  ;...if (http_cod
7fe0: 65 20 3d 3d 20 32 30 36 29 20 7b 0a 09 09 66 70  e == 206) {...fp
7ff0: 72 69 6e 74 66 28 66 70 2c 20 22 43 6f 6e 74 65  rintf(fp, "Conte
8000: 6e 74 2d 52 61 6e 67 65 3a 20 62 79 74 65 73 20  nt-Range: bytes 
8010: 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c 6c 75 5c 72  %llu-%llu/%llu\r
8020: 5c 6e 22 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65  \n",....(unsigne
8030: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71  d long long) req
8040: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  uest->headers.ra
8050: 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 28  nge.offset,....(
8060: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
8070: 6e 67 29 20 28 72 65 71 75 65 73 74 2d 3e 68 65  ng) (request->he
8080: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73  aders.range.offs
8090: 65 74 20 2b 20 72 65 71 75 65 73 74 2d 3e 68 65  et + request->he
80a0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
80b0: 74 68 20 2d 20 31 29 2c 0a 09 09 09 28 75 6e 73  th - 1),....(uns
80c0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
80d0: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 0a 09   fileinfo->len..
80e0: 09 29 3b 0a 09 7d 0a 09 66 70 72 69 6e 74 66 28  .);..}..fprintf(
80f0: 66 70 2c 20 22 5c 72 5c 6e 22 29 3b 0a 09 66 66  fp, "\r\n");..ff
8100: 6c 75 73 68 28 66 70 29 3b 0a 0a 09 6c 6f 67 2d  lush(fp);...log-
8110: 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 68 74 74  >http_code = htt
8120: 70 5f 63 6f 64 65 3b 0a 09 6c 6f 67 2d 3e 72 65  p_code;..log->re
8130: 61 73 6f 6e 20 3d 20 22 4f 4b 22 3b 0a 09 6c 6f  ason = "OK";..lo
8140: 67 2d 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 74  g->starttime = t
8150: 69 6d 65 28 4e 55 4c 4c 29 3b 0a 09 6c 6f 67 2d  ime(NULL);..log-
8160: 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 72 65  >req_offset = re
8170: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
8180: 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 6c 6f  ange.offset;..lo
8190: 67 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20 3d 20  g->req_length = 
81a0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
81b0: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09  .range.length;..
81c0: 6c 6f 67 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 68  log->file_length
81d0: 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e   = fileinfo->len
81e0: 3b 0a 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f  ;..#ifdef FILED_
81f0: 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 69  NONBLOCK_HTTP..i
8200: 6e 74 20 73 6f 63 6b 65 74 5f 66 6c 61 67 73 3b  nt socket_flags;
8210: 0a 09 66 64 5f 73 65 74 20 72 66 64 2c 20 77 66  ..fd_set rfd, wf
8220: 64 3b 0a 09 63 68 61 72 20 73 69 6e 6b 62 75 66  d;..char sinkbuf
8230: 5b 38 31 39 32 5d 3b 0a 09 73 73 69 7a 65 5f 74  [8192];..ssize_t
8240: 20 72 65 61 64 5f 72 65 74 3b 0a 0a 09 46 44 5f   read_ret;...FD_
8250: 5a 45 52 4f 28 26 72 66 64 29 3b 0a 09 46 44 5f  ZERO(&rfd);..FD_
8260: 5a 45 52 4f 28 26 77 66 64 29 3b 0a 09 46 44 5f  ZERO(&wfd);..FD_
8270: 53 45 54 28 66 64 2c 20 26 72 66 64 29 3b 0a 09  SET(fd, &rfd);..
8280: 46 44 5f 53 45 54 28 66 64 2c 20 26 77 66 64 29  FD_SET(fd, &wfd)
8290: 3b 0a 0a 09 73 6f 63 6b 65 74 5f 66 6c 61 67 73  ;...socket_flags
82a0: 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47   = fcntl(fd, F_G
82b0: 45 54 46 4c 29 3b 0a 09 66 63 6e 74 6c 28 66 64  ETFL);..fcntl(fd
82c0: 2c 20 46 5f 53 45 54 46 4c 2c 20 73 6f 63 6b 65  , F_SETFL, socke
82d0: 74 5f 66 6c 61 67 73 20 7c 20 4f 5f 4e 4f 4e 42  t_flags | O_NONB
82e0: 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 0a 09 73  LOCK);.#endif..s
82f0: 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 20 3d  endfile_offset =
8300: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72   request->header
8310: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a  s.range.offset;.
8320: 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 20  .sendfile_len = 
8330: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
8340: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09  .range.length;..
8350: 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 3d 20  sendfile_sent = 
8360: 30 3b 0a 09 77 68 69 6c 65 20 28 72 65 71 75 65  0;..while (reque
8370: 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 3d 20 46 49  st->method == FI
8380: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48  LED_REQUEST_METH
8390: 4f 44 5f 47 45 54 29 20 7b 0a 09 09 69 66 20 28  OD_GET) {...if (
83a0: 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3e 20 46  sendfile_len > F
83b0: 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41  ILED_SENDFILE_MA
83c0: 58 29 20 7b 0a 09 09 09 73 65 6e 64 66 69 6c 65  X) {....sendfile
83d0: 5f 73 69 7a 65 20 3d 20 46 49 4c 45 44 5f 53 45  _size = FILED_SE
83e0: 4e 44 46 49 4c 45 5f 4d 41 58 3b 0a 09 09 7d 20  NDFILE_MAX;...} 
83f0: 65 6c 73 65 20 7b 0a 09 09 09 73 65 6e 64 66 69  else {....sendfi
8400: 6c 65 5f 73 69 7a 65 20 3d 20 73 65 6e 64 66 69  le_size = sendfi
8410: 6c 65 5f 6c 65 6e 3b 0a 09 09 7d 0a 0a 09 09 73  le_len;...}....s
8420: 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20 73 65  endfile_ret = se
8430: 6e 64 66 69 6c 65 28 66 64 2c 20 66 69 6c 65 69  ndfile(fd, filei
8440: 6e 66 6f 2d 3e 66 64 2c 20 26 73 65 6e 64 66 69  nfo->fd, &sendfi
8450: 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 66  le_offset, sendf
8460: 69 6c 65 5f 73 69 7a 65 29 3b 0a 09 09 69 66 20  ile_size);...if 
8470: 28 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3c 3d  (sendfile_ret <=
8480: 20 30 29 20 7b 0a 23 69 66 64 65 66 20 46 49 4c   0) {.#ifdef FIL
8490: 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50  ED_NONBLOCK_HTTP
84a0: 0a 09 09 09 69 66 20 28 65 72 72 6e 6f 20 3d 3d  ....if (errno ==
84b0: 20 45 41 47 41 49 4e 29 20 7b 0a 09 09 09 09 73   EAGAIN) {.....s
84c0: 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20 30 3b  endfile_ret = 0;
84d0: 0a 0a 09 09 09 09 77 68 69 6c 65 20 28 31 29 20  ......while (1) 
84e0: 7b 0a 09 09 09 09 09 73 65 6c 65 63 74 28 66 64  {......select(fd
84f0: 20 2b 20 31 2c 20 26 72 66 64 2c 20 26 77 66 64   + 1, &rfd, &wfd
8500: 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 09  , NULL, NULL);..
8510: 09 09 09 09 69 66 20 28 46 44 5f 49 53 53 45 54  ....if (FD_ISSET
8520: 28 66 64 2c 20 26 72 66 64 29 29 20 7b 0a 09 09  (fd, &rfd)) {...
8530: 09 09 09 09 72 65 61 64 5f 72 65 74 20 3d 20 72  ....read_ret = r
8540: 65 61 64 28 66 64 2c 20 73 69 6e 6b 62 75 66 2c  ead(fd, sinkbuf,
8550: 20 73 69 7a 65 6f 66 28 73 69 6e 6b 62 75 66 29   sizeof(sinkbuf)
8560: 29 3b 0a 0a 09 09 09 09 09 09 69 66 20 28 72 65  );........if (re
8570: 61 64 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09  ad_ret <= 0) {..
8580: 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09  ......break;....
8590: 09 09 09 7d 0a 09 09 09 09 09 7d 0a 0a 09 09 09  ...}......}.....
85a0: 09 09 69 66 20 28 46 44 5f 49 53 53 45 54 28 66  ..if (FD_ISSET(f
85b0: 64 2c 20 26 77 66 64 29 29 20 7b 0a 09 09 09 09  d, &wfd)) {.....
85c0: 09 09 72 65 61 64 5f 72 65 74 20 3d 20 31 3b 0a  ..read_ret = 1;.
85d0: 0a 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09  .......break;...
85e0: 09 09 09 7d 0a 09 09 09 09 7d 0a 0a 09 09 09 09  ...}.....}......
85f0: 69 66 20 28 72 65 61 64 5f 72 65 74 20 3c 3d 20  if (read_ret <= 
8600: 30 29 20 7b 0a 09 09 09 09 09 62 72 65 61 6b 3b  0) {......break;
8610: 0a 09 09 09 09 7d 0a 09 09 09 7d 20 65 6c 73 65  .....}....} else
8620: 20 7b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09   {.....break;...
8630: 09 7d 0a 23 65 6c 73 65 0a 09 09 09 62 72 65 61  .}.#else....brea
8640: 6b 3b 0a 23 65 6e 64 69 66 0a 09 09 7d 0a 0a 09  k;.#endif...}...
8650: 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 2d 3d  .sendfile_len -=
8660: 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09   sendfile_ret;..
8670: 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 2b  .sendfile_sent +
8680: 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a  = sendfile_ret;.
8690: 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c  ..if (sendfile_l
86a0: 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72  en == 0) {....br
86b0: 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 6c 6f  eak;...}..}...lo
86c0: 67 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 28 74 69  g->endtime = (ti
86d0: 6d 65 5f 74 29 20 2d 31 3b 0a 09 6c 6f 67 2d 3e  me_t) -1;..log->
86e0: 73 65 6e 74 5f 6c 65 6e 67 74 68 20 3d 20 73 65  sent_length = se
86f0: 6e 64 66 69 6c 65 5f 73 65 6e 74 3b 0a 0a 09 66  ndfile_sent;...f
8700: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c  iled_log_entry(l
8710: 6f 67 29 3b 0a 0a 09 63 6c 6f 73 65 28 66 69 6c  og);...close(fil
8720: 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 69 66  einfo->fd);...if
8730: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65   (request->heade
8740: 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 21 3d  rs.connection !=
8750: 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f   FILED_CONNECTIO
8760: 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 29 20 7b 0a  N_KEEP_ALIVE) {.
8770: 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  ..filed_socketti
8780: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 64 29 3b  meout_close(fd);
8790: 0a 0a 09 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a  ....fclose(fp);.
87a0: 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f  ...return(FILED_
87b0: 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45  CONNECTION_CLOSE
87c0: 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f  );..}...filed_so
87d0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63  ckettimeout_proc
87e0: 65 73 73 69 6e 67 5f 65 6e 64 28 66 64 29 3b 0a  essing_end(fd);.
87f0: 0a 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43  ..return(FILED_C
8800: 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41  ONNECTION_KEEP_A
8810: 4c 49 56 45 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e  LIVE);.}../* Han
8820: 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 6e  dle incoming con
8830: 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74  nections */.stat
8840: 69 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f 77  ic void *filed_w
8850: 6f 72 6b 65 72 5f 74 68 72 65 61 64 28 76 6f 69  orker_thread(voi
8860: 64 20 2a 61 72 67 5f 76 29 20 7b 0a 09 73 74 72  d *arg_v) {..str
8870: 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72  uct filed_worker
8880: 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72  _thread_args *ar
8890: 67 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64  g;..struct filed
88a0: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 72 65  _http_request re
88b0: 71 75 65 73 74 3b 0a 09 73 74 72 75 63 74 20 66  quest;..struct f
88c0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a  iled_log_entry *
88d0: 6c 6f 67 2c 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79  log, local_dummy
88e0: 5f 6c 6f 67 3b 0a 09 73 74 72 75 63 74 20 66 69  _log;..struct fi
88f0: 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74  led_options *opt
8900: 69 6f 6e 73 3b 0a 09 73 74 72 75 63 74 20 73 6f  ions;..struct so
8910: 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 64 72 3b  ckaddr_in6 addr;
8920: 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 72  ..socklen_t addr
8930: 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 69 6c 75 72  len;..int failur
8940: 65 5f 63 6f 75 6e 74 20 3d 20 30 2c 20 6d 61 78  e_count = 0, max
8950: 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d  _failure_count =
8960: 20 46 49 4c 45 44 5f 4d 41 58 5f 46 41 49 4c 55   FILED_MAX_FAILU
8970: 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63  RE_COUNT;..int c
8980: 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65 20  onnection_state 
8990: 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49  = FILED_CONNECTI
89a0: 4f 4e 5f 43 4c 4f 53 45 3b 0a 09 69 6e 74 20 6d  ON_CLOSE;..int m
89b0: 61 73 74 65 72 5f 66 64 2c 20 66 64 20 3d 20 2d  aster_fd, fd = -
89c0: 31 3b 0a 0a 09 2f 2a 20 52 65 61 64 20 61 72 67  1;.../* Read arg
89d0: 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 72 67 20 3d  uments */..arg =
89e0: 20 61 72 67 5f 76 3b 0a 0a 09 6d 61 73 74 65 72   arg_v;...master
89f0: 5f 66 64 20 3d 20 61 72 67 2d 3e 66 64 3b 0a 09  _fd = arg->fd;..
8a00: 6f 70 74 69 6f 6e 73 20 3d 20 26 61 72 67 2d 3e  options = &arg->
8a10: 6f 70 74 69 6f 6e 73 3b 0a 0a 09 77 68 69 6c 65  options;...while
8a20: 20 28 31 29 20 7b 0a 09 09 2f 2a 20 46 61 69 6c   (1) {.../* Fail
8a30: 75 72 65 20 6c 6f 6f 70 20 70 72 65 76 65 6e 74  ure loop prevent
8a40: 69 6f 6e 20 2a 2f 0a 09 09 69 66 20 28 66 61 69  ion */...if (fai
8a50: 6c 75 72 65 5f 63 6f 75 6e 74 20 3e 20 6d 61 78  lure_count > max
8a60: 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 29 20  _failure_count) 
8a70: 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a  {....break;...}.
8a80: 0a 09 09 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .../* Allocate a
8a90: 20 6e 65 77 20 6c 6f 67 20 62 75 66 66 65 72 20   new log buffer 
8aa0: 2a 2f 0a 09 09 6c 6f 67 20 3d 20 66 69 6c 65 64  */...log = filed
8ab0: 5f 6c 6f 67 5f 6e 65 77 28 31 29 3b 0a 09 09 69  _log_new(1);...i
8ac0: 66 20 28 6c 6f 67 20 3d 3d 20 4e 55 4c 4c 29 20  f (log == NULL) 
8ad0: 7b 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  {....filed_log_m
8ae0: 73 67 28 22 41 4c 4c 4f 43 41 54 45 5f 4c 4f 47  sg("ALLOCATE_LOG
8af0: 5f 4d 53 47 5f 46 41 49 4c 45 44 22 29 3b 0a 0a  _MSG_FAILED");..
8b00: 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09  ...break;...}...
8b10: 09 6c 6f 67 2d 3e 74 79 70 65 20 3d 20 46 49 4c  .log->type = FIL
8b20: 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54 52 41 4e  ED_LOG_TYPE_TRAN
8b30: 53 46 45 52 3b 0a 0a 09 09 2f 2a 20 49 66 20 77  SFER;..../* If w
8b40: 65 20 63 6c 6f 73 65 64 20 74 68 65 20 6f 6c 64  e closed the old
8b50: 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 61 63 63   connection, acc
8b60: 65 70 74 20 61 20 6e 65 77 20 6f 6e 65 20 2a 2f  ept a new one */
8b70: 0a 09 09 69 66 20 28 63 6f 6e 6e 65 63 74 69 6f  ...if (connectio
8b80: 6e 5f 73 74 61 74 65 20 3d 3d 20 46 49 4c 45 44  n_state == FILED
8b90: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53  _CONNECTION_CLOS
8ba0: 45 29 20 7b 0a 09 09 09 2f 2a 20 41 63 63 65 70  E) {..../* Accep
8bb0: 74 20 61 20 6e 65 77 20 63 6c 69 65 6e 74 20 2a  t a new client *
8bc0: 2f 0a 09 09 09 61 64 64 72 6c 65 6e 20 3d 20 73  /....addrlen = s
8bd0: 69 7a 65 6f 66 28 61 64 64 72 29 3b 0a 0a 09 09  izeof(addr);....
8be0: 09 66 64 20 3d 20 61 63 63 65 70 74 28 6d 61 73  .fd = accept(mas
8bf0: 74 65 72 5f 66 64 2c 20 28 73 74 72 75 63 74 20  ter_fd, (struct 
8c00: 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64  sockaddr *) &add
8c10: 72 2c 20 26 61 64 64 72 6c 65 6e 29 3b 0a 09 09  r, &addrlen);...
8c20: 7d 0a 0a 09 09 2f 2a 0a 09 09 20 2a 20 49 66 20  }..../*... * If 
8c30: 77 65 20 66 61 69 6c 2c 20 6d 61 6b 65 20 61 20  we fail, make a 
8c40: 6e 6f 74 65 20 6f 66 20 69 74 20 73 6f 20 77 65  note of it so we
8c50: 20 64 6f 6e 27 74 20 67 6f 20 69 6e 74 6f 20 61   don't go into a
8c60: 20 6c 6f 6f 70 20 6f 66 0a 09 09 20 2a 20 61 63   loop of... * ac
8c70: 63 65 70 74 28 29 20 66 61 69 6c 69 6e 67 0a 09  cept() failing..
8c80: 09 20 2a 2f 0a 09 09 69 66 20 28 66 64 20 3c 20  . */...if (fd < 
8c90: 30 29 20 7b 0a 09 09 09 2f 2a 20 4c 6f 67 20 74  0) {..../* Log t
8ca0: 68 65 20 6e 65 77 20 63 6f 6e 6e 65 63 74 69 6f  he new connectio
8cb0: 6e 20 2a 2f 0a 09 09 09 66 69 6c 65 64 5f 6c 6f  n */....filed_lo
8cc0: 67 5f 6d 73 67 28 22 41 43 43 45 50 54 5f 46 41  g_msg("ACCEPT_FA
8cd0: 49 4c 45 44 22 29 3b 0a 0a 09 09 09 66 61 69 6c  ILED");.....fail
8ce0: 75 72 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 0a 09 09  ure_count++;....
8cf0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 66 72 65 65 28  .filed_log_free(
8d00: 6c 6f 67 29 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e  log);.....contin
8d10: 75 65 3b 0a 09 09 7d 0a 0a 09 09 66 69 6c 65 64  ue;...}....filed
8d20: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 61  _sockettimeout_a
8d30: 63 63 65 70 74 28 66 64 29 3b 0a 0a 09 09 2f 2a  ccept(fd);..../*
8d40: 20 46 69 6c 6c 20 69 6e 20 6c 6f 67 20 73 74 72   Fill in log str
8d50: 75 63 74 75 72 65 20 2a 2f 0a 09 09 69 66 20 28  ucture */...if (
8d60: 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 28 73 74  filed_log_ip((st
8d70: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29  ruct sockaddr *)
8d80: 20 26 61 64 64 72 2c 20 6c 6f 67 2d 3e 69 70 2c   &addr, log->ip,
8d90: 20 73 69 7a 65 6f 66 28 6c 6f 67 2d 3e 69 70 29   sizeof(log->ip)
8da0: 29 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09  ) == NULL) {....
8db0: 6c 6f 67 2d 3e 69 70 5b 30 5d 20 3d 20 27 5c 30  log->ip[0] = '\0
8dc0: 27 3b 0a 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 20  ';....log->port 
8dd0: 3d 20 30 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a  = 0;...} else {.
8de0: 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 20 3d 20 61  ...log->port = a
8df0: 64 64 72 2e 73 69 6e 36 5f 70 6f 72 74 3b 0a 09  ddr.sin6_port;..
8e00: 09 7d 0a 0a 09 09 2f 2a 20 52 65 73 65 74 20 66  .}..../* Reset f
8e10: 61 69 6c 75 72 65 20 63 6f 75 6e 74 2a 2f 0a 09  ailure count*/..
8e20: 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d  .failure_count =
8e30: 20 30 3b 0a 0a 09 09 2f 2a 20 48 61 6e 64 6c 65   0;..../* Handle
8e40: 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 09 63 6f 6e   socket */...con
8e50: 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65 20 3d 20  nection_state = 
8e60: 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69  filed_handle_cli
8e70: 65 6e 74 28 66 64 2c 20 26 72 65 71 75 65 73 74  ent(fd, &request
8e80: 2c 20 6c 6f 67 2c 20 6f 70 74 69 6f 6e 73 29 3b  , log, options);
8e90: 0a 09 7d 0a 0a 09 2f 2a 20 52 65 70 6f 72 74 20  ..}.../* Report 
8ea0: 65 72 72 6f 72 20 2a 2f 0a 09 66 69 6c 65 64 5f  error */..filed_
8eb0: 6c 6f 67 5f 6d 73 67 28 22 54 48 52 45 41 44 5f  log_msg("THREAD_
8ec0: 44 49 45 44 20 41 42 4e 4f 52 4d 41 4c 22 29 3b  DIED ABNORMAL");
8ed0: 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b  ...return(NULL);
8ee0: 0a 0a 09 2f 2a 20 6c 6f 63 61 6c 5f 64 75 6d 6d  .../* local_dumm
8ef0: 79 5f 6c 6f 67 20 69 73 20 6f 6e 6c 79 20 75 73  y_log is only us
8f00: 65 64 20 69 66 20 46 49 4c 45 44 5f 44 4f 4e 54  ed if FILED_DONT
8f10: 5f 4c 4f 47 20 69 73 20 65 6e 61 62 6c 65 64 2c  _LOG is enabled,
8f20: 20 6f 74 68 65 72 77 69 73 65 20 69 74 27 73 20   otherwise it's 
8f30: 6e 6f 74 20 75 73 65 64 2c 20 62 75 74 20 74 68  not used, but th
8f40: 65 20 63 6f 6d 70 69 6c 65 72 20 68 61 74 65 73  e compiler hates
8f50: 20 74 68 61 74 20 69 64 65 61 2e 20 2a 2f 0a 09   that idea. */..
8f60: 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e  local_dummy_log.
8f70: 74 79 70 65 20 3d 20 30 3b 0a 09 6c 6f 63 61 6c  type = 0;..local
8f80: 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 20  _dummy_log.type 
8f90: 3d 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f  = local_dummy_lo
8fa0: 67 2e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 43 72  g.type;.}../* Cr
8fb0: 65 61 74 65 20 77 6f 72 6b 65 72 20 74 68 72 65  eate worker thre
8fc0: 61 64 73 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ads */.static in
8fd0: 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74  t filed_worker_t
8fe0: 68 72 65 61 64 73 5f 69 6e 69 74 28 69 6e 74 20  hreads_init(int 
8ff0: 66 64 2c 20 69 6e 74 20 74 68 72 65 61 64 5f 63  fd, int thread_c
9000: 6f 75 6e 74 2c 20 73 74 72 75 63 74 20 66 69 6c  ount, struct fil
9010: 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69  ed_options *opti
9020: 6f 6e 73 29 20 7b 0a 09 73 74 72 75 63 74 20 66  ons) {..struct f
9030: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65  iled_worker_thre
9040: 61 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 70  ad_args *arg;..p
9050: 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 69  thread_t threadi
9060: 64 3b 0a 09 69 6e 74 20 70 74 68 72 65 61 64 5f  d;..int pthread_
9070: 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 66  ret;..int i;...f
9080: 6f 72 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 74  or (i = 0; i < t
9090: 68 72 65 61 64 5f 63 6f 75 6e 74 3b 20 69 2b 2b  hread_count; i++
90a0: 29 20 7b 0a 09 09 61 72 67 20 3d 20 6d 61 6c 6c  ) {...arg = mall
90b0: 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72 67 29 29  oc(sizeof(*arg))
90c0: 3b 0a 0a 09 09 61 72 67 2d 3e 66 64 20 3d 20 66  ;....arg->fd = f
90d0: 64 3b 0a 09 09 6d 65 6d 63 70 79 28 26 61 72 67  d;...memcpy(&arg
90e0: 2d 3e 6f 70 74 69 6f 6e 73 2c 20 6f 70 74 69 6f  ->options, optio
90f0: 6e 73 2c 20 73 69 7a 65 6f 66 28 2a 6f 70 74 69  ns, sizeof(*opti
9100: 6f 6e 73 29 29 3b 0a 0a 09 09 70 74 68 72 65 61  ons));....pthrea
9110: 64 5f 72 65 74 20 3d 20 70 74 68 72 65 61 64 5f  d_ret = pthread_
9120: 63 72 65 61 74 65 28 26 74 68 72 65 61 64 69 64  create(&threadid
9130: 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 77 6f  , NULL, filed_wo
9140: 72 6b 65 72 5f 74 68 72 65 61 64 2c 20 61 72 67  rker_thread, arg
9150: 29 3b 0a 09 09 69 66 20 28 70 74 68 72 65 61 64  );...if (pthread
9160: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09  _ret != 0) {....
9170: 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a  return(-1);...}.
9180: 09 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a  .}...return(0);.
9190: 7d 0a 0a 2f 2a 20 44 69 73 70 6c 61 79 20 68 65  }../* Display he
91a0: 6c 70 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  lp */.static voi
91b0: 64 20 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65  d filed_print_he
91c0: 6c 70 28 46 49 4c 45 20 2a 6f 75 74 70 75 74 2c  lp(FILE *output,
91d0: 20 69 6e 74 20 6c 6f 6e 67 5f 68 65 6c 70 2c 20   int long_help, 
91e0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 65 78 74 72  const char *extr
91f0: 61 29 20 7b 0a 09 69 66 20 28 65 78 74 72 61 29  a) {..if (extra)
9200: 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74   {...fprintf(out
9210: 70 75 74 2c 20 22 25 73 5c 6e 22 2c 20 65 78 74  put, "%s\n", ext
9220: 72 61 29 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74  ra);..}...fprint
9230: 66 28 6f 75 74 70 75 74 2c 20 22 55 73 61 67 65  f(output, "Usage
9240: 3a 20 66 69 6c 65 64 20 5b 3c 6f 70 74 69 6f 6e  : filed [<option
9250: 73 3e 5d 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74  s>]\n");..fprint
9260: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 4f 70 74  f(output, "  Opt
9270: 69 6f 6e 73 3a 5c 6e 22 29 3b 0a 09 66 70 72 69  ions:\n");..fpri
9280: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9290: 20 20 20 2d 68 2c 20 2d 2d 68 65 6c 70 5c 6e 22     -h, --help\n"
92a0: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70  );..fprintf(outp
92b0: 75 74 2c 20 22 20 20 20 20 20 20 2d 64 2c 20 2d  ut, "      -d, -
92c0: 2d 64 61 65 6d 6f 6e 5c 6e 22 29 3b 0a 09 66 70  -daemon\n");..fp
92d0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
92e0: 20 20 20 20 20 2d 76 2c 20 2d 2d 76 65 72 73 69       -v, --versi
92f0: 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66  on\n");..fprintf
9300: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
9310: 2d 56 2c 20 2d 2d 76 68 6f 73 74 5c 6e 22 29 3b  -V, --vhost\n");
9320: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9330: 2c 20 22 20 20 20 20 20 20 2d 62 20 3c 61 64 64  , "      -b <add
9340: 72 65 73 73 3e 2c 20 2d 2d 62 69 6e 64 20 3c 61  ress>, --bind <a
9350: 64 64 72 65 73 73 3e 5c 6e 22 29 3b 0a 09 66 70  ddress>\n");..fp
9360: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9370: 20 20 20 20 20 2d 70 20 3c 70 6f 72 74 3e 2c 20       -p <port>, 
9380: 2d 2d 70 6f 72 74 20 3c 70 6f 72 74 3e 5c 6e 22  --port <port>\n"
9390: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70  );..fprintf(outp
93a0: 75 74 2c 20 22 20 20 20 20 20 20 2d 74 20 3c 63  ut, "      -t <c
93b0: 6f 75 6e 74 3e 2c 20 2d 2d 74 68 72 65 61 64 73  ount>, --threads
93c0: 20 3c 63 6f 75 6e 74 3e 5c 6e 22 29 3b 0a 09 66   <count>\n");..f
93d0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
93e0: 20 20 20 20 20 20 2d 63 20 3c 65 6e 74 72 69 65        -c <entrie
93f0: 73 3e 2c 20 2d 2d 63 61 63 68 65 20 3c 65 6e 74  s>, --cache <ent
9400: 72 69 65 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69  ries>\n");..fpri
9410: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9420: 20 20 20 2d 6c 20 3c 66 69 6c 65 3e 2c 20 2d 2d     -l <file>, --
9430: 6c 6f 67 20 3c 66 69 6c 65 3e 5c 6e 22 29 3b 0a  log <file>\n");.
9440: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9450: 20 22 20 20 20 20 20 20 2d 75 20 3c 75 73 65 72   "      -u <user
9460: 3e 2c 20 2d 2d 75 73 65 72 20 3c 75 73 65 72 3e  >, --user <user>
9470: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f  \n");..fprintf(o
9480: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 72  utput, "      -r
9490: 20 3c 64 69 72 65 63 74 6f 72 79 3e 2c 20 2d 2d   <directory>, --
94a0: 72 6f 6f 74 20 3c 64 69 72 65 63 74 6f 72 79 3e  root <directory>
94b0: 5c 6e 22 29 3b 0a 0a 09 69 66 20 28 6c 6f 6e 67  \n");...if (long
94c0: 5f 68 65 6c 70 29 20 7b 0a 09 09 66 70 72 69 6e  _help) {...fprin
94d0: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29  tf(output, "\n")
94e0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
94f0: 75 74 2c 20 22 20 20 55 73 61 67 65 3a 5c 6e 22  ut, "  Usage:\n"
9500: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
9510: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 68 20 28  put, "      -h (
9520: 6f 72 20 2d 2d 68 65 6c 70 29 20 70 72 69 6e 74  or --help) print
9530: 73 20 74 68 69 73 20 75 73 61 67 65 20 69 6e 66  s this usage inf
9540: 6f 72 6d 61 74 69 6f 6e 2e 5c 6e 22 29 3b 0a 09  ormation.\n");..
9550: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9560: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
9570: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
9580: 20 2d 64 20 28 6f 72 20 2d 2d 64 61 65 6d 6f 6e   -d (or --daemon
9590: 29 20 69 6e 73 74 72 75 63 74 73 20 66 69 6c 65  ) instructs file
95a0: 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 64 61  d to become a da
95b0: 65 6d 6f 6e 20 61 66 74 65 72 20 69 6e 69 74 69  emon after initi
95c0: 61 6c 69 7a 69 6e 67 5c 6e 22 29 3b 0a 09 09 66  alizing\n");...f
95d0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
95e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
95f0: 20 20 20 20 20 20 20 74 68 65 20 6c 69 73 74 65         the liste
9600: 6e 69 6e 67 20 54 43 50 20 73 6f 63 6b 65 74 20  ning TCP socket 
9610: 61 6e 64 20 6c 6f 67 20 66 69 6c 65 73 2e 5c 6e  and log files.\n
9620: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
9630: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66  tput, "\n");...f
9640: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
9650: 20 20 20 20 20 20 2d 76 20 28 6f 72 20 2d 2d 76        -v (or --v
9660: 65 72 73 69 6f 6e 29 20 69 6e 73 74 72 75 63 74  ersion) instruct
9670: 73 20 66 69 6c 65 64 20 70 72 69 6e 74 20 6f 75  s filed print ou
9680: 74 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75  t the version nu
9690: 6d 62 65 72 20 61 6e 64 20 65 78 69 74 2e 5c 6e  mber and exit.\n
96a0: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
96b0: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66  tput, "\n");...f
96c0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
96d0: 20 20 20 20 20 20 2d 56 20 28 6f 72 20 2d 2d 76        -V (or --v
96e0: 68 6f 73 74 29 20 69 6e 73 74 72 75 63 74 73 20  host) instructs 
96f0: 66 69 6c 65 64 20 74 6f 20 70 72 65 70 65 6e 64  filed to prepend
9700: 20 61 6c 6c 20 72 65 71 75 65 73 74 73 20 77 69   all requests wi
9710: 74 68 20 74 68 65 69 72 20 48 54 54 50 5c 6e 22  th their HTTP\n"
9720: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
9730: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
9740: 20 20 20 20 20 20 20 20 20 20 20 20 48 6f 73 74              Host
9750: 20 68 65 61 64 65 72 2e 5c 6e 22 29 3b 0a 09 09   header.\n");...
9760: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
9770: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  "\n");...fprintf
9780: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
9790: 2d 62 20 28 6f 72 20 2d 2d 62 69 6e 64 29 20 73  -b (or --bind) s
97a0: 70 65 63 69 66 69 65 73 20 74 68 65 20 61 64 64  pecifies the add
97b0: 72 65 73 73 20 74 6f 20 6c 69 73 74 65 6e 20 66  ress to listen f
97c0: 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 48 54 54 50  or incoming HTTP
97d0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
97e0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
97f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
9800: 71 75 65 73 74 73 20 6f 6e 2e 20 20 54 68 65 20  quests on.  The 
9810: 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73  default value is
9820: 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 42 49 4e   \"%s\".\n", BIN
9830: 44 5f 41 44 44 52 29 3b 0a 09 09 66 70 72 69 6e  D_ADDR);...fprin
9840: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29  tf(output, "\n")
9850: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
9860: 75 74 2c 20 22 20 20 20 20 20 20 2d 70 20 28 6f  ut, "      -p (o
9870: 72 20 2d 2d 70 6f 72 74 29 20 73 70 65 63 69 66  r --port) specif
9880: 69 65 73 20 74 68 65 20 54 43 50 20 70 6f 72 74  ies the TCP port
9890: 20 6e 75 6d 62 65 72 20 74 6f 20 6c 69 73 74 65   number to liste
98a0: 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 48  n for incoming H
98b0: 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  TTP\n");...fprin
98c0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
98d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98e0: 20 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 20 54   requests on.  T
98f0: 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 75  he default is %u
9900: 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 20  .\n", (unsigned 
9910: 69 6e 74 29 20 50 4f 52 54 29 3b 0a 09 09 66 70  int) PORT);...fp
9920: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c  rintf(output, "\
9930: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
9940: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 74  utput, "      -t
9950: 20 28 6f 72 20 2d 2d 74 68 72 65 61 64 73 29 20   (or --threads) 
9960: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75  specifies the nu
9970: 6d 62 65 72 20 6f 66 20 77 6f 72 6b 65 72 20 74  mber of worker t
9980: 68 72 65 61 64 73 20 74 6f 20 63 72 65 61 74 65  hreads to create
9990: 2e 20 45 61 63 68 5c 6e 22 29 3b 0a 09 09 66 70  . Each\n");...fp
99a0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
99b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99c0: 20 20 20 20 20 20 20 77 6f 72 6b 65 72 20 74 68         worker th
99d0: 72 65 61 64 20 63 61 6e 20 73 65 72 76 69 63 65  read can service
99e0: 20 6f 6e 65 20 63 6f 6e 63 75 72 72 65 6e 74 20   one concurrent 
99f0: 48 54 54 50 20 73 65 73 73 69 6f 6e 2e 5c 6e 22  HTTP session.\n"
9a00: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
9a10: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
9a20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
9a30: 75 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  us the number of
9a40: 20 74 68 72 65 61 64 73 20 63 72 65 61 74 65 64   threads created
9a50: 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 20   will determine 
9a60: 68 6f 77 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  how\n");...fprin
9a70: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9a80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a90: 20 20 20 20 6d 61 6e 79 20 73 69 6d 75 6c 74 61      many simulta
9aa0: 6e 65 6f 75 73 20 74 72 61 6e 73 66 65 72 73 20  neous transfers 
9ab0: 77 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c 65  will be possible
9ac0: 2e 20 54 68 65 5c 6e 22 29 3b 0a 09 09 66 70 72  . The\n");...fpr
9ad0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
9ae0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9af0: 20 20 20 20 20 20 64 65 66 61 75 6c 74 20 69 73        default is
9b00: 20 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67   %lu.\n", (unsig
9b10: 6e 65 64 20 6c 6f 6e 67 29 20 54 48 52 45 41 44  ned long) THREAD
9b20: 5f 43 4f 55 4e 54 29 3b 0a 09 09 66 70 72 69 6e  _COUNT);...fprin
9b30: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29  tf(output, "\n")
9b40: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
9b50: 75 74 2c 20 22 20 20 20 20 20 20 2d 63 20 28 6f  ut, "      -c (o
9b60: 72 20 2d 2d 63 61 63 68 65 29 20 73 70 65 63 69  r --cache) speci
9b70: 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20  fies the number 
9b80: 6f 66 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74  of file informat
9b90: 69 6f 6e 20 63 61 63 68 65 20 65 6e 74 72 69 65  ion cache entrie
9ba0: 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  s\n");...fprintf
9bb0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
9bc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9bd0: 74 6f 20 61 6c 6c 6f 63 61 74 65 2e 20 20 45 61  to allocate.  Ea
9be0: 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 68  ch cache entry h
9bf0: 6f 6c 64 73 20 66 69 6c 65 20 69 6e 66 6f 72 6d  olds file inform
9c00: 61 74 69 6f 6e 20 61 73 5c 6e 22 29 3b 0a 09 09  ation as\n");...
9c10: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
9c20: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
9c30: 20 20 20 20 20 20 20 77 65 6c 6c 20 61 73 20 61         well as a
9c40: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 64 65 73 63  n open file desc
9c50: 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20 66 69  riptor to the fi
9c60: 6c 65 2c 20 73 6f 20 72 65 73 6f 75 72 63 65 5c  le, so resource\
9c70: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
9c80: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
9c90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69                li
9ca0: 6d 69 74 73 20 28 69 2e 65 2e 2c 20 75 6c 69 6d  mits (i.e., ulim
9cb0: 69 74 29 20 73 68 6f 75 6c 64 20 62 65 20 63 6f  it) should be co
9cc0: 6e 73 69 64 65 72 65 64 2e 20 20 54 68 69 73 20  nsidered.  This 
9cd0: 73 68 6f 75 6c 64 5c 6e 22 29 3b 0a 09 09 66 70  should\n");...fp
9ce0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9cf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9d00: 20 20 20 20 20 62 65 20 61 20 70 72 69 6d 65 20       be a prime 
9d10: 6e 75 6d 62 65 72 20 66 6f 72 20 69 64 65 61 6c  number for ideal
9d20: 20 75 73 65 20 77 69 74 68 20 74 68 65 20 6c 6f   use with the lo
9d30: 6f 6b 75 70 20 6d 65 74 68 6f 64 2e 5c 6e 22 29  okup method.\n")
9d40: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
9d50: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
9d60: 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 64             The d
9d70: 65 66 61 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e  efault is %lu.\n
9d80: 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ", (unsigned lon
9d90: 67 29 20 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a  g) CACHE_SIZE);.
9da0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9db0: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  , "\n");...fprin
9dc0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9dd0: 20 20 2d 6c 20 28 6f 72 20 2d 2d 6c 6f 67 29 20    -l (or --log) 
9de0: 73 70 65 63 69 66 69 65 73 20 61 20 66 69 6c 65  specifies a file
9df0: 6e 61 6d 65 20 74 6f 20 6f 70 65 6e 20 66 6f 72  name to open for
9e00: 20 77 72 69 74 69 6e 67 20 6c 6f 67 20 65 6e 74   writing log ent
9e10: 72 69 65 73 2e 20 20 4c 6f 67 5c 6e 22 29 3b 0a  ries.  Log\n");.
9e20: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9e30: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
9e40: 20 20 20 20 20 20 20 65 6e 74 72 69 65 73 20 61         entries a
9e50: 72 65 20 6d 61 64 65 20 66 6f 72 20 76 61 72 69  re made for vari
9e60: 6f 75 73 20 73 74 61 67 65 73 20 69 6e 20 74 72  ous stages in tr
9e70: 61 6e 73 66 65 72 69 6e 67 20 66 69 6c 65 73 2e  ansfering files.
9e80: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
9e90: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
9ea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65               The
9eb0: 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 6f 70 65   log file is ope
9ec0: 6e 65 64 20 62 65 66 6f 72 65 20 73 77 69 74 63  ned before switc
9ed0: 68 69 6e 67 20 75 73 65 72 73 20 28 73 65 65 20  hing users (see 
9ee0: 5c 22 2d 75 5c 22 29 5c 6e 22 29 3b 0a 09 09 66  \"-u\")\n");...f
9ef0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
9f00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f10: 20 20 20 20 61 6e 64 20 72 6f 6f 74 20 64 69 72      and root dir
9f20: 65 63 74 6f 72 69 65 73 20 28 73 65 65 20 5c 22  ectories (see \"
9f30: 2d 72 5c 22 29 2e 20 20 54 68 65 20 6c 6f 67 20  -r\").  The log 
9f40: 66 69 6c 65 20 69 73 20 6e 65 76 65 72 5c 6e 22  file is never\n"
9f50: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
9f60: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
9f70: 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 64            closed
9f80: 20 73 6f 20 6c 6f 67 20 72 6f 74 61 74 69 6f 6e   so log rotation
9f90: 20 77 69 74 68 6f 75 74 20 73 74 6f 70 70 69 6e   without stoppin
9fa0: 67 20 74 68 65 20 64 61 65 6d 6f 6e 20 69 73 20  g the daemon is 
9fb0: 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 72 69  will\n");...fpri
9fc0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9fd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fe0: 20 6e 6f 74 20 77 6f 72 6b 2e 20 20 54 68 65 20   not work.  The 
9ff0: 76 61 6c 75 65 20 6f 66 20 5c 22 2d 5c 22 20 69  value of \"-\" i
a000: 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 73 74  ndicates that st
a010: 61 6e 64 61 72 64 20 6f 75 74 70 75 74 5c 6e 22  andard output\n"
a020: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
a030: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
a040: 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64            should
a050: 20 62 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 67   be used for log
a060: 67 69 6e 67 2e 20 20 49 66 20 74 68 65 20 66 69  ging.  If the fi
a070: 6c 65 6e 61 6d 65 20 62 65 67 69 6e 73 20 77 69  lename begins wi
a080: 74 68 20 61 5c 6e 22 29 3b 0a 09 09 66 70 72 69  th a\n");...fpri
a090: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
a0a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0b0: 20 70 69 70 65 20 28 5c 22 7c 5c 22 29 20 74 68   pipe (\"|\") th
a0c0: 65 6e 20 61 20 70 72 6f 63 65 73 73 20 69 73 20  en a process is 
a0d0: 73 74 61 72 74 65 64 20 61 6e 64 20 75 73 65 64  started and used
a0e0: 20 66 6f 72 20 6c 6f 67 67 69 6e 67 5c 6e 22 29   for logging\n")
a0f0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
a100: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
a110: 20 20 20 20 20 20 20 20 20 69 6e 73 74 65 61 64           instead
a120: 20 6f 66 20 61 20 66 69 6c 65 2e 20 20 54 68 65   of a file.  The
a130: 20 64 65 66 61 75 6c 74 20 69 73 20 5c 22 25 73   default is \"%s
a140: 5c 22 2e 5c 6e 22 2c 20 4c 4f 47 5f 46 49 4c 45  \".\n", LOG_FILE
a150: 29 3b 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f  );.#ifdef FILED_
a160: 44 4f 4e 54 5f 4c 4f 47 0a 09 09 66 70 72 69 6e  DONT_LOG...fprin
a170: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
a180: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a190: 4e 6f 74 65 20 74 68 61 74 20 6c 6f 67 67 69 6e  Note that loggin
a1a0: 67 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20  g is completely 
a1b0: 64 69 73 61 62 6c 65 64 20 73 6f 20 74 68 69 73  disabled so this
a1c0: 20 6f 70 74 69 6f 6e 20 64 6f 65 73 5c 6e 22 29   option does\n")
a1d0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
a1e0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
a1f0: 20 20 20 20 20 20 20 20 20 6e 6f 74 68 69 6e 67           nothing
a200: 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2e 5c   in this build.\
a210: 6e 22 29 3b 0a 23 65 6e 64 69 66 0a 09 09 66 70  n");.#endif...fp
a220: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c  rintf(output, "\
a230: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
a240: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 75  utput, "      -u
a250: 20 28 6f 72 20 2d 2d 75 73 65 72 29 20 73 70 65   (or --user) spe
a260: 63 69 66 69 65 73 20 74 68 65 20 75 73 65 72 20  cifies the user 
a270: 74 6f 20 73 77 69 74 63 68 20 75 73 65 72 20 49  to switch user I
a280: 44 73 20 74 6f 20 62 65 66 6f 72 65 20 73 65 72  Ds to before ser
a290: 76 69 63 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70  vicing\n");...fp
a2a0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
a2b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2c0: 20 20 20 20 72 65 71 75 65 73 74 73 2e 20 20 54      requests.  T
a2d0: 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 6e 6f  he default is no
a2e0: 74 20 63 68 61 6e 67 65 20 75 73 65 72 20 49 44  t change user ID
a2f0: 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74  s.\n");...fprint
a300: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b  f(output, "\n");
a310: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
a320: 74 2c 20 22 20 20 20 20 20 20 2d 72 20 28 6f 72  t, "      -r (or
a330: 20 2d 2d 72 6f 6f 74 29 20 73 70 65 63 69 66 69   --root) specifi
a340: 65 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  es the directory
a350: 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72   to act as the r
a360: 6f 6f 74 20 64 69 72 65 63 74 6f 72 79 20 66 6f  oot directory fo
a370: 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  r\n");...fprintf
a380: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
a390: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
a3a0: 68 65 20 66 69 6c 65 20 73 65 72 76 65 72 2e 20  he file server. 
a3b0: 20 49 66 20 74 68 69 73 20 6f 70 74 69 6f 6e 20   If this option 
a3c0: 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 63 68  is specified, ch
a3d0: 72 6f 6f 74 28 32 29 5c 6e 22 29 3b 0a 09 09 66  root(2)\n");...f
a3e0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
a3f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a400: 20 20 20 20 20 69 73 20 63 61 6c 6c 65 64 2e 20       is called. 
a410: 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20   The default is 
a420: 6e 6f 74 20 63 68 61 6e 67 65 20 72 6f 6f 74 20  not change root 
a430: 64 69 72 65 63 74 6f 72 69 65 73 2c 5c 6e 22 29  directories,\n")
a440: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
a450: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
a460: 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69            that i
a470: 73 2c 20 74 68 65 20 5c 22 2f 5c 22 20 64 69 72  s, the \"/\" dir
a480: 65 63 74 6f 72 79 20 69 73 20 73 68 61 72 65 64  ectory is shared
a490: 20 6f 75 74 2e 20 20 54 68 69 73 20 77 69 6c 6c   out.  This will
a4a0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
a4b0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
a4c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69                li
a4d0: 6b 65 6c 79 20 62 65 20 61 20 73 65 63 75 72 69  kely be a securi
a4e0: 74 79 20 69 73 73 75 65 2c 20 73 6f 20 74 68 69  ty issue, so thi
a4f0: 73 20 6f 70 74 69 6f 6e 20 73 68 6f 75 6c 64 20  s option should 
a500: 61 6c 77 61 79 73 5c 6e 22 29 3b 0a 09 09 66 70  always\n");...fp
a510: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
a520: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a530: 20 20 20 20 62 65 20 75 73 65 64 2e 5c 6e 22 29      be used.\n")
a540: 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d  ;..}...return;.}
a550: 0a 0a 2f 2a 20 41 64 64 20 61 20 67 65 74 6f 70  ../* Add a getop
a560: 74 20 6f 70 74 69 6f 6e 20 2a 2f 0a 73 74 61 74  t option */.stat
a570: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67 65  ic void filed_ge
a580: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74  topt_long_setopt
a590: 28 73 74 72 75 63 74 20 6f 70 74 69 6f 6e 20 2a  (struct option *
a5a0: 6f 70 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  opt, const char 
a5b0: 2a 6e 61 6d 65 2c 20 69 6e 74 20 68 61 73 5f 61  *name, int has_a
a5c0: 72 67 2c 20 69 6e 74 20 76 61 6c 29 20 7b 0a 09  rg, int val) {..
a5d0: 6f 70 74 2d 3e 6e 61 6d 65 20 20 20 20 20 3d 20  opt->name     = 
a5e0: 6e 61 6d 65 3b 0a 09 6f 70 74 2d 3e 68 61 73 5f  name;..opt->has_
a5f0: 61 72 67 20 20 3d 20 68 61 73 5f 61 72 67 3b 0a  arg  = has_arg;.
a600: 09 6f 70 74 2d 3e 66 6c 61 67 20 20 20 20 20 3d  .opt->flag     =
a610: 20 4e 55 4c 4c 3b 0a 09 6f 70 74 2d 3e 76 61 6c   NULL;..opt->val
a620: 20 20 20 20 20 20 3d 20 76 61 6c 3b 0a 0a 09 72        = val;...r
a630: 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 73  eturn;.}../* Res
a640: 6f 6c 76 65 20 61 20 75 73 65 72 6e 61 6d 65 20  olve a username 
a650: 74 6f 20 61 20 55 49 44 20 2a 2f 0a 73 74 61 74  to a UID */.stat
a660: 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 75 73 65  ic int filed_use
a670: 72 5f 6c 6f 6f 6b 75 70 28 63 6f 6e 73 74 20 63  r_lookup(const c
a680: 68 61 72 20 2a 75 73 65 72 2c 20 75 69 64 5f 74  har *user, uid_t
a690: 20 2a 75 73 65 72 5f 69 64 29 20 7b 0a 09 63 68   *user_id) {..ch
a6a0: 61 72 20 2a 6e 65 78 74 3b 0a 09 75 69 64 5f 74  ar *next;..uid_t
a6b0: 20 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 3b 0a   user_id_check;.
a6c0: 23 69 66 6e 64 65 66 20 46 49 4c 45 44 5f 4e 4f  #ifndef FILED_NO
a6d0: 5f 47 45 54 50 57 4e 41 4d 0a 09 73 74 72 75 63  _GETPWNAM..struc
a6e0: 74 20 70 61 73 73 77 64 20 2a 65 6e 74 3b 0a 0a  t passwd *ent;..
a6f0: 09 65 6e 74 20 3d 20 67 65 74 70 77 6e 61 6d 28  .ent = getpwnam(
a700: 75 73 65 72 29 3b 0a 09 69 66 20 28 65 6e 74 20  user);..if (ent 
a710: 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 75 73  != NULL) {...*us
a720: 65 72 5f 69 64 20 3d 20 65 6e 74 2d 3e 70 77 5f  er_id = ent->pw_
a730: 75 69 64 3b 0a 0a 09 09 72 65 74 75 72 6e 28 30  uid;....return(0
a740: 29 3b 0a 09 7d 0a 23 65 6e 64 69 66 0a 0a 09 75  );..}.#endif...u
a750: 73 65 72 5f 69 64 5f 63 68 65 63 6b 20 3d 20 73  ser_id_check = s
a760: 74 72 74 6f 75 6c 6c 28 75 73 65 72 2c 20 26 6e  trtoull(user, &n
a770: 65 78 74 2c 20 31 30 29 3b 0a 09 69 66 20 28 6e  ext, 10);..if (n
a780: 65 78 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09  ext == NULL) {..
a790: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a  .return(1);..}..
a7a0: 09 69 66 20 28 6e 65 78 74 5b 30 5d 20 21 3d 20  .if (next[0] != 
a7b0: 27 5c 30 27 29 20 7b 0a 09 09 72 65 74 75 72 6e  '\0') {...return
a7c0: 28 31 29 3b 0a 09 7d 0a 0a 09 2a 75 73 65 72 5f  (1);..}...*user_
a7d0: 69 64 20 3d 20 75 73 65 72 5f 69 64 5f 63 68 65  id = user_id_che
a7e0: 63 6b 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b  ck;...return(0);
a7f0: 0a 7d 0a 0a 2f 2a 20 44 61 65 6d 6f 6e 69 7a 65  .}../* Daemonize
a800: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
a810: 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 76  iled_daemonize(v
a820: 6f 69 64 29 20 7b 0a 09 70 69 64 5f 74 20 73 65  oid) {..pid_t se
a830: 74 73 69 64 5f 72 65 74 2c 20 66 6f 72 6b 5f 72  tsid_ret, fork_r
a840: 65 74 3b 0a 09 69 6e 74 20 63 68 64 69 72 5f 72  et;..int chdir_r
a850: 65 74 2c 20 64 75 70 32 5f 72 65 74 3b 0a 09 69  et, dup2_ret;..i
a860: 6e 74 20 66 64 5f 69 6e 2c 20 66 64 5f 6f 75 74  nt fd_in, fd_out
a870: 3b 0a 0a 09 63 68 64 69 72 5f 72 65 74 20 3d 20  ;...chdir_ret = 
a880: 63 68 64 69 72 28 22 2f 22 29 3b 0a 09 69 66 20  chdir("/");..if 
a890: 28 63 68 64 69 72 5f 72 65 74 20 21 3d 20 30 29  (chdir_ret != 0)
a8a0: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a   {...return(1);.
a8b0: 09 7d 0a 0a 09 66 6f 72 6b 5f 72 65 74 20 3d 20  .}...fork_ret = 
a8c0: 66 6f 72 6b 28 29 3b 0a 09 69 66 20 28 66 6f 72  fork();..if (for
a8d0: 6b 5f 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 72  k_ret < 0) {...r
a8e0: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69  eturn(1);..}...i
a8f0: 66 20 28 66 6f 72 6b 5f 72 65 74 20 3e 20 30 29  f (fork_ret > 0)
a900: 20 7b 0a 09 09 2f 2a 20 50 61 72 65 6e 74 20 2a   {.../* Parent *
a910: 2f 0a 09 09 77 61 69 74 70 69 64 28 66 6f 72 6b  /...waitpid(fork
a920: 5f 72 65 74 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a  _ret, NULL, 0);.
a930: 0a 09 09 65 78 69 74 28 45 58 49 54 5f 53 55 43  ...exit(EXIT_SUC
a940: 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43  CESS);..}.../* C
a950: 68 69 6c 64 20 2a 2f 0a 09 69 66 20 28 66 6f 72  hild */..if (for
a960: 6b 28 29 20 21 3d 20 30 29 20 7b 0a 09 09 2f 2a  k() != 0) {.../*
a970: 20 43 68 69 6c 64 20 2a 2f 0a 09 09 65 78 69 74   Child */...exit
a980: 28 45 58 49 54 5f 53 55 43 43 45 53 53 29 3b 0a  (EXIT_SUCCESS);.
a990: 09 7d 0a 0a 09 2f 2a 20 47 72 61 6e 64 20 63 68  .}.../* Grand ch
a9a0: 69 6c 64 20 2a 2f 0a 09 73 65 74 73 69 64 5f 72  ild */..setsid_r
a9b0: 65 74 20 3d 20 73 65 74 73 69 64 28 29 3b 0a 09  et = setsid();..
a9c0: 69 66 20 28 73 65 74 73 69 64 5f 72 65 74 20 3d  if (setsid_ret =
a9d0: 3d 20 28 28 70 69 64 5f 74 29 20 2d 31 29 29 20  = ((pid_t) -1)) 
a9e0: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  {...return(1);..
a9f0: 7d 0a 0a 09 66 64 5f 69 6e 20 3d 20 6f 70 65 6e  }...fd_in = open
aa00: 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f  ("/dev/null", O_
aa10: 52 44 4f 4e 4c 59 29 3b 0a 09 66 64 5f 6f 75 74  RDONLY);..fd_out
aa20: 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75   = open("/dev/nu
aa30: 6c 6c 22 2c 20 4f 5f 57 52 4f 4e 4c 59 29 3b 0a  ll", O_WRONLY);.
aa40: 09 69 66 20 28 66 64 5f 69 6e 20 3c 20 30 20 7c  .if (fd_in < 0 |
aa50: 7c 20 66 64 5f 6f 75 74 20 3c 20 30 29 20 7b 0a  | fd_out < 0) {.
aa60: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a  ..return(1);..}.
aa70: 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70  ..dup2_ret = dup
aa80: 32 28 66 64 5f 69 6e 2c 20 53 54 44 49 4e 5f 46  2(fd_in, STDIN_F
aa90: 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70  ILENO);..if (dup
aaa0: 32 5f 72 65 74 20 21 3d 20 53 54 44 49 4e 5f 46  2_ret != STDIN_F
aab0: 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72  ILENO) {...retur
aac0: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f  n(1);..}...dup2_
aad0: 72 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f 75  ret = dup2(fd_ou
aae0: 74 2c 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f  t, STDOUT_FILENO
aaf0: 29 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74  );..if (dup2_ret
ab00: 20 21 3d 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e   != STDOUT_FILEN
ab10: 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29  O) {...return(1)
ab20: 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20  ;..}...dup2_ret 
ab30: 3d 20 64 75 70 32 28 66 64 5f 6f 75 74 2c 20 53  = dup2(fd_out, S
ab40: 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29 3b 0a 09  TDERR_FILENO);..
ab50: 69 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20  if (dup2_ret != 
ab60: 53 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29 20 7b  STDERR_FILENO) {
ab70: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d  ...return(1);..}
ab80: 0a 0a 09 63 6c 6f 73 65 28 66 64 5f 69 6e 29 3b  ...close(fd_in);
ab90: 0a 09 63 6c 6f 73 65 28 66 64 5f 6f 75 74 29 3b  ..close(fd_out);
aba0: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a  ...return(0);.}.
abb0: 0a 2f 2a 20 52 75 6e 20 70 72 6f 63 65 73 73 20  ./* Run process 
abc0: 2a 2f 0a 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20  */.int main(int 
abd0: 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67  argc, char **arg
abe0: 76 29 20 7b 0a 09 73 74 72 75 63 74 20 6f 70 74  v) {..struct opt
abf0: 69 6f 6e 20 6f 70 74 69 6f 6e 73 5b 31 32 5d 3b  ion options[12];
ac00: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f  ..struct filed_o
ac10: 70 74 69 6f 6e 73 20 74 68 72 65 61 64 5f 6f 70  ptions thread_op
ac20: 74 69 6f 6e 73 20 3d 20 7b 30 7d 3b 0a 09 63 6f  tions = {0};..co
ac30: 6e 73 74 20 63 68 61 72 20 2a 62 69 6e 64 5f 61  nst char *bind_a
ac40: 64 64 72 20 3d 20 42 49 4e 44 5f 41 44 44 52 2c  ddr = BIND_ADDR,
ac50: 20 2a 6e 65 77 72 6f 6f 74 20 3d 20 4e 55 4c 4c   *newroot = NULL
ac60: 2c 20 2a 6c 6f 67 5f 66 69 6c 65 20 3d 20 4c 4f  , *log_file = LO
ac70: 47 5f 46 49 4c 45 3b 0a 09 46 49 4c 45 20 2a 6c  G_FILE;..FILE *l
ac80: 6f 67 5f 66 70 3b 0a 09 75 69 64 5f 74 20 75 73  og_fp;..uid_t us
ac90: 65 72 20 3d 20 30 3b 0a 09 69 6e 74 20 70 6f 72  er = 0;..int por
aca0: 74 20 3d 20 50 4f 52 54 2c 20 74 68 72 65 61 64  t = PORT, thread
acb0: 5f 63 6f 75 6e 74 20 3d 20 54 48 52 45 41 44 5f  _count = THREAD_
acc0: 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 61 63 68  COUNT;..int cach
acd0: 65 5f 73 69 7a 65 20 3d 20 43 41 43 48 45 5f 53  e_size = CACHE_S
ace0: 49 5a 45 3b 0a 09 69 6e 74 20 69 6e 69 74 5f 72  IZE;..int init_r
acf0: 65 74 2c 20 63 68 72 6f 6f 74 5f 72 65 74 2c 20  et, chroot_ret, 
ad00: 73 65 74 75 69 64 5f 72 65 74 2c 20 6c 6f 6f 6b  setuid_ret, look
ad10: 75 70 5f 72 65 74 2c 20 63 68 64 69 72 5f 72 65  up_ret, chdir_re
ad20: 74 3b 0a 09 69 6e 74 20 73 65 74 75 69 64 5f 65  t;..int setuid_e
ad30: 6e 61 62 6c 65 64 20 3d 20 30 2c 20 64 61 65 6d  nabled = 0, daem
ad40: 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 30 3b 0a  on_enabled = 0;.
ad50: 09 69 6e 74 20 63 68 3b 0a 09 69 6e 74 20 66 64  .int ch;..int fd
ad60: 3b 0a 0a 09 2f 2a 20 50 72 6f 63 65 73 73 20 61  ;.../* Process a
ad70: 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 66 69 6c  rguments */..fil
ad80: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73  ed_getopt_long_s
ad90: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 30  etopt(&options[0
ada0: 5d 2c 20 22 70 6f 72 74 22 2c 20 72 65 71 75 69  ], "port", requi
adb0: 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 70  red_argument, 'p
adc0: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70  ');..filed_getop
add0: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f  t_long_setopt(&o
ade0: 70 74 69 6f 6e 73 5b 31 5d 2c 20 22 74 68 72 65  ptions[1], "thre
adf0: 61 64 73 22 2c 20 72 65 71 75 69 72 65 64 5f 61  ads", required_a
ae00: 72 67 75 6d 65 6e 74 2c 20 27 74 27 29 3b 0a 09  rgument, 't');..
ae10: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e  filed_getopt_lon
ae20: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e  g_setopt(&option
ae30: 73 5b 32 5d 2c 20 22 63 61 63 68 65 22 2c 20 72  s[2], "cache", r
ae40: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74  equired_argument
ae50: 2c 20 27 63 27 29 3b 0a 09 66 69 6c 65 64 5f 67  , 'c');..filed_g
ae60: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70  etopt_long_setop
ae70: 74 28 26 6f 70 74 69 6f 6e 73 5b 33 5d 2c 20 22  t(&options[3], "
ae80: 62 69 6e 64 22 2c 20 72 65 71 75 69 72 65 64 5f  bind", required_
ae90: 61 72 67 75 6d 65 6e 74 2c 20 27 62 27 29 3b 0a  argument, 'b');.
aea0: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f  .filed_getopt_lo
aeb0: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f  ng_setopt(&optio
aec0: 6e 73 5b 34 5d 2c 20 22 75 73 65 72 22 2c 20 72  ns[4], "user", r
aed0: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74  equired_argument
aee0: 2c 20 27 75 27 29 3b 0a 09 66 69 6c 65 64 5f 67  , 'u');..filed_g
aef0: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70  etopt_long_setop
af00: 74 28 26 6f 70 74 69 6f 6e 73 5b 35 5d 2c 20 22  t(&options[5], "
af10: 72 6f 6f 74 22 2c 20 72 65 71 75 69 72 65 64 5f  root", required_
af20: 61 72 67 75 6d 65 6e 74 2c 20 27 72 27 29 3b 0a  argument, 'r');.
af30: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f  .filed_getopt_lo
af40: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f  ng_setopt(&optio
af50: 6e 73 5b 36 5d 2c 20 22 68 65 6c 70 22 2c 20 6e  ns[6], "help", n
af60: 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 68 27 29  o_argument, 'h')
af70: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f  ;..filed_getopt_
af80: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74  long_setopt(&opt
af90: 69 6f 6e 73 5b 37 5d 2c 20 22 64 61 65 6d 6f 6e  ions[7], "daemon
afa0: 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20  ", no_argument, 
afb0: 27 64 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74  'd');..filed_get
afc0: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28  opt_long_setopt(
afd0: 26 6f 70 74 69 6f 6e 73 5b 38 5d 2c 20 22 6c 6f  &options[8], "lo
afe0: 67 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67  g", required_arg
aff0: 75 6d 65 6e 74 2c 20 27 6c 27 29 3b 0a 09 66 69  ument, 'l');..fi
b000: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f  led_getopt_long_
b010: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b  setopt(&options[
b020: 39 5d 2c 20 22 76 65 72 73 69 6f 6e 22 2c 20 6e  9], "version", n
b030: 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 76 27 29  o_argument, 'v')
b040: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f  ;..filed_getopt_
b050: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74  long_setopt(&opt
b060: 69 6f 6e 73 5b 31 30 5d 2c 20 22 76 68 6f 73 74  ions[10], "vhost
b070: 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20  ", no_argument, 
b080: 27 56 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74  'V');..filed_get
b090: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28  opt_long_setopt(
b0a0: 26 6f 70 74 69 6f 6e 73 5b 31 31 5d 2c 20 4e 55  &options[11], NU
b0b0: 4c 4c 2c 20 30 2c 20 30 29 3b 0a 09 77 68 69 6c  LL, 0, 0);..whil
b0c0: 65 20 28 28 63 68 20 3d 20 67 65 74 6f 70 74 5f  e ((ch = getopt_
b0d0: 6c 6f 6e 67 28 61 72 67 63 2c 20 61 72 67 76 2c  long(argc, argv,
b0e0: 20 22 70 3a 74 3a 63 3a 62 3a 75 3a 72 3a 6c 3a   "p:t:c:b:u:r:l:
b0f0: 68 64 76 56 22 2c 20 6f 70 74 69 6f 6e 73 2c 20  hdvV", options, 
b100: 4e 55 4c 4c 29 29 20 21 3d 20 2d 31 29 20 7b 0a  NULL)) != -1) {.
b110: 09 09 73 77 69 74 63 68 28 63 68 29 20 7b 0a 09  ..switch(ch) {..
b120: 09 09 63 61 73 65 20 27 70 27 3a 0a 09 09 09 09  ..case 'p':.....
b130: 70 6f 72 74 20 3d 20 61 74 6f 69 28 6f 70 74 61  port = atoi(opta
b140: 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a  rg);.....break;.
b150: 09 09 09 63 61 73 65 20 27 74 27 3a 0a 09 09 09  ...case 't':....
b160: 09 74 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d 20  .thread_count = 
b170: 61 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09  atoi(optarg);...
b180: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65  ..break;....case
b190: 20 27 63 27 3a 0a 09 09 09 09 63 61 63 68 65 5f   'c':.....cache_
b1a0: 73 69 7a 65 20 3d 20 61 74 6f 69 28 6f 70 74 61  size = atoi(opta
b1b0: 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a  rg);.....break;.
b1c0: 09 09 09 63 61 73 65 20 27 62 27 3a 0a 09 09 09  ...case 'b':....
b1d0: 09 62 69 6e 64 5f 61 64 64 72 20 3d 20 73 74 72  .bind_addr = str
b1e0: 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09  dup(optarg);....
b1f0: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20  .break;....case 
b200: 27 75 27 3a 0a 09 09 09 09 73 65 74 75 69 64 5f  'u':.....setuid_
b210: 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09  enabled = 1;....
b220: 09 6c 6f 6f 6b 75 70 5f 72 65 74 20 3d 20 66 69  .lookup_ret = fi
b230: 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28  led_user_lookup(
b240: 6f 70 74 61 72 67 2c 20 26 75 73 65 72 29 3b 0a  optarg, &user);.
b250: 09 09 09 09 69 66 20 28 6c 6f 6f 6b 75 70 5f 72  ....if (lookup_r
b260: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 09 09  et != 0) {......
b270: 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70  filed_print_help
b280: 28 73 74 64 65 72 72 2c 20 30 2c 20 22 49 6e 76  (stderr, 0, "Inv
b290: 61 6c 69 64 20 75 73 65 72 6e 61 6d 65 20 73 70  alid username sp
b2a0: 65 63 69 66 69 65 64 22 29 3b 0a 0a 09 09 09 09  ecified");......
b2b0: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 09 09  .return(1);.....
b2c0: 7d 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09  }.....break;....
b2d0: 63 61 73 65 20 27 72 27 3a 0a 09 09 09 09 66 69  case 'r':.....fi
b2e0: 6c 65 64 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61  led_path_transla
b2f0: 74 65 5f 73 65 74 5f 72 6f 6f 74 28 6e 65 77 72  te_set_root(newr
b300: 6f 6f 74 2c 20 26 74 68 72 65 61 64 5f 6f 70 74  oot, &thread_opt
b310: 69 6f 6e 73 2c 20 6f 70 74 61 72 67 29 3b 0a 09  ions, optarg);..
b320: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73  ...break;....cas
b330: 65 20 27 6c 27 3a 0a 09 09 09 09 6c 6f 67 5f 66  e 'l':.....log_f
b340: 69 6c 65 20 3d 20 73 74 72 64 75 70 28 6f 70 74  ile = strdup(opt
b350: 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b  arg);.....break;
b360: 0a 09 09 09 63 61 73 65 20 27 64 27 3a 0a 09 09  ....case 'd':...
b370: 09 09 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64  ..daemon_enabled
b380: 20 3d 20 31 3b 0a 09 09 09 09 62 72 65 61 6b 3b   = 1;.....break;
b390: 0a 09 09 09 63 61 73 65 20 27 56 27 3a 0a 09 09  ....case 'V':...
b3a0: 09 09 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73  ..thread_options
b3b0: 2e 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 64 20  .vhosts_enabled 
b3c0: 3d 20 31 3b 0a 0a 09 09 09 09 62 72 65 61 6b 3b  = 1;......break;
b3d0: 0a 09 09 09 63 61 73 65 20 27 76 27 3a 0a 09 09  ....case 'v':...
b3e0: 09 09 70 72 69 6e 74 66 28 22 66 69 6c 65 64 20  ..printf("filed 
b3f0: 76 65 72 73 69 6f 6e 20 25 73 5c 6e 22 2c 20 46  version %s\n", F
b400: 49 4c 45 44 5f 56 45 52 53 49 4f 4e 29 3b 0a 0a  ILED_VERSION);..
b410: 09 09 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09  ....return(0);..
b420: 09 09 63 61 73 65 20 27 3f 27 3a 0a 09 09 09 63  ..case '?':....c
b430: 61 73 65 20 27 3a 27 3a 0a 09 09 09 09 66 69 6c  ase ':':.....fil
b440: 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73 74  ed_print_help(st
b450: 64 65 72 72 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a  derr, 0, NULL);.
b460: 0a 09 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a  .....return(1);.
b470: 09 09 09 63 61 73 65 20 27 68 27 3a 0a 09 09 09  ...case 'h':....
b480: 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c  .filed_print_hel
b490: 70 28 73 74 64 6f 75 74 2c 20 31 2c 20 4e 55 4c  p(stdout, 1, NUL
b4a0: 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28  L);......return(
b4b0: 30 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20  0);...}..}.../* 
b4c0: 4f 70 65 6e 20 6c 6f 67 20 66 69 6c 65 20 2a 2f  Open log file */
b4d0: 0a 09 6c 6f 67 5f 66 70 20 3d 20 66 69 6c 65 64  ..log_fp = filed
b4e0: 5f 6c 6f 67 5f 6f 70 65 6e 28 6c 6f 67 5f 66 69  _log_open(log_fi
b4f0: 6c 65 29 3b 0a 09 69 66 20 28 6c 6f 67 5f 66 70  le);..if (log_fp
b500: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 70 65   == NULL) {...pe
b510: 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 6f 67 5f  rror("filed_log_
b520: 6f 70 65 6e 22 29 3b 0a 0a 09 09 72 65 74 75 72  open");....retur
b530: 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72  n(4);..}.../* Cr
b540: 65 61 74 65 20 6c 69 73 74 65 6e 69 6e 67 20 73  eate listening s
b550: 6f 63 6b 65 74 20 2a 2f 0a 09 66 64 20 3d 20 66  ocket */..fd = f
b560: 69 6c 65 64 5f 6c 69 73 74 65 6e 28 62 69 6e 64  iled_listen(bind
b570: 5f 61 64 64 72 2c 20 70 6f 72 74 29 3b 0a 09 69  _addr, port);..i
b580: 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 70  f (fd < 0) {...p
b590: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 69 73  error("filed_lis
b5a0: 74 65 6e 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e  ten");....return
b5b0: 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69  (1);..}.../* Ini
b5c0: 74 69 61 6c 69 7a 65 20 74 69 6d 65 6f 75 74 20  tialize timeout 
b5d0: 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 09 69  structures */..i
b5e0: 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f  nit_ret = filed_
b5f0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e  sockettimeout_in
b600: 69 74 28 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f  it();..if (init_
b610: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65  ret != 0) {...pe
b620: 72 72 6f 72 28 22 66 69 6c 65 64 5f 73 6f 63 6b  rror("filed_sock
b630: 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69 74 22 29  ettimeout_init")
b640: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 38 29 3b 0a  ;....return(8);.
b650: 09 7d 0a 0a 09 2f 2a 20 42 65 63 6f 6d 65 20 61  .}.../* Become a
b660: 20 64 61 65 6d 6f 6e 20 2a 2f 0a 09 69 66 20 28   daemon */..if (
b670: 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 29 20  daemon_enabled) 
b680: 7b 0a 09 09 69 6e 69 74 5f 72 65 74 20 3d 20 66  {...init_ret = f
b690: 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 29  iled_daemonize()
b6a0: 3b 0a 09 09 69 66 20 28 69 6e 69 74 5f 72 65 74  ;...if (init_ret
b6b0: 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72   != 0) {....perr
b6c0: 6f 72 28 22 66 69 6c 65 64 5f 64 61 65 6d 6f 6e  or("filed_daemon
b6d0: 69 7a 65 22 29 3b 0a 0a 09 09 09 72 65 74 75 72  ize");.....retur
b6e0: 6e 28 36 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f  n(6);...}..}.../
b6f0: 2a 20 43 68 72 6f 6f 74 2c 20 69 66 20 61 70 70  * Chroot, if app
b700: 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09 69 66 20  ropriate */..if 
b710: 28 6e 65 77 72 6f 6f 74 29 20 7b 0a 09 09 63 68  (newroot) {...ch
b720: 64 69 72 5f 72 65 74 20 3d 20 63 68 64 69 72 28  dir_ret = chdir(
b730: 6e 65 77 72 6f 6f 74 29 3b 0a 09 09 69 66 20 28  newroot);...if (
b740: 63 68 64 69 72 5f 72 65 74 20 21 3d 20 30 29 20  chdir_ret != 0) 
b750: 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 63 68 64  {....perror("chd
b760: 69 72 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e  ir");.....return
b770: 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 63 68 72 6f  (1);...}....chro
b780: 6f 74 5f 72 65 74 20 3d 20 63 68 72 6f 6f 74 28  ot_ret = chroot(
b790: 22 2e 22 29 3b 0a 09 09 69 66 20 28 63 68 72 6f  ".");...if (chro
b7a0: 6f 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09  ot_ret != 0) {..
b7b0: 09 09 70 65 72 72 6f 72 28 22 63 68 72 6f 6f 74  ..perror("chroot
b7c0: 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31  ");.....return(1
b7d0: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44  );...}..}.../* D
b7e0: 72 6f 70 20 70 72 69 76 69 6c 65 67 65 73 2c 20  rop privileges, 
b7f0: 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a  if appropriate *
b800: 2f 0a 09 69 66 20 28 73 65 74 75 69 64 5f 65 6e  /..if (setuid_en
b810: 61 62 6c 65 64 29 20 7b 0a 09 09 73 65 74 75 69  abled) {...setui
b820: 64 5f 72 65 74 20 3d 20 73 65 74 75 69 64 28 75  d_ret = setuid(u
b830: 73 65 72 29 3b 0a 09 09 69 66 20 28 73 65 74 75  ser);...if (setu
b840: 69 64 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09  id_ret != 0) {..
b850: 09 09 70 65 72 72 6f 72 28 22 73 65 74 75 69 64  ..perror("setuid
b860: 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31  ");.....return(1
b870: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 49  );...}..}.../* I
b880: 6e 69 74 69 61 6c 69 7a 65 20 2a 2f 0a 09 69 6e  nitialize */..in
b890: 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 69  it_ret = filed_i
b8a0: 6e 69 74 28 63 61 63 68 65 5f 73 69 7a 65 29 3b  nit(cache_size);
b8b0: 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21  ..if (init_ret !
b8c0: 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28  = 0) {...perror(
b8d0: 22 66 69 6c 65 64 5f 69 6e 69 74 22 29 3b 0a 0a  "filed_init");..
b8e0: 09 09 72 65 74 75 72 6e 28 33 29 3b 0a 09 7d 0a  ..return(3);..}.
b8f0: 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 6f 67 67  ../* Create logg
b900: 69 6e 67 20 74 68 72 65 61 64 20 2a 2f 0a 09 69  ing thread */..i
b910: 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f  nit_ret = filed_
b920: 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 69  logging_thread_i
b930: 6e 69 74 28 6c 6f 67 5f 66 70 29 3b 0a 09 69 66  nit(log_fp);..if
b940: 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29   (init_ret != 0)
b950: 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c   {...perror("fil
b960: 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61  ed_logging_threa
b970: 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74  d_init");....ret
b980: 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20  urn(4);..}.../* 
b990: 43 72 65 61 74 65 20 73 6f 63 6b 65 74 20 74 65  Create socket te
b9a0: 72 6d 69 6e 61 74 69 6f 6e 20 74 68 72 65 61 64  rmination thread
b9b0: 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20   */..init_ret = 
b9c0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
b9d0: 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e 69 74 28  out_thread_init(
b9e0: 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74  );..if (init_ret
b9f0: 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f   != 0) {...perro
ba00: 72 28 22 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  r("filed_sockett
ba10: 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e  imeout_thread_in
ba20: 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28  it");....return(
ba30: 37 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61  7);..}.../* Crea
ba40: 74 65 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64  te worker thread
ba50: 73 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d  s */..init_ret =
ba60: 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68   filed_worker_th
ba70: 72 65 61 64 73 5f 69 6e 69 74 28 66 64 2c 20 74  reads_init(fd, t
ba80: 68 72 65 61 64 5f 63 6f 75 6e 74 2c 20 26 74 68  hread_count, &th
ba90: 72 65 61 64 5f 6f 70 74 69 6f 6e 73 29 3b 0a 09  read_options);..
baa0: 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20  if (init_ret != 
bab0: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66  0) {...perror("f
bac0: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65  iled_worker_thre
bad0: 61 64 73 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72  ads_init");....r
bae0: 65 74 75 72 6e 28 35 29 3b 0a 09 7d 0a 0a 09 2f  eturn(5);..}.../
baf0: 2a 20 57 61 69 74 20 66 6f 72 20 74 68 72 65 61  * Wait for threa
bb00: 64 73 20 74 6f 20 65 78 69 74 20 2a 2f 0a 09 2f  ds to exit */../
bb10: 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 4d 6f 6e 69  * XXX:TODO: Moni
bb20: 74 6f 72 20 74 68 72 65 61 64 20 75 73 61 67 65  tor thread usage
bb30: 20 2a 2f 0a 09 77 68 69 6c 65 20 28 31 29 20 7b   */..while (1) {
bb40: 0a 09 09 73 6c 65 65 70 28 38 36 34 30 30 29 3b  ...sleep(86400);
bb50: 0a 09 7d 0a 0a 09 2f 2a 20 52 65 74 75 72 6e 20  ..}.../* Return 
bb60: 69 6e 20 66 61 69 6c 75 72 65 20 2a 2f 0a 09 72  in failure */..r
bb70: 65 74 75 72 6e 28 32 29 3b 0a 7d 0a              eturn(2);.}.