Hex Artifact Content

Artifact a9d9efdd3879a540cac3c94925ab89600e272f0c:


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 73 74 64 61 72 67  #include <stdarg
0140: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63  .h>.#include <fc
0150: 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  ntl.h>.#include 
0160: 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75  <stdio.h>.#inclu
0170: 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 23 69 6e  de <errno.h>.#in
0180: 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 23  clude <time.h>.#
0190: 69 6e 63 6c 75 64 65 20 3c 70 77 64 2e 68 3e 0a  include <pwd.h>.
01a0: 0a 2f 2a 20 43 6f 6d 70 69 6c 65 20 74 69 6d 65  ./* Compile time
01b0: 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 23 64   constants */.#d
01c0: 65 66 69 6e 65 20 46 49 4c 45 44 5f 56 45 52 53  efine FILED_VERS
01d0: 49 4f 4e 20 22 31 2e 31 22 0a 23 64 65 66 69 6e  ION "1.1".#defin
01e0: 65 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45  e FILED_SENDFILE
01f0: 5f 4d 41 58 20 31 36 37 37 37 32 31 35 0a 23 64  _MAX 16777215.#d
0200: 65 66 69 6e 65 20 46 49 4c 45 44 5f 4d 41 58 5f  efine FILED_MAX_
0210: 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 20 33 30  FAILURE_COUNT 30
0220: 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 44  .#define FILED_D
0230: 45 46 41 55 4c 54 5f 54 59 50 45 20 22 61 70 70  EFAULT_TYPE "app
0240: 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d 73  lication/octet-s
0250: 74 72 65 61 6d 22 0a 0a 2f 2a 20 44 65 66 61 75  tream"../* Defau
0260: 6c 74 20 76 61 6c 75 65 73 20 2a 2f 0a 23 64 65  lt values */.#de
0270: 66 69 6e 65 20 50 4f 52 54 20 38 30 0a 23 64 65  fine PORT 80.#de
0280: 66 69 6e 65 20 54 48 52 45 41 44 5f 43 4f 55 4e  fine THREAD_COUN
0290: 54 20 35 0a 23 64 65 66 69 6e 65 20 42 49 4e 44  T 5.#define BIND
02a0: 5f 41 44 44 52 20 22 3a 3a 22 0a 23 64 65 66 69  _ADDR "::".#defi
02b0: 6e 65 20 43 41 43 48 45 5f 53 49 5a 45 20 38 32  ne CACHE_SIZE 82
02c0: 30 39 0a 23 64 65 66 69 6e 65 20 4c 4f 47 5f 46  09.#define LOG_F
02d0: 49 4c 45 20 22 2d 22 0a 0a 2f 2a 20 41 72 67 75  ILE "-"../* Argu
02e0: 6d 65 6e 74 73 20 66 6f 72 20 77 6f 72 6b 65 72  ments for worker
02f0: 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 72 75   threads */.stru
0300: 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f  ct filed_worker_
0310: 74 68 72 65 61 64 5f 61 72 67 73 20 7b 0a 09 69  thread_args {..i
0320: 6e 74 20 66 64 3b 0a 7d 3b 0a 0a 2f 2a 20 41 72  nt fd;.};../* Ar
0330: 67 75 6d 65 6e 74 73 20 66 6f 72 20 6c 6f 67 67  guments for logg
0340: 69 6e 67 20 74 68 72 65 61 64 73 20 2a 2f 0a 73  ing threads */.s
0350: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 67  truct filed_logg
0360: 69 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 73 20  ing_thread_args 
0370: 7b 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 7d 3b 0a  {..FILE *fp;.};.
0380: 0a 2f 2a 20 46 69 6c 65 20 69 6e 66 6f 72 6d 61  ./* File informa
0390: 74 69 6f 6e 20 2a 2f 0a 73 74 72 75 63 74 20 66  tion */.struct f
03a0: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 7b 0a  iled_fileinfo {.
03b0: 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74  .pthread_mutex_t
03c0: 20 6d 75 74 65 78 3b 0a 09 63 68 61 72 20 2a 70   mutex;..char *p
03d0: 61 74 68 3b 0a 09 69 6e 74 20 66 64 3b 0a 09 6f  ath;..int fd;..o
03e0: 66 66 5f 74 20 6c 65 6e 3b 0a 09 63 68 61 72 20  ff_t len;..char 
03f0: 2a 6c 61 73 74 6d 6f 64 3b 0a 09 63 68 61 72 20  *lastmod;..char 
0400: 6c 61 73 74 6d 6f 64 5f 62 5b 36 34 5d 3b 0a 09  lastmod_b[64];..
0410: 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 79 70 65  const char *type
0420: 3b 0a 7d 3b 0a 0a 2f 2a 20 52 65 71 75 65 73 74  ;.};../* Request
0430: 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 73 74   variables */.st
0440: 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f  ruct filed_http_
0450: 72 65 71 75 65 73 74 20 7b 0a 09 2f 2a 2a 20 42  request {../** B
0460: 75 66 66 65 72 73 20 2a 2a 2f 0a 09 73 74 72 75  uffers **/..stru
0470: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  ct filed_fileinf
0480: 6f 20 66 69 6c 65 69 6e 66 6f 3b 0a 09 63 68 61  o fileinfo;..cha
0490: 72 20 70 61 74 68 5f 62 5b 31 30 31 30 5d 3b 0a  r path_b[1010];.
04a0: 09 63 68 61 72 20 74 6d 70 62 75 66 5b 31 30 31  .char tmpbuf[101
04b0: 30 5d 3b 0a 0a 09 2f 2a 2a 20 48 54 54 50 20 52  0];.../** HTTP R
04c0: 65 71 75 65 73 74 20 69 6e 66 6f 72 6d 61 74 69  equest informati
04d0: 6f 6e 20 2a 2a 2f 0a 09 63 68 61 72 20 2a 70 61  on **/..char *pa
04e0: 74 68 3b 20 20 20 20 20 2f 2a 2a 2a 20 50 61 74  th;     /*** Pat
04f0: 68 20 62 65 69 6e 67 20 72 65 71 75 65 73 74 65  h being requeste
0500: 64 20 2a 2a 2a 2f 0a 0a 09 73 74 72 75 63 74 20  d ***/...struct 
0510: 7b 0a 09 09 73 74 72 75 63 74 20 7b 0a 09 09 09  {...struct {....
0520: 69 6e 74 20 70 72 65 73 65 6e 74 3b 0a 09 09 09  int present;....
0530: 6f 66 66 5f 74 20 6f 66 66 73 65 74 3b 20 20 20  off_t offset;   
0540: 2f 2a 2a 2a 20 52 61 6e 67 65 20 73 74 61 72 74  /*** Range start
0550: 20 2a 2a 2a 2f 0a 09 09 09 6f 66 66 5f 74 20 6c   ***/....off_t l
0560: 65 6e 67 74 68 3b 20 20 20 2f 2a 2a 2a 20 52 61  ength;   /*** Ra
0570: 6e 67 65 20 6c 65 6e 67 74 68 20 2a 2a 2a 2f 0a  nge length ***/.
0580: 09 09 7d 20 72 61 6e 67 65 3b 0a 09 7d 20 68 65  ..} range;..} he
0590: 61 64 65 72 73 3b 0a 7d 3b 0a 0a 2f 2a 20 4c 6f  aders;.};../* Lo
05a0: 67 20 72 65 63 6f 72 64 20 2a 2f 0a 73 74 72 75  g record */.stru
05b0: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  ct filed_log_ent
05c0: 72 79 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c  ry {..struct fil
05d0: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 5f 6e  ed_log_entry *_n
05e0: 65 78 74 3b 0a 09 73 74 72 75 63 74 20 66 69 6c  ext;..struct fil
05f0: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 5f 70  ed_log_entry *_p
0600: 72 65 76 3b 0a 09 70 74 68 72 65 61 64 5f 74 20  rev;..pthread_t 
0610: 74 68 72 65 61 64 3b 0a 09 63 68 61 72 20 62 75  thread;..char bu
0620: 66 66 65 72 5b 31 30 31 30 5d 3b 0a 09 69 6e 74  ffer[1010];..int
0630: 20 6c 65 76 65 6c 3b 0a 7d 3b 0a 0a 2f 2a 20 47   level;.};../* G
0640: 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20  lobal variables 
0650: 2a 2f 0a 2f 2a 2a 20 4f 70 65 6e 20 46 69 6c 65  */./** Open File
0660: 20 63 61 63 68 65 20 2a 2a 2f 0a 73 74 72 75 63   cache **/.struc
0670: 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  t filed_fileinfo
0680: 20 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f   *filed_fileinfo
0690: 5f 66 64 63 61 63 68 65 20 3d 20 4e 55 4c 4c 3b  _fdcache = NULL;
06a0: 0a 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 66 69  .unsigned int fi
06b0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
06c0: 61 63 68 65 5f 73 69 7a 65 20 3d 20 30 3b 0a 0a  ache_size = 0;..
06d0: 2f 2a 2a 20 4c 6f 67 67 69 6e 67 20 2a 2a 2f 0a  /** Logging **/.
06e0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
06f0: 5f 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c 6f  _entry *filed_lo
0700: 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a 70 74 68 72  g_msg_list;.pthr
0710: 65 61 64 5f 6d 75 74 65 78 5f 74 20 66 69 6c 65  ead_mutex_t file
0720: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d  d_log_msg_list_m
0730: 75 74 65 78 3b 0a 70 74 68 72 65 61 64 5f 63 6f  utex;.pthread_co
0740: 6e 64 5f 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d  nd_t filed_log_m
0750: 73 67 5f 6c 69 73 74 5f 72 65 61 64 79 3b 0a 0a  sg_list_ready;..
0760: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 63 61  /* Initialize ca
0770: 63 68 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  che */.static in
0780: 74 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63  t filed_init_cac
0790: 68 65 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  he(unsigned int 
07a0: 63 61 63 68 65 5f 73 69 7a 65 29 20 7b 0a 09 75  cache_size) {..u
07b0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 64 78 3b  nsigned int idx;
07c0: 0a 09 69 6e 74 20 6d 75 74 65 78 5f 69 6e 69 74  ..int mutex_init
07d0: 5f 72 65 74 3b 0a 0a 09 2f 2a 20 43 61 63 68 65  _ret;.../* Cache
07e0: 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 2d 69   may not be re-i
07f0: 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 09 69  nitialized */..i
0800: 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  f (filed_fileinf
0810: 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 21  o_fdcache_size !
0820: 3d 20 30 20 7c 7c 20 66 69 6c 65 64 5f 66 69 6c  = 0 || filed_fil
0830: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 21 3d  einfo_fdcache !=
0840: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72   NULL) {...retur
0850: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 41 6c  n(1);..}.../* Al
0860: 6c 6f 63 61 74 65 20 63 61 63 68 65 20 2a 2f 0a  locate cache */.
0870: 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f  .filed_fileinfo_
0880: 66 64 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 63  fdcache_size = c
0890: 61 63 68 65 5f 73 69 7a 65 3b 0a 09 66 69 6c 65  ache_size;..file
08a0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63  d_fileinfo_fdcac
08b0: 68 65 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65  he = malloc(size
08c0: 6f 66 28 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e  of(*filed_filein
08d0: 66 6f 5f 66 64 63 61 63 68 65 29 20 2a 20 66 69  fo_fdcache) * fi
08e0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
08f0: 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20  ache_size);..if 
0900: 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f  (filed_fileinfo_
0910: 66 64 63 61 63 68 65 20 3d 3d 20 4e 55 4c 4c 29  fdcache == NULL)
0920: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a   {...return(1);.
0930: 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69  .}.../* Initiali
0940: 7a 65 20 63 61 63 68 65 20 65 6e 74 72 69 65 73  ze cache entries
0950: 20 2a 2f 0a 09 66 6f 72 20 28 69 64 78 20 3d 20   */..for (idx = 
0960: 30 3b 20 69 64 78 20 3c 20 66 69 6c 65 64 5f 66  0; idx < filed_f
0970: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f  ileinfo_fdcache_
0980: 73 69 7a 65 3b 20 69 64 78 2b 2b 29 20 7b 0a 09  size; idx++) {..
0990: 09 6d 75 74 65 78 5f 69 6e 69 74 5f 72 65 74 20  .mutex_init_ret 
09a0: 3d 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  = pthread_mutex_
09b0: 69 6e 69 74 28 26 66 69 6c 65 64 5f 66 69 6c 65  init(&filed_file
09c0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78  info_fdcache[idx
09d0: 5d 2e 6d 75 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a  ].mutex, NULL);.
09e0: 09 09 69 66 20 28 6d 75 74 65 78 5f 69 6e 69 74  ..if (mutex_init
09f0: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09  _ret != 0) {....
0a00: 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a  return(1);...}..
0a10: 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  ..filed_fileinfo
0a20: 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 70 61  _fdcache[idx].pa
0a30: 74 68 20 3d 20 73 74 72 64 75 70 28 22 22 29 3b  th = strdup("");
0a40: 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66  ...filed_fileinf
0a50: 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 66  o_fdcache[idx].f
0a60: 64 20 3d 20 2d 31 3b 0a 09 09 66 69 6c 65 64 5f  d = -1;...filed_
0a70: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65  fileinfo_fdcache
0a80: 5b 69 64 78 5d 2e 6c 61 73 74 6d 6f 64 20 3d 20  [idx].lastmod = 
0a90: 22 22 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65  "";...filed_file
0aa0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78  info_fdcache[idx
0ab0: 5d 2e 74 79 70 65 20 3d 20 22 22 3b 0a 09 7d 0a  ].type = "";..}.
0ac0: 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a  ..return(0);.}..
0ad0: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 72  /* Initialize pr
0ae0: 6f 63 65 73 73 20 2a 2f 0a 73 74 61 74 69 63 20  ocess */.static 
0af0: 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74 28 75  int filed_init(u
0b00: 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63 68  nsigned int cach
0b10: 65 5f 73 69 7a 65 29 20 7b 0a 09 73 74 61 74 69  e_size) {..stati
0b20: 63 20 69 6e 74 20 63 61 6c 6c 65 64 20 3d 20 30  c int called = 0
0b30: 3b 0a 09 69 6e 74 20 63 61 63 68 65 5f 72 65 74  ;..int cache_ret
0b40: 3b 0a 0a 09 69 66 20 28 63 61 6c 6c 65 64 29 20  ;...if (called) 
0b50: 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09  {...return(0);..
0b60: 7d 0a 0a 09 63 61 6c 6c 65 64 20 3d 20 31 3b 0a  }...called = 1;.
0b70: 0a 09 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 4c 5f 43  ..mlockall(MCL_C
0b80: 55 52 52 45 4e 54 20 7c 20 4d 43 4c 5f 46 55 54  URRENT | MCL_FUT
0b90: 55 52 45 29 3b 0a 0a 09 73 69 67 6e 61 6c 28 53  URE);...signal(S
0ba0: 49 47 50 49 50 45 2c 20 53 49 47 5f 49 47 4e 29  IGPIPE, SIG_IGN)
0bb0: 3b 0a 0a 09 63 61 63 68 65 5f 72 65 74 20 3d 20  ;...cache_ret = 
0bc0: 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 65  filed_init_cache
0bd0: 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69  (cache_size);..i
0be0: 66 20 28 63 61 63 68 65 5f 72 65 74 20 21 3d 20  f (cache_ret != 
0bf0: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 63 61  0) {...return(ca
0c00: 63 68 65 5f 72 65 74 29 3b 0a 09 7d 0a 0a 09 72  che_ret);..}...r
0c10: 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20  eturn(0);.}../* 
0c20: 4c 69 73 74 65 6e 20 6f 6e 20 61 20 70 61 72 74  Listen on a part
0c30: 69 63 75 6c 61 72 20 61 64 64 72 65 73 73 2f 70  icular address/p
0c40: 6f 72 74 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ort */.static in
0c50: 74 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 63  t filed_listen(c
0c60: 6f 6e 73 74 20 63 68 61 72 20 2a 61 64 64 72 65  onst char *addre
0c70: 73 73 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ss, unsigned int
0c80: 20 70 6f 72 74 29 20 7b 0a 09 73 74 72 75 63 74   port) {..struct
0c90: 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 64   sockaddr_in6 ad
0ca0: 64 72 5f 76 36 3b 0a 09 73 74 72 75 63 74 20 73  dr_v6;..struct s
0cb0: 6f 63 6b 61 64 64 72 5f 69 6e 20 61 64 64 72 5f  ockaddr_in addr_
0cc0: 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b  v4;..struct sock
0cd0: 61 64 64 72 20 2a 61 64 64 72 3b 0a 09 73 6f 63  addr *addr;..soc
0ce0: 6b 6c 65 6e 5f 74 20 61 64 64 72 5f 6c 65 6e 3b  klen_t addr_len;
0cf0: 0a 09 69 6e 74 20 70 74 6f 6e 5f 72 65 74 2c 20  ..int pton_ret, 
0d00: 62 69 6e 64 5f 72 65 74 2c 20 6c 69 73 74 65 6e  bind_ret, listen
0d10: 5f 72 65 74 3b 0a 09 69 6e 74 20 66 61 6d 69 6c  _ret;..int famil
0d20: 79 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 0a 09 66  y;..int fd;....f
0d30: 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36  amily = AF_INET6
0d40: 3b 0a 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e  ;..pton_ret = in
0d50: 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20  et_pton(family, 
0d60: 61 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f 76  address, &addr_v
0d70: 36 2e 73 69 6e 36 5f 61 64 64 72 2e 73 36 5f 61  6.sin6_addr.s6_a
0d80: 64 64 72 29 3b 0a 09 69 66 20 28 70 74 6f 6e 5f  ddr);..if (pton_
0d90: 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 66 61  ret != 1) {...fa
0da0: 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 3b 0a  mily = AF_INET;.
0db0: 09 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e 65  ..pton_ret = ine
0dc0: 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 61  t_pton(family, a
0dd0: 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f 76 34  ddress, &addr_v4
0de0: 2e 73 69 6e 5f 61 64 64 72 2e 73 5f 61 64 64 72  .sin_addr.s_addr
0df0: 29 3b 0a 09 09 69 66 20 28 70 74 6f 6e 5f 72 65  );...if (pton_re
0e00: 74 20 21 3d 20 31 29 20 7b 0a 09 09 09 72 65 74  t != 1) {....ret
0e10: 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 0a 09 09  urn(-1);...}....
0e20: 61 64 64 72 5f 76 34 2e 73 69 6e 5f 66 61 6d 69  addr_v4.sin_fami
0e30: 6c 79 20 3d 20 66 61 6d 69 6c 79 3b 0a 09 09 61  ly = family;...a
0e40: 64 64 72 5f 76 34 2e 73 69 6e 5f 70 6f 72 74 20  ddr_v4.sin_port 
0e50: 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a  = htons(port);..
0e60: 09 09 61 64 64 72 20 3d 20 28 73 74 72 75 63 74  ..addr = (struct
0e70: 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64   sockaddr *) &ad
0e80: 64 72 5f 76 34 3b 0a 09 09 61 64 64 72 5f 6c 65  dr_v4;...addr_le
0e90: 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 5f  n = sizeof(addr_
0ea0: 76 34 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09  v4);..} else {..
0eb0: 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 61  .addr_v6.sin6_fa
0ec0: 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 3b  mily = AF_INET6;
0ed0: 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f  ...addr_v6.sin6_
0ee0: 66 6c 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a 09 09  flowinfo = 0;...
0ef0: 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 73 63 6f  addr_v6.sin6_sco
0f00: 70 65 5f 69 64 20 3d 20 30 3b 0a 09 09 61 64 64  pe_id = 0;...add
0f10: 72 5f 76 36 2e 73 69 6e 36 5f 70 6f 72 74 20 3d  r_v6.sin6_port =
0f20: 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a 09   htons(port);...
0f30: 09 61 64 64 72 20 3d 20 28 73 74 72 75 63 74 20  .addr = (struct 
0f40: 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64  sockaddr *) &add
0f50: 72 5f 76 36 3b 0a 09 09 61 64 64 72 5f 6c 65 6e  r_v6;...addr_len
0f60: 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 5f 76   = sizeof(addr_v
0f70: 36 29 3b 0a 09 7d 0a 0a 09 66 64 20 3d 20 73 6f  6);..}...fd = so
0f80: 63 6b 65 74 28 66 61 6d 69 6c 79 2c 20 53 4f 43  cket(family, SOC
0f90: 4b 5f 53 54 52 45 41 4d 2c 20 30 29 3b 0a 09 69  K_STREAM, 0);..i
0fa0: 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 72  f (fd < 0) {...r
0fb0: 65 74 75 72 6e 28 66 64 29 3b 0a 09 7d 0a 0a 09  eturn(fd);..}...
0fc0: 62 69 6e 64 5f 72 65 74 20 3d 20 62 69 6e 64 28  bind_ret = bind(
0fd0: 66 64 2c 20 61 64 64 72 2c 20 61 64 64 72 5f 6c  fd, addr, addr_l
0fe0: 65 6e 29 3b 0a 09 69 66 20 28 62 69 6e 64 5f 72  en);..if (bind_r
0ff0: 65 74 20 3c 20 30 29 20 7b 0a 09 09 63 6c 6f 73  et < 0) {...clos
1000: 65 28 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 6e  e(fd);....return
1010: 28 2d 31 29 3b 0a 09 7d 0a 0a 09 6c 69 73 74 65  (-1);..}...liste
1020: 6e 5f 72 65 74 20 3d 20 6c 69 73 74 65 6e 28 66  n_ret = listen(f
1030: 64 2c 20 31 32 38 29 3b 0a 09 69 66 20 28 6c 69  d, 128);..if (li
1040: 73 74 65 6e 5f 72 65 74 20 21 3d 20 30 29 20 7b  sten_ret != 0) {
1050: 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09  ...close(fd);...
1060: 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a  .return(-1);..}.
1070: 0a 09 72 65 74 75 72 6e 28 66 64 29 3b 0a 7d 0a  ..return(fd);.}.
1080: 0a 2f 2a 20 4c 6f 67 20 61 20 6d 65 73 73 61 67  ./* Log a messag
1090: 65 20 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c 45  e */.#ifdef FILE
10a0: 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 23 20 20 64 65  D_DONT_LOG.#  de
10b0: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 67 69  fine filed_loggi
10c0: 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 78  ng_thread_init(x
10d0: 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20 66 69  ) 0.#  define fi
10e0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75  led_log_msg_debu
10f0: 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23  g(x, ...) /**/.#
1100: 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c    define filed_l
1110: 6f 67 5f 6d 73 67 28 78 2c 20 2e 2e 2e 29 20 2f  og_msg(x, ...) /
1120: 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 69  **/.#  define fi
1130: 6c 65 64 5f 6c 6f 67 5f 69 70 28 78 2c 20 2e 2e  led_log_ip(x, ..
1140: 2e 29 20 22 3c 75 6e 6b 6e 6f 77 6e 3e 22 0a 23  .) "<unknown>".#
1150: 65 6c 73 65 0a 23 69 66 64 65 66 20 46 49 4c 45  else.#ifdef FILE
1160: 44 5f 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e  D_DEBUG.#  defin
1170: 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  e filed_log_msg_
1180: 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20 7b 20  debug(x, ...) { 
1190: 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
11a0: 78 2c 20 5f 5f 56 41 5f 41 52 47 53 5f 5f 29 3b  x, __VA_ARGS__);
11b0: 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
11c0: 20 22 5c 6e 22 29 3b 20 66 66 6c 75 73 68 28 73   "\n"); fflush(s
11d0: 74 64 65 72 72 29 3b 20 7d 0a 23 65 6c 73 65 0a  tderr); }.#else.
11e0: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f  #  define filed_
11f0: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c  log_msg_debug(x,
1200: 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 65 6e 64 69   ...) /**/.#endi
1210: 66 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  f../* Initialize
1220: 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64 20   logging thread 
1230: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
1240: 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68  filed_logging_th
1250: 72 65 61 64 28 76 6f 69 64 20 2a 61 72 67 5f 70  read(void *arg_p
1260: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65  ) {..struct file
1270: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64  d_logging_thread
1280: 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 73 74 72  _args *arg;..str
1290: 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e  uct filed_log_en
12a0: 74 72 79 20 2a 63 75 72 72 2c 20 2a 70 72 65 76  try *curr, *prev
12b0: 3b 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 61  ;..FILE *fp;...a
12c0: 72 67 20 3d 20 61 72 67 5f 70 3b 0a 0a 09 66 70  rg = arg_p;...fp
12d0: 20 3d 20 61 72 67 2d 3e 66 70 3b 0a 0a 09 77 68   = arg->fp;...wh
12e0: 69 6c 65 20 28 31 29 20 7b 0a 09 09 70 74 68 72  ile (1) {...pthr
12f0: 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26  ead_mutex_lock(&
1300: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69  filed_log_msg_li
1310: 73 74 5f 6d 75 74 65 78 29 3b 0a 09 09 70 74 68  st_mutex);...pth
1320: 72 65 61 64 5f 63 6f 6e 64 5f 77 61 69 74 28 26  read_cond_wait(&
1330: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69  filed_log_msg_li
1340: 73 74 5f 72 65 61 64 79 2c 20 26 66 69 6c 65 64  st_ready, &filed
1350: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75  _log_msg_list_mu
1360: 74 65 78 29 3b 0a 0a 09 09 63 75 72 72 20 3d 20  tex);....curr = 
1370: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69  filed_log_msg_li
1380: 73 74 3b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f  st;...filed_log_
1390: 6d 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b  msg_list = NULL;
13a0: 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65  ....pthread_mute
13b0: 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f  x_unlock(&filed_
13c0: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74  log_msg_list_mut
13d0: 65 78 29 3b 0a 0a 09 09 70 72 65 76 20 3d 20 4e  ex);....prev = N
13e0: 55 4c 4c 3b 0a 09 09 66 6f 72 20 28 3b 20 63 75  ULL;...for (; cu
13f0: 72 72 3b 20 63 75 72 72 20 3d 20 63 75 72 72 2d  rr; curr = curr-
1400: 3e 5f 6e 65 78 74 29 20 7b 0a 09 09 09 63 75 72  >_next) {....cur
1410: 72 2d 3e 5f 70 72 65 76 20 3d 20 70 72 65 76 3b  r->_prev = prev;
1420: 0a 0a 09 09 09 70 72 65 76 20 3d 20 63 75 72 72  .....prev = curr
1430: 3b 0a 09 09 7d 0a 0a 09 09 63 75 72 72 20 3d 20  ;...}....curr = 
1440: 70 72 65 76 3b 0a 09 09 77 68 69 6c 65 20 28 63  prev;...while (c
1450: 75 72 72 29 20 7b 0a 09 09 09 66 70 72 69 6e 74  urr) {....fprint
1460: 66 28 66 70 2c 20 22 25 73 20 54 48 52 45 41 44  f(fp, "%s THREAD
1470: 3d 25 6c 6c 75 5c 6e 22 2c 20 63 75 72 72 2d 3e  =%llu\n", curr->
1480: 62 75 66 66 65 72 2c 20 28 75 6e 73 69 67 6e 65  buffer, (unsigne
1490: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72  d long long) cur
14a0: 72 2d 3e 74 68 72 65 61 64 29 3b 0a 09 09 09 66  r->thread);....f
14b0: 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 09 09 70  flush(fp);.....p
14c0: 72 65 76 20 3d 20 63 75 72 72 3b 0a 09 09 09 63  rev = curr;....c
14d0: 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f 70 72 65  urr = curr->_pre
14e0: 76 3b 0a 0a 09 09 09 66 72 65 65 28 70 72 65 76  v;.....free(prev
14f0: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75  );...}..}...retu
1500: 72 6e 28 4e 55 4c 4c 29 3b 0a 7d 0a 0a 73 74 61  rn(NULL);.}..sta
1510: 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 6c 6f  tic int filed_lo
1520: 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69  gging_thread_ini
1530: 74 28 46 49 4c 45 20 2a 6c 6f 67 66 70 29 20 7b  t(FILE *logfp) {
1540: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c  ..struct filed_l
1550: 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 72  ogging_thread_ar
1560: 67 73 20 2a 61 72 67 73 3b 0a 09 70 74 68 72 65  gs *args;..pthre
1570: 61 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b 0a  ad_t thread_id;.
1580: 0a 09 61 72 67 73 20 3d 20 6d 61 6c 6c 6f 63 28  ..args = malloc(
1590: 73 69 7a 65 6f 66 28 2a 61 72 67 73 29 29 3b 0a  sizeof(*args));.
15a0: 09 61 72 67 73 2d 3e 66 70 20 3d 20 6c 6f 67 66  .args->fp = logf
15b0: 70 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  p;...filed_log_m
15c0: 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a  sg_list = NULL;.
15d0: 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  ..pthread_mutex_
15e0: 69 6e 69 74 28 26 66 69 6c 65 64 5f 6c 6f 67 5f  init(&filed_log_
15f0: 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 2c 20  msg_list_mutex, 
1600: 4e 55 4c 4c 29 3b 0a 0a 09 70 74 68 72 65 61 64  NULL);...pthread
1610: 5f 63 72 65 61 74 65 28 26 74 68 72 65 61 64 5f  _create(&thread_
1620: 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f  id, NULL, filed_
1630: 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 2c 20  logging_thread, 
1640: 61 72 67 73 29 3b 0a 0a 09 72 65 74 75 72 6e 28  args);...return(
1650: 30 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  0);.}..static vo
1660: 69 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  id filed_log_msg
1670: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d 74  (const char *fmt
1680: 2c 20 2e 2e 2e 29 20 7b 0a 09 73 74 72 75 63 74  , ...) {..struct
1690: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79   filed_log_entry
16a0: 20 2a 65 6e 74 72 79 3b 0a 09 76 61 5f 6c 69 73   *entry;..va_lis
16b0: 74 20 61 72 67 73 3b 0a 0a 09 65 6e 74 72 79 20  t args;...entry 
16c0: 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  = malloc(sizeof(
16d0: 2a 65 6e 74 72 79 29 29 3b 0a 0a 09 76 61 5f 73  *entry));...va_s
16e0: 74 61 72 74 28 61 72 67 73 2c 20 66 6d 74 29 3b  tart(args, fmt);
16f0: 0a 0a 09 76 73 6e 70 72 69 6e 74 66 28 65 6e 74  ...vsnprintf(ent
1700: 72 79 2d 3e 62 75 66 66 65 72 2c 20 73 69 7a 65  ry->buffer, size
1710: 6f 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 65 72  of(entry->buffer
1720: 29 2c 20 66 6d 74 2c 20 61 72 67 73 29 3b 0a 0a  ), fmt, args);..
1730: 09 76 61 5f 65 6e 64 28 61 72 67 73 29 3b 0a 0a  .va_end(args);..
1740: 09 65 6e 74 72 79 2d 3e 74 68 72 65 61 64 20 3d  .entry->thread =
1750: 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b   pthread_self();
1760: 0a 09 65 6e 74 72 79 2d 3e 6c 65 76 65 6c 20 3d  ..entry->level =
1770: 20 30 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75   0;...pthread_mu
1780: 74 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f  tex_lock(&filed_
1790: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74  log_msg_list_mut
17a0: 65 78 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 5f 6e  ex);...entry->_n
17b0: 65 78 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f  ext = filed_log_
17c0: 6d 73 67 5f 6c 69 73 74 3b 0a 09 66 69 6c 65 64  msg_list;..filed
17d0: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20  _log_msg_list = 
17e0: 65 6e 74 72 79 3b 0a 0a 09 70 74 68 72 65 61 64  entry;...pthread
17f0: 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66  _mutex_unlock(&f
1800: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73  iled_log_msg_lis
1810: 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 70 74 68 72  t_mutex);...pthr
1820: 65 61 64 5f 63 6f 6e 64 5f 73 69 67 6e 61 6c 28  ead_cond_signal(
1830: 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c  &filed_log_msg_l
1840: 69 73 74 5f 72 65 61 64 79 29 3b 0a 0a 09 72 65  ist_ready);...re
1850: 74 75 72 6e 3b 0a 7d 0a 0a 0a 73 74 61 74 69 63  turn;.}...static
1860: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
1870: 65 64 5f 6c 6f 67 5f 69 70 28 73 74 72 75 63 74  ed_log_ip(struct
1880: 20 73 6f 63 6b 61 64 64 72 20 2a 61 64 64 72 2c   sockaddr *addr,
1890: 20 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 73   char *buffer, s
18a0: 69 7a 65 5f 74 20 62 75 66 66 65 72 6c 65 6e 29  ize_t bufferlen)
18b0: 20 7b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61   {..struct socka
18c0: 64 64 72 5f 69 6e 20 2a 61 64 64 72 5f 76 34 3b  ddr_in *addr_v4;
18d0: 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64  ..struct sockadd
18e0: 72 5f 69 6e 36 20 2a 61 64 64 72 5f 76 36 3b 0a  r_in6 *addr_v6;.
18f0: 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 74  .const char *ret
1900: 76 61 6c 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 61 64  val = NULL;...ad
1910: 64 72 5f 76 36 20 3d 20 28 73 74 72 75 63 74 20  dr_v6 = (struct 
1920: 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20  sockaddr_in6 *) 
1930: 61 64 64 72 3b 0a 0a 09 73 77 69 74 63 68 20 28  addr;...switch (
1940: 61 64 64 72 5f 76 36 2d 3e 73 69 6e 36 5f 66 61  addr_v6->sin6_fa
1950: 6d 69 6c 79 29 20 7b 0a 09 09 63 61 73 65 20 41  mily) {...case A
1960: 46 5f 49 4e 45 54 3a 0a 09 09 09 61 64 64 72 5f  F_INET:....addr_
1970: 76 34 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63  v4 = (struct soc
1980: 6b 61 64 64 72 5f 69 6e 20 2a 29 20 61 64 64 72  kaddr_in *) addr
1990: 3b 0a 09 09 09 72 65 74 76 61 6c 20 3d 20 69 6e  ;....retval = in
19a0: 65 74 5f 6e 74 6f 70 28 41 46 5f 49 4e 45 54 2c  et_ntop(AF_INET,
19b0: 20 26 61 64 64 72 5f 76 34 2d 3e 73 69 6e 5f 61   &addr_v4->sin_a
19c0: 64 64 72 2c 20 62 75 66 66 65 72 2c 20 62 75 66  ddr, buffer, buf
19d0: 66 65 72 6c 65 6e 29 3b 0a 09 09 09 62 72 65 61  ferlen);....brea
19e0: 6b 3b 0a 09 09 63 61 73 65 20 41 46 5f 49 4e 45  k;...case AF_INE
19f0: 54 36 3a 0a 09 09 09 72 65 74 76 61 6c 20 3d 20  T6:....retval = 
1a00: 69 6e 65 74 5f 6e 74 6f 70 28 41 46 5f 49 4e 45  inet_ntop(AF_INE
1a10: 54 36 2c 20 26 61 64 64 72 5f 76 36 2d 3e 73 69  T6, &addr_v6->si
1a20: 6e 36 5f 61 64 64 72 2c 20 62 75 66 66 65 72 2c  n6_addr, buffer,
1a30: 20 62 75 66 66 65 72 6c 65 6e 29 3b 0a 09 09 09   bufferlen);....
1a40: 62 72 65 61 6b 3b 0a 09 7d 0a 0a 09 69 66 20 28  break;..}...if (
1a50: 72 65 74 76 61 6c 20 3d 3d 20 4e 55 4c 4c 29 20  retval == NULL) 
1a60: 7b 0a 09 09 72 65 74 76 61 6c 20 3d 20 22 3c 75  {...retval = "<u
1a70: 6e 6b 6e 6f 77 6e 3e 22 3b 0a 09 7d 0a 0a 09 72  nknown>";..}...r
1a80: 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d  eturn(retval);.}
1a90: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 6d  .#endif../* Form
1aa0: 61 74 20 74 69 6d 65 20 70 65 72 20 52 46 43 32  at time per RFC2
1ab0: 36 31 36 20 2a 2f 0a 73 74 61 74 69 63 20 63 68  616 */.static ch
1ac0: 61 72 20 2a 66 69 6c 65 64 5f 66 6f 72 6d 61 74  ar *filed_format
1ad0: 5f 74 69 6d 65 28 63 68 61 72 20 2a 62 75 66 66  _time(char *buff
1ae0: 65 72 2c 20 73 69 7a 65 5f 74 20 62 75 66 66 65  er, size_t buffe
1af0: 72 5f 6c 65 6e 2c 20 63 6f 6e 73 74 20 74 69 6d  r_len, const tim
1b00: 65 5f 74 20 74 69 6d 65 69 6e 66 6f 29 20 7b 0a  e_t timeinfo) {.
1b10: 09 73 74 72 75 63 74 20 74 6d 20 74 69 6d 65 69  .struct tm timei
1b20: 6e 66 6f 5f 74 6d 2c 20 2a 74 69 6d 65 69 6e 66  nfo_tm, *timeinf
1b30: 6f 5f 74 6d 5f 70 3b 0a 0a 09 74 69 6d 65 69 6e  o_tm_p;...timein
1b40: 66 6f 5f 74 6d 5f 70 20 3d 20 67 6d 74 69 6d 65  fo_tm_p = gmtime
1b50: 5f 72 28 26 74 69 6d 65 69 6e 66 6f 2c 20 26 74  _r(&timeinfo, &t
1b60: 69 6d 65 69 6e 66 6f 5f 74 6d 29 3b 0a 09 69 66  imeinfo_tm);..if
1b70: 20 28 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20   (timeinfo_tm_p 
1b80: 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74  == NULL) {...ret
1b90: 75 72 6e 28 22 75 6e 6b 6e 6f 77 6e 22 29 3b 0a  urn("unknown");.
1ba0: 09 7d 0a 0a 09 62 75 66 66 65 72 5b 62 75 66 66  .}...buffer[buff
1bb0: 65 72 5f 6c 65 6e 20 2d 20 31 5d 20 3d 20 27 5c  er_len - 1] = '\
1bc0: 30 27 3b 0a 09 62 75 66 66 65 72 5f 6c 65 6e 20  0';..buffer_len 
1bd0: 3d 20 73 74 72 66 74 69 6d 65 28 62 75 66 66 65  = strftime(buffe
1be0: 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20  r, buffer_len - 
1bf0: 31 2c 20 22 25 61 2c 20 25 64 20 25 62 20 25 59  1, "%a, %d %b %Y
1c00: 20 25 48 3a 25 4d 3a 25 53 20 47 4d 54 22 2c 20   %H:%M:%S GMT", 
1c10: 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 29 3b 0a  timeinfo_tm_p);.
1c20: 0a 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 29  ..return(buffer)
1c30: 3b 0a 7d 0a 0a 2f 2a 20 68 61 73 68 20 2a 2f 0a  ;.}../* hash */.
1c40: 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20  static unsigned 
1c50: 69 6e 74 20 66 69 6c 65 64 5f 68 61 73 68 28 63  int filed_hash(c
1c60: 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1c70: 61 72 20 2a 76 61 6c 75 65 2c 20 75 6e 73 69 67  ar *value, unsig
1c80: 6e 65 64 20 69 6e 74 20 6d 6f 64 75 6c 75 73 29  ned int modulus)
1c90: 20 7b 0a 09 75 6e 73 69 67 6e 65 64 20 63 68 61   {..unsigned cha
1ca0: 72 20 63 75 72 72 2c 20 70 72 65 76 3b 0a 09 69  r curr, prev;..i
1cb0: 6e 74 20 64 69 66 66 3b 0a 09 75 6e 73 69 67 6e  nt diff;..unsign
1cc0: 65 64 20 69 6e 74 20 72 65 74 76 61 6c 3b 0a 0a  ed int retval;..
1cd0: 09 72 65 74 76 61 6c 20 3d 20 6d 6f 64 75 6c 75  .retval = modulu
1ce0: 73 20 2d 20 31 3b 0a 09 70 72 65 76 20 3d 20 6d  s - 1;..prev = m
1cf0: 6f 64 75 6c 75 73 20 25 20 32 35 35 3b 0a 0a 09  odulus % 255;...
1d00: 77 68 69 6c 65 20 28 28 63 75 72 72 20 3d 20 2a  while ((curr = *
1d10: 76 61 6c 75 65 29 29 20 7b 0a 09 09 69 66 20 28  value)) {...if (
1d20: 63 75 72 72 20 3c 20 33 32 29 20 7b 0a 09 09 09  curr < 32) {....
1d30: 63 75 72 72 20 3d 20 32 35 35 20 2d 20 63 75 72  curr = 255 - cur
1d40: 72 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09  r;...} else {...
1d50: 09 63 75 72 72 20 2d 3d 20 33 32 3b 0a 09 09 7d  .curr -= 32;...}
1d60: 0a 0a 09 09 69 66 20 28 70 72 65 76 20 3c 20 63  ....if (prev < c
1d70: 75 72 72 29 20 7b 0a 09 09 09 64 69 66 66 20 3d  urr) {....diff =
1d80: 20 63 75 72 72 20 2d 20 70 72 65 76 3b 0a 09 09   curr - prev;...
1d90: 7d 20 65 6c 73 65 20 7b 0a 09 09 09 64 69 66 66  } else {....diff
1da0: 20 3d 20 70 72 65 76 20 2d 20 63 75 72 72 3b 0a   = prev - curr;.
1db0: 09 09 7d 0a 0a 09 09 70 72 65 76 20 3d 20 63 75  ..}....prev = cu
1dc0: 72 72 3b 0a 0a 09 09 72 65 74 76 61 6c 20 3c 3c  rr;....retval <<
1dd0: 3d 20 33 3b 0a 09 09 72 65 74 76 61 6c 20 26 3d  = 3;...retval &=
1de0: 20 30 78 46 46 46 46 46 46 46 46 4c 55 3b 0a 09   0xFFFFFFFFLU;..
1df0: 09 72 65 74 76 61 6c 20 5e 3d 20 64 69 66 66 3b  .retval ^= diff;
1e00: 0a 0a 09 09 76 61 6c 75 65 2b 2b 3b 0a 09 7d 0a  ....value++;..}.
1e10: 0a 09 72 65 74 76 61 6c 20 3d 20 72 65 74 76 61  ..retval = retva
1e20: 6c 20 25 20 6d 6f 64 75 6c 75 73 3b 0a 0a 09 72  l % modulus;...r
1e30: 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d  eturn(retval);.}
1e40: 0a 0a 2f 2a 20 46 69 6e 64 20 61 20 6d 69 6d 65  ../* Find a mime
1e50: 2d 74 79 70 65 20 62 61 73 65 64 20 6f 6e 20 74  -type based on t
1e60: 68 65 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 73  he filename */.s
1e70: 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
1e80: 20 2a 66 69 6c 65 64 5f 64 65 74 65 72 6d 69 6e   *filed_determin
1e90: 65 5f 6d 69 6d 65 74 79 70 65 28 63 6f 6e 73 74  e_mimetype(const
1ea0: 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 09   char *path) {..
1eb0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 3b 0a 0a  const char *p;..
1ec0: 09 70 20 3d 20 73 74 72 72 63 68 72 28 70 61 74  .p = strrchr(pat
1ed0: 68 2c 20 27 2e 27 29 3b 0a 09 69 66 20 28 70 20  h, '.');..if (p 
1ee0: 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74  == NULL) {...ret
1ef0: 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c  urn(FILED_DEFAUL
1f00: 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 70 2b  T_TYPE);..}...p+
1f10: 2b 3b 0a 09 69 66 20 28 2a 70 20 3d 3d 20 27 5c  +;..if (*p == '\
1f20: 30 27 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 46  0') {...return(F
1f30: 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50  ILED_DEFAULT_TYP
1f40: 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 6c  E);..}...filed_l
1f50: 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 4c 6f  og_msg_debug("Lo
1f60: 6f 6b 69 6e 67 20 75 70 20 4d 49 4d 45 20 74 79  oking up MIME ty
1f70: 70 65 20 66 6f 72 20 25 73 20 28 68 61 73 68 20  pe for %s (hash 
1f80: 3d 20 25 6c 6c 75 29 22 2c 20 70 2c 20 28 75 6e  = %llu)", p, (un
1f90: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
1fa0: 29 20 66 69 6c 65 64 5f 68 61 73 68 28 28 63 6f  ) filed_hash((co
1fb0: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1fc0: 72 20 2a 29 20 70 2c 20 31 36 37 37 37 32 35 39  r *) p, 16777259
1fd0: 29 29 3b 0a 0a 23 69 6e 63 6c 75 64 65 20 22 66  ));..#include "f
1fe0: 69 6c 65 64 2d 6d 69 6d 65 2d 74 79 70 65 73 2e  iled-mime-types.
1ff0: 68 22 0a 0a 09 72 65 74 75 72 6e 28 46 49 4c 45  h"...return(FILE
2000: 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b  D_DEFAULT_TYPE);
2010: 0a 7d 0a 0a 2f 2a 20 4f 70 65 6e 20 61 20 66 69  .}../* Open a fi
2020: 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 66 69  le and return fi
2030: 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a  le information *
2040: 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
2050: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a  filed_fileinfo *
2060: 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 28  filed_open_file(
2070: 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68  const char *path
2080: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66  , struct filed_f
2090: 69 6c 65 69 6e 66 6f 20 2a 62 75 66 66 65 72 29  ileinfo *buffer)
20a0: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64   {..struct filed
20b0: 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 63 68 65  _fileinfo *cache
20c0: 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 6f  ;..const char *o
20d0: 70 65 6e 5f 70 61 74 68 3b 0a 09 75 6e 73 69 67  pen_path;..unsig
20e0: 6e 65 64 20 69 6e 74 20 63 61 63 68 65 5f 69 64  ned int cache_id
20f0: 78 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09  x;..off_t len;..
2100: 69 6e 74 20 66 64 3b 0a 0a 09 63 61 63 68 65 5f  int fd;...cache_
2110: 69 64 78 20 3d 20 66 69 6c 65 64 5f 68 61 73 68  idx = filed_hash
2120: 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  ((const unsigned
2130: 20 63 68 61 72 20 2a 29 20 70 61 74 68 2c 20 66   char *) path, f
2140: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64  iled_fileinfo_fd
2150: 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 0a 09 63  cache_size);...c
2160: 61 63 68 65 20 3d 20 26 66 69 6c 65 64 5f 66 69  ache = &filed_fi
2170: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 63  leinfo_fdcache[c
2180: 61 63 68 65 5f 69 64 78 5d 3b 0a 0a 09 66 69 6c  ache_idx];...fil
2190: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67  ed_log_msg_debug
21a0: 28 22 4c 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20  ("Locking mutex 
21b0: 66 6f 72 20 69 64 78 3a 20 25 6c 75 22 2c 20 28  for idx: %lu", (
21c0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63  unsigned long) c
21d0: 61 63 68 65 5f 69 64 78 29 3b 0a 0a 09 70 74 68  ache_idx);...pth
21e0: 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28  read_mutex_lock(
21f0: 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a  &cache->mutex);.
2200: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  ..filed_log_msg_
2210: 64 65 62 75 67 28 22 43 6f 6d 70 6c 65 74 65 64  debug("Completed
2220: 20 6c 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20 66   locking mutex f
2230: 6f 72 20 69 64 78 3a 20 25 6c 75 22 2c 20 28 75  or idx: %lu", (u
2240: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61  nsigned long) ca
2250: 63 68 65 5f 69 64 78 29 3b 0a 0a 09 69 66 20 28  che_idx);...if (
2260: 73 74 72 63 6d 70 28 70 61 74 68 2c 20 63 61 63  strcmp(path, cac
2270: 68 65 2d 3e 70 61 74 68 29 20 21 3d 20 30 29 20  he->path) != 0) 
2280: 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  {...filed_log_ms
2290: 67 5f 64 65 62 75 67 28 22 43 61 63 68 65 20 6d  g_debug("Cache m
22a0: 69 73 73 20 66 6f 72 20 69 64 78 3a 20 25 6c 75  iss for idx: %lu
22b0: 3a 20 4f 4c 44 20 5c 22 25 73 5c 22 2c 20 4e 45  : OLD \"%s\", NE
22c0: 57 20 5c 22 25 73 5c 22 22 2c 20 28 75 6e 73 69  W \"%s\"", (unsi
22d0: 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65  gned long) cache
22e0: 5f 69 64 78 2c 20 63 61 63 68 65 2d 3e 70 61 74  _idx, cache->pat
22f0: 68 2c 20 70 61 74 68 29 3b 0a 0a 09 09 2f 2a 20  h, path);..../* 
2300: 46 6f 72 20 72 65 71 75 65 73 74 73 20 66 6f 72  For requests for
2310: 20 74 68 65 20 72 6f 6f 74 20 64 69 72 65 63 74   the root direct
2320: 6f 72 79 2c 20 73 65 72 76 65 20 6f 75 74 20 69  ory, serve out i
2330: 6e 64 65 78 2e 68 74 6d 6c 20 2a 2f 0a 09 09 69  ndex.html */...i
2340: 66 20 28 70 61 74 68 5b 30 5d 20 3d 3d 20 27 5c  f (path[0] == '\
2350: 30 27 20 7c 7c 20 28 70 61 74 68 5b 30 5d 20 3d  0' || (path[0] =
2360: 3d 20 27 2f 27 20 26 26 20 70 61 74 68 5b 31 5d  = '/' && path[1]
2370: 20 3d 3d 20 27 5c 30 27 29 29 20 7b 0a 09 09 09   == '\0')) {....
2380: 6f 70 65 6e 5f 70 61 74 68 20 3d 20 22 2f 69 6e  open_path = "/in
2390: 64 65 78 2e 68 74 6d 6c 22 3b 0a 09 09 7d 20 65  dex.html";...} e
23a0: 6c 73 65 20 7b 0a 09 09 09 6f 70 65 6e 5f 70 61  lse {....open_pa
23b0: 74 68 20 3d 20 70 61 74 68 3b 0a 09 09 7d 0a 0a  th = path;...}..
23c0: 09 09 66 64 20 3d 20 6f 70 65 6e 28 6f 70 65 6e  ..fd = open(open
23d0: 5f 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 20  _path, O_RDONLY 
23e0: 7c 20 4f 5f 4c 41 52 47 45 46 49 4c 45 29 3b 0a  | O_LARGEFILE);.
23f0: 09 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a  ..if (fd < 0) {.
2400: 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78  ...pthread_mutex
2410: 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e  _unlock(&cache->
2420: 6d 75 74 65 78 29 3b 0a 0a 09 09 09 72 65 74 75  mutex);.....retu
2430: 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09  rn(NULL);...}...
2440: 09 66 72 65 65 28 63 61 63 68 65 2d 3e 70 61 74  .free(cache->pat
2450: 68 29 3b 0a 09 09 69 66 20 28 63 61 63 68 65 2d  h);...if (cache-
2460: 3e 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09 09 63  >fd >= 0) {....c
2470: 6c 6f 73 65 28 63 61 63 68 65 2d 3e 66 64 29 3b  lose(cache->fd);
2480: 0a 09 09 7d 0a 0a 09 09 6c 65 6e 20 3d 20 6c 73  ...}....len = ls
2490: 65 65 6b 28 66 64 2c 20 30 2c 20 53 45 45 4b 5f  eek(fd, 0, SEEK_
24a0: 45 4e 44 29 3b 0a 09 09 6c 73 65 65 6b 28 66 64  END);...lseek(fd
24b0: 2c 20 30 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a  , 0, SEEK_SET);.
24c0: 0a 09 09 63 61 63 68 65 2d 3e 66 64 20 3d 20 66  ...cache->fd = f
24d0: 64 3b 0a 09 09 63 61 63 68 65 2d 3e 6c 65 6e 20  d;...cache->len 
24e0: 3d 20 6c 65 6e 3b 0a 09 09 63 61 63 68 65 2d 3e  = len;...cache->
24f0: 70 61 74 68 20 3d 20 73 74 72 64 75 70 28 70 61  path = strdup(pa
2500: 74 68 29 3b 0a 09 09 63 61 63 68 65 2d 3e 74 79  th);...cache->ty
2510: 70 65 20 3d 20 66 69 6c 65 64 5f 64 65 74 65 72  pe = filed_deter
2520: 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 6f 70  mine_mimetype(op
2530: 65 6e 5f 70 61 74 68 29 3b 0a 0a 09 09 2f 2a 20  en_path);..../* 
2540: 58 58 58 3a 54 4f 44 4f 3a 20 44 65 74 65 72 6d  XXX:TODO: Determ
2550: 69 6e 65 20 2a 2f 0a 09 09 63 61 63 68 65 2d 3e  ine */...cache->
2560: 6c 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 64 5f  lastmod = filed_
2570: 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 63 68  format_time(cach
2580: 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69  e->lastmod_b, si
2590: 7a 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 73 74  zeof(cache->last
25a0: 6d 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c  mod_b), time(NUL
25b0: 4c 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 6c 73  L) - 30);..} els
25c0: 65 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f  e {...filed_log_
25d0: 6d 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65  msg_debug("Cache
25e0: 20 68 69 74 20 66 6f 72 20 69 64 78 3a 20 25 6c   hit for idx: %l
25f0: 75 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 22 2c  u: PATH \"%s\"",
2600: 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29   (unsigned long)
2610: 20 63 61 63 68 65 5f 69 64 78 2c 20 70 61 74 68   cache_idx, path
2620: 29 3b 0a 09 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 57  );..}.../*.. * W
2630: 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61  e have to make a
2640: 20 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20 62   duplicate FD, b
2650: 65 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20 72  ecause once we r
2660: 65 6c 65 61 73 65 20 74 68 65 20 63 61 63 68 65  elease the cache
2670: 0a 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 65 20  .. * mutex, the 
2680: 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
2690: 6d 61 79 20 62 65 20 63 6c 6f 73 65 64 0a 09 20  may be closed.. 
26a0: 2a 2f 0a 09 66 64 20 3d 20 64 75 70 28 63 61 63  */..fd = dup(cac
26b0: 68 65 2d 3e 66 64 29 3b 0a 09 69 66 20 28 66 64  he->fd);..if (fd
26c0: 20 3c 20 30 29 20 7b 0a 09 09 70 74 68 72 65 61   < 0) {...pthrea
26d0: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26  d_mutex_unlock(&
26e0: 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a  cache->mutex);..
26f0: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a  ..return(NULL);.
2700: 09 7d 0a 0a 09 62 75 66 66 65 72 2d 3e 66 64 20  .}...buffer->fd 
2710: 3d 20 66 64 3b 0a 09 62 75 66 66 65 72 2d 3e 6c  = fd;..buffer->l
2720: 65 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e 3b  en = cache->len;
2730: 0a 09 62 75 66 66 65 72 2d 3e 74 79 70 65 20 3d  ..buffer->type =
2740: 20 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a 09 6d   cache->type;..m
2750: 65 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 6c 61  emcpy(buffer->la
2760: 73 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d 3e  stmod_b, cache->
2770: 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f  lastmod_b, sizeo
2780: 66 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f  f(buffer->lastmo
2790: 64 5f 62 29 29 3b 0a 09 62 75 66 66 65 72 2d 3e  d_b));..buffer->
27a0: 6c 61 73 74 6d 6f 64 20 3d 20 62 75 66 66 65 72  lastmod = buffer
27b0: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 20 2b 20 28 63  ->lastmod_b + (c
27c0: 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 2d 20  ache->lastmod - 
27d0: 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62  cache->lastmod_b
27e0: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74  );...pthread_mut
27f0: 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65  ex_unlock(&cache
2800: 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 72 65 74 75  ->mutex);...retu
2810: 72 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f  rn(buffer);.}../
2820: 2a 20 50 72 6f 63 65 73 73 20 61 6e 20 48 54 54  * Process an HTT
2830: 50 20 72 65 71 75 65 73 74 20 61 6e 64 20 72 65  P request and re
2840: 74 75 72 6e 20 74 68 65 20 70 61 74 68 20 72 65  turn the path re
2850: 71 75 65 73 74 65 64 20 2a 2f 0a 73 74 61 74 69  quested */.stati
2860: 63 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68  c struct filed_h
2870: 74 74 70 5f 72 65 71 75 65 73 74 20 2a 66 69 6c  ttp_request *fil
2880: 65 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71 75  ed_get_http_requ
2890: 65 73 74 28 46 49 4c 45 20 2a 66 70 2c 20 73 74  est(FILE *fp, st
28a0: 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f  ruct filed_http_
28b0: 72 65 71 75 65 73 74 20 2a 62 75 66 66 65 72 5f  request *buffer_
28c0: 73 74 29 20 7b 0a 09 63 68 61 72 20 2a 6d 65 74  st) {..char *met
28d0: 68 6f 64 2c 20 2a 70 61 74 68 3b 0a 09 63 68 61  hod, *path;..cha
28e0: 72 20 2a 62 75 66 66 65 72 2c 20 2a 74 6d 70 62  r *buffer, *tmpb
28f0: 75 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66  uffer, *workbuff
2900: 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72 5f  er, *workbuffer_
2910: 6e 65 78 74 3b 0a 09 73 69 7a 65 5f 74 20 62 75  next;..size_t bu
2920: 66 66 65 72 5f 6c 65 6e 2c 20 74 6d 70 62 75 66  ffer_len, tmpbuf
2930: 66 65 72 5f 6c 65 6e 3b 0a 09 6f 66 66 5f 74 20  fer_len;..off_t 
2940: 72 61 6e 67 65 5f 73 74 61 72 74 2c 20 72 61 6e  range_start, ran
2950: 67 65 5f 65 6e 64 2c 20 72 61 6e 67 65 5f 6c 65  ge_end, range_le
2960: 6e 67 74 68 3b 0a 09 69 6e 74 20 72 61 6e 67 65  ngth;..int range
2970: 5f 72 65 71 75 65 73 74 3b 0a 09 69 6e 74 20 66  _request;..int f
2980: 64 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 66 64 20  d;..int i;...fd 
2990: 3d 20 66 69 6c 65 6e 6f 28 66 70 29 3b 0a 0a 09  = fileno(fp);...
29a0: 72 61 6e 67 65 5f 73 74 61 72 74 20 3d 20 30 3b  range_start = 0;
29b0: 0a 09 72 61 6e 67 65 5f 65 6e 64 20 20 20 3d 20  ..range_end   = 
29c0: 30 3b 0a 09 72 61 6e 67 65 5f 72 65 71 75 65 73  0;..range_reques
29d0: 74 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 6c 65  t = 0;..range_le
29e0: 6e 67 74 68 20 3d 20 2d 31 3b 0a 0a 09 62 75 66  ngth = -1;...buf
29f0: 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d  fer = buffer_st-
2a00: 3e 70 61 74 68 5f 62 3b 0a 09 62 75 66 66 65 72  >path_b;..buffer
2a10: 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75  _len = sizeof(bu
2a20: 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 5f 62 29  ffer_st->path_b)
2a30: 3b 0a 0a 09 74 6d 70 62 75 66 66 65 72 20 3d 20  ;...tmpbuffer = 
2a40: 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75  buffer_st->tmpbu
2a50: 66 3b 0a 09 74 6d 70 62 75 66 66 65 72 5f 6c 65  f;..tmpbuffer_le
2a60: 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 66 65  n = sizeof(buffe
2a70: 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a  r_st->tmpbuf);..
2a80: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22  .filed_log_msg("
2a90: 57 41 49 54 5f 46 4f 52 5f 52 45 51 55 45 53 54  WAIT_FOR_REQUEST
2aa0: 20 46 44 3d 25 69 22 2c 20 66 64 29 3b 0a 0a 09   FD=%i", fd);...
2ab0: 66 67 65 74 73 28 62 75 66 66 65 72 2c 20 62 75  fgets(buffer, bu
2ac0: 66 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 0a  ffer_len, fp);..
2ad0: 09 6d 65 74 68 6f 64 20 3d 20 62 75 66 66 65 72  .method = buffer
2ae0: 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 73 74 72  ;...buffer = str
2af0: 63 68 72 28 62 75 66 66 65 72 2c 20 27 20 27 29  chr(buffer, ' ')
2b00: 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20 3d 3d  ;..if (buffer ==
2b10: 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64   NULL) {...filed
2b20: 5f 6c 6f 67 5f 6d 73 67 28 22 47 4f 54 5f 52 45  _log_msg("GOT_RE
2b30: 51 55 45 53 54 20 46 44 3d 25 69 20 45 52 52 4f  QUEST FD=%i ERRO
2b40: 52 3d 66 6f 72 6d 61 74 22 2c 20 66 64 29 3b 0a  R=format", fd);.
2b50: 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b  ...return(NULL);
2b60: 0a 09 7d 0a 0a 09 2a 62 75 66 66 65 72 20 3d 20  ..}...*buffer = 
2b70: 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 2b 2b 3b  '\0';..buffer++;
2b80: 0a 0a 09 70 61 74 68 20 3d 20 62 75 66 66 65 72  ...path = buffer
2b90: 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 73 74 72  ;...buffer = str
2ba0: 63 68 72 28 62 75 66 66 65 72 2c 20 27 20 27 29  chr(buffer, ' ')
2bb0: 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20 21 3d  ;..if (buffer !=
2bc0: 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 62 75 66 66   NULL) {...*buff
2bd0: 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 62 75 66  er = '\0';...buf
2be0: 66 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 66 69 6c 65  fer++;..}...file
2bf0: 64 5f 6c 6f 67 5f 6d 73 67 28 22 47 4f 54 5f 52  d_log_msg("GOT_R
2c00: 45 51 55 45 53 54 20 46 44 3d 25 69 20 50 41 54  EQUEST FD=%i PAT
2c10: 48 3d 25 73 22 2c 20 66 64 2c 20 70 61 74 68 29  H=%s", fd, path)
2c20: 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  ;...filed_log_ms
2c30: 67 28 22 57 41 49 54 5f 46 4f 52 5f 48 45 41 44  g("WAIT_FOR_HEAD
2c40: 45 52 53 20 46 44 3d 25 69 22 2c 20 66 64 29 3b  ERS FD=%i", fd);
2c50: 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 69  ...for (i = 0; i
2c60: 20 3c 20 31 30 30 3b 20 69 2b 2b 29 20 7b 0a 09   < 100; i++) {..
2c70: 09 66 67 65 74 73 28 74 6d 70 62 75 66 66 65 72  .fgets(tmpbuffer
2c80: 2c 20 74 6d 70 62 75 66 66 65 72 5f 6c 65 6e 2c  , tmpbuffer_len,
2c90: 20 66 70 29 3b 0a 0a 09 09 69 66 20 28 73 74 72   fp);....if (str
2ca0: 6e 63 61 73 65 63 6d 70 28 74 6d 70 62 75 66 66  ncasecmp(tmpbuff
2cb0: 65 72 2c 20 22 52 61 6e 67 65 3a 20 22 2c 20 37  er, "Range: ", 7
2cc0: 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 77 6f 72  ) == 0) {....wor
2cd0: 6b 62 75 66 66 65 72 20 3d 20 74 6d 70 62 75 66  kbuffer = tmpbuf
2ce0: 66 65 72 20 2b 20 37 3b 0a 0a 09 09 09 69 66 20  fer + 7;.....if 
2cf0: 28 73 74 72 6e 63 61 73 65 63 6d 70 28 77 6f 72  (strncasecmp(wor
2d00: 6b 62 75 66 66 65 72 2c 20 22 62 79 74 65 73 3d  kbuffer, "bytes=
2d10: 22 2c 20 36 29 20 3d 3d 20 30 29 20 7b 0a 09 09  ", 6) == 0) {...
2d20: 09 09 77 6f 72 6b 62 75 66 66 65 72 20 2b 3d 20  ..workbuffer += 
2d30: 36 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f 72 65  6;......range_re
2d40: 71 75 65 73 74 20 3d 20 31 3b 0a 0a 09 09 09 09  quest = 1;......
2d50: 72 61 6e 67 65 5f 73 74 61 72 74 20 3d 20 73 74  range_start = st
2d60: 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66 65  rtoull(workbuffe
2d70: 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 72 5f 6e  r, &workbuffer_n
2d80: 65 78 74 2c 20 31 30 29 3b 0a 0a 09 09 09 09 77  ext, 10);......w
2d90: 6f 72 6b 62 75 66 66 65 72 20 3d 20 77 6f 72 6b  orkbuffer = work
2da0: 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 0a 09 09  buffer_next;....
2db0: 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 66 66 65  ..if (*workbuffe
2dc0: 72 20 3d 3d 20 27 2d 27 29 20 7b 0a 09 09 09 09  r == '-') {.....
2dd0: 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a 0a  .workbuffer++;..
2de0: 09 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75  .....if (*workbu
2df0: 66 66 65 72 20 21 3d 20 27 5c 72 27 20 26 26 20  ffer != '\r' && 
2e00: 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 27  *workbuffer != '
2e10: 5c 6e 27 29 20 7b 0a 09 09 09 09 09 09 72 61 6e  \n') {.......ran
2e20: 67 65 5f 65 6e 64 20 3d 20 73 74 72 74 6f 75 6c  ge_end = strtoul
2e30: 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 26 77  l(workbuffer, &w
2e40: 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 2c 20  orkbuffer_next, 
2e50: 31 30 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09  10);......}.....
2e60: 7d 0a 09 09 09 7d 0a 09 09 7d 0a 0a 09 09 69 66  }....}...}....if
2e70: 20 28 6d 65 6d 63 6d 70 28 74 6d 70 62 75 66 66   (memcmp(tmpbuff
2e80: 65 72 2c 20 22 5c 72 5c 6e 22 2c 20 32 29 20 3d  er, "\r\n", 2) =
2e90: 3d 20 30 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b  = 0) {....break;
2ea0: 0a 09 09 7d 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f  ...}..}...filed_
2eb0: 6c 6f 67 5f 6d 73 67 28 22 47 4f 54 5f 48 45 41  log_msg("GOT_HEA
2ec0: 44 45 52 53 20 46 44 3d 25 69 22 2c 20 66 64 29  DERS FD=%i", fd)
2ed0: 3b 0a 0a 09 2f 2a 20 57 65 20 6f 6e 6c 79 20 68  ;.../* We only h
2ee0: 61 6e 64 6c 65 20 74 68 65 20 22 47 45 54 22 20  andle the "GET" 
2ef0: 6d 65 74 68 6f 64 20 2a 2f 0a 09 69 66 20 28 73  method */..if (s
2f00: 74 72 63 61 73 65 63 6d 70 28 6d 65 74 68 6f 64  trcasecmp(method
2f10: 2c 20 22 67 65 74 22 29 20 21 3d 20 30 29 20 7b  , "get") != 0) {
2f20: 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b  ...return(NULL);
2f30: 0a 09 7d 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69  ..}.../* Determi
2f40: 6e 65 20 72 61 6e 67 65 20 2a 2f 0a 09 69 66 20  ne range */..if 
2f50: 28 72 61 6e 67 65 5f 65 6e 64 20 21 3d 20 30 29  (range_end != 0)
2f60: 20 7b 0a 09 09 69 66 20 28 72 61 6e 67 65 5f 65   {...if (range_e
2f70: 6e 64 20 3c 3d 20 72 61 6e 67 65 5f 73 74 61 72  nd <= range_star
2f80: 74 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 4e  t) {....return(N
2f90: 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 72 61 6e  ULL);...}....ran
2fa0: 67 65 5f 6c 65 6e 67 74 68 20 3d 20 72 61 6e 67  ge_length = rang
2fb0: 65 5f 65 6e 64 20 2d 20 72 61 6e 67 65 5f 73 74  e_end - range_st
2fc0: 61 72 74 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f  art;....filed_lo
2fd0: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d  g_msg_debug("Com
2fe0: 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 70 61  puting length pa
2ff0: 72 61 6d 65 74 65 72 3a 20 25 6c 6c 75 20 3d 20  rameter: %llu = 
3000: 25 6c 6c 75 20 2d 20 25 6c 6c 75 22 2c 0a 09 09  %llu - %llu",...
3010: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
3020: 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 6c 65 6e 67  long) range_leng
3030: 74 68 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64  th,....(unsigned
3040: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67   long long) rang
3050: 65 5f 65 6e 64 2c 0a 09 09 09 28 75 6e 73 69 67  e_end,....(unsig
3060: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72  ned long long) r
3070: 61 6e 67 65 5f 73 74 61 72 74 0a 09 09 29 3b 0a  ange_start...);.
3080: 09 7d 0a 0a 09 2f 2a 20 46 69 6c 6c 20 75 70 20  .}.../* Fill up 
3090: 73 74 72 75 63 74 75 72 65 20 74 6f 20 72 65 74  structure to ret
30a0: 75 72 6e 20 2a 2f 0a 09 62 75 66 66 65 72 5f 73  urn */..buffer_s
30b0: 74 2d 3e 70 61 74 68 20 20 20 3d 20 70 61 74 68  t->path   = path
30c0: 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65  ;..buffer_st->he
30d0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 73  aders.range.pres
30e0: 65 6e 74 20 3d 20 72 61 6e 67 65 5f 72 65 71 75  ent = range_requ
30f0: 65 73 74 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d  est;..buffer_st-
3100: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f  >headers.range.o
3110: 66 66 73 65 74 20 20 3d 20 72 61 6e 67 65 5f 73  ffset  = range_s
3120: 74 61 72 74 3b 0a 09 62 75 66 66 65 72 5f 73 74  tart;..buffer_st
3130: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
3140: 6c 65 6e 67 74 68 20 20 3d 20 72 61 6e 67 65 5f  length  = range_
3150: 6c 65 6e 67 74 68 3b 0a 0a 09 72 65 74 75 72 6e  length;...return
3160: 28 62 75 66 66 65 72 5f 73 74 29 3b 0a 0a 09 2f  (buffer_st);.../
3170: 2a 20 4d 61 6b 65 20 63 6f 6d 70 69 6c 65 72 20  * Make compiler 
3180: 68 61 70 70 79 20 2a 2f 0a 09 66 64 20 3d 20 66  happy */..fd = f
3190: 64 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  d;.}../* Return 
31a0: 61 6e 20 65 72 72 6f 72 20 70 61 67 65 20 2a 2f  an error page */
31b0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
31c0: 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 46 49  ed_error_page(FI
31d0: 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68  LE *fp, const ch
31e0: 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 6e 74  ar *date_current
31f0: 2c 20 69 6e 74 20 65 72 72 6f 72 5f 6e 75 6d 62  , int error_numb
3200: 65 72 29 20 7b 0a 09 63 68 61 72 20 2a 65 72 72  er) {..char *err
3210: 6f 72 5f 73 74 72 69 6e 67 20 3d 20 22 3c 68 74  or_string = "<ht
3220: 6d 6c 3e 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e  ml><head><title>
3230: 45 52 52 4f 52 3c 2f 74 69 74 6c 65 3e 3c 2f 68  ERROR</title></h
3240: 65 61 64 3e 3c 62 6f 64 79 3e 55 6e 61 62 6c 65  ead><body>Unable
3250: 20 74 6f 20 70 72 6f 63 65 73 73 20 72 65 71 75   to process requ
3260: 65 73 74 3c 2f 62 6f 64 79 3e 3c 2f 68 74 6d 6c  est</body></html
3270: 3e 22 3b 0a 0a 09 66 70 72 69 6e 74 66 28 66 70  >";...fprintf(fp
3280: 2c 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4e  , "HTTP/1.1 %i N
3290: 6f 74 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25  ot OK\r\nDate: %
32a0: 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c  s\r\nServer: fil
32b0: 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66  ed\r\nLast-Modif
32c0: 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65  ied: %s\r\nConte
32d0: 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c  nt-Length: %llu\
32e0: 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a  r\nContent-Type:
32f0: 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f   %s\r\nConnectio
3300: 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 5c 72 5c 6e  n: close\r\n\r\n
3310: 25 73 22 2c 0a 09 09 65 72 72 6f 72 5f 6e 75 6d  %s",...error_num
3320: 62 65 72 2c 0a 09 09 64 61 74 65 5f 63 75 72 72  ber,...date_curr
3330: 65 6e 74 2c 0a 09 09 64 61 74 65 5f 63 75 72 72  ent,...date_curr
3340: 65 6e 74 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64  ent,...(unsigned
3350: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 73 74 72 6c   long long) strl
3360: 65 6e 28 65 72 72 6f 72 5f 73 74 72 69 6e 67 29  en(error_string)
3370: 2c 0a 09 09 22 74 65 78 74 2f 68 74 6d 6c 22 2c  ,..."text/html",
3380: 0a 09 09 65 72 72 6f 72 5f 73 74 72 69 6e 67 0a  ...error_string.
3390: 09 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e 64 6c 65  .);.}../* Handle
33a0: 20 61 20 73 69 6e 67 6c 65 20 72 65 71 75 65 73   a single reques
33b0: 74 20 66 72 6f 6d 20 61 20 63 6c 69 65 6e 74 20  t from a client 
33c0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
33d0: 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69 65  iled_handle_clie
33e0: 6e 74 28 69 6e 74 20 66 64 2c 20 73 74 72 75 63  nt(int fd, struc
33f0: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71  t filed_http_req
3400: 75 65 73 74 20 2a 72 65 71 75 65 73 74 29 20 7b  uest *request) {
3410: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66  ..struct filed_f
3420: 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 69 6e 66  ileinfo *fileinf
3430: 6f 3b 0a 09 73 73 69 7a 65 5f 74 20 73 65 6e 64  o;..ssize_t send
3440: 66 69 6c 65 5f 72 65 74 3b 0a 09 73 69 7a 65 5f  file_ret;..size_
3450: 74 20 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 3b  t sendfile_size;
3460: 0a 09 6f 66 66 5f 74 20 73 65 6e 64 66 69 6c 65  ..off_t sendfile
3470: 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 66 69 6c  _offset, sendfil
3480: 65 5f 73 65 6e 74 2c 20 73 65 6e 64 66 69 6c 65  e_sent, sendfile
3490: 5f 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 70 61 74  _len;..char *pat
34a0: 68 3b 0a 09 63 68 61 72 20 2a 64 61 74 65 5f 63  h;..char *date_c
34b0: 75 72 72 65 6e 74 2c 20 64 61 74 65 5f 63 75 72  urrent, date_cur
34c0: 72 65 6e 74 5f 62 5b 36 34 5d 3b 0a 09 69 6e 74  rent_b[64];..int
34d0: 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 46 49 4c   http_code;..FIL
34e0: 45 20 2a 66 70 3b 0a 0a 09 2f 2a 20 44 65 74 65  E *fp;.../* Dete
34f0: 72 6d 69 6e 65 20 63 75 72 72 65 6e 74 20 74 69  rmine current ti
3500: 6d 65 20 2a 2f 0a 09 64 61 74 65 5f 63 75 72 72  me */..date_curr
3510: 65 6e 74 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d  ent = filed_form
3520: 61 74 5f 74 69 6d 65 28 64 61 74 65 5f 63 75 72  at_time(date_cur
3530: 72 65 6e 74 5f 62 2c 20 73 69 7a 65 6f 66 28 64  rent_b, sizeof(d
3540: 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 29 2c 20  ate_current_b), 
3550: 74 69 6d 65 28 4e 55 4c 4c 29 29 3b 0a 0a 09 2f  time(NULL));.../
3560: 2a 20 4f 70 65 6e 20 73 6f 63 6b 65 74 20 61 73  * Open socket as
3570: 20 41 4e 53 49 20 49 2f 4f 20 66 6f 72 20 65 61   ANSI I/O for ea
3580: 73 65 20 6f 66 20 75 73 65 20 2a 2f 0a 09 66 70  se of use */..fp
3590: 20 3d 20 66 64 6f 70 65 6e 28 66 64 2c 20 22 77   = fdopen(fd, "w
35a0: 2b 62 22 29 3b 0a 09 69 66 20 28 66 70 20 3d 3d  +b");..if (fp ==
35b0: 20 4e 55 4c 4c 29 20 7b 0a 09 09 63 6c 6f 73 65   NULL) {...close
35c0: 28 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 6e 3b  (fd);....return;
35d0: 0a 09 7d 0a 0a 09 72 65 71 75 65 73 74 20 3d 20  ..}...request = 
35e0: 66 69 6c 65 64 5f 67 65 74 5f 68 74 74 70 5f 72  filed_get_http_r
35f0: 65 71 75 65 73 74 28 66 70 2c 20 72 65 71 75 65  equest(fp, reque
3600: 73 74 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 65  st);...if (reque
3610: 73 74 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 72 65  st == NULL || re
3620: 71 75 65 73 74 2d 3e 70 61 74 68 20 3d 3d 20 4e  quest->path == N
3630: 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65  ULL) {...filed_e
3640: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61  rror_page(fp, da
3650: 74 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 30 29  te_current, 500)
3660: 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  ;....filed_log_m
3670: 73 67 28 22 49 4e 56 41 4c 49 44 5f 52 45 51 55  sg("INVALID_REQU
3680: 45 53 54 20 46 44 3d 25 69 20 45 52 52 4f 52 3d  EST FD=%i ERROR=
3690: 35 30 30 22 2c 20 66 64 29 3b 0a 0a 09 09 66 63  500", fd);....fc
36a0: 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 09 72 65 74  lose(fp);....ret
36b0: 75 72 6e 3b 0a 09 7d 0a 0a 09 69 66 20 28 72 65  urn;..}...if (re
36c0: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
36d0: 61 6e 67 65 2e 70 72 65 73 65 6e 74 29 20 7b 0a  ange.present) {.
36e0: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28  ..filed_log_msg(
36f0: 22 50 52 4f 43 45 53 53 5f 52 45 50 4c 59 5f 53  "PROCESS_REPLY_S
3700: 54 41 52 54 20 46 44 3d 25 69 20 50 41 54 48 3d  TART FD=%i PATH=
3710: 25 73 20 52 41 4e 47 45 5f 53 54 41 52 54 3d 25  %s RANGE_START=%
3720: 6c 6c 75 20 52 41 4e 47 45 5f 4c 45 4e 47 54 48  llu RANGE_LENGTH
3730: 3d 25 6c 6c 75 22 2c 0a 09 09 09 66 64 2c 0a 09  =%llu",....fd,..
3740: 09 09 72 65 71 75 65 73 74 2d 3e 70 61 74 68 2c  ..request->path,
3750: 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f  ....(unsigned lo
3760: 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74  ng long) request
3770: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
3780: 6f 66 66 73 65 74 2c 0a 09 09 09 28 75 6e 73 69  offset,....(unsi
3790: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
37a0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
37b0: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 0a 09 09  .range.length...
37c0: 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 66  );..} else {...f
37d0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 50 52  iled_log_msg("PR
37e0: 4f 43 45 53 53 5f 52 45 50 4c 59 5f 53 54 41 52  OCESS_REPLY_STAR
37f0: 54 20 46 44 3d 25 69 20 50 41 54 48 3d 25 73 22  T FD=%i PATH=%s"
3800: 2c 20 66 64 2c 20 72 65 71 75 65 73 74 2d 3e 70  , fd, request->p
3810: 61 74 68 29 3b 0a 09 7d 0a 0a 09 70 61 74 68 20  ath);..}...path 
3820: 3d 20 72 65 71 75 65 73 74 2d 3e 70 61 74 68 3b  = request->path;
3830: 0a 0a 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 2d  ...http_code = -
3840: 31 3b 0a 0a 09 66 69 6c 65 69 6e 66 6f 20 3d 20  1;...fileinfo = 
3850: 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 28  filed_open_file(
3860: 70 61 74 68 2c 20 26 72 65 71 75 65 73 74 2d 3e  path, &request->
3870: 66 69 6c 65 69 6e 66 6f 29 3b 0a 09 69 66 20 28  fileinfo);..if (
3880: 66 69 6c 65 69 6e 66 6f 20 3d 3d 20 4e 55 4c 4c  fileinfo == NULL
3890: 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65 72 72 6f  ) {...filed_erro
38a0: 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f  r_page(fp, date_
38b0: 63 75 72 72 65 6e 74 2c 20 34 30 34 29 3b 0a 0a  current, 404);..
38c0: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28  ..filed_log_msg(
38d0: 22 50 52 4f 43 45 53 53 5f 52 45 50 4c 59 5f 43  "PROCESS_REPLY_C
38e0: 4f 4d 50 4c 45 54 45 20 46 44 3d 25 69 20 45 52  OMPLETE FD=%i ER
38f0: 52 4f 52 3d 34 30 34 22 2c 20 66 64 29 3b 0a 09  ROR=404", fd);..
3900: 7d 20 65 6c 73 65 20 7b 0a 09 09 69 66 20 28 72  } else {...if (r
3910: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
3920: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 21 3d 20  range.offset != 
3930: 30 20 7c 7c 20 72 65 71 75 65 73 74 2d 3e 68 65  0 || request->he
3940: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
3950: 74 68 20 3e 3d 20 30 29 20 7b 0a 09 09 09 69 66  th >= 0) {....if
3960: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65   (request->heade
3970: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20  rs.range.offset 
3980: 3e 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e  >= fileinfo->len
3990: 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f 6c 6f  ) {.....filed_lo
39a0: 67 5f 6d 73 67 28 22 50 52 4f 43 45 53 53 5f 52  g_msg("PROCESS_R
39b0: 45 50 4c 59 5f 43 4f 4d 50 4c 45 54 45 20 46 44  EPLY_COMPLETE FD
39c0: 3d 25 69 20 45 52 52 4f 52 3d 34 31 36 22 2c 20  =%i ERROR=416", 
39d0: 66 64 29 3b 0a 0a 09 09 09 09 66 69 6c 65 64 5f  fd);......filed_
39e0: 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64  error_page(fp, d
39f0: 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 31 36  ate_current, 416
3a00: 29 3b 0a 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09  );....} else {..
3a10: 09 09 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e  ...if (request->
3a20: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65  headers.range.le
3a30: 6e 67 74 68 20 3c 20 30 29 20 7b 0a 09 09 09 09  ngth < 0) {.....
3a40: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64  .filed_log_msg_d
3a50: 65 62 75 67 28 22 43 6f 6d 70 75 74 69 6e 67 20  ebug("Computing 
3a60: 6c 65 6e 67 74 68 20 74 6f 20 66 69 74 20 69 6e  length to fit in
3a70: 20 62 6f 75 6e 64 73 3a 20 66 69 6c 65 69 6e 66   bounds: fileinf
3a80: 6f 2d 3e 6c 65 6e 20 3d 20 25 6c 6c 75 2c 20 72  o->len = %llu, r
3a90: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
3aa0: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d 20 25  range.offset = %
3ab0: 6c 6c 75 22 2c 0a 09 09 09 09 09 09 28 75 6e 73  llu",.......(uns
3ac0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
3ad0: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 2c 0a   fileinfo->len,.
3ae0: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ......(unsigned 
3af0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65  long long) reque
3b00: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
3b10: 65 2e 6f 66 66 73 65 74 0a 09 09 09 09 09 29 3b  e.offset......);
3b20: 0a 0a 09 09 09 09 09 72 65 71 75 65 73 74 2d 3e  .......request->
3b30: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65  headers.range.le
3b40: 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d  ngth = fileinfo-
3b50: 3e 6c 65 6e 20 2d 20 72 65 71 75 65 73 74 2d 3e  >len - request->
3b60: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66  headers.range.of
3b70: 66 73 65 74 3b 0a 09 09 09 09 7d 0a 0a 09 09 09  fset;.....}.....
3b80: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64  .filed_log_msg_d
3b90: 65 62 75 67 28 22 50 61 72 74 69 61 6c 20 72 65  ebug("Partial re
3ba0: 71 75 65 73 74 2c 20 73 74 61 72 74 69 6e 67 20  quest, starting 
3bb0: 61 74 3a 20 25 6c 6c 75 20 61 6e 64 20 72 75 6e  at: %llu and run
3bc0: 6e 69 6e 67 20 66 6f 72 20 25 6c 6c 75 20 62 79  ning for %llu by
3bd0: 74 65 73 22 2c 0a 09 09 09 09 09 28 75 6e 73 69  tes",......(unsi
3be0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
3bf0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
3c00: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09  .range.offset,..
3c10: 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f  ....(unsigned lo
3c20: 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74  ng long) request
3c30: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
3c40: 6c 65 6e 67 74 68 0a 09 09 09 09 29 3b 0a 0a 09  length.....);...
3c50: 09 09 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 32  ...http_code = 2
3c60: 30 36 3b 0a 09 09 09 7d 0a 09 09 7d 20 65 6c 73  06;....}...} els
3c70: 65 20 7b 0a 09 09 09 69 66 20 28 72 65 71 75 65  e {....if (reque
3c80: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
3c90: 65 2e 70 72 65 73 65 6e 74 29 20 7b 0a 09 09 09  e.present) {....
3ca0: 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 36  .http_code = 206
3cb0: 3b 0a 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09  ;....} else {...
3cc0: 09 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 30  ..http_code = 20
3cd0: 30 3b 0a 09 09 09 7d 0a 09 09 09 72 65 71 75 65  0;....}....reque
3ce0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
3cf0: 65 2e 6f 66 66 73 65 74 20 3d 20 30 3b 0a 09 09  e.offset = 0;...
3d00: 09 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72  .request->header
3d10: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d  s.range.length =
3d20: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a   fileinfo->len;.
3d30: 09 09 7d 0a 0a 09 09 69 66 20 28 68 74 74 70 5f  ..}....if (http_
3d40: 63 6f 64 65 20 3e 20 30 29 20 7b 0a 09 09 09 66  code > 0) {....f
3d50: 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50  printf(fp, "HTTP
3d60: 2f 31 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61  /1.1 %i OK\r\nDa
3d70: 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 72  te: %s\r\nServer
3d80: 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d  : filed\r\nLast-
3d90: 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e  Modified: %s\r\n
3da0: 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20  Content-Length: 
3db0: 25 6c 6c 75 5c 72 5c 6e 41 63 63 65 70 74 2d 52  %llu\r\nAccept-R
3dc0: 61 6e 67 65 73 3a 20 62 79 74 65 73 5c 72 5c 6e  anges: bytes\r\n
3dd0: 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25 73  Content-Type: %s
3de0: 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20  \r\nConnection: 
3df0: 63 6c 6f 73 65 5c 72 5c 6e 22 2c 0a 09 09 09 09  close\r\n",.....
3e00: 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 09 09 64  http_code,.....d
3e10: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 09  ate_current,....
3e20: 09 66 69 6c 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d  .fileinfo->lastm
3e30: 6f 64 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65  od,.....(unsigne
3e40: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71  d long long) req
3e50: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  uest->headers.ra
3e60: 6e 67 65 2e 6c 65 6e 67 74 68 2c 0a 09 09 09 09  nge.length,.....
3e70: 66 69 6c 65 69 6e 66 6f 2d 3e 74 79 70 65 0a 09  fileinfo->type..
3e80: 09 09 29 3b 0a 09 09 09 69 66 20 28 68 74 74 70  ..);....if (http
3e90: 5f 63 6f 64 65 20 3d 3d 20 32 30 36 29 20 7b 0a  _code == 206) {.
3ea0: 09 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20  ....fprintf(fp, 
3eb0: 22 43 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20  "Content-Range: 
3ec0: 62 79 74 65 73 20 25 6c 6c 75 2d 25 6c 6c 75 2f  bytes %llu-%llu/
3ed0: 25 6c 6c 75 5c 72 5c 6e 22 2c 0a 09 09 09 09 09  %llu\r\n",......
3ee0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
3ef0: 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65  ong) request->he
3f00: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73  aders.range.offs
3f10: 65 74 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 6e  et,......(unsign
3f20: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 28 72  ed long long) (r
3f30: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
3f40: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 2b 20 72  range.offset + r
3f50: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
3f60: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 2d 20 31  range.length - 1
3f70: 29 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65  ),......(unsigne
3f80: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c  d long long) fil
3f90: 65 69 6e 66 6f 2d 3e 6c 65 6e 0a 09 09 09 09 29  einfo->len.....)
3fa0: 3b 0a 09 09 09 7d 0a 09 09 09 66 70 72 69 6e 74  ;....}....fprint
3fb0: 66 28 66 70 2c 20 22 5c 72 5c 6e 22 29 3b 0a 09  f(fp, "\r\n");..
3fc0: 09 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09  ..fflush(fp);...
3fd0: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28  ..filed_log_msg(
3fe0: 22 50 52 4f 43 45 53 53 5f 52 45 50 4c 59 5f 43  "PROCESS_REPLY_C
3ff0: 4f 4d 50 4c 45 54 45 20 46 44 3d 25 69 20 53 54  OMPLETE FD=%i ST
4000: 41 54 55 53 3d 25 69 22 2c 20 66 64 2c 20 68 74  ATUS=%i", fd, ht
4010: 74 70 5f 63 6f 64 65 29 3b 0a 0a 23 69 66 64 65  tp_code);..#ifde
4020: 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b  f FILED_NONBLOCK
4030: 5f 48 54 54 50 0a 09 09 09 69 6e 74 20 73 6f 63  _HTTP....int soc
4040: 6b 65 74 5f 66 6c 61 67 73 3b 0a 09 09 09 66 64  ket_flags;....fd
4050: 5f 73 65 74 20 72 66 64 2c 20 77 66 64 3b 0a 09  _set rfd, wfd;..
4060: 09 09 63 68 61 72 20 73 69 6e 6b 62 75 66 5b 38  ..char sinkbuf[8
4070: 31 39 32 5d 3b 0a 09 09 09 73 73 69 7a 65 5f 74  192];....ssize_t
4080: 20 72 65 61 64 5f 72 65 74 3b 0a 0a 09 09 09 46   read_ret;.....F
4090: 44 5f 5a 45 52 4f 28 26 72 66 64 29 3b 0a 09 09  D_ZERO(&rfd);...
40a0: 09 46 44 5f 5a 45 52 4f 28 26 77 66 64 29 3b 0a  .FD_ZERO(&wfd);.
40b0: 09 09 09 46 44 5f 53 45 54 28 66 64 2c 20 26 72  ...FD_SET(fd, &r
40c0: 66 64 29 3b 0a 09 09 09 46 44 5f 53 45 54 28 66  fd);....FD_SET(f
40d0: 64 2c 20 26 77 66 64 29 3b 0a 0a 09 09 09 73 6f  d, &wfd);.....so
40e0: 63 6b 65 74 5f 66 6c 61 67 73 20 3d 20 66 63 6e  cket_flags = fcn
40f0: 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 4c 29 3b  tl(fd, F_GETFL);
4100: 0a 09 09 09 66 63 6e 74 6c 28 66 64 2c 20 46 5f  ....fcntl(fd, F_
4110: 53 45 54 46 4c 2c 20 73 6f 63 6b 65 74 5f 66 6c  SETFL, socket_fl
4120: 61 67 73 20 7c 20 4f 5f 4e 4f 4e 42 4c 4f 43 4b  ags | O_NONBLOCK
4130: 29 3b 0a 23 65 6e 64 69 66 0a 0a 09 09 09 66 69  );.#endif.....fi
4140: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 53 45 4e  led_log_msg("SEN
4150: 44 5f 53 54 41 52 54 20 46 49 4c 45 5f 46 44 3d  D_START FILE_FD=
4160: 25 69 20 46 44 3d 25 69 20 42 59 54 45 53 3d 25  %i FD=%i BYTES=%
4170: 6c 6c 75 20 4f 46 46 53 45 54 3d 25 6c 6c 75 22  llu OFFSET=%llu"
4180: 2c 0a 09 09 09 09 66 69 6c 65 69 6e 66 6f 2d 3e  ,.....fileinfo->
4190: 66 64 2c 0a 09 09 09 09 66 64 2c 0a 09 09 09 09  fd,.....fd,.....
41a0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
41b0: 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65  ong) request->he
41c0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
41d0: 74 68 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65  th,.....(unsigne
41e0: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71  d long long) req
41f0: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  uest->headers.ra
4200: 6e 67 65 2e 6f 66 66 73 65 74 0a 09 09 09 29 3b  nge.offset....);
4210: 0a 0a 09 09 09 73 65 6e 64 66 69 6c 65 5f 6f 66  .....sendfile_of
4220: 66 73 65 74 20 3d 20 72 65 71 75 65 73 74 2d 3e  fset = request->
4230: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66  headers.range.of
4240: 66 73 65 74 3b 0a 09 09 09 73 65 6e 64 66 69 6c  fset;....sendfil
4250: 65 5f 6c 65 6e 20 3d 20 72 65 71 75 65 73 74 2d  e_len = request-
4260: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c  >headers.range.l
4270: 65 6e 67 74 68 3b 0a 09 09 09 73 65 6e 64 66 69  ength;....sendfi
4280: 6c 65 5f 73 65 6e 74 20 3d 20 30 3b 0a 09 09 09  le_sent = 0;....
4290: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 09 09  while (1) {.....
42a0: 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c 65 6e  if (sendfile_len
42b0: 20 3e 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c   > FILED_SENDFIL
42c0: 45 5f 4d 41 58 29 20 7b 0a 09 09 09 09 09 73 65  E_MAX) {......se
42d0: 6e 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 46 49  ndfile_size = FI
42e0: 4c 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41 58  LED_SENDFILE_MAX
42f0: 3b 0a 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09  ;.....} else {..
4300: 09 09 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a  ....sendfile_siz
4310: 65 20 3d 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e  e = sendfile_len
4320: 3b 0a 09 09 09 09 7d 0a 0a 09 09 09 09 73 65 6e  ;.....}......sen
4330: 64 66 69 6c 65 5f 72 65 74 20 3d 20 73 65 6e 64  dfile_ret = send
4340: 66 69 6c 65 28 66 64 2c 20 66 69 6c 65 69 6e 66  file(fd, fileinf
4350: 6f 2d 3e 66 64 2c 20 26 73 65 6e 64 66 69 6c 65  o->fd, &sendfile
4360: 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 66 69 6c  _offset, sendfil
4370: 65 5f 73 69 7a 65 29 3b 0a 09 09 09 09 69 66 20  e_size);.....if 
4380: 28 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3c 3d  (sendfile_ret <=
4390: 20 30 29 20 7b 0a 23 69 66 64 65 66 20 46 49 4c   0) {.#ifdef FIL
43a0: 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50  ED_NONBLOCK_HTTP
43b0: 0a 09 09 09 09 09 69 66 20 28 65 72 72 6e 6f 20  ......if (errno 
43c0: 3d 3d 20 45 41 47 41 49 4e 29 20 7b 0a 09 09 09  == EAGAIN) {....
43d0: 09 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20  ...sendfile_ret 
43e0: 3d 20 30 3b 0a 0a 09 09 09 09 09 09 77 68 69 6c  = 0;........whil
43f0: 65 20 28 31 29 20 7b 0a 09 09 09 09 09 09 09 73  e (1) {........s
4400: 65 6c 65 63 74 28 66 64 20 2b 20 31 2c 20 26 72  elect(fd + 1, &r
4410: 66 64 2c 20 26 77 66 64 2c 20 4e 55 4c 4c 2c 20  fd, &wfd, NULL, 
4420: 4e 55 4c 4c 29 3b 0a 09 09 09 09 09 09 09 69 66  NULL);........if
4430: 20 28 46 44 5f 49 53 53 45 54 28 66 64 2c 20 26   (FD_ISSET(fd, &
4440: 72 66 64 29 29 20 7b 0a 09 09 09 09 09 09 09 09  rfd)) {.........
4450: 72 65 61 64 5f 72 65 74 20 3d 20 72 65 61 64 28  read_ret = read(
4460: 66 64 2c 20 73 69 6e 6b 62 75 66 2c 20 73 69 7a  fd, sinkbuf, siz
4470: 65 6f 66 28 73 69 6e 6b 62 75 66 29 29 3b 0a 0a  eof(sinkbuf));..
4480: 09 09 09 09 09 09 09 09 69 66 20 28 72 65 61 64  ........if (read
4490: 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 09 09  _ret <= 0) {....
44a0: 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09  ......break;....
44b0: 09 09 09 09 09 7d 0a 09 09 09 09 09 09 09 7d 0a  .....}........}.
44c0: 0a 09 09 09 09 09 09 09 69 66 20 28 46 44 5f 49  ........if (FD_I
44d0: 53 53 45 54 28 66 64 2c 20 26 77 66 64 29 29 20  SSET(fd, &wfd)) 
44e0: 7b 0a 09 09 09 09 09 09 09 09 72 65 61 64 5f 72  {.........read_r
44f0: 65 74 20 3d 20 31 3b 0a 0a 09 09 09 09 09 09 09  et = 1;.........
4500: 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 09 7d  .break;........}
4510: 0a 09 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 09  .......}........
4520: 69 66 20 28 72 65 61 64 5f 72 65 74 20 3c 3d 20  if (read_ret <= 
4530: 30 29 20 7b 0a 09 09 09 09 09 09 09 62 72 65 61  0) {........brea
4540: 6b 3b 0a 09 09 09 09 09 09 7d 0a 09 09 09 09 09  k;.......}......
4550: 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 09 62  } else {.......b
4560: 72 65 61 6b 3b 0a 09 09 09 09 09 7d 0a 23 65 6c  reak;......}.#el
4570: 73 65 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 23  se......break;.#
4580: 65 6e 64 69 66 0a 09 09 09 09 7d 0a 0a 09 09 09  endif.....}.....
4590: 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 2d 3d  .sendfile_len -=
45a0: 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09   sendfile_ret;..
45b0: 09 09 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74  ...sendfile_sent
45c0: 20 2b 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 74   += sendfile_ret
45d0: 3b 0a 09 09 09 09 69 66 20 28 73 65 6e 64 66 69  ;.....if (sendfi
45e0: 6c 65 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09  le_len == 0) {..
45f0: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 7d  ....break;.....}
4600: 0a 09 09 09 7d 0a 0a 09 09 09 66 69 6c 65 64 5f  ....}.....filed_
4610: 6c 6f 67 5f 6d 73 67 28 22 53 45 4e 44 5f 43 4f  log_msg("SEND_CO
4620: 4d 50 4c 45 54 45 20 53 54 41 54 55 53 3d 25 73  MPLETE STATUS=%s
4630: 20 46 49 4c 45 5f 46 44 3d 25 69 20 46 44 3d 25   FILE_FD=%i FD=%
4640: 69 20 42 59 54 45 53 3d 25 6c 6c 75 20 42 59 54  i BYTES=%llu BYT
4650: 45 53 5f 53 45 4e 54 3d 25 6c 6c 75 22 2c 0a 09  ES_SENT=%llu",..
4660: 09 09 09 28 73 65 6e 64 66 69 6c 65 5f 73 65 6e  ...(sendfile_sen
4670: 74 20 3d 3d 20 72 65 71 75 65 73 74 2d 3e 68 65  t == request->he
4680: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67  aders.range.leng
4690: 74 68 29 20 3f 20 22 4f 4b 22 20 3a 20 22 50 41  th) ? "OK" : "PA
46a0: 52 54 49 41 4c 22 2c 0a 09 09 09 09 66 69 6c 65  RTIAL",.....file
46b0: 69 6e 66 6f 2d 3e 66 64 2c 0a 09 09 09 09 66 64  info->fd,.....fd
46c0: 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ,.....(unsigned 
46d0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65  long long) reque
46e0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
46f0: 65 2e 6c 65 6e 67 74 68 2c 0a 09 09 09 09 28 75  e.length,.....(u
4700: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
4710: 67 29 20 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74  g) sendfile_sent
4720: 0a 09 09 09 29 3b 0a 09 09 7d 0a 0a 09 09 63 6c  ....);...}....cl
4730: 6f 73 65 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64  ose(fileinfo->fd
4740: 29 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f  );....filed_log_
4750: 6d 73 67 28 22 43 4c 4f 53 45 5f 46 49 4c 45 20  msg("CLOSE_FILE 
4760: 46 44 3d 25 69 22 2c 20 66 64 29 3b 0a 09 7d 0a  FD=%i", fd);..}.
4770: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28  ..filed_log_msg(
4780: 22 43 4c 4f 53 45 5f 43 4f 4e 4e 45 43 54 49 4f  "CLOSE_CONNECTIO
4790: 4e 20 46 44 3d 25 69 22 2c 20 66 64 29 3b 0a 0a  N FD=%i", fd);..
47a0: 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72  .fclose(fp);...r
47b0: 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e  eturn;.}../* Han
47c0: 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 6e  dle incoming con
47d0: 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74  nections */.stat
47e0: 69 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f 77  ic void *filed_w
47f0: 6f 72 6b 65 72 5f 74 68 72 65 61 64 28 76 6f 69  orker_thread(voi
4800: 64 20 2a 61 72 67 5f 76 29 20 7b 0a 09 73 74 72  d *arg_v) {..str
4810: 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72  uct filed_worker
4820: 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72  _thread_args *ar
4830: 67 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64  g;..struct filed
4840: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 72 65  _http_request re
4850: 71 75 65 73 74 3b 0a 09 73 74 72 75 63 74 20 73  quest;..struct s
4860: 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 64 72  ockaddr_in6 addr
4870: 3b 0a 09 63 68 61 72 20 6c 6f 67 62 75 66 5f 69  ;..char logbuf_i
4880: 70 5b 31 32 38 5d 3b 0a 09 73 6f 63 6b 6c 65 6e  p[128];..socklen
4890: 5f 74 20 61 64 64 72 6c 65 6e 3b 0a 09 69 6e 74  _t addrlen;..int
48a0: 20 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d   failure_count =
48b0: 20 30 2c 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f   0, max_failure_
48c0: 63 6f 75 6e 74 20 3d 20 46 49 4c 45 44 5f 4d 41  count = FILED_MA
48d0: 58 5f 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 3b  X_FAILURE_COUNT;
48e0: 0a 09 69 6e 74 20 6d 61 73 74 65 72 5f 66 64 2c  ..int master_fd,
48f0: 20 66 64 3b 0a 0a 09 2f 2a 20 52 65 61 64 20 61   fd;.../* Read a
4900: 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 72 67  rguments */..arg
4910: 20 3d 20 61 72 67 5f 76 3b 0a 0a 09 6d 61 73 74   = arg_v;...mast
4920: 65 72 5f 66 64 20 3d 20 61 72 67 2d 3e 66 64 3b  er_fd = arg->fd;
4930: 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09  ...while (1) {..
4940: 09 2f 2a 20 46 61 69 6c 75 72 65 20 6c 6f 6f 70  ./* Failure loop
4950: 20 70 72 65 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09   prevention */..
4960: 09 69 66 20 28 66 61 69 6c 75 72 65 5f 63 6f 75  .if (failure_cou
4970: 6e 74 20 3e 20 6d 61 78 5f 66 61 69 6c 75 72 65  nt > max_failure
4980: 5f 63 6f 75 6e 74 29 20 7b 0a 09 09 09 62 72 65  _count) {....bre
4990: 61 6b 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 41 63  ak;...}..../* Ac
49a0: 63 65 70 74 20 61 20 6e 65 77 20 63 6c 69 65 6e  cept a new clien
49b0: 74 20 2a 2f 0a 09 09 61 64 64 72 6c 65 6e 20 3d  t */...addrlen =
49c0: 20 73 69 7a 65 6f 66 28 61 64 64 72 29 3b 0a 09   sizeof(addr);..
49d0: 09 66 64 20 3d 20 61 63 63 65 70 74 28 6d 61 73  .fd = accept(mas
49e0: 74 65 72 5f 66 64 2c 20 28 73 74 72 75 63 74 20  ter_fd, (struct 
49f0: 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64  sockaddr *) &add
4a00: 72 2c 20 26 61 64 64 72 6c 65 6e 29 3b 0a 0a 09  r, &addrlen);...
4a10: 09 2f 2a 0a 09 09 20 2a 20 49 66 20 77 65 20 66  ./*... * If we f
4a20: 61 69 6c 2c 20 6d 61 6b 65 20 61 20 6e 6f 74 65  ail, make a note
4a30: 20 6f 66 20 69 74 20 73 6f 20 77 65 20 64 6f 6e   of it so we don
4a40: 27 74 20 67 6f 20 69 6e 74 6f 20 61 20 6c 6f 6f  't go into a loo
4a50: 70 20 6f 66 0a 09 09 20 2a 20 61 63 63 65 70 74  p of... * accept
4a60: 28 29 20 66 61 69 6c 69 6e 67 0a 09 09 20 2a 2f  () failing... */
4a70: 0a 09 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b  ...if (fd < 0) {
4a80: 0a 09 09 09 2f 2a 20 4c 6f 67 20 74 68 65 20 6e  ..../* Log the n
4a90: 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  ew connection */
4aa0: 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  ....filed_log_ms
4ab0: 67 28 22 41 43 43 45 50 54 5f 46 41 49 4c 45 44  g("ACCEPT_FAILED
4ac0: 22 29 3b 0a 0a 09 09 09 66 61 69 6c 75 72 65 5f  ");.....failure_
4ad0: 63 6f 75 6e 74 2b 2b 3b 0a 0a 09 09 09 63 6f 6e  count++;.....con
4ae0: 74 69 6e 75 65 3b 0a 09 09 7d 0a 0a 09 09 2f 2a  tinue;...}..../*
4af0: 20 4c 6f 67 20 74 68 65 20 6e 65 77 20 63 6f 6e   Log the new con
4b00: 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 66 69 6c  nection */...fil
4b10: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 4e 45 57 5f  ed_log_msg("NEW_
4b20: 43 4f 4e 4e 45 43 54 49 4f 4e 20 53 52 43 5f 41  CONNECTION SRC_A
4b30: 44 44 52 3d 25 73 20 53 52 43 5f 50 4f 52 54 3d  DDR=%s SRC_PORT=
4b40: 25 6c 75 20 46 44 3d 25 69 22 2c 0a 09 09 09 66  %lu FD=%i",....f
4b50: 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 28 73 74 72  iled_log_ip((str
4b60: 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20  uct sockaddr *) 
4b70: 26 61 64 64 72 2c 20 6c 6f 67 62 75 66 5f 69 70  &addr, logbuf_ip
4b80: 2c 20 73 69 7a 65 6f 66 28 6c 6f 67 62 75 66 5f  , sizeof(logbuf_
4b90: 69 70 29 29 2c 0a 09 09 09 28 75 6e 73 69 67 6e  ip)),....(unsign
4ba0: 65 64 20 6c 6f 6e 67 29 20 61 64 64 72 2e 73 69  ed long) addr.si
4bb0: 6e 36 5f 70 6f 72 74 2c 0a 09 09 09 66 64 0a 09  n6_port,....fd..
4bc0: 09 29 3b 0a 0a 09 09 2f 2a 20 52 65 73 65 74 20  .);..../* Reset 
4bd0: 66 61 69 6c 75 72 65 20 63 6f 75 6e 74 2a 2f 0a  failure count*/.
4be0: 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20  ..failure_count 
4bf0: 3d 20 30 3b 0a 0a 09 09 2f 2a 20 48 61 6e 64 6c  = 0;..../* Handl
4c00: 65 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 09 66 69  e socket */...fi
4c10: 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69 65 6e  led_handle_clien
4c20: 74 28 66 64 2c 20 26 72 65 71 75 65 73 74 29 3b  t(fd, &request);
4c30: 0a 09 7d 0a 0a 09 2f 2a 20 52 65 70 6f 72 74 20  ..}.../* Report 
4c40: 65 72 72 6f 72 20 2a 2f 0a 09 66 69 6c 65 64 5f  error */..filed_
4c50: 6c 6f 67 5f 6d 73 67 28 22 54 48 52 45 41 44 5f  log_msg("THREAD_
4c60: 44 49 45 44 20 41 42 4e 4f 52 4d 41 4c 22 29 3b  DIED ABNORMAL");
4c70: 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b  ...return(NULL);
4c80: 0a 0a 09 2f 2a 20 4d 61 6b 65 20 63 6f 6d 70 69  .../* Make compi
4c90: 6c 65 72 20 68 61 70 70 79 20 2a 2f 0a 09 6c 6f  ler happy */..lo
4ca0: 67 62 75 66 5f 69 70 5b 30 5d 20 3d 20 27 5c 30  gbuf_ip[0] = '\0
4cb0: 27 3b 0a 09 6c 6f 67 62 75 66 5f 69 70 5b 30 5d  ';..logbuf_ip[0]
4cc0: 20 3d 20 6c 6f 67 62 75 66 5f 69 70 5b 30 5d 3b   = logbuf_ip[0];
4cd0: 0a 0a 7d 0a 0a 2f 2a 20 43 72 65 61 74 65 20 77  ..}../* Create w
4ce0: 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a 2f  orker threads */
4cf0: 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65  .static int file
4d00: 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73  d_worker_threads
4d10: 5f 69 6e 69 74 28 69 6e 74 20 66 64 2c 20 69 6e  _init(int fd, in
4d20: 74 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 29 20  t thread_count) 
4d30: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f  {..struct filed_
4d40: 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72  worker_thread_ar
4d50: 67 73 20 2a 61 72 67 3b 0a 09 70 74 68 72 65 61  gs *arg;..pthrea
4d60: 64 5f 74 20 74 68 72 65 61 64 69 64 3b 0a 09 69  d_t threadid;..i
4d70: 6e 74 20 70 74 68 72 65 61 64 5f 72 65 74 3b 0a  nt pthread_ret;.
4d80: 09 69 6e 74 20 69 3b 0a 0a 09 66 6f 72 20 28 69  .int i;...for (i
4d90: 20 3d 20 30 3b 20 69 20 3c 20 74 68 72 65 61 64   = 0; i < thread
4da0: 5f 63 6f 75 6e 74 3b 20 69 2b 2b 29 20 7b 0a 09  _count; i++) {..
4db0: 09 61 72 67 20 3d 20 6d 61 6c 6c 6f 63 28 73 69  .arg = malloc(si
4dc0: 7a 65 6f 66 28 2a 61 72 67 29 29 3b 0a 0a 09 09  zeof(*arg));....
4dd0: 61 72 67 2d 3e 66 64 20 3d 20 66 64 3b 0a 0a 09  arg->fd = fd;...
4de0: 09 70 74 68 72 65 61 64 5f 72 65 74 20 3d 20 70  .pthread_ret = p
4df0: 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74  thread_create(&t
4e00: 68 72 65 61 64 69 64 2c 20 4e 55 4c 4c 2c 20 66  hreadid, NULL, f
4e10: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65  iled_worker_thre
4e20: 61 64 2c 20 61 72 67 29 3b 0a 09 09 69 66 20 28  ad, arg);...if (
4e30: 70 74 68 72 65 61 64 5f 72 65 74 20 21 3d 20 30  pthread_ret != 0
4e40: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 31  ) {....return(-1
4e50: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75  );...}..}...retu
4e60: 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 73  rn(0);.}../* Dis
4e70: 70 6c 61 79 20 68 65 6c 70 20 2a 2f 0a 73 74 61  play help */.sta
4e80: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 70  tic void filed_p
4e90: 72 69 6e 74 5f 68 65 6c 70 28 46 49 4c 45 20 2a  rint_help(FILE *
4ea0: 6f 75 74 70 75 74 2c 20 69 6e 74 20 6c 6f 6e 67  output, int long
4eb0: 5f 68 65 6c 70 2c 20 63 6f 6e 73 74 20 63 68 61  _help, const cha
4ec0: 72 20 2a 65 78 74 72 61 29 20 7b 0a 09 69 66 20  r *extra) {..if 
4ed0: 28 65 78 74 72 61 29 20 7b 0a 09 09 66 70 72 69  (extra) {...fpri
4ee0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 25 73 5c  ntf(output, "%s\
4ef0: 6e 22 2c 20 65 78 74 72 61 29 3b 0a 09 7d 0a 0a  n", extra);..}..
4f00: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
4f10: 20 22 55 73 61 67 65 3a 20 66 69 6c 65 64 20 5b   "Usage: filed [
4f20: 3c 6f 70 74 69 6f 6e 73 3e 5d 5c 6e 22 29 3b 0a  <options>]\n");.
4f30: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
4f40: 20 22 20 20 4f 70 74 69 6f 6e 73 3a 5c 6e 22 29   "  Options:\n")
4f50: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ;..fprintf(outpu
4f60: 74 2c 20 22 20 20 20 20 20 20 2d 68 2c 20 2d 2d  t, "      -h, --
4f70: 68 65 6c 70 5c 6e 22 29 3b 0a 09 66 70 72 69 6e  help\n");..fprin
4f80: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
4f90: 20 20 2d 64 2c 20 2d 2d 64 61 65 6d 6f 6e 5c 6e    -d, --daemon\n
4fa0: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  ");..fprintf(out
4fb0: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 2c 20  put, "      -v, 
4fc0: 2d 2d 76 65 72 73 69 6f 6e 5c 6e 22 29 3b 0a 09  --version\n");..
4fd0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
4fe0: 22 20 20 20 20 20 20 2d 62 20 3c 61 64 64 72 65  "      -b <addre
4ff0: 73 73 3e 2c 20 2d 2d 62 69 6e 64 20 3c 61 64 64  ss>, --bind <add
5000: 72 65 73 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69  ress>\n");..fpri
5010: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
5020: 20 20 20 2d 70 20 3c 70 6f 72 74 3e 2c 20 2d 2d     -p <port>, --
5030: 70 6f 72 74 20 3c 70 6f 72 74 3e 5c 6e 22 29 3b  port <port>\n");
5040: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
5050: 2c 20 22 20 20 20 20 20 20 2d 74 20 3c 63 6f 75  , "      -t <cou
5060: 6e 74 3e 2c 20 2d 2d 74 68 72 65 61 64 73 20 3c  nt>, --threads <
5070: 63 6f 75 6e 74 3e 5c 6e 22 29 3b 0a 09 66 70 72  count>\n");..fpr
5080: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
5090: 20 20 20 20 2d 63 20 3c 65 6e 74 72 69 65 73 3e      -c <entries>
50a0: 2c 20 2d 2d 63 61 63 68 65 20 3c 65 6e 74 72 69  , --cache <entri
50b0: 65 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74  es>\n");..fprint
50c0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
50d0: 20 2d 6c 20 3c 66 69 6c 65 3e 2c 20 2d 2d 6c 6f   -l <file>, --lo
50e0: 67 20 3c 66 69 6c 65 3e 5c 6e 22 29 3b 0a 09 66  g <file>\n");..f
50f0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
5100: 20 20 20 20 20 20 2d 75 20 3c 75 73 65 72 3e 2c        -u <user>,
5110: 20 2d 2d 75 73 65 72 20 3c 75 73 65 72 3e 5c 6e   --user <user>\n
5120: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  ");..fprintf(out
5130: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 72 20 3c  put, "      -r <
5140: 64 69 72 65 63 74 6f 72 79 3e 2c 20 2d 2d 72 6f  directory>, --ro
5150: 6f 74 20 3c 64 69 72 65 63 74 6f 72 79 3e 5c 6e  ot <directory>\n
5160: 22 29 3b 0a 0a 09 69 66 20 28 6c 6f 6e 67 5f 68  ");...if (long_h
5170: 65 6c 70 29 20 7b 0a 09 09 66 70 72 69 6e 74 66  elp) {...fprintf
5180: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a  (output, "\n");.
5190: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
51a0: 2c 20 22 20 20 55 73 61 67 65 3a 5c 6e 22 29 3b  , "  Usage:\n");
51b0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
51c0: 74 2c 20 22 20 20 20 20 20 20 2d 68 20 28 6f 72  t, "      -h (or
51d0: 20 2d 2d 68 65 6c 70 29 20 70 72 69 6e 74 73 20   --help) prints 
51e0: 74 68 69 73 20 75 73 61 67 65 20 69 6e 66 6f 72  this usage infor
51f0: 6d 61 74 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 66  mation.\n");...f
5200: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
5210: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
5220: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
5230: 64 20 28 6f 72 20 2d 2d 64 61 65 6d 6f 6e 29 20  d (or --daemon) 
5240: 69 6e 73 74 72 75 63 74 73 20 66 69 6c 65 64 20  instructs filed 
5250: 74 6f 20 62 65 63 6f 6d 65 20 61 20 64 61 65 6d  to become a daem
5260: 6f 6e 20 61 66 74 65 72 20 69 6e 69 74 69 61 6c  on after initial
5270: 69 7a 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72  izing\n");...fpr
5280: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
5290: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52a0: 20 20 20 20 20 74 68 65 20 6c 69 73 74 65 6e 69       the listeni
52b0: 6e 67 20 54 43 50 20 73 6f 63 6b 65 74 20 61 6e  ng TCP socket an
52c0: 64 20 6c 6f 67 20 66 69 6c 65 73 2e 5c 6e 22 29  d log files.\n")
52d0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
52e0: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72  ut, "\n");...fpr
52f0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
5300: 20 20 20 20 2d 76 20 28 6f 72 20 2d 2d 76 65 72      -v (or --ver
5310: 73 69 6f 6e 29 20 69 6e 73 74 72 75 63 74 73 20  sion) instructs 
5320: 66 69 6c 65 64 20 70 72 69 6e 74 20 6f 75 74 20  filed print out 
5330: 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  the version numb
5340: 65 72 20 61 6e 64 20 65 78 69 74 2e 5c 6e 22 29  er and exit.\n")
5350: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
5360: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72  ut, "\n");...fpr
5370: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
5380: 20 20 20 20 2d 62 20 28 6f 72 20 2d 2d 62 69 6e      -b (or --bin
5390: 64 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65  d) specifies the
53a0: 20 61 64 64 72 65 73 73 20 74 6f 20 6c 69 73 74   address to list
53b0: 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 20  en for incoming 
53c0: 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69  HTTP\n");...fpri
53d0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
53e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53f0: 20 20 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 20    requests on.  
5400: 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  The default valu
5410: 65 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c  e is \"%s\".\n",
5420: 20 42 49 4e 44 5f 41 44 44 52 29 3b 0a 09 09 66   BIND_ADDR);...f
5430: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
5440: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
5450: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
5460: 70 20 28 6f 72 20 2d 2d 70 6f 72 74 29 20 73 70  p (or --port) sp
5470: 65 63 69 66 69 65 73 20 74 68 65 20 54 43 50 20  ecifies the TCP 
5480: 70 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f 20 6c  port number to l
5490: 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69  isten for incomi
54a0: 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66  ng HTTP\n");...f
54b0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
54c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d0: 20 20 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e       requests on
54e0: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69  .  The default i
54f0: 73 20 25 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67  s %u.\n", (unsig
5500: 6e 65 64 20 69 6e 74 29 20 50 4f 52 54 29 3b 0a  ned int) PORT);.
5510: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
5520: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  , "\n");...fprin
5530: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
5540: 20 20 2d 74 20 28 6f 72 20 2d 2d 74 68 72 65 61    -t (or --threa
5550: 64 73 29 20 73 70 65 63 69 66 69 65 73 20 74 68  ds) specifies th
5560: 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 6f 72 6b  e number of work
5570: 65 72 20 74 68 72 65 61 64 73 20 74 6f 20 63 72  er threads to cr
5580: 65 61 74 65 2e 20 45 61 63 68 5c 6e 22 29 3b 0a  eate. Each\n");.
5590: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
55a0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
55b0: 20 20 20 20 20 20 20 20 20 20 20 77 6f 72 6b 65             worke
55c0: 72 20 74 68 72 65 61 64 20 63 61 6e 20 73 65 72  r thread can ser
55d0: 76 69 63 65 20 6f 6e 65 20 63 6f 6e 63 75 72 72  vice one concurr
55e0: 65 6e 74 20 48 54 54 50 20 73 65 73 73 69 6f 6e  ent HTTP session
55f0: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  .\n");...fprintf
5600: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
5610: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5620: 20 20 54 68 75 73 20 74 68 65 20 6e 75 6d 62 65    Thus the numbe
5630: 72 20 6f 66 20 74 68 72 65 61 64 73 20 63 72 65  r of threads cre
5640: 61 74 65 64 20 77 69 6c 6c 20 64 65 74 65 72 6d  ated will determ
5650: 69 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a 09 09 66  ine how\n");...f
5660: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
5670: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5680: 20 20 20 20 20 20 20 20 6d 61 6e 79 20 73 69 6d          many sim
5690: 75 6c 74 61 6e 65 6f 75 73 20 74 72 61 6e 73 66  ultaneous transf
56a0: 65 72 73 20 77 69 6c 6c 20 62 65 20 70 6f 73 73  ers will be poss
56b0: 69 62 6c 65 2e 20 54 68 65 5c 6e 22 29 3b 0a 09  ible. The\n");..
56c0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
56d0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
56e0: 20 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c            defaul
56f0: 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 75  t is %lu.\n", (u
5700: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 54 48  nsigned long) TH
5710: 52 45 41 44 5f 43 4f 55 4e 54 29 3b 0a 09 09 66  READ_COUNT);...f
5720: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
5730: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
5740: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
5750: 63 20 28 6f 72 20 2d 2d 63 61 63 68 65 29 20 73  c (or --cache) s
5760: 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d  pecifies the num
5770: 62 65 72 20 6f 66 20 66 69 6c 65 20 69 6e 66 6f  ber of file info
5780: 72 6d 61 74 69 6f 6e 20 63 61 63 68 65 20 65 6e  rmation cache en
5790: 74 72 69 65 73 5c 6e 22 29 3b 0a 09 09 66 70 72  tries\n");...fpr
57a0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
57b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57c0: 20 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2e      to allocate.
57d0: 20 20 45 61 63 68 20 63 61 63 68 65 20 65 6e 74    Each cache ent
57e0: 72 79 20 68 6f 6c 64 73 20 66 69 6c 65 20 69 6e  ry holds file in
57f0: 66 6f 72 6d 61 74 69 6f 6e 20 61 73 5c 6e 22 29  formation as\n")
5800: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
5810: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
5820: 20 20 20 20 20 20 20 20 20 20 20 77 65 6c 6c 20             well 
5830: 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  as an open file 
5840: 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74 68  descriptor to th
5850: 65 20 66 69 6c 65 2c 20 73 6f 20 72 65 73 6f 75  e file, so resou
5860: 72 63 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  rce\n");...fprin
5870: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
5880: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5890: 20 20 6c 69 6d 69 74 73 20 28 69 2e 65 2e 2c 20    limits (i.e., 
58a0: 75 6c 69 6d 69 74 29 20 73 68 6f 75 6c 64 20 62  ulimit) should b
58b0: 65 20 63 6f 6e 73 69 64 65 72 65 64 2e 20 20 54  e considered.  T
58c0: 68 69 73 20 73 68 6f 75 6c 64 5c 6e 22 29 3b 0a  his should\n");.
58d0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
58e0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
58f0: 20 20 20 20 20 20 20 20 20 62 65 20 61 20 70 72           be a pr
5900: 69 6d 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 69  ime number for i
5910: 64 65 61 6c 20 75 73 65 20 77 69 74 68 20 74 68  deal use with th
5920: 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 6f 64 2e  e lookup method.
5930: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
5940: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
5950: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
5960: 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 6c  he default is %l
5970: 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64  u.\n", (unsigned
5980: 20 6c 6f 6e 67 29 20 43 41 43 48 45 5f 53 49 5a   long) CACHE_SIZ
5990: 45 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  E);...fprintf(ou
59a0: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66  tput, "\n");...f
59b0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
59c0: 20 20 20 20 20 20 2d 6c 20 28 6f 72 20 2d 2d 6c        -l (or --l
59d0: 6f 67 29 20 73 70 65 63 69 66 69 65 73 20 61 20  og) specifies a 
59e0: 66 69 6c 65 6e 61 6d 65 20 74 6f 20 6f 70 65 6e  filename to open
59f0: 20 66 6f 72 20 77 72 69 74 69 6e 67 20 6c 6f 67   for writing log
5a00: 20 65 6e 74 72 69 65 73 2e 20 20 4c 6f 67 5c 6e   entries.  Log\n
5a10: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
5a20: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20  tput, "         
5a30: 20 20 20 20 20 20 20 20 20 20 20 65 6e 74 72 69             entri
5a40: 65 73 20 61 72 65 20 6d 61 64 65 20 66 6f 72 20  es are made for 
5a50: 76 61 72 69 6f 75 73 20 73 74 61 67 65 73 20 69  various stages i
5a60: 6e 20 74 72 61 6e 73 66 65 72 69 6e 67 20 66 69  n transfering fi
5a70: 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69  les.\n");...fpri
5a80: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
5a90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aa0: 20 54 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 73   The log file is
5ab0: 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 73   opened before s
5ac0: 77 69 74 63 68 69 6e 67 20 75 73 65 72 73 20 28  witching users (
5ad0: 73 65 65 20 5c 22 2d 75 5c 22 29 5c 6e 22 29 3b  see \"-u\")\n");
5ae0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
5af0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
5b00: 20 20 20 20 20 20 20 20 61 6e 64 20 72 6f 6f 74          and root
5b10: 20 64 69 72 65 63 74 6f 72 69 65 73 20 28 73 65   directories (se
5b20: 65 20 5c 22 2d 72 5c 22 29 2e 20 20 54 68 65 20  e \"-r\").  The 
5b30: 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e 65 76 65  log file is neve
5b40: 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  r\n");...fprintf
5b50: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
5b60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6c                cl
5b70: 6f 73 65 64 20 73 6f 20 6c 6f 67 20 72 6f 74 61  osed so log rota
5b80: 74 69 6f 6e 20 77 69 74 68 6f 75 74 20 73 74 6f  tion without sto
5b90: 70 70 69 6e 67 20 74 68 65 20 64 61 65 6d 6f 6e  pping the daemon
5ba0: 20 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09   is will\n");...
5bb0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
5bc0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
5bd0: 20 20 20 20 20 6e 6f 74 20 77 6f 72 6b 2e 20 20       not work.  
5be0: 54 68 65 20 76 61 6c 75 65 20 6f 66 20 5c 22 2d  The value of \"-
5bf0: 5c 22 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  \" indicates tha
5c00: 74 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 75  t standard outpu
5c10: 74 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  t\n");...fprintf
5c20: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
5c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 68                sh
5c40: 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
5c50: 20 6c 6f 67 67 69 6e 67 2e 20 20 54 68 65 20 64   logging.  The d
5c60: 65 66 61 75 6c 74 20 69 73 20 5c 22 25 73 5c 22  efault is \"%s\"
5c70: 2e 5c 6e 22 2c 20 4c 4f 47 5f 46 49 4c 45 29 3b  .\n", LOG_FILE);
5c80: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
5c90: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69  t, "\n");...fpri
5ca0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
5cb0: 20 20 20 2d 75 20 28 6f 72 20 2d 2d 75 73 65 72     -u (or --user
5cc0: 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  ) specifies the 
5cd0: 75 73 65 72 20 74 6f 20 73 77 69 74 63 68 20 75  user to switch u
5ce0: 73 65 72 20 49 44 73 20 74 6f 20 62 65 66 6f 72  ser IDs to befor
5cf0: 65 20 73 65 72 76 69 63 69 6e 67 5c 6e 22 29 3b  e servicing\n");
5d00: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
5d10: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
5d20: 20 20 20 20 20 20 20 20 20 72 65 71 75 65 73 74           request
5d30: 73 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  s.  The default 
5d40: 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 75 73  is not change us
5d50: 65 72 20 49 44 73 2e 5c 6e 22 29 3b 0a 09 09 66  er IDs.\n");...f
5d60: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
5d70: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
5d80: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d  output, "      -
5d90: 72 20 28 6f 72 20 2d 2d 72 6f 6f 74 29 20 73 70  r (or --root) sp
5da0: 65 63 69 66 69 65 73 20 74 68 65 20 64 69 72 65  ecifies the dire
5db0: 63 74 6f 72 79 20 74 6f 20 61 63 74 20 61 73 20  ctory to act as 
5dc0: 74 68 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f  the root directo
5dd0: 72 79 20 66 6f 72 5c 6e 22 29 3b 0a 09 09 66 70  ry for\n");...fp
5de0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
5df0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e00: 20 20 20 20 74 68 65 20 66 69 6c 65 20 73 65 72      the file ser
5e10: 76 65 72 2e 20 20 49 66 20 74 68 69 73 20 6f 70  ver.  If this op
5e20: 74 69 6f 6e 20 69 73 20 73 70 65 63 69 66 69 65  tion is specifie
5e30: 64 2c 20 63 68 72 6f 6f 74 28 32 29 5c 6e 22 29  d, chroot(2)\n")
5e40: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
5e50: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
5e60: 20 20 20 20 20 20 20 20 20 20 69 73 20 63 61 6c            is cal
5e70: 6c 65 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c  led.  The defaul
5e80: 74 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20  t is not change 
5e90: 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73  root directories
5ea0: 2c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  ,\n");...fprintf
5eb0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
5ec0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
5ed0: 68 61 74 20 69 73 2c 20 74 68 65 20 5c 22 2f 5c  hat is, the \"/\
5ee0: 22 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 73  " directory is s
5ef0: 68 61 72 65 64 20 6f 75 74 2e 20 20 54 68 69 73  hared out.  This
5f00: 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 72   will\n");...fpr
5f10: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
5f20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5f30: 20 20 20 6c 69 6b 65 6c 79 20 62 65 20 61 20 73     likely be a s
5f40: 65 63 75 72 69 74 79 20 69 73 73 75 65 2c 20 73  ecurity issue, s
5f50: 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 73 68  o this option sh
5f60: 6f 75 6c 64 20 61 6c 77 61 79 73 5c 6e 22 29 3b  ould always\n");
5f70: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
5f80: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
5f90: 20 20 20 20 20 20 20 20 20 62 65 20 75 73 65 64           be used
5fa0: 2e 5c 6e 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75  .\n");..}...retu
5fb0: 72 6e 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 20  rn;.}../* Add a 
5fc0: 67 65 74 6f 70 74 20 6f 70 74 69 6f 6e 20 2a 2f  getopt option */
5fd0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
5fe0: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73  ed_getopt_long_s
5ff0: 65 74 6f 70 74 28 73 74 72 75 63 74 20 6f 70 74  etopt(struct opt
6000: 69 6f 6e 20 2a 6f 70 74 2c 20 63 6f 6e 73 74 20  ion *opt, const 
6010: 63 68 61 72 20 2a 6e 61 6d 65 2c 20 69 6e 74 20  char *name, int 
6020: 68 61 73 5f 61 72 67 2c 20 69 6e 74 20 76 61 6c  has_arg, int val
6030: 29 20 7b 0a 09 6f 70 74 2d 3e 6e 61 6d 65 20 20  ) {..opt->name  
6040: 20 20 20 3d 20 6e 61 6d 65 3b 0a 09 6f 70 74 2d     = name;..opt-
6050: 3e 68 61 73 5f 61 72 67 20 20 3d 20 68 61 73 5f  >has_arg  = has_
6060: 61 72 67 3b 0a 09 6f 70 74 2d 3e 66 6c 61 67 20  arg;..opt->flag 
6070: 20 20 20 20 3d 20 4e 55 4c 4c 3b 0a 09 6f 70 74      = NULL;..opt
6080: 2d 3e 76 61 6c 20 20 20 20 20 20 3d 20 76 61 6c  ->val      = val
6090: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;...return;.}../
60a0: 2a 20 52 65 73 6f 6c 76 65 20 61 20 75 73 65 72  * Resolve a user
60b0: 6e 61 6d 65 20 74 6f 20 61 20 55 49 44 20 2a 2f  name to a UID */
60c0: 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65  .static int file
60d0: 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 63 6f  d_user_lookup(co
60e0: 6e 73 74 20 63 68 61 72 20 2a 75 73 65 72 2c 20  nst char *user, 
60f0: 75 69 64 5f 74 20 2a 75 73 65 72 5f 69 64 29 20  uid_t *user_id) 
6100: 7b 0a 09 63 68 61 72 20 2a 6e 65 78 74 3b 0a 09  {..char *next;..
6110: 75 69 64 5f 74 20 75 73 65 72 5f 69 64 5f 63 68  uid_t user_id_ch
6120: 65 63 6b 3b 0a 23 69 66 6e 64 65 66 20 46 49 4c  eck;.#ifndef FIL
6130: 45 44 5f 4e 4f 5f 47 45 54 50 57 4e 41 4d 0a 09  ED_NO_GETPWNAM..
6140: 73 74 72 75 63 74 20 70 61 73 73 77 64 20 2a 65  struct passwd *e
6150: 6e 74 3b 0a 0a 09 65 6e 74 20 3d 20 67 65 74 70  nt;...ent = getp
6160: 77 6e 61 6d 28 75 73 65 72 29 3b 0a 09 69 66 20  wnam(user);..if 
6170: 28 65 6e 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a  (ent != NULL) {.
6180: 09 09 2a 75 73 65 72 5f 69 64 20 3d 20 65 6e 74  ..*user_id = ent
6190: 2d 3e 70 77 5f 75 69 64 3b 0a 0a 09 09 72 65 74  ->pw_uid;....ret
61a0: 75 72 6e 28 30 29 3b 0a 09 7d 0a 23 65 6e 64 69  urn(0);..}.#endi
61b0: 66 0a 0a 09 75 73 65 72 5f 69 64 5f 63 68 65 63  f...user_id_chec
61c0: 6b 20 3d 20 73 74 72 74 6f 75 6c 6c 28 75 73 65  k = strtoull(use
61d0: 72 2c 20 26 6e 65 78 74 2c 20 31 30 29 3b 0a 09  r, &next, 10);..
61e0: 69 66 20 28 6e 65 78 74 20 3d 3d 20 4e 55 4c 4c  if (next == NULL
61f0: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b  ) {...return(1);
6200: 0a 09 7d 0a 0a 09 69 66 20 28 6e 65 78 74 5b 30  ..}...if (next[0
6210: 5d 20 21 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72  ] != '\0') {...r
6220: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2a  eturn(1);..}...*
6230: 75 73 65 72 5f 69 64 20 3d 20 75 73 65 72 5f 69  user_id = user_i
6240: 64 5f 63 68 65 63 6b 3b 0a 0a 09 72 65 74 75 72  d_check;...retur
6250: 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 61 65 6d  n(0);.}../* Daem
6260: 6f 6e 69 7a 65 20 2a 2f 0a 73 74 61 74 69 63 20  onize */.static 
6270: 69 6e 74 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e  int filed_daemon
6280: 69 7a 65 28 76 6f 69 64 29 20 7b 0a 09 70 69 64  ize(void) {..pid
6290: 5f 74 20 73 65 74 73 69 64 5f 72 65 74 2c 20 66  _t setsid_ret, f
62a0: 6f 72 6b 5f 72 65 74 3b 0a 09 69 6e 74 20 63 68  ork_ret;..int ch
62b0: 64 69 72 5f 72 65 74 2c 20 64 75 70 32 5f 72 65  dir_ret, dup2_re
62c0: 74 3b 0a 09 69 6e 74 20 66 64 5f 69 6e 2c 20 66  t;..int fd_in, f
62d0: 64 5f 6f 75 74 3b 0a 0a 09 63 68 64 69 72 5f 72  d_out;...chdir_r
62e0: 65 74 20 3d 20 63 68 64 69 72 28 22 2f 22 29 3b  et = chdir("/");
62f0: 0a 09 69 66 20 28 63 68 64 69 72 5f 72 65 74 20  ..if (chdir_ret 
6300: 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e  != 0) {...return
6310: 28 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 6b 5f 72  (1);..}...fork_r
6320: 65 74 20 3d 20 66 6f 72 6b 28 29 3b 0a 09 69 66  et = fork();..if
6330: 20 28 66 6f 72 6b 5f 72 65 74 20 3c 20 30 29 20   (fork_ret < 0) 
6340: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  {...return(1);..
6350: 7d 0a 0a 09 69 66 20 28 66 6f 72 6b 5f 72 65 74  }...if (fork_ret
6360: 20 3e 20 30 29 20 7b 0a 09 09 2f 2a 20 50 61 72   > 0) {.../* Par
6370: 65 6e 74 20 2a 2f 0a 09 09 77 61 69 74 70 69 64  ent */...waitpid
6380: 28 66 6f 72 6b 5f 72 65 74 2c 20 4e 55 4c 4c 2c  (fork_ret, NULL,
6390: 20 30 29 3b 0a 0a 09 09 65 78 69 74 28 45 58 49   0);....exit(EXI
63a0: 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a 0a  T_SUCCESS);..}..
63b0: 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 69 66  ./* Child */..if
63c0: 20 28 66 6f 72 6b 28 29 20 21 3d 20 30 29 20 7b   (fork() != 0) {
63d0: 0a 09 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09  .../* Child */..
63e0: 09 65 78 69 74 28 45 58 49 54 5f 53 55 43 43 45  .exit(EXIT_SUCCE
63f0: 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 47 72 61  SS);..}.../* Gra
6400: 6e 64 20 63 68 69 6c 64 20 2a 2f 0a 09 73 65 74  nd child */..set
6410: 73 69 64 5f 72 65 74 20 3d 20 73 65 74 73 69 64  sid_ret = setsid
6420: 28 29 3b 0a 09 69 66 20 28 73 65 74 73 69 64 5f  ();..if (setsid_
6430: 72 65 74 20 3d 3d 20 28 28 70 69 64 5f 74 29 20  ret == ((pid_t) 
6440: 2d 31 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  -1)) {...return(
6450: 31 29 3b 0a 09 7d 0a 0a 09 66 64 5f 69 6e 20 3d  1);..}...fd_in =
6460: 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c   open("/dev/null
6470: 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 09 66  ", O_RDONLY);..f
6480: 64 5f 6f 75 74 20 3d 20 6f 70 65 6e 28 22 2f 64  d_out = open("/d
6490: 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 52 4f 4e  ev/null", O_WRON
64a0: 4c 59 29 3b 0a 09 69 66 20 28 66 64 5f 69 6e 20  LY);..if (fd_in 
64b0: 3c 20 30 20 7c 7c 20 66 64 5f 6f 75 74 20 3c 20  < 0 || fd_out < 
64c0: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29  0) {...return(1)
64d0: 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20  ;..}...dup2_ret 
64e0: 3d 20 64 75 70 32 28 66 64 5f 69 6e 2c 20 53 54  = dup2(fd_in, ST
64f0: 44 49 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66  DIN_FILENO);..if
6500: 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54   (dup2_ret != ST
6510: 44 49 4e 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09  DIN_FILENO) {...
6520: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09  return(1);..}...
6530: 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28  dup2_ret = dup2(
6540: 66 64 5f 6f 75 74 2c 20 53 54 44 4f 55 54 5f 46  fd_out, STDOUT_F
6550: 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70  ILENO);..if (dup
6560: 32 5f 72 65 74 20 21 3d 20 53 54 44 4f 55 54 5f  2_ret != STDOUT_
6570: 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75  FILENO) {...retu
6580: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32  rn(1);..}...dup2
6590: 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f  _ret = dup2(fd_o
65a0: 75 74 2c 20 53 54 44 45 52 52 5f 46 49 4c 45 4e  ut, STDERR_FILEN
65b0: 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65  O);..if (dup2_re
65c0: 74 20 21 3d 20 53 54 44 45 52 52 5f 46 49 4c 45  t != STDERR_FILE
65d0: 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31  NO) {...return(1
65e0: 29 3b 0a 09 7d 0a 0a 09 63 6c 6f 73 65 28 66 64  );..}...close(fd
65f0: 5f 69 6e 29 3b 0a 09 63 6c 6f 73 65 28 66 64 5f  _in);..close(fd_
6600: 6f 75 74 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30  out);...return(0
6610: 29 3b 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 70 72 6f  );.}../* Run pro
6620: 63 65 73 73 20 2a 2f 0a 69 6e 74 20 6d 61 69 6e  cess */.int main
6630: 28 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20  (int argc, char 
6640: 2a 2a 61 72 67 76 29 20 7b 0a 09 73 74 72 75 63  **argv) {..struc
6650: 74 20 6f 70 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  t option options
6660: 5b 31 31 5d 3b 0a 09 63 6f 6e 73 74 20 63 68 61  [11];..const cha
6670: 72 20 2a 62 69 6e 64 5f 61 64 64 72 20 3d 20 42  r *bind_addr = B
6680: 49 4e 44 5f 41 44 44 52 2c 20 2a 6e 65 77 72 6f  IND_ADDR, *newro
6690: 6f 74 20 3d 20 4e 55 4c 4c 2c 20 2a 6c 6f 67 5f  ot = NULL, *log_
66a0: 66 69 6c 65 20 3d 20 4c 4f 47 5f 46 49 4c 45 3b  file = LOG_FILE;
66b0: 0a 09 46 49 4c 45 20 2a 6c 6f 67 5f 66 70 3b 0a  ..FILE *log_fp;.
66c0: 09 75 69 64 5f 74 20 75 73 65 72 20 3d 20 30 3b  .uid_t user = 0;
66d0: 0a 09 69 6e 74 20 70 6f 72 74 20 3d 20 50 4f 52  ..int port = POR
66e0: 54 2c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 20  T, thread_count 
66f0: 3d 20 54 48 52 45 41 44 5f 43 4f 55 4e 54 3b 0a  = THREAD_COUNT;.
6700: 09 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 20  .int cache_size 
6710: 3d 20 43 41 43 48 45 5f 53 49 5a 45 3b 0a 09 69  = CACHE_SIZE;..i
6720: 6e 74 20 69 6e 69 74 5f 72 65 74 2c 20 63 68 72  nt init_ret, chr
6730: 6f 6f 74 5f 72 65 74 2c 20 73 65 74 75 69 64 5f  oot_ret, setuid_
6740: 72 65 74 2c 20 6c 6f 6f 6b 75 70 5f 72 65 74 2c  ret, lookup_ret,
6750: 20 63 68 64 69 72 5f 72 65 74 3b 0a 09 69 6e 74   chdir_ret;..int
6760: 20 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 20   setuid_enabled 
6770: 3d 20 30 2c 20 64 61 65 6d 6f 6e 5f 65 6e 61 62  = 0, daemon_enab
6780: 6c 65 64 20 3d 20 30 3b 0a 09 69 6e 74 20 63 68  led = 0;..int ch
6790: 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 09 2f 2a 20  ;..int fd;.../* 
67a0: 50 72 6f 63 65 73 73 20 61 72 67 75 6d 65 6e 74  Process argument
67b0: 73 20 2a 2f 0a 09 66 69 6c 65 64 5f 67 65 74 6f  s */..filed_geto
67c0: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26  pt_long_setopt(&
67d0: 6f 70 74 69 6f 6e 73 5b 30 5d 2c 20 22 70 6f 72  options[0], "por
67e0: 74 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67  t", required_arg
67f0: 75 6d 65 6e 74 2c 20 27 70 27 29 3b 0a 09 66 69  ument, 'p');..fi
6800: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f  led_getopt_long_
6810: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b  setopt(&options[
6820: 31 5d 2c 20 22 74 68 72 65 61 64 73 22 2c 20 72  1], "threads", r
6830: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74  equired_argument
6840: 2c 20 27 74 27 29 3b 0a 09 66 69 6c 65 64 5f 67  , 't');..filed_g
6850: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70  etopt_long_setop
6860: 74 28 26 6f 70 74 69 6f 6e 73 5b 32 5d 2c 20 22  t(&options[2], "
6870: 63 61 63 68 65 22 2c 20 72 65 71 75 69 72 65 64  cache", required
6880: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 63 27 29 3b  _argument, 'c');
6890: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c  ..filed_getopt_l
68a0: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69  ong_setopt(&opti
68b0: 6f 6e 73 5b 33 5d 2c 20 22 62 69 6e 64 22 2c 20  ons[3], "bind", 
68c0: 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e  required_argumen
68d0: 74 2c 20 27 62 27 29 3b 0a 09 66 69 6c 65 64 5f  t, 'b');..filed_
68e0: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f  getopt_long_seto
68f0: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 34 5d 2c 20  pt(&options[4], 
6900: 22 75 73 65 72 22 2c 20 72 65 71 75 69 72 65 64  "user", required
6910: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 75 27 29 3b  _argument, 'u');
6920: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c  ..filed_getopt_l
6930: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69  ong_setopt(&opti
6940: 6f 6e 73 5b 35 5d 2c 20 22 72 6f 6f 74 22 2c 20  ons[5], "root", 
6950: 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e  required_argumen
6960: 74 2c 20 27 72 27 29 3b 0a 09 66 69 6c 65 64 5f  t, 'r');..filed_
6970: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f  getopt_long_seto
6980: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 36 5d 2c 20  pt(&options[6], 
6990: 22 68 65 6c 70 22 2c 20 6e 6f 5f 61 72 67 75 6d  "help", no_argum
69a0: 65 6e 74 2c 20 27 68 27 29 3b 0a 09 66 69 6c 65  ent, 'h');..file
69b0: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65  d_getopt_long_se
69c0: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 37 5d  topt(&options[7]
69d0: 2c 20 22 64 61 65 6d 6f 6e 22 2c 20 6e 6f 5f 61  , "daemon", no_a
69e0: 72 67 75 6d 65 6e 74 2c 20 27 64 27 29 3b 0a 09  rgument, 'd');..
69f0: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e  filed_getopt_lon
6a00: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e  g_setopt(&option
6a10: 73 5b 38 5d 2c 20 22 6c 6f 67 22 2c 20 72 65 71  s[8], "log", req
6a20: 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20  uired_argument, 
6a30: 27 6c 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74  'l');..filed_get
6a40: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28  opt_long_setopt(
6a50: 26 6f 70 74 69 6f 6e 73 5b 39 5d 2c 20 22 76 65  &options[9], "ve
6a60: 72 73 69 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d  rsion", no_argum
6a70: 65 6e 74 2c 20 27 76 27 29 3b 0a 09 66 69 6c 65  ent, 'v');..file
6a80: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65  d_getopt_long_se
6a90: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 30  topt(&options[10
6aa0: 5d 2c 20 4e 55 4c 4c 2c 20 30 2c 20 30 29 3b 0a  ], NULL, 0, 0);.
6ab0: 09 77 68 69 6c 65 20 28 28 63 68 20 3d 20 67 65  .while ((ch = ge
6ac0: 74 6f 70 74 5f 6c 6f 6e 67 28 61 72 67 63 2c 20  topt_long(argc, 
6ad0: 61 72 67 76 2c 20 22 70 3a 74 3a 63 3a 62 3a 75  argv, "p:t:c:b:u
6ae0: 3a 72 3a 6c 3a 68 64 76 22 2c 20 6f 70 74 69 6f  :r:l:hdv", optio
6af0: 6e 73 2c 20 4e 55 4c 4c 29 29 20 21 3d 20 2d 31  ns, NULL)) != -1
6b00: 29 20 7b 0a 09 09 73 77 69 74 63 68 28 63 68 29  ) {...switch(ch)
6b10: 20 7b 0a 09 09 09 63 61 73 65 20 27 70 27 3a 0a   {....case 'p':.
6b20: 09 09 09 09 70 6f 72 74 20 3d 20 61 74 6f 69 28  ....port = atoi(
6b30: 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65  optarg);.....bre
6b40: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 74 27 3a  ak;....case 't':
6b50: 0a 09 09 09 09 74 68 72 65 61 64 5f 63 6f 75 6e  .....thread_coun
6b60: 74 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29  t = atoi(optarg)
6b70: 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09  ;.....break;....
6b80: 63 61 73 65 20 27 63 27 3a 0a 09 09 09 09 63 61  case 'c':.....ca
6b90: 63 68 65 5f 73 69 7a 65 20 3d 20 61 74 6f 69 28  che_size = atoi(
6ba0: 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65  optarg);.....bre
6bb0: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 62 27 3a  ak;....case 'b':
6bc0: 0a 09 09 09 09 62 69 6e 64 5f 61 64 64 72 20 3d  .....bind_addr =
6bd0: 20 73 74 72 64 75 70 28 6f 70 74 61 72 67 29 3b   strdup(optarg);
6be0: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63  .....break;....c
6bf0: 61 73 65 20 27 75 27 3a 0a 09 09 09 09 73 65 74  ase 'u':.....set
6c00: 75 69 64 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b  uid_enabled = 1;
6c10: 0a 09 09 09 09 6c 6f 6f 6b 75 70 5f 72 65 74 20  .....lookup_ret 
6c20: 3d 20 66 69 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f  = filed_user_loo
6c30: 6b 75 70 28 6f 70 74 61 72 67 2c 20 26 75 73 65  kup(optarg, &use
6c40: 72 29 3b 0a 09 09 09 09 69 66 20 28 6c 6f 6f 6b  r);.....if (look
6c50: 75 70 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09  up_ret != 0) {..
6c60: 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f  ....filed_print_
6c70: 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20  help(stderr, 0, 
6c80: 22 49 6e 76 61 6c 69 64 20 75 73 65 72 6e 61 6d  "Invalid usernam
6c90: 65 20 73 70 65 63 69 66 69 65 64 22 29 3b 0a 0a  e specified");..
6ca0: 09 09 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a  .....return(1);.
6cb0: 09 09 09 09 7d 0a 09 09 09 09 62 72 65 61 6b 3b  ....}.....break;
6cc0: 0a 09 09 09 63 61 73 65 20 27 72 27 3a 0a 09 09  ....case 'r':...
6cd0: 09 09 6e 65 77 72 6f 6f 74 20 3d 20 73 74 72 64  ..newroot = strd
6ce0: 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09  up(optarg);.....
6cf0: 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27  break;....case '
6d00: 6c 27 3a 0a 09 09 09 09 6c 6f 67 5f 66 69 6c 65  l':.....log_file
6d10: 20 3d 20 73 74 72 64 75 70 28 6f 70 74 61 72 67   = strdup(optarg
6d20: 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09  );.....break;...
6d30: 09 63 61 73 65 20 27 64 27 3a 0a 09 09 09 09 64  .case 'd':.....d
6d40: 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20  aemon_enabled = 
6d50: 31 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09  1;.....break;...
6d60: 09 63 61 73 65 20 27 76 27 3a 0a 09 09 09 09 70  .case 'v':.....p
6d70: 72 69 6e 74 66 28 22 66 69 6c 65 64 20 76 65 72  rintf("filed ver
6d80: 73 69 6f 6e 20 25 73 5c 6e 22 2c 20 46 49 4c 45  sion %s\n", FILE
6d90: 44 5f 56 45 52 53 49 4f 4e 29 3b 0a 0a 09 09 09  D_VERSION);.....
6da0: 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 09 63  .return(0);....c
6db0: 61 73 65 20 27 3f 27 3a 0a 09 09 09 63 61 73 65  ase '?':....case
6dc0: 20 27 3a 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f   ':':.....filed_
6dd0: 70 72 69 6e 74 5f 68 65 6c 70 28 73 74 64 65 72  print_help(stder
6de0: 72 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09  r, 0, NULL);....
6df0: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 09  ..return(1);....
6e00: 63 61 73 65 20 27 68 27 3a 0a 09 09 09 09 66 69  case 'h':.....fi
6e10: 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73  led_print_help(s
6e20: 74 64 6f 75 74 2c 20 31 2c 20 4e 55 4c 4c 29 3b  tdout, 1, NULL);
6e30: 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 30 29 3b  ......return(0);
6e40: 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 4f 70 65  ...}..}.../* Ope
6e50: 6e 20 6c 6f 67 20 66 69 6c 65 20 2a 2f 0a 09 69  n log file */..i
6e60: 66 20 28 73 74 72 63 6d 70 28 6c 6f 67 5f 66 69  f (strcmp(log_fi
6e70: 6c 65 2c 20 22 2d 22 29 20 3d 3d 20 30 29 20 7b  le, "-") == 0) {
6e80: 0a 09 09 6c 6f 67 5f 66 70 20 3d 20 73 74 64 6f  ...log_fp = stdo
6e90: 75 74 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09  ut;..} else {...
6ea0: 6c 6f 67 5f 66 70 20 3d 20 66 6f 70 65 6e 28 6c  log_fp = fopen(l
6eb0: 6f 67 5f 66 69 6c 65 2c 20 22 61 2b 22 29 3b 0a  og_file, "a+");.
6ec0: 09 09 69 66 20 28 6c 6f 67 5f 66 70 20 3d 3d 20  ..if (log_fp == 
6ed0: 4e 55 4c 4c 29 20 7b 0a 09 09 09 70 65 72 72 6f  NULL) {....perro
6ee0: 72 28 22 66 6f 70 65 6e 22 29 3b 0a 0a 09 09 09  r("fopen");.....
6ef0: 72 65 74 75 72 6e 28 34 29 3b 0a 09 09 7d 0a 09  return(4);...}..
6f00: 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 69  }.../* Create li
6f10: 73 74 65 6e 69 6e 67 20 73 6f 63 6b 65 74 20 2a  stening socket *
6f20: 2f 0a 09 66 64 20 3d 20 66 69 6c 65 64 5f 6c 69  /..fd = filed_li
6f30: 73 74 65 6e 28 62 69 6e 64 5f 61 64 64 72 2c 20  sten(bind_addr, 
6f40: 70 6f 72 74 29 3b 0a 09 69 66 20 28 66 64 20 3c  port);..if (fd <
6f50: 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22   0) {...perror("
6f60: 66 69 6c 65 64 5f 6c 69 73 74 65 6e 22 29 3b 0a  filed_listen");.
6f70: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d  ...return(1);..}
6f80: 0a 0a 09 2f 2a 20 42 65 63 6f 6d 65 20 61 20 64  .../* Become a d
6f90: 61 65 6d 6f 6e 20 2a 2f 0a 09 69 66 20 28 64 61  aemon */..if (da
6fa0: 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 29 20 7b 0a  emon_enabled) {.
6fb0: 09 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c  ..init_ret = fil
6fc0: 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 29 3b 0a  ed_daemonize();.
6fd0: 09 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21  ..if (init_ret !
6fe0: 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72  = 0) {....perror
6ff0: 28 22 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a  ("filed_daemoniz
7000: 65 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28  e");.....return(
7010: 36 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20  6);...}..}.../* 
7020: 43 68 72 6f 6f 74 2c 20 69 66 20 61 70 70 72 6f  Chroot, if appro
7030: 70 72 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 6e  priate */..if (n
7040: 65 77 72 6f 6f 74 29 20 7b 0a 09 09 63 68 64 69  ewroot) {...chdi
7050: 72 5f 72 65 74 20 3d 20 63 68 64 69 72 28 6e 65  r_ret = chdir(ne
7060: 77 72 6f 6f 74 29 3b 0a 09 09 69 66 20 28 63 68  wroot);...if (ch
7070: 64 69 72 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a  dir_ret != 0) {.
7080: 09 09 09 70 65 72 72 6f 72 28 22 63 68 64 69 72  ...perror("chdir
7090: 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31  ");.....return(1
70a0: 29 3b 0a 09 09 7d 0a 0a 09 09 63 68 72 6f 6f 74  );...}....chroot
70b0: 5f 72 65 74 20 3d 20 63 68 72 6f 6f 74 28 22 2e  _ret = chroot(".
70c0: 22 29 3b 0a 09 09 69 66 20 28 63 68 72 6f 6f 74  ");...if (chroot
70d0: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09  _ret != 0) {....
70e0: 70 65 72 72 6f 72 28 22 63 68 72 6f 6f 74 22 29  perror("chroot")
70f0: 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b  ;.....return(1);
7100: 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 72 6f  ...}..}.../* Dro
7110: 70 20 70 72 69 76 69 6c 65 67 65 73 2c 20 69 66  p privileges, if
7120: 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a   appropriate */.
7130: 09 69 66 20 28 73 65 74 75 69 64 5f 65 6e 61 62  .if (setuid_enab
7140: 6c 65 64 29 20 7b 0a 09 09 73 65 74 75 69 64 5f  led) {...setuid_
7150: 72 65 74 20 3d 20 73 65 74 75 69 64 28 75 73 65  ret = setuid(use
7160: 72 29 3b 0a 09 09 69 66 20 28 73 65 74 75 69 64  r);...if (setuid
7170: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09  _ret != 0) {....
7180: 70 65 72 72 6f 72 28 22 73 65 74 75 69 64 22 29  perror("setuid")
7190: 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b  ;.....return(1);
71a0: 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69  ...}..}.../* Ini
71b0: 74 69 61 6c 69 7a 65 20 2a 2f 0a 09 69 6e 69 74  tialize */..init
71c0: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 69 6e 69  _ret = filed_ini
71d0: 74 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09  t(cache_size);..
71e0: 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20  if (init_ret != 
71f0: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66  0) {...perror("f
7200: 69 6c 65 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09  iled_init");....
7210: 72 65 74 75 72 6e 28 33 29 3b 0a 09 7d 0a 0a 09  return(3);..}...
7220: 2f 2a 20 43 72 65 61 74 65 20 6c 6f 67 67 69 6e  /* Create loggin
7230: 67 20 74 68 72 65 61 64 20 2a 2f 0a 09 69 6e 69  g thread */..ini
7240: 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 6c 6f  t_ret = filed_lo
7250: 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69  gging_thread_ini
7260: 74 28 6c 6f 67 5f 66 70 29 3b 0a 09 69 66 20 28  t(log_fp);..if (
7270: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b  init_ret != 0) {
7280: 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64  ...perror("filed
7290: 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f  _logging_thread_
72a0: 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72  init");....retur
72b0: 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72  n(4);..}.../* Cr
72c0: 65 61 74 65 20 77 6f 72 6b 65 72 20 74 68 72 65  eate worker thre
72d0: 61 64 73 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74  ads */..init_ret
72e0: 20 3d 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f   = filed_worker_
72f0: 74 68 72 65 61 64 73 5f 69 6e 69 74 28 66 64 2c  threads_init(fd,
7300: 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 29 3b 0a   thread_count);.
7310: 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d  .if (init_ret !=
7320: 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22   0) {...perror("
7330: 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72  filed_worker_thr
7340: 65 61 64 73 5f 69 6e 69 74 22 29 3b 0a 0a 09 09  eads_init");....
7350: 72 65 74 75 72 6e 28 35 29 3b 0a 09 7d 0a 0a 09  return(5);..}...
7360: 2f 2a 20 57 61 69 74 20 66 6f 72 20 74 68 72 65  /* Wait for thre
7370: 61 64 73 20 74 6f 20 65 78 69 74 20 2a 2f 0a 09  ads to exit */..
7380: 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 4d 6f 6e  /* XXX:TODO: Mon
7390: 69 74 6f 72 20 74 68 72 65 61 64 20 75 73 61 67  itor thread usag
73a0: 65 20 2a 2f 0a 09 77 68 69 6c 65 20 28 31 29 20  e */..while (1) 
73b0: 7b 0a 09 09 73 6c 65 65 70 28 36 30 29 3b 0a 09  {...sleep(60);..
73c0: 7d 0a 0a 09 2f 2a 20 52 65 74 75 72 6e 20 69 6e  }.../* Return in
73d0: 20 66 61 69 6c 75 72 65 20 2a 2f 0a 09 72 65 74   failure */..ret
73e0: 75 72 6e 28 32 29 3b 0a 7d 0a                    urn(2);.}.