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 20 2f 2a 20 46 49 4c .}.#endif /* FIL
3810: 45 44 5f 44 4f 4e 54 5f 4c 4f 47 20 2a 2f 0a 0a ED_DONT_LOG */..
3820: 23 69 66 64 65 66 20 46 49 4c 45 44 5f 44 4f 4e #ifdef FILED_DON
3830: 54 5f 54 49 4d 45 4f 55 54 0a 23 64 65 66 69 6e T_TIMEOUT.#defin
3840: 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 e filed_socketti
3850: 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e 69 meout_thread_ini
3860: 74 28 29 20 30 0a 23 64 65 66 69 6e 65 20 66 69 t() 0.#define fi
3870: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
3880: 74 5f 69 6e 69 74 28 29 20 30 0a 23 64 65 66 69 t_init() 0.#defi
3890: 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ne filed_sockett
38a0: 69 6d 65 6f 75 74 5f 61 63 63 65 70 74 28 78 29 imeout_accept(x)
38b0: 20 2f 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 /**/.#define fi
38c0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
38d0: 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 73 74 61 t_processing_sta
38e0: 72 74 28 78 29 20 2f 2a 2a 2f 0a 23 64 65 66 69 rt(x) /**/.#defi
38f0: 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ne filed_sockett
3900: 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e imeout_processin
3910: 67 5f 65 6e 64 28 78 29 20 2f 2a 2a 2f 0a 23 64 g_end(x) /**/.#d
3920: 65 66 69 6e 65 20 66 69 6c 65 64 5f 73 6f 63 6b efine filed_sock
3930: 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 ettimeout_close(
3940: 78 2c 20 79 29 20 2f 2a 2a 2f 0a 23 65 6c 73 65 x, y) /**/.#else
3950: 0a 74 69 6d 65 5f 74 20 66 69 6c 65 64 5f 73 6f .time_t filed_so
3960: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 69 6d 65 ckettimeout_time
3970: 3b 0a 73 74 72 75 63 74 20 7b 0a 09 74 69 6d 65 ;.struct {..time
3980: 5f 74 20 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 _t expiration_ti
3990: 6d 65 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 74 me;..pthread_t t
39a0: 68 72 65 61 64 5f 69 64 3b 0a 09 65 6e 75 6d 20 hread_id;..enum
39b0: 7b 0a 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 {...filed_socket
39c0: 74 69 6d 65 6f 75 74 5f 76 61 6c 69 64 2c 0a 09 timeout_valid,..
39d0: 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d .filed_sockettim
39e0: 65 6f 75 74 5f 69 6e 76 61 6c 69 64 2c 0a 09 7d eout_invalid,..}
39f0: 20 76 61 6c 69 64 3b 0a 7d 20 2a 66 69 6c 65 64 valid;.} *filed
3a00: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 _sockettimeout_s
3a10: 6f 63 6b 73 74 61 74 75 73 3b 0a 6c 6f 6e 67 20 ockstatus;.long
3a20: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
3a30: 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5f 6c out_sockstatus_l
3a40: 65 6e 67 74 68 3b 0a 69 6e 74 20 66 69 6c 65 64 ength;.int filed
3a50: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 64 _sockettimeout_d
3a60: 65 76 6e 75 6c 6c 5f 66 64 3b 0a 70 74 68 72 65 evnull_fd;.pthre
3a70: 61 64 5f 6d 75 74 65 78 5f 74 20 66 69 6c 65 64 ad_mutex_t filed
3a80: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d _sockettimeout_m
3a90: 75 74 65 78 20 3d 20 50 54 48 52 45 41 44 5f 4d utex = PTHREAD_M
3aa0: 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
3ab0: 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 ;..static int fi
3ac0: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
3ad0: 74 5f 73 6f 63 6b 66 64 5f 69 6e 5f 72 61 6e 67 t_sockfd_in_rang
3ae0: 65 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a e(int sockfd) {.
3af0: 09 69 66 20 28 73 6f 63 6b 66 64 20 3c 20 33 29 .if (sockfd < 3)
3b00: 20 7b 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a {...return(0);.
3b10: 09 7d 0a 0a 09 69 66 20 28 73 6f 63 6b 66 64 20 .}...if (sockfd
3b20: 3e 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 > filed_socketti
3b30: 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 meout_sockstatus
3b40: 5f 6c 65 6e 67 74 68 29 20 7b 0a 09 09 72 65 74 _length) {...ret
3b50: 75 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 72 65 74 urn(0);..}...ret
3b60: 75 72 6e 28 31 29 3b 0a 7d 0a 0a 73 74 61 74 69 urn(1);.}..stati
3b70: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63 c void filed_soc
3b80: 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78 70 69 72 kettimeout_expir
3b90: 65 28 69 6e 74 20 73 6f 63 6b 66 64 2c 20 69 6e e(int sockfd, in
3ba0: 74 20 6c 65 6e 67 74 68 2c 20 69 6e 74 20 6c 6f t length, int lo
3bb0: 63 6b 68 65 6c 64 29 20 7b 0a 09 74 69 6d 65 5f ckheld) {..time_
3bc0: 74 20 6e 6f 77 2c 20 65 78 70 69 72 65 3b 0a 0a t now, expire;..
3bd0: 09 69 66 20 28 21 6c 6f 63 6b 68 65 6c 64 29 20 .if (!lockheld)
3be0: 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 {...pthread_mute
3bf0: 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 73 6f x_lock(&filed_so
3c00: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 65 ckettimeout_mute
3c10: 78 29 3b 0a 09 7d 0a 0a 09 6e 6f 77 20 3d 20 66 x);..}...now = f
3c20: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
3c30: 75 74 5f 74 69 6d 65 3b 0a 0a 09 65 78 70 69 72 ut_time;...expir
3c40: 65 20 3d 20 6e 6f 77 20 2b 20 6c 65 6e 67 74 68 e = now + length
3c50: 3b 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 ;...filed_socket
3c60: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
3c70: 75 73 5b 73 6f 63 6b 66 64 5d 2e 65 78 70 69 72 us[sockfd].expir
3c80: 61 74 69 6f 6e 5f 74 69 6d 65 20 3d 20 65 78 70 ation_time = exp
3c90: 69 72 65 3b 0a 0a 09 69 66 20 28 21 6c 6f 63 6b ire;...if (!lock
3ca0: 68 65 6c 64 29 20 7b 0a 09 09 70 74 68 72 65 61 held) {...pthrea
3cb0: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
3cc0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
3cd0: 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 09 7d 0a 0a out_mutex);..}..
3ce0: 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 .return;.}..stat
3cf0: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f ic void filed_so
3d00: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 61 63 63 65 ckettimeout_acce
3d10: 70 74 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b pt(int sockfd) {
3d20: 0a 09 69 66 20 28 21 66 69 6c 65 64 5f 73 6f 63 ..if (!filed_soc
3d30: 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 kettimeout_sockf
3d40: 64 5f 69 6e 5f 72 61 6e 67 65 28 73 6f 63 6b 66 d_in_range(sockf
3d50: 64 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 3b 0a d)) {...return;.
3d60: 09 7d 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 .}...pthread_mut
3d70: 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 73 ex_lock(&filed_s
3d80: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 ockettimeout_mut
3d90: 65 78 29 3b 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 ex);...filed_soc
3da0: 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78 70 69 72 kettimeout_expir
3db0: 65 28 73 6f 63 6b 66 64 2c 20 36 30 2c 20 31 29 e(sockfd, 60, 1)
3dc0: 3b 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 ;...filed_socket
3dd0: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
3de0: 75 73 5b 73 6f 63 6b 66 64 5d 2e 74 68 72 65 61 us[sockfd].threa
3df0: 64 5f 69 64 20 3d 20 70 74 68 72 65 61 64 5f 73 d_id = pthread_s
3e00: 65 6c 66 28 29 3b 0a 0a 09 66 69 6c 65 64 5f 73 elf();...filed_s
3e10: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
3e20: 6b 73 74 61 74 75 73 5b 73 6f 63 6b 66 64 5d 2e kstatus[sockfd].
3e30: 76 61 6c 69 64 20 3d 20 66 69 6c 65 64 5f 73 6f valid = filed_so
3e40: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 76 61 6c 69 ckettimeout_vali
3e50: 64 3b 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 d;...pthread_mut
3e60: 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 ex_unlock(&filed
3e70: 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d _sockettimeout_m
3e80: 75 74 65 78 29 3b 0a 0a 09 72 65 74 75 72 6e 3b utex);...return;
3e90: 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .}..static void
3ea0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
3eb0: 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 73 out_processing_s
3ec0: 74 61 72 74 28 69 6e 74 20 73 6f 63 6b 66 64 29 tart(int sockfd)
3ed0: 20 7b 0a 09 69 66 20 28 21 66 69 6c 65 64 5f 73 {..if (!filed_s
3ee0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
3ef0: 6b 66 64 5f 69 6e 5f 72 61 6e 67 65 28 73 6f 63 kfd_in_range(soc
3f00: 6b 66 64 29 29 20 7b 0a 09 09 72 65 74 75 72 6e kfd)) {...return
3f10: 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 ;..}...filed_soc
3f20: 6b 65 74 74 69 6d 65 6f 75 74 5f 65 78 70 69 72 kettimeout_expir
3f30: 65 28 73 6f 63 6b 66 64 2c 20 38 36 34 30 30 2c e(sockfd, 86400,
3f40: 20 30 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0);...return;.}
3f50: 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 ..static void fi
3f60: 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 led_sockettimeou
3f70: 74 5f 70 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64 t_processing_end
3f80: 28 69 6e 74 20 73 6f 63 6b 66 64 29 20 7b 0a 09 (int sockfd) {..
3f90: 69 66 20 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65 if (!filed_socke
3fa0: 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f ttimeout_sockfd_
3fb0: 69 6e 5f 72 61 6e 67 65 28 73 6f 63 6b 66 64 29 in_range(sockfd)
3fc0: 29 20 7b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d ) {...return;..}
3fd0: 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ...filed_sockett
3fe0: 69 6d 65 6f 75 74 5f 65 78 70 69 72 65 28 73 6f imeout_expire(so
3ff0: 63 6b 66 64 2c 20 36 30 2c 20 30 29 3b 0a 0a 09 ckfd, 60, 0);...
4000: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 return;.}..stati
4010: 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 73 6f 63 c void filed_soc
4020: 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 kettimeout_close
4030: 28 69 6e 74 20 73 6f 63 6b 66 64 2c 20 69 6e 74 (int sockfd, int
4040: 20 6c 6f 63 6b 68 65 6c 64 29 20 7b 0a 09 69 66 lockheld) {..if
4050: 20 28 21 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 (!filed_sockett
4060: 69 6d 65 6f 75 74 5f 73 6f 63 6b 66 64 5f 69 6e imeout_sockfd_in
4070: 5f 72 61 6e 67 65 28 73 6f 63 6b 66 64 29 29 20 _range(sockfd))
4080: 7b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 7d 0a 0a {...return;..}..
4090: 09 69 66 20 28 21 6c 6f 63 6b 68 65 6c 64 29 20 .if (!lockheld)
40a0: 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 {...pthread_mute
40b0: 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 73 6f x_lock(&filed_so
40c0: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 65 ckettimeout_mute
40d0: 78 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 73 x);..}...filed_s
40e0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 ockettimeout_soc
40f0: 6b 73 74 61 74 75 73 5b 73 6f 63 6b 66 64 5d 2e kstatus[sockfd].
4100: 76 61 6c 69 64 20 3d 20 66 69 6c 65 64 5f 73 6f valid = filed_so
4110: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 76 61 ckettimeout_inva
4120: 6c 69 64 3b 0a 0a 09 69 66 20 28 21 6c 6f 63 6b lid;...if (!lock
4130: 68 65 6c 64 29 20 7b 0a 09 09 70 74 68 72 65 61 held) {...pthrea
4140: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
4150: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
4160: 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 09 7d 0a 0a out_mutex);..}..
4170: 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 .return;.}..stat
4180: 69 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f 73 ic void *filed_s
4190: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 ockettimeout_thr
41a0: 65 61 64 28 76 6f 69 64 20 2a 61 72 67 29 20 7b ead(void *arg) {
41b0: 0a 09 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 ..struct timespe
41c0: 63 20 73 6c 65 65 70 5f 74 69 6d 65 3b 0a 09 74 c sleep_time;..t
41d0: 69 6d 65 5f 74 20 6e 6f 77 2c 20 65 78 70 69 72 ime_t now, expir
41e0: 61 74 69 6f 6e 5f 74 69 6d 65 3b 0a 09 70 74 68 ation_time;..pth
41f0: 72 65 61 64 5f 74 20 74 68 72 65 61 64 5f 69 64 read_t thread_id
4200: 3b 0a 09 6c 6f 6e 67 20 69 64 78 3b 0a 09 69 6e ;..long idx;..in
4210: 74 20 63 6f 75 6e 74 3b 0a 09 69 6e 74 20 76 61 t count;..int va
4220: 6c 69 64 3b 0a 09 69 6e 74 20 74 69 6d 65 5f 69 lid;..int time_i
4230: 6e 74 65 72 76 61 6c 20 3d 20 33 30 3b 0a 09 69 nterval = 30;..i
4240: 6e 74 20 63 68 65 63 6b 5f 70 65 72 69 6f 64 20 nt check_period
4250: 3d 20 39 30 3b 0a 0a 09 66 69 6c 65 64 5f 73 6f = 90;...filed_so
4260: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 69 6d 65 ckettimeout_time
4270: 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a = time(NULL);..
4280: 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 66 .while (1) {...f
4290: 6f 72 20 28 63 6f 75 6e 74 20 3d 20 30 3b 20 63 or (count = 0; c
42a0: 6f 75 6e 74 20 3c 20 28 63 68 65 63 6b 5f 70 65 ount < (check_pe
42b0: 72 69 6f 64 20 2f 20 74 69 6d 65 5f 69 6e 74 65 riod / time_inte
42c0: 72 76 61 6c 29 3b 20 63 6f 75 6e 74 2b 2b 29 20 rval); count++)
42d0: 7b 0a 09 09 09 73 6c 65 65 70 5f 74 69 6d 65 2e {....sleep_time.
42e0: 74 76 5f 73 65 63 20 3d 20 74 69 6d 65 5f 69 6e tv_sec = time_in
42f0: 74 65 72 76 61 6c 3b 0a 09 09 09 73 6c 65 65 70 terval;....sleep
4300: 5f 74 69 6d 65 2e 74 76 5f 6e 73 65 63 20 3d 20 _time.tv_nsec =
4310: 30 3b 0a 09 09 09 6e 61 6e 6f 73 6c 65 65 70 28 0;....nanosleep(
4320: 26 73 6c 65 65 70 5f 74 69 6d 65 2c 20 4e 55 4c &sleep_time, NUL
4330: 4c 29 3b 0a 0a 09 09 09 70 74 68 72 65 61 64 5f L);.....pthread_
4340: 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 mutex_lock(&file
4350: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
4360: 6d 75 74 65 78 29 3b 0a 0a 09 09 09 6e 6f 77 20 mutex);.....now
4370: 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 = time(NULL);...
4380: 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
4390: 6d 65 6f 75 74 5f 74 69 6d 65 20 3d 20 6e 6f 77 meout_time = now
43a0: 3b 0a 0a 09 09 09 70 74 68 72 65 61 64 5f 6d 75 ;.....pthread_mu
43b0: 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 tex_unlock(&file
43c0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
43d0: 6d 75 74 65 78 29 3b 0a 09 09 7d 0a 0a 09 09 70 mutex);...}....p
43e0: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 thread_mutex_loc
43f0: 6b 28 26 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 k(&filed_sockett
4400: 69 6d 65 6f 75 74 5f 6d 75 74 65 78 29 3b 0a 0a imeout_mutex);..
4410: 09 09 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 ..for (idx = 0;
4420: 69 64 78 20 3c 20 66 69 6c 65 64 5f 73 6f 63 6b idx < filed_sock
4430: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 ettimeout_sockst
4440: 61 74 75 73 5f 6c 65 6e 67 74 68 3b 20 69 64 78 atus_length; idx
4450: 2b 2b 29 20 7b 0a 09 09 09 76 61 6c 69 64 20 3d ++) {....valid =
4460: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d filed_sockettim
4470: 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 73 5b eout_sockstatus[
4480: 69 64 78 5d 2e 76 61 6c 69 64 3b 0a 0a 09 09 09 idx].valid;.....
4490: 69 66 20 28 76 61 6c 69 64 20 21 3d 20 66 69 6c if (valid != fil
44a0: 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 ed_sockettimeout
44b0: 5f 76 61 6c 69 64 29 20 7b 0a 09 09 09 09 63 6f _valid) {.....co
44c0: 6e 74 69 6e 75 65 3b 0a 09 09 09 7d 0a 0a 09 09 ntinue;....}....
44d0: 09 65 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 .expiration_time
44e0: 20 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 = filed_sockett
44f0: 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 imeout_sockstatu
4500: 73 5b 69 64 78 5d 2e 65 78 70 69 72 61 74 69 6f s[idx].expiratio
4510: 6e 5f 74 69 6d 65 3b 0a 0a 09 09 09 74 68 72 65 n_time;.....thre
4520: 61 64 5f 69 64 20 3d 20 66 69 6c 65 64 5f 73 6f ad_id = filed_so
4530: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b ckettimeout_sock
4540: 73 74 61 74 75 73 5b 69 64 78 5d 2e 74 68 72 65 status[idx].thre
4550: 61 64 5f 69 64 3b 0a 0a 09 09 09 69 66 20 28 65 ad_id;.....if (e
4560: 78 70 69 72 61 74 69 6f 6e 5f 74 69 6d 65 20 3e xpiration_time >
4570: 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d filed_sockettim
4580: 65 6f 75 74 5f 74 69 6d 65 29 20 7b 0a 09 09 09 eout_time) {....
4590: 09 63 6f 6e 74 69 6e 75 65 3b 0a 09 09 09 7d 0a .continue;....}.
45a0: 0a 09 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 ....filed_socket
45b0: 74 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 69 64 timeout_close(id
45c0: 78 2c 20 31 29 3b 0a 0a 09 09 09 64 75 70 32 28 x, 1);.....dup2(
45d0: 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 filed_sockettime
45e0: 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f 66 64 2c 20 out_devnull_fd,
45f0: 69 64 78 29 3b 0a 0a 09 09 09 70 74 68 72 65 61 idx);.....pthrea
4600: 64 5f 6b 69 6c 6c 28 74 68 72 65 61 64 5f 69 64 d_kill(thread_id
4610: 2c 20 53 49 47 50 49 50 45 29 3b 0a 09 09 7d 0a , SIGPIPE);...}.
4620: 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 ...pthread_mutex
4630: 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 73 _unlock(&filed_s
4640: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 6d 75 74 ockettimeout_mut
4650: 65 78 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e ex);..}...return
4660: 28 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 4e 4f 54 (NULL);.../* NOT
4670: 52 45 41 43 48 3a 20 57 65 20 64 6f 6e 27 74 20 REACH: We don't
4680: 61 63 74 75 61 6c 6c 79 20 74 61 6b 65 20 61 6e actually take an
4690: 79 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 y arguments */..
46a0: 61 72 67 20 3d 20 61 72 67 3b 0a 7d 0a 0a 73 74 arg = arg;.}..st
46b0: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 atic int filed_s
46c0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 72 ockettimeout_thr
46d0: 65 61 64 5f 69 6e 69 74 28 76 6f 69 64 29 20 7b ead_init(void) {
46e0: 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 ..pthread_t thre
46f0: 61 64 5f 69 64 3b 0a 0a 09 70 74 68 72 65 61 64 ad_id;...pthread
4700: 5f 63 72 65 61 74 65 28 26 74 68 72 65 61 64 5f _create(&thread_
4710: 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 64 5f id, NULL, filed_
4720: 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 74 68 sockettimeout_th
4730: 72 65 61 64 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 72 read, NULL);...r
4740: 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 73 74 61 eturn(0);.}..sta
4750: 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 73 6f tic int filed_so
4760: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69 74 ckettimeout_init
4770: 28 76 6f 69 64 29 20 7b 0a 09 6c 6f 6e 67 20 6d (void) {..long m
4780: 61 78 66 64 2c 20 69 64 78 3b 0a 0a 09 6d 61 78 axfd, idx;...max
4790: 66 64 20 3d 20 73 79 73 63 6f 6e 66 28 5f 53 43 fd = sysconf(_SC
47a0: 5f 4f 50 45 4e 5f 4d 41 58 29 3b 0a 09 69 66 20 _OPEN_MAX);..if
47b0: 28 6d 61 78 66 64 20 3c 3d 20 30 29 20 7b 0a 09 (maxfd <= 0) {..
47c0: 09 6d 61 78 66 64 20 3d 20 34 30 39 36 3b 0a 09 .maxfd = 4096;..
47d0: 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 }...filed_socket
47e0: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
47f0: 75 73 5f 6c 65 6e 67 74 68 20 3d 20 6d 61 78 66 us_length = maxf
4800: 64 3b 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 d;..filed_socket
4810: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
4820: 75 73 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 us = malloc(size
4830: 6f 66 28 2a 66 69 6c 65 64 5f 73 6f 63 6b 65 74 of(*filed_socket
4840: 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 timeout_sockstat
4850: 75 73 29 20 2a 20 66 69 6c 65 64 5f 73 6f 63 6b us) * filed_sock
4860: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 ettimeout_sockst
4870: 61 74 75 73 5f 6c 65 6e 67 74 68 29 3b 0a 09 69 atus_length);..i
4880: 66 20 28 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 f (filed_sockett
4890: 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 61 74 75 imeout_sockstatu
48a0: 73 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 s == NULL) {...r
48b0: 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 eturn(-1);..}...
48c0: 66 6f 72 20 28 69 64 78 20 3d 20 30 3b 20 69 64 for (idx = 0; id
48d0: 78 20 3c 20 6d 61 78 66 64 3b 20 69 64 78 2b 2b x < maxfd; idx++
48e0: 29 20 7b 0a 09 09 66 69 6c 65 64 5f 73 6f 63 6b ) {...filed_sock
48f0: 65 74 74 69 6d 65 6f 75 74 5f 73 6f 63 6b 73 74 ettimeout_sockst
4900: 61 74 75 73 5b 69 64 78 5d 2e 76 61 6c 69 64 20 atus[idx].valid
4910: 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 = filed_socketti
4920: 6d 65 6f 75 74 5f 69 6e 76 61 6c 69 64 3b 0a 09 meout_invalid;..
4930: 7d 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 }...filed_socket
4940: 74 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c 6c 5f timeout_devnull_
4950: 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f fd = open("/dev/
4960: 6e 75 6c 6c 22 2c 20 4f 5f 52 44 57 52 29 3b 0a null", O_RDWR);.
4970: 09 69 66 20 28 66 69 6c 65 64 5f 73 6f 63 6b 65 .if (filed_socke
4980: 74 74 69 6d 65 6f 75 74 5f 64 65 76 6e 75 6c 6c ttimeout_devnull
4990: 5f 66 64 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 _fd < 0) {...ret
49a0: 75 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 72 65 urn(-1);..}...re
49b0: 74 75 72 6e 28 30 29 3b 0a 7d 0a 23 65 6e 64 69 turn(0);.}.#endi
49c0: 66 20 2f 2a 20 46 49 4c 45 44 5f 44 4f 4e 54 5f f /* FILED_DONT_
49d0: 54 49 4d 45 4f 55 54 20 2a 2f 0a 0a 23 69 66 6e TIMEOUT */..#ifn
49e0: 64 65 66 20 46 49 4c 45 44 5f 44 4f 5f 53 45 43 def FILED_DO_SEC
49f0: 43 4f 4d 50 0a 23 64 65 66 69 6e 65 20 66 69 6c COMP.#define fil
4a00: 65 64 5f 69 6e 69 74 5f 73 65 63 63 6f 6d 70 28 ed_init_seccomp(
4a10: 29 20 30 0a 23 65 6c 73 65 0a 23 69 6e 63 6c 75 ) 0.#else.#inclu
4a20: 64 65 20 3c 6c 69 6e 75 78 2f 73 65 63 63 6f 6d de <linux/seccom
4a30: 70 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 6c p.h>.#include <l
4a40: 69 6e 75 78 2f 66 69 6c 74 65 72 2e 68 3e 0a 23 inux/filter.h>.#
4a50: 69 6e 63 6c 75 64 65 20 3c 6c 69 6e 75 78 2f 61 include <linux/a
4a60: 75 64 69 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 udit.h>.#include
4a70: 20 3c 73 79 73 2f 70 74 72 61 63 65 2e 68 3e 0a <sys/ptrace.h>.
4a80: 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 72 #include <sys/pr
4a90: 63 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 ctl.h>.#include
4aa0: 3c 73 74 64 64 65 66 2e 68 3e 0a 0a 73 74 61 74 <stddef.h>..stat
4ab0: 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 ic int filed_ini
4ac0: 74 5f 73 65 63 63 6f 6d 70 28 76 6f 69 64 29 20 t_seccomp(void)
4ad0: 7b 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 5f 66 {..struct sock_f
4ae0: 70 72 6f 67 20 66 69 6c 74 65 72 3b 0a 09 73 74 prog filter;..st
4af0: 72 75 63 74 20 73 6f 63 6b 5f 66 69 6c 74 65 72 ruct sock_filter
4b00: 20 72 75 6c 65 73 5b 5d 20 3d 20 7b 0a 23 69 6e rules[] = {.#in
4b10: 63 6c 75 64 65 20 22 66 69 6c 65 64 2e 73 65 63 clude "filed.sec
4b20: 63 6f 6d 70 2e 68 22 0a 09 7d 3b 0a 09 69 6e 74 comp.h"..};..int
4b30: 20 70 72 63 74 6c 5f 72 65 74 3b 0a 0a 09 2f 2a prctl_ret;.../*
4b40: 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e Do not allow an
4b50: 79 20 70 72 69 76 69 6c 65 67 65 20 63 68 61 6e y privilege chan
4b60: 67 65 73 20 62 65 79 6f 6e 64 20 74 68 69 73 20 ges beyond this
4b70: 70 6f 69 6e 74 20 2a 2f 0a 20 09 70 72 63 74 6c point */. .prctl
4b80: 5f 72 65 74 20 3d 20 70 72 63 74 6c 28 50 52 5f _ret = prctl(PR_
4b90: 53 45 54 5f 4e 4f 5f 4e 45 57 5f 50 52 49 56 53 SET_NO_NEW_PRIVS
4ba0: 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 09 , 1, 0, 0, 0);..
4bb0: 69 66 20 28 70 72 63 74 6c 5f 72 65 74 20 21 3d if (prctl_ret !=
4bc0: 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 2d 0) {...return(-
4bd0: 31 29 3b 0a 09 7d 0a 0a 09 66 69 6c 74 65 72 2e 1);..}...filter.
4be0: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 72 75 6c len = sizeof(rul
4bf0: 65 73 29 20 2f 20 73 69 7a 65 6f 66 28 2a 72 75 es) / sizeof(*ru
4c00: 6c 65 73 29 3b 0a 09 66 69 6c 74 65 72 2e 66 69 les);..filter.fi
4c10: 6c 74 65 72 20 3d 20 72 75 6c 65 73 3b 0a 0a 09 lter = rules;...
4c20: 70 72 63 74 6c 5f 72 65 74 20 3d 20 70 72 63 74 prctl_ret = prct
4c30: 6c 28 50 52 5f 53 45 54 5f 53 45 43 43 4f 4d 50 l(PR_SET_SECCOMP
4c40: 2c 20 53 45 43 43 4f 4d 50 5f 4d 4f 44 45 5f 46 , SECCOMP_MODE_F
4c50: 49 4c 54 45 52 2c 20 26 66 69 6c 74 65 72 29 3b ILTER, &filter);
4c60: 0a 09 69 66 20 28 70 72 63 74 6c 5f 72 65 74 20 ..if (prctl_ret
4c70: 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e != 0) {...return
4c80: 28 2d 31 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 (-1);..}...retur
4c90: 6e 28 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f n(0);.}.#endif /
4ca0: 2a 20 46 49 4c 45 44 5f 44 4f 5f 53 45 43 43 4f * FILED_DO_SECCO
4cb0: 4d 50 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 6d 61 74 MP */../* Format
4cc0: 20 74 69 6d 65 20 70 65 72 20 52 46 43 32 36 31 time per RFC261
4cd0: 36 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 6 */.static char
4ce0: 20 2a 66 69 6c 65 64 5f 66 6f 72 6d 61 74 5f 74 *filed_format_t
4cf0: 69 6d 65 28 63 68 61 72 20 2a 62 75 66 66 65 72 ime(char *buffer
4d00: 2c 20 73 69 7a 65 5f 74 20 62 75 66 66 65 72 5f , size_t buffer_
4d10: 6c 65 6e 2c 20 63 6f 6e 73 74 20 74 69 6d 65 5f len, const time_
4d20: 74 20 74 69 6d 65 69 6e 66 6f 29 20 7b 0a 09 73 t timeinfo) {..s
4d30: 74 72 75 63 74 20 74 6d 20 74 69 6d 65 69 6e 66 truct tm timeinf
4d40: 6f 5f 74 6d 2c 20 2a 74 69 6d 65 69 6e 66 6f 5f o_tm, *timeinfo_
4d50: 74 6d 5f 70 3b 0a 0a 09 74 69 6d 65 69 6e 66 6f tm_p;...timeinfo
4d60: 5f 74 6d 5f 70 20 3d 20 67 6d 74 69 6d 65 5f 72 _tm_p = gmtime_r
4d70: 28 26 74 69 6d 65 69 6e 66 6f 2c 20 26 74 69 6d (&timeinfo, &tim
4d80: 65 69 6e 66 6f 5f 74 6d 29 3b 0a 09 69 66 20 28 einfo_tm);..if (
4d90: 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d 3d timeinfo_tm_p ==
4da0: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 NULL) {...retur
4db0: 6e 28 22 75 6e 6b 6e 6f 77 6e 22 29 3b 0a 09 7d n("unknown");..}
4dc0: 0a 0a 09 62 75 66 66 65 72 5b 62 75 66 66 65 72 ...buffer[buffer
4dd0: 5f 6c 65 6e 20 2d 20 31 5d 20 3d 20 27 5c 30 27 _len - 1] = '\0'
4de0: 3b 0a 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 ;..buffer_len =
4df0: 73 74 72 66 74 69 6d 65 28 62 75 66 66 65 72 2c strftime(buffer,
4e00: 20 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 31 2c buffer_len - 1,
4e10: 20 22 25 61 2c 20 25 64 20 25 62 20 25 59 20 25 "%a, %d %b %Y %
4e20: 48 3a 25 4d 3a 25 53 20 47 4d 54 22 2c 20 74 69 H:%M:%S GMT", ti
4e30: 6d 65 69 6e 66 6f 5f 74 6d 5f 70 29 3b 0a 0a 09 meinfo_tm_p);...
4e40: 72 65 74 75 72 6e 28 62 75 66 66 65 72 29 3b 0a return(buffer);.
4e50: 7d 0a 0a 2f 2a 20 68 61 73 68 20 2a 2f 0a 73 74 }../* hash */.st
4e60: 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20 69 6e atic unsigned in
4e70: 74 20 66 69 6c 65 64 5f 68 61 73 68 28 63 6f 6e t filed_hash(con
4e80: 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
4e90: 20 2a 76 61 6c 75 65 2c 20 75 6e 73 69 67 6e 65 *value, unsigne
4ea0: 64 20 69 6e 74 20 6d 6f 64 75 6c 75 73 29 20 7b d int modulus) {
4eb0: 0a 09 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 ..unsigned char
4ec0: 63 75 72 72 2c 20 70 72 65 76 3b 0a 09 69 6e 74 curr, prev;..int
4ed0: 20 64 69 66 66 3b 0a 09 75 6e 73 69 67 6e 65 64 diff;..unsigned
4ee0: 20 69 6e 74 20 72 65 74 76 61 6c 3b 0a 0a 09 72 int retval;...r
4ef0: 65 74 76 61 6c 20 3d 20 6d 6f 64 75 6c 75 73 20 etval = modulus
4f00: 2d 20 31 3b 0a 09 70 72 65 76 20 3d 20 6d 6f 64 - 1;..prev = mod
4f10: 75 6c 75 73 20 25 20 32 35 35 3b 0a 0a 09 77 68 ulus % 255;...wh
4f20: 69 6c 65 20 28 28 63 75 72 72 20 3d 20 2a 76 61 ile ((curr = *va
4f30: 6c 75 65 29 29 20 7b 0a 09 09 69 66 20 28 63 75 lue)) {...if (cu
4f40: 72 72 20 3c 20 33 32 29 20 7b 0a 09 09 09 63 75 rr < 32) {....cu
4f50: 72 72 20 3d 20 32 35 35 20 2d 20 63 75 72 72 3b rr = 255 - curr;
4f60: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 63 ...} else {....c
4f70: 75 72 72 20 2d 3d 20 33 32 3b 0a 09 09 7d 0a 0a urr -= 32;...}..
4f80: 09 09 69 66 20 28 70 72 65 76 20 3c 20 63 75 72 ..if (prev < cur
4f90: 72 29 20 7b 0a 09 09 09 64 69 66 66 20 3d 20 63 r) {....diff = c
4fa0: 75 72 72 20 2d 20 70 72 65 76 3b 0a 09 09 7d 20 urr - prev;...}
4fb0: 65 6c 73 65 20 7b 0a 09 09 09 64 69 66 66 20 3d else {....diff =
4fc0: 20 70 72 65 76 20 2d 20 63 75 72 72 3b 0a 09 09 prev - curr;...
4fd0: 7d 0a 0a 09 09 70 72 65 76 20 3d 20 63 75 72 72 }....prev = curr
4fe0: 3b 0a 0a 09 09 72 65 74 76 61 6c 20 3c 3c 3d 20 ;....retval <<=
4ff0: 33 3b 0a 09 09 72 65 74 76 61 6c 20 26 3d 20 30 3;...retval &= 0
5000: 78 46 46 46 46 46 46 46 46 4c 55 3b 0a 09 09 72 xFFFFFFFFLU;...r
5010: 65 74 76 61 6c 20 5e 3d 20 64 69 66 66 3b 0a 0a etval ^= diff;..
5020: 09 09 76 61 6c 75 65 2b 2b 3b 0a 09 7d 0a 0a 09 ..value++;..}...
5030: 72 65 74 76 61 6c 20 3d 20 72 65 74 76 61 6c 20 retval = retval
5040: 25 20 6d 6f 64 75 6c 75 73 3b 0a 0a 09 72 65 74 % modulus;...ret
5050: 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a urn(retval);.}..
5060: 2f 2a 20 46 69 6e 64 20 61 20 6d 69 6d 65 2d 74 /* Find a mime-t
5070: 79 70 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 ype based on the
5080: 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 73 74 61 filename */.sta
5090: 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
50a0: 66 69 6c 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f filed_determine_
50b0: 6d 69 6d 65 74 79 70 65 28 63 6f 6e 73 74 20 63 mimetype(const c
50c0: 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 09 63 6f har *path) {..co
50d0: 6e 73 74 20 63 68 61 72 20 2a 70 3b 0a 0a 09 70 nst char *p;...p
50e0: 20 3d 20 73 74 72 72 63 68 72 28 70 61 74 68 2c = strrchr(path,
50f0: 20 27 2e 27 29 3b 0a 09 69 66 20 28 70 20 3d 3d '.');..if (p ==
5100: 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 NULL) {...retur
5110: 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f n(FILED_DEFAULT_
5120: 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 70 2b 2b 3b TYPE);..}...p++;
5130: 0a 09 69 66 20 28 2a 70 20 3d 3d 20 27 5c 30 27 ..if (*p == '\0'
5140: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 46 49 4c ) {...return(FIL
5150: 45 44 5f 44 45 46 41 55 4c 54 5f 54 59 50 45 29 ED_DEFAULT_TYPE)
5160: 3b 0a 09 7d 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 ;..}...filed_log
5170: 5f 6d 73 67 5f 64 65 62 75 67 28 22 4c 6f 6f 6b _msg_debug("Look
5180: 69 6e 67 20 75 70 20 4d 49 4d 45 20 74 79 70 65 ing up MIME type
5190: 20 66 6f 72 20 25 73 20 28 68 61 73 68 20 3d 20 for %s (hash =
51a0: 25 6c 6c 75 29 22 2c 20 70 2c 20 28 75 6e 73 69 %llu)", p, (unsi
51b0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
51c0: 66 69 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e 73 filed_hash((cons
51d0: 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
51e0: 2a 29 20 70 2c 20 31 36 37 37 37 32 35 39 29 29 *) p, 16777259))
51f0: 3b 0a 0a 23 69 6e 63 6c 75 64 65 20 22 66 69 6c ;..#include "fil
5200: 65 64 2d 6d 69 6d 65 2d 74 79 70 65 73 2e 68 22 ed-mime-types.h"
5210: 0a 0a 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f ...return(FILED_
5220: 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 7d DEFAULT_TYPE);.}
5230: 0a 0a 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 ../* Generate a
5240: 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 unique identifie
5250: 72 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 r */.static void
5260: 20 66 69 6c 65 64 5f 67 65 6e 65 72 61 74 65 5f filed_generate_
5270: 65 74 61 67 28 63 68 61 72 20 2a 65 74 61 67 2c etag(char *etag,
5280: 20 73 69 7a 65 5f 74 20 6c 65 6e 67 74 68 29 20 size_t length)
5290: 7b 0a 09 73 6e 70 72 69 6e 74 66 28 65 74 61 67 {..snprintf(etag
52a0: 2c 20 6c 65 6e 67 74 68 2c 20 22 25 6c 6c 78 2d , length, "%llx-
52b0: 25 6c 6c 78 25 6c 6c 78 25 6c 6c 78 25 6c 6c 78 %llx%llx%llx%llx
52c0: 22 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c ",...(unsigned l
52d0: 6f 6e 67 20 6c 6f 6e 67 29 20 74 69 6d 65 28 4e ong long) time(N
52e0: 55 4c 4c 29 2c 0a 09 09 28 75 6e 73 69 67 6e 65 ULL),...(unsigne
52f0: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e d long long) ran
5300: 64 6f 6d 28 29 2c 0a 09 09 28 75 6e 73 69 67 6e dom(),...(unsign
5310: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 ed long long) ra
5320: 6e 64 6f 6d 28 29 2c 0a 09 09 28 75 6e 73 69 67 ndom(),...(unsig
5330: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 ned long long) r
5340: 61 6e 64 6f 6d 28 29 2c 0a 09 09 28 75 6e 73 69 andom(),...(unsi
5350: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 gned long long)
5360: 72 61 6e 64 6f 6d 28 29 0a 09 29 3b 0a 7d 0a 0a random()..);.}..
5370: 23 69 66 64 65 66 20 46 49 4c 45 44 5f 46 41 4b #ifdef FILED_FAK
5380: 45 5f 43 48 52 4f 4f 54 0a 2f 2a 20 54 72 61 6e E_CHROOT./* Tran
5390: 73 6c 61 74 65 20 61 20 70 61 74 68 20 69 6e 74 slate a path int
53a0: 6f 20 61 20 66 61 6b 65 20 63 68 72 6f 6f 74 20 o a fake chroot
53b0: 70 61 74 68 20 2a 2f 0a 73 74 61 74 69 63 20 63 path */.static c
53c0: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 onst char *filed
53d0: 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 65 28 _path_translate(
53e0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 const char *path
53f0: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f , struct filed_o
5400: 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 ptions *options)
5410: 20 7b 0a 09 73 74 61 74 69 63 20 5f 5f 74 68 72 {..static __thr
5420: 65 61 64 20 63 68 61 72 20 70 61 74 68 42 75 66 ead char pathBuf
5430: 66 65 72 5b 38 31 39 32 5d 3b 0a 09 69 6e 74 20 fer[8192];..int
5440: 73 6e 70 72 69 6e 74 66 5f 72 65 74 3b 0a 0a 09 snprintf_ret;...
5450: 2f 2a 20 49 66 20 6e 6f 20 61 6c 74 65 72 6e 61 /* If no alterna
5460: 74 69 76 65 20 72 6f 6f 74 20 69 73 20 73 70 65 tive root is spe
5470: 63 69 66 69 65 64 2c 20 72 65 74 75 72 6e 20 74 cified, return t
5480: 68 65 20 75 6e 61 64 6f 72 6e 65 64 20 70 61 74 he unadorned pat
5490: 68 20 2a 2f 0a 09 69 66 20 28 21 6f 70 74 69 6f h */..if (!optio
54a0: 6e 73 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f 74 ns->fake_newroot
54b0: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 70 61 74 ) {...return(pat
54c0: 68 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 56 65 72 69 h);..}.../* Veri
54d0: 66 79 20 74 68 61 74 20 74 68 69 73 20 72 65 71 fy that this req
54e0: 75 65 73 74 20 77 69 6c 6c 20 6e 6f 74 20 67 6f uest will not go
54f0: 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 outside of the
5500: 73 70 65 63 69 66 69 65 64 20 72 6f 6f 74 20 2a specified root *
5510: 2f 0a 09 69 66 20 28 73 74 72 73 74 72 28 70 61 /..if (strstr(pa
5520: 74 68 2c 20 22 2f 2e 2e 2f 22 29 20 21 3d 20 4e th, "/../") != N
5530: 55 4c 4c 20 7c 7c 20 70 61 74 68 5b 30 5d 20 21 ULL || path[0] !
5540: 3d 20 27 2f 27 29 20 7b 0a 09 09 66 69 6c 65 64 = '/') {...filed
5550: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 _log_msg_debug("
5560: 55 6e 61 62 6c 65 20 74 6f 20 74 72 61 6e 73 6c Unable to transl
5570: 61 74 65 20 70 61 74 68 20 5c 22 25 73 5c 22 2c ate path \"%s\",
5580: 20 63 6f 6e 74 61 69 6e 73 20 69 6e 76 61 6c 69 contains invali
5590: 64 20 63 68 61 72 61 63 74 65 72 73 22 2c 20 70 d characters", p
55a0: 61 74 68 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 ath);....return(
55b0: 6f 70 74 69 6f 6e 73 2d 3e 66 61 6b 65 5f 6e 65 options->fake_ne
55c0: 77 72 6f 6f 74 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 wroot);..}.../*
55d0: 43 72 65 61 74 65 20 74 68 65 20 6e 65 77 20 70 Create the new p
55e0: 61 74 68 20 69 6e 74 6f 20 6f 75 72 20 6c 6f 63 ath into our loc
55f0: 61 6c 20 28 54 4c 53 29 20 73 74 61 74 69 63 20 al (TLS) static
5600: 62 75 66 66 65 72 20 2a 2f 0a 09 73 6e 70 72 69 buffer */..snpri
5610: 6e 74 66 5f 72 65 74 20 3d 20 73 6e 70 72 69 6e ntf_ret = snprin
5620: 74 66 28 70 61 74 68 42 75 66 66 65 72 2c 20 73 tf(pathBuffer, s
5630: 69 7a 65 6f 66 28 70 61 74 68 42 75 66 66 65 72 izeof(pathBuffer
5640: 29 2c 20 22 25 73 2f 25 73 22 2c 20 6f 70 74 69 ), "%s/%s", opti
5650: 6f 6e 73 2d 3e 66 61 6b 65 5f 6e 65 77 72 6f 6f ons->fake_newroo
5660: 74 2c 20 70 61 74 68 29 3b 0a 09 69 66 20 28 73 t, path);..if (s
5670: 6e 70 72 69 6e 74 66 5f 72 65 74 20 3c 20 30 20 nprintf_ret < 0
5680: 7c 7c 20 28 28 75 6e 73 69 67 6e 65 64 20 69 6e || ((unsigned in
5690: 74 29 20 73 6e 70 72 69 6e 74 66 5f 72 65 74 29 t) snprintf_ret)
56a0: 20 3e 3d 20 73 69 7a 65 6f 66 28 70 61 74 68 42 >= sizeof(pathB
56b0: 75 66 66 65 72 29 29 20 7b 0a 09 09 66 69 6c 65 uffer)) {...file
56c0: 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 d_log_msg_debug(
56d0: 22 55 6e 61 62 6c 65 20 74 6f 20 74 72 61 6e 73 "Unable to trans
56e0: 6c 61 74 65 20 70 61 74 68 20 5c 22 25 73 5c 22 late path \"%s\"
56f0: 2c 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 , will not fit i
5700: 6e 74 6f 20 6e 65 77 20 62 75 66 66 65 72 22 2c nto new buffer",
5710: 20 70 61 74 68 29 3b 0a 0a 09 09 72 65 74 75 72 path);....retur
5720: 6e 28 6f 70 74 69 6f 6e 73 2d 3e 66 61 6b 65 5f n(options->fake_
5730: 6e 65 77 72 6f 6f 74 29 3b 0a 09 7d 0a 0a 09 66 newroot);..}...f
5740: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
5750: 75 67 28 22 54 72 61 6e 73 6c 61 74 69 6e 67 20 ug("Translating
5760: 70 61 74 68 20 5c 22 25 73 5c 22 20 69 6e 74 6f path \"%s\" into
5770: 20 5c 22 25 73 5c 22 22 2c 20 70 61 74 68 2c 20 \"%s\"", path,
5780: 70 61 74 68 42 75 66 66 65 72 29 3b 0a 0a 09 2f pathBuffer);.../
5790: 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 65 77 * Return the new
57a0: 20 70 61 74 68 20 2a 2f 0a 09 72 65 74 75 72 6e path */..return
57b0: 28 70 61 74 68 42 75 66 66 65 72 29 3b 0a 7d 0a (pathBuffer);.}.
57c0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c .static void fil
57d0: 65 64 5f 70 61 74 68 5f 74 72 61 6e 73 6c 61 74 ed_path_translat
57e0: 65 5f 73 65 74 5f 72 6f 6f 74 28 63 6f 6e 73 74 e_set_root(const
57f0: 20 63 68 61 72 20 2a 76 61 72 2c 20 73 74 72 75 char *var, stru
5800: 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 ct filed_options
5810: 20 2a 6f 70 74 69 6f 6e 73 2c 20 63 6f 6e 73 74 *options, const
5820: 20 63 68 61 72 20 2a 76 61 6c 29 20 7b 0a 09 6f char *val) {..o
5830: 70 74 69 6f 6e 73 2d 3e 66 61 6b 65 5f 6e 65 77 ptions->fake_new
5840: 72 6f 6f 74 20 3d 20 73 74 72 64 75 70 28 76 61 root = strdup(va
5850: 6c 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 0a 09 l);...return;...
5860: 2f 2a 20 76 61 72 20 69 73 20 6f 6e 6c 79 20 75 /* var is only u
5870: 73 65 64 20 69 6e 20 74 68 65 20 6d 61 63 72 6f sed in the macro
5880: 20 2d 2d 20 64 69 73 63 61 72 64 20 69 74 20 68 -- discard it h
5890: 65 72 65 20 2a 2f 0a 09 76 61 72 20 3d 20 76 61 ere */..var = va
58a0: 72 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 r;.}.#else.#defi
58b0: 6e 65 20 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 ne filed_path_tr
58c0: 61 6e 73 6c 61 74 65 28 70 61 74 68 2c 20 6f 70 anslate(path, op
58d0: 74 69 6f 6e 73 29 20 70 61 74 68 0a 23 64 65 66 tions) path.#def
58e0: 69 6e 65 20 66 69 6c 65 64 5f 70 61 74 68 5f 74 ine filed_path_t
58f0: 72 61 6e 73 6c 61 74 65 5f 73 65 74 5f 72 6f 6f ranslate_set_roo
5900: 74 28 76 61 72 2c 20 6f 70 74 69 6f 6e 73 2c 20 t(var, options,
5910: 76 61 6c 29 20 76 61 72 20 3d 20 73 74 72 64 75 val) var = strdu
5920: 70 28 76 61 6c 29 0a 23 65 6e 64 69 66 0a 0a 2f p(val).#endif../
5930: 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 20 61 6e * Open a file an
5940: 64 20 72 65 74 75 72 6e 20 66 69 6c 65 20 69 6e d return file in
5950: 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 formation */.sta
5960: 74 69 63 20 73 74 72 75 63 74 20 66 69 6c 65 64 tic struct filed
5970: 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 64 _fileinfo *filed
5980: 5f 6f 70 65 6e 5f 66 69 6c 65 28 63 6f 6e 73 74 _open_file(const
5990: 20 63 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 char *path, str
59a0: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e uct filed_filein
59b0: 66 6f 20 2a 62 75 66 66 65 72 2c 20 73 74 72 75 fo *buffer, stru
59c0: 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 ct filed_options
59d0: 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 *options) {..st
59e0: 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 ruct filed_filei
59f0: 6e 66 6f 20 2a 63 61 63 68 65 3b 0a 09 75 6e 73 nfo *cache;..uns
5a00: 69 67 6e 65 64 20 69 6e 74 20 63 61 63 68 65 5f igned int cache_
5a10: 69 64 78 3b 0a 09 6f 66 66 5f 74 20 6c 65 6e 3b idx;..off_t len;
5a20: 0a 09 69 6e 74 20 66 64 3b 0a 0a 09 69 66 20 28 ..int fd;...if (
5a30: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 filed_fileinfo_f
5a40: 64 63 61 63 68 65 5f 73 69 7a 65 20 21 3d 20 30 dcache_size != 0
5a50: 29 20 7b 0a 09 09 63 61 63 68 65 5f 69 64 78 20 ) {...cache_idx
5a60: 3d 20 66 69 6c 65 64 5f 68 61 73 68 28 28 63 6f = filed_hash((co
5a70: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
5a80: 72 20 2a 29 20 70 61 74 68 2c 20 66 69 6c 65 64 r *) path, filed
5a90: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 _fileinfo_fdcach
5aa0: 65 5f 73 69 7a 65 29 3b 0a 0a 09 09 63 61 63 68 e_size);....cach
5ab0: 65 20 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 e = &filed_filei
5ac0: 6e 66 6f 5f 66 64 63 61 63 68 65 5b 63 61 63 68 nfo_fdcache[cach
5ad0: 65 5f 69 64 78 5d 3b 0a 0a 09 09 66 69 6c 65 64 e_idx];....filed
5ae0: 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 _log_msg_debug("
5af0: 4c 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f Locking mutex fo
5b00: 72 20 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e r idx: %lu", (un
5b10: 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 signed long) cac
5b20: 68 65 5f 69 64 78 29 3b 0a 0a 09 09 70 74 68 72 he_idx);....pthr
5b30: 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 ead_mutex_lock(&
5b40: 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a cache->mutex);..
5b50: 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f ..filed_log_msg_
5b60: 64 65 62 75 67 28 22 43 6f 6d 70 6c 65 74 65 64 debug("Completed
5b70: 20 6c 6f 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 locking mutex f
5b80: 6f 72 20 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 or idx: %lu", (u
5b90: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 nsigned long) ca
5ba0: 63 68 65 5f 69 64 78 29 3b 0a 09 7d 20 65 6c 73 che_idx);..} els
5bb0: 65 20 7b 0a 09 09 63 61 63 68 65 5f 69 64 78 20 e {...cache_idx
5bc0: 3d 20 30 3b 0a 09 09 63 61 63 68 65 20 3d 20 62 = 0;...cache = b
5bd0: 75 66 66 65 72 3b 0a 09 09 63 61 63 68 65 2d 3e uffer;...cache->
5be0: 70 61 74 68 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a path[0] = '\0';.
5bf0: 09 09 63 61 63 68 65 2d 3e 66 64 20 3d 20 2d 31 ..cache->fd = -1
5c00: 3b 0a 09 7d 0a 0a 09 69 66 20 28 73 74 72 63 6d ;..}...if (strcm
5c10: 70 28 70 61 74 68 2c 20 63 61 63 68 65 2d 3e 70 p(path, cache->p
5c20: 61 74 68 29 20 21 3d 20 30 29 20 7b 0a 09 09 66 ath) != 0) {...f
5c30: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
5c40: 75 67 28 22 43 61 63 68 65 20 6d 69 73 73 20 66 ug("Cache miss f
5c50: 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20 4f 4c 44 or idx: %lu: OLD
5c60: 20 5c 22 25 73 5c 22 2c 20 4e 45 57 20 5c 22 25 \"%s\", NEW \"%
5c70: 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 s\"", (unsigned
5c80: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 2c long) cache_idx,
5c90: 20 63 61 63 68 65 2d 3e 70 61 74 68 2c 20 70 61 cache->path, pa
5ca0: 74 68 29 3b 0a 0a 09 09 66 64 20 3d 20 6f 70 65 th);....fd = ope
5cb0: 6e 28 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 61 n(filed_path_tra
5cc0: 6e 73 6c 61 74 65 28 70 61 74 68 2c 20 6f 70 74 nslate(path, opt
5cd0: 69 6f 6e 73 29 2c 20 4f 5f 52 44 4f 4e 4c 59 20 ions), O_RDONLY
5ce0: 7c 20 4f 5f 4c 41 52 47 45 46 49 4c 45 29 3b 0a | O_LARGEFILE);.
5cf0: 09 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a ..if (fd < 0) {.
5d00: 09 09 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c ...if (filed_fil
5d10: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 einfo_fdcache_si
5d20: 7a 65 20 21 3d 20 30 29 20 7b 0a 09 09 09 09 70 ze != 0) {.....p
5d30: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c thread_mutex_unl
5d40: 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 ock(&cache->mute
5d50: 78 29 3b 0a 09 09 09 7d 0a 0a 09 09 09 72 65 74 x);....}.....ret
5d60: 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a urn(NULL);...}..
5d70: 09 09 69 66 20 28 63 61 63 68 65 2d 3e 66 64 20 ..if (cache->fd
5d80: 3e 3d 20 30 29 20 7b 0a 09 09 09 63 6c 6f 73 65 >= 0) {....close
5d90: 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 09 7d (cache->fd);...}
5da0: 0a 0a 09 09 6c 65 6e 20 3d 20 6c 73 65 65 6b 28 ....len = lseek(
5db0: 66 64 2c 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29 fd, 0, SEEK_END)
5dc0: 3b 0a 09 09 6c 73 65 65 6b 28 66 64 2c 20 30 2c ;...lseek(fd, 0,
5dd0: 20 53 45 45 4b 5f 53 45 54 29 3b 0a 0a 09 09 63 SEEK_SET);....c
5de0: 61 63 68 65 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 ache->fd = fd;..
5df0: 09 63 61 63 68 65 2d 3e 6c 65 6e 20 3d 20 6c 65 .cache->len = le
5e00: 6e 3b 0a 09 09 73 74 72 63 70 79 28 63 61 63 68 n;...strcpy(cach
5e10: 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 3b 0a e->path, path);.
5e20: 09 09 63 61 63 68 65 2d 3e 74 79 70 65 20 3d 20 ..cache->type =
5e30: 66 69 6c 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f filed_determine_
5e40: 6d 69 6d 65 74 79 70 65 28 70 61 74 68 29 3b 0a mimetype(path);.
5e50: 09 09 66 69 6c 65 64 5f 67 65 6e 65 72 61 74 65 ..filed_generate
5e60: 5f 65 74 61 67 28 63 61 63 68 65 2d 3e 65 74 61 _etag(cache->eta
5e70: 67 2c 20 73 69 7a 65 6f 66 28 63 61 63 68 65 2d g, sizeof(cache-
5e80: 3e 65 74 61 67 29 29 3b 0a 0a 09 09 2f 2a 20 58 >etag));..../* X
5e90: 58 58 3a 54 4f 44 4f 3a 20 44 65 74 65 72 6d 69 XX:TODO: Determi
5ea0: 6e 65 20 2a 2f 0a 09 09 63 61 63 68 65 2d 3e 6c ne */...cache->l
5eb0: 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 64 5f 66 astmod = filed_f
5ec0: 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 63 68 65 ormat_time(cache
5ed0: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a ->lastmod_b, siz
5ee0: 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d eof(cache->lastm
5ef0: 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c od_b), time(NULL
5f00: 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 6c 73 65 ) - 30);..} else
5f10: 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d {...filed_log_m
5f20: 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65 20 sg_debug("Cache
5f30: 68 69 74 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 hit for idx: %lu
5f40: 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 22 2c 20 : PATH \"%s\"",
5f50: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 (unsigned long)
5f60: 63 61 63 68 65 5f 69 64 78 2c 20 70 61 74 68 29 cache_idx, path)
5f70: 3b 0a 09 7d 0a 0a 09 69 66 20 28 66 69 6c 65 64 ;..}...if (filed
5f80: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 _fileinfo_fdcach
5f90: 65 5f 73 69 7a 65 20 21 3d 20 30 29 20 7b 0a 09 e_size != 0) {..
5fa0: 09 2f 2a 0a 09 09 20 2a 20 57 65 20 68 61 76 65 ./*... * We have
5fb0: 20 74 6f 20 6d 61 6b 65 20 61 20 64 75 70 6c 69 to make a dupli
5fc0: 63 61 74 65 20 46 44 2c 20 62 65 63 61 75 73 65 cate FD, because
5fd0: 20 6f 6e 63 65 20 77 65 20 72 65 6c 65 61 73 65 once we release
5fe0: 20 74 68 65 20 63 61 63 68 65 0a 09 09 20 2a 20 the cache... *
5ff0: 6d 75 74 65 78 2c 20 74 68 65 20 66 69 6c 65 20 mutex, the file
6000: 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79 20 62 descriptor may b
6010: 65 20 63 6c 6f 73 65 64 0a 09 09 20 2a 2f 0a 09 e closed... */..
6020: 09 66 64 20 3d 20 64 75 70 28 63 61 63 68 65 2d .fd = dup(cache-
6030: 3e 66 64 29 3b 0a 09 09 69 66 20 28 66 64 20 3c >fd);...if (fd <
6040: 20 30 29 20 7b 0a 09 09 09 70 74 68 72 65 61 64 0) {....pthread
6050: 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 _mutex_unlock(&c
6060: 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 ache->mutex);...
6070: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a ..return(NULL);.
6080: 09 09 7d 0a 0a 09 09 62 75 66 66 65 72 2d 3e 66 ..}....buffer->f
6090: 64 20 3d 20 66 64 3b 0a 09 09 62 75 66 66 65 72 d = fd;...buffer
60a0: 2d 3e 6c 65 6e 20 3d 20 63 61 63 68 65 2d 3e 6c ->len = cache->l
60b0: 65 6e 3b 0a 09 09 62 75 66 66 65 72 2d 3e 74 79 en;...buffer->ty
60c0: 70 65 20 3d 20 63 61 63 68 65 2d 3e 74 79 70 65 pe = cache->type
60d0: 3b 0a 09 09 6d 65 6d 63 70 79 28 62 75 66 66 65 ;...memcpy(buffe
60e0: 72 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 63 61 r->lastmod_b, ca
60f0: 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 che->lastmod_b,
6100: 73 69 7a 65 6f 66 28 62 75 66 66 65 72 2d 3e 6c sizeof(buffer->l
6110: 61 73 74 6d 6f 64 5f 62 29 29 3b 0a 09 09 6d 65 astmod_b));...me
6120: 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 65 74 61 mcpy(buffer->eta
6130: 67 2c 20 63 61 63 68 65 2d 3e 65 74 61 67 2c 20 g, cache->etag,
6140: 73 69 7a 65 6f 66 28 62 75 66 66 65 72 2d 3e 65 sizeof(buffer->e
6150: 74 61 67 29 29 3b 0a 09 09 62 75 66 66 65 72 2d tag));...buffer-
6160: 3e 6c 61 73 74 6d 6f 64 20 3d 20 62 75 66 66 65 >lastmod = buffe
6170: 72 2d 3e 6c 61 73 74 6d 6f 64 5f 62 20 2b 20 28 r->lastmod_b + (
6180: 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 2d cache->lastmod -
6190: 20 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 5f cache->lastmod_
61a0: 62 29 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f 6d b);....pthread_m
61b0: 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 utex_unlock(&cac
61c0: 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 09 7d 0a 0a he->mutex);..}..
61d0: 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 29 3b .return(buffer);
61e0: 0a 0a 09 2f 2a 20 6f 70 74 69 6f 6e 73 20 69 73 .../* options is
61f0: 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 66 61 only used if fa
6200: 6b 65 20 63 68 72 6f 6f 74 20 69 73 20 65 6e 61 ke chroot is ena
6210: 62 6c 65 64 2c 20 63 6f 6e 66 75 73 65 20 74 68 bled, confuse th
6220: 65 20 63 6f 6d 70 69 6c 65 72 20 2a 2f 0a 09 6f e compiler */..o
6230: 70 74 69 6f 6e 73 20 3d 20 6f 70 74 69 6f 6e 73 ptions = options
6240: 3b 0a 7d 0a 0a 2f 2a 20 50 72 6f 63 65 73 73 20 ;.}../* Process
6250: 61 6e 20 48 54 54 50 20 72 65 71 75 65 73 74 20 an HTTP request
6260: 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 70 and return the p
6270: 61 74 68 20 72 65 71 75 65 73 74 65 64 20 2a 2f ath requested */
6280: 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 66 .static struct f
6290: 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 iled_http_reques
62a0: 74 20 2a 66 69 6c 65 64 5f 67 65 74 5f 68 74 74 t *filed_get_htt
62b0: 70 5f 72 65 71 75 65 73 74 28 46 49 4c 45 20 2a p_request(FILE *
62c0: 66 70 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 fp, struct filed
62d0: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 2a 62 _http_request *b
62e0: 75 66 66 65 72 5f 73 74 2c 20 73 74 72 75 63 74 uffer_st, struct
62f0: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a filed_options *
6300: 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 63 68 61 72 options) {..char
6310: 20 2a 6d 65 74 68 6f 64 2c 20 2a 70 61 74 68 3b *method, *path;
6320: 0a 09 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 ..char *buffer,
6330: 2a 77 6f 72 6b 62 75 66 66 65 72 2c 20 2a 77 6f *workbuffer, *wo
6340: 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a 09 rkbuffer_next;..
6350: 63 68 61 72 20 2a 66 67 65 74 73 5f 72 65 74 3b char *fgets_ret;
6360: 0a 09 73 69 7a 65 5f 74 20 62 75 66 66 65 72 5f ..size_t buffer_
6370: 6c 65 6e 2c 20 70 61 74 68 5f 6c 65 6e 3b 0a 09 len, path_len;..
6380: 6f 66 66 5f 74 20 72 61 6e 67 65 5f 73 74 61 72 off_t range_star
6390: 74 2c 20 72 61 6e 67 65 5f 65 6e 64 2c 20 72 61 t, range_end, ra
63a0: 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 09 69 6e 74 nge_length;..int
63b0: 20 72 61 6e 67 65 5f 72 65 71 75 65 73 74 3b 0a range_request;.
63c0: 09 69 6e 74 20 73 6e 70 72 69 6e 74 66 5f 72 65 .int snprintf_re
63d0: 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 2f 2a 20 t;..int i;.../*
63e0: 53 65 74 20 74 6f 20 64 65 66 61 75 6c 74 20 76 Set to default v
63f0: 61 6c 75 65 73 20 2a 2f 0a 09 72 61 6e 67 65 5f alues */..range_
6400: 73 74 61 72 74 20 3d 20 30 3b 0a 09 72 61 6e 67 start = 0;..rang
6410: 65 5f 65 6e 64 20 20 20 3d 20 30 3b 0a 09 72 61 e_end = 0;..ra
6420: 6e 67 65 5f 72 65 71 75 65 73 74 20 3d 20 30 3b nge_request = 0;
6430: 0a 09 72 61 6e 67 65 5f 6c 65 6e 67 74 68 20 3d ..range_length =
6440: 20 2d 31 3b 0a 09 62 75 66 66 65 72 5f 73 74 2d -1;..buffer_st-
6450: 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 >headers.host.pr
6460: 65 73 65 6e 74 20 3d 20 30 3b 0a 09 62 75 66 66 esent = 0;..buff
6470: 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 63 er_st->headers.c
6480: 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 46 49 4c 45 onnection = FILE
6490: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f D_CONNECTION_CLO
64a0: 53 45 3b 0a 0a 09 62 75 66 66 65 72 20 3d 20 62 SE;...buffer = b
64b0: 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 uffer_st->tmpbuf
64c0: 3b 0a 09 62 75 66 66 65 72 5f 6c 65 6e 20 3d 20 ;..buffer_len =
64d0: 73 69 7a 65 6f 66 28 62 75 66 66 65 72 5f 73 74 sizeof(buffer_st
64e0: 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 66 67 65 ->tmpbuf);...fge
64f0: 74 73 5f 72 65 74 20 3d 20 66 67 65 74 73 28 62 ts_ret = fgets(b
6500: 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 uffer, buffer_le
6510: 6e 2c 20 66 70 29 3b 0a 09 69 66 20 28 66 67 65 n, fp);..if (fge
6520: 74 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 ts_ret == NULL)
6530: 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 {...return(NULL)
6540: 3b 0a 09 7d 0a 0a 09 6d 65 74 68 6f 64 20 3d 20 ;..}...method =
6550: 62 75 66 66 65 72 3b 0a 0a 09 62 75 66 66 65 72 buffer;...buffer
6560: 20 3d 20 73 74 72 63 68 72 28 62 75 66 66 65 72 = strchr(buffer
6570: 2c 20 27 20 27 29 3b 0a 09 69 66 20 28 62 75 66 , ' ');..if (buf
6580: 66 65 72 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 fer == NULL) {..
6590: 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 .return(NULL);..
65a0: 7d 0a 0a 09 2a 62 75 66 66 65 72 20 3d 20 27 5c }...*buffer = '\
65b0: 30 27 3b 0a 09 62 75 66 66 65 72 2b 2b 3b 0a 0a 0';..buffer++;..
65c0: 09 70 61 74 68 20 3d 20 62 75 66 66 65 72 3b 0a .path = buffer;.
65d0: 0a 09 2f 2a 20 54 65 72 6d 69 6e 61 74 65 20 70 ../* Terminate p
65e0: 61 74 68 20 63 6f 6d 70 6f 6e 65 6e 74 20 2a 2f ath component */
65f0: 0a 09 62 75 66 66 65 72 20 3d 20 73 74 72 70 62 ..buffer = strpb
6600: 72 6b 28 70 61 74 68 2c 20 22 5c 72 5c 6e 20 22 rk(path, "\r\n "
6610: 29 3b 0a 09 69 66 20 28 62 75 66 66 65 72 20 21 );..if (buffer !
6620: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 62 75 66 = NULL) {...*buf
6630: 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 62 75 fer = '\0';...bu
6640: 66 66 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 2f 2a 20 ffer++;..}.../*
6650: 57 65 20 6f 6e 6c 79 20 68 61 6e 64 6c 65 20 74 We only handle t
6660: 68 65 20 22 47 45 54 22 20 61 6e 64 20 22 48 45 he "GET" and "HE
6670: 41 44 27 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 09 AD' methods */..
6680: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 28 6d if (strcasecmp(m
6690: 65 74 68 6f 64 2c 20 22 68 65 61 64 22 29 20 21 ethod, "head") !
66a0: 3d 20 30 29 20 7b 0a 09 09 69 66 20 28 73 74 72 = 0) {...if (str
66b0: 63 61 73 65 63 6d 70 28 6d 65 74 68 6f 64 2c 20 casecmp(method,
66c0: 22 67 65 74 22 29 20 21 3d 20 30 29 20 7b 0a 09 "get") != 0) {..
66d0: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a ..return(NULL);.
66e0: 09 09 7d 0a 0a 09 09 2f 2a 20 47 45 54 20 72 65 ..}..../* GET re
66f0: 71 75 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 quest */...buffe
6700: 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 r_st->method = F
6710: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 ILED_REQUEST_MET
6720: 48 4f 44 5f 47 45 54 3b 0a 09 7d 20 65 6c 73 65 HOD_GET;..} else
6730: 20 7b 0a 09 09 2f 2a 20 48 45 41 44 20 72 65 71 {.../* HEAD req
6740: 75 65 73 74 20 2a 2f 0a 09 09 62 75 66 66 65 72 uest */...buffer
6750: 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 20 46 49 _st->method = FI
6760: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 LED_REQUEST_METH
6770: 4f 44 5f 48 45 41 44 3b 0a 09 7d 0a 0a 09 2f 2a OD_HEAD;..}.../*
6780: 20 4e 6f 74 65 20 70 61 74 68 20 2a 2f 0a 09 70 Note path */..p
6790: 61 74 68 5f 6c 65 6e 20 3d 20 73 74 72 6c 65 6e ath_len = strlen
67a0: 28 70 61 74 68 29 3b 0a 09 6d 65 6d 63 70 79 28 (path);..memcpy(
67b0: 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c buffer_st->path,
67c0: 20 70 61 74 68 2c 20 70 61 74 68 5f 6c 65 6e 20 path, path_len
67d0: 2b 20 31 29 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 + 1);.../* Deter
67e0: 6d 69 6e 65 20 74 79 70 65 20 6f 66 20 72 65 71 mine type of req
67f0: 75 65 73 74 20 66 72 6f 6d 20 70 61 74 68 20 2a uest from path *
6800: 2f 0a 09 69 66 20 28 70 61 74 68 5f 6c 65 6e 20 /..if (path_len
6810: 3d 3d 20 30 29 20 7b 0a 09 09 62 75 66 66 65 72 == 0) {...buffer
6820: 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 _st->type = FILE
6830: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 D_REQUEST_TYPE_D
6840: 49 52 45 43 54 4f 52 59 3b 0a 09 7d 20 65 6c 73 IRECTORY;..} els
6850: 65 20 7b 0a 09 09 69 66 20 28 70 61 74 68 5b 70 e {...if (path[p
6860: 61 74 68 5f 6c 65 6e 20 2d 20 31 5d 20 3d 3d 20 ath_len - 1] ==
6870: 27 2f 27 29 20 7b 0a 09 09 09 62 75 66 66 65 72 '/') {....buffer
6880: 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 _st->type = FILE
6890: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 D_REQUEST_TYPE_D
68a0: 49 52 45 43 54 4f 52 59 3b 0a 09 09 7d 20 65 6c IRECTORY;...} el
68b0: 73 65 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 se {....buffer_s
68c0: 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f t->type = FILED_
68d0: 52 45 51 55 45 53 54 5f 54 59 50 45 5f 4f 54 48 REQUEST_TYPE_OTH
68e0: 45 52 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 ER;...}..}.../*
68f0: 52 65 73 65 74 20 62 75 66 66 65 72 20 66 6f 72 Reset buffer for
6900: 20 6c 61 74 65 72 20 75 73 65 20 2a 2f 0a 09 62 later use */..b
6910: 75 66 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 uffer = buffer_s
6920: 74 2d 3e 74 6d 70 62 75 66 3b 0a 0a 09 66 6f 72 t->tmpbuf;...for
6930: 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 31 30 30 (i = 0; i < 100
6940: 3b 20 69 2b 2b 29 20 7b 0a 09 09 66 67 65 74 73 ; i++) {...fgets
6950: 5f 72 65 74 20 3d 20 66 67 65 74 73 28 62 75 66 _ret = fgets(buf
6960: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c fer, buffer_len,
6970: 20 66 70 29 3b 0a 09 09 69 66 20 28 66 67 65 74 fp);...if (fget
6980: 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b s_ret == NULL) {
6990: 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 0a ....break;...}..
69a0: 09 09 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d ..if (strncasecm
69b0: 70 28 62 75 66 66 65 72 2c 20 22 52 61 6e 67 65 p(buffer, "Range
69c0: 3a 20 22 2c 20 37 29 20 3d 3d 20 30 29 20 7b 0a : ", 7) == 0) {.
69d0: 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 ...workbuffer =
69e0: 62 75 66 66 65 72 20 2b 20 37 3b 0a 0a 09 09 09 buffer + 7;.....
69f0: 69 66 20 28 73 74 72 6e 63 61 73 65 63 6d 70 28 if (strncasecmp(
6a00: 77 6f 72 6b 62 75 66 66 65 72 2c 20 22 62 79 74 workbuffer, "byt
6a10: 65 73 3d 22 2c 20 36 29 20 3d 3d 20 30 29 20 7b es=", 6) == 0) {
6a20: 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 .....workbuffer
6a30: 2b 3d 20 36 3b 0a 0a 09 09 09 09 72 61 6e 67 65 += 6;......range
6a40: 5f 72 65 71 75 65 73 74 20 3d 20 31 3b 0a 0a 09 _request = 1;...
6a50: 09 09 09 72 61 6e 67 65 5f 73 74 61 72 74 20 3d ...range_start =
6a60: 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 strtoull(workbu
6a70: 66 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 ffer, &workbuffe
6a80: 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 0a 09 09 r_next, 10);....
6a90: 09 09 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 77 ..workbuffer = w
6aa0: 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 3b 0a orkbuffer_next;.
6ab0: 0a 09 09 09 09 69 66 20 28 2a 77 6f 72 6b 62 75 .....if (*workbu
6ac0: 66 66 65 72 20 3d 3d 20 27 2d 27 29 20 7b 0a 09 ffer == '-') {..
6ad0: 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b ....workbuffer++
6ae0: 3b 0a 0a 09 09 09 09 09 69 66 20 28 2a 77 6f 72 ;.......if (*wor
6af0: 6b 62 75 66 66 65 72 20 21 3d 20 27 5c 72 27 20 kbuffer != '\r'
6b00: 26 26 20 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 && *workbuffer !
6b10: 3d 20 27 5c 6e 27 29 20 7b 0a 09 09 09 09 09 09 = '\n') {.......
6b20: 72 61 6e 67 65 5f 65 6e 64 20 3d 20 73 74 72 74 range_end = strt
6b30: 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66 65 72 2c oull(workbuffer,
6b40: 20 26 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 &workbuffer_nex
6b50: 74 2c 20 31 30 29 3b 0a 09 09 09 09 09 7d 0a 09 t, 10);......}..
6b60: 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 20 65 6c ...}....}...} el
6b70: 73 65 20 69 66 20 28 73 74 72 6e 63 61 73 65 63 se if (strncasec
6b80: 6d 70 28 62 75 66 66 65 72 2c 20 22 48 6f 73 74 mp(buffer, "Host
6b90: 3a 20 22 2c 20 35 29 20 3d 3d 20 30 29 20 7b 0a : ", 5) == 0) {.
6ba0: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 ...buffer_st->he
6bb0: 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65 aders.host.prese
6bc0: 6e 74 20 3d 20 31 3b 0a 0a 09 09 09 77 6f 72 6b nt = 1;.....work
6bd0: 62 75 66 66 65 72 20 3d 20 73 74 72 70 62 72 6b buffer = strpbrk
6be0: 28 62 75 66 66 65 72 20 2b 20 35 2c 20 22 5c 72 (buffer + 5, "\r
6bf0: 5c 6e 3a 22 29 3b 0a 09 09 09 69 66 20 28 77 6f \n:");....if (wo
6c00: 72 6b 62 75 66 66 65 72 20 21 3d 20 4e 55 4c 4c rkbuffer != NULL
6c10: 29 20 7b 0a 09 09 09 09 2a 77 6f 72 6b 62 75 66 ) {.....*workbuf
6c20: 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 09 09 7d fer = '\0';....}
6c30: 0a 0a 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 .....workbuffer
6c40: 3d 20 62 75 66 66 65 72 20 2b 20 35 3b 0a 09 09 = buffer + 5;...
6c50: 09 77 68 69 6c 65 20 28 2a 77 6f 72 6b 62 75 66 .while (*workbuf
6c60: 66 65 72 20 3d 3d 20 27 20 27 29 20 7b 0a 09 09 fer == ' ') {...
6c70: 09 09 77 6f 72 6b 62 75 66 66 65 72 2b 2b 3b 0a ..workbuffer++;.
6c80: 09 09 09 7d 0a 0a 09 09 09 73 74 72 63 70 79 28 ...}.....strcpy(
6c90: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
6ca0: 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c 20 77 6f rs.host.host, wo
6cb0: 72 6b 62 75 66 66 65 72 29 3b 0a 09 09 7d 20 65 rkbuffer);...} e
6cc0: 6c 73 65 20 69 66 20 28 73 74 72 6e 63 61 73 65 lse if (strncase
6cd0: 63 6d 70 28 62 75 66 66 65 72 2c 20 22 43 6f 6e cmp(buffer, "Con
6ce0: 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c nection: Keep-Al
6cf0: 69 76 65 22 2c 20 32 32 29 20 3d 3d 20 30 29 20 ive", 22) == 0)
6d00: 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e {....buffer_st->
6d10: 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 headers.connecti
6d20: 6f 6e 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 on = FILED_CONNE
6d30: 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 CTION_KEEP_ALIVE
6d40: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 6d 65 6d ;...}....if (mem
6d50: 63 6d 70 28 62 75 66 66 65 72 2c 20 22 5c 72 5c cmp(buffer, "\r\
6d60: 6e 22 2c 20 32 29 20 3d 3d 20 30 29 20 7b 0a 09 n", 2) == 0) {..
6d70: 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a ..break;...}..}.
6d80: 0a 09 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 72 ../* Determine r
6d90: 61 6e 67 65 20 2a 2f 0a 09 69 66 20 28 72 61 6e ange */..if (ran
6da0: 67 65 5f 65 6e 64 20 21 3d 20 30 29 20 7b 0a 09 ge_end != 0) {..
6db0: 09 69 66 20 28 72 61 6e 67 65 5f 65 6e 64 20 3c .if (range_end <
6dc0: 3d 20 72 61 6e 67 65 5f 73 74 61 72 74 29 20 7b = range_start) {
6dd0: 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 ....return(NULL)
6de0: 3b 0a 09 09 7d 0a 0a 09 09 72 61 6e 67 65 5f 6c ;...}....range_l
6df0: 65 6e 67 74 68 20 3d 20 72 61 6e 67 65 5f 65 6e ength = range_en
6e00: 64 20 2d 20 72 61 6e 67 65 5f 73 74 61 72 74 3b d - range_start;
6e10: 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 ....filed_log_ms
6e20: 67 5f 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 g_debug("Computi
6e30: 6e 67 20 6c 65 6e 67 74 68 20 70 61 72 61 6d 65 ng length parame
6e40: 74 65 72 3a 20 25 6c 6c 75 20 3d 20 25 6c 6c 75 ter: %llu = %llu
6e50: 20 2d 20 25 6c 6c 75 22 2c 0a 09 09 09 28 75 6e - %llu",....(un
6e60: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
6e70: 29 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 2c 0a ) range_length,.
6e80: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
6e90: 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 65 6e g long) range_en
6ea0: 64 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 d,....(unsigned
6eb0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 long long) range
6ec0: 5f 73 74 61 72 74 0a 09 09 29 3b 0a 09 7d 0a 0a _start...);..}..
6ed0: 09 2f 2a 20 46 69 6c 6c 20 75 70 20 73 74 72 75 ./* Fill up stru
6ee0: 63 74 75 72 65 20 74 6f 20 72 65 74 75 72 6e 20 cture to return
6ef0: 2a 2f 0a 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 */..buffer_st->h
6f00: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 eaders.range.pre
6f10: 73 65 6e 74 20 3d 20 72 61 6e 67 65 5f 72 65 71 sent = range_req
6f20: 75 65 73 74 3b 0a 09 62 75 66 66 65 72 5f 73 74 uest;..buffer_st
6f30: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
6f40: 6f 66 66 73 65 74 20 20 3d 20 72 61 6e 67 65 5f offset = range_
6f50: 73 74 61 72 74 3b 0a 09 62 75 66 66 65 72 5f 73 start;..buffer_s
6f60: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
6f70: 2e 6c 65 6e 67 74 68 20 20 3d 20 72 61 6e 67 65 .length = range
6f80: 5f 6c 65 6e 67 74 68 3b 0a 0a 09 2f 2a 20 49 66 _length;.../* If
6f90: 20 76 68 6f 73 74 73 20 61 72 65 20 65 6e 61 62 vhosts are enab
6fa0: 6c 65 64 2c 20 63 6f 6d 70 75 74 65 20 6e 65 77 led, compute new
6fb0: 20 70 61 74 68 20 2a 2f 0a 09 69 66 20 28 6f 70 path */..if (op
6fc0: 74 69 6f 6e 73 2d 3e 76 68 6f 73 74 73 5f 65 6e tions->vhosts_en
6fd0: 61 62 6c 65 64 29 20 7b 0a 09 09 69 66 20 28 62 abled) {...if (b
6fe0: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 uffer_st->header
6ff0: 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e 74 20 3d s.host.present =
7000: 3d 20 31 29 20 7b 0a 09 09 09 62 75 66 66 65 72 = 1) {....buffer
7010: 20 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d = buffer_st->tm
7020: 70 62 75 66 3b 0a 09 09 09 62 75 66 66 65 72 5f pbuf;....buffer_
7030: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 len = sizeof(buf
7040: 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b fer_st->tmpbuf);
7050: 0a 0a 09 09 09 73 6e 70 72 69 6e 74 66 5f 72 65 .....snprintf_re
7060: 74 20 3d 20 73 6e 70 72 69 6e 74 66 28 62 75 66 t = snprintf(buf
7070: 66 65 72 2c 20 62 75 66 66 65 72 5f 6c 65 6e 2c fer, buffer_len,
7080: 20 22 2f 25 73 25 73 25 73 22 2c 0a 09 09 09 09 "/%s%s%s",.....
7090: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 buffer_st->heade
70a0: 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c 0a 09 09 rs.host.host,...
70b0: 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 ..buffer_st->pat
70c0: 68 5b 30 5d 20 3d 3d 20 27 2f 27 20 3f 20 22 22 h[0] == '/' ? ""
70d0: 20 3a 20 22 2f 22 2c 0a 09 09 09 09 62 75 66 66 : "/",.....buff
70e0: 65 72 5f 73 74 2d 3e 70 61 74 68 0a 09 09 09 29 er_st->path....)
70f0: 3b 0a 09 09 09 69 66 20 28 73 6e 70 72 69 6e 74 ;....if (snprint
7100: 66 5f 72 65 74 20 3e 3d 20 30 29 20 7b 0a 09 09 f_ret >= 0) {...
7110: 09 09 69 66 20 28 28 28 75 6e 73 69 67 6e 65 64 ..if (((unsigned
7120: 20 69 6e 74 29 20 73 6e 70 72 69 6e 74 66 5f 72 int) snprintf_r
7130: 65 74 29 20 3c 20 62 75 66 66 65 72 5f 6c 65 6e et) < buffer_len
7140: 29 20 7b 0a 09 09 09 09 09 73 74 72 63 70 79 28 ) {......strcpy(
7150: 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 2c buffer_st->path,
7160: 20 62 75 66 66 65 72 29 3b 0a 09 09 09 09 7d 0a buffer);.....}.
7170: 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 ...}...}..}...re
7180: 74 75 72 6e 28 62 75 66 66 65 72 5f 73 74 29 3b turn(buffer_st);
7190: 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 6e .}../* Return an
71a0: 20 65 72 72 6f 72 20 70 61 67 65 20 2a 2f 0a 73 error page */.s
71b0: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 tatic void filed
71c0: 5f 65 72 72 6f 72 5f 70 61 67 65 28 46 49 4c 45 _error_page(FILE
71d0: 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 *fp, const char
71e0: 20 2a 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 *date_current,
71f0: 69 6e 74 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 int error_number
7200: 2c 20 69 6e 74 20 6d 65 74 68 6f 64 2c 20 63 6f , int method, co
7210: 6e 73 74 20 63 68 61 72 20 2a 72 65 61 73 6f 6e nst char *reason
7220: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c , struct filed_l
7230: 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 29 20 7b og_entry *log) {
7240: 0a 09 63 68 61 72 20 2a 65 72 72 6f 72 5f 73 74 ..char *error_st
7250: 72 69 6e 67 20 3d 20 22 3c 68 74 6d 6c 3e 3c 68 ring = "<html><h
7260: 65 61 64 3e 3c 74 69 74 6c 65 3e 45 52 52 4f 52 ead><title>ERROR
7270: 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 3e 3c </title></head><
7280: 62 6f 64 79 3e 55 6e 61 62 6c 65 20 74 6f 20 70 body>Unable to p
7290: 72 6f 63 65 73 73 20 72 65 71 75 65 73 74 3c 2f rocess request</
72a0: 62 6f 64 79 3e 3c 2f 68 74 6d 6c 3e 22 3b 0a 0a body></html>";..
72b0: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54 .fprintf(fp, "HT
72c0: 54 50 2f 31 2e 31 20 25 69 20 4e 6f 74 20 4f 4b TP/1.1 %i Not OK
72d0: 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e \r\nDate: %s\r\n
72e0: 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c Server: filed\r\
72f0: 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 nLast-Modified:
7300: 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 %s\r\nContent-Le
7310: 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 43 6f ngth: %llu\r\nCo
7320: 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c 72 ntent-Type: %s\r
7330: 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c \nConnection: cl
7340: 6f 73 65 5c 72 5c 6e 5c 72 5c 6e 22 2c 0a 09 09 ose\r\n\r\n",...
7350: 65 72 72 6f 72 5f 6e 75 6d 62 65 72 2c 0a 09 09 error_number,...
7360: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 date_current,...
7370: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 date_current,...
7380: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
7390: 6f 6e 67 29 20 73 74 72 6c 65 6e 28 65 72 72 6f ong) strlen(erro
73a0: 72 5f 73 74 72 69 6e 67 29 2c 0a 09 09 22 74 65 r_string),..."te
73b0: 78 74 2f 68 74 6d 6c 22 0a 09 29 3b 0a 0a 09 2f xt/html"..);.../
73c0: 2a 20 73 69 6c 65 6e 63 65 20 65 72 72 6f 72 20 * silence error
73d0: 73 74 72 69 6e 67 20 66 6f 72 20 48 45 41 44 20 string for HEAD
73e0: 72 65 71 75 65 73 74 73 20 2a 2f 0a 09 69 66 20 requests */..if
73f0: 28 6d 65 74 68 6f 64 20 21 3d 20 46 49 4c 45 44 (method != FILED
7400: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f _REQUEST_METHOD_
7410: 48 45 41 44 29 20 7b 0a 09 09 66 70 72 69 6e 74 HEAD) {...fprint
7420: 66 28 66 70 2c 20 22 25 73 22 2c 20 65 72 72 6f f(fp, "%s", erro
7430: 72 5f 73 74 72 69 6e 67 29 3b 0a 09 7d 0a 0a 09 r_string);..}...
7440: 2f 2a 20 4c 6f 67 20 65 72 72 6f 72 20 2a 2f 0a /* Log error */.
7450: 09 2f 2a 2a 20 72 65 61 73 6f 6e 20 6d 75 73 74 ./** reason must
7460: 20 70 6f 69 6e 74 20 74 6f 20 61 20 67 6c 6f 62 point to a glob
7470: 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 76 ally allocated v
7480: 61 6c 75 65 20 2a 2a 2f 0a 09 6c 6f 67 2d 3e 72 alue **/..log->r
7490: 65 61 73 6f 6e 20 3d 20 72 65 61 73 6f 6e 3b 0a eason = reason;.
74a0: 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 .log->http_code
74b0: 3d 20 65 72 72 6f 72 5f 6e 75 6d 62 65 72 3b 0a = error_number;.
74c0: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 ..filed_log_entr
74d0: 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f y(log);.../* Clo
74e0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
74f0: 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 ..filed_socketti
7500: 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 69 6c 65 meout_close(file
7510: 6e 6f 28 66 70 29 2c 20 30 29 3b 0a 0a 09 66 63 no(fp), 0);...fc
7520: 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 74 75 lose(fp);...retu
7530: 72 6e 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e rn;.}../* Return
7540: 20 61 20 72 65 64 69 72 65 63 74 20 74 6f 20 69 a redirect to i
7550: 6e 64 65 78 2e 68 74 6d 6c 20 2a 2f 0a 23 69 66 ndex.html */.#if
7560: 6e 64 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f ndef FILED_DONT_
7570: 52 45 44 49 52 45 43 54 5f 44 49 52 45 43 54 4f REDIRECT_DIRECTO
7580: 52 49 45 53 0a 73 74 61 74 69 63 20 76 6f 69 64 RIES.static void
7590: 20 66 69 6c 65 64 5f 72 65 64 69 72 65 63 74 5f filed_redirect_
75a0: 69 6e 64 65 78 28 46 49 4c 45 20 2a 66 70 2c 20 index(FILE *fp,
75b0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 65 const char *date
75c0: 5f 63 75 72 72 65 6e 74 2c 20 63 6f 6e 73 74 20 _current, const
75d0: 63 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 75 char *path, stru
75e0: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ct filed_log_ent
75f0: 72 79 20 2a 6c 6f 67 29 20 7b 0a 09 69 6e 74 20 ry *log) {..int
7600: 68 74 74 70 5f 63 6f 64 65 20 3d 20 33 30 32 3b http_code = 302;
7610: 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48 ..fprintf(fp, "H
7620: 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c 72 5c TTP/1.1 %i OK\r\
7630: 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72 nDate: %s\r\nSer
7640: 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 ver: filed\r\nLa
7650: 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c st-Modified: %s\
7660: 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 r\nContent-Lengt
7670: 68 3a 20 30 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 h: 0\r\nConnecti
7680: 6f 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 4c 6f 63 on: close\r\nLoc
7690: 61 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e 5c 72 5c ation: %s\r\n\r\
76a0: 6e 22 2c 0a 09 09 68 74 74 70 5f 63 6f 64 65 2c n",...http_code,
76b0: 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c ...date_current,
76c0: 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c ...date_current,
76d0: 0a 09 09 22 69 6e 64 65 78 2e 68 74 6d 6c 22 0a ..."index.html".
76e0: 09 29 3b 0a 0a 09 2f 2a 20 4c 6f 67 20 72 65 64 .);.../* Log red
76f0: 69 72 65 63 74 20 2a 2f 0a 09 6c 6f 67 2d 3e 72 irect */..log->r
7700: 65 61 73 6f 6e 20 3d 20 22 72 65 64 69 72 65 63 eason = "redirec
7710: 74 22 3b 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 t";..log->http_c
7720: 6f 64 65 20 3d 20 68 74 74 70 5f 63 6f 64 65 3b ode = http_code;
7730: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ...filed_log_ent
7740: 72 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c ry(log);.../* Cl
7750: 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ose connection *
7760: 2f 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 /..filed_sockett
7770: 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 69 6c imeout_close(fil
7780: 65 6e 6f 28 66 70 29 2c 20 30 29 3b 0a 0a 09 66 eno(fp), 0);...f
7790: 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 72 65 74 close(fp);...ret
77a0: 75 72 6e 3b 0a 0a 09 2f 2a 20 43 75 72 72 65 6e urn;.../* Curren
77b0: 74 6c 79 20 75 6e 75 73 65 64 3a 20 70 61 74 68 tly unused: path
77c0: 20 2a 2f 0a 09 70 61 74 68 20 3d 20 70 61 74 68 */..path = path
77d0: 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 43 ;.}.#endif../* C
77e0: 6f 6e 76 65 72 74 20 61 6e 20 65 6e 75 6d 20 72 onvert an enum r
77f0: 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
7800: 22 43 6f 6e 6e 65 63 74 69 6f 6e 22 20 68 65 61 "Connection" hea
7810: 64 65 72 20 76 61 6c 75 65 20 74 6f 20 61 20 73 der value to a s
7820: 74 72 69 6e 67 20 2a 2f 0a 73 74 61 74 69 63 20 tring */.static
7830: 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 const char *file
7840: 64 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 d_connection_str
7850: 28 69 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f (int connection_
7860: 76 61 6c 75 65 29 20 7b 0a 09 73 77 69 74 63 68 value) {..switch
7870: 20 28 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c (connection_val
7880: 75 65 29 20 7b 0a 09 09 63 61 73 65 20 46 49 4c ue) {...case FIL
7890: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c ED_CONNECTION_CL
78a0: 4f 53 45 3a 0a 09 09 09 72 65 74 75 72 6e 28 22 OSE:....return("
78b0: 63 6c 6f 73 65 22 29 3b 0a 09 09 63 61 73 65 20 close");...case
78c0: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e FILED_CONNECTION
78d0: 5f 4b 45 45 50 5f 41 4c 49 56 45 3a 0a 09 09 09 _KEEP_ALIVE:....
78e0: 72 65 74 75 72 6e 28 22 6b 65 65 70 2d 61 6c 69 return("keep-ali
78f0: 76 65 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 ve");..}...retur
7900: 6e 28 22 63 6c 6f 73 65 22 29 3b 0a 7d 0a 0a 2f n("close");.}../
7910: 2a 20 48 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c * Handle a singl
7920: 65 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 61 e request from a
7930: 20 63 6c 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 client */.stati
7940: 63 20 69 6e 74 20 66 69 6c 65 64 5f 68 61 6e 64 c int filed_hand
7950: 6c 65 5f 63 6c 69 65 6e 74 28 69 6e 74 20 66 64 le_client(int fd
7960: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 68 , struct filed_h
7970: 74 74 70 5f 72 65 71 75 65 73 74 20 2a 72 65 71 ttp_request *req
7980: 75 65 73 74 2c 20 73 74 72 75 63 74 20 66 69 6c uest, struct fil
7990: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f ed_log_entry *lo
79a0: 67 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f g, struct filed_
79b0: 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 options *options
79c0: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 ) {..struct file
79d0: 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 d_fileinfo *file
79e0: 69 6e 66 6f 3b 0a 09 73 73 69 7a 65 5f 74 20 73 info;..ssize_t s
79f0: 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a 09 73 69 endfile_ret;..si
7a00: 7a 65 5f 74 20 73 65 6e 64 66 69 6c 65 5f 73 69 ze_t sendfile_si
7a10: 7a 65 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 64 66 ze;..off_t sendf
7a20: 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 ile_offset, send
7a30: 66 69 6c 65 5f 73 65 6e 74 2c 20 73 65 6e 64 66 file_sent, sendf
7a40: 69 6c 65 5f 6c 65 6e 3b 0a 09 63 68 61 72 20 2a ile_len;..char *
7a50: 70 61 74 68 3b 0a 09 63 68 61 72 20 2a 64 61 74 path;..char *dat
7a60: 65 5f 63 75 72 72 65 6e 74 2c 20 64 61 74 65 5f e_current, date_
7a70: 63 75 72 72 65 6e 74 5f 62 5b 36 34 5d 3b 0a 09 current_b[64];..
7a80: 69 6e 74 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 int http_code;..
7a90: 46 49 4c 45 20 2a 66 70 3b 0a 0a 09 2f 2a 20 49 FILE *fp;.../* I
7aa0: 6e 64 69 63 61 74 65 20 74 68 65 20 63 6f 6e 6e ndicate the conn
7ab0: 65 63 74 69 6f 6e 20 73 74 61 72 74 20 74 69 6d ection start tim
7ac0: 65 20 2a 2f 0a 09 6c 6f 67 2d 3e 63 6f 6e 6e 65 e */..log->conne
7ad0: 63 74 74 69 6d 65 20 3d 20 74 69 6d 65 28 4e 55 cttime = time(NU
7ae0: 4c 4c 29 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 6d LL);.../* Determ
7af0: 69 6e 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 ine current time
7b00: 20 2a 2f 0a 09 64 61 74 65 5f 63 75 72 72 65 6e */..date_curren
7b10: 74 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d 61 74 t = filed_format
7b20: 5f 74 69 6d 65 28 64 61 74 65 5f 63 75 72 72 65 _time(date_curre
7b30: 6e 74 5f 62 2c 20 73 69 7a 65 6f 66 28 64 61 74 nt_b, sizeof(dat
7b40: 65 5f 63 75 72 72 65 6e 74 5f 62 29 2c 20 74 69 e_current_b), ti
7b50: 6d 65 28 4e 55 4c 4c 29 29 3b 0a 0a 09 2f 2a 20 me(NULL));.../*
7b60: 4f 70 65 6e 20 73 6f 63 6b 65 74 20 61 73 20 41 Open socket as A
7b70: 4e 53 49 20 49 2f 4f 20 66 6f 72 20 65 61 73 65 NSI I/O for ease
7b80: 20 6f 66 20 75 73 65 20 2a 2f 0a 09 66 70 20 3d of use */..fp =
7b90: 20 66 64 6f 70 65 6e 28 66 64 2c 20 22 77 2b 62 fdopen(fd, "w+b
7ba0: 22 29 3b 0a 09 69 66 20 28 66 70 20 3d 3d 20 4e ");..if (fp == N
7bb0: 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 73 ULL) {...filed_s
7bc0: 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f 63 6c 6f ockettimeout_clo
7bd0: 73 65 28 66 64 2c 20 30 29 3b 0a 0a 09 09 63 6c se(fd, 0);....cl
7be0: 6f 73 65 28 66 64 29 3b 0a 0a 09 09 6c 6f 67 2d ose(fd);....log-
7bf0: 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 >buffer[0] = '\0
7c00: 27 3b 0a 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 ';...log->http_c
7c10: 6f 64 65 20 3d 20 2d 31 3b 0a 09 09 6c 6f 67 2d ode = -1;...log-
7c20: 3e 72 65 61 73 6f 6e 20 3d 20 22 66 64 6f 70 65 >reason = "fdope
7c30: 6e 5f 66 61 69 6c 65 64 22 3b 0a 0a 09 09 66 69 n_failed";....fi
7c40: 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f led_log_entry(lo
7c50: 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 g);....return(FI
7c60: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 LED_CONNECTION_C
7c70: 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 72 65 71 75 LOSE);..}...requ
7c80: 65 73 74 20 3d 20 66 69 6c 65 64 5f 67 65 74 5f est = filed_get_
7c90: 68 74 74 70 5f 72 65 71 75 65 73 74 28 66 70 2c http_request(fp,
7ca0: 20 72 65 71 75 65 73 74 2c 20 6f 70 74 69 6f 6e request, option
7cb0: 73 29 3b 0a 0a 09 69 66 20 28 72 65 71 75 65 73 s);...if (reques
7cc0: 74 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 6c t == NULL) {...l
7cd0: 6f 67 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 og->buffer[0] =
7ce0: 27 5c 30 27 3b 0a 0a 09 09 66 69 6c 65 64 5f 65 '\0';....filed_e
7cf0: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 rror_page(fp, da
7d00: 74 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 30 2c te_current, 500,
7d10: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d FILED_REQUEST_M
7d20: 45 54 48 4f 44 5f 47 45 54 2c 20 22 66 6f 72 6d ETHOD_GET, "form
7d30: 61 74 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 at", log);....re
7d40: 74 75 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 turn(FILED_CONNE
7d50: 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d CTION_CLOSE);..}
7d60: 0a 0a 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ...filed_sockett
7d70: 69 6d 65 6f 75 74 5f 70 72 6f 63 65 73 73 69 6e imeout_processin
7d80: 67 5f 73 74 61 72 74 28 66 64 29 3b 0a 0a 09 70 g_start(fd);...p
7d90: 61 74 68 20 3d 20 72 65 71 75 65 73 74 2d 3e 70 ath = request->p
7da0: 61 74 68 3b 0a 09 73 74 72 63 70 79 28 6c 6f 67 ath;..strcpy(log
7db0: 2d 3e 62 75 66 66 65 72 2c 20 70 61 74 68 29 3b ->buffer, path);
7dc0: 0a 09 6c 6f 67 2d 3e 6d 65 74 68 6f 64 20 3d 20 ..log->method =
7dd0: 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 3b request->method;
7de0: 0a 0a 09 2f 2a 20 49 66 20 74 68 65 20 72 65 71 .../* If the req
7df0: 75 65 73 74 65 64 20 70 61 74 68 20 69 73 20 61 uested path is a
7e00: 20 64 69 72 65 63 74 6f 72 79 2c 20 72 65 64 69 directory, redi
7e10: 72 65 63 74 20 74 6f 20 69 6e 64 65 78 20 70 61 rect to index pa
7e20: 67 65 20 2a 2f 0a 09 69 66 20 28 72 65 71 75 65 ge */..if (reque
7e30: 73 74 2d 3e 74 79 70 65 20 3d 3d 20 46 49 4c 45 st->type == FILE
7e40: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 D_REQUEST_TYPE_D
7e50: 49 52 45 43 54 4f 52 59 29 20 7b 0a 23 69 66 64 IRECTORY) {.#ifd
7e60: 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 52 45 ef FILED_DONT_RE
7e70: 44 49 52 45 43 54 5f 44 49 52 45 43 54 4f 52 49 DIRECT_DIRECTORI
7e80: 45 53 0a 09 09 63 68 61 72 20 6c 6f 63 61 6c 70 ES...char localp
7e90: 61 74 68 5b 38 31 39 32 5d 3b 0a 09 09 69 6e 74 ath[8192];...int
7ea0: 20 73 6e 70 72 69 6e 74 66 5f 72 65 74 3b 0a 0a snprintf_ret;..
7eb0: 09 09 73 6e 70 72 69 6e 74 66 5f 72 65 74 20 3d ..snprintf_ret =
7ec0: 20 73 6e 70 72 69 6e 74 66 28 6c 6f 63 61 6c 70 snprintf(localp
7ed0: 61 74 68 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 61 ath, sizeof(loca
7ee0: 6c 70 61 74 68 29 2c 20 22 25 73 2f 69 6e 64 65 lpath), "%s/inde
7ef0: 78 2e 68 74 6d 6c 22 2c 20 70 61 74 68 29 3b 0a x.html", path);.
7f00: 0a 09 09 69 66 20 28 73 6e 70 72 69 6e 74 66 5f ...if (snprintf_
7f10: 72 65 74 20 3c 3d 20 30 20 7c 7c 20 73 6e 70 72 ret <= 0 || snpr
7f20: 69 6e 74 66 5f 72 65 74 20 3e 20 28 73 69 7a 65 intf_ret > (size
7f30: 6f 66 28 6c 6f 63 61 6c 70 61 74 68 29 20 2d 20 of(localpath) -
7f40: 31 29 29 20 7b 0a 09 09 09 66 69 6c 65 64 5f 65 1)) {....filed_e
7f50: 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 rror_page(fp, da
7f60: 74 65 5f 63 75 72 72 65 6e 74 2c 20 35 30 30 2c te_current, 500,
7f70: 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 request->method
7f80: 2c 20 22 70 61 74 68 5f 66 6f 72 6d 61 74 22 2c , "path_format",
7f90: 20 6c 6f 67 29 3b 0a 0a 09 09 09 72 65 74 75 72 log);.....retur
7fa0: 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 n(FILED_CONNECTI
7fb0: 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 09 7d 0a 0a ON_CLOSE);...}..
7fc0: 09 09 70 61 74 68 20 3d 20 6c 6f 63 61 6c 70 61 ..path = localpa
7fd0: 74 68 3b 0a 23 65 6c 73 65 0a 09 09 66 69 6c 65 th;.#else...file
7fe0: 64 5f 72 65 64 69 72 65 63 74 5f 69 6e 64 65 78 d_redirect_index
7ff0: 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e (fp, date_curren
8000: 74 2c 20 70 61 74 68 2c 20 6c 6f 67 29 3b 0a 0a t, path, log);..
8010: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 ..return(FILED_C
8020: 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 ONNECTION_CLOSE)
8030: 3b 0a 23 65 6e 64 69 66 0a 09 7d 0a 0a 09 66 69 ;.#endif..}...fi
8040: 6c 65 69 6e 66 6f 20 3d 20 66 69 6c 65 64 5f 6f leinfo = filed_o
8050: 70 65 6e 5f 66 69 6c 65 28 70 61 74 68 2c 20 26 pen_file(path, &
8060: 72 65 71 75 65 73 74 2d 3e 66 69 6c 65 69 6e 66 request->fileinf
8070: 6f 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a 09 69 66 o, options);..if
8080: 20 28 66 69 6c 65 69 6e 66 6f 20 3d 3d 20 4e 55 (fileinfo == NU
8090: 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 5f 65 72 LL) {...filed_er
80a0: 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 ror_page(fp, dat
80b0: 65 5f 63 75 72 72 65 6e 74 2c 20 34 30 34 2c 20 e_current, 404,
80c0: 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c request->method,
80d0: 20 22 6f 70 65 6e 5f 66 61 69 6c 65 64 22 2c 20 "open_failed",
80e0: 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 log);....return(
80f0: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e FILED_CONNECTION
8100: 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 69 66 _CLOSE);..}...if
8110: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 (request->heade
8120: 72 73 2e 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 rs.range.present
8130: 29 20 7b 0a 09 09 69 66 20 28 72 65 71 75 65 73 ) {...if (reques
8140: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
8150: 2e 6f 66 66 73 65 74 20 21 3d 20 30 20 7c 7c 20 .offset != 0 ||
8160: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
8170: 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3e 3d .range.length >=
8180: 20 30 29 20 7b 0a 09 09 09 69 66 20 28 72 65 71 0) {....if (req
8190: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 uest->headers.ra
81a0: 6e 67 65 2e 6f 66 66 73 65 74 20 3e 3d 20 66 69 nge.offset >= fi
81b0: 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 29 20 7b 0a 09 leinfo->len) {..
81c0: 09 09 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 ...filed_error_p
81d0: 61 67 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 age(fp, date_cur
81e0: 72 65 6e 74 2c 20 34 31 36 2c 20 72 65 71 75 65 rent, 416, reque
81f0: 73 74 2d 3e 6d 65 74 68 6f 64 2c 20 22 72 61 6e st->method, "ran
8200: 67 65 5f 69 6e 76 61 6c 69 64 22 2c 20 6c 6f 67 ge_invalid", log
8210: 29 3b 0a 0a 09 09 09 09 63 6c 6f 73 65 28 66 69 );......close(fi
8220: 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 09 leinfo->fd);....
8230: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 43 ..return(FILED_C
8240: 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 29 ONNECTION_CLOSE)
8250: 3b 0a 09 09 09 7d 0a 0a 09 09 09 69 66 20 28 72 ;....}.....if (r
8260: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
8270: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 3d 20 range.length ==
8280: 28 28 6f 66 66 5f 74 29 20 2d 31 29 29 20 7b 0a ((off_t) -1)) {.
8290: 09 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 ....filed_log_ms
82a0: 67 5f 64 65 62 75 67 28 22 43 6f 6d 70 75 74 69 g_debug("Computi
82b0: 6e 67 20 6c 65 6e 67 74 68 20 74 6f 20 66 69 74 ng length to fit
82c0: 20 69 6e 20 62 6f 75 6e 64 73 3a 20 66 69 6c 65 in bounds: file
82d0: 69 6e 66 6f 2d 3e 6c 65 6e 20 3d 20 25 6c 6c 75 info->len = %llu
82e0: 2c 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 , request->heade
82f0: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 rs.range.offset
8300: 3d 20 25 6c 6c 75 22 2c 0a 09 09 09 09 09 28 75 = %llu",......(u
8310: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
8320: 67 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e g) fileinfo->len
8330: 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 ,......(unsigned
8340: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 long long) requ
8350: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e est->headers.ran
8360: 67 65 2e 6f 66 66 73 65 74 0a 09 09 09 09 29 3b ge.offset.....);
8370: 0a 0a 09 09 09 09 72 65 71 75 65 73 74 2d 3e 68 ......request->h
8380: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e eaders.range.len
8390: 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e gth = fileinfo->
83a0: 6c 65 6e 20 2d 20 72 65 71 75 65 73 74 2d 3e 68 len - request->h
83b0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
83c0: 73 65 74 3b 0a 09 09 09 7d 0a 0a 09 09 09 66 69 set;....}.....fi
83d0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 led_log_msg_debu
83e0: 67 28 22 50 61 72 74 69 61 6c 20 72 65 71 75 65 g("Partial reque
83f0: 73 74 2c 20 73 74 61 72 74 69 6e 67 20 61 74 3a st, starting at:
8400: 20 25 6c 6c 75 20 61 6e 64 20 72 75 6e 6e 69 6e %llu and runnin
8410: 67 20 66 6f 72 20 25 6c 6c 69 20 62 79 74 65 73 g for %lli bytes
8420: 22 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 64 ",.....(unsigned
8430: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 long long) requ
8440: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e est->headers.ran
8450: 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 09 28 ge.offset,.....(
8460: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 long long) reque
8470: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
8480: 65 2e 6c 65 6e 67 74 68 0a 09 09 09 29 3b 0a 0a e.length....);..
8490: 09 09 7d 0a 0a 09 09 68 74 74 70 5f 63 6f 64 65 ..}....http_code
84a0: 20 3d 20 32 30 36 3b 0a 09 7d 20 65 6c 73 65 20 = 206;..} else
84b0: 7b 0a 09 09 68 74 74 70 5f 63 6f 64 65 20 3d 20 {...http_code =
84c0: 32 30 30 3b 0a 0a 09 09 2f 2a 20 43 6f 6d 70 75 200;..../* Compu
84d0: 74 65 20 66 61 6b 65 20 72 61 6e 67 65 20 70 61 te fake range pa
84e0: 72 61 6d 65 74 65 72 73 20 74 68 61 74 20 69 6e rameters that in
84f0: 63 6c 75 64 65 73 20 74 68 65 20 65 6e 74 69 72 cludes the entir
8500: 65 20 66 69 6c 65 20 2a 2f 0a 09 09 72 65 71 75 e file */...requ
8510: 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e est->headers.ran
8520: 67 65 2e 6f 66 66 73 65 74 20 3d 20 30 3b 0a 09 ge.offset = 0;..
8530: 09 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 .request->header
8540: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d s.range.length =
8550: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a fileinfo->len;.
8560: 09 7d 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 2c .}...fprintf(fp,
8570: 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b "HTTP/1.1 %i OK
8580: 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e \r\nDate: %s\r\n
8590: 53 65 72 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c Server: filed\r\
85a0: 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 nLast-Modified:
85b0: 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 %s\r\nContent-Le
85c0: 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c 6e 41 63 ngth: %llu\r\nAc
85d0: 63 65 70 74 2d 52 61 6e 67 65 73 3a 20 62 79 74 cept-Ranges: byt
85e0: 65 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 es\r\nContent-Ty
85f0: 70 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 pe: %s\r\nConnec
8600: 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e 45 54 61 67 tion: %s\r\nETag
8610: 3a 20 5c 22 25 73 5c 22 5c 72 5c 6e 22 2c 0a 09 : \"%s\"\r\n",..
8620: 09 68 74 74 70 5f 63 6f 64 65 2c 0a 09 09 64 61 .http_code,...da
8630: 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09 09 66 69 te_current,...fi
8640: 6c 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d 6f 64 2c leinfo->lastmod,
8650: 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
8660: 67 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d g long) request-
8670: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c >headers.range.l
8680: 65 6e 67 74 68 2c 0a 09 09 66 69 6c 65 69 6e 66 ength,...fileinf
8690: 6f 2d 3e 74 79 70 65 2c 0a 09 09 66 69 6c 65 64 o->type,...filed
86a0: 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 28 _connection_str(
86b0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
86c0: 2e 63 6f 6e 6e 65 63 74 69 6f 6e 29 2c 0a 09 09 .connection),...
86d0: 66 69 6c 65 69 6e 66 6f 2d 3e 65 74 61 67 0a 09 fileinfo->etag..
86e0: 29 3b 0a 0a 09 69 66 20 28 68 74 74 70 5f 63 6f );...if (http_co
86f0: 64 65 20 3d 3d 20 32 30 36 29 20 7b 0a 09 09 66 de == 206) {...f
8700: 70 72 69 6e 74 66 28 66 70 2c 20 22 43 6f 6e 74 printf(fp, "Cont
8710: 65 6e 74 2d 52 61 6e 67 65 3a 20 62 79 74 65 73 ent-Range: bytes
8720: 20 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c 6c 75 5c %llu-%llu/%llu\
8730: 72 5c 6e 22 2c 0a 09 09 09 28 75 6e 73 69 67 6e r\n",....(unsign
8740: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 ed long long) re
8750: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
8760: 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09 09 ange.offset,....
8770: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
8780: 6f 6e 67 29 20 28 72 65 71 75 65 73 74 2d 3e 68 ong) (request->h
8790: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 eaders.range.off
87a0: 73 65 74 20 2b 20 72 65 71 75 65 73 74 2d 3e 68 set + request->h
87b0: 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e eaders.range.len
87c0: 67 74 68 20 2d 20 31 29 2c 0a 09 09 09 28 75 6e gth - 1),....(un
87d0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
87e0: 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 0a ) fileinfo->len.
87f0: 09 09 29 3b 0a 09 7d 0a 09 66 70 72 69 6e 74 66 ..);..}..fprintf
8800: 28 66 70 2c 20 22 5c 72 5c 6e 22 29 3b 0a 09 66 (fp, "\r\n");..f
8810: 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 6c 6f 67 flush(fp);...log
8820: 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 68 74 ->http_code = ht
8830: 74 70 5f 63 6f 64 65 3b 0a 09 6c 6f 67 2d 3e 72 tp_code;..log->r
8840: 65 61 73 6f 6e 20 3d 20 22 4f 4b 22 3b 0a 09 6c eason = "OK";..l
8850: 6f 67 2d 3e 73 74 61 72 74 74 69 6d 65 20 3d 20 og->starttime =
8860: 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 09 6c 6f 67 time(NULL);..log
8870: 2d 3e 72 65 71 5f 6f 66 66 73 65 74 20 3d 20 72 ->req_offset = r
8880: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
8890: 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 6c range.offset;..l
88a0: 6f 67 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20 3d og->req_length =
88b0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
88c0: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a s.range.length;.
88d0: 09 6c 6f 67 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 .log->file_lengt
88e0: 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 h = fileinfo->le
88f0: 6e 3b 0a 0a 23 69 66 64 65 66 20 46 49 4c 45 44 n;..#ifdef FILED
8900: 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 _NONBLOCK_HTTP..
8910: 69 6e 74 20 73 6f 63 6b 65 74 5f 66 6c 61 67 73 int socket_flags
8920: 3b 0a 09 66 64 5f 73 65 74 20 72 66 64 2c 20 77 ;..fd_set rfd, w
8930: 66 64 3b 0a 09 63 68 61 72 20 73 69 6e 6b 62 75 fd;..char sinkbu
8940: 66 5b 38 31 39 32 5d 3b 0a 09 73 73 69 7a 65 5f f[8192];..ssize_
8950: 74 20 72 65 61 64 5f 72 65 74 3b 0a 0a 09 46 44 t read_ret;...FD
8960: 5f 5a 45 52 4f 28 26 72 66 64 29 3b 0a 09 46 44 _ZERO(&rfd);..FD
8970: 5f 5a 45 52 4f 28 26 77 66 64 29 3b 0a 09 46 44 _ZERO(&wfd);..FD
8980: 5f 53 45 54 28 66 64 2c 20 26 72 66 64 29 3b 0a _SET(fd, &rfd);.
8990: 09 46 44 5f 53 45 54 28 66 64 2c 20 26 77 66 64 .FD_SET(fd, &wfd
89a0: 29 3b 0a 0a 09 73 6f 63 6b 65 74 5f 66 6c 61 67 );...socket_flag
89b0: 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f s = fcntl(fd, F_
89c0: 47 45 54 46 4c 29 3b 0a 09 66 63 6e 74 6c 28 66 GETFL);..fcntl(f
89d0: 64 2c 20 46 5f 53 45 54 46 4c 2c 20 73 6f 63 6b d, F_SETFL, sock
89e0: 65 74 5f 66 6c 61 67 73 20 7c 20 4f 5f 4e 4f 4e et_flags | O_NON
89f0: 42 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 0a 09 BLOCK);.#endif..
8a00: 73 65 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 20 sendfile_offset
8a10: 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 = request->heade
8a20: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b rs.range.offset;
8a30: 0a 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d ..sendfile_len =
8a40: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
8a50: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 3b 0a s.range.length;.
8a60: 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 3d .sendfile_sent =
8a70: 20 30 3b 0a 09 77 68 69 6c 65 20 28 72 65 71 75 0;..while (requ
8a80: 65 73 74 2d 3e 6d 65 74 68 6f 64 20 3d 3d 20 46 est->method == F
8a90: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 ILED_REQUEST_MET
8aa0: 48 4f 44 5f 47 45 54 29 20 7b 0a 09 09 69 66 20 HOD_GET) {...if
8ab0: 28 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3e 20 (sendfile_len >
8ac0: 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 5f 4d FILED_SENDFILE_M
8ad0: 41 58 29 20 7b 0a 09 09 09 73 65 6e 64 66 69 6c AX) {....sendfil
8ae0: 65 5f 73 69 7a 65 20 3d 20 46 49 4c 45 44 5f 53 e_size = FILED_S
8af0: 45 4e 44 46 49 4c 45 5f 4d 41 58 3b 0a 09 09 7d ENDFILE_MAX;...}
8b00: 20 65 6c 73 65 20 7b 0a 09 09 09 73 65 6e 64 66 else {....sendf
8b10: 69 6c 65 5f 73 69 7a 65 20 3d 20 73 65 6e 64 66 ile_size = sendf
8b20: 69 6c 65 5f 6c 65 6e 3b 0a 09 09 7d 0a 0a 09 09 ile_len;...}....
8b30: 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20 73 sendfile_ret = s
8b40: 65 6e 64 66 69 6c 65 28 66 64 2c 20 66 69 6c 65 endfile(fd, file
8b50: 69 6e 66 6f 2d 3e 66 64 2c 20 26 73 65 6e 64 66 info->fd, &sendf
8b60: 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 ile_offset, send
8b70: 66 69 6c 65 5f 73 69 7a 65 29 3b 0a 09 09 69 66 file_size);...if
8b80: 20 28 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3c (sendfile_ret <
8b90: 3d 20 30 29 20 7b 0a 23 69 66 64 65 66 20 46 49 = 0) {.#ifdef FI
8ba0: 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54 LED_NONBLOCK_HTT
8bb0: 50 0a 09 09 09 69 66 20 28 65 72 72 6e 6f 20 3d P....if (errno =
8bc0: 3d 20 45 41 47 41 49 4e 29 20 7b 0a 09 09 09 09 = EAGAIN) {.....
8bd0: 73 65 6e 64 66 69 6c 65 5f 72 65 74 20 3d 20 30 sendfile_ret = 0
8be0: 3b 0a 0a 09 09 09 09 77 68 69 6c 65 20 28 31 29 ;......while (1)
8bf0: 20 7b 0a 09 09 09 09 09 73 65 6c 65 63 74 28 66 {......select(f
8c00: 64 20 2b 20 31 2c 20 26 72 66 64 2c 20 26 77 66 d + 1, &rfd, &wf
8c10: 64 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a d, NULL, NULL);.
8c20: 09 09 09 09 09 69 66 20 28 46 44 5f 49 53 53 45 .....if (FD_ISSE
8c30: 54 28 66 64 2c 20 26 72 66 64 29 29 20 7b 0a 09 T(fd, &rfd)) {..
8c40: 09 09 09 09 09 72 65 61 64 5f 72 65 74 20 3d 20 .....read_ret =
8c50: 72 65 61 64 28 66 64 2c 20 73 69 6e 6b 62 75 66 read(fd, sinkbuf
8c60: 2c 20 73 69 7a 65 6f 66 28 73 69 6e 6b 62 75 66 , sizeof(sinkbuf
8c70: 29 29 3b 0a 0a 09 09 09 09 09 09 69 66 20 28 72 ));........if (r
8c80: 65 61 64 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a ead_ret <= 0) {.
8c90: 09 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 .......break;...
8ca0: 09 09 09 09 7d 0a 09 09 09 09 09 7d 0a 0a 09 09 ....}......}....
8cb0: 09 09 09 69 66 20 28 46 44 5f 49 53 53 45 54 28 ...if (FD_ISSET(
8cc0: 66 64 2c 20 26 77 66 64 29 29 20 7b 0a 09 09 09 fd, &wfd)) {....
8cd0: 09 09 09 72 65 61 64 5f 72 65 74 20 3d 20 31 3b ...read_ret = 1;
8ce0: 0a 0a 09 09 09 09 09 09 62 72 65 61 6b 3b 0a 09 ........break;..
8cf0: 09 09 09 09 7d 0a 09 09 09 09 7d 0a 0a 09 09 09 ....}.....}.....
8d00: 09 69 66 20 28 72 65 61 64 5f 72 65 74 20 3c 3d .if (read_ret <=
8d10: 20 30 29 20 7b 0a 09 09 09 09 09 62 72 65 61 6b 0) {......break
8d20: 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 20 65 6c 73 ;.....}....} els
8d30: 65 20 7b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 e {.....break;..
8d40: 09 09 7d 0a 23 65 6c 73 65 0a 09 09 09 62 72 65 ..}.#else....bre
8d50: 61 6b 3b 0a 23 65 6e 64 69 66 0a 09 09 7d 0a 0a ak;.#endif...}..
8d60: 09 09 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 2d ..sendfile_len -
8d70: 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b 0a = sendfile_ret;.
8d80: 09 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 20 ..sendfile_sent
8d90: 2b 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65 74 3b += sendfile_ret;
8da0: 0a 09 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f ...if (sendfile_
8db0: 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 09 62 len == 0) {....b
8dc0: 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 6c reak;...}..}...l
8dd0: 6f 67 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 28 74 og->endtime = (t
8de0: 69 6d 65 5f 74 29 20 2d 31 3b 0a 09 6c 6f 67 2d ime_t) -1;..log-
8df0: 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 20 3d 20 73 >sent_length = s
8e00: 65 6e 64 66 69 6c 65 5f 73 65 6e 74 3b 0a 0a 09 endfile_sent;...
8e10: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 filed_log_entry(
8e20: 6c 6f 67 29 3b 0a 0a 09 63 6c 6f 73 65 28 66 69 log);...close(fi
8e30: 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 69 leinfo->fd);...i
8e40: 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 f (request->head
8e50: 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 21 ers.connection !
8e60: 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 = FILED_CONNECTI
8e70: 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 29 20 7b ON_KEEP_ALIVE) {
8e80: 0a 09 09 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 ...filed_sockett
8e90: 69 6d 65 6f 75 74 5f 63 6c 6f 73 65 28 66 64 2c imeout_close(fd,
8ea0: 20 30 29 3b 0a 0a 09 09 66 63 6c 6f 73 65 28 66 0);....fclose(f
8eb0: 70 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 p);....return(FI
8ec0: 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 LED_CONNECTION_C
8ed0: 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 LOSE);..}...file
8ee0: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
8ef0: 70 72 6f 63 65 73 73 69 6e 67 5f 65 6e 64 28 66 processing_end(f
8f00: 64 29 3b 0a 0a 09 72 65 74 75 72 6e 28 46 49 4c d);...return(FIL
8f10: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 ED_CONNECTION_KE
8f20: 45 50 5f 41 4c 49 56 45 29 3b 0a 7d 0a 0a 2f 2a EP_ALIVE);.}../*
8f30: 20 48 61 6e 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 Handle incoming
8f40: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a connections */.
8f50: 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 69 6c static void *fil
8f60: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 ed_worker_thread
8f70: 28 76 6f 69 64 20 2a 61 72 67 5f 76 29 20 7b 0a (void *arg_v) {.
8f80: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 6f .struct filed_wo
8f90: 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 73 rker_thread_args
8fa0: 20 2a 61 72 67 3b 0a 09 73 74 72 75 63 74 20 66 *arg;..struct f
8fb0: 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 iled_http_reques
8fc0: 74 20 72 65 71 75 65 73 74 3b 0a 09 73 74 72 75 t request;..stru
8fd0: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 ct filed_log_ent
8fe0: 72 79 20 2a 6c 6f 67 2c 20 6c 6f 63 61 6c 5f 64 ry *log, local_d
8ff0: 75 6d 6d 79 5f 6c 6f 67 3b 0a 09 73 74 72 75 63 ummy_log;..struc
9000: 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 t filed_options
9010: 2a 6f 70 74 69 6f 6e 73 3b 0a 09 73 74 72 75 63 *options;..struc
9020: 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 t sockaddr_in6 a
9030: 64 64 72 3b 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 ddr;..socklen_t
9040: 61 64 64 72 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 addrlen;..int fa
9050: 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20 30 2c ilure_count = 0,
9060: 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 max_failure_cou
9070: 6e 74 20 3d 20 46 49 4c 45 44 5f 4d 41 58 5f 46 nt = FILED_MAX_F
9080: 41 49 4c 55 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 AILURE_COUNT;..i
9090: 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 nt connection_st
90a0: 61 74 65 20 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e ate = FILED_CONN
90b0: 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 3b 0a 09 69 ECTION_CLOSE;..i
90c0: 6e 74 20 6d 61 73 74 65 72 5f 66 64 2c 20 66 64 nt master_fd, fd
90d0: 20 3d 20 2d 31 3b 0a 0a 09 2f 2a 20 52 65 61 64 = -1;.../* Read
90e0: 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 61 arguments */..a
90f0: 72 67 20 3d 20 61 72 67 5f 76 3b 0a 0a 09 6d 61 rg = arg_v;...ma
9100: 73 74 65 72 5f 66 64 20 3d 20 61 72 67 2d 3e 66 ster_fd = arg->f
9110: 64 3b 0a 09 6f 70 74 69 6f 6e 73 20 3d 20 26 61 d;..options = &a
9120: 72 67 2d 3e 6f 70 74 69 6f 6e 73 3b 0a 0a 09 77 rg->options;...w
9130: 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 2f 2a 20 hile (1) {.../*
9140: 46 61 69 6c 75 72 65 20 6c 6f 6f 70 20 70 72 65 Failure loop pre
9150: 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 20 vention */...if
9160: 28 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3e (failure_count >
9170: 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f 75 max_failure_cou
9180: 6e 74 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a nt) {....break;.
9190: 09 09 7d 0a 0a 09 09 2f 2a 20 41 6c 6c 6f 63 61 ..}..../* Alloca
91a0: 74 65 20 61 20 6e 65 77 20 6c 6f 67 20 62 75 66 te a new log buf
91b0: 66 65 72 20 2a 2f 0a 09 09 6c 6f 67 20 3d 20 66 fer */...log = f
91c0: 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 31 29 3b iled_log_new(1);
91d0: 0a 09 09 69 66 20 28 6c 6f 67 20 3d 3d 20 4e 55 ...if (log == NU
91e0: 4c 4c 29 20 7b 0a 09 09 09 66 69 6c 65 64 5f 6c LL) {....filed_l
91f0: 6f 67 5f 6d 73 67 28 22 41 4c 4c 4f 43 41 54 45 og_msg("ALLOCATE
9200: 5f 4c 4f 47 5f 4d 53 47 5f 46 41 49 4c 45 44 22 _LOG_MSG_FAILED"
9210: 29 3b 0a 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 );.....break;...
9220: 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 79 70 65 20 3d }....log->type =
9230: 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f FILED_LOG_TYPE_
9240: 54 52 41 4e 53 46 45 52 3b 0a 0a 09 09 2f 2a 20 TRANSFER;..../*
9250: 49 66 20 77 65 20 63 6c 6f 73 65 64 20 74 68 65 If we closed the
9260: 20 6f 6c 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c old connection,
9270: 20 61 63 63 65 70 74 20 61 20 6e 65 77 20 6f 6e accept a new on
9280: 65 20 2a 2f 0a 09 09 69 66 20 28 63 6f 6e 6e 65 e */...if (conne
9290: 63 74 69 6f 6e 5f 73 74 61 74 65 20 3d 3d 20 46 ction_state == F
92a0: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f ILED_CONNECTION_
92b0: 43 4c 4f 53 45 29 20 7b 0a 09 09 09 2f 2a 20 41 CLOSE) {..../* A
92c0: 63 63 65 70 74 20 61 20 6e 65 77 20 63 6c 69 65 ccept a new clie
92d0: 6e 74 20 2a 2f 0a 09 09 09 61 64 64 72 6c 65 6e nt */....addrlen
92e0: 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 29 3b = sizeof(addr);
92f0: 0a 0a 09 09 09 66 64 20 3d 20 61 63 63 65 70 74 .....fd = accept
9300: 28 6d 61 73 74 65 72 5f 66 64 2c 20 28 73 74 72 (master_fd, (str
9310: 75 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 uct sockaddr *)
9320: 26 61 64 64 72 2c 20 26 61 64 64 72 6c 65 6e 29 &addr, &addrlen)
9330: 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 0a 09 09 20 2a ;...}..../*... *
9340: 20 49 66 20 77 65 20 66 61 69 6c 2c 20 6d 61 6b If we fail, mak
9350: 65 20 61 20 6e 6f 74 65 20 6f 66 20 69 74 20 73 e a note of it s
9360: 6f 20 77 65 20 64 6f 6e 27 74 20 67 6f 20 69 6e o we don't go in
9370: 74 6f 20 61 20 6c 6f 6f 70 20 6f 66 0a 09 09 20 to a loop of...
9380: 2a 20 61 63 63 65 70 74 28 29 20 66 61 69 6c 69 * accept() faili
9390: 6e 67 0a 09 09 20 2a 2f 0a 09 09 69 66 20 28 66 ng... */...if (f
93a0: 64 20 3c 20 30 29 20 7b 0a 09 09 09 2f 2a 20 4c d < 0) {..../* L
93b0: 6f 67 20 74 68 65 20 6e 65 77 20 63 6f 6e 6e 65 og the new conne
93c0: 63 74 69 6f 6e 20 2a 2f 0a 09 09 09 66 69 6c 65 ction */....file
93d0: 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 43 43 45 50 d_log_msg("ACCEP
93e0: 54 5f 46 41 49 4c 45 44 22 29 3b 0a 0a 09 09 09 T_FAILED");.....
93f0: 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 2b 2b 3b failure_count++;
9400: 0a 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 66 .....filed_log_f
9410: 72 65 65 28 6c 6f 67 29 3b 0a 0a 09 09 09 63 6f ree(log);.....co
9420: 6e 74 69 6e 75 65 3b 0a 09 09 7d 0a 0a 09 09 66 ntinue;...}....f
9430: 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f iled_sockettimeo
9440: 75 74 5f 61 63 63 65 70 74 28 66 64 29 3b 0a 0a ut_accept(fd);..
9450: 09 09 2f 2a 20 46 69 6c 6c 20 69 6e 20 6c 6f 67 ../* Fill in log
9460: 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 09 09 structure */...
9470: 69 66 20 28 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 if (filed_log_ip
9480: 28 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 ((struct sockadd
9490: 72 20 2a 29 20 26 61 64 64 72 2c 20 6c 6f 67 2d r *) &addr, log-
94a0: 3e 69 70 2c 20 73 69 7a 65 6f 66 28 6c 6f 67 2d >ip, sizeof(log-
94b0: 3e 69 70 29 29 20 3d 3d 20 4e 55 4c 4c 29 20 7b >ip)) == NULL) {
94c0: 0a 09 09 09 6c 6f 67 2d 3e 69 70 5b 30 5d 20 3d ....log->ip[0] =
94d0: 20 27 5c 30 27 3b 0a 09 09 09 6c 6f 67 2d 3e 70 '\0';....log->p
94e0: 6f 72 74 20 3d 20 30 3b 0a 09 09 7d 20 65 6c 73 ort = 0;...} els
94f0: 65 20 7b 0a 09 09 09 6c 6f 67 2d 3e 70 6f 72 74 e {....log->port
9500: 20 3d 20 61 64 64 72 2e 73 69 6e 36 5f 70 6f 72 = addr.sin6_por
9510: 74 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 52 65 73 t;...}..../* Res
9520: 65 74 20 66 61 69 6c 75 72 65 20 63 6f 75 6e 74 et failure count
9530: 2a 2f 0a 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 */...failure_cou
9540: 6e 74 20 3d 20 30 3b 0a 0a 09 09 2f 2a 20 48 61 nt = 0;..../* Ha
9550: 6e 64 6c 65 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 ndle socket */..
9560: 09 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 74 .connection_stat
9570: 65 20 3d 20 66 69 6c 65 64 5f 68 61 6e 64 6c 65 e = filed_handle
9580: 5f 63 6c 69 65 6e 74 28 66 64 2c 20 26 72 65 71 _client(fd, &req
9590: 75 65 73 74 2c 20 6c 6f 67 2c 20 6f 70 74 69 6f uest, log, optio
95a0: 6e 73 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65 70 ns);..}.../* Rep
95b0: 6f 72 74 20 65 72 72 6f 72 20 2a 2f 0a 09 66 69 ort error */..fi
95c0: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 54 48 52 led_log_msg("THR
95d0: 45 41 44 5f 44 49 45 44 20 41 42 4e 4f 52 4d 41 EAD_DIED ABNORMA
95e0: 4c 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 4e 55 L");...return(NU
95f0: 4c 4c 29 3b 0a 0a 09 2f 2a 20 6c 6f 63 61 6c 5f LL);.../* local_
9600: 64 75 6d 6d 79 5f 6c 6f 67 20 69 73 20 6f 6e 6c dummy_log is onl
9610: 79 20 75 73 65 64 20 69 66 20 46 49 4c 45 44 5f y used if FILED_
9620: 44 4f 4e 54 5f 4c 4f 47 20 69 73 20 65 6e 61 62 DONT_LOG is enab
9630: 6c 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20 69 led, otherwise i
9640: 74 27 73 20 6e 6f 74 20 75 73 65 64 2c 20 62 75 t's not used, bu
9650: 74 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 68 t the compiler h
9660: 61 74 65 73 20 74 68 61 74 20 69 64 65 61 2e 20 ates that idea.
9670: 2a 2f 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f */..local_dummy_
9680: 6c 6f 67 2e 74 79 70 65 20 3d 20 30 3b 0a 09 6c log.type = 0;..l
9690: 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 ocal_dummy_log.t
96a0: 79 70 65 20 3d 20 6c 6f 63 61 6c 5f 64 75 6d 6d ype = local_dumm
96b0: 79 5f 6c 6f 67 2e 74 79 70 65 3b 0a 7d 0a 0a 2f y_log.type;.}../
96c0: 2a 20 43 72 65 61 74 65 20 77 6f 72 6b 65 72 20 * Create worker
96d0: 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 61 74 69 threads */.stati
96e0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 77 6f 72 6b c int filed_work
96f0: 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 28 er_threads_init(
9700: 69 6e 74 20 66 64 2c 20 69 6e 74 20 74 68 72 65 int fd, int thre
9710: 61 64 5f 63 6f 75 6e 74 2c 20 73 74 72 75 63 74 ad_count, struct
9720: 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a filed_options *
9730: 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 74 72 75 options) {..stru
9740: 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f ct filed_worker_
9750: 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 thread_args *arg
9760: 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 ;..pthread_t thr
9770: 65 61 64 69 64 3b 0a 09 69 6e 74 20 70 74 68 72 eadid;..int pthr
9780: 65 61 64 5f 72 65 74 3b 0a 09 69 6e 74 20 69 3b ead_ret;..int i;
9790: 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 ...for (i = 0; i
97a0: 20 3c 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 3b < thread_count;
97b0: 20 69 2b 2b 29 20 7b 0a 09 09 61 72 67 20 3d 20 i++) {...arg =
97c0: 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 61 malloc(sizeof(*a
97d0: 72 67 29 29 3b 0a 0a 09 09 61 72 67 2d 3e 66 64 rg));....arg->fd
97e0: 20 3d 20 66 64 3b 0a 09 09 6d 65 6d 63 70 79 28 = fd;...memcpy(
97f0: 26 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 2c 20 6f &arg->options, o
9800: 70 74 69 6f 6e 73 2c 20 73 69 7a 65 6f 66 28 2a ptions, sizeof(*
9810: 6f 70 74 69 6f 6e 73 29 29 3b 0a 0a 09 09 70 74 options));....pt
9820: 68 72 65 61 64 5f 72 65 74 20 3d 20 70 74 68 72 hread_ret = pthr
9830: 65 61 64 5f 63 72 65 61 74 65 28 26 74 68 72 65 ead_create(&thre
9840: 61 64 69 64 2c 20 4e 55 4c 4c 2c 20 66 69 6c 65 adid, NULL, file
9850: 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 2c d_worker_thread,
9860: 20 61 72 67 29 3b 0a 09 09 69 66 20 28 70 74 68 arg);...if (pth
9870: 72 65 61 64 5f 72 65 74 20 21 3d 20 30 29 20 7b read_ret != 0) {
9880: 0a 09 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a ....return(-1);.
9890: 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 ..}..}...return(
98a0: 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 73 70 6c 61 0);.}../* Displa
98b0: 79 20 68 65 6c 70 20 2a 2f 0a 73 74 61 74 69 63 y help */.static
98c0: 20 76 6f 69 64 20 66 69 6c 65 64 5f 70 72 69 6e void filed_prin
98d0: 74 5f 68 65 6c 70 28 46 49 4c 45 20 2a 6f 75 74 t_help(FILE *out
98e0: 70 75 74 2c 20 69 6e 74 20 6c 6f 6e 67 5f 68 65 put, int long_he
98f0: 6c 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a lp, const char *
9900: 65 78 74 72 61 29 20 7b 0a 09 69 66 20 28 65 78 extra) {..if (ex
9910: 74 72 61 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 tra) {...fprintf
9920: 28 6f 75 74 70 75 74 2c 20 22 25 73 5c 6e 22 2c (output, "%s\n",
9930: 20 65 78 74 72 61 29 3b 0a 09 7d 0a 0a 09 66 70 extra);..}...fp
9940: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 55 rintf(output, "U
9950: 73 61 67 65 3a 20 66 69 6c 65 64 20 5b 3c 6f 70 sage: filed [<op
9960: 74 69 6f 6e 73 3e 5d 5c 6e 22 29 3b 0a 09 66 70 tions>]\n");..fp
9970: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
9980: 20 4f 70 74 69 6f 6e 73 3a 5c 6e 22 29 3b 0a 09 Options:\n");..
9990: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
99a0: 22 20 20 20 20 20 20 2d 68 2c 20 2d 2d 68 65 6c " -h, --hel
99b0: 70 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 p\n");..fprintf(
99c0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
99d0: 64 2c 20 2d 2d 64 61 65 6d 6f 6e 5c 6e 22 29 3b d, --daemon\n");
99e0: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
99f0: 2c 20 22 20 20 20 20 20 20 2d 76 2c 20 2d 2d 76 , " -v, --v
9a00: 65 72 73 69 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 ersion\n");..fpr
9a10: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
9a20: 20 20 20 20 2d 56 2c 20 2d 2d 76 68 6f 73 74 5c -V, --vhost\
9a30: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
9a40: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 62 20 tput, " -b
9a50: 3c 61 64 64 72 65 73 73 3e 2c 20 2d 2d 62 69 6e <address>, --bin
9a60: 64 20 3c 61 64 64 72 65 73 73 3e 5c 6e 22 29 3b d <address>\n");
9a70: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
9a80: 2c 20 22 20 20 20 20 20 20 2d 70 20 3c 70 6f 72 , " -p <por
9a90: 74 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70 6f 72 74 t>, --port <port
9aa0: 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 >\n");..fprintf(
9ab0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
9ac0: 74 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d 74 68 72 t <count>, --thr
9ad0: 65 61 64 73 20 3c 63 6f 75 6e 74 3e 5c 6e 22 29 eads <count>\n")
9ae0: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
9af0: 74 2c 20 22 20 20 20 20 20 20 2d 63 20 3c 65 6e t, " -c <en
9b00: 74 72 69 65 73 3e 2c 20 2d 2d 63 61 63 68 65 20 tries>, --cache
9b10: 3c 65 6e 74 72 69 65 73 3e 5c 6e 22 29 3b 0a 09 <entries>\n");..
9b20: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
9b30: 22 20 20 20 20 20 20 2d 6c 20 3c 66 69 6c 65 3e " -l <file>
9b40: 2c 20 2d 2d 6c 6f 67 20 3c 66 69 6c 65 3e 5c 6e , --log <file>\n
9b50: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 ");..fprintf(out
9b60: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 75 20 3c put, " -u <
9b70: 75 73 65 72 3e 2c 20 2d 2d 75 73 65 72 20 3c 75 user>, --user <u
9b80: 73 65 72 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e ser>\n");..fprin
9b90: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
9ba0: 20 20 2d 72 20 3c 64 69 72 65 63 74 6f 72 79 3e -r <directory>
9bb0: 2c 20 2d 2d 72 6f 6f 74 20 3c 64 69 72 65 63 74 , --root <direct
9bc0: 6f 72 79 3e 5c 6e 22 29 3b 0a 0a 09 69 66 20 28 ory>\n");...if (
9bd0: 6c 6f 6e 67 5f 68 65 6c 70 29 20 7b 0a 09 09 66 long_help) {...f
9be0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
9bf0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
9c00: 6f 75 74 70 75 74 2c 20 22 20 20 55 73 61 67 65 output, " Usage
9c10: 3a 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 :\n");...fprintf
9c20: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
9c30: 2d 68 20 28 6f 72 20 2d 2d 68 65 6c 70 29 20 70 -h (or --help) p
9c40: 72 69 6e 74 73 20 74 68 69 73 20 75 73 61 67 65 rints this usage
9c50: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 5c 6e 22 information.\n"
9c60: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
9c70: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 put, "\n");...fp
9c80: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
9c90: 20 20 20 20 20 2d 64 20 28 6f 72 20 2d 2d 64 61 -d (or --da
9ca0: 65 6d 6f 6e 29 20 69 6e 73 74 72 75 63 74 73 20 emon) instructs
9cb0: 66 69 6c 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 filed to become
9cc0: 61 20 64 61 65 6d 6f 6e 20 61 66 74 65 72 20 69 a daemon after i
9cd0: 6e 69 74 69 61 6c 69 7a 69 6e 67 5c 6e 22 29 3b nitializing\n");
9ce0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9cf0: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
9d00: 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c the l
9d10: 69 73 74 65 6e 69 6e 67 20 54 43 50 20 73 6f 63 istening TCP soc
9d20: 6b 65 74 20 61 6e 64 20 6c 6f 67 20 66 69 6c 65 ket and log file
9d30: 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 s.\n");...fprint
9d40: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
9d50: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9d60: 74 2c 20 22 20 20 20 20 20 20 2d 76 20 28 6f 72 t, " -v (or
9d70: 20 2d 2d 76 65 72 73 69 6f 6e 29 20 69 6e 73 74 --version) inst
9d80: 72 75 63 74 73 20 66 69 6c 65 64 20 70 72 69 6e ructs filed prin
9d90: 74 20 6f 75 74 20 74 68 65 20 76 65 72 73 69 6f t out the versio
9da0: 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 65 78 69 n number and exi
9db0: 74 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 t.\n");...fprint
9dc0: 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b f(output, "\n");
9dd0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
9de0: 74 2c 20 22 20 20 20 20 20 20 2d 56 20 28 6f 72 t, " -V (or
9df0: 20 2d 2d 76 68 6f 73 74 29 20 69 6e 73 74 72 75 --vhost) instru
9e00: 63 74 73 20 66 69 6c 65 64 20 74 6f 20 70 72 65 cts filed to pre
9e10: 70 65 6e 64 20 61 6c 6c 20 72 65 71 75 65 73 74 pend all request
9e20: 73 20 77 69 74 68 20 74 68 65 69 72 20 48 54 54 s with their HTT
9e30: 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 P\n");...fprintf
9e40: 28 6f 75 74 70 75 74 2c 20 22 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: 48 6f 73 74 20 68 65 61 64 65 72 2e 5c 6e 22 29 Host header.\n")
9e70: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
9e80: 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 ut, "\n");...fpr
9e90: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
9ea0: 20 20 20 20 2d 62 20 28 6f 72 20 2d 2d 62 69 6e -b (or --bin
9eb0: 64 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 d) specifies the
9ec0: 20 61 64 64 72 65 73 73 20 74 6f 20 6c 69 73 74 address to list
9ed0: 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 6e 67 20 en for incoming
9ee0: 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 HTTP\n");...fpri
9ef0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
9f00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f10: 20 20 72 65 71 75 65 73 74 73 20 6f 6e 2e 20 20 requests on.
9f20: 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 The default valu
9f30: 65 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c e is \"%s\".\n",
9f40: 20 42 49 4e 44 5f 41 44 44 52 29 3b 0a 09 09 66 BIND_ADDR);...f
9f50: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
9f60: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
9f70: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
9f80: 70 20 28 6f 72 20 2d 2d 70 6f 72 74 29 20 73 70 p (or --port) sp
9f90: 65 63 69 66 69 65 73 20 74 68 65 20 54 43 50 20 ecifies the TCP
9fa0: 70 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f 20 6c port number to l
9fb0: 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 isten for incomi
9fc0: 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 ng HTTP\n");...f
9fd0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
9fe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ff0: 20 20 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e requests on
a000: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 . The default i
a010: 73 20 25 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 s %u.\n", (unsig
a020: 6e 65 64 20 69 6e 74 29 20 50 4f 52 54 29 3b 0a ned int) PORT);.
a030: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
a040: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e , "\n");...fprin
a050: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
a060: 20 20 2d 74 20 28 6f 72 20 2d 2d 74 68 72 65 61 -t (or --threa
a070: 64 73 29 20 73 70 65 63 69 66 69 65 73 20 74 68 ds) specifies th
a080: 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 6f 72 6b e number of work
a090: 65 72 20 74 68 72 65 61 64 73 20 74 6f 20 63 72 er threads to cr
a0a0: 65 61 74 65 2e 20 45 61 63 68 5c 6e 22 29 3b 0a eate. Each\n");.
a0b0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
a0c0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
a0d0: 20 20 20 20 20 20 20 20 20 20 20 77 6f 72 6b 65 worke
a0e0: 72 20 74 68 72 65 61 64 20 63 61 6e 20 73 65 72 r thread can ser
a0f0: 76 69 63 65 20 6f 6e 65 20 63 6f 6e 63 75 72 72 vice one concurr
a100: 65 6e 74 20 48 54 54 50 20 73 65 73 73 69 6f 6e ent HTTP session
a110: 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 .\n");...fprintf
a120: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
a130: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a140: 20 20 54 68 75 73 20 74 68 65 20 6e 75 6d 62 65 Thus the numbe
a150: 72 20 6f 66 20 74 68 72 65 61 64 73 20 63 72 65 r of threads cre
a160: 61 74 65 64 20 77 69 6c 6c 20 64 65 74 65 72 6d ated will determ
a170: 69 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a 09 09 66 ine how\n");...f
a180: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
a190: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1a0: 20 20 20 20 20 20 20 20 6d 61 6e 79 20 73 69 6d many sim
a1b0: 75 6c 74 61 6e 65 6f 75 73 20 74 72 61 6e 73 66 ultaneous transf
a1c0: 65 72 73 20 77 69 6c 6c 20 62 65 20 70 6f 73 73 ers will be poss
a1d0: 69 62 6c 65 2e 20 54 68 65 5c 6e 22 29 3b 0a 09 ible. The\n");..
a1e0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
a1f0: 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a200: 20 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c defaul
a210: 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 75 t is %lu.\n", (u
a220: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 54 48 nsigned long) TH
a230: 52 45 41 44 5f 43 4f 55 4e 54 29 3b 0a 09 09 66 READ_COUNT);...f
a240: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
a250: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
a260: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
a270: 63 20 28 6f 72 20 2d 2d 63 61 63 68 65 29 20 73 c (or --cache) s
a280: 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d pecifies the num
a290: 62 65 72 20 6f 66 20 66 69 6c 65 20 69 6e 66 6f ber of file info
a2a0: 72 6d 61 74 69 6f 6e 20 63 61 63 68 65 20 65 6e rmation cache en
a2b0: 74 72 69 65 73 5c 6e 22 29 3b 0a 09 09 66 70 72 tries\n");...fpr
a2c0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
a2d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2e0: 20 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2e to allocate.
a2f0: 20 20 45 61 63 68 20 63 61 63 68 65 20 65 6e 74 Each cache ent
a300: 72 79 20 68 6f 6c 64 73 20 66 69 6c 65 20 69 6e ry holds file in
a310: 66 6f 72 6d 61 74 69 6f 6e 20 61 73 5c 6e 22 29 formation as\n")
a320: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
a330: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
a340: 20 20 20 20 20 20 20 20 20 20 20 77 65 6c 6c 20 well
a350: 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 as an open file
a360: 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74 68 descriptor to th
a370: 65 20 66 69 6c 65 2c 20 73 6f 20 72 65 73 6f 75 e file, so resou
a380: 72 63 65 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e rce\n");...fprin
a390: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
a3a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3b0: 20 20 6c 69 6d 69 74 73 20 28 69 2e 65 2e 2c 20 limits (i.e.,
a3c0: 75 6c 69 6d 69 74 29 20 73 68 6f 75 6c 64 20 62 ulimit) should b
a3d0: 65 20 63 6f 6e 73 69 64 65 72 65 64 2e 20 20 54 e considered. T
a3e0: 68 69 73 20 73 68 6f 75 6c 64 5c 6e 22 29 3b 0a his should\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 20 20 62 65 20 61 20 70 72 be a pr
a420: 69 6d 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 69 ime number for i
a430: 64 65 61 6c 20 75 73 65 20 77 69 74 68 20 74 68 deal use with th
a440: 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 6f 64 2e e lookup method.
a450: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
a460: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
a470: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
a480: 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 25 6c he default is %l
a490: 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 u.\n", (unsigned
a4a0: 20 6c 6f 6e 67 29 20 43 41 43 48 45 5f 53 49 5a long) CACHE_SIZ
a4b0: 45 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 E);...fprintf(ou
a4c0: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 tput, "\n");...f
a4d0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
a4e0: 20 20 20 20 20 20 2d 6c 20 28 6f 72 20 2d 2d 6c -l (or --l
a4f0: 6f 67 29 20 73 70 65 63 69 66 69 65 73 20 61 20 og) specifies a
a500: 66 69 6c 65 6e 61 6d 65 20 74 6f 20 6f 70 65 6e filename to open
a510: 20 66 6f 72 20 77 72 69 74 69 6e 67 20 6c 6f 67 for writing log
a520: 20 65 6e 74 72 69 65 73 2e 20 20 4c 6f 67 5c 6e entries. Log\n
a530: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
a540: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
a550: 20 20 20 20 20 20 20 20 20 20 20 65 6e 74 72 69 entri
a560: 65 73 20 61 72 65 20 6d 61 64 65 20 66 6f 72 20 es are made for
a570: 76 61 72 69 6f 75 73 20 73 74 61 67 65 73 20 69 various stages i
a580: 6e 20 74 72 61 6e 73 66 65 72 69 6e 67 20 66 69 n transfering fi
a590: 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 les.\n");...fpri
a5a0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
a5b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5c0: 20 54 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 73 The log file is
a5d0: 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 73 opened before s
a5e0: 77 69 74 63 68 69 6e 67 20 75 73 65 72 73 20 28 witching users (
a5f0: 73 65 65 20 5c 22 2d 75 5c 22 29 5c 6e 22 29 3b see \"-u\")\n");
a600: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
a610: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
a620: 20 20 20 20 20 20 20 20 61 6e 64 20 72 6f 6f 74 and root
a630: 20 64 69 72 65 63 74 6f 72 69 65 73 20 28 73 65 directories (se
a640: 65 20 5c 22 2d 72 5c 22 29 2e 20 20 54 68 65 20 e \"-r\"). The
a650: 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e 65 76 65 log file is neve
a660: 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 r\n");...fprintf
a670: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
a680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6c cl
a690: 6f 73 65 64 20 73 6f 20 6c 6f 67 20 72 6f 74 61 osed so log rota
a6a0: 74 69 6f 6e 20 77 69 74 68 6f 75 74 20 73 74 6f tion without sto
a6b0: 70 70 69 6e 67 20 74 68 65 20 64 61 65 6d 6f 6e pping the daemon
a6c0: 20 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 is will\n");...
a6d0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
a6e0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a6f0: 20 20 20 20 20 6e 6f 74 20 77 6f 72 6b 2e 20 20 not work.
a700: 54 68 65 20 76 61 6c 75 65 20 6f 66 20 5c 22 2d The value of \"-
a710: 5c 22 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 \" indicates tha
a720: 74 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 75 t standard outpu
a730: 74 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 t\n");...fprintf
a740: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
a750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 68 sh
a760: 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
a770: 20 6c 6f 67 67 69 6e 67 2e 20 20 49 66 20 74 68 logging. If th
a780: 65 20 66 69 6c 65 6e 61 6d 65 20 62 65 67 69 6e e filename begin
a790: 73 20 77 69 74 68 20 61 5c 6e 22 29 3b 0a 09 09 s with a\n");...
a7a0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
a7b0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a7c0: 20 20 20 20 20 70 69 70 65 20 28 5c 22 7c 5c 22 pipe (\"|\"
a7d0: 29 20 74 68 65 6e 20 61 20 70 72 6f 63 65 73 73 ) then a process
a7e0: 20 69 73 20 73 74 61 72 74 65 64 20 61 6e 64 20 is started and
a7f0: 75 73 65 64 20 66 6f 72 20 6c 6f 67 67 69 6e 67 used for logging
a800: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
a810: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
a820: 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 73 ins
a830: 74 65 61 64 20 6f 66 20 61 20 66 69 6c 65 2e 20 tead of a file.
a840: 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 The default is
a850: 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 4c 4f 47 5f \"%s\".\n", LOG_
a860: 46 49 4c 45 29 3b 0a 23 69 66 64 65 66 20 46 49 FILE);.#ifdef FI
a870: 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 09 09 66 LED_DONT_LOG...f
a880: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
a890: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8a0: 20 20 20 20 4e 6f 74 65 20 74 68 61 74 20 6c 6f Note that lo
a8b0: 67 67 69 6e 67 20 69 73 20 63 6f 6d 70 6c 65 74 gging is complet
a8c0: 65 6c 79 20 64 69 73 61 62 6c 65 64 20 73 6f 20 ely disabled so
a8d0: 74 68 69 73 20 6f 70 74 69 6f 6e 20 64 6f 65 73 this option does
a8e0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
a8f0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
a900: 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f 74 not
a910: 68 69 6e 67 20 69 6e 20 74 68 69 73 20 62 75 69 hing in this bui
a920: 6c 64 2e 5c 6e 22 29 3b 0a 23 65 6e 64 69 66 0a ld.\n");.#endif.
a930: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
a940: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e , "\n");...fprin
a950: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
a960: 20 20 2d 75 20 28 6f 72 20 2d 2d 75 73 65 72 29 -u (or --user)
a970: 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 75 specifies the u
a980: 73 65 72 20 74 6f 20 73 77 69 74 63 68 20 75 73 ser to switch us
a990: 65 72 20 49 44 73 20 74 6f 20 62 65 66 6f 72 65 er IDs to before
a9a0: 20 73 65 72 76 69 63 69 6e 67 5c 6e 22 29 3b 0a servicing\n");.
a9b0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
a9c0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
a9d0: 20 20 20 20 20 20 20 20 72 65 71 75 65 73 74 73 requests
a9e0: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 . The default i
a9f0: 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 75 73 65 s not change use
aa00: 72 20 49 44 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 r IDs.\n");...fp
aa10: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
aa20: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
aa30: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 72 utput, " -r
aa40: 20 28 6f 72 20 2d 2d 72 6f 6f 74 29 20 73 70 65 (or --root) spe
aa50: 63 69 66 69 65 73 20 74 68 65 20 64 69 72 65 63 cifies the direc
aa60: 74 6f 72 79 20 74 6f 20 61 63 74 20 61 73 20 74 tory to act as t
aa70: 68 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 he root director
aa80: 79 20 66 6f 72 5c 6e 22 29 3b 0a 09 09 66 70 72 y for\n");...fpr
aa90: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 intf(output, "
aaa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aab0: 20 20 20 74 68 65 20 66 69 6c 65 20 73 65 72 76 the file serv
aac0: 65 72 2e 20 20 49 66 20 74 68 69 73 20 6f 70 74 er. If this opt
aad0: 69 6f 6e 20 69 73 20 73 70 65 63 69 66 69 65 64 ion is specified
aae0: 2c 20 63 68 72 6f 6f 74 28 32 29 5c 6e 22 29 3b , chroot(2)\n");
aaf0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
ab00: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
ab10: 20 20 20 20 20 20 20 20 20 69 73 20 63 61 6c 6c is call
ab20: 65 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 ed. The default
ab30: 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 72 is not change r
ab40: 6f 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73 2c oot directories,
ab50: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
ab60: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 output, "
ab70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
ab80: 61 74 20 69 73 2c 20 74 68 65 20 5c 22 2f 5c 22 at is, the \"/\"
ab90: 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 73 68 directory is sh
aba0: 61 72 65 64 20 6f 75 74 2e 20 20 54 68 69 73 20 ared out. This
abb0: 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 will\n");...fpri
abc0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
abd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
abe0: 20 20 6c 69 6b 65 6c 79 20 62 65 20 61 20 73 65 likely be a se
abf0: 63 75 72 69 74 79 20 69 73 73 75 65 2c 20 73 6f curity issue, so
ac00: 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 73 68 6f this option sho
ac10: 75 6c 64 20 61 6c 77 61 79 73 5c 6e 22 29 3b 0a uld always\n");.
ac20: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
ac30: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
ac40: 20 20 20 20 20 20 20 20 62 65 20 75 73 65 64 2e be used.
ac50: 5c 6e 22 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 \n");..}...retur
ac60: 6e 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 20 67 n;.}../* Add a g
ac70: 65 74 6f 70 74 20 6f 70 74 69 6f 6e 20 2a 2f 0a etopt option */.
ac80: 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 static void file
ac90: 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 d_getopt_long_se
aca0: 74 6f 70 74 28 73 74 72 75 63 74 20 6f 70 74 69 topt(struct opti
acb0: 6f 6e 20 2a 6f 70 74 2c 20 63 6f 6e 73 74 20 63 on *opt, const c
acc0: 68 61 72 20 2a 6e 61 6d 65 2c 20 69 6e 74 20 68 har *name, int h
acd0: 61 73 5f 61 72 67 2c 20 69 6e 74 20 76 61 6c 29 as_arg, int val)
ace0: 20 7b 0a 09 6f 70 74 2d 3e 6e 61 6d 65 20 20 20 {..opt->name
acf0: 20 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 = (const char
ad00: 2a 29 20 6e 61 6d 65 3b 0a 09 6f 70 74 2d 3e 68 *) name;..opt->h
ad10: 61 73 5f 61 72 67 20 20 3d 20 68 61 73 5f 61 72 as_arg = has_ar
ad20: 67 3b 0a 09 6f 70 74 2d 3e 66 6c 61 67 20 20 20 g;..opt->flag
ad30: 20 20 3d 20 4e 55 4c 4c 3b 0a 09 6f 70 74 2d 3e = NULL;..opt->
ad40: 76 61 6c 20 20 20 20 20 20 3d 20 76 61 6c 3b 0a val = val;.
ad50: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 ..return;.}../*
ad60: 52 65 73 6f 6c 76 65 20 61 20 75 73 65 72 6e 61 Resolve a userna
ad70: 6d 65 20 74 6f 20 61 20 55 49 44 20 2a 2f 0a 73 me to a UID */.s
ad80: 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f tatic int filed_
ad90: 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 63 6f 6e 73 user_lookup(cons
ada0: 74 20 63 68 61 72 20 2a 75 73 65 72 2c 20 75 69 t char *user, ui
adb0: 64 5f 74 20 2a 75 73 65 72 5f 69 64 29 20 7b 0a d_t *user_id) {.
adc0: 09 63 68 61 72 20 2a 6e 65 78 74 3b 0a 09 75 69 .char *next;..ui
add0: 64 5f 74 20 75 73 65 72 5f 69 64 5f 63 68 65 63 d_t user_id_chec
ade0: 6b 3b 0a 23 69 66 6e 64 65 66 20 46 49 4c 45 44 k;.#ifndef FILED
adf0: 5f 4e 4f 5f 47 45 54 50 57 4e 41 4d 0a 09 73 74 _NO_GETPWNAM..st
ae00: 72 75 63 74 20 70 61 73 73 77 64 20 2a 65 6e 74 ruct passwd *ent
ae10: 3b 0a 0a 09 65 6e 74 20 3d 20 67 65 74 70 77 6e ;...ent = getpwn
ae20: 61 6d 28 75 73 65 72 29 3b 0a 09 69 66 20 28 65 am(user);..if (e
ae30: 6e 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 nt != NULL) {...
ae40: 2a 75 73 65 72 5f 69 64 20 3d 20 65 6e 74 2d 3e *user_id = ent->
ae50: 70 77 5f 75 69 64 3b 0a 0a 09 09 72 65 74 75 72 pw_uid;....retur
ae60: 6e 28 30 29 3b 0a 09 7d 0a 23 65 6e 64 69 66 0a n(0);..}.#endif.
ae70: 0a 09 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 20 ..user_id_check
ae80: 3d 20 73 74 72 74 6f 75 6c 6c 28 75 73 65 72 2c = strtoull(user,
ae90: 20 26 6e 65 78 74 2c 20 31 30 29 3b 0a 09 69 66 &next, 10);..if
aea0: 20 28 6e 65 78 74 20 3d 3d 20 4e 55 4c 4c 29 20 (next == NULL)
aeb0: 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 {...return(1);..
aec0: 7d 0a 0a 09 69 66 20 28 6e 65 78 74 5b 30 5d 20 }...if (next[0]
aed0: 21 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 65 74 != '\0') {...ret
aee0: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2a 75 73 urn(1);..}...*us
aef0: 65 72 5f 69 64 20 3d 20 75 73 65 72 5f 69 64 5f er_id = user_id_
af00: 63 68 65 63 6b 3b 0a 0a 09 72 65 74 75 72 6e 28 check;...return(
af10: 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 61 65 6d 6f 6e 0);.}../* Daemon
af20: 69 7a 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e ize */.static in
af30: 74 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a t filed_daemoniz
af40: 65 28 76 6f 69 64 29 20 7b 0a 09 70 69 64 5f 74 e(void) {..pid_t
af50: 20 73 65 74 73 69 64 5f 72 65 74 2c 20 66 6f 72 setsid_ret, for
af60: 6b 5f 72 65 74 3b 0a 09 69 6e 74 20 63 68 64 69 k_ret;..int chdi
af70: 72 5f 72 65 74 2c 20 64 75 70 32 5f 72 65 74 3b r_ret, dup2_ret;
af80: 0a 09 69 6e 74 20 66 64 5f 69 6e 2c 20 66 64 5f ..int fd_in, fd_
af90: 6f 75 74 3b 0a 0a 09 63 68 64 69 72 5f 72 65 74 out;...chdir_ret
afa0: 20 3d 20 63 68 64 69 72 28 22 2f 22 29 3b 0a 09 = chdir("/");..
afb0: 69 66 20 28 63 68 64 69 72 5f 72 65 74 20 21 3d if (chdir_ret !=
afc0: 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 0) {...return(1
afd0: 29 3b 0a 09 7d 0a 0a 09 66 6f 72 6b 5f 72 65 74 );..}...fork_ret
afe0: 20 3d 20 66 6f 72 6b 28 29 3b 0a 09 69 66 20 28 = fork();..if (
aff0: 66 6f 72 6b 5f 72 65 74 20 3c 20 30 29 20 7b 0a fork_ret < 0) {.
b000: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
b010: 0a 09 69 66 20 28 66 6f 72 6b 5f 72 65 74 20 3e ..if (fork_ret >
b020: 20 30 29 20 7b 0a 09 09 2f 2a 20 50 61 72 65 6e 0) {.../* Paren
b030: 74 20 2a 2f 0a 09 09 77 61 69 74 70 69 64 28 66 t */...waitpid(f
b040: 6f 72 6b 5f 72 65 74 2c 20 4e 55 4c 4c 2c 20 30 ork_ret, NULL, 0
b050: 29 3b 0a 0a 09 09 65 78 69 74 28 45 58 49 54 5f );....exit(EXIT_
b060: 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f SUCCESS);..}.../
b070: 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 69 66 20 28 * Child */..if (
b080: 66 6f 72 6b 28 29 20 21 3d 20 30 29 20 7b 0a 09 fork() != 0) {..
b090: 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 09 65 ./* Child */...e
b0a0: 78 69 74 28 45 58 49 54 5f 53 55 43 43 45 53 53 xit(EXIT_SUCCESS
b0b0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 47 72 61 6e 64 );..}.../* Grand
b0c0: 20 63 68 69 6c 64 20 2a 2f 0a 09 73 65 74 73 69 child */..setsi
b0d0: 64 5f 72 65 74 20 3d 20 73 65 74 73 69 64 28 29 d_ret = setsid()
b0e0: 3b 0a 09 69 66 20 28 73 65 74 73 69 64 5f 72 65 ;..if (setsid_re
b0f0: 74 20 3d 3d 20 28 28 70 69 64 5f 74 29 20 2d 31 t == ((pid_t) -1
b100: 29 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 )) {...return(1)
b110: 3b 0a 09 7d 0a 0a 09 66 64 5f 69 6e 20 3d 20 6f ;..}...fd_in = o
b120: 70 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c pen("/dev/null",
b130: 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 09 66 64 5f O_RDONLY);..fd_
b140: 6f 75 74 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 out = open("/dev
b150: 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 52 4f 4e 4c 59 /null", O_WRONLY
b160: 29 3b 0a 09 69 66 20 28 66 64 5f 69 6e 20 3c 20 );..if (fd_in <
b170: 30 20 7c 7c 20 66 64 5f 6f 75 74 20 3c 20 30 29 0 || fd_out < 0)
b180: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a {...return(1);.
b190: 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 .}...dup2_ret =
b1a0: 64 75 70 32 28 66 64 5f 69 6e 2c 20 53 54 44 49 dup2(fd_in, STDI
b1b0: 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 N_FILENO);..if (
b1c0: 64 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 49 dup2_ret != STDI
b1d0: 4e 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 N_FILENO) {...re
b1e0: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 turn(1);..}...du
b1f0: 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64 p2_ret = dup2(fd
b200: 5f 6f 75 74 2c 20 53 54 44 4f 55 54 5f 46 49 4c _out, STDOUT_FIL
b210: 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f ENO);..if (dup2_
b220: 72 65 74 20 21 3d 20 53 54 44 4f 55 54 5f 46 49 ret != STDOUT_FI
b230: 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e LENO) {...return
b240: 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 (1);..}...dup2_r
b250: 65 74 20 3d 20 64 75 70 32 28 66 64 5f 6f 75 74 et = dup2(fd_out
b260: 2c 20 53 54 44 45 52 52 5f 46 49 4c 45 4e 4f 29 , STDERR_FILENO)
b270: 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74 20 ;..if (dup2_ret
b280: 21 3d 20 53 54 44 45 52 52 5f 46 49 4c 45 4e 4f != STDERR_FILENO
b290: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b ) {...return(1);
b2a0: 0a 09 7d 0a 0a 09 63 6c 6f 73 65 28 66 64 5f 69 ..}...close(fd_i
b2b0: 6e 29 3b 0a 09 63 6c 6f 73 65 28 66 64 5f 6f 75 n);..close(fd_ou
b2c0: 74 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b t);...return(0);
b2d0: 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 70 72 6f 63 65 .}../* Run proce
b2e0: 73 73 20 2a 2f 0a 69 6e 74 20 6d 61 69 6e 28 69 ss */.int main(i
b2f0: 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a nt argc, char **
b300: 61 72 67 76 29 20 7b 0a 09 73 74 72 75 63 74 20 argv) {..struct
b310: 6f 70 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 5b 31 option options[1
b320: 32 5d 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 2];..struct file
b330: 64 5f 6f 70 74 69 6f 6e 73 20 74 68 72 65 61 64 d_options thread
b340: 5f 6f 70 74 69 6f 6e 73 20 3d 20 7b 30 7d 3b 0a _options = {0};.
b350: 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 62 69 6e .const char *bin
b360: 64 5f 61 64 64 72 20 3d 20 42 49 4e 44 5f 41 44 d_addr = BIND_AD
b370: 44 52 2c 20 2a 6e 65 77 72 6f 6f 74 20 3d 20 4e DR, *newroot = N
b380: 55 4c 4c 2c 20 2a 6c 6f 67 5f 66 69 6c 65 20 3d ULL, *log_file =
b390: 20 4c 4f 47 5f 46 49 4c 45 3b 0a 09 46 49 4c 45 LOG_FILE;..FILE
b3a0: 20 2a 6c 6f 67 5f 66 70 3b 0a 09 75 69 64 5f 74 *log_fp;..uid_t
b3b0: 20 75 73 65 72 20 3d 20 30 3b 0a 09 69 6e 74 20 user = 0;..int
b3c0: 70 6f 72 74 20 3d 20 50 4f 52 54 2c 20 74 68 72 port = PORT, thr
b3d0: 65 61 64 5f 63 6f 75 6e 74 20 3d 20 54 48 52 45 ead_count = THRE
b3e0: 41 44 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 AD_COUNT;..int c
b3f0: 61 63 68 65 5f 73 69 7a 65 20 3d 20 43 41 43 48 ache_size = CACH
b400: 45 5f 53 49 5a 45 3b 0a 09 69 6e 74 20 69 6e 69 E_SIZE;..int ini
b410: 74 5f 72 65 74 2c 20 63 68 72 6f 6f 74 5f 72 65 t_ret, chroot_re
b420: 74 2c 20 73 65 74 75 69 64 5f 72 65 74 2c 20 6c t, setuid_ret, l
b430: 6f 6f 6b 75 70 5f 72 65 74 2c 20 63 68 64 69 72 ookup_ret, chdir
b440: 5f 72 65 74 3b 0a 09 69 6e 74 20 73 65 74 75 69 _ret;..int setui
b450: 64 5f 65 6e 61 62 6c 65 64 20 3d 20 30 2c 20 64 d_enabled = 0, d
b460: 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 aemon_enabled =
b470: 30 3b 0a 09 69 6e 74 20 63 68 3b 0a 09 69 6e 74 0;..int ch;..int
b480: 20 66 64 3b 0a 0a 09 2f 2a 20 53 65 74 20 64 65 fd;.../* Set de
b490: 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a 09 fault value */..
b4a0: 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73 2e 66 thread_options.f
b4b0: 61 6b 65 5f 6e 65 77 72 6f 6f 74 20 3d 20 4e 55 ake_newroot = NU
b4c0: 4c 4c 3b 0a 0a 09 2f 2a 20 50 72 6f 63 65 73 73 LL;.../* Process
b4d0: 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 66 arguments */..f
b4e0: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
b4f0: 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 _setopt(&options
b500: 5b 30 5d 2c 20 22 70 6f 72 74 22 2c 20 72 65 71 [0], "port", req
b510: 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 uired_argument,
b520: 27 70 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 'p');..filed_get
b530: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 opt_long_setopt(
b540: 26 6f 70 74 69 6f 6e 73 5b 31 5d 2c 20 22 74 68 &options[1], "th
b550: 72 65 61 64 73 22 2c 20 72 65 71 75 69 72 65 64 reads", required
b560: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 74 27 29 3b _argument, 't');
b570: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c ..filed_getopt_l
b580: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 ong_setopt(&opti
b590: 6f 6e 73 5b 32 5d 2c 20 22 63 61 63 68 65 22 2c ons[2], "cache",
b5a0: 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 required_argume
b5b0: 6e 74 2c 20 27 63 27 29 3b 0a 09 66 69 6c 65 64 nt, 'c');..filed
b5c0: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 _getopt_long_set
b5d0: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 33 5d 2c opt(&options[3],
b5e0: 20 22 62 69 6e 64 22 2c 20 72 65 71 75 69 72 65 "bind", require
b5f0: 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 62 27 29 d_argument, 'b')
b600: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f ;..filed_getopt_
b610: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 long_setopt(&opt
b620: 69 6f 6e 73 5b 34 5d 2c 20 22 75 73 65 72 22 2c ions[4], "user",
b630: 20 72 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 required_argume
b640: 6e 74 2c 20 27 75 27 29 3b 0a 09 66 69 6c 65 64 nt, 'u');..filed
b650: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 _getopt_long_set
b660: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 35 5d 2c opt(&options[5],
b670: 20 22 72 6f 6f 74 22 2c 20 72 65 71 75 69 72 65 "root", require
b680: 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 72 27 29 d_argument, 'r')
b690: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f ;..filed_getopt_
b6a0: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 long_setopt(&opt
b6b0: 69 6f 6e 73 5b 36 5d 2c 20 22 68 65 6c 70 22 2c ions[6], "help",
b6c0: 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 68 no_argument, 'h
b6d0: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 ');..filed_getop
b6e0: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f t_long_setopt(&o
b6f0: 70 74 69 6f 6e 73 5b 37 5d 2c 20 22 64 61 65 6d ptions[7], "daem
b700: 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 on", no_argument
b710: 2c 20 27 64 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 'd');..filed_g
b720: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
b730: 74 28 26 6f 70 74 69 6f 6e 73 5b 38 5d 2c 20 22 t(&options[8], "
b740: 6c 6f 67 22 2c 20 72 65 71 75 69 72 65 64 5f 61 log", required_a
b750: 72 67 75 6d 65 6e 74 2c 20 27 6c 27 29 3b 0a 09 rgument, 'l');..
b760: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
b770: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
b780: 73 5b 39 5d 2c 20 22 76 65 72 73 69 6f 6e 22 2c s[9], "version",
b790: 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 76 no_argument, 'v
b7a0: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 ');..filed_getop
b7b0: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f t_long_setopt(&o
b7c0: 70 74 69 6f 6e 73 5b 31 30 5d 2c 20 22 76 68 6f ptions[10], "vho
b7d0: 73 74 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 st", no_argument
b7e0: 2c 20 27 56 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 'V');..filed_g
b7f0: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
b800: 74 28 26 6f 70 74 69 6f 6e 73 5b 31 31 5d 2c 20 t(&options[11],
b810: 4e 55 4c 4c 2c 20 30 2c 20 30 29 3b 0a 09 77 68 NULL, 0, 0);..wh
b820: 69 6c 65 20 28 28 63 68 20 3d 20 67 65 74 6f 70 ile ((ch = getop
b830: 74 5f 6c 6f 6e 67 28 61 72 67 63 2c 20 61 72 67 t_long(argc, arg
b840: 76 2c 20 22 70 3a 74 3a 63 3a 62 3a 75 3a 72 3a v, "p:t:c:b:u:r:
b850: 6c 3a 68 64 76 56 22 2c 20 6f 70 74 69 6f 6e 73 l:hdvV", options
b860: 2c 20 4e 55 4c 4c 29 29 20 21 3d 20 2d 31 29 20 , NULL)) != -1)
b870: 7b 0a 09 09 73 77 69 74 63 68 28 63 68 29 20 7b {...switch(ch) {
b880: 0a 09 09 09 63 61 73 65 20 27 70 27 3a 0a 09 09 ....case 'p':...
b890: 09 09 70 6f 72 74 20 3d 20 61 74 6f 69 28 6f 70 ..port = atoi(op
b8a0: 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b targ);.....break
b8b0: 3b 0a 09 09 09 63 61 73 65 20 27 74 27 3a 0a 09 ;....case 't':..
b8c0: 09 09 09 74 68 72 65 61 64 5f 63 6f 75 6e 74 20 ...thread_count
b8d0: 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a = atoi(optarg);.
b8e0: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 ....break;....ca
b8f0: 73 65 20 27 63 27 3a 0a 09 09 09 09 63 61 63 68 se 'c':.....cach
b900: 65 5f 73 69 7a 65 20 3d 20 61 74 6f 69 28 6f 70 e_size = atoi(op
b910: 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b targ);.....break
b920: 3b 0a 09 09 09 63 61 73 65 20 27 62 27 3a 0a 09 ;....case 'b':..
b930: 09 09 09 62 69 6e 64 5f 61 64 64 72 20 3d 20 73 ...bind_addr = s
b940: 74 72 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 trdup(optarg);..
b950: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 ...break;....cas
b960: 65 20 27 75 27 3a 0a 09 09 09 09 73 65 74 75 69 e 'u':.....setui
b970: 64 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 d_enabled = 1;..
b980: 09 09 09 6c 6f 6f 6b 75 70 5f 72 65 74 20 3d 20 ...lookup_ret =
b990: 66 69 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 filed_user_looku
b9a0: 70 28 6f 70 74 61 72 67 2c 20 26 75 73 65 72 29 p(optarg, &user)
b9b0: 3b 0a 09 09 09 09 69 66 20 28 6c 6f 6f 6b 75 70 ;.....if (lookup
b9c0: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 _ret != 0) {....
b9d0: 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 ..filed_print_he
b9e0: 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20 22 49 lp(stderr, 0, "I
b9f0: 6e 76 61 6c 69 64 20 75 73 65 72 6e 61 6d 65 20 nvalid username
ba00: 73 70 65 63 69 66 69 65 64 22 29 3b 0a 0a 09 09 specified");....
ba10: 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 ...return(1);...
ba20: 09 09 7d 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 ..}.....break;..
ba30: 09 09 63 61 73 65 20 27 72 27 3a 0a 09 09 09 09 ..case 'r':.....
ba40: 66 69 6c 65 64 5f 70 61 74 68 5f 74 72 61 6e 73 filed_path_trans
ba50: 6c 61 74 65 5f 73 65 74 5f 72 6f 6f 74 28 6e 65 late_set_root(ne
ba60: 77 72 6f 6f 74 2c 20 26 74 68 72 65 61 64 5f 6f wroot, &thread_o
ba70: 70 74 69 6f 6e 73 2c 20 6f 70 74 61 72 67 29 3b ptions, optarg);
ba80: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 .....break;....c
ba90: 61 73 65 20 27 6c 27 3a 0a 09 09 09 09 6c 6f 67 ase 'l':.....log
baa0: 5f 66 69 6c 65 20 3d 20 73 74 72 64 75 70 28 6f _file = strdup(o
bab0: 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 ptarg);.....brea
bac0: 6b 3b 0a 09 09 09 63 61 73 65 20 27 64 27 3a 0a k;....case 'd':.
bad0: 09 09 09 09 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c ....daemon_enabl
bae0: 65 64 20 3d 20 31 3b 0a 09 09 09 09 62 72 65 61 ed = 1;.....brea
baf0: 6b 3b 0a 09 09 09 63 61 73 65 20 27 56 27 3a 0a k;....case 'V':.
bb00: 09 09 09 09 74 68 72 65 61 64 5f 6f 70 74 69 6f ....thread_optio
bb10: 6e 73 2e 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 ns.vhosts_enable
bb20: 64 20 3d 20 31 3b 0a 0a 09 09 09 09 62 72 65 61 d = 1;......brea
bb30: 6b 3b 0a 09 09 09 63 61 73 65 20 27 76 27 3a 0a k;....case 'v':.
bb40: 09 09 09 09 70 72 69 6e 74 66 28 22 66 69 6c 65 ....printf("file
bb50: 64 20 76 65 72 73 69 6f 6e 20 25 73 5c 6e 22 2c d version %s\n",
bb60: 20 46 49 4c 45 44 5f 56 45 52 53 49 4f 4e 29 3b FILED_VERSION);
bb70: 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 30 29 3b ......return(0);
bb80: 0a 09 09 09 63 61 73 65 20 27 3f 27 3a 0a 09 09 ....case '?':...
bb90: 09 63 61 73 65 20 27 3a 27 3a 0a 09 09 09 09 66 .case ':':.....f
bba0: 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 iled_print_help(
bbb0: 73 74 64 65 72 72 2c 20 30 2c 20 4e 55 4c 4c 29 stderr, 0, NULL)
bbc0: 3b 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 31 29 ;......return(1)
bbd0: 3b 0a 09 09 09 63 61 73 65 20 27 68 27 3a 0a 09 ;....case 'h':..
bbe0: 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 ...filed_print_h
bbf0: 65 6c 70 28 73 74 64 6f 75 74 2c 20 31 2c 20 4e elp(stdout, 1, N
bc00: 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 74 75 72 ULL);......retur
bc10: 6e 28 30 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f n(0);...}..}.../
bc20: 2a 20 4f 70 65 6e 20 6c 6f 67 20 66 69 6c 65 20 * Open log file
bc30: 2a 2f 0a 09 6c 6f 67 5f 66 70 20 3d 20 66 69 6c */..log_fp = fil
bc40: 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 6c 6f 67 5f ed_log_open(log_
bc50: 66 69 6c 65 29 3b 0a 09 69 66 20 28 6c 6f 67 5f file);..if (log_
bc60: 66 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 fp == NULL) {...
bc70: 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c 6f perror("filed_lo
bc80: 67 5f 6f 70 65 6e 22 29 3b 0a 0a 09 09 72 65 74 g_open");....ret
bc90: 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 urn(4);..}.../*
bca0: 43 72 65 61 74 65 20 6c 69 73 74 65 6e 69 6e 67 Create listening
bcb0: 20 73 6f 63 6b 65 74 20 2a 2f 0a 09 66 64 20 3d socket */..fd =
bcc0: 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 62 69 filed_listen(bi
bcd0: 6e 64 5f 61 64 64 72 2c 20 70 6f 72 74 29 3b 0a nd_addr, port);.
bce0: 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 .if (fd < 0) {..
bcf0: 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 6c .perror("filed_l
bd00: 69 73 74 65 6e 22 29 3b 0a 0a 09 09 72 65 74 75 isten");....retu
bd10: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 rn(1);..}.../* I
bd20: 6e 69 74 69 61 6c 69 7a 65 20 74 69 6d 65 6f 75 nitialize timeou
bd30: 74 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a t structures */.
bd40: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 .init_ret = file
bd50: 64 5f 73 6f 63 6b 65 74 74 69 6d 65 6f 75 74 5f d_sockettimeout_
bd60: 69 6e 69 74 28 29 3b 0a 09 69 66 20 28 69 6e 69 init();..if (ini
bd70: 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 t_ret != 0) {...
bd80: 70 65 72 72 6f 72 28 22 66 69 6c 65 64 5f 73 6f perror("filed_so
bd90: 63 6b 65 74 74 69 6d 65 6f 75 74 5f 69 6e 69 74 ckettimeout_init
bda0: 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 38 29 ");....return(8)
bdb0: 3b 0a 09 7d 0a 0a 09 2f 2a 20 42 65 63 6f 6d 65 ;..}.../* Become
bdc0: 20 61 20 64 61 65 6d 6f 6e 20 2a 2f 0a 09 69 66 a daemon */..if
bdd0: 20 28 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 (daemon_enabled
bde0: 29 20 7b 0a 09 09 69 6e 69 74 5f 72 65 74 20 3d ) {...init_ret =
bdf0: 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 filed_daemonize
be00: 28 29 3b 0a 09 09 69 66 20 28 69 6e 69 74 5f 72 ();...if (init_r
be10: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65 et != 0) {....pe
be20: 72 72 6f 72 28 22 66 69 6c 65 64 5f 64 61 65 6d rror("filed_daem
be30: 6f 6e 69 7a 65 22 29 3b 0a 0a 09 09 09 72 65 74 onize");.....ret
be40: 75 72 6e 28 36 29 3b 0a 09 09 7d 0a 09 7d 0a 0a urn(6);...}..}..
be50: 09 2f 2a 20 43 68 72 6f 6f 74 2c 20 69 66 20 61 ./* Chroot, if a
be60: 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09 69 ppropriate */..i
be70: 66 20 28 6e 65 77 72 6f 6f 74 29 20 7b 0a 09 09 f (newroot) {...
be80: 63 68 64 69 72 5f 72 65 74 20 3d 20 63 68 64 69 chdir_ret = chdi
be90: 72 28 6e 65 77 72 6f 6f 74 29 3b 0a 09 09 69 66 r(newroot);...if
bea0: 20 28 63 68 64 69 72 5f 72 65 74 20 21 3d 20 30 (chdir_ret != 0
beb0: 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 63 ) {....perror("c
bec0: 68 64 69 72 22 29 3b 0a 0a 09 09 09 72 65 74 75 hdir");.....retu
bed0: 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 63 68 rn(1);...}....ch
bee0: 72 6f 6f 74 5f 72 65 74 20 3d 20 63 68 72 6f 6f root_ret = chroo
bef0: 74 28 22 2e 22 29 3b 0a 09 09 69 66 20 28 63 68 t(".");...if (ch
bf00: 72 6f 6f 74 5f 72 65 74 20 21 3d 20 30 29 20 7b root_ret != 0) {
bf10: 0a 09 09 09 70 65 72 72 6f 72 28 22 63 68 72 6f ....perror("chro
bf20: 6f 74 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e ot");.....return
bf30: 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a (1);...}..}.../*
bf40: 20 44 72 6f 70 20 70 72 69 76 69 6c 65 67 65 73 Drop privileges
bf50: 2c 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 , if appropriate
bf60: 20 2a 2f 0a 09 69 66 20 28 73 65 74 75 69 64 5f */..if (setuid_
bf70: 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 73 65 74 enabled) {...set
bf80: 75 69 64 5f 72 65 74 20 3d 20 73 65 74 75 69 64 uid_ret = setuid
bf90: 28 75 73 65 72 29 3b 0a 09 09 69 66 20 28 73 65 (user);...if (se
bfa0: 74 75 69 64 5f 72 65 74 20 21 3d 20 30 29 20 7b tuid_ret != 0) {
bfb0: 0a 09 09 09 70 65 72 72 6f 72 28 22 73 65 74 75 ....perror("setu
bfc0: 69 64 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e id");.....return
bfd0: 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a (1);...}..}.../*
bfe0: 20 49 6e 69 74 69 61 6c 69 7a 65 20 2a 2f 0a 09 Initialize */..
bff0: 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 init_ret = filed
c000: 5f 69 6e 69 74 28 63 61 63 68 65 5f 73 69 7a 65 _init(cache_size
c010: 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 );..if (init_ret
c020: 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f != 0) {...perro
c030: 72 28 22 66 69 6c 65 64 5f 69 6e 69 74 22 29 3b r("filed_init");
c040: 0a 0a 09 09 72 65 74 75 72 6e 28 33 29 3b 0a 09 ....return(3);..
c050: 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 6f }.../* Create lo
c060: 67 67 69 6e 67 20 74 68 72 65 61 64 20 2a 2f 0a gging thread */.
c070: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 .init_ret = file
c080: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 d_logging_thread
c090: 5f 69 6e 69 74 28 6c 6f 67 5f 66 70 29 3b 0a 09 _init(log_fp);..
c0a0: 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 if (init_ret !=
c0b0: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 0) {...perror("f
c0c0: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 iled_logging_thr
c0d0: 65 61 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72 ead_init");....r
c0e0: 65 74 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f eturn(4);..}.../
c0f0: 2a 20 43 72 65 61 74 65 20 73 6f 63 6b 65 74 20 * Create socket
c100: 74 65 72 6d 69 6e 61 74 69 6f 6e 20 74 68 72 65 termination thre
c110: 61 64 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 ad */..init_ret
c120: 3d 20 66 69 6c 65 64 5f 73 6f 63 6b 65 74 74 69 = filed_socketti
c130: 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f 69 6e 69 meout_thread_ini
c140: 74 28 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 t();..if (init_r
c150: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 et != 0) {...per
c160: 72 6f 72 28 22 66 69 6c 65 64 5f 73 6f 63 6b 65 ror("filed_socke
c170: 74 74 69 6d 65 6f 75 74 5f 74 68 72 65 61 64 5f ttimeout_thread_
c180: 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 init");....retur
c190: 6e 28 37 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e n(7);..}.../* In
c1a0: 69 74 69 61 6c 69 7a 65 20 73 65 63 63 6f 6d 70 itialize seccomp
c1b0: 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 20 3d 20 */..init_ret =
c1c0: 66 69 6c 65 64 5f 69 6e 69 74 5f 73 65 63 63 6f filed_init_secco
c1d0: 6d 70 28 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f mp();..if (init_
c1e0: 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 ret != 0) {...pe
c1f0: 72 72 6f 72 28 22 66 69 6c 65 64 5f 69 6e 69 74 rror("filed_init
c200: 5f 73 65 63 63 6f 6d 70 22 29 3b 0a 0a 09 09 72 _seccomp");....r
c210: 65 74 75 72 6e 28 39 29 3b 0a 09 7d 0a 0a 09 2f eturn(9);..}.../
c220: 2a 20 43 72 65 61 74 65 20 77 6f 72 6b 65 72 20 * Create worker
c230: 74 68 72 65 61 64 73 20 2a 2f 0a 09 69 6e 69 74 threads */..init
c240: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 77 6f 72 _ret = filed_wor
c250: 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 ker_threads_init
c260: 28 66 64 2c 20 74 68 72 65 61 64 5f 63 6f 75 6e (fd, thread_coun
c270: 74 2c 20 26 74 68 72 65 61 64 5f 6f 70 74 69 6f t, &thread_optio
c280: 6e 73 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 ns);..if (init_r
c290: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 et != 0) {...per
c2a0: 72 6f 72 28 22 66 69 6c 65 64 5f 77 6f 72 6b 65 ror("filed_worke
c2b0: 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 22 29 r_threads_init")
c2c0: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 35 29 3b 0a ;....return(5);.
c2d0: 09 7d 0a 0a 09 2f 2a 20 57 61 69 74 20 66 6f 72 .}.../* Wait for
c2e0: 20 74 68 72 65 61 64 73 20 74 6f 20 65 78 69 74 threads to exit
c2f0: 20 2a 2f 0a 09 2f 2a 20 58 58 58 3a 54 4f 44 4f */../* XXX:TODO
c300: 3a 20 4d 6f 6e 69 74 6f 72 20 74 68 72 65 61 64 : Monitor thread
c310: 20 75 73 61 67 65 20 2a 2f 0a 09 77 68 69 6c 65 usage */..while
c320: 20 28 31 29 20 7b 0a 09 09 73 6c 65 65 70 28 38 (1) {...sleep(8
c330: 36 34 30 30 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 6400);..}.../* R
c340: 65 74 75 72 6e 20 69 6e 20 66 61 69 6c 75 72 65 eturn in failure
c350: 20 2a 2f 0a 09 72 65 74 75 72 6e 28 32 29 3b 0a */..return(2);.
c360: 7d 0a }.