Hex Artifact Content

Artifact 5138c920c196688fc344333555f81dc83715fcc8:


0000: 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 65  #include <sys/se
0010: 6e 64 66 69 6c 65 2e 68 3e 0a 23 69 6e 63 6c 75  ndfile.h>.#inclu
0020: 64 65 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68  de <sys/socket.h
0030: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f  >.#include <sys/
0040: 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64  types.h>.#includ
0050: 65 20 3c 61 72 70 61 2f 69 6e 65 74 2e 68 3e 0a  e <arpa/inet.h>.
0060: 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d  #include <sys/mm
0070: 61 6e 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  an.h>.#include <
0080: 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63  sys/stat.h>.#inc
0090: 6c 75 64 65 20 3c 73 79 73 2f 77 61 69 74 2e 68  lude <sys/wait.h
00a0: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72  >.#include <pthr
00b0: 65 61 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  ead.h>.#include 
00c0: 3c 73 74 72 69 6e 67 73 2e 68 3e 0a 23 69 6e 63  <strings.h>.#inc
00d0: 6c 75 64 65 20 3c 73 69 67 6e 61 6c 2e 68 3e 0a  lude <signal.h>.
00e0: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62  #include <stdlib
00f0: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e  .h>.#include <un
0100: 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  istd.h>.#include
0110: 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63   <string.h>.#inc
0120: 6c 75 64 65 20 3c 67 65 74 6f 70 74 2e 68 3e 0a  lude <getopt.h>.
0130: 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c 2e  #include <fcntl.
0140: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  h>.#include <std
0150: 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  io.h>.#include <
0160: 65 72 72 6e 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64  errno.h>.#includ
0170: 65 20 3c 74 69 6d 65 2e 68 3e 0a 23 69 6e 63 6c  e <time.h>.#incl
0180: 75 64 65 20 3c 70 77 64 2e 68 3e 0a 0a 2f 2a 20  ude <pwd.h>../* 
0190: 43 6f 6d 70 69 6c 65 20 74 69 6d 65 20 63 6f 6e  Compile time con
01a0: 73 74 61 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e  stants */.#defin
01b0: 65 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45  e FILED_SENDFILE
01c0: 5f 4d 41 58 20 31 36 37 37 37 32 31 35 0a 23 64  _MAX 16777215.#d
01d0: 65 66 69 6e 65 20 4d 41 58 5f 46 41 49 4c 55 52  efine MAX_FAILUR
01e0: 45 5f 43 4f 55 4e 54 20 33 30 0a 0a 2f 2a 20 44  E_COUNT 30../* D
01f0: 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f  efault values */
0200: 0a 23 64 65 66 69 6e 65 20 50 4f 52 54 20 38 30  .#define PORT 80
0210: 0a 23 64 65 66 69 6e 65 20 54 48 52 45 41 44 5f  .#define THREAD_
0220: 43 4f 55 4e 54 20 35 0a 23 64 65 66 69 6e 65 20  COUNT 5.#define 
0230: 42 49 4e 44 5f 41 44 44 52 20 22 3a 3a 22 0a 23  BIND_ADDR "::".#
0240: 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 49 5a  define CACHE_SIZ
0250: 45 20 38 32 30 39 0a 0a 2f 2a 20 41 72 67 75 6d  E 8209../* Argum
0260: 65 6e 74 73 20 66 6f 72 20 77 6f 72 6b 65 72 20  ents for worker 
0270: 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 72 75 63  threads */.struc
0280: 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74  t filed_worker_t
0290: 68 72 65 61 64 5f 61 72 67 73 20 7b 0a 09 69 6e  hread_args {..in
02a0: 74 20 66 64 3b 0a 7d 3b 0a 0a 2f 2a 20 46 69 6c  t fd;.};../* Fil
02b0: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  e information */
02c0: 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69  .struct filed_fi
02d0: 6c 65 69 6e 66 6f 20 7b 0a 09 70 74 68 72 65 61  leinfo {..pthrea
02e0: 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b  d_mutex_t mutex;
02f0: 0a 09 63 68 61 72 20 2a 70 61 74 68 3b 0a 09 69  ..char *path;..i
0300: 6e 74 20 66 64 3b 0a 09 6f 66 66 5f 74 20 6c 65  nt fd;..off_t le
0310: 6e 3b 0a 09 63 68 61 72 20 2a 6c 61 73 74 6d 6f  n;..char *lastmo
0320: 64 3b 0a 09 63 68 61 72 20 6c 61 73 74 6d 6f 64  d;..char lastmod
0330: 5f 62 5b 36 34 5d 3b 0a 09 63 68 61 72 20 2a 74  _b[64];..char *t
0340: 79 70 65 3b 0a 7d 3b 0a 0a 2f 2a 20 52 65 71 75  ype;.};../* Requ
0350: 65 73 74 20 76 61 72 69 61 62 6c 65 73 20 2a 2f  est variables */
0360: 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74  .struct filed_ht
0370: 74 70 5f 72 65 71 75 65 73 74 20 7b 0a 09 2f 2a  tp_request {../*
0380: 2a 20 42 75 66 66 65 72 73 20 2a 2a 2f 0a 09 73  * Buffers **/..s
0390: 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65  truct filed_file
03a0: 69 6e 66 6f 20 66 69 6c 65 69 6e 66 6f 3b 0a 09  info fileinfo;..
03b0: 63 68 61 72 20 70 61 74 68 5f 62 5b 31 30 31 30  char path_b[1010
03c0: 5d 3b 0a 09 63 68 61 72 20 74 6d 70 62 75 66 5b  ];..char tmpbuf[
03d0: 31 30 31 30 5d 3b 0a 0a 09 2f 2a 2a 20 48 54 54  1010];.../** HTT
03e0: 50 20 52 65 71 75 65 73 74 20 69 6e 66 6f 72 6d  P Request inform
03f0: 61 74 69 6f 6e 20 2a 2a 2f 0a 09 63 68 61 72 20  ation **/..char 
0400: 2a 70 61 74 68 3b 20 20 20 20 20 2f 2a 2a 2a 20  *path;     /*** 
0410: 50 61 74 68 20 62 65 69 6e 67 20 72 65 71 75 65  Path being reque
0420: 73 74 65 64 20 2a 2a 2a 2f 0a 0a 09 73 74 72 75  sted ***/...stru
0430: 63 74 20 7b 0a 09 09 73 74 72 75 63 74 20 7b 0a  ct {...struct {.
0440: 09 09 09 69 6e 74 20 70 72 65 73 65 6e 74 3b 0a  ...int present;.
0450: 09 09 09 6f 66 66 5f 74 20 6f 66 66 73 65 74 3b  ...off_t offset;
0460: 20 20 20 2f 2a 2a 2a 20 52 61 6e 67 65 20 73 74     /*** Range st
0470: 61 72 74 20 2a 2a 2a 2f 0a 09 09 09 6f 66 66 5f  art ***/....off_
0480: 74 20 6c 65 6e 67 74 68 3b 20 20 20 2f 2a 2a 2a  t length;   /***
0490: 20 52 61 6e 67 65 20 6c 65 6e 67 74 68 20 2a 2a   Range length **
04a0: 2a 2f 0a 09 09 7d 20 72 61 6e 67 65 3b 0a 09 7d  */...} range;..}
04b0: 20 68 65 61 64 65 72 73 3b 0a 7d 3b 0a 0a 2f 2a   headers;.};../*
04c0: 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   Global variable
04d0: 73 20 2a 2f 0a 2f 2a 2a 20 4f 70 65 6e 20 46 69  s */./** Open Fi
04e0: 6c 65 20 63 61 63 68 65 20 2a 2a 2f 0a 73 74 72  le cache **/.str
04f0: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e  uct filed_filein
0500: 66 6f 20 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e  fo *filed_filein
0510: 66 6f 5f 66 64 63 61 63 68 65 20 3d 20 4e 55 4c  fo_fdcache = NUL
0520: 4c 3b 0a 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  L;.unsigned int 
0530: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66  filed_fileinfo_f
0540: 64 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 30 3b  dcache_size = 0;
0550: 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ../* Initialize 
0560: 63 61 63 68 65 20 2a 2f 0a 73 74 61 74 69 63 20  cache */.static 
0570: 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63  int filed_init_c
0580: 61 63 68 65 28 75 6e 73 69 67 6e 65 64 20 69 6e  ache(unsigned in
0590: 74 20 63 61 63 68 65 5f 73 69 7a 65 29 20 7b 0a  t cache_size) {.
05a0: 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 64  .unsigned int id
05b0: 78 3b 0a 09 69 6e 74 20 6d 75 74 65 78 5f 69 6e  x;..int mutex_in
05c0: 69 74 5f 72 65 74 3b 0a 0a 09 2f 2a 20 43 61 63  it_ret;.../* Cac
05d0: 68 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65  he may not be re
05e0: 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a  -initialized */.
05f0: 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69  .if (filed_filei
0600: 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65  nfo_fdcache_size
0610: 20 21 3d 20 30 20 7c 7c 20 66 69 6c 65 64 5f 66   != 0 || filed_f
0620: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20  ileinfo_fdcache 
0630: 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74  != NULL) {...ret
0640: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20  urn(1);..}.../* 
0650: 41 6c 6c 6f 63 61 74 65 20 63 61 63 68 65 20 2a  Allocate cache *
0660: 2f 0a 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  /..filed_fileinf
0670: 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 3d  o_fdcache_size =
0680: 20 63 61 63 68 65 5f 73 69 7a 65 3b 0a 09 66 69   cache_size;..fi
0690: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
06a0: 61 63 68 65 20 3d 20 6d 61 6c 6c 6f 63 28 73 69  ache = malloc(si
06b0: 7a 65 6f 66 28 2a 66 69 6c 65 64 5f 66 69 6c 65  zeof(*filed_file
06c0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 29 20 2a 20  info_fdcache) * 
06d0: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66  filed_fileinfo_f
06e0: 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69  dcache_size);..i
06f0: 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  f (filed_fileinf
0700: 6f 5f 66 64 63 61 63 68 65 20 3d 3d 20 4e 55 4c  o_fdcache == NUL
0710: 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29  L) {...return(1)
0720: 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 61  ;..}.../* Initia
0730: 6c 69 7a 65 20 63 61 63 68 65 20 65 6e 74 72 69  lize cache entri
0740: 65 73 20 2a 2f 0a 09 66 6f 72 20 28 69 64 78 20  es */..for (idx 
0750: 3d 20 30 3b 20 69 64 78 20 3c 20 66 69 6c 65 64  = 0; idx < filed
0760: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68  _fileinfo_fdcach
0770: 65 5f 73 69 7a 65 3b 20 69 64 78 2b 2b 29 20 7b  e_size; idx++) {
0780: 0a 09 09 6d 75 74 65 78 5f 69 6e 69 74 5f 72 65  ...mutex_init_re
0790: 74 20 3d 20 70 74 68 72 65 61 64 5f 6d 75 74 65  t = pthread_mute
07a0: 78 5f 69 6e 69 74 28 26 66 69 6c 65 64 5f 66 69  x_init(&filed_fi
07b0: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69  leinfo_fdcache[i
07c0: 64 78 5d 2e 6d 75 74 65 78 2c 20 4e 55 4c 4c 29  dx].mutex, NULL)
07d0: 3b 0a 09 09 69 66 20 28 6d 75 74 65 78 5f 69 6e  ;...if (mutex_in
07e0: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09  it_ret != 0) {..
07f0: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d  ..return(1);...}
0800: 0a 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e  ....filed_filein
0810: 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e  fo_fdcache[idx].
0820: 70 61 74 68 20 3d 20 73 74 72 64 75 70 28 22 22  path = strdup(""
0830: 29 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69  );...filed_filei
0840: 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d  nfo_fdcache[idx]
0850: 2e 66 64 20 3d 20 2d 31 3b 0a 09 09 66 69 6c 65  .fd = -1;...file
0860: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63  d_fileinfo_fdcac
0870: 68 65 5b 69 64 78 5d 2e 6c 61 73 74 6d 6f 64 20  he[idx].lastmod 
0880: 3d 20 22 22 3b 0a 09 09 66 69 6c 65 64 5f 66 69  = "";...filed_fi
0890: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69  leinfo_fdcache[i
08a0: 64 78 5d 2e 74 79 70 65 20 3d 20 22 22 3b 0a 09  dx].type = "";..
08b0: 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d  }...return(0);.}
08c0: 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ../* Initialize 
08d0: 70 72 6f 63 65 73 73 20 2a 2f 0a 73 74 61 74 69  process */.stati
08e0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74  c int filed_init
08f0: 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61  (unsigned int ca
0900: 63 68 65 5f 73 69 7a 65 29 20 7b 0a 09 73 74 61  che_size) {..sta
0910: 74 69 63 20 69 6e 74 20 63 61 6c 6c 65 64 20 3d  tic int called =
0920: 20 30 3b 0a 09 69 6e 74 20 63 61 63 68 65 5f 72   0;..int cache_r
0930: 65 74 3b 0a 0a 09 69 66 20 28 63 61 6c 6c 65 64  et;...if (called
0940: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b  ) {...return(0);
0950: 0a 09 7d 0a 0a 09 63 61 6c 6c 65 64 20 3d 20 31  ..}...called = 1
0960: 3b 0a 0a 09 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 4c  ;...mlockall(MCL
0970: 5f 43 55 52 52 45 4e 54 20 7c 20 4d 43 4c 5f 46  _CURRENT | MCL_F
0980: 55 54 55 52 45 29 3b 0a 0a 09 73 69 67 6e 61 6c  UTURE);...signal
0990: 28 53 49 47 50 49 50 45 2c 20 53 49 47 5f 49 47  (SIGPIPE, SIG_IG
09a0: 4e 29 3b 0a 0a 09 63 61 63 68 65 5f 72 65 74 20  N);...cache_ret 
09b0: 3d 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63  = filed_init_cac
09c0: 68 65 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  he(cache_size);.
09d0: 09 69 66 20 28 63 61 63 68 65 5f 72 65 74 20 21  .if (cache_ret !
09e0: 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  = 0) {...return(
09f0: 63 61 63 68 65 5f 72 65 74 29 3b 0a 09 7d 0a 0a  cache_ret);..}..
0a00: 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f  .return(0);.}../
0a10: 2a 20 4c 69 73 74 65 6e 20 6f 6e 20 61 20 70 61  * Listen on a pa
0a20: 72 74 69 63 75 6c 61 72 20 61 64 64 72 65 73 73  rticular address
0a30: 2f 70 6f 72 74 20 2a 2f 0a 73 74 61 74 69 63 20  /port */.static 
0a40: 69 6e 74 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e  int filed_listen
0a50: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 64 64  (const char *add
0a60: 72 65 73 73 2c 20 75 6e 73 69 67 6e 65 64 20 69  ress, unsigned i
0a70: 6e 74 20 70 6f 72 74 29 20 7b 0a 09 73 74 72 75  nt port) {..stru
0a80: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20  ct sockaddr_in6 
0a90: 61 64 64 72 3b 0a 09 69 6e 74 20 70 74 6f 6e 5f  addr;..int pton_
0aa0: 72 65 74 2c 20 62 69 6e 64 5f 72 65 74 2c 20 6c  ret, bind_ret, l
0ab0: 69 73 74 65 6e 5f 72 65 74 3b 0a 09 69 6e 74 20  isten_ret;..int 
0ac0: 66 64 3b 0a 0a 09 61 64 64 72 2e 73 69 6e 36 5f  fd;...addr.sin6_
0ad0: 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54  family = AF_INET
0ae0: 36 3b 0a 09 61 64 64 72 2e 73 69 6e 36 5f 66 6c  6;..addr.sin6_fl
0af0: 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a 09 61 64 64  owinfo = 0;..add
0b00: 72 2e 73 69 6e 36 5f 73 63 6f 70 65 5f 69 64 20  r.sin6_scope_id 
0b10: 3d 20 30 3b 0a 09 61 64 64 72 2e 73 69 6e 36 5f  = 0;..addr.sin6_
0b20: 70 6f 72 74 20 3d 20 68 74 6f 6e 73 28 70 6f 72  port = htons(por
0b30: 74 29 3b 0a 09 70 74 6f 6e 5f 72 65 74 20 3d 20  t);..pton_ret = 
0b40: 69 6e 65 74 5f 70 74 6f 6e 28 41 46 5f 49 4e 45  inet_pton(AF_INE
0b50: 54 36 2c 20 61 64 64 72 65 73 73 2c 20 61 64 64  T6, address, add
0b60: 72 2e 73 69 6e 36 5f 61 64 64 72 2e 73 36 5f 61  r.sin6_addr.s6_a
0b70: 64 64 72 29 3b 0a 09 69 66 20 28 70 74 6f 6e 5f  ddr);..if (pton_
0b80: 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 72 65  ret != 1) {...re
0b90: 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 66  turn(-1);..}...f
0ba0: 64 20 3d 20 73 6f 63 6b 65 74 28 41 46 5f 49 4e  d = socket(AF_IN
0bb0: 45 54 36 2c 20 53 4f 43 4b 5f 53 54 52 45 41 4d  ET6, SOCK_STREAM
0bc0: 2c 20 30 29 3b 0a 09 69 66 20 28 66 64 20 3c 20  , 0);..if (fd < 
0bd0: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 66 64  0) {...return(fd
0be0: 29 3b 0a 09 7d 0a 0a 09 62 69 6e 64 5f 72 65 74  );..}...bind_ret
0bf0: 20 3d 20 62 69 6e 64 28 66 64 2c 20 28 63 6f 6e   = bind(fd, (con
0c00: 73 74 20 73 74 72 75 63 74 20 73 6f 63 6b 61 64  st struct sockad
0c10: 64 72 20 2a 29 20 26 61 64 64 72 2c 20 73 69 7a  dr *) &addr, siz
0c20: 65 6f 66 28 61 64 64 72 29 29 3b 0a 09 69 66 20  eof(addr));..if 
0c30: 28 62 69 6e 64 5f 72 65 74 20 3c 20 30 29 20 7b  (bind_ret < 0) {
0c40: 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09  ...close(fd);...
0c50: 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a  .return(-1);..}.
0c60: 0a 09 6c 69 73 74 65 6e 5f 72 65 74 20 3d 20 6c  ..listen_ret = l
0c70: 69 73 74 65 6e 28 66 64 2c 20 31 32 38 29 3b 0a  isten(fd, 128);.
0c80: 09 69 66 20 28 6c 69 73 74 65 6e 5f 72 65 74 20  .if (listen_ret 
0c90: 21 3d 20 30 29 20 7b 0a 09 09 63 6c 6f 73 65 28  != 0) {...close(
0ca0: 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 2d  fd);....return(-
0cb0: 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28  1);..}...return(
0cc0: 66 64 29 3b 0a 7d 0a 0a 2f 2a 20 4c 6f 67 20 61  fd);.}../* Log a
0cd0: 20 6d 65 73 73 61 67 65 20 2a 2f 0a 2f 2f 23 64   message */.//#d
0ce0: 65 66 69 6e 65 20 46 49 4c 45 44 5f 44 4f 4e 54  efine FILED_DONT
0cf0: 5f 4c 4f 47 0a 23 69 66 64 65 66 20 46 49 4c 45  _LOG.#ifdef FILE
0d00: 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 23 20 20 64 65  D_DONT_LOG.#  de
0d10: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 67 69  fine filed_loggi
0d20: 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 29  ng_thread_init()
0d30: 20 30 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c   0.#  define fil
0d40: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67  ed_log_msg_debug
0d50: 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20  (x, ...) /**/.# 
0d60: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f   define filed_lo
0d70: 67 5f 6d 73 67 28 78 29 20 2f 2a 2a 2f 0a 23 65  g_msg(x) /**/.#e
0d80: 6c 73 65 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  lse./* Initializ
0d90: 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64  e logging thread
0da0: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
0db0: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72  iled_logging_thr
0dc0: 65 61 64 5f 69 6e 69 74 28 76 6f 69 64 29 20 7b  ead_init(void) {
0dd0: 0a 09 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 55  ../* XXX:TODO: U
0de0: 6e 69 6d 70 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a  nimplemented */.
0df0: 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f  .return(0);.}../
0e00: 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 55 6e 69 6d  * XXX:TODO: Unim
0e10: 70 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a 23 64 65  plemented */.#de
0e20: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d  fine filed_log_m
0e30: 73 67 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29  sg_debug(x, ...)
0e40: 20 7b 20 66 70 72 69 6e 74 66 28 73 74 64 65 72   { fprintf(stder
0e50: 72 2c 20 78 2c 20 5f 5f 56 41 5f 41 52 47 53 5f  r, x, __VA_ARGS_
0e60: 5f 29 3b 20 66 70 72 69 6e 74 66 28 73 74 64 65  _); fprintf(stde
0e70: 72 72 2c 20 22 5c 6e 22 29 3b 20 66 66 6c 75 73  rr, "\n"); fflus
0e80: 68 28 73 74 64 65 72 72 29 3b 20 7d 0a 0a 73 74  h(stderr); }..st
0e90: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f  atic void filed_
0ea0: 6c 6f 67 5f 6d 73 67 28 63 6f 6e 73 74 20 63 68  log_msg(const ch
0eb0: 61 72 20 2a 62 75 66 66 65 72 29 20 7b 0a 09 2f  ar *buffer) {../
0ec0: 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 55 6e 69 6d  * XXX:TODO: Unim
0ed0: 70 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a 09 66 70  plemented */..fp
0ee0: 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 25  rintf(stderr, "%
0ef0: 73 5c 6e 22 2c 20 62 75 66 66 65 72 29 3b 0a 0a  s\n", buffer);..
0f00: 09 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69  .return;.}.#endi
0f10: 66 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20 74 69 6d  f../* Format tim
0f20: 65 20 70 65 72 20 52 46 43 32 36 31 36 20 2a 2f  e per RFC2616 */
0f30: 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 69  .static char *fi
0f40: 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28  led_format_time(
0f50: 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 73 69  char *buffer, si
0f60: 7a 65 5f 74 20 62 75 66 66 65 72 5f 6c 65 6e 2c  ze_t buffer_len,
0f70: 20 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 74 69   const time_t ti
0f80: 6d 65 69 6e 66 6f 29 20 7b 0a 09 73 74 72 75 63  meinfo) {..struc
0f90: 74 20 74 6d 20 74 69 6d 65 69 6e 66 6f 5f 74 6d  t tm timeinfo_tm
0fa0: 2c 20 2a 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70  , *timeinfo_tm_p
0fb0: 3b 0a 0a 09 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f  ;...timeinfo_tm_
0fc0: 70 20 3d 20 67 6d 74 69 6d 65 5f 72 28 26 74 69  p = gmtime_r(&ti
0fd0: 6d 65 69 6e 66 6f 2c 20 26 74 69 6d 65 69 6e 66  meinfo, &timeinf
0fe0: 6f 5f 74 6d 29 3b 0a 09 69 66 20 28 74 69 6d 65  o_tm);..if (time
0ff0: 69 6e 66 6f 5f 74 6d 5f 70 20 3d 3d 20 4e 55 4c  info_tm_p == NUL
1000: 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 22 75  L) {...return("u
1010: 6e 6b 6e 6f 77 6e 22 29 3b 0a 09 7d 0a 0a 09 62  nknown");..}...b
1020: 75 66 66 65 72 5b 62 75 66 66 65 72 5f 6c 65 6e  uffer[buffer_len
1030: 20 2d 20 31 5d 20 3d 20 27 5c 30 27 3b 0a 09 62   - 1] = '\0';..b
1040: 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 74 72 66  uffer_len = strf
1050: 74 69 6d 65 28 62 75 66 66 65 72 2c 20 62 75 66  time(buffer, buf
1060: 66 65 72 5f 6c 65 6e 20 2d 20 31 2c 20 22 25 61  fer_len - 1, "%a
1070: 2c 20 25 64 20 25 62 20 25 59 20 25 48 3a 25 4d  , %d %b %Y %H:%M
1080: 3a 25 53 20 47 4d 54 22 2c 20 74 69 6d 65 69 6e  :%S GMT", timein
1090: 66 6f 5f 74 6d 5f 70 29 3b 0a 0a 09 72 65 74 75  fo_tm_p);...retu
10a0: 72 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f  rn(buffer);.}../
10b0: 2a 20 68 61 73 68 20 2a 2f 0a 2f 2a 20 58 58 58  * hash */./* XXX
10c0: 3a 54 4f 44 4f 3a 20 52 65 77 72 69 74 65 20 74  :TODO: Rewrite t
10d0: 68 69 73 20 2a 2f 0a 73 74 61 74 69 63 20 75 6e  his */.static un
10e0: 73 69 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64  signed int filed
10f0: 5f 68 61 73 68 28 63 6f 6e 73 74 20 75 6e 73 69  _hash(const unsi
1100: 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 75 65  gned char *value
1110: 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d  , unsigned int m
1120: 6f 64 75 6c 75 73 29 20 7b 0a 09 75 6e 73 69 67  odulus) {..unsig
1130: 6e 65 64 20 63 68 61 72 20 63 75 72 72 2c 20 70  ned char curr, p
1140: 72 65 76 3b 0a 09 69 6e 74 20 64 69 66 66 3b 0a  rev;..int diff;.
1150: 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 65  .unsigned int re
1160: 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d  tval;...retval =
1170: 20 6d 6f 64 75 6c 75 73 20 2d 20 31 3b 0a 09 70   modulus - 1;..p
1180: 72 65 76 20 3d 20 6d 6f 64 75 6c 75 73 20 25 20  rev = modulus % 
1190: 32 35 35 3b 0a 0a 09 77 68 69 6c 65 20 28 28 63  255;...while ((c
11a0: 75 72 72 20 3d 20 2a 76 61 6c 75 65 29 29 20 7b  urr = *value)) {
11b0: 0a 09 09 69 66 20 28 63 75 72 72 20 3c 20 33 32  ...if (curr < 32
11c0: 29 20 7b 0a 09 09 09 63 75 72 72 20 3d 20 32 35  ) {....curr = 25
11d0: 35 20 2d 20 63 75 72 72 3b 0a 09 09 7d 20 65 6c  5 - curr;...} el
11e0: 73 65 20 7b 0a 09 09 09 63 75 72 72 20 2d 3d 20  se {....curr -= 
11f0: 33 32 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 70  32;...}....if (p
1200: 72 65 76 20 3c 20 63 75 72 72 29 20 7b 0a 09 09  rev < curr) {...
1210: 09 64 69 66 66 20 3d 20 63 75 72 72 20 2d 20 70  .diff = curr - p
1220: 72 65 76 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a  rev;...} else {.
1230: 09 09 09 64 69 66 66 20 3d 20 70 72 65 76 20 2d  ...diff = prev -
1240: 20 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 72 65   curr;...}....re
1250: 74 76 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 65  tval <<= 3;...re
1260: 74 76 61 6c 20 5e 3d 20 64 69 66 66 3b 0a 0a 09  tval ^= diff;...
1270: 09 76 61 6c 75 65 2b 2b 3b 0a 09 7d 0a 0a 09 72  .value++;..}...r
1280: 65 74 76 61 6c 20 3d 20 72 65 74 76 61 6c 20 25  etval = retval %
1290: 20 6d 6f 64 75 6c 75 73 3b 0a 0a 09 72 65 74 75   modulus;...retu
12a0: 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 2f  rn(retval);.}../
12b0: 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 20 61 6e  * Open a file an
12c0: 64 20 72 65 74 75 72 6e 20 66 69 6c 65 20 69 6e  d return file in
12d0: 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74 61  formation */.sta
12e0: 74 69 63 20 73 74 72 75 63 74 20 66 69 6c 65 64  tic struct filed
12f0: 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 64  _fileinfo *filed
1300: 5f 6f 70 65 6e 5f 66 69 6c 65 28 63 6f 6e 73 74  _open_file(const
1310: 20 63 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72   char *path, str
1320: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e  uct filed_filein
1330: 66 6f 20 2a 62 75 66 66 65 72 29 20 7b 0a 09 73  fo *buffer) {..s
1340: 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65  truct filed_file
1350: 69 6e 66 6f 20 2a 63 61 63 68 65 3b 0a 09 75 6e  info *cache;..un
1360: 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63 68 65  signed int cache
1370: 5f 69 64 78 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e  _idx;..off_t len
1380: 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 09 63 61 63  ;..int fd;...cac
1390: 68 65 5f 69 64 78 20 3d 20 66 69 6c 65 64 5f 68  he_idx = filed_h
13a0: 61 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67  ash((const unsig
13b0: 6e 65 64 20 63 68 61 72 20 2a 29 20 70 61 74 68  ned char *) path
13c0: 2c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  , filed_fileinfo
13d0: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  _fdcache_size);.
13e0: 0a 09 63 61 63 68 65 20 3d 20 26 66 69 6c 65 64  ..cache = &filed
13f0: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68  _fileinfo_fdcach
1400: 65 5b 63 61 63 68 65 5f 69 64 78 5d 3b 0a 0a 09  e[cache_idx];...
1410: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65  filed_log_msg_de
1420: 62 75 67 28 22 4c 6f 63 6b 69 6e 67 20 6d 75 74  bug("Locking mut
1430: 65 78 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 22  ex for idx: %lu"
1440: 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  , (unsigned long
1450: 29 20 63 61 63 68 65 5f 69 64 78 29 3b 0a 0a 09  ) cache_idx);...
1460: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f  pthread_mutex_lo
1470: 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78  ck(&cache->mutex
1480: 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  );...filed_log_m
1490: 73 67 5f 64 65 62 75 67 28 22 43 6f 6d 70 6c 65  sg_debug("Comple
14a0: 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6d 75 74 65  ted locking mute
14b0: 78 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 22 2c  x for idx: %lu",
14c0: 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29   (unsigned long)
14d0: 20 63 61 63 68 65 5f 69 64 78 29 3b 0a 0a 09 69   cache_idx);...i
14e0: 66 20 28 73 74 72 63 6d 70 28 70 61 74 68 2c 20  f (strcmp(path, 
14f0: 63 61 63 68 65 2d 3e 70 61 74 68 29 20 21 3d 20  cache->path) != 
1500: 30 29 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67  0) {...filed_log
1510: 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 61 63 68  _msg_debug("Cach
1520: 65 20 6d 69 73 73 20 66 6f 72 20 69 64 78 3a 20  e miss for idx: 
1530: 25 6c 75 3a 20 4f 4c 44 20 5c 22 25 73 5c 22 2c  %lu: OLD \"%s\",
1540: 20 4e 45 57 20 5c 22 25 73 5c 22 22 2c 20 28 75   NEW \"%s\"", (u
1550: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61  nsigned long) ca
1560: 63 68 65 5f 69 64 78 2c 20 63 61 63 68 65 2d 3e  che_idx, cache->
1570: 70 61 74 68 2c 20 70 61 74 68 29 3b 0a 0a 09 09  path, path);....
1580: 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20  fd = open(path, 
1590: 4f 5f 52 44 4f 4e 4c 59 20 7c 20 4f 5f 4c 41 52  O_RDONLY | O_LAR
15a0: 47 45 46 49 4c 45 29 3b 0a 09 09 69 66 20 28 66  GEFILE);...if (f
15b0: 64 20 3c 20 30 29 20 7b 0a 09 09 09 70 74 68 72  d < 0) {....pthr
15c0: 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b  ead_mutex_unlock
15d0: 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b  (&cache->mutex);
15e0: 0a 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c  .....return(NULL
15f0: 29 3b 0a 09 09 7d 0a 0a 09 09 66 72 65 65 28 63  );...}....free(c
1600: 61 63 68 65 2d 3e 70 61 74 68 29 3b 0a 09 09 69  ache->path);...i
1610: 66 20 28 63 61 63 68 65 2d 3e 66 64 20 3e 3d 20  f (cache->fd >= 
1620: 30 29 20 7b 0a 09 09 09 63 6c 6f 73 65 28 63 61  0) {....close(ca
1630: 63 68 65 2d 3e 66 64 29 3b 0a 09 09 7d 0a 0a 09  che->fd);...}...
1640: 09 6c 65 6e 20 3d 20 6c 73 65 65 6b 28 66 64 2c  .len = lseek(fd,
1650: 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29 3b 0a 09   0, SEEK_END);..
1660: 09 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 53 45  .lseek(fd, 0, SE
1670: 45 4b 5f 53 45 54 29 3b 0a 0a 09 09 63 61 63 68  EK_SET);....cach
1680: 65 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 09 63 61  e->fd = fd;...ca
1690: 63 68 65 2d 3e 6c 65 6e 20 3d 20 6c 65 6e 3b 0a  che->len = len;.
16a0: 09 09 63 61 63 68 65 2d 3e 70 61 74 68 20 3d 20  ..cache->path = 
16b0: 73 74 72 64 75 70 28 70 61 74 68 29 3b 0a 0a 09  strdup(path);...
16c0: 09 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 44 65  ./* XXX:TODO: De
16d0: 74 65 72 6d 69 6e 65 20 2a 2f 0a 09 09 63 61 63  termine */...cac
16e0: 68 65 2d 3e 74 79 70 65 20 3d 20 22 76 69 64 65  he->type = "vide
16f0: 6f 2f 6d 70 34 22 3b 0a 09 09 63 61 63 68 65 2d  o/mp4";...cache-
1700: 3e 6c 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 64  >lastmod = filed
1710: 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 63  _format_time(cac
1720: 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73  he->lastmod_b, s
1730: 69 7a 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 73  izeof(cache->las
1740: 74 6d 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e 55  tmod_b), time(NU
1750: 4c 4c 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 6c  LL) - 30);..} el
1760: 73 65 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67  se {...filed_log
1770: 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 61 63 68  _msg_debug("Cach
1780: 65 20 68 69 74 20 66 6f 72 20 69 64 78 3a 20 25  e hit for idx: %
1790: 6c 75 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 22  lu: PATH \"%s\""
17a0: 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  , (unsigned long
17b0: 29 20 63 61 63 68 65 5f 69 64 78 2c 20 70 61 74  ) cache_idx, pat
17c0: 68 29 3b 0a 09 7d 0a 0a 09 2f 2a 0a 09 20 2a 20  h);..}.../*.. * 
17d0: 57 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20  We have to make 
17e0: 61 20 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20  a duplicate FD, 
17f0: 62 65 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20  because once we 
1800: 72 65 6c 65 61 73 65 20 74 68 65 20 63 61 63 68  release the cach
1810: 65 0a 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 65  e.. * mutex, the
1820: 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1830: 20 6d 61 79 20 62 65 20 63 6c 6f 73 65 64 0a 09   may be closed..
1840: 20 2a 2f 0a 09 66 64 20 3d 20 64 75 70 28 63 61   */..fd = dup(ca
1850: 63 68 65 2d 3e 66 64 29 3b 0a 09 69 66 20 28 66  che->fd);..if (f
1860: 64 20 3c 20 30 29 20 7b 0a 09 09 70 74 68 72 65  d < 0) {...pthre
1870: 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28  ad_mutex_unlock(
1880: 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a  &cache->mutex);.
1890: 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b  ...return(NULL);
18a0: 0a 09 7d 0a 0a 09 62 75 66 66 65 72 2d 3e 66 64  ..}...buffer->fd
18b0: 20 3d 20 66 64 3b 0a 09 62 75 66 66 65 72 2d 3e   = fd;..buffer->
18c0: 6c 65 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e  len = cache->len
18d0: 3b 0a 09 62 75 66 66 65 72 2d 3e 74 79 70 65 20  ;..buffer->type 
18e0: 3d 20 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a 09  = cache->type;..
18f0: 6d 65 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 6c  memcpy(buffer->l
1900: 61 73 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d  astmod_b, cache-
1910: 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65  >lastmod_b, size
1920: 6f 66 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d  of(buffer->lastm
1930: 6f 64 5f 62 29 29 3b 0a 09 62 75 66 66 65 72 2d  od_b));..buffer-
1940: 3e 6c 61 73 74 6d 6f 64 20 3d 20 62 75 66 66 65  >lastmod = buffe
1950: 72 2d 3e 6c 61 73 74 6d 6f 64 5f 62 20 2b 20 28  r->lastmod_b + (
1960: 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 2d  cache->lastmod -
1970: 20 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f   cache->lastmod_
1980: 62 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75  b);...pthread_mu
1990: 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68  tex_unlock(&cach
19a0: 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 72 65 74  e->mutex);...ret
19b0: 75 72 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a 0a  urn(buffer);.}..
19c0: 2f 2a 20 50 72 6f 63 65 73 73 20 61 6e 20 48 54  /* Process an HT
19d0: 54 50 20 72 65 71 75 65 73 74 20 61 6e 64 20 72  TP request and r
19e0: 65 74 75 72 6e 20 74 68 65 20 70 61 74 68 20 72  eturn the path r
19f0: 65 71 75 65 73 74 65 64 20 2a 2f 0a 73 74 61 74  equested */.stat
1a00: 69 63 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f  ic struct filed_
1a10: 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 66 69  http_request *fi
1a20: 6c 65 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71  led_get_http_req
1a30: 75 65 73 74 28 46 49 4c 45 20 2a 66 70 2c 20 73  uest(FILE *fp, s
1a40: 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70  truct filed_http
1a50: 5f 72 65 71 75 65 73 74 20 2a 62 75 66 66 65 72  _request *buffer
1a60: 5f 73 74 29 20 7b 0a 09 63 68 61 72 20 2a 6d 65  _st) {..char *me
1a70: 74 68 6f 64 2c 20 2a 70 61 74 68 3b 0a 09 63 68  thod, *path;..ch
1a80: 61 72 20 2a 62 75 66 66 65 72 2c 20 2a 74 6d 70  ar *buffer, *tmp
1a90: 62 75 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66  buffer, *workbuf
1aa0: 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72  fer, *workbuffer
1ab0: 5f 6e 65 78 74 3b 0a 09 73 69 7a 65 5f 74 20 62  _next;..size_t b
1ac0: 75 66 66 65 72 5f 6c 65 6e 2c 20 74 6d 70 62 75  uffer_len, tmpbu
1ad0: 66 66 65 72 5f 6c 65 6e 3b 0a 09 6f 66 66 5f 74  ffer_len;..off_t
1ae0: 20 72 61 6e 67 65 5f 73 74 61 72 74 2c 20 72 61   range_start, ra
1af0: 6e 67 65 5f 65 6e 64 2c 20 72 61 6e 67 65 5f 6c  nge_end, range_l
1b00: 65 6e 67 74 68 3b 0a 09 69 6e 74 20 72 61 6e 67  ength;..int rang
1b10: 65 5f 72 65 71 75 65 73 74 3b 0a 09 69 6e 74 20  e_request;..int 
1b20: 69 3b 0a 0a 09 72 61 6e 67 65 5f 73 74 61 72 74  i;...range_start
1b30: 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 65 6e 64   = 0;..range_end
1b40: 20 20 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 72     = 0;..range_r
1b50: 65 71 75 65 73 74 20 3d 20 30 3b 0a 09 72 61 6e  equest = 0;..ran
1b60: 67 65 5f 6c 65 6e 67 74 68 20 3d 20 2d 31 3b 0a  ge_length = -1;.
1b70: 0a 09 62 75 66 66 65 72 20 3d 20 62 75 66 66 65  ..buffer = buffe
1b80: 72 5f 73 74 2d 3e 70 61 74 68 5f 62 3b 0a 09 62  r_st->path_b;..b
1b90: 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 69 7a 65  uffer_len = size
1ba0: 6f 66 28 62 75 66 66 65 72 5f 73 74 2d 3e 70 61  of(buffer_st->pa
1bb0: 74 68 5f 62 29 3b 0a 0a 09 74 6d 70 62 75 66 66  th_b);...tmpbuff
1bc0: 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e  er = buffer_st->
1bd0: 74 6d 70 62 75 66 3b 0a 09 74 6d 70 62 75 66 66  tmpbuf;..tmpbuff
1be0: 65 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28  er_len = sizeof(
1bf0: 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75  buffer_st->tmpbu
1c00: 66 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f  f);...filed_log_
1c10: 6d 73 67 28 22 57 41 49 54 5f 46 4f 52 5f 52 45  msg("WAIT_FOR_RE
1c20: 51 55 45 53 54 20 46 44 3d 2e 2e 2e 22 29 3b 0a  QUEST FD=...");.
1c30: 0a 09 66 67 65 74 73 28 62 75 66 66 65 72 2c 20  ..fgets(buffer, 
1c40: 62 75 66 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b  buffer_len, fp);
1c50: 0a 0a 09 6d 65 74 68 6f 64 20 3d 20 62 75 66 66  ...method = buff
1c60: 65 72 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 73  er;...buffer = s
1c70: 74 72 63 68 72 28 62 75 66 66 65 72 2c 20 27 20  trchr(buffer, ' 
1c80: 27 29 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20  ');..if (buffer 
1c90: 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c  == NULL) {...fil
1ca0: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 47 4f 54 5f  ed_log_msg("GOT_
1cb0: 52 45 51 55 45 53 54 20 46 44 3d 2e 2e 2e 20 45  REQUEST FD=... E
1cc0: 52 52 4f 52 3d 66 6f 72 6d 61 74 22 29 3b 0a 0a  RROR=format");..
1cd0: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a  ..return(NULL);.
1ce0: 09 7d 0a 0a 09 2a 62 75 66 66 65 72 20 3d 20 27  .}...*buffer = '
1cf0: 5c 30 27 3b 0a 09 62 75 66 66 65 72 2b 2b 3b 0a  \0';..buffer++;.
1d00: 0a 09 70 61 74 68 20 3d 20 62 75 66 66 65 72 3b  ..path = buffer;
1d10: 0a 0a 09 62 75 66 66 65 72 20 3d 20 73 74 72 63  ...buffer = strc
1d20: 68 72 28 62 75 66 66 65 72 2c 20 27 20 27 29 3b  hr(buffer, ' ');
1d30: 0a 09 69 66 20 28 62 75 66 66 65 72 20 21 3d 20  ..if (buffer != 
1d40: 4e 55 4c 4c 29 20 7b 0a 09 09 2a 62 75 66 66 65  NULL) {...*buffe
1d50: 72 20 3d 20 27 5c 30 27 3b 0a 09 09 62 75 66 66  r = '\0';...buff
1d60: 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64  er++;..}...filed
1d70: 5f 6c 6f 67 5f 6d 73 67 28 22 47 4f 54 5f 52 45  _log_msg("GOT_RE
1d80: 51 55 45 53 54 20 46 44 3d 2e 2e 2e 20 50 41 54  QUEST FD=... PAT
1d90: 48 3d 2e 2e 2e 22 29 3b 0a 0a 09 66 69 6c 65 64  H=...");...filed
1da0: 5f 6c 6f 67 5f 6d 73 67 28 22 57 41 49 54 5f 46  _log_msg("WAIT_F
1db0: 4f 52 5f 48 45 41 44 45 52 53 20 46 44 3d 2e 2e  OR_HEADERS FD=..
1dc0: 2e 22 29 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20  .");...for (i = 
1dd0: 30 3b 20 69 20 3c 20 31 30 30 3b 20 69 2b 2b 29  0; i < 100; i++)
1de0: 20 7b 0a 09 09 66 67 65 74 73 28 74 6d 70 62 75   {...fgets(tmpbu
1df0: 66 66 65 72 2c 20 74 6d 70 62 75 66 66 65 72 5f  ffer, tmpbuffer_
1e00: 6c 65 6e 2c 20 66 70 29 3b 0a 0a 09 09 69 66 20  len, fp);....if 
1e10: 28 73 74 72 6e 63 61 73 65 63 6d 70 28 74 6d 70  (strncasecmp(tmp
1e20: 62 75 66 66 65 72 2c 20 22 52 61 6e 67 65 3a 20  buffer, "Range: 
1e30: 22 2c 20 37 29 20 3d 3d 20 30 29 20 7b 0a 09 09  ", 7) == 0) {...
1e40: 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 74 6d  .workbuffer = tm
1e50: 70 62 75 66 66 65 72 20 2b 20 37 3b 0a 0a 09 09  pbuffer + 7;....
1e60: 09 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70  .if (strncasecmp
1e70: 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 22 62 79  (workbuffer, "by
1e80: 74 65 73 3d 22 2c 20 36 29 20 3d 3d 20 30 29 20  tes=", 6) == 0) 
1e90: 7b 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72  {.....workbuffer
1ea0: 20 2b 3d 20 36 3b 0a 0a 09 09 09 09 72 61 6e 67   += 6;......rang
1eb0: 65 5f 72 65 71 75 65 73 74 20 3d 20 31 3b 0a 0a  e_request = 1;..
1ec0: 09 09 09 09 72 61 6e 67 65 5f 73 74 61 72 74 20  ....range_start 
1ed0: 3d 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62  = strtoull(workb
1ee0: 75 66 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 66  uffer, &workbuff
1ef0: 65 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 0a 09  er_next, 10);...
1f00: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20  ...workbuffer = 
1f10: 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b  workbuffer_next;
1f20: 0a 0a 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62  ......if (*workb
1f30: 75 66 66 65 72 20 3d 3d 20 27 2d 27 29 20 7b 0a  uffer == '-') {.
1f40: 09 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 2b  .....workbuffer+
1f50: 2b 3b 0a 0a 09 09 09 09 09 69 66 20 28 2a 77 6f  +;.......if (*wo
1f60: 72 6b 62 75 66 66 65 72 20 21 3d 20 27 5c 72 27  rkbuffer != '\r'
1f70: 20 26 26 20 2a 77 6f 72 6b 62 75 66 66 65 72 20   && *workbuffer 
1f80: 21 3d 20 27 5c 6e 27 29 20 7b 0a 09 09 09 09 09  != '\n') {......
1f90: 09 72 61 6e 67 65 5f 65 6e 64 20 3d 20 73 74 72  .range_end = str
1fa0: 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72  toull(workbuffer
1fb0: 2c 20 26 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65  , &workbuffer_ne
1fc0: 78 74 2c 20 31 30 29 3b 0a 09 09 09 09 09 7d 0a  xt, 10);......}.
1fd0: 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a 0a  ....}....}...}..
1fe0: 09 09 69 66 20 28 6d 65 6d 63 6d 70 28 74 6d 70  ..if (memcmp(tmp
1ff0: 62 75 66 66 65 72 2c 20 22 5c 72 5c 6e 22 2c 20  buffer, "\r\n", 
2000: 32 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72  2) == 0) {....br
2010: 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 66 69  eak;...}..}...fi
2020: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 47 4f 54  led_log_msg("GOT
2030: 5f 48 45 41 44 45 52 53 20 46 44 3d 2e 2e 2e 22  _HEADERS FD=..."
2040: 29 3b 0a 0a 09 2f 2a 20 57 65 20 6f 6e 6c 79 20  );.../* We only 
2050: 68 61 6e 64 6c 65 20 74 68 65 20 22 47 45 54 22  handle the "GET"
2060: 20 6d 65 74 68 6f 64 20 2a 2f 0a 09 69 66 20 28   method */..if (
2070: 73 74 72 63 61 73 65 63 6d 70 28 6d 65 74 68 6f  strcasecmp(metho
2080: 64 2c 20 22 67 65 74 22 29 20 21 3d 20 30 29 20  d, "get") != 0) 
2090: 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29  {...return(NULL)
20a0: 3b 0a 09 7d 0a 0a 09 2f 2a 20 44 65 74 65 72 6d  ;..}.../* Determ
20b0: 69 6e 65 20 72 61 6e 67 65 20 2a 2f 0a 09 69 66  ine range */..if
20c0: 20 28 72 61 6e 67 65 5f 65 6e 64 20 21 3d 20 30   (range_end != 0
20d0: 29 20 7b 0a 09 09 69 66 20 28 72 61 6e 67 65 5f  ) {...if (range_
20e0: 65 6e 64 20 3c 3d 20 72 61 6e 67 65 5f 73 74 61  end <= range_sta
20f0: 72 74 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28  rt) {....return(
2100: 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 72 61  NULL);...}....ra
2110: 6e 67 65 5f 6c 65 6e 67 74 68 20 3d 20 72 61 6e  nge_length = ran
2120: 67 65 5f 65 6e 64 20 2d 20 72 61 6e 67 65 5f 73  ge_end - range_s
2130: 74 61 72 74 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c  tart;....filed_l
2140: 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f  og_msg_debug("Co
2150: 6d 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 70  mputing length p
2160: 61 72 61 6d 65 74 65 72 3a 20 25 6c 6c 75 20 3d  arameter: %llu =
2170: 20 25 6c 6c 75 20 2d 20 25 6c 6c 75 22 2c 0a 09   %llu - %llu",..
2180: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
2190: 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 6c 65 6e   long) range_len
21a0: 67 74 68 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65  gth,....(unsigne
21b0: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e  d long long) ran
21c0: 67 65 5f 65 6e 64 2c 0a 09 09 09 28 75 6e 73 69  ge_end,....(unsi
21d0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
21e0: 72 61 6e 67 65 5f 73 74 61 72 74 0a 09 09 29 3b  range_start...);
21f0: 0a 09 7d 0a 0a 09 2f 2a 20 46 69 6c 6c 20 75 70  ..}.../* Fill up
2200: 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 72 65   structure to re
2210: 74 75 72 6e 20 2a 2f 0a 09 62 75 66 66 65 72 5f  turn */..buffer_
2220: 73 74 2d 3e 70 61 74 68 20 20 20 3d 20 70 61 74  st->path   = pat
2230: 68 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68  h;..buffer_st->h
2240: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65  eaders.range.pre
2250: 73 65 6e 74 20 3d 20 72 61 6e 67 65 5f 72 65 71  sent = range_req
2260: 75 65 73 74 3b 0a 09 62 75 66 66 65 72 5f 73 74  uest;..buffer_st
2270: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
2280: 6f 66 66 73 65 74 20 20 3d 20 72 61 6e 67 65 5f  offset  = range_
2290: 73 74 61 72 74 3b 0a 09 62 75 66 66 65 72 5f 73  start;..buffer_s
22a0: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65  t->headers.range
22b0: 2e 6c 65 6e 67 74 68 20 20 3d 20 72 61 6e 67 65  .length  = range
22c0: 5f 6c 65 6e 67 74 68 3b 0a 0a 09 72 65 74 75 72  _length;...retur
22d0: 6e 28 62 75 66 66 65 72 5f 73 74 29 3b 0a 7d 0a  n(buffer_st);.}.
22e0: 0a 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72  ./* Return an er
22f0: 72 6f 72 20 70 61 67 65 20 2a 2f 0a 73 74 61 74  ror page */.stat
2300: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 65 72  ic void filed_er
2310: 72 6f 72 5f 70 61 67 65 28 46 49 4c 45 20 2a 66  ror_page(FILE *f
2320: 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64  p, const char *d
2330: 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 69 6e 74  ate_current, int
2340: 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 29 20 7b   error_number) {
2350: 0a 09 63 68 61 72 20 2a 65 72 72 6f 72 5f 73 74  ..char *error_st
2360: 72 69 6e 67 20 3d 20 22 3c 68 74 6d 6c 3e 3c 68  ring = "<html><h
2370: 65 61 64 3e 3c 74 69 74 6c 65 3e 45 52 52 4f 52  ead><title>ERROR
2380: 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 3e 3c  </title></head><
2390: 62 6f 64 79 3e 55 6e 61 62 6c 65 20 74 6f 20 70  body>Unable to p
23a0: 72 6f 63 65 73 73 20 72 65 71 75 65 73 74 3c 2f  rocess request</
23b0: 62 6f 64 79 3e 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a  body></html>";..
23c0: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54  .fprintf(fp, "HT
23d0: 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e  TP/1.1 %i OK\r\n
23e0: 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76  Date: %s\r\nServ
23f0: 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73  er: filed\r\nLas
2400: 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c 72  t-Modified: %s\r
2410: 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68  \nContent-Length
2420: 3a 20 25 6c 6c 75 5c 72 5c 6e 43 6f 6e 74 65 6e  : %llu\r\nConten
2430: 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e 43 6f  t-Type: %s\r\nCo
2440: 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c  nnection: close\
2450: 72 5c 6e 5c 72 5c 6e 25 73 22 2c 0a 09 09 65 72  r\n\r\n%s",...er
2460: 72 6f 72 5f 6e 75 6d 62 65 72 2c 0a 09 09 64 61  ror_number,...da
2470: 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64 61  te_current,...da
2480: 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 28 75  te_current,...(u
2490: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
24a0: 67 29 20 73 74 72 6c 65 6e 28 65 72 72 6f 72 5f  g) strlen(error_
24b0: 73 74 72 69 6e 67 29 2c 0a 09 09 22 74 65 78 74  string),..."text
24c0: 2f 68 74 6d 6c 22 2c 0a 09 09 65 72 72 6f 72 5f  /html",...error_
24d0: 73 74 72 69 6e 67 0a 09 29 3b 0a 7d 0a 0a 2f 2a  string..);.}../*
24e0: 20 48 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c 65   Handle a single
24f0: 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 61 20   request from a 
2500: 63 6c 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 63  client */.static
2510: 20 76 6f 69 64 20 66 69 6c 65 64 5f 68 61 6e 64   void filed_hand
2520: 6c 65 5f 63 6c 69 65 6e 74 28 69 6e 74 20 66 64  le_client(int fd
2530: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68  , struct filed_h
2540: 74 74 70 5f 72 65 71 75 65 73 74 20 2a 72 65 71  ttp_request *req
2550: 75 65 73 74 29 20 7b 0a 09 73 74 72 75 63 74 20  uest) {..struct 
2560: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a  filed_fileinfo *
2570: 66 69 6c 65 69 6e 66 6f 3b 0a 09 73 73 69 7a 65  fileinfo;..ssize
2580: 5f 74 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b  _t sendfile_ret;
2590: 0a 09 73 69 7a 65 5f 74 20 73 65 6e 64 66 69 6c  ..size_t sendfil
25a0: 65 5f 6c 65 6e 2c 20 73 65 6e 64 66 69 6c 65 5f  e_len, sendfile_
25b0: 73 65 6e 74 2c 20 73 65 6e 64 66 69 6c 65 5f 73  sent, sendfile_s
25c0: 69 7a 65 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 64  ize;..off_t send
25d0: 66 69 6c 65 5f 6f 66 66 73 65 74 3b 0a 09 63 68  file_offset;..ch
25e0: 61 72 20 2a 70 61 74 68 3b 0a 09 63 68 61 72 20  ar *path;..char 
25f0: 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 64  *date_current, d
2600: 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 5b 36 34  ate_current_b[64
2610: 5d 3b 0a 09 69 6e 74 20 68 74 74 70 5f 63 6f 64  ];..int http_cod
2620: 65 3b 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09  e;..FILE *fp;...
2630: 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 63 75 72  /* Determine cur
2640: 72 65 6e 74 20 74 69 6d 65 20 2a 2f 0a 09 64 61  rent time */..da
2650: 74 65 5f 63 75 72 72 65 6e 74 20 3d 20 66 69 6c  te_current = fil
2660: 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 64  ed_format_time(d
2670: 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 2c 20 73  ate_current_b, s
2680: 69 7a 65 6f 66 28 64 61 74 65 5f 63 75 72 72 65  izeof(date_curre
2690: 6e 74 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c  nt_b), time(NULL
26a0: 29 29 3b 0a 0a 09 2f 2a 20 4f 70 65 6e 20 73 6f  ));.../* Open so
26b0: 63 6b 65 74 20 61 73 20 41 4e 53 49 20 49 2f 4f  cket as ANSI I/O
26c0: 20 66 6f 72 20 65 61 73 65 20 6f 66 20 75 73 65   for ease of use
26d0: 20 2a 2f 0a 09 66 70 20 3d 20 66 64 6f 70 65 6e   */..fp = fdopen
26e0: 28 66 64 2c 20 22 77 2b 62 22 29 3b 0a 09 69 66  (fd, "w+b");..if
26f0: 20 28 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a   (fp == NULL) {.
2700: 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09  ..close(fd);....
2710: 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 72 65 71  return;..}...req
2720: 75 65 73 74 20 3d 20 66 69 6c 65 64 5f 67 65 74  uest = filed_get
2730: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 28 66 70  _http_request(fp
2740: 2c 20 72 65 71 75 65 73 74 29 3b 0a 0a 09 66 69  , request);...fi
2750: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 50 52 4f  led_log_msg("PRO
2760: 43 45 53 53 5f 52 45 50 4c 59 5f 53 54 41 52 54  CESS_REPLY_START
2770: 20 46 44 3d 2e 2e 2e 20 50 41 54 48 3d 2e 2e 2e   FD=... PATH=...
2780: 20 52 41 4e 47 45 5f 53 54 41 52 54 3d 2e 2e 2e   RANGE_START=...
2790: 20 52 41 4e 47 45 5f 4c 45 4e 47 54 48 3d 2e 2e   RANGE_LENGTH=..
27a0: 2e 22 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 65  .");...if (reque
27b0: 73 74 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 72 65  st == NULL || re
27c0: 71 75 65 73 74 2d 3e 70 61 74 68 20 3d 3d 20 4e  quest->path == N
27d0: 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65  ULL) {...filed_e
27e0: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61  rror_page(fp, da
27f0: 74 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 30 29  te_current, 500)
2800: 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  ;....filed_log_m
2810: 73 67 28 22 50 52 4f 43 45 53 53 5f 52 45 50 4c  sg("PROCESS_REPL
2820: 59 5f 43 4f 4d 50 4c 45 54 45 20 46 44 3d 2e 2e  Y_COMPLETE FD=..
2830: 2e 20 45 52 52 4f 52 3d 35 30 30 22 29 3b 0a 0a  . ERROR=500");..
2840: 09 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09  ..fclose(fp);...
2850: 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 70 61  .return;..}...pa
2860: 74 68 20 3d 20 72 65 71 75 65 73 74 2d 3e 70 61  th = request->pa
2870: 74 68 3b 0a 0a 09 68 74 74 70 5f 63 6f 64 65 20  th;...http_code 
2880: 3d 20 2d 31 3b 0a 0a 09 66 69 6c 65 69 6e 66 6f  = -1;...fileinfo
2890: 20 3d 20 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69   = filed_open_fi
28a0: 6c 65 28 70 61 74 68 2c 20 26 72 65 71 75 65 73  le(path, &reques
28b0: 74 2d 3e 66 69 6c 65 69 6e 66 6f 29 3b 0a 09 69  t->fileinfo);..i
28c0: 66 20 28 66 69 6c 65 69 6e 66 6f 20 3d 3d 20 4e  f (fileinfo == N
28d0: 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65  ULL) {...filed_e
28e0: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61  rror_page(fp, da
28f0: 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 30 34 29  te_current, 404)
2900: 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  ;....filed_log_m
2910: 73 67 28 22 50 52 4f 43 45 53 53 5f 52 45 50 4c  sg("PROCESS_REPL
2920: 59 5f 43 4f 4d 50 4c 45 54 45 20 46 44 3d 2e 2e  Y_COMPLETE FD=..
2930: 2e 20 45 52 52 4f 52 3d 34 30 34 22 29 3b 0a 09  . ERROR=404");..
2940: 7d 20 65 6c 73 65 20 7b 0a 09 09 69 66 20 28 72  } else {...if (r
2950: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
2960: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 21 3d 20  range.offset != 
2970: 30 20 7c 7c 20 72 65 71 75 65 73 74 2d 3e 68 65  0 || request->he
2980: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
2990: 74 68 20 3e 3d 20 30 29 20 7b 0a 09 09 09 69 66  th >= 0) {....if
29a0: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65   (request->heade
29b0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20  rs.range.offset 
29c0: 3e 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e  >= fileinfo->len
29d0: 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f 6c 6f  ) {.....filed_lo
29e0: 67 5f 6d 73 67 28 22 50 52 4f 43 45 53 53 5f 52  g_msg("PROCESS_R
29f0: 45 50 4c 59 5f 43 4f 4d 50 4c 45 54 45 20 46 44  EPLY_COMPLETE FD
2a00: 3d 2e 2e 2e 20 45 52 52 4f 52 3d 34 31 36 22 29  =... ERROR=416")
2a10: 3b 0a 0a 09 09 09 09 66 69 6c 65 64 5f 65 72 72  ;......filed_err
2a20: 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65  or_page(fp, date
2a30: 5f 63 75 72 72 65 6e 74 2c 20 34 31 36 29 3b 0a  _current, 416);.
2a40: 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09  ...} else {.....
2a50: 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61  if (request->hea
2a60: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74  ders.range.lengt
2a70: 68 20 3c 20 30 29 20 7b 0a 09 09 09 09 09 66 69  h < 0) {......fi
2a80: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75  led_log_msg_debu
2a90: 67 28 22 43 6f 6d 70 75 74 69 6e 67 20 6c 65 6e  g("Computing len
2aa0: 67 74 68 20 74 6f 20 66 69 74 20 69 6e 20 62 6f  gth to fit in bo
2ab0: 75 6e 64 73 3a 20 66 69 6c 65 69 6e 66 6f 2d 3e  unds: fileinfo->
2ac0: 6c 65 6e 20 3d 20 25 6c 6c 75 2c 20 72 65 71 75  len = %llu, requ
2ad0: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
2ae0: 67 65 2e 6f 66 66 73 65 74 20 3d 20 25 6c 6c 75  ge.offset = %llu
2af0: 22 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e  ",.......(unsign
2b00: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69  ed long long) fi
2b10: 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 2c 0a 09 09 09  leinfo->len,....
2b20: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
2b30: 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d  g long) request-
2b40: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f  >headers.range.o
2b50: 66 66 73 65 74 0a 09 09 09 09 09 29 3b 0a 0a 09  ffset......);...
2b60: 09 09 09 09 72 65 71 75 65 73 74 2d 3e 68 65 61  ....request->hea
2b70: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74  ders.range.lengt
2b80: 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65  h = fileinfo->le
2b90: 6e 20 2d 20 72 65 71 75 65 73 74 2d 3e 68 65 61  n - request->hea
2ba0: 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65  ders.range.offse
2bb0: 74 3b 0a 09 09 09 09 7d 0a 0a 09 09 09 09 66 69  t;.....}......fi
2bc0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75  led_log_msg_debu
2bd0: 67 28 22 50 61 72 74 69 61 6c 20 72 65 71 75 65  g("Partial reque
2be0: 73 74 2c 20 73 74 61 72 74 69 6e 67 20 61 74 3a  st, starting at:
2bf0: 20 25 6c 6c 75 20 61 6e 64 20 72 75 6e 6e 69 6e   %llu and runnin
2c00: 67 20 66 6f 72 20 25 6c 6c 75 20 62 79 74 65 73  g for %llu bytes
2c10: 22 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65  ",......(unsigne
2c20: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71  d long long) req
2c30: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  uest->headers.ra
2c40: 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 09  nge.offset,.....
2c50: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
2c60: 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68  long) request->h
2c70: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e  eaders.range.len
2c80: 67 74 68 0a 09 09 09 09 29 3b 0a 0a 09 09 09 09  gth.....);......
2c90: 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 36 3b  http_code = 206;
2ca0: 0a 09 09 09 7d 0a 09 09 7d 20 65 6c 73 65 20 7b  ....}...} else {
2cb0: 0a 09 09 09 69 66 20 28 72 65 71 75 65 73 74 2d  ....if (request-
2cc0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 70  >headers.range.p
2cd0: 72 65 73 65 6e 74 29 20 7b 0a 09 09 09 09 68 74  resent) {.....ht
2ce0: 74 70 5f 63 6f 64 65 20 3d 20 32 30 36 3b 0a 09  tp_code = 206;..
2cf0: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 68  ..} else {.....h
2d00: 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 30 3b 0a  ttp_code = 200;.
2d10: 09 09 09 7d 0a 09 09 09 72 65 71 75 65 73 74 2d  ...}....request-
2d20: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f  >headers.range.o
2d30: 66 66 73 65 74 20 3d 20 30 3b 0a 09 09 09 72 65  ffset = 0;....re
2d40: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
2d50: 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20 66 69  ange.length = fi
2d60: 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 09 09 7d  leinfo->len;...}
2d70: 0a 0a 09 09 69 66 20 28 68 74 74 70 5f 63 6f 64  ....if (http_cod
2d80: 65 20 3e 20 30 29 20 7b 0a 09 09 09 66 70 72 69  e > 0) {....fpri
2d90: 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e  ntf(fp, "HTTP/1.
2da0: 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a  1 %i OK\r\nDate:
2db0: 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66   %s\r\nServer: f
2dc0: 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64  iled\r\nLast-Mod
2dd0: 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e  ified: %s\r\nCon
2de0: 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c  tent-Length: %ll
2df0: 75 5c 72 5c 6e 41 63 63 65 70 74 2d 52 61 6e 67  u\r\nAccept-Rang
2e00: 65 73 3a 20 62 79 74 65 73 5c 72 5c 6e 43 6f 6e  es: bytes\r\nCon
2e10: 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c  tent-Type: %s\r\
2e20: 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f  nConnection: clo
2e30: 73 65 5c 72 5c 6e 22 2c 0a 09 09 09 09 68 74 74  se\r\n",.....htt
2e40: 70 5f 63 6f 64 65 2c 0a 09 09 09 09 64 61 74 65  p_code,.....date
2e50: 5f 63 75 72 72 65 6e 74 2c 0a 09 09 09 09 66 69  _current,.....fi
2e60: 6c 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d 6f 64 2c  leinfo->lastmod,
2e70: 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c  .....(unsigned l
2e80: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73  ong long) reques
2e90: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65  t->headers.range
2ea0: 2e 6c 65 6e 67 74 68 2c 0a 09 09 09 09 66 69 6c  .length,.....fil
2eb0: 65 69 6e 66 6f 2d 3e 74 79 70 65 0a 09 09 09 29  einfo->type....)
2ec0: 3b 0a 09 09 09 69 66 20 28 68 74 74 70 5f 63 6f  ;....if (http_co
2ed0: 64 65 20 3d 3d 20 32 30 36 29 20 7b 0a 09 09 09  de == 206) {....
2ee0: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 43 6f  .fprintf(fp, "Co
2ef0: 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20 62 79 74  ntent-Range: byt
2f00: 65 73 20 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c 6c  es %llu-%llu/%ll
2f10: 75 5c 72 5c 6e 22 2c 0a 09 09 09 09 09 28 75 6e  u\r\n",......(un
2f20: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
2f30: 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65  ) request->heade
2f40: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c  rs.range.offset,
2f50: 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ......(unsigned 
2f60: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 28 72 65 71 75  long long) (requ
2f70: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
2f80: 67 65 2e 6f 66 66 73 65 74 20 2b 20 72 65 71 75  ge.offset + requ
2f90: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
2fa0: 67 65 2e 6c 65 6e 67 74 68 20 2d 20 31 29 2c 0a  ge.length - 1),.
2fb0: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c  .....(unsigned l
2fc0: 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 6e  ong long) filein
2fd0: 66 6f 2d 3e 6c 65 6e 0a 09 09 09 09 29 3b 0a 09  fo->len.....);..
2fe0: 09 09 7d 0a 09 09 09 66 70 72 69 6e 74 66 28 66  ..}....fprintf(f
2ff0: 70 2c 20 22 5c 72 5c 6e 22 29 3b 0a 09 09 09 66  p, "\r\n");....f
3000: 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 09 09 66  flush(fp);.....f
3010: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 50 52  iled_log_msg("PR
3020: 4f 43 45 53 53 5f 52 45 50 4c 59 5f 43 4f 4d 50  OCESS_REPLY_COMP
3030: 4c 45 54 45 20 46 44 3d 2e 2e 2e 20 53 54 41 54  LETE FD=... STAT
3040: 55 53 3d 32 30 58 22 29 3b 0a 0a 23 69 66 64 65  US=20X");..#ifde
3050: 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b  f FILED_NONBLOCK
3060: 5f 48 54 54 50 0a 09 09 09 69 6e 74 20 73 6f 63  _HTTP....int soc
3070: 6b 65 74 5f 66 6c 61 67 73 3b 0a 09 09 09 66 64  ket_flags;....fd
3080: 5f 73 65 74 20 72 66 64 2c 20 77 66 64 3b 0a 09  _set rfd, wfd;..
3090: 09 09 63 68 61 72 20 73 69 6e 6b 62 75 66 5b 38  ..char sinkbuf[8
30a0: 31 39 32 5d 3b 0a 09 09 09 73 73 69 7a 65 5f 74  192];....ssize_t
30b0: 20 72 65 61 64 5f 72 65 74 3b 0a 0a 09 09 09 46   read_ret;.....F
30c0: 44 5f 5a 45 52 4f 28 26 72 66 64 29 3b 0a 09 09  D_ZERO(&rfd);...
30d0: 09 46 44 5f 5a 45 52 4f 28 26 77 66 64 29 3b 0a  .FD_ZERO(&wfd);.
30e0: 09 09 09 46 44 5f 53 45 54 28 66 64 2c 20 26 72  ...FD_SET(fd, &r
30f0: 66 64 29 3b 0a 09 09 09 46 44 5f 53 45 54 28 66  fd);....FD_SET(f
3100: 64 2c 20 26 77 66 64 29 3b 0a 0a 09 09 09 73 6f  d, &wfd);.....so
3110: 63 6b 65 74 5f 66 6c 61 67 73 20 3d 20 66 63 6e  cket_flags = fcn
3120: 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 4c 29 3b  tl(fd, F_GETFL);
3130: 0a 09 09 09 66 63 6e 74 6c 28 66 64 2c 20 46 5f  ....fcntl(fd, F_
3140: 53 45 54 46 4c 2c 20 73 6f 63 6b 65 74 5f 66 6c  SETFL, socket_fl
3150: 61 67 73 20 7c 20 4f 5f 4e 4f 4e 42 4c 4f 43 4b  ags | O_NONBLOCK
3160: 29 3b 0a 23 65 6e 64 69 66 0a 0a 09 09 09 66 69  );.#endif.....fi
3170: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 53 45 4e  led_log_msg("SEN
3180: 44 5f 53 54 41 52 54 20 49 46 44 3d 2e 2e 2e 20  D_START IFD=... 
3190: 4f 46 44 3d 2e 2e 2e 20 42 59 54 45 53 3d 2e 2e  OFD=... BYTES=..
31a0: 2e 22 29 3b 0a 0a 09 09 09 73 65 6e 64 66 69 6c  .");.....sendfil
31b0: 65 5f 6f 66 66 73 65 74 20 3d 20 72 65 71 75 65  e_offset = reque
31c0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
31d0: 65 2e 6f 66 66 73 65 74 3b 0a 09 09 09 73 65 6e  e.offset;....sen
31e0: 64 66 69 6c 65 5f 6c 65 6e 20 3d 20 72 65 71 75  dfile_len = requ
31f0: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e  est->headers.ran
3200: 67 65 2e 6c 65 6e 67 74 68 3b 0a 09 09 09 73 65  ge.length;....se
3210: 6e 64 66 69 6c 65 5f 73 65 6e 74 20 3d 20 30 3b  ndfile_sent = 0;
3220: 0a 09 09 09 77 68 69 6c 65 20 28 31 29 20 7b 0a  ....while (1) {.
3230: 09 09 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65  ....if (sendfile
3240: 5f 6c 65 6e 20 3e 20 46 49 4c 45 44 5f 53 45 4e  _len > FILED_SEN
3250: 44 46 49 4c 45 5f 4d 41 58 29 20 7b 0a 09 09 09  DFILE_MAX) {....
3260: 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 20  ..sendfile_size 
3270: 3d 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45  = FILED_SENDFILE
3280: 5f 4d 41 58 3b 0a 09 09 09 09 7d 20 65 6c 73 65  _MAX;.....} else
3290: 20 7b 0a 09 09 09 09 09 73 65 6e 64 66 69 6c 65   {......sendfile
32a0: 5f 73 69 7a 65 20 3d 20 73 65 6e 64 66 69 6c 65  _size = sendfile
32b0: 5f 6c 65 6e 3b 0a 09 09 09 09 7d 0a 0a 09 09 09  _len;.....}.....
32c0: 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20  .sendfile_ret = 
32d0: 73 65 6e 64 66 69 6c 65 28 66 64 2c 20 66 69 6c  sendfile(fd, fil
32e0: 65 69 6e 66 6f 2d 3e 66 64 2c 20 26 73 65 6e 64  einfo->fd, &send
32f0: 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e  file_offset, sen
3300: 64 66 69 6c 65 5f 73 69 7a 65 29 3b 0a 09 09 09  dfile_size);....
3310: 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 72 65  .if (sendfile_re
3320: 74 20 3c 3d 20 30 29 20 7b 0a 23 69 66 64 65 66  t <= 0) {.#ifdef
3330: 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f   FILED_NONBLOCK_
3340: 48 54 54 50 0a 09 09 09 09 09 69 66 20 28 65 72  HTTP......if (er
3350: 72 6e 6f 20 3d 3d 20 45 41 47 41 49 4e 29 20 7b  rno == EAGAIN) {
3360: 0a 09 09 09 09 09 09 73 65 6e 64 66 69 6c 65 5f  .......sendfile_
3370: 72 65 74 20 3d 20 30 3b 0a 0a 09 09 09 09 09 09  ret = 0;........
3380: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 09 09  while (1) {.....
3390: 09 09 09 73 65 6c 65 63 74 28 66 64 20 2b 20 31  ...select(fd + 1
33a0: 2c 20 26 72 66 64 2c 20 26 77 66 64 2c 20 4e 55  , &rfd, &wfd, NU
33b0: 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 09 09 09 09 09  LL, NULL);......
33c0: 09 09 69 66 20 28 46 44 5f 49 53 53 45 54 28 66  ..if (FD_ISSET(f
33d0: 64 2c 20 26 72 66 64 29 29 20 7b 0a 09 09 09 09  d, &rfd)) {.....
33e0: 09 09 09 09 72 65 61 64 5f 72 65 74 20 3d 20 72  ....read_ret = r
33f0: 65 61 64 28 66 64 2c 20 73 69 6e 6b 62 75 66 2c  ead(fd, sinkbuf,
3400: 20 73 69 7a 65 6f 66 28 73 69 6e 6b 62 75 66 29   sizeof(sinkbuf)
3410: 29 3b 0a 0a 09 09 09 09 09 09 09 09 69 66 20 28  );..........if (
3420: 72 65 61 64 5f 72 65 74 20 3c 3d 20 30 29 20 7b  read_ret <= 0) {
3430: 0a 09 09 09 09 09 09 09 09 09 62 72 65 61 6b 3b  ..........break;
3440: 0a 09 09 09 09 09 09 09 09 7d 0a 09 09 09 09 09  .........}......
3450: 09 09 7d 0a 0a 09 09 09 09 09 09 09 69 66 20 28  ..}.........if (
3460: 46 44 5f 49 53 53 45 54 28 66 64 2c 20 26 77 66  FD_ISSET(fd, &wf
3470: 64 29 29 20 7b 0a 09 09 09 09 09 09 09 09 72 65  d)) {.........re
3480: 61 64 5f 72 65 74 20 3d 20 31 3b 0a 0a 09 09 09  ad_ret = 1;.....
3490: 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09  .....break;.....
34a0: 09 09 09 7d 0a 09 09 09 09 09 09 7d 0a 0a 09 09  ...}.......}....
34b0: 09 09 09 09 69 66 20 28 72 65 61 64 5f 72 65 74  ....if (read_ret
34c0: 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 09 09 09   <= 0) {........
34d0: 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 7d 0a 09  break;.......}..
34e0: 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09  ....} else {....
34f0: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 7d  ...break;......}
3500: 0a 23 65 6c 73 65 0a 09 09 09 09 09 62 72 65 61  .#else......brea
3510: 6b 3b 0a 23 65 6e 64 69 66 0a 09 09 09 09 7d 0a  k;.#endif.....}.
3520: 0a 09 09 09 09 73 65 6e 64 66 69 6c 65 5f 6c 65  .....sendfile_le
3530: 6e 20 2d 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65  n -= sendfile_re
3540: 74 3b 0a 09 09 09 09 73 65 6e 64 66 69 6c 65 5f  t;.....sendfile_
3550: 73 65 6e 74 20 2b 3d 20 73 65 6e 64 66 69 6c 65  sent += sendfile
3560: 5f 72 65 74 3b 0a 09 09 09 09 69 66 20 28 73 65  _ret;.....if (se
3570: 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 3d 20 30 29  ndfile_len == 0)
3580: 20 7b 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09   {......break;..
3590: 09 09 09 7d 0a 09 09 09 7d 0a 0a 09 09 09 66 69  ...}....}.....fi
35a0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 53 45 4e  led_log_msg("SEN
35b0: 44 5f 43 4f 4d 50 4c 45 54 45 20 53 54 41 54 55  D_COMPLETE STATU
35c0: 53 3d 2e 2e 2e 20 49 46 44 3d 2e 2e 2e 20 4f 46  S=... IFD=... OF
35d0: 44 3d 2e 2e 2e 20 42 59 54 45 53 3d 2e 2e 2e 20  D=... BYTES=... 
35e0: 42 59 54 45 53 5f 53 45 4e 54 3d 2e 2e 2e 22 29  BYTES_SENT=...")
35f0: 3b 0a 09 09 7d 0a 0a 09 09 63 6c 6f 73 65 28 66  ;...}....close(f
3600: 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09  ileinfo->fd);...
3610: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22  .filed_log_msg("
3620: 43 4c 4f 53 45 5f 46 49 4c 45 20 46 44 3d 2e 2e  CLOSE_FILE FD=..
3630: 2e 22 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f  .");..}...filed_
3640: 6c 6f 67 5f 6d 73 67 28 22 43 4c 4f 53 45 5f 43  log_msg("CLOSE_C
3650: 4f 4e 4e 45 43 54 49 4f 4e 20 46 44 3d 2e 2e 2e  ONNECTION FD=...
3660: 22 29 3b 0a 0a 09 66 63 6c 6f 73 65 28 66 70 29  ");...fclose(fp)
3670: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;...return;.}../
3680: 2a 20 48 61 6e 64 6c 65 20 69 6e 63 6f 6d 69 6e  * Handle incomin
3690: 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f  g connections */
36a0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69  .static void *fi
36b0: 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61  led_worker_threa
36c0: 64 28 76 6f 69 64 20 2a 61 72 67 5f 76 29 20 7b  d(void *arg_v) {
36d0: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77  ..struct filed_w
36e0: 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67  orker_thread_arg
36f0: 73 20 2a 61 72 67 3b 0a 09 73 74 72 75 63 74 20  s *arg;..struct 
3700: 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65  filed_http_reque
3710: 73 74 20 72 65 71 75 65 73 74 3b 0a 09 73 74 72  st request;..str
3720: 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36  uct sockaddr_in6
3730: 20 61 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f   addr;..socklen_
3740: 74 20 61 64 64 72 6c 65 6e 3b 0a 09 69 6e 74 20  t addrlen;..int 
3750: 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20  failure_count = 
3760: 30 2c 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63  0, max_failure_c
3770: 6f 75 6e 74 20 3d 20 4d 41 58 5f 46 41 49 4c 55  ount = MAX_FAILU
3780: 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 6d  RE_COUNT;..int m
3790: 61 73 74 65 72 5f 66 64 2c 20 66 64 3b 0a 0a 09  aster_fd, fd;...
37a0: 2f 2a 20 52 65 61 64 20 61 72 67 75 6d 65 6e 74  /* Read argument
37b0: 73 20 2a 2f 0a 09 61 72 67 20 3d 20 61 72 67 5f  s */..arg = arg_
37c0: 76 3b 0a 0a 09 6d 61 73 74 65 72 5f 66 64 20 3d  v;...master_fd =
37d0: 20 61 72 67 2d 3e 66 64 3b 0a 0a 09 77 68 69 6c   arg->fd;...whil
37e0: 65 20 28 31 29 20 7b 0a 09 09 2f 2a 20 46 61 69  e (1) {.../* Fai
37f0: 6c 75 72 65 20 6c 6f 6f 70 20 70 72 65 76 65 6e  lure loop preven
3800: 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 20 28 66 61  tion */...if (fa
3810: 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3e 20 6d 61  ilure_count > ma
3820: 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 29  x_failure_count)
3830: 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d   {....break;...}
3840: 0a 0a 09 09 2f 2a 20 41 63 63 65 70 74 20 61 20  ..../* Accept a 
3850: 6e 65 77 20 63 6c 69 65 6e 74 20 2a 2f 0a 09 09  new client */...
3860: 61 64 64 72 6c 65 6e 20 3d 20 73 69 7a 65 6f 66  addrlen = sizeof
3870: 28 61 64 64 72 29 3b 0a 09 09 66 64 20 3d 20 61  (addr);...fd = a
3880: 63 63 65 70 74 28 6d 61 73 74 65 72 5f 66 64 2c  ccept(master_fd,
3890: 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64   (struct sockadd
38a0: 72 20 2a 29 20 26 61 64 64 72 2c 20 26 61 64 64  r *) &addr, &add
38b0: 72 6c 65 6e 29 3b 0a 0a 09 09 2f 2a 0a 09 09 20  rlen);..../*... 
38c0: 2a 20 49 66 20 77 65 20 66 61 69 6c 2c 20 6d 61  * If we fail, ma
38d0: 6b 65 20 61 20 6e 6f 74 65 20 6f 66 20 69 74 20  ke a note of it 
38e0: 73 6f 20 77 65 20 64 6f 6e 27 74 20 67 6f 20 69  so we don't go i
38f0: 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f 66 0a 09 09  nto a loop of...
3900: 20 2a 20 61 63 63 65 70 74 28 29 20 66 61 69 6c   * accept() fail
3910: 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 69 66 20 28  ing... */...if (
3920: 66 64 20 3c 20 30 29 20 7b 0a 09 09 09 2f 2a 20  fd < 0) {..../* 
3930: 4c 6f 67 20 74 68 65 20 6e 65 77 20 63 6f 6e 6e  Log the new conn
3940: 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 09 66 69 6c  ection */....fil
3950: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 43 43 45  ed_log_msg("ACCE
3960: 50 54 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 09  PT_FAILED");....
3970: 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 2b 2b  .failure_count++
3980: 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e 75 65 3b 0a  ;.....continue;.
3990: 09 09 7d 0a 0a 09 09 2f 2a 20 4c 6f 67 20 74 68  ..}..../* Log th
39a0: 65 20 6e 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e  e new connection
39b0: 20 2a 2f 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f   */...filed_log_
39c0: 6d 73 67 28 22 4e 45 57 5f 43 4f 4e 4e 45 43 54  msg("NEW_CONNECT
39d0: 49 4f 4e 20 53 52 43 5f 41 44 44 52 3d 2e 2e 2e  ION SRC_ADDR=...
39e0: 20 53 52 43 5f 50 4f 52 54 3d 2e 2e 2e 20 46 44   SRC_PORT=... FD
39f0: 3d 2e 2e 2e 22 29 3b 0a 0a 09 09 2f 2a 20 52 65  =...");..../* Re
3a00: 73 65 74 20 66 61 69 6c 75 72 65 20 63 6f 75 6e  set failure coun
3a10: 74 2a 2f 0a 09 09 66 61 69 6c 75 72 65 5f 63 6f  t*/...failure_co
3a20: 75 6e 74 20 3d 20 30 3b 0a 0a 09 09 2f 2a 20 48  unt = 0;..../* H
3a30: 61 6e 64 6c 65 20 73 6f 63 6b 65 74 20 2a 2f 0a  andle socket */.
3a40: 09 09 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63  ..filed_handle_c
3a50: 6c 69 65 6e 74 28 66 64 2c 20 26 72 65 71 75 65  lient(fd, &reque
3a60: 73 74 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65 70  st);..}.../* Rep
3a70: 6f 72 74 20 65 72 72 6f 72 20 2a 2f 0a 09 66 69  ort error */..fi
3a80: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 54 48 52  led_log_msg("THR
3a90: 45 41 44 5f 44 49 45 44 20 41 42 4e 4f 52 4d 41  EAD_DIED ABNORMA
3aa0: 4c 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 4e 55  L");...return(NU
3ab0: 4c 4c 29 3b 0a 7d 0a 0a 2f 2a 20 43 72 65 61 74  LL);.}../* Creat
3ac0: 65 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73  e worker threads
3ad0: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
3ae0: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65  iled_worker_thre
3af0: 61 64 73 5f 69 6e 69 74 28 69 6e 74 20 66 64 2c  ads_init(int fd,
3b00: 20 69 6e 74 20 74 68 72 65 61 64 5f 63 6f 75 6e   int thread_coun
3b10: 74 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c  t) {..struct fil
3b20: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64  ed_worker_thread
3b30: 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 70 74 68  _args *arg;..pth
3b40: 72 65 61 64 5f 74 20 74 68 72 65 61 64 69 64 3b  read_t threadid;
3b50: 0a 09 69 6e 74 20 70 74 68 72 65 61 64 5f 72 65  ..int pthread_re
3b60: 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 66 6f 72  t;..int i;...for
3b70: 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 74 68 72   (i = 0; i < thr
3b80: 65 61 64 5f 63 6f 75 6e 74 3b 20 69 2b 2b 29 20  ead_count; i++) 
3b90: 7b 0a 09 09 61 72 67 20 3d 20 6d 61 6c 6c 6f 63  {...arg = malloc
3ba0: 28 73 69 7a 65 6f 66 28 2a 61 72 67 29 29 3b 0a  (sizeof(*arg));.
3bb0: 0a 09 09 61 72 67 2d 3e 66 64 20 3d 20 66 64 3b  ...arg->fd = fd;
3bc0: 0a 0a 09 09 70 74 68 72 65 61 64 5f 72 65 74 20  ....pthread_ret 
3bd0: 3d 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65  = pthread_create
3be0: 28 26 74 68 72 65 61 64 69 64 2c 20 4e 55 4c 4c  (&threadid, NULL
3bf0: 2c 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74  , filed_worker_t
3c00: 68 72 65 61 64 2c 20 61 72 67 29 3b 0a 09 09 69  hread, arg);...i
3c10: 66 20 28 70 74 68 72 65 61 64 5f 72 65 74 20 21  f (pthread_ret !
3c20: 3d 20 30 29 20 7b 0a 09 09 09 72 65 74 75 72 6e  = 0) {....return
3c30: 28 2d 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72  (-1);...}..}...r
3c40: 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20  eturn(0);.}../* 
3c50: 44 69 73 70 6c 61 79 20 68 65 6c 70 20 2a 2f 0a  Display help */.
3c60: 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65  static void file
3c70: 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 46 49 4c  d_print_help(FIL
3c80: 45 20 2a 6f 75 74 70 75 74 2c 20 69 6e 74 20 6c  E *output, int l
3c90: 6f 6e 67 5f 68 65 6c 70 2c 20 63 6f 6e 73 74 20  ong_help, const 
3ca0: 63 68 61 72 20 2a 65 78 74 72 61 29 20 7b 0a 09  char *extra) {..
3cb0: 69 66 20 28 65 78 74 72 61 29 20 7b 0a 09 09 66  if (extra) {...f
3cc0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
3cd0: 25 73 5c 6e 22 2c 20 65 78 74 72 61 29 3b 0a 09  %s\n", extra);..
3ce0: 7d 0a 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70  }...fprintf(outp
3cf0: 75 74 2c 20 22 55 73 61 67 65 3a 20 66 69 6c 65  ut, "Usage: file
3d00: 64 20 5b 3c 6f 70 74 69 6f 6e 73 3e 5d 5c 6e 22  d [<options>]\n"
3d10: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70  );..fprintf(outp
3d20: 75 74 2c 20 22 20 20 4f 70 74 69 6f 6e 73 3a 5c  ut, "  Options:\
3d30: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75  n");..fprintf(ou
3d40: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 68 2c  tput, "      -h,
3d50: 20 2d 2d 68 65 6c 70 5c 6e 22 29 3b 0a 09 66 70   --help\n");..fp
3d60: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
3d70: 20 20 20 20 20 2d 64 2c 20 2d 2d 64 61 65 6d 6f       -d, --daemo
3d80: 6e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28  n\n");..fprintf(
3d90: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
3da0: 62 20 3c 61 64 64 72 65 73 73 3e 2c 20 2d 2d 62  b <address>, --b
3db0: 69 6e 64 20 3c 61 64 64 72 65 73 73 3e 5c 6e 22  ind <address>\n"
3dc0: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70  );..fprintf(outp
3dd0: 75 74 2c 20 22 20 20 20 20 20 20 2d 70 20 3c 70  ut, "      -p <p
3de0: 6f 72 74 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70 6f  ort>, --port <po
3df0: 72 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74  rt>\n");..fprint
3e00: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
3e10: 20 2d 74 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d 74   -t <count>, --t
3e20: 68 72 65 61 64 73 20 3c 63 6f 75 6e 74 3e 5c 6e  hreads <count>\n
3e30: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  ");..fprintf(out
3e40: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 63 20 3c  put, "      -c <
3e50: 65 6e 74 72 69 65 73 3e 2c 20 2d 2d 63 61 63 68  entries>, --cach
3e60: 65 20 3c 65 6e 74 72 69 65 73 3e 5c 6e 22 29 3b  e <entries>\n");
3e70: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
3e80: 2c 20 22 20 20 20 20 20 20 2d 75 20 3c 75 73 65  , "      -u <use
3e90: 72 3e 2c 20 2d 2d 75 73 65 72 20 3c 75 73 65 72  r>, --user <user
3ea0: 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28  >\n");..fprintf(
3eb0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
3ec0: 72 20 3c 64 69 72 65 63 74 6f 72 79 3e 2c 20 2d  r <directory>, -
3ed0: 2d 72 6f 6f 74 20 3c 64 69 72 65 63 74 6f 72 79  -root <directory
3ee0: 3e 5c 6e 22 29 3b 0a 0a 09 69 66 20 28 6c 6f 6e  >\n");...if (lon
3ef0: 67 5f 68 65 6c 70 29 20 7b 0a 09 09 66 70 72 69  g_help) {...fpri
3f00: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22  ntf(output, "\n"
3f10: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
3f20: 70 75 74 2c 20 22 20 20 55 73 61 67 65 3a 5c 6e  put, "  Usage:\n
3f30: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
3f40: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 68 20  tput, "      -h 
3f50: 28 6f 72 20 2d 2d 68 65 6c 70 29 20 70 72 69 6e  (or --help) prin
3f60: 74 73 20 74 68 69 73 20 75 73 61 67 65 20 69 6e  ts this usage in
3f70: 66 6f 72 6d 61 74 69 6f 6e 5c 6e 22 29 3b 0a 09  formation\n");..
3f80: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
3f90: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
3fa0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
3fb0: 20 2d 64 20 28 6f 72 20 2d 2d 64 61 65 6d 6f 6e   -d (or --daemon
3fc0: 29 20 69 6e 73 74 72 75 63 74 73 20 66 69 6c 65  ) instructs file
3fd0: 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 64 61  d to become a da
3fe0: 65 6d 6f 6e 20 61 66 74 65 72 20 69 6e 69 74 69  emon after initi
3ff0: 61 6c 69 7a 69 6e 67 5c 6e 22 29 3b 0a 09 09 66  alizing\n");...f
4000: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
4010: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4020: 20 20 20 20 20 20 20 74 68 65 20 6c 69 73 74 65         the liste
4030: 6e 69 6e 67 20 54 43 50 20 73 6f 63 6b 65 74 20  ning TCP socket 
4040: 61 6e 64 20 6c 6f 67 20 66 69 6c 65 73 2e 5c 6e  and log files.\n
4050: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
4060: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66  tput, "\n");...f
4070: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
4080: 20 20 20 20 20 20 2d 62 20 28 6f 72 20 2d 2d 62        -b (or --b
4090: 69 6e 64 29 20 73 70 65 63 69 66 69 65 73 20 74  ind) specifies t
40a0: 68 65 20 61 64 64 72 65 73 73 20 74 6f 20 6c 69  he address to li
40b0: 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e  sten for incomin
40c0: 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70  g HTTP\n");...fp
40d0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
40e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40f0: 20 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e 2e      requests on.
4100: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61    The default va
4110: 6c 75 65 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e  lue is \"%s\".\n
4120: 22 2c 20 42 49 4e 44 5f 41 44 44 52 29 3b 0a 09  ", BIND_ADDR);..
4130: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
4140: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
4150: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
4160: 20 2d 70 20 28 6f 72 20 2d 2d 70 6f 72 74 29 20   -p (or --port) 
4170: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 54 43  specifies the TC
4180: 50 20 70 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f  P port number to
4190: 20 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f   listen for inco
41a0: 6d 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09  ming HTTP\n");..
41b0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
41c0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
41d0: 20 20 20 20 20 20 20 72 65 71 75 65 73 74 73 20         requests 
41e0: 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  on.  The default
41f0: 20 69 73 20 25 75 2e 5c 6e 22 2c 20 28 75 6e 73   is %u.\n", (uns
4200: 69 67 6e 65 64 20 69 6e 74 29 20 50 4f 52 54 29  igned int) PORT)
4210: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
4220: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72  ut, "\n");...fpr
4230: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
4240: 20 20 20 20 2d 74 20 28 6f 72 20 2d 2d 74 68 72      -t (or --thr
4250: 65 61 64 73 29 20 73 70 65 63 69 66 69 65 73 20  eads) specifies 
4260: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 6f  the number of wo
4270: 72 6b 65 72 20 74 68 72 65 61 64 73 20 74 6f 20  rker threads to 
4280: 63 72 65 61 74 65 2e 20 45 61 63 68 5c 6e 22 29  create. Each\n")
4290: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
42a0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
42b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 77 6f 72               wor
42c0: 6b 65 72 20 74 68 72 65 61 64 20 63 61 6e 20 73  ker thread can s
42d0: 65 72 76 69 63 65 20 6f 6e 65 20 63 6f 6e 63 75  ervice one concu
42e0: 72 72 65 6e 74 20 48 54 54 50 20 73 65 73 73 69  rrent HTTP sessi
42f0: 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  on.\n");...fprin
4300: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
4310: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4320: 20 20 20 20 54 68 75 73 20 74 68 65 20 6e 75 6d      Thus the num
4330: 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 20 63  ber of threads c
4340: 72 65 61 74 65 64 20 77 69 6c 6c 20 64 65 74 65  reated will dete
4350: 72 6d 69 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a 09  rmine how\n");..
4360: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
4370: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
4380: 20 20 20 20 20 20 20 20 20 20 6d 61 6e 79 20 73            many s
4390: 69 6d 75 6c 74 61 6e 65 6f 75 73 20 74 72 61 6e  imultaneous tran
43a0: 73 66 65 72 73 20 77 69 6c 6c 20 62 65 20 70 6f  sfers will be po
43b0: 73 73 69 62 6c 65 2e 20 54 68 65 5c 6e 22 29 3b  ssible. The\n");
43c0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
43d0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
43e0: 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66 61              defa
43f0: 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20  ult is %lu.\n", 
4400: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20  (unsigned long) 
4410: 54 48 52 45 41 44 5f 43 4f 55 4e 54 29 3b 0a 09  THREAD_COUNT);..
4420: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
4430: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
4440: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
4450: 20 2d 63 20 28 6f 72 20 2d 2d 63 61 63 68 65 29   -c (or --cache)
4460: 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e   specifies the n
4470: 75 6d 62 65 72 20 6f 66 20 66 69 6c 65 20 69 6e  umber of file in
4480: 66 6f 72 6d 61 74 69 6f 6e 20 63 61 63 68 65 20  formation cache 
4490: 65 6e 74 72 69 65 73 5c 6e 22 29 3b 0a 09 09 66  entries\n");...f
44a0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
44b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44c0: 20 20 20 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74        to allocat
44d0: 65 2e 20 20 45 61 63 68 20 63 61 63 68 65 20 65  e.  Each cache e
44e0: 6e 74 72 79 20 68 6f 6c 64 73 20 66 69 6c 65 20  ntry holds file 
44f0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 5c 6e  information as\n
4500: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
4510: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20  tput, "         
4520: 20 20 20 20 20 20 20 20 20 20 20 20 20 77 65 6c               wel
4530: 6c 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c  l as an open fil
4540: 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20  e descriptor to 
4550: 74 68 65 20 66 69 6c 65 2c 20 73 6f 20 72 65 73  the file, so res
4560: 6f 75 72 63 65 5c 6e 22 29 3b 0a 09 09 66 70 72  ource\n");...fpr
4570: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
4580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4590: 20 20 20 20 6c 69 6d 69 74 73 20 28 69 2e 65 2e      limits (i.e.
45a0: 2c 20 75 6c 69 6d 69 74 29 20 73 68 6f 75 6c 64  , ulimit) should
45b0: 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 2e 20   be considered. 
45c0: 20 54 68 69 73 20 73 68 6f 75 6c 64 5c 6e 22 29   This should\n")
45d0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
45e0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
45f0: 20 20 20 20 20 20 20 20 20 20 20 62 65 20 61 20             be a 
4600: 70 72 69 6d 65 20 6e 75 6d 62 65 72 20 66 6f 72  prime number for
4610: 20 69 64 65 61 6c 20 75 73 65 20 77 69 74 68 20   ideal use with 
4620: 74 68 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 6f  the lookup metho
4630: 64 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74  d.\n");...fprint
4640: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
4650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4660: 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20   The default is 
4670: 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e  %lu.\n", (unsign
4680: 65 64 20 6c 6f 6e 67 29 20 43 41 43 48 45 5f 53  ed long) CACHE_S
4690: 49 5a 45 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  IZE);...fprintf(
46a0: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09  output, "\n");..
46b0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
46c0: 20 22 20 20 20 20 20 20 2d 75 20 28 6f 72 20 2d   "      -u (or -
46d0: 2d 75 73 65 72 29 20 73 70 65 63 69 66 69 65 73  -user) specifies
46e0: 20 74 68 65 20 75 73 65 72 20 74 6f 20 73 77 69   the user to swi
46f0: 74 63 68 20 75 73 65 72 20 49 44 73 20 74 6f 20  tch user IDs to 
4700: 62 65 66 6f 72 65 20 73 65 72 76 69 63 69 6e 67  before servicing
4710: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
4720: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
4730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
4740: 71 75 65 73 74 73 2e 20 20 54 68 65 20 64 65 66  quests.  The def
4750: 61 75 6c 74 20 69 73 20 6e 6f 74 20 63 68 61 6e  ault is not chan
4760: 67 65 20 75 73 65 72 20 49 44 73 2e 5c 6e 22 29  ge user IDs.\n")
4770: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
4780: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72  ut, "\n");...fpr
4790: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
47a0: 20 20 20 20 2d 72 20 28 6f 72 20 2d 2d 72 6f 6f      -r (or --roo
47b0: 74 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65  t) specifies the
47c0: 20 64 69 72 65 63 74 6f 72 79 20 74 6f 20 61 63   directory to ac
47d0: 74 20 61 73 20 74 68 65 20 72 6f 6f 74 20 64 69  t as the root di
47e0: 72 65 63 74 6f 72 79 20 66 6f 72 5c 6e 22 29 3b  rectory for\n");
47f0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
4800: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
4810: 20 20 20 20 20 20 20 20 20 74 68 65 20 66 69 6c           the fil
4820: 65 20 73 65 72 76 65 72 2e 20 20 49 66 20 74 68  e server.  If th
4830: 69 73 20 6f 70 74 69 6f 6e 20 69 73 20 73 70 65  is option is spe
4840: 63 69 66 69 65 64 2c 20 63 68 72 6f 6f 74 28 32  cified, chroot(2
4850: 29 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  )\n");...fprintf
4860: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
4870: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
4880: 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 64  s called.  The d
4890: 65 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 63 68  efault is not ch
48a0: 61 6e 67 65 20 72 6f 6f 74 20 64 69 72 65 63 74  ange root direct
48b0: 6f 72 69 65 73 2c 5c 6e 22 29 3b 0a 09 09 66 70  ories,\n");...fp
48c0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
48d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48e0: 20 20 20 20 74 68 61 74 20 69 73 2c 20 74 68 65      that is, the
48f0: 20 5c 22 2f 5c 22 20 64 69 72 65 63 74 6f 72 79   \"/\" directory
4900: 20 69 73 20 73 68 61 72 65 64 20 6f 75 74 2e 20   is shared out. 
4910: 20 54 68 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a   This will\n");.
4920: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
4930: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
4940: 20 20 20 20 20 20 20 20 6c 69 6b 65 6c 79 20 62          likely b
4950: 65 20 61 20 73 65 63 75 72 69 74 79 20 69 73 73  e a security iss
4960: 75 65 2c 20 73 6f 20 74 68 69 73 20 6f 70 74 69  ue, so this opti
4970: 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73  on should always
4980: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
4990: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
49a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
49b0: 20 75 73 65 64 2e 5c 6e 22 29 3b 0a 09 7d 0a 0a   used.\n");..}..
49c0: 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 41  .return;.}../* A
49d0: 64 64 20 61 20 67 65 74 6f 70 74 20 6f 70 74 69  dd a getopt opti
49e0: 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on */.static voi
49f0: 64 20 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c  d filed_getopt_l
4a00: 6f 6e 67 5f 73 65 74 6f 70 74 28 73 74 72 75 63  ong_setopt(struc
4a10: 74 20 6f 70 74 69 6f 6e 20 2a 6f 70 74 2c 20 63  t option *opt, c
4a20: 6f 6e 73 74 20 63 68 61 72 20 2a 6e 61 6d 65 2c  onst char *name,
4a30: 20 69 6e 74 20 68 61 73 5f 61 72 67 2c 20 69 6e   int has_arg, in
4a40: 74 20 76 61 6c 29 20 7b 0a 09 6f 70 74 2d 3e 6e  t val) {..opt->n
4a50: 61 6d 65 20 20 20 20 20 3d 20 6e 61 6d 65 3b 0a  ame     = name;.
4a60: 09 6f 70 74 2d 3e 68 61 73 5f 61 72 67 20 20 3d  .opt->has_arg  =
4a70: 20 68 61 73 5f 61 72 67 3b 0a 09 6f 70 74 2d 3e   has_arg;..opt->
4a80: 66 6c 61 67 20 20 20 20 20 3d 20 4e 55 4c 4c 3b  flag     = NULL;
4a90: 0a 09 6f 70 74 2d 3e 76 61 6c 20 20 20 20 20 20  ..opt->val      
4aa0: 3d 20 76 61 6c 3b 0a 0a 09 72 65 74 75 72 6e 3b  = val;...return;
4ab0: 0a 7d 0a 0a 2f 2a 20 52 65 73 6f 6c 76 65 20 61  .}../* Resolve a
4ac0: 20 75 73 65 72 6e 61 6d 65 20 74 6f 20 61 20 55   username to a U
4ad0: 49 44 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ID */.static int
4ae0: 20 66 69 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b   filed_user_look
4af0: 75 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 75  up(const char *u
4b00: 73 65 72 2c 20 75 69 64 5f 74 20 2a 75 73 65 72  ser, uid_t *user
4b10: 5f 69 64 29 20 7b 0a 09 63 68 61 72 20 2a 6e 65  _id) {..char *ne
4b20: 78 74 3b 0a 09 75 69 64 5f 74 20 75 73 65 72 5f  xt;..uid_t user_
4b30: 69 64 5f 63 68 65 63 6b 3b 0a 23 69 66 6e 64 65  id_check;.#ifnde
4b40: 66 20 46 49 4c 45 44 5f 4e 4f 5f 47 45 54 50 57  f FILED_NO_GETPW
4b50: 4e 41 4d 0a 09 73 74 72 75 63 74 20 70 61 73 73  NAM..struct pass
4b60: 77 64 20 2a 65 6e 74 3b 0a 0a 09 65 6e 74 20 3d  wd *ent;...ent =
4b70: 20 67 65 74 70 77 6e 61 6d 28 75 73 65 72 29 3b   getpwnam(user);
4b80: 0a 09 69 66 20 28 65 6e 74 20 21 3d 20 4e 55 4c  ..if (ent != NUL
4b90: 4c 29 20 7b 0a 09 09 2a 75 73 65 72 5f 69 64 20  L) {...*user_id 
4ba0: 3d 20 65 6e 74 2d 3e 70 77 5f 75 69 64 3b 0a 0a  = ent->pw_uid;..
4bb0: 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a  ..return(0);..}.
4bc0: 23 65 6e 64 69 66 0a 0a 09 75 73 65 72 5f 69 64  #endif...user_id
4bd0: 5f 63 68 65 63 6b 20 3d 20 73 74 72 74 6f 75 6c  _check = strtoul
4be0: 6c 28 75 73 65 72 2c 20 26 6e 65 78 74 2c 20 31  l(user, &next, 1
4bf0: 30 29 3b 0a 09 69 66 20 28 6e 65 78 74 20 3d 3d  0);..if (next ==
4c00: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72   NULL) {...retur
4c10: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 6e  n(1);..}...if (n
4c20: 65 78 74 5b 30 5d 20 21 3d 20 27 5c 30 27 29 20  ext[0] != '\0') 
4c30: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  {...return(1);..
4c40: 7d 0a 0a 09 2a 75 73 65 72 5f 69 64 20 3d 20 75  }...*user_id = u
4c50: 73 65 72 5f 69 64 5f 63 68 65 63 6b 3b 0a 0a 09  ser_id_check;...
4c60: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a  return(0);.}../*
4c70: 20 44 61 65 6d 6f 6e 69 7a 65 20 2a 2f 0a 73 74   Daemonize */.st
4c80: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 64  atic int filed_d
4c90: 61 65 6d 6f 6e 69 7a 65 28 76 6f 69 64 29 20 7b  aemonize(void) {
4ca0: 0a 09 70 69 64 5f 74 20 73 65 74 73 69 64 5f 72  ..pid_t setsid_r
4cb0: 65 74 2c 20 66 6f 72 6b 5f 72 65 74 3b 0a 09 69  et, fork_ret;..i
4cc0: 6e 74 20 63 68 64 69 72 5f 72 65 74 2c 20 64 75  nt chdir_ret, du
4cd0: 70 32 5f 72 65 74 3b 0a 09 69 6e 74 20 66 64 5f  p2_ret;..int fd_
4ce0: 69 6e 2c 20 66 64 5f 6f 75 74 3b 0a 0a 09 63 68  in, fd_out;...ch
4cf0: 64 69 72 5f 72 65 74 20 3d 20 63 68 64 69 72 28  dir_ret = chdir(
4d00: 22 2f 22 29 3b 0a 09 69 66 20 28 63 68 64 69 72  "/");..if (chdir
4d10: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 72  _ret != 0) {...r
4d20: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66  eturn(1);..}...f
4d30: 6f 72 6b 5f 72 65 74 20 3d 20 66 6f 72 6b 28 29  ork_ret = fork()
4d40: 3b 0a 09 69 66 20 28 66 6f 72 6b 5f 72 65 74 20  ;..if (fork_ret 
4d50: 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  < 0) {...return(
4d60: 31 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 66 6f 72  1);..}...if (for
4d70: 6b 5f 72 65 74 20 3e 20 30 29 20 7b 0a 09 09 2f  k_ret > 0) {.../
4d80: 2a 20 50 61 72 65 6e 74 20 2a 2f 0a 09 09 77 61  * Parent */...wa
4d90: 69 74 70 69 64 28 66 6f 72 6b 5f 72 65 74 2c 20  itpid(fork_ret, 
4da0: 4e 55 4c 4c 2c 20 30 29 3b 0a 0a 09 09 65 78 69  NULL, 0);....exi
4db0: 74 28 45 58 49 54 5f 53 55 43 43 45 53 53 29 3b  t(EXIT_SUCCESS);
4dc0: 0a 09 7d 0a 0a 09 2f 2a 20 43 68 69 6c 64 20 2a  ..}.../* Child *
4dd0: 2f 0a 09 69 66 20 28 66 6f 72 6b 28 29 20 21 3d  /..if (fork() !=
4de0: 20 30 29 20 7b 0a 09 09 2f 2a 20 43 68 69 6c 64   0) {.../* Child
4df0: 20 2a 2f 0a 09 09 65 78 69 74 28 45 58 49 54 5f   */...exit(EXIT_
4e00: 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f  SUCCESS);..}.../
4e10: 2a 20 47 72 61 6e 64 20 63 68 69 6c 64 20 2a 2f  * Grand child */
4e20: 0a 09 73 65 74 73 69 64 5f 72 65 74 20 3d 20 73  ..setsid_ret = s
4e30: 65 74 73 69 64 28 29 3b 0a 09 69 66 20 28 73 65  etsid();..if (se
4e40: 74 73 69 64 5f 72 65 74 20 3d 3d 20 28 28 70 69  tsid_ret == ((pi
4e50: 64 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09 72 65  d_t) -1)) {...re
4e60: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66 64  turn(1);..}...fd
4e70: 5f 69 6e 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76  _in = open("/dev
4e80: 2f 6e 75 6c 6c 22 2c 20 4f 5f 52 44 4f 4e 4c 59  /null", O_RDONLY
4e90: 29 3b 0a 09 66 64 5f 6f 75 74 20 3d 20 6f 70 65  );..fd_out = ope
4ea0: 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f  n("/dev/null", O
4eb0: 5f 57 52 4f 4e 4c 59 29 3b 0a 09 69 66 20 28 66  _WRONLY);..if (f
4ec0: 64 5f 69 6e 20 3c 20 30 20 7c 7c 20 66 64 5f 6f  d_in < 0 || fd_o
4ed0: 75 74 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 75  ut < 0) {...retu
4ee0: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32  rn(1);..}...dup2
4ef0: 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64 5f 69  _ret = dup2(fd_i
4f00: 6e 2c 20 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29  n, STDIN_FILENO)
4f10: 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74 20  ;..if (dup2_ret 
4f20: 21 3d 20 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29  != STDIN_FILENO)
4f30: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a   {...return(1);.
4f40: 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d 20  .}...dup2_ret = 
4f50: 64 75 70 32 28 66 64 5f 6f 75 74 2c 20 53 54 44  dup2(fd_out, STD
4f60: 4f 55 54 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66  OUT_FILENO);..if
4f70: 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54   (dup2_ret != ST
4f80: 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09  DOUT_FILENO) {..
4f90: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a  .return(1);..}..
4fa0: 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32  .dup2_ret = dup2
4fb0: 28 66 64 5f 6f 75 74 2c 20 53 54 44 45 52 52 5f  (fd_out, STDERR_
4fc0: 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75  FILENO);..if (du
4fd0: 70 32 5f 72 65 74 20 21 3d 20 53 54 44 45 52 52  p2_ret != STDERR
4fe0: 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74  _FILENO) {...ret
4ff0: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 63 6c 6f  urn(1);..}...clo
5000: 73 65 28 66 64 5f 69 6e 29 3b 0a 09 63 6c 6f 73  se(fd_in);..clos
5010: 65 28 66 64 5f 6f 75 74 29 3b 0a 0a 09 72 65 74  e(fd_out);...ret
5020: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 52 75  urn(0);.}../* Ru
5030: 6e 20 70 72 6f 63 65 73 73 20 2a 2f 0a 69 6e 74  n process */.int
5040: 20 6d 61 69 6e 28 69 6e 74 20 61 72 67 63 2c 20   main(int argc, 
5050: 63 68 61 72 20 2a 2a 61 72 67 76 29 20 7b 0a 09  char **argv) {..
5060: 73 74 72 75 63 74 20 6f 70 74 69 6f 6e 20 6f 70  struct option op
5070: 74 69 6f 6e 73 5b 39 5d 3b 0a 09 63 6f 6e 73 74  tions[9];..const
5080: 20 63 68 61 72 20 2a 62 69 6e 64 5f 61 64 64 72   char *bind_addr
5090: 20 3d 20 42 49 4e 44 5f 41 44 44 52 2c 20 2a 6e   = BIND_ADDR, *n
50a0: 65 77 72 6f 6f 74 20 3d 20 4e 55 4c 4c 3b 0a 09  ewroot = NULL;..
50b0: 75 69 64 5f 74 20 75 73 65 72 20 3d 20 30 3b 0a  uid_t user = 0;.
50c0: 09 69 6e 74 20 70 6f 72 74 20 3d 20 50 4f 52 54  .int port = PORT
50d0: 2c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d  , thread_count =
50e0: 20 54 48 52 45 41 44 5f 43 4f 55 4e 54 3b 0a 09   THREAD_COUNT;..
50f0: 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 20 3d  int cache_size =
5100: 20 43 41 43 48 45 5f 53 49 5a 45 3b 0a 09 69 6e   CACHE_SIZE;..in
5110: 74 20 69 6e 69 74 5f 72 65 74 2c 20 63 68 72 6f  t init_ret, chro
5120: 6f 74 5f 72 65 74 2c 20 73 65 74 75 69 64 5f 72  ot_ret, setuid_r
5130: 65 74 2c 20 6c 6f 6f 6b 75 70 5f 72 65 74 2c 20  et, lookup_ret, 
5140: 63 68 64 69 72 5f 72 65 74 3b 0a 09 69 6e 74 20  chdir_ret;..int 
5150: 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 20 3d  setuid_enabled =
5160: 20 30 2c 20 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c   0, daemon_enabl
5170: 65 64 20 3d 20 30 3b 0a 09 69 6e 74 20 63 68 3b  ed = 0;..int ch;
5180: 0a 09 69 6e 74 20 66 64 3b 0a 0a 09 2f 2a 20 50  ..int fd;.../* P
5190: 72 6f 63 65 73 73 20 61 72 67 75 6d 65 6e 74 73  rocess arguments
51a0: 20 2a 2f 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70   */..filed_getop
51b0: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f  t_long_setopt(&o
51c0: 70 74 69 6f 6e 73 5b 30 5d 2c 20 22 70 6f 72 74  ptions[0], "port
51d0: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75  ", required_argu
51e0: 6d 65 6e 74 2c 20 27 70 27 29 3b 0a 09 66 69 6c  ment, 'p');..fil
51f0: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73  ed_getopt_long_s
5200: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31  etopt(&options[1
5210: 5d 2c 20 22 74 68 72 65 61 64 73 22 2c 20 72 65  ], "threads", re
5220: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c  quired_argument,
5230: 20 27 74 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65   't');..filed_ge
5240: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74  topt_long_setopt
5250: 28 26 6f 70 74 69 6f 6e 73 5b 32 5d 2c 20 22 63  (&options[2], "c
5260: 61 63 68 65 22 2c 20 72 65 71 75 69 72 65 64 5f  ache", required_
5270: 61 72 67 75 6d 65 6e 74 2c 20 27 63 27 29 3b 0a  argument, 'c');.
5280: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f  .filed_getopt_lo
5290: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f  ng_setopt(&optio
52a0: 6e 73 5b 33 5d 2c 20 22 62 69 6e 64 22 2c 20 72  ns[3], "bind", r
52b0: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74  equired_argument
52c0: 2c 20 27 62 27 29 3b 0a 09 66 69 6c 65 64 5f 67  , 'b');..filed_g
52d0: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70  etopt_long_setop
52e0: 74 28 26 6f 70 74 69 6f 6e 73 5b 34 5d 2c 20 22  t(&options[4], "
52f0: 75 73 65 72 22 2c 20 72 65 71 75 69 72 65 64 5f  user", required_
5300: 61 72 67 75 6d 65 6e 74 2c 20 27 75 27 29 3b 0a  argument, 'u');.
5310: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f  .filed_getopt_lo
5320: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f  ng_setopt(&optio
5330: 6e 73 5b 35 5d 2c 20 22 72 6f 6f 74 22 2c 20 72  ns[5], "root", r
5340: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74  equired_argument
5350: 2c 20 27 72 27 29 3b 0a 09 66 69 6c 65 64 5f 67  , 'r');..filed_g
5360: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70  etopt_long_setop
5370: 74 28 26 6f 70 74 69 6f 6e 73 5b 36 5d 2c 20 22  t(&options[6], "
5380: 68 65 6c 70 22 2c 20 6e 6f 5f 61 72 67 75 6d 65  help", no_argume
5390: 6e 74 2c 20 27 68 27 29 3b 0a 09 66 69 6c 65 64  nt, 'h');..filed
53a0: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74  _getopt_long_set
53b0: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 37 5d 2c  opt(&options[7],
53c0: 20 22 64 61 65 6d 6f 6e 22 2c 20 6e 6f 5f 61 72   "daemon", no_ar
53d0: 67 75 6d 65 6e 74 2c 20 27 64 27 29 3b 0a 09 66  gument, 'd');..f
53e0: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67  iled_getopt_long
53f0: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73  _setopt(&options
5400: 5b 38 5d 2c 20 4e 55 4c 4c 2c 20 30 2c 20 30 29  [8], NULL, 0, 0)
5410: 3b 0a 09 77 68 69 6c 65 20 28 28 63 68 20 3d 20  ;..while ((ch = 
5420: 67 65 74 6f 70 74 5f 6c 6f 6e 67 28 61 72 67 63  getopt_long(argc
5430: 2c 20 61 72 67 76 2c 20 22 70 3a 74 3a 63 3a 62  , argv, "p:t:c:b
5440: 3a 75 3a 72 3a 68 64 22 2c 20 6f 70 74 69 6f 6e  :u:r:hd", option
5450: 73 2c 20 4e 55 4c 4c 29 29 20 21 3d 20 2d 31 29  s, NULL)) != -1)
5460: 20 7b 0a 09 09 73 77 69 74 63 68 28 63 68 29 20   {...switch(ch) 
5470: 7b 0a 09 09 09 63 61 73 65 20 27 70 27 3a 0a 09  {....case 'p':..
5480: 09 09 09 70 6f 72 74 20 3d 20 61 74 6f 69 28 6f  ...port = atoi(o
5490: 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61  ptarg);.....brea
54a0: 6b 3b 0a 09 09 09 63 61 73 65 20 27 74 27 3a 0a  k;....case 't':.
54b0: 09 09 09 09 74 68 72 65 61 64 5f 63 6f 75 6e 74  ....thread_count
54c0: 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 3b   = atoi(optarg);
54d0: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63  .....break;....c
54e0: 61 73 65 20 27 63 27 3a 0a 09 09 09 09 63 61 63  ase 'c':.....cac
54f0: 68 65 5f 73 69 7a 65 20 3d 20 61 74 6f 69 28 6f  he_size = atoi(o
5500: 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61  ptarg);.....brea
5510: 6b 3b 0a 09 09 09 63 61 73 65 20 27 62 27 3a 0a  k;....case 'b':.
5520: 09 09 09 09 62 69 6e 64 5f 61 64 64 72 20 3d 20  ....bind_addr = 
5530: 73 74 72 64 75 70 28 6f 70 74 61 72 67 29 3b 0a  strdup(optarg);.
5540: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61  ....break;....ca
5550: 73 65 20 27 75 27 3a 0a 09 09 09 09 73 65 74 75  se 'u':.....setu
5560: 69 64 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a  id_enabled = 1;.
5570: 09 09 09 09 6c 6f 6f 6b 75 70 5f 72 65 74 20 3d  ....lookup_ret =
5580: 20 66 69 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b   filed_user_look
5590: 75 70 28 6f 70 74 61 72 67 2c 20 26 75 73 65 72  up(optarg, &user
55a0: 29 3b 0a 09 09 09 09 69 66 20 28 6c 6f 6f 6b 75  );.....if (looku
55b0: 70 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09  p_ret != 0) {...
55c0: 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68  ...filed_print_h
55d0: 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20 22  elp(stderr, 0, "
55e0: 49 6e 76 61 6c 69 64 20 75 73 65 72 6e 61 6d 65  Invalid username
55f0: 20 73 70 65 63 69 66 69 65 64 22 29 3b 0a 0a 09   specified");...
5600: 09 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  ....return(1);..
5610: 09 09 09 7d 0a 09 09 09 09 62 72 65 61 6b 3b 0a  ...}.....break;.
5620: 09 09 09 63 61 73 65 20 27 72 27 3a 0a 09 09 09  ...case 'r':....
5630: 09 6e 65 77 72 6f 6f 74 20 3d 20 73 74 72 64 75  .newroot = strdu
5640: 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62  p(optarg);.....b
5650: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 64  reak;....case 'd
5660: 27 3a 0a 09 09 09 09 64 61 65 6d 6f 6e 5f 65 6e  ':.....daemon_en
5670: 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 62  abled = 1;.....b
5680: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 3f  reak;....case '?
5690: 27 3a 0a 09 09 09 63 61 73 65 20 27 3a 27 3a 0a  ':....case ':':.
56a0: 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f  ....filed_print_
56b0: 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20  help(stderr, 0, 
56c0: 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75  NULL);......retu
56d0: 72 6e 28 31 29 3b 0a 09 09 09 63 61 73 65 20 27  rn(1);....case '
56e0: 68 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 72  h':.....filed_pr
56f0: 69 6e 74 5f 68 65 6c 70 28 73 74 64 6f 75 74 2c  int_help(stdout,
5700: 20 31 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09   1, NULL);......
5710: 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 7d 0a 09  return(0);...}..
5720: 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 69  }.../* Create li
5730: 73 74 65 6e 69 6e 67 20 73 6f 63 6b 65 74 20 2a  stening socket *
5740: 2f 0a 09 66 64 20 3d 20 66 69 6c 65 64 5f 6c 69  /..fd = filed_li
5750: 73 74 65 6e 28 62 69 6e 64 5f 61 64 64 72 2c 20  sten(bind_addr, 
5760: 70 6f 72 74 29 3b 0a 09 69 66 20 28 66 64 20 3c  port);..if (fd <
5770: 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22   0) {...perror("
5780: 66 69 6c 65 64 5f 6c 69 73 74 65 6e 22 29 3b 0a  filed_listen");.
5790: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d  ...return(1);..}
57a0: 0a 0a 09 2f 2a 20 43 68 72 6f 6f 74 2c 20 69 66  .../* Chroot, if
57b0: 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a   appropriate */.
57c0: 09 69 66 20 28 6e 65 77 72 6f 6f 74 29 20 7b 0a  .if (newroot) {.
57d0: 09 09 63 68 64 69 72 5f 72 65 74 20 3d 20 63 68  ..chdir_ret = ch
57e0: 64 69 72 28 6e 65 77 72 6f 6f 74 29 3b 0a 09 09  dir(newroot);...
57f0: 69 66 20 28 63 68 64 69 72 5f 72 65 74 20 21 3d  if (chdir_ret !=
5800: 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28   0) {....perror(
5810: 22 63 68 64 69 72 22 29 3b 0a 0a 09 09 09 72 65  "chdir");.....re
5820: 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09  turn(1);...}....
5830: 63 68 72 6f 6f 74 5f 72 65 74 20 3d 20 63 68 72  chroot_ret = chr
5840: 6f 6f 74 28 22 2e 22 29 3b 0a 09 09 69 66 20 28  oot(".");...if (
5850: 63 68 72 6f 6f 74 5f 72 65 74 20 21 3d 20 30 29  chroot_ret != 0)
5860: 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 63 68   {....perror("ch
5870: 72 6f 6f 74 22 29 3b 0a 0a 09 09 09 72 65 74 75  root");.....retu
5880: 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09  rn(1);...}..}...
5890: 2f 2a 20 44 72 6f 70 20 70 72 69 76 69 6c 65 67  /* Drop privileg
58a0: 65 73 2c 20 69 66 20 61 70 70 72 6f 70 72 69 61  es, if appropria
58b0: 74 65 20 2a 2f 0a 09 69 66 20 28 73 65 74 75 69  te */..if (setui
58c0: 64 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 73  d_enabled) {...s
58d0: 65 74 75 69 64 5f 72 65 74 20 3d 20 73 65 74 75  etuid_ret = setu
58e0: 69 64 28 75 73 65 72 29 3b 0a 09 09 69 66 20 28  id(user);...if (
58f0: 73 65 74 75 69 64 5f 72 65 74 20 21 3d 20 30 29  setuid_ret != 0)
5900: 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 73 65   {....perror("se
5910: 74 75 69 64 22 29 3b 0a 0a 09 09 09 72 65 74 75  tuid");.....retu
5920: 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09  rn(1);...}..}...
5930: 2f 2a 20 42 65 63 6f 6d 65 20 61 20 64 61 65 6d  /* Become a daem
5940: 6f 6e 20 2a 2f 0a 09 69 66 20 28 64 61 65 6d 6f  on */..if (daemo
5950: 6e 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 66  n_enabled) {...f
5960: 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 29  iled_daemonize()
5970: 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 61  ;..}.../* Initia
5980: 6c 69 7a 65 20 2a 2f 0a 09 69 6e 69 74 5f 72 65  lize */..init_re
5990: 74 20 3d 20 66 69 6c 65 64 5f 69 6e 69 74 28 63  t = filed_init(c
59a0: 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20  ache_size);..if 
59b0: 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20  (init_ret != 0) 
59c0: 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65  {...perror("file
59d0: 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74  d_init");....ret
59e0: 75 72 6e 28 33 29 3b 0a 09 7d 0a 0a 09 2f 2a 20  urn(3);..}.../* 
59f0: 43 72 65 61 74 65 20 6c 6f 67 67 69 6e 67 20 74  Create logging t
5a00: 68 72 65 61 64 20 2a 2f 0a 09 69 6e 69 74 5f 72  hread */..init_r
5a10: 65 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 67 69  et = filed_loggi
5a20: 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 29  ng_thread_init()
5a30: 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20  ;..if (init_ret 
5a40: 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72  != 0) {...perror
5a50: 28 22 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f  ("filed_logging_
5a60: 74 68 72 65 61 64 5f 69 6e 69 74 22 29 3b 0a 0a  thread_init");..
5a70: 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d 0a  ..return(4);..}.
5a80: 0a 09 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 6b  ../* Create work
5a90: 65 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 09 69  er threads */..i
5aa0: 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f  nit_ret = filed_
5ab0: 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69  worker_threads_i
5ac0: 6e 69 74 28 66 64 2c 20 74 68 72 65 61 64 5f 63  nit(fd, thread_c
5ad0: 6f 75 6e 74 29 3b 0a 09 69 66 20 28 69 6e 69 74  ount);..if (init
5ae0: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70  _ret != 0) {...p
5af0: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 77 6f 72  error("filed_wor
5b00: 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74  ker_threads_init
5b10: 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 34 29  ");....return(4)
5b20: 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 61 69 74 20 66  ;..}.../* Wait f
5b30: 6f 72 20 74 68 72 65 61 64 73 20 74 6f 20 65 78  or threads to ex
5b40: 69 74 20 2a 2f 0a 09 2f 2a 20 58 58 58 3a 54 4f  it */../* XXX:TO
5b50: 44 4f 3a 20 4d 6f 6e 69 74 6f 72 20 74 68 72 65  DO: Monitor thre
5b60: 61 64 20 75 73 61 67 65 20 2a 2f 0a 09 77 68 69  ad usage */..whi
5b70: 6c 65 20 28 31 29 20 7b 0a 09 09 73 6c 65 65 70  le (1) {...sleep
5b80: 28 36 30 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65  (60);..}.../* Re
5b90: 74 75 72 6e 20 69 6e 20 66 61 69 6c 75 72 65 20  turn in failure 
5ba0: 2a 2f 0a 09 72 65 74 75 72 6e 28 32 29 3b 0a 7d  */..return(2);.}
5bb0: 0a                                               .