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 38 22 0a 23 64 65 66 69 6e ION "1.8".#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: 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 Configuration op
0310: 74 69 6f 6e 73 20 74 68 61 74 20 77 6f 72 6b 20 tions that work
0320: 74 68 72 65 61 64 73 20 6e 65 65 64 20 74 6f 20 threads need to
0330: 62 65 20 61 77 61 72 65 20 6f 66 20 2a 2f 0a 73 be aware of */.s
0340: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 truct filed_opti
0350: 6f 6e 73 20 7b 0a 09 69 6e 74 20 76 68 6f 73 74 ons {..int vhost
0360: 73 5f 65 6e 61 62 6c 65 64 3b 0a 7d 3b 0a 0a 2f s_enabled;.};../
0370: 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72 20 * Arguments for
0380: 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a worker threads *
0390: 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 /.struct filed_w
03a0: 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 orker_thread_arg
03b0: 73 20 7b 0a 09 69 6e 74 20 66 64 3b 0a 09 73 74 s {..int fd;..st
03c0: 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f ruct filed_optio
03d0: 6e 73 20 6f 70 74 69 6f 6e 73 3b 0a 7d 3b 0a 0a ns options;.};..
03e0: 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72 /* Arguments for
03f0: 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64 73 logging threads
0400: 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 */.struct filed
0410: 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f _logging_thread_
0420: 61 72 67 73 20 7b 0a 09 46 49 4c 45 20 2a 66 70 args {..FILE *fp
0430: 3b 0a 7d 3b 0a 0a 2f 2a 20 46 69 6c 65 20 69 6e ;.};../* File in
0440: 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74 72 formation */.str
0450: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e uct filed_filein
0460: 66 6f 20 7b 0a 09 70 74 68 72 65 61 64 5f 6d 75 fo {..pthread_mu
0470: 74 65 78 5f 74 20 6d 75 74 65 78 3b 0a 09 63 68 tex_t mutex;..ch
0480: 61 72 20 70 61 74 68 5b 46 49 4c 45 44 5f 50 41 ar path[FILED_PA
0490: 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b TH_BUFFER_SIZE];
04a0: 0a 09 69 6e 74 20 66 64 3b 0a 09 6f 66 66 5f 74 ..int fd;..off_t
04b0: 20 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 6c 61 73 len;..char *las
04c0: 74 6d 6f 64 3b 0a 09 63 68 61 72 20 6c 61 73 74 tmod;..char last
04d0: 6d 6f 64 5f 62 5b 36 34 5d 3b 0a 09 63 6f 6e 73 mod_b[64];..cons
04e0: 74 20 63 68 61 72 20 2a 74 79 70 65 3b 0a 09 63 t char *type;..c
04f0: 68 61 72 20 65 74 61 67 5b 36 34 5d 3b 0a 7d 3b har etag[64];.};
0500: 0a 0a 2f 2a 20 52 65 71 75 65 73 74 20 76 61 72 ../* Request var
0510: 69 61 62 6c 65 73 20 2a 2f 0a 73 74 72 75 63 74 iables */.struct
0520: 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 filed_http_requ
0530: 65 73 74 20 7b 0a 09 2f 2a 2a 20 42 75 66 66 65 est {../** Buffe
0540: 72 73 20 2a 2a 2f 0a 09 73 74 72 75 63 74 20 66 rs **/..struct f
0550: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 66 69 iled_fileinfo fi
0560: 6c 65 69 6e 66 6f 3b 0a 09 63 68 61 72 20 74 6d leinfo;..char tm
0570: 70 62 75 66 5b 46 49 4c 45 44 5f 50 41 54 48 5f pbuf[FILED_PATH_
0580: 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b 0a 0a 09 BUFFER_SIZE];...
0590: 2f 2a 2a 20 48 54 54 50 20 52 65 71 75 65 73 74 /** HTTP Request
05a0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2a 2f information **/
05b0: 0a 09 2f 2a 2a 2a 20 54 79 70 65 20 6f 66 20 72 ../*** Type of r
05c0: 65 71 75 65 73 74 20 28 48 45 41 44 20 6f 72 20 equest (HEAD or
05d0: 47 45 54 29 20 2a 2a 2a 2f 0a 09 65 6e 75 6d 20 GET) ***/..enum
05e0: 7b 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45 53 {...FILED_REQUES
05f0: 54 5f 4d 45 54 48 4f 44 5f 47 45 54 2c 0a 09 09 T_METHOD_GET,...
0600: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 FILED_REQUEST_ME
0610: 54 48 4f 44 5f 48 45 41 44 0a 09 7d 20 6d 65 74 THOD_HEAD..} met
0620: 68 6f 64 3b 0a 0a 09 2f 2a 2a 2a 20 50 61 74 68 hod;.../*** Path
0630: 20 62 65 69 6e 67 20 72 65 71 75 65 73 74 65 64 being requested
0640: 20 2a 2a 2a 2f 0a 09 63 68 61 72 20 70 61 74 68 ***/..char path
0650: 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 [FILED_PATH_BUFF
0660: 45 52 5f 53 49 5a 45 5d 3b 20 0a 0a 09 2f 2a 2a ER_SIZE]; .../**
0670: 2a 20 50 61 74 68 20 74 79 70 65 20 2a 2a 2a 2f * Path type ***/
0680: 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 4c 45 44 ..enum {...FILED
0690: 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 49 _REQUEST_TYPE_DI
06a0: 52 45 43 54 4f 52 59 2c 0a 09 09 46 49 4c 45 44 RECTORY,...FILED
06b0: 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 4f 54 _REQUEST_TYPE_OT
06c0: 48 45 52 0a 09 7d 20 74 79 70 65 3b 0a 0a 09 73 HER..} type;...s
06d0: 74 72 75 63 74 20 7b 0a 09 09 73 74 72 75 63 74 truct {...struct
06e0: 20 7b 0a 09 09 09 69 6e 74 20 70 72 65 73 65 6e {....int presen
06f0: 74 3b 0a 09 09 09 6f 66 66 5f 74 20 6f 66 66 73 t;....off_t offs
0700: 65 74 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67 65 et; /*** Range
0710: 20 73 74 61 72 74 20 2a 2a 2a 2f 0a 09 09 09 6f start ***/....o
0720: 66 66 5f 74 20 6c 65 6e 67 74 68 3b 20 20 20 2f ff_t length; /
0730: 2a 2a 2a 20 52 61 6e 67 65 20 6c 65 6e 67 74 68 *** Range length
0740: 20 2a 2a 2a 2f 0a 09 09 7d 20 72 61 6e 67 65 3b ***/...} range;
0750: 0a 0a 09 09 73 74 72 75 63 74 20 7b 0a 09 09 09 ....struct {....
0760: 69 6e 74 20 70 72 65 73 65 6e 74 3b 0a 09 09 09 int present;....
0770: 63 68 61 72 20 68 6f 73 74 5b 46 49 4c 45 44 5f char host[FILED_
0780: 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 PATH_BUFFER_SIZE
0790: 5d 3b 0a 09 09 7d 20 68 6f 73 74 3b 0a 09 7d 20 ];...} host;..}
07a0: 68 65 61 64 65 72 73 3b 0a 7d 3b 0a 0a 2f 2a 20 headers;.};../*
07b0: 4c 6f 67 20 72 65 63 6f 72 64 20 2a 2f 0a 73 74 Log record */.st
07c0: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 ruct filed_log_e
07d0: 6e 74 72 79 20 7b 0a 09 2f 2a 20 54 79 70 65 20 ntry {../* Type
07e0: 6f 66 20 6c 6f 67 20 65 6e 74 72 79 20 2a 2f 0a of log entry */.
07f0: 09 65 6e 75 6d 20 7b 0a 09 09 46 49 4c 45 44 5f .enum {...FILED_
0800: 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 53 41 47 45 LOG_TYPE_MESSAGE
0810: 2c 0a 09 09 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 ,...FILED_LOG_TY
0820: 50 45 5f 54 52 41 4e 53 46 45 52 0a 09 7d 20 74 PE_TRANSFER..} t
0830: 79 70 65 3b 0a 0a 09 2f 2a 20 4c 69 6e 6b 65 64 ype;.../* Linked
0840: 20 6c 69 73 74 20 68 65 61 64 2f 74 61 69 6c 20 list head/tail
0850: 2a 2f 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 */..struct filed
0860: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 5f 6e 65 78 _log_entry *_nex
0870: 74 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 t;..struct filed
0880: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 5f 70 72 65 _log_entry *_pre
0890: 76 3b 0a 0a 09 2f 2a 20 54 68 72 65 61 64 20 66 v;.../* Thread f
08a0: 72 6f 6d 20 77 68 69 63 68 20 74 68 69 73 20 6c rom which this l
08b0: 6f 67 20 65 6e 74 72 79 20 65 6d 69 6e 61 74 65 og entry eminate
08c0: 73 20 2a 2f 0a 09 70 74 68 72 65 61 64 5f 74 20 s */..pthread_t
08d0: 74 68 72 65 61 64 3b 0a 0a 09 2f 2a 20 4d 65 73 thread;.../* Mes
08e0: 73 61 67 65 20 62 75 66 66 65 72 20 66 6f 72 20 sage buffer for
08f0: 74 79 70 65 20 3d 20 4d 45 53 53 41 47 45 20 2a type = MESSAGE *
0900: 2f 0a 09 2f 2a 20 50 61 74 68 20 62 75 66 66 65 /../* Path buffe
0910: 72 20 66 6f 72 20 74 79 70 65 20 3d 20 54 52 41 r for type = TRA
0920: 4e 53 46 45 52 20 2a 2f 0a 09 63 68 61 72 20 62 NSFER */..char b
0930: 75 66 66 65 72 5b 46 49 4c 45 44 5f 50 41 54 48 uffer[FILED_PATH
0940: 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b 0a 0a _BUFFER_SIZE];..
0950: 09 2f 2a 20 49 74 65 6d 73 20 66 6f 72 20 74 79 ./* Items for ty
0960: 70 65 20 3d 20 54 52 41 4e 53 46 45 52 20 2a 2f pe = TRANSFER */
0970: 0a 09 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b ..int http_code;
0980: 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 ..const char *re
0990: 61 73 6f 6e 3b 0a 09 74 69 6d 65 5f 74 20 73 74 ason;..time_t st
09a0: 61 72 74 74 69 6d 65 3b 0a 09 74 69 6d 65 5f 74 arttime;..time_t
09b0: 20 65 6e 64 74 69 6d 65 3b 0a 09 6f 66 66 5f 74 endtime;..off_t
09c0: 20 72 65 71 5f 6f 66 66 73 65 74 3b 0a 09 6f 66 req_offset;..of
09d0: 66 5f 74 20 72 65 71 5f 6c 65 6e 67 74 68 3b 0a f_t req_length;.
09e0: 09 6f 66 66 5f 74 20 73 65 6e 74 5f 6c 65 6e 67 .off_t sent_leng
09f0: 74 68 3b 0a 09 6f 66 66 5f 74 20 66 69 6c 65 5f th;..off_t file_
0a00: 6c 65 6e 67 74 68 3b 0a 09 63 68 61 72 20 69 70 length;..char ip
0a10: 5b 31 32 38 5d 3b 0a 09 69 6e 74 20 70 6f 72 74 [128];..int port
0a20: 3b 0a 09 69 6e 74 20 6d 65 74 68 6f 64 3b 0a 7d ;..int method;.}
0a30: 3b 0a 0a 2f 2a 20 47 6c 6f 62 61 6c 20 76 61 72 ;../* Global var
0a40: 69 61 62 6c 65 73 20 2a 2f 0a 2f 2a 2a 20 4f 70 iables */./** Op
0a50: 65 6e 20 46 69 6c 65 20 63 61 63 68 65 20 2a 2a en File cache **
0a60: 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 /.struct filed_f
0a70: 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 64 5f 66 ileinfo *filed_f
0a80: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 ileinfo_fdcache
0a90: 3d 20 4e 55 4c 4c 3b 0a 75 6e 73 69 67 6e 65 64 = NULL;.unsigned
0aa0: 20 69 6e 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 int filed_filei
0ab0: 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 nfo_fdcache_size
0ac0: 20 3d 20 30 3b 0a 0a 2f 2a 2a 20 4c 6f 67 67 69 = 0;../** Loggi
0ad0: 6e 67 20 2a 2a 2f 0a 73 74 72 75 63 74 20 66 69 ng **/.struct fi
0ae0: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 66 led_log_entry *f
0af0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 iled_log_msg_lis
0b00: 74 3b 0a 70 74 68 72 65 61 64 5f 6d 75 74 65 78 t;.pthread_mutex
0b10: 5f 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 _t filed_log_msg
0b20: 5f 6c 69 73 74 5f 6d 75 74 65 78 3b 0a 70 74 68 _list_mutex;.pth
0b30: 72 65 61 64 5f 63 6f 6e 64 5f 74 20 66 69 6c 65 read_cond_t file
0b40: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 d_log_msg_list_r
0b50: 65 61 64 79 3b 0a 0a 2f 2a 20 49 6e 69 74 69 61 eady;../* Initia
0b60: 6c 69 7a 65 20 63 61 63 68 65 20 2a 2f 0a 73 74 lize cache */.st
0b70: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 atic int filed_i
0b80: 6e 69 74 5f 63 61 63 68 65 28 75 6e 73 69 67 6e nit_cache(unsign
0b90: 65 64 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a ed int cache_siz
0ba0: 65 29 20 7b 0a 09 75 6e 73 69 67 6e 65 64 20 69 e) {..unsigned i
0bb0: 6e 74 20 69 64 78 3b 0a 09 69 6e 74 20 6d 75 74 nt idx;..int mut
0bc0: 65 78 5f 69 6e 69 74 5f 72 65 74 3b 0a 0a 09 2f ex_init_ret;.../
0bd0: 2a 20 43 61 63 68 65 20 6d 61 79 20 6e 6f 74 20 * Cache may not
0be0: 62 65 20 72 65 2d 69 6e 69 74 69 61 6c 69 7a 65 be re-initialize
0bf0: 64 20 2a 2f 0a 09 69 66 20 28 66 69 6c 65 64 5f d */..if (filed_
0c00: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
0c10: 5f 73 69 7a 65 20 21 3d 20 30 20 7c 7c 20 66 69 _size != 0 || fi
0c20: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
0c30: 61 63 68 65 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a ache != NULL) {.
0c40: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
0c50: 0a 09 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 63 61 ../* Allocate ca
0c60: 63 68 65 20 2a 2f 0a 09 66 69 6c 65 64 5f 66 69 che */..filed_fi
0c70: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 leinfo_fdcache_s
0c80: 69 7a 65 20 3d 20 63 61 63 68 65 5f 73 69 7a 65 ize = cache_size
0c90: 3b 0a 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 ;..filed_fileinf
0ca0: 6f 5f 66 64 63 61 63 68 65 20 3d 20 6d 61 6c 6c o_fdcache = mall
0cb0: 6f 63 28 73 69 7a 65 6f 66 28 2a 66 69 6c 65 64 oc(sizeof(*filed
0cc0: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 _fileinfo_fdcach
0cd0: 65 29 20 2a 20 66 69 6c 65 64 5f 66 69 6c 65 69 e) * filed_filei
0ce0: 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 nfo_fdcache_size
0cf0: 29 3b 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 69 );..if (filed_fi
0d00: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d leinfo_fdcache =
0d10: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 = NULL) {...retu
0d20: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 rn(1);..}.../* I
0d30: 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 20 nitialize cache
0d40: 65 6e 74 72 69 65 73 20 2a 2f 0a 09 66 6f 72 20 entries */..for
0d50: 28 69 64 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 (idx = 0; idx <
0d60: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
0d70: 64 63 61 63 68 65 5f 73 69 7a 65 3b 20 69 64 78 dcache_size; idx
0d80: 2b 2b 29 20 7b 0a 09 09 6d 75 74 65 78 5f 69 6e ++) {...mutex_in
0d90: 69 74 5f 72 65 74 20 3d 20 70 74 68 72 65 61 64 it_ret = pthread
0da0: 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 66 69 6c _mutex_init(&fil
0db0: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 ed_fileinfo_fdca
0dc0: 63 68 65 5b 69 64 78 5d 2e 6d 75 74 65 78 2c 20 che[idx].mutex,
0dd0: 4e 55 4c 4c 29 3b 0a 09 09 69 66 20 28 6d 75 74 NULL);...if (mut
0de0: 65 78 5f 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 ex_init_ret != 0
0df0: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 31 29 ) {....return(1)
0e00: 3b 0a 09 09 7d 0a 0a 09 09 66 69 6c 65 64 5f 66 ;...}....filed_f
0e10: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b ileinfo_fdcache[
0e20: 69 64 78 5d 2e 70 61 74 68 5b 30 5d 20 3d 20 27 idx].path[0] = '
0e30: 5c 30 27 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c \0';...filed_fil
0e40: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 einfo_fdcache[id
0e50: 78 5d 2e 66 64 20 3d 20 2d 31 3b 0a 09 09 66 69 x].fd = -1;...fi
0e60: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
0e70: 61 63 68 65 5b 69 64 78 5d 2e 6c 61 73 74 6d 6f ache[idx].lastmo
0e80: 64 20 3d 20 22 22 3b 0a 09 09 66 69 6c 65 64 5f d = "";...filed_
0e90: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
0ea0: 5b 69 64 78 5d 2e 74 79 70 65 20 3d 20 22 22 3b [idx].type = "";
0eb0: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b ..}...return(0);
0ec0: 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a .}../* Initializ
0ed0: 65 20 70 72 6f 63 65 73 73 20 2a 2f 0a 73 74 61 e process */.sta
0ee0: 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e tic int filed_in
0ef0: 69 74 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 it(unsigned int
0f00: 63 61 63 68 65 5f 73 69 7a 65 29 20 7b 0a 09 73 cache_size) {..s
0f10: 74 61 74 69 63 20 69 6e 74 20 63 61 6c 6c 65 64 tatic int called
0f20: 20 3d 20 30 3b 0a 09 75 6e 73 69 67 6e 65 64 20 = 0;..unsigned
0f30: 69 6e 74 20 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 int random_value
0f40: 20 3d 20 30 3b 0a 09 69 6e 74 20 63 61 63 68 65 = 0;..int cache
0f50: 5f 72 65 74 3b 0a 09 69 6e 74 20 72 61 6e 64 6f _ret;..int rando
0f60: 6d 5f 66 64 3b 0a 0a 09 69 66 20 28 63 61 6c 6c m_fd;...if (call
0f70: 65 64 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 ed) {...return(0
0f80: 29 3b 0a 09 7d 0a 0a 09 63 61 6c 6c 65 64 20 3d );..}...called =
0f90: 20 31 3b 0a 0a 09 2f 2a 20 41 74 74 65 6d 70 74 1;.../* Attempt
0fa0: 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 6d 65 6d to lock all mem
0fb0: 6f 72 79 20 74 6f 20 70 68 79 73 69 63 61 6c 20 ory to physical
0fc0: 52 41 4d 20 28 62 75 74 20 64 6f 6e 27 74 20 63 RAM (but don't c
0fd0: 61 72 65 20 69 66 20 77 65 20 63 61 6e 27 74 29 are if we can't)
0fe0: 20 2a 2f 0a 09 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 */..mlockall(MC
0ff0: 4c 5f 43 55 52 52 45 4e 54 20 7c 20 4d 43 4c 5f L_CURRENT | MCL_
1000: 46 55 54 55 52 45 29 3b 0a 0a 09 2f 2a 20 49 67 FUTURE);.../* Ig
1010: 6e 6f 72 65 20 53 49 47 50 49 50 45 20 2a 2f 0a nore SIGPIPE */.
1020: 09 73 69 67 6e 61 6c 28 53 49 47 50 49 50 45 2c .signal(SIGPIPE,
1030: 20 53 49 47 5f 49 47 4e 29 3b 0a 0a 09 2f 2a 20 SIG_IGN);.../*
1040: 49 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 Initialize cache
1050: 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 63 structure */..c
1060: 61 63 68 65 5f 72 65 74 20 3d 20 66 69 6c 65 64 ache_ret = filed
1070: 5f 69 6e 69 74 5f 63 61 63 68 65 28 63 61 63 68 _init_cache(cach
1080: 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 63 61 e_size);..if (ca
1090: 63 68 65 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a che_ret != 0) {.
10a0: 09 09 72 65 74 75 72 6e 28 63 61 63 68 65 5f 72 ..return(cache_r
10b0: 65 74 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 et);..}.../* Ini
10c0: 74 69 61 6c 69 7a 65 20 72 61 6e 64 6f 6d 20 6e tialize random n
10d0: 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
10e0: 2a 2f 0a 09 72 61 6e 64 6f 6d 5f 66 64 20 3d 20 */..random_fd =
10f0: 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 open("/dev/urand
1100: 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a om", O_RDONLY);.
1110: 09 69 66 20 28 72 61 6e 64 6f 6d 5f 66 64 20 3e .if (random_fd >
1120: 3d 20 30 29 20 7b 0a 09 09 72 65 61 64 28 72 61 = 0) {...read(ra
1130: 6e 64 6f 6d 5f 66 64 2c 20 26 72 61 6e 64 6f 6d ndom_fd, &random
1140: 5f 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66 28 72 _value, sizeof(r
1150: 61 6e 64 6f 6d 5f 76 61 6c 75 65 29 29 3b 0a 0a andom_value));..
1160: 09 09 63 6c 6f 73 65 28 72 61 6e 64 6f 6d 5f 66 ..close(random_f
1170: 64 29 3b 0a 09 7d 0a 0a 09 72 61 6e 64 6f 6d 5f d);..}...random_
1180: 76 61 6c 75 65 20 5e 3d 20 67 65 74 70 69 64 28 value ^= getpid(
1190: 29 3b 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 );..random_value
11a0: 20 5e 3d 20 67 65 74 75 69 64 28 29 3b 0a 09 72 ^= getuid();..r
11b0: 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 74 andom_value ^= t
11c0: 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 73 72 61 ime(NULL);...sra
11d0: 6e 64 6f 6d 28 72 61 6e 64 6f 6d 5f 76 61 6c 75 ndom(random_valu
11e0: 65 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b e);...return(0);
11f0: 0a 7d 0a 0a 2f 2a 20 4c 69 73 74 65 6e 20 6f 6e .}../* Listen on
1200: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 61 64 a particular ad
1210: 64 72 65 73 73 2f 70 6f 72 74 20 2a 2f 0a 73 74 dress/port */.st
1220: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 6c atic int filed_l
1230: 69 73 74 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 isten(const char
1240: 20 2a 61 64 64 72 65 73 73 2c 20 75 6e 73 69 67 *address, unsig
1250: 6e 65 64 20 69 6e 74 20 70 6f 72 74 29 20 7b 0a ned int port) {.
1260: 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 .struct sockaddr
1270: 5f 69 6e 36 20 61 64 64 72 5f 76 36 3b 0a 09 73 _in6 addr_v6;..s
1280: 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 truct sockaddr_i
1290: 6e 20 61 64 64 72 5f 76 34 3b 0a 09 73 74 72 75 n addr_v4;..stru
12a0: 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 61 64 64 ct sockaddr *add
12b0: 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 61 64 r;..socklen_t ad
12c0: 64 72 5f 6c 65 6e 3b 0a 09 69 6e 74 20 70 74 6f dr_len;..int pto
12d0: 6e 5f 72 65 74 2c 20 62 69 6e 64 5f 72 65 74 2c n_ret, bind_ret,
12e0: 20 6c 69 73 74 65 6e 5f 72 65 74 3b 0a 09 69 6e listen_ret;..in
12f0: 74 20 66 61 6d 69 6c 79 3b 0a 09 69 6e 74 20 66 t family;..int f
1300: 64 3b 0a 0a 0a 09 66 61 6d 69 6c 79 20 3d 20 41 d;....family = A
1310: 46 5f 49 4e 45 54 36 3b 0a 09 70 74 6f 6e 5f 72 F_INET6;..pton_r
1320: 65 74 20 3d 20 69 6e 65 74 5f 70 74 6f 6e 28 66 et = inet_pton(f
1330: 61 6d 69 6c 79 2c 20 61 64 64 72 65 73 73 2c 20 amily, address,
1340: 26 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 61 64 &addr_v6.sin6_ad
1350: 64 72 2e 73 36 5f 61 64 64 72 29 3b 0a 09 69 66 dr.s6_addr);..if
1360: 20 28 70 74 6f 6e 5f 72 65 74 20 21 3d 20 31 29 (pton_ret != 1)
1370: 20 7b 0a 09 09 66 61 6d 69 6c 79 20 3d 20 41 46 {...family = AF
1380: 5f 49 4e 45 54 3b 0a 09 09 70 74 6f 6e 5f 72 65 _INET;...pton_re
1390: 74 20 3d 20 69 6e 65 74 5f 70 74 6f 6e 28 66 61 t = inet_pton(fa
13a0: 6d 69 6c 79 2c 20 61 64 64 72 65 73 73 2c 20 26 mily, address, &
13b0: 61 64 64 72 5f 76 34 2e 73 69 6e 5f 61 64 64 72 addr_v4.sin_addr
13c0: 2e 73 5f 61 64 64 72 29 3b 0a 09 09 69 66 20 28 .s_addr);...if (
13d0: 70 74 6f 6e 5f 72 65 74 20 21 3d 20 31 29 20 7b pton_ret != 1) {
13e0: 0a 09 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a ....return(-1);.
13f0: 09 09 7d 0a 0a 09 09 61 64 64 72 5f 76 34 2e 73 ..}....addr_v4.s
1400: 69 6e 5f 66 61 6d 69 6c 79 20 3d 20 66 61 6d 69 in_family = fami
1410: 6c 79 3b 0a 09 09 61 64 64 72 5f 76 34 2e 73 69 ly;...addr_v4.si
1420: 6e 5f 70 6f 72 74 20 3d 20 68 74 6f 6e 73 28 70 n_port = htons(p
1430: 6f 72 74 29 3b 0a 0a 09 09 61 64 64 72 20 3d 20 ort);....addr =
1440: 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 (struct sockaddr
1450: 20 2a 29 20 26 61 64 64 72 5f 76 34 3b 0a 09 09 *) &addr_v4;...
1460: 61 64 64 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f addr_len = sizeo
1470: 66 28 61 64 64 72 5f 76 34 29 3b 0a 09 7d 20 65 f(addr_v4);..} e
1480: 6c 73 65 20 7b 0a 09 09 61 64 64 72 5f 76 36 2e lse {...addr_v6.
1490: 73 69 6e 36 5f 66 61 6d 69 6c 79 20 3d 20 41 46 sin6_family = AF
14a0: 5f 49 4e 45 54 36 3b 0a 09 09 61 64 64 72 5f 76 _INET6;...addr_v
14b0: 36 2e 73 69 6e 36 5f 66 6c 6f 77 69 6e 66 6f 20 6.sin6_flowinfo
14c0: 3d 20 30 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 = 0;...addr_v6.s
14d0: 69 6e 36 5f 73 63 6f 70 65 5f 69 64 20 3d 20 30 in6_scope_id = 0
14e0: 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 ;...addr_v6.sin6
14f0: 5f 70 6f 72 74 20 3d 20 68 74 6f 6e 73 28 70 6f _port = htons(po
1500: 72 74 29 3b 0a 0a 09 09 61 64 64 72 20 3d 20 28 rt);....addr = (
1510: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 struct sockaddr
1520: 2a 29 20 26 61 64 64 72 5f 76 36 3b 0a 09 09 61 *) &addr_v6;...a
1530: 64 64 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 ddr_len = sizeof
1540: 28 61 64 64 72 5f 76 36 29 3b 0a 09 7d 0a 0a 09 (addr_v6);..}...
1550: 66 64 20 3d 20 73 6f 63 6b 65 74 28 66 61 6d 69 fd = socket(fami
1560: 6c 79 2c 20 53 4f 43 4b 5f 53 54 52 45 41 4d 2c ly, SOCK_STREAM,
1570: 20 30 29 3b 0a 09 69 66 20 28 66 64 20 3c 20 30 0);..if (fd < 0
1580: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 66 64 29 ) {...return(fd)
1590: 3b 0a 09 7d 0a 0a 09 62 69 6e 64 5f 72 65 74 20 ;..}...bind_ret
15a0: 3d 20 62 69 6e 64 28 66 64 2c 20 61 64 64 72 2c = bind(fd, addr,
15b0: 20 61 64 64 72 5f 6c 65 6e 29 3b 0a 09 69 66 20 addr_len);..if
15c0: 28 62 69 6e 64 5f 72 65 74 20 3c 20 30 29 20 7b (bind_ret < 0) {
15d0: 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 ...close(fd);...
15e0: 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a .return(-1);..}.
15f0: 0a 09 6c 69 73 74 65 6e 5f 72 65 74 20 3d 20 6c ..listen_ret = l
1600: 69 73 74 65 6e 28 66 64 2c 20 31 32 38 29 3b 0a isten(fd, 128);.
1610: 09 69 66 20 28 6c 69 73 74 65 6e 5f 72 65 74 20 .if (listen_ret
1620: 21 3d 20 30 29 20 7b 0a 09 09 63 6c 6f 73 65 28 != 0) {...close(
1630: 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 2d fd);....return(-
1640: 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 1);..}...return(
1650: 66 64 29 3b 0a 7d 0a 0a 2f 2a 20 4c 6f 67 20 61 fd);.}../* Log a
1660: 20 6d 65 73 73 61 67 65 20 2a 2f 0a 23 69 66 64 message */.#ifd
1670: 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f ef FILED_DONT_LO
1680: 47 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 G.# define file
1690: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 d_logging_thread
16a0: 5f 69 6e 69 74 28 78 29 20 30 0a 23 20 20 64 65 _init(x) 0.# de
16b0: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d fine filed_log_m
16c0: 73 67 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29 sg_debug(x, ...)
16d0: 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 /**/.# define
16e0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 78 2c filed_log_msg(x,
16f0: 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 ...) /**/.# de
1700: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 fine filed_log_e
1710: 6e 74 72 79 28 78 29 20 2f 2a 2a 2f 0a 23 20 20 ntry(x) /**/.#
1720: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 define filed_log
1730: 5f 69 70 28 78 2c 20 2e 2e 2e 29 20 4e 55 4c 4c _ip(x, ...) NULL
1740: 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 .# define filed
1750: 5f 6c 6f 67 5f 6e 65 77 28 78 29 20 26 6c 6f 63 _log_new(x) &loc
1760: 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 0a 23 20 20 al_dummy_log.#
1770: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 define filed_log
1780: 5f 6f 70 65 6e 28 78 29 20 73 74 64 6f 75 74 0a _open(x) stdout.
1790: 23 65 6c 73 65 0a 23 20 20 69 66 64 65 66 20 46 #else.# ifdef F
17a0: 49 4c 45 44 5f 44 45 42 55 47 0a 23 20 20 20 20 ILED_DEBUG.#
17b0: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 define filed_log
17c0: 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c 20 2e 2e _msg_debug(x, ..
17d0: 2e 29 20 7b 20 66 70 72 69 6e 74 66 28 73 74 64 .) { fprintf(std
17e0: 65 72 72 2c 20 78 2c 20 5f 5f 56 41 5f 41 52 47 err, x, __VA_ARG
17f0: 53 5f 5f 29 3b 20 66 70 72 69 6e 74 66 28 73 74 S__); fprintf(st
1800: 64 65 72 72 2c 20 22 5c 6e 22 29 3b 20 66 66 6c derr, "\n"); ffl
1810: 75 73 68 28 73 74 64 65 72 72 29 3b 20 7d 0a 23 ush(stderr); }.#
1820: 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 else.# defi
1830: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ne filed_log_msg
1840: 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20 2f _debug(x, ...) /
1850: 2a 2a 2f 0a 23 20 20 65 6e 64 69 66 0a 0a 2f 2a **/.# endif../*
1860: 20 49 6e 69 74 69 61 6c 69 7a 65 20 6c 6f 67 67 Initialize logg
1870: 69 6e 67 20 74 68 72 65 61 64 20 2a 2f 0a 73 74 ing thread */.st
1880: 61 74 69 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 atic void *filed
1890: 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 28 _logging_thread(
18a0: 76 6f 69 64 20 2a 61 72 67 5f 70 29 20 7b 0a 09 void *arg_p) {..
18b0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 struct filed_log
18c0: 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 73 ging_thread_args
18d0: 20 2a 61 72 67 3b 0a 09 73 74 72 75 63 74 20 66 *arg;..struct f
18e0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
18f0: 63 75 72 72 2c 20 2a 70 72 65 76 3b 0a 09 63 6f curr, *prev;..co
1900: 6e 73 74 20 63 68 61 72 20 2a 6d 65 74 68 6f 64 nst char *method
1910: 3b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 3b 0a 09 ;..time_t now;..
1920: 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 61 72 67 20 FILE *fp;...arg
1930: 3d 20 61 72 67 5f 70 3b 0a 0a 09 66 70 20 3d 20 = arg_p;...fp =
1940: 61 72 67 2d 3e 66 70 3b 0a 0a 09 77 68 69 6c 65 arg->fp;...while
1950: 20 28 31 29 20 7b 0a 09 09 70 74 68 72 65 61 64 (1) {...pthread
1960: 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 66 69 6c _mutex_lock(&fil
1970: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f ed_log_msg_list_
1980: 6d 75 74 65 78 29 3b 0a 09 09 70 74 68 72 65 61 mutex);...pthrea
1990: 64 5f 63 6f 6e 64 5f 77 61 69 74 28 26 66 69 6c d_cond_wait(&fil
19a0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f ed_log_msg_list_
19b0: 72 65 61 64 79 2c 20 26 66 69 6c 65 64 5f 6c 6f ready, &filed_lo
19c0: 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 g_msg_list_mutex
19d0: 29 3b 0a 0a 09 09 63 75 72 72 20 3d 20 66 69 6c );....curr = fil
19e0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b ed_log_msg_list;
19f0: 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ...filed_log_msg
1a00: 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 _list = NULL;...
1a10: 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 .pthread_mutex_u
1a20: 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 nlock(&filed_log
1a30: 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 _msg_list_mutex)
1a40: 3b 0a 0a 09 09 6e 6f 77 20 3d 20 74 69 6d 65 28 ;....now = time(
1a50: 4e 55 4c 4c 29 3b 0a 0a 09 09 70 72 65 76 20 3d NULL);....prev =
1a60: 20 4e 55 4c 4c 3b 0a 09 09 66 6f 72 20 28 3b 20 NULL;...for (;
1a70: 63 75 72 72 3b 20 63 75 72 72 20 3d 20 63 75 72 curr; curr = cur
1a80: 72 2d 3e 5f 6e 65 78 74 29 20 7b 0a 09 09 09 63 r->_next) {....c
1a90: 75 72 72 2d 3e 5f 70 72 65 76 20 3d 20 70 72 65 urr->_prev = pre
1aa0: 76 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20 63 75 v;.....prev = cu
1ab0: 72 72 3b 0a 09 09 7d 0a 0a 09 09 63 75 72 72 20 rr;...}....curr
1ac0: 3d 20 70 72 65 76 3b 0a 09 09 77 68 69 6c 65 20 = prev;...while
1ad0: 28 63 75 72 72 29 20 7b 0a 09 09 09 73 77 69 74 (curr) {....swit
1ae0: 63 68 20 28 63 75 72 72 2d 3e 74 79 70 65 29 20 ch (curr->type)
1af0: 7b 0a 09 09 09 09 63 61 73 65 20 46 49 4c 45 44 {.....case FILED
1b00: 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 53 41 47 _LOG_TYPE_MESSAG
1b10: 45 3a 0a 09 09 09 09 09 66 70 72 69 6e 74 66 28 E:......fprintf(
1b20: 66 70 2c 20 22 25 73 22 2c 20 63 75 72 72 2d 3e fp, "%s", curr->
1b30: 62 75 66 66 65 72 29 3b 0a 0a 09 09 09 09 09 62 buffer);.......b
1b40: 72 65 61 6b 3b 0a 09 09 09 09 63 61 73 65 20 46 reak;.....case F
1b50: 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 54 52 ILED_LOG_TYPE_TR
1b60: 41 4e 53 46 45 52 3a 0a 09 09 09 09 09 73 77 69 ANSFER:......swi
1b70: 74 63 68 20 28 63 75 72 72 2d 3e 6d 65 74 68 6f tch (curr->metho
1b80: 64 29 20 7b 0a 09 09 09 09 09 09 63 61 73 65 20 d) {.......case
1b90: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 FILED_REQUEST_ME
1ba0: 54 48 4f 44 5f 47 45 54 3a 0a 09 09 09 09 09 09 THOD_GET:.......
1bb0: 09 6d 65 74 68 6f 64 3d 22 47 45 54 22 3b 0a 09 .method="GET";..
1bc0: 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 ......break;....
1bd0: 09 09 09 63 61 73 65 20 46 49 4c 45 44 5f 52 45 ...case FILED_RE
1be0: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45 41 QUEST_METHOD_HEA
1bf0: 44 3a 0a 09 09 09 09 09 09 09 6d 65 74 68 6f 64 D:........method
1c00: 3d 22 48 45 41 44 22 3b 0a 09 09 09 09 09 09 09 ="HEAD";........
1c10: 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 64 65 66 break;.......def
1c20: 61 75 6c 74 3a 0a 09 09 09 09 09 09 09 6d 65 74 ault:........met
1c30: 68 6f 64 3d 22 3c 75 6e 6b 6e 6f 77 6e 3e 22 3b hod="<unknown>";
1c40: 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 ........break;..
1c50: 09 09 09 09 7d 0a 0a 09 09 09 09 09 69 66 20 28 ....}.......if (
1c60: 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 20 3d 3d curr->endtime ==
1c70: 20 28 28 74 69 6d 65 5f 74 29 20 2d 31 29 29 20 ((time_t) -1))
1c80: 7b 0a 09 09 09 09 09 09 63 75 72 72 2d 3e 65 6e {.......curr->en
1c90: 64 74 69 6d 65 20 3d 20 6e 6f 77 3b 0a 09 09 09 dtime = now;....
1ca0: 09 09 7d 0a 0a 09 09 09 09 09 66 70 72 69 6e 74 ..}.......fprint
1cb0: 66 28 66 70 2c 20 22 54 52 41 4e 53 46 45 52 20 f(fp, "TRANSFER
1cc0: 4d 45 54 48 4f 44 3d 25 73 20 50 41 54 48 3d 25 METHOD=%s PATH=%
1cd0: 73 20 53 52 43 3d 25 73 3a 25 69 20 54 49 4d 45 s SRC=%s:%i TIME
1ce0: 2e 53 54 41 52 54 3d 25 6c 6c 75 20 54 49 4d 45 .START=%llu TIME
1cf0: 2e 45 4e 44 3d 25 6c 6c 75 20 43 4f 44 45 2e 56 .END=%llu CODE.V
1d00: 41 4c 55 45 3d 25 75 20 43 4f 44 45 2e 52 45 41 ALUE=%u CODE.REA
1d10: 53 4f 4e 3d 25 73 20 52 45 51 55 45 53 54 2e 4f SON=%s REQUEST.O
1d20: 46 46 53 45 54 3d 25 6c 6c 75 20 52 45 51 55 45 FFSET=%llu REQUE
1d30: 53 54 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 20 46 ST.LENGTH=%llu F
1d40: 49 4c 45 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 20 ILE.LENGTH=%llu
1d50: 54 52 41 4e 53 46 45 52 2e 4c 45 4e 47 54 48 3d TRANSFER.LENGTH=
1d60: 25 6c 6c 75 22 2c 0a 09 09 09 09 09 09 6d 65 74 %llu",.......met
1d70: 68 6f 64 2c 0a 09 09 09 09 09 09 63 75 72 72 2d hod,.......curr-
1d80: 3e 62 75 66 66 65 72 2c 0a 09 09 09 09 09 09 63 >buffer,.......c
1d90: 75 72 72 2d 3e 69 70 2c 20 63 75 72 72 2d 3e 70 urr->ip, curr->p
1da0: 6f 72 74 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 ort,.......(unsi
1db0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
1dc0: 63 75 72 72 2d 3e 73 74 61 72 74 74 69 6d 65 2c curr->starttime,
1dd0: 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 .......(unsigned
1de0: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 long long) curr
1df0: 2d 3e 65 6e 64 74 69 6d 65 2c 0a 09 09 09 09 09 ->endtime,......
1e00: 09 63 75 72 72 2d 3e 68 74 74 70 5f 63 6f 64 65 .curr->http_code
1e10: 2c 20 63 75 72 72 2d 3e 72 65 61 73 6f 6e 2c 0a , curr->reason,.
1e20: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ......(unsigned
1e30: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d long long) curr-
1e40: 3e 72 65 71 5f 6f 66 66 73 65 74 2c 0a 09 09 09 >req_offset,....
1e50: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
1e60: 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 72 65 g long) curr->re
1e70: 71 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09 09 q_length,.......
1e80: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
1e90: 6f 6e 67 29 20 63 75 72 72 2d 3e 66 69 6c 65 5f ong) curr->file_
1ea0: 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09 09 28 75 length,.......(u
1eb0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
1ec0: 67 29 20 63 75 72 72 2d 3e 73 65 6e 74 5f 6c 65 g) curr->sent_le
1ed0: 6e 67 74 68 0a 09 09 09 09 09 29 3b 0a 0a 09 09 ngth......);....
1ee0: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 7d 0a 09 ...break;....}..
1ef0: 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 20 ..fprintf(fp, "
1f00: 54 48 52 45 41 44 3d 25 6c 6c 75 20 54 49 4d 45 THREAD=%llu TIME
1f10: 3d 25 6c 6c 75 5c 6e 22 2c 0a 09 09 09 09 28 75 =%llu\n",.....(u
1f20: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
1f30: 67 29 20 63 75 72 72 2d 3e 74 68 72 65 61 64 2c g) curr->thread,
1f40: 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
1f50: 6f 6e 67 20 6c 6f 6e 67 29 20 6e 6f 77 0a 09 09 ong long) now...
1f60: 09 29 3b 0a 09 09 09 66 66 6c 75 73 68 28 66 70 .);....fflush(fp
1f70: 29 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20 63 75 );.....prev = cu
1f80: 72 72 3b 0a 09 09 09 63 75 72 72 20 3d 20 63 75 rr;....curr = cu
1f90: 72 72 2d 3e 5f 70 72 65 76 3b 0a 0a 09 09 09 66 rr->_prev;.....f
1fa0: 72 65 65 28 70 72 65 76 29 3b 0a 09 09 7d 0a 09 ree(prev);...}..
1fb0: 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 }...return(NULL)
1fc0: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 ;.}..static void
1fd0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 filed_log_entry
1fe0: 28 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f (struct filed_lo
1ff0: 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 29 20 g_entry *entry)
2000: 7b 0a 09 65 6e 74 72 79 2d 3e 74 68 72 65 61 64 {..entry->thread
2010: 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
2020: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 );...pthread_mut
2030: 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c ex_lock(&filed_l
2040: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 og_msg_list_mute
2050: 78 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 5f 6e 65 x);...entry->_ne
2060: 78 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d xt = filed_log_m
2070: 73 67 5f 6c 69 73 74 3b 0a 09 66 69 6c 65 64 5f sg_list;..filed_
2080: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 65 log_msg_list = e
2090: 6e 74 72 79 3b 0a 0a 09 70 74 68 72 65 61 64 5f ntry;...pthread_
20a0: 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 mutex_unlock(&fi
20b0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 led_log_msg_list
20c0: 5f 6d 75 74 65 78 29 3b 0a 0a 09 70 74 68 72 65 _mutex);...pthre
20d0: 61 64 5f 63 6f 6e 64 5f 73 69 67 6e 61 6c 28 26 ad_cond_signal(&
20e0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
20f0: 73 74 5f 72 65 61 64 79 29 3b 0a 0a 09 72 65 74 st_ready);...ret
2100: 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73 urn;.}..static s
2110: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
2120: 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c 6f 67 entry *filed_log
2130: 5f 6e 65 77 28 69 6e 74 20 69 6e 69 74 69 61 6c _new(int initial
2140: 69 7a 65 29 20 7b 0a 09 73 74 72 75 63 74 20 66 ize) {..struct f
2150: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
2160: 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c retval;...retval
2170: 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 = malloc(sizeof
2180: 28 2a 72 65 74 76 61 6c 29 29 3b 0a 0a 09 69 66 (*retval));...if
2190: 20 28 69 6e 69 74 69 61 6c 69 7a 65 29 20 7b 0a (initialize) {.
21a0: 09 09 72 65 74 76 61 6c 2d 3e 62 75 66 66 65 72 ..retval->buffer
21b0: 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 72 65 [0] = '\0';...re
21c0: 74 76 61 6c 2d 3e 68 74 74 70 5f 63 6f 64 65 20 tval->http_code
21d0: 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c 2d 3e = -1;...retval->
21e0: 73 74 61 72 74 74 69 6d 65 20 3d 20 30 3b 0a 09 starttime = 0;..
21f0: 09 72 65 74 76 61 6c 2d 3e 65 6e 64 74 69 6d 65 .retval->endtime
2200: 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e = 0;...retval->
2210: 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 30 3b 0a req_offset = 0;.
2220: 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f 6c 65 ..retval->req_le
2230: 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 ngth = 0;...retv
2240: 61 6c 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 20 al->sent_length
2250: 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 66 = 0;...retval->f
2260: 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 30 3b 0a ile_length = 0;.
2270: 09 09 72 65 74 76 61 6c 2d 3e 69 70 5b 30 5d 20 ..retval->ip[0]
2280: 3d 20 27 5c 30 27 3b 0a 09 09 72 65 74 76 61 6c = '\0';...retval
2290: 2d 3e 70 6f 72 74 20 3d 20 2d 31 3b 0a 09 09 72 ->port = -1;...r
22a0: 65 74 76 61 6c 2d 3e 6d 65 74 68 6f 64 20 3d 20 etval->method =
22b0: 2d 31 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 -1;..}...return(
22c0: 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 retval);.}..stat
22d0: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 6c 6f ic void filed_lo
22e0: 67 5f 6d 73 67 28 63 6f 6e 73 74 20 63 68 61 72 g_msg(const char
22f0: 20 2a 66 6d 74 2c 20 2e 2e 2e 29 20 7b 0a 09 73 *fmt, ...) {..s
2300: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
2310: 65 6e 74 72 79 20 2a 65 6e 74 72 79 3b 0a 09 76 entry *entry;..v
2320: 61 5f 6c 69 73 74 20 61 72 67 73 3b 0a 0a 09 65 a_list args;...e
2330: 6e 74 72 79 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 ntry = filed_log
2340: 5f 6e 65 77 28 30 29 3b 0a 0a 09 76 61 5f 73 74 _new(0);...va_st
2350: 61 72 74 28 61 72 67 73 2c 20 66 6d 74 29 3b 0a art(args, fmt);.
2360: 0a 09 76 73 6e 70 72 69 6e 74 66 28 65 6e 74 72 ..vsnprintf(entr
2370: 79 2d 3e 62 75 66 66 65 72 2c 20 73 69 7a 65 6f y->buffer, sizeo
2380: 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 65 72 29 f(entry->buffer)
2390: 2c 20 66 6d 74 2c 20 61 72 67 73 29 3b 0a 0a 09 , fmt, args);...
23a0: 76 61 5f 65 6e 64 28 61 72 67 73 29 3b 0a 0a 09 va_end(args);...
23b0: 65 6e 74 72 79 2d 3e 74 79 70 65 20 3d 20 46 49 entry->type = FI
23c0: 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 LED_LOG_TYPE_MES
23d0: 53 41 47 45 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f SAGE;...filed_lo
23e0: 67 5f 65 6e 74 72 79 28 65 6e 74 72 79 29 3b 0a g_entry(entry);.
23f0: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 ..return;.}..sta
2400: 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
2410: 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 73 74 72 filed_log_ip(str
2420: 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 61 64 uct sockaddr *ad
2430: 64 72 2c 20 63 68 61 72 20 2a 62 75 66 66 65 72 dr, char *buffer
2440: 2c 20 73 69 7a 65 5f 74 20 62 75 66 66 65 72 6c , size_t bufferl
2450: 65 6e 29 20 7b 0a 09 73 74 72 75 63 74 20 73 6f en) {..struct so
2460: 63 6b 61 64 64 72 5f 69 6e 20 2a 61 64 64 72 5f ckaddr_in *addr_
2470: 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b v4;..struct sock
2480: 61 64 64 72 5f 69 6e 36 20 2a 61 64 64 72 5f 76 addr_in6 *addr_v
2490: 36 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 6;..const char *
24a0: 72 65 74 76 61 6c 20 3d 20 4e 55 4c 4c 3b 0a 0a retval = NULL;..
24b0: 09 61 64 64 72 5f 76 36 20 3d 20 28 73 74 72 75 .addr_v6 = (stru
24c0: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 ct sockaddr_in6
24d0: 2a 29 20 61 64 64 72 3b 0a 0a 09 73 77 69 74 63 *) addr;...switc
24e0: 68 20 28 61 64 64 72 5f 76 36 2d 3e 73 69 6e 36 h (addr_v6->sin6
24f0: 5f 66 61 6d 69 6c 79 29 20 7b 0a 09 09 63 61 73 _family) {...cas
2500: 65 20 41 46 5f 49 4e 45 54 3a 0a 09 09 09 61 64 e AF_INET:....ad
2510: 64 72 5f 76 34 20 3d 20 28 73 74 72 75 63 74 20 dr_v4 = (struct
2520: 73 6f 63 6b 61 64 64 72 5f 69 6e 20 2a 29 20 61 sockaddr_in *) a
2530: 64 64 72 3b 0a 09 09 09 72 65 74 76 61 6c 20 3d ddr;....retval =
2540: 20 69 6e 65 74 5f 6e 74 6f 70 28 41 46 5f 49 4e inet_ntop(AF_IN
2550: 45 54 2c 20 26 61 64 64 72 5f 76 34 2d 3e 73 69 ET, &addr_v4->si
2560: 6e 5f 61 64 64 72 2c 20 62 75 66 66 65 72 2c 20 n_addr, buffer,
2570: 62 75 66 66 65 72 6c 65 6e 29 3b 0a 09 09 09 62 bufferlen);....b
2580: 72 65 61 6b 3b 0a 09 09 63 61 73 65 20 41 46 5f reak;...case AF_
2590: 49 4e 45 54 36 3a 0a 09 09 09 72 65 74 76 61 6c INET6:....retval
25a0: 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 41 46 5f = inet_ntop(AF_
25b0: 49 4e 45 54 36 2c 20 26 61 64 64 72 5f 76 36 2d INET6, &addr_v6-
25c0: 3e 73 69 6e 36 5f 61 64 64 72 2c 20 62 75 66 66 >sin6_addr, buff
25d0: 65 72 2c 20 62 75 66 66 65 72 6c 65 6e 29 3b 0a er, bufferlen);.
25e0: 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a 0a 09 72 ...break;..}...r
25f0: 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d eturn(retval);.}
2600: 0a 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a 66 ..static FILE *f
2610: 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 63 6f iled_log_open(co
2620: 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 29 20 nst char *file)
2630: 7b 0a 09 46 49 4c 45 20 2a 72 65 74 76 61 6c 3b {..FILE *retval;
2640: 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 28 66 69 ...if (strcmp(fi
2650: 6c 65 2c 20 22 2d 22 29 20 3d 3d 20 30 29 20 7b le, "-") == 0) {
2660: 0a 09 09 72 65 74 76 61 6c 20 3d 20 73 74 64 6f ...retval = stdo
2670: 75 74 3b 0a 09 7d 20 65 6c 73 65 20 69 66 20 28 ut;..} else if (
2680: 66 69 6c 65 5b 30 5d 20 3d 3d 20 27 7c 27 29 20 file[0] == '|')
2690: 7b 0a 09 09 66 69 6c 65 2b 2b 3b 0a 09 09 72 65 {...file++;...re
26a0: 74 76 61 6c 20 3d 20 70 6f 70 65 6e 28 66 69 6c tval = popen(fil
26b0: 65 2c 20 22 77 22 29 3b 0a 09 7d 20 65 6c 73 65 e, "w");..} else
26c0: 20 7b 0a 09 09 72 65 74 76 61 6c 20 3d 20 66 6f {...retval = fo
26d0: 70 65 6e 28 66 69 6c 65 2c 20 22 61 2b 22 29 3b pen(file, "a+");
26e0: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 ..}...return(ret
26f0: 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 val);.}..static
2700: 69 6e 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e int filed_loggin
2710: 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 46 49 g_thread_init(FI
2720: 4c 45 20 2a 6c 6f 67 66 70 29 20 7b 0a 09 73 74 LE *logfp) {..st
2730: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 ruct filed_loggi
2740: 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a ng_thread_args *
2750: 61 72 67 73 3b 0a 09 70 74 68 72 65 61 64 5f 74 args;..pthread_t
2760: 20 74 68 72 65 61 64 5f 69 64 3b 0a 0a 09 61 72 thread_id;...ar
2770: 67 73 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 gs = malloc(size
2780: 6f 66 28 2a 61 72 67 73 29 29 3b 0a 09 61 72 67 of(*args));..arg
2790: 73 2d 3e 66 70 20 3d 20 6c 6f 67 66 70 3b 0a 0a s->fp = logfp;..
27a0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c .filed_log_msg_l
27b0: 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 70 74 ist = NULL;...pt
27c0: 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 hread_mutex_init
27d0: 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f (&filed_log_msg_
27e0: 6c 69 73 74 5f 6d 75 74 65 78 2c 20 4e 55 4c 4c list_mutex, NULL
27f0: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 72 65 );...pthread_cre
2800: 61 74 65 28 26 74 68 72 65 61 64 5f 69 64 2c 20 ate(&thread_id,
2810: 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 6c 6f 67 67 NULL, filed_logg
2820: 69 6e 67 5f 74 68 72 65 61 64 2c 20 61 72 67 73 ing_thread, args
2830: 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d );...filed_log_m
2840: 73 67 28 22 53 54 41 52 54 22 29 3b 0a 0a 09 72 sg("START");...r
2850: 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 23 65 6e 64 eturn(0);.}.#end
2860: 69 66 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20 74 69 if../* Format ti
2870: 6d 65 20 70 65 72 20 52 46 43 32 36 31 36 20 2a me per RFC2616 *
2880: 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 /.static char *f
2890: 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 iled_format_time
28a0: 28 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 73 (char *buffer, s
28b0: 69 7a 65 5f 74 20 62 75 66 66 65 72 5f 6c 65 6e ize_t buffer_len
28c0: 2c 20 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 74 , const time_t t
28d0: 69 6d 65 69 6e 66 6f 29 20 7b 0a 09 73 74 72 75 imeinfo) {..stru
28e0: 63 74 20 74 6d 20 74 69 6d 65 69 6e 66 6f 5f 74 ct tm timeinfo_t
28f0: 6d 2c 20 2a 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f m, *timeinfo_tm_
2900: 70 3b 0a 0a 09 74 69 6d 65 69 6e 66 6f 5f 74 6d p;...timeinfo_tm
2910: 5f 70 20 3d 20 67 6d 74 69 6d 65 5f 72 28 26 74 _p = gmtime_r(&t
2920: 69 6d 65 69 6e 66 6f 2c 20 26 74 69 6d 65 69 6e imeinfo, &timein
2930: 66 6f 5f 74 6d 29 3b 0a 09 69 66 20 28 74 69 6d fo_tm);..if (tim
2940: 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d 3d 20 4e 55 einfo_tm_p == NU
2950: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 22 LL) {...return("
2960: 75 6e 6b 6e 6f 77 6e 22 29 3b 0a 09 7d 0a 0a 09 unknown");..}...
2970: 62 75 66 66 65 72 5b 62 75 66 66 65 72 5f 6c 65 buffer[buffer_le
2980: 6e 20 2d 20 31 5d 20 3d 20 27 5c 30 27 3b 0a 09 n - 1] = '\0';..
2990: 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 74 72 buffer_len = str
29a0: 66 74 69 6d 65 28 62 75 66 66 65 72 2c 20 62 75 ftime(buffer, bu
29b0: 66 66 65 72 5f 6c 65 6e 20 2d 20 31 2c 20 22 25 ffer_len - 1, "%
29c0: 61 2c 20 25 64 20 25 62 20 25 59 20 25 48 3a 25 a, %d %b %Y %H:%
29d0: 4d 3a 25 53 20 47 4d 54 22 2c 20 74 69 6d 65 69 M:%S GMT", timei
29e0: 6e 66 6f 5f 74 6d 5f 70 29 3b 0a 0a 09 72 65 74 nfo_tm_p);...ret
29f0: 75 72 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a 0a urn(buffer);.}..
2a00: 2f 2a 20 68 61 73 68 20 2a 2f 0a 73 74 61 74 69 /* hash */.stati
2a10: 63 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 66 c unsigned int f
2a20: 69 6c 65 64 5f 68 61 73 68 28 63 6f 6e 73 74 20 iled_hash(const
2a30: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76 unsigned char *v
2a40: 61 6c 75 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 alue, unsigned i
2a50: 6e 74 20 6d 6f 64 75 6c 75 73 29 20 7b 0a 09 75 nt modulus) {..u
2a60: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 75 72 nsigned char cur
2a70: 72 2c 20 70 72 65 76 3b 0a 09 69 6e 74 20 64 69 r, prev;..int di
2a80: 66 66 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e ff;..unsigned in
2a90: 74 20 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76 t retval;...retv
2aa0: 61 6c 20 3d 20 6d 6f 64 75 6c 75 73 20 2d 20 31 al = modulus - 1
2ab0: 3b 0a 09 70 72 65 76 20 3d 20 6d 6f 64 75 6c 75 ;..prev = modulu
2ac0: 73 20 25 20 32 35 35 3b 0a 0a 09 77 68 69 6c 65 s % 255;...while
2ad0: 20 28 28 63 75 72 72 20 3d 20 2a 76 61 6c 75 65 ((curr = *value
2ae0: 29 29 20 7b 0a 09 09 69 66 20 28 63 75 72 72 20 )) {...if (curr
2af0: 3c 20 33 32 29 20 7b 0a 09 09 09 63 75 72 72 20 < 32) {....curr
2b00: 3d 20 32 35 35 20 2d 20 63 75 72 72 3b 0a 09 09 = 255 - curr;...
2b10: 7d 20 65 6c 73 65 20 7b 0a 09 09 09 63 75 72 72 } else {....curr
2b20: 20 2d 3d 20 33 32 3b 0a 09 09 7d 0a 0a 09 09 69 -= 32;...}....i
2b30: 66 20 28 70 72 65 76 20 3c 20 63 75 72 72 29 20 f (prev < curr)
2b40: 7b 0a 09 09 09 64 69 66 66 20 3d 20 63 75 72 72 {....diff = curr
2b50: 20 2d 20 70 72 65 76 3b 0a 09 09 7d 20 65 6c 73 - prev;...} els
2b60: 65 20 7b 0a 09 09 09 64 69 66 66 20 3d 20 70 72 e {....diff = pr
2b70: 65 76 20 2d 20 63 75 72 72 3b 0a 09 09 7d 0a 0a ev - curr;...}..
2b80: 09 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 0a ..prev = curr;..
2b90: 09 09 72 65 74 76 61 6c 20 3c 3c 3d 20 33 3b 0a ..retval <<= 3;.
2ba0: 09 09 72 65 74 76 61 6c 20 26 3d 20 30 78 46 46 ..retval &= 0xFF
2bb0: 46 46 46 46 46 46 4c 55 3b 0a 09 09 72 65 74 76 FFFFFFLU;...retv
2bc0: 61 6c 20 5e 3d 20 64 69 66 66 3b 0a 0a 09 09 76 al ^= diff;....v
2bd0: 61 6c 75 65 2b 2b 3b 0a 09 7d 0a 0a 09 72 65 74 alue++;..}...ret
2be0: 76 61 6c 20 3d 20 72 65 74 76 61 6c 20 25 20 6d val = retval % m
2bf0: 6f 64 75 6c 75 73 3b 0a 0a 09 72 65 74 75 72 6e odulus;...return
2c00: 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 2f 2a 20 (retval);.}../*
2c10: 46 69 6e 64 20 61 20 6d 69 6d 65 2d 74 79 70 65 Find a mime-type
2c20: 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66 69 based on the fi
2c30: 6c 65 6e 61 6d 65 20 2a 2f 0a 73 74 61 74 69 63 lename */.static
2c40: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
2c50: 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f 6d 69 6d ed_determine_mim
2c60: 65 74 79 70 65 28 63 6f 6e 73 74 20 63 68 61 72 etype(const char
2c70: 20 2a 70 61 74 68 29 20 7b 0a 09 63 6f 6e 73 74 *path) {..const
2c80: 20 63 68 61 72 20 2a 70 3b 0a 0a 09 70 20 3d 20 char *p;...p =
2c90: 73 74 72 72 63 68 72 28 70 61 74 68 2c 20 27 2e strrchr(path, '.
2ca0: 27 29 3b 0a 09 69 66 20 28 70 20 3d 3d 20 4e 55 ');..if (p == NU
2cb0: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 46 LL) {...return(F
2cc0: 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 ILED_DEFAULT_TYP
2cd0: 45 29 3b 0a 09 7d 0a 0a 09 70 2b 2b 3b 0a 09 69 E);..}...p++;..i
2ce0: 66 20 28 2a 70 20 3d 3d 20 27 5c 30 27 29 20 7b f (*p == '\0') {
2cf0: 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f ...return(FILED_
2d00: 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 09 DEFAULT_TYPE);..
2d10: 7d 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 }...filed_log_ms
2d20: 67 5f 64 65 62 75 67 28 22 4c 6f 6f 6b 69 6e 67 g_debug("Looking
2d30: 20 75 70 20 4d 49 4d 45 20 74 79 70 65 20 66 6f up MIME type fo
2d40: 72 20 25 73 20 28 68 61 73 68 20 3d 20 25 6c 6c r %s (hash = %ll
2d50: 75 29 22 2c 20 70 2c 20 28 75 6e 73 69 67 6e 65 u)", p, (unsigne
2d60: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c d long long) fil
2d70: 65 64 5f 68 61 73 68 28 28 63 6f 6e 73 74 20 75 ed_hash((const u
2d80: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 nsigned char *)
2d90: 70 2c 20 31 36 37 37 37 32 35 39 29 29 3b 0a 0a p, 16777259));..
2da0: 23 69 6e 63 6c 75 64 65 20 22 66 69 6c 65 64 2d #include "filed-
2db0: 6d 69 6d 65 2d 74 79 70 65 73 2e 68 22 0a 0a 09 mime-types.h"...
2dc0: 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 return(FILED_DEF
2dd0: 41 55 4c 54 5f 54 59 50 45 29 3b 0a 7d 0a 0a 2f AULT_TYPE);.}../
2de0: 2a 20 47 65 6e 65 72 61 74 65 20 61 20 75 6e 69 * Generate a uni
2df0: 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 2a que identifier *
2e00: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 /.static void fi
2e10: 6c 65 64 5f 67 65 6e 65 72 61 74 65 5f 65 74 61 led_generate_eta
2e20: 67 28 63 68 61 72 20 2a 65 74 61 67 2c 20 73 69 g(char *etag, si
2e30: 7a 65 5f 74 20 6c 65 6e 67 74 68 29 20 7b 0a 09 ze_t length) {..
2e40: 73 6e 70 72 69 6e 74 66 28 65 74 61 67 2c 20 6c snprintf(etag, l
2e50: 65 6e 67 74 68 2c 20 22 25 6c 6c 78 2d 25 6c 6c ength, "%llx-%ll
2e60: 78 25 6c 6c 78 25 6c 6c 78 25 6c 6c 78 22 2c 0a x%llx%llx%llx",.
2e70: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
2e80: 20 6c 6f 6e 67 29 20 74 69 6d 65 28 4e 55 4c 4c long) time(NULL
2e90: 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c ),...(unsigned l
2ea0: 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d ong long) random
2eb0: 28 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 (),...(unsigned
2ec0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f long long) rando
2ed0: 6d 28 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 m(),...(unsigned
2ee0: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 long long) rand
2ef0: 6f 6d 28 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 om(),...(unsigne
2f00: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e d long long) ran
2f10: 64 6f 6d 28 29 0a 09 29 3b 0a 7d 0a 0a 2f 2a 20 dom()..);.}../*
2f20: 4f 70 65 6e 20 61 20 66 69 6c 65 20 61 6e 64 20 Open a file and
2f30: 72 65 74 75 72 6e 20 66 69 6c 65 20 69 6e 66 6f return file info
2f40: 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 rmation */.stati
2f50: 63 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 c struct filed_f
2f60: 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 64 5f 6f ileinfo *filed_o
2f70: 70 65 6e 5f 66 69 6c 65 28 63 6f 6e 73 74 20 63 pen_file(const c
2f80: 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 75 63 har *path, struc
2f90: 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f t filed_fileinfo
2fa0: 20 2a 62 75 66 66 65 72 29 20 7b 0a 09 73 74 72 *buffer) {..str
2fb0: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e uct filed_filein
2fc0: 66 6f 20 2a 63 61 63 68 65 3b 0a 09 75 6e 73 69 fo *cache;..unsi
2fd0: 67 6e 65 64 20 69 6e 74 20 63 61 63 68 65 5f 69 gned int cache_i
2fe0: 64 78 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a dx;..off_t len;.
2ff0: 09 69 6e 74 20 66 64 3b 0a 0a 09 63 61 63 68 65 .int fd;...cache
3000: 5f 69 64 78 20 3d 20 66 69 6c 65 64 5f 68 61 73 _idx = filed_has
3010: 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 h((const unsigne
3020: 64 20 63 68 61 72 20 2a 29 20 70 61 74 68 2c 20 d char *) path,
3030: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
3040: 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 0a 09 dcache_size);...
3050: 63 61 63 68 65 20 3d 20 26 66 69 6c 65 64 5f 66 cache = &filed_f
3060: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b ileinfo_fdcache[
3070: 63 61 63 68 65 5f 69 64 78 5d 3b 0a 0a 09 66 69 cache_idx];...fi
3080: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 led_log_msg_debu
3090: 67 28 22 4c 6f 63 6b 69 6e 67 20 6d 75 74 65 78 g("Locking mutex
30a0: 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 22 2c 20 for idx: %lu",
30b0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 (unsigned long)
30c0: 63 61 63 68 65 5f 69 64 78 29 3b 0a 0a 09 70 74 cache_idx);...pt
30d0: 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b hread_mutex_lock
30e0: 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b (&cache->mutex);
30f0: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ...filed_log_msg
3100: 5f 64 65 62 75 67 28 22 43 6f 6d 70 6c 65 74 65 _debug("Complete
3110: 64 20 6c 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20 d locking mutex
3120: 66 6f 72 20 69 64 78 3a 20 25 6c 75 22 2c 20 28 for idx: %lu", (
3130: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 unsigned long) c
3140: 61 63 68 65 5f 69 64 78 29 3b 0a 0a 09 69 66 20 ache_idx);...if
3150: 28 73 74 72 63 6d 70 28 70 61 74 68 2c 20 63 61 (strcmp(path, ca
3160: 63 68 65 2d 3e 70 61 74 68 29 20 21 3d 20 30 29 che->path) != 0)
3170: 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d {...filed_log_m
3180: 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65 20 sg_debug("Cache
3190: 6d 69 73 73 20 66 6f 72 20 69 64 78 3a 20 25 6c miss for idx: %l
31a0: 75 3a 20 4f 4c 44 20 5c 22 25 73 5c 22 2c 20 4e u: OLD \"%s\", N
31b0: 45 57 20 5c 22 25 73 5c 22 22 2c 20 28 75 6e 73 EW \"%s\"", (uns
31c0: 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 igned long) cach
31d0: 65 5f 69 64 78 2c 20 63 61 63 68 65 2d 3e 70 61 e_idx, cache->pa
31e0: 74 68 2c 20 70 61 74 68 29 3b 0a 0a 09 09 66 64 th, path);....fd
31f0: 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f = open(path, O_
3200: 52 44 4f 4e 4c 59 20 7c 20 4f 5f 4c 41 52 47 45 RDONLY | O_LARGE
3210: 46 49 4c 45 29 3b 0a 09 09 69 66 20 28 66 64 20 FILE);...if (fd
3220: 3c 20 30 29 20 7b 0a 09 09 09 70 74 68 72 65 61 < 0) {....pthrea
3230: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
3240: 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a cache->mutex);..
3250: 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b ...return(NULL);
3260: 0a 09 09 7d 0a 0a 09 09 69 66 20 28 63 61 63 68 ...}....if (cach
3270: 65 2d 3e 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09 e->fd >= 0) {...
3280: 09 63 6c 6f 73 65 28 63 61 63 68 65 2d 3e 66 64 .close(cache->fd
3290: 29 3b 0a 09 09 7d 0a 0a 09 09 6c 65 6e 20 3d 20 );...}....len =
32a0: 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 53 45 45 lseek(fd, 0, SEE
32b0: 4b 5f 45 4e 44 29 3b 0a 09 09 6c 73 65 65 6b 28 K_END);...lseek(
32c0: 66 64 2c 20 30 2c 20 53 45 45 4b 5f 53 45 54 29 fd, 0, SEEK_SET)
32d0: 3b 0a 0a 09 09 63 61 63 68 65 2d 3e 66 64 20 3d ;....cache->fd =
32e0: 20 66 64 3b 0a 09 09 63 61 63 68 65 2d 3e 6c 65 fd;...cache->le
32f0: 6e 20 3d 20 6c 65 6e 3b 0a 09 09 73 74 72 63 70 n = len;...strcp
3300: 79 28 63 61 63 68 65 2d 3e 70 61 74 68 2c 20 70 y(cache->path, p
3310: 61 74 68 29 3b 0a 09 09 63 61 63 68 65 2d 3e 74 ath);...cache->t
3320: 79 70 65 20 3d 20 66 69 6c 65 64 5f 64 65 74 65 ype = filed_dete
3330: 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 70 rmine_mimetype(p
3340: 61 74 68 29 3b 0a 09 09 66 69 6c 65 64 5f 67 65 ath);...filed_ge
3350: 6e 65 72 61 74 65 5f 65 74 61 67 28 63 61 63 68 nerate_etag(cach
3360: 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f 66 28 e->etag, sizeof(
3370: 63 61 63 68 65 2d 3e 65 74 61 67 29 29 3b 0a 0a cache->etag));..
3380: 09 09 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 44 ../* XXX:TODO: D
3390: 65 74 65 72 6d 69 6e 65 20 2a 2f 0a 09 09 63 61 etermine */...ca
33a0: 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20 66 che->lastmod = f
33b0: 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 iled_format_time
33c0: 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f (cache->lastmod_
33d0: 62 2c 20 73 69 7a 65 6f 66 28 63 61 63 68 65 2d b, sizeof(cache-
33e0: 3e 6c 61 73 74 6d 6f 64 5f 62 29 2c 20 74 69 6d >lastmod_b), tim
33f0: 65 28 4e 55 4c 4c 29 20 2d 20 33 30 29 3b 0a 09 e(NULL) - 30);..
3400: 7d 20 65 6c 73 65 20 7b 0a 09 09 66 69 6c 65 64 } else {...filed
3410: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 _log_msg_debug("
3420: 43 61 63 68 65 20 68 69 74 20 66 6f 72 20 69 64 Cache hit for id
3430: 78 3a 20 25 6c 75 3a 20 50 41 54 48 20 5c 22 25 x: %lu: PATH \"%
3440: 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 s\"", (unsigned
3450: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 2c long) cache_idx,
3460: 20 70 61 74 68 29 3b 0a 09 7d 0a 0a 09 2f 2a 0a path);..}.../*.
3470: 09 20 2a 20 57 65 20 68 61 76 65 20 74 6f 20 6d . * We have to m
3480: 61 6b 65 20 61 20 64 75 70 6c 69 63 61 74 65 20 ake a duplicate
3490: 46 44 2c 20 62 65 63 61 75 73 65 20 6f 6e 63 65 FD, because once
34a0: 20 77 65 20 72 65 6c 65 61 73 65 20 74 68 65 20 we release the
34b0: 63 61 63 68 65 0a 09 20 2a 20 6d 75 74 65 78 2c cache.. * mutex,
34c0: 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
34d0: 70 74 6f 72 20 6d 61 79 20 62 65 20 63 6c 6f 73 ptor may be clos
34e0: 65 64 0a 09 20 2a 2f 0a 09 66 64 20 3d 20 64 75 ed.. */..fd = du
34f0: 70 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 69 p(cache->fd);..i
3500: 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 70 f (fd < 0) {...p
3510: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c thread_mutex_unl
3520: 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 ock(&cache->mute
3530: 78 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 4e 55 x);....return(NU
3540: 4c 4c 29 3b 0a 09 7d 0a 0a 09 62 75 66 66 65 72 LL);..}...buffer
3550: 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 62 75 66 66 ->fd = fd;..buff
3560: 65 72 2d 3e 6c 65 6e 20 3d 20 63 61 63 68 65 2d er->len = cache-
3570: 3e 6c 65 6e 3b 0a 09 62 75 66 66 65 72 2d 3e 74 >len;..buffer->t
3580: 79 70 65 20 3d 20 63 61 63 68 65 2d 3e 74 79 70 ype = cache->typ
3590: 65 3b 0a 09 6d 65 6d 63 70 79 28 62 75 66 66 65 e;..memcpy(buffe
35a0: 72 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 63 61 r->lastmod_b, ca
35b0: 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 che->lastmod_b,
35c0: 73 69 7a 65 6f 66 28 62 75 66 66 65 72 2d 3e 6c sizeof(buffer->l
35d0: 61 73 74 6d 6f 64 5f 62 29 29 3b 0a 09 6d 65 6d astmod_b));..mem
35e0: 63 70 79 28 62 75 66 66 65 72 2d 3e 65 74 61 67 cpy(buffer->etag
35f0: 2c 20 63 61 63 68 65 2d 3e 65 74 61 67 2c 20 73 , cache->etag, s
3600: 69 7a 65 6f 66 28 62 75 66 66 65 72 2d 3e 65 74 izeof(buffer->et
3610: 61 67 29 29 3b 0a 09 62 75 66 66 65 72 2d 3e 6c ag));..buffer->l
3620: 61 73 74 6d 6f 64 20 3d 20 62 75 66 66 65 72 2d astmod = buffer-
3630: 3e 6c 61 73 74 6d 6f 64 5f 62 20 2b 20 28 63 61 >lastmod_b + (ca
3640: 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 2d 20 63 che->lastmod - c
3650: 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 ache->lastmod_b)
3660: 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 65 ;...pthread_mute
3670: 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d x_unlock(&cache-
3680: 3e 6d 75 74 65 78 29 3b 0a 0a 09 72 65 74 75 72 >mutex);...retur
3690: 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a n(buffer);.}../*
36a0: 20 50 72 6f 63 65 73 73 20 61 6e 20 48 54 54 50 Process an HTTP
36b0: 20 72 65 71 75 65 73 74 20 61 6e 64 20 72 65 74 request and ret
36c0: 75 72 6e 20 74 68 65 20 70 61 74 68 20 72 65 71 urn the path req
36d0: 75 65 73 74 65 64 20 2a 2f 0a 73 74 61 74 69 63 uested */.static
36e0: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 struct filed_ht
36f0: 74 70 5f 72 65 71 75 65 73 74 20 2a 66 69 6c 65 tp_request *file
3700: 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71 75 65 d_get_http_reque
3710: 73 74 28 46 49 4c 45 20 2a 66 70 2c 20 73 74 72 st(FILE *fp, str
3720: 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 uct filed_http_r
3730: 65 71 75 65 73 74 20 2a 62 75 66 66 65 72 5f 73 equest *buffer_s
3740: 74 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f t, struct filed_
3750: 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 options *options
3760: 29 20 7b 0a 09 63 68 61 72 20 2a 6d 65 74 68 6f ) {..char *metho
3770: 64 2c 20 2a 70 61 74 68 3b 0a 09 63 68 61 72 20 d, *path;..char
3780: 2a 62 75 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 *buffer, *workbu
3790: 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 ffer, *workbuffe
37a0: 72 5f 6e 65 78 74 3b 0a 09 63 68 61 72 20 2a 66 r_next;..char *f
37b0: 67 65 74 73 5f 72 65 74 3b 0a 09 73 69 7a 65 5f gets_ret;..size_
37c0: 74 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 70 61 t buffer_len, pa
37d0: 74 68 5f 6c 65 6e 3b 0a 09 6f 66 66 5f 74 20 72 th_len;..off_t r
37e0: 61 6e 67 65 5f 73 74 61 72 74 2c 20 72 61 6e 67 ange_start, rang
37f0: 65 5f 65 6e 64 2c 20 72 61 6e 67 65 5f 6c 65 6e e_end, range_len
3800: 67 74 68 3b 0a 09 69 6e 74 20 72 61 6e 67 65 5f gth;..int range_
3810: 72 65 71 75 65 73 74 3b 0a 09 69 6e 74 20 73 6e request;..int sn
3820: 70 72 69 6e 74 66 5f 72 65 74 3b 0a 09 69 6e 74 printf_ret;..int
3830: 20 69 3b 0a 0a 09 2f 2a 20 53 65 74 20 74 6f 20 i;.../* Set to
3840: 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a default values *
3850: 2f 0a 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d /..range_start =
3860: 20 30 3b 0a 09 72 61 6e 67 65 5f 65 6e 64 20 20 0;..range_end
3870: 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 72 65 71 = 0;..range_req
3880: 75 65 73 74 20 3d 20 30 3b 0a 09 72 61 6e 67 65 uest = 0;..range
3890: 5f 6c 65 6e 67 74 68 20 3d 20 2d 31 3b 0a 09 62 _length = -1;..b
38a0: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 uffer_st->header
38b0: 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d s.host.present =
38c0: 20 30 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 62 0;...buffer = b
38d0: 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 uffer_st->tmpbuf
38e0: 3b 0a 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 ;..buffer_len =
38f0: 73 69 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 74 sizeof(buffer_st
3900: 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 66 67 65 ->tmpbuf);...fge
3910: 74 73 5f 72 65 74 20 3d 20 66 67 65 74 73 28 62 ts_ret = fgets(b
3920: 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 uffer, buffer_le
3930: 6e 2c 20 66 70 29 3b 0a 09 69 66 20 28 66 67 65 n, fp);..if (fge
3940: 74 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 ts_ret == NULL)
3950: 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 {...return(NULL)
3960: 3b 0a 09 7d 0a 0a 09 6d 65 74 68 6f 64 20 3d 20 ;..}...method =
3970: 62 75 66 66 65 72 3b 0a 0a 09 62 75 66 66 65 72 buffer;...buffer
3980: 20 3d 20 73 74 72 63 68 72 28 62 75 66 66 65 72 = strchr(buffer
3990: 2c 20 27 20 27 29 3b 0a 09 69 66 20 28 62 75 66 , ' ');..if (buf
39a0: 66 65 72 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 fer == NULL) {..
39b0: 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 .return(NULL);..
39c0: 7d 0a 0a 09 2a 62 75 66 66 65 72 20 3d 20 27 5c }...*buffer = '\
39d0: 30 27 3b 0a 09 62 75 66 66 65 72 2b 2b 3b 0a 0a 0';..buffer++;..
39e0: 09 70 61 74 68 20 3d 20 62 75 66 66 65 72 3b 0a .path = buffer;.
39f0: 0a 09 2f 2a 20 54 65 72 6d 69 6e 61 74 65 20 70 ../* Terminate p
3a00: 61 74 68 20 63 6f 6d 70 6f 6e 65 6e 74 20 2a 2f ath component */
3a10: 0a 09 62 75 66 66 65 72 20 3d 20 73 74 72 70 62 ..buffer = strpb
3a20: 72 6b 28 70 61 74 68 2c 20 22 5c 72 5c 6e 20 22 rk(path, "\r\n "
3a30: 29 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20 21 );..if (buffer !
3a40: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 62 75 66 = NULL) {...*buf
3a50: 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 62 75 fer = '\0';...bu
3a60: 66 66 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 2f 2a 20 ffer++;..}.../*
3a70: 57 65 20 6f 6e 6c 79 20 68 61 6e 64 6c 65 20 74 We only handle t
3a80: 68 65 20 22 47 45 54 22 20 61 6e 64 20 22 48 45 he "GET" and "HE
3a90: 41 44 27 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 09 AD' methods */..
3aa0: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 28 6d if (strcasecmp(m
3ab0: 65 74 68 6f 64 2c 20 22 68 65 61 64 22 29 20 21 ethod, "head") !
3ac0: 3d 20 30 29 20 7b 0a 09 09 69 66 20 28 73 74 72 = 0) {...if (str
3ad0: 63 61 73 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 casecmp(method,
3ae0: 22 67 65 74 22 29 20 21 3d 20 30 29 20 7b 0a 09 "get") != 0) {..
3af0: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a ..return(NULL);.
3b00: 09 09 7d 0a 0a 09 09 2f 2a 20 47 45 54 20 72 65 ..}..../* GET re
3b10: 71 75 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 quest */...buffe
3b20: 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 r_st->method = F
3b30: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 ILED_REQUEST_MET
3b40: 48 4f 44 5f 47 45 54 3b 0a 09 7d 20 65 6c 73 65 HOD_GET;..} else
3b50: 20 7b 0a 09 09 2f 2a 20 48 45 41 44 20 72 65 71 {.../* HEAD req
3b60: 75 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 72 uest */...buffer
3b70: 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 49 _st->method = FI
3b80: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 LED_REQUEST_METH
3b90: 4f 44 5f 48 45 41 44 3b 0a 09 7d 0a 0a 09 2f 2a OD_HEAD;..}.../*
3ba0: 20 4e 6f 74 65 20 70 61 74 68 20 2a 2f 0a 09 70 Note path */..p
3bb0: 61 74 68 5f 6c 65 6e 20 3d 20 73 74 72 6c 65 6e ath_len = strlen
3bc0: 28 70 61 74 68 29 3b 0a 09 6d 65 6d 63 70 79 28 (path);..memcpy(
3bd0: 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c buffer_st->path,
3be0: 20 70 61 74 68 2c 20 70 61 74 68 5f 6c 65 6e 20 path, path_len
3bf0: 2b 20 31 29 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 + 1);.../* Deter
3c00: 6d 69 6e 65 20 74 79 70 65 20 6f 66 20 72 65 71 mine type of req
3c10: 75 65 73 74 20 66 72 6f 6d 20 70 61 74 68 20 2a uest from path *
3c20: 2f 0a 09 69 66 20 28 70 61 74 68 5f 6c 65 6e 20 /..if (path_len
3c30: 3d 3d 20 30 29 20 7b 0a 09 09 62 75 66 66 65 72 == 0) {...buffer
3c40: 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 _st->type = FILE
3c50: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 D_REQUEST_TYPE_D
3c60: 49 52 45 43 54 4f 52 59 3b 0a 09 7d 20 65 6c 73 IRECTORY;..} els
3c70: 65 20 7b 0a 09 09 69 66 20 28 70 61 74 68 5b 70 e {...if (path[p
3c80: 61 74 68 5f 6c 65 6e 20 2d 20 31 5d 20 3d 3d 20 ath_len - 1] ==
3c90: 27 2f 27 29 20 7b 0a 09 09 09 62 75 66 66 65 72 '/') {....buffer
3ca0: 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 _st->type = FILE
3cb0: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 D_REQUEST_TYPE_D
3cc0: 49 52 45 43 54 4f 52 59 3b 0a 09 09 7d 20 65 6c IRECTORY;...} el
3cd0: 73 65 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 se {....buffer_s
3ce0: 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f t->type = FILED_
3cf0: 52 45 51 55 45 53 54 5f 54 59 50 45 5f 4f 54 48 REQUEST_TYPE_OTH
3d00: 45 52 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 ER;...}..}.../*
3d10: 52 65 73 65 74 20 62 75 66 66 65 72 20 66 6f 72 Reset buffer for
3d20: 20 6c 61 74 65 72 20 75 73 65 20 2a 2f 0a 09 62 later use */..b
3d30: 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 uffer = buffer_s
3d40: 74 2d 3e 74 6d 70 62 75 66 3b 0a 0a 09 66 6f 72 t->tmpbuf;...for
3d50: 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 31 30 30 (i = 0; i < 100
3d60: 3b 20 69 2b 2b 29 20 7b 0a 09 09 66 67 65 74 73 ; i++) {...fgets
3d70: 5f 72 65 74 20 3d 20 66 67 65 74 73 28 62 75 66 _ret = fgets(buf
3d80: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c fer, buffer_len,
3d90: 20 66 70 29 3b 0a 09 09 69 66 20 28 66 67 65 74 fp);...if (fget
3da0: 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b s_ret == NULL) {
3db0: 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a ....break;...}..
3dc0: 09 09 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d ..if (strncasecm
3dd0: 70 28 62 75 66 66 65 72 2c 20 22 52 61 6e 67 65 p(buffer, "Range
3de0: 3a 20 22 2c 20 37 29 20 3d 3d 20 30 29 20 7b 0a : ", 7) == 0) {.
3df0: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 ...workbuffer =
3e00: 62 75 66 66 65 72 20 2b 20 37 3b 0a 0a 09 09 09 buffer + 7;.....
3e10: 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 if (strncasecmp(
3e20: 77 6f 72 6b 62 75 66 66 65 72 2c 20 22 62 79 74 workbuffer, "byt
3e30: 65 73 3d 22 2c 20 36 29 20 3d 3d 20 30 29 20 7b es=", 6) == 0) {
3e40: 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 .....workbuffer
3e50: 2b 3d 20 36 3b 0a 0a 09 09 09 09 72 61 6e 67 65 += 6;......range
3e60: 5f 72 65 71 75 65 73 74 20 3d 20 31 3b 0a 0a 09 _request = 1;...
3e70: 09 09 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d ...range_start =
3e80: 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 strtoull(workbu
3e90: 66 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 ffer, &workbuffe
3ea0: 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 0a 09 09 r_next, 10);....
3eb0: 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 77 ..workbuffer = w
3ec0: 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a orkbuffer_next;.
3ed0: 0a 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 .....if (*workbu
3ee0: 66 66 65 72 20 3d 3d 20 27 2d 27 29 20 7b 0a 09 ffer == '-') {..
3ef0: 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b ....workbuffer++
3f00: 3b 0a 0a 09 09 09 09 09 69 66 20 28 2a 77 6f 72 ;.......if (*wor
3f10: 6b 62 75 66 66 65 72 20 21 3d 20 27 5c 72 27 20 kbuffer != '\r'
3f20: 26 26 20 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 && *workbuffer !
3f30: 3d 20 27 5c 6e 27 29 20 7b 0a 09 09 09 09 09 09 = '\n') {.......
3f40: 72 61 6e 67 65 5f 65 6e 64 20 3d 20 73 74 72 74 range_end = strt
3f50: 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c oull(workbuffer,
3f60: 20 26 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 &workbuffer_nex
3f70: 74 2c 20 31 30 29 3b 0a 09 09 09 09 09 7d 0a 09 t, 10);......}..
3f80: 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 20 65 6c ...}....}...} el
3f90: 73 65 20 69 66 20 28 73 74 72 6e 63 61 73 65 63 se if (strncasec
3fa0: 6d 70 28 62 75 66 66 65 72 2c 20 22 48 6f 73 74 mp(buffer, "Host
3fb0: 3a 20 22 2c 20 35 29 20 3d 3d 20 30 29 20 7b 0a : ", 5) == 0) {.
3fc0: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 ...buffer_st->he
3fd0: 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65 aders.host.prese
3fe0: 6e 74 20 3d 20 31 3b 0a 0a 09 09 09 77 6f 72 6b nt = 1;.....work
3ff0: 62 75 66 66 65 72 20 3d 20 73 74 72 70 62 72 6b buffer = strpbrk
4000: 28 62 75 66 66 65 72 20 2b 20 35 2c 20 22 5c 72 (buffer + 5, "\r
4010: 5c 6e 3a 22 29 3b 0a 09 09 09 69 66 20 28 77 6f \n:");....if (wo
4020: 72 6b 62 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c rkbuffer != NULL
4030: 29 20 7b 0a 09 09 09 09 2a 77 6f 72 6b 62 75 66 ) {.....*workbuf
4040: 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 09 7d fer = '\0';....}
4050: 0a 0a 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 .....workbuffer
4060: 3d 20 62 75 66 66 65 72 20 2b 20 35 3b 0a 09 09 = buffer + 5;...
4070: 09 77 68 69 6c 65 20 28 2a 77 6f 72 6b 62 75 66 .while (*workbuf
4080: 66 65 72 20 3d 3d 20 27 20 27 29 20 7b 0a 09 09 fer == ' ') {...
4090: 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a ..workbuffer++;.
40a0: 09 09 09 7d 0a 0a 09 09 09 73 74 72 63 70 79 28 ...}.....strcpy(
40b0: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
40c0: 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c 20 77 6f rs.host.host, wo
40d0: 72 6b 62 75 66 66 65 72 29 3b 0a 09 09 7d 0a 0a rkbuffer);...}..
40e0: 09 09 69 66 20 28 6d 65 6d 63 6d 70 28 62 75 66 ..if (memcmp(buf
40f0: 66 65 72 2c 20 22 5c 72 5c 6e 22 2c 20 32 29 20 fer, "\r\n", 2)
4100: 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72 65 61 6b == 0) {....break
4110: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 65 ;...}..}.../* De
4120: 74 65 72 6d 69 6e 65 20 72 61 6e 67 65 20 2a 2f termine range */
4130: 0a 09 69 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 ..if (range_end
4140: 21 3d 20 30 29 20 7b 0a 09 09 69 66 20 28 72 61 != 0) {...if (ra
4150: 6e 67 65 5f 65 6e 64 20 3c 3d 20 72 61 6e 67 65 nge_end <= range
4160: 5f 73 74 61 72 74 29 20 7b 0a 09 09 09 72 65 74 _start) {....ret
4170: 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a urn(NULL);...}..
4180: 09 09 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d ..range_length =
4190: 20 72 61 6e 67 65 5f 65 6e 64 20 2d 20 72 61 6e range_end - ran
41a0: 67 65 5f 73 74 61 72 74 3b 0a 0a 09 09 66 69 6c ge_start;....fil
41b0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
41c0: 28 22 43 6f 6d 70 75 74 69 6e 67 20 6c 65 6e 67 ("Computing leng
41d0: 74 68 20 70 61 72 61 6d 65 74 65 72 3a 20 25 6c th parameter: %l
41e0: 6c 75 20 3d 20 25 6c 6c 75 20 2d 20 25 6c 6c 75 lu = %llu - %llu
41f0: 22 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ",....(unsigned
4200: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 long long) range
4210: 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 28 75 6e 73 _length,....(uns
4220: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
4230: 20 72 61 6e 67 65 5f 65 6e 64 2c 0a 09 09 09 28 range_end,....(
4240: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
4250: 6e 67 29 20 72 61 6e 67 65 5f 73 74 61 72 74 0a ng) range_start.
4260: 09 09 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 46 69 6c ..);..}.../* Fil
4270: 6c 20 75 70 20 73 74 72 75 63 74 75 72 65 20 74 l up structure t
4280: 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 09 62 75 66 o return */..buf
4290: 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e fer_st->headers.
42a0: 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 20 3d 20 range.present =
42b0: 72 61 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a 09 range_request;..
42c0: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
42d0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 rs.range.offset
42e0: 20 3d 20 72 61 6e 67 65 5f 73 74 61 72 74 3b 0a = range_start;.
42f0: 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 .buffer_st->head
4300: 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 ers.range.length
4310: 20 20 3d 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 = range_length
4320: 3b 0a 0a 09 2f 2a 20 49 66 20 76 68 6f 73 74 73 ;.../* If vhosts
4330: 20 61 72 65 20 65 6e 61 62 6c 65 64 2c 20 63 6f are enabled, co
4340: 6d 70 75 74 65 20 6e 65 77 20 70 61 74 68 20 2a mpute new path *
4350: 2f 0a 09 69 66 20 28 6f 70 74 69 6f 6e 73 2d 3e /..if (options->
4360: 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 64 29 20 vhosts_enabled)
4370: 7b 0a 09 09 69 66 20 28 62 75 66 66 65 72 5f 73 {...if (buffer_s
4380: 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e t->headers.host.
4390: 70 72 65 73 65 6e 74 20 3d 3d 20 31 29 20 7b 0a present == 1) {.
43a0: 09 09 09 62 75 66 66 65 72 20 3d 20 62 75 66 66 ...buffer = buff
43b0: 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a 09 er_st->tmpbuf;..
43c0: 09 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 ..buffer_len = s
43d0: 69 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 74 2d izeof(buffer_st-
43e0: 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 09 09 73 6e >tmpbuf);.....sn
43f0: 70 72 69 6e 74 66 5f 72 65 74 20 3d 20 73 6e 70 printf_ret = snp
4400: 72 69 6e 74 66 28 62 75 66 66 65 72 2c 20 62 75 rintf(buffer, bu
4410: 66 66 65 72 5f 6c 65 6e 2c 20 22 2f 25 73 25 73 ffer_len, "/%s%s
4420: 25 73 22 2c 0a 09 09 09 09 62 75 66 66 65 72 5f %s",.....buffer_
4430: 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 st->headers.host
4440: 2e 68 6f 73 74 2c 0a 09 09 09 09 62 75 66 66 65 .host,.....buffe
4450: 72 5f 73 74 2d 3e 70 61 74 68 5b 30 5d 20 3d 3d r_st->path[0] ==
4460: 20 27 2f 27 20 3f 20 22 22 20 3a 20 22 2f 22 2c '/' ? "" : "/",
4470: 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e .....buffer_st->
4480: 70 61 74 68 0a 09 09 09 29 3b 0a 09 09 09 69 66 path....);....if
4490: 20 28 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3e (snprintf_ret >
44a0: 3d 20 30 29 20 7b 0a 09 09 09 09 69 66 20 28 28 = 0) {.....if ((
44b0: 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 20 73 (unsigned int) s
44c0: 6e 70 72 69 6e 74 66 5f 72 65 74 29 20 3c 20 62 nprintf_ret) < b
44d0: 75 66 66 65 72 5f 6c 65 6e 29 20 7b 0a 09 09 09 uffer_len) {....
44e0: 09 09 73 74 72 63 70 79 28 62 75 66 66 65 72 5f ..strcpy(buffer_
44f0: 73 74 2d 3e 70 61 74 68 2c 20 62 75 66 66 65 72 st->path, buffer
4500: 29 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 );.....}....}...
4510: 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 62 75 }..}...return(bu
4520: 66 66 65 72 5f 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 ffer_st);.}../*
4530: 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
4540: 70 61 67 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 page */.static v
4550: 6f 69 64 20 66 69 6c 65 64 5f 65 72 72 6f 72 5f oid filed_error_
4560: 70 61 67 65 28 46 49 4c 45 20 2a 66 70 2c 20 63 page(FILE *fp, c
4570: 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 65 5f onst char *date_
4580: 63 75 72 72 65 6e 74 2c 20 69 6e 74 20 65 72 72 current, int err
4590: 6f 72 5f 6e 75 6d 62 65 72 2c 20 69 6e 74 20 6d or_number, int m
45a0: 65 74 68 6f 64 2c 20 63 6f 6e 73 74 20 63 68 61 ethod, const cha
45b0: 72 20 2a 72 65 61 73 6f 6e 2c 20 73 74 72 75 63 r *reason, struc
45c0: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
45d0: 79 20 2a 6c 6f 67 29 20 7b 0a 09 63 68 61 72 20 y *log) {..char
45e0: 2a 65 72 72 6f 72 5f 73 74 72 69 6e 67 20 3d 20 *error_string =
45f0: 22 3c 68 74 6d 6c 3e 3c 68 65 61 64 3e 3c 74 69 "<html><head><ti
4600: 74 6c 65 3e 45 52 52 4f 52 3c 2f 74 69 74 6c 65 tle>ERROR</title
4610: 3e 3c 2f 68 65 61 64 3e 3c 62 6f 64 79 3e 55 6e ></head><body>Un
4620: 61 62 6c 65 20 74 6f 20 70 72 6f 63 65 73 73 20 able to process
4630: 72 65 71 75 65 73 74 3c 2f 62 6f 64 79 3e 3c 2f request</body></
4640: 68 74 6d 6c 3e 22 3b 0a 0a 09 66 70 72 69 6e 74 html>";...fprint
4650: 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 f(fp, "HTTP/1.1
4660: 25 69 20 4e 6f 74 20 4f 4b 5c 72 5c 6e 44 61 74 %i Not OK\r\nDat
4670: 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a e: %s\r\nServer:
4680: 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d filed\r\nLast-M
4690: 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 odified: %s\r\nC
46a0: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 ontent-Length: %
46b0: 6c 6c 75 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 llu\r\nContent-T
46c0: 79 70 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 ype: %s\r\nConne
46d0: 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e ction: close\r\n
46e0: 5c 72 5c 6e 22 2c 0a 09 09 65 72 72 6f 72 5f 6e \r\n",...error_n
46f0: 75 6d 62 65 72 2c 0a 09 09 64 61 74 65 5f 63 75 umber,...date_cu
4700: 72 72 65 6e 74 2c 0a 09 09 64 61 74 65 5f 63 75 rrent,...date_cu
4710: 72 72 65 6e 74 2c 0a 09 09 28 75 6e 73 69 67 6e rrent,...(unsign
4720: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 73 74 ed long long) st
4730: 72 6c 65 6e 28 65 72 72 6f 72 5f 73 74 72 69 6e rlen(error_strin
4740: 67 29 2c 0a 09 09 22 74 65 78 74 2f 68 74 6d 6c g),..."text/html
4750: 22 0a 09 29 3b 0a 0a 09 2f 2a 20 73 69 6c 65 6e "..);.../* silen
4760: 63 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 ce error string
4770: 66 6f 72 20 48 45 41 44 20 72 65 71 75 65 73 74 for HEAD request
4780: 73 20 2a 2f 0a 09 69 66 20 28 6d 65 74 68 6f 64 s */..if (method
4790: 20 21 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 != FILED_REQUES
47a0: 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 29 20 7b T_METHOD_HEAD) {
47b0: 0a 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 ...fprintf(fp, "
47c0: 25 73 22 2c 20 65 72 72 6f 72 5f 73 74 72 69 6e %s", error_strin
47d0: 67 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 4c 6f 67 20 g);..}.../* Log
47e0: 65 72 72 6f 72 20 2a 2f 0a 09 2f 2a 2a 20 72 65 error */../** re
47f0: 61 73 6f 6e 20 6d 75 73 74 20 70 6f 69 6e 74 20 ason must point
4800: 74 6f 20 61 20 67 6c 6f 62 61 6c 6c 79 20 61 6c to a globally al
4810: 6c 6f 63 61 74 65 64 20 76 61 6c 75 65 20 2a 2a located value **
4820: 2f 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d /..log->reason =
4830: 20 72 65 61 73 6f 6e 3b 0a 09 6c 6f 67 2d 3e 68 reason;..log->h
4840: 74 74 70 5f 63 6f 64 65 20 3d 20 65 72 72 6f 72 ttp_code = error
4850: 5f 6e 75 6d 62 65 72 3b 0a 0a 09 66 69 6c 65 64 _number;...filed
4860: 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b _log_entry(log);
4870: 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20 63 6f 6e 6e .../* Close conn
4880: 65 63 74 69 6f 6e 20 2a 2f 0a 09 66 63 6c 6f 73 ection */..fclos
4890: 65 28 66 70 29 3b 0a 0a 09 72 65 74 75 72 6e 3b e(fp);...return;
48a0: 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 .}../* Return a
48b0: 72 65 64 69 72 65 63 74 20 74 6f 20 69 6e 64 65 redirect to inde
48c0: 78 2e 68 74 6d 6c 20 2a 2f 0a 73 74 61 74 69 63 x.html */.static
48d0: 20 76 6f 69 64 20 66 69 6c 65 64 5f 72 65 64 69 void filed_redi
48e0: 72 65 63 74 5f 69 6e 64 65 78 28 46 49 4c 45 20 rect_index(FILE
48f0: 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *fp, const char
4900: 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 63 *date_current, c
4910: 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c onst char *path,
4920: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f struct filed_lo
4930: 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 29 20 7b 0a g_entry *log) {.
4940: 09 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 20 3d .int http_code =
4950: 20 33 30 31 3b 0a 09 66 70 72 69 6e 74 66 28 66 301;..fprintf(f
4960: 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20 p, "HTTP/1.1 %i
4970: 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 OK\r\nDate: %s\r
4980: 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c \nServer: filed\
4990: 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 r\nLast-Modified
49a0: 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d : %s\r\nContent-
49b0: 4c 65 6e 67 74 68 3a 20 30 5c 72 5c 6e 43 6f 6e Length: 0\r\nCon
49c0: 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c 72 nection: close\r
49d0: 5c 6e 4c 6f 63 61 74 69 6f 6e 3a 20 25 73 5c 72 \nLocation: %s\r
49e0: 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 68 74 74 70 5f \n\r\n",...http_
49f0: 63 6f 64 65 2c 0a 09 09 64 61 74 65 5f 63 75 72 code,...date_cur
4a00: 72 65 6e 74 2c 0a 09 09 64 61 74 65 5f 63 75 72 rent,...date_cur
4a10: 72 65 6e 74 2c 0a 09 09 22 69 6e 64 65 78 2e 68 rent,..."index.h
4a20: 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f 2a 20 4c 6f tml"..);.../* Lo
4a30: 67 20 72 65 64 69 72 65 63 74 20 2a 2f 0a 09 6c g redirect */..l
4a40: 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 72 65 og->reason = "re
4a50: 64 69 72 65 63 74 22 3b 0a 09 6c 6f 67 2d 3e 68 direct";..log->h
4a60: 74 74 70 5f 63 6f 64 65 20 3d 20 68 74 74 70 5f ttp_code = http_
4a70: 63 6f 64 65 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f code;...filed_lo
4a80: 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 g_entry(log);...
4a90: 2f 2a 20 43 6c 6f 73 65 20 63 6f 6e 6e 65 63 74 /* Close connect
4aa0: 69 6f 6e 20 2a 2f 0a 09 66 63 6c 6f 73 65 28 66 ion */..fclose(f
4ab0: 70 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 0a 09 p);...return;...
4ac0: 2f 2a 20 43 75 72 72 65 6e 74 6c 79 20 75 6e 75 /* Currently unu
4ad0: 73 65 64 3a 20 70 61 74 68 20 2a 2f 0a 09 70 61 sed: path */..pa
4ae0: 74 68 20 3d 20 70 61 74 68 3b 0a 7d 0a 0a 2f 2a th = path;.}../*
4af0: 20 48 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c 65 Handle a single
4b00: 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 61 20 request from a
4b10: 63 6c 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 63 client */.static
4b20: 20 76 6f 69 64 20 66 69 6c 65 64 5f 68 61 6e 64 void filed_hand
4b30: 6c 65 5f 63 6c 69 65 6e 74 28 69 6e 74 20 66 64 le_client(int fd
4b40: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 , struct filed_h
4b50: 74 74 70 5f 72 65 71 75 65 73 74 20 2a 72 65 71 ttp_request *req
4b60: 75 65 73 74 2c 20 73 74 72 75 63 74 20 66 69 6c uest, struct fil
4b70: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f ed_log_entry *lo
4b80: 67 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f g, struct filed_
4b90: 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 options *options
4ba0: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 ) {..struct file
4bb0: 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 d_fileinfo *file
4bc0: 69 6e 66 6f 3b 0a 09 73 73 69 7a 65 5f 74 20 73 info;..ssize_t s
4bd0: 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 73 69 endfile_ret;..si
4be0: 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f 73 69 ze_t sendfile_si
4bf0: 7a 65 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 64 66 ze;..off_t sendf
4c00: 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 ile_offset, send
4c10: 66 69 6c 65 5f 73 65 6e 74 2c 20 73 65 6e 64 66 file_sent, sendf
4c20: 69 6c 65 5f 6c 65 6e 3b 0a 09 63 68 61 72 20 2a ile_len;..char *
4c30: 70 61 74 68 3b 0a 09 63 68 61 72 20 2a 64 61 74 path;..char *dat
4c40: 65 5f 63 75 72 72 65 6e 74 2c 20 64 61 74 65 5f e_current, date_
4c50: 63 75 72 72 65 6e 74 5f 62 5b 36 34 5d 3b 0a 09 current_b[64];..
4c60: 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 int http_code;..
4c70: 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 2f 2a 20 44 FILE *fp;.../* D
4c80: 65 74 65 72 6d 69 6e 65 20 63 75 72 72 65 6e 74 etermine current
4c90: 20 74 69 6d 65 20 2a 2f 0a 09 64 61 74 65 5f 63 time */..date_c
4ca0: 75 72 72 65 6e 74 20 3d 20 66 69 6c 65 64 5f 66 urrent = filed_f
4cb0: 6f 72 6d 61 74 5f 74 69 6d 65 28 64 61 74 65 5f ormat_time(date_
4cc0: 63 75 72 72 65 6e 74 5f 62 2c 20 73 69 7a 65 6f current_b, sizeo
4cd0: 66 28 64 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 f(date_current_b
4ce0: 29 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 29 3b 0a ), time(NULL));.
4cf0: 0a 09 2f 2a 20 4f 70 65 6e 20 73 6f 63 6b 65 74 ../* Open socket
4d00: 20 61 73 20 41 4e 53 49 20 49 2f 4f 20 66 6f 72 as ANSI I/O for
4d10: 20 65 61 73 65 20 6f 66 20 75 73 65 20 2a 2f 0a ease of use */.
4d20: 09 66 70 20 3d 20 66 64 6f 70 65 6e 28 66 64 2c .fp = fdopen(fd,
4d30: 20 22 77 2b 62 22 29 3b 0a 09 69 66 20 28 66 70 "w+b");..if (fp
4d40: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 63 6c == NULL) {...cl
4d50: 6f 73 65 28 66 64 29 3b 0a 0a 09 09 6c 6f 67 2d ose(fd);....log-
4d60: 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 >buffer[0] = '\0
4d70: 27 3b 0a 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 ';...log->http_c
4d80: 6f 64 65 20 3d 20 2d 31 3b 0a 09 09 6c 6f 67 2d ode = -1;...log-
4d90: 3e 72 65 61 73 6f 6e 20 3d 20 22 66 64 6f 70 65 >reason = "fdope
4da0: 6e 5f 66 61 69 6c 65 64 22 3b 0a 0a 09 09 66 69 n_failed";....fi
4db0: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f led_log_entry(lo
4dc0: 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 3b 0a 09 g);....return;..
4dd0: 7d 0a 0a 09 72 65 71 75 65 73 74 20 3d 20 66 69 }...request = fi
4de0: 6c 65 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71 led_get_http_req
4df0: 75 65 73 74 28 66 70 2c 20 72 65 71 75 65 73 74 uest(fp, request
4e00: 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a 0a 09 69 66 , options);...if
4e10: 20 28 72 65 71 75 65 73 74 20 3d 3d 20 4e 55 4c (request == NUL
4e20: 4c 29 20 7b 0a 09 09 6c 6f 67 2d 3e 62 75 66 66 L) {...log->buff
4e30: 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 09 er[0] = '\0';...
4e40: 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 .filed_error_pag
4e50: 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 e(fp, date_curre
4e60: 6e 74 2c 20 35 30 30 2c 20 46 49 4c 45 44 5f 52 nt, 500, FILED_R
4e70: 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 EQUEST_METHOD_GE
4e80: 54 2c 20 22 66 6f 72 6d 61 74 22 2c 20 6c 6f 67 T, "format", log
4e90: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d );....return;..}
4ea0: 0a 0a 09 70 61 74 68 20 3d 20 72 65 71 75 65 73 ...path = reques
4eb0: 74 2d 3e 70 61 74 68 3b 0a 09 73 74 72 63 70 79 t->path;..strcpy
4ec0: 28 6c 6f 67 2d 3e 62 75 66 66 65 72 2c 20 70 61 (log->buffer, pa
4ed0: 74 68 29 3b 0a 09 6c 6f 67 2d 3e 6d 65 74 68 6f th);..log->metho
4ee0: 64 20 3d 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 d = request->met
4ef0: 68 6f 64 3b 0a 0a 09 2f 2a 20 49 66 20 74 68 65 hod;.../* If the
4f00: 20 72 65 71 75 65 73 74 65 64 20 70 61 74 68 20 requested path
4f10: 69 73 20 61 20 64 69 72 65 63 74 6f 72 79 2c 20 is a directory,
4f20: 72 65 64 69 72 65 63 74 20 74 6f 20 69 6e 64 65 redirect to inde
4f30: 78 20 70 61 67 65 20 2a 2f 0a 09 69 66 20 28 72 x page */..if (r
4f40: 65 71 75 65 73 74 2d 3e 74 79 70 65 20 3d 3d 20 equest->type ==
4f50: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 FILED_REQUEST_TY
4f60: 50 45 5f 44 49 52 45 43 54 4f 52 59 29 20 7b 0a PE_DIRECTORY) {.
4f70: 09 09 66 69 6c 65 64 5f 72 65 64 69 72 65 63 74 ..filed_redirect
4f80: 5f 69 6e 64 65 78 28 66 70 2c 20 64 61 74 65 5f _index(fp, date_
4f90: 63 75 72 72 65 6e 74 2c 20 70 61 74 68 2c 20 6c current, path, l
4fa0: 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 3b 0a og);....return;.
4fb0: 09 7d 0a 0a 09 66 69 6c 65 69 6e 66 6f 20 3d 20 .}...fileinfo =
4fc0: 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 28 filed_open_file(
4fd0: 70 61 74 68 2c 20 26 72 65 71 75 65 73 74 2d 3e path, &request->
4fe0: 66 69 6c 65 69 6e 66 6f 29 3b 0a 09 69 66 20 28 fileinfo);..if (
4ff0: 66 69 6c 65 69 6e 66 6f 20 3d 3d 20 4e 55 4c 4c fileinfo == NULL
5000: 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65 72 72 6f ) {...filed_erro
5010: 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f r_page(fp, date_
5020: 63 75 72 72 65 6e 74 2c 20 34 30 34 2c 20 72 65 current, 404, re
5030: 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c 20 22 quest->method, "
5040: 6f 70 65 6e 5f 66 61 69 6c 65 64 22 2c 20 6c 6f open_failed", lo
5050: 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 3b 0a 09 g);....return;..
5060: 7d 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74 2d }...if (request-
5070: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 >headers.range.p
5080: 72 65 73 65 6e 74 29 20 7b 0a 09 09 69 66 20 28 resent) {...if (
5090: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
50a0: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 21 3d .range.offset !=
50b0: 20 30 20 7c 7c 20 72 65 71 75 65 73 74 2d 3e 68 0 || request->h
50c0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e eaders.range.len
50d0: 67 74 68 20 3e 3d 20 30 29 20 7b 0a 09 09 09 69 gth >= 0) {....i
50e0: 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 f (request->head
50f0: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 ers.range.offset
5100: 20 3e 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 >= fileinfo->le
5110: 6e 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f 65 n) {.....filed_e
5120: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 rror_page(fp, da
5130: 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 31 36 2c te_current, 416,
5140: 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 request->method
5150: 2c 20 22 72 61 6e 67 65 5f 69 6e 76 61 6c 69 64 , "range_invalid
5160: 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 09 09 63 6c ", log);......cl
5170: 6f 73 65 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 ose(fileinfo->fd
5180: 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 3b 0a );......return;.
5190: 09 09 09 7d 0a 0a 09 09 09 69 66 20 28 72 65 71 ...}.....if (req
51a0: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
51b0: 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 3d 20 28 28 nge.length == ((
51c0: 6f 66 66 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09 off_t) -1)) {...
51d0: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f ..filed_log_msg_
51e0: 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 6e 67 debug("Computing
51f0: 20 6c 65 6e 67 74 68 20 74 6f 20 66 69 74 20 69 length to fit i
5200: 6e 20 62 6f 75 6e 64 73 3a 20 66 69 6c 65 69 6e n bounds: filein
5210: 66 6f 2d 3e 6c 65 6e 20 3d 20 25 6c 6c 75 2c 20 fo->len = %llu,
5220: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
5230: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d 20 .range.offset =
5240: 25 6c 6c 75 22 2c 0a 09 09 09 09 09 28 75 6e 73 %llu",......(uns
5250: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
5260: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 2c 0a fileinfo->len,.
5270: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
5280: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 ong long) reques
5290: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
52a0: 2e 6f 66 66 73 65 74 0a 09 09 09 09 29 3b 0a 0a .offset.....);..
52b0: 09 09 09 09 72 65 71 75 65 73 74 2d 3e 68 65 61 ....request->hea
52c0: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 ders.range.lengt
52d0: 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 h = fileinfo->le
52e0: 6e 20 2d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 n - request->hea
52f0: 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 ders.range.offse
5300: 74 3b 0a 09 09 09 7d 0a 0a 09 09 09 66 69 6c 65 t;....}.....file
5310: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 d_log_msg_debug(
5320: 22 50 61 72 74 69 61 6c 20 72 65 71 75 65 73 74 "Partial request
5330: 2c 20 73 74 61 72 74 69 6e 67 20 61 74 3a 20 25 , starting at: %
5340: 6c 6c 75 20 61 6e 64 20 72 75 6e 6e 69 6e 67 20 llu and running
5350: 66 6f 72 20 25 6c 6c 69 20 62 79 74 65 73 22 2c for %lli bytes",
5360: 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
5370: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 ong long) reques
5380: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
5390: 2e 6f 66 66 73 65 74 2c 0a 09 09 09 09 28 6c 6f .offset,.....(lo
53a0: 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 ng long) request
53b0: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
53c0: 6c 65 6e 67 74 68 0a 09 09 09 29 3b 0a 0a 09 09 length....);....
53d0: 7d 0a 0a 09 09 68 74 74 70 5f 63 6f 64 65 20 3d }....http_code =
53e0: 20 32 30 36 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 206;..} else {.
53f0: 09 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 ..http_code = 20
5400: 30 3b 0a 0a 09 09 2f 2a 20 43 6f 6d 70 75 74 65 0;..../* Compute
5410: 20 66 61 6b 65 20 72 61 6e 67 65 20 70 61 72 61 fake range para
5420: 6d 65 74 65 72 73 20 74 68 61 74 20 69 6e 63 6c meters that incl
5430: 75 64 65 73 20 74 68 65 20 65 6e 74 69 72 65 20 udes the entire
5440: 66 69 6c 65 20 2a 2f 0a 09 09 72 65 71 75 65 73 file */...reques
5450: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
5460: 2e 6f 66 66 73 65 74 20 3d 20 30 3b 0a 09 09 72 .offset = 0;...r
5470: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
5480: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20 66 range.length = f
5490: 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 09 7d ileinfo->len;..}
54a0: 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 ...fprintf(fp, "
54b0: 48 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c 72 HTTP/1.1 %i OK\r
54c0: 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 \nDate: %s\r\nSe
54d0: 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c rver: filed\r\nL
54e0: 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 ast-Modified: %s
54f0: 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 \r\nContent-Leng
5500: 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 41 63 63 65 th: %llu\r\nAcce
5510: 70 74 2d 52 61 6e 67 65 73 3a 20 62 79 74 65 73 pt-Ranges: bytes
5520: 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 70 65 \r\nContent-Type
5530: 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 : %s\r\nConnecti
5540: 6f 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 45 54 61 on: close\r\nETa
5550: 67 3a 20 5c 22 25 73 5c 22 5c 72 5c 6e 22 2c 0a g: \"%s\"\r\n",.
5560: 09 09 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64 ..http_code,...d
5570: 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 66 ate_current,...f
5580: 69 6c 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d 6f 64 ileinfo->lastmod
5590: 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f ,...(unsigned lo
55a0: 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 ng long) request
55b0: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
55c0: 6c 65 6e 67 74 68 2c 0a 09 09 66 69 6c 65 69 6e length,...filein
55d0: 66 6f 2d 3e 74 79 70 65 2c 0a 09 09 66 69 6c 65 fo->type,...file
55e0: 69 6e 66 6f 2d 3e 65 74 61 67 0a 09 29 3b 0a 0a info->etag..);..
55f0: 09 69 66 20 28 68 74 74 70 5f 63 6f 64 65 20 3d .if (http_code =
5600: 3d 20 32 30 36 29 20 7b 0a 09 09 66 70 72 69 6e = 206) {...fprin
5610: 74 66 28 66 70 2c 20 22 43 6f 6e 74 65 6e 74 2d tf(fp, "Content-
5620: 52 61 6e 67 65 3a 20 62 79 74 65 73 20 25 6c 6c Range: bytes %ll
5630: 75 2d 25 6c 6c 75 2f 25 6c 6c 75 5c 72 5c 6e 22 u-%llu/%llu\r\n"
5640: 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c ,....(unsigned l
5650: 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 ong long) reques
5660: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
5670: 2e 6f 66 66 73 65 74 2c 0a 09 09 09 28 75 6e 73 .offset,....(uns
5680: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
5690: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 (request->heade
56a0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 rs.range.offset
56b0: 2b 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 + request->heade
56c0: 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 rs.range.length
56d0: 2d 20 31 29 2c 0a 09 09 09 28 75 6e 73 69 67 6e - 1),....(unsign
56e0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 ed long long) fi
56f0: 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 0a 09 09 29 3b leinfo->len...);
5700: 0a 09 7d 0a 09 66 70 72 69 6e 74 66 28 66 70 2c ..}..fprintf(fp,
5710: 20 22 5c 72 5c 6e 22 29 3b 0a 09 66 66 6c 75 73 "\r\n");..fflus
5720: 68 28 66 70 29 3b 0a 0a 09 6c 6f 67 2d 3e 68 74 h(fp);...log->ht
5730: 74 70 5f 63 6f 64 65 20 3d 20 68 74 74 70 5f 63 tp_code = http_c
5740: 6f 64 65 3b 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f ode;..log->reaso
5750: 6e 20 3d 20 22 4f 4b 22 3b 0a 09 6c 6f 67 2d 3e n = "OK";..log->
5760: 73 74 61 72 74 74 69 6d 65 20 3d 20 74 69 6d 65 starttime = time
5770: 28 4e 55 4c 4c 29 3b 0a 09 6c 6f 67 2d 3e 72 65 (NULL);..log->re
5780: 71 5f 6f 66 66 73 65 74 20 3d 20 72 65 71 75 65 q_offset = reque
5790: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
57a0: 65 2e 6f 66 66 73 65 74 3b 0a 09 6c 6f 67 2d 3e e.offset;..log->
57b0: 72 65 71 5f 6c 65 6e 67 74 68 20 3d 20 72 65 71 req_length = req
57c0: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
57d0: 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09 6c 6f 67 nge.length;..log
57e0: 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 ->file_length =
57f0: 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 0a fileinfo->len;..
5800: 23 69 66 64 65 66 20 46 49 4c 45 44 5f 4e 4f 4e #ifdef FILED_NON
5810: 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 69 6e 74 20 BLOCK_HTTP..int
5820: 73 6f 63 6b 65 74 5f 66 6c 61 67 73 3b 0a 09 66 socket_flags;..f
5830: 64 5f 73 65 74 20 72 66 64 2c 20 77 66 64 3b 0a d_set rfd, wfd;.
5840: 09 63 68 61 72 20 73 69 6e 6b 62 75 66 5b 38 31 .char sinkbuf[81
5850: 39 32 5d 3b 0a 09 73 73 69 7a 65 5f 74 20 72 65 92];..ssize_t re
5860: 61 64 5f 72 65 74 3b 0a 0a 09 46 44 5f 5a 45 52 ad_ret;...FD_ZER
5870: 4f 28 26 72 66 64 29 3b 0a 09 46 44 5f 5a 45 52 O(&rfd);..FD_ZER
5880: 4f 28 26 77 66 64 29 3b 0a 09 46 44 5f 53 45 54 O(&wfd);..FD_SET
5890: 28 66 64 2c 20 26 72 66 64 29 3b 0a 09 46 44 5f (fd, &rfd);..FD_
58a0: 53 45 54 28 66 64 2c 20 26 77 66 64 29 3b 0a 0a SET(fd, &wfd);..
58b0: 09 73 6f 63 6b 65 74 5f 66 6c 61 67 73 20 3d 20 .socket_flags =
58c0: 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 fcntl(fd, F_GETF
58d0: 4c 29 3b 0a 09 66 63 6e 74 6c 28 66 64 2c 20 46 L);..fcntl(fd, F
58e0: 5f 53 45 54 46 4c 2c 20 73 6f 63 6b 65 74 5f 66 _SETFL, socket_f
58f0: 6c 61 67 73 20 7c 20 4f 5f 4e 4f 4e 42 4c 4f 43 lags | O_NONBLOC
5900: 4b 29 3b 0a 23 65 6e 64 69 66 0a 09 73 65 6e 64 K);.#endif..send
5910: 66 69 6c 65 5f 6f 66 66 73 65 74 20 3d 20 72 65 file_offset = re
5920: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
5930: 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 73 65 ange.offset;..se
5940: 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 20 72 65 71 ndfile_len = req
5950: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
5960: 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a 09 73 65 6e nge.length;..sen
5970: 64 66 69 6c 65 5f 73 65 6e 74 20 3d 20 30 3b 0a dfile_sent = 0;.
5980: 09 77 68 69 6c 65 20 28 72 65 71 75 65 73 74 2d .while (request-
5990: 3e 6d 65 74 68 6f 64 20 3d 3d 20 46 49 4c 45 44 >method == FILED
59a0: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f _REQUEST_METHOD_
59b0: 47 45 54 29 20 7b 0a 09 09 69 66 20 28 73 65 6e GET) {...if (sen
59c0: 64 66 69 6c 65 5f 6c 65 6e 20 3e 20 46 49 4c 45 dfile_len > FILE
59d0: 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41 58 29 20 D_SENDFILE_MAX)
59e0: 7b 0a 09 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 {....sendfile_si
59f0: 7a 65 20 3d 20 46 49 4c 45 44 5f 53 45 4e 44 46 ze = FILED_SENDF
5a00: 49 4c 45 5f 4d 41 58 3b 0a 09 09 7d 20 65 6c 73 ILE_MAX;...} els
5a10: 65 20 7b 0a 09 09 09 73 65 6e 64 66 69 6c 65 5f e {....sendfile_
5a20: 73 69 7a 65 20 3d 20 73 65 6e 64 66 69 6c 65 5f size = sendfile_
5a30: 6c 65 6e 3b 0a 09 09 7d 0a 0a 09 09 73 65 6e 64 len;...}....send
5a40: 66 69 6c 65 5f 72 65 74 20 3d 20 73 65 6e 64 66 file_ret = sendf
5a50: 69 6c 65 28 66 64 2c 20 66 69 6c 65 69 6e 66 6f ile(fd, fileinfo
5a60: 2d 3e 66 64 2c 20 26 73 65 6e 64 66 69 6c 65 5f ->fd, &sendfile_
5a70: 6f 66 66 73 65 74 2c 20 73 65 6e 64 66 69 6c 65 offset, sendfile
5a80: 5f 73 69 7a 65 29 3b 0a 09 09 69 66 20 28 73 65 _size);...if (se
5a90: 6e 64 66 69 6c 65 5f 72 65 74 20 3c 3d 20 30 29 ndfile_ret <= 0)
5aa0: 20 7b 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f {.#ifdef FILED_
5ab0: 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 09 NONBLOCK_HTTP...
5ac0: 09 69 66 20 28 65 72 72 6e 6f 20 3d 3d 20 45 41 .if (errno == EA
5ad0: 47 41 49 4e 29 20 7b 0a 09 09 09 09 73 65 6e 64 GAIN) {.....send
5ae0: 66 69 6c 65 5f 72 65 74 20 3d 20 30 3b 0a 0a 09 file_ret = 0;...
5af0: 09 09 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 ...while (1) {..
5b00: 09 09 09 09 73 65 6c 65 63 74 28 66 64 20 2b 20 ....select(fd +
5b10: 31 2c 20 26 72 66 64 2c 20 26 77 66 64 2c 20 4e 1, &rfd, &wfd, N
5b20: 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 09 09 09 09 ULL, NULL);.....
5b30: 09 69 66 20 28 46 44 5f 49 53 53 45 54 28 66 64 .if (FD_ISSET(fd
5b40: 2c 20 26 72 66 64 29 29 20 7b 0a 09 09 09 09 09 , &rfd)) {......
5b50: 09 72 65 61 64 5f 72 65 74 20 3d 20 72 65 61 64 .read_ret = read
5b60: 28 66 64 2c 20 73 69 6e 6b 62 75 66 2c 20 73 69 (fd, sinkbuf, si
5b70: 7a 65 6f 66 28 73 69 6e 6b 62 75 66 29 29 3b 0a zeof(sinkbuf));.
5b80: 0a 09 09 09 09 09 09 69 66 20 28 72 65 61 64 5f .......if (read_
5b90: 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 ret <= 0) {.....
5ba0: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 ...break;.......
5bb0: 7d 0a 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 69 }......}.......i
5bc0: 66 20 28 46 44 5f 49 53 53 45 54 28 66 64 2c 20 f (FD_ISSET(fd,
5bd0: 26 77 66 64 29 29 20 7b 0a 09 09 09 09 09 09 72 &wfd)) {.......r
5be0: 65 61 64 5f 72 65 74 20 3d 20 31 3b 0a 0a 09 09 ead_ret = 1;....
5bf0: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 ....break;......
5c00: 7d 0a 09 09 09 09 7d 0a 0a 09 09 09 09 69 66 20 }.....}......if
5c10: 28 72 65 61 64 5f 72 65 74 20 3c 3d 20 30 29 20 (read_ret <= 0)
5c20: 7b 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 {......break;...
5c30: 09 09 7d 0a 09 09 09 7d 20 65 6c 73 65 20 7b 0a ..}....} else {.
5c40: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 7d 0a ....break;....}.
5c50: 23 65 6c 73 65 0a 09 09 09 62 72 65 61 6b 3b 0a #else....break;.
5c60: 23 65 6e 64 69 66 0a 09 09 7d 0a 0a 09 09 73 65 #endif...}....se
5c70: 6e 64 66 69 6c 65 5f 6c 65 6e 20 2d 3d 20 73 65 ndfile_len -= se
5c80: 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 09 73 65 ndfile_ret;...se
5c90: 6e 64 66 69 6c 65 5f 73 65 6e 74 20 2b 3d 20 73 ndfile_sent += s
5ca0: 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 09 69 endfile_ret;...i
5cb0: 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 f (sendfile_len
5cc0: 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72 65 61 6b == 0) {....break
5cd0: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 6c 6f 67 2d 3e ;...}..}...log->
5ce0: 65 6e 64 74 69 6d 65 20 3d 20 28 74 69 6d 65 5f endtime = (time_
5cf0: 74 29 20 2d 31 3b 0a 09 6c 6f 67 2d 3e 73 65 6e t) -1;..log->sen
5d00: 74 5f 6c 65 6e 67 74 68 20 3d 20 73 65 6e 64 66 t_length = sendf
5d10: 69 6c 65 5f 73 65 6e 74 3b 0a 0a 09 66 69 6c 65 ile_sent;...file
5d20: 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 d_log_entry(log)
5d30: 3b 0a 0a 09 63 6c 6f 73 65 28 66 69 6c 65 69 6e ;...close(filein
5d40: 66 6f 2d 3e 66 64 29 3b 0a 0a 09 66 63 6c 6f 73 fo->fd);...fclos
5d50: 65 28 66 70 29 3b 0a 0a 09 72 65 74 75 72 6e 3b e(fp);...return;
5d60: 0a 7d 0a 0a 2f 2a 20 48 61 6e 64 6c 65 20 69 6e .}../* Handle in
5d70: 63 6f 6d 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f coming connectio
5d80: 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ns */.static voi
5d90: 64 20 2a 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f d *filed_worker_
5da0: 74 68 72 65 61 64 28 76 6f 69 64 20 2a 61 72 67 thread(void *arg
5db0: 5f 76 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 _v) {..struct fi
5dc0: 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 led_worker_threa
5dd0: 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 73 74 d_args *arg;..st
5de0: 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f ruct filed_http_
5df0: 72 65 71 75 65 73 74 20 72 65 71 75 65 73 74 3b request request;
5e00: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c ..struct filed_l
5e10: 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 2c 20 6c og_entry *log, l
5e20: 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 3b 0a ocal_dummy_log;.
5e30: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 .struct filed_op
5e40: 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 3b 0a tions *options;.
5e50: 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 .struct sockaddr
5e60: 5f 69 6e 36 20 61 64 64 72 3b 0a 09 73 6f 63 6b _in6 addr;..sock
5e70: 6c 65 6e 5f 74 20 61 64 64 72 6c 65 6e 3b 0a 09 len_t addrlen;..
5e80: 69 6e 74 20 66 61 69 6c 75 72 65 5f 63 6f 75 6e int failure_coun
5e90: 74 20 3d 20 30 2c 20 6d 61 78 5f 66 61 69 6c 75 t = 0, max_failu
5ea0: 72 65 5f 63 6f 75 6e 74 20 3d 20 46 49 4c 45 44 re_count = FILED
5eb0: 5f 4d 41 58 5f 46 41 49 4c 55 52 45 5f 43 4f 55 _MAX_FAILURE_COU
5ec0: 4e 54 3b 0a 09 69 6e 74 20 6d 61 73 74 65 72 5f NT;..int master_
5ed0: 66 64 2c 20 66 64 3b 0a 0a 09 2f 2a 20 52 65 61 fd, fd;.../* Rea
5ee0: 64 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 d arguments */..
5ef0: 61 72 67 20 3d 20 61 72 67 5f 76 3b 0a 0a 09 6d arg = arg_v;...m
5f00: 61 73 74 65 72 5f 66 64 20 3d 20 61 72 67 2d 3e aster_fd = arg->
5f10: 66 64 3b 0a 09 6f 70 74 69 6f 6e 73 20 3d 20 26 fd;..options = &
5f20: 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 3b 0a 0a 09 arg->options;...
5f30: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 2f 2a while (1) {.../*
5f40: 20 46 61 69 6c 75 72 65 20 6c 6f 6f 70 20 70 72 Failure loop pr
5f50: 65 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 evention */...if
5f60: 20 28 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 (failure_count
5f70: 3e 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f > max_failure_co
5f80: 75 6e 74 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b unt) {....break;
5f90: 0a 09 09 7d 0a 0a 09 09 2f 2a 20 41 6c 6c 6f 63 ...}..../* Alloc
5fa0: 61 74 65 20 61 20 6e 65 77 20 6c 6f 67 20 62 75 ate a new log bu
5fb0: 66 66 65 72 20 2a 2f 0a 09 09 6c 6f 67 20 3d 20 ffer */...log =
5fc0: 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 31 29 filed_log_new(1)
5fd0: 3b 0a 09 09 69 66 20 28 6c 6f 67 20 3d 3d 20 4e ;...if (log == N
5fe0: 55 4c 4c 29 20 7b 0a 09 09 09 66 69 6c 65 64 5f ULL) {....filed_
5ff0: 6c 6f 67 5f 6d 73 67 28 22 41 4c 4c 4f 43 41 54 log_msg("ALLOCAT
6000: 45 5f 4c 4f 47 5f 4d 53 47 5f 46 41 49 4c 45 44 E_LOG_MSG_FAILED
6010: 22 29 3b 0a 0a 09 09 09 62 72 65 61 6b 3b 0a 09 ");.....break;..
6020: 09 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 79 70 65 20 .}....log->type
6030: 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 = FILED_LOG_TYPE
6040: 5f 54 52 41 4e 53 46 45 52 3b 0a 0a 09 09 2f 2a _TRANSFER;..../*
6050: 20 41 63 63 65 70 74 20 61 20 6e 65 77 20 63 6c Accept a new cl
6060: 69 65 6e 74 20 2a 2f 0a 09 09 61 64 64 72 6c 65 ient */...addrle
6070: 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 29 n = sizeof(addr)
6080: 3b 0a 09 09 66 64 20 3d 20 61 63 63 65 70 74 28 ;...fd = accept(
6090: 6d 61 73 74 65 72 5f 66 64 2c 20 28 73 74 72 75 master_fd, (stru
60a0: 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 ct sockaddr *) &
60b0: 61 64 64 72 2c 20 26 61 64 64 72 6c 65 6e 29 3b addr, &addrlen);
60c0: 0a 0a 09 09 2f 2a 0a 09 09 20 2a 20 49 66 20 77 ..../*... * If w
60d0: 65 20 66 61 69 6c 2c 20 6d 61 6b 65 20 61 20 6e e fail, make a n
60e0: 6f 74 65 20 6f 66 20 69 74 20 73 6f 20 77 65 20 ote of it so we
60f0: 64 6f 6e 27 74 20 67 6f 20 69 6e 74 6f 20 61 20 don't go into a
6100: 6c 6f 6f 70 20 6f 66 0a 09 09 20 2a 20 61 63 63 loop of... * acc
6110: 65 70 74 28 29 20 66 61 69 6c 69 6e 67 0a 09 09 ept() failing...
6120: 20 2a 2f 0a 09 09 69 66 20 28 66 64 20 3c 20 30 */...if (fd < 0
6130: 29 20 7b 0a 09 09 09 2f 2a 20 4c 6f 67 20 74 68 ) {..../* Log th
6140: 65 20 6e 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e e new connection
6150: 20 2a 2f 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 */....filed_log
6160: 5f 6d 73 67 28 22 41 43 43 45 50 54 5f 46 41 49 _msg("ACCEPT_FAI
6170: 4c 45 44 22 29 3b 0a 0a 09 09 09 66 61 69 6c 75 LED");.....failu
6180: 72 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 0a 09 09 09 re_count++;.....
6190: 66 72 65 65 28 6c 6f 67 29 3b 0a 0a 09 09 09 63 free(log);.....c
61a0: 6f 6e 74 69 6e 75 65 3b 0a 09 09 7d 0a 0a 09 09 ontinue;...}....
61b0: 2f 2a 20 46 69 6c 6c 20 69 6e 20 6c 6f 67 20 73 /* Fill in log s
61c0: 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 09 69 66 tructure */...if
61d0: 20 28 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 28 (filed_log_ip((
61e0: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 struct sockaddr
61f0: 2a 29 20 26 61 64 64 72 2c 20 6c 6f 67 2d 3e 69 *) &addr, log->i
6200: 70 2c 20 73 69 7a 65 6f 66 28 6c 6f 67 2d 3e 69 p, sizeof(log->i
6210: 70 29 29 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 p)) == NULL) {..
6220: 09 09 6c 6f 67 2d 3e 69 70 5b 30 5d 20 3d 20 27 ..log->ip[0] = '
6230: 5c 30 27 3b 0a 09 09 09 6c 6f 67 2d 3e 70 6f 72 \0';....log->por
6240: 74 20 3d 20 30 3b 0a 09 09 7d 20 65 6c 73 65 20 t = 0;...} else
6250: 7b 0a 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 20 3d {....log->port =
6260: 20 61 64 64 72 2e 73 69 6e 36 5f 70 6f 72 74 3b addr.sin6_port;
6270: 0a 09 09 7d 0a 0a 09 09 2f 2a 20 52 65 73 65 74 ...}..../* Reset
6280: 20 66 61 69 6c 75 72 65 20 63 6f 75 6e 74 2a 2f failure count*/
6290: 0a 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 ...failure_count
62a0: 20 3d 20 30 3b 0a 0a 09 09 2f 2a 20 48 61 6e 64 = 0;..../* Hand
62b0: 6c 65 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 09 66 le socket */...f
62c0: 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69 65 iled_handle_clie
62d0: 6e 74 28 66 64 2c 20 26 72 65 71 75 65 73 74 2c nt(fd, &request,
62e0: 20 6c 6f 67 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a log, options);.
62f0: 09 7d 0a 0a 09 2f 2a 20 52 65 70 6f 72 74 20 65 .}.../* Report e
6300: 72 72 6f 72 20 2a 2f 0a 09 66 69 6c 65 64 5f 6c rror */..filed_l
6310: 6f 67 5f 6d 73 67 28 22 54 48 52 45 41 44 5f 44 og_msg("THREAD_D
6320: 49 45 44 20 41 42 4e 4f 52 4d 41 4c 22 29 3b 0a IED ABNORMAL");.
6330: 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a ..return(NULL);.
6340: 0a 09 2f 2a 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 ../* local_dummy
6350: 5f 6c 6f 67 20 69 73 20 6f 6e 6c 79 20 75 73 65 _log is only use
6360: 64 20 69 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f d if FILED_DONT_
6370: 4c 4f 47 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 LOG is enabled,
6380: 6f 74 68 65 72 77 69 73 65 20 69 74 27 73 20 6e otherwise it's n
6390: 6f 74 20 75 73 65 64 2c 20 62 75 74 20 74 68 65 ot used, but the
63a0: 20 63 6f 6d 70 69 6c 65 72 20 68 61 74 65 73 20 compiler hates
63b0: 74 68 61 74 20 69 64 65 61 2e 20 2a 2f 0a 09 6c that idea. */..l
63c0: 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 ocal_dummy_log.t
63d0: 79 70 65 20 3d 20 30 3b 0a 09 6c 6f 63 61 6c 5f ype = 0;..local_
63e0: 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 20 3d dummy_log.type =
63f0: 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 local_dummy_log
6400: 2e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 43 72 65 .type;.}../* Cre
6410: 61 74 65 20 77 6f 72 6b 65 72 20 74 68 72 65 61 ate worker threa
6420: 64 73 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 ds */.static int
6430: 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 filed_worker_th
6440: 72 65 61 64 73 5f 69 6e 69 74 28 69 6e 74 20 66 reads_init(int f
6450: 64 2c 20 69 6e 74 20 74 68 72 65 61 64 5f 63 6f d, int thread_co
6460: 75 6e 74 2c 20 73 74 72 75 63 74 20 66 69 6c 65 unt, struct file
6470: 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f d_options *optio
6480: 6e 73 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 ns) {..struct fi
6490: 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 led_worker_threa
64a0: 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 70 74 d_args *arg;..pt
64b0: 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 69 64 hread_t threadid
64c0: 3b 0a 09 69 6e 74 20 70 74 68 72 65 61 64 5f 72 ;..int pthread_r
64d0: 65 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 66 6f et;..int i;...fo
64e0: 72 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 74 68 r (i = 0; i < th
64f0: 72 65 61 64 5f 63 6f 75 6e 74 3b 20 69 2b 2b 29 read_count; i++)
6500: 20 7b 0a 09 09 61 72 67 20 3d 20 6d 61 6c 6c 6f {...arg = mallo
6510: 63 28 73 69 7a 65 6f 66 28 2a 61 72 67 29 29 3b c(sizeof(*arg));
6520: 0a 0a 09 09 61 72 67 2d 3e 66 64 20 3d 20 66 64 ....arg->fd = fd
6530: 3b 0a 09 09 6d 65 6d 63 70 79 28 26 61 72 67 2d ;...memcpy(&arg-
6540: 3e 6f 70 74 69 6f 6e 73 2c 20 6f 70 74 69 6f 6e >options, option
6550: 73 2c 20 73 69 7a 65 6f 66 28 2a 6f 70 74 69 6f s, sizeof(*optio
6560: 6e 73 29 29 3b 0a 0a 09 09 70 74 68 72 65 61 64 ns));....pthread
6570: 5f 72 65 74 20 3d 20 70 74 68 72 65 61 64 5f 63 _ret = pthread_c
6580: 72 65 61 74 65 28 26 74 68 72 65 61 64 69 64 2c reate(&threadid,
6590: 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 77 6f 72 NULL, filed_wor
65a0: 6b 65 72 5f 74 68 72 65 61 64 2c 20 61 72 67 29 ker_thread, arg)
65b0: 3b 0a 09 09 69 66 20 28 70 74 68 72 65 61 64 5f ;...if (pthread_
65c0: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 72 ret != 0) {....r
65d0: 65 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 09 eturn(-1);...}..
65e0: 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d }...return(0);.}
65f0: 0a 0a 2f 2a 20 44 69 73 70 6c 61 79 20 68 65 6c ../* Display hel
6600: 70 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 p */.static void
6610: 20 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c filed_print_hel
6620: 70 28 46 49 4c 45 20 2a 6f 75 74 70 75 74 2c 20 p(FILE *output,
6630: 69 6e 74 20 6c 6f 6e 67 5f 68 65 6c 70 2c 20 63 int long_help, c
6640: 6f 6e 73 74 20 63 68 61 72 20 2a 65 78 74 72 61 onst char *extra
6650: 29 20 7b 0a 09 69 66 20 28 65 78 74 72 61 29 20 ) {..if (extra)
6660: 7b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 {...fprintf(outp
6670: 75 74 2c 20 22 25 73 5c 6e 22 2c 20 65 78 74 72 ut, "%s\n", extr
6680: 61 29 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74 66 a);..}...fprintf
6690: 28 6f 75 74 70 75 74 2c 20 22 55 73 61 67 65 3a (output, "Usage:
66a0: 20 66 69 6c 65 64 20 5b 3c 6f 70 74 69 6f 6e 73 filed [<options
66b0: 3e 5d 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 >]\n");..fprintf
66c0: 28 6f 75 74 70 75 74 2c 20 22 20 20 4f 70 74 69 (output, " Opti
66d0: 6f 6e 73 3a 5c 6e 22 29 3b 0a 09 66 70 72 69 6e ons:\n");..fprin
66e0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
66f0: 20 20 2d 68 2c 20 2d 2d 68 65 6c 70 5c 6e 22 29 -h, --help\n")
6700: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
6710: 74 2c 20 22 20 20 20 20 20 20 2d 64 2c 20 2d 2d t, " -d, --
6720: 64 61 65 6d 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 daemon\n");..fpr
6730: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
6740: 20 20 20 20 2d 76 2c 20 2d 2d 76 65 72 73 69 6f -v, --versio
6750: 6e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 n\n");..fprintf(
6760: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
6770: 56 2c 20 2d 2d 76 68 6f 73 74 5c 6e 22 29 3b 0a V, --vhost\n");.
6780: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6790: 20 22 20 20 20 20 20 20 2d 62 20 3c 61 64 64 72 " -b <addr
67a0: 65 73 73 3e 2c 20 2d 2d 62 69 6e 64 20 3c 61 64 ess>, --bind <ad
67b0: 64 72 65 73 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 dress>\n");..fpr
67c0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
67d0: 20 20 20 20 2d 70 20 3c 70 6f 72 74 3e 2c 20 2d -p <port>, -
67e0: 2d 70 6f 72 74 20 3c 70 6f 72 74 3e 5c 6e 22 29 -port <port>\n")
67f0: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
6800: 74 2c 20 22 20 20 20 20 20 20 2d 74 20 3c 63 6f t, " -t <co
6810: 75 6e 74 3e 2c 20 2d 2d 74 68 72 65 61 64 73 20 unt>, --threads
6820: 3c 63 6f 75 6e 74 3e 5c 6e 22 29 3b 0a 09 66 70 <count>\n");..fp
6830: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
6840: 20 20 20 20 20 2d 63 20 3c 65 6e 74 72 69 65 73 -c <entries
6850: 3e 2c 20 2d 2d 63 61 63 68 65 20 3c 65 6e 74 72 >, --cache <entr
6860: 69 65 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e ies>\n");..fprin
6870: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
6880: 20 20 2d 6c 20 3c 66 69 6c 65 3e 2c 20 2d 2d 6c -l <file>, --l
6890: 6f 67 20 3c 66 69 6c 65 3e 5c 6e 22 29 3b 0a 09 og <file>\n");..
68a0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
68b0: 22 20 20 20 20 20 20 2d 75 20 3c 75 73 65 72 3e " -u <user>
68c0: 2c 20 2d 2d 75 73 65 72 20 3c 75 73 65 72 3e 5c , --user <user>\
68d0: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
68e0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 72 20 tput, " -r
68f0: 3c 64 69 72 65 63 74 6f 72 79 3e 2c 20 2d 2d 72 <directory>, --r
6900: 6f 6f 74 20 3c 64 69 72 65 63 74 6f 72 79 3e 5c oot <directory>\
6910: 6e 22 29 3b 0a 0a 09 69 66 20 28 6c 6f 6e 67 5f n");...if (long_
6920: 68 65 6c 70 29 20 7b 0a 09 09 66 70 72 69 6e 74 help) {...fprint
6930: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
6940: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
6950: 74 2c 20 22 20 20 55 73 61 67 65 3a 5c 6e 22 29 t, " Usage:\n")
6960: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
6970: 75 74 2c 20 22 20 20 20 20 20 20 2d 68 20 28 6f ut, " -h (o
6980: 72 20 2d 2d 68 65 6c 70 29 20 70 72 69 6e 74 73 r --help) prints
6990: 20 74 68 69 73 20 75 73 61 67 65 20 69 6e 66 6f this usage info
69a0: 72 6d 61 74 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 rmation.\n");...
69b0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
69c0: 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 "\n");...fprintf
69d0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
69e0: 2d 64 20 28 6f 72 20 2d 2d 64 61 65 6d 6f 6e 29 -d (or --daemon)
69f0: 20 69 6e 73 74 72 75 63 74 73 20 66 69 6c 65 64 instructs filed
6a00: 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 64 61 65 to become a dae
6a10: 6d 6f 6e 20 61 66 74 65 72 20 69 6e 69 74 69 61 mon after initia
6a20: 6c 69 7a 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 lizing\n");...fp
6a30: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
6a40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6a50: 20 20 20 20 20 20 74 68 65 20 6c 69 73 74 65 6e the listen
6a60: 69 6e 67 20 54 43 50 20 73 6f 63 6b 65 74 20 61 ing TCP socket a
6a70: 6e 64 20 6c 6f 67 20 66 69 6c 65 73 2e 5c 6e 22 nd log files.\n"
6a80: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
6a90: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 put, "\n");...fp
6aa0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
6ab0: 20 20 20 20 20 2d 76 20 28 6f 72 20 2d 2d 76 65 -v (or --ve
6ac0: 72 73 69 6f 6e 29 20 69 6e 73 74 72 75 63 74 73 rsion) instructs
6ad0: 20 66 69 6c 65 64 20 70 72 69 6e 74 20 6f 75 74 filed print out
6ae0: 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d the version num
6af0: 62 65 72 20 61 6e 64 20 65 78 69 74 2e 5c 6e 22 ber and exit.\n"
6b00: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
6b10: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 put, "\n");...fp
6b20: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
6b30: 20 20 20 20 20 2d 56 20 28 6f 72 20 2d 2d 76 68 -V (or --vh
6b40: 6f 73 74 29 20 69 6e 73 74 72 75 63 74 73 20 66 ost) instructs f
6b50: 69 6c 65 64 20 74 6f 20 70 72 65 70 65 6e 64 20 iled to prepend
6b60: 61 6c 6c 20 72 65 71 75 65 73 74 73 20 77 69 74 all requests wit
6b70: 68 20 74 68 65 69 72 20 48 54 54 50 5c 6e 22 29 h their HTTP\n")
6b80: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
6b90: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
6ba0: 20 20 20 20 20 20 20 20 20 20 20 48 6f 73 74 20 Host
6bb0: 68 65 61 64 65 72 2e 5c 6e 22 29 3b 0a 09 09 66 header.\n");...f
6bc0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
6bd0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
6be0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
6bf0: 62 20 28 6f 72 20 2d 2d 62 69 6e 64 29 20 73 70 b (or --bind) sp
6c00: 65 63 69 66 69 65 73 20 74 68 65 20 61 64 64 72 ecifies the addr
6c10: 65 73 73 20 74 6f 20 6c 69 73 74 65 6e 20 66 6f ess to listen fo
6c20: 72 20 69 6e 63 6f 6d 69 6e 67 20 48 54 54 50 5c r incoming HTTP\
6c30: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
6c40: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
6c50: 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 71 req
6c60: 75 65 73 74 73 20 6f 6e 2e 20 20 54 68 65 20 64 uests on. The d
6c70: 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20 efault value is
6c80: 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 42 49 4e 44 \"%s\".\n", BIND
6c90: 5f 41 44 44 52 29 3b 0a 09 09 66 70 72 69 6e 74 _ADDR);...fprint
6ca0: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
6cb0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
6cc0: 74 2c 20 22 20 20 20 20 20 20 2d 70 20 28 6f 72 t, " -p (or
6cd0: 20 2d 2d 70 6f 72 74 29 20 73 70 65 63 69 66 69 --port) specifi
6ce0: 65 73 20 74 68 65 20 54 43 50 20 70 6f 72 74 20 es the TCP port
6cf0: 6e 75 6d 62 65 72 20 74 6f 20 6c 69 73 74 65 6e number to listen
6d00: 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 48 54 for incoming HT
6d10: 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 TP\n");...fprint
6d20: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
6d30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d40: 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 20 54 68 requests on. Th
6d50: 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 75 2e e default is %u.
6d60: 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 69 \n", (unsigned i
6d70: 6e 74 29 20 50 4f 52 54 29 3b 0a 09 09 66 70 72 nt) PORT);...fpr
6d80: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e intf(output, "\n
6d90: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
6da0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 74 20 tput, " -t
6db0: 28 6f 72 20 2d 2d 74 68 72 65 61 64 73 29 20 73 (or --threads) s
6dc0: 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d pecifies the num
6dd0: 62 65 72 20 6f 66 20 77 6f 72 6b 65 72 20 74 68 ber of worker th
6de0: 72 65 61 64 73 20 74 6f 20 63 72 65 61 74 65 2e reads to create.
6df0: 20 45 61 63 68 5c 6e 22 29 3b 0a 09 09 66 70 72 Each\n");...fpr
6e00: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
6e10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e20: 20 20 20 20 20 20 77 6f 72 6b 65 72 20 74 68 72 worker thr
6e30: 65 61 64 20 63 61 6e 20 73 65 72 76 69 63 65 20 ead can service
6e40: 6f 6e 65 20 63 6f 6e 63 75 72 72 65 6e 74 20 48 one concurrent H
6e50: 54 54 50 20 73 65 73 73 69 6f 6e 2e 5c 6e 22 29 TTP session.\n")
6e60: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
6e70: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
6e80: 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 75 Thu
6e90: 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
6ea0: 74 68 72 65 61 64 73 20 63 72 65 61 74 65 64 20 threads created
6eb0: 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 20 68 will determine h
6ec0: 6f 77 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 ow\n");...fprint
6ed0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
6ee0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ef0: 20 20 20 6d 61 6e 79 20 73 69 6d 75 6c 74 61 6e many simultan
6f00: 65 6f 75 73 20 74 72 61 6e 73 66 65 72 73 20 77 eous transfers w
6f10: 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c 65 2e ill be possible.
6f20: 20 54 68 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 The\n");...fpri
6f30: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
6f40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6f50: 20 20 20 20 20 64 65 66 61 75 6c 74 20 69 73 20 default is
6f60: 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e %lu.\n", (unsign
6f70: 65 64 20 6c 6f 6e 67 29 20 54 48 52 45 41 44 5f ed long) THREAD_
6f80: 43 4f 55 4e 54 29 3b 0a 09 09 66 70 72 69 6e 74 COUNT);...fprint
6f90: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
6fa0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
6fb0: 74 2c 20 22 20 20 20 20 20 20 2d 63 20 28 6f 72 t, " -c (or
6fc0: 20 2d 2d 63 61 63 68 65 29 20 73 70 65 63 69 66 --cache) specif
6fd0: 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ies the number o
6fe0: 66 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 f file informati
6ff0: 6f 6e 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 on cache entries
7000: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
7010: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
7020: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
7030: 6f 20 61 6c 6c 6f 63 61 74 65 2e 20 20 45 61 63 o allocate. Eac
7040: 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 68 6f h cache entry ho
7050: 6c 64 73 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 lds file informa
7060: 74 69 6f 6e 20 61 73 5c 6e 22 29 3b 0a 09 09 66 tion as\n");...f
7070: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
7080: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7090: 20 20 20 20 20 20 77 65 6c 6c 20 61 73 20 61 6e well as an
70a0: 20 6f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 open file descr
70b0: 69 70 74 6f 72 20 74 6f 20 74 68 65 20 66 69 6c iptor to the fil
70c0: 65 2c 20 73 6f 20 72 65 73 6f 75 72 63 65 5c 6e e, so resource\n
70d0: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
70e0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
70f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69 6d lim
7100: 69 74 73 20 28 69 2e 65 2e 2c 20 75 6c 69 6d 69 its (i.e., ulimi
7110: 74 29 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e t) should be con
7120: 73 69 64 65 72 65 64 2e 20 20 54 68 69 73 20 73 sidered. This s
7130: 68 6f 75 6c 64 5c 6e 22 29 3b 0a 09 09 66 70 72 hould\n");...fpr
7140: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
7150: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7160: 20 20 20 20 62 65 20 61 20 70 72 69 6d 65 20 6e be a prime n
7170: 75 6d 62 65 72 20 66 6f 72 20 69 64 65 61 6c 20 umber for ideal
7180: 75 73 65 20 77 69 74 68 20 74 68 65 20 6c 6f 6f use with the loo
7190: 6b 75 70 20 6d 65 74 68 6f 64 2e 5c 6e 22 29 3b kup method.\n");
71a0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
71b0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
71c0: 20 20 20 20 20 20 20 20 20 20 54 68 65 20 64 65 The de
71d0: 66 61 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 fault is %lu.\n"
71e0: 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 , (unsigned long
71f0: 29 20 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a 09 ) CACHE_SIZE);..
7200: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
7210: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 "\n");...fprint
7220: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
7230: 20 2d 6c 20 28 6f 72 20 2d 2d 6c 6f 67 29 20 73 -l (or --log) s
7240: 70 65 63 69 66 69 65 73 20 61 20 66 69 6c 65 6e pecifies a filen
7250: 61 6d 65 20 74 6f 20 6f 70 65 6e 20 66 6f 72 20 ame to open for
7260: 77 72 69 74 69 6e 67 20 6c 6f 67 20 65 6e 74 72 writing log entr
7270: 69 65 73 2e 20 20 4c 6f 67 5c 6e 22 29 3b 0a 09 ies. Log\n");..
7280: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
7290: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
72a0: 20 20 20 20 20 20 65 6e 74 72 69 65 73 20 61 72 entries ar
72b0: 65 20 6d 61 64 65 20 66 6f 72 20 76 61 72 69 6f e made for vario
72c0: 75 73 20 73 74 61 67 65 73 20 69 6e 20 74 72 61 us stages in tra
72d0: 6e 73 66 65 72 69 6e 67 20 66 69 6c 65 73 2e 5c nsfering files.\
72e0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
72f0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
7300: 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
7310: 6c 6f 67 20 66 69 6c 65 20 69 73 20 6f 70 65 6e log file is open
7320: 65 64 20 62 65 66 6f 72 65 20 73 77 69 74 63 68 ed before switch
7330: 69 6e 67 20 75 73 65 72 73 20 28 73 65 65 20 5c ing users (see \
7340: 22 2d 75 5c 22 29 5c 6e 22 29 3b 0a 09 09 66 70 "-u\")\n");...fp
7350: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
7360: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7370: 20 20 20 61 6e 64 20 72 6f 6f 74 20 64 69 72 65 and root dire
7380: 63 74 6f 72 69 65 73 20 28 73 65 65 20 5c 22 2d ctories (see \"-
7390: 72 5c 22 29 2e 20 20 54 68 65 20 6c 6f 67 20 66 r\"). The log f
73a0: 69 6c 65 20 69 73 20 6e 65 76 65 72 5c 6e 22 29 ile is never\n")
73b0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
73c0: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
73d0: 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 64 20 closed
73e0: 73 6f 20 6c 6f 67 20 72 6f 74 61 74 69 6f 6e 20 so log rotation
73f0: 77 69 74 68 6f 75 74 20 73 74 6f 70 70 69 6e 67 without stopping
7400: 20 74 68 65 20 64 61 65 6d 6f 6e 20 69 73 20 77 the daemon is w
7410: 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ill\n");...fprin
7420: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
7430: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7440: 6e 6f 74 20 77 6f 72 6b 2e 20 20 54 68 65 20 76 not work. The v
7450: 61 6c 75 65 20 6f 66 20 5c 22 2d 5c 22 20 69 6e alue of \"-\" in
7460: 64 69 63 61 74 65 73 20 74 68 61 74 20 73 74 61 dicates that sta
7470: 6e 64 61 72 64 20 6f 75 74 70 75 74 5c 6e 22 29 ndard output\n")
7480: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
7490: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
74a0: 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64 20 should
74b0: 62 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 67 67 be used for logg
74c0: 69 6e 67 2e 20 20 49 66 20 74 68 65 20 66 69 6c ing. If the fil
74d0: 65 6e 61 6d 65 20 62 65 67 69 6e 73 20 77 69 74 ename begins wit
74e0: 68 20 61 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e h a\n");...fprin
74f0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
7500: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7510: 70 69 70 65 20 28 5c 22 7c 5c 22 29 20 74 68 65 pipe (\"|\") the
7520: 6e 20 61 20 70 72 6f 63 65 73 73 20 69 73 20 73 n a process is s
7530: 74 61 72 74 65 64 20 61 6e 64 20 75 73 65 64 20 tarted and used
7540: 66 6f 72 20 6c 6f 67 67 69 6e 67 5c 6e 22 29 3b for logging\n");
7550: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
7560: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
7570: 20 20 20 20 20 20 20 20 69 6e 73 74 65 61 64 20 instead
7580: 6f 66 20 61 20 66 69 6c 65 2e 20 20 54 68 65 20 of a file. The
7590: 64 65 66 61 75 6c 74 20 69 73 20 5c 22 25 73 5c default is \"%s\
75a0: 22 2e 5c 6e 22 2c 20 4c 4f 47 5f 46 49 4c 45 29 ".\n", LOG_FILE)
75b0: 3b 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f 44 ;.#ifdef FILED_D
75c0: 4f 4e 54 5f 4c 4f 47 0a 09 09 66 70 72 69 6e 74 ONT_LOG...fprint
75d0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
75e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e N
75f0: 6f 74 65 20 74 68 61 74 20 6c 6f 67 67 69 6e 67 ote that logging
7600: 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 is completely d
7610: 69 73 61 62 6c 65 64 20 73 6f 20 74 68 69 73 20 isabled so this
7620: 6f 70 74 69 6f 6e 20 64 6f 65 73 5c 6e 22 29 3b option does\n");
7630: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
7640: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
7650: 20 20 20 20 20 20 20 20 6e 6f 74 68 69 6e 67 20 nothing
7660: 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2e 5c 6e in this build.\n
7670: 22 29 3b 0a 23 65 6e 64 69 66 0a 09 09 66 70 72 ");.#endif...fpr
7680: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e intf(output, "\n
7690: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
76a0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 75 20 tput, " -u
76b0: 28 6f 72 20 2d 2d 75 73 65 72 29 20 73 70 65 63 (or --user) spec
76c0: 69 66 69 65 73 20 74 68 65 20 75 73 65 72 20 74 ifies the user t
76d0: 6f 20 73 77 69 74 63 68 20 75 73 65 72 20 49 44 o switch user ID
76e0: 73 20 74 6f 20 62 65 66 6f 72 65 20 73 65 72 76 s to before serv
76f0: 69 63 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 icing\n");...fpr
7700: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
7710: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7720: 20 20 20 72 65 71 75 65 73 74 73 2e 20 20 54 68 requests. Th
7730: 65 20 64 65 66 61 75 6c 74 20 69 73 20 6e 6f 74 e default is not
7740: 20 63 68 61 6e 67 65 20 75 73 65 72 20 49 44 73 change user IDs
7750: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 .\n");...fprintf
7760: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a (output, "\n");.
7770: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
7780: 2c 20 22 20 20 20 20 20 20 2d 72 20 28 6f 72 20 , " -r (or
7790: 2d 2d 72 6f 6f 74 29 20 73 70 65 63 69 66 69 65 --root) specifie
77a0: 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 s the directory
77b0: 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72 6f to act as the ro
77c0: 6f 74 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 ot directory for
77d0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
77e0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
77f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
7800: 65 20 66 69 6c 65 20 73 65 72 76 65 72 2e 20 20 e file server.
7810: 49 66 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 69 If this option i
7820: 73 20 73 70 65 63 69 66 69 65 64 2c 20 63 68 72 s specified, chr
7830: 6f 6f 74 28 32 29 5c 6e 22 29 3b 0a 09 09 66 70 oot(2)\n");...fp
7840: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
7850: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7860: 20 20 20 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 is called.
7870: 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 6e The default is n
7880: 6f 74 20 63 68 61 6e 67 65 20 72 6f 6f 74 20 64 ot change root d
7890: 69 72 65 63 74 6f 72 69 65 73 2c 5c 6e 22 29 3b irectories,\n");
78a0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
78b0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
78c0: 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 73 that is
78d0: 2c 20 74 68 65 20 5c 22 2f 5c 22 20 64 69 72 65 , the \"/\" dire
78e0: 63 74 6f 72 79 20 69 73 20 73 68 61 72 65 64 20 ctory is shared
78f0: 6f 75 74 2e 20 20 54 68 69 73 20 77 69 6c 6c 5c out. This will\
7900: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
7910: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
7920: 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69 6b lik
7930: 65 6c 79 20 62 65 20 61 20 73 65 63 75 72 69 74 ely be a securit
7940: 79 20 69 73 73 75 65 2c 20 73 6f 20 74 68 69 73 y issue, so this
7950: 20 6f 70 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 option should a
7960: 6c 77 61 79 73 5c 6e 22 29 3b 0a 09 09 66 70 72 lways\n");...fpr
7970: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
7980: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7990: 20 20 20 62 65 20 75 73 65 64 2e 5c 6e 22 29 3b be used.\n");
79a0: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a ..}...return;.}.
79b0: 0a 2f 2a 20 41 64 64 20 61 20 67 65 74 6f 70 74 ./* Add a getopt
79c0: 20 6f 70 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 option */.stati
79d0: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 74 c void filed_get
79e0: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 opt_long_setopt(
79f0: 73 74 72 75 63 74 20 6f 70 74 69 6f 6e 20 2a 6f struct option *o
7a00: 70 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a pt, const char *
7a10: 6e 61 6d 65 2c 20 69 6e 74 20 68 61 73 5f 61 72 name, int has_ar
7a20: 67 2c 20 69 6e 74 20 76 61 6c 29 20 7b 0a 09 6f g, int val) {..o
7a30: 70 74 2d 3e 6e 61 6d 65 20 20 20 20 20 3d 20 6e pt->name = n
7a40: 61 6d 65 3b 0a 09 6f 70 74 2d 3e 68 61 73 5f 61 ame;..opt->has_a
7a50: 72 67 20 20 3d 20 68 61 73 5f 61 72 67 3b 0a 09 rg = has_arg;..
7a60: 6f 70 74 2d 3e 66 6c 61 67 20 20 20 20 20 3d 20 opt->flag =
7a70: 4e 55 4c 4c 3b 0a 09 6f 70 74 2d 3e 76 61 6c 20 NULL;..opt->val
7a80: 20 20 20 20 20 3d 20 76 61 6c 3b 0a 0a 09 72 65 = val;...re
7a90: 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 73 6f turn;.}../* Reso
7aa0: 6c 76 65 20 61 20 75 73 65 72 6e 61 6d 65 20 74 lve a username t
7ab0: 6f 20 61 20 55 49 44 20 2a 2f 0a 73 74 61 74 69 o a UID */.stati
7ac0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 75 73 65 72 c int filed_user
7ad0: 5f 6c 6f 6f 6b 75 70 28 63 6f 6e 73 74 20 63 68 _lookup(const ch
7ae0: 61 72 20 2a 75 73 65 72 2c 20 75 69 64 5f 74 20 ar *user, uid_t
7af0: 2a 75 73 65 72 5f 69 64 29 20 7b 0a 09 63 68 61 *user_id) {..cha
7b00: 72 20 2a 6e 65 78 74 3b 0a 09 75 69 64 5f 74 20 r *next;..uid_t
7b10: 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 3b 0a 23 user_id_check;.#
7b20: 69 66 6e 64 65 66 20 46 49 4c 45 44 5f 4e 4f 5f ifndef FILED_NO_
7b30: 47 45 54 50 57 4e 41 4d 0a 09 73 74 72 75 63 74 GETPWNAM..struct
7b40: 20 70 61 73 73 77 64 20 2a 65 6e 74 3b 0a 0a 09 passwd *ent;...
7b50: 65 6e 74 20 3d 20 67 65 74 70 77 6e 61 6d 28 75 ent = getpwnam(u
7b60: 73 65 72 29 3b 0a 09 69 66 20 28 65 6e 74 20 21 ser);..if (ent !
7b70: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 75 73 65 = NULL) {...*use
7b80: 72 5f 69 64 20 3d 20 65 6e 74 2d 3e 70 77 5f 75 r_id = ent->pw_u
7b90: 69 64 3b 0a 0a 09 09 72 65 74 75 72 6e 28 30 29 id;....return(0)
7ba0: 3b 0a 09 7d 0a 23 65 6e 64 69 66 0a 0a 09 75 73 ;..}.#endif...us
7bb0: 65 72 5f 69 64 5f 63 68 65 63 6b 20 3d 20 73 74 er_id_check = st
7bc0: 72 74 6f 75 6c 6c 28 75 73 65 72 2c 20 26 6e 65 rtoull(user, &ne
7bd0: 78 74 2c 20 31 30 29 3b 0a 09 69 66 20 28 6e 65 xt, 10);..if (ne
7be0: 78 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 xt == NULL) {...
7bf0: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 return(1);..}...
7c00: 69 66 20 28 6e 65 78 74 5b 30 5d 20 21 3d 20 27 if (next[0] != '
7c10: 5c 30 27 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 \0') {...return(
7c20: 31 29 3b 0a 09 7d 0a 0a 09 2a 75 73 65 72 5f 69 1);..}...*user_i
7c30: 64 20 3d 20 75 73 65 72 5f 69 64 5f 63 68 65 63 d = user_id_chec
7c40: 6b 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a k;...return(0);.
7c50: 7d 0a 0a 2f 2a 20 44 61 65 6d 6f 6e 69 7a 65 20 }../* Daemonize
7c60: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
7c70: 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 76 6f led_daemonize(vo
7c80: 69 64 29 20 7b 0a 09 70 69 64 5f 74 20 73 65 74 id) {..pid_t set
7c90: 73 69 64 5f 72 65 74 2c 20 66 6f 72 6b 5f 72 65 sid_ret, fork_re
7ca0: 74 3b 0a 09 69 6e 74 20 63 68 64 69 72 5f 72 65 t;..int chdir_re
7cb0: 74 2c 20 64 75 70 32 5f 72 65 74 3b 0a 09 69 6e t, dup2_ret;..in
7cc0: 74 20 66 64 5f 69 6e 2c 20 66 64 5f 6f 75 74 3b t fd_in, fd_out;
7cd0: 0a 0a 09 63 68 64 69 72 5f 72 65 74 20 3d 20 63 ...chdir_ret = c
7ce0: 68 64 69 72 28 22 2f 22 29 3b 0a 09 69 66 20 28 hdir("/");..if (
7cf0: 63 68 64 69 72 5f 72 65 74 20 21 3d 20 30 29 20 chdir_ret != 0)
7d00: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 {...return(1);..
7d10: 7d 0a 0a 09 66 6f 72 6b 5f 72 65 74 20 3d 20 66 }...fork_ret = f
7d20: 6f 72 6b 28 29 3b 0a 09 69 66 20 28 66 6f 72 6b ork();..if (fork
7d30: 5f 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 72 65 _ret < 0) {...re
7d40: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 turn(1);..}...if
7d50: 20 28 66 6f 72 6b 5f 72 65 74 20 3e 20 30 29 20 (fork_ret > 0)
7d60: 7b 0a 09 09 2f 2a 20 50 61 72 65 6e 74 20 2a 2f {.../* Parent */
7d70: 0a 09 09 77 61 69 74 70 69 64 28 66 6f 72 6b 5f ...waitpid(fork_
7d80: 72 65 74 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 0a ret, NULL, 0);..
7d90: 09 09 65 78 69 74 28 45 58 49 54 5f 53 55 43 43 ..exit(EXIT_SUCC
7da0: 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 68 ESS);..}.../* Ch
7db0: 69 6c 64 20 2a 2f 0a 09 69 66 20 28 66 6f 72 6b ild */..if (fork
7dc0: 28 29 20 21 3d 20 30 29 20 7b 0a 09 09 2f 2a 20 () != 0) {.../*
7dd0: 43 68 69 6c 64 20 2a 2f 0a 09 09 65 78 69 74 28 Child */...exit(
7de0: 45 58 49 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 EXIT_SUCCESS);..
7df0: 7d 0a 0a 09 2f 2a 20 47 72 61 6e 64 20 63 68 69 }.../* Grand chi
7e00: 6c 64 20 2a 2f 0a 09 73 65 74 73 69 64 5f 72 65 ld */..setsid_re
7e10: 74 20 3d 20 73 65 74 73 69 64 28 29 3b 0a 09 69 t = setsid();..i
7e20: 66 20 28 73 65 74 73 69 64 5f 72 65 74 20 3d 3d f (setsid_ret ==
7e30: 20 28 28 70 69 64 5f 74 29 20 2d 31 29 29 20 7b ((pid_t) -1)) {
7e40: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d ...return(1);..}
7e50: 0a 0a 09 66 64 5f 69 6e 20 3d 20 6f 70 65 6e 28 ...fd_in = open(
7e60: 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 52 "/dev/null", O_R
7e70: 44 4f 4e 4c 59 29 3b 0a 09 66 64 5f 6f 75 74 20 DONLY);..fd_out
7e80: 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c = open("/dev/nul
7e90: 6c 22 2c 20 4f 5f 57 52 4f 4e 4c 59 29 3b 0a 09 l", O_WRONLY);..
7ea0: 69 66 20 28 66 64 5f 69 6e 20 3c 20 30 20 7c 7c if (fd_in < 0 ||
7eb0: 20 66 64 5f 6f 75 74 20 3c 20 30 29 20 7b 0a 09 fd_out < 0) {..
7ec0: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a .return(1);..}..
7ed0: 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32 .dup2_ret = dup2
7ee0: 28 66 64 5f 69 6e 2c 20 53 54 44 49 4e 5f 46 49 (fd_in, STDIN_FI
7ef0: 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 LENO);..if (dup2
7f00: 5f 72 65 74 20 21 3d 20 53 54 44 49 4e 5f 46 49 _ret != STDIN_FI
7f10: 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e LENO) {...return
7f20: 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 (1);..}...dup2_r
7f30: 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f 75 74 et = dup2(fd_out
7f40: 2c 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 , STDOUT_FILENO)
7f50: 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74 20 ;..if (dup2_ret
7f60: 21 3d 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f != STDOUT_FILENO
7f70: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b ) {...return(1);
7f80: 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d ..}...dup2_ret =
7f90: 20 64 75 70 32 28 66 64 5f 6f 75 74 2c 20 53 54 dup2(fd_out, ST
7fa0: 44 45 52 52 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 DERR_FILENO);..i
7fb0: 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53 f (dup2_ret != S
7fc0: 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29 20 7b 0a TDERR_FILENO) {.
7fd0: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
7fe0: 0a 09 63 6c 6f 73 65 28 66 64 5f 69 6e 29 3b 0a ..close(fd_in);.
7ff0: 09 63 6c 6f 73 65 28 66 64 5f 6f 75 74 29 3b 0a .close(fd_out);.
8000: 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a ..return(0);.}..
8010: 2f 2a 20 52 75 6e 20 70 72 6f 63 65 73 73 20 2a /* Run process *
8020: 2f 0a 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20 61 /.int main(int a
8030: 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 rgc, char **argv
8040: 29 20 7b 0a 09 73 74 72 75 63 74 20 6f 70 74 69 ) {..struct opti
8050: 6f 6e 20 6f 70 74 69 6f 6e 73 5b 31 32 5d 3b 0a on options[12];.
8060: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 .struct filed_op
8070: 74 69 6f 6e 73 20 74 68 72 65 61 64 5f 6f 70 74 tions thread_opt
8080: 69 6f 6e 73 3b 0a 09 63 6f 6e 73 74 20 63 68 61 ions;..const cha
8090: 72 20 2a 62 69 6e 64 5f 61 64 64 72 20 3d 20 42 r *bind_addr = B
80a0: 49 4e 44 5f 41 44 44 52 2c 20 2a 6e 65 77 72 6f IND_ADDR, *newro
80b0: 6f 74 20 3d 20 4e 55 4c 4c 2c 20 2a 6c 6f 67 5f ot = NULL, *log_
80c0: 66 69 6c 65 20 3d 20 4c 4f 47 5f 46 49 4c 45 3b file = LOG_FILE;
80d0: 0a 09 46 49 4c 45 20 2a 6c 6f 67 5f 66 70 3b 0a ..FILE *log_fp;.
80e0: 09 75 69 64 5f 74 20 75 73 65 72 20 3d 20 30 3b .uid_t user = 0;
80f0: 0a 09 69 6e 74 20 70 6f 72 74 20 3d 20 50 4f 52 ..int port = POR
8100: 54 2c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 20 T, thread_count
8110: 3d 20 54 48 52 45 41 44 5f 43 4f 55 4e 54 3b 0a = THREAD_COUNT;.
8120: 09 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 20 .int cache_size
8130: 3d 20 43 41 43 48 45 5f 53 49 5a 45 3b 0a 09 69 = CACHE_SIZE;..i
8140: 6e 74 20 69 6e 69 74 5f 72 65 74 2c 20 63 68 72 nt init_ret, chr
8150: 6f 6f 74 5f 72 65 74 2c 20 73 65 74 75 69 64 5f oot_ret, setuid_
8160: 72 65 74 2c 20 6c 6f 6f 6b 75 70 5f 72 65 74 2c ret, lookup_ret,
8170: 20 63 68 64 69 72 5f 72 65 74 3b 0a 09 69 6e 74 chdir_ret;..int
8180: 20 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 20 setuid_enabled
8190: 3d 20 30 2c 20 64 61 65 6d 6f 6e 5f 65 6e 61 62 = 0, daemon_enab
81a0: 6c 65 64 20 3d 20 30 3b 0a 09 69 6e 74 20 63 68 led = 0;..int ch
81b0: 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 09 2f 2a 20 ;..int fd;.../*
81c0: 53 65 74 20 64 65 66 61 75 6c 74 20 76 61 6c 75 Set default valu
81d0: 65 73 20 2a 2f 0a 09 74 68 72 65 61 64 5f 6f 70 es */..thread_op
81e0: 74 69 6f 6e 73 2e 76 68 6f 73 74 73 5f 65 6e 61 tions.vhosts_ena
81f0: 62 6c 65 64 20 3d 20 30 3b 0a 0a 09 2f 2a 20 50 bled = 0;.../* P
8200: 72 6f 63 65 73 73 20 61 72 67 75 6d 65 6e 74 73 rocess arguments
8210: 20 2a 2f 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 */..filed_getop
8220: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f t_long_setopt(&o
8230: 70 74 69 6f 6e 73 5b 30 5d 2c 20 22 70 6f 72 74 ptions[0], "port
8240: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 ", required_argu
8250: 6d 65 6e 74 2c 20 27 70 27 29 3b 0a 09 66 69 6c ment, 'p');..fil
8260: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 ed_getopt_long_s
8270: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 etopt(&options[1
8280: 5d 2c 20 22 74 68 72 65 61 64 73 22 2c 20 72 65 ], "threads", re
8290: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c quired_argument,
82a0: 20 27 74 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 't');..filed_ge
82b0: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 topt_long_setopt
82c0: 28 26 6f 70 74 69 6f 6e 73 5b 32 5d 2c 20 22 63 (&options[2], "c
82d0: 61 63 68 65 22 2c 20 72 65 71 75 69 72 65 64 5f ache", required_
82e0: 61 72 67 75 6d 65 6e 74 2c 20 27 63 27 29 3b 0a argument, 'c');.
82f0: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
8300: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
8310: 6e 73 5b 33 5d 2c 20 22 62 69 6e 64 22 2c 20 72 ns[3], "bind", r
8320: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 equired_argument
8330: 2c 20 27 62 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 'b');..filed_g
8340: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
8350: 74 28 26 6f 70 74 69 6f 6e 73 5b 34 5d 2c 20 22 t(&options[4], "
8360: 75 73 65 72 22 2c 20 72 65 71 75 69 72 65 64 5f user", required_
8370: 61 72 67 75 6d 65 6e 74 2c 20 27 75 27 29 3b 0a argument, 'u');.
8380: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
8390: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
83a0: 6e 73 5b 35 5d 2c 20 22 72 6f 6f 74 22 2c 20 72 ns[5], "root", r
83b0: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 equired_argument
83c0: 2c 20 27 72 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 'r');..filed_g
83d0: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
83e0: 74 28 26 6f 70 74 69 6f 6e 73 5b 36 5d 2c 20 22 t(&options[6], "
83f0: 68 65 6c 70 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 help", no_argume
8400: 6e 74 2c 20 27 68 27 29 3b 0a 09 66 69 6c 65 64 nt, 'h');..filed
8410: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 _getopt_long_set
8420: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 37 5d 2c opt(&options[7],
8430: 20 22 64 61 65 6d 6f 6e 22 2c 20 6e 6f 5f 61 72 "daemon", no_ar
8440: 67 75 6d 65 6e 74 2c 20 27 64 27 29 3b 0a 09 66 gument, 'd');..f
8450: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
8460: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
8470: 5b 38 5d 2c 20 22 6c 6f 67 22 2c 20 72 65 71 75 [8], "log", requ
8480: 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 ired_argument, '
8490: 6c 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f l');..filed_geto
84a0: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
84b0: 6f 70 74 69 6f 6e 73 5b 39 5d 2c 20 22 76 65 72 options[9], "ver
84c0: 73 69 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 sion", no_argume
84d0: 6e 74 2c 20 27 76 27 29 3b 0a 09 66 69 6c 65 64 nt, 'v');..filed
84e0: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 _getopt_long_set
84f0: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 30 5d opt(&options[10]
8500: 2c 20 22 76 68 6f 73 74 22 2c 20 6e 6f 5f 61 72 , "vhost", no_ar
8510: 67 75 6d 65 6e 74 2c 20 27 56 27 29 3b 0a 09 66 gument, 'V');..f
8520: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
8530: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
8540: 5b 31 31 5d 2c 20 4e 55 4c 4c 2c 20 30 2c 20 30 [11], NULL, 0, 0
8550: 29 3b 0a 09 77 68 69 6c 65 20 28 28 63 68 20 3d );..while ((ch =
8560: 20 67 65 74 6f 70 74 5f 6c 6f 6e 67 28 61 72 67 getopt_long(arg
8570: 63 2c 20 61 72 67 76 2c 20 22 70 3a 74 3a 63 3a c, argv, "p:t:c:
8580: 62 3a 75 3a 72 3a 6c 3a 68 64 76 56 22 2c 20 6f b:u:r:l:hdvV", o
8590: 70 74 69 6f 6e 73 2c 20 4e 55 4c 4c 29 29 20 21 ptions, NULL)) !
85a0: 3d 20 2d 31 29 20 7b 0a 09 09 73 77 69 74 63 68 = -1) {...switch
85b0: 28 63 68 29 20 7b 0a 09 09 09 63 61 73 65 20 27 (ch) {....case '
85c0: 70 27 3a 0a 09 09 09 09 70 6f 72 74 20 3d 20 61 p':.....port = a
85d0: 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 toi(optarg);....
85e0: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
85f0: 27 74 27 3a 0a 09 09 09 09 74 68 72 65 61 64 5f 't':.....thread_
8600: 63 6f 75 6e 74 20 3d 20 61 74 6f 69 28 6f 70 74 count = atoi(opt
8610: 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b arg);.....break;
8620: 0a 09 09 09 63 61 73 65 20 27 63 27 3a 0a 09 09 ....case 'c':...
8630: 09 09 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 61 ..cache_size = a
8640: 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 toi(optarg);....
8650: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
8660: 27 62 27 3a 0a 09 09 09 09 62 69 6e 64 5f 61 64 'b':.....bind_ad
8670: 64 72 20 3d 20 73 74 72 64 75 70 28 6f 70 74 61 dr = strdup(opta
8680: 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a rg);.....break;.
8690: 09 09 09 63 61 73 65 20 27 75 27 3a 0a 09 09 09 ...case 'u':....
86a0: 09 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 20 .setuid_enabled
86b0: 3d 20 31 3b 0a 09 09 09 09 6c 6f 6f 6b 75 70 5f = 1;.....lookup_
86c0: 72 65 74 20 3d 20 66 69 6c 65 64 5f 75 73 65 72 ret = filed_user
86d0: 5f 6c 6f 6f 6b 75 70 28 6f 70 74 61 72 67 2c 20 _lookup(optarg,
86e0: 26 75 73 65 72 29 3b 0a 09 09 09 09 69 66 20 28 &user);.....if (
86f0: 6c 6f 6f 6b 75 70 5f 72 65 74 20 21 3d 20 30 29 lookup_ret != 0)
8700: 20 7b 0a 09 09 09 09 09 66 69 6c 65 64 5f 70 72 {......filed_pr
8710: 69 6e 74 5f 68 65 6c 70 28 73 74 64 65 72 72 2c int_help(stderr,
8720: 20 30 2c 20 22 49 6e 76 61 6c 69 64 20 75 73 65 0, "Invalid use
8730: 72 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64 22 rname specified"
8740: 29 3b 0a 0a 09 09 09 09 09 72 65 74 75 72 6e 28 );.......return(
8750: 31 29 3b 0a 09 09 09 09 7d 0a 09 09 09 09 62 72 1);.....}.....br
8760: 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 72 27 eak;....case 'r'
8770: 3a 0a 09 09 09 09 6e 65 77 72 6f 6f 74 20 3d 20 :.....newroot =
8780: 73 74 72 64 75 70 28 6f 70 74 61 72 67 29 3b 0a strdup(optarg);.
8790: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 ....break;....ca
87a0: 73 65 20 27 6c 27 3a 0a 09 09 09 09 6c 6f 67 5f se 'l':.....log_
87b0: 66 69 6c 65 20 3d 20 73 74 72 64 75 70 28 6f 70 file = strdup(op
87c0: 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b targ);.....break
87d0: 3b 0a 09 09 09 63 61 73 65 20 27 64 27 3a 0a 09 ;....case 'd':..
87e0: 09 09 09 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 ...daemon_enable
87f0: 64 20 3d 20 31 3b 0a 09 09 09 09 62 72 65 61 6b d = 1;.....break
8800: 3b 0a 09 09 09 63 61 73 65 20 27 56 27 3a 0a 09 ;....case 'V':..
8810: 09 09 09 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e ...thread_option
8820: 73 2e 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 64 s.vhosts_enabled
8830: 20 3d 20 31 3b 0a 0a 09 09 09 09 62 72 65 61 6b = 1;......break
8840: 3b 0a 09 09 09 63 61 73 65 20 27 76 27 3a 0a 09 ;....case 'v':..
8850: 09 09 09 70 72 69 6e 74 66 28 22 66 69 6c 65 64 ...printf("filed
8860: 20 76 65 72 73 69 6f 6e 20 25 73 5c 6e 22 2c 20 version %s\n",
8870: 46 49 4c 45 44 5f 56 45 52 53 49 4f 4e 29 3b 0a FILED_VERSION);.
8880: 0a 09 09 09 09 72 65 74 75 72 6e 28 30 29 3b 0a .....return(0);.
8890: 09 09 09 63 61 73 65 20 27 3f 27 3a 0a 09 09 09 ...case '?':....
88a0: 63 61 73 65 20 27 3a 27 3a 0a 09 09 09 09 66 69 case ':':.....fi
88b0: 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73 led_print_help(s
88c0: 74 64 65 72 72 2c 20 30 2c 20 4e 55 4c 4c 29 3b tderr, 0, NULL);
88d0: 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 31 29 3b ......return(1);
88e0: 0a 09 09 09 63 61 73 65 20 27 68 27 3a 0a 09 09 ....case 'h':...
88f0: 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 ..filed_print_he
8900: 6c 70 28 73 74 64 6f 75 74 2c 20 31 2c 20 4e 55 lp(stdout, 1, NU
8910: 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e LL);......return
8920: 28 30 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a (0);...}..}.../*
8930: 20 4f 70 65 6e 20 6c 6f 67 20 66 69 6c 65 20 2a Open log file *
8940: 2f 0a 09 6c 6f 67 5f 66 70 20 3d 20 66 69 6c 65 /..log_fp = file
8950: 64 5f 6c 6f 67 5f 6f 70 65 6e 28 6c 6f 67 5f 66 d_log_open(log_f
8960: 69 6c 65 29 3b 0a 09 69 66 20 28 6c 6f 67 5f 66 ile);..if (log_f
8970: 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 70 p == NULL) {...p
8980: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 6f 67 error("filed_log
8990: 5f 6f 70 65 6e 22 29 3b 0a 0a 09 09 72 65 74 75 _open");....retu
89a0: 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 rn(4);..}.../* C
89b0: 72 65 61 74 65 20 6c 69 73 74 65 6e 69 6e 67 20 reate listening
89c0: 73 6f 63 6b 65 74 20 2a 2f 0a 09 66 64 20 3d 20 socket */..fd =
89d0: 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 62 69 6e filed_listen(bin
89e0: 64 5f 61 64 64 72 2c 20 70 6f 72 74 29 3b 0a 09 d_addr, port);..
89f0: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 if (fd < 0) {...
8a00: 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 69 perror("filed_li
8a10: 73 74 65 6e 22 29 3b 0a 0a 09 09 72 65 74 75 72 sten");....retur
8a20: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 42 65 n(1);..}.../* Be
8a30: 63 6f 6d 65 20 61 20 64 61 65 6d 6f 6e 20 2a 2f come a daemon */
8a40: 0a 09 69 66 20 28 64 61 65 6d 6f 6e 5f 65 6e 61 ..if (daemon_ena
8a50: 62 6c 65 64 29 20 7b 0a 09 09 69 6e 69 74 5f 72 bled) {...init_r
8a60: 65 74 20 3d 20 66 69 6c 65 64 5f 64 61 65 6d 6f et = filed_daemo
8a70: 6e 69 7a 65 28 29 3b 0a 09 09 69 66 20 28 69 6e nize();...if (in
8a80: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 it_ret != 0) {..
8a90: 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f ..perror("filed_
8aa0: 64 61 65 6d 6f 6e 69 7a 65 22 29 3b 0a 0a 09 09 daemonize");....
8ab0: 09 72 65 74 75 72 6e 28 36 29 3b 0a 09 09 7d 0a .return(6);...}.
8ac0: 09 7d 0a 0a 09 2f 2a 20 43 68 72 6f 6f 74 2c 20 .}.../* Chroot,
8ad0: 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a if appropriate *
8ae0: 2f 0a 09 69 66 20 28 6e 65 77 72 6f 6f 74 29 20 /..if (newroot)
8af0: 7b 0a 09 09 63 68 64 69 72 5f 72 65 74 20 3d 20 {...chdir_ret =
8b00: 63 68 64 69 72 28 6e 65 77 72 6f 6f 74 29 3b 0a chdir(newroot);.
8b10: 09 09 69 66 20 28 63 68 64 69 72 5f 72 65 74 20 ..if (chdir_ret
8b20: 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f != 0) {....perro
8b30: 72 28 22 63 68 64 69 72 22 29 3b 0a 0a 09 09 09 r("chdir");.....
8b40: 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a return(1);...}..
8b50: 09 09 63 68 72 6f 6f 74 5f 72 65 74 20 3d 20 63 ..chroot_ret = c
8b60: 68 72 6f 6f 74 28 22 2e 22 29 3b 0a 09 09 69 66 hroot(".");...if
8b70: 20 28 63 68 72 6f 6f 74 5f 72 65 74 20 21 3d 20 (chroot_ret !=
8b80: 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 0) {....perror("
8b90: 63 68 72 6f 6f 74 22 29 3b 0a 0a 09 09 09 72 65 chroot");.....re
8ba0: 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a turn(1);...}..}.
8bb0: 0a 09 2f 2a 20 44 72 6f 70 20 70 72 69 76 69 6c ../* Drop privil
8bc0: 65 67 65 73 2c 20 69 66 20 61 70 70 72 6f 70 72 eges, if appropr
8bd0: 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 73 65 74 iate */..if (set
8be0: 75 69 64 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 uid_enabled) {..
8bf0: 09 73 65 74 75 69 64 5f 72 65 74 20 3d 20 73 65 .setuid_ret = se
8c00: 74 75 69 64 28 75 73 65 72 29 3b 0a 09 09 69 66 tuid(user);...if
8c10: 20 28 73 65 74 75 69 64 5f 72 65 74 20 21 3d 20 (setuid_ret !=
8c20: 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 0) {....perror("
8c30: 73 65 74 75 69 64 22 29 3b 0a 0a 09 09 09 72 65 setuid");.....re
8c40: 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a turn(1);...}..}.
8c50: 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ../* Initialize
8c60: 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 */..init_ret = f
8c70: 69 6c 65 64 5f 69 6e 69 74 28 63 61 63 68 65 5f iled_init(cache_
8c80: 73 69 7a 65 29 3b 0a 09 69 66 20 28 69 6e 69 74 size);..if (init
8c90: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 _ret != 0) {...p
8ca0: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 69 6e 69 error("filed_ini
8cb0: 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 33 t");....return(3
8cc0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 );..}.../* Creat
8cd0: 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64 e logging thread
8ce0: 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 */..init_ret =
8cf0: 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 filed_logging_th
8d00: 72 65 61 64 5f 69 6e 69 74 28 6c 6f 67 5f 66 70 read_init(log_fp
8d10: 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 );..if (init_ret
8d20: 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f != 0) {...perro
8d30: 72 28 22 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 r("filed_logging
8d40: 5f 74 68 72 65 61 64 5f 69 6e 69 74 22 29 3b 0a _thread_init");.
8d50: 0a 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d ...return(4);..}
8d60: 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 .../* Create wor
8d70: 6b 65 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 09 ker threads */..
8d80: 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 init_ret = filed
8d90: 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f _worker_threads_
8da0: 69 6e 69 74 28 66 64 2c 20 74 68 72 65 61 64 5f init(fd, thread_
8db0: 63 6f 75 6e 74 2c 20 26 74 68 72 65 61 64 5f 6f count, &thread_o
8dc0: 70 74 69 6f 6e 73 29 3b 0a 09 69 66 20 28 69 6e ptions);..if (in
8dd0: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 it_ret != 0) {..
8de0: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 77 .perror("filed_w
8df0: 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e orker_threads_in
8e00: 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 it");....return(
8e10: 35 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 61 69 74 5);..}.../* Wait
8e20: 20 66 6f 72 20 74 68 72 65 61 64 73 20 74 6f 20 for threads to
8e30: 65 78 69 74 20 2a 2f 0a 09 2f 2a 20 58 58 58 3a exit */../* XXX:
8e40: 54 4f 44 4f 3a 20 4d 6f 6e 69 74 6f 72 20 74 68 TODO: Monitor th
8e50: 72 65 61 64 20 75 73 61 67 65 20 2a 2f 0a 09 77 read usage */..w
8e60: 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 73 6c 65 hile (1) {...sle
8e70: 65 70 28 36 30 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 ep(60);..}.../*
8e80: 52 65 74 75 72 6e 20 69 6e 20 66 61 69 6c 75 72 Return in failur
8e90: 65 20 2a 2f 0a 09 72 65 74 75 72 6e 28 32 29 3b e */..return(2);
8ea0: 0a 7d 0a .}.