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 73 79 73 2f 70 72 63 74 6c 2e 68 3e 0a e <sys/prctl.h>.
0060: 23 69 6e 63 6c 75 64 65 20 3c 61 72 70 61 2f 69 #include <arpa/i
0070: 6e 65 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 net.h>.#include
0080: 3c 73 79 73 2f 6d 6d 61 6e 2e 68 3e 0a 23 69 6e <sys/mman.h>.#in
0090: 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e clude <sys/stat.
00a0: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 h>.#include <sys
00b0: 2f 77 61 69 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 /wait.h>.#includ
00c0: 65 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a 23 69 e <pthread.h>.#i
00d0: 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 73 2e nclude <strings.
00e0: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 69 67 h>.#include <sig
00f0: 6e 61 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 nal.h>.#include
0100: 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c <stdlib.h>.#incl
0110: 75 64 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 ude <unistd.h>.#
0120: 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e include <string.
0130: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 67 65 74 h>.#include <get
0140: 6f 70 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 opt.h>.#include
0150: 3c 73 74 64 61 72 67 2e 68 3e 0a 23 69 6e 63 6c <stdarg.h>.#incl
0160: 75 64 65 20 3c 66 63 6e 74 6c 2e 68 3e 0a 23 69 ude <fcntl.h>.#i
0170: 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e nclude <stdio.h>
0180: 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f .#include <errno
0190: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 .h>.#include <ti
01a0: 6d 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c me.h>.#include <
01b0: 70 77 64 2e 68 3e 0a 0a 2f 2a 20 43 6f 6d 70 69 pwd.h>../* Compi
01c0: 6c 65 20 74 69 6d 65 20 63 6f 6e 73 74 61 6e 74 le time constant
01d0: 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 49 4c s */.#define FIL
01e0: 45 44 5f 56 45 52 53 49 4f 4e 20 22 31 2e 39 22 ED_VERSION "1.9"
01f0: 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 53 .#define FILED_S
0200: 45 4e 44 46 49 4c 45 5f 4d 41 58 20 31 36 37 37 ENDFILE_MAX 1677
0210: 37 32 31 35 0a 23 64 65 66 69 6e 65 20 46 49 4c 7215.#define FIL
0220: 45 44 5f 4d 41 58 5f 46 41 49 4c 55 52 45 5f 43 ED_MAX_FAILURE_C
0230: 4f 55 4e 54 20 33 30 0a 23 64 65 66 69 6e 65 20 OUNT 30.#define
0240: 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 FILED_DEFAULT_TY
0250: 50 45 20 22 61 70 70 6c 69 63 61 74 69 6f 6e 2f PE "application/
0260: 6f 63 74 65 74 2d 73 74 72 65 61 6d 22 0a 23 64 octet-stream".#d
0270: 65 66 69 6e 65 20 46 49 4c 45 44 5f 50 41 54 48 efine FILED_PATH
0280: 5f 42 55 46 46 45 52 5f 53 49 5a 45 20 31 30 31 _BUFFER_SIZE 101
0290: 30 0a 0a 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 0../* Default va
02a0: 6c 75 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 lues */.#define
02b0: 50 4f 52 54 20 38 30 0a 23 64 65 66 69 6e 65 20 PORT 80.#define
02c0: 54 48 52 45 41 44 5f 43 4f 55 4e 54 20 35 0a 23 THREAD_COUNT 5.#
02d0: 64 65 66 69 6e 65 20 42 49 4e 44 5f 41 44 44 52 define BIND_ADDR
02e0: 20 22 3a 3a 22 0a 23 64 65 66 69 6e 65 20 43 41 "::".#define CA
02f0: 43 48 45 5f 53 49 5a 45 20 38 32 30 39 0a 23 64 CHE_SIZE 8209.#d
0300: 65 66 69 6e 65 20 4c 4f 47 5f 46 49 4c 45 20 22 efine LOG_FILE "
0310: 2d 22 0a 0a 2f 2a 20 43 6f 6e 66 69 67 75 72 61 -"../* Configura
0320: 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 tion options tha
0330: 74 20 77 6f 72 6b 20 74 68 72 65 61 64 73 20 6e t work threads n
0340: 65 65 64 20 74 6f 20 62 65 20 61 77 61 72 65 20 eed to be aware
0350: 6f 66 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c of */.struct fil
0360: 65 64 5f 6f 70 74 69 6f 6e 73 20 7b 0a 09 69 6e ed_options {..in
0370: 74 20 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 64 t vhosts_enabled
0380: 3b 0a 7d 3b 0a 0a 2f 2a 20 41 72 67 75 6d 65 6e ;.};../* Argumen
0390: 74 73 20 66 6f 72 20 77 6f 72 6b 65 72 20 74 68 ts for worker th
03a0: 72 65 61 64 73 20 2a 2f 0a 73 74 72 75 63 74 20 reads */.struct
03b0: 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 filed_worker_thr
03c0: 65 61 64 5f 61 72 67 73 20 7b 0a 09 69 6e 74 20 ead_args {..int
03d0: 66 64 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 fd;..struct file
03e0: 64 5f 6f 70 74 69 6f 6e 73 20 6f 70 74 69 6f 6e d_options option
03f0: 73 3b 0a 7d 3b 0a 0a 2f 2a 20 41 72 67 75 6d 65 s;.};../* Argume
0400: 6e 74 73 20 66 6f 72 20 6c 6f 67 67 69 6e 67 20 nts for logging
0410: 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 72 75 63 threads */.struc
0420: 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f t filed_logging_
0430: 74 68 72 65 61 64 5f 61 72 67 73 20 7b 0a 09 46 thread_args {..F
0440: 49 4c 45 20 2a 66 70 3b 0a 7d 3b 0a 0a 2f 2a 20 ILE *fp;.};../*
0450: 46 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e File information
0460: 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 */.struct filed
0470: 5f 66 69 6c 65 69 6e 66 6f 20 7b 0a 09 70 74 68 _fileinfo {..pth
0480: 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 read_mutex_t mut
0490: 65 78 3b 0a 09 63 68 61 72 20 70 61 74 68 5b 46 ex;..char path[F
04a0: 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 ILED_PATH_BUFFER
04b0: 5f 53 49 5a 45 5d 3b 0a 09 69 6e 74 20 66 64 3b _SIZE];..int fd;
04c0: 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09 63 68 ..off_t len;..ch
04d0: 61 72 20 2a 6c 61 73 74 6d 6f 64 3b 0a 09 63 68 ar *lastmod;..ch
04e0: 61 72 20 6c 61 73 74 6d 6f 64 5f 62 5b 36 34 5d ar lastmod_b[64]
04f0: 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 ;..const char *t
0500: 79 70 65 3b 0a 09 63 68 61 72 20 65 74 61 67 5b ype;..char etag[
0510: 36 34 5d 3b 0a 7d 3b 0a 0a 2f 2a 20 52 65 71 75 64];.};../* Requ
0520: 65 73 74 20 76 61 72 69 61 62 6c 65 73 20 2a 2f est variables */
0530: 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 .struct filed_ht
0540: 74 70 5f 72 65 71 75 65 73 74 20 7b 0a 09 2f 2a tp_request {../*
0550: 2a 20 42 75 66 66 65 72 73 20 2a 2a 2f 0a 09 73 * Buffers **/..s
0560: 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 truct filed_file
0570: 69 6e 66 6f 20 66 69 6c 65 69 6e 66 6f 3b 0a 09 info fileinfo;..
0580: 63 68 61 72 20 74 6d 70 62 75 66 5b 46 49 4c 45 char tmpbuf[FILE
0590: 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 D_PATH_BUFFER_SI
05a0: 5a 45 5d 3b 0a 0a 09 2f 2a 2a 20 48 54 54 50 20 ZE];.../** HTTP
05b0: 52 65 71 75 65 73 74 20 69 6e 66 6f 72 6d 61 74 Request informat
05c0: 69 6f 6e 20 2a 2a 2f 0a 09 2f 2a 2a 2a 20 54 79 ion **/../*** Ty
05d0: 70 65 20 6f 66 20 72 65 71 75 65 73 74 20 28 48 pe of request (H
05e0: 45 41 44 20 6f 72 20 47 45 54 29 20 2a 2a 2a 2f EAD or GET) ***/
05f0: 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 4c 45 44 ..enum {...FILED
0600: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f _REQUEST_METHOD_
0610: 47 45 54 2c 0a 09 09 46 49 4c 45 44 5f 52 45 51 GET,...FILED_REQ
0620: 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 UEST_METHOD_HEAD
0630: 0a 09 7d 20 6d 65 74 68 6f 64 3b 0a 0a 09 2f 2a ..} method;.../*
0640: 2a 2a 20 50 61 74 68 20 62 65 69 6e 67 20 72 65 ** Path being re
0650: 71 75 65 73 74 65 64 20 2a 2a 2a 2f 0a 09 63 68 quested ***/..ch
0660: 61 72 20 70 61 74 68 5b 46 49 4c 45 44 5f 50 41 ar path[FILED_PA
0670: 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b TH_BUFFER_SIZE];
0680: 20 0a 0a 09 2f 2a 2a 2a 20 50 61 74 68 20 74 79 .../*** Path ty
0690: 70 65 20 2a 2a 2a 2f 0a 09 65 6e 75 6d 20 7b 0a pe ***/..enum {.
06a0: 09 09 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f ..FILED_REQUEST_
06b0: 54 59 50 45 5f 44 49 52 45 43 54 4f 52 59 2c 0a TYPE_DIRECTORY,.
06c0: 09 09 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f ..FILED_REQUEST_
06d0: 54 59 50 45 5f 4f 54 48 45 52 0a 09 7d 20 74 79 TYPE_OTHER..} ty
06e0: 70 65 3b 0a 0a 09 73 74 72 75 63 74 20 7b 0a 09 pe;...struct {..
06f0: 09 73 74 72 75 63 74 20 7b 0a 09 09 09 69 6e 74 .struct {....int
0700: 20 70 72 65 73 65 6e 74 3b 0a 09 09 09 6f 66 66 present;....off
0710: 5f 74 20 6f 66 66 73 65 74 3b 20 20 20 2f 2a 2a _t offset; /**
0720: 2a 20 52 61 6e 67 65 20 73 74 61 72 74 20 2a 2a * Range start **
0730: 2a 2f 0a 09 09 09 6f 66 66 5f 74 20 6c 65 6e 67 */....off_t leng
0740: 74 68 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67 65 th; /*** Range
0750: 20 6c 65 6e 67 74 68 20 2a 2a 2a 2f 0a 09 09 7d length ***/...}
0760: 20 72 61 6e 67 65 3b 0a 0a 09 09 73 74 72 75 63 range;....struc
0770: 74 20 7b 0a 09 09 09 69 6e 74 20 70 72 65 73 65 t {....int prese
0780: 6e 74 3b 0a 09 09 09 63 68 61 72 20 68 6f 73 74 nt;....char host
0790: 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 [FILED_PATH_BUFF
07a0: 45 52 5f 53 49 5a 45 5d 3b 0a 09 09 7d 20 68 6f ER_SIZE];...} ho
07b0: 73 74 3b 0a 09 7d 20 68 65 61 64 65 72 73 3b 0a st;..} headers;.
07c0: 7d 3b 0a 0a 2f 2a 20 4c 6f 67 20 72 65 63 6f 72 };../* Log recor
07d0: 64 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 d */.struct file
07e0: 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 7b 0a 09 2f d_log_entry {../
07f0: 2a 20 54 79 70 65 20 6f 66 20 6c 6f 67 20 65 6e * Type of log en
0800: 74 72 79 20 2a 2f 0a 09 65 6e 75 6d 20 7b 0a 09 try */..enum {..
0810: 09 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f .FILED_LOG_TYPE_
0820: 4d 45 53 53 41 47 45 2c 0a 09 09 46 49 4c 45 44 MESSAGE,...FILED
0830: 5f 4c 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 _LOG_TYPE_TRANSF
0840: 45 52 0a 09 7d 20 74 79 70 65 3b 0a 0a 09 2f 2a ER..} type;.../*
0850: 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 Linked list hea
0860: 64 2f 74 61 69 6c 20 2a 2f 0a 09 73 74 72 75 63 d/tail */..struc
0870: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
0880: 79 20 2a 5f 6e 65 78 74 3b 0a 09 73 74 72 75 63 y *_next;..struc
0890: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
08a0: 79 20 2a 5f 70 72 65 76 3b 0a 0a 09 2f 2a 20 54 y *_prev;.../* T
08b0: 68 72 65 61 64 20 66 72 6f 6d 20 77 68 69 63 68 hread from which
08c0: 20 74 68 69 73 20 6c 6f 67 20 65 6e 74 72 79 20 this log entry
08d0: 65 6d 69 6e 61 74 65 73 20 2a 2f 0a 09 70 74 68 eminates */..pth
08e0: 72 65 61 64 5f 74 20 74 68 72 65 61 64 3b 0a 0a read_t thread;..
08f0: 09 2f 2a 20 4d 65 73 73 61 67 65 20 62 75 66 66 ./* Message buff
0900: 65 72 20 66 6f 72 20 74 79 70 65 20 3d 20 4d 45 er for type = ME
0910: 53 53 41 47 45 20 2a 2f 0a 09 2f 2a 20 50 61 74 SSAGE */../* Pat
0920: 68 20 62 75 66 66 65 72 20 66 6f 72 20 74 79 70 h buffer for typ
0930: 65 20 3d 20 54 52 41 4e 53 46 45 52 20 2a 2f 0a e = TRANSFER */.
0940: 09 63 68 61 72 20 62 75 66 66 65 72 5b 46 49 4c .char buffer[FIL
0950: 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 ED_PATH_BUFFER_S
0960: 49 5a 45 5d 3b 0a 0a 09 2f 2a 20 49 74 65 6d 73 IZE];.../* Items
0970: 20 66 6f 72 20 74 79 70 65 20 3d 20 54 52 41 4e for type = TRAN
0980: 53 46 45 52 20 2a 2f 0a 09 69 6e 74 20 68 74 74 SFER */..int htt
0990: 70 5f 63 6f 64 65 3b 0a 09 63 6f 6e 73 74 20 63 p_code;..const c
09a0: 68 61 72 20 2a 72 65 61 73 6f 6e 3b 0a 09 74 69 har *reason;..ti
09b0: 6d 65 5f 74 20 73 74 61 72 74 74 69 6d 65 3b 0a me_t starttime;.
09c0: 09 74 69 6d 65 5f 74 20 65 6e 64 74 69 6d 65 3b .time_t endtime;
09d0: 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6f 66 66 73 ..off_t req_offs
09e0: 65 74 3b 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6c et;..off_t req_l
09f0: 65 6e 67 74 68 3b 0a 09 6f 66 66 5f 74 20 73 65 ength;..off_t se
0a00: 6e 74 5f 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 5f nt_length;..off_
0a10: 74 20 66 69 6c 65 5f 6c 65 6e 67 74 68 3b 0a 09 t file_length;..
0a20: 63 68 61 72 20 69 70 5b 31 32 38 5d 3b 0a 09 69 char ip[128];..i
0a30: 6e 74 20 70 6f 72 74 3b 0a 09 69 6e 74 20 6d 65 nt port;..int me
0a40: 74 68 6f 64 3b 0a 7d 3b 0a 0a 2f 2a 20 47 6c 6f thod;.};../* Glo
0a50: 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 2a 2f bal variables */
0a60: 0a 2f 2a 2a 20 4f 70 65 6e 20 46 69 6c 65 20 63 ./** Open File c
0a70: 61 63 68 65 20 2a 2a 2f 0a 73 74 72 75 63 74 20 ache **/.struct
0a80: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a filed_fileinfo *
0a90: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
0aa0: 64 63 61 63 68 65 20 3d 20 4e 55 4c 4c 3b 0a 75 dcache = NULL;.u
0ab0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 nsigned int file
0ac0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
0ad0: 68 65 5f 73 69 7a 65 20 3d 20 30 3b 0a 0a 2f 2a he_size = 0;../*
0ae0: 2a 20 4c 6f 67 67 69 6e 67 20 2a 2a 2f 0a 73 74 * Logging **/.st
0af0: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 ruct filed_log_e
0b00: 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f ntry *filed_log_
0b10: 6d 73 67 5f 6c 69 73 74 3b 0a 70 74 68 72 65 61 msg_list;.pthrea
0b20: 64 5f 6d 75 74 65 78 5f 74 20 66 69 6c 65 64 5f d_mutex_t filed_
0b30: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 log_msg_list_mut
0b40: 65 78 3b 0a 70 74 68 72 65 61 64 5f 63 6f 6e 64 ex;.pthread_cond
0b50: 5f 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 _t filed_log_msg
0b60: 5f 6c 69 73 74 5f 72 65 61 64 79 3b 0a 0a 2f 2a _list_ready;../*
0b70: 20 49 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 Initialize cach
0b80: 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 e */.static int
0b90: 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 65 filed_init_cache
0ba0: 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 (unsigned int ca
0bb0: 63 68 65 5f 73 69 7a 65 29 20 7b 0a 09 75 6e 73 che_size) {..uns
0bc0: 69 67 6e 65 64 20 69 6e 74 20 69 64 78 3b 0a 09 igned int idx;..
0bd0: 69 6e 74 20 6d 75 74 65 78 5f 69 6e 69 74 5f 72 int mutex_init_r
0be0: 65 74 3b 0a 0a 09 2f 2a 20 43 61 63 68 65 20 6d et;.../* Cache m
0bf0: 61 79 20 6e 6f 74 20 62 65 20 72 65 2d 69 6e 69 ay not be re-ini
0c00: 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 09 69 66 20 tialized */..if
0c10: 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f (filed_fileinfo_
0c20: 66 64 63 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 fdcache_size !=
0c30: 30 20 7c 7c 20 66 69 6c 65 64 5f 66 69 6c 65 69 0 || filed_filei
0c40: 6e 66 6f 5f 66 64 63 61 63 68 65 20 21 3d 20 4e nfo_fdcache != N
0c50: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 ULL) {...return(
0c60: 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 41 6c 6c 6f 1);..}.../* Allo
0c70: 63 61 74 65 20 63 61 63 68 65 20 2a 2f 0a 09 66 cate cache */..f
0c80: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
0c90: 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 63 61 63 cache_size = cac
0ca0: 68 65 5f 73 69 7a 65 3b 0a 09 66 69 6c 65 64 5f he_size;..filed_
0cb0: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
0cc0: 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 = malloc(sizeof
0cd0: 28 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f (*filed_fileinfo
0ce0: 5f 66 64 63 61 63 68 65 29 20 2a 20 66 69 6c 65 _fdcache) * file
0cf0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
0d00: 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 66 he_size);..if (f
0d10: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
0d20: 63 61 63 68 65 20 3d 3d 20 4e 55 4c 4c 29 20 7b cache == NULL) {
0d30: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d ...return(1);..}
0d40: 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 .../* Initialize
0d50: 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 20 2a cache entries *
0d60: 2f 0a 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b /..for (idx = 0;
0d70: 20 69 64 78 20 3c 20 66 69 6c 65 64 5f 66 69 6c idx < filed_fil
0d80: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 einfo_fdcache_si
0d90: 7a 65 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09 6d ze; idx++) {...m
0da0: 75 74 65 78 5f 69 6e 69 74 5f 72 65 74 20 3d 20 utex_init_ret =
0db0: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e pthread_mutex_in
0dc0: 69 74 28 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e it(&filed_filein
0dd0: 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e fo_fdcache[idx].
0de0: 6d 75 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a 09 09 mutex, NULL);...
0df0: 69 66 20 28 6d 75 74 65 78 5f 69 6e 69 74 5f 72 if (mutex_init_r
0e00: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 et != 0) {....re
0e10: 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 turn(1);...}....
0e20: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
0e30: 64 63 61 63 68 65 5b 69 64 78 5d 2e 70 61 74 68 dcache[idx].path
0e40: 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 66 69 [0] = '\0';...fi
0e50: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
0e60: 61 63 68 65 5b 69 64 78 5d 2e 66 64 20 3d 20 2d ache[idx].fd = -
0e70: 31 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 1;...filed_filei
0e80: 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d nfo_fdcache[idx]
0e90: 2e 6c 61 73 74 6d 6f 64 20 3d 20 22 22 3b 0a 09 .lastmod = "";..
0ea0: 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f .filed_fileinfo_
0eb0: 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 74 79 70 fdcache[idx].typ
0ec0: 65 20 3d 20 22 22 3b 0a 09 7d 0a 0a 09 72 65 74 e = "";..}...ret
0ed0: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 49 6e urn(0);.}../* In
0ee0: 69 74 69 61 6c 69 7a 65 20 70 72 6f 63 65 73 73 itialize process
0ef0: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 */.static int f
0f00: 69 6c 65 64 5f 69 6e 69 74 28 75 6e 73 69 67 6e iled_init(unsign
0f10: 65 64 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a ed int cache_siz
0f20: 65 29 20 7b 0a 09 73 74 61 74 69 63 20 69 6e 74 e) {..static int
0f30: 20 63 61 6c 6c 65 64 20 3d 20 30 3b 0a 09 75 6e called = 0;..un
0f40: 73 69 67 6e 65 64 20 69 6e 74 20 72 61 6e 64 6f signed int rando
0f50: 6d 5f 76 61 6c 75 65 20 3d 20 30 3b 0a 09 69 6e m_value = 0;..in
0f60: 74 20 63 61 63 68 65 5f 72 65 74 3b 0a 09 69 6e t cache_ret;..in
0f70: 74 20 72 61 6e 64 6f 6d 5f 66 64 3b 0a 0a 09 69 t random_fd;...i
0f80: 66 20 28 63 61 6c 6c 65 64 29 20 7b 0a 09 09 72 f (called) {...r
0f90: 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 63 eturn(0);..}...c
0fa0: 61 6c 6c 65 64 20 3d 20 31 3b 0a 0a 09 2f 2a 20 alled = 1;.../*
0fb0: 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 Attempt to lock
0fc0: 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 6f 20 70 68 all memory to ph
0fd0: 79 73 69 63 61 6c 20 52 41 4d 20 28 62 75 74 20 ysical RAM (but
0fe0: 64 6f 6e 27 74 20 63 61 72 65 20 69 66 20 77 65 don't care if we
0ff0: 20 63 61 6e 27 74 29 20 2a 2f 0a 09 6d 6c 6f 63 can't) */..mloc
1000: 6b 61 6c 6c 28 4d 43 4c 5f 43 55 52 52 45 4e 54 kall(MCL_CURRENT
1010: 20 7c 20 4d 43 4c 5f 46 55 54 55 52 45 29 3b 0a | MCL_FUTURE);.
1020: 0a 09 2f 2a 20 49 67 6e 6f 72 65 20 53 49 47 50 ../* Ignore SIGP
1030: 49 50 45 20 2a 2f 0a 09 73 69 67 6e 61 6c 28 53 IPE */..signal(S
1040: 49 47 50 49 50 45 2c 20 53 49 47 5f 49 47 4e 29 IGPIPE, SIG_IGN)
1050: 3b 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a ;.../* Initializ
1060: 65 20 63 61 63 68 65 20 73 74 72 75 63 74 75 72 e cache structur
1070: 65 20 2a 2f 0a 09 63 61 63 68 65 5f 72 65 74 20 e */..cache_ret
1080: 3d 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63 = filed_init_cac
1090: 68 65 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a he(cache_size);.
10a0: 09 69 66 20 28 63 61 63 68 65 5f 72 65 74 20 21 .if (cache_ret !
10b0: 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 = 0) {...return(
10c0: 63 61 63 68 65 5f 72 65 74 29 3b 0a 09 7d 0a 0a cache_ret);..}..
10d0: 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 72 ./* Initialize r
10e0: 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e andom number gen
10f0: 65 72 61 74 6f 72 20 2a 2f 0a 09 72 61 6e 64 6f erator */..rando
1100: 6d 5f 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 m_fd = open("/de
1110: 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 v/urandom", O_RD
1120: 4f 4e 4c 59 29 3b 0a 09 69 66 20 28 72 61 6e 64 ONLY);..if (rand
1130: 6f 6d 5f 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09 om_fd >= 0) {...
1140: 72 65 61 64 28 72 61 6e 64 6f 6d 5f 66 64 2c 20 read(random_fd,
1150: 26 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 2c 20 73 &random_value, s
1160: 69 7a 65 6f 66 28 72 61 6e 64 6f 6d 5f 76 61 6c izeof(random_val
1170: 75 65 29 29 3b 0a 0a 09 09 63 6c 6f 73 65 28 72 ue));....close(r
1180: 61 6e 64 6f 6d 5f 66 64 29 3b 0a 09 7d 0a 0a 09 andom_fd);..}...
1190: 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 random_value ^=
11a0: 67 65 74 70 69 64 28 29 3b 0a 09 72 61 6e 64 6f getpid();..rando
11b0: 6d 5f 76 61 6c 75 65 20 5e 3d 20 67 65 74 75 69 m_value ^= getui
11c0: 64 28 29 3b 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c d();..random_val
11d0: 75 65 20 5e 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 ue ^= time(NULL)
11e0: 3b 0a 0a 09 73 72 61 6e 64 6f 6d 28 72 61 6e 64 ;...srandom(rand
11f0: 6f 6d 5f 76 61 6c 75 65 29 3b 0a 0a 09 72 65 74 om_value);...ret
1200: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 4c 69 urn(0);.}../* Li
1210: 73 74 65 6e 20 6f 6e 20 61 20 70 61 72 74 69 63 sten on a partic
1220: 75 6c 61 72 20 61 64 64 72 65 73 73 2f 70 6f 72 ular address/por
1230: 74 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 t */.static int
1240: 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 63 6f 6e filed_listen(con
1250: 73 74 20 63 68 61 72 20 2a 61 64 64 72 65 73 73 st char *address
1260: 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 70 , unsigned int p
1270: 6f 72 74 29 20 7b 0a 09 73 74 72 75 63 74 20 73 ort) {..struct s
1280: 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 64 72 ockaddr_in6 addr
1290: 5f 76 36 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 _v6;..struct soc
12a0: 6b 61 64 64 72 5f 69 6e 20 61 64 64 72 5f 76 34 kaddr_in addr_v4
12b0: 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 ;..struct sockad
12c0: 64 72 20 2a 61 64 64 72 3b 0a 09 73 6f 63 6b 6c dr *addr;..sockl
12d0: 65 6e 5f 74 20 61 64 64 72 5f 6c 65 6e 3b 0a 09 en_t addr_len;..
12e0: 69 6e 74 20 70 74 6f 6e 5f 72 65 74 2c 20 62 69 int pton_ret, bi
12f0: 6e 64 5f 72 65 74 2c 20 6c 69 73 74 65 6e 5f 72 nd_ret, listen_r
1300: 65 74 3b 0a 09 69 6e 74 20 66 61 6d 69 6c 79 3b et;..int family;
1310: 0a 09 69 6e 74 20 66 64 3b 0a 0a 0a 09 66 61 6d ..int fd;....fam
1320: 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 3b 0a ily = AF_INET6;.
1330: 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e 65 74 .pton_ret = inet
1340: 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 61 64 _pton(family, ad
1350: 64 72 65 73 73 2c 20 26 61 64 64 72 5f 76 36 2e dress, &addr_v6.
1360: 73 69 6e 36 5f 61 64 64 72 2e 73 36 5f 61 64 64 sin6_addr.s6_add
1370: 72 29 3b 0a 09 69 66 20 28 70 74 6f 6e 5f 72 65 r);..if (pton_re
1380: 74 20 21 3d 20 31 29 20 7b 0a 09 09 66 61 6d 69 t != 1) {...fami
1390: 6c 79 20 3d 20 41 46 5f 49 4e 45 54 3b 0a 09 09 ly = AF_INET;...
13a0: 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e 65 74 5f pton_ret = inet_
13b0: 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 61 64 64 pton(family, add
13c0: 72 65 73 73 2c 20 26 61 64 64 72 5f 76 34 2e 73 ress, &addr_v4.s
13d0: 69 6e 5f 61 64 64 72 2e 73 5f 61 64 64 72 29 3b in_addr.s_addr);
13e0: 0a 09 09 69 66 20 28 70 74 6f 6e 5f 72 65 74 20 ...if (pton_ret
13f0: 21 3d 20 31 29 20 7b 0a 09 09 09 72 65 74 75 72 != 1) {....retur
1400: 6e 28 2d 31 29 3b 0a 09 09 7d 0a 0a 09 09 61 64 n(-1);...}....ad
1410: 64 72 5f 76 34 2e 73 69 6e 5f 66 61 6d 69 6c 79 dr_v4.sin_family
1420: 20 3d 20 66 61 6d 69 6c 79 3b 0a 09 09 61 64 64 = family;...add
1430: 72 5f 76 34 2e 73 69 6e 5f 70 6f 72 74 20 3d 20 r_v4.sin_port =
1440: 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a 09 09 htons(port);....
1450: 61 64 64 72 20 3d 20 28 73 74 72 75 63 74 20 73 addr = (struct s
1460: 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 ockaddr *) &addr
1470: 5f 76 34 3b 0a 09 09 61 64 64 72 5f 6c 65 6e 20 _v4;...addr_len
1480: 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 5f 76 34 = sizeof(addr_v4
1490: 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 61 );..} else {...a
14a0: 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 61 6d 69 ddr_v6.sin6_fami
14b0: 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 3b 0a 09 ly = AF_INET6;..
14c0: 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 6c .addr_v6.sin6_fl
14d0: 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a 09 09 61 64 owinfo = 0;...ad
14e0: 64 72 5f 76 36 2e 73 69 6e 36 5f 73 63 6f 70 65 dr_v6.sin6_scope
14f0: 5f 69 64 20 3d 20 30 3b 0a 09 09 61 64 64 72 5f _id = 0;...addr_
1500: 76 36 2e 73 69 6e 36 5f 70 6f 72 74 20 3d 20 68 v6.sin6_port = h
1510: 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a 09 09 61 tons(port);....a
1520: 64 64 72 20 3d 20 28 73 74 72 75 63 74 20 73 6f ddr = (struct so
1530: 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 5f ckaddr *) &addr_
1540: 76 36 3b 0a 09 09 61 64 64 72 5f 6c 65 6e 20 3d v6;...addr_len =
1550: 20 73 69 7a 65 6f 66 28 61 64 64 72 5f 76 36 29 sizeof(addr_v6)
1560: 3b 0a 09 7d 0a 0a 09 66 64 20 3d 20 73 6f 63 6b ;..}...fd = sock
1570: 65 74 28 66 61 6d 69 6c 79 2c 20 53 4f 43 4b 5f et(family, SOCK_
1580: 53 54 52 45 41 4d 2c 20 30 29 3b 0a 09 69 66 20 STREAM, 0);..if
1590: 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 (fd < 0) {...ret
15a0: 75 72 6e 28 66 64 29 3b 0a 09 7d 0a 0a 09 62 69 urn(fd);..}...bi
15b0: 6e 64 5f 72 65 74 20 3d 20 62 69 6e 64 28 66 64 nd_ret = bind(fd
15c0: 2c 20 61 64 64 72 2c 20 61 64 64 72 5f 6c 65 6e , addr, addr_len
15d0: 29 3b 0a 09 69 66 20 28 62 69 6e 64 5f 72 65 74 );..if (bind_ret
15e0: 20 3c 20 30 29 20 7b 0a 09 09 63 6c 6f 73 65 28 < 0) {...close(
15f0: 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 2d fd);....return(-
1600: 31 29 3b 0a 09 7d 0a 0a 09 6c 69 73 74 65 6e 5f 1);..}...listen_
1610: 72 65 74 20 3d 20 6c 69 73 74 65 6e 28 66 64 2c ret = listen(fd,
1620: 20 31 32 38 29 3b 0a 09 69 66 20 28 6c 69 73 74 128);..if (list
1630: 65 6e 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 en_ret != 0) {..
1640: 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 72 .close(fd);....r
1650: 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 eturn(-1);..}...
1660: 72 65 74 75 72 6e 28 66 64 29 3b 0a 7d 0a 0a 2f return(fd);.}../
1670: 2a 20 4c 6f 67 20 61 20 6d 65 73 73 61 67 65 20 * Log a message
1680: 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f */.#ifdef FILED_
1690: 44 4f 4e 54 5f 4c 4f 47 0a 23 20 20 64 65 66 69 DONT_LOG.# defi
16a0: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 ne filed_logging
16b0: 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 78 29 20 _thread_init(x)
16c0: 30 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 0.# define file
16d0: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 d_log_msg_debug(
16e0: 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 x, ...) /**/.#
16f0: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 define filed_log
1700: 5f 6d 73 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a _msg(x, ...) /**
1710: 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 /.# define file
1720: 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 78 29 20 2f d_log_entry(x) /
1730: 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 69 **/.# define fi
1740: 6c 65 64 5f 6c 6f 67 5f 69 70 28 78 2c 20 2e 2e led_log_ip(x, ..
1750: 2e 29 20 4e 55 4c 4c 0a 23 20 20 64 65 66 69 6e .) NULL.# defin
1760: 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 e filed_log_new(
1770: 78 29 20 26 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f x) &local_dummy_
1780: 6c 6f 67 0a 23 20 20 64 65 66 69 6e 65 20 66 69 log.# define fi
1790: 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 78 29 20 led_log_open(x)
17a0: 73 74 64 6f 75 74 0a 23 65 6c 73 65 0a 23 20 20 stdout.#else.#
17b0: 69 66 64 65 66 20 46 49 4c 45 44 5f 44 45 42 55 ifdef FILED_DEBU
17c0: 47 0a 23 20 20 20 20 64 65 66 69 6e 65 20 66 69 G.# define fi
17d0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 led_log_msg_debu
17e0: 67 28 78 2c 20 2e 2e 2e 29 20 7b 20 66 70 72 69 g(x, ...) { fpri
17f0: 6e 74 66 28 73 74 64 65 72 72 2c 20 78 2c 20 5f ntf(stderr, x, _
1800: 5f 56 41 5f 41 52 47 53 5f 5f 29 3b 20 66 70 72 _VA_ARGS__); fpr
1810: 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 5c 6e intf(stderr, "\n
1820: 22 29 3b 20 66 66 6c 75 73 68 28 73 74 64 65 72 "); fflush(stder
1830: 72 29 3b 20 7d 0a 23 20 20 65 6c 73 65 0a 23 20 r); }.# else.#
1840: 20 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f define filed_
1850: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c log_msg_debug(x,
1860: 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 65 6e ...) /**/.# en
1870: 64 69 66 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 dif../* Initiali
1880: 7a 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 ze logging threa
1890: 64 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 d */.static void
18a0: 20 2a 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f *filed_logging_
18b0: 74 68 72 65 61 64 28 76 6f 69 64 20 2a 61 72 67 thread(void *arg
18c0: 5f 70 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 _p) {..struct fi
18d0: 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 led_logging_thre
18e0: 61 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 73 ad_args *arg;..s
18f0: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
1900: 65 6e 74 72 79 20 2a 63 75 72 72 2c 20 2a 70 72 entry *curr, *pr
1910: 65 76 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 ev;..const char
1920: 2a 6d 65 74 68 6f 64 3b 0a 09 74 69 6d 65 5f 74 *method;..time_t
1930: 20 6e 6f 77 3b 0a 09 46 49 4c 45 20 2a 66 70 3b now;..FILE *fp;
1940: 0a 0a 09 61 72 67 20 3d 20 61 72 67 5f 70 3b 0a ...arg = arg_p;.
1950: 0a 09 66 70 20 3d 20 61 72 67 2d 3e 66 70 3b 0a ..fp = arg->fp;.
1960: 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 ..while (1) {...
1970: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f pthread_mutex_lo
1980: 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 ck(&filed_log_ms
1990: 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 09 g_list_mutex);..
19a0: 09 70 74 68 72 65 61 64 5f 63 6f 6e 64 5f 77 61 .pthread_cond_wa
19b0: 69 74 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 it(&filed_log_ms
19c0: 67 5f 6c 69 73 74 5f 72 65 61 64 79 2c 20 26 66 g_list_ready, &f
19d0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 iled_log_msg_lis
19e0: 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 63 75 72 t_mutex);....cur
19f0: 72 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 r = filed_log_ms
1a00: 67 5f 6c 69 73 74 3b 0a 09 09 66 69 6c 65 64 5f g_list;...filed_
1a10: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 4e log_msg_list = N
1a20: 55 4c 4c 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f ULL;....pthread_
1a30: 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 mutex_unlock(&fi
1a40: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 led_log_msg_list
1a50: 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 6e 6f 77 20 _mutex);....now
1a60: 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 = time(NULL);...
1a70: 09 70 72 65 76 20 3d 20 4e 55 4c 4c 3b 0a 09 09 .prev = NULL;...
1a80: 66 6f 72 20 28 3b 20 63 75 72 72 3b 20 63 75 72 for (; curr; cur
1a90: 72 20 3d 20 63 75 72 72 2d 3e 5f 6e 65 78 74 29 r = curr->_next)
1aa0: 20 7b 0a 09 09 09 63 75 72 72 2d 3e 5f 70 72 65 {....curr->_pre
1ab0: 76 20 3d 20 70 72 65 76 3b 0a 0a 09 09 09 70 72 v = prev;.....pr
1ac0: 65 76 20 3d 20 63 75 72 72 3b 0a 09 09 7d 0a 0a ev = curr;...}..
1ad0: 09 09 63 75 72 72 20 3d 20 70 72 65 76 3b 0a 09 ..curr = prev;..
1ae0: 09 77 68 69 6c 65 20 28 63 75 72 72 29 20 7b 0a .while (curr) {.
1af0: 09 09 09 73 77 69 74 63 68 20 28 63 75 72 72 2d ...switch (curr-
1b00: 3e 74 79 70 65 29 20 7b 0a 09 09 09 09 63 61 73 >type) {.....cas
1b10: 65 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 e FILED_LOG_TYPE
1b20: 5f 4d 45 53 53 41 47 45 3a 0a 09 09 09 09 09 66 _MESSAGE:......f
1b30: 70 72 69 6e 74 66 28 66 70 2c 20 22 25 73 22 2c printf(fp, "%s",
1b40: 20 63 75 72 72 2d 3e 62 75 66 66 65 72 29 3b 0a curr->buffer);.
1b50: 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 ......break;....
1b60: 09 63 61 73 65 20 46 49 4c 45 44 5f 4c 4f 47 5f .case FILED_LOG_
1b70: 54 59 50 45 5f 54 52 41 4e 53 46 45 52 3a 0a 09 TYPE_TRANSFER:..
1b80: 09 09 09 09 73 77 69 74 63 68 20 28 63 75 72 72 ....switch (curr
1b90: 2d 3e 6d 65 74 68 6f 64 29 20 7b 0a 09 09 09 09 ->method) {.....
1ba0: 09 09 63 61 73 65 20 46 49 4c 45 44 5f 52 45 51 ..case FILED_REQ
1bb0: 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 3a UEST_METHOD_GET:
1bc0: 0a 09 09 09 09 09 09 09 6d 65 74 68 6f 64 3d 22 ........method="
1bd0: 47 45 54 22 3b 0a 09 09 09 09 09 09 09 62 72 65 GET";........bre
1be0: 61 6b 3b 0a 09 09 09 09 09 09 63 61 73 65 20 46 ak;.......case F
1bf0: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 ILED_REQUEST_MET
1c00: 48 4f 44 5f 48 45 41 44 3a 0a 09 09 09 09 09 09 HOD_HEAD:.......
1c10: 09 6d 65 74 68 6f 64 3d 22 48 45 41 44 22 3b 0a .method="HEAD";.
1c20: 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 .......break;...
1c30: 09 09 09 09 64 65 66 61 75 6c 74 3a 0a 09 09 09 ....default:....
1c40: 09 09 09 09 6d 65 74 68 6f 64 3d 22 3c 75 6e 6b ....method="<unk
1c50: 6e 6f 77 6e 3e 22 3b 0a 09 09 09 09 09 09 09 62 nown>";........b
1c60: 72 65 61 6b 3b 0a 09 09 09 09 09 7d 0a 0a 09 09 reak;......}....
1c70: 09 09 09 69 66 20 28 63 75 72 72 2d 3e 65 6e 64 ...if (curr->end
1c80: 74 69 6d 65 20 3d 3d 20 28 28 74 69 6d 65 5f 74 time == ((time_t
1c90: 29 20 2d 31 29 29 20 7b 0a 09 09 09 09 09 09 63 ) -1)) {.......c
1ca0: 75 72 72 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 6e urr->endtime = n
1cb0: 6f 77 3b 0a 09 09 09 09 09 7d 0a 0a 09 09 09 09 ow;......}......
1cc0: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 54 52 .fprintf(fp, "TR
1cd0: 41 4e 53 46 45 52 20 4d 45 54 48 4f 44 3d 25 73 ANSFER METHOD=%s
1ce0: 20 50 41 54 48 3d 25 73 20 53 52 43 3d 25 73 3a PATH=%s SRC=%s:
1cf0: 25 69 20 54 49 4d 45 2e 53 54 41 52 54 3d 25 6c %i TIME.START=%l
1d00: 6c 75 20 54 49 4d 45 2e 45 4e 44 3d 25 6c 6c 75 lu TIME.END=%llu
1d10: 20 43 4f 44 45 2e 56 41 4c 55 45 3d 25 75 20 43 CODE.VALUE=%u C
1d20: 4f 44 45 2e 52 45 41 53 4f 4e 3d 25 73 20 52 45 ODE.REASON=%s RE
1d30: 51 55 45 53 54 2e 4f 46 46 53 45 54 3d 25 6c 6c QUEST.OFFSET=%ll
1d40: 75 20 52 45 51 55 45 53 54 2e 4c 45 4e 47 54 48 u REQUEST.LENGTH
1d50: 3d 25 6c 6c 75 20 46 49 4c 45 2e 4c 45 4e 47 54 =%llu FILE.LENGT
1d60: 48 3d 25 6c 6c 75 20 54 52 41 4e 53 46 45 52 2e H=%llu TRANSFER.
1d70: 4c 45 4e 47 54 48 3d 25 6c 6c 75 22 2c 0a 09 09 LENGTH=%llu",...
1d80: 09 09 09 09 6d 65 74 68 6f 64 2c 0a 09 09 09 09 ....method,.....
1d90: 09 09 63 75 72 72 2d 3e 62 75 66 66 65 72 2c 0a ..curr->buffer,.
1da0: 09 09 09 09 09 09 63 75 72 72 2d 3e 69 70 2c 20 ......curr->ip,
1db0: 63 75 72 72 2d 3e 70 6f 72 74 2c 0a 09 09 09 09 curr->port,.....
1dc0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
1dd0: 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 73 74 61 long) curr->sta
1de0: 72 74 74 69 6d 65 2c 0a 09 09 09 09 09 09 28 75 rttime,.......(u
1df0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
1e00: 67 29 20 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 g) curr->endtime
1e10: 2c 0a 09 09 09 09 09 09 63 75 72 72 2d 3e 68 74 ,.......curr->ht
1e20: 74 70 5f 63 6f 64 65 2c 20 63 75 72 72 2d 3e 72 tp_code, curr->r
1e30: 65 61 73 6f 6e 2c 0a 09 09 09 09 09 09 28 75 6e eason,.......(un
1e40: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
1e50: 29 20 63 75 72 72 2d 3e 72 65 71 5f 6f 66 66 73 ) curr->req_offs
1e60: 65 74 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 et,.......(unsig
1e70: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 ned long long) c
1e80: 75 72 72 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 2c urr->req_length,
1e90: 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 .......(unsigned
1ea0: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 long long) curr
1eb0: 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 68 2c 0a 09 ->file_length,..
1ec0: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
1ed0: 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e ong long) curr->
1ee0: 73 65 6e 74 5f 6c 65 6e 67 74 68 0a 09 09 09 09 sent_length.....
1ef0: 09 29 3b 0a 0a 09 09 09 09 09 62 72 65 61 6b 3b .);.......break;
1f00: 0a 09 09 09 7d 0a 09 09 09 66 70 72 69 6e 74 66 ....}....fprintf
1f10: 28 66 70 2c 20 22 20 54 48 52 45 41 44 3d 25 6c (fp, " THREAD=%l
1f20: 6c 75 20 54 49 4d 45 3d 25 6c 6c 75 5c 6e 22 2c lu TIME=%llu\n",
1f30: 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
1f40: 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e ong long) curr->
1f50: 74 68 72 65 61 64 2c 0a 09 09 09 09 28 75 6e 73 thread,.....(uns
1f60: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
1f70: 20 6e 6f 77 0a 09 09 09 29 3b 0a 09 09 09 66 66 now....);....ff
1f80: 6c 75 73 68 28 66 70 29 3b 0a 0a 09 09 09 70 72 lush(fp);.....pr
1f90: 65 76 20 3d 20 63 75 72 72 3b 0a 09 09 09 63 75 ev = curr;....cu
1fa0: 72 72 20 3d 20 63 75 72 72 2d 3e 5f 70 72 65 76 rr = curr->_prev
1fb0: 3b 0a 0a 09 09 09 66 72 65 65 28 70 72 65 76 29 ;.....free(prev)
1fc0: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 ;...}..}...retur
1fd0: 6e 28 4e 55 4c 4c 29 3b 0a 7d 0a 0a 73 74 61 74 n(NULL);.}..stat
1fe0: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 6c 6f ic void filed_lo
1ff0: 67 5f 65 6e 74 72 79 28 73 74 72 75 63 74 20 66 g_entry(struct f
2000: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
2010: 65 6e 74 72 79 29 20 7b 0a 09 65 6e 74 72 79 2d entry) {..entry-
2020: 3e 74 68 72 65 61 64 20 3d 20 70 74 68 72 65 61 >thread = pthrea
2030: 64 5f 73 65 6c 66 28 29 3b 0a 0a 09 70 74 68 72 d_self();...pthr
2040: 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 ead_mutex_lock(&
2050: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
2060: 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 65 6e 74 st_mutex);...ent
2070: 72 79 2d 3e 5f 6e 65 78 74 20 3d 20 66 69 6c 65 ry->_next = file
2080: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a d_log_msg_list;.
2090: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c .filed_log_msg_l
20a0: 69 73 74 20 3d 20 65 6e 74 72 79 3b 0a 0a 09 70 ist = entry;...p
20b0: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c thread_mutex_unl
20c0: 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d ock(&filed_log_m
20d0: 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a sg_list_mutex);.
20e0: 0a 09 70 74 68 72 65 61 64 5f 63 6f 6e 64 5f 73 ..pthread_cond_s
20f0: 69 67 6e 61 6c 28 26 66 69 6c 65 64 5f 6c 6f 67 ignal(&filed_log
2100: 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 79 29 _msg_list_ready)
2110: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 ;...return;.}..s
2120: 74 61 74 69 63 20 73 74 72 75 63 74 20 66 69 6c tatic struct fil
2130: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 66 69 ed_log_entry *fi
2140: 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 69 6e 74 20 led_log_new(int
2150: 69 6e 69 74 69 61 6c 69 7a 65 29 20 7b 0a 09 73 initialize) {..s
2160: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
2170: 65 6e 74 72 79 20 2a 72 65 74 76 61 6c 3b 0a 0a entry *retval;..
2180: 09 72 65 74 76 61 6c 20 3d 20 6d 61 6c 6c 6f 63 .retval = malloc
2190: 28 73 69 7a 65 6f 66 28 2a 72 65 74 76 61 6c 29 (sizeof(*retval)
21a0: 29 3b 0a 0a 09 69 66 20 28 69 6e 69 74 69 61 6c );...if (initial
21b0: 69 7a 65 29 20 7b 0a 09 09 72 65 74 76 61 6c 2d ize) {...retval-
21c0: 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 >buffer[0] = '\0
21d0: 27 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 68 74 74 ';...retval->htt
21e0: 70 5f 63 6f 64 65 20 3d 20 2d 31 3b 0a 09 09 72 p_code = -1;...r
21f0: 65 74 76 61 6c 2d 3e 73 74 61 72 74 74 69 6d 65 etval->starttime
2200: 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e = 0;...retval->
2210: 65 6e 64 74 69 6d 65 20 3d 20 30 3b 0a 09 09 72 endtime = 0;...r
2220: 65 74 76 61 6c 2d 3e 72 65 71 5f 6f 66 66 73 65 etval->req_offse
2230: 74 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d t = 0;...retval-
2240: 3e 72 65 71 5f 6c 65 6e 67 74 68 20 3d 20 30 3b >req_length = 0;
2250: 0a 09 09 72 65 74 76 61 6c 2d 3e 73 65 6e 74 5f ...retval->sent_
2260: 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 length = 0;...re
2270: 74 76 61 6c 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 tval->file_lengt
2280: 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d h = 0;...retval-
2290: 3e 69 70 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 >ip[0] = '\0';..
22a0: 09 72 65 74 76 61 6c 2d 3e 70 6f 72 74 20 3d 20 .retval->port =
22b0: 2d 31 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 6d 65 -1;...retval->me
22c0: 74 68 6f 64 20 3d 20 2d 31 3b 0a 09 7d 0a 0a 09 thod = -1;..}...
22d0: 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a return(retval);.
22e0: 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 }..static void f
22f0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 63 6f 6e iled_log_msg(con
2300: 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 2e 2e st char *fmt, ..
2310: 2e 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c .) {..struct fil
2320: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 65 6e ed_log_entry *en
2330: 74 72 79 3b 0a 09 76 61 5f 6c 69 73 74 20 61 72 try;..va_list ar
2340: 67 73 3b 0a 0a 09 65 6e 74 72 79 20 3d 20 66 69 gs;...entry = fi
2350: 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 30 29 3b 0a led_log_new(0);.
2360: 0a 09 76 61 5f 73 74 61 72 74 28 61 72 67 73 2c ..va_start(args,
2370: 20 66 6d 74 29 3b 0a 0a 09 76 73 6e 70 72 69 6e fmt);...vsnprin
2380: 74 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 65 72 tf(entry->buffer
2390: 2c 20 73 69 7a 65 6f 66 28 65 6e 74 72 79 2d 3e , sizeof(entry->
23a0: 62 75 66 66 65 72 29 2c 20 66 6d 74 2c 20 61 72 buffer), fmt, ar
23b0: 67 73 29 3b 0a 0a 09 76 61 5f 65 6e 64 28 61 72 gs);...va_end(ar
23c0: 67 73 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 74 79 gs);...entry->ty
23d0: 70 65 20 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 pe = FILED_LOG_T
23e0: 59 50 45 5f 4d 45 53 53 41 47 45 3b 0a 0a 09 66 YPE_MESSAGE;...f
23f0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 65 iled_log_entry(e
2400: 6e 74 72 79 29 3b 0a 0a 09 72 65 74 75 72 6e 3b ntry);...return;
2410: 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .}..static const
2420: 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 6c 6f 67 char *filed_log
2430: 5f 69 70 28 73 74 72 75 63 74 20 73 6f 63 6b 61 _ip(struct socka
2440: 64 64 72 20 2a 61 64 64 72 2c 20 63 68 61 72 20 ddr *addr, char
2450: 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f 74 20 *buffer, size_t
2460: 62 75 66 66 65 72 6c 65 6e 29 20 7b 0a 09 73 74 bufferlen) {..st
2470: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e ruct sockaddr_in
2480: 20 2a 61 64 64 72 5f 76 34 3b 0a 09 73 74 72 75 *addr_v4;..stru
2490: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 ct sockaddr_in6
24a0: 2a 61 64 64 72 5f 76 36 3b 0a 09 63 6f 6e 73 74 *addr_v6;..const
24b0: 20 63 68 61 72 20 2a 72 65 74 76 61 6c 20 3d 20 char *retval =
24c0: 4e 55 4c 4c 3b 0a 0a 09 61 64 64 72 5f 76 36 20 NULL;...addr_v6
24d0: 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 = (struct sockad
24e0: 64 72 5f 69 6e 36 20 2a 29 20 61 64 64 72 3b 0a dr_in6 *) addr;.
24f0: 0a 09 73 77 69 74 63 68 20 28 61 64 64 72 5f 76 ..switch (addr_v
2500: 36 2d 3e 73 69 6e 36 5f 66 61 6d 69 6c 79 29 20 6->sin6_family)
2510: 7b 0a 09 09 63 61 73 65 20 41 46 5f 49 4e 45 54 {...case AF_INET
2520: 3a 0a 09 09 09 61 64 64 72 5f 76 34 20 3d 20 28 :....addr_v4 = (
2530: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f struct sockaddr_
2540: 69 6e 20 2a 29 20 61 64 64 72 3b 0a 09 09 09 72 in *) addr;....r
2550: 65 74 76 61 6c 20 3d 20 69 6e 65 74 5f 6e 74 6f etval = inet_nto
2560: 70 28 41 46 5f 49 4e 45 54 2c 20 26 61 64 64 72 p(AF_INET, &addr
2570: 5f 76 34 2d 3e 73 69 6e 5f 61 64 64 72 2c 20 62 _v4->sin_addr, b
2580: 75 66 66 65 72 2c 20 62 75 66 66 65 72 6c 65 6e uffer, bufferlen
2590: 29 3b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 63 );....break;...c
25a0: 61 73 65 20 41 46 5f 49 4e 45 54 36 3a 0a 09 09 ase AF_INET6:...
25b0: 09 72 65 74 76 61 6c 20 3d 20 69 6e 65 74 5f 6e .retval = inet_n
25c0: 74 6f 70 28 41 46 5f 49 4e 45 54 36 2c 20 26 61 top(AF_INET6, &a
25d0: 64 64 72 5f 76 36 2d 3e 73 69 6e 36 5f 61 64 64 ddr_v6->sin6_add
25e0: 72 2c 20 62 75 66 66 65 72 2c 20 62 75 66 66 65 r, buffer, buffe
25f0: 72 6c 65 6e 29 3b 0a 09 09 09 62 72 65 61 6b 3b rlen);....break;
2600: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 ..}...return(ret
2610: 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 val);.}..static
2620: 46 49 4c 45 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f FILE *filed_log_
2630: 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 open(const char
2640: 2a 66 69 6c 65 29 20 7b 0a 09 46 49 4c 45 20 2a *file) {..FILE *
2650: 72 65 74 76 61 6c 3b 0a 0a 09 69 66 20 28 73 74 retval;...if (st
2660: 72 63 6d 70 28 66 69 6c 65 2c 20 22 2d 22 29 20 rcmp(file, "-")
2670: 3d 3d 20 30 29 20 7b 0a 09 09 72 65 74 76 61 6c == 0) {...retval
2680: 20 3d 20 73 74 64 6f 75 74 3b 0a 09 7d 20 65 6c = stdout;..} el
2690: 73 65 20 69 66 20 28 66 69 6c 65 5b 30 5d 20 3d se if (file[0] =
26a0: 3d 20 27 7c 27 29 20 7b 0a 09 09 66 69 6c 65 2b = '|') {...file+
26b0: 2b 3b 0a 09 09 72 65 74 76 61 6c 20 3d 20 70 6f +;...retval = po
26c0: 70 65 6e 28 66 69 6c 65 2c 20 22 77 22 29 3b 0a pen(file, "w");.
26d0: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 72 65 74 76 .} else {...retv
26e0: 61 6c 20 3d 20 66 6f 70 65 6e 28 66 69 6c 65 2c al = fopen(file,
26f0: 20 22 61 2b 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 "a+");..}...ret
2700: 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a urn(retval);.}..
2710: 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 static int filed
2720: 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f _logging_thread_
2730: 69 6e 69 74 28 46 49 4c 45 20 2a 6c 6f 67 66 70 init(FILE *logfp
2740: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 ) {..struct file
2750: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 d_logging_thread
2760: 5f 61 72 67 73 20 2a 61 72 67 73 3b 0a 09 70 74 _args *args;..pt
2770: 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 5f 69 hread_t thread_i
2780: 64 3b 0a 0a 09 61 72 67 73 20 3d 20 6d 61 6c 6c d;...args = mall
2790: 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72 67 73 29 oc(sizeof(*args)
27a0: 29 3b 0a 09 61 72 67 73 2d 3e 66 70 20 3d 20 6c );..args->fp = l
27b0: 6f 67 66 70 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f ogfp;...filed_lo
27c0: 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c g_msg_list = NUL
27d0: 4c 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 L;...pthread_mut
27e0: 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 64 5f 6c ex_init(&filed_l
27f0: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 og_msg_list_mute
2800: 78 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 70 74 68 72 x, NULL);...pthr
2810: 65 61 64 5f 63 72 65 61 74 65 28 26 74 68 72 65 ead_create(&thre
2820: 61 64 5f 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c ad_id, NULL, fil
2830: 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 ed_logging_threa
2840: 64 2c 20 61 72 67 73 29 3b 0a 0a 09 66 69 6c 65 d, args);...file
2850: 64 5f 6c 6f 67 5f 6d 73 67 28 22 53 54 41 52 54 d_log_msg("START
2860: 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b ");...return(0);
2870: 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f .}.#endif../* Fo
2880: 72 6d 61 74 20 74 69 6d 65 20 70 65 72 20 52 46 rmat time per RF
2890: 43 32 36 31 36 20 2a 2f 0a 73 74 61 74 69 63 20 C2616 */.static
28a0: 63 68 61 72 20 2a 66 69 6c 65 64 5f 66 6f 72 6d char *filed_form
28b0: 61 74 5f 74 69 6d 65 28 63 68 61 72 20 2a 62 75 at_time(char *bu
28c0: 66 66 65 72 2c 20 73 69 7a 65 5f 74 20 62 75 66 ffer, size_t buf
28d0: 66 65 72 5f 6c 65 6e 2c 20 63 6f 6e 73 74 20 74 fer_len, const t
28e0: 69 6d 65 5f 74 20 74 69 6d 65 69 6e 66 6f 29 20 ime_t timeinfo)
28f0: 7b 0a 09 73 74 72 75 63 74 20 74 6d 20 74 69 6d {..struct tm tim
2900: 65 69 6e 66 6f 5f 74 6d 2c 20 2a 74 69 6d 65 69 einfo_tm, *timei
2910: 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a 09 74 69 6d 65 nfo_tm_p;...time
2920: 69 6e 66 6f 5f 74 6d 5f 70 20 3d 20 67 6d 74 69 info_tm_p = gmti
2930: 6d 65 5f 72 28 26 74 69 6d 65 69 6e 66 6f 2c 20 me_r(&timeinfo,
2940: 26 74 69 6d 65 69 6e 66 6f 5f 74 6d 29 3b 0a 09 &timeinfo_tm);..
2950: 69 66 20 28 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f if (timeinfo_tm_
2960: 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 p == NULL) {...r
2970: 65 74 75 72 6e 28 22 75 6e 6b 6e 6f 77 6e 22 29 eturn("unknown")
2980: 3b 0a 09 7d 0a 0a 09 62 75 66 66 65 72 5b 62 75 ;..}...buffer[bu
2990: 66 66 65 72 5f 6c 65 6e 20 2d 20 31 5d 20 3d 20 ffer_len - 1] =
29a0: 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 5f 6c 65 '\0';..buffer_le
29b0: 6e 20 3d 20 73 74 72 66 74 69 6d 65 28 62 75 66 n = strftime(buf
29c0: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 20 fer, buffer_len
29d0: 2d 20 31 2c 20 22 25 61 2c 20 25 64 20 25 62 20 - 1, "%a, %d %b
29e0: 25 59 20 25 48 3a 25 4d 3a 25 53 20 47 4d 54 22 %Y %H:%M:%S GMT"
29f0: 2c 20 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 29 , timeinfo_tm_p)
2a00: 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75 66 66 65 ;...return(buffe
2a10: 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 61 73 68 20 2a r);.}../* hash *
2a20: 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 /.static unsigne
2a30: 64 20 69 6e 74 20 66 69 6c 65 64 5f 68 61 73 68 d int filed_hash
2a40: 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
2a50: 63 68 61 72 20 2a 76 61 6c 75 65 2c 20 75 6e 73 char *value, uns
2a60: 69 67 6e 65 64 20 69 6e 74 20 6d 6f 64 75 6c 75 igned int modulu
2a70: 73 29 20 7b 0a 09 75 6e 73 69 67 6e 65 64 20 63 s) {..unsigned c
2a80: 68 61 72 20 63 75 72 72 2c 20 70 72 65 76 3b 0a har curr, prev;.
2a90: 09 69 6e 74 20 64 69 66 66 3b 0a 09 75 6e 73 69 .int diff;..unsi
2aa0: 67 6e 65 64 20 69 6e 74 20 72 65 74 76 61 6c 3b gned int retval;
2ab0: 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d 6f 64 75 ...retval = modu
2ac0: 6c 75 73 20 2d 20 31 3b 0a 09 70 72 65 76 20 3d lus - 1;..prev =
2ad0: 20 6d 6f 64 75 6c 75 73 20 25 20 32 35 35 3b 0a modulus % 255;.
2ae0: 0a 09 77 68 69 6c 65 20 28 28 63 75 72 72 20 3d ..while ((curr =
2af0: 20 2a 76 61 6c 75 65 29 29 20 7b 0a 09 09 69 66 *value)) {...if
2b00: 20 28 63 75 72 72 20 3c 20 33 32 29 20 7b 0a 09 (curr < 32) {..
2b10: 09 09 63 75 72 72 20 3d 20 32 35 35 20 2d 20 63 ..curr = 255 - c
2b20: 75 72 72 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a urr;...} else {.
2b30: 09 09 09 63 75 72 72 20 2d 3d 20 33 32 3b 0a 09 ...curr -= 32;..
2b40: 09 7d 0a 0a 09 09 69 66 20 28 70 72 65 76 20 3c .}....if (prev <
2b50: 20 63 75 72 72 29 20 7b 0a 09 09 09 64 69 66 66 curr) {....diff
2b60: 20 3d 20 63 75 72 72 20 2d 20 70 72 65 76 3b 0a = curr - prev;.
2b70: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 64 69 ..} else {....di
2b80: 66 66 20 3d 20 70 72 65 76 20 2d 20 63 75 72 72 ff = prev - curr
2b90: 3b 0a 09 09 7d 0a 0a 09 09 70 72 65 76 20 3d 20 ;...}....prev =
2ba0: 63 75 72 72 3b 0a 0a 09 09 72 65 74 76 61 6c 20 curr;....retval
2bb0: 3c 3c 3d 20 33 3b 0a 09 09 72 65 74 76 61 6c 20 <<= 3;...retval
2bc0: 26 3d 20 30 78 46 46 46 46 46 46 46 46 4c 55 3b &= 0xFFFFFFFFLU;
2bd0: 0a 09 09 72 65 74 76 61 6c 20 5e 3d 20 64 69 66 ...retval ^= dif
2be0: 66 3b 0a 0a 09 09 76 61 6c 75 65 2b 2b 3b 0a 09 f;....value++;..
2bf0: 7d 0a 0a 09 72 65 74 76 61 6c 20 3d 20 72 65 74 }...retval = ret
2c00: 76 61 6c 20 25 20 6d 6f 64 75 6c 75 73 3b 0a 0a val % modulus;..
2c10: 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b .return(retval);
2c20: 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 20 61 20 6d 69 .}../* Find a mi
2c30: 6d 65 2d 74 79 70 65 20 62 61 73 65 64 20 6f 6e me-type based on
2c40: 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 2a 2f the filename */
2c50: 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 .static const ch
2c60: 61 72 20 2a 66 69 6c 65 64 5f 64 65 74 65 72 6d ar *filed_determ
2c70: 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 63 6f 6e ine_mimetype(con
2c80: 73 74 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b st char *path) {
2c90: 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 3b ..const char *p;
2ca0: 0a 0a 09 70 20 3d 20 73 74 72 72 63 68 72 28 70 ...p = strrchr(p
2cb0: 61 74 68 2c 20 27 2e 27 29 3b 0a 09 69 66 20 28 ath, '.');..if (
2cc0: 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 p == NULL) {...r
2cd0: 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 eturn(FILED_DEFA
2ce0: 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 ULT_TYPE);..}...
2cf0: 70 2b 2b 3b 0a 09 69 66 20 28 2a 70 20 3d 3d 20 p++;..if (*p ==
2d00: 27 5c 30 27 29 20 7b 0a 09 09 72 65 74 75 72 6e '\0') {...return
2d10: 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 (FILED_DEFAULT_T
2d20: 59 50 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 YPE);..}...filed
2d30: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 _log_msg_debug("
2d40: 4c 6f 6f 6b 69 6e 67 20 75 70 20 4d 49 4d 45 20 Looking up MIME
2d50: 74 79 70 65 20 66 6f 72 20 25 73 20 28 68 61 73 type for %s (has
2d60: 68 20 3d 20 25 6c 6c 75 29 22 2c 20 70 2c 20 28 h = %llu)", p, (
2d70: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
2d80: 6e 67 29 20 66 69 6c 65 64 5f 68 61 73 68 28 28 ng) filed_hash((
2d90: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
2da0: 68 61 72 20 2a 29 20 70 2c 20 31 36 37 37 37 32 har *) p, 167772
2db0: 35 39 29 29 3b 0a 0a 23 69 6e 63 6c 75 64 65 20 59));..#include
2dc0: 22 66 69 6c 65 64 2d 6d 69 6d 65 2d 74 79 70 65 "filed-mime-type
2dd0: 73 2e 68 22 0a 0a 09 72 65 74 75 72 6e 28 46 49 s.h"...return(FI
2de0: 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 LED_DEFAULT_TYPE
2df0: 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 6e 65 72 61 74 );.}../* Generat
2e00: 65 20 61 20 75 6e 69 71 75 65 20 69 64 65 6e 74 e a unique ident
2e10: 69 66 69 65 72 20 2a 2f 0a 73 74 61 74 69 63 20 ifier */.static
2e20: 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 6e 65 72 void filed_gener
2e30: 61 74 65 5f 65 74 61 67 28 63 68 61 72 20 2a 65 ate_etag(char *e
2e40: 74 61 67 2c 20 73 69 7a 65 5f 74 20 6c 65 6e 67 tag, size_t leng
2e50: 74 68 29 20 7b 0a 09 73 6e 70 72 69 6e 74 66 28 th) {..snprintf(
2e60: 65 74 61 67 2c 20 6c 65 6e 67 74 68 2c 20 22 25 etag, length, "%
2e70: 6c 6c 78 2d 25 6c 6c 78 25 6c 6c 78 25 6c 6c 78 llx-%llx%llx%llx
2e80: 25 6c 6c 78 22 2c 0a 09 09 28 75 6e 73 69 67 6e %llx",...(unsign
2e90: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 74 69 ed long long) ti
2ea0: 6d 65 28 4e 55 4c 4c 29 2c 0a 09 09 28 75 6e 73 me(NULL),...(uns
2eb0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
2ec0: 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09 28 75 6e random(),...(un
2ed0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
2ee0: 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09 28 75 ) random(),...(u
2ef0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
2f00: 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09 28 g) random(),...(
2f10: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
2f20: 6e 67 29 20 72 61 6e 64 6f 6d 28 29 0a 09 29 3b ng) random()..);
2f30: 0a 7d 0a 0a 2f 2a 20 4f 70 65 6e 20 61 20 66 69 .}../* Open a fi
2f40: 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 66 69 le and return fi
2f50: 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a le information *
2f60: 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
2f70: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a filed_fileinfo *
2f80: 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 28 filed_open_file(
2f90: 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 const char *path
2fa0: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 , struct filed_f
2fb0: 69 6c 65 69 6e 66 6f 20 2a 62 75 66 66 65 72 29 ileinfo *buffer)
2fc0: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 {..struct filed
2fd0: 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 63 68 65 _fileinfo *cache
2fe0: 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 ;..unsigned int
2ff0: 63 61 63 68 65 5f 69 64 78 3b 0a 09 6f 66 66 5f cache_idx;..off_
3000: 74 20 6c 65 6e 3b 0a 09 69 6e 74 20 66 64 3b 0a t len;..int fd;.
3010: 0a 09 63 61 63 68 65 5f 69 64 78 20 3d 20 66 69 ..cache_idx = fi
3020: 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e 73 74 20 led_hash((const
3030: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
3040: 20 70 61 74 68 2c 20 66 69 6c 65 64 5f 66 69 6c path, filed_fil
3050: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 einfo_fdcache_si
3060: 7a 65 29 3b 0a 0a 09 63 61 63 68 65 20 3d 20 26 ze);...cache = &
3070: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
3080: 64 63 61 63 68 65 5b 63 61 63 68 65 5f 69 64 78 dcache[cache_idx
3090: 5d 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d ];...filed_log_m
30a0: 73 67 5f 64 65 62 75 67 28 22 4c 6f 63 6b 69 6e sg_debug("Lockin
30b0: 67 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a g mutex for idx:
30c0: 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 %lu", (unsigned
30d0: 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 long) cache_idx
30e0: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 );...pthread_mut
30f0: 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e ex_lock(&cache->
3100: 6d 75 74 65 78 29 3b 0a 0a 09 66 69 6c 65 64 5f mutex);...filed_
3110: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 log_msg_debug("C
3120: 6f 6d 70 6c 65 74 65 64 20 6c 6f 63 6b 69 6e 67 ompleted locking
3130: 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a 20 mutex for idx:
3140: 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 %lu", (unsigned
3150: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 29 long) cache_idx)
3160: 3b 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 28 70 ;...if (strcmp(p
3170: 61 74 68 2c 20 63 61 63 68 65 2d 3e 70 61 74 68 ath, cache->path
3180: 29 20 21 3d 20 30 29 20 7b 0a 09 09 66 69 6c 65 ) != 0) {...file
3190: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 d_log_msg_debug(
31a0: 22 43 61 63 68 65 20 6d 69 73 73 20 66 6f 72 20 "Cache miss for
31b0: 69 64 78 3a 20 25 6c 75 3a 20 4f 4c 44 20 5c 22 idx: %lu: OLD \"
31c0: 25 73 5c 22 2c 20 4e 45 57 20 5c 22 25 73 5c 22 %s\", NEW \"%s\"
31d0: 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ", (unsigned lon
31e0: 67 29 20 63 61 63 68 65 5f 69 64 78 2c 20 63 61 g) cache_idx, ca
31f0: 63 68 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 che->path, path)
3200: 3b 0a 0a 09 09 66 64 20 3d 20 6f 70 65 6e 28 70 ;....fd = open(p
3210: 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 20 7c 20 ath, O_RDONLY |
3220: 4f 5f 4c 41 52 47 45 46 49 4c 45 29 3b 0a 09 09 O_LARGEFILE);...
3230: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 if (fd < 0) {...
3240: 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 .pthread_mutex_u
3250: 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 nlock(&cache->mu
3260: 74 65 78 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e tex);.....return
3270: 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 69 (NULL);...}....i
3280: 66 20 28 63 61 63 68 65 2d 3e 66 64 20 3e 3d 20 f (cache->fd >=
3290: 30 29 20 7b 0a 09 09 09 63 6c 6f 73 65 28 63 61 0) {....close(ca
32a0: 63 68 65 2d 3e 66 64 29 3b 0a 09 09 7d 0a 0a 09 che->fd);...}...
32b0: 09 6c 65 6e 20 3d 20 6c 73 65 65 6b 28 66 64 2c .len = lseek(fd,
32c0: 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29 3b 0a 09 0, SEEK_END);..
32d0: 09 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 53 45 .lseek(fd, 0, SE
32e0: 45 4b 5f 53 45 54 29 3b 0a 0a 09 09 63 61 63 68 EK_SET);....cach
32f0: 65 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 09 63 61 e->fd = fd;...ca
3300: 63 68 65 2d 3e 6c 65 6e 20 3d 20 6c 65 6e 3b 0a che->len = len;.
3310: 09 09 73 74 72 63 70 79 28 63 61 63 68 65 2d 3e ..strcpy(cache->
3320: 70 61 74 68 2c 20 70 61 74 68 29 3b 0a 09 09 63 path, path);...c
3330: 61 63 68 65 2d 3e 74 79 70 65 20 3d 20 66 69 6c ache->type = fil
3340: 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f 6d 69 6d ed_determine_mim
3350: 65 74 79 70 65 28 70 61 74 68 29 3b 0a 09 09 66 etype(path);...f
3360: 69 6c 65 64 5f 67 65 6e 65 72 61 74 65 5f 65 74 iled_generate_et
3370: 61 67 28 63 61 63 68 65 2d 3e 65 74 61 67 2c 20 ag(cache->etag,
3380: 73 69 7a 65 6f 66 28 63 61 63 68 65 2d 3e 65 74 sizeof(cache->et
3390: 61 67 29 29 3b 0a 0a 09 09 2f 2a 20 58 58 58 3a ag));..../* XXX:
33a0: 54 4f 44 4f 3a 20 44 65 74 65 72 6d 69 6e 65 20 TODO: Determine
33b0: 2a 2f 0a 09 09 63 61 63 68 65 2d 3e 6c 61 73 74 */...cache->last
33c0: 6d 6f 64 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d mod = filed_form
33d0: 61 74 5f 74 69 6d 65 28 63 61 63 68 65 2d 3e 6c at_time(cache->l
33e0: 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 astmod_b, sizeof
33f0: 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f (cache->lastmod_
3400: 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 20 2d b), time(NULL) -
3410: 20 33 30 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 30);..} else {.
3420: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f ..filed_log_msg_
3430: 64 65 62 75 67 28 22 43 61 63 68 65 20 68 69 74 debug("Cache hit
3440: 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20 50 for idx: %lu: P
3450: 41 54 48 20 5c 22 25 73 5c 22 22 2c 20 28 75 6e ATH \"%s\"", (un
3460: 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 signed long) cac
3470: 68 65 5f 69 64 78 2c 20 70 61 74 68 29 3b 0a 09 he_idx, path);..
3480: 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 57 65 20 68 61 }.../*.. * We ha
3490: 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 64 75 70 ve to make a dup
34a0: 6c 69 63 61 74 65 20 46 44 2c 20 62 65 63 61 75 licate FD, becau
34b0: 73 65 20 6f 6e 63 65 20 77 65 20 72 65 6c 65 61 se once we relea
34c0: 73 65 20 74 68 65 20 63 61 63 68 65 0a 09 20 2a se the cache.. *
34d0: 20 6d 75 74 65 78 2c 20 74 68 65 20 66 69 6c 65 mutex, the file
34e0: 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79 20 descriptor may
34f0: 62 65 20 63 6c 6f 73 65 64 0a 09 20 2a 2f 0a 09 be closed.. */..
3500: 66 64 20 3d 20 64 75 70 28 63 61 63 68 65 2d 3e fd = dup(cache->
3510: 66 64 29 3b 0a 09 69 66 20 28 66 64 20 3c 20 30 fd);..if (fd < 0
3520: 29 20 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 ) {...pthread_mu
3530: 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 tex_unlock(&cach
3540: 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 72 65 e->mutex);....re
3550: 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a turn(NULL);..}..
3560: 09 62 75 66 66 65 72 2d 3e 66 64 20 3d 20 66 64 .buffer->fd = fd
3570: 3b 0a 09 62 75 66 66 65 72 2d 3e 6c 65 6e 20 3d ;..buffer->len =
3580: 20 63 61 63 68 65 2d 3e 6c 65 6e 3b 0a 09 62 75 cache->len;..bu
3590: 66 66 65 72 2d 3e 74 79 70 65 20 3d 20 63 61 63 ffer->type = cac
35a0: 68 65 2d 3e 74 79 70 65 3b 0a 09 6d 65 6d 63 70 he->type;..memcp
35b0: 79 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f y(buffer->lastmo
35c0: 64 5f 62 2c 20 63 61 63 68 65 2d 3e 6c 61 73 74 d_b, cache->last
35d0: 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 28 62 75 mod_b, sizeof(bu
35e0: 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 ffer->lastmod_b)
35f0: 29 3b 0a 09 6d 65 6d 63 70 79 28 62 75 66 66 65 );..memcpy(buffe
3600: 72 2d 3e 65 74 61 67 2c 20 63 61 63 68 65 2d 3e r->etag, cache->
3610: 65 74 61 67 2c 20 73 69 7a 65 6f 66 28 62 75 66 etag, sizeof(buf
3620: 66 65 72 2d 3e 65 74 61 67 29 29 3b 0a 09 62 75 fer->etag));..bu
3630: 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20 ffer->lastmod =
3640: 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 5f buffer->lastmod_
3650: 62 20 2b 20 28 63 61 63 68 65 2d 3e 6c 61 73 74 b + (cache->last
3660: 6d 6f 64 20 2d 20 63 61 63 68 65 2d 3e 6c 61 73 mod - cache->las
3670: 74 6d 6f 64 5f 62 29 3b 0a 0a 09 70 74 68 72 65 tmod_b);...pthre
3680: 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 ad_mutex_unlock(
3690: 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a &cache->mutex);.
36a0: 0a 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 29 ..return(buffer)
36b0: 3b 0a 7d 0a 0a 2f 2a 20 50 72 6f 63 65 73 73 20 ;.}../* Process
36c0: 61 6e 20 48 54 54 50 20 72 65 71 75 65 73 74 20 an HTTP request
36d0: 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 70 and return the p
36e0: 61 74 68 20 72 65 71 75 65 73 74 65 64 20 2a 2f ath requested */
36f0: 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 66 .static struct f
3700: 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 iled_http_reques
3710: 74 20 2a 66 69 6c 65 64 5f 67 65 74 5f 68 74 74 t *filed_get_htt
3720: 70 5f 72 65 71 75 65 73 74 28 46 49 4c 45 20 2a p_request(FILE *
3730: 66 70 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 fp, struct filed
3740: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 62 _http_request *b
3750: 75 66 66 65 72 5f 73 74 2c 20 73 74 72 75 63 74 uffer_st, struct
3760: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a filed_options *
3770: 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 63 68 61 72 options) {..char
3780: 20 2a 6d 65 74 68 6f 64 2c 20 2a 70 61 74 68 3b *method, *path;
3790: 0a 09 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 ..char *buffer,
37a0: 2a 77 6f 72 6b 62 75 66 66 65 72 2c 20 2a 77 6f *workbuffer, *wo
37b0: 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 09 rkbuffer_next;..
37c0: 63 68 61 72 20 2a 66 67 65 74 73 5f 72 65 74 3b char *fgets_ret;
37d0: 0a 09 73 69 7a 65 5f 74 20 62 75 66 66 65 72 5f ..size_t buffer_
37e0: 6c 65 6e 2c 20 70 61 74 68 5f 6c 65 6e 3b 0a 09 len, path_len;..
37f0: 6f 66 66 5f 74 20 72 61 6e 67 65 5f 73 74 61 72 off_t range_star
3800: 74 2c 20 72 61 6e 67 65 5f 65 6e 64 2c 20 72 61 t, range_end, ra
3810: 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 09 69 6e 74 nge_length;..int
3820: 20 72 61 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a range_request;.
3830: 09 69 6e 74 20 73 6e 70 72 69 6e 74 66 5f 72 65 .int snprintf_re
3840: 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 2f 2a 20 t;..int i;.../*
3850: 53 65 74 20 74 6f 20 64 65 66 61 75 6c 74 20 76 Set to default v
3860: 61 6c 75 65 73 20 2a 2f 0a 09 72 61 6e 67 65 5f alues */..range_
3870: 73 74 61 72 74 20 3d 20 30 3b 0a 09 72 61 6e 67 start = 0;..rang
3880: 65 5f 65 6e 64 20 20 20 3d 20 30 3b 0a 09 72 61 e_end = 0;..ra
3890: 6e 67 65 5f 72 65 71 75 65 73 74 20 3d 20 30 3b nge_request = 0;
38a0: 0a 09 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d ..range_length =
38b0: 20 2d 31 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d -1;..buffer_st-
38c0: 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 >headers.host.pr
38d0: 65 73 65 6e 74 20 3d 20 30 3b 0a 0a 09 62 75 66 esent = 0;...buf
38e0: 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d fer = buffer_st-
38f0: 3e 74 6d 70 62 75 66 3b 0a 09 62 75 66 66 65 72 >tmpbuf;..buffer
3900: 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 _len = sizeof(bu
3910: 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 ffer_st->tmpbuf)
3920: 3b 0a 0a 09 66 67 65 74 73 5f 72 65 74 20 3d 20 ;...fgets_ret =
3930: 66 67 65 74 73 28 62 75 66 66 65 72 2c 20 62 75 fgets(buffer, bu
3940: 66 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 ffer_len, fp);..
3950: 69 66 20 28 66 67 65 74 73 5f 72 65 74 20 3d 3d if (fgets_ret ==
3960: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 NULL) {...retur
3970: 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 6d 65 n(NULL);..}...me
3980: 74 68 6f 64 20 3d 20 62 75 66 66 65 72 3b 0a 0a thod = buffer;..
3990: 09 62 75 66 66 65 72 20 3d 20 73 74 72 63 68 72 .buffer = strchr
39a0: 28 62 75 66 66 65 72 2c 20 27 20 27 29 3b 0a 09 (buffer, ' ');..
39b0: 69 66 20 28 62 75 66 66 65 72 20 3d 3d 20 4e 55 if (buffer == NU
39c0: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 4e LL) {...return(N
39d0: 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 2a 62 75 66 66 ULL);..}...*buff
39e0: 65 72 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 er = '\0';..buff
39f0: 65 72 2b 2b 3b 0a 0a 09 70 61 74 68 20 3d 20 62 er++;...path = b
3a00: 75 66 66 65 72 3b 0a 0a 09 2f 2a 20 54 65 72 6d uffer;.../* Term
3a10: 69 6e 61 74 65 20 70 61 74 68 20 63 6f 6d 70 6f inate path compo
3a20: 6e 65 6e 74 20 2a 2f 0a 09 62 75 66 66 65 72 20 nent */..buffer
3a30: 3d 20 73 74 72 70 62 72 6b 28 70 61 74 68 2c 20 = strpbrk(path,
3a40: 22 5c 72 5c 6e 20 22 29 3b 0a 09 69 66 20 28 62 "\r\n ");..if (b
3a50: 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c 29 20 7b uffer != NULL) {
3a60: 0a 09 09 2a 62 75 66 66 65 72 20 3d 20 27 5c 30 ...*buffer = '\0
3a70: 27 3b 0a 09 09 62 75 66 66 65 72 2b 2b 3b 0a 09 ';...buffer++;..
3a80: 7d 0a 0a 09 2f 2a 20 57 65 20 6f 6e 6c 79 20 68 }.../* We only h
3a90: 61 6e 64 6c 65 20 74 68 65 20 22 47 45 54 22 20 andle the "GET"
3aa0: 61 6e 64 20 22 48 45 41 44 27 20 6d 65 74 68 6f and "HEAD' metho
3ab0: 64 73 20 2a 2f 0a 09 69 66 20 28 73 74 72 63 61 ds */..if (strca
3ac0: 73 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 22 68 secmp(method, "h
3ad0: 65 61 64 22 29 20 21 3d 20 30 29 20 7b 0a 09 09 ead") != 0) {...
3ae0: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 28 6d if (strcasecmp(m
3af0: 65 74 68 6f 64 2c 20 22 67 65 74 22 29 20 21 3d ethod, "get") !=
3b00: 20 30 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 0) {....return(
3b10: 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a NULL);...}..../*
3b20: 20 47 45 54 20 72 65 71 75 65 73 74 20 2a 2f 0a GET request */.
3b30: 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 6d 65 74 ..buffer_st->met
3b40: 68 6f 64 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 hod = FILED_REQU
3b50: 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 3b 0a EST_METHOD_GET;.
3b60: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 2f 2a 20 48 .} else {.../* H
3b70: 45 41 44 20 72 65 71 75 65 73 74 20 2a 2f 0a 09 EAD request */..
3b80: 09 62 75 66 66 65 72 5f 73 74 2d 3e 6d 65 74 68 .buffer_st->meth
3b90: 6f 64 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 od = FILED_REQUE
3ba0: 53 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 3b 0a ST_METHOD_HEAD;.
3bb0: 09 7d 0a 0a 09 2f 2a 20 4e 6f 74 65 20 70 61 74 .}.../* Note pat
3bc0: 68 20 2a 2f 0a 09 70 61 74 68 5f 6c 65 6e 20 3d h */..path_len =
3bd0: 20 73 74 72 6c 65 6e 28 70 61 74 68 29 3b 0a 09 strlen(path);..
3be0: 6d 65 6d 63 70 79 28 62 75 66 66 65 72 5f 73 74 memcpy(buffer_st
3bf0: 2d 3e 70 61 74 68 2c 20 70 61 74 68 2c 20 70 61 ->path, path, pa
3c00: 74 68 5f 6c 65 6e 20 2b 20 31 29 3b 0a 0a 09 2f th_len + 1);.../
3c10: 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 79 70 65 * Determine type
3c20: 20 6f 66 20 72 65 71 75 65 73 74 20 66 72 6f 6d of request from
3c30: 20 70 61 74 68 20 2a 2f 0a 09 69 66 20 28 70 61 path */..if (pa
3c40: 74 68 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 th_len == 0) {..
3c50: 09 62 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 .buffer_st->type
3c60: 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 = FILED_REQUEST
3c70: 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 59 3b _TYPE_DIRECTORY;
3c80: 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 69 66 20 ..} else {...if
3c90: 28 70 61 74 68 5b 70 61 74 68 5f 6c 65 6e 20 2d (path[path_len -
3ca0: 20 31 5d 20 3d 3d 20 27 2f 27 29 20 7b 0a 09 09 1] == '/') {...
3cb0: 09 62 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 .buffer_st->type
3cc0: 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 = FILED_REQUEST
3cd0: 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 59 3b _TYPE_DIRECTORY;
3ce0: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 62 ...} else {....b
3cf0: 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d uffer_st->type =
3d00: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 FILED_REQUEST_T
3d10: 59 50 45 5f 4f 54 48 45 52 3b 0a 09 09 7d 0a 09 YPE_OTHER;...}..
3d20: 7d 0a 0a 09 2f 2a 20 52 65 73 65 74 20 62 75 66 }.../* Reset buf
3d30: 66 65 72 20 66 6f 72 20 6c 61 74 65 72 20 75 73 fer for later us
3d40: 65 20 2a 2f 0a 09 62 75 66 66 65 72 20 3d 20 62 e */..buffer = b
3d50: 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 uffer_st->tmpbuf
3d60: 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 ;...for (i = 0;
3d70: 69 20 3c 20 31 30 30 3b 20 69 2b 2b 29 20 7b 0a i < 100; i++) {.
3d80: 09 09 66 67 65 74 73 5f 72 65 74 20 3d 20 66 67 ..fgets_ret = fg
3d90: 65 74 73 28 62 75 66 66 65 72 2c 20 62 75 66 66 ets(buffer, buff
3da0: 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 09 69 er_len, fp);...i
3db0: 66 20 28 66 67 65 74 73 5f 72 65 74 20 3d 3d 20 f (fgets_ret ==
3dc0: 4e 55 4c 4c 29 20 7b 0a 09 09 09 62 72 65 61 6b NULL) {....break
3dd0: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 73 74 72 ;...}....if (str
3de0: 6e 63 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c ncasecmp(buffer,
3df0: 20 22 52 61 6e 67 65 3a 20 22 2c 20 37 29 20 3d "Range: ", 7) =
3e00: 3d 20 30 29 20 7b 0a 09 09 09 77 6f 72 6b 62 75 = 0) {....workbu
3e10: 66 66 65 72 20 3d 20 62 75 66 66 65 72 20 2b 20 ffer = buffer +
3e20: 37 3b 0a 0a 09 09 09 69 66 20 28 73 74 72 6e 63 7;.....if (strnc
3e30: 61 73 65 63 6d 70 28 77 6f 72 6b 62 75 66 66 65 asecmp(workbuffe
3e40: 72 2c 20 22 62 79 74 65 73 3d 22 2c 20 36 29 20 r, "bytes=", 6)
3e50: 3d 3d 20 30 29 20 7b 0a 09 09 09 09 77 6f 72 6b == 0) {.....work
3e60: 62 75 66 66 65 72 20 2b 3d 20 36 3b 0a 0a 09 09 buffer += 6;....
3e70: 09 09 72 61 6e 67 65 5f 72 65 71 75 65 73 74 20 ..range_request
3e80: 3d 20 31 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f = 1;......range_
3e90: 73 74 61 72 74 20 3d 20 73 74 72 74 6f 75 6c 6c start = strtoull
3ea0: 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 26 77 6f (workbuffer, &wo
3eb0: 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 2c 20 31 rkbuffer_next, 1
3ec0: 30 29 3b 0a 0a 09 09 09 09 77 6f 72 6b 62 75 66 0);......workbuf
3ed0: 66 65 72 20 3d 20 77 6f 72 6b 62 75 66 66 65 72 fer = workbuffer
3ee0: 5f 6e 65 78 74 3b 0a 0a 09 09 09 09 69 66 20 28 _next;......if (
3ef0: 2a 77 6f 72 6b 62 75 66 66 65 72 20 3d 3d 20 27 *workbuffer == '
3f00: 2d 27 29 20 7b 0a 09 09 09 09 09 77 6f 72 6b 62 -') {......workb
3f10: 75 66 66 65 72 2b 2b 3b 0a 0a 09 09 09 09 09 69 uffer++;.......i
3f20: 66 20 28 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 f (*workbuffer !
3f30: 3d 20 27 5c 72 27 20 26 26 20 2a 77 6f 72 6b 62 = '\r' && *workb
3f40: 75 66 66 65 72 20 21 3d 20 27 5c 6e 27 29 20 7b uffer != '\n') {
3f50: 0a 09 09 09 09 09 09 72 61 6e 67 65 5f 65 6e 64 .......range_end
3f60: 20 3d 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b = strtoull(work
3f70: 62 75 66 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 buffer, &workbuf
3f80: 66 65 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 09 fer_next, 10);..
3f90: 09 09 09 09 7d 0a 09 09 09 09 7d 0a 09 09 09 7d ....}.....}....}
3fa0: 0a 09 09 7d 20 65 6c 73 65 20 69 66 20 28 73 74 ...} else if (st
3fb0: 72 6e 63 61 73 65 63 6d 70 28 62 75 66 66 65 72 rncasecmp(buffer
3fc0: 2c 20 22 48 6f 73 74 3a 20 22 2c 20 35 29 20 3d , "Host: ", 5) =
3fd0: 3d 20 30 29 20 7b 0a 09 09 09 62 75 66 66 65 72 = 0) {....buffer
3fe0: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 _st->headers.hos
3ff0: 74 2e 70 72 65 73 65 6e 74 20 3d 20 31 3b 0a 0a t.present = 1;..
4000: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 ...workbuffer =
4010: 73 74 72 70 62 72 6b 28 62 75 66 66 65 72 20 2b strpbrk(buffer +
4020: 20 35 2c 20 22 5c 72 5c 6e 3a 22 29 3b 0a 09 09 5, "\r\n:");...
4030: 09 69 66 20 28 77 6f 72 6b 62 75 66 66 65 72 20 .if (workbuffer
4040: 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 09 2a != NULL) {.....*
4050: 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 27 5c 30 workbuffer = '\0
4060: 27 3b 0a 09 09 09 7d 0a 0a 09 09 09 77 6f 72 6b ';....}.....work
4070: 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 20 buffer = buffer
4080: 2b 20 35 3b 0a 09 09 09 77 68 69 6c 65 20 28 2a + 5;....while (*
4090: 77 6f 72 6b 62 75 66 66 65 72 20 3d 3d 20 27 20 workbuffer == '
40a0: 27 29 20 7b 0a 09 09 09 09 77 6f 72 6b 62 75 66 ') {.....workbuf
40b0: 66 65 72 2b 2b 3b 0a 09 09 09 7d 0a 0a 09 09 09 fer++;....}.....
40c0: 73 74 72 63 70 79 28 62 75 66 66 65 72 5f 73 74 strcpy(buffer_st
40d0: 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 68 ->headers.host.h
40e0: 6f 73 74 2c 20 77 6f 72 6b 62 75 66 66 65 72 29 ost, workbuffer)
40f0: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 6d 65 6d ;...}....if (mem
4100: 63 6d 70 28 62 75 66 66 65 72 2c 20 22 5c 72 5c cmp(buffer, "\r\
4110: 6e 22 2c 20 32 29 20 3d 3d 20 30 29 20 7b 0a 09 n", 2) == 0) {..
4120: 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a ..break;...}..}.
4130: 0a 09 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 72 ../* Determine r
4140: 61 6e 67 65 20 2a 2f 0a 09 69 66 20 28 72 61 6e ange */..if (ran
4150: 67 65 5f 65 6e 64 20 21 3d 20 30 29 20 7b 0a 09 ge_end != 0) {..
4160: 09 69 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 3c .if (range_end <
4170: 3d 20 72 61 6e 67 65 5f 73 74 61 72 74 29 20 7b = range_start) {
4180: 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 ....return(NULL)
4190: 3b 0a 09 09 7d 0a 0a 09 09 72 61 6e 67 65 5f 6c ;...}....range_l
41a0: 65 6e 67 74 68 20 3d 20 72 61 6e 67 65 5f 65 6e ength = range_en
41b0: 64 20 2d 20 72 61 6e 67 65 5f 73 74 61 72 74 3b d - range_start;
41c0: 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 ....filed_log_ms
41d0: 67 5f 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 g_debug("Computi
41e0: 6e 67 20 6c 65 6e 67 74 68 20 70 61 72 61 6d 65 ng length parame
41f0: 74 65 72 3a 20 25 6c 6c 75 20 3d 20 25 6c 6c 75 ter: %llu = %llu
4200: 20 2d 20 25 6c 6c 75 22 2c 0a 09 09 09 28 75 6e - %llu",....(un
4210: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
4220: 29 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 2c 0a ) range_length,.
4230: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
4240: 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 65 6e g long) range_en
4250: 64 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 d,....(unsigned
4260: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 long long) range
4270: 5f 73 74 61 72 74 0a 09 09 29 3b 0a 09 7d 0a 0a _start...);..}..
4280: 09 2f 2a 20 46 69 6c 6c 20 75 70 20 73 74 72 75 ./* Fill up stru
4290: 63 74 75 72 65 20 74 6f 20 72 65 74 75 72 6e 20 cture to return
42a0: 2a 2f 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 */..buffer_st->h
42b0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 eaders.range.pre
42c0: 73 65 6e 74 20 3d 20 72 61 6e 67 65 5f 72 65 71 sent = range_req
42d0: 75 65 73 74 3b 0a 09 62 75 66 66 65 72 5f 73 74 uest;..buffer_st
42e0: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
42f0: 6f 66 66 73 65 74 20 20 3d 20 72 61 6e 67 65 5f offset = range_
4300: 73 74 61 72 74 3b 0a 09 62 75 66 66 65 72 5f 73 start;..buffer_s
4310: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
4320: 2e 6c 65 6e 67 74 68 20 20 3d 20 72 61 6e 67 65 .length = range
4330: 5f 6c 65 6e 67 74 68 3b 0a 0a 09 2f 2a 20 49 66 _length;.../* If
4340: 20 76 68 6f 73 74 73 20 61 72 65 20 65 6e 61 62 vhosts are enab
4350: 6c 65 64 2c 20 63 6f 6d 70 75 74 65 20 6e 65 77 led, compute new
4360: 20 70 61 74 68 20 2a 2f 0a 09 69 66 20 28 6f 70 path */..if (op
4370: 74 69 6f 6e 73 2d 3e 76 68 6f 73 74 73 5f 65 6e tions->vhosts_en
4380: 61 62 6c 65 64 29 20 7b 0a 09 09 69 66 20 28 62 abled) {...if (b
4390: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 uffer_st->header
43a0: 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d s.host.present =
43b0: 3d 20 31 29 20 7b 0a 09 09 09 62 75 66 66 65 72 = 1) {....buffer
43c0: 20 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d = buffer_st->tm
43d0: 70 62 75 66 3b 0a 09 09 09 62 75 66 66 65 72 5f pbuf;....buffer_
43e0: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 len = sizeof(buf
43f0: 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b fer_st->tmpbuf);
4400: 0a 0a 09 09 09 73 6e 70 72 69 6e 74 66 5f 72 65 .....snprintf_re
4410: 74 20 3d 20 73 6e 70 72 69 6e 74 66 28 62 75 66 t = snprintf(buf
4420: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c fer, buffer_len,
4430: 20 22 2f 25 73 25 73 25 73 22 2c 0a 09 09 09 09 "/%s%s%s",.....
4440: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
4450: 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c 0a 09 09 rs.host.host,...
4460: 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 ..buffer_st->pat
4470: 68 5b 30 5d 20 3d 3d 20 27 2f 27 20 3f 20 22 22 h[0] == '/' ? ""
4480: 20 3a 20 22 2f 22 2c 0a 09 09 09 09 62 75 66 66 : "/",.....buff
4490: 65 72 5f 73 74 2d 3e 70 61 74 68 0a 09 09 09 29 er_st->path....)
44a0: 3b 0a 09 09 09 69 66 20 28 73 6e 70 72 69 6e 74 ;....if (snprint
44b0: 66 5f 72 65 74 20 3e 3d 20 30 29 20 7b 0a 09 09 f_ret >= 0) {...
44c0: 09 09 69 66 20 28 28 28 75 6e 73 69 67 6e 65 64 ..if (((unsigned
44d0: 20 69 6e 74 29 20 73 6e 70 72 69 6e 74 66 5f 72 int) snprintf_r
44e0: 65 74 29 20 3c 20 62 75 66 66 65 72 5f 6c 65 6e et) < buffer_len
44f0: 29 20 7b 0a 09 09 09 09 09 73 74 72 63 70 79 28 ) {......strcpy(
4500: 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c buffer_st->path,
4510: 20 62 75 66 66 65 72 29 3b 0a 09 09 09 09 7d 0a buffer);.....}.
4520: 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 ...}...}..}...re
4530: 74 75 72 6e 28 62 75 66 66 65 72 5f 73 74 29 3b turn(buffer_st);
4540: 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 6e .}../* Return an
4550: 20 65 72 72 6f 72 20 70 61 67 65 20 2a 2f 0a 73 error page */.s
4560: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 tatic void filed
4570: 5f 65 72 72 6f 72 5f 70 61 67 65 28 46 49 4c 45 _error_page(FILE
4580: 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 *fp, const char
4590: 20 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 *date_current,
45a0: 69 6e 74 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 int error_number
45b0: 2c 20 69 6e 74 20 6d 65 74 68 6f 64 2c 20 63 6f , int method, co
45c0: 6e 73 74 20 63 68 61 72 20 2a 72 65 61 73 6f 6e nst char *reason
45d0: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c , struct filed_l
45e0: 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 29 20 7b og_entry *log) {
45f0: 0a 09 63 68 61 72 20 2a 65 72 72 6f 72 5f 73 74 ..char *error_st
4600: 72 69 6e 67 20 3d 20 22 3c 68 74 6d 6c 3e 3c 68 ring = "<html><h
4610: 65 61 64 3e 3c 74 69 74 6c 65 3e 45 52 52 4f 52 ead><title>ERROR
4620: 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 3e 3c </title></head><
4630: 62 6f 64 79 3e 55 6e 61 62 6c 65 20 74 6f 20 70 body>Unable to p
4640: 72 6f 63 65 73 73 20 72 65 71 75 65 73 74 3c 2f rocess request</
4650: 62 6f 64 79 3e 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a body></html>";..
4660: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54 .fprintf(fp, "HT
4670: 54 50 2f 31 2e 31 20 25 69 20 4e 6f 74 20 4f 4b TP/1.1 %i Not OK
4680: 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e \r\nDate: %s\r\n
4690: 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c Server: filed\r\
46a0: 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 nLast-Modified:
46b0: 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 %s\r\nContent-Le
46c0: 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 43 6f ngth: %llu\r\nCo
46d0: 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 ntent-Type: %s\r
46e0: 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c \nConnection: cl
46f0: 6f 73 65 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 ose\r\n\r\n",...
4700: 65 72 72 6f 72 5f 6e 75 6d 62 65 72 2c 0a 09 09 error_number,...
4710: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 date_current,...
4720: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 date_current,...
4730: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
4740: 6f 6e 67 29 20 73 74 72 6c 65 6e 28 65 72 72 6f ong) strlen(erro
4750: 72 5f 73 74 72 69 6e 67 29 2c 0a 09 09 22 74 65 r_string),..."te
4760: 78 74 2f 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f xt/html"..);.../
4770: 2a 20 73 69 6c 65 6e 63 65 20 65 72 72 6f 72 20 * silence error
4780: 73 74 72 69 6e 67 20 66 6f 72 20 48 45 41 44 20 string for HEAD
4790: 72 65 71 75 65 73 74 73 20 2a 2f 0a 09 69 66 20 requests */..if
47a0: 28 6d 65 74 68 6f 64 20 21 3d 20 46 49 4c 45 44 (method != FILED
47b0: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f _REQUEST_METHOD_
47c0: 48 45 41 44 29 20 7b 0a 09 09 66 70 72 69 6e 74 HEAD) {...fprint
47d0: 66 28 66 70 2c 20 22 25 73 22 2c 20 65 72 72 6f f(fp, "%s", erro
47e0: 72 5f 73 74 72 69 6e 67 29 3b 0a 09 7d 0a 0a 09 r_string);..}...
47f0: 2f 2a 20 4c 6f 67 20 65 72 72 6f 72 20 2a 2f 0a /* Log error */.
4800: 09 2f 2a 2a 20 72 65 61 73 6f 6e 20 6d 75 73 74 ./** reason must
4810: 20 70 6f 69 6e 74 20 74 6f 20 61 20 67 6c 6f 62 point to a glob
4820: 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 76 ally allocated v
4830: 61 6c 75 65 20 2a 2a 2f 0a 09 6c 6f 67 2d 3e 72 alue **/..log->r
4840: 65 61 73 6f 6e 20 3d 20 72 65 61 73 6f 6e 3b 0a eason = reason;.
4850: 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 .log->http_code
4860: 3d 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 3b 0a = error_number;.
4870: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 ..filed_log_entr
4880: 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f y(log);.../* Clo
4890: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
48a0: 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 ..fclose(fp);...
48b0: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 return;.}../* Re
48c0: 74 75 72 6e 20 61 20 72 65 64 69 72 65 63 74 20 turn a redirect
48d0: 74 6f 20 69 6e 64 65 78 2e 68 74 6d 6c 20 2a 2f to index.html */
48e0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c .static void fil
48f0: 65 64 5f 72 65 64 69 72 65 63 74 5f 69 6e 64 65 ed_redirect_inde
4900: 78 28 46 49 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 x(FILE *fp, cons
4910: 74 20 63 68 61 72 20 2a 64 61 74 65 5f 63 75 72 t char *date_cur
4920: 72 65 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 rent, const char
4930: 20 2a 70 61 74 68 2c 20 73 74 72 75 63 74 20 66 *path, struct f
4940: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
4950: 6c 6f 67 29 20 7b 0a 09 69 6e 74 20 68 74 74 70 log) {..int http
4960: 5f 63 6f 64 65 20 3d 20 33 30 31 3b 0a 09 66 70 _code = 301;..fp
4970: 72 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f rintf(fp, "HTTP/
4980: 31 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 1.1 %i OK\r\nDat
4990: 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a e: %s\r\nServer:
49a0: 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d filed\r\nLast-M
49b0: 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 odified: %s\r\nC
49c0: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 30 ontent-Length: 0
49d0: 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 \r\nConnection:
49e0: 63 6c 6f 73 65 5c 72 5c 6e 4c 6f 63 61 74 69 6f close\r\nLocatio
49f0: 6e 3a 20 25 73 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a n: %s\r\n\r\n",.
4a00: 09 09 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64 ..http_code,...d
4a10: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64 ate_current,...d
4a20: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 22 ate_current,..."
4a30: 69 6e 64 65 78 2e 68 74 6d 6c 22 0a 09 29 3b 0a index.html"..);.
4a40: 0a 09 2f 2a 20 4c 6f 67 20 72 65 64 69 72 65 63 ../* Log redirec
4a50: 74 20 2a 2f 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f t */..log->reaso
4a60: 6e 20 3d 20 22 72 65 64 69 72 65 63 74 22 3b 0a n = "redirect";.
4a70: 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 .log->http_code
4a80: 3d 20 68 74 74 70 5f 63 6f 64 65 3b 0a 0a 09 66 = http_code;...f
4a90: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c iled_log_entry(l
4aa0: 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20 og);.../* Close
4ab0: 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 66 connection */..f
4ac0: 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 74 close(fp);...ret
4ad0: 75 72 6e 3b 0a 0a 09 2f 2a 20 43 75 72 72 65 6e urn;.../* Curren
4ae0: 74 6c 79 20 75 6e 75 73 65 64 3a 20 70 61 74 68 tly unused: path
4af0: 20 2a 2f 0a 09 70 61 74 68 20 3d 20 70 61 74 68 */..path = path
4b00: 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e 64 6c 65 20 61 ;.}../* Handle a
4b10: 20 73 69 6e 67 6c 65 20 72 65 71 75 65 73 74 20 single request
4b20: 66 72 6f 6d 20 61 20 63 6c 69 65 6e 74 20 2a 2f from a client */
4b30: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c .static void fil
4b40: 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69 65 6e 74 ed_handle_client
4b50: 28 69 6e 74 20 66 64 2c 20 73 74 72 75 63 74 20 (int fd, struct
4b60: 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 filed_http_reque
4b70: 73 74 20 2a 72 65 71 75 65 73 74 2c 20 73 74 72 st *request, str
4b80: 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e uct filed_log_en
4b90: 74 72 79 20 2a 6c 6f 67 2c 20 73 74 72 75 63 74 try *log, struct
4ba0: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a filed_options *
4bb0: 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 72 75 options) {..stru
4bc0: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 ct filed_fileinf
4bd0: 6f 20 2a 66 69 6c 65 69 6e 66 6f 3b 0a 09 73 73 o *fileinfo;..ss
4be0: 69 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f 72 ize_t sendfile_r
4bf0: 65 74 3b 0a 09 73 69 7a 65 5f 74 20 73 65 6e 64 et;..size_t send
4c00: 66 69 6c 65 5f 73 69 7a 65 3b 0a 09 6f 66 66 5f file_size;..off_
4c10: 74 20 73 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 t sendfile_offse
4c20: 74 2c 20 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 t, sendfile_sent
4c30: 2c 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 3b 0a , sendfile_len;.
4c40: 09 63 68 61 72 20 2a 70 61 74 68 3b 0a 09 63 68 .char *path;..ch
4c50: 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 ar *date_current
4c60: 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 , date_current_b
4c70: 5b 36 34 5d 3b 0a 09 69 6e 74 20 68 74 74 70 5f [64];..int http_
4c80: 63 6f 64 65 3b 0a 09 46 49 4c 45 20 2a 66 70 3b code;..FILE *fp;
4c90: 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 .../* Determine
4ca0: 63 75 72 72 65 6e 74 20 74 69 6d 65 20 2a 2f 0a current time */.
4cb0: 09 64 61 74 65 5f 63 75 72 72 65 6e 74 20 3d 20 .date_current =
4cc0: 66 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d filed_format_tim
4cd0: 65 28 64 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 e(date_current_b
4ce0: 2c 20 73 69 7a 65 6f 66 28 64 61 74 65 5f 63 75 , sizeof(date_cu
4cf0: 72 72 65 6e 74 5f 62 29 2c 20 74 69 6d 65 28 4e rrent_b), time(N
4d00: 55 4c 4c 29 29 3b 0a 0a 09 2f 2a 20 4f 70 65 6e ULL));.../* Open
4d10: 20 73 6f 63 6b 65 74 20 61 73 20 41 4e 53 49 20 socket as ANSI
4d20: 49 2f 4f 20 66 6f 72 20 65 61 73 65 20 6f 66 20 I/O for ease of
4d30: 75 73 65 20 2a 2f 0a 09 66 70 20 3d 20 66 64 6f use */..fp = fdo
4d40: 70 65 6e 28 66 64 2c 20 22 77 2b 62 22 29 3b 0a pen(fd, "w+b");.
4d50: 09 69 66 20 28 66 70 20 3d 3d 20 4e 55 4c 4c 29 .if (fp == NULL)
4d60: 20 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a {...close(fd);.
4d70: 0a 09 09 6c 6f 67 2d 3e 62 75 66 66 65 72 5b 30 ...log->buffer[0
4d80: 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 6c 6f 67 2d ] = '\0';...log-
4d90: 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 2d 31 3b >http_code = -1;
4da0: 0a 09 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d ...log->reason =
4db0: 20 22 66 64 6f 70 65 6e 5f 66 61 69 6c 65 64 22 "fdopen_failed"
4dc0: 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 ;....filed_log_e
4dd0: 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 09 72 65 ntry(log);....re
4de0: 74 75 72 6e 3b 0a 09 7d 0a 0a 09 72 65 71 75 65 turn;..}...reque
4df0: 73 74 20 3d 20 66 69 6c 65 64 5f 67 65 74 5f 68 st = filed_get_h
4e00: 74 74 70 5f 72 65 71 75 65 73 74 28 66 70 2c 20 ttp_request(fp,
4e10: 72 65 71 75 65 73 74 2c 20 6f 70 74 69 6f 6e 73 request, options
4e20: 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74 );...if (request
4e30: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 6c 6f == NULL) {...lo
4e40: 67 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 g->buffer[0] = '
4e50: 5c 30 27 3b 0a 0a 09 09 66 69 6c 65 64 5f 65 72 \0';....filed_er
4e60: 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 ror_page(fp, dat
4e70: 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 30 2c 20 e_current, 500,
4e80: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 FILED_REQUEST_ME
4e90: 54 48 4f 44 5f 47 45 54 2c 20 22 66 6f 72 6d 61 THOD_GET, "forma
4ea0: 74 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 t", log);....ret
4eb0: 75 72 6e 3b 0a 09 7d 0a 0a 09 70 61 74 68 20 3d urn;..}...path =
4ec0: 20 72 65 71 75 65 73 74 2d 3e 70 61 74 68 3b 0a request->path;.
4ed0: 09 73 74 72 63 70 79 28 6c 6f 67 2d 3e 62 75 66 .strcpy(log->buf
4ee0: 66 65 72 2c 20 70 61 74 68 29 3b 0a 09 6c 6f 67 fer, path);..log
4ef0: 2d 3e 6d 65 74 68 6f 64 20 3d 20 72 65 71 75 65 ->method = reque
4f00: 73 74 2d 3e 6d 65 74 68 6f 64 3b 0a 0a 09 2f 2a st->method;.../*
4f10: 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 If the requeste
4f20: 64 20 70 61 74 68 20 69 73 20 61 20 64 69 72 65 d path is a dire
4f30: 63 74 6f 72 79 2c 20 72 65 64 69 72 65 63 74 20 ctory, redirect
4f40: 74 6f 20 69 6e 64 65 78 20 70 61 67 65 20 2a 2f to index page */
4f50: 0a 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 74 ..if (request->t
4f60: 79 70 65 20 3d 3d 20 46 49 4c 45 44 5f 52 45 51 ype == FILED_REQ
4f70: 55 45 53 54 5f 54 59 50 45 5f 44 49 52 45 43 54 UEST_TYPE_DIRECT
4f80: 4f 52 59 29 20 7b 0a 09 09 66 69 6c 65 64 5f 72 ORY) {...filed_r
4f90: 65 64 69 72 65 63 74 5f 69 6e 64 65 78 28 66 70 edirect_index(fp
4fa0: 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 , date_current,
4fb0: 70 61 74 68 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 path, log);....r
4fc0: 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 66 69 6c 65 eturn;..}...file
4fd0: 69 6e 66 6f 20 3d 20 66 69 6c 65 64 5f 6f 70 65 info = filed_ope
4fe0: 6e 5f 66 69 6c 65 28 70 61 74 68 2c 20 26 72 65 n_file(path, &re
4ff0: 71 75 65 73 74 2d 3e 66 69 6c 65 69 6e 66 6f 29 quest->fileinfo)
5000: 3b 0a 09 69 66 20 28 66 69 6c 65 69 6e 66 6f 20 ;..if (fileinfo
5010: 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c == NULL) {...fil
5020: 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70 ed_error_page(fp
5030: 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 , date_current,
5040: 34 30 34 2c 20 72 65 71 75 65 73 74 2d 3e 6d 65 404, request->me
5050: 74 68 6f 64 2c 20 22 6f 70 65 6e 5f 66 61 69 6c thod, "open_fail
5060: 65 64 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 ed", log);....re
5070: 74 75 72 6e 3b 0a 09 7d 0a 0a 09 69 66 20 28 72 turn;..}...if (r
5080: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
5090: 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 29 20 7b range.present) {
50a0: 0a 09 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e ...if (request->
50b0: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 headers.range.of
50c0: 66 73 65 74 20 21 3d 20 30 20 7c 7c 20 72 65 71 fset != 0 || req
50d0: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
50e0: 6e 67 65 2e 6c 65 6e 67 74 68 20 3e 3d 20 30 29 nge.length >= 0)
50f0: 20 7b 0a 09 09 09 69 66 20 28 72 65 71 75 65 73 {....if (reques
5100: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
5110: 2e 6f 66 66 73 65 74 20 3e 3d 20 66 69 6c 65 69 .offset >= filei
5120: 6e 66 6f 2d 3e 6c 65 6e 29 20 7b 0a 09 09 09 09 nfo->len) {.....
5130: 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 filed_error_page
5140: 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e (fp, date_curren
5150: 74 2c 20 34 31 36 2c 20 72 65 71 75 65 73 74 2d t, 416, request-
5160: 3e 6d 65 74 68 6f 64 2c 20 22 72 61 6e 67 65 5f >method, "range_
5170: 69 6e 76 61 6c 69 64 22 2c 20 6c 6f 67 29 3b 0a invalid", log);.
5180: 0a 09 09 09 09 63 6c 6f 73 65 28 66 69 6c 65 69 .....close(filei
5190: 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 09 09 09 72 nfo->fd);......r
51a0: 65 74 75 72 6e 3b 0a 09 09 09 7d 0a 0a 09 09 09 eturn;....}.....
51b0: 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 if (request->hea
51c0: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 ders.range.lengt
51d0: 68 20 3d 3d 20 28 28 6f 66 66 5f 74 29 20 2d 31 h == ((off_t) -1
51e0: 29 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f 6c )) {.....filed_l
51f0: 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f og_msg_debug("Co
5200: 6d 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 74 mputing length t
5210: 6f 20 66 69 74 20 69 6e 20 62 6f 75 6e 64 73 3a o fit in bounds:
5220: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20 3d fileinfo->len =
5230: 20 25 6c 6c 75 2c 20 72 65 71 75 65 73 74 2d 3e %llu, request->
5240: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 headers.range.of
5250: 66 73 65 74 20 3d 20 25 6c 6c 75 22 2c 0a 09 09 fset = %llu",...
5260: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
5270: 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f g long) fileinfo
5280: 2d 3e 6c 65 6e 2c 0a 09 09 09 09 09 28 75 6e 73 ->len,......(uns
5290: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
52a0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
52b0: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 0a 09 s.range.offset..
52c0: 09 09 09 29 3b 0a 0a 09 09 09 09 72 65 71 75 65 ...);......reque
52d0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
52e0: 65 2e 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 e.length = filei
52f0: 6e 66 6f 2d 3e 6c 65 6e 20 2d 20 72 65 71 75 65 nfo->len - reque
5300: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
5310: 65 2e 6f 66 66 73 65 74 3b 0a 09 09 09 7d 0a 0a e.offset;....}..
5320: 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ...filed_log_msg
5330: 5f 64 65 62 75 67 28 22 50 61 72 74 69 61 6c 20 _debug("Partial
5340: 72 65 71 75 65 73 74 2c 20 73 74 61 72 74 69 6e request, startin
5350: 67 20 61 74 3a 20 25 6c 6c 75 20 61 6e 64 20 72 g at: %llu and r
5360: 75 6e 6e 69 6e 67 20 66 6f 72 20 25 6c 6c 69 20 unning for %lli
5370: 62 79 74 65 73 22 2c 0a 09 09 09 09 28 75 6e 73 bytes",.....(uns
5380: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
5390: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
53a0: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a s.range.offset,.
53b0: 09 09 09 09 28 6c 6f 6e 67 20 6c 6f 6e 67 29 20 ....(long long)
53c0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
53d0: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 0a 09 09 .range.length...
53e0: 09 29 3b 0a 0a 09 09 7d 0a 0a 09 09 68 74 74 70 .);....}....http
53f0: 5f 63 6f 64 65 20 3d 20 32 30 36 3b 0a 09 7d 20 _code = 206;..}
5400: 65 6c 73 65 20 7b 0a 09 09 68 74 74 70 5f 63 6f else {...http_co
5410: 64 65 20 3d 20 32 30 30 3b 0a 0a 09 09 2f 2a 20 de = 200;..../*
5420: 43 6f 6d 70 75 74 65 20 66 61 6b 65 20 72 61 6e Compute fake ran
5430: 67 65 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 ge parameters th
5440: 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 at includes the
5450: 65 6e 74 69 72 65 20 66 69 6c 65 20 2a 2f 0a 09 entire file */..
5460: 09 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 .request->header
5470: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d s.range.offset =
5480: 20 30 3b 0a 09 09 72 65 71 75 65 73 74 2d 3e 68 0;...request->h
5490: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e eaders.range.len
54a0: 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e gth = fileinfo->
54b0: 6c 65 6e 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74 len;..}...fprint
54c0: 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 f(fp, "HTTP/1.1
54d0: 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 %i OK\r\nDate: %
54e0: 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c s\r\nServer: fil
54f0: 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 ed\r\nLast-Modif
5500: 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 ied: %s\r\nConte
5510: 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c nt-Length: %llu\
5520: 72 5c 6e 41 63 63 65 70 74 2d 52 61 6e 67 65 73 r\nAccept-Ranges
5530: 3a 20 62 79 74 65 73 5c 72 5c 6e 43 6f 6e 74 65 : bytes\r\nConte
5540: 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e 43 nt-Type: %s\r\nC
5550: 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 onnection: close
5560: 5c 72 5c 6e 45 54 61 67 3a 20 5c 22 25 73 5c 22 \r\nETag: \"%s\"
5570: 5c 72 5c 6e 22 2c 0a 09 09 68 74 74 70 5f 63 6f \r\n",...http_co
5580: 64 65 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65 de,...date_curre
5590: 6e 74 2c 0a 09 09 66 69 6c 65 69 6e 66 6f 2d 3e nt,...fileinfo->
55a0: 6c 61 73 74 6d 6f 64 2c 0a 09 09 28 75 6e 73 69 lastmod,...(unsi
55b0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
55c0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
55d0: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 2c 0a 09 .range.length,..
55e0: 09 66 69 6c 65 69 6e 66 6f 2d 3e 74 79 70 65 2c .fileinfo->type,
55f0: 0a 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 65 74 61 ...fileinfo->eta
5600: 67 0a 09 29 3b 0a 0a 09 69 66 20 28 68 74 74 70 g..);...if (http
5610: 5f 63 6f 64 65 20 3d 3d 20 32 30 36 29 20 7b 0a _code == 206) {.
5620: 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 43 ..fprintf(fp, "C
5630: 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20 62 79 ontent-Range: by
5640: 74 65 73 20 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c tes %llu-%llu/%l
5650: 6c 75 5c 72 5c 6e 22 2c 0a 09 09 09 28 75 6e 73 lu\r\n",....(uns
5660: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
5670: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
5680: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a s.range.offset,.
5690: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
56a0: 67 20 6c 6f 6e 67 29 20 28 72 65 71 75 65 73 74 g long) (request
56b0: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
56c0: 6f 66 66 73 65 74 20 2b 20 72 65 71 75 65 73 74 offset + request
56d0: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
56e0: 6c 65 6e 67 74 68 20 2d 20 31 29 2c 0a 09 09 09 length - 1),....
56f0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
5700: 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c ong) fileinfo->l
5710: 65 6e 0a 09 09 29 3b 0a 09 7d 0a 09 66 70 72 69 en...);..}..fpri
5720: 6e 74 66 28 66 70 2c 20 22 5c 72 5c 6e 22 29 3b ntf(fp, "\r\n");
5730: 0a 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 ..fflush(fp);...
5740: 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d log->http_code =
5750: 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 6c 6f 67 http_code;..log
5760: 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 4f 4b 22 3b ->reason = "OK";
5770: 0a 09 6c 6f 67 2d 3e 73 74 61 72 74 74 69 6d 65 ..log->starttime
5780: 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 09 = time(NULL);..
5790: 6c 6f 67 2d 3e 72 65 71 5f 6f 66 66 73 65 74 20 log->req_offset
57a0: 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 = request->heade
57b0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b rs.range.offset;
57c0: 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6c 65 6e 67 74 ..log->req_lengt
57d0: 68 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 h = request->hea
57e0: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 ders.range.lengt
57f0: 68 3b 0a 09 6c 6f 67 2d 3e 66 69 6c 65 5f 6c 65 h;..log->file_le
5800: 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d ngth = fileinfo-
5810: 3e 6c 65 6e 3b 0a 0a 23 69 66 64 65 66 20 46 49 >len;..#ifdef FI
5820: 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 LED_NONBLOCK_HTT
5830: 50 0a 09 69 6e 74 20 73 6f 63 6b 65 74 5f 66 6c P..int socket_fl
5840: 61 67 73 3b 0a 09 66 64 5f 73 65 74 20 72 66 64 ags;..fd_set rfd
5850: 2c 20 77 66 64 3b 0a 09 63 68 61 72 20 73 69 6e , wfd;..char sin
5860: 6b 62 75 66 5b 38 31 39 32 5d 3b 0a 09 73 73 69 kbuf[8192];..ssi
5870: 7a 65 5f 74 20 72 65 61 64 5f 72 65 74 3b 0a 0a ze_t read_ret;..
5880: 09 46 44 5f 5a 45 52 4f 28 26 72 66 64 29 3b 0a .FD_ZERO(&rfd);.
5890: 09 46 44 5f 5a 45 52 4f 28 26 77 66 64 29 3b 0a .FD_ZERO(&wfd);.
58a0: 09 46 44 5f 53 45 54 28 66 64 2c 20 26 72 66 64 .FD_SET(fd, &rfd
58b0: 29 3b 0a 09 46 44 5f 53 45 54 28 66 64 2c 20 26 );..FD_SET(fd, &
58c0: 77 66 64 29 3b 0a 0a 09 73 6f 63 6b 65 74 5f 66 wfd);...socket_f
58d0: 6c 61 67 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c lags = fcntl(fd,
58e0: 20 46 5f 47 45 54 46 4c 29 3b 0a 09 66 63 6e 74 F_GETFL);..fcnt
58f0: 6c 28 66 64 2c 20 46 5f 53 45 54 46 4c 2c 20 73 l(fd, F_SETFL, s
5900: 6f 63 6b 65 74 5f 66 6c 61 67 73 20 7c 20 4f 5f ocket_flags | O_
5910: 4e 4f 4e 42 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 NONBLOCK);.#endi
5920: 66 0a 09 73 65 6e 64 66 69 6c 65 5f 6f 66 66 73 f..sendfile_offs
5930: 65 74 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 et = request->he
5940: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
5950: 65 74 3b 0a 09 73 65 6e 64 66 69 6c 65 5f 6c 65 et;..sendfile_le
5960: 6e 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 n = request->hea
5970: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 ders.range.lengt
5980: 68 3b 0a 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e h;..sendfile_sen
5990: 74 20 3d 20 30 3b 0a 09 77 68 69 6c 65 20 28 72 t = 0;..while (r
59a0: 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 20 3d equest->method =
59b0: 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f = FILED_REQUEST_
59c0: 4d 45 54 48 4f 44 5f 47 45 54 29 20 7b 0a 09 09 METHOD_GET) {...
59d0: 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c 65 6e if (sendfile_len
59e0: 20 3e 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c > FILED_SENDFIL
59f0: 45 5f 4d 41 58 29 20 7b 0a 09 09 09 73 65 6e 64 E_MAX) {....send
5a00: 66 69 6c 65 5f 73 69 7a 65 20 3d 20 46 49 4c 45 file_size = FILE
5a10: 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41 58 3b 0a D_SENDFILE_MAX;.
5a20: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 73 65 ..} else {....se
5a30: 6e 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 73 65 ndfile_size = se
5a40: 6e 64 66 69 6c 65 5f 6c 65 6e 3b 0a 09 09 7d 0a ndfile_len;...}.
5a50: 0a 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 ...sendfile_ret
5a60: 3d 20 73 65 6e 64 66 69 6c 65 28 66 64 2c 20 66 = sendfile(fd, f
5a70: 69 6c 65 69 6e 66 6f 2d 3e 66 64 2c 20 26 73 65 ileinfo->fd, &se
5a80: 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 ndfile_offset, s
5a90: 65 6e 64 66 69 6c 65 5f 73 69 7a 65 29 3b 0a 09 endfile_size);..
5aa0: 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 72 65 .if (sendfile_re
5ab0: 74 20 3c 3d 20 30 29 20 7b 0a 23 69 66 64 65 66 t <= 0) {.#ifdef
5ac0: 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f FILED_NONBLOCK_
5ad0: 48 54 54 50 0a 09 09 09 69 66 20 28 65 72 72 6e HTTP....if (errn
5ae0: 6f 20 3d 3d 20 45 41 47 41 49 4e 29 20 7b 0a 09 o == EAGAIN) {..
5af0: 09 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 ...sendfile_ret
5b00: 3d 20 30 3b 0a 0a 09 09 09 09 77 68 69 6c 65 20 = 0;......while
5b10: 28 31 29 20 7b 0a 09 09 09 09 09 73 65 6c 65 63 (1) {......selec
5b20: 74 28 66 64 20 2b 20 31 2c 20 26 72 66 64 2c 20 t(fd + 1, &rfd,
5b30: 26 77 66 64 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c &wfd, NULL, NULL
5b40: 29 3b 0a 09 09 09 09 09 69 66 20 28 46 44 5f 49 );......if (FD_I
5b50: 53 53 45 54 28 66 64 2c 20 26 72 66 64 29 29 20 SSET(fd, &rfd))
5b60: 7b 0a 09 09 09 09 09 09 72 65 61 64 5f 72 65 74 {.......read_ret
5b70: 20 3d 20 72 65 61 64 28 66 64 2c 20 73 69 6e 6b = read(fd, sink
5b80: 62 75 66 2c 20 73 69 7a 65 6f 66 28 73 69 6e 6b buf, sizeof(sink
5b90: 62 75 66 29 29 3b 0a 0a 09 09 09 09 09 09 69 66 buf));........if
5ba0: 20 28 72 65 61 64 5f 72 65 74 20 3c 3d 20 30 29 (read_ret <= 0)
5bb0: 20 7b 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b {........break;
5bc0: 0a 09 09 09 09 09 09 7d 0a 09 09 09 09 09 7d 0a .......}......}.
5bd0: 0a 09 09 09 09 09 69 66 20 28 46 44 5f 49 53 53 ......if (FD_ISS
5be0: 45 54 28 66 64 2c 20 26 77 66 64 29 29 20 7b 0a ET(fd, &wfd)) {.
5bf0: 09 09 09 09 09 09 72 65 61 64 5f 72 65 74 20 3d ......read_ret =
5c00: 20 31 3b 0a 0a 09 09 09 09 09 09 62 72 65 61 6b 1;........break
5c10: 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09 7d 0a 0a ;......}.....}..
5c20: 09 09 09 09 69 66 20 28 72 65 61 64 5f 72 65 74 ....if (read_ret
5c30: 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 09 62 72 <= 0) {......br
5c40: 65 61 6b 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 20 eak;.....}....}
5c50: 65 6c 73 65 20 7b 0a 09 09 09 09 62 72 65 61 6b else {.....break
5c60: 3b 0a 09 09 09 7d 0a 23 65 6c 73 65 0a 09 09 09 ;....}.#else....
5c70: 62 72 65 61 6b 3b 0a 23 65 6e 64 69 66 0a 09 09 break;.#endif...
5c80: 7d 0a 0a 09 09 73 65 6e 64 66 69 6c 65 5f 6c 65 }....sendfile_le
5c90: 6e 20 2d 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 n -= sendfile_re
5ca0: 74 3b 0a 09 09 73 65 6e 64 66 69 6c 65 5f 73 65 t;...sendfile_se
5cb0: 6e 74 20 2b 3d 20 73 65 6e 64 66 69 6c 65 5f 72 nt += sendfile_r
5cc0: 65 74 3b 0a 09 09 69 66 20 28 73 65 6e 64 66 69 et;...if (sendfi
5cd0: 6c 65 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 le_len == 0) {..
5ce0: 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a ..break;...}..}.
5cf0: 0a 09 6c 6f 67 2d 3e 65 6e 64 74 69 6d 65 20 3d ..log->endtime =
5d00: 20 28 74 69 6d 65 5f 74 29 20 2d 31 3b 0a 09 6c (time_t) -1;..l
5d10: 6f 67 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 20 og->sent_length
5d20: 3d 20 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 3b = sendfile_sent;
5d30: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ...filed_log_ent
5d40: 72 79 28 6c 6f 67 29 3b 0a 0a 09 63 6c 6f 73 65 ry(log);...close
5d50: 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a (fileinfo->fd);.
5d60: 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 ..fclose(fp);...
5d70: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 48 61 return;.}../* Ha
5d80: 6e 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f ndle incoming co
5d90: 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 nnections */.sta
5da0: 74 69 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f tic void *filed_
5db0: 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 28 76 6f worker_thread(vo
5dc0: 69 64 20 2a 61 72 67 5f 76 29 20 7b 0a 09 73 74 id *arg_v) {..st
5dd0: 72 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 ruct filed_worke
5de0: 72 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 r_thread_args *a
5df0: 72 67 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 rg;..struct file
5e00: 64 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 72 d_http_request r
5e10: 65 71 75 65 73 74 3b 0a 09 73 74 72 75 63 74 20 equest;..struct
5e20: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 filed_log_entry
5e30: 2a 6c 6f 67 2c 20 6c 6f 63 61 6c 5f 64 75 6d 6d *log, local_dumm
5e40: 79 5f 6c 6f 67 3b 0a 09 73 74 72 75 63 74 20 66 y_log;..struct f
5e50: 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 iled_options *op
5e60: 74 69 6f 6e 73 3b 0a 09 73 74 72 75 63 74 20 73 tions;..struct s
5e70: 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 64 72 ockaddr_in6 addr
5e80: 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 ;..socklen_t add
5e90: 72 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 69 6c 75 rlen;..int failu
5ea0: 72 65 5f 63 6f 75 6e 74 20 3d 20 30 2c 20 6d 61 re_count = 0, ma
5eb0: 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 x_failure_count
5ec0: 3d 20 46 49 4c 45 44 5f 4d 41 58 5f 46 41 49 4c = FILED_MAX_FAIL
5ed0: 55 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 URE_COUNT;..int
5ee0: 6d 61 73 74 65 72 5f 66 64 2c 20 66 64 3b 0a 0a master_fd, fd;..
5ef0: 09 2f 2a 20 52 65 61 64 20 61 72 67 75 6d 65 6e ./* Read argumen
5f00: 74 73 20 2a 2f 0a 09 61 72 67 20 3d 20 61 72 67 ts */..arg = arg
5f10: 5f 76 3b 0a 0a 09 6d 61 73 74 65 72 5f 66 64 20 _v;...master_fd
5f20: 3d 20 61 72 67 2d 3e 66 64 3b 0a 09 6f 70 74 69 = arg->fd;..opti
5f30: 6f 6e 73 20 3d 20 26 61 72 67 2d 3e 6f 70 74 69 ons = &arg->opti
5f40: 6f 6e 73 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29 ons;...while (1)
5f50: 20 7b 0a 09 09 2f 2a 20 46 61 69 6c 75 72 65 20 {.../* Failure
5f60: 6c 6f 6f 70 20 70 72 65 76 65 6e 74 69 6f 6e 20 loop prevention
5f70: 2a 2f 0a 09 09 69 66 20 28 66 61 69 6c 75 72 65 */...if (failure
5f80: 5f 63 6f 75 6e 74 20 3e 20 6d 61 78 5f 66 61 69 _count > max_fai
5f90: 6c 75 72 65 5f 63 6f 75 6e 74 29 20 7b 0a 09 09 lure_count) {...
5fa0: 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09 09 2f .break;...}..../
5fb0: 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
5fc0: 20 6c 6f 67 20 62 75 66 66 65 72 20 2a 2f 0a 09 log buffer */..
5fd0: 09 6c 6f 67 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 .log = filed_log
5fe0: 5f 6e 65 77 28 31 29 3b 0a 09 09 69 66 20 28 6c _new(1);...if (l
5ff0: 6f 67 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 og == NULL) {...
6000: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 .filed_log_msg("
6010: 41 4c 4c 4f 43 41 54 45 5f 4c 4f 47 5f 4d 53 47 ALLOCATE_LOG_MSG
6020: 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 09 09 62 _FAILED");.....b
6030: 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09 09 6c 6f 67 reak;...}....log
6040: 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 4c ->type = FILED_L
6050: 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 52 OG_TYPE_TRANSFER
6060: 3b 0a 0a 09 09 2f 2a 20 41 63 63 65 70 74 20 61 ;..../* Accept a
6070: 20 6e 65 77 20 63 6c 69 65 6e 74 20 2a 2f 0a 09 new client */..
6080: 09 61 64 64 72 6c 65 6e 20 3d 20 73 69 7a 65 6f .addrlen = sizeo
6090: 66 28 61 64 64 72 29 3b 0a 09 09 66 64 20 3d 20 f(addr);...fd =
60a0: 61 63 63 65 70 74 28 6d 61 73 74 65 72 5f 66 64 accept(master_fd
60b0: 2c 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 , (struct sockad
60c0: 64 72 20 2a 29 20 26 61 64 64 72 2c 20 26 61 64 dr *) &addr, &ad
60d0: 64 72 6c 65 6e 29 3b 0a 0a 09 09 2f 2a 0a 09 09 drlen);..../*...
60e0: 20 2a 20 49 66 20 77 65 20 66 61 69 6c 2c 20 6d * If we fail, m
60f0: 61 6b 65 20 61 20 6e 6f 74 65 20 6f 66 20 69 74 ake a note of it
6100: 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 67 6f 20 so we don't go
6110: 69 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f 66 0a 09 into a loop of..
6120: 09 20 2a 20 61 63 63 65 70 74 28 29 20 66 61 69 . * accept() fai
6130: 6c 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 69 66 20 ling... */...if
6140: 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 09 2f 2a (fd < 0) {..../*
6150: 20 4c 6f 67 20 74 68 65 20 6e 65 77 20 63 6f 6e Log the new con
6160: 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 09 66 69 nection */....fi
6170: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 43 43 led_log_msg("ACC
6180: 45 50 54 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 EPT_FAILED");...
6190: 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 2b ..failure_count+
61a0: 2b 3b 0a 0a 09 09 09 66 72 65 65 28 6c 6f 67 29 +;.....free(log)
61b0: 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e 75 65 3b 0a ;.....continue;.
61c0: 09 09 7d 0a 0a 09 09 2f 2a 20 46 69 6c 6c 20 69 ..}..../* Fill i
61d0: 6e 20 6c 6f 67 20 73 74 72 75 63 74 75 72 65 20 n log structure
61e0: 2a 2f 0a 09 09 69 66 20 28 66 69 6c 65 64 5f 6c */...if (filed_l
61f0: 6f 67 5f 69 70 28 28 73 74 72 75 63 74 20 73 6f og_ip((struct so
6200: 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 2c ckaddr *) &addr,
6210: 20 6c 6f 67 2d 3e 69 70 2c 20 73 69 7a 65 6f 66 log->ip, sizeof
6220: 28 6c 6f 67 2d 3e 69 70 29 29 20 3d 3d 20 4e 55 (log->ip)) == NU
6230: 4c 4c 29 20 7b 0a 09 09 09 6c 6f 67 2d 3e 69 70 LL) {....log->ip
6240: 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 09 6c [0] = '\0';....l
6250: 6f 67 2d 3e 70 6f 72 74 20 3d 20 30 3b 0a 09 09 og->port = 0;...
6260: 7d 20 65 6c 73 65 20 7b 0a 09 09 09 6c 6f 67 2d } else {....log-
6270: 3e 70 6f 72 74 20 3d 20 61 64 64 72 2e 73 69 6e >port = addr.sin
6280: 36 5f 70 6f 72 74 3b 0a 09 09 7d 0a 0a 09 09 2f 6_port;...}..../
6290: 2a 20 52 65 73 65 74 20 66 61 69 6c 75 72 65 20 * Reset failure
62a0: 63 6f 75 6e 74 2a 2f 0a 09 09 66 61 69 6c 75 72 count*/...failur
62b0: 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 0a 09 09 e_count = 0;....
62c0: 2f 2a 20 48 61 6e 64 6c 65 20 73 6f 63 6b 65 74 /* Handle socket
62d0: 20 2a 2f 0a 09 09 66 69 6c 65 64 5f 68 61 6e 64 */...filed_hand
62e0: 6c 65 5f 63 6c 69 65 6e 74 28 66 64 2c 20 26 72 le_client(fd, &r
62f0: 65 71 75 65 73 74 2c 20 6c 6f 67 2c 20 6f 70 74 equest, log, opt
6300: 69 6f 6e 73 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 ions);..}.../* R
6310: 65 70 6f 72 74 20 65 72 72 6f 72 20 2a 2f 0a 09 eport error */..
6320: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 54 filed_log_msg("T
6330: 48 52 45 41 44 5f 44 49 45 44 20 41 42 4e 4f 52 HREAD_DIED ABNOR
6340: 4d 41 4c 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 MAL");...return(
6350: 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 6c 6f 63 61 NULL);.../* loca
6360: 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 20 69 73 20 6f l_dummy_log is o
6370: 6e 6c 79 20 75 73 65 64 20 69 66 20 46 49 4c 45 nly used if FILE
6380: 44 5f 44 4f 4e 54 5f 4c 4f 47 20 69 73 20 65 6e D_DONT_LOG is en
6390: 61 62 6c 65 64 2c 20 6f 74 68 65 72 77 69 73 65 abled, otherwise
63a0: 20 69 74 27 73 20 6e 6f 74 20 75 73 65 64 2c 20 it's not used,
63b0: 62 75 74 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 but the compiler
63c0: 20 68 61 74 65 73 20 74 68 61 74 20 69 64 65 61 hates that idea
63d0: 2e 20 2a 2f 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d . */..local_dumm
63e0: 79 5f 6c 6f 67 2e 74 79 70 65 20 3d 20 30 3b 0a y_log.type = 0;.
63f0: 09 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 .local_dummy_log
6400: 2e 74 79 70 65 20 3d 20 6c 6f 63 61 6c 5f 64 75 .type = local_du
6410: 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 3b 0a 7d 0a mmy_log.type;.}.
6420: 0a 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 6b 65 ./* Create worke
6430: 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 61 r threads */.sta
6440: 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 77 6f tic int filed_wo
6450: 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 rker_threads_ini
6460: 74 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 74 68 t(int fd, int th
6470: 72 65 61 64 5f 63 6f 75 6e 74 2c 20 73 74 72 75 read_count, stru
6480: 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 ct filed_options
6490: 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 *options) {..st
64a0: 72 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 ruct filed_worke
64b0: 72 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 r_thread_args *a
64c0: 72 67 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 74 rg;..pthread_t t
64d0: 68 72 65 61 64 69 64 3b 0a 09 69 6e 74 20 70 74 hreadid;..int pt
64e0: 68 72 65 61 64 5f 72 65 74 3b 0a 09 69 6e 74 20 hread_ret;..int
64f0: 69 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b i;...for (i = 0;
6500: 20 69 20 3c 20 74 68 72 65 61 64 5f 63 6f 75 6e i < thread_coun
6510: 74 3b 20 69 2b 2b 29 20 7b 0a 09 09 61 72 67 20 t; i++) {...arg
6520: 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 = malloc(sizeof(
6530: 2a 61 72 67 29 29 3b 0a 0a 09 09 61 72 67 2d 3e *arg));....arg->
6540: 66 64 20 3d 20 66 64 3b 0a 09 09 6d 65 6d 63 70 fd = fd;...memcp
6550: 79 28 26 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 2c y(&arg->options,
6560: 20 6f 70 74 69 6f 6e 73 2c 20 73 69 7a 65 6f 66 options, sizeof
6570: 28 2a 6f 70 74 69 6f 6e 73 29 29 3b 0a 0a 09 09 (*options));....
6580: 70 74 68 72 65 61 64 5f 72 65 74 20 3d 20 70 74 pthread_ret = pt
6590: 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 68 hread_create(&th
65a0: 72 65 61 64 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 readid, NULL, fi
65b0: 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 led_worker_threa
65c0: 64 2c 20 61 72 67 29 3b 0a 09 09 69 66 20 28 70 d, arg);...if (p
65d0: 74 68 72 65 61 64 5f 72 65 74 20 21 3d 20 30 29 thread_ret != 0)
65e0: 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 31 29 {....return(-1)
65f0: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 ;...}..}...retur
6600: 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 73 70 n(0);.}../* Disp
6610: 6c 61 79 20 68 65 6c 70 20 2a 2f 0a 73 74 61 74 lay help */.stat
6620: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 70 72 ic void filed_pr
6630: 69 6e 74 5f 68 65 6c 70 28 46 49 4c 45 20 2a 6f int_help(FILE *o
6640: 75 74 70 75 74 2c 20 69 6e 74 20 6c 6f 6e 67 5f utput, int long_
6650: 68 65 6c 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 help, const char
6660: 20 2a 65 78 74 72 61 29 20 7b 0a 09 69 66 20 28 *extra) {..if (
6670: 65 78 74 72 61 29 20 7b 0a 09 09 66 70 72 69 6e extra) {...fprin
6680: 74 66 28 6f 75 74 70 75 74 2c 20 22 25 73 5c 6e tf(output, "%s\n
6690: 22 2c 20 65 78 74 72 61 29 3b 0a 09 7d 0a 0a 09 ", extra);..}...
66a0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
66b0: 22 55 73 61 67 65 3a 20 66 69 6c 65 64 20 5b 3c "Usage: filed [<
66c0: 6f 70 74 69 6f 6e 73 3e 5d 5c 6e 22 29 3b 0a 09 options>]\n");..
66d0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
66e0: 22 20 20 4f 70 74 69 6f 6e 73 3a 5c 6e 22 29 3b " Options:\n");
66f0: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
6700: 2c 20 22 20 20 20 20 20 20 2d 68 2c 20 2d 2d 68 , " -h, --h
6710: 65 6c 70 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 elp\n");..fprint
6720: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
6730: 20 2d 64 2c 20 2d 2d 64 61 65 6d 6f 6e 5c 6e 22 -d, --daemon\n"
6740: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
6750: 75 74 2c 20 22 20 20 20 20 20 20 2d 76 2c 20 2d ut, " -v, -
6760: 2d 76 65 72 73 69 6f 6e 5c 6e 22 29 3b 0a 09 66 -version\n");..f
6770: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
6780: 20 20 20 20 20 20 2d 56 2c 20 2d 2d 76 68 6f 73 -V, --vhos
6790: 74 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 t\n");..fprintf(
67a0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
67b0: 62 20 3c 61 64 64 72 65 73 73 3e 2c 20 2d 2d 62 b <address>, --b
67c0: 69 6e 64 20 3c 61 64 64 72 65 73 73 3e 5c 6e 22 ind <address>\n"
67d0: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
67e0: 75 74 2c 20 22 20 20 20 20 20 20 2d 70 20 3c 70 ut, " -p <p
67f0: 6f 72 74 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70 6f ort>, --port <po
6800: 72 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 rt>\n");..fprint
6810: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
6820: 20 2d 74 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d 74 -t <count>, --t
6830: 68 72 65 61 64 73 20 3c 63 6f 75 6e 74 3e 5c 6e hreads <count>\n
6840: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 ");..fprintf(out
6850: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 63 20 3c put, " -c <
6860: 65 6e 74 72 69 65 73 3e 2c 20 2d 2d 63 61 63 68 entries>, --cach
6870: 65 20 3c 65 6e 74 72 69 65 73 3e 5c 6e 22 29 3b e <entries>\n");
6880: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
6890: 2c 20 22 20 20 20 20 20 20 2d 6c 20 3c 66 69 6c , " -l <fil
68a0: 65 3e 2c 20 2d 2d 6c 6f 67 20 3c 66 69 6c 65 3e e>, --log <file>
68b0: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f \n");..fprintf(o
68c0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 75 utput, " -u
68d0: 20 3c 75 73 65 72 3e 2c 20 2d 2d 75 73 65 72 20 <user>, --user
68e0: 3c 75 73 65 72 3e 5c 6e 22 29 3b 0a 09 66 70 72 <user>\n");..fpr
68f0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
6900: 20 20 20 20 2d 72 20 3c 64 69 72 65 63 74 6f 72 -r <director
6910: 79 3e 2c 20 2d 2d 72 6f 6f 74 20 3c 64 69 72 65 y>, --root <dire
6920: 63 74 6f 72 79 3e 5c 6e 22 29 3b 0a 0a 09 69 66 ctory>\n");...if
6930: 20 28 6c 6f 6e 67 5f 68 65 6c 70 29 20 7b 0a 09 (long_help) {..
6940: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6950: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 "\n");...fprint
6960: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 55 73 61 f(output, " Usa
6970: 67 65 3a 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ge:\n");...fprin
6980: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
6990: 20 20 2d 68 20 28 6f 72 20 2d 2d 68 65 6c 70 29 -h (or --help)
69a0: 20 70 72 69 6e 74 73 20 74 68 69 73 20 75 73 61 prints this usa
69b0: 67 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 5c ge information.\
69c0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
69d0: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
69e0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
69f0: 22 20 20 20 20 20 20 2d 64 20 28 6f 72 20 2d 2d " -d (or --
6a00: 64 61 65 6d 6f 6e 29 20 69 6e 73 74 72 75 63 74 daemon) instruct
6a10: 73 20 66 69 6c 65 64 20 74 6f 20 62 65 63 6f 6d s filed to becom
6a20: 65 20 61 20 64 61 65 6d 6f 6e 20 61 66 74 65 72 e a daemon after
6a30: 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 5c 6e 22 initializing\n"
6a40: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
6a50: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
6a60: 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
6a70: 20 6c 69 73 74 65 6e 69 6e 67 20 54 43 50 20 73 listening TCP s
6a80: 6f 63 6b 65 74 20 61 6e 64 20 6c 6f 67 20 66 69 ocket and log fi
6a90: 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 les.\n");...fpri
6aa0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 ntf(output, "\n"
6ab0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
6ac0: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 20 28 put, " -v (
6ad0: 6f 72 20 2d 2d 76 65 72 73 69 6f 6e 29 20 69 6e or --version) in
6ae0: 73 74 72 75 63 74 73 20 66 69 6c 65 64 20 70 72 structs filed pr
6af0: 69 6e 74 20 6f 75 74 20 74 68 65 20 76 65 72 73 int out the vers
6b00: 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 65 ion number and e
6b10: 78 69 74 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 xit.\n");...fpri
6b20: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 ntf(output, "\n"
6b30: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
6b40: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 56 20 28 put, " -V (
6b50: 6f 72 20 2d 2d 76 68 6f 73 74 29 20 69 6e 73 74 or --vhost) inst
6b60: 72 75 63 74 73 20 66 69 6c 65 64 20 74 6f 20 70 ructs filed to p
6b70: 72 65 70 65 6e 64 20 61 6c 6c 20 72 65 71 75 65 repend all reque
6b80: 73 74 73 20 77 69 74 68 20 74 68 65 69 72 20 48 sts with their H
6b90: 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e TTP\n");...fprin
6ba0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
6bb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6bc0: 20 20 48 6f 73 74 20 68 65 61 64 65 72 2e 5c 6e Host header.\n
6bd0: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
6be0: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 tput, "\n");...f
6bf0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
6c00: 20 20 20 20 20 20 2d 62 20 28 6f 72 20 2d 2d 62 -b (or --b
6c10: 69 6e 64 29 20 73 70 65 63 69 66 69 65 73 20 74 ind) specifies t
6c20: 68 65 20 61 64 64 72 65 73 73 20 74 6f 20 6c 69 he address to li
6c30: 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e sten for incomin
6c40: 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 g HTTP\n");...fp
6c50: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
6c60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c70: 20 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e 2e requests on.
6c80: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 The default va
6c90: 6c 75 65 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e lue is \"%s\".\n
6ca0: 22 2c 20 42 49 4e 44 5f 41 44 44 52 29 3b 0a 09 ", BIND_ADDR);..
6cb0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6cc0: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 "\n");...fprint
6cd0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
6ce0: 20 2d 70 20 28 6f 72 20 2d 2d 70 6f 72 74 29 20 -p (or --port)
6cf0: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 54 43 specifies the TC
6d00: 50 20 70 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f P port number to
6d10: 20 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f listen for inco
6d20: 6d 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 ming HTTP\n");..
6d30: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6d40: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
6d50: 20 20 20 20 20 20 20 72 65 71 75 65 73 74 73 20 requests
6d60: 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 on. The default
6d70: 20 69 73 20 25 75 2e 5c 6e 22 2c 20 28 75 6e 73 is %u.\n", (uns
6d80: 69 67 6e 65 64 20 69 6e 74 29 20 50 4f 52 54 29 igned int) PORT)
6d90: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
6da0: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 ut, "\n");...fpr
6db0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
6dc0: 20 20 20 20 2d 74 20 28 6f 72 20 2d 2d 74 68 72 -t (or --thr
6dd0: 65 61 64 73 29 20 73 70 65 63 69 66 69 65 73 20 eads) specifies
6de0: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 6f the number of wo
6df0: 72 6b 65 72 20 74 68 72 65 61 64 73 20 74 6f 20 rker threads to
6e00: 63 72 65 61 74 65 2e 20 45 61 63 68 5c 6e 22 29 create. Each\n")
6e10: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
6e20: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
6e30: 20 20 20 20 20 20 20 20 20 20 20 20 20 77 6f 72 wor
6e40: 6b 65 72 20 74 68 72 65 61 64 20 63 61 6e 20 73 ker thread can s
6e50: 65 72 76 69 63 65 20 6f 6e 65 20 63 6f 6e 63 75 ervice one concu
6e60: 72 72 65 6e 74 20 48 54 54 50 20 73 65 73 73 69 rrent HTTP sessi
6e70: 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e on.\n");...fprin
6e80: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
6e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ea0: 20 20 20 20 54 68 75 73 20 74 68 65 20 6e 75 6d Thus the num
6eb0: 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 20 63 ber of threads c
6ec0: 72 65 61 74 65 64 20 77 69 6c 6c 20 64 65 74 65 reated will dete
6ed0: 72 6d 69 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a 09 rmine how\n");..
6ee0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6ef0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
6f00: 20 20 20 20 20 20 20 20 20 20 6d 61 6e 79 20 73 many s
6f10: 69 6d 75 6c 74 61 6e 65 6f 75 73 20 74 72 61 6e imultaneous tran
6f20: 73 66 65 72 73 20 77 69 6c 6c 20 62 65 20 70 6f sfers will be po
6f30: 73 73 69 62 6c 65 2e 20 54 68 65 5c 6e 22 29 3b ssible. The\n");
6f40: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
6f50: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
6f60: 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66 61 defa
6f70: 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 ult is %lu.\n",
6f80: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 (unsigned long)
6f90: 54 48 52 45 41 44 5f 43 4f 55 4e 54 29 3b 0a 09 THREAD_COUNT);..
6fa0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6fb0: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 "\n");...fprint
6fc0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
6fd0: 20 2d 63 20 28 6f 72 20 2d 2d 63 61 63 68 65 29 -c (or --cache)
6fe0: 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e specifies the n
6ff0: 75 6d 62 65 72 20 6f 66 20 66 69 6c 65 20 69 6e umber of file in
7000: 66 6f 72 6d 61 74 69 6f 6e 20 63 61 63 68 65 20 formation cache
7010: 65 6e 74 72 69 65 73 5c 6e 22 29 3b 0a 09 09 66 entries\n");...f
7020: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
7030: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7040: 20 20 20 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74 to allocat
7050: 65 2e 20 20 45 61 63 68 20 63 61 63 68 65 20 65 e. Each cache e
7060: 6e 74 72 79 20 68 6f 6c 64 73 20 66 69 6c 65 20 ntry holds file
7070: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 5c 6e information as\n
7080: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
7090: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
70a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 77 65 6c wel
70b0: 6c 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c l as an open fil
70c0: 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 e descriptor to
70d0: 74 68 65 20 66 69 6c 65 2c 20 73 6f 20 72 65 73 the file, so res
70e0: 6f 75 72 63 65 5c 6e 22 29 3b 0a 09 09 66 70 72 ource\n");...fpr
70f0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
7100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7110: 20 20 20 20 6c 69 6d 69 74 73 20 28 69 2e 65 2e limits (i.e.
7120: 2c 20 75 6c 69 6d 69 74 29 20 73 68 6f 75 6c 64 , ulimit) should
7130: 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 2e 20 be considered.
7140: 20 54 68 69 73 20 73 68 6f 75 6c 64 5c 6e 22 29 This should\n")
7150: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
7160: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
7170: 20 20 20 20 20 20 20 20 20 20 20 62 65 20 61 20 be a
7180: 70 72 69 6d 65 20 6e 75 6d 62 65 72 20 66 6f 72 prime number for
7190: 20 69 64 65 61 6c 20 75 73 65 20 77 69 74 68 20 ideal use with
71a0: 74 68 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 6f the lookup metho
71b0: 64 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 d.\n");...fprint
71c0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
71d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71e0: 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 The default is
71f0: 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e %lu.\n", (unsign
7200: 65 64 20 6c 6f 6e 67 29 20 43 41 43 48 45 5f 53 ed long) CACHE_S
7210: 49 5a 45 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 IZE);...fprintf(
7220: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 output, "\n");..
7230: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
7240: 20 22 20 20 20 20 20 20 2d 6c 20 28 6f 72 20 2d " -l (or -
7250: 2d 6c 6f 67 29 20 73 70 65 63 69 66 69 65 73 20 -log) specifies
7260: 61 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 6f 70 a filename to op
7270: 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 20 6c en for writing l
7280: 6f 67 20 65 6e 74 72 69 65 73 2e 20 20 4c 6f 67 og entries. Log
7290: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
72a0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
72b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 65 6e 74 ent
72c0: 72 69 65 73 20 61 72 65 20 6d 61 64 65 20 66 6f ries are made fo
72d0: 72 20 76 61 72 69 6f 75 73 20 73 74 61 67 65 73 r various stages
72e0: 20 69 6e 20 74 72 61 6e 73 66 65 72 69 6e 67 20 in transfering
72f0: 66 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 files.\n");...fp
7300: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
7310: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7320: 20 20 20 54 68 65 20 6c 6f 67 20 66 69 6c 65 20 The log file
7330: 69 73 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 is opened before
7340: 20 73 77 69 74 63 68 69 6e 67 20 75 73 65 72 73 switching users
7350: 20 28 73 65 65 20 5c 22 2d 75 5c 22 29 5c 6e 22 (see \"-u\")\n"
7360: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
7370: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
7380: 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 72 6f and ro
7390: 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73 20 28 ot directories (
73a0: 73 65 65 20 5c 22 2d 72 5c 22 29 2e 20 20 54 68 see \"-r\"). Th
73b0: 65 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e 65 e log file is ne
73c0: 76 65 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ver\n");...fprin
73d0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
73e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73f0: 63 6c 6f 73 65 64 20 73 6f 20 6c 6f 67 20 72 6f closed so log ro
7400: 74 61 74 69 6f 6e 20 77 69 74 68 6f 75 74 20 73 tation without s
7410: 74 6f 70 70 69 6e 67 20 74 68 65 20 64 61 65 6d topping the daem
7420: 6f 6e 20 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a on is will\n");.
7430: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
7440: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
7450: 20 20 20 20 20 20 20 6e 6f 74 20 77 6f 72 6b 2e not work.
7460: 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 5c The value of \
7470: 22 2d 5c 22 20 69 6e 64 69 63 61 74 65 73 20 74 "-\" indicates t
7480: 68 61 74 20 73 74 61 6e 64 61 72 64 20 6f 75 74 hat standard out
7490: 70 75 74 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e put\n");...fprin
74a0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
74b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
74c0: 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 should be used f
74d0: 6f 72 20 6c 6f 67 67 69 6e 67 2e 20 20 49 66 20 or logging. If
74e0: 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 62 65 67 the filename beg
74f0: 69 6e 73 20 77 69 74 68 20 61 5c 6e 22 29 3b 0a ins with a\n");.
7500: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
7510: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
7520: 20 20 20 20 20 20 20 70 69 70 65 20 28 5c 22 7c pipe (\"|
7530: 5c 22 29 20 74 68 65 6e 20 61 20 70 72 6f 63 65 \") then a proce
7540: 73 73 20 69 73 20 73 74 61 72 74 65 64 20 61 6e ss is started an
7550: 64 20 75 73 65 64 20 66 6f 72 20 6c 6f 67 67 69 d used for loggi
7560: 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 ng\n");...fprint
7570: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
7580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
7590: 6e 73 74 65 61 64 20 6f 66 20 61 20 66 69 6c 65 nstead of a file
75a0: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 . The default i
75b0: 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 4c 4f s \"%s\".\n", LO
75c0: 47 5f 46 49 4c 45 29 3b 0a 23 69 66 64 65 66 20 G_FILE);.#ifdef
75d0: 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 09 FILED_DONT_LOG..
75e0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
75f0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
7600: 20 20 20 20 20 20 4e 6f 74 65 20 74 68 61 74 20 Note that
7610: 6c 6f 67 67 69 6e 67 20 69 73 20 63 6f 6d 70 6c logging is compl
7620: 65 74 65 6c 79 20 64 69 73 61 62 6c 65 64 20 73 etely disabled s
7630: 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 64 6f o this option do
7640: 65 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 es\n");...fprint
7650: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
7660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e n
7670: 6f 74 68 69 6e 67 20 69 6e 20 74 68 69 73 20 62 othing in this b
7680: 75 69 6c 64 2e 5c 6e 22 29 3b 0a 23 65 6e 64 69 uild.\n");.#endi
7690: 66 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 f...fprintf(outp
76a0: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 ut, "\n");...fpr
76b0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
76c0: 20 20 20 20 2d 75 20 28 6f 72 20 2d 2d 75 73 65 -u (or --use
76d0: 72 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 r) specifies the
76e0: 20 75 73 65 72 20 74 6f 20 73 77 69 74 63 68 20 user to switch
76f0: 75 73 65 72 20 49 44 73 20 74 6f 20 62 65 66 6f user IDs to befo
7700: 72 65 20 73 65 72 76 69 63 69 6e 67 5c 6e 22 29 re servicing\n")
7710: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
7720: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
7730: 20 20 20 20 20 20 20 20 20 20 72 65 71 75 65 73 reques
7740: 74 73 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 ts. The default
7750: 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 75 is not change u
7760: 73 65 72 20 49 44 73 2e 5c 6e 22 29 3b 0a 09 09 ser IDs.\n");...
7770: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
7780: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
7790: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
77a0: 2d 72 20 28 6f 72 20 2d 2d 72 6f 6f 74 29 20 73 -r (or --root) s
77b0: 70 65 63 69 66 69 65 73 20 74 68 65 20 64 69 72 pecifies the dir
77c0: 65 63 74 6f 72 79 20 74 6f 20 61 63 74 20 61 73 ectory to act as
77d0: 20 74 68 65 20 72 6f 6f 74 20 64 69 72 65 63 74 the root direct
77e0: 6f 72 79 20 66 6f 72 5c 6e 22 29 3b 0a 09 09 66 ory for\n");...f
77f0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
7800: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7810: 20 20 20 20 20 74 68 65 20 66 69 6c 65 20 73 65 the file se
7820: 72 76 65 72 2e 20 20 49 66 20 74 68 69 73 20 6f rver. If this o
7830: 70 74 69 6f 6e 20 69 73 20 73 70 65 63 69 66 69 ption is specifi
7840: 65 64 2c 20 63 68 72 6f 6f 74 28 32 29 5c 6e 22 ed, chroot(2)\n"
7850: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
7860: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
7870: 20 20 20 20 20 20 20 20 20 20 20 69 73 20 63 61 is ca
7880: 6c 6c 65 64 2e 20 20 54 68 65 20 64 65 66 61 75 lled. The defau
7890: 6c 74 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 lt is not change
78a0: 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 69 65 root directorie
78b0: 73 2c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 s,\n");...fprint
78c0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
78d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78e0: 74 68 61 74 20 69 73 2c 20 74 68 65 20 5c 22 2f that is, the \"/
78f0: 5c 22 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 \" directory is
7900: 73 68 61 72 65 64 20 6f 75 74 2e 20 20 54 68 69 shared out. Thi
7910: 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 s will\n");...fp
7920: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
7930: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7940: 20 20 20 20 6c 69 6b 65 6c 79 20 62 65 20 61 20 likely be a
7950: 73 65 63 75 72 69 74 79 20 69 73 73 75 65 2c 20 security issue,
7960: 73 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 73 so this option s
7970: 68 6f 75 6c 64 20 61 6c 77 61 79 73 5c 6e 22 29 hould always\n")
7980: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
7990: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
79a0: 20 20 20 20 20 20 20 20 20 20 62 65 20 75 73 65 be use
79b0: 64 2e 5c 6e 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 d.\n");..}...ret
79c0: 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 urn;.}../* Add a
79d0: 20 67 65 74 6f 70 74 20 6f 70 74 69 6f 6e 20 2a getopt option *
79e0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 /.static void fi
79f0: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f led_getopt_long_
7a00: 73 65 74 6f 70 74 28 73 74 72 75 63 74 20 6f 70 setopt(struct op
7a10: 74 69 6f 6e 20 2a 6f 70 74 2c 20 63 6f 6e 73 74 tion *opt, const
7a20: 20 63 68 61 72 20 2a 6e 61 6d 65 2c 20 69 6e 74 char *name, int
7a30: 20 68 61 73 5f 61 72 67 2c 20 69 6e 74 20 76 61 has_arg, int va
7a40: 6c 29 20 7b 0a 09 6f 70 74 2d 3e 6e 61 6d 65 20 l) {..opt->name
7a50: 20 20 20 20 3d 20 6e 61 6d 65 3b 0a 09 6f 70 74 = name;..opt
7a60: 2d 3e 68 61 73 5f 61 72 67 20 20 3d 20 68 61 73 ->has_arg = has
7a70: 5f 61 72 67 3b 0a 09 6f 70 74 2d 3e 66 6c 61 67 _arg;..opt->flag
7a80: 20 20 20 20 20 3d 20 4e 55 4c 4c 3b 0a 09 6f 70 = NULL;..op
7a90: 74 2d 3e 76 61 6c 20 20 20 20 20 20 3d 20 76 61 t->val = va
7aa0: 6c 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a l;...return;.}..
7ab0: 2f 2a 20 52 65 73 6f 6c 76 65 20 61 20 75 73 65 /* Resolve a use
7ac0: 72 6e 61 6d 65 20 74 6f 20 61 20 55 49 44 20 2a rname to a UID *
7ad0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c /.static int fil
7ae0: 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 63 ed_user_lookup(c
7af0: 6f 6e 73 74 20 63 68 61 72 20 2a 75 73 65 72 2c onst char *user,
7b00: 20 75 69 64 5f 74 20 2a 75 73 65 72 5f 69 64 29 uid_t *user_id)
7b10: 20 7b 0a 09 63 68 61 72 20 2a 6e 65 78 74 3b 0a {..char *next;.
7b20: 09 75 69 64 5f 74 20 75 73 65 72 5f 69 64 5f 63 .uid_t user_id_c
7b30: 68 65 63 6b 3b 0a 23 69 66 6e 64 65 66 20 46 49 heck;.#ifndef FI
7b40: 4c 45 44 5f 4e 4f 5f 47 45 54 50 57 4e 41 4d 0a LED_NO_GETPWNAM.
7b50: 09 73 74 72 75 63 74 20 70 61 73 73 77 64 20 2a .struct passwd *
7b60: 65 6e 74 3b 0a 0a 09 65 6e 74 20 3d 20 67 65 74 ent;...ent = get
7b70: 70 77 6e 61 6d 28 75 73 65 72 29 3b 0a 09 69 66 pwnam(user);..if
7b80: 20 28 65 6e 74 20 21 3d 20 4e 55 4c 4c 29 20 7b (ent != NULL) {
7b90: 0a 09 09 2a 75 73 65 72 5f 69 64 20 3d 20 65 6e ...*user_id = en
7ba0: 74 2d 3e 70 77 5f 75 69 64 3b 0a 0a 09 09 72 65 t->pw_uid;....re
7bb0: 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 23 65 6e 64 turn(0);..}.#end
7bc0: 69 66 0a 0a 09 75 73 65 72 5f 69 64 5f 63 68 65 if...user_id_che
7bd0: 63 6b 20 3d 20 73 74 72 74 6f 75 6c 6c 28 75 73 ck = strtoull(us
7be0: 65 72 2c 20 26 6e 65 78 74 2c 20 31 30 29 3b 0a er, &next, 10);.
7bf0: 09 69 66 20 28 6e 65 78 74 20 3d 3d 20 4e 55 4c .if (next == NUL
7c00: 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 L) {...return(1)
7c10: 3b 0a 09 7d 0a 0a 09 69 66 20 28 6e 65 78 74 5b ;..}...if (next[
7c20: 30 5d 20 21 3d 20 27 5c 30 27 29 20 7b 0a 09 09 0] != '\0') {...
7c30: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 return(1);..}...
7c40: 2a 75 73 65 72 5f 69 64 20 3d 20 75 73 65 72 5f *user_id = user_
7c50: 69 64 5f 63 68 65 63 6b 3b 0a 0a 09 72 65 74 75 id_check;...retu
7c60: 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 61 65 rn(0);.}../* Dae
7c70: 6d 6f 6e 69 7a 65 20 2a 2f 0a 73 74 61 74 69 63 monize */.static
7c80: 20 69 6e 74 20 66 69 6c 65 64 5f 64 61 65 6d 6f int filed_daemo
7c90: 6e 69 7a 65 28 76 6f 69 64 29 20 7b 0a 09 70 69 nize(void) {..pi
7ca0: 64 5f 74 20 73 65 74 73 69 64 5f 72 65 74 2c 20 d_t setsid_ret,
7cb0: 66 6f 72 6b 5f 72 65 74 3b 0a 09 69 6e 74 20 63 fork_ret;..int c
7cc0: 68 64 69 72 5f 72 65 74 2c 20 64 75 70 32 5f 72 hdir_ret, dup2_r
7cd0: 65 74 3b 0a 09 69 6e 74 20 66 64 5f 69 6e 2c 20 et;..int fd_in,
7ce0: 66 64 5f 6f 75 74 3b 0a 0a 09 63 68 64 69 72 5f fd_out;...chdir_
7cf0: 72 65 74 20 3d 20 63 68 64 69 72 28 22 2f 22 29 ret = chdir("/")
7d00: 3b 0a 09 69 66 20 28 63 68 64 69 72 5f 72 65 74 ;..if (chdir_ret
7d10: 20 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 != 0) {...retur
7d20: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 6b 5f n(1);..}...fork_
7d30: 72 65 74 20 3d 20 66 6f 72 6b 28 29 3b 0a 09 69 ret = fork();..i
7d40: 66 20 28 66 6f 72 6b 5f 72 65 74 20 3c 20 30 29 f (fork_ret < 0)
7d50: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a {...return(1);.
7d60: 09 7d 0a 0a 09 69 66 20 28 66 6f 72 6b 5f 72 65 .}...if (fork_re
7d70: 74 20 3e 20 30 29 20 7b 0a 09 09 2f 2a 20 50 61 t > 0) {.../* Pa
7d80: 72 65 6e 74 20 2a 2f 0a 09 09 77 61 69 74 70 69 rent */...waitpi
7d90: 64 28 66 6f 72 6b 5f 72 65 74 2c 20 4e 55 4c 4c d(fork_ret, NULL
7da0: 2c 20 30 29 3b 0a 0a 09 09 65 78 69 74 28 45 58 , 0);....exit(EX
7db0: 49 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a IT_SUCCESS);..}.
7dc0: 0a 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 69 ../* Child */..i
7dd0: 66 20 28 66 6f 72 6b 28 29 20 21 3d 20 30 29 20 f (fork() != 0)
7de0: 7b 0a 09 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a {.../* Child */.
7df0: 09 09 65 78 69 74 28 45 58 49 54 5f 53 55 43 43 ..exit(EXIT_SUCC
7e00: 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 47 72 ESS);..}.../* Gr
7e10: 61 6e 64 20 63 68 69 6c 64 20 2a 2f 0a 09 73 65 and child */..se
7e20: 74 73 69 64 5f 72 65 74 20 3d 20 73 65 74 73 69 tsid_ret = setsi
7e30: 64 28 29 3b 0a 09 69 66 20 28 73 65 74 73 69 64 d();..if (setsid
7e40: 5f 72 65 74 20 3d 3d 20 28 28 70 69 64 5f 74 29 _ret == ((pid_t)
7e50: 20 2d 31 29 29 20 7b 0a 09 09 72 65 74 75 72 6e -1)) {...return
7e60: 28 31 29 3b 0a 09 7d 0a 0a 09 66 64 5f 69 6e 20 (1);..}...fd_in
7e70: 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c = open("/dev/nul
7e80: 6c 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 09 l", O_RDONLY);..
7e90: 66 64 5f 6f 75 74 20 3d 20 6f 70 65 6e 28 22 2f fd_out = open("/
7ea0: 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 52 4f dev/null", O_WRO
7eb0: 4e 4c 59 29 3b 0a 09 69 66 20 28 66 64 5f 69 6e NLY);..if (fd_in
7ec0: 20 3c 20 30 20 7c 7c 20 66 64 5f 6f 75 74 20 3c < 0 || fd_out <
7ed0: 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 0) {...return(1
7ee0: 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 );..}...dup2_ret
7ef0: 20 3d 20 64 75 70 32 28 66 64 5f 69 6e 2c 20 53 = dup2(fd_in, S
7f00: 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 TDIN_FILENO);..i
7f10: 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53 f (dup2_ret != S
7f20: 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 TDIN_FILENO) {..
7f30: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a .return(1);..}..
7f40: 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32 .dup2_ret = dup2
7f50: 28 66 64 5f 6f 75 74 2c 20 53 54 44 4f 55 54 5f (fd_out, STDOUT_
7f60: 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 FILENO);..if (du
7f70: 70 32 5f 72 65 74 20 21 3d 20 53 54 44 4f 55 54 p2_ret != STDOUT
7f80: 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 _FILENO) {...ret
7f90: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 urn(1);..}...dup
7fa0: 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64 5f 2_ret = dup2(fd_
7fb0: 6f 75 74 2c 20 53 54 44 45 52 52 5f 46 49 4c 45 out, STDERR_FILE
7fc0: 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 NO);..if (dup2_r
7fd0: 65 74 20 21 3d 20 53 54 44 45 52 52 5f 46 49 4c et != STDERR_FIL
7fe0: 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 ENO) {...return(
7ff0: 31 29 3b 0a 09 7d 0a 0a 09 63 6c 6f 73 65 28 66 1);..}...close(f
8000: 64 5f 69 6e 29 3b 0a 09 63 6c 6f 73 65 28 66 64 d_in);..close(fd
8010: 5f 6f 75 74 29 3b 0a 0a 09 72 65 74 75 72 6e 28 _out);...return(
8020: 30 29 3b 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 70 72 0);.}../* Run pr
8030: 6f 63 65 73 73 20 2a 2f 0a 69 6e 74 20 6d 61 69 ocess */.int mai
8040: 6e 28 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 n(int argc, char
8050: 20 2a 2a 61 72 67 76 29 20 7b 0a 09 73 74 72 75 **argv) {..stru
8060: 63 74 20 6f 70 74 69 6f 6e 20 6f 70 74 69 6f 6e ct option option
8070: 73 5b 31 32 5d 3b 0a 09 73 74 72 75 63 74 20 66 s[12];..struct f
8080: 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 74 68 72 iled_options thr
8090: 65 61 64 5f 6f 70 74 69 6f 6e 73 3b 0a 09 63 6f ead_options;..co
80a0: 6e 73 74 20 63 68 61 72 20 2a 62 69 6e 64 5f 61 nst char *bind_a
80b0: 64 64 72 20 3d 20 42 49 4e 44 5f 41 44 44 52 2c ddr = BIND_ADDR,
80c0: 20 2a 6e 65 77 72 6f 6f 74 20 3d 20 4e 55 4c 4c *newroot = NULL
80d0: 2c 20 2a 6c 6f 67 5f 66 69 6c 65 20 3d 20 4c 4f , *log_file = LO
80e0: 47 5f 46 49 4c 45 3b 0a 09 46 49 4c 45 20 2a 6c G_FILE;..FILE *l
80f0: 6f 67 5f 66 70 3b 0a 09 75 69 64 5f 74 20 75 73 og_fp;..uid_t us
8100: 65 72 20 3d 20 30 3b 0a 09 69 6e 74 20 70 6f 72 er = 0;..int por
8110: 74 20 3d 20 50 4f 52 54 2c 20 74 68 72 65 61 64 t = PORT, thread
8120: 5f 63 6f 75 6e 74 20 3d 20 54 48 52 45 41 44 5f _count = THREAD_
8130: 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 61 63 68 COUNT;..int cach
8140: 65 5f 73 69 7a 65 20 3d 20 43 41 43 48 45 5f 53 e_size = CACHE_S
8150: 49 5a 45 3b 0a 09 69 6e 74 20 69 6e 69 74 5f 72 IZE;..int init_r
8160: 65 74 2c 20 63 68 72 6f 6f 74 5f 72 65 74 2c 20 et, chroot_ret,
8170: 73 65 74 75 69 64 5f 72 65 74 2c 20 6c 6f 6f 6b setuid_ret, look
8180: 75 70 5f 72 65 74 2c 20 63 68 64 69 72 5f 72 65 up_ret, chdir_re
8190: 74 3b 0a 09 69 6e 74 20 73 65 74 75 69 64 5f 65 t;..int setuid_e
81a0: 6e 61 62 6c 65 64 20 3d 20 30 2c 20 64 61 65 6d nabled = 0, daem
81b0: 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 30 3b 0a on_enabled = 0;.
81c0: 09 69 6e 74 20 63 68 3b 0a 09 69 6e 74 20 66 64 .int ch;..int fd
81d0: 3b 0a 0a 09 2f 2a 20 53 65 74 20 64 65 66 61 75 ;.../* Set defau
81e0: 6c 74 20 76 61 6c 75 65 73 20 2a 2f 0a 09 74 68 lt values */..th
81f0: 72 65 61 64 5f 6f 70 74 69 6f 6e 73 2e 76 68 6f read_options.vho
8200: 73 74 73 5f 65 6e 61 62 6c 65 64 20 3d 20 30 3b sts_enabled = 0;
8210: 0a 0a 09 2f 2a 20 50 72 6f 63 65 73 73 20 61 72 .../* Process ar
8220: 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 66 69 6c 65 guments */..file
8230: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 d_getopt_long_se
8240: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 30 5d topt(&options[0]
8250: 2c 20 22 70 6f 72 74 22 2c 20 72 65 71 75 69 72 , "port", requir
8260: 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 70 27 ed_argument, 'p'
8270: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 );..filed_getopt
8280: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 _long_setopt(&op
8290: 74 69 6f 6e 73 5b 31 5d 2c 20 22 74 68 72 65 61 tions[1], "threa
82a0: 64 73 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 ds", required_ar
82b0: 67 75 6d 65 6e 74 2c 20 27 74 27 29 3b 0a 09 66 gument, 't');..f
82c0: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
82d0: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
82e0: 5b 32 5d 2c 20 22 63 61 63 68 65 22 2c 20 72 65 [2], "cache", re
82f0: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c quired_argument,
8300: 20 27 63 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 'c');..filed_ge
8310: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 topt_long_setopt
8320: 28 26 6f 70 74 69 6f 6e 73 5b 33 5d 2c 20 22 62 (&options[3], "b
8330: 69 6e 64 22 2c 20 72 65 71 75 69 72 65 64 5f 61 ind", required_a
8340: 72 67 75 6d 65 6e 74 2c 20 27 62 27 29 3b 0a 09 rgument, 'b');..
8350: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
8360: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
8370: 73 5b 34 5d 2c 20 22 75 73 65 72 22 2c 20 72 65 s[4], "user", re
8380: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c quired_argument,
8390: 20 27 75 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 'u');..filed_ge
83a0: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 topt_long_setopt
83b0: 28 26 6f 70 74 69 6f 6e 73 5b 35 5d 2c 20 22 72 (&options[5], "r
83c0: 6f 6f 74 22 2c 20 72 65 71 75 69 72 65 64 5f 61 oot", required_a
83d0: 72 67 75 6d 65 6e 74 2c 20 27 72 27 29 3b 0a 09 rgument, 'r');..
83e0: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
83f0: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
8400: 73 5b 36 5d 2c 20 22 68 65 6c 70 22 2c 20 6e 6f s[6], "help", no
8410: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 68 27 29 3b _argument, 'h');
8420: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c ..filed_getopt_l
8430: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 ong_setopt(&opti
8440: 6f 6e 73 5b 37 5d 2c 20 22 64 61 65 6d 6f 6e 22 ons[7], "daemon"
8450: 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 , no_argument, '
8460: 64 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f d');..filed_geto
8470: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
8480: 6f 70 74 69 6f 6e 73 5b 38 5d 2c 20 22 6c 6f 67 options[8], "log
8490: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 ", required_argu
84a0: 6d 65 6e 74 2c 20 27 6c 27 29 3b 0a 09 66 69 6c ment, 'l');..fil
84b0: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 ed_getopt_long_s
84c0: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 39 etopt(&options[9
84d0: 5d 2c 20 22 76 65 72 73 69 6f 6e 22 2c 20 6e 6f ], "version", no
84e0: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 76 27 29 3b _argument, 'v');
84f0: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c ..filed_getopt_l
8500: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 ong_setopt(&opti
8510: 6f 6e 73 5b 31 30 5d 2c 20 22 76 68 6f 73 74 22 ons[10], "vhost"
8520: 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 , no_argument, '
8530: 56 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f V');..filed_geto
8540: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
8550: 6f 70 74 69 6f 6e 73 5b 31 31 5d 2c 20 4e 55 4c options[11], NUL
8560: 4c 2c 20 30 2c 20 30 29 3b 0a 09 77 68 69 6c 65 L, 0, 0);..while
8570: 20 28 28 63 68 20 3d 20 67 65 74 6f 70 74 5f 6c ((ch = getopt_l
8580: 6f 6e 67 28 61 72 67 63 2c 20 61 72 67 76 2c 20 ong(argc, argv,
8590: 22 70 3a 74 3a 63 3a 62 3a 75 3a 72 3a 6c 3a 68 "p:t:c:b:u:r:l:h
85a0: 64 76 56 22 2c 20 6f 70 74 69 6f 6e 73 2c 20 4e dvV", options, N
85b0: 55 4c 4c 29 29 20 21 3d 20 2d 31 29 20 7b 0a 09 ULL)) != -1) {..
85c0: 09 73 77 69 74 63 68 28 63 68 29 20 7b 0a 09 09 .switch(ch) {...
85d0: 09 63 61 73 65 20 27 70 27 3a 0a 09 09 09 09 70 .case 'p':.....p
85e0: 6f 72 74 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 ort = atoi(optar
85f0: 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 g);.....break;..
8600: 09 09 63 61 73 65 20 27 74 27 3a 0a 09 09 09 09 ..case 't':.....
8610: 74 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d 20 61 thread_count = a
8620: 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 toi(optarg);....
8630: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
8640: 27 63 27 3a 0a 09 09 09 09 63 61 63 68 65 5f 73 'c':.....cache_s
8650: 69 7a 65 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 ize = atoi(optar
8660: 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 g);.....break;..
8670: 09 09 63 61 73 65 20 27 62 27 3a 0a 09 09 09 09 ..case 'b':.....
8680: 62 69 6e 64 5f 61 64 64 72 20 3d 20 73 74 72 64 bind_addr = strd
8690: 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 up(optarg);.....
86a0: 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 break;....case '
86b0: 75 27 3a 0a 09 09 09 09 73 65 74 75 69 64 5f 65 u':.....setuid_e
86c0: 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 nabled = 1;.....
86d0: 6c 6f 6f 6b 75 70 5f 72 65 74 20 3d 20 66 69 6c lookup_ret = fil
86e0: 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 6f ed_user_lookup(o
86f0: 70 74 61 72 67 2c 20 26 75 73 65 72 29 3b 0a 09 ptarg, &user);..
8700: 09 09 09 69 66 20 28 6c 6f 6f 6b 75 70 5f 72 65 ...if (lookup_re
8710: 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 09 09 66 t != 0) {......f
8720: 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 iled_print_help(
8730: 73 74 64 65 72 72 2c 20 30 2c 20 22 49 6e 76 61 stderr, 0, "Inva
8740: 6c 69 64 20 75 73 65 72 6e 61 6d 65 20 73 70 65 lid username spe
8750: 63 69 66 69 65 64 22 29 3b 0a 0a 09 09 09 09 09 cified");.......
8760: 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 09 09 7d return(1);.....}
8770: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 .....break;....c
8780: 61 73 65 20 27 72 27 3a 0a 09 09 09 09 6e 65 77 ase 'r':.....new
8790: 72 6f 6f 74 20 3d 20 73 74 72 64 75 70 28 6f 70 root = strdup(op
87a0: 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b targ);.....break
87b0: 3b 0a 09 09 09 63 61 73 65 20 27 6c 27 3a 0a 09 ;....case 'l':..
87c0: 09 09 09 6c 6f 67 5f 66 69 6c 65 20 3d 20 73 74 ...log_file = st
87d0: 72 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 rdup(optarg);...
87e0: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
87f0: 20 27 64 27 3a 0a 09 09 09 09 64 61 65 6d 6f 6e 'd':.....daemon
8800: 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 _enabled = 1;...
8810: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
8820: 20 27 56 27 3a 0a 09 09 09 09 74 68 72 65 61 64 'V':.....thread
8830: 5f 6f 70 74 69 6f 6e 73 2e 76 68 6f 73 74 73 5f _options.vhosts_
8840: 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 0a 09 09 enabled = 1;....
8850: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
8860: 20 27 76 27 3a 0a 09 09 09 09 70 72 69 6e 74 66 'v':.....printf
8870: 28 22 66 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 ("filed version
8880: 25 73 5c 6e 22 2c 20 46 49 4c 45 44 5f 56 45 52 %s\n", FILED_VER
8890: 53 49 4f 4e 29 3b 0a 0a 09 09 09 09 72 65 74 75 SION);......retu
88a0: 72 6e 28 30 29 3b 0a 09 09 09 63 61 73 65 20 27 rn(0);....case '
88b0: 3f 27 3a 0a 09 09 09 63 61 73 65 20 27 3a 27 3a ?':....case ':':
88c0: 0a 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 .....filed_print
88d0: 5f 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c _help(stderr, 0,
88e0: 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 NULL);......ret
88f0: 75 72 6e 28 31 29 3b 0a 09 09 09 63 61 73 65 20 urn(1);....case
8900: 27 68 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 'h':.....filed_p
8910: 72 69 6e 74 5f 68 65 6c 70 28 73 74 64 6f 75 74 rint_help(stdout
8920: 2c 20 31 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 , 1, NULL);.....
8930: 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 7d 0a .return(0);...}.
8940: 09 7d 0a 0a 09 2f 2a 20 4f 70 65 6e 20 6c 6f 67 .}.../* Open log
8950: 20 66 69 6c 65 20 2a 2f 0a 09 6c 6f 67 5f 66 70 file */..log_fp
8960: 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 = filed_log_ope
8970: 6e 28 6c 6f 67 5f 66 69 6c 65 29 3b 0a 09 69 66 n(log_file);..if
8980: 20 28 6c 6f 67 5f 66 70 20 3d 3d 20 4e 55 4c 4c (log_fp == NULL
8990: 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 ) {...perror("fi
89a0: 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 22 29 3b 0a led_log_open");.
89b0: 0a 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d ...return(4);..}
89c0: 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 69 73 .../* Create lis
89d0: 74 65 6e 69 6e 67 20 73 6f 63 6b 65 74 20 2a 2f tening socket */
89e0: 0a 09 66 64 20 3d 20 66 69 6c 65 64 5f 6c 69 73 ..fd = filed_lis
89f0: 74 65 6e 28 62 69 6e 64 5f 61 64 64 72 2c 20 70 ten(bind_addr, p
8a00: 6f 72 74 29 3b 0a 09 69 66 20 28 66 64 20 3c 20 ort);..if (fd <
8a10: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 0) {...perror("f
8a20: 69 6c 65 64 5f 6c 69 73 74 65 6e 22 29 3b 0a 0a iled_listen");..
8a30: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
8a40: 0a 09 2f 2a 20 42 65 63 6f 6d 65 20 61 20 64 61 ../* Become a da
8a50: 65 6d 6f 6e 20 2a 2f 0a 09 69 66 20 28 64 61 65 emon */..if (dae
8a60: 6d 6f 6e 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 mon_enabled) {..
8a70: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 .init_ret = file
8a80: 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 29 3b 0a 09 d_daemonize();..
8a90: 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d .if (init_ret !=
8aa0: 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 0) {....perror(
8ab0: 22 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 "filed_daemonize
8ac0: 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 36 ");.....return(6
8ad0: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 43 );...}..}.../* C
8ae0: 68 72 6f 6f 74 2c 20 69 66 20 61 70 70 72 6f 70 hroot, if approp
8af0: 72 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 6e 65 riate */..if (ne
8b00: 77 72 6f 6f 74 29 20 7b 0a 09 09 63 68 64 69 72 wroot) {...chdir
8b10: 5f 72 65 74 20 3d 20 63 68 64 69 72 28 6e 65 77 _ret = chdir(new
8b20: 72 6f 6f 74 29 3b 0a 09 09 69 66 20 28 63 68 64 root);...if (chd
8b30: 69 72 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 ir_ret != 0) {..
8b40: 09 09 70 65 72 72 6f 72 28 22 63 68 64 69 72 22 ..perror("chdir"
8b50: 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 );.....return(1)
8b60: 3b 0a 09 09 7d 0a 0a 09 09 63 68 72 6f 6f 74 5f ;...}....chroot_
8b70: 72 65 74 20 3d 20 63 68 72 6f 6f 74 28 22 2e 22 ret = chroot("."
8b80: 29 3b 0a 09 09 69 66 20 28 63 68 72 6f 6f 74 5f );...if (chroot_
8b90: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 ret != 0) {....p
8ba0: 65 72 72 6f 72 28 22 63 68 72 6f 6f 74 22 29 3b error("chroot");
8bb0: 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a .....return(1);.
8bc0: 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 72 6f 70 ..}..}.../* Drop
8bd0: 20 70 72 69 76 69 6c 65 67 65 73 2c 20 69 66 20 privileges, if
8be0: 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09 appropriate */..
8bf0: 69 66 20 28 73 65 74 75 69 64 5f 65 6e 61 62 6c if (setuid_enabl
8c00: 65 64 29 20 7b 0a 09 09 73 65 74 75 69 64 5f 72 ed) {...setuid_r
8c10: 65 74 20 3d 20 73 65 74 75 69 64 28 75 73 65 72 et = setuid(user
8c20: 29 3b 0a 09 09 69 66 20 28 73 65 74 75 69 64 5f );...if (setuid_
8c30: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 ret != 0) {....p
8c40: 65 72 72 6f 72 28 22 73 65 74 75 69 64 22 29 3b error("setuid");
8c50: 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a .....return(1);.
8c60: 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 6f 20 6e ..}..}.../* Do n
8c70: 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 70 72 69 ot allow any pri
8c80: 76 69 6c 65 67 65 20 63 68 61 6e 67 65 73 20 62 vilege changes b
8c90: 65 79 6f 6e 64 20 74 68 69 73 20 70 6f 69 6e 74 eyond this point
8ca0: 20 2a 2f 0a 09 70 72 63 74 6c 28 50 52 5f 53 45 */..prctl(PR_SE
8cb0: 54 5f 4e 4f 5f 4e 45 57 5f 50 52 49 56 53 2c 20 T_NO_NEW_PRIVS,
8cc0: 31 29 3b 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 1);.../* Initial
8cd0: 69 7a 65 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 ize */..init_ret
8ce0: 20 3d 20 66 69 6c 65 64 5f 69 6e 69 74 28 63 61 = filed_init(ca
8cf0: 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 che_size);..if (
8d00: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b init_ret != 0) {
8d10: 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 ...perror("filed
8d20: 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 _init");....retu
8d30: 72 6e 28 33 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 rn(3);..}.../* C
8d40: 72 65 61 74 65 20 6c 6f 67 67 69 6e 67 20 74 68 reate logging th
8d50: 72 65 61 64 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 read */..init_re
8d60: 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e t = filed_loggin
8d70: 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 6c 6f g_thread_init(lo
8d80: 67 5f 66 70 29 3b 0a 09 69 66 20 28 69 6e 69 74 g_fp);..if (init
8d90: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 _ret != 0) {...p
8da0: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 6f 67 error("filed_log
8db0: 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 ging_thread_init
8dc0: 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 34 29 ");....return(4)
8dd0: 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 ;..}.../* Create
8de0: 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 worker threads
8df0: 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 */..init_ret = f
8e00: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 iled_worker_thre
8e10: 61 64 73 5f 69 6e 69 74 28 66 64 2c 20 74 68 72 ads_init(fd, thr
8e20: 65 61 64 5f 63 6f 75 6e 74 2c 20 26 74 68 72 65 ead_count, &thre
8e30: 61 64 5f 6f 70 74 69 6f 6e 73 29 3b 0a 09 69 66 ad_options);..if
8e40: 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 (init_ret != 0)
8e50: 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c {...perror("fil
8e60: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 ed_worker_thread
8e70: 73 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 s_init");....ret
8e80: 75 72 6e 28 35 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 urn(5);..}.../*
8e90: 57 61 69 74 20 66 6f 72 20 74 68 72 65 61 64 73 Wait for threads
8ea0: 20 74 6f 20 65 78 69 74 20 2a 2f 0a 09 2f 2a 20 to exit */../*
8eb0: 58 58 58 3a 54 4f 44 4f 3a 20 4d 6f 6e 69 74 6f XXX:TODO: Monito
8ec0: 72 20 74 68 72 65 61 64 20 75 73 61 67 65 20 2a r thread usage *
8ed0: 2f 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 /..while (1) {..
8ee0: 09 73 6c 65 65 70 28 36 30 29 3b 0a 09 7d 0a 0a .sleep(60);..}..
8ef0: 09 2f 2a 20 52 65 74 75 72 6e 20 69 6e 20 66 61 ./* Return in fa
8f00: 69 6c 75 72 65 20 2a 2f 0a 09 72 65 74 75 72 6e ilure */..return
8f10: 28 32 29 3b 0a 7d 0a (2);.}.