0000: 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 65 #include <sys/se
0010: 6e 64 66 69 6c 65 2e 68 3e 0a 23 69 6e 63 6c 75 ndfile.h>.#inclu
0020: 64 65 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68 de <sys/socket.h
0030: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f >.#include <sys/
0040: 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 types.h>.#includ
0050: 65 20 3c 61 72 70 61 2f 69 6e 65 74 2e 68 3e 0a e <arpa/inet.h>.
0060: 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d #include <sys/mm
0070: 61 6e 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c an.h>.#include <
0080: 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 sys/stat.h>.#inc
0090: 6c 75 64 65 20 3c 73 79 73 2f 77 61 69 74 2e 68 lude <sys/wait.h
00a0: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 >.#include <pthr
00b0: 65 61 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 ead.h>.#include
00c0: 3c 73 74 72 69 6e 67 73 2e 68 3e 0a 23 69 6e 63 <strings.h>.#inc
00d0: 6c 75 64 65 20 3c 73 69 67 6e 61 6c 2e 68 3e 0a lude <signal.h>.
00e0: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 #include <stdlib
00f0: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e .h>.#include <un
0100: 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 istd.h>.#include
0110: 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 <string.h>.#inc
0120: 6c 75 64 65 20 3c 67 65 74 6f 70 74 2e 68 3e 0a lude <getopt.h>.
0130: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 #include <stdarg
0140: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 .h>.#include <fc
0150: 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 ntl.h>.#include
0160: 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 <stdio.h>.#inclu
0170: 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 23 69 6e de <errno.h>.#in
0180: 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 23 clude <time.h>.#
0190: 69 6e 63 6c 75 64 65 20 3c 70 77 64 2e 68 3e 0a include <pwd.h>.
01a0: 0a 2f 2a 20 43 6f 6d 70 69 6c 65 20 74 69 6d 65 ./* Compile time
01b0: 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 23 64 constants */.#d
01c0: 65 66 69 6e 65 20 46 49 4c 45 44 5f 56 45 52 53 efine FILED_VERS
01d0: 49 4f 4e 20 22 31 2e 31 22 0a 23 64 65 66 69 6e ION "1.1".#defin
01e0: 65 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 e FILED_SENDFILE
01f0: 5f 4d 41 58 20 31 36 37 37 37 32 31 35 0a 23 64 _MAX 16777215.#d
0200: 65 66 69 6e 65 20 46 49 4c 45 44 5f 4d 41 58 5f efine FILED_MAX_
0210: 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 20 33 30 FAILURE_COUNT 30
0220: 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 44 .#define FILED_D
0230: 45 46 41 55 4c 54 5f 54 59 50 45 20 22 61 70 70 EFAULT_TYPE "app
0240: 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d 73 lication/octet-s
0250: 74 72 65 61 6d 22 0a 23 64 65 66 69 6e 65 20 46 tream".#define F
0260: 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 ILED_PATH_BUFFER
0270: 5f 53 49 5a 45 20 31 30 31 30 0a 0a 2f 2a 20 44 _SIZE 1010../* D
0280: 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f efault values */
0290: 0a 23 64 65 66 69 6e 65 20 50 4f 52 54 20 38 30 .#define PORT 80
02a0: 0a 23 64 65 66 69 6e 65 20 54 48 52 45 41 44 5f .#define THREAD_
02b0: 43 4f 55 4e 54 20 35 0a 23 64 65 66 69 6e 65 20 COUNT 5.#define
02c0: 42 49 4e 44 5f 41 44 44 52 20 22 3a 3a 22 0a 23 BIND_ADDR "::".#
02d0: 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 49 5a define CACHE_SIZ
02e0: 45 20 38 32 30 39 0a 23 64 65 66 69 6e 65 20 4c E 8209.#define L
02f0: 4f 47 5f 46 49 4c 45 20 22 2d 22 0a 0a 2f 2a 20 OG_FILE "-"../*
0300: 41 72 67 75 6d 65 6e 74 73 20 66 6f 72 20 77 6f Arguments for wo
0310: 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a 2f 0a rker threads */.
0320: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 struct filed_wor
0330: 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 73 20 ker_thread_args
0340: 7b 0a 09 69 6e 74 20 66 64 3b 0a 7d 3b 0a 0a 2f {..int fd;.};../
0350: 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72 20 * Arguments for
0360: 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64 73 20 logging threads
0370: 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f */.struct filed_
0380: 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 logging_thread_a
0390: 72 67 73 20 7b 0a 09 46 49 4c 45 20 2a 66 70 3b rgs {..FILE *fp;
03a0: 0a 7d 3b 0a 0a 2f 2a 20 46 69 6c 65 20 69 6e 66 .};../* File inf
03b0: 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74 72 75 ormation */.stru
03c0: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 ct filed_fileinf
03d0: 6f 20 7b 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 o {..pthread_mut
03e0: 65 78 5f 74 20 6d 75 74 65 78 3b 0a 09 63 68 61 ex_t mutex;..cha
03f0: 72 20 2a 70 61 74 68 3b 0a 09 69 6e 74 20 66 64 r *path;..int fd
0400: 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09 63 ;..off_t len;..c
0410: 68 61 72 20 2a 6c 61 73 74 6d 6f 64 3b 0a 09 63 har *lastmod;..c
0420: 68 61 72 20 6c 61 73 74 6d 6f 64 5f 62 5b 36 34 har lastmod_b[64
0430: 5d 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a ];..const char *
0440: 74 79 70 65 3b 0a 7d 3b 0a 0a 2f 2a 20 52 65 71 type;.};../* Req
0450: 75 65 73 74 20 76 61 72 69 61 62 6c 65 73 20 2a uest variables *
0460: 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 /.struct filed_h
0470: 74 74 70 5f 72 65 71 75 65 73 74 20 7b 0a 09 2f ttp_request {../
0480: 2a 2a 20 42 75 66 66 65 72 73 20 2a 2a 2f 0a 09 ** Buffers **/..
0490: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c struct filed_fil
04a0: 65 69 6e 66 6f 20 66 69 6c 65 69 6e 66 6f 3b 0a einfo fileinfo;.
04b0: 09 63 68 61 72 20 70 61 74 68 5f 62 5b 46 49 4c .char path_b[FIL
04c0: 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 ED_PATH_BUFFER_S
04d0: 49 5a 45 5d 3b 0a 09 63 68 61 72 20 74 6d 70 62 IZE];..char tmpb
04e0: 75 66 5b 31 30 31 30 5d 3b 0a 0a 09 2f 2a 2a 20 uf[1010];.../**
04f0: 48 54 54 50 20 52 65 71 75 65 73 74 20 69 6e 66 HTTP Request inf
0500: 6f 72 6d 61 74 69 6f 6e 20 2a 2a 2f 0a 09 63 68 ormation **/..ch
0510: 61 72 20 2a 70 61 74 68 3b 20 20 20 20 20 2f 2a ar *path; /*
0520: 2a 2a 20 50 61 74 68 20 62 65 69 6e 67 20 72 65 ** Path being re
0530: 71 75 65 73 74 65 64 20 2a 2a 2a 2f 0a 0a 09 73 quested ***/...s
0540: 74 72 75 63 74 20 7b 0a 09 09 73 74 72 75 63 74 truct {...struct
0550: 20 7b 0a 09 09 09 69 6e 74 20 70 72 65 73 65 6e {....int presen
0560: 74 3b 0a 09 09 09 6f 66 66 5f 74 20 6f 66 66 73 t;....off_t offs
0570: 65 74 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67 65 et; /*** Range
0580: 20 73 74 61 72 74 20 2a 2a 2a 2f 0a 09 09 09 6f start ***/....o
0590: 66 66 5f 74 20 6c 65 6e 67 74 68 3b 20 20 20 2f ff_t length; /
05a0: 2a 2a 2a 20 52 61 6e 67 65 20 6c 65 6e 67 74 68 *** Range length
05b0: 20 2a 2a 2a 2f 0a 09 09 7d 20 72 61 6e 67 65 3b ***/...} range;
05c0: 0a 09 7d 20 68 65 61 64 65 72 73 3b 0a 7d 3b 0a ..} headers;.};.
05d0: 0a 2f 2a 20 4c 6f 67 20 72 65 63 6f 72 64 20 2a ./* Log record *
05e0: 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c /.struct filed_l
05f0: 6f 67 5f 65 6e 74 72 79 20 7b 0a 09 2f 2a 20 54 og_entry {../* T
0600: 79 70 65 20 6f 66 20 6c 6f 67 20 65 6e 74 72 79 ype of log entry
0610: 20 2a 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 */..enum {...FI
0620: 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 LED_LOG_TYPE_MES
0630: 53 41 47 45 2c 0a 09 09 46 49 4c 45 44 5f 4c 4f SAGE,...FILED_LO
0640: 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 52 0a G_TYPE_TRANSFER.
0650: 09 7d 20 74 79 70 65 3b 0a 0a 09 2f 2a 20 4c 69 .} type;.../* Li
0660: 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 2f 74 nked list head/t
0670: 61 69 6c 20 2a 2f 0a 09 73 74 72 75 63 74 20 66 ail */..struct f
0680: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
0690: 5f 6e 65 78 74 3b 0a 09 73 74 72 75 63 74 20 66 _next;..struct f
06a0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
06b0: 5f 70 72 65 76 3b 0a 0a 09 2f 2a 20 54 68 72 65 _prev;.../* Thre
06c0: 61 64 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68 ad from which th
06d0: 69 73 20 6c 6f 67 20 65 6e 74 72 79 20 65 6d 69 is log entry emi
06e0: 6e 61 74 65 73 20 2a 2f 0a 09 70 74 68 72 65 61 nates */..pthrea
06f0: 64 5f 74 20 74 68 72 65 61 64 3b 0a 0a 09 2f 2a d_t thread;.../*
0700: 20 4d 65 73 73 61 67 65 20 62 75 66 66 65 72 20 Message buffer
0710: 66 6f 72 20 74 79 70 65 20 3d 20 4d 45 53 53 41 for type = MESSA
0720: 47 45 20 2a 2f 0a 09 2f 2a 20 50 61 74 68 20 62 GE */../* Path b
0730: 75 66 66 65 72 20 66 6f 72 20 74 79 70 65 20 3d uffer for type =
0740: 20 54 52 41 4e 53 46 45 52 20 2a 2f 0a 09 63 68 TRANSFER */..ch
0750: 61 72 20 62 75 66 66 65 72 5b 46 49 4c 45 44 5f ar buffer[FILED_
0760: 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 PATH_BUFFER_SIZE
0770: 5d 3b 0a 0a 09 2f 2a 20 49 74 65 6d 73 20 66 6f ];.../* Items fo
0780: 72 20 74 79 70 65 20 3d 20 54 52 41 4e 53 46 45 r type = TRANSFE
0790: 52 20 2a 2f 0a 09 69 6e 74 20 68 74 74 70 5f 63 R */..int http_c
07a0: 6f 64 65 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 ode;..const char
07b0: 20 2a 72 65 61 73 6f 6e 3b 0a 09 74 69 6d 65 5f *reason;..time_
07c0: 74 20 73 74 61 72 74 74 69 6d 65 3b 0a 09 74 69 t starttime;..ti
07d0: 6d 65 5f 74 20 65 6e 64 74 69 6d 65 3b 0a 09 6f me_t endtime;..o
07e0: 66 66 5f 74 20 72 65 71 5f 6f 66 66 73 65 74 3b ff_t req_offset;
07f0: 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6c 65 6e 67 ..off_t req_leng
0800: 74 68 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 74 5f th;..off_t sent_
0810: 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 5f 74 20 66 length;..off_t f
0820: 69 6c 65 5f 6c 65 6e 67 74 68 3b 0a 09 63 68 61 ile_length;..cha
0830: 72 20 69 70 5b 31 32 38 5d 3b 0a 09 69 6e 74 20 r ip[128];..int
0840: 70 6f 72 74 3b 0a 7d 3b 0a 0a 2f 2a 20 47 6c 6f port;.};../* Glo
0850: 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 2a 2f bal variables */
0860: 0a 2f 2a 2a 20 4f 70 65 6e 20 46 69 6c 65 20 63 ./** Open File c
0870: 61 63 68 65 20 2a 2a 2f 0a 73 74 72 75 63 74 20 ache **/.struct
0880: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a filed_fileinfo *
0890: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
08a0: 64 63 61 63 68 65 20 3d 20 4e 55 4c 4c 3b 0a 75 dcache = NULL;.u
08b0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 nsigned int file
08c0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
08d0: 68 65 5f 73 69 7a 65 20 3d 20 30 3b 0a 0a 2f 2a he_size = 0;../*
08e0: 2a 20 4c 6f 67 67 69 6e 67 20 2a 2a 2f 0a 73 74 * Logging **/.st
08f0: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 ruct filed_log_e
0900: 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f ntry *filed_log_
0910: 6d 73 67 5f 6c 69 73 74 3b 0a 70 74 68 72 65 61 msg_list;.pthrea
0920: 64 5f 6d 75 74 65 78 5f 74 20 66 69 6c 65 64 5f d_mutex_t filed_
0930: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 log_msg_list_mut
0940: 65 78 3b 0a 70 74 68 72 65 61 64 5f 63 6f 6e 64 ex;.pthread_cond
0950: 5f 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 _t filed_log_msg
0960: 5f 6c 69 73 74 5f 72 65 61 64 79 3b 0a 0a 2f 2a _list_ready;../*
0970: 20 49 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 Initialize cach
0980: 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 e */.static int
0990: 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 65 filed_init_cache
09a0: 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 (unsigned int ca
09b0: 63 68 65 5f 73 69 7a 65 29 20 7b 0a 09 75 6e 73 che_size) {..uns
09c0: 69 67 6e 65 64 20 69 6e 74 20 69 64 78 3b 0a 09 igned int idx;..
09d0: 69 6e 74 20 6d 75 74 65 78 5f 69 6e 69 74 5f 72 int mutex_init_r
09e0: 65 74 3b 0a 0a 09 2f 2a 20 43 61 63 68 65 20 6d et;.../* Cache m
09f0: 61 79 20 6e 6f 74 20 62 65 20 72 65 2d 69 6e 69 ay not be re-ini
0a00: 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 09 69 66 20 tialized */..if
0a10: 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f (filed_fileinfo_
0a20: 66 64 63 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 fdcache_size !=
0a30: 30 20 7c 7c 20 66 69 6c 65 64 5f 66 69 6c 65 69 0 || filed_filei
0a40: 6e 66 6f 5f 66 64 63 61 63 68 65 20 21 3d 20 4e nfo_fdcache != N
0a50: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 ULL) {...return(
0a60: 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 41 6c 6c 6f 1);..}.../* Allo
0a70: 63 61 74 65 20 63 61 63 68 65 20 2a 2f 0a 09 66 cate cache */..f
0a80: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
0a90: 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 63 61 63 cache_size = cac
0aa0: 68 65 5f 73 69 7a 65 3b 0a 09 66 69 6c 65 64 5f he_size;..filed_
0ab0: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
0ac0: 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 = malloc(sizeof
0ad0: 28 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f (*filed_fileinfo
0ae0: 5f 66 64 63 61 63 68 65 29 20 2a 20 66 69 6c 65 _fdcache) * file
0af0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
0b00: 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 66 he_size);..if (f
0b10: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
0b20: 63 61 63 68 65 20 3d 3d 20 4e 55 4c 4c 29 20 7b cache == NULL) {
0b30: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d ...return(1);..}
0b40: 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 .../* Initialize
0b50: 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 20 2a cache entries *
0b60: 2f 0a 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b /..for (idx = 0;
0b70: 20 69 64 78 20 3c 20 66 69 6c 65 64 5f 66 69 6c idx < filed_fil
0b80: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 einfo_fdcache_si
0b90: 7a 65 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 09 6d ze; idx++) {...m
0ba0: 75 74 65 78 5f 69 6e 69 74 5f 72 65 74 20 3d 20 utex_init_ret =
0bb0: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e pthread_mutex_in
0bc0: 69 74 28 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e it(&filed_filein
0bd0: 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e fo_fdcache[idx].
0be0: 6d 75 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a 09 09 mutex, NULL);...
0bf0: 69 66 20 28 6d 75 74 65 78 5f 69 6e 69 74 5f 72 if (mutex_init_r
0c00: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 et != 0) {....re
0c10: 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 turn(1);...}....
0c20: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
0c30: 64 63 61 63 68 65 5b 69 64 78 5d 2e 70 61 74 68 dcache[idx].path
0c40: 20 3d 20 73 74 72 64 75 70 28 22 22 29 3b 0a 09 = strdup("");..
0c50: 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f .filed_fileinfo_
0c60: 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 66 64 20 fdcache[idx].fd
0c70: 3d 20 2d 31 3b 0a 09 09 66 69 6c 65 64 5f 66 69 = -1;...filed_fi
0c80: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 leinfo_fdcache[i
0c90: 64 78 5d 2e 6c 61 73 74 6d 6f 64 20 3d 20 22 22 dx].lastmod = ""
0ca0: 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e ;...filed_filein
0cb0: 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e fo_fdcache[idx].
0cc0: 74 79 70 65 20 3d 20 22 22 3b 0a 09 7d 0a 0a 09 type = "";..}...
0cd0: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a return(0);.}../*
0ce0: 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 72 6f 63 Initialize proc
0cf0: 65 73 73 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e ess */.static in
0d00: 74 20 66 69 6c 65 64 5f 69 6e 69 74 28 75 6e 73 t filed_init(uns
0d10: 69 67 6e 65 64 20 69 6e 74 20 63 61 63 68 65 5f igned int cache_
0d20: 73 69 7a 65 29 20 7b 0a 09 73 74 61 74 69 63 20 size) {..static
0d30: 69 6e 74 20 63 61 6c 6c 65 64 20 3d 20 30 3b 0a int called = 0;.
0d40: 09 69 6e 74 20 63 61 63 68 65 5f 72 65 74 3b 0a .int cache_ret;.
0d50: 0a 09 69 66 20 28 63 61 6c 6c 65 64 29 20 7b 0a ..if (called) {.
0d60: 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a ..return(0);..}.
0d70: 0a 09 63 61 6c 6c 65 64 20 3d 20 31 3b 0a 0a 09 ..called = 1;...
0d80: 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 4c 5f 43 55 52 mlockall(MCL_CUR
0d90: 52 45 4e 54 20 7c 20 4d 43 4c 5f 46 55 54 55 52 RENT | MCL_FUTUR
0da0: 45 29 3b 0a 0a 09 73 69 67 6e 61 6c 28 53 49 47 E);...signal(SIG
0db0: 50 49 50 45 2c 20 53 49 47 5f 49 47 4e 29 3b 0a PIPE, SIG_IGN);.
0dc0: 0a 09 63 61 63 68 65 5f 72 65 74 20 3d 20 66 69 ..cache_ret = fi
0dd0: 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 65 28 63 led_init_cache(c
0de0: 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 ache_size);..if
0df0: 28 63 61 63 68 65 5f 72 65 74 20 21 3d 20 30 29 (cache_ret != 0)
0e00: 20 7b 0a 09 09 72 65 74 75 72 6e 28 63 61 63 68 {...return(cach
0e10: 65 5f 72 65 74 29 3b 0a 09 7d 0a 0a 09 72 65 74 e_ret);..}...ret
0e20: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 4c 69 urn(0);.}../* Li
0e30: 73 74 65 6e 20 6f 6e 20 61 20 70 61 72 74 69 63 sten on a partic
0e40: 75 6c 61 72 20 61 64 64 72 65 73 73 2f 70 6f 72 ular address/por
0e50: 74 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 t */.static int
0e60: 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 63 6f 6e filed_listen(con
0e70: 73 74 20 63 68 61 72 20 2a 61 64 64 72 65 73 73 st char *address
0e80: 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 70 , unsigned int p
0e90: 6f 72 74 29 20 7b 0a 09 73 74 72 75 63 74 20 73 ort) {..struct s
0ea0: 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 64 72 ockaddr_in6 addr
0eb0: 5f 76 36 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 _v6;..struct soc
0ec0: 6b 61 64 64 72 5f 69 6e 20 61 64 64 72 5f 76 34 kaddr_in addr_v4
0ed0: 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 ;..struct sockad
0ee0: 64 72 20 2a 61 64 64 72 3b 0a 09 73 6f 63 6b 6c dr *addr;..sockl
0ef0: 65 6e 5f 74 20 61 64 64 72 5f 6c 65 6e 3b 0a 09 en_t addr_len;..
0f00: 69 6e 74 20 70 74 6f 6e 5f 72 65 74 2c 20 62 69 int pton_ret, bi
0f10: 6e 64 5f 72 65 74 2c 20 6c 69 73 74 65 6e 5f 72 nd_ret, listen_r
0f20: 65 74 3b 0a 09 69 6e 74 20 66 61 6d 69 6c 79 3b et;..int family;
0f30: 0a 09 69 6e 74 20 66 64 3b 0a 0a 0a 09 66 61 6d ..int fd;....fam
0f40: 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 3b 0a ily = AF_INET6;.
0f50: 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e 65 74 .pton_ret = inet
0f60: 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 61 64 _pton(family, ad
0f70: 64 72 65 73 73 2c 20 26 61 64 64 72 5f 76 36 2e dress, &addr_v6.
0f80: 73 69 6e 36 5f 61 64 64 72 2e 73 36 5f 61 64 64 sin6_addr.s6_add
0f90: 72 29 3b 0a 09 69 66 20 28 70 74 6f 6e 5f 72 65 r);..if (pton_re
0fa0: 74 20 21 3d 20 31 29 20 7b 0a 09 09 66 61 6d 69 t != 1) {...fami
0fb0: 6c 79 20 3d 20 41 46 5f 49 4e 45 54 3b 0a 09 09 ly = AF_INET;...
0fc0: 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e 65 74 5f pton_ret = inet_
0fd0: 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 61 64 64 pton(family, add
0fe0: 72 65 73 73 2c 20 26 61 64 64 72 5f 76 34 2e 73 ress, &addr_v4.s
0ff0: 69 6e 5f 61 64 64 72 2e 73 5f 61 64 64 72 29 3b in_addr.s_addr);
1000: 0a 09 09 69 66 20 28 70 74 6f 6e 5f 72 65 74 20 ...if (pton_ret
1010: 21 3d 20 31 29 20 7b 0a 09 09 09 72 65 74 75 72 != 1) {....retur
1020: 6e 28 2d 31 29 3b 0a 09 09 7d 0a 0a 09 09 61 64 n(-1);...}....ad
1030: 64 72 5f 76 34 2e 73 69 6e 5f 66 61 6d 69 6c 79 dr_v4.sin_family
1040: 20 3d 20 66 61 6d 69 6c 79 3b 0a 09 09 61 64 64 = family;...add
1050: 72 5f 76 34 2e 73 69 6e 5f 70 6f 72 74 20 3d 20 r_v4.sin_port =
1060: 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a 09 09 htons(port);....
1070: 61 64 64 72 20 3d 20 28 73 74 72 75 63 74 20 73 addr = (struct s
1080: 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 ockaddr *) &addr
1090: 5f 76 34 3b 0a 09 09 61 64 64 72 5f 6c 65 6e 20 _v4;...addr_len
10a0: 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 5f 76 34 = sizeof(addr_v4
10b0: 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 61 );..} else {...a
10c0: 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 61 6d 69 ddr_v6.sin6_fami
10d0: 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 3b 0a 09 ly = AF_INET6;..
10e0: 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 6c .addr_v6.sin6_fl
10f0: 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a 09 09 61 64 owinfo = 0;...ad
1100: 64 72 5f 76 36 2e 73 69 6e 36 5f 73 63 6f 70 65 dr_v6.sin6_scope
1110: 5f 69 64 20 3d 20 30 3b 0a 09 09 61 64 64 72 5f _id = 0;...addr_
1120: 76 36 2e 73 69 6e 36 5f 70 6f 72 74 20 3d 20 68 v6.sin6_port = h
1130: 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a 09 09 61 tons(port);....a
1140: 64 64 72 20 3d 20 28 73 74 72 75 63 74 20 73 6f ddr = (struct so
1150: 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 5f ckaddr *) &addr_
1160: 76 36 3b 0a 09 09 61 64 64 72 5f 6c 65 6e 20 3d v6;...addr_len =
1170: 20 73 69 7a 65 6f 66 28 61 64 64 72 5f 76 36 29 sizeof(addr_v6)
1180: 3b 0a 09 7d 0a 0a 09 66 64 20 3d 20 73 6f 63 6b ;..}...fd = sock
1190: 65 74 28 66 61 6d 69 6c 79 2c 20 53 4f 43 4b 5f et(family, SOCK_
11a0: 53 54 52 45 41 4d 2c 20 30 29 3b 0a 09 69 66 20 STREAM, 0);..if
11b0: 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 (fd < 0) {...ret
11c0: 75 72 6e 28 66 64 29 3b 0a 09 7d 0a 0a 09 62 69 urn(fd);..}...bi
11d0: 6e 64 5f 72 65 74 20 3d 20 62 69 6e 64 28 66 64 nd_ret = bind(fd
11e0: 2c 20 61 64 64 72 2c 20 61 64 64 72 5f 6c 65 6e , addr, addr_len
11f0: 29 3b 0a 09 69 66 20 28 62 69 6e 64 5f 72 65 74 );..if (bind_ret
1200: 20 3c 20 30 29 20 7b 0a 09 09 63 6c 6f 73 65 28 < 0) {...close(
1210: 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 2d fd);....return(-
1220: 31 29 3b 0a 09 7d 0a 0a 09 6c 69 73 74 65 6e 5f 1);..}...listen_
1230: 72 65 74 20 3d 20 6c 69 73 74 65 6e 28 66 64 2c ret = listen(fd,
1240: 20 31 32 38 29 3b 0a 09 69 66 20 28 6c 69 73 74 128);..if (list
1250: 65 6e 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 en_ret != 0) {..
1260: 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 72 .close(fd);....r
1270: 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 eturn(-1);..}...
1280: 72 65 74 75 72 6e 28 66 64 29 3b 0a 7d 0a 0a 2f return(fd);.}../
1290: 2a 20 4c 6f 67 20 61 20 6d 65 73 73 61 67 65 20 * Log a message
12a0: 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f */.#ifdef FILED_
12b0: 44 4f 4e 54 5f 4c 4f 47 0a 73 74 72 75 63 74 20 DONT_LOG.struct
12c0: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 filed_log_entry
12d0: 66 69 6c 65 64 5f 64 75 6d 6d 79 5f 6c 6f 67 5f filed_dummy_log_
12e0: 65 6e 74 72 79 3b 0a 23 20 20 64 65 66 69 6e 65 entry;.# define
12f0: 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 filed_logging_t
1300: 68 72 65 61 64 5f 69 6e 69 74 28 78 29 20 30 0a hread_init(x) 0.
1310: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f # define filed_
1320: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c log_msg_debug(x,
1330: 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 ...) /**/.# de
1340: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d fine filed_log_m
1350: 73 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a sg(x, ...) /**/.
1360: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f # define filed_
1370: 6c 6f 67 5f 65 6e 74 72 79 28 78 29 20 2f 2a 2a log_entry(x) /**
1380: 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 /.# define file
1390: 64 5f 6c 6f 67 5f 69 70 28 78 2c 20 2e 2e 2e 29 d_log_ip(x, ...)
13a0: 20 4e 55 4c 4c 0a 23 20 20 64 65 66 69 6e 65 20 NULL.# define
13b0: 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 78 29 filed_log_new(x)
13c0: 20 26 66 69 6c 65 64 5f 64 75 6d 6d 79 5f 6c 6f &filed_dummy_lo
13d0: 67 5f 65 6e 74 72 79 0a 23 65 6c 73 65 0a 23 69 g_entry.#else.#i
13e0: 66 64 65 66 20 46 49 4c 45 44 5f 44 45 42 55 47 fdef FILED_DEBUG
13f0: 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 .# define filed
1400: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 _log_msg_debug(x
1410: 2c 20 2e 2e 2e 29 20 7b 20 66 70 72 69 6e 74 66 , ...) { fprintf
1420: 28 73 74 64 65 72 72 2c 20 78 2c 20 5f 5f 56 41 (stderr, x, __VA
1430: 5f 41 52 47 53 5f 5f 29 3b 20 66 70 72 69 6e 74 _ARGS__); fprint
1440: 66 28 73 74 64 65 72 72 2c 20 22 5c 6e 22 29 3b f(stderr, "\n");
1450: 20 66 66 6c 75 73 68 28 73 74 64 65 72 72 29 3b fflush(stderr);
1460: 20 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 }.#else.# defi
1470: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ne filed_log_msg
1480: 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20 2f _debug(x, ...) /
1490: 2a 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 **/.#endif../* I
14a0: 6e 69 74 69 61 6c 69 7a 65 20 6c 6f 67 67 69 6e nitialize loggin
14b0: 67 20 74 68 72 65 61 64 20 2a 2f 0a 73 74 61 74 g thread */.stat
14c0: 69 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f 6c ic void *filed_l
14d0: 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 28 76 6f ogging_thread(vo
14e0: 69 64 20 2a 61 72 67 5f 70 29 20 7b 0a 09 73 74 id *arg_p) {..st
14f0: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 ruct filed_loggi
1500: 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a ng_thread_args *
1510: 61 72 67 3b 0a 09 73 74 72 75 63 74 20 66 69 6c arg;..struct fil
1520: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 63 75 ed_log_entry *cu
1530: 72 72 2c 20 2a 70 72 65 76 3b 0a 09 74 69 6d 65 rr, *prev;..time
1540: 5f 74 20 6e 6f 77 3b 0a 09 46 49 4c 45 20 2a 66 _t now;..FILE *f
1550: 70 3b 0a 0a 09 61 72 67 20 3d 20 61 72 67 5f 70 p;...arg = arg_p
1560: 3b 0a 0a 09 66 70 20 3d 20 61 72 67 2d 3e 66 70 ;...fp = arg->fp
1570: 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a ;...while (1) {.
1580: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ..pthread_mutex_
1590: 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f lock(&filed_log_
15a0: 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b msg_list_mutex);
15b0: 0a 09 09 70 74 68 72 65 61 64 5f 63 6f 6e 64 5f ...pthread_cond_
15c0: 77 61 69 74 28 26 66 69 6c 65 64 5f 6c 6f 67 5f wait(&filed_log_
15d0: 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 79 2c 20 msg_list_ready,
15e0: 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c &filed_log_msg_l
15f0: 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 63 ist_mutex);....c
1600: 75 72 72 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f urr = filed_log_
1610: 6d 73 67 5f 6c 69 73 74 3b 0a 09 09 66 69 6c 65 msg_list;...file
1620: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d d_log_msg_list =
1630: 20 4e 55 4c 4c 3b 0a 0a 09 09 70 74 68 72 65 61 NULL;....pthrea
1640: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
1650: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
1660: 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 6e 6f st_mutex);....no
1670: 77 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a w = time(NULL);.
1680: 0a 09 09 70 72 65 76 20 3d 20 4e 55 4c 4c 3b 0a ...prev = NULL;.
1690: 09 09 66 6f 72 20 28 3b 20 63 75 72 72 3b 20 63 ..for (; curr; c
16a0: 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f 6e 65 78 urr = curr->_nex
16b0: 74 29 20 7b 0a 09 09 09 63 75 72 72 2d 3e 5f 70 t) {....curr->_p
16c0: 72 65 76 20 3d 20 70 72 65 76 3b 0a 0a 09 09 09 rev = prev;.....
16d0: 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 09 09 7d prev = curr;...}
16e0: 0a 0a 09 09 63 75 72 72 20 3d 20 70 72 65 76 3b ....curr = prev;
16f0: 0a 09 09 77 68 69 6c 65 20 28 63 75 72 72 29 20 ...while (curr)
1700: 7b 0a 09 09 09 73 77 69 74 63 68 20 28 63 75 72 {....switch (cur
1710: 72 2d 3e 74 79 70 65 29 20 7b 0a 09 09 09 09 63 r->type) {.....c
1720: 61 73 65 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 ase FILED_LOG_TY
1730: 50 45 5f 4d 45 53 53 41 47 45 3a 0a 09 09 09 09 PE_MESSAGE:.....
1740: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 25 73 .fprintf(fp, "%s
1750: 22 2c 20 63 75 72 72 2d 3e 62 75 66 66 65 72 29 ", curr->buffer)
1760: 3b 0a 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 ;.......break;..
1770: 09 09 09 63 61 73 65 20 46 49 4c 45 44 5f 4c 4f ...case FILED_LO
1780: 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 52 3a G_TYPE_TRANSFER:
1790: 0a 09 09 09 09 09 69 66 20 28 63 75 72 72 2d 3e ......if (curr->
17a0: 65 6e 64 74 69 6d 65 20 3d 3d 20 28 28 74 69 6d endtime == ((tim
17b0: 65 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09 09 09 e_t) -1)) {.....
17c0: 09 09 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 20 ..curr->endtime
17d0: 3d 20 6e 6f 77 3b 0a 09 09 09 09 09 7d 0a 0a 09 = now;......}...
17e0: 09 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 ....fprintf(fp,
17f0: 22 54 52 41 4e 53 46 45 52 20 50 41 54 48 3d 25 "TRANSFER PATH=%
1800: 73 20 53 52 43 3d 25 73 3a 25 69 20 54 49 4d 45 s SRC=%s:%i TIME
1810: 2e 53 54 41 52 54 3d 25 6c 6c 75 20 54 49 4d 45 .START=%llu TIME
1820: 2e 45 4e 44 3d 25 6c 6c 75 20 43 4f 44 45 2e 56 .END=%llu CODE.V
1830: 41 4c 55 45 3d 25 75 20 43 4f 44 45 2e 52 45 41 ALUE=%u CODE.REA
1840: 53 4f 4e 3d 25 73 20 52 45 51 55 45 53 54 2e 4f SON=%s REQUEST.O
1850: 46 46 53 45 54 3d 25 6c 6c 75 20 52 45 51 55 45 FFSET=%llu REQUE
1860: 53 54 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 20 46 ST.LENGTH=%llu F
1870: 49 4c 45 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 20 ILE.LENGTH=%llu
1880: 54 52 41 4e 53 46 45 52 2e 4c 45 4e 47 54 48 3d TRANSFER.LENGTH=
1890: 25 6c 6c 75 22 2c 0a 09 09 09 09 09 09 63 75 72 %llu",.......cur
18a0: 72 2d 3e 62 75 66 66 65 72 2c 0a 09 09 09 09 09 r->buffer,......
18b0: 09 63 75 72 72 2d 3e 69 70 2c 20 63 75 72 72 2d .curr->ip, curr-
18c0: 3e 70 6f 72 74 2c 0a 09 09 09 09 09 09 28 75 6e >port,.......(un
18d0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
18e0: 29 20 63 75 72 72 2d 3e 73 74 61 72 74 74 69 6d ) curr->starttim
18f0: 65 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e e,.......(unsign
1900: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 ed long long) cu
1910: 72 72 2d 3e 65 6e 64 74 69 6d 65 2c 0a 09 09 09 rr->endtime,....
1920: 09 09 09 63 75 72 72 2d 3e 68 74 74 70 5f 63 6f ...curr->http_co
1930: 64 65 2c 20 63 75 72 72 2d 3e 72 65 61 73 6f 6e de, curr->reason
1940: 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 ,.......(unsigne
1950: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 d long long) cur
1960: 72 2d 3e 72 65 71 5f 6f 66 66 73 65 74 2c 0a 09 r->req_offset,..
1970: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
1980: 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e ong long) curr->
1990: 72 65 71 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 req_length,.....
19a0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
19b0: 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 66 69 6c long) curr->fil
19c0: 65 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09 09 e_length,.......
19d0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
19e0: 6f 6e 67 29 20 63 75 72 72 2d 3e 73 65 6e 74 5f ong) curr->sent_
19f0: 6c 65 6e 67 74 68 0a 09 09 09 09 09 29 3b 0a 0a length......);..
1a00: 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 7d .....break;....}
1a10: 0a 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 ....fprintf(fp,
1a20: 22 20 54 48 52 45 41 44 3d 25 6c 6c 75 20 54 49 " THREAD=%llu TI
1a30: 4d 45 3d 25 6c 6c 75 5c 6e 22 2c 0a 09 09 09 09 ME=%llu\n",.....
1a40: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
1a50: 6f 6e 67 29 20 63 75 72 72 2d 3e 74 68 72 65 61 ong) curr->threa
1a60: 64 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 d,.....(unsigned
1a70: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 6e 6f 77 0a long long) now.
1a80: 09 09 09 29 3b 0a 09 09 09 66 66 6c 75 73 68 28 ...);....fflush(
1a90: 66 70 29 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20 fp);.....prev =
1aa0: 63 75 72 72 3b 0a 09 09 09 63 75 72 72 20 3d 20 curr;....curr =
1ab0: 63 75 72 72 2d 3e 5f 70 72 65 76 3b 0a 0a 09 09 curr->_prev;....
1ac0: 09 66 72 65 65 28 70 72 65 76 29 3b 0a 09 09 7d .free(prev);...}
1ad0: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c ..}...return(NUL
1ae0: 4c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f L);.}..static vo
1af0: 69 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 id filed_log_ent
1b00: 72 79 28 73 74 72 75 63 74 20 66 69 6c 65 64 5f ry(struct filed_
1b10: 6c 6f 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 log_entry *entry
1b20: 29 20 7b 0a 09 65 6e 74 72 79 2d 3e 74 68 72 65 ) {..entry->thre
1b30: 61 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c ad = pthread_sel
1b40: 66 28 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d f();...pthread_m
1b50: 75 74 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 utex_lock(&filed
1b60: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 _log_msg_list_mu
1b70: 74 65 78 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 5f tex);...entry->_
1b80: 6e 65 78 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 next = filed_log
1b90: 5f 6d 73 67 5f 6c 69 73 74 3b 0a 09 66 69 6c 65 _msg_list;..file
1ba0: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d d_log_msg_list =
1bb0: 20 65 6e 74 72 79 3b 0a 0a 09 70 74 68 72 65 61 entry;...pthrea
1bc0: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
1bd0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
1be0: 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 70 74 68 st_mutex);...pth
1bf0: 72 65 61 64 5f 63 6f 6e 64 5f 73 69 67 6e 61 6c read_cond_signal
1c00: 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f (&filed_log_msg_
1c10: 6c 69 73 74 5f 72 65 61 64 79 29 3b 0a 0a 09 72 list_ready);...r
1c20: 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 eturn;.}..static
1c30: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f struct filed_lo
1c40: 67 5f 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c g_entry *filed_l
1c50: 6f 67 5f 6e 65 77 28 69 6e 74 20 69 6e 69 74 69 og_new(int initi
1c60: 61 6c 69 7a 65 29 20 7b 0a 09 73 74 72 75 63 74 alize) {..struct
1c70: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 filed_log_entry
1c80: 20 2a 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76 *retval;...retv
1c90: 61 6c 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 al = malloc(size
1ca0: 6f 66 28 2a 72 65 74 76 61 6c 29 29 3b 0a 0a 09 of(*retval));...
1cb0: 69 66 20 28 69 6e 69 74 69 61 6c 69 7a 65 29 20 if (initialize)
1cc0: 7b 0a 09 09 72 65 74 76 61 6c 2d 3e 62 75 66 66 {...retval->buff
1cd0: 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 er[0] = '\0';...
1ce0: 72 65 74 76 61 6c 2d 3e 68 74 74 70 5f 63 6f 64 retval->http_cod
1cf0: 65 20 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c e = -1;...retval
1d00: 2d 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 30 3b ->starttime = 0;
1d10: 0a 09 09 72 65 74 76 61 6c 2d 3e 65 6e 64 74 69 ...retval->endti
1d20: 6d 65 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c me = 0;...retval
1d30: 2d 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 30 ->req_offset = 0
1d40: 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f ;...retval->req_
1d50: 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 length = 0;...re
1d60: 74 76 61 6c 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 tval->sent_lengt
1d70: 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d h = 0;...retval-
1d80: 3e 66 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 30 >file_length = 0
1d90: 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 69 70 5b 30 ;...retval->ip[0
1da0: 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 72 65 74 76 ] = '\0';...retv
1db0: 61 6c 2d 3e 70 6f 72 74 20 3d 20 2d 31 3b 0a 09 al->port = -1;..
1dc0: 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61 }...return(retva
1dd0: 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f l);.}..static vo
1de0: 69 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 id filed_log_msg
1df0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d 74 (const char *fmt
1e00: 2c 20 2e 2e 2e 29 20 7b 0a 09 73 74 72 75 63 74 , ...) {..struct
1e10: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 filed_log_entry
1e20: 20 2a 65 6e 74 72 79 3b 0a 09 76 61 5f 6c 69 73 *entry;..va_lis
1e30: 74 20 61 72 67 73 3b 0a 0a 09 65 6e 74 72 79 20 t args;...entry
1e40: 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 = filed_log_new(
1e50: 30 29 3b 0a 0a 09 76 61 5f 73 74 61 72 74 28 61 0);...va_start(a
1e60: 72 67 73 2c 20 66 6d 74 29 3b 0a 0a 09 76 73 6e rgs, fmt);...vsn
1e70: 70 72 69 6e 74 66 28 65 6e 74 72 79 2d 3e 62 75 printf(entry->bu
1e80: 66 66 65 72 2c 20 73 69 7a 65 6f 66 28 65 6e 74 ffer, sizeof(ent
1e90: 72 79 2d 3e 62 75 66 66 65 72 29 2c 20 66 6d 74 ry->buffer), fmt
1ea0: 2c 20 61 72 67 73 29 3b 0a 0a 09 76 61 5f 65 6e , args);...va_en
1eb0: 64 28 61 72 67 73 29 3b 0a 0a 09 65 6e 74 72 79 d(args);...entry
1ec0: 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 4c ->type = FILED_L
1ed0: 4f 47 5f 54 59 50 45 5f 4d 45 53 53 41 47 45 3b OG_TYPE_MESSAGE;
1ee0: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ...filed_log_ent
1ef0: 72 79 28 65 6e 74 72 79 29 3b 0a 0a 09 72 65 74 ry(entry);...ret
1f00: 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 urn;.}..static c
1f10: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 onst char *filed
1f20: 5f 6c 6f 67 5f 69 70 28 73 74 72 75 63 74 20 73 _log_ip(struct s
1f30: 6f 63 6b 61 64 64 72 20 2a 61 64 64 72 2c 20 63 ockaddr *addr, c
1f40: 68 61 72 20 2a 62 75 66 66 65 72 2c 20 73 69 7a har *buffer, siz
1f50: 65 5f 74 20 62 75 66 66 65 72 6c 65 6e 29 20 7b e_t bufferlen) {
1f60: 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 ..struct sockadd
1f70: 72 5f 69 6e 20 2a 61 64 64 72 5f 76 34 3b 0a 09 r_in *addr_v4;..
1f80: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f struct sockaddr_
1f90: 69 6e 36 20 2a 61 64 64 72 5f 76 36 3b 0a 09 63 in6 *addr_v6;..c
1fa0: 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 74 76 61 onst char *retva
1fb0: 6c 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 61 64 64 72 l = NULL;...addr
1fc0: 5f 76 36 20 3d 20 28 73 74 72 75 63 74 20 73 6f _v6 = (struct so
1fd0: 63 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20 61 64 ckaddr_in6 *) ad
1fe0: 64 72 3b 0a 0a 09 73 77 69 74 63 68 20 28 61 64 dr;...switch (ad
1ff0: 64 72 5f 76 36 2d 3e 73 69 6e 36 5f 66 61 6d 69 dr_v6->sin6_fami
2000: 6c 79 29 20 7b 0a 09 09 63 61 73 65 20 41 46 5f ly) {...case AF_
2010: 49 4e 45 54 3a 0a 09 09 09 61 64 64 72 5f 76 34 INET:....addr_v4
2020: 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 = (struct socka
2030: 64 64 72 5f 69 6e 20 2a 29 20 61 64 64 72 3b 0a ddr_in *) addr;.
2040: 09 09 09 72 65 74 76 61 6c 20 3d 20 69 6e 65 74 ...retval = inet
2050: 5f 6e 74 6f 70 28 41 46 5f 49 4e 45 54 2c 20 26 _ntop(AF_INET, &
2060: 61 64 64 72 5f 76 34 2d 3e 73 69 6e 5f 61 64 64 addr_v4->sin_add
2070: 72 2c 20 62 75 66 66 65 72 2c 20 62 75 66 66 65 r, buffer, buffe
2080: 72 6c 65 6e 29 3b 0a 09 09 09 62 72 65 61 6b 3b rlen);....break;
2090: 0a 09 09 63 61 73 65 20 41 46 5f 49 4e 45 54 36 ...case AF_INET6
20a0: 3a 0a 09 09 09 72 65 74 76 61 6c 20 3d 20 69 6e :....retval = in
20b0: 65 74 5f 6e 74 6f 70 28 41 46 5f 49 4e 45 54 36 et_ntop(AF_INET6
20c0: 2c 20 26 61 64 64 72 5f 76 36 2d 3e 73 69 6e 36 , &addr_v6->sin6
20d0: 5f 61 64 64 72 2c 20 62 75 66 66 65 72 2c 20 62 _addr, buffer, b
20e0: 75 66 66 65 72 6c 65 6e 29 3b 0a 09 09 09 62 72 ufferlen);....br
20f0: 65 61 6b 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e eak;..}...return
2100: 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 (retval);.}..sta
2110: 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 6c 6f tic int filed_lo
2120: 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 gging_thread_ini
2130: 74 28 46 49 4c 45 20 2a 6c 6f 67 66 70 29 20 7b t(FILE *logfp) {
2140: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c ..struct filed_l
2150: 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 72 ogging_thread_ar
2160: 67 73 20 2a 61 72 67 73 3b 0a 09 70 74 68 72 65 gs *args;..pthre
2170: 61 64 5f 74 20 74 68 72 65 61 64 5f 69 64 3b 0a ad_t thread_id;.
2180: 0a 09 61 72 67 73 20 3d 20 6d 61 6c 6c 6f 63 28 ..args = malloc(
2190: 73 69 7a 65 6f 66 28 2a 61 72 67 73 29 29 3b 0a sizeof(*args));.
21a0: 09 61 72 67 73 2d 3e 66 70 20 3d 20 6c 6f 67 66 .args->fp = logf
21b0: 70 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d p;...filed_log_m
21c0: 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a sg_list = NULL;.
21d0: 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ..pthread_mutex_
21e0: 69 6e 69 74 28 26 66 69 6c 65 64 5f 6c 6f 67 5f init(&filed_log_
21f0: 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 2c 20 msg_list_mutex,
2200: 4e 55 4c 4c 29 3b 0a 0a 09 70 74 68 72 65 61 64 NULL);...pthread
2210: 5f 63 72 65 61 74 65 28 26 74 68 72 65 61 64 5f _create(&thread_
2220: 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f id, NULL, filed_
2230: 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 2c 20 logging_thread,
2240: 61 72 67 73 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c args);...filed_l
2250: 6f 67 5f 6d 73 67 28 22 53 54 41 52 54 22 29 3b og_msg("START");
2260: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a ...return(0);.}.
2270: 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 6d 61 #endif../* Forma
2280: 74 20 74 69 6d 65 20 70 65 72 20 52 46 43 32 36 t time per RFC26
2290: 31 36 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 16 */.static cha
22a0: 72 20 2a 66 69 6c 65 64 5f 66 6f 72 6d 61 74 5f r *filed_format_
22b0: 74 69 6d 65 28 63 68 61 72 20 2a 62 75 66 66 65 time(char *buffe
22c0: 72 2c 20 73 69 7a 65 5f 74 20 62 75 66 66 65 72 r, size_t buffer
22d0: 5f 6c 65 6e 2c 20 63 6f 6e 73 74 20 74 69 6d 65 _len, const time
22e0: 5f 74 20 74 69 6d 65 69 6e 66 6f 29 20 7b 0a 09 _t timeinfo) {..
22f0: 73 74 72 75 63 74 20 74 6d 20 74 69 6d 65 69 6e struct tm timein
2300: 66 6f 5f 74 6d 2c 20 2a 74 69 6d 65 69 6e 66 6f fo_tm, *timeinfo
2310: 5f 74 6d 5f 70 3b 0a 0a 09 74 69 6d 65 69 6e 66 _tm_p;...timeinf
2320: 6f 5f 74 6d 5f 70 20 3d 20 67 6d 74 69 6d 65 5f o_tm_p = gmtime_
2330: 72 28 26 74 69 6d 65 69 6e 66 6f 2c 20 26 74 69 r(&timeinfo, &ti
2340: 6d 65 69 6e 66 6f 5f 74 6d 29 3b 0a 09 69 66 20 meinfo_tm);..if
2350: 28 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d (timeinfo_tm_p =
2360: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 = NULL) {...retu
2370: 72 6e 28 22 75 6e 6b 6e 6f 77 6e 22 29 3b 0a 09 rn("unknown");..
2380: 7d 0a 0a 09 62 75 66 66 65 72 5b 62 75 66 66 65 }...buffer[buffe
2390: 72 5f 6c 65 6e 20 2d 20 31 5d 20 3d 20 27 5c 30 r_len - 1] = '\0
23a0: 27 3b 0a 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d ';..buffer_len =
23b0: 20 73 74 72 66 74 69 6d 65 28 62 75 66 66 65 72 strftime(buffer
23c0: 2c 20 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 31 , buffer_len - 1
23d0: 2c 20 22 25 61 2c 20 25 64 20 25 62 20 25 59 20 , "%a, %d %b %Y
23e0: 25 48 3a 25 4d 3a 25 53 20 47 4d 54 22 2c 20 74 %H:%M:%S GMT", t
23f0: 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 29 3b 0a 0a imeinfo_tm_p);..
2400: 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 29 3b .return(buffer);
2410: 0a 7d 0a 0a 2f 2a 20 68 61 73 68 20 2a 2f 0a 73 .}../* hash */.s
2420: 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20 69 tatic unsigned i
2430: 6e 74 20 66 69 6c 65 64 5f 68 61 73 68 28 63 6f nt filed_hash(co
2440: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
2450: 72 20 2a 76 61 6c 75 65 2c 20 75 6e 73 69 67 6e r *value, unsign
2460: 65 64 20 69 6e 74 20 6d 6f 64 75 6c 75 73 29 20 ed int modulus)
2470: 7b 0a 09 75 6e 73 69 67 6e 65 64 20 63 68 61 72 {..unsigned char
2480: 20 63 75 72 72 2c 20 70 72 65 76 3b 0a 09 69 6e curr, prev;..in
2490: 74 20 64 69 66 66 3b 0a 09 75 6e 73 69 67 6e 65 t diff;..unsigne
24a0: 64 20 69 6e 74 20 72 65 74 76 61 6c 3b 0a 0a 09 d int retval;...
24b0: 72 65 74 76 61 6c 20 3d 20 6d 6f 64 75 6c 75 73 retval = modulus
24c0: 20 2d 20 31 3b 0a 09 70 72 65 76 20 3d 20 6d 6f - 1;..prev = mo
24d0: 64 75 6c 75 73 20 25 20 32 35 35 3b 0a 0a 09 77 dulus % 255;...w
24e0: 68 69 6c 65 20 28 28 63 75 72 72 20 3d 20 2a 76 hile ((curr = *v
24f0: 61 6c 75 65 29 29 20 7b 0a 09 09 69 66 20 28 63 alue)) {...if (c
2500: 75 72 72 20 3c 20 33 32 29 20 7b 0a 09 09 09 63 urr < 32) {....c
2510: 75 72 72 20 3d 20 32 35 35 20 2d 20 63 75 72 72 urr = 255 - curr
2520: 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 ;...} else {....
2530: 63 75 72 72 20 2d 3d 20 33 32 3b 0a 09 09 7d 0a curr -= 32;...}.
2540: 0a 09 09 69 66 20 28 70 72 65 76 20 3c 20 63 75 ...if (prev < cu
2550: 72 72 29 20 7b 0a 09 09 09 64 69 66 66 20 3d 20 rr) {....diff =
2560: 63 75 72 72 20 2d 20 70 72 65 76 3b 0a 09 09 7d curr - prev;...}
2570: 20 65 6c 73 65 20 7b 0a 09 09 09 64 69 66 66 20 else {....diff
2580: 3d 20 70 72 65 76 20 2d 20 63 75 72 72 3b 0a 09 = prev - curr;..
2590: 09 7d 0a 0a 09 09 70 72 65 76 20 3d 20 63 75 72 .}....prev = cur
25a0: 72 3b 0a 0a 09 09 72 65 74 76 61 6c 20 3c 3c 3d r;....retval <<=
25b0: 20 33 3b 0a 09 09 72 65 74 76 61 6c 20 26 3d 20 3;...retval &=
25c0: 30 78 46 46 46 46 46 46 46 46 4c 55 3b 0a 09 09 0xFFFFFFFFLU;...
25d0: 72 65 74 76 61 6c 20 5e 3d 20 64 69 66 66 3b 0a retval ^= diff;.
25e0: 0a 09 09 76 61 6c 75 65 2b 2b 3b 0a 09 7d 0a 0a ...value++;..}..
25f0: 09 72 65 74 76 61 6c 20 3d 20 72 65 74 76 61 6c .retval = retval
2600: 20 25 20 6d 6f 64 75 6c 75 73 3b 0a 0a 09 72 65 % modulus;...re
2610: 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a turn(retval);.}.
2620: 0a 2f 2a 20 46 69 6e 64 20 61 20 6d 69 6d 65 2d ./* Find a mime-
2630: 74 79 70 65 20 62 61 73 65 64 20 6f 6e 20 74 68 type based on th
2640: 65 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 73 74 e filename */.st
2650: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
2660: 2a 66 69 6c 65 64 5f 64 65 74 65 72 6d 69 6e 65 *filed_determine
2670: 5f 6d 69 6d 65 74 79 70 65 28 63 6f 6e 73 74 20 _mimetype(const
2680: 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 09 63 char *path) {..c
2690: 6f 6e 73 74 20 63 68 61 72 20 2a 70 3b 0a 0a 09 onst char *p;...
26a0: 70 20 3d 20 73 74 72 72 63 68 72 28 70 61 74 68 p = strrchr(path
26b0: 2c 20 27 2e 27 29 3b 0a 09 69 66 20 28 70 20 3d , '.');..if (p =
26c0: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 = NULL) {...retu
26d0: 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 rn(FILED_DEFAULT
26e0: 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 70 2b 2b _TYPE);..}...p++
26f0: 3b 0a 09 69 66 20 28 2a 70 20 3d 3d 20 27 5c 30 ;..if (*p == '\0
2700: 27 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 46 49 ') {...return(FI
2710: 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 LED_DEFAULT_TYPE
2720: 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 6c 6f );..}...filed_lo
2730: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 4c 6f 6f g_msg_debug("Loo
2740: 6b 69 6e 67 20 75 70 20 4d 49 4d 45 20 74 79 70 king up MIME typ
2750: 65 20 66 6f 72 20 25 73 20 28 68 61 73 68 20 3d e for %s (hash =
2760: 20 25 6c 6c 75 29 22 2c 20 70 2c 20 28 75 6e 73 %llu)", p, (uns
2770: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
2780: 20 66 69 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e filed_hash((con
2790: 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
27a0: 20 2a 29 20 70 2c 20 31 36 37 37 37 32 35 39 29 *) p, 16777259)
27b0: 29 3b 0a 0a 23 69 6e 63 6c 75 64 65 20 22 66 69 );..#include "fi
27c0: 6c 65 64 2d 6d 69 6d 65 2d 74 79 70 65 73 2e 68 led-mime-types.h
27d0: 22 0a 0a 09 72 65 74 75 72 6e 28 46 49 4c 45 44 "...return(FILED
27e0: 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a _DEFAULT_TYPE);.
27f0: 7d 0a 0a 2f 2a 20 4f 70 65 6e 20 61 20 66 69 6c }../* Open a fil
2800: 65 20 61 6e 64 20 72 65 74 75 72 6e 20 66 69 6c e and return fil
2810: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f e information */
2820: 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 66 .static struct f
2830: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 iled_fileinfo *f
2840: 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 28 63 iled_open_file(c
2850: 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c onst char *path,
2860: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 struct filed_fi
2870: 6c 65 69 6e 66 6f 20 2a 62 75 66 66 65 72 29 20 leinfo *buffer)
2880: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f {..struct filed_
2890: 66 69 6c 65 69 6e 66 6f 20 2a 63 61 63 68 65 3b fileinfo *cache;
28a0: 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 6f 70 ..const char *op
28b0: 65 6e 5f 70 61 74 68 3b 0a 09 75 6e 73 69 67 6e en_path;..unsign
28c0: 65 64 20 69 6e 74 20 63 61 63 68 65 5f 69 64 78 ed int cache_idx
28d0: 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09 69 ;..off_t len;..i
28e0: 6e 74 20 66 64 3b 0a 0a 09 63 61 63 68 65 5f 69 nt fd;...cache_i
28f0: 64 78 20 3d 20 66 69 6c 65 64 5f 68 61 73 68 28 dx = filed_hash(
2900: 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
2910: 63 68 61 72 20 2a 29 20 70 61 74 68 2c 20 66 69 char *) path, fi
2920: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
2930: 61 63 68 65 5f 73 69 7a 65 29 3b 0a 0a 09 63 61 ache_size);...ca
2940: 63 68 65 20 3d 20 26 66 69 6c 65 64 5f 66 69 6c che = &filed_fil
2950: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 63 61 einfo_fdcache[ca
2960: 63 68 65 5f 69 64 78 5d 3b 0a 0a 09 66 69 6c 65 che_idx];...file
2970: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 d_log_msg_debug(
2980: 22 4c 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 "Locking mutex f
2990: 6f 72 20 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 or idx: %lu", (u
29a0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 nsigned long) ca
29b0: 63 68 65 5f 69 64 78 29 3b 0a 0a 09 70 74 68 72 che_idx);...pthr
29c0: 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 ead_mutex_lock(&
29d0: 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a cache->mutex);..
29e0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 .filed_log_msg_d
29f0: 65 62 75 67 28 22 43 6f 6d 70 6c 65 74 65 64 20 ebug("Completed
2a00: 6c 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f locking mutex fo
2a10: 72 20 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e r idx: %lu", (un
2a20: 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 signed long) cac
2a30: 68 65 5f 69 64 78 29 3b 0a 0a 09 69 66 20 28 73 he_idx);...if (s
2a40: 74 72 63 6d 70 28 70 61 74 68 2c 20 63 61 63 68 trcmp(path, cach
2a50: 65 2d 3e 70 61 74 68 29 20 21 3d 20 30 29 20 7b e->path) != 0) {
2a60: 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ...filed_log_msg
2a70: 5f 64 65 62 75 67 28 22 43 61 63 68 65 20 6d 69 _debug("Cache mi
2a80: 73 73 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 3a ss for idx: %lu:
2a90: 20 4f 4c 44 20 5c 22 25 73 5c 22 2c 20 4e 45 57 OLD \"%s\", NEW
2aa0: 20 5c 22 25 73 5c 22 22 2c 20 28 75 6e 73 69 67 \"%s\"", (unsig
2ab0: 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f ned long) cache_
2ac0: 69 64 78 2c 20 63 61 63 68 65 2d 3e 70 61 74 68 idx, cache->path
2ad0: 2c 20 70 61 74 68 29 3b 0a 0a 09 09 2f 2a 20 46 , path);..../* F
2ae0: 6f 72 20 72 65 71 75 65 73 74 73 20 66 6f 72 20 or requests for
2af0: 74 68 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f the root directo
2b00: 72 79 2c 20 73 65 72 76 65 20 6f 75 74 20 69 6e ry, serve out in
2b10: 64 65 78 2e 68 74 6d 6c 20 2a 2f 0a 09 09 69 66 dex.html */...if
2b20: 20 28 70 61 74 68 5b 30 5d 20 3d 3d 20 27 5c 30 (path[0] == '\0
2b30: 27 20 7c 7c 20 28 70 61 74 68 5b 30 5d 20 3d 3d ' || (path[0] ==
2b40: 20 27 2f 27 20 26 26 20 70 61 74 68 5b 31 5d 20 '/' && path[1]
2b50: 3d 3d 20 27 5c 30 27 29 29 20 7b 0a 09 09 09 6f == '\0')) {....o
2b60: 70 65 6e 5f 70 61 74 68 20 3d 20 22 2f 69 6e 64 pen_path = "/ind
2b70: 65 78 2e 68 74 6d 6c 22 3b 0a 09 09 7d 20 65 6c ex.html";...} el
2b80: 73 65 20 7b 0a 09 09 09 6f 70 65 6e 5f 70 61 74 se {....open_pat
2b90: 68 20 3d 20 70 61 74 68 3b 0a 09 09 7d 0a 0a 09 h = path;...}...
2ba0: 09 66 64 20 3d 20 6f 70 65 6e 28 6f 70 65 6e 5f .fd = open(open_
2bb0: 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 20 7c path, O_RDONLY |
2bc0: 20 4f 5f 4c 41 52 47 45 46 49 4c 45 29 3b 0a 09 O_LARGEFILE);..
2bd0: 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 .if (fd < 0) {..
2be0: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ..pthread_mutex_
2bf0: 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d unlock(&cache->m
2c00: 75 74 65 78 29 3b 0a 0a 09 09 09 72 65 74 75 72 utex);.....retur
2c10: 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 n(NULL);...}....
2c20: 66 72 65 65 28 63 61 63 68 65 2d 3e 70 61 74 68 free(cache->path
2c30: 29 3b 0a 09 09 69 66 20 28 63 61 63 68 65 2d 3e );...if (cache->
2c40: 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09 09 63 6c fd >= 0) {....cl
2c50: 6f 73 65 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a ose(cache->fd);.
2c60: 09 09 7d 0a 0a 09 09 6c 65 6e 20 3d 20 6c 73 65 ..}....len = lse
2c70: 65 6b 28 66 64 2c 20 30 2c 20 53 45 45 4b 5f 45 ek(fd, 0, SEEK_E
2c80: 4e 44 29 3b 0a 09 09 6c 73 65 65 6b 28 66 64 2c ND);...lseek(fd,
2c90: 20 30 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 0a 0, SEEK_SET);..
2ca0: 09 09 63 61 63 68 65 2d 3e 66 64 20 3d 20 66 64 ..cache->fd = fd
2cb0: 3b 0a 09 09 63 61 63 68 65 2d 3e 6c 65 6e 20 3d ;...cache->len =
2cc0: 20 6c 65 6e 3b 0a 09 09 63 61 63 68 65 2d 3e 70 len;...cache->p
2cd0: 61 74 68 20 3d 20 73 74 72 64 75 70 28 70 61 74 ath = strdup(pat
2ce0: 68 29 3b 0a 09 09 63 61 63 68 65 2d 3e 74 79 70 h);...cache->typ
2cf0: 65 20 3d 20 66 69 6c 65 64 5f 64 65 74 65 72 6d e = filed_determ
2d00: 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 6f 70 65 ine_mimetype(ope
2d10: 6e 5f 70 61 74 68 29 3b 0a 0a 09 09 2f 2a 20 58 n_path);..../* X
2d20: 58 58 3a 54 4f 44 4f 3a 20 44 65 74 65 72 6d 69 XX:TODO: Determi
2d30: 6e 65 20 2a 2f 0a 09 09 63 61 63 68 65 2d 3e 6c ne */...cache->l
2d40: 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 64 5f 66 astmod = filed_f
2d50: 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 63 68 65 ormat_time(cache
2d60: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a ->lastmod_b, siz
2d70: 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d eof(cache->lastm
2d80: 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c od_b), time(NULL
2d90: 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 6c 73 65 ) - 30);..} else
2da0: 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d {...filed_log_m
2db0: 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65 20 sg_debug("Cache
2dc0: 68 69 74 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 hit for idx: %lu
2dd0: 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 22 2c 20 : PATH \"%s\"",
2de0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 (unsigned long)
2df0: 63 61 63 68 65 5f 69 64 78 2c 20 70 61 74 68 29 cache_idx, path)
2e00: 3b 0a 09 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 57 65 ;..}.../*.. * We
2e10: 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 have to make a
2e20: 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20 62 65 duplicate FD, be
2e30: 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20 72 65 cause once we re
2e40: 6c 65 61 73 65 20 74 68 65 20 63 61 63 68 65 0a lease the cache.
2e50: 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 65 20 66 . * mutex, the f
2e60: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d ile descriptor m
2e70: 61 79 20 62 65 20 63 6c 6f 73 65 64 0a 09 20 2a ay be closed.. *
2e80: 2f 0a 09 66 64 20 3d 20 64 75 70 28 63 61 63 68 /..fd = dup(cach
2e90: 65 2d 3e 66 64 29 3b 0a 09 69 66 20 28 66 64 20 e->fd);..if (fd
2ea0: 3c 20 30 29 20 7b 0a 09 09 70 74 68 72 65 61 64 < 0) {...pthread
2eb0: 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 _mutex_unlock(&c
2ec0: 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 ache->mutex);...
2ed0: 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 .return(NULL);..
2ee0: 7d 0a 0a 09 62 75 66 66 65 72 2d 3e 66 64 20 3d }...buffer->fd =
2ef0: 20 66 64 3b 0a 09 62 75 66 66 65 72 2d 3e 6c 65 fd;..buffer->le
2f00: 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e 3b 0a n = cache->len;.
2f10: 09 62 75 66 66 65 72 2d 3e 74 79 70 65 20 3d 20 .buffer->type =
2f20: 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a 09 6d 65 cache->type;..me
2f30: 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 6c 61 73 mcpy(buffer->las
2f40: 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d 3e 6c tmod_b, cache->l
2f50: 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 astmod_b, sizeof
2f60: 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 (buffer->lastmod
2f70: 5f 62 29 29 3b 0a 09 62 75 66 66 65 72 2d 3e 6c _b));..buffer->l
2f80: 61 73 74 6d 6f 64 20 3d 20 62 75 66 66 65 72 2d astmod = buffer-
2f90: 3e 6c 61 73 74 6d 6f 64 5f 62 20 2b 20 28 63 61 >lastmod_b + (ca
2fa0: 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 2d 20 63 che->lastmod - c
2fb0: 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 ache->lastmod_b)
2fc0: 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 65 ;...pthread_mute
2fd0: 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d x_unlock(&cache-
2fe0: 3e 6d 75 74 65 78 29 3b 0a 0a 09 72 65 74 75 72 >mutex);...retur
2ff0: 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a n(buffer);.}../*
3000: 20 50 72 6f 63 65 73 73 20 61 6e 20 48 54 54 50 Process an HTTP
3010: 20 72 65 71 75 65 73 74 20 61 6e 64 20 72 65 74 request and ret
3020: 75 72 6e 20 74 68 65 20 70 61 74 68 20 72 65 71 urn the path req
3030: 75 65 73 74 65 64 20 2a 2f 0a 73 74 61 74 69 63 uested */.static
3040: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 struct filed_ht
3050: 74 70 5f 72 65 71 75 65 73 74 20 2a 66 69 6c 65 tp_request *file
3060: 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71 75 65 d_get_http_reque
3070: 73 74 28 46 49 4c 45 20 2a 66 70 2c 20 73 74 72 st(FILE *fp, str
3080: 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 uct filed_http_r
3090: 65 71 75 65 73 74 20 2a 62 75 66 66 65 72 5f 73 equest *buffer_s
30a0: 74 29 20 7b 0a 09 63 68 61 72 20 2a 6d 65 74 68 t) {..char *meth
30b0: 6f 64 2c 20 2a 70 61 74 68 3b 0a 09 63 68 61 72 od, *path;..char
30c0: 20 2a 62 75 66 66 65 72 2c 20 2a 74 6d 70 62 75 *buffer, *tmpbu
30d0: 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 ffer, *workbuffe
30e0: 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72 5f 6e r, *workbuffer_n
30f0: 65 78 74 3b 0a 09 73 69 7a 65 5f 74 20 62 75 66 ext;..size_t buf
3100: 66 65 72 5f 6c 65 6e 2c 20 74 6d 70 62 75 66 66 fer_len, tmpbuff
3110: 65 72 5f 6c 65 6e 3b 0a 09 6f 66 66 5f 74 20 72 er_len;..off_t r
3120: 61 6e 67 65 5f 73 74 61 72 74 2c 20 72 61 6e 67 ange_start, rang
3130: 65 5f 65 6e 64 2c 20 72 61 6e 67 65 5f 6c 65 6e e_end, range_len
3140: 67 74 68 3b 0a 09 69 6e 74 20 72 61 6e 67 65 5f gth;..int range_
3150: 72 65 71 75 65 73 74 3b 0a 09 69 6e 74 20 66 64 request;..int fd
3160: 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 66 64 20 3d ;..int i;...fd =
3170: 20 66 69 6c 65 6e 6f 28 66 70 29 3b 0a 0a 09 72 fileno(fp);...r
3180: 61 6e 67 65 5f 73 74 61 72 74 20 3d 20 30 3b 0a ange_start = 0;.
3190: 09 72 61 6e 67 65 5f 65 6e 64 20 20 20 3d 20 30 .range_end = 0
31a0: 3b 0a 09 72 61 6e 67 65 5f 72 65 71 75 65 73 74 ;..range_request
31b0: 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 6c 65 6e = 0;..range_len
31c0: 67 74 68 20 3d 20 2d 31 3b 0a 0a 09 62 75 66 66 gth = -1;...buff
31d0: 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e er = buffer_st->
31e0: 70 61 74 68 5f 62 3b 0a 09 62 75 66 66 65 72 5f path_b;..buffer_
31f0: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 len = sizeof(buf
3200: 66 65 72 5f 73 74 2d 3e 70 61 74 68 5f 62 29 3b fer_st->path_b);
3210: 0a 0a 09 74 6d 70 62 75 66 66 65 72 20 3d 20 62 ...tmpbuffer = b
3220: 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 uffer_st->tmpbuf
3230: 3b 0a 09 74 6d 70 62 75 66 66 65 72 5f 6c 65 6e ;..tmpbuffer_len
3240: 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 66 65 72 = sizeof(buffer
3250: 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 _st->tmpbuf);...
3260: 66 67 65 74 73 28 62 75 66 66 65 72 2c 20 62 75 fgets(buffer, bu
3270: 66 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 0a ffer_len, fp);..
3280: 09 6d 65 74 68 6f 64 20 3d 20 62 75 66 66 65 72 .method = buffer
3290: 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 73 74 72 ;...buffer = str
32a0: 63 68 72 28 62 75 66 66 65 72 2c 20 27 20 27 29 chr(buffer, ' ')
32b0: 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20 3d 3d ;..if (buffer ==
32c0: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 NULL) {...retur
32d0: 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 2a 62 n(NULL);..}...*b
32e0: 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 62 uffer = '\0';..b
32f0: 75 66 66 65 72 2b 2b 3b 0a 0a 09 70 61 74 68 20 uffer++;...path
3300: 3d 20 62 75 66 66 65 72 3b 0a 0a 09 62 75 66 66 = buffer;...buff
3310: 65 72 20 3d 20 73 74 72 63 68 72 28 62 75 66 66 er = strchr(buff
3320: 65 72 2c 20 27 20 27 29 3b 0a 09 69 66 20 28 62 er, ' ');..if (b
3330: 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c 29 20 7b uffer != NULL) {
3340: 0a 09 09 2a 62 75 66 66 65 72 20 3d 20 27 5c 30 ...*buffer = '\0
3350: 27 3b 0a 09 09 62 75 66 66 65 72 2b 2b 3b 0a 09 ';...buffer++;..
3360: 7d 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 }...for (i = 0;
3370: 69 20 3c 20 31 30 30 3b 20 69 2b 2b 29 20 7b 0a i < 100; i++) {.
3380: 09 09 66 67 65 74 73 28 74 6d 70 62 75 66 66 65 ..fgets(tmpbuffe
3390: 72 2c 20 74 6d 70 62 75 66 66 65 72 5f 6c 65 6e r, tmpbuffer_len
33a0: 2c 20 66 70 29 3b 0a 0a 09 09 69 66 20 28 73 74 , fp);....if (st
33b0: 72 6e 63 61 73 65 63 6d 70 28 74 6d 70 62 75 66 rncasecmp(tmpbuf
33c0: 66 65 72 2c 20 22 52 61 6e 67 65 3a 20 22 2c 20 fer, "Range: ",
33d0: 37 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 77 6f 7) == 0) {....wo
33e0: 72 6b 62 75 66 66 65 72 20 3d 20 74 6d 70 62 75 rkbuffer = tmpbu
33f0: 66 66 65 72 20 2b 20 37 3b 0a 0a 09 09 09 69 66 ffer + 7;.....if
3400: 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 77 6f (strncasecmp(wo
3410: 72 6b 62 75 66 66 65 72 2c 20 22 62 79 74 65 73 rkbuffer, "bytes
3420: 3d 22 2c 20 36 29 20 3d 3d 20 30 29 20 7b 0a 09 =", 6) == 0) {..
3430: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 2b 3d ...workbuffer +=
3440: 20 36 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f 72 6;......range_r
3450: 65 71 75 65 73 74 20 3d 20 31 3b 0a 0a 09 09 09 equest = 1;.....
3460: 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d 20 73 .range_start = s
3470: 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66 trtoull(workbuff
3480: 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 72 5f er, &workbuffer_
3490: 6e 65 78 74 2c 20 31 30 29 3b 0a 0a 09 09 09 09 next, 10);......
34a0: 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 77 6f 72 workbuffer = wor
34b0: 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 0a 09 kbuffer_next;...
34c0: 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 66 66 ...if (*workbuff
34d0: 65 72 20 3d 3d 20 27 2d 27 29 20 7b 0a 09 09 09 er == '-') {....
34e0: 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a ..workbuffer++;.
34f0: 0a 09 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 ......if (*workb
3500: 75 66 66 65 72 20 21 3d 20 27 5c 72 27 20 26 26 uffer != '\r' &&
3510: 20 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 *workbuffer !=
3520: 27 5c 6e 27 29 20 7b 0a 09 09 09 09 09 09 72 61 '\n') {.......ra
3530: 6e 67 65 5f 65 6e 64 20 3d 20 73 74 72 74 6f 75 nge_end = strtou
3540: 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 26 ll(workbuffer, &
3550: 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 2c workbuffer_next,
3560: 20 31 30 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09 10);......}....
3570: 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a 0a 09 09 69 .}....}...}....i
3580: 66 20 28 6d 65 6d 63 6d 70 28 74 6d 70 62 75 66 f (memcmp(tmpbuf
3590: 66 65 72 2c 20 22 5c 72 5c 6e 22 2c 20 32 29 20 fer, "\r\n", 2)
35a0: 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72 65 61 6b == 0) {....break
35b0: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 57 65 ;...}..}.../* We
35c0: 20 6f 6e 6c 79 20 68 61 6e 64 6c 65 20 74 68 65 only handle the
35d0: 20 22 47 45 54 22 20 6d 65 74 68 6f 64 20 2a 2f "GET" method */
35e0: 0a 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 ..if (strcasecmp
35f0: 28 6d 65 74 68 6f 64 2c 20 22 67 65 74 22 29 20 (method, "get")
3600: 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e != 0) {...return
3610: 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 (NULL);..}.../*
3620: 44 65 74 65 72 6d 69 6e 65 20 72 61 6e 67 65 20 Determine range
3630: 2a 2f 0a 09 69 66 20 28 72 61 6e 67 65 5f 65 6e */..if (range_en
3640: 64 20 21 3d 20 30 29 20 7b 0a 09 09 69 66 20 28 d != 0) {...if (
3650: 72 61 6e 67 65 5f 65 6e 64 20 3c 3d 20 72 61 6e range_end <= ran
3660: 67 65 5f 73 74 61 72 74 29 20 7b 0a 09 09 09 72 ge_start) {....r
3670: 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d eturn(NULL);...}
3680: 0a 0a 09 09 72 61 6e 67 65 5f 6c 65 6e 67 74 68 ....range_length
3690: 20 3d 20 72 61 6e 67 65 5f 65 6e 64 20 2d 20 72 = range_end - r
36a0: 61 6e 67 65 5f 73 74 61 72 74 3b 0a 0a 09 09 66 ange_start;....f
36b0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
36c0: 75 67 28 22 43 6f 6d 70 75 74 69 6e 67 20 6c 65 ug("Computing le
36d0: 6e 67 74 68 20 70 61 72 61 6d 65 74 65 72 3a 20 ngth parameter:
36e0: 25 6c 6c 75 20 3d 20 25 6c 6c 75 20 2d 20 25 6c %llu = %llu - %l
36f0: 6c 75 22 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 lu",....(unsigne
3700: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e d long long) ran
3710: 67 65 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 28 75 ge_length,....(u
3720: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
3730: 67 29 20 72 61 6e 67 65 5f 65 6e 64 2c 0a 09 09 g) range_end,...
3740: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
3750: 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 73 74 61 72 long) range_star
3760: 74 0a 09 09 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 46 t...);..}.../* F
3770: 69 6c 6c 20 75 70 20 73 74 72 75 63 74 75 72 65 ill up structure
3780: 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 09 62 to return */..b
3790: 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 20 20 uffer_st->path
37a0: 20 3d 20 70 61 74 68 3b 0a 09 62 75 66 66 65 72 = path;..buffer
37b0: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e _st->headers.ran
37c0: 67 65 2e 70 72 65 73 65 6e 74 20 3d 20 72 61 6e ge.present = ran
37d0: 67 65 5f 72 65 71 75 65 73 74 3b 0a 09 62 75 66 ge_request;..buf
37e0: 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e fer_st->headers.
37f0: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 20 3d 20 range.offset =
3800: 72 61 6e 67 65 5f 73 74 61 72 74 3b 0a 09 62 75 range_start;..bu
3810: 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 ffer_st->headers
3820: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 20 3d .range.length =
3830: 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 0a range_length;..
3840: 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 5f 73 .return(buffer_s
3850: 74 29 3b 0a 0a 09 2f 2a 20 4d 61 6b 65 20 63 6f t);.../* Make co
3860: 6d 70 69 6c 65 72 20 68 61 70 70 79 20 2a 2f 0a mpiler happy */.
3870: 09 66 64 20 3d 20 66 64 3b 0a 7d 0a 0a 2f 2a 20 .fd = fd;.}../*
3880: 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
3890: 70 61 67 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 page */.static v
38a0: 6f 69 64 20 66 69 6c 65 64 5f 65 72 72 6f 72 5f oid filed_error_
38b0: 70 61 67 65 28 46 49 4c 45 20 2a 66 70 2c 20 63 page(FILE *fp, c
38c0: 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 65 5f onst char *date_
38d0: 63 75 72 72 65 6e 74 2c 20 69 6e 74 20 65 72 72 current, int err
38e0: 6f 72 5f 6e 75 6d 62 65 72 29 20 7b 0a 09 63 68 or_number) {..ch
38f0: 61 72 20 2a 65 72 72 6f 72 5f 73 74 72 69 6e 67 ar *error_string
3900: 20 3d 20 22 3c 68 74 6d 6c 3e 3c 68 65 61 64 3e = "<html><head>
3910: 3c 74 69 74 6c 65 3e 45 52 52 4f 52 3c 2f 74 69 <title>ERROR</ti
3920: 74 6c 65 3e 3c 2f 68 65 61 64 3e 3c 62 6f 64 79 tle></head><body
3930: 3e 55 6e 61 62 6c 65 20 74 6f 20 70 72 6f 63 65 >Unable to proce
3940: 73 73 20 72 65 71 75 65 73 74 3c 2f 62 6f 64 79 ss request</body
3950: 3e 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a 09 66 70 72 ></html>";...fpr
3960: 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f 31 intf(fp, "HTTP/1
3970: 2e 31 20 25 69 20 4e 6f 74 20 4f 4b 5c 72 5c 6e .1 %i Not OK\r\n
3980: 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 Date: %s\r\nServ
3990: 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 er: filed\r\nLas
39a0: 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 t-Modified: %s\r
39b0: 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 \nContent-Length
39c0: 3a 20 25 6c 6c 75 5c 72 5c 6e 43 6f 6e 74 65 6e : %llu\r\nConten
39d0: 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e 43 6f t-Type: %s\r\nCo
39e0: 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c nnection: close\
39f0: 72 5c 6e 5c 72 5c 6e 25 73 22 2c 0a 09 09 65 72 r\n\r\n%s",...er
3a00: 72 6f 72 5f 6e 75 6d 62 65 72 2c 0a 09 09 64 61 ror_number,...da
3a10: 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64 61 te_current,...da
3a20: 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 28 75 te_current,...(u
3a30: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
3a40: 67 29 20 73 74 72 6c 65 6e 28 65 72 72 6f 72 5f g) strlen(error_
3a50: 73 74 72 69 6e 67 29 2c 0a 09 09 22 74 65 78 74 string),..."text
3a60: 2f 68 74 6d 6c 22 2c 0a 09 09 65 72 72 6f 72 5f /html",...error_
3a70: 73 74 72 69 6e 67 0a 09 29 3b 0a 7d 0a 0a 2f 2a string..);.}../*
3a80: 20 48 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c 65 Handle a single
3a90: 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 61 20 request from a
3aa0: 63 6c 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 63 client */.static
3ab0: 20 76 6f 69 64 20 66 69 6c 65 64 5f 68 61 6e 64 void filed_hand
3ac0: 6c 65 5f 63 6c 69 65 6e 74 28 69 6e 74 20 66 64 le_client(int fd
3ad0: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 , struct filed_h
3ae0: 74 74 70 5f 72 65 71 75 65 73 74 20 2a 72 65 71 ttp_request *req
3af0: 75 65 73 74 2c 20 73 74 72 75 63 74 20 66 69 6c uest, struct fil
3b00: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f ed_log_entry *lo
3b10: 67 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c g) {..struct fil
3b20: 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c ed_fileinfo *fil
3b30: 65 69 6e 66 6f 3b 0a 09 73 73 69 7a 65 5f 74 20 einfo;..ssize_t
3b40: 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 73 sendfile_ret;..s
3b50: 69 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f 73 ize_t sendfile_s
3b60: 69 7a 65 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 64 ize;..off_t send
3b70: 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e file_offset, sen
3b80: 64 66 69 6c 65 5f 73 65 6e 74 2c 20 73 65 6e 64 dfile_sent, send
3b90: 66 69 6c 65 5f 6c 65 6e 3b 0a 09 63 68 61 72 20 file_len;..char
3ba0: 2a 70 61 74 68 3b 0a 09 63 68 61 72 20 2a 64 61 *path;..char *da
3bb0: 74 65 5f 63 75 72 72 65 6e 74 2c 20 64 61 74 65 te_current, date
3bc0: 5f 63 75 72 72 65 6e 74 5f 62 5b 36 34 5d 3b 0a _current_b[64];.
3bd0: 09 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b 0a .int http_code;.
3be0: 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 2f 2a 20 .FILE *fp;.../*
3bf0: 44 65 74 65 72 6d 69 6e 65 20 63 75 72 72 65 6e Determine curren
3c00: 74 20 74 69 6d 65 20 2a 2f 0a 09 64 61 74 65 5f t time */..date_
3c10: 63 75 72 72 65 6e 74 20 3d 20 66 69 6c 65 64 5f current = filed_
3c20: 66 6f 72 6d 61 74 5f 74 69 6d 65 28 64 61 74 65 format_time(date
3c30: 5f 63 75 72 72 65 6e 74 5f 62 2c 20 73 69 7a 65 _current_b, size
3c40: 6f 66 28 64 61 74 65 5f 63 75 72 72 65 6e 74 5f of(date_current_
3c50: 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 29 3b b), time(NULL));
3c60: 0a 0a 09 2f 2a 20 4f 70 65 6e 20 73 6f 63 6b 65 .../* Open socke
3c70: 74 20 61 73 20 41 4e 53 49 20 49 2f 4f 20 66 6f t as ANSI I/O fo
3c80: 72 20 65 61 73 65 20 6f 66 20 75 73 65 20 2a 2f r ease of use */
3c90: 0a 09 66 70 20 3d 20 66 64 6f 70 65 6e 28 66 64 ..fp = fdopen(fd
3ca0: 2c 20 22 77 2b 62 22 29 3b 0a 09 69 66 20 28 66 , "w+b");..if (f
3cb0: 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 63 p == NULL) {...c
3cc0: 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74 lose(fd);....ret
3cd0: 75 72 6e 3b 0a 09 7d 0a 0a 09 72 65 71 75 65 73 urn;..}...reques
3ce0: 74 20 3d 20 66 69 6c 65 64 5f 67 65 74 5f 68 74 t = filed_get_ht
3cf0: 74 70 5f 72 65 71 75 65 73 74 28 66 70 2c 20 72 tp_request(fp, r
3d00: 65 71 75 65 73 74 29 3b 0a 0a 09 69 66 20 28 72 equest);...if (r
3d10: 65 71 75 65 73 74 20 3d 3d 20 4e 55 4c 4c 20 7c equest == NULL |
3d20: 7c 20 72 65 71 75 65 73 74 2d 3e 70 61 74 68 20 | request->path
3d30: 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c == NULL) {...fil
3d40: 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70 ed_error_page(fp
3d50: 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 , date_current,
3d60: 35 30 30 29 3b 0a 0a 09 09 6c 6f 67 2d 3e 62 75 500);....log->bu
3d70: 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a ffer[0] = '\0';.
3d80: 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 ..log->http_code
3d90: 20 3d 20 35 30 30 3b 0a 09 09 6c 6f 67 2d 3e 72 = 500;...log->r
3da0: 65 61 73 6f 6e 20 3d 20 22 66 6f 72 6d 61 74 22 eason = "format"
3db0: 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 ;....filed_log_e
3dc0: 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 09 66 63 ntry(log);....fc
3dd0: 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 09 72 65 74 lose(fp);....ret
3de0: 75 72 6e 3b 0a 09 7d 0a 0a 09 70 61 74 68 20 3d urn;..}...path =
3df0: 20 72 65 71 75 65 73 74 2d 3e 70 61 74 68 3b 0a request->path;.
3e00: 09 73 74 72 63 70 79 28 6c 6f 67 2d 3e 62 75 66 .strcpy(log->buf
3e10: 66 65 72 2c 20 70 61 74 68 29 3b 0a 0a 09 68 74 fer, path);...ht
3e20: 74 70 5f 63 6f 64 65 20 3d 20 2d 31 3b 0a 0a 09 tp_code = -1;...
3e30: 66 69 6c 65 69 6e 66 6f 20 3d 20 66 69 6c 65 64 fileinfo = filed
3e40: 5f 6f 70 65 6e 5f 66 69 6c 65 28 70 61 74 68 2c _open_file(path,
3e50: 20 26 72 65 71 75 65 73 74 2d 3e 66 69 6c 65 69 &request->filei
3e60: 6e 66 6f 29 3b 0a 09 69 66 20 28 66 69 6c 65 69 nfo);..if (filei
3e70: 6e 66 6f 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 nfo == NULL) {..
3e80: 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 .filed_error_pag
3e90: 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 e(fp, date_curre
3ea0: 6e 74 2c 20 34 30 34 29 3b 0a 0a 09 09 6c 6f 67 nt, 404);....log
3eb0: 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 34 30 ->http_code = 40
3ec0: 34 3b 0a 09 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 4;...log->reason
3ed0: 20 3d 20 22 6f 70 65 6e 5f 66 61 69 6c 65 64 22 = "open_failed"
3ee0: 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 ;....filed_log_e
3ef0: 6e 74 72 79 28 6c 6f 67 29 3b 0a 09 7d 20 65 6c ntry(log);..} el
3f00: 73 65 20 7b 0a 09 09 69 66 20 28 72 65 71 75 65 se {...if (reque
3f10: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
3f20: 65 2e 6f 66 66 73 65 74 20 21 3d 20 30 20 7c 7c e.offset != 0 ||
3f30: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
3f40: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3e s.range.length >
3f50: 3d 20 30 29 20 7b 0a 09 09 09 69 66 20 28 72 65 = 0) {....if (re
3f60: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
3f70: 61 6e 67 65 2e 6f 66 66 73 65 74 20 3e 3d 20 66 ange.offset >= f
3f80: 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 29 20 7b 0a ileinfo->len) {.
3f90: 09 09 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f ....filed_error_
3fa0: 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f 63 75 page(fp, date_cu
3fb0: 72 72 65 6e 74 2c 20 34 31 36 29 3b 0a 0a 09 09 rrent, 416);....
3fc0: 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 ..log->http_code
3fd0: 20 3d 20 34 31 36 3b 0a 09 09 09 09 6c 6f 67 2d = 416;.....log-
3fe0: 3e 72 65 61 73 6f 6e 20 3d 20 22 72 61 6e 67 65 >reason = "range
3ff0: 5f 69 6e 76 61 6c 69 64 22 3b 0a 0a 09 09 09 09 _invalid";......
4000: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 filed_log_entry(
4010: 6c 6f 67 29 3b 0a 09 09 09 7d 20 65 6c 73 65 20 log);....} else
4020: 7b 0a 09 09 09 09 69 66 20 28 72 65 71 75 65 73 {.....if (reques
4030: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
4040: 2e 6c 65 6e 67 74 68 20 3d 3d 20 28 28 6f 66 66 .length == ((off
4050: 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09 09 09 09 _t) -1)) {......
4060: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 filed_log_msg_de
4070: 62 75 67 28 22 43 6f 6d 70 75 74 69 6e 67 20 6c bug("Computing l
4080: 65 6e 67 74 68 20 74 6f 20 66 69 74 20 69 6e 20 ength to fit in
4090: 62 6f 75 6e 64 73 3a 20 66 69 6c 65 69 6e 66 6f bounds: fileinfo
40a0: 2d 3e 6c 65 6e 20 3d 20 25 6c 6c 75 2c 20 72 65 ->len = %llu, re
40b0: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
40c0: 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d 20 25 6c ange.offset = %l
40d0: 6c 75 22 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 lu",.......(unsi
40e0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
40f0: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 2c 0a 09 fileinfo->len,..
4100: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
4110: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 ong long) reques
4120: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
4130: 2e 6f 66 66 73 65 74 0a 09 09 09 09 09 29 3b 0a .offset......);.
4140: 0a 09 09 09 09 09 72 65 71 75 65 73 74 2d 3e 68 ......request->h
4150: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e eaders.range.len
4160: 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e gth = fileinfo->
4170: 6c 65 6e 20 2d 20 72 65 71 75 65 73 74 2d 3e 68 len - request->h
4180: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
4190: 73 65 74 3b 0a 09 09 09 09 7d 0a 0a 09 09 09 09 set;.....}......
41a0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 filed_log_msg_de
41b0: 62 75 67 28 22 50 61 72 74 69 61 6c 20 72 65 71 bug("Partial req
41c0: 75 65 73 74 2c 20 73 74 61 72 74 69 6e 67 20 61 uest, starting a
41d0: 74 3a 20 25 6c 6c 75 20 61 6e 64 20 72 75 6e 6e t: %llu and runn
41e0: 69 6e 67 20 66 6f 72 20 25 6c 6c 69 20 62 79 74 ing for %lli byt
41f0: 65 73 22 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 es",......(unsig
4200: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 ned long long) r
4210: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
4220: 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09 range.offset,...
4230: 09 09 09 28 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 ...(long long) r
4240: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
4250: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 0a 09 09 09 range.length....
4260: 09 29 3b 0a 0a 09 09 09 09 68 74 74 70 5f 63 6f .);......http_co
4270: 64 65 20 3d 20 32 30 36 3b 0a 09 09 09 7d 0a 09 de = 206;....}..
4280: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 69 66 20 .} else {....if
4290: 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 (request->header
42a0: 73 2e 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 29 s.range.present)
42b0: 20 7b 0a 09 09 09 09 68 74 74 70 5f 63 6f 64 65 {.....http_code
42c0: 20 3d 20 32 30 36 3b 0a 09 09 09 7d 20 65 6c 73 = 206;....} els
42d0: 65 20 7b 0a 09 09 09 09 68 74 74 70 5f 63 6f 64 e {.....http_cod
42e0: 65 20 3d 20 32 30 30 3b 0a 09 09 09 7d 0a 09 09 e = 200;....}...
42f0: 09 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 .request->header
4300: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d s.range.offset =
4310: 20 30 3b 0a 09 09 09 72 65 71 75 65 73 74 2d 3e 0;....request->
4320: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 headers.range.le
4330: 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d ngth = fileinfo-
4340: 3e 6c 65 6e 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 >len;...}....if
4350: 28 68 74 74 70 5f 63 6f 64 65 20 3e 20 30 29 20 (http_code > 0)
4360: 7b 0a 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c {....fprintf(fp,
4370: 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b "HTTP/1.1 %i OK
4380: 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e \r\nDate: %s\r\n
4390: 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c Server: filed\r\
43a0: 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 nLast-Modified:
43b0: 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 %s\r\nContent-Le
43c0: 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 41 63 ngth: %llu\r\nAc
43d0: 63 65 70 74 2d 52 61 6e 67 65 73 3a 20 62 79 74 cept-Ranges: byt
43e0: 65 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 es\r\nContent-Ty
43f0: 70 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 pe: %s\r\nConnec
4400: 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 22 tion: close\r\n"
4410: 2c 0a 09 09 09 09 68 74 74 70 5f 63 6f 64 65 2c ,.....http_code,
4420: 0a 09 09 09 09 64 61 74 65 5f 63 75 72 72 65 6e .....date_curren
4430: 74 2c 0a 09 09 09 09 66 69 6c 65 69 6e 66 6f 2d t,.....fileinfo-
4440: 3e 6c 61 73 74 6d 6f 64 2c 0a 09 09 09 09 28 75 >lastmod,.....(u
4450: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
4460: 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 g) request->head
4470: 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 ers.range.length
4480: 2c 0a 09 09 09 09 66 69 6c 65 69 6e 66 6f 2d 3e ,.....fileinfo->
4490: 74 79 70 65 0a 09 09 09 29 3b 0a 09 09 09 69 66 type....);....if
44a0: 20 28 68 74 74 70 5f 63 6f 64 65 20 3d 3d 20 32 (http_code == 2
44b0: 30 36 29 20 7b 0a 09 09 09 09 66 70 72 69 6e 74 06) {.....fprint
44c0: 66 28 66 70 2c 20 22 43 6f 6e 74 65 6e 74 2d 52 f(fp, "Content-R
44d0: 61 6e 67 65 3a 20 62 79 74 65 73 20 25 6c 6c 75 ange: bytes %llu
44e0: 2d 25 6c 6c 75 2f 25 6c 6c 75 5c 72 5c 6e 22 2c -%llu/%llu\r\n",
44f0: 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ......(unsigned
4500: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 long long) reque
4510: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
4520: 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 09 09 28 e.offset,......(
4530: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
4540: 6e 67 29 20 28 72 65 71 75 65 73 74 2d 3e 68 65 ng) (request->he
4550: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
4560: 65 74 20 2b 20 72 65 71 75 65 73 74 2d 3e 68 65 et + request->he
4570: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 aders.range.leng
4580: 74 68 20 2d 20 31 29 2c 0a 09 09 09 09 09 28 75 th - 1),......(u
4590: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
45a0: 67 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e g) fileinfo->len
45b0: 0a 09 09 09 09 29 3b 0a 09 09 09 7d 0a 09 09 09 .....);....}....
45c0: 66 70 72 69 6e 74 66 28 66 70 2c 20 22 5c 72 5c fprintf(fp, "\r\
45d0: 6e 22 29 3b 0a 09 09 09 66 66 6c 75 73 68 28 66 n");....fflush(f
45e0: 70 29 3b 0a 0a 09 09 09 6c 6f 67 2d 3e 68 74 74 p);.....log->htt
45f0: 70 5f 63 6f 64 65 20 3d 20 68 74 74 70 5f 63 6f p_code = http_co
4600: 64 65 3b 0a 09 09 09 6c 6f 67 2d 3e 72 65 61 73 de;....log->reas
4610: 6f 6e 20 3d 20 22 4f 4b 22 3b 0a 09 09 09 6c 6f on = "OK";....lo
4620: 67 2d 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 74 g->starttime = t
4630: 69 6d 65 28 4e 55 4c 4c 29 3b 0a 09 09 09 6c 6f ime(NULL);....lo
4640: 67 2d 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 g->req_offset =
4650: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
4660: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 .range.offset;..
4670: 09 09 6c 6f 67 2d 3e 72 65 71 5f 6c 65 6e 67 74 ..log->req_lengt
4680: 68 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 h = request->hea
4690: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 ders.range.lengt
46a0: 68 3b 0a 09 09 09 6c 6f 67 2d 3e 66 69 6c 65 5f h;....log->file_
46b0: 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 length = fileinf
46c0: 6f 2d 3e 6c 65 6e 3b 0a 0a 23 69 66 64 65 66 20 o->len;..#ifdef
46d0: 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 FILED_NONBLOCK_H
46e0: 54 54 50 0a 09 09 09 69 6e 74 20 73 6f 63 6b 65 TTP....int socke
46f0: 74 5f 66 6c 61 67 73 3b 0a 09 09 09 66 64 5f 73 t_flags;....fd_s
4700: 65 74 20 72 66 64 2c 20 77 66 64 3b 0a 09 09 09 et rfd, wfd;....
4710: 63 68 61 72 20 73 69 6e 6b 62 75 66 5b 38 31 39 char sinkbuf[819
4720: 32 5d 3b 0a 09 09 09 73 73 69 7a 65 5f 74 20 72 2];....ssize_t r
4730: 65 61 64 5f 72 65 74 3b 0a 0a 09 09 09 46 44 5f ead_ret;.....FD_
4740: 5a 45 52 4f 28 26 72 66 64 29 3b 0a 09 09 09 46 ZERO(&rfd);....F
4750: 44 5f 5a 45 52 4f 28 26 77 66 64 29 3b 0a 09 09 D_ZERO(&wfd);...
4760: 09 46 44 5f 53 45 54 28 66 64 2c 20 26 72 66 64 .FD_SET(fd, &rfd
4770: 29 3b 0a 09 09 09 46 44 5f 53 45 54 28 66 64 2c );....FD_SET(fd,
4780: 20 26 77 66 64 29 3b 0a 0a 09 09 09 73 6f 63 6b &wfd);.....sock
4790: 65 74 5f 66 6c 61 67 73 20 3d 20 66 63 6e 74 6c et_flags = fcntl
47a0: 28 66 64 2c 20 46 5f 47 45 54 46 4c 29 3b 0a 09 (fd, F_GETFL);..
47b0: 09 09 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 ..fcntl(fd, F_SE
47c0: 54 46 4c 2c 20 73 6f 63 6b 65 74 5f 66 6c 61 67 TFL, socket_flag
47d0: 73 20 7c 20 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 3b s | O_NONBLOCK);
47e0: 0a 23 65 6e 64 69 66 0a 0a 09 09 09 73 65 6e 64 .#endif.....send
47f0: 66 69 6c 65 5f 6f 66 66 73 65 74 20 3d 20 72 65 file_offset = re
4800: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
4810: 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 09 09 ange.offset;....
4820: 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 20 72 sendfile_len = r
4830: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
4840: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09 09 range.length;...
4850: 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 3d .sendfile_sent =
4860: 20 30 3b 0a 09 09 09 77 68 69 6c 65 20 28 31 29 0;....while (1)
4870: 20 7b 0a 09 09 09 09 69 66 20 28 73 65 6e 64 66 {.....if (sendf
4880: 69 6c 65 5f 6c 65 6e 20 3e 20 46 49 4c 45 44 5f ile_len > FILED_
4890: 53 45 4e 44 46 49 4c 45 5f 4d 41 58 29 20 7b 0a SENDFILE_MAX) {.
48a0: 09 09 09 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 .....sendfile_si
48b0: 7a 65 20 3d 20 46 49 4c 45 44 5f 53 45 4e 44 46 ze = FILED_SENDF
48c0: 49 4c 45 5f 4d 41 58 3b 0a 09 09 09 09 7d 20 65 ILE_MAX;.....} e
48d0: 6c 73 65 20 7b 0a 09 09 09 09 09 73 65 6e 64 66 lse {......sendf
48e0: 69 6c 65 5f 73 69 7a 65 20 3d 20 73 65 6e 64 66 ile_size = sendf
48f0: 69 6c 65 5f 6c 65 6e 3b 0a 09 09 09 09 7d 0a 0a ile_len;.....}..
4900: 09 09 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 ....sendfile_ret
4910: 20 3d 20 73 65 6e 64 66 69 6c 65 28 66 64 2c 20 = sendfile(fd,
4920: 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 2c 20 26 73 fileinfo->fd, &s
4930: 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 endfile_offset,
4940: 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 29 3b 0a sendfile_size);.
4950: 09 09 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65 ....if (sendfile
4960: 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 23 69 66 _ret <= 0) {.#if
4970: 64 65 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f def FILED_NONBLO
4980: 43 4b 5f 48 54 54 50 0a 09 09 09 09 09 69 66 20 CK_HTTP......if
4990: 28 65 72 72 6e 6f 20 3d 3d 20 45 41 47 41 49 4e (errno == EAGAIN
49a0: 29 20 7b 0a 09 09 09 09 09 09 73 65 6e 64 66 69 ) {.......sendfi
49b0: 6c 65 5f 72 65 74 20 3d 20 30 3b 0a 0a 09 09 09 le_ret = 0;.....
49c0: 09 09 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 ...while (1) {..
49d0: 09 09 09 09 09 09 73 65 6c 65 63 74 28 66 64 20 ......select(fd
49e0: 2b 20 31 2c 20 26 72 66 64 2c 20 26 77 66 64 2c + 1, &rfd, &wfd,
49f0: 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 09 09 NULL, NULL);...
4a00: 09 09 09 09 09 69 66 20 28 46 44 5f 49 53 53 45 .....if (FD_ISSE
4a10: 54 28 66 64 2c 20 26 72 66 64 29 29 20 7b 0a 09 T(fd, &rfd)) {..
4a20: 09 09 09 09 09 09 09 72 65 61 64 5f 72 65 74 20 .......read_ret
4a30: 3d 20 72 65 61 64 28 66 64 2c 20 73 69 6e 6b 62 = read(fd, sinkb
4a40: 75 66 2c 20 73 69 7a 65 6f 66 28 73 69 6e 6b 62 uf, sizeof(sinkb
4a50: 75 66 29 29 3b 0a 0a 09 09 09 09 09 09 09 09 69 uf));..........i
4a60: 66 20 28 72 65 61 64 5f 72 65 74 20 3c 3d 20 30 f (read_ret <= 0
4a70: 29 20 7b 0a 09 09 09 09 09 09 09 09 09 62 72 65 ) {..........bre
4a80: 61 6b 3b 0a 09 09 09 09 09 09 09 09 7d 0a 09 09 ak;.........}...
4a90: 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 09 09 69 .....}.........i
4aa0: 66 20 28 46 44 5f 49 53 53 45 54 28 66 64 2c 20 f (FD_ISSET(fd,
4ab0: 26 77 66 64 29 29 20 7b 0a 09 09 09 09 09 09 09 &wfd)) {........
4ac0: 09 72 65 61 64 5f 72 65 74 20 3d 20 31 3b 0a 0a .read_ret = 1;..
4ad0: 09 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 ........break;..
4ae0: 09 09 09 09 09 09 7d 0a 09 09 09 09 09 09 7d 0a ......}.......}.
4af0: 0a 09 09 09 09 09 09 69 66 20 28 72 65 61 64 5f .......if (read_
4b00: 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 ret <= 0) {.....
4b10: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 ...break;.......
4b20: 7d 0a 09 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a }......} else {.
4b30: 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 ......break;....
4b40: 09 09 7d 0a 23 65 6c 73 65 0a 09 09 09 09 09 62 ..}.#else......b
4b50: 72 65 61 6b 3b 0a 23 65 6e 64 69 66 0a 09 09 09 reak;.#endif....
4b60: 09 7d 0a 0a 09 09 09 09 73 65 6e 64 66 69 6c 65 .}......sendfile
4b70: 5f 6c 65 6e 20 2d 3d 20 73 65 6e 64 66 69 6c 65 _len -= sendfile
4b80: 5f 72 65 74 3b 0a 09 09 09 09 73 65 6e 64 66 69 _ret;.....sendfi
4b90: 6c 65 5f 73 65 6e 74 20 2b 3d 20 73 65 6e 64 66 le_sent += sendf
4ba0: 69 6c 65 5f 72 65 74 3b 0a 09 09 09 09 69 66 20 ile_ret;.....if
4bb0: 28 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 3d (sendfile_len ==
4bc0: 20 30 29 20 7b 0a 09 09 09 09 09 62 72 65 61 6b 0) {......break
4bd0: 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 0a 09 09 ;.....}....}....
4be0: 09 6c 6f 67 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 .log->endtime =
4bf0: 28 74 69 6d 65 5f 74 29 20 2d 31 3b 0a 09 09 09 (time_t) -1;....
4c00: 6c 6f 67 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 log->sent_length
4c10: 20 3d 20 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 = sendfile_sent
4c20: 3b 0a 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f ;.....filed_log_
4c30: 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 09 09 7d 0a entry(log);...}.
4c40: 0a 09 09 63 6c 6f 73 65 28 66 69 6c 65 69 6e 66 ...close(fileinf
4c50: 6f 2d 3e 66 64 29 3b 0a 09 7d 0a 0a 09 66 63 6c o->fd);..}...fcl
4c60: 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 74 75 72 ose(fp);...retur
4c70: 6e 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e 64 6c 65 20 n;.}../* Handle
4c80: 69 6e 63 6f 6d 69 6e 67 20 63 6f 6e 6e 65 63 74 incoming connect
4c90: 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 76 ions */.static v
4ca0: 6f 69 64 20 2a 66 69 6c 65 64 5f 77 6f 72 6b 65 oid *filed_worke
4cb0: 72 5f 74 68 72 65 61 64 28 76 6f 69 64 20 2a 61 r_thread(void *a
4cc0: 72 67 5f 76 29 20 7b 0a 09 73 74 72 75 63 74 20 rg_v) {..struct
4cd0: 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 filed_worker_thr
4ce0: 65 61 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 ead_args *arg;..
4cf0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 struct filed_htt
4d00: 70 5f 72 65 71 75 65 73 74 20 72 65 71 75 65 73 p_request reques
4d10: 74 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 t;..struct filed
4d20: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 3b _log_entry *log;
4d30: 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 ..struct sockadd
4d40: 72 5f 69 6e 36 20 61 64 64 72 3b 0a 09 63 68 61 r_in6 addr;..cha
4d50: 72 20 6c 6f 67 62 75 66 5f 69 70 5b 31 32 38 5d r logbuf_ip[128]
4d60: 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 ;..socklen_t add
4d70: 72 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 69 6c 75 rlen;..int failu
4d80: 72 65 5f 63 6f 75 6e 74 20 3d 20 30 2c 20 6d 61 re_count = 0, ma
4d90: 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 x_failure_count
4da0: 3d 20 46 49 4c 45 44 5f 4d 41 58 5f 46 41 49 4c = FILED_MAX_FAIL
4db0: 55 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 URE_COUNT;..int
4dc0: 6d 61 73 74 65 72 5f 66 64 2c 20 66 64 3b 0a 0a master_fd, fd;..
4dd0: 09 2f 2a 20 52 65 61 64 20 61 72 67 75 6d 65 6e ./* Read argumen
4de0: 74 73 20 2a 2f 0a 09 61 72 67 20 3d 20 61 72 67 ts */..arg = arg
4df0: 5f 76 3b 0a 0a 09 6d 61 73 74 65 72 5f 66 64 20 _v;...master_fd
4e00: 3d 20 61 72 67 2d 3e 66 64 3b 0a 0a 09 77 68 69 = arg->fd;...whi
4e10: 6c 65 20 28 31 29 20 7b 0a 09 09 2f 2a 20 46 61 le (1) {.../* Fa
4e20: 69 6c 75 72 65 20 6c 6f 6f 70 20 70 72 65 76 65 ilure loop preve
4e30: 6e 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 20 28 66 ntion */...if (f
4e40: 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3e 20 6d ailure_count > m
4e50: 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 ax_failure_count
4e60: 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 ) {....break;...
4e70: 7d 0a 0a 09 09 2f 2a 20 41 63 63 65 70 74 20 61 }..../* Accept a
4e80: 20 6e 65 77 20 63 6c 69 65 6e 74 20 2a 2f 0a 09 new client */..
4e90: 09 61 64 64 72 6c 65 6e 20 3d 20 73 69 7a 65 6f .addrlen = sizeo
4ea0: 66 28 61 64 64 72 29 3b 0a 09 09 66 64 20 3d 20 f(addr);...fd =
4eb0: 61 63 63 65 70 74 28 6d 61 73 74 65 72 5f 66 64 accept(master_fd
4ec0: 2c 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 , (struct sockad
4ed0: 64 72 20 2a 29 20 26 61 64 64 72 2c 20 26 61 64 dr *) &addr, &ad
4ee0: 64 72 6c 65 6e 29 3b 0a 0a 09 09 2f 2a 0a 09 09 drlen);..../*...
4ef0: 20 2a 20 49 66 20 77 65 20 66 61 69 6c 2c 20 6d * If we fail, m
4f00: 61 6b 65 20 61 20 6e 6f 74 65 20 6f 66 20 69 74 ake a note of it
4f10: 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 67 6f 20 so we don't go
4f20: 69 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f 66 0a 09 into a loop of..
4f30: 09 20 2a 20 61 63 63 65 70 74 28 29 20 66 61 69 . * accept() fai
4f40: 6c 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 69 66 20 ling... */...if
4f50: 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 09 2f 2a (fd < 0) {..../*
4f60: 20 4c 6f 67 20 74 68 65 20 6e 65 77 20 63 6f 6e Log the new con
4f70: 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 09 66 69 nection */....fi
4f80: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 43 43 led_log_msg("ACC
4f90: 45 50 54 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 EPT_FAILED");...
4fa0: 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 2b ..failure_count+
4fb0: 2b 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e 75 65 3b +;.....continue;
4fc0: 0a 09 09 7d 0a 0a 09 09 2f 2a 20 4c 6f 67 20 74 ...}..../* Log t
4fd0: 68 65 20 6e 65 77 20 63 6f 6e 6e 65 63 74 69 6f he new connectio
4fe0: 6e 20 2a 2f 0a 09 09 6c 6f 67 20 3d 20 66 69 6c n */...log = fil
4ff0: 65 64 5f 6c 6f 67 5f 6e 65 77 28 31 29 3b 0a 09 ed_log_new(1);..
5000: 09 69 66 20 28 6c 6f 67 20 3d 3d 20 4e 55 4c 4c .if (log == NULL
5010: 29 20 7b 0a 09 09 09 63 6c 6f 73 65 28 66 64 29 ) {....close(fd)
5020: 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e 75 65 3b 0a ;.....continue;.
5030: 09 09 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 79 70 65 ..}....log->type
5040: 20 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 = FILED_LOG_TYP
5050: 45 5f 54 52 41 4e 53 46 45 52 3b 0a 09 09 69 66 E_TRANSFER;...if
5060: 20 28 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 28 (filed_log_ip((
5070: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 struct sockaddr
5080: 2a 29 20 26 61 64 64 72 2c 20 6c 6f 67 2d 3e 69 *) &addr, log->i
5090: 70 2c 20 73 69 7a 65 6f 66 28 6c 6f 67 2d 3e 69 p, sizeof(log->i
50a0: 70 29 29 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 p)) == NULL) {..
50b0: 09 09 6c 6f 67 2d 3e 69 70 5b 30 5d 20 3d 20 27 ..log->ip[0] = '
50c0: 5c 30 27 3b 0a 09 09 7d 0a 09 09 6c 6f 67 2d 3e \0';...}...log->
50d0: 70 6f 72 74 20 3d 20 61 64 64 72 2e 73 69 6e 36 port = addr.sin6
50e0: 5f 70 6f 72 74 3b 0a 0a 09 09 2f 2a 20 52 65 73 _port;..../* Res
50f0: 65 74 20 66 61 69 6c 75 72 65 20 63 6f 75 6e 74 et failure count
5100: 2a 2f 0a 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 */...failure_cou
5110: 6e 74 20 3d 20 30 3b 0a 0a 09 09 2f 2a 20 48 61 nt = 0;..../* Ha
5120: 6e 64 6c 65 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 ndle socket */..
5130: 09 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c .filed_handle_cl
5140: 69 65 6e 74 28 66 64 2c 20 26 72 65 71 75 65 73 ient(fd, &reques
5150: 74 2c 20 6c 6f 67 29 3b 0a 09 7d 0a 0a 09 2f 2a t, log);..}.../*
5160: 20 52 65 70 6f 72 74 20 65 72 72 6f 72 20 2a 2f Report error */
5170: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 ..filed_log_msg(
5180: 22 54 48 52 45 41 44 5f 44 49 45 44 20 41 42 4e "THREAD_DIED ABN
5190: 4f 52 4d 41 4c 22 29 3b 0a 0a 09 72 65 74 75 72 ORMAL");...retur
51a0: 6e 28 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 4d 61 n(NULL);.../* Ma
51b0: 6b 65 20 63 6f 6d 70 69 6c 65 72 20 68 61 70 70 ke compiler happ
51c0: 79 20 2a 2f 0a 09 6c 6f 67 62 75 66 5f 69 70 5b y */..logbuf_ip[
51d0: 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 6c 6f 67 62 0] = '\0';..logb
51e0: 75 66 5f 69 70 5b 30 5d 20 3d 20 6c 6f 67 62 75 uf_ip[0] = logbu
51f0: 66 5f 69 70 5b 30 5d 3b 0a 0a 7d 0a 0a 2f 2a 20 f_ip[0];..}../*
5200: 43 72 65 61 74 65 20 77 6f 72 6b 65 72 20 74 68 Create worker th
5210: 72 65 61 64 73 20 2a 2f 0a 73 74 61 74 69 63 20 reads */.static
5220: 69 6e 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 int filed_worker
5230: 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 28 69 6e _threads_init(in
5240: 74 20 66 64 2c 20 69 6e 74 20 74 68 72 65 61 64 t fd, int thread
5250: 5f 63 6f 75 6e 74 29 20 7b 0a 09 73 74 72 75 63 _count) {..struc
5260: 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 t filed_worker_t
5270: 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 3b hread_args *arg;
5280: 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 ..pthread_t thre
5290: 61 64 69 64 3b 0a 09 69 6e 74 20 70 74 68 72 65 adid;..int pthre
52a0: 61 64 5f 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a ad_ret;..int i;.
52b0: 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 20 ..for (i = 0; i
52c0: 3c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 3b 20 < thread_count;
52d0: 69 2b 2b 29 20 7b 0a 09 09 61 72 67 20 3d 20 6d i++) {...arg = m
52e0: 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72 alloc(sizeof(*ar
52f0: 67 29 29 3b 0a 0a 09 09 61 72 67 2d 3e 66 64 20 g));....arg->fd
5300: 3d 20 66 64 3b 0a 0a 09 09 70 74 68 72 65 61 64 = fd;....pthread
5310: 5f 72 65 74 20 3d 20 70 74 68 72 65 61 64 5f 63 _ret = pthread_c
5320: 72 65 61 74 65 28 26 74 68 72 65 61 64 69 64 2c reate(&threadid,
5330: 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 77 6f 72 NULL, filed_wor
5340: 6b 65 72 5f 74 68 72 65 61 64 2c 20 61 72 67 29 ker_thread, arg)
5350: 3b 0a 09 09 69 66 20 28 70 74 68 72 65 61 64 5f ;...if (pthread_
5360: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 72 ret != 0) {....r
5370: 65 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 09 eturn(-1);...}..
5380: 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d }...return(0);.}
5390: 0a 0a 2f 2a 20 44 69 73 70 6c 61 79 20 68 65 6c ../* Display hel
53a0: 70 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 p */.static void
53b0: 20 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c filed_print_hel
53c0: 70 28 46 49 4c 45 20 2a 6f 75 74 70 75 74 2c 20 p(FILE *output,
53d0: 69 6e 74 20 6c 6f 6e 67 5f 68 65 6c 70 2c 20 63 int long_help, c
53e0: 6f 6e 73 74 20 63 68 61 72 20 2a 65 78 74 72 61 onst char *extra
53f0: 29 20 7b 0a 09 69 66 20 28 65 78 74 72 61 29 20 ) {..if (extra)
5400: 7b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 {...fprintf(outp
5410: 75 74 2c 20 22 25 73 5c 6e 22 2c 20 65 78 74 72 ut, "%s\n", extr
5420: 61 29 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74 66 a);..}...fprintf
5430: 28 6f 75 74 70 75 74 2c 20 22 55 73 61 67 65 3a (output, "Usage:
5440: 20 66 69 6c 65 64 20 5b 3c 6f 70 74 69 6f 6e 73 filed [<options
5450: 3e 5d 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 >]\n");..fprintf
5460: 28 6f 75 74 70 75 74 2c 20 22 20 20 4f 70 74 69 (output, " Opti
5470: 6f 6e 73 3a 5c 6e 22 29 3b 0a 09 66 70 72 69 6e ons:\n");..fprin
5480: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
5490: 20 20 2d 68 2c 20 2d 2d 68 65 6c 70 5c 6e 22 29 -h, --help\n")
54a0: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
54b0: 74 2c 20 22 20 20 20 20 20 20 2d 64 2c 20 2d 2d t, " -d, --
54c0: 64 61 65 6d 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 daemon\n");..fpr
54d0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
54e0: 20 20 20 20 2d 76 2c 20 2d 2d 76 65 72 73 69 6f -v, --versio
54f0: 6e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 n\n");..fprintf(
5500: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
5510: 62 20 3c 61 64 64 72 65 73 73 3e 2c 20 2d 2d 62 b <address>, --b
5520: 69 6e 64 20 3c 61 64 64 72 65 73 73 3e 5c 6e 22 ind <address>\n"
5530: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
5540: 75 74 2c 20 22 20 20 20 20 20 20 2d 70 20 3c 70 ut, " -p <p
5550: 6f 72 74 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70 6f ort>, --port <po
5560: 72 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 rt>\n");..fprint
5570: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
5580: 20 2d 74 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d 74 -t <count>, --t
5590: 68 72 65 61 64 73 20 3c 63 6f 75 6e 74 3e 5c 6e hreads <count>\n
55a0: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 ");..fprintf(out
55b0: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 63 20 3c put, " -c <
55c0: 65 6e 74 72 69 65 73 3e 2c 20 2d 2d 63 61 63 68 entries>, --cach
55d0: 65 20 3c 65 6e 74 72 69 65 73 3e 5c 6e 22 29 3b e <entries>\n");
55e0: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
55f0: 2c 20 22 20 20 20 20 20 20 2d 6c 20 3c 66 69 6c , " -l <fil
5600: 65 3e 2c 20 2d 2d 6c 6f 67 20 3c 66 69 6c 65 3e e>, --log <file>
5610: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f \n");..fprintf(o
5620: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 75 utput, " -u
5630: 20 3c 75 73 65 72 3e 2c 20 2d 2d 75 73 65 72 20 <user>, --user
5640: 3c 75 73 65 72 3e 5c 6e 22 29 3b 0a 09 66 70 72 <user>\n");..fpr
5650: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
5660: 20 20 20 20 2d 72 20 3c 64 69 72 65 63 74 6f 72 -r <director
5670: 79 3e 2c 20 2d 2d 72 6f 6f 74 20 3c 64 69 72 65 y>, --root <dire
5680: 63 74 6f 72 79 3e 5c 6e 22 29 3b 0a 0a 09 69 66 ctory>\n");...if
5690: 20 28 6c 6f 6e 67 5f 68 65 6c 70 29 20 7b 0a 09 (long_help) {..
56a0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
56b0: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 "\n");...fprint
56c0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 55 73 61 f(output, " Usa
56d0: 67 65 3a 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ge:\n");...fprin
56e0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
56f0: 20 20 2d 68 20 28 6f 72 20 2d 2d 68 65 6c 70 29 -h (or --help)
5700: 20 70 72 69 6e 74 73 20 74 68 69 73 20 75 73 61 prints this usa
5710: 67 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 5c ge information.\
5720: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
5730: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
5740: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5750: 22 20 20 20 20 20 20 2d 64 20 28 6f 72 20 2d 2d " -d (or --
5760: 64 61 65 6d 6f 6e 29 20 69 6e 73 74 72 75 63 74 daemon) instruct
5770: 73 20 66 69 6c 65 64 20 74 6f 20 62 65 63 6f 6d s filed to becom
5780: 65 20 61 20 64 61 65 6d 6f 6e 20 61 66 74 65 72 e a daemon after
5790: 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 5c 6e 22 initializing\n"
57a0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
57b0: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
57c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
57d0: 20 6c 69 73 74 65 6e 69 6e 67 20 54 43 50 20 73 listening TCP s
57e0: 6f 63 6b 65 74 20 61 6e 64 20 6c 6f 67 20 66 69 ocket and log fi
57f0: 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 les.\n");...fpri
5800: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 ntf(output, "\n"
5810: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
5820: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 20 28 put, " -v (
5830: 6f 72 20 2d 2d 76 65 72 73 69 6f 6e 29 20 69 6e or --version) in
5840: 73 74 72 75 63 74 73 20 66 69 6c 65 64 20 70 72 structs filed pr
5850: 69 6e 74 20 6f 75 74 20 74 68 65 20 76 65 72 73 int out the vers
5860: 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 65 ion number and e
5870: 78 69 74 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 xit.\n");...fpri
5880: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 ntf(output, "\n"
5890: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
58a0: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 62 20 28 put, " -b (
58b0: 6f 72 20 2d 2d 62 69 6e 64 29 20 73 70 65 63 69 or --bind) speci
58c0: 66 69 65 73 20 74 68 65 20 61 64 64 72 65 73 73 fies the address
58d0: 20 74 6f 20 6c 69 73 74 65 6e 20 66 6f 72 20 69 to listen for i
58e0: 6e 63 6f 6d 69 6e 67 20 48 54 54 50 5c 6e 22 29 ncoming HTTP\n")
58f0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
5900: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
5910: 20 20 20 20 20 20 20 20 20 20 72 65 71 75 65 73 reques
5920: 74 73 20 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 ts on. The defa
5930: 75 6c 74 20 76 61 6c 75 65 20 69 73 20 5c 22 25 ult value is \"%
5940: 73 5c 22 2e 5c 6e 22 2c 20 42 49 4e 44 5f 41 44 s\".\n", BIND_AD
5950: 44 52 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f DR);...fprintf(o
5960: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
5970: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5980: 22 20 20 20 20 20 20 2d 70 20 28 6f 72 20 2d 2d " -p (or --
5990: 70 6f 72 74 29 20 73 70 65 63 69 66 69 65 73 20 port) specifies
59a0: 74 68 65 20 54 43 50 20 70 6f 72 74 20 6e 75 6d the TCP port num
59b0: 62 65 72 20 74 6f 20 6c 69 73 74 65 6e 20 66 6f ber to listen fo
59c0: 72 20 69 6e 63 6f 6d 69 6e 67 20 48 54 54 50 5c r incoming HTTP\
59d0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
59e0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
59f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 71 req
5a00: 75 65 73 74 73 20 6f 6e 2e 20 20 54 68 65 20 64 uests on. The d
5a10: 65 66 61 75 6c 74 20 69 73 20 25 75 2e 5c 6e 22 efault is %u.\n"
5a20: 2c 20 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 , (unsigned int)
5a30: 20 50 4f 52 54 29 3b 0a 09 09 66 70 72 69 6e 74 PORT);...fprint
5a40: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
5a50: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
5a60: 74 2c 20 22 20 20 20 20 20 20 2d 74 20 28 6f 72 t, " -t (or
5a70: 20 2d 2d 74 68 72 65 61 64 73 29 20 73 70 65 63 --threads) spec
5a80: 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72 ifies the number
5a90: 20 6f 66 20 77 6f 72 6b 65 72 20 74 68 72 65 61 of worker threa
5aa0: 64 73 20 74 6f 20 63 72 65 61 74 65 2e 20 45 61 ds to create. Ea
5ab0: 63 68 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 ch\n");...fprint
5ac0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
5ad0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae0: 20 20 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64 worker thread
5af0: 20 63 61 6e 20 73 65 72 76 69 63 65 20 6f 6e 65 can service one
5b00: 20 63 6f 6e 63 75 72 72 65 6e 74 20 48 54 54 50 concurrent HTTP
5b10: 20 73 65 73 73 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 session.\n");..
5b20: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
5b30: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
5b40: 20 20 20 20 20 20 20 20 20 20 54 68 75 73 20 74 Thus t
5b50: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 he number of thr
5b60: 65 61 64 73 20 63 72 65 61 74 65 64 20 77 69 6c eads created wil
5b70: 6c 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 5c l determine how\
5b80: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
5b90: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
5ba0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bb0: 6d 61 6e 79 20 73 69 6d 75 6c 74 61 6e 65 6f 75 many simultaneou
5bc0: 73 20 74 72 61 6e 73 66 65 72 73 20 77 69 6c 6c s transfers will
5bd0: 20 62 65 20 70 6f 73 73 69 62 6c 65 2e 20 54 68 be possible. Th
5be0: 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 e\n");...fprintf
5bf0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
5c00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c10: 20 20 64 65 66 61 75 6c 74 20 69 73 20 25 6c 75 default is %lu
5c20: 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 .\n", (unsigned
5c30: 6c 6f 6e 67 29 20 54 48 52 45 41 44 5f 43 4f 55 long) THREAD_COU
5c40: 4e 54 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f NT);...fprintf(o
5c50: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
5c60: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5c70: 22 20 20 20 20 20 20 2d 63 20 28 6f 72 20 2d 2d " -c (or --
5c80: 63 61 63 68 65 29 20 73 70 65 63 69 66 69 65 73 cache) specifies
5c90: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 the number of f
5ca0: 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ile information
5cb0: 63 61 63 68 65 20 65 6e 74 72 69 65 73 5c 6e 22 cache entries\n"
5cc0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
5cd0: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
5ce0: 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 20 61 to a
5cf0: 6c 6c 6f 63 61 74 65 2e 20 20 45 61 63 68 20 63 llocate. Each c
5d00: 61 63 68 65 20 65 6e 74 72 79 20 68 6f 6c 64 73 ache entry holds
5d10: 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f file informatio
5d20: 6e 20 61 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 n as\n");...fpri
5d30: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
5d40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5d50: 20 20 20 77 65 6c 6c 20 61 73 20 61 6e 20 6f 70 well as an op
5d60: 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 en file descript
5d70: 6f 72 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 or to the file,
5d80: 73 6f 20 72 65 73 6f 75 72 63 65 5c 6e 22 29 3b so resource\n");
5d90: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
5da0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
5db0: 20 20 20 20 20 20 20 20 20 20 6c 69 6d 69 74 73 limits
5dc0: 20 28 69 2e 65 2e 2c 20 75 6c 69 6d 69 74 29 20 (i.e., ulimit)
5dd0: 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64 should be consid
5de0: 65 72 65 64 2e 20 20 54 68 69 73 20 73 68 6f 75 ered. This shou
5df0: 6c 64 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 ld\n");...fprint
5e00: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
5e10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e20: 20 62 65 20 61 20 70 72 69 6d 65 20 6e 75 6d 62 be a prime numb
5e30: 65 72 20 66 6f 72 20 69 64 65 61 6c 20 75 73 65 er for ideal use
5e40: 20 77 69 74 68 20 74 68 65 20 6c 6f 6f 6b 75 70 with the lookup
5e50: 20 6d 65 74 68 6f 64 2e 5c 6e 22 29 3b 0a 09 09 method.\n");...
5e60: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5e70: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
5e80: 20 20 20 20 20 20 20 54 68 65 20 64 65 66 61 75 The defau
5e90: 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 lt is %lu.\n", (
5ea0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 43 unsigned long) C
5eb0: 41 43 48 45 5f 53 49 5a 45 29 3b 0a 09 09 66 70 ACHE_SIZE);...fp
5ec0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
5ed0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
5ee0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 6c utput, " -l
5ef0: 20 28 6f 72 20 2d 2d 6c 6f 67 29 20 73 70 65 63 (or --log) spec
5f00: 69 66 69 65 73 20 61 20 66 69 6c 65 6e 61 6d 65 ifies a filename
5f10: 20 74 6f 20 6f 70 65 6e 20 66 6f 72 20 77 72 69 to open for wri
5f20: 74 69 6e 67 20 6c 6f 67 20 65 6e 74 72 69 65 73 ting log entries
5f30: 2e 20 20 4c 6f 67 5c 6e 22 29 3b 0a 09 09 66 70 . Log\n");...fp
5f40: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
5f50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5f60: 20 20 20 65 6e 74 72 69 65 73 20 61 72 65 20 6d entries are m
5f70: 61 64 65 20 66 6f 72 20 76 61 72 69 6f 75 73 20 ade for various
5f80: 73 74 61 67 65 73 20 69 6e 20 74 72 61 6e 73 66 stages in transf
5f90: 65 72 69 6e 67 20 66 69 6c 65 73 2e 5c 6e 22 29 ering files.\n")
5fa0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
5fb0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
5fc0: 20 20 20 20 20 20 20 20 20 54 68 65 20 6c 6f 67 The log
5fd0: 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 file is opened
5fe0: 62 65 66 6f 72 65 20 73 77 69 74 63 68 69 6e 67 before switching
5ff0: 20 75 73 65 72 73 20 28 73 65 65 20 5c 22 2d 75 users (see \"-u
6000: 5c 22 29 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e \")\n");...fprin
6010: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
6020: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6030: 61 6e 64 20 72 6f 6f 74 20 64 69 72 65 63 74 6f and root directo
6040: 72 69 65 73 20 28 73 65 65 20 5c 22 2d 72 5c 22 ries (see \"-r\"
6050: 29 2e 20 20 54 68 65 20 6c 6f 67 20 66 69 6c 65 ). The log file
6060: 20 69 73 20 6e 65 76 65 72 5c 6e 22 29 3b 0a 09 is never\n");..
6070: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6080: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
6090: 20 20 20 20 20 20 63 6c 6f 73 65 64 20 73 6f 20 closed so
60a0: 6c 6f 67 20 72 6f 74 61 74 69 6f 6e 20 77 69 74 log rotation wit
60b0: 68 6f 75 74 20 73 74 6f 70 70 69 6e 67 20 74 68 hout stopping th
60c0: 65 20 64 61 65 6d 6f 6e 20 69 73 20 77 69 6c 6c e daemon is will
60d0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
60e0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
60f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f 74 not
6100: 20 77 6f 72 6b 2e 20 20 54 68 65 20 76 61 6c 75 work. The valu
6110: 65 20 6f 66 20 5c 22 2d 5c 22 20 69 6e 64 69 63 e of \"-\" indic
6120: 61 74 65 73 20 74 68 61 74 20 73 74 61 6e 64 61 ates that standa
6130: 72 64 20 6f 75 74 70 75 74 5c 6e 22 29 3b 0a 09 rd output\n");..
6140: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6150: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
6160: 20 20 20 20 20 20 73 68 6f 75 6c 64 20 62 65 20 should be
6170: 75 73 65 64 20 66 6f 72 20 6c 6f 67 67 69 6e 67 used for logging
6180: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 . The default i
6190: 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 4c 4f s \"%s\".\n", LO
61a0: 47 5f 46 49 4c 45 29 3b 0a 09 09 66 70 72 69 6e G_FILE);...fprin
61b0: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 tf(output, "\n")
61c0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
61d0: 75 74 2c 20 22 20 20 20 20 20 20 2d 75 20 28 6f ut, " -u (o
61e0: 72 20 2d 2d 75 73 65 72 29 20 73 70 65 63 69 66 r --user) specif
61f0: 69 65 73 20 74 68 65 20 75 73 65 72 20 74 6f 20 ies the user to
6200: 73 77 69 74 63 68 20 75 73 65 72 20 49 44 73 20 switch user IDs
6210: 74 6f 20 62 65 66 6f 72 65 20 73 65 72 76 69 63 to before servic
6220: 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ing\n");...fprin
6230: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
6240: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6250: 20 72 65 71 75 65 73 74 73 2e 20 20 54 68 65 20 requests. The
6260: 64 65 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 63 default is not c
6270: 68 61 6e 67 65 20 75 73 65 72 20 49 44 73 2e 5c hange user IDs.\
6280: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
6290: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
62a0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
62b0: 22 20 20 20 20 20 20 2d 72 20 28 6f 72 20 2d 2d " -r (or --
62c0: 72 6f 6f 74 29 20 73 70 65 63 69 66 69 65 73 20 root) specifies
62d0: 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f the directory to
62e0: 20 61 63 74 20 61 73 20 74 68 65 20 72 6f 6f 74 act as the root
62f0: 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 5c 6e directory for\n
6300: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
6310: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
6320: 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 the
6330: 66 69 6c 65 20 73 65 72 76 65 72 2e 20 20 49 66 file server. If
6340: 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 69 73 20 this option is
6350: 73 70 65 63 69 66 69 65 64 2c 20 63 68 72 6f 6f specified, chroo
6360: 74 28 32 29 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t(2)\n");...fpri
6370: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
6380: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6390: 20 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 is called. Th
63a0: 65 20 64 65 66 61 75 6c 74 20 69 73 20 6e 6f 74 e default is not
63b0: 20 63 68 61 6e 67 65 20 72 6f 6f 74 20 64 69 72 change root dir
63c0: 65 63 74 6f 72 69 65 73 2c 5c 6e 22 29 3b 0a 09 ectories,\n");..
63d0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
63e0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
63f0: 20 20 20 20 20 20 20 74 68 61 74 20 69 73 2c 20 that is,
6400: 74 68 65 20 5c 22 2f 5c 22 20 64 69 72 65 63 74 the \"/\" direct
6410: 6f 72 79 20 69 73 20 73 68 61 72 65 64 20 6f 75 ory is shared ou
6420: 74 2e 20 20 54 68 69 73 20 77 69 6c 6c 5c 6e 22 t. This will\n"
6430: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
6440: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
6450: 20 20 20 20 20 20 20 20 20 20 20 6c 69 6b 65 6c likel
6460: 79 20 62 65 20 61 20 73 65 63 75 72 69 74 79 20 y be a security
6470: 69 73 73 75 65 2c 20 73 6f 20 74 68 69 73 20 6f issue, so this o
6480: 70 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 ption should alw
6490: 61 79 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ays\n");...fprin
64a0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
64b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64c0: 20 62 65 20 75 73 65 64 2e 5c 6e 22 29 3b 0a 09 be used.\n");..
64d0: 7d 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f }...return;.}../
64e0: 2a 20 41 64 64 20 61 20 67 65 74 6f 70 74 20 6f * Add a getopt o
64f0: 70 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 ption */.static
6500: 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 74 6f 70 void filed_getop
6510: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 73 74 t_long_setopt(st
6520: 72 75 63 74 20 6f 70 74 69 6f 6e 20 2a 6f 70 74 ruct option *opt
6530: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6e 61 , const char *na
6540: 6d 65 2c 20 69 6e 74 20 68 61 73 5f 61 72 67 2c me, int has_arg,
6550: 20 69 6e 74 20 76 61 6c 29 20 7b 0a 09 6f 70 74 int val) {..opt
6560: 2d 3e 6e 61 6d 65 20 20 20 20 20 3d 20 6e 61 6d ->name = nam
6570: 65 3b 0a 09 6f 70 74 2d 3e 68 61 73 5f 61 72 67 e;..opt->has_arg
6580: 20 20 3d 20 68 61 73 5f 61 72 67 3b 0a 09 6f 70 = has_arg;..op
6590: 74 2d 3e 66 6c 61 67 20 20 20 20 20 3d 20 4e 55 t->flag = NU
65a0: 4c 4c 3b 0a 09 6f 70 74 2d 3e 76 61 6c 20 20 20 LL;..opt->val
65b0: 20 20 20 3d 20 76 61 6c 3b 0a 0a 09 72 65 74 75 = val;...retu
65c0: 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 73 6f 6c 76 rn;.}../* Resolv
65d0: 65 20 61 20 75 73 65 72 6e 61 6d 65 20 74 6f 20 e a username to
65e0: 61 20 55 49 44 20 2a 2f 0a 73 74 61 74 69 63 20 a UID */.static
65f0: 69 6e 74 20 66 69 6c 65 64 5f 75 73 65 72 5f 6c int filed_user_l
6600: 6f 6f 6b 75 70 28 63 6f 6e 73 74 20 63 68 61 72 ookup(const char
6610: 20 2a 75 73 65 72 2c 20 75 69 64 5f 74 20 2a 75 *user, uid_t *u
6620: 73 65 72 5f 69 64 29 20 7b 0a 09 63 68 61 72 20 ser_id) {..char
6630: 2a 6e 65 78 74 3b 0a 09 75 69 64 5f 74 20 75 73 *next;..uid_t us
6640: 65 72 5f 69 64 5f 63 68 65 63 6b 3b 0a 23 69 66 er_id_check;.#if
6650: 6e 64 65 66 20 46 49 4c 45 44 5f 4e 4f 5f 47 45 ndef FILED_NO_GE
6660: 54 50 57 4e 41 4d 0a 09 73 74 72 75 63 74 20 70 TPWNAM..struct p
6670: 61 73 73 77 64 20 2a 65 6e 74 3b 0a 0a 09 65 6e asswd *ent;...en
6680: 74 20 3d 20 67 65 74 70 77 6e 61 6d 28 75 73 65 t = getpwnam(use
6690: 72 29 3b 0a 09 69 66 20 28 65 6e 74 20 21 3d 20 r);..if (ent !=
66a0: 4e 55 4c 4c 29 20 7b 0a 09 09 2a 75 73 65 72 5f NULL) {...*user_
66b0: 69 64 20 3d 20 65 6e 74 2d 3e 70 77 5f 75 69 64 id = ent->pw_uid
66c0: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a ;....return(0);.
66d0: 09 7d 0a 23 65 6e 64 69 66 0a 0a 09 75 73 65 72 .}.#endif...user
66e0: 5f 69 64 5f 63 68 65 63 6b 20 3d 20 73 74 72 74 _id_check = strt
66f0: 6f 75 6c 6c 28 75 73 65 72 2c 20 26 6e 65 78 74 oull(user, &next
6700: 2c 20 31 30 29 3b 0a 09 69 66 20 28 6e 65 78 74 , 10);..if (next
6710: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 == NULL) {...re
6720: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 turn(1);..}...if
6730: 20 28 6e 65 78 74 5b 30 5d 20 21 3d 20 27 5c 30 (next[0] != '\0
6740: 27 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 ') {...return(1)
6750: 3b 0a 09 7d 0a 0a 09 2a 75 73 65 72 5f 69 64 20 ;..}...*user_id
6760: 3d 20 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 3b = user_id_check;
6770: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a ...return(0);.}.
6780: 0a 2f 2a 20 44 61 65 6d 6f 6e 69 7a 65 20 2a 2f ./* Daemonize */
6790: 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 .static int file
67a0: 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 76 6f 69 64 d_daemonize(void
67b0: 29 20 7b 0a 09 70 69 64 5f 74 20 73 65 74 73 69 ) {..pid_t setsi
67c0: 64 5f 72 65 74 2c 20 66 6f 72 6b 5f 72 65 74 3b d_ret, fork_ret;
67d0: 0a 09 69 6e 74 20 63 68 64 69 72 5f 72 65 74 2c ..int chdir_ret,
67e0: 20 64 75 70 32 5f 72 65 74 3b 0a 09 69 6e 74 20 dup2_ret;..int
67f0: 66 64 5f 69 6e 2c 20 66 64 5f 6f 75 74 3b 0a 0a fd_in, fd_out;..
6800: 09 63 68 64 69 72 5f 72 65 74 20 3d 20 63 68 64 .chdir_ret = chd
6810: 69 72 28 22 2f 22 29 3b 0a 09 69 66 20 28 63 68 ir("/");..if (ch
6820: 64 69 72 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a dir_ret != 0) {.
6830: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
6840: 0a 09 66 6f 72 6b 5f 72 65 74 20 3d 20 66 6f 72 ..fork_ret = for
6850: 6b 28 29 3b 0a 09 69 66 20 28 66 6f 72 6b 5f 72 k();..if (fork_r
6860: 65 74 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 et < 0) {...retu
6870: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 rn(1);..}...if (
6880: 66 6f 72 6b 5f 72 65 74 20 3e 20 30 29 20 7b 0a fork_ret > 0) {.
6890: 09 09 2f 2a 20 50 61 72 65 6e 74 20 2a 2f 0a 09 ../* Parent */..
68a0: 09 77 61 69 74 70 69 64 28 66 6f 72 6b 5f 72 65 .waitpid(fork_re
68b0: 74 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 0a 09 09 t, NULL, 0);....
68c0: 65 78 69 74 28 45 58 49 54 5f 53 55 43 43 45 53 exit(EXIT_SUCCES
68d0: 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 68 69 6c S);..}.../* Chil
68e0: 64 20 2a 2f 0a 09 69 66 20 28 66 6f 72 6b 28 29 d */..if (fork()
68f0: 20 21 3d 20 30 29 20 7b 0a 09 09 2f 2a 20 43 68 != 0) {.../* Ch
6900: 69 6c 64 20 2a 2f 0a 09 09 65 78 69 74 28 45 58 ild */...exit(EX
6910: 49 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a IT_SUCCESS);..}.
6920: 0a 09 2f 2a 20 47 72 61 6e 64 20 63 68 69 6c 64 ../* Grand child
6930: 20 2a 2f 0a 09 73 65 74 73 69 64 5f 72 65 74 20 */..setsid_ret
6940: 3d 20 73 65 74 73 69 64 28 29 3b 0a 09 69 66 20 = setsid();..if
6950: 28 73 65 74 73 69 64 5f 72 65 74 20 3d 3d 20 28 (setsid_ret == (
6960: 28 70 69 64 5f 74 29 20 2d 31 29 29 20 7b 0a 09 (pid_t) -1)) {..
6970: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a .return(1);..}..
6980: 09 66 64 5f 69 6e 20 3d 20 6f 70 65 6e 28 22 2f .fd_in = open("/
6990: 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 52 44 4f dev/null", O_RDO
69a0: 4e 4c 59 29 3b 0a 09 66 64 5f 6f 75 74 20 3d 20 NLY);..fd_out =
69b0: 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 open("/dev/null"
69c0: 2c 20 4f 5f 57 52 4f 4e 4c 59 29 3b 0a 09 69 66 , O_WRONLY);..if
69d0: 20 28 66 64 5f 69 6e 20 3c 20 30 20 7c 7c 20 66 (fd_in < 0 || f
69e0: 64 5f 6f 75 74 20 3c 20 30 29 20 7b 0a 09 09 72 d_out < 0) {...r
69f0: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 eturn(1);..}...d
6a00: 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 up2_ret = dup2(f
6a10: 64 5f 69 6e 2c 20 53 54 44 49 4e 5f 46 49 4c 45 d_in, STDIN_FILE
6a20: 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 NO);..if (dup2_r
6a30: 65 74 20 21 3d 20 53 54 44 49 4e 5f 46 49 4c 45 et != STDIN_FILE
6a40: 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 NO) {...return(1
6a50: 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 );..}...dup2_ret
6a60: 20 3d 20 64 75 70 32 28 66 64 5f 6f 75 74 2c 20 = dup2(fd_out,
6a70: 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 3b 0a STDOUT_FILENO);.
6a80: 09 69 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d .if (dup2_ret !=
6a90: 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 20 STDOUT_FILENO)
6aa0: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 {...return(1);..
6ab0: 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 64 }...dup2_ret = d
6ac0: 75 70 32 28 66 64 5f 6f 75 74 2c 20 53 54 44 45 up2(fd_out, STDE
6ad0: 52 52 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 RR_FILENO);..if
6ae0: 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 (dup2_ret != STD
6af0: 45 52 52 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 ERR_FILENO) {...
6b00: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 return(1);..}...
6b10: 63 6c 6f 73 65 28 66 64 5f 69 6e 29 3b 0a 09 63 close(fd_in);..c
6b20: 6c 6f 73 65 28 66 64 5f 6f 75 74 29 3b 0a 0a 09 lose(fd_out);...
6b30: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a return(0);.}../*
6b40: 20 52 75 6e 20 70 72 6f 63 65 73 73 20 2a 2f 0a Run process */.
6b50: 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20 61 72 67 int main(int arg
6b60: 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 29 20 c, char **argv)
6b70: 7b 0a 09 73 74 72 75 63 74 20 6f 70 74 69 6f 6e {..struct option
6b80: 20 6f 70 74 69 6f 6e 73 5b 31 31 5d 3b 0a 09 63 options[11];..c
6b90: 6f 6e 73 74 20 63 68 61 72 20 2a 62 69 6e 64 5f onst char *bind_
6ba0: 61 64 64 72 20 3d 20 42 49 4e 44 5f 41 44 44 52 addr = BIND_ADDR
6bb0: 2c 20 2a 6e 65 77 72 6f 6f 74 20 3d 20 4e 55 4c , *newroot = NUL
6bc0: 4c 2c 20 2a 6c 6f 67 5f 66 69 6c 65 20 3d 20 4c L, *log_file = L
6bd0: 4f 47 5f 46 49 4c 45 3b 0a 09 46 49 4c 45 20 2a OG_FILE;..FILE *
6be0: 6c 6f 67 5f 66 70 3b 0a 09 75 69 64 5f 74 20 75 log_fp;..uid_t u
6bf0: 73 65 72 20 3d 20 30 3b 0a 09 69 6e 74 20 70 6f ser = 0;..int po
6c00: 72 74 20 3d 20 50 4f 52 54 2c 20 74 68 72 65 61 rt = PORT, threa
6c10: 64 5f 63 6f 75 6e 74 20 3d 20 54 48 52 45 41 44 d_count = THREAD
6c20: 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 61 63 _COUNT;..int cac
6c30: 68 65 5f 73 69 7a 65 20 3d 20 43 41 43 48 45 5f he_size = CACHE_
6c40: 53 49 5a 45 3b 0a 09 69 6e 74 20 69 6e 69 74 5f SIZE;..int init_
6c50: 72 65 74 2c 20 63 68 72 6f 6f 74 5f 72 65 74 2c ret, chroot_ret,
6c60: 20 73 65 74 75 69 64 5f 72 65 74 2c 20 6c 6f 6f setuid_ret, loo
6c70: 6b 75 70 5f 72 65 74 2c 20 63 68 64 69 72 5f 72 kup_ret, chdir_r
6c80: 65 74 3b 0a 09 69 6e 74 20 73 65 74 75 69 64 5f et;..int setuid_
6c90: 65 6e 61 62 6c 65 64 20 3d 20 30 2c 20 64 61 65 enabled = 0, dae
6ca0: 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 30 3b mon_enabled = 0;
6cb0: 0a 09 69 6e 74 20 63 68 3b 0a 09 69 6e 74 20 66 ..int ch;..int f
6cc0: 64 3b 0a 0a 09 2f 2a 20 50 72 6f 63 65 73 73 20 d;.../* Process
6cd0: 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 66 69 arguments */..fi
6ce0: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f led_getopt_long_
6cf0: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b setopt(&options[
6d00: 30 5d 2c 20 22 70 6f 72 74 22 2c 20 72 65 71 75 0], "port", requ
6d10: 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 ired_argument, '
6d20: 70 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f p');..filed_geto
6d30: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
6d40: 6f 70 74 69 6f 6e 73 5b 31 5d 2c 20 22 74 68 72 options[1], "thr
6d50: 65 61 64 73 22 2c 20 72 65 71 75 69 72 65 64 5f eads", required_
6d60: 61 72 67 75 6d 65 6e 74 2c 20 27 74 27 29 3b 0a argument, 't');.
6d70: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
6d80: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
6d90: 6e 73 5b 32 5d 2c 20 22 63 61 63 68 65 22 2c 20 ns[2], "cache",
6da0: 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e required_argumen
6db0: 74 2c 20 27 63 27 29 3b 0a 09 66 69 6c 65 64 5f t, 'c');..filed_
6dc0: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f getopt_long_seto
6dd0: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 33 5d 2c 20 pt(&options[3],
6de0: 22 62 69 6e 64 22 2c 20 72 65 71 75 69 72 65 64 "bind", required
6df0: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 62 27 29 3b _argument, 'b');
6e00: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c ..filed_getopt_l
6e10: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 ong_setopt(&opti
6e20: 6f 6e 73 5b 34 5d 2c 20 22 75 73 65 72 22 2c 20 ons[4], "user",
6e30: 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e required_argumen
6e40: 74 2c 20 27 75 27 29 3b 0a 09 66 69 6c 65 64 5f t, 'u');..filed_
6e50: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f getopt_long_seto
6e60: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 35 5d 2c 20 pt(&options[5],
6e70: 22 72 6f 6f 74 22 2c 20 72 65 71 75 69 72 65 64 "root", required
6e80: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 72 27 29 3b _argument, 'r');
6e90: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c ..filed_getopt_l
6ea0: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 ong_setopt(&opti
6eb0: 6f 6e 73 5b 36 5d 2c 20 22 68 65 6c 70 22 2c 20 ons[6], "help",
6ec0: 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 68 27 no_argument, 'h'
6ed0: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 );..filed_getopt
6ee0: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 _long_setopt(&op
6ef0: 74 69 6f 6e 73 5b 37 5d 2c 20 22 64 61 65 6d 6f tions[7], "daemo
6f00: 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c n", no_argument,
6f10: 20 27 64 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 'd');..filed_ge
6f20: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 topt_long_setopt
6f30: 28 26 6f 70 74 69 6f 6e 73 5b 38 5d 2c 20 22 6c (&options[8], "l
6f40: 6f 67 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 og", required_ar
6f50: 67 75 6d 65 6e 74 2c 20 27 6c 27 29 3b 0a 09 66 gument, 'l');..f
6f60: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
6f70: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
6f80: 5b 39 5d 2c 20 22 76 65 72 73 69 6f 6e 22 2c 20 [9], "version",
6f90: 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 76 27 no_argument, 'v'
6fa0: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 );..filed_getopt
6fb0: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 _long_setopt(&op
6fc0: 74 69 6f 6e 73 5b 31 30 5d 2c 20 4e 55 4c 4c 2c tions[10], NULL,
6fd0: 20 30 2c 20 30 29 3b 0a 09 77 68 69 6c 65 20 28 0, 0);..while (
6fe0: 28 63 68 20 3d 20 67 65 74 6f 70 74 5f 6c 6f 6e (ch = getopt_lon
6ff0: 67 28 61 72 67 63 2c 20 61 72 67 76 2c 20 22 70 g(argc, argv, "p
7000: 3a 74 3a 63 3a 62 3a 75 3a 72 3a 6c 3a 68 64 76 :t:c:b:u:r:l:hdv
7010: 22 2c 20 6f 70 74 69 6f 6e 73 2c 20 4e 55 4c 4c ", options, NULL
7020: 29 29 20 21 3d 20 2d 31 29 20 7b 0a 09 09 73 77 )) != -1) {...sw
7030: 69 74 63 68 28 63 68 29 20 7b 0a 09 09 09 63 61 itch(ch) {....ca
7040: 73 65 20 27 70 27 3a 0a 09 09 09 09 70 6f 72 74 se 'p':.....port
7050: 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 3b = atoi(optarg);
7060: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 .....break;....c
7070: 61 73 65 20 27 74 27 3a 0a 09 09 09 09 74 68 72 ase 't':.....thr
7080: 65 61 64 5f 63 6f 75 6e 74 20 3d 20 61 74 6f 69 ead_count = atoi
7090: 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 (optarg);.....br
70a0: 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 63 27 eak;....case 'c'
70b0: 3a 0a 09 09 09 09 63 61 63 68 65 5f 73 69 7a 65 :.....cache_size
70c0: 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 3b = atoi(optarg);
70d0: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 .....break;....c
70e0: 61 73 65 20 27 62 27 3a 0a 09 09 09 09 62 69 6e ase 'b':.....bin
70f0: 64 5f 61 64 64 72 20 3d 20 73 74 72 64 75 70 28 d_addr = strdup(
7100: 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 optarg);.....bre
7110: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 75 27 3a ak;....case 'u':
7120: 0a 09 09 09 09 73 65 74 75 69 64 5f 65 6e 61 62 .....setuid_enab
7130: 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 6c 6f 6f led = 1;.....loo
7140: 6b 75 70 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f kup_ret = filed_
7150: 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 6f 70 74 61 user_lookup(opta
7160: 72 67 2c 20 26 75 73 65 72 29 3b 0a 09 09 09 09 rg, &user);.....
7170: 69 66 20 28 6c 6f 6f 6b 75 70 5f 72 65 74 20 21 if (lookup_ret !
7180: 3d 20 30 29 20 7b 0a 09 09 09 09 09 66 69 6c 65 = 0) {......file
7190: 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73 74 64 d_print_help(std
71a0: 65 72 72 2c 20 30 2c 20 22 49 6e 76 61 6c 69 64 err, 0, "Invalid
71b0: 20 75 73 65 72 6e 61 6d 65 20 73 70 65 63 69 66 username specif
71c0: 69 65 64 22 29 3b 0a 0a 09 09 09 09 09 72 65 74 ied");.......ret
71d0: 75 72 6e 28 31 29 3b 0a 09 09 09 09 7d 0a 09 09 urn(1);.....}...
71e0: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
71f0: 20 27 72 27 3a 0a 09 09 09 09 6e 65 77 72 6f 6f 'r':.....newroo
7200: 74 20 3d 20 73 74 72 64 75 70 28 6f 70 74 61 72 t = strdup(optar
7210: 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 g);.....break;..
7220: 09 09 63 61 73 65 20 27 6c 27 3a 0a 09 09 09 09 ..case 'l':.....
7230: 6c 6f 67 5f 66 69 6c 65 20 3d 20 73 74 72 64 75 log_file = strdu
7240: 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 p(optarg);.....b
7250: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 64 reak;....case 'd
7260: 27 3a 0a 09 09 09 09 64 61 65 6d 6f 6e 5f 65 6e ':.....daemon_en
7270: 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 62 abled = 1;.....b
7280: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 76 reak;....case 'v
7290: 27 3a 0a 09 09 09 09 70 72 69 6e 74 66 28 22 66 ':.....printf("f
72a0: 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 25 73 5c iled version %s\
72b0: 6e 22 2c 20 46 49 4c 45 44 5f 56 45 52 53 49 4f n", FILED_VERSIO
72c0: 4e 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 N);......return(
72d0: 30 29 3b 0a 09 09 09 63 61 73 65 20 27 3f 27 3a 0);....case '?':
72e0: 0a 09 09 09 63 61 73 65 20 27 3a 27 3a 0a 09 09 ....case ':':...
72f0: 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 ..filed_print_he
7300: 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20 4e 55 lp(stderr, 0, NU
7310: 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e LL);......return
7320: 28 31 29 3b 0a 09 09 09 63 61 73 65 20 27 68 27 (1);....case 'h'
7330: 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e :.....filed_prin
7340: 74 5f 68 65 6c 70 28 73 74 64 6f 75 74 2c 20 31 t_help(stdout, 1
7350: 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 , NULL);......re
7360: 74 75 72 6e 28 30 29 3b 0a 09 09 7d 0a 09 7d 0a turn(0);...}..}.
7370: 0a 09 2f 2a 20 4f 70 65 6e 20 6c 6f 67 20 66 69 ../* Open log fi
7380: 6c 65 20 2a 2f 0a 09 69 66 20 28 73 74 72 63 6d le */..if (strcm
7390: 70 28 6c 6f 67 5f 66 69 6c 65 2c 20 22 2d 22 29 p(log_file, "-")
73a0: 20 3d 3d 20 30 29 20 7b 0a 09 09 6c 6f 67 5f 66 == 0) {...log_f
73b0: 70 20 3d 20 73 74 64 6f 75 74 3b 0a 09 7d 20 65 p = stdout;..} e
73c0: 6c 73 65 20 7b 0a 09 09 6c 6f 67 5f 66 70 20 3d lse {...log_fp =
73d0: 20 66 6f 70 65 6e 28 6c 6f 67 5f 66 69 6c 65 2c fopen(log_file,
73e0: 20 22 61 2b 22 29 3b 0a 09 09 69 66 20 28 6c 6f "a+");...if (lo
73f0: 67 5f 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a g_fp == NULL) {.
7400: 09 09 09 70 65 72 72 6f 72 28 22 66 6f 70 65 6e ...perror("fopen
7410: 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 34 ");.....return(4
7420: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 43 );...}..}.../* C
7430: 72 65 61 74 65 20 6c 69 73 74 65 6e 69 6e 67 20 reate listening
7440: 73 6f 63 6b 65 74 20 2a 2f 0a 09 66 64 20 3d 20 socket */..fd =
7450: 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 62 69 6e filed_listen(bin
7460: 64 5f 61 64 64 72 2c 20 70 6f 72 74 29 3b 0a 09 d_addr, port);..
7470: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 if (fd < 0) {...
7480: 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 69 perror("filed_li
7490: 73 74 65 6e 22 29 3b 0a 0a 09 09 72 65 74 75 72 sten");....retur
74a0: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 42 65 n(1);..}.../* Be
74b0: 63 6f 6d 65 20 61 20 64 61 65 6d 6f 6e 20 2a 2f come a daemon */
74c0: 0a 09 69 66 20 28 64 61 65 6d 6f 6e 5f 65 6e 61 ..if (daemon_ena
74d0: 62 6c 65 64 29 20 7b 0a 09 09 69 6e 69 74 5f 72 bled) {...init_r
74e0: 65 74 20 3d 20 66 69 6c 65 64 5f 64 61 65 6d 6f et = filed_daemo
74f0: 6e 69 7a 65 28 29 3b 0a 09 09 69 66 20 28 69 6e nize();...if (in
7500: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 it_ret != 0) {..
7510: 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f ..perror("filed_
7520: 64 61 65 6d 6f 6e 69 7a 65 22 29 3b 0a 0a 09 09 daemonize");....
7530: 09 72 65 74 75 72 6e 28 36 29 3b 0a 09 09 7d 0a .return(6);...}.
7540: 09 7d 0a 0a 09 2f 2a 20 43 68 72 6f 6f 74 2c 20 .}.../* Chroot,
7550: 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a if appropriate *
7560: 2f 0a 09 69 66 20 28 6e 65 77 72 6f 6f 74 29 20 /..if (newroot)
7570: 7b 0a 09 09 63 68 64 69 72 5f 72 65 74 20 3d 20 {...chdir_ret =
7580: 63 68 64 69 72 28 6e 65 77 72 6f 6f 74 29 3b 0a chdir(newroot);.
7590: 09 09 69 66 20 28 63 68 64 69 72 5f 72 65 74 20 ..if (chdir_ret
75a0: 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f != 0) {....perro
75b0: 72 28 22 63 68 64 69 72 22 29 3b 0a 0a 09 09 09 r("chdir");.....
75c0: 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a return(1);...}..
75d0: 09 09 63 68 72 6f 6f 74 5f 72 65 74 20 3d 20 63 ..chroot_ret = c
75e0: 68 72 6f 6f 74 28 22 2e 22 29 3b 0a 09 09 69 66 hroot(".");...if
75f0: 20 28 63 68 72 6f 6f 74 5f 72 65 74 20 21 3d 20 (chroot_ret !=
7600: 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 0) {....perror("
7610: 63 68 72 6f 6f 74 22 29 3b 0a 0a 09 09 09 72 65 chroot");.....re
7620: 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a turn(1);...}..}.
7630: 0a 09 2f 2a 20 44 72 6f 70 20 70 72 69 76 69 6c ../* Drop privil
7640: 65 67 65 73 2c 20 69 66 20 61 70 70 72 6f 70 72 eges, if appropr
7650: 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 73 65 74 iate */..if (set
7660: 75 69 64 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 uid_enabled) {..
7670: 09 73 65 74 75 69 64 5f 72 65 74 20 3d 20 73 65 .setuid_ret = se
7680: 74 75 69 64 28 75 73 65 72 29 3b 0a 09 09 69 66 tuid(user);...if
7690: 20 28 73 65 74 75 69 64 5f 72 65 74 20 21 3d 20 (setuid_ret !=
76a0: 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 0) {....perror("
76b0: 73 65 74 75 69 64 22 29 3b 0a 0a 09 09 09 72 65 setuid");.....re
76c0: 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a turn(1);...}..}.
76d0: 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ../* Initialize
76e0: 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 */..init_ret = f
76f0: 69 6c 65 64 5f 69 6e 69 74 28 63 61 63 68 65 5f iled_init(cache_
7700: 73 69 7a 65 29 3b 0a 09 69 66 20 28 69 6e 69 74 size);..if (init
7710: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 _ret != 0) {...p
7720: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 69 6e 69 error("filed_ini
7730: 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 33 t");....return(3
7740: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 );..}.../* Creat
7750: 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64 e logging thread
7760: 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 */..init_ret =
7770: 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 filed_logging_th
7780: 72 65 61 64 5f 69 6e 69 74 28 6c 6f 67 5f 66 70 read_init(log_fp
7790: 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 );..if (init_ret
77a0: 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f != 0) {...perro
77b0: 72 28 22 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 r("filed_logging
77c0: 5f 74 68 72 65 61 64 5f 69 6e 69 74 22 29 3b 0a _thread_init");.
77d0: 0a 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d ...return(4);..}
77e0: 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 .../* Create wor
77f0: 6b 65 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 09 ker threads */..
7800: 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 init_ret = filed
7810: 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f _worker_threads_
7820: 69 6e 69 74 28 66 64 2c 20 74 68 72 65 61 64 5f init(fd, thread_
7830: 63 6f 75 6e 74 29 3b 0a 09 69 66 20 28 69 6e 69 count);..if (ini
7840: 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 t_ret != 0) {...
7850: 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 77 6f perror("filed_wo
7860: 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 rker_threads_ini
7870: 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 35 t");....return(5
7880: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 61 69 74 20 );..}.../* Wait
7890: 66 6f 72 20 74 68 72 65 61 64 73 20 74 6f 20 65 for threads to e
78a0: 78 69 74 20 2a 2f 0a 09 2f 2a 20 58 58 58 3a 54 xit */../* XXX:T
78b0: 4f 44 4f 3a 20 4d 6f 6e 69 74 6f 72 20 74 68 72 ODO: Monitor thr
78c0: 65 61 64 20 75 73 61 67 65 20 2a 2f 0a 09 77 68 ead usage */..wh
78d0: 69 6c 65 20 28 31 29 20 7b 0a 09 09 73 6c 65 65 ile (1) {...slee
78e0: 70 28 36 30 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 p(60);..}.../* R
78f0: 65 74 75 72 6e 20 69 6e 20 66 61 69 6c 75 72 65 eturn in failure
7900: 20 2a 2f 0a 09 72 65 74 75 72 6e 28 32 29 3b 0a */..return(2);.
7910: 7d 0a }.