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 70 61 74 68 5b 46 49 4c 45 44 5f 50 41 54 r path[FILED_PAT
0400: 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b 0a H_BUFFER_SIZE];.
0410: 09 69 6e 74 20 66 64 3b 0a 09 6f 66 66 5f 74 20 .int fd;..off_t
0420: 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 6c 61 73 74 len;..char *last
0430: 6d 6f 64 3b 0a 09 63 68 61 72 20 6c 61 73 74 6d mod;..char lastm
0440: 6f 64 5f 62 5b 36 34 5d 3b 0a 09 63 6f 6e 73 74 od_b[64];..const
0450: 20 63 68 61 72 20 2a 74 79 70 65 3b 0a 7d 3b 0a char *type;.};.
0460: 0a 2f 2a 20 52 65 71 75 65 73 74 20 76 61 72 69 ./* Request vari
0470: 61 62 6c 65 73 20 2a 2f 0a 73 74 72 75 63 74 20 ables */.struct
0480: 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 filed_http_reque
0490: 73 74 20 7b 0a 09 2f 2a 2a 20 42 75 66 66 65 72 st {../** Buffer
04a0: 73 20 2a 2a 2f 0a 09 73 74 72 75 63 74 20 66 69 s **/..struct fi
04b0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 66 69 6c led_fileinfo fil
04c0: 65 69 6e 66 6f 3b 0a 09 63 68 61 72 20 74 6d 70 einfo;..char tmp
04d0: 62 75 66 5b 31 30 31 30 5d 3b 0a 0a 09 2f 2a 2a buf[1010];.../**
04e0: 20 48 54 54 50 20 52 65 71 75 65 73 74 20 69 6e HTTP Request in
04f0: 66 6f 72 6d 61 74 69 6f 6e 20 2a 2a 2f 0a 09 2f formation **/../
0500: 2a 2a 2a 20 50 61 74 68 20 62 65 69 6e 67 20 72 *** Path being r
0510: 65 71 75 65 73 74 65 64 20 2a 2a 2a 2f 0a 09 63 equested ***/..c
0520: 68 61 72 20 70 61 74 68 5b 46 49 4c 45 44 5f 50 har path[FILED_P
0530: 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d ATH_BUFFER_SIZE]
0540: 3b 20 0a 0a 09 73 74 72 75 63 74 20 7b 0a 09 09 ; ...struct {...
0550: 73 74 72 75 63 74 20 7b 0a 09 09 09 69 6e 74 20 struct {....int
0560: 70 72 65 73 65 6e 74 3b 0a 09 09 09 6f 66 66 5f present;....off_
0570: 74 20 6f 66 66 73 65 74 3b 20 20 20 2f 2a 2a 2a t offset; /***
0580: 20 52 61 6e 67 65 20 73 74 61 72 74 20 2a 2a 2a Range start ***
0590: 2f 0a 09 09 09 6f 66 66 5f 74 20 6c 65 6e 67 74 /....off_t lengt
05a0: 68 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67 65 20 h; /*** Range
05b0: 6c 65 6e 67 74 68 20 2a 2a 2a 2f 0a 09 09 7d 20 length ***/...}
05c0: 72 61 6e 67 65 3b 0a 09 7d 20 68 65 61 64 65 72 range;..} header
05d0: 73 3b 0a 7d 3b 0a 0a 2f 2a 20 4c 6f 67 20 72 65 s;.};../* Log re
05e0: 63 6f 72 64 20 2a 2f 0a 73 74 72 75 63 74 20 66 cord */.struct f
05f0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 7b iled_log_entry {
0600: 0a 09 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 67 ../* Type of log
0610: 20 65 6e 74 72 79 20 2a 2f 0a 09 65 6e 75 6d 20 entry */..enum
0620: 7b 0a 09 09 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 {...FILED_LOG_TY
0630: 50 45 5f 4d 45 53 53 41 47 45 2c 0a 09 09 46 49 PE_MESSAGE,...FI
0640: 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54 52 41 LED_LOG_TYPE_TRA
0650: 4e 53 46 45 52 0a 09 7d 20 74 79 70 65 3b 0a 0a NSFER..} type;..
0660: 09 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 ./* Linked list
0670: 68 65 61 64 2f 74 61 69 6c 20 2a 2f 0a 09 73 74 head/tail */..st
0680: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 ruct filed_log_e
0690: 6e 74 72 79 20 2a 5f 6e 65 78 74 3b 0a 09 73 74 ntry *_next;..st
06a0: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 ruct filed_log_e
06b0: 6e 74 72 79 20 2a 5f 70 72 65 76 3b 0a 0a 09 2f ntry *_prev;.../
06c0: 2a 20 54 68 72 65 61 64 20 66 72 6f 6d 20 77 68 * Thread from wh
06d0: 69 63 68 20 74 68 69 73 20 6c 6f 67 20 65 6e 74 ich this log ent
06e0: 72 79 20 65 6d 69 6e 61 74 65 73 20 2a 2f 0a 09 ry eminates */..
06f0: 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 pthread_t thread
0700: 3b 0a 0a 09 2f 2a 20 4d 65 73 73 61 67 65 20 62 ;.../* Message b
0710: 75 66 66 65 72 20 66 6f 72 20 74 79 70 65 20 3d uffer for type =
0720: 20 4d 45 53 53 41 47 45 20 2a 2f 0a 09 2f 2a 20 MESSAGE */../*
0730: 50 61 74 68 20 62 75 66 66 65 72 20 66 6f 72 20 Path buffer for
0740: 74 79 70 65 20 3d 20 54 52 41 4e 53 46 45 52 20 type = TRANSFER
0750: 2a 2f 0a 09 63 68 61 72 20 62 75 66 66 65 72 5b */..char buffer[
0760: 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 FILED_PATH_BUFFE
0770: 52 5f 53 49 5a 45 5d 3b 0a 0a 09 2f 2a 20 49 74 R_SIZE];.../* It
0780: 65 6d 73 20 66 6f 72 20 74 79 70 65 20 3d 20 54 ems for type = T
0790: 52 41 4e 53 46 45 52 20 2a 2f 0a 09 69 6e 74 20 RANSFER */..int
07a0: 68 74 74 70 5f 63 6f 64 65 3b 0a 09 63 6f 6e 73 http_code;..cons
07b0: 74 20 63 68 61 72 20 2a 72 65 61 73 6f 6e 3b 0a t char *reason;.
07c0: 09 74 69 6d 65 5f 74 20 73 74 61 72 74 74 69 6d .time_t starttim
07d0: 65 3b 0a 09 74 69 6d 65 5f 74 20 65 6e 64 74 69 e;..time_t endti
07e0: 6d 65 3b 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6f me;..off_t req_o
07f0: 66 66 73 65 74 3b 0a 09 6f 66 66 5f 74 20 72 65 ffset;..off_t re
0800: 71 5f 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 5f 74 q_length;..off_t
0810: 20 73 65 6e 74 5f 6c 65 6e 67 74 68 3b 0a 09 6f sent_length;..o
0820: 66 66 5f 74 20 66 69 6c 65 5f 6c 65 6e 67 74 68 ff_t file_length
0830: 3b 0a 09 63 68 61 72 20 69 70 5b 31 32 38 5d 3b ;..char ip[128];
0840: 0a 09 69 6e 74 20 70 6f 72 74 3b 0a 7d 3b 0a 0a ..int port;.};..
0850: 2f 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 /* Global variab
0860: 6c 65 73 20 2a 2f 0a 2f 2a 2a 20 4f 70 65 6e 20 les */./** Open
0870: 46 69 6c 65 20 63 61 63 68 65 20 2a 2a 2f 0a 73 File cache **/.s
0880: 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 truct filed_file
0890: 69 6e 66 6f 20 2a 66 69 6c 65 64 5f 66 69 6c 65 info *filed_file
08a0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d 20 4e info_fdcache = N
08b0: 55 4c 4c 3b 0a 75 6e 73 69 67 6e 65 64 20 69 6e ULL;.unsigned in
08c0: 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f t filed_fileinfo
08d0: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 _fdcache_size =
08e0: 30 3b 0a 0a 2f 2a 2a 20 4c 6f 67 67 69 6e 67 20 0;../** Logging
08f0: 2a 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 **/.struct filed
0900: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 66 69 6c 65 _log_entry *file
0910: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a d_log_msg_list;.
0920: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 pthread_mutex_t
0930: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
0940: 73 74 5f 6d 75 74 65 78 3b 0a 70 74 68 72 65 61 st_mutex;.pthrea
0950: 64 5f 63 6f 6e 64 5f 74 20 66 69 6c 65 64 5f 6c d_cond_t filed_l
0960: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 og_msg_list_read
0970: 79 3b 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a y;../* Initializ
0980: 65 20 63 61 63 68 65 20 2a 2f 0a 73 74 61 74 69 e cache */.stati
0990: 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74 c int filed_init
09a0: 5f 63 61 63 68 65 28 75 6e 73 69 67 6e 65 64 20 _cache(unsigned
09b0: 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 29 20 int cache_size)
09c0: 7b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 {..unsigned int
09d0: 69 64 78 3b 0a 09 69 6e 74 20 6d 75 74 65 78 5f idx;..int mutex_
09e0: 69 6e 69 74 5f 72 65 74 3b 0a 0a 09 2f 2a 20 43 init_ret;.../* C
09f0: 61 63 68 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 ache may not be
0a00: 72 65 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a re-initialized *
0a10: 2f 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c /..if (filed_fil
0a20: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 einfo_fdcache_si
0a30: 7a 65 20 21 3d 20 30 20 7c 7c 20 66 69 6c 65 64 ze != 0 || filed
0a40: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 _fileinfo_fdcach
0a50: 65 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 e != NULL) {...r
0a60: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f eturn(1);..}.../
0a70: 2a 20 41 6c 6c 6f 63 61 74 65 20 63 61 63 68 65 * Allocate cache
0a80: 20 2a 2f 0a 09 66 69 6c 65 64 5f 66 69 6c 65 69 */..filed_filei
0a90: 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 nfo_fdcache_size
0aa0: 20 3d 20 63 61 63 68 65 5f 73 69 7a 65 3b 0a 09 = cache_size;..
0ab0: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
0ac0: 64 63 61 63 68 65 20 3d 20 6d 61 6c 6c 6f 63 28 dcache = malloc(
0ad0: 73 69 7a 65 6f 66 28 2a 66 69 6c 65 64 5f 66 69 sizeof(*filed_fi
0ae0: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 29 20 leinfo_fdcache)
0af0: 2a 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f * filed_fileinfo
0b00: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a _fdcache_size);.
0b10: 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 .if (filed_filei
0b20: 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d 3d 20 4e nfo_fdcache == N
0b30: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 ULL) {...return(
0b40: 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 1);..}.../* Init
0b50: 69 61 6c 69 7a 65 20 63 61 63 68 65 20 65 6e 74 ialize cache ent
0b60: 72 69 65 73 20 2a 2f 0a 09 66 6f 72 20 28 69 64 ries */..for (id
0b70: 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 66 69 6c x = 0; idx < fil
0b80: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 ed_fileinfo_fdca
0b90: 63 68 65 5f 73 69 7a 65 3b 20 69 64 78 2b 2b 29 che_size; idx++)
0ba0: 20 7b 0a 09 09 6d 75 74 65 78 5f 69 6e 69 74 5f {...mutex_init_
0bb0: 72 65 74 20 3d 20 70 74 68 72 65 61 64 5f 6d 75 ret = pthread_mu
0bc0: 74 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 64 5f tex_init(&filed_
0bd0: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
0be0: 5b 69 64 78 5d 2e 6d 75 74 65 78 2c 20 4e 55 4c [idx].mutex, NUL
0bf0: 4c 29 3b 0a 09 09 69 66 20 28 6d 75 74 65 78 5f L);...if (mutex_
0c00: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b init_ret != 0) {
0c10: 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 ....return(1);..
0c20: 09 7d 0a 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 .}....filed_file
0c30: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 info_fdcache[idx
0c40: 5d 2e 70 61 74 68 5b 30 5d 20 3d 20 27 5c 30 27 ].path[0] = '\0'
0c50: 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e ;...filed_filein
0c60: 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e fo_fdcache[idx].
0c70: 66 64 20 3d 20 2d 31 3b 0a 09 09 66 69 6c 65 64 fd = -1;...filed
0c80: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 _fileinfo_fdcach
0c90: 65 5b 69 64 78 5d 2e 6c 61 73 74 6d 6f 64 20 3d e[idx].lastmod =
0ca0: 20 22 22 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c "";...filed_fil
0cb0: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 einfo_fdcache[id
0cc0: 78 5d 2e 74 79 70 65 20 3d 20 22 22 3b 0a 09 7d x].type = "";..}
0cd0: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a ...return(0);.}.
0ce0: 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 ./* Initialize p
0cf0: 72 6f 63 65 73 73 20 2a 2f 0a 73 74 61 74 69 63 rocess */.static
0d00: 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74 28 int filed_init(
0d10: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63 unsigned int cac
0d20: 68 65 5f 73 69 7a 65 29 20 7b 0a 09 73 74 61 74 he_size) {..stat
0d30: 69 63 20 69 6e 74 20 63 61 6c 6c 65 64 20 3d 20 ic int called =
0d40: 30 3b 0a 09 69 6e 74 20 63 61 63 68 65 5f 72 65 0;..int cache_re
0d50: 74 3b 0a 0a 09 69 66 20 28 63 61 6c 6c 65 64 29 t;...if (called)
0d60: 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a {...return(0);.
0d70: 09 7d 0a 0a 09 63 61 6c 6c 65 64 20 3d 20 31 3b .}...called = 1;
0d80: 0a 0a 09 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 4c 5f ...mlockall(MCL_
0d90: 43 55 52 52 45 4e 54 20 7c 20 4d 43 4c 5f 46 55 CURRENT | MCL_FU
0da0: 54 55 52 45 29 3b 0a 0a 09 73 69 67 6e 61 6c 28 TURE);...signal(
0db0: 53 49 47 50 49 50 45 2c 20 53 49 47 5f 49 47 4e SIGPIPE, SIG_IGN
0dc0: 29 3b 0a 0a 09 63 61 63 68 65 5f 72 65 74 20 3d );...cache_ret =
0dd0: 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 filed_init_cach
0de0: 65 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 e(cache_size);..
0df0: 69 66 20 28 63 61 63 68 65 5f 72 65 74 20 21 3d if (cache_ret !=
0e00: 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 63 0) {...return(c
0e10: 61 63 68 65 5f 72 65 74 29 3b 0a 09 7d 0a 0a 09 ache_ret);..}...
0e20: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a return(0);.}../*
0e30: 20 4c 69 73 74 65 6e 20 6f 6e 20 61 20 70 61 72 Listen on a par
0e40: 74 69 63 75 6c 61 72 20 61 64 64 72 65 73 73 2f ticular address/
0e50: 70 6f 72 74 20 2a 2f 0a 73 74 61 74 69 63 20 69 port */.static i
0e60: 6e 74 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 nt filed_listen(
0e70: 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 64 64 72 const char *addr
0e80: 65 73 73 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e ess, unsigned in
0e90: 74 20 70 6f 72 74 29 20 7b 0a 09 73 74 72 75 63 t port) {..struc
0ea0: 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 t sockaddr_in6 a
0eb0: 64 64 72 5f 76 36 3b 0a 09 73 74 72 75 63 74 20 ddr_v6;..struct
0ec0: 73 6f 63 6b 61 64 64 72 5f 69 6e 20 61 64 64 72 sockaddr_in addr
0ed0: 5f 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 _v4;..struct soc
0ee0: 6b 61 64 64 72 20 2a 61 64 64 72 3b 0a 09 73 6f kaddr *addr;..so
0ef0: 63 6b 6c 65 6e 5f 74 20 61 64 64 72 5f 6c 65 6e cklen_t addr_len
0f00: 3b 0a 09 69 6e 74 20 70 74 6f 6e 5f 72 65 74 2c ;..int pton_ret,
0f10: 20 62 69 6e 64 5f 72 65 74 2c 20 6c 69 73 74 65 bind_ret, liste
0f20: 6e 5f 72 65 74 3b 0a 09 69 6e 74 20 66 61 6d 69 n_ret;..int fami
0f30: 6c 79 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 0a 09 ly;..int fd;....
0f40: 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 family = AF_INET
0f50: 36 3b 0a 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6;..pton_ret = i
0f60: 6e 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c net_pton(family,
0f70: 20 61 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f address, &addr_
0f80: 76 36 2e 73 69 6e 36 5f 61 64 64 72 2e 73 36 5f v6.sin6_addr.s6_
0f90: 61 64 64 72 29 3b 0a 09 69 66 20 28 70 74 6f 6e addr);..if (pton
0fa0: 5f 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 66 _ret != 1) {...f
0fb0: 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 3b amily = AF_INET;
0fc0: 0a 09 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e ...pton_ret = in
0fd0: 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 et_pton(family,
0fe0: 61 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f 76 address, &addr_v
0ff0: 34 2e 73 69 6e 5f 61 64 64 72 2e 73 5f 61 64 64 4.sin_addr.s_add
1000: 72 29 3b 0a 09 09 69 66 20 28 70 74 6f 6e 5f 72 r);...if (pton_r
1010: 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 09 72 65 et != 1) {....re
1020: 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 0a 09 turn(-1);...}...
1030: 09 61 64 64 72 5f 76 34 2e 73 69 6e 5f 66 61 6d .addr_v4.sin_fam
1040: 69 6c 79 20 3d 20 66 61 6d 69 6c 79 3b 0a 09 09 ily = family;...
1050: 61 64 64 72 5f 76 34 2e 73 69 6e 5f 70 6f 72 74 addr_v4.sin_port
1060: 20 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a = htons(port);.
1070: 0a 09 09 61 64 64 72 20 3d 20 28 73 74 72 75 63 ...addr = (struc
1080: 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 t sockaddr *) &a
1090: 64 64 72 5f 76 34 3b 0a 09 09 61 64 64 72 5f 6c ddr_v4;...addr_l
10a0: 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 en = sizeof(addr
10b0: 5f 76 34 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a _v4);..} else {.
10c0: 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 ..addr_v6.sin6_f
10d0: 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 amily = AF_INET6
10e0: 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 ;...addr_v6.sin6
10f0: 5f 66 6c 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a 09 _flowinfo = 0;..
1100: 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 73 63 .addr_v6.sin6_sc
1110: 6f 70 65 5f 69 64 20 3d 20 30 3b 0a 09 09 61 64 ope_id = 0;...ad
1120: 64 72 5f 76 36 2e 73 69 6e 36 5f 70 6f 72 74 20 dr_v6.sin6_port
1130: 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a = htons(port);..
1140: 09 09 61 64 64 72 20 3d 20 28 73 74 72 75 63 74 ..addr = (struct
1150: 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 sockaddr *) &ad
1160: 64 72 5f 76 36 3b 0a 09 09 61 64 64 72 5f 6c 65 dr_v6;...addr_le
1170: 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 5f n = sizeof(addr_
1180: 76 36 29 3b 0a 09 7d 0a 0a 09 66 64 20 3d 20 73 v6);..}...fd = s
1190: 6f 63 6b 65 74 28 66 61 6d 69 6c 79 2c 20 53 4f ocket(family, SO
11a0: 43 4b 5f 53 54 52 45 41 4d 2c 20 30 29 3b 0a 09 CK_STREAM, 0);..
11b0: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 if (fd < 0) {...
11c0: 72 65 74 75 72 6e 28 66 64 29 3b 0a 09 7d 0a 0a return(fd);..}..
11d0: 09 62 69 6e 64 5f 72 65 74 20 3d 20 62 69 6e 64 .bind_ret = bind
11e0: 28 66 64 2c 20 61 64 64 72 2c 20 61 64 64 72 5f (fd, addr, addr_
11f0: 6c 65 6e 29 3b 0a 09 69 66 20 28 62 69 6e 64 5f len);..if (bind_
1200: 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 63 6c 6f ret < 0) {...clo
1210: 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 se(fd);....retur
1220: 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 6c 69 73 74 n(-1);..}...list
1230: 65 6e 5f 72 65 74 20 3d 20 6c 69 73 74 65 6e 28 en_ret = listen(
1240: 66 64 2c 20 31 32 38 29 3b 0a 09 69 66 20 28 6c fd, 128);..if (l
1250: 69 73 74 65 6e 5f 72 65 74 20 21 3d 20 30 29 20 isten_ret != 0)
1260: 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a {...close(fd);..
1270: 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d ..return(-1);..}
1280: 0a 0a 09 72 65 74 75 72 6e 28 66 64 29 3b 0a 7d ...return(fd);.}
1290: 0a 0a 2f 2a 20 4c 6f 67 20 61 20 6d 65 73 73 61 ../* Log a messa
12a0: 67 65 20 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c ge */.#ifdef FIL
12b0: 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 23 20 20 64 ED_DONT_LOG.# d
12c0: 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 67 efine filed_logg
12d0: 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 ing_thread_init(
12e0: 78 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20 66 x) 0.# define f
12f0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
1300: 75 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a ug(x, ...) /**/.
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 28 78 2c 20 2e 2e 2e 29 20 log_msg(x, ...)
1330: 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 /**/.# define f
1340: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 78 iled_log_entry(x
1350: 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 ) /**/.# define
1360: 20 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 78 2c filed_log_ip(x,
1370: 20 2e 2e 2e 29 20 4e 55 4c 4c 0a 23 20 20 64 65 ...) NULL.# de
1380: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e fine filed_log_n
1390: 65 77 28 78 29 20 26 6c 6f 63 61 6c 5f 64 75 6d ew(x) &local_dum
13a0: 6d 79 5f 6c 6f 67 0a 23 65 6c 73 65 0a 23 20 20 my_log.#else.#
13b0: 69 66 64 65 66 20 46 49 4c 45 44 5f 44 45 42 55 ifdef FILED_DEBU
13c0: 47 0a 23 20 20 20 20 64 65 66 69 6e 65 20 66 69 G.# define fi
13d0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 led_log_msg_debu
13e0: 67 28 78 2c 20 2e 2e 2e 29 20 7b 20 66 70 72 69 g(x, ...) { fpri
13f0: 6e 74 66 28 73 74 64 65 72 72 2c 20 78 2c 20 5f ntf(stderr, x, _
1400: 5f 56 41 5f 41 52 47 53 5f 5f 29 3b 20 66 70 72 _VA_ARGS__); fpr
1410: 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 5c 6e intf(stderr, "\n
1420: 22 29 3b 20 66 66 6c 75 73 68 28 73 74 64 65 72 "); fflush(stder
1430: 72 29 3b 20 7d 0a 23 20 20 65 6c 73 65 0a 23 20 r); }.# else.#
1440: 20 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f define filed_
1450: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c log_msg_debug(x,
1460: 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 65 6e ...) /**/.# en
1470: 64 69 66 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 dif../* Initiali
1480: 7a 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 ze logging threa
1490: 64 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 d */.static void
14a0: 20 2a 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f *filed_logging_
14b0: 74 68 72 65 61 64 28 76 6f 69 64 20 2a 61 72 67 thread(void *arg
14c0: 5f 70 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 _p) {..struct fi
14d0: 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 led_logging_thre
14e0: 61 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 73 ad_args *arg;..s
14f0: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
1500: 65 6e 74 72 79 20 2a 63 75 72 72 2c 20 2a 70 72 entry *curr, *pr
1510: 65 76 3b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 3b ev;..time_t now;
1520: 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 61 72 ..FILE *fp;...ar
1530: 67 20 3d 20 61 72 67 5f 70 3b 0a 0a 09 66 70 20 g = arg_p;...fp
1540: 3d 20 61 72 67 2d 3e 66 70 3b 0a 0a 09 77 68 69 = arg->fp;...whi
1550: 6c 65 20 28 31 29 20 7b 0a 09 09 70 74 68 72 65 le (1) {...pthre
1560: 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 66 ad_mutex_lock(&f
1570: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 iled_log_msg_lis
1580: 74 5f 6d 75 74 65 78 29 3b 0a 09 09 70 74 68 72 t_mutex);...pthr
1590: 65 61 64 5f 63 6f 6e 64 5f 77 61 69 74 28 26 66 ead_cond_wait(&f
15a0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 iled_log_msg_lis
15b0: 74 5f 72 65 61 64 79 2c 20 26 66 69 6c 65 64 5f t_ready, &filed_
15c0: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 log_msg_list_mut
15d0: 65 78 29 3b 0a 0a 09 09 63 75 72 72 20 3d 20 66 ex);....curr = f
15e0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 iled_log_msg_lis
15f0: 74 3b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d t;...filed_log_m
1600: 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a sg_list = NULL;.
1610: 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 ...pthread_mutex
1620: 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c _unlock(&filed_l
1630: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 og_msg_list_mute
1640: 78 29 3b 0a 0a 09 09 6e 6f 77 20 3d 20 74 69 6d x);....now = tim
1650: 65 28 4e 55 4c 4c 29 3b 0a 0a 09 09 70 72 65 76 e(NULL);....prev
1660: 20 3d 20 4e 55 4c 4c 3b 0a 09 09 66 6f 72 20 28 = NULL;...for (
1670: 3b 20 63 75 72 72 3b 20 63 75 72 72 20 3d 20 63 ; curr; curr = c
1680: 75 72 72 2d 3e 5f 6e 65 78 74 29 20 7b 0a 09 09 urr->_next) {...
1690: 09 63 75 72 72 2d 3e 5f 70 72 65 76 20 3d 20 70 .curr->_prev = p
16a0: 72 65 76 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20 rev;.....prev =
16b0: 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 63 75 72 curr;...}....cur
16c0: 72 20 3d 20 70 72 65 76 3b 0a 09 09 77 68 69 6c r = prev;...whil
16d0: 65 20 28 63 75 72 72 29 20 7b 0a 09 09 09 73 77 e (curr) {....sw
16e0: 69 74 63 68 20 28 63 75 72 72 2d 3e 74 79 70 65 itch (curr->type
16f0: 29 20 7b 0a 09 09 09 09 63 61 73 65 20 46 49 4c ) {.....case FIL
1700: 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 53 ED_LOG_TYPE_MESS
1710: 41 47 45 3a 0a 09 09 09 09 09 66 70 72 69 6e 74 AGE:......fprint
1720: 66 28 66 70 2c 20 22 25 73 22 2c 20 63 75 72 72 f(fp, "%s", curr
1730: 2d 3e 62 75 66 66 65 72 29 3b 0a 0a 09 09 09 09 ->buffer);......
1740: 09 62 72 65 61 6b 3b 0a 09 09 09 09 63 61 73 65 .break;.....case
1750: 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f FILED_LOG_TYPE_
1760: 54 52 41 4e 53 46 45 52 3a 0a 09 09 09 09 09 69 TRANSFER:......i
1770: 66 20 28 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 f (curr->endtime
1780: 20 3d 3d 20 28 28 74 69 6d 65 5f 74 29 20 2d 31 == ((time_t) -1
1790: 29 29 20 7b 0a 09 09 09 09 09 09 63 75 72 72 2d )) {.......curr-
17a0: 3e 65 6e 64 74 69 6d 65 20 3d 20 6e 6f 77 3b 0a >endtime = now;.
17b0: 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 66 70 72 .....}.......fpr
17c0: 69 6e 74 66 28 66 70 2c 20 22 54 52 41 4e 53 46 intf(fp, "TRANSF
17d0: 45 52 20 50 41 54 48 3d 25 73 20 53 52 43 3d 25 ER PATH=%s SRC=%
17e0: 73 3a 25 69 20 54 49 4d 45 2e 53 54 41 52 54 3d s:%i TIME.START=
17f0: 25 6c 6c 75 20 54 49 4d 45 2e 45 4e 44 3d 25 6c %llu TIME.END=%l
1800: 6c 75 20 43 4f 44 45 2e 56 41 4c 55 45 3d 25 75 lu CODE.VALUE=%u
1810: 20 43 4f 44 45 2e 52 45 41 53 4f 4e 3d 25 73 20 CODE.REASON=%s
1820: 52 45 51 55 45 53 54 2e 4f 46 46 53 45 54 3d 25 REQUEST.OFFSET=%
1830: 6c 6c 75 20 52 45 51 55 45 53 54 2e 4c 45 4e 47 llu REQUEST.LENG
1840: 54 48 3d 25 6c 6c 75 20 46 49 4c 45 2e 4c 45 4e TH=%llu FILE.LEN
1850: 47 54 48 3d 25 6c 6c 75 20 54 52 41 4e 53 46 45 GTH=%llu TRANSFE
1860: 52 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 22 2c 0a R.LENGTH=%llu",.
1870: 09 09 09 09 09 09 63 75 72 72 2d 3e 62 75 66 66 ......curr->buff
1880: 65 72 2c 0a 09 09 09 09 09 09 63 75 72 72 2d 3e er,.......curr->
1890: 69 70 2c 20 63 75 72 72 2d 3e 70 6f 72 74 2c 0a ip, curr->port,.
18a0: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ......(unsigned
18b0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d long long) curr-
18c0: 3e 73 74 61 72 74 74 69 6d 65 2c 0a 09 09 09 09 >starttime,.....
18d0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
18e0: 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 65 6e 64 long) curr->end
18f0: 74 69 6d 65 2c 0a 09 09 09 09 09 09 63 75 72 72 time,.......curr
1900: 2d 3e 68 74 74 70 5f 63 6f 64 65 2c 20 63 75 72 ->http_code, cur
1910: 72 2d 3e 72 65 61 73 6f 6e 2c 0a 09 09 09 09 09 r->reason,......
1920: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
1930: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 72 65 71 5f long) curr->req_
1940: 6f 66 66 73 65 74 2c 0a 09 09 09 09 09 09 28 75 offset,.......(u
1950: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
1960: 67 29 20 63 75 72 72 2d 3e 72 65 71 5f 6c 65 6e g) curr->req_len
1970: 67 74 68 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 gth,.......(unsi
1980: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
1990: 63 75 72 72 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 curr->file_lengt
19a0: 68 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e h,.......(unsign
19b0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 ed long long) cu
19c0: 72 72 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 0a rr->sent_length.
19d0: 09 09 09 09 09 29 3b 0a 0a 09 09 09 09 09 62 72 .....);.......br
19e0: 65 61 6b 3b 0a 09 09 09 7d 0a 09 09 09 66 70 72 eak;....}....fpr
19f0: 69 6e 74 66 28 66 70 2c 20 22 20 54 48 52 45 41 intf(fp, " THREA
1a00: 44 3d 25 6c 6c 75 20 54 49 4d 45 3d 25 6c 6c 75 D=%llu TIME=%llu
1a10: 5c 6e 22 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e \n",.....(unsign
1a20: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 ed long long) cu
1a30: 72 72 2d 3e 74 68 72 65 61 64 2c 0a 09 09 09 09 rr->thread,.....
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 6e 6f 77 0a 09 09 09 29 3b 0a 09 ong) now....);..
1a60: 09 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 ..fflush(fp);...
1a70: 09 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 09 ..prev = curr;..
1a80: 09 09 63 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f ..curr = curr->_
1a90: 70 72 65 76 3b 0a 0a 09 09 09 66 72 65 65 28 70 prev;.....free(p
1aa0: 72 65 76 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 rev);...}..}...r
1ab0: 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 7d 0a 0a eturn(NULL);.}..
1ac0: 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 static void file
1ad0: 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 73 74 72 75 d_log_entry(stru
1ae0: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ct filed_log_ent
1af0: 72 79 20 2a 65 6e 74 72 79 29 20 7b 0a 09 65 6e ry *entry) {..en
1b00: 74 72 79 2d 3e 74 68 72 65 61 64 20 3d 20 70 74 try->thread = pt
1b10: 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 0a 09 hread_self();...
1b20: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f pthread_mutex_lo
1b30: 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 ck(&filed_log_ms
1b40: 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a g_list_mutex);..
1b50: 09 65 6e 74 72 79 2d 3e 5f 6e 65 78 74 20 3d 20 .entry->_next =
1b60: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
1b70: 73 74 3b 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d st;..filed_log_m
1b80: 73 67 5f 6c 69 73 74 20 3d 20 65 6e 74 72 79 3b sg_list = entry;
1b90: 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 ...pthread_mutex
1ba0: 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c _unlock(&filed_l
1bb0: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 og_msg_list_mute
1bc0: 78 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 6f x);...pthread_co
1bd0: 6e 64 5f 73 69 67 6e 61 6c 28 26 66 69 6c 65 64 nd_signal(&filed
1be0: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 _log_msg_list_re
1bf0: 61 64 79 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a ady);...return;.
1c00: 7d 0a 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 }..static struct
1c10: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 filed_log_entry
1c20: 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 *filed_log_new(
1c30: 69 6e 74 20 69 6e 69 74 69 61 6c 69 7a 65 29 20 int initialize)
1c40: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f {..struct filed_
1c50: 6c 6f 67 5f 65 6e 74 72 79 20 2a 72 65 74 76 61 log_entry *retva
1c60: 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d 61 l;...retval = ma
1c70: 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 72 65 74 lloc(sizeof(*ret
1c80: 76 61 6c 29 29 3b 0a 0a 09 69 66 20 28 69 6e 69 val));...if (ini
1c90: 74 69 61 6c 69 7a 65 29 20 7b 0a 09 09 72 65 74 tialize) {...ret
1ca0: 76 61 6c 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d val->buffer[0] =
1cb0: 20 27 5c 30 27 3b 0a 09 09 72 65 74 76 61 6c 2d '\0';...retval-
1cc0: 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 2d 31 3b >http_code = -1;
1cd0: 0a 09 09 72 65 74 76 61 6c 2d 3e 73 74 61 72 74 ...retval->start
1ce0: 74 69 6d 65 20 3d 20 30 3b 0a 09 09 72 65 74 76 time = 0;...retv
1cf0: 61 6c 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 30 3b al->endtime = 0;
1d00: 0a 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f 6f ...retval->req_o
1d10: 66 66 73 65 74 20 3d 20 30 3b 0a 09 09 72 65 74 ffset = 0;...ret
1d20: 76 61 6c 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20 val->req_length
1d30: 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 73 = 0;...retval->s
1d40: 65 6e 74 5f 6c 65 6e 67 74 68 20 3d 20 30 3b 0a ent_length = 0;.
1d50: 09 09 72 65 74 76 61 6c 2d 3e 66 69 6c 65 5f 6c ..retval->file_l
1d60: 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 74 ength = 0;...ret
1d70: 76 61 6c 2d 3e 69 70 5b 30 5d 20 3d 20 27 5c 30 val->ip[0] = '\0
1d80: 27 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 70 6f 72 ';...retval->por
1d90: 74 20 3d 20 2d 31 3b 0a 09 7d 0a 0a 09 72 65 74 t = -1;..}...ret
1da0: 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a urn(retval);.}..
1db0: 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 static void file
1dc0: 64 5f 6c 6f 67 5f 6d 73 67 28 63 6f 6e 73 74 20 d_log_msg(const
1dd0: 63 68 61 72 20 2a 66 6d 74 2c 20 2e 2e 2e 29 20 char *fmt, ...)
1de0: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f {..struct filed_
1df0: 6c 6f 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 log_entry *entry
1e00: 3b 0a 09 76 61 5f 6c 69 73 74 20 61 72 67 73 3b ;..va_list args;
1e10: 0a 0a 09 65 6e 74 72 79 20 3d 20 66 69 6c 65 64 ...entry = filed
1e20: 5f 6c 6f 67 5f 6e 65 77 28 30 29 3b 0a 0a 09 76 _log_new(0);...v
1e30: 61 5f 73 74 61 72 74 28 61 72 67 73 2c 20 66 6d a_start(args, fm
1e40: 74 29 3b 0a 0a 09 76 73 6e 70 72 69 6e 74 66 28 t);...vsnprintf(
1e50: 65 6e 74 72 79 2d 3e 62 75 66 66 65 72 2c 20 73 entry->buffer, s
1e60: 69 7a 65 6f 66 28 65 6e 74 72 79 2d 3e 62 75 66 izeof(entry->buf
1e70: 66 65 72 29 2c 20 66 6d 74 2c 20 61 72 67 73 29 fer), fmt, args)
1e80: 3b 0a 0a 09 76 61 5f 65 6e 64 28 61 72 67 73 29 ;...va_end(args)
1e90: 3b 0a 0a 09 65 6e 74 72 79 2d 3e 74 79 70 65 20 ;...entry->type
1ea0: 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 = FILED_LOG_TYPE
1eb0: 5f 4d 45 53 53 41 47 45 3b 0a 0a 09 66 69 6c 65 _MESSAGE;...file
1ec0: 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 65 6e 74 72 d_log_entry(entr
1ed0: 79 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a y);...return;.}.
1ee0: 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 .static const ch
1ef0: 61 72 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 ar *filed_log_ip
1f00: 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 (struct sockaddr
1f10: 20 2a 61 64 64 72 2c 20 63 68 61 72 20 2a 62 75 *addr, char *bu
1f20: 66 66 65 72 2c 20 73 69 7a 65 5f 74 20 62 75 66 ffer, size_t buf
1f30: 66 65 72 6c 65 6e 29 20 7b 0a 09 73 74 72 75 63 ferlen) {..struc
1f40: 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 2a 61 t sockaddr_in *a
1f50: 64 64 72 5f 76 34 3b 0a 09 73 74 72 75 63 74 20 ddr_v4;..struct
1f60: 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 2a 61 64 sockaddr_in6 *ad
1f70: 64 72 5f 76 36 3b 0a 09 63 6f 6e 73 74 20 63 68 dr_v6;..const ch
1f80: 61 72 20 2a 72 65 74 76 61 6c 20 3d 20 4e 55 4c ar *retval = NUL
1f90: 4c 3b 0a 0a 09 61 64 64 72 5f 76 36 20 3d 20 28 L;...addr_v6 = (
1fa0: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f struct sockaddr_
1fb0: 69 6e 36 20 2a 29 20 61 64 64 72 3b 0a 0a 09 73 in6 *) addr;...s
1fc0: 77 69 74 63 68 20 28 61 64 64 72 5f 76 36 2d 3e witch (addr_v6->
1fd0: 73 69 6e 36 5f 66 61 6d 69 6c 79 29 20 7b 0a 09 sin6_family) {..
1fe0: 09 63 61 73 65 20 41 46 5f 49 4e 45 54 3a 0a 09 .case AF_INET:..
1ff0: 09 09 61 64 64 72 5f 76 34 20 3d 20 28 73 74 72 ..addr_v4 = (str
2000: 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 uct sockaddr_in
2010: 2a 29 20 61 64 64 72 3b 0a 09 09 09 72 65 74 76 *) addr;....retv
2020: 61 6c 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 41 al = inet_ntop(A
2030: 46 5f 49 4e 45 54 2c 20 26 61 64 64 72 5f 76 34 F_INET, &addr_v4
2040: 2d 3e 73 69 6e 5f 61 64 64 72 2c 20 62 75 66 66 ->sin_addr, buff
2050: 65 72 2c 20 62 75 66 66 65 72 6c 65 6e 29 3b 0a er, bufferlen);.
2060: 09 09 09 62 72 65 61 6b 3b 0a 09 09 63 61 73 65 ...break;...case
2070: 20 41 46 5f 49 4e 45 54 36 3a 0a 09 09 09 72 65 AF_INET6:....re
2080: 74 76 61 6c 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 tval = inet_ntop
2090: 28 41 46 5f 49 4e 45 54 36 2c 20 26 61 64 64 72 (AF_INET6, &addr
20a0: 5f 76 36 2d 3e 73 69 6e 36 5f 61 64 64 72 2c 20 _v6->sin6_addr,
20b0: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 6c 65 buffer, bufferle
20c0: 6e 29 3b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 7d n);....break;..}
20d0: 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c ...return(retval
20e0: 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 );.}..static int
20f0: 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 filed_logging_t
2100: 68 72 65 61 64 5f 69 6e 69 74 28 46 49 4c 45 20 hread_init(FILE
2110: 2a 6c 6f 67 66 70 29 20 7b 0a 09 73 74 72 75 63 *logfp) {..struc
2120: 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f t filed_logging_
2130: 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 thread_args *arg
2140: 73 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 s;..pthread_t th
2150: 72 65 61 64 5f 69 64 3b 0a 0a 09 61 72 67 73 20 read_id;...args
2160: 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 = malloc(sizeof(
2170: 2a 61 72 67 73 29 29 3b 0a 09 61 72 67 73 2d 3e *args));..args->
2180: 66 70 20 3d 20 6c 6f 67 66 70 3b 0a 0a 09 66 69 fp = logfp;...fi
2190: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 led_log_msg_list
21a0: 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 70 74 68 72 65 = NULL;...pthre
21b0: 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 66 ad_mutex_init(&f
21c0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 iled_log_msg_lis
21d0: 74 5f 6d 75 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a t_mutex, NULL);.
21e0: 0a 09 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 ..pthread_create
21f0: 28 26 74 68 72 65 61 64 5f 69 64 2c 20 4e 55 4c (&thread_id, NUL
2200: 4c 2c 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 L, filed_logging
2210: 5f 74 68 72 65 61 64 2c 20 61 72 67 73 29 3b 0a _thread, args);.
2220: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 ..filed_log_msg(
2230: 22 53 54 41 52 54 22 29 3b 0a 0a 09 72 65 74 75 "START");...retu
2240: 72 6e 28 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a rn(0);.}.#endif.
2250: 0a 2f 2a 20 46 6f 72 6d 61 74 20 74 69 6d 65 20 ./* Format time
2260: 70 65 72 20 52 46 43 32 36 31 36 20 2a 2f 0a 73 per RFC2616 */.s
2270: 74 61 74 69 63 20 63 68 61 72 20 2a 66 69 6c 65 tatic char *file
2280: 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 68 d_format_time(ch
2290: 61 72 20 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 ar *buffer, size
22a0: 5f 74 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 63 _t buffer_len, c
22b0: 6f 6e 73 74 20 74 69 6d 65 5f 74 20 74 69 6d 65 onst time_t time
22c0: 69 6e 66 6f 29 20 7b 0a 09 73 74 72 75 63 74 20 info) {..struct
22d0: 74 6d 20 74 69 6d 65 69 6e 66 6f 5f 74 6d 2c 20 tm timeinfo_tm,
22e0: 2a 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 3b 0a *timeinfo_tm_p;.
22f0: 0a 09 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 ..timeinfo_tm_p
2300: 3d 20 67 6d 74 69 6d 65 5f 72 28 26 74 69 6d 65 = gmtime_r(&time
2310: 69 6e 66 6f 2c 20 26 74 69 6d 65 69 6e 66 6f 5f info, &timeinfo_
2320: 74 6d 29 3b 0a 09 69 66 20 28 74 69 6d 65 69 6e tm);..if (timein
2330: 66 6f 5f 74 6d 5f 70 20 3d 3d 20 4e 55 4c 4c 29 fo_tm_p == NULL)
2340: 20 7b 0a 09 09 72 65 74 75 72 6e 28 22 75 6e 6b {...return("unk
2350: 6e 6f 77 6e 22 29 3b 0a 09 7d 0a 0a 09 62 75 66 nown");..}...buf
2360: 66 65 72 5b 62 75 66 66 65 72 5f 6c 65 6e 20 2d fer[buffer_len -
2370: 20 31 5d 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 1] = '\0';..buf
2380: 66 65 72 5f 6c 65 6e 20 3d 20 73 74 72 66 74 69 fer_len = strfti
2390: 6d 65 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 me(buffer, buffe
23a0: 72 5f 6c 65 6e 20 2d 20 31 2c 20 22 25 61 2c 20 r_len - 1, "%a,
23b0: 25 64 20 25 62 20 25 59 20 25 48 3a 25 4d 3a 25 %d %b %Y %H:%M:%
23c0: 53 20 47 4d 54 22 2c 20 74 69 6d 65 69 6e 66 6f S GMT", timeinfo
23d0: 5f 74 6d 5f 70 29 3b 0a 0a 09 72 65 74 75 72 6e _tm_p);...return
23e0: 28 62 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 (buffer);.}../*
23f0: 68 61 73 68 20 2a 2f 0a 73 74 61 74 69 63 20 75 hash */.static u
2400: 6e 73 69 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 nsigned int file
2410: 64 5f 68 61 73 68 28 63 6f 6e 73 74 20 75 6e 73 d_hash(const uns
2420: 69 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 75 igned char *valu
2430: 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 e, unsigned int
2440: 6d 6f 64 75 6c 75 73 29 20 7b 0a 09 75 6e 73 69 modulus) {..unsi
2450: 67 6e 65 64 20 63 68 61 72 20 63 75 72 72 2c 20 gned char curr,
2460: 70 72 65 76 3b 0a 09 69 6e 74 20 64 69 66 66 3b prev;..int diff;
2470: 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 ..unsigned int r
2480: 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 etval;...retval
2490: 3d 20 6d 6f 64 75 6c 75 73 20 2d 20 31 3b 0a 09 = modulus - 1;..
24a0: 70 72 65 76 20 3d 20 6d 6f 64 75 6c 75 73 20 25 prev = modulus %
24b0: 20 32 35 35 3b 0a 0a 09 77 68 69 6c 65 20 28 28 255;...while ((
24c0: 63 75 72 72 20 3d 20 2a 76 61 6c 75 65 29 29 20 curr = *value))
24d0: 7b 0a 09 09 69 66 20 28 63 75 72 72 20 3c 20 33 {...if (curr < 3
24e0: 32 29 20 7b 0a 09 09 09 63 75 72 72 20 3d 20 32 2) {....curr = 2
24f0: 35 35 20 2d 20 63 75 72 72 3b 0a 09 09 7d 20 65 55 - curr;...} e
2500: 6c 73 65 20 7b 0a 09 09 09 63 75 72 72 20 2d 3d lse {....curr -=
2510: 20 33 32 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 32;...}....if (
2520: 70 72 65 76 20 3c 20 63 75 72 72 29 20 7b 0a 09 prev < curr) {..
2530: 09 09 64 69 66 66 20 3d 20 63 75 72 72 20 2d 20 ..diff = curr -
2540: 70 72 65 76 3b 0a 09 09 7d 20 65 6c 73 65 20 7b prev;...} else {
2550: 0a 09 09 09 64 69 66 66 20 3d 20 70 72 65 76 20 ....diff = prev
2560: 2d 20 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 70 - curr;...}....p
2570: 72 65 76 20 3d 20 63 75 72 72 3b 0a 0a 09 09 72 rev = curr;....r
2580: 65 74 76 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 etval <<= 3;...r
2590: 65 74 76 61 6c 20 26 3d 20 30 78 46 46 46 46 46 etval &= 0xFFFFF
25a0: 46 46 46 4c 55 3b 0a 09 09 72 65 74 76 61 6c 20 FFFLU;...retval
25b0: 5e 3d 20 64 69 66 66 3b 0a 0a 09 09 76 61 6c 75 ^= diff;....valu
25c0: 65 2b 2b 3b 0a 09 7d 0a 0a 09 72 65 74 76 61 6c e++;..}...retval
25d0: 20 3d 20 72 65 74 76 61 6c 20 25 20 6d 6f 64 75 = retval % modu
25e0: 6c 75 73 3b 0a 0a 09 72 65 74 75 72 6e 28 72 65 lus;...return(re
25f0: 74 76 61 6c 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 6e tval);.}../* Fin
2600: 64 20 61 20 6d 69 6d 65 2d 74 79 70 65 20 62 61 d a mime-type ba
2610: 73 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 6e sed on the filen
2620: 61 6d 65 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f ame */.static co
2630: 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f nst char *filed_
2640: 64 65 74 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 determine_mimety
2650: 70 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 pe(const char *p
2660: 61 74 68 29 20 7b 0a 09 63 6f 6e 73 74 20 63 68 ath) {..const ch
2670: 61 72 20 2a 70 3b 0a 0a 09 70 20 3d 20 73 74 72 ar *p;...p = str
2680: 72 63 68 72 28 70 61 74 68 2c 20 27 2e 27 29 3b rchr(path, '.');
2690: 0a 09 69 66 20 28 70 20 3d 3d 20 4e 55 4c 4c 29 ..if (p == NULL)
26a0: 20 7b 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 {...return(FILE
26b0: 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b D_DEFAULT_TYPE);
26c0: 0a 09 7d 0a 0a 09 70 2b 2b 3b 0a 09 69 66 20 28 ..}...p++;..if (
26d0: 2a 70 20 3d 3d 20 27 5c 30 27 29 20 7b 0a 09 09 *p == '\0') {...
26e0: 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 return(FILED_DEF
26f0: 41 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a AULT_TYPE);..}..
2700: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 .filed_log_msg_d
2710: 65 62 75 67 28 22 4c 6f 6f 6b 69 6e 67 20 75 70 ebug("Looking up
2720: 20 4d 49 4d 45 20 74 79 70 65 20 66 6f 72 20 25 MIME type for %
2730: 73 20 28 68 61 73 68 20 3d 20 25 6c 6c 75 29 22 s (hash = %llu)"
2740: 2c 20 70 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c , p, (unsigned l
2750: 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 64 5f ong long) filed_
2760: 68 61 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 hash((const unsi
2770: 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70 2c 20 gned char *) p,
2780: 31 36 37 37 37 32 35 39 29 29 3b 0a 0a 23 69 6e 16777259));..#in
2790: 63 6c 75 64 65 20 22 66 69 6c 65 64 2d 6d 69 6d clude "filed-mim
27a0: 65 2d 74 79 70 65 73 2e 68 22 0a 0a 09 72 65 74 e-types.h"...ret
27b0: 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c urn(FILED_DEFAUL
27c0: 54 5f 54 59 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 4f T_TYPE);.}../* O
27d0: 70 65 6e 20 61 20 66 69 6c 65 20 61 6e 64 20 72 pen a file and r
27e0: 65 74 75 72 6e 20 66 69 6c 65 20 69 6e 66 6f 72 eturn file infor
27f0: 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 mation */.static
2800: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 struct filed_fi
2810: 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 64 5f 6f 70 leinfo *filed_op
2820: 65 6e 5f 66 69 6c 65 28 63 6f 6e 73 74 20 63 68 en_file(const ch
2830: 61 72 20 2a 70 61 74 68 2c 20 73 74 72 75 63 74 ar *path, struct
2840: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 filed_fileinfo
2850: 2a 62 75 66 66 65 72 29 20 7b 0a 09 73 74 72 75 *buffer) {..stru
2860: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 ct filed_fileinf
2870: 6f 20 2a 63 61 63 68 65 3b 0a 09 63 6f 6e 73 74 o *cache;..const
2880: 20 63 68 61 72 20 2a 6f 70 65 6e 5f 70 61 74 68 char *open_path
2890: 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 ;..unsigned int
28a0: 63 61 63 68 65 5f 69 64 78 3b 0a 09 6f 66 66 5f cache_idx;..off_
28b0: 74 20 6c 65 6e 3b 0a 09 69 6e 74 20 66 64 3b 0a t len;..int fd;.
28c0: 0a 09 63 61 63 68 65 5f 69 64 78 20 3d 20 66 69 ..cache_idx = fi
28d0: 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e 73 74 20 led_hash((const
28e0: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
28f0: 20 70 61 74 68 2c 20 66 69 6c 65 64 5f 66 69 6c path, filed_fil
2900: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 einfo_fdcache_si
2910: 7a 65 29 3b 0a 0a 09 63 61 63 68 65 20 3d 20 26 ze);...cache = &
2920: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
2930: 64 63 61 63 68 65 5b 63 61 63 68 65 5f 69 64 78 dcache[cache_idx
2940: 5d 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d ];...filed_log_m
2950: 73 67 5f 64 65 62 75 67 28 22 4c 6f 63 6b 69 6e sg_debug("Lockin
2960: 67 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a g mutex for idx:
2970: 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 %lu", (unsigned
2980: 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 long) cache_idx
2990: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 );...pthread_mut
29a0: 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e ex_lock(&cache->
29b0: 6d 75 74 65 78 29 3b 0a 0a 09 66 69 6c 65 64 5f mutex);...filed_
29c0: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 log_msg_debug("C
29d0: 6f 6d 70 6c 65 74 65 64 20 6c 6f 63 6b 69 6e 67 ompleted locking
29e0: 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a 20 mutex for idx:
29f0: 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 %lu", (unsigned
2a00: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 29 long) cache_idx)
2a10: 3b 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 28 70 ;...if (strcmp(p
2a20: 61 74 68 2c 20 63 61 63 68 65 2d 3e 70 61 74 68 ath, cache->path
2a30: 29 20 21 3d 20 30 29 20 7b 0a 09 09 66 69 6c 65 ) != 0) {...file
2a40: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 d_log_msg_debug(
2a50: 22 43 61 63 68 65 20 6d 69 73 73 20 66 6f 72 20 "Cache miss for
2a60: 69 64 78 3a 20 25 6c 75 3a 20 4f 4c 44 20 5c 22 idx: %lu: OLD \"
2a70: 25 73 5c 22 2c 20 4e 45 57 20 5c 22 25 73 5c 22 %s\", NEW \"%s\"
2a80: 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ", (unsigned lon
2a90: 67 29 20 63 61 63 68 65 5f 69 64 78 2c 20 63 61 g) cache_idx, ca
2aa0: 63 68 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 che->path, path)
2ab0: 3b 0a 0a 09 09 2f 2a 20 46 6f 72 20 72 65 71 75 ;..../* For requ
2ac0: 65 73 74 73 20 66 6f 72 20 74 68 65 20 72 6f 6f ests for the roo
2ad0: 74 20 64 69 72 65 63 74 6f 72 79 2c 20 73 65 72 t directory, ser
2ae0: 76 65 20 6f 75 74 20 69 6e 64 65 78 2e 68 74 6d ve out index.htm
2af0: 6c 20 2a 2f 0a 09 09 69 66 20 28 70 61 74 68 5b l */...if (path[
2b00: 30 5d 20 3d 3d 20 27 5c 30 27 20 7c 7c 20 28 70 0] == '\0' || (p
2b10: 61 74 68 5b 30 5d 20 3d 3d 20 27 2f 27 20 26 26 ath[0] == '/' &&
2b20: 20 70 61 74 68 5b 31 5d 20 3d 3d 20 27 5c 30 27 path[1] == '\0'
2b30: 29 29 20 7b 0a 09 09 09 6f 70 65 6e 5f 70 61 74 )) {....open_pat
2b40: 68 20 3d 20 22 2f 69 6e 64 65 78 2e 68 74 6d 6c h = "/index.html
2b50: 22 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 ";...} else {...
2b60: 09 6f 70 65 6e 5f 70 61 74 68 20 3d 20 70 61 74 .open_path = pat
2b70: 68 3b 0a 09 09 7d 0a 0a 09 09 66 64 20 3d 20 6f h;...}....fd = o
2b80: 70 65 6e 28 6f 70 65 6e 5f 70 61 74 68 2c 20 4f pen(open_path, O
2b90: 5f 52 44 4f 4e 4c 59 20 7c 20 4f 5f 4c 41 52 47 _RDONLY | O_LARG
2ba0: 45 46 49 4c 45 29 3b 0a 09 09 69 66 20 28 66 64 EFILE);...if (fd
2bb0: 20 3c 20 30 29 20 7b 0a 09 09 09 70 74 68 72 65 < 0) {....pthre
2bc0: 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 ad_mutex_unlock(
2bd0: 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a &cache->mutex);.
2be0: 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 ....return(NULL)
2bf0: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 63 61 63 ;...}....if (cac
2c00: 68 65 2d 3e 66 64 20 3e 3d 20 30 29 20 7b 0a 09 he->fd >= 0) {..
2c10: 09 09 63 6c 6f 73 65 28 63 61 63 68 65 2d 3e 66 ..close(cache->f
2c20: 64 29 3b 0a 09 09 7d 0a 0a 09 09 6c 65 6e 20 3d d);...}....len =
2c30: 20 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 53 45 lseek(fd, 0, SE
2c40: 45 4b 5f 45 4e 44 29 3b 0a 09 09 6c 73 65 65 6b EK_END);...lseek
2c50: 28 66 64 2c 20 30 2c 20 53 45 45 4b 5f 53 45 54 (fd, 0, SEEK_SET
2c60: 29 3b 0a 0a 09 09 63 61 63 68 65 2d 3e 66 64 20 );....cache->fd
2c70: 3d 20 66 64 3b 0a 09 09 63 61 63 68 65 2d 3e 6c = fd;...cache->l
2c80: 65 6e 20 3d 20 6c 65 6e 3b 0a 09 09 73 74 72 63 en = len;...strc
2c90: 70 79 28 63 61 63 68 65 2d 3e 70 61 74 68 2c 20 py(cache->path,
2ca0: 70 61 74 68 29 3b 0a 09 09 63 61 63 68 65 2d 3e path);...cache->
2cb0: 74 79 70 65 20 3d 20 66 69 6c 65 64 5f 64 65 74 type = filed_det
2cc0: 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 ermine_mimetype(
2cd0: 6f 70 65 6e 5f 70 61 74 68 29 3b 0a 0a 09 09 2f open_path);..../
2ce0: 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 44 65 74 65 * XXX:TODO: Dete
2cf0: 72 6d 69 6e 65 20 2a 2f 0a 09 09 63 61 63 68 65 rmine */...cache
2d00: 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 ->lastmod = file
2d10: 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 d_format_time(ca
2d20: 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 che->lastmod_b,
2d30: 73 69 7a 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 sizeof(cache->la
2d40: 73 74 6d 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e stmod_b), time(N
2d50: 55 4c 4c 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 ULL) - 30);..} e
2d60: 6c 73 65 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f lse {...filed_lo
2d70: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 61 63 g_msg_debug("Cac
2d80: 68 65 20 68 69 74 20 66 6f 72 20 69 64 78 3a 20 he hit for idx:
2d90: 25 6c 75 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 %lu: PATH \"%s\"
2da0: 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ", (unsigned lon
2db0: 67 29 20 63 61 63 68 65 5f 69 64 78 2c 20 70 61 g) cache_idx, pa
2dc0: 74 68 29 3b 0a 09 7d 0a 0a 09 2f 2a 0a 09 20 2a th);..}.../*.. *
2dd0: 20 57 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 We have to make
2de0: 20 61 20 64 75 70 6c 69 63 61 74 65 20 46 44 2c a duplicate FD,
2df0: 20 62 65 63 61 75 73 65 20 6f 6e 63 65 20 77 65 because once we
2e00: 20 72 65 6c 65 61 73 65 20 74 68 65 20 63 61 63 release the cac
2e10: 68 65 0a 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 he.. * mutex, th
2e20: 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
2e30: 72 20 6d 61 79 20 62 65 20 63 6c 6f 73 65 64 0a r may be closed.
2e40: 09 20 2a 2f 0a 09 66 64 20 3d 20 64 75 70 28 63 . */..fd = dup(c
2e50: 61 63 68 65 2d 3e 66 64 29 3b 0a 09 69 66 20 28 ache->fd);..if (
2e60: 66 64 20 3c 20 30 29 20 7b 0a 09 09 70 74 68 72 fd < 0) {...pthr
2e70: 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b ead_mutex_unlock
2e80: 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b (&cache->mutex);
2e90: 0a 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 ....return(NULL)
2ea0: 3b 0a 09 7d 0a 0a 09 62 75 66 66 65 72 2d 3e 66 ;..}...buffer->f
2eb0: 64 20 3d 20 66 64 3b 0a 09 62 75 66 66 65 72 2d d = fd;..buffer-
2ec0: 3e 6c 65 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 >len = cache->le
2ed0: 6e 3b 0a 09 62 75 66 66 65 72 2d 3e 74 79 70 65 n;..buffer->type
2ee0: 20 3d 20 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a = cache->type;.
2ef0: 09 6d 65 6d 63 70 79 28 62 75 66 66 65 72 2d 3e .memcpy(buffer->
2f00: 6c 61 73 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 lastmod_b, cache
2f10: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a ->lastmod_b, siz
2f20: 65 6f 66 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 eof(buffer->last
2f30: 6d 6f 64 5f 62 29 29 3b 0a 09 62 75 66 66 65 72 mod_b));..buffer
2f40: 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20 62 75 66 66 ->lastmod = buff
2f50: 65 72 2d 3e 6c 61 73 74 6d 6f 64 5f 62 20 2b 20 er->lastmod_b +
2f60: 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 (cache->lastmod
2f70: 2d 20 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 - cache->lastmod
2f80: 5f 62 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d _b);...pthread_m
2f90: 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 utex_unlock(&cac
2fa0: 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 72 65 he->mutex);...re
2fb0: 74 75 72 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a turn(buffer);.}.
2fc0: 0a 2f 2a 20 50 72 6f 63 65 73 73 20 61 6e 20 48 ./* Process an H
2fd0: 54 54 50 20 72 65 71 75 65 73 74 20 61 6e 64 20 TTP request and
2fe0: 72 65 74 75 72 6e 20 74 68 65 20 70 61 74 68 20 return the path
2ff0: 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 73 74 61 requested */.sta
3000: 74 69 63 20 73 74 72 75 63 74 20 66 69 6c 65 64 tic struct filed
3010: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 66 _http_request *f
3020: 69 6c 65 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 iled_get_http_re
3030: 71 75 65 73 74 28 46 49 4c 45 20 2a 66 70 2c 20 quest(FILE *fp,
3040: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 struct filed_htt
3050: 70 5f 72 65 71 75 65 73 74 20 2a 62 75 66 66 65 p_request *buffe
3060: 72 5f 73 74 29 20 7b 0a 09 63 68 61 72 20 2a 6d r_st) {..char *m
3070: 65 74 68 6f 64 2c 20 2a 70 61 74 68 3b 0a 09 63 ethod, *path;..c
3080: 68 61 72 20 2a 62 75 66 66 65 72 2c 20 2a 77 6f har *buffer, *wo
3090: 72 6b 62 75 66 66 65 72 2c 20 2a 77 6f 72 6b 62 rkbuffer, *workb
30a0: 75 66 66 65 72 5f 6e 65 78 74 3b 0a 09 73 69 7a uffer_next;..siz
30b0: 65 5f 74 20 62 75 66 66 65 72 5f 6c 65 6e 3b 0a e_t buffer_len;.
30c0: 09 6f 66 66 5f 74 20 72 61 6e 67 65 5f 73 74 61 .off_t range_sta
30d0: 72 74 2c 20 72 61 6e 67 65 5f 65 6e 64 2c 20 72 rt, range_end, r
30e0: 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 09 69 6e ange_length;..in
30f0: 74 20 72 61 6e 67 65 5f 72 65 71 75 65 73 74 3b t range_request;
3100: 0a 09 69 6e 74 20 66 64 3b 0a 09 69 6e 74 20 69 ..int fd;..int i
3110: 3b 0a 0a 09 66 64 20 3d 20 66 69 6c 65 6e 6f 28 ;...fd = fileno(
3120: 66 70 29 3b 0a 0a 09 72 61 6e 67 65 5f 73 74 61 fp);...range_sta
3130: 72 74 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 65 rt = 0;..range_e
3140: 6e 64 20 20 20 3d 20 30 3b 0a 09 72 61 6e 67 65 nd = 0;..range
3150: 5f 72 65 71 75 65 73 74 20 3d 20 30 3b 0a 09 72 _request = 0;..r
3160: 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d 20 2d 31 ange_length = -1
3170: 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 62 75 66 ;...buffer = buf
3180: 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a fer_st->tmpbuf;.
3190: 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 69 .buffer_len = si
31a0: 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 74 2d 3e zeof(buffer_st->
31b0: 74 6d 70 62 75 66 29 3b 0a 0a 09 66 67 65 74 73 tmpbuf);...fgets
31c0: 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f (buffer, buffer_
31d0: 6c 65 6e 2c 20 66 70 29 3b 0a 0a 09 6d 65 74 68 len, fp);...meth
31e0: 6f 64 20 3d 20 62 75 66 66 65 72 3b 0a 0a 09 62 od = buffer;...b
31f0: 75 66 66 65 72 20 3d 20 73 74 72 63 68 72 28 62 uffer = strchr(b
3200: 75 66 66 65 72 2c 20 27 20 27 29 3b 0a 09 69 66 uffer, ' ');..if
3210: 20 28 62 75 66 66 65 72 20 3d 3d 20 4e 55 4c 4c (buffer == NULL
3220: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c ) {...return(NUL
3230: 4c 29 3b 0a 09 7d 0a 0a 09 2a 62 75 66 66 65 72 L);..}...*buffer
3240: 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 = '\0';..buffer
3250: 2b 2b 3b 0a 0a 09 70 61 74 68 20 3d 20 62 75 66 ++;...path = buf
3260: 66 65 72 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 fer;...buffer =
3270: 73 74 72 63 68 72 28 62 75 66 66 65 72 2c 20 27 strchr(buffer, '
3280: 20 27 29 3b 0a 09 69 66 20 28 62 75 66 66 65 72 ');..if (buffer
3290: 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 62 != NULL) {...*b
32a0: 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 uffer = '\0';...
32b0: 62 75 66 66 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 2f buffer++;..}.../
32c0: 2a 20 57 65 20 6f 6e 6c 79 20 68 61 6e 64 6c 65 * We only handle
32d0: 20 74 68 65 20 22 47 45 54 22 20 6d 65 74 68 6f the "GET" metho
32e0: 64 20 2a 2f 0a 09 69 66 20 28 73 74 72 63 61 73 d */..if (strcas
32f0: 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 22 67 65 ecmp(method, "ge
3300: 74 22 29 20 21 3d 20 30 29 20 7b 0a 09 09 72 65 t") != 0) {...re
3310: 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a turn(NULL);..}..
3320: 09 2f 2a 20 4e 6f 74 65 20 70 61 74 68 20 2a 2f ./* Note path */
3330: 0a 09 73 74 72 63 70 79 28 62 75 66 66 65 72 5f ..strcpy(buffer_
3340: 73 74 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 3b st->path, path);
3350: 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 ...for (i = 0; i
3360: 20 3c 20 31 30 30 3b 20 69 2b 2b 29 20 7b 0a 09 < 100; i++) {..
3370: 09 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 .buffer = buffer
3380: 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a 09 09 66 _st->tmpbuf;...f
3390: 67 65 74 73 28 62 75 66 66 65 72 2c 20 62 75 66 gets(buffer, buf
33a0: 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 0a 09 fer_len, fp);...
33b0: 09 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70 .if (strncasecmp
33c0: 28 62 75 66 66 65 72 2c 20 22 52 61 6e 67 65 3a (buffer, "Range:
33d0: 20 22 2c 20 37 29 20 3d 3d 20 30 29 20 7b 0a 09 ", 7) == 0) {..
33e0: 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 62 ..workbuffer = b
33f0: 75 66 66 65 72 20 2b 20 37 3b 0a 0a 09 09 09 69 uffer + 7;.....i
3400: 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 77 f (strncasecmp(w
3410: 6f 72 6b 62 75 66 66 65 72 2c 20 22 62 79 74 65 orkbuffer, "byte
3420: 73 3d 22 2c 20 36 29 20 3d 3d 20 30 29 20 7b 0a s=", 6) == 0) {.
3430: 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 2b ....workbuffer +
3440: 3d 20 36 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f = 6;......range_
3450: 72 65 71 75 65 73 74 20 3d 20 31 3b 0a 0a 09 09 request = 1;....
3460: 09 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d 20 ..range_start =
3470: 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 strtoull(workbuf
3480: 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 72 fer, &workbuffer
3490: 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 0a 09 09 09 _next, 10);.....
34a0: 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 77 6f .workbuffer = wo
34b0: 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 0a rkbuffer_next;..
34c0: 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 66 ....if (*workbuf
34d0: 66 65 72 20 3d 3d 20 27 2d 27 29 20 7b 0a 09 09 fer == '-') {...
34e0: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b ...workbuffer++;
34f0: 0a 0a 09 09 09 09 09 69 66 20 28 2a 77 6f 72 6b .......if (*work
3500: 62 75 66 66 65 72 20 21 3d 20 27 5c 72 27 20 26 buffer != '\r' &
3510: 26 20 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 3d & *workbuffer !=
3520: 20 27 5c 6e 27 29 20 7b 0a 09 09 09 09 09 09 72 '\n') {.......r
3530: 61 6e 67 65 5f 65 6e 64 20 3d 20 73 74 72 74 6f ange_end = strto
3540: 75 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 ull(workbuffer,
3550: 26 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 &workbuffer_next
3560: 2c 20 31 30 29 3b 0a 09 09 09 09 09 7d 0a 09 09 , 10);......}...
3570: 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a 0a 09 09 ..}....}...}....
3580: 69 66 20 28 6d 65 6d 63 6d 70 28 62 75 66 66 65 if (memcmp(buffe
3590: 72 2c 20 22 5c 72 5c 6e 22 2c 20 32 29 20 3d 3d r, "\r\n", 2) ==
35a0: 20 30 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 0) {....break;.
35b0: 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 65 74 65 ..}..}.../* Dete
35c0: 72 6d 69 6e 65 20 72 61 6e 67 65 20 2a 2f 0a 09 rmine range */..
35d0: 69 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 21 3d if (range_end !=
35e0: 20 30 29 20 7b 0a 09 09 69 66 20 28 72 61 6e 67 0) {...if (rang
35f0: 65 5f 65 6e 64 20 3c 3d 20 72 61 6e 67 65 5f 73 e_end <= range_s
3600: 74 61 72 74 29 20 7b 0a 09 09 09 72 65 74 75 72 tart) {....retur
3610: 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 n(NULL);...}....
3620: 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d 20 72 range_length = r
3630: 61 6e 67 65 5f 65 6e 64 20 2d 20 72 61 6e 67 65 ange_end - range
3640: 5f 73 74 61 72 74 3b 0a 0a 09 09 66 69 6c 65 64 _start;....filed
3650: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 _log_msg_debug("
3660: 43 6f 6d 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 Computing length
3670: 20 70 61 72 61 6d 65 74 65 72 3a 20 25 6c 6c 75 parameter: %llu
3680: 20 3d 20 25 6c 6c 75 20 2d 20 25 6c 6c 75 22 2c = %llu - %llu",
3690: 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f ....(unsigned lo
36a0: 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 6c ng long) range_l
36b0: 65 6e 67 74 68 2c 0a 09 09 09 28 75 6e 73 69 67 ength,....(unsig
36c0: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 ned long long) r
36d0: 61 6e 67 65 5f 65 6e 64 2c 0a 09 09 09 28 75 6e ange_end,....(un
36e0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
36f0: 29 20 72 61 6e 67 65 5f 73 74 61 72 74 0a 09 09 ) range_start...
3700: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 46 69 6c 6c 20 );..}.../* Fill
3710: 75 70 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 up structure to
3720: 72 65 74 75 72 6e 20 2a 2f 0a 09 62 75 66 66 65 return */..buffe
3730: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 r_st->headers.ra
3740: 6e 67 65 2e 70 72 65 73 65 6e 74 20 3d 20 72 61 nge.present = ra
3750: 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a 09 62 75 nge_request;..bu
3760: 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 ffer_st->headers
3770: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 20 3d .range.offset =
3780: 20 72 61 6e 67 65 5f 73 74 61 72 74 3b 0a 09 62 range_start;..b
3790: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 uffer_st->header
37a0: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 20 s.range.length
37b0: 3d 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a = range_length;.
37c0: 0a 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 5f ..return(buffer_
37d0: 73 74 29 3b 0a 0a 09 2f 2a 20 4d 61 6b 65 20 63 st);.../* Make c
37e0: 6f 6d 70 69 6c 65 72 20 68 61 70 70 79 20 2a 2f ompiler happy */
37f0: 0a 09 66 64 20 3d 20 66 64 3b 0a 7d 0a 0a 2f 2a ..fd = fd;.}../*
3800: 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
3810: 20 70 61 67 65 20 2a 2f 0a 73 74 61 74 69 63 20 page */.static
3820: 76 6f 69 64 20 66 69 6c 65 64 5f 65 72 72 6f 72 void filed_error
3830: 5f 70 61 67 65 28 46 49 4c 45 20 2a 66 70 2c 20 _page(FILE *fp,
3840: 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 65 const char *date
3850: 5f 63 75 72 72 65 6e 74 2c 20 69 6e 74 20 65 72 _current, int er
3860: 72 6f 72 5f 6e 75 6d 62 65 72 29 20 7b 0a 09 63 ror_number) {..c
3870: 68 61 72 20 2a 65 72 72 6f 72 5f 73 74 72 69 6e har *error_strin
3880: 67 20 3d 20 22 3c 68 74 6d 6c 3e 3c 68 65 61 64 g = "<html><head
3890: 3e 3c 74 69 74 6c 65 3e 45 52 52 4f 52 3c 2f 74 ><title>ERROR</t
38a0: 69 74 6c 65 3e 3c 2f 68 65 61 64 3e 3c 62 6f 64 itle></head><bod
38b0: 79 3e 55 6e 61 62 6c 65 20 74 6f 20 70 72 6f 63 y>Unable to proc
38c0: 65 73 73 20 72 65 71 75 65 73 74 3c 2f 62 6f 64 ess request</bod
38d0: 79 3e 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a 09 66 70 y></html>";...fp
38e0: 72 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f rintf(fp, "HTTP/
38f0: 31 2e 31 20 25 69 20 4e 6f 74 20 4f 4b 5c 72 5c 1.1 %i Not OK\r\
3900: 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72 nDate: %s\r\nSer
3910: 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 ver: filed\r\nLa
3920: 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c st-Modified: %s\
3930: 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 r\nContent-Lengt
3940: 68 3a 20 25 6c 6c 75 5c 72 5c 6e 43 6f 6e 74 65 h: %llu\r\nConte
3950: 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e 43 nt-Type: %s\r\nC
3960: 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 onnection: close
3970: 5c 72 5c 6e 5c 72 5c 6e 25 73 22 2c 0a 09 09 65 \r\n\r\n%s",...e
3980: 72 72 6f 72 5f 6e 75 6d 62 65 72 2c 0a 09 09 64 rror_number,...d
3990: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64 ate_current,...d
39a0: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 28 ate_current,...(
39b0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
39c0: 6e 67 29 20 73 74 72 6c 65 6e 28 65 72 72 6f 72 ng) strlen(error
39d0: 5f 73 74 72 69 6e 67 29 2c 0a 09 09 22 74 65 78 _string),..."tex
39e0: 74 2f 68 74 6d 6c 22 2c 0a 09 09 65 72 72 6f 72 t/html",...error
39f0: 5f 73 74 72 69 6e 67 0a 09 29 3b 0a 7d 0a 0a 2f _string..);.}../
3a00: 2a 20 48 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c * Handle a singl
3a10: 65 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 61 e request from a
3a20: 20 63 6c 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 client */.stati
3a30: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 68 61 6e c void filed_han
3a40: 64 6c 65 5f 63 6c 69 65 6e 74 28 69 6e 74 20 66 dle_client(int f
3a50: 64 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f d, struct filed_
3a60: 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 72 65 http_request *re
3a70: 71 75 65 73 74 2c 20 73 74 72 75 63 74 20 66 69 quest, struct fi
3a80: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c led_log_entry *l
3a90: 6f 67 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 og) {..struct fi
3aa0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 led_fileinfo *fi
3ab0: 6c 65 69 6e 66 6f 3b 0a 09 73 73 69 7a 65 5f 74 leinfo;..ssize_t
3ac0: 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 sendfile_ret;..
3ad0: 73 69 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f size_t sendfile_
3ae0: 73 69 7a 65 3b 0a 09 6f 66 66 5f 74 20 73 65 6e size;..off_t sen
3af0: 64 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 dfile_offset, se
3b00: 6e 64 66 69 6c 65 5f 73 65 6e 74 2c 20 73 65 6e ndfile_sent, sen
3b10: 64 66 69 6c 65 5f 6c 65 6e 3b 0a 09 63 68 61 72 dfile_len;..char
3b20: 20 2a 70 61 74 68 3b 0a 09 63 68 61 72 20 2a 64 *path;..char *d
3b30: 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 64 61 74 ate_current, dat
3b40: 65 5f 63 75 72 72 65 6e 74 5f 62 5b 36 34 5d 3b e_current_b[64];
3b50: 0a 09 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b ..int http_code;
3b60: 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 2f 2a ..FILE *fp;.../*
3b70: 20 44 65 74 65 72 6d 69 6e 65 20 63 75 72 72 65 Determine curre
3b80: 6e 74 20 74 69 6d 65 20 2a 2f 0a 09 64 61 74 65 nt time */..date
3b90: 5f 63 75 72 72 65 6e 74 20 3d 20 66 69 6c 65 64 _current = filed
3ba0: 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 64 61 74 _format_time(dat
3bb0: 65 5f 63 75 72 72 65 6e 74 5f 62 2c 20 73 69 7a e_current_b, siz
3bc0: 65 6f 66 28 64 61 74 65 5f 63 75 72 72 65 6e 74 eof(date_current
3bd0: 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 29 _b), time(NULL))
3be0: 3b 0a 0a 09 2f 2a 20 4f 70 65 6e 20 73 6f 63 6b ;.../* Open sock
3bf0: 65 74 20 61 73 20 41 4e 53 49 20 49 2f 4f 20 66 et as ANSI I/O f
3c00: 6f 72 20 65 61 73 65 20 6f 66 20 75 73 65 20 2a or ease of use *
3c10: 2f 0a 09 66 70 20 3d 20 66 64 6f 70 65 6e 28 66 /..fp = fdopen(f
3c20: 64 2c 20 22 77 2b 62 22 29 3b 0a 09 69 66 20 28 d, "w+b");..if (
3c30: 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 fp == NULL) {...
3c40: 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 close(fd);....re
3c50: 74 75 72 6e 3b 0a 09 7d 0a 0a 09 72 65 71 75 65 turn;..}...reque
3c60: 73 74 20 3d 20 66 69 6c 65 64 5f 67 65 74 5f 68 st = filed_get_h
3c70: 74 74 70 5f 72 65 71 75 65 73 74 28 66 70 2c 20 ttp_request(fp,
3c80: 72 65 71 75 65 73 74 29 3b 0a 0a 09 69 66 20 28 request);...if (
3c90: 72 65 71 75 65 73 74 20 3d 3d 20 4e 55 4c 4c 29 request == NULL)
3ca0: 20 7b 0a 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 {...filed_error
3cb0: 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f 63 _page(fp, date_c
3cc0: 75 72 72 65 6e 74 2c 20 35 30 30 29 3b 0a 0a 09 urrent, 500);...
3cd0: 09 6c 6f 67 2d 3e 62 75 66 66 65 72 5b 30 5d 20 .log->buffer[0]
3ce0: 3d 20 27 5c 30 27 3b 0a 09 09 6c 6f 67 2d 3e 68 = '\0';...log->h
3cf0: 74 74 70 5f 63 6f 64 65 20 3d 20 35 30 30 3b 0a ttp_code = 500;.
3d00: 09 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 ..log->reason =
3d10: 22 66 6f 72 6d 61 74 22 3b 0a 0a 09 09 66 69 6c "format";....fil
3d20: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 ed_log_entry(log
3d30: 29 3b 0a 0a 09 09 66 63 6c 6f 73 65 28 66 70 29 );....fclose(fp)
3d40: 3b 0a 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a ;....return;..}.
3d50: 0a 09 70 61 74 68 20 3d 20 72 65 71 75 65 73 74 ..path = request
3d60: 2d 3e 70 61 74 68 3b 0a 09 73 74 72 63 70 79 28 ->path;..strcpy(
3d70: 6c 6f 67 2d 3e 62 75 66 66 65 72 2c 20 70 61 74 log->buffer, pat
3d80: 68 29 3b 0a 0a 09 68 74 74 70 5f 63 6f 64 65 20 h);...http_code
3d90: 3d 20 2d 31 3b 0a 0a 09 66 69 6c 65 69 6e 66 6f = -1;...fileinfo
3da0: 20 3d 20 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 = filed_open_fi
3db0: 6c 65 28 70 61 74 68 2c 20 26 72 65 71 75 65 73 le(path, &reques
3dc0: 74 2d 3e 66 69 6c 65 69 6e 66 6f 29 3b 0a 09 69 t->fileinfo);..i
3dd0: 66 20 28 66 69 6c 65 69 6e 66 6f 20 3d 3d 20 4e f (fileinfo == N
3de0: 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65 ULL) {...filed_e
3df0: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 rror_page(fp, da
3e00: 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 30 34 29 te_current, 404)
3e10: 3b 0a 0a 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 ;....log->http_c
3e20: 6f 64 65 20 3d 20 34 30 34 3b 0a 09 09 6c 6f 67 ode = 404;...log
3e30: 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 6f 70 65 6e ->reason = "open
3e40: 5f 66 61 69 6c 65 64 22 3b 0a 0a 09 09 66 69 6c _failed";....fil
3e50: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 ed_log_entry(log
3e60: 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 69 );..} else {...i
3e70: 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 f (request->head
3e80: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 ers.range.offset
3e90: 20 21 3d 20 30 20 7c 7c 20 72 65 71 75 65 73 74 != 0 || request
3ea0: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
3eb0: 6c 65 6e 67 74 68 20 3e 3d 20 30 29 20 7b 0a 09 length >= 0) {..
3ec0: 09 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 ..if (request->h
3ed0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
3ee0: 73 65 74 20 3e 3d 20 66 69 6c 65 69 6e 66 6f 2d set >= fileinfo-
3ef0: 3e 6c 65 6e 29 20 7b 0a 09 09 09 09 66 69 6c 65 >len) {.....file
3f00: 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c d_error_page(fp,
3f10: 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 date_current, 4
3f20: 31 36 29 3b 0a 0a 09 09 09 09 6c 6f 67 2d 3e 68 16);......log->h
3f30: 74 74 70 5f 63 6f 64 65 20 3d 20 34 31 36 3b 0a ttp_code = 416;.
3f40: 09 09 09 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 ....log->reason
3f50: 3d 20 22 72 61 6e 67 65 5f 69 6e 76 61 6c 69 64 = "range_invalid
3f60: 22 3b 0a 0a 09 09 09 09 66 69 6c 65 64 5f 6c 6f ";......filed_lo
3f70: 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 09 09 g_entry(log);...
3f80: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 69 66 .} else {.....if
3f90: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 (request->heade
3fa0: 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 rs.range.length
3fb0: 3d 3d 20 28 28 6f 66 66 5f 74 29 20 2d 31 29 29 == ((off_t) -1))
3fc0: 20 7b 0a 09 09 09 09 09 66 69 6c 65 64 5f 6c 6f {......filed_lo
3fd0: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d g_msg_debug("Com
3fe0: 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 74 6f puting length to
3ff0: 20 66 69 74 20 69 6e 20 62 6f 75 6e 64 73 3a 20 fit in bounds:
4000: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20 3d 20 fileinfo->len =
4010: 25 6c 6c 75 2c 20 72 65 71 75 65 73 74 2d 3e 68 %llu, request->h
4020: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
4030: 73 65 74 20 3d 20 25 6c 6c 75 22 2c 0a 09 09 09 set = %llu",....
4040: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
4050: 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f g long) fileinfo
4060: 2d 3e 6c 65 6e 2c 0a 09 09 09 09 09 09 28 75 6e ->len,.......(un
4070: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
4080: 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 ) request->heade
4090: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 0a rs.range.offset.
40a0: 09 09 09 09 09 29 3b 0a 0a 09 09 09 09 09 72 65 .....);.......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 6c 65 6e 67 74 68 20 3d 20 66 69 ange.length = fi
40d0: 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20 2d 20 72 65 leinfo->len - re
40e0: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
40f0: 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 09 09 ange.offset;....
4100: 09 7d 0a 0a 09 09 09 09 66 69 6c 65 64 5f 6c 6f .}......filed_lo
4110: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 50 61 72 g_msg_debug("Par
4120: 74 69 61 6c 20 72 65 71 75 65 73 74 2c 20 73 74 tial request, st
4130: 61 72 74 69 6e 67 20 61 74 3a 20 25 6c 6c 75 20 arting at: %llu
4140: 61 6e 64 20 72 75 6e 6e 69 6e 67 20 66 6f 72 20 and running for
4150: 25 6c 6c 69 20 62 79 74 65 73 22 2c 0a 09 09 09 %lli bytes",....
4160: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
4170: 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e long) request->
4180: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 headers.range.of
4190: 66 73 65 74 2c 0a 09 09 09 09 09 28 6c 6f 6e 67 fset,......(long
41a0: 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e long) request->
41b0: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 headers.range.le
41c0: 6e 67 74 68 0a 09 09 09 09 29 3b 0a 0a 09 09 09 ngth.....);.....
41d0: 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 36 .http_code = 206
41e0: 3b 0a 09 09 09 7d 0a 09 09 7d 20 65 6c 73 65 20 ;....}...} else
41f0: 7b 0a 09 09 09 69 66 20 28 72 65 71 75 65 73 74 {....if (request
4200: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
4210: 70 72 65 73 65 6e 74 29 20 7b 0a 09 09 09 09 68 present) {.....h
4220: 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 36 3b 0a ttp_code = 206;.
4230: 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 ...} else {.....
4240: 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 30 3b http_code = 200;
4250: 0a 09 09 09 7d 0a 09 09 09 72 65 71 75 65 73 74 ....}....request
4260: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
4270: 6f 66 66 73 65 74 20 3d 20 30 3b 0a 09 09 09 72 offset = 0;....r
4280: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
4290: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20 66 range.length = f
42a0: 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 09 09 ileinfo->len;...
42b0: 7d 0a 0a 09 09 69 66 20 28 68 74 74 70 5f 63 6f }....if (http_co
42c0: 64 65 20 3e 20 30 29 20 7b 0a 09 09 09 66 70 72 de > 0) {....fpr
42d0: 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f 31 intf(fp, "HTTP/1
42e0: 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 .1 %i OK\r\nDate
42f0: 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 : %s\r\nServer:
4300: 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f filed\r\nLast-Mo
4310: 64 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f dified: %s\r\nCo
4320: 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 6c ntent-Length: %l
4330: 6c 75 5c 72 5c 6e 41 63 63 65 70 74 2d 52 61 6e lu\r\nAccept-Ran
4340: 67 65 73 3a 20 62 79 74 65 73 5c 72 5c 6e 43 6f ges: bytes\r\nCo
4350: 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 ntent-Type: %s\r
4360: 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c \nConnection: cl
4370: 6f 73 65 5c 72 5c 6e 22 2c 0a 09 09 09 09 68 74 ose\r\n",.....ht
4380: 74 70 5f 63 6f 64 65 2c 0a 09 09 09 09 64 61 74 tp_code,.....dat
4390: 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 09 09 66 e_current,.....f
43a0: 69 6c 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d 6f 64 ileinfo->lastmod
43b0: 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ,.....(unsigned
43c0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 long long) reque
43d0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
43e0: 65 2e 6c 65 6e 67 74 68 2c 0a 09 09 09 09 66 69 e.length,.....fi
43f0: 6c 65 69 6e 66 6f 2d 3e 74 79 70 65 0a 09 09 09 leinfo->type....
4400: 29 3b 0a 09 09 09 69 66 20 28 68 74 74 70 5f 63 );....if (http_c
4410: 6f 64 65 20 3d 3d 20 32 30 36 29 20 7b 0a 09 09 ode == 206) {...
4420: 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 43 ..fprintf(fp, "C
4430: 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20 62 79 ontent-Range: by
4440: 74 65 73 20 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c tes %llu-%llu/%l
4450: 6c 75 5c 72 5c 6e 22 2c 0a 09 09 09 09 09 28 75 lu\r\n",......(u
4460: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
4470: 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 g) request->head
4480: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 ers.range.offset
4490: 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 ,......(unsigned
44a0: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 28 72 65 71 long long) (req
44b0: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
44c0: 6e 67 65 2e 6f 66 66 73 65 74 20 2b 20 72 65 71 nge.offset + req
44d0: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
44e0: 6e 67 65 2e 6c 65 6e 67 74 68 20 2d 20 31 29 2c nge.length - 1),
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 66 69 6c 65 69 long long) filei
4510: 6e 66 6f 2d 3e 6c 65 6e 0a 09 09 09 09 29 3b 0a nfo->len.....);.
4520: 09 09 09 7d 0a 09 09 09 66 70 72 69 6e 74 66 28 ...}....fprintf(
4530: 66 70 2c 20 22 5c 72 5c 6e 22 29 3b 0a 09 09 09 fp, "\r\n");....
4540: 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 09 09 fflush(fp);.....
4550: 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d log->http_code =
4560: 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 09 09 6c http_code;....l
4570: 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 4f 4b og->reason = "OK
4580: 22 3b 0a 09 09 09 6c 6f 67 2d 3e 73 74 61 72 74 ";....log->start
4590: 74 69 6d 65 20 3d 20 74 69 6d 65 28 4e 55 4c 4c time = time(NULL
45a0: 29 3b 0a 09 09 09 6c 6f 67 2d 3e 72 65 71 5f 6f );....log->req_o
45b0: 66 66 73 65 74 20 3d 20 72 65 71 75 65 73 74 2d ffset = request-
45c0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f >headers.range.o
45d0: 66 66 73 65 74 3b 0a 09 09 09 6c 6f 67 2d 3e 72 ffset;....log->r
45e0: 65 71 5f 6c 65 6e 67 74 68 20 3d 20 72 65 71 75 eq_length = requ
45f0: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e est->headers.ran
4600: 67 65 2e 6c 65 6e 67 74 68 3b 0a 09 09 09 6c 6f ge.length;....lo
4610: 67 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 68 20 3d g->file_length =
4620: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a fileinfo->len;.
4630: 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f 4e 4f .#ifdef FILED_NO
4640: 4e 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 09 09 69 NBLOCK_HTTP....i
4650: 6e 74 20 73 6f 63 6b 65 74 5f 66 6c 61 67 73 3b nt socket_flags;
4660: 0a 09 09 09 66 64 5f 73 65 74 20 72 66 64 2c 20 ....fd_set rfd,
4670: 77 66 64 3b 0a 09 09 09 63 68 61 72 20 73 69 6e wfd;....char sin
4680: 6b 62 75 66 5b 38 31 39 32 5d 3b 0a 09 09 09 73 kbuf[8192];....s
4690: 73 69 7a 65 5f 74 20 72 65 61 64 5f 72 65 74 3b size_t read_ret;
46a0: 0a 0a 09 09 09 46 44 5f 5a 45 52 4f 28 26 72 66 .....FD_ZERO(&rf
46b0: 64 29 3b 0a 09 09 09 46 44 5f 5a 45 52 4f 28 26 d);....FD_ZERO(&
46c0: 77 66 64 29 3b 0a 09 09 09 46 44 5f 53 45 54 28 wfd);....FD_SET(
46d0: 66 64 2c 20 26 72 66 64 29 3b 0a 09 09 09 46 44 fd, &rfd);....FD
46e0: 5f 53 45 54 28 66 64 2c 20 26 77 66 64 29 3b 0a _SET(fd, &wfd);.
46f0: 0a 09 09 09 73 6f 63 6b 65 74 5f 66 6c 61 67 73 ....socket_flags
4700: 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 = fcntl(fd, F_G
4710: 45 54 46 4c 29 3b 0a 09 09 09 66 63 6e 74 6c 28 ETFL);....fcntl(
4720: 66 64 2c 20 46 5f 53 45 54 46 4c 2c 20 73 6f 63 fd, F_SETFL, soc
4730: 6b 65 74 5f 66 6c 61 67 73 20 7c 20 4f 5f 4e 4f ket_flags | O_NO
4740: 4e 42 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 0a NBLOCK);.#endif.
4750: 0a 09 09 09 73 65 6e 64 66 69 6c 65 5f 6f 66 66 ....sendfile_off
4760: 73 65 74 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 set = request->h
4770: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
4780: 73 65 74 3b 0a 09 09 09 73 65 6e 64 66 69 6c 65 set;....sendfile
4790: 5f 6c 65 6e 20 3d 20 72 65 71 75 65 73 74 2d 3e _len = request->
47a0: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 headers.range.le
47b0: 6e 67 74 68 3b 0a 09 09 09 73 65 6e 64 66 69 6c ngth;....sendfil
47c0: 65 5f 73 65 6e 74 20 3d 20 30 3b 0a 09 09 09 77 e_sent = 0;....w
47d0: 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 09 09 69 hile (1) {.....i
47e0: 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 f (sendfile_len
47f0: 3e 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 > FILED_SENDFILE
4800: 5f 4d 41 58 29 20 7b 0a 09 09 09 09 09 73 65 6e _MAX) {......sen
4810: 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 46 49 4c dfile_size = FIL
4820: 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41 58 3b ED_SENDFILE_MAX;
4830: 0a 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 .....} else {...
4840: 09 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 ...sendfile_size
4850: 20 3d 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 3b = sendfile_len;
4860: 0a 09 09 09 09 7d 0a 0a 09 09 09 09 73 65 6e 64 .....}......send
4870: 66 69 6c 65 5f 72 65 74 20 3d 20 73 65 6e 64 66 file_ret = sendf
4880: 69 6c 65 28 66 64 2c 20 66 69 6c 65 69 6e 66 6f ile(fd, fileinfo
4890: 2d 3e 66 64 2c 20 26 73 65 6e 64 66 69 6c 65 5f ->fd, &sendfile_
48a0: 6f 66 66 73 65 74 2c 20 73 65 6e 64 66 69 6c 65 offset, sendfile
48b0: 5f 73 69 7a 65 29 3b 0a 09 09 09 09 69 66 20 28 _size);.....if (
48c0: 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3c 3d 20 sendfile_ret <=
48d0: 30 29 20 7b 0a 23 69 66 64 65 66 20 46 49 4c 45 0) {.#ifdef FILE
48e0: 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50 0a D_NONBLOCK_HTTP.
48f0: 09 09 09 09 09 69 66 20 28 65 72 72 6e 6f 20 3d .....if (errno =
4900: 3d 20 45 41 47 41 49 4e 29 20 7b 0a 09 09 09 09 = EAGAIN) {.....
4910: 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d ..sendfile_ret =
4920: 20 30 3b 0a 0a 09 09 09 09 09 09 77 68 69 6c 65 0;........while
4930: 20 28 31 29 20 7b 0a 09 09 09 09 09 09 09 73 65 (1) {........se
4940: 6c 65 63 74 28 66 64 20 2b 20 31 2c 20 26 72 66 lect(fd + 1, &rf
4950: 64 2c 20 26 77 66 64 2c 20 4e 55 4c 4c 2c 20 4e d, &wfd, NULL, N
4960: 55 4c 4c 29 3b 0a 09 09 09 09 09 09 09 69 66 20 ULL);........if
4970: 28 46 44 5f 49 53 53 45 54 28 66 64 2c 20 26 72 (FD_ISSET(fd, &r
4980: 66 64 29 29 20 7b 0a 09 09 09 09 09 09 09 09 72 fd)) {.........r
4990: 65 61 64 5f 72 65 74 20 3d 20 72 65 61 64 28 66 ead_ret = read(f
49a0: 64 2c 20 73 69 6e 6b 62 75 66 2c 20 73 69 7a 65 d, sinkbuf, size
49b0: 6f 66 28 73 69 6e 6b 62 75 66 29 29 3b 0a 0a 09 of(sinkbuf));...
49c0: 09 09 09 09 09 09 09 69 66 20 28 72 65 61 64 5f .......if (read_
49d0: 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 ret <= 0) {.....
49e0: 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 .....break;.....
49f0: 09 09 09 09 7d 0a 09 09 09 09 09 09 09 7d 0a 0a ....}........}..
4a00: 09 09 09 09 09 09 09 69 66 20 28 46 44 5f 49 53 .......if (FD_IS
4a10: 53 45 54 28 66 64 2c 20 26 77 66 64 29 29 20 7b SET(fd, &wfd)) {
4a20: 0a 09 09 09 09 09 09 09 09 72 65 61 64 5f 72 65 .........read_re
4a30: 74 20 3d 20 31 3b 0a 0a 09 09 09 09 09 09 09 09 t = 1;..........
4a40: 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 09 7d 0a break;........}.
4a50: 09 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 09 69 ......}........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 62 72 65 61 6b ) {........break
4a80: 3b 0a 09 09 09 09 09 09 7d 0a 09 09 09 09 09 7d ;.......}......}
4a90: 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 09 62 72 else {.......br
4aa0: 65 61 6b 3b 0a 09 09 09 09 09 7d 0a 23 65 6c 73 eak;......}.#els
4ab0: 65 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 23 65 e......break;.#e
4ac0: 6e 64 69 66 0a 09 09 09 09 7d 0a 0a 09 09 09 09 ndif.....}......
4ad0: 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 2d 3d 20 sendfile_len -=
4ae0: 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 09 sendfile_ret;...
4af0: 09 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 ..sendfile_sent
4b00: 2b 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b += sendfile_ret;
4b10: 0a 09 09 09 09 69 66 20 28 73 65 6e 64 66 69 6c .....if (sendfil
4b20: 65 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 e_len == 0) {...
4b30: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 7d 0a ...break;.....}.
4b40: 09 09 09 7d 0a 0a 09 09 09 6c 6f 67 2d 3e 65 6e ...}.....log->en
4b50: 64 74 69 6d 65 20 3d 20 28 74 69 6d 65 5f 74 29 dtime = (time_t)
4b60: 20 2d 31 3b 0a 09 09 09 6c 6f 67 2d 3e 73 65 6e -1;....log->sen
4b70: 74 5f 6c 65 6e 67 74 68 20 3d 20 73 65 6e 64 66 t_length = sendf
4b80: 69 6c 65 5f 73 65 6e 74 3b 0a 0a 09 09 09 66 69 ile_sent;.....fi
4b90: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f led_log_entry(lo
4ba0: 67 29 3b 0a 09 09 7d 0a 0a 09 09 63 6c 6f 73 65 g);...}....close
4bb0: 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a (fileinfo->fd);.
4bc0: 09 7d 0a 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b .}...fclose(fp);
4bd0: 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a ...return;.}../*
4be0: 20 48 61 6e 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 Handle incoming
4bf0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a connections */.
4c00: 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 6c static void *fil
4c10: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 ed_worker_thread
4c20: 28 76 6f 69 64 20 2a 61 72 67 5f 76 29 20 7b 0a (void *arg_v) {.
4c30: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 6f .struct filed_wo
4c40: 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 73 rker_thread_args
4c50: 20 2a 61 72 67 3b 0a 09 73 74 72 75 63 74 20 66 *arg;..struct f
4c60: 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 iled_http_reques
4c70: 74 20 72 65 71 75 65 73 74 3b 0a 09 73 74 72 75 t request;..stru
4c80: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ct filed_log_ent
4c90: 72 79 20 2a 6c 6f 67 2c 20 6c 6f 63 61 6c 5f 64 ry *log, local_d
4ca0: 75 6d 6d 79 5f 6c 6f 67 3b 0a 09 73 74 72 75 63 ummy_log;..struc
4cb0: 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 t sockaddr_in6 a
4cc0: 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 ddr;..socklen_t
4cd0: 61 64 64 72 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 addrlen;..int fa
4ce0: 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20 30 2c ilure_count = 0,
4cf0: 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 max_failure_cou
4d00: 6e 74 20 3d 20 46 49 4c 45 44 5f 4d 41 58 5f 46 nt = FILED_MAX_F
4d10: 41 49 4c 55 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 AILURE_COUNT;..i
4d20: 6e 74 20 6d 61 73 74 65 72 5f 66 64 2c 20 66 64 nt master_fd, fd
4d30: 3b 0a 0a 09 2f 2a 20 52 65 61 64 20 61 72 67 75 ;.../* Read argu
4d40: 6d 65 6e 74 73 20 2a 2f 0a 09 61 72 67 20 3d 20 ments */..arg =
4d50: 61 72 67 5f 76 3b 0a 0a 09 6d 61 73 74 65 72 5f arg_v;...master_
4d60: 66 64 20 3d 20 61 72 67 2d 3e 66 64 3b 0a 0a 09 fd = arg->fd;...
4d70: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 2f 2a while (1) {.../*
4d80: 20 46 61 69 6c 75 72 65 20 6c 6f 6f 70 20 70 72 Failure loop pr
4d90: 65 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 evention */...if
4da0: 20 28 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 (failure_count
4db0: 3e 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f > max_failure_co
4dc0: 75 6e 74 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b unt) {....break;
4dd0: 0a 09 09 7d 0a 0a 09 09 2f 2a 20 41 63 63 65 70 ...}..../* Accep
4de0: 74 20 61 20 6e 65 77 20 63 6c 69 65 6e 74 20 2a t a new client *
4df0: 2f 0a 09 09 61 64 64 72 6c 65 6e 20 3d 20 73 69 /...addrlen = si
4e00: 7a 65 6f 66 28 61 64 64 72 29 3b 0a 09 09 66 64 zeof(addr);...fd
4e10: 20 3d 20 61 63 63 65 70 74 28 6d 61 73 74 65 72 = accept(master
4e20: 5f 66 64 2c 20 28 73 74 72 75 63 74 20 73 6f 63 _fd, (struct soc
4e30: 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 2c 20 kaddr *) &addr,
4e40: 26 61 64 64 72 6c 65 6e 29 3b 0a 0a 09 09 2f 2a &addrlen);..../*
4e50: 0a 09 09 20 2a 20 49 66 20 77 65 20 66 61 69 6c ... * If we fail
4e60: 2c 20 6d 61 6b 65 20 61 20 6e 6f 74 65 20 6f 66 , make a note of
4e70: 20 69 74 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 it so we don't
4e80: 67 6f 20 69 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f go into a loop o
4e90: 66 0a 09 09 20 2a 20 61 63 63 65 70 74 28 29 20 f... * accept()
4ea0: 66 61 69 6c 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 failing... */...
4eb0: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 if (fd < 0) {...
4ec0: 09 2f 2a 20 4c 6f 67 20 74 68 65 20 6e 65 77 20 ./* Log the new
4ed0: 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 connection */...
4ee0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 .filed_log_msg("
4ef0: 41 43 43 45 50 54 5f 46 41 49 4c 45 44 22 29 3b ACCEPT_FAILED");
4f00: 0a 0a 09 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 .....failure_cou
4f10: 6e 74 2b 2b 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e nt++;.....contin
4f20: 75 65 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 4c 6f ue;...}..../* Lo
4f30: 67 20 74 68 65 20 6e 65 77 20 63 6f 6e 6e 65 63 g the new connec
4f40: 74 69 6f 6e 20 2a 2f 0a 09 09 6c 6f 67 20 3d 20 tion */...log =
4f50: 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 31 29 filed_log_new(1)
4f60: 3b 0a 09 09 69 66 20 28 6c 6f 67 20 3d 3d 20 4e ;...if (log == N
4f70: 55 4c 4c 29 20 7b 0a 09 09 09 63 6c 6f 73 65 28 ULL) {....close(
4f80: 66 64 29 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e 75 fd);.....continu
4f90: 65 3b 0a 09 09 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 e;...}....log->t
4fa0: 79 70 65 20 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f ype = FILED_LOG_
4fb0: 54 59 50 45 5f 54 52 41 4e 53 46 45 52 3b 0a 09 TYPE_TRANSFER;..
4fc0: 09 69 66 20 28 66 69 6c 65 64 5f 6c 6f 67 5f 69 .if (filed_log_i
4fd0: 70 28 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 p((struct sockad
4fe0: 64 72 20 2a 29 20 26 61 64 64 72 2c 20 6c 6f 67 dr *) &addr, log
4ff0: 2d 3e 69 70 2c 20 73 69 7a 65 6f 66 28 6c 6f 67 ->ip, sizeof(log
5000: 2d 3e 69 70 29 29 20 3d 3d 20 4e 55 4c 4c 29 20 ->ip)) == NULL)
5010: 7b 0a 09 09 09 6c 6f 67 2d 3e 69 70 5b 30 5d 20 {....log->ip[0]
5020: 3d 20 27 5c 30 27 3b 0a 09 09 7d 0a 09 09 6c 6f = '\0';...}...lo
5030: 67 2d 3e 70 6f 72 74 20 3d 20 61 64 64 72 2e 73 g->port = addr.s
5040: 69 6e 36 5f 70 6f 72 74 3b 0a 0a 09 09 2f 2a 20 in6_port;..../*
5050: 52 65 73 65 74 20 66 61 69 6c 75 72 65 20 63 6f Reset failure co
5060: 75 6e 74 2a 2f 0a 09 09 66 61 69 6c 75 72 65 5f unt*/...failure_
5070: 63 6f 75 6e 74 20 3d 20 30 3b 0a 0a 09 09 2f 2a count = 0;..../*
5080: 20 48 61 6e 64 6c 65 20 73 6f 63 6b 65 74 20 2a Handle socket *
5090: 2f 0a 09 09 66 69 6c 65 64 5f 68 61 6e 64 6c 65 /...filed_handle
50a0: 5f 63 6c 69 65 6e 74 28 66 64 2c 20 26 72 65 71 _client(fd, &req
50b0: 75 65 73 74 2c 20 6c 6f 67 29 3b 0a 09 7d 0a 0a uest, log);..}..
50c0: 09 2f 2a 20 52 65 70 6f 72 74 20 65 72 72 6f 72 ./* Report error
50d0: 20 2a 2f 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d */..filed_log_m
50e0: 73 67 28 22 54 48 52 45 41 44 5f 44 49 45 44 20 sg("THREAD_DIED
50f0: 41 42 4e 4f 52 4d 41 4c 22 29 3b 0a 0a 09 72 65 ABNORMAL");...re
5100: 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a turn(NULL);.../*
5110: 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 local_dummy_log
5120: 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 is only used if
5130: 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 20 FILED_DONT_LOG
5140: 69 73 20 61 6e 65 62 6c 65 64 2c 20 6f 74 68 65 is anebled, othe
5150: 72 77 69 73 65 20 69 74 27 73 20 6e 6f 74 20 75 rwise it's not u
5160: 73 65 64 2c 20 62 75 74 20 74 68 65 20 63 6f 6d sed, but the com
5170: 70 69 6c 65 72 20 68 61 74 65 73 20 74 68 61 74 piler hates that
5180: 20 69 64 65 61 2e 20 2a 2f 0a 09 6c 6f 63 61 6c idea. */..local
5190: 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 20 _dummy_log.type
51a0: 3d 20 30 3b 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d = 0;..local_dumm
51b0: 79 5f 6c 6f 67 2e 74 79 70 65 20 3d 20 6c 6f 63 y_log.type = loc
51c0: 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 al_dummy_log.typ
51d0: 65 3b 0a 7d 0a 0a 2f 2a 20 43 72 65 61 74 65 20 e;.}../* Create
51e0: 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a worker threads *
51f0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c /.static int fil
5200: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 ed_worker_thread
5210: 73 5f 69 6e 69 74 28 69 6e 74 20 66 64 2c 20 69 s_init(int fd, i
5220: 6e 74 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 29 nt thread_count)
5230: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 {..struct filed
5240: 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 _worker_thread_a
5250: 72 67 73 20 2a 61 72 67 3b 0a 09 70 74 68 72 65 rgs *arg;..pthre
5260: 61 64 5f 74 20 74 68 72 65 61 64 69 64 3b 0a 09 ad_t threadid;..
5270: 69 6e 74 20 70 74 68 72 65 61 64 5f 72 65 74 3b int pthread_ret;
5280: 0a 09 69 6e 74 20 69 3b 0a 0a 09 66 6f 72 20 28 ..int i;...for (
5290: 69 20 3d 20 30 3b 20 69 20 3c 20 74 68 72 65 61 i = 0; i < threa
52a0: 64 5f 63 6f 75 6e 74 3b 20 69 2b 2b 29 20 7b 0a d_count; i++) {.
52b0: 09 09 61 72 67 20 3d 20 6d 61 6c 6c 6f 63 28 73 ..arg = malloc(s
52c0: 69 7a 65 6f 66 28 2a 61 72 67 29 29 3b 0a 0a 09 izeof(*arg));...
52d0: 09 61 72 67 2d 3e 66 64 20 3d 20 66 64 3b 0a 0a .arg->fd = fd;..
52e0: 09 09 70 74 68 72 65 61 64 5f 72 65 74 20 3d 20 ..pthread_ret =
52f0: 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 pthread_create(&
5300: 74 68 72 65 61 64 69 64 2c 20 4e 55 4c 4c 2c 20 threadid, NULL,
5310: 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 filed_worker_thr
5320: 65 61 64 2c 20 61 72 67 29 3b 0a 09 09 69 66 20 ead, arg);...if
5330: 28 70 74 68 72 65 61 64 5f 72 65 74 20 21 3d 20 (pthread_ret !=
5340: 30 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 0) {....return(-
5350: 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 1);...}..}...ret
5360: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 urn(0);.}../* Di
5370: 73 70 6c 61 79 20 68 65 6c 70 20 2a 2f 0a 73 74 splay help */.st
5380: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f atic void filed_
5390: 70 72 69 6e 74 5f 68 65 6c 70 28 46 49 4c 45 20 print_help(FILE
53a0: 2a 6f 75 74 70 75 74 2c 20 69 6e 74 20 6c 6f 6e *output, int lon
53b0: 67 5f 68 65 6c 70 2c 20 63 6f 6e 73 74 20 63 68 g_help, const ch
53c0: 61 72 20 2a 65 78 74 72 61 29 20 7b 0a 09 69 66 ar *extra) {..if
53d0: 20 28 65 78 74 72 61 29 20 7b 0a 09 09 66 70 72 (extra) {...fpr
53e0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 25 73 intf(output, "%s
53f0: 5c 6e 22 2c 20 65 78 74 72 61 29 3b 0a 09 7d 0a \n", extra);..}.
5400: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
5410: 2c 20 22 55 73 61 67 65 3a 20 66 69 6c 65 64 20 , "Usage: filed
5420: 5b 3c 6f 70 74 69 6f 6e 73 3e 5d 5c 6e 22 29 3b [<options>]\n");
5430: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
5440: 2c 20 22 20 20 4f 70 74 69 6f 6e 73 3a 5c 6e 22 , " Options:\n"
5450: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
5460: 75 74 2c 20 22 20 20 20 20 20 20 2d 68 2c 20 2d ut, " -h, -
5470: 2d 68 65 6c 70 5c 6e 22 29 3b 0a 09 66 70 72 69 -help\n");..fpri
5480: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
5490: 20 20 20 2d 64 2c 20 2d 2d 64 61 65 6d 6f 6e 5c -d, --daemon\
54a0: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
54b0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 2c tput, " -v,
54c0: 20 2d 2d 76 65 72 73 69 6f 6e 5c 6e 22 29 3b 0a --version\n");.
54d0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
54e0: 20 22 20 20 20 20 20 20 2d 62 20 3c 61 64 64 72 " -b <addr
54f0: 65 73 73 3e 2c 20 2d 2d 62 69 6e 64 20 3c 61 64 ess>, --bind <ad
5500: 64 72 65 73 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 dress>\n");..fpr
5510: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
5520: 20 20 20 20 2d 70 20 3c 70 6f 72 74 3e 2c 20 2d -p <port>, -
5530: 2d 70 6f 72 74 20 3c 70 6f 72 74 3e 5c 6e 22 29 -port <port>\n")
5540: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
5550: 74 2c 20 22 20 20 20 20 20 20 2d 74 20 3c 63 6f t, " -t <co
5560: 75 6e 74 3e 2c 20 2d 2d 74 68 72 65 61 64 73 20 unt>, --threads
5570: 3c 63 6f 75 6e 74 3e 5c 6e 22 29 3b 0a 09 66 70 <count>\n");..fp
5580: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
5590: 20 20 20 20 20 2d 63 20 3c 65 6e 74 72 69 65 73 -c <entries
55a0: 3e 2c 20 2d 2d 63 61 63 68 65 20 3c 65 6e 74 72 >, --cache <entr
55b0: 69 65 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e ies>\n");..fprin
55c0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
55d0: 20 20 2d 6c 20 3c 66 69 6c 65 3e 2c 20 2d 2d 6c -l <file>, --l
55e0: 6f 67 20 3c 66 69 6c 65 3e 5c 6e 22 29 3b 0a 09 og <file>\n");..
55f0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5600: 22 20 20 20 20 20 20 2d 75 20 3c 75 73 65 72 3e " -u <user>
5610: 2c 20 2d 2d 75 73 65 72 20 3c 75 73 65 72 3e 5c , --user <user>\
5620: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
5630: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 72 20 tput, " -r
5640: 3c 64 69 72 65 63 74 6f 72 79 3e 2c 20 2d 2d 72 <directory>, --r
5650: 6f 6f 74 20 3c 64 69 72 65 63 74 6f 72 79 3e 5c oot <directory>\
5660: 6e 22 29 3b 0a 0a 09 69 66 20 28 6c 6f 6e 67 5f n");...if (long_
5670: 68 65 6c 70 29 20 7b 0a 09 09 66 70 72 69 6e 74 help) {...fprint
5680: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
5690: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
56a0: 74 2c 20 22 20 20 55 73 61 67 65 3a 5c 6e 22 29 t, " Usage:\n")
56b0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
56c0: 75 74 2c 20 22 20 20 20 20 20 20 2d 68 20 28 6f ut, " -h (o
56d0: 72 20 2d 2d 68 65 6c 70 29 20 70 72 69 6e 74 73 r --help) prints
56e0: 20 74 68 69 73 20 75 73 61 67 65 20 69 6e 66 6f this usage info
56f0: 72 6d 61 74 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 rmation.\n");...
5700: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5710: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
5720: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
5730: 2d 64 20 28 6f 72 20 2d 2d 64 61 65 6d 6f 6e 29 -d (or --daemon)
5740: 20 69 6e 73 74 72 75 63 74 73 20 66 69 6c 65 64 instructs filed
5750: 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 64 61 65 to become a dae
5760: 6d 6f 6e 20 61 66 74 65 72 20 69 6e 69 74 69 61 mon after initia
5770: 6c 69 7a 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 lizing\n");...fp
5780: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
5790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57a0: 20 20 20 20 20 20 74 68 65 20 6c 69 73 74 65 6e the listen
57b0: 69 6e 67 20 54 43 50 20 73 6f 63 6b 65 74 20 61 ing TCP socket a
57c0: 6e 64 20 6c 6f 67 20 66 69 6c 65 73 2e 5c 6e 22 nd log files.\n"
57d0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
57e0: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 put, "\n");...fp
57f0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
5800: 20 20 20 20 20 2d 76 20 28 6f 72 20 2d 2d 76 65 -v (or --ve
5810: 72 73 69 6f 6e 29 20 69 6e 73 74 72 75 63 74 73 rsion) instructs
5820: 20 66 69 6c 65 64 20 70 72 69 6e 74 20 6f 75 74 filed print out
5830: 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d the version num
5840: 62 65 72 20 61 6e 64 20 65 78 69 74 2e 5c 6e 22 ber and exit.\n"
5850: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
5860: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 put, "\n");...fp
5870: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
5880: 20 20 20 20 20 2d 62 20 28 6f 72 20 2d 2d 62 69 -b (or --bi
5890: 6e 64 29 20 73 70 65 63 69 66 69 65 73 20 74 68 nd) specifies th
58a0: 65 20 61 64 64 72 65 73 73 20 74 6f 20 6c 69 73 e address to lis
58b0: 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 ten for incoming
58c0: 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 HTTP\n");...fpr
58d0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
58e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58f0: 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 requests on.
5900: 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c The default val
5910: 75 65 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 ue is \"%s\".\n"
5920: 2c 20 42 49 4e 44 5f 41 44 44 52 29 3b 0a 09 09 , BIND_ADDR);...
5930: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5940: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
5950: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
5960: 2d 70 20 28 6f 72 20 2d 2d 70 6f 72 74 29 20 73 -p (or --port) s
5970: 70 65 63 69 66 69 65 73 20 74 68 65 20 54 43 50 pecifies the TCP
5980: 20 70 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f 20 port number to
5990: 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d listen for incom
59a0: 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 ing HTTP\n");...
59b0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
59c0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
59d0: 20 20 20 20 20 20 72 65 71 75 65 73 74 73 20 6f requests o
59e0: 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 n. The default
59f0: 69 73 20 25 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 is %u.\n", (unsi
5a00: 67 6e 65 64 20 69 6e 74 29 20 50 4f 52 54 29 3b gned int) PORT);
5a10: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
5a20: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t, "\n");...fpri
5a30: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
5a40: 20 20 20 2d 74 20 28 6f 72 20 2d 2d 74 68 72 65 -t (or --thre
5a50: 61 64 73 29 20 73 70 65 63 69 66 69 65 73 20 74 ads) specifies t
5a60: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 6f 72 he number of wor
5a70: 6b 65 72 20 74 68 72 65 61 64 73 20 74 6f 20 63 ker threads to c
5a80: 72 65 61 74 65 2e 20 45 61 63 68 5c 6e 22 29 3b reate. Each\n");
5a90: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
5aa0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
5ab0: 20 20 20 20 20 20 20 20 20 20 20 20 77 6f 72 6b work
5ac0: 65 72 20 74 68 72 65 61 64 20 63 61 6e 20 73 65 er thread can se
5ad0: 72 76 69 63 65 20 6f 6e 65 20 63 6f 6e 63 75 72 rvice one concur
5ae0: 72 65 6e 74 20 48 54 54 50 20 73 65 73 73 69 6f rent HTTP sessio
5af0: 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 n.\n");...fprint
5b00: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
5b10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b20: 20 20 20 54 68 75 73 20 74 68 65 20 6e 75 6d 62 Thus the numb
5b30: 65 72 20 6f 66 20 74 68 72 65 61 64 73 20 63 72 er of threads cr
5b40: 65 61 74 65 64 20 77 69 6c 6c 20 64 65 74 65 72 eated will deter
5b50: 6d 69 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a 09 09 mine how\n");...
5b60: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5b70: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
5b80: 20 20 20 20 20 20 20 20 20 6d 61 6e 79 20 73 69 many si
5b90: 6d 75 6c 74 61 6e 65 6f 75 73 20 74 72 61 6e 73 multaneous trans
5ba0: 66 65 72 73 20 77 69 6c 6c 20 62 65 20 70 6f 73 fers will be pos
5bb0: 73 69 62 6c 65 2e 20 54 68 65 5c 6e 22 29 3b 0a sible. The\n");.
5bc0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
5bd0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
5be0: 20 20 20 20 20 20 20 20 20 20 20 64 65 66 61 75 defau
5bf0: 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 lt is %lu.\n", (
5c00: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 54 unsigned long) T
5c10: 48 52 45 41 44 5f 43 4f 55 4e 54 29 3b 0a 09 09 HREAD_COUNT);...
5c20: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5c30: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
5c40: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
5c50: 2d 63 20 28 6f 72 20 2d 2d 63 61 63 68 65 29 20 -c (or --cache)
5c60: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 specifies the nu
5c70: 6d 62 65 72 20 6f 66 20 66 69 6c 65 20 69 6e 66 mber of file inf
5c80: 6f 72 6d 61 74 69 6f 6e 20 63 61 63 68 65 20 65 ormation cache e
5c90: 6e 74 72 69 65 73 5c 6e 22 29 3b 0a 09 09 66 70 ntries\n");...fp
5ca0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
5cb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cc0: 20 20 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74 65 to allocate
5cd0: 2e 20 20 45 61 63 68 20 63 61 63 68 65 20 65 6e . Each cache en
5ce0: 74 72 79 20 68 6f 6c 64 73 20 66 69 6c 65 20 69 try holds file i
5cf0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 5c 6e 22 nformation as\n"
5d00: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
5d10: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
5d20: 20 20 20 20 20 20 20 20 20 20 20 20 77 65 6c 6c well
5d30: 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 as an open file
5d40: 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74 descriptor to t
5d50: 68 65 20 66 69 6c 65 2c 20 73 6f 20 72 65 73 6f he file, so reso
5d60: 75 72 63 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 urce\n");...fpri
5d70: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
5d80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5d90: 20 20 20 6c 69 6d 69 74 73 20 28 69 2e 65 2e 2c limits (i.e.,
5da0: 20 75 6c 69 6d 69 74 29 20 73 68 6f 75 6c 64 20 ulimit) should
5db0: 62 65 20 63 6f 6e 73 69 64 65 72 65 64 2e 20 20 be considered.
5dc0: 54 68 69 73 20 73 68 6f 75 6c 64 5c 6e 22 29 3b This should\n");
5dd0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
5de0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
5df0: 20 20 20 20 20 20 20 20 20 20 62 65 20 61 20 70 be a p
5e00: 72 69 6d 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 rime number for
5e10: 69 64 65 61 6c 20 75 73 65 20 77 69 74 68 20 74 ideal use with t
5e20: 68 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 6f 64 he lookup method
5e30: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 .\n");...fprintf
5e40: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
5e50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e60: 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 The default is %
5e70: 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 lu.\n", (unsigne
5e80: 64 20 6c 6f 6e 67 29 20 43 41 43 48 45 5f 53 49 d long) CACHE_SI
5e90: 5a 45 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f ZE);...fprintf(o
5ea0: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
5eb0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
5ec0: 22 20 20 20 20 20 20 2d 6c 20 28 6f 72 20 2d 2d " -l (or --
5ed0: 6c 6f 67 29 20 73 70 65 63 69 66 69 65 73 20 61 log) specifies a
5ee0: 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 6f 70 65 filename to ope
5ef0: 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 20 6c 6f n for writing lo
5f00: 67 20 65 6e 74 72 69 65 73 2e 20 20 4c 6f 67 5c g entries. Log\
5f10: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
5f20: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
5f30: 20 20 20 20 20 20 20 20 20 20 20 20 65 6e 74 72 entr
5f40: 69 65 73 20 61 72 65 20 6d 61 64 65 20 66 6f 72 ies are made for
5f50: 20 76 61 72 69 6f 75 73 20 73 74 61 67 65 73 20 various stages
5f60: 69 6e 20 74 72 61 6e 73 66 65 72 69 6e 67 20 66 in transfering f
5f70: 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 iles.\n");...fpr
5f80: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
5f90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5fa0: 20 20 54 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 The log file i
5fb0: 73 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 s opened before
5fc0: 73 77 69 74 63 68 69 6e 67 20 75 73 65 72 73 20 switching users
5fd0: 28 73 65 65 20 5c 22 2d 75 5c 22 29 5c 6e 22 29 (see \"-u\")\n")
5fe0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
5ff0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
6000: 20 20 20 20 20 20 20 20 20 61 6e 64 20 72 6f 6f and roo
6010: 74 20 64 69 72 65 63 74 6f 72 69 65 73 20 28 73 t directories (s
6020: 65 65 20 5c 22 2d 72 5c 22 29 2e 20 20 54 68 65 ee \"-r\"). The
6030: 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e 65 76 log file is nev
6040: 65 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 er\n");...fprint
6050: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
6060: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
6070: 6c 6f 73 65 64 20 73 6f 20 6c 6f 67 20 72 6f 74 losed so log rot
6080: 61 74 69 6f 6e 20 77 69 74 68 6f 75 74 20 73 74 ation without st
6090: 6f 70 70 69 6e 67 20 74 68 65 20 64 61 65 6d 6f opping the daemo
60a0: 6e 20 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 n is will\n");..
60b0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
60c0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
60d0: 20 20 20 20 20 20 6e 6f 74 20 77 6f 72 6b 2e 20 not work.
60e0: 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 5c 22 The value of \"
60f0: 2d 5c 22 20 69 6e 64 69 63 61 74 65 73 20 74 68 -\" indicates th
6100: 61 74 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 at standard outp
6110: 75 74 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 ut\n");...fprint
6120: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
6130: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
6140: 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f hould be used fo
6150: 72 20 6c 6f 67 67 69 6e 67 2e 20 20 54 68 65 20 r logging. The
6160: 64 65 66 61 75 6c 74 20 69 73 20 5c 22 25 73 5c default is \"%s\
6170: 22 2e 5c 6e 22 2c 20 4c 4f 47 5f 46 49 4c 45 29 ".\n", LOG_FILE)
6180: 3b 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f 44 ;.#ifdef FILED_D
6190: 4f 4e 54 5f 4c 4f 47 0a 09 09 66 70 72 69 6e 74 ONT_LOG...fprint
61a0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
61b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e N
61c0: 6f 74 65 20 74 68 61 74 20 6c 6f 67 67 69 6e 67 ote that logging
61d0: 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 is completely d
61e0: 69 73 61 62 6c 65 64 20 73 6f 20 74 68 69 73 20 isabled so this
61f0: 6f 70 74 69 6f 6e 20 64 6f 65 73 5c 6e 22 29 3b option does\n");
6200: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
6210: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
6220: 20 20 20 20 20 20 20 20 6e 6f 74 68 69 6e 67 20 nothing
6230: 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2e 5c 6e in this build.\n
6240: 22 29 3b 0a 23 65 6e 64 69 66 0a 09 09 66 70 72 ");.#endif...fpr
6250: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e intf(output, "\n
6260: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
6270: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 75 20 tput, " -u
6280: 28 6f 72 20 2d 2d 75 73 65 72 29 20 73 70 65 63 (or --user) spec
6290: 69 66 69 65 73 20 74 68 65 20 75 73 65 72 20 74 ifies the user t
62a0: 6f 20 73 77 69 74 63 68 20 75 73 65 72 20 49 44 o switch user ID
62b0: 73 20 74 6f 20 62 65 66 6f 72 65 20 73 65 72 76 s to before serv
62c0: 69 63 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 icing\n");...fpr
62d0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
62e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62f0: 20 20 20 72 65 71 75 65 73 74 73 2e 20 20 54 68 requests. Th
6300: 65 20 64 65 66 61 75 6c 74 20 69 73 20 6e 6f 74 e default is not
6310: 20 63 68 61 6e 67 65 20 75 73 65 72 20 49 44 73 change user IDs
6320: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 .\n");...fprintf
6330: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a (output, "\n");.
6340: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
6350: 2c 20 22 20 20 20 20 20 20 2d 72 20 28 6f 72 20 , " -r (or
6360: 2d 2d 72 6f 6f 74 29 20 73 70 65 63 69 66 69 65 --root) specifie
6370: 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 s the directory
6380: 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72 6f to act as the ro
6390: 6f 74 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 ot directory for
63a0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
63b0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
63c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
63d0: 65 20 66 69 6c 65 20 73 65 72 76 65 72 2e 20 20 e file server.
63e0: 49 66 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 69 If this option i
63f0: 73 20 73 70 65 63 69 66 69 65 64 2c 20 63 68 72 s specified, chr
6400: 6f 6f 74 28 32 29 5c 6e 22 29 3b 0a 09 09 66 70 oot(2)\n");...fp
6410: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
6420: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6430: 20 20 20 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 is called.
6440: 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 6e The default is n
6450: 6f 74 20 63 68 61 6e 67 65 20 72 6f 6f 74 20 64 ot change root d
6460: 69 72 65 63 74 6f 72 69 65 73 2c 5c 6e 22 29 3b irectories,\n");
6470: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
6480: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
6490: 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 73 that is
64a0: 2c 20 74 68 65 20 5c 22 2f 5c 22 20 64 69 72 65 , the \"/\" dire
64b0: 63 74 6f 72 79 20 69 73 20 73 68 61 72 65 64 20 ctory is shared
64c0: 6f 75 74 2e 20 20 54 68 69 73 20 77 69 6c 6c 5c out. This will\
64d0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
64e0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
64f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69 6b lik
6500: 65 6c 79 20 62 65 20 61 20 73 65 63 75 72 69 74 ely be a securit
6510: 79 20 69 73 73 75 65 2c 20 73 6f 20 74 68 69 73 y issue, so this
6520: 20 6f 70 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 option should a
6530: 6c 77 61 79 73 5c 6e 22 29 3b 0a 09 09 66 70 72 lways\n");...fpr
6540: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
6550: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6560: 20 20 20 62 65 20 75 73 65 64 2e 5c 6e 22 29 3b be used.\n");
6570: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a ..}...return;.}.
6580: 0a 2f 2a 20 41 64 64 20 61 20 67 65 74 6f 70 74 ./* Add a getopt
6590: 20 6f 70 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 option */.stati
65a0: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 74 c void filed_get
65b0: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 opt_long_setopt(
65c0: 73 74 72 75 63 74 20 6f 70 74 69 6f 6e 20 2a 6f struct option *o
65d0: 70 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a pt, const char *
65e0: 6e 61 6d 65 2c 20 69 6e 74 20 68 61 73 5f 61 72 name, int has_ar
65f0: 67 2c 20 69 6e 74 20 76 61 6c 29 20 7b 0a 09 6f g, int val) {..o
6600: 70 74 2d 3e 6e 61 6d 65 20 20 20 20 20 3d 20 6e pt->name = n
6610: 61 6d 65 3b 0a 09 6f 70 74 2d 3e 68 61 73 5f 61 ame;..opt->has_a
6620: 72 67 20 20 3d 20 68 61 73 5f 61 72 67 3b 0a 09 rg = has_arg;..
6630: 6f 70 74 2d 3e 66 6c 61 67 20 20 20 20 20 3d 20 opt->flag =
6640: 4e 55 4c 4c 3b 0a 09 6f 70 74 2d 3e 76 61 6c 20 NULL;..opt->val
6650: 20 20 20 20 20 3d 20 76 61 6c 3b 0a 0a 09 72 65 = val;...re
6660: 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 73 6f turn;.}../* Reso
6670: 6c 76 65 20 61 20 75 73 65 72 6e 61 6d 65 20 74 lve a username t
6680: 6f 20 61 20 55 49 44 20 2a 2f 0a 73 74 61 74 69 o a UID */.stati
6690: 63 20 69 6e 74 20 66 69 6c 65 64 5f 75 73 65 72 c int filed_user
66a0: 5f 6c 6f 6f 6b 75 70 28 63 6f 6e 73 74 20 63 68 _lookup(const ch
66b0: 61 72 20 2a 75 73 65 72 2c 20 75 69 64 5f 74 20 ar *user, uid_t
66c0: 2a 75 73 65 72 5f 69 64 29 20 7b 0a 09 63 68 61 *user_id) {..cha
66d0: 72 20 2a 6e 65 78 74 3b 0a 09 75 69 64 5f 74 20 r *next;..uid_t
66e0: 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 3b 0a 23 user_id_check;.#
66f0: 69 66 6e 64 65 66 20 46 49 4c 45 44 5f 4e 4f 5f ifndef FILED_NO_
6700: 47 45 54 50 57 4e 41 4d 0a 09 73 74 72 75 63 74 GETPWNAM..struct
6710: 20 70 61 73 73 77 64 20 2a 65 6e 74 3b 0a 0a 09 passwd *ent;...
6720: 65 6e 74 20 3d 20 67 65 74 70 77 6e 61 6d 28 75 ent = getpwnam(u
6730: 73 65 72 29 3b 0a 09 69 66 20 28 65 6e 74 20 21 ser);..if (ent !
6740: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 75 73 65 = NULL) {...*use
6750: 72 5f 69 64 20 3d 20 65 6e 74 2d 3e 70 77 5f 75 r_id = ent->pw_u
6760: 69 64 3b 0a 0a 09 09 72 65 74 75 72 6e 28 30 29 id;....return(0)
6770: 3b 0a 09 7d 0a 23 65 6e 64 69 66 0a 0a 09 75 73 ;..}.#endif...us
6780: 65 72 5f 69 64 5f 63 68 65 63 6b 20 3d 20 73 74 er_id_check = st
6790: 72 74 6f 75 6c 6c 28 75 73 65 72 2c 20 26 6e 65 rtoull(user, &ne
67a0: 78 74 2c 20 31 30 29 3b 0a 09 69 66 20 28 6e 65 xt, 10);..if (ne
67b0: 78 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 xt == NULL) {...
67c0: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 return(1);..}...
67d0: 69 66 20 28 6e 65 78 74 5b 30 5d 20 21 3d 20 27 if (next[0] != '
67e0: 5c 30 27 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 \0') {...return(
67f0: 31 29 3b 0a 09 7d 0a 0a 09 2a 75 73 65 72 5f 69 1);..}...*user_i
6800: 64 20 3d 20 75 73 65 72 5f 69 64 5f 63 68 65 63 d = user_id_chec
6810: 6b 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a k;...return(0);.
6820: 7d 0a 0a 2f 2a 20 44 61 65 6d 6f 6e 69 7a 65 20 }../* Daemonize
6830: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
6840: 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 76 6f led_daemonize(vo
6850: 69 64 29 20 7b 0a 09 70 69 64 5f 74 20 73 65 74 id) {..pid_t set
6860: 73 69 64 5f 72 65 74 2c 20 66 6f 72 6b 5f 72 65 sid_ret, fork_re
6870: 74 3b 0a 09 69 6e 74 20 63 68 64 69 72 5f 72 65 t;..int chdir_re
6880: 74 2c 20 64 75 70 32 5f 72 65 74 3b 0a 09 69 6e t, dup2_ret;..in
6890: 74 20 66 64 5f 69 6e 2c 20 66 64 5f 6f 75 74 3b t fd_in, fd_out;
68a0: 0a 0a 09 63 68 64 69 72 5f 72 65 74 20 3d 20 63 ...chdir_ret = c
68b0: 68 64 69 72 28 22 2f 22 29 3b 0a 09 69 66 20 28 hdir("/");..if (
68c0: 63 68 64 69 72 5f 72 65 74 20 21 3d 20 30 29 20 chdir_ret != 0)
68d0: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 {...return(1);..
68e0: 7d 0a 0a 09 66 6f 72 6b 5f 72 65 74 20 3d 20 66 }...fork_ret = f
68f0: 6f 72 6b 28 29 3b 0a 09 69 66 20 28 66 6f 72 6b ork();..if (fork
6900: 5f 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 72 65 _ret < 0) {...re
6910: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 turn(1);..}...if
6920: 20 28 66 6f 72 6b 5f 72 65 74 20 3e 20 30 29 20 (fork_ret > 0)
6930: 7b 0a 09 09 2f 2a 20 50 61 72 65 6e 74 20 2a 2f {.../* Parent */
6940: 0a 09 09 77 61 69 74 70 69 64 28 66 6f 72 6b 5f ...waitpid(fork_
6950: 72 65 74 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 0a ret, NULL, 0);..
6960: 09 09 65 78 69 74 28 45 58 49 54 5f 53 55 43 43 ..exit(EXIT_SUCC
6970: 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 68 ESS);..}.../* Ch
6980: 69 6c 64 20 2a 2f 0a 09 69 66 20 28 66 6f 72 6b ild */..if (fork
6990: 28 29 20 21 3d 20 30 29 20 7b 0a 09 09 2f 2a 20 () != 0) {.../*
69a0: 43 68 69 6c 64 20 2a 2f 0a 09 09 65 78 69 74 28 Child */...exit(
69b0: 45 58 49 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 EXIT_SUCCESS);..
69c0: 7d 0a 0a 09 2f 2a 20 47 72 61 6e 64 20 63 68 69 }.../* Grand chi
69d0: 6c 64 20 2a 2f 0a 09 73 65 74 73 69 64 5f 72 65 ld */..setsid_re
69e0: 74 20 3d 20 73 65 74 73 69 64 28 29 3b 0a 09 69 t = setsid();..i
69f0: 66 20 28 73 65 74 73 69 64 5f 72 65 74 20 3d 3d f (setsid_ret ==
6a00: 20 28 28 70 69 64 5f 74 29 20 2d 31 29 29 20 7b ((pid_t) -1)) {
6a10: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d ...return(1);..}
6a20: 0a 0a 09 66 64 5f 69 6e 20 3d 20 6f 70 65 6e 28 ...fd_in = open(
6a30: 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 52 "/dev/null", O_R
6a40: 44 4f 4e 4c 59 29 3b 0a 09 66 64 5f 6f 75 74 20 DONLY);..fd_out
6a50: 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c = open("/dev/nul
6a60: 6c 22 2c 20 4f 5f 57 52 4f 4e 4c 59 29 3b 0a 09 l", O_WRONLY);..
6a70: 69 66 20 28 66 64 5f 69 6e 20 3c 20 30 20 7c 7c if (fd_in < 0 ||
6a80: 20 66 64 5f 6f 75 74 20 3c 20 30 29 20 7b 0a 09 fd_out < 0) {..
6a90: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a .return(1);..}..
6aa0: 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32 .dup2_ret = dup2
6ab0: 28 66 64 5f 69 6e 2c 20 53 54 44 49 4e 5f 46 49 (fd_in, STDIN_FI
6ac0: 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 LENO);..if (dup2
6ad0: 5f 72 65 74 20 21 3d 20 53 54 44 49 4e 5f 46 49 _ret != STDIN_FI
6ae0: 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e LENO) {...return
6af0: 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 (1);..}...dup2_r
6b00: 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f 75 74 et = dup2(fd_out
6b10: 2c 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 , STDOUT_FILENO)
6b20: 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74 20 ;..if (dup2_ret
6b30: 21 3d 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f != STDOUT_FILENO
6b40: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b ) {...return(1);
6b50: 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d ..}...dup2_ret =
6b60: 20 64 75 70 32 28 66 64 5f 6f 75 74 2c 20 53 54 dup2(fd_out, ST
6b70: 44 45 52 52 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 DERR_FILENO);..i
6b80: 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53 f (dup2_ret != S
6b90: 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29 20 7b 0a TDERR_FILENO) {.
6ba0: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
6bb0: 0a 09 63 6c 6f 73 65 28 66 64 5f 69 6e 29 3b 0a ..close(fd_in);.
6bc0: 09 63 6c 6f 73 65 28 66 64 5f 6f 75 74 29 3b 0a .close(fd_out);.
6bd0: 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a ..return(0);.}..
6be0: 2f 2a 20 52 75 6e 20 70 72 6f 63 65 73 73 20 2a /* Run process *
6bf0: 2f 0a 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20 61 /.int main(int a
6c00: 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 rgc, char **argv
6c10: 29 20 7b 0a 09 73 74 72 75 63 74 20 6f 70 74 69 ) {..struct opti
6c20: 6f 6e 20 6f 70 74 69 6f 6e 73 5b 31 31 5d 3b 0a on options[11];.
6c30: 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 62 69 6e .const char *bin
6c40: 64 5f 61 64 64 72 20 3d 20 42 49 4e 44 5f 41 44 d_addr = BIND_AD
6c50: 44 52 2c 20 2a 6e 65 77 72 6f 6f 74 20 3d 20 4e DR, *newroot = N
6c60: 55 4c 4c 2c 20 2a 6c 6f 67 5f 66 69 6c 65 20 3d ULL, *log_file =
6c70: 20 4c 4f 47 5f 46 49 4c 45 3b 0a 09 46 49 4c 45 LOG_FILE;..FILE
6c80: 20 2a 6c 6f 67 5f 66 70 3b 0a 09 75 69 64 5f 74 *log_fp;..uid_t
6c90: 20 75 73 65 72 20 3d 20 30 3b 0a 09 69 6e 74 20 user = 0;..int
6ca0: 70 6f 72 74 20 3d 20 50 4f 52 54 2c 20 74 68 72 port = PORT, thr
6cb0: 65 61 64 5f 63 6f 75 6e 74 20 3d 20 54 48 52 45 ead_count = THRE
6cc0: 41 44 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 AD_COUNT;..int c
6cd0: 61 63 68 65 5f 73 69 7a 65 20 3d 20 43 41 43 48 ache_size = CACH
6ce0: 45 5f 53 49 5a 45 3b 0a 09 69 6e 74 20 69 6e 69 E_SIZE;..int ini
6cf0: 74 5f 72 65 74 2c 20 63 68 72 6f 6f 74 5f 72 65 t_ret, chroot_re
6d00: 74 2c 20 73 65 74 75 69 64 5f 72 65 74 2c 20 6c t, setuid_ret, l
6d10: 6f 6f 6b 75 70 5f 72 65 74 2c 20 63 68 64 69 72 ookup_ret, chdir
6d20: 5f 72 65 74 3b 0a 09 69 6e 74 20 73 65 74 75 69 _ret;..int setui
6d30: 64 5f 65 6e 61 62 6c 65 64 20 3d 20 30 2c 20 64 d_enabled = 0, d
6d40: 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 aemon_enabled =
6d50: 30 3b 0a 09 69 6e 74 20 63 68 3b 0a 09 69 6e 74 0;..int ch;..int
6d60: 20 66 64 3b 0a 0a 09 2f 2a 20 50 72 6f 63 65 73 fd;.../* Proces
6d70: 73 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 s arguments */..
6d80: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
6d90: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
6da0: 73 5b 30 5d 2c 20 22 70 6f 72 74 22 2c 20 72 65 s[0], "port", re
6db0: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c quired_argument,
6dc0: 20 27 70 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 'p');..filed_ge
6dd0: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 topt_long_setopt
6de0: 28 26 6f 70 74 69 6f 6e 73 5b 31 5d 2c 20 22 74 (&options[1], "t
6df0: 68 72 65 61 64 73 22 2c 20 72 65 71 75 69 72 65 hreads", require
6e00: 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 74 27 29 d_argument, 't')
6e10: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f ;..filed_getopt_
6e20: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 long_setopt(&opt
6e30: 69 6f 6e 73 5b 32 5d 2c 20 22 63 61 63 68 65 22 ions[2], "cache"
6e40: 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d , required_argum
6e50: 65 6e 74 2c 20 27 63 27 29 3b 0a 09 66 69 6c 65 ent, 'c');..file
6e60: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 d_getopt_long_se
6e70: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 33 5d topt(&options[3]
6e80: 2c 20 22 62 69 6e 64 22 2c 20 72 65 71 75 69 72 , "bind", requir
6e90: 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 62 27 ed_argument, 'b'
6ea0: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 );..filed_getopt
6eb0: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 _long_setopt(&op
6ec0: 74 69 6f 6e 73 5b 34 5d 2c 20 22 75 73 65 72 22 tions[4], "user"
6ed0: 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d , required_argum
6ee0: 65 6e 74 2c 20 27 75 27 29 3b 0a 09 66 69 6c 65 ent, 'u');..file
6ef0: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 d_getopt_long_se
6f00: 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 35 5d topt(&options[5]
6f10: 2c 20 22 72 6f 6f 74 22 2c 20 72 65 71 75 69 72 , "root", requir
6f20: 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 72 27 ed_argument, 'r'
6f30: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 );..filed_getopt
6f40: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 _long_setopt(&op
6f50: 74 69 6f 6e 73 5b 36 5d 2c 20 22 68 65 6c 70 22 tions[6], "help"
6f60: 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 , no_argument, '
6f70: 68 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f h');..filed_geto
6f80: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
6f90: 6f 70 74 69 6f 6e 73 5b 37 5d 2c 20 22 64 61 65 options[7], "dae
6fa0: 6d 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e mon", no_argumen
6fb0: 74 2c 20 27 64 27 29 3b 0a 09 66 69 6c 65 64 5f t, 'd');..filed_
6fc0: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f getopt_long_seto
6fd0: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 38 5d 2c 20 pt(&options[8],
6fe0: 22 6c 6f 67 22 2c 20 72 65 71 75 69 72 65 64 5f "log", required_
6ff0: 61 72 67 75 6d 65 6e 74 2c 20 27 6c 27 29 3b 0a argument, 'l');.
7000: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
7010: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
7020: 6e 73 5b 39 5d 2c 20 22 76 65 72 73 69 6f 6e 22 ns[9], "version"
7030: 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 , no_argument, '
7040: 76 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f v');..filed_geto
7050: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
7060: 6f 70 74 69 6f 6e 73 5b 31 30 5d 2c 20 4e 55 4c options[10], NUL
7070: 4c 2c 20 30 2c 20 30 29 3b 0a 09 77 68 69 6c 65 L, 0, 0);..while
7080: 20 28 28 63 68 20 3d 20 67 65 74 6f 70 74 5f 6c ((ch = getopt_l
7090: 6f 6e 67 28 61 72 67 63 2c 20 61 72 67 76 2c 20 ong(argc, argv,
70a0: 22 70 3a 74 3a 63 3a 62 3a 75 3a 72 3a 6c 3a 68 "p:t:c:b:u:r:l:h
70b0: 64 76 22 2c 20 6f 70 74 69 6f 6e 73 2c 20 4e 55 dv", options, NU
70c0: 4c 4c 29 29 20 21 3d 20 2d 31 29 20 7b 0a 09 09 LL)) != -1) {...
70d0: 73 77 69 74 63 68 28 63 68 29 20 7b 0a 09 09 09 switch(ch) {....
70e0: 63 61 73 65 20 27 70 27 3a 0a 09 09 09 09 70 6f case 'p':.....po
70f0: 72 74 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 rt = atoi(optarg
7100: 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 );.....break;...
7110: 09 63 61 73 65 20 27 74 27 3a 0a 09 09 09 09 74 .case 't':.....t
7120: 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d 20 61 74 hread_count = at
7130: 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 oi(optarg);.....
7140: 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 break;....case '
7150: 63 27 3a 0a 09 09 09 09 63 61 63 68 65 5f 73 69 c':.....cache_si
7160: 7a 65 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 ze = atoi(optarg
7170: 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 );.....break;...
7180: 09 63 61 73 65 20 27 62 27 3a 0a 09 09 09 09 62 .case 'b':.....b
7190: 69 6e 64 5f 61 64 64 72 20 3d 20 73 74 72 64 75 ind_addr = strdu
71a0: 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 p(optarg);.....b
71b0: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 75 reak;....case 'u
71c0: 27 3a 0a 09 09 09 09 73 65 74 75 69 64 5f 65 6e ':.....setuid_en
71d0: 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 6c abled = 1;.....l
71e0: 6f 6f 6b 75 70 5f 72 65 74 20 3d 20 66 69 6c 65 ookup_ret = file
71f0: 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 6f 70 d_user_lookup(op
7200: 74 61 72 67 2c 20 26 75 73 65 72 29 3b 0a 09 09 targ, &user);...
7210: 09 09 69 66 20 28 6c 6f 6f 6b 75 70 5f 72 65 74 ..if (lookup_ret
7220: 20 21 3d 20 30 29 20 7b 0a 09 09 09 09 09 66 69 != 0) {......fi
7230: 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73 led_print_help(s
7240: 74 64 65 72 72 2c 20 30 2c 20 22 49 6e 76 61 6c tderr, 0, "Inval
7250: 69 64 20 75 73 65 72 6e 61 6d 65 20 73 70 65 63 id username spec
7260: 69 66 69 65 64 22 29 3b 0a 0a 09 09 09 09 09 72 ified");.......r
7270: 65 74 75 72 6e 28 31 29 3b 0a 09 09 09 09 7d 0a eturn(1);.....}.
7280: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 ....break;....ca
7290: 73 65 20 27 72 27 3a 0a 09 09 09 09 6e 65 77 72 se 'r':.....newr
72a0: 6f 6f 74 20 3d 20 73 74 72 64 75 70 28 6f 70 74 oot = strdup(opt
72b0: 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b arg);.....break;
72c0: 0a 09 09 09 63 61 73 65 20 27 6c 27 3a 0a 09 09 ....case 'l':...
72d0: 09 09 6c 6f 67 5f 66 69 6c 65 20 3d 20 73 74 72 ..log_file = str
72e0: 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 dup(optarg);....
72f0: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
7300: 27 64 27 3a 0a 09 09 09 09 64 61 65 6d 6f 6e 5f 'd':.....daemon_
7310: 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 enabled = 1;....
7320: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
7330: 27 76 27 3a 0a 09 09 09 09 70 72 69 6e 74 66 28 'v':.....printf(
7340: 22 66 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 25 "filed version %
7350: 73 5c 6e 22 2c 20 46 49 4c 45 44 5f 56 45 52 53 s\n", FILED_VERS
7360: 49 4f 4e 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 ION);......retur
7370: 6e 28 30 29 3b 0a 09 09 09 63 61 73 65 20 27 3f n(0);....case '?
7380: 27 3a 0a 09 09 09 63 61 73 65 20 27 3a 27 3a 0a ':....case ':':.
7390: 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f ....filed_print_
73a0: 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20 help(stderr, 0,
73b0: 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75 NULL);......retu
73c0: 72 6e 28 31 29 3b 0a 09 09 09 63 61 73 65 20 27 rn(1);....case '
73d0: 68 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 72 h':.....filed_pr
73e0: 69 6e 74 5f 68 65 6c 70 28 73 74 64 6f 75 74 2c int_help(stdout,
73f0: 20 31 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 1, NULL);......
7400: 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 7d 0a 09 return(0);...}..
7410: 7d 0a 0a 09 2f 2a 20 4f 70 65 6e 20 6c 6f 67 20 }.../* Open log
7420: 66 69 6c 65 20 2a 2f 0a 09 69 66 20 28 73 74 72 file */..if (str
7430: 63 6d 70 28 6c 6f 67 5f 66 69 6c 65 2c 20 22 2d cmp(log_file, "-
7440: 22 29 20 3d 3d 20 30 29 20 7b 0a 09 09 6c 6f 67 ") == 0) {...log
7450: 5f 66 70 20 3d 20 73 74 64 6f 75 74 3b 0a 09 7d _fp = stdout;..}
7460: 20 65 6c 73 65 20 7b 0a 09 09 6c 6f 67 5f 66 70 else {...log_fp
7470: 20 3d 20 66 6f 70 65 6e 28 6c 6f 67 5f 66 69 6c = fopen(log_fil
7480: 65 2c 20 22 61 2b 22 29 3b 0a 09 09 69 66 20 28 e, "a+");...if (
7490: 6c 6f 67 5f 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 log_fp == NULL)
74a0: 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 66 6f 70 {....perror("fop
74b0: 65 6e 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e en");.....return
74c0: 28 34 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a (4);...}..}.../*
74d0: 20 43 72 65 61 74 65 20 6c 69 73 74 65 6e 69 6e Create listenin
74e0: 67 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 66 64 20 g socket */..fd
74f0: 3d 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 62 = filed_listen(b
7500: 69 6e 64 5f 61 64 64 72 2c 20 70 6f 72 74 29 3b ind_addr, port);
7510: 0a 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a ..if (fd < 0) {.
7520: 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f ..perror("filed_
7530: 6c 69 73 74 65 6e 22 29 3b 0a 0a 09 09 72 65 74 listen");....ret
7540: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 urn(1);..}.../*
7550: 42 65 63 6f 6d 65 20 61 20 64 61 65 6d 6f 6e 20 Become a daemon
7560: 2a 2f 0a 09 69 66 20 28 64 61 65 6d 6f 6e 5f 65 */..if (daemon_e
7570: 6e 61 62 6c 65 64 29 20 7b 0a 09 09 69 6e 69 74 nabled) {...init
7580: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 64 61 65 _ret = filed_dae
7590: 6d 6f 6e 69 7a 65 28 29 3b 0a 09 09 69 66 20 28 monize();...if (
75a0: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b init_ret != 0) {
75b0: 0a 09 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 ....perror("file
75c0: 64 5f 64 61 65 6d 6f 6e 69 7a 65 22 29 3b 0a 0a d_daemonize");..
75d0: 09 09 09 72 65 74 75 72 6e 28 36 29 3b 0a 09 09 ...return(6);...
75e0: 7d 0a 09 7d 0a 0a 09 2f 2a 20 43 68 72 6f 6f 74 }..}.../* Chroot
75f0: 2c 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 , if appropriate
7600: 20 2a 2f 0a 09 69 66 20 28 6e 65 77 72 6f 6f 74 */..if (newroot
7610: 29 20 7b 0a 09 09 63 68 64 69 72 5f 72 65 74 20 ) {...chdir_ret
7620: 3d 20 63 68 64 69 72 28 6e 65 77 72 6f 6f 74 29 = chdir(newroot)
7630: 3b 0a 09 09 69 66 20 28 63 68 64 69 72 5f 72 65 ;...if (chdir_re
7640: 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 t != 0) {....per
7650: 72 6f 72 28 22 63 68 64 69 72 22 29 3b 0a 0a 09 ror("chdir");...
7660: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d ..return(1);...}
7670: 0a 0a 09 09 63 68 72 6f 6f 74 5f 72 65 74 20 3d ....chroot_ret =
7680: 20 63 68 72 6f 6f 74 28 22 2e 22 29 3b 0a 09 09 chroot(".");...
7690: 69 66 20 28 63 68 72 6f 6f 74 5f 72 65 74 20 21 if (chroot_ret !
76a0: 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 = 0) {....perror
76b0: 28 22 63 68 72 6f 6f 74 22 29 3b 0a 0a 09 09 09 ("chroot");.....
76c0: 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 return(1);...}..
76d0: 7d 0a 0a 09 2f 2a 20 44 72 6f 70 20 70 72 69 76 }.../* Drop priv
76e0: 69 6c 65 67 65 73 2c 20 69 66 20 61 70 70 72 6f ileges, if appro
76f0: 70 72 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 73 priate */..if (s
7700: 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 29 20 7b etuid_enabled) {
7710: 0a 09 09 73 65 74 75 69 64 5f 72 65 74 20 3d 20 ...setuid_ret =
7720: 73 65 74 75 69 64 28 75 73 65 72 29 3b 0a 09 09 setuid(user);...
7730: 69 66 20 28 73 65 74 75 69 64 5f 72 65 74 20 21 if (setuid_ret !
7740: 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 = 0) {....perror
7750: 28 22 73 65 74 75 69 64 22 29 3b 0a 0a 09 09 09 ("setuid");.....
7760: 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 return(1);...}..
7770: 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a }.../* Initializ
7780: 65 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d e */..init_ret =
7790: 20 66 69 6c 65 64 5f 69 6e 69 74 28 63 61 63 68 filed_init(cach
77a0: 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 69 6e e_size);..if (in
77b0: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 it_ret != 0) {..
77c0: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 69 .perror("filed_i
77d0: 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e nit");....return
77e0: 28 33 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 (3);..}.../* Cre
77f0: 61 74 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 ate logging thre
7800: 61 64 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 ad */..init_ret
7810: 3d 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f = filed_logging_
7820: 74 68 72 65 61 64 5f 69 6e 69 74 28 6c 6f 67 5f thread_init(log_
7830: 66 70 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 fp);..if (init_r
7840: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 et != 0) {...per
7850: 72 6f 72 28 22 66 69 6c 65 64 5f 6c 6f 67 67 69 ror("filed_loggi
7860: 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 22 29 ng_thread_init")
7870: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 34 29 3b 0a ;....return(4);.
7880: 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 77 .}.../* Create w
7890: 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a 2f orker threads */
78a0: 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c ..init_ret = fil
78b0: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 ed_worker_thread
78c0: 73 5f 69 6e 69 74 28 66 64 2c 20 74 68 72 65 61 s_init(fd, threa
78d0: 64 5f 63 6f 75 6e 74 29 3b 0a 09 69 66 20 28 69 d_count);..if (i
78e0: 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a nit_ret != 0) {.
78f0: 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f ..perror("filed_
7900: 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 worker_threads_i
7910: 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e nit");....return
7920: 28 35 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 61 69 (5);..}.../* Wai
7930: 74 20 66 6f 72 20 74 68 72 65 61 64 73 20 74 6f t for threads to
7940: 20 65 78 69 74 20 2a 2f 0a 09 2f 2a 20 58 58 58 exit */../* XXX
7950: 3a 54 4f 44 4f 3a 20 4d 6f 6e 69 74 6f 72 20 74 :TODO: Monitor t
7960: 68 72 65 61 64 20 75 73 61 67 65 20 2a 2f 0a 09 hread usage */..
7970: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 73 6c while (1) {...sl
7980: 65 65 70 28 36 30 29 3b 0a 09 7d 0a 0a 09 2f 2a eep(60);..}.../*
7990: 20 52 65 74 75 72 6e 20 69 6e 20 66 61 69 6c 75 Return in failu
79a0: 72 65 20 2a 2f 0a 09 72 65 74 75 72 6e 28 32 29 re */..return(2)
79b0: 3b 0a 7d 0a ;.}.