Hex Artifact Content

Artifact 73ad6aa47cc014e70c7a52154eb92dbd6a0677e1:


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 31 39 22 0a 23  VERSION "1.19".#
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 73 69 7a 65 5f 74 20 72 65 61   0;..ssize_t rea
1cf0: 64 5f 72 65 74 20 3d 20 30 3b 0a 09 75 6e 73 69  d_ret = 0;..unsi
1d00: 67 6e 65 64 20 69 6e 74 20 72 61 6e 64 6f 6d 5f  gned int random_
1d10: 76 61 6c 75 65 20 3d 20 30 3b 0a 09 69 6e 74 20  value = 0;..int 
1d20: 63 61 63 68 65 5f 72 65 74 3b 0a 09 69 6e 74 20  cache_ret;..int 
1d30: 72 61 6e 64 6f 6d 5f 66 64 3b 0a 0a 09 69 66 20  random_fd;...if 
1d40: 28 63 61 6c 6c 65 64 29 20 7b 0a 09 09 72 65 74  (called) {...ret
1d50: 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 63 61 6c  urn(0);..}...cal
1d60: 6c 65 64 20 3d 20 31 3b 0a 0a 09 2f 2a 20 41 74  led = 1;.../* At
1d70: 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 61 6c  tempt to lock al
1d80: 6c 20 6d 65 6d 6f 72 79 20 74 6f 20 70 68 79 73  l memory to phys
1d90: 69 63 61 6c 20 52 41 4d 20 28 62 75 74 20 64 6f  ical RAM (but do
1da0: 6e 27 74 20 63 61 72 65 20 69 66 20 77 65 20 63  n't care if we c
1db0: 61 6e 27 74 29 20 2a 2f 0a 09 6d 6c 6f 63 6b 61  an't) */..mlocka
1dc0: 6c 6c 28 4d 43 4c 5f 43 55 52 52 45 4e 54 20 7c  ll(MCL_CURRENT |
1dd0: 20 4d 43 4c 5f 46 55 54 55 52 45 29 3b 0a 0a 09   MCL_FUTURE);...
1de0: 2f 2a 20 49 67 6e 6f 72 65 20 53 49 47 50 49 50  /* Ignore SIGPIP
1df0: 45 20 2a 2f 0a 09 73 69 67 6e 61 6c 28 53 49 47  E */..signal(SIG
1e00: 50 49 50 45 2c 20 53 49 47 5f 49 47 4e 29 3b 0a  PIPE, SIG_IGN);.
1e10: 0a 09 2f 2a 20 48 61 6e 64 6c 65 20 53 49 47 48  ../* Handle SIGH
1e20: 55 50 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c  UP to release al
1e30: 6c 20 63 61 63 68 65 73 20 2a 2f 0a 09 73 69 67  l caches */..sig
1e40: 6e 61 6c 28 53 49 47 48 55 50 2c 20 66 69 6c 65  nal(SIGHUP, file
1e50: 64 5f 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72  d_signal_handler
1e60: 29 3b 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69  );.../* Initiali
1e70: 7a 65 20 63 61 63 68 65 20 73 74 72 75 63 74 75  ze cache structu
1e80: 72 65 20 2a 2f 0a 09 63 61 63 68 65 5f 72 65 74  re */..cache_ret
1e90: 20 3d 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61   = filed_init_ca
1ea0: 63 68 65 28 63 61 63 68 65 5f 73 69 7a 65 29 3b  che(cache_size);
1eb0: 0a 09 69 66 20 28 63 61 63 68 65 5f 72 65 74 20  ..if (cache_ret 
1ec0: 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e  != 0) {...return
1ed0: 28 63 61 63 68 65 5f 72 65 74 29 3b 0a 09 7d 0a  (cache_ret);..}.
1ee0: 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ../* Initialize 
1ef0: 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65  random number ge
1f00: 6e 65 72 61 74 6f 72 20 2a 2f 0a 09 72 61 6e 64  nerator */..rand
1f10: 6f 6d 5f 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64  om_fd = open("/d
1f20: 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52  ev/urandom", O_R
1f30: 44 4f 4e 4c 59 29 3b 0a 09 69 66 20 28 72 61 6e  DONLY);..if (ran
1f40: 64 6f 6d 5f 66 64 20 3e 3d 20 30 29 20 7b 0a 09  dom_fd >= 0) {..
1f50: 09 72 65 61 64 5f 72 65 74 20 3d 20 72 65 61 64  .read_ret = read
1f60: 28 72 61 6e 64 6f 6d 5f 66 64 2c 20 26 72 61 6e  (random_fd, &ran
1f70: 64 6f 6d 5f 76 61 6c 75 65 2c 20 73 69 7a 65 6f  dom_value, sizeo
1f80: 66 28 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 29 29  f(random_value))
1f90: 3b 0a 0a 09 09 63 6c 6f 73 65 28 72 61 6e 64 6f  ;....close(rando
1fa0: 6d 5f 66 64 29 3b 0a 09 7d 0a 0a 09 72 61 6e 64  m_fd);..}...rand
1fb0: 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 67 65 74 70  om_value ^= getp
1fc0: 69 64 28 29 3b 0a 09 72 61 6e 64 6f 6d 5f 76 61  id();..random_va
1fd0: 6c 75 65 20 5e 3d 20 67 65 74 75 69 64 28 29 3b  lue ^= getuid();
1fe0: 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e  ..random_value ^
1ff0: 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09  = time(NULL);...
2000: 73 72 61 6e 64 6f 6d 28 72 61 6e 64 6f 6d 5f 76  srandom(random_v
2010: 61 6c 75 65 29 3b 0a 0a 09 72 65 74 75 72 6e 28  alue);...return(
2020: 30 29 3b 0a 0a 09 2f 2a 20 4e 4f 54 52 45 41 43  0);.../* NOTREAC
2030: 48 3a 20 52 65 61 64 20 6d 61 79 20 66 61 69 6c  H: Read may fail
2040: 20 6f 72 20 73 75 63 63 65 65 64 2c 20 77 65 20   or succeed, we 
2050: 64 6f 6e 27 74 20 61 63 74 75 61 6c 6c 79 20 63  don't actually c
2060: 61 72 65 20 2a 2f 0a 09 72 65 61 64 5f 72 65 74  are */..read_ret
2070: 20 3d 20 72 65 61 64 5f 72 65 74 3b 0a 7d 0a 0a   = read_ret;.}..
2080: 2f 2a 20 4c 69 73 74 65 6e 20 6f 6e 20 61 20 70  /* Listen on a p
2090: 61 72 74 69 63 75 6c 61 72 20 61 64 64 72 65 73  articular addres
20a0: 73 2f 70 6f 72 74 20 2a 2f 0a 73 74 61 74 69 63  s/port */.static
20b0: 20 69 6e 74 20 66 69 6c 65 64 5f 6c 69 73 74 65   int filed_liste
20c0: 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 64  n(const char *ad
20d0: 64 72 65 73 73 2c 20 75 6e 73 69 67 6e 65 64 20  dress, unsigned 
20e0: 69 6e 74 20 70 6f 72 74 29 20 7b 0a 09 73 74 72  int port) {..str
20f0: 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36  uct sockaddr_in6
2100: 20 61 64 64 72 5f 76 36 3b 0a 09 73 74 72 75 63   addr_v6;..struc
2110: 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 61 64  t sockaddr_in ad
2120: 64 72 5f 76 34 3b 0a 09 73 74 72 75 63 74 20 73  dr_v4;..struct s
2130: 6f 63 6b 61 64 64 72 20 2a 61 64 64 72 3b 0a 09  ockaddr *addr;..
2140: 73 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 72 5f 6c  socklen_t addr_l
2150: 65 6e 3b 0a 09 69 6e 74 20 70 74 6f 6e 5f 72 65  en;..int pton_re
2160: 74 2c 20 62 69 6e 64 5f 72 65 74 2c 20 6c 69 73  t, bind_ret, lis
2170: 74 65 6e 5f 72 65 74 3b 0a 09 69 6e 74 20 66 61  ten_ret;..int fa
2180: 6d 69 6c 79 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a  mily;..int fd;..
2190: 09 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45  .family = AF_INE
21a0: 54 36 3b 0a 09 70 74 6f 6e 5f 72 65 74 20 3d 20  T6;..pton_ret = 
21b0: 69 6e 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79  inet_pton(family
21c0: 2c 20 61 64 64 72 65 73 73 2c 20 26 61 64 64 72  , address, &addr
21d0: 5f 76 36 2e 73 69 6e 36 5f 61 64 64 72 2e 73 36  _v6.sin6_addr.s6
21e0: 5f 61 64 64 72 29 3b 0a 09 69 66 20 28 70 74 6f  _addr);..if (pto
21f0: 6e 5f 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09  n_ret != 1) {...
2200: 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54  family = AF_INET
2210: 3b 0a 09 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69  ;...pton_ret = i
2220: 6e 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c  net_pton(family,
2230: 20 61 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f   address, &addr_
2240: 76 34 2e 73 69 6e 5f 61 64 64 72 2e 73 5f 61 64  v4.sin_addr.s_ad
2250: 64 72 29 3b 0a 09 09 69 66 20 28 70 74 6f 6e 5f  dr);...if (pton_
2260: 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 09 72  ret != 1) {....r
2270: 65 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 0a  eturn(-1);...}..
2280: 09 09 61 64 64 72 5f 76 34 2e 73 69 6e 5f 66 61  ..addr_v4.sin_fa
2290: 6d 69 6c 79 20 3d 20 66 61 6d 69 6c 79 3b 0a 09  mily = family;..
22a0: 09 61 64 64 72 5f 76 34 2e 73 69 6e 5f 70 6f 72  .addr_v4.sin_por
22b0: 74 20 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b  t = htons(port);
22c0: 0a 0a 09 09 61 64 64 72 20 3d 20 28 73 74 72 75  ....addr = (stru
22d0: 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26  ct sockaddr *) &
22e0: 61 64 64 72 5f 76 34 3b 0a 09 09 61 64 64 72 5f  addr_v4;...addr_
22f0: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64  len = sizeof(add
2300: 72 5f 76 34 29 3b 0a 09 7d 20 65 6c 73 65 20 7b  r_v4);..} else {
2310: 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f  ...addr_v6.sin6_
2320: 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54  family = AF_INET
2330: 36 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e  6;...addr_v6.sin
2340: 36 5f 66 6c 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a  6_flowinfo = 0;.
2350: 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 73  ..addr_v6.sin6_s
2360: 63 6f 70 65 5f 69 64 20 3d 20 30 3b 0a 09 09 61  cope_id = 0;...a
2370: 64 64 72 5f 76 36 2e 73 69 6e 36 5f 70 6f 72 74  ddr_v6.sin6_port
2380: 20 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a   = htons(port);.
2390: 0a 09 09 61 64 64 72 20 3d 20 28 73 74 72 75 63  ...addr = (struc
23a0: 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61  t sockaddr *) &a
23b0: 64 64 72 5f 76 36 3b 0a 09 09 61 64 64 72 5f 6c  ddr_v6;...addr_l
23c0: 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72  en = sizeof(addr
23d0: 5f 76 36 29 3b 0a 09 7d 0a 0a 09 66 64 20 3d 20  _v6);..}...fd = 
23e0: 73 6f 63 6b 65 74 28 66 61 6d 69 6c 79 2c 20 53  socket(family, S
23f0: 4f 43 4b 5f 53 54 52 45 41 4d 2c 20 30 29 3b 0a  OCK_STREAM, 0);.
2400: 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09  .if (fd < 0) {..
2410: 09 72 65 74 75 72 6e 28 66 64 29 3b 0a 09 7d 0a  .return(fd);..}.
2420: 0a 09 62 69 6e 64 5f 72 65 74 20 3d 20 62 69 6e  ..bind_ret = bin
2430: 64 28 66 64 2c 20 61 64 64 72 2c 20 61 64 64 72  d(fd, addr, addr
2440: 5f 6c 65 6e 29 3b 0a 09 69 66 20 28 62 69 6e 64  _len);..if (bind
2450: 5f 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 63 6c  _ret < 0) {...cl
2460: 6f 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74 75  ose(fd);....retu
2470: 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 6c 69 73  rn(-1);..}...lis
2480: 74 65 6e 5f 72 65 74 20 3d 20 6c 69 73 74 65 6e  ten_ret = listen
2490: 28 66 64 2c 20 31 32 38 29 3b 0a 09 69 66 20 28  (fd, 128);..if (
24a0: 6c 69 73 74 65 6e 5f 72 65 74 20 21 3d 20 30 29  listen_ret != 0)
24b0: 20 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a   {...close(fd);.
24c0: 0a 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09  ...return(-1);..
24d0: 7d 0a 0a 09 72 65 74 75 72 6e 28 66 64 29 3b 0a  }...return(fd);.
24e0: 7d 0a 0a 2f 2a 20 4c 6f 67 20 61 20 6d 65 73 73  }../* Log a mess
24f0: 61 67 65 20 2a 2f 0a 23 69 66 64 65 66 20 46 49  age */.#ifdef FI
2500: 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 23 20 20  LED_DONT_LOG.#  
2510: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67  define filed_log
2520: 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74  ging_thread_init
2530: 28 78 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20  (x) 0.#  define 
2540: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65  filed_log_msg_de
2550: 62 75 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f  bug(x, ...) /**/
2560: 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64  .#  define filed
2570: 5f 6c 6f 67 5f 6d 73 67 28 78 2c 20 2e 2e 2e 29  _log_msg(x, ...)
2580: 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20   /**/.#  define 
2590: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28  filed_log_entry(
25a0: 78 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e  x) /**/.#  defin
25b0: 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 78  e filed_log_ip(x
25c0: 2c 20 2e 2e 2e 29 20 4e 55 4c 4c 0a 23 20 20 64  , ...) NULL.#  d
25d0: 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f  efine filed_log_
25e0: 6e 65 77 28 78 29 20 26 6c 6f 63 61 6c 5f 64 75  new(x) &local_du
25f0: 6d 6d 79 5f 6c 6f 67 0a 23 20 20 64 65 66 69 6e  mmy_log.#  defin
2600: 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 66 72 65 65  e filed_log_free
2610: 28 78 29 20 2f 2a 2a 2f 0a 0a 2f 2a 20 52 65 74  (x) /**/../* Ret
2620: 75 72 6e 20 6c 6f 67 67 69 6e 67 20 68 61 6e 64  urn logging hand
2630: 6c 65 20 2a 2f 0a 73 74 61 74 69 63 20 46 49 4c  le */.static FIL
2640: 45 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65  E *filed_log_ope
2650: 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69  n(const char *fi
2660: 6c 65 29 20 7b 0a 09 72 65 74 75 72 6e 28 73 74  le) {..return(st
2670: 64 6f 75 74 29 3b 0a 09 66 69 6c 65 20 3d 20 66  dout);..file = f
2680: 69 6c 65 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 20  ile;.}.#else.#  
2690: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67  define filed_log
26a0: 5f 66 72 65 65 28 78 29 20 66 72 65 65 28 78 29  _free(x) free(x)
26b0: 0a 23 20 20 69 66 64 65 66 20 46 49 4c 45 44 5f  .#  ifdef FILED_
26c0: 44 45 42 55 47 0a 23 20 20 20 20 64 65 66 69 6e  DEBUG.#    defin
26d0: 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  e filed_log_msg_
26e0: 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20 7b 20  debug(x, ...) { 
26f0: 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
2700: 78 2c 20 5f 5f 56 41 5f 41 52 47 53 5f 5f 29 3b  x, __VA_ARGS__);
2710: 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
2720: 20 22 5c 6e 22 29 3b 20 66 66 6c 75 73 68 28 73   "\n"); fflush(s
2730: 74 64 65 72 72 29 3b 20 7d 0a 23 20 20 65 6c 73  tderr); }.#  els
2740: 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 66 69  e.#    define fi
2750: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75  led_log_msg_debu
2760: 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23  g(x, ...) /**/.#
2770: 20 20 65 6e 64 69 66 0a 0a 2f 2a 20 49 6e 69 74    endif../* Init
2780: 69 61 6c 69 7a 65 20 6c 6f 67 67 69 6e 67 20 74  ialize logging t
2790: 68 72 65 61 64 20 2a 2f 0a 73 74 61 74 69 63 20  hread */.static 
27a0: 76 6f 69 64 20 2a 66 69 6c 65 64 5f 6c 6f 67 67  void *filed_logg
27b0: 69 6e 67 5f 74 68 72 65 61 64 28 76 6f 69 64 20  ing_thread(void 
27c0: 2a 61 72 67 5f 70 29 20 7b 0a 09 73 74 72 75 63  *arg_p) {..struc
27d0: 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f  t filed_logging_
27e0: 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67  thread_args *arg
27f0: 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f  ;..struct filed_
2800: 6c 6f 67 5f 65 6e 74 72 79 20 2a 63 75 72 72 2c  log_entry *curr,
2810: 20 2a 70 72 65 76 3b 0a 09 63 6f 6e 73 74 20 63   *prev;..const c
2820: 68 61 72 20 2a 6d 65 74 68 6f 64 3b 0a 09 74 69  har *method;..ti
2830: 6d 65 5f 74 20 6e 6f 77 3b 0a 09 46 49 4c 45 20  me_t now;..FILE 
2840: 2a 66 70 3b 0a 0a 09 61 72 67 20 3d 20 61 72 67  *fp;...arg = arg
2850: 5f 70 3b 0a 0a 09 66 70 20 3d 20 61 72 67 2d 3e  _p;...fp = arg->
2860: 66 70 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29 20  fp;...while (1) 
2870: 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65  {...pthread_mute
2880: 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f  x_lock(&filed_lo
2890: 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78  g_msg_list_mutex
28a0: 29 3b 0a 09 09 70 74 68 72 65 61 64 5f 63 6f 6e  );...pthread_con
28b0: 64 5f 77 61 69 74 28 26 66 69 6c 65 64 5f 6c 6f  d_wait(&filed_lo
28c0: 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 79  g_msg_list_ready
28d0: 2c 20 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  , &filed_log_msg
28e0: 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09  _list_mutex);...
28f0: 09 63 75 72 72 20 3d 20 66 69 6c 65 64 5f 6c 6f  .curr = filed_lo
2900: 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a 09 09 66 69  g_msg_list;...fi
2910: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74  led_log_msg_list
2920: 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 09 70 74 68 72   = NULL;....pthr
2930: 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b  ead_mutex_unlock
2940: 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  (&filed_log_msg_
2950: 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09  list_mutex);....
2960: 6e 6f 77 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29  now = time(NULL)
2970: 3b 0a 0a 09 09 70 72 65 76 20 3d 20 4e 55 4c 4c  ;....prev = NULL
2980: 3b 0a 09 09 66 6f 72 20 28 3b 20 63 75 72 72 3b  ;...for (; curr;
2990: 20 63 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f 6e   curr = curr->_n
29a0: 65 78 74 29 20 7b 0a 09 09 09 63 75 72 72 2d 3e  ext) {....curr->
29b0: 5f 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 0a 09  _prev = prev;...
29c0: 09 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 09  ..prev = curr;..
29d0: 09 7d 0a 0a 09 09 63 75 72 72 20 3d 20 70 72 65  .}....curr = pre
29e0: 76 3b 0a 09 09 77 68 69 6c 65 20 28 63 75 72 72  v;...while (curr
29f0: 29 20 7b 0a 09 09 09 73 77 69 74 63 68 20 28 63  ) {....switch (c
2a00: 75 72 72 2d 3e 74 79 70 65 29 20 7b 0a 09 09 09  urr->type) {....
2a10: 09 63 61 73 65 20 46 49 4c 45 44 5f 4c 4f 47 5f  .case FILED_LOG_
2a20: 54 59 50 45 5f 4d 45 53 53 41 47 45 3a 0a 09 09  TYPE_MESSAGE:...
2a30: 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22  ...fprintf(fp, "
2a40: 25 73 22 2c 20 63 75 72 72 2d 3e 62 75 66 66 65  %s", curr->buffe
2a50: 72 29 3b 0a 0a 09 09 09 09 09 62 72 65 61 6b 3b  r);.......break;
2a60: 0a 09 09 09 09 63 61 73 65 20 46 49 4c 45 44 5f  .....case FILED_
2a70: 4c 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45  LOG_TYPE_TRANSFE
2a80: 52 3a 0a 09 09 09 09 09 73 77 69 74 63 68 20 28  R:......switch (
2a90: 63 75 72 72 2d 3e 6d 65 74 68 6f 64 29 20 7b 0a  curr->method) {.
2aa0: 09 09 09 09 09 09 63 61 73 65 20 46 49 4c 45 44  ......case FILED
2ab0: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f  _REQUEST_METHOD_
2ac0: 47 45 54 3a 0a 09 09 09 09 09 09 09 6d 65 74 68  GET:........meth
2ad0: 6f 64 3d 22 47 45 54 22 3b 0a 09 09 09 09 09 09  od="GET";.......
2ae0: 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 63 61  .break;.......ca
2af0: 73 65 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54  se FILED_REQUEST
2b00: 5f 4d 45 54 48 4f 44 5f 48 45 41 44 3a 0a 09 09  _METHOD_HEAD:...
2b10: 09 09 09 09 09 6d 65 74 68 6f 64 3d 22 48 45 41  .....method="HEA
2b20: 44 22 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b  D";........break
2b30: 3b 0a 09 09 09 09 09 09 64 65 66 61 75 6c 74 3a  ;.......default:
2b40: 0a 09 09 09 09 09 09 09 6d 65 74 68 6f 64 3d 22  ........method="
2b50: 3c 75 6e 6b 6e 6f 77 6e 3e 22 3b 0a 09 09 09 09  <unknown>";.....
2b60: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 7d  ...break;......}
2b70: 0a 0a 09 09 09 09 09 69 66 20 28 63 75 72 72 2d  .......if (curr-
2b80: 3e 65 6e 64 74 69 6d 65 20 3d 3d 20 28 28 74 69  >endtime == ((ti
2b90: 6d 65 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09 09  me_t) -1)) {....
2ba0: 09 09 09 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65  ...curr->endtime
2bb0: 20 3d 20 6e 6f 77 3b 0a 09 09 09 09 09 7d 0a 0a   = now;......}..
2bc0: 09 09 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c  .....fprintf(fp,
2bd0: 20 22 54 52 41 4e 53 46 45 52 20 4d 45 54 48 4f   "TRANSFER METHO
2be0: 44 3d 25 73 20 50 41 54 48 3d 25 73 20 53 52 43  D=%s PATH=%s SRC
2bf0: 3d 25 73 3a 25 69 20 54 49 4d 45 2e 53 54 41 52  =%s:%i TIME.STAR
2c00: 54 3d 25 6c 6c 75 20 54 49 4d 45 2e 45 4e 44 3d  T=%llu TIME.END=
2c10: 25 6c 6c 75 20 43 4f 44 45 2e 56 41 4c 55 45 3d  %llu CODE.VALUE=
2c20: 25 75 20 43 4f 44 45 2e 52 45 41 53 4f 4e 3d 25  %u CODE.REASON=%
2c30: 73 20 52 45 51 55 45 53 54 2e 4f 46 46 53 45 54  s REQUEST.OFFSET
2c40: 3d 25 6c 6c 75 20 52 45 51 55 45 53 54 2e 4c 45  =%llu REQUEST.LE
2c50: 4e 47 54 48 3d 25 6c 6c 75 20 46 49 4c 45 2e 4c  NGTH=%llu FILE.L
2c60: 45 4e 47 54 48 3d 25 6c 6c 75 20 54 52 41 4e 53  ENGTH=%llu TRANS
2c70: 46 45 52 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 22  FER.LENGTH=%llu"
2c80: 2c 0a 09 09 09 09 09 09 6d 65 74 68 6f 64 2c 0a  ,.......method,.
2c90: 09 09 09 09 09 09 63 75 72 72 2d 3e 62 75 66 66  ......curr->buff
2ca0: 65 72 2c 0a 09 09 09 09 09 09 63 75 72 72 2d 3e  er,.......curr->
2cb0: 69 70 2c 20 63 75 72 72 2d 3e 70 6f 72 74 2c 0a  ip, curr->port,.
2cc0: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ......(unsigned 
2cd0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d  long long) curr-
2ce0: 3e 73 74 61 72 74 74 69 6d 65 2c 0a 09 09 09 09  >starttime,.....
2cf0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
2d00: 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 65 6e 64   long) curr->end
2d10: 74 69 6d 65 2c 0a 09 09 09 09 09 09 63 75 72 72  time,.......curr
2d20: 2d 3e 68 74 74 70 5f 63 6f 64 65 2c 20 63 75 72  ->http_code, cur
2d30: 72 2d 3e 72 65 61 73 6f 6e 2c 0a 09 09 09 09 09  r->reason,......
2d40: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
2d50: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 72 65 71 5f  long) curr->req_
2d60: 6f 66 66 73 65 74 2c 0a 09 09 09 09 09 09 28 75  offset,.......(u
2d70: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
2d80: 67 29 20 63 75 72 72 2d 3e 72 65 71 5f 6c 65 6e  g) curr->req_len
2d90: 67 74 68 2c 0a 09 09 09 09 09 09 28 75 6e 73 69  gth,.......(unsi
2da0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
2db0: 63 75 72 72 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74  curr->file_lengt
2dc0: 68 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e  h,.......(unsign
2dd0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75  ed long long) cu
2de0: 72 72 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 0a  rr->sent_length.
2df0: 09 09 09 09 09 29 3b 0a 0a 09 09 09 09 09 62 72  .....);.......br
2e00: 65 61 6b 3b 0a 09 09 09 7d 0a 09 09 09 66 70 72  eak;....}....fpr
2e10: 69 6e 74 66 28 66 70 2c 20 22 20 54 48 52 45 41  intf(fp, " THREA
2e20: 44 3d 25 6c 6c 75 20 54 49 4d 45 3d 25 6c 6c 75  D=%llu TIME=%llu
2e30: 5c 6e 22 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e  \n",.....(unsign
2e40: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 28 28  ed long long) ((
2e50: 69 6e 74 70 74 72 5f 74 29 20 63 75 72 72 2d 3e  intptr_t) curr->
2e60: 74 68 72 65 61 64 29 2c 0a 09 09 09 09 28 75 6e  thread),.....(un
2e70: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
2e80: 29 20 6e 6f 77 0a 09 09 09 29 3b 0a 09 09 09 66  ) now....);....f
2e90: 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 09 09 70  flush(fp);.....p
2ea0: 72 65 76 20 3d 20 63 75 72 72 3b 0a 09 09 09 63  rev = curr;....c
2eb0: 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f 70 72 65  urr = curr->_pre
2ec0: 76 3b 0a 0a 09 09 09 66 72 65 65 28 70 72 65 76  v;.....free(prev
2ed0: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75  );...}..}...retu
2ee0: 72 6e 28 4e 55 4c 4c 29 3b 0a 7d 0a 0a 73 74 61  rn(NULL);.}..sta
2ef0: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 6c  tic void filed_l
2f00: 6f 67 5f 65 6e 74 72 79 28 73 74 72 75 63 74 20  og_entry(struct 
2f10: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20  filed_log_entry 
2f20: 2a 65 6e 74 72 79 29 20 7b 0a 09 65 6e 74 72 79  *entry) {..entry
2f30: 2d 3e 74 68 72 65 61 64 20 3d 20 70 74 68 72 65  ->thread = pthre
2f40: 61 64 5f 73 65 6c 66 28 29 3b 0a 0a 09 70 74 68  ad_self();...pth
2f50: 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28  read_mutex_lock(
2f60: 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c  &filed_log_msg_l
2f70: 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 65 6e  ist_mutex);...en
2f80: 74 72 79 2d 3e 5f 6e 65 78 74 20 3d 20 66 69 6c  try->_next = fil
2f90: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b  ed_log_msg_list;
2fa0: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  ..filed_log_msg_
2fb0: 6c 69 73 74 20 3d 20 65 6e 74 72 79 3b 0a 0a 09  list = entry;...
2fc0: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e  pthread_mutex_un
2fd0: 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f  lock(&filed_log_
2fe0: 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b  msg_list_mutex);
2ff0: 0a 0a 09 70 74 68 72 65 61 64 5f 63 6f 6e 64 5f  ...pthread_cond_
3000: 73 69 67 6e 61 6c 28 26 66 69 6c 65 64 5f 6c 6f  signal(&filed_lo
3010: 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 79  g_msg_list_ready
3020: 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );...return;.}..
3030: 73 74 61 74 69 63 20 73 74 72 75 63 74 20 66 69  static struct fi
3040: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 66  led_log_entry *f
3050: 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 69 6e 74  iled_log_new(int
3060: 20 69 6e 69 74 69 61 6c 69 7a 65 29 20 7b 0a 09   initialize) {..
3070: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
3080: 5f 65 6e 74 72 79 20 2a 72 65 74 76 61 6c 3b 0a  _entry *retval;.
3090: 0a 09 72 65 74 76 61 6c 20 3d 20 6d 61 6c 6c 6f  ..retval = mallo
30a0: 63 28 73 69 7a 65 6f 66 28 2a 72 65 74 76 61 6c  c(sizeof(*retval
30b0: 29 29 3b 0a 0a 09 69 66 20 28 69 6e 69 74 69 61  ));...if (initia
30c0: 6c 69 7a 65 29 20 7b 0a 09 09 72 65 74 76 61 6c  lize) {...retval
30d0: 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c  ->buffer[0] = '\
30e0: 30 27 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 68 74  0';...retval->ht
30f0: 74 70 5f 63 6f 64 65 20 3d 20 2d 31 3b 0a 09 09  tp_code = -1;...
3100: 72 65 74 76 61 6c 2d 3e 73 74 61 72 74 74 69 6d  retval->starttim
3110: 65 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d  e = 0;...retval-
3120: 3e 65 6e 64 74 69 6d 65 20 3d 20 30 3b 0a 09 09  >endtime = 0;...
3130: 72 65 74 76 61 6c 2d 3e 72 65 71 5f 6f 66 66 73  retval->req_offs
3140: 65 74 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c  et = 0;...retval
3150: 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20 3d 20 30  ->req_length = 0
3160: 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 73 65 6e 74  ;...retval->sent
3170: 5f 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72  _length = 0;...r
3180: 65 74 76 61 6c 2d 3e 66 69 6c 65 5f 6c 65 6e 67  etval->file_leng
3190: 74 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c  th = 0;...retval
31a0: 2d 3e 69 70 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a  ->ip[0] = '\0';.
31b0: 09 09 72 65 74 76 61 6c 2d 3e 70 6f 72 74 20 3d  ..retval->port =
31c0: 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 6d   -1;...retval->m
31d0: 65 74 68 6f 64 20 3d 20 2d 31 3b 0a 09 7d 0a 0a  ethod = -1;..}..
31e0: 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b  .return(retval);
31f0: 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
3200: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 63 6f  filed_log_msg(co
3210: 6e 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 2e  nst char *fmt, .
3220: 2e 2e 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69  ..) {..struct fi
3230: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 65  led_log_entry *e
3240: 6e 74 72 79 3b 0a 09 76 61 5f 6c 69 73 74 20 61  ntry;..va_list a
3250: 72 67 73 3b 0a 0a 09 65 6e 74 72 79 20 3d 20 66  rgs;...entry = f
3260: 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 30 29 3b  iled_log_new(0);
3270: 0a 0a 09 76 61 5f 73 74 61 72 74 28 61 72 67 73  ...va_start(args
3280: 2c 20 66 6d 74 29 3b 0a 0a 09 76 73 6e 70 72 69  , fmt);...vsnpri
3290: 6e 74 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 65  ntf(entry->buffe
32a0: 72 2c 20 73 69 7a 65 6f 66 28 65 6e 74 72 79 2d  r, sizeof(entry-
32b0: 3e 62 75 66 66 65 72 29 2c 20 66 6d 74 2c 20 61  >buffer), fmt, a
32c0: 72 67 73 29 3b 0a 0a 09 76 61 5f 65 6e 64 28 61  rgs);...va_end(a
32d0: 72 67 73 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 74  rgs);...entry->t
32e0: 79 70 65 20 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f  ype = FILED_LOG_
32f0: 54 59 50 45 5f 4d 45 53 53 41 47 45 3b 0a 0a 09  TYPE_MESSAGE;...
3300: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28  filed_log_entry(
3310: 65 6e 74 72 79 29 3b 0a 0a 09 72 65 74 75 72 6e  entry);...return
3320: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73  ;.}..static cons
3330: 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 6c 6f  t char *filed_lo
3340: 67 5f 69 70 28 73 74 72 75 63 74 20 73 6f 63 6b  g_ip(struct sock
3350: 61 64 64 72 20 2a 61 64 64 72 2c 20 63 68 61 72  addr *addr, char
3360: 20 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f 74   *buffer, size_t
3370: 20 62 75 66 66 65 72 6c 65 6e 29 20 7b 0a 09 73   bufferlen) {..s
3380: 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69  truct sockaddr_i
3390: 6e 20 2a 61 64 64 72 5f 76 34 3b 0a 09 73 74 72  n *addr_v4;..str
33a0: 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36  uct sockaddr_in6
33b0: 20 2a 61 64 64 72 5f 76 36 3b 0a 09 63 6f 6e 73   *addr_v6;..cons
33c0: 74 20 63 68 61 72 20 2a 72 65 74 76 61 6c 20 3d  t char *retval =
33d0: 20 4e 55 4c 4c 3b 0a 0a 09 61 64 64 72 5f 76 36   NULL;...addr_v6
33e0: 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61   = (struct socka
33f0: 64 64 72 5f 69 6e 36 20 2a 29 20 61 64 64 72 3b  ddr_in6 *) addr;
3400: 0a 0a 09 73 77 69 74 63 68 20 28 61 64 64 72 5f  ...switch (addr_
3410: 76 36 2d 3e 73 69 6e 36 5f 66 61 6d 69 6c 79 29  v6->sin6_family)
3420: 20 7b 0a 09 09 63 61 73 65 20 41 46 5f 49 4e 45   {...case AF_INE
3430: 54 3a 0a 09 09 09 61 64 64 72 5f 76 34 20 3d 20  T:....addr_v4 = 
3440: 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72  (struct sockaddr
3450: 5f 69 6e 20 2a 29 20 61 64 64 72 3b 0a 09 09 09  _in *) addr;....
3460: 72 65 74 76 61 6c 20 3d 20 69 6e 65 74 5f 6e 74  retval = inet_nt
3470: 6f 70 28 41 46 5f 49 4e 45 54 2c 20 26 61 64 64  op(AF_INET, &add
3480: 72 5f 76 34 2d 3e 73 69 6e 5f 61 64 64 72 2c 20  r_v4->sin_addr, 
3490: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 6c 65  buffer, bufferle
34a0: 6e 29 3b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09  n);....break;...
34b0: 63 61 73 65 20 41 46 5f 49 4e 45 54 36 3a 0a 09  case AF_INET6:..
34c0: 09 09 72 65 74 76 61 6c 20 3d 20 69 6e 65 74 5f  ..retval = inet_
34d0: 6e 74 6f 70 28 41 46 5f 49 4e 45 54 36 2c 20 26  ntop(AF_INET6, &
34e0: 61 64 64 72 5f 76 36 2d 3e 73 69 6e 36 5f 61 64  addr_v6->sin6_ad
34f0: 64 72 2c 20 62 75 66 66 65 72 2c 20 62 75 66 66  dr, buffer, buff
3500: 65 72 6c 65 6e 29 3b 0a 09 09 09 62 72 65 61 6b  erlen);....break
3510: 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65  ;..}...return(re
3520: 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  tval);.}..static
3530: 20 46 49 4c 45 20 2a 66 69 6c 65 64 5f 6c 6f 67   FILE *filed_log
3540: 5f 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72  _open(const char
3550: 20 2a 66 69 6c 65 29 20 7b 0a 09 46 49 4c 45 20   *file) {..FILE 
3560: 2a 72 65 74 76 61 6c 3b 0a 0a 09 69 66 20 28 73  *retval;...if (s
3570: 74 72 63 6d 70 28 66 69 6c 65 2c 20 22 2d 22 29  trcmp(file, "-")
3580: 20 3d 3d 20 30 29 20 7b 0a 09 09 72 65 74 76 61   == 0) {...retva
3590: 6c 20 3d 20 73 74 64 6f 75 74 3b 0a 09 7d 20 65  l = stdout;..} e
35a0: 6c 73 65 20 69 66 20 28 66 69 6c 65 5b 30 5d 20  lse if (file[0] 
35b0: 3d 3d 20 27 7c 27 29 20 7b 0a 09 09 66 69 6c 65  == '|') {...file
35c0: 2b 2b 3b 0a 09 09 72 65 74 76 61 6c 20 3d 20 70  ++;...retval = p
35d0: 6f 70 65 6e 28 66 69 6c 65 2c 20 22 77 22 29 3b  open(file, "w");
35e0: 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 72 65 74  ..} else {...ret
35f0: 76 61 6c 20 3d 20 66 6f 70 65 6e 28 66 69 6c 65  val = fopen(file
3600: 2c 20 22 61 2b 22 29 3b 0a 09 7d 0a 0a 09 72 65  , "a+");..}...re
3610: 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a  turn(retval);.}.
3620: 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65  .static int file
3630: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64  d_logging_thread
3640: 5f 69 6e 69 74 28 46 49 4c 45 20 2a 6c 6f 67 66  _init(FILE *logf
3650: 70 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c  p) {..struct fil
3660: 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61  ed_logging_threa
3670: 64 5f 61 72 67 73 20 2a 61 72 67 73 3b 0a 09 70  d_args *args;..p
3680: 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 5f  thread_t thread_
3690: 69 64 3b 0a 0a 09 61 72 67 73 20 3d 20 6d 61 6c  id;...args = mal
36a0: 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72 67 73  loc(sizeof(*args
36b0: 29 29 3b 0a 09 61 72 67 73 2d 3e 66 70 20 3d 20  ));..args->fp = 
36c0: 6c 6f 67 66 70 3b 0a 0a 09 66 69 6c 65 64 5f 6c  logfp;...filed_l
36d0: 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 4e 55  og_msg_list = NU
36e0: 4c 4c 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75  LL;...pthread_mu
36f0: 74 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 64 5f  tex_init(&filed_
3700: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74  log_msg_list_mut
3710: 65 78 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 70 74 68  ex, NULL);...pth
3720: 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 68 72  read_create(&thr
3730: 65 61 64 5f 69 64 2c 20 4e 55 4c 4c 2c 20 66 69  ead_id, NULL, fi
3740: 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65  led_logging_thre
3750: 61 64 2c 20 61 72 67 73 29 3b 0a 0a 09 66 69 6c  ad, args);...fil
3760: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 53 54 41 52  ed_log_msg("STAR
3770: 54 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29  T");...return(0)
3780: 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  ;.}.#endif..#ifd
3790: 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 54 49  ef FILED_DONT_TI
37a0: 4d 45 4f 55 54 0a 23 64 65 66 69 6e 65 20 66 69  MEOUT.#define fi
37b0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
37c0: 74 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 29 20  t_thread_init() 
37d0: 30 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f  0.#define filed_
37e0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e  sockettimeout_in
37f0: 69 74 28 29 20 30 0a 23 64 65 66 69 6e 65 20 66  it() 0.#define f
3800: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3810: 75 74 5f 61 63 63 65 70 74 28 78 29 20 2f 2a 2a  ut_accept(x) /**
3820: 2f 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f  /.#define filed_
3830: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72  sockettimeout_pr
3840: 6f 63 65 73 73 69 6e 67 5f 73 74 61 72 74 28 78  ocessing_start(x
3850: 29 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ) /**/.#define f
3860: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3870: 75 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 65 6e  ut_processing_en
3880: 64 28 78 29 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e  d(x) /**/.#defin
3890: 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  e filed_socketti
38a0: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 78 29 20 2f  meout_close(x) /
38b0: 2a 2a 2f 0a 23 65 6c 73 65 0a 5f 41 74 6f 6d 69  **/.#else._Atomi
38c0: 63 20 74 69 6d 65 5f 74 20 66 69 6c 65 64 5f 73  c time_t filed_s
38d0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 69 6d  ockettimeout_tim
38e0: 65 3b 0a 73 74 72 75 63 74 20 7b 0a 09 5f 41 74  e;.struct {.._At
38f0: 6f 6d 69 63 20 74 69 6d 65 5f 74 20 65 78 70 69  omic time_t expi
3900: 72 61 74 69 6f 6e 5f 74 69 6d 65 3b 0a 09 5f 41  ration_time;.._A
3910: 74 6f 6d 69 63 20 70 74 68 72 65 61 64 5f 74 20  tomic pthread_t 
3920: 74 68 72 65 61 64 5f 69 64 3b 0a 09 62 6f 6f 6c  thread_id;..bool
3930: 20 76 61 6c 69 64 3b 0a 7d 2a 20 66 69 6c 65 64   valid;.}* filed
3940: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73  _sockettimeout_s
3950: 6f 63 6b 73 74 61 74 75 73 3b 0a 6c 6f 6e 67 20  ockstatus;.long 
3960: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3970: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c  out_sockstatus_l
3980: 65 6e 67 74 68 3b 0a 69 6e 74 20 66 69 6c 65 64  ength;.int filed
3990: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 64  _sockettimeout_d
39a0: 65 76 6e 75 6c 6c 5f 66 64 3b 0a 0a 73 74 61 74  evnull_fd;..stat
39b0: 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63  ic int filed_soc
39c0: 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66  kettimeout_sockf
39d0: 64 5f 69 6e 5f 72 61 6e 67 65 28 69 6e 74 20 73  d_in_range(int s
39e0: 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 73 6f  ockfd) {..if (so
39f0: 63 6b 66 64 20 3c 20 33 29 20 7b 0a 09 09 72 65  ckfd < 3) {...re
3a00: 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 69 66  turn(0);..}...if
3a10: 20 28 73 6f 63 6b 66 64 20 3e 20 66 69 6c 65 64   (sockfd > filed
3a20: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73  _sockettimeout_s
3a30: 6f 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68  ockstatus_length
3a40: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b  ) {...return(0);
3a50: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 31 29 3b  ..}...return(1);
3a60: 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
3a70: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3a80: 6f 75 74 5f 65 78 70 69 72 65 28 69 6e 74 20 73  out_expire(int s
3a90: 6f 63 6b 66 64 2c 20 69 6e 74 20 6c 65 6e 67 74  ockfd, int lengt
3aa0: 68 29 20 7b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77  h) {..time_t now
3ab0: 2c 20 65 78 70 69 72 65 3b 0a 0a 09 6e 6f 77 20  , expire;...now 
3ac0: 3d 20 61 74 6f 6d 69 63 5f 6c 6f 61 64 28 26 66  = atomic_load(&f
3ad0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3ae0: 75 74 5f 74 69 6d 65 29 3b 0a 0a 09 65 78 70 69  ut_time);...expi
3af0: 72 65 20 3d 20 6e 6f 77 20 2b 20 6c 65 6e 67 74  re = now + lengt
3b00: 68 3b 0a 0a 09 61 74 6f 6d 69 63 5f 73 74 6f 72  h;...atomic_stor
3b10: 65 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  e(&filed_sockett
3b20: 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75  imeout_sockstatu
3b30: 73 5b 73 6f 63 6b 66 64 5d 2e 65 78 70 69 72 61  s[sockfd].expira
3b40: 74 69 6f 6e 5f 74 69 6d 65 2c 20 65 78 70 69 72  tion_time, expir
3b50: 65 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a  e);...return;.}.
3b60: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
3b70: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
3b80: 5f 61 63 63 65 70 74 28 69 6e 74 20 73 6f 63 6b  _accept(int sock
3b90: 66 64 29 20 7b 0a 09 69 66 20 28 21 66 69 6c 65  fd) {..if (!file
3ba0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
3bb0: 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28  sockfd_in_range(
3bc0: 73 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 65 74  sockfd)) {...ret
3bd0: 75 72 6e 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f  urn;..}...filed_
3be0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78  sockettimeout_ex
3bf0: 70 69 72 65 28 73 6f 63 6b 66 64 2c 20 36 30 29  pire(sockfd, 60)
3c00: 3b 0a 0a 09 61 74 6f 6d 69 63 5f 73 74 6f 72 65  ;...atomic_store
3c10: 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  (&filed_socketti
3c20: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73  meout_sockstatus
3c30: 5b 73 6f 63 6b 66 64 5d 2e 74 68 72 65 61 64 5f  [sockfd].thread_
3c40: 69 64 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66  id, pthread_self
3c50: 28 29 29 3b 0a 0a 09 61 74 6f 6d 69 63 5f 73 74  ());...atomic_st
3c60: 6f 72 65 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65  ore(&filed_socke
3c70: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61  ttimeout_socksta
3c80: 74 75 73 5b 73 6f 63 6b 66 64 5d 2e 76 61 6c 69  tus[sockfd].vali
3c90: 64 2c 20 74 72 75 65 29 3b 0a 0a 09 72 65 74 75  d, true);...retu
3ca0: 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  rn;.}..static vo
3cb0: 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  id filed_sockett
3cc0: 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e  imeout_processin
3cd0: 67 5f 73 74 61 72 74 28 69 6e 74 20 73 6f 63 6b  g_start(int sock
3ce0: 66 64 29 20 7b 0a 09 69 66 20 28 21 66 69 6c 65  fd) {..if (!file
3cf0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
3d00: 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28  sockfd_in_range(
3d10: 73 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 65 74  sockfd)) {...ret
3d20: 75 72 6e 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f  urn;..}...filed_
3d30: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78  sockettimeout_ex
3d40: 70 69 72 65 28 73 6f 63 6b 66 64 2c 20 38 36 34  pire(sockfd, 864
3d50: 30 30 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d  00);...return;.}
3d60: 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69  ..static void fi
3d70: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
3d80: 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64  t_processing_end
3d90: 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09  (int sockfd) {..
3da0: 69 66 20 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65  if (!filed_socke
3db0: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f  ttimeout_sockfd_
3dc0: 69 6e 5f 72 61 6e 67 65 28 73 6f 63 6b 66 64 29  in_range(sockfd)
3dd0: 29 20 7b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d  ) {...return;..}
3de0: 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  ...filed_sockett
3df0: 69 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 73 6f  imeout_expire(so
3e00: 63 6b 66 64 2c 20 36 30 29 3b 0a 0a 09 72 65 74  ckfd, 60);...ret
3e10: 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  urn;.}..static v
3e20: 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74  oid filed_socket
3e30: 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 69 6e  timeout_close(in
3e40: 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20  t sockfd) {..if 
3e50: 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  (!filed_socketti
3e60: 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f  meout_sockfd_in_
3e70: 72 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b  range(sockfd)) {
3e80: 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09  ...return;..}...
3e90: 61 74 6f 6d 69 63 5f 73 74 6f 72 65 28 26 66 69  atomic_store(&fi
3ea0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
3eb0: 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f 63  t_sockstatus[soc
3ec0: 6b 66 64 5d 2e 76 61 6c 69 64 2c 20 66 61 6c 73  kfd].valid, fals
3ed0: 65 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a  e);...return;.}.
3ee0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69  .static void *fi
3ef0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
3f00: 74 5f 74 68 72 65 61 64 28 76 6f 69 64 20 2a 61  t_thread(void *a
3f10: 72 67 29 20 7b 0a 09 74 69 6d 65 5f 74 20 6e 6f  rg) {..time_t no
3f20: 77 2c 20 65 78 70 69 72 61 74 69 6f 6e 5f 74 69  w, expiration_ti
3f30: 6d 65 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 74  me;..pthread_t t
3f40: 68 72 65 61 64 5f 69 64 3b 0a 09 6c 6f 6e 67 20  hread_id;..long 
3f50: 69 64 78 3b 0a 09 69 6e 74 20 63 6f 75 6e 74 3b  idx;..int count;
3f60: 0a 09 62 6f 6f 6c 20 76 61 6c 69 64 3b 0a 0a 09  ..bool valid;...
3f70: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 66 6f  while (1) {...fo
3f80: 72 20 28 63 6f 75 6e 74 20 3d 20 30 3b 20 63 6f  r (count = 0; co
3f90: 75 6e 74 20 3c 20 31 30 3b 20 63 6f 75 6e 74 2b  unt < 10; count+
3fa0: 2b 29 20 7b 0a 09 09 09 75 73 6c 65 65 70 28 33  +) {....usleep(3
3fb0: 30 30 30 30 30 30 30 29 3b 0a 0a 09 09 09 6e 6f  0000000);.....no
3fc0: 77 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a  w = time(NULL);.
3fd0: 0a 09 09 09 61 74 6f 6d 69 63 5f 73 74 6f 72 65  ....atomic_store
3fe0: 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  (&filed_socketti
3ff0: 6d 65 6f 75 74 5f 74 69 6d 65 2c 20 6e 6f 77 29  meout_time, now)
4000: 3b 0a 09 09 7d 0a 0a 09 09 66 6f 72 20 28 69 64  ;...}....for (id
4010: 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 66 69 6c  x = 0; idx < fil
4020: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
4030: 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67  _sockstatus_leng
4040: 74 68 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09 09  th; idx++) {....
4050: 76 61 6c 69 64 20 3d 20 61 74 6f 6d 69 63 5f 6c  valid = atomic_l
4060: 6f 61 64 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65  oad(&filed_socke
4070: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61  ttimeout_socksta
4080: 74 75 73 5b 69 64 78 5d 2e 76 61 6c 69 64 29 3b  tus[idx].valid);
4090: 0a 0a 09 09 09 69 66 20 28 21 76 61 6c 69 64 29  .....if (!valid)
40a0: 20 7b 0a 09 09 09 09 63 6f 6e 74 69 6e 75 65 3b   {.....continue;
40b0: 0a 09 09 09 7d 0a 0a 09 09 09 65 78 70 69 72 61  ....}.....expira
40c0: 74 69 6f 6e 5f 74 69 6d 65 20 3d 20 61 74 6f 6d  tion_time = atom
40d0: 69 63 5f 6c 6f 61 64 28 26 66 69 6c 65 64 5f 73  ic_load(&filed_s
40e0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63  ockettimeout_soc
40f0: 6b 73 74 61 74 75 73 5b 69 64 78 5d 2e 65 78 70  kstatus[idx].exp
4100: 69 72 61 74 69 6f 6e 5f 74 69 6d 65 29 3b 0a 0a  iration_time);..
4110: 09 09 09 74 68 72 65 61 64 5f 69 64 20 3d 20 61  ...thread_id = a
4120: 74 6f 6d 69 63 5f 6c 6f 61 64 28 26 66 69 6c 65  tomic_load(&file
4130: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
4140: 73 6f 63 6b 73 74 61 74 75 73 5b 69 64 78 5d 2e  sockstatus[idx].
4150: 74 68 72 65 61 64 5f 69 64 29 3b 0a 0a 09 09 09  thread_id);.....
4160: 69 66 20 28 65 78 70 69 72 61 74 69 6f 6e 5f 74  if (expiration_t
4170: 69 6d 65 20 3e 20 6e 6f 77 29 20 7b 0a 09 09 09  ime > now) {....
4180: 09 63 6f 6e 74 69 6e 75 65 3b 0a 09 09 09 7d 0a  .continue;....}.
4190: 0a 09 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74  ....filed_socket
41a0: 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 69 64  timeout_close(id
41b0: 78 29 3b 0a 0a 09 09 09 64 75 70 32 28 66 69 6c  x);.....dup2(fil
41c0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
41d0: 5f 64 65 76 6e 75 6c 6c 5f 66 64 2c 20 69 64 78  _devnull_fd, idx
41e0: 29 3b 0a 0a 09 09 09 70 74 68 72 65 61 64 5f 6b  );.....pthread_k
41f0: 69 6c 6c 28 74 68 72 65 61 64 5f 69 64 2c 20 53  ill(thread_id, S
4200: 49 47 50 49 50 45 29 3b 0a 09 09 7d 0a 09 7d 0a  IGPIPE);...}..}.
4210: 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a  ..return(NULL);.
4220: 0a 09 2f 2a 20 4e 4f 54 52 45 41 43 48 3a 20 57  ../* NOTREACH: W
4230: 65 20 64 6f 6e 27 74 20 61 63 74 75 61 6c 6c 79  e don't actually
4240: 20 74 61 6b 65 20 61 6e 79 20 61 72 67 75 6d 65   take any argume
4250: 6e 74 73 20 2a 2f 0a 09 61 72 67 20 3d 20 61 72  nts */..arg = ar
4260: 67 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  g;.}..static int
4270: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d   filed_sockettim
4280: 65 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e 69 74  eout_thread_init
4290: 28 76 6f 69 64 29 20 7b 0a 09 70 74 68 72 65 61  (void) {..pthrea
42a0: 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b 0a 0a  d_t thread_id;..
42b0: 09 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28  .pthread_create(
42c0: 26 74 68 72 65 61 64 5f 69 64 2c 20 4e 55 4c 4c  &thread_id, NULL
42d0: 2c 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  , filed_socketti
42e0: 6d 65 6f 75 74 5f 74 68 72 65 61 64 2c 20 4e 55  meout_thread, NU
42f0: 4c 4c 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29  LL);...return(0)
4300: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
4310: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
4320: 6f 75 74 5f 69 6e 69 74 28 76 6f 69 64 29 20 7b  out_init(void) {
4330: 0a 09 6c 6f 6e 67 20 6d 61 78 66 64 2c 20 69 64  ..long maxfd, id
4340: 78 3b 0a 0a 09 6d 61 78 66 64 20 3d 20 73 79 73  x;...maxfd = sys
4350: 63 6f 6e 66 28 5f 53 43 5f 4f 50 45 4e 5f 4d 41  conf(_SC_OPEN_MA
4360: 58 29 3b 0a 09 69 66 20 28 6d 61 78 66 64 20 3c  X);..if (maxfd <
4370: 3d 20 30 29 20 7b 0a 09 09 6d 61 78 66 64 20 3d  = 0) {...maxfd =
4380: 20 34 30 39 36 3b 0a 09 7d 0a 0a 09 66 69 6c 65   4096;..}...file
4390: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
43a0: 73 6f 63 6b 73 74 61 74 75 73 20 3d 20 6d 61 6c  sockstatus = mal
43b0: 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 66 69 6c 65  loc(sizeof(*file
43c0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
43d0: 73 6f 63 6b 73 74 61 74 75 73 29 20 2a 20 6d 61  sockstatus) * ma
43e0: 78 66 64 29 3b 0a 09 69 66 20 28 66 69 6c 65 64  xfd);..if (filed
43f0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73  _sockettimeout_s
4400: 6f 63 6b 73 74 61 74 75 73 20 3d 3d 20 4e 55 4c  ockstatus == NUL
4410: 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 2d 31  L) {...return(-1
4420: 29 3b 0a 09 7d 0a 0a 09 66 6f 72 20 28 69 64 78  );..}...for (idx
4430: 20 3d 20 30 3b 20 69 64 78 20 3c 20 6d 61 78 66   = 0; idx < maxf
4440: 64 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09 66 69  d; idx++) {...fi
4450: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
4460: 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 69 64 78  t_sockstatus[idx
4470: 5d 2e 76 61 6c 69 64 20 3d 20 66 61 6c 73 65 3b  ].valid = false;
4480: 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b  ..}...filed_sock
4490: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74  ettimeout_sockst
44a0: 61 74 75 73 5f 6c 65 6e 67 74 68 20 3d 20 6d 61  atus_length = ma
44b0: 78 66 64 3b 0a 09 66 69 6c 65 64 5f 73 6f 63 6b  xfd;..filed_sock
44c0: 65 74 74 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c  ettimeout_devnul
44d0: 6c 5f 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65  l_fd = open("/de
44e0: 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 52 44 57 52 29  v/null", O_RDWR)
44f0: 3b 0a 09 69 66 20 28 66 69 6c 65 64 5f 73 6f 63  ;..if (filed_soc
4500: 6b 65 74 74 69 6d 65 6f 75 74 5f 64 65 76 6e 75  kettimeout_devnu
4510: 6c 6c 5f 66 64 20 3c 20 30 29 20 7b 0a 09 09 72  ll_fd < 0) {...r
4520: 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09  eturn(-1);..}...
4530: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 23 65 6e  return(0);.}.#en
4540: 64 69 66 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20 74  dif../* Format t
4550: 69 6d 65 20 70 65 72 20 52 46 43 32 36 31 36 20  ime per RFC2616 
4560: 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
4570: 66 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d  filed_format_tim
4580: 65 28 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20  e(char *buffer, 
4590: 73 69 7a 65 5f 74 20 62 75 66 66 65 72 5f 6c 65  size_t buffer_le
45a0: 6e 2c 20 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20  n, const time_t 
45b0: 74 69 6d 65 69 6e 66 6f 29 20 7b 0a 09 73 74 72  timeinfo) {..str
45c0: 75 63 74 20 74 6d 20 74 69 6d 65 69 6e 66 6f 5f  uct tm timeinfo_
45d0: 74 6d 2c 20 2a 74 69 6d 65 69 6e 66 6f 5f 74 6d  tm, *timeinfo_tm
45e0: 5f 70 3b 0a 0a 09 74 69 6d 65 69 6e 66 6f 5f 74  _p;...timeinfo_t
45f0: 6d 5f 70 20 3d 20 67 6d 74 69 6d 65 5f 72 28 26  m_p = gmtime_r(&
4600: 74 69 6d 65 69 6e 66 6f 2c 20 26 74 69 6d 65 69  timeinfo, &timei
4610: 6e 66 6f 5f 74 6d 29 3b 0a 09 69 66 20 28 74 69  nfo_tm);..if (ti
4620: 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d 3d 20 4e  meinfo_tm_p == N
4630: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  ULL) {...return(
4640: 22 75 6e 6b 6e 6f 77 6e 22 29 3b 0a 09 7d 0a 0a  "unknown");..}..
4650: 09 62 75 66 66 65 72 5b 62 75 66 66 65 72 5f 6c  .buffer[buffer_l
4660: 65 6e 20 2d 20 31 5d 20 3d 20 27 5c 30 27 3b 0a  en - 1] = '\0';.
4670: 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 74  .buffer_len = st
4680: 72 66 74 69 6d 65 28 62 75 66 66 65 72 2c 20 62  rftime(buffer, b
4690: 75 66 66 65 72 5f 6c 65 6e 20 2d 20 31 2c 20 22  uffer_len - 1, "
46a0: 25 61 2c 20 25 64 20 25 62 20 25 59 20 25 48 3a  %a, %d %b %Y %H:
46b0: 25 4d 3a 25 53 20 47 4d 54 22 2c 20 74 69 6d 65  %M:%S GMT", time
46c0: 69 6e 66 6f 5f 74 6d 5f 70 29 3b 0a 0a 09 72 65  info_tm_p);...re
46d0: 74 75 72 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a  turn(buffer);.}.
46e0: 0a 2f 2a 20 68 61 73 68 20 2a 2f 0a 73 74 61 74  ./* hash */.stat
46f0: 69 63 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  ic unsigned int 
4700: 66 69 6c 65 64 5f 68 61 73 68 28 63 6f 6e 73 74  filed_hash(const
4710: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
4720: 76 61 6c 75 65 2c 20 75 6e 73 69 67 6e 65 64 20  value, unsigned 
4730: 69 6e 74 20 6d 6f 64 75 6c 75 73 29 20 7b 0a 09  int modulus) {..
4740: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 75  unsigned char cu
4750: 72 72 2c 20 70 72 65 76 3b 0a 09 69 6e 74 20 64  rr, prev;..int d
4760: 69 66 66 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69  iff;..unsigned i
4770: 6e 74 20 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74  nt retval;...ret
4780: 76 61 6c 20 3d 20 6d 6f 64 75 6c 75 73 20 2d 20  val = modulus - 
4790: 31 3b 0a 09 70 72 65 76 20 3d 20 6d 6f 64 75 6c  1;..prev = modul
47a0: 75 73 20 25 20 32 35 35 3b 0a 0a 09 77 68 69 6c  us % 255;...whil
47b0: 65 20 28 28 63 75 72 72 20 3d 20 2a 76 61 6c 75  e ((curr = *valu
47c0: 65 29 29 20 7b 0a 09 09 69 66 20 28 63 75 72 72  e)) {...if (curr
47d0: 20 3c 20 33 32 29 20 7b 0a 09 09 09 63 75 72 72   < 32) {....curr
47e0: 20 3d 20 32 35 35 20 2d 20 63 75 72 72 3b 0a 09   = 255 - curr;..
47f0: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 63 75 72  .} else {....cur
4800: 72 20 2d 3d 20 33 32 3b 0a 09 09 7d 0a 0a 09 09  r -= 32;...}....
4810: 69 66 20 28 70 72 65 76 20 3c 20 63 75 72 72 29  if (prev < curr)
4820: 20 7b 0a 09 09 09 64 69 66 66 20 3d 20 63 75 72   {....diff = cur
4830: 72 20 2d 20 70 72 65 76 3b 0a 09 09 7d 20 65 6c  r - prev;...} el
4840: 73 65 20 7b 0a 09 09 09 64 69 66 66 20 3d 20 70  se {....diff = p
4850: 72 65 76 20 2d 20 63 75 72 72 3b 0a 09 09 7d 0a  rev - curr;...}.
4860: 0a 09 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a  ...prev = curr;.
4870: 0a 09 09 72 65 74 76 61 6c 20 3c 3c 3d 20 33 3b  ...retval <<= 3;
4880: 0a 09 09 72 65 74 76 61 6c 20 26 3d 20 30 78 46  ...retval &= 0xF
4890: 46 46 46 46 46 46 46 4c 55 3b 0a 09 09 72 65 74  FFFFFFFLU;...ret
48a0: 76 61 6c 20 5e 3d 20 64 69 66 66 3b 0a 0a 09 09  val ^= diff;....
48b0: 76 61 6c 75 65 2b 2b 3b 0a 09 7d 0a 0a 09 72 65  value++;..}...re
48c0: 74 76 61 6c 20 3d 20 72 65 74 76 61 6c 20 25 20  tval = retval % 
48d0: 6d 6f 64 75 6c 75 73 3b 0a 0a 09 72 65 74 75 72  modulus;...retur
48e0: 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 2f 2a  n(retval);.}../*
48f0: 20 46 69 6e 64 20 61 20 6d 69 6d 65 2d 74 79 70   Find a mime-typ
4900: 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66  e based on the f
4910: 69 6c 65 6e 61 6d 65 20 2a 2f 0a 73 74 61 74 69  ilename */.stati
4920: 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69  c const char *fi
4930: 6c 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f 6d 69  led_determine_mi
4940: 6d 65 74 79 70 65 28 63 6f 6e 73 74 20 63 68 61  metype(const cha
4950: 72 20 2a 70 61 74 68 29 20 7b 0a 09 63 6f 6e 73  r *path) {..cons
4960: 74 20 63 68 61 72 20 2a 70 3b 0a 0a 09 70 20 3d  t char *p;...p =
4970: 20 73 74 72 72 63 68 72 28 70 61 74 68 2c 20 27   strrchr(path, '
4980: 2e 27 29 3b 0a 09 69 66 20 28 70 20 3d 3d 20 4e  .');..if (p == N
4990: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  ULL) {...return(
49a0: 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59  FILED_DEFAULT_TY
49b0: 50 45 29 3b 0a 09 7d 0a 0a 09 70 2b 2b 3b 0a 09  PE);..}...p++;..
49c0: 69 66 20 28 2a 70 20 3d 3d 20 27 5c 30 27 29 20  if (*p == '\0') 
49d0: 7b 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44  {...return(FILED
49e0: 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a  _DEFAULT_TYPE);.
49f0: 09 7d 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  .}...filed_log_m
4a00: 73 67 5f 64 65 62 75 67 28 22 4c 6f 6f 6b 69 6e  sg_debug("Lookin
4a10: 67 20 75 70 20 4d 49 4d 45 20 74 79 70 65 20 66  g up MIME type f
4a20: 6f 72 20 25 73 20 28 68 61 73 68 20 3d 20 25 6c  or %s (hash = %l
4a30: 6c 75 29 22 2c 20 70 2c 20 28 75 6e 73 69 67 6e  lu)", p, (unsign
4a40: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69  ed long long) fi
4a50: 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e 73 74 20  led_hash((const 
4a60: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
4a70: 20 70 2c 20 31 36 37 37 37 32 35 39 29 29 3b 0a   p, 16777259));.
4a80: 0a 23 69 6e 63 6c 75 64 65 20 22 66 69 6c 65 64  .#include "filed
4a90: 2d 6d 69 6d 65 2d 74 79 70 65 73 2e 68 22 0a 0a  -mime-types.h"..
4aa0: 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 45  .return(FILED_DE
4ab0: 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 7d 0a 0a  FAULT_TYPE);.}..
4ac0: 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 75 6e  /* Generate a un
4ad0: 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20  ique identifier 
4ae0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
4af0: 69 6c 65 64 5f 67 65 6e 65 72 61 74 65 5f 65 74  iled_generate_et
4b00: 61 67 28 63 68 61 72 20 2a 65 74 61 67 2c 20 73  ag(char *etag, s
4b10: 69 7a 65 5f 74 20 6c 65 6e 67 74 68 29 20 7b 0a  ize_t length) {.
4b20: 09 73 6e 70 72 69 6e 74 66 28 65 74 61 67 2c 20  .snprintf(etag, 
4b30: 6c 65 6e 67 74 68 2c 20 22 25 6c 6c 78 2d 25 6c  length, "%llx-%l
4b40: 6c 78 25 6c 6c 78 25 6c 6c 78 25 6c 6c 78 22 2c  lx%llx%llx%llx",
4b50: 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
4b60: 67 20 6c 6f 6e 67 29 20 74 69 6d 65 28 4e 55 4c  g long) time(NUL
4b70: 4c 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20  L),...(unsigned 
4b80: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f  long long) rando
4b90: 6d 28 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64  m(),...(unsigned
4ba0: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64   long long) rand
4bb0: 6f 6d 28 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65  om(),...(unsigne
4bc0: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e  d long long) ran
4bd0: 64 6f 6d 28 29 2c 0a 09 09 28 75 6e 73 69 67 6e  dom(),...(unsign
4be0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61  ed long long) ra
4bf0: 6e 64 6f 6d 28 29 0a 09 29 3b 0a 7d 0a 0a 23 69  ndom()..);.}..#i
4c00: 66 64 65 66 20 46 49 4c 45 44 5f 46 41 4b 45 5f  fdef FILED_FAKE_
4c10: 43 48 52 4f 4f 54 0a 2f 2a 20 54 72 61 6e 73 6c  CHROOT./* Transl
4c20: 61 74 65 20 61 20 70 61 74 68 20 69 6e 74 6f 20  ate a path into 
4c30: 61 20 66 61 6b 65 20 63 68 72 6f 6f 74 20 70 61  a fake chroot pa
4c40: 74 68 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  th */.static con
4c50: 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 70  st char *filed_p
4c60: 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 28 63 6f  ath_translate(co
4c70: 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20  nst char *path, 
4c80: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74  struct filed_opt
4c90: 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b  ions *options) {
4ca0: 0a 09 73 74 61 74 69 63 20 5f 5f 74 68 72 65 61  ..static __threa
4cb0: 64 20 63 68 61 72 20 70 61 74 68 42 75 66 66 65  d char pathBuffe
4cc0: 72 5b 38 31 39 32 5d 3b 0a 09 69 6e 74 20 73 6e  r[8192];..int sn
4cd0: 70 72 69 6e 74 66 5f 72 65 74 3b 0a 0a 09 2f 2a  printf_ret;.../*
4ce0: 20 49 66 20 6e 6f 20 61 6c 74 65 72 6e 61 74 69   If no alternati
4cf0: 76 65 20 72 6f 6f 74 20 69 73 20 73 70 65 63 69  ve root is speci
4d00: 66 69 65 64 2c 20 72 65 74 75 72 6e 20 74 68 65  fied, return the
4d10: 20 75 6e 61 64 6f 72 6e 65 64 20 70 61 74 68 20   unadorned path 
4d20: 2a 2f 0a 09 69 66 20 28 21 6f 70 74 69 6f 6e 73  */..if (!options
4d30: 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 29 20  ->fake_newroot) 
4d40: 7b 0a 09 09 72 65 74 75 72 6e 28 70 61 74 68 29  {...return(path)
4d50: 3b 0a 09 7d 0a 0a 09 2f 2a 20 56 65 72 69 66 79  ;..}.../* Verify
4d60: 20 74 68 61 74 20 74 68 69 73 20 72 65 71 75 65   that this reque
4d70: 73 74 20 77 69 6c 6c 20 6e 6f 74 20 67 6f 20 6f  st will not go o
4d80: 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 73 70  utside of the sp
4d90: 65 63 69 66 69 65 64 20 72 6f 6f 74 20 2a 2f 0a  ecified root */.
4da0: 09 69 66 20 28 73 74 72 73 74 72 28 70 61 74 68  .if (strstr(path
4db0: 2c 20 22 2f 2e 2e 2f 22 29 20 21 3d 20 4e 55 4c  , "/../") != NUL
4dc0: 4c 20 7c 7c 20 70 61 74 68 5b 30 5d 20 21 3d 20  L || path[0] != 
4dd0: 27 2f 27 29 20 7b 0a 09 09 66 69 6c 65 64 5f 6c  '/') {...filed_l
4de0: 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 55 6e  og_msg_debug("Un
4df0: 61 62 6c 65 20 74 6f 20 74 72 61 6e 73 6c 61 74  able to translat
4e00: 65 20 70 61 74 68 20 5c 22 25 73 5c 22 2c 20 63  e path \"%s\", c
4e10: 6f 6e 74 61 69 6e 73 20 69 6e 76 61 6c 69 64 20  ontains invalid 
4e20: 63 68 61 72 61 63 74 65 72 73 22 2c 20 70 61 74  characters", pat
4e30: 68 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 6f 70  h);....return(op
4e40: 74 69 6f 6e 73 2d 3e 66 61 6b 65 5f 6e 65 77 72  tions->fake_newr
4e50: 6f 6f 74 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72  oot);..}.../* Cr
4e60: 65 61 74 65 20 74 68 65 20 6e 65 77 20 70 61 74  eate the new pat
4e70: 68 20 69 6e 74 6f 20 6f 75 72 20 6c 6f 63 61 6c  h into our local
4e80: 20 28 54 4c 53 29 20 73 74 61 74 69 63 20 62 75   (TLS) static bu
4e90: 66 66 65 72 20 2a 2f 0a 09 73 6e 70 72 69 6e 74  ffer */..snprint
4ea0: 66 5f 72 65 74 20 3d 20 73 6e 70 72 69 6e 74 66  f_ret = snprintf
4eb0: 28 70 61 74 68 42 75 66 66 65 72 2c 20 73 69 7a  (pathBuffer, siz
4ec0: 65 6f 66 28 70 61 74 68 42 75 66 66 65 72 29 2c  eof(pathBuffer),
4ed0: 20 22 25 73 2f 25 73 22 2c 20 6f 70 74 69 6f 6e   "%s/%s", option
4ee0: 73 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 2c  s->fake_newroot,
4ef0: 20 70 61 74 68 29 3b 0a 09 69 66 20 28 73 6e 70   path);..if (snp
4f00: 72 69 6e 74 66 5f 72 65 74 20 3c 20 30 20 7c 7c  rintf_ret < 0 ||
4f10: 20 28 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29   ((unsigned int)
4f20: 20 73 6e 70 72 69 6e 74 66 5f 72 65 74 29 20 3e   snprintf_ret) >
4f30: 3d 20 73 69 7a 65 6f 66 28 70 61 74 68 42 75 66  = sizeof(pathBuf
4f40: 66 65 72 29 29 20 7b 0a 09 09 66 69 6c 65 64 5f  fer)) {...filed_
4f50: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 55  log_msg_debug("U
4f60: 6e 61 62 6c 65 20 74 6f 20 74 72 61 6e 73 6c 61  nable to transla
4f70: 74 65 20 70 61 74 68 20 5c 22 25 73 5c 22 2c 20  te path \"%s\", 
4f80: 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74  will not fit int
4f90: 6f 20 6e 65 77 20 62 75 66 66 65 72 22 2c 20 70  o new buffer", p
4fa0: 61 74 68 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28  ath);....return(
4fb0: 6f 70 74 69 6f 6e 73 2d 3e 66 61 6b 65 5f 6e 65  options->fake_ne
4fc0: 77 72 6f 6f 74 29 3b 0a 09 7d 0a 0a 09 66 69 6c  wroot);..}...fil
4fd0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67  ed_log_msg_debug
4fe0: 28 22 54 72 61 6e 73 6c 61 74 69 6e 67 20 70 61  ("Translating pa
4ff0: 74 68 20 5c 22 25 73 5c 22 20 69 6e 74 6f 20 5c  th \"%s\" into \
5000: 22 25 73 5c 22 22 2c 20 70 61 74 68 2c 20 70 61  "%s\"", path, pa
5010: 74 68 42 75 66 66 65 72 29 3b 0a 0a 09 2f 2a 20  thBuffer);.../* 
5020: 52 65 74 75 72 6e 20 74 68 65 20 6e 65 77 20 70  Return the new p
5030: 61 74 68 20 2a 2f 0a 09 72 65 74 75 72 6e 28 70  ath */..return(p
5040: 61 74 68 42 75 66 66 65 72 29 3b 0a 7d 0a 0a 73  athBuffer);.}..s
5050: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64  tatic void filed
5060: 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 5f  _path_translate_
5070: 73 65 74 5f 72 6f 6f 74 28 63 6f 6e 73 74 20 63  set_root(const c
5080: 68 61 72 20 2a 76 61 72 2c 20 73 74 72 75 63 74  har *var, struct
5090: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a   filed_options *
50a0: 6f 70 74 69 6f 6e 73 2c 20 63 6f 6e 73 74 20 63  options, const c
50b0: 68 61 72 20 2a 76 61 6c 29 20 7b 0a 09 6f 70 74  har *val) {..opt
50c0: 69 6f 6e 73 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f  ions->fake_newro
50d0: 6f 74 20 3d 20 73 74 72 64 75 70 28 76 61 6c 29  ot = strdup(val)
50e0: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 0a 09 2f 2a  ;...return;.../*
50f0: 20 76 61 72 20 69 73 20 6f 6e 6c 79 20 75 73 65   var is only use
5100: 64 20 69 6e 20 74 68 65 20 6d 61 63 72 6f 20 2d  d in the macro -
5110: 2d 20 64 69 73 63 61 72 64 20 69 74 20 68 65 72  - discard it her
5120: 65 20 2a 2f 0a 09 76 61 72 20 3d 20 76 61 72 3b  e */..var = var;
5130: 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  .}.#else.#define
5140: 20 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 61 6e   filed_path_tran
5150: 73 6c 61 74 65 28 70 61 74 68 2c 20 6f 70 74 69  slate(path, opti
5160: 6f 6e 73 29 20 70 61 74 68 0a 23 64 65 66 69 6e  ons) path.#defin
5170: 65 20 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 61  e filed_path_tra
5180: 6e 73 6c 61 74 65 5f 73 65 74 5f 72 6f 6f 74 28  nslate_set_root(
5190: 76 61 72 2c 20 6f 70 74 69 6f 6e 73 2c 20 76 61  var, options, va
51a0: 6c 29 20 76 61 72 20 3d 20 73 74 72 64 75 70 28  l) var = strdup(
51b0: 76 61 6c 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  val).#endif../* 
51c0: 4f 70 65 6e 20 61 20 66 69 6c 65 20 61 6e 64 20  Open a file and 
51d0: 72 65 74 75 72 6e 20 66 69 6c 65 20 69 6e 66 6f  return file info
51e0: 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69  rmation */.stati
51f0: 63 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66  c struct filed_f
5200: 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 64 5f 6f  ileinfo *filed_o
5210: 70 65 6e 5f 66 69 6c 65 28 63 6f 6e 73 74 20 63  pen_file(const c
5220: 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 75 63  har *path, struc
5230: 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  t filed_fileinfo
5240: 20 2a 62 75 66 66 65 72 2c 20 73 74 72 75 63 74   *buffer, struct
5250: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a   filed_options *
5260: 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 72 75  options) {..stru
5270: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  ct filed_fileinf
5280: 6f 20 2a 63 61 63 68 65 3b 0a 09 75 6e 73 69 67  o *cache;..unsig
5290: 6e 65 64 20 69 6e 74 20 63 61 63 68 65 5f 69 64  ned int cache_id
52a0: 78 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09  x;..off_t len;..
52b0: 69 6e 74 20 66 64 3b 0a 0a 09 69 66 20 28 66 69  int fd;...if (fi
52c0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
52d0: 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 30 29 20  ache_size != 0) 
52e0: 7b 0a 09 09 63 61 63 68 65 5f 69 64 78 20 3d 20  {...cache_idx = 
52f0: 66 69 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e 73  filed_hash((cons
5300: 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
5310: 2a 29 20 70 61 74 68 2c 20 66 69 6c 65 64 5f 66  *) path, filed_f
5320: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f  ileinfo_fdcache_
5330: 73 69 7a 65 29 3b 0a 0a 09 09 63 61 63 68 65 20  size);....cache 
5340: 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  = &filed_fileinf
5350: 6f 5f 66 64 63 61 63 68 65 5b 63 61 63 68 65 5f  o_fdcache[cache_
5360: 69 64 78 5d 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c  idx];....filed_l
5370: 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 4c 6f  og_msg_debug("Lo
5380: 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20  cking mutex for 
5390: 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69  idx: %lu", (unsi
53a0: 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65  gned long) cache
53b0: 5f 69 64 78 29 3b 0a 0a 09 09 70 74 68 72 65 61  _idx);....pthrea
53c0: 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 63 61  d_mutex_lock(&ca
53d0: 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09  che->mutex);....
53e0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65  filed_log_msg_de
53f0: 62 75 67 28 22 43 6f 6d 70 6c 65 74 65 64 20 6c  bug("Completed l
5400: 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f 72  ocking mutex for
5410: 20 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73   idx: %lu", (uns
5420: 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68  igned long) cach
5430: 65 5f 69 64 78 29 3b 0a 09 7d 20 65 6c 73 65 20  e_idx);..} else 
5440: 7b 0a 09 09 63 61 63 68 65 5f 69 64 78 20 3d 20  {...cache_idx = 
5450: 30 3b 0a 09 09 63 61 63 68 65 20 3d 20 62 75 66  0;...cache = buf
5460: 66 65 72 3b 0a 09 09 63 61 63 68 65 2d 3e 70 61  fer;...cache->pa
5470: 74 68 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09  th[0] = '\0';...
5480: 63 61 63 68 65 2d 3e 66 64 20 3d 20 2d 31 3b 0a  cache->fd = -1;.
5490: 09 7d 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 28  .}...if (strcmp(
54a0: 70 61 74 68 2c 20 63 61 63 68 65 2d 3e 70 61 74  path, cache->pat
54b0: 68 29 20 21 3d 20 30 29 20 7b 0a 09 09 66 69 6c  h) != 0) {...fil
54c0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67  ed_log_msg_debug
54d0: 28 22 43 61 63 68 65 20 6d 69 73 73 20 66 6f 72  ("Cache miss for
54e0: 20 69 64 78 3a 20 25 6c 75 3a 20 4f 4c 44 20 5c   idx: %lu: OLD \
54f0: 22 25 73 5c 22 2c 20 4e 45 57 20 5c 22 25 73 5c  "%s\", NEW \"%s\
5500: 22 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f  "", (unsigned lo
5510: 6e 67 29 20 63 61 63 68 65 5f 69 64 78 2c 20 63  ng) cache_idx, c
5520: 61 63 68 65 2d 3e 70 61 74 68 2c 20 70 61 74 68  ache->path, path
5530: 29 3b 0a 0a 09 09 66 64 20 3d 20 6f 70 65 6e 28  );....fd = open(
5540: 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 61 6e 73  filed_path_trans
5550: 6c 61 74 65 28 70 61 74 68 2c 20 6f 70 74 69 6f  late(path, optio
5560: 6e 73 29 2c 20 4f 5f 52 44 4f 4e 4c 59 20 7c 20  ns), O_RDONLY | 
5570: 4f 5f 4c 41 52 47 45 46 49 4c 45 29 3b 0a 09 09  O_LARGEFILE);...
5580: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09  if (fd < 0) {...
5590: 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69  .if (filed_filei
55a0: 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65  nfo_fdcache_size
55b0: 20 21 3d 20 30 29 20 7b 0a 09 09 09 09 70 74 68   != 0) {.....pth
55c0: 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63  read_mutex_unloc
55d0: 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29  k(&cache->mutex)
55e0: 3b 0a 09 09 09 7d 0a 0a 09 09 09 72 65 74 75 72  ;....}.....retur
55f0: 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09  n(NULL);...}....
5600: 69 66 20 28 63 61 63 68 65 2d 3e 66 64 20 3e 3d  if (cache->fd >=
5610: 20 30 29 20 7b 0a 09 09 09 63 6c 6f 73 65 28 63   0) {....close(c
5620: 61 63 68 65 2d 3e 66 64 29 3b 0a 09 09 7d 0a 0a  ache->fd);...}..
5630: 09 09 6c 65 6e 20 3d 20 6c 73 65 65 6b 28 66 64  ..len = lseek(fd
5640: 2c 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29 3b 0a  , 0, SEEK_END);.
5650: 09 09 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 53  ..lseek(fd, 0, S
5660: 45 45 4b 5f 53 45 54 29 3b 0a 0a 09 09 63 61 63  EEK_SET);....cac
5670: 68 65 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 09 63  he->fd = fd;...c
5680: 61 63 68 65 2d 3e 6c 65 6e 20 3d 20 6c 65 6e 3b  ache->len = len;
5690: 0a 09 09 73 74 72 63 70 79 28 63 61 63 68 65 2d  ...strcpy(cache-
56a0: 3e 70 61 74 68 2c 20 70 61 74 68 29 3b 0a 09 09  >path, path);...
56b0: 63 61 63 68 65 2d 3e 74 79 70 65 20 3d 20 66 69  cache->type = fi
56c0: 6c 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f 6d 69  led_determine_mi
56d0: 6d 65 74 79 70 65 28 70 61 74 68 29 3b 0a 09 09  metype(path);...
56e0: 66 69 6c 65 64 5f 67 65 6e 65 72 61 74 65 5f 65  filed_generate_e
56f0: 74 61 67 28 63 61 63 68 65 2d 3e 65 74 61 67 2c  tag(cache->etag,
5700: 20 73 69 7a 65 6f 66 28 63 61 63 68 65 2d 3e 65   sizeof(cache->e
5710: 74 61 67 29 29 3b 0a 0a 09 09 2f 2a 20 58 58 58  tag));..../* XXX
5720: 3a 54 4f 44 4f 3a 20 44 65 74 65 72 6d 69 6e 65  :TODO: Determine
5730: 20 2a 2f 0a 09 09 63 61 63 68 65 2d 3e 6c 61 73   */...cache->las
5740: 74 6d 6f 64 20 3d 20 66 69 6c 65 64 5f 66 6f 72  tmod = filed_for
5750: 6d 61 74 5f 74 69 6d 65 28 63 61 63 68 65 2d 3e  mat_time(cache->
5760: 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f  lastmod_b, sizeo
5770: 66 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64  f(cache->lastmod
5780: 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 20  _b), time(NULL) 
5790: 2d 20 33 30 29 3b 0a 09 7d 20 65 6c 73 65 20 7b  - 30);..} else {
57a0: 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  ...filed_log_msg
57b0: 5f 64 65 62 75 67 28 22 43 61 63 68 65 20 68 69  _debug("Cache hi
57c0: 74 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20  t for idx: %lu: 
57d0: 50 41 54 48 20 5c 22 25 73 5c 22 22 2c 20 28 75  PATH \"%s\"", (u
57e0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61  nsigned long) ca
57f0: 63 68 65 5f 69 64 78 2c 20 70 61 74 68 29 3b 0a  che_idx, path);.
5800: 09 7d 0a 0a 09 69 66 20 28 66 69 6c 65 64 5f 66  .}...if (filed_f
5810: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f  ileinfo_fdcache_
5820: 73 69 7a 65 20 21 3d 20 30 29 20 7b 0a 09 09 2f  size != 0) {.../
5830: 2a 0a 09 09 20 2a 20 57 65 20 68 61 76 65 20 74  *... * We have t
5840: 6f 20 6d 61 6b 65 20 61 20 64 75 70 6c 69 63 61  o make a duplica
5850: 74 65 20 46 44 2c 20 62 65 63 61 75 73 65 20 6f  te FD, because o
5860: 6e 63 65 20 77 65 20 72 65 6c 65 61 73 65 20 74  nce we release t
5870: 68 65 20 63 61 63 68 65 0a 09 09 20 2a 20 6d 75  he cache... * mu
5880: 74 65 78 2c 20 74 68 65 20 66 69 6c 65 20 64 65  tex, the file de
5890: 73 63 72 69 70 74 6f 72 20 6d 61 79 20 62 65 20  scriptor may be 
58a0: 63 6c 6f 73 65 64 0a 09 09 20 2a 2f 0a 09 09 66  closed... */...f
58b0: 64 20 3d 20 64 75 70 28 63 61 63 68 65 2d 3e 66  d = dup(cache->f
58c0: 64 29 3b 0a 09 09 69 66 20 28 66 64 20 3c 20 30  d);...if (fd < 0
58d0: 29 20 7b 0a 09 09 09 70 74 68 72 65 61 64 5f 6d  ) {....pthread_m
58e0: 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63  utex_unlock(&cac
58f0: 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09  he->mutex);.....
5900: 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09  return(NULL);...
5910: 7d 0a 0a 09 09 62 75 66 66 65 72 2d 3e 66 64 20  }....buffer->fd 
5920: 3d 20 66 64 3b 0a 09 09 62 75 66 66 65 72 2d 3e  = fd;...buffer->
5930: 6c 65 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e  len = cache->len
5940: 3b 0a 09 09 62 75 66 66 65 72 2d 3e 74 79 70 65  ;...buffer->type
5950: 20 3d 20 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a   = cache->type;.
5960: 09 09 6d 65 6d 63 70 79 28 62 75 66 66 65 72 2d  ..memcpy(buffer-
5970: 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 63 61 63 68  >lastmod_b, cach
5980: 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69  e->lastmod_b, si
5990: 7a 65 6f 66 28 62 75 66 66 65 72 2d 3e 6c 61 73  zeof(buffer->las
59a0: 74 6d 6f 64 5f 62 29 29 3b 0a 09 09 6d 65 6d 63  tmod_b));...memc
59b0: 70 79 28 62 75 66 66 65 72 2d 3e 65 74 61 67 2c  py(buffer->etag,
59c0: 20 63 61 63 68 65 2d 3e 65 74 61 67 2c 20 73 69   cache->etag, si
59d0: 7a 65 6f 66 28 62 75 66 66 65 72 2d 3e 65 74 61  zeof(buffer->eta
59e0: 67 29 29 3b 0a 09 09 62 75 66 66 65 72 2d 3e 6c  g));...buffer->l
59f0: 61 73 74 6d 6f 64 20 3d 20 62 75 66 66 65 72 2d  astmod = buffer-
5a00: 3e 6c 61 73 74 6d 6f 64 5f 62 20 2b 20 28 63 61  >lastmod_b + (ca
5a10: 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 2d 20 63  che->lastmod - c
5a20: 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29  ache->lastmod_b)
5a30: 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74  ;....pthread_mut
5a40: 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65  ex_unlock(&cache
5a50: 2d 3e 6d 75 74 65 78 29 3b 0a 09 7d 0a 0a 09 72  ->mutex);..}...r
5a60: 65 74 75 72 6e 28 62 75 66 66 65 72 29 3b 0a 0a  eturn(buffer);..
5a70: 09 2f 2a 20 6f 70 74 69 6f 6e 73 20 69 73 20 6f  ./* options is o
5a80: 6e 6c 79 20 75 73 65 64 20 69 66 20 66 61 6b 65  nly used if fake
5a90: 20 63 68 72 6f 6f 74 20 69 73 20 65 6e 61 62 6c   chroot is enabl
5aa0: 65 64 2c 20 63 6f 6e 66 75 73 65 20 74 68 65 20  ed, confuse the 
5ab0: 63 6f 6d 70 69 6c 65 72 20 2a 2f 0a 09 6f 70 74  compiler */..opt
5ac0: 69 6f 6e 73 20 3d 20 6f 70 74 69 6f 6e 73 3b 0a  ions = options;.
5ad0: 7d 0a 0a 2f 2a 20 50 72 6f 63 65 73 73 20 61 6e  }../* Process an
5ae0: 20 48 54 54 50 20 72 65 71 75 65 73 74 20 61 6e   HTTP request an
5af0: 64 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 74  d return the pat
5b00: 68 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 73  h requested */.s
5b10: 74 61 74 69 63 20 73 74 72 75 63 74 20 66 69 6c  tatic struct fil
5b20: 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20  ed_http_request 
5b30: 2a 66 69 6c 65 64 5f 67 65 74 5f 68 74 74 70 5f  *filed_get_http_
5b40: 72 65 71 75 65 73 74 28 46 49 4c 45 20 2a 66 70  request(FILE *fp
5b50: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68  , struct filed_h
5b60: 74 74 70 5f 72 65 71 75 65 73 74 20 2a 62 75 66  ttp_request *buf
5b70: 66 65 72 5f 73 74 2c 20 73 74 72 75 63 74 20 66  fer_st, struct f
5b80: 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70  iled_options *op
5b90: 74 69 6f 6e 73 29 20 7b 0a 09 63 68 61 72 20 2a  tions) {..char *
5ba0: 6d 65 74 68 6f 64 2c 20 2a 70 61 74 68 3b 0a 09  method, *path;..
5bb0: 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 2a 77  char *buffer, *w
5bc0: 6f 72 6b 62 75 66 66 65 72 2c 20 2a 77 6f 72 6b  orkbuffer, *work
5bd0: 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 09 63 68  buffer_next;..ch
5be0: 61 72 20 2a 66 67 65 74 73 5f 72 65 74 3b 0a 09  ar *fgets_ret;..
5bf0: 73 69 7a 65 5f 74 20 62 75 66 66 65 72 5f 6c 65  size_t buffer_le
5c00: 6e 2c 20 70 61 74 68 5f 6c 65 6e 3b 0a 09 6f 66  n, path_len;..of
5c10: 66 5f 74 20 72 61 6e 67 65 5f 73 74 61 72 74 2c  f_t range_start,
5c20: 20 72 61 6e 67 65 5f 65 6e 64 2c 20 72 61 6e 67   range_end, rang
5c30: 65 5f 6c 65 6e 67 74 68 3b 0a 09 69 6e 74 20 72  e_length;..int r
5c40: 61 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a 09 69  ange_request;..i
5c50: 6e 74 20 73 6e 70 72 69 6e 74 66 5f 72 65 74 3b  nt snprintf_ret;
5c60: 0a 09 69 6e 74 20 69 3b 0a 0a 09 2f 2a 20 53 65  ..int i;.../* Se
5c70: 74 20 74 6f 20 64 65 66 61 75 6c 74 20 76 61 6c  t to default val
5c80: 75 65 73 20 2a 2f 0a 09 72 61 6e 67 65 5f 73 74  ues */..range_st
5c90: 61 72 74 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f  art = 0;..range_
5ca0: 65 6e 64 20 20 20 3d 20 30 3b 0a 09 72 61 6e 67  end   = 0;..rang
5cb0: 65 5f 72 65 71 75 65 73 74 20 3d 20 30 3b 0a 09  e_request = 0;..
5cc0: 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d 20 2d  range_length = -
5cd0: 31 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68  1;..buffer_st->h
5ce0: 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73  eaders.host.pres
5cf0: 65 6e 74 20 3d 20 30 3b 0a 09 62 75 66 66 65 72  ent = 0;..buffer
5d00: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e  _st->headers.con
5d10: 6e 65 63 74 69 6f 6e 20 3d 20 46 49 4c 45 44 5f  nection = FILED_
5d20: 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45  CONNECTION_CLOSE
5d30: 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 62 75 66  ;...buffer = buf
5d40: 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a  fer_st->tmpbuf;.
5d50: 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 69  .buffer_len = si
5d60: 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 74 2d 3e  zeof(buffer_st->
5d70: 74 6d 70 62 75 66 29 3b 0a 0a 09 66 67 65 74 73  tmpbuf);...fgets
5d80: 5f 72 65 74 20 3d 20 66 67 65 74 73 28 62 75 66  _ret = fgets(buf
5d90: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c  fer, buffer_len,
5da0: 20 66 70 29 3b 0a 09 69 66 20 28 66 67 65 74 73   fp);..if (fgets
5db0: 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  _ret == NULL) {.
5dc0: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a  ..return(NULL);.
5dd0: 09 7d 0a 0a 09 6d 65 74 68 6f 64 20 3d 20 62 75  .}...method = bu
5de0: 66 66 65 72 3b 0a 0a 09 62 75 66 66 65 72 20 3d  ffer;...buffer =
5df0: 20 73 74 72 63 68 72 28 62 75 66 66 65 72 2c 20   strchr(buffer, 
5e00: 27 20 27 29 3b 0a 09 69 66 20 28 62 75 66 66 65  ' ');..if (buffe
5e10: 72 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72  r == NULL) {...r
5e20: 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a  eturn(NULL);..}.
5e30: 0a 09 2a 62 75 66 66 65 72 20 3d 20 27 5c 30 27  ..*buffer = '\0'
5e40: 3b 0a 09 62 75 66 66 65 72 2b 2b 3b 0a 0a 09 70  ;..buffer++;...p
5e50: 61 74 68 20 3d 20 62 75 66 66 65 72 3b 0a 0a 09  ath = buffer;...
5e60: 2f 2a 20 54 65 72 6d 69 6e 61 74 65 20 70 61 74  /* Terminate pat
5e70: 68 20 63 6f 6d 70 6f 6e 65 6e 74 20 2a 2f 0a 09  h component */..
5e80: 62 75 66 66 65 72 20 3d 20 73 74 72 70 62 72 6b  buffer = strpbrk
5e90: 28 70 61 74 68 2c 20 22 5c 72 5c 6e 20 22 29 3b  (path, "\r\n ");
5ea0: 0a 09 69 66 20 28 62 75 66 66 65 72 20 21 3d 20  ..if (buffer != 
5eb0: 4e 55 4c 4c 29 20 7b 0a 09 09 2a 62 75 66 66 65  NULL) {...*buffe
5ec0: 72 20 3d 20 27 5c 30 27 3b 0a 09 09 62 75 66 66  r = '\0';...buff
5ed0: 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 65  er++;..}.../* We
5ee0: 20 6f 6e 6c 79 20 68 61 6e 64 6c 65 20 74 68 65   only handle the
5ef0: 20 22 47 45 54 22 20 61 6e 64 20 22 48 45 41 44   "GET" and "HEAD
5f00: 27 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 09 69 66  ' methods */..if
5f10: 20 28 73 74 72 63 61 73 65 63 6d 70 28 6d 65 74   (strcasecmp(met
5f20: 68 6f 64 2c 20 22 68 65 61 64 22 29 20 21 3d 20  hod, "head") != 
5f30: 30 29 20 7b 0a 09 09 69 66 20 28 73 74 72 63 61  0) {...if (strca
5f40: 73 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 22 67  secmp(method, "g
5f50: 65 74 22 29 20 21 3d 20 30 29 20 7b 0a 09 09 09  et") != 0) {....
5f60: 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09  return(NULL);...
5f70: 7d 0a 0a 09 09 2f 2a 20 47 45 54 20 72 65 71 75  }..../* GET requ
5f80: 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 72 5f  est */...buffer_
5f90: 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 49 4c  st->method = FIL
5fa0: 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f  ED_REQUEST_METHO
5fb0: 44 5f 47 45 54 3b 0a 09 7d 20 65 6c 73 65 20 7b  D_GET;..} else {
5fc0: 0a 09 09 2f 2a 20 48 45 41 44 20 72 65 71 75 65  .../* HEAD reque
5fd0: 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 72 5f 73  st */...buffer_s
5fe0: 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 49 4c 45  t->method = FILE
5ff0: 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44  D_REQUEST_METHOD
6000: 5f 48 45 41 44 3b 0a 09 7d 0a 0a 09 2f 2a 20 4e  _HEAD;..}.../* N
6010: 6f 74 65 20 70 61 74 68 20 2a 2f 0a 09 70 61 74  ote path */..pat
6020: 68 5f 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 28 70  h_len = strlen(p
6030: 61 74 68 29 3b 0a 09 6d 65 6d 63 70 79 28 62 75  ath);..memcpy(bu
6040: 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c 20 70  ffer_st->path, p
6050: 61 74 68 2c 20 70 61 74 68 5f 6c 65 6e 20 2b 20  ath, path_len + 
6060: 31 29 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69  1);.../* Determi
6070: 6e 65 20 74 79 70 65 20 6f 66 20 72 65 71 75 65  ne type of reque
6080: 73 74 20 66 72 6f 6d 20 70 61 74 68 20 2a 2f 0a  st from path */.
6090: 09 69 66 20 28 70 61 74 68 5f 6c 65 6e 20 3d 3d  .if (path_len ==
60a0: 20 30 29 20 7b 0a 09 09 62 75 66 66 65 72 5f 73   0) {...buffer_s
60b0: 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f  t->type = FILED_
60c0: 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 49 52  REQUEST_TYPE_DIR
60d0: 45 43 54 4f 52 59 3b 0a 09 7d 20 65 6c 73 65 20  ECTORY;..} else 
60e0: 7b 0a 09 09 69 66 20 28 70 61 74 68 5b 70 61 74  {...if (path[pat
60f0: 68 5f 6c 65 6e 20 2d 20 31 5d 20 3d 3d 20 27 2f  h_len - 1] == '/
6100: 27 29 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73  ') {....buffer_s
6110: 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f  t->type = FILED_
6120: 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 49 52  REQUEST_TYPE_DIR
6130: 45 43 54 4f 52 59 3b 0a 09 09 7d 20 65 6c 73 65  ECTORY;...} else
6140: 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 74 2d   {....buffer_st-
6150: 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 52 45  >type = FILED_RE
6160: 51 55 45 53 54 5f 54 59 50 45 5f 4f 54 48 45 52  QUEST_TYPE_OTHER
6170: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 52 65  ;...}..}.../* Re
6180: 73 65 74 20 62 75 66 66 65 72 20 66 6f 72 20 6c  set buffer for l
6190: 61 74 65 72 20 75 73 65 20 2a 2f 0a 09 62 75 66  ater use */..buf
61a0: 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d  fer = buffer_st-
61b0: 3e 74 6d 70 62 75 66 3b 0a 0a 09 66 6f 72 20 28  >tmpbuf;...for (
61c0: 69 20 3d 20 30 3b 20 69 20 3c 20 31 30 30 3b 20  i = 0; i < 100; 
61d0: 69 2b 2b 29 20 7b 0a 09 09 66 67 65 74 73 5f 72  i++) {...fgets_r
61e0: 65 74 20 3d 20 66 67 65 74 73 28 62 75 66 66 65  et = fgets(buffe
61f0: 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 66  r, buffer_len, f
6200: 70 29 3b 0a 09 09 69 66 20 28 66 67 65 74 73 5f  p);...if (fgets_
6210: 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09  ret == NULL) {..
6220: 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09 09  ..break;...}....
6230: 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28  if (strncasecmp(
6240: 62 75 66 66 65 72 2c 20 22 52 61 6e 67 65 3a 20  buffer, "Range: 
6250: 22 2c 20 37 29 20 3d 3d 20 30 29 20 7b 0a 09 09  ", 7) == 0) {...
6260: 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 62 75  .workbuffer = bu
6270: 66 66 65 72 20 2b 20 37 3b 0a 0a 09 09 09 69 66  ffer + 7;.....if
6280: 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 77 6f   (strncasecmp(wo
6290: 72 6b 62 75 66 66 65 72 2c 20 22 62 79 74 65 73  rkbuffer, "bytes
62a0: 3d 22 2c 20 36 29 20 3d 3d 20 30 29 20 7b 0a 09  =", 6) == 0) {..
62b0: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 2b 3d  ...workbuffer +=
62c0: 20 36 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f 72   6;......range_r
62d0: 65 71 75 65 73 74 20 3d 20 31 3b 0a 0a 09 09 09  equest = 1;.....
62e0: 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d 20 73  .range_start = s
62f0: 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66  trtoull(workbuff
6300: 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 72 5f  er, &workbuffer_
6310: 6e 65 78 74 2c 20 31 30 29 3b 0a 0a 09 09 09 09  next, 10);......
6320: 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 77 6f 72  workbuffer = wor
6330: 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 0a 09  kbuffer_next;...
6340: 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 66 66  ...if (*workbuff
6350: 65 72 20 3d 3d 20 27 2d 27 29 20 7b 0a 09 09 09  er == '-') {....
6360: 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a  ..workbuffer++;.
6370: 0a 09 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62  ......if (*workb
6380: 75 66 66 65 72 20 21 3d 20 27 5c 72 27 20 26 26  uffer != '\r' &&
6390: 20 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20   *workbuffer != 
63a0: 27 5c 6e 27 29 20 7b 0a 09 09 09 09 09 09 72 61  '\n') {.......ra
63b0: 6e 67 65 5f 65 6e 64 20 3d 20 73 74 72 74 6f 75  nge_end = strtou
63c0: 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 26  ll(workbuffer, &
63d0: 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 2c  workbuffer_next,
63e0: 20 31 30 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09   10);......}....
63f0: 09 7d 0a 09 09 09 7d 0a 09 09 7d 20 65 6c 73 65  .}....}...} else
6400: 20 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70   if (strncasecmp
6410: 28 62 75 66 66 65 72 2c 20 22 48 6f 73 74 3a 20  (buffer, "Host: 
6420: 22 2c 20 35 29 20 3d 3d 20 30 29 20 7b 0a 09 09  ", 5) == 0) {...
6430: 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64  .buffer_st->head
6440: 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74  ers.host.present
6450: 20 3d 20 31 3b 0a 0a 09 09 09 77 6f 72 6b 62 75   = 1;.....workbu
6460: 66 66 65 72 20 3d 20 73 74 72 70 62 72 6b 28 62  ffer = strpbrk(b
6470: 75 66 66 65 72 20 2b 20 35 2c 20 22 5c 72 5c 6e  uffer + 5, "\r\n
6480: 3a 22 29 3b 0a 09 09 09 69 66 20 28 77 6f 72 6b  :");....if (work
6490: 62 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c 29 20  buffer != NULL) 
64a0: 7b 0a 09 09 09 09 2a 77 6f 72 6b 62 75 66 66 65  {.....*workbuffe
64b0: 72 20 3d 20 27 5c 30 27 3b 0a 09 09 09 7d 0a 0a  r = '\0';....}..
64c0: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20  ...workbuffer = 
64d0: 62 75 66 66 65 72 20 2b 20 35 3b 0a 09 09 09 77  buffer + 5;....w
64e0: 68 69 6c 65 20 28 2a 77 6f 72 6b 62 75 66 66 65  hile (*workbuffe
64f0: 72 20 3d 3d 20 27 20 27 29 20 7b 0a 09 09 09 09  r == ' ') {.....
6500: 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a 09 09  workbuffer++;...
6510: 09 7d 0a 0a 09 09 09 73 74 72 63 70 79 28 62 75  .}.....strcpy(bu
6520: 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73  ffer_st->headers
6530: 2e 68 6f 73 74 2e 68 6f 73 74 2c 20 77 6f 72 6b  .host.host, work
6540: 62 75 66 66 65 72 29 3b 0a 09 09 7d 20 65 6c 73  buffer);...} els
6550: 65 20 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d  e if (strncasecm
6560: 70 28 62 75 66 66 65 72 2c 20 22 43 6f 6e 6e 65  p(buffer, "Conne
6570: 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69 76  ction: Keep-Aliv
6580: 65 22 2c 20 32 32 29 20 3d 3d 20 30 29 20 7b 0a  e", 22) == 0) {.
6590: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65  ...buffer_st->he
65a0: 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e  aders.connection
65b0: 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54   = FILED_CONNECT
65c0: 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 3b 0a  ION_KEEP_ALIVE;.
65d0: 09 09 7d 0a 0a 09 09 69 66 20 28 6d 65 6d 63 6d  ..}....if (memcm
65e0: 70 28 62 75 66 66 65 72 2c 20 22 5c 72 5c 6e 22  p(buffer, "\r\n"
65f0: 2c 20 32 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09  , 2) == 0) {....
6600: 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a 09  break;...}..}...
6610: 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 72 61 6e  /* Determine ran
6620: 67 65 20 2a 2f 0a 09 69 66 20 28 72 61 6e 67 65  ge */..if (range
6630: 5f 65 6e 64 20 21 3d 20 30 29 20 7b 0a 09 09 69  _end != 0) {...i
6640: 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 3c 3d 20  f (range_end <= 
6650: 72 61 6e 67 65 5f 73 74 61 72 74 29 20 7b 0a 09  range_start) {..
6660: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a  ..return(NULL);.
6670: 09 09 7d 0a 0a 09 09 72 61 6e 67 65 5f 6c 65 6e  ..}....range_len
6680: 67 74 68 20 3d 20 72 61 6e 67 65 5f 65 6e 64 20  gth = range_end 
6690: 2d 20 72 61 6e 67 65 5f 73 74 61 72 74 3b 0a 0a  - range_start;..
66a0: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  ..filed_log_msg_
66b0: 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 6e 67  debug("Computing
66c0: 20 6c 65 6e 67 74 68 20 70 61 72 61 6d 65 74 65   length paramete
66d0: 72 3a 20 25 6c 6c 75 20 3d 20 25 6c 6c 75 20 2d  r: %llu = %llu -
66e0: 20 25 6c 6c 75 22 2c 0a 09 09 09 28 75 6e 73 69   %llu",....(unsi
66f0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
6700: 72 61 6e 67 65 5f 6c 65 6e 67 74 68 2c 0a 09 09  range_length,...
6710: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
6720: 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 65 6e 64 2c  long) range_end,
6730: 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f  ....(unsigned lo
6740: 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 73  ng long) range_s
6750: 74 61 72 74 0a 09 09 29 3b 0a 09 7d 0a 0a 09 2f  tart...);..}.../
6760: 2a 20 46 69 6c 6c 20 75 70 20 73 74 72 75 63 74  * Fill up struct
6770: 75 72 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  ure to return */
6780: 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61  ..buffer_st->hea
6790: 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 73 65  ders.range.prese
67a0: 6e 74 20 3d 20 72 61 6e 67 65 5f 72 65 71 75 65  nt = range_reque
67b0: 73 74 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e  st;..buffer_st->
67c0: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66  headers.range.of
67d0: 66 73 65 74 20 20 3d 20 72 61 6e 67 65 5f 73 74  fset  = range_st
67e0: 61 72 74 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d  art;..buffer_st-
67f0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c  >headers.range.l
6800: 65 6e 67 74 68 20 20 3d 20 72 61 6e 67 65 5f 6c  ength  = range_l
6810: 65 6e 67 74 68 3b 0a 0a 09 2f 2a 20 49 66 20 76  ength;.../* If v
6820: 68 6f 73 74 73 20 61 72 65 20 65 6e 61 62 6c 65  hosts are enable
6830: 64 2c 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 70  d, compute new p
6840: 61 74 68 20 2a 2f 0a 09 69 66 20 28 6f 70 74 69  ath */..if (opti
6850: 6f 6e 73 2d 3e 76 68 6f 73 74 73 5f 65 6e 61 62  ons->vhosts_enab
6860: 6c 65 64 29 20 7b 0a 09 09 69 66 20 28 62 75 66  led) {...if (buf
6870: 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e  fer_st->headers.
6880: 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d 3d 20  host.present == 
6890: 31 29 20 7b 0a 09 09 09 62 75 66 66 65 72 20 3d  1) {....buffer =
68a0: 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62   buffer_st->tmpb
68b0: 75 66 3b 0a 09 09 09 62 75 66 66 65 72 5f 6c 65  uf;....buffer_le
68c0: 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 66 65  n = sizeof(buffe
68d0: 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a  r_st->tmpbuf);..
68e0: 09 09 09 73 6e 70 72 69 6e 74 66 5f 72 65 74 20  ...snprintf_ret 
68f0: 3d 20 73 6e 70 72 69 6e 74 66 28 62 75 66 66 65  = snprintf(buffe
6900: 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 22  r, buffer_len, "
6910: 2f 25 73 25 73 25 73 22 2c 0a 09 09 09 09 62 75  /%s%s%s",.....bu
6920: 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73  ffer_st->headers
6930: 2e 68 6f 73 74 2e 68 6f 73 74 2c 0a 09 09 09 09  .host.host,.....
6940: 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 5b  buffer_st->path[
6950: 30 5d 20 3d 3d 20 27 2f 27 20 3f 20 22 22 20 3a  0] == '/' ? "" :
6960: 20 22 2f 22 2c 0a 09 09 09 09 62 75 66 66 65 72   "/",.....buffer
6970: 5f 73 74 2d 3e 70 61 74 68 0a 09 09 09 29 3b 0a  _st->path....);.
6980: 09 09 09 69 66 20 28 73 6e 70 72 69 6e 74 66 5f  ...if (snprintf_
6990: 72 65 74 20 3e 3d 20 30 29 20 7b 0a 09 09 09 09  ret >= 0) {.....
69a0: 69 66 20 28 28 28 75 6e 73 69 67 6e 65 64 20 69  if (((unsigned i
69b0: 6e 74 29 20 73 6e 70 72 69 6e 74 66 5f 72 65 74  nt) snprintf_ret
69c0: 29 20 3c 20 62 75 66 66 65 72 5f 6c 65 6e 29 20  ) < buffer_len) 
69d0: 7b 0a 09 09 09 09 09 73 74 72 63 70 79 28 62 75  {......strcpy(bu
69e0: 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c 20 62  ffer_st->path, b
69f0: 75 66 66 65 72 29 3b 0a 09 09 09 09 7d 0a 09 09  uffer);.....}...
6a00: 09 7d 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75  .}...}..}...retu
6a10: 72 6e 28 62 75 66 66 65 72 5f 73 74 29 3b 0a 7d  rn(buffer_st);.}
6a20: 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65  ../* Return an e
6a30: 72 72 6f 72 20 70 61 67 65 20 2a 2f 0a 73 74 61  rror page */.sta
6a40: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 65  tic void filed_e
6a50: 72 72 6f 72 5f 70 61 67 65 28 46 49 4c 45 20 2a  rror_page(FILE *
6a60: 66 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  fp, const char *
6a70: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 69 6e  date_current, in
6a80: 74 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 2c 20  t error_number, 
6a90: 69 6e 74 20 6d 65 74 68 6f 64 2c 20 63 6f 6e 73  int method, cons
6aa0: 74 20 63 68 61 72 20 2a 72 65 61 73 6f 6e 2c 20  t char *reason, 
6ab0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
6ac0: 5f 65 6e 74 72 79 20 2a 6c 6f 67 29 20 7b 0a 09  _entry *log) {..
6ad0: 63 68 61 72 20 2a 65 72 72 6f 72 5f 73 74 72 69  char *error_stri
6ae0: 6e 67 20 3d 20 22 3c 68 74 6d 6c 3e 3c 68 65 61  ng = "<html><hea
6af0: 64 3e 3c 74 69 74 6c 65 3e 45 52 52 4f 52 3c 2f  d><title>ERROR</
6b00: 74 69 74 6c 65 3e 3c 2f 68 65 61 64 3e 3c 62 6f  title></head><bo
6b10: 64 79 3e 55 6e 61 62 6c 65 20 74 6f 20 70 72 6f  dy>Unable to pro
6b20: 63 65 73 73 20 72 65 71 75 65 73 74 3c 2f 62 6f  cess request</bo
6b30: 64 79 3e 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a 09 66  dy></html>";...f
6b40: 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50  printf(fp, "HTTP
6b50: 2f 31 2e 31 20 25 69 20 4e 6f 74 20 4f 4b 5c 72  /1.1 %i Not OK\r
6b60: 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65  \nDate: %s\r\nSe
6b70: 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c  rver: filed\r\nL
6b80: 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73  ast-Modified: %s
6b90: 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67  \r\nContent-Leng
6ba0: 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 43 6f 6e 74  th: %llu\r\nCont
6bb0: 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e  ent-Type: %s\r\n
6bc0: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73  Connection: clos
6bd0: 65 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 65 72  e\r\n\r\n",...er
6be0: 72 6f 72 5f 6e 75 6d 62 65 72 2c 0a 09 09 64 61  ror_number,...da
6bf0: 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64 61  te_current,...da
6c00: 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 28 75  te_current,...(u
6c10: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
6c20: 67 29 20 73 74 72 6c 65 6e 28 65 72 72 6f 72 5f  g) strlen(error_
6c30: 73 74 72 69 6e 67 29 2c 0a 09 09 22 74 65 78 74  string),..."text
6c40: 2f 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f 2a 20  /html"..);.../* 
6c50: 73 69 6c 65 6e 63 65 20 65 72 72 6f 72 20 73 74  silence error st
6c60: 72 69 6e 67 20 66 6f 72 20 48 45 41 44 20 72 65  ring for HEAD re
6c70: 71 75 65 73 74 73 20 2a 2f 0a 09 69 66 20 28 6d  quests */..if (m
6c80: 65 74 68 6f 64 20 21 3d 20 46 49 4c 45 44 5f 52  ethod != FILED_R
6c90: 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45  EQUEST_METHOD_HE
6ca0: 41 44 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 28  AD) {...fprintf(
6cb0: 66 70 2c 20 22 25 73 22 2c 20 65 72 72 6f 72 5f  fp, "%s", error_
6cc0: 73 74 72 69 6e 67 29 3b 0a 09 7d 0a 0a 09 2f 2a  string);..}.../*
6cd0: 20 4c 6f 67 20 65 72 72 6f 72 20 2a 2f 0a 09 2f   Log error */../
6ce0: 2a 2a 20 72 65 61 73 6f 6e 20 6d 75 73 74 20 70  ** reason must p
6cf0: 6f 69 6e 74 20 74 6f 20 61 20 67 6c 6f 62 61 6c  oint to a global
6d00: 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 76 61 6c  ly allocated val
6d10: 75 65 20 2a 2a 2f 0a 09 6c 6f 67 2d 3e 72 65 61  ue **/..log->rea
6d20: 73 6f 6e 20 3d 20 72 65 61 73 6f 6e 3b 0a 09 6c  son = reason;..l
6d30: 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20  og->http_code = 
6d40: 65 72 72 6f 72 5f 6e 75 6d 62 65 72 3b 0a 0a 09  error_number;...
6d50: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28  filed_log_entry(
6d60: 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f 73 65  log);.../* Close
6d70: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09   connection */..
6d80: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
6d90: 6f 75 74 5f 63 6c 6f 73 65 28 66 69 6c 65 6e 6f  out_close(fileno
6da0: 28 66 70 29 29 3b 0a 0a 09 66 63 6c 6f 73 65 28  (fp));...fclose(
6db0: 66 70 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d  fp);...return;.}
6dc0: 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 72 65  ../* Return a re
6dd0: 64 69 72 65 63 74 20 74 6f 20 69 6e 64 65 78 2e  direct to index.
6de0: 68 74 6d 6c 20 2a 2f 0a 73 74 61 74 69 63 20 76  html */.static v
6df0: 6f 69 64 20 66 69 6c 65 64 5f 72 65 64 69 72 65  oid filed_redire
6e00: 63 74 5f 69 6e 64 65 78 28 46 49 4c 45 20 2a 66  ct_index(FILE *f
6e10: 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64  p, const char *d
6e20: 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 63 6f 6e  ate_current, con
6e30: 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 73  st char *path, s
6e40: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f  truct filed_log_
6e50: 65 6e 74 72 79 20 2a 6c 6f 67 29 20 7b 0a 09 69  entry *log) {..i
6e60: 6e 74 20 68 74 74 70 5f 63 6f 64 65 20 3d 20 33  nt http_code = 3
6e70: 30 31 3b 0a 09 66 70 72 69 6e 74 66 28 66 70 2c  01;..fprintf(fp,
6e80: 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b   "HTTP/1.1 %i OK
6e90: 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e  \r\nDate: %s\r\n
6ea0: 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c  Server: filed\r\
6eb0: 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20  nLast-Modified: 
6ec0: 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65  %s\r\nContent-Le
6ed0: 6e 67 74 68 3a 20 30 5c 72 5c 6e 43 6f 6e 6e 65  ngth: 0\r\nConne
6ee0: 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e  ction: close\r\n
6ef0: 4c 6f 63 61 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e  Location: %s\r\n
6f00: 5c 72 5c 6e 22 2c 0a 09 09 68 74 74 70 5f 63 6f  \r\n",...http_co
6f10: 64 65 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65  de,...date_curre
6f20: 6e 74 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65  nt,...date_curre
6f30: 6e 74 2c 0a 09 09 22 69 6e 64 65 78 2e 68 74 6d  nt,..."index.htm
6f40: 6c 22 0a 09 29 3b 0a 0a 09 2f 2a 20 4c 6f 67 20  l"..);.../* Log 
6f50: 72 65 64 69 72 65 63 74 20 2a 2f 0a 09 6c 6f 67  redirect */..log
6f60: 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 72 65 64 69  ->reason = "redi
6f70: 72 65 63 74 22 3b 0a 09 6c 6f 67 2d 3e 68 74 74  rect";..log->htt
6f80: 70 5f 63 6f 64 65 20 3d 20 68 74 74 70 5f 63 6f  p_code = http_co
6f90: 64 65 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f  de;...filed_log_
6fa0: 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a  entry(log);.../*
6fb0: 20 43 6c 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f   Close connectio
6fc0: 6e 20 2a 2f 0a 09 66 69 6c 65 64 5f 73 6f 63 6b  n */..filed_sock
6fd0: 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28  ettimeout_close(
6fe0: 66 69 6c 65 6e 6f 28 66 70 29 29 3b 0a 0a 09 66  fileno(fp));...f
6ff0: 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 74  close(fp);...ret
7000: 75 72 6e 3b 0a 0a 09 2f 2a 20 43 75 72 72 65 6e  urn;.../* Curren
7010: 74 6c 79 20 75 6e 75 73 65 64 3a 20 70 61 74 68  tly unused: path
7020: 20 2a 2f 0a 09 70 61 74 68 20 3d 20 70 61 74 68   */..path = path
7030: 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 72 74 20  ;.}../* Convert 
7040: 61 6e 20 65 6e 75 6d 20 72 65 70 72 65 73 65 6e  an enum represen
7050: 74 69 6e 67 20 74 68 65 20 22 43 6f 6e 6e 65 63  ting the "Connec
7060: 74 69 6f 6e 22 20 68 65 61 64 65 72 20 76 61 6c  tion" header val
7070: 75 65 20 74 6f 20 61 20 73 74 72 69 6e 67 20 2a  ue to a string *
7080: 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
7090: 68 61 72 20 2a 66 69 6c 65 64 5f 63 6f 6e 6e 65  har *filed_conne
70a0: 63 74 69 6f 6e 5f 73 74 72 28 69 6e 74 20 63 6f  ction_str(int co
70b0: 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c 75 65 29 20  nnection_value) 
70c0: 7b 0a 09 73 77 69 74 63 68 20 28 63 6f 6e 6e 65  {..switch (conne
70d0: 63 74 69 6f 6e 5f 76 61 6c 75 65 29 20 7b 0a 09  ction_value) {..
70e0: 09 63 61 73 65 20 46 49 4c 45 44 5f 43 4f 4e 4e  .case FILED_CONN
70f0: 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 3a 0a 09 09  ECTION_CLOSE:...
7100: 09 72 65 74 75 72 6e 28 22 63 6c 6f 73 65 22 29  .return("close")
7110: 3b 0a 09 09 63 61 73 65 20 46 49 4c 45 44 5f 43  ;...case FILED_C
7120: 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41  ONNECTION_KEEP_A
7130: 4c 49 56 45 3a 0a 09 09 09 72 65 74 75 72 6e 28  LIVE:....return(
7140: 22 6b 65 65 70 2d 61 6c 69 76 65 22 29 3b 0a 09  "keep-alive");..
7150: 7d 0a 0a 09 72 65 74 75 72 6e 28 22 63 6c 6f 73  }...return("clos
7160: 65 22 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e 64 6c  e");.}../* Handl
7170: 65 20 61 20 73 69 6e 67 6c 65 20 72 65 71 75 65  e a single reque
7180: 73 74 20 66 72 6f 6d 20 61 20 63 6c 69 65 6e 74  st from a client
7190: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
71a0: 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69 65  iled_handle_clie
71b0: 6e 74 28 69 6e 74 20 66 64 2c 20 73 74 72 75 63  nt(int fd, struc
71c0: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71  t filed_http_req
71d0: 75 65 73 74 20 2a 72 65 71 75 65 73 74 2c 20 73  uest *request, s
71e0: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f  truct filed_log_
71f0: 65 6e 74 72 79 20 2a 6c 6f 67 2c 20 73 74 72 75  entry *log, stru
7200: 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73  ct filed_options
7210: 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74   *options) {..st
7220: 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69  ruct filed_filei
7230: 6e 66 6f 20 2a 66 69 6c 65 69 6e 66 6f 3b 0a 09  nfo *fileinfo;..
7240: 73 73 69 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65  ssize_t sendfile
7250: 5f 72 65 74 3b 0a 09 73 69 7a 65 5f 74 20 73 65  _ret;..size_t se
7260: 6e 64 66 69 6c 65 5f 73 69 7a 65 3b 0a 09 6f 66  ndfile_size;..of
7270: 66 5f 74 20 73 65 6e 64 66 69 6c 65 5f 6f 66 66  f_t sendfile_off
7280: 73 65 74 2c 20 73 65 6e 64 66 69 6c 65 5f 73 65  set, sendfile_se
7290: 6e 74 2c 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e  nt, sendfile_len
72a0: 3b 0a 09 63 68 61 72 20 2a 70 61 74 68 3b 0a 09  ;..char *path;..
72b0: 63 68 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65  char *date_curre
72c0: 6e 74 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74  nt, date_current
72d0: 5f 62 5b 36 34 5d 3b 0a 09 69 6e 74 20 68 74 74  _b[64];..int htt
72e0: 70 5f 63 6f 64 65 3b 0a 09 46 49 4c 45 20 2a 66  p_code;..FILE *f
72f0: 70 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69 6e  p;.../* Determin
7300: 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 2a  e current time *
7310: 2f 0a 09 64 61 74 65 5f 63 75 72 72 65 6e 74 20  /..date_current 
7320: 3d 20 66 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74  = filed_format_t
7330: 69 6d 65 28 64 61 74 65 5f 63 75 72 72 65 6e 74  ime(date_current
7340: 5f 62 2c 20 73 69 7a 65 6f 66 28 64 61 74 65 5f  _b, sizeof(date_
7350: 63 75 72 72 65 6e 74 5f 62 29 2c 20 74 69 6d 65  current_b), time
7360: 28 4e 55 4c 4c 29 29 3b 0a 0a 09 2f 2a 20 4f 70  (NULL));.../* Op
7370: 65 6e 20 73 6f 63 6b 65 74 20 61 73 20 41 4e 53  en socket as ANS
7380: 49 20 49 2f 4f 20 66 6f 72 20 65 61 73 65 20 6f  I I/O for ease o
7390: 66 20 75 73 65 20 2a 2f 0a 09 66 70 20 3d 20 66  f use */..fp = f
73a0: 64 6f 70 65 6e 28 66 64 2c 20 22 77 2b 62 22 29  dopen(fd, "w+b")
73b0: 3b 0a 09 69 66 20 28 66 70 20 3d 3d 20 4e 55 4c  ;..if (fp == NUL
73c0: 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 73 6f 63  L) {...filed_soc
73d0: 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65  kettimeout_close
73e0: 28 66 64 29 3b 0a 0a 09 09 63 6c 6f 73 65 28 66  (fd);....close(f
73f0: 64 29 3b 0a 0a 09 09 6c 6f 67 2d 3e 62 75 66 66  d);....log->buff
7400: 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09  er[0] = '\0';...
7410: 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d  log->http_code =
7420: 20 2d 31 3b 0a 09 09 6c 6f 67 2d 3e 72 65 61 73   -1;...log->reas
7430: 6f 6e 20 3d 20 22 66 64 6f 70 65 6e 5f 66 61 69  on = "fdopen_fai
7440: 6c 65 64 22 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c  led";....filed_l
7450: 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a  og_entry(log);..
7460: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43  ..return(FILED_C
7470: 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29  ONNECTION_CLOSE)
7480: 3b 0a 09 7d 0a 0a 09 72 65 71 75 65 73 74 20 3d  ;..}...request =
7490: 20 66 69 6c 65 64 5f 67 65 74 5f 68 74 74 70 5f   filed_get_http_
74a0: 72 65 71 75 65 73 74 28 66 70 2c 20 72 65 71 75  request(fp, requ
74b0: 65 73 74 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a 0a  est, options);..
74c0: 09 69 66 20 28 72 65 71 75 65 73 74 20 3d 3d 20  .if (request == 
74d0: 4e 55 4c 4c 29 20 7b 0a 09 09 6c 6f 67 2d 3e 62  NULL) {...log->b
74e0: 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b  uffer[0] = '\0';
74f0: 0a 0a 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f  ....filed_error_
7500: 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f 63 75  page(fp, date_cu
7510: 72 72 65 6e 74 2c 20 35 30 30 2c 20 46 49 4c 45  rrent, 500, FILE
7520: 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44  D_REQUEST_METHOD
7530: 5f 47 45 54 2c 20 22 66 6f 72 6d 61 74 22 2c 20  _GET, "format", 
7540: 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28  log);....return(
7550: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e  FILED_CONNECTION
7560: 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66 69  _CLOSE);..}...fi
7570: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
7580: 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 73 74 61  t_processing_sta
7590: 72 74 28 66 64 29 3b 0a 0a 09 70 61 74 68 20 3d  rt(fd);...path =
75a0: 20 72 65 71 75 65 73 74 2d 3e 70 61 74 68 3b 0a   request->path;.
75b0: 09 73 74 72 63 70 79 28 6c 6f 67 2d 3e 62 75 66  .strcpy(log->buf
75c0: 66 65 72 2c 20 70 61 74 68 29 3b 0a 09 6c 6f 67  fer, path);..log
75d0: 2d 3e 6d 65 74 68 6f 64 20 3d 20 72 65 71 75 65  ->method = reque
75e0: 73 74 2d 3e 6d 65 74 68 6f 64 3b 0a 0a 09 2f 2a  st->method;.../*
75f0: 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65   If the requeste
7600: 64 20 70 61 74 68 20 69 73 20 61 20 64 69 72 65  d path is a dire
7610: 63 74 6f 72 79 2c 20 72 65 64 69 72 65 63 74 20  ctory, redirect 
7620: 74 6f 20 69 6e 64 65 78 20 70 61 67 65 20 2a 2f  to index page */
7630: 0a 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 74  ..if (request->t
7640: 79 70 65 20 3d 3d 20 46 49 4c 45 44 5f 52 45 51  ype == FILED_REQ
7650: 55 45 53 54 5f 54 59 50 45 5f 44 49 52 45 43 54  UEST_TYPE_DIRECT
7660: 4f 52 59 29 20 7b 0a 09 09 66 69 6c 65 64 5f 72  ORY) {...filed_r
7670: 65 64 69 72 65 63 74 5f 69 6e 64 65 78 28 66 70  edirect_index(fp
7680: 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20  , date_current, 
7690: 70 61 74 68 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72  path, log);....r
76a0: 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e  eturn(FILED_CONN
76b0: 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09  ECTION_CLOSE);..
76c0: 7d 0a 0a 09 66 69 6c 65 69 6e 66 6f 20 3d 20 66  }...fileinfo = f
76d0: 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 28 70  iled_open_file(p
76e0: 61 74 68 2c 20 26 72 65 71 75 65 73 74 2d 3e 66  ath, &request->f
76f0: 69 6c 65 69 6e 66 6f 2c 20 6f 70 74 69 6f 6e 73  ileinfo, options
7700: 29 3b 0a 09 69 66 20 28 66 69 6c 65 69 6e 66 6f  );..if (fileinfo
7710: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69   == NULL) {...fi
7720: 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 66  led_error_page(f
7730: 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c  p, date_current,
7740: 20 34 30 34 2c 20 72 65 71 75 65 73 74 2d 3e 6d   404, request->m
7750: 65 74 68 6f 64 2c 20 22 6f 70 65 6e 5f 66 61 69  ethod, "open_fai
7760: 6c 65 64 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72  led", log);....r
7770: 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e  eturn(FILED_CONN
7780: 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09  ECTION_CLOSE);..
7790: 7d 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74 2d  }...if (request-
77a0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 70  >headers.range.p
77b0: 72 65 73 65 6e 74 29 20 7b 0a 09 09 69 66 20 28  resent) {...if (
77c0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
77d0: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 21 3d  .range.offset !=
77e0: 20 30 20 7c 7c 20 72 65 71 75 65 73 74 2d 3e 68   0 || request->h
77f0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e  eaders.range.len
7800: 67 74 68 20 3e 3d 20 30 29 20 7b 0a 09 09 09 69  gth >= 0) {....i
7810: 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64  f (request->head
7820: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74  ers.range.offset
7830: 20 3e 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65   >= fileinfo->le
7840: 6e 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f 65  n) {.....filed_e
7850: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61  rror_page(fp, da
7860: 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 31 36 2c  te_current, 416,
7870: 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64   request->method
7880: 2c 20 22 72 61 6e 67 65 5f 69 6e 76 61 6c 69 64  , "range_invalid
7890: 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 09 09 63 6c  ", log);......cl
78a0: 6f 73 65 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64  ose(fileinfo->fd
78b0: 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 46  );......return(F
78c0: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f  ILED_CONNECTION_
78d0: 43 4c 4f 53 45 29 3b 0a 09 09 09 7d 0a 0a 09 09  CLOSE);....}....
78e0: 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65  .if (request->he
78f0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
7900: 74 68 20 3d 3d 20 28 28 6f 66 66 5f 74 29 20 2d  th == ((off_t) -
7910: 31 29 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f  1)) {.....filed_
7920: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43  log_msg_debug("C
7930: 6f 6d 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 20  omputing length 
7940: 74 6f 20 66 69 74 20 69 6e 20 62 6f 75 6e 64 73  to fit in bounds
7950: 3a 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20  : fileinfo->len 
7960: 3d 20 25 6c 6c 75 2c 20 72 65 71 75 65 73 74 2d  = %llu, request-
7970: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f  >headers.range.o
7980: 66 66 73 65 74 20 3d 20 25 6c 6c 75 22 2c 0a 09  ffset = %llu",..
7990: 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f  ....(unsigned lo
79a0: 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 6e 66  ng long) fileinf
79b0: 6f 2d 3e 6c 65 6e 2c 0a 09 09 09 09 09 28 75 6e  o->len,......(un
79c0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
79d0: 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65  ) request->heade
79e0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 0a  rs.range.offset.
79f0: 09 09 09 09 29 3b 0a 0a 09 09 09 09 72 65 71 75  ....);......requ
7a00: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
7a10: 67 65 2e 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65  ge.length = file
7a20: 69 6e 66 6f 2d 3e 6c 65 6e 20 2d 20 72 65 71 75  info->len - requ
7a30: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
7a40: 67 65 2e 6f 66 66 73 65 74 3b 0a 09 09 09 7d 0a  ge.offset;....}.
7a50: 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  ....filed_log_ms
7a60: 67 5f 64 65 62 75 67 28 22 50 61 72 74 69 61 6c  g_debug("Partial
7a70: 20 72 65 71 75 65 73 74 2c 20 73 74 61 72 74 69   request, starti
7a80: 6e 67 20 61 74 3a 20 25 6c 6c 75 20 61 6e 64 20  ng at: %llu and 
7a90: 72 75 6e 6e 69 6e 67 20 66 6f 72 20 25 6c 6c 69  running for %lli
7aa0: 20 62 79 74 65 73 22 2c 0a 09 09 09 09 28 75 6e   bytes",.....(un
7ab0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
7ac0: 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65  ) request->heade
7ad0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c  rs.range.offset,
7ae0: 0a 09 09 09 09 28 6c 6f 6e 67 20 6c 6f 6e 67 29  .....(long long)
7af0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72   request->header
7b00: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 0a 09  s.range.length..
7b10: 09 09 29 3b 0a 0a 09 09 7d 0a 0a 09 09 68 74 74  ..);....}....htt
7b20: 70 5f 63 6f 64 65 20 3d 20 32 30 36 3b 0a 09 7d  p_code = 206;..}
7b30: 20 65 6c 73 65 20 7b 0a 09 09 68 74 74 70 5f 63   else {...http_c
7b40: 6f 64 65 20 3d 20 32 30 30 3b 0a 0a 09 09 2f 2a  ode = 200;..../*
7b50: 20 43 6f 6d 70 75 74 65 20 66 61 6b 65 20 72 61   Compute fake ra
7b60: 6e 67 65 20 70 61 72 61 6d 65 74 65 72 73 20 74  nge parameters t
7b70: 68 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65  hat includes the
7b80: 20 65 6e 74 69 72 65 20 66 69 6c 65 20 2a 2f 0a   entire file */.
7b90: 09 09 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65  ..request->heade
7ba0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20  rs.range.offset 
7bb0: 3d 20 30 3b 0a 09 09 72 65 71 75 65 73 74 2d 3e  = 0;...request->
7bc0: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65  headers.range.le
7bd0: 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d  ngth = fileinfo-
7be0: 3e 6c 65 6e 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e  >len;..}...fprin
7bf0: 74 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e 31  tf(fp, "HTTP/1.1
7c00: 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20   %i OK\r\nDate: 
7c10: 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69  %s\r\nServer: fi
7c20: 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69  led\r\nLast-Modi
7c30: 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74  fied: %s\r\nCont
7c40: 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75  ent-Length: %llu
7c50: 5c 72 5c 6e 41 63 63 65 70 74 2d 52 61 6e 67 65  \r\nAccept-Range
7c60: 73 3a 20 62 79 74 65 73 5c 72 5c 6e 43 6f 6e 74  s: bytes\r\nCont
7c70: 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e  ent-Type: %s\r\n
7c80: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 25 73 5c 72  Connection: %s\r
7c90: 5c 6e 45 54 61 67 3a 20 5c 22 25 73 5c 22 5c 72  \nETag: \"%s\"\r
7ca0: 5c 6e 22 2c 0a 09 09 68 74 74 70 5f 63 6f 64 65  \n",...http_code
7cb0: 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74  ,...date_current
7cc0: 2c 0a 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 6c 61  ,...fileinfo->la
7cd0: 73 74 6d 6f 64 2c 0a 09 09 28 75 6e 73 69 67 6e  stmod,...(unsign
7ce0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65  ed long long) re
7cf0: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
7d00: 61 6e 67 65 2e 6c 65 6e 67 74 68 2c 0a 09 09 66  ange.length,...f
7d10: 69 6c 65 69 6e 66 6f 2d 3e 74 79 70 65 2c 0a 09  ileinfo->type,..
7d20: 09 66 69 6c 65 64 5f 63 6f 6e 6e 65 63 74 69 6f  .filed_connectio
7d30: 6e 5f 73 74 72 28 72 65 71 75 65 73 74 2d 3e 68  n_str(request->h
7d40: 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f  eaders.connectio
7d50: 6e 29 2c 0a 09 09 66 69 6c 65 69 6e 66 6f 2d 3e  n),...fileinfo->
7d60: 65 74 61 67 0a 09 29 3b 0a 0a 09 69 66 20 28 68  etag..);...if (h
7d70: 74 74 70 5f 63 6f 64 65 20 3d 3d 20 32 30 36 29  ttp_code == 206)
7d80: 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 66 70 2c   {...fprintf(fp,
7d90: 20 22 43 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 3a   "Content-Range:
7da0: 20 62 79 74 65 73 20 25 6c 6c 75 2d 25 6c 6c 75   bytes %llu-%llu
7db0: 2f 25 6c 6c 75 5c 72 5c 6e 22 2c 0a 09 09 09 28  /%llu\r\n",....(
7dc0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
7dd0: 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61  ng) request->hea
7de0: 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65  ders.range.offse
7df0: 74 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20  t,....(unsigned 
7e00: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 28 72 65 71 75  long long) (requ
7e10: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
7e20: 67 65 2e 6f 66 66 73 65 74 20 2b 20 72 65 71 75  ge.offset + requ
7e30: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
7e40: 67 65 2e 6c 65 6e 67 74 68 20 2d 20 31 29 2c 0a  ge.length - 1),.
7e50: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
7e60: 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f  g long) fileinfo
7e70: 2d 3e 6c 65 6e 0a 09 09 29 3b 0a 09 7d 0a 09 66  ->len...);..}..f
7e80: 70 72 69 6e 74 66 28 66 70 2c 20 22 5c 72 5c 6e  printf(fp, "\r\n
7e90: 22 29 3b 0a 09 66 66 6c 75 73 68 28 66 70 29 3b  ");..fflush(fp);
7ea0: 0a 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64  ...log->http_cod
7eb0: 65 20 3d 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09  e = http_code;..
7ec0: 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 4f  log->reason = "O
7ed0: 4b 22 3b 0a 09 6c 6f 67 2d 3e 73 74 61 72 74 74  K";..log->startt
7ee0: 69 6d 65 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29  ime = time(NULL)
7ef0: 3b 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6f 66 66 73  ;..log->req_offs
7f00: 65 74 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65  et = request->he
7f10: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73  aders.range.offs
7f20: 65 74 3b 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6c 65  et;..log->req_le
7f30: 6e 67 74 68 20 3d 20 72 65 71 75 65 73 74 2d 3e  ngth = request->
7f40: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65  headers.range.le
7f50: 6e 67 74 68 3b 0a 09 6c 6f 67 2d 3e 66 69 6c 65  ngth;..log->file
7f60: 5f 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e  _length = filein
7f70: 66 6f 2d 3e 6c 65 6e 3b 0a 0a 23 69 66 64 65 66  fo->len;..#ifdef
7f80: 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f   FILED_NONBLOCK_
7f90: 48 54 54 50 0a 09 69 6e 74 20 73 6f 63 6b 65 74  HTTP..int socket
7fa0: 5f 66 6c 61 67 73 3b 0a 09 66 64 5f 73 65 74 20  _flags;..fd_set 
7fb0: 72 66 64 2c 20 77 66 64 3b 0a 09 63 68 61 72 20  rfd, wfd;..char 
7fc0: 73 69 6e 6b 62 75 66 5b 38 31 39 32 5d 3b 0a 09  sinkbuf[8192];..
7fd0: 73 73 69 7a 65 5f 74 20 72 65 61 64 5f 72 65 74  ssize_t read_ret
7fe0: 3b 0a 0a 09 46 44 5f 5a 45 52 4f 28 26 72 66 64  ;...FD_ZERO(&rfd
7ff0: 29 3b 0a 09 46 44 5f 5a 45 52 4f 28 26 77 66 64  );..FD_ZERO(&wfd
8000: 29 3b 0a 09 46 44 5f 53 45 54 28 66 64 2c 20 26  );..FD_SET(fd, &
8010: 72 66 64 29 3b 0a 09 46 44 5f 53 45 54 28 66 64  rfd);..FD_SET(fd
8020: 2c 20 26 77 66 64 29 3b 0a 0a 09 73 6f 63 6b 65  , &wfd);...socke
8030: 74 5f 66 6c 61 67 73 20 3d 20 66 63 6e 74 6c 28  t_flags = fcntl(
8040: 66 64 2c 20 46 5f 47 45 54 46 4c 29 3b 0a 09 66  fd, F_GETFL);..f
8050: 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 4c  cntl(fd, F_SETFL
8060: 2c 20 73 6f 63 6b 65 74 5f 66 6c 61 67 73 20 7c  , socket_flags |
8070: 20 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 3b 0a 23 65   O_NONBLOCK);.#e
8080: 6e 64 69 66 0a 09 73 65 6e 64 66 69 6c 65 5f 6f  ndif..sendfile_o
8090: 66 66 73 65 74 20 3d 20 72 65 71 75 65 73 74 2d  ffset = request-
80a0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f  >headers.range.o
80b0: 66 66 73 65 74 3b 0a 09 73 65 6e 64 66 69 6c 65  ffset;..sendfile
80c0: 5f 6c 65 6e 20 3d 20 72 65 71 75 65 73 74 2d 3e  _len = request->
80d0: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65  headers.range.le
80e0: 6e 67 74 68 3b 0a 09 73 65 6e 64 66 69 6c 65 5f  ngth;..sendfile_
80f0: 73 65 6e 74 20 3d 20 30 3b 0a 09 77 68 69 6c 65  sent = 0;..while
8100: 20 28 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f   (request->metho
8110: 64 20 3d 3d 20 46 49 4c 45 44 5f 52 45 51 55 45  d == FILED_REQUE
8120: 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 29 20 7b  ST_METHOD_GET) {
8130: 0a 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f  ...if (sendfile_
8140: 6c 65 6e 20 3e 20 46 49 4c 45 44 5f 53 45 4e 44  len > FILED_SEND
8150: 46 49 4c 45 5f 4d 41 58 29 20 7b 0a 09 09 09 73  FILE_MAX) {....s
8160: 65 6e 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 46  endfile_size = F
8170: 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41  ILED_SENDFILE_MA
8180: 58 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09  X;...} else {...
8190: 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 20 3d  .sendfile_size =
81a0: 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 3b 0a 09   sendfile_len;..
81b0: 09 7d 0a 0a 09 09 73 65 6e 64 66 69 6c 65 5f 72  .}....sendfile_r
81c0: 65 74 20 3d 20 73 65 6e 64 66 69 6c 65 28 66 64  et = sendfile(fd
81d0: 2c 20 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 2c 20  , fileinfo->fd, 
81e0: 26 73 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74  &sendfile_offset
81f0: 2c 20 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 29  , sendfile_size)
8200: 3b 0a 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65  ;...if (sendfile
8210: 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 23 69 66  _ret <= 0) {.#if
8220: 64 65 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f  def FILED_NONBLO
8230: 43 4b 5f 48 54 54 50 0a 09 09 09 69 66 20 28 65  CK_HTTP....if (e
8240: 72 72 6e 6f 20 3d 3d 20 45 41 47 41 49 4e 29 20  rrno == EAGAIN) 
8250: 7b 0a 09 09 09 09 73 65 6e 64 66 69 6c 65 5f 72  {.....sendfile_r
8260: 65 74 20 3d 20 30 3b 0a 0a 09 09 09 09 77 68 69  et = 0;......whi
8270: 6c 65 20 28 31 29 20 7b 0a 09 09 09 09 09 73 65  le (1) {......se
8280: 6c 65 63 74 28 66 64 20 2b 20 31 2c 20 26 72 66  lect(fd + 1, &rf
8290: 64 2c 20 26 77 66 64 2c 20 4e 55 4c 4c 2c 20 4e  d, &wfd, NULL, N
82a0: 55 4c 4c 29 3b 0a 09 09 09 09 09 69 66 20 28 46  ULL);......if (F
82b0: 44 5f 49 53 53 45 54 28 66 64 2c 20 26 72 66 64  D_ISSET(fd, &rfd
82c0: 29 29 20 7b 0a 09 09 09 09 09 09 72 65 61 64 5f  )) {.......read_
82d0: 72 65 74 20 3d 20 72 65 61 64 28 66 64 2c 20 73  ret = read(fd, s
82e0: 69 6e 6b 62 75 66 2c 20 73 69 7a 65 6f 66 28 73  inkbuf, sizeof(s
82f0: 69 6e 6b 62 75 66 29 29 3b 0a 0a 09 09 09 09 09  inkbuf));.......
8300: 09 69 66 20 28 72 65 61 64 5f 72 65 74 20 3c 3d  .if (read_ret <=
8310: 20 30 29 20 7b 0a 09 09 09 09 09 09 09 62 72 65   0) {........bre
8320: 61 6b 3b 0a 09 09 09 09 09 09 7d 0a 09 09 09 09  ak;.......}.....
8330: 09 7d 0a 0a 09 09 09 09 09 69 66 20 28 46 44 5f  .}.......if (FD_
8340: 49 53 53 45 54 28 66 64 2c 20 26 77 66 64 29 29  ISSET(fd, &wfd))
8350: 20 7b 0a 09 09 09 09 09 09 72 65 61 64 5f 72 65   {.......read_re
8360: 74 20 3d 20 31 3b 0a 0a 09 09 09 09 09 09 62 72  t = 1;........br
8370: 65 61 6b 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09  eak;......}.....
8380: 7d 0a 0a 09 09 09 09 69 66 20 28 72 65 61 64 5f  }......if (read_
8390: 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09  ret <= 0) {.....
83a0: 09 62 72 65 61 6b 3b 0a 09 09 09 09 7d 0a 09 09  .break;.....}...
83b0: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 62 72  .} else {.....br
83c0: 65 61 6b 3b 0a 09 09 09 7d 0a 23 65 6c 73 65 0a  eak;....}.#else.
83d0: 09 09 09 62 72 65 61 6b 3b 0a 23 65 6e 64 69 66  ...break;.#endif
83e0: 0a 09 09 7d 0a 0a 09 09 73 65 6e 64 66 69 6c 65  ...}....sendfile
83f0: 5f 6c 65 6e 20 2d 3d 20 73 65 6e 64 66 69 6c 65  _len -= sendfile
8400: 5f 72 65 74 3b 0a 09 09 73 65 6e 64 66 69 6c 65  _ret;...sendfile
8410: 5f 73 65 6e 74 20 2b 3d 20 73 65 6e 64 66 69 6c  _sent += sendfil
8420: 65 5f 72 65 74 3b 0a 09 09 69 66 20 28 73 65 6e  e_ret;...if (sen
8430: 64 66 69 6c 65 5f 6c 65 6e 20 3d 3d 20 30 29 20  dfile_len == 0) 
8440: 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a  {....break;...}.
8450: 09 7d 0a 0a 09 6c 6f 67 2d 3e 65 6e 64 74 69 6d  .}...log->endtim
8460: 65 20 3d 20 28 74 69 6d 65 5f 74 29 20 2d 31 3b  e = (time_t) -1;
8470: 0a 09 6c 6f 67 2d 3e 73 65 6e 74 5f 6c 65 6e 67  ..log->sent_leng
8480: 74 68 20 3d 20 73 65 6e 64 66 69 6c 65 5f 73 65  th = sendfile_se
8490: 6e 74 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f  nt;...filed_log_
84a0: 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 63 6c  entry(log);...cl
84b0: 6f 73 65 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64  ose(fileinfo->fd
84c0: 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74  );...if (request
84d0: 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63  ->headers.connec
84e0: 74 69 6f 6e 20 21 3d 20 46 49 4c 45 44 5f 43 4f  tion != FILED_CO
84f0: 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c  NNECTION_KEEP_AL
8500: 49 56 45 29 20 7b 0a 09 09 66 69 6c 65 64 5f 73  IVE) {...filed_s
8510: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f  ockettimeout_clo
8520: 73 65 28 66 64 29 3b 0a 0a 09 09 66 63 6c 6f 73  se(fd);....fclos
8530: 65 28 66 70 29 3b 0a 0a 09 09 72 65 74 75 72 6e  e(fp);....return
8540: 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f  (FILED_CONNECTIO
8550: 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66  N_CLOSE);..}...f
8560: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
8570: 75 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 65 6e  ut_processing_en
8580: 64 28 66 64 29 3b 0a 0a 09 72 65 74 75 72 6e 28  d(fd);...return(
8590: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e  FILED_CONNECTION
85a0: 5f 4b 45 45 50 5f 41 4c 49 56 45 29 3b 0a 7d 0a  _KEEP_ALIVE);.}.
85b0: 0a 2f 2a 20 48 61 6e 64 6c 65 20 69 6e 63 6f 6d  ./* Handle incom
85c0: 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ing connections 
85d0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
85e0: 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72  filed_worker_thr
85f0: 65 61 64 28 76 6f 69 64 20 2a 61 72 67 5f 76 29  ead(void *arg_v)
8600: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64   {..struct filed
8610: 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61  _worker_thread_a
8620: 72 67 73 20 2a 61 72 67 3b 0a 09 73 74 72 75 63  rgs *arg;..struc
8630: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71  t filed_http_req
8640: 75 65 73 74 20 72 65 71 75 65 73 74 3b 0a 09 73  uest request;..s
8650: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f  truct filed_log_
8660: 65 6e 74 72 79 20 2a 6c 6f 67 2c 20 6c 6f 63 61  entry *log, loca
8670: 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 3b 0a 09 73 74  l_dummy_log;..st
8680: 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f  ruct filed_optio
8690: 6e 73 20 2a 6f 70 74 69 6f 6e 73 3b 0a 09 73 74  ns *options;..st
86a0: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e  ruct sockaddr_in
86b0: 36 20 61 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e  6 addr;..socklen
86c0: 5f 74 20 61 64 64 72 6c 65 6e 3b 0a 09 69 6e 74  _t addrlen;..int
86d0: 20 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d   failure_count =
86e0: 20 30 2c 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f   0, max_failure_
86f0: 63 6f 75 6e 74 20 3d 20 46 49 4c 45 44 5f 4d 41  count = FILED_MA
8700: 58 5f 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 3b  X_FAILURE_COUNT;
8710: 0a 09 69 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e  ..int connection
8720: 5f 73 74 61 74 65 20 3d 20 46 49 4c 45 44 5f 43  _state = FILED_C
8730: 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 3b  ONNECTION_CLOSE;
8740: 0a 09 69 6e 74 20 6d 61 73 74 65 72 5f 66 64 2c  ..int master_fd,
8750: 20 66 64 20 3d 20 2d 31 3b 0a 0a 09 2f 2a 20 52   fd = -1;.../* R
8760: 65 61 64 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f  ead arguments */
8770: 0a 09 61 72 67 20 3d 20 61 72 67 5f 76 3b 0a 0a  ..arg = arg_v;..
8780: 09 6d 61 73 74 65 72 5f 66 64 20 3d 20 61 72 67  .master_fd = arg
8790: 2d 3e 66 64 3b 0a 09 6f 70 74 69 6f 6e 73 20 3d  ->fd;..options =
87a0: 20 26 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 3b 0a   &arg->options;.
87b0: 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09  ..while (1) {...
87c0: 2f 2a 20 46 61 69 6c 75 72 65 20 6c 6f 6f 70 20  /* Failure loop 
87d0: 70 72 65 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09 09  prevention */...
87e0: 69 66 20 28 66 61 69 6c 75 72 65 5f 63 6f 75 6e  if (failure_coun
87f0: 74 20 3e 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f  t > max_failure_
8800: 63 6f 75 6e 74 29 20 7b 0a 09 09 09 62 72 65 61  count) {....brea
8810: 6b 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 41 6c 6c  k;...}..../* All
8820: 6f 63 61 74 65 20 61 20 6e 65 77 20 6c 6f 67 20  ocate a new log 
8830: 62 75 66 66 65 72 20 2a 2f 0a 09 09 6c 6f 67 20  buffer */...log 
8840: 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28  = filed_log_new(
8850: 31 29 3b 0a 09 09 69 66 20 28 6c 6f 67 20 3d 3d  1);...if (log ==
8860: 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 66 69 6c 65   NULL) {....file
8870: 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 4c 4c 4f 43  d_log_msg("ALLOC
8880: 41 54 45 5f 4c 4f 47 5f 4d 53 47 5f 46 41 49 4c  ATE_LOG_MSG_FAIL
8890: 45 44 22 29 3b 0a 0a 09 09 09 62 72 65 61 6b 3b  ED");.....break;
88a0: 0a 09 09 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 79 70  ...}....log->typ
88b0: 65 20 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59  e = FILED_LOG_TY
88c0: 50 45 5f 54 52 41 4e 53 46 45 52 3b 0a 0a 09 09  PE_TRANSFER;....
88d0: 2f 2a 20 49 66 20 77 65 20 63 6c 6f 73 65 64 20  /* If we closed 
88e0: 74 68 65 20 6f 6c 64 20 63 6f 6e 6e 65 63 74 69  the old connecti
88f0: 6f 6e 2c 20 61 63 63 65 70 74 20 61 20 6e 65 77  on, accept a new
8900: 20 6f 6e 65 20 2a 2f 0a 09 09 69 66 20 28 63 6f   one */...if (co
8910: 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65 20 3d  nnection_state =
8920: 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49  = FILED_CONNECTI
8930: 4f 4e 5f 43 4c 4f 53 45 29 20 7b 0a 09 09 09 2f  ON_CLOSE) {..../
8940: 2a 20 41 63 63 65 70 74 20 61 20 6e 65 77 20 63  * Accept a new c
8950: 6c 69 65 6e 74 20 2a 2f 0a 09 09 09 61 64 64 72  lient */....addr
8960: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64  len = sizeof(add
8970: 72 29 3b 0a 0a 09 09 09 66 64 20 3d 20 61 63 63  r);.....fd = acc
8980: 65 70 74 28 6d 61 73 74 65 72 5f 66 64 2c 20 28  ept(master_fd, (
8990: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20  struct sockaddr 
89a0: 2a 29 20 26 61 64 64 72 2c 20 26 61 64 64 72 6c  *) &addr, &addrl
89b0: 65 6e 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 0a 09  en);...}..../*..
89c0: 09 20 2a 20 49 66 20 77 65 20 66 61 69 6c 2c 20  . * If we fail, 
89d0: 6d 61 6b 65 20 61 20 6e 6f 74 65 20 6f 66 20 69  make a note of i
89e0: 74 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 67 6f  t so we don't go
89f0: 20 69 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f 66 0a   into a loop of.
8a00: 09 09 20 2a 20 61 63 63 65 70 74 28 29 20 66 61  .. * accept() fa
8a10: 69 6c 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 69 66  iling... */...if
8a20: 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 09 2f   (fd < 0) {..../
8a30: 2a 20 4c 6f 67 20 74 68 65 20 6e 65 77 20 63 6f  * Log the new co
8a40: 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 09 66  nnection */....f
8a50: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 43  iled_log_msg("AC
8a60: 43 45 50 54 5f 46 41 49 4c 45 44 22 29 3b 0a 0a  CEPT_FAILED");..
8a70: 09 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74  ...failure_count
8a80: 2b 2b 3b 0a 0a 09 09 09 66 69 6c 65 64 5f 6c 6f  ++;.....filed_lo
8a90: 67 5f 66 72 65 65 28 6c 6f 67 29 3b 0a 0a 09 09  g_free(log);....
8aa0: 09 63 6f 6e 74 69 6e 75 65 3b 0a 09 09 7d 0a 0a  .continue;...}..
8ab0: 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  ..filed_socketti
8ac0: 6d 65 6f 75 74 5f 61 63 63 65 70 74 28 66 64 29  meout_accept(fd)
8ad0: 3b 0a 0a 09 09 2f 2a 20 46 69 6c 6c 20 69 6e 20  ;..../* Fill in 
8ae0: 6c 6f 67 20 73 74 72 75 63 74 75 72 65 20 2a 2f  log structure */
8af0: 0a 09 09 69 66 20 28 66 69 6c 65 64 5f 6c 6f 67  ...if (filed_log
8b00: 5f 69 70 28 28 73 74 72 75 63 74 20 73 6f 63 6b  _ip((struct sock
8b10: 61 64 64 72 20 2a 29 20 26 61 64 64 72 2c 20 6c  addr *) &addr, l
8b20: 6f 67 2d 3e 69 70 2c 20 73 69 7a 65 6f 66 28 6c  og->ip, sizeof(l
8b30: 6f 67 2d 3e 69 70 29 29 20 3d 3d 20 4e 55 4c 4c  og->ip)) == NULL
8b40: 29 20 7b 0a 09 09 09 6c 6f 67 2d 3e 69 70 5b 30  ) {....log->ip[0
8b50: 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 09 6c 6f 67  ] = '\0';....log
8b60: 2d 3e 70 6f 72 74 20 3d 20 30 3b 0a 09 09 7d 20  ->port = 0;...} 
8b70: 65 6c 73 65 20 7b 0a 09 09 09 6c 6f 67 2d 3e 70  else {....log->p
8b80: 6f 72 74 20 3d 20 61 64 64 72 2e 73 69 6e 36 5f  ort = addr.sin6_
8b90: 70 6f 72 74 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20  port;...}..../* 
8ba0: 52 65 73 65 74 20 66 61 69 6c 75 72 65 20 63 6f  Reset failure co
8bb0: 75 6e 74 2a 2f 0a 09 09 66 61 69 6c 75 72 65 5f  unt*/...failure_
8bc0: 63 6f 75 6e 74 20 3d 20 30 3b 0a 0a 09 09 2f 2a  count = 0;..../*
8bd0: 20 48 61 6e 64 6c 65 20 73 6f 63 6b 65 74 20 2a   Handle socket *
8be0: 2f 0a 09 09 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73  /...connection_s
8bf0: 74 61 74 65 20 3d 20 66 69 6c 65 64 5f 68 61 6e  tate = filed_han
8c00: 64 6c 65 5f 63 6c 69 65 6e 74 28 66 64 2c 20 26  dle_client(fd, &
8c10: 72 65 71 75 65 73 74 2c 20 6c 6f 67 2c 20 6f 70  request, log, op
8c20: 74 69 6f 6e 73 29 3b 0a 09 7d 0a 0a 09 2f 2a 20  tions);..}.../* 
8c30: 52 65 70 6f 72 74 20 65 72 72 6f 72 20 2a 2f 0a  Report error */.
8c40: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22  .filed_log_msg("
8c50: 54 48 52 45 41 44 5f 44 49 45 44 20 41 42 4e 4f  THREAD_DIED ABNO
8c60: 52 4d 41 4c 22 29 3b 0a 0a 09 72 65 74 75 72 6e  RMAL");...return
8c70: 28 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 6c 6f 63  (NULL);.../* loc
8c80: 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 20 69 73 20  al_dummy_log is 
8c90: 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 46 49 4c  only used if FIL
8ca0: 45 44 5f 44 4f 4e 54 5f 4c 4f 47 20 69 73 20 65  ED_DONT_LOG is e
8cb0: 6e 61 62 6c 65 64 2c 20 6f 74 68 65 72 77 69 73  nabled, otherwis
8cc0: 65 20 69 74 27 73 20 6e 6f 74 20 75 73 65 64 2c  e it's not used,
8cd0: 20 62 75 74 20 74 68 65 20 63 6f 6d 70 69 6c 65   but the compile
8ce0: 72 20 68 61 74 65 73 20 74 68 61 74 20 69 64 65  r hates that ide
8cf0: 61 2e 20 2a 2f 0a 09 6c 6f 63 61 6c 5f 64 75 6d  a. */..local_dum
8d00: 6d 79 5f 6c 6f 67 2e 74 79 70 65 20 3d 20 30 3b  my_log.type = 0;
8d10: 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f  ..local_dummy_lo
8d20: 67 2e 74 79 70 65 20 3d 20 6c 6f 63 61 6c 5f 64  g.type = local_d
8d30: 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 3b 0a 7d  ummy_log.type;.}
8d40: 0a 0a 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 6b  ../* Create work
8d50: 65 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 74  er threads */.st
8d60: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 77  atic int filed_w
8d70: 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e  orker_threads_in
8d80: 69 74 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 74  it(int fd, int t
8d90: 68 72 65 61 64 5f 63 6f 75 6e 74 2c 20 73 74 72  hread_count, str
8da0: 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e  uct filed_option
8db0: 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73  s *options) {..s
8dc0: 74 72 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b  truct filed_work
8dd0: 65 72 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a  er_thread_args *
8de0: 61 72 67 3b 0a 09 70 74 68 72 65 61 64 5f 74 20  arg;..pthread_t 
8df0: 74 68 72 65 61 64 69 64 3b 0a 09 69 6e 74 20 70  threadid;..int p
8e00: 74 68 72 65 61 64 5f 72 65 74 3b 0a 09 69 6e 74  thread_ret;..int
8e10: 20 69 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30   i;...for (i = 0
8e20: 3b 20 69 20 3c 20 74 68 72 65 61 64 5f 63 6f 75  ; i < thread_cou
8e30: 6e 74 3b 20 69 2b 2b 29 20 7b 0a 09 09 61 72 67  nt; i++) {...arg
8e40: 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66   = malloc(sizeof
8e50: 28 2a 61 72 67 29 29 3b 0a 0a 09 09 61 72 67 2d  (*arg));....arg-
8e60: 3e 66 64 20 3d 20 66 64 3b 0a 09 09 6d 65 6d 63  >fd = fd;...memc
8e70: 70 79 28 26 61 72 67 2d 3e 6f 70 74 69 6f 6e 73  py(&arg->options
8e80: 2c 20 6f 70 74 69 6f 6e 73 2c 20 73 69 7a 65 6f  , options, sizeo
8e90: 66 28 2a 6f 70 74 69 6f 6e 73 29 29 3b 0a 0a 09  f(*options));...
8ea0: 09 70 74 68 72 65 61 64 5f 72 65 74 20 3d 20 70  .pthread_ret = p
8eb0: 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74  thread_create(&t
8ec0: 68 72 65 61 64 69 64 2c 20 4e 55 4c 4c 2c 20 66  hreadid, NULL, f
8ed0: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65  iled_worker_thre
8ee0: 61 64 2c 20 61 72 67 29 3b 0a 09 09 69 66 20 28  ad, arg);...if (
8ef0: 70 74 68 72 65 61 64 5f 72 65 74 20 21 3d 20 30  pthread_ret != 0
8f00: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 31  ) {....return(-1
8f10: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75  );...}..}...retu
8f20: 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 73  rn(0);.}../* Dis
8f30: 70 6c 61 79 20 68 65 6c 70 20 2a 2f 0a 73 74 61  play help */.sta
8f40: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 70  tic void filed_p
8f50: 72 69 6e 74 5f 68 65 6c 70 28 46 49 4c 45 20 2a  rint_help(FILE *
8f60: 6f 75 74 70 75 74 2c 20 69 6e 74 20 6c 6f 6e 67  output, int long
8f70: 5f 68 65 6c 70 2c 20 63 6f 6e 73 74 20 63 68 61  _help, const cha
8f80: 72 20 2a 65 78 74 72 61 29 20 7b 0a 09 69 66 20  r *extra) {..if 
8f90: 28 65 78 74 72 61 29 20 7b 0a 09 09 66 70 72 69  (extra) {...fpri
8fa0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 25 73 5c  ntf(output, "%s\
8fb0: 6e 22 2c 20 65 78 74 72 61 29 3b 0a 09 7d 0a 0a  n", extra);..}..
8fc0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
8fd0: 20 22 55 73 61 67 65 3a 20 66 69 6c 65 64 20 5b   "Usage: filed [
8fe0: 3c 6f 70 74 69 6f 6e 73 3e 5d 5c 6e 22 29 3b 0a  <options>]\n");.
8ff0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9000: 20 22 20 20 4f 70 74 69 6f 6e 73 3a 5c 6e 22 29   "  Options:\n")
9010: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ;..fprintf(outpu
9020: 74 2c 20 22 20 20 20 20 20 20 2d 68 2c 20 2d 2d  t, "      -h, --
9030: 68 65 6c 70 5c 6e 22 29 3b 0a 09 66 70 72 69 6e  help\n");..fprin
9040: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9050: 20 20 2d 64 2c 20 2d 2d 64 61 65 6d 6f 6e 5c 6e    -d, --daemon\n
9060: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  ");..fprintf(out
9070: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 2c 20  put, "      -v, 
9080: 2d 2d 76 65 72 73 69 6f 6e 5c 6e 22 29 3b 0a 09  --version\n");..
9090: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
90a0: 22 20 20 20 20 20 20 2d 56 2c 20 2d 2d 76 68 6f  "      -V, --vho
90b0: 73 74 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66  st\n");..fprintf
90c0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
90d0: 2d 62 20 3c 61 64 64 72 65 73 73 3e 2c 20 2d 2d  -b <address>, --
90e0: 62 69 6e 64 20 3c 61 64 64 72 65 73 73 3e 5c 6e  bind <address>\n
90f0: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  ");..fprintf(out
9100: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 70 20 3c  put, "      -p <
9110: 70 6f 72 74 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70  port>, --port <p
9120: 6f 72 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e  ort>\n");..fprin
9130: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9140: 20 20 2d 74 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d    -t <count>, --
9150: 74 68 72 65 61 64 73 20 3c 63 6f 75 6e 74 3e 5c  threads <count>\
9160: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75  n");..fprintf(ou
9170: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 63 20  tput, "      -c 
9180: 3c 65 6e 74 72 69 65 73 3e 2c 20 2d 2d 63 61 63  <entries>, --cac
9190: 68 65 20 3c 65 6e 74 72 69 65 73 3e 5c 6e 22 29  he <entries>\n")
91a0: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ;..fprintf(outpu
91b0: 74 2c 20 22 20 20 20 20 20 20 2d 6c 20 3c 66 69  t, "      -l <fi
91c0: 6c 65 3e 2c 20 2d 2d 6c 6f 67 20 3c 66 69 6c 65  le>, --log <file
91d0: 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28  >\n");..fprintf(
91e0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
91f0: 75 20 3c 75 73 65 72 3e 2c 20 2d 2d 75 73 65 72  u <user>, --user
9200: 20 3c 75 73 65 72 3e 5c 6e 22 29 3b 0a 09 66 70   <user>\n");..fp
9210: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9220: 20 20 20 20 20 2d 72 20 3c 64 69 72 65 63 74 6f       -r <directo
9230: 72 79 3e 2c 20 2d 2d 72 6f 6f 74 20 3c 64 69 72  ry>, --root <dir
9240: 65 63 74 6f 72 79 3e 5c 6e 22 29 3b 0a 0a 09 69  ectory>\n");...i
9250: 66 20 28 6c 6f 6e 67 5f 68 65 6c 70 29 20 7b 0a  f (long_help) {.
9260: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9270: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  , "\n");...fprin
9280: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 55 73  tf(output, "  Us
9290: 61 67 65 3a 5c 6e 22 29 3b 0a 09 09 66 70 72 69  age:\n");...fpri
92a0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
92b0: 20 20 20 2d 68 20 28 6f 72 20 2d 2d 68 65 6c 70     -h (or --help
92c0: 29 20 70 72 69 6e 74 73 20 74 68 69 73 20 75 73  ) prints this us
92d0: 61 67 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  age information.
92e0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
92f0: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09  output, "\n");..
9300: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9310: 20 22 20 20 20 20 20 20 2d 64 20 28 6f 72 20 2d   "      -d (or -
9320: 2d 64 61 65 6d 6f 6e 29 20 69 6e 73 74 72 75 63  -daemon) instruc
9330: 74 73 20 66 69 6c 65 64 20 74 6f 20 62 65 63 6f  ts filed to beco
9340: 6d 65 20 61 20 64 61 65 6d 6f 6e 20 61 66 74 65  me a daemon afte
9350: 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 5c 6e  r initializing\n
9360: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
9370: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20  tput, "         
9380: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
9390: 65 20 6c 69 73 74 65 6e 69 6e 67 20 54 43 50 20  e listening TCP 
93a0: 73 6f 63 6b 65 74 20 61 6e 64 20 6c 6f 67 20 66  socket and log f
93b0: 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72  iles.\n");...fpr
93c0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e  intf(output, "\n
93d0: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
93e0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 20  tput, "      -v 
93f0: 28 6f 72 20 2d 2d 76 65 72 73 69 6f 6e 29 20 69  (or --version) i
9400: 6e 73 74 72 75 63 74 73 20 66 69 6c 65 64 20 70  nstructs filed p
9410: 72 69 6e 74 20 6f 75 74 20 74 68 65 20 76 65 72  rint out the ver
9420: 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20  sion number and 
9430: 65 78 69 74 2e 5c 6e 22 29 3b 0a 09 09 66 70 72  exit.\n");...fpr
9440: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e  intf(output, "\n
9450: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
9460: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 56 20  tput, "      -V 
9470: 28 6f 72 20 2d 2d 76 68 6f 73 74 29 20 69 6e 73  (or --vhost) ins
9480: 74 72 75 63 74 73 20 66 69 6c 65 64 20 74 6f 20  tructs filed to 
9490: 70 72 65 70 65 6e 64 20 61 6c 6c 20 72 65 71 75  prepend all requ
94a0: 65 73 74 73 20 77 69 74 68 20 74 68 65 69 72 20  ests with their 
94b0: 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69  HTTP\n");...fpri
94c0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
94d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94e0: 20 20 20 48 6f 73 74 20 68 65 61 64 65 72 2e 5c     Host header.\
94f0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
9500: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09  utput, "\n");...
9510: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
9520: 22 20 20 20 20 20 20 2d 62 20 28 6f 72 20 2d 2d  "      -b (or --
9530: 62 69 6e 64 29 20 73 70 65 63 69 66 69 65 73 20  bind) specifies 
9540: 74 68 65 20 61 64 64 72 65 73 73 20 74 6f 20 6c  the address to l
9550: 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69  isten for incomi
9560: 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66  ng HTTP\n");...f
9570: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
9580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9590: 20 20 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e       requests on
95a0: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 76  .  The default v
95b0: 61 6c 75 65 20 69 73 20 5c 22 25 73 5c 22 2e 5c  alue is \"%s\".\
95c0: 6e 22 2c 20 42 49 4e 44 5f 41 44 44 52 29 3b 0a  n", BIND_ADDR);.
95d0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
95e0: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  , "\n");...fprin
95f0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9600: 20 20 2d 70 20 28 6f 72 20 2d 2d 70 6f 72 74 29    -p (or --port)
9610: 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 54   specifies the T
9620: 43 50 20 70 6f 72 74 20 6e 75 6d 62 65 72 20 74  CP port number t
9630: 6f 20 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e 63  o listen for inc
9640: 6f 6d 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a  oming HTTP\n");.
9650: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9660: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
9670: 20 20 20 20 20 20 20 20 72 65 71 75 65 73 74 73          requests
9680: 20 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c   on.  The defaul
9690: 74 20 69 73 20 25 75 2e 5c 6e 22 2c 20 28 75 6e  t is %u.\n", (un
96a0: 73 69 67 6e 65 64 20 69 6e 74 29 20 50 4f 52 54  signed int) PORT
96b0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
96c0: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70  put, "\n");...fp
96d0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
96e0: 20 20 20 20 20 2d 74 20 28 6f 72 20 2d 2d 74 68       -t (or --th
96f0: 72 65 61 64 73 29 20 73 70 65 63 69 66 69 65 73  reads) specifies
9700: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77   the number of w
9710: 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 74 6f  orker threads to
9720: 20 63 72 65 61 74 65 2e 20 45 61 63 68 5c 6e 22   create. Each\n"
9730: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
9740: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
9750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 6f                wo
9760: 72 6b 65 72 20 74 68 72 65 61 64 20 63 61 6e 20  rker thread can 
9770: 73 65 72 76 69 63 65 20 6f 6e 65 20 63 6f 6e 63  service one conc
9780: 75 72 72 65 6e 74 20 48 54 54 50 20 73 65 73 73  urrent HTTP sess
9790: 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69  ion.\n");...fpri
97a0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
97b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97c0: 20 20 20 20 20 54 68 75 73 20 74 68 65 20 6e 75       Thus the nu
97d0: 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 20  mber of threads 
97e0: 63 72 65 61 74 65 64 20 77 69 6c 6c 20 64 65 74  created will det
97f0: 65 72 6d 69 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a  ermine how\n");.
9800: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9810: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
9820: 20 20 20 20 20 20 20 20 20 20 20 6d 61 6e 79 20             many 
9830: 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 74 72 61  simultaneous tra
9840: 6e 73 66 65 72 73 20 77 69 6c 6c 20 62 65 20 70  nsfers will be p
9850: 6f 73 73 69 62 6c 65 2e 20 54 68 65 5c 6e 22 29  ossible. The\n")
9860: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
9870: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
9880: 20 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66               def
9890: 61 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c  ault is %lu.\n",
98a0: 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29   (unsigned long)
98b0: 20 54 48 52 45 41 44 5f 43 4f 55 4e 54 29 3b 0a   THREAD_COUNT);.
98c0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
98d0: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  , "\n");...fprin
98e0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
98f0: 20 20 2d 63 20 28 6f 72 20 2d 2d 63 61 63 68 65    -c (or --cache
9900: 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  ) specifies the 
9910: 6e 75 6d 62 65 72 20 6f 66 20 66 69 6c 65 20 69  number of file i
9920: 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 61 63 68 65  nformation cache
9930: 20 65 6e 74 72 69 65 73 5c 6e 22 29 3b 0a 09 09   entries\n");...
9940: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
9950: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
9960: 20 20 20 20 20 20 20 74 6f 20 61 6c 6c 6f 63 61         to alloca
9970: 74 65 2e 20 20 45 61 63 68 20 63 61 63 68 65 20  te.  Each cache 
9980: 65 6e 74 72 79 20 68 6f 6c 64 73 20 66 69 6c 65  entry holds file
9990: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 5c   information as\
99a0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
99b0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
99c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 65                we
99d0: 6c 6c 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69  ll as an open fi
99e0: 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
99f0: 20 74 68 65 20 66 69 6c 65 2c 20 73 6f 20 72 65   the file, so re
9a00: 73 6f 75 72 63 65 5c 6e 22 29 3b 0a 09 09 66 70  source\n");...fp
9a10: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9a20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a30: 20 20 20 20 20 6c 69 6d 69 74 73 20 28 69 2e 65       limits (i.e
9a40: 2e 2c 20 75 6c 69 6d 69 74 29 20 73 68 6f 75 6c  ., ulimit) shoul
9a50: 64 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 2e  d be considered.
9a60: 20 20 54 68 69 73 20 73 68 6f 75 6c 64 5c 6e 22    This should\n"
9a70: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
9a80: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
9a90: 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20 61              be a
9aa0: 20 70 72 69 6d 65 20 6e 75 6d 62 65 72 20 66 6f   prime number fo
9ab0: 72 20 69 64 65 61 6c 20 75 73 65 20 77 69 74 68  r ideal use with
9ac0: 20 74 68 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 68   the lookup meth
9ad0: 6f 64 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  od.\n");...fprin
9ae0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9af0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b00: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73    The default is
9b10: 20 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67   %lu.\n", (unsig
9b20: 6e 65 64 20 6c 6f 6e 67 29 20 43 41 43 48 45 5f  ned long) CACHE_
9b30: 53 49 5a 45 29 3b 0a 09 09 66 70 72 69 6e 74 66  SIZE);...fprintf
9b40: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a  (output, "\n");.
9b50: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9b60: 2c 20 22 20 20 20 20 20 20 2d 6c 20 28 6f 72 20  , "      -l (or 
9b70: 2d 2d 6c 6f 67 29 20 73 70 65 63 69 66 69 65 73  --log) specifies
9b80: 20 61 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 6f   a filename to o
9b90: 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 20  pen for writing 
9ba0: 6c 6f 67 20 65 6e 74 72 69 65 73 2e 20 20 4c 6f  log entries.  Lo
9bb0: 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  g\n");...fprintf
9bc0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
9bd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 6e                en
9be0: 74 72 69 65 73 20 61 72 65 20 6d 61 64 65 20 66  tries are made f
9bf0: 6f 72 20 76 61 72 69 6f 75 73 20 73 74 61 67 65  or various stage
9c00: 73 20 69 6e 20 74 72 61 6e 73 66 65 72 69 6e 67  s in transfering
9c10: 20 66 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66   files.\n");...f
9c20: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
9c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c40: 20 20 20 20 54 68 65 20 6c 6f 67 20 66 69 6c 65      The log file
9c50: 20 69 73 20 6f 70 65 6e 65 64 20 62 65 66 6f 72   is opened befor
9c60: 65 20 73 77 69 74 63 68 69 6e 67 20 75 73 65 72  e switching user
9c70: 73 20 28 73 65 65 20 5c 22 2d 75 5c 22 29 5c 6e  s (see \"-u\")\n
9c80: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
9c90: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20  tput, "         
9ca0: 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 72             and r
9cb0: 6f 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73 20  oot directories 
9cc0: 28 73 65 65 20 5c 22 2d 72 5c 22 29 2e 20 20 54  (see \"-r\").  T
9cd0: 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e  he log file is n
9ce0: 65 76 65 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69  ever\n");...fpri
9cf0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9d00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9d10: 20 63 6c 6f 73 65 64 20 73 6f 20 6c 6f 67 20 72   closed so log r
9d20: 6f 74 61 74 69 6f 6e 20 77 69 74 68 6f 75 74 20  otation without 
9d30: 73 74 6f 70 70 69 6e 67 20 74 68 65 20 64 61 65  stopping the dae
9d40: 6d 6f 6e 20 69 73 20 77 69 6c 6c 5c 6e 22 29 3b  mon is will\n");
9d50: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
9d60: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
9d70: 20 20 20 20 20 20 20 20 6e 6f 74 20 77 6f 72 6b          not work
9d80: 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  .  The value of 
9d90: 5c 22 2d 5c 22 20 69 6e 64 69 63 61 74 65 73 20  \"-\" indicates 
9da0: 74 68 61 74 20 73 74 61 6e 64 61 72 64 20 6f 75  that standard ou
9db0: 74 70 75 74 5c 6e 22 29 3b 0a 09 09 66 70 72 69  tput\n");...fpri
9dc0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9dd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9de0: 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
9df0: 66 6f 72 20 6c 6f 67 67 69 6e 67 2e 20 20 49 66  for logging.  If
9e00: 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 62 65   the filename be
9e10: 67 69 6e 73 20 77 69 74 68 20 61 5c 6e 22 29 3b  gins with a\n");
9e20: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
9e30: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
9e40: 20 20 20 20 20 20 20 20 70 69 70 65 20 28 5c 22          pipe (\"
9e50: 7c 5c 22 29 20 74 68 65 6e 20 61 20 70 72 6f 63  |\") then a proc
9e60: 65 73 73 20 69 73 20 73 74 61 72 74 65 64 20 61  ess is started a
9e70: 6e 64 20 75 73 65 64 20 66 6f 72 20 6c 6f 67 67  nd used for logg
9e80: 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  ing\n");...fprin
9e90: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9ea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9eb0: 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 69 6c  instead of a fil
9ec0: 65 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  e.  The default 
9ed0: 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 4c  is \"%s\".\n", L
9ee0: 4f 47 5f 46 49 4c 45 29 3b 0a 23 69 66 64 65 66  OG_FILE);.#ifdef
9ef0: 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a   FILED_DONT_LOG.
9f00: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9f10: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
9f20: 20 20 20 20 20 20 20 4e 6f 74 65 20 74 68 61 74         Note that
9f30: 20 6c 6f 67 67 69 6e 67 20 69 73 20 63 6f 6d 70   logging is comp
9f40: 6c 65 74 65 6c 79 20 64 69 73 61 62 6c 65 64 20  letely disabled 
9f50: 73 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 64  so this option d
9f60: 6f 65 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  oes\n");...fprin
9f70: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9f80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f90: 6e 6f 74 68 69 6e 67 20 69 6e 20 74 68 69 73 20  nothing in this 
9fa0: 62 75 69 6c 64 2e 5c 6e 22 29 3b 0a 23 65 6e 64  build.\n");.#end
9fb0: 69 66 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  if...fprintf(out
9fc0: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70  put, "\n");...fp
9fd0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9fe0: 20 20 20 20 20 2d 75 20 28 6f 72 20 2d 2d 75 73       -u (or --us
9ff0: 65 72 29 20 73 70 65 63 69 66 69 65 73 20 74 68  er) specifies th
a000: 65 20 75 73 65 72 20 74 6f 20 73 77 69 74 63 68  e user to switch
a010: 20 75 73 65 72 20 49 44 73 20 74 6f 20 62 65 66   user IDs to bef
a020: 6f 72 65 20 73 65 72 76 69 63 69 6e 67 5c 6e 22  ore servicing\n"
a030: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
a040: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
a050: 20 20 20 20 20 20 20 20 20 20 20 72 65 71 75 65             reque
a060: 73 74 73 2e 20 20 54 68 65 20 64 65 66 61 75 6c  sts.  The defaul
a070: 74 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20  t is not change 
a080: 75 73 65 72 20 49 44 73 2e 5c 6e 22 29 3b 0a 09  user IDs.\n");..
a090: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
a0a0: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
a0b0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
a0c0: 20 2d 72 20 28 6f 72 20 2d 2d 72 6f 6f 74 29 20   -r (or --root) 
a0d0: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 64 69  specifies the di
a0e0: 72 65 63 74 6f 72 79 20 74 6f 20 61 63 74 20 61  rectory to act a
a0f0: 73 20 74 68 65 20 72 6f 6f 74 20 64 69 72 65 63  s the root direc
a100: 74 6f 72 79 20 66 6f 72 5c 6e 22 29 3b 0a 09 09  tory for\n");...
a110: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
a120: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
a130: 20 20 20 20 20 20 74 68 65 20 66 69 6c 65 20 73        the file s
a140: 65 72 76 65 72 2e 20 20 49 66 20 74 68 69 73 20  erver.  If this 
a150: 6f 70 74 69 6f 6e 20 69 73 20 73 70 65 63 69 66  option is specif
a160: 69 65 64 2c 20 63 68 72 6f 6f 74 28 32 29 5c 6e  ied, chroot(2)\n
a170: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
a180: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20  tput, "         
a190: 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 63              is c
a1a0: 61 6c 6c 65 64 2e 20 20 54 68 65 20 64 65 66 61  alled.  The defa
a1b0: 75 6c 74 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  ult is not chang
a1c0: 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 69  e root directori
a1d0: 65 73 2c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  es,\n");...fprin
a1e0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
a1f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a200: 20 74 68 61 74 20 69 73 2c 20 74 68 65 20 5c 22   that is, the \"
a210: 2f 5c 22 20 64 69 72 65 63 74 6f 72 79 20 69 73  /\" directory is
a220: 20 73 68 61 72 65 64 20 6f 75 74 2e 20 20 54 68   shared out.  Th
a230: 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66  is will\n");...f
a240: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
a250: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a260: 20 20 20 20 20 6c 69 6b 65 6c 79 20 62 65 20 61       likely be a
a270: 20 73 65 63 75 72 69 74 79 20 69 73 73 75 65 2c   security issue,
a280: 20 73 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20   so this option 
a290: 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 5c 6e 22  should always\n"
a2a0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
a2b0: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
a2c0: 20 20 20 20 20 20 20 20 20 20 20 62 65 20 75 73             be us
a2d0: 65 64 2e 5c 6e 22 29 3b 0a 09 7d 0a 0a 09 72 65  ed.\n");..}...re
a2e0: 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20  turn;.}../* Add 
a2f0: 61 20 67 65 74 6f 70 74 20 6f 70 74 69 6f 6e 20  a getopt option 
a300: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
a310: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67  iled_getopt_long
a320: 5f 73 65 74 6f 70 74 28 73 74 72 75 63 74 20 6f  _setopt(struct o
a330: 70 74 69 6f 6e 20 2a 6f 70 74 2c 20 63 6f 6e 73  ption *opt, cons
a340: 74 20 63 68 61 72 20 2a 6e 61 6d 65 2c 20 69 6e  t char *name, in
a350: 74 20 68 61 73 5f 61 72 67 2c 20 69 6e 74 20 76  t has_arg, int v
a360: 61 6c 29 20 7b 0a 09 6f 70 74 2d 3e 6e 61 6d 65  al) {..opt->name
a370: 20 20 20 20 20 3d 20 6e 61 6d 65 3b 0a 09 6f 70       = name;..op
a380: 74 2d 3e 68 61 73 5f 61 72 67 20 20 3d 20 68 61  t->has_arg  = ha
a390: 73 5f 61 72 67 3b 0a 09 6f 70 74 2d 3e 66 6c 61  s_arg;..opt->fla
a3a0: 67 20 20 20 20 20 3d 20 4e 55 4c 4c 3b 0a 09 6f  g     = NULL;..o
a3b0: 70 74 2d 3e 76 61 6c 20 20 20 20 20 20 3d 20 76  pt->val      = v
a3c0: 61 6c 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a  al;...return;.}.
a3d0: 0a 2f 2a 20 52 65 73 6f 6c 76 65 20 61 20 75 73  ./* Resolve a us
a3e0: 65 72 6e 61 6d 65 20 74 6f 20 61 20 55 49 44 20  ername to a UID 
a3f0: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
a400: 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28  led_user_lookup(
a410: 63 6f 6e 73 74 20 63 68 61 72 20 2a 75 73 65 72  const char *user
a420: 2c 20 75 69 64 5f 74 20 2a 75 73 65 72 5f 69 64  , uid_t *user_id
a430: 29 20 7b 0a 09 63 68 61 72 20 2a 6e 65 78 74 3b  ) {..char *next;
a440: 0a 09 75 69 64 5f 74 20 75 73 65 72 5f 69 64 5f  ..uid_t user_id_
a450: 63 68 65 63 6b 3b 0a 23 69 66 6e 64 65 66 20 46  check;.#ifndef F
a460: 49 4c 45 44 5f 4e 4f 5f 47 45 54 50 57 4e 41 4d  ILED_NO_GETPWNAM
a470: 0a 09 73 74 72 75 63 74 20 70 61 73 73 77 64 20  ..struct passwd 
a480: 2a 65 6e 74 3b 0a 0a 09 65 6e 74 20 3d 20 67 65  *ent;...ent = ge
a490: 74 70 77 6e 61 6d 28 75 73 65 72 29 3b 0a 09 69  tpwnam(user);..i
a4a0: 66 20 28 65 6e 74 20 21 3d 20 4e 55 4c 4c 29 20  f (ent != NULL) 
a4b0: 7b 0a 09 09 2a 75 73 65 72 5f 69 64 20 3d 20 65  {...*user_id = e
a4c0: 6e 74 2d 3e 70 77 5f 75 69 64 3b 0a 0a 09 09 72  nt->pw_uid;....r
a4d0: 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 23 65 6e  eturn(0);..}.#en
a4e0: 64 69 66 0a 0a 09 75 73 65 72 5f 69 64 5f 63 68  dif...user_id_ch
a4f0: 65 63 6b 20 3d 20 73 74 72 74 6f 75 6c 6c 28 75  eck = strtoull(u
a500: 73 65 72 2c 20 26 6e 65 78 74 2c 20 31 30 29 3b  ser, &next, 10);
a510: 0a 09 69 66 20 28 6e 65 78 74 20 3d 3d 20 4e 55  ..if (next == NU
a520: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31  LL) {...return(1
a530: 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 6e 65 78 74  );..}...if (next
a540: 5b 30 5d 20 21 3d 20 27 5c 30 27 29 20 7b 0a 09  [0] != '\0') {..
a550: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a  .return(1);..}..
a560: 09 2a 75 73 65 72 5f 69 64 20 3d 20 75 73 65 72  .*user_id = user
a570: 5f 69 64 5f 63 68 65 63 6b 3b 0a 0a 09 72 65 74  _id_check;...ret
a580: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 61  urn(0);.}../* Da
a590: 65 6d 6f 6e 69 7a 65 20 2a 2f 0a 73 74 61 74 69  emonize */.stati
a5a0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 64 61 65 6d  c int filed_daem
a5b0: 6f 6e 69 7a 65 28 76 6f 69 64 29 20 7b 0a 09 70  onize(void) {..p
a5c0: 69 64 5f 74 20 73 65 74 73 69 64 5f 72 65 74 2c  id_t setsid_ret,
a5d0: 20 66 6f 72 6b 5f 72 65 74 3b 0a 09 69 6e 74 20   fork_ret;..int 
a5e0: 63 68 64 69 72 5f 72 65 74 2c 20 64 75 70 32 5f  chdir_ret, dup2_
a5f0: 72 65 74 3b 0a 09 69 6e 74 20 66 64 5f 69 6e 2c  ret;..int fd_in,
a600: 20 66 64 5f 6f 75 74 3b 0a 0a 09 63 68 64 69 72   fd_out;...chdir
a610: 5f 72 65 74 20 3d 20 63 68 64 69 72 28 22 2f 22  _ret = chdir("/"
a620: 29 3b 0a 09 69 66 20 28 63 68 64 69 72 5f 72 65  );..if (chdir_re
a630: 74 20 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75  t != 0) {...retu
a640: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 6b  rn(1);..}...fork
a650: 5f 72 65 74 20 3d 20 66 6f 72 6b 28 29 3b 0a 09  _ret = fork();..
a660: 69 66 20 28 66 6f 72 6b 5f 72 65 74 20 3c 20 30  if (fork_ret < 0
a670: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b  ) {...return(1);
a680: 0a 09 7d 0a 0a 09 69 66 20 28 66 6f 72 6b 5f 72  ..}...if (fork_r
a690: 65 74 20 3e 20 30 29 20 7b 0a 09 09 2f 2a 20 50  et > 0) {.../* P
a6a0: 61 72 65 6e 74 20 2a 2f 0a 09 09 77 61 69 74 70  arent */...waitp
a6b0: 69 64 28 66 6f 72 6b 5f 72 65 74 2c 20 4e 55 4c  id(fork_ret, NUL
a6c0: 4c 2c 20 30 29 3b 0a 0a 09 09 65 78 69 74 28 45  L, 0);....exit(E
a6d0: 58 49 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 7d  XIT_SUCCESS);..}
a6e0: 0a 0a 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09  .../* Child */..
a6f0: 69 66 20 28 66 6f 72 6b 28 29 20 21 3d 20 30 29  if (fork() != 0)
a700: 20 7b 0a 09 09 2f 2a 20 43 68 69 6c 64 20 2a 2f   {.../* Child */
a710: 0a 09 09 65 78 69 74 28 45 58 49 54 5f 53 55 43  ...exit(EXIT_SUC
a720: 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 47  CESS);..}.../* G
a730: 72 61 6e 64 20 63 68 69 6c 64 20 2a 2f 0a 09 73  rand child */..s
a740: 65 74 73 69 64 5f 72 65 74 20 3d 20 73 65 74 73  etsid_ret = sets
a750: 69 64 28 29 3b 0a 09 69 66 20 28 73 65 74 73 69  id();..if (setsi
a760: 64 5f 72 65 74 20 3d 3d 20 28 28 70 69 64 5f 74  d_ret == ((pid_t
a770: 29 20 2d 31 29 29 20 7b 0a 09 09 72 65 74 75 72  ) -1)) {...retur
a780: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66 64 5f 69 6e  n(1);..}...fd_in
a790: 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75   = open("/dev/nu
a7a0: 6c 6c 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a  ll", O_RDONLY);.
a7b0: 09 66 64 5f 6f 75 74 20 3d 20 6f 70 65 6e 28 22  .fd_out = open("
a7c0: 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 52  /dev/null", O_WR
a7d0: 4f 4e 4c 59 29 3b 0a 09 69 66 20 28 66 64 5f 69  ONLY);..if (fd_i
a7e0: 6e 20 3c 20 30 20 7c 7c 20 66 64 5f 6f 75 74 20  n < 0 || fd_out 
a7f0: 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  < 0) {...return(
a800: 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65  1);..}...dup2_re
a810: 74 20 3d 20 64 75 70 32 28 66 64 5f 69 6e 2c 20  t = dup2(fd_in, 
a820: 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09  STDIN_FILENO);..
a830: 69 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20  if (dup2_ret != 
a840: 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 20 7b 0a  STDIN_FILENO) {.
a850: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a  ..return(1);..}.
a860: 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70  ..dup2_ret = dup
a870: 32 28 66 64 5f 6f 75 74 2c 20 53 54 44 4f 55 54  2(fd_out, STDOUT
a880: 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64  _FILENO);..if (d
a890: 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 4f 55  up2_ret != STDOU
a8a0: 54 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65  T_FILENO) {...re
a8b0: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75  turn(1);..}...du
a8c0: 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64  p2_ret = dup2(fd
a8d0: 5f 6f 75 74 2c 20 53 54 44 45 52 52 5f 46 49 4c  _out, STDERR_FIL
a8e0: 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f  ENO);..if (dup2_
a8f0: 72 65 74 20 21 3d 20 53 54 44 45 52 52 5f 46 49  ret != STDERR_FI
a900: 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e  LENO) {...return
a910: 28 31 29 3b 0a 09 7d 0a 0a 09 63 6c 6f 73 65 28  (1);..}...close(
a920: 66 64 5f 69 6e 29 3b 0a 09 63 6c 6f 73 65 28 66  fd_in);..close(f
a930: 64 5f 6f 75 74 29 3b 0a 0a 09 72 65 74 75 72 6e  d_out);...return
a940: 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 70  (0);.}../* Run p
a950: 72 6f 63 65 73 73 20 2a 2f 0a 69 6e 74 20 6d 61  rocess */.int ma
a960: 69 6e 28 69 6e 74 20 61 72 67 63 2c 20 63 68 61  in(int argc, cha
a970: 72 20 2a 2a 61 72 67 76 29 20 7b 0a 09 73 74 72  r **argv) {..str
a980: 75 63 74 20 6f 70 74 69 6f 6e 20 6f 70 74 69 6f  uct option optio
a990: 6e 73 5b 31 32 5d 3b 0a 09 73 74 72 75 63 74 20  ns[12];..struct 
a9a0: 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 74 68  filed_options th
a9b0: 72 65 61 64 5f 6f 70 74 69 6f 6e 73 20 3d 20 7b  read_options = {
a9c0: 30 7d 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20  0};..const char 
a9d0: 2a 62 69 6e 64 5f 61 64 64 72 20 3d 20 42 49 4e  *bind_addr = BIN
a9e0: 44 5f 41 44 44 52 2c 20 2a 6e 65 77 72 6f 6f 74  D_ADDR, *newroot
a9f0: 20 3d 20 4e 55 4c 4c 2c 20 2a 6c 6f 67 5f 66 69   = NULL, *log_fi
aa00: 6c 65 20 3d 20 4c 4f 47 5f 46 49 4c 45 3b 0a 09  le = LOG_FILE;..
aa10: 46 49 4c 45 20 2a 6c 6f 67 5f 66 70 3b 0a 09 75  FILE *log_fp;..u
aa20: 69 64 5f 74 20 75 73 65 72 20 3d 20 30 3b 0a 09  id_t user = 0;..
aa30: 69 6e 74 20 70 6f 72 74 20 3d 20 50 4f 52 54 2c  int port = PORT,
aa40: 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d 20   thread_count = 
aa50: 54 48 52 45 41 44 5f 43 4f 55 4e 54 3b 0a 09 69  THREAD_COUNT;..i
aa60: 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 20 3d 20  nt cache_size = 
aa70: 43 41 43 48 45 5f 53 49 5a 45 3b 0a 09 69 6e 74  CACHE_SIZE;..int
aa80: 20 69 6e 69 74 5f 72 65 74 2c 20 63 68 72 6f 6f   init_ret, chroo
aa90: 74 5f 72 65 74 2c 20 73 65 74 75 69 64 5f 72 65  t_ret, setuid_re
aaa0: 74 2c 20 6c 6f 6f 6b 75 70 5f 72 65 74 2c 20 63  t, lookup_ret, c
aab0: 68 64 69 72 5f 72 65 74 3b 0a 09 69 6e 74 20 73  hdir_ret;..int s
aac0: 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 20 3d 20  etuid_enabled = 
aad0: 30 2c 20 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65  0, daemon_enable
aae0: 64 20 3d 20 30 3b 0a 09 69 6e 74 20 63 68 3b 0a  d = 0;..int ch;.
aaf0: 09 69 6e 74 20 66 64 3b 0a 0a 09 2f 2a 20 50 72  .int fd;.../* Pr
ab00: 6f 63 65 73 73 20 61 72 67 75 6d 65 6e 74 73 20  ocess arguments 
ab10: 2a 2f 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74  */..filed_getopt
ab20: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70  _long_setopt(&op
ab30: 74 69 6f 6e 73 5b 30 5d 2c 20 22 70 6f 72 74 22  tions[0], "port"
ab40: 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d  , required_argum
ab50: 65 6e 74 2c 20 27 70 27 29 3b 0a 09 66 69 6c 65  ent, 'p');..file
ab60: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65  d_getopt_long_se
ab70: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 5d  topt(&options[1]
ab80: 2c 20 22 74 68 72 65 61 64 73 22 2c 20 72 65 71  , "threads", req
ab90: 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20  uired_argument, 
aba0: 27 74 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74  't');..filed_get
abb0: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28  opt_long_setopt(
abc0: 26 6f 70 74 69 6f 6e 73 5b 32 5d 2c 20 22 63 61  &options[2], "ca
abd0: 63 68 65 22 2c 20 72 65 71 75 69 72 65 64 5f 61  che", required_a
abe0: 72 67 75 6d 65 6e 74 2c 20 27 63 27 29 3b 0a 09  rgument, 'c');..
abf0: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e  filed_getopt_lon
ac00: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e  g_setopt(&option
ac10: 73 5b 33 5d 2c 20 22 62 69 6e 64 22 2c 20 72 65  s[3], "bind", re
ac20: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c  quired_argument,
ac30: 20 27 62 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65   'b');..filed_ge
ac40: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74  topt_long_setopt
ac50: 28 26 6f 70 74 69 6f 6e 73 5b 34 5d 2c 20 22 75  (&options[4], "u
ac60: 73 65 72 22 2c 20 72 65 71 75 69 72 65 64 5f 61  ser", required_a
ac70: 72 67 75 6d 65 6e 74 2c 20 27 75 27 29 3b 0a 09  rgument, 'u');..
ac80: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e  filed_getopt_lon
ac90: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e  g_setopt(&option
aca0: 73 5b 35 5d 2c 20 22 72 6f 6f 74 22 2c 20 72 65  s[5], "root", re
acb0: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c  quired_argument,
acc0: 20 27 72 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65   'r');..filed_ge
acd0: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74  topt_long_setopt
ace0: 28 26 6f 70 74 69 6f 6e 73 5b 36 5d 2c 20 22 68  (&options[6], "h
acf0: 65 6c 70 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e  elp", no_argumen
ad00: 74 2c 20 27 68 27 29 3b 0a 09 66 69 6c 65 64 5f  t, 'h');..filed_
ad10: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f  getopt_long_seto
ad20: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 37 5d 2c 20  pt(&options[7], 
ad30: 22 64 61 65 6d 6f 6e 22 2c 20 6e 6f 5f 61 72 67  "daemon", no_arg
ad40: 75 6d 65 6e 74 2c 20 27 64 27 29 3b 0a 09 66 69  ument, 'd');..fi
ad50: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f  led_getopt_long_
ad60: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b  setopt(&options[
ad70: 38 5d 2c 20 22 6c 6f 67 22 2c 20 72 65 71 75 69  8], "log", requi
ad80: 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 6c  red_argument, 'l
ad90: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70  ');..filed_getop
ada0: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f  t_long_setopt(&o
adb0: 70 74 69 6f 6e 73 5b 39 5d 2c 20 22 76 65 72 73  ptions[9], "vers
adc0: 69 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e  ion", no_argumen
add0: 74 2c 20 27 76 27 29 3b 0a 09 66 69 6c 65 64 5f  t, 'v');..filed_
ade0: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f  getopt_long_seto
adf0: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 30 5d 2c  pt(&options[10],
ae00: 20 22 76 68 6f 73 74 22 2c 20 6e 6f 5f 61 72 67   "vhost", no_arg
ae10: 75 6d 65 6e 74 2c 20 27 56 27 29 3b 0a 09 66 69  ument, 'V');..fi
ae20: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f  led_getopt_long_
ae30: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b  setopt(&options[
ae40: 31 31 5d 2c 20 4e 55 4c 4c 2c 20 30 2c 20 30 29  11], NULL, 0, 0)
ae50: 3b 0a 09 77 68 69 6c 65 20 28 28 63 68 20 3d 20  ;..while ((ch = 
ae60: 67 65 74 6f 70 74 5f 6c 6f 6e 67 28 61 72 67 63  getopt_long(argc
ae70: 2c 20 61 72 67 76 2c 20 22 70 3a 74 3a 63 3a 62  , argv, "p:t:c:b
ae80: 3a 75 3a 72 3a 6c 3a 68 64 76 56 22 2c 20 6f 70  :u:r:l:hdvV", op
ae90: 74 69 6f 6e 73 2c 20 4e 55 4c 4c 29 29 20 21 3d  tions, NULL)) !=
aea0: 20 2d 31 29 20 7b 0a 09 09 73 77 69 74 63 68 28   -1) {...switch(
aeb0: 63 68 29 20 7b 0a 09 09 09 63 61 73 65 20 27 70  ch) {....case 'p
aec0: 27 3a 0a 09 09 09 09 70 6f 72 74 20 3d 20 61 74  ':.....port = at
aed0: 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09  oi(optarg);.....
aee0: 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27  break;....case '
aef0: 74 27 3a 0a 09 09 09 09 74 68 72 65 61 64 5f 63  t':.....thread_c
af00: 6f 75 6e 74 20 3d 20 61 74 6f 69 28 6f 70 74 61  ount = atoi(opta
af10: 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a  rg);.....break;.
af20: 09 09 09 63 61 73 65 20 27 63 27 3a 0a 09 09 09  ...case 'c':....
af30: 09 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 61 74  .cache_size = at
af40: 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09  oi(optarg);.....
af50: 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27  break;....case '
af60: 62 27 3a 0a 09 09 09 09 62 69 6e 64 5f 61 64 64  b':.....bind_add
af70: 72 20 3d 20 73 74 72 64 75 70 28 6f 70 74 61 72  r = strdup(optar
af80: 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09  g);.....break;..
af90: 09 09 63 61 73 65 20 27 75 27 3a 0a 09 09 09 09  ..case 'u':.....
afa0: 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 20 3d  setuid_enabled =
afb0: 20 31 3b 0a 09 09 09 09 6c 6f 6f 6b 75 70 5f 72   1;.....lookup_r
afc0: 65 74 20 3d 20 66 69 6c 65 64 5f 75 73 65 72 5f  et = filed_user_
afd0: 6c 6f 6f 6b 75 70 28 6f 70 74 61 72 67 2c 20 26  lookup(optarg, &
afe0: 75 73 65 72 29 3b 0a 09 09 09 09 69 66 20 28 6c  user);.....if (l
aff0: 6f 6f 6b 75 70 5f 72 65 74 20 21 3d 20 30 29 20  ookup_ret != 0) 
b000: 7b 0a 09 09 09 09 09 66 69 6c 65 64 5f 70 72 69  {......filed_pri
b010: 6e 74 5f 68 65 6c 70 28 73 74 64 65 72 72 2c 20  nt_help(stderr, 
b020: 30 2c 20 22 49 6e 76 61 6c 69 64 20 75 73 65 72  0, "Invalid user
b030: 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64 22 29  name specified")
b040: 3b 0a 0a 09 09 09 09 09 72 65 74 75 72 6e 28 31  ;.......return(1
b050: 29 3b 0a 09 09 09 09 7d 0a 09 09 09 09 62 72 65  );.....}.....bre
b060: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 72 27 3a  ak;....case 'r':
b070: 0a 09 09 09 09 66 69 6c 65 64 5f 70 61 74 68 5f  .....filed_path_
b080: 74 72 61 6e 73 6c 61 74 65 5f 73 65 74 5f 72 6f  translate_set_ro
b090: 6f 74 28 6e 65 77 72 6f 6f 74 2c 20 26 74 68 72  ot(newroot, &thr
b0a0: 65 61 64 5f 6f 70 74 69 6f 6e 73 2c 20 6f 70 74  ead_options, opt
b0b0: 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b  arg);.....break;
b0c0: 0a 09 09 09 63 61 73 65 20 27 6c 27 3a 0a 09 09  ....case 'l':...
b0d0: 09 09 6c 6f 67 5f 66 69 6c 65 20 3d 20 73 74 72  ..log_file = str
b0e0: 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09  dup(optarg);....
b0f0: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20  .break;....case 
b100: 27 64 27 3a 0a 09 09 09 09 64 61 65 6d 6f 6e 5f  'd':.....daemon_
b110: 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09  enabled = 1;....
b120: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20  .break;....case 
b130: 27 56 27 3a 0a 09 09 09 09 74 68 72 65 61 64 5f  'V':.....thread_
b140: 6f 70 74 69 6f 6e 73 2e 76 68 6f 73 74 73 5f 65  options.vhosts_e
b150: 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 0a 09 09 09  nabled = 1;.....
b160: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20  .break;....case 
b170: 27 76 27 3a 0a 09 09 09 09 70 72 69 6e 74 66 28  'v':.....printf(
b180: 22 66 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 25  "filed version %
b190: 73 5c 6e 22 2c 20 46 49 4c 45 44 5f 56 45 52 53  s\n", FILED_VERS
b1a0: 49 4f 4e 29 3b 0a 0a 09 09 09 09 72 65 74 75 72  ION);......retur
b1b0: 6e 28 30 29 3b 0a 09 09 09 63 61 73 65 20 27 3f  n(0);....case '?
b1c0: 27 3a 0a 09 09 09 63 61 73 65 20 27 3a 27 3a 0a  ':....case ':':.
b1d0: 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f  ....filed_print_
b1e0: 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20  help(stderr, 0, 
b1f0: 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75  NULL);......retu
b200: 72 6e 28 31 29 3b 0a 09 09 09 63 61 73 65 20 27  rn(1);....case '
b210: 68 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 72  h':.....filed_pr
b220: 69 6e 74 5f 68 65 6c 70 28 73 74 64 6f 75 74 2c  int_help(stdout,
b230: 20 31 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09   1, NULL);......
b240: 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 7d 0a 09  return(0);...}..
b250: 7d 0a 0a 09 2f 2a 20 4f 70 65 6e 20 6c 6f 67 20  }.../* Open log 
b260: 66 69 6c 65 20 2a 2f 0a 09 6c 6f 67 5f 66 70 20  file */..log_fp 
b270: 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e  = filed_log_open
b280: 28 6c 6f 67 5f 66 69 6c 65 29 3b 0a 09 69 66 20  (log_file);..if 
b290: 28 6c 6f 67 5f 66 70 20 3d 3d 20 4e 55 4c 4c 29  (log_fp == NULL)
b2a0: 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c   {...perror("fil
b2b0: 65 64 5f 6c 6f 67 5f 6f 70 65 6e 22 29 3b 0a 0a  ed_log_open");..
b2c0: 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d 0a  ..return(4);..}.
b2d0: 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 69 73 74  ../* Create list
b2e0: 65 6e 69 6e 67 20 73 6f 63 6b 65 74 20 2a 2f 0a  ening socket */.
b2f0: 09 66 64 20 3d 20 66 69 6c 65 64 5f 6c 69 73 74  .fd = filed_list
b300: 65 6e 28 62 69 6e 64 5f 61 64 64 72 2c 20 70 6f  en(bind_addr, po
b310: 72 74 29 3b 0a 09 69 66 20 28 66 64 20 3c 20 30  rt);..if (fd < 0
b320: 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69  ) {...perror("fi
b330: 6c 65 64 5f 6c 69 73 74 65 6e 22 29 3b 0a 0a 09  led_listen");...
b340: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a  .return(1);..}..
b350: 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  ./* Initialize t
b360: 69 6d 65 6f 75 74 20 73 74 72 75 63 74 75 72 65  imeout structure
b370: 73 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d  s */..init_ret =
b380: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d   filed_sockettim
b390: 65 6f 75 74 5f 69 6e 69 74 28 29 3b 0a 09 69 66  eout_init();..if
b3a0: 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29   (init_ret != 0)
b3b0: 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c   {...perror("fil
b3c0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
b3d0: 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75  _init");....retu
b3e0: 72 6e 28 38 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 42  rn(8);..}.../* B
b3f0: 65 63 6f 6d 65 20 61 20 64 61 65 6d 6f 6e 20 2a  ecome a daemon *
b400: 2f 0a 09 69 66 20 28 64 61 65 6d 6f 6e 5f 65 6e  /..if (daemon_en
b410: 61 62 6c 65 64 29 20 7b 0a 09 09 69 6e 69 74 5f  abled) {...init_
b420: 72 65 74 20 3d 20 66 69 6c 65 64 5f 64 61 65 6d  ret = filed_daem
b430: 6f 6e 69 7a 65 28 29 3b 0a 09 09 69 66 20 28 69  onize();...if (i
b440: 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a  nit_ret != 0) {.
b450: 09 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64  ...perror("filed
b460: 5f 64 61 65 6d 6f 6e 69 7a 65 22 29 3b 0a 0a 09  _daemonize");...
b470: 09 09 72 65 74 75 72 6e 28 36 29 3b 0a 09 09 7d  ..return(6);...}
b480: 0a 09 7d 0a 0a 09 2f 2a 20 43 68 72 6f 6f 74 2c  ..}.../* Chroot,
b490: 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 20   if appropriate 
b4a0: 2a 2f 0a 09 69 66 20 28 6e 65 77 72 6f 6f 74 29  */..if (newroot)
b4b0: 20 7b 0a 09 09 63 68 64 69 72 5f 72 65 74 20 3d   {...chdir_ret =
b4c0: 20 63 68 64 69 72 28 6e 65 77 72 6f 6f 74 29 3b   chdir(newroot);
b4d0: 0a 09 09 69 66 20 28 63 68 64 69 72 5f 72 65 74  ...if (chdir_ret
b4e0: 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72   != 0) {....perr
b4f0: 6f 72 28 22 63 68 64 69 72 22 29 3b 0a 0a 09 09  or("chdir");....
b500: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a  .return(1);...}.
b510: 0a 09 09 63 68 72 6f 6f 74 5f 72 65 74 20 3d 20  ...chroot_ret = 
b520: 63 68 72 6f 6f 74 28 22 2e 22 29 3b 0a 09 09 69  chroot(".");...i
b530: 66 20 28 63 68 72 6f 6f 74 5f 72 65 74 20 21 3d  f (chroot_ret !=
b540: 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28   0) {....perror(
b550: 22 63 68 72 6f 6f 74 22 29 3b 0a 0a 09 09 09 72  "chroot");.....r
b560: 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d  eturn(1);...}..}
b570: 0a 0a 09 2f 2a 20 44 72 6f 70 20 70 72 69 76 69  .../* Drop privi
b580: 6c 65 67 65 73 2c 20 69 66 20 61 70 70 72 6f 70  leges, if approp
b590: 72 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 73 65  riate */..if (se
b5a0: 74 75 69 64 5f 65 6e 61 62 6c 65 64 29 20 7b 0a  tuid_enabled) {.
b5b0: 09 09 73 65 74 75 69 64 5f 72 65 74 20 3d 20 73  ..setuid_ret = s
b5c0: 65 74 75 69 64 28 75 73 65 72 29 3b 0a 09 09 69  etuid(user);...i
b5d0: 66 20 28 73 65 74 75 69 64 5f 72 65 74 20 21 3d  f (setuid_ret !=
b5e0: 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28   0) {....perror(
b5f0: 22 73 65 74 75 69 64 22 29 3b 0a 0a 09 09 09 72  "setuid");.....r
b600: 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d  eturn(1);...}..}
b610: 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .../* Initialize
b620: 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20   */..init_ret = 
b630: 66 69 6c 65 64 5f 69 6e 69 74 28 63 61 63 68 65  filed_init(cache
b640: 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 69 6e 69  _size);..if (ini
b650: 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09  t_ret != 0) {...
b660: 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 69 6e  perror("filed_in
b670: 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28  it");....return(
b680: 33 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61  3);..}.../* Crea
b690: 74 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61  te logging threa
b6a0: 64 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d  d */..init_ret =
b6b0: 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74   filed_logging_t
b6c0: 68 72 65 61 64 5f 69 6e 69 74 28 6c 6f 67 5f 66  hread_init(log_f
b6d0: 70 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65  p);..if (init_re
b6e0: 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72  t != 0) {...perr
b6f0: 6f 72 28 22 66 69 6c 65 64 5f 6c 6f 67 67 69 6e  or("filed_loggin
b700: 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 22 29 3b  g_thread_init");
b710: 0a 0a 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09  ....return(4);..
b720: 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 73 6f  }.../* Create so
b730: 63 6b 65 74 20 74 65 72 6d 69 6e 61 74 69 6f 6e  cket termination
b740: 20 74 68 72 65 61 64 20 2a 2f 0a 09 69 6e 69 74   thread */..init
b750: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 73 6f 63  _ret = filed_soc
b760: 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61  kettimeout_threa
b770: 64 5f 69 6e 69 74 28 29 3b 0a 09 69 66 20 28 69  d_init();..if (i
b780: 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a  nit_ret != 0) {.
b790: 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f  ..perror("filed_
b7a0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68  sockettimeout_th
b7b0: 72 65 61 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09  read_init");....
b7c0: 72 65 74 75 72 6e 28 37 29 3b 0a 09 7d 0a 0a 09  return(7);..}...
b7d0: 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 6b 65 72  /* Create worker
b7e0: 20 74 68 72 65 61 64 73 20 2a 2f 0a 09 69 6e 69   threads */..ini
b7f0: 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 77 6f  t_ret = filed_wo
b800: 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69  rker_threads_ini
b810: 74 28 66 64 2c 20 74 68 72 65 61 64 5f 63 6f 75  t(fd, thread_cou
b820: 6e 74 2c 20 26 74 68 72 65 61 64 5f 6f 70 74 69  nt, &thread_opti
b830: 6f 6e 73 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f  ons);..if (init_
b840: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65  ret != 0) {...pe
b850: 72 72 6f 72 28 22 66 69 6c 65 64 5f 77 6f 72 6b  rror("filed_work
b860: 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 22  er_threads_init"
b870: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 35 29 3b  );....return(5);
b880: 0a 09 7d 0a 0a 09 2f 2a 20 57 61 69 74 20 66 6f  ..}.../* Wait fo
b890: 72 20 74 68 72 65 61 64 73 20 74 6f 20 65 78 69  r threads to exi
b8a0: 74 20 2a 2f 0a 09 2f 2a 20 58 58 58 3a 54 4f 44  t */../* XXX:TOD
b8b0: 4f 3a 20 4d 6f 6e 69 74 6f 72 20 74 68 72 65 61  O: Monitor threa
b8c0: 64 20 75 73 61 67 65 20 2a 2f 0a 09 77 68 69 6c  d usage */..whil
b8d0: 65 20 28 31 29 20 7b 0a 09 09 73 6c 65 65 70 28  e (1) {...sleep(
b8e0: 36 30 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65 74  60);..}.../* Ret
b8f0: 75 72 6e 20 69 6e 20 66 61 69 6c 75 72 65 20 2a  urn in failure *
b900: 2f 0a 09 72 65 74 75 72 6e 28 32 29 3b 0a 7d 0a  /..return(2);.}.