0000: 2f 2a 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20 /*. * Copyright
0010: 28 63 29 20 32 30 31 34 20 2d 20 32 30 31 36 2c (c) 2014 - 2016,
0020: 20 52 6f 79 20 4b 65 65 6e 65 0a 20 2a 20 41 6c Roy Keene. * Al
0030: 6c 20 72 69 67 68 74 73 20 72 65 73 65 72 76 65 l rights reserve
0040: 64 2e 0a 20 2a 20 0a 20 2a 20 52 65 64 69 73 74 d.. * . * Redist
0050: 72 69 62 75 74 69 6f 6e 20 61 6e 64 20 75 73 65 ribution and use
0060: 20 69 6e 20 73 6f 75 72 63 65 20 61 6e 64 20 62 in source and b
0070: 69 6e 61 72 79 20 66 6f 72 6d 73 2c 20 77 69 74 inary forms, wit
0080: 68 20 6f 72 20 77 69 74 68 6f 75 74 0a 20 2a 20 h or without. *
0090: 6d 6f 64 69 66 69 63 61 74 69 6f 6e 2c 20 61 72 modification, ar
00a0: 65 20 70 65 72 6d 69 74 74 65 64 20 70 72 6f 76 e permitted prov
00b0: 69 64 65 64 20 74 68 61 74 20 74 68 65 20 66 6f ided that the fo
00c0: 6c 6c 6f 77 69 6e 67 20 63 6f 6e 64 69 74 69 6f llowing conditio
00d0: 6e 73 20 61 72 65 20 6d 65 74 3a 0a 20 2a 20 09 ns are met:. * .
00e0: 31 2e 20 52 65 64 69 73 74 72 69 62 75 74 69 6f 1. Redistributio
00f0: 6e 73 20 6f 66 20 73 6f 75 72 63 65 20 63 6f 64 ns of source cod
0100: 65 20 6d 75 73 74 20 72 65 74 61 69 6e 20 74 68 e must retain th
0110: 65 20 61 62 6f 76 65 20 63 6f 70 79 72 69 67 68 e above copyrigh
0120: 74 0a 20 2a 20 09 20 20 20 6e 6f 74 69 63 65 2c t. * . notice,
0130: 20 74 68 69 73 20 6c 69 73 74 20 6f 66 20 63 6f this list of co
0140: 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nditions and the
0150: 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 69 73 63 6c following discl
0160: 61 69 6d 65 72 2e 0a 20 2a 20 09 32 2e 20 52 65 aimer.. * .2. Re
0170: 64 69 73 74 72 69 62 75 74 69 6f 6e 73 20 69 6e distributions in
0180: 20 62 69 6e 61 72 79 20 66 6f 72 6d 20 6d 75 73 binary form mus
0190: 74 20 72 65 70 72 6f 64 75 63 65 20 74 68 65 20 t reproduce the
01a0: 61 62 6f 76 65 20 63 6f 70 79 72 69 67 68 74 0a above copyright.
01b0: 20 2a 20 09 20 20 20 6e 6f 74 69 63 65 2c 20 74 * . notice, t
01c0: 68 69 73 20 6c 69 73 74 20 6f 66 20 63 6f 6e 64 his list of cond
01d0: 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 66 itions and the f
01e0: 6f 6c 6c 6f 77 69 6e 67 20 64 69 73 63 6c 61 69 ollowing disclai
01f0: 6d 65 72 20 69 6e 20 74 68 65 0a 20 2a 20 09 20 mer in the. * .
0200: 20 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 documentation
0210: 61 6e 64 2f 6f 72 20 6f 74 68 65 72 20 6d 61 74 and/or other mat
0220: 65 72 69 61 6c 73 20 70 72 6f 76 69 64 65 64 20 erials provided
0230: 77 69 74 68 20 74 68 65 20 64 69 73 74 72 69 62 with the distrib
0240: 75 74 69 6f 6e 2e 0a 20 2a 20 0a 20 2a 20 54 48 ution.. * . * TH
0250: 49 53 20 53 4f 46 54 57 41 52 45 20 49 53 20 50 IS SOFTWARE IS P
0260: 52 4f 56 49 44 45 44 20 42 59 20 54 48 45 20 43 ROVIDED BY THE C
0270: 4f 50 59 52 49 47 48 54 20 48 4f 4c 44 45 52 53 OPYRIGHT HOLDERS
0280: 20 41 4e 44 20 43 4f 4e 54 52 49 42 55 54 4f 52 AND CONTRIBUTOR
0290: 53 20 22 41 53 20 49 53 22 20 0a 20 2a 20 41 4e S "AS IS" . * AN
02a0: 44 20 41 4e 59 20 45 58 50 52 45 53 53 20 4f 52 D ANY EXPRESS OR
02b0: 20 49 4d 50 4c 49 45 44 20 57 41 52 52 41 4e 54 IMPLIED WARRANT
02c0: 49 45 53 2c 20 49 4e 43 4c 55 44 49 4e 47 2c 20 IES, INCLUDING,
02d0: 42 55 54 20 4e 4f 54 20 4c 49 4d 49 54 45 44 20 BUT NOT LIMITED
02e0: 54 4f 2c 20 54 48 45 20 0a 20 2a 20 49 4d 50 4c TO, THE . * IMPL
02f0: 49 45 44 20 57 41 52 52 41 4e 54 49 45 53 20 4f IED WARRANTIES O
0300: 46 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 F MERCHANTABILIT
0310: 59 20 41 4e 44 20 46 49 54 4e 45 53 53 20 46 4f Y AND FITNESS FO
0320: 52 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 R A PARTICULAR P
0330: 55 52 50 4f 53 45 20 0a 20 2a 20 41 52 45 20 44 URPOSE . * ARE D
0340: 49 53 43 4c 41 49 4d 45 44 2e 20 20 49 4e 20 4e ISCLAIMED. IN N
0350: 4f 20 45 56 45 4e 54 20 53 48 41 4c 4c 20 54 48 O EVENT SHALL TH
0360: 45 20 43 4f 50 59 52 49 47 48 54 20 48 4f 4c 44 E COPYRIGHT HOLD
0370: 45 52 20 4f 52 20 43 4f 4e 54 52 49 42 55 54 4f ER OR CONTRIBUTO
0380: 52 53 20 42 45 20 0a 20 2a 20 4c 49 41 42 4c 45 RS BE . * LIABLE
0390: 20 46 4f 52 20 41 4e 59 20 44 49 52 45 43 54 2c FOR ANY DIRECT,
03a0: 20 49 4e 44 49 52 45 43 54 2c 20 49 4e 43 49 44 INDIRECT, INCID
03b0: 45 4e 54 41 4c 2c 20 53 50 45 43 49 41 4c 2c 20 ENTAL, SPECIAL,
03c0: 45 58 45 4d 50 4c 41 52 59 2c 20 4f 52 20 0a 20 EXEMPLARY, OR .
03d0: 2a 20 43 4f 4e 53 45 51 55 45 4e 54 49 41 4c 20 * CONSEQUENTIAL
03e0: 44 41 4d 41 47 45 53 20 28 49 4e 43 4c 55 44 49 DAMAGES (INCLUDI
03f0: 4e 47 2c 20 42 55 54 20 4e 4f 54 20 4c 49 4d 49 NG, BUT NOT LIMI
0400: 54 45 44 20 54 4f 2c 20 50 52 4f 43 55 52 45 4d TED TO, PROCUREM
0410: 45 4e 54 20 4f 46 20 0a 20 2a 20 53 55 42 53 54 ENT OF . * SUBST
0420: 49 54 55 54 45 20 47 4f 4f 44 53 20 4f 52 20 53 ITUTE GOODS OR S
0430: 45 52 56 49 43 45 53 3b 20 4c 4f 53 53 20 4f 46 ERVICES; LOSS OF
0440: 20 55 53 45 2c 20 44 41 54 41 2c 20 4f 52 20 50 USE, DATA, OR P
0450: 52 4f 46 49 54 53 3b 20 4f 52 20 42 55 53 49 4e ROFITS; OR BUSIN
0460: 45 53 53 20 0a 20 2a 20 49 4e 54 45 52 52 55 50 ESS . * INTERRUP
0470: 54 49 4f 4e 29 20 48 4f 57 45 56 45 52 20 43 41 TION) HOWEVER CA
0480: 55 53 45 44 20 41 4e 44 20 4f 4e 20 41 4e 59 20 USED AND ON ANY
0490: 54 48 45 4f 52 59 20 4f 46 20 4c 49 41 42 49 4c THEORY OF LIABIL
04a0: 49 54 59 2c 20 57 48 45 54 48 45 52 20 49 4e 20 ITY, WHETHER IN
04b0: 0a 20 2a 20 43 4f 4e 54 52 41 43 54 2c 20 53 54 . * CONTRACT, ST
04c0: 52 49 43 54 20 4c 49 41 42 49 4c 49 54 59 2c 20 RICT LIABILITY,
04d0: 4f 52 20 54 4f 52 54 20 28 49 4e 43 4c 55 44 49 OR TORT (INCLUDI
04e0: 4e 47 20 4e 45 47 4c 49 47 45 4e 43 45 20 4f 52 NG NEGLIGENCE OR
04f0: 20 4f 54 48 45 52 57 49 53 45 29 20 0a 20 2a 20 OTHERWISE) . *
0500: 41 52 49 53 49 4e 47 20 49 4e 20 41 4e 59 20 57 ARISING IN ANY W
0510: 41 59 20 4f 55 54 20 4f 46 20 54 48 45 20 55 53 AY OUT OF THE US
0520: 45 20 4f 46 20 54 48 49 53 20 53 4f 46 54 57 41 E OF THIS SOFTWA
0530: 52 45 2c 20 45 56 45 4e 20 49 46 20 41 44 56 49 RE, EVEN IF ADVI
0540: 53 45 44 20 4f 46 20 54 48 45 20 0a 20 2a 20 50 SED OF THE . * P
0550: 4f 53 53 49 42 49 4c 49 54 59 20 4f 46 20 53 55 OSSIBILITY OF SU
0560: 43 48 20 44 41 4d 41 47 45 2e 0a 20 2a 2f 0a 23 CH DAMAGE.. */.#
0570: 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 65 6e include <sys/sen
0580: 64 66 69 6c 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 dfile.h>.#includ
0590: 65 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68 3e e <sys/socket.h>
05a0: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 .#include <sys/t
05b0: 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 ypes.h>.#include
05c0: 20 3c 61 72 70 61 2f 69 6e 65 74 2e 68 3e 0a 23 <arpa/inet.h>.#
05d0: 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d 61 include <sys/mma
05e0: 6e 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 n.h>.#include <s
05f0: 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 6c ys/stat.h>.#incl
0600: 75 64 65 20 3c 73 79 73 2f 77 61 69 74 2e 68 3e ude <sys/wait.h>
0610: 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65 .#include <pthre
0620: 61 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c ad.h>.#include <
0630: 73 74 72 69 6e 67 73 2e 68 3e 0a 23 69 6e 63 6c strings.h>.#incl
0640: 75 64 65 20 3c 73 69 67 6e 61 6c 2e 68 3e 0a 23 ude <signal.h>.#
0650: 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e include <stdlib.
0660: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 h>.#include <uni
0670: 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 std.h>.#include
0680: 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c <string.h>.#incl
0690: 75 64 65 20 3c 67 65 74 6f 70 74 2e 68 3e 0a 23 ude <getopt.h>.#
06a0: 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e include <stdarg.
06b0: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e h>.#include <fcn
06c0: 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c tl.h>.#include <
06d0: 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 stdio.h>.#includ
06e0: 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 23 69 6e 63 e <errno.h>.#inc
06f0: 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 23 69 lude <time.h>.#i
0700: 6e 63 6c 75 64 65 20 3c 70 77 64 2e 68 3e 0a 0a nclude <pwd.h>..
0710: 2f 2a 20 43 6f 6d 70 69 6c 65 20 74 69 6d 65 20 /* Compile time
0720: 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 23 64 65 constants */.#de
0730: 66 69 6e 65 20 46 49 4c 45 44 5f 56 45 52 53 49 fine FILED_VERSI
0740: 4f 4e 20 22 31 2e 32 31 22 0a 23 64 65 66 69 6e ON "1.21".#defin
0750: 65 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 e FILED_SENDFILE
0760: 5f 4d 41 58 20 31 36 37 37 37 32 31 35 0a 23 64 _MAX 16777215.#d
0770: 65 66 69 6e 65 20 46 49 4c 45 44 5f 4d 41 58 5f efine FILED_MAX_
0780: 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 20 33 30 FAILURE_COUNT 30
0790: 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 44 .#define FILED_D
07a0: 45 46 41 55 4c 54 5f 54 59 50 45 20 22 61 70 70 EFAULT_TYPE "app
07b0: 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d 73 lication/octet-s
07c0: 74 72 65 61 6d 22 0a 23 64 65 66 69 6e 65 20 46 tream".#define F
07d0: 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 ILED_PATH_BUFFER
07e0: 5f 53 49 5a 45 20 31 30 31 30 0a 0a 2f 2a 20 44 _SIZE 1010../* D
07f0: 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f efault values */
0800: 0a 23 64 65 66 69 6e 65 20 50 4f 52 54 20 38 30 .#define PORT 80
0810: 0a 23 64 65 66 69 6e 65 20 54 48 52 45 41 44 5f .#define THREAD_
0820: 43 4f 55 4e 54 20 35 0a 23 64 65 66 69 6e 65 20 COUNT 5.#define
0830: 42 49 4e 44 5f 41 44 44 52 20 22 3a 3a 22 0a 23 BIND_ADDR "::".#
0840: 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 49 5a define CACHE_SIZ
0850: 45 20 38 32 30 39 0a 23 64 65 66 69 6e 65 20 4c E 8209.#define L
0860: 4f 47 5f 46 49 4c 45 20 22 2d 22 0a 0a 2f 2a 20 OG_FILE "-"../*
0870: 46 75 7a 7a 69 6e 67 20 54 65 73 74 20 43 6f 64 Fuzzing Test Cod
0880: 65 20 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c 45 e */.#ifdef FILE
0890: 44 5f 54 45 53 54 5f 41 46 4c 0a 23 64 65 66 69 D_TEST_AFL.#defi
08a0: 6e 65 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f ne FILED_DONT_LO
08b0: 47 20 31 0a 23 64 65 66 69 6e 65 20 70 74 68 72 G 1.#define pthr
08c0: 65 61 64 5f 63 72 65 61 74 65 28 61 2c 20 78 2c ead_create(a, x,
08d0: 20 79 2c 20 7a 29 20 61 66 6c 5f 70 74 68 72 65 y, z) afl_pthre
08e0: 61 64 5f 63 72 65 61 74 65 28 61 2c 20 78 2c 20 ad_create(a, x,
08f0: 79 2c 20 7a 29 0a 23 64 65 66 69 6e 65 20 62 69 y, z).#define bi
0900: 6e 64 28 78 2c 20 79 2c 20 7a 29 20 61 66 6c 5f nd(x, y, z) afl_
0910: 62 69 6e 64 28 78 2c 20 79 2c 20 7a 29 0a 23 64 bind(x, y, z).#d
0920: 65 66 69 6e 65 20 73 6f 63 6b 65 74 28 78 2c 20 efine socket(x,
0930: 79 2c 20 7a 29 20 38 31 39 33 0a 23 64 65 66 69 y, z) 8193.#defi
0940: 6e 65 20 6c 69 73 74 65 6e 28 78 2c 20 79 29 20 ne listen(x, y)
0950: 30 0a 23 64 65 66 69 6e 65 20 61 63 63 65 70 74 0.#define accept
0960: 28 78 2c 20 79 2c 20 7a 29 20 61 66 6c 5f 61 63 (x, y, z) afl_ac
0970: 63 65 70 74 28 78 2c 20 79 2c 20 7a 29 0a 23 64 cept(x, y, z).#d
0980: 65 66 69 6e 65 20 63 6c 6f 73 65 28 78 29 20 7b efine close(x) {
0990: 20 69 66 20 28 73 74 72 63 6d 70 28 23 78 2c 20 if (strcmp(#x,
09a0: 22 72 61 6e 64 6f 6d 5f 66 64 22 29 20 3d 3d 20 "random_fd") ==
09b0: 30 29 20 7b 20 63 6c 6f 73 65 28 78 29 3b 20 7d 0) { close(x); }
09c0: 20 65 6c 73 65 20 7b 20 65 78 69 74 28 30 29 3b else { exit(0);
09d0: 20 7d 20 7d 0a 23 64 65 66 69 6e 65 20 66 63 6c } }.#define fcl
09e0: 6f 73 65 28 78 29 20 65 78 69 74 28 30 29 0a 0a ose(x) exit(0)..
09f0: 73 74 61 74 69 63 20 69 6e 74 20 61 66 6c 5f 61 static int afl_a
0a00: 63 63 65 70 74 28 69 6e 74 20 78 2c 20 76 6f 69 ccept(int x, voi
0a10: 64 20 2a 61 64 64 72 2c 20 76 6f 69 64 20 2a 7a d *addr, void *z
0a20: 29 20 7b 0a 09 28 28 73 74 72 75 63 74 20 73 6f ) {..((struct so
0a30: 63 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20 61 64 ckaddr_in6 *) ad
0a40: 64 72 29 2d 3e 73 69 6e 36 5f 66 61 6d 69 6c 79 dr)->sin6_family
0a50: 20 3d 20 41 46 5f 49 4e 45 54 20 2b 20 41 46 5f = AF_INET + AF_
0a60: 49 4e 45 54 36 20 2b 20 31 3b 0a 09 72 65 74 75 INET6 + 1;..retu
0a70: 72 6e 28 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 rn(STDIN_FILENO)
0a80: 3b 0a 09 78 20 3d 20 78 3b 0a 09 7a 20 3d 20 7a ;..x = x;..z = z
0a90: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 ;.}..static int
0aa0: 61 66 6c 5f 62 69 6e 64 28 69 6e 74 20 78 2c 20 afl_bind(int x,
0ab0: 76 6f 69 64 20 2a 79 2c 20 73 6f 63 6b 6c 65 6e void *y, socklen
0ac0: 5f 74 20 7a 29 20 7b 0a 09 72 65 74 75 72 6e 28 _t z) {..return(
0ad0: 38 31 39 34 29 3b 0a 09 78 20 3d 20 78 3b 0a 09 8194);..x = x;..
0ae0: 79 20 3d 20 79 3b 0a 09 7a 20 3d 20 7a 3b 0a 7d y = y;..z = z;.}
0af0: 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 6c ..static int afl
0b00: 5f 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 _pthread_create(
0b10: 70 74 68 72 65 61 64 5f 74 20 2a 74 68 72 65 61 pthread_t *threa
0b20: 64 2c 20 63 6f 6e 73 74 20 70 74 68 72 65 61 64 d, const pthread
0b30: 5f 61 74 74 72 5f 74 20 2a 61 74 74 72 2c 20 76 _attr_t *attr, v
0b40: 6f 69 64 20 2a 28 2a 73 74 61 72 74 5f 72 6f 75 oid *(*start_rou
0b50: 74 69 6e 65 29 20 28 76 6f 69 64 20 2a 29 2c 20 tine) (void *),
0b60: 76 6f 69 64 20 2a 61 72 67 29 20 7b 0a 09 73 74 void *arg) {..st
0b70: 61 72 74 5f 72 6f 75 74 69 6e 65 28 61 72 67 29 art_routine(arg)
0b80: 3b 0a 09 65 78 69 74 28 33 29 3b 0a 09 74 68 72 ;..exit(3);..thr
0b90: 65 61 64 20 3d 20 74 68 72 65 61 64 3b 0a 09 61 ead = thread;..a
0ba0: 74 74 72 20 3d 20 61 74 74 72 3b 0a 7d 0a 23 65 ttr = attr;.}.#e
0bb0: 6e 64 69 66 0a 0a 2f 2a 20 43 6f 6e 66 69 67 75 ndif../* Configu
0bc0: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 ration options t
0bd0: 68 61 74 20 77 6f 72 6b 20 74 68 72 65 61 64 73 hat work threads
0be0: 20 6e 65 65 64 20 74 6f 20 62 65 20 61 77 61 72 need to be awar
0bf0: 65 20 6f 66 20 2a 2f 0a 73 74 72 75 63 74 20 66 e of */.struct f
0c00: 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 7b 0a 09 iled_options {..
0c10: 69 6e 74 20 76 68 6f 73 74 73 5f 65 6e 61 62 6c int vhosts_enabl
0c20: 65 64 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 ed;..const char
0c30: 2a 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 3b 0a 7d *fake_newroot;.}
0c40: 3b 0a 0a 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 ;../* Arguments
0c50: 66 6f 72 20 77 6f 72 6b 65 72 20 74 68 72 65 61 for worker threa
0c60: 64 73 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c ds */.struct fil
0c70: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 ed_worker_thread
0c80: 5f 61 72 67 73 20 7b 0a 09 69 6e 74 20 66 64 3b _args {..int fd;
0c90: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f ..struct filed_o
0ca0: 70 74 69 6f 6e 73 20 6f 70 74 69 6f 6e 73 3b 0a ptions options;.
0cb0: 7d 3b 0a 0a 2f 2a 20 41 72 67 75 6d 65 6e 74 73 };../* Arguments
0cc0: 20 66 6f 72 20 6c 6f 67 67 69 6e 67 20 74 68 72 for logging thr
0cd0: 65 61 64 73 20 2a 2f 0a 73 74 72 75 63 74 20 66 eads */.struct f
0ce0: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 iled_logging_thr
0cf0: 65 61 64 5f 61 72 67 73 20 7b 0a 09 46 49 4c 45 ead_args {..FILE
0d00: 20 2a 66 70 3b 0a 7d 3b 0a 0a 2f 2a 20 46 69 6c *fp;.};../* Fil
0d10: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f e information */
0d20: 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 .struct filed_fi
0d30: 6c 65 69 6e 66 6f 20 7b 0a 09 70 74 68 72 65 61 leinfo {..pthrea
0d40: 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b d_mutex_t mutex;
0d50: 0a 09 63 68 61 72 20 70 61 74 68 5b 46 49 4c 45 ..char path[FILE
0d60: 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 D_PATH_BUFFER_SI
0d70: 5a 45 5d 3b 0a 09 69 6e 74 20 66 64 3b 0a 09 6f ZE];..int fd;..o
0d80: 66 66 5f 74 20 6c 65 6e 3b 0a 09 63 68 61 72 20 ff_t len;..char
0d90: 2a 6c 61 73 74 6d 6f 64 3b 0a 09 63 68 61 72 20 *lastmod;..char
0da0: 6c 61 73 74 6d 6f 64 5f 62 5b 36 34 5d 3b 0a 09 lastmod_b[64];..
0db0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 79 70 65 const char *type
0dc0: 3b 0a 09 63 68 61 72 20 65 74 61 67 5b 36 34 5d ;..char etag[64]
0dd0: 3b 0a 7d 3b 0a 0a 2f 2a 20 52 65 71 75 65 73 74 ;.};../* Request
0de0: 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 73 74 variables */.st
0df0: 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f ruct filed_http_
0e00: 72 65 71 75 65 73 74 20 7b 0a 09 2f 2a 2a 20 42 request {../** B
0e10: 75 66 66 65 72 73 20 2a 2a 2f 0a 09 73 74 72 75 uffers **/..stru
0e20: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 ct filed_fileinf
0e30: 6f 20 66 69 6c 65 69 6e 66 6f 3b 0a 09 63 68 61 o fileinfo;..cha
0e40: 72 20 74 6d 70 62 75 66 5b 46 49 4c 45 44 5f 50 r tmpbuf[FILED_P
0e50: 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d ATH_BUFFER_SIZE]
0e60: 3b 0a 0a 09 2f 2a 2a 20 48 54 54 50 20 52 65 71 ;.../** HTTP Req
0e70: 75 65 73 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e uest information
0e80: 20 2a 2a 2f 0a 09 2f 2a 2a 2a 20 54 79 70 65 20 **/../*** Type
0e90: 6f 66 20 72 65 71 75 65 73 74 20 28 48 45 41 44 of request (HEAD
0ea0: 20 6f 72 20 47 45 54 29 20 2a 2a 2a 2f 0a 09 65 or GET) ***/..e
0eb0: 6e 75 6d 20 7b 0a 09 09 46 49 4c 45 44 5f 52 45 num {...FILED_RE
0ec0: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 QUEST_METHOD_GET
0ed0: 2c 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45 53 ,...FILED_REQUES
0ee0: 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 0a 09 7d T_METHOD_HEAD..}
0ef0: 20 6d 65 74 68 6f 64 3b 0a 0a 09 2f 2a 2a 2a 20 method;.../***
0f00: 50 61 74 68 20 62 65 69 6e 67 20 72 65 71 75 65 Path being reque
0f10: 73 74 65 64 20 2a 2a 2a 2f 0a 09 63 68 61 72 20 sted ***/..char
0f20: 70 61 74 68 5b 46 49 4c 45 44 5f 50 41 54 48 5f path[FILED_PATH_
0f30: 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b 20 0a 0a BUFFER_SIZE]; ..
0f40: 09 2f 2a 2a 2a 20 50 61 74 68 20 74 79 70 65 20 ./*** Path type
0f50: 2a 2a 2a 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 ***/..enum {...F
0f60: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 ILED_REQUEST_TYP
0f70: 45 5f 44 49 52 45 43 54 4f 52 59 2c 0a 09 09 46 E_DIRECTORY,...F
0f80: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 ILED_REQUEST_TYP
0f90: 45 5f 4f 54 48 45 52 0a 09 7d 20 74 79 70 65 3b E_OTHER..} type;
0fa0: 0a 0a 09 73 74 72 75 63 74 20 7b 0a 09 09 73 74 ...struct {...st
0fb0: 72 75 63 74 20 7b 0a 09 09 09 69 6e 74 20 70 72 ruct {....int pr
0fc0: 65 73 65 6e 74 3b 0a 09 09 09 6f 66 66 5f 74 20 esent;....off_t
0fd0: 6f 66 66 73 65 74 3b 20 20 20 2f 2a 2a 2a 20 52 offset; /*** R
0fe0: 61 6e 67 65 20 73 74 61 72 74 20 2a 2a 2a 2f 0a ange start ***/.
0ff0: 09 09 09 6f 66 66 5f 74 20 6c 65 6e 67 74 68 3b ...off_t length;
1000: 20 20 20 2f 2a 2a 2a 20 52 61 6e 67 65 20 6c 65 /*** Range le
1010: 6e 67 74 68 20 2a 2a 2a 2f 0a 09 09 7d 20 72 61 ngth ***/...} ra
1020: 6e 67 65 3b 0a 0a 09 09 73 74 72 75 63 74 20 7b nge;....struct {
1030: 0a 09 09 09 69 6e 74 20 70 72 65 73 65 6e 74 3b ....int present;
1040: 0a 09 09 09 63 68 61 72 20 68 6f 73 74 5b 46 49 ....char host[FI
1050: 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f LED_PATH_BUFFER_
1060: 53 49 5a 45 5d 3b 0a 09 09 7d 20 68 6f 73 74 3b SIZE];...} host;
1070: 0a 0a 09 09 65 6e 75 6d 20 7b 0a 09 09 09 46 49 ....enum {....FI
1080: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 LED_CONNECTION_C
1090: 4c 4f 53 45 2c 0a 09 09 09 46 49 4c 45 44 5f 43 LOSE,....FILED_C
10a0: 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 ONNECTION_KEEP_A
10b0: 4c 49 56 45 0a 09 09 7d 20 63 6f 6e 6e 65 63 74 LIVE...} connect
10c0: 69 6f 6e 3b 0a 09 7d 20 68 65 61 64 65 72 73 3b ion;..} headers;
10d0: 0a 7d 3b 0a 0a 2f 2a 20 4c 6f 67 20 72 65 63 6f .};../* Log reco
10e0: 72 64 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c rd */.struct fil
10f0: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 7b 0a 09 ed_log_entry {..
1100: 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 67 20 65 /* Type of log e
1110: 6e 74 72 79 20 2a 2f 0a 09 65 6e 75 6d 20 7b 0a ntry */..enum {.
1120: 09 09 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 ..FILED_LOG_TYPE
1130: 5f 4d 45 53 53 41 47 45 2c 0a 09 09 46 49 4c 45 _MESSAGE,...FILE
1140: 44 5f 4c 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 D_LOG_TYPE_TRANS
1150: 46 45 52 0a 09 7d 20 74 79 70 65 3b 0a 0a 09 2f FER..} type;.../
1160: 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 * Linked list he
1170: 61 64 2f 74 61 69 6c 20 2a 2f 0a 09 73 74 72 75 ad/tail */..stru
1180: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ct filed_log_ent
1190: 72 79 20 2a 5f 6e 65 78 74 3b 0a 09 73 74 72 75 ry *_next;..stru
11a0: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ct filed_log_ent
11b0: 72 79 20 2a 5f 70 72 65 76 3b 0a 0a 09 2f 2a 20 ry *_prev;.../*
11c0: 54 68 72 65 61 64 20 66 72 6f 6d 20 77 68 69 63 Thread from whic
11d0: 68 20 74 68 69 73 20 6c 6f 67 20 65 6e 74 72 79 h this log entry
11e0: 20 65 6d 69 6e 61 74 65 73 20 2a 2f 0a 09 70 74 eminates */..pt
11f0: 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 3b 0a hread_t thread;.
1200: 0a 09 2f 2a 20 4d 65 73 73 61 67 65 20 62 75 66 ../* Message buf
1210: 66 65 72 20 66 6f 72 20 74 79 70 65 20 3d 20 4d fer for type = M
1220: 45 53 53 41 47 45 20 2a 2f 0a 09 2f 2a 20 50 61 ESSAGE */../* Pa
1230: 74 68 20 62 75 66 66 65 72 20 66 6f 72 20 74 79 th buffer for ty
1240: 70 65 20 3d 20 54 52 41 4e 53 46 45 52 20 2a 2f pe = TRANSFER */
1250: 0a 09 63 68 61 72 20 62 75 66 66 65 72 5b 46 49 ..char buffer[FI
1260: 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 5f LED_PATH_BUFFER_
1270: 53 49 5a 45 5d 3b 0a 0a 09 2f 2a 20 49 74 65 6d SIZE];.../* Item
1280: 73 20 66 6f 72 20 74 79 70 65 20 3d 20 54 52 41 s for type = TRA
1290: 4e 53 46 45 52 20 2a 2f 0a 09 69 6e 74 20 68 74 NSFER */..int ht
12a0: 74 70 5f 63 6f 64 65 3b 0a 09 63 6f 6e 73 74 20 tp_code;..const
12b0: 63 68 61 72 20 2a 72 65 61 73 6f 6e 3b 0a 09 74 char *reason;..t
12c0: 69 6d 65 5f 74 20 73 74 61 72 74 74 69 6d 65 3b ime_t starttime;
12d0: 0a 09 74 69 6d 65 5f 74 20 65 6e 64 74 69 6d 65 ..time_t endtime
12e0: 3b 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6f 66 66 ;..off_t req_off
12f0: 73 65 74 3b 0a 09 6f 66 66 5f 74 20 72 65 71 5f set;..off_t req_
1300: 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 5f 74 20 73 length;..off_t s
1310: 65 6e 74 5f 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 ent_length;..off
1320: 5f 74 20 66 69 6c 65 5f 6c 65 6e 67 74 68 3b 0a _t file_length;.
1330: 09 63 68 61 72 20 69 70 5b 31 32 38 5d 3b 0a 09 .char ip[128];..
1340: 69 6e 74 20 70 6f 72 74 3b 0a 09 69 6e 74 20 6d int port;..int m
1350: 65 74 68 6f 64 3b 0a 7d 3b 0a 0a 2f 2a 20 47 6c ethod;.};../* Gl
1360: 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 2a obal variables *
1370: 2f 0a 2f 2a 2a 20 4f 70 65 6e 20 46 69 6c 65 20 /./** Open File
1380: 63 61 63 68 65 20 2a 2a 2f 0a 73 74 72 75 63 74 cache **/.struct
1390: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 filed_fileinfo
13a0: 2a 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f *filed_fileinfo_
13b0: 66 64 63 61 63 68 65 20 3d 20 4e 55 4c 4c 3b 0a fdcache = NULL;.
13c0: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 66 69 6c unsigned int fil
13d0: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 ed_fileinfo_fdca
13e0: 63 68 65 5f 73 69 7a 65 20 3d 20 30 3b 0a 0a 2f che_size = 0;../
13f0: 2a 2a 20 4c 6f 67 67 69 6e 67 20 2a 2a 2f 0a 73 ** Logging **/.s
1400: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
1410: 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c 6f 67 entry *filed_log
1420: 5f 6d 73 67 5f 6c 69 73 74 3b 0a 70 74 68 72 65 _msg_list;.pthre
1430: 61 64 5f 6d 75 74 65 78 5f 74 20 66 69 6c 65 64 ad_mutex_t filed
1440: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 _log_msg_list_mu
1450: 74 65 78 3b 0a 70 74 68 72 65 61 64 5f 63 6f 6e tex;.pthread_con
1460: 64 5f 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 d_t filed_log_ms
1470: 67 5f 6c 69 73 74 5f 72 65 61 64 79 3b 0a 0a 2f g_list_ready;../
1480: 2a 20 53 69 67 6e 61 6c 20 48 61 6e 64 6c 65 72 * Signal Handler
1490: 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */.static void
14a0: 66 69 6c 65 64 5f 73 69 67 6e 61 6c 5f 68 61 6e filed_signal_han
14b0: 64 6c 65 72 28 69 6e 74 20 73 69 67 6e 61 6c 5f dler(int signal_
14c0: 6e 75 6d 62 65 72 29 20 7b 0a 09 73 74 72 75 63 number) {..struc
14d0: 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f t filed_fileinfo
14e0: 20 2a 63 61 63 68 65 3b 0a 09 75 6e 73 69 67 6e *cache;..unsign
14f0: 65 64 20 69 6e 74 20 69 64 78 3b 0a 0a 09 73 77 ed int idx;...sw
1500: 69 74 63 68 20 28 73 69 67 6e 61 6c 5f 6e 75 6d itch (signal_num
1510: 62 65 72 29 20 7b 0a 09 09 63 61 73 65 20 53 49 ber) {...case SI
1520: 47 48 55 50 3a 0a 09 09 09 66 6f 72 20 28 69 64 GHUP:....for (id
1530: 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 66 69 6c x = 0; idx < fil
1540: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 ed_fileinfo_fdca
1550: 63 68 65 5f 73 69 7a 65 3b 20 69 64 78 2b 2b 29 che_size; idx++)
1560: 20 7b 0a 09 09 09 09 63 61 63 68 65 20 3d 20 26 {.....cache = &
1570: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
1580: 64 63 61 63 68 65 5b 69 64 78 5d 3b 0a 0a 09 09 dcache[idx];....
1590: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ..pthread_mutex_
15a0: 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 lock(&cache->mut
15b0: 65 78 29 3b 0a 0a 09 09 09 09 63 61 63 68 65 2d ex);......cache-
15c0: 3e 70 61 74 68 5b 30 5d 20 3d 20 27 5c 30 27 3b >path[0] = '\0';
15d0: 0a 09 09 09 09 69 66 20 28 63 61 63 68 65 2d 3e .....if (cache->
15e0: 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09 09 09 09 fd >= 0) {......
15f0: 63 6c 6f 73 65 28 63 61 63 68 65 2d 3e 66 64 29 close(cache->fd)
1600: 3b 0a 0a 09 09 09 09 09 63 61 63 68 65 2d 3e 66 ;.......cache->f
1610: 64 20 3d 20 2d 31 3b 0a 09 09 09 09 7d 0a 0a 09 d = -1;.....}...
1620: 09 09 09 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f ...cache->lastmo
1630: 64 20 3d 20 22 22 3b 0a 09 09 09 09 63 61 63 68 d = "";.....cach
1640: 65 2d 3e 74 79 70 65 20 3d 20 22 22 3b 0a 0a 09 e->type = "";...
1650: 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 ...pthread_mutex
1660: 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e _unlock(&cache->
1670: 6d 75 74 65 78 29 3b 0a 09 09 09 7d 0a 09 09 09 mutex);....}....
1680: 62 72 65 61 6b 3b 0a 09 7d 0a 0a 09 72 65 74 75 break;..}...retu
1690: 72 6e 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 rn;.}../* Initia
16a0: 6c 69 7a 65 20 63 61 63 68 65 20 2a 2f 0a 73 74 lize cache */.st
16b0: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 atic int filed_i
16c0: 6e 69 74 5f 63 61 63 68 65 28 75 6e 73 69 67 6e nit_cache(unsign
16d0: 65 64 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a ed int cache_siz
16e0: 65 29 20 7b 0a 09 75 6e 73 69 67 6e 65 64 20 69 e) {..unsigned i
16f0: 6e 74 20 69 64 78 3b 0a 09 69 6e 74 20 6d 75 74 nt idx;..int mut
1700: 65 78 5f 69 6e 69 74 5f 72 65 74 3b 0a 0a 09 2f ex_init_ret;.../
1710: 2a 20 43 61 63 68 65 20 6d 61 79 20 6e 6f 74 20 * Cache may not
1720: 62 65 20 72 65 2d 69 6e 69 74 69 61 6c 69 7a 65 be re-initialize
1730: 64 20 2a 2f 0a 09 69 66 20 28 66 69 6c 65 64 5f d */..if (filed_
1740: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
1750: 5f 73 69 7a 65 20 21 3d 20 30 20 7c 7c 20 66 69 _size != 0 || fi
1760: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
1770: 61 63 68 65 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a ache != NULL) {.
1780: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
1790: 0a 09 2f 2a 20 43 61 63 68 65 20 64 6f 65 73 20 ../* Cache does
17a0: 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 61 not need to be a
17b0: 6c 6c 6f 63 61 74 65 64 20 69 66 20 63 61 63 68 llocated if cach
17c0: 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 e is not enabled
17d0: 20 2a 2f 0a 09 69 66 20 28 63 61 63 68 65 5f 73 */..if (cache_s
17e0: 69 7a 65 20 3d 3d 20 30 29 20 7b 0a 09 09 72 65 ize == 0) {...re
17f0: 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 2f 2a turn(0);..}.../*
1800: 20 41 6c 6c 6f 63 61 74 65 20 63 61 63 68 65 20 Allocate cache
1810: 2a 2f 0a 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e */..filed_filein
1820: 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 fo_fdcache_size
1830: 3d 20 63 61 63 68 65 5f 73 69 7a 65 3b 0a 09 66 = cache_size;..f
1840: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
1850: 63 61 63 68 65 20 3d 20 6d 61 6c 6c 6f 63 28 73 cache = malloc(s
1860: 69 7a 65 6f 66 28 2a 66 69 6c 65 64 5f 66 69 6c izeof(*filed_fil
1870: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 29 20 2a einfo_fdcache) *
1880: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f filed_fileinfo_
1890: 66 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 fdcache_size);..
18a0: 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e if (filed_filein
18b0: 66 6f 5f 66 64 63 61 63 68 65 20 3d 3d 20 4e 55 fo_fdcache == NU
18c0: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 LL) {...return(1
18d0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 );..}.../* Initi
18e0: 61 6c 69 7a 65 20 63 61 63 68 65 20 65 6e 74 72 alize cache entr
18f0: 69 65 73 20 2a 2f 0a 09 66 6f 72 20 28 69 64 78 ies */..for (idx
1900: 20 3d 20 30 3b 20 69 64 78 20 3c 20 66 69 6c 65 = 0; idx < file
1910: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
1920: 68 65 5f 73 69 7a 65 3b 20 69 64 78 2b 2b 29 20 he_size; idx++)
1930: 7b 0a 09 09 6d 75 74 65 78 5f 69 6e 69 74 5f 72 {...mutex_init_r
1940: 65 74 20 3d 20 70 74 68 72 65 61 64 5f 6d 75 74 et = pthread_mut
1950: 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 64 5f 66 ex_init(&filed_f
1960: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b ileinfo_fdcache[
1970: 69 64 78 5d 2e 6d 75 74 65 78 2c 20 4e 55 4c 4c idx].mutex, NULL
1980: 29 3b 0a 09 09 69 66 20 28 6d 75 74 65 78 5f 69 );...if (mutex_i
1990: 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a nit_ret != 0) {.
19a0: 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 ...return(1);...
19b0: 7d 0a 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 }....filed_filei
19c0: 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d nfo_fdcache[idx]
19d0: 2e 70 61 74 68 5b 30 5d 20 3d 20 27 5c 30 27 3b .path[0] = '\0';
19e0: 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 ...filed_fileinf
19f0: 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 66 o_fdcache[idx].f
1a00: 64 20 3d 20 2d 31 3b 0a 09 09 66 69 6c 65 64 5f d = -1;...filed_
1a10: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
1a20: 5b 69 64 78 5d 2e 6c 61 73 74 6d 6f 64 20 3d 20 [idx].lastmod =
1a30: 22 22 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 "";...filed_file
1a40: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 info_fdcache[idx
1a50: 5d 2e 74 79 70 65 20 3d 20 22 22 3b 0a 09 7d 0a ].type = "";..}.
1a60: 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a ..return(0);.}..
1a70: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 72 /* Initialize pr
1a80: 6f 63 65 73 73 20 2a 2f 0a 73 74 61 74 69 63 20 ocess */.static
1a90: 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74 28 75 int filed_init(u
1aa0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63 68 nsigned int cach
1ab0: 65 5f 73 69 7a 65 29 20 7b 0a 09 73 74 61 74 69 e_size) {..stati
1ac0: 63 20 69 6e 74 20 63 61 6c 6c 65 64 20 3d 20 30 c int called = 0
1ad0: 3b 0a 09 73 74 72 75 63 74 20 73 69 67 61 63 74 ;..struct sigact
1ae0: 69 6f 6e 20 73 69 67 6e 61 6c 5f 68 61 6e 64 6c ion signal_handl
1af0: 65 72 5f 69 6e 66 6f 3b 0a 09 73 69 67 73 65 74 er_info;..sigset
1b00: 5f 74 20 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 _t signal_handle
1b10: 72 5f 6d 61 73 6b 3b 0a 09 73 73 69 7a 65 5f 74 r_mask;..ssize_t
1b20: 20 72 65 61 64 5f 72 65 74 20 3d 20 30 3b 0a 09 read_ret = 0;..
1b30: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 61 6e unsigned int ran
1b40: 64 6f 6d 5f 76 61 6c 75 65 20 3d 20 30 3b 0a 09 dom_value = 0;..
1b50: 69 6e 74 20 63 61 63 68 65 5f 72 65 74 3b 0a 09 int cache_ret;..
1b60: 69 6e 74 20 72 61 6e 64 6f 6d 5f 66 64 3b 0a 0a int random_fd;..
1b70: 09 69 66 20 28 63 61 6c 6c 65 64 29 20 7b 0a 09 .if (called) {..
1b80: 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a .return(0);..}..
1b90: 09 63 61 6c 6c 65 64 20 3d 20 31 3b 0a 0a 09 2f .called = 1;.../
1ba0: 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 * Attempt to loc
1bb0: 6b 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 6f 20 k all memory to
1bc0: 70 68 79 73 69 63 61 6c 20 52 41 4d 20 28 62 75 physical RAM (bu
1bd0: 74 20 64 6f 6e 27 74 20 63 61 72 65 20 69 66 20 t don't care if
1be0: 77 65 20 63 61 6e 27 74 29 20 2a 2f 0a 09 6d 6c we can't) */..ml
1bf0: 6f 63 6b 61 6c 6c 28 4d 43 4c 5f 43 55 52 52 45 ockall(MCL_CURRE
1c00: 4e 54 20 7c 20 4d 43 4c 5f 46 55 54 55 52 45 29 NT | MCL_FUTURE)
1c10: 3b 0a 0a 09 2f 2a 20 45 73 74 61 62 6c 69 73 68 ;.../* Establish
1c20: 20 73 69 67 6e 61 6c 20 68 61 6e 64 6c 65 72 73 signal handlers
1c30: 20 2a 2f 0a 09 2f 2a 20 53 49 47 50 49 50 45 20 */../* SIGPIPE
1c40: 73 68 6f 75 6c 64 20 69 6e 74 65 72 72 75 70 74 should interrupt
1c50: 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 2a 2f system calls */
1c60: 0a 09 73 69 67 66 69 6c 6c 73 65 74 28 26 73 69 ..sigfillset(&si
1c70: 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 6d 61 73 gnal_handler_mas
1c80: 6b 29 3b 0a 09 73 69 67 6e 61 6c 5f 68 61 6e 64 k);..signal_hand
1c90: 6c 65 72 5f 69 6e 66 6f 2e 73 61 5f 68 61 6e 64 ler_info.sa_hand
1ca0: 6c 65 72 20 3d 20 66 69 6c 65 64 5f 73 69 67 6e ler = filed_sign
1cb0: 61 6c 5f 68 61 6e 64 6c 65 72 3b 0a 09 73 69 67 al_handler;..sig
1cc0: 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 6f nal_handler_info
1cd0: 2e 73 61 5f 6d 61 73 6b 20 3d 20 73 69 67 6e 61 .sa_mask = signa
1ce0: 6c 5f 68 61 6e 64 6c 65 72 5f 6d 61 73 6b 3b 0a l_handler_mask;.
1cf0: 09 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f .signal_handler_
1d00: 69 6e 66 6f 2e 73 61 5f 66 6c 61 67 73 20 3d 20 info.sa_flags =
1d10: 53 41 5f 52 45 53 54 41 52 54 3b 0a 09 73 69 67 SA_RESTART;..sig
1d20: 61 63 74 69 6f 6e 28 53 49 47 50 49 50 45 2c 20 action(SIGPIPE,
1d30: 26 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f &signal_handler_
1d40: 69 6e 66 6f 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 2f info, NULL);.../
1d50: 2a 20 48 61 6e 64 6c 65 20 53 49 47 48 55 50 20 * Handle SIGHUP
1d60: 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 63 to release all c
1d70: 61 63 68 65 73 20 2a 2f 0a 09 73 69 67 66 69 6c aches */..sigfil
1d80: 6c 73 65 74 28 26 73 69 67 6e 61 6c 5f 68 61 6e lset(&signal_han
1d90: 64 6c 65 72 5f 6d 61 73 6b 29 3b 0a 09 73 69 67 dler_mask);..sig
1da0: 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 6f nal_handler_info
1db0: 2e 73 61 5f 68 61 6e 64 6c 65 72 20 3d 20 66 69 .sa_handler = fi
1dc0: 6c 65 64 5f 73 69 67 6e 61 6c 5f 68 61 6e 64 6c led_signal_handl
1dd0: 65 72 3b 0a 09 73 69 67 6e 61 6c 5f 68 61 6e 64 er;..signal_hand
1de0: 6c 65 72 5f 69 6e 66 6f 2e 73 61 5f 6d 61 73 6b ler_info.sa_mask
1df0: 20 3d 20 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 = signal_handle
1e00: 72 5f 6d 61 73 6b 3b 0a 09 73 69 67 6e 61 6c 5f r_mask;..signal_
1e10: 68 61 6e 64 6c 65 72 5f 69 6e 66 6f 2e 73 61 5f handler_info.sa_
1e20: 66 6c 61 67 73 20 3d 20 30 3b 0a 09 73 69 67 61 flags = 0;..siga
1e30: 63 74 69 6f 6e 28 53 49 47 48 55 50 2c 20 26 73 ction(SIGHUP, &s
1e40: 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e ignal_handler_in
1e50: 66 6f 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 fo, NULL);.../*
1e60: 49 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 Initialize cache
1e70: 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 63 structure */..c
1e80: 61 63 68 65 5f 72 65 74 20 3d 20 66 69 6c 65 64 ache_ret = filed
1e90: 5f 69 6e 69 74 5f 63 61 63 68 65 28 63 61 63 68 _init_cache(cach
1ea0: 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 63 61 e_size);..if (ca
1eb0: 63 68 65 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a che_ret != 0) {.
1ec0: 09 09 72 65 74 75 72 6e 28 63 61 63 68 65 5f 72 ..return(cache_r
1ed0: 65 74 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 et);..}.../* Ini
1ee0: 74 69 61 6c 69 7a 65 20 72 61 6e 64 6f 6d 20 6e tialize random n
1ef0: 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
1f00: 2a 2f 0a 09 72 61 6e 64 6f 6d 5f 66 64 20 3d 20 */..random_fd =
1f10: 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 open("/dev/urand
1f20: 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a om", O_RDONLY);.
1f30: 09 69 66 20 28 72 61 6e 64 6f 6d 5f 66 64 20 3e .if (random_fd >
1f40: 3d 20 30 29 20 7b 0a 09 09 72 65 61 64 5f 72 65 = 0) {...read_re
1f50: 74 20 3d 20 72 65 61 64 28 72 61 6e 64 6f 6d 5f t = read(random_
1f60: 66 64 2c 20 26 72 61 6e 64 6f 6d 5f 76 61 6c 75 fd, &random_valu
1f70: 65 2c 20 73 69 7a 65 6f 66 28 72 61 6e 64 6f 6d e, sizeof(random
1f80: 5f 76 61 6c 75 65 29 29 3b 0a 0a 09 09 63 6c 6f _value));....clo
1f90: 73 65 28 72 61 6e 64 6f 6d 5f 66 64 29 3b 0a 09 se(random_fd);..
1fa0: 7d 0a 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 }...random_value
1fb0: 20 5e 3d 20 67 65 74 70 69 64 28 29 3b 0a 09 72 ^= getpid();..r
1fc0: 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 67 andom_value ^= g
1fd0: 65 74 75 69 64 28 29 3b 0a 09 72 61 6e 64 6f 6d etuid();..random
1fe0: 5f 76 61 6c 75 65 20 5e 3d 20 74 69 6d 65 28 4e _value ^= time(N
1ff0: 55 4c 4c 29 3b 0a 0a 09 73 72 61 6e 64 6f 6d 28 ULL);...srandom(
2000: 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 29 3b 0a 0a random_value);..
2010: 09 72 65 74 75 72 6e 28 30 29 3b 0a 0a 09 2f 2a .return(0);.../*
2020: 20 4e 4f 54 52 45 41 43 48 3a 20 52 65 61 64 20 NOTREACH: Read
2030: 6d 61 79 20 66 61 69 6c 20 6f 72 20 73 75 63 63 may fail or succ
2040: 65 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 61 63 eed, we don't ac
2050: 74 75 61 6c 6c 79 20 63 61 72 65 20 2a 2f 0a 09 tually care */..
2060: 72 65 61 64 5f 72 65 74 20 3d 20 72 65 61 64 5f read_ret = read_
2070: 72 65 74 3b 0a 7d 0a 0a 2f 2a 20 4c 69 73 74 65 ret;.}../* Liste
2080: 6e 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 n on a particula
2090: 72 20 61 64 64 72 65 73 73 2f 70 6f 72 74 20 2a r address/port *
20a0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c /.static int fil
20b0: 65 64 5f 6c 69 73 74 65 6e 28 63 6f 6e 73 74 20 ed_listen(const
20c0: 63 68 61 72 20 2a 61 64 64 72 65 73 73 2c 20 75 char *address, u
20d0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 70 6f 72 74 nsigned int port
20e0: 29 20 7b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b ) {..struct sock
20f0: 61 64 64 72 5f 69 6e 36 20 61 64 64 72 5f 76 36 addr_in6 addr_v6
2100: 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 ;..struct sockad
2110: 64 72 5f 69 6e 20 61 64 64 72 5f 76 34 3b 0a 09 dr_in addr_v4;..
2120: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 20 struct sockaddr
2130: 2a 61 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f *addr;..socklen_
2140: 74 20 61 64 64 72 5f 6c 65 6e 3b 0a 09 69 6e 74 t addr_len;..int
2150: 20 70 74 6f 6e 5f 72 65 74 2c 20 62 69 6e 64 5f pton_ret, bind_
2160: 72 65 74 2c 20 6c 69 73 74 65 6e 5f 72 65 74 3b ret, listen_ret;
2170: 0a 09 69 6e 74 20 66 61 6d 69 6c 79 3b 0a 09 69 ..int family;..i
2180: 6e 74 20 66 64 3b 0a 0a 09 66 61 6d 69 6c 79 20 nt fd;...family
2190: 3d 20 41 46 5f 49 4e 45 54 36 3b 0a 09 70 74 6f = AF_INET6;..pto
21a0: 6e 5f 72 65 74 20 3d 20 69 6e 65 74 5f 70 74 6f n_ret = inet_pto
21b0: 6e 28 66 61 6d 69 6c 79 2c 20 61 64 64 72 65 73 n(family, addres
21c0: 73 2c 20 26 61 64 64 72 5f 76 36 2e 73 69 6e 36 s, &addr_v6.sin6
21d0: 5f 61 64 64 72 2e 73 36 5f 61 64 64 72 29 3b 0a _addr.s6_addr);.
21e0: 09 69 66 20 28 70 74 6f 6e 5f 72 65 74 20 21 3d .if (pton_ret !=
21f0: 20 31 29 20 7b 0a 09 09 66 61 6d 69 6c 79 20 3d 1) {...family =
2200: 20 41 46 5f 49 4e 45 54 3b 0a 09 09 70 74 6f 6e AF_INET;...pton
2210: 5f 72 65 74 20 3d 20 69 6e 65 74 5f 70 74 6f 6e _ret = inet_pton
2220: 28 66 61 6d 69 6c 79 2c 20 61 64 64 72 65 73 73 (family, address
2230: 2c 20 26 61 64 64 72 5f 76 34 2e 73 69 6e 5f 61 , &addr_v4.sin_a
2240: 64 64 72 2e 73 5f 61 64 64 72 29 3b 0a 09 09 69 ddr.s_addr);...i
2250: 66 20 28 70 74 6f 6e 5f 72 65 74 20 21 3d 20 31 f (pton_ret != 1
2260: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 31 ) {....return(-1
2270: 29 3b 0a 09 09 7d 0a 0a 09 09 61 64 64 72 5f 76 );...}....addr_v
2280: 34 2e 73 69 6e 5f 66 61 6d 69 6c 79 20 3d 20 66 4.sin_family = f
2290: 61 6d 69 6c 79 3b 0a 09 09 61 64 64 72 5f 76 34 amily;...addr_v4
22a0: 2e 73 69 6e 5f 70 6f 72 74 20 3d 20 68 74 6f 6e .sin_port = hton
22b0: 73 28 70 6f 72 74 29 3b 0a 0a 09 09 61 64 64 72 s(port);....addr
22c0: 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 = (struct socka
22d0: 64 64 72 20 2a 29 20 26 61 64 64 72 5f 76 34 3b ddr *) &addr_v4;
22e0: 0a 09 09 61 64 64 72 5f 6c 65 6e 20 3d 20 73 69 ...addr_len = si
22f0: 7a 65 6f 66 28 61 64 64 72 5f 76 34 29 3b 0a 09 zeof(addr_v4);..
2300: 7d 20 65 6c 73 65 20 7b 0a 09 09 61 64 64 72 5f } else {...addr_
2310: 76 36 2e 73 69 6e 36 5f 66 61 6d 69 6c 79 20 3d v6.sin6_family =
2320: 20 41 46 5f 49 4e 45 54 36 3b 0a 09 09 61 64 64 AF_INET6;...add
2330: 72 5f 76 36 2e 73 69 6e 36 5f 66 6c 6f 77 69 6e r_v6.sin6_flowin
2340: 66 6f 20 3d 20 30 3b 0a 09 09 61 64 64 72 5f 76 fo = 0;...addr_v
2350: 36 2e 73 69 6e 36 5f 73 63 6f 70 65 5f 69 64 20 6.sin6_scope_id
2360: 3d 20 30 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 = 0;...addr_v6.s
2370: 69 6e 36 5f 70 6f 72 74 20 3d 20 68 74 6f 6e 73 in6_port = htons
2380: 28 70 6f 72 74 29 3b 0a 0a 09 09 61 64 64 72 20 (port);....addr
2390: 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 = (struct sockad
23a0: 64 72 20 2a 29 20 26 61 64 64 72 5f 76 36 3b 0a dr *) &addr_v6;.
23b0: 09 09 61 64 64 72 5f 6c 65 6e 20 3d 20 73 69 7a ..addr_len = siz
23c0: 65 6f 66 28 61 64 64 72 5f 76 36 29 3b 0a 09 7d eof(addr_v6);..}
23d0: 0a 0a 09 66 64 20 3d 20 73 6f 63 6b 65 74 28 66 ...fd = socket(f
23e0: 61 6d 69 6c 79 2c 20 53 4f 43 4b 5f 53 54 52 45 amily, SOCK_STRE
23f0: 41 4d 2c 20 30 29 3b 0a 09 69 66 20 28 66 64 20 AM, 0);..if (fd
2400: 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 < 0) {...return(
2410: 66 64 29 3b 0a 09 7d 0a 0a 09 62 69 6e 64 5f 72 fd);..}...bind_r
2420: 65 74 20 3d 20 62 69 6e 64 28 66 64 2c 20 61 64 et = bind(fd, ad
2430: 64 72 2c 20 61 64 64 72 5f 6c 65 6e 29 3b 0a 09 dr, addr_len);..
2440: 69 66 20 28 62 69 6e 64 5f 72 65 74 20 3c 20 30 if (bind_ret < 0
2450: 29 20 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b ) {...close(fd);
2460: 0a 0a 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a ....return(-1);.
2470: 09 7d 0a 0a 09 6c 69 73 74 65 6e 5f 72 65 74 20 .}...listen_ret
2480: 3d 20 6c 69 73 74 65 6e 28 66 64 2c 20 31 32 38 = listen(fd, 128
2490: 29 3b 0a 09 69 66 20 28 6c 69 73 74 65 6e 5f 72 );..if (listen_r
24a0: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 63 6c 6f et != 0) {...clo
24b0: 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 se(fd);....retur
24c0: 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 n(-1);..}...retu
24d0: 72 6e 28 66 64 29 3b 0a 7d 0a 0a 2f 2a 20 4c 6f rn(fd);.}../* Lo
24e0: 67 20 61 20 6d 65 73 73 61 67 65 20 2a 2f 0a 23 g a message */.#
24f0: 69 66 64 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 ifdef FILED_DONT
2500: 5f 4c 4f 47 0a 23 20 20 64 65 66 69 6e 65 20 66 _LOG.# define f
2510: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 iled_logging_thr
2520: 65 61 64 5f 69 6e 69 74 28 78 29 20 30 0a 23 20 ead_init(x) 0.#
2530: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f define filed_lo
2540: 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c 20 2e g_msg_debug(x, .
2550: 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 ..) /**/.# defi
2560: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 ne filed_log_msg
2570: 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 (x, ...) /**/.#
2580: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f define filed_lo
2590: 67 5f 65 6e 74 72 79 28 78 29 20 2f 2a 2a 2f 0a g_entry(x) /**/.
25a0: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f # define filed_
25b0: 6c 6f 67 5f 69 70 28 78 2c 20 2e 2e 2e 29 20 4e log_ip(x, ...) N
25c0: 55 4c 4c 0a 23 20 20 64 65 66 69 6e 65 20 66 69 ULL.# define fi
25d0: 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 78 29 20 26 led_log_new(x) &
25e0: 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 0a local_dummy_log.
25f0: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f # define filed_
2600: 6c 6f 67 5f 66 72 65 65 28 78 29 20 2f 2a 2a 2f log_free(x) /**/
2610: 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 6c 6f 67 67 ../* Return logg
2620: 69 6e 67 20 68 61 6e 64 6c 65 20 2a 2f 0a 73 74 ing handle */.st
2630: 61 74 69 63 20 46 49 4c 45 20 2a 66 69 6c 65 64 atic FILE *filed
2640: 5f 6c 6f 67 5f 6f 70 65 6e 28 63 6f 6e 73 74 20 _log_open(const
2650: 63 68 61 72 20 2a 66 69 6c 65 29 20 7b 0a 09 72 char *file) {..r
2660: 65 74 75 72 6e 28 73 74 64 6f 75 74 29 3b 0a 09 eturn(stdout);..
2670: 66 69 6c 65 20 3d 20 66 69 6c 65 3b 0a 7d 0a 23 file = file;.}.#
2680: 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 66 else.# define f
2690: 69 6c 65 64 5f 6c 6f 67 5f 66 72 65 65 28 78 29 iled_log_free(x)
26a0: 20 66 72 65 65 28 78 29 0a 23 20 20 69 66 64 65 free(x).# ifde
26b0: 66 20 46 49 4c 45 44 5f 44 45 42 55 47 0a 23 20 f FILED_DEBUG.#
26c0: 20 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f define filed_
26d0: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 2c log_msg_debug(x,
26e0: 20 2e 2e 2e 29 20 7b 20 66 70 72 69 6e 74 66 28 ...) { fprintf(
26f0: 73 74 64 65 72 72 2c 20 78 2c 20 5f 5f 56 41 5f stderr, x, __VA_
2700: 41 52 47 53 5f 5f 29 3b 20 66 70 72 69 6e 74 66 ARGS__); fprintf
2710: 28 73 74 64 65 72 72 2c 20 22 5c 6e 22 29 3b 20 (stderr, "\n");
2720: 66 66 6c 75 73 68 28 73 74 64 65 72 72 29 3b 20 fflush(stderr);
2730: 7d 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 }.# else.# d
2740: 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f efine filed_log_
2750: 6d 73 67 5f 64 65 62 75 67 28 78 2c 20 2e 2e 2e msg_debug(x, ...
2760: 29 20 2f 2a 2a 2f 0a 23 20 20 65 6e 64 69 66 0a ) /**/.# endif.
2770: 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6c ./* Initialize l
2780: 6f 67 67 69 6e 67 20 74 68 72 65 61 64 20 2a 2f ogging thread */
2790: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 .static void *fi
27a0: 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 led_logging_thre
27b0: 61 64 28 76 6f 69 64 20 2a 61 72 67 5f 70 29 20 ad(void *arg_p)
27c0: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f {..struct filed_
27d0: 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 61 logging_thread_a
27e0: 72 67 73 20 2a 61 72 67 3b 0a 09 73 74 72 75 63 rgs *arg;..struc
27f0: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
2800: 79 20 2a 63 75 72 72 2c 20 2a 70 72 65 76 3b 0a y *curr, *prev;.
2810: 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 6d 65 74 .const char *met
2820: 68 6f 64 3b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 hod;..time_t now
2830: 3b 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 61 ;..FILE *fp;...a
2840: 72 67 20 3d 20 61 72 67 5f 70 3b 0a 0a 09 66 70 rg = arg_p;...fp
2850: 20 3d 20 61 72 67 2d 3e 66 70 3b 0a 0a 09 77 68 = arg->fp;...wh
2860: 69 6c 65 20 28 31 29 20 7b 0a 09 09 70 74 68 72 ile (1) {...pthr
2870: 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 ead_mutex_lock(&
2880: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
2890: 73 74 5f 6d 75 74 65 78 29 3b 0a 09 09 70 74 68 st_mutex);...pth
28a0: 72 65 61 64 5f 63 6f 6e 64 5f 77 61 69 74 28 26 read_cond_wait(&
28b0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
28c0: 73 74 5f 72 65 61 64 79 2c 20 26 66 69 6c 65 64 st_ready, &filed
28d0: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 _log_msg_list_mu
28e0: 74 65 78 29 3b 0a 0a 09 09 63 75 72 72 20 3d 20 tex);....curr =
28f0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
2900: 73 74 3b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f st;...filed_log_
2910: 6d 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c 4c 3b msg_list = NULL;
2920: 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 ....pthread_mute
2930: 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f x_unlock(&filed_
2940: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 log_msg_list_mut
2950: 65 78 29 3b 0a 0a 09 09 6e 6f 77 20 3d 20 74 69 ex);....now = ti
2960: 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 09 70 72 65 me(NULL);....pre
2970: 76 20 3d 20 4e 55 4c 4c 3b 0a 09 09 66 6f 72 20 v = NULL;...for
2980: 28 3b 20 63 75 72 72 3b 20 63 75 72 72 20 3d 20 (; curr; curr =
2990: 63 75 72 72 2d 3e 5f 6e 65 78 74 29 20 7b 0a 09 curr->_next) {..
29a0: 09 09 63 75 72 72 2d 3e 5f 70 72 65 76 20 3d 20 ..curr->_prev =
29b0: 70 72 65 76 3b 0a 0a 09 09 09 70 72 65 76 20 3d prev;.....prev =
29c0: 20 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 63 75 curr;...}....cu
29d0: 72 72 20 3d 20 70 72 65 76 3b 0a 09 09 77 68 69 rr = prev;...whi
29e0: 6c 65 20 28 63 75 72 72 29 20 7b 0a 09 09 09 73 le (curr) {....s
29f0: 77 69 74 63 68 20 28 63 75 72 72 2d 3e 74 79 70 witch (curr->typ
2a00: 65 29 20 7b 0a 09 09 09 09 63 61 73 65 20 46 49 e) {.....case FI
2a10: 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 LED_LOG_TYPE_MES
2a20: 53 41 47 45 3a 0a 09 09 09 09 09 66 70 72 69 6e SAGE:......fprin
2a30: 74 66 28 66 70 2c 20 22 25 73 22 2c 20 63 75 72 tf(fp, "%s", cur
2a40: 72 2d 3e 62 75 66 66 65 72 29 3b 0a 0a 09 09 09 r->buffer);.....
2a50: 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 63 61 73 ..break;.....cas
2a60: 65 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 e FILED_LOG_TYPE
2a70: 5f 54 52 41 4e 53 46 45 52 3a 0a 09 09 09 09 09 _TRANSFER:......
2a80: 73 77 69 74 63 68 20 28 63 75 72 72 2d 3e 6d 65 switch (curr->me
2a90: 74 68 6f 64 29 20 7b 0a 09 09 09 09 09 09 63 61 thod) {.......ca
2aa0: 73 65 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 se FILED_REQUEST
2ab0: 5f 4d 45 54 48 4f 44 5f 47 45 54 3a 0a 09 09 09 _METHOD_GET:....
2ac0: 09 09 09 09 6d 65 74 68 6f 64 3d 22 47 45 54 22 ....method="GET"
2ad0: 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a ;........break;.
2ae0: 09 09 09 09 09 09 63 61 73 65 20 46 49 4c 45 44 ......case FILED
2af0: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f _REQUEST_METHOD_
2b00: 48 45 41 44 3a 0a 09 09 09 09 09 09 09 6d 65 74 HEAD:........met
2b10: 68 6f 64 3d 22 48 45 41 44 22 3b 0a 09 09 09 09 hod="HEAD";.....
2b20: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 ...break;.......
2b30: 64 65 66 61 75 6c 74 3a 0a 09 09 09 09 09 09 09 default:........
2b40: 6d 65 74 68 6f 64 3d 22 3c 75 6e 6b 6e 6f 77 6e method="<unknown
2b50: 3e 22 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b >";........break
2b60: 3b 0a 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 69 ;......}.......i
2b70: 66 20 28 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 f (curr->endtime
2b80: 20 3d 3d 20 28 28 74 69 6d 65 5f 74 29 20 2d 31 == ((time_t) -1
2b90: 29 29 20 7b 0a 09 09 09 09 09 09 63 75 72 72 2d )) {.......curr-
2ba0: 3e 65 6e 64 74 69 6d 65 20 3d 20 6e 6f 77 3b 0a >endtime = now;.
2bb0: 09 09 09 09 09 7d 0a 0a 09 09 09 09 09 66 70 72 .....}.......fpr
2bc0: 69 6e 74 66 28 66 70 2c 20 22 54 52 41 4e 53 46 intf(fp, "TRANSF
2bd0: 45 52 20 4d 45 54 48 4f 44 3d 25 73 20 50 41 54 ER METHOD=%s PAT
2be0: 48 3d 25 73 20 53 52 43 3d 25 73 3a 25 69 20 54 H=%s SRC=%s:%i T
2bf0: 49 4d 45 2e 53 54 41 52 54 3d 25 6c 6c 75 20 54 IME.START=%llu T
2c00: 49 4d 45 2e 45 4e 44 3d 25 6c 6c 75 20 43 4f 44 IME.END=%llu COD
2c10: 45 2e 56 41 4c 55 45 3d 25 75 20 43 4f 44 45 2e E.VALUE=%u CODE.
2c20: 52 45 41 53 4f 4e 3d 25 73 20 52 45 51 55 45 53 REASON=%s REQUES
2c30: 54 2e 4f 46 46 53 45 54 3d 25 6c 6c 75 20 52 45 T.OFFSET=%llu RE
2c40: 51 55 45 53 54 2e 4c 45 4e 47 54 48 3d 25 6c 6c QUEST.LENGTH=%ll
2c50: 75 20 46 49 4c 45 2e 4c 45 4e 47 54 48 3d 25 6c u FILE.LENGTH=%l
2c60: 6c 75 20 54 52 41 4e 53 46 45 52 2e 4c 45 4e 47 lu TRANSFER.LENG
2c70: 54 48 3d 25 6c 6c 75 22 2c 0a 09 09 09 09 09 09 TH=%llu",.......
2c80: 6d 65 74 68 6f 64 2c 0a 09 09 09 09 09 09 63 75 method,.......cu
2c90: 72 72 2d 3e 62 75 66 66 65 72 2c 0a 09 09 09 09 rr->buffer,.....
2ca0: 09 09 63 75 72 72 2d 3e 69 70 2c 20 63 75 72 72 ..curr->ip, curr
2cb0: 2d 3e 70 6f 72 74 2c 0a 09 09 09 09 09 09 28 75 ->port,.......(u
2cc0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
2cd0: 67 29 20 63 75 72 72 2d 3e 73 74 61 72 74 74 69 g) curr->startti
2ce0: 6d 65 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 me,.......(unsig
2cf0: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 ned long long) c
2d00: 75 72 72 2d 3e 65 6e 64 74 69 6d 65 2c 0a 09 09 urr->endtime,...
2d10: 09 09 09 09 63 75 72 72 2d 3e 68 74 74 70 5f 63 ....curr->http_c
2d20: 6f 64 65 2c 20 63 75 72 72 2d 3e 72 65 61 73 6f ode, curr->reaso
2d30: 6e 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e n,.......(unsign
2d40: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 ed long long) cu
2d50: 72 72 2d 3e 72 65 71 5f 6f 66 66 73 65 74 2c 0a rr->req_offset,.
2d60: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ......(unsigned
2d70: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d long long) curr-
2d80: 3e 72 65 71 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 >req_length,....
2d90: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
2da0: 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 66 69 g long) curr->fi
2db0: 6c 65 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09 le_length,......
2dc0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
2dd0: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 73 65 6e 74 long) curr->sent
2de0: 5f 6c 65 6e 67 74 68 0a 09 09 09 09 09 29 3b 0a _length......);.
2df0: 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 ......break;....
2e00: 7d 0a 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c }....fprintf(fp,
2e10: 20 22 20 54 48 52 45 41 44 3d 25 6c 6c 75 20 54 " THREAD=%llu T
2e20: 49 4d 45 3d 25 6c 6c 75 5c 6e 22 2c 0a 09 09 09 IME=%llu\n",....
2e30: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
2e40: 6c 6f 6e 67 29 20 28 28 69 6e 74 70 74 72 5f 74 long) ((intptr_t
2e50: 29 20 63 75 72 72 2d 3e 74 68 72 65 61 64 29 2c ) curr->thread),
2e60: 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
2e70: 6f 6e 67 20 6c 6f 6e 67 29 20 6e 6f 77 0a 09 09 ong long) now...
2e80: 09 29 3b 0a 09 09 09 66 66 6c 75 73 68 28 66 70 .);....fflush(fp
2e90: 29 3b 0a 0a 09 09 09 70 72 65 76 20 3d 20 63 75 );.....prev = cu
2ea0: 72 72 3b 0a 09 09 09 63 75 72 72 20 3d 20 63 75 rr;....curr = cu
2eb0: 72 72 2d 3e 5f 70 72 65 76 3b 0a 0a 09 09 09 66 rr->_prev;.....f
2ec0: 72 65 65 28 70 72 65 76 29 3b 0a 09 09 7d 0a 09 ree(prev);...}..
2ed0: 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 }...return(NULL)
2ee0: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 ;.}..static void
2ef0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 filed_log_entry
2f00: 28 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f (struct filed_lo
2f10: 67 5f 65 6e 74 72 79 20 2a 65 6e 74 72 79 29 20 g_entry *entry)
2f20: 7b 0a 09 65 6e 74 72 79 2d 3e 74 68 72 65 61 64 {..entry->thread
2f30: 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
2f40: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 );...pthread_mut
2f50: 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c ex_lock(&filed_l
2f60: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 og_msg_list_mute
2f70: 78 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 5f 6e 65 x);...entry->_ne
2f80: 78 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d xt = filed_log_m
2f90: 73 67 5f 6c 69 73 74 3b 0a 09 66 69 6c 65 64 5f sg_list;..filed_
2fa0: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 65 log_msg_list = e
2fb0: 6e 74 72 79 3b 0a 0a 09 70 74 68 72 65 61 64 5f ntry;...pthread_
2fc0: 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 mutex_unlock(&fi
2fd0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 led_log_msg_list
2fe0: 5f 6d 75 74 65 78 29 3b 0a 0a 09 70 74 68 72 65 _mutex);...pthre
2ff0: 61 64 5f 63 6f 6e 64 5f 73 69 67 6e 61 6c 28 26 ad_cond_signal(&
3000: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
3010: 73 74 5f 72 65 61 64 79 29 3b 0a 0a 09 72 65 74 st_ready);...ret
3020: 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73 urn;.}..static s
3030: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
3040: 65 6e 74 72 79 20 2a 66 69 6c 65 64 5f 6c 6f 67 entry *filed_log
3050: 5f 6e 65 77 28 69 6e 74 20 69 6e 69 74 69 61 6c _new(int initial
3060: 69 7a 65 29 20 7b 0a 09 73 74 72 75 63 74 20 66 ize) {..struct f
3070: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
3080: 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c retval;...retval
3090: 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 = malloc(sizeof
30a0: 28 2a 72 65 74 76 61 6c 29 29 3b 0a 0a 09 69 66 (*retval));...if
30b0: 20 28 69 6e 69 74 69 61 6c 69 7a 65 29 20 7b 0a (initialize) {.
30c0: 09 09 72 65 74 76 61 6c 2d 3e 62 75 66 66 65 72 ..retval->buffer
30d0: 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 72 65 [0] = '\0';...re
30e0: 74 76 61 6c 2d 3e 68 74 74 70 5f 63 6f 64 65 20 tval->http_code
30f0: 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c 2d 3e = -1;...retval->
3100: 73 74 61 72 74 74 69 6d 65 20 3d 20 30 3b 0a 09 starttime = 0;..
3110: 09 72 65 74 76 61 6c 2d 3e 65 6e 64 74 69 6d 65 .retval->endtime
3120: 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e = 0;...retval->
3130: 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 30 3b 0a req_offset = 0;.
3140: 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f 6c 65 ..retval->req_le
3150: 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 ngth = 0;...retv
3160: 61 6c 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 20 al->sent_length
3170: 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 66 = 0;...retval->f
3180: 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 30 3b 0a ile_length = 0;.
3190: 09 09 72 65 74 76 61 6c 2d 3e 69 70 5b 30 5d 20 ..retval->ip[0]
31a0: 3d 20 27 5c 30 27 3b 0a 09 09 72 65 74 76 61 6c = '\0';...retval
31b0: 2d 3e 70 6f 72 74 20 3d 20 2d 31 3b 0a 09 09 72 ->port = -1;...r
31c0: 65 74 76 61 6c 2d 3e 6d 65 74 68 6f 64 20 3d 20 etval->method =
31d0: 2d 31 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 -1;..}...return(
31e0: 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 retval);.}..stat
31f0: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 6c 6f ic void filed_lo
3200: 67 5f 6d 73 67 28 63 6f 6e 73 74 20 63 68 61 72 g_msg(const char
3210: 20 2a 66 6d 74 2c 20 2e 2e 2e 29 20 7b 0a 09 73 *fmt, ...) {..s
3220: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
3230: 65 6e 74 72 79 20 2a 65 6e 74 72 79 3b 0a 09 76 entry *entry;..v
3240: 61 5f 6c 69 73 74 20 61 72 67 73 3b 0a 0a 09 65 a_list args;...e
3250: 6e 74 72 79 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 ntry = filed_log
3260: 5f 6e 65 77 28 30 29 3b 0a 0a 09 76 61 5f 73 74 _new(0);...va_st
3270: 61 72 74 28 61 72 67 73 2c 20 66 6d 74 29 3b 0a art(args, fmt);.
3280: 0a 09 76 73 6e 70 72 69 6e 74 66 28 65 6e 74 72 ..vsnprintf(entr
3290: 79 2d 3e 62 75 66 66 65 72 2c 20 73 69 7a 65 6f y->buffer, sizeo
32a0: 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 65 72 29 f(entry->buffer)
32b0: 2c 20 66 6d 74 2c 20 61 72 67 73 29 3b 0a 0a 09 , fmt, args);...
32c0: 76 61 5f 65 6e 64 28 61 72 67 73 29 3b 0a 0a 09 va_end(args);...
32d0: 65 6e 74 72 79 2d 3e 74 79 70 65 20 3d 20 46 49 entry->type = FI
32e0: 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 LED_LOG_TYPE_MES
32f0: 53 41 47 45 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f SAGE;...filed_lo
3300: 67 5f 65 6e 74 72 79 28 65 6e 74 72 79 29 3b 0a g_entry(entry);.
3310: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 ..return;.}..sta
3320: 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
3330: 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 73 74 72 filed_log_ip(str
3340: 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 61 64 uct sockaddr *ad
3350: 64 72 2c 20 63 68 61 72 20 2a 62 75 66 66 65 72 dr, char *buffer
3360: 2c 20 73 69 7a 65 5f 74 20 62 75 66 66 65 72 6c , size_t bufferl
3370: 65 6e 29 20 7b 0a 09 73 74 72 75 63 74 20 73 6f en) {..struct so
3380: 63 6b 61 64 64 72 5f 69 6e 20 2a 61 64 64 72 5f ckaddr_in *addr_
3390: 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b v4;..struct sock
33a0: 61 64 64 72 5f 69 6e 36 20 2a 61 64 64 72 5f 76 addr_in6 *addr_v
33b0: 36 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 6;..const char *
33c0: 72 65 74 76 61 6c 20 3d 20 4e 55 4c 4c 3b 0a 0a retval = NULL;..
33d0: 09 61 64 64 72 5f 76 36 20 3d 20 28 73 74 72 75 .addr_v6 = (stru
33e0: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 ct sockaddr_in6
33f0: 2a 29 20 61 64 64 72 3b 0a 0a 09 73 77 69 74 63 *) addr;...switc
3400: 68 20 28 61 64 64 72 5f 76 36 2d 3e 73 69 6e 36 h (addr_v6->sin6
3410: 5f 66 61 6d 69 6c 79 29 20 7b 0a 09 09 63 61 73 _family) {...cas
3420: 65 20 41 46 5f 49 4e 45 54 3a 0a 09 09 09 61 64 e AF_INET:....ad
3430: 64 72 5f 76 34 20 3d 20 28 73 74 72 75 63 74 20 dr_v4 = (struct
3440: 73 6f 63 6b 61 64 64 72 5f 69 6e 20 2a 29 20 61 sockaddr_in *) a
3450: 64 64 72 3b 0a 09 09 09 72 65 74 76 61 6c 20 3d ddr;....retval =
3460: 20 69 6e 65 74 5f 6e 74 6f 70 28 41 46 5f 49 4e inet_ntop(AF_IN
3470: 45 54 2c 20 26 61 64 64 72 5f 76 34 2d 3e 73 69 ET, &addr_v4->si
3480: 6e 5f 61 64 64 72 2c 20 62 75 66 66 65 72 2c 20 n_addr, buffer,
3490: 62 75 66 66 65 72 6c 65 6e 29 3b 0a 09 09 09 62 bufferlen);....b
34a0: 72 65 61 6b 3b 0a 09 09 63 61 73 65 20 41 46 5f reak;...case AF_
34b0: 49 4e 45 54 36 3a 0a 09 09 09 72 65 74 76 61 6c INET6:....retval
34c0: 20 3d 20 69 6e 65 74 5f 6e 74 6f 70 28 41 46 5f = inet_ntop(AF_
34d0: 49 4e 45 54 36 2c 20 26 61 64 64 72 5f 76 36 2d INET6, &addr_v6-
34e0: 3e 73 69 6e 36 5f 61 64 64 72 2c 20 62 75 66 66 >sin6_addr, buff
34f0: 65 72 2c 20 62 75 66 66 65 72 6c 65 6e 29 3b 0a er, bufferlen);.
3500: 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a 0a 09 72 ...break;..}...r
3510: 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d eturn(retval);.}
3520: 0a 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a 66 ..static FILE *f
3530: 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 63 6f iled_log_open(co
3540: 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 29 20 nst char *file)
3550: 7b 0a 09 46 49 4c 45 20 2a 72 65 74 76 61 6c 3b {..FILE *retval;
3560: 0a 0a 09 69 66 20 28 73 74 72 63 6d 70 28 66 69 ...if (strcmp(fi
3570: 6c 65 2c 20 22 2d 22 29 20 3d 3d 20 30 29 20 7b le, "-") == 0) {
3580: 0a 09 09 72 65 74 76 61 6c 20 3d 20 73 74 64 6f ...retval = stdo
3590: 75 74 3b 0a 09 7d 20 65 6c 73 65 20 69 66 20 28 ut;..} else if (
35a0: 66 69 6c 65 5b 30 5d 20 3d 3d 20 27 7c 27 29 20 file[0] == '|')
35b0: 7b 0a 09 09 66 69 6c 65 2b 2b 3b 0a 09 09 72 65 {...file++;...re
35c0: 74 76 61 6c 20 3d 20 70 6f 70 65 6e 28 66 69 6c tval = popen(fil
35d0: 65 2c 20 22 77 22 29 3b 0a 09 7d 20 65 6c 73 65 e, "w");..} else
35e0: 20 7b 0a 09 09 72 65 74 76 61 6c 20 3d 20 66 6f {...retval = fo
35f0: 70 65 6e 28 66 69 6c 65 2c 20 22 61 2b 22 29 3b pen(file, "a+");
3600: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 ..}...return(ret
3610: 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 val);.}..static
3620: 69 6e 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e int filed_loggin
3630: 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 46 49 g_thread_init(FI
3640: 4c 45 20 2a 6c 6f 67 66 70 29 20 7b 0a 09 73 74 LE *logfp) {..st
3650: 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 ruct filed_loggi
3660: 6e 67 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a ng_thread_args *
3670: 61 72 67 73 3b 0a 09 70 74 68 72 65 61 64 5f 74 args;..pthread_t
3680: 20 74 68 72 65 61 64 5f 69 64 3b 0a 0a 09 61 72 thread_id;...ar
3690: 67 73 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 gs = malloc(size
36a0: 6f 66 28 2a 61 72 67 73 29 29 3b 0a 09 61 72 67 of(*args));..arg
36b0: 73 2d 3e 66 70 20 3d 20 6c 6f 67 66 70 3b 0a 0a s->fp = logfp;..
36c0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c .filed_log_msg_l
36d0: 69 73 74 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 70 74 ist = NULL;...pt
36e0: 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 hread_mutex_init
36f0: 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f (&filed_log_msg_
3700: 6c 69 73 74 5f 6d 75 74 65 78 2c 20 4e 55 4c 4c list_mutex, NULL
3710: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 72 65 );...pthread_cre
3720: 61 74 65 28 26 74 68 72 65 61 64 5f 69 64 2c 20 ate(&thread_id,
3730: 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 6c 6f 67 67 NULL, filed_logg
3740: 69 6e 67 5f 74 68 72 65 61 64 2c 20 61 72 67 73 ing_thread, args
3750: 29 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d );...filed_log_m
3760: 73 67 28 22 53 54 41 52 54 22 29 3b 0a 0a 09 72 sg("START");...r
3770: 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 23 65 6e 64 eturn(0);.}.#end
3780: 69 66 0a 0a 23 69 66 64 65 66 20 46 49 4c 45 44 if..#ifdef FILED
3790: 5f 44 4f 4e 54 5f 54 49 4d 45 4f 55 54 0a 23 64 _DONT_TIMEOUT.#d
37a0: 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b efine filed_sock
37b0: 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 ettimeout_thread
37c0: 5f 69 6e 69 74 28 29 20 30 0a 23 64 65 66 69 6e _init() 0.#defin
37d0: 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 e filed_socketti
37e0: 6d 65 6f 75 74 5f 69 6e 69 74 28 29 20 30 0a 23 meout_init() 0.#
37f0: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 define filed_soc
3800: 6b 65 74 74 69 6d 65 6f 75 74 5f 61 63 63 65 70 kettimeout_accep
3810: 74 28 78 29 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e t(x) /**/.#defin
3820: 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 e filed_socketti
3830: 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e 67 meout_processing
3840: 5f 73 74 61 72 74 28 78 29 20 2f 2a 2a 2f 0a 23 _start(x) /**/.#
3850: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 define filed_soc
3860: 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65 kettimeout_proce
3870: 73 73 69 6e 67 5f 65 6e 64 28 78 29 20 2f 2a 2a ssing_end(x) /**
3880: 2f 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f /.#define filed_
3890: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c sockettimeout_cl
38a0: 6f 73 65 28 78 29 20 2f 2a 2a 2f 0a 23 65 6c 73 ose(x) /**/.#els
38b0: 65 0a 74 69 6d 65 5f 74 20 66 69 6c 65 64 5f 73 e.time_t filed_s
38c0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 69 6d ockettimeout_tim
38d0: 65 3b 0a 73 74 72 75 63 74 20 7b 0a 09 74 69 6d e;.struct {..tim
38e0: 65 5f 74 20 65 78 70 69 72 61 74 69 6f 6e 5f 74 e_t expiration_t
38f0: 69 6d 65 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 ime;..pthread_t
3900: 74 68 72 65 61 64 5f 69 64 3b 0a 09 69 6e 74 20 thread_id;..int
3910: 76 61 6c 69 64 3b 0a 7d 20 2a 66 69 6c 65 64 5f valid;.} *filed_
3920: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f sockettimeout_so
3930: 63 6b 73 74 61 74 75 73 3b 0a 6c 6f 6e 67 20 66 ckstatus;.long f
3940: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
3950: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c 65 ut_sockstatus_le
3960: 6e 67 74 68 3b 0a 69 6e 74 20 66 69 6c 65 64 5f ngth;.int filed_
3970: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 64 65 sockettimeout_de
3980: 76 6e 75 6c 6c 5f 66 64 3b 0a 0a 73 74 61 74 69 vnull_fd;..stati
3990: 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b c int filed_sock
39a0: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 ettimeout_sockfd
39b0: 5f 69 6e 5f 72 61 6e 67 65 28 69 6e 74 20 73 6f _in_range(int so
39c0: 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 73 6f 63 ckfd) {..if (soc
39d0: 6b 66 64 20 3c 20 33 29 20 7b 0a 09 09 72 65 74 kfd < 3) {...ret
39e0: 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 69 66 20 urn(0);..}...if
39f0: 28 73 6f 63 6b 66 64 20 3e 20 66 69 6c 65 64 5f (sockfd > filed_
3a00: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f sockettimeout_so
3a10: 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 29 ckstatus_length)
3a20: 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a {...return(0);.
3a30: 09 7d 0a 0a 09 72 65 74 75 72 6e 28 31 29 3b 0a .}...return(1);.
3a40: 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 }..static void f
3a50: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
3a60: 75 74 5f 65 78 70 69 72 65 28 69 6e 74 20 73 6f ut_expire(int so
3a70: 63 6b 66 64 2c 20 69 6e 74 20 6c 65 6e 67 74 68 ckfd, int length
3a80: 29 20 7b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 2c ) {..time_t now,
3a90: 20 65 78 70 69 72 65 3b 0a 0a 09 6e 6f 77 20 3d expire;...now =
3aa0: 20 61 74 6f 6d 69 63 5f 6c 6f 61 64 28 26 66 69 atomic_load(&fi
3ab0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
3ac0: 74 5f 74 69 6d 65 29 3b 0a 0a 09 65 78 70 69 72 t_time);...expir
3ad0: 65 20 3d 20 6e 6f 77 20 2b 20 6c 65 6e 67 74 68 e = now + length
3ae0: 3b 0a 0a 09 61 74 6f 6d 69 63 5f 73 74 6f 72 65 ;...atomic_store
3af0: 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 (&filed_socketti
3b00: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 meout_sockstatus
3b10: 5b 73 6f 63 6b 66 64 5d 2e 65 78 70 69 72 61 74 [sockfd].expirat
3b20: 69 6f 6e 5f 74 69 6d 65 2c 20 65 78 70 69 72 65 ion_time, expire
3b30: 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a );...return;.}..
3b40: 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 static void file
3b50: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
3b60: 61 63 63 65 70 74 28 69 6e 74 20 73 6f 63 6b 66 accept(int sockf
3b70: 64 29 20 7b 0a 09 69 66 20 28 21 66 69 6c 65 64 d) {..if (!filed
3b80: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
3b90: 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28 73 ockfd_in_range(s
3ba0: 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 65 74 75 ockfd)) {...retu
3bb0: 72 6e 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 rn;..}...filed_s
3bc0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78 70 ockettimeout_exp
3bd0: 69 72 65 28 73 6f 63 6b 66 64 2c 20 36 30 29 3b ire(sockfd, 60);
3be0: 0a 0a 09 61 74 6f 6d 69 63 5f 73 74 6f 72 65 28 ...atomic_store(
3bf0: 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d &filed_sockettim
3c00: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b eout_sockstatus[
3c10: 73 6f 63 6b 66 64 5d 2e 74 68 72 65 61 64 5f 69 sockfd].thread_i
3c20: 64 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 d, pthread_self(
3c30: 29 29 3b 0a 0a 09 61 74 6f 6d 69 63 5f 73 74 6f ));...atomic_sto
3c40: 72 65 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 re(&filed_socket
3c50: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
3c60: 75 73 5b 73 6f 63 6b 66 64 5d 2e 76 61 6c 69 64 us[sockfd].valid
3c70: 2c 20 74 72 75 65 29 3b 0a 0a 09 72 65 74 75 72 , true);...retur
3c80: 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 n;.}..static voi
3c90: 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 d filed_socketti
3ca0: 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e 67 meout_processing
3cb0: 5f 73 74 61 72 74 28 69 6e 74 20 73 6f 63 6b 66 _start(int sockf
3cc0: 64 29 20 7b 0a 09 69 66 20 28 21 66 69 6c 65 64 d) {..if (!filed
3cd0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
3ce0: 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28 73 ockfd_in_range(s
3cf0: 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 65 74 75 ockfd)) {...retu
3d00: 72 6e 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 rn;..}...filed_s
3d10: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78 70 ockettimeout_exp
3d20: 69 72 65 28 73 6f 63 6b 66 64 2c 20 38 36 34 30 ire(sockfd, 8640
3d30: 30 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0);...return;.}.
3d40: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c .static void fil
3d50: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
3d60: 5f 70 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64 28 _processing_end(
3d70: 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 int sockfd) {..i
3d80: 66 20 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 f (!filed_socket
3d90: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 timeout_sockfd_i
3da0: 6e 5f 72 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 n_range(sockfd))
3db0: 20 7b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a {...return;..}.
3dc0: 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
3dd0: 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 73 6f 63 meout_expire(soc
3de0: 6b 66 64 2c 20 36 30 29 3b 0a 0a 09 72 65 74 75 kfd, 60);...retu
3df0: 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f rn;.}..static vo
3e00: 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 id filed_sockett
3e10: 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 69 6e 74 imeout_close(int
3e20: 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 sockfd) {..if (
3e30: 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d !filed_sockettim
3e40: 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 eout_sockfd_in_r
3e50: 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b 0a ange(sockfd)) {.
3e60: 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 61 ..return;..}...a
3e70: 74 6f 6d 69 63 5f 73 74 6f 72 65 28 26 66 69 6c tomic_store(&fil
3e80: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
3e90: 5f 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f 63 6b _sockstatus[sock
3ea0: 66 64 5d 2e 76 61 6c 69 64 2c 20 66 61 6c 73 65 fd].valid, false
3eb0: 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a );...return;.}..
3ec0: 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 6c static void *fil
3ed0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
3ee0: 5f 74 68 72 65 61 64 28 76 6f 69 64 20 2a 61 72 _thread(void *ar
3ef0: 67 29 20 7b 0a 09 73 74 72 75 63 74 20 74 69 6d g) {..struct tim
3f00: 65 73 70 65 63 20 73 6c 65 65 70 5f 74 69 6d 65 espec sleep_time
3f10: 3b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 2c 20 65 ;..time_t now, e
3f20: 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 3b 0a xpiration_time;.
3f30: 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 .pthread_t threa
3f40: 64 5f 69 64 3b 0a 09 6c 6f 6e 67 20 69 64 78 3b d_id;..long idx;
3f50: 0a 09 69 6e 74 20 63 6f 75 6e 74 3b 0a 09 69 6e ..int count;..in
3f60: 74 20 76 61 6c 69 64 3b 0a 0a 09 77 68 69 6c 65 t valid;...while
3f70: 20 28 31 29 20 7b 0a 09 09 66 6f 72 20 28 63 6f (1) {...for (co
3f80: 75 6e 74 20 3d 20 30 3b 20 63 6f 75 6e 74 20 3c unt = 0; count <
3f90: 20 31 30 3b 20 63 6f 75 6e 74 2b 2b 29 20 7b 0a 10; count++) {.
3fa0: 09 09 09 73 6c 65 65 70 5f 74 69 6d 65 2e 74 76 ...sleep_time.tv
3fb0: 5f 73 65 63 20 3d 20 33 30 3b 0a 09 09 09 73 6c _sec = 30;....sl
3fc0: 65 65 70 5f 74 69 6d 65 2e 74 76 5f 6e 73 65 63 eep_time.tv_nsec
3fd0: 20 3d 20 30 3b 0a 09 09 09 6e 61 6e 6f 73 6c 65 = 0;....nanosle
3fe0: 65 70 28 26 73 6c 65 65 70 5f 74 69 6d 65 2c 20 ep(&sleep_time,
3ff0: 4e 55 4c 4c 29 3b 0a 0a 09 09 09 6e 6f 77 20 3d NULL);.....now =
4000: 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 09 time(NULL);....
4010: 09 61 74 6f 6d 69 63 5f 73 74 6f 72 65 28 26 66 .atomic_store(&f
4020: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
4030: 75 74 5f 74 69 6d 65 2c 20 6e 6f 77 29 3b 0a 09 ut_time, now);..
4040: 09 7d 0a 0a 09 09 66 6f 72 20 28 69 64 78 20 3d .}....for (idx =
4050: 20 30 3b 20 69 64 78 20 3c 20 66 69 6c 65 64 5f 0; idx < filed_
4060: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f sockettimeout_so
4070: 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 3b ckstatus_length;
4080: 20 69 64 78 2b 2b 29 20 7b 0a 09 09 09 76 61 6c idx++) {....val
4090: 69 64 20 3d 20 61 74 6f 6d 69 63 5f 6c 6f 61 64 id = atomic_load
40a0: 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 (&filed_socketti
40b0: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 meout_sockstatus
40c0: 5b 69 64 78 5d 2e 76 61 6c 69 64 29 3b 0a 0a 09 [idx].valid);...
40d0: 09 09 69 66 20 28 21 76 61 6c 69 64 29 20 7b 0a ..if (!valid) {.
40e0: 09 09 09 09 63 6f 6e 74 69 6e 75 65 3b 0a 09 09 ....continue;...
40f0: 09 7d 0a 0a 09 09 09 65 78 70 69 72 61 74 69 6f .}.....expiratio
4100: 6e 5f 74 69 6d 65 20 3d 20 61 74 6f 6d 69 63 5f n_time = atomic_
4110: 6c 6f 61 64 28 26 66 69 6c 65 64 5f 73 6f 63 6b load(&filed_sock
4120: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 ettimeout_sockst
4130: 61 74 75 73 5b 69 64 78 5d 2e 65 78 70 69 72 61 atus[idx].expira
4140: 74 69 6f 6e 5f 74 69 6d 65 29 3b 0a 0a 09 09 09 tion_time);.....
4150: 74 68 72 65 61 64 5f 69 64 20 3d 20 61 74 6f 6d thread_id = atom
4160: 69 63 5f 6c 6f 61 64 28 26 66 69 6c 65 64 5f 73 ic_load(&filed_s
4170: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
4180: 6b 73 74 61 74 75 73 5b 69 64 78 5d 2e 74 68 72 kstatus[idx].thr
4190: 65 61 64 5f 69 64 29 3b 0a 0a 09 09 09 69 66 20 ead_id);.....if
41a0: 28 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 (expiration_time
41b0: 20 3e 20 6e 6f 77 29 20 7b 0a 09 09 09 09 63 6f > now) {.....co
41c0: 6e 74 69 6e 75 65 3b 0a 09 09 09 7d 0a 0a 09 09 ntinue;....}....
41d0: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d .filed_sockettim
41e0: 65 6f 75 74 5f 63 6c 6f 73 65 28 69 64 78 29 3b eout_close(idx);
41f0: 0a 0a 09 09 09 64 75 70 32 28 66 69 6c 65 64 5f .....dup2(filed_
4200: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 64 65 sockettimeout_de
4210: 76 6e 75 6c 6c 5f 66 64 2c 20 69 64 78 29 3b 0a vnull_fd, idx);.
4220: 0a 09 09 09 70 74 68 72 65 61 64 5f 6b 69 6c 6c ....pthread_kill
4230: 28 74 68 72 65 61 64 5f 69 64 2c 20 53 49 47 50 (thread_id, SIGP
4240: 49 50 45 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 IPE);...}..}...r
4250: 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 0a 09 2f eturn(NULL);.../
4260: 2a 20 4e 4f 54 52 45 41 43 48 3a 20 57 65 20 64 * NOTREACH: We d
4270: 6f 6e 27 74 20 61 63 74 75 61 6c 6c 79 20 74 61 on't actually ta
4280: 6b 65 20 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 ke any arguments
4290: 20 2a 2f 0a 09 61 72 67 20 3d 20 61 72 67 3b 0a */..arg = arg;.
42a0: 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 }..static int fi
42b0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
42c0: 74 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 76 6f t_thread_init(vo
42d0: 69 64 29 20 7b 0a 09 70 74 68 72 65 61 64 5f 74 id) {..pthread_t
42e0: 20 74 68 72 65 61 64 5f 69 64 3b 0a 0a 09 70 74 thread_id;...pt
42f0: 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 68 hread_create(&th
4300: 72 65 61 64 5f 69 64 2c 20 4e 55 4c 4c 2c 20 66 read_id, NULL, f
4310: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
4320: 75 74 5f 74 68 72 65 61 64 2c 20 4e 55 4c 4c 29 ut_thread, NULL)
4330: 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d ;...return(0);.}
4340: 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c ..static int fil
4350: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
4360: 5f 69 6e 69 74 28 76 6f 69 64 29 20 7b 0a 09 6c _init(void) {..l
4370: 6f 6e 67 20 6d 61 78 66 64 2c 20 69 64 78 3b 0a ong maxfd, idx;.
4380: 0a 09 6d 61 78 66 64 20 3d 20 73 79 73 63 6f 6e ..maxfd = syscon
4390: 66 28 5f 53 43 5f 4f 50 45 4e 5f 4d 41 58 29 3b f(_SC_OPEN_MAX);
43a0: 0a 09 69 66 20 28 6d 61 78 66 64 20 3c 3d 20 30 ..if (maxfd <= 0
43b0: 29 20 7b 0a 09 09 6d 61 78 66 64 20 3d 20 34 30 ) {...maxfd = 40
43c0: 39 36 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 96;..}...filed_s
43d0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
43e0: 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 20 3d kstatus_length =
43f0: 20 6d 61 78 66 64 3b 0a 09 66 69 6c 65 64 5f 73 maxfd;..filed_s
4400: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
4410: 6b 73 74 61 74 75 73 20 3d 20 6d 61 6c 6c 6f 63 kstatus = malloc
4420: 28 73 69 7a 65 6f 66 28 2a 66 69 6c 65 64 5f 73 (sizeof(*filed_s
4430: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
4440: 6b 73 74 61 74 75 73 29 20 2a 20 66 69 6c 65 64 kstatus) * filed
4450: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
4460: 6f 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 ockstatus_length
4470: 29 3b 0a 09 69 66 20 28 66 69 6c 65 64 5f 73 6f );..if (filed_so
4480: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b ckettimeout_sock
4490: 73 74 61 74 75 73 20 3d 3d 20 4e 55 4c 4c 29 20 status == NULL)
44a0: 7b 0a 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a {...return(-1);.
44b0: 09 7d 0a 0a 09 66 6f 72 20 28 69 64 78 20 3d 20 .}...for (idx =
44c0: 30 3b 20 69 64 78 20 3c 20 6d 61 78 66 64 3b 20 0; idx < maxfd;
44d0: 69 64 78 2b 2b 29 20 7b 0a 09 09 66 69 6c 65 64 idx++) {...filed
44e0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
44f0: 6f 63 6b 73 74 61 74 75 73 5b 69 64 78 5d 2e 76 ockstatus[idx].v
4500: 61 6c 69 64 20 3d 20 66 61 6c 73 65 3b 0a 09 7d alid = false;..}
4510: 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ...filed_sockett
4520: 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 imeout_devnull_f
4530: 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e d = open("/dev/n
4540: 75 6c 6c 22 2c 20 4f 5f 52 44 57 52 29 3b 0a 09 ull", O_RDWR);..
4550: 69 66 20 28 66 69 6c 65 64 5f 73 6f 63 6b 65 74 if (filed_socket
4560: 74 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f timeout_devnull_
4570: 66 64 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 fd < 0) {...retu
4580: 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 rn(-1);..}...ret
4590: 75 72 6e 28 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 urn(0);.}.#endif
45a0: 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20 74 69 6d 65 ../* Format time
45b0: 20 70 65 72 20 52 46 43 32 36 31 36 20 2a 2f 0a per RFC2616 */.
45c0: 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 69 6c static char *fil
45d0: 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 ed_format_time(c
45e0: 68 61 72 20 2a 62 75 66 66 65 72 2c 20 73 69 7a har *buffer, siz
45f0: 65 5f 74 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 e_t buffer_len,
4600: 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 74 69 6d const time_t tim
4610: 65 69 6e 66 6f 29 20 7b 0a 09 73 74 72 75 63 74 einfo) {..struct
4620: 20 74 6d 20 74 69 6d 65 69 6e 66 6f 5f 74 6d 2c tm timeinfo_tm,
4630: 20 2a 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 3b *timeinfo_tm_p;
4640: 0a 0a 09 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 ...timeinfo_tm_p
4650: 20 3d 20 67 6d 74 69 6d 65 5f 72 28 26 74 69 6d = gmtime_r(&tim
4660: 65 69 6e 66 6f 2c 20 26 74 69 6d 65 69 6e 66 6f einfo, &timeinfo
4670: 5f 74 6d 29 3b 0a 09 69 66 20 28 74 69 6d 65 69 _tm);..if (timei
4680: 6e 66 6f 5f 74 6d 5f 70 20 3d 3d 20 4e 55 4c 4c nfo_tm_p == NULL
4690: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 22 75 6e ) {...return("un
46a0: 6b 6e 6f 77 6e 22 29 3b 0a 09 7d 0a 0a 09 62 75 known");..}...bu
46b0: 66 66 65 72 5b 62 75 66 66 65 72 5f 6c 65 6e 20 ffer[buffer_len
46c0: 2d 20 31 5d 20 3d 20 27 5c 30 27 3b 0a 09 62 75 - 1] = '\0';..bu
46d0: 66 66 65 72 5f 6c 65 6e 20 3d 20 73 74 72 66 74 ffer_len = strft
46e0: 69 6d 65 28 62 75 66 66 65 72 2c 20 62 75 66 66 ime(buffer, buff
46f0: 65 72 5f 6c 65 6e 20 2d 20 31 2c 20 22 25 61 2c er_len - 1, "%a,
4700: 20 25 64 20 25 62 20 25 59 20 25 48 3a 25 4d 3a %d %b %Y %H:%M:
4710: 25 53 20 47 4d 54 22 2c 20 74 69 6d 65 69 6e 66 %S GMT", timeinf
4720: 6f 5f 74 6d 5f 70 29 3b 0a 0a 09 72 65 74 75 72 o_tm_p);...retur
4730: 6e 28 62 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a n(buffer);.}../*
4740: 20 68 61 73 68 20 2a 2f 0a 73 74 61 74 69 63 20 hash */.static
4750: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 66 69 6c unsigned int fil
4760: 65 64 5f 68 61 73 68 28 63 6f 6e 73 74 20 75 6e ed_hash(const un
4770: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c signed char *val
4780: 75 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 ue, unsigned int
4790: 20 6d 6f 64 75 6c 75 73 29 20 7b 0a 09 75 6e 73 modulus) {..uns
47a0: 69 67 6e 65 64 20 63 68 61 72 20 63 75 72 72 2c igned char curr,
47b0: 20 70 72 65 76 3b 0a 09 69 6e 74 20 64 69 66 66 prev;..int diff
47c0: 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 ;..unsigned int
47d0: 72 65 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c retval;...retval
47e0: 20 3d 20 6d 6f 64 75 6c 75 73 20 2d 20 31 3b 0a = modulus - 1;.
47f0: 09 70 72 65 76 20 3d 20 6d 6f 64 75 6c 75 73 20 .prev = modulus
4800: 25 20 32 35 35 3b 0a 0a 09 77 68 69 6c 65 20 28 % 255;...while (
4810: 28 63 75 72 72 20 3d 20 2a 76 61 6c 75 65 29 29 (curr = *value))
4820: 20 7b 0a 09 09 69 66 20 28 63 75 72 72 20 3c 20 {...if (curr <
4830: 33 32 29 20 7b 0a 09 09 09 63 75 72 72 20 3d 20 32) {....curr =
4840: 32 35 35 20 2d 20 63 75 72 72 3b 0a 09 09 7d 20 255 - curr;...}
4850: 65 6c 73 65 20 7b 0a 09 09 09 63 75 72 72 20 2d else {....curr -
4860: 3d 20 33 32 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 = 32;...}....if
4870: 28 70 72 65 76 20 3c 20 63 75 72 72 29 20 7b 0a (prev < curr) {.
4880: 09 09 09 64 69 66 66 20 3d 20 63 75 72 72 20 2d ...diff = curr -
4890: 20 70 72 65 76 3b 0a 09 09 7d 20 65 6c 73 65 20 prev;...} else
48a0: 7b 0a 09 09 09 64 69 66 66 20 3d 20 70 72 65 76 {....diff = prev
48b0: 20 2d 20 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 - curr;...}....
48c0: 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 0a 09 09 prev = curr;....
48d0: 72 65 74 76 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 retval <<= 3;...
48e0: 72 65 74 76 61 6c 20 26 3d 20 30 78 46 46 46 46 retval &= 0xFFFF
48f0: 46 46 46 46 4c 55 3b 0a 09 09 72 65 74 76 61 6c FFFFLU;...retval
4900: 20 5e 3d 20 64 69 66 66 3b 0a 0a 09 09 76 61 6c ^= diff;....val
4910: 75 65 2b 2b 3b 0a 09 7d 0a 0a 09 72 65 74 76 61 ue++;..}...retva
4920: 6c 20 3d 20 72 65 74 76 61 6c 20 25 20 6d 6f 64 l = retval % mod
4930: 75 6c 75 73 3b 0a 0a 09 72 65 74 75 72 6e 28 72 ulus;...return(r
4940: 65 74 76 61 6c 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 etval);.}../* Fi
4950: 6e 64 20 61 20 6d 69 6d 65 2d 74 79 70 65 20 62 nd a mime-type b
4960: 61 73 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 ased on the file
4970: 6e 61 6d 65 20 2a 2f 0a 73 74 61 74 69 63 20 63 name */.static c
4980: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 onst char *filed
4990: 5f 64 65 74 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 _determine_mimet
49a0: 79 70 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ype(const char *
49b0: 70 61 74 68 29 20 7b 0a 09 63 6f 6e 73 74 20 63 path) {..const c
49c0: 68 61 72 20 2a 70 3b 0a 0a 09 70 20 3d 20 73 74 har *p;...p = st
49d0: 72 72 63 68 72 28 70 61 74 68 2c 20 27 2e 27 29 rrchr(path, '.')
49e0: 3b 0a 09 69 66 20 28 70 20 3d 3d 20 4e 55 4c 4c ;..if (p == NULL
49f0: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 46 49 4c ) {...return(FIL
4a00: 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 29 ED_DEFAULT_TYPE)
4a10: 3b 0a 09 7d 0a 0a 09 70 2b 2b 3b 0a 09 69 66 20 ;..}...p++;..if
4a20: 28 2a 70 20 3d 3d 20 27 5c 30 27 29 20 7b 0a 09 (*p == '\0') {..
4a30: 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 45 .return(FILED_DE
4a40: 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d 0a FAULT_TYPE);..}.
4a50: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f ..filed_log_msg_
4a60: 64 65 62 75 67 28 22 4c 6f 6f 6b 69 6e 67 20 75 debug("Looking u
4a70: 70 20 4d 49 4d 45 20 74 79 70 65 20 66 6f 72 20 p MIME type for
4a80: 25 73 20 28 68 61 73 68 20 3d 20 25 6c 6c 75 29 %s (hash = %llu)
4a90: 22 2c 20 70 2c 20 28 75 6e 73 69 67 6e 65 64 20 ", p, (unsigned
4aa0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 64 long long) filed
4ab0: 5f 68 61 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 _hash((const uns
4ac0: 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70 2c igned char *) p,
4ad0: 20 31 36 37 37 37 32 35 39 29 29 3b 0a 0a 23 69 16777259));..#i
4ae0: 6e 63 6c 75 64 65 20 22 66 69 6c 65 64 2d 6d 69 nclude "filed-mi
4af0: 6d 65 2d 74 79 70 65 73 2e 68 22 0a 0a 09 72 65 me-types.h"...re
4b00: 74 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 turn(FILED_DEFAU
4b10: 4c 54 5f 54 59 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 LT_TYPE);.}../*
4b20: 47 65 6e 65 72 61 74 65 20 61 20 75 6e 69 71 75 Generate a uniqu
4b30: 65 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a e identifier */.
4b40: 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 static void file
4b50: 64 5f 67 65 6e 65 72 61 74 65 5f 65 74 61 67 28 d_generate_etag(
4b60: 63 68 61 72 20 2a 65 74 61 67 2c 20 73 69 7a 65 char *etag, size
4b70: 5f 74 20 6c 65 6e 67 74 68 29 20 7b 0a 09 73 6e _t length) {..sn
4b80: 70 72 69 6e 74 66 28 65 74 61 67 2c 20 6c 65 6e printf(etag, len
4b90: 67 74 68 2c 20 22 25 6c 6c 78 2d 25 6c 6c 78 25 gth, "%llx-%llx%
4ba0: 6c 6c 78 25 6c 6c 78 25 6c 6c 78 22 2c 0a 09 09 llx%llx%llx",...
4bb0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
4bc0: 6f 6e 67 29 20 74 69 6d 65 28 4e 55 4c 4c 29 2c ong) time(NULL),
4bd0: 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
4be0: 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 g long) random()
4bf0: 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f ,...(unsigned lo
4c00: 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 ng long) random(
4c10: 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c ),...(unsigned l
4c20: 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d ong long) random
4c30: 28 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 (),...(unsigned
4c40: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f long long) rando
4c50: 6d 28 29 0a 09 29 3b 0a 7d 0a 0a 23 69 66 64 65 m()..);.}..#ifde
4c60: 66 20 46 49 4c 45 44 5f 46 41 4b 45 5f 43 48 52 f FILED_FAKE_CHR
4c70: 4f 4f 54 0a 2f 2a 20 54 72 61 6e 73 6c 61 74 65 OOT./* Translate
4c80: 20 61 20 70 61 74 68 20 69 6e 74 6f 20 61 20 66 a path into a f
4c90: 61 6b 65 20 63 68 72 6f 6f 74 20 70 61 74 68 20 ake chroot path
4ca0: 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
4cb0: 63 68 61 72 20 2a 66 69 6c 65 64 5f 70 61 74 68 char *filed_path
4cc0: 5f 74 72 61 6e 73 6c 61 74 65 28 63 6f 6e 73 74 _translate(const
4cd0: 20 63 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 char *path, str
4ce0: 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e uct filed_option
4cf0: 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 s *options) {..s
4d00: 74 61 74 69 63 20 5f 5f 74 68 72 65 61 64 20 63 tatic __thread c
4d10: 68 61 72 20 70 61 74 68 42 75 66 66 65 72 5b 38 har pathBuffer[8
4d20: 31 39 32 5d 3b 0a 09 69 6e 74 20 73 6e 70 72 69 192];..int snpri
4d30: 6e 74 66 5f 72 65 74 3b 0a 0a 09 2f 2a 20 49 66 ntf_ret;.../* If
4d40: 20 6e 6f 20 61 6c 74 65 72 6e 61 74 69 76 65 20 no alternative
4d50: 72 6f 6f 74 20 69 73 20 73 70 65 63 69 66 69 65 root is specifie
4d60: 64 2c 20 72 65 74 75 72 6e 20 74 68 65 20 75 6e d, return the un
4d70: 61 64 6f 72 6e 65 64 20 70 61 74 68 20 2a 2f 0a adorned path */.
4d80: 09 69 66 20 28 21 6f 70 74 69 6f 6e 73 2d 3e 66 .if (!options->f
4d90: 61 6b 65 5f 6e 65 77 72 6f 6f 74 29 20 7b 0a 09 ake_newroot) {..
4da0: 09 72 65 74 75 72 6e 28 70 61 74 68 29 3b 0a 09 .return(path);..
4db0: 7d 0a 0a 09 2f 2a 20 56 65 72 69 66 79 20 74 68 }.../* Verify th
4dc0: 61 74 20 74 68 69 73 20 72 65 71 75 65 73 74 20 at this request
4dd0: 77 69 6c 6c 20 6e 6f 74 20 67 6f 20 6f 75 74 73 will not go outs
4de0: 69 64 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 ide of the speci
4df0: 66 69 65 64 20 72 6f 6f 74 20 2a 2f 0a 09 69 66 fied root */..if
4e00: 20 28 73 74 72 73 74 72 28 70 61 74 68 2c 20 22 (strstr(path, "
4e10: 2f 2e 2e 2f 22 29 20 21 3d 20 4e 55 4c 4c 20 7c /../") != NULL |
4e20: 7c 20 70 61 74 68 5b 30 5d 20 21 3d 20 27 2f 27 | path[0] != '/'
4e30: 29 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f ) {...filed_log_
4e40: 6d 73 67 5f 64 65 62 75 67 28 22 55 6e 61 62 6c msg_debug("Unabl
4e50: 65 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 70 e to translate p
4e60: 61 74 68 20 5c 22 25 73 5c 22 2c 20 63 6f 6e 74 ath \"%s\", cont
4e70: 61 69 6e 73 20 69 6e 76 61 6c 69 64 20 63 68 61 ains invalid cha
4e80: 72 61 63 74 65 72 73 22 2c 20 70 61 74 68 29 3b racters", path);
4e90: 0a 0a 09 09 72 65 74 75 72 6e 28 6f 70 74 69 6f ....return(optio
4ea0: 6e 73 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 ns->fake_newroot
4eb0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 );..}.../* Creat
4ec0: 65 20 74 68 65 20 6e 65 77 20 70 61 74 68 20 69 e the new path i
4ed0: 6e 74 6f 20 6f 75 72 20 6c 6f 63 61 6c 20 28 54 nto our local (T
4ee0: 4c 53 29 20 73 74 61 74 69 63 20 62 75 66 66 65 LS) static buffe
4ef0: 72 20 2a 2f 0a 09 73 6e 70 72 69 6e 74 66 5f 72 r */..snprintf_r
4f00: 65 74 20 3d 20 73 6e 70 72 69 6e 74 66 28 70 61 et = snprintf(pa
4f10: 74 68 42 75 66 66 65 72 2c 20 73 69 7a 65 6f 66 thBuffer, sizeof
4f20: 28 70 61 74 68 42 75 66 66 65 72 29 2c 20 22 25 (pathBuffer), "%
4f30: 73 2f 25 73 22 2c 20 6f 70 74 69 6f 6e 73 2d 3e s/%s", options->
4f40: 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 2c 20 70 61 fake_newroot, pa
4f50: 74 68 29 3b 0a 09 69 66 20 28 73 6e 70 72 69 6e th);..if (snprin
4f60: 74 66 5f 72 65 74 20 3c 20 30 20 7c 7c 20 28 28 tf_ret < 0 || ((
4f70: 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 20 73 6e unsigned int) sn
4f80: 70 72 69 6e 74 66 5f 72 65 74 29 20 3e 3d 20 73 printf_ret) >= s
4f90: 69 7a 65 6f 66 28 70 61 74 68 42 75 66 66 65 72 izeof(pathBuffer
4fa0: 29 29 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 )) {...filed_log
4fb0: 5f 6d 73 67 5f 64 65 62 75 67 28 22 55 6e 61 62 _msg_debug("Unab
4fc0: 6c 65 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 le to translate
4fd0: 70 61 74 68 20 5c 22 25 73 5c 22 2c 20 77 69 6c path \"%s\", wil
4fe0: 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 6f 20 6e l not fit into n
4ff0: 65 77 20 62 75 66 66 65 72 22 2c 20 70 61 74 68 ew buffer", path
5000: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 6f 70 74 );....return(opt
5010: 69 6f 6e 73 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f ions->fake_newro
5020: 6f 74 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f ot);..}...filed_
5030: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 54 log_msg_debug("T
5040: 72 61 6e 73 6c 61 74 69 6e 67 20 70 61 74 68 20 ranslating path
5050: 5c 22 25 73 5c 22 20 69 6e 74 6f 20 5c 22 25 73 \"%s\" into \"%s
5060: 5c 22 22 2c 20 70 61 74 68 2c 20 70 61 74 68 42 \"", path, pathB
5070: 75 66 66 65 72 29 3b 0a 0a 09 2f 2a 20 52 65 74 uffer);.../* Ret
5080: 75 72 6e 20 74 68 65 20 6e 65 77 20 70 61 74 68 urn the new path
5090: 20 2a 2f 0a 09 72 65 74 75 72 6e 28 70 61 74 68 */..return(path
50a0: 42 75 66 66 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 Buffer);.}..stat
50b0: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 70 61 ic void filed_pa
50c0: 74 68 5f 74 72 61 6e 73 6c 61 74 65 5f 73 65 74 th_translate_set
50d0: 5f 72 6f 6f 74 28 63 6f 6e 73 74 20 63 68 61 72 _root(const char
50e0: 20 2a 76 61 72 2c 20 73 74 72 75 63 74 20 66 69 *var, struct fi
50f0: 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 led_options *opt
5100: 69 6f 6e 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 ions, const char
5110: 20 2a 76 61 6c 29 20 7b 0a 09 6f 70 74 69 6f 6e *val) {..option
5120: 73 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 20 s->fake_newroot
5130: 3d 20 73 74 72 64 75 70 28 76 61 6c 29 3b 0a 0a = strdup(val);..
5140: 09 72 65 74 75 72 6e 3b 0a 0a 09 2f 2a 20 76 61 .return;.../* va
5150: 72 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 r is only used i
5160: 6e 20 74 68 65 20 6d 61 63 72 6f 20 2d 2d 20 64 n the macro -- d
5170: 69 73 63 61 72 64 20 69 74 20 68 65 72 65 20 2a iscard it here *
5180: 2f 0a 09 76 61 72 20 3d 20 76 61 72 3b 0a 7d 0a /..var = var;.}.
5190: 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 66 69 #else.#define fi
51a0: 6c 65 64 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 led_path_transla
51b0: 74 65 28 70 61 74 68 2c 20 6f 70 74 69 6f 6e 73 te(path, options
51c0: 29 20 70 61 74 68 0a 23 64 65 66 69 6e 65 20 66 ) path.#define f
51d0: 69 6c 65 64 5f 70 61 74 68 5f 74 72 61 6e 73 6c iled_path_transl
51e0: 61 74 65 5f 73 65 74 5f 72 6f 6f 74 28 76 61 72 ate_set_root(var
51f0: 2c 20 6f 70 74 69 6f 6e 73 2c 20 76 61 6c 29 20 , options, val)
5200: 76 61 72 20 3d 20 73 74 72 64 75 70 28 76 61 6c var = strdup(val
5210: 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 65 ).#endif../* Ope
5220: 6e 20 61 20 66 69 6c 65 20 61 6e 64 20 72 65 74 n a file and ret
5230: 75 72 6e 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 urn file informa
5240: 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 73 tion */.static s
5250: 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 truct filed_file
5260: 69 6e 66 6f 20 2a 66 69 6c 65 64 5f 6f 70 65 6e info *filed_open
5270: 5f 66 69 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 _file(const char
5280: 20 2a 70 61 74 68 2c 20 73 74 72 75 63 74 20 66 *path, struct f
5290: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 62 iled_fileinfo *b
52a0: 75 66 66 65 72 2c 20 73 74 72 75 63 74 20 66 69 uffer, struct fi
52b0: 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 led_options *opt
52c0: 69 6f 6e 73 29 20 7b 0a 09 73 74 72 75 63 74 20 ions) {..struct
52d0: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a filed_fileinfo *
52e0: 63 61 63 68 65 3b 0a 09 75 6e 73 69 67 6e 65 64 cache;..unsigned
52f0: 20 69 6e 74 20 63 61 63 68 65 5f 69 64 78 3b 0a int cache_idx;.
5300: 09 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09 69 6e 74 .off_t len;..int
5310: 20 66 64 3b 0a 0a 09 69 66 20 28 66 69 6c 65 64 fd;...if (filed
5320: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 _fileinfo_fdcach
5330: 65 5f 73 69 7a 65 20 21 3d 20 30 29 20 7b 0a 09 e_size != 0) {..
5340: 09 63 61 63 68 65 5f 69 64 78 20 3d 20 66 69 6c .cache_idx = fil
5350: 65 64 5f 68 61 73 68 28 28 63 6f 6e 73 74 20 75 ed_hash((const u
5360: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 nsigned char *)
5370: 70 61 74 68 2c 20 66 69 6c 65 64 5f 66 69 6c 65 path, filed_file
5380: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a info_fdcache_siz
5390: 65 29 3b 0a 0a 09 09 63 61 63 68 65 20 3d 20 26 e);....cache = &
53a0: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
53b0: 64 63 61 63 68 65 5b 63 61 63 68 65 5f 69 64 78 dcache[cache_idx
53c0: 5d 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f ];....filed_log_
53d0: 6d 73 67 5f 64 65 62 75 67 28 22 4c 6f 63 6b 69 msg_debug("Locki
53e0: 6e 67 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 ng mutex for idx
53f0: 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 : %lu", (unsigne
5400: 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 d long) cache_id
5410: 78 29 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d x);....pthread_m
5420: 75 74 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 65 utex_lock(&cache
5430: 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 66 69 6c ->mutex);....fil
5440: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
5450: 28 22 43 6f 6d 70 6c 65 74 65 64 20 6c 6f 63 6b ("Completed lock
5460: 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20 69 64 ing mutex for id
5470: 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e x: %lu", (unsign
5480: 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 ed long) cache_i
5490: 64 78 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 dx);..} else {..
54a0: 09 63 61 63 68 65 5f 69 64 78 20 3d 20 30 3b 0a .cache_idx = 0;.
54b0: 09 09 63 61 63 68 65 20 3d 20 62 75 66 66 65 72 ..cache = buffer
54c0: 3b 0a 09 09 63 61 63 68 65 2d 3e 70 61 74 68 5b ;...cache->path[
54d0: 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 63 61 63 0] = '\0';...cac
54e0: 68 65 2d 3e 66 64 20 3d 20 2d 31 3b 0a 09 7d 0a he->fd = -1;..}.
54f0: 0a 09 69 66 20 28 73 74 72 63 6d 70 28 70 61 74 ..if (strcmp(pat
5500: 68 2c 20 63 61 63 68 65 2d 3e 70 61 74 68 29 20 h, cache->path)
5510: 21 3d 20 30 29 20 7b 0a 09 09 66 69 6c 65 64 5f != 0) {...filed_
5520: 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 log_msg_debug("C
5530: 61 63 68 65 20 6d 69 73 73 20 66 6f 72 20 69 64 ache miss for id
5540: 78 3a 20 25 6c 75 3a 20 4f 4c 44 20 5c 22 25 73 x: %lu: OLD \"%s
5550: 5c 22 2c 20 4e 45 57 20 5c 22 25 73 5c 22 22 2c \", NEW \"%s\"",
5560: 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 (unsigned long)
5570: 20 63 61 63 68 65 5f 69 64 78 2c 20 63 61 63 68 cache_idx, cach
5580: 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 3b 0a e->path, path);.
5590: 0a 09 09 66 64 20 3d 20 6f 70 65 6e 28 66 69 6c ...fd = open(fil
55a0: 65 64 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 ed_path_translat
55b0: 65 28 70 61 74 68 2c 20 6f 70 74 69 6f 6e 73 29 e(path, options)
55c0: 2c 20 4f 5f 52 44 4f 4e 4c 59 20 7c 20 4f 5f 4c , O_RDONLY | O_L
55d0: 41 52 47 45 46 49 4c 45 29 3b 0a 09 09 69 66 20 ARGEFILE);...if
55e0: 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 09 69 66 (fd < 0) {....if
55f0: 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f (filed_fileinfo
5600: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 21 3d _fdcache_size !=
5610: 20 30 29 20 7b 0a 09 09 09 09 70 74 68 72 65 61 0) {.....pthrea
5620: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
5630: 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 09 cache->mutex);..
5640: 09 09 7d 0a 0a 09 09 09 72 65 74 75 72 6e 28 4e ..}.....return(N
5650: 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 ULL);...}....if
5660: 28 63 61 63 68 65 2d 3e 66 64 20 3e 3d 20 30 29 (cache->fd >= 0)
5670: 20 7b 0a 09 09 09 63 6c 6f 73 65 28 63 61 63 68 {....close(cach
5680: 65 2d 3e 66 64 29 3b 0a 09 09 7d 0a 0a 09 09 6c e->fd);...}....l
5690: 65 6e 20 3d 20 6c 73 65 65 6b 28 66 64 2c 20 30 en = lseek(fd, 0
56a0: 2c 20 53 45 45 4b 5f 45 4e 44 29 3b 0a 09 09 6c , SEEK_END);...l
56b0: 73 65 65 6b 28 66 64 2c 20 30 2c 20 53 45 45 4b seek(fd, 0, SEEK
56c0: 5f 53 45 54 29 3b 0a 0a 09 09 63 61 63 68 65 2d _SET);....cache-
56d0: 3e 66 64 20 3d 20 66 64 3b 0a 09 09 63 61 63 68 >fd = fd;...cach
56e0: 65 2d 3e 6c 65 6e 20 3d 20 6c 65 6e 3b 0a 09 09 e->len = len;...
56f0: 73 74 72 63 70 79 28 63 61 63 68 65 2d 3e 70 61 strcpy(cache->pa
5700: 74 68 2c 20 70 61 74 68 29 3b 0a 09 09 63 61 63 th, path);...cac
5710: 68 65 2d 3e 74 79 70 65 20 3d 20 66 69 6c 65 64 he->type = filed
5720: 5f 64 65 74 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 _determine_mimet
5730: 79 70 65 28 70 61 74 68 29 3b 0a 09 09 66 69 6c ype(path);...fil
5740: 65 64 5f 67 65 6e 65 72 61 74 65 5f 65 74 61 67 ed_generate_etag
5750: 28 63 61 63 68 65 2d 3e 65 74 61 67 2c 20 73 69 (cache->etag, si
5760: 7a 65 6f 66 28 63 61 63 68 65 2d 3e 65 74 61 67 zeof(cache->etag
5770: 29 29 3b 0a 0a 09 09 2f 2a 20 58 58 58 3a 54 4f ));..../* XXX:TO
5780: 44 4f 3a 20 44 65 74 65 72 6d 69 6e 65 20 2a 2f DO: Determine */
5790: 0a 09 09 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f ...cache->lastmo
57a0: 64 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d 61 74 d = filed_format
57b0: 5f 74 69 6d 65 28 63 61 63 68 65 2d 3e 6c 61 73 _time(cache->las
57c0: 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 28 63 tmod_b, sizeof(c
57d0: 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 ache->lastmod_b)
57e0: 2c 20 74 69 6d 65 28 4e 55 4c 4c 29 20 2d 20 33 , time(NULL) - 3
57f0: 30 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 0);..} else {...
5800: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 filed_log_msg_de
5810: 62 75 67 28 22 43 61 63 68 65 20 68 69 74 20 66 bug("Cache hit f
5820: 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20 50 41 54 or idx: %lu: PAT
5830: 48 20 5c 22 25 73 5c 22 22 2c 20 28 75 6e 73 69 H \"%s\"", (unsi
5840: 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 gned long) cache
5850: 5f 69 64 78 2c 20 70 61 74 68 29 3b 0a 09 7d 0a _idx, path);..}.
5860: 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 ..if (filed_file
5870: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a info_fdcache_siz
5880: 65 20 21 3d 20 30 29 20 7b 0a 09 09 2f 2a 0a 09 e != 0) {.../*..
5890: 09 20 2a 20 57 65 20 68 61 76 65 20 74 6f 20 6d . * We have to m
58a0: 61 6b 65 20 61 20 64 75 70 6c 69 63 61 74 65 20 ake a duplicate
58b0: 46 44 2c 20 62 65 63 61 75 73 65 20 6f 6e 63 65 FD, because once
58c0: 20 77 65 20 72 65 6c 65 61 73 65 20 74 68 65 20 we release the
58d0: 63 61 63 68 65 0a 09 09 20 2a 20 6d 75 74 65 78 cache... * mutex
58e0: 2c 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 , the file descr
58f0: 69 70 74 6f 72 20 6d 61 79 20 62 65 20 63 6c 6f iptor may be clo
5900: 73 65 64 0a 09 09 20 2a 2f 0a 09 09 66 64 20 3d sed... */...fd =
5910: 20 64 75 70 28 63 61 63 68 65 2d 3e 66 64 29 3b dup(cache->fd);
5920: 0a 09 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b ...if (fd < 0) {
5930: 0a 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 ....pthread_mute
5940: 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d x_unlock(&cache-
5950: 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09 72 65 74 >mutex);.....ret
5960: 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a urn(NULL);...}..
5970: 09 09 62 75 66 66 65 72 2d 3e 66 64 20 3d 20 66 ..buffer->fd = f
5980: 64 3b 0a 09 09 62 75 66 66 65 72 2d 3e 6c 65 6e d;...buffer->len
5990: 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e 3b 0a 09 = cache->len;..
59a0: 09 62 75 66 66 65 72 2d 3e 74 79 70 65 20 3d 20 .buffer->type =
59b0: 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a 09 09 6d cache->type;...m
59c0: 65 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 6c 61 emcpy(buffer->la
59d0: 73 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d 3e stmod_b, cache->
59e0: 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f lastmod_b, sizeo
59f0: 66 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f f(buffer->lastmo
5a00: 64 5f 62 29 29 3b 0a 09 09 6d 65 6d 63 70 79 28 d_b));...memcpy(
5a10: 62 75 66 66 65 72 2d 3e 65 74 61 67 2c 20 63 61 buffer->etag, ca
5a20: 63 68 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f che->etag, sizeo
5a30: 66 28 62 75 66 66 65 72 2d 3e 65 74 61 67 29 29 f(buffer->etag))
5a40: 3b 0a 09 09 62 75 66 66 65 72 2d 3e 6c 61 73 74 ;...buffer->last
5a50: 6d 6f 64 20 3d 20 62 75 66 66 65 72 2d 3e 6c 61 mod = buffer->la
5a60: 73 74 6d 6f 64 5f 62 20 2b 20 28 63 61 63 68 65 stmod_b + (cache
5a70: 2d 3e 6c 61 73 74 6d 6f 64 20 2d 20 63 61 63 68 ->lastmod - cach
5a80: 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 3b 0a 0a e->lastmod_b);..
5a90: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ..pthread_mutex_
5aa0: 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d unlock(&cache->m
5ab0: 75 74 65 78 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 utex);..}...retu
5ac0: 72 6e 28 62 75 66 66 65 72 29 3b 0a 0a 09 2f 2a rn(buffer);.../*
5ad0: 20 6f 70 74 69 6f 6e 73 20 69 73 20 6f 6e 6c 79 options is only
5ae0: 20 75 73 65 64 20 69 66 20 66 61 6b 65 20 63 68 used if fake ch
5af0: 72 6f 6f 74 20 69 73 20 65 6e 61 62 6c 65 64 2c root is enabled,
5b00: 20 63 6f 6e 66 75 73 65 20 74 68 65 20 63 6f 6d confuse the com
5b10: 70 69 6c 65 72 20 2a 2f 0a 09 6f 70 74 69 6f 6e piler */..option
5b20: 73 20 3d 20 6f 70 74 69 6f 6e 73 3b 0a 7d 0a 0a s = options;.}..
5b30: 2f 2a 20 50 72 6f 63 65 73 73 20 61 6e 20 48 54 /* Process an HT
5b40: 54 50 20 72 65 71 75 65 73 74 20 61 6e 64 20 72 TP request and r
5b50: 65 74 75 72 6e 20 74 68 65 20 70 61 74 68 20 72 eturn the path r
5b60: 65 71 75 65 73 74 65 64 20 2a 2f 0a 73 74 61 74 equested */.stat
5b70: 69 63 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f ic struct filed_
5b80: 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 66 69 http_request *fi
5b90: 6c 65 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71 led_get_http_req
5ba0: 75 65 73 74 28 46 49 4c 45 20 2a 66 70 2c 20 73 uest(FILE *fp, s
5bb0: 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 truct filed_http
5bc0: 5f 72 65 71 75 65 73 74 20 2a 62 75 66 66 65 72 _request *buffer
5bd0: 5f 73 74 2c 20 73 74 72 75 63 74 20 66 69 6c 65 _st, struct file
5be0: 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f d_options *optio
5bf0: 6e 73 29 20 7b 0a 09 63 68 61 72 20 2a 6d 65 74 ns) {..char *met
5c00: 68 6f 64 2c 20 2a 70 61 74 68 3b 0a 09 63 68 61 hod, *path;..cha
5c10: 72 20 2a 62 75 66 66 65 72 2c 20 2a 77 6f 72 6b r *buffer, *work
5c20: 62 75 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66 buffer, *workbuf
5c30: 66 65 72 5f 6e 65 78 74 3b 0a 09 63 68 61 72 20 fer_next;..char
5c40: 2a 66 67 65 74 73 5f 72 65 74 3b 0a 09 73 69 7a *fgets_ret;..siz
5c50: 65 5f 74 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 e_t buffer_len,
5c60: 70 61 74 68 5f 6c 65 6e 3b 0a 09 6f 66 66 5f 74 path_len;..off_t
5c70: 20 72 61 6e 67 65 5f 73 74 61 72 74 2c 20 72 61 range_start, ra
5c80: 6e 67 65 5f 65 6e 64 2c 20 72 61 6e 67 65 5f 6c nge_end, range_l
5c90: 65 6e 67 74 68 3b 0a 09 69 6e 74 20 72 61 6e 67 ength;..int rang
5ca0: 65 5f 72 65 71 75 65 73 74 3b 0a 09 69 6e 74 20 e_request;..int
5cb0: 73 6e 70 72 69 6e 74 66 5f 72 65 74 3b 0a 09 69 snprintf_ret;..i
5cc0: 6e 74 20 69 3b 0a 0a 09 2f 2a 20 53 65 74 20 74 nt i;.../* Set t
5cd0: 6f 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 o default values
5ce0: 20 2a 2f 0a 09 72 61 6e 67 65 5f 73 74 61 72 74 */..range_start
5cf0: 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 65 6e 64 = 0;..range_end
5d00: 20 20 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 72 = 0;..range_r
5d10: 65 71 75 65 73 74 20 3d 20 30 3b 0a 09 72 61 6e equest = 0;..ran
5d20: 67 65 5f 6c 65 6e 67 74 68 20 3d 20 2d 31 3b 0a ge_length = -1;.
5d30: 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 .buffer_st->head
5d40: 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74 ers.host.present
5d50: 20 3d 20 30 3b 0a 09 62 75 66 66 65 72 5f 73 74 = 0;..buffer_st
5d60: 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 ->headers.connec
5d70: 74 69 6f 6e 20 3d 20 46 49 4c 45 44 5f 43 4f 4e tion = FILED_CON
5d80: 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 3b 0a 0a NECTION_CLOSE;..
5d90: 09 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 .buffer = buffer
5da0: 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a 09 62 75 _st->tmpbuf;..bu
5db0: 66 66 65 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f ffer_len = sizeo
5dc0: 66 28 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 f(buffer_st->tmp
5dd0: 62 75 66 29 3b 0a 0a 09 66 67 65 74 73 5f 72 65 buf);...fgets_re
5de0: 74 20 3d 20 66 67 65 74 73 28 62 75 66 66 65 72 t = fgets(buffer
5df0: 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 66 70 , buffer_len, fp
5e00: 29 3b 0a 09 69 66 20 28 66 67 65 74 73 5f 72 65 );..if (fgets_re
5e10: 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 t == NULL) {...r
5e20: 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a eturn(NULL);..}.
5e30: 0a 09 6d 65 74 68 6f 64 20 3d 20 62 75 66 66 65 ..method = buffe
5e40: 72 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 73 74 r;...buffer = st
5e50: 72 63 68 72 28 62 75 66 66 65 72 2c 20 27 20 27 rchr(buffer, ' '
5e60: 29 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20 3d );..if (buffer =
5e70: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 = NULL) {...retu
5e80: 72 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 2a rn(NULL);..}...*
5e90: 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 buffer = '\0';..
5ea0: 62 75 66 66 65 72 2b 2b 3b 0a 0a 09 70 61 74 68 buffer++;...path
5eb0: 20 3d 20 62 75 66 66 65 72 3b 0a 0a 09 2f 2a 20 = buffer;.../*
5ec0: 54 65 72 6d 69 6e 61 74 65 20 70 61 74 68 20 63 Terminate path c
5ed0: 6f 6d 70 6f 6e 65 6e 74 20 2a 2f 0a 09 62 75 66 omponent */..buf
5ee0: 66 65 72 20 3d 20 73 74 72 70 62 72 6b 28 70 61 fer = strpbrk(pa
5ef0: 74 68 2c 20 22 5c 72 5c 6e 20 22 29 3b 0a 09 69 th, "\r\n ");..i
5f00: 66 20 28 62 75 66 66 65 72 20 21 3d 20 4e 55 4c f (buffer != NUL
5f10: 4c 29 20 7b 0a 09 09 2a 62 75 66 66 65 72 20 3d L) {...*buffer =
5f20: 20 27 5c 30 27 3b 0a 09 09 62 75 66 66 65 72 2b '\0';...buffer+
5f30: 2b 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 65 20 6f 6e +;..}.../* We on
5f40: 6c 79 20 68 61 6e 64 6c 65 20 74 68 65 20 22 47 ly handle the "G
5f50: 45 54 22 20 61 6e 64 20 22 48 45 41 44 27 20 6d ET" and "HEAD' m
5f60: 65 74 68 6f 64 73 20 2a 2f 0a 09 69 66 20 28 73 ethods */..if (s
5f70: 74 72 63 61 73 65 63 6d 70 28 6d 65 74 68 6f 64 trcasecmp(method
5f80: 2c 20 22 68 65 61 64 22 29 20 21 3d 20 30 29 20 , "head") != 0)
5f90: 7b 0a 09 09 69 66 20 28 73 74 72 63 61 73 65 63 {...if (strcasec
5fa0: 6d 70 28 6d 65 74 68 6f 64 2c 20 22 67 65 74 22 mp(method, "get"
5fb0: 29 20 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 74 ) != 0) {....ret
5fc0: 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a urn(NULL);...}..
5fd0: 09 09 2f 2a 20 47 45 54 20 72 65 71 75 65 73 74 ../* GET request
5fe0: 20 2a 2f 0a 09 09 62 75 66 66 65 72 5f 73 74 2d */...buffer_st-
5ff0: 3e 6d 65 74 68 6f 64 20 3d 20 46 49 4c 45 44 5f >method = FILED_
6000: 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 REQUEST_METHOD_G
6010: 45 54 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 ET;..} else {...
6020: 2f 2a 20 48 45 41 44 20 72 65 71 75 65 73 74 20 /* HEAD request
6030: 2a 2f 0a 09 09 62 75 66 66 65 72 5f 73 74 2d 3e */...buffer_st->
6040: 6d 65 74 68 6f 64 20 3d 20 46 49 4c 45 44 5f 52 method = FILED_R
6050: 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45 EQUEST_METHOD_HE
6060: 41 44 3b 0a 09 7d 0a 0a 09 2f 2a 20 4e 6f 74 65 AD;..}.../* Note
6070: 20 70 61 74 68 20 2a 2f 0a 09 70 61 74 68 5f 6c path */..path_l
6080: 65 6e 20 3d 20 73 74 72 6c 65 6e 28 70 61 74 68 en = strlen(path
6090: 29 3b 0a 09 6d 65 6d 63 70 79 28 62 75 66 66 65 );..memcpy(buffe
60a0: 72 5f 73 74 2d 3e 70 61 74 68 2c 20 70 61 74 68 r_st->path, path
60b0: 2c 20 70 61 74 68 5f 6c 65 6e 20 2b 20 31 29 3b , path_len + 1);
60c0: 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 .../* Determine
60d0: 74 79 70 65 20 6f 66 20 72 65 71 75 65 73 74 20 type of request
60e0: 66 72 6f 6d 20 70 61 74 68 20 2a 2f 0a 09 69 66 from path */..if
60f0: 20 28 70 61 74 68 5f 6c 65 6e 20 3d 3d 20 30 29 (path_len == 0)
6100: 20 7b 0a 09 09 62 75 66 66 65 72 5f 73 74 2d 3e {...buffer_st->
6110: 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 52 45 51 type = FILED_REQ
6120: 55 45 53 54 5f 54 59 50 45 5f 44 49 52 45 43 54 UEST_TYPE_DIRECT
6130: 4f 52 59 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 ORY;..} else {..
6140: 09 69 66 20 28 70 61 74 68 5b 70 61 74 68 5f 6c .if (path[path_l
6150: 65 6e 20 2d 20 31 5d 20 3d 3d 20 27 2f 27 29 20 en - 1] == '/')
6160: 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e {....buffer_st->
6170: 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 52 45 51 type = FILED_REQ
6180: 55 45 53 54 5f 54 59 50 45 5f 44 49 52 45 43 54 UEST_TYPE_DIRECT
6190: 4f 52 59 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a ORY;...} else {.
61a0: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 74 79 ...buffer_st->ty
61b0: 70 65 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 pe = FILED_REQUE
61c0: 53 54 5f 54 59 50 45 5f 4f 54 48 45 52 3b 0a 09 ST_TYPE_OTHER;..
61d0: 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 52 65 73 65 74 .}..}.../* Reset
61e0: 20 62 75 66 66 65 72 20 66 6f 72 20 6c 61 74 65 buffer for late
61f0: 72 20 75 73 65 20 2a 2f 0a 09 62 75 66 66 65 72 r use */..buffer
6200: 20 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d = buffer_st->tm
6210: 70 62 75 66 3b 0a 0a 09 66 6f 72 20 28 69 20 3d pbuf;...for (i =
6220: 20 30 3b 20 69 20 3c 20 31 30 30 3b 20 69 2b 2b 0; i < 100; i++
6230: 29 20 7b 0a 09 09 66 67 65 74 73 5f 72 65 74 20 ) {...fgets_ret
6240: 3d 20 66 67 65 74 73 28 62 75 66 66 65 72 2c 20 = fgets(buffer,
6250: 62 75 66 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b buffer_len, fp);
6260: 0a 09 09 69 66 20 28 66 67 65 74 73 5f 72 65 74 ...if (fgets_ret
6270: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 62 == NULL) {....b
6280: 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 reak;...}....if
6290: 28 73 74 72 6e 63 61 73 65 63 6d 70 28 62 75 66 (strncasecmp(buf
62a0: 66 65 72 2c 20 22 52 61 6e 67 65 3a 20 22 2c 20 fer, "Range: ",
62b0: 37 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 77 6f 7) == 0) {....wo
62c0: 72 6b 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 rkbuffer = buffe
62d0: 72 20 2b 20 37 3b 0a 0a 09 09 09 69 66 20 28 73 r + 7;.....if (s
62e0: 74 72 6e 63 61 73 65 63 6d 70 28 77 6f 72 6b 62 trncasecmp(workb
62f0: 75 66 66 65 72 2c 20 22 62 79 74 65 73 3d 22 2c uffer, "bytes=",
6300: 20 36 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 09 6) == 0) {.....
6310: 77 6f 72 6b 62 75 66 66 65 72 20 2b 3d 20 36 3b workbuffer += 6;
6320: 0a 0a 09 09 09 09 72 61 6e 67 65 5f 72 65 71 75 ......range_requ
6330: 65 73 74 20 3d 20 31 3b 0a 0a 09 09 09 09 72 61 est = 1;......ra
6340: 6e 67 65 5f 73 74 61 72 74 20 3d 20 73 74 72 74 nge_start = strt
6350: 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c oull(workbuffer,
6360: 20 26 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 &workbuffer_nex
6370: 74 2c 20 31 30 29 3b 0a 0a 09 09 09 09 77 6f 72 t, 10);......wor
6380: 6b 62 75 66 66 65 72 20 3d 20 77 6f 72 6b 62 75 kbuffer = workbu
6390: 66 66 65 72 5f 6e 65 78 74 3b 0a 0a 09 09 09 09 ffer_next;......
63a0: 69 66 20 28 2a 77 6f 72 6b 62 75 66 66 65 72 20 if (*workbuffer
63b0: 3d 3d 20 27 2d 27 29 20 7b 0a 09 09 09 09 09 77 == '-') {......w
63c0: 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a 0a 09 09 orkbuffer++;....
63d0: 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 66 66 ...if (*workbuff
63e0: 65 72 20 21 3d 20 27 5c 72 27 20 26 26 20 2a 77 er != '\r' && *w
63f0: 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 27 5c 6e orkbuffer != '\n
6400: 27 29 20 7b 0a 09 09 09 09 09 09 72 61 6e 67 65 ') {.......range
6410: 5f 65 6e 64 20 3d 20 73 74 72 74 6f 75 6c 6c 28 _end = strtoull(
6420: 77 6f 72 6b 62 75 66 66 65 72 2c 20 26 77 6f 72 workbuffer, &wor
6430: 6b 62 75 66 66 65 72 5f 6e 65 78 74 2c 20 31 30 kbuffer_next, 10
6440: 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09 7d 0a );......}.....}.
6450: 09 09 09 7d 0a 09 09 7d 20 65 6c 73 65 20 69 66 ...}...} else if
6460: 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 62 75 (strncasecmp(bu
6470: 66 66 65 72 2c 20 22 48 6f 73 74 3a 20 22 2c 20 ffer, "Host: ",
6480: 35 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 75 5) == 0) {....bu
6490: 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 ffer_st->headers
64a0: 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d 20 .host.present =
64b0: 31 3b 0a 0a 09 09 09 77 6f 72 6b 62 75 66 66 65 1;.....workbuffe
64c0: 72 20 3d 20 73 74 72 70 62 72 6b 28 62 75 66 66 r = strpbrk(buff
64d0: 65 72 20 2b 20 35 2c 20 22 5c 72 5c 6e 3a 22 29 er + 5, "\r\n:")
64e0: 3b 0a 09 09 09 69 66 20 28 77 6f 72 6b 62 75 66 ;....if (workbuf
64f0: 66 65 72 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 fer != NULL) {..
6500: 09 09 09 2a 77 6f 72 6b 62 75 66 66 65 72 20 3d ...*workbuffer =
6510: 20 27 5c 30 27 3b 0a 09 09 09 7d 0a 0a 09 09 09 '\0';....}.....
6520: 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 62 75 66 workbuffer = buf
6530: 66 65 72 20 2b 20 35 3b 0a 09 09 09 77 68 69 6c fer + 5;....whil
6540: 65 20 28 2a 77 6f 72 6b 62 75 66 66 65 72 20 3d e (*workbuffer =
6550: 3d 20 27 20 27 29 20 7b 0a 09 09 09 09 77 6f 72 = ' ') {.....wor
6560: 6b 62 75 66 66 65 72 2b 2b 3b 0a 09 09 09 7d 0a kbuffer++;....}.
6570: 0a 09 09 09 73 74 72 63 70 79 28 62 75 66 66 65 ....strcpy(buffe
6580: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f r_st->headers.ho
6590: 73 74 2e 68 6f 73 74 2c 20 77 6f 72 6b 62 75 66 st.host, workbuf
65a0: 66 65 72 29 3b 0a 09 09 7d 20 65 6c 73 65 20 69 fer);...} else i
65b0: 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 62 f (strncasecmp(b
65c0: 75 66 66 65 72 2c 20 22 43 6f 6e 6e 65 63 74 69 uffer, "Connecti
65d0: 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69 76 65 22 2c on: Keep-Alive",
65e0: 20 32 32 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 22) == 0) {....
65f0: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
6600: 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 rs.connection =
6610: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e FILED_CONNECTION
6620: 5f 4b 45 45 50 5f 41 4c 49 56 45 3b 0a 09 09 7d _KEEP_ALIVE;...}
6630: 0a 0a 09 09 69 66 20 28 6d 65 6d 63 6d 70 28 62 ....if (memcmp(b
6640: 75 66 66 65 72 2c 20 22 5c 72 5c 6e 22 2c 20 32 uffer, "\r\n", 2
6650: 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72 65 ) == 0) {....bre
6660: 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 ak;...}..}.../*
6670: 44 65 74 65 72 6d 69 6e 65 20 72 61 6e 67 65 20 Determine range
6680: 2a 2f 0a 09 69 66 20 28 72 61 6e 67 65 5f 65 6e */..if (range_en
6690: 64 20 21 3d 20 30 29 20 7b 0a 09 09 69 66 20 28 d != 0) {...if (
66a0: 72 61 6e 67 65 5f 65 6e 64 20 3c 3d 20 72 61 6e range_end <= ran
66b0: 67 65 5f 73 74 61 72 74 29 20 7b 0a 09 09 09 72 ge_start) {....r
66c0: 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d eturn(NULL);...}
66d0: 0a 0a 09 09 72 61 6e 67 65 5f 6c 65 6e 67 74 68 ....range_length
66e0: 20 3d 20 72 61 6e 67 65 5f 65 6e 64 20 2d 20 72 = range_end - r
66f0: 61 6e 67 65 5f 73 74 61 72 74 3b 0a 0a 09 09 66 ange_start;....f
6700: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
6710: 75 67 28 22 43 6f 6d 70 75 74 69 6e 67 20 6c 65 ug("Computing le
6720: 6e 67 74 68 20 70 61 72 61 6d 65 74 65 72 3a 20 ngth parameter:
6730: 25 6c 6c 75 20 3d 20 25 6c 6c 75 20 2d 20 25 6c %llu = %llu - %l
6740: 6c 75 22 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 lu",....(unsigne
6750: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e d long long) ran
6760: 67 65 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 28 75 ge_length,....(u
6770: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
6780: 67 29 20 72 61 6e 67 65 5f 65 6e 64 2c 0a 09 09 g) range_end,...
6790: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
67a0: 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 73 74 61 72 long) range_star
67b0: 74 0a 09 09 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 46 t...);..}.../* F
67c0: 69 6c 6c 20 75 70 20 73 74 72 75 63 74 75 72 65 ill up structure
67d0: 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 09 62 to return */..b
67e0: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 uffer_st->header
67f0: 73 2e 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 20 s.range.present
6800: 3d 20 72 61 6e 67 65 5f 72 65 71 75 65 73 74 3b = range_request;
6810: 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 ..buffer_st->hea
6820: 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 ders.range.offse
6830: 74 20 20 3d 20 72 61 6e 67 65 5f 73 74 61 72 74 t = range_start
6840: 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 ;..buffer_st->he
6850: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 aders.range.leng
6860: 74 68 20 20 3d 20 72 61 6e 67 65 5f 6c 65 6e 67 th = range_leng
6870: 74 68 3b 0a 0a 09 2f 2a 20 49 66 20 76 68 6f 73 th;.../* If vhos
6880: 74 73 20 61 72 65 20 65 6e 61 62 6c 65 64 2c 20 ts are enabled,
6890: 63 6f 6d 70 75 74 65 20 6e 65 77 20 70 61 74 68 compute new path
68a0: 20 2a 2f 0a 09 69 66 20 28 6f 70 74 69 6f 6e 73 */..if (options
68b0: 2d 3e 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 64 ->vhosts_enabled
68c0: 29 20 7b 0a 09 09 69 66 20 28 62 75 66 66 65 72 ) {...if (buffer
68d0: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 _st->headers.hos
68e0: 74 2e 70 72 65 73 65 6e 74 20 3d 3d 20 31 29 20 t.present == 1)
68f0: 7b 0a 09 09 09 62 75 66 66 65 72 20 3d 20 62 75 {....buffer = bu
6900: 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b ffer_st->tmpbuf;
6910: 0a 09 09 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d ....buffer_len =
6920: 20 73 69 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 sizeof(buffer_s
6930: 74 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 09 09 t->tmpbuf);.....
6940: 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3d 20 73 snprintf_ret = s
6950: 6e 70 72 69 6e 74 66 28 62 75 66 66 65 72 2c 20 nprintf(buffer,
6960: 62 75 66 66 65 72 5f 6c 65 6e 2c 20 22 2f 25 73 buffer_len, "/%s
6970: 25 73 25 73 22 2c 0a 09 09 09 09 62 75 66 66 65 %s%s",.....buffe
6980: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f r_st->headers.ho
6990: 73 74 2e 68 6f 73 74 2c 0a 09 09 09 09 62 75 66 st.host,.....buf
69a0: 66 65 72 5f 73 74 2d 3e 70 61 74 68 5b 30 5d 20 fer_st->path[0]
69b0: 3d 3d 20 27 2f 27 20 3f 20 22 22 20 3a 20 22 2f == '/' ? "" : "/
69c0: 22 2c 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74 ",.....buffer_st
69d0: 2d 3e 70 61 74 68 0a 09 09 09 29 3b 0a 09 09 09 ->path....);....
69e0: 69 66 20 28 73 6e 70 72 69 6e 74 66 5f 72 65 74 if (snprintf_ret
69f0: 20 3e 3d 20 30 29 20 7b 0a 09 09 09 09 69 66 20 >= 0) {.....if
6a00: 28 28 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 (((unsigned int)
6a10: 20 73 6e 70 72 69 6e 74 66 5f 72 65 74 29 20 3c snprintf_ret) <
6a20: 20 62 75 66 66 65 72 5f 6c 65 6e 29 20 7b 0a 09 buffer_len) {..
6a30: 09 09 09 09 73 74 72 63 70 79 28 62 75 66 66 65 ....strcpy(buffe
6a40: 72 5f 73 74 2d 3e 70 61 74 68 2c 20 62 75 66 66 r_st->path, buff
6a50: 65 72 29 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 0a er);.....}....}.
6a60: 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 ..}..}...return(
6a70: 62 75 66 66 65 72 5f 73 74 29 3b 0a 7d 0a 0a 2f buffer_st);.}../
6a80: 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f * Return an erro
6a90: 72 20 70 61 67 65 20 2a 2f 0a 73 74 61 74 69 63 r page */.static
6aa0: 20 76 6f 69 64 20 66 69 6c 65 64 5f 65 72 72 6f void filed_erro
6ab0: 72 5f 70 61 67 65 28 46 49 4c 45 20 2a 66 70 2c r_page(FILE *fp,
6ac0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 const char *dat
6ad0: 65 5f 63 75 72 72 65 6e 74 2c 20 69 6e 74 20 65 e_current, int e
6ae0: 72 72 6f 72 5f 6e 75 6d 62 65 72 2c 20 69 6e 74 rror_number, int
6af0: 20 6d 65 74 68 6f 64 2c 20 63 6f 6e 73 74 20 63 method, const c
6b00: 68 61 72 20 2a 72 65 61 73 6f 6e 2c 20 73 74 72 har *reason, str
6b10: 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e uct filed_log_en
6b20: 74 72 79 20 2a 6c 6f 67 29 20 7b 0a 09 63 68 61 try *log) {..cha
6b30: 72 20 2a 65 72 72 6f 72 5f 73 74 72 69 6e 67 20 r *error_string
6b40: 3d 20 22 3c 68 74 6d 6c 3e 3c 68 65 61 64 3e 3c = "<html><head><
6b50: 74 69 74 6c 65 3e 45 52 52 4f 52 3c 2f 74 69 74 title>ERROR</tit
6b60: 6c 65 3e 3c 2f 68 65 61 64 3e 3c 62 6f 64 79 3e le></head><body>
6b70: 55 6e 61 62 6c 65 20 74 6f 20 70 72 6f 63 65 73 Unable to proces
6b80: 73 20 72 65 71 75 65 73 74 3c 2f 62 6f 64 79 3e s request</body>
6b90: 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a 09 66 70 72 69 </html>";...fpri
6ba0: 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e ntf(fp, "HTTP/1.
6bb0: 31 20 25 69 20 4e 6f 74 20 4f 4b 5c 72 5c 6e 44 1 %i Not OK\r\nD
6bc0: 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 ate: %s\r\nServe
6bd0: 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 r: filed\r\nLast
6be0: 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 5c -Modified: %s\r\
6bf0: 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a nContent-Length:
6c00: 20 25 6c 6c 75 5c 72 5c 6e 43 6f 6e 74 65 6e 74 %llu\r\nContent
6c10: 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e -Type: %s\r\nCon
6c20: 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c 72 nection: close\r
6c30: 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 65 72 72 6f 72 \n\r\n",...error
6c40: 5f 6e 75 6d 62 65 72 2c 0a 09 09 64 61 74 65 5f _number,...date_
6c50: 63 75 72 72 65 6e 74 2c 0a 09 09 64 61 74 65 5f current,...date_
6c60: 63 75 72 72 65 6e 74 2c 0a 09 09 28 75 6e 73 69 current,...(unsi
6c70: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
6c80: 73 74 72 6c 65 6e 28 65 72 72 6f 72 5f 73 74 72 strlen(error_str
6c90: 69 6e 67 29 2c 0a 09 09 22 74 65 78 74 2f 68 74 ing),..."text/ht
6ca0: 6d 6c 22 0a 09 29 3b 0a 0a 09 2f 2a 20 73 69 6c ml"..);.../* sil
6cb0: 65 6e 63 65 20 65 72 72 6f 72 20 73 74 72 69 6e ence error strin
6cc0: 67 20 66 6f 72 20 48 45 41 44 20 72 65 71 75 65 g for HEAD reque
6cd0: 73 74 73 20 2a 2f 0a 09 69 66 20 28 6d 65 74 68 sts */..if (meth
6ce0: 6f 64 20 21 3d 20 46 49 4c 45 44 5f 52 45 51 55 od != FILED_REQU
6cf0: 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 29 EST_METHOD_HEAD)
6d00: 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 66 70 2c {...fprintf(fp,
6d10: 20 22 25 73 22 2c 20 65 72 72 6f 72 5f 73 74 72 "%s", error_str
6d20: 69 6e 67 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 4c 6f ing);..}.../* Lo
6d30: 67 20 65 72 72 6f 72 20 2a 2f 0a 09 2f 2a 2a 20 g error */../**
6d40: 72 65 61 73 6f 6e 20 6d 75 73 74 20 70 6f 69 6e reason must poin
6d50: 74 20 74 6f 20 61 20 67 6c 6f 62 61 6c 6c 79 20 t to a globally
6d60: 61 6c 6c 6f 63 61 74 65 64 20 76 61 6c 75 65 20 allocated value
6d70: 2a 2a 2f 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e **/..log->reason
6d80: 20 3d 20 72 65 61 73 6f 6e 3b 0a 09 6c 6f 67 2d = reason;..log-
6d90: 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 65 72 72 >http_code = err
6da0: 6f 72 5f 6e 75 6d 62 65 72 3b 0a 0a 09 66 69 6c or_number;...fil
6db0: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 ed_log_entry(log
6dc0: 29 3b 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20 63 6f );.../* Close co
6dd0: 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 66 69 6c nnection */..fil
6de0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
6df0: 5f 63 6c 6f 73 65 28 66 69 6c 65 6e 6f 28 66 70 _close(fileno(fp
6e00: 29 29 3b 0a 0a 09 66 63 6c 6f 73 65 28 66 70 29 ));...fclose(fp)
6e10: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f ;...return;.}../
6e20: 2a 20 52 65 74 75 72 6e 20 61 20 72 65 64 69 72 * Return a redir
6e30: 65 63 74 20 74 6f 20 69 6e 64 65 78 2e 68 74 6d ect to index.htm
6e40: 6c 20 2a 2f 0a 23 69 66 6e 64 65 66 20 46 49 4c l */.#ifndef FIL
6e50: 45 44 5f 44 4f 4e 54 5f 52 45 44 49 52 45 43 54 ED_DONT_REDIRECT
6e60: 5f 44 49 52 45 43 54 4f 52 49 45 53 0a 73 74 61 _DIRECTORIES.sta
6e70: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 72 tic void filed_r
6e80: 65 64 69 72 65 63 74 5f 69 6e 64 65 78 28 46 49 edirect_index(FI
6e90: 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68 LE *fp, const ch
6ea0: 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 ar *date_current
6eb0: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 , const char *pa
6ec0: 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 th, struct filed
6ed0: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 29 _log_entry *log)
6ee0: 20 7b 0a 09 69 6e 74 20 68 74 74 70 5f 63 6f 64 {..int http_cod
6ef0: 65 20 3d 20 33 30 32 3b 0a 09 66 70 72 69 6e 74 e = 302;..fprint
6f00: 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 f(fp, "HTTP/1.1
6f10: 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 %i OK\r\nDate: %
6f20: 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c s\r\nServer: fil
6f30: 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 ed\r\nLast-Modif
6f40: 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 ied: %s\r\nConte
6f50: 6e 74 2d 4c 65 6e 67 74 68 3a 20 30 5c 72 5c 6e nt-Length: 0\r\n
6f60: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 Connection: clos
6f70: 65 5c 72 5c 6e 4c 6f 63 61 74 69 6f 6e 3a 20 25 e\r\nLocation: %
6f80: 73 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 68 74 s\r\n\r\n",...ht
6f90: 74 70 5f 63 6f 64 65 2c 0a 09 09 64 61 74 65 5f tp_code,...date_
6fa0: 63 75 72 72 65 6e 74 2c 0a 09 09 64 61 74 65 5f current,...date_
6fb0: 63 75 72 72 65 6e 74 2c 0a 09 09 22 69 6e 64 65 current,..."inde
6fc0: 78 2e 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f 2a x.html"..);.../*
6fd0: 20 4c 6f 67 20 72 65 64 69 72 65 63 74 20 2a 2f Log redirect */
6fe0: 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 ..log->reason =
6ff0: 22 72 65 64 69 72 65 63 74 22 3b 0a 09 6c 6f 67 "redirect";..log
7000: 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 68 74 ->http_code = ht
7010: 74 70 5f 63 6f 64 65 3b 0a 0a 09 66 69 6c 65 64 tp_code;...filed
7020: 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b _log_entry(log);
7030: 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20 63 6f 6e 6e .../* Close conn
7040: 65 63 74 69 6f 6e 20 2a 2f 0a 09 66 69 6c 65 64 ection */..filed
7050: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 _sockettimeout_c
7060: 6c 6f 73 65 28 66 69 6c 65 6e 6f 28 66 70 29 29 lose(fileno(fp))
7070: 3b 0a 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a ;...fclose(fp);.
7080: 0a 09 72 65 74 75 72 6e 3b 0a 0a 09 2f 2a 20 43 ..return;.../* C
7090: 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 3a urrently unused:
70a0: 20 70 61 74 68 20 2a 2f 0a 09 70 61 74 68 20 3d path */..path =
70b0: 20 70 61 74 68 3b 0a 7d 0a 23 65 6e 64 69 66 0a path;.}.#endif.
70c0: 0a 2f 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 65 ./* Convert an e
70d0: 6e 75 6d 20 72 65 70 72 65 73 65 6e 74 69 6e 67 num representing
70e0: 20 74 68 65 20 22 43 6f 6e 6e 65 63 74 69 6f 6e the "Connection
70f0: 22 20 68 65 61 64 65 72 20 76 61 6c 75 65 20 74 " header value t
7100: 6f 20 61 20 73 74 72 69 6e 67 20 2a 2f 0a 73 74 o a string */.st
7110: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
7120: 2a 66 69 6c 65 64 5f 63 6f 6e 6e 65 63 74 69 6f *filed_connectio
7130: 6e 5f 73 74 72 28 69 6e 74 20 63 6f 6e 6e 65 63 n_str(int connec
7140: 74 69 6f 6e 5f 76 61 6c 75 65 29 20 7b 0a 09 73 tion_value) {..s
7150: 77 69 74 63 68 20 28 63 6f 6e 6e 65 63 74 69 6f witch (connectio
7160: 6e 5f 76 61 6c 75 65 29 20 7b 0a 09 09 63 61 73 n_value) {...cas
7170: 65 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 e FILED_CONNECTI
7180: 4f 4e 5f 43 4c 4f 53 45 3a 0a 09 09 09 72 65 74 ON_CLOSE:....ret
7190: 75 72 6e 28 22 63 6c 6f 73 65 22 29 3b 0a 09 09 urn("close");...
71a0: 63 61 73 65 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 case FILED_CONNE
71b0: 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 CTION_KEEP_ALIVE
71c0: 3a 0a 09 09 09 72 65 74 75 72 6e 28 22 6b 65 65 :....return("kee
71d0: 70 2d 61 6c 69 76 65 22 29 3b 0a 09 7d 0a 0a 09 p-alive");..}...
71e0: 72 65 74 75 72 6e 28 22 63 6c 6f 73 65 22 29 3b return("close");
71f0: 0a 7d 0a 0a 2f 2a 20 48 61 6e 64 6c 65 20 61 20 .}../* Handle a
7200: 73 69 6e 67 6c 65 20 72 65 71 75 65 73 74 20 66 single request f
7210: 72 6f 6d 20 61 20 63 6c 69 65 6e 74 20 2a 2f 0a rom a client */.
7220: 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 static int filed
7230: 5f 68 61 6e 64 6c 65 5f 63 6c 69 65 6e 74 28 69 _handle_client(i
7240: 6e 74 20 66 64 2c 20 73 74 72 75 63 74 20 66 69 nt fd, struct fi
7250: 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 74 led_http_request
7260: 20 2a 72 65 71 75 65 73 74 2c 20 73 74 72 75 63 *request, struc
7270: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
7280: 79 20 2a 6c 6f 67 2c 20 73 74 72 75 63 74 20 66 y *log, struct f
7290: 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 iled_options *op
72a0: 74 69 6f 6e 73 29 20 7b 0a 09 73 74 72 75 63 74 tions) {..struct
72b0: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 filed_fileinfo
72c0: 2a 66 69 6c 65 69 6e 66 6f 3b 0a 09 73 73 69 7a *fileinfo;..ssiz
72d0: 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 e_t sendfile_ret
72e0: 3b 0a 09 73 69 7a 65 5f 74 20 73 65 6e 64 66 69 ;..size_t sendfi
72f0: 6c 65 5f 73 69 7a 65 3b 0a 09 6f 66 66 5f 74 20 le_size;..off_t
7300: 73 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 2c sendfile_offset,
7310: 20 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 2c 20 sendfile_sent,
7320: 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 3b 0a 09 63 sendfile_len;..c
7330: 68 61 72 20 2a 70 61 74 68 3b 0a 09 63 68 61 72 har *path;..char
7340: 20 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 *date_current,
7350: 64 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 5b 36 date_current_b[6
7360: 34 5d 3b 0a 09 69 6e 74 20 68 74 74 70 5f 63 6f 4];..int http_co
7370: 64 65 3b 0a 09 46 49 4c 45 20 2a 66 70 3b 0a 0a de;..FILE *fp;..
7380: 09 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 63 75 ./* Determine cu
7390: 72 72 65 6e 74 20 74 69 6d 65 20 2a 2f 0a 09 64 rrent time */..d
73a0: 61 74 65 5f 63 75 72 72 65 6e 74 20 3d 20 66 69 ate_current = fi
73b0: 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 led_format_time(
73c0: 64 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 2c 20 date_current_b,
73d0: 73 69 7a 65 6f 66 28 64 61 74 65 5f 63 75 72 72 sizeof(date_curr
73e0: 65 6e 74 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c ent_b), time(NUL
73f0: 4c 29 29 3b 0a 0a 09 2f 2a 20 4f 70 65 6e 20 73 L));.../* Open s
7400: 6f 63 6b 65 74 20 61 73 20 41 4e 53 49 20 49 2f ocket as ANSI I/
7410: 4f 20 66 6f 72 20 65 61 73 65 20 6f 66 20 75 73 O for ease of us
7420: 65 20 2a 2f 0a 09 66 70 20 3d 20 66 64 6f 70 65 e */..fp = fdope
7430: 6e 28 66 64 2c 20 22 77 2b 62 22 29 3b 0a 09 69 n(fd, "w+b");..i
7440: 66 20 28 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b f (fp == NULL) {
7450: 0a 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ...filed_sockett
7460: 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 64 29 imeout_close(fd)
7470: 3b 0a 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a ;....close(fd);.
7480: 0a 09 09 6c 6f 67 2d 3e 62 75 66 66 65 72 5b 30 ...log->buffer[0
7490: 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 6c 6f 67 2d ] = '\0';...log-
74a0: 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 2d 31 3b >http_code = -1;
74b0: 0a 09 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d ...log->reason =
74c0: 20 22 66 64 6f 70 65 6e 5f 66 61 69 6c 65 64 22 "fdopen_failed"
74d0: 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 ;....filed_log_e
74e0: 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 09 72 65 ntry(log);....re
74f0: 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 turn(FILED_CONNE
7500: 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d CTION_CLOSE);..}
7510: 0a 0a 09 72 65 71 75 65 73 74 20 3d 20 66 69 6c ...request = fil
7520: 65 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71 75 ed_get_http_requ
7530: 65 73 74 28 66 70 2c 20 72 65 71 75 65 73 74 2c est(fp, request,
7540: 20 6f 70 74 69 6f 6e 73 29 3b 0a 0a 09 69 66 20 options);...if
7550: 28 72 65 71 75 65 73 74 20 3d 3d 20 4e 55 4c 4c (request == NULL
7560: 29 20 7b 0a 09 09 6c 6f 67 2d 3e 62 75 66 66 65 ) {...log->buffe
7570: 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 09 09 r[0] = '\0';....
7580: 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 filed_error_page
7590: 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e (fp, date_curren
75a0: 74 2c 20 35 30 30 2c 20 46 49 4c 45 44 5f 52 45 t, 500, FILED_RE
75b0: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 QUEST_METHOD_GET
75c0: 2c 20 22 66 6f 72 6d 61 74 22 2c 20 6c 6f 67 29 , "format", log)
75d0: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 ;....return(FILE
75e0: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f D_CONNECTION_CLO
75f0: 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f SE);..}...filed_
7600: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 sockettimeout_pr
7610: 6f 63 65 73 73 69 6e 67 5f 73 74 61 72 74 28 66 ocessing_start(f
7620: 64 29 3b 0a 0a 09 70 61 74 68 20 3d 20 72 65 71 d);...path = req
7630: 75 65 73 74 2d 3e 70 61 74 68 3b 0a 09 73 74 72 uest->path;..str
7640: 63 70 79 28 6c 6f 67 2d 3e 62 75 66 66 65 72 2c cpy(log->buffer,
7650: 20 70 61 74 68 29 3b 0a 09 6c 6f 67 2d 3e 6d 65 path);..log->me
7660: 74 68 6f 64 20 3d 20 72 65 71 75 65 73 74 2d 3e thod = request->
7670: 6d 65 74 68 6f 64 3b 0a 0a 09 2f 2a 20 49 66 20 method;.../* If
7680: 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
7690: 74 68 20 69 73 20 61 20 64 69 72 65 63 74 6f 72 th is a director
76a0: 79 2c 20 72 65 64 69 72 65 63 74 20 74 6f 20 69 y, redirect to i
76b0: 6e 64 65 78 20 70 61 67 65 20 2a 2f 0a 09 69 66 ndex page */..if
76c0: 20 28 72 65 71 75 65 73 74 2d 3e 74 79 70 65 20 (request->type
76d0: 3d 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 == FILED_REQUEST
76e0: 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 59 29 _TYPE_DIRECTORY)
76f0: 20 7b 0a 23 69 66 64 65 66 20 46 49 4c 45 44 5f {.#ifdef FILED_
7700: 44 4f 4e 54 5f 52 45 44 49 52 45 43 54 5f 44 49 DONT_REDIRECT_DI
7710: 52 45 43 54 4f 52 49 45 53 0a 09 09 63 68 61 72 RECTORIES...char
7720: 20 6c 6f 63 61 6c 70 61 74 68 5b 38 31 39 32 5d localpath[8192]
7730: 3b 0a 09 09 69 6e 74 20 73 6e 70 72 69 6e 74 66 ;...int snprintf
7740: 5f 72 65 74 3b 0a 0a 09 09 73 6e 70 72 69 6e 74 _ret;....snprint
7750: 66 5f 72 65 74 20 3d 20 73 6e 70 72 69 6e 74 66 f_ret = snprintf
7760: 28 6c 6f 63 61 6c 70 61 74 68 2c 20 73 69 7a 65 (localpath, size
7770: 6f 66 28 6c 6f 63 61 6c 70 61 74 68 29 2c 20 22 of(localpath), "
7780: 25 73 2f 69 6e 64 65 78 2e 68 74 6d 6c 22 2c 20 %s/index.html",
7790: 70 61 74 68 29 3b 0a 0a 09 09 69 66 20 28 73 6e path);....if (sn
77a0: 70 72 69 6e 74 66 5f 72 65 74 20 3c 3d 20 30 20 printf_ret <= 0
77b0: 7c 7c 20 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 || snprintf_ret
77c0: 3e 20 28 73 69 7a 65 6f 66 28 6c 6f 63 61 6c 70 > (sizeof(localp
77d0: 61 74 68 29 20 2d 20 31 29 29 20 7b 0a 09 09 09 ath) - 1)) {....
77e0: 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 filed_error_page
77f0: 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e (fp, date_curren
7800: 74 2c 20 35 30 30 2c 20 72 65 71 75 65 73 74 2d t, 500, request-
7810: 3e 6d 65 74 68 6f 64 2c 20 22 70 61 74 68 5f 66 >method, "path_f
7820: 6f 72 6d 61 74 22 2c 20 6c 6f 67 29 3b 0a 0a 09 ormat", log);...
7830: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 ..return(FILED_C
7840: 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 ONNECTION_CLOSE)
7850: 3b 0a 09 09 7d 0a 0a 09 09 70 61 74 68 20 3d 20 ;...}....path =
7860: 6c 6f 63 61 6c 70 61 74 68 3b 0a 23 65 6c 73 65 localpath;.#else
7870: 0a 09 09 66 69 6c 65 64 5f 72 65 64 69 72 65 63 ...filed_redirec
7880: 74 5f 69 6e 64 65 78 28 66 70 2c 20 64 61 74 65 t_index(fp, date
7890: 5f 63 75 72 72 65 6e 74 2c 20 70 61 74 68 2c 20 _current, path,
78a0: 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 log);....return(
78b0: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e FILED_CONNECTION
78c0: 5f 43 4c 4f 53 45 29 3b 0a 23 65 6e 64 69 66 0a _CLOSE);.#endif.
78d0: 09 7d 0a 0a 09 66 69 6c 65 69 6e 66 6f 20 3d 20 .}...fileinfo =
78e0: 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c 65 28 filed_open_file(
78f0: 70 61 74 68 2c 20 26 72 65 71 75 65 73 74 2d 3e path, &request->
7900: 66 69 6c 65 69 6e 66 6f 2c 20 6f 70 74 69 6f 6e fileinfo, option
7910: 73 29 3b 0a 09 69 66 20 28 66 69 6c 65 69 6e 66 s);..if (fileinf
7920: 6f 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 o == NULL) {...f
7930: 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 iled_error_page(
7940: 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 fp, date_current
7950: 2c 20 34 30 34 2c 20 72 65 71 75 65 73 74 2d 3e , 404, request->
7960: 6d 65 74 68 6f 64 2c 20 22 6f 70 65 6e 5f 66 61 method, "open_fa
7970: 69 6c 65 64 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 iled", log);....
7980: 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e return(FILED_CON
7990: 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a NECTION_CLOSE);.
79a0: 09 7d 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74 .}...if (request
79b0: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
79c0: 70 72 65 73 65 6e 74 29 20 7b 0a 09 09 69 66 20 present) {...if
79d0: 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 (request->header
79e0: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 21 s.range.offset !
79f0: 3d 20 30 20 7c 7c 20 72 65 71 75 65 73 74 2d 3e = 0 || request->
7a00: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 headers.range.le
7a10: 6e 67 74 68 20 3e 3d 20 30 29 20 7b 0a 09 09 09 ngth >= 0) {....
7a20: 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 if (request->hea
7a30: 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 ders.range.offse
7a40: 74 20 3e 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c t >= fileinfo->l
7a50: 65 6e 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f en) {.....filed_
7a60: 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 error_page(fp, d
7a70: 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 31 36 ate_current, 416
7a80: 2c 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f , request->metho
7a90: 64 2c 20 22 72 61 6e 67 65 5f 69 6e 76 61 6c 69 d, "range_invali
7aa0: 64 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 09 09 63 d", log);......c
7ab0: 6c 6f 73 65 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 lose(fileinfo->f
7ac0: 64 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 d);......return(
7ad0: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e FILED_CONNECTION
7ae0: 5f 43 4c 4f 53 45 29 3b 0a 09 09 09 7d 0a 0a 09 _CLOSE);....}...
7af0: 09 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 ..if (request->h
7b00: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e eaders.range.len
7b10: 67 74 68 20 3d 3d 20 28 28 6f 66 66 5f 74 29 20 gth == ((off_t)
7b20: 2d 31 29 29 20 7b 0a 09 09 09 09 66 69 6c 65 64 -1)) {.....filed
7b30: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 _log_msg_debug("
7b40: 43 6f 6d 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 Computing length
7b50: 20 74 6f 20 66 69 74 20 69 6e 20 62 6f 75 6e 64 to fit in bound
7b60: 73 3a 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e s: fileinfo->len
7b70: 20 3d 20 25 6c 6c 75 2c 20 72 65 71 75 65 73 74 = %llu, request
7b80: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
7b90: 6f 66 66 73 65 74 20 3d 20 25 6c 6c 75 22 2c 0a offset = %llu",.
7ba0: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
7bb0: 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 6e ong long) filein
7bc0: 66 6f 2d 3e 6c 65 6e 2c 0a 09 09 09 09 09 28 75 fo->len,......(u
7bd0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
7be0: 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 g) request->head
7bf0: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 ers.range.offset
7c00: 0a 09 09 09 09 29 3b 0a 0a 09 09 09 09 72 65 71 .....);......req
7c10: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
7c20: 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20 66 69 6c nge.length = fil
7c30: 65 69 6e 66 6f 2d 3e 6c 65 6e 20 2d 20 72 65 71 einfo->len - req
7c40: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
7c50: 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 09 09 7d nge.offset;....}
7c60: 0a 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d .....filed_log_m
7c70: 73 67 5f 64 65 62 75 67 28 22 50 61 72 74 69 61 sg_debug("Partia
7c80: 6c 20 72 65 71 75 65 73 74 2c 20 73 74 61 72 74 l request, start
7c90: 69 6e 67 20 61 74 3a 20 25 6c 6c 75 20 61 6e 64 ing at: %llu and
7ca0: 20 72 75 6e 6e 69 6e 67 20 66 6f 72 20 25 6c 6c running for %ll
7cb0: 69 20 62 79 74 65 73 22 2c 0a 09 09 09 09 28 75 i bytes",.....(u
7cc0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
7cd0: 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 g) request->head
7ce0: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 ers.range.offset
7cf0: 2c 0a 09 09 09 09 28 6c 6f 6e 67 20 6c 6f 6e 67 ,.....(long long
7d00: 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 ) request->heade
7d10: 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 0a rs.range.length.
7d20: 09 09 09 29 3b 0a 0a 09 09 7d 0a 0a 09 09 68 74 ...);....}....ht
7d30: 74 70 5f 63 6f 64 65 20 3d 20 32 30 36 3b 0a 09 tp_code = 206;..
7d40: 7d 20 65 6c 73 65 20 7b 0a 09 09 68 74 74 70 5f } else {...http_
7d50: 63 6f 64 65 20 3d 20 32 30 30 3b 0a 0a 09 09 2f code = 200;..../
7d60: 2a 20 43 6f 6d 70 75 74 65 20 66 61 6b 65 20 72 * Compute fake r
7d70: 61 6e 67 65 20 70 61 72 61 6d 65 74 65 72 73 20 ange parameters
7d80: 74 68 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 that includes th
7d90: 65 20 65 6e 74 69 72 65 20 66 69 6c 65 20 2a 2f e entire file */
7da0: 0a 09 09 72 65 71 75 65 73 74 2d 3e 68 65 61 64 ...request->head
7db0: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 ers.range.offset
7dc0: 20 3d 20 30 3b 0a 09 09 72 65 71 75 65 73 74 2d = 0;...request-
7dd0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c >headers.range.l
7de0: 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f ength = fileinfo
7df0: 2d 3e 6c 65 6e 3b 0a 09 7d 0a 0a 09 66 70 72 69 ->len;..}...fpri
7e00: 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e ntf(fp, "HTTP/1.
7e10: 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 1 %i OK\r\nDate:
7e20: 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 %s\r\nServer: f
7e30: 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 iled\r\nLast-Mod
7e40: 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e ified: %s\r\nCon
7e50: 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c tent-Length: %ll
7e60: 75 5c 72 5c 6e 41 63 63 65 70 74 2d 52 61 6e 67 u\r\nAccept-Rang
7e70: 65 73 3a 20 62 79 74 65 73 5c 72 5c 6e 43 6f 6e es: bytes\r\nCon
7e80: 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 5c tent-Type: %s\r\
7e90: 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 25 73 5c nConnection: %s\
7ea0: 72 5c 6e 45 54 61 67 3a 20 5c 22 25 73 5c 22 5c r\nETag: \"%s\"\
7eb0: 72 5c 6e 22 2c 0a 09 09 68 74 74 70 5f 63 6f 64 r\n",...http_cod
7ec0: 65 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e e,...date_curren
7ed0: 74 2c 0a 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 6c t,...fileinfo->l
7ee0: 61 73 74 6d 6f 64 2c 0a 09 09 28 75 6e 73 69 67 astmod,...(unsig
7ef0: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 ned long long) r
7f00: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
7f10: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 2c 0a 09 09 range.length,...
7f20: 66 69 6c 65 69 6e 66 6f 2d 3e 74 79 70 65 2c 0a fileinfo->type,.
7f30: 09 09 66 69 6c 65 64 5f 63 6f 6e 6e 65 63 74 69 ..filed_connecti
7f40: 6f 6e 5f 73 74 72 28 72 65 71 75 65 73 74 2d 3e on_str(request->
7f50: 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 headers.connecti
7f60: 6f 6e 29 2c 0a 09 09 66 69 6c 65 69 6e 66 6f 2d on),...fileinfo-
7f70: 3e 65 74 61 67 0a 09 29 3b 0a 0a 09 69 66 20 28 >etag..);...if (
7f80: 68 74 74 70 5f 63 6f 64 65 20 3d 3d 20 32 30 36 http_code == 206
7f90: 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 66 70 ) {...fprintf(fp
7fa0: 2c 20 22 43 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 , "Content-Range
7fb0: 3a 20 62 79 74 65 73 20 25 6c 6c 75 2d 25 6c 6c : bytes %llu-%ll
7fc0: 75 2f 25 6c 6c 75 5c 72 5c 6e 22 2c 0a 09 09 09 u/%llu\r\n",....
7fd0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
7fe0: 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 ong) request->he
7ff0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
8000: 65 74 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 et,....(unsigned
8010: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 28 72 65 71 long long) (req
8020: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
8030: 6e 67 65 2e 6f 66 66 73 65 74 20 2b 20 72 65 71 nge.offset + req
8040: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
8050: 6e 67 65 2e 6c 65 6e 67 74 68 20 2d 20 31 29 2c nge.length - 1),
8060: 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f ....(unsigned lo
8070: 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 6e 66 ng long) fileinf
8080: 6f 2d 3e 6c 65 6e 0a 09 09 29 3b 0a 09 7d 0a 09 o->len...);..}..
8090: 66 70 72 69 6e 74 66 28 66 70 2c 20 22 5c 72 5c fprintf(fp, "\r\
80a0: 6e 22 29 3b 0a 09 66 66 6c 75 73 68 28 66 70 29 n");..fflush(fp)
80b0: 3b 0a 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f ;...log->http_co
80c0: 64 65 20 3d 20 68 74 74 70 5f 63 6f 64 65 3b 0a de = http_code;.
80d0: 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 .log->reason = "
80e0: 4f 4b 22 3b 0a 09 6c 6f 67 2d 3e 73 74 61 72 74 OK";..log->start
80f0: 74 69 6d 65 20 3d 20 74 69 6d 65 28 4e 55 4c 4c time = time(NULL
8100: 29 3b 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6f 66 66 );..log->req_off
8110: 73 65 74 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 set = request->h
8120: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
8130: 73 65 74 3b 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6c set;..log->req_l
8140: 65 6e 67 74 68 20 3d 20 72 65 71 75 65 73 74 2d ength = request-
8150: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c >headers.range.l
8160: 65 6e 67 74 68 3b 0a 09 6c 6f 67 2d 3e 66 69 6c ength;..log->fil
8170: 65 5f 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 e_length = filei
8180: 6e 66 6f 2d 3e 6c 65 6e 3b 0a 0a 23 69 66 64 65 nfo->len;..#ifde
8190: 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b f FILED_NONBLOCK
81a0: 5f 48 54 54 50 0a 09 69 6e 74 20 73 6f 63 6b 65 _HTTP..int socke
81b0: 74 5f 66 6c 61 67 73 3b 0a 09 66 64 5f 73 65 74 t_flags;..fd_set
81c0: 20 72 66 64 2c 20 77 66 64 3b 0a 09 63 68 61 72 rfd, wfd;..char
81d0: 20 73 69 6e 6b 62 75 66 5b 38 31 39 32 5d 3b 0a sinkbuf[8192];.
81e0: 09 73 73 69 7a 65 5f 74 20 72 65 61 64 5f 72 65 .ssize_t read_re
81f0: 74 3b 0a 0a 09 46 44 5f 5a 45 52 4f 28 26 72 66 t;...FD_ZERO(&rf
8200: 64 29 3b 0a 09 46 44 5f 5a 45 52 4f 28 26 77 66 d);..FD_ZERO(&wf
8210: 64 29 3b 0a 09 46 44 5f 53 45 54 28 66 64 2c 20 d);..FD_SET(fd,
8220: 26 72 66 64 29 3b 0a 09 46 44 5f 53 45 54 28 66 &rfd);..FD_SET(f
8230: 64 2c 20 26 77 66 64 29 3b 0a 0a 09 73 6f 63 6b d, &wfd);...sock
8240: 65 74 5f 66 6c 61 67 73 20 3d 20 66 63 6e 74 6c et_flags = fcntl
8250: 28 66 64 2c 20 46 5f 47 45 54 46 4c 29 3b 0a 09 (fd, F_GETFL);..
8260: 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 fcntl(fd, F_SETF
8270: 4c 2c 20 73 6f 63 6b 65 74 5f 66 6c 61 67 73 20 L, socket_flags
8280: 7c 20 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 3b 0a 23 | O_NONBLOCK);.#
8290: 65 6e 64 69 66 0a 09 73 65 6e 64 66 69 6c 65 5f endif..sendfile_
82a0: 6f 66 66 73 65 74 20 3d 20 72 65 71 75 65 73 74 offset = request
82b0: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
82c0: 6f 66 66 73 65 74 3b 0a 09 73 65 6e 64 66 69 6c offset;..sendfil
82d0: 65 5f 6c 65 6e 20 3d 20 72 65 71 75 65 73 74 2d e_len = request-
82e0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c >headers.range.l
82f0: 65 6e 67 74 68 3b 0a 09 73 65 6e 64 66 69 6c 65 ength;..sendfile
8300: 5f 73 65 6e 74 20 3d 20 30 3b 0a 09 77 68 69 6c _sent = 0;..whil
8310: 65 20 28 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 e (request->meth
8320: 6f 64 20 3d 3d 20 46 49 4c 45 44 5f 52 45 51 55 od == FILED_REQU
8330: 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 29 20 EST_METHOD_GET)
8340: 7b 0a 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65 {...if (sendfile
8350: 5f 6c 65 6e 20 3e 20 46 49 4c 45 44 5f 53 45 4e _len > FILED_SEN
8360: 44 46 49 4c 45 5f 4d 41 58 29 20 7b 0a 09 09 09 DFILE_MAX) {....
8370: 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 sendfile_size =
8380: 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d FILED_SENDFILE_M
8390: 41 58 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 AX;...} else {..
83a0: 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 20 ..sendfile_size
83b0: 3d 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 3b 0a = sendfile_len;.
83c0: 09 09 7d 0a 0a 09 09 73 65 6e 64 66 69 6c 65 5f ..}....sendfile_
83d0: 72 65 74 20 3d 20 73 65 6e 64 66 69 6c 65 28 66 ret = sendfile(f
83e0: 64 2c 20 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 2c d, fileinfo->fd,
83f0: 20 26 73 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 &sendfile_offse
8400: 74 2c 20 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 t, sendfile_size
8410: 29 3b 0a 09 09 69 66 20 28 73 65 6e 64 66 69 6c );...if (sendfil
8420: 65 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 23 69 e_ret <= 0) {.#i
8430: 66 64 65 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c fdef FILED_NONBL
8440: 4f 43 4b 5f 48 54 54 50 0a 09 09 09 69 66 20 28 OCK_HTTP....if (
8450: 65 72 72 6e 6f 20 3d 3d 20 45 41 47 41 49 4e 29 errno == EAGAIN)
8460: 20 7b 0a 09 09 09 09 73 65 6e 64 66 69 6c 65 5f {.....sendfile_
8470: 72 65 74 20 3d 20 30 3b 0a 0a 09 09 09 09 77 68 ret = 0;......wh
8480: 69 6c 65 20 28 31 29 20 7b 0a 09 09 09 09 09 73 ile (1) {......s
8490: 65 6c 65 63 74 28 66 64 20 2b 20 31 2c 20 26 72 elect(fd + 1, &r
84a0: 66 64 2c 20 26 77 66 64 2c 20 4e 55 4c 4c 2c 20 fd, &wfd, NULL,
84b0: 4e 55 4c 4c 29 3b 0a 09 09 09 09 09 69 66 20 28 NULL);......if (
84c0: 46 44 5f 49 53 53 45 54 28 66 64 2c 20 26 72 66 FD_ISSET(fd, &rf
84d0: 64 29 29 20 7b 0a 09 09 09 09 09 09 72 65 61 64 d)) {.......read
84e0: 5f 72 65 74 20 3d 20 72 65 61 64 28 66 64 2c 20 _ret = read(fd,
84f0: 73 69 6e 6b 62 75 66 2c 20 73 69 7a 65 6f 66 28 sinkbuf, sizeof(
8500: 73 69 6e 6b 62 75 66 29 29 3b 0a 0a 09 09 09 09 sinkbuf));......
8510: 09 09 69 66 20 28 72 65 61 64 5f 72 65 74 20 3c ..if (read_ret <
8520: 3d 20 30 29 20 7b 0a 09 09 09 09 09 09 09 62 72 = 0) {........br
8530: 65 61 6b 3b 0a 09 09 09 09 09 09 7d 0a 09 09 09 eak;.......}....
8540: 09 09 7d 0a 0a 09 09 09 09 09 69 66 20 28 46 44 ..}.......if (FD
8550: 5f 49 53 53 45 54 28 66 64 2c 20 26 77 66 64 29 _ISSET(fd, &wfd)
8560: 29 20 7b 0a 09 09 09 09 09 09 72 65 61 64 5f 72 ) {.......read_r
8570: 65 74 20 3d 20 31 3b 0a 0a 09 09 09 09 09 09 62 et = 1;........b
8580: 72 65 61 6b 3b 0a 09 09 09 09 09 7d 0a 09 09 09 reak;......}....
8590: 09 7d 0a 0a 09 09 09 09 69 66 20 28 72 65 61 64 .}......if (read
85a0: 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 09 09 _ret <= 0) {....
85b0: 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 7d 0a 09 ..break;.....}..
85c0: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 62 ..} else {.....b
85d0: 72 65 61 6b 3b 0a 09 09 09 7d 0a 23 65 6c 73 65 reak;....}.#else
85e0: 0a 09 09 09 62 72 65 61 6b 3b 0a 23 65 6e 64 69 ....break;.#endi
85f0: 66 0a 09 09 7d 0a 0a 09 09 73 65 6e 64 66 69 6c f...}....sendfil
8600: 65 5f 6c 65 6e 20 2d 3d 20 73 65 6e 64 66 69 6c e_len -= sendfil
8610: 65 5f 72 65 74 3b 0a 09 09 73 65 6e 64 66 69 6c e_ret;...sendfil
8620: 65 5f 73 65 6e 74 20 2b 3d 20 73 65 6e 64 66 69 e_sent += sendfi
8630: 6c 65 5f 72 65 74 3b 0a 09 09 69 66 20 28 73 65 le_ret;...if (se
8640: 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 3d 20 30 29 ndfile_len == 0)
8650: 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d {....break;...}
8660: 0a 09 7d 0a 0a 09 6c 6f 67 2d 3e 65 6e 64 74 69 ..}...log->endti
8670: 6d 65 20 3d 20 28 74 69 6d 65 5f 74 29 20 2d 31 me = (time_t) -1
8680: 3b 0a 09 6c 6f 67 2d 3e 73 65 6e 74 5f 6c 65 6e ;..log->sent_len
8690: 67 74 68 20 3d 20 73 65 6e 64 66 69 6c 65 5f 73 gth = sendfile_s
86a0: 65 6e 74 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 ent;...filed_log
86b0: 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 63 _entry(log);...c
86c0: 6c 6f 73 65 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 lose(fileinfo->f
86d0: 64 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 65 73 d);...if (reques
86e0: 74 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 t->headers.conne
86f0: 63 74 69 6f 6e 20 21 3d 20 46 49 4c 45 44 5f 43 ction != FILED_C
8700: 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 ONNECTION_KEEP_A
8710: 4c 49 56 45 29 20 7b 0a 09 09 66 69 6c 65 64 5f LIVE) {...filed_
8720: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c sockettimeout_cl
8730: 6f 73 65 28 66 64 29 3b 0a 0a 09 09 66 63 6c 6f ose(fd);....fclo
8740: 73 65 28 66 70 29 3b 0a 0a 09 09 72 65 74 75 72 se(fp);....retur
8750: 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 n(FILED_CONNECTI
8760: 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 ON_CLOSE);..}...
8770: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
8780: 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 65 out_processing_e
8790: 6e 64 28 66 64 29 3b 0a 0a 09 72 65 74 75 72 6e nd(fd);...return
87a0: 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f (FILED_CONNECTIO
87b0: 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 29 3b 0a 7d N_KEEP_ALIVE);.}
87c0: 0a 0a 2f 2a 20 48 61 6e 64 6c 65 20 69 6e 63 6f ../* Handle inco
87d0: 6d 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 ming connections
87e0: 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */.static void
87f0: 2a 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 *filed_worker_th
8800: 72 65 61 64 28 76 6f 69 64 20 2a 61 72 67 5f 76 read(void *arg_v
8810: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 ) {..struct file
8820: 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f d_worker_thread_
8830: 61 72 67 73 20 2a 61 72 67 3b 0a 09 73 74 72 75 args *arg;..stru
8840: 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 ct filed_http_re
8850: 71 75 65 73 74 20 72 65 71 75 65 73 74 3b 0a 09 quest request;..
8860: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 struct filed_log
8870: 5f 65 6e 74 72 79 20 2a 6c 6f 67 2c 20 6c 6f 63 _entry *log, loc
8880: 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 3b 0a 09 73 al_dummy_log;..s
8890: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 truct filed_opti
88a0: 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 3b 0a 09 73 ons *options;..s
88b0: 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 truct sockaddr_i
88c0: 6e 36 20 61 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 n6 addr;..sockle
88d0: 6e 5f 74 20 61 64 64 72 6c 65 6e 3b 0a 09 69 6e n_t addrlen;..in
88e0: 74 20 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 t failure_count
88f0: 3d 20 30 2c 20 6d 61 78 5f 66 61 69 6c 75 72 65 = 0, max_failure
8900: 5f 63 6f 75 6e 74 20 3d 20 46 49 4c 45 44 5f 4d _count = FILED_M
8910: 41 58 5f 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 AX_FAILURE_COUNT
8920: 3b 0a 09 69 6e 74 20 63 6f 6e 6e 65 63 74 69 6f ;..int connectio
8930: 6e 5f 73 74 61 74 65 20 3d 20 46 49 4c 45 44 5f n_state = FILED_
8940: 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 CONNECTION_CLOSE
8950: 3b 0a 09 69 6e 74 20 6d 61 73 74 65 72 5f 66 64 ;..int master_fd
8960: 2c 20 66 64 20 3d 20 2d 31 3b 0a 0a 09 2f 2a 20 , fd = -1;.../*
8970: 52 65 61 64 20 61 72 67 75 6d 65 6e 74 73 20 2a Read arguments *
8980: 2f 0a 09 61 72 67 20 3d 20 61 72 67 5f 76 3b 0a /..arg = arg_v;.
8990: 0a 09 6d 61 73 74 65 72 5f 66 64 20 3d 20 61 72 ..master_fd = ar
89a0: 67 2d 3e 66 64 3b 0a 09 6f 70 74 69 6f 6e 73 20 g->fd;..options
89b0: 3d 20 26 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 3b = &arg->options;
89c0: 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 ...while (1) {..
89d0: 09 2f 2a 20 46 61 69 6c 75 72 65 20 6c 6f 6f 70 ./* Failure loop
89e0: 20 70 72 65 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09 prevention */..
89f0: 09 69 66 20 28 66 61 69 6c 75 72 65 5f 63 6f 75 .if (failure_cou
8a00: 6e 74 20 3e 20 6d 61 78 5f 66 61 69 6c 75 72 65 nt > max_failure
8a10: 5f 63 6f 75 6e 74 29 20 7b 0a 09 09 09 62 72 65 _count) {....bre
8a20: 61 6b 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 41 6c ak;...}..../* Al
8a30: 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 6c 6f 67 locate a new log
8a40: 20 62 75 66 66 65 72 20 2a 2f 0a 09 09 6c 6f 67 buffer */...log
8a50: 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 = filed_log_new
8a60: 28 31 29 3b 0a 09 09 69 66 20 28 6c 6f 67 20 3d (1);...if (log =
8a70: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 66 69 6c = NULL) {....fil
8a80: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 4c 4c 4f ed_log_msg("ALLO
8a90: 43 41 54 45 5f 4c 4f 47 5f 4d 53 47 5f 46 41 49 CATE_LOG_MSG_FAI
8aa0: 4c 45 44 22 29 3b 0a 0a 09 09 09 62 72 65 61 6b LED");.....break
8ab0: 3b 0a 09 09 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 79 ;...}....log->ty
8ac0: 70 65 20 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 pe = FILED_LOG_T
8ad0: 59 50 45 5f 54 52 41 4e 53 46 45 52 3b 0a 0a 09 YPE_TRANSFER;...
8ae0: 09 2f 2a 20 49 66 20 77 65 20 63 6c 6f 73 65 64 ./* If we closed
8af0: 20 74 68 65 20 6f 6c 64 20 63 6f 6e 6e 65 63 74 the old connect
8b00: 69 6f 6e 2c 20 61 63 63 65 70 74 20 61 20 6e 65 ion, accept a ne
8b10: 77 20 6f 6e 65 20 2a 2f 0a 09 09 69 66 20 28 63 w one */...if (c
8b20: 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65 20 onnection_state
8b30: 3d 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 == FILED_CONNECT
8b40: 49 4f 4e 5f 43 4c 4f 53 45 29 20 7b 0a 09 09 09 ION_CLOSE) {....
8b50: 2f 2a 20 41 63 63 65 70 74 20 61 20 6e 65 77 20 /* Accept a new
8b60: 63 6c 69 65 6e 74 20 2a 2f 0a 09 09 09 61 64 64 client */....add
8b70: 72 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 rlen = sizeof(ad
8b80: 64 72 29 3b 0a 0a 09 09 09 66 64 20 3d 20 61 63 dr);.....fd = ac
8b90: 63 65 70 74 28 6d 61 73 74 65 72 5f 66 64 2c 20 cept(master_fd,
8ba0: 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 (struct sockaddr
8bb0: 20 2a 29 20 26 61 64 64 72 2c 20 26 61 64 64 72 *) &addr, &addr
8bc0: 6c 65 6e 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 0a len);...}..../*.
8bd0: 09 09 20 2a 20 49 66 20 77 65 20 66 61 69 6c 2c .. * If we fail,
8be0: 20 6d 61 6b 65 20 61 20 6e 6f 74 65 20 6f 66 20 make a note of
8bf0: 69 74 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 67 it so we don't g
8c00: 6f 20 69 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f 66 o into a loop of
8c10: 0a 09 09 20 2a 20 61 63 63 65 70 74 28 29 20 66 ... * accept() f
8c20: 61 69 6c 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 69 ailing... */...i
8c30: 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 09 f (fd < 0) {....
8c40: 2f 2a 20 4c 6f 67 20 74 68 65 20 6e 65 77 20 63 /* Log the new c
8c50: 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 09 onnection */....
8c60: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 filed_log_msg("A
8c70: 43 43 45 50 54 5f 46 41 49 4c 45 44 22 29 3b 0a CCEPT_FAILED");.
8c80: 0a 09 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e ....failure_coun
8c90: 74 2b 2b 3b 0a 0a 09 09 09 66 69 6c 65 64 5f 6c t++;.....filed_l
8ca0: 6f 67 5f 66 72 65 65 28 6c 6f 67 29 3b 0a 0a 09 og_free(log);...
8cb0: 09 09 63 6f 6e 74 69 6e 75 65 3b 0a 09 09 7d 0a ..continue;...}.
8cc0: 0a 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ...filed_sockett
8cd0: 69 6d 65 6f 75 74 5f 61 63 63 65 70 74 28 66 64 imeout_accept(fd
8ce0: 29 3b 0a 0a 09 09 2f 2a 20 46 69 6c 6c 20 69 6e );..../* Fill in
8cf0: 20 6c 6f 67 20 73 74 72 75 63 74 75 72 65 20 2a log structure *
8d00: 2f 0a 09 09 69 66 20 28 66 69 6c 65 64 5f 6c 6f /...if (filed_lo
8d10: 67 5f 69 70 28 28 73 74 72 75 63 74 20 73 6f 63 g_ip((struct soc
8d20: 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 2c 20 kaddr *) &addr,
8d30: 6c 6f 67 2d 3e 69 70 2c 20 73 69 7a 65 6f 66 28 log->ip, sizeof(
8d40: 6c 6f 67 2d 3e 69 70 29 29 20 3d 3d 20 4e 55 4c log->ip)) == NUL
8d50: 4c 29 20 7b 0a 09 09 09 6c 6f 67 2d 3e 69 70 5b L) {....log->ip[
8d60: 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 09 6c 6f 0] = '\0';....lo
8d70: 67 2d 3e 70 6f 72 74 20 3d 20 30 3b 0a 09 09 7d g->port = 0;...}
8d80: 20 65 6c 73 65 20 7b 0a 09 09 09 6c 6f 67 2d 3e else {....log->
8d90: 70 6f 72 74 20 3d 20 61 64 64 72 2e 73 69 6e 36 port = addr.sin6
8da0: 5f 70 6f 72 74 3b 0a 09 09 7d 0a 0a 09 09 2f 2a _port;...}..../*
8db0: 20 52 65 73 65 74 20 66 61 69 6c 75 72 65 20 63 Reset failure c
8dc0: 6f 75 6e 74 2a 2f 0a 09 09 66 61 69 6c 75 72 65 ount*/...failure
8dd0: 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 0a 09 09 2f _count = 0;..../
8de0: 2a 20 48 61 6e 64 6c 65 20 73 6f 63 6b 65 74 20 * Handle socket
8df0: 2a 2f 0a 09 09 63 6f 6e 6e 65 63 74 69 6f 6e 5f */...connection_
8e00: 73 74 61 74 65 20 3d 20 66 69 6c 65 64 5f 68 61 state = filed_ha
8e10: 6e 64 6c 65 5f 63 6c 69 65 6e 74 28 66 64 2c 20 ndle_client(fd,
8e20: 26 72 65 71 75 65 73 74 2c 20 6c 6f 67 2c 20 6f &request, log, o
8e30: 70 74 69 6f 6e 73 29 3b 0a 09 7d 0a 0a 09 2f 2a ptions);..}.../*
8e40: 20 52 65 70 6f 72 74 20 65 72 72 6f 72 20 2a 2f Report error */
8e50: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 ..filed_log_msg(
8e60: 22 54 48 52 45 41 44 5f 44 49 45 44 20 41 42 4e "THREAD_DIED ABN
8e70: 4f 52 4d 41 4c 22 29 3b 0a 0a 09 72 65 74 75 72 ORMAL");...retur
8e80: 6e 28 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 6c 6f n(NULL);.../* lo
8e90: 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 20 69 73 cal_dummy_log is
8ea0: 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 46 49 only used if FI
8eb0: 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 20 69 73 20 LED_DONT_LOG is
8ec0: 65 6e 61 62 6c 65 64 2c 20 6f 74 68 65 72 77 69 enabled, otherwi
8ed0: 73 65 20 69 74 27 73 20 6e 6f 74 20 75 73 65 64 se it's not used
8ee0: 2c 20 62 75 74 20 74 68 65 20 63 6f 6d 70 69 6c , but the compil
8ef0: 65 72 20 68 61 74 65 73 20 74 68 61 74 20 69 64 er hates that id
8f00: 65 61 2e 20 2a 2f 0a 09 6c 6f 63 61 6c 5f 64 75 ea. */..local_du
8f10: 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 20 3d 20 30 mmy_log.type = 0
8f20: 3b 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c ;..local_dummy_l
8f30: 6f 67 2e 74 79 70 65 20 3d 20 6c 6f 63 61 6c 5f og.type = local_
8f40: 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 3b 0a dummy_log.type;.
8f50: 7d 0a 0a 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 }../* Create wor
8f60: 6b 65 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 ker threads */.s
8f70: 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f tatic int filed_
8f80: 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 worker_threads_i
8f90: 6e 69 74 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 nit(int fd, int
8fa0: 74 68 72 65 61 64 5f 63 6f 75 6e 74 2c 20 73 74 thread_count, st
8fb0: 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f ruct filed_optio
8fc0: 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 ns *options) {..
8fd0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 struct filed_wor
8fe0: 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 73 20 ker_thread_args
8ff0: 2a 61 72 67 3b 0a 09 70 74 68 72 65 61 64 5f 74 *arg;..pthread_t
9000: 20 74 68 72 65 61 64 69 64 3b 0a 09 69 6e 74 20 threadid;..int
9010: 70 74 68 72 65 61 64 5f 72 65 74 3b 0a 09 69 6e pthread_ret;..in
9020: 74 20 69 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20 t i;...for (i =
9030: 30 3b 20 69 20 3c 20 74 68 72 65 61 64 5f 63 6f 0; i < thread_co
9040: 75 6e 74 3b 20 69 2b 2b 29 20 7b 0a 09 09 61 72 unt; i++) {...ar
9050: 67 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f g = malloc(sizeo
9060: 66 28 2a 61 72 67 29 29 3b 0a 0a 09 09 61 72 67 f(*arg));....arg
9070: 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 09 6d 65 6d ->fd = fd;...mem
9080: 63 70 79 28 26 61 72 67 2d 3e 6f 70 74 69 6f 6e cpy(&arg->option
9090: 73 2c 20 6f 70 74 69 6f 6e 73 2c 20 73 69 7a 65 s, options, size
90a0: 6f 66 28 2a 6f 70 74 69 6f 6e 73 29 29 3b 0a 0a of(*options));..
90b0: 09 09 70 74 68 72 65 61 64 5f 72 65 74 20 3d 20 ..pthread_ret =
90c0: 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 pthread_create(&
90d0: 74 68 72 65 61 64 69 64 2c 20 4e 55 4c 4c 2c 20 threadid, NULL,
90e0: 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 filed_worker_thr
90f0: 65 61 64 2c 20 61 72 67 29 3b 0a 09 09 69 66 20 ead, arg);...if
9100: 28 70 74 68 72 65 61 64 5f 72 65 74 20 21 3d 20 (pthread_ret !=
9110: 30 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 0) {....return(-
9120: 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 1);...}..}...ret
9130: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 urn(0);.}../* Di
9140: 73 70 6c 61 79 20 68 65 6c 70 20 2a 2f 0a 73 74 splay help */.st
9150: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f atic void filed_
9160: 70 72 69 6e 74 5f 68 65 6c 70 28 46 49 4c 45 20 print_help(FILE
9170: 2a 6f 75 74 70 75 74 2c 20 69 6e 74 20 6c 6f 6e *output, int lon
9180: 67 5f 68 65 6c 70 2c 20 63 6f 6e 73 74 20 63 68 g_help, const ch
9190: 61 72 20 2a 65 78 74 72 61 29 20 7b 0a 09 69 66 ar *extra) {..if
91a0: 20 28 65 78 74 72 61 29 20 7b 0a 09 09 66 70 72 (extra) {...fpr
91b0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 25 73 intf(output, "%s
91c0: 5c 6e 22 2c 20 65 78 74 72 61 29 3b 0a 09 7d 0a \n", extra);..}.
91d0: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
91e0: 2c 20 22 55 73 61 67 65 3a 20 66 69 6c 65 64 20 , "Usage: filed
91f0: 5b 3c 6f 70 74 69 6f 6e 73 3e 5d 5c 6e 22 29 3b [<options>]\n");
9200: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
9210: 2c 20 22 20 20 4f 70 74 69 6f 6e 73 3a 5c 6e 22 , " Options:\n"
9220: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
9230: 75 74 2c 20 22 20 20 20 20 20 20 2d 68 2c 20 2d ut, " -h, -
9240: 2d 68 65 6c 70 5c 6e 22 29 3b 0a 09 66 70 72 69 -help\n");..fpri
9250: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9260: 20 20 20 2d 64 2c 20 2d 2d 64 61 65 6d 6f 6e 5c -d, --daemon\
9270: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
9280: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 2c tput, " -v,
9290: 20 2d 2d 76 65 72 73 69 6f 6e 5c 6e 22 29 3b 0a --version\n");.
92a0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
92b0: 20 22 20 20 20 20 20 20 2d 56 2c 20 2d 2d 76 68 " -V, --vh
92c0: 6f 73 74 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 ost\n");..fprint
92d0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
92e0: 20 2d 62 20 3c 61 64 64 72 65 73 73 3e 2c 20 2d -b <address>, -
92f0: 2d 62 69 6e 64 20 3c 61 64 64 72 65 73 73 3e 5c -bind <address>\
9300: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
9310: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 70 20 tput, " -p
9320: 3c 70 6f 72 74 3e 2c 20 2d 2d 70 6f 72 74 20 3c <port>, --port <
9330: 70 6f 72 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 port>\n");..fpri
9340: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9350: 20 20 20 2d 74 20 3c 63 6f 75 6e 74 3e 2c 20 2d -t <count>, -
9360: 2d 74 68 72 65 61 64 73 20 3c 63 6f 75 6e 74 3e -threads <count>
9370: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f \n");..fprintf(o
9380: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 63 utput, " -c
9390: 20 3c 65 6e 74 72 69 65 73 3e 2c 20 2d 2d 63 61 <entries>, --ca
93a0: 63 68 65 20 3c 65 6e 74 72 69 65 73 3e 5c 6e 22 che <entries>\n"
93b0: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
93c0: 75 74 2c 20 22 20 20 20 20 20 20 2d 6c 20 3c 66 ut, " -l <f
93d0: 69 6c 65 3e 2c 20 2d 2d 6c 6f 67 20 3c 66 69 6c ile>, --log <fil
93e0: 65 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 e>\n");..fprintf
93f0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
9400: 2d 75 20 3c 75 73 65 72 3e 2c 20 2d 2d 75 73 65 -u <user>, --use
9410: 72 20 3c 75 73 65 72 3e 5c 6e 22 29 3b 0a 09 66 r <user>\n");..f
9420: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
9430: 20 20 20 20 20 20 2d 72 20 3c 64 69 72 65 63 74 -r <direct
9440: 6f 72 79 3e 2c 20 2d 2d 72 6f 6f 74 20 3c 64 69 ory>, --root <di
9450: 72 65 63 74 6f 72 79 3e 5c 6e 22 29 3b 0a 0a 09 rectory>\n");...
9460: 69 66 20 28 6c 6f 6e 67 5f 68 65 6c 70 29 20 7b if (long_help) {
9470: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9480: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t, "\n");...fpri
9490: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 55 ntf(output, " U
94a0: 73 61 67 65 3a 5c 6e 22 29 3b 0a 09 09 66 70 72 sage:\n");...fpr
94b0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
94c0: 20 20 20 20 2d 68 20 28 6f 72 20 2d 2d 68 65 6c -h (or --hel
94d0: 70 29 20 70 72 69 6e 74 73 20 74 68 69 73 20 75 p) prints this u
94e0: 73 61 67 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e sage information
94f0: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 .\n");...fprintf
9500: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a (output, "\n");.
9510: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
9520: 2c 20 22 20 20 20 20 20 20 2d 64 20 28 6f 72 20 , " -d (or
9530: 2d 2d 64 61 65 6d 6f 6e 29 20 69 6e 73 74 72 75 --daemon) instru
9540: 63 74 73 20 66 69 6c 65 64 20 74 6f 20 62 65 63 cts filed to bec
9550: 6f 6d 65 20 61 20 64 61 65 6d 6f 6e 20 61 66 74 ome a daemon aft
9560: 65 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 5c er initializing\
9570: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
9580: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
9590: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
95a0: 68 65 20 6c 69 73 74 65 6e 69 6e 67 20 54 43 50 he listening TCP
95b0: 20 73 6f 63 6b 65 74 20 61 6e 64 20 6c 6f 67 20 socket and log
95c0: 66 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 files.\n");...fp
95d0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
95e0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
95f0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 utput, " -v
9600: 20 28 6f 72 20 2d 2d 76 65 72 73 69 6f 6e 29 20 (or --version)
9610: 69 6e 73 74 72 75 63 74 73 20 66 69 6c 65 64 20 instructs filed
9620: 70 72 69 6e 74 20 6f 75 74 20 74 68 65 20 76 65 print out the ve
9630: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 rsion number and
9640: 20 65 78 69 74 2e 5c 6e 22 29 3b 0a 09 09 66 70 exit.\n");...fp
9650: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
9660: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
9670: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 56 utput, " -V
9680: 20 28 6f 72 20 2d 2d 76 68 6f 73 74 29 20 69 6e (or --vhost) in
9690: 73 74 72 75 63 74 73 20 66 69 6c 65 64 20 74 6f structs filed to
96a0: 20 70 72 65 70 65 6e 64 20 61 6c 6c 20 72 65 71 prepend all req
96b0: 75 65 73 74 73 20 77 69 74 68 20 74 68 65 69 72 uests with their
96c0: 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 HTTP\n");...fpr
96d0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
96e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
96f0: 20 20 20 20 48 6f 73 74 20 68 65 61 64 65 72 2e Host header.
9700: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
9710: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 output, "\n");..
9720: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
9730: 20 22 20 20 20 20 20 20 2d 62 20 28 6f 72 20 2d " -b (or -
9740: 2d 62 69 6e 64 29 20 73 70 65 63 69 66 69 65 73 -bind) specifies
9750: 20 74 68 65 20 61 64 64 72 65 73 73 20 74 6f 20 the address to
9760: 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d listen for incom
9770: 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 ing HTTP\n");...
9780: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
9790: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
97a0: 20 20 20 20 20 20 72 65 71 75 65 73 74 73 20 6f requests o
97b0: 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 n. The default
97c0: 76 61 6c 75 65 20 69 73 20 5c 22 25 73 5c 22 2e value is \"%s\".
97d0: 5c 6e 22 2c 20 42 49 4e 44 5f 41 44 44 52 29 3b \n", BIND_ADDR);
97e0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
97f0: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t, "\n");...fpri
9800: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9810: 20 20 20 2d 70 20 28 6f 72 20 2d 2d 70 6f 72 74 -p (or --port
9820: 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 ) specifies the
9830: 54 43 50 20 70 6f 72 74 20 6e 75 6d 62 65 72 20 TCP port number
9840: 74 6f 20 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e to listen for in
9850: 63 6f 6d 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b coming HTTP\n");
9860: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9870: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
9880: 20 20 20 20 20 20 20 20 20 72 65 71 75 65 73 74 request
9890: 73 20 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 s on. The defau
98a0: 6c 74 20 69 73 20 25 75 2e 5c 6e 22 2c 20 28 75 lt is %u.\n", (u
98b0: 6e 73 69 67 6e 65 64 20 69 6e 74 29 20 50 4f 52 nsigned int) POR
98c0: 54 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 T);...fprintf(ou
98d0: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 tput, "\n");...f
98e0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
98f0: 20 20 20 20 20 20 2d 74 20 28 6f 72 20 2d 2d 74 -t (or --t
9900: 68 72 65 61 64 73 29 20 73 70 65 63 69 66 69 65 hreads) specifie
9910: 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
9920: 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 74 worker threads t
9930: 6f 20 63 72 65 61 74 65 2e 20 45 61 63 68 5c 6e o create. Each\n
9940: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
9950: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
9960: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 w
9970: 6f 72 6b 65 72 20 74 68 72 65 61 64 20 63 61 6e orker thread can
9980: 20 73 65 72 76 69 63 65 20 6f 6e 65 20 63 6f 6e service one con
9990: 63 75 72 72 65 6e 74 20 48 54 54 50 20 73 65 73 current HTTP ses
99a0: 73 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 sion.\n");...fpr
99b0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
99c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
99d0: 20 20 20 20 20 20 54 68 75 73 20 74 68 65 20 6e Thus the n
99e0: 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 umber of threads
99f0: 20 63 72 65 61 74 65 64 20 77 69 6c 6c 20 64 65 created will de
9a00: 74 65 72 6d 69 6e 65 20 68 6f 77 5c 6e 22 29 3b termine how\n");
9a10: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9a20: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
9a30: 20 20 20 20 20 20 20 20 20 20 20 20 6d 61 6e 79 many
9a40: 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 74 72 simultaneous tr
9a50: 61 6e 73 66 65 72 73 20 77 69 6c 6c 20 62 65 20 ansfers will be
9a60: 70 6f 73 73 69 62 6c 65 2e 20 54 68 65 5c 6e 22 possible. The\n"
9a70: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
9a80: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
9a90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 65 de
9aa0: 66 61 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 fault is %lu.\n"
9ab0: 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 , (unsigned long
9ac0: 29 20 54 48 52 45 41 44 5f 43 4f 55 4e 54 29 3b ) THREAD_COUNT);
9ad0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9ae0: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t, "\n");...fpri
9af0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9b00: 20 20 20 2d 63 20 28 6f 72 20 2d 2d 63 61 63 68 -c (or --cach
9b10: 65 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 e) specifies the
9b20: 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 6c 65 20 number of file
9b30: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 61 63 68 information cach
9b40: 65 20 65 6e 74 72 69 65 73 5c 6e 22 29 3b 0a 09 e entries\n");..
9b50: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
9b60: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
9b70: 20 20 20 20 20 20 20 20 74 6f 20 61 6c 6c 6f 63 to alloc
9b80: 61 74 65 2e 20 20 45 61 63 68 20 63 61 63 68 65 ate. Each cache
9b90: 20 65 6e 74 72 79 20 68 6f 6c 64 73 20 66 69 6c entry holds fil
9ba0: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 e information as
9bb0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
9bc0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
9bd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 w
9be0: 65 6c 6c 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 ell as an open f
9bf0: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
9c00: 6f 20 74 68 65 20 66 69 6c 65 2c 20 73 6f 20 72 o the file, so r
9c10: 65 73 6f 75 72 63 65 5c 6e 22 29 3b 0a 09 09 66 esource\n");...f
9c20: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
9c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c40: 20 20 20 20 20 20 6c 69 6d 69 74 73 20 28 69 2e limits (i.
9c50: 65 2e 2c 20 75 6c 69 6d 69 74 29 20 73 68 6f 75 e., ulimit) shou
9c60: 6c 64 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 ld be considered
9c70: 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 5c 6e . This should\n
9c80: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
9c90: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
9ca0: 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20 be
9cb0: 61 20 70 72 69 6d 65 20 6e 75 6d 62 65 72 20 66 a prime number f
9cc0: 6f 72 20 69 64 65 61 6c 20 75 73 65 20 77 69 74 or ideal use wit
9cd0: 68 20 74 68 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 h the lookup met
9ce0: 68 6f 64 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 hod.\n");...fpri
9cf0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9d00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9d10: 20 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 The default i
9d20: 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 s %lu.\n", (unsi
9d30: 67 6e 65 64 20 6c 6f 6e 67 29 20 43 41 43 48 45 gned long) CACHE
9d40: 5f 53 49 5a 45 29 3b 0a 09 09 66 70 72 69 6e 74 _SIZE);...fprint
9d50: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
9d60: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9d70: 74 2c 20 22 20 20 20 20 20 20 2d 6c 20 28 6f 72 t, " -l (or
9d80: 20 2d 2d 6c 6f 67 29 20 73 70 65 63 69 66 69 65 --log) specifie
9d90: 73 20 61 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 s a filename to
9da0: 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 open for writing
9db0: 20 6c 6f 67 20 65 6e 74 72 69 65 73 2e 20 20 4c log entries. L
9dc0: 6f 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 og\n");...fprint
9dd0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
9de0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 e
9df0: 6e 74 72 69 65 73 20 61 72 65 20 6d 61 64 65 20 ntries are made
9e00: 66 6f 72 20 76 61 72 69 6f 75 73 20 73 74 61 67 for various stag
9e10: 65 73 20 69 6e 20 74 72 61 6e 73 66 65 72 69 6e es in transferin
9e20: 67 20 66 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 g files.\n");...
9e30: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
9e40: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
9e50: 20 20 20 20 20 54 68 65 20 6c 6f 67 20 66 69 6c The log fil
9e60: 65 20 69 73 20 6f 70 65 6e 65 64 20 62 65 66 6f e is opened befo
9e70: 72 65 20 73 77 69 74 63 68 69 6e 67 20 75 73 65 re switching use
9e80: 72 73 20 28 73 65 65 20 5c 22 2d 75 5c 22 29 5c rs (see \"-u\")\
9e90: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
9ea0: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
9eb0: 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 and
9ec0: 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73 root directories
9ed0: 20 28 73 65 65 20 5c 22 2d 72 5c 22 29 2e 20 20 (see \"-r\").
9ee0: 54 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 The log file is
9ef0: 6e 65 76 65 72 5c 6e 22 29 3b 0a 09 09 66 70 72 never\n");...fpr
9f00: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
9f10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f20: 20 20 63 6c 6f 73 65 64 20 73 6f 20 6c 6f 67 20 closed so log
9f30: 72 6f 74 61 74 69 6f 6e 20 77 69 74 68 6f 75 74 rotation without
9f40: 20 73 74 6f 70 70 69 6e 67 20 74 68 65 20 64 61 stopping the da
9f50: 65 6d 6f 6e 20 69 73 20 77 69 6c 6c 5c 6e 22 29 emon is will\n")
9f60: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
9f70: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
9f80: 20 20 20 20 20 20 20 20 20 6e 6f 74 20 77 6f 72 not wor
9f90: 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 k. The value of
9fa0: 20 5c 22 2d 5c 22 20 69 6e 64 69 63 61 74 65 73 \"-\" indicates
9fb0: 20 74 68 61 74 20 73 74 61 6e 64 61 72 64 20 6f that standard o
9fc0: 75 74 70 75 74 5c 6e 22 29 3b 0a 09 09 66 70 72 utput\n");...fpr
9fd0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
9fe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ff0: 20 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 should be used
a000: 20 66 6f 72 20 6c 6f 67 67 69 6e 67 2e 20 20 49 for logging. I
a010: 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 62 f the filename b
a020: 65 67 69 6e 73 20 77 69 74 68 20 61 5c 6e 22 29 egins with a\n")
a030: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
a040: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
a050: 20 20 20 20 20 20 20 20 20 70 69 70 65 20 28 5c pipe (\
a060: 22 7c 5c 22 29 20 74 68 65 6e 20 61 20 70 72 6f "|\") then a pro
a070: 63 65 73 73 20 69 73 20 73 74 61 72 74 65 64 20 cess is started
a080: 61 6e 64 20 75 73 65 64 20 66 6f 72 20 6c 6f 67 and used for log
a090: 67 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 ging\n");...fpri
a0a0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
a0b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0c0: 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 69 instead of a fi
a0d0: 6c 65 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 le. The default
a0e0: 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 is \"%s\".\n",
a0f0: 4c 4f 47 5f 46 49 4c 45 29 3b 0a 23 69 66 64 65 LOG_FILE);.#ifde
a100: 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 f FILED_DONT_LOG
a110: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
a120: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
a130: 20 20 20 20 20 20 20 20 4e 6f 74 65 20 74 68 61 Note tha
a140: 74 20 6c 6f 67 67 69 6e 67 20 69 73 20 63 6f 6d t logging is com
a150: 70 6c 65 74 65 6c 79 20 64 69 73 61 62 6c 65 64 pletely disabled
a160: 20 73 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 so this option
a170: 64 6f 65 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 does\n");...fpri
a180: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
a190: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1a0: 20 6e 6f 74 68 69 6e 67 20 69 6e 20 74 68 69 73 nothing in this
a1b0: 20 62 75 69 6c 64 2e 5c 6e 22 29 3b 0a 23 65 6e build.\n");.#en
a1c0: 64 69 66 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 dif...fprintf(ou
a1d0: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 tput, "\n");...f
a1e0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
a1f0: 20 20 20 20 20 20 2d 75 20 28 6f 72 20 2d 2d 75 -u (or --u
a200: 73 65 72 29 20 73 70 65 63 69 66 69 65 73 20 74 ser) specifies t
a210: 68 65 20 75 73 65 72 20 74 6f 20 73 77 69 74 63 he user to switc
a220: 68 20 75 73 65 72 20 49 44 73 20 74 6f 20 62 65 h user IDs to be
a230: 66 6f 72 65 20 73 65 72 76 69 63 69 6e 67 5c 6e fore servicing\n
a240: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
a250: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
a260: 20 20 20 20 20 20 20 20 20 20 20 20 72 65 71 75 requ
a270: 65 73 74 73 2e 20 20 54 68 65 20 64 65 66 61 75 ests. The defau
a280: 6c 74 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 lt is not change
a290: 20 75 73 65 72 20 49 44 73 2e 5c 6e 22 29 3b 0a user IDs.\n");.
a2a0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
a2b0: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e , "\n");...fprin
a2c0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
a2d0: 20 20 2d 72 20 28 6f 72 20 2d 2d 72 6f 6f 74 29 -r (or --root)
a2e0: 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 64 specifies the d
a2f0: 69 72 65 63 74 6f 72 79 20 74 6f 20 61 63 74 20 irectory to act
a300: 61 73 20 74 68 65 20 72 6f 6f 74 20 64 69 72 65 as the root dire
a310: 63 74 6f 72 79 20 66 6f 72 5c 6e 22 29 3b 0a 09 ctory for\n");..
a320: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
a330: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a340: 20 20 20 20 20 20 20 74 68 65 20 66 69 6c 65 20 the file
a350: 73 65 72 76 65 72 2e 20 20 49 66 20 74 68 69 73 server. If this
a360: 20 6f 70 74 69 6f 6e 20 69 73 20 73 70 65 63 69 option is speci
a370: 66 69 65 64 2c 20 63 68 72 6f 6f 74 28 32 29 5c fied, chroot(2)\
a380: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
a390: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
a3a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 is
a3b0: 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 64 65 66 called. The def
a3c0: 61 75 6c 74 20 69 73 20 6e 6f 74 20 63 68 61 6e ault is not chan
a3d0: 67 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 ge root director
a3e0: 69 65 73 2c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 ies,\n");...fpri
a3f0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
a400: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a410: 20 20 74 68 61 74 20 69 73 2c 20 74 68 65 20 5c that is, the \
a420: 22 2f 5c 22 20 64 69 72 65 63 74 6f 72 79 20 69 "/\" directory i
a430: 73 20 73 68 61 72 65 64 20 6f 75 74 2e 20 20 54 s shared out. T
a440: 68 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 his will\n");...
a450: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
a460: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a470: 20 20 20 20 20 20 6c 69 6b 65 6c 79 20 62 65 20 likely be
a480: 61 20 73 65 63 75 72 69 74 79 20 69 73 73 75 65 a security issue
a490: 2c 20 73 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e , so this option
a4a0: 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 5c 6e should always\n
a4b0: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
a4c0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
a4d0: 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20 75 be u
a4e0: 73 65 64 2e 5c 6e 22 29 3b 0a 09 7d 0a 0a 09 72 sed.\n");..}...r
a4f0: 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 eturn;.}../* Add
a500: 20 61 20 67 65 74 6f 70 74 20 6f 70 74 69 6f 6e a getopt option
a510: 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */.static void
a520: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
a530: 67 5f 73 65 74 6f 70 74 28 73 74 72 75 63 74 20 g_setopt(struct
a540: 6f 70 74 69 6f 6e 20 2a 6f 70 74 2c 20 63 6f 6e option *opt, con
a550: 73 74 20 63 68 61 72 20 2a 6e 61 6d 65 2c 20 69 st char *name, i
a560: 6e 74 20 68 61 73 5f 61 72 67 2c 20 69 6e 74 20 nt has_arg, int
a570: 76 61 6c 29 20 7b 0a 09 6f 70 74 2d 3e 6e 61 6d val) {..opt->nam
a580: 65 20 20 20 20 20 3d 20 6e 61 6d 65 3b 0a 09 6f e = name;..o
a590: 70 74 2d 3e 68 61 73 5f 61 72 67 20 20 3d 20 68 pt->has_arg = h
a5a0: 61 73 5f 61 72 67 3b 0a 09 6f 70 74 2d 3e 66 6c as_arg;..opt->fl
a5b0: 61 67 20 20 20 20 20 3d 20 4e 55 4c 4c 3b 0a 09 ag = NULL;..
a5c0: 6f 70 74 2d 3e 76 61 6c 20 20 20 20 20 20 3d 20 opt->val =
a5d0: 76 61 6c 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d val;...return;.}
a5e0: 0a 0a 2f 2a 20 52 65 73 6f 6c 76 65 20 61 20 75 ../* Resolve a u
a5f0: 73 65 72 6e 61 6d 65 20 74 6f 20 61 20 55 49 44 sername to a UID
a600: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 */.static int f
a610: 69 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 iled_user_lookup
a620: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 75 73 65 (const char *use
a630: 72 2c 20 75 69 64 5f 74 20 2a 75 73 65 72 5f 69 r, uid_t *user_i
a640: 64 29 20 7b 0a 09 63 68 61 72 20 2a 6e 65 78 74 d) {..char *next
a650: 3b 0a 09 75 69 64 5f 74 20 75 73 65 72 5f 69 64 ;..uid_t user_id
a660: 5f 63 68 65 63 6b 3b 0a 23 69 66 6e 64 65 66 20 _check;.#ifndef
a670: 46 49 4c 45 44 5f 4e 4f 5f 47 45 54 50 57 4e 41 FILED_NO_GETPWNA
a680: 4d 0a 09 73 74 72 75 63 74 20 70 61 73 73 77 64 M..struct passwd
a690: 20 2a 65 6e 74 3b 0a 0a 09 65 6e 74 20 3d 20 67 *ent;...ent = g
a6a0: 65 74 70 77 6e 61 6d 28 75 73 65 72 29 3b 0a 09 etpwnam(user);..
a6b0: 69 66 20 28 65 6e 74 20 21 3d 20 4e 55 4c 4c 29 if (ent != NULL)
a6c0: 20 7b 0a 09 09 2a 75 73 65 72 5f 69 64 20 3d 20 {...*user_id =
a6d0: 65 6e 74 2d 3e 70 77 5f 75 69 64 3b 0a 0a 09 09 ent->pw_uid;....
a6e0: 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 23 65 return(0);..}.#e
a6f0: 6e 64 69 66 0a 0a 09 75 73 65 72 5f 69 64 5f 63 ndif...user_id_c
a700: 68 65 63 6b 20 3d 20 73 74 72 74 6f 75 6c 6c 28 heck = strtoull(
a710: 75 73 65 72 2c 20 26 6e 65 78 74 2c 20 31 30 29 user, &next, 10)
a720: 3b 0a 09 69 66 20 28 6e 65 78 74 20 3d 3d 20 4e ;..if (next == N
a730: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 ULL) {...return(
a740: 31 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 6e 65 78 1);..}...if (nex
a750: 74 5b 30 5d 20 21 3d 20 27 5c 30 27 29 20 7b 0a t[0] != '\0') {.
a760: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
a770: 0a 09 2a 75 73 65 72 5f 69 64 20 3d 20 75 73 65 ..*user_id = use
a780: 72 5f 69 64 5f 63 68 65 63 6b 3b 0a 0a 09 72 65 r_id_check;...re
a790: 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 turn(0);.}../* D
a7a0: 61 65 6d 6f 6e 69 7a 65 20 2a 2f 0a 73 74 61 74 aemonize */.stat
a7b0: 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 64 61 65 ic int filed_dae
a7c0: 6d 6f 6e 69 7a 65 28 76 6f 69 64 29 20 7b 0a 09 monize(void) {..
a7d0: 70 69 64 5f 74 20 73 65 74 73 69 64 5f 72 65 74 pid_t setsid_ret
a7e0: 2c 20 66 6f 72 6b 5f 72 65 74 3b 0a 09 69 6e 74 , fork_ret;..int
a7f0: 20 63 68 64 69 72 5f 72 65 74 2c 20 64 75 70 32 chdir_ret, dup2
a800: 5f 72 65 74 3b 0a 09 69 6e 74 20 66 64 5f 69 6e _ret;..int fd_in
a810: 2c 20 66 64 5f 6f 75 74 3b 0a 0a 09 63 68 64 69 , fd_out;...chdi
a820: 72 5f 72 65 74 20 3d 20 63 68 64 69 72 28 22 2f r_ret = chdir("/
a830: 22 29 3b 0a 09 69 66 20 28 63 68 64 69 72 5f 72 ");..if (chdir_r
a840: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 et != 0) {...ret
a850: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 urn(1);..}...for
a860: 6b 5f 72 65 74 20 3d 20 66 6f 72 6b 28 29 3b 0a k_ret = fork();.
a870: 09 69 66 20 28 66 6f 72 6b 5f 72 65 74 20 3c 20 .if (fork_ret <
a880: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 0) {...return(1)
a890: 3b 0a 09 7d 0a 0a 09 69 66 20 28 66 6f 72 6b 5f ;..}...if (fork_
a8a0: 72 65 74 20 3e 20 30 29 20 7b 0a 09 09 2f 2a 20 ret > 0) {.../*
a8b0: 50 61 72 65 6e 74 20 2a 2f 0a 09 09 77 61 69 74 Parent */...wait
a8c0: 70 69 64 28 66 6f 72 6b 5f 72 65 74 2c 20 4e 55 pid(fork_ret, NU
a8d0: 4c 4c 2c 20 30 29 3b 0a 0a 09 09 65 78 69 74 28 LL, 0);....exit(
a8e0: 45 58 49 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 EXIT_SUCCESS);..
a8f0: 7d 0a 0a 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a }.../* Child */.
a900: 09 69 66 20 28 66 6f 72 6b 28 29 20 21 3d 20 30 .if (fork() != 0
a910: 29 20 7b 0a 09 09 2f 2a 20 43 68 69 6c 64 20 2a ) {.../* Child *
a920: 2f 0a 09 09 65 78 69 74 28 45 58 49 54 5f 53 55 /...exit(EXIT_SU
a930: 43 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 CCESS);..}.../*
a940: 47 72 61 6e 64 20 63 68 69 6c 64 20 2a 2f 0a 09 Grand child */..
a950: 73 65 74 73 69 64 5f 72 65 74 20 3d 20 73 65 74 setsid_ret = set
a960: 73 69 64 28 29 3b 0a 09 69 66 20 28 73 65 74 73 sid();..if (sets
a970: 69 64 5f 72 65 74 20 3d 3d 20 28 28 70 69 64 5f id_ret == ((pid_
a980: 74 29 20 2d 31 29 29 20 7b 0a 09 09 72 65 74 75 t) -1)) {...retu
a990: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66 64 5f 69 rn(1);..}...fd_i
a9a0: 6e 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e n = open("/dev/n
a9b0: 75 6c 6c 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b ull", O_RDONLY);
a9c0: 0a 09 66 64 5f 6f 75 74 20 3d 20 6f 70 65 6e 28 ..fd_out = open(
a9d0: 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 "/dev/null", O_W
a9e0: 52 4f 4e 4c 59 29 3b 0a 09 69 66 20 28 66 64 5f RONLY);..if (fd_
a9f0: 69 6e 20 3c 20 30 20 7c 7c 20 66 64 5f 6f 75 74 in < 0 || fd_out
aa00: 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e < 0) {...return
aa10: 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 (1);..}...dup2_r
aa20: 65 74 20 3d 20 64 75 70 32 28 66 64 5f 69 6e 2c et = dup2(fd_in,
aa30: 20 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 3b 0a STDIN_FILENO);.
aa40: 09 69 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d .if (dup2_ret !=
aa50: 20 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 20 7b STDIN_FILENO) {
aa60: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d ...return(1);..}
aa70: 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 ...dup2_ret = du
aa80: 70 32 28 66 64 5f 6f 75 74 2c 20 53 54 44 4f 55 p2(fd_out, STDOU
aa90: 54 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 T_FILENO);..if (
aaa0: 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 4f dup2_ret != STDO
aab0: 55 54 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 UT_FILENO) {...r
aac0: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 eturn(1);..}...d
aad0: 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 up2_ret = dup2(f
aae0: 64 5f 6f 75 74 2c 20 53 54 44 45 52 52 5f 46 49 d_out, STDERR_FI
aaf0: 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 LENO);..if (dup2
ab00: 5f 72 65 74 20 21 3d 20 53 54 44 45 52 52 5f 46 _ret != STDERR_F
ab10: 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 ILENO) {...retur
ab20: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 63 6c 6f 73 65 n(1);..}...close
ab30: 28 66 64 5f 69 6e 29 3b 0a 09 63 6c 6f 73 65 28 (fd_in);..close(
ab40: 66 64 5f 6f 75 74 29 3b 0a 0a 09 72 65 74 75 72 fd_out);...retur
ab50: 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 n(0);.}../* Run
ab60: 70 72 6f 63 65 73 73 20 2a 2f 0a 69 6e 74 20 6d process */.int m
ab70: 61 69 6e 28 69 6e 74 20 61 72 67 63 2c 20 63 68 ain(int argc, ch
ab80: 61 72 20 2a 2a 61 72 67 76 29 20 7b 0a 09 73 74 ar **argv) {..st
ab90: 72 75 63 74 20 6f 70 74 69 6f 6e 20 6f 70 74 69 ruct option opti
aba0: 6f 6e 73 5b 31 32 5d 3b 0a 09 73 74 72 75 63 74 ons[12];..struct
abb0: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 74 filed_options t
abc0: 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73 20 3d 20 hread_options =
abd0: 7b 30 7d 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 {0};..const char
abe0: 20 2a 62 69 6e 64 5f 61 64 64 72 20 3d 20 42 49 *bind_addr = BI
abf0: 4e 44 5f 41 44 44 52 2c 20 2a 6e 65 77 72 6f 6f ND_ADDR, *newroo
ac00: 74 20 3d 20 4e 55 4c 4c 2c 20 2a 6c 6f 67 5f 66 t = NULL, *log_f
ac10: 69 6c 65 20 3d 20 4c 4f 47 5f 46 49 4c 45 3b 0a ile = LOG_FILE;.
ac20: 09 46 49 4c 45 20 2a 6c 6f 67 5f 66 70 3b 0a 09 .FILE *log_fp;..
ac30: 75 69 64 5f 74 20 75 73 65 72 20 3d 20 30 3b 0a uid_t user = 0;.
ac40: 09 69 6e 74 20 70 6f 72 74 20 3d 20 50 4f 52 54 .int port = PORT
ac50: 2c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d , thread_count =
ac60: 20 54 48 52 45 41 44 5f 43 4f 55 4e 54 3b 0a 09 THREAD_COUNT;..
ac70: 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 20 3d int cache_size =
ac80: 20 43 41 43 48 45 5f 53 49 5a 45 3b 0a 09 69 6e CACHE_SIZE;..in
ac90: 74 20 69 6e 69 74 5f 72 65 74 2c 20 63 68 72 6f t init_ret, chro
aca0: 6f 74 5f 72 65 74 2c 20 73 65 74 75 69 64 5f 72 ot_ret, setuid_r
acb0: 65 74 2c 20 6c 6f 6f 6b 75 70 5f 72 65 74 2c 20 et, lookup_ret,
acc0: 63 68 64 69 72 5f 72 65 74 3b 0a 09 69 6e 74 20 chdir_ret;..int
acd0: 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 20 3d setuid_enabled =
ace0: 20 30 2c 20 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 0, daemon_enabl
acf0: 65 64 20 3d 20 30 3b 0a 09 69 6e 74 20 63 68 3b ed = 0;..int ch;
ad00: 0a 09 69 6e 74 20 66 64 3b 0a 0a 09 2f 2a 20 50 ..int fd;.../* P
ad10: 72 6f 63 65 73 73 20 61 72 67 75 6d 65 6e 74 73 rocess arguments
ad20: 20 2a 2f 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 */..filed_getop
ad30: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f t_long_setopt(&o
ad40: 70 74 69 6f 6e 73 5b 30 5d 2c 20 22 70 6f 72 74 ptions[0], "port
ad50: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 ", required_argu
ad60: 6d 65 6e 74 2c 20 27 70 27 29 3b 0a 09 66 69 6c ment, 'p');..fil
ad70: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 ed_getopt_long_s
ad80: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 etopt(&options[1
ad90: 5d 2c 20 22 74 68 72 65 61 64 73 22 2c 20 72 65 ], "threads", re
ada0: 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c quired_argument,
adb0: 20 27 74 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 't');..filed_ge
adc0: 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 topt_long_setopt
add0: 28 26 6f 70 74 69 6f 6e 73 5b 32 5d 2c 20 22 63 (&options[2], "c
ade0: 61 63 68 65 22 2c 20 72 65 71 75 69 72 65 64 5f ache", required_
adf0: 61 72 67 75 6d 65 6e 74 2c 20 27 63 27 29 3b 0a argument, 'c');.
ae00: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
ae10: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
ae20: 6e 73 5b 33 5d 2c 20 22 62 69 6e 64 22 2c 20 72 ns[3], "bind", r
ae30: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 equired_argument
ae40: 2c 20 27 62 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 'b');..filed_g
ae50: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
ae60: 74 28 26 6f 70 74 69 6f 6e 73 5b 34 5d 2c 20 22 t(&options[4], "
ae70: 75 73 65 72 22 2c 20 72 65 71 75 69 72 65 64 5f user", required_
ae80: 61 72 67 75 6d 65 6e 74 2c 20 27 75 27 29 3b 0a argument, 'u');.
ae90: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
aea0: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
aeb0: 6e 73 5b 35 5d 2c 20 22 72 6f 6f 74 22 2c 20 72 ns[5], "root", r
aec0: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 equired_argument
aed0: 2c 20 27 72 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 'r');..filed_g
aee0: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
aef0: 74 28 26 6f 70 74 69 6f 6e 73 5b 36 5d 2c 20 22 t(&options[6], "
af00: 68 65 6c 70 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 help", no_argume
af10: 6e 74 2c 20 27 68 27 29 3b 0a 09 66 69 6c 65 64 nt, 'h');..filed
af20: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 _getopt_long_set
af30: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 37 5d 2c opt(&options[7],
af40: 20 22 64 61 65 6d 6f 6e 22 2c 20 6e 6f 5f 61 72 "daemon", no_ar
af50: 67 75 6d 65 6e 74 2c 20 27 64 27 29 3b 0a 09 66 gument, 'd');..f
af60: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
af70: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
af80: 5b 38 5d 2c 20 22 6c 6f 67 22 2c 20 72 65 71 75 [8], "log", requ
af90: 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 ired_argument, '
afa0: 6c 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f l');..filed_geto
afb0: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
afc0: 6f 70 74 69 6f 6e 73 5b 39 5d 2c 20 22 76 65 72 options[9], "ver
afd0: 73 69 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 sion", no_argume
afe0: 6e 74 2c 20 27 76 27 29 3b 0a 09 66 69 6c 65 64 nt, 'v');..filed
aff0: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 _getopt_long_set
b000: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 30 5d opt(&options[10]
b010: 2c 20 22 76 68 6f 73 74 22 2c 20 6e 6f 5f 61 72 , "vhost", no_ar
b020: 67 75 6d 65 6e 74 2c 20 27 56 27 29 3b 0a 09 66 gument, 'V');..f
b030: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
b040: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
b050: 5b 31 31 5d 2c 20 4e 55 4c 4c 2c 20 30 2c 20 30 [11], NULL, 0, 0
b060: 29 3b 0a 09 77 68 69 6c 65 20 28 28 63 68 20 3d );..while ((ch =
b070: 20 67 65 74 6f 70 74 5f 6c 6f 6e 67 28 61 72 67 getopt_long(arg
b080: 63 2c 20 61 72 67 76 2c 20 22 70 3a 74 3a 63 3a c, argv, "p:t:c:
b090: 62 3a 75 3a 72 3a 6c 3a 68 64 76 56 22 2c 20 6f b:u:r:l:hdvV", o
b0a0: 70 74 69 6f 6e 73 2c 20 4e 55 4c 4c 29 29 20 21 ptions, NULL)) !
b0b0: 3d 20 2d 31 29 20 7b 0a 09 09 73 77 69 74 63 68 = -1) {...switch
b0c0: 28 63 68 29 20 7b 0a 09 09 09 63 61 73 65 20 27 (ch) {....case '
b0d0: 70 27 3a 0a 09 09 09 09 70 6f 72 74 20 3d 20 61 p':.....port = a
b0e0: 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 toi(optarg);....
b0f0: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
b100: 27 74 27 3a 0a 09 09 09 09 74 68 72 65 61 64 5f 't':.....thread_
b110: 63 6f 75 6e 74 20 3d 20 61 74 6f 69 28 6f 70 74 count = atoi(opt
b120: 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b arg);.....break;
b130: 0a 09 09 09 63 61 73 65 20 27 63 27 3a 0a 09 09 ....case 'c':...
b140: 09 09 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 61 ..cache_size = a
b150: 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 toi(optarg);....
b160: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
b170: 27 62 27 3a 0a 09 09 09 09 62 69 6e 64 5f 61 64 'b':.....bind_ad
b180: 64 72 20 3d 20 73 74 72 64 75 70 28 6f 70 74 61 dr = strdup(opta
b190: 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a rg);.....break;.
b1a0: 09 09 09 63 61 73 65 20 27 75 27 3a 0a 09 09 09 ...case 'u':....
b1b0: 09 73 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 20 .setuid_enabled
b1c0: 3d 20 31 3b 0a 09 09 09 09 6c 6f 6f 6b 75 70 5f = 1;.....lookup_
b1d0: 72 65 74 20 3d 20 66 69 6c 65 64 5f 75 73 65 72 ret = filed_user
b1e0: 5f 6c 6f 6f 6b 75 70 28 6f 70 74 61 72 67 2c 20 _lookup(optarg,
b1f0: 26 75 73 65 72 29 3b 0a 09 09 09 09 69 66 20 28 &user);.....if (
b200: 6c 6f 6f 6b 75 70 5f 72 65 74 20 21 3d 20 30 29 lookup_ret != 0)
b210: 20 7b 0a 09 09 09 09 09 66 69 6c 65 64 5f 70 72 {......filed_pr
b220: 69 6e 74 5f 68 65 6c 70 28 73 74 64 65 72 72 2c int_help(stderr,
b230: 20 30 2c 20 22 49 6e 76 61 6c 69 64 20 75 73 65 0, "Invalid use
b240: 72 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64 22 rname specified"
b250: 29 3b 0a 0a 09 09 09 09 09 72 65 74 75 72 6e 28 );.......return(
b260: 31 29 3b 0a 09 09 09 09 7d 0a 09 09 09 09 62 72 1);.....}.....br
b270: 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 72 27 eak;....case 'r'
b280: 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 61 74 68 :.....filed_path
b290: 5f 74 72 61 6e 73 6c 61 74 65 5f 73 65 74 5f 72 _translate_set_r
b2a0: 6f 6f 74 28 6e 65 77 72 6f 6f 74 2c 20 26 74 68 oot(newroot, &th
b2b0: 72 65 61 64 5f 6f 70 74 69 6f 6e 73 2c 20 6f 70 read_options, op
b2c0: 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b targ);.....break
b2d0: 3b 0a 09 09 09 63 61 73 65 20 27 6c 27 3a 0a 09 ;....case 'l':..
b2e0: 09 09 09 6c 6f 67 5f 66 69 6c 65 20 3d 20 73 74 ...log_file = st
b2f0: 72 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 rdup(optarg);...
b300: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
b310: 20 27 64 27 3a 0a 09 09 09 09 64 61 65 6d 6f 6e 'd':.....daemon
b320: 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 _enabled = 1;...
b330: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
b340: 20 27 56 27 3a 0a 09 09 09 09 74 68 72 65 61 64 'V':.....thread
b350: 5f 6f 70 74 69 6f 6e 73 2e 76 68 6f 73 74 73 5f _options.vhosts_
b360: 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 0a 09 09 enabled = 1;....
b370: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
b380: 20 27 76 27 3a 0a 09 09 09 09 70 72 69 6e 74 66 'v':.....printf
b390: 28 22 66 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 ("filed version
b3a0: 25 73 5c 6e 22 2c 20 46 49 4c 45 44 5f 56 45 52 %s\n", FILED_VER
b3b0: 53 49 4f 4e 29 3b 0a 0a 09 09 09 09 72 65 74 75 SION);......retu
b3c0: 72 6e 28 30 29 3b 0a 09 09 09 63 61 73 65 20 27 rn(0);....case '
b3d0: 3f 27 3a 0a 09 09 09 63 61 73 65 20 27 3a 27 3a ?':....case ':':
b3e0: 0a 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 .....filed_print
b3f0: 5f 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c _help(stderr, 0,
b400: 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 NULL);......ret
b410: 75 72 6e 28 31 29 3b 0a 09 09 09 63 61 73 65 20 urn(1);....case
b420: 27 68 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 'h':.....filed_p
b430: 72 69 6e 74 5f 68 65 6c 70 28 73 74 64 6f 75 74 rint_help(stdout
b440: 2c 20 31 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 , 1, NULL);.....
b450: 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 7d 0a .return(0);...}.
b460: 09 7d 0a 0a 09 2f 2a 20 4f 70 65 6e 20 6c 6f 67 .}.../* Open log
b470: 20 66 69 6c 65 20 2a 2f 0a 09 6c 6f 67 5f 66 70 file */..log_fp
b480: 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 = filed_log_ope
b490: 6e 28 6c 6f 67 5f 66 69 6c 65 29 3b 0a 09 69 66 n(log_file);..if
b4a0: 20 28 6c 6f 67 5f 66 70 20 3d 3d 20 4e 55 4c 4c (log_fp == NULL
b4b0: 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 ) {...perror("fi
b4c0: 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 22 29 3b 0a led_log_open");.
b4d0: 0a 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09 7d ...return(4);..}
b4e0: 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 69 73 .../* Create lis
b4f0: 74 65 6e 69 6e 67 20 73 6f 63 6b 65 74 20 2a 2f tening socket */
b500: 0a 09 66 64 20 3d 20 66 69 6c 65 64 5f 6c 69 73 ..fd = filed_lis
b510: 74 65 6e 28 62 69 6e 64 5f 61 64 64 72 2c 20 70 ten(bind_addr, p
b520: 6f 72 74 29 3b 0a 09 69 66 20 28 66 64 20 3c 20 ort);..if (fd <
b530: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 0) {...perror("f
b540: 69 6c 65 64 5f 6c 69 73 74 65 6e 22 29 3b 0a 0a iled_listen");..
b550: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
b560: 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ../* Initialize
b570: 74 69 6d 65 6f 75 74 20 73 74 72 75 63 74 75 72 timeout structur
b580: 65 73 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 es */..init_ret
b590: 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 = filed_socketti
b5a0: 6d 65 6f 75 74 5f 69 6e 69 74 28 29 3b 0a 09 69 meout_init();..i
b5b0: 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 f (init_ret != 0
b5c0: 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 ) {...perror("fi
b5d0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
b5e0: 74 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 t_init");....ret
b5f0: 75 72 6e 28 38 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 urn(8);..}.../*
b600: 42 65 63 6f 6d 65 20 61 20 64 61 65 6d 6f 6e 20 Become a daemon
b610: 2a 2f 0a 09 69 66 20 28 64 61 65 6d 6f 6e 5f 65 */..if (daemon_e
b620: 6e 61 62 6c 65 64 29 20 7b 0a 09 09 69 6e 69 74 nabled) {...init
b630: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 64 61 65 _ret = filed_dae
b640: 6d 6f 6e 69 7a 65 28 29 3b 0a 09 09 69 66 20 28 monize();...if (
b650: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b init_ret != 0) {
b660: 0a 09 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 ....perror("file
b670: 64 5f 64 61 65 6d 6f 6e 69 7a 65 22 29 3b 0a 0a d_daemonize");..
b680: 09 09 09 72 65 74 75 72 6e 28 36 29 3b 0a 09 09 ...return(6);...
b690: 7d 0a 09 7d 0a 0a 09 2f 2a 20 43 68 72 6f 6f 74 }..}.../* Chroot
b6a0: 2c 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 , if appropriate
b6b0: 20 2a 2f 0a 09 69 66 20 28 6e 65 77 72 6f 6f 74 */..if (newroot
b6c0: 29 20 7b 0a 09 09 63 68 64 69 72 5f 72 65 74 20 ) {...chdir_ret
b6d0: 3d 20 63 68 64 69 72 28 6e 65 77 72 6f 6f 74 29 = chdir(newroot)
b6e0: 3b 0a 09 09 69 66 20 28 63 68 64 69 72 5f 72 65 ;...if (chdir_re
b6f0: 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 t != 0) {....per
b700: 72 6f 72 28 22 63 68 64 69 72 22 29 3b 0a 0a 09 ror("chdir");...
b710: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d ..return(1);...}
b720: 0a 0a 09 09 63 68 72 6f 6f 74 5f 72 65 74 20 3d ....chroot_ret =
b730: 20 63 68 72 6f 6f 74 28 22 2e 22 29 3b 0a 09 09 chroot(".");...
b740: 69 66 20 28 63 68 72 6f 6f 74 5f 72 65 74 20 21 if (chroot_ret !
b750: 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 = 0) {....perror
b760: 28 22 63 68 72 6f 6f 74 22 29 3b 0a 0a 09 09 09 ("chroot");.....
b770: 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 return(1);...}..
b780: 7d 0a 0a 09 2f 2a 20 44 72 6f 70 20 70 72 69 76 }.../* Drop priv
b790: 69 6c 65 67 65 73 2c 20 69 66 20 61 70 70 72 6f ileges, if appro
b7a0: 70 72 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 73 priate */..if (s
b7b0: 65 74 75 69 64 5f 65 6e 61 62 6c 65 64 29 20 7b etuid_enabled) {
b7c0: 0a 09 09 73 65 74 75 69 64 5f 72 65 74 20 3d 20 ...setuid_ret =
b7d0: 73 65 74 75 69 64 28 75 73 65 72 29 3b 0a 09 09 setuid(user);...
b7e0: 69 66 20 28 73 65 74 75 69 64 5f 72 65 74 20 21 if (setuid_ret !
b7f0: 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 = 0) {....perror
b800: 28 22 73 65 74 75 69 64 22 29 3b 0a 0a 09 09 09 ("setuid");.....
b810: 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 7d 0a 09 return(1);...}..
b820: 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a }.../* Initializ
b830: 65 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d e */..init_ret =
b840: 20 66 69 6c 65 64 5f 69 6e 69 74 28 63 61 63 68 filed_init(cach
b850: 65 5f 73 69 7a 65 29 3b 0a 09 69 66 20 28 69 6e e_size);..if (in
b860: 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 it_ret != 0) {..
b870: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 69 .perror("filed_i
b880: 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e nit");....return
b890: 28 33 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 (3);..}.../* Cre
b8a0: 61 74 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 ate logging thre
b8b0: 61 64 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 ad */..init_ret
b8c0: 3d 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f = filed_logging_
b8d0: 74 68 72 65 61 64 5f 69 6e 69 74 28 6c 6f 67 5f thread_init(log_
b8e0: 66 70 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 fp);..if (init_r
b8f0: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 et != 0) {...per
b900: 72 6f 72 28 22 66 69 6c 65 64 5f 6c 6f 67 67 69 ror("filed_loggi
b910: 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 22 29 ng_thread_init")
b920: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 34 29 3b 0a ;....return(4);.
b930: 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 73 .}.../* Create s
b940: 6f 63 6b 65 74 20 74 65 72 6d 69 6e 61 74 69 6f ocket terminatio
b950: 6e 20 74 68 72 65 61 64 20 2a 2f 0a 09 69 6e 69 n thread */..ini
b960: 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 73 6f t_ret = filed_so
b970: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 ckettimeout_thre
b980: 61 64 5f 69 6e 69 74 28 29 3b 0a 09 69 66 20 28 ad_init();..if (
b990: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b init_ret != 0) {
b9a0: 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 ...perror("filed
b9b0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 _sockettimeout_t
b9c0: 68 72 65 61 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 hread_init");...
b9d0: 09 72 65 74 75 72 6e 28 37 29 3b 0a 09 7d 0a 0a .return(7);..}..
b9e0: 09 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 6b 65 ./* Create worke
b9f0: 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 09 69 6e r threads */..in
ba00: 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 77 it_ret = filed_w
ba10: 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e orker_threads_in
ba20: 69 74 28 66 64 2c 20 74 68 72 65 61 64 5f 63 6f it(fd, thread_co
ba30: 75 6e 74 2c 20 26 74 68 72 65 61 64 5f 6f 70 74 unt, &thread_opt
ba40: 69 6f 6e 73 29 3b 0a 09 69 66 20 28 69 6e 69 74 ions);..if (init
ba50: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 _ret != 0) {...p
ba60: 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 77 6f 72 error("filed_wor
ba70: 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 ker_threads_init
ba80: 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 35 29 ");....return(5)
ba90: 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 61 69 74 20 66 ;..}.../* Wait f
baa0: 6f 72 20 74 68 72 65 61 64 73 20 74 6f 20 65 78 or threads to ex
bab0: 69 74 20 2a 2f 0a 09 2f 2a 20 58 58 58 3a 54 4f it */../* XXX:TO
bac0: 44 4f 3a 20 4d 6f 6e 69 74 6f 72 20 74 68 72 65 DO: Monitor thre
bad0: 61 64 20 75 73 61 67 65 20 2a 2f 0a 09 77 68 69 ad usage */..whi
bae0: 6c 65 20 28 31 29 20 7b 0a 09 09 73 6c 65 65 70 le (1) {...sleep
baf0: 28 38 36 34 30 30 29 3b 0a 09 7d 0a 0a 09 2f 2a (86400);..}.../*
bb00: 20 52 65 74 75 72 6e 20 69 6e 20 66 61 69 6c 75 Return in failu
bb10: 72 65 20 2a 2f 0a 09 72 65 74 75 72 6e 28 32 29 re */..return(2)
bb20: 3b 0a 7d 0a ;.}.