Hex Artifact Content

Artifact 59492d048fdaa97e31a21a7d41e871faf2a0e315:


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: 23 69 66 6e 64 65 66 20 46 49 4c 45 44 5f 44 4f  #ifndef FILED_DO
0720: 4e 54 5f 54 49 4d 45 4f 55 54 0a 23 69 6e 63 6c  NT_TIMEOUT.#incl
0730: 75 64 65 20 3c 73 74 64 61 74 6f 6d 69 63 2e 68  ude <stdatomic.h
0740: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 62  >.#include <stdb
0750: 6f 6f 6c 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f  ool.h>.#endif../
0760: 2a 20 43 6f 6d 70 69 6c 65 20 74 69 6d 65 20 63  * Compile time c
0770: 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 23 64 65 66  onstants */.#def
0780: 69 6e 65 20 46 49 4c 45 44 5f 56 45 52 53 49 4f  ine FILED_VERSIO
0790: 4e 20 22 31 2e 31 33 22 0a 23 64 65 66 69 6e 65  N "1.13".#define
07a0: 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 5f   FILED_SENDFILE_
07b0: 4d 41 58 20 31 36 37 37 37 32 31 35 0a 23 64 65  MAX 16777215.#de
07c0: 66 69 6e 65 20 46 49 4c 45 44 5f 4d 41 58 5f 46  fine FILED_MAX_F
07d0: 41 49 4c 55 52 45 5f 43 4f 55 4e 54 20 33 30 0a  AILURE_COUNT 30.
07e0: 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 44 45  #define FILED_DE
07f0: 46 41 55 4c 54 5f 54 59 50 45 20 22 61 70 70 6c  FAULT_TYPE "appl
0800: 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d 73 74  ication/octet-st
0810: 72 65 61 6d 22 0a 23 64 65 66 69 6e 65 20 46 49  ream".#define FI
0820: 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f  LED_PATH_BUFFER_
0830: 53 49 5a 45 20 31 30 31 30 0a 0a 2f 2a 20 44 65  SIZE 1010../* De
0840: 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f 0a  fault values */.
0850: 23 64 65 66 69 6e 65 20 50 4f 52 54 20 38 30 0a  #define PORT 80.
0860: 23 64 65 66 69 6e 65 20 54 48 52 45 41 44 5f 43  #define THREAD_C
0870: 4f 55 4e 54 20 35 0a 23 64 65 66 69 6e 65 20 42  OUNT 5.#define B
0880: 49 4e 44 5f 41 44 44 52 20 22 3a 3a 22 0a 23 64  IND_ADDR "::".#d
0890: 65 66 69 6e 65 20 43 41 43 48 45 5f 53 49 5a 45  efine CACHE_SIZE
08a0: 20 38 32 30 39 0a 23 64 65 66 69 6e 65 20 4c 4f   8209.#define LO
08b0: 47 5f 46 49 4c 45 20 22 2d 22 0a 0a 2f 2a 20 46  G_FILE "-"../* F
08c0: 75 7a 7a 69 6e 67 20 54 65 73 74 20 43 6f 64 65  uzzing Test Code
08d0: 20 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c 45 44   */.#ifdef FILED
08e0: 5f 54 45 53 54 5f 41 46 4c 0a 23 64 65 66 69 6e  _TEST_AFL.#defin
08f0: 65 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47  e FILED_DONT_LOG
0900: 20 31 0a 23 64 65 66 69 6e 65 20 70 74 68 72 65   1.#define pthre
0910: 61 64 5f 63 72 65 61 74 65 28 61 2c 20 78 2c 20  ad_create(a, x, 
0920: 79 2c 20 7a 29 20 61 66 6c 5f 70 74 68 72 65 61  y, z) afl_pthrea
0930: 64 5f 63 72 65 61 74 65 28 61 2c 20 78 2c 20 79  d_create(a, x, y
0940: 2c 20 7a 29 0a 23 64 65 66 69 6e 65 20 62 69 6e  , z).#define bin
0950: 64 28 78 2c 20 79 2c 20 7a 29 20 61 66 6c 5f 62  d(x, y, z) afl_b
0960: 69 6e 64 28 78 2c 20 79 2c 20 7a 29 0a 23 64 65  ind(x, y, z).#de
0970: 66 69 6e 65 20 73 6f 63 6b 65 74 28 78 2c 20 79  fine socket(x, y
0980: 2c 20 7a 29 20 38 31 39 33 0a 23 64 65 66 69 6e  , z) 8193.#defin
0990: 65 20 6c 69 73 74 65 6e 28 78 2c 20 79 29 20 30  e listen(x, y) 0
09a0: 0a 23 64 65 66 69 6e 65 20 61 63 63 65 70 74 28  .#define accept(
09b0: 78 2c 20 79 2c 20 7a 29 20 61 66 6c 5f 61 63 63  x, y, z) afl_acc
09c0: 65 70 74 28 78 2c 20 79 2c 20 7a 29 0a 23 64 65  ept(x, y, z).#de
09d0: 66 69 6e 65 20 63 6c 6f 73 65 28 78 29 20 7b 20  fine close(x) { 
09e0: 69 66 20 28 73 74 72 63 6d 70 28 23 78 2c 20 22  if (strcmp(#x, "
09f0: 72 61 6e 64 6f 6d 5f 66 64 22 29 20 3d 3d 20 30  random_fd") == 0
0a00: 29 20 7b 20 63 6c 6f 73 65 28 78 29 3b 20 7d 20  ) { close(x); } 
0a10: 65 6c 73 65 20 7b 20 65 78 69 74 28 30 29 3b 20  else { exit(0); 
0a20: 7d 20 7d 0a 23 64 65 66 69 6e 65 20 66 63 6c 6f  } }.#define fclo
0a30: 73 65 28 78 29 20 65 78 69 74 28 30 29 0a 0a 73  se(x) exit(0)..s
0a40: 74 61 74 69 63 20 69 6e 74 20 61 66 6c 5f 61 63  tatic int afl_ac
0a50: 63 65 70 74 28 69 6e 74 20 78 2c 20 76 6f 69 64  cept(int x, void
0a60: 20 2a 61 64 64 72 2c 20 76 6f 69 64 20 2a 7a 29   *addr, void *z)
0a70: 20 7b 0a 09 28 28 73 74 72 75 63 74 20 73 6f 63   {..((struct soc
0a80: 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20 61 64 64  kaddr_in6 *) add
0a90: 72 29 2d 3e 73 69 6e 36 5f 66 61 6d 69 6c 79 20  r)->sin6_family 
0aa0: 3d 20 41 46 5f 49 4e 45 54 20 2b 20 41 46 5f 49  = AF_INET + AF_I
0ab0: 4e 45 54 36 20 2b 20 31 3b 0a 09 72 65 74 75 72  NET6 + 1;..retur
0ac0: 6e 28 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 3b  n(STDIN_FILENO);
0ad0: 0a 09 78 20 3d 20 78 3b 0a 09 7a 20 3d 20 7a 3b  ..x = x;..z = z;
0ae0: 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .}..static int a
0af0: 66 6c 5f 62 69 6e 64 28 69 6e 74 20 78 2c 20 76  fl_bind(int x, v
0b00: 6f 69 64 20 2a 79 2c 20 73 6f 63 6b 6c 65 6e 5f  oid *y, socklen_
0b10: 74 20 7a 29 20 7b 0a 09 72 65 74 75 72 6e 28 38  t z) {..return(8
0b20: 31 39 34 29 3b 0a 09 78 20 3d 20 78 3b 0a 09 79  194);..x = x;..y
0b30: 20 3d 20 79 3b 0a 09 7a 20 3d 20 7a 3b 0a 7d 0a   = y;..z = z;.}.
0b40: 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 6c 5f  .static int afl_
0b50: 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 70  pthread_create(p
0b60: 74 68 72 65 61 64 5f 74 20 2a 74 68 72 65 61 64  thread_t *thread
0b70: 2c 20 63 6f 6e 73 74 20 70 74 68 72 65 61 64 5f  , const pthread_
0b80: 61 74 74 72 5f 74 20 2a 61 74 74 72 2c 20 76 6f  attr_t *attr, vo
0b90: 69 64 20 2a 28 2a 73 74 61 72 74 5f 72 6f 75 74  id *(*start_rout
0ba0: 69 6e 65 29 20 28 76 6f 69 64 20 2a 29 2c 20 76  ine) (void *), v
0bb0: 6f 69 64 20 2a 61 72 67 29 20 7b 0a 09 73 74 61  oid *arg) {..sta
0bc0: 72 74 5f 72 6f 75 74 69 6e 65 28 61 72 67 29 3b  rt_routine(arg);
0bd0: 0a 09 65 78 69 74 28 33 29 3b 0a 09 74 68 72 65  ..exit(3);..thre
0be0: 61 64 20 3d 20 74 68 72 65 61 64 3b 0a 09 61 74  ad = thread;..at
0bf0: 74 72 20 3d 20 61 74 74 72 3b 0a 7d 0a 23 65 6e  tr = attr;.}.#en
0c00: 64 69 66 0a 0a 2f 2a 20 43 6f 6e 66 69 67 75 72  dif../* Configur
0c10: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68  ation options th
0c20: 61 74 20 77 6f 72 6b 20 74 68 72 65 61 64 73 20  at work threads 
0c30: 6e 65 65 64 20 74 6f 20 62 65 20 61 77 61 72 65  need to be aware
0c40: 20 6f 66 20 2a 2f 0a 73 74 72 75 63 74 20 66 69   of */.struct fi
0c50: 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 7b 0a 09 69  led_options {..i
0c60: 6e 74 20 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65  nt vhosts_enable
0c70: 64 3b 0a 7d 3b 0a 0a 2f 2a 20 41 72 67 75 6d 65  d;.};../* Argume
0c80: 6e 74 73 20 66 6f 72 20 77 6f 72 6b 65 72 20 74  nts for worker t
0c90: 68 72 65 61 64 73 20 2a 2f 0a 73 74 72 75 63 74  hreads */.struct
0ca0: 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68   filed_worker_th
0cb0: 72 65 61 64 5f 61 72 67 73 20 7b 0a 09 69 6e 74  read_args {..int
0cc0: 20 66 64 3b 0a 09 73 74 72 75 63 74 20 66 69 6c   fd;..struct fil
0cd0: 65 64 5f 6f 70 74 69 6f 6e 73 20 6f 70 74 69 6f  ed_options optio
0ce0: 6e 73 3b 0a 7d 3b 0a 0a 2f 2a 20 41 72 67 75 6d  ns;.};../* Argum
0cf0: 65 6e 74 73 20 66 6f 72 20 6c 6f 67 67 69 6e 67  ents for logging
0d00: 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 72 75   threads */.stru
0d10: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67  ct filed_logging
0d20: 5f 74 68 72 65 61 64 5f 61 72 67 73 20 7b 0a 09  _thread_args {..
0d30: 46 49 4c 45 20 2a 66 70 3b 0a 7d 3b 0a 0a 2f 2a  FILE *fp;.};../*
0d40: 20 46 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f   File informatio
0d50: 6e 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65  n */.struct file
0d60: 64 5f 66 69 6c 65 69 6e 66 6f 20 7b 0a 09 70 74  d_fileinfo {..pt
0d70: 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75  hread_mutex_t mu
0d80: 74 65 78 3b 0a 09 63 68 61 72 20 70 61 74 68 5b  tex;..char path[
0d90: 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45  FILED_PATH_BUFFE
0da0: 52 5f 53 49 5a 45 5d 3b 0a 09 69 6e 74 20 66 64  R_SIZE];..int fd
0db0: 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09 63  ;..off_t len;..c
0dc0: 68 61 72 20 2a 6c 61 73 74 6d 6f 64 3b 0a 09 63  har *lastmod;..c
0dd0: 68 61 72 20 6c 61 73 74 6d 6f 64 5f 62 5b 36 34  har lastmod_b[64
0de0: 5d 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a  ];..const char *
0df0: 74 79 70 65 3b 0a 09 63 68 61 72 20 65 74 61 67  type;..char etag
0e00: 5b 36 34 5d 3b 0a 7d 3b 0a 0a 2f 2a 20 52 65 71  [64];.};../* Req
0e10: 75 65 73 74 20 76 61 72 69 61 62 6c 65 73 20 2a  uest variables *
0e20: 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68  /.struct filed_h
0e30: 74 74 70 5f 72 65 71 75 65 73 74 20 7b 0a 09 2f  ttp_request {../
0e40: 2a 2a 20 42 75 66 66 65 72 73 20 2a 2a 2f 0a 09  ** Buffers **/..
0e50: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c  struct filed_fil
0e60: 65 69 6e 66 6f 20 66 69 6c 65 69 6e 66 6f 3b 0a  einfo fileinfo;.
0e70: 09 63 68 61 72 20 74 6d 70 62 75 66 5b 46 49 4c  .char tmpbuf[FIL
0e80: 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53  ED_PATH_BUFFER_S
0e90: 49 5a 45 5d 3b 0a 0a 09 2f 2a 2a 20 48 54 54 50  IZE];.../** HTTP
0ea0: 20 52 65 71 75 65 73 74 20 69 6e 66 6f 72 6d 61   Request informa
0eb0: 74 69 6f 6e 20 2a 2a 2f 0a 09 2f 2a 2a 2a 20 54  tion **/../*** T
0ec0: 79 70 65 20 6f 66 20 72 65 71 75 65 73 74 20 28  ype of request (
0ed0: 48 45 41 44 20 6f 72 20 47 45 54 29 20 2a 2a 2a  HEAD or GET) ***
0ee0: 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 4c 45  /..enum {...FILE
0ef0: 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44  D_REQUEST_METHOD
0f00: 5f 47 45 54 2c 0a 09 09 46 49 4c 45 44 5f 52 45  _GET,...FILED_RE
0f10: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45 41  QUEST_METHOD_HEA
0f20: 44 0a 09 7d 20 6d 65 74 68 6f 64 3b 0a 0a 09 2f  D..} method;.../
0f30: 2a 2a 2a 20 50 61 74 68 20 62 65 69 6e 67 20 72  *** Path being r
0f40: 65 71 75 65 73 74 65 64 20 2a 2a 2a 2f 0a 09 63  equested ***/..c
0f50: 68 61 72 20 70 61 74 68 5b 46 49 4c 45 44 5f 50  har path[FILED_P
0f60: 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d  ATH_BUFFER_SIZE]
0f70: 3b 20 0a 0a 09 2f 2a 2a 2a 20 50 61 74 68 20 74  ; .../*** Path t
0f80: 79 70 65 20 2a 2a 2a 2f 0a 09 65 6e 75 6d 20 7b  ype ***/..enum {
0f90: 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45 53 54  ...FILED_REQUEST
0fa0: 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 59 2c  _TYPE_DIRECTORY,
0fb0: 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45 53 54  ...FILED_REQUEST
0fc0: 5f 54 59 50 45 5f 4f 54 48 45 52 0a 09 7d 20 74  _TYPE_OTHER..} t
0fd0: 79 70 65 3b 0a 0a 09 73 74 72 75 63 74 20 7b 0a  ype;...struct {.
0fe0: 09 09 73 74 72 75 63 74 20 7b 0a 09 09 09 69 6e  ..struct {....in
0ff0: 74 20 70 72 65 73 65 6e 74 3b 0a 09 09 09 6f 66  t present;....of
1000: 66 5f 74 20 6f 66 66 73 65 74 3b 20 20 20 2f 2a  f_t offset;   /*
1010: 2a 2a 20 52 61 6e 67 65 20 73 74 61 72 74 20 2a  ** Range start *
1020: 2a 2a 2f 0a 09 09 09 6f 66 66 5f 74 20 6c 65 6e  **/....off_t len
1030: 67 74 68 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67  gth;   /*** Rang
1040: 65 20 6c 65 6e 67 74 68 20 2a 2a 2a 2f 0a 09 09  e length ***/...
1050: 7d 20 72 61 6e 67 65 3b 0a 0a 09 09 73 74 72 75  } range;....stru
1060: 63 74 20 7b 0a 09 09 09 69 6e 74 20 70 72 65 73  ct {....int pres
1070: 65 6e 74 3b 0a 09 09 09 63 68 61 72 20 68 6f 73  ent;....char hos
1080: 74 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46  t[FILED_PATH_BUF
1090: 46 45 52 5f 53 49 5a 45 5d 3b 0a 09 09 7d 20 68  FER_SIZE];...} h
10a0: 6f 73 74 3b 0a 0a 09 09 65 6e 75 6d 20 7b 0a 09  ost;....enum {..
10b0: 09 09 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49  ..FILED_CONNECTI
10c0: 4f 4e 5f 43 4c 4f 53 45 2c 0a 09 09 09 46 49 4c  ON_CLOSE,....FIL
10d0: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45  ED_CONNECTION_KE
10e0: 45 50 5f 41 4c 49 56 45 0a 09 09 7d 20 63 6f 6e  EP_ALIVE...} con
10f0: 6e 65 63 74 69 6f 6e 3b 0a 09 7d 20 68 65 61 64  nection;..} head
1100: 65 72 73 3b 0a 7d 3b 0a 0a 2f 2a 20 4c 6f 67 20  ers;.};../* Log 
1110: 72 65 63 6f 72 64 20 2a 2f 0a 73 74 72 75 63 74  record */.struct
1120: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79   filed_log_entry
1130: 20 7b 0a 09 2f 2a 20 54 79 70 65 20 6f 66 20 6c   {../* Type of l
1140: 6f 67 20 65 6e 74 72 79 20 2a 2f 0a 09 65 6e 75  og entry */..enu
1150: 6d 20 7b 0a 09 09 46 49 4c 45 44 5f 4c 4f 47 5f  m {...FILED_LOG_
1160: 54 59 50 45 5f 4d 45 53 53 41 47 45 2c 0a 09 09  TYPE_MESSAGE,...
1170: 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54  FILED_LOG_TYPE_T
1180: 52 41 4e 53 46 45 52 0a 09 7d 20 74 79 70 65 3b  RANSFER..} type;
1190: 0a 0a 09 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73  .../* Linked lis
11a0: 74 20 68 65 61 64 2f 74 61 69 6c 20 2a 2f 0a 09  t head/tail */..
11b0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
11c0: 5f 65 6e 74 72 79 20 2a 5f 6e 65 78 74 3b 0a 09  _entry *_next;..
11d0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
11e0: 5f 65 6e 74 72 79 20 2a 5f 70 72 65 76 3b 0a 0a  _entry *_prev;..
11f0: 09 2f 2a 20 54 68 72 65 61 64 20 66 72 6f 6d 20  ./* Thread from 
1200: 77 68 69 63 68 20 74 68 69 73 20 6c 6f 67 20 65  which this log e
1210: 6e 74 72 79 20 65 6d 69 6e 61 74 65 73 20 2a 2f  ntry eminates */
1220: 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65  ..pthread_t thre
1230: 61 64 3b 0a 0a 09 2f 2a 20 4d 65 73 73 61 67 65  ad;.../* Message
1240: 20 62 75 66 66 65 72 20 66 6f 72 20 74 79 70 65   buffer for type
1250: 20 3d 20 4d 45 53 53 41 47 45 20 2a 2f 0a 09 2f   = MESSAGE */../
1260: 2a 20 50 61 74 68 20 62 75 66 66 65 72 20 66 6f  * Path buffer fo
1270: 72 20 74 79 70 65 20 3d 20 54 52 41 4e 53 46 45  r type = TRANSFE
1280: 52 20 2a 2f 0a 09 63 68 61 72 20 62 75 66 66 65  R */..char buffe
1290: 72 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46  r[FILED_PATH_BUF
12a0: 46 45 52 5f 53 49 5a 45 5d 3b 0a 0a 09 2f 2a 20  FER_SIZE];.../* 
12b0: 49 74 65 6d 73 20 66 6f 72 20 74 79 70 65 20 3d  Items for type =
12c0: 20 54 52 41 4e 53 46 45 52 20 2a 2f 0a 09 69 6e   TRANSFER */..in
12d0: 74 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 63 6f  t http_code;..co
12e0: 6e 73 74 20 63 68 61 72 20 2a 72 65 61 73 6f 6e  nst char *reason
12f0: 3b 0a 09 74 69 6d 65 5f 74 20 73 74 61 72 74 74  ;..time_t startt
1300: 69 6d 65 3b 0a 09 74 69 6d 65 5f 74 20 65 6e 64  ime;..time_t end
1310: 74 69 6d 65 3b 0a 09 6f 66 66 5f 74 20 72 65 71  time;..off_t req
1320: 5f 6f 66 66 73 65 74 3b 0a 09 6f 66 66 5f 74 20  _offset;..off_t 
1330: 72 65 71 5f 6c 65 6e 67 74 68 3b 0a 09 6f 66 66  req_length;..off
1340: 5f 74 20 73 65 6e 74 5f 6c 65 6e 67 74 68 3b 0a  _t sent_length;.
1350: 09 6f 66 66 5f 74 20 66 69 6c 65 5f 6c 65 6e 67  .off_t file_leng
1360: 74 68 3b 0a 09 63 68 61 72 20 69 70 5b 31 32 38  th;..char ip[128
1370: 5d 3b 0a 09 69 6e 74 20 70 6f 72 74 3b 0a 09 69  ];..int port;..i
1380: 6e 74 20 6d 65 74 68 6f 64 3b 0a 7d 3b 0a 0a 2f  nt method;.};../
1390: 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  * Global variabl
13a0: 65 73 20 2a 2f 0a 2f 2a 2a 20 4f 70 65 6e 20 46  es */./** Open F
13b0: 69 6c 65 20 63 61 63 68 65 20 2a 2a 2f 0a 73 74  ile cache **/.st
13c0: 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69  ruct filed_filei
13d0: 6e 66 6f 20 2a 66 69 6c 65 64 5f 66 69 6c 65 69  nfo *filed_filei
13e0: 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d 20 4e 55  nfo_fdcache = NU
13f0: 4c 4c 3b 0a 75 6e 73 69 67 6e 65 64 20 69 6e 74  LL;.unsigned int
1400: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f   filed_fileinfo_
1410: 66 64 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 30  fdcache_size = 0
1420: 3b 0a 0a 2f 2a 2a 20 4c 6f 67 67 69 6e 67 20 2a  ;../** Logging *
1430: 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f  */.struct filed_
1440: 6c 6f 67 5f 65 6e 74 72 79 20 2a 66 69 6c 65 64  log_entry *filed
1450: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a 70  _log_msg_list;.p
1460: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 66  thread_mutex_t f
1470: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73  iled_log_msg_lis
1480: 74 5f 6d 75 74 65 78 3b 0a 70 74 68 72 65 61 64  t_mutex;.pthread
1490: 5f 63 6f 6e 64 5f 74 20 66 69 6c 65 64 5f 6c 6f  _cond_t filed_lo
14a0: 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 79  g_msg_list_ready
14b0: 3b 0a 0a 2f 2a 20 53 69 67 6e 61 6c 20 48 61 6e  ;../* Signal Han
14c0: 64 6c 65 72 20 2a 2f 0a 73 74 61 74 69 63 20 76  dler */.static v
14d0: 6f 69 64 20 66 69 6c 65 64 5f 73 69 67 6e 61 6c  oid filed_signal
14e0: 5f 68 61 6e 64 6c 65 72 28 69 6e 74 20 73 69 67  _handler(int sig
14f0: 6e 61 6c 5f 6e 75 6d 62 65 72 29 20 7b 0a 09 73  nal_number) {..s
1500: 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65  truct filed_file
1510: 69 6e 66 6f 20 2a 63 61 63 68 65 3b 0a 09 75 6e  info *cache;..un
1520: 73 69 67 6e 65 64 20 69 6e 74 20 69 64 78 3b 0a  signed int idx;.
1530: 0a 09 73 77 69 74 63 68 20 28 73 69 67 6e 61 6c  ..switch (signal
1540: 5f 6e 75 6d 62 65 72 29 20 7b 0a 09 09 63 61 73  _number) {...cas
1550: 65 20 53 49 47 48 55 50 3a 0a 09 09 09 66 6f 72  e SIGHUP:....for
1560: 20 28 69 64 78 20 3d 20 30 3b 20 69 64 78 20 3c   (idx = 0; idx <
1570: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f   filed_fileinfo_
1580: 66 64 63 61 63 68 65 5f 73 69 7a 65 3b 20 69 64  fdcache_size; id
1590: 78 2b 2b 29 20 7b 0a 09 09 09 09 63 61 63 68 65  x++) {.....cache
15a0: 20 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e   = &filed_filein
15b0: 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 3b  fo_fdcache[idx];
15c0: 0a 0a 09 09 09 09 70 74 68 72 65 61 64 5f 6d 75  ......pthread_mu
15d0: 74 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 65 2d  tex_lock(&cache-
15e0: 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09 09 63 61  >mutex);......ca
15f0: 63 68 65 2d 3e 70 61 74 68 5b 30 5d 20 3d 20 27  che->path[0] = '
1600: 5c 30 27 3b 0a 09 09 09 09 69 66 20 28 63 61 63  \0';.....if (cac
1610: 68 65 2d 3e 66 64 20 3e 3d 20 30 29 20 7b 0a 09  he->fd >= 0) {..
1620: 09 09 09 09 63 6c 6f 73 65 28 63 61 63 68 65 2d  ....close(cache-
1630: 3e 66 64 29 3b 0a 0a 09 09 09 09 09 63 61 63 68  >fd);.......cach
1640: 65 2d 3e 66 64 20 3d 20 2d 31 3b 0a 09 09 09 09  e->fd = -1;.....
1650: 7d 0a 0a 09 09 09 09 63 61 63 68 65 2d 3e 6c 61  }......cache->la
1660: 73 74 6d 6f 64 20 3d 20 22 22 3b 0a 09 09 09 09  stmod = "";.....
1670: 63 61 63 68 65 2d 3e 74 79 70 65 20 3d 20 22 22  cache->type = ""
1680: 3b 0a 0a 09 09 09 09 70 74 68 72 65 61 64 5f 6d  ;......pthread_m
1690: 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63  utex_unlock(&cac
16a0: 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 09 09 09 7d  he->mutex);....}
16b0: 0a 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a 0a 09  ....break;..}...
16c0: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 49 6e  return;.}../* In
16d0: 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 20 2a  itialize cache *
16e0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c  /.static int fil
16f0: 65 64 5f 69 6e 69 74 5f 63 61 63 68 65 28 75 6e  ed_init_cache(un
1700: 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63 68 65  signed int cache
1710: 5f 73 69 7a 65 29 20 7b 0a 09 75 6e 73 69 67 6e  _size) {..unsign
1720: 65 64 20 69 6e 74 20 69 64 78 3b 0a 09 69 6e 74  ed int idx;..int
1730: 20 6d 75 74 65 78 5f 69 6e 69 74 5f 72 65 74 3b   mutex_init_ret;
1740: 0a 0a 09 2f 2a 20 43 61 63 68 65 20 6d 61 79 20  .../* Cache may 
1750: 6e 6f 74 20 62 65 20 72 65 2d 69 6e 69 74 69 61  not be re-initia
1760: 6c 69 7a 65 64 20 2a 2f 0a 09 69 66 20 28 66 69  lized */..if (fi
1770: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
1780: 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 30 20 7c  ache_size != 0 |
1790: 7c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  | filed_fileinfo
17a0: 5f 66 64 63 61 63 68 65 20 21 3d 20 4e 55 4c 4c  _fdcache != NULL
17b0: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b  ) {...return(1);
17c0: 0a 09 7d 0a 0a 09 2f 2a 20 41 6c 6c 6f 63 61 74  ..}.../* Allocat
17d0: 65 20 63 61 63 68 65 20 2a 2f 0a 09 66 69 6c 65  e cache */..file
17e0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63  d_fileinfo_fdcac
17f0: 68 65 5f 73 69 7a 65 20 3d 20 63 61 63 68 65 5f  he_size = cache_
1800: 73 69 7a 65 3b 0a 09 66 69 6c 65 64 5f 66 69 6c  size;..filed_fil
1810: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d 20  einfo_fdcache = 
1820: 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 66  malloc(sizeof(*f
1830: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64  iled_fileinfo_fd
1840: 63 61 63 68 65 29 20 2a 20 66 69 6c 65 64 5f 66  cache) * filed_f
1850: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f  ileinfo_fdcache_
1860: 73 69 7a 65 29 3b 0a 09 69 66 20 28 66 69 6c 65  size);..if (file
1870: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63  d_fileinfo_fdcac
1880: 68 65 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09  he == NULL) {...
1890: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09  return(1);..}...
18a0: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 63 61  /* Initialize ca
18b0: 63 68 65 20 65 6e 74 72 69 65 73 20 2a 2f 0a 09  che entries */..
18c0: 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 69 64  for (idx = 0; id
18d0: 78 20 3c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e  x < filed_filein
18e0: 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 3b  fo_fdcache_size;
18f0: 20 69 64 78 2b 2b 29 20 7b 0a 09 09 6d 75 74 65   idx++) {...mute
1900: 78 5f 69 6e 69 74 5f 72 65 74 20 3d 20 70 74 68  x_init_ret = pth
1910: 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28  read_mutex_init(
1920: 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f  &filed_fileinfo_
1930: 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 6d 75 74  fdcache[idx].mut
1940: 65 78 2c 20 4e 55 4c 4c 29 3b 0a 09 09 69 66 20  ex, NULL);...if 
1950: 28 6d 75 74 65 78 5f 69 6e 69 74 5f 72 65 74 20  (mutex_init_ret 
1960: 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 74 75 72  != 0) {....retur
1970: 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 66 69 6c  n(1);...}....fil
1980: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61  ed_fileinfo_fdca
1990: 63 68 65 5b 69 64 78 5d 2e 70 61 74 68 5b 30 5d  che[idx].path[0]
19a0: 20 3d 20 27 5c 30 27 3b 0a 09 09 66 69 6c 65 64   = '\0';...filed
19b0: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68  _fileinfo_fdcach
19c0: 65 5b 69 64 78 5d 2e 66 64 20 3d 20 2d 31 3b 0a  e[idx].fd = -1;.
19d0: 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  ..filed_fileinfo
19e0: 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 6c 61  _fdcache[idx].la
19f0: 73 74 6d 6f 64 20 3d 20 22 22 3b 0a 09 09 66 69  stmod = "";...fi
1a00: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
1a10: 61 63 68 65 5b 69 64 78 5d 2e 74 79 70 65 20 3d  ache[idx].type =
1a20: 20 22 22 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e   "";..}...return
1a30: 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69  (0);.}../* Initi
1a40: 61 6c 69 7a 65 20 70 72 6f 63 65 73 73 20 2a 2f  alize process */
1a50: 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65  .static int file
1a60: 64 5f 69 6e 69 74 28 75 6e 73 69 67 6e 65 64 20  d_init(unsigned 
1a70: 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 29 20  int cache_size) 
1a80: 7b 0a 09 73 74 61 74 69 63 20 69 6e 74 20 63 61  {..static int ca
1a90: 6c 6c 65 64 20 3d 20 30 3b 0a 09 73 73 69 7a 65  lled = 0;..ssize
1aa0: 5f 74 20 72 65 61 64 5f 72 65 74 20 3d 20 30 3b  _t read_ret = 0;
1ab0: 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 72  ..unsigned int r
1ac0: 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 3d 20 30 3b  andom_value = 0;
1ad0: 0a 09 69 6e 74 20 63 61 63 68 65 5f 72 65 74 3b  ..int cache_ret;
1ae0: 0a 09 69 6e 74 20 72 61 6e 64 6f 6d 5f 66 64 3b  ..int random_fd;
1af0: 0a 0a 09 69 66 20 28 63 61 6c 6c 65 64 29 20 7b  ...if (called) {
1b00: 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d  ...return(0);..}
1b10: 0a 0a 09 63 61 6c 6c 65 64 20 3d 20 31 3b 0a 0a  ...called = 1;..
1b20: 09 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c  ./* Attempt to l
1b30: 6f 63 6b 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74  ock all memory t
1b40: 6f 20 70 68 79 73 69 63 61 6c 20 52 41 4d 20 28  o physical RAM (
1b50: 62 75 74 20 64 6f 6e 27 74 20 63 61 72 65 20 69  but don't care i
1b60: 66 20 77 65 20 63 61 6e 27 74 29 20 2a 2f 0a 09  f we can't) */..
1b70: 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 4c 5f 43 55 52  mlockall(MCL_CUR
1b80: 52 45 4e 54 20 7c 20 4d 43 4c 5f 46 55 54 55 52  RENT | MCL_FUTUR
1b90: 45 29 3b 0a 0a 09 2f 2a 20 49 67 6e 6f 72 65 20  E);.../* Ignore 
1ba0: 53 49 47 50 49 50 45 20 2a 2f 0a 09 73 69 67 6e  SIGPIPE */..sign
1bb0: 61 6c 28 53 49 47 50 49 50 45 2c 20 53 49 47 5f  al(SIGPIPE, SIG_
1bc0: 49 47 4e 29 3b 0a 0a 09 2f 2a 20 48 61 6e 64 6c  IGN);.../* Handl
1bd0: 65 20 53 49 47 48 55 50 20 74 6f 20 72 65 6c 65  e SIGHUP to rele
1be0: 61 73 65 20 61 6c 6c 20 63 61 63 68 65 73 20 2a  ase all caches *
1bf0: 2f 0a 09 73 69 67 6e 61 6c 28 53 49 47 48 55 50  /..signal(SIGHUP
1c00: 2c 20 66 69 6c 65 64 5f 73 69 67 6e 61 6c 5f 68  , filed_signal_h
1c10: 61 6e 64 6c 65 72 29 3b 0a 0a 09 2f 2a 20 49 6e  andler);.../* In
1c20: 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 20 73  itialize cache s
1c30: 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 63 61 63  tructure */..cac
1c40: 68 65 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 69  he_ret = filed_i
1c50: 6e 69 74 5f 63 61 63 68 65 28 63 61 63 68 65 5f  nit_cache(cache_
1c60: 73 69 7a 65 29 3b 0a 09 69 66 20 28 63 61 63 68  size);..if (cach
1c70: 65 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09  e_ret != 0) {...
1c80: 72 65 74 75 72 6e 28 63 61 63 68 65 5f 72 65 74  return(cache_ret
1c90: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69  );..}.../* Initi
1ca0: 61 6c 69 7a 65 20 72 61 6e 64 6f 6d 20 6e 75 6d  alize random num
1cb0: 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f  ber generator */
1cc0: 0a 09 72 61 6e 64 6f 6d 5f 66 64 20 3d 20 6f 70  ..random_fd = op
1cd0: 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d  en("/dev/urandom
1ce0: 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 09 69  ", O_RDONLY);..i
1cf0: 66 20 28 72 61 6e 64 6f 6d 5f 66 64 20 3e 3d 20  f (random_fd >= 
1d00: 30 29 20 7b 0a 09 09 72 65 61 64 5f 72 65 74 20  0) {...read_ret 
1d10: 3d 20 72 65 61 64 28 72 61 6e 64 6f 6d 5f 66 64  = read(random_fd
1d20: 2c 20 26 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 2c  , &random_value,
1d30: 20 73 69 7a 65 6f 66 28 72 61 6e 64 6f 6d 5f 76   sizeof(random_v
1d40: 61 6c 75 65 29 29 3b 0a 0a 09 09 63 6c 6f 73 65  alue));....close
1d50: 28 72 61 6e 64 6f 6d 5f 66 64 29 3b 0a 09 7d 0a  (random_fd);..}.
1d60: 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e  ..random_value ^
1d70: 3d 20 67 65 74 70 69 64 28 29 3b 0a 09 72 61 6e  = getpid();..ran
1d80: 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 67 65 74  dom_value ^= get
1d90: 75 69 64 28 29 3b 0a 09 72 61 6e 64 6f 6d 5f 76  uid();..random_v
1da0: 61 6c 75 65 20 5e 3d 20 74 69 6d 65 28 4e 55 4c  alue ^= time(NUL
1db0: 4c 29 3b 0a 0a 09 73 72 61 6e 64 6f 6d 28 72 61  L);...srandom(ra
1dc0: 6e 64 6f 6d 5f 76 61 6c 75 65 29 3b 0a 0a 09 72  ndom_value);...r
1dd0: 65 74 75 72 6e 28 30 29 3b 0a 0a 09 2f 2a 20 4e  eturn(0);.../* N
1de0: 4f 54 52 45 41 43 48 3a 20 52 65 61 64 20 6d 61  OTREACH: Read ma
1df0: 79 20 66 61 69 6c 20 6f 72 20 73 75 63 63 65 65  y fail or succee
1e00: 64 2c 20 77 65 20 64 6f 6e 27 74 20 61 63 74 75  d, we don't actu
1e10: 61 6c 6c 79 20 63 61 72 65 20 2a 2f 0a 09 72 65  ally care */..re
1e20: 61 64 5f 72 65 74 20 3d 20 72 65 61 64 5f 72 65  ad_ret = read_re
1e30: 74 3b 0a 7d 0a 0a 2f 2a 20 4c 69 73 74 65 6e 20  t;.}../* Listen 
1e40: 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  on a particular 
1e50: 61 64 64 72 65 73 73 2f 70 6f 72 74 20 2a 2f 0a  address/port */.
1e60: 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64  static int filed
1e70: 5f 6c 69 73 74 65 6e 28 63 6f 6e 73 74 20 63 68  _listen(const ch
1e80: 61 72 20 2a 61 64 64 72 65 73 73 2c 20 75 6e 73  ar *address, uns
1e90: 69 67 6e 65 64 20 69 6e 74 20 70 6f 72 74 29 20  igned int port) 
1ea0: 7b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64  {..struct sockad
1eb0: 64 72 5f 69 6e 36 20 61 64 64 72 5f 76 36 3b 0a  dr_in6 addr_v6;.
1ec0: 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72  .struct sockaddr
1ed0: 5f 69 6e 20 61 64 64 72 5f 76 34 3b 0a 09 73 74  _in addr_v4;..st
1ee0: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 61  ruct sockaddr *a
1ef0: 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20  ddr;..socklen_t 
1f00: 61 64 64 72 5f 6c 65 6e 3b 0a 09 69 6e 74 20 70  addr_len;..int p
1f10: 74 6f 6e 5f 72 65 74 2c 20 62 69 6e 64 5f 72 65  ton_ret, bind_re
1f20: 74 2c 20 6c 69 73 74 65 6e 5f 72 65 74 3b 0a 09  t, listen_ret;..
1f30: 69 6e 74 20 66 61 6d 69 6c 79 3b 0a 09 69 6e 74  int family;..int
1f40: 20 66 64 3b 0a 0a 09 66 61 6d 69 6c 79 20 3d 20   fd;...family = 
1f50: 41 46 5f 49 4e 45 54 36 3b 0a 09 70 74 6f 6e 5f  AF_INET6;..pton_
1f60: 72 65 74 20 3d 20 69 6e 65 74 5f 70 74 6f 6e 28  ret = inet_pton(
1f70: 66 61 6d 69 6c 79 2c 20 61 64 64 72 65 73 73 2c  family, address,
1f80: 20 26 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 61   &addr_v6.sin6_a
1f90: 64 64 72 2e 73 36 5f 61 64 64 72 29 3b 0a 09 69  ddr.s6_addr);..i
1fa0: 66 20 28 70 74 6f 6e 5f 72 65 74 20 21 3d 20 31  f (pton_ret != 1
1fb0: 29 20 7b 0a 09 09 66 61 6d 69 6c 79 20 3d 20 41  ) {...family = A
1fc0: 46 5f 49 4e 45 54 3b 0a 09 09 70 74 6f 6e 5f 72  F_INET;...pton_r
1fd0: 65 74 20 3d 20 69 6e 65 74 5f 70 74 6f 6e 28 66  et = inet_pton(f
1fe0: 61 6d 69 6c 79 2c 20 61 64 64 72 65 73 73 2c 20  amily, address, 
1ff0: 26 61 64 64 72 5f 76 34 2e 73 69 6e 5f 61 64 64  &addr_v4.sin_add
2000: 72 2e 73 5f 61 64 64 72 29 3b 0a 09 09 69 66 20  r.s_addr);...if 
2010: 28 70 74 6f 6e 5f 72 65 74 20 21 3d 20 31 29 20  (pton_ret != 1) 
2020: 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 31 29 3b  {....return(-1);
2030: 0a 09 09 7d 0a 0a 09 09 61 64 64 72 5f 76 34 2e  ...}....addr_v4.
2040: 73 69 6e 5f 66 61 6d 69 6c 79 20 3d 20 66 61 6d  sin_family = fam
2050: 69 6c 79 3b 0a 09 09 61 64 64 72 5f 76 34 2e 73  ily;...addr_v4.s
2060: 69 6e 5f 70 6f 72 74 20 3d 20 68 74 6f 6e 73 28  in_port = htons(
2070: 70 6f 72 74 29 3b 0a 0a 09 09 61 64 64 72 20 3d  port);....addr =
2080: 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64   (struct sockadd
2090: 72 20 2a 29 20 26 61 64 64 72 5f 76 34 3b 0a 09  r *) &addr_v4;..
20a0: 09 61 64 64 72 5f 6c 65 6e 20 3d 20 73 69 7a 65  .addr_len = size
20b0: 6f 66 28 61 64 64 72 5f 76 34 29 3b 0a 09 7d 20  of(addr_v4);..} 
20c0: 65 6c 73 65 20 7b 0a 09 09 61 64 64 72 5f 76 36  else {...addr_v6
20d0: 2e 73 69 6e 36 5f 66 61 6d 69 6c 79 20 3d 20 41  .sin6_family = A
20e0: 46 5f 49 4e 45 54 36 3b 0a 09 09 61 64 64 72 5f  F_INET6;...addr_
20f0: 76 36 2e 73 69 6e 36 5f 66 6c 6f 77 69 6e 66 6f  v6.sin6_flowinfo
2100: 20 3d 20 30 3b 0a 09 09 61 64 64 72 5f 76 36 2e   = 0;...addr_v6.
2110: 73 69 6e 36 5f 73 63 6f 70 65 5f 69 64 20 3d 20  sin6_scope_id = 
2120: 30 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e  0;...addr_v6.sin
2130: 36 5f 70 6f 72 74 20 3d 20 68 74 6f 6e 73 28 70  6_port = htons(p
2140: 6f 72 74 29 3b 0a 0a 09 09 61 64 64 72 20 3d 20  ort);....addr = 
2150: 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72  (struct sockaddr
2160: 20 2a 29 20 26 61 64 64 72 5f 76 36 3b 0a 09 09   *) &addr_v6;...
2170: 61 64 64 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f  addr_len = sizeo
2180: 66 28 61 64 64 72 5f 76 36 29 3b 0a 09 7d 0a 0a  f(addr_v6);..}..
2190: 09 66 64 20 3d 20 73 6f 63 6b 65 74 28 66 61 6d  .fd = socket(fam
21a0: 69 6c 79 2c 20 53 4f 43 4b 5f 53 54 52 45 41 4d  ily, SOCK_STREAM
21b0: 2c 20 30 29 3b 0a 09 69 66 20 28 66 64 20 3c 20  , 0);..if (fd < 
21c0: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 66 64  0) {...return(fd
21d0: 29 3b 0a 09 7d 0a 0a 09 62 69 6e 64 5f 72 65 74  );..}...bind_ret
21e0: 20 3d 20 62 69 6e 64 28 66 64 2c 20 61 64 64 72   = bind(fd, addr
21f0: 2c 20 61 64 64 72 5f 6c 65 6e 29 3b 0a 09 69 66  , addr_len);..if
2200: 20 28 62 69 6e 64 5f 72 65 74 20 3c 20 30 29 20   (bind_ret < 0) 
2210: 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a  {...close(fd);..
2220: 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d  ..return(-1);..}
2230: 0a 0a 09 6c 69 73 74 65 6e 5f 72 65 74 20 3d 20  ...listen_ret = 
2240: 6c 69 73 74 65 6e 28 66 64 2c 20 31 32 38 29 3b  listen(fd, 128);
2250: 0a 09 69 66 20 28 6c 69 73 74 65 6e 5f 72 65 74  ..if (listen_ret
2260: 20 21 3d 20 30 29 20 7b 0a 09 09 63 6c 6f 73 65   != 0) {...close
2270: 28 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28  (fd);....return(
2280: 2d 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e  -1);..}...return
2290: 28 66 64 29 3b 0a 7d 0a 0a 2f 2a 20 4c 6f 67 20  (fd);.}../* Log 
22a0: 61 20 6d 65 73 73 61 67 65 20 2a 2f 0a 23 69 66  a message */.#if
22b0: 64 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c  def FILED_DONT_L
22c0: 4f 47 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c  OG.#  define fil
22d0: 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61  ed_logging_threa
22e0: 64 5f 69 6e 69 74 28 78 29 20 30 0a 23 20 20 64  d_init(x) 0.#  d
22f0: 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f  efine filed_log_
2300: 6d 73 67 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e  msg_debug(x, ...
2310: 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65  ) /**/.#  define
2320: 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 78   filed_log_msg(x
2330: 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 64  , ...) /**/.#  d
2340: 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f  efine filed_log_
2350: 65 6e 74 72 79 28 78 29 20 2f 2a 2a 2f 0a 23 20  entry(x) /**/.# 
2360: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f   define filed_lo
2370: 67 5f 69 70 28 78 2c 20 2e 2e 2e 29 20 4e 55 4c  g_ip(x, ...) NUL
2380: 4c 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65  L.#  define file
2390: 64 5f 6c 6f 67 5f 6e 65 77 28 78 29 20 26 6c 6f  d_log_new(x) &lo
23a0: 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 0a 23 20  cal_dummy_log.# 
23b0: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f   define filed_lo
23c0: 67 5f 66 72 65 65 28 78 29 20 2f 2a 2a 2f 0a 0a  g_free(x) /**/..
23d0: 2f 2a 20 52 65 74 75 72 6e 20 6c 6f 67 67 69 6e  /* Return loggin
23e0: 67 20 68 61 6e 64 6c 65 20 2a 2f 0a 73 74 61 74  g handle */.stat
23f0: 69 63 20 46 49 4c 45 20 2a 66 69 6c 65 64 5f 6c  ic FILE *filed_l
2400: 6f 67 5f 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68  og_open(const ch
2410: 61 72 20 2a 66 69 6c 65 29 20 7b 0a 09 72 65 74  ar *file) {..ret
2420: 75 72 6e 28 73 74 64 6f 75 74 29 3b 0a 09 66 69  urn(stdout);..fi
2430: 6c 65 20 3d 20 66 69 6c 65 3b 0a 7d 0a 23 65 6c  le = file;.}.#el
2440: 73 65 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c  se.#  define fil
2450: 65 64 5f 6c 6f 67 5f 66 72 65 65 28 78 29 20 66  ed_log_free(x) f
2460: 72 65 65 28 78 29 0a 23 20 20 69 66 64 65 66 20  ree(x).#  ifdef 
2470: 46 49 4c 45 44 5f 44 45 42 55 47 0a 23 20 20 20  FILED_DEBUG.#   
2480: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f   define filed_lo
2490: 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c 20 2e  g_msg_debug(x, .
24a0: 2e 2e 29 20 7b 20 66 70 72 69 6e 74 66 28 73 74  ..) { fprintf(st
24b0: 64 65 72 72 2c 20 78 2c 20 5f 5f 56 41 5f 41 52  derr, x, __VA_AR
24c0: 47 53 5f 5f 29 3b 20 66 70 72 69 6e 74 66 28 73  GS__); fprintf(s
24d0: 74 64 65 72 72 2c 20 22 5c 6e 22 29 3b 20 66 66  tderr, "\n"); ff
24e0: 6c 75 73 68 28 73 74 64 65 72 72 29 3b 20 7d 0a  lush(stderr); }.
24f0: 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66  #  else.#    def
2500: 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  ine filed_log_ms
2510: 67 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20  g_debug(x, ...) 
2520: 2f 2a 2a 2f 0a 23 20 20 65 6e 64 69 66 0a 0a 2f  /**/.#  endif../
2530: 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6c 6f 67  * Initialize log
2540: 67 69 6e 67 20 74 68 72 65 61 64 20 2a 2f 0a 73  ging thread */.s
2550: 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 6c 65  tatic void *file
2560: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64  d_logging_thread
2570: 28 76 6f 69 64 20 2a 61 72 67 5f 70 29 20 7b 0a  (void *arg_p) {.
2580: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f  .struct filed_lo
2590: 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 72 67  gging_thread_arg
25a0: 73 20 2a 61 72 67 3b 0a 09 73 74 72 75 63 74 20  s *arg;..struct 
25b0: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20  filed_log_entry 
25c0: 2a 63 75 72 72 2c 20 2a 70 72 65 76 3b 0a 09 63  *curr, *prev;..c
25d0: 6f 6e 73 74 20 63 68 61 72 20 2a 6d 65 74 68 6f  onst char *metho
25e0: 64 3b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 3b 0a  d;..time_t now;.
25f0: 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 61 72 67  .FILE *fp;...arg
2600: 20 3d 20 61 72 67 5f 70 3b 0a 0a 09 66 70 20 3d   = arg_p;...fp =
2610: 20 61 72 67 2d 3e 66 70 3b 0a 0a 09 77 68 69 6c   arg->fp;...whil
2620: 65 20 28 31 29 20 7b 0a 09 09 70 74 68 72 65 61  e (1) {...pthrea
2630: 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 66 69  d_mutex_lock(&fi
2640: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74  led_log_msg_list
2650: 5f 6d 75 74 65 78 29 3b 0a 09 09 70 74 68 72 65  _mutex);...pthre
2660: 61 64 5f 63 6f 6e 64 5f 77 61 69 74 28 26 66 69  ad_cond_wait(&fi
2670: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74  led_log_msg_list
2680: 5f 72 65 61 64 79 2c 20 26 66 69 6c 65 64 5f 6c  _ready, &filed_l
2690: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65  og_msg_list_mute
26a0: 78 29 3b 0a 0a 09 09 63 75 72 72 20 3d 20 66 69  x);....curr = fi
26b0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74  led_log_msg_list
26c0: 3b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  ;...filed_log_ms
26d0: 67 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a 0a  g_list = NULL;..
26e0: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  ..pthread_mutex_
26f0: 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f  unlock(&filed_lo
2700: 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78  g_msg_list_mutex
2710: 29 3b 0a 0a 09 09 6e 6f 77 20 3d 20 74 69 6d 65  );....now = time
2720: 28 4e 55 4c 4c 29 3b 0a 0a 09 09 70 72 65 76 20  (NULL);....prev 
2730: 3d 20 4e 55 4c 4c 3b 0a 09 09 66 6f 72 20 28 3b  = NULL;...for (;
2740: 20 63 75 72 72 3b 20 63 75 72 72 20 3d 20 63 75   curr; curr = cu
2750: 72 72 2d 3e 5f 6e 65 78 74 29 20 7b 0a 09 09 09  rr->_next) {....
2760: 63 75 72 72 2d 3e 5f 70 72 65 76 20 3d 20 70 72  curr->_prev = pr
2770: 65 76 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20 63  ev;.....prev = c
2780: 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 63 75 72 72  urr;...}....curr
2790: 20 3d 20 70 72 65 76 3b 0a 09 09 77 68 69 6c 65   = prev;...while
27a0: 20 28 63 75 72 72 29 20 7b 0a 09 09 09 73 77 69   (curr) {....swi
27b0: 74 63 68 20 28 63 75 72 72 2d 3e 74 79 70 65 29  tch (curr->type)
27c0: 20 7b 0a 09 09 09 09 63 61 73 65 20 46 49 4c 45   {.....case FILE
27d0: 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 53 41  D_LOG_TYPE_MESSA
27e0: 47 45 3a 0a 09 09 09 09 09 66 70 72 69 6e 74 66  GE:......fprintf
27f0: 28 66 70 2c 20 22 25 73 22 2c 20 63 75 72 72 2d  (fp, "%s", curr-
2800: 3e 62 75 66 66 65 72 29 3b 0a 0a 09 09 09 09 09  >buffer);.......
2810: 62 72 65 61 6b 3b 0a 09 09 09 09 63 61 73 65 20  break;.....case 
2820: 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54  FILED_LOG_TYPE_T
2830: 52 41 4e 53 46 45 52 3a 0a 09 09 09 09 09 73 77  RANSFER:......sw
2840: 69 74 63 68 20 28 63 75 72 72 2d 3e 6d 65 74 68  itch (curr->meth
2850: 6f 64 29 20 7b 0a 09 09 09 09 09 09 63 61 73 65  od) {.......case
2860: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d   FILED_REQUEST_M
2870: 45 54 48 4f 44 5f 47 45 54 3a 0a 09 09 09 09 09  ETHOD_GET:......
2880: 09 09 6d 65 74 68 6f 64 3d 22 47 45 54 22 3b 0a  ..method="GET";.
2890: 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09  .......break;...
28a0: 09 09 09 09 63 61 73 65 20 46 49 4c 45 44 5f 52  ....case FILED_R
28b0: 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45  EQUEST_METHOD_HE
28c0: 41 44 3a 0a 09 09 09 09 09 09 09 6d 65 74 68 6f  AD:........metho
28d0: 64 3d 22 48 45 41 44 22 3b 0a 09 09 09 09 09 09  d="HEAD";.......
28e0: 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 64 65  .break;.......de
28f0: 66 61 75 6c 74 3a 0a 09 09 09 09 09 09 09 6d 65  fault:........me
2900: 74 68 6f 64 3d 22 3c 75 6e 6b 6e 6f 77 6e 3e 22  thod="<unknown>"
2910: 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a  ;........break;.
2920: 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 69 66 20  .....}.......if 
2930: 28 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 20 3d  (curr->endtime =
2940: 3d 20 28 28 74 69 6d 65 5f 74 29 20 2d 31 29 29  = ((time_t) -1))
2950: 20 7b 0a 09 09 09 09 09 09 63 75 72 72 2d 3e 65   {.......curr->e
2960: 6e 64 74 69 6d 65 20 3d 20 6e 6f 77 3b 0a 09 09  ndtime = now;...
2970: 09 09 09 7d 0a 0a 09 09 09 09 09 66 70 72 69 6e  ...}.......fprin
2980: 74 66 28 66 70 2c 20 22 54 52 41 4e 53 46 45 52  tf(fp, "TRANSFER
2990: 20 4d 45 54 48 4f 44 3d 25 73 20 50 41 54 48 3d   METHOD=%s PATH=
29a0: 25 73 20 53 52 43 3d 25 73 3a 25 69 20 54 49 4d  %s SRC=%s:%i TIM
29b0: 45 2e 53 54 41 52 54 3d 25 6c 6c 75 20 54 49 4d  E.START=%llu TIM
29c0: 45 2e 45 4e 44 3d 25 6c 6c 75 20 43 4f 44 45 2e  E.END=%llu CODE.
29d0: 56 41 4c 55 45 3d 25 75 20 43 4f 44 45 2e 52 45  VALUE=%u CODE.RE
29e0: 41 53 4f 4e 3d 25 73 20 52 45 51 55 45 53 54 2e  ASON=%s REQUEST.
29f0: 4f 46 46 53 45 54 3d 25 6c 6c 75 20 52 45 51 55  OFFSET=%llu REQU
2a00: 45 53 54 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 20  EST.LENGTH=%llu 
2a10: 46 49 4c 45 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75  FILE.LENGTH=%llu
2a20: 20 54 52 41 4e 53 46 45 52 2e 4c 45 4e 47 54 48   TRANSFER.LENGTH
2a30: 3d 25 6c 6c 75 22 2c 0a 09 09 09 09 09 09 6d 65  =%llu",.......me
2a40: 74 68 6f 64 2c 0a 09 09 09 09 09 09 63 75 72 72  thod,.......curr
2a50: 2d 3e 62 75 66 66 65 72 2c 0a 09 09 09 09 09 09  ->buffer,.......
2a60: 63 75 72 72 2d 3e 69 70 2c 20 63 75 72 72 2d 3e  curr->ip, curr->
2a70: 70 6f 72 74 2c 0a 09 09 09 09 09 09 28 75 6e 73  port,.......(uns
2a80: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
2a90: 20 63 75 72 72 2d 3e 73 74 61 72 74 74 69 6d 65   curr->starttime
2aa0: 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65  ,.......(unsigne
2ab0: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72  d long long) cur
2ac0: 72 2d 3e 65 6e 64 74 69 6d 65 2c 0a 09 09 09 09  r->endtime,.....
2ad0: 09 09 63 75 72 72 2d 3e 68 74 74 70 5f 63 6f 64  ..curr->http_cod
2ae0: 65 2c 20 63 75 72 72 2d 3e 72 65 61 73 6f 6e 2c  e, curr->reason,
2af0: 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64  .......(unsigned
2b00: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72   long long) curr
2b10: 2d 3e 72 65 71 5f 6f 66 66 73 65 74 2c 0a 09 09  ->req_offset,...
2b20: 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f  ....(unsigned lo
2b30: 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 72  ng long) curr->r
2b40: 65 71 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09  eq_length,......
2b50: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
2b60: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 66 69 6c 65  long) curr->file
2b70: 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09 09 28  _length,.......(
2b80: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
2b90: 6e 67 29 20 63 75 72 72 2d 3e 73 65 6e 74 5f 6c  ng) curr->sent_l
2ba0: 65 6e 67 74 68 0a 09 09 09 09 09 29 3b 0a 0a 09  ength......);...
2bb0: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 7d 0a  ....break;....}.
2bc0: 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22  ...fprintf(fp, "
2bd0: 20 54 48 52 45 41 44 3d 25 6c 6c 75 20 54 49 4d   THREAD=%llu TIM
2be0: 45 3d 25 6c 6c 75 5c 6e 22 2c 0a 09 09 09 09 28  E=%llu\n",.....(
2bf0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
2c00: 6e 67 29 20 63 75 72 72 2d 3e 74 68 72 65 61 64  ng) curr->thread
2c10: 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ,.....(unsigned 
2c20: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 6e 6f 77 0a 09  long long) now..
2c30: 09 09 29 3b 0a 09 09 09 66 66 6c 75 73 68 28 66  ..);....fflush(f
2c40: 70 29 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20 63  p);.....prev = c
2c50: 75 72 72 3b 0a 09 09 09 63 75 72 72 20 3d 20 63  urr;....curr = c
2c60: 75 72 72 2d 3e 5f 70 72 65 76 3b 0a 0a 09 09 09  urr->_prev;.....
2c70: 66 72 65 65 28 70 72 65 76 29 3b 0a 09 09 7d 0a  free(prev);...}.
2c80: 09 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c  .}...return(NULL
2c90: 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
2ca0: 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72  d filed_log_entr
2cb0: 79 28 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c  y(struct filed_l
2cc0: 6f 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 29  og_entry *entry)
2cd0: 20 7b 0a 09 65 6e 74 72 79 2d 3e 74 68 72 65 61   {..entry->threa
2ce0: 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66  d = pthread_self
2cf0: 28 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75  ();...pthread_mu
2d00: 74 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f  tex_lock(&filed_
2d10: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74  log_msg_list_mut
2d20: 65 78 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 5f 6e  ex);...entry->_n
2d30: 65 78 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f  ext = filed_log_
2d40: 6d 73 67 5f 6c 69 73 74 3b 0a 09 66 69 6c 65 64  msg_list;..filed
2d50: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20  _log_msg_list = 
2d60: 65 6e 74 72 79 3b 0a 0a 09 70 74 68 72 65 61 64  entry;...pthread
2d70: 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66  _mutex_unlock(&f
2d80: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73  iled_log_msg_lis
2d90: 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 70 74 68 72  t_mutex);...pthr
2da0: 65 61 64 5f 63 6f 6e 64 5f 73 69 67 6e 61 6c 28  ead_cond_signal(
2db0: 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c  &filed_log_msg_l
2dc0: 69 73 74 5f 72 65 61 64 79 29 3b 0a 0a 09 72 65  ist_ready);...re
2dd0: 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  turn;.}..static 
2de0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
2df0: 5f 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c 6f  _entry *filed_lo
2e00: 67 5f 6e 65 77 28 69 6e 74 20 69 6e 69 74 69 61  g_new(int initia
2e10: 6c 69 7a 65 29 20 7b 0a 09 73 74 72 75 63 74 20  lize) {..struct 
2e20: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20  filed_log_entry 
2e30: 2a 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61  *retval;...retva
2e40: 6c 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  l = malloc(sizeo
2e50: 66 28 2a 72 65 74 76 61 6c 29 29 3b 0a 0a 09 69  f(*retval));...i
2e60: 66 20 28 69 6e 69 74 69 61 6c 69 7a 65 29 20 7b  f (initialize) {
2e70: 0a 09 09 72 65 74 76 61 6c 2d 3e 62 75 66 66 65  ...retval->buffe
2e80: 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 72  r[0] = '\0';...r
2e90: 65 74 76 61 6c 2d 3e 68 74 74 70 5f 63 6f 64 65  etval->http_code
2ea0: 20 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c 2d   = -1;...retval-
2eb0: 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 30 3b 0a  >starttime = 0;.
2ec0: 09 09 72 65 74 76 61 6c 2d 3e 65 6e 64 74 69 6d  ..retval->endtim
2ed0: 65 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d  e = 0;...retval-
2ee0: 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 30 3b  >req_offset = 0;
2ef0: 0a 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f 6c  ...retval->req_l
2f00: 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 74  ength = 0;...ret
2f10: 76 61 6c 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68  val->sent_length
2f20: 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e   = 0;...retval->
2f30: 66 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 30 3b  file_length = 0;
2f40: 0a 09 09 72 65 74 76 61 6c 2d 3e 69 70 5b 30 5d  ...retval->ip[0]
2f50: 20 3d 20 27 5c 30 27 3b 0a 09 09 72 65 74 76 61   = '\0';...retva
2f60: 6c 2d 3e 70 6f 72 74 20 3d 20 2d 31 3b 0a 09 09  l->port = -1;...
2f70: 72 65 74 76 61 6c 2d 3e 6d 65 74 68 6f 64 20 3d  retval->method =
2f80: 20 2d 31 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e   -1;..}...return
2f90: 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61  (retval);.}..sta
2fa0: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 6c  tic void filed_l
2fb0: 6f 67 5f 6d 73 67 28 63 6f 6e 73 74 20 63 68 61  og_msg(const cha
2fc0: 72 20 2a 66 6d 74 2c 20 2e 2e 2e 29 20 7b 0a 09  r *fmt, ...) {..
2fd0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
2fe0: 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 3b 0a 09  _entry *entry;..
2ff0: 76 61 5f 6c 69 73 74 20 61 72 67 73 3b 0a 0a 09  va_list args;...
3000: 65 6e 74 72 79 20 3d 20 66 69 6c 65 64 5f 6c 6f  entry = filed_lo
3010: 67 5f 6e 65 77 28 30 29 3b 0a 0a 09 76 61 5f 73  g_new(0);...va_s
3020: 74 61 72 74 28 61 72 67 73 2c 20 66 6d 74 29 3b  tart(args, fmt);
3030: 0a 0a 09 76 73 6e 70 72 69 6e 74 66 28 65 6e 74  ...vsnprintf(ent
3040: 72 79 2d 3e 62 75 66 66 65 72 2c 20 73 69 7a 65  ry->buffer, size
3050: 6f 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 65 72  of(entry->buffer
3060: 29 2c 20 66 6d 74 2c 20 61 72 67 73 29 3b 0a 0a  ), fmt, args);..
3070: 09 76 61 5f 65 6e 64 28 61 72 67 73 29 3b 0a 0a  .va_end(args);..
3080: 09 65 6e 74 72 79 2d 3e 74 79 70 65 20 3d 20 46  .entry->type = F
3090: 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45  ILED_LOG_TYPE_ME
30a0: 53 53 41 47 45 3b 0a 0a 09 66 69 6c 65 64 5f 6c  SSAGE;...filed_l
30b0: 6f 67 5f 65 6e 74 72 79 28 65 6e 74 72 79 29 3b  og_entry(entry);
30c0: 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74  ...return;.}..st
30d0: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
30e0: 2a 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 73 74  *filed_log_ip(st
30f0: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 61  ruct sockaddr *a
3100: 64 64 72 2c 20 63 68 61 72 20 2a 62 75 66 66 65  ddr, char *buffe
3110: 72 2c 20 73 69 7a 65 5f 74 20 62 75 66 66 65 72  r, size_t buffer
3120: 6c 65 6e 29 20 7b 0a 09 73 74 72 75 63 74 20 73  len) {..struct s
3130: 6f 63 6b 61 64 64 72 5f 69 6e 20 2a 61 64 64 72  ockaddr_in *addr
3140: 5f 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f 63  _v4;..struct soc
3150: 6b 61 64 64 72 5f 69 6e 36 20 2a 61 64 64 72 5f  kaddr_in6 *addr_
3160: 76 36 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20  v6;..const char 
3170: 2a 72 65 74 76 61 6c 20 3d 20 4e 55 4c 4c 3b 0a  *retval = NULL;.
3180: 0a 09 61 64 64 72 5f 76 36 20 3d 20 28 73 74 72  ..addr_v6 = (str
3190: 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36  uct sockaddr_in6
31a0: 20 2a 29 20 61 64 64 72 3b 0a 0a 09 73 77 69 74   *) addr;...swit
31b0: 63 68 20 28 61 64 64 72 5f 76 36 2d 3e 73 69 6e  ch (addr_v6->sin
31c0: 36 5f 66 61 6d 69 6c 79 29 20 7b 0a 09 09 63 61  6_family) {...ca
31d0: 73 65 20 41 46 5f 49 4e 45 54 3a 0a 09 09 09 61  se AF_INET:....a
31e0: 64 64 72 5f 76 34 20 3d 20 28 73 74 72 75 63 74  ddr_v4 = (struct
31f0: 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 2a 29 20   sockaddr_in *) 
3200: 61 64 64 72 3b 0a 09 09 09 72 65 74 76 61 6c 20  addr;....retval 
3210: 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 41 46 5f 49  = inet_ntop(AF_I
3220: 4e 45 54 2c 20 26 61 64 64 72 5f 76 34 2d 3e 73  NET, &addr_v4->s
3230: 69 6e 5f 61 64 64 72 2c 20 62 75 66 66 65 72 2c  in_addr, buffer,
3240: 20 62 75 66 66 65 72 6c 65 6e 29 3b 0a 09 09 09   bufferlen);....
3250: 62 72 65 61 6b 3b 0a 09 09 63 61 73 65 20 41 46  break;...case AF
3260: 5f 49 4e 45 54 36 3a 0a 09 09 09 72 65 74 76 61  _INET6:....retva
3270: 6c 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 41 46  l = inet_ntop(AF
3280: 5f 49 4e 45 54 36 2c 20 26 61 64 64 72 5f 76 36  _INET6, &addr_v6
3290: 2d 3e 73 69 6e 36 5f 61 64 64 72 2c 20 62 75 66  ->sin6_addr, buf
32a0: 66 65 72 2c 20 62 75 66 66 65 72 6c 65 6e 29 3b  fer, bufferlen);
32b0: 0a 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a 0a 09  ....break;..}...
32c0: 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a  return(retval);.
32d0: 7d 0a 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a  }..static FILE *
32e0: 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 63  filed_log_open(c
32f0: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 29  onst char *file)
3300: 20 7b 0a 09 46 49 4c 45 20 2a 72 65 74 76 61 6c   {..FILE *retval
3310: 3b 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 28 66  ;...if (strcmp(f
3320: 69 6c 65 2c 20 22 2d 22 29 20 3d 3d 20 30 29 20  ile, "-") == 0) 
3330: 7b 0a 09 09 72 65 74 76 61 6c 20 3d 20 73 74 64  {...retval = std
3340: 6f 75 74 3b 0a 09 7d 20 65 6c 73 65 20 69 66 20  out;..} else if 
3350: 28 66 69 6c 65 5b 30 5d 20 3d 3d 20 27 7c 27 29  (file[0] == '|')
3360: 20 7b 0a 09 09 66 69 6c 65 2b 2b 3b 0a 09 09 72   {...file++;...r
3370: 65 74 76 61 6c 20 3d 20 70 6f 70 65 6e 28 66 69  etval = popen(fi
3380: 6c 65 2c 20 22 77 22 29 3b 0a 09 7d 20 65 6c 73  le, "w");..} els
3390: 65 20 7b 0a 09 09 72 65 74 76 61 6c 20 3d 20 66  e {...retval = f
33a0: 6f 70 65 6e 28 66 69 6c 65 2c 20 22 61 2b 22 29  open(file, "a+")
33b0: 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65  ;..}...return(re
33c0: 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  tval);.}..static
33d0: 20 69 6e 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69   int filed_loggi
33e0: 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 46  ng_thread_init(F
33f0: 49 4c 45 20 2a 6c 6f 67 66 70 29 20 7b 0a 09 73  ILE *logfp) {..s
3400: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 67  truct filed_logg
3410: 69 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 73 20  ing_thread_args 
3420: 2a 61 72 67 73 3b 0a 09 70 74 68 72 65 61 64 5f  *args;..pthread_
3430: 74 20 74 68 72 65 61 64 5f 69 64 3b 0a 0a 09 61  t thread_id;...a
3440: 72 67 73 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a  rgs = malloc(siz
3450: 65 6f 66 28 2a 61 72 67 73 29 29 3b 0a 09 61 72  eof(*args));..ar
3460: 67 73 2d 3e 66 70 20 3d 20 6c 6f 67 66 70 3b 0a  gs->fp = logfp;.
3470: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  ..filed_log_msg_
3480: 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 70  list = NULL;...p
3490: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69  thread_mutex_ini
34a0: 74 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  t(&filed_log_msg
34b0: 5f 6c 69 73 74 5f 6d 75 74 65 78 2c 20 4e 55 4c  _list_mutex, NUL
34c0: 4c 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 72  L);...pthread_cr
34d0: 65 61 74 65 28 26 74 68 72 65 61 64 5f 69 64 2c  eate(&thread_id,
34e0: 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 6c 6f 67   NULL, filed_log
34f0: 67 69 6e 67 5f 74 68 72 65 61 64 2c 20 61 72 67  ging_thread, arg
3500: 73 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f  s);...filed_log_
3510: 6d 73 67 28 22 53 54 41 52 54 22 29 3b 0a 0a 09  msg("START");...
3520: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 23 65 6e  return(0);.}.#en
3530: 64 69 66 0a 0a 23 69 66 64 65 66 20 46 49 4c 45  dif..#ifdef FILE
3540: 44 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55 54 0a 23  D_DONT_TIMEOUT.#
3550: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63  define filed_soc
3560: 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61  kettimeout_threa
3570: 64 5f 69 6e 69 74 28 29 20 30 0a 23 64 65 66 69  d_init() 0.#defi
3580: 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  ne filed_sockett
3590: 69 6d 65 6f 75 74 5f 61 63 63 65 70 74 28 78 29  imeout_accept(x)
35a0: 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69   /**/.#define fi
35b0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
35c0: 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 73 74 61  t_processing_sta
35d0: 72 74 28 78 29 20 2f 2a 2a 2f 0a 23 64 65 66 69  rt(x) /**/.#defi
35e0: 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  ne filed_sockett
35f0: 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e  imeout_processin
3600: 67 5f 65 6e 64 28 78 29 20 2f 2a 2a 2f 0a 23 64  g_end(x) /**/.#d
3610: 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b  efine filed_sock
3620: 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28  ettimeout_close(
3630: 78 29 20 2f 2a 2a 2f 0a 23 65 6c 73 65 0a 5f 41  x) /**/.#else._A
3640: 74 6f 6d 69 63 20 74 69 6d 65 5f 74 20 66 69 6c  tomic time_t fil
3650: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
3660: 5f 74 69 6d 65 3b 0a 73 74 72 75 63 74 20 7b 0a  _time;.struct {.
3670: 09 5f 41 74 6f 6d 69 63 20 74 69 6d 65 5f 74 20  ._Atomic time_t 
3680: 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 3b  expiration_time;
3690: 0a 09 5f 41 74 6f 6d 69 63 20 70 74 68 72 65 61  .._Atomic pthrea
36a0: 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b 0a 09  d_t thread_id;..
36b0: 62 6f 6f 6c 20 76 61 6c 69 64 3b 0a 7d 2a 20 66  bool valid;.}* f
36c0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
36d0: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 3b 0a 6c  ut_sockstatus;.l
36e0: 6f 6e 67 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74  ong filed_socket
36f0: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74  timeout_sockstat
3700: 75 73 5f 6c 65 6e 67 74 68 3b 0a 69 6e 74 20 66  us_length;.int f
3710: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3720: 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 3b 0a 0a  ut_devnull_fd;..
3730: 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64  static int filed
3740: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73  _sockettimeout_s
3750: 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28 69  ockfd_in_range(i
3760: 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66  nt sockfd) {..if
3770: 20 28 73 6f 63 6b 66 64 20 3c 20 33 29 20 7b 0a   (sockfd < 3) {.
3780: 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a  ..return(0);..}.
3790: 0a 09 69 66 20 28 73 6f 63 6b 66 64 20 3e 20 66  ..if (sockfd > f
37a0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
37b0: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c 65  ut_sockstatus_le
37c0: 6e 67 74 68 29 20 7b 0a 09 09 72 65 74 75 72 6e  ngth) {...return
37d0: 28 30 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e  (0);..}...return
37e0: 28 31 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  (1);.}..static v
37f0: 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74  oid filed_socket
3800: 74 69 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 69  timeout_expire(i
3810: 6e 74 20 73 6f 63 6b 66 64 2c 20 69 6e 74 20 6c  nt sockfd, int l
3820: 65 6e 67 74 68 29 20 7b 0a 09 74 69 6d 65 5f 74  ength) {..time_t
3830: 20 6e 6f 77 2c 20 65 78 70 69 72 65 3b 0a 0a 09   now, expire;...
3840: 6e 6f 77 20 3d 20 61 74 6f 6d 69 63 5f 6c 6f 61  now = atomic_loa
3850: 64 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  d(&filed_sockett
3860: 69 6d 65 6f 75 74 5f 74 69 6d 65 29 3b 0a 0a 09  imeout_time);...
3870: 65 78 70 69 72 65 20 3d 20 6e 6f 77 20 2b 20 6c  expire = now + l
3880: 65 6e 67 74 68 3b 0a 0a 09 61 74 6f 6d 69 63 5f  ength;...atomic_
3890: 73 74 6f 72 65 28 26 66 69 6c 65 64 5f 73 6f 63  store(&filed_soc
38a0: 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73  kettimeout_socks
38b0: 74 61 74 75 73 5b 73 6f 63 6b 66 64 5d 2e 65 78  tatus[sockfd].ex
38c0: 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 2c 20 65  piration_time, e
38d0: 78 70 69 72 65 29 3b 0a 0a 09 72 65 74 75 72 6e  xpire);...return
38e0: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
38f0: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d   filed_sockettim
3900: 65 6f 75 74 5f 61 63 63 65 70 74 28 69 6e 74 20  eout_accept(int 
3910: 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 21  sockfd) {..if (!
3920: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3930: 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61  out_sockfd_in_ra
3940: 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b 0a 09  nge(sockfd)) {..
3950: 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 66 69  .return;..}...fi
3960: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
3970: 74 5f 65 78 70 69 72 65 28 73 6f 63 6b 66 64 2c  t_expire(sockfd,
3980: 20 36 30 29 3b 0a 0a 09 61 74 6f 6d 69 63 5f 73   60);...atomic_s
3990: 74 6f 72 65 28 26 66 69 6c 65 64 5f 73 6f 63 6b  tore(&filed_sock
39a0: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74  ettimeout_sockst
39b0: 61 74 75 73 5b 73 6f 63 6b 66 64 5d 2e 74 68 72  atus[sockfd].thr
39c0: 65 61 64 5f 69 64 2c 20 70 74 68 72 65 61 64 5f  ead_id, pthread_
39d0: 73 65 6c 66 28 29 29 3b 0a 0a 09 61 74 6f 6d 69  self());...atomi
39e0: 63 5f 73 74 6f 72 65 28 26 66 69 6c 65 64 5f 73  c_store(&filed_s
39f0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63  ockettimeout_soc
3a00: 6b 73 74 61 74 75 73 5b 73 6f 63 6b 66 64 5d 2e  kstatus[sockfd].
3a10: 76 61 6c 69 64 2c 20 74 72 75 65 29 3b 0a 0a 09  valid, true);...
3a20: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69  return;.}..stati
3a30: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63  c void filed_soc
3a40: 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65  kettimeout_proce
3a50: 73 73 69 6e 67 5f 73 74 61 72 74 28 69 6e 74 20  ssing_start(int 
3a60: 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 21  sockfd) {..if (!
3a70: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3a80: 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61  out_sockfd_in_ra
3a90: 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b 0a 09  nge(sockfd)) {..
3aa0: 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 66 69  .return;..}...fi
3ab0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
3ac0: 74 5f 65 78 70 69 72 65 28 73 6f 63 6b 66 64 2c  t_expire(sockfd,
3ad0: 20 38 36 34 30 30 29 3b 0a 0a 09 72 65 74 75 72   86400);...retur
3ae0: 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  n;.}..static voi
3af0: 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  d filed_socketti
3b00: 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e 67  meout_processing
3b10: 5f 65 6e 64 28 69 6e 74 20 73 6f 63 6b 66 64 29  _end(int sockfd)
3b20: 20 7b 0a 09 69 66 20 28 21 66 69 6c 65 64 5f 73   {..if (!filed_s
3b30: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63  ockettimeout_soc
3b40: 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28 73 6f 63  kfd_in_range(soc
3b50: 6b 66 64 29 29 20 7b 0a 09 09 72 65 74 75 72 6e  kfd)) {...return
3b60: 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63  ;..}...filed_soc
3b70: 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78 70 69 72  kettimeout_expir
3b80: 65 28 73 6f 63 6b 66 64 2c 20 36 30 29 3b 0a 0a  e(sockfd, 60);..
3b90: 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74  .return;.}..stat
3ba0: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f  ic void filed_so
3bb0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73  ckettimeout_clos
3bc0: 65 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a  e(int sockfd) {.
3bd0: 09 69 66 20 28 21 66 69 6c 65 64 5f 73 6f 63 6b  .if (!filed_sock
3be0: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64  ettimeout_sockfd
3bf0: 5f 69 6e 5f 72 61 6e 67 65 28 73 6f 63 6b 66 64  _in_range(sockfd
3c00: 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 3b 0a 09  )) {...return;..
3c10: 7d 0a 0a 09 61 74 6f 6d 69 63 5f 73 74 6f 72 65  }...atomic_store
3c20: 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  (&filed_socketti
3c30: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73  meout_sockstatus
3c40: 5b 73 6f 63 6b 66 64 5d 2e 76 61 6c 69 64 2c 20  [sockfd].valid, 
3c50: 66 61 6c 73 65 29 3b 0a 0a 09 72 65 74 75 72 6e  false);...return
3c60: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
3c70: 20 2a 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69   *filed_socketti
3c80: 6d 65 6f 75 74 5f 74 68 72 65 61 64 28 76 6f 69  meout_thread(voi
3c90: 64 20 2a 61 72 67 29 20 7b 0a 09 74 69 6d 65 5f  d *arg) {..time_
3ca0: 74 20 6e 6f 77 2c 20 65 78 70 69 72 61 74 69 6f  t now, expiratio
3cb0: 6e 5f 74 69 6d 65 3b 0a 09 70 74 68 72 65 61 64  n_time;..pthread
3cc0: 5f 74 20 74 68 72 65 61 64 5f 69 64 3b 0a 09 6c  _t thread_id;..l
3cd0: 6f 6e 67 20 69 64 78 3b 0a 09 69 6e 74 20 63 6f  ong idx;..int co
3ce0: 75 6e 74 3b 0a 09 62 6f 6f 6c 20 76 61 6c 69 64  unt;..bool valid
3cf0: 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a  ;...while (1) {.
3d00: 09 09 66 6f 72 20 28 63 6f 75 6e 74 20 3d 20 30  ..for (count = 0
3d10: 3b 20 63 6f 75 6e 74 20 3c 20 31 30 3b 20 63 6f  ; count < 10; co
3d20: 75 6e 74 2b 2b 29 20 7b 0a 09 09 09 75 73 6c 65  unt++) {....usle
3d30: 65 70 28 33 30 30 30 30 30 30 30 29 3b 0a 0a 09  ep(30000000);...
3d40: 09 09 6e 6f 77 20 3d 20 74 69 6d 65 28 4e 55 4c  ..now = time(NUL
3d50: 4c 29 3b 0a 0a 09 09 09 61 74 6f 6d 69 63 5f 73  L);.....atomic_s
3d60: 74 6f 72 65 28 26 66 69 6c 65 64 5f 73 6f 63 6b  tore(&filed_sock
3d70: 65 74 74 69 6d 65 6f 75 74 5f 74 69 6d 65 2c 20  ettimeout_time, 
3d80: 6e 6f 77 29 3b 0a 09 09 7d 0a 0a 09 09 66 6f 72  now);...}....for
3d90: 20 28 69 64 78 20 3d 20 30 3b 20 69 64 78 20 3c   (idx = 0; idx <
3da0: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d   filed_sockettim
3db0: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f  eout_sockstatus_
3dc0: 6c 65 6e 67 74 68 3b 20 69 64 78 2b 2b 29 20 7b  length; idx++) {
3dd0: 0a 09 09 09 76 61 6c 69 64 20 3d 20 61 74 6f 6d  ....valid = atom
3de0: 69 63 5f 6c 6f 61 64 28 26 66 69 6c 65 64 5f 73  ic_load(&filed_s
3df0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63  ockettimeout_soc
3e00: 6b 73 74 61 74 75 73 5b 69 64 78 5d 2e 76 61 6c  kstatus[idx].val
3e10: 69 64 29 3b 0a 0a 09 09 09 69 66 20 28 21 76 61  id);.....if (!va
3e20: 6c 69 64 29 20 7b 0a 09 09 09 09 63 6f 6e 74 69  lid) {.....conti
3e30: 6e 75 65 3b 0a 09 09 09 7d 0a 0a 09 09 09 65 78  nue;....}.....ex
3e40: 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 20 3d 20  piration_time = 
3e50: 61 74 6f 6d 69 63 5f 6c 6f 61 64 28 26 66 69 6c  atomic_load(&fil
3e60: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
3e70: 5f 73 6f 63 6b 73 74 61 74 75 73 5b 69 64 78 5d  _sockstatus[idx]
3e80: 2e 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65  .expiration_time
3e90: 29 3b 0a 0a 09 09 09 74 68 72 65 61 64 5f 69 64  );.....thread_id
3ea0: 20 3d 20 61 74 6f 6d 69 63 5f 6c 6f 61 64 28 26   = atomic_load(&
3eb0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3ec0: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 69  out_sockstatus[i
3ed0: 64 78 5d 2e 74 68 72 65 61 64 5f 69 64 29 3b 0a  dx].thread_id);.
3ee0: 0a 09 09 09 69 66 20 28 65 78 70 69 72 61 74 69  ....if (expirati
3ef0: 6f 6e 5f 74 69 6d 65 20 3e 20 6e 6f 77 29 20 7b  on_time > now) {
3f00: 0a 09 09 09 09 63 6f 6e 74 69 6e 75 65 3b 0a 09  .....continue;..
3f10: 09 09 7d 0a 0a 09 09 09 66 69 6c 65 64 5f 73 6f  ..}.....filed_so
3f20: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73  ckettimeout_clos
3f30: 65 28 69 64 78 29 3b 0a 0a 09 09 09 64 75 70 32  e(idx);.....dup2
3f40: 28 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d  (filed_sockettim
3f50: 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 2c  eout_devnull_fd,
3f60: 20 69 64 78 29 3b 0a 0a 09 09 09 70 74 68 72 65   idx);.....pthre
3f70: 61 64 5f 6b 69 6c 6c 28 74 68 72 65 61 64 5f 69  ad_kill(thread_i
3f80: 64 2c 20 53 49 47 50 49 50 45 29 3b 0a 09 09 7d  d, SIGPIPE);...}
3f90: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c  ..}...return(NUL
3fa0: 4c 29 3b 0a 0a 09 2f 2a 20 4e 4f 54 52 45 41 43  L);.../* NOTREAC
3fb0: 48 3a 20 57 65 20 64 6f 6e 27 74 20 61 63 74 75  H: We don't actu
3fc0: 61 6c 6c 79 20 74 61 6b 65 20 61 6e 79 20 61 72  ally take any ar
3fd0: 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 72 67 20  guments */..arg 
3fe0: 3d 20 61 72 67 3b 0a 7d 0a 0a 73 74 61 74 69 63  = arg;.}..static
3ff0: 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65   int filed_socke
4000: 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f  ttimeout_thread_
4010: 69 6e 69 74 28 76 6f 69 64 29 20 7b 0a 09 70 74  init(void) {..pt
4020: 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 5f 69  hread_t thread_i
4030: 64 3b 0a 09 6c 6f 6e 67 20 6d 61 78 66 64 2c 20  d;..long maxfd, 
4040: 69 64 78 3b 0a 0a 09 6d 61 78 66 64 20 3d 20 73  idx;...maxfd = s
4050: 79 73 63 6f 6e 66 28 5f 53 43 5f 4f 50 45 4e 5f  ysconf(_SC_OPEN_
4060: 4d 41 58 29 3b 0a 09 69 66 20 28 6d 61 78 66 64  MAX);..if (maxfd
4070: 20 3c 3d 20 30 29 20 7b 0a 09 09 6d 61 78 66 64   <= 0) {...maxfd
4080: 20 3d 20 34 30 39 36 3b 0a 09 7d 0a 0a 09 66 69   = 4096;..}...fi
4090: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
40a0: 74 5f 73 6f 63 6b 73 74 61 74 75 73 20 3d 20 6d  t_sockstatus = m
40b0: 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 66 69  alloc(sizeof(*fi
40c0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
40d0: 74 5f 73 6f 63 6b 73 74 61 74 75 73 29 20 2a 20  t_sockstatus) * 
40e0: 6d 61 78 66 64 29 3b 0a 09 69 66 20 28 66 69 6c  maxfd);..if (fil
40f0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
4100: 5f 73 6f 63 6b 73 74 61 74 75 73 20 3d 3d 20 4e  _sockstatus == N
4110: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  ULL) {...return(
4120: 2d 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 20 28 69  -1);..}...for (i
4130: 64 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 6d 61  dx = 0; idx < ma
4140: 78 66 64 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09  xfd; idx++) {...
4150: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
4160: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 69  out_sockstatus[i
4170: 64 78 5d 2e 76 61 6c 69 64 20 3d 20 66 61 6c 73  dx].valid = fals
4180: 65 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f  e;..}...filed_so
4190: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b  ckettimeout_sock
41a0: 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 20 3d 20  status_length = 
41b0: 6d 61 78 66 64 3b 0a 0a 09 66 69 6c 65 64 5f 73  maxfd;...filed_s
41c0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 64 65 76  ockettimeout_dev
41d0: 6e 75 6c 6c 5f 66 64 20 3d 20 6f 70 65 6e 28 22  null_fd = open("
41e0: 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 52 44  /dev/null", O_RD
41f0: 57 52 29 3b 0a 09 69 66 20 28 66 69 6c 65 64 5f  WR);..if (filed_
4200: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 64 65  sockettimeout_de
4210: 76 6e 75 6c 6c 5f 66 64 20 3c 20 30 29 20 7b 0a  vnull_fd < 0) {.
4220: 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d  ..return(-1);..}
4230: 0a 0a 09 70 74 68 72 65 61 64 5f 63 72 65 61 74  ...pthread_creat
4240: 65 28 26 74 68 72 65 61 64 5f 69 64 2c 20 4e 55  e(&thread_id, NU
4250: 4c 4c 2c 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74  LL, filed_socket
4260: 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 2c 20  timeout_thread, 
4270: 4e 55 4c 4c 29 3b 0a 0a 09 72 65 74 75 72 6e 28  NULL);...return(
4280: 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  0);.}.#endif../*
4290: 20 46 6f 72 6d 61 74 20 74 69 6d 65 20 70 65 72   Format time per
42a0: 20 52 46 43 32 36 31 36 20 2a 2f 0a 73 74 61 74   RFC2616 */.stat
42b0: 69 63 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 66  ic char *filed_f
42c0: 6f 72 6d 61 74 5f 74 69 6d 65 28 63 68 61 72 20  ormat_time(char 
42d0: 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f 74 20  *buffer, size_t 
42e0: 62 75 66 66 65 72 5f 6c 65 6e 2c 20 63 6f 6e 73  buffer_len, cons
42f0: 74 20 74 69 6d 65 5f 74 20 74 69 6d 65 69 6e 66  t time_t timeinf
4300: 6f 29 20 7b 0a 09 73 74 72 75 63 74 20 74 6d 20  o) {..struct tm 
4310: 74 69 6d 65 69 6e 66 6f 5f 74 6d 2c 20 2a 74 69  timeinfo_tm, *ti
4320: 6d 65 69 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a 09 74  meinfo_tm_p;...t
4330: 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d 20 67  imeinfo_tm_p = g
4340: 6d 74 69 6d 65 5f 72 28 26 74 69 6d 65 69 6e 66  mtime_r(&timeinf
4350: 6f 2c 20 26 74 69 6d 65 69 6e 66 6f 5f 74 6d 29  o, &timeinfo_tm)
4360: 3b 0a 09 69 66 20 28 74 69 6d 65 69 6e 66 6f 5f  ;..if (timeinfo_
4370: 74 6d 5f 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  tm_p == NULL) {.
4380: 09 09 72 65 74 75 72 6e 28 22 75 6e 6b 6e 6f 77  ..return("unknow
4390: 6e 22 29 3b 0a 09 7d 0a 0a 09 62 75 66 66 65 72  n");..}...buffer
43a0: 5b 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 31 5d  [buffer_len - 1]
43b0: 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72   = '\0';..buffer
43c0: 5f 6c 65 6e 20 3d 20 73 74 72 66 74 69 6d 65 28  _len = strftime(
43d0: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c  buffer, buffer_l
43e0: 65 6e 20 2d 20 31 2c 20 22 25 61 2c 20 25 64 20  en - 1, "%a, %d 
43f0: 25 62 20 25 59 20 25 48 3a 25 4d 3a 25 53 20 47  %b %Y %H:%M:%S G
4400: 4d 54 22 2c 20 74 69 6d 65 69 6e 66 6f 5f 74 6d  MT", timeinfo_tm
4410: 5f 70 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75  _p);...return(bu
4420: 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 61 73  ffer);.}../* has
4430: 68 20 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69  h */.static unsi
4440: 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 5f 68  gned int filed_h
4450: 61 73 68 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ash(const unsign
4460: 65 64 20 63 68 61 72 20 2a 76 61 6c 75 65 2c 20  ed char *value, 
4470: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 6f 64  unsigned int mod
4480: 75 6c 75 73 29 20 7b 0a 09 75 6e 73 69 67 6e 65  ulus) {..unsigne
4490: 64 20 63 68 61 72 20 63 75 72 72 2c 20 70 72 65  d char curr, pre
44a0: 76 3b 0a 09 69 6e 74 20 64 69 66 66 3b 0a 09 75  v;..int diff;..u
44b0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 65 74 76  nsigned int retv
44c0: 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d  al;...retval = m
44d0: 6f 64 75 6c 75 73 20 2d 20 31 3b 0a 09 70 72 65  odulus - 1;..pre
44e0: 76 20 3d 20 6d 6f 64 75 6c 75 73 20 25 20 32 35  v = modulus % 25
44f0: 35 3b 0a 0a 09 77 68 69 6c 65 20 28 28 63 75 72  5;...while ((cur
4500: 72 20 3d 20 2a 76 61 6c 75 65 29 29 20 7b 0a 09  r = *value)) {..
4510: 09 69 66 20 28 63 75 72 72 20 3c 20 33 32 29 20  .if (curr < 32) 
4520: 7b 0a 09 09 09 63 75 72 72 20 3d 20 32 35 35 20  {....curr = 255 
4530: 2d 20 63 75 72 72 3b 0a 09 09 7d 20 65 6c 73 65  - curr;...} else
4540: 20 7b 0a 09 09 09 63 75 72 72 20 2d 3d 20 33 32   {....curr -= 32
4550: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 70 72 65  ;...}....if (pre
4560: 76 20 3c 20 63 75 72 72 29 20 7b 0a 09 09 09 64  v < curr) {....d
4570: 69 66 66 20 3d 20 63 75 72 72 20 2d 20 70 72 65  iff = curr - pre
4580: 76 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09  v;...} else {...
4590: 09 64 69 66 66 20 3d 20 70 72 65 76 20 2d 20 63  .diff = prev - c
45a0: 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 70 72 65 76  urr;...}....prev
45b0: 20 3d 20 63 75 72 72 3b 0a 0a 09 09 72 65 74 76   = curr;....retv
45c0: 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 65 74 76  al <<= 3;...retv
45d0: 61 6c 20 26 3d 20 30 78 46 46 46 46 46 46 46 46  al &= 0xFFFFFFFF
45e0: 4c 55 3b 0a 09 09 72 65 74 76 61 6c 20 5e 3d 20  LU;...retval ^= 
45f0: 64 69 66 66 3b 0a 0a 09 09 76 61 6c 75 65 2b 2b  diff;....value++
4600: 3b 0a 09 7d 0a 0a 09 72 65 74 76 61 6c 20 3d 20  ;..}...retval = 
4610: 72 65 74 76 61 6c 20 25 20 6d 6f 64 75 6c 75 73  retval % modulus
4620: 3b 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61  ;...return(retva
4630: 6c 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 20 61  l);.}../* Find a
4640: 20 6d 69 6d 65 2d 74 79 70 65 20 62 61 73 65 64   mime-type based
4650: 20 6f 6e 20 74 68 65 20 66 69 6c 65 6e 61 6d 65   on the filename
4660: 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
4670: 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 64 65 74   char *filed_det
4680: 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28  ermine_mimetype(
4690: 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68  const char *path
46a0: 29 20 7b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20  ) {..const char 
46b0: 2a 70 3b 0a 0a 09 70 20 3d 20 73 74 72 72 63 68  *p;...p = strrch
46c0: 72 28 70 61 74 68 2c 20 27 2e 27 29 3b 0a 09 69  r(path, '.');..i
46d0: 66 20 28 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  f (p == NULL) {.
46e0: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 44  ..return(FILED_D
46f0: 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d  EFAULT_TYPE);..}
4700: 0a 0a 09 70 2b 2b 3b 0a 09 69 66 20 28 2a 70 20  ...p++;..if (*p 
4710: 3d 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 65 74  == '\0') {...ret
4720: 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c  urn(FILED_DEFAUL
4730: 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 66 69  T_TYPE);..}...fi
4740: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75  led_log_msg_debu
4750: 67 28 22 4c 6f 6f 6b 69 6e 67 20 75 70 20 4d 49  g("Looking up MI
4760: 4d 45 20 74 79 70 65 20 66 6f 72 20 25 73 20 28  ME type for %s (
4770: 68 61 73 68 20 3d 20 25 6c 6c 75 29 22 2c 20 70  hash = %llu)", p
4780: 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  , (unsigned long
4790: 20 6c 6f 6e 67 29 20 66 69 6c 65 64 5f 68 61 73   long) filed_has
47a0: 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  h((const unsigne
47b0: 64 20 63 68 61 72 20 2a 29 20 70 2c 20 31 36 37  d char *) p, 167
47c0: 37 37 32 35 39 29 29 3b 0a 0a 23 69 6e 63 6c 75  77259));..#inclu
47d0: 64 65 20 22 66 69 6c 65 64 2d 6d 69 6d 65 2d 74  de "filed-mime-t
47e0: 79 70 65 73 2e 68 22 0a 0a 09 72 65 74 75 72 6e  ypes.h"...return
47f0: 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54  (FILED_DEFAULT_T
4800: 59 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 6e 65  YPE);.}../* Gene
4810: 72 61 74 65 20 61 20 75 6e 69 71 75 65 20 69 64  rate a unique id
4820: 65 6e 74 69 66 69 65 72 20 2a 2f 0a 73 74 61 74  entifier */.stat
4830: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67 65  ic void filed_ge
4840: 6e 65 72 61 74 65 5f 65 74 61 67 28 63 68 61 72  nerate_etag(char
4850: 20 2a 65 74 61 67 2c 20 73 69 7a 65 5f 74 20 6c   *etag, size_t l
4860: 65 6e 67 74 68 29 20 7b 0a 09 73 6e 70 72 69 6e  ength) {..snprin
4870: 74 66 28 65 74 61 67 2c 20 6c 65 6e 67 74 68 2c  tf(etag, length,
4880: 20 22 25 6c 6c 78 2d 25 6c 6c 78 25 6c 6c 78 25   "%llx-%llx%llx%
4890: 6c 6c 78 25 6c 6c 78 22 2c 0a 09 09 28 75 6e 73  llx%llx",...(uns
48a0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
48b0: 20 74 69 6d 65 28 4e 55 4c 4c 29 2c 0a 09 09 28   time(NULL),...(
48c0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
48d0: 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09  ng) random(),...
48e0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
48f0: 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09  ong) random(),..
4900: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
4910: 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a  long) random(),.
4920: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
4930: 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 0a   long) random().
4940: 09 29 3b 0a 7d 0a 0a 2f 2a 20 4f 70 65 6e 20 61  .);.}../* Open a
4950: 20 66 69 6c 65 20 61 6e 64 20 72 65 74 75 72 6e   file and return
4960: 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f   file informatio
4970: 6e 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  n */.static stru
4980: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  ct filed_fileinf
4990: 6f 20 2a 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69  o *filed_open_fi
49a0: 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  le(const char *p
49b0: 61 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c 65  ath, struct file
49c0: 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 62 75 66 66  d_fileinfo *buff
49d0: 65 72 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69  er) {..struct fi
49e0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61  led_fileinfo *ca
49f0: 63 68 65 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69  che;..unsigned i
4a00: 6e 74 20 63 61 63 68 65 5f 69 64 78 3b 0a 09 6f  nt cache_idx;..o
4a10: 66 66 5f 74 20 6c 65 6e 3b 0a 09 69 6e 74 20 66  ff_t len;..int f
4a20: 64 3b 0a 0a 09 63 61 63 68 65 5f 69 64 78 20 3d  d;...cache_idx =
4a30: 20 66 69 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e   filed_hash((con
4a40: 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
4a50: 20 2a 29 20 70 61 74 68 2c 20 66 69 6c 65 64 5f   *) path, filed_
4a60: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65  fileinfo_fdcache
4a70: 5f 73 69 7a 65 29 3b 0a 0a 09 63 61 63 68 65 20  _size);...cache 
4a80: 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  = &filed_fileinf
4a90: 6f 5f 66 64 63 61 63 68 65 5b 63 61 63 68 65 5f  o_fdcache[cache_
4aa0: 69 64 78 5d 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f  idx];...filed_lo
4ab0: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 4c 6f 63  g_msg_debug("Loc
4ac0: 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20 69  king mutex for i
4ad0: 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69 67  dx: %lu", (unsig
4ae0: 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f  ned long) cache_
4af0: 69 64 78 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f  idx);...pthread_
4b00: 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68  mutex_lock(&cach
4b10: 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 66 69 6c  e->mutex);...fil
4b20: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67  ed_log_msg_debug
4b30: 28 22 43 6f 6d 70 6c 65 74 65 64 20 6c 6f 63 6b  ("Completed lock
4b40: 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20 69 64  ing mutex for id
4b50: 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e  x: %lu", (unsign
4b60: 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69  ed long) cache_i
4b70: 64 78 29 3b 0a 0a 09 69 66 20 28 73 74 72 63 6d  dx);...if (strcm
4b80: 70 28 70 61 74 68 2c 20 63 61 63 68 65 2d 3e 70  p(path, cache->p
4b90: 61 74 68 29 20 21 3d 20 30 29 20 7b 0a 09 09 66  ath) != 0) {...f
4ba0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62  iled_log_msg_deb
4bb0: 75 67 28 22 43 61 63 68 65 20 6d 69 73 73 20 66  ug("Cache miss f
4bc0: 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20 4f 4c 44  or idx: %lu: OLD
4bd0: 20 5c 22 25 73 5c 22 2c 20 4e 45 57 20 5c 22 25   \"%s\", NEW \"%
4be0: 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e 65 64 20  s\"", (unsigned 
4bf0: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 2c  long) cache_idx,
4c00: 20 63 61 63 68 65 2d 3e 70 61 74 68 2c 20 70 61   cache->path, pa
4c10: 74 68 29 3b 0a 0a 09 09 66 64 20 3d 20 6f 70 65  th);....fd = ope
4c20: 6e 28 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59  n(path, O_RDONLY
4c30: 20 7c 20 4f 5f 4c 41 52 47 45 46 49 4c 45 29 3b   | O_LARGEFILE);
4c40: 0a 09 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b  ...if (fd < 0) {
4c50: 0a 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65  ....pthread_mute
4c60: 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d  x_unlock(&cache-
4c70: 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09 72 65 74  >mutex);.....ret
4c80: 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a  urn(NULL);...}..
4c90: 09 09 69 66 20 28 63 61 63 68 65 2d 3e 66 64 20  ..if (cache->fd 
4ca0: 3e 3d 20 30 29 20 7b 0a 09 09 09 63 6c 6f 73 65  >= 0) {....close
4cb0: 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 09 7d  (cache->fd);...}
4cc0: 0a 0a 09 09 6c 65 6e 20 3d 20 6c 73 65 65 6b 28  ....len = lseek(
4cd0: 66 64 2c 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29  fd, 0, SEEK_END)
4ce0: 3b 0a 09 09 6c 73 65 65 6b 28 66 64 2c 20 30 2c  ;...lseek(fd, 0,
4cf0: 20 53 45 45 4b 5f 53 45 54 29 3b 0a 0a 09 09 63   SEEK_SET);....c
4d00: 61 63 68 65 2d 3e 66 64 20 3d 20 66 64 3b 0a 09  ache->fd = fd;..
4d10: 09 63 61 63 68 65 2d 3e 6c 65 6e 20 3d 20 6c 65  .cache->len = le
4d20: 6e 3b 0a 09 09 73 74 72 63 70 79 28 63 61 63 68  n;...strcpy(cach
4d30: 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 3b 0a  e->path, path);.
4d40: 09 09 63 61 63 68 65 2d 3e 74 79 70 65 20 3d 20  ..cache->type = 
4d50: 66 69 6c 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f  filed_determine_
4d60: 6d 69 6d 65 74 79 70 65 28 70 61 74 68 29 3b 0a  mimetype(path);.
4d70: 09 09 66 69 6c 65 64 5f 67 65 6e 65 72 61 74 65  ..filed_generate
4d80: 5f 65 74 61 67 28 63 61 63 68 65 2d 3e 65 74 61  _etag(cache->eta
4d90: 67 2c 20 73 69 7a 65 6f 66 28 63 61 63 68 65 2d  g, sizeof(cache-
4da0: 3e 65 74 61 67 29 29 3b 0a 0a 09 09 2f 2a 20 58  >etag));..../* X
4db0: 58 58 3a 54 4f 44 4f 3a 20 44 65 74 65 72 6d 69  XX:TODO: Determi
4dc0: 6e 65 20 2a 2f 0a 09 09 63 61 63 68 65 2d 3e 6c  ne */...cache->l
4dd0: 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 64 5f 66  astmod = filed_f
4de0: 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 63 68 65  ormat_time(cache
4df0: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a  ->lastmod_b, siz
4e00: 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d  eof(cache->lastm
4e10: 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c  od_b), time(NULL
4e20: 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 6c 73 65  ) - 30);..} else
4e30: 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d   {...filed_log_m
4e40: 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65 20  sg_debug("Cache 
4e50: 68 69 74 20 66 6f 72 20 69 64 78 3a 20 25 6c 75  hit for idx: %lu
4e60: 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 22 2c 20  : PATH \"%s\"", 
4e70: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20  (unsigned long) 
4e80: 63 61 63 68 65 5f 69 64 78 2c 20 70 61 74 68 29  cache_idx, path)
4e90: 3b 0a 09 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 57 65  ;..}.../*.. * We
4ea0: 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20   have to make a 
4eb0: 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20 62 65  duplicate FD, be
4ec0: 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20 72 65  cause once we re
4ed0: 6c 65 61 73 65 20 74 68 65 20 63 61 63 68 65 0a  lease the cache.
4ee0: 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 65 20 66  . * mutex, the f
4ef0: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d  ile descriptor m
4f00: 61 79 20 62 65 20 63 6c 6f 73 65 64 0a 09 20 2a  ay be closed.. *
4f10: 2f 0a 09 66 64 20 3d 20 64 75 70 28 63 61 63 68  /..fd = dup(cach
4f20: 65 2d 3e 66 64 29 3b 0a 09 69 66 20 28 66 64 20  e->fd);..if (fd 
4f30: 3c 20 30 29 20 7b 0a 09 09 70 74 68 72 65 61 64  < 0) {...pthread
4f40: 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63  _mutex_unlock(&c
4f50: 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09  ache->mutex);...
4f60: 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09  .return(NULL);..
4f70: 7d 0a 0a 09 62 75 66 66 65 72 2d 3e 66 64 20 3d  }...buffer->fd =
4f80: 20 66 64 3b 0a 09 62 75 66 66 65 72 2d 3e 6c 65   fd;..buffer->le
4f90: 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e 3b 0a  n = cache->len;.
4fa0: 09 62 75 66 66 65 72 2d 3e 74 79 70 65 20 3d 20  .buffer->type = 
4fb0: 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a 09 6d 65  cache->type;..me
4fc0: 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 6c 61 73  mcpy(buffer->las
4fd0: 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d 3e 6c  tmod_b, cache->l
4fe0: 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66  astmod_b, sizeof
4ff0: 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64  (buffer->lastmod
5000: 5f 62 29 29 3b 0a 09 6d 65 6d 63 70 79 28 62 75  _b));..memcpy(bu
5010: 66 66 65 72 2d 3e 65 74 61 67 2c 20 63 61 63 68  ffer->etag, cach
5020: 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f 66 28  e->etag, sizeof(
5030: 62 75 66 66 65 72 2d 3e 65 74 61 67 29 29 3b 0a  buffer->etag));.
5040: 09 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64  .buffer->lastmod
5050: 20 3d 20 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d   = buffer->lastm
5060: 6f 64 5f 62 20 2b 20 28 63 61 63 68 65 2d 3e 6c  od_b + (cache->l
5070: 61 73 74 6d 6f 64 20 2d 20 63 61 63 68 65 2d 3e  astmod - cache->
5080: 6c 61 73 74 6d 6f 64 5f 62 29 3b 0a 0a 09 70 74  lastmod_b);...pt
5090: 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f  hread_mutex_unlo
50a0: 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78  ck(&cache->mutex
50b0: 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75 66 66  );...return(buff
50c0: 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 72 6f 63 65  er);.}../* Proce
50d0: 73 73 20 61 6e 20 48 54 54 50 20 72 65 71 75 65  ss an HTTP reque
50e0: 73 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  st and return th
50f0: 65 20 70 61 74 68 20 72 65 71 75 65 73 74 65 64  e path requested
5100: 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63   */.static struc
5110: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71  t filed_http_req
5120: 75 65 73 74 20 2a 66 69 6c 65 64 5f 67 65 74 5f  uest *filed_get_
5130: 68 74 74 70 5f 72 65 71 75 65 73 74 28 46 49 4c  http_request(FIL
5140: 45 20 2a 66 70 2c 20 73 74 72 75 63 74 20 66 69  E *fp, struct fi
5150: 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 74  led_http_request
5160: 20 2a 62 75 66 66 65 72 5f 73 74 2c 20 73 74 72   *buffer_st, str
5170: 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e  uct filed_option
5180: 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 63  s *options) {..c
5190: 68 61 72 20 2a 6d 65 74 68 6f 64 2c 20 2a 70 61  har *method, *pa
51a0: 74 68 3b 0a 09 63 68 61 72 20 2a 62 75 66 66 65  th;..char *buffe
51b0: 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72 2c 20  r, *workbuffer, 
51c0: 2a 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74  *workbuffer_next
51d0: 3b 0a 09 63 68 61 72 20 2a 66 67 65 74 73 5f 72  ;..char *fgets_r
51e0: 65 74 3b 0a 09 73 69 7a 65 5f 74 20 62 75 66 66  et;..size_t buff
51f0: 65 72 5f 6c 65 6e 2c 20 70 61 74 68 5f 6c 65 6e  er_len, path_len
5200: 3b 0a 09 6f 66 66 5f 74 20 72 61 6e 67 65 5f 73  ;..off_t range_s
5210: 74 61 72 74 2c 20 72 61 6e 67 65 5f 65 6e 64 2c  tart, range_end,
5220: 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 09   range_length;..
5230: 69 6e 74 20 72 61 6e 67 65 5f 72 65 71 75 65 73  int range_reques
5240: 74 3b 0a 09 69 6e 74 20 73 6e 70 72 69 6e 74 66  t;..int snprintf
5250: 5f 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09  _ret;..int i;...
5260: 2f 2a 20 53 65 74 20 74 6f 20 64 65 66 61 75 6c  /* Set to defaul
5270: 74 20 76 61 6c 75 65 73 20 2a 2f 0a 09 72 61 6e  t values */..ran
5280: 67 65 5f 73 74 61 72 74 20 3d 20 30 3b 0a 09 72  ge_start = 0;..r
5290: 61 6e 67 65 5f 65 6e 64 20 20 20 3d 20 30 3b 0a  ange_end   = 0;.
52a0: 09 72 61 6e 67 65 5f 72 65 71 75 65 73 74 20 3d  .range_request =
52b0: 20 30 3b 0a 09 72 61 6e 67 65 5f 6c 65 6e 67 74   0;..range_lengt
52c0: 68 20 3d 20 2d 31 3b 0a 09 62 75 66 66 65 72 5f  h = -1;..buffer_
52d0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74  st->headers.host
52e0: 2e 70 72 65 73 65 6e 74 20 3d 20 30 3b 0a 09 62  .present = 0;..b
52f0: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72  uffer_st->header
5300: 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 46  s.connection = F
5310: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f  ILED_CONNECTION_
5320: 43 4c 4f 53 45 3b 0a 0a 09 62 75 66 66 65 72 20  CLOSE;...buffer 
5330: 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70  = buffer_st->tmp
5340: 62 75 66 3b 0a 09 62 75 66 66 65 72 5f 6c 65 6e  buf;..buffer_len
5350: 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 66 65 72   = sizeof(buffer
5360: 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a 09  _st->tmpbuf);...
5370: 66 67 65 74 73 5f 72 65 74 20 3d 20 66 67 65 74  fgets_ret = fget
5380: 73 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72  s(buffer, buffer
5390: 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 69 66 20 28  _len, fp);..if (
53a0: 66 67 65 74 73 5f 72 65 74 20 3d 3d 20 4e 55 4c  fgets_ret == NUL
53b0: 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55  L) {...return(NU
53c0: 4c 4c 29 3b 0a 09 7d 0a 0a 09 6d 65 74 68 6f 64  LL);..}...method
53d0: 20 3d 20 62 75 66 66 65 72 3b 0a 0a 09 62 75 66   = buffer;...buf
53e0: 66 65 72 20 3d 20 73 74 72 63 68 72 28 62 75 66  fer = strchr(buf
53f0: 66 65 72 2c 20 27 20 27 29 3b 0a 09 69 66 20 28  fer, ' ');..if (
5400: 62 75 66 66 65 72 20 3d 3d 20 4e 55 4c 4c 29 20  buffer == NULL) 
5410: 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29  {...return(NULL)
5420: 3b 0a 09 7d 0a 0a 09 2a 62 75 66 66 65 72 20 3d  ;..}...*buffer =
5430: 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 2b 2b   '\0';..buffer++
5440: 3b 0a 0a 09 70 61 74 68 20 3d 20 62 75 66 66 65  ;...path = buffe
5450: 72 3b 0a 0a 09 2f 2a 20 54 65 72 6d 69 6e 61 74  r;.../* Terminat
5460: 65 20 70 61 74 68 20 63 6f 6d 70 6f 6e 65 6e 74  e path component
5470: 20 2a 2f 0a 09 62 75 66 66 65 72 20 3d 20 73 74   */..buffer = st
5480: 72 70 62 72 6b 28 70 61 74 68 2c 20 22 5c 72 5c  rpbrk(path, "\r\
5490: 6e 20 22 29 3b 0a 09 69 66 20 28 62 75 66 66 65  n ");..if (buffe
54a0: 72 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a  r != NULL) {...*
54b0: 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09  buffer = '\0';..
54c0: 09 62 75 66 66 65 72 2b 2b 3b 0a 09 7d 0a 0a 09  .buffer++;..}...
54d0: 2f 2a 20 57 65 20 6f 6e 6c 79 20 68 61 6e 64 6c  /* We only handl
54e0: 65 20 74 68 65 20 22 47 45 54 22 20 61 6e 64 20  e the "GET" and 
54f0: 22 48 45 41 44 27 20 6d 65 74 68 6f 64 73 20 2a  "HEAD' methods *
5500: 2f 0a 09 69 66 20 28 73 74 72 63 61 73 65 63 6d  /..if (strcasecm
5510: 70 28 6d 65 74 68 6f 64 2c 20 22 68 65 61 64 22  p(method, "head"
5520: 29 20 21 3d 20 30 29 20 7b 0a 09 09 69 66 20 28  ) != 0) {...if (
5530: 73 74 72 63 61 73 65 63 6d 70 28 6d 65 74 68 6f  strcasecmp(metho
5540: 64 2c 20 22 67 65 74 22 29 20 21 3d 20 30 29 20  d, "get") != 0) 
5550: 7b 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c  {....return(NULL
5560: 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 47 45 54  );...}..../* GET
5570: 20 72 65 71 75 65 73 74 20 2a 2f 0a 09 09 62 75   request */...bu
5580: 66 66 65 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 20  ffer_st->method 
5590: 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f  = FILED_REQUEST_
55a0: 4d 45 54 48 4f 44 5f 47 45 54 3b 0a 09 7d 20 65  METHOD_GET;..} e
55b0: 6c 73 65 20 7b 0a 09 09 2f 2a 20 48 45 41 44 20  lse {.../* HEAD 
55c0: 72 65 71 75 65 73 74 20 2a 2f 0a 09 09 62 75 66  request */...buf
55d0: 66 65 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d  fer_st->method =
55e0: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d   FILED_REQUEST_M
55f0: 45 54 48 4f 44 5f 48 45 41 44 3b 0a 09 7d 0a 0a  ETHOD_HEAD;..}..
5600: 09 2f 2a 20 4e 6f 74 65 20 70 61 74 68 20 2a 2f  ./* Note path */
5610: 0a 09 70 61 74 68 5f 6c 65 6e 20 3d 20 73 74 72  ..path_len = str
5620: 6c 65 6e 28 70 61 74 68 29 3b 0a 09 6d 65 6d 63  len(path);..memc
5630: 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 70 61  py(buffer_st->pa
5640: 74 68 2c 20 70 61 74 68 2c 20 70 61 74 68 5f 6c  th, path, path_l
5650: 65 6e 20 2b 20 31 29 3b 0a 0a 09 2f 2a 20 44 65  en + 1);.../* De
5660: 74 65 72 6d 69 6e 65 20 74 79 70 65 20 6f 66 20  termine type of 
5670: 72 65 71 75 65 73 74 20 66 72 6f 6d 20 70 61 74  request from pat
5680: 68 20 2a 2f 0a 09 69 66 20 28 70 61 74 68 5f 6c  h */..if (path_l
5690: 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 62 75 66  en == 0) {...buf
56a0: 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46  fer_st->type = F
56b0: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50  ILED_REQUEST_TYP
56c0: 45 5f 44 49 52 45 43 54 4f 52 59 3b 0a 09 7d 20  E_DIRECTORY;..} 
56d0: 65 6c 73 65 20 7b 0a 09 09 69 66 20 28 70 61 74  else {...if (pat
56e0: 68 5b 70 61 74 68 5f 6c 65 6e 20 2d 20 31 5d 20  h[path_len - 1] 
56f0: 3d 3d 20 27 2f 27 29 20 7b 0a 09 09 09 62 75 66  == '/') {....buf
5700: 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46  fer_st->type = F
5710: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50  ILED_REQUEST_TYP
5720: 45 5f 44 49 52 45 43 54 4f 52 59 3b 0a 09 09 7d  E_DIRECTORY;...}
5730: 20 65 6c 73 65 20 7b 0a 09 09 09 62 75 66 66 65   else {....buffe
5740: 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c  r_st->type = FIL
5750: 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f  ED_REQUEST_TYPE_
5760: 4f 54 48 45 52 3b 0a 09 09 7d 0a 09 7d 0a 0a 09  OTHER;...}..}...
5770: 2f 2a 20 52 65 73 65 74 20 62 75 66 66 65 72 20  /* Reset buffer 
5780: 66 6f 72 20 6c 61 74 65 72 20 75 73 65 20 2a 2f  for later use */
5790: 0a 09 62 75 66 66 65 72 20 3d 20 62 75 66 66 65  ..buffer = buffe
57a0: 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a 0a 09  r_st->tmpbuf;...
57b0: 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 20 3c 20  for (i = 0; i < 
57c0: 31 30 30 3b 20 69 2b 2b 29 20 7b 0a 09 09 66 67  100; i++) {...fg
57d0: 65 74 73 5f 72 65 74 20 3d 20 66 67 65 74 73 28  ets_ret = fgets(
57e0: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c  buffer, buffer_l
57f0: 65 6e 2c 20 66 70 29 3b 0a 09 09 69 66 20 28 66  en, fp);...if (f
5800: 67 65 74 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c  gets_ret == NULL
5810: 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09  ) {....break;...
5820: 7d 0a 0a 09 09 69 66 20 28 73 74 72 6e 63 61 73  }....if (strncas
5830: 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22 52 61  ecmp(buffer, "Ra
5840: 6e 67 65 3a 20 22 2c 20 37 29 20 3d 3d 20 30 29  nge: ", 7) == 0)
5850: 20 7b 0a 09 09 09 77 6f 72 6b 62 75 66 66 65 72   {....workbuffer
5860: 20 3d 20 62 75 66 66 65 72 20 2b 20 37 3b 0a 0a   = buffer + 7;..
5870: 09 09 09 69 66 20 28 73 74 72 6e 63 61 73 65 63  ...if (strncasec
5880: 6d 70 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 22  mp(workbuffer, "
5890: 62 79 74 65 73 3d 22 2c 20 36 29 20 3d 3d 20 30  bytes=", 6) == 0
58a0: 29 20 7b 0a 09 09 09 09 77 6f 72 6b 62 75 66 66  ) {.....workbuff
58b0: 65 72 20 2b 3d 20 36 3b 0a 0a 09 09 09 09 72 61  er += 6;......ra
58c0: 6e 67 65 5f 72 65 71 75 65 73 74 20 3d 20 31 3b  nge_request = 1;
58d0: 0a 0a 09 09 09 09 72 61 6e 67 65 5f 73 74 61 72  ......range_star
58e0: 74 20 3d 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72  t = strtoull(wor
58f0: 6b 62 75 66 66 65 72 2c 20 26 77 6f 72 6b 62 75  kbuffer, &workbu
5900: 66 66 65 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a  ffer_next, 10);.
5910: 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20  .....workbuffer 
5920: 3d 20 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78  = workbuffer_nex
5930: 74 3b 0a 0a 09 09 09 09 69 66 20 28 2a 77 6f 72  t;......if (*wor
5940: 6b 62 75 66 66 65 72 20 3d 3d 20 27 2d 27 29 20  kbuffer == '-') 
5950: 7b 0a 09 09 09 09 09 77 6f 72 6b 62 75 66 66 65  {......workbuffe
5960: 72 2b 2b 3b 0a 0a 09 09 09 09 09 69 66 20 28 2a  r++;.......if (*
5970: 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 27 5c  workbuffer != '\
5980: 72 27 20 26 26 20 2a 77 6f 72 6b 62 75 66 66 65  r' && *workbuffe
5990: 72 20 21 3d 20 27 5c 6e 27 29 20 7b 0a 09 09 09  r != '\n') {....
59a0: 09 09 09 72 61 6e 67 65 5f 65 6e 64 20 3d 20 73  ...range_end = s
59b0: 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66  trtoull(workbuff
59c0: 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 72 5f  er, &workbuffer_
59d0: 6e 65 78 74 2c 20 31 30 29 3b 0a 09 09 09 09 09  next, 10);......
59e0: 7d 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d  }.....}....}...}
59f0: 20 65 6c 73 65 20 69 66 20 28 73 74 72 6e 63 61   else if (strnca
5a00: 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22 48  secmp(buffer, "H
5a10: 6f 73 74 3a 20 22 2c 20 35 29 20 3d 3d 20 30 29  ost: ", 5) == 0)
5a20: 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 74 2d   {....buffer_st-
5a30: 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72  >headers.host.pr
5a40: 65 73 65 6e 74 20 3d 20 31 3b 0a 0a 09 09 09 77  esent = 1;.....w
5a50: 6f 72 6b 62 75 66 66 65 72 20 3d 20 73 74 72 70  orkbuffer = strp
5a60: 62 72 6b 28 62 75 66 66 65 72 20 2b 20 35 2c 20  brk(buffer + 5, 
5a70: 22 5c 72 5c 6e 3a 22 29 3b 0a 09 09 09 69 66 20  "\r\n:");....if 
5a80: 28 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 4e  (workbuffer != N
5a90: 55 4c 4c 29 20 7b 0a 09 09 09 09 2a 77 6f 72 6b  ULL) {.....*work
5aa0: 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09  buffer = '\0';..
5ab0: 09 09 7d 0a 0a 09 09 09 77 6f 72 6b 62 75 66 66  ..}.....workbuff
5ac0: 65 72 20 3d 20 62 75 66 66 65 72 20 2b 20 35 3b  er = buffer + 5;
5ad0: 0a 09 09 09 77 68 69 6c 65 20 28 2a 77 6f 72 6b  ....while (*work
5ae0: 62 75 66 66 65 72 20 3d 3d 20 27 20 27 29 20 7b  buffer == ' ') {
5af0: 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 2b  .....workbuffer+
5b00: 2b 3b 0a 09 09 09 7d 0a 0a 09 09 09 73 74 72 63  +;....}.....strc
5b10: 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 68 65  py(buffer_st->he
5b20: 61 64 65 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c  aders.host.host,
5b30: 20 77 6f 72 6b 62 75 66 66 65 72 29 3b 0a 09 09   workbuffer);...
5b40: 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 6e 63  } else if (strnc
5b50: 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22  asecmp(buffer, "
5b60: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70  Connection: Keep
5b70: 2d 41 6c 69 76 65 22 2c 20 32 32 29 20 3d 3d 20  -Alive", 22) == 
5b80: 30 29 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73  0) {....buffer_s
5b90: 74 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65  t->headers.conne
5ba0: 63 74 69 6f 6e 20 3d 20 46 49 4c 45 44 5f 43 4f  ction = FILED_CO
5bb0: 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c  NNECTION_KEEP_AL
5bc0: 49 56 45 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28  IVE;...}....if (
5bd0: 6d 65 6d 63 6d 70 28 62 75 66 66 65 72 2c 20 22  memcmp(buffer, "
5be0: 5c 72 5c 6e 22 2c 20 32 29 20 3d 3d 20 30 29 20  \r\n", 2) == 0) 
5bf0: 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a  {....break;...}.
5c00: 09 7d 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69 6e  .}.../* Determin
5c10: 65 20 72 61 6e 67 65 20 2a 2f 0a 09 69 66 20 28  e range */..if (
5c20: 72 61 6e 67 65 5f 65 6e 64 20 21 3d 20 30 29 20  range_end != 0) 
5c30: 7b 0a 09 09 69 66 20 28 72 61 6e 67 65 5f 65 6e  {...if (range_en
5c40: 64 20 3c 3d 20 72 61 6e 67 65 5f 73 74 61 72 74  d <= range_start
5c50: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 4e 55  ) {....return(NU
5c60: 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 72 61 6e 67  LL);...}....rang
5c70: 65 5f 6c 65 6e 67 74 68 20 3d 20 72 61 6e 67 65  e_length = range
5c80: 5f 65 6e 64 20 2d 20 72 61 6e 67 65 5f 73 74 61  _end - range_sta
5c90: 72 74 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67  rt;....filed_log
5ca0: 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d 70  _msg_debug("Comp
5cb0: 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 70 61 72  uting length par
5cc0: 61 6d 65 74 65 72 3a 20 25 6c 6c 75 20 3d 20 25  ameter: %llu = %
5cd0: 6c 6c 75 20 2d 20 25 6c 6c 75 22 2c 0a 09 09 09  llu - %llu",....
5ce0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
5cf0: 6f 6e 67 29 20 72 61 6e 67 65 5f 6c 65 6e 67 74  ong) range_lengt
5d00: 68 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20  h,....(unsigned 
5d10: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65  long long) range
5d20: 5f 65 6e 64 2c 0a 09 09 09 28 75 6e 73 69 67 6e  _end,....(unsign
5d30: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61  ed long long) ra
5d40: 6e 67 65 5f 73 74 61 72 74 0a 09 09 29 3b 0a 09  nge_start...);..
5d50: 7d 0a 0a 09 2f 2a 20 46 69 6c 6c 20 75 70 20 73  }.../* Fill up s
5d60: 74 72 75 63 74 75 72 65 20 74 6f 20 72 65 74 75  tructure to retu
5d70: 72 6e 20 2a 2f 0a 09 62 75 66 66 65 72 5f 73 74  rn */..buffer_st
5d80: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
5d90: 70 72 65 73 65 6e 74 20 3d 20 72 61 6e 67 65 5f  present = range_
5da0: 72 65 71 75 65 73 74 3b 0a 09 62 75 66 66 65 72  request;..buffer
5db0: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  _st->headers.ran
5dc0: 67 65 2e 6f 66 66 73 65 74 20 20 3d 20 72 61 6e  ge.offset  = ran
5dd0: 67 65 5f 73 74 61 72 74 3b 0a 09 62 75 66 66 65  ge_start;..buffe
5de0: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  r_st->headers.ra
5df0: 6e 67 65 2e 6c 65 6e 67 74 68 20 20 3d 20 72 61  nge.length  = ra
5e00: 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 0a 09 2f 2a  nge_length;.../*
5e10: 20 49 66 20 76 68 6f 73 74 73 20 61 72 65 20 65   If vhosts are e
5e20: 6e 61 62 6c 65 64 2c 20 63 6f 6d 70 75 74 65 20  nabled, compute 
5e30: 6e 65 77 20 70 61 74 68 20 2a 2f 0a 09 69 66 20  new path */..if 
5e40: 28 6f 70 74 69 6f 6e 73 2d 3e 76 68 6f 73 74 73  (options->vhosts
5e50: 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 69 66  _enabled) {...if
5e60: 20 28 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61   (buffer_st->hea
5e70: 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e  ders.host.presen
5e80: 74 20 3d 3d 20 31 29 20 7b 0a 09 09 09 62 75 66  t == 1) {....buf
5e90: 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d  fer = buffer_st-
5ea0: 3e 74 6d 70 62 75 66 3b 0a 09 09 09 62 75 66 66  >tmpbuf;....buff
5eb0: 65 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28  er_len = sizeof(
5ec0: 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75  buffer_st->tmpbu
5ed0: 66 29 3b 0a 0a 09 09 09 73 6e 70 72 69 6e 74 66  f);.....snprintf
5ee0: 5f 72 65 74 20 3d 20 73 6e 70 72 69 6e 74 66 28  _ret = snprintf(
5ef0: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c  buffer, buffer_l
5f00: 65 6e 2c 20 22 2f 25 73 25 73 25 73 22 2c 0a 09  en, "/%s%s%s",..
5f10: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65  ...buffer_st->he
5f20: 61 64 65 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c  aders.host.host,
5f30: 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e  .....buffer_st->
5f40: 70 61 74 68 5b 30 5d 20 3d 3d 20 27 2f 27 20 3f  path[0] == '/' ?
5f50: 20 22 22 20 3a 20 22 2f 22 2c 0a 09 09 09 09 62   "" : "/",.....b
5f60: 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 0a 09  uffer_st->path..
5f70: 09 09 29 3b 0a 09 09 09 69 66 20 28 73 6e 70 72  ..);....if (snpr
5f80: 69 6e 74 66 5f 72 65 74 20 3e 3d 20 30 29 20 7b  intf_ret >= 0) {
5f90: 0a 09 09 09 09 69 66 20 28 28 28 75 6e 73 69 67  .....if (((unsig
5fa0: 6e 65 64 20 69 6e 74 29 20 73 6e 70 72 69 6e 74  ned int) snprint
5fb0: 66 5f 72 65 74 29 20 3c 20 62 75 66 66 65 72 5f  f_ret) < buffer_
5fc0: 6c 65 6e 29 20 7b 0a 09 09 09 09 09 73 74 72 63  len) {......strc
5fd0: 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 70 61  py(buffer_st->pa
5fe0: 74 68 2c 20 62 75 66 66 65 72 29 3b 0a 09 09 09  th, buffer);....
5ff0: 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a 0a  .}....}...}..}..
6000: 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 5f 73  .return(buffer_s
6010: 74 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e  t);.}../* Return
6020: 20 61 6e 20 65 72 72 6f 72 20 70 61 67 65 20 2a   an error page *
6030: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69  /.static void fi
6040: 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 46  led_error_page(F
6050: 49 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63  ILE *fp, const c
6060: 68 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 6e  har *date_curren
6070: 74 2c 20 69 6e 74 20 65 72 72 6f 72 5f 6e 75 6d  t, int error_num
6080: 62 65 72 2c 20 69 6e 74 20 6d 65 74 68 6f 64 2c  ber, int method,
6090: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 61   const char *rea
60a0: 73 6f 6e 2c 20 73 74 72 75 63 74 20 66 69 6c 65  son, struct file
60b0: 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67  d_log_entry *log
60c0: 29 20 7b 0a 09 63 68 61 72 20 2a 65 72 72 6f 72  ) {..char *error
60d0: 5f 73 74 72 69 6e 67 20 3d 20 22 3c 68 74 6d 6c  _string = "<html
60e0: 3e 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 45 52  ><head><title>ER
60f0: 52 4f 52 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61  ROR</title></hea
6100: 64 3e 3c 62 6f 64 79 3e 55 6e 61 62 6c 65 20 74  d><body>Unable t
6110: 6f 20 70 72 6f 63 65 73 73 20 72 65 71 75 65 73  o process reques
6120: 74 3c 2f 62 6f 64 79 3e 3c 2f 68 74 6d 6c 3e 22  t</body></html>"
6130: 3b 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20  ;...fprintf(fp, 
6140: 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4e 6f 74  "HTTP/1.1 %i Not
6150: 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c   OK\r\nDate: %s\
6160: 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c 65 64  r\nServer: filed
6170: 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65  \r\nLast-Modifie
6180: 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74  d: %s\r\nContent
6190: 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c  -Length: %llu\r\
61a0: 6e 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25  nContent-Type: %
61b0: 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a  s\r\nConnection:
61c0: 20 63 6c 6f 73 65 5c 72 5c 6e 5c 72 5c 6e 22 2c   close\r\n\r\n",
61d0: 0a 09 09 65 72 72 6f 72 5f 6e 75 6d 62 65 72 2c  ...error_number,
61e0: 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c  ...date_current,
61f0: 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c  ...date_current,
6200: 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
6210: 67 20 6c 6f 6e 67 29 20 73 74 72 6c 65 6e 28 65  g long) strlen(e
6220: 72 72 6f 72 5f 73 74 72 69 6e 67 29 2c 0a 09 09  rror_string),...
6230: 22 74 65 78 74 2f 68 74 6d 6c 22 0a 09 29 3b 0a  "text/html"..);.
6240: 0a 09 2f 2a 20 73 69 6c 65 6e 63 65 20 65 72 72  ../* silence err
6250: 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 48 45  or string for HE
6260: 41 44 20 72 65 71 75 65 73 74 73 20 2a 2f 0a 09  AD requests */..
6270: 69 66 20 28 6d 65 74 68 6f 64 20 21 3d 20 46 49  if (method != FI
6280: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48  LED_REQUEST_METH
6290: 4f 44 5f 48 45 41 44 29 20 7b 0a 09 09 66 70 72  OD_HEAD) {...fpr
62a0: 69 6e 74 66 28 66 70 2c 20 22 25 73 22 2c 20 65  intf(fp, "%s", e
62b0: 72 72 6f 72 5f 73 74 72 69 6e 67 29 3b 0a 09 7d  rror_string);..}
62c0: 0a 0a 09 2f 2a 20 4c 6f 67 20 65 72 72 6f 72 20  .../* Log error 
62d0: 2a 2f 0a 09 2f 2a 2a 20 72 65 61 73 6f 6e 20 6d  */../** reason m
62e0: 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 67  ust point to a g
62f0: 6c 6f 62 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  lobally allocate
6300: 64 20 76 61 6c 75 65 20 2a 2a 2f 0a 09 6c 6f 67  d value **/..log
6310: 2d 3e 72 65 61 73 6f 6e 20 3d 20 72 65 61 73 6f  ->reason = reaso
6320: 6e 3b 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f  n;..log->http_co
6330: 64 65 20 3d 20 65 72 72 6f 72 5f 6e 75 6d 62 65  de = error_numbe
6340: 72 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65  r;...filed_log_e
6350: 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20  ntry(log);.../* 
6360: 43 6c 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  Close connection
6370: 20 2a 2f 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65   */..filed_socke
6380: 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66  ttimeout_close(f
6390: 69 6c 65 6e 6f 28 66 70 29 29 3b 0a 0a 09 66 63  ileno(fp));...fc
63a0: 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 74 75  lose(fp);...retu
63b0: 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e  rn;.}../* Return
63c0: 20 61 20 72 65 64 69 72 65 63 74 20 74 6f 20 69   a redirect to i
63d0: 6e 64 65 78 2e 68 74 6d 6c 20 2a 2f 0a 73 74 61  ndex.html */.sta
63e0: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 72  tic void filed_r
63f0: 65 64 69 72 65 63 74 5f 69 6e 64 65 78 28 46 49  edirect_index(FI
6400: 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68  LE *fp, const ch
6410: 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 6e 74  ar *date_current
6420: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61  , const char *pa
6430: 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64  th, struct filed
6440: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 29  _log_entry *log)
6450: 20 7b 0a 09 69 6e 74 20 68 74 74 70 5f 63 6f 64   {..int http_cod
6460: 65 20 3d 20 33 30 31 3b 0a 09 66 70 72 69 6e 74  e = 301;..fprint
6470: 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 20  f(fp, "HTTP/1.1 
6480: 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25  %i OK\r\nDate: %
6490: 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c  s\r\nServer: fil
64a0: 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66  ed\r\nLast-Modif
64b0: 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65  ied: %s\r\nConte
64c0: 6e 74 2d 4c 65 6e 67 74 68 3a 20 30 5c 72 5c 6e  nt-Length: 0\r\n
64d0: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73  Connection: clos
64e0: 65 5c 72 5c 6e 4c 6f 63 61 74 69 6f 6e 3a 20 25  e\r\nLocation: %
64f0: 73 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 68 74  s\r\n\r\n",...ht
6500: 74 70 5f 63 6f 64 65 2c 0a 09 09 64 61 74 65 5f  tp_code,...date_
6510: 63 75 72 72 65 6e 74 2c 0a 09 09 64 61 74 65 5f  current,...date_
6520: 63 75 72 72 65 6e 74 2c 0a 09 09 22 69 6e 64 65  current,..."inde
6530: 78 2e 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f 2a  x.html"..);.../*
6540: 20 4c 6f 67 20 72 65 64 69 72 65 63 74 20 2a 2f   Log redirect */
6550: 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20  ..log->reason = 
6560: 22 72 65 64 69 72 65 63 74 22 3b 0a 09 6c 6f 67  "redirect";..log
6570: 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 68 74  ->http_code = ht
6580: 74 70 5f 63 6f 64 65 3b 0a 0a 09 66 69 6c 65 64  tp_code;...filed
6590: 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b  _log_entry(log);
65a0: 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20 63 6f 6e 6e  .../* Close conn
65b0: 65 63 74 69 6f 6e 20 2a 2f 0a 09 66 69 6c 65 64  ection */..filed
65c0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63  _sockettimeout_c
65d0: 6c 6f 73 65 28 66 69 6c 65 6e 6f 28 66 70 29 29  lose(fileno(fp))
65e0: 3b 0a 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a  ;...fclose(fp);.
65f0: 0a 09 72 65 74 75 72 6e 3b 0a 0a 09 2f 2a 20 43  ..return;.../* C
6600: 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 3a  urrently unused:
6610: 20 70 61 74 68 20 2a 2f 0a 09 70 61 74 68 20 3d   path */..path =
6620: 20 70 61 74 68 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e   path;.}../* Con
6630: 76 65 72 74 20 61 6e 20 65 6e 75 6d 20 72 65 70  vert an enum rep
6640: 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22 43  resenting the "C
6650: 6f 6e 6e 65 63 74 69 6f 6e 22 20 68 65 61 64 65  onnection" heade
6660: 72 20 76 61 6c 75 65 20 74 6f 20 61 20 73 74 72  r value to a str
6670: 69 6e 67 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  ing */.static co
6680: 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f  nst char *filed_
6690: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 28 69  connection_str(i
66a0: 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61  nt connection_va
66b0: 6c 75 65 29 20 7b 0a 09 73 77 69 74 63 68 20 28  lue) {..switch (
66c0: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c 75 65  connection_value
66d0: 29 20 7b 0a 09 09 63 61 73 65 20 46 49 4c 45 44  ) {...case FILED
66e0: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53  _CONNECTION_CLOS
66f0: 45 3a 0a 09 09 09 72 65 74 75 72 6e 28 22 63 6c  E:....return("cl
6700: 6f 73 65 22 29 3b 0a 09 09 63 61 73 65 20 46 49  ose");...case FI
6710: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b  LED_CONNECTION_K
6720: 45 45 50 5f 41 4c 49 56 45 3a 0a 09 09 09 72 65  EEP_ALIVE:....re
6730: 74 75 72 6e 28 22 6b 65 65 70 2d 61 6c 69 76 65  turn("keep-alive
6740: 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28  ");..}...return(
6750: 22 63 6c 6f 73 65 22 29 3b 0a 7d 0a 0a 2f 2a 20  "close");.}../* 
6760: 48 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c 65 20  Handle a single 
6770: 72 65 71 75 65 73 74 20 66 72 6f 6d 20 61 20 63  request from a c
6780: 6c 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 63 20  lient */.static 
6790: 69 6e 74 20 66 69 6c 65 64 5f 68 61 6e 64 6c 65  int filed_handle
67a0: 5f 63 6c 69 65 6e 74 28 69 6e 74 20 66 64 2c 20  _client(int fd, 
67b0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74  struct filed_htt
67c0: 70 5f 72 65 71 75 65 73 74 20 2a 72 65 71 75 65  p_request *reque
67d0: 73 74 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64  st, struct filed
67e0: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 2c  _log_entry *log,
67f0: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70   struct filed_op
6800: 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20  tions *options) 
6810: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f  {..struct filed_
6820: 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 69 6e  fileinfo *filein
6830: 66 6f 3b 0a 09 73 73 69 7a 65 5f 74 20 73 65 6e  fo;..ssize_t sen
6840: 64 66 69 6c 65 5f 72 65 74 3b 0a 09 73 69 7a 65  dfile_ret;..size
6850: 5f 74 20 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65  _t sendfile_size
6860: 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 64 66 69 6c  ;..off_t sendfil
6870: 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 66 69  e_offset, sendfi
6880: 6c 65 5f 73 65 6e 74 2c 20 73 65 6e 64 66 69 6c  le_sent, sendfil
6890: 65 5f 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 70 61  e_len;..char *pa
68a0: 74 68 3b 0a 09 63 68 61 72 20 2a 64 61 74 65 5f  th;..char *date_
68b0: 63 75 72 72 65 6e 74 2c 20 64 61 74 65 5f 63 75  current, date_cu
68c0: 72 72 65 6e 74 5f 62 5b 36 34 5d 3b 0a 09 69 6e  rrent_b[64];..in
68d0: 74 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 46 49  t http_code;..FI
68e0: 4c 45 20 2a 66 70 3b 0a 0a 09 2f 2a 20 44 65 74  LE *fp;.../* Det
68f0: 65 72 6d 69 6e 65 20 63 75 72 72 65 6e 74 20 74  ermine current t
6900: 69 6d 65 20 2a 2f 0a 09 64 61 74 65 5f 63 75 72  ime */..date_cur
6910: 72 65 6e 74 20 3d 20 66 69 6c 65 64 5f 66 6f 72  rent = filed_for
6920: 6d 61 74 5f 74 69 6d 65 28 64 61 74 65 5f 63 75  mat_time(date_cu
6930: 72 72 65 6e 74 5f 62 2c 20 73 69 7a 65 6f 66 28  rrent_b, sizeof(
6940: 64 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 29 2c  date_current_b),
6950: 20 74 69 6d 65 28 4e 55 4c 4c 29 29 3b 0a 0a 09   time(NULL));...
6960: 2f 2a 20 4f 70 65 6e 20 73 6f 63 6b 65 74 20 61  /* Open socket a
6970: 73 20 41 4e 53 49 20 49 2f 4f 20 66 6f 72 20 65  s ANSI I/O for e
6980: 61 73 65 20 6f 66 20 75 73 65 20 2a 2f 0a 09 66  ase of use */..f
6990: 70 20 3d 20 66 64 6f 70 65 6e 28 66 64 2c 20 22  p = fdopen(fd, "
69a0: 77 2b 62 22 29 3b 0a 09 69 66 20 28 66 70 20 3d  w+b");..if (fp =
69b0: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65  = NULL) {...file
69c0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
69d0: 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 63 6c  close(fd);....cl
69e0: 6f 73 65 28 66 64 29 3b 0a 0a 09 09 6c 6f 67 2d  ose(fd);....log-
69f0: 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30  >buffer[0] = '\0
6a00: 27 3b 0a 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63  ';...log->http_c
6a10: 6f 64 65 20 3d 20 2d 31 3b 0a 09 09 6c 6f 67 2d  ode = -1;...log-
6a20: 3e 72 65 61 73 6f 6e 20 3d 20 22 66 64 6f 70 65  >reason = "fdope
6a30: 6e 5f 66 61 69 6c 65 64 22 3b 0a 0a 09 09 66 69  n_failed";....fi
6a40: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f  led_log_entry(lo
6a50: 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49  g);....return(FI
6a60: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43  LED_CONNECTION_C
6a70: 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 72 65 71 75  LOSE);..}...requ
6a80: 65 73 74 20 3d 20 66 69 6c 65 64 5f 67 65 74 5f  est = filed_get_
6a90: 68 74 74 70 5f 72 65 71 75 65 73 74 28 66 70 2c  http_request(fp,
6aa0: 20 72 65 71 75 65 73 74 2c 20 6f 70 74 69 6f 6e   request, option
6ab0: 73 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 65 73  s);...if (reques
6ac0: 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 6c  t == NULL) {...l
6ad0: 6f 67 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20  og->buffer[0] = 
6ae0: 27 5c 30 27 3b 0a 0a 09 09 66 69 6c 65 64 5f 65  '\0';....filed_e
6af0: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61  rror_page(fp, da
6b00: 74 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 30 2c  te_current, 500,
6b10: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d   FILED_REQUEST_M
6b20: 45 54 48 4f 44 5f 47 45 54 2c 20 22 66 6f 72 6d  ETHOD_GET, "form
6b30: 61 74 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65  at", log);....re
6b40: 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45  turn(FILED_CONNE
6b50: 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d  CTION_CLOSE);..}
6b60: 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  ...filed_sockett
6b70: 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e  imeout_processin
6b80: 67 5f 73 74 61 72 74 28 66 64 29 3b 0a 0a 09 70  g_start(fd);...p
6b90: 61 74 68 20 3d 20 72 65 71 75 65 73 74 2d 3e 70  ath = request->p
6ba0: 61 74 68 3b 0a 09 73 74 72 63 70 79 28 6c 6f 67  ath;..strcpy(log
6bb0: 2d 3e 62 75 66 66 65 72 2c 20 70 61 74 68 29 3b  ->buffer, path);
6bc0: 0a 09 6c 6f 67 2d 3e 6d 65 74 68 6f 64 20 3d 20  ..log->method = 
6bd0: 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 3b  request->method;
6be0: 0a 0a 09 2f 2a 20 49 66 20 74 68 65 20 72 65 71  .../* If the req
6bf0: 75 65 73 74 65 64 20 70 61 74 68 20 69 73 20 61  uested path is a
6c00: 20 64 69 72 65 63 74 6f 72 79 2c 20 72 65 64 69   directory, redi
6c10: 72 65 63 74 20 74 6f 20 69 6e 64 65 78 20 70 61  rect to index pa
6c20: 67 65 20 2a 2f 0a 09 69 66 20 28 72 65 71 75 65  ge */..if (reque
6c30: 73 74 2d 3e 74 79 70 65 20 3d 3d 20 46 49 4c 45  st->type == FILE
6c40: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44  D_REQUEST_TYPE_D
6c50: 49 52 45 43 54 4f 52 59 29 20 7b 0a 09 09 66 69  IRECTORY) {...fi
6c60: 6c 65 64 5f 72 65 64 69 72 65 63 74 5f 69 6e 64  led_redirect_ind
6c70: 65 78 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72  ex(fp, date_curr
6c80: 65 6e 74 2c 20 70 61 74 68 2c 20 6c 6f 67 29 3b  ent, path, log);
6c90: 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44  ....return(FILED
6ca0: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53  _CONNECTION_CLOS
6cb0: 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 69 6e 66  E);..}...fileinf
6cc0: 6f 20 3d 20 66 69 6c 65 64 5f 6f 70 65 6e 5f 66  o = filed_open_f
6cd0: 69 6c 65 28 70 61 74 68 2c 20 26 72 65 71 75 65  ile(path, &reque
6ce0: 73 74 2d 3e 66 69 6c 65 69 6e 66 6f 29 3b 0a 09  st->fileinfo);..
6cf0: 69 66 20 28 66 69 6c 65 69 6e 66 6f 20 3d 3d 20  if (fileinfo == 
6d00: 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f  NULL) {...filed_
6d10: 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64  error_page(fp, d
6d20: 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 30 34  ate_current, 404
6d30: 2c 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f  , request->metho
6d40: 64 2c 20 22 6f 70 65 6e 5f 66 61 69 6c 65 64 22  d, "open_failed"
6d50: 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72  , log);....retur
6d60: 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49  n(FILED_CONNECTI
6d70: 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09  ON_CLOSE);..}...
6d80: 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61  if (request->hea
6d90: 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 73 65  ders.range.prese
6da0: 6e 74 29 20 7b 0a 09 09 69 66 20 28 72 65 71 75  nt) {...if (requ
6db0: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
6dc0: 67 65 2e 6f 66 66 73 65 74 20 21 3d 20 30 20 7c  ge.offset != 0 |
6dd0: 7c 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65  | request->heade
6de0: 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20  rs.range.length 
6df0: 3e 3d 20 30 29 20 7b 0a 09 09 09 69 66 20 28 72  >= 0) {....if (r
6e00: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
6e10: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3e 3d 20  range.offset >= 
6e20: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 29 20 7b  fileinfo->len) {
6e30: 0a 09 09 09 09 66 69 6c 65 64 5f 65 72 72 6f 72  .....filed_error
6e40: 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f 63  _page(fp, date_c
6e50: 75 72 72 65 6e 74 2c 20 34 31 36 2c 20 72 65 71  urrent, 416, req
6e60: 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c 20 22 72  uest->method, "r
6e70: 61 6e 67 65 5f 69 6e 76 61 6c 69 64 22 2c 20 6c  ange_invalid", l
6e80: 6f 67 29 3b 0a 0a 09 09 09 09 63 6c 6f 73 65 28  og);......close(
6e90: 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a  fileinfo->fd);..
6ea0: 09 09 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44  ....return(FILED
6eb0: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53  _CONNECTION_CLOS
6ec0: 45 29 3b 0a 09 09 09 7d 0a 0a 09 09 09 69 66 20  E);....}.....if 
6ed0: 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72  (request->header
6ee0: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d  s.range.length =
6ef0: 3d 20 28 28 6f 66 66 5f 74 29 20 2d 31 29 29 20  = ((off_t) -1)) 
6f00: 7b 0a 09 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f  {.....filed_log_
6f10: 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d 70 75  msg_debug("Compu
6f20: 74 69 6e 67 20 6c 65 6e 67 74 68 20 74 6f 20 66  ting length to f
6f30: 69 74 20 69 6e 20 62 6f 75 6e 64 73 3a 20 66 69  it in bounds: fi
6f40: 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20 3d 20 25 6c  leinfo->len = %l
6f50: 6c 75 2c 20 72 65 71 75 65 73 74 2d 3e 68 65 61  lu, request->hea
6f60: 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65  ders.range.offse
6f70: 74 20 3d 20 25 6c 6c 75 22 2c 0a 09 09 09 09 09  t = %llu",......
6f80: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
6f90: 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c  ong) fileinfo->l
6fa0: 65 6e 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 6e  en,......(unsign
6fb0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65  ed long long) re
6fc0: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
6fd0: 61 6e 67 65 2e 6f 66 66 73 65 74 0a 09 09 09 09  ange.offset.....
6fe0: 29 3b 0a 0a 09 09 09 09 72 65 71 75 65 73 74 2d  );......request-
6ff0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c  >headers.range.l
7000: 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f  ength = fileinfo
7010: 2d 3e 6c 65 6e 20 2d 20 72 65 71 75 65 73 74 2d  ->len - request-
7020: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f  >headers.range.o
7030: 66 66 73 65 74 3b 0a 09 09 09 7d 0a 0a 09 09 09  ffset;....}.....
7040: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65  filed_log_msg_de
7050: 62 75 67 28 22 50 61 72 74 69 61 6c 20 72 65 71  bug("Partial req
7060: 75 65 73 74 2c 20 73 74 61 72 74 69 6e 67 20 61  uest, starting a
7070: 74 3a 20 25 6c 6c 75 20 61 6e 64 20 72 75 6e 6e  t: %llu and runn
7080: 69 6e 67 20 66 6f 72 20 25 6c 6c 69 20 62 79 74  ing for %lli byt
7090: 65 73 22 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e  es",.....(unsign
70a0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65  ed long long) re
70b0: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
70c0: 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09  ange.offset,....
70d0: 09 28 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71  .(long long) req
70e0: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  uest->headers.ra
70f0: 6e 67 65 2e 6c 65 6e 67 74 68 0a 09 09 09 29 3b  nge.length....);
7100: 0a 0a 09 09 7d 0a 0a 09 09 68 74 74 70 5f 63 6f  ....}....http_co
7110: 64 65 20 3d 20 32 30 36 3b 0a 09 7d 20 65 6c 73  de = 206;..} els
7120: 65 20 7b 0a 09 09 68 74 74 70 5f 63 6f 64 65 20  e {...http_code 
7130: 3d 20 32 30 30 3b 0a 0a 09 09 2f 2a 20 43 6f 6d  = 200;..../* Com
7140: 70 75 74 65 20 66 61 6b 65 20 72 61 6e 67 65 20  pute fake range 
7150: 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20  parameters that 
7160: 69 6e 63 6c 75 64 65 73 20 74 68 65 20 65 6e 74  includes the ent
7170: 69 72 65 20 66 69 6c 65 20 2a 2f 0a 09 09 72 65  ire file */...re
7180: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
7190: 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d 20 30 3b  ange.offset = 0;
71a0: 0a 09 09 72 65 71 75 65 73 74 2d 3e 68 65 61 64  ...request->head
71b0: 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68  ers.range.length
71c0: 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e   = fileinfo->len
71d0: 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74 66 28 66  ;..}...fprintf(f
71e0: 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20  p, "HTTP/1.1 %i 
71f0: 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72  OK\r\nDate: %s\r
7200: 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c  \nServer: filed\
7210: 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64  r\nLast-Modified
7220: 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d  : %s\r\nContent-
7230: 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e  Length: %llu\r\n
7240: 41 63 63 65 70 74 2d 52 61 6e 67 65 73 3a 20 62  Accept-Ranges: b
7250: 79 74 65 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d  ytes\r\nContent-
7260: 54 79 70 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e  Type: %s\r\nConn
7270: 65 63 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e 45 54  ection: %s\r\nET
7280: 61 67 3a 20 5c 22 25 73 5c 22 5c 72 5c 6e 22 2c  ag: \"%s\"\r\n",
7290: 0a 09 09 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09  ...http_code,...
72a0: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09  date_current,...
72b0: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d 6f  fileinfo->lastmo
72c0: 64 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c  d,...(unsigned l
72d0: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73  ong long) reques
72e0: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65  t->headers.range
72f0: 2e 6c 65 6e 67 74 68 2c 0a 09 09 66 69 6c 65 69  .length,...filei
7300: 6e 66 6f 2d 3e 74 79 70 65 2c 0a 09 09 66 69 6c  nfo->type,...fil
7310: 65 64 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74  ed_connection_st
7320: 72 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65  r(request->heade
7330: 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 29 2c 0a  rs.connection),.
7340: 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 65 74 61 67  ..fileinfo->etag
7350: 0a 09 29 3b 0a 0a 09 69 66 20 28 68 74 74 70 5f  ..);...if (http_
7360: 63 6f 64 65 20 3d 3d 20 32 30 36 29 20 7b 0a 09  code == 206) {..
7370: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 43 6f  .fprintf(fp, "Co
7380: 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20 62 79 74  ntent-Range: byt
7390: 65 73 20 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c 6c  es %llu-%llu/%ll
73a0: 75 5c 72 5c 6e 22 2c 0a 09 09 09 28 75 6e 73 69  u\r\n",....(unsi
73b0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
73c0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
73d0: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09  .range.offset,..
73e0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
73f0: 20 6c 6f 6e 67 29 20 28 72 65 71 75 65 73 74 2d   long) (request-
7400: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f  >headers.range.o
7410: 66 66 73 65 74 20 2b 20 72 65 71 75 65 73 74 2d  ffset + request-
7420: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c  >headers.range.l
7430: 65 6e 67 74 68 20 2d 20 31 29 2c 0a 09 09 09 28  ength - 1),....(
7440: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
7450: 6e 67 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65  ng) fileinfo->le
7460: 6e 0a 09 09 29 3b 0a 09 7d 0a 09 66 70 72 69 6e  n...);..}..fprin
7470: 74 66 28 66 70 2c 20 22 5c 72 5c 6e 22 29 3b 0a  tf(fp, "\r\n");.
7480: 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 6c  .fflush(fp);...l
7490: 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20  og->http_code = 
74a0: 68 74 74 70 5f 63 6f 64 65 3b 0a 09 6c 6f 67 2d  http_code;..log-
74b0: 3e 72 65 61 73 6f 6e 20 3d 20 22 4f 4b 22 3b 0a  >reason = "OK";.
74c0: 09 6c 6f 67 2d 3e 73 74 61 72 74 74 69 6d 65 20  .log->starttime 
74d0: 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 09 6c  = time(NULL);..l
74e0: 6f 67 2d 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d  og->req_offset =
74f0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72   request->header
7500: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a  s.range.offset;.
7510: 09 6c 6f 67 2d 3e 72 65 71 5f 6c 65 6e 67 74 68  .log->req_length
7520: 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64   = request->head
7530: 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68  ers.range.length
7540: 3b 0a 09 6c 6f 67 2d 3e 66 69 6c 65 5f 6c 65 6e  ;..log->file_len
7550: 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e  gth = fileinfo->
7560: 6c 65 6e 3b 0a 0a 23 69 66 64 65 66 20 46 49 4c  len;..#ifdef FIL
7570: 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50  ED_NONBLOCK_HTTP
7580: 0a 09 69 6e 74 20 73 6f 63 6b 65 74 5f 66 6c 61  ..int socket_fla
7590: 67 73 3b 0a 09 66 64 5f 73 65 74 20 72 66 64 2c  gs;..fd_set rfd,
75a0: 20 77 66 64 3b 0a 09 63 68 61 72 20 73 69 6e 6b   wfd;..char sink
75b0: 62 75 66 5b 38 31 39 32 5d 3b 0a 09 73 73 69 7a  buf[8192];..ssiz
75c0: 65 5f 74 20 72 65 61 64 5f 72 65 74 3b 0a 0a 09  e_t read_ret;...
75d0: 46 44 5f 5a 45 52 4f 28 26 72 66 64 29 3b 0a 09  FD_ZERO(&rfd);..
75e0: 46 44 5f 5a 45 52 4f 28 26 77 66 64 29 3b 0a 09  FD_ZERO(&wfd);..
75f0: 46 44 5f 53 45 54 28 66 64 2c 20 26 72 66 64 29  FD_SET(fd, &rfd)
7600: 3b 0a 09 46 44 5f 53 45 54 28 66 64 2c 20 26 77  ;..FD_SET(fd, &w
7610: 66 64 29 3b 0a 0a 09 73 6f 63 6b 65 74 5f 66 6c  fd);...socket_fl
7620: 61 67 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20  ags = fcntl(fd, 
7630: 46 5f 47 45 54 46 4c 29 3b 0a 09 66 63 6e 74 6c  F_GETFL);..fcntl
7640: 28 66 64 2c 20 46 5f 53 45 54 46 4c 2c 20 73 6f  (fd, F_SETFL, so
7650: 63 6b 65 74 5f 66 6c 61 67 73 20 7c 20 4f 5f 4e  cket_flags | O_N
7660: 4f 4e 42 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66  ONBLOCK);.#endif
7670: 0a 09 73 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65  ..sendfile_offse
7680: 74 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61  t = request->hea
7690: 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65  ders.range.offse
76a0: 74 3b 0a 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e  t;..sendfile_len
76b0: 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64   = request->head
76c0: 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68  ers.range.length
76d0: 3b 0a 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74  ;..sendfile_sent
76e0: 20 3d 20 30 3b 0a 09 77 68 69 6c 65 20 28 72 65   = 0;..while (re
76f0: 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 3d  quest->method ==
7700: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d   FILED_REQUEST_M
7710: 45 54 48 4f 44 5f 47 45 54 29 20 7b 0a 09 09 69  ETHOD_GET) {...i
7720: 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20  f (sendfile_len 
7730: 3e 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45  > FILED_SENDFILE
7740: 5f 4d 41 58 29 20 7b 0a 09 09 09 73 65 6e 64 66  _MAX) {....sendf
7750: 69 6c 65 5f 73 69 7a 65 20 3d 20 46 49 4c 45 44  ile_size = FILED
7760: 5f 53 45 4e 44 46 49 4c 45 5f 4d 41 58 3b 0a 09  _SENDFILE_MAX;..
7770: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 73 65 6e  .} else {....sen
7780: 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 73 65 6e  dfile_size = sen
7790: 64 66 69 6c 65 5f 6c 65 6e 3b 0a 09 09 7d 0a 0a  dfile_len;...}..
77a0: 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d  ..sendfile_ret =
77b0: 20 73 65 6e 64 66 69 6c 65 28 66 64 2c 20 66 69   sendfile(fd, fi
77c0: 6c 65 69 6e 66 6f 2d 3e 66 64 2c 20 26 73 65 6e  leinfo->fd, &sen
77d0: 64 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65  dfile_offset, se
77e0: 6e 64 66 69 6c 65 5f 73 69 7a 65 29 3b 0a 09 09  ndfile_size);...
77f0: 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 72 65 74  if (sendfile_ret
7800: 20 3c 3d 20 30 29 20 7b 0a 23 69 66 64 65 66 20   <= 0) {.#ifdef 
7810: 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48  FILED_NONBLOCK_H
7820: 54 54 50 0a 09 09 09 69 66 20 28 65 72 72 6e 6f  TTP....if (errno
7830: 20 3d 3d 20 45 41 47 41 49 4e 29 20 7b 0a 09 09   == EAGAIN) {...
7840: 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d  ..sendfile_ret =
7850: 20 30 3b 0a 0a 09 09 09 09 77 68 69 6c 65 20 28   0;......while (
7860: 31 29 20 7b 0a 09 09 09 09 09 73 65 6c 65 63 74  1) {......select
7870: 28 66 64 20 2b 20 31 2c 20 26 72 66 64 2c 20 26  (fd + 1, &rfd, &
7880: 77 66 64 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29  wfd, NULL, NULL)
7890: 3b 0a 09 09 09 09 09 69 66 20 28 46 44 5f 49 53  ;......if (FD_IS
78a0: 53 45 54 28 66 64 2c 20 26 72 66 64 29 29 20 7b  SET(fd, &rfd)) {
78b0: 0a 09 09 09 09 09 09 72 65 61 64 5f 72 65 74 20  .......read_ret 
78c0: 3d 20 72 65 61 64 28 66 64 2c 20 73 69 6e 6b 62  = read(fd, sinkb
78d0: 75 66 2c 20 73 69 7a 65 6f 66 28 73 69 6e 6b 62  uf, sizeof(sinkb
78e0: 75 66 29 29 3b 0a 0a 09 09 09 09 09 09 69 66 20  uf));........if 
78f0: 28 72 65 61 64 5f 72 65 74 20 3c 3d 20 30 29 20  (read_ret <= 0) 
7900: 7b 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a  {........break;.
7910: 09 09 09 09 09 09 7d 0a 09 09 09 09 09 7d 0a 0a  ......}......}..
7920: 09 09 09 09 09 69 66 20 28 46 44 5f 49 53 53 45  .....if (FD_ISSE
7930: 54 28 66 64 2c 20 26 77 66 64 29 29 20 7b 0a 09  T(fd, &wfd)) {..
7940: 09 09 09 09 09 72 65 61 64 5f 72 65 74 20 3d 20  .....read_ret = 
7950: 31 3b 0a 0a 09 09 09 09 09 09 62 72 65 61 6b 3b  1;........break;
7960: 0a 09 09 09 09 09 7d 0a 09 09 09 09 7d 0a 0a 09  ......}.....}...
7970: 09 09 09 69 66 20 28 72 65 61 64 5f 72 65 74 20  ...if (read_ret 
7980: 3c 3d 20 30 29 20 7b 0a 09 09 09 09 09 62 72 65  <= 0) {......bre
7990: 61 6b 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 20 65  ak;.....}....} e
79a0: 6c 73 65 20 7b 0a 09 09 09 09 62 72 65 61 6b 3b  lse {.....break;
79b0: 0a 09 09 09 7d 0a 23 65 6c 73 65 0a 09 09 09 62  ....}.#else....b
79c0: 72 65 61 6b 3b 0a 23 65 6e 64 69 66 0a 09 09 7d  reak;.#endif...}
79d0: 0a 0a 09 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e  ....sendfile_len
79e0: 20 2d 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 74   -= sendfile_ret
79f0: 3b 0a 09 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e  ;...sendfile_sen
7a00: 74 20 2b 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65  t += sendfile_re
7a10: 74 3b 0a 09 09 69 66 20 28 73 65 6e 64 66 69 6c  t;...if (sendfil
7a20: 65 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09  e_len == 0) {...
7a30: 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a  .break;...}..}..
7a40: 09 6c 6f 67 2d 3e 65 6e 64 74 69 6d 65 20 3d 20  .log->endtime = 
7a50: 28 74 69 6d 65 5f 74 29 20 2d 31 3b 0a 09 6c 6f  (time_t) -1;..lo
7a60: 67 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 20 3d  g->sent_length =
7a70: 20 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 3b 0a   sendfile_sent;.
7a80: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72  ..filed_log_entr
7a90: 79 28 6c 6f 67 29 3b 0a 0a 09 63 6c 6f 73 65 28  y(log);...close(
7aa0: 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a  fileinfo->fd);..
7ab0: 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65  .if (request->he
7ac0: 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e  aders.connection
7ad0: 20 21 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43   != FILED_CONNEC
7ae0: 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 29  TION_KEEP_ALIVE)
7af0: 20 7b 0a 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65   {...filed_socke
7b00: 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66  ttimeout_close(f
7b10: 64 29 3b 0a 0a 09 09 66 63 6c 6f 73 65 28 66 70  d);....fclose(fp
7b20: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c  );....return(FIL
7b30: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c  ED_CONNECTION_CL
7b40: 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64  OSE);..}...filed
7b50: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70  _sockettimeout_p
7b60: 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64 28 66 64  rocessing_end(fd
7b70: 29 3b 0a 0a 09 72 65 74 75 72 6e 28 46 49 4c 45  );...return(FILE
7b80: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45  D_CONNECTION_KEE
7b90: 50 5f 41 4c 49 56 45 29 3b 0a 7d 0a 0a 2f 2a 20  P_ALIVE);.}../* 
7ba0: 48 61 6e 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 20  Handle incoming 
7bb0: 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 73  connections */.s
7bc0: 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 6c 65  tatic void *file
7bd0: 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 28  d_worker_thread(
7be0: 76 6f 69 64 20 2a 61 72 67 5f 76 29 20 7b 0a 09  void *arg_v) {..
7bf0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 6f 72  struct filed_wor
7c00: 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 73 20  ker_thread_args 
7c10: 2a 61 72 67 3b 0a 09 73 74 72 75 63 74 20 66 69  *arg;..struct fi
7c20: 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 74  led_http_request
7c30: 20 72 65 71 75 65 73 74 3b 0a 09 73 74 72 75 63   request;..struc
7c40: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72  t filed_log_entr
7c50: 79 20 2a 6c 6f 67 2c 20 6c 6f 63 61 6c 5f 64 75  y *log, local_du
7c60: 6d 6d 79 5f 6c 6f 67 3b 0a 09 73 74 72 75 63 74  mmy_log;..struct
7c70: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a   filed_options *
7c80: 6f 70 74 69 6f 6e 73 3b 0a 09 73 74 72 75 63 74  options;..struct
7c90: 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 64   sockaddr_in6 ad
7ca0: 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 61  dr;..socklen_t a
7cb0: 64 64 72 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 69  ddrlen;..int fai
7cc0: 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20 30 2c 20  lure_count = 0, 
7cd0: 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e  max_failure_coun
7ce0: 74 20 3d 20 46 49 4c 45 44 5f 4d 41 58 5f 46 41  t = FILED_MAX_FA
7cf0: 49 4c 55 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 6e  ILURE_COUNT;..in
7d00: 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61  t connection_sta
7d10: 74 65 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45  te = FILED_CONNE
7d20: 43 54 49 4f 4e 5f 43 4c 4f 53 45 3b 0a 09 69 6e  CTION_CLOSE;..in
7d30: 74 20 6d 61 73 74 65 72 5f 66 64 2c 20 66 64 20  t master_fd, fd 
7d40: 3d 20 2d 31 3b 0a 0a 09 2f 2a 20 52 65 61 64 20  = -1;.../* Read 
7d50: 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 72  arguments */..ar
7d60: 67 20 3d 20 61 72 67 5f 76 3b 0a 0a 09 6d 61 73  g = arg_v;...mas
7d70: 74 65 72 5f 66 64 20 3d 20 61 72 67 2d 3e 66 64  ter_fd = arg->fd
7d80: 3b 0a 09 6f 70 74 69 6f 6e 73 20 3d 20 26 61 72  ;..options = &ar
7d90: 67 2d 3e 6f 70 74 69 6f 6e 73 3b 0a 0a 09 77 68  g->options;...wh
7da0: 69 6c 65 20 28 31 29 20 7b 0a 09 09 2f 2a 20 46  ile (1) {.../* F
7db0: 61 69 6c 75 72 65 20 6c 6f 6f 70 20 70 72 65 76  ailure loop prev
7dc0: 65 6e 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 20 28  ention */...if (
7dd0: 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3e 20  failure_count > 
7de0: 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e  max_failure_coun
7df0: 74 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09  t) {....break;..
7e00: 09 7d 0a 0a 09 09 2f 2a 20 41 6c 6c 6f 63 61 74  .}..../* Allocat
7e10: 65 20 61 20 6e 65 77 20 6c 6f 67 20 62 75 66 66  e a new log buff
7e20: 65 72 20 2a 2f 0a 09 09 6c 6f 67 20 3d 20 66 69  er */...log = fi
7e30: 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 31 29 3b 0a  led_log_new(1);.
7e40: 09 09 69 66 20 28 6c 6f 67 20 3d 3d 20 4e 55 4c  ..if (log == NUL
7e50: 4c 29 20 7b 0a 09 09 09 66 69 6c 65 64 5f 6c 6f  L) {....filed_lo
7e60: 67 5f 6d 73 67 28 22 41 4c 4c 4f 43 41 54 45 5f  g_msg("ALLOCATE_
7e70: 4c 4f 47 5f 4d 53 47 5f 46 41 49 4c 45 44 22 29  LOG_MSG_FAILED")
7e80: 3b 0a 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d  ;.....break;...}
7e90: 0a 0a 09 09 6c 6f 67 2d 3e 74 79 70 65 20 3d 20  ....log->type = 
7ea0: 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54  FILED_LOG_TYPE_T
7eb0: 52 41 4e 53 46 45 52 3b 0a 0a 09 09 2f 2a 20 49  RANSFER;..../* I
7ec0: 66 20 77 65 20 63 6c 6f 73 65 64 20 74 68 65 20  f we closed the 
7ed0: 6f 6c 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  old connection, 
7ee0: 61 63 63 65 70 74 20 61 20 6e 65 77 20 6f 6e 65  accept a new one
7ef0: 20 2a 2f 0a 09 09 69 66 20 28 63 6f 6e 6e 65 63   */...if (connec
7f00: 74 69 6f 6e 5f 73 74 61 74 65 20 3d 3d 20 46 49  tion_state == FI
7f10: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43  LED_CONNECTION_C
7f20: 4c 4f 53 45 29 20 7b 0a 09 09 09 2f 2a 20 41 63  LOSE) {..../* Ac
7f30: 63 65 70 74 20 61 20 6e 65 77 20 63 6c 69 65 6e  cept a new clien
7f40: 74 20 2a 2f 0a 09 09 09 61 64 64 72 6c 65 6e 20  t */....addrlen 
7f50: 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 29 3b 0a  = sizeof(addr);.
7f60: 0a 09 09 09 66 64 20 3d 20 61 63 63 65 70 74 28  ....fd = accept(
7f70: 6d 61 73 74 65 72 5f 66 64 2c 20 28 73 74 72 75  master_fd, (stru
7f80: 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26  ct sockaddr *) &
7f90: 61 64 64 72 2c 20 26 61 64 64 72 6c 65 6e 29 3b  addr, &addrlen);
7fa0: 0a 09 09 7d 0a 0a 09 09 2f 2a 0a 09 09 20 2a 20  ...}..../*... * 
7fb0: 49 66 20 77 65 20 66 61 69 6c 2c 20 6d 61 6b 65  If we fail, make
7fc0: 20 61 20 6e 6f 74 65 20 6f 66 20 69 74 20 73 6f   a note of it so
7fd0: 20 77 65 20 64 6f 6e 27 74 20 67 6f 20 69 6e 74   we don't go int
7fe0: 6f 20 61 20 6c 6f 6f 70 20 6f 66 0a 09 09 20 2a  o a loop of... *
7ff0: 20 61 63 63 65 70 74 28 29 20 66 61 69 6c 69 6e   accept() failin
8000: 67 0a 09 09 20 2a 2f 0a 09 09 69 66 20 28 66 64  g... */...if (fd
8010: 20 3c 20 30 29 20 7b 0a 09 09 09 2f 2a 20 4c 6f   < 0) {..../* Lo
8020: 67 20 74 68 65 20 6e 65 77 20 63 6f 6e 6e 65 63  g the new connec
8030: 74 69 6f 6e 20 2a 2f 0a 09 09 09 66 69 6c 65 64  tion */....filed
8040: 5f 6c 6f 67 5f 6d 73 67 28 22 41 43 43 45 50 54  _log_msg("ACCEPT
8050: 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 09 09 66  _FAILED");.....f
8060: 61 69 6c 75 72 65 5f 63 6f 75 6e 74 2b 2b 3b 0a  ailure_count++;.
8070: 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 66 72  ....filed_log_fr
8080: 65 65 28 6c 6f 67 29 3b 0a 0a 09 09 09 63 6f 6e  ee(log);.....con
8090: 74 69 6e 75 65 3b 0a 09 09 7d 0a 0a 09 09 66 69  tinue;...}....fi
80a0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
80b0: 74 5f 61 63 63 65 70 74 28 66 64 29 3b 0a 0a 09  t_accept(fd);...
80c0: 09 2f 2a 20 46 69 6c 6c 20 69 6e 20 6c 6f 67 20  ./* Fill in log 
80d0: 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 09 69  structure */...i
80e0: 66 20 28 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28  f (filed_log_ip(
80f0: 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72  (struct sockaddr
8100: 20 2a 29 20 26 61 64 64 72 2c 20 6c 6f 67 2d 3e   *) &addr, log->
8110: 69 70 2c 20 73 69 7a 65 6f 66 28 6c 6f 67 2d 3e  ip, sizeof(log->
8120: 69 70 29 29 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  ip)) == NULL) {.
8130: 09 09 09 6c 6f 67 2d 3e 69 70 5b 30 5d 20 3d 20  ...log->ip[0] = 
8140: 27 5c 30 27 3b 0a 09 09 09 6c 6f 67 2d 3e 70 6f  '\0';....log->po
8150: 72 74 20 3d 20 30 3b 0a 09 09 7d 20 65 6c 73 65  rt = 0;...} else
8160: 20 7b 0a 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 20   {....log->port 
8170: 3d 20 61 64 64 72 2e 73 69 6e 36 5f 70 6f 72 74  = addr.sin6_port
8180: 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 52 65 73 65  ;...}..../* Rese
8190: 74 20 66 61 69 6c 75 72 65 20 63 6f 75 6e 74 2a  t failure count*
81a0: 2f 0a 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e  /...failure_coun
81b0: 74 20 3d 20 30 3b 0a 0a 09 09 2f 2a 20 48 61 6e  t = 0;..../* Han
81c0: 64 6c 65 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 09  dle socket */...
81d0: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65  connection_state
81e0: 20 3d 20 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f   = filed_handle_
81f0: 63 6c 69 65 6e 74 28 66 64 2c 20 26 72 65 71 75  client(fd, &requ
8200: 65 73 74 2c 20 6c 6f 67 2c 20 6f 70 74 69 6f 6e  est, log, option
8210: 73 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65 70 6f  s);..}.../* Repo
8220: 72 74 20 65 72 72 6f 72 20 2a 2f 0a 09 66 69 6c  rt error */..fil
8230: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 54 48 52 45  ed_log_msg("THRE
8240: 41 44 5f 44 49 45 44 20 41 42 4e 4f 52 4d 41 4c  AD_DIED ABNORMAL
8250: 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c  ");...return(NUL
8260: 4c 29 3b 0a 0a 09 2f 2a 20 6c 6f 63 61 6c 5f 64  L);.../* local_d
8270: 75 6d 6d 79 5f 6c 6f 67 20 69 73 20 6f 6e 6c 79  ummy_log is only
8280: 20 75 73 65 64 20 69 66 20 46 49 4c 45 44 5f 44   used if FILED_D
8290: 4f 4e 54 5f 4c 4f 47 20 69 73 20 65 6e 61 62 6c  ONT_LOG is enabl
82a0: 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74  ed, otherwise it
82b0: 27 73 20 6e 6f 74 20 75 73 65 64 2c 20 62 75 74  's not used, but
82c0: 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 68 61   the compiler ha
82d0: 74 65 73 20 74 68 61 74 20 69 64 65 61 2e 20 2a  tes that idea. *
82e0: 2f 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c  /..local_dummy_l
82f0: 6f 67 2e 74 79 70 65 20 3d 20 30 3b 0a 09 6c 6f  og.type = 0;..lo
8300: 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79  cal_dummy_log.ty
8310: 70 65 20 3d 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79  pe = local_dummy
8320: 5f 6c 6f 67 2e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a  _log.type;.}../*
8330: 20 43 72 65 61 74 65 20 77 6f 72 6b 65 72 20 74   Create worker t
8340: 68 72 65 61 64 73 20 2a 2f 0a 73 74 61 74 69 63  hreads */.static
8350: 20 69 6e 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65   int filed_worke
8360: 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 28 69  r_threads_init(i
8370: 6e 74 20 66 64 2c 20 69 6e 74 20 74 68 72 65 61  nt fd, int threa
8380: 64 5f 63 6f 75 6e 74 2c 20 73 74 72 75 63 74 20  d_count, struct 
8390: 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f  filed_options *o
83a0: 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 72 75 63  ptions) {..struc
83b0: 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74  t filed_worker_t
83c0: 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 3b  hread_args *arg;
83d0: 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65  ..pthread_t thre
83e0: 61 64 69 64 3b 0a 09 69 6e 74 20 70 74 68 72 65  adid;..int pthre
83f0: 61 64 5f 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a  ad_ret;..int i;.
8400: 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 20  ..for (i = 0; i 
8410: 3c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 3b 20  < thread_count; 
8420: 69 2b 2b 29 20 7b 0a 09 09 61 72 67 20 3d 20 6d  i++) {...arg = m
8430: 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72  alloc(sizeof(*ar
8440: 67 29 29 3b 0a 0a 09 09 61 72 67 2d 3e 66 64 20  g));....arg->fd 
8450: 3d 20 66 64 3b 0a 09 09 6d 65 6d 63 70 79 28 26  = fd;...memcpy(&
8460: 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 2c 20 6f 70  arg->options, op
8470: 74 69 6f 6e 73 2c 20 73 69 7a 65 6f 66 28 2a 6f  tions, sizeof(*o
8480: 70 74 69 6f 6e 73 29 29 3b 0a 0a 09 09 70 74 68  ptions));....pth
8490: 72 65 61 64 5f 72 65 74 20 3d 20 70 74 68 72 65  read_ret = pthre
84a0: 61 64 5f 63 72 65 61 74 65 28 26 74 68 72 65 61  ad_create(&threa
84b0: 64 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64  did, NULL, filed
84c0: 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 2c 20  _worker_thread, 
84d0: 61 72 67 29 3b 0a 09 09 69 66 20 28 70 74 68 72  arg);...if (pthr
84e0: 65 61 64 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a  ead_ret != 0) {.
84f0: 09 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09  ...return(-1);..
8500: 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 30  .}..}...return(0
8510: 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 73 70 6c 61 79  );.}../* Display
8520: 20 68 65 6c 70 20 2a 2f 0a 73 74 61 74 69 63 20   help */.static 
8530: 76 6f 69 64 20 66 69 6c 65 64 5f 70 72 69 6e 74  void filed_print
8540: 5f 68 65 6c 70 28 46 49 4c 45 20 2a 6f 75 74 70  _help(FILE *outp
8550: 75 74 2c 20 69 6e 74 20 6c 6f 6e 67 5f 68 65 6c  ut, int long_hel
8560: 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 65  p, const char *e
8570: 78 74 72 61 29 20 7b 0a 09 69 66 20 28 65 78 74  xtra) {..if (ext
8580: 72 61 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 28  ra) {...fprintf(
8590: 6f 75 74 70 75 74 2c 20 22 25 73 5c 6e 22 2c 20  output, "%s\n", 
85a0: 65 78 74 72 61 29 3b 0a 09 7d 0a 0a 09 66 70 72  extra);..}...fpr
85b0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 55 73  intf(output, "Us
85c0: 61 67 65 3a 20 66 69 6c 65 64 20 5b 3c 6f 70 74  age: filed [<opt
85d0: 69 6f 6e 73 3e 5d 5c 6e 22 29 3b 0a 09 66 70 72  ions>]\n");..fpr
85e0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
85f0: 4f 70 74 69 6f 6e 73 3a 5c 6e 22 29 3b 0a 09 66  Options:\n");..f
8600: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
8610: 20 20 20 20 20 20 2d 68 2c 20 2d 2d 68 65 6c 70        -h, --help
8620: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f  \n");..fprintf(o
8630: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 64  utput, "      -d
8640: 2c 20 2d 2d 64 61 65 6d 6f 6e 5c 6e 22 29 3b 0a  , --daemon\n");.
8650: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
8660: 20 22 20 20 20 20 20 20 2d 76 2c 20 2d 2d 76 65   "      -v, --ve
8670: 72 73 69 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 69  rsion\n");..fpri
8680: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
8690: 20 20 20 2d 56 2c 20 2d 2d 76 68 6f 73 74 5c 6e     -V, --vhost\n
86a0: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  ");..fprintf(out
86b0: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 62 20 3c  put, "      -b <
86c0: 61 64 64 72 65 73 73 3e 2c 20 2d 2d 62 69 6e 64  address>, --bind
86d0: 20 3c 61 64 64 72 65 73 73 3e 5c 6e 22 29 3b 0a   <address>\n");.
86e0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
86f0: 20 22 20 20 20 20 20 20 2d 70 20 3c 70 6f 72 74   "      -p <port
8700: 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70 6f 72 74 3e  >, --port <port>
8710: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f  \n");..fprintf(o
8720: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 74  utput, "      -t
8730: 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d 74 68 72 65   <count>, --thre
8740: 61 64 73 20 3c 63 6f 75 6e 74 3e 5c 6e 22 29 3b  ads <count>\n");
8750: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
8760: 2c 20 22 20 20 20 20 20 20 2d 63 20 3c 65 6e 74  , "      -c <ent
8770: 72 69 65 73 3e 2c 20 2d 2d 63 61 63 68 65 20 3c  ries>, --cache <
8780: 65 6e 74 72 69 65 73 3e 5c 6e 22 29 3b 0a 09 66  entries>\n");..f
8790: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
87a0: 20 20 20 20 20 20 2d 6c 20 3c 66 69 6c 65 3e 2c        -l <file>,
87b0: 20 2d 2d 6c 6f 67 20 3c 66 69 6c 65 3e 5c 6e 22   --log <file>\n"
87c0: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70  );..fprintf(outp
87d0: 75 74 2c 20 22 20 20 20 20 20 20 2d 75 20 3c 75  ut, "      -u <u
87e0: 73 65 72 3e 2c 20 2d 2d 75 73 65 72 20 3c 75 73  ser>, --user <us
87f0: 65 72 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74  er>\n");..fprint
8800: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
8810: 20 2d 72 20 3c 64 69 72 65 63 74 6f 72 79 3e 2c   -r <directory>,
8820: 20 2d 2d 72 6f 6f 74 20 3c 64 69 72 65 63 74 6f   --root <directo
8830: 72 79 3e 5c 6e 22 29 3b 0a 0a 09 69 66 20 28 6c  ry>\n");...if (l
8840: 6f 6e 67 5f 68 65 6c 70 29 20 7b 0a 09 09 66 70  ong_help) {...fp
8850: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c  rintf(output, "\
8860: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
8870: 75 74 70 75 74 2c 20 22 20 20 55 73 61 67 65 3a  utput, "  Usage:
8880: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
8890: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
88a0: 68 20 28 6f 72 20 2d 2d 68 65 6c 70 29 20 70 72  h (or --help) pr
88b0: 69 6e 74 73 20 74 68 69 73 20 75 73 61 67 65 20  ints this usage 
88c0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 5c 6e 22 29  information.\n")
88d0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
88e0: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72  ut, "\n");...fpr
88f0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
8900: 20 20 20 20 2d 64 20 28 6f 72 20 2d 2d 64 61 65      -d (or --dae
8910: 6d 6f 6e 29 20 69 6e 73 74 72 75 63 74 73 20 66  mon) instructs f
8920: 69 6c 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 61  iled to become a
8930: 20 64 61 65 6d 6f 6e 20 61 66 74 65 72 20 69 6e   daemon after in
8940: 69 74 69 61 6c 69 7a 69 6e 67 5c 6e 22 29 3b 0a  itializing\n");.
8950: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
8960: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
8970: 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 69            the li
8980: 73 74 65 6e 69 6e 67 20 54 43 50 20 73 6f 63 6b  stening TCP sock
8990: 65 74 20 61 6e 64 20 6c 6f 67 20 66 69 6c 65 73  et and log files
89a0: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  .\n");...fprintf
89b0: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a  (output, "\n");.
89c0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
89d0: 2c 20 22 20 20 20 20 20 20 2d 76 20 28 6f 72 20  , "      -v (or 
89e0: 2d 2d 76 65 72 73 69 6f 6e 29 20 69 6e 73 74 72  --version) instr
89f0: 75 63 74 73 20 66 69 6c 65 64 20 70 72 69 6e 74  ucts filed print
8a00: 20 6f 75 74 20 74 68 65 20 76 65 72 73 69 6f 6e   out the version
8a10: 20 6e 75 6d 62 65 72 20 61 6e 64 20 65 78 69 74   number and exit
8a20: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  .\n");...fprintf
8a30: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a  (output, "\n");.
8a40: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
8a50: 2c 20 22 20 20 20 20 20 20 2d 56 20 28 6f 72 20  , "      -V (or 
8a60: 2d 2d 76 68 6f 73 74 29 20 69 6e 73 74 72 75 63  --vhost) instruc
8a70: 74 73 20 66 69 6c 65 64 20 74 6f 20 70 72 65 70  ts filed to prep
8a80: 65 6e 64 20 61 6c 6c 20 72 65 71 75 65 73 74 73  end all requests
8a90: 20 77 69 74 68 20 74 68 65 69 72 20 48 54 54 50   with their HTTP
8aa0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
8ab0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
8ac0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 48                 H
8ad0: 6f 73 74 20 68 65 61 64 65 72 2e 5c 6e 22 29 3b  ost header.\n");
8ae0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
8af0: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69  t, "\n");...fpri
8b00: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
8b10: 20 20 20 2d 62 20 28 6f 72 20 2d 2d 62 69 6e 64     -b (or --bind
8b20: 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  ) specifies the 
8b30: 61 64 64 72 65 73 73 20 74 6f 20 6c 69 73 74 65  address to liste
8b40: 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 48  n for incoming H
8b50: 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  TTP\n");...fprin
8b60: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
8b70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8b80: 20 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 20 54   requests on.  T
8b90: 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
8ba0: 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20   is \"%s\".\n", 
8bb0: 42 49 4e 44 5f 41 44 44 52 29 3b 0a 09 09 66 70  BIND_ADDR);...fp
8bc0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c  rintf(output, "\
8bd0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
8be0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 70  utput, "      -p
8bf0: 20 28 6f 72 20 2d 2d 70 6f 72 74 29 20 73 70 65   (or --port) spe
8c00: 63 69 66 69 65 73 20 74 68 65 20 54 43 50 20 70  cifies the TCP p
8c10: 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f 20 6c 69  ort number to li
8c20: 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e  sten for incomin
8c30: 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70  g HTTP\n");...fp
8c40: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
8c50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8c60: 20 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e 2e      requests on.
8c70: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73    The default is
8c80: 20 25 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e   %u.\n", (unsign
8c90: 65 64 20 69 6e 74 29 20 50 4f 52 54 29 3b 0a 09  ed int) PORT);..
8ca0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
8cb0: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
8cc0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
8cd0: 20 2d 74 20 28 6f 72 20 2d 2d 74 68 72 65 61 64   -t (or --thread
8ce0: 73 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65  s) specifies the
8cf0: 20 6e 75 6d 62 65 72 20 6f 66 20 77 6f 72 6b 65   number of worke
8d00: 72 20 74 68 72 65 61 64 73 20 74 6f 20 63 72 65  r threads to cre
8d10: 61 74 65 2e 20 45 61 63 68 5c 6e 22 29 3b 0a 09  ate. Each\n");..
8d20: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
8d30: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
8d40: 20 20 20 20 20 20 20 20 20 20 77 6f 72 6b 65 72            worker
8d50: 20 74 68 72 65 61 64 20 63 61 6e 20 73 65 72 76   thread can serv
8d60: 69 63 65 20 6f 6e 65 20 63 6f 6e 63 75 72 72 65  ice one concurre
8d70: 6e 74 20 48 54 54 50 20 73 65 73 73 69 6f 6e 2e  nt HTTP session.
8d80: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
8d90: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
8da0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8db0: 20 54 68 75 73 20 74 68 65 20 6e 75 6d 62 65 72   Thus the number
8dc0: 20 6f 66 20 74 68 72 65 61 64 73 20 63 72 65 61   of threads crea
8dd0: 74 65 64 20 77 69 6c 6c 20 64 65 74 65 72 6d 69  ted will determi
8de0: 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a 09 09 66 70  ne how\n");...fp
8df0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
8e00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e10: 20 20 20 20 20 20 20 6d 61 6e 79 20 73 69 6d 75         many simu
8e20: 6c 74 61 6e 65 6f 75 73 20 74 72 61 6e 73 66 65  ltaneous transfe
8e30: 72 73 20 77 69 6c 6c 20 62 65 20 70 6f 73 73 69  rs will be possi
8e40: 62 6c 65 2e 20 54 68 65 5c 6e 22 29 3b 0a 09 09  ble. The\n");...
8e50: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
8e60: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
8e70: 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74           default
8e80: 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e   is %lu.\n", (un
8e90: 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 54 48 52  signed long) THR
8ea0: 45 41 44 5f 43 4f 55 4e 54 29 3b 0a 09 09 66 70  EAD_COUNT);...fp
8eb0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c  rintf(output, "\
8ec0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
8ed0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 63  utput, "      -c
8ee0: 20 28 6f 72 20 2d 2d 63 61 63 68 65 29 20 73 70   (or --cache) sp
8ef0: 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62  ecifies the numb
8f00: 65 72 20 6f 66 20 66 69 6c 65 20 69 6e 66 6f 72  er of file infor
8f10: 6d 61 74 69 6f 6e 20 63 61 63 68 65 20 65 6e 74  mation cache ent
8f20: 72 69 65 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69  ries\n");...fpri
8f30: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
8f40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8f50: 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2e 20     to allocate. 
8f60: 20 45 61 63 68 20 63 61 63 68 65 20 65 6e 74 72   Each cache entr
8f70: 79 20 68 6f 6c 64 73 20 66 69 6c 65 20 69 6e 66  y holds file inf
8f80: 6f 72 6d 61 74 69 6f 6e 20 61 73 5c 6e 22 29 3b  ormation as\n");
8f90: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
8fa0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
8fb0: 20 20 20 20 20 20 20 20 20 20 77 65 6c 6c 20 61            well a
8fc0: 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 64  s an open file d
8fd0: 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74 68 65  escriptor to the
8fe0: 20 66 69 6c 65 2c 20 73 6f 20 72 65 73 6f 75 72   file, so resour
8ff0: 63 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74  ce\n");...fprint
9000: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
9010: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9020: 20 6c 69 6d 69 74 73 20 28 69 2e 65 2e 2c 20 75   limits (i.e., u
9030: 6c 69 6d 69 74 29 20 73 68 6f 75 6c 64 20 62 65  limit) should be
9040: 20 63 6f 6e 73 69 64 65 72 65 64 2e 20 20 54 68   considered.  Th
9050: 69 73 20 73 68 6f 75 6c 64 5c 6e 22 29 3b 0a 09  is should\n");..
9060: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9070: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
9080: 20 20 20 20 20 20 20 20 62 65 20 61 20 70 72 69          be a pri
9090: 6d 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 69 64  me number for id
90a0: 65 61 6c 20 75 73 65 20 77 69 74 68 20 74 68 65  eal use with the
90b0: 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 6f 64 2e 5c   lookup method.\
90c0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
90d0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
90e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
90f0: 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 6c 75  e default is %lu
9100: 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 20  .\n", (unsigned 
9110: 6c 6f 6e 67 29 20 43 41 43 48 45 5f 53 49 5a 45  long) CACHE_SIZE
9120: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
9130: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70  put, "\n");...fp
9140: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9150: 20 20 20 20 20 2d 6c 20 28 6f 72 20 2d 2d 6c 6f       -l (or --lo
9160: 67 29 20 73 70 65 63 69 66 69 65 73 20 61 20 66  g) specifies a f
9170: 69 6c 65 6e 61 6d 65 20 74 6f 20 6f 70 65 6e 20  ilename to open 
9180: 66 6f 72 20 77 72 69 74 69 6e 67 20 6c 6f 67 20  for writing log 
9190: 65 6e 74 72 69 65 73 2e 20 20 4c 6f 67 5c 6e 22  entries.  Log\n"
91a0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
91b0: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
91c0: 20 20 20 20 20 20 20 20 20 20 65 6e 74 72 69 65            entrie
91d0: 73 20 61 72 65 20 6d 61 64 65 20 66 6f 72 20 76  s are made for v
91e0: 61 72 69 6f 75 73 20 73 74 61 67 65 73 20 69 6e  arious stages in
91f0: 20 74 72 61 6e 73 66 65 72 69 6e 67 20 66 69 6c   transfering fil
9200: 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  es.\n");...fprin
9210: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9220: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9230: 54 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 73 20  The log file is 
9240: 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 73 77  opened before sw
9250: 69 74 63 68 69 6e 67 20 75 73 65 72 73 20 28 73  itching users (s
9260: 65 65 20 5c 22 2d 75 5c 22 29 5c 6e 22 29 3b 0a  ee \"-u\")\n");.
9270: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9280: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
9290: 20 20 20 20 20 20 20 61 6e 64 20 72 6f 6f 74 20         and root 
92a0: 64 69 72 65 63 74 6f 72 69 65 73 20 28 73 65 65  directories (see
92b0: 20 5c 22 2d 72 5c 22 29 2e 20 20 54 68 65 20 6c   \"-r\").  The l
92c0: 6f 67 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72  og file is never
92d0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
92e0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
92f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6c 6f               clo
9300: 73 65 64 20 73 6f 20 6c 6f 67 20 72 6f 74 61 74  sed so log rotat
9310: 69 6f 6e 20 77 69 74 68 6f 75 74 20 73 74 6f 70  ion without stop
9320: 70 69 6e 67 20 74 68 65 20 64 61 65 6d 6f 6e 20  ping the daemon 
9330: 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66  is will\n");...f
9340: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
9350: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9360: 20 20 20 20 6e 6f 74 20 77 6f 72 6b 2e 20 20 54      not work.  T
9370: 68 65 20 76 61 6c 75 65 20 6f 66 20 5c 22 2d 5c  he value of \"-\
9380: 22 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  " indicates that
9390: 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 75 74   standard output
93a0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
93b0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
93c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 73 68 6f               sho
93d0: 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20  uld be used for 
93e0: 6c 6f 67 67 69 6e 67 2e 20 20 49 66 20 74 68 65  logging.  If the
93f0: 20 66 69 6c 65 6e 61 6d 65 20 62 65 67 69 6e 73   filename begins
9400: 20 77 69 74 68 20 61 5c 6e 22 29 3b 0a 09 09 66   with a\n");...f
9410: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
9420: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9430: 20 20 20 20 70 69 70 65 20 28 5c 22 7c 5c 22 29      pipe (\"|\")
9440: 20 74 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20   then a process 
9450: 69 73 20 73 74 61 72 74 65 64 20 61 6e 64 20 75  is started and u
9460: 73 65 64 20 66 6f 72 20 6c 6f 67 67 69 6e 67 5c  sed for logging\
9470: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
9480: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
9490: 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 73 74              inst
94a0: 65 61 64 20 6f 66 20 61 20 66 69 6c 65 2e 20 20  ead of a file.  
94b0: 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 5c  The default is \
94c0: 22 25 73 5c 22 2e 5c 6e 22 2c 20 4c 4f 47 5f 46  "%s\".\n", LOG_F
94d0: 49 4c 45 29 3b 0a 23 69 66 64 65 66 20 46 49 4c  ILE);.#ifdef FIL
94e0: 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 09 09 66 70  ED_DONT_LOG...fp
94f0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9500: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9510: 20 20 20 4e 6f 74 65 20 74 68 61 74 20 6c 6f 67     Note that log
9520: 67 69 6e 67 20 69 73 20 63 6f 6d 70 6c 65 74 65  ging is complete
9530: 6c 79 20 64 69 73 61 62 6c 65 64 20 73 6f 20 74  ly disabled so t
9540: 68 69 73 20 6f 70 74 69 6f 6e 20 64 6f 65 73 5c  his option does\
9550: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
9560: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
9570: 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f 74 68              noth
9580: 69 6e 67 20 69 6e 20 74 68 69 73 20 62 75 69 6c  ing in this buil
9590: 64 2e 5c 6e 22 29 3b 0a 23 65 6e 64 69 66 0a 09  d.\n");.#endif..
95a0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
95b0: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
95c0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
95d0: 20 2d 75 20 28 6f 72 20 2d 2d 75 73 65 72 29 20   -u (or --user) 
95e0: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 75 73  specifies the us
95f0: 65 72 20 74 6f 20 73 77 69 74 63 68 20 75 73 65  er to switch use
9600: 72 20 49 44 73 20 74 6f 20 62 65 66 6f 72 65 20  r IDs to before 
9610: 73 65 72 76 69 63 69 6e 67 5c 6e 22 29 3b 0a 09  servicing\n");..
9620: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9630: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
9640: 20 20 20 20 20 20 20 72 65 71 75 65 73 74 73 2e         requests.
9650: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73    The default is
9660: 20 6e 6f 74 20 63 68 61 6e 67 65 20 75 73 65 72   not change user
9670: 20 49 44 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72   IDs.\n");...fpr
9680: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e  intf(output, "\n
9690: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
96a0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 72 20  tput, "      -r 
96b0: 28 6f 72 20 2d 2d 72 6f 6f 74 29 20 73 70 65 63  (or --root) spec
96c0: 69 66 69 65 73 20 74 68 65 20 64 69 72 65 63 74  ifies the direct
96d0: 6f 72 79 20 74 6f 20 61 63 74 20 61 73 20 74 68  ory to act as th
96e0: 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 79  e root directory
96f0: 20 66 6f 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69   for\n");...fpri
9700: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9710: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9720: 20 20 74 68 65 20 66 69 6c 65 20 73 65 72 76 65    the file serve
9730: 72 2e 20 20 49 66 20 74 68 69 73 20 6f 70 74 69  r.  If this opti
9740: 6f 6e 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  on is specified,
9750: 20 63 68 72 6f 6f 74 28 32 29 5c 6e 22 29 3b 0a   chroot(2)\n");.
9760: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9770: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
9780: 20 20 20 20 20 20 20 20 69 73 20 63 61 6c 6c 65          is calle
9790: 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  d.  The default 
97a0: 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 72 6f  is not change ro
97b0: 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73 2c 5c  ot directories,\
97c0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
97d0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
97e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 61               tha
97f0: 74 20 69 73 2c 20 74 68 65 20 5c 22 2f 5c 22 20  t is, the \"/\" 
9800: 64 69 72 65 63 74 6f 72 79 20 69 73 20 73 68 61  directory is sha
9810: 72 65 64 20 6f 75 74 2e 20 20 54 68 69 73 20 77  red out.  This w
9820: 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  ill\n");...fprin
9830: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9840: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9850: 20 6c 69 6b 65 6c 79 20 62 65 20 61 20 73 65 63   likely be a sec
9860: 75 72 69 74 79 20 69 73 73 75 65 2c 20 73 6f 20  urity issue, so 
9870: 74 68 69 73 20 6f 70 74 69 6f 6e 20 73 68 6f 75  this option shou
9880: 6c 64 20 61 6c 77 61 79 73 5c 6e 22 29 3b 0a 09  ld always\n");..
9890: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
98a0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
98b0: 20 20 20 20 20 20 20 62 65 20 75 73 65 64 2e 5c         be used.\
98c0: 6e 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e  n");..}...return
98d0: 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 20 67 65  ;.}../* Add a ge
98e0: 74 6f 70 74 20 6f 70 74 69 6f 6e 20 2a 2f 0a 73  topt option */.s
98f0: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64  tatic void filed
9900: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74  _getopt_long_set
9910: 6f 70 74 28 73 74 72 75 63 74 20 6f 70 74 69 6f  opt(struct optio
9920: 6e 20 2a 6f 70 74 2c 20 63 6f 6e 73 74 20 63 68  n *opt, const ch
9930: 61 72 20 2a 6e 61 6d 65 2c 20 69 6e 74 20 68 61  ar *name, int ha
9940: 73 5f 61 72 67 2c 20 69 6e 74 20 76 61 6c 29 20  s_arg, int val) 
9950: 7b 0a 09 6f 70 74 2d 3e 6e 61 6d 65 20 20 20 20  {..opt->name    
9960: 20 3d 20 6e 61 6d 65 3b 0a 09 6f 70 74 2d 3e 68   = name;..opt->h
9970: 61 73 5f 61 72 67 20 20 3d 20 68 61 73 5f 61 72  as_arg  = has_ar
9980: 67 3b 0a 09 6f 70 74 2d 3e 66 6c 61 67 20 20 20  g;..opt->flag   
9990: 20 20 3d 20 4e 55 4c 4c 3b 0a 09 6f 70 74 2d 3e    = NULL;..opt->
99a0: 76 61 6c 20 20 20 20 20 20 3d 20 76 61 6c 3b 0a  val      = val;.
99b0: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20  ..return;.}../* 
99c0: 52 65 73 6f 6c 76 65 20 61 20 75 73 65 72 6e 61  Resolve a userna
99d0: 6d 65 20 74 6f 20 61 20 55 49 44 20 2a 2f 0a 73  me to a UID */.s
99e0: 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f  tatic int filed_
99f0: 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 63 6f 6e 73  user_lookup(cons
9a00: 74 20 63 68 61 72 20 2a 75 73 65 72 2c 20 75 69  t char *user, ui
9a10: 64 5f 74 20 2a 75 73 65 72 5f 69 64 29 20 7b 0a  d_t *user_id) {.
9a20: 09 63 68 61 72 20 2a 6e 65 78 74 3b 0a 09 75 69  .char *next;..ui
9a30: 64 5f 74 20 75 73 65 72 5f 69 64 5f 63 68 65 63  d_t user_id_chec
9a40: 6b 3b 0a 23 69 66 6e 64 65 66 20 46 49 4c 45 44  k;.#ifndef FILED
9a50: 5f 4e 4f 5f 47 45 54 50 57 4e 41 4d 0a 09 73 74  _NO_GETPWNAM..st
9a60: 72 75 63 74 20 70 61 73 73 77 64 20 2a 65 6e 74  ruct passwd *ent
9a70: 3b 0a 0a 09 65 6e 74 20 3d 20 67 65 74 70 77 6e  ;...ent = getpwn
9a80: 61 6d 28 75 73 65 72 29 3b 0a 09 69 66 20 28 65  am(user);..if (e
9a90: 6e 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09  nt != NULL) {...
9aa0: 2a 75 73 65 72 5f 69 64 20 3d 20 65 6e 74 2d 3e  *user_id = ent->
9ab0: 70 77 5f 75 69 64 3b 0a 0a 09 09 72 65 74 75 72  pw_uid;....retur
9ac0: 6e 28 30 29 3b 0a 09 7d 0a 23 65 6e 64 69 66 0a  n(0);..}.#endif.
9ad0: 0a 09 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 20  ..user_id_check 
9ae0: 3d 20 73 74 72 74 6f 75 6c 6c 28 75 73 65 72 2c  = strtoull(user,
9af0: 20 26 6e 65 78 74 2c 20 31 30 29 3b 0a 09 69 66   &next, 10);..if
9b00: 20 28 6e 65 78 74 20 3d 3d 20 4e 55 4c 4c 29 20   (next == NULL) 
9b10: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  {...return(1);..
9b20: 7d 0a 0a 09 69 66 20 28 6e 65 78 74 5b 30 5d 20  }...if (next[0] 
9b30: 21 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 65 74  != '\0') {...ret
9b40: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2a 75 73  urn(1);..}...*us
9b50: 65 72 5f 69 64 20 3d 20 75 73 65 72 5f 69 64 5f  er_id = user_id_
9b60: 63 68 65 63 6b 3b 0a 0a 09 72 65 74 75 72 6e 28  check;...return(
9b70: 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 61 65 6d 6f 6e  0);.}../* Daemon
9b80: 69 7a 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ize */.static in
9b90: 74 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a  t filed_daemoniz
9ba0: 65 28 76 6f 69 64 29 20 7b 0a 09 70 69 64 5f 74  e(void) {..pid_t
9bb0: 20 73 65 74 73 69 64 5f 72 65 74 2c 20 66 6f 72   setsid_ret, for
9bc0: 6b 5f 72 65 74 3b 0a 09 69 6e 74 20 63 68 64 69  k_ret;..int chdi
9bd0: 72 5f 72 65 74 2c 20 64 75 70 32 5f 72 65 74 3b  r_ret, dup2_ret;
9be0: 0a 09 69 6e 74 20 66 64 5f 69 6e 2c 20 66 64 5f  ..int fd_in, fd_
9bf0: 6f 75 74 3b 0a 0a 09 63 68 64 69 72 5f 72 65 74  out;...chdir_ret
9c00: 20 3d 20 63 68 64 69 72 28 22 2f 22 29 3b 0a 09   = chdir("/");..
9c10: 69 66 20 28 63 68 64 69 72 5f 72 65 74 20 21 3d  if (chdir_ret !=
9c20: 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31   0) {...return(1
9c30: 29 3b 0a 09 7d 0a 0a 09 66 6f 72 6b 5f 72 65 74  );..}...fork_ret
9c40: 20 3d 20 66 6f 72 6b 28 29 3b 0a 09 69 66 20 28   = fork();..if (
9c50: 66 6f 72 6b 5f 72 65 74 20 3c 20 30 29 20 7b 0a  fork_ret < 0) {.
9c60: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a  ..return(1);..}.
9c70: 0a 09 69 66 20 28 66 6f 72 6b 5f 72 65 74 20 3e  ..if (fork_ret >
9c80: 20 30 29 20 7b 0a 09 09 2f 2a 20 50 61 72 65 6e   0) {.../* Paren
9c90: 74 20 2a 2f 0a 09 09 77 61 69 74 70 69 64 28 66  t */...waitpid(f
9ca0: 6f 72 6b 5f 72 65 74 2c 20 4e 55 4c 4c 2c 20 30  ork_ret, NULL, 0
9cb0: 29 3b 0a 0a 09 09 65 78 69 74 28 45 58 49 54 5f  );....exit(EXIT_
9cc0: 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f  SUCCESS);..}.../
9cd0: 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 69 66 20 28  * Child */..if (
9ce0: 66 6f 72 6b 28 29 20 21 3d 20 30 29 20 7b 0a 09  fork() != 0) {..
9cf0: 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 09 65  ./* Child */...e
9d00: 78 69 74 28 45 58 49 54 5f 53 55 43 43 45 53 53  xit(EXIT_SUCCESS
9d10: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 47 72 61 6e 64  );..}.../* Grand
9d20: 20 63 68 69 6c 64 20 2a 2f 0a 09 73 65 74 73 69   child */..setsi
9d30: 64 5f 72 65 74 20 3d 20 73 65 74 73 69 64 28 29  d_ret = setsid()
9d40: 3b 0a 09 69 66 20 28 73 65 74 73 69 64 5f 72 65  ;..if (setsid_re
9d50: 74 20 3d 3d 20 28 28 70 69 64 5f 74 29 20 2d 31  t == ((pid_t) -1
9d60: 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29  )) {...return(1)
9d70: 3b 0a 09 7d 0a 0a 09 66 64 5f 69 6e 20 3d 20 6f  ;..}...fd_in = o
9d80: 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c  pen("/dev/null",
9d90: 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 09 66 64 5f   O_RDONLY);..fd_
9da0: 6f 75 74 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76  out = open("/dev
9db0: 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 52 4f 4e 4c 59  /null", O_WRONLY
9dc0: 29 3b 0a 09 69 66 20 28 66 64 5f 69 6e 20 3c 20  );..if (fd_in < 
9dd0: 30 20 7c 7c 20 66 64 5f 6f 75 74 20 3c 20 30 29  0 || fd_out < 0)
9de0: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a   {...return(1);.
9df0: 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d 20  .}...dup2_ret = 
9e00: 64 75 70 32 28 66 64 5f 69 6e 2c 20 53 54 44 49  dup2(fd_in, STDI
9e10: 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28  N_FILENO);..if (
9e20: 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 49  dup2_ret != STDI
9e30: 4e 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65  N_FILENO) {...re
9e40: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75  turn(1);..}...du
9e50: 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64  p2_ret = dup2(fd
9e60: 5f 6f 75 74 2c 20 53 54 44 4f 55 54 5f 46 49 4c  _out, STDOUT_FIL
9e70: 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f  ENO);..if (dup2_
9e80: 72 65 74 20 21 3d 20 53 54 44 4f 55 54 5f 46 49  ret != STDOUT_FI
9e90: 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e  LENO) {...return
9ea0: 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72  (1);..}...dup2_r
9eb0: 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f 75 74  et = dup2(fd_out
9ec0: 2c 20 53 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29  , STDERR_FILENO)
9ed0: 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74 20  ;..if (dup2_ret 
9ee0: 21 3d 20 53 54 44 45 52 52 5f 46 49 4c 45 4e 4f  != STDERR_FILENO
9ef0: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b  ) {...return(1);
9f00: 0a 09 7d 0a 0a 09 63 6c 6f 73 65 28 66 64 5f 69  ..}...close(fd_i
9f10: 6e 29 3b 0a 09 63 6c 6f 73 65 28 66 64 5f 6f 75  n);..close(fd_ou
9f20: 74 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b  t);...return(0);
9f30: 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 70 72 6f 63 65  .}../* Run proce
9f40: 73 73 20 2a 2f 0a 69 6e 74 20 6d 61 69 6e 28 69  ss */.int main(i
9f50: 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a  nt argc, char **
9f60: 61 72 67 76 29 20 7b 0a 09 73 74 72 75 63 74 20  argv) {..struct 
9f70: 6f 70 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 5b 31  option options[1
9f80: 32 5d 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65  2];..struct file
9f90: 64 5f 6f 70 74 69 6f 6e 73 20 74 68 72 65 61 64  d_options thread
9fa0: 5f 6f 70 74 69 6f 6e 73 3b 0a 09 63 6f 6e 73 74  _options;..const
9fb0: 20 63 68 61 72 20 2a 62 69 6e 64 5f 61 64 64 72   char *bind_addr
9fc0: 20 3d 20 42 49 4e 44 5f 41 44 44 52 2c 20 2a 6e   = BIND_ADDR, *n
9fd0: 65 77 72 6f 6f 74 20 3d 20 4e 55 4c 4c 2c 20 2a  ewroot = NULL, *
9fe0: 6c 6f 67 5f 66 69 6c 65 20 3d 20 4c 4f 47 5f 46  log_file = LOG_F
9ff0: 49 4c 45 3b 0a 09 46 49 4c 45 20 2a 6c 6f 67 5f  ILE;..FILE *log_
a000: 66 70 3b 0a 09 75 69 64 5f 74 20 75 73 65 72 20  fp;..uid_t user 
a010: 3d 20 30 3b 0a 09 69 6e 74 20 70 6f 72 74 20 3d  = 0;..int port =
a020: 20 50 4f 52 54 2c 20 74 68 72 65 61 64 5f 63 6f   PORT, thread_co
a030: 75 6e 74 20 3d 20 54 48 52 45 41 44 5f 43 4f 55  unt = THREAD_COU
a040: 4e 54 3b 0a 09 69 6e 74 20 63 61 63 68 65 5f 73  NT;..int cache_s
a050: 69 7a 65 20 3d 20 43 41 43 48 45 5f 53 49 5a 45  ize = CACHE_SIZE
a060: 3b 0a 09 69 6e 74 20 69 6e 69 74 5f 72 65 74 2c  ;..int init_ret,
a070: 20 63 68 72 6f 6f 74 5f 72 65 74 2c 20 73 65 74   chroot_ret, set
a080: 75 69 64 5f 72 65 74 2c 20 6c 6f 6f 6b 75 70 5f  uid_ret, lookup_
a090: 72 65 74 2c 20 63 68 64 69 72 5f 72 65 74 3b 0a  ret, chdir_ret;.
a0a0: 09 69 6e 74 20 73 65 74 75 69 64 5f 65 6e 61 62  .int setuid_enab
a0b0: 6c 65 64 20 3d 20 30 2c 20 64 61 65 6d 6f 6e 5f  led = 0, daemon_
a0c0: 65 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 09 69 6e  enabled = 0;..in
a0d0: 74 20 63 68 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a  t ch;..int fd;..
a0e0: 09 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20  ./* Set default 
a0f0: 76 61 6c 75 65 73 20 2a 2f 0a 09 74 68 72 65 61  values */..threa
a100: 64 5f 6f 70 74 69 6f 6e 73 2e 76 68 6f 73 74 73  d_options.vhosts
a110: 5f 65 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 0a 09  _enabled = 0;...
a120: 2f 2a 20 50 72 6f 63 65 73 73 20 61 72 67 75 6d  /* Process argum
a130: 65 6e 74 73 20 2a 2f 0a 09 66 69 6c 65 64 5f 67  ents */..filed_g
a140: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70  etopt_long_setop
a150: 74 28 26 6f 70 74 69 6f 6e 73 5b 30 5d 2c 20 22  t(&options[0], "
a160: 70 6f 72 74 22 2c 20 72 65 71 75 69 72 65 64 5f  port", required_
a170: 61 72 67 75 6d 65 6e 74 2c 20 27 70 27 29 3b 0a  argument, 'p');.
a180: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f  .filed_getopt_lo
a190: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f  ng_setopt(&optio
a1a0: 6e 73 5b 31 5d 2c 20 22 74 68 72 65 61 64 73 22  ns[1], "threads"
a1b0: 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d  , required_argum
a1c0: 65 6e 74 2c 20 27 74 27 29 3b 0a 09 66 69 6c 65  ent, 't');..file
a1d0: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65  d_getopt_long_se
a1e0: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 32 5d  topt(&options[2]
a1f0: 2c 20 22 63 61 63 68 65 22 2c 20 72 65 71 75 69  , "cache", requi
a200: 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 63  red_argument, 'c
a210: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70  ');..filed_getop
a220: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f  t_long_setopt(&o
a230: 70 74 69 6f 6e 73 5b 33 5d 2c 20 22 62 69 6e 64  ptions[3], "bind
a240: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75  ", required_argu
a250: 6d 65 6e 74 2c 20 27 62 27 29 3b 0a 09 66 69 6c  ment, 'b');..fil
a260: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73  ed_getopt_long_s
a270: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 34  etopt(&options[4
a280: 5d 2c 20 22 75 73 65 72 22 2c 20 72 65 71 75 69  ], "user", requi
a290: 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 75  red_argument, 'u
a2a0: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70  ');..filed_getop
a2b0: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f  t_long_setopt(&o
a2c0: 70 74 69 6f 6e 73 5b 35 5d 2c 20 22 72 6f 6f 74  ptions[5], "root
a2d0: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75  ", required_argu
a2e0: 6d 65 6e 74 2c 20 27 72 27 29 3b 0a 09 66 69 6c  ment, 'r');..fil
a2f0: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73  ed_getopt_long_s
a300: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 36  etopt(&options[6
a310: 5d 2c 20 22 68 65 6c 70 22 2c 20 6e 6f 5f 61 72  ], "help", no_ar
a320: 67 75 6d 65 6e 74 2c 20 27 68 27 29 3b 0a 09 66  gument, 'h');..f
a330: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67  iled_getopt_long
a340: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73  _setopt(&options
a350: 5b 37 5d 2c 20 22 64 61 65 6d 6f 6e 22 2c 20 6e  [7], "daemon", n
a360: 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 64 27 29  o_argument, 'd')
a370: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f  ;..filed_getopt_
a380: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74  long_setopt(&opt
a390: 69 6f 6e 73 5b 38 5d 2c 20 22 6c 6f 67 22 2c 20  ions[8], "log", 
a3a0: 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e  required_argumen
a3b0: 74 2c 20 27 6c 27 29 3b 0a 09 66 69 6c 65 64 5f  t, 'l');..filed_
a3c0: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f  getopt_long_seto
a3d0: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 39 5d 2c 20  pt(&options[9], 
a3e0: 22 76 65 72 73 69 6f 6e 22 2c 20 6e 6f 5f 61 72  "version", no_ar
a3f0: 67 75 6d 65 6e 74 2c 20 27 76 27 29 3b 0a 09 66  gument, 'v');..f
a400: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67  iled_getopt_long
a410: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73  _setopt(&options
a420: 5b 31 30 5d 2c 20 22 76 68 6f 73 74 22 2c 20 6e  [10], "vhost", n
a430: 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 56 27 29  o_argument, 'V')
a440: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f  ;..filed_getopt_
a450: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74  long_setopt(&opt
a460: 69 6f 6e 73 5b 31 31 5d 2c 20 4e 55 4c 4c 2c 20  ions[11], NULL, 
a470: 30 2c 20 30 29 3b 0a 09 77 68 69 6c 65 20 28 28  0, 0);..while ((
a480: 63 68 20 3d 20 67 65 74 6f 70 74 5f 6c 6f 6e 67  ch = getopt_long
a490: 28 61 72 67 63 2c 20 61 72 67 76 2c 20 22 70 3a  (argc, argv, "p:
a4a0: 74 3a 63 3a 62 3a 75 3a 72 3a 6c 3a 68 64 76 56  t:c:b:u:r:l:hdvV
a4b0: 22 2c 20 6f 70 74 69 6f 6e 73 2c 20 4e 55 4c 4c  ", options, NULL
a4c0: 29 29 20 21 3d 20 2d 31 29 20 7b 0a 09 09 73 77  )) != -1) {...sw
a4d0: 69 74 63 68 28 63 68 29 20 7b 0a 09 09 09 63 61  itch(ch) {....ca
a4e0: 73 65 20 27 70 27 3a 0a 09 09 09 09 70 6f 72 74  se 'p':.....port
a4f0: 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 3b   = atoi(optarg);
a500: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63  .....break;....c
a510: 61 73 65 20 27 74 27 3a 0a 09 09 09 09 74 68 72  ase 't':.....thr
a520: 65 61 64 5f 63 6f 75 6e 74 20 3d 20 61 74 6f 69  ead_count = atoi
a530: 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72  (optarg);.....br
a540: 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 63 27  eak;....case 'c'
a550: 3a 0a 09 09 09 09 63 61 63 68 65 5f 73 69 7a 65  :.....cache_size
a560: 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 3b   = atoi(optarg);
a570: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63  .....break;....c
a580: 61 73 65 20 27 62 27 3a 0a 09 09 09 09 62 69 6e  ase 'b':.....bin
a590: 64 5f 61 64 64 72 20 3d 20 73 74 72 64 75 70 28  d_addr = strdup(
a5a0: 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65  optarg);.....bre
a5b0: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 75 27 3a  ak;....case 'u':
a5c0: 0a 09 09 09 09 73 65 74 75 69 64 5f 65 6e 61 62  .....setuid_enab
a5d0: 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 6c 6f 6f  led = 1;.....loo
a5e0: 6b 75 70 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f  kup_ret = filed_
a5f0: 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 6f 70 74 61  user_lookup(opta
a600: 72 67 2c 20 26 75 73 65 72 29 3b 0a 09 09 09 09  rg, &user);.....
a610: 69 66 20 28 6c 6f 6f 6b 75 70 5f 72 65 74 20 21  if (lookup_ret !
a620: 3d 20 30 29 20 7b 0a 09 09 09 09 09 66 69 6c 65  = 0) {......file
a630: 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73 74 64  d_print_help(std
a640: 65 72 72 2c 20 30 2c 20 22 49 6e 76 61 6c 69 64  err, 0, "Invalid
a650: 20 75 73 65 72 6e 61 6d 65 20 73 70 65 63 69 66   username specif
a660: 69 65 64 22 29 3b 0a 0a 09 09 09 09 09 72 65 74  ied");.......ret
a670: 75 72 6e 28 31 29 3b 0a 09 09 09 09 7d 0a 09 09  urn(1);.....}...
a680: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65  ..break;....case
a690: 20 27 72 27 3a 0a 09 09 09 09 6e 65 77 72 6f 6f   'r':.....newroo
a6a0: 74 20 3d 20 73 74 72 64 75 70 28 6f 70 74 61 72  t = strdup(optar
a6b0: 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09  g);.....break;..
a6c0: 09 09 63 61 73 65 20 27 6c 27 3a 0a 09 09 09 09  ..case 'l':.....
a6d0: 6c 6f 67 5f 66 69 6c 65 20 3d 20 73 74 72 64 75  log_file = strdu
a6e0: 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62  p(optarg);.....b
a6f0: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 64  reak;....case 'd
a700: 27 3a 0a 09 09 09 09 64 61 65 6d 6f 6e 5f 65 6e  ':.....daemon_en
a710: 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 62  abled = 1;.....b
a720: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 56  reak;....case 'V
a730: 27 3a 0a 09 09 09 09 74 68 72 65 61 64 5f 6f 70  ':.....thread_op
a740: 74 69 6f 6e 73 2e 76 68 6f 73 74 73 5f 65 6e 61  tions.vhosts_ena
a750: 62 6c 65 64 20 3d 20 31 3b 0a 0a 09 09 09 09 62  bled = 1;......b
a760: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 76  reak;....case 'v
a770: 27 3a 0a 09 09 09 09 70 72 69 6e 74 66 28 22 66  ':.....printf("f
a780: 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 25 73 5c  iled version %s\
a790: 6e 22 2c 20 46 49 4c 45 44 5f 56 45 52 53 49 4f  n", FILED_VERSIO
a7a0: 4e 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28  N);......return(
a7b0: 30 29 3b 0a 09 09 09 63 61 73 65 20 27 3f 27 3a  0);....case '?':
a7c0: 0a 09 09 09 63 61 73 65 20 27 3a 27 3a 0a 09 09  ....case ':':...
a7d0: 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65  ..filed_print_he
a7e0: 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20 4e 55  lp(stderr, 0, NU
a7f0: 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e  LL);......return
a800: 28 31 29 3b 0a 09 09 09 63 61 73 65 20 27 68 27  (1);....case 'h'
a810: 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e  :.....filed_prin
a820: 74 5f 68 65 6c 70 28 73 74 64 6f 75 74 2c 20 31  t_help(stdout, 1
a830: 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65  , NULL);......re
a840: 74 75 72 6e 28 30 29 3b 0a 09 09 7d 0a 09 7d 0a  turn(0);...}..}.
a850: 0a 09 2f 2a 20 4f 70 65 6e 20 6c 6f 67 20 66 69  ../* Open log fi
a860: 6c 65 20 2a 2f 0a 09 6c 6f 67 5f 66 70 20 3d 20  le */..log_fp = 
a870: 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 6c  filed_log_open(l
a880: 6f 67 5f 66 69 6c 65 29 3b 0a 09 69 66 20 28 6c  og_file);..if (l
a890: 6f 67 5f 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b  og_fp == NULL) {
a8a0: 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64  ...perror("filed
a8b0: 5f 6c 6f 67 5f 6f 70 65 6e 22 29 3b 0a 0a 09 09  _log_open");....
a8c0: 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09  return(4);..}...
a8d0: 2f 2a 20 43 72 65 61 74 65 20 6c 69 73 74 65 6e  /* Create listen
a8e0: 69 6e 67 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 66  ing socket */..f
a8f0: 64 20 3d 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e  d = filed_listen
a900: 28 62 69 6e 64 5f 61 64 64 72 2c 20 70 6f 72 74  (bind_addr, port
a910: 29 3b 0a 09 69 66 20 28 66 64 20 3c 20 30 29 20  );..if (fd < 0) 
a920: 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65  {...perror("file
a930: 64 5f 6c 69 73 74 65 6e 22 29 3b 0a 0a 09 09 72  d_listen");....r
a940: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f  eturn(1);..}.../
a950: 2a 20 42 65 63 6f 6d 65 20 61 20 64 61 65 6d 6f  * Become a daemo
a960: 6e 20 2a 2f 0a 09 69 66 20 28 64 61 65 6d 6f 6e  n */..if (daemon
a970: 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 69 6e  _enabled) {...in
a980: 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 64  it_ret = filed_d
a990: 61 65 6d 6f 6e 69 7a 65 28 29 3b 0a 09 09 69 66  aemonize();...if
a9a0: 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29   (init_ret != 0)
a9b0: 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 66 69   {....perror("fi
a9c0: 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 22 29 3b  led_daemonize");
a9d0: 0a 0a 09 09 09 72 65 74 75 72 6e 28 36 29 3b 0a  .....return(6);.
a9e0: 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 43 68 72 6f  ..}..}.../* Chro
a9f0: 6f 74 2c 20 69 66 20 61 70 70 72 6f 70 72 69 61  ot, if appropria
aa00: 74 65 20 2a 2f 0a 09 69 66 20 28 6e 65 77 72 6f  te */..if (newro
aa10: 6f 74 29 20 7b 0a 09 09 63 68 64 69 72 5f 72 65  ot) {...chdir_re
aa20: 74 20 3d 20 63 68 64 69 72 28 6e 65 77 72 6f 6f  t = chdir(newroo
aa30: 74 29 3b 0a 09 09 69 66 20 28 63 68 64 69 72 5f  t);...if (chdir_
aa40: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70  ret != 0) {....p
aa50: 65 72 72 6f 72 28 22 63 68 64 69 72 22 29 3b 0a  error("chdir");.
aa60: 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  ....return(1);..
aa70: 09 7d 0a 0a 09 09 63 68 72 6f 6f 74 5f 72 65 74  .}....chroot_ret
aa80: 20 3d 20 63 68 72 6f 6f 74 28 22 2e 22 29 3b 0a   = chroot(".");.
aa90: 09 09 69 66 20 28 63 68 72 6f 6f 74 5f 72 65 74  ..if (chroot_ret
aaa0: 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72   != 0) {....perr
aab0: 6f 72 28 22 63 68 72 6f 6f 74 22 29 3b 0a 0a 09  or("chroot");...
aac0: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d  ..return(1);...}
aad0: 0a 09 7d 0a 0a 09 2f 2a 20 44 72 6f 70 20 70 72  ..}.../* Drop pr
aae0: 69 76 69 6c 65 67 65 73 2c 20 69 66 20 61 70 70  ivileges, if app
aaf0: 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09 69 66 20  ropriate */..if 
ab00: 28 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 29  (setuid_enabled)
ab10: 20 7b 0a 09 09 73 65 74 75 69 64 5f 72 65 74 20   {...setuid_ret 
ab20: 3d 20 73 65 74 75 69 64 28 75 73 65 72 29 3b 0a  = setuid(user);.
ab30: 09 09 69 66 20 28 73 65 74 75 69 64 5f 72 65 74  ..if (setuid_ret
ab40: 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72   != 0) {....perr
ab50: 6f 72 28 22 73 65 74 75 69 64 22 29 3b 0a 0a 09  or("setuid");...
ab60: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d  ..return(1);...}
ab70: 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c  ..}.../* Initial
ab80: 69 7a 65 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74  ize */..init_ret
ab90: 20 3d 20 66 69 6c 65 64 5f 69 6e 69 74 28 63 61   = filed_init(ca
aba0: 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28  che_size);..if (
abb0: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b  init_ret != 0) {
abc0: 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64  ...perror("filed
abd0: 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75  _init");....retu
abe0: 72 6e 28 33 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43  rn(3);..}.../* C
abf0: 72 65 61 74 65 20 6c 6f 67 67 69 6e 67 20 74 68  reate logging th
ac00: 72 65 61 64 20 2a 2f 0a 09 69 6e 69 74 5f 72 65  read */..init_re
ac10: 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e  t = filed_loggin
ac20: 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 6c 6f  g_thread_init(lo
ac30: 67 5f 66 70 29 3b 0a 09 69 66 20 28 69 6e 69 74  g_fp);..if (init
ac40: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70  _ret != 0) {...p
ac50: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 6f 67  error("filed_log
ac60: 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74  ging_thread_init
ac70: 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 34 29  ");....return(4)
ac80: 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65  ;..}.../* Create
ac90: 20 73 6f 63 6b 65 74 20 74 65 72 6d 69 6e 61 74   socket terminat
aca0: 69 6f 6e 20 74 68 72 65 61 64 20 2a 2f 0a 09 69  ion thread */..i
acb0: 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f  nit_ret = filed_
acc0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68  sockettimeout_th
acd0: 72 65 61 64 5f 69 6e 69 74 28 29 3b 0a 09 69 66  read_init();..if
ace0: 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29   (init_ret != 0)
acf0: 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c   {...perror("fil
ad00: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
ad10: 5f 74 68 72 65 61 64 5f 69 6e 69 74 22 29 3b 0a  _thread_init");.
ad20: 0a 09 09 72 65 74 75 72 6e 28 36 29 3b 0a 09 7d  ...return(6);..}
ad30: 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 77 6f 72  .../* Create wor
ad40: 6b 65 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 09  ker threads */..
ad50: 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64  init_ret = filed
ad60: 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f  _worker_threads_
ad70: 69 6e 69 74 28 66 64 2c 20 74 68 72 65 61 64 5f  init(fd, thread_
ad80: 63 6f 75 6e 74 2c 20 26 74 68 72 65 61 64 5f 6f  count, &thread_o
ad90: 70 74 69 6f 6e 73 29 3b 0a 09 69 66 20 28 69 6e  ptions);..if (in
ada0: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09  it_ret != 0) {..
adb0: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 77  .perror("filed_w
adc0: 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e  orker_threads_in
add0: 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28  it");....return(
ade0: 35 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 61 69 74  5);..}.../* Wait
adf0: 20 66 6f 72 20 74 68 72 65 61 64 73 20 74 6f 20   for threads to 
ae00: 65 78 69 74 20 2a 2f 0a 09 2f 2a 20 58 58 58 3a  exit */../* XXX:
ae10: 54 4f 44 4f 3a 20 4d 6f 6e 69 74 6f 72 20 74 68  TODO: Monitor th
ae20: 72 65 61 64 20 75 73 61 67 65 20 2a 2f 0a 09 77  read usage */..w
ae30: 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 73 6c 65  hile (1) {...sle
ae40: 65 70 28 36 30 29 3b 0a 09 7d 0a 0a 09 2f 2a 20  ep(60);..}.../* 
ae50: 52 65 74 75 72 6e 20 69 6e 20 66 61 69 6c 75 72  Return in failur
ae60: 65 20 2a 2f 0a 09 72 65 74 75 72 6e 28 32 29 3b  e */..return(2);
ae70: 0a 7d 0a                                         .}.