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 63 6f 6e 6e 65 63 74 74 69 6d ime_t connecttim
12d0: 65 3b 0a 09 74 69 6d 65 5f 74 20 73 74 61 72 74 e;..time_t start
12e0: 74 69 6d 65 3b 0a 09 74 69 6d 65 5f 74 20 65 6e time;..time_t en
12f0: 64 74 69 6d 65 3b 0a 09 6f 66 66 5f 74 20 72 65 dtime;..off_t re
1300: 71 5f 6f 66 66 73 65 74 3b 0a 09 6f 66 66 5f 74 q_offset;..off_t
1310: 20 72 65 71 5f 6c 65 6e 67 74 68 3b 0a 09 6f 66 req_length;..of
1320: 66 5f 74 20 73 65 6e 74 5f 6c 65 6e 67 74 68 3b f_t sent_length;
1330: 0a 09 6f 66 66 5f 74 20 66 69 6c 65 5f 6c 65 6e ..off_t file_len
1340: 67 74 68 3b 0a 09 63 68 61 72 20 69 70 5b 31 32 gth;..char ip[12
1350: 38 5d 3b 0a 09 69 6e 74 20 70 6f 72 74 3b 0a 09 8];..int port;..
1360: 69 6e 74 20 6d 65 74 68 6f 64 3b 0a 7d 3b 0a 0a int method;.};..
1370: 2f 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 /* Global variab
1380: 6c 65 73 20 2a 2f 0a 2f 2a 2a 20 4f 70 65 6e 20 les */./** Open
1390: 46 69 6c 65 20 63 61 63 68 65 20 2a 2a 2f 0a 73 File cache **/.s
13a0: 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 truct filed_file
13b0: 69 6e 66 6f 20 2a 66 69 6c 65 64 5f 66 69 6c 65 info *filed_file
13c0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d 20 4e info_fdcache = N
13d0: 55 4c 4c 3b 0a 75 6e 73 69 67 6e 65 64 20 69 6e ULL;.unsigned in
13e0: 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f t filed_fileinfo
13f0: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 _fdcache_size =
1400: 30 3b 0a 0a 2f 2a 2a 20 4c 6f 67 67 69 6e 67 20 0;../** Logging
1410: 2a 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 **/.struct filed
1420: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 66 69 6c 65 _log_entry *file
1430: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a d_log_msg_list;.
1440: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 pthread_mutex_t
1450: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
1460: 73 74 5f 6d 75 74 65 78 3b 0a 70 74 68 72 65 61 st_mutex;.pthrea
1470: 64 5f 63 6f 6e 64 5f 74 20 66 69 6c 65 64 5f 6c d_cond_t filed_l
1480: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 og_msg_list_read
1490: 79 3b 0a 0a 2f 2a 20 53 69 67 6e 61 6c 20 48 61 y;../* Signal Ha
14a0: 6e 64 6c 65 72 20 2a 2f 0a 73 74 61 74 69 63 20 ndler */.static
14b0: 76 6f 69 64 20 66 69 6c 65 64 5f 73 69 67 6e 61 void filed_signa
14c0: 6c 5f 68 61 6e 64 6c 65 72 28 69 6e 74 20 73 69 l_handler(int si
14d0: 67 6e 61 6c 5f 6e 75 6d 62 65 72 29 20 7b 0a 09 gnal_number) {..
14e0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c struct filed_fil
14f0: 65 69 6e 66 6f 20 2a 63 61 63 68 65 3b 0a 09 75 einfo *cache;..u
1500: 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 64 78 3b nsigned int idx;
1510: 0a 0a 09 73 77 69 74 63 68 20 28 73 69 67 6e 61 ...switch (signa
1520: 6c 5f 6e 75 6d 62 65 72 29 20 7b 0a 09 09 63 61 l_number) {...ca
1530: 73 65 20 53 49 47 48 55 50 3a 0a 09 09 09 66 6f se SIGHUP:....fo
1540: 72 20 28 69 64 78 20 3d 20 30 3b 20 69 64 78 20 r (idx = 0; idx
1550: 3c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f < filed_fileinfo
1560: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 3b 20 69 _fdcache_size; i
1570: 64 78 2b 2b 29 20 7b 0a 09 09 09 09 63 61 63 68 dx++) {.....cach
1580: 65 20 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 e = &filed_filei
1590: 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d nfo_fdcache[idx]
15a0: 3b 0a 0a 09 09 09 09 70 74 68 72 65 61 64 5f 6d ;......pthread_m
15b0: 75 74 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 65 utex_lock(&cache
15c0: 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09 09 63 ->mutex);......c
15d0: 61 63 68 65 2d 3e 70 61 74 68 5b 30 5d 20 3d 20 ache->path[0] =
15e0: 27 5c 30 27 3b 0a 09 09 09 09 69 66 20 28 63 61 '\0';.....if (ca
15f0: 63 68 65 2d 3e 66 64 20 3e 3d 20 30 29 20 7b 0a che->fd >= 0) {.
1600: 09 09 09 09 09 63 6c 6f 73 65 28 63 61 63 68 65 .....close(cache
1610: 2d 3e 66 64 29 3b 0a 0a 09 09 09 09 09 63 61 63 ->fd);.......cac
1620: 68 65 2d 3e 66 64 20 3d 20 2d 31 3b 0a 09 09 09 he->fd = -1;....
1630: 09 7d 0a 0a 09 09 09 09 63 61 63 68 65 2d 3e 6c .}......cache->l
1640: 61 73 74 6d 6f 64 20 3d 20 22 22 3b 0a 09 09 09 astmod = "";....
1650: 09 63 61 63 68 65 2d 3e 74 79 70 65 20 3d 20 22 .cache->type = "
1660: 22 3b 0a 0a 09 09 09 09 70 74 68 72 65 61 64 5f ";......pthread_
1670: 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 mutex_unlock(&ca
1680: 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 09 09 09 che->mutex);....
1690: 7d 0a 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a 0a }....break;..}..
16a0: 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 49 .return;.}../* I
16b0: 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 20 nitialize cache
16c0: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
16d0: 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 65 28 75 led_init_cache(u
16e0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63 68 nsigned int cach
16f0: 65 5f 73 69 7a 65 29 20 7b 0a 09 75 6e 73 69 67 e_size) {..unsig
1700: 6e 65 64 20 69 6e 74 20 69 64 78 3b 0a 09 69 6e ned int idx;..in
1710: 74 20 6d 75 74 65 78 5f 69 6e 69 74 5f 72 65 74 t mutex_init_ret
1720: 3b 0a 0a 09 2f 2a 20 43 61 63 68 65 20 6d 61 79 ;.../* Cache may
1730: 20 6e 6f 74 20 62 65 20 72 65 2d 69 6e 69 74 69 not be re-initi
1740: 61 6c 69 7a 65 64 20 2a 2f 0a 09 69 66 20 28 66 alized */..if (f
1750: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
1760: 63 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 30 20 cache_size != 0
1770: 7c 7c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 || filed_fileinf
1780: 6f 5f 66 64 63 61 63 68 65 20 21 3d 20 4e 55 4c o_fdcache != NUL
1790: 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 L) {...return(1)
17a0: 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 61 63 68 65 20 ;..}.../* Cache
17b0: 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
17c0: 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 66 be allocated if
17d0: 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 65 6e cache is not en
17e0: 61 62 6c 65 64 20 2a 2f 0a 09 69 66 20 28 63 61 abled */..if (ca
17f0: 63 68 65 5f 73 69 7a 65 20 3d 3d 20 30 29 20 7b che_size == 0) {
1800: 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d ...return(0);..}
1810: 0a 0a 09 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 63 .../* Allocate c
1820: 61 63 68 65 20 2a 2f 0a 09 66 69 6c 65 64 5f 66 ache */..filed_f
1830: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f ileinfo_fdcache_
1840: 73 69 7a 65 20 3d 20 63 61 63 68 65 5f 73 69 7a size = cache_siz
1850: 65 3b 0a 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e e;..filed_filein
1860: 66 6f 5f 66 64 63 61 63 68 65 20 3d 20 6d 61 6c fo_fdcache = mal
1870: 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 66 69 6c 65 loc(sizeof(*file
1880: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
1890: 68 65 29 20 2a 20 66 69 6c 65 64 5f 66 69 6c 65 he) * filed_file
18a0: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a info_fdcache_siz
18b0: 65 29 3b 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 e);..if (filed_f
18c0: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 20 ileinfo_fdcache
18d0: 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 == NULL) {...ret
18e0: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 urn(1);..}.../*
18f0: 49 6e 69 74 69 61 6c 69 7a 65 20 63 61 63 68 65 Initialize cache
1900: 20 65 6e 74 72 69 65 73 20 2a 2f 0a 09 66 6f 72 entries */..for
1910: 20 28 69 64 78 20 3d 20 30 3b 20 69 64 78 20 3c (idx = 0; idx <
1920: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f filed_fileinfo_
1930: 66 64 63 61 63 68 65 5f 73 69 7a 65 3b 20 69 64 fdcache_size; id
1940: 78 2b 2b 29 20 7b 0a 09 09 6d 75 74 65 78 5f 69 x++) {...mutex_i
1950: 6e 69 74 5f 72 65 74 20 3d 20 70 74 68 72 65 61 nit_ret = pthrea
1960: 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 66 69 d_mutex_init(&fi
1970: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
1980: 61 63 68 65 5b 69 64 78 5d 2e 6d 75 74 65 78 2c ache[idx].mutex,
1990: 20 4e 55 4c 4c 29 3b 0a 09 09 69 66 20 28 6d 75 NULL);...if (mu
19a0: 74 65 78 5f 69 6e 69 74 5f 72 65 74 20 21 3d 20 tex_init_ret !=
19b0: 30 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 31 0) {....return(1
19c0: 29 3b 0a 09 09 7d 0a 0a 09 09 66 69 6c 65 64 5f );...}....filed_
19d0: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
19e0: 5b 69 64 78 5d 2e 70 61 74 68 5b 30 5d 20 3d 20 [idx].path[0] =
19f0: 27 5c 30 27 3b 0a 09 09 66 69 6c 65 64 5f 66 69 '\0';...filed_fi
1a00: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 leinfo_fdcache[i
1a10: 64 78 5d 2e 66 64 20 3d 20 2d 31 3b 0a 09 09 66 dx].fd = -1;...f
1a20: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
1a30: 63 61 63 68 65 5b 69 64 78 5d 2e 6c 61 73 74 6d cache[idx].lastm
1a40: 6f 64 20 3d 20 22 22 3b 0a 09 09 66 69 6c 65 64 od = "";...filed
1a50: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 _fileinfo_fdcach
1a60: 65 5b 69 64 78 5d 2e 74 79 70 65 20 3d 20 22 22 e[idx].type = ""
1a70: 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 30 29 ;..}...return(0)
1a80: 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 ;.}../* Initiali
1a90: 7a 65 20 70 72 6f 63 65 73 73 20 2a 2f 0a 73 74 ze process */.st
1aa0: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 atic int filed_i
1ab0: 6e 69 74 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 nit(unsigned int
1ac0: 20 63 61 63 68 65 5f 73 69 7a 65 29 20 7b 0a 09 cache_size) {..
1ad0: 73 74 61 74 69 63 20 69 6e 74 20 63 61 6c 6c 65 static int calle
1ae0: 64 20 3d 20 30 3b 0a 09 73 74 72 75 63 74 20 73 d = 0;..struct s
1af0: 69 67 61 63 74 69 6f 6e 20 73 69 67 6e 61 6c 5f igaction signal_
1b00: 68 61 6e 64 6c 65 72 5f 69 6e 66 6f 3b 0a 09 73 handler_info;..s
1b10: 69 67 73 65 74 5f 74 20 73 69 67 6e 61 6c 5f 68 igset_t signal_h
1b20: 61 6e 64 6c 65 72 5f 6d 61 73 6b 3b 0a 09 73 73 andler_mask;..ss
1b30: 69 7a 65 5f 74 20 72 65 61 64 5f 72 65 74 20 3d ize_t read_ret =
1b40: 20 30 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 0;..unsigned in
1b50: 74 20 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 3d t random_value =
1b60: 20 30 3b 0a 09 69 6e 74 20 63 61 63 68 65 5f 72 0;..int cache_r
1b70: 65 74 3b 0a 09 69 6e 74 20 72 61 6e 64 6f 6d 5f et;..int random_
1b80: 66 64 3b 0a 0a 09 69 66 20 28 63 61 6c 6c 65 64 fd;...if (called
1b90: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b ) {...return(0);
1ba0: 0a 09 7d 0a 0a 09 63 61 6c 6c 65 64 20 3d 20 31 ..}...called = 1
1bb0: 3b 0a 0a 09 2f 2a 20 41 74 74 65 6d 70 74 20 74 ;.../* Attempt t
1bc0: 6f 20 6c 6f 63 6b 20 61 6c 6c 20 6d 65 6d 6f 72 o lock all memor
1bd0: 79 20 74 6f 20 70 68 79 73 69 63 61 6c 20 52 41 y to physical RA
1be0: 4d 20 28 62 75 74 20 64 6f 6e 27 74 20 63 61 72 M (but don't car
1bf0: 65 20 69 66 20 77 65 20 63 61 6e 27 74 29 20 2a e if we can't) *
1c00: 2f 0a 09 6d 6c 6f 63 6b 61 6c 6c 28 4d 43 4c 5f /..mlockall(MCL_
1c10: 43 55 52 52 45 4e 54 20 7c 20 4d 43 4c 5f 46 55 CURRENT | MCL_FU
1c20: 54 55 52 45 29 3b 0a 0a 09 2f 2a 20 45 73 74 61 TURE);.../* Esta
1c30: 62 6c 69 73 68 20 73 69 67 6e 61 6c 20 68 61 6e blish signal han
1c40: 64 6c 65 72 73 20 2a 2f 0a 09 2f 2a 20 53 49 47 dlers */../* SIG
1c50: 50 49 50 45 20 73 68 6f 75 6c 64 20 69 6e 74 65 PIPE should inte
1c60: 72 72 75 70 74 20 73 79 73 74 65 6d 20 63 61 6c rrupt system cal
1c70: 6c 73 20 2a 2f 0a 09 73 69 67 66 69 6c 6c 73 65 ls */..sigfillse
1c80: 74 28 26 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 t(&signal_handle
1c90: 72 5f 6d 61 73 6b 29 3b 0a 09 73 69 67 6e 61 6c r_mask);..signal
1ca0: 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 6f 2e 73 61 _handler_info.sa
1cb0: 5f 68 61 6e 64 6c 65 72 20 3d 20 66 69 6c 65 64 _handler = filed
1cc0: 5f 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 3b _signal_handler;
1cd0: 0a 09 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 ..signal_handler
1ce0: 5f 69 6e 66 6f 2e 73 61 5f 6d 61 73 6b 20 3d 20 _info.sa_mask =
1cf0: 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 6d signal_handler_m
1d00: 61 73 6b 3b 0a 09 73 69 67 6e 61 6c 5f 68 61 6e ask;..signal_han
1d10: 64 6c 65 72 5f 69 6e 66 6f 2e 73 61 5f 66 6c 61 dler_info.sa_fla
1d20: 67 73 20 3d 20 53 41 5f 52 45 53 54 41 52 54 3b gs = SA_RESTART;
1d30: 0a 09 73 69 67 61 63 74 69 6f 6e 28 53 49 47 50 ..sigaction(SIGP
1d40: 49 50 45 2c 20 26 73 69 67 6e 61 6c 5f 68 61 6e IPE, &signal_han
1d50: 64 6c 65 72 5f 69 6e 66 6f 2c 20 4e 55 4c 4c 29 dler_info, NULL)
1d60: 3b 0a 0a 09 2f 2a 20 48 61 6e 64 6c 65 20 53 49 ;.../* Handle SI
1d70: 47 48 55 50 20 74 6f 20 72 65 6c 65 61 73 65 20 GHUP to release
1d80: 61 6c 6c 20 63 61 63 68 65 73 20 2a 2f 0a 09 73 all caches */..s
1d90: 69 67 66 69 6c 6c 73 65 74 28 26 73 69 67 6e 61 igfillset(&signa
1da0: 6c 5f 68 61 6e 64 6c 65 72 5f 6d 61 73 6b 29 3b l_handler_mask);
1db0: 0a 09 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 ..signal_handler
1dc0: 5f 69 6e 66 6f 2e 73 61 5f 68 61 6e 64 6c 65 72 _info.sa_handler
1dd0: 20 3d 20 66 69 6c 65 64 5f 73 69 67 6e 61 6c 5f = filed_signal_
1de0: 68 61 6e 64 6c 65 72 3b 0a 09 73 69 67 6e 61 6c handler;..signal
1df0: 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 6f 2e 73 61 _handler_info.sa
1e00: 5f 6d 61 73 6b 20 3d 20 73 69 67 6e 61 6c 5f 68 _mask = signal_h
1e10: 61 6e 64 6c 65 72 5f 6d 61 73 6b 3b 0a 09 73 69 andler_mask;..si
1e20: 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 5f 69 6e 66 gnal_handler_inf
1e30: 6f 2e 73 61 5f 66 6c 61 67 73 20 3d 20 30 3b 0a o.sa_flags = 0;.
1e40: 09 73 69 67 61 63 74 69 6f 6e 28 53 49 47 48 55 .sigaction(SIGHU
1e50: 50 2c 20 26 73 69 67 6e 61 6c 5f 68 61 6e 64 6c P, &signal_handl
1e60: 65 72 5f 69 6e 66 6f 2c 20 4e 55 4c 4c 29 3b 0a er_info, NULL);.
1e70: 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ../* Initialize
1e80: 63 61 63 68 65 20 73 74 72 75 63 74 75 72 65 20 cache structure
1e90: 2a 2f 0a 09 63 61 63 68 65 5f 72 65 74 20 3d 20 */..cache_ret =
1ea0: 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 65 filed_init_cache
1eb0: 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 (cache_size);..i
1ec0: 66 20 28 63 61 63 68 65 5f 72 65 74 20 21 3d 20 f (cache_ret !=
1ed0: 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 63 61 0) {...return(ca
1ee0: 63 68 65 5f 72 65 74 29 3b 0a 09 7d 0a 0a 09 2f che_ret);..}.../
1ef0: 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 72 61 6e * Initialize ran
1f00: 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 dom number gener
1f10: 61 74 6f 72 20 2a 2f 0a 09 72 61 6e 64 6f 6d 5f ator */..random_
1f20: 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f fd = open("/dev/
1f30: 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e urandom", O_RDON
1f40: 4c 59 29 3b 0a 09 69 66 20 28 72 61 6e 64 6f 6d LY);..if (random
1f50: 5f 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09 72 65 _fd >= 0) {...re
1f60: 61 64 5f 72 65 74 20 3d 20 72 65 61 64 28 72 61 ad_ret = read(ra
1f70: 6e 64 6f 6d 5f 66 64 2c 20 26 72 61 6e 64 6f 6d ndom_fd, &random
1f80: 5f 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66 28 72 _value, sizeof(r
1f90: 61 6e 64 6f 6d 5f 76 61 6c 75 65 29 29 3b 0a 0a andom_value));..
1fa0: 09 09 63 6c 6f 73 65 28 72 61 6e 64 6f 6d 5f 66 ..close(random_f
1fb0: 64 29 3b 0a 09 7d 0a 0a 09 72 61 6e 64 6f 6d 5f d);..}...random_
1fc0: 76 61 6c 75 65 20 5e 3d 20 67 65 74 70 69 64 28 value ^= getpid(
1fd0: 29 3b 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 );..random_value
1fe0: 20 5e 3d 20 67 65 74 75 69 64 28 29 3b 0a 09 72 ^= getuid();..r
1ff0: 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 74 andom_value ^= t
2000: 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 73 72 61 ime(NULL);...sra
2010: 6e 64 6f 6d 28 72 61 6e 64 6f 6d 5f 76 61 6c 75 ndom(random_valu
2020: 65 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b e);...return(0);
2030: 0a 0a 09 2f 2a 20 4e 4f 54 52 45 41 43 48 3a 20 .../* NOTREACH:
2040: 52 65 61 64 20 6d 61 79 20 66 61 69 6c 20 6f 72 Read may fail or
2050: 20 73 75 63 63 65 65 64 2c 20 77 65 20 64 6f 6e succeed, we don
2060: 27 74 20 61 63 74 75 61 6c 6c 79 20 63 61 72 65 't actually care
2070: 20 2a 2f 0a 09 72 65 61 64 5f 72 65 74 20 3d 20 */..read_ret =
2080: 72 65 61 64 5f 72 65 74 3b 0a 7d 0a 0a 2f 2a 20 read_ret;.}../*
2090: 4c 69 73 74 65 6e 20 6f 6e 20 61 20 70 61 72 74 Listen on a part
20a0: 69 63 75 6c 61 72 20 61 64 64 72 65 73 73 2f 70 icular address/p
20b0: 6f 72 74 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e ort */.static in
20c0: 74 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 63 t filed_listen(c
20d0: 6f 6e 73 74 20 63 68 61 72 20 2a 61 64 64 72 65 onst char *addre
20e0: 73 73 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 ss, unsigned int
20f0: 20 70 6f 72 74 29 20 7b 0a 09 73 74 72 75 63 74 port) {..struct
2100: 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 sockaddr_in6 ad
2110: 64 72 5f 76 36 3b 0a 09 73 74 72 75 63 74 20 73 dr_v6;..struct s
2120: 6f 63 6b 61 64 64 72 5f 69 6e 20 61 64 64 72 5f ockaddr_in addr_
2130: 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b v4;..struct sock
2140: 61 64 64 72 20 2a 61 64 64 72 3b 0a 09 73 6f 63 addr *addr;..soc
2150: 6b 6c 65 6e 5f 74 20 61 64 64 72 5f 6c 65 6e 3b klen_t addr_len;
2160: 0a 09 69 6e 74 20 70 74 6f 6e 5f 72 65 74 2c 20 ..int pton_ret,
2170: 62 69 6e 64 5f 72 65 74 2c 20 6c 69 73 74 65 6e bind_ret, listen
2180: 5f 72 65 74 3b 0a 09 69 6e 74 20 66 61 6d 69 6c _ret;..int famil
2190: 79 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 09 66 61 y;..int fd;...fa
21a0: 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 3b mily = AF_INET6;
21b0: 0a 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e 65 ..pton_ret = ine
21c0: 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 61 t_pton(family, a
21d0: 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f 76 36 ddress, &addr_v6
21e0: 2e 73 69 6e 36 5f 61 64 64 72 2e 73 36 5f 61 64 .sin6_addr.s6_ad
21f0: 64 72 29 3b 0a 09 69 66 20 28 70 74 6f 6e 5f 72 dr);..if (pton_r
2200: 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 66 61 6d et != 1) {...fam
2210: 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 3b 0a 09 ily = AF_INET;..
2220: 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e 65 74 .pton_ret = inet
2230: 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 61 64 _pton(family, ad
2240: 64 72 65 73 73 2c 20 26 61 64 64 72 5f 76 34 2e dress, &addr_v4.
2250: 73 69 6e 5f 61 64 64 72 2e 73 5f 61 64 64 72 29 sin_addr.s_addr)
2260: 3b 0a 09 09 69 66 20 28 70 74 6f 6e 5f 72 65 74 ;...if (pton_ret
2270: 20 21 3d 20 31 29 20 7b 0a 09 09 09 72 65 74 75 != 1) {....retu
2280: 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 0a 09 09 61 rn(-1);...}....a
2290: 64 64 72 5f 76 34 2e 73 69 6e 5f 66 61 6d 69 6c ddr_v4.sin_famil
22a0: 79 20 3d 20 66 61 6d 69 6c 79 3b 0a 09 09 61 64 y = family;...ad
22b0: 64 72 5f 76 34 2e 73 69 6e 5f 70 6f 72 74 20 3d dr_v4.sin_port =
22c0: 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a 09 htons(port);...
22d0: 09 61 64 64 72 20 3d 20 28 73 74 72 75 63 74 20 .addr = (struct
22e0: 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64 sockaddr *) &add
22f0: 72 5f 76 34 3b 0a 09 09 61 64 64 72 5f 6c 65 6e r_v4;...addr_len
2300: 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 5f 76 = sizeof(addr_v
2310: 34 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 4);..} else {...
2320: 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 61 6d addr_v6.sin6_fam
2330: 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 3b 0a ily = AF_INET6;.
2340: 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 ..addr_v6.sin6_f
2350: 6c 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a 09 09 61 lowinfo = 0;...a
2360: 64 64 72 5f 76 36 2e 73 69 6e 36 5f 73 63 6f 70 ddr_v6.sin6_scop
2370: 65 5f 69 64 20 3d 20 30 3b 0a 09 09 61 64 64 72 e_id = 0;...addr
2380: 5f 76 36 2e 73 69 6e 36 5f 70 6f 72 74 20 3d 20 _v6.sin6_port =
2390: 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a 09 09 htons(port);....
23a0: 61 64 64 72 20 3d 20 28 73 74 72 75 63 74 20 73 addr = (struct s
23b0: 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 ockaddr *) &addr
23c0: 5f 76 36 3b 0a 09 09 61 64 64 72 5f 6c 65 6e 20 _v6;...addr_len
23d0: 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 5f 76 36 = sizeof(addr_v6
23e0: 29 3b 0a 09 7d 0a 0a 09 66 64 20 3d 20 73 6f 63 );..}...fd = soc
23f0: 6b 65 74 28 66 61 6d 69 6c 79 2c 20 53 4f 43 4b ket(family, SOCK
2400: 5f 53 54 52 45 41 4d 2c 20 30 29 3b 0a 09 69 66 _STREAM, 0);..if
2410: 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 72 65 (fd < 0) {...re
2420: 74 75 72 6e 28 66 64 29 3b 0a 09 7d 0a 0a 09 62 turn(fd);..}...b
2430: 69 6e 64 5f 72 65 74 20 3d 20 62 69 6e 64 28 66 ind_ret = bind(f
2440: 64 2c 20 61 64 64 72 2c 20 61 64 64 72 5f 6c 65 d, addr, addr_le
2450: 6e 29 3b 0a 09 69 66 20 28 62 69 6e 64 5f 72 65 n);..if (bind_re
2460: 74 20 3c 20 30 29 20 7b 0a 09 09 63 6c 6f 73 65 t < 0) {...close
2470: 28 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 (fd);....return(
2480: 2d 31 29 3b 0a 09 7d 0a 0a 09 6c 69 73 74 65 6e -1);..}...listen
2490: 5f 72 65 74 20 3d 20 6c 69 73 74 65 6e 28 66 64 _ret = listen(fd
24a0: 2c 20 31 32 38 29 3b 0a 09 69 66 20 28 6c 69 73 , 128);..if (lis
24b0: 74 65 6e 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a ten_ret != 0) {.
24c0: 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a 09 09 ..close(fd);....
24d0: 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a return(-1);..}..
24e0: 09 72 65 74 75 72 6e 28 66 64 29 3b 0a 7d 0a 0a .return(fd);.}..
24f0: 2f 2a 20 4c 6f 67 20 61 20 6d 65 73 73 61 67 65 /* Log a message
2500: 20 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c 45 44 */.#ifdef FILED
2510: 5f 44 4f 4e 54 5f 4c 4f 47 0a 23 20 20 64 65 66 _DONT_LOG.# def
2520: 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e ine filed_loggin
2530: 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 78 29 g_thread_init(x)
2540: 20 30 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 0.# define fil
2550: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
2560: 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 (x, ...) /**/.#
2570: 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f define filed_lo
2580: 67 5f 6d 73 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a g_msg(x, ...) /*
2590: 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c */.# define fil
25a0: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 78 29 20 ed_log_entry(x)
25b0: 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 /**/.# define f
25c0: 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 78 2c 20 2e iled_log_ip(x, .
25d0: 2e 2e 29 20 4e 55 4c 4c 0a 23 20 20 64 65 66 69 ..) NULL.# defi
25e0: 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 ne filed_log_new
25f0: 28 78 29 20 26 6c 6f 63 61 6c 5f 64 75 6d 6d 79 (x) &local_dummy
2600: 5f 6c 6f 67 0a 23 20 20 64 65 66 69 6e 65 20 66 _log.# define f
2610: 69 6c 65 64 5f 6c 6f 67 5f 66 72 65 65 28 78 29 iled_log_free(x)
2620: 20 2f 2a 2a 2f 0a 0a 2f 2a 20 52 65 74 75 72 6e /**/../* Return
2630: 20 6c 6f 67 67 69 6e 67 20 68 61 6e 64 6c 65 20 logging handle
2640: 2a 2f 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a */.static FILE *
2650: 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 63 filed_log_open(c
2660: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 29 onst char *file)
2670: 20 7b 0a 09 72 65 74 75 72 6e 28 73 74 64 6f 75 {..return(stdou
2680: 74 29 3b 0a 09 66 69 6c 65 20 3d 20 66 69 6c 65 t);..file = file
2690: 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 ;.}.#else.# def
26a0: 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 66 72 ine filed_log_fr
26b0: 65 65 28 78 29 20 66 72 65 65 28 78 29 0a 23 20 ee(x) free(x).#
26c0: 20 69 66 64 65 66 20 46 49 4c 45 44 5f 44 45 42 ifdef FILED_DEB
26d0: 55 47 0a 23 20 20 20 20 64 65 66 69 6e 65 20 66 UG.# define f
26e0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
26f0: 75 67 28 78 2c 20 2e 2e 2e 29 20 7b 20 66 70 72 ug(x, ...) { fpr
2700: 69 6e 74 66 28 73 74 64 65 72 72 2c 20 78 2c 20 intf(stderr, x,
2710: 5f 5f 56 41 5f 41 52 47 53 5f 5f 29 3b 20 66 70 __VA_ARGS__); fp
2720: 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 5c rintf(stderr, "\
2730: 6e 22 29 3b 20 66 66 6c 75 73 68 28 73 74 64 65 n"); fflush(stde
2740: 72 72 29 3b 20 7d 0a 23 20 20 65 6c 73 65 0a 23 rr); }.# else.#
2750: 20 20 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 define filed
2760: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 78 _log_msg_debug(x
2770: 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 20 65 , ...) /**/.# e
2780: 6e 64 69 66 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c ndif../* Initial
2790: 69 7a 65 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 ize logging thre
27a0: 61 64 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ad */.static voi
27b0: 64 20 2a 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 d *filed_logging
27c0: 5f 74 68 72 65 61 64 28 76 6f 69 64 20 2a 61 72 _thread(void *ar
27d0: 67 5f 70 29 20 7b 0a 09 73 74 72 75 63 74 20 66 g_p) {..struct f
27e0: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 iled_logging_thr
27f0: 65 61 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a 09 ead_args *arg;..
2800: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 struct filed_log
2810: 5f 65 6e 74 72 79 20 2a 63 75 72 72 2c 20 2a 70 _entry *curr, *p
2820: 72 65 76 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 rev;..const char
2830: 20 2a 6d 65 74 68 6f 64 3b 0a 09 74 69 6d 65 5f *method;..time_
2840: 74 20 6e 6f 77 3b 0a 09 46 49 4c 45 20 2a 66 70 t now;..FILE *fp
2850: 3b 0a 0a 09 61 72 67 20 3d 20 61 72 67 5f 70 3b ;...arg = arg_p;
2860: 0a 0a 09 66 70 20 3d 20 61 72 67 2d 3e 66 70 3b ...fp = arg->fp;
2870: 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 ...while (1) {..
2880: 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c .pthread_mutex_l
2890: 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d ock(&filed_log_m
28a0: 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a sg_list_mutex);.
28b0: 09 09 70 74 68 72 65 61 64 5f 63 6f 6e 64 5f 77 ..pthread_cond_w
28c0: 61 69 74 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d ait(&filed_log_m
28d0: 73 67 5f 6c 69 73 74 5f 72 65 61 64 79 2c 20 26 sg_list_ready, &
28e0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
28f0: 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 63 75 st_mutex);....cu
2900: 72 72 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d rr = filed_log_m
2910: 73 67 5f 6c 69 73 74 3b 0a 09 09 66 69 6c 65 64 sg_list;...filed
2920: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 _log_msg_list =
2930: 4e 55 4c 4c 3b 0a 0a 09 09 70 74 68 72 65 61 64 NULL;....pthread
2940: 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 _mutex_unlock(&f
2950: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 iled_log_msg_lis
2960: 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 6e 6f 77 t_mutex);....now
2970: 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a = time(NULL);..
2980: 09 09 70 72 65 76 20 3d 20 4e 55 4c 4c 3b 0a 09 ..prev = NULL;..
2990: 09 66 6f 72 20 28 3b 20 63 75 72 72 3b 20 63 75 .for (; curr; cu
29a0: 72 72 20 3d 20 63 75 72 72 2d 3e 5f 6e 65 78 74 rr = curr->_next
29b0: 29 20 7b 0a 09 09 09 63 75 72 72 2d 3e 5f 70 72 ) {....curr->_pr
29c0: 65 76 20 3d 20 70 72 65 76 3b 0a 0a 09 09 09 70 ev = prev;.....p
29d0: 72 65 76 20 3d 20 63 75 72 72 3b 0a 09 09 7d 0a rev = curr;...}.
29e0: 0a 09 09 63 75 72 72 20 3d 20 70 72 65 76 3b 0a ...curr = prev;.
29f0: 09 09 77 68 69 6c 65 20 28 63 75 72 72 29 20 7b ..while (curr) {
2a00: 0a 09 09 09 73 77 69 74 63 68 20 28 63 75 72 72 ....switch (curr
2a10: 2d 3e 74 79 70 65 29 20 7b 0a 09 09 09 09 63 61 ->type) {.....ca
2a20: 73 65 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 se FILED_LOG_TYP
2a30: 45 5f 4d 45 53 53 41 47 45 3a 0a 09 09 09 09 09 E_MESSAGE:......
2a40: 66 70 72 69 6e 74 66 28 66 70 2c 20 22 25 73 22 fprintf(fp, "%s"
2a50: 2c 20 63 75 72 72 2d 3e 62 75 66 66 65 72 29 3b , curr->buffer);
2a60: 0a 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 .......break;...
2a70: 09 09 63 61 73 65 20 46 49 4c 45 44 5f 4c 4f 47 ..case FILED_LOG
2a80: 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 52 3a 0a _TYPE_TRANSFER:.
2a90: 09 09 09 09 09 73 77 69 74 63 68 20 28 63 75 72 .....switch (cur
2aa0: 72 2d 3e 6d 65 74 68 6f 64 29 20 7b 0a 09 09 09 r->method) {....
2ab0: 09 09 09 63 61 73 65 20 46 49 4c 45 44 5f 52 45 ...case FILED_RE
2ac0: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 QUEST_METHOD_GET
2ad0: 3a 0a 09 09 09 09 09 09 09 6d 65 74 68 6f 64 3d :........method=
2ae0: 22 47 45 54 22 3b 0a 09 09 09 09 09 09 09 62 72 "GET";........br
2af0: 65 61 6b 3b 0a 09 09 09 09 09 09 63 61 73 65 20 eak;.......case
2b00: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 FILED_REQUEST_ME
2b10: 54 48 4f 44 5f 48 45 41 44 3a 0a 09 09 09 09 09 THOD_HEAD:......
2b20: 09 09 6d 65 74 68 6f 64 3d 22 48 45 41 44 22 3b ..method="HEAD";
2b30: 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 ........break;..
2b40: 09 09 09 09 09 64 65 66 61 75 6c 74 3a 0a 09 09 .....default:...
2b50: 09 09 09 09 09 6d 65 74 68 6f 64 3d 22 3c 75 6e .....method="<un
2b60: 6b 6e 6f 77 6e 3e 22 3b 0a 09 09 09 09 09 09 09 known>";........
2b70: 62 72 65 61 6b 3b 0a 09 09 09 09 09 7d 0a 0a 09 break;......}...
2b80: 09 09 09 09 69 66 20 28 63 75 72 72 2d 3e 65 6e ....if (curr->en
2b90: 64 74 69 6d 65 20 3d 3d 20 28 28 74 69 6d 65 5f dtime == ((time_
2ba0: 74 29 20 2d 31 29 29 20 7b 0a 09 09 09 09 09 09 t) -1)) {.......
2bb0: 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 curr->endtime =
2bc0: 6e 6f 77 3b 0a 09 09 09 09 09 7d 0a 0a 09 09 09 now;......}.....
2bd0: 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 54 ..fprintf(fp, "T
2be0: 52 41 4e 53 46 45 52 20 4d 45 54 48 4f 44 3d 25 RANSFER METHOD=%
2bf0: 73 20 50 41 54 48 3d 25 73 20 53 52 43 3d 25 73 s PATH=%s SRC=%s
2c00: 3a 25 69 20 43 4c 49 45 4e 54 2e 54 49 4d 45 2e :%i CLIENT.TIME.
2c10: 43 4f 4e 4e 45 43 54 3d 25 6c 6c 75 20 52 45 51 CONNECT=%llu REQ
2c20: 55 45 53 54 2e 54 49 4d 45 2e 53 54 41 52 54 3d UEST.TIME.START=
2c30: 25 6c 6c 75 20 52 45 51 55 45 53 54 2e 54 49 4d %llu REQUEST.TIM
2c40: 45 2e 45 4e 44 3d 25 6c 6c 75 20 43 4f 44 45 2e E.END=%llu CODE.
2c50: 56 41 4c 55 45 3d 25 75 20 43 4f 44 45 2e 52 45 VALUE=%u CODE.RE
2c60: 41 53 4f 4e 3d 25 73 20 52 45 51 55 45 53 54 2e ASON=%s REQUEST.
2c70: 4f 46 46 53 45 54 3d 25 6c 6c 75 20 52 45 51 55 OFFSET=%llu REQU
2c80: 45 53 54 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 20 EST.LENGTH=%llu
2c90: 46 49 4c 45 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 FILE.LENGTH=%llu
2ca0: 20 54 52 41 4e 53 46 45 52 2e 4c 45 4e 47 54 48 TRANSFER.LENGTH
2cb0: 3d 25 6c 6c 75 22 2c 0a 09 09 09 09 09 09 6d 65 =%llu",.......me
2cc0: 74 68 6f 64 2c 0a 09 09 09 09 09 09 63 75 72 72 thod,.......curr
2cd0: 2d 3e 62 75 66 66 65 72 2c 0a 09 09 09 09 09 09 ->buffer,.......
2ce0: 63 75 72 72 2d 3e 69 70 2c 20 63 75 72 72 2d 3e curr->ip, curr->
2cf0: 70 6f 72 74 2c 0a 09 09 09 09 09 09 28 75 6e 73 port,.......(uns
2d00: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
2d10: 20 63 75 72 72 2d 3e 63 6f 6e 6e 65 63 74 74 69 curr->connectti
2d20: 6d 65 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 me,.......(unsig
2d30: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 ned long long) c
2d40: 75 72 72 2d 3e 73 74 61 72 74 74 69 6d 65 2c 0a urr->starttime,.
2d50: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ......(unsigned
2d60: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d long long) curr-
2d70: 3e 65 6e 64 74 69 6d 65 2c 0a 09 09 09 09 09 09 >endtime,.......
2d80: 63 75 72 72 2d 3e 68 74 74 70 5f 63 6f 64 65 2c curr->http_code,
2d90: 20 63 75 72 72 2d 3e 72 65 61 73 6f 6e 2c 0a 09 curr->reason,..
2da0: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
2db0: 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e ong long) curr->
2dc0: 72 65 71 5f 6f 66 66 73 65 74 2c 0a 09 09 09 09 req_offset,.....
2dd0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
2de0: 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 72 65 71 long) curr->req
2df0: 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 09 09 09 28 _length,.......(
2e00: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
2e10: 6e 67 29 20 63 75 72 72 2d 3e 66 69 6c 65 5f 6c ng) curr->file_l
2e20: 65 6e 67 74 68 2c 0a 09 09 09 09 09 09 28 75 6e ength,.......(un
2e30: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
2e40: 29 20 63 75 72 72 2d 3e 73 65 6e 74 5f 6c 65 6e ) curr->sent_len
2e50: 67 74 68 0a 09 09 09 09 09 29 3b 0a 0a 09 09 09 gth......);.....
2e60: 09 09 62 72 65 61 6b 3b 0a 09 09 09 7d 0a 09 09 ..break;....}...
2e70: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 20 54 .fprintf(fp, " T
2e80: 48 52 45 41 44 3d 25 6c 6c 75 20 54 49 4d 45 3d HREAD=%llu TIME=
2e90: 25 6c 6c 75 5c 6e 22 2c 0a 09 09 09 09 28 75 6e %llu\n",.....(un
2ea0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
2eb0: 29 20 28 28 69 6e 74 70 74 72 5f 74 29 20 63 75 ) ((intptr_t) cu
2ec0: 72 72 2d 3e 74 68 72 65 61 64 29 2c 0a 09 09 09 rr->thread),....
2ed0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
2ee0: 6c 6f 6e 67 29 20 6e 6f 77 0a 09 09 09 29 3b 0a long) now....);.
2ef0: 09 09 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a ...fflush(fp);..
2f00: 09 09 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a ...prev = curr;.
2f10: 09 09 09 63 75 72 72 20 3d 20 63 75 72 72 2d 3e ...curr = curr->
2f20: 5f 70 72 65 76 3b 0a 0a 09 09 09 66 72 65 65 28 _prev;.....free(
2f30: 70 72 65 76 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 prev);...}..}...
2f40: 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 7d 0a return(NULL);.}.
2f50: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c .static void fil
2f60: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 73 74 72 ed_log_entry(str
2f70: 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e uct filed_log_en
2f80: 74 72 79 20 2a 65 6e 74 72 79 29 20 7b 0a 09 65 try *entry) {..e
2f90: 6e 74 72 79 2d 3e 74 68 72 65 61 64 20 3d 20 70 ntry->thread = p
2fa0: 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 0a thread_self();..
2fb0: 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c .pthread_mutex_l
2fc0: 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d ock(&filed_log_m
2fd0: 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a sg_list_mutex);.
2fe0: 0a 09 65 6e 74 72 79 2d 3e 5f 6e 65 78 74 20 3d ..entry->_next =
2ff0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c filed_log_msg_l
3000: 69 73 74 3b 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f ist;..filed_log_
3010: 6d 73 67 5f 6c 69 73 74 20 3d 20 65 6e 74 72 79 msg_list = entry
3020: 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 65 ;...pthread_mute
3030: 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f x_unlock(&filed_
3040: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 log_msg_list_mut
3050: 65 78 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 ex);...pthread_c
3060: 6f 6e 64 5f 73 69 67 6e 61 6c 28 26 66 69 6c 65 ond_signal(&file
3070: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 d_log_msg_list_r
3080: 65 61 64 79 29 3b 0a 0a 09 72 65 74 75 72 6e 3b eady);...return;
3090: 0a 7d 0a 0a 73 74 61 74 69 63 20 73 74 72 75 63 .}..static struc
30a0: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
30b0: 79 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 y *filed_log_new
30c0: 28 69 6e 74 20 69 6e 69 74 69 61 6c 69 7a 65 29 (int initialize)
30d0: 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 {..struct filed
30e0: 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 72 65 74 76 _log_entry *retv
30f0: 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d al;...retval = m
3100: 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 72 65 alloc(sizeof(*re
3110: 74 76 61 6c 29 29 3b 0a 0a 09 69 66 20 28 69 6e tval));...if (in
3120: 69 74 69 61 6c 69 7a 65 29 20 7b 0a 09 09 72 65 itialize) {...re
3130: 74 76 61 6c 2d 3e 62 75 66 66 65 72 5b 30 5d 20 tval->buffer[0]
3140: 3d 20 27 5c 30 27 3b 0a 09 09 72 65 74 76 61 6c = '\0';...retval
3150: 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 2d 31 ->http_code = -1
3160: 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 63 6f 6e 6e ;...retval->conn
3170: 65 63 74 74 69 6d 65 20 3d 20 30 3b 0a 09 09 72 ecttime = 0;...r
3180: 65 74 76 61 6c 2d 3e 73 74 61 72 74 74 69 6d 65 etval->starttime
3190: 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e = 0;...retval->
31a0: 65 6e 64 74 69 6d 65 20 3d 20 30 3b 0a 09 09 72 endtime = 0;...r
31b0: 65 74 76 61 6c 2d 3e 72 65 71 5f 6f 66 66 73 65 etval->req_offse
31c0: 74 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d t = 0;...retval-
31d0: 3e 72 65 71 5f 6c 65 6e 67 74 68 20 3d 20 30 3b >req_length = 0;
31e0: 0a 09 09 72 65 74 76 61 6c 2d 3e 73 65 6e 74 5f ...retval->sent_
31f0: 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 length = 0;...re
3200: 74 76 61 6c 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 tval->file_lengt
3210: 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d h = 0;...retval-
3220: 3e 69 70 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 >ip[0] = '\0';..
3230: 09 72 65 74 76 61 6c 2d 3e 70 6f 72 74 20 3d 20 .retval->port =
3240: 2d 31 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 6d 65 -1;...retval->me
3250: 74 68 6f 64 20 3d 20 2d 31 3b 0a 09 7d 0a 0a 09 thod = -1;..}...
3260: 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a return(retval);.
3270: 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 }..static void f
3280: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 63 6f 6e iled_log_msg(con
3290: 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 2e 2e st char *fmt, ..
32a0: 2e 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c .) {..struct fil
32b0: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 65 6e ed_log_entry *en
32c0: 74 72 79 3b 0a 09 76 61 5f 6c 69 73 74 20 61 72 try;..va_list ar
32d0: 67 73 3b 0a 0a 09 65 6e 74 72 79 20 3d 20 66 69 gs;...entry = fi
32e0: 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 30 29 3b 0a led_log_new(0);.
32f0: 0a 09 76 61 5f 73 74 61 72 74 28 61 72 67 73 2c ..va_start(args,
3300: 20 66 6d 74 29 3b 0a 0a 09 76 73 6e 70 72 69 6e fmt);...vsnprin
3310: 74 66 28 65 6e 74 72 79 2d 3e 62 75 66 66 65 72 tf(entry->buffer
3320: 2c 20 73 69 7a 65 6f 66 28 65 6e 74 72 79 2d 3e , sizeof(entry->
3330: 62 75 66 66 65 72 29 2c 20 66 6d 74 2c 20 61 72 buffer), fmt, ar
3340: 67 73 29 3b 0a 0a 09 76 61 5f 65 6e 64 28 61 72 gs);...va_end(ar
3350: 67 73 29 3b 0a 0a 09 65 6e 74 72 79 2d 3e 74 79 gs);...entry->ty
3360: 70 65 20 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 pe = FILED_LOG_T
3370: 59 50 45 5f 4d 45 53 53 41 47 45 3b 0a 0a 09 66 YPE_MESSAGE;...f
3380: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 65 iled_log_entry(e
3390: 6e 74 72 79 29 3b 0a 0a 09 72 65 74 75 72 6e 3b ntry);...return;
33a0: 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .}..static const
33b0: 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 6c 6f 67 char *filed_log
33c0: 5f 69 70 28 73 74 72 75 63 74 20 73 6f 63 6b 61 _ip(struct socka
33d0: 64 64 72 20 2a 61 64 64 72 2c 20 63 68 61 72 20 ddr *addr, char
33e0: 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f 74 20 *buffer, size_t
33f0: 62 75 66 66 65 72 6c 65 6e 29 20 7b 0a 09 73 74 bufferlen) {..st
3400: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e ruct sockaddr_in
3410: 20 2a 61 64 64 72 5f 76 34 3b 0a 09 73 74 72 75 *addr_v4;..stru
3420: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 ct sockaddr_in6
3430: 2a 61 64 64 72 5f 76 36 3b 0a 09 63 6f 6e 73 74 *addr_v6;..const
3440: 20 63 68 61 72 20 2a 72 65 74 76 61 6c 20 3d 20 char *retval =
3450: 4e 55 4c 4c 3b 0a 0a 09 61 64 64 72 5f 76 36 20 NULL;...addr_v6
3460: 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 = (struct sockad
3470: 64 72 5f 69 6e 36 20 2a 29 20 61 64 64 72 3b 0a dr_in6 *) addr;.
3480: 0a 09 73 77 69 74 63 68 20 28 61 64 64 72 5f 76 ..switch (addr_v
3490: 36 2d 3e 73 69 6e 36 5f 66 61 6d 69 6c 79 29 20 6->sin6_family)
34a0: 7b 0a 09 09 63 61 73 65 20 41 46 5f 49 4e 45 54 {...case AF_INET
34b0: 3a 0a 09 09 09 61 64 64 72 5f 76 34 20 3d 20 28 :....addr_v4 = (
34c0: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f struct sockaddr_
34d0: 69 6e 20 2a 29 20 61 64 64 72 3b 0a 09 09 09 72 in *) addr;....r
34e0: 65 74 76 61 6c 20 3d 20 69 6e 65 74 5f 6e 74 6f etval = inet_nto
34f0: 70 28 41 46 5f 49 4e 45 54 2c 20 26 61 64 64 72 p(AF_INET, &addr
3500: 5f 76 34 2d 3e 73 69 6e 5f 61 64 64 72 2c 20 62 _v4->sin_addr, b
3510: 75 66 66 65 72 2c 20 62 75 66 66 65 72 6c 65 6e uffer, bufferlen
3520: 29 3b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 63 );....break;...c
3530: 61 73 65 20 41 46 5f 49 4e 45 54 36 3a 0a 09 09 ase AF_INET6:...
3540: 09 72 65 74 76 61 6c 20 3d 20 69 6e 65 74 5f 6e .retval = inet_n
3550: 74 6f 70 28 41 46 5f 49 4e 45 54 36 2c 20 26 61 top(AF_INET6, &a
3560: 64 64 72 5f 76 36 2d 3e 73 69 6e 36 5f 61 64 64 ddr_v6->sin6_add
3570: 72 2c 20 62 75 66 66 65 72 2c 20 62 75 66 66 65 r, buffer, buffe
3580: 72 6c 65 6e 29 3b 0a 09 09 09 62 72 65 61 6b 3b rlen);....break;
3590: 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 ..}...return(ret
35a0: 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 val);.}..static
35b0: 46 49 4c 45 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f FILE *filed_log_
35c0: 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 open(const char
35d0: 2a 66 69 6c 65 29 20 7b 0a 09 46 49 4c 45 20 2a *file) {..FILE *
35e0: 72 65 74 76 61 6c 3b 0a 0a 09 69 66 20 28 73 74 retval;...if (st
35f0: 72 63 6d 70 28 66 69 6c 65 2c 20 22 2d 22 29 20 rcmp(file, "-")
3600: 3d 3d 20 30 29 20 7b 0a 09 09 72 65 74 76 61 6c == 0) {...retval
3610: 20 3d 20 73 74 64 6f 75 74 3b 0a 09 7d 20 65 6c = stdout;..} el
3620: 73 65 20 69 66 20 28 66 69 6c 65 5b 30 5d 20 3d se if (file[0] =
3630: 3d 20 27 7c 27 29 20 7b 0a 09 09 66 69 6c 65 2b = '|') {...file+
3640: 2b 3b 0a 09 09 72 65 74 76 61 6c 20 3d 20 70 6f +;...retval = po
3650: 70 65 6e 28 66 69 6c 65 2c 20 22 77 22 29 3b 0a pen(file, "w");.
3660: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 72 65 74 76 .} else {...retv
3670: 61 6c 20 3d 20 66 6f 70 65 6e 28 66 69 6c 65 2c al = fopen(file,
3680: 20 22 61 2b 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 "a+");..}...ret
3690: 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a urn(retval);.}..
36a0: 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 static int filed
36b0: 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f _logging_thread_
36c0: 69 6e 69 74 28 46 49 4c 45 20 2a 6c 6f 67 66 70 init(FILE *logfp
36d0: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 ) {..struct file
36e0: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 d_logging_thread
36f0: 5f 61 72 67 73 20 2a 61 72 67 73 3b 0a 09 70 74 _args *args;..pt
3700: 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 5f 69 hread_t thread_i
3710: 64 3b 0a 0a 09 61 72 67 73 20 3d 20 6d 61 6c 6c d;...args = mall
3720: 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72 67 73 29 oc(sizeof(*args)
3730: 29 3b 0a 09 61 72 67 73 2d 3e 66 70 20 3d 20 6c );..args->fp = l
3740: 6f 67 66 70 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f ogfp;...filed_lo
3750: 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 4e 55 4c g_msg_list = NUL
3760: 4c 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 L;...pthread_mut
3770: 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 64 5f 6c ex_init(&filed_l
3780: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 og_msg_list_mute
3790: 78 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 70 74 68 72 x, NULL);...pthr
37a0: 65 61 64 5f 63 72 65 61 74 65 28 26 74 68 72 65 ead_create(&thre
37b0: 61 64 5f 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c ad_id, NULL, fil
37c0: 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 ed_logging_threa
37d0: 64 2c 20 61 72 67 73 29 3b 0a 0a 09 66 69 6c 65 d, args);...file
37e0: 64 5f 6c 6f 67 5f 6d 73 67 28 22 53 54 41 52 54 d_log_msg("START
37f0: 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b ");...return(0);
3800: 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 .}.#endif..#ifde
3810: 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 54 49 4d f FILED_DONT_TIM
3820: 45 4f 55 54 0a 23 64 65 66 69 6e 65 20 66 69 6c EOUT.#define fil
3830: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
3840: 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 29 20 30 _thread_init() 0
3850: 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 .#define filed_s
3860: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69 ockettimeout_ini
3870: 74 28 29 20 30 0a 23 64 65 66 69 6e 65 20 66 69 t() 0.#define fi
3880: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
3890: 74 5f 61 63 63 65 70 74 28 78 29 20 2f 2a 2a 2f t_accept(x) /**/
38a0: 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 .#define filed_s
38b0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f ockettimeout_pro
38c0: 63 65 73 73 69 6e 67 5f 73 74 61 72 74 28 78 29 cessing_start(x)
38d0: 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 /**/.#define fi
38e0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
38f0: 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64 t_processing_end
3900: 28 78 29 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e 65 (x) /**/.#define
3910: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d filed_sockettim
3920: 65 6f 75 74 5f 63 6c 6f 73 65 28 78 2c 20 79 29 eout_close(x, y)
3930: 20 2f 2a 2a 2f 0a 23 65 6c 73 65 0a 74 69 6d 65 /**/.#else.time
3940: 5f 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 _t filed_sockett
3950: 69 6d 65 6f 75 74 5f 74 69 6d 65 3b 0a 73 74 72 imeout_time;.str
3960: 75 63 74 20 7b 0a 09 74 69 6d 65 5f 74 20 65 78 uct {..time_t ex
3970: 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 3b 0a 09 piration_time;..
3980: 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 pthread_t thread
3990: 5f 69 64 3b 0a 09 65 6e 75 6d 20 7b 0a 09 09 66 _id;..enum {...f
39a0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
39b0: 75 74 5f 76 61 6c 69 64 2c 0a 09 09 66 69 6c 65 ut_valid,...file
39c0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
39d0: 69 6e 76 61 6c 69 64 2c 0a 09 7d 20 76 61 6c 69 invalid,..} vali
39e0: 64 3b 0a 7d 20 2a 66 69 6c 65 64 5f 73 6f 63 6b d;.} *filed_sock
39f0: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 ettimeout_sockst
3a00: 61 74 75 73 3b 0a 6c 6f 6e 67 20 66 69 6c 65 64 atus;.long filed
3a10: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
3a20: 6f 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 ockstatus_length
3a30: 3b 0a 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b ;.int filed_sock
3a40: 65 74 74 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c ettimeout_devnul
3a50: 6c 5f 66 64 3b 0a 70 74 68 72 65 61 64 5f 6d 75 l_fd;.pthread_mu
3a60: 74 65 78 5f 74 20 66 69 6c 65 64 5f 73 6f 63 6b tex_t filed_sock
3a70: 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 65 78 20 ettimeout_mutex
3a80: 3d 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f = PTHREAD_MUTEX_
3a90: 49 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 73 74 INITIALIZER;..st
3aa0: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 atic int filed_s
3ab0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
3ac0: 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28 69 6e 74 kfd_in_range(int
3ad0: 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 sockfd) {..if (
3ae0: 73 6f 63 6b 66 64 20 3c 20 33 29 20 7b 0a 09 09 sockfd < 3) {...
3af0: 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 return(0);..}...
3b00: 69 66 20 28 73 6f 63 6b 66 64 20 3e 20 66 69 6c if (sockfd > fil
3b10: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
3b20: 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c 65 6e 67 _sockstatus_leng
3b30: 74 68 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 th) {...return(0
3b40: 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 31 );..}...return(1
3b50: 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 );.}..static voi
3b60: 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 d filed_socketti
3b70: 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 69 6e 74 meout_expire(int
3b80: 20 73 6f 63 6b 66 64 2c 20 69 6e 74 20 6c 65 6e sockfd, int len
3b90: 67 74 68 2c 20 69 6e 74 20 6c 6f 63 6b 68 65 6c gth, int lockhel
3ba0: 64 29 20 7b 0a 09 74 69 6d 65 5f 74 20 6e 6f 77 d) {..time_t now
3bb0: 2c 20 65 78 70 69 72 65 3b 0a 0a 09 69 66 20 28 , expire;...if (
3bc0: 21 6c 6f 63 6b 68 65 6c 64 29 20 7b 0a 09 09 70 !lockheld) {...p
3bd0: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 thread_mutex_loc
3be0: 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 k(&filed_sockett
3bf0: 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 09 imeout_mutex);..
3c00: 7d 0a 0a 09 6e 6f 77 20 3d 20 66 69 6c 65 64 5f }...now = filed_
3c10: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 69 sockettimeout_ti
3c20: 6d 65 3b 0a 0a 09 65 78 70 69 72 65 20 3d 20 6e me;...expire = n
3c30: 6f 77 20 2b 20 6c 65 6e 67 74 68 3b 0a 0a 09 66 ow + length;...f
3c40: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
3c50: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f ut_sockstatus[so
3c60: 63 6b 66 64 5d 2e 65 78 70 69 72 61 74 69 6f 6e ckfd].expiration
3c70: 5f 74 69 6d 65 20 3d 20 65 78 70 69 72 65 3b 0a _time = expire;.
3c80: 0a 09 69 66 20 28 21 6c 6f 63 6b 68 65 6c 64 29 ..if (!lockheld)
3c90: 20 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 {...pthread_mut
3ca0: 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 ex_unlock(&filed
3cb0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d _sockettimeout_m
3cc0: 75 74 65 78 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 utex);..}...retu
3cd0: 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f rn;.}..static vo
3ce0: 69 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 id filed_sockett
3cf0: 69 6d 65 6f 75 74 5f 61 63 63 65 70 74 28 69 6e imeout_accept(in
3d00: 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 t sockfd) {..if
3d10: 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 (!filed_socketti
3d20: 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f meout_sockfd_in_
3d30: 72 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b range(sockfd)) {
3d40: 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 ...return;..}...
3d50: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f pthread_mutex_lo
3d60: 63 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 ck(&filed_socket
3d70: 74 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 3b 0a timeout_mutex);.
3d80: 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
3d90: 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 73 6f 63 meout_expire(soc
3da0: 6b 66 64 2c 20 36 30 2c 20 31 29 3b 0a 0a 09 66 kfd, 60, 1);...f
3db0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
3dc0: 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b 73 6f ut_sockstatus[so
3dd0: 63 6b 66 64 5d 2e 74 68 72 65 61 64 5f 69 64 20 ckfd].thread_id
3de0: 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
3df0: 3b 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 ;...filed_socket
3e00: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
3e10: 75 73 5b 73 6f 63 6b 66 64 5d 2e 76 61 6c 69 64 us[sockfd].valid
3e20: 20 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 = filed_sockett
3e30: 69 6d 65 6f 75 74 5f 76 61 6c 69 64 3b 0a 0a 09 imeout_valid;...
3e40: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e pthread_mutex_un
3e50: 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b lock(&filed_sock
3e60: 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 ettimeout_mutex)
3e70: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 ;...return;.}..s
3e80: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 tatic void filed
3e90: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 _sockettimeout_p
3ea0: 72 6f 63 65 73 73 69 6e 67 5f 73 74 61 72 74 28 rocessing_start(
3eb0: 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 69 int sockfd) {..i
3ec0: 66 20 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 f (!filed_socket
3ed0: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 timeout_sockfd_i
3ee0: 6e 5f 72 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 n_range(sockfd))
3ef0: 20 7b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a {...return;..}.
3f00: 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
3f10: 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 73 6f 63 meout_expire(soc
3f20: 6b 66 64 2c 20 38 36 34 30 30 2c 20 30 29 3b 0a kfd, 86400, 0);.
3f30: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 ..return;.}..sta
3f40: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 tic void filed_s
3f50: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 70 72 6f ockettimeout_pro
3f60: 63 65 73 73 69 6e 67 5f 65 6e 64 28 69 6e 74 20 cessing_end(int
3f70: 73 6f 63 6b 66 64 29 20 7b 0a 09 69 66 20 28 21 sockfd) {..if (!
3f80: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
3f90: 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 out_sockfd_in_ra
3fa0: 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 7b 0a 09 nge(sockfd)) {..
3fb0: 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 66 69 .return;..}...fi
3fc0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
3fd0: 74 5f 65 78 70 69 72 65 28 73 6f 63 6b 66 64 2c t_expire(sockfd,
3fe0: 20 36 30 2c 20 30 29 3b 0a 0a 09 72 65 74 75 72 60, 0);...retur
3ff0: 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 n;.}..static voi
4000: 64 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 d filed_socketti
4010: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 69 6e 74 20 meout_close(int
4020: 73 6f 63 6b 66 64 2c 20 69 6e 74 20 6c 6f 63 6b sockfd, int lock
4030: 68 65 6c 64 29 20 7b 0a 09 69 66 20 28 21 66 69 held) {..if (!fi
4040: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
4050: 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 t_sockfd_in_rang
4060: 65 28 73 6f 63 6b 66 64 29 29 20 7b 0a 09 09 72 e(sockfd)) {...r
4070: 65 74 75 72 6e 3b 0a 09 7d 0a 0a 09 69 66 20 28 eturn;..}...if (
4080: 21 6c 6f 63 6b 68 65 6c 64 29 20 7b 0a 09 09 70 !lockheld) {...p
4090: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 thread_mutex_loc
40a0: 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 k(&filed_sockett
40b0: 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 09 imeout_mutex);..
40c0: 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 }...filed_socket
40d0: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
40e0: 75 73 5b 73 6f 63 6b 66 64 5d 2e 76 61 6c 69 64 us[sockfd].valid
40f0: 20 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 = filed_sockett
4100: 69 6d 65 6f 75 74 5f 69 6e 76 61 6c 69 64 3b 0a imeout_invalid;.
4110: 0a 09 69 66 20 28 21 6c 6f 63 6b 68 65 6c 64 29 ..if (!lockheld)
4120: 20 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 {...pthread_mut
4130: 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 ex_unlock(&filed
4140: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d _sockettimeout_m
4150: 75 74 65 78 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 utex);..}...retu
4160: 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f rn;.}..static vo
4170: 69 64 20 2a 66 69 6c 65 64 5f 73 6f 63 6b 65 74 id *filed_socket
4180: 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 28 76 timeout_thread(v
4190: 6f 69 64 20 2a 61 72 67 29 20 7b 0a 09 73 74 72 oid *arg) {..str
41a0: 75 63 74 20 74 69 6d 65 73 70 65 63 20 73 6c 65 uct timespec sle
41b0: 65 70 5f 74 69 6d 65 3b 0a 09 74 69 6d 65 5f 74 ep_time;..time_t
41c0: 20 6e 6f 77 2c 20 65 78 70 69 72 61 74 69 6f 6e now, expiration
41d0: 5f 74 69 6d 65 3b 0a 09 70 74 68 72 65 61 64 5f _time;..pthread_
41e0: 74 20 74 68 72 65 61 64 5f 69 64 3b 0a 09 6c 6f t thread_id;..lo
41f0: 6e 67 20 69 64 78 3b 0a 09 69 6e 74 20 63 6f 75 ng idx;..int cou
4200: 6e 74 3b 0a 09 69 6e 74 20 76 61 6c 69 64 3b 0a nt;..int valid;.
4210: 09 69 6e 74 20 74 69 6d 65 5f 69 6e 74 65 72 76 .int time_interv
4220: 61 6c 20 3d 20 33 30 3b 0a 09 69 6e 74 20 63 68 al = 30;..int ch
4230: 65 63 6b 5f 70 65 72 69 6f 64 20 3d 20 39 30 3b eck_period = 90;
4240: 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 ...while (1) {..
4250: 09 66 6f 72 20 28 63 6f 75 6e 74 20 3d 20 30 3b .for (count = 0;
4260: 20 63 6f 75 6e 74 20 3c 20 28 63 68 65 63 6b 5f count < (check_
4270: 70 65 72 69 6f 64 20 2f 20 74 69 6d 65 5f 69 6e period / time_in
4280: 74 65 72 76 61 6c 29 3b 20 63 6f 75 6e 74 2b 2b terval); count++
4290: 29 20 7b 0a 09 09 09 73 6c 65 65 70 5f 74 69 6d ) {....sleep_tim
42a0: 65 2e 74 76 5f 73 65 63 20 3d 20 74 69 6d 65 5f e.tv_sec = time_
42b0: 69 6e 74 65 72 76 61 6c 3b 0a 09 09 09 73 6c 65 interval;....sle
42c0: 65 70 5f 74 69 6d 65 2e 74 76 5f 6e 73 65 63 20 ep_time.tv_nsec
42d0: 3d 20 30 3b 0a 09 09 09 6e 61 6e 6f 73 6c 65 65 = 0;....nanoslee
42e0: 70 28 26 73 6c 65 65 70 5f 74 69 6d 65 2c 20 4e p(&sleep_time, N
42f0: 55 4c 4c 29 3b 0a 0a 09 09 09 70 74 68 72 65 61 ULL);.....pthrea
4300: 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 66 69 d_mutex_lock(&fi
4310: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
4320: 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 09 6e 6f t_mutex);.....no
4330: 77 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a w = time(NULL);.
4340: 0a 09 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 ....filed_socket
4350: 74 69 6d 65 6f 75 74 5f 74 69 6d 65 20 3d 20 6e timeout_time = n
4360: 6f 77 3b 0a 0a 09 09 09 70 74 68 72 65 61 64 5f ow;.....pthread_
4370: 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 mutex_unlock(&fi
4380: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
4390: 74 5f 6d 75 74 65 78 29 3b 0a 09 09 7d 0a 0a 09 t_mutex);...}...
43a0: 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c .pthread_mutex_l
43b0: 6f 63 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 ock(&filed_socke
43c0: 74 74 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 3b ttimeout_mutex);
43d0: 0a 0a 09 09 66 6f 72 20 28 69 64 78 20 3d 20 30 ....for (idx = 0
43e0: 3b 20 69 64 78 20 3c 20 66 69 6c 65 64 5f 73 6f ; idx < filed_so
43f0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b ckettimeout_sock
4400: 73 74 61 74 75 73 5f 6c 65 6e 67 74 68 3b 20 69 status_length; i
4410: 64 78 2b 2b 29 20 7b 0a 09 09 09 76 61 6c 69 64 dx++) {....valid
4420: 20 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 = filed_sockett
4430: 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 imeout_sockstatu
4440: 73 5b 69 64 78 5d 2e 76 61 6c 69 64 3b 0a 0a 09 s[idx].valid;...
4450: 09 09 69 66 20 28 76 61 6c 69 64 20 21 3d 20 66 ..if (valid != f
4460: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
4470: 75 74 5f 76 61 6c 69 64 29 20 7b 0a 09 09 09 09 ut_valid) {.....
4480: 63 6f 6e 74 69 6e 75 65 3b 0a 09 09 09 7d 0a 0a continue;....}..
4490: 09 09 09 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 ...expiration_ti
44a0: 6d 65 20 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 me = filed_socke
44b0: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 ttimeout_socksta
44c0: 74 75 73 5b 69 64 78 5d 2e 65 78 70 69 72 61 74 tus[idx].expirat
44d0: 69 6f 6e 5f 74 69 6d 65 3b 0a 0a 09 09 09 74 68 ion_time;.....th
44e0: 72 65 61 64 5f 69 64 20 3d 20 66 69 6c 65 64 5f read_id = filed_
44f0: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f sockettimeout_so
4500: 63 6b 73 74 61 74 75 73 5b 69 64 78 5d 2e 74 68 ckstatus[idx].th
4510: 72 65 61 64 5f 69 64 3b 0a 0a 09 09 09 69 66 20 read_id;.....if
4520: 28 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 (expiration_time
4530: 20 3e 20 6e 6f 77 29 20 7b 0a 09 09 09 09 63 6f > now) {.....co
4540: 6e 74 69 6e 75 65 3b 0a 09 09 09 7d 0a 0a 09 09 ntinue;....}....
4550: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d .filed_sockettim
4560: 65 6f 75 74 5f 63 6c 6f 73 65 28 69 64 78 2c 20 eout_close(idx,
4570: 31 29 3b 0a 0a 09 09 09 64 75 70 32 28 66 69 6c 1);.....dup2(fil
4580: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
4590: 5f 64 65 76 6e 75 6c 6c 5f 66 64 2c 20 69 64 78 _devnull_fd, idx
45a0: 29 3b 0a 0a 09 09 09 70 74 68 72 65 61 64 5f 6b );.....pthread_k
45b0: 69 6c 6c 28 74 68 72 65 61 64 5f 69 64 2c 20 53 ill(thread_id, S
45c0: 49 47 50 49 50 45 29 3b 0a 09 09 7d 0a 0a 09 09 IGPIPE);...}....
45d0: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e pthread_mutex_un
45e0: 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b lock(&filed_sock
45f0: 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 ettimeout_mutex)
4600: 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 4e 55 ;..}...return(NU
4610: 4c 4c 29 3b 0a 0a 09 2f 2a 20 4e 4f 54 52 45 41 LL);.../* NOTREA
4620: 43 48 3a 20 57 65 20 64 6f 6e 27 74 20 61 63 74 CH: We don't act
4630: 75 61 6c 6c 79 20 74 61 6b 65 20 61 6e 79 20 61 ually take any a
4640: 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 72 67 rguments */..arg
4650: 20 3d 20 61 72 67 3b 0a 7d 0a 0a 73 74 61 74 69 = arg;.}..stati
4660: 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b c int filed_sock
4670: 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 ettimeout_thread
4680: 5f 69 6e 69 74 28 76 6f 69 64 29 20 7b 0a 09 70 _init(void) {..p
4690: 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 64 5f thread_t thread_
46a0: 69 64 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 72 id;...pthread_cr
46b0: 65 61 74 65 28 26 74 68 72 65 61 64 5f 69 64 2c eate(&thread_id,
46c0: 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f 73 6f 63 NULL, filed_soc
46d0: 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 kettimeout_threa
46e0: 64 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 72 65 74 75 d, NULL);...retu
46f0: 72 6e 28 30 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 rn(0);.}..static
4700: 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f 63 6b 65 int filed_socke
4710: 74 74 69 6d 65 6f 75 74 5f 69 6e 69 74 28 76 6f ttimeout_init(vo
4720: 69 64 29 20 7b 0a 09 6c 6f 6e 67 20 6d 61 78 66 id) {..long maxf
4730: 64 2c 20 69 64 78 3b 0a 0a 09 6d 61 78 66 64 20 d, idx;...maxfd
4740: 3d 20 73 79 73 63 6f 6e 66 28 5f 53 43 5f 4f 50 = sysconf(_SC_OP
4750: 45 4e 5f 4d 41 58 29 3b 0a 09 69 66 20 28 6d 61 EN_MAX);..if (ma
4760: 78 66 64 20 3c 3d 20 30 29 20 7b 0a 09 09 6d 61 xfd <= 0) {...ma
4770: 78 66 64 20 3d 20 34 30 39 36 3b 0a 09 7d 0a 0a xfd = 4096;..}..
4780: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d .filed_sockettim
4790: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f eout_sockstatus_
47a0: 6c 65 6e 67 74 68 20 3d 20 6d 61 78 66 64 3b 0a length = maxfd;.
47b0: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d .filed_sockettim
47c0: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 20 eout_sockstatus
47d0: 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 = malloc(sizeof(
47e0: 2a 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d *filed_sockettim
47f0: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 29 eout_sockstatus)
4800: 20 2a 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 * filed_sockett
4810: 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 imeout_sockstatu
4820: 73 5f 6c 65 6e 67 74 68 29 3b 0a 09 69 66 20 28 s_length);..if (
4830: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
4840: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 20 3d out_sockstatus =
4850: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 = NULL) {...retu
4860: 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 rn(-1);..}...for
4870: 20 28 69 64 78 20 3d 20 30 3b 20 69 64 78 20 3c (idx = 0; idx <
4880: 20 6d 61 78 66 64 3b 20 69 64 78 2b 2b 29 20 7b maxfd; idx++) {
4890: 0a 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ...filed_sockett
48a0: 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 imeout_sockstatu
48b0: 73 5b 69 64 78 5d 2e 76 61 6c 69 64 20 3d 20 66 s[idx].valid = f
48c0: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
48d0: 75 74 5f 69 6e 76 61 6c 69 64 3b 0a 09 7d 0a 0a ut_invalid;..}..
48e0: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d .filed_sockettim
48f0: 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 20 eout_devnull_fd
4900: 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c = open("/dev/nul
4910: 6c 22 2c 20 4f 5f 52 44 57 52 29 3b 0a 09 69 66 l", O_RDWR);..if
4920: 20 28 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 (filed_socketti
4930: 6d 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 meout_devnull_fd
4940: 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e < 0) {...return
4950: 28 2d 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 (-1);..}...retur
4960: 6e 28 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a n(0);.}.#endif..
4970: 2f 2a 20 46 6f 72 6d 61 74 20 74 69 6d 65 20 70 /* Format time p
4980: 65 72 20 52 46 43 32 36 31 36 20 2a 2f 0a 73 74 er RFC2616 */.st
4990: 61 74 69 63 20 63 68 61 72 20 2a 66 69 6c 65 64 atic char *filed
49a0: 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 68 61 _format_time(cha
49b0: 72 20 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f r *buffer, size_
49c0: 74 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 63 6f t buffer_len, co
49d0: 6e 73 74 20 74 69 6d 65 5f 74 20 74 69 6d 65 69 nst time_t timei
49e0: 6e 66 6f 29 20 7b 0a 09 73 74 72 75 63 74 20 74 nfo) {..struct t
49f0: 6d 20 74 69 6d 65 69 6e 66 6f 5f 74 6d 2c 20 2a m timeinfo_tm, *
4a00: 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a timeinfo_tm_p;..
4a10: 09 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d .timeinfo_tm_p =
4a20: 20 67 6d 74 69 6d 65 5f 72 28 26 74 69 6d 65 69 gmtime_r(&timei
4a30: 6e 66 6f 2c 20 26 74 69 6d 65 69 6e 66 6f 5f 74 nfo, &timeinfo_t
4a40: 6d 29 3b 0a 09 69 66 20 28 74 69 6d 65 69 6e 66 m);..if (timeinf
4a50: 6f 5f 74 6d 5f 70 20 3d 3d 20 4e 55 4c 4c 29 20 o_tm_p == NULL)
4a60: 7b 0a 09 09 72 65 74 75 72 6e 28 22 75 6e 6b 6e {...return("unkn
4a70: 6f 77 6e 22 29 3b 0a 09 7d 0a 0a 09 62 75 66 66 own");..}...buff
4a80: 65 72 5b 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 er[buffer_len -
4a90: 31 5d 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 1] = '\0';..buff
4aa0: 65 72 5f 6c 65 6e 20 3d 20 73 74 72 66 74 69 6d er_len = strftim
4ab0: 65 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 e(buffer, buffer
4ac0: 5f 6c 65 6e 20 2d 20 31 2c 20 22 25 61 2c 20 25 _len - 1, "%a, %
4ad0: 64 20 25 62 20 25 59 20 25 48 3a 25 4d 3a 25 53 d %b %Y %H:%M:%S
4ae0: 20 47 4d 54 22 2c 20 74 69 6d 65 69 6e 66 6f 5f GMT", timeinfo_
4af0: 74 6d 5f 70 29 3b 0a 0a 09 72 65 74 75 72 6e 28 tm_p);...return(
4b00: 62 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 buffer);.}../* h
4b10: 61 73 68 20 2a 2f 0a 73 74 61 74 69 63 20 75 6e ash */.static un
4b20: 73 69 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 signed int filed
4b30: 5f 68 61 73 68 28 63 6f 6e 73 74 20 75 6e 73 69 _hash(const unsi
4b40: 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 75 65 gned char *value
4b50: 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d , unsigned int m
4b60: 6f 64 75 6c 75 73 29 20 7b 0a 09 75 6e 73 69 67 odulus) {..unsig
4b70: 6e 65 64 20 63 68 61 72 20 63 75 72 72 2c 20 70 ned char curr, p
4b80: 72 65 76 3b 0a 09 69 6e 74 20 64 69 66 66 3b 0a rev;..int diff;.
4b90: 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 65 .unsigned int re
4ba0: 74 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d tval;...retval =
4bb0: 20 6d 6f 64 75 6c 75 73 20 2d 20 31 3b 0a 09 70 modulus - 1;..p
4bc0: 72 65 76 20 3d 20 6d 6f 64 75 6c 75 73 20 25 20 rev = modulus %
4bd0: 32 35 35 3b 0a 0a 09 77 68 69 6c 65 20 28 28 63 255;...while ((c
4be0: 75 72 72 20 3d 20 2a 76 61 6c 75 65 29 29 20 7b urr = *value)) {
4bf0: 0a 09 09 69 66 20 28 63 75 72 72 20 3c 20 33 32 ...if (curr < 32
4c00: 29 20 7b 0a 09 09 09 63 75 72 72 20 3d 20 32 35 ) {....curr = 25
4c10: 35 20 2d 20 63 75 72 72 3b 0a 09 09 7d 20 65 6c 5 - curr;...} el
4c20: 73 65 20 7b 0a 09 09 09 63 75 72 72 20 2d 3d 20 se {....curr -=
4c30: 33 32 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 70 32;...}....if (p
4c40: 72 65 76 20 3c 20 63 75 72 72 29 20 7b 0a 09 09 rev < curr) {...
4c50: 09 64 69 66 66 20 3d 20 63 75 72 72 20 2d 20 70 .diff = curr - p
4c60: 72 65 76 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a rev;...} else {.
4c70: 09 09 09 64 69 66 66 20 3d 20 70 72 65 76 20 2d ...diff = prev -
4c80: 20 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 70 72 curr;...}....pr
4c90: 65 76 20 3d 20 63 75 72 72 3b 0a 0a 09 09 72 65 ev = curr;....re
4ca0: 74 76 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 65 tval <<= 3;...re
4cb0: 74 76 61 6c 20 26 3d 20 30 78 46 46 46 46 46 46 tval &= 0xFFFFFF
4cc0: 46 46 4c 55 3b 0a 09 09 72 65 74 76 61 6c 20 5e FFLU;...retval ^
4cd0: 3d 20 64 69 66 66 3b 0a 0a 09 09 76 61 6c 75 65 = diff;....value
4ce0: 2b 2b 3b 0a 09 7d 0a 0a 09 72 65 74 76 61 6c 20 ++;..}...retval
4cf0: 3d 20 72 65 74 76 61 6c 20 25 20 6d 6f 64 75 6c = retval % modul
4d00: 75 73 3b 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 us;...return(ret
4d10: 76 61 6c 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 val);.}../* Find
4d20: 20 61 20 6d 69 6d 65 2d 74 79 70 65 20 62 61 73 a mime-type bas
4d30: 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 6e 61 ed on the filena
4d40: 6d 65 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e me */.static con
4d50: 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 64 st char *filed_d
4d60: 65 74 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 etermine_mimetyp
4d70: 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 e(const char *pa
4d80: 74 68 29 20 7b 0a 09 63 6f 6e 73 74 20 63 68 61 th) {..const cha
4d90: 72 20 2a 70 3b 0a 0a 09 70 20 3d 20 73 74 72 72 r *p;...p = strr
4da0: 63 68 72 28 70 61 74 68 2c 20 27 2e 27 29 3b 0a chr(path, '.');.
4db0: 09 69 66 20 28 70 20 3d 3d 20 4e 55 4c 4c 29 20 .if (p == NULL)
4dc0: 7b 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 {...return(FILED
4dd0: 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a _DEFAULT_TYPE);.
4de0: 09 7d 0a 0a 09 70 2b 2b 3b 0a 09 69 66 20 28 2a .}...p++;..if (*
4df0: 70 20 3d 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 p == '\0') {...r
4e00: 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 eturn(FILED_DEFA
4e10: 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 ULT_TYPE);..}...
4e20: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 filed_log_msg_de
4e30: 62 75 67 28 22 4c 6f 6f 6b 69 6e 67 20 75 70 20 bug("Looking up
4e40: 4d 49 4d 45 20 74 79 70 65 20 66 6f 72 20 25 73 MIME type for %s
4e50: 20 28 68 61 73 68 20 3d 20 25 6c 6c 75 29 22 2c (hash = %llu)",
4e60: 20 70 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f p, (unsigned lo
4e70: 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 64 5f 68 ng long) filed_h
4e80: 61 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 ash((const unsig
4e90: 6e 65 64 20 63 68 61 72 20 2a 29 20 70 2c 20 31 ned char *) p, 1
4ea0: 36 37 37 37 32 35 39 29 29 3b 0a 0a 23 69 6e 63 6777259));..#inc
4eb0: 6c 75 64 65 20 22 66 69 6c 65 64 2d 6d 69 6d 65 lude "filed-mime
4ec0: 2d 74 79 70 65 73 2e 68 22 0a 0a 09 72 65 74 75 -types.h"...retu
4ed0: 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 rn(FILED_DEFAULT
4ee0: 5f 54 59 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 _TYPE);.}../* Ge
4ef0: 6e 65 72 61 74 65 20 61 20 75 6e 69 71 75 65 20 nerate a unique
4f00: 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 73 74 identifier */.st
4f10: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f atic void filed_
4f20: 67 65 6e 65 72 61 74 65 5f 65 74 61 67 28 63 68 generate_etag(ch
4f30: 61 72 20 2a 65 74 61 67 2c 20 73 69 7a 65 5f 74 ar *etag, size_t
4f40: 20 6c 65 6e 67 74 68 29 20 7b 0a 09 73 6e 70 72 length) {..snpr
4f50: 69 6e 74 66 28 65 74 61 67 2c 20 6c 65 6e 67 74 intf(etag, lengt
4f60: 68 2c 20 22 25 6c 6c 78 2d 25 6c 6c 78 25 6c 6c h, "%llx-%llx%ll
4f70: 78 25 6c 6c 78 25 6c 6c 78 22 2c 0a 09 09 28 75 x%llx%llx",...(u
4f80: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
4f90: 67 29 20 74 69 6d 65 28 4e 55 4c 4c 29 2c 0a 09 g) time(NULL),..
4fa0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
4fb0: 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a long) random(),.
4fc0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
4fd0: 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c long) random(),
4fe0: 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
4ff0: 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 g long) random()
5000: 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f ,...(unsigned lo
5010: 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 ng long) random(
5020: 29 0a 09 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 )..);.}..#ifdef
5030: 46 49 4c 45 44 5f 46 41 4b 45 5f 43 48 52 4f 4f FILED_FAKE_CHROO
5040: 54 0a 2f 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 T./* Translate a
5050: 20 70 61 74 68 20 69 6e 74 6f 20 61 20 66 61 6b path into a fak
5060: 65 20 63 68 72 6f 6f 74 20 70 61 74 68 20 2a 2f e chroot path */
5070: 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 .static const ch
5080: 61 72 20 2a 66 69 6c 65 64 5f 70 61 74 68 5f 74 ar *filed_path_t
5090: 72 61 6e 73 6c 61 74 65 28 63 6f 6e 73 74 20 63 ranslate(const c
50a0: 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 75 63 har *path, struc
50b0: 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 t filed_options
50c0: 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 61 *options) {..sta
50d0: 74 69 63 20 5f 5f 74 68 72 65 61 64 20 63 68 61 tic __thread cha
50e0: 72 20 70 61 74 68 42 75 66 66 65 72 5b 38 31 39 r pathBuffer[819
50f0: 32 5d 3b 0a 09 69 6e 74 20 73 6e 70 72 69 6e 74 2];..int snprint
5100: 66 5f 72 65 74 3b 0a 0a 09 2f 2a 20 49 66 20 6e f_ret;.../* If n
5110: 6f 20 61 6c 74 65 72 6e 61 74 69 76 65 20 72 6f o alternative ro
5120: 6f 74 20 69 73 20 73 70 65 63 69 66 69 65 64 2c ot is specified,
5130: 20 72 65 74 75 72 6e 20 74 68 65 20 75 6e 61 64 return the unad
5140: 6f 72 6e 65 64 20 70 61 74 68 20 2a 2f 0a 09 69 orned path */..i
5150: 66 20 28 21 6f 70 74 69 6f 6e 73 2d 3e 66 61 6b f (!options->fak
5160: 65 5f 6e 65 77 72 6f 6f 74 29 20 7b 0a 09 09 72 e_newroot) {...r
5170: 65 74 75 72 6e 28 70 61 74 68 29 3b 0a 09 7d 0a eturn(path);..}.
5180: 0a 09 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 ../* Verify that
5190: 20 74 68 69 73 20 72 65 71 75 65 73 74 20 77 69 this request wi
51a0: 6c 6c 20 6e 6f 74 20 67 6f 20 6f 75 74 73 69 64 ll not go outsid
51b0: 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 66 69 e of the specifi
51c0: 65 64 20 72 6f 6f 74 20 2a 2f 0a 09 69 66 20 28 ed root */..if (
51d0: 73 74 72 73 74 72 28 70 61 74 68 2c 20 22 2f 2e strstr(path, "/.
51e0: 2e 2f 22 29 20 21 3d 20 4e 55 4c 4c 20 7c 7c 20 ./") != NULL ||
51f0: 70 61 74 68 5b 30 5d 20 21 3d 20 27 2f 27 29 20 path[0] != '/')
5200: 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 {...filed_log_ms
5210: 67 5f 64 65 62 75 67 28 22 55 6e 61 62 6c 65 20 g_debug("Unable
5220: 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 70 61 74 to translate pat
5230: 68 20 5c 22 25 73 5c 22 2c 20 63 6f 6e 74 61 69 h \"%s\", contai
5240: 6e 73 20 69 6e 76 61 6c 69 64 20 63 68 61 72 61 ns invalid chara
5250: 63 74 65 72 73 22 2c 20 70 61 74 68 29 3b 0a 0a cters", path);..
5260: 09 09 72 65 74 75 72 6e 28 6f 70 74 69 6f 6e 73 ..return(options
5270: 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 29 3b ->fake_newroot);
5280: 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 ..}.../* Create
5290: 74 68 65 20 6e 65 77 20 70 61 74 68 20 69 6e 74 the new path int
52a0: 6f 20 6f 75 72 20 6c 6f 63 61 6c 20 28 54 4c 53 o our local (TLS
52b0: 29 20 73 74 61 74 69 63 20 62 75 66 66 65 72 20 ) static buffer
52c0: 2a 2f 0a 09 73 6e 70 72 69 6e 74 66 5f 72 65 74 */..snprintf_ret
52d0: 20 3d 20 73 6e 70 72 69 6e 74 66 28 70 61 74 68 = snprintf(path
52e0: 42 75 66 66 65 72 2c 20 73 69 7a 65 6f 66 28 70 Buffer, sizeof(p
52f0: 61 74 68 42 75 66 66 65 72 29 2c 20 22 25 73 2f athBuffer), "%s/
5300: 25 73 22 2c 20 6f 70 74 69 6f 6e 73 2d 3e 66 61 %s", options->fa
5310: 6b 65 5f 6e 65 77 72 6f 6f 74 2c 20 70 61 74 68 ke_newroot, path
5320: 29 3b 0a 09 69 66 20 28 73 6e 70 72 69 6e 74 66 );..if (snprintf
5330: 5f 72 65 74 20 3c 20 30 20 7c 7c 20 28 28 75 6e _ret < 0 || ((un
5340: 73 69 67 6e 65 64 20 69 6e 74 29 20 73 6e 70 72 signed int) snpr
5350: 69 6e 74 66 5f 72 65 74 29 20 3e 3d 20 73 69 7a intf_ret) >= siz
5360: 65 6f 66 28 70 61 74 68 42 75 66 66 65 72 29 29 eof(pathBuffer))
5370: 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d {...filed_log_m
5380: 73 67 5f 64 65 62 75 67 28 22 55 6e 61 62 6c 65 sg_debug("Unable
5390: 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 70 61 to translate pa
53a0: 74 68 20 5c 22 25 73 5c 22 2c 20 77 69 6c 6c 20 th \"%s\", will
53b0: 6e 6f 74 20 66 69 74 20 69 6e 74 6f 20 6e 65 77 not fit into new
53c0: 20 62 75 66 66 65 72 22 2c 20 70 61 74 68 29 3b buffer", path);
53d0: 0a 0a 09 09 72 65 74 75 72 6e 28 6f 70 74 69 6f ....return(optio
53e0: 6e 73 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 ns->fake_newroot
53f0: 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 6c 6f );..}...filed_lo
5400: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 54 72 61 g_msg_debug("Tra
5410: 6e 73 6c 61 74 69 6e 67 20 70 61 74 68 20 5c 22 nslating path \"
5420: 25 73 5c 22 20 69 6e 74 6f 20 5c 22 25 73 5c 22 %s\" into \"%s\"
5430: 22 2c 20 70 61 74 68 2c 20 70 61 74 68 42 75 66 ", path, pathBuf
5440: 66 65 72 29 3b 0a 0a 09 2f 2a 20 52 65 74 75 72 fer);.../* Retur
5450: 6e 20 74 68 65 20 6e 65 77 20 70 61 74 68 20 2a n the new path *
5460: 2f 0a 09 72 65 74 75 72 6e 28 70 61 74 68 42 75 /..return(pathBu
5470: 66 66 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 ffer);.}..static
5480: 20 76 6f 69 64 20 66 69 6c 65 64 5f 70 61 74 68 void filed_path
5490: 5f 74 72 61 6e 73 6c 61 74 65 5f 73 65 74 5f 72 _translate_set_r
54a0: 6f 6f 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a oot(const char *
54b0: 76 61 72 2c 20 73 74 72 75 63 74 20 66 69 6c 65 var, struct file
54c0: 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f d_options *optio
54d0: 6e 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ns, const char *
54e0: 76 61 6c 29 20 7b 0a 09 6f 70 74 69 6f 6e 73 2d val) {..options-
54f0: 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 20 3d 20 >fake_newroot =
5500: 73 74 72 64 75 70 28 76 61 6c 29 3b 0a 0a 09 72 strdup(val);...r
5510: 65 74 75 72 6e 3b 0a 0a 09 2f 2a 20 76 61 72 20 eturn;.../* var
5520: 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 is only used in
5530: 74 68 65 20 6d 61 63 72 6f 20 2d 2d 20 64 69 73 the macro -- dis
5540: 63 61 72 64 20 69 74 20 68 65 72 65 20 2a 2f 0a card it here */.
5550: 09 76 61 72 20 3d 20 76 61 72 3b 0a 7d 0a 23 65 .var = var;.}.#e
5560: 6c 73 65 0a 23 64 65 66 69 6e 65 20 66 69 6c 65 lse.#define file
5570: 64 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 d_path_translate
5580: 28 70 61 74 68 2c 20 6f 70 74 69 6f 6e 73 29 20 (path, options)
5590: 70 61 74 68 0a 23 64 65 66 69 6e 65 20 66 69 6c path.#define fil
55a0: 65 64 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 ed_path_translat
55b0: 65 5f 73 65 74 5f 72 6f 6f 74 28 76 61 72 2c 20 e_set_root(var,
55c0: 6f 70 74 69 6f 6e 73 2c 20 76 61 6c 29 20 76 61 options, val) va
55d0: 72 20 3d 20 73 74 72 64 75 70 28 76 61 6c 29 0a r = strdup(val).
55e0: 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 65 6e 20 #endif../* Open
55f0: 61 20 66 69 6c 65 20 61 6e 64 20 72 65 74 75 72 a file and retur
5600: 6e 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 n file informati
5610: 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 on */.static str
5620: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e uct filed_filein
5630: 66 6f 20 2a 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 fo *filed_open_f
5640: 69 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ile(const char *
5650: 70 61 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c path, struct fil
5660: 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 62 75 66 ed_fileinfo *buf
5670: 66 65 72 2c 20 73 74 72 75 63 74 20 66 69 6c 65 fer, struct file
5680: 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f d_options *optio
5690: 6e 73 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 ns) {..struct fi
56a0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 led_fileinfo *ca
56b0: 63 68 65 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 che;..unsigned i
56c0: 6e 74 20 63 61 63 68 65 5f 69 64 78 3b 0a 09 6f nt cache_idx;..o
56d0: 66 66 5f 74 20 6c 65 6e 3b 0a 09 69 6e 74 20 66 ff_t len;..int f
56e0: 64 3b 0a 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 d;...if (filed_f
56f0: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f ileinfo_fdcache_
5700: 73 69 7a 65 20 21 3d 20 30 29 20 7b 0a 09 09 63 size != 0) {...c
5710: 61 63 68 65 5f 69 64 78 20 3d 20 66 69 6c 65 64 ache_idx = filed
5720: 5f 68 61 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 _hash((const uns
5730: 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70 61 igned char *) pa
5740: 74 68 2c 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e th, filed_filein
5750: 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 29 fo_fdcache_size)
5760: 3b 0a 0a 09 09 63 61 63 68 65 20 3d 20 26 66 69 ;....cache = &fi
5770: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 led_fileinfo_fdc
5780: 61 63 68 65 5b 63 61 63 68 65 5f 69 64 78 5d 3b ache[cache_idx];
5790: 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 ....filed_log_ms
57a0: 67 5f 64 65 62 75 67 28 22 4c 6f 63 6b 69 6e 67 g_debug("Locking
57b0: 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a 20 mutex for idx:
57c0: 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 %lu", (unsigned
57d0: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 29 long) cache_idx)
57e0: 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 ;....pthread_mut
57f0: 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e ex_lock(&cache->
5800: 6d 75 74 65 78 29 3b 0a 0a 09 09 66 69 6c 65 64 mutex);....filed
5810: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 _log_msg_debug("
5820: 43 6f 6d 70 6c 65 74 65 64 20 6c 6f 63 6b 69 6e Completed lockin
5830: 67 20 6d 75 74 65 78 20 66 6f 72 20 69 64 78 3a g mutex for idx:
5840: 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e 65 64 %lu", (unsigned
5850: 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 long) cache_idx
5860: 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 63 );..} else {...c
5870: 61 63 68 65 5f 69 64 78 20 3d 20 30 3b 0a 09 09 ache_idx = 0;...
5880: 63 61 63 68 65 20 3d 20 62 75 66 66 65 72 3b 0a cache = buffer;.
5890: 09 09 63 61 63 68 65 2d 3e 70 61 74 68 5b 30 5d ..cache->path[0]
58a0: 20 3d 20 27 5c 30 27 3b 0a 09 09 63 61 63 68 65 = '\0';...cache
58b0: 2d 3e 66 64 20 3d 20 2d 31 3b 0a 09 7d 0a 0a 09 ->fd = -1;..}...
58c0: 69 66 20 28 73 74 72 63 6d 70 28 70 61 74 68 2c if (strcmp(path,
58d0: 20 63 61 63 68 65 2d 3e 70 61 74 68 29 20 21 3d cache->path) !=
58e0: 20 30 29 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 0) {...filed_lo
58f0: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 61 63 g_msg_debug("Cac
5900: 68 65 20 6d 69 73 73 20 66 6f 72 20 69 64 78 3a he miss for idx:
5910: 20 25 6c 75 3a 20 4f 4c 44 20 5c 22 25 73 5c 22 %lu: OLD \"%s\"
5920: 2c 20 4e 45 57 20 5c 22 25 73 5c 22 22 2c 20 28 , NEW \"%s\"", (
5930: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 unsigned long) c
5940: 61 63 68 65 5f 69 64 78 2c 20 63 61 63 68 65 2d ache_idx, cache-
5950: 3e 70 61 74 68 2c 20 70 61 74 68 29 3b 0a 0a 09 >path, path);...
5960: 09 66 64 20 3d 20 6f 70 65 6e 28 66 69 6c 65 64 .fd = open(filed
5970: 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 28 _path_translate(
5980: 70 61 74 68 2c 20 6f 70 74 69 6f 6e 73 29 2c 20 path, options),
5990: 4f 5f 52 44 4f 4e 4c 59 20 7c 20 4f 5f 4c 41 52 O_RDONLY | O_LAR
59a0: 47 45 46 49 4c 45 29 3b 0a 09 09 69 66 20 28 66 GEFILE);...if (f
59b0: 64 20 3c 20 30 29 20 7b 0a 09 09 09 69 66 20 28 d < 0) {....if (
59c0: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
59d0: 64 63 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 30 dcache_size != 0
59e0: 29 20 7b 0a 09 09 09 09 70 74 68 72 65 61 64 5f ) {.....pthread_
59f0: 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 mutex_unlock(&ca
5a00: 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 09 09 09 che->mutex);....
5a10: 7d 0a 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c }.....return(NUL
5a20: 4c 29 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 63 L);...}....if (c
5a30: 61 63 68 65 2d 3e 66 64 20 3e 3d 20 30 29 20 7b ache->fd >= 0) {
5a40: 0a 09 09 09 63 6c 6f 73 65 28 63 61 63 68 65 2d ....close(cache-
5a50: 3e 66 64 29 3b 0a 09 09 7d 0a 0a 09 09 6c 65 6e >fd);...}....len
5a60: 20 3d 20 6c 73 65 65 6b 28 66 64 2c 20 30 2c 20 = lseek(fd, 0,
5a70: 53 45 45 4b 5f 45 4e 44 29 3b 0a 09 09 6c 73 65 SEEK_END);...lse
5a80: 65 6b 28 66 64 2c 20 30 2c 20 53 45 45 4b 5f 53 ek(fd, 0, SEEK_S
5a90: 45 54 29 3b 0a 0a 09 09 63 61 63 68 65 2d 3e 66 ET);....cache->f
5aa0: 64 20 3d 20 66 64 3b 0a 09 09 63 61 63 68 65 2d d = fd;...cache-
5ab0: 3e 6c 65 6e 20 3d 20 6c 65 6e 3b 0a 09 09 73 74 >len = len;...st
5ac0: 72 63 70 79 28 63 61 63 68 65 2d 3e 70 61 74 68 rcpy(cache->path
5ad0: 2c 20 70 61 74 68 29 3b 0a 09 09 63 61 63 68 65 , path);...cache
5ae0: 2d 3e 74 79 70 65 20 3d 20 66 69 6c 65 64 5f 64 ->type = filed_d
5af0: 65 74 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 etermine_mimetyp
5b00: 65 28 70 61 74 68 29 3b 0a 09 09 66 69 6c 65 64 e(path);...filed
5b10: 5f 67 65 6e 65 72 61 74 65 5f 65 74 61 67 28 63 _generate_etag(c
5b20: 61 63 68 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 ache->etag, size
5b30: 6f 66 28 63 61 63 68 65 2d 3e 65 74 61 67 29 29 of(cache->etag))
5b40: 3b 0a 0a 09 09 2f 2a 20 58 58 58 3a 54 4f 44 4f ;..../* XXX:TODO
5b50: 3a 20 44 65 74 65 72 6d 69 6e 65 20 2a 2f 0a 09 : Determine */..
5b60: 09 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 .cache->lastmod
5b70: 3d 20 66 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 = filed_format_t
5b80: 69 6d 65 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d ime(cache->lastm
5b90: 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 28 63 61 63 od_b, sizeof(cac
5ba0: 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 29 2c 20 he->lastmod_b),
5bb0: 74 69 6d 65 28 4e 55 4c 4c 29 20 2d 20 33 30 29 time(NULL) - 30)
5bc0: 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 66 69 ;..} else {...fi
5bd0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 led_log_msg_debu
5be0: 67 28 22 43 61 63 68 65 20 68 69 74 20 66 6f 72 g("Cache hit for
5bf0: 20 69 64 78 3a 20 25 6c 75 3a 20 50 41 54 48 20 idx: %lu: PATH
5c00: 5c 22 25 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e \"%s\"", (unsign
5c10: 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 ed long) cache_i
5c20: 64 78 2c 20 70 61 74 68 29 3b 0a 09 7d 0a 0a 09 dx, path);..}...
5c30: 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e if (filed_filein
5c40: 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 fo_fdcache_size
5c50: 21 3d 20 30 29 20 7b 0a 09 09 2f 2a 0a 09 09 20 != 0) {.../*...
5c60: 2a 20 57 65 20 68 61 76 65 20 74 6f 20 6d 61 6b * We have to mak
5c70: 65 20 61 20 64 75 70 6c 69 63 61 74 65 20 46 44 e a duplicate FD
5c80: 2c 20 62 65 63 61 75 73 65 20 6f 6e 63 65 20 77 , because once w
5c90: 65 20 72 65 6c 65 61 73 65 20 74 68 65 20 63 61 e release the ca
5ca0: 63 68 65 0a 09 09 20 2a 20 6d 75 74 65 78 2c 20 che... * mutex,
5cb0: 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
5cc0: 74 6f 72 20 6d 61 79 20 62 65 20 63 6c 6f 73 65 tor may be close
5cd0: 64 0a 09 09 20 2a 2f 0a 09 09 66 64 20 3d 20 64 d... */...fd = d
5ce0: 75 70 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 up(cache->fd);..
5cf0: 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 .if (fd < 0) {..
5d00: 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ..pthread_mutex_
5d10: 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d unlock(&cache->m
5d20: 75 74 65 78 29 3b 0a 0a 09 09 09 72 65 74 75 72 utex);.....retur
5d30: 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 n(NULL);...}....
5d40: 62 75 66 66 65 72 2d 3e 66 64 20 3d 20 66 64 3b buffer->fd = fd;
5d50: 0a 09 09 62 75 66 66 65 72 2d 3e 6c 65 6e 20 3d ...buffer->len =
5d60: 20 63 61 63 68 65 2d 3e 6c 65 6e 3b 0a 09 09 62 cache->len;...b
5d70: 75 66 66 65 72 2d 3e 74 79 70 65 20 3d 20 63 61 uffer->type = ca
5d80: 63 68 65 2d 3e 74 79 70 65 3b 0a 09 09 6d 65 6d che->type;...mem
5d90: 63 70 79 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 cpy(buffer->last
5da0: 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d 3e 6c 61 mod_b, cache->la
5db0: 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 28 stmod_b, sizeof(
5dc0: 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 5f buffer->lastmod_
5dd0: 62 29 29 3b 0a 09 09 6d 65 6d 63 70 79 28 62 75 b));...memcpy(bu
5de0: 66 66 65 72 2d 3e 65 74 61 67 2c 20 63 61 63 68 ffer->etag, cach
5df0: 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f 66 28 e->etag, sizeof(
5e00: 62 75 66 66 65 72 2d 3e 65 74 61 67 29 29 3b 0a buffer->etag));.
5e10: 09 09 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f ..buffer->lastmo
5e20: 64 20 3d 20 62 75 66 66 65 72 2d 3e 6c 61 73 74 d = buffer->last
5e30: 6d 6f 64 5f 62 20 2b 20 28 63 61 63 68 65 2d 3e mod_b + (cache->
5e40: 6c 61 73 74 6d 6f 64 20 2d 20 63 61 63 68 65 2d lastmod - cache-
5e50: 3e 6c 61 73 74 6d 6f 64 5f 62 29 3b 0a 0a 09 09 >lastmod_b);....
5e60: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e pthread_mutex_un
5e70: 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 lock(&cache->mut
5e80: 65 78 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e ex);..}...return
5e90: 28 62 75 66 66 65 72 29 3b 0a 0a 09 2f 2a 20 6f (buffer);.../* o
5ea0: 70 74 69 6f 6e 73 20 69 73 20 6f 6e 6c 79 20 75 ptions is only u
5eb0: 73 65 64 20 69 66 20 66 61 6b 65 20 63 68 72 6f sed if fake chro
5ec0: 6f 74 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 63 ot is enabled, c
5ed0: 6f 6e 66 75 73 65 20 74 68 65 20 63 6f 6d 70 69 onfuse the compi
5ee0: 6c 65 72 20 2a 2f 0a 09 6f 70 74 69 6f 6e 73 20 ler */..options
5ef0: 3d 20 6f 70 74 69 6f 6e 73 3b 0a 7d 0a 0a 2f 2a = options;.}../*
5f00: 20 50 72 6f 63 65 73 73 20 61 6e 20 48 54 54 50 Process an HTTP
5f10: 20 72 65 71 75 65 73 74 20 61 6e 64 20 72 65 74 request and ret
5f20: 75 72 6e 20 74 68 65 20 70 61 74 68 20 72 65 71 urn the path req
5f30: 75 65 73 74 65 64 20 2a 2f 0a 73 74 61 74 69 63 uested */.static
5f40: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 struct filed_ht
5f50: 74 70 5f 72 65 71 75 65 73 74 20 2a 66 69 6c 65 tp_request *file
5f60: 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71 75 65 d_get_http_reque
5f70: 73 74 28 46 49 4c 45 20 2a 66 70 2c 20 73 74 72 st(FILE *fp, str
5f80: 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 uct filed_http_r
5f90: 65 71 75 65 73 74 20 2a 62 75 66 66 65 72 5f 73 equest *buffer_s
5fa0: 74 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f t, struct filed_
5fb0: 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 options *options
5fc0: 29 20 7b 0a 09 63 68 61 72 20 2a 6d 65 74 68 6f ) {..char *metho
5fd0: 64 2c 20 2a 70 61 74 68 3b 0a 09 63 68 61 72 20 d, *path;..char
5fe0: 2a 62 75 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 *buffer, *workbu
5ff0: 66 66 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 ffer, *workbuffe
6000: 72 5f 6e 65 78 74 3b 0a 09 63 68 61 72 20 2a 66 r_next;..char *f
6010: 67 65 74 73 5f 72 65 74 3b 0a 09 73 69 7a 65 5f gets_ret;..size_
6020: 74 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 70 61 t buffer_len, pa
6030: 74 68 5f 6c 65 6e 3b 0a 09 6f 66 66 5f 74 20 72 th_len;..off_t r
6040: 61 6e 67 65 5f 73 74 61 72 74 2c 20 72 61 6e 67 ange_start, rang
6050: 65 5f 65 6e 64 2c 20 72 61 6e 67 65 5f 6c 65 6e e_end, range_len
6060: 67 74 68 3b 0a 09 69 6e 74 20 72 61 6e 67 65 5f gth;..int range_
6070: 72 65 71 75 65 73 74 3b 0a 09 69 6e 74 20 73 6e request;..int sn
6080: 70 72 69 6e 74 66 5f 72 65 74 3b 0a 09 69 6e 74 printf_ret;..int
6090: 20 69 3b 0a 0a 09 2f 2a 20 53 65 74 20 74 6f 20 i;.../* Set to
60a0: 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a default values *
60b0: 2f 0a 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d /..range_start =
60c0: 20 30 3b 0a 09 72 61 6e 67 65 5f 65 6e 64 20 20 0;..range_end
60d0: 20 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 72 65 71 = 0;..range_req
60e0: 75 65 73 74 20 3d 20 30 3b 0a 09 72 61 6e 67 65 uest = 0;..range
60f0: 5f 6c 65 6e 67 74 68 20 3d 20 2d 31 3b 0a 09 62 _length = -1;..b
6100: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 uffer_st->header
6110: 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d s.host.present =
6120: 20 30 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 0;..buffer_st->
6130: 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 headers.connecti
6140: 6f 6e 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 on = FILED_CONNE
6150: 43 54 49 4f 4e 5f 43 4c 4f 53 45 3b 0a 0a 09 62 CTION_CLOSE;...b
6160: 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 uffer = buffer_s
6170: 74 2d 3e 74 6d 70 62 75 66 3b 0a 09 62 75 66 66 t->tmpbuf;..buff
6180: 65 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 er_len = sizeof(
6190: 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 buffer_st->tmpbu
61a0: 66 29 3b 0a 0a 09 66 67 65 74 73 5f 72 65 74 20 f);...fgets_ret
61b0: 3d 20 66 67 65 74 73 28 62 75 66 66 65 72 2c 20 = fgets(buffer,
61c0: 62 75 66 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b buffer_len, fp);
61d0: 0a 09 69 66 20 28 66 67 65 74 73 5f 72 65 74 20 ..if (fgets_ret
61e0: 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 == NULL) {...ret
61f0: 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 urn(NULL);..}...
6200: 6d 65 74 68 6f 64 20 3d 20 62 75 66 66 65 72 3b method = buffer;
6210: 0a 0a 09 62 75 66 66 65 72 20 3d 20 73 74 72 63 ...buffer = strc
6220: 68 72 28 62 75 66 66 65 72 2c 20 27 20 27 29 3b hr(buffer, ' ');
6230: 0a 09 69 66 20 28 62 75 66 66 65 72 20 3d 3d 20 ..if (buffer ==
6240: 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e NULL) {...return
6250: 28 4e 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 2a 62 75 (NULL);..}...*bu
6260: 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 62 75 ffer = '\0';..bu
6270: 66 66 65 72 2b 2b 3b 0a 0a 09 70 61 74 68 20 3d ffer++;...path =
6280: 20 62 75 66 66 65 72 3b 0a 0a 09 2f 2a 20 54 65 buffer;.../* Te
6290: 72 6d 69 6e 61 74 65 20 70 61 74 68 20 63 6f 6d rminate path com
62a0: 70 6f 6e 65 6e 74 20 2a 2f 0a 09 62 75 66 66 65 ponent */..buffe
62b0: 72 20 3d 20 73 74 72 70 62 72 6b 28 70 61 74 68 r = strpbrk(path
62c0: 2c 20 22 5c 72 5c 6e 20 22 29 3b 0a 09 69 66 20 , "\r\n ");..if
62d0: 28 62 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c 29 (buffer != NULL)
62e0: 20 7b 0a 09 09 2a 62 75 66 66 65 72 20 3d 20 27 {...*buffer = '
62f0: 5c 30 27 3b 0a 09 09 62 75 66 66 65 72 2b 2b 3b \0';...buffer++;
6300: 0a 09 7d 0a 0a 09 2f 2a 20 57 65 20 6f 6e 6c 79 ..}.../* We only
6310: 20 68 61 6e 64 6c 65 20 74 68 65 20 22 47 45 54 handle the "GET
6320: 22 20 61 6e 64 20 22 48 45 41 44 27 20 6d 65 74 " and "HEAD' met
6330: 68 6f 64 73 20 2a 2f 0a 09 69 66 20 28 73 74 72 hods */..if (str
6340: 63 61 73 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 casecmp(method,
6350: 22 68 65 61 64 22 29 20 21 3d 20 30 29 20 7b 0a "head") != 0) {.
6360: 09 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 ..if (strcasecmp
6370: 28 6d 65 74 68 6f 64 2c 20 22 67 65 74 22 29 20 (method, "get")
6380: 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 74 75 72 != 0) {....retur
6390: 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 n(NULL);...}....
63a0: 2f 2a 20 47 45 54 20 72 65 71 75 65 73 74 20 2a /* GET request *
63b0: 2f 0a 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 6d /...buffer_st->m
63c0: 65 74 68 6f 64 20 3d 20 46 49 4c 45 44 5f 52 45 ethod = FILED_RE
63d0: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 QUEST_METHOD_GET
63e0: 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 2f 2a ;..} else {.../*
63f0: 20 48 45 41 44 20 72 65 71 75 65 73 74 20 2a 2f HEAD request */
6400: 0a 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 6d 65 ...buffer_st->me
6410: 74 68 6f 64 20 3d 20 46 49 4c 45 44 5f 52 45 51 thod = FILED_REQ
6420: 55 45 53 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 UEST_METHOD_HEAD
6430: 3b 0a 09 7d 0a 0a 09 2f 2a 20 4e 6f 74 65 20 70 ;..}.../* Note p
6440: 61 74 68 20 2a 2f 0a 09 70 61 74 68 5f 6c 65 6e ath */..path_len
6450: 20 3d 20 73 74 72 6c 65 6e 28 70 61 74 68 29 3b = strlen(path);
6460: 0a 09 6d 65 6d 63 70 79 28 62 75 66 66 65 72 5f ..memcpy(buffer_
6470: 73 74 2d 3e 70 61 74 68 2c 20 70 61 74 68 2c 20 st->path, path,
6480: 70 61 74 68 5f 6c 65 6e 20 2b 20 31 29 3b 0a 0a path_len + 1);..
6490: 09 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 79 ./* Determine ty
64a0: 70 65 20 6f 66 20 72 65 71 75 65 73 74 20 66 72 pe of request fr
64b0: 6f 6d 20 70 61 74 68 20 2a 2f 0a 09 69 66 20 28 om path */..if (
64c0: 70 61 74 68 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b path_len == 0) {
64d0: 0a 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 74 79 ...buffer_st->ty
64e0: 70 65 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 pe = FILED_REQUE
64f0: 53 54 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 ST_TYPE_DIRECTOR
6500: 59 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 69 Y;..} else {...i
6510: 66 20 28 70 61 74 68 5b 70 61 74 68 5f 6c 65 6e f (path[path_len
6520: 20 2d 20 31 5d 20 3d 3d 20 27 2f 27 29 20 7b 0a - 1] == '/') {.
6530: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 74 79 ...buffer_st->ty
6540: 70 65 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 pe = FILED_REQUE
6550: 53 54 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 ST_TYPE_DIRECTOR
6560: 59 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 Y;...} else {...
6570: 09 62 75 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 .buffer_st->type
6580: 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 = FILED_REQUEST
6590: 5f 54 59 50 45 5f 4f 54 48 45 52 3b 0a 09 09 7d _TYPE_OTHER;...}
65a0: 0a 09 7d 0a 0a 09 2f 2a 20 52 65 73 65 74 20 62 ..}.../* Reset b
65b0: 75 66 66 65 72 20 66 6f 72 20 6c 61 74 65 72 20 uffer for later
65c0: 75 73 65 20 2a 2f 0a 09 62 75 66 66 65 72 20 3d use */..buffer =
65d0: 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 buffer_st->tmpb
65e0: 75 66 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 uf;...for (i = 0
65f0: 3b 20 69 20 3c 20 31 30 30 3b 20 69 2b 2b 29 20 ; i < 100; i++)
6600: 7b 0a 09 09 66 67 65 74 73 5f 72 65 74 20 3d 20 {...fgets_ret =
6610: 66 67 65 74 73 28 62 75 66 66 65 72 2c 20 62 75 fgets(buffer, bu
6620: 66 66 65 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 ffer_len, fp);..
6630: 09 69 66 20 28 66 67 65 74 73 5f 72 65 74 20 3d .if (fgets_ret =
6640: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 62 72 65 = NULL) {....bre
6650: 61 6b 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 73 ak;...}....if (s
6660: 74 72 6e 63 61 73 65 63 6d 70 28 62 75 66 66 65 trncasecmp(buffe
6670: 72 2c 20 22 52 61 6e 67 65 3a 20 22 2c 20 37 29 r, "Range: ", 7)
6680: 20 3d 3d 20 30 29 20 7b 0a 09 09 09 77 6f 72 6b == 0) {....work
6690: 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 20 buffer = buffer
66a0: 2b 20 37 3b 0a 0a 09 09 09 69 66 20 28 73 74 72 + 7;.....if (str
66b0: 6e 63 61 73 65 63 6d 70 28 77 6f 72 6b 62 75 66 ncasecmp(workbuf
66c0: 66 65 72 2c 20 22 62 79 74 65 73 3d 22 2c 20 36 fer, "bytes=", 6
66d0: 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 09 77 6f ) == 0) {.....wo
66e0: 72 6b 62 75 66 66 65 72 20 2b 3d 20 36 3b 0a 0a rkbuffer += 6;..
66f0: 09 09 09 09 72 61 6e 67 65 5f 72 65 71 75 65 73 ....range_reques
6700: 74 20 3d 20 31 3b 0a 0a 09 09 09 09 72 61 6e 67 t = 1;......rang
6710: 65 5f 73 74 61 72 74 20 3d 20 73 74 72 74 6f 75 e_start = strtou
6720: 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 26 ll(workbuffer, &
6730: 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 2c workbuffer_next,
6740: 20 31 30 29 3b 0a 0a 09 09 09 09 77 6f 72 6b 62 10);......workb
6750: 75 66 66 65 72 20 3d 20 77 6f 72 6b 62 75 66 66 uffer = workbuff
6760: 65 72 5f 6e 65 78 74 3b 0a 0a 09 09 09 09 69 66 er_next;......if
6770: 20 28 2a 77 6f 72 6b 62 75 66 66 65 72 20 3d 3d (*workbuffer ==
6780: 20 27 2d 27 29 20 7b 0a 09 09 09 09 09 77 6f 72 '-') {......wor
6790: 6b 62 75 66 66 65 72 2b 2b 3b 0a 0a 09 09 09 09 kbuffer++;......
67a0: 09 69 66 20 28 2a 77 6f 72 6b 62 75 66 66 65 72 .if (*workbuffer
67b0: 20 21 3d 20 27 5c 72 27 20 26 26 20 2a 77 6f 72 != '\r' && *wor
67c0: 6b 62 75 66 66 65 72 20 21 3d 20 27 5c 6e 27 29 kbuffer != '\n')
67d0: 20 7b 0a 09 09 09 09 09 09 72 61 6e 67 65 5f 65 {.......range_e
67e0: 6e 64 20 3d 20 73 74 72 74 6f 75 6c 6c 28 77 6f nd = strtoull(wo
67f0: 72 6b 62 75 66 66 65 72 2c 20 26 77 6f 72 6b 62 rkbuffer, &workb
6800: 75 66 66 65 72 5f 6e 65 78 74 2c 20 31 30 29 3b uffer_next, 10);
6810: 0a 09 09 09 09 09 7d 0a 09 09 09 09 7d 0a 09 09 ......}.....}...
6820: 09 7d 0a 09 09 7d 20 65 6c 73 65 20 69 66 20 28 .}...} else if (
6830: 73 74 72 6e 63 61 73 65 63 6d 70 28 62 75 66 66 strncasecmp(buff
6840: 65 72 2c 20 22 48 6f 73 74 3a 20 22 2c 20 35 29 er, "Host: ", 5)
6850: 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 75 66 66 == 0) {....buff
6860: 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 er_st->headers.h
6870: 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d 20 31 3b ost.present = 1;
6880: 0a 0a 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 .....workbuffer
6890: 3d 20 73 74 72 70 62 72 6b 28 62 75 66 66 65 72 = strpbrk(buffer
68a0: 20 2b 20 35 2c 20 22 5c 72 5c 6e 3a 22 29 3b 0a + 5, "\r\n:");.
68b0: 09 09 09 69 66 20 28 77 6f 72 6b 62 75 66 66 65 ...if (workbuffe
68c0: 72 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 r != NULL) {....
68d0: 09 2a 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 27 .*workbuffer = '
68e0: 5c 30 27 3b 0a 09 09 09 7d 0a 0a 09 09 09 77 6f \0';....}.....wo
68f0: 72 6b 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 rkbuffer = buffe
6900: 72 20 2b 20 35 3b 0a 09 09 09 77 68 69 6c 65 20 r + 5;....while
6910: 28 2a 77 6f 72 6b 62 75 66 66 65 72 20 3d 3d 20 (*workbuffer ==
6920: 27 20 27 29 20 7b 0a 09 09 09 09 77 6f 72 6b 62 ' ') {.....workb
6930: 75 66 66 65 72 2b 2b 3b 0a 09 09 09 7d 0a 0a 09 uffer++;....}...
6940: 09 09 73 74 72 63 70 79 28 62 75 66 66 65 72 5f ..strcpy(buffer_
6950: 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 st->headers.host
6960: 2e 68 6f 73 74 2c 20 77 6f 72 6b 62 75 66 66 65 .host, workbuffe
6970: 72 29 3b 0a 09 09 7d 20 65 6c 73 65 20 69 66 20 r);...} else if
6980: 28 73 74 72 6e 63 61 73 65 63 6d 70 28 62 75 66 (strncasecmp(buf
6990: 66 65 72 2c 20 22 43 6f 6e 6e 65 63 74 69 6f 6e fer, "Connection
69a0: 3a 20 4b 65 65 70 2d 41 6c 69 76 65 22 2c 20 32 : Keep-Alive", 2
69b0: 32 29 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 75 2) == 0) {....bu
69c0: 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 ffer_st->headers
69d0: 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 46 49 .connection = FI
69e0: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b LED_CONNECTION_K
69f0: 45 45 50 5f 41 4c 49 56 45 3b 0a 09 09 7d 0a 0a EEP_ALIVE;...}..
6a00: 09 09 69 66 20 28 6d 65 6d 63 6d 70 28 62 75 66 ..if (memcmp(buf
6a10: 66 65 72 2c 20 22 5c 72 5c 6e 22 2c 20 32 29 20 fer, "\r\n", 2)
6a20: 3d 3d 20 30 29 20 7b 0a 09 09 09 62 72 65 61 6b == 0) {....break
6a30: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 65 ;...}..}.../* De
6a40: 74 65 72 6d 69 6e 65 20 72 61 6e 67 65 20 2a 2f termine range */
6a50: 0a 09 69 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 ..if (range_end
6a60: 21 3d 20 30 29 20 7b 0a 09 09 69 66 20 28 72 61 != 0) {...if (ra
6a70: 6e 67 65 5f 65 6e 64 20 3c 3d 20 72 61 6e 67 65 nge_end <= range
6a80: 5f 73 74 61 72 74 29 20 7b 0a 09 09 09 72 65 74 _start) {....ret
6a90: 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a urn(NULL);...}..
6aa0: 09 09 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d ..range_length =
6ab0: 20 72 61 6e 67 65 5f 65 6e 64 20 2d 20 72 61 6e range_end - ran
6ac0: 67 65 5f 73 74 61 72 74 3b 0a 0a 09 09 66 69 6c ge_start;....fil
6ad0: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
6ae0: 28 22 43 6f 6d 70 75 74 69 6e 67 20 6c 65 6e 67 ("Computing leng
6af0: 74 68 20 70 61 72 61 6d 65 74 65 72 3a 20 25 6c th parameter: %l
6b00: 6c 75 20 3d 20 25 6c 6c 75 20 2d 20 25 6c 6c 75 lu = %llu - %llu
6b10: 22 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ",....(unsigned
6b20: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 long long) range
6b30: 5f 6c 65 6e 67 74 68 2c 0a 09 09 09 28 75 6e 73 _length,....(uns
6b40: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
6b50: 20 72 61 6e 67 65 5f 65 6e 64 2c 0a 09 09 09 28 range_end,....(
6b60: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
6b70: 6e 67 29 20 72 61 6e 67 65 5f 73 74 61 72 74 0a ng) range_start.
6b80: 09 09 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 46 69 6c ..);..}.../* Fil
6b90: 6c 20 75 70 20 73 74 72 75 63 74 75 72 65 20 74 l up structure t
6ba0: 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 09 62 75 66 o return */..buf
6bb0: 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e fer_st->headers.
6bc0: 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 20 3d 20 range.present =
6bd0: 72 61 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a 09 range_request;..
6be0: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
6bf0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 rs.range.offset
6c00: 20 3d 20 72 61 6e 67 65 5f 73 74 61 72 74 3b 0a = range_start;.
6c10: 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 .buffer_st->head
6c20: 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 ers.range.length
6c30: 20 20 3d 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 = range_length
6c40: 3b 0a 0a 09 2f 2a 20 49 66 20 76 68 6f 73 74 73 ;.../* If vhosts
6c50: 20 61 72 65 20 65 6e 61 62 6c 65 64 2c 20 63 6f are enabled, co
6c60: 6d 70 75 74 65 20 6e 65 77 20 70 61 74 68 20 2a mpute new path *
6c70: 2f 0a 09 69 66 20 28 6f 70 74 69 6f 6e 73 2d 3e /..if (options->
6c80: 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 64 29 20 vhosts_enabled)
6c90: 7b 0a 09 09 69 66 20 28 62 75 66 66 65 72 5f 73 {...if (buffer_s
6ca0: 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e t->headers.host.
6cb0: 70 72 65 73 65 6e 74 20 3d 3d 20 31 29 20 7b 0a present == 1) {.
6cc0: 09 09 09 62 75 66 66 65 72 20 3d 20 62 75 66 66 ...buffer = buff
6cd0: 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a 09 er_st->tmpbuf;..
6ce0: 09 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 73 ..buffer_len = s
6cf0: 69 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 74 2d izeof(buffer_st-
6d00: 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 09 09 73 6e >tmpbuf);.....sn
6d10: 70 72 69 6e 74 66 5f 72 65 74 20 3d 20 73 6e 70 printf_ret = snp
6d20: 72 69 6e 74 66 28 62 75 66 66 65 72 2c 20 62 75 rintf(buffer, bu
6d30: 66 66 65 72 5f 6c 65 6e 2c 20 22 2f 25 73 25 73 ffer_len, "/%s%s
6d40: 25 73 22 2c 0a 09 09 09 09 62 75 66 66 65 72 5f %s",.....buffer_
6d50: 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 st->headers.host
6d60: 2e 68 6f 73 74 2c 0a 09 09 09 09 62 75 66 66 65 .host,.....buffe
6d70: 72 5f 73 74 2d 3e 70 61 74 68 5b 30 5d 20 3d 3d r_st->path[0] ==
6d80: 20 27 2f 27 20 3f 20 22 22 20 3a 20 22 2f 22 2c '/' ? "" : "/",
6d90: 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e .....buffer_st->
6da0: 70 61 74 68 0a 09 09 09 29 3b 0a 09 09 09 69 66 path....);....if
6db0: 20 28 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3e (snprintf_ret >
6dc0: 3d 20 30 29 20 7b 0a 09 09 09 09 69 66 20 28 28 = 0) {.....if ((
6dd0: 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 20 73 (unsigned int) s
6de0: 6e 70 72 69 6e 74 66 5f 72 65 74 29 20 3c 20 62 nprintf_ret) < b
6df0: 75 66 66 65 72 5f 6c 65 6e 29 20 7b 0a 09 09 09 uffer_len) {....
6e00: 09 09 73 74 72 63 70 79 28 62 75 66 66 65 72 5f ..strcpy(buffer_
6e10: 73 74 2d 3e 70 61 74 68 2c 20 62 75 66 66 65 72 st->path, buffer
6e20: 29 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 );.....}....}...
6e30: 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 62 75 }..}...return(bu
6e40: 66 66 65 72 5f 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 ffer_st);.}../*
6e50: 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
6e60: 70 61 67 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 page */.static v
6e70: 6f 69 64 20 66 69 6c 65 64 5f 65 72 72 6f 72 5f oid filed_error_
6e80: 70 61 67 65 28 46 49 4c 45 20 2a 66 70 2c 20 63 page(FILE *fp, c
6e90: 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 65 5f onst char *date_
6ea0: 63 75 72 72 65 6e 74 2c 20 69 6e 74 20 65 72 72 current, int err
6eb0: 6f 72 5f 6e 75 6d 62 65 72 2c 20 69 6e 74 20 6d or_number, int m
6ec0: 65 74 68 6f 64 2c 20 63 6f 6e 73 74 20 63 68 61 ethod, const cha
6ed0: 72 20 2a 72 65 61 73 6f 6e 2c 20 73 74 72 75 63 r *reason, struc
6ee0: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
6ef0: 79 20 2a 6c 6f 67 29 20 7b 0a 09 63 68 61 72 20 y *log) {..char
6f00: 2a 65 72 72 6f 72 5f 73 74 72 69 6e 67 20 3d 20 *error_string =
6f10: 22 3c 68 74 6d 6c 3e 3c 68 65 61 64 3e 3c 74 69 "<html><head><ti
6f20: 74 6c 65 3e 45 52 52 4f 52 3c 2f 74 69 74 6c 65 tle>ERROR</title
6f30: 3e 3c 2f 68 65 61 64 3e 3c 62 6f 64 79 3e 55 6e ></head><body>Un
6f40: 61 62 6c 65 20 74 6f 20 70 72 6f 63 65 73 73 20 able to process
6f50: 72 65 71 75 65 73 74 3c 2f 62 6f 64 79 3e 3c 2f request</body></
6f60: 68 74 6d 6c 3e 22 3b 0a 0a 09 66 70 72 69 6e 74 html>";...fprint
6f70: 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 f(fp, "HTTP/1.1
6f80: 25 69 20 4e 6f 74 20 4f 4b 5c 72 5c 6e 44 61 74 %i Not OK\r\nDat
6f90: 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a e: %s\r\nServer:
6fa0: 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d filed\r\nLast-M
6fb0: 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 odified: %s\r\nC
6fc0: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 ontent-Length: %
6fd0: 6c 6c 75 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 llu\r\nContent-T
6fe0: 79 70 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 ype: %s\r\nConne
6ff0: 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e ction: close\r\n
7000: 5c 72 5c 6e 22 2c 0a 09 09 65 72 72 6f 72 5f 6e \r\n",...error_n
7010: 75 6d 62 65 72 2c 0a 09 09 64 61 74 65 5f 63 75 umber,...date_cu
7020: 72 72 65 6e 74 2c 0a 09 09 64 61 74 65 5f 63 75 rrent,...date_cu
7030: 72 72 65 6e 74 2c 0a 09 09 28 75 6e 73 69 67 6e rrent,...(unsign
7040: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 73 74 ed long long) st
7050: 72 6c 65 6e 28 65 72 72 6f 72 5f 73 74 72 69 6e rlen(error_strin
7060: 67 29 2c 0a 09 09 22 74 65 78 74 2f 68 74 6d 6c g),..."text/html
7070: 22 0a 09 29 3b 0a 0a 09 2f 2a 20 73 69 6c 65 6e "..);.../* silen
7080: 63 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 ce error string
7090: 66 6f 72 20 48 45 41 44 20 72 65 71 75 65 73 74 for HEAD request
70a0: 73 20 2a 2f 0a 09 69 66 20 28 6d 65 74 68 6f 64 s */..if (method
70b0: 20 21 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 != FILED_REQUES
70c0: 54 5f 4d 45 54 48 4f 44 5f 48 45 41 44 29 20 7b T_METHOD_HEAD) {
70d0: 0a 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 ...fprintf(fp, "
70e0: 25 73 22 2c 20 65 72 72 6f 72 5f 73 74 72 69 6e %s", error_strin
70f0: 67 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 4c 6f 67 20 g);..}.../* Log
7100: 65 72 72 6f 72 20 2a 2f 0a 09 2f 2a 2a 20 72 65 error */../** re
7110: 61 73 6f 6e 20 6d 75 73 74 20 70 6f 69 6e 74 20 ason must point
7120: 74 6f 20 61 20 67 6c 6f 62 61 6c 6c 79 20 61 6c to a globally al
7130: 6c 6f 63 61 74 65 64 20 76 61 6c 75 65 20 2a 2a located value **
7140: 2f 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d /..log->reason =
7150: 20 72 65 61 73 6f 6e 3b 0a 09 6c 6f 67 2d 3e 68 reason;..log->h
7160: 74 74 70 5f 63 6f 64 65 20 3d 20 65 72 72 6f 72 ttp_code = error
7170: 5f 6e 75 6d 62 65 72 3b 0a 0a 09 66 69 6c 65 64 _number;...filed
7180: 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b _log_entry(log);
7190: 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20 63 6f 6e 6e .../* Close conn
71a0: 65 63 74 69 6f 6e 20 2a 2f 0a 09 66 69 6c 65 64 ection */..filed
71b0: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 _sockettimeout_c
71c0: 6c 6f 73 65 28 66 69 6c 65 6e 6f 28 66 70 29 2c lose(fileno(fp),
71d0: 20 30 29 3b 0a 0a 09 66 63 6c 6f 73 65 28 66 70 0);...fclose(fp
71e0: 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a );...return;.}..
71f0: 2f 2a 20 52 65 74 75 72 6e 20 61 20 72 65 64 69 /* Return a redi
7200: 72 65 63 74 20 74 6f 20 69 6e 64 65 78 2e 68 74 rect to index.ht
7210: 6d 6c 20 2a 2f 0a 23 69 66 6e 64 65 66 20 46 49 ml */.#ifndef FI
7220: 4c 45 44 5f 44 4f 4e 54 5f 52 45 44 49 52 45 43 LED_DONT_REDIREC
7230: 54 5f 44 49 52 45 43 54 4f 52 49 45 53 0a 73 74 T_DIRECTORIES.st
7240: 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f atic void filed_
7250: 72 65 64 69 72 65 63 74 5f 69 6e 64 65 78 28 46 redirect_index(F
7260: 49 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63 ILE *fp, const c
7270: 68 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 6e har *date_curren
7280: 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 t, const char *p
7290: 61 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c 65 ath, struct file
72a0: 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 d_log_entry *log
72b0: 29 20 7b 0a 09 69 6e 74 20 68 74 74 70 5f 63 6f ) {..int http_co
72c0: 64 65 20 3d 20 33 30 32 3b 0a 09 66 70 72 69 6e de = 302;..fprin
72d0: 74 66 28 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 tf(fp, "HTTP/1.1
72e0: 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 %i OK\r\nDate:
72f0: 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 %s\r\nServer: fi
7300: 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 led\r\nLast-Modi
7310: 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 fied: %s\r\nCont
7320: 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 30 5c 72 5c ent-Length: 0\r\
7330: 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f nConnection: clo
7340: 73 65 5c 72 5c 6e 4c 6f 63 61 74 69 6f 6e 3a 20 se\r\nLocation:
7350: 25 73 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 68 %s\r\n\r\n",...h
7360: 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64 61 74 65 ttp_code,...date
7370: 5f 63 75 72 72 65 6e 74 2c 0a 09 09 64 61 74 65 _current,...date
7380: 5f 63 75 72 72 65 6e 74 2c 0a 09 09 22 69 6e 64 _current,..."ind
7390: 65 78 2e 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f ex.html"..);.../
73a0: 2a 20 4c 6f 67 20 72 65 64 69 72 65 63 74 20 2a * Log redirect *
73b0: 2f 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d /..log->reason =
73c0: 20 22 72 65 64 69 72 65 63 74 22 3b 0a 09 6c 6f "redirect";..lo
73d0: 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 68 g->http_code = h
73e0: 74 74 70 5f 63 6f 64 65 3b 0a 0a 09 66 69 6c 65 ttp_code;...file
73f0: 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 d_log_entry(log)
7400: 3b 0a 0a 09 2f 2a 20 43 6c 6f 73 65 20 63 6f 6e ;.../* Close con
7410: 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 66 69 6c 65 nection */..file
7420: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
7430: 63 6c 6f 73 65 28 66 69 6c 65 6e 6f 28 66 70 29 close(fileno(fp)
7440: 2c 20 30 29 3b 0a 0a 09 66 63 6c 6f 73 65 28 66 , 0);...fclose(f
7450: 70 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 0a 09 p);...return;...
7460: 2f 2a 20 43 75 72 72 65 6e 74 6c 79 20 75 6e 75 /* Currently unu
7470: 73 65 64 3a 20 70 61 74 68 20 2a 2f 0a 09 70 61 sed: path */..pa
7480: 74 68 20 3d 20 70 61 74 68 3b 0a 7d 0a 23 65 6e th = path;.}.#en
7490: 64 69 66 0a 0a 2f 2a 20 43 6f 6e 76 65 72 74 20 dif../* Convert
74a0: 61 6e 20 65 6e 75 6d 20 72 65 70 72 65 73 65 6e an enum represen
74b0: 74 69 6e 67 20 74 68 65 20 22 43 6f 6e 6e 65 63 ting the "Connec
74c0: 74 69 6f 6e 22 20 68 65 61 64 65 72 20 76 61 6c tion" header val
74d0: 75 65 20 74 6f 20 61 20 73 74 72 69 6e 67 20 2a ue to a string *
74e0: 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 /.static const c
74f0: 68 61 72 20 2a 66 69 6c 65 64 5f 63 6f 6e 6e 65 har *filed_conne
7500: 63 74 69 6f 6e 5f 73 74 72 28 69 6e 74 20 63 6f ction_str(int co
7510: 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c 75 65 29 20 nnection_value)
7520: 7b 0a 09 73 77 69 74 63 68 20 28 63 6f 6e 6e 65 {..switch (conne
7530: 63 74 69 6f 6e 5f 76 61 6c 75 65 29 20 7b 0a 09 ction_value) {..
7540: 09 63 61 73 65 20 46 49 4c 45 44 5f 43 4f 4e 4e .case FILED_CONN
7550: 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 3a 0a 09 09 ECTION_CLOSE:...
7560: 09 72 65 74 75 72 6e 28 22 63 6c 6f 73 65 22 29 .return("close")
7570: 3b 0a 09 09 63 61 73 65 20 46 49 4c 45 44 5f 43 ;...case FILED_C
7580: 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 ONNECTION_KEEP_A
7590: 4c 49 56 45 3a 0a 09 09 09 72 65 74 75 72 6e 28 LIVE:....return(
75a0: 22 6b 65 65 70 2d 61 6c 69 76 65 22 29 3b 0a 09 "keep-alive");..
75b0: 7d 0a 0a 09 72 65 74 75 72 6e 28 22 63 6c 6f 73 }...return("clos
75c0: 65 22 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e 64 6c e");.}../* Handl
75d0: 65 20 61 20 73 69 6e 67 6c 65 20 72 65 71 75 65 e a single reque
75e0: 73 74 20 66 72 6f 6d 20 61 20 63 6c 69 65 6e 74 st from a client
75f0: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 */.static int f
7600: 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69 65 iled_handle_clie
7610: 6e 74 28 69 6e 74 20 66 64 2c 20 73 74 72 75 63 nt(int fd, struc
7620: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 t filed_http_req
7630: 75 65 73 74 20 2a 72 65 71 75 65 73 74 2c 20 73 uest *request, s
7640: 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f truct filed_log_
7650: 65 6e 74 72 79 20 2a 6c 6f 67 2c 20 73 74 72 75 entry *log, stru
7660: 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 ct filed_options
7670: 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 *options) {..st
7680: 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 ruct filed_filei
7690: 6e 66 6f 20 2a 66 69 6c 65 69 6e 66 6f 3b 0a 09 nfo *fileinfo;..
76a0: 73 73 69 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65 ssize_t sendfile
76b0: 5f 72 65 74 3b 0a 09 73 69 7a 65 5f 74 20 73 65 _ret;..size_t se
76c0: 6e 64 66 69 6c 65 5f 73 69 7a 65 3b 0a 09 6f 66 ndfile_size;..of
76d0: 66 5f 74 20 73 65 6e 64 66 69 6c 65 5f 6f 66 66 f_t sendfile_off
76e0: 73 65 74 2c 20 73 65 6e 64 66 69 6c 65 5f 73 65 set, sendfile_se
76f0: 6e 74 2c 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e nt, sendfile_len
7700: 3b 0a 09 63 68 61 72 20 2a 70 61 74 68 3b 0a 09 ;..char *path;..
7710: 63 68 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 char *date_curre
7720: 6e 74 2c 20 64 61 74 65 5f 63 75 72 72 65 6e 74 nt, date_current
7730: 5f 62 5b 36 34 5d 3b 0a 09 69 6e 74 20 68 74 74 _b[64];..int htt
7740: 70 5f 63 6f 64 65 3b 0a 09 46 49 4c 45 20 2a 66 p_code;..FILE *f
7750: 70 3b 0a 0a 09 2f 2a 20 49 6e 64 69 63 61 74 65 p;.../* Indicate
7760: 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
7770: 73 74 61 72 74 20 74 69 6d 65 20 2a 2f 0a 09 6c start time */..l
7780: 6f 67 2d 3e 63 6f 6e 6e 65 63 74 74 69 6d 65 20 og->connecttime
7790: 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 = time(NULL);...
77a0: 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 63 75 72 /* Determine cur
77b0: 72 65 6e 74 20 74 69 6d 65 20 2a 2f 0a 09 64 61 rent time */..da
77c0: 74 65 5f 63 75 72 72 65 6e 74 20 3d 20 66 69 6c te_current = fil
77d0: 65 64 5f 66 6f 72 6d 61 74 5f 74 69 6d 65 28 64 ed_format_time(d
77e0: 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 2c 20 73 ate_current_b, s
77f0: 69 7a 65 6f 66 28 64 61 74 65 5f 63 75 72 72 65 izeof(date_curre
7800: 6e 74 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c nt_b), time(NULL
7810: 29 29 3b 0a 0a 09 2f 2a 20 4f 70 65 6e 20 73 6f ));.../* Open so
7820: 63 6b 65 74 20 61 73 20 41 4e 53 49 20 49 2f 4f cket as ANSI I/O
7830: 20 66 6f 72 20 65 61 73 65 20 6f 66 20 75 73 65 for ease of use
7840: 20 2a 2f 0a 09 66 70 20 3d 20 66 64 6f 70 65 6e */..fp = fdopen
7850: 28 66 64 2c 20 22 77 2b 62 22 29 3b 0a 09 69 66 (fd, "w+b");..if
7860: 20 28 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a (fp == NULL) {.
7870: 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
7880: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 64 2c 20 meout_close(fd,
7890: 30 29 3b 0a 0a 09 09 63 6c 6f 73 65 28 66 64 29 0);....close(fd)
78a0: 3b 0a 0a 09 09 6c 6f 67 2d 3e 62 75 66 66 65 72 ;....log->buffer
78b0: 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 6c 6f [0] = '\0';...lo
78c0: 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 2d g->http_code = -
78d0: 31 3b 0a 09 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 1;...log->reason
78e0: 20 3d 20 22 66 64 6f 70 65 6e 5f 66 61 69 6c 65 = "fdopen_faile
78f0: 64 22 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 d";....filed_log
7900: 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 09 _entry(log);....
7910: 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e return(FILED_CON
7920: 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a NECTION_CLOSE);.
7930: 09 7d 0a 0a 09 72 65 71 75 65 73 74 20 3d 20 66 .}...request = f
7940: 69 6c 65 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 iled_get_http_re
7950: 71 75 65 73 74 28 66 70 2c 20 72 65 71 75 65 73 quest(fp, reques
7960: 74 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a 0a 09 69 t, options);...i
7970: 66 20 28 72 65 71 75 65 73 74 20 3d 3d 20 4e 55 f (request == NU
7980: 4c 4c 29 20 7b 0a 09 09 6c 6f 67 2d 3e 62 75 66 LL) {...log->buf
7990: 66 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a fer[0] = '\0';..
79a0: 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 ..filed_error_pa
79b0: 67 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 ge(fp, date_curr
79c0: 65 6e 74 2c 20 35 30 30 2c 20 46 49 4c 45 44 5f ent, 500, FILED_
79d0: 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 REQUEST_METHOD_G
79e0: 45 54 2c 20 22 66 6f 72 6d 61 74 22 2c 20 6c 6f ET, "format", lo
79f0: 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 g);....return(FI
7a00: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 LED_CONNECTION_C
7a10: 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 LOSE);..}...file
7a20: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
7a30: 70 72 6f 63 65 73 73 69 6e 67 5f 73 74 61 72 74 processing_start
7a40: 28 66 64 29 3b 0a 0a 09 70 61 74 68 20 3d 20 72 (fd);...path = r
7a50: 65 71 75 65 73 74 2d 3e 70 61 74 68 3b 0a 09 73 equest->path;..s
7a60: 74 72 63 70 79 28 6c 6f 67 2d 3e 62 75 66 66 65 trcpy(log->buffe
7a70: 72 2c 20 70 61 74 68 29 3b 0a 09 6c 6f 67 2d 3e r, path);..log->
7a80: 6d 65 74 68 6f 64 20 3d 20 72 65 71 75 65 73 74 method = request
7a90: 2d 3e 6d 65 74 68 6f 64 3b 0a 0a 09 2f 2a 20 49 ->method;.../* I
7aa0: 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
7ab0: 70 61 74 68 20 69 73 20 61 20 64 69 72 65 63 74 path is a direct
7ac0: 6f 72 79 2c 20 72 65 64 69 72 65 63 74 20 74 6f ory, redirect to
7ad0: 20 69 6e 64 65 78 20 70 61 67 65 20 2a 2f 0a 09 index page */..
7ae0: 69 66 20 28 72 65 71 75 65 73 74 2d 3e 74 79 70 if (request->typ
7af0: 65 20 3d 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 e == FILED_REQUE
7b00: 53 54 5f 54 59 50 45 5f 44 49 52 45 43 54 4f 52 ST_TYPE_DIRECTOR
7b10: 59 29 20 7b 0a 23 69 66 64 65 66 20 46 49 4c 45 Y) {.#ifdef FILE
7b20: 44 5f 44 4f 4e 54 5f 52 45 44 49 52 45 43 54 5f D_DONT_REDIRECT_
7b30: 44 49 52 45 43 54 4f 52 49 45 53 0a 09 09 63 68 DIRECTORIES...ch
7b40: 61 72 20 6c 6f 63 61 6c 70 61 74 68 5b 38 31 39 ar localpath[819
7b50: 32 5d 3b 0a 09 09 69 6e 74 20 73 6e 70 72 69 6e 2];...int snprin
7b60: 74 66 5f 72 65 74 3b 0a 0a 09 09 73 6e 70 72 69 tf_ret;....snpri
7b70: 6e 74 66 5f 72 65 74 20 3d 20 73 6e 70 72 69 6e ntf_ret = snprin
7b80: 74 66 28 6c 6f 63 61 6c 70 61 74 68 2c 20 73 69 tf(localpath, si
7b90: 7a 65 6f 66 28 6c 6f 63 61 6c 70 61 74 68 29 2c zeof(localpath),
7ba0: 20 22 25 73 2f 69 6e 64 65 78 2e 68 74 6d 6c 22 "%s/index.html"
7bb0: 2c 20 70 61 74 68 29 3b 0a 0a 09 09 69 66 20 28 , path);....if (
7bc0: 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3c 3d 20 snprintf_ret <=
7bd0: 30 20 7c 7c 20 73 6e 70 72 69 6e 74 66 5f 72 65 0 || snprintf_re
7be0: 74 20 3e 20 28 73 69 7a 65 6f 66 28 6c 6f 63 61 t > (sizeof(loca
7bf0: 6c 70 61 74 68 29 20 2d 20 31 29 29 20 7b 0a 09 lpath) - 1)) {..
7c00: 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 ..filed_error_pa
7c10: 67 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 ge(fp, date_curr
7c20: 65 6e 74 2c 20 35 30 30 2c 20 72 65 71 75 65 73 ent, 500, reques
7c30: 74 2d 3e 6d 65 74 68 6f 64 2c 20 22 70 61 74 68 t->method, "path
7c40: 5f 66 6f 72 6d 61 74 22 2c 20 6c 6f 67 29 3b 0a _format", log);.
7c50: 0a 09 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 ....return(FILED
7c60: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 _CONNECTION_CLOS
7c70: 45 29 3b 0a 09 09 7d 0a 0a 09 09 70 61 74 68 20 E);...}....path
7c80: 3d 20 6c 6f 63 61 6c 70 61 74 68 3b 0a 23 65 6c = localpath;.#el
7c90: 73 65 0a 09 09 66 69 6c 65 64 5f 72 65 64 69 72 se...filed_redir
7ca0: 65 63 74 5f 69 6e 64 65 78 28 66 70 2c 20 64 61 ect_index(fp, da
7cb0: 74 65 5f 63 75 72 72 65 6e 74 2c 20 70 61 74 68 te_current, path
7cc0: 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 , log);....retur
7cd0: 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 n(FILED_CONNECTI
7ce0: 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 23 65 6e 64 69 ON_CLOSE);.#endi
7cf0: 66 0a 09 7d 0a 0a 09 66 69 6c 65 69 6e 66 6f 20 f..}...fileinfo
7d00: 3d 20 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 6c = filed_open_fil
7d10: 65 28 70 61 74 68 2c 20 26 72 65 71 75 65 73 74 e(path, &request
7d20: 2d 3e 66 69 6c 65 69 6e 66 6f 2c 20 6f 70 74 69 ->fileinfo, opti
7d30: 6f 6e 73 29 3b 0a 09 69 66 20 28 66 69 6c 65 69 ons);..if (filei
7d40: 6e 66 6f 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 nfo == NULL) {..
7d50: 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 .filed_error_pag
7d60: 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 e(fp, date_curre
7d70: 6e 74 2c 20 34 30 34 2c 20 72 65 71 75 65 73 74 nt, 404, request
7d80: 2d 3e 6d 65 74 68 6f 64 2c 20 22 6f 70 65 6e 5f ->method, "open_
7d90: 66 61 69 6c 65 64 22 2c 20 6c 6f 67 29 3b 0a 0a failed", log);..
7da0: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 ..return(FILED_C
7db0: 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 ONNECTION_CLOSE)
7dc0: 3b 0a 09 7d 0a 0a 09 69 66 20 28 72 65 71 75 65 ;..}...if (reque
7dd0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
7de0: 65 2e 70 72 65 73 65 6e 74 29 20 7b 0a 09 09 69 e.present) {...i
7df0: 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 f (request->head
7e00: 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 ers.range.offset
7e10: 20 21 3d 20 30 20 7c 7c 20 72 65 71 75 65 73 74 != 0 || request
7e20: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
7e30: 6c 65 6e 67 74 68 20 3e 3d 20 30 29 20 7b 0a 09 length >= 0) {..
7e40: 09 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 ..if (request->h
7e50: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
7e60: 73 65 74 20 3e 3d 20 66 69 6c 65 69 6e 66 6f 2d set >= fileinfo-
7e70: 3e 6c 65 6e 29 20 7b 0a 09 09 09 09 66 69 6c 65 >len) {.....file
7e80: 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c d_error_page(fp,
7e90: 20 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 date_current, 4
7ea0: 31 36 2c 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 16, request->met
7eb0: 68 6f 64 2c 20 22 72 61 6e 67 65 5f 69 6e 76 61 hod, "range_inva
7ec0: 6c 69 64 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 09 lid", log);.....
7ed0: 09 63 6c 6f 73 65 28 66 69 6c 65 69 6e 66 6f 2d .close(fileinfo-
7ee0: 3e 66 64 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 >fd);......retur
7ef0: 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 n(FILED_CONNECTI
7f00: 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 09 09 7d 0a ON_CLOSE);....}.
7f10: 0a 09 09 09 69 66 20 28 72 65 71 75 65 73 74 2d ....if (request-
7f20: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c >headers.range.l
7f30: 65 6e 67 74 68 20 3d 3d 20 28 28 6f 66 66 5f 74 ength == ((off_t
7f40: 29 20 2d 31 29 29 20 7b 0a 09 09 09 09 66 69 6c ) -1)) {.....fil
7f50: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
7f60: 28 22 43 6f 6d 70 75 74 69 6e 67 20 6c 65 6e 67 ("Computing leng
7f70: 74 68 20 74 6f 20 66 69 74 20 69 6e 20 62 6f 75 th to fit in bou
7f80: 6e 64 73 3a 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c nds: fileinfo->l
7f90: 65 6e 20 3d 20 25 6c 6c 75 2c 20 72 65 71 75 65 en = %llu, reque
7fa0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
7fb0: 65 2e 6f 66 66 73 65 74 20 3d 20 25 6c 6c 75 22 e.offset = %llu"
7fc0: 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 ,......(unsigned
7fd0: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 long long) file
7fe0: 69 6e 66 6f 2d 3e 6c 65 6e 2c 0a 09 09 09 09 09 info->len,......
7ff0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
8000: 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 ong) request->he
8010: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
8020: 65 74 0a 09 09 09 09 29 3b 0a 0a 09 09 09 09 72 et.....);......r
8030: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
8040: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20 66 range.length = f
8050: 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20 2d 20 72 ileinfo->len - r
8060: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
8070: 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 09 range.offset;...
8080: 09 7d 0a 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 .}.....filed_log
8090: 5f 6d 73 67 5f 64 65 62 75 67 28 22 50 61 72 74 _msg_debug("Part
80a0: 69 61 6c 20 72 65 71 75 65 73 74 2c 20 73 74 61 ial request, sta
80b0: 72 74 69 6e 67 20 61 74 3a 20 25 6c 6c 75 20 61 rting at: %llu a
80c0: 6e 64 20 72 75 6e 6e 69 6e 67 20 66 6f 72 20 25 nd running for %
80d0: 6c 6c 69 20 62 79 74 65 73 22 2c 0a 09 09 09 09 lli bytes",.....
80e0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
80f0: 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 ong) request->he
8100: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
8110: 65 74 2c 0a 09 09 09 09 28 6c 6f 6e 67 20 6c 6f et,.....(long lo
8120: 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 ng) request->hea
8130: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 ders.range.lengt
8140: 68 0a 09 09 09 29 3b 0a 0a 09 09 7d 0a 0a 09 09 h....);....}....
8150: 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 36 3b http_code = 206;
8160: 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 68 74 74 ..} else {...htt
8170: 70 5f 63 6f 64 65 20 3d 20 32 30 30 3b 0a 0a 09 p_code = 200;...
8180: 09 2f 2a 20 43 6f 6d 70 75 74 65 20 66 61 6b 65 ./* Compute fake
8190: 20 72 61 6e 67 65 20 70 61 72 61 6d 65 74 65 72 range parameter
81a0: 73 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73 20 s that includes
81b0: 74 68 65 20 65 6e 74 69 72 65 20 66 69 6c 65 20 the entire file
81c0: 2a 2f 0a 09 09 72 65 71 75 65 73 74 2d 3e 68 65 */...request->he
81d0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
81e0: 65 74 20 3d 20 30 3b 0a 09 09 72 65 71 75 65 73 et = 0;...reques
81f0: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
8200: 2e 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e .length = filein
8210: 66 6f 2d 3e 6c 65 6e 3b 0a 09 7d 0a 0a 09 66 70 fo->len;..}...fp
8220: 72 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f rintf(fp, "HTTP/
8230: 31 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 1.1 %i OK\r\nDat
8240: 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a e: %s\r\nServer:
8250: 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d filed\r\nLast-M
8260: 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 odified: %s\r\nC
8270: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 ontent-Length: %
8280: 6c 6c 75 5c 72 5c 6e 41 63 63 65 70 74 2d 52 61 llu\r\nAccept-Ra
8290: 6e 67 65 73 3a 20 62 79 74 65 73 5c 72 5c 6e 43 nges: bytes\r\nC
82a0: 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c ontent-Type: %s\
82b0: 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 25 r\nConnection: %
82c0: 73 5c 72 5c 6e 45 54 61 67 3a 20 5c 22 25 73 5c s\r\nETag: \"%s\
82d0: 22 5c 72 5c 6e 22 2c 0a 09 09 68 74 74 70 5f 63 "\r\n",...http_c
82e0: 6f 64 65 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 ode,...date_curr
82f0: 65 6e 74 2c 0a 09 09 66 69 6c 65 69 6e 66 6f 2d ent,...fileinfo-
8300: 3e 6c 61 73 74 6d 6f 64 2c 0a 09 09 28 75 6e 73 >lastmod,...(uns
8310: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
8320: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
8330: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 2c 0a s.range.length,.
8340: 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 74 79 70 65 ..fileinfo->type
8350: 2c 0a 09 09 66 69 6c 65 64 5f 63 6f 6e 6e 65 63 ,...filed_connec
8360: 74 69 6f 6e 5f 73 74 72 28 72 65 71 75 65 73 74 tion_str(request
8370: 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 ->headers.connec
8380: 74 69 6f 6e 29 2c 0a 09 09 66 69 6c 65 69 6e 66 tion),...fileinf
8390: 6f 2d 3e 65 74 61 67 0a 09 29 3b 0a 0a 09 69 66 o->etag..);...if
83a0: 20 28 68 74 74 70 5f 63 6f 64 65 20 3d 3d 20 32 (http_code == 2
83b0: 30 36 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 06) {...fprintf(
83c0: 66 70 2c 20 22 43 6f 6e 74 65 6e 74 2d 52 61 6e fp, "Content-Ran
83d0: 67 65 3a 20 62 79 74 65 73 20 25 6c 6c 75 2d 25 ge: bytes %llu-%
83e0: 6c 6c 75 2f 25 6c 6c 75 5c 72 5c 6e 22 2c 0a 09 llu/%llu\r\n",..
83f0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
8400: 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e long) request->
8410: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 headers.range.of
8420: 66 73 65 74 2c 0a 09 09 09 28 75 6e 73 69 67 6e fset,....(unsign
8430: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 28 72 ed long long) (r
8440: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
8450: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 2b 20 72 range.offset + r
8460: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
8470: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 2d 20 31 range.length - 1
8480: 29 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ),....(unsigned
8490: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 long long) filei
84a0: 6e 66 6f 2d 3e 6c 65 6e 0a 09 09 29 3b 0a 09 7d nfo->len...);..}
84b0: 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 5c ..fprintf(fp, "\
84c0: 72 5c 6e 22 29 3b 0a 09 66 66 6c 75 73 68 28 66 r\n");..fflush(f
84d0: 70 29 3b 0a 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f p);...log->http_
84e0: 63 6f 64 65 20 3d 20 68 74 74 70 5f 63 6f 64 65 code = http_code
84f0: 3b 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d ;..log->reason =
8500: 20 22 4f 4b 22 3b 0a 09 6c 6f 67 2d 3e 73 74 61 "OK";..log->sta
8510: 72 74 74 69 6d 65 20 3d 20 74 69 6d 65 28 4e 55 rttime = time(NU
8520: 4c 4c 29 3b 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6f LL);..log->req_o
8530: 66 66 73 65 74 20 3d 20 72 65 71 75 65 73 74 2d ffset = request-
8540: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f >headers.range.o
8550: 66 66 73 65 74 3b 0a 09 6c 6f 67 2d 3e 72 65 71 ffset;..log->req
8560: 5f 6c 65 6e 67 74 68 20 3d 20 72 65 71 75 65 73 _length = reques
8570: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
8580: 2e 6c 65 6e 67 74 68 3b 0a 09 6c 6f 67 2d 3e 66 .length;..log->f
8590: 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 66 69 6c ile_length = fil
85a0: 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 0a 23 69 66 einfo->len;..#if
85b0: 64 65 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f def FILED_NONBLO
85c0: 43 4b 5f 48 54 54 50 0a 09 69 6e 74 20 73 6f 63 CK_HTTP..int soc
85d0: 6b 65 74 5f 66 6c 61 67 73 3b 0a 09 66 64 5f 73 ket_flags;..fd_s
85e0: 65 74 20 72 66 64 2c 20 77 66 64 3b 0a 09 63 68 et rfd, wfd;..ch
85f0: 61 72 20 73 69 6e 6b 62 75 66 5b 38 31 39 32 5d ar sinkbuf[8192]
8600: 3b 0a 09 73 73 69 7a 65 5f 74 20 72 65 61 64 5f ;..ssize_t read_
8610: 72 65 74 3b 0a 0a 09 46 44 5f 5a 45 52 4f 28 26 ret;...FD_ZERO(&
8620: 72 66 64 29 3b 0a 09 46 44 5f 5a 45 52 4f 28 26 rfd);..FD_ZERO(&
8630: 77 66 64 29 3b 0a 09 46 44 5f 53 45 54 28 66 64 wfd);..FD_SET(fd
8640: 2c 20 26 72 66 64 29 3b 0a 09 46 44 5f 53 45 54 , &rfd);..FD_SET
8650: 28 66 64 2c 20 26 77 66 64 29 3b 0a 0a 09 73 6f (fd, &wfd);...so
8660: 63 6b 65 74 5f 66 6c 61 67 73 20 3d 20 66 63 6e cket_flags = fcn
8670: 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 4c 29 3b tl(fd, F_GETFL);
8680: 0a 09 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 ..fcntl(fd, F_SE
8690: 54 46 4c 2c 20 73 6f 63 6b 65 74 5f 66 6c 61 67 TFL, socket_flag
86a0: 73 20 7c 20 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 3b s | O_NONBLOCK);
86b0: 0a 23 65 6e 64 69 66 0a 09 73 65 6e 64 66 69 6c .#endif..sendfil
86c0: 65 5f 6f 66 66 73 65 74 20 3d 20 72 65 71 75 65 e_offset = reque
86d0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
86e0: 65 2e 6f 66 66 73 65 74 3b 0a 09 73 65 6e 64 66 e.offset;..sendf
86f0: 69 6c 65 5f 6c 65 6e 20 3d 20 72 65 71 75 65 73 ile_len = reques
8700: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
8710: 2e 6c 65 6e 67 74 68 3b 0a 09 73 65 6e 64 66 69 .length;..sendfi
8720: 6c 65 5f 73 65 6e 74 20 3d 20 30 3b 0a 09 77 68 le_sent = 0;..wh
8730: 69 6c 65 20 28 72 65 71 75 65 73 74 2d 3e 6d 65 ile (request->me
8740: 74 68 6f 64 20 3d 3d 20 46 49 4c 45 44 5f 52 45 thod == FILED_RE
8750: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 QUEST_METHOD_GET
8760: 29 20 7b 0a 09 09 69 66 20 28 73 65 6e 64 66 69 ) {...if (sendfi
8770: 6c 65 5f 6c 65 6e 20 3e 20 46 49 4c 45 44 5f 53 le_len > FILED_S
8780: 45 4e 44 46 49 4c 45 5f 4d 41 58 29 20 7b 0a 09 ENDFILE_MAX) {..
8790: 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 20 ..sendfile_size
87a0: 3d 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 = FILED_SENDFILE
87b0: 5f 4d 41 58 3b 0a 09 09 7d 20 65 6c 73 65 20 7b _MAX;...} else {
87c0: 0a 09 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a ....sendfile_siz
87d0: 65 20 3d 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e e = sendfile_len
87e0: 3b 0a 09 09 7d 0a 0a 09 09 73 65 6e 64 66 69 6c ;...}....sendfil
87f0: 65 5f 72 65 74 20 3d 20 73 65 6e 64 66 69 6c 65 e_ret = sendfile
8800: 28 66 64 2c 20 66 69 6c 65 69 6e 66 6f 2d 3e 66 (fd, fileinfo->f
8810: 64 2c 20 26 73 65 6e 64 66 69 6c 65 5f 6f 66 66 d, &sendfile_off
8820: 73 65 74 2c 20 73 65 6e 64 66 69 6c 65 5f 73 69 set, sendfile_si
8830: 7a 65 29 3b 0a 09 09 69 66 20 28 73 65 6e 64 66 ze);...if (sendf
8840: 69 6c 65 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a ile_ret <= 0) {.
8850: 23 69 66 64 65 66 20 46 49 4c 45 44 5f 4e 4f 4e #ifdef FILED_NON
8860: 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 09 09 69 66 BLOCK_HTTP....if
8870: 20 28 65 72 72 6e 6f 20 3d 3d 20 45 41 47 41 49 (errno == EAGAI
8880: 4e 29 20 7b 0a 09 09 09 09 73 65 6e 64 66 69 6c N) {.....sendfil
8890: 65 5f 72 65 74 20 3d 20 30 3b 0a 0a 09 09 09 09 e_ret = 0;......
88a0: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 09 09 while (1) {.....
88b0: 09 73 65 6c 65 63 74 28 66 64 20 2b 20 31 2c 20 .select(fd + 1,
88c0: 26 72 66 64 2c 20 26 77 66 64 2c 20 4e 55 4c 4c &rfd, &wfd, NULL
88d0: 2c 20 4e 55 4c 4c 29 3b 0a 09 09 09 09 09 69 66 , NULL);......if
88e0: 20 28 46 44 5f 49 53 53 45 54 28 66 64 2c 20 26 (FD_ISSET(fd, &
88f0: 72 66 64 29 29 20 7b 0a 09 09 09 09 09 09 72 65 rfd)) {.......re
8900: 61 64 5f 72 65 74 20 3d 20 72 65 61 64 28 66 64 ad_ret = read(fd
8910: 2c 20 73 69 6e 6b 62 75 66 2c 20 73 69 7a 65 6f , sinkbuf, sizeo
8920: 66 28 73 69 6e 6b 62 75 66 29 29 3b 0a 0a 09 09 f(sinkbuf));....
8930: 09 09 09 09 69 66 20 28 72 65 61 64 5f 72 65 74 ....if (read_ret
8940: 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 09 09 09 <= 0) {........
8950: 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 7d 0a 09 break;.......}..
8960: 09 09 09 09 7d 0a 0a 09 09 09 09 09 69 66 20 28 ....}.......if (
8970: 46 44 5f 49 53 53 45 54 28 66 64 2c 20 26 77 66 FD_ISSET(fd, &wf
8980: 64 29 29 20 7b 0a 09 09 09 09 09 09 72 65 61 64 d)) {.......read
8990: 5f 72 65 74 20 3d 20 31 3b 0a 0a 09 09 09 09 09 _ret = 1;.......
89a0: 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 7d 0a 09 .break;......}..
89b0: 09 09 09 7d 0a 0a 09 09 09 09 69 66 20 28 72 65 ...}......if (re
89c0: 61 64 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 ad_ret <= 0) {..
89d0: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 7d ....break;.....}
89e0: 0a 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 ....} else {....
89f0: 09 62 72 65 61 6b 3b 0a 09 09 09 7d 0a 23 65 6c .break;....}.#el
8a00: 73 65 0a 09 09 09 62 72 65 61 6b 3b 0a 23 65 6e se....break;.#en
8a10: 64 69 66 0a 09 09 7d 0a 0a 09 09 73 65 6e 64 66 dif...}....sendf
8a20: 69 6c 65 5f 6c 65 6e 20 2d 3d 20 73 65 6e 64 66 ile_len -= sendf
8a30: 69 6c 65 5f 72 65 74 3b 0a 09 09 73 65 6e 64 66 ile_ret;...sendf
8a40: 69 6c 65 5f 73 65 6e 74 20 2b 3d 20 73 65 6e 64 ile_sent += send
8a50: 66 69 6c 65 5f 72 65 74 3b 0a 09 09 69 66 20 28 file_ret;...if (
8a60: 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 3d 20 sendfile_len ==
8a70: 30 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 0) {....break;..
8a80: 09 7d 0a 09 7d 0a 0a 09 6c 6f 67 2d 3e 65 6e 64 .}..}...log->end
8a90: 74 69 6d 65 20 3d 20 28 74 69 6d 65 5f 74 29 20 time = (time_t)
8aa0: 2d 31 3b 0a 09 6c 6f 67 2d 3e 73 65 6e 74 5f 6c -1;..log->sent_l
8ab0: 65 6e 67 74 68 20 3d 20 73 65 6e 64 66 69 6c 65 ength = sendfile
8ac0: 5f 73 65 6e 74 3b 0a 0a 09 66 69 6c 65 64 5f 6c _sent;...filed_l
8ad0: 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a og_entry(log);..
8ae0: 09 63 6c 6f 73 65 28 66 69 6c 65 69 6e 66 6f 2d .close(fileinfo-
8af0: 3e 66 64 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 >fd);...if (requ
8b00: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e est->headers.con
8b10: 6e 65 63 74 69 6f 6e 20 21 3d 20 46 49 4c 45 44 nection != FILED
8b20: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 _CONNECTION_KEEP
8b30: 5f 41 4c 49 56 45 29 20 7b 0a 09 09 66 69 6c 65 _ALIVE) {...file
8b40: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
8b50: 63 6c 6f 73 65 28 66 64 2c 20 30 29 3b 0a 0a 09 close(fd, 0);...
8b60: 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 09 .fclose(fp);....
8b70: 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e return(FILED_CON
8b80: 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a NECTION_CLOSE);.
8b90: 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 .}...filed_socke
8ba0: 74 74 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 ttimeout_process
8bb0: 69 6e 67 5f 65 6e 64 28 66 64 29 3b 0a 0a 09 72 ing_end(fd);...r
8bc0: 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e eturn(FILED_CONN
8bd0: 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 ECTION_KEEP_ALIV
8be0: 45 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e 64 6c 65 E);.}../* Handle
8bf0: 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 6e 6e 65 63 incoming connec
8c00: 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 tions */.static
8c10: 76 6f 69 64 20 2a 66 69 6c 65 64 5f 77 6f 72 6b void *filed_work
8c20: 65 72 5f 74 68 72 65 61 64 28 76 6f 69 64 20 2a er_thread(void *
8c30: 61 72 67 5f 76 29 20 7b 0a 09 73 74 72 75 63 74 arg_v) {..struct
8c40: 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 filed_worker_th
8c50: 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 3b 0a read_args *arg;.
8c60: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 .struct filed_ht
8c70: 74 70 5f 72 65 71 75 65 73 74 20 72 65 71 75 65 tp_request reque
8c80: 73 74 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 st;..struct file
8c90: 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 d_log_entry *log
8ca0: 2c 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f , local_dummy_lo
8cb0: 67 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 g;..struct filed
8cc0: 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e _options *option
8cd0: 73 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 s;..struct socka
8ce0: 64 64 72 5f 69 6e 36 20 61 64 64 72 3b 0a 09 73 ddr_in6 addr;..s
8cf0: 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 72 6c 65 6e ocklen_t addrlen
8d00: 3b 0a 09 69 6e 74 20 66 61 69 6c 75 72 65 5f 63 ;..int failure_c
8d10: 6f 75 6e 74 20 3d 20 30 2c 20 6d 61 78 5f 66 61 ount = 0, max_fa
8d20: 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20 46 49 ilure_count = FI
8d30: 4c 45 44 5f 4d 41 58 5f 46 41 49 4c 55 52 45 5f LED_MAX_FAILURE_
8d40: 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 6f 6e 6e COUNT;..int conn
8d50: 65 63 74 69 6f 6e 5f 73 74 61 74 65 20 3d 20 46 ection_state = F
8d60: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f ILED_CONNECTION_
8d70: 43 4c 4f 53 45 3b 0a 09 69 6e 74 20 6d 61 73 74 CLOSE;..int mast
8d80: 65 72 5f 66 64 2c 20 66 64 20 3d 20 2d 31 3b 0a er_fd, fd = -1;.
8d90: 0a 09 2f 2a 20 52 65 61 64 20 61 72 67 75 6d 65 ../* Read argume
8da0: 6e 74 73 20 2a 2f 0a 09 61 72 67 20 3d 20 61 72 nts */..arg = ar
8db0: 67 5f 76 3b 0a 0a 09 6d 61 73 74 65 72 5f 66 64 g_v;...master_fd
8dc0: 20 3d 20 61 72 67 2d 3e 66 64 3b 0a 09 6f 70 74 = arg->fd;..opt
8dd0: 69 6f 6e 73 20 3d 20 26 61 72 67 2d 3e 6f 70 74 ions = &arg->opt
8de0: 69 6f 6e 73 3b 0a 0a 09 77 68 69 6c 65 20 28 31 ions;...while (1
8df0: 29 20 7b 0a 09 09 2f 2a 20 46 61 69 6c 75 72 65 ) {.../* Failure
8e00: 20 6c 6f 6f 70 20 70 72 65 76 65 6e 74 69 6f 6e loop prevention
8e10: 20 2a 2f 0a 09 09 69 66 20 28 66 61 69 6c 75 72 */...if (failur
8e20: 65 5f 63 6f 75 6e 74 20 3e 20 6d 61 78 5f 66 61 e_count > max_fa
8e30: 69 6c 75 72 65 5f 63 6f 75 6e 74 29 20 7b 0a 09 ilure_count) {..
8e40: 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09 09 ..break;...}....
8e50: 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 /* Allocate a ne
8e60: 77 20 6c 6f 67 20 62 75 66 66 65 72 20 2a 2f 0a w log buffer */.
8e70: 09 09 6c 6f 67 20 3d 20 66 69 6c 65 64 5f 6c 6f ..log = filed_lo
8e80: 67 5f 6e 65 77 28 31 29 3b 0a 09 09 69 66 20 28 g_new(1);...if (
8e90: 6c 6f 67 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 log == NULL) {..
8ea0: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 ..filed_log_msg(
8eb0: 22 41 4c 4c 4f 43 41 54 45 5f 4c 4f 47 5f 4d 53 "ALLOCATE_LOG_MS
8ec0: 47 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 09 09 G_FAILED");.....
8ed0: 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a 09 09 6c 6f break;...}....lo
8ee0: 67 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f g->type = FILED_
8ef0: 4c 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 LOG_TYPE_TRANSFE
8f00: 52 3b 0a 0a 09 09 2f 2a 20 49 66 20 77 65 20 63 R;..../* If we c
8f10: 6c 6f 73 65 64 20 74 68 65 20 6f 6c 64 20 63 6f losed the old co
8f20: 6e 6e 65 63 74 69 6f 6e 2c 20 61 63 63 65 70 74 nnection, accept
8f30: 20 61 20 6e 65 77 20 6f 6e 65 20 2a 2f 0a 09 09 a new one */...
8f40: 69 66 20 28 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 if (connection_s
8f50: 74 61 74 65 20 3d 3d 20 46 49 4c 45 44 5f 43 4f tate == FILED_CO
8f60: 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 20 NNECTION_CLOSE)
8f70: 7b 0a 09 09 09 2f 2a 20 41 63 63 65 70 74 20 61 {..../* Accept a
8f80: 20 6e 65 77 20 63 6c 69 65 6e 74 20 2a 2f 0a 09 new client */..
8f90: 09 09 61 64 64 72 6c 65 6e 20 3d 20 73 69 7a 65 ..addrlen = size
8fa0: 6f 66 28 61 64 64 72 29 3b 0a 0a 09 09 09 66 64 of(addr);.....fd
8fb0: 20 3d 20 61 63 63 65 70 74 28 6d 61 73 74 65 72 = accept(master
8fc0: 5f 66 64 2c 20 28 73 74 72 75 63 74 20 73 6f 63 _fd, (struct soc
8fd0: 6b 61 64 64 72 20 2a 29 20 26 61 64 64 72 2c 20 kaddr *) &addr,
8fe0: 26 61 64 64 72 6c 65 6e 29 3b 0a 09 09 7d 0a 0a &addrlen);...}..
8ff0: 09 09 2f 2a 0a 09 09 20 2a 20 49 66 20 77 65 20 ../*... * If we
9000: 66 61 69 6c 2c 20 6d 61 6b 65 20 61 20 6e 6f 74 fail, make a not
9010: 65 20 6f 66 20 69 74 20 73 6f 20 77 65 20 64 6f e of it so we do
9020: 6e 27 74 20 67 6f 20 69 6e 74 6f 20 61 20 6c 6f n't go into a lo
9030: 6f 70 20 6f 66 0a 09 09 20 2a 20 61 63 63 65 70 op of... * accep
9040: 74 28 29 20 66 61 69 6c 69 6e 67 0a 09 09 20 2a t() failing... *
9050: 2f 0a 09 09 69 66 20 28 66 64 20 3c 20 30 29 20 /...if (fd < 0)
9060: 7b 0a 09 09 09 2f 2a 20 4c 6f 67 20 74 68 65 20 {..../* Log the
9070: 6e 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a new connection *
9080: 2f 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d /....filed_log_m
9090: 73 67 28 22 41 43 43 45 50 54 5f 46 41 49 4c 45 sg("ACCEPT_FAILE
90a0: 44 22 29 3b 0a 0a 09 09 09 66 61 69 6c 75 72 65 D");.....failure
90b0: 5f 63 6f 75 6e 74 2b 2b 3b 0a 0a 09 09 09 66 69 _count++;.....fi
90c0: 6c 65 64 5f 6c 6f 67 5f 66 72 65 65 28 6c 6f 67 led_log_free(log
90d0: 29 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e 75 65 3b );.....continue;
90e0: 0a 09 09 7d 0a 0a 09 09 66 69 6c 65 64 5f 73 6f ...}....filed_so
90f0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 61 63 63 65 ckettimeout_acce
9100: 70 74 28 66 64 29 3b 0a 0a 09 09 2f 2a 20 46 69 pt(fd);..../* Fi
9110: 6c 6c 20 69 6e 20 6c 6f 67 20 73 74 72 75 63 74 ll in log struct
9120: 75 72 65 20 2a 2f 0a 09 09 69 66 20 28 66 69 6c ure */...if (fil
9130: 65 64 5f 6c 6f 67 5f 69 70 28 28 73 74 72 75 63 ed_log_ip((struc
9140: 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 t sockaddr *) &a
9150: 64 64 72 2c 20 6c 6f 67 2d 3e 69 70 2c 20 73 69 ddr, log->ip, si
9160: 7a 65 6f 66 28 6c 6f 67 2d 3e 69 70 29 29 20 3d zeof(log->ip)) =
9170: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 6c 6f 67 = NULL) {....log
9180: 2d 3e 69 70 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a ->ip[0] = '\0';.
9190: 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 20 3d 20 30 ...log->port = 0
91a0: 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 ;...} else {....
91b0: 6c 6f 67 2d 3e 70 6f 72 74 20 3d 20 61 64 64 72 log->port = addr
91c0: 2e 73 69 6e 36 5f 70 6f 72 74 3b 0a 09 09 7d 0a .sin6_port;...}.
91d0: 0a 09 09 2f 2a 20 52 65 73 65 74 20 66 61 69 6c .../* Reset fail
91e0: 75 72 65 20 63 6f 75 6e 74 2a 2f 0a 09 09 66 61 ure count*/...fa
91f0: 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20 30 3b ilure_count = 0;
9200: 0a 0a 09 09 2f 2a 20 48 61 6e 64 6c 65 20 73 6f ..../* Handle so
9210: 63 6b 65 74 20 2a 2f 0a 09 09 63 6f 6e 6e 65 63 cket */...connec
9220: 74 69 6f 6e 5f 73 74 61 74 65 20 3d 20 66 69 6c tion_state = fil
9230: 65 64 5f 68 61 6e 64 6c 65 5f 63 6c 69 65 6e 74 ed_handle_client
9240: 28 66 64 2c 20 26 72 65 71 75 65 73 74 2c 20 6c (fd, &request, l
9250: 6f 67 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a 09 7d og, options);..}
9260: 0a 0a 09 2f 2a 20 52 65 70 6f 72 74 20 65 72 72 .../* Report err
9270: 6f 72 20 2a 2f 0a 09 66 69 6c 65 64 5f 6c 6f 67 or */..filed_log
9280: 5f 6d 73 67 28 22 54 48 52 45 41 44 5f 44 49 45 _msg("THREAD_DIE
9290: 44 20 41 42 4e 4f 52 4d 41 4c 22 29 3b 0a 0a 09 D ABNORMAL");...
92a0: 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 0a 09 return(NULL);...
92b0: 2f 2a 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c /* local_dummy_l
92c0: 6f 67 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 og is only used
92d0: 69 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f if FILED_DONT_LO
92e0: 47 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 6f 74 G is enabled, ot
92f0: 68 65 72 77 69 73 65 20 69 74 27 73 20 6e 6f 74 herwise it's not
9300: 20 75 73 65 64 2c 20 62 75 74 20 74 68 65 20 63 used, but the c
9310: 6f 6d 70 69 6c 65 72 20 68 61 74 65 73 20 74 68 ompiler hates th
9320: 61 74 20 69 64 65 61 2e 20 2a 2f 0a 09 6c 6f 63 at idea. */..loc
9330: 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 al_dummy_log.typ
9340: 65 20 3d 20 30 3b 0a 09 6c 6f 63 61 6c 5f 64 75 e = 0;..local_du
9350: 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 20 3d 20 6c mmy_log.type = l
9360: 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 ocal_dummy_log.t
9370: 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 43 72 65 61 74 ype;.}../* Creat
9380: 65 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 e worker threads
9390: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 */.static int f
93a0: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 iled_worker_thre
93b0: 61 64 73 5f 69 6e 69 74 28 69 6e 74 20 66 64 2c ads_init(int fd,
93c0: 20 69 6e 74 20 74 68 72 65 61 64 5f 63 6f 75 6e int thread_coun
93d0: 74 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f t, struct filed_
93e0: 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 options *options
93f0: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 ) {..struct file
9400: 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f d_worker_thread_
9410: 61 72 67 73 20 2a 61 72 67 3b 0a 09 70 74 68 72 args *arg;..pthr
9420: 65 61 64 5f 74 20 74 68 72 65 61 64 69 64 3b 0a ead_t threadid;.
9430: 09 69 6e 74 20 70 74 68 72 65 61 64 5f 72 65 74 .int pthread_ret
9440: 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 66 6f 72 20 ;..int i;...for
9450: 28 69 20 3d 20 30 3b 20 69 20 3c 20 74 68 72 65 (i = 0; i < thre
9460: 61 64 5f 63 6f 75 6e 74 3b 20 69 2b 2b 29 20 7b ad_count; i++) {
9470: 0a 09 09 61 72 67 20 3d 20 6d 61 6c 6c 6f 63 28 ...arg = malloc(
9480: 73 69 7a 65 6f 66 28 2a 61 72 67 29 29 3b 0a 0a sizeof(*arg));..
9490: 09 09 61 72 67 2d 3e 66 64 20 3d 20 66 64 3b 0a ..arg->fd = fd;.
94a0: 09 09 6d 65 6d 63 70 79 28 26 61 72 67 2d 3e 6f ..memcpy(&arg->o
94b0: 70 74 69 6f 6e 73 2c 20 6f 70 74 69 6f 6e 73 2c ptions, options,
94c0: 20 73 69 7a 65 6f 66 28 2a 6f 70 74 69 6f 6e 73 sizeof(*options
94d0: 29 29 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f 72 ));....pthread_r
94e0: 65 74 20 3d 20 70 74 68 72 65 61 64 5f 63 72 65 et = pthread_cre
94f0: 61 74 65 28 26 74 68 72 65 61 64 69 64 2c 20 4e ate(&threadid, N
9500: 55 4c 4c 2c 20 66 69 6c 65 64 5f 77 6f 72 6b 65 ULL, filed_worke
9510: 72 5f 74 68 72 65 61 64 2c 20 61 72 67 29 3b 0a r_thread, arg);.
9520: 09 09 69 66 20 28 70 74 68 72 65 61 64 5f 72 65 ..if (pthread_re
9530: 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 74 t != 0) {....ret
9540: 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 09 7d 0a urn(-1);...}..}.
9550: 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a ..return(0);.}..
9560: 2f 2a 20 44 69 73 70 6c 61 79 20 68 65 6c 70 20 /* Display help
9570: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 */.static void f
9580: 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 iled_print_help(
9590: 46 49 4c 45 20 2a 6f 75 74 70 75 74 2c 20 69 6e FILE *output, in
95a0: 74 20 6c 6f 6e 67 5f 68 65 6c 70 2c 20 63 6f 6e t long_help, con
95b0: 73 74 20 63 68 61 72 20 2a 65 78 74 72 61 29 20 st char *extra)
95c0: 7b 0a 09 69 66 20 28 65 78 74 72 61 29 20 7b 0a {..if (extra) {.
95d0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
95e0: 2c 20 22 25 73 5c 6e 22 2c 20 65 78 74 72 61 29 , "%s\n", extra)
95f0: 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74 66 28 6f ;..}...fprintf(o
9600: 75 74 70 75 74 2c 20 22 55 73 61 67 65 3a 20 66 utput, "Usage: f
9610: 69 6c 65 64 20 5b 3c 6f 70 74 69 6f 6e 73 3e 5d iled [<options>]
9620: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f \n");..fprintf(o
9630: 75 74 70 75 74 2c 20 22 20 20 4f 70 74 69 6f 6e utput, " Option
9640: 73 3a 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 s:\n");..fprintf
9650: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
9660: 2d 68 2c 20 2d 2d 68 65 6c 70 5c 6e 22 29 3b 0a -h, --help\n");.
9670: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
9680: 20 22 20 20 20 20 20 20 2d 64 2c 20 2d 2d 64 61 " -d, --da
9690: 65 6d 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e emon\n");..fprin
96a0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
96b0: 20 20 2d 76 2c 20 2d 2d 76 65 72 73 69 6f 6e 5c -v, --version\
96c0: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
96d0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 56 2c tput, " -V,
96e0: 20 2d 2d 76 68 6f 73 74 5c 6e 22 29 3b 0a 09 66 --vhost\n");..f
96f0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
9700: 20 20 20 20 20 20 2d 62 20 3c 61 64 64 72 65 73 -b <addres
9710: 73 3e 2c 20 2d 2d 62 69 6e 64 20 3c 61 64 64 72 s>, --bind <addr
9720: 65 73 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e ess>\n");..fprin
9730: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
9740: 20 20 2d 70 20 3c 70 6f 72 74 3e 2c 20 2d 2d 70 -p <port>, --p
9750: 6f 72 74 20 3c 70 6f 72 74 3e 5c 6e 22 29 3b 0a ort <port>\n");.
9760: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
9770: 20 22 20 20 20 20 20 20 2d 74 20 3c 63 6f 75 6e " -t <coun
9780: 74 3e 2c 20 2d 2d 74 68 72 65 61 64 73 20 3c 63 t>, --threads <c
9790: 6f 75 6e 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 ount>\n");..fpri
97a0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
97b0: 20 20 20 2d 63 20 3c 65 6e 74 72 69 65 73 3e 2c -c <entries>,
97c0: 20 2d 2d 63 61 63 68 65 20 3c 65 6e 74 72 69 65 --cache <entrie
97d0: 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 s>\n");..fprintf
97e0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
97f0: 2d 6c 20 3c 66 69 6c 65 3e 2c 20 2d 2d 6c 6f 67 -l <file>, --log
9800: 20 3c 66 69 6c 65 3e 5c 6e 22 29 3b 0a 09 66 70 <file>\n");..fp
9810: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
9820: 20 20 20 20 20 2d 75 20 3c 75 73 65 72 3e 2c 20 -u <user>,
9830: 2d 2d 75 73 65 72 20 3c 75 73 65 72 3e 5c 6e 22 --user <user>\n"
9840: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 );..fprintf(outp
9850: 75 74 2c 20 22 20 20 20 20 20 20 2d 72 20 3c 64 ut, " -r <d
9860: 69 72 65 63 74 6f 72 79 3e 2c 20 2d 2d 72 6f 6f irectory>, --roo
9870: 74 20 3c 64 69 72 65 63 74 6f 72 79 3e 5c 6e 22 t <directory>\n"
9880: 29 3b 0a 0a 09 69 66 20 28 6c 6f 6e 67 5f 68 65 );...if (long_he
9890: 6c 70 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 lp) {...fprintf(
98a0: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 output, "\n");..
98b0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
98c0: 20 22 20 20 55 73 61 67 65 3a 5c 6e 22 29 3b 0a " Usage:\n");.
98d0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
98e0: 2c 20 22 20 20 20 20 20 20 2d 68 20 28 6f 72 20 , " -h (or
98f0: 2d 2d 68 65 6c 70 29 20 70 72 69 6e 74 73 20 74 --help) prints t
9900: 68 69 73 20 75 73 61 67 65 20 69 6e 66 6f 72 6d his usage inform
9910: 61 74 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 ation.\n");...fp
9920: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
9930: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
9940: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 64 utput, " -d
9950: 20 28 6f 72 20 2d 2d 64 61 65 6d 6f 6e 29 20 69 (or --daemon) i
9960: 6e 73 74 72 75 63 74 73 20 66 69 6c 65 64 20 74 nstructs filed t
9970: 6f 20 62 65 63 6f 6d 65 20 61 20 64 61 65 6d 6f o become a daemo
9980: 6e 20 61 66 74 65 72 20 69 6e 69 74 69 61 6c 69 n after initiali
9990: 7a 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 zing\n");...fpri
99a0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
99b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
99c0: 20 20 20 20 74 68 65 20 6c 69 73 74 65 6e 69 6e the listenin
99d0: 67 20 54 43 50 20 73 6f 63 6b 65 74 20 61 6e 64 g TCP socket and
99e0: 20 6c 6f 67 20 66 69 6c 65 73 2e 5c 6e 22 29 3b log files.\n");
99f0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9a00: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t, "\n");...fpri
9a10: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9a20: 20 20 20 2d 76 20 28 6f 72 20 2d 2d 76 65 72 73 -v (or --vers
9a30: 69 6f 6e 29 20 69 6e 73 74 72 75 63 74 73 20 66 ion) instructs f
9a40: 69 6c 65 64 20 70 72 69 6e 74 20 6f 75 74 20 74 iled print out t
9a50: 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 he version numbe
9a60: 72 20 61 6e 64 20 65 78 69 74 2e 5c 6e 22 29 3b r and exit.\n");
9a70: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9a80: 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t, "\n");...fpri
9a90: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9aa0: 20 20 20 2d 56 20 28 6f 72 20 2d 2d 76 68 6f 73 -V (or --vhos
9ab0: 74 29 20 69 6e 73 74 72 75 63 74 73 20 66 69 6c t) instructs fil
9ac0: 65 64 20 74 6f 20 70 72 65 70 65 6e 64 20 61 6c ed to prepend al
9ad0: 6c 20 72 65 71 75 65 73 74 73 20 77 69 74 68 20 l requests with
9ae0: 74 68 65 69 72 20 48 54 54 50 5c 6e 22 29 3b 0a their HTTP\n");.
9af0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
9b00: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
9b10: 20 20 20 20 20 20 20 20 20 48 6f 73 74 20 68 65 Host he
9b20: 61 64 65 72 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 ader.\n");...fpr
9b30: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e intf(output, "\n
9b40: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
9b50: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 62 20 tput, " -b
9b60: 28 6f 72 20 2d 2d 62 69 6e 64 29 20 73 70 65 63 (or --bind) spec
9b70: 69 66 69 65 73 20 74 68 65 20 61 64 64 72 65 73 ifies the addres
9b80: 73 20 74 6f 20 6c 69 73 74 65 6e 20 66 6f 72 20 s to listen for
9b90: 69 6e 63 6f 6d 69 6e 67 20 48 54 54 50 5c 6e 22 incoming HTTP\n"
9ba0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
9bb0: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
9bc0: 20 20 20 20 20 20 20 20 20 20 20 72 65 71 75 65 reque
9bd0: 73 74 73 20 6f 6e 2e 20 20 54 68 65 20 64 65 66 sts on. The def
9be0: 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20 5c 22 ault value is \"
9bf0: 25 73 5c 22 2e 5c 6e 22 2c 20 42 49 4e 44 5f 41 %s\".\n", BIND_A
9c00: 44 44 52 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 DDR);...fprintf(
9c10: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 output, "\n");..
9c20: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
9c30: 20 22 20 20 20 20 20 20 2d 70 20 28 6f 72 20 2d " -p (or -
9c40: 2d 70 6f 72 74 29 20 73 70 65 63 69 66 69 65 73 -port) specifies
9c50: 20 74 68 65 20 54 43 50 20 70 6f 72 74 20 6e 75 the TCP port nu
9c60: 6d 62 65 72 20 74 6f 20 6c 69 73 74 65 6e 20 66 mber to listen f
9c70: 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 48 54 54 50 or incoming HTTP
9c80: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
9c90: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
9ca0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
9cb0: 71 75 65 73 74 73 20 6f 6e 2e 20 20 54 68 65 20 quests on. The
9cc0: 64 65 66 61 75 6c 74 20 69 73 20 25 75 2e 5c 6e default is %u.\n
9cd0: 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 ", (unsigned int
9ce0: 29 20 50 4f 52 54 29 3b 0a 09 09 66 70 72 69 6e ) PORT);...fprin
9cf0: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 tf(output, "\n")
9d00: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
9d10: 75 74 2c 20 22 20 20 20 20 20 20 2d 74 20 28 6f ut, " -t (o
9d20: 72 20 2d 2d 74 68 72 65 61 64 73 29 20 73 70 65 r --threads) spe
9d30: 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 cifies the numbe
9d40: 72 20 6f 66 20 77 6f 72 6b 65 72 20 74 68 72 65 r of worker thre
9d50: 61 64 73 20 74 6f 20 63 72 65 61 74 65 2e 20 45 ads to create. E
9d60: 61 63 68 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ach\n");...fprin
9d70: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
9d80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9d90: 20 20 20 20 77 6f 72 6b 65 72 20 74 68 72 65 61 worker threa
9da0: 64 20 63 61 6e 20 73 65 72 76 69 63 65 20 6f 6e d can service on
9db0: 65 20 63 6f 6e 63 75 72 72 65 6e 74 20 48 54 54 e concurrent HTT
9dc0: 50 20 73 65 73 73 69 6f 6e 2e 5c 6e 22 29 3b 0a P session.\n");.
9dd0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
9de0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
9df0: 20 20 20 20 20 20 20 20 20 20 20 54 68 75 73 20 Thus
9e00: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 the number of th
9e10: 72 65 61 64 73 20 63 72 65 61 74 65 64 20 77 69 reads created wi
9e20: 6c 6c 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 ll determine how
9e30: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
9e40: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
9e50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9e60: 20 6d 61 6e 79 20 73 69 6d 75 6c 74 61 6e 65 6f many simultaneo
9e70: 75 73 20 74 72 61 6e 73 66 65 72 73 20 77 69 6c us transfers wil
9e80: 6c 20 62 65 20 70 6f 73 73 69 62 6c 65 2e 20 54 l be possible. T
9e90: 68 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 he\n");...fprint
9ea0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
9eb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ec0: 20 20 20 64 65 66 61 75 6c 74 20 69 73 20 25 6c default is %l
9ed0: 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 u.\n", (unsigned
9ee0: 20 6c 6f 6e 67 29 20 54 48 52 45 41 44 5f 43 4f long) THREAD_CO
9ef0: 55 4e 54 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 UNT);...fprintf(
9f00: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 output, "\n");..
9f10: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
9f20: 20 22 20 20 20 20 20 20 2d 63 20 28 6f 72 20 2d " -c (or -
9f30: 2d 63 61 63 68 65 29 20 73 70 65 63 69 66 69 65 -cache) specifie
9f40: 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
9f50: 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e file information
9f60: 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 5c 6e cache entries\n
9f70: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
9f80: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
9f90: 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 20 to
9fa0: 61 6c 6c 6f 63 61 74 65 2e 20 20 45 61 63 68 20 allocate. Each
9fb0: 63 61 63 68 65 20 65 6e 74 72 79 20 68 6f 6c 64 cache entry hold
9fc0: 73 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 s file informati
9fd0: 6f 6e 20 61 73 5c 6e 22 29 3b 0a 09 09 66 70 72 on as\n");...fpr
9fe0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
9ff0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a000: 20 20 20 20 77 65 6c 6c 20 61 73 20 61 6e 20 6f well as an o
a010: 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 pen file descrip
a020: 74 6f 72 20 74 6f 20 74 68 65 20 66 69 6c 65 2c tor to the file,
a030: 20 73 6f 20 72 65 73 6f 75 72 63 65 5c 6e 22 29 so resource\n")
a040: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
a050: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
a060: 20 20 20 20 20 20 20 20 20 20 20 6c 69 6d 69 74 limit
a070: 73 20 28 69 2e 65 2e 2c 20 75 6c 69 6d 69 74 29 s (i.e., ulimit)
a080: 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 should be consi
a090: 64 65 72 65 64 2e 20 20 54 68 69 73 20 73 68 6f dered. This sho
a0a0: 75 6c 64 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e uld\n");...fprin
a0b0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
a0c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0d0: 20 20 62 65 20 61 20 70 72 69 6d 65 20 6e 75 6d be a prime num
a0e0: 62 65 72 20 66 6f 72 20 69 64 65 61 6c 20 75 73 ber for ideal us
a0f0: 65 20 77 69 74 68 20 74 68 65 20 6c 6f 6f 6b 75 e with the looku
a100: 70 20 6d 65 74 68 6f 64 2e 5c 6e 22 29 3b 0a 09 p method.\n");..
a110: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
a120: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a130: 20 20 20 20 20 20 20 20 54 68 65 20 64 65 66 61 The defa
a140: 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 ult is %lu.\n",
a150: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 (unsigned long)
a160: 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a 09 09 66 CACHE_SIZE);...f
a170: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
a180: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
a190: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
a1a0: 6c 20 28 6f 72 20 2d 2d 6c 6f 67 29 20 73 70 65 l (or --log) spe
a1b0: 63 69 66 69 65 73 20 61 20 66 69 6c 65 6e 61 6d cifies a filenam
a1c0: 65 20 74 6f 20 6f 70 65 6e 20 66 6f 72 20 77 72 e to open for wr
a1d0: 69 74 69 6e 67 20 6c 6f 67 20 65 6e 74 72 69 65 iting log entrie
a1e0: 73 2e 20 20 4c 6f 67 5c 6e 22 29 3b 0a 09 09 66 s. Log\n");...f
a1f0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
a200: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a210: 20 20 20 20 65 6e 74 72 69 65 73 20 61 72 65 20 entries are
a220: 6d 61 64 65 20 66 6f 72 20 76 61 72 69 6f 75 73 made for various
a230: 20 73 74 61 67 65 73 20 69 6e 20 74 72 61 6e 73 stages in trans
a240: 66 65 72 69 6e 67 20 66 69 6c 65 73 2e 5c 6e 22 fering files.\n"
a250: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
a260: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
a270: 20 20 20 20 20 20 20 20 20 20 54 68 65 20 6c 6f The lo
a280: 67 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 g file is opened
a290: 20 62 65 66 6f 72 65 20 73 77 69 74 63 68 69 6e before switchin
a2a0: 67 20 75 73 65 72 73 20 28 73 65 65 20 5c 22 2d g users (see \"-
a2b0: 75 5c 22 29 5c 6e 22 29 3b 0a 09 09 66 70 72 69 u\")\n");...fpri
a2c0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
a2d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2e0: 20 61 6e 64 20 72 6f 6f 74 20 64 69 72 65 63 74 and root direct
a2f0: 6f 72 69 65 73 20 28 73 65 65 20 5c 22 2d 72 5c ories (see \"-r\
a300: 22 29 2e 20 20 54 68 65 20 6c 6f 67 20 66 69 6c "). The log fil
a310: 65 20 69 73 20 6e 65 76 65 72 5c 6e 22 29 3b 0a e is never\n");.
a320: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
a330: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
a340: 20 20 20 20 20 20 20 63 6c 6f 73 65 64 20 73 6f closed so
a350: 20 6c 6f 67 20 72 6f 74 61 74 69 6f 6e 20 77 69 log rotation wi
a360: 74 68 6f 75 74 20 73 74 6f 70 70 69 6e 67 20 74 thout stopping t
a370: 68 65 20 64 61 65 6d 6f 6e 20 69 73 20 77 69 6c he daemon is wil
a380: 6c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 l\n");...fprintf
a390: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
a3a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f no
a3b0: 74 20 77 6f 72 6b 2e 20 20 54 68 65 20 76 61 6c t work. The val
a3c0: 75 65 20 6f 66 20 5c 22 2d 5c 22 20 69 6e 64 69 ue of \"-\" indi
a3d0: 63 61 74 65 73 20 74 68 61 74 20 73 74 61 6e 64 cates that stand
a3e0: 61 72 64 20 6f 75 74 70 75 74 5c 6e 22 29 3b 0a ard output\n");.
a3f0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
a400: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
a410: 20 20 20 20 20 20 20 73 68 6f 75 6c 64 20 62 65 should be
a420: 20 75 73 65 64 20 66 6f 72 20 6c 6f 67 67 69 6e used for loggin
a430: 67 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 6e g. If the filen
a440: 61 6d 65 20 62 65 67 69 6e 73 20 77 69 74 68 20 ame begins with
a450: 61 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 a\n");...fprintf
a460: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
a470: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 69 pi
a480: 70 65 20 28 5c 22 7c 5c 22 29 20 74 68 65 6e 20 pe (\"|\") then
a490: 61 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 61 a process is sta
a4a0: 72 74 65 64 20 61 6e 64 20 75 73 65 64 20 66 6f rted and used fo
a4b0: 72 20 6c 6f 67 67 69 6e 67 5c 6e 22 29 3b 0a 09 r logging\n");..
a4c0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
a4d0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a4e0: 20 20 20 20 20 20 69 6e 73 74 65 61 64 20 6f 66 instead of
a4f0: 20 61 20 66 69 6c 65 2e 20 20 54 68 65 20 64 65 a file. The de
a500: 66 61 75 6c 74 20 69 73 20 5c 22 25 73 5c 22 2e fault is \"%s\".
a510: 5c 6e 22 2c 20 4c 4f 47 5f 46 49 4c 45 29 3b 0a \n", LOG_FILE);.
a520: 23 69 66 64 65 66 20 46 49 4c 45 44 5f 44 4f 4e #ifdef FILED_DON
a530: 54 5f 4c 4f 47 0a 09 09 66 70 72 69 6e 74 66 28 T_LOG...fprintf(
a540: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
a550: 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 6f 74 Not
a560: 65 20 74 68 61 74 20 6c 6f 67 67 69 6e 67 20 69 e that logging i
a570: 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 s completely dis
a580: 61 62 6c 65 64 20 73 6f 20 74 68 69 73 20 6f 70 abled so this op
a590: 74 69 6f 6e 20 64 6f 65 73 5c 6e 22 29 3b 0a 09 tion does\n");..
a5a0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
a5b0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a5c0: 20 20 20 20 20 20 6e 6f 74 68 69 6e 67 20 69 6e nothing in
a5d0: 20 74 68 69 73 20 62 75 69 6c 64 2e 5c 6e 22 29 this build.\n")
a5e0: 3b 0a 23 65 6e 64 69 66 0a 09 09 66 70 72 69 6e ;.#endif...fprin
a5f0: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 tf(output, "\n")
a600: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
a610: 75 74 2c 20 22 20 20 20 20 20 20 2d 75 20 28 6f ut, " -u (o
a620: 72 20 2d 2d 75 73 65 72 29 20 73 70 65 63 69 66 r --user) specif
a630: 69 65 73 20 74 68 65 20 75 73 65 72 20 74 6f 20 ies the user to
a640: 73 77 69 74 63 68 20 75 73 65 72 20 49 44 73 20 switch user IDs
a650: 74 6f 20 62 65 66 6f 72 65 20 73 65 72 76 69 63 to before servic
a660: 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ing\n");...fprin
a670: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
a680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a690: 20 72 65 71 75 65 73 74 73 2e 20 20 54 68 65 20 requests. The
a6a0: 64 65 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 63 default is not c
a6b0: 68 61 6e 67 65 20 75 73 65 72 20 49 44 73 2e 5c hange user IDs.\
a6c0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
a6d0: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
a6e0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
a6f0: 22 20 20 20 20 20 20 2d 72 20 28 6f 72 20 2d 2d " -r (or --
a700: 72 6f 6f 74 29 20 73 70 65 63 69 66 69 65 73 20 root) specifies
a710: 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f the directory to
a720: 20 61 63 74 20 61 73 20 74 68 65 20 72 6f 6f 74 act as the root
a730: 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 5c 6e directory for\n
a740: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
a750: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
a760: 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 the
a770: 66 69 6c 65 20 73 65 72 76 65 72 2e 20 20 49 66 file server. If
a780: 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 69 73 20 this option is
a790: 73 70 65 63 69 66 69 65 64 2c 20 63 68 72 6f 6f specified, chroo
a7a0: 74 28 32 29 5c 6e 22 29 3b 0a 09 09 66 70 72 69 t(2)\n");...fpri
a7b0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
a7c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7d0: 20 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 is called. Th
a7e0: 65 20 64 65 66 61 75 6c 74 20 69 73 20 6e 6f 74 e default is not
a7f0: 20 63 68 61 6e 67 65 20 72 6f 6f 74 20 64 69 72 change root dir
a800: 65 63 74 6f 72 69 65 73 2c 5c 6e 22 29 3b 0a 09 ectories,\n");..
a810: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
a820: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a830: 20 20 20 20 20 20 20 74 68 61 74 20 69 73 2c 20 that is,
a840: 74 68 65 20 5c 22 2f 5c 22 20 64 69 72 65 63 74 the \"/\" direct
a850: 6f 72 79 20 69 73 20 73 68 61 72 65 64 20 6f 75 ory is shared ou
a860: 74 2e 20 20 54 68 69 73 20 77 69 6c 6c 5c 6e 22 t. This will\n"
a870: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
a880: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
a890: 20 20 20 20 20 20 20 20 20 20 20 6c 69 6b 65 6c likel
a8a0: 79 20 62 65 20 61 20 73 65 63 75 72 69 74 79 20 y be a security
a8b0: 69 73 73 75 65 2c 20 73 6f 20 74 68 69 73 20 6f issue, so this o
a8c0: 70 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 ption should alw
a8d0: 61 79 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ays\n");...fprin
a8e0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
a8f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a900: 20 62 65 20 75 73 65 64 2e 5c 6e 22 29 3b 0a 09 be used.\n");..
a910: 7d 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f }...return;.}../
a920: 2a 20 41 64 64 20 61 20 67 65 74 6f 70 74 20 6f * Add a getopt o
a930: 70 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 ption */.static
a940: 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 74 6f 70 void filed_getop
a950: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 73 74 t_long_setopt(st
a960: 72 75 63 74 20 6f 70 74 69 6f 6e 20 2a 6f 70 74 ruct option *opt
a970: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6e 61 , const char *na
a980: 6d 65 2c 20 69 6e 74 20 68 61 73 5f 61 72 67 2c me, int has_arg,
a990: 20 69 6e 74 20 76 61 6c 29 20 7b 0a 09 6f 70 74 int val) {..opt
a9a0: 2d 3e 6e 61 6d 65 20 20 20 20 20 3d 20 6e 61 6d ->name = nam
a9b0: 65 3b 0a 09 6f 70 74 2d 3e 68 61 73 5f 61 72 67 e;..opt->has_arg
a9c0: 20 20 3d 20 68 61 73 5f 61 72 67 3b 0a 09 6f 70 = has_arg;..op
a9d0: 74 2d 3e 66 6c 61 67 20 20 20 20 20 3d 20 4e 55 t->flag = NU
a9e0: 4c 4c 3b 0a 09 6f 70 74 2d 3e 76 61 6c 20 20 20 LL;..opt->val
a9f0: 20 20 20 3d 20 76 61 6c 3b 0a 0a 09 72 65 74 75 = val;...retu
aa00: 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 73 6f 6c 76 rn;.}../* Resolv
aa10: 65 20 61 20 75 73 65 72 6e 61 6d 65 20 74 6f 20 e a username to
aa20: 61 20 55 49 44 20 2a 2f 0a 73 74 61 74 69 63 20 a UID */.static
aa30: 69 6e 74 20 66 69 6c 65 64 5f 75 73 65 72 5f 6c int filed_user_l
aa40: 6f 6f 6b 75 70 28 63 6f 6e 73 74 20 63 68 61 72 ookup(const char
aa50: 20 2a 75 73 65 72 2c 20 75 69 64 5f 74 20 2a 75 *user, uid_t *u
aa60: 73 65 72 5f 69 64 29 20 7b 0a 09 63 68 61 72 20 ser_id) {..char
aa70: 2a 6e 65 78 74 3b 0a 09 75 69 64 5f 74 20 75 73 *next;..uid_t us
aa80: 65 72 5f 69 64 5f 63 68 65 63 6b 3b 0a 23 69 66 er_id_check;.#if
aa90: 6e 64 65 66 20 46 49 4c 45 44 5f 4e 4f 5f 47 45 ndef FILED_NO_GE
aaa0: 54 50 57 4e 41 4d 0a 09 73 74 72 75 63 74 20 70 TPWNAM..struct p
aab0: 61 73 73 77 64 20 2a 65 6e 74 3b 0a 0a 09 65 6e asswd *ent;...en
aac0: 74 20 3d 20 67 65 74 70 77 6e 61 6d 28 75 73 65 t = getpwnam(use
aad0: 72 29 3b 0a 09 69 66 20 28 65 6e 74 20 21 3d 20 r);..if (ent !=
aae0: 4e 55 4c 4c 29 20 7b 0a 09 09 2a 75 73 65 72 5f NULL) {...*user_
aaf0: 69 64 20 3d 20 65 6e 74 2d 3e 70 77 5f 75 69 64 id = ent->pw_uid
ab00: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a ;....return(0);.
ab10: 09 7d 0a 23 65 6e 64 69 66 0a 0a 09 75 73 65 72 .}.#endif...user
ab20: 5f 69 64 5f 63 68 65 63 6b 20 3d 20 73 74 72 74 _id_check = strt
ab30: 6f 75 6c 6c 28 75 73 65 72 2c 20 26 6e 65 78 74 oull(user, &next
ab40: 2c 20 31 30 29 3b 0a 09 69 66 20 28 6e 65 78 74 , 10);..if (next
ab50: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 == NULL) {...re
ab60: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 turn(1);..}...if
ab70: 20 28 6e 65 78 74 5b 30 5d 20 21 3d 20 27 5c 30 (next[0] != '\0
ab80: 27 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 ') {...return(1)
ab90: 3b 0a 09 7d 0a 0a 09 2a 75 73 65 72 5f 69 64 20 ;..}...*user_id
aba0: 3d 20 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 3b = user_id_check;
abb0: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a ...return(0);.}.
abc0: 0a 2f 2a 20 44 61 65 6d 6f 6e 69 7a 65 20 2a 2f ./* Daemonize */
abd0: 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 .static int file
abe0: 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 76 6f 69 64 d_daemonize(void
abf0: 29 20 7b 0a 09 70 69 64 5f 74 20 73 65 74 73 69 ) {..pid_t setsi
ac00: 64 5f 72 65 74 2c 20 66 6f 72 6b 5f 72 65 74 3b d_ret, fork_ret;
ac10: 0a 09 69 6e 74 20 63 68 64 69 72 5f 72 65 74 2c ..int chdir_ret,
ac20: 20 64 75 70 32 5f 72 65 74 3b 0a 09 69 6e 74 20 dup2_ret;..int
ac30: 66 64 5f 69 6e 2c 20 66 64 5f 6f 75 74 3b 0a 0a fd_in, fd_out;..
ac40: 09 63 68 64 69 72 5f 72 65 74 20 3d 20 63 68 64 .chdir_ret = chd
ac50: 69 72 28 22 2f 22 29 3b 0a 09 69 66 20 28 63 68 ir("/");..if (ch
ac60: 64 69 72 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a dir_ret != 0) {.
ac70: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
ac80: 0a 09 66 6f 72 6b 5f 72 65 74 20 3d 20 66 6f 72 ..fork_ret = for
ac90: 6b 28 29 3b 0a 09 69 66 20 28 66 6f 72 6b 5f 72 k();..if (fork_r
aca0: 65 74 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 et < 0) {...retu
acb0: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 rn(1);..}...if (
acc0: 66 6f 72 6b 5f 72 65 74 20 3e 20 30 29 20 7b 0a fork_ret > 0) {.
acd0: 09 09 2f 2a 20 50 61 72 65 6e 74 20 2a 2f 0a 09 ../* Parent */..
ace0: 09 77 61 69 74 70 69 64 28 66 6f 72 6b 5f 72 65 .waitpid(fork_re
acf0: 74 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 0a 09 09 t, NULL, 0);....
ad00: 65 78 69 74 28 45 58 49 54 5f 53 55 43 43 45 53 exit(EXIT_SUCCES
ad10: 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 68 69 6c S);..}.../* Chil
ad20: 64 20 2a 2f 0a 09 69 66 20 28 66 6f 72 6b 28 29 d */..if (fork()
ad30: 20 21 3d 20 30 29 20 7b 0a 09 09 2f 2a 20 43 68 != 0) {.../* Ch
ad40: 69 6c 64 20 2a 2f 0a 09 09 65 78 69 74 28 45 58 ild */...exit(EX
ad50: 49 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a IT_SUCCESS);..}.
ad60: 0a 09 2f 2a 20 47 72 61 6e 64 20 63 68 69 6c 64 ../* Grand child
ad70: 20 2a 2f 0a 09 73 65 74 73 69 64 5f 72 65 74 20 */..setsid_ret
ad80: 3d 20 73 65 74 73 69 64 28 29 3b 0a 09 69 66 20 = setsid();..if
ad90: 28 73 65 74 73 69 64 5f 72 65 74 20 3d 3d 20 28 (setsid_ret == (
ada0: 28 70 69 64 5f 74 29 20 2d 31 29 29 20 7b 0a 09 (pid_t) -1)) {..
adb0: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a .return(1);..}..
adc0: 09 66 64 5f 69 6e 20 3d 20 6f 70 65 6e 28 22 2f .fd_in = open("/
add0: 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 52 44 4f dev/null", O_RDO
ade0: 4e 4c 59 29 3b 0a 09 66 64 5f 6f 75 74 20 3d 20 NLY);..fd_out =
adf0: 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 open("/dev/null"
ae00: 2c 20 4f 5f 57 52 4f 4e 4c 59 29 3b 0a 09 69 66 , O_WRONLY);..if
ae10: 20 28 66 64 5f 69 6e 20 3c 20 30 20 7c 7c 20 66 (fd_in < 0 || f
ae20: 64 5f 6f 75 74 20 3c 20 30 29 20 7b 0a 09 09 72 d_out < 0) {...r
ae30: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 eturn(1);..}...d
ae40: 75 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 up2_ret = dup2(f
ae50: 64 5f 69 6e 2c 20 53 54 44 49 4e 5f 46 49 4c 45 d_in, STDIN_FILE
ae60: 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 NO);..if (dup2_r
ae70: 65 74 20 21 3d 20 53 54 44 49 4e 5f 46 49 4c 45 et != STDIN_FILE
ae80: 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 NO) {...return(1
ae90: 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 );..}...dup2_ret
aea0: 20 3d 20 64 75 70 32 28 66 64 5f 6f 75 74 2c 20 = dup2(fd_out,
aeb0: 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 3b 0a STDOUT_FILENO);.
aec0: 09 69 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d .if (dup2_ret !=
aed0: 20 53 54 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 20 STDOUT_FILENO)
aee0: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 {...return(1);..
aef0: 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 64 }...dup2_ret = d
af00: 75 70 32 28 66 64 5f 6f 75 74 2c 20 53 54 44 45 up2(fd_out, STDE
af10: 52 52 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 RR_FILENO);..if
af20: 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 (dup2_ret != STD
af30: 45 52 52 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 ERR_FILENO) {...
af40: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 return(1);..}...
af50: 63 6c 6f 73 65 28 66 64 5f 69 6e 29 3b 0a 09 63 close(fd_in);..c
af60: 6c 6f 73 65 28 66 64 5f 6f 75 74 29 3b 0a 0a 09 lose(fd_out);...
af70: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a return(0);.}../*
af80: 20 52 75 6e 20 70 72 6f 63 65 73 73 20 2a 2f 0a Run process */.
af90: 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20 61 72 67 int main(int arg
afa0: 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 29 20 c, char **argv)
afb0: 7b 0a 09 73 74 72 75 63 74 20 6f 70 74 69 6f 6e {..struct option
afc0: 20 6f 70 74 69 6f 6e 73 5b 31 32 5d 3b 0a 09 73 options[12];..s
afd0: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 truct filed_opti
afe0: 6f 6e 73 20 74 68 72 65 61 64 5f 6f 70 74 69 6f ons thread_optio
aff0: 6e 73 20 3d 20 7b 30 7d 3b 0a 09 63 6f 6e 73 74 ns = {0};..const
b000: 20 63 68 61 72 20 2a 62 69 6e 64 5f 61 64 64 72 char *bind_addr
b010: 20 3d 20 42 49 4e 44 5f 41 44 44 52 2c 20 2a 6e = BIND_ADDR, *n
b020: 65 77 72 6f 6f 74 20 3d 20 4e 55 4c 4c 2c 20 2a ewroot = NULL, *
b030: 6c 6f 67 5f 66 69 6c 65 20 3d 20 4c 4f 47 5f 46 log_file = LOG_F
b040: 49 4c 45 3b 0a 09 46 49 4c 45 20 2a 6c 6f 67 5f ILE;..FILE *log_
b050: 66 70 3b 0a 09 75 69 64 5f 74 20 75 73 65 72 20 fp;..uid_t user
b060: 3d 20 30 3b 0a 09 69 6e 74 20 70 6f 72 74 20 3d = 0;..int port =
b070: 20 50 4f 52 54 2c 20 74 68 72 65 61 64 5f 63 6f PORT, thread_co
b080: 75 6e 74 20 3d 20 54 48 52 45 41 44 5f 43 4f 55 unt = THREAD_COU
b090: 4e 54 3b 0a 09 69 6e 74 20 63 61 63 68 65 5f 73 NT;..int cache_s
b0a0: 69 7a 65 20 3d 20 43 41 43 48 45 5f 53 49 5a 45 ize = CACHE_SIZE
b0b0: 3b 0a 09 69 6e 74 20 69 6e 69 74 5f 72 65 74 2c ;..int init_ret,
b0c0: 20 63 68 72 6f 6f 74 5f 72 65 74 2c 20 73 65 74 chroot_ret, set
b0d0: 75 69 64 5f 72 65 74 2c 20 6c 6f 6f 6b 75 70 5f uid_ret, lookup_
b0e0: 72 65 74 2c 20 63 68 64 69 72 5f 72 65 74 3b 0a ret, chdir_ret;.
b0f0: 09 69 6e 74 20 73 65 74 75 69 64 5f 65 6e 61 62 .int setuid_enab
b100: 6c 65 64 20 3d 20 30 2c 20 64 61 65 6d 6f 6e 5f led = 0, daemon_
b110: 65 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 09 69 6e enabled = 0;..in
b120: 74 20 63 68 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a t ch;..int fd;..
b130: 09 2f 2a 20 50 72 6f 63 65 73 73 20 61 72 67 75 ./* Process argu
b140: 6d 65 6e 74 73 20 2a 2f 0a 09 66 69 6c 65 64 5f ments */..filed_
b150: 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f getopt_long_seto
b160: 70 74 28 26 6f 70 74 69 6f 6e 73 5b 30 5d 2c 20 pt(&options[0],
b170: 22 70 6f 72 74 22 2c 20 72 65 71 75 69 72 65 64 "port", required
b180: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 70 27 29 3b _argument, 'p');
b190: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c ..filed_getopt_l
b1a0: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 ong_setopt(&opti
b1b0: 6f 6e 73 5b 31 5d 2c 20 22 74 68 72 65 61 64 73 ons[1], "threads
b1c0: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 ", required_argu
b1d0: 6d 65 6e 74 2c 20 27 74 27 29 3b 0a 09 66 69 6c ment, 't');..fil
b1e0: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 ed_getopt_long_s
b1f0: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 32 etopt(&options[2
b200: 5d 2c 20 22 63 61 63 68 65 22 2c 20 72 65 71 75 ], "cache", requ
b210: 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 ired_argument, '
b220: 63 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f c');..filed_geto
b230: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
b240: 6f 70 74 69 6f 6e 73 5b 33 5d 2c 20 22 62 69 6e options[3], "bin
b250: 64 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 d", required_arg
b260: 75 6d 65 6e 74 2c 20 27 62 27 29 3b 0a 09 66 69 ument, 'b');..fi
b270: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f led_getopt_long_
b280: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b setopt(&options[
b290: 34 5d 2c 20 22 75 73 65 72 22 2c 20 72 65 71 75 4], "user", requ
b2a0: 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 ired_argument, '
b2b0: 75 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f u');..filed_geto
b2c0: 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 pt_long_setopt(&
b2d0: 6f 70 74 69 6f 6e 73 5b 35 5d 2c 20 22 72 6f 6f options[5], "roo
b2e0: 74 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 t", required_arg
b2f0: 75 6d 65 6e 74 2c 20 27 72 27 29 3b 0a 09 66 69 ument, 'r');..fi
b300: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f led_getopt_long_
b310: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b setopt(&options[
b320: 36 5d 2c 20 22 68 65 6c 70 22 2c 20 6e 6f 5f 61 6], "help", no_a
b330: 72 67 75 6d 65 6e 74 2c 20 27 68 27 29 3b 0a 09 rgument, 'h');..
b340: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
b350: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
b360: 73 5b 37 5d 2c 20 22 64 61 65 6d 6f 6e 22 2c 20 s[7], "daemon",
b370: 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 64 27 no_argument, 'd'
b380: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 );..filed_getopt
b390: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 _long_setopt(&op
b3a0: 74 69 6f 6e 73 5b 38 5d 2c 20 22 6c 6f 67 22 2c tions[8], "log",
b3b0: 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 required_argume
b3c0: 6e 74 2c 20 27 6c 27 29 3b 0a 09 66 69 6c 65 64 nt, 'l');..filed
b3d0: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 _getopt_long_set
b3e0: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 39 5d 2c opt(&options[9],
b3f0: 20 22 76 65 72 73 69 6f 6e 22 2c 20 6e 6f 5f 61 "version", no_a
b400: 72 67 75 6d 65 6e 74 2c 20 27 76 27 29 3b 0a 09 rgument, 'v');..
b410: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
b420: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
b430: 73 5b 31 30 5d 2c 20 22 76 68 6f 73 74 22 2c 20 s[10], "vhost",
b440: 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 56 27 no_argument, 'V'
b450: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 );..filed_getopt
b460: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 _long_setopt(&op
b470: 74 69 6f 6e 73 5b 31 31 5d 2c 20 4e 55 4c 4c 2c tions[11], NULL,
b480: 20 30 2c 20 30 29 3b 0a 09 77 68 69 6c 65 20 28 0, 0);..while (
b490: 28 63 68 20 3d 20 67 65 74 6f 70 74 5f 6c 6f 6e (ch = getopt_lon
b4a0: 67 28 61 72 67 63 2c 20 61 72 67 76 2c 20 22 70 g(argc, argv, "p
b4b0: 3a 74 3a 63 3a 62 3a 75 3a 72 3a 6c 3a 68 64 76 :t:c:b:u:r:l:hdv
b4c0: 56 22 2c 20 6f 70 74 69 6f 6e 73 2c 20 4e 55 4c V", options, NUL
b4d0: 4c 29 29 20 21 3d 20 2d 31 29 20 7b 0a 09 09 73 L)) != -1) {...s
b4e0: 77 69 74 63 68 28 63 68 29 20 7b 0a 09 09 09 63 witch(ch) {....c
b4f0: 61 73 65 20 27 70 27 3a 0a 09 09 09 09 70 6f 72 ase 'p':.....por
b500: 74 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 t = atoi(optarg)
b510: 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 ;.....break;....
b520: 63 61 73 65 20 27 74 27 3a 0a 09 09 09 09 74 68 case 't':.....th
b530: 72 65 61 64 5f 63 6f 75 6e 74 20 3d 20 61 74 6f read_count = ato
b540: 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 i(optarg);.....b
b550: 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 63 reak;....case 'c
b560: 27 3a 0a 09 09 09 09 63 61 63 68 65 5f 73 69 7a ':.....cache_siz
b570: 65 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 e = atoi(optarg)
b580: 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 ;.....break;....
b590: 63 61 73 65 20 27 62 27 3a 0a 09 09 09 09 62 69 case 'b':.....bi
b5a0: 6e 64 5f 61 64 64 72 20 3d 20 73 74 72 64 75 70 nd_addr = strdup
b5b0: 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 (optarg);.....br
b5c0: 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 75 27 eak;....case 'u'
b5d0: 3a 0a 09 09 09 09 73 65 74 75 69 64 5f 65 6e 61 :.....setuid_ena
b5e0: 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 09 6c 6f bled = 1;.....lo
b5f0: 6f 6b 75 70 5f 72 65 74 20 3d 20 66 69 6c 65 64 okup_ret = filed
b600: 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 6f 70 74 _user_lookup(opt
b610: 61 72 67 2c 20 26 75 73 65 72 29 3b 0a 09 09 09 arg, &user);....
b620: 09 69 66 20 28 6c 6f 6f 6b 75 70 5f 72 65 74 20 .if (lookup_ret
b630: 21 3d 20 30 29 20 7b 0a 09 09 09 09 09 66 69 6c != 0) {......fil
b640: 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73 74 ed_print_help(st
b650: 64 65 72 72 2c 20 30 2c 20 22 49 6e 76 61 6c 69 derr, 0, "Invali
b660: 64 20 75 73 65 72 6e 61 6d 65 20 73 70 65 63 69 d username speci
b670: 66 69 65 64 22 29 3b 0a 0a 09 09 09 09 09 72 65 fied");.......re
b680: 74 75 72 6e 28 31 29 3b 0a 09 09 09 09 7d 0a 09 turn(1);.....}..
b690: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 ...break;....cas
b6a0: 65 20 27 72 27 3a 0a 09 09 09 09 66 69 6c 65 64 e 'r':.....filed
b6b0: 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 5f _path_translate_
b6c0: 73 65 74 5f 72 6f 6f 74 28 6e 65 77 72 6f 6f 74 set_root(newroot
b6d0: 2c 20 26 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e , &thread_option
b6e0: 73 2c 20 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 s, optarg);.....
b6f0: 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 break;....case '
b700: 6c 27 3a 0a 09 09 09 09 6c 6f 67 5f 66 69 6c 65 l':.....log_file
b710: 20 3d 20 73 74 72 64 75 70 28 6f 70 74 61 72 67 = strdup(optarg
b720: 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 );.....break;...
b730: 09 63 61 73 65 20 27 64 27 3a 0a 09 09 09 09 64 .case 'd':.....d
b740: 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 aemon_enabled =
b750: 31 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 1;.....break;...
b760: 09 63 61 73 65 20 27 56 27 3a 0a 09 09 09 09 74 .case 'V':.....t
b770: 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73 2e 76 68 hread_options.vh
b780: 6f 73 74 73 5f 65 6e 61 62 6c 65 64 20 3d 20 31 osts_enabled = 1
b790: 3b 0a 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 ;......break;...
b7a0: 09 63 61 73 65 20 27 76 27 3a 0a 09 09 09 09 70 .case 'v':.....p
b7b0: 72 69 6e 74 66 28 22 66 69 6c 65 64 20 76 65 72 rintf("filed ver
b7c0: 73 69 6f 6e 20 25 73 5c 6e 22 2c 20 46 49 4c 45 sion %s\n", FILE
b7d0: 44 5f 56 45 52 53 49 4f 4e 29 3b 0a 0a 09 09 09 D_VERSION);.....
b7e0: 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 09 63 .return(0);....c
b7f0: 61 73 65 20 27 3f 27 3a 0a 09 09 09 63 61 73 65 ase '?':....case
b800: 20 27 3a 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f ':':.....filed_
b810: 70 72 69 6e 74 5f 68 65 6c 70 28 73 74 64 65 72 print_help(stder
b820: 72 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 r, 0, NULL);....
b830: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 09 ..return(1);....
b840: 63 61 73 65 20 27 68 27 3a 0a 09 09 09 09 66 69 case 'h':.....fi
b850: 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73 led_print_help(s
b860: 74 64 6f 75 74 2c 20 31 2c 20 4e 55 4c 4c 29 3b tdout, 1, NULL);
b870: 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 30 29 3b ......return(0);
b880: 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 4f 70 65 ...}..}.../* Ope
b890: 6e 20 6c 6f 67 20 66 69 6c 65 20 2a 2f 0a 09 6c n log file */..l
b8a0: 6f 67 5f 66 70 20 3d 20 66 69 6c 65 64 5f 6c 6f og_fp = filed_lo
b8b0: 67 5f 6f 70 65 6e 28 6c 6f 67 5f 66 69 6c 65 29 g_open(log_file)
b8c0: 3b 0a 09 69 66 20 28 6c 6f 67 5f 66 70 20 3d 3d ;..if (log_fp ==
b8d0: 20 4e 55 4c 4c 29 20 7b 0a 09 09 70 65 72 72 6f NULL) {...perro
b8e0: 72 28 22 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 r("filed_log_ope
b8f0: 6e 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 34 n");....return(4
b900: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 );..}.../* Creat
b910: 65 20 6c 69 73 74 65 6e 69 6e 67 20 73 6f 63 6b e listening sock
b920: 65 74 20 2a 2f 0a 09 66 64 20 3d 20 66 69 6c 65 et */..fd = file
b930: 64 5f 6c 69 73 74 65 6e 28 62 69 6e 64 5f 61 64 d_listen(bind_ad
b940: 64 72 2c 20 70 6f 72 74 29 3b 0a 09 69 66 20 28 dr, port);..if (
b950: 66 64 20 3c 20 30 29 20 7b 0a 09 09 70 65 72 72 fd < 0) {...perr
b960: 6f 72 28 22 66 69 6c 65 64 5f 6c 69 73 74 65 6e or("filed_listen
b970: 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 31 29 ");....return(1)
b980: 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 ;..}.../* Initia
b990: 6c 69 7a 65 20 74 69 6d 65 6f 75 74 20 73 74 72 lize timeout str
b9a0: 75 63 74 75 72 65 73 20 2a 2f 0a 09 69 6e 69 74 uctures */..init
b9b0: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 73 6f 63 _ret = filed_soc
b9c0: 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69 74 28 kettimeout_init(
b9d0: 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 );..if (init_ret
b9e0: 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f != 0) {...perro
b9f0: 72 28 22 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 r("filed_sockett
ba00: 69 6d 65 6f 75 74 5f 69 6e 69 74 22 29 3b 0a 0a imeout_init");..
ba10: 09 09 72 65 74 75 72 6e 28 38 29 3b 0a 09 7d 0a ..return(8);..}.
ba20: 0a 09 2f 2a 20 42 65 63 6f 6d 65 20 61 20 64 61 ../* Become a da
ba30: 65 6d 6f 6e 20 2a 2f 0a 09 69 66 20 28 64 61 65 emon */..if (dae
ba40: 6d 6f 6e 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 mon_enabled) {..
ba50: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 .init_ret = file
ba60: 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 29 3b 0a 09 d_daemonize();..
ba70: 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d .if (init_ret !=
ba80: 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 0) {....perror(
ba90: 22 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 "filed_daemonize
baa0: 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 36 ");.....return(6
bab0: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 43 );...}..}.../* C
bac0: 68 72 6f 6f 74 2c 20 69 66 20 61 70 70 72 6f 70 hroot, if approp
bad0: 72 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 6e 65 riate */..if (ne
bae0: 77 72 6f 6f 74 29 20 7b 0a 09 09 63 68 64 69 72 wroot) {...chdir
baf0: 5f 72 65 74 20 3d 20 63 68 64 69 72 28 6e 65 77 _ret = chdir(new
bb00: 72 6f 6f 74 29 3b 0a 09 09 69 66 20 28 63 68 64 root);...if (chd
bb10: 69 72 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 ir_ret != 0) {..
bb20: 09 09 70 65 72 72 6f 72 28 22 63 68 64 69 72 22 ..perror("chdir"
bb30: 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 );.....return(1)
bb40: 3b 0a 09 09 7d 0a 0a 09 09 63 68 72 6f 6f 74 5f ;...}....chroot_
bb50: 72 65 74 20 3d 20 63 68 72 6f 6f 74 28 22 2e 22 ret = chroot("."
bb60: 29 3b 0a 09 09 69 66 20 28 63 68 72 6f 6f 74 5f );...if (chroot_
bb70: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 ret != 0) {....p
bb80: 65 72 72 6f 72 28 22 63 68 72 6f 6f 74 22 29 3b error("chroot");
bb90: 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a .....return(1);.
bba0: 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 72 6f 70 ..}..}.../* Drop
bbb0: 20 70 72 69 76 69 6c 65 67 65 73 2c 20 69 66 20 privileges, if
bbc0: 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09 appropriate */..
bbd0: 69 66 20 28 73 65 74 75 69 64 5f 65 6e 61 62 6c if (setuid_enabl
bbe0: 65 64 29 20 7b 0a 09 09 73 65 74 75 69 64 5f 72 ed) {...setuid_r
bbf0: 65 74 20 3d 20 73 65 74 75 69 64 28 75 73 65 72 et = setuid(user
bc00: 29 3b 0a 09 09 69 66 20 28 73 65 74 75 69 64 5f );...if (setuid_
bc10: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 ret != 0) {....p
bc20: 65 72 72 6f 72 28 22 73 65 74 75 69 64 22 29 3b error("setuid");
bc30: 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a .....return(1);.
bc40: 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 ..}..}.../* Init
bc50: 69 61 6c 69 7a 65 20 2a 2f 0a 09 69 6e 69 74 5f ialize */..init_
bc60: 72 65 74 20 3d 20 66 69 6c 65 64 5f 69 6e 69 74 ret = filed_init
bc70: 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 69 (cache_size);..i
bc80: 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 f (init_ret != 0
bc90: 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 ) {...perror("fi
bca0: 6c 65 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 led_init");....r
bcb0: 65 74 75 72 6e 28 33 29 3b 0a 09 7d 0a 0a 09 2f eturn(3);..}.../
bcc0: 2a 20 43 72 65 61 74 65 20 6c 6f 67 67 69 6e 67 * Create logging
bcd0: 20 74 68 72 65 61 64 20 2a 2f 0a 09 69 6e 69 74 thread */..init
bce0: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 _ret = filed_log
bcf0: 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 ging_thread_init
bd00: 28 6c 6f 67 5f 66 70 29 3b 0a 09 69 66 20 28 69 (log_fp);..if (i
bd10: 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a nit_ret != 0) {.
bd20: 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f ..perror("filed_
bd30: 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 logging_thread_i
bd40: 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e nit");....return
bd50: 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 (4);..}.../* Cre
bd60: 61 74 65 20 73 6f 63 6b 65 74 20 74 65 72 6d 69 ate socket termi
bd70: 6e 61 74 69 6f 6e 20 74 68 72 65 61 64 20 2a 2f nation thread */
bd80: 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c ..init_ret = fil
bd90: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
bda0: 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 29 3b 0a _thread_init();.
bdb0: 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d .if (init_ret !=
bdc0: 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 0) {...perror("
bdd0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
bde0: 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e 69 74 22 out_thread_init"
bdf0: 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 37 29 3b );....return(7);
be00: 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 ..}.../* Create
be10: 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a worker threads *
be20: 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 /..init_ret = fi
be30: 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 led_worker_threa
be40: 64 73 5f 69 6e 69 74 28 66 64 2c 20 74 68 72 65 ds_init(fd, thre
be50: 61 64 5f 63 6f 75 6e 74 2c 20 26 74 68 72 65 61 ad_count, &threa
be60: 64 5f 6f 70 74 69 6f 6e 73 29 3b 0a 09 69 66 20 d_options);..if
be70: 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 (init_ret != 0)
be80: 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 {...perror("file
be90: 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 d_worker_threads
bea0: 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 _init");....retu
beb0: 72 6e 28 35 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 57 rn(5);..}.../* W
bec0: 61 69 74 20 66 6f 72 20 74 68 72 65 61 64 73 20 ait for threads
bed0: 74 6f 20 65 78 69 74 20 2a 2f 0a 09 2f 2a 20 58 to exit */../* X
bee0: 58 58 3a 54 4f 44 4f 3a 20 4d 6f 6e 69 74 6f 72 XX:TODO: Monitor
bef0: 20 74 68 72 65 61 64 20 75 73 61 67 65 20 2a 2f thread usage */
bf00: 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 ..while (1) {...
bf10: 73 6c 65 65 70 28 38 36 34 30 30 29 3b 0a 09 7d sleep(86400);..}
bf20: 0a 0a 09 2f 2a 20 52 65 74 75 72 6e 20 69 6e 20 .../* Return in
bf30: 66 61 69 6c 75 72 65 20 2a 2f 0a 09 72 65 74 75 failure */..retu
bf40: 72 6e 28 32 29 3b 0a 7d 0a rn(2);.}.