Hex Artifact Content

Artifact d39c0feefb3f9113df79e627c9ce3909c6737bf3:


0000: 2f 2a 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20  /*. * Copyright 
0010: 28 63 29 20 32 30 31 34 20 2d 20 32 30 31 36 2c  (c) 2014 - 2016,
0020: 20 52 6f 79 20 4b 65 65 6e 65 0a 20 2a 20 41 6c   Roy Keene. * Al
0030: 6c 20 72 69 67 68 74 73 20 72 65 73 65 72 76 65  l rights reserve
0040: 64 2e 0a 20 2a 20 0a 20 2a 20 52 65 64 69 73 74  d.. * . * Redist
0050: 72 69 62 75 74 69 6f 6e 20 61 6e 64 20 75 73 65  ribution and use
0060: 20 69 6e 20 73 6f 75 72 63 65 20 61 6e 64 20 62   in source and b
0070: 69 6e 61 72 79 20 66 6f 72 6d 73 2c 20 77 69 74  inary forms, wit
0080: 68 20 6f 72 20 77 69 74 68 6f 75 74 0a 20 2a 20  h or without. * 
0090: 6d 6f 64 69 66 69 63 61 74 69 6f 6e 2c 20 61 72  modification, ar
00a0: 65 20 70 65 72 6d 69 74 74 65 64 20 70 72 6f 76  e permitted prov
00b0: 69 64 65 64 20 74 68 61 74 20 74 68 65 20 66 6f  ided that the fo
00c0: 6c 6c 6f 77 69 6e 67 20 63 6f 6e 64 69 74 69 6f  llowing conditio
00d0: 6e 73 20 61 72 65 20 6d 65 74 3a 0a 20 2a 20 09  ns are met:. * .
00e0: 31 2e 20 52 65 64 69 73 74 72 69 62 75 74 69 6f  1. Redistributio
00f0: 6e 73 20 6f 66 20 73 6f 75 72 63 65 20 63 6f 64  ns of source cod
0100: 65 20 6d 75 73 74 20 72 65 74 61 69 6e 20 74 68  e must retain th
0110: 65 20 61 62 6f 76 65 20 63 6f 70 79 72 69 67 68  e above copyrigh
0120: 74 0a 20 2a 20 09 20 20 20 6e 6f 74 69 63 65 2c  t. * .   notice,
0130: 20 74 68 69 73 20 6c 69 73 74 20 6f 66 20 63 6f   this list of co
0140: 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nditions and the
0150: 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 69 73 63 6c   following discl
0160: 61 69 6d 65 72 2e 0a 20 2a 20 09 32 2e 20 52 65  aimer.. * .2. Re
0170: 64 69 73 74 72 69 62 75 74 69 6f 6e 73 20 69 6e  distributions in
0180: 20 62 69 6e 61 72 79 20 66 6f 72 6d 20 6d 75 73   binary form mus
0190: 74 20 72 65 70 72 6f 64 75 63 65 20 74 68 65 20  t reproduce the 
01a0: 61 62 6f 76 65 20 63 6f 70 79 72 69 67 68 74 0a  above copyright.
01b0: 20 2a 20 09 20 20 20 6e 6f 74 69 63 65 2c 20 74   * .   notice, t
01c0: 68 69 73 20 6c 69 73 74 20 6f 66 20 63 6f 6e 64  his list of cond
01d0: 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 66  itions and the f
01e0: 6f 6c 6c 6f 77 69 6e 67 20 64 69 73 63 6c 61 69  ollowing disclai
01f0: 6d 65 72 20 69 6e 20 74 68 65 0a 20 2a 20 09 20  mer in the. * . 
0200: 20 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20    documentation 
0210: 61 6e 64 2f 6f 72 20 6f 74 68 65 72 20 6d 61 74  and/or other mat
0220: 65 72 69 61 6c 73 20 70 72 6f 76 69 64 65 64 20  erials provided 
0230: 77 69 74 68 20 74 68 65 20 64 69 73 74 72 69 62  with the distrib
0240: 75 74 69 6f 6e 2e 0a 20 2a 20 0a 20 2a 20 54 48  ution.. * . * TH
0250: 49 53 20 53 4f 46 54 57 41 52 45 20 49 53 20 50  IS SOFTWARE IS P
0260: 52 4f 56 49 44 45 44 20 42 59 20 54 48 45 20 43  ROVIDED BY THE C
0270: 4f 50 59 52 49 47 48 54 20 48 4f 4c 44 45 52 53  OPYRIGHT HOLDERS
0280: 20 41 4e 44 20 43 4f 4e 54 52 49 42 55 54 4f 52   AND CONTRIBUTOR
0290: 53 20 22 41 53 20 49 53 22 20 0a 20 2a 20 41 4e  S "AS IS" . * AN
02a0: 44 20 41 4e 59 20 45 58 50 52 45 53 53 20 4f 52  D ANY EXPRESS OR
02b0: 20 49 4d 50 4c 49 45 44 20 57 41 52 52 41 4e 54   IMPLIED WARRANT
02c0: 49 45 53 2c 20 49 4e 43 4c 55 44 49 4e 47 2c 20  IES, INCLUDING, 
02d0: 42 55 54 20 4e 4f 54 20 4c 49 4d 49 54 45 44 20  BUT NOT LIMITED 
02e0: 54 4f 2c 20 54 48 45 20 0a 20 2a 20 49 4d 50 4c  TO, THE . * IMPL
02f0: 49 45 44 20 57 41 52 52 41 4e 54 49 45 53 20 4f  IED WARRANTIES O
0300: 46 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  F MERCHANTABILIT
0310: 59 20 41 4e 44 20 46 49 54 4e 45 53 53 20 46 4f  Y AND FITNESS FO
0320: 52 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50  R A PARTICULAR P
0330: 55 52 50 4f 53 45 20 0a 20 2a 20 41 52 45 20 44  URPOSE . * ARE D
0340: 49 53 43 4c 41 49 4d 45 44 2e 20 20 49 4e 20 4e  ISCLAIMED.  IN N
0350: 4f 20 45 56 45 4e 54 20 53 48 41 4c 4c 20 54 48  O EVENT SHALL TH
0360: 45 20 43 4f 50 59 52 49 47 48 54 20 48 4f 4c 44  E COPYRIGHT HOLD
0370: 45 52 20 4f 52 20 43 4f 4e 54 52 49 42 55 54 4f  ER OR CONTRIBUTO
0380: 52 53 20 42 45 20 0a 20 2a 20 4c 49 41 42 4c 45  RS BE . * LIABLE
0390: 20 46 4f 52 20 41 4e 59 20 44 49 52 45 43 54 2c   FOR ANY DIRECT,
03a0: 20 49 4e 44 49 52 45 43 54 2c 20 49 4e 43 49 44   INDIRECT, INCID
03b0: 45 4e 54 41 4c 2c 20 53 50 45 43 49 41 4c 2c 20  ENTAL, SPECIAL, 
03c0: 45 58 45 4d 50 4c 41 52 59 2c 20 4f 52 20 0a 20  EXEMPLARY, OR . 
03d0: 2a 20 43 4f 4e 53 45 51 55 45 4e 54 49 41 4c 20  * CONSEQUENTIAL 
03e0: 44 41 4d 41 47 45 53 20 28 49 4e 43 4c 55 44 49  DAMAGES (INCLUDI
03f0: 4e 47 2c 20 42 55 54 20 4e 4f 54 20 4c 49 4d 49  NG, BUT NOT LIMI
0400: 54 45 44 20 54 4f 2c 20 50 52 4f 43 55 52 45 4d  TED TO, PROCUREM
0410: 45 4e 54 20 4f 46 20 0a 20 2a 20 53 55 42 53 54  ENT OF . * SUBST
0420: 49 54 55 54 45 20 47 4f 4f 44 53 20 4f 52 20 53  ITUTE GOODS OR S
0430: 45 52 56 49 43 45 53 3b 20 4c 4f 53 53 20 4f 46  ERVICES; LOSS OF
0440: 20 55 53 45 2c 20 44 41 54 41 2c 20 4f 52 20 50   USE, DATA, OR P
0450: 52 4f 46 49 54 53 3b 20 4f 52 20 42 55 53 49 4e  ROFITS; OR BUSIN
0460: 45 53 53 20 0a 20 2a 20 49 4e 54 45 52 52 55 50  ESS . * INTERRUP
0470: 54 49 4f 4e 29 20 48 4f 57 45 56 45 52 20 43 41  TION) HOWEVER CA
0480: 55 53 45 44 20 41 4e 44 20 4f 4e 20 41 4e 59 20  USED AND ON ANY 
0490: 54 48 45 4f 52 59 20 4f 46 20 4c 49 41 42 49 4c  THEORY OF LIABIL
04a0: 49 54 59 2c 20 57 48 45 54 48 45 52 20 49 4e 20  ITY, WHETHER IN 
04b0: 0a 20 2a 20 43 4f 4e 54 52 41 43 54 2c 20 53 54  . * CONTRACT, ST
04c0: 52 49 43 54 20 4c 49 41 42 49 4c 49 54 59 2c 20  RICT LIABILITY, 
04d0: 4f 52 20 54 4f 52 54 20 28 49 4e 43 4c 55 44 49  OR TORT (INCLUDI
04e0: 4e 47 20 4e 45 47 4c 49 47 45 4e 43 45 20 4f 52  NG NEGLIGENCE OR
04f0: 20 4f 54 48 45 52 57 49 53 45 29 20 0a 20 2a 20   OTHERWISE) . * 
0500: 41 52 49 53 49 4e 47 20 49 4e 20 41 4e 59 20 57  ARISING IN ANY W
0510: 41 59 20 4f 55 54 20 4f 46 20 54 48 45 20 55 53  AY OUT OF THE US
0520: 45 20 4f 46 20 54 48 49 53 20 53 4f 46 54 57 41  E OF THIS SOFTWA
0530: 52 45 2c 20 45 56 45 4e 20 49 46 20 41 44 56 49  RE, EVEN IF ADVI
0540: 53 45 44 20 4f 46 20 54 48 45 20 0a 20 2a 20 50  SED OF THE . * P
0550: 4f 53 53 49 42 49 4c 49 54 59 20 4f 46 20 53 55  OSSIBILITY OF SU
0560: 43 48 20 44 41 4d 41 47 45 2e 0a 20 2a 2f 0a 23  CH DAMAGE.. */.#
0570: 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 65 6e  include <sys/sen
0580: 64 66 69 6c 65 2e 68 3e 0a 23 69 6e 63 6c 75 64  dfile.h>.#includ
0590: 65 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68 3e  e <sys/socket.h>
05a0: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74  .#include <sys/t
05b0: 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  ypes.h>.#include
05c0: 20 3c 61 72 70 61 2f 69 6e 65 74 2e 68 3e 0a 23   <arpa/inet.h>.#
05d0: 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d 61  include <sys/mma
05e0: 6e 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  n.h>.#include <s
05f0: 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 6c  ys/stat.h>.#incl
0600: 75 64 65 20 3c 73 79 73 2f 77 61 69 74 2e 68 3e  ude <sys/wait.h>
0610: 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65  .#include <pthre
0620: 61 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  ad.h>.#include <
0630: 73 74 72 69 6e 67 73 2e 68 3e 0a 23 69 6e 63 6c  strings.h>.#incl
0640: 75 64 65 20 3c 73 69 67 6e 61 6c 2e 68 3e 0a 23  ude <signal.h>.#
0650: 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e  include <stdlib.
0660: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
0670: 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  std.h>.#include 
0680: 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c  <string.h>.#incl
0690: 75 64 65 20 3c 67 65 74 6f 70 74 2e 68 3e 0a 23  ude <getopt.h>.#
06a0: 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e  include <stdarg.
06b0: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e  h>.#include <fcn
06c0: 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  tl.h>.#include <
06d0: 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64  stdio.h>.#includ
06e0: 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 23 69 6e 63  e <errno.h>.#inc
06f0: 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 23 69  lude <time.h>.#i
0700: 6e 63 6c 75 64 65 20 3c 70 77 64 2e 68 3e 0a 0a  nclude <pwd.h>..
0710: 2f 2a 20 43 6f 6d 70 69 6c 65 20 74 69 6d 65 20  /* Compile time 
0720: 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 23 64 65  constants */.#de
0730: 66 69 6e 65 20 46 49 4c 45 44 5f 56 45 52 53 49  fine FILED_VERSI
0740: 4f 4e 20 22 31 2e 32 33 22 0a 23 64 65 66 69 6e  ON "1.23".#defin
0750: 65 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45  e FILED_SENDFILE
0760: 5f 4d 41 58 20 31 36 37 37 37 32 31 35 0a 23 64  _MAX 16777215.#d
0770: 65 66 69 6e 65 20 46 49 4c 45 44 5f 4d 41 58 5f  efine FILED_MAX_
0780: 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 20 33 30  FAILURE_COUNT 30
0790: 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 44  .#define FILED_D
07a0: 45 46 41 55 4c 54 5f 54 59 50 45 20 22 61 70 70  EFAULT_TYPE "app
07b0: 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d 73  lication/octet-s
07c0: 74 72 65 61 6d 22 0a 23 64 65 66 69 6e 65 20 46  tream".#define F
07d0: 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52  ILED_PATH_BUFFER
07e0: 5f 53 49 5a 45 20 31 30 31 30 0a 0a 2f 2a 20 44  _SIZE 1010../* D
07f0: 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f  efault values */
0800: 0a 23 64 65 66 69 6e 65 20 50 4f 52 54 20 38 30  .#define PORT 80
0810: 0a 23 64 65 66 69 6e 65 20 54 48 52 45 41 44 5f  .#define THREAD_
0820: 43 4f 55 4e 54 20 35 0a 23 64 65 66 69 6e 65 20  COUNT 5.#define 
0830: 42 49 4e 44 5f 41 44 44 52 20 22 3a 3a 22 0a 23  BIND_ADDR "::".#
0840: 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 49 5a  define CACHE_SIZ
0850: 45 20 38 32 30 39 0a 23 64 65 66 69 6e 65 20 4c  E 8209.#define L
0860: 4f 47 5f 46 49 4c 45 20 22 2d 22 0a 0a 2f 2a 20  OG_FILE "-"../* 
0870: 46 75 7a 7a 69 6e 67 20 54 65 73 74 20 43 6f 64  Fuzzing Test Cod
0880: 65 20 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c 45  e */.#ifdef FILE
0890: 44 5f 54 45 53 54 5f 41 46 4c 0a 23 64 65 66 69  D_TEST_AFL.#defi
08a0: 6e 65 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f  ne FILED_DONT_LO
08b0: 47 20 31 0a 23 64 65 66 69 6e 65 20 46 49 4c 45  G 1.#define FILE
08c0: 44 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55 54 20 31  D_DONT_TIMEOUT 1
08d0: 0a 23 64 65 66 69 6e 65 20 70 74 68 72 65 61 64  .#define pthread
08e0: 5f 63 72 65 61 74 65 28 61 2c 20 78 2c 20 79 2c  _create(a, x, y,
08f0: 20 7a 29 20 61 66 6c 5f 70 74 68 72 65 61 64 5f   z) afl_pthread_
0900: 63 72 65 61 74 65 28 61 2c 20 78 2c 20 79 2c 20  create(a, x, y, 
0910: 7a 29 0a 23 64 65 66 69 6e 65 20 62 69 6e 64 28  z).#define bind(
0920: 78 2c 20 79 2c 20 7a 29 20 61 66 6c 5f 62 69 6e  x, y, z) afl_bin
0930: 64 28 78 2c 20 79 2c 20 7a 29 0a 23 64 65 66 69  d(x, y, z).#defi
0940: 6e 65 20 73 6f 63 6b 65 74 28 78 2c 20 79 2c 20  ne socket(x, y, 
0950: 7a 29 20 38 31 39 33 0a 23 64 65 66 69 6e 65 20  z) 8193.#define 
0960: 6c 69 73 74 65 6e 28 78 2c 20 79 29 20 30 0a 23  listen(x, y) 0.#
0970: 64 65 66 69 6e 65 20 61 63 63 65 70 74 28 78 2c  define accept(x,
0980: 20 79 2c 20 7a 29 20 61 66 6c 5f 61 63 63 65 70   y, z) afl_accep
0990: 74 28 78 2c 20 79 2c 20 7a 29 0a 23 64 65 66 69  t(x, y, z).#defi
09a0: 6e 65 20 63 6c 6f 73 65 28 78 29 20 7b 20 69 66  ne close(x) { if
09b0: 20 28 73 74 72 63 6d 70 28 23 78 2c 20 22 72 61   (strcmp(#x, "ra
09c0: 6e 64 6f 6d 5f 66 64 22 29 20 3d 3d 20 30 29 20  ndom_fd") == 0) 
09d0: 7b 20 63 6c 6f 73 65 28 78 29 3b 20 7d 20 65 6c  { close(x); } el
09e0: 73 65 20 7b 20 65 78 69 74 28 30 29 3b 20 7d 20  se { exit(0); } 
09f0: 7d 0a 23 64 65 66 69 6e 65 20 66 63 6c 6f 73 65  }.#define fclose
0a00: 28 78 29 20 65 78 69 74 28 30 29 0a 0a 73 74 61  (x) exit(0)..sta
0a10: 74 69 63 20 69 6e 74 20 61 66 6c 5f 61 63 63 65  tic int afl_acce
0a20: 70 74 28 69 6e 74 20 78 2c 20 76 6f 69 64 20 2a  pt(int x, void *
0a30: 61 64 64 72 2c 20 76 6f 69 64 20 2a 7a 29 20 7b  addr, void *z) {
0a40: 0a 09 28 28 73 74 72 75 63 74 20 73 6f 63 6b 61  ..((struct socka
0a50: 64 64 72 5f 69 6e 36 20 2a 29 20 61 64 64 72 29  ddr_in6 *) addr)
0a60: 2d 3e 73 69 6e 36 5f 66 61 6d 69 6c 79 20 3d 20  ->sin6_family = 
0a70: 41 46 5f 49 4e 45 54 20 2b 20 41 46 5f 49 4e 45  AF_INET + AF_INE
0a80: 54 36 20 2b 20 31 3b 0a 09 72 65 74 75 72 6e 28  T6 + 1;..return(
0a90: 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09  STDIN_FILENO);..
0aa0: 78 20 3d 20 78 3b 0a 09 7a 20 3d 20 7a 3b 0a 7d  x = x;..z = z;.}
0ab0: 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 6c  ..static int afl
0ac0: 5f 62 69 6e 64 28 69 6e 74 20 78 2c 20 76 6f 69  _bind(int x, voi
0ad0: 64 20 2a 79 2c 20 73 6f 63 6b 6c 65 6e 5f 74 20  d *y, socklen_t 
0ae0: 7a 29 20 7b 0a 09 72 65 74 75 72 6e 28 38 31 39  z) {..return(819
0af0: 34 29 3b 0a 09 78 20 3d 20 78 3b 0a 09 79 20 3d  4);..x = x;..y =
0b00: 20 79 3b 0a 09 7a 20 3d 20 7a 3b 0a 7d 0a 0a 73   y;..z = z;.}..s
0b10: 74 61 74 69 63 20 69 6e 74 20 61 66 6c 5f 70 74  tatic int afl_pt
0b20: 68 72 65 61 64 5f 63 72 65 61 74 65 28 70 74 68  hread_create(pth
0b30: 72 65 61 64 5f 74 20 2a 74 68 72 65 61 64 2c 20  read_t *thread, 
0b40: 63 6f 6e 73 74 20 70 74 68 72 65 61 64 5f 61 74  const pthread_at
0b50: 74 72 5f 74 20 2a 61 74 74 72 2c 20 76 6f 69 64  tr_t *attr, void
0b60: 20 2a 28 2a 73 74 61 72 74 5f 72 6f 75 74 69 6e   *(*start_routin
0b70: 65 29 20 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69  e) (void *), voi
0b80: 64 20 2a 61 72 67 29 20 7b 0a 09 73 74 61 72 74  d *arg) {..start
0b90: 5f 72 6f 75 74 69 6e 65 28 61 72 67 29 3b 0a 09  _routine(arg);..
0ba0: 65 78 69 74 28 33 29 3b 0a 09 74 68 72 65 61 64  exit(3);..thread
0bb0: 20 3d 20 74 68 72 65 61 64 3b 0a 09 61 74 74 72   = thread;..attr
0bc0: 20 3d 20 61 74 74 72 3b 0a 7d 0a 23 65 6e 64 69   = attr;.}.#endi
0bd0: 66 0a 0a 2f 2a 20 43 6f 6e 66 69 67 75 72 61 74  f../* Configurat
0be0: 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74  ion options that
0bf0: 20 77 6f 72 6b 20 74 68 72 65 61 64 73 20 6e 65   work threads ne
0c00: 65 64 20 74 6f 20 62 65 20 61 77 61 72 65 20 6f  ed to be aware o
0c10: 66 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65  f */.struct file
0c20: 64 5f 6f 70 74 69 6f 6e 73 20 7b 0a 09 69 6e 74  d_options {..int
0c30: 20 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 64 3b   vhosts_enabled;
0c40: 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 61  ..const char *fa
0c50: 6b 65 5f 6e 65 77 72 6f 6f 74 3b 0a 7d 3b 0a 0a  ke_newroot;.};..
0c60: 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72  /* Arguments for
0c70: 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20   worker threads 
0c80: 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f  */.struct filed_
0c90: 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72  worker_thread_ar
0ca0: 67 73 20 7b 0a 09 69 6e 74 20 66 64 3b 0a 09 73  gs {..int fd;..s
0cb0: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69  truct filed_opti
0cc0: 6f 6e 73 20 6f 70 74 69 6f 6e 73 3b 0a 7d 3b 0a  ons options;.};.
0cd0: 0a 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f  ./* Arguments fo
0ce0: 72 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64  r logging thread
0cf0: 73 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65  s */.struct file
0d00: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64  d_logging_thread
0d10: 5f 61 72 67 73 20 7b 0a 09 46 49 4c 45 20 2a 66  _args {..FILE *f
0d20: 70 3b 0a 7d 3b 0a 0a 2f 2a 20 46 69 6c 65 20 69  p;.};../* File i
0d30: 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74  nformation */.st
0d40: 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69  ruct filed_filei
0d50: 6e 66 6f 20 7b 0a 09 70 74 68 72 65 61 64 5f 6d  nfo {..pthread_m
0d60: 75 74 65 78 5f 74 20 6d 75 74 65 78 3b 0a 09 63  utex_t mutex;..c
0d70: 68 61 72 20 70 61 74 68 5b 46 49 4c 45 44 5f 50  har path[FILED_P
0d80: 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d  ATH_BUFFER_SIZE]
0d90: 3b 0a 09 69 6e 74 20 66 64 3b 0a 09 6f 66 66 5f  ;..int fd;..off_
0da0: 74 20 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 6c 61  t len;..char *la
0db0: 73 74 6d 6f 64 3b 0a 09 63 68 61 72 20 6c 61 73  stmod;..char las
0dc0: 74 6d 6f 64 5f 62 5b 36 34 5d 3b 0a 09 63 6f 6e  tmod_b[64];..con
0dd0: 73 74 20 63 68 61 72 20 2a 74 79 70 65 3b 0a 09  st char *type;..
0de0: 63 68 61 72 20 65 74 61 67 5b 36 34 5d 3b 0a 7d  char etag[64];.}
0df0: 3b 0a 0a 2f 2a 20 52 65 71 75 65 73 74 20 76 61  ;../* Request va
0e00: 72 69 61 62 6c 65 73 20 2a 2f 0a 73 74 72 75 63  riables */.struc
0e10: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71  t filed_http_req
0e20: 75 65 73 74 20 7b 0a 09 2f 2a 2a 20 42 75 66 66  uest {../** Buff
0e30: 65 72 73 20 2a 2a 2f 0a 09 73 74 72 75 63 74 20  ers **/..struct 
0e40: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 66  filed_fileinfo f
0e50: 69 6c 65 69 6e 66 6f 3b 0a 09 63 68 61 72 20 74  ileinfo;..char t
0e60: 6d 70 62 75 66 5b 46 49 4c 45 44 5f 50 41 54 48  mpbuf[FILED_PATH
0e70: 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b 0a 0a  _BUFFER_SIZE];..
0e80: 09 2f 2a 2a 20 48 54 54 50 20 52 65 71 75 65 73  ./** HTTP Reques
0e90: 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2a  t information **
0ea0: 2f 0a 09 2f 2a 2a 2a 20 54 79 70 65 20 6f 66 20  /../*** Type of 
0eb0: 72 65 71 75 65 73 74 20 28 48 45 41 44 20 6f 72  request (HEAD or
0ec0: 20 47 45 54 29 20 2a 2a 2a 2f 0a 09 65 6e 75 6d   GET) ***/..enum
0ed0: 20 7b 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45   {...FILED_REQUE
0ee0: 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 2c 0a 09  ST_METHOD_GET,..
0ef0: 09 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d  .FILED_REQUEST_M
0f00: 45 54 48 4f 44 5f 48 45 41 44 0a 09 7d 20 6d 65  ETHOD_HEAD..} me
0f10: 74 68 6f 64 3b 0a 0a 09 2f 2a 2a 2a 20 50 61 74  thod;.../*** Pat
0f20: 68 20 62 65 69 6e 67 20 72 65 71 75 65 73 74 65  h being requeste
0f30: 64 20 2a 2a 2a 2f 0a 09 63 68 61 72 20 70 61 74  d ***/..char pat
0f40: 68 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46  h[FILED_PATH_BUF
0f50: 46 45 52 5f 53 49 5a 45 5d 3b 20 0a 0a 09 2f 2a  FER_SIZE]; .../*
0f60: 2a 2a 20 50 61 74 68 20 74 79 70 65 20 2a 2a 2a  ** Path type ***
0f70: 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 4c 45  /..enum {...FILE
0f80: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44  D_REQUEST_TYPE_D
0f90: 49 52 45 43 54 4f 52 59 2c 0a 09 09 46 49 4c 45  IRECTORY,...FILE
0fa0: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 4f  D_REQUEST_TYPE_O
0fb0: 54 48 45 52 0a 09 7d 20 74 79 70 65 3b 0a 0a 09  THER..} type;...
0fc0: 73 74 72 75 63 74 20 7b 0a 09 09 73 74 72 75 63  struct {...struc
0fd0: 74 20 7b 0a 09 09 09 69 6e 74 20 70 72 65 73 65  t {....int prese
0fe0: 6e 74 3b 0a 09 09 09 6f 66 66 5f 74 20 6f 66 66  nt;....off_t off
0ff0: 73 65 74 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67  set;   /*** Rang
1000: 65 20 73 74 61 72 74 20 2a 2a 2a 2f 0a 09 09 09  e start ***/....
1010: 6f 66 66 5f 74 20 6c 65 6e 67 74 68 3b 20 20 20  off_t length;   
1020: 2f 2a 2a 2a 20 52 61 6e 67 65 20 6c 65 6e 67 74  /*** Range lengt
1030: 68 20 2a 2a 2a 2f 0a 09 09 7d 20 72 61 6e 67 65  h ***/...} range
1040: 3b 0a 0a 09 09 73 74 72 75 63 74 20 7b 0a 09 09  ;....struct {...
1050: 09 69 6e 74 20 70 72 65 73 65 6e 74 3b 0a 09 09  .int present;...
1060: 09 63 68 61 72 20 68 6f 73 74 5b 46 49 4c 45 44  .char host[FILED
1070: 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a  _PATH_BUFFER_SIZ
1080: 45 5d 3b 0a 09 09 7d 20 68 6f 73 74 3b 0a 0a 09  E];...} host;...
1090: 09 65 6e 75 6d 20 7b 0a 09 09 09 46 49 4c 45 44  .enum {....FILED
10a0: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53  _CONNECTION_CLOS
10b0: 45 2c 0a 09 09 09 46 49 4c 45 44 5f 43 4f 4e 4e  E,....FILED_CONN
10c0: 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56  ECTION_KEEP_ALIV
10d0: 45 0a 09 09 7d 20 63 6f 6e 6e 65 63 74 69 6f 6e  E...} connection
10e0: 3b 0a 09 7d 20 68 65 61 64 65 72 73 3b 0a 7d 3b  ;..} headers;.};
10f0: 0a 0a 2f 2a 20 4c 6f 67 20 72 65 63 6f 72 64 20  ../* Log record 
1100: 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f  */.struct filed_
1110: 6c 6f 67 5f 65 6e 74 72 79 20 7b 0a 09 2f 2a 20  log_entry {../* 
1120: 54 79 70 65 20 6f 66 20 6c 6f 67 20 65 6e 74 72  Type of log entr
1130: 79 20 2a 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46  y */..enum {...F
1140: 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45  ILED_LOG_TYPE_ME
1150: 53 53 41 47 45 2c 0a 09 09 46 49 4c 45 44 5f 4c  SSAGE,...FILED_L
1160: 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 52  OG_TYPE_TRANSFER
1170: 0a 09 7d 20 74 79 70 65 3b 0a 0a 09 2f 2a 20 4c  ..} type;.../* L
1180: 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 2f  inked list head/
1190: 74 61 69 6c 20 2a 2f 0a 09 73 74 72 75 63 74 20  tail */..struct 
11a0: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20  filed_log_entry 
11b0: 2a 5f 6e 65 78 74 3b 0a 09 73 74 72 75 63 74 20  *_next;..struct 
11c0: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20  filed_log_entry 
11d0: 2a 5f 70 72 65 76 3b 0a 0a 09 2f 2a 20 54 68 72  *_prev;.../* Thr
11e0: 65 61 64 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ead from which t
11f0: 68 69 73 20 6c 6f 67 20 65 6e 74 72 79 20 65 6d  his log entry em
1200: 69 6e 61 74 65 73 20 2a 2f 0a 09 70 74 68 72 65  inates */..pthre
1210: 61 64 5f 74 20 74 68 72 65 61 64 3b 0a 0a 09 2f  ad_t thread;.../
1220: 2a 20 4d 65 73 73 61 67 65 20 62 75 66 66 65 72  * Message buffer
1230: 20 66 6f 72 20 74 79 70 65 20 3d 20 4d 45 53 53   for type = MESS
1240: 41 47 45 20 2a 2f 0a 09 2f 2a 20 50 61 74 68 20  AGE */../* Path 
1250: 62 75 66 66 65 72 20 66 6f 72 20 74 79 70 65 20  buffer for type 
1260: 3d 20 54 52 41 4e 53 46 45 52 20 2a 2f 0a 09 63  = TRANSFER */..c
1270: 68 61 72 20 62 75 66 66 65 72 5b 46 49 4c 45 44  har buffer[FILED
1280: 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a  _PATH_BUFFER_SIZ
1290: 45 5d 3b 0a 0a 09 2f 2a 20 49 74 65 6d 73 20 66  E];.../* Items f
12a0: 6f 72 20 74 79 70 65 20 3d 20 54 52 41 4e 53 46  or type = TRANSF
12b0: 45 52 20 2a 2f 0a 09 69 6e 74 20 68 74 74 70 5f  ER */..int http_
12c0: 63 6f 64 65 3b 0a 09 63 6f 6e 73 74 20 63 68 61  code;..const cha
12d0: 72 20 2a 72 65 61 73 6f 6e 3b 0a 09 74 69 6d 65  r *reason;..time
12e0: 5f 74 20 63 6f 6e 6e 65 63 74 74 69 6d 65 3b 0a  _t connecttime;.
12f0: 09 74 69 6d 65 5f 74 20 73 74 61 72 74 74 69 6d  .time_t starttim
1300: 65 3b 0a 09 74 69 6d 65 5f 74 20 65 6e 64 74 69  e;..time_t endti
1310: 6d 65 3b 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6f  me;..off_t req_o
1320: 66 66 73 65 74 3b 0a 09 6f 66 66 5f 74 20 72 65  ffset;..off_t re
1330: 71 5f 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 5f 74  q_length;..off_t
1340: 20 73 65 6e 74 5f 6c 65 6e 67 74 68 3b 0a 09 6f   sent_length;..o
1350: 66 66 5f 74 20 66 69 6c 65 5f 6c 65 6e 67 74 68  ff_t file_length
1360: 3b 0a 09 63 68 61 72 20 69 70 5b 31 32 38 5d 3b  ;..char ip[128];
1370: 0a 09 69 6e 74 20 70 6f 72 74 3b 0a 09 69 6e 74  ..int port;..int
1380: 20 6d 65 74 68 6f 64 3b 0a 7d 3b 0a 0a 2f 2a 20   method;.};../* 
1390: 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73  Global variables
13a0: 20 2a 2f 0a 2f 2a 2a 20 4f 70 65 6e 20 46 69 6c   */./** Open Fil
13b0: 65 20 63 61 63 68 65 20 2a 2a 2f 0a 73 74 72 75  e cache **/.stru
13c0: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  ct filed_fileinf
13d0: 6f 20 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  o *filed_fileinf
13e0: 6f 5f 66 64 63 61 63 68 65 20 3d 20 4e 55 4c 4c  o_fdcache = NULL
13f0: 3b 0a 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 66  ;.unsigned int f
1400: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64  iled_fileinfo_fd
1410: 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 30 3b 0a  cache_size = 0;.
1420: 0a 2f 2a 2a 20 4c 6f 67 67 69 6e 67 20 2a 2a 2f  ./** Logging **/
1430: 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f  .struct filed_lo
1440: 67 5f 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c  g_entry *filed_l
1450: 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a 70 74 68  og_msg_list;.pth
1460: 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 66 69 6c  read_mutex_t fil
1470: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f  ed_log_msg_list_
1480: 6d 75 74 65 78 20 3d 20 50 54 48 52 45 41 44 5f  mutex = PTHREAD_
1490: 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
14a0: 52 3b 0a 70 74 68 72 65 61 64 5f 63 6f 6e 64 5f  R;.pthread_cond_
14b0: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  t filed_log_msg_
14c0: 6c 69 73 74 5f 72 65 61 64 79 20 3d 20 50 54 48  list_ready = PTH
14d0: 52 45 41 44 5f 43 4f 4e 44 5f 49 4e 49 54 49 41  READ_COND_INITIA
14e0: 4c 49 5a 45 52 3b 0a 0a 2f 2a 20 53 69 67 6e 61  LIZER;../* Signa
14f0: 6c 20 48 61 6e 64 6c 65 72 20 2a 2f 0a 73 74 61  l Handler */.sta
1500: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73  tic void filed_s
1510: 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 28 69 6e  ignal_handler(in
1520: 74 20 73 69 67 6e 61 6c 5f 6e 75 6d 62 65 72 29  t signal_number)
1530: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64   {..struct filed
1540: 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 63 68 65  _fileinfo *cache
1550: 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  ;..unsigned int 
1560: 69 64 78 3b 0a 0a 09 73 77 69 74 63 68 20 28 73  idx;...switch (s
1570: 69 67 6e 61 6c 5f 6e 75 6d 62 65 72 29 20 7b 0a  ignal_number) {.
1580: 09 09 63 61 73 65 20 53 49 47 48 55 50 3a 0a 09  ..case SIGHUP:..
1590: 09 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20  ..for (idx = 0; 
15a0: 69 64 78 20 3c 20 66 69 6c 65 64 5f 66 69 6c 65  idx < filed_file
15b0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a  info_fdcache_siz
15c0: 65 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09 09 09  e; idx++) {.....
15d0: 63 61 63 68 65 20 3d 20 26 66 69 6c 65 64 5f 66  cache = &filed_f
15e0: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b  ileinfo_fdcache[
15f0: 69 64 78 5d 3b 0a 0a 09 09 09 09 70 74 68 72 65  idx];......pthre
1600: 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 63  ad_mutex_lock(&c
1610: 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09  ache->mutex);...
1620: 09 09 09 63 61 63 68 65 2d 3e 70 61 74 68 5b 30  ...cache->path[0
1630: 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 09 09 69 66  ] = '\0';.....if
1640: 20 28 63 61 63 68 65 2d 3e 66 64 20 3e 3d 20 30   (cache->fd >= 0
1650: 29 20 7b 0a 09 09 09 09 09 63 6c 6f 73 65 28 63  ) {......close(c
1660: 61 63 68 65 2d 3e 66 64 29 3b 0a 0a 09 09 09 09  ache->fd);......
1670: 09 63 61 63 68 65 2d 3e 66 64 20 3d 20 2d 31 3b  .cache->fd = -1;
1680: 0a 09 09 09 09 7d 0a 0a 09 09 09 09 63 61 63 68  .....}......cach
1690: 65 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20 22 22 3b  e->lastmod = "";
16a0: 0a 09 09 09 09 63 61 63 68 65 2d 3e 74 79 70 65  .....cache->type
16b0: 20 3d 20 22 22 3b 0a 0a 09 09 09 09 70 74 68 72   = "";......pthr
16c0: 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b  ead_mutex_unlock
16d0: 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b  (&cache->mutex);
16e0: 0a 09 09 09 7d 0a 09 09 09 62 72 65 61 6b 3b 0a  ....}....break;.
16f0: 09 7d 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a  .}...return;.}..
1700: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 63 61  /* Initialize ca
1710: 63 68 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  che */.static in
1720: 74 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63  t filed_init_cac
1730: 68 65 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  he(unsigned int 
1740: 63 61 63 68 65 5f 73 69 7a 65 29 20 7b 0a 09 75  cache_size) {..u
1750: 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 64 78 3b  nsigned int idx;
1760: 0a 09 69 6e 74 20 6d 75 74 65 78 5f 69 6e 69 74  ..int mutex_init
1770: 5f 72 65 74 3b 0a 0a 09 2f 2a 20 43 61 63 68 65  _ret;.../* Cache
1780: 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 2d 69   may not be re-i
1790: 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 09 69  nitialized */..i
17a0: 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  f (filed_fileinf
17b0: 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 21  o_fdcache_size !
17c0: 3d 20 30 20 7c 7c 20 66 69 6c 65 64 5f 66 69 6c  = 0 || filed_fil
17d0: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 21 3d  einfo_fdcache !=
17e0: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72   NULL) {...retur
17f0: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 61  n(1);..}.../* Ca
1800: 63 68 65 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  che does not nee
1810: 64 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65  d to be allocate
1820: 64 20 69 66 20 63 61 63 68 65 20 69 73 20 6e 6f  d if cache is no
1830: 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 09 69 66  t enabled */..if
1840: 20 28 63 61 63 68 65 5f 73 69 7a 65 20 3d 3d 20   (cache_size == 
1850: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29  0) {...return(0)
1860: 3b 0a 09 7d 0a 0a 09 2f 2a 20 41 6c 6c 6f 63 61  ;..}.../* Alloca
1870: 74 65 20 63 61 63 68 65 20 2a 2f 0a 09 66 69 6c  te cache */..fil
1880: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61  ed_fileinfo_fdca
1890: 63 68 65 5f 73 69 7a 65 20 3d 20 63 61 63 68 65  che_size = cache
18a0: 5f 73 69 7a 65 3b 0a 09 66 69 6c 65 64 5f 66 69  _size;..filed_fi
18b0: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d  leinfo_fdcache =
18c0: 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a   malloc(sizeof(*
18d0: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66  filed_fileinfo_f
18e0: 64 63 61 63 68 65 29 20 2a 20 66 69 6c 65 64 5f  dcache) * filed_
18f0: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65  fileinfo_fdcache
1900: 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 66 69 6c  _size);..if (fil
1910: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61  ed_fileinfo_fdca
1920: 63 68 65 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09  che == NULL) {..
1930: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a  .return(1);..}..
1940: 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 63  ./* Initialize c
1950: 61 63 68 65 20 65 6e 74 72 69 65 73 20 2a 2f 0a  ache entries */.
1960: 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 69  .for (idx = 0; i
1970: 64 78 20 3c 20 66 69 6c 65 64 5f 66 69 6c 65 69  dx < filed_filei
1980: 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65  nfo_fdcache_size
1990: 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09 6d 75 74  ; idx++) {...mut
19a0: 65 78 5f 69 6e 69 74 5f 72 65 74 20 3d 20 70 74  ex_init_ret = pt
19b0: 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74  hread_mutex_init
19c0: 28 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  (&filed_fileinfo
19d0: 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 6d 75  _fdcache[idx].mu
19e0: 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a 09 09 69 66  tex, NULL);...if
19f0: 20 28 6d 75 74 65 78 5f 69 6e 69 74 5f 72 65 74   (mutex_init_ret
1a00: 20 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 74 75   != 0) {....retu
1a10: 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 66 69  rn(1);...}....fi
1a20: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
1a30: 61 63 68 65 5b 69 64 78 5d 2e 70 61 74 68 5b 30  ache[idx].path[0
1a40: 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 66 69 6c 65  ] = '\0';...file
1a50: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63  d_fileinfo_fdcac
1a60: 68 65 5b 69 64 78 5d 2e 66 64 20 3d 20 2d 31 3b  he[idx].fd = -1;
1a70: 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  ...filed_fileinf
1a80: 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 6c  o_fdcache[idx].l
1a90: 61 73 74 6d 6f 64 20 3d 20 22 22 3b 0a 09 09 66  astmod = "";...f
1aa0: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64  iled_fileinfo_fd
1ab0: 63 61 63 68 65 5b 69 64 78 5d 2e 74 79 70 65 20  cache[idx].type 
1ac0: 3d 20 22 22 3b 0a 09 7d 0a 0a 09 72 65 74 75 72  = "";..}...retur
1ad0: 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69 74  n(0);.}../* Init
1ae0: 69 61 6c 69 7a 65 20 70 72 6f 63 65 73 73 20 2a  ialize process *
1af0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c  /.static int fil
1b00: 65 64 5f 69 6e 69 74 28 75 6e 73 69 67 6e 65 64  ed_init(unsigned
1b10: 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 29   int cache_size)
1b20: 20 7b 0a 09 73 74 61 74 69 63 20 69 6e 74 20 63   {..static int c
1b30: 61 6c 6c 65 64 20 3d 20 30 3b 0a 09 73 74 72 75  alled = 0;..stru
1b40: 63 74 20 73 69 67 61 63 74 69 6f 6e 20 73 69 67  ct sigaction sig
1b50: 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 6f  nal_handler_info
1b60: 3b 0a 09 73 69 67 73 65 74 5f 74 20 73 69 67 6e  ;..sigset_t sign
1b70: 61 6c 5f 68 61 6e 64 6c 65 72 5f 6d 61 73 6b 3b  al_handler_mask;
1b80: 0a 09 73 73 69 7a 65 5f 74 20 72 65 61 64 5f 72  ..ssize_t read_r
1b90: 65 74 20 3d 20 30 3b 0a 09 75 6e 73 69 67 6e 65  et = 0;..unsigne
1ba0: 64 20 69 6e 74 20 72 61 6e 64 6f 6d 5f 76 61 6c  d int random_val
1bb0: 75 65 20 3d 20 30 3b 0a 09 69 6e 74 20 63 61 63  ue = 0;..int cac
1bc0: 68 65 5f 72 65 74 3b 0a 09 69 6e 74 20 72 61 6e  he_ret;..int ran
1bd0: 64 6f 6d 5f 66 64 3b 0a 0a 09 69 66 20 28 63 61  dom_fd;...if (ca
1be0: 6c 6c 65 64 29 20 7b 0a 09 09 72 65 74 75 72 6e  lled) {...return
1bf0: 28 30 29 3b 0a 09 7d 0a 0a 09 63 61 6c 6c 65 64  (0);..}...called
1c00: 20 3d 20 31 3b 0a 0a 09 2f 2a 20 41 74 74 65 6d   = 1;.../* Attem
1c10: 70 74 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 6d  pt to lock all m
1c20: 65 6d 6f 72 79 20 74 6f 20 70 68 79 73 69 63 61  emory to physica
1c30: 6c 20 52 41 4d 20 28 62 75 74 20 64 6f 6e 27 74  l RAM (but don't
1c40: 20 63 61 72 65 20 69 66 20 77 65 20 63 61 6e 27   care if we can'
1c50: 74 29 20 2a 2f 0a 09 6d 6c 6f 63 6b 61 6c 6c 28  t) */..mlockall(
1c60: 4d 43 4c 5f 43 55 52 52 45 4e 54 20 7c 20 4d 43  MCL_CURRENT | MC
1c70: 4c 5f 46 55 54 55 52 45 29 3b 0a 0a 09 2f 2a 20  L_FUTURE);.../* 
1c80: 45 73 74 61 62 6c 69 73 68 20 73 69 67 6e 61 6c  Establish signal
1c90: 20 68 61 6e 64 6c 65 72 73 20 2a 2f 0a 09 2f 2a   handlers */../*
1ca0: 20 53 49 47 50 49 50 45 20 73 68 6f 75 6c 64 20   SIGPIPE should 
1cb0: 69 6e 74 65 72 72 75 70 74 20 73 79 73 74 65 6d  interrupt system
1cc0: 20 63 61 6c 6c 73 20 2a 2f 0a 09 73 69 67 66 69   calls */..sigfi
1cd0: 6c 6c 73 65 74 28 26 73 69 67 6e 61 6c 5f 68 61  llset(&signal_ha
1ce0: 6e 64 6c 65 72 5f 6d 61 73 6b 29 3b 0a 09 73 69  ndler_mask);..si
1cf0: 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e 66  gnal_handler_inf
1d00: 6f 2e 73 61 5f 68 61 6e 64 6c 65 72 20 3d 20 66  o.sa_handler = f
1d10: 69 6c 65 64 5f 73 69 67 6e 61 6c 5f 68 61 6e 64  iled_signal_hand
1d20: 6c 65 72 3b 0a 09 73 69 67 6e 61 6c 5f 68 61 6e  ler;..signal_han
1d30: 64 6c 65 72 5f 69 6e 66 6f 2e 73 61 5f 6d 61 73  dler_info.sa_mas
1d40: 6b 20 3d 20 73 69 67 6e 61 6c 5f 68 61 6e 64 6c  k = signal_handl
1d50: 65 72 5f 6d 61 73 6b 3b 0a 09 73 69 67 6e 61 6c  er_mask;..signal
1d60: 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 6f 2e 73 61  _handler_info.sa
1d70: 5f 66 6c 61 67 73 20 3d 20 53 41 5f 52 45 53 54  _flags = SA_REST
1d80: 41 52 54 3b 0a 09 73 69 67 61 63 74 69 6f 6e 28  ART;..sigaction(
1d90: 53 49 47 50 49 50 45 2c 20 26 73 69 67 6e 61 6c  SIGPIPE, &signal
1da0: 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 6f 2c 20 4e  _handler_info, N
1db0: 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 48 61 6e 64 6c  ULL);.../* Handl
1dc0: 65 20 53 49 47 48 55 50 20 74 6f 20 72 65 6c 65  e SIGHUP to rele
1dd0: 61 73 65 20 61 6c 6c 20 63 61 63 68 65 73 20 2a  ase all caches *
1de0: 2f 0a 09 73 69 67 66 69 6c 6c 73 65 74 28 26 73  /..sigfillset(&s
1df0: 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 6d 61  ignal_handler_ma
1e00: 73 6b 29 3b 0a 09 73 69 67 6e 61 6c 5f 68 61 6e  sk);..signal_han
1e10: 64 6c 65 72 5f 69 6e 66 6f 2e 73 61 5f 68 61 6e  dler_info.sa_han
1e20: 64 6c 65 72 20 3d 20 66 69 6c 65 64 5f 73 69 67  dler = filed_sig
1e30: 6e 61 6c 5f 68 61 6e 64 6c 65 72 3b 0a 09 73 69  nal_handler;..si
1e40: 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e 66  gnal_handler_inf
1e50: 6f 2e 73 61 5f 6d 61 73 6b 20 3d 20 73 69 67 6e  o.sa_mask = sign
1e60: 61 6c 5f 68 61 6e 64 6c 65 72 5f 6d 61 73 6b 3b  al_handler_mask;
1e70: 0a 09 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72  ..signal_handler
1e80: 5f 69 6e 66 6f 2e 73 61 5f 66 6c 61 67 73 20 3d  _info.sa_flags =
1e90: 20 30 3b 0a 09 73 69 67 61 63 74 69 6f 6e 28 53   0;..sigaction(S
1ea0: 49 47 48 55 50 2c 20 26 73 69 67 6e 61 6c 5f 68  IGHUP, &signal_h
1eb0: 61 6e 64 6c 65 72 5f 69 6e 66 6f 2c 20 4e 55 4c  andler_info, NUL
1ec0: 4c 29 3b 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c  L);.../* Initial
1ed0: 69 7a 65 20 63 61 63 68 65 20 73 74 72 75 63 74  ize cache struct
1ee0: 75 72 65 20 2a 2f 0a 09 63 61 63 68 65 5f 72 65  ure */..cache_re
1ef0: 74 20 3d 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63  t = filed_init_c
1f00: 61 63 68 65 28 63 61 63 68 65 5f 73 69 7a 65 29  ache(cache_size)
1f10: 3b 0a 09 69 66 20 28 63 61 63 68 65 5f 72 65 74  ;..if (cache_ret
1f20: 20 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72   != 0) {...retur
1f30: 6e 28 63 61 63 68 65 5f 72 65 74 29 3b 0a 09 7d  n(cache_ret);..}
1f40: 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .../* Initialize
1f50: 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67   random number g
1f60: 65 6e 65 72 61 74 6f 72 20 2a 2f 0a 09 72 61 6e  enerator */..ran
1f70: 64 6f 6d 5f 66 64 20 3d 20 6f 70 65 6e 28 22 2f  dom_fd = open("/
1f80: 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f  dev/urandom", O_
1f90: 52 44 4f 4e 4c 59 29 3b 0a 09 69 66 20 28 72 61  RDONLY);..if (ra
1fa0: 6e 64 6f 6d 5f 66 64 20 3e 3d 20 30 29 20 7b 0a  ndom_fd >= 0) {.
1fb0: 09 09 72 65 61 64 5f 72 65 74 20 3d 20 72 65 61  ..read_ret = rea
1fc0: 64 28 72 61 6e 64 6f 6d 5f 66 64 2c 20 26 72 61  d(random_fd, &ra
1fd0: 6e 64 6f 6d 5f 76 61 6c 75 65 2c 20 73 69 7a 65  ndom_value, size
1fe0: 6f 66 28 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 29  of(random_value)
1ff0: 29 3b 0a 0a 09 09 63 6c 6f 73 65 28 72 61 6e 64  );....close(rand
2000: 6f 6d 5f 66 64 29 3b 0a 09 7d 0a 0a 09 72 61 6e  om_fd);..}...ran
2010: 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 67 65 74  dom_value ^= get
2020: 70 69 64 28 29 3b 0a 09 72 61 6e 64 6f 6d 5f 76  pid();..random_v
2030: 61 6c 75 65 20 5e 3d 20 67 65 74 75 69 64 28 29  alue ^= getuid()
2040: 3b 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20  ;..random_value 
2050: 5e 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a  ^= time(NULL);..
2060: 09 73 72 61 6e 64 6f 6d 28 72 61 6e 64 6f 6d 5f  .srandom(random_
2070: 76 61 6c 75 65 29 3b 0a 0a 09 72 65 74 75 72 6e  value);...return
2080: 28 30 29 3b 0a 0a 09 2f 2a 20 4e 4f 54 52 45 41  (0);.../* NOTREA
2090: 43 48 3a 20 52 65 61 64 20 6d 61 79 20 66 61 69  CH: Read may fai
20a0: 6c 20 6f 72 20 73 75 63 63 65 65 64 2c 20 77 65  l or succeed, we
20b0: 20 64 6f 6e 27 74 20 61 63 74 75 61 6c 6c 79 20   don't actually 
20c0: 63 61 72 65 20 2a 2f 0a 09 72 65 61 64 5f 72 65  care */..read_re
20d0: 74 20 3d 20 72 65 61 64 5f 72 65 74 3b 0a 7d 0a  t = read_ret;.}.
20e0: 0a 2f 2a 20 4c 69 73 74 65 6e 20 6f 6e 20 61 20  ./* Listen on a 
20f0: 70 61 72 74 69 63 75 6c 61 72 20 61 64 64 72 65  particular addre
2100: 73 73 2f 70 6f 72 74 20 2a 2f 0a 73 74 61 74 69  ss/port */.stati
2110: 63 20 69 6e 74 20 66 69 6c 65 64 5f 6c 69 73 74  c int filed_list
2120: 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 61  en(const char *a
2130: 64 64 72 65 73 73 2c 20 75 6e 73 69 67 6e 65 64  ddress, unsigned
2140: 20 69 6e 74 20 70 6f 72 74 29 20 7b 0a 09 73 74   int port) {..st
2150: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e  ruct sockaddr_in
2160: 36 20 61 64 64 72 5f 76 36 3b 0a 09 73 74 72 75  6 addr_v6;..stru
2170: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 61  ct sockaddr_in a
2180: 64 64 72 5f 76 34 3b 0a 09 73 74 72 75 63 74 20  ddr_v4;..struct 
2190: 73 6f 63 6b 61 64 64 72 20 2a 61 64 64 72 3b 0a  sockaddr *addr;.
21a0: 09 73 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 72 5f  .socklen_t addr_
21b0: 6c 65 6e 3b 0a 09 69 6e 74 20 70 74 6f 6e 5f 72  len;..int pton_r
21c0: 65 74 2c 20 62 69 6e 64 5f 72 65 74 2c 20 6c 69  et, bind_ret, li
21d0: 73 74 65 6e 5f 72 65 74 3b 0a 09 69 6e 74 20 66  sten_ret;..int f
21e0: 61 6d 69 6c 79 3b 0a 09 69 6e 74 20 66 64 3b 0a  amily;..int fd;.
21f0: 0a 09 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e  ..family = AF_IN
2200: 45 54 36 3b 0a 09 70 74 6f 6e 5f 72 65 74 20 3d  ET6;..pton_ret =
2210: 20 69 6e 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c   inet_pton(famil
2220: 79 2c 20 61 64 64 72 65 73 73 2c 20 26 61 64 64  y, address, &add
2230: 72 5f 76 36 2e 73 69 6e 36 5f 61 64 64 72 2e 73  r_v6.sin6_addr.s
2240: 36 5f 61 64 64 72 29 3b 0a 09 69 66 20 28 70 74  6_addr);..if (pt
2250: 6f 6e 5f 72 65 74 20 21 3d 20 31 29 20 7b 0a 09  on_ret != 1) {..
2260: 09 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45  .family = AF_INE
2270: 54 3b 0a 09 09 70 74 6f 6e 5f 72 65 74 20 3d 20  T;...pton_ret = 
2280: 69 6e 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79  inet_pton(family
2290: 2c 20 61 64 64 72 65 73 73 2c 20 26 61 64 64 72  , address, &addr
22a0: 5f 76 34 2e 73 69 6e 5f 61 64 64 72 2e 73 5f 61  _v4.sin_addr.s_a
22b0: 64 64 72 29 3b 0a 09 09 69 66 20 28 70 74 6f 6e  ddr);...if (pton
22c0: 5f 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 09  _ret != 1) {....
22d0: 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a  return(-1);...}.
22e0: 0a 09 09 61 64 64 72 5f 76 34 2e 73 69 6e 5f 66  ...addr_v4.sin_f
22f0: 61 6d 69 6c 79 20 3d 20 66 61 6d 69 6c 79 3b 0a  amily = family;.
2300: 09 09 61 64 64 72 5f 76 34 2e 73 69 6e 5f 70 6f  ..addr_v4.sin_po
2310: 72 74 20 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29  rt = htons(port)
2320: 3b 0a 0a 09 09 61 64 64 72 20 3d 20 28 73 74 72  ;....addr = (str
2330: 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20  uct sockaddr *) 
2340: 26 61 64 64 72 5f 76 34 3b 0a 09 09 61 64 64 72  &addr_v4;...addr
2350: 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64  _len = sizeof(ad
2360: 64 72 5f 76 34 29 3b 0a 09 7d 20 65 6c 73 65 20  dr_v4);..} else 
2370: 7b 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36  {...addr_v6.sin6
2380: 5f 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45  _family = AF_INE
2390: 54 36 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 69  T6;...addr_v6.si
23a0: 6e 36 5f 66 6c 6f 77 69 6e 66 6f 20 3d 20 30 3b  n6_flowinfo = 0;
23b0: 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f  ...addr_v6.sin6_
23c0: 73 63 6f 70 65 5f 69 64 20 3d 20 30 3b 0a 09 09  scope_id = 0;...
23d0: 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 70 6f 72  addr_v6.sin6_por
23e0: 74 20 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b  t = htons(port);
23f0: 0a 0a 09 09 61 64 64 72 20 3d 20 28 73 74 72 75  ....addr = (stru
2400: 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26  ct sockaddr *) &
2410: 61 64 64 72 5f 76 36 3b 0a 09 09 61 64 64 72 5f  addr_v6;...addr_
2420: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64  len = sizeof(add
2430: 72 5f 76 36 29 3b 0a 09 7d 0a 0a 09 66 64 20 3d  r_v6);..}...fd =
2440: 20 73 6f 63 6b 65 74 28 66 61 6d 69 6c 79 2c 20   socket(family, 
2450: 53 4f 43 4b 5f 53 54 52 45 41 4d 2c 20 30 29 3b  SOCK_STREAM, 0);
2460: 0a 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a  ..if (fd < 0) {.
2470: 09 09 72 65 74 75 72 6e 28 66 64 29 3b 0a 09 7d  ..return(fd);..}
2480: 0a 0a 09 62 69 6e 64 5f 72 65 74 20 3d 20 62 69  ...bind_ret = bi
2490: 6e 64 28 66 64 2c 20 61 64 64 72 2c 20 61 64 64  nd(fd, addr, add
24a0: 72 5f 6c 65 6e 29 3b 0a 09 69 66 20 28 62 69 6e  r_len);..if (bin
24b0: 64 5f 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 63  d_ret < 0) {...c
24c0: 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74  lose(fd);....ret
24d0: 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 6c 69  urn(-1);..}...li
24e0: 73 74 65 6e 5f 72 65 74 20 3d 20 6c 69 73 74 65  sten_ret = liste
24f0: 6e 28 66 64 2c 20 31 32 38 29 3b 0a 09 69 66 20  n(fd, 128);..if 
2500: 28 6c 69 73 74 65 6e 5f 72 65 74 20 21 3d 20 30  (listen_ret != 0
2510: 29 20 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b  ) {...close(fd);
2520: 0a 0a 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a  ....return(-1);.
2530: 09 7d 0a 0a 09 72 65 74 75 72 6e 28 66 64 29 3b  .}...return(fd);
2540: 0a 7d 0a 0a 2f 2a 20 4c 6f 67 20 61 20 6d 65 73  .}../* Log a mes
2550: 73 61 67 65 20 2a 2f 0a 23 69 66 64 65 66 20 46  sage */.#ifdef F
2560: 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 23 20  ILED_DONT_LOG.# 
2570: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f   define filed_lo
2580: 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69  gging_thread_ini
2590: 74 28 78 29 20 30 0a 23 20 20 64 65 66 69 6e 65  t(x) 0.#  define
25a0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64   filed_log_msg_d
25b0: 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a  ebug(x, ...) /**
25c0: 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65  /.#  define file
25d0: 64 5f 6c 6f 67 5f 6d 73 67 28 78 2c 20 2e 2e 2e  d_log_msg(x, ...
25e0: 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65  ) /**/.#  define
25f0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79   filed_log_entry
2600: 28 78 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69  (x) /**/.#  defi
2610: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28  ne filed_log_ip(
2620: 78 2c 20 2e 2e 2e 29 20 4e 55 4c 4c 0a 23 20 20  x, ...) NULL.#  
2630: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67  define filed_log
2640: 5f 6e 65 77 28 78 29 20 26 6c 6f 63 61 6c 5f 64  _new(x) &local_d
2650: 75 6d 6d 79 5f 6c 6f 67 0a 23 20 20 64 65 66 69  ummy_log.#  defi
2660: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 66 72 65  ne filed_log_fre
2670: 65 28 78 29 20 2f 2a 2a 2f 0a 0a 2f 2a 20 52 65  e(x) /**/../* Re
2680: 74 75 72 6e 20 6c 6f 67 67 69 6e 67 20 68 61 6e  turn logging han
2690: 64 6c 65 20 2a 2f 0a 73 74 61 74 69 63 20 46 49  dle */.static FI
26a0: 4c 45 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70  LE *filed_log_op
26b0: 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 66  en(const char *f
26c0: 69 6c 65 29 20 7b 0a 09 72 65 74 75 72 6e 28 73  ile) {..return(s
26d0: 74 64 6f 75 74 29 3b 0a 09 66 69 6c 65 20 3d 20  tdout);..file = 
26e0: 66 69 6c 65 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20  file;.}.#else.# 
26f0: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f   define filed_lo
2700: 67 5f 66 72 65 65 28 78 29 20 66 72 65 65 28 78  g_free(x) free(x
2710: 29 0a 23 20 20 69 66 64 65 66 20 46 49 4c 45 44  ).#  ifdef FILED
2720: 5f 44 45 42 55 47 0a 23 20 20 20 20 64 65 66 69  _DEBUG.#    defi
2730: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  ne filed_log_msg
2740: 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20 7b  _debug(x, ...) {
2750: 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
2760: 20 78 2c 20 5f 5f 56 41 5f 41 52 47 53 5f 5f 29   x, __VA_ARGS__)
2770: 3b 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72  ; fprintf(stderr
2780: 2c 20 22 5c 6e 22 29 3b 20 66 66 6c 75 73 68 28  , "\n"); fflush(
2790: 73 74 64 65 72 72 29 3b 20 7d 0a 23 20 20 65 6c  stderr); }.#  el
27a0: 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 66  se.#    define f
27b0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62  iled_log_msg_deb
27c0: 75 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a  ug(x, ...) /**/.
27d0: 23 20 20 65 6e 64 69 66 0a 0a 2f 2a 20 49 6e 69  #  endif../* Ini
27e0: 74 69 61 6c 69 7a 65 20 6c 6f 67 67 69 6e 67 20  tialize logging 
27f0: 74 68 72 65 61 64 20 2a 2f 0a 73 74 61 74 69 63  thread */.static
2800: 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f 6c 6f 67   void *filed_log
2810: 67 69 6e 67 5f 74 68 72 65 61 64 28 76 6f 69 64  ging_thread(void
2820: 20 2a 61 72 67 5f 70 29 20 7b 0a 09 73 74 72 75   *arg_p) {..stru
2830: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67  ct filed_logging
2840: 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72  _thread_args *ar
2850: 67 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64  g;..struct filed
2860: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 63 75 72 72  _log_entry *curr
2870: 2c 20 2a 70 72 65 76 3b 0a 09 63 6f 6e 73 74 20  , *prev;..const 
2880: 63 68 61 72 20 2a 6d 65 74 68 6f 64 3b 0a 09 74  char *method;..t
2890: 69 6d 65 5f 74 20 6e 6f 77 3b 0a 09 46 49 4c 45  ime_t now;..FILE
28a0: 20 2a 66 70 3b 0a 0a 09 61 72 67 20 3d 20 61 72   *fp;...arg = ar
28b0: 67 5f 70 3b 0a 0a 09 66 70 20 3d 20 61 72 67 2d  g_p;...fp = arg-
28c0: 3e 66 70 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29  >fp;...while (1)
28d0: 20 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74   {...pthread_mut
28e0: 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c  ex_lock(&filed_l
28f0: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65  og_msg_list_mute
2900: 78 29 3b 0a 09 09 70 74 68 72 65 61 64 5f 63 6f  x);...pthread_co
2910: 6e 64 5f 77 61 69 74 28 26 66 69 6c 65 64 5f 6c  nd_wait(&filed_l
2920: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64  og_msg_list_read
2930: 79 2c 20 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  y, &filed_log_ms
2940: 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a  g_list_mutex);..
2950: 09 09 63 75 72 72 20 3d 20 66 69 6c 65 64 5f 6c  ..curr = filed_l
2960: 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a 09 09 66  og_msg_list;...f
2970: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73  iled_log_msg_lis
2980: 74 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 09 70 74 68  t = NULL;....pth
2990: 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63  read_mutex_unloc
29a0: 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  k(&filed_log_msg
29b0: 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09  _list_mutex);...
29c0: 09 6e 6f 77 20 3d 20 74 69 6d 65 28 4e 55 4c 4c  .now = time(NULL
29d0: 29 3b 0a 0a 09 09 70 72 65 76 20 3d 20 4e 55 4c  );....prev = NUL
29e0: 4c 3b 0a 09 09 66 6f 72 20 28 3b 20 63 75 72 72  L;...for (; curr
29f0: 3b 20 63 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f  ; curr = curr->_
2a00: 6e 65 78 74 29 20 7b 0a 09 09 09 63 75 72 72 2d  next) {....curr-
2a10: 3e 5f 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 0a  >_prev = prev;..
2a20: 09 09 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a  ...prev = curr;.
2a30: 09 09 7d 0a 0a 09 09 63 75 72 72 20 3d 20 70 72  ..}....curr = pr
2a40: 65 76 3b 0a 09 09 77 68 69 6c 65 20 28 63 75 72  ev;...while (cur
2a50: 72 29 20 7b 0a 09 09 09 73 77 69 74 63 68 20 28  r) {....switch (
2a60: 63 75 72 72 2d 3e 74 79 70 65 29 20 7b 0a 09 09  curr->type) {...
2a70: 09 09 63 61 73 65 20 46 49 4c 45 44 5f 4c 4f 47  ..case FILED_LOG
2a80: 5f 54 59 50 45 5f 4d 45 53 53 41 47 45 3a 0a 09  _TYPE_MESSAGE:..
2a90: 09 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20  ....fprintf(fp, 
2aa0: 22 25 73 22 2c 20 63 75 72 72 2d 3e 62 75 66 66  "%s", curr->buff
2ab0: 65 72 29 3b 0a 0a 09 09 09 09 09 62 72 65 61 6b  er);.......break
2ac0: 3b 0a 09 09 09 09 63 61 73 65 20 46 49 4c 45 44  ;.....case FILED
2ad0: 5f 4c 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46  _LOG_TYPE_TRANSF
2ae0: 45 52 3a 0a 09 09 09 09 09 73 77 69 74 63 68 20  ER:......switch 
2af0: 28 63 75 72 72 2d 3e 6d 65 74 68 6f 64 29 20 7b  (curr->method) {
2b00: 0a 09 09 09 09 09 09 63 61 73 65 20 46 49 4c 45  .......case FILE
2b10: 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44  D_REQUEST_METHOD
2b20: 5f 47 45 54 3a 0a 09 09 09 09 09 09 09 6d 65 74  _GET:........met
2b30: 68 6f 64 3d 22 47 45 54 22 3b 0a 09 09 09 09 09  hod="GET";......
2b40: 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 63  ..break;.......c
2b50: 61 73 65 20 46 49 4c 45 44 5f 52 45 51 55 45 53  ase FILED_REQUES
2b60: 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 3a 0a 09  T_METHOD_HEAD:..
2b70: 09 09 09 09 09 09 6d 65 74 68 6f 64 3d 22 48 45  ......method="HE
2b80: 41 44 22 3b 0a 09 09 09 09 09 09 09 62 72 65 61  AD";........brea
2b90: 6b 3b 0a 09 09 09 09 09 09 64 65 66 61 75 6c 74  k;.......default
2ba0: 3a 0a 09 09 09 09 09 09 09 6d 65 74 68 6f 64 3d  :........method=
2bb0: 22 3c 75 6e 6b 6e 6f 77 6e 3e 22 3b 0a 09 09 09  "<unknown>";....
2bc0: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09  ....break;......
2bd0: 7d 0a 0a 09 09 09 09 09 69 66 20 28 63 75 72 72  }.......if (curr
2be0: 2d 3e 65 6e 64 74 69 6d 65 20 3d 3d 20 28 28 74  ->endtime == ((t
2bf0: 69 6d 65 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09  ime_t) -1)) {...
2c00: 09 09 09 09 63 75 72 72 2d 3e 65 6e 64 74 69 6d  ....curr->endtim
2c10: 65 20 3d 20 6e 6f 77 3b 0a 09 09 09 09 09 7d 0a  e = now;......}.
2c20: 0a 09 09 09 09 09 66 70 72 69 6e 74 66 28 66 70  ......fprintf(fp
2c30: 2c 20 22 54 52 41 4e 53 46 45 52 20 4d 45 54 48  , "TRANSFER METH
2c40: 4f 44 3d 25 73 20 50 41 54 48 3d 25 73 20 53 52  OD=%s PATH=%s SR
2c50: 43 3d 25 73 3a 25 69 20 43 4c 49 45 4e 54 2e 54  C=%s:%i CLIENT.T
2c60: 49 4d 45 2e 43 4f 4e 4e 45 43 54 3d 25 6c 6c 75  IME.CONNECT=%llu
2c70: 20 52 45 51 55 45 53 54 2e 54 49 4d 45 2e 53 54   REQUEST.TIME.ST
2c80: 41 52 54 3d 25 6c 6c 75 20 52 45 51 55 45 53 54  ART=%llu REQUEST
2c90: 2e 54 49 4d 45 2e 45 4e 44 3d 25 6c 6c 75 20 43  .TIME.END=%llu C
2ca0: 4f 44 45 2e 56 41 4c 55 45 3d 25 75 20 43 4f 44  ODE.VALUE=%u COD
2cb0: 45 2e 52 45 41 53 4f 4e 3d 25 73 20 52 45 51 55  E.REASON=%s REQU
2cc0: 45 53 54 2e 4f 46 46 53 45 54 3d 25 6c 6c 75 20  EST.OFFSET=%llu 
2cd0: 52 45 51 55 45 53 54 2e 4c 45 4e 47 54 48 3d 25  REQUEST.LENGTH=%
2ce0: 6c 6c 75 20 46 49 4c 45 2e 4c 45 4e 47 54 48 3d  llu FILE.LENGTH=
2cf0: 25 6c 6c 75 20 54 52 41 4e 53 46 45 52 2e 4c 45  %llu TRANSFER.LE
2d00: 4e 47 54 48 3d 25 6c 6c 75 22 2c 0a 09 09 09 09  NGTH=%llu",.....
2d10: 09 09 6d 65 74 68 6f 64 2c 0a 09 09 09 09 09 09  ..method,.......
2d20: 63 75 72 72 2d 3e 62 75 66 66 65 72 2c 0a 09 09  curr->buffer,...
2d30: 09 09 09 09 63 75 72 72 2d 3e 69 70 2c 20 63 75  ....curr->ip, cu
2d40: 72 72 2d 3e 70 6f 72 74 2c 0a 09 09 09 09 09 09  rr->port,.......
2d50: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
2d60: 6f 6e 67 29 20 63 75 72 72 2d 3e 63 6f 6e 6e 65  ong) curr->conne
2d70: 63 74 74 69 6d 65 2c 0a 09 09 09 09 09 09 28 75  cttime,.......(u
2d80: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
2d90: 67 29 20 63 75 72 72 2d 3e 73 74 61 72 74 74 69  g) curr->startti
2da0: 6d 65 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67  me,.......(unsig
2db0: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63  ned long long) c
2dc0: 75 72 72 2d 3e 65 6e 64 74 69 6d 65 2c 0a 09 09  urr->endtime,...
2dd0: 09 09 09 09 63 75 72 72 2d 3e 68 74 74 70 5f 63  ....curr->http_c
2de0: 6f 64 65 2c 20 63 75 72 72 2d 3e 72 65 61 73 6f  ode, curr->reaso
2df0: 6e 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e  n,.......(unsign
2e00: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75  ed long long) cu
2e10: 72 72 2d 3e 72 65 71 5f 6f 66 66 73 65 74 2c 0a  rr->req_offset,.
2e20: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ......(unsigned 
2e30: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d  long long) curr-
2e40: 3e 72 65 71 5f 6c 65 6e 67 74 68 2c 0a 09 09 09  >req_length,....
2e50: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
2e60: 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 66 69  g long) curr->fi
2e70: 6c 65 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09  le_length,......
2e80: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
2e90: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 73 65 6e 74  long) curr->sent
2ea0: 5f 6c 65 6e 67 74 68 0a 09 09 09 09 09 29 3b 0a  _length......);.
2eb0: 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09  ......break;....
2ec0: 7d 0a 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c  }....fprintf(fp,
2ed0: 20 22 20 54 48 52 45 41 44 3d 25 6c 6c 75 20 54   " THREAD=%llu T
2ee0: 49 4d 45 3d 25 6c 6c 75 5c 6e 22 2c 0a 09 09 09  IME=%llu\n",....
2ef0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
2f00: 6c 6f 6e 67 29 20 28 28 69 6e 74 70 74 72 5f 74  long) ((intptr_t
2f10: 29 20 63 75 72 72 2d 3e 74 68 72 65 61 64 29 2c  ) curr->thread),
2f20: 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c  .....(unsigned l
2f30: 6f 6e 67 20 6c 6f 6e 67 29 20 6e 6f 77 0a 09 09  ong long) now...
2f40: 09 29 3b 0a 09 09 09 66 66 6c 75 73 68 28 66 70  .);....fflush(fp
2f50: 29 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20 63 75  );.....prev = cu
2f60: 72 72 3b 0a 09 09 09 63 75 72 72 20 3d 20 63 75  rr;....curr = cu
2f70: 72 72 2d 3e 5f 70 72 65 76 3b 0a 0a 09 09 09 66  rr->_prev;.....f
2f80: 72 65 65 28 70 72 65 76 29 3b 0a 09 09 7d 0a 09  ree(prev);...}..
2f90: 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29  }...return(NULL)
2fa0: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
2fb0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79   filed_log_entry
2fc0: 28 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f  (struct filed_lo
2fd0: 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 29 20  g_entry *entry) 
2fe0: 7b 0a 09 65 6e 74 72 79 2d 3e 74 68 72 65 61 64  {..entry->thread
2ff0: 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28   = pthread_self(
3000: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74  );...pthread_mut
3010: 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c  ex_lock(&filed_l
3020: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65  og_msg_list_mute
3030: 78 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 5f 6e 65  x);...entry->_ne
3040: 78 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d  xt = filed_log_m
3050: 73 67 5f 6c 69 73 74 3b 0a 09 66 69 6c 65 64 5f  sg_list;..filed_
3060: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 65  log_msg_list = e
3070: 6e 74 72 79 3b 0a 0a 09 70 74 68 72 65 61 64 5f  ntry;...pthread_
3080: 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69  mutex_unlock(&fi
3090: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74  led_log_msg_list
30a0: 5f 6d 75 74 65 78 29 3b 0a 0a 09 70 74 68 72 65  _mutex);...pthre
30b0: 61 64 5f 63 6f 6e 64 5f 73 69 67 6e 61 6c 28 26  ad_cond_signal(&
30c0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69  filed_log_msg_li
30d0: 73 74 5f 72 65 61 64 79 29 3b 0a 0a 09 72 65 74  st_ready);...ret
30e0: 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73  urn;.}..static s
30f0: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f  truct filed_log_
3100: 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c 6f 67  entry *filed_log
3110: 5f 6e 65 77 28 69 6e 74 20 69 6e 69 74 69 61 6c  _new(int initial
3120: 69 7a 65 29 20 7b 0a 09 73 74 72 75 63 74 20 66  ize) {..struct f
3130: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a  iled_log_entry *
3140: 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c  retval;...retval
3150: 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66   = malloc(sizeof
3160: 28 2a 72 65 74 76 61 6c 29 29 3b 0a 0a 09 69 66  (*retval));...if
3170: 20 28 69 6e 69 74 69 61 6c 69 7a 65 29 20 7b 0a   (initialize) {.
3180: 09 09 72 65 74 76 61 6c 2d 3e 62 75 66 66 65 72  ..retval->buffer
3190: 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 72 65  [0] = '\0';...re
31a0: 74 76 61 6c 2d 3e 68 74 74 70 5f 63 6f 64 65 20  tval->http_code 
31b0: 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c 2d 3e  = -1;...retval->
31c0: 63 6f 6e 6e 65 63 74 74 69 6d 65 20 3d 20 30 3b  connecttime = 0;
31d0: 0a 09 09 72 65 74 76 61 6c 2d 3e 73 74 61 72 74  ...retval->start
31e0: 74 69 6d 65 20 3d 20 30 3b 0a 09 09 72 65 74 76  time = 0;...retv
31f0: 61 6c 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 30 3b  al->endtime = 0;
3200: 0a 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f 6f  ...retval->req_o
3210: 66 66 73 65 74 20 3d 20 30 3b 0a 09 09 72 65 74  ffset = 0;...ret
3220: 76 61 6c 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20  val->req_length 
3230: 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 73  = 0;...retval->s
3240: 65 6e 74 5f 6c 65 6e 67 74 68 20 3d 20 30 3b 0a  ent_length = 0;.
3250: 09 09 72 65 74 76 61 6c 2d 3e 66 69 6c 65 5f 6c  ..retval->file_l
3260: 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 74  ength = 0;...ret
3270: 76 61 6c 2d 3e 69 70 5b 30 5d 20 3d 20 27 5c 30  val->ip[0] = '\0
3280: 27 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 70 6f 72  ';...retval->por
3290: 74 20 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c  t = -1;...retval
32a0: 2d 3e 6d 65 74 68 6f 64 20 3d 20 2d 31 3b 0a 09  ->method = -1;..
32b0: 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61  }...return(retva
32c0: 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  l);.}..static vo
32d0: 69 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  id filed_log_msg
32e0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d 74  (const char *fmt
32f0: 2c 20 2e 2e 2e 29 20 7b 0a 09 73 74 72 75 63 74  , ...) {..struct
3300: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79   filed_log_entry
3310: 20 2a 65 6e 74 72 79 3b 0a 09 76 61 5f 6c 69 73   *entry;..va_lis
3320: 74 20 61 72 67 73 3b 0a 0a 09 65 6e 74 72 79 20  t args;...entry 
3330: 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28  = filed_log_new(
3340: 30 29 3b 0a 0a 09 76 61 5f 73 74 61 72 74 28 61  0);...va_start(a
3350: 72 67 73 2c 20 66 6d 74 29 3b 0a 0a 09 76 73 6e  rgs, fmt);...vsn
3360: 70 72 69 6e 74 66 28 65 6e 74 72 79 2d 3e 62 75  printf(entry->bu
3370: 66 66 65 72 2c 20 73 69 7a 65 6f 66 28 65 6e 74  ffer, sizeof(ent
3380: 72 79 2d 3e 62 75 66 66 65 72 29 2c 20 66 6d 74  ry->buffer), fmt
3390: 2c 20 61 72 67 73 29 3b 0a 0a 09 76 61 5f 65 6e  , args);...va_en
33a0: 64 28 61 72 67 73 29 3b 0a 0a 09 65 6e 74 72 79  d(args);...entry
33b0: 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 4c  ->type = FILED_L
33c0: 4f 47 5f 54 59 50 45 5f 4d 45 53 53 41 47 45 3b  OG_TYPE_MESSAGE;
33d0: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  ...filed_log_ent
33e0: 72 79 28 65 6e 74 72 79 29 3b 0a 0a 09 72 65 74  ry(entry);...ret
33f0: 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63  urn;.}..static c
3400: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64  onst char *filed
3410: 5f 6c 6f 67 5f 69 70 28 73 74 72 75 63 74 20 73  _log_ip(struct s
3420: 6f 63 6b 61 64 64 72 20 2a 61 64 64 72 2c 20 63  ockaddr *addr, c
3430: 68 61 72 20 2a 62 75 66 66 65 72 2c 20 73 69 7a  har *buffer, siz
3440: 65 5f 74 20 62 75 66 66 65 72 6c 65 6e 29 20 7b  e_t bufferlen) {
3450: 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64  ..struct sockadd
3460: 72 5f 69 6e 20 2a 61 64 64 72 5f 76 34 3b 0a 09  r_in *addr_v4;..
3470: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f  struct sockaddr_
3480: 69 6e 36 20 2a 61 64 64 72 5f 76 36 3b 0a 09 63  in6 *addr_v6;..c
3490: 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 74 76 61  onst char *retva
34a0: 6c 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 61 64 64 72  l = NULL;...addr
34b0: 5f 76 36 20 3d 20 28 73 74 72 75 63 74 20 73 6f  _v6 = (struct so
34c0: 63 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20 61 64  ckaddr_in6 *) ad
34d0: 64 72 3b 0a 0a 09 73 77 69 74 63 68 20 28 61 64  dr;...switch (ad
34e0: 64 72 5f 76 36 2d 3e 73 69 6e 36 5f 66 61 6d 69  dr_v6->sin6_fami
34f0: 6c 79 29 20 7b 0a 09 09 63 61 73 65 20 41 46 5f  ly) {...case AF_
3500: 49 4e 45 54 3a 0a 09 09 09 61 64 64 72 5f 76 34  INET:....addr_v4
3510: 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61   = (struct socka
3520: 64 64 72 5f 69 6e 20 2a 29 20 61 64 64 72 3b 0a  ddr_in *) addr;.
3530: 09 09 09 72 65 74 76 61 6c 20 3d 20 69 6e 65 74  ...retval = inet
3540: 5f 6e 74 6f 70 28 41 46 5f 49 4e 45 54 2c 20 26  _ntop(AF_INET, &
3550: 61 64 64 72 5f 76 34 2d 3e 73 69 6e 5f 61 64 64  addr_v4->sin_add
3560: 72 2c 20 62 75 66 66 65 72 2c 20 62 75 66 66 65  r, buffer, buffe
3570: 72 6c 65 6e 29 3b 0a 09 09 09 62 72 65 61 6b 3b  rlen);....break;
3580: 0a 09 09 63 61 73 65 20 41 46 5f 49 4e 45 54 36  ...case AF_INET6
3590: 3a 0a 09 09 09 72 65 74 76 61 6c 20 3d 20 69 6e  :....retval = in
35a0: 65 74 5f 6e 74 6f 70 28 41 46 5f 49 4e 45 54 36  et_ntop(AF_INET6
35b0: 2c 20 26 61 64 64 72 5f 76 36 2d 3e 73 69 6e 36  , &addr_v6->sin6
35c0: 5f 61 64 64 72 2c 20 62 75 66 66 65 72 2c 20 62  _addr, buffer, b
35d0: 75 66 66 65 72 6c 65 6e 29 3b 0a 09 09 09 62 72  ufferlen);....br
35e0: 65 61 6b 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e  eak;..}...return
35f0: 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61  (retval);.}..sta
3600: 74 69 63 20 46 49 4c 45 20 2a 66 69 6c 65 64 5f  tic FILE *filed_
3610: 6c 6f 67 5f 6f 70 65 6e 28 63 6f 6e 73 74 20 63  log_open(const c
3620: 68 61 72 20 2a 66 69 6c 65 29 20 7b 0a 09 46 49  har *file) {..FI
3630: 4c 45 20 2a 72 65 74 76 61 6c 3b 0a 0a 09 69 66  LE *retval;...if
3640: 20 28 73 74 72 63 6d 70 28 66 69 6c 65 2c 20 22   (strcmp(file, "
3650: 2d 22 29 20 3d 3d 20 30 29 20 7b 0a 09 09 72 65  -") == 0) {...re
3660: 74 76 61 6c 20 3d 20 73 74 64 6f 75 74 3b 0a 09  tval = stdout;..
3670: 7d 20 65 6c 73 65 20 69 66 20 28 66 69 6c 65 5b  } else if (file[
3680: 30 5d 20 3d 3d 20 27 7c 27 29 20 7b 0a 09 09 66  0] == '|') {...f
3690: 69 6c 65 2b 2b 3b 0a 09 09 72 65 74 76 61 6c 20  ile++;...retval 
36a0: 3d 20 70 6f 70 65 6e 28 66 69 6c 65 2c 20 22 77  = popen(file, "w
36b0: 22 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09  ");..} else {...
36c0: 72 65 74 76 61 6c 20 3d 20 66 6f 70 65 6e 28 66  retval = fopen(f
36d0: 69 6c 65 2c 20 22 61 2b 22 29 3b 0a 09 7d 0a 0a  ile, "a+");..}..
36e0: 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b  .return(retval);
36f0: 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .}..static int f
3700: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72  iled_logging_thr
3710: 65 61 64 5f 69 6e 69 74 28 46 49 4c 45 20 2a 6c  ead_init(FILE *l
3720: 6f 67 66 70 29 20 7b 0a 09 73 74 72 75 63 74 20  ogfp) {..struct 
3730: 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68  filed_logging_th
3740: 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 73 3b  read_args *args;
3750: 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65  ..pthread_t thre
3760: 61 64 5f 69 64 3b 0a 0a 09 61 72 67 73 20 3d 20  ad_id;...args = 
3770: 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 61  malloc(sizeof(*a
3780: 72 67 73 29 29 3b 0a 09 61 72 67 73 2d 3e 66 70  rgs));..args->fp
3790: 20 3d 20 6c 6f 67 66 70 3b 0a 0a 09 66 69 6c 65   = logfp;...file
37a0: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d  d_log_msg_list =
37b0: 20 4e 55 4c 4c 3b 0a 0a 09 70 74 68 72 65 61 64   NULL;...pthread
37c0: 5f 63 72 65 61 74 65 28 26 74 68 72 65 61 64 5f  _create(&thread_
37d0: 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f  id, NULL, filed_
37e0: 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 2c 20  logging_thread, 
37f0: 61 72 67 73 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c  args);...filed_l
3800: 6f 67 5f 6d 73 67 28 22 53 54 41 52 54 22 29 3b  og_msg("START");
3810: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a  ...return(0);.}.
3820: 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 46  #endif..#ifdef F
3830: 49 4c 45 44 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55  ILED_DONT_TIMEOU
3840: 54 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f  T.#define filed_
3850: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68  sockettimeout_th
3860: 72 65 61 64 5f 69 6e 69 74 28 29 20 30 0a 23 64  read_init() 0.#d
3870: 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b  efine filed_sock
3880: 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69 74 28 29  ettimeout_init()
3890: 20 30 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64   0.#define filed
38a0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 61  _sockettimeout_a
38b0: 63 63 65 70 74 28 78 29 20 2f 2a 2a 2f 0a 23 64  ccept(x) /**/.#d
38c0: 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b  efine filed_sock
38d0: 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73  ettimeout_proces
38e0: 73 69 6e 67 5f 73 74 61 72 74 28 78 29 20 2f 2a  sing_start(x) /*
38f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64  */.#define filed
3900: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70  _sockettimeout_p
3910: 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64 28 78 29  rocessing_end(x)
3920: 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69   /**/.#define fi
3930: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
3940: 74 5f 63 6c 6f 73 65 28 78 2c 20 79 29 20 2f 2a  t_close(x, y) /*
3950: 2a 2f 0a 23 65 6c 73 65 0a 74 69 6d 65 5f 74 20  */.#else.time_t 
3960: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3970: 6f 75 74 5f 74 69 6d 65 3b 0a 73 74 72 75 63 74  out_time;.struct
3980: 20 7b 0a 09 74 69 6d 65 5f 74 20 65 78 70 69 72   {..time_t expir
3990: 61 74 69 6f 6e 5f 74 69 6d 65 3b 0a 09 70 74 68  ation_time;..pth
39a0: 72 65 61 64 5f 74 20 74 68 72 65 61 64 5f 69 64  read_t thread_id
39b0: 3b 0a 09 65 6e 75 6d 20 7b 0a 09 09 66 69 6c 65  ;..enum {...file
39c0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
39d0: 76 61 6c 69 64 2c 0a 09 09 66 69 6c 65 64 5f 73  valid,...filed_s
39e0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 76  ockettimeout_inv
39f0: 61 6c 69 64 2c 0a 09 7d 20 76 61 6c 69 64 3b 0a  alid,..} valid;.
3a00: 7d 20 2a 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  } *filed_sockett
3a10: 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75  imeout_sockstatu
3a20: 73 3b 0a 6c 6f 6e 67 20 66 69 6c 65 64 5f 73 6f  s;.long filed_so
3a30: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b  ckettimeout_sock
3a40: 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 3b 0a 69  status_length;.i
3a50: 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  nt filed_sockett
3a60: 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66  imeout_devnull_f
3a70: 64 3b 0a 70 74 68 72 65 61 64 5f 6d 75 74 65 78  d;.pthread_mutex
3a80: 5f 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  _t filed_sockett
3a90: 69 6d 65 6f 75 74 5f 6d 75 74 65 78 20 3d 20 50  imeout_mutex = P
3aa0: 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49  THREAD_MUTEX_INI
3ab0: 54 49 41 4c 49 5a 45 52 3b 0a 0a 73 74 61 74 69  TIALIZER;..stati
3ac0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b  c int filed_sock
3ad0: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64  ettimeout_sockfd
3ae0: 5f 69 6e 5f 72 61 6e 67 65 28 69 6e 74 20 73 6f  _in_range(int so
3af0: 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 73 6f 63  ckfd) {..if (soc
3b00: 6b 66 64 20 3c 20 33 29 20 7b 0a 09 09 72 65 74  kfd < 3) {...ret
3b10: 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 69 66 20  urn(0);..}...if 
3b20: 28 73 6f 63 6b 66 64 20 3e 20 66 69 6c 65 64 5f  (sockfd > filed_
3b30: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f  sockettimeout_so
3b40: 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 29  ckstatus_length)
3b50: 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a   {...return(0);.
3b60: 09 7d 0a 0a 09 72 65 74 75 72 6e 28 31 29 3b 0a  .}...return(1);.
3b70: 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  }..static void f
3b80: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3b90: 75 74 5f 65 78 70 69 72 65 28 69 6e 74 20 73 6f  ut_expire(int so
3ba0: 63 6b 66 64 2c 20 69 6e 74 20 6c 65 6e 67 74 68  ckfd, int length
3bb0: 2c 20 69 6e 74 20 6c 6f 63 6b 68 65 6c 64 29 20  , int lockheld) 
3bc0: 7b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 2c 20 65  {..time_t now, e
3bd0: 78 70 69 72 65 3b 0a 0a 09 69 66 20 28 21 6c 6f  xpire;...if (!lo
3be0: 63 6b 68 65 6c 64 29 20 7b 0a 09 09 70 74 68 72  ckheld) {...pthr
3bf0: 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26  ead_mutex_lock(&
3c00: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3c10: 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 09 7d 0a 0a  out_mutex);..}..
3c20: 09 6e 6f 77 20 3d 20 66 69 6c 65 64 5f 73 6f 63  .now = filed_soc
3c30: 6b 65 74 74 69 6d 65 6f 75 74 5f 74 69 6d 65 3b  kettimeout_time;
3c40: 0a 0a 09 65 78 70 69 72 65 20 3d 20 6e 6f 77 20  ...expire = now 
3c50: 2b 20 6c 65 6e 67 74 68 3b 0a 0a 09 66 69 6c 65  + length;...file
3c60: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
3c70: 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f 63 6b 66  sockstatus[sockf
3c80: 64 5d 2e 65 78 70 69 72 61 74 69 6f 6e 5f 74 69  d].expiration_ti
3c90: 6d 65 20 3d 20 65 78 70 69 72 65 3b 0a 0a 09 69  me = expire;...i
3ca0: 66 20 28 21 6c 6f 63 6b 68 65 6c 64 29 20 7b 0a  f (!lockheld) {.
3cb0: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  ..pthread_mutex_
3cc0: 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 73 6f  unlock(&filed_so
3cd0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 65  ckettimeout_mute
3ce0: 78 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 3b  x);..}...return;
3cf0: 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
3d00: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3d10: 6f 75 74 5f 61 63 63 65 70 74 28 69 6e 74 20 73  out_accept(int s
3d20: 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 21 66  ockfd) {..if (!f
3d30: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3d40: 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e  ut_sockfd_in_ran
3d50: 67 65 28 73 6f 63 6b 66 64 29 29 20 7b 0a 09 09  ge(sockfd)) {...
3d60: 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 70 74 68  return;..}...pth
3d70: 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28  read_mutex_lock(
3d80: 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d  &filed_sockettim
3d90: 65 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 66  eout_mutex);...f
3da0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3db0: 75 74 5f 65 78 70 69 72 65 28 73 6f 63 6b 66 64  ut_expire(sockfd
3dc0: 2c 20 36 30 2c 20 31 29 3b 0a 0a 09 66 69 6c 65  , 60, 1);...file
3dd0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
3de0: 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f 63 6b 66  sockstatus[sockf
3df0: 64 5d 2e 74 68 72 65 61 64 5f 69 64 20 3d 20 70  d].thread_id = p
3e00: 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 0a  thread_self();..
3e10: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d  .filed_sockettim
3e20: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b  eout_sockstatus[
3e30: 73 6f 63 6b 66 64 5d 2e 76 61 6c 69 64 20 3d 20  sockfd].valid = 
3e40: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
3e50: 6f 75 74 5f 76 61 6c 69 64 3b 0a 0a 09 70 74 68  out_valid;...pth
3e60: 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63  read_mutex_unloc
3e70: 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  k(&filed_sockett
3e80: 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 0a  imeout_mutex);..
3e90: 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74  .return;.}..stat
3ea0: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f  ic void filed_so
3eb0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63  ckettimeout_proc
3ec0: 65 73 73 69 6e 67 5f 73 74 61 72 74 28 69 6e 74  essing_start(int
3ed0: 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28   sockfd) {..if (
3ee0: 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d  !filed_sockettim
3ef0: 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72  eout_sockfd_in_r
3f00: 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b 0a  ange(sockfd)) {.
3f10: 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 66  ..return;..}...f
3f20: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
3f30: 75 74 5f 65 78 70 69 72 65 28 73 6f 63 6b 66 64  ut_expire(sockfd
3f40: 2c 20 38 36 34 30 30 2c 20 30 29 3b 0a 0a 09 72  , 86400, 0);...r
3f50: 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63  eturn;.}..static
3f60: 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b   void filed_sock
3f70: 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73  ettimeout_proces
3f80: 73 69 6e 67 5f 65 6e 64 28 69 6e 74 20 73 6f 63  sing_end(int soc
3f90: 6b 66 64 29 20 7b 0a 09 69 66 20 28 21 66 69 6c  kfd) {..if (!fil
3fa0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
3fb0: 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65  _sockfd_in_range
3fc0: 28 73 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 65  (sockfd)) {...re
3fd0: 74 75 72 6e 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64  turn;..}...filed
3fe0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 65  _sockettimeout_e
3ff0: 78 70 69 72 65 28 73 6f 63 6b 66 64 2c 20 36 30  xpire(sockfd, 60
4000: 2c 20 30 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a  , 0);...return;.
4010: 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  }..static void f
4020: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
4030: 75 74 5f 63 6c 6f 73 65 28 69 6e 74 20 73 6f 63  ut_close(int soc
4040: 6b 66 64 2c 20 69 6e 74 20 6c 6f 63 6b 68 65 6c  kfd, int lockhel
4050: 64 29 20 7b 0a 09 69 66 20 28 21 66 69 6c 65 64  d) {..if (!filed
4060: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73  _sockettimeout_s
4070: 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28 73  ockfd_in_range(s
4080: 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 65 74 75  ockfd)) {...retu
4090: 72 6e 3b 0a 09 7d 0a 0a 09 69 66 20 28 21 6c 6f  rn;..}...if (!lo
40a0: 63 6b 68 65 6c 64 29 20 7b 0a 09 09 70 74 68 72  ckheld) {...pthr
40b0: 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26  ead_mutex_lock(&
40c0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
40d0: 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 09 7d 0a 0a  out_mutex);..}..
40e0: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d  .filed_sockettim
40f0: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b  eout_sockstatus[
4100: 73 6f 63 6b 66 64 5d 2e 76 61 6c 69 64 20 3d 20  sockfd].valid = 
4110: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
4120: 6f 75 74 5f 69 6e 76 61 6c 69 64 3b 0a 0a 09 69  out_invalid;...i
4130: 66 20 28 21 6c 6f 63 6b 68 65 6c 64 29 20 7b 0a  f (!lockheld) {.
4140: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  ..pthread_mutex_
4150: 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 73 6f  unlock(&filed_so
4160: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 65  ckettimeout_mute
4170: 78 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 3b  x);..}...return;
4180: 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
4190: 2a 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d  *filed_sockettim
41a0: 65 6f 75 74 5f 74 68 72 65 61 64 28 76 6f 69 64  eout_thread(void
41b0: 20 2a 61 72 67 29 20 7b 0a 09 73 74 72 75 63 74   *arg) {..struct
41c0: 20 74 69 6d 65 73 70 65 63 20 73 6c 65 65 70 5f   timespec sleep_
41d0: 74 69 6d 65 3b 0a 09 74 69 6d 65 5f 74 20 6e 6f  time;..time_t no
41e0: 77 2c 20 65 78 70 69 72 61 74 69 6f 6e 5f 74 69  w, expiration_ti
41f0: 6d 65 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 74  me;..pthread_t t
4200: 68 72 65 61 64 5f 69 64 3b 0a 09 6c 6f 6e 67 20  hread_id;..long 
4210: 69 64 78 3b 0a 09 69 6e 74 20 63 6f 75 6e 74 3b  idx;..int count;
4220: 0a 09 69 6e 74 20 76 61 6c 69 64 3b 0a 09 69 6e  ..int valid;..in
4230: 74 20 74 69 6d 65 5f 69 6e 74 65 72 76 61 6c 20  t time_interval 
4240: 3d 20 33 30 3b 0a 09 69 6e 74 20 63 68 65 63 6b  = 30;..int check
4250: 5f 70 65 72 69 6f 64 20 3d 20 39 30 3b 0a 0a 09  _period = 90;...
4260: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 66 6f  while (1) {...fo
4270: 72 20 28 63 6f 75 6e 74 20 3d 20 30 3b 20 63 6f  r (count = 0; co
4280: 75 6e 74 20 3c 20 28 63 68 65 63 6b 5f 70 65 72  unt < (check_per
4290: 69 6f 64 20 2f 20 74 69 6d 65 5f 69 6e 74 65 72  iod / time_inter
42a0: 76 61 6c 29 3b 20 63 6f 75 6e 74 2b 2b 29 20 7b  val); count++) {
42b0: 0a 09 09 09 73 6c 65 65 70 5f 74 69 6d 65 2e 74  ....sleep_time.t
42c0: 76 5f 73 65 63 20 3d 20 74 69 6d 65 5f 69 6e 74  v_sec = time_int
42d0: 65 72 76 61 6c 3b 0a 09 09 09 73 6c 65 65 70 5f  erval;....sleep_
42e0: 74 69 6d 65 2e 74 76 5f 6e 73 65 63 20 3d 20 30  time.tv_nsec = 0
42f0: 3b 0a 09 09 09 6e 61 6e 6f 73 6c 65 65 70 28 26  ;....nanosleep(&
4300: 73 6c 65 65 70 5f 74 69 6d 65 2c 20 4e 55 4c 4c  sleep_time, NULL
4310: 29 3b 0a 0a 09 09 09 70 74 68 72 65 61 64 5f 6d  );.....pthread_m
4320: 75 74 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64  utex_lock(&filed
4330: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d  _sockettimeout_m
4340: 75 74 65 78 29 3b 0a 0a 09 09 09 6e 6f 77 20 3d  utex);.....now =
4350: 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 09   time(NULL);....
4360: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d  .filed_sockettim
4370: 65 6f 75 74 5f 74 69 6d 65 20 3d 20 6e 6f 77 3b  eout_time = now;
4380: 0a 0a 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74  .....pthread_mut
4390: 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64  ex_unlock(&filed
43a0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d  _sockettimeout_m
43b0: 75 74 65 78 29 3b 0a 09 09 7d 0a 0a 09 09 70 74  utex);...}....pt
43c0: 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b  hread_mutex_lock
43d0: 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  (&filed_socketti
43e0: 6d 65 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 0a 09  meout_mutex);...
43f0: 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 69  .for (idx = 0; i
4400: 64 78 20 3c 20 66 69 6c 65 64 5f 73 6f 63 6b 65  dx < filed_socke
4410: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61  ttimeout_socksta
4420: 74 75 73 5f 6c 65 6e 67 74 68 3b 20 69 64 78 2b  tus_length; idx+
4430: 2b 29 20 7b 0a 09 09 09 76 61 6c 69 64 20 3d 20  +) {....valid = 
4440: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
4450: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 69  out_sockstatus[i
4460: 64 78 5d 2e 76 61 6c 69 64 3b 0a 0a 09 09 09 69  dx].valid;.....i
4470: 66 20 28 76 61 6c 69 64 20 21 3d 20 66 69 6c 65  f (valid != file
4480: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
4490: 76 61 6c 69 64 29 20 7b 0a 09 09 09 09 63 6f 6e  valid) {.....con
44a0: 74 69 6e 75 65 3b 0a 09 09 09 7d 0a 0a 09 09 09  tinue;....}.....
44b0: 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 20  expiration_time 
44c0: 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  = filed_socketti
44d0: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73  meout_sockstatus
44e0: 5b 69 64 78 5d 2e 65 78 70 69 72 61 74 69 6f 6e  [idx].expiration
44f0: 5f 74 69 6d 65 3b 0a 0a 09 09 09 74 68 72 65 61  _time;.....threa
4500: 64 5f 69 64 20 3d 20 66 69 6c 65 64 5f 73 6f 63  d_id = filed_soc
4510: 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73  kettimeout_socks
4520: 74 61 74 75 73 5b 69 64 78 5d 2e 74 68 72 65 61  tatus[idx].threa
4530: 64 5f 69 64 3b 0a 0a 09 09 09 69 66 20 28 65 78  d_id;.....if (ex
4540: 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 20 3e 20  piration_time > 
4550: 6e 6f 77 29 20 7b 0a 09 09 09 09 63 6f 6e 74 69  now) {.....conti
4560: 6e 75 65 3b 0a 09 09 09 7d 0a 0a 09 09 09 66 69  nue;....}.....fi
4570: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
4580: 74 5f 63 6c 6f 73 65 28 69 64 78 2c 20 31 29 3b  t_close(idx, 1);
4590: 0a 0a 09 09 09 64 75 70 32 28 66 69 6c 65 64 5f  .....dup2(filed_
45a0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 64 65  sockettimeout_de
45b0: 76 6e 75 6c 6c 5f 66 64 2c 20 69 64 78 29 3b 0a  vnull_fd, idx);.
45c0: 0a 09 09 09 70 74 68 72 65 61 64 5f 6b 69 6c 6c  ....pthread_kill
45d0: 28 74 68 72 65 61 64 5f 69 64 2c 20 53 49 47 50  (thread_id, SIGP
45e0: 49 50 45 29 3b 0a 09 09 7d 0a 0a 09 09 70 74 68  IPE);...}....pth
45f0: 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63  read_mutex_unloc
4600: 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  k(&filed_sockett
4610: 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 09  imeout_mutex);..
4620: 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29  }...return(NULL)
4630: 3b 0a 0a 09 2f 2a 20 4e 4f 54 52 45 41 43 48 3a  ;.../* NOTREACH:
4640: 20 57 65 20 64 6f 6e 27 74 20 61 63 74 75 61 6c   We don't actual
4650: 6c 79 20 74 61 6b 65 20 61 6e 79 20 61 72 67 75  ly take any argu
4660: 6d 65 6e 74 73 20 2a 2f 0a 09 61 72 67 20 3d 20  ments */..arg = 
4670: 61 72 67 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  arg;.}..static i
4680: 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74  nt filed_sockett
4690: 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e  imeout_thread_in
46a0: 69 74 28 76 6f 69 64 29 20 7b 0a 09 70 74 68 72  it(void) {..pthr
46b0: 65 61 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b  ead_t thread_id;
46c0: 0a 0a 09 70 74 68 72 65 61 64 5f 63 72 65 61 74  ...pthread_creat
46d0: 65 28 26 74 68 72 65 61 64 5f 69 64 2c 20 4e 55  e(&thread_id, NU
46e0: 4c 4c 2c 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74  LL, filed_socket
46f0: 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 2c 20  timeout_thread, 
4700: 4e 55 4c 4c 29 3b 0a 0a 09 72 65 74 75 72 6e 28  NULL);...return(
4710: 30 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  0);.}..static in
4720: 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  t filed_socketti
4730: 6d 65 6f 75 74 5f 69 6e 69 74 28 76 6f 69 64 29  meout_init(void)
4740: 20 7b 0a 09 6c 6f 6e 67 20 6d 61 78 66 64 2c 20   {..long maxfd, 
4750: 69 64 78 3b 0a 0a 09 6d 61 78 66 64 20 3d 20 73  idx;...maxfd = s
4760: 79 73 63 6f 6e 66 28 5f 53 43 5f 4f 50 45 4e 5f  ysconf(_SC_OPEN_
4770: 4d 41 58 29 3b 0a 09 69 66 20 28 6d 61 78 66 64  MAX);..if (maxfd
4780: 20 3c 3d 20 30 29 20 7b 0a 09 09 6d 61 78 66 64   <= 0) {...maxfd
4790: 20 3d 20 34 30 39 36 3b 0a 09 7d 0a 0a 09 66 69   = 4096;..}...fi
47a0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
47b0: 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c 65 6e  t_sockstatus_len
47c0: 67 74 68 20 3d 20 6d 61 78 66 64 3b 0a 09 66 69  gth = maxfd;..fi
47d0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
47e0: 74 5f 73 6f 63 6b 73 74 61 74 75 73 20 3d 20 6d  t_sockstatus = m
47f0: 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 66 69  alloc(sizeof(*fi
4800: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
4810: 74 5f 73 6f 63 6b 73 74 61 74 75 73 29 20 2a 20  t_sockstatus) * 
4820: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
4830: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c  out_sockstatus_l
4840: 65 6e 67 74 68 29 3b 0a 09 69 66 20 28 66 69 6c  ength);..if (fil
4850: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
4860: 5f 73 6f 63 6b 73 74 61 74 75 73 20 3d 3d 20 4e  _sockstatus == N
4870: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  ULL) {...return(
4880: 2d 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 20 28 69  -1);..}...for (i
4890: 64 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 6d 61  dx = 0; idx < ma
48a0: 78 66 64 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09  xfd; idx++) {...
48b0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65  filed_sockettime
48c0: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 69  out_sockstatus[i
48d0: 64 78 5d 2e 76 61 6c 69 64 20 3d 20 66 69 6c 65  dx].valid = file
48e0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f  d_sockettimeout_
48f0: 69 6e 76 61 6c 69 64 3b 0a 09 7d 0a 0a 09 66 69  invalid;..}...fi
4900: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
4910: 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 20 3d 20 6f  t_devnull_fd = o
4920: 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c  pen("/dev/null",
4930: 20 4f 5f 52 44 57 52 29 3b 0a 09 69 66 20 28 66   O_RDWR);..if (f
4940: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f  iled_sockettimeo
4950: 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 20 3c 20  ut_devnull_fd < 
4960: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 2d 31  0) {...return(-1
4970: 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 30  );..}...return(0
4980: 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  );.}.#endif../* 
4990: 46 6f 72 6d 61 74 20 74 69 6d 65 20 70 65 72 20  Format time per 
49a0: 52 46 43 32 36 31 36 20 2a 2f 0a 73 74 61 74 69  RFC2616 */.stati
49b0: 63 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 66 6f  c char *filed_fo
49c0: 72 6d 61 74 5f 74 69 6d 65 28 63 68 61 72 20 2a  rmat_time(char *
49d0: 62 75 66 66 65 72 2c 20 73 69 7a 65 5f 74 20 62  buffer, size_t b
49e0: 75 66 66 65 72 5f 6c 65 6e 2c 20 63 6f 6e 73 74  uffer_len, const
49f0: 20 74 69 6d 65 5f 74 20 74 69 6d 65 69 6e 66 6f   time_t timeinfo
4a00: 29 20 7b 0a 09 73 74 72 75 63 74 20 74 6d 20 74  ) {..struct tm t
4a10: 69 6d 65 69 6e 66 6f 5f 74 6d 2c 20 2a 74 69 6d  imeinfo_tm, *tim
4a20: 65 69 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a 09 74 69  einfo_tm_p;...ti
4a30: 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d 20 67 6d  meinfo_tm_p = gm
4a40: 74 69 6d 65 5f 72 28 26 74 69 6d 65 69 6e 66 6f  time_r(&timeinfo
4a50: 2c 20 26 74 69 6d 65 69 6e 66 6f 5f 74 6d 29 3b  , &timeinfo_tm);
4a60: 0a 09 69 66 20 28 74 69 6d 65 69 6e 66 6f 5f 74  ..if (timeinfo_t
4a70: 6d 5f 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09  m_p == NULL) {..
4a80: 09 72 65 74 75 72 6e 28 22 75 6e 6b 6e 6f 77 6e  .return("unknown
4a90: 22 29 3b 0a 09 7d 0a 0a 09 62 75 66 66 65 72 5b  ");..}...buffer[
4aa0: 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 31 5d 20  buffer_len - 1] 
4ab0: 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 5f  = '\0';..buffer_
4ac0: 6c 65 6e 20 3d 20 73 74 72 66 74 69 6d 65 28 62  len = strftime(b
4ad0: 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65  uffer, buffer_le
4ae0: 6e 20 2d 20 31 2c 20 22 25 61 2c 20 25 64 20 25  n - 1, "%a, %d %
4af0: 62 20 25 59 20 25 48 3a 25 4d 3a 25 53 20 47 4d  b %Y %H:%M:%S GM
4b00: 54 22 2c 20 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f  T", timeinfo_tm_
4b10: 70 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75 66  p);...return(buf
4b20: 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 61 73 68  fer);.}../* hash
4b30: 20 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 67   */.static unsig
4b40: 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 5f 68 61  ned int filed_ha
4b50: 73 68 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  sh(const unsigne
4b60: 64 20 63 68 61 72 20 2a 76 61 6c 75 65 2c 20 75  d char *value, u
4b70: 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 6f 64 75  nsigned int modu
4b80: 6c 75 73 29 20 7b 0a 09 75 6e 73 69 67 6e 65 64  lus) {..unsigned
4b90: 20 63 68 61 72 20 63 75 72 72 2c 20 70 72 65 76   char curr, prev
4ba0: 3b 0a 09 69 6e 74 20 64 69 66 66 3b 0a 09 75 6e  ;..int diff;..un
4bb0: 73 69 67 6e 65 64 20 69 6e 74 20 72 65 74 76 61  signed int retva
4bc0: 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d 6f  l;...retval = mo
4bd0: 64 75 6c 75 73 20 2d 20 31 3b 0a 09 70 72 65 76  dulus - 1;..prev
4be0: 20 3d 20 6d 6f 64 75 6c 75 73 20 25 20 32 35 35   = modulus % 255
4bf0: 3b 0a 0a 09 77 68 69 6c 65 20 28 28 63 75 72 72  ;...while ((curr
4c00: 20 3d 20 2a 76 61 6c 75 65 29 29 20 7b 0a 09 09   = *value)) {...
4c10: 69 66 20 28 63 75 72 72 20 3c 20 33 32 29 20 7b  if (curr < 32) {
4c20: 0a 09 09 09 63 75 72 72 20 3d 20 32 35 35 20 2d  ....curr = 255 -
4c30: 20 63 75 72 72 3b 0a 09 09 7d 20 65 6c 73 65 20   curr;...} else 
4c40: 7b 0a 09 09 09 63 75 72 72 20 2d 3d 20 33 32 3b  {....curr -= 32;
4c50: 0a 09 09 7d 0a 0a 09 09 69 66 20 28 70 72 65 76  ...}....if (prev
4c60: 20 3c 20 63 75 72 72 29 20 7b 0a 09 09 09 64 69   < curr) {....di
4c70: 66 66 20 3d 20 63 75 72 72 20 2d 20 70 72 65 76  ff = curr - prev
4c80: 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09  ;...} else {....
4c90: 64 69 66 66 20 3d 20 70 72 65 76 20 2d 20 63 75  diff = prev - cu
4ca0: 72 72 3b 0a 09 09 7d 0a 0a 09 09 70 72 65 76 20  rr;...}....prev 
4cb0: 3d 20 63 75 72 72 3b 0a 0a 09 09 72 65 74 76 61  = curr;....retva
4cc0: 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 65 74 76 61  l <<= 3;...retva
4cd0: 6c 20 26 3d 20 30 78 46 46 46 46 46 46 46 46 4c  l &= 0xFFFFFFFFL
4ce0: 55 3b 0a 09 09 72 65 74 76 61 6c 20 5e 3d 20 64  U;...retval ^= d
4cf0: 69 66 66 3b 0a 0a 09 09 76 61 6c 75 65 2b 2b 3b  iff;....value++;
4d00: 0a 09 7d 0a 0a 09 72 65 74 76 61 6c 20 3d 20 72  ..}...retval = r
4d10: 65 74 76 61 6c 20 25 20 6d 6f 64 75 6c 75 73 3b  etval % modulus;
4d20: 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c  ...return(retval
4d30: 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 20 61 20  );.}../* Find a 
4d40: 6d 69 6d 65 2d 74 79 70 65 20 62 61 73 65 64 20  mime-type based 
4d50: 6f 6e 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20  on the filename 
4d60: 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
4d70: 63 68 61 72 20 2a 66 69 6c 65 64 5f 64 65 74 65  char *filed_dete
4d80: 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 63  rmine_mimetype(c
4d90: 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 29  onst char *path)
4da0: 20 7b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a   {..const char *
4db0: 70 3b 0a 0a 09 70 20 3d 20 73 74 72 72 63 68 72  p;...p = strrchr
4dc0: 28 70 61 74 68 2c 20 27 2e 27 29 3b 0a 09 69 66  (path, '.');..if
4dd0: 20 28 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09   (p == NULL) {..
4de0: 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 45  .return(FILED_DE
4df0: 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d 0a  FAULT_TYPE);..}.
4e00: 0a 09 70 2b 2b 3b 0a 09 69 66 20 28 2a 70 20 3d  ..p++;..if (*p =
4e10: 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 65 74 75  = '\0') {...retu
4e20: 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54  rn(FILED_DEFAULT
4e30: 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c  _TYPE);..}...fil
4e40: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67  ed_log_msg_debug
4e50: 28 22 4c 6f 6f 6b 69 6e 67 20 75 70 20 4d 49 4d  ("Looking up MIM
4e60: 45 20 74 79 70 65 20 66 6f 72 20 25 73 20 28 68  E type for %s (h
4e70: 61 73 68 20 3d 20 25 6c 6c 75 29 22 2c 20 70 2c  ash = %llu)", p,
4e80: 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20   (unsigned long 
4e90: 6c 6f 6e 67 29 20 66 69 6c 65 64 5f 68 61 73 68  long) filed_hash
4ea0: 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  ((const unsigned
4eb0: 20 63 68 61 72 20 2a 29 20 70 2c 20 31 36 37 37   char *) p, 1677
4ec0: 37 32 35 39 29 29 3b 0a 0a 23 69 6e 63 6c 75 64  7259));..#includ
4ed0: 65 20 22 66 69 6c 65 64 2d 6d 69 6d 65 2d 74 79  e "filed-mime-ty
4ee0: 70 65 73 2e 68 22 0a 0a 09 72 65 74 75 72 6e 28  pes.h"...return(
4ef0: 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59  FILED_DEFAULT_TY
4f00: 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 6e 65 72  PE);.}../* Gener
4f10: 61 74 65 20 61 20 75 6e 69 71 75 65 20 69 64 65  ate a unique ide
4f20: 6e 74 69 66 69 65 72 20 2a 2f 0a 73 74 61 74 69  ntifier */.stati
4f30: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 6e  c void filed_gen
4f40: 65 72 61 74 65 5f 65 74 61 67 28 63 68 61 72 20  erate_etag(char 
4f50: 2a 65 74 61 67 2c 20 73 69 7a 65 5f 74 20 6c 65  *etag, size_t le
4f60: 6e 67 74 68 29 20 7b 0a 09 73 6e 70 72 69 6e 74  ngth) {..snprint
4f70: 66 28 65 74 61 67 2c 20 6c 65 6e 67 74 68 2c 20  f(etag, length, 
4f80: 22 25 6c 6c 78 2d 25 6c 6c 78 25 6c 6c 78 25 6c  "%llx-%llx%llx%l
4f90: 6c 78 25 6c 6c 78 22 2c 0a 09 09 28 75 6e 73 69  lx%llx",...(unsi
4fa0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
4fb0: 74 69 6d 65 28 4e 55 4c 4c 29 2c 0a 09 09 28 75  time(NULL),...(u
4fc0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
4fd0: 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09 28  g) random(),...(
4fe0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
4ff0: 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09  ng) random(),...
5000: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
5010: 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09  ong) random(),..
5020: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
5030: 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 0a 09  long) random()..
5040: 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 46 49 4c  );.}..#ifdef FIL
5050: 45 44 5f 46 41 4b 45 5f 43 48 52 4f 4f 54 0a 2f  ED_FAKE_CHROOT./
5060: 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 70 61  * Translate a pa
5070: 74 68 20 69 6e 74 6f 20 61 20 66 61 6b 65 20 63  th into a fake c
5080: 68 72 6f 6f 74 20 70 61 74 68 20 2a 2f 0a 73 74  hroot path */.st
5090: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
50a0: 2a 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 61 6e  *filed_path_tran
50b0: 73 6c 61 74 65 28 63 6f 6e 73 74 20 63 68 61 72  slate(const char
50c0: 20 2a 70 61 74 68 2c 20 73 74 72 75 63 74 20 66   *path, struct f
50d0: 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70  iled_options *op
50e0: 74 69 6f 6e 73 29 20 7b 0a 09 73 74 61 74 69 63  tions) {..static
50f0: 20 5f 5f 74 68 72 65 61 64 20 63 68 61 72 20 70   __thread char p
5100: 61 74 68 42 75 66 66 65 72 5b 38 31 39 32 5d 3b  athBuffer[8192];
5110: 0a 09 69 6e 74 20 73 6e 70 72 69 6e 74 66 5f 72  ..int snprintf_r
5120: 65 74 3b 0a 0a 09 2f 2a 20 49 66 20 6e 6f 20 61  et;.../* If no a
5130: 6c 74 65 72 6e 61 74 69 76 65 20 72 6f 6f 74 20  lternative root 
5140: 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 72 65  is specified, re
5150: 74 75 72 6e 20 74 68 65 20 75 6e 61 64 6f 72 6e  turn the unadorn
5160: 65 64 20 70 61 74 68 20 2a 2f 0a 09 69 66 20 28  ed path */..if (
5170: 21 6f 70 74 69 6f 6e 73 2d 3e 66 61 6b 65 5f 6e  !options->fake_n
5180: 65 77 72 6f 6f 74 29 20 7b 0a 09 09 72 65 74 75  ewroot) {...retu
5190: 72 6e 28 70 61 74 68 29 3b 0a 09 7d 0a 0a 09 2f  rn(path);..}.../
51a0: 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
51b0: 69 73 20 72 65 71 75 65 73 74 20 77 69 6c 6c 20  is request will 
51c0: 6e 6f 74 20 67 6f 20 6f 75 74 73 69 64 65 20 6f  not go outside o
51d0: 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  f the specified 
51e0: 72 6f 6f 74 20 2a 2f 0a 09 69 66 20 28 73 74 72  root */..if (str
51f0: 73 74 72 28 70 61 74 68 2c 20 22 2f 2e 2e 2f 22  str(path, "/../"
5200: 29 20 21 3d 20 4e 55 4c 4c 20 7c 7c 20 70 61 74  ) != NULL || pat
5210: 68 5b 30 5d 20 21 3d 20 27 2f 27 29 20 7b 0a 09  h[0] != '/') {..
5220: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64  .filed_log_msg_d
5230: 65 62 75 67 28 22 55 6e 61 62 6c 65 20 74 6f 20  ebug("Unable to 
5240: 74 72 61 6e 73 6c 61 74 65 20 70 61 74 68 20 5c  translate path \
5250: 22 25 73 5c 22 2c 20 63 6f 6e 74 61 69 6e 73 20  "%s\", contains 
5260: 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 65  invalid characte
5270: 72 73 22 2c 20 70 61 74 68 29 3b 0a 0a 09 09 72  rs", path);....r
5280: 65 74 75 72 6e 28 6f 70 74 69 6f 6e 73 2d 3e 66  eturn(options->f
5290: 61 6b 65 5f 6e 65 77 72 6f 6f 74 29 3b 0a 09 7d  ake_newroot);..}
52a0: 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 74 68 65  .../* Create the
52b0: 20 6e 65 77 20 70 61 74 68 20 69 6e 74 6f 20 6f   new path into o
52c0: 75 72 20 6c 6f 63 61 6c 20 28 54 4c 53 29 20 73  ur local (TLS) s
52d0: 74 61 74 69 63 20 62 75 66 66 65 72 20 2a 2f 0a  tatic buffer */.
52e0: 09 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3d 20  .snprintf_ret = 
52f0: 73 6e 70 72 69 6e 74 66 28 70 61 74 68 42 75 66  snprintf(pathBuf
5300: 66 65 72 2c 20 73 69 7a 65 6f 66 28 70 61 74 68  fer, sizeof(path
5310: 42 75 66 66 65 72 29 2c 20 22 25 73 2f 25 73 22  Buffer), "%s/%s"
5320: 2c 20 6f 70 74 69 6f 6e 73 2d 3e 66 61 6b 65 5f  , options->fake_
5330: 6e 65 77 72 6f 6f 74 2c 20 70 61 74 68 29 3b 0a  newroot, path);.
5340: 09 69 66 20 28 73 6e 70 72 69 6e 74 66 5f 72 65  .if (snprintf_re
5350: 74 20 3c 20 30 20 7c 7c 20 28 28 75 6e 73 69 67  t < 0 || ((unsig
5360: 6e 65 64 20 69 6e 74 29 20 73 6e 70 72 69 6e 74  ned int) snprint
5370: 66 5f 72 65 74 29 20 3e 3d 20 73 69 7a 65 6f 66  f_ret) >= sizeof
5380: 28 70 61 74 68 42 75 66 66 65 72 29 29 20 7b 0a  (pathBuffer)) {.
5390: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  ..filed_log_msg_
53a0: 64 65 62 75 67 28 22 55 6e 61 62 6c 65 20 74 6f  debug("Unable to
53b0: 20 74 72 61 6e 73 6c 61 74 65 20 70 61 74 68 20   translate path 
53c0: 5c 22 25 73 5c 22 2c 20 77 69 6c 6c 20 6e 6f 74  \"%s\", will not
53d0: 20 66 69 74 20 69 6e 74 6f 20 6e 65 77 20 62 75   fit into new bu
53e0: 66 66 65 72 22 2c 20 70 61 74 68 29 3b 0a 0a 09  ffer", path);...
53f0: 09 72 65 74 75 72 6e 28 6f 70 74 69 6f 6e 73 2d  .return(options-
5400: 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 29 3b 0a  >fake_newroot);.
5410: 09 7d 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  .}...filed_log_m
5420: 73 67 5f 64 65 62 75 67 28 22 54 72 61 6e 73 6c  sg_debug("Transl
5430: 61 74 69 6e 67 20 70 61 74 68 20 5c 22 25 73 5c  ating path \"%s\
5440: 22 20 69 6e 74 6f 20 5c 22 25 73 5c 22 22 2c 20  " into \"%s\"", 
5450: 70 61 74 68 2c 20 70 61 74 68 42 75 66 66 65 72  path, pathBuffer
5460: 29 3b 0a 0a 09 2f 2a 20 52 65 74 75 72 6e 20 74  );.../* Return t
5470: 68 65 20 6e 65 77 20 70 61 74 68 20 2a 2f 0a 09  he new path */..
5480: 72 65 74 75 72 6e 28 70 61 74 68 42 75 66 66 65  return(pathBuffe
5490: 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  r);.}..static vo
54a0: 69 64 20 66 69 6c 65 64 5f 70 61 74 68 5f 74 72  id filed_path_tr
54b0: 61 6e 73 6c 61 74 65 5f 73 65 74 5f 72 6f 6f 74  anslate_set_root
54c0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 76 61 72  (const char *var
54d0: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f  , struct filed_o
54e0: 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 2c  ptions *options,
54f0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 76 61 6c   const char *val
5500: 29 20 7b 0a 09 6f 70 74 69 6f 6e 73 2d 3e 66 61  ) {..options->fa
5510: 6b 65 5f 6e 65 77 72 6f 6f 74 20 3d 20 73 74 72  ke_newroot = str
5520: 64 75 70 28 76 61 6c 29 3b 0a 0a 09 72 65 74 75  dup(val);...retu
5530: 72 6e 3b 0a 0a 09 2f 2a 20 76 61 72 20 69 73 20  rn;.../* var is 
5540: 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 74 68 65  only used in the
5550: 20 6d 61 63 72 6f 20 2d 2d 20 64 69 73 63 61 72   macro -- discar
5560: 64 20 69 74 20 68 65 72 65 20 2a 2f 0a 09 76 61  d it here */..va
5570: 72 20 3d 20 76 61 72 3b 0a 7d 0a 23 65 6c 73 65  r = var;.}.#else
5580: 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 70  .#define filed_p
5590: 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 28 70 61  ath_translate(pa
55a0: 74 68 2c 20 6f 70 74 69 6f 6e 73 29 20 70 61 74  th, options) pat
55b0: 68 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f  h.#define filed_
55c0: 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 5f 73  path_translate_s
55d0: 65 74 5f 72 6f 6f 74 28 76 61 72 2c 20 6f 70 74  et_root(var, opt
55e0: 69 6f 6e 73 2c 20 76 61 6c 29 20 76 61 72 20 3d  ions, val) var =
55f0: 20 73 74 72 64 75 70 28 76 61 6c 29 0a 23 65 6e   strdup(val).#en
5600: 64 69 66 0a 0a 2f 2a 20 4f 70 65 6e 20 61 20 66  dif../* Open a f
5610: 69 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 66  ile and return f
5620: 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ile information 
5630: 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
5640: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20   filed_fileinfo 
5650: 2a 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65  *filed_open_file
5660: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74  (const char *pat
5670: 68 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f  h, struct filed_
5680: 66 69 6c 65 69 6e 66 6f 20 2a 62 75 66 66 65 72  fileinfo *buffer
5690: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f  , struct filed_o
56a0: 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29  ptions *options)
56b0: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64   {..struct filed
56c0: 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 63 68 65  _fileinfo *cache
56d0: 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  ;..unsigned int 
56e0: 63 61 63 68 65 5f 69 64 78 3b 0a 09 6f 66 66 5f  cache_idx;..off_
56f0: 74 20 6c 65 6e 3b 0a 09 69 6e 74 20 66 64 3b 0a  t len;..int fd;.
5700: 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65  ..if (filed_file
5710: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a  info_fdcache_siz
5720: 65 20 21 3d 20 30 29 20 7b 0a 09 09 63 61 63 68  e != 0) {...cach
5730: 65 5f 69 64 78 20 3d 20 66 69 6c 65 64 5f 68 61  e_idx = filed_ha
5740: 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  sh((const unsign
5750: 65 64 20 63 68 61 72 20 2a 29 20 70 61 74 68 2c  ed char *) path,
5760: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f   filed_fileinfo_
5770: 66 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 0a  fdcache_size);..
5780: 09 09 63 61 63 68 65 20 3d 20 26 66 69 6c 65 64  ..cache = &filed
5790: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68  _fileinfo_fdcach
57a0: 65 5b 63 61 63 68 65 5f 69 64 78 5d 3b 0a 0a 09  e[cache_idx];...
57b0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64  .filed_log_msg_d
57c0: 65 62 75 67 28 22 4c 6f 63 6b 69 6e 67 20 6d 75  ebug("Locking mu
57d0: 74 65 78 20 66 6f 72 20 69 64 78 3a 20 25 6c 75  tex for idx: %lu
57e0: 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ", (unsigned lon
57f0: 67 29 20 63 61 63 68 65 5f 69 64 78 29 3b 0a 0a  g) cache_idx);..
5800: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  ..pthread_mutex_
5810: 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74  lock(&cache->mut
5820: 65 78 29 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f  ex);....filed_lo
5830: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d  g_msg_debug("Com
5840: 70 6c 65 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6d  pleted locking m
5850: 75 74 65 78 20 66 6f 72 20 69 64 78 3a 20 25 6c  utex for idx: %l
5860: 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f  u", (unsigned lo
5870: 6e 67 29 20 63 61 63 68 65 5f 69 64 78 29 3b 0a  ng) cache_idx);.
5880: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 63 61 63 68  .} else {...cach
5890: 65 5f 69 64 78 20 3d 20 30 3b 0a 09 09 63 61 63  e_idx = 0;...cac
58a0: 68 65 20 3d 20 62 75 66 66 65 72 3b 0a 09 09 63  he = buffer;...c
58b0: 61 63 68 65 2d 3e 70 61 74 68 5b 30 5d 20 3d 20  ache->path[0] = 
58c0: 27 5c 30 27 3b 0a 09 09 63 61 63 68 65 2d 3e 66  '\0';...cache->f
58d0: 64 20 3d 20 2d 31 3b 0a 09 7d 0a 0a 09 69 66 20  d = -1;..}...if 
58e0: 28 73 74 72 63 6d 70 28 70 61 74 68 2c 20 63 61  (strcmp(path, ca
58f0: 63 68 65 2d 3e 70 61 74 68 29 20 21 3d 20 30 29  che->path) != 0)
5900: 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d   {...filed_log_m
5910: 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65 20  sg_debug("Cache 
5920: 6d 69 73 73 20 66 6f 72 20 69 64 78 3a 20 25 6c  miss for idx: %l
5930: 75 3a 20 4f 4c 44 20 5c 22 25 73 5c 22 2c 20 4e  u: OLD \"%s\", N
5940: 45 57 20 5c 22 25 73 5c 22 22 2c 20 28 75 6e 73  EW \"%s\"", (uns
5950: 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68  igned long) cach
5960: 65 5f 69 64 78 2c 20 63 61 63 68 65 2d 3e 70 61  e_idx, cache->pa
5970: 74 68 2c 20 70 61 74 68 29 3b 0a 0a 09 09 66 64  th, path);....fd
5980: 20 3d 20 6f 70 65 6e 28 66 69 6c 65 64 5f 70 61   = open(filed_pa
5990: 74 68 5f 74 72 61 6e 73 6c 61 74 65 28 70 61 74  th_translate(pat
59a0: 68 2c 20 6f 70 74 69 6f 6e 73 29 2c 20 4f 5f 52  h, options), O_R
59b0: 44 4f 4e 4c 59 20 7c 20 4f 5f 4c 41 52 47 45 46  DONLY | O_LARGEF
59c0: 49 4c 45 29 3b 0a 09 09 69 66 20 28 66 64 20 3c  ILE);...if (fd <
59d0: 20 30 29 20 7b 0a 09 09 09 69 66 20 28 66 69 6c   0) {....if (fil
59e0: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61  ed_fileinfo_fdca
59f0: 63 68 65 5f 73 69 7a 65 20 21 3d 20 30 29 20 7b  che_size != 0) {
5a00: 0a 09 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74  .....pthread_mut
5a10: 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65  ex_unlock(&cache
5a20: 2d 3e 6d 75 74 65 78 29 3b 0a 09 09 09 7d 0a 0a  ->mutex);....}..
5a30: 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b  ...return(NULL);
5a40: 0a 09 09 7d 0a 0a 09 09 69 66 20 28 63 61 63 68  ...}....if (cach
5a50: 65 2d 3e 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09  e->fd >= 0) {...
5a60: 09 63 6c 6f 73 65 28 63 61 63 68 65 2d 3e 66 64  .close(cache->fd
5a70: 29 3b 0a 09 09 7d 0a 0a 09 09 6c 65 6e 20 3d 20  );...}....len = 
5a80: 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 53 45 45  lseek(fd, 0, SEE
5a90: 4b 5f 45 4e 44 29 3b 0a 09 09 6c 73 65 65 6b 28  K_END);...lseek(
5aa0: 66 64 2c 20 30 2c 20 53 45 45 4b 5f 53 45 54 29  fd, 0, SEEK_SET)
5ab0: 3b 0a 0a 09 09 63 61 63 68 65 2d 3e 66 64 20 3d  ;....cache->fd =
5ac0: 20 66 64 3b 0a 09 09 63 61 63 68 65 2d 3e 6c 65   fd;...cache->le
5ad0: 6e 20 3d 20 6c 65 6e 3b 0a 09 09 73 74 72 63 70  n = len;...strcp
5ae0: 79 28 63 61 63 68 65 2d 3e 70 61 74 68 2c 20 70  y(cache->path, p
5af0: 61 74 68 29 3b 0a 09 09 63 61 63 68 65 2d 3e 74  ath);...cache->t
5b00: 79 70 65 20 3d 20 66 69 6c 65 64 5f 64 65 74 65  ype = filed_dete
5b10: 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 70  rmine_mimetype(p
5b20: 61 74 68 29 3b 0a 09 09 66 69 6c 65 64 5f 67 65  ath);...filed_ge
5b30: 6e 65 72 61 74 65 5f 65 74 61 67 28 63 61 63 68  nerate_etag(cach
5b40: 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f 66 28  e->etag, sizeof(
5b50: 63 61 63 68 65 2d 3e 65 74 61 67 29 29 3b 0a 0a  cache->etag));..
5b60: 09 09 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 44  ../* XXX:TODO: D
5b70: 65 74 65 72 6d 69 6e 65 20 2a 2f 0a 09 09 63 61  etermine */...ca
5b80: 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20 66  che->lastmod = f
5b90: 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65  iled_format_time
5ba0: 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f  (cache->lastmod_
5bb0: 62 2c 20 73 69 7a 65 6f 66 28 63 61 63 68 65 2d  b, sizeof(cache-
5bc0: 3e 6c 61 73 74 6d 6f 64 5f 62 29 2c 20 74 69 6d  >lastmod_b), tim
5bd0: 65 28 4e 55 4c 4c 29 20 2d 20 33 30 29 3b 0a 09  e(NULL) - 30);..
5be0: 7d 20 65 6c 73 65 20 7b 0a 09 09 66 69 6c 65 64  } else {...filed
5bf0: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22  _log_msg_debug("
5c00: 43 61 63 68 65 20 68 69 74 20 66 6f 72 20 69 64  Cache hit for id
5c10: 78 3a 20 25 6c 75 3a 20 50 41 54 48 20 5c 22 25  x: %lu: PATH \"%
5c20: 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e 65 64 20  s\"", (unsigned 
5c30: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 2c  long) cache_idx,
5c40: 20 70 61 74 68 29 3b 0a 09 7d 0a 0a 09 69 66 20   path);..}...if 
5c50: 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f  (filed_fileinfo_
5c60: 66 64 63 61 63 68 65 5f 73 69 7a 65 20 21 3d 20  fdcache_size != 
5c70: 30 29 20 7b 0a 09 09 2f 2a 0a 09 09 20 2a 20 57  0) {.../*... * W
5c80: 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61  e have to make a
5c90: 20 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20 62   duplicate FD, b
5ca0: 65 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20 72  ecause once we r
5cb0: 65 6c 65 61 73 65 20 74 68 65 20 63 61 63 68 65  elease the cache
5cc0: 0a 09 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 65  ... * mutex, the
5cd0: 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
5ce0: 20 6d 61 79 20 62 65 20 63 6c 6f 73 65 64 0a 09   may be closed..
5cf0: 09 20 2a 2f 0a 09 09 66 64 20 3d 20 64 75 70 28  . */...fd = dup(
5d00: 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 09 69 66  cache->fd);...if
5d10: 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 09 63   (fd < 0) {....c
5d20: 6c 6f 73 65 28 63 61 63 68 65 2d 3e 66 64 29 3b  lose(cache->fd);
5d30: 0a 09 09 09 63 61 63 68 65 2d 3e 66 64 20 3d 20  ....cache->fd = 
5d40: 2d 31 3b 0a 0a 09 09 09 70 74 68 72 65 61 64 5f  -1;.....pthread_
5d50: 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61  mutex_unlock(&ca
5d60: 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09  che->mutex);....
5d70: 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09  .return(NULL);..
5d80: 09 7d 0a 0a 09 09 62 75 66 66 65 72 2d 3e 66 64  .}....buffer->fd
5d90: 20 3d 20 66 64 3b 0a 09 09 62 75 66 66 65 72 2d   = fd;...buffer-
5da0: 3e 6c 65 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65  >len = cache->le
5db0: 6e 3b 0a 09 09 62 75 66 66 65 72 2d 3e 74 79 70  n;...buffer->typ
5dc0: 65 20 3d 20 63 61 63 68 65 2d 3e 74 79 70 65 3b  e = cache->type;
5dd0: 0a 09 09 6d 65 6d 63 70 79 28 62 75 66 66 65 72  ...memcpy(buffer
5de0: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 63 61 63  ->lastmod_b, cac
5df0: 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73  he->lastmod_b, s
5e00: 69 7a 65 6f 66 28 62 75 66 66 65 72 2d 3e 6c 61  izeof(buffer->la
5e10: 73 74 6d 6f 64 5f 62 29 29 3b 0a 09 09 6d 65 6d  stmod_b));...mem
5e20: 63 70 79 28 62 75 66 66 65 72 2d 3e 65 74 61 67  cpy(buffer->etag
5e30: 2c 20 63 61 63 68 65 2d 3e 65 74 61 67 2c 20 73  , cache->etag, s
5e40: 69 7a 65 6f 66 28 62 75 66 66 65 72 2d 3e 65 74  izeof(buffer->et
5e50: 61 67 29 29 3b 0a 09 09 62 75 66 66 65 72 2d 3e  ag));...buffer->
5e60: 6c 61 73 74 6d 6f 64 20 3d 20 62 75 66 66 65 72  lastmod = buffer
5e70: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 20 2b 20 28 63  ->lastmod_b + (c
5e80: 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 2d 20  ache->lastmod - 
5e90: 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62  cache->lastmod_b
5ea0: 29 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d 75  );....pthread_mu
5eb0: 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68  tex_unlock(&cach
5ec0: 65 2d 3e 6d 75 74 65 78 29 3b 0a 09 7d 0a 0a 09  e->mutex);..}...
5ed0: 72 65 74 75 72 6e 28 62 75 66 66 65 72 29 3b 0a  return(buffer);.
5ee0: 0a 09 2f 2a 20 6f 70 74 69 6f 6e 73 20 69 73 20  ../* options is 
5ef0: 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 66 61 6b  only used if fak
5f00: 65 20 63 68 72 6f 6f 74 20 69 73 20 65 6e 61 62  e chroot is enab
5f10: 6c 65 64 2c 20 63 6f 6e 66 75 73 65 20 74 68 65  led, confuse the
5f20: 20 63 6f 6d 70 69 6c 65 72 20 2a 2f 0a 09 6f 70   compiler */..op
5f30: 74 69 6f 6e 73 20 3d 20 6f 70 74 69 6f 6e 73 3b  tions = options;
5f40: 0a 7d 0a 0a 2f 2a 20 50 72 6f 63 65 73 73 20 61  .}../* Process a
5f50: 6e 20 48 54 54 50 20 72 65 71 75 65 73 74 20 61  n HTTP request a
5f60: 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 70 61  nd return the pa
5f70: 74 68 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a  th requested */.
5f80: 73 74 61 74 69 63 20 73 74 72 75 63 74 20 66 69  static struct fi
5f90: 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 74  led_http_request
5fa0: 20 2a 66 69 6c 65 64 5f 67 65 74 5f 68 74 74 70   *filed_get_http
5fb0: 5f 72 65 71 75 65 73 74 28 46 49 4c 45 20 2a 66  _request(FILE *f
5fc0: 70 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f  p, struct filed_
5fd0: 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 62 75  http_request *bu
5fe0: 66 66 65 72 5f 73 74 2c 20 73 74 72 75 63 74 20  ffer_st, struct 
5ff0: 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f  filed_options *o
6000: 70 74 69 6f 6e 73 29 20 7b 0a 09 63 68 61 72 20  ptions) {..char 
6010: 2a 6d 65 74 68 6f 64 2c 20 2a 70 61 74 68 3b 0a  *method, *path;.
6020: 09 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 2a  .char *buffer, *
6030: 77 6f 72 6b 62 75 66 66 65 72 2c 20 2a 77 6f 72  workbuffer, *wor
6040: 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 09 63  kbuffer_next;..c
6050: 68 61 72 20 2a 66 67 65 74 73 5f 72 65 74 3b 0a  har *fgets_ret;.
6060: 09 73 69 7a 65 5f 74 20 62 75 66 66 65 72 5f 6c  .size_t buffer_l
6070: 65 6e 2c 20 70 61 74 68 5f 6c 65 6e 3b 0a 09 6f  en, path_len;..o
6080: 66 66 5f 74 20 72 61 6e 67 65 5f 73 74 61 72 74  ff_t range_start
6090: 2c 20 72 61 6e 67 65 5f 65 6e 64 2c 20 72 61 6e  , range_end, ran
60a0: 67 65 5f 6c 65 6e 67 74 68 3b 0a 09 69 6e 74 20  ge_length;..int 
60b0: 72 61 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a 09  range_request;..
60c0: 69 6e 74 20 73 6e 70 72 69 6e 74 66 5f 72 65 74  int snprintf_ret
60d0: 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 2f 2a 20 53  ;..int i;.../* S
60e0: 65 74 20 74 6f 20 64 65 66 61 75 6c 74 20 76 61  et to default va
60f0: 6c 75 65 73 20 2a 2f 0a 09 72 61 6e 67 65 5f 73  lues */..range_s
6100: 74 61 72 74 20 3d 20 30 3b 0a 09 72 61 6e 67 65  tart = 0;..range
6110: 5f 65 6e 64 20 20 20 3d 20 30 3b 0a 09 72 61 6e  _end   = 0;..ran
6120: 67 65 5f 72 65 71 75 65 73 74 20 3d 20 30 3b 0a  ge_request = 0;.
6130: 09 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d 20  .range_length = 
6140: 2d 31 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e  -1;..buffer_st->
6150: 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 65  headers.host.pre
6160: 73 65 6e 74 20 3d 20 30 3b 0a 09 62 75 66 66 65  sent = 0;..buffe
6170: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 63 6f  r_st->headers.co
6180: 6e 6e 65 63 74 69 6f 6e 20 3d 20 46 49 4c 45 44  nnection = FILED
6190: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53  _CONNECTION_CLOS
61a0: 45 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 62 75  E;...buffer = bu
61b0: 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b  ffer_st->tmpbuf;
61c0: 0a 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73  ..buffer_len = s
61d0: 69 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 74 2d  izeof(buffer_st-
61e0: 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 66 67 65 74  >tmpbuf);...fget
61f0: 73 5f 72 65 74 20 3d 20 66 67 65 74 73 28 62 75  s_ret = fgets(bu
6200: 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e  ffer, buffer_len
6210: 2c 20 66 70 29 3b 0a 09 69 66 20 28 66 67 65 74  , fp);..if (fget
6220: 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b  s_ret == NULL) {
6230: 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b  ...return(NULL);
6240: 0a 09 7d 0a 0a 09 6d 65 74 68 6f 64 20 3d 20 62  ..}...method = b
6250: 75 66 66 65 72 3b 0a 0a 09 62 75 66 66 65 72 20  uffer;...buffer 
6260: 3d 20 73 74 72 63 68 72 28 62 75 66 66 65 72 2c  = strchr(buffer,
6270: 20 27 20 27 29 3b 0a 09 69 66 20 28 62 75 66 66   ' ');..if (buff
6280: 65 72 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09  er == NULL) {...
6290: 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 7d  return(NULL);..}
62a0: 0a 0a 09 2a 62 75 66 66 65 72 20 3d 20 27 5c 30  ...*buffer = '\0
62b0: 27 3b 0a 09 62 75 66 66 65 72 2b 2b 3b 0a 0a 09  ';..buffer++;...
62c0: 70 61 74 68 20 3d 20 62 75 66 66 65 72 3b 0a 0a  path = buffer;..
62d0: 09 2f 2a 20 54 65 72 6d 69 6e 61 74 65 20 70 61  ./* Terminate pa
62e0: 74 68 20 63 6f 6d 70 6f 6e 65 6e 74 20 2a 2f 0a  th component */.
62f0: 09 62 75 66 66 65 72 20 3d 20 73 74 72 70 62 72  .buffer = strpbr
6300: 6b 28 70 61 74 68 2c 20 22 5c 72 5c 6e 20 22 29  k(path, "\r\n ")
6310: 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20 21 3d  ;..if (buffer !=
6320: 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 62 75 66 66   NULL) {...*buff
6330: 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 62 75 66  er = '\0';...buf
6340: 66 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 2f 2a 20 57  fer++;..}.../* W
6350: 65 20 6f 6e 6c 79 20 68 61 6e 64 6c 65 20 74 68  e only handle th
6360: 65 20 22 47 45 54 22 20 61 6e 64 20 22 48 45 41  e "GET" and "HEA
6370: 44 27 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 09 69  D' methods */..i
6380: 66 20 28 73 74 72 63 61 73 65 63 6d 70 28 6d 65  f (strcasecmp(me
6390: 74 68 6f 64 2c 20 22 68 65 61 64 22 29 20 21 3d  thod, "head") !=
63a0: 20 30 29 20 7b 0a 09 09 69 66 20 28 73 74 72 63   0) {...if (strc
63b0: 61 73 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 22  asecmp(method, "
63c0: 67 65 74 22 29 20 21 3d 20 30 29 20 7b 0a 09 09  get") != 0) {...
63d0: 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09  .return(NULL);..
63e0: 09 7d 0a 0a 09 09 2f 2a 20 47 45 54 20 72 65 71  .}..../* GET req
63f0: 75 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 72  uest */...buffer
6400: 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 49  _st->method = FI
6410: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48  LED_REQUEST_METH
6420: 4f 44 5f 47 45 54 3b 0a 09 7d 20 65 6c 73 65 20  OD_GET;..} else 
6430: 7b 0a 09 09 2f 2a 20 48 45 41 44 20 72 65 71 75  {.../* HEAD requ
6440: 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 72 5f  est */...buffer_
6450: 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 49 4c  st->method = FIL
6460: 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f  ED_REQUEST_METHO
6470: 44 5f 48 45 41 44 3b 0a 09 7d 0a 0a 09 2f 2a 20  D_HEAD;..}.../* 
6480: 4e 6f 74 65 20 70 61 74 68 20 2a 2f 0a 09 70 61  Note path */..pa
6490: 74 68 5f 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 28  th_len = strlen(
64a0: 70 61 74 68 29 3b 0a 09 6d 65 6d 63 70 79 28 62  path);..memcpy(b
64b0: 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c 20  uffer_st->path, 
64c0: 70 61 74 68 2c 20 70 61 74 68 5f 6c 65 6e 20 2b  path, path_len +
64d0: 20 31 29 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 6d   1);.../* Determ
64e0: 69 6e 65 20 74 79 70 65 20 6f 66 20 72 65 71 75  ine type of requ
64f0: 65 73 74 20 66 72 6f 6d 20 70 61 74 68 20 2a 2f  est from path */
6500: 0a 09 69 66 20 28 70 61 74 68 5f 6c 65 6e 20 3d  ..if (path_len =
6510: 3d 20 30 29 20 7b 0a 09 09 62 75 66 66 65 72 5f  = 0) {...buffer_
6520: 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44  st->type = FILED
6530: 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 49  _REQUEST_TYPE_DI
6540: 52 45 43 54 4f 52 59 3b 0a 09 7d 20 65 6c 73 65  RECTORY;..} else
6550: 20 7b 0a 09 09 69 66 20 28 70 61 74 68 5b 70 61   {...if (path[pa
6560: 74 68 5f 6c 65 6e 20 2d 20 31 5d 20 3d 3d 20 27  th_len - 1] == '
6570: 2f 27 29 20 7b 0a 09 09 09 62 75 66 66 65 72 5f  /') {....buffer_
6580: 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44  st->type = FILED
6590: 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 49  _REQUEST_TYPE_DI
65a0: 52 45 43 54 4f 52 59 3b 0a 09 09 7d 20 65 6c 73  RECTORY;...} els
65b0: 65 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 74  e {....buffer_st
65c0: 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 52  ->type = FILED_R
65d0: 45 51 55 45 53 54 5f 54 59 50 45 5f 4f 54 48 45  EQUEST_TYPE_OTHE
65e0: 52 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 52  R;...}..}.../* R
65f0: 65 73 65 74 20 62 75 66 66 65 72 20 66 6f 72 20  eset buffer for 
6600: 6c 61 74 65 72 20 75 73 65 20 2a 2f 0a 09 62 75  later use */..bu
6610: 66 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74  ffer = buffer_st
6620: 2d 3e 74 6d 70 62 75 66 3b 0a 0a 09 66 6f 72 20  ->tmpbuf;...for 
6630: 28 69 20 3d 20 30 3b 20 69 20 3c 20 31 30 30 3b  (i = 0; i < 100;
6640: 20 69 2b 2b 29 20 7b 0a 09 09 66 67 65 74 73 5f   i++) {...fgets_
6650: 72 65 74 20 3d 20 66 67 65 74 73 28 62 75 66 66  ret = fgets(buff
6660: 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20  er, buffer_len, 
6670: 66 70 29 3b 0a 09 09 69 66 20 28 66 67 65 74 73  fp);...if (fgets
6680: 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  _ret == NULL) {.
6690: 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09  ...break;...}...
66a0: 09 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70  .if (strncasecmp
66b0: 28 62 75 66 66 65 72 2c 20 22 52 61 6e 67 65 3a  (buffer, "Range:
66c0: 20 22 2c 20 37 29 20 3d 3d 20 30 29 20 7b 0a 09   ", 7) == 0) {..
66d0: 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 62  ..workbuffer = b
66e0: 75 66 66 65 72 20 2b 20 37 3b 0a 0a 09 09 09 69  uffer + 7;.....i
66f0: 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 77  f (strncasecmp(w
6700: 6f 72 6b 62 75 66 66 65 72 2c 20 22 62 79 74 65  orkbuffer, "byte
6710: 73 3d 22 2c 20 36 29 20 3d 3d 20 30 29 20 7b 0a  s=", 6) == 0) {.
6720: 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 2b  ....workbuffer +
6730: 3d 20 36 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f  = 6;......range_
6740: 72 65 71 75 65 73 74 20 3d 20 31 3b 0a 0a 09 09  request = 1;....
6750: 09 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d 20  ..range_start = 
6760: 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66  strtoull(workbuf
6770: 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 72  fer, &workbuffer
6780: 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 0a 09 09 09  _next, 10);.....
6790: 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 77 6f  .workbuffer = wo
67a0: 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 0a  rkbuffer_next;..
67b0: 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 66  ....if (*workbuf
67c0: 66 65 72 20 3d 3d 20 27 2d 27 29 20 7b 0a 09 09  fer == '-') {...
67d0: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b  ...workbuffer++;
67e0: 0a 0a 09 09 09 09 09 69 66 20 28 2a 77 6f 72 6b  .......if (*work
67f0: 62 75 66 66 65 72 20 21 3d 20 27 5c 72 27 20 26  buffer != '\r' &
6800: 26 20 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 3d  & *workbuffer !=
6810: 20 27 5c 6e 27 29 20 7b 0a 09 09 09 09 09 09 72   '\n') {.......r
6820: 61 6e 67 65 5f 65 6e 64 20 3d 20 73 74 72 74 6f  ange_end = strto
6830: 75 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c 20  ull(workbuffer, 
6840: 26 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74  &workbuffer_next
6850: 2c 20 31 30 29 3b 0a 09 09 09 09 09 7d 0a 09 09  , 10);......}...
6860: 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 20 65 6c 73  ..}....}...} els
6870: 65 20 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d  e if (strncasecm
6880: 70 28 62 75 66 66 65 72 2c 20 22 48 6f 73 74 3a  p(buffer, "Host:
6890: 20 22 2c 20 35 29 20 3d 3d 20 30 29 20 7b 0a 09   ", 5) == 0) {..
68a0: 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61  ..buffer_st->hea
68b0: 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e  ders.host.presen
68c0: 74 20 3d 20 31 3b 0a 0a 09 09 09 77 6f 72 6b 62  t = 1;.....workb
68d0: 75 66 66 65 72 20 3d 20 73 74 72 70 62 72 6b 28  uffer = strpbrk(
68e0: 62 75 66 66 65 72 20 2b 20 35 2c 20 22 5c 72 5c  buffer + 5, "\r\
68f0: 6e 3a 22 29 3b 0a 09 09 09 69 66 20 28 77 6f 72  n:");....if (wor
6900: 6b 62 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c 29  kbuffer != NULL)
6910: 20 7b 0a 09 09 09 09 2a 77 6f 72 6b 62 75 66 66   {.....*workbuff
6920: 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 09 7d 0a  er = '\0';....}.
6930: 0a 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d  ....workbuffer =
6940: 20 62 75 66 66 65 72 20 2b 20 35 3b 0a 09 09 09   buffer + 5;....
6950: 77 68 69 6c 65 20 28 2a 77 6f 72 6b 62 75 66 66  while (*workbuff
6960: 65 72 20 3d 3d 20 27 20 27 29 20 7b 0a 09 09 09  er == ' ') {....
6970: 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a 09  .workbuffer++;..
6980: 09 09 7d 0a 0a 09 09 09 73 74 72 63 70 79 28 62  ..}.....strcpy(b
6990: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72  uffer_st->header
69a0: 73 2e 68 6f 73 74 2e 68 6f 73 74 2c 20 77 6f 72  s.host.host, wor
69b0: 6b 62 75 66 66 65 72 29 3b 0a 09 09 7d 20 65 6c  kbuffer);...} el
69c0: 73 65 20 69 66 20 28 73 74 72 6e 63 61 73 65 63  se if (strncasec
69d0: 6d 70 28 62 75 66 66 65 72 2c 20 22 43 6f 6e 6e  mp(buffer, "Conn
69e0: 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69  ection: Keep-Ali
69f0: 76 65 22 2c 20 32 32 29 20 3d 3d 20 30 29 20 7b  ve", 22) == 0) {
6a00: 0a 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68  ....buffer_st->h
6a10: 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f  eaders.connectio
6a20: 6e 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43  n = FILED_CONNEC
6a30: 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 3b  TION_KEEP_ALIVE;
6a40: 0a 09 09 7d 0a 0a 09 09 69 66 20 28 6d 65 6d 63  ...}....if (memc
6a50: 6d 70 28 62 75 66 66 65 72 2c 20 22 5c 72 5c 6e  mp(buffer, "\r\n
6a60: 22 2c 20 32 29 20 3d 3d 20 30 29 20 7b 0a 09 09  ", 2) == 0) {...
6a70: 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a  .break;...}..}..
6a80: 09 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 72 61  ./* Determine ra
6a90: 6e 67 65 20 2a 2f 0a 09 69 66 20 28 72 61 6e 67  nge */..if (rang
6aa0: 65 5f 65 6e 64 20 21 3d 20 30 29 20 7b 0a 09 09  e_end != 0) {...
6ab0: 69 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 3c 3d  if (range_end <=
6ac0: 20 72 61 6e 67 65 5f 73 74 61 72 74 29 20 7b 0a   range_start) {.
6ad0: 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b  ...return(NULL);
6ae0: 0a 09 09 7d 0a 0a 09 09 72 61 6e 67 65 5f 6c 65  ...}....range_le
6af0: 6e 67 74 68 20 3d 20 72 61 6e 67 65 5f 65 6e 64  ngth = range_end
6b00: 20 2d 20 72 61 6e 67 65 5f 73 74 61 72 74 3b 0a   - range_start;.
6b10: 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  ...filed_log_msg
6b20: 5f 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 6e  _debug("Computin
6b30: 67 20 6c 65 6e 67 74 68 20 70 61 72 61 6d 65 74  g length paramet
6b40: 65 72 3a 20 25 6c 6c 75 20 3d 20 25 6c 6c 75 20  er: %llu = %llu 
6b50: 2d 20 25 6c 6c 75 22 2c 0a 09 09 09 28 75 6e 73  - %llu",....(uns
6b60: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
6b70: 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 2c 0a 09   range_length,..
6b80: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
6b90: 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 65 6e 64   long) range_end
6ba0: 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c  ,....(unsigned l
6bb0: 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f  ong long) range_
6bc0: 73 74 61 72 74 0a 09 09 29 3b 0a 09 7d 0a 0a 09  start...);..}...
6bd0: 2f 2a 20 46 69 6c 6c 20 75 70 20 73 74 72 75 63  /* Fill up struc
6be0: 74 75 72 65 20 74 6f 20 72 65 74 75 72 6e 20 2a  ture to return *
6bf0: 2f 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65  /..buffer_st->he
6c00: 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 73  aders.range.pres
6c10: 65 6e 74 20 3d 20 72 61 6e 67 65 5f 72 65 71 75  ent = range_requ
6c20: 65 73 74 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d  est;..buffer_st-
6c30: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f  >headers.range.o
6c40: 66 66 73 65 74 20 20 3d 20 72 61 6e 67 65 5f 73  ffset  = range_s
6c50: 74 61 72 74 3b 0a 09 62 75 66 66 65 72 5f 73 74  tart;..buffer_st
6c60: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
6c70: 6c 65 6e 67 74 68 20 20 3d 20 72 61 6e 67 65 5f  length  = range_
6c80: 6c 65 6e 67 74 68 3b 0a 0a 09 2f 2a 20 49 66 20  length;.../* If 
6c90: 76 68 6f 73 74 73 20 61 72 65 20 65 6e 61 62 6c  vhosts are enabl
6ca0: 65 64 2c 20 63 6f 6d 70 75 74 65 20 6e 65 77 20  ed, compute new 
6cb0: 70 61 74 68 20 2a 2f 0a 09 69 66 20 28 6f 70 74  path */..if (opt
6cc0: 69 6f 6e 73 2d 3e 76 68 6f 73 74 73 5f 65 6e 61  ions->vhosts_ena
6cd0: 62 6c 65 64 29 20 7b 0a 09 09 69 66 20 28 62 75  bled) {...if (bu
6ce0: 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73  ffer_st->headers
6cf0: 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d 3d  .host.present ==
6d00: 20 31 29 20 7b 0a 09 09 09 62 75 66 66 65 72 20   1) {....buffer 
6d10: 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70  = buffer_st->tmp
6d20: 62 75 66 3b 0a 09 09 09 62 75 66 66 65 72 5f 6c  buf;....buffer_l
6d30: 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 66  en = sizeof(buff
6d40: 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b 0a  er_st->tmpbuf);.
6d50: 0a 09 09 09 73 6e 70 72 69 6e 74 66 5f 72 65 74  ....snprintf_ret
6d60: 20 3d 20 73 6e 70 72 69 6e 74 66 28 62 75 66 66   = snprintf(buff
6d70: 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20  er, buffer_len, 
6d80: 22 2f 25 73 25 73 25 73 22 2c 0a 09 09 09 09 62  "/%s%s%s",.....b
6d90: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72  uffer_st->header
6da0: 73 2e 68 6f 73 74 2e 68 6f 73 74 2c 0a 09 09 09  s.host.host,....
6db0: 09 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68  .buffer_st->path
6dc0: 5b 30 5d 20 3d 3d 20 27 2f 27 20 3f 20 22 22 20  [0] == '/' ? "" 
6dd0: 3a 20 22 2f 22 2c 0a 09 09 09 09 62 75 66 66 65  : "/",.....buffe
6de0: 72 5f 73 74 2d 3e 70 61 74 68 0a 09 09 09 29 3b  r_st->path....);
6df0: 0a 09 09 09 69 66 20 28 73 6e 70 72 69 6e 74 66  ....if (snprintf
6e00: 5f 72 65 74 20 3e 3d 20 30 29 20 7b 0a 09 09 09  _ret >= 0) {....
6e10: 09 69 66 20 28 28 28 75 6e 73 69 67 6e 65 64 20  .if (((unsigned 
6e20: 69 6e 74 29 20 73 6e 70 72 69 6e 74 66 5f 72 65  int) snprintf_re
6e30: 74 29 20 3c 20 62 75 66 66 65 72 5f 6c 65 6e 29  t) < buffer_len)
6e40: 20 7b 0a 09 09 09 09 09 73 74 72 63 70 79 28 62   {......strcpy(b
6e50: 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c 20  uffer_st->path, 
6e60: 62 75 66 66 65 72 29 3b 0a 09 09 09 09 7d 0a 09  buffer);.....}..
6e70: 09 09 7d 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74  ..}...}..}...ret
6e80: 75 72 6e 28 62 75 66 66 65 72 5f 73 74 29 3b 0a  urn(buffer_st);.
6e90: 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20  }../* Return an 
6ea0: 65 72 72 6f 72 20 70 61 67 65 20 2a 2f 0a 73 74  error page */.st
6eb0: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f  atic void filed_
6ec0: 65 72 72 6f 72 5f 70 61 67 65 28 46 49 4c 45 20  error_page(FILE 
6ed0: 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *fp, const char 
6ee0: 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 69  *date_current, i
6ef0: 6e 74 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 2c  nt error_number,
6f00: 20 69 6e 74 20 6d 65 74 68 6f 64 2c 20 63 6f 6e   int method, con
6f10: 73 74 20 63 68 61 72 20 2a 72 65 61 73 6f 6e 2c  st char *reason,
6f20: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f   struct filed_lo
6f30: 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 29 20 7b 0a  g_entry *log) {.
6f40: 09 63 68 61 72 20 2a 65 72 72 6f 72 5f 73 74 72  .char *error_str
6f50: 69 6e 67 20 3d 20 22 3c 68 74 6d 6c 3e 3c 68 65  ing = "<html><he
6f60: 61 64 3e 3c 74 69 74 6c 65 3e 45 52 52 4f 52 3c  ad><title>ERROR<
6f70: 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 3e 3c 62  /title></head><b
6f80: 6f 64 79 3e 55 6e 61 62 6c 65 20 74 6f 20 70 72  ody>Unable to pr
6f90: 6f 63 65 73 73 20 72 65 71 75 65 73 74 3c 2f 62  ocess request</b
6fa0: 6f 64 79 3e 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a 09  ody></html>";...
6fb0: 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54 54  fprintf(fp, "HTT
6fc0: 50 2f 31 2e 31 20 25 69 20 4e 6f 74 20 4f 4b 5c  P/1.1 %i Not OK\
6fd0: 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53  r\nDate: %s\r\nS
6fe0: 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e  erver: filed\r\n
6ff0: 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25  Last-Modified: %
7000: 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e  s\r\nContent-Len
7010: 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 43 6f 6e  gth: %llu\r\nCon
7020: 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c  tent-Type: %s\r\
7030: 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f  nConnection: clo
7040: 73 65 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 65  se\r\n\r\n",...e
7050: 72 72 6f 72 5f 6e 75 6d 62 65 72 2c 0a 09 09 64  rror_number,...d
7060: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64  ate_current,...d
7070: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 28  ate_current,...(
7080: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
7090: 6e 67 29 20 73 74 72 6c 65 6e 28 65 72 72 6f 72  ng) strlen(error
70a0: 5f 73 74 72 69 6e 67 29 2c 0a 09 09 22 74 65 78  _string),..."tex
70b0: 74 2f 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f 2a  t/html"..);.../*
70c0: 20 73 69 6c 65 6e 63 65 20 65 72 72 6f 72 20 73   silence error s
70d0: 74 72 69 6e 67 20 66 6f 72 20 48 45 41 44 20 72  tring for HEAD r
70e0: 65 71 75 65 73 74 73 20 2a 2f 0a 09 69 66 20 28  equests */..if (
70f0: 6d 65 74 68 6f 64 20 21 3d 20 46 49 4c 45 44 5f  method != FILED_
7100: 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48  REQUEST_METHOD_H
7110: 45 41 44 29 20 7b 0a 09 09 66 70 72 69 6e 74 66  EAD) {...fprintf
7120: 28 66 70 2c 20 22 25 73 22 2c 20 65 72 72 6f 72  (fp, "%s", error
7130: 5f 73 74 72 69 6e 67 29 3b 0a 09 7d 0a 0a 09 2f  _string);..}.../
7140: 2a 20 4c 6f 67 20 65 72 72 6f 72 20 2a 2f 0a 09  * Log error */..
7150: 2f 2a 2a 20 72 65 61 73 6f 6e 20 6d 75 73 74 20  /** reason must 
7160: 70 6f 69 6e 74 20 74 6f 20 61 20 67 6c 6f 62 61  point to a globa
7170: 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 76 61  lly allocated va
7180: 6c 75 65 20 2a 2a 2f 0a 09 6c 6f 67 2d 3e 72 65  lue **/..log->re
7190: 61 73 6f 6e 20 3d 20 72 65 61 73 6f 6e 3b 0a 09  ason = reason;..
71a0: 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d  log->http_code =
71b0: 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 3b 0a 0a   error_number;..
71c0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79  .filed_log_entry
71d0: 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f 73  (log);.../* Clos
71e0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
71f0: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d  .filed_sockettim
7200: 65 6f 75 74 5f 63 6c 6f 73 65 28 66 69 6c 65 6e  eout_close(filen
7210: 6f 28 66 70 29 2c 20 30 29 3b 0a 0a 09 66 63 6c  o(fp), 0);...fcl
7220: 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 74 75 72  ose(fp);...retur
7230: 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  n;.}../* Return 
7240: 61 20 72 65 64 69 72 65 63 74 20 74 6f 20 69 6e  a redirect to in
7250: 64 65 78 2e 68 74 6d 6c 20 2a 2f 0a 23 69 66 6e  dex.html */.#ifn
7260: 64 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 52  def FILED_DONT_R
7270: 45 44 49 52 45 43 54 5f 44 49 52 45 43 54 4f 52  EDIRECT_DIRECTOR
7280: 49 45 53 0a 73 74 61 74 69 63 20 76 6f 69 64 20  IES.static void 
7290: 66 69 6c 65 64 5f 72 65 64 69 72 65 63 74 5f 69  filed_redirect_i
72a0: 6e 64 65 78 28 46 49 4c 45 20 2a 66 70 2c 20 63  ndex(FILE *fp, c
72b0: 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 65 5f  onst char *date_
72c0: 63 75 72 72 65 6e 74 2c 20 63 6f 6e 73 74 20 63  current, const c
72d0: 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 75 63  har *path, struc
72e0: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72  t filed_log_entr
72f0: 79 20 2a 6c 6f 67 29 20 7b 0a 09 69 6e 74 20 68  y *log) {..int h
7300: 74 74 70 5f 63 6f 64 65 20 3d 20 33 30 32 3b 0a  ttp_code = 302;.
7310: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54  .fprintf(fp, "HT
7320: 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e  TP/1.1 %i OK\r\n
7330: 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76  Date: %s\r\nServ
7340: 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73  er: filed\r\nLas
7350: 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c 72  t-Modified: %s\r
7360: 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68  \nContent-Length
7370: 3a 20 30 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f  : 0\r\nConnectio
7380: 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 4c 6f 63 61  n: close\r\nLoca
7390: 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e 5c 72 5c 6e  tion: %s\r\n\r\n
73a0: 22 2c 0a 09 09 68 74 74 70 5f 63 6f 64 65 2c 0a  ",...http_code,.
73b0: 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a  ..date_current,.
73c0: 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a  ..date_current,.
73d0: 09 09 22 69 6e 64 65 78 2e 68 74 6d 6c 22 0a 09  .."index.html"..
73e0: 29 3b 0a 0a 09 2f 2a 20 4c 6f 67 20 72 65 64 69  );.../* Log redi
73f0: 72 65 63 74 20 2a 2f 0a 09 6c 6f 67 2d 3e 72 65  rect */..log->re
7400: 61 73 6f 6e 20 3d 20 22 72 65 64 69 72 65 63 74  ason = "redirect
7410: 22 3b 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f  ";..log->http_co
7420: 64 65 20 3d 20 68 74 74 70 5f 63 6f 64 65 3b 0a  de = http_code;.
7430: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72  ..filed_log_entr
7440: 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f  y(log);.../* Clo
7450: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
7460: 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  ..filed_socketti
7470: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 69 6c 65  meout_close(file
7480: 6e 6f 28 66 70 29 2c 20 30 29 3b 0a 0a 09 66 63  no(fp), 0);...fc
7490: 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 74 75  lose(fp);...retu
74a0: 72 6e 3b 0a 0a 09 2f 2a 20 43 75 72 72 65 6e 74  rn;.../* Current
74b0: 6c 79 20 75 6e 75 73 65 64 3a 20 70 61 74 68 20  ly unused: path 
74c0: 2a 2f 0a 09 70 61 74 68 20 3d 20 70 61 74 68 3b  */..path = path;
74d0: 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 43 6f  .}.#endif../* Co
74e0: 6e 76 65 72 74 20 61 6e 20 65 6e 75 6d 20 72 65  nvert an enum re
74f0: 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22  presenting the "
7500: 43 6f 6e 6e 65 63 74 69 6f 6e 22 20 68 65 61 64  Connection" head
7510: 65 72 20 76 61 6c 75 65 20 74 6f 20 61 20 73 74  er value to a st
7520: 72 69 6e 67 20 2a 2f 0a 73 74 61 74 69 63 20 63  ring */.static c
7530: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64  onst char *filed
7540: 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 28  _connection_str(
7550: 69 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76  int connection_v
7560: 61 6c 75 65 29 20 7b 0a 09 73 77 69 74 63 68 20  alue) {..switch 
7570: 28 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c 75  (connection_valu
7580: 65 29 20 7b 0a 09 09 63 61 73 65 20 46 49 4c 45  e) {...case FILE
7590: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f  D_CONNECTION_CLO
75a0: 53 45 3a 0a 09 09 09 72 65 74 75 72 6e 28 22 63  SE:....return("c
75b0: 6c 6f 73 65 22 29 3b 0a 09 09 63 61 73 65 20 46  lose");...case F
75c0: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f  ILED_CONNECTION_
75d0: 4b 45 45 50 5f 41 4c 49 56 45 3a 0a 09 09 09 72  KEEP_ALIVE:....r
75e0: 65 74 75 72 6e 28 22 6b 65 65 70 2d 61 6c 69 76  eturn("keep-aliv
75f0: 65 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e  e");..}...return
7600: 28 22 63 6c 6f 73 65 22 29 3b 0a 7d 0a 0a 2f 2a  ("close");.}../*
7610: 20 48 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c 65   Handle a single
7620: 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 61 20   request from a 
7630: 63 6c 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 63  client */.static
7640: 20 69 6e 74 20 66 69 6c 65 64 5f 68 61 6e 64 6c   int filed_handl
7650: 65 5f 63 6c 69 65 6e 74 28 69 6e 74 20 66 64 2c  e_client(int fd,
7660: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74   struct filed_ht
7670: 74 70 5f 72 65 71 75 65 73 74 20 2a 72 65 71 75  tp_request *requ
7680: 65 73 74 2c 20 73 74 72 75 63 74 20 66 69 6c 65  est, struct file
7690: 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67  d_log_entry *log
76a0: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f  , struct filed_o
76b0: 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29  ptions *options)
76c0: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64   {..struct filed
76d0: 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 69  _fileinfo *filei
76e0: 6e 66 6f 3b 0a 09 73 73 69 7a 65 5f 74 20 73 65  nfo;..ssize_t se
76f0: 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 73 69 7a  ndfile_ret;..siz
7700: 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f 73 69 7a  e_t sendfile_siz
7710: 65 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 64 66 69  e;..off_t sendfi
7720: 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 66  le_offset, sendf
7730: 69 6c 65 5f 73 65 6e 74 2c 20 73 65 6e 64 66 69  ile_sent, sendfi
7740: 6c 65 5f 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 70  le_len;..char *p
7750: 61 74 68 3b 0a 09 63 68 61 72 20 2a 64 61 74 65  ath;..char *date
7760: 5f 63 75 72 72 65 6e 74 2c 20 64 61 74 65 5f 63  _current, date_c
7770: 75 72 72 65 6e 74 5f 62 5b 36 34 5d 3b 0a 09 69  urrent_b[64];..i
7780: 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 46  nt http_code;..F
7790: 49 4c 45 20 2a 66 70 3b 0a 0a 09 2f 2a 20 49 6e  ILE *fp;.../* In
77a0: 64 69 63 61 74 65 20 74 68 65 20 63 6f 6e 6e 65  dicate the conne
77b0: 63 74 69 6f 6e 20 73 74 61 72 74 20 74 69 6d 65  ction start time
77c0: 20 2a 2f 0a 09 6c 6f 67 2d 3e 63 6f 6e 6e 65 63   */..log->connec
77d0: 74 74 69 6d 65 20 3d 20 74 69 6d 65 28 4e 55 4c  ttime = time(NUL
77e0: 4c 29 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69  L);.../* Determi
77f0: 6e 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  ne current time 
7800: 2a 2f 0a 09 64 61 74 65 5f 63 75 72 72 65 6e 74  */..date_current
7810: 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d 61 74 5f   = filed_format_
7820: 74 69 6d 65 28 64 61 74 65 5f 63 75 72 72 65 6e  time(date_curren
7830: 74 5f 62 2c 20 73 69 7a 65 6f 66 28 64 61 74 65  t_b, sizeof(date
7840: 5f 63 75 72 72 65 6e 74 5f 62 29 2c 20 74 69 6d  _current_b), tim
7850: 65 28 4e 55 4c 4c 29 29 3b 0a 0a 09 2f 2a 20 4f  e(NULL));.../* O
7860: 70 65 6e 20 73 6f 63 6b 65 74 20 61 73 20 41 4e  pen socket as AN
7870: 53 49 20 49 2f 4f 20 66 6f 72 20 65 61 73 65 20  SI I/O for ease 
7880: 6f 66 20 75 73 65 20 2a 2f 0a 09 66 70 20 3d 20  of use */..fp = 
7890: 66 64 6f 70 65 6e 28 66 64 2c 20 22 77 2b 62 22  fdopen(fd, "w+b"
78a0: 29 3b 0a 09 69 66 20 28 66 70 20 3d 3d 20 4e 55  );..if (fp == NU
78b0: 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 73 6f  LL) {...filed_so
78c0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73  ckettimeout_clos
78d0: 65 28 66 64 2c 20 30 29 3b 0a 0a 09 09 63 6c 6f  e(fd, 0);....clo
78e0: 73 65 28 66 64 29 3b 0a 0a 09 09 6c 6f 67 2d 3e  se(fd);....log->
78f0: 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 27  buffer[0] = '\0'
7900: 3b 0a 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f  ;...log->http_co
7910: 64 65 20 3d 20 2d 31 3b 0a 09 09 6c 6f 67 2d 3e  de = -1;...log->
7920: 72 65 61 73 6f 6e 20 3d 20 22 66 64 6f 70 65 6e  reason = "fdopen
7930: 5f 66 61 69 6c 65 64 22 3b 0a 0a 09 09 66 69 6c  _failed";....fil
7940: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67  ed_log_entry(log
7950: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c  );....return(FIL
7960: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c  ED_CONNECTION_CL
7970: 4f 53 45 29 3b 0a 09 7d 0a 0a 09 72 65 71 75 65  OSE);..}...reque
7980: 73 74 20 3d 20 66 69 6c 65 64 5f 67 65 74 5f 68  st = filed_get_h
7990: 74 74 70 5f 72 65 71 75 65 73 74 28 66 70 2c 20  ttp_request(fp, 
79a0: 72 65 71 75 65 73 74 2c 20 6f 70 74 69 6f 6e 73  request, options
79b0: 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74  );...if (request
79c0: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 6c 6f   == NULL) {...lo
79d0: 67 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27  g->buffer[0] = '
79e0: 5c 30 27 3b 0a 0a 09 09 66 69 6c 65 64 5f 65 72  \0';....filed_er
79f0: 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74  ror_page(fp, dat
7a00: 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 30 2c 20  e_current, 500, 
7a10: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45  FILED_REQUEST_ME
7a20: 54 48 4f 44 5f 47 45 54 2c 20 22 66 6f 72 6d 61  THOD_GET, "forma
7a30: 74 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74  t", log);....ret
7a40: 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43  urn(FILED_CONNEC
7a50: 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a  TION_CLOSE);..}.
7a60: 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  ..filed_socketti
7a70: 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e 67  meout_processing
7a80: 5f 73 74 61 72 74 28 66 64 29 3b 0a 0a 09 70 61  _start(fd);...pa
7a90: 74 68 20 3d 20 72 65 71 75 65 73 74 2d 3e 70 61  th = request->pa
7aa0: 74 68 3b 0a 09 73 74 72 63 70 79 28 6c 6f 67 2d  th;..strcpy(log-
7ab0: 3e 62 75 66 66 65 72 2c 20 70 61 74 68 29 3b 0a  >buffer, path);.
7ac0: 09 6c 6f 67 2d 3e 6d 65 74 68 6f 64 20 3d 20 72  .log->method = r
7ad0: 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 3b 0a  equest->method;.
7ae0: 0a 09 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75  ../* If the requ
7af0: 65 73 74 65 64 20 70 61 74 68 20 69 73 20 61 20  ested path is a 
7b00: 64 69 72 65 63 74 6f 72 79 2c 20 72 65 64 69 72  directory, redir
7b10: 65 63 74 20 74 6f 20 69 6e 64 65 78 20 70 61 67  ect to index pag
7b20: 65 20 2a 2f 0a 09 69 66 20 28 72 65 71 75 65 73  e */..if (reques
7b30: 74 2d 3e 74 79 70 65 20 3d 3d 20 46 49 4c 45 44  t->type == FILED
7b40: 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 49  _REQUEST_TYPE_DI
7b50: 52 45 43 54 4f 52 59 29 20 7b 0a 23 69 66 64 65  RECTORY) {.#ifde
7b60: 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 52 45 44  f FILED_DONT_RED
7b70: 49 52 45 43 54 5f 44 49 52 45 43 54 4f 52 49 45  IRECT_DIRECTORIE
7b80: 53 0a 09 09 63 68 61 72 20 6c 6f 63 61 6c 70 61  S...char localpa
7b90: 74 68 5b 38 31 39 32 5d 3b 0a 09 09 69 6e 74 20  th[8192];...int 
7ba0: 73 6e 70 72 69 6e 74 66 5f 72 65 74 3b 0a 0a 09  snprintf_ret;...
7bb0: 09 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3d 20  .snprintf_ret = 
7bc0: 73 6e 70 72 69 6e 74 66 28 6c 6f 63 61 6c 70 61  snprintf(localpa
7bd0: 74 68 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 61 6c  th, sizeof(local
7be0: 70 61 74 68 29 2c 20 22 25 73 2f 69 6e 64 65 78  path), "%s/index
7bf0: 2e 68 74 6d 6c 22 2c 20 70 61 74 68 29 3b 0a 0a  .html", path);..
7c00: 09 09 69 66 20 28 73 6e 70 72 69 6e 74 66 5f 72  ..if (snprintf_r
7c10: 65 74 20 3c 3d 20 30 20 7c 7c 20 73 6e 70 72 69  et <= 0 || snpri
7c20: 6e 74 66 5f 72 65 74 20 3e 20 28 73 69 7a 65 6f  ntf_ret > (sizeo
7c30: 66 28 6c 6f 63 61 6c 70 61 74 68 29 20 2d 20 31  f(localpath) - 1
7c40: 29 29 20 7b 0a 09 09 09 66 69 6c 65 64 5f 65 72  )) {....filed_er
7c50: 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74  ror_page(fp, dat
7c60: 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 30 2c 20  e_current, 500, 
7c70: 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c  request->method,
7c80: 20 22 70 61 74 68 5f 66 6f 72 6d 61 74 22 2c 20   "path_format", 
7c90: 6c 6f 67 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e  log);.....return
7ca0: 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f  (FILED_CONNECTIO
7cb0: 4e 5f 43 4c 4f 53 45 29 3b 0a 09 09 7d 0a 0a 09  N_CLOSE);...}...
7cc0: 09 70 61 74 68 20 3d 20 6c 6f 63 61 6c 70 61 74  .path = localpat
7cd0: 68 3b 0a 23 65 6c 73 65 0a 09 09 66 69 6c 65 64  h;.#else...filed
7ce0: 5f 72 65 64 69 72 65 63 74 5f 69 6e 64 65 78 28  _redirect_index(
7cf0: 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74  fp, date_current
7d00: 2c 20 70 61 74 68 2c 20 6c 6f 67 29 3b 0a 0a 09  , path, log);...
7d10: 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f  .return(FILED_CO
7d20: 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b  NNECTION_CLOSE);
7d30: 0a 23 65 6e 64 69 66 0a 09 7d 0a 0a 09 66 69 6c  .#endif..}...fil
7d40: 65 69 6e 66 6f 20 3d 20 66 69 6c 65 64 5f 6f 70  einfo = filed_op
7d50: 65 6e 5f 66 69 6c 65 28 70 61 74 68 2c 20 26 72  en_file(path, &r
7d60: 65 71 75 65 73 74 2d 3e 66 69 6c 65 69 6e 66 6f  equest->fileinfo
7d70: 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a 09 69 66 20  , options);..if 
7d80: 28 66 69 6c 65 69 6e 66 6f 20 3d 3d 20 4e 55 4c  (fileinfo == NUL
7d90: 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65 72 72  L) {...filed_err
7da0: 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65  or_page(fp, date
7db0: 5f 63 75 72 72 65 6e 74 2c 20 34 30 34 2c 20 72  _current, 404, r
7dc0: 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c 20  equest->method, 
7dd0: 22 6f 70 65 6e 5f 66 61 69 6c 65 64 22 2c 20 6c  "open_failed", l
7de0: 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46  og);....return(F
7df0: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f  ILED_CONNECTION_
7e00: 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 69 66 20  CLOSE);..}...if 
7e10: 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72  (request->header
7e20: 73 2e 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 29  s.range.present)
7e30: 20 7b 0a 09 09 69 66 20 28 72 65 71 75 65 73 74   {...if (request
7e40: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
7e50: 6f 66 66 73 65 74 20 21 3d 20 30 20 7c 7c 20 72  offset != 0 || r
7e60: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
7e70: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3e 3d 20  range.length >= 
7e80: 30 29 20 7b 0a 09 09 09 69 66 20 28 72 65 71 75  0) {....if (requ
7e90: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
7ea0: 67 65 2e 6f 66 66 73 65 74 20 3e 3d 20 66 69 6c  ge.offset >= fil
7eb0: 65 69 6e 66 6f 2d 3e 6c 65 6e 29 20 7b 0a 09 09  einfo->len) {...
7ec0: 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61  ..filed_error_pa
7ed0: 67 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72  ge(fp, date_curr
7ee0: 65 6e 74 2c 20 34 31 36 2c 20 72 65 71 75 65 73  ent, 416, reques
7ef0: 74 2d 3e 6d 65 74 68 6f 64 2c 20 22 72 61 6e 67  t->method, "rang
7f00: 65 5f 69 6e 76 61 6c 69 64 22 2c 20 6c 6f 67 29  e_invalid", log)
7f10: 3b 0a 0a 09 09 09 09 63 6c 6f 73 65 28 66 69 6c  ;......close(fil
7f20: 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 09 09  einfo->fd);.....
7f30: 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f  .return(FILED_CO
7f40: 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b  NNECTION_CLOSE);
7f50: 0a 09 09 09 7d 0a 0a 09 09 09 69 66 20 28 72 65  ....}.....if (re
7f60: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
7f70: 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 3d 20 28  ange.length == (
7f80: 28 6f 66 66 5f 74 29 20 2d 31 29 29 20 7b 0a 09  (off_t) -1)) {..
7f90: 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  ...filed_log_msg
7fa0: 5f 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 6e  _debug("Computin
7fb0: 67 20 6c 65 6e 67 74 68 20 74 6f 20 66 69 74 20  g length to fit 
7fc0: 69 6e 20 62 6f 75 6e 64 73 3a 20 66 69 6c 65 69  in bounds: filei
7fd0: 6e 66 6f 2d 3e 6c 65 6e 20 3d 20 25 6c 6c 75 2c  nfo->len = %llu,
7fe0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72   request->header
7ff0: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d  s.range.offset =
8000: 20 25 6c 6c 75 22 2c 0a 09 09 09 09 09 28 75 6e   %llu",......(un
8010: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
8020: 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 2c  ) fileinfo->len,
8030: 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ......(unsigned 
8040: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65  long long) reque
8050: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
8060: 65 2e 6f 66 66 73 65 74 0a 09 09 09 09 29 3b 0a  e.offset.....);.
8070: 0a 09 09 09 09 72 65 71 75 65 73 74 2d 3e 68 65  .....request->he
8080: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
8090: 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c  th = fileinfo->l
80a0: 65 6e 20 2d 20 72 65 71 75 65 73 74 2d 3e 68 65  en - request->he
80b0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73  aders.range.offs
80c0: 65 74 3b 0a 09 09 09 7d 0a 0a 09 09 09 66 69 6c  et;....}.....fil
80d0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67  ed_log_msg_debug
80e0: 28 22 50 61 72 74 69 61 6c 20 72 65 71 75 65 73  ("Partial reques
80f0: 74 2c 20 73 74 61 72 74 69 6e 67 20 61 74 3a 20  t, starting at: 
8100: 25 6c 6c 75 20 61 6e 64 20 72 75 6e 6e 69 6e 67  %llu and running
8110: 20 66 6f 72 20 25 6c 6c 69 20 62 79 74 65 73 22   for %lli bytes"
8120: 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ,.....(unsigned 
8130: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65  long long) reque
8140: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
8150: 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 09 28 6c  e.offset,.....(l
8160: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73  ong long) reques
8170: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65  t->headers.range
8180: 2e 6c 65 6e 67 74 68 0a 09 09 09 29 3b 0a 0a 09  .length....);...
8190: 09 7d 0a 0a 09 09 68 74 74 70 5f 63 6f 64 65 20  .}....http_code 
81a0: 3d 20 32 30 36 3b 0a 09 7d 20 65 6c 73 65 20 7b  = 206;..} else {
81b0: 0a 09 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 32  ...http_code = 2
81c0: 30 30 3b 0a 0a 09 09 2f 2a 20 43 6f 6d 70 75 74  00;..../* Comput
81d0: 65 20 66 61 6b 65 20 72 61 6e 67 65 20 70 61 72  e fake range par
81e0: 61 6d 65 74 65 72 73 20 74 68 61 74 20 69 6e 63  ameters that inc
81f0: 6c 75 64 65 73 20 74 68 65 20 65 6e 74 69 72 65  ludes the entire
8200: 20 66 69 6c 65 20 2a 2f 0a 09 09 72 65 71 75 65   file */...reque
8210: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
8220: 65 2e 6f 66 66 73 65 74 20 3d 20 30 3b 0a 09 09  e.offset = 0;...
8230: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
8240: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20  .range.length = 
8250: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 09  fileinfo->len;..
8260: 7d 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20  }...fprintf(fp, 
8270: 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c  "HTTP/1.1 %i OK\
8280: 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53  r\nDate: %s\r\nS
8290: 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e  erver: filed\r\n
82a0: 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25  Last-Modified: %
82b0: 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e  s\r\nContent-Len
82c0: 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 41 63 63  gth: %llu\r\nAcc
82d0: 65 70 74 2d 52 61 6e 67 65 73 3a 20 62 79 74 65  ept-Ranges: byte
82e0: 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 70  s\r\nContent-Typ
82f0: 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74  e: %s\r\nConnect
8300: 69 6f 6e 3a 20 25 73 5c 72 5c 6e 45 54 61 67 3a  ion: %s\r\nETag:
8310: 20 5c 22 25 73 5c 22 5c 72 5c 6e 22 2c 0a 09 09   \"%s\"\r\n",...
8320: 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64 61 74  http_code,...dat
8330: 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 66 69 6c  e_current,...fil
8340: 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d 6f 64 2c 0a  einfo->lastmod,.
8350: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
8360: 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e   long) request->
8370: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65  headers.range.le
8380: 6e 67 74 68 2c 0a 09 09 66 69 6c 65 69 6e 66 6f  ngth,...fileinfo
8390: 2d 3e 74 79 70 65 2c 0a 09 09 66 69 6c 65 64 5f  ->type,...filed_
83a0: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 28 72  connection_str(r
83b0: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
83c0: 63 6f 6e 6e 65 63 74 69 6f 6e 29 2c 0a 09 09 66  connection),...f
83d0: 69 6c 65 69 6e 66 6f 2d 3e 65 74 61 67 0a 09 29  ileinfo->etag..)
83e0: 3b 0a 0a 09 69 66 20 28 68 74 74 70 5f 63 6f 64  ;...if (http_cod
83f0: 65 20 3d 3d 20 32 30 36 29 20 7b 0a 09 09 66 70  e == 206) {...fp
8400: 72 69 6e 74 66 28 66 70 2c 20 22 43 6f 6e 74 65  rintf(fp, "Conte
8410: 6e 74 2d 52 61 6e 67 65 3a 20 62 79 74 65 73 20  nt-Range: bytes 
8420: 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c 6c 75 5c 72  %llu-%llu/%llu\r
8430: 5c 6e 22 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65  \n",....(unsigne
8440: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71  d long long) req
8450: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  uest->headers.ra
8460: 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 28  nge.offset,....(
8470: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
8480: 6e 67 29 20 28 72 65 71 75 65 73 74 2d 3e 68 65  ng) (request->he
8490: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73  aders.range.offs
84a0: 65 74 20 2b 20 72 65 71 75 65 73 74 2d 3e 68 65  et + request->he
84b0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
84c0: 74 68 20 2d 20 31 29 2c 0a 09 09 09 28 75 6e 73  th - 1),....(uns
84d0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
84e0: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 0a 09   fileinfo->len..
84f0: 09 29 3b 0a 09 7d 0a 09 66 70 72 69 6e 74 66 28  .);..}..fprintf(
8500: 66 70 2c 20 22 5c 72 5c 6e 22 29 3b 0a 09 66 66  fp, "\r\n");..ff
8510: 6c 75 73 68 28 66 70 29 3b 0a 0a 09 6c 6f 67 2d  lush(fp);...log-
8520: 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 68 74 74  >http_code = htt
8530: 70 5f 63 6f 64 65 3b 0a 09 6c 6f 67 2d 3e 72 65  p_code;..log->re
8540: 61 73 6f 6e 20 3d 20 22 4f 4b 22 3b 0a 09 6c 6f  ason = "OK";..lo
8550: 67 2d 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 74  g->starttime = t
8560: 69 6d 65 28 4e 55 4c 4c 29 3b 0a 09 6c 6f 67 2d  ime(NULL);..log-
8570: 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 72 65  >req_offset = re
8580: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
8590: 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 6c 6f  ange.offset;..lo
85a0: 67 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20 3d 20  g->req_length = 
85b0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
85c0: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09  .range.length;..
85d0: 6c 6f 67 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 68  log->file_length
85e0: 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e   = fileinfo->len
85f0: 3b 0a 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f  ;..#ifdef FILED_
8600: 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 69  NONBLOCK_HTTP..i
8610: 6e 74 20 73 6f 63 6b 65 74 5f 66 6c 61 67 73 3b  nt socket_flags;
8620: 0a 09 66 64 5f 73 65 74 20 72 66 64 2c 20 77 66  ..fd_set rfd, wf
8630: 64 3b 0a 09 63 68 61 72 20 73 69 6e 6b 62 75 66  d;..char sinkbuf
8640: 5b 38 31 39 32 5d 3b 0a 09 73 73 69 7a 65 5f 74  [8192];..ssize_t
8650: 20 72 65 61 64 5f 72 65 74 3b 0a 0a 09 46 44 5f   read_ret;...FD_
8660: 5a 45 52 4f 28 26 72 66 64 29 3b 0a 09 46 44 5f  ZERO(&rfd);..FD_
8670: 5a 45 52 4f 28 26 77 66 64 29 3b 0a 09 46 44 5f  ZERO(&wfd);..FD_
8680: 53 45 54 28 66 64 2c 20 26 72 66 64 29 3b 0a 09  SET(fd, &rfd);..
8690: 46 44 5f 53 45 54 28 66 64 2c 20 26 77 66 64 29  FD_SET(fd, &wfd)
86a0: 3b 0a 0a 09 73 6f 63 6b 65 74 5f 66 6c 61 67 73  ;...socket_flags
86b0: 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47   = fcntl(fd, F_G
86c0: 45 54 46 4c 29 3b 0a 09 66 63 6e 74 6c 28 66 64  ETFL);..fcntl(fd
86d0: 2c 20 46 5f 53 45 54 46 4c 2c 20 73 6f 63 6b 65  , F_SETFL, socke
86e0: 74 5f 66 6c 61 67 73 20 7c 20 4f 5f 4e 4f 4e 42  t_flags | O_NONB
86f0: 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 0a 09 73  LOCK);.#endif..s
8700: 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 20 3d  endfile_offset =
8710: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72   request->header
8720: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a  s.range.offset;.
8730: 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 20  .sendfile_len = 
8740: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
8750: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09  .range.length;..
8760: 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 3d 20  sendfile_sent = 
8770: 30 3b 0a 09 77 68 69 6c 65 20 28 72 65 71 75 65  0;..while (reque
8780: 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 3d 20 46 49  st->method == FI
8790: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48  LED_REQUEST_METH
87a0: 4f 44 5f 47 45 54 29 20 7b 0a 09 09 69 66 20 28  OD_GET) {...if (
87b0: 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3e 20 46  sendfile_len > F
87c0: 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41  ILED_SENDFILE_MA
87d0: 58 29 20 7b 0a 09 09 09 73 65 6e 64 66 69 6c 65  X) {....sendfile
87e0: 5f 73 69 7a 65 20 3d 20 46 49 4c 45 44 5f 53 45  _size = FILED_SE
87f0: 4e 44 46 49 4c 45 5f 4d 41 58 3b 0a 09 09 7d 20  NDFILE_MAX;...} 
8800: 65 6c 73 65 20 7b 0a 09 09 09 73 65 6e 64 66 69  else {....sendfi
8810: 6c 65 5f 73 69 7a 65 20 3d 20 73 65 6e 64 66 69  le_size = sendfi
8820: 6c 65 5f 6c 65 6e 3b 0a 09 09 7d 0a 0a 09 09 73  le_len;...}....s
8830: 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20 73 65  endfile_ret = se
8840: 6e 64 66 69 6c 65 28 66 64 2c 20 66 69 6c 65 69  ndfile(fd, filei
8850: 6e 66 6f 2d 3e 66 64 2c 20 26 73 65 6e 64 66 69  nfo->fd, &sendfi
8860: 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 66  le_offset, sendf
8870: 69 6c 65 5f 73 69 7a 65 29 3b 0a 09 09 69 66 20  ile_size);...if 
8880: 28 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3c 3d  (sendfile_ret <=
8890: 20 30 29 20 7b 0a 23 69 66 64 65 66 20 46 49 4c   0) {.#ifdef FIL
88a0: 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50  ED_NONBLOCK_HTTP
88b0: 0a 09 09 09 69 66 20 28 65 72 72 6e 6f 20 3d 3d  ....if (errno ==
88c0: 20 45 41 47 41 49 4e 29 20 7b 0a 09 09 09 09 73   EAGAIN) {.....s
88d0: 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20 30 3b  endfile_ret = 0;
88e0: 0a 0a 09 09 09 09 77 68 69 6c 65 20 28 31 29 20  ......while (1) 
88f0: 7b 0a 09 09 09 09 09 73 65 6c 65 63 74 28 66 64  {......select(fd
8900: 20 2b 20 31 2c 20 26 72 66 64 2c 20 26 77 66 64   + 1, &rfd, &wfd
8910: 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 09  , NULL, NULL);..
8920: 09 09 09 09 69 66 20 28 46 44 5f 49 53 53 45 54  ....if (FD_ISSET
8930: 28 66 64 2c 20 26 72 66 64 29 29 20 7b 0a 09 09  (fd, &rfd)) {...
8940: 09 09 09 09 72 65 61 64 5f 72 65 74 20 3d 20 72  ....read_ret = r
8950: 65 61 64 28 66 64 2c 20 73 69 6e 6b 62 75 66 2c  ead(fd, sinkbuf,
8960: 20 73 69 7a 65 6f 66 28 73 69 6e 6b 62 75 66 29   sizeof(sinkbuf)
8970: 29 3b 0a 0a 09 09 09 09 09 09 69 66 20 28 72 65  );........if (re
8980: 61 64 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09  ad_ret <= 0) {..
8990: 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09  ......break;....
89a0: 09 09 09 7d 0a 09 09 09 09 09 7d 0a 0a 09 09 09  ...}......}.....
89b0: 09 09 69 66 20 28 46 44 5f 49 53 53 45 54 28 66  ..if (FD_ISSET(f
89c0: 64 2c 20 26 77 66 64 29 29 20 7b 0a 09 09 09 09  d, &wfd)) {.....
89d0: 09 09 72 65 61 64 5f 72 65 74 20 3d 20 31 3b 0a  ..read_ret = 1;.
89e0: 0a 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09  .......break;...
89f0: 09 09 09 7d 0a 09 09 09 09 7d 0a 0a 09 09 09 09  ...}.....}......
8a00: 69 66 20 28 72 65 61 64 5f 72 65 74 20 3c 3d 20  if (read_ret <= 
8a10: 30 29 20 7b 0a 09 09 09 09 09 62 72 65 61 6b 3b  0) {......break;
8a20: 0a 09 09 09 09 7d 0a 09 09 09 7d 20 65 6c 73 65  .....}....} else
8a30: 20 7b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09   {.....break;...
8a40: 09 7d 0a 23 65 6c 73 65 0a 09 09 09 62 72 65 61  .}.#else....brea
8a50: 6b 3b 0a 23 65 6e 64 69 66 0a 09 09 7d 0a 0a 09  k;.#endif...}...
8a60: 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 2d 3d  .sendfile_len -=
8a70: 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09   sendfile_ret;..
8a80: 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 2b  .sendfile_sent +
8a90: 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a  = sendfile_ret;.
8aa0: 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c  ..if (sendfile_l
8ab0: 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72  en == 0) {....br
8ac0: 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 6c 6f  eak;...}..}...lo
8ad0: 67 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 28 74 69  g->endtime = (ti
8ae0: 6d 65 5f 74 29 20 2d 31 3b 0a 09 6c 6f 67 2d 3e  me_t) -1;..log->
8af0: 73 65 6e 74 5f 6c 65 6e 67 74 68 20 3d 20 73 65  sent_length = se
8b00: 6e 64 66 69 6c 65 5f 73 65 6e 74 3b 0a 0a 09 66  ndfile_sent;...f
8b10: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c  iled_log_entry(l
8b20: 6f 67 29 3b 0a 0a 09 63 6c 6f 73 65 28 66 69 6c  og);...close(fil
8b30: 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 69 66  einfo->fd);...if
8b40: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65   (request->heade
8b50: 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 21 3d  rs.connection !=
8b60: 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f   FILED_CONNECTIO
8b70: 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 29 20 7b 0a  N_KEEP_ALIVE) {.
8b80: 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69  ..filed_socketti
8b90: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 64 2c 20  meout_close(fd, 
8ba0: 30 29 3b 0a 0a 09 09 66 63 6c 6f 73 65 28 66 70  0);....fclose(fp
8bb0: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c  );....return(FIL
8bc0: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c  ED_CONNECTION_CL
8bd0: 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64  OSE);..}...filed
8be0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70  _sockettimeout_p
8bf0: 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64 28 66 64  rocessing_end(fd
8c00: 29 3b 0a 0a 09 72 65 74 75 72 6e 28 46 49 4c 45  );...return(FILE
8c10: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45  D_CONNECTION_KEE
8c20: 50 5f 41 4c 49 56 45 29 3b 0a 7d 0a 0a 2f 2a 20  P_ALIVE);.}../* 
8c30: 48 61 6e 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 20  Handle incoming 
8c40: 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 73  connections */.s
8c50: 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 6c 65  tatic void *file
8c60: 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 28  d_worker_thread(
8c70: 76 6f 69 64 20 2a 61 72 67 5f 76 29 20 7b 0a 09  void *arg_v) {..
8c80: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 6f 72  struct filed_wor
8c90: 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 73 20  ker_thread_args 
8ca0: 2a 61 72 67 3b 0a 09 73 74 72 75 63 74 20 66 69  *arg;..struct fi
8cb0: 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 74  led_http_request
8cc0: 20 72 65 71 75 65 73 74 3b 0a 09 73 74 72 75 63   request;..struc
8cd0: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72  t filed_log_entr
8ce0: 79 20 2a 6c 6f 67 2c 20 6c 6f 63 61 6c 5f 64 75  y *log, local_du
8cf0: 6d 6d 79 5f 6c 6f 67 3b 0a 09 73 74 72 75 63 74  mmy_log;..struct
8d00: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a   filed_options *
8d10: 6f 70 74 69 6f 6e 73 3b 0a 09 73 74 72 75 63 74  options;..struct
8d20: 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 64   sockaddr_in6 ad
8d30: 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 61  dr;..socklen_t a
8d40: 64 64 72 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 69  ddrlen;..int fai
8d50: 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20 30 2c 20  lure_count = 0, 
8d60: 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e  max_failure_coun
8d70: 74 20 3d 20 46 49 4c 45 44 5f 4d 41 58 5f 46 41  t = FILED_MAX_FA
8d80: 49 4c 55 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 6e  ILURE_COUNT;..in
8d90: 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61  t connection_sta
8da0: 74 65 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45  te = FILED_CONNE
8db0: 43 54 49 4f 4e 5f 43 4c 4f 53 45 3b 0a 09 69 6e  CTION_CLOSE;..in
8dc0: 74 20 6d 61 73 74 65 72 5f 66 64 2c 20 66 64 20  t master_fd, fd 
8dd0: 3d 20 2d 31 3b 0a 0a 09 2f 2a 20 52 65 61 64 20  = -1;.../* Read 
8de0: 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 72  arguments */..ar
8df0: 67 20 3d 20 61 72 67 5f 76 3b 0a 0a 09 6d 61 73  g = arg_v;...mas
8e00: 74 65 72 5f 66 64 20 3d 20 61 72 67 2d 3e 66 64  ter_fd = arg->fd
8e10: 3b 0a 09 6f 70 74 69 6f 6e 73 20 3d 20 26 61 72  ;..options = &ar
8e20: 67 2d 3e 6f 70 74 69 6f 6e 73 3b 0a 0a 09 77 68  g->options;...wh
8e30: 69 6c 65 20 28 31 29 20 7b 0a 09 09 2f 2a 20 46  ile (1) {.../* F
8e40: 61 69 6c 75 72 65 20 6c 6f 6f 70 20 70 72 65 76  ailure loop prev
8e50: 65 6e 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 20 28  ention */...if (
8e60: 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3e 20  failure_count > 
8e70: 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e  max_failure_coun
8e80: 74 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09  t) {....break;..
8e90: 09 7d 0a 0a 09 09 2f 2a 20 41 6c 6c 6f 63 61 74  .}..../* Allocat
8ea0: 65 20 61 20 6e 65 77 20 6c 6f 67 20 62 75 66 66  e a new log buff
8eb0: 65 72 20 2a 2f 0a 09 09 6c 6f 67 20 3d 20 66 69  er */...log = fi
8ec0: 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 31 29 3b 0a  led_log_new(1);.
8ed0: 09 09 69 66 20 28 6c 6f 67 20 3d 3d 20 4e 55 4c  ..if (log == NUL
8ee0: 4c 29 20 7b 0a 09 09 09 66 69 6c 65 64 5f 6c 6f  L) {....filed_lo
8ef0: 67 5f 6d 73 67 28 22 41 4c 4c 4f 43 41 54 45 5f  g_msg("ALLOCATE_
8f00: 4c 4f 47 5f 4d 53 47 5f 46 41 49 4c 45 44 22 29  LOG_MSG_FAILED")
8f10: 3b 0a 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d  ;.....break;...}
8f20: 0a 0a 09 09 6c 6f 67 2d 3e 74 79 70 65 20 3d 20  ....log->type = 
8f30: 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54  FILED_LOG_TYPE_T
8f40: 52 41 4e 53 46 45 52 3b 0a 0a 09 09 2f 2a 20 49  RANSFER;..../* I
8f50: 66 20 77 65 20 63 6c 6f 73 65 64 20 74 68 65 20  f we closed the 
8f60: 6f 6c 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  old connection, 
8f70: 61 63 63 65 70 74 20 61 20 6e 65 77 20 6f 6e 65  accept a new one
8f80: 20 2a 2f 0a 09 09 69 66 20 28 63 6f 6e 6e 65 63   */...if (connec
8f90: 74 69 6f 6e 5f 73 74 61 74 65 20 3d 3d 20 46 49  tion_state == FI
8fa0: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43  LED_CONNECTION_C
8fb0: 4c 4f 53 45 29 20 7b 0a 09 09 09 2f 2a 20 41 63  LOSE) {..../* Ac
8fc0: 63 65 70 74 20 61 20 6e 65 77 20 63 6c 69 65 6e  cept a new clien
8fd0: 74 20 2a 2f 0a 09 09 09 61 64 64 72 6c 65 6e 20  t */....addrlen 
8fe0: 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 29 3b 0a  = sizeof(addr);.
8ff0: 0a 09 09 09 66 64 20 3d 20 61 63 63 65 70 74 28  ....fd = accept(
9000: 6d 61 73 74 65 72 5f 66 64 2c 20 28 73 74 72 75  master_fd, (stru
9010: 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26  ct sockaddr *) &
9020: 61 64 64 72 2c 20 26 61 64 64 72 6c 65 6e 29 3b  addr, &addrlen);
9030: 0a 09 09 7d 0a 0a 09 09 2f 2a 0a 09 09 20 2a 20  ...}..../*... * 
9040: 49 66 20 77 65 20 66 61 69 6c 2c 20 6d 61 6b 65  If we fail, make
9050: 20 61 20 6e 6f 74 65 20 6f 66 20 69 74 20 73 6f   a note of it so
9060: 20 77 65 20 64 6f 6e 27 74 20 67 6f 20 69 6e 74   we don't go int
9070: 6f 20 61 20 6c 6f 6f 70 20 6f 66 0a 09 09 20 2a  o a loop of... *
9080: 20 61 63 63 65 70 74 28 29 20 66 61 69 6c 69 6e   accept() failin
9090: 67 0a 09 09 20 2a 2f 0a 09 09 69 66 20 28 66 64  g... */...if (fd
90a0: 20 3c 20 30 29 20 7b 0a 09 09 09 2f 2a 20 4c 6f   < 0) {..../* Lo
90b0: 67 20 74 68 65 20 6e 65 77 20 63 6f 6e 6e 65 63  g the new connec
90c0: 74 69 6f 6e 20 2a 2f 0a 09 09 09 66 69 6c 65 64  tion */....filed
90d0: 5f 6c 6f 67 5f 6d 73 67 28 22 41 43 43 45 50 54  _log_msg("ACCEPT
90e0: 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 09 09 66  _FAILED");.....f
90f0: 61 69 6c 75 72 65 5f 63 6f 75 6e 74 2b 2b 3b 0a  ailure_count++;.
9100: 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 66 72  ....filed_log_fr
9110: 65 65 28 6c 6f 67 29 3b 0a 0a 09 09 09 63 6f 6e  ee(log);.....con
9120: 74 69 6e 75 65 3b 0a 09 09 7d 0a 0a 09 09 66 69  tinue;...}....fi
9130: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75  led_sockettimeou
9140: 74 5f 61 63 63 65 70 74 28 66 64 29 3b 0a 0a 09  t_accept(fd);...
9150: 09 2f 2a 20 46 69 6c 6c 20 69 6e 20 6c 6f 67 20  ./* Fill in log 
9160: 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 09 69  structure */...i
9170: 66 20 28 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28  f (filed_log_ip(
9180: 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72  (struct sockaddr
9190: 20 2a 29 20 26 61 64 64 72 2c 20 6c 6f 67 2d 3e   *) &addr, log->
91a0: 69 70 2c 20 73 69 7a 65 6f 66 28 6c 6f 67 2d 3e  ip, sizeof(log->
91b0: 69 70 29 29 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  ip)) == NULL) {.
91c0: 09 09 09 6c 6f 67 2d 3e 69 70 5b 30 5d 20 3d 20  ...log->ip[0] = 
91d0: 27 5c 30 27 3b 0a 09 09 09 6c 6f 67 2d 3e 70 6f  '\0';....log->po
91e0: 72 74 20 3d 20 30 3b 0a 09 09 7d 20 65 6c 73 65  rt = 0;...} else
91f0: 20 7b 0a 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 20   {....log->port 
9200: 3d 20 61 64 64 72 2e 73 69 6e 36 5f 70 6f 72 74  = addr.sin6_port
9210: 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 52 65 73 65  ;...}..../* Rese
9220: 74 20 66 61 69 6c 75 72 65 20 63 6f 75 6e 74 2a  t failure count*
9230: 2f 0a 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e  /...failure_coun
9240: 74 20 3d 20 30 3b 0a 0a 09 09 2f 2a 20 48 61 6e  t = 0;..../* Han
9250: 64 6c 65 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 09  dle socket */...
9260: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65  connection_state
9270: 20 3d 20 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f   = filed_handle_
9280: 63 6c 69 65 6e 74 28 66 64 2c 20 26 72 65 71 75  client(fd, &requ
9290: 65 73 74 2c 20 6c 6f 67 2c 20 6f 70 74 69 6f 6e  est, log, option
92a0: 73 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65 70 6f  s);..}.../* Repo
92b0: 72 74 20 65 72 72 6f 72 20 2a 2f 0a 09 66 69 6c  rt error */..fil
92c0: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 54 48 52 45  ed_log_msg("THRE
92d0: 41 44 5f 44 49 45 44 20 41 42 4e 4f 52 4d 41 4c  AD_DIED ABNORMAL
92e0: 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c  ");...return(NUL
92f0: 4c 29 3b 0a 0a 09 2f 2a 20 6c 6f 63 61 6c 5f 64  L);.../* local_d
9300: 75 6d 6d 79 5f 6c 6f 67 20 69 73 20 6f 6e 6c 79  ummy_log is only
9310: 20 75 73 65 64 20 69 66 20 46 49 4c 45 44 5f 44   used if FILED_D
9320: 4f 4e 54 5f 4c 4f 47 20 69 73 20 65 6e 61 62 6c  ONT_LOG is enabl
9330: 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74  ed, otherwise it
9340: 27 73 20 6e 6f 74 20 75 73 65 64 2c 20 62 75 74  's not used, but
9350: 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 68 61   the compiler ha
9360: 74 65 73 20 74 68 61 74 20 69 64 65 61 2e 20 2a  tes that idea. *
9370: 2f 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c  /..local_dummy_l
9380: 6f 67 2e 74 79 70 65 20 3d 20 30 3b 0a 09 6c 6f  og.type = 0;..lo
9390: 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79  cal_dummy_log.ty
93a0: 70 65 20 3d 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79  pe = local_dummy
93b0: 5f 6c 6f 67 2e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a  _log.type;.}../*
93c0: 20 43 72 65 61 74 65 20 77 6f 72 6b 65 72 20 74   Create worker t
93d0: 68 72 65 61 64 73 20 2a 2f 0a 73 74 61 74 69 63  hreads */.static
93e0: 20 69 6e 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65   int filed_worke
93f0: 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 28 69  r_threads_init(i
9400: 6e 74 20 66 64 2c 20 69 6e 74 20 74 68 72 65 61  nt fd, int threa
9410: 64 5f 63 6f 75 6e 74 2c 20 73 74 72 75 63 74 20  d_count, struct 
9420: 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f  filed_options *o
9430: 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 72 75 63  ptions) {..struc
9440: 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74  t filed_worker_t
9450: 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 3b  hread_args *arg;
9460: 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65  ..pthread_t thre
9470: 61 64 69 64 3b 0a 09 69 6e 74 20 70 74 68 72 65  adid;..int pthre
9480: 61 64 5f 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a  ad_ret;..int i;.
9490: 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 20  ..for (i = 0; i 
94a0: 3c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 3b 20  < thread_count; 
94b0: 69 2b 2b 29 20 7b 0a 09 09 61 72 67 20 3d 20 6d  i++) {...arg = m
94c0: 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72  alloc(sizeof(*ar
94d0: 67 29 29 3b 0a 0a 09 09 61 72 67 2d 3e 66 64 20  g));....arg->fd 
94e0: 3d 20 66 64 3b 0a 09 09 6d 65 6d 63 70 79 28 26  = fd;...memcpy(&
94f0: 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 2c 20 6f 70  arg->options, op
9500: 74 69 6f 6e 73 2c 20 73 69 7a 65 6f 66 28 2a 6f  tions, sizeof(*o
9510: 70 74 69 6f 6e 73 29 29 3b 0a 0a 09 09 70 74 68  ptions));....pth
9520: 72 65 61 64 5f 72 65 74 20 3d 20 70 74 68 72 65  read_ret = pthre
9530: 61 64 5f 63 72 65 61 74 65 28 26 74 68 72 65 61  ad_create(&threa
9540: 64 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64  did, NULL, filed
9550: 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 2c 20  _worker_thread, 
9560: 61 72 67 29 3b 0a 09 09 69 66 20 28 70 74 68 72  arg);...if (pthr
9570: 65 61 64 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a  ead_ret != 0) {.
9580: 09 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09  ...return(-1);..
9590: 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 30  .}..}...return(0
95a0: 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 73 70 6c 61 79  );.}../* Display
95b0: 20 68 65 6c 70 20 2a 2f 0a 73 74 61 74 69 63 20   help */.static 
95c0: 76 6f 69 64 20 66 69 6c 65 64 5f 70 72 69 6e 74  void filed_print
95d0: 5f 68 65 6c 70 28 46 49 4c 45 20 2a 6f 75 74 70  _help(FILE *outp
95e0: 75 74 2c 20 69 6e 74 20 6c 6f 6e 67 5f 68 65 6c  ut, int long_hel
95f0: 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 65  p, const char *e
9600: 78 74 72 61 29 20 7b 0a 09 69 66 20 28 65 78 74  xtra) {..if (ext
9610: 72 61 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 28  ra) {...fprintf(
9620: 6f 75 74 70 75 74 2c 20 22 25 73 5c 6e 22 2c 20  output, "%s\n", 
9630: 65 78 74 72 61 29 3b 0a 09 7d 0a 0a 09 66 70 72  extra);..}...fpr
9640: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 55 73  intf(output, "Us
9650: 61 67 65 3a 20 66 69 6c 65 64 20 5b 3c 6f 70 74  age: filed [<opt
9660: 69 6f 6e 73 3e 5d 5c 6e 22 29 3b 0a 09 66 70 72  ions>]\n");..fpr
9670: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
9680: 4f 70 74 69 6f 6e 73 3a 5c 6e 22 29 3b 0a 09 66  Options:\n");..f
9690: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
96a0: 20 20 20 20 20 20 2d 68 2c 20 2d 2d 68 65 6c 70        -h, --help
96b0: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f  \n");..fprintf(o
96c0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 64  utput, "      -d
96d0: 2c 20 2d 2d 64 61 65 6d 6f 6e 5c 6e 22 29 3b 0a  , --daemon\n");.
96e0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
96f0: 20 22 20 20 20 20 20 20 2d 76 2c 20 2d 2d 76 65   "      -v, --ve
9700: 72 73 69 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 69  rsion\n");..fpri
9710: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9720: 20 20 20 2d 56 2c 20 2d 2d 76 68 6f 73 74 5c 6e     -V, --vhost\n
9730: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  ");..fprintf(out
9740: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 62 20 3c  put, "      -b <
9750: 61 64 64 72 65 73 73 3e 2c 20 2d 2d 62 69 6e 64  address>, --bind
9760: 20 3c 61 64 64 72 65 73 73 3e 5c 6e 22 29 3b 0a   <address>\n");.
9770: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9780: 20 22 20 20 20 20 20 20 2d 70 20 3c 70 6f 72 74   "      -p <port
9790: 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70 6f 72 74 3e  >, --port <port>
97a0: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f  \n");..fprintf(o
97b0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 74  utput, "      -t
97c0: 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d 74 68 72 65   <count>, --thre
97d0: 61 64 73 20 3c 63 6f 75 6e 74 3e 5c 6e 22 29 3b  ads <count>\n");
97e0: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
97f0: 2c 20 22 20 20 20 20 20 20 2d 63 20 3c 65 6e 74  , "      -c <ent
9800: 72 69 65 73 3e 2c 20 2d 2d 63 61 63 68 65 20 3c  ries>, --cache <
9810: 65 6e 74 72 69 65 73 3e 5c 6e 22 29 3b 0a 09 66  entries>\n");..f
9820: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
9830: 20 20 20 20 20 20 2d 6c 20 3c 66 69 6c 65 3e 2c        -l <file>,
9840: 20 2d 2d 6c 6f 67 20 3c 66 69 6c 65 3e 5c 6e 22   --log <file>\n"
9850: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70  );..fprintf(outp
9860: 75 74 2c 20 22 20 20 20 20 20 20 2d 75 20 3c 75  ut, "      -u <u
9870: 73 65 72 3e 2c 20 2d 2d 75 73 65 72 20 3c 75 73  ser>, --user <us
9880: 65 72 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74  er>\n");..fprint
9890: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
98a0: 20 2d 72 20 3c 64 69 72 65 63 74 6f 72 79 3e 2c   -r <directory>,
98b0: 20 2d 2d 72 6f 6f 74 20 3c 64 69 72 65 63 74 6f   --root <directo
98c0: 72 79 3e 5c 6e 22 29 3b 0a 0a 09 69 66 20 28 6c  ry>\n");...if (l
98d0: 6f 6e 67 5f 68 65 6c 70 29 20 7b 0a 09 09 66 70  ong_help) {...fp
98e0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c  rintf(output, "\
98f0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
9900: 75 74 70 75 74 2c 20 22 20 20 55 73 61 67 65 3a  utput, "  Usage:
9910: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
9920: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
9930: 68 20 28 6f 72 20 2d 2d 68 65 6c 70 29 20 70 72  h (or --help) pr
9940: 69 6e 74 73 20 74 68 69 73 20 75 73 61 67 65 20  ints this usage 
9950: 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 5c 6e 22 29  information.\n")
9960: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
9970: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72  ut, "\n");...fpr
9980: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
9990: 20 20 20 20 2d 64 20 28 6f 72 20 2d 2d 64 61 65      -d (or --dae
99a0: 6d 6f 6e 29 20 69 6e 73 74 72 75 63 74 73 20 66  mon) instructs f
99b0: 69 6c 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 61  iled to become a
99c0: 20 64 61 65 6d 6f 6e 20 61 66 74 65 72 20 69 6e   daemon after in
99d0: 69 74 69 61 6c 69 7a 69 6e 67 5c 6e 22 29 3b 0a  itializing\n");.
99e0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
99f0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
9a00: 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 69            the li
9a10: 73 74 65 6e 69 6e 67 20 54 43 50 20 73 6f 63 6b  stening TCP sock
9a20: 65 74 20 61 6e 64 20 6c 6f 67 20 66 69 6c 65 73  et and log files
9a30: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  .\n");...fprintf
9a40: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a  (output, "\n");.
9a50: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9a60: 2c 20 22 20 20 20 20 20 20 2d 76 20 28 6f 72 20  , "      -v (or 
9a70: 2d 2d 76 65 72 73 69 6f 6e 29 20 69 6e 73 74 72  --version) instr
9a80: 75 63 74 73 20 66 69 6c 65 64 20 70 72 69 6e 74  ucts filed print
9a90: 20 6f 75 74 20 74 68 65 20 76 65 72 73 69 6f 6e   out the version
9aa0: 20 6e 75 6d 62 65 72 20 61 6e 64 20 65 78 69 74   number and exit
9ab0: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  .\n");...fprintf
9ac0: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a  (output, "\n");.
9ad0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
9ae0: 2c 20 22 20 20 20 20 20 20 2d 56 20 28 6f 72 20  , "      -V (or 
9af0: 2d 2d 76 68 6f 73 74 29 20 69 6e 73 74 72 75 63  --vhost) instruc
9b00: 74 73 20 66 69 6c 65 64 20 74 6f 20 70 72 65 70  ts filed to prep
9b10: 65 6e 64 20 61 6c 6c 20 72 65 71 75 65 73 74 73  end all requests
9b20: 20 77 69 74 68 20 74 68 65 69 72 20 48 54 54 50   with their HTTP
9b30: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
9b40: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
9b50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 48                 H
9b60: 6f 73 74 20 68 65 61 64 65 72 2e 5c 6e 22 29 3b  ost header.\n");
9b70: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
9b80: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69  t, "\n");...fpri
9b90: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9ba0: 20 20 20 2d 62 20 28 6f 72 20 2d 2d 62 69 6e 64     -b (or --bind
9bb0: 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  ) specifies the 
9bc0: 61 64 64 72 65 73 73 20 74 6f 20 6c 69 73 74 65  address to liste
9bd0: 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 48  n for incoming H
9be0: 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  TTP\n");...fprin
9bf0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
9c00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c10: 20 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 20 54   requests on.  T
9c20: 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
9c30: 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20   is \"%s\".\n", 
9c40: 42 49 4e 44 5f 41 44 44 52 29 3b 0a 09 09 66 70  BIND_ADDR);...fp
9c50: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c  rintf(output, "\
9c60: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
9c70: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 70  utput, "      -p
9c80: 20 28 6f 72 20 2d 2d 70 6f 72 74 29 20 73 70 65   (or --port) spe
9c90: 63 69 66 69 65 73 20 74 68 65 20 54 43 50 20 70  cifies the TCP p
9ca0: 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f 20 6c 69  ort number to li
9cb0: 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e  sten for incomin
9cc0: 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70  g HTTP\n");...fp
9cd0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9ce0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9cf0: 20 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e 2e      requests on.
9d00: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73    The default is
9d10: 20 25 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e   %u.\n", (unsign
9d20: 65 64 20 69 6e 74 29 20 50 4f 52 54 29 3b 0a 09  ed int) PORT);..
9d30: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9d40: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
9d50: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
9d60: 20 2d 74 20 28 6f 72 20 2d 2d 74 68 72 65 61 64   -t (or --thread
9d70: 73 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65  s) specifies the
9d80: 20 6e 75 6d 62 65 72 20 6f 66 20 77 6f 72 6b 65   number of worke
9d90: 72 20 74 68 72 65 61 64 73 20 74 6f 20 63 72 65  r threads to cre
9da0: 61 74 65 2e 20 45 61 63 68 5c 6e 22 29 3b 0a 09  ate. Each\n");..
9db0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
9dc0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
9dd0: 20 20 20 20 20 20 20 20 20 20 77 6f 72 6b 65 72            worker
9de0: 20 74 68 72 65 61 64 20 63 61 6e 20 73 65 72 76   thread can serv
9df0: 69 63 65 20 6f 6e 65 20 63 6f 6e 63 75 72 72 65  ice one concurre
9e00: 6e 74 20 48 54 54 50 20 73 65 73 73 69 6f 6e 2e  nt HTTP session.
9e10: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
9e20: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
9e30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9e40: 20 54 68 75 73 20 74 68 65 20 6e 75 6d 62 65 72   Thus the number
9e50: 20 6f 66 20 74 68 72 65 61 64 73 20 63 72 65 61   of threads crea
9e60: 74 65 64 20 77 69 6c 6c 20 64 65 74 65 72 6d 69  ted will determi
9e70: 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a 09 09 66 70  ne how\n");...fp
9e80: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
9e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ea0: 20 20 20 20 20 20 20 6d 61 6e 79 20 73 69 6d 75         many simu
9eb0: 6c 74 61 6e 65 6f 75 73 20 74 72 61 6e 73 66 65  ltaneous transfe
9ec0: 72 73 20 77 69 6c 6c 20 62 65 20 70 6f 73 73 69  rs will be possi
9ed0: 62 6c 65 2e 20 54 68 65 5c 6e 22 29 3b 0a 09 09  ble. The\n");...
9ee0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
9ef0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
9f00: 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74           default
9f10: 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e   is %lu.\n", (un
9f20: 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 54 48 52  signed long) THR
9f30: 45 41 44 5f 43 4f 55 4e 54 29 3b 0a 09 09 66 70  EAD_COUNT);...fp
9f40: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c  rintf(output, "\
9f50: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
9f60: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 63  utput, "      -c
9f70: 20 28 6f 72 20 2d 2d 63 61 63 68 65 29 20 73 70   (or --cache) sp
9f80: 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62  ecifies the numb
9f90: 65 72 20 6f 66 20 66 69 6c 65 20 69 6e 66 6f 72  er of file infor
9fa0: 6d 61 74 69 6f 6e 20 63 61 63 68 65 20 65 6e 74  mation cache ent
9fb0: 72 69 65 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69  ries\n");...fpri
9fc0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
9fd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fe0: 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2e 20     to allocate. 
9ff0: 20 45 61 63 68 20 63 61 63 68 65 20 65 6e 74 72   Each cache entr
a000: 79 20 68 6f 6c 64 73 20 66 69 6c 65 20 69 6e 66  y holds file inf
a010: 6f 72 6d 61 74 69 6f 6e 20 61 73 5c 6e 22 29 3b  ormation as\n");
a020: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
a030: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
a040: 20 20 20 20 20 20 20 20 20 20 77 65 6c 6c 20 61            well a
a050: 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 64  s an open file d
a060: 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74 68 65  escriptor to the
a070: 20 66 69 6c 65 2c 20 73 6f 20 72 65 73 6f 75 72   file, so resour
a080: 63 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74  ce\n");...fprint
a090: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
a0a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0b0: 20 6c 69 6d 69 74 73 20 28 69 2e 65 2e 2c 20 75   limits (i.e., u
a0c0: 6c 69 6d 69 74 29 20 73 68 6f 75 6c 64 20 62 65  limit) should be
a0d0: 20 63 6f 6e 73 69 64 65 72 65 64 2e 20 20 54 68   considered.  Th
a0e0: 69 73 20 73 68 6f 75 6c 64 5c 6e 22 29 3b 0a 09  is should\n");..
a0f0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
a100: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
a110: 20 20 20 20 20 20 20 20 62 65 20 61 20 70 72 69          be a pri
a120: 6d 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 69 64  me number for id
a130: 65 61 6c 20 75 73 65 20 77 69 74 68 20 74 68 65  eal use with the
a140: 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 6f 64 2e 5c   lookup method.\
a150: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
a160: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
a170: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
a180: 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 6c 75  e default is %lu
a190: 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 20  .\n", (unsigned 
a1a0: 6c 6f 6e 67 29 20 43 41 43 48 45 5f 53 49 5a 45  long) CACHE_SIZE
a1b0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
a1c0: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70  put, "\n");...fp
a1d0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
a1e0: 20 20 20 20 20 2d 6c 20 28 6f 72 20 2d 2d 6c 6f       -l (or --lo
a1f0: 67 29 20 73 70 65 63 69 66 69 65 73 20 61 20 66  g) specifies a f
a200: 69 6c 65 6e 61 6d 65 20 74 6f 20 6f 70 65 6e 20  ilename to open 
a210: 66 6f 72 20 77 72 69 74 69 6e 67 20 6c 6f 67 20  for writing log 
a220: 65 6e 74 72 69 65 73 2e 20 20 4c 6f 67 5c 6e 22  entries.  Log\n"
a230: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
a240: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20  put, "          
a250: 20 20 20 20 20 20 20 20 20 20 65 6e 74 72 69 65            entrie
a260: 73 20 61 72 65 20 6d 61 64 65 20 66 6f 72 20 76  s are made for v
a270: 61 72 69 6f 75 73 20 73 74 61 67 65 73 20 69 6e  arious stages in
a280: 20 74 72 61 6e 73 66 65 72 69 6e 67 20 66 69 6c   transfering fil
a290: 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  es.\n");...fprin
a2a0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
a2b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2c0: 54 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 73 20  The log file is 
a2d0: 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 73 77  opened before sw
a2e0: 69 74 63 68 69 6e 67 20 75 73 65 72 73 20 28 73  itching users (s
a2f0: 65 65 20 5c 22 2d 75 5c 22 29 5c 6e 22 29 3b 0a  ee \"-u\")\n");.
a300: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
a310: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
a320: 20 20 20 20 20 20 20 61 6e 64 20 72 6f 6f 74 20         and root 
a330: 64 69 72 65 63 74 6f 72 69 65 73 20 28 73 65 65  directories (see
a340: 20 5c 22 2d 72 5c 22 29 2e 20 20 54 68 65 20 6c   \"-r\").  The l
a350: 6f 67 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72  og file is never
a360: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
a370: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
a380: 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6c 6f               clo
a390: 73 65 64 20 73 6f 20 6c 6f 67 20 72 6f 74 61 74  sed so log rotat
a3a0: 69 6f 6e 20 77 69 74 68 6f 75 74 20 73 74 6f 70  ion without stop
a3b0: 70 69 6e 67 20 74 68 65 20 64 61 65 6d 6f 6e 20  ping the daemon 
a3c0: 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66  is will\n");...f
a3d0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
a3e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3f0: 20 20 20 20 6e 6f 74 20 77 6f 72 6b 2e 20 20 54      not work.  T
a400: 68 65 20 76 61 6c 75 65 20 6f 66 20 5c 22 2d 5c  he value of \"-\
a410: 22 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  " indicates that
a420: 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 75 74   standard output
a430: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
a440: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
a450: 20 20 20 20 20 20 20 20 20 20 20 20 20 73 68 6f               sho
a460: 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20  uld be used for 
a470: 6c 6f 67 67 69 6e 67 2e 20 20 49 66 20 74 68 65  logging.  If the
a480: 20 66 69 6c 65 6e 61 6d 65 20 62 65 67 69 6e 73   filename begins
a490: 20 77 69 74 68 20 61 5c 6e 22 29 3b 0a 09 09 66   with a\n");...f
a4a0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
a4b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4c0: 20 20 20 20 70 69 70 65 20 28 5c 22 7c 5c 22 29      pipe (\"|\")
a4d0: 20 74 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20   then a process 
a4e0: 69 73 20 73 74 61 72 74 65 64 20 61 6e 64 20 75  is started and u
a4f0: 73 65 64 20 66 6f 72 20 6c 6f 67 67 69 6e 67 5c  sed for logging\
a500: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
a510: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
a520: 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 73 74              inst
a530: 65 61 64 20 6f 66 20 61 20 66 69 6c 65 2e 20 20  ead of a file.  
a540: 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 5c  The default is \
a550: 22 25 73 5c 22 2e 5c 6e 22 2c 20 4c 4f 47 5f 46  "%s\".\n", LOG_F
a560: 49 4c 45 29 3b 0a 23 69 66 64 65 66 20 46 49 4c  ILE);.#ifdef FIL
a570: 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 09 09 66 70  ED_DONT_LOG...fp
a580: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
a590: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5a0: 20 20 20 4e 6f 74 65 20 74 68 61 74 20 6c 6f 67     Note that log
a5b0: 67 69 6e 67 20 69 73 20 63 6f 6d 70 6c 65 74 65  ging is complete
a5c0: 6c 79 20 64 69 73 61 62 6c 65 64 20 73 6f 20 74  ly disabled so t
a5d0: 68 69 73 20 6f 70 74 69 6f 6e 20 64 6f 65 73 5c  his option does\
a5e0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
a5f0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
a600: 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f 74 68              noth
a610: 69 6e 67 20 69 6e 20 74 68 69 73 20 62 75 69 6c  ing in this buil
a620: 64 2e 5c 6e 22 29 3b 0a 23 65 6e 64 69 66 0a 09  d.\n");.#endif..
a630: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
a640: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
a650: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
a660: 20 2d 75 20 28 6f 72 20 2d 2d 75 73 65 72 29 20   -u (or --user) 
a670: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 75 73  specifies the us
a680: 65 72 20 74 6f 20 73 77 69 74 63 68 20 75 73 65  er to switch use
a690: 72 20 49 44 73 20 74 6f 20 62 65 66 6f 72 65 20  r IDs to before 
a6a0: 73 65 72 76 69 63 69 6e 67 5c 6e 22 29 3b 0a 09  servicing\n");..
a6b0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
a6c0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
a6d0: 20 20 20 20 20 20 20 72 65 71 75 65 73 74 73 2e         requests.
a6e0: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73    The default is
a6f0: 20 6e 6f 74 20 63 68 61 6e 67 65 20 75 73 65 72   not change user
a700: 20 49 44 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72   IDs.\n");...fpr
a710: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e  intf(output, "\n
a720: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
a730: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 72 20  tput, "      -r 
a740: 28 6f 72 20 2d 2d 72 6f 6f 74 29 20 73 70 65 63  (or --root) spec
a750: 69 66 69 65 73 20 74 68 65 20 64 69 72 65 63 74  ifies the direct
a760: 6f 72 79 20 74 6f 20 61 63 74 20 61 73 20 74 68  ory to act as th
a770: 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 79  e root directory
a780: 20 66 6f 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69   for\n");...fpri
a790: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
a7a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7b0: 20 20 74 68 65 20 66 69 6c 65 20 73 65 72 76 65    the file serve
a7c0: 72 2e 20 20 49 66 20 74 68 69 73 20 6f 70 74 69  r.  If this opti
a7d0: 6f 6e 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  on is specified,
a7e0: 20 63 68 72 6f 6f 74 28 32 29 5c 6e 22 29 3b 0a   chroot(2)\n");.
a7f0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
a800: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
a810: 20 20 20 20 20 20 20 20 69 73 20 63 61 6c 6c 65          is calle
a820: 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  d.  The default 
a830: 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 72 6f  is not change ro
a840: 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73 2c 5c  ot directories,\
a850: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
a860: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
a870: 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 61               tha
a880: 74 20 69 73 2c 20 74 68 65 20 5c 22 2f 5c 22 20  t is, the \"/\" 
a890: 64 69 72 65 63 74 6f 72 79 20 69 73 20 73 68 61  directory is sha
a8a0: 72 65 64 20 6f 75 74 2e 20 20 54 68 69 73 20 77  red out.  This w
a8b0: 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  ill\n");...fprin
a8c0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
a8d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8e0: 20 6c 69 6b 65 6c 79 20 62 65 20 61 20 73 65 63   likely be a sec
a8f0: 75 72 69 74 79 20 69 73 73 75 65 2c 20 73 6f 20  urity issue, so 
a900: 74 68 69 73 20 6f 70 74 69 6f 6e 20 73 68 6f 75  this option shou
a910: 6c 64 20 61 6c 77 61 79 73 5c 6e 22 29 3b 0a 09  ld always\n");..
a920: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
a930: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
a940: 20 20 20 20 20 20 20 62 65 20 75 73 65 64 2e 5c         be used.\
a950: 6e 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e  n");..}...return
a960: 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 20 67 65  ;.}../* Add a ge
a970: 74 6f 70 74 20 6f 70 74 69 6f 6e 20 2a 2f 0a 73  topt option */.s
a980: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64  tatic void filed
a990: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74  _getopt_long_set
a9a0: 6f 70 74 28 73 74 72 75 63 74 20 6f 70 74 69 6f  opt(struct optio
a9b0: 6e 20 2a 6f 70 74 2c 20 63 6f 6e 73 74 20 63 68  n *opt, const ch
a9c0: 61 72 20 2a 6e 61 6d 65 2c 20 69 6e 74 20 68 61  ar *name, int ha
a9d0: 73 5f 61 72 67 2c 20 69 6e 74 20 76 61 6c 29 20  s_arg, int val) 
a9e0: 7b 0a 09 6f 70 74 2d 3e 6e 61 6d 65 20 20 20 20  {..opt->name    
a9f0: 20 3d 20 6e 61 6d 65 3b 0a 09 6f 70 74 2d 3e 68   = name;..opt->h
aa00: 61 73 5f 61 72 67 20 20 3d 20 68 61 73 5f 61 72  as_arg  = has_ar
aa10: 67 3b 0a 09 6f 70 74 2d 3e 66 6c 61 67 20 20 20  g;..opt->flag   
aa20: 20 20 3d 20 4e 55 4c 4c 3b 0a 09 6f 70 74 2d 3e    = NULL;..opt->
aa30: 76 61 6c 20 20 20 20 20 20 3d 20 76 61 6c 3b 0a  val      = val;.
aa40: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20  ..return;.}../* 
aa50: 52 65 73 6f 6c 76 65 20 61 20 75 73 65 72 6e 61  Resolve a userna
aa60: 6d 65 20 74 6f 20 61 20 55 49 44 20 2a 2f 0a 73  me to a UID */.s
aa70: 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f  tatic int filed_
aa80: 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 63 6f 6e 73  user_lookup(cons
aa90: 74 20 63 68 61 72 20 2a 75 73 65 72 2c 20 75 69  t char *user, ui
aaa0: 64 5f 74 20 2a 75 73 65 72 5f 69 64 29 20 7b 0a  d_t *user_id) {.
aab0: 09 63 68 61 72 20 2a 6e 65 78 74 3b 0a 09 75 69  .char *next;..ui
aac0: 64 5f 74 20 75 73 65 72 5f 69 64 5f 63 68 65 63  d_t user_id_chec
aad0: 6b 3b 0a 23 69 66 6e 64 65 66 20 46 49 4c 45 44  k;.#ifndef FILED
aae0: 5f 4e 4f 5f 47 45 54 50 57 4e 41 4d 0a 09 73 74  _NO_GETPWNAM..st
aaf0: 72 75 63 74 20 70 61 73 73 77 64 20 2a 65 6e 74  ruct passwd *ent
ab00: 3b 0a 0a 09 65 6e 74 20 3d 20 67 65 74 70 77 6e  ;...ent = getpwn
ab10: 61 6d 28 75 73 65 72 29 3b 0a 09 69 66 20 28 65  am(user);..if (e
ab20: 6e 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09  nt != NULL) {...
ab30: 2a 75 73 65 72 5f 69 64 20 3d 20 65 6e 74 2d 3e  *user_id = ent->
ab40: 70 77 5f 75 69 64 3b 0a 0a 09 09 72 65 74 75 72  pw_uid;....retur
ab50: 6e 28 30 29 3b 0a 09 7d 0a 23 65 6e 64 69 66 0a  n(0);..}.#endif.
ab60: 0a 09 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 20  ..user_id_check 
ab70: 3d 20 73 74 72 74 6f 75 6c 6c 28 75 73 65 72 2c  = strtoull(user,
ab80: 20 26 6e 65 78 74 2c 20 31 30 29 3b 0a 09 69 66   &next, 10);..if
ab90: 20 28 6e 65 78 74 20 3d 3d 20 4e 55 4c 4c 29 20   (next == NULL) 
aba0: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  {...return(1);..
abb0: 7d 0a 0a 09 69 66 20 28 6e 65 78 74 5b 30 5d 20  }...if (next[0] 
abc0: 21 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 65 74  != '\0') {...ret
abd0: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2a 75 73  urn(1);..}...*us
abe0: 65 72 5f 69 64 20 3d 20 75 73 65 72 5f 69 64 5f  er_id = user_id_
abf0: 63 68 65 63 6b 3b 0a 0a 09 72 65 74 75 72 6e 28  check;...return(
ac00: 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 61 65 6d 6f 6e  0);.}../* Daemon
ac10: 69 7a 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ize */.static in
ac20: 74 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a  t filed_daemoniz
ac30: 65 28 76 6f 69 64 29 20 7b 0a 09 70 69 64 5f 74  e(void) {..pid_t
ac40: 20 73 65 74 73 69 64 5f 72 65 74 2c 20 66 6f 72   setsid_ret, for
ac50: 6b 5f 72 65 74 3b 0a 09 69 6e 74 20 63 68 64 69  k_ret;..int chdi
ac60: 72 5f 72 65 74 2c 20 64 75 70 32 5f 72 65 74 3b  r_ret, dup2_ret;
ac70: 0a 09 69 6e 74 20 66 64 5f 69 6e 2c 20 66 64 5f  ..int fd_in, fd_
ac80: 6f 75 74 3b 0a 0a 09 63 68 64 69 72 5f 72 65 74  out;...chdir_ret
ac90: 20 3d 20 63 68 64 69 72 28 22 2f 22 29 3b 0a 09   = chdir("/");..
aca0: 69 66 20 28 63 68 64 69 72 5f 72 65 74 20 21 3d  if (chdir_ret !=
acb0: 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31   0) {...return(1
acc0: 29 3b 0a 09 7d 0a 0a 09 66 6f 72 6b 5f 72 65 74  );..}...fork_ret
acd0: 20 3d 20 66 6f 72 6b 28 29 3b 0a 09 69 66 20 28   = fork();..if (
ace0: 66 6f 72 6b 5f 72 65 74 20 3c 20 30 29 20 7b 0a  fork_ret < 0) {.
acf0: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a  ..return(1);..}.
ad00: 0a 09 69 66 20 28 66 6f 72 6b 5f 72 65 74 20 3e  ..if (fork_ret >
ad10: 20 30 29 20 7b 0a 09 09 2f 2a 20 50 61 72 65 6e   0) {.../* Paren
ad20: 74 20 2a 2f 0a 09 09 77 61 69 74 70 69 64 28 66  t */...waitpid(f
ad30: 6f 72 6b 5f 72 65 74 2c 20 4e 55 4c 4c 2c 20 30  ork_ret, NULL, 0
ad40: 29 3b 0a 0a 09 09 65 78 69 74 28 45 58 49 54 5f  );....exit(EXIT_
ad50: 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f  SUCCESS);..}.../
ad60: 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 69 66 20 28  * Child */..if (
ad70: 66 6f 72 6b 28 29 20 21 3d 20 30 29 20 7b 0a 09  fork() != 0) {..
ad80: 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 09 65  ./* Child */...e
ad90: 78 69 74 28 45 58 49 54 5f 53 55 43 43 45 53 53  xit(EXIT_SUCCESS
ada0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 47 72 61 6e 64  );..}.../* Grand
adb0: 20 63 68 69 6c 64 20 2a 2f 0a 09 73 65 74 73 69   child */..setsi
adc0: 64 5f 72 65 74 20 3d 20 73 65 74 73 69 64 28 29  d_ret = setsid()
add0: 3b 0a 09 69 66 20 28 73 65 74 73 69 64 5f 72 65  ;..if (setsid_re
ade0: 74 20 3d 3d 20 28 28 70 69 64 5f 74 29 20 2d 31  t == ((pid_t) -1
adf0: 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29  )) {...return(1)
ae00: 3b 0a 09 7d 0a 0a 09 66 64 5f 69 6e 20 3d 20 6f  ;..}...fd_in = o
ae10: 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c  pen("/dev/null",
ae20: 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 09 66 64 5f   O_RDONLY);..fd_
ae30: 6f 75 74 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76  out = open("/dev
ae40: 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 52 4f 4e 4c 59  /null", O_WRONLY
ae50: 29 3b 0a 09 69 66 20 28 66 64 5f 69 6e 20 3c 20  );..if (fd_in < 
ae60: 30 20 7c 7c 20 66 64 5f 6f 75 74 20 3c 20 30 29  0 || fd_out < 0)
ae70: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a   {...return(1);.
ae80: 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d 20  .}...dup2_ret = 
ae90: 64 75 70 32 28 66 64 5f 69 6e 2c 20 53 54 44 49  dup2(fd_in, STDI
aea0: 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28  N_FILENO);..if (
aeb0: 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 49  dup2_ret != STDI
aec0: 4e 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65  N_FILENO) {...re
aed0: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75  turn(1);..}...du
aee0: 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64  p2_ret = dup2(fd
aef0: 5f 6f 75 74 2c 20 53 54 44 4f 55 54 5f 46 49 4c  _out, STDOUT_FIL
af00: 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f  ENO);..if (dup2_
af10: 72 65 74 20 21 3d 20 53 54 44 4f 55 54 5f 46 49  ret != STDOUT_FI
af20: 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e  LENO) {...return
af30: 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72  (1);..}...dup2_r
af40: 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f 75 74  et = dup2(fd_out
af50: 2c 20 53 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29  , STDERR_FILENO)
af60: 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74 20  ;..if (dup2_ret 
af70: 21 3d 20 53 54 44 45 52 52 5f 46 49 4c 45 4e 4f  != STDERR_FILENO
af80: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b  ) {...return(1);
af90: 0a 09 7d 0a 0a 09 63 6c 6f 73 65 28 66 64 5f 69  ..}...close(fd_i
afa0: 6e 29 3b 0a 09 63 6c 6f 73 65 28 66 64 5f 6f 75  n);..close(fd_ou
afb0: 74 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b  t);...return(0);
afc0: 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 70 72 6f 63 65  .}../* Run proce
afd0: 73 73 20 2a 2f 0a 69 6e 74 20 6d 61 69 6e 28 69  ss */.int main(i
afe0: 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a  nt argc, char **
aff0: 61 72 67 76 29 20 7b 0a 09 73 74 72 75 63 74 20  argv) {..struct 
b000: 6f 70 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 5b 31  option options[1
b010: 32 5d 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65  2];..struct file
b020: 64 5f 6f 70 74 69 6f 6e 73 20 74 68 72 65 61 64  d_options thread
b030: 5f 6f 70 74 69 6f 6e 73 20 3d 20 7b 30 7d 3b 0a  _options = {0};.
b040: 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 62 69 6e  .const char *bin
b050: 64 5f 61 64 64 72 20 3d 20 42 49 4e 44 5f 41 44  d_addr = BIND_AD
b060: 44 52 2c 20 2a 6e 65 77 72 6f 6f 74 20 3d 20 4e  DR, *newroot = N
b070: 55 4c 4c 2c 20 2a 6c 6f 67 5f 66 69 6c 65 20 3d  ULL, *log_file =
b080: 20 4c 4f 47 5f 46 49 4c 45 3b 0a 09 46 49 4c 45   LOG_FILE;..FILE
b090: 20 2a 6c 6f 67 5f 66 70 3b 0a 09 75 69 64 5f 74   *log_fp;..uid_t
b0a0: 20 75 73 65 72 20 3d 20 30 3b 0a 09 69 6e 74 20   user = 0;..int 
b0b0: 70 6f 72 74 20 3d 20 50 4f 52 54 2c 20 74 68 72  port = PORT, thr
b0c0: 65 61 64 5f 63 6f 75 6e 74 20 3d 20 54 48 52 45  ead_count = THRE
b0d0: 41 44 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63  AD_COUNT;..int c
b0e0: 61 63 68 65 5f 73 69 7a 65 20 3d 20 43 41 43 48  ache_size = CACH
b0f0: 45 5f 53 49 5a 45 3b 0a 09 69 6e 74 20 69 6e 69  E_SIZE;..int ini
b100: 74 5f 72 65 74 2c 20 63 68 72 6f 6f 74 5f 72 65  t_ret, chroot_re
b110: 74 2c 20 73 65 74 75 69 64 5f 72 65 74 2c 20 6c  t, setuid_ret, l
b120: 6f 6f 6b 75 70 5f 72 65 74 2c 20 63 68 64 69 72  ookup_ret, chdir
b130: 5f 72 65 74 3b 0a 09 69 6e 74 20 73 65 74 75 69  _ret;..int setui
b140: 64 5f 65 6e 61 62 6c 65 64 20 3d 20 30 2c 20 64  d_enabled = 0, d
b150: 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20  aemon_enabled = 
b160: 30 3b 0a 09 69 6e 74 20 63 68 3b 0a 09 69 6e 74  0;..int ch;..int
b170: 20 66 64 3b 0a 0a 09 2f 2a 20 50 72 6f 63 65 73   fd;.../* Proces
b180: 73 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09  s arguments */..
b190: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e  filed_getopt_lon
b1a0: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e  g_setopt(&option
b1b0: 73 5b 30 5d 2c 20 22 70 6f 72 74 22 2c 20 72 65  s[0], "port", re
b1c0: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c  quired_argument,
b1d0: 20 27 70 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65   'p');..filed_ge
b1e0: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74  topt_long_setopt
b1f0: 28 26 6f 70 74 69 6f 6e 73 5b 31 5d 2c 20 22 74  (&options[1], "t
b200: 68 72 65 61 64 73 22 2c 20 72 65 71 75 69 72 65  hreads", require
b210: 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 74 27 29  d_argument, 't')
b220: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f  ;..filed_getopt_
b230: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74  long_setopt(&opt
b240: 69 6f 6e 73 5b 32 5d 2c 20 22 63 61 63 68 65 22  ions[2], "cache"
b250: 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d  , required_argum
b260: 65 6e 74 2c 20 27 63 27 29 3b 0a 09 66 69 6c 65  ent, 'c');..file
b270: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65  d_getopt_long_se
b280: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 33 5d  topt(&options[3]
b290: 2c 20 22 62 69 6e 64 22 2c 20 72 65 71 75 69 72  , "bind", requir
b2a0: 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 62 27  ed_argument, 'b'
b2b0: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74  );..filed_getopt
b2c0: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70  _long_setopt(&op
b2d0: 74 69 6f 6e 73 5b 34 5d 2c 20 22 75 73 65 72 22  tions[4], "user"
b2e0: 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d  , required_argum
b2f0: 65 6e 74 2c 20 27 75 27 29 3b 0a 09 66 69 6c 65  ent, 'u');..file
b300: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65  d_getopt_long_se
b310: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 35 5d  topt(&options[5]
b320: 2c 20 22 72 6f 6f 74 22 2c 20 72 65 71 75 69 72  , "root", requir
b330: 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 72 27  ed_argument, 'r'
b340: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74  );..filed_getopt
b350: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70  _long_setopt(&op
b360: 74 69 6f 6e 73 5b 36 5d 2c 20 22 68 65 6c 70 22  tions[6], "help"
b370: 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27  , no_argument, '
b380: 68 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f  h');..filed_geto
b390: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26  pt_long_setopt(&
b3a0: 6f 70 74 69 6f 6e 73 5b 37 5d 2c 20 22 64 61 65  options[7], "dae
b3b0: 6d 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e  mon", no_argumen
b3c0: 74 2c 20 27 64 27 29 3b 0a 09 66 69 6c 65 64 5f  t, 'd');..filed_
b3d0: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f  getopt_long_seto
b3e0: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 38 5d 2c 20  pt(&options[8], 
b3f0: 22 6c 6f 67 22 2c 20 72 65 71 75 69 72 65 64 5f  "log", required_
b400: 61 72 67 75 6d 65 6e 74 2c 20 27 6c 27 29 3b 0a  argument, 'l');.
b410: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f  .filed_getopt_lo
b420: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f  ng_setopt(&optio
b430: 6e 73 5b 39 5d 2c 20 22 76 65 72 73 69 6f 6e 22  ns[9], "version"
b440: 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27  , no_argument, '
b450: 76 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f  v');..filed_geto
b460: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26  pt_long_setopt(&
b470: 6f 70 74 69 6f 6e 73 5b 31 30 5d 2c 20 22 76 68  options[10], "vh
b480: 6f 73 74 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e  ost", no_argumen
b490: 74 2c 20 27 56 27 29 3b 0a 09 66 69 6c 65 64 5f  t, 'V');..filed_
b4a0: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f  getopt_long_seto
b4b0: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 31 5d 2c  pt(&options[11],
b4c0: 20 4e 55 4c 4c 2c 20 30 2c 20 30 29 3b 0a 09 77   NULL, 0, 0);..w
b4d0: 68 69 6c 65 20 28 28 63 68 20 3d 20 67 65 74 6f  hile ((ch = geto
b4e0: 70 74 5f 6c 6f 6e 67 28 61 72 67 63 2c 20 61 72  pt_long(argc, ar
b4f0: 67 76 2c 20 22 70 3a 74 3a 63 3a 62 3a 75 3a 72  gv, "p:t:c:b:u:r
b500: 3a 6c 3a 68 64 76 56 22 2c 20 6f 70 74 69 6f 6e  :l:hdvV", option
b510: 73 2c 20 4e 55 4c 4c 29 29 20 21 3d 20 2d 31 29  s, NULL)) != -1)
b520: 20 7b 0a 09 09 73 77 69 74 63 68 28 63 68 29 20   {...switch(ch) 
b530: 7b 0a 09 09 09 63 61 73 65 20 27 70 27 3a 0a 09  {....case 'p':..
b540: 09 09 09 70 6f 72 74 20 3d 20 61 74 6f 69 28 6f  ...port = atoi(o
b550: 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61  ptarg);.....brea
b560: 6b 3b 0a 09 09 09 63 61 73 65 20 27 74 27 3a 0a  k;....case 't':.
b570: 09 09 09 09 74 68 72 65 61 64 5f 63 6f 75 6e 74  ....thread_count
b580: 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 3b   = atoi(optarg);
b590: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63  .....break;....c
b5a0: 61 73 65 20 27 63 27 3a 0a 09 09 09 09 63 61 63  ase 'c':.....cac
b5b0: 68 65 5f 73 69 7a 65 20 3d 20 61 74 6f 69 28 6f  he_size = atoi(o
b5c0: 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61  ptarg);.....brea
b5d0: 6b 3b 0a 09 09 09 63 61 73 65 20 27 62 27 3a 0a  k;....case 'b':.
b5e0: 09 09 09 09 62 69 6e 64 5f 61 64 64 72 20 3d 20  ....bind_addr = 
b5f0: 73 74 72 64 75 70 28 6f 70 74 61 72 67 29 3b 0a  strdup(optarg);.
b600: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61  ....break;....ca
b610: 73 65 20 27 75 27 3a 0a 09 09 09 09 73 65 74 75  se 'u':.....setu
b620: 69 64 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a  id_enabled = 1;.
b630: 09 09 09 09 6c 6f 6f 6b 75 70 5f 72 65 74 20 3d  ....lookup_ret =
b640: 20 66 69 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b   filed_user_look
b650: 75 70 28 6f 70 74 61 72 67 2c 20 26 75 73 65 72  up(optarg, &user
b660: 29 3b 0a 09 09 09 09 69 66 20 28 6c 6f 6f 6b 75  );.....if (looku
b670: 70 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09  p_ret != 0) {...
b680: 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68  ...filed_print_h
b690: 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20 22  elp(stderr, 0, "
b6a0: 49 6e 76 61 6c 69 64 20 75 73 65 72 6e 61 6d 65  Invalid username
b6b0: 20 73 70 65 63 69 66 69 65 64 22 29 3b 0a 0a 09   specified");...
b6c0: 09 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  ....return(1);..
b6d0: 09 09 09 7d 0a 09 09 09 09 62 72 65 61 6b 3b 0a  ...}.....break;.
b6e0: 09 09 09 63 61 73 65 20 27 72 27 3a 0a 09 09 09  ...case 'r':....
b6f0: 09 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 61 6e  .filed_path_tran
b700: 73 6c 61 74 65 5f 73 65 74 5f 72 6f 6f 74 28 6e  slate_set_root(n
b710: 65 77 72 6f 6f 74 2c 20 26 74 68 72 65 61 64 5f  ewroot, &thread_
b720: 6f 70 74 69 6f 6e 73 2c 20 6f 70 74 61 72 67 29  options, optarg)
b730: 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09  ;.....break;....
b740: 63 61 73 65 20 27 6c 27 3a 0a 09 09 09 09 6c 6f  case 'l':.....lo
b750: 67 5f 66 69 6c 65 20 3d 20 73 74 72 64 75 70 28  g_file = strdup(
b760: 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65  optarg);.....bre
b770: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 64 27 3a  ak;....case 'd':
b780: 0a 09 09 09 09 64 61 65 6d 6f 6e 5f 65 6e 61 62  .....daemon_enab
b790: 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 62 72 65  led = 1;.....bre
b7a0: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 56 27 3a  ak;....case 'V':
b7b0: 0a 09 09 09 09 74 68 72 65 61 64 5f 6f 70 74 69  .....thread_opti
b7c0: 6f 6e 73 2e 76 68 6f 73 74 73 5f 65 6e 61 62 6c  ons.vhosts_enabl
b7d0: 65 64 20 3d 20 31 3b 0a 0a 09 09 09 09 62 72 65  ed = 1;......bre
b7e0: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 76 27 3a  ak;....case 'v':
b7f0: 0a 09 09 09 09 70 72 69 6e 74 66 28 22 66 69 6c  .....printf("fil
b800: 65 64 20 76 65 72 73 69 6f 6e 20 25 73 5c 6e 22  ed version %s\n"
b810: 2c 20 46 49 4c 45 44 5f 56 45 52 53 49 4f 4e 29  , FILED_VERSION)
b820: 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 30 29  ;......return(0)
b830: 3b 0a 09 09 09 63 61 73 65 20 27 3f 27 3a 0a 09  ;....case '?':..
b840: 09 09 63 61 73 65 20 27 3a 27 3a 0a 09 09 09 09  ..case ':':.....
b850: 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70  filed_print_help
b860: 28 73 74 64 65 72 72 2c 20 30 2c 20 4e 55 4c 4c  (stderr, 0, NULL
b870: 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 31  );......return(1
b880: 29 3b 0a 09 09 09 63 61 73 65 20 27 68 27 3a 0a  );....case 'h':.
b890: 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f  ....filed_print_
b8a0: 68 65 6c 70 28 73 74 64 6f 75 74 2c 20 31 2c 20  help(stdout, 1, 
b8b0: 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75  NULL);......retu
b8c0: 72 6e 28 30 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09  rn(0);...}..}...
b8d0: 2f 2a 20 4f 70 65 6e 20 6c 6f 67 20 66 69 6c 65  /* Open log file
b8e0: 20 2a 2f 0a 09 6c 6f 67 5f 66 70 20 3d 20 66 69   */..log_fp = fi
b8f0: 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 6c 6f 67  led_log_open(log
b900: 5f 66 69 6c 65 29 3b 0a 09 69 66 20 28 6c 6f 67  _file);..if (log
b910: 5f 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09  _fp == NULL) {..
b920: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c  .perror("filed_l
b930: 6f 67 5f 6f 70 65 6e 22 29 3b 0a 0a 09 09 72 65  og_open");....re
b940: 74 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a  turn(4);..}.../*
b950: 20 43 72 65 61 74 65 20 6c 69 73 74 65 6e 69 6e   Create listenin
b960: 67 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 66 64 20  g socket */..fd 
b970: 3d 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 62  = filed_listen(b
b980: 69 6e 64 5f 61 64 64 72 2c 20 70 6f 72 74 29 3b  ind_addr, port);
b990: 0a 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a  ..if (fd < 0) {.
b9a0: 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f  ..perror("filed_
b9b0: 6c 69 73 74 65 6e 22 29 3b 0a 0a 09 09 72 65 74  listen");....ret
b9c0: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20  urn(1);..}.../* 
b9d0: 49 6e 69 74 69 61 6c 69 7a 65 20 74 69 6d 65 6f  Initialize timeo
b9e0: 75 74 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f  ut structures */
b9f0: 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c  ..init_ret = fil
ba00: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74  ed_sockettimeout
ba10: 5f 69 6e 69 74 28 29 3b 0a 09 69 66 20 28 69 6e  _init();..if (in
ba20: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09  it_ret != 0) {..
ba30: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 73  .perror("filed_s
ba40: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69  ockettimeout_ini
ba50: 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 38  t");....return(8
ba60: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 42 65 63 6f 6d  );..}.../* Becom
ba70: 65 20 61 20 64 61 65 6d 6f 6e 20 2a 2f 0a 09 69  e a daemon */..i
ba80: 66 20 28 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65  f (daemon_enable
ba90: 64 29 20 7b 0a 09 09 69 6e 69 74 5f 72 65 74 20  d) {...init_ret 
baa0: 3d 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a  = filed_daemoniz
bab0: 65 28 29 3b 0a 09 09 69 66 20 28 69 6e 69 74 5f  e();...if (init_
bac0: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70  ret != 0) {....p
bad0: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 64 61 65  error("filed_dae
bae0: 6d 6f 6e 69 7a 65 22 29 3b 0a 0a 09 09 09 72 65  monize");.....re
baf0: 74 75 72 6e 28 36 29 3b 0a 09 09 7d 0a 09 7d 0a  turn(6);...}..}.
bb00: 0a 09 2f 2a 20 43 68 72 6f 6f 74 2c 20 69 66 20  ../* Chroot, if 
bb10: 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09  appropriate */..
bb20: 69 66 20 28 6e 65 77 72 6f 6f 74 29 20 7b 0a 09  if (newroot) {..
bb30: 09 63 68 64 69 72 5f 72 65 74 20 3d 20 63 68 64  .chdir_ret = chd
bb40: 69 72 28 6e 65 77 72 6f 6f 74 29 3b 0a 09 09 69  ir(newroot);...i
bb50: 66 20 28 63 68 64 69 72 5f 72 65 74 20 21 3d 20  f (chdir_ret != 
bb60: 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22  0) {....perror("
bb70: 63 68 64 69 72 22 29 3b 0a 0a 09 09 09 72 65 74  chdir");.....ret
bb80: 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 63  urn(1);...}....c
bb90: 68 72 6f 6f 74 5f 72 65 74 20 3d 20 63 68 72 6f  hroot_ret = chro
bba0: 6f 74 28 22 2e 22 29 3b 0a 09 09 69 66 20 28 63  ot(".");...if (c
bbb0: 68 72 6f 6f 74 5f 72 65 74 20 21 3d 20 30 29 20  hroot_ret != 0) 
bbc0: 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 63 68 72  {....perror("chr
bbd0: 6f 6f 74 22 29 3b 0a 0a 09 09 09 72 65 74 75 72  oot");.....retur
bbe0: 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f  n(1);...}..}.../
bbf0: 2a 20 44 72 6f 70 20 70 72 69 76 69 6c 65 67 65  * Drop privilege
bc00: 73 2c 20 69 66 20 61 70 70 72 6f 70 72 69 61 74  s, if appropriat
bc10: 65 20 2a 2f 0a 09 69 66 20 28 73 65 74 75 69 64  e */..if (setuid
bc20: 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 73 65  _enabled) {...se
bc30: 74 75 69 64 5f 72 65 74 20 3d 20 73 65 74 75 69  tuid_ret = setui
bc40: 64 28 75 73 65 72 29 3b 0a 09 09 69 66 20 28 73  d(user);...if (s
bc50: 65 74 75 69 64 5f 72 65 74 20 21 3d 20 30 29 20  etuid_ret != 0) 
bc60: 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 73 65 74  {....perror("set
bc70: 75 69 64 22 29 3b 0a 0a 09 09 09 72 65 74 75 72  uid");.....retur
bc80: 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f  n(1);...}..}.../
bc90: 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 2a 2f 0a  * Initialize */.
bca0: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65  .init_ret = file
bcb0: 64 5f 69 6e 69 74 28 63 61 63 68 65 5f 73 69 7a  d_init(cache_siz
bcc0: 65 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65  e);..if (init_re
bcd0: 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72  t != 0) {...perr
bce0: 6f 72 28 22 66 69 6c 65 64 5f 69 6e 69 74 22 29  or("filed_init")
bcf0: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 33 29 3b 0a  ;....return(3);.
bd00: 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c  .}.../* Create l
bd10: 6f 67 67 69 6e 67 20 74 68 72 65 61 64 20 2a 2f  ogging thread */
bd20: 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c  ..init_ret = fil
bd30: 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61  ed_logging_threa
bd40: 64 5f 69 6e 69 74 28 6c 6f 67 5f 66 70 29 3b 0a  d_init(log_fp);.
bd50: 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d  .if (init_ret !=
bd60: 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22   0) {...perror("
bd70: 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68  filed_logging_th
bd80: 72 65 61 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09  read_init");....
bd90: 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09  return(4);..}...
bda0: 2f 2a 20 43 72 65 61 74 65 20 73 6f 63 6b 65 74  /* Create socket
bdb0: 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 74 68 72   termination thr
bdc0: 65 61 64 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74  ead */..init_ret
bdd0: 20 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74   = filed_sockett
bde0: 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e  imeout_thread_in
bdf0: 69 74 28 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f  it();..if (init_
be00: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65  ret != 0) {...pe
be10: 72 72 6f 72 28 22 66 69 6c 65 64 5f 73 6f 63 6b  rror("filed_sock
be20: 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64  ettimeout_thread
be30: 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75  _init");....retu
be40: 72 6e 28 37 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43  rn(7);..}.../* C
be50: 72 65 61 74 65 20 77 6f 72 6b 65 72 20 74 68 72  reate worker thr
be60: 65 61 64 73 20 2a 2f 0a 09 69 6e 69 74 5f 72 65  eads */..init_re
be70: 74 20 3d 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72  t = filed_worker
be80: 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 28 66 64  _threads_init(fd
be90: 2c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 2c 20  , thread_count, 
bea0: 26 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73 29  &thread_options)
beb0: 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20  ;..if (init_ret 
bec0: 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72  != 0) {...perror
bed0: 28 22 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74  ("filed_worker_t
bee0: 68 72 65 61 64 73 5f 69 6e 69 74 22 29 3b 0a 0a  hreads_init");..
bef0: 09 09 72 65 74 75 72 6e 28 35 29 3b 0a 09 7d 0a  ..return(5);..}.
bf00: 0a 09 2f 2a 20 57 61 69 74 20 66 6f 72 20 74 68  ../* Wait for th
bf10: 72 65 61 64 73 20 74 6f 20 65 78 69 74 20 2a 2f  reads to exit */
bf20: 0a 09 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 4d  ../* XXX:TODO: M
bf30: 6f 6e 69 74 6f 72 20 74 68 72 65 61 64 20 75 73  onitor thread us
bf40: 61 67 65 20 2a 2f 0a 09 77 68 69 6c 65 20 28 31  age */..while (1
bf50: 29 20 7b 0a 09 09 73 6c 65 65 70 28 38 36 34 30  ) {...sleep(8640
bf60: 30 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65 74 75  0);..}.../* Retu
bf70: 72 6e 20 69 6e 20 66 61 69 6c 75 72 65 20 2a 2f  rn in failure */
bf80: 0a 09 72 65 74 75 72 6e 28 32 29 3b 0a 7d 0a     ..return(2);.}.