• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Debuginfo-over-http server.
2    Copyright (C) 2019-2021 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 
19 /* cargo-cult from libdwfl linux-kernel-modules.c */
20 /* In case we have a bad fts we include this before config.h because it
21    can't handle _FILE_OFFSET_BITS.
22    Everything we need here is fine if its declarations just come first.
23    Also, include sys/types.h before fts. On some systems fts.h is not self
24    contained. */
25 #ifdef BAD_FTS
26   #include <sys/types.h>
27   #include <fts.h>
28 #endif
29 
30 #ifdef HAVE_CONFIG_H
31   #include "config.h"
32 #endif
33 
34 extern "C" {
35 #include "printversion.h"
36 }
37 
38 #include "debuginfod.h"
39 #include <dwarf.h>
40 #include <system.h>
41 
42 #include <argp.h>
43 #ifdef __GNUC__
44 #undef __attribute__ /* glibc bug - rhbz 1763325 */
45 #endif
46 
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <libintl.h>
50 #include <locale.h>
51 #include <pthread.h>
52 #include <signal.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 #include <sys/vfs.h>
56 #include <unistd.h>
57 #include <fcntl.h>
58 #include <netdb.h>
59 
60 
61 /* If fts.h is included before config.h, its indirect inclusions may not
62    give us the right LFS aliases of these functions, so map them manually.  */
63 #ifdef BAD_FTS
64   #ifdef _FILE_OFFSET_BITS
65     #define open open64
66     #define fopen fopen64
67   #endif
68 #else
69   #include <sys/types.h>
70   #include <fts.h>
71 #endif
72 
73 #include <cstring>
74 #include <vector>
75 #include <set>
76 #include <map>
77 #include <string>
78 #include <iostream>
79 #include <iomanip>
80 #include <ostream>
81 #include <sstream>
82 #include <mutex>
83 #include <deque>
84 #include <condition_variable>
85 #include <thread>
86 // #include <regex> // on rhel7 gcc 4.8, not competent
87 #include <regex.h>
88 // #include <algorithm>
89 using namespace std;
90 
91 #include <gelf.h>
92 #include <libdwelf.h>
93 
94 #include <microhttpd.h>
95 
96 #if MHD_VERSION >= 0x00097002
97 // libmicrohttpd 0.9.71 broke API
98 #define MHD_RESULT enum MHD_Result
99 #else
100 #define MHD_RESULT int
101 #endif
102 
103 #include <curl/curl.h>
104 #include <archive.h>
105 #include <archive_entry.h>
106 #include <sqlite3.h>
107 
108 #ifdef __linux__
109 #include <sys/syscall.h>
110 #endif
111 
112 #ifdef __linux__
113 #define tid() syscall(SYS_gettid)
114 #else
115 #define tid() pthread_self()
116 #endif
117 
118 
119 inline bool
string_endswith(const string & haystack,const string & needle)120 string_endswith(const string& haystack, const string& needle)
121 {
122   return (haystack.size() >= needle.size() &&
123 	  equal(haystack.end()-needle.size(), haystack.end(),
124                 needle.begin()));
125 }
126 
127 
128 // Roll this identifier for every sqlite schema incompatibility.
129 #define BUILDIDS "buildids9"
130 
131 #if SQLITE_VERSION_NUMBER >= 3008000
132 #define WITHOUT_ROWID "without rowid"
133 #else
134 #define WITHOUT_ROWID ""
135 #endif
136 
137 static const char DEBUGINFOD_SQLITE_DDL[] =
138   "pragma foreign_keys = on;\n"
139   "pragma synchronous = 0;\n" // disable fsync()s - this cache is disposable across a machine crash
140   "pragma journal_mode = wal;\n" // https://sqlite.org/wal.html
141   "pragma wal_checkpoint = truncate;\n" // clean out any preexisting wal file
142   "pragma journal_size_limit = 0;\n" // limit steady state file (between grooming, which also =truncate's)
143   "pragma auto_vacuum = incremental;\n" // https://sqlite.org/pragma.html
144   "pragma busy_timeout = 1000;\n" // https://sqlite.org/pragma.html
145   // NB: all these are overridable with -D option
146 
147   // Normalization table for interning file names
148   "create table if not exists " BUILDIDS "_files (\n"
149   "        id integer primary key not null,\n"
150   "        name text unique not null\n"
151   "        );\n"
152   // Normalization table for interning buildids
153   "create table if not exists " BUILDIDS "_buildids (\n"
154   "        id integer primary key not null,\n"
155   "        hex text unique not null);\n"
156   // Track the completion of scanning of a given file & sourcetype at given time
157   "create table if not exists " BUILDIDS "_file_mtime_scanned (\n"
158   "        mtime integer not null,\n"
159   "        file integer not null,\n"
160   "        size integer not null,\n" // in bytes
161   "        sourcetype text(1) not null\n"
162   "            check (sourcetype IN ('F', 'R')),\n"
163   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
164   "        primary key (file, mtime, sourcetype)\n"
165   "        ) " WITHOUT_ROWID ";\n"
166   "create table if not exists " BUILDIDS "_f_de (\n"
167   "        buildid integer not null,\n"
168   "        debuginfo_p integer not null,\n"
169   "        executable_p integer not null,\n"
170   "        file integer not null,\n"
171   "        mtime integer not null,\n"
172   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
173   "        foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
174   "        primary key (buildid, file, mtime)\n"
175   "        ) " WITHOUT_ROWID ";\n"
176   "create table if not exists " BUILDIDS "_f_s (\n"
177   "        buildid integer not null,\n"
178   "        artifactsrc integer not null,\n"
179   "        file integer not null,\n" // NB: not necessarily entered into _mtime_scanned
180   "        mtime integer not null,\n"
181   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
182   "        foreign key (artifactsrc) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
183   "        foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
184   "        primary key (buildid, artifactsrc, file, mtime)\n"
185   "        ) " WITHOUT_ROWID ";\n"
186   "create table if not exists " BUILDIDS "_r_de (\n"
187   "        buildid integer not null,\n"
188   "        debuginfo_p integer not null,\n"
189   "        executable_p integer not null,\n"
190   "        file integer not null,\n"
191   "        mtime integer not null,\n"
192   "        content integer not null,\n"
193   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
194   "        foreign key (content) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
195   "        foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
196   "        primary key (buildid, debuginfo_p, executable_p, file, content, mtime)\n"
197   "        ) " WITHOUT_ROWID ";\n"
198   "create table if not exists " BUILDIDS "_r_sref (\n" // outgoing dwarf sourcefile references from rpm
199   "        buildid integer not null,\n"
200   "        artifactsrc integer not null,\n"
201   "        foreign key (artifactsrc) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
202   "        foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
203   "        primary key (buildid, artifactsrc)\n"
204   "        ) " WITHOUT_ROWID ";\n"
205   "create table if not exists " BUILDIDS "_r_sdef (\n" // rpm contents that may satisfy sref
206   "        file integer not null,\n"
207   "        mtime integer not null,\n"
208   "        content integer not null,\n"
209   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
210   "        foreign key (content) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
211   "        primary key (content, file, mtime)\n"
212   "        ) " WITHOUT_ROWID ";\n"
213   // create views to glue together some of the above tables, for webapi D queries
214   "create view if not exists " BUILDIDS "_query_d as \n"
215   "select\n"
216   "        b.hex as buildid, n.mtime, 'F' as sourcetype, f0.name as source0, n.mtime as mtime, null as source1\n"
217   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_f_de n\n"
218   "        where b.id = n.buildid and f0.id = n.file and n.debuginfo_p = 1\n"
219   "union all select\n"
220   "        b.hex as buildid, n.mtime, 'R' as sourcetype, f0.name as source0, n.mtime as mtime, f1.name as source1\n"
221   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_files f1, " BUILDIDS "_r_de n\n"
222   "        where b.id = n.buildid and f0.id = n.file and f1.id = n.content and n.debuginfo_p = 1\n"
223   ";"
224   // ... and for E queries
225   "create view if not exists " BUILDIDS "_query_e as \n"
226   "select\n"
227   "        b.hex as buildid, n.mtime, 'F' as sourcetype, f0.name as source0, n.mtime as mtime, null as source1\n"
228   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_f_de n\n"
229   "        where b.id = n.buildid and f0.id = n.file and n.executable_p = 1\n"
230   "union all select\n"
231   "        b.hex as buildid, n.mtime, 'R' as sourcetype, f0.name as source0, n.mtime as mtime, f1.name as source1\n"
232   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_files f1, " BUILDIDS "_r_de n\n"
233   "        where b.id = n.buildid and f0.id = n.file and f1.id = n.content and n.executable_p = 1\n"
234   ";"
235   // ... and for S queries
236   "create view if not exists " BUILDIDS "_query_s as \n"
237   "select\n"
238   "        b.hex as buildid, fs.name as artifactsrc, 'F' as sourcetype, f0.name as source0, n.mtime as mtime, null as source1, null as source0ref\n"
239   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_files fs, " BUILDIDS "_f_s n\n"
240   "        where b.id = n.buildid and f0.id = n.file and fs.id = n.artifactsrc\n"
241   "union all select\n"
242   "        b.hex as buildid, f1.name as artifactsrc, 'R' as sourcetype, f0.name as source0, sd.mtime as mtime, f1.name as source1, fsref.name as source0ref\n"
243   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_files f1, " BUILDIDS "_files fsref, "
244   "        " BUILDIDS "_r_sdef sd, " BUILDIDS "_r_sref sr, " BUILDIDS "_r_de sde\n"
245   "        where b.id = sr.buildid and f0.id = sd.file and fsref.id = sde.file and f1.id = sd.content\n"
246   "        and sr.artifactsrc = sd.content and sde.buildid = sr.buildid\n"
247   ";"
248   // and for startup overview counts
249   "drop view if exists " BUILDIDS "_stats;\n"
250   "create view if not exists " BUILDIDS "_stats as\n"
251   "          select 'file d/e' as label,count(*) as quantity from " BUILDIDS "_f_de\n"
252   "union all select 'file s',count(*) from " BUILDIDS "_f_s\n"
253   "union all select 'archive d/e',count(*) from " BUILDIDS "_r_de\n"
254   "union all select 'archive sref',count(*) from " BUILDIDS "_r_sref\n"
255   "union all select 'archive sdef',count(*) from " BUILDIDS "_r_sdef\n"
256   "union all select 'buildids',count(*) from " BUILDIDS "_buildids\n"
257   "union all select 'filenames',count(*) from " BUILDIDS "_files\n"
258   "union all select 'files scanned (#)',count(*) from " BUILDIDS "_file_mtime_scanned\n"
259   "union all select 'files scanned (mb)',coalesce(sum(size)/1024/1024,0) from " BUILDIDS "_file_mtime_scanned\n"
260 #if SQLITE_VERSION_NUMBER >= 3016000
261   "union all select 'index db size (mb)',page_count*page_size/1024/1024 as size FROM pragma_page_count(), pragma_page_size()\n"
262 #endif
263   ";\n"
264 
265 // schema change history & garbage collection
266 //
267 // XXX: we could have migration queries here to bring prior-schema
268 // data over instead of just dropping it.
269 //
270 // buildids9: widen the mtime_scanned table
271   "" // <<< we are here
272 // buildids8: slim the sref table
273   "drop table if exists buildids8_f_de;\n"
274   "drop table if exists buildids8_f_s;\n"
275   "drop table if exists buildids8_r_de;\n"
276   "drop table if exists buildids8_r_sref;\n"
277   "drop table if exists buildids8_r_sdef;\n"
278   "drop table if exists buildids8_file_mtime_scanned;\n"
279   "drop table if exists buildids8_files;\n"
280   "drop table if exists buildids8_buildids;\n"
281 // buildids7: separate _norm table into dense subtype tables
282   "drop table if exists buildids7_f_de;\n"
283   "drop table if exists buildids7_f_s;\n"
284   "drop table if exists buildids7_r_de;\n"
285   "drop table if exists buildids7_r_sref;\n"
286   "drop table if exists buildids7_r_sdef;\n"
287   "drop table if exists buildids7_file_mtime_scanned;\n"
288   "drop table if exists buildids7_files;\n"
289   "drop table if exists buildids7_buildids;\n"
290 // buildids6: drop bolo/rfolo again, represent sources / rpmcontents in main table
291   "drop table if exists buildids6_norm;\n"
292   "drop table if exists buildids6_files;\n"
293   "drop table if exists buildids6_buildids;\n"
294   "drop view if exists buildids6;\n"
295 // buildids5: redefine srcfile1 column to be '.'-less (for rpms)
296   "drop table if exists buildids5_norm;\n"
297   "drop table if exists buildids5_files;\n"
298   "drop table if exists buildids5_buildids;\n"
299   "drop table if exists buildids5_bolo;\n"
300   "drop table if exists buildids5_rfolo;\n"
301   "drop view if exists buildids5;\n"
302 // buildids4: introduce rpmfile RFOLO
303   "drop table if exists buildids4_norm;\n"
304   "drop table if exists buildids4_files;\n"
305   "drop table if exists buildids4_buildids;\n"
306   "drop table if exists buildids4_bolo;\n"
307   "drop table if exists buildids4_rfolo;\n"
308   "drop view if exists buildids4;\n"
309 // buildids3*: split out srcfile BOLO
310   "drop table if exists buildids3_norm;\n"
311   "drop table if exists buildids3_files;\n"
312   "drop table if exists buildids3_buildids;\n"
313   "drop table if exists buildids3_bolo;\n"
314   "drop view if exists buildids3;\n"
315 // buildids2: normalized buildid and filenames into interning tables;
316   "drop table if exists buildids2_norm;\n"
317   "drop table if exists buildids2_files;\n"
318   "drop table if exists buildids2_buildids;\n"
319   "drop view if exists buildids2;\n"
320   // buildids1: made buildid and artifacttype NULLable, to represent cached-negative
321 //           lookups from sources, e.g. files or rpms that contain no buildid-indexable content
322   "drop table if exists buildids1;\n"
323 // buildids: original
324   "drop table if exists buildids;\n"
325   ;
326 
327 static const char DEBUGINFOD_SQLITE_CLEANUP_DDL[] =
328   "pragma wal_checkpoint = truncate;\n" // clean out any preexisting wal file
329   ;
330 
331 
332 
333 
334 /* Name and version of program.  */
335 /* ARGP_PROGRAM_VERSION_HOOK_DEF = print_version; */ // not this simple for C++
336 
337 /* Bug report address.  */
338 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
339 
340 /* Definitions of arguments for argp functions.  */
341 static const struct argp_option options[] =
342   {
343    { NULL, 0, NULL, 0, "Scanners:", 1 },
344    { "scan-file-dir", 'F', NULL, 0, "Enable ELF/DWARF file scanning.", 0 },
345    { "scan-rpm-dir", 'R', NULL, 0, "Enable RPM scanning.", 0 },
346    { "scan-deb-dir", 'U', NULL, 0, "Enable DEB scanning.", 0 },
347    { "scan-archive", 'Z', "EXT=CMD", 0, "Enable arbitrary archive scanning.", 0 },
348    // "source-oci-imageregistry"  ...
349 
350    { NULL, 0, NULL, 0, "Options:", 2 },
351    { "logical", 'L', NULL, 0, "Follow symlinks, default=ignore.", 0 },
352    { "rescan-time", 't', "SECONDS", 0, "Number of seconds to wait between rescans, 0=disable.", 0 },
353    { "groom-time", 'g', "SECONDS", 0, "Number of seconds to wait between database grooming, 0=disable.", 0 },
354    { "maxigroom", 'G', NULL, 0, "Run a complete database groom/shrink pass at startup.", 0 },
355    { "concurrency", 'c', "NUM", 0, "Limit scanning thread concurrency to NUM.", 0 },
356    { "include", 'I', "REGEX", 0, "Include files matching REGEX, default=all.", 0 },
357    { "exclude", 'X', "REGEX", 0, "Exclude files matching REGEX, default=none.", 0 },
358    { "port", 'p', "NUM", 0, "HTTP port to listen on, default 8002.", 0 },
359    { "database", 'd', "FILE", 0, "Path to sqlite database.", 0 },
360    { "ddl", 'D', "SQL", 0, "Apply extra sqlite ddl/pragma to connection.", 0 },
361    { "verbose", 'v', NULL, 0, "Increase verbosity.", 0 },
362    { "regex-groom", 'r', NULL, 0,"Uses regexes from -I and -X arguments to groom the database.",0},
363 #define ARGP_KEY_FDCACHE_FDS 0x1001
364    { "fdcache-fds", ARGP_KEY_FDCACHE_FDS, "NUM", 0, "Maximum number of archive files to keep in fdcache.", 0 },
365 #define ARGP_KEY_FDCACHE_MBS 0x1002
366    { "fdcache-mbs", ARGP_KEY_FDCACHE_MBS, "MB", 0, "Maximum total size of archive file fdcache.", 0 },
367 #define ARGP_KEY_FDCACHE_PREFETCH 0x1003
368    { "fdcache-prefetch", ARGP_KEY_FDCACHE_PREFETCH, "NUM", 0, "Number of archive files to prefetch into fdcache.", 0 },
369 #define ARGP_KEY_FDCACHE_MINTMP 0x1004
370    { "fdcache-mintmp", ARGP_KEY_FDCACHE_MINTMP, "NUM", 0, "Minimum free space% on tmpdir.", 0 },
371 #define ARGP_KEY_FDCACHE_PREFETCH_MBS 0x1005
372    { "fdcache-prefetch-mbs", ARGP_KEY_FDCACHE_PREFETCH_MBS, "MB", 0,"Megabytes allocated to the \
373       prefetch cache.", 0},
374 #define ARGP_KEY_FDCACHE_PREFETCH_FDS 0x1006
375    { "fdcache-prefetch-fds", ARGP_KEY_FDCACHE_PREFETCH_FDS, "NUM", 0,"Number of files allocated to the \
376       prefetch cache.", 0},
377 #define ARGP_KEY_FORWARDED_TTL_LIMIT 0x1007
378    {"forwarded-ttl-limit", ARGP_KEY_FORWARDED_TTL_LIMIT, "NUM", 0, "Limit of X-Forwarded-For hops, default 8.", 0},
379 #define ARGP_KEY_PASSIVE 0x1008
380    { "passive", ARGP_KEY_PASSIVE, NULL, 0, "Do not scan or groom, read-only database.", 0 },
381    { NULL, 0, NULL, 0, NULL, 0 },
382   };
383 
384 /* Short description of program.  */
385 static const char doc[] = "Serve debuginfo-related content across HTTP from files under PATHs.";
386 
387 /* Strings for arguments in help texts.  */
388 static const char args_doc[] = "[PATH ...]";
389 
390 /* Prototype for option handler.  */
391 static error_t parse_opt (int key, char *arg, struct argp_state *state);
392 
393 /* Data structure to communicate with argp functions.  */
394 static struct argp argp =
395   {
396    options, parse_opt, args_doc, doc, NULL, NULL, NULL
397   };
398 
399 
400 static string db_path;
401 static sqlite3 *db;  // single connection, serialized across all our threads!
402 static sqlite3 *dbq; // webapi query-servicing readonly connection, serialized ditto!
403 static unsigned verbose;
404 static volatile sig_atomic_t interrupted = 0;
405 static volatile sig_atomic_t forced_rescan_count = 0;
406 static volatile sig_atomic_t sigusr1 = 0;
407 static volatile sig_atomic_t forced_groom_count = 0;
408 static volatile sig_atomic_t sigusr2 = 0;
409 static unsigned http_port = 8002;
410 static unsigned rescan_s = 300;
411 static unsigned groom_s = 86400;
412 static bool maxigroom = false;
413 static unsigned concurrency = std::thread::hardware_concurrency() ?: 1;
414 static set<string> source_paths;
415 static bool scan_files = false;
416 static map<string,string> scan_archives;
417 static vector<string> extra_ddl;
418 static regex_t file_include_regex;
419 static regex_t file_exclude_regex;
420 static bool regex_groom = false;
421 static bool traverse_logical;
422 static long fdcache_fds;
423 static long fdcache_mbs;
424 static long fdcache_prefetch;
425 static long fdcache_mintmp;
426 static long fdcache_prefetch_mbs;
427 static long fdcache_prefetch_fds;
428 static unsigned forwarded_ttl_limit = 8;
429 static string tmpdir;
430 static bool passive_p = false;
431 
432 static void set_metric(const string& key, double value);
433 // static void inc_metric(const string& key);
434 static void set_metric(const string& metric,
435                        const string& lname, const string& lvalue,
436                        double value);
437 static void inc_metric(const string& metric,
438                        const string& lname, const string& lvalue);
439 static void add_metric(const string& metric,
440                        const string& lname, const string& lvalue,
441                        double value);
442 static void inc_metric(const string& metric,
443                        const string& lname, const string& lvalue,
444                        const string& rname, const string& rvalue);
445 static void add_metric(const string& metric,
446                        const string& lname, const string& lvalue,
447                        const string& rname, const string& rvalue,
448                        double value);
449 
450 
451 class tmp_inc_metric { // a RAII style wrapper for exception-safe scoped increment & decrement
452   string m, n, v;
453 public:
tmp_inc_metric(const string & mname,const string & lname,const string & lvalue)454   tmp_inc_metric(const string& mname, const string& lname, const string& lvalue):
455     m(mname), n(lname), v(lvalue)
456   {
457     add_metric (m, n, v, 1);
458   }
~tmp_inc_metric()459   ~tmp_inc_metric()
460   {
461     add_metric (m, n, v, -1);
462   }
463 };
464 
465 class tmp_ms_metric { // a RAII style wrapper for exception-safe scoped timing
466   string m, n, v;
467   struct timespec ts_start;
468 public:
tmp_ms_metric(const string & mname,const string & lname,const string & lvalue)469   tmp_ms_metric(const string& mname, const string& lname, const string& lvalue):
470     m(mname), n(lname), v(lvalue)
471   {
472     clock_gettime (CLOCK_MONOTONIC, & ts_start);
473   }
~tmp_ms_metric()474   ~tmp_ms_metric()
475   {
476     struct timespec ts_end;
477     clock_gettime (CLOCK_MONOTONIC, & ts_end);
478     double deltas = (ts_end.tv_sec - ts_start.tv_sec)
479       + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
480 
481     add_metric (m + "_milliseconds_sum", n, v, (deltas*1000.0));
482     inc_metric (m + "_milliseconds_count", n, v);
483   }
484 };
485 
486 
487 /* Handle program arguments.  */
488 static error_t
parse_opt(int key,char * arg,struct argp_state * state)489 parse_opt (int key, char *arg,
490 	   struct argp_state *state __attribute__ ((unused)))
491 {
492   int rc;
493   switch (key)
494     {
495     case 'v': verbose ++; break;
496     case 'd':
497       /* When using the in-memory database make sure it is shareable,
498 	 so we can open it twice as read/write and read-only.  */
499       if (strcmp (arg, ":memory:") == 0)
500 	db_path = "file::memory:?cache=shared";
501       else
502 	db_path = string(arg);
503       break;
504     case 'p': http_port = (unsigned) atoi(arg);
505       if (http_port == 0 || http_port > 65535)
506         argp_failure(state, 1, EINVAL, "port number");
507       break;
508     case 'F': scan_files = true; break;
509     case 'R':
510       scan_archives[".rpm"]="cat"; // libarchive groks rpm natively
511       break;
512     case 'U':
513       scan_archives[".deb"]="(bsdtar -O -x -f - data.tar\\*)<";
514       scan_archives[".ddeb"]="(bsdtar -O -x -f - data.tar\\*)<";
515       scan_archives[".ipk"]="(bsdtar -O -x -f - data.tar\\*)<";
516       // .udeb too?
517       break;
518     case 'Z':
519       {
520         char* extension = strchr(arg, '=');
521         if (arg[0] == '\0')
522           argp_failure(state, 1, EINVAL, "missing EXT");
523         else if (extension)
524           scan_archives[string(arg, (extension-arg))]=string(extension+1);
525         else
526           scan_archives[string(arg)]=string("cat");
527       }
528       break;
529     case 'L':
530       if (passive_p)
531         argp_failure(state, 1, EINVAL, "-L option inconsistent with passive mode");
532       traverse_logical = true;
533       break;
534     case 'D':
535       if (passive_p)
536         argp_failure(state, 1, EINVAL, "-D option inconsistent with passive mode");
537       extra_ddl.push_back(string(arg));
538       break;
539     case 't':
540       if (passive_p)
541         argp_failure(state, 1, EINVAL, "-t option inconsistent with passive mode");
542       rescan_s = (unsigned) atoi(arg);
543       break;
544     case 'g':
545       if (passive_p)
546         argp_failure(state, 1, EINVAL, "-g option inconsistent with passive mode");
547       groom_s = (unsigned) atoi(arg);
548       break;
549     case 'G':
550       if (passive_p)
551         argp_failure(state, 1, EINVAL, "-G option inconsistent with passive mode");
552       maxigroom = true;
553       break;
554     case 'c':
555       if (passive_p)
556         argp_failure(state, 1, EINVAL, "-c option inconsistent with passive mode");
557       concurrency = (unsigned) atoi(arg);
558       if (concurrency < 1) concurrency = 1;
559       break;
560     case 'I':
561       // NB: no problem with unconditional free here - an earlier failed regcomp would exit program
562       if (passive_p)
563         argp_failure(state, 1, EINVAL, "-I option inconsistent with passive mode");
564       regfree (&file_include_regex);
565       rc = regcomp (&file_include_regex, arg, REG_EXTENDED|REG_NOSUB);
566       if (rc != 0)
567         argp_failure(state, 1, EINVAL, "regular expression");
568       break;
569     case 'X':
570       if (passive_p)
571         argp_failure(state, 1, EINVAL, "-X option inconsistent with passive mode");
572       regfree (&file_exclude_regex);
573       rc = regcomp (&file_exclude_regex, arg, REG_EXTENDED|REG_NOSUB);
574       if (rc != 0)
575         argp_failure(state, 1, EINVAL, "regular expression");
576       break;
577     case 'r':
578       if (passive_p)
579         argp_failure(state, 1, EINVAL, "-r option inconsistent with passive mode");
580       regex_groom = true;
581       break;
582     case ARGP_KEY_FDCACHE_FDS:
583       fdcache_fds = atol (arg);
584       break;
585     case ARGP_KEY_FDCACHE_MBS:
586       fdcache_mbs = atol (arg);
587       break;
588     case ARGP_KEY_FDCACHE_PREFETCH:
589       fdcache_prefetch = atol (arg);
590       break;
591     case ARGP_KEY_FDCACHE_MINTMP:
592       fdcache_mintmp = atol (arg);
593       if( fdcache_mintmp > 100 || fdcache_mintmp < 0 )
594         argp_failure(state, 1, EINVAL, "fdcache mintmp percent");
595       break;
596     case ARGP_KEY_FORWARDED_TTL_LIMIT:
597       forwarded_ttl_limit = (unsigned) atoi(arg);
598       break;
599     case ARGP_KEY_ARG:
600       source_paths.insert(string(arg));
601       break;
602     case ARGP_KEY_FDCACHE_PREFETCH_FDS:
603       fdcache_prefetch_fds = atol(arg);
604       if ( fdcache_prefetch_fds < 0)
605         argp_failure(state, 1, EINVAL, "fdcache prefetch fds");
606       break;
607     case ARGP_KEY_FDCACHE_PREFETCH_MBS:
608       fdcache_prefetch_mbs = atol(arg);
609       if ( fdcache_prefetch_mbs < 0)
610         argp_failure(state, 1, EINVAL, "fdcache prefetch mbs");
611       break;
612     case ARGP_KEY_PASSIVE:
613       passive_p = true;
614       if (source_paths.size() > 0
615           || maxigroom
616           || extra_ddl.size() > 0
617           || traverse_logical)
618         // other conflicting options tricky to check
619         argp_failure(state, 1, EINVAL, "inconsistent options with passive mode");
620       break;
621       // case 'h': argp_state_help (state, stderr, ARGP_HELP_LONG|ARGP_HELP_EXIT_OK);
622     default: return ARGP_ERR_UNKNOWN;
623     }
624 
625   return 0;
626 }
627 
628 
629 ////////////////////////////////////////////////////////////////////////
630 
631 
632 // represent errors that may get reported to an ostream and/or a libmicrohttpd connection
633 
634 struct reportable_exception
635 {
636   int code;
637   string message;
638 
reportable_exceptionreportable_exception639   reportable_exception(int c, const string& m): code(c), message(m) {}
reportable_exceptionreportable_exception640   reportable_exception(const string& m): code(503), message(m) {}
reportable_exceptionreportable_exception641   reportable_exception(): code(503), message() {}
642 
643   void report(ostream& o) const; // defined under obatched() class below
644 
mhd_send_responsereportable_exception645   MHD_RESULT mhd_send_response(MHD_Connection* c) const {
646     MHD_Response* r = MHD_create_response_from_buffer (message.size(),
647                                                        (void*) message.c_str(),
648                                                        MHD_RESPMEM_MUST_COPY);
649     MHD_add_response_header (r, "Content-Type", "text/plain");
650     MHD_RESULT rc = MHD_queue_response (c, code, r);
651     MHD_destroy_response (r);
652     return rc;
653   }
654 };
655 
656 
657 struct sqlite_exception: public reportable_exception
658 {
sqlite_exceptionsqlite_exception659   sqlite_exception(int rc, const string& msg):
660     reportable_exception(string("sqlite3 error: ") + msg + ": " + string(sqlite3_errstr(rc) ?: "?")) {
661     inc_metric("error_count","sqlite3",sqlite3_errstr(rc));
662   }
663 };
664 
665 struct libc_exception: public reportable_exception
666 {
libc_exceptionlibc_exception667   libc_exception(int rc, const string& msg):
668     reportable_exception(string("libc error: ") + msg + ": " + string(strerror(rc) ?: "?")) {
669     inc_metric("error_count","libc",strerror(rc));
670   }
671 };
672 
673 
674 struct archive_exception: public reportable_exception
675 {
archive_exceptionarchive_exception676   archive_exception(const string& msg):
677     reportable_exception(string("libarchive error: ") + msg) {
678       inc_metric("error_count","libarchive",msg);
679   }
archive_exceptionarchive_exception680   archive_exception(struct archive* a, const string& msg):
681     reportable_exception(string("libarchive error: ") + msg + ": " + string(archive_error_string(a) ?: "?")) {
682     inc_metric("error_count","libarchive",msg + ": " + string(archive_error_string(a) ?: "?"));
683   }
684 };
685 
686 
687 struct elfutils_exception: public reportable_exception
688 {
elfutils_exceptionelfutils_exception689   elfutils_exception(int rc, const string& msg):
690     reportable_exception(string("elfutils error: ") + msg + ": " + string(elf_errmsg(rc) ?: "?")) {
691     inc_metric("error_count","elfutils",elf_errmsg(rc));
692   }
693 };
694 
695 
696 ////////////////////////////////////////////////////////////////////////
697 
698 template <typename Payload>
699 class workq
700 {
701   set<Payload> q; // eliminate duplicates
702   mutex mtx;
703   condition_variable cv;
704   bool dead;
705   unsigned idlers;   // number of threads busy with wait_idle / done_idle
706   unsigned fronters; // number of threads busy with wait_front / done_front
707 
708 public:
workq()709   workq() { dead = false; idlers = 0; fronters = 0; }
~workq()710   ~workq() {}
711 
push_back(const Payload & p)712   void push_back(const Payload& p)
713   {
714     unique_lock<mutex> lock(mtx);
715     q.insert (p);
716     set_metric("thread_work_pending","role","scan", q.size());
717     cv.notify_all();
718   }
719 
720   // kill this workqueue, wake up all idlers / scanners
nuke()721   void nuke() {
722     unique_lock<mutex> lock(mtx);
723     // optional: q.clear();
724     dead = true;
725     cv.notify_all();
726   }
727 
728   // clear the workqueue, when scanning is interrupted with USR2
clear()729   void clear() {
730     unique_lock<mutex> lock(mtx);
731     q.clear();
732     set_metric("thread_work_pending","role","scan", q.size());
733     // NB: there may still be some live fronters
734     cv.notify_all(); // maybe wake up waiting idlers
735   }
736 
737   // block this scanner thread until there is work to do and no active idler
wait_front(Payload & p)738   bool wait_front (Payload& p)
739   {
740     unique_lock<mutex> lock(mtx);
741     while (!dead && (q.size() == 0 || idlers > 0))
742       cv.wait(lock);
743     if (dead)
744       return false;
745     else
746       {
747         p = * q.begin();
748         q.erase (q.begin());
749         fronters ++; // prevent idlers from starting awhile, even if empty q
750         set_metric("thread_work_pending","role","scan", q.size());
751         // NB: don't wake up idlers yet!  The consumer is busy
752         // processing this element until it calls done_front().
753         return true;
754       }
755   }
756 
757   // notify waitq that scanner thread is done with that last item
done_front()758   void done_front ()
759   {
760     unique_lock<mutex> lock(mtx);
761     fronters --;
762     if (q.size() == 0 && fronters == 0)
763       cv.notify_all(); // maybe wake up waiting idlers
764   }
765 
766   // block this idler thread until there is no work to do
wait_idle()767   void wait_idle ()
768   {
769     unique_lock<mutex> lock(mtx);
770     cv.notify_all(); // maybe wake up waiting scanners
771     while (!dead && ((q.size() != 0) || fronters > 0))
772       cv.wait(lock);
773     idlers ++;
774   }
775 
done_idle()776   void done_idle ()
777   {
778     unique_lock<mutex> lock(mtx);
779     idlers --;
780     cv.notify_all(); // maybe wake up waiting scanners, but probably not (shutting down)
781   }
782 };
783 
784 typedef struct stat stat_t;
785 typedef pair<string,stat_t> scan_payload;
operator <(const scan_payload & a,const scan_payload & b)786 inline bool operator< (const scan_payload& a, const scan_payload& b)
787 {
788   return a.first < b.first; // don't bother compare the stat fields
789 }
790 static workq<scan_payload> scanq; // just a single one
791 // producer & idler: thread_main_fts_source_paths()
792 // consumer: thread_main_scanner()
793 // idler: thread_main_groom()
794 
795 
796 ////////////////////////////////////////////////////////////////////////
797 
798 // Unique set is a thread-safe structure that lends 'ownership' of a value
799 // to a thread.  Other threads requesting the same thing are made to wait.
800 // It's like a semaphore-on-demand.
801 template <typename T>
802 class unique_set
803 {
804 private:
805   set<T> values;
806   mutex mtx;
807   condition_variable cv;
808 public:
unique_set()809   unique_set() {}
~unique_set()810   ~unique_set() {}
811 
acquire(const T & value)812   void acquire(const T& value)
813   {
814     unique_lock<mutex> lock(mtx);
815     while (values.find(value) != values.end())
816       cv.wait(lock);
817     values.insert(value);
818   }
819 
release(const T & value)820   void release(const T& value)
821   {
822     unique_lock<mutex> lock(mtx);
823     // assert (values.find(value) != values.end());
824     values.erase(value);
825     cv.notify_all();
826   }
827 };
828 
829 
830 // This is the object that's instantiate to uniquely hold a value in a
831 // RAII-pattern way.
832 template <typename T>
833 class unique_set_reserver
834 {
835 private:
836   unique_set<T>& please_hold;
837   T mine;
838 public:
unique_set_reserver(unique_set<T> & t,const T & value)839   unique_set_reserver(unique_set<T>& t, const T& value):
840     please_hold(t), mine(value)  { please_hold.acquire(mine); }
~unique_set_reserver()841   ~unique_set_reserver() { please_hold.release(mine); }
842 };
843 
844 
845 ////////////////////////////////////////////////////////////////////////
846 
847 
848 // Print a standard timestamp.
849 static ostream&
timestamp(ostream & o)850 timestamp (ostream &o)
851 {
852   char datebuf[80];
853   char *now2 = NULL;
854   time_t now_t = time(NULL);
855   struct tm *now = gmtime (&now_t);
856   if (now)
857     {
858       (void) strftime (datebuf, sizeof (datebuf), "%c", now);
859       now2 = datebuf;
860     }
861 
862   return o << "[" << (now2 ? now2 : "") << "] "
863            << "(" << getpid () << "/" << tid() << "): ";
864 }
865 
866 
867 // A little class that impersonates an ostream to the extent that it can
868 // take << streaming operations.  It batches up the bits into an internal
869 // stringstream until it is destroyed; then flushes to the original ostream.
870 // It adds a timestamp
871 class obatched
872 {
873 private:
874   ostream& o;
875   stringstream stro;
876   static mutex lock;
877 public:
obatched(ostream & oo,bool timestamp_p=true)878   obatched(ostream& oo, bool timestamp_p = true): o(oo)
879   {
880     if (timestamp_p)
881       timestamp(stro);
882   }
~obatched()883   ~obatched()
884   {
885     unique_lock<mutex> do_not_cross_the_streams(obatched::lock);
886     o << stro.str();
887     o.flush();
888   }
operator ostream&()889   operator ostream& () { return stro; }
operator <<(const T & t)890   template <typename T> ostream& operator << (const T& t) { stro << t; return stro; }
891 };
892 mutex obatched::lock; // just the one, since cout/cerr iostreams are not thread-safe
893 
894 
report(ostream & o) const895 void reportable_exception::report(ostream& o) const {
896   obatched(o) << message << endl;
897 }
898 
899 
900 ////////////////////////////////////////////////////////////////////////
901 
902 
903 // RAII style sqlite prepared-statement holder that matches { } block lifetime
904 
905 struct sqlite_ps
906 {
907 private:
908   sqlite3* db;
909   const string nickname;
910   const string sql;
911   sqlite3_stmt *pp;
912 
913   sqlite_ps(const sqlite_ps&); // make uncopyable
914   sqlite_ps& operator=(const sqlite_ps &); // make unassignable
915 
916 public:
sqlite_pssqlite_ps917   sqlite_ps (sqlite3* d, const string& n, const string& s): db(d), nickname(n), sql(s) {
918     // tmp_ms_metric tick("sqlite3","prep",nickname);
919     if (verbose > 4)
920       obatched(clog) << nickname << " prep " << sql << endl;
921     int rc = sqlite3_prepare_v2 (db, sql.c_str(), -1 /* to \0 */, & this->pp, NULL);
922     if (rc != SQLITE_OK)
923       throw sqlite_exception(rc, "prepare " + sql);
924   }
925 
resetsqlite_ps926   sqlite_ps& reset()
927   {
928     tmp_ms_metric tick("sqlite3","reset",nickname);
929     sqlite3_reset(this->pp);
930     return *this;
931   }
932 
bindsqlite_ps933   sqlite_ps& bind(int parameter, const string& str)
934   {
935     if (verbose > 4)
936       obatched(clog) << nickname << " bind " << parameter << "=" << str << endl;
937     int rc = sqlite3_bind_text (this->pp, parameter, str.c_str(), -1, SQLITE_TRANSIENT);
938     if (rc != SQLITE_OK)
939       throw sqlite_exception(rc, "sqlite3 bind");
940     return *this;
941   }
942 
bindsqlite_ps943   sqlite_ps& bind(int parameter, int64_t value)
944   {
945     if (verbose > 4)
946       obatched(clog) << nickname << " bind " << parameter << "=" << value << endl;
947     int rc = sqlite3_bind_int64 (this->pp, parameter, value);
948     if (rc != SQLITE_OK)
949       throw sqlite_exception(rc, "sqlite3 bind");
950     return *this;
951   }
952 
bindsqlite_ps953   sqlite_ps& bind(int parameter)
954   {
955     if (verbose > 4)
956       obatched(clog) << nickname << " bind " << parameter << "=" << "NULL" << endl;
957     int rc = sqlite3_bind_null (this->pp, parameter);
958     if (rc != SQLITE_OK)
959       throw sqlite_exception(rc, "sqlite3 bind");
960     return *this;
961   }
962 
963 
step_ok_donesqlite_ps964   void step_ok_done() {
965     tmp_ms_metric tick("sqlite3","step_done",nickname);
966     int rc = sqlite3_step (this->pp);
967     if (verbose > 4)
968       obatched(clog) << nickname << " step-ok-done(" << sqlite3_errstr(rc) << ") " << sql << endl;
969     if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW)
970       throw sqlite_exception(rc, "sqlite3 step");
971     (void) sqlite3_reset (this->pp);
972   }
973 
974 
stepsqlite_ps975   int step() {
976     tmp_ms_metric tick("sqlite3","step",nickname);
977     int rc = sqlite3_step (this->pp);
978     if (verbose > 4)
979       obatched(clog) << nickname << " step(" << sqlite3_errstr(rc) << ") " << sql << endl;
980     return rc;
981   }
982 
~sqlite_pssqlite_ps983   ~sqlite_ps () { sqlite3_finalize (this->pp); }
operator sqlite3_stmt*sqlite_ps984   operator sqlite3_stmt* () { return this->pp; }
985 };
986 
987 
988 ////////////////////////////////////////////////////////////////////////
989 
990 // RAII style templated autocloser
991 
992 template <class Payload, class Ignore>
993 struct defer_dtor
994 {
995 public:
996   typedef Ignore (*dtor_fn) (Payload);
997 
998 private:
999   Payload p;
1000   dtor_fn fn;
1001 
1002 public:
defer_dtordefer_dtor1003   defer_dtor(Payload _p, dtor_fn _fn): p(_p), fn(_fn) {}
~defer_dtordefer_dtor1004   ~defer_dtor() { (void) (*fn)(p); }
1005 
1006 private:
1007   defer_dtor(const defer_dtor<Payload,Ignore>&); // make uncopyable
1008   defer_dtor& operator=(const defer_dtor<Payload,Ignore> &); // make unassignable
1009 };
1010 
1011 
1012 
1013 ////////////////////////////////////////////////////////////////////////
1014 
1015 
1016 static string
header_censor(const string & str)1017 header_censor(const string& str)
1018 {
1019   string y;
1020   for (auto&& x : str)
1021     {
1022       if (isalnum(x) || x == '/' || x == '.' || x == ',' || x == '_' || x == ':')
1023         y += x;
1024     }
1025   return y;
1026 }
1027 
1028 
1029 static string
conninfo(struct MHD_Connection * conn)1030 conninfo (struct MHD_Connection * conn)
1031 {
1032   char hostname[256]; // RFC1035
1033   char servname[256];
1034   int sts = -1;
1035 
1036   if (conn == 0)
1037     return "internal";
1038 
1039   /* Look up client address data. */
1040   const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
1041                                                                MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1042   struct sockaddr *so = u ? u->client_addr : 0;
1043 
1044   if (so && so->sa_family == AF_INET) {
1045     sts = getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), servname,
1046                        sizeof (servname), NI_NUMERICHOST | NI_NUMERICSERV);
1047   } else if (so && so->sa_family == AF_INET6) {
1048     sts = getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname),
1049                        servname, sizeof (servname), NI_NUMERICHOST | NI_NUMERICSERV);
1050   }
1051   if (sts != 0) {
1052     hostname[0] = servname[0] = '\0';
1053   }
1054 
1055   // extract headers relevant to administration
1056   const char* user_agent = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
1057   const char* x_forwarded_for = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
1058   // NB: these are untrustworthy, beware if machine-processing log files
1059 
1060   return string(hostname) + string(":") + string(servname) +
1061     string(" UA:") + header_censor(string(user_agent)) +
1062     string(" XFF:") + header_censor(string(x_forwarded_for));
1063 }
1064 
1065 
1066 
1067 ////////////////////////////////////////////////////////////////////////
1068 
1069 
1070 static void
add_mhd_last_modified(struct MHD_Response * resp,time_t mtime)1071 add_mhd_last_modified (struct MHD_Response *resp, time_t mtime)
1072 {
1073   struct tm *now = gmtime (&mtime);
1074   if (now != NULL)
1075     {
1076       char datebuf[80];
1077       size_t rc = strftime (datebuf, sizeof (datebuf), "%a, %d %b %Y %T GMT", now);
1078       if (rc > 0 && rc < sizeof (datebuf))
1079         (void) MHD_add_response_header (resp, "Last-Modified", datebuf);
1080     }
1081 
1082   (void) MHD_add_response_header (resp, "Cache-Control", "public");
1083 }
1084 
1085 
1086 
1087 static struct MHD_Response*
handle_buildid_f_match(bool internal_req_t,int64_t b_mtime,const string & b_source0,int * result_fd)1088 handle_buildid_f_match (bool internal_req_t,
1089                         int64_t b_mtime,
1090                         const string& b_source0,
1091                         int *result_fd)
1092 {
1093   (void) internal_req_t; // ignored
1094   int fd = open(b_source0.c_str(), O_RDONLY);
1095   if (fd < 0)
1096     throw libc_exception (errno, string("open ") + b_source0);
1097 
1098   // NB: use manual close(2) in error case instead of defer_dtor, because
1099   // in the normal case, we want to hand the fd over to libmicrohttpd for
1100   // file transfer.
1101 
1102   struct stat s;
1103   int rc = fstat(fd, &s);
1104   if (rc < 0)
1105     {
1106       close(fd);
1107       throw libc_exception (errno, string("fstat ") + b_source0);
1108     }
1109 
1110   if ((int64_t) s.st_mtime != b_mtime)
1111     {
1112       if (verbose)
1113         obatched(clog) << "mtime mismatch for " << b_source0 << endl;
1114       close(fd);
1115       return 0;
1116     }
1117 
1118   inc_metric ("http_responses_total","result","file");
1119   struct MHD_Response* r = MHD_create_response_from_fd ((uint64_t) s.st_size, fd);
1120   if (r == 0)
1121     {
1122       if (verbose)
1123         obatched(clog) << "cannot create fd-response for " << b_source0 << endl;
1124       close(fd);
1125     }
1126   else
1127     {
1128       MHD_add_response_header (r, "Content-Type", "application/octet-stream");
1129       std::string file = b_source0.substr(b_source0.find_last_of("/")+1, b_source0.length());
1130       MHD_add_response_header (r, "X-DEBUGINFOD-SIZE", to_string(s.st_size).c_str() );
1131       MHD_add_response_header (r, "X-DEBUGINFOD-FILE", file.c_str() );
1132       add_mhd_last_modified (r, s.st_mtime);
1133       if (verbose > 1)
1134         obatched(clog) << "serving file " << b_source0 << endl;
1135       /* libmicrohttpd will close it. */
1136       if (result_fd)
1137         *result_fd = fd;
1138     }
1139 
1140   return r;
1141 }
1142 
1143 
1144 // quote all questionable characters of str for safe passage through a sh -c expansion.
1145 static string
shell_escape(const string & str)1146 shell_escape(const string& str)
1147 {
1148   string y;
1149   for (auto&& x : str)
1150     {
1151       if (! isalnum(x) && x != '/')
1152         y += "\\";
1153       y += x;
1154     }
1155   return y;
1156 }
1157 
1158 
1159 // PR25548: Perform POSIX / RFC3986 style path canonicalization on the input string.
1160 //
1161 // Namely:
1162 //    //         ->   /
1163 //    /foo/../   ->   /
1164 //    /./        ->   /
1165 //
1166 // This mapping is done on dwarf-side source path names, which may
1167 // include these constructs, so we can deal with debuginfod clients
1168 // that accidentally canonicalize the paths.
1169 //
1170 // realpath(3) is close but not quite right, because it also resolves
1171 // symbolic links.  Symlinks at the debuginfod server have nothing to
1172 // do with the build-time symlinks, thus they must not be considered.
1173 //
1174 // see also curl Curl_dedotdotify() aka RFC3986, which we mostly follow here
1175 // see also libc __realpath()
1176 // see also llvm llvm::sys::path::remove_dots()
1177 static string
canon_pathname(const string & input)1178 canon_pathname (const string& input)
1179 {
1180   string i = input; // 5.2.4 (1)
1181   string o;
1182 
1183   while (i.size() != 0)
1184     {
1185       // 5.2.4 (2) A
1186       if (i.substr(0,3) == "../")
1187         i = i.substr(3);
1188       else if(i.substr(0,2) == "./")
1189         i = i.substr(2);
1190 
1191       // 5.2.4 (2) B
1192       else if (i.substr(0,3) == "/./")
1193         i = i.substr(2);
1194       else if (i == "/.")
1195         i = ""; // no need to handle "/." complete-path-segment case; we're dealing with file names
1196 
1197       // 5.2.4 (2) C
1198       else if (i.substr(0,4) == "/../") {
1199         i = i.substr(3);
1200         string::size_type sl = o.rfind("/");
1201         if (sl != string::npos)
1202           o = o.substr(0, sl);
1203         else
1204           o = "";
1205       } else if (i == "/..")
1206         i = ""; // no need to handle "/.." complete-path-segment case; we're dealing with file names
1207 
1208       // 5.2.4 (2) D
1209       // no need to handle these cases; we're dealing with file names
1210       else if (i == ".")
1211         i = "";
1212       else if (i == "..")
1213         i = "";
1214 
1215       // POSIX special: map // to /
1216       else if (i.substr(0,2) == "//")
1217         i = i.substr(1);
1218 
1219       // 5.2.4 (2) E
1220       else {
1221         string::size_type next_slash = i.find("/", (i[0]=='/' ? 1 : 0)); // skip first slash
1222         o += i.substr(0, next_slash);
1223         if (next_slash == string::npos)
1224           i = "";
1225         else
1226           i = i.substr(next_slash);
1227       }
1228     }
1229 
1230   return o;
1231 }
1232 
1233 
1234 // Estimate available free space for a given filesystem via statfs(2).
1235 // Return true if the free fraction is known to be smaller than the
1236 // given minimum percentage.  Also update a related metric.
statfs_free_enough_p(const string & path,const string & label,long minfree=0)1237 bool statfs_free_enough_p(const string& path, const string& label, long minfree = 0)
1238 {
1239   struct statfs sfs;
1240   int rc = statfs(path.c_str(), &sfs);
1241   if (rc == 0)
1242     {
1243       double s = (double) sfs.f_bavail / (double) sfs.f_blocks;
1244       set_metric("filesys_free_ratio","purpose",label, s);
1245       return ((s * 100.0) < minfree);
1246     }
1247   return false;
1248 }
1249 
1250 
1251 
1252 // A map-like class that owns a cache of file descriptors (indexed by
1253 // file / content names).
1254 //
1255 // If only it could use fd's instead of file names ... but we can't
1256 // dup(2) to create independent descriptors for the same unlinked
1257 // files, so would have to use some goofy linux /proc/self/fd/%d
1258 // hack such as the following
1259 
1260 #if 0
1261 int superdup(int fd)
1262 {
1263 #ifdef __linux__
1264   char *fdpath = NULL;
1265   int rc = asprintf(& fdpath, "/proc/self/fd/%d", fd);
1266   int newfd;
1267   if (rc >= 0)
1268     newfd = open(fdpath, O_RDONLY);
1269   else
1270     newfd = -1;
1271   free (fdpath);
1272   return newfd;
1273 #else
1274   return -1;
1275 #endif
1276 }
1277 #endif
1278 
1279 class libarchive_fdcache
1280 {
1281 private:
1282   mutex fdcache_lock;
1283 
1284   struct fdcache_entry
1285   {
1286     string archive;
1287     string entry;
1288     string fd;
1289     double fd_size_mb; // slightly rounded up megabytes
1290   };
1291   deque<fdcache_entry> lru; // @head: most recently used
1292   long max_fds;
1293   deque<fdcache_entry> prefetch; // prefetched
1294   long max_mbs;
1295   long max_prefetch_mbs;
1296   long max_prefetch_fds;
1297 
1298 public:
set_metrics()1299   void set_metrics()
1300   {
1301     double fdcache_mb = 0.0;
1302     double prefetch_mb = 0.0;
1303     for (auto i = lru.begin(); i < lru.end(); i++)
1304       fdcache_mb += i->fd_size_mb;
1305     for (auto j = prefetch.begin(); j < prefetch.end(); j++)
1306       prefetch_mb += j->fd_size_mb;
1307     set_metric("fdcache_bytes", fdcache_mb*1024.0*1024.0);
1308     set_metric("fdcache_count", lru.size());
1309     set_metric("fdcache_prefetch_bytes", prefetch_mb*1024.0*1024.0);
1310     set_metric("fdcache_prefetch_count", prefetch.size());
1311   }
1312 
intern(const string & a,const string & b,string fd,off_t sz,bool front_p)1313   void intern(const string& a, const string& b, string fd, off_t sz, bool front_p)
1314   {
1315     {
1316       unique_lock<mutex> lock(fdcache_lock);
1317       // nuke preexisting copy
1318       for (auto i = lru.begin(); i < lru.end(); i++)
1319         {
1320           if (i->archive == a && i->entry == b)
1321             {
1322               unlink (i->fd.c_str());
1323               lru.erase(i);
1324               inc_metric("fdcache_op_count","op","dequeue");
1325               break; // must not continue iterating
1326             }
1327         }
1328       // nuke preexisting copy in prefetch
1329       for (auto i = prefetch.begin(); i < prefetch.end(); i++)
1330         {
1331           if (i->archive == a && i->entry == b)
1332             {
1333               unlink (i->fd.c_str());
1334               prefetch.erase(i);
1335               inc_metric("fdcache_op_count","op","prefetch_dequeue");
1336               break; // must not continue iterating
1337             }
1338         }
1339       double mb = (sz+65535)/1048576.0; // round up to 64K block
1340       fdcache_entry n = { a, b, fd, mb };
1341       if (front_p)
1342         {
1343           inc_metric("fdcache_op_count","op","enqueue");
1344           lru.push_front(n);
1345         }
1346       else
1347         {
1348           inc_metric("fdcache_op_count","op","prefetch_enqueue");
1349           prefetch.push_front(n);
1350         }
1351       if (verbose > 3)
1352         obatched(clog) << "fdcache interned a=" << a << " b=" << b
1353                        << " fd=" << fd << " mb=" << mb << " front=" << front_p << endl;
1354     }
1355     set_metrics();
1356 
1357     // NB: we age the cache at lookup time too
1358     if (statfs_free_enough_p(tmpdir, "tmpdir", fdcache_mintmp))
1359       {
1360         inc_metric("fdcache_op_count","op","emerg-flush");
1361         obatched(clog) << "fdcache emergency flush for filling tmpdir" << endl;
1362         this->limit(0, 0, 0, 0); // emergency flush
1363       }
1364     else if (front_p)
1365       this->limit(max_fds, max_mbs, max_prefetch_fds, max_prefetch_mbs); // age cache if required
1366   }
1367 
lookup(const string & a,const string & b)1368   int lookup(const string& a, const string& b)
1369   {
1370     int fd = -1;
1371     {
1372       unique_lock<mutex> lock(fdcache_lock);
1373       for (auto i = lru.begin(); i < lru.end(); i++)
1374         {
1375           if (i->archive == a && i->entry == b)
1376             { // found it; move it to head of lru
1377               fdcache_entry n = *i;
1378               lru.erase(i); // invalidates i, so no more iteration!
1379               lru.push_front(n);
1380               inc_metric("fdcache_op_count","op","requeue_front");
1381               fd = open(n.fd.c_str(), O_RDONLY);
1382               break;
1383             }
1384         }
1385       // Iterate through prefetch while fd == -1 to ensure that no duplication between lru and
1386       // prefetch occurs.
1387       for ( auto i = prefetch.begin(); fd == -1 && i < prefetch.end(); ++i)
1388         {
1389           if (i->archive == a && i->entry == b)
1390             { // found it; take the entry from the prefetch deque to the lru deque, since it has now been accessed.
1391               fdcache_entry n = *i;
1392               prefetch.erase(i);
1393               lru.push_front(n);
1394               inc_metric("fdcache_op_count","op","prefetch_access");
1395               fd = open(n.fd.c_str(), O_RDONLY);
1396               break;
1397             }
1398         }
1399     }
1400 
1401     if (statfs_free_enough_p(tmpdir, "tmpdir", fdcache_mintmp))
1402       {
1403         inc_metric("fdcache_op_count","op","emerg-flush");
1404         obatched(clog) << "fdcache emergency flush for filling tmpdir" << endl;
1405         this->limit(0, 0, 0, 0); // emergency flush
1406       }
1407     else if (fd >= 0)
1408       this->limit(max_fds, max_mbs, max_prefetch_fds, max_prefetch_mbs); // age cache if required
1409 
1410     return fd;
1411   }
1412 
probe(const string & a,const string & b)1413   int probe(const string& a, const string& b) // just a cache residency check - don't modify LRU state, don't open
1414   {
1415     unique_lock<mutex> lock(fdcache_lock);
1416     for (auto i = lru.begin(); i < lru.end(); i++)
1417       {
1418         if (i->archive == a && i->entry == b)
1419           {
1420             inc_metric("fdcache_op_count","op","probe_hit");
1421             return true;
1422           }
1423       }
1424     for (auto i = prefetch.begin(); i < prefetch.end(); i++)
1425       {
1426         if (i->archive == a && i->entry == b)
1427           {
1428             inc_metric("fdcache_op_count","op","prefetch_probe_hit");
1429             return true;
1430           }
1431       }
1432     inc_metric("fdcache_op_count","op","probe_miss");
1433     return false;
1434   }
1435 
clear(const string & a,const string & b)1436   void clear(const string& a, const string& b)
1437   {
1438     unique_lock<mutex> lock(fdcache_lock);
1439     for (auto i = lru.begin(); i < lru.end(); i++)
1440       {
1441         if (i->archive == a && i->entry == b)
1442           { // found it; erase it from lru
1443             fdcache_entry n = *i;
1444             lru.erase(i); // invalidates i, so no more iteration!
1445             inc_metric("fdcache_op_count","op","clear");
1446             unlink (n.fd.c_str());
1447             set_metrics();
1448             return;
1449           }
1450       }
1451     for (auto i = prefetch.begin(); i < prefetch.end(); i++)
1452       {
1453         if (i->archive == a && i->entry == b)
1454           { // found it; erase it from lru
1455             fdcache_entry n = *i;
1456             prefetch.erase(i); // invalidates i, so no more iteration!
1457             inc_metric("fdcache_op_count","op","prefetch_clear");
1458             unlink (n.fd.c_str());
1459             set_metrics();
1460             return;
1461           }
1462       }
1463   }
1464 
limit(long maxfds,long maxmbs,long maxprefetchfds,long maxprefetchmbs,bool metrics_p=true)1465   void limit(long maxfds, long maxmbs, long maxprefetchfds, long maxprefetchmbs , bool metrics_p = true)
1466   {
1467     if (verbose > 3 && (this->max_fds != maxfds || this->max_mbs != maxmbs))
1468       obatched(clog) << "fdcache limited to maxfds=" << maxfds << " maxmbs=" << maxmbs << endl;
1469 
1470     unique_lock<mutex> lock(fdcache_lock);
1471     this->max_fds = maxfds;
1472     this->max_mbs = maxmbs;
1473     this->max_prefetch_fds = maxprefetchfds;
1474     this->max_prefetch_mbs = maxprefetchmbs;
1475     long total_fd = 0;
1476     double total_mb = 0.0;
1477     for (auto i = lru.begin(); i < lru.end(); i++)
1478       {
1479         // accumulate totals from most recently used one going backward
1480         total_fd ++;
1481         total_mb += i->fd_size_mb;
1482         if (total_fd > this->max_fds || total_mb > this->max_mbs)
1483           {
1484             // found the cut here point!
1485 
1486             for (auto j = i; j < lru.end(); j++) // close all the fds from here on in
1487               {
1488                 if (verbose > 3)
1489                   obatched(clog) << "fdcache evicted a=" << j->archive << " b=" << j->entry
1490                                  << " fd=" << j->fd << " mb=" << j->fd_size_mb << endl;
1491                 if (metrics_p)
1492                   inc_metric("fdcache_op_count","op","evict");
1493                 unlink (j->fd.c_str());
1494               }
1495 
1496             lru.erase(i, lru.end()); // erase the nodes generally
1497             break;
1498           }
1499       }
1500     total_fd = 0;
1501     total_mb = 0.0;
1502     for(auto i = prefetch.begin(); i < prefetch.end(); i++){
1503       // accumulate totals from most recently used one going backward
1504         total_fd ++;
1505         total_mb += i->fd_size_mb;
1506         if (total_fd > this->max_prefetch_fds || total_mb > this->max_prefetch_mbs)
1507           {
1508             // found the cut here point!
1509             for (auto j = i; j < prefetch.end(); j++) // close all the fds from here on in
1510               {
1511                 if (verbose > 3)
1512                   obatched(clog) << "fdcache evicted from prefetch a=" << j->archive << " b=" << j->entry
1513                                  << " fd=" << j->fd << " mb=" << j->fd_size_mb << endl;
1514                 if (metrics_p)
1515                   inc_metric("fdcache_op_count","op","prefetch_evict");
1516                 unlink (j->fd.c_str());
1517               }
1518 
1519             prefetch.erase(i, prefetch.end()); // erase the nodes generally
1520             break;
1521           }
1522     }
1523     if (metrics_p) set_metrics();
1524   }
1525 
1526 
~libarchive_fdcache()1527   ~libarchive_fdcache()
1528   {
1529     // unlink any fdcache entries in $TMPDIR
1530     // don't update metrics; those globals may be already destroyed
1531     limit(0, 0, 0, 0, false);
1532   }
1533 };
1534 static libarchive_fdcache fdcache;
1535 
1536 
1537 // For security/portability reasons, many distro-package archives have
1538 // a "./" in front of path names; others have nothing, others have
1539 // "/".  Canonicalize them all to a single leading "/", with the
1540 // assumption that this matches the dwarf-derived file names too.
canonicalized_archive_entry_pathname(struct archive_entry * e)1541 string canonicalized_archive_entry_pathname(struct archive_entry *e)
1542 {
1543   string fn = archive_entry_pathname(e);
1544   if (fn.size() == 0)
1545     return fn;
1546   if (fn[0] == '/')
1547     return fn;
1548   if (fn[0] == '.')
1549     return fn.substr(1);
1550   else
1551     return string("/")+fn;
1552 }
1553 
1554 
1555 
1556 static struct MHD_Response*
handle_buildid_r_match(bool internal_req_p,int64_t b_mtime,const string & b_source0,const string & b_source1,int * result_fd)1557 handle_buildid_r_match (bool internal_req_p,
1558                         int64_t b_mtime,
1559                         const string& b_source0,
1560                         const string& b_source1,
1561                         int *result_fd)
1562 {
1563   struct stat fs;
1564   int rc = stat (b_source0.c_str(), &fs);
1565   if (rc != 0)
1566     throw libc_exception (errno, string("stat ") + b_source0);
1567 
1568   if ((int64_t) fs.st_mtime != b_mtime)
1569     {
1570       if (verbose)
1571         obatched(clog) << "mtime mismatch for " << b_source0 << endl;
1572       return 0;
1573     }
1574 
1575   // check for a match in the fdcache first
1576   int fd = fdcache.lookup(b_source0, b_source1);
1577   while (fd >= 0) // got one!; NB: this is really an if() with a possible branch out to the end
1578     {
1579       rc = fstat(fd, &fs);
1580       if (rc < 0) // disappeared?
1581         {
1582           if (verbose)
1583             obatched(clog) << "cannot fstat fdcache " << b_source0 << endl;
1584           close(fd);
1585           fdcache.clear(b_source0, b_source1);
1586           break; // branch out of if "loop", to try new libarchive fetch attempt
1587         }
1588 
1589       struct MHD_Response* r = MHD_create_response_from_fd (fs.st_size, fd);
1590       if (r == 0)
1591         {
1592           if (verbose)
1593             obatched(clog) << "cannot create fd-response for " << b_source0 << endl;
1594           close(fd);
1595           break; // branch out of if "loop", to try new libarchive fetch attempt
1596         }
1597 
1598       inc_metric ("http_responses_total","result","archive fdcache");
1599 
1600       MHD_add_response_header (r, "Content-Type", "application/octet-stream");
1601       MHD_add_response_header (r, "X-DEBUGINFOD-SIZE", to_string(fs.st_size).c_str());
1602       MHD_add_response_header (r, "X-DEBUGINFOD-ARCHIVE", b_source0.c_str());
1603       MHD_add_response_header (r, "X-DEBUGINFOD-FILE", b_source1.c_str());
1604       add_mhd_last_modified (r, fs.st_mtime);
1605       if (verbose > 1)
1606         obatched(clog) << "serving fdcache archive " << b_source0 << " file " << b_source1 << endl;
1607       /* libmicrohttpd will close it. */
1608       if (result_fd)
1609         *result_fd = fd;
1610       return r;
1611       // NB: see, we never go around the 'loop' more than once
1612     }
1613 
1614   // no match ... grumble, must process the archive
1615   string archive_decoder = "/dev/null";
1616   string archive_extension = "";
1617   for (auto&& arch : scan_archives)
1618     if (string_endswith(b_source0, arch.first))
1619       {
1620         archive_extension = arch.first;
1621         archive_decoder = arch.second;
1622       }
1623   FILE* fp;
1624   defer_dtor<FILE*,int>::dtor_fn dfn;
1625   if (archive_decoder != "cat")
1626     {
1627       string popen_cmd = archive_decoder + " " + shell_escape(b_source0);
1628       fp = popen (popen_cmd.c_str(), "r"); // "e" O_CLOEXEC?
1629       dfn = pclose;
1630       if (fp == NULL)
1631         throw libc_exception (errno, string("popen ") + popen_cmd);
1632     }
1633   else
1634     {
1635       fp = fopen (b_source0.c_str(), "r");
1636       dfn = fclose;
1637       if (fp == NULL)
1638         throw libc_exception (errno, string("fopen ") + b_source0);
1639     }
1640   defer_dtor<FILE*,int> fp_closer (fp, dfn);
1641 
1642   struct archive *a;
1643   a = archive_read_new();
1644   if (a == NULL)
1645     throw archive_exception("cannot create archive reader");
1646   defer_dtor<struct archive*,int> archive_closer (a, archive_read_free);
1647 
1648   rc = archive_read_support_format_all(a);
1649   if (rc != ARCHIVE_OK)
1650     throw archive_exception(a, "cannot select all format");
1651   rc = archive_read_support_filter_all(a);
1652   if (rc != ARCHIVE_OK)
1653     throw archive_exception(a, "cannot select all filters");
1654 
1655   rc = archive_read_open_FILE (a, fp);
1656   if (rc != ARCHIVE_OK)
1657     throw archive_exception(a, "cannot open archive from pipe");
1658 
1659   // archive traversal is in three stages, no, four stages:
1660   // 1) skip entries whose names do not match the requested one
1661   // 2) extract the matching entry name (set r = result)
1662   // 3) extract some number of prefetched entries (just into fdcache)
1663   // 4) abort any further processing
1664   struct MHD_Response* r = 0;                 // will set in stage 2
1665   unsigned prefetch_count =
1666     internal_req_p ? 0 : fdcache_prefetch;    // will decrement in stage 3
1667 
1668   while(r == 0 || prefetch_count > 0) // stage 1, 2, or 3
1669     {
1670       if (interrupted)
1671         break;
1672 
1673       struct archive_entry *e;
1674       rc = archive_read_next_header (a, &e);
1675       if (rc != ARCHIVE_OK)
1676         break;
1677 
1678       if (! S_ISREG(archive_entry_mode (e))) // skip non-files completely
1679         continue;
1680 
1681       string fn = canonicalized_archive_entry_pathname (e);
1682       if ((r == 0) && (fn != b_source1)) // stage 1
1683         continue;
1684 
1685       if (fdcache.probe (b_source0, fn)) // skip if already interned
1686         continue;
1687 
1688       // extract this file to a temporary file
1689       char* tmppath = NULL;
1690       rc = asprintf (&tmppath, "%s/debuginfod.XXXXXX", tmpdir.c_str());
1691       if (rc < 0)
1692         throw libc_exception (ENOMEM, "cannot allocate tmppath");
1693       defer_dtor<void*,void> tmmpath_freer (tmppath, free);
1694       fd = mkstemp (tmppath);
1695       if (fd < 0)
1696         throw libc_exception (errno, "cannot create temporary file");
1697       // NB: don't unlink (tmppath), as fdcache will take charge of it.
1698 
1699       // NB: this can take many uninterruptible seconds for a huge file
1700       rc = archive_read_data_into_fd (a, fd);
1701       if (rc != ARCHIVE_OK) // e.g. ENOSPC!
1702         {
1703           close (fd);
1704           unlink (tmppath);
1705           throw archive_exception(a, "cannot extract file");
1706         }
1707 
1708       // Set the mtime so the fdcache file mtimes, even prefetched ones,
1709       // propagate to future webapi clients.
1710       struct timeval tvs[2];
1711       tvs[0].tv_sec = tvs[1].tv_sec = archive_entry_mtime(e);
1712       tvs[0].tv_usec = tvs[1].tv_usec = 0;
1713       (void) futimes (fd, tvs);  /* best effort */
1714 
1715       if (r != 0) // stage 3
1716         {
1717           // NB: now we know we have a complete reusable file; make fdcache
1718           // responsible for unlinking it later.
1719           fdcache.intern(b_source0, fn,
1720                          tmppath, archive_entry_size(e),
1721                          false); // prefetched ones go to the prefetch cache
1722           prefetch_count --;
1723           close (fd); // we're not saving this fd to make a mhd-response from!
1724           continue;
1725         }
1726 
1727       // NB: now we know we have a complete reusable file; make fdcache
1728       // responsible for unlinking it later.
1729       fdcache.intern(b_source0, b_source1,
1730                      tmppath, archive_entry_size(e),
1731                      true); // requested ones go to the front of lru
1732 
1733       inc_metric ("http_responses_total","result",archive_extension + " archive");
1734       r = MHD_create_response_from_fd (archive_entry_size(e), fd);
1735       if (r == 0)
1736         {
1737           if (verbose)
1738             obatched(clog) << "cannot create fd-response for " << b_source0 << endl;
1739           close(fd);
1740           break; // assume no chance of better luck around another iteration; no other copies of same file
1741         }
1742       else
1743         {
1744           MHD_add_response_header (r, "Content-Type", "application/octet-stream");
1745           std::string file = b_source1.substr(b_source1.find_last_of("/")+1, b_source1.length());
1746           MHD_add_response_header (r, "X-DEBUGINFOD-SIZE", to_string(fs.st_size).c_str());
1747           MHD_add_response_header (r, "X-DEBUGINFOD-ARCHIVE", b_source0.c_str());
1748           MHD_add_response_header (r, "X-DEBUGINFOD-FILE", file.c_str());
1749 
1750           add_mhd_last_modified (r, archive_entry_mtime(e));
1751           if (verbose > 1)
1752             obatched(clog) << "serving archive " << b_source0 << " file " << b_source1 << endl;
1753           /* libmicrohttpd will close it. */
1754           if (result_fd)
1755             *result_fd = fd;
1756           continue;
1757         }
1758     }
1759 
1760   // XXX: rpm/file not found: delete this R entry?
1761   return r;
1762 }
1763 
1764 
1765 static struct MHD_Response*
handle_buildid_match(bool internal_req_p,int64_t b_mtime,const string & b_stype,const string & b_source0,const string & b_source1,int * result_fd)1766 handle_buildid_match (bool internal_req_p,
1767                       int64_t b_mtime,
1768                       const string& b_stype,
1769                       const string& b_source0,
1770                       const string& b_source1,
1771                       int *result_fd)
1772 {
1773   try
1774     {
1775       if (b_stype == "F")
1776         return handle_buildid_f_match(internal_req_p, b_mtime, b_source0, result_fd);
1777       else if (b_stype == "R")
1778         return handle_buildid_r_match(internal_req_p, b_mtime, b_source0, b_source1, result_fd);
1779     }
1780   catch (const reportable_exception &e)
1781     {
1782       e.report(clog);
1783       // Report but swallow libc etc. errors here; let the caller
1784       // iterate to other matches of the content.
1785     }
1786 
1787   return 0;
1788 }
1789 
1790 
1791 static int
debuginfod_find_progress(debuginfod_client *,long a,long b)1792 debuginfod_find_progress (debuginfod_client *, long a, long b)
1793 {
1794   if (verbose > 4)
1795     obatched(clog) << "federated debuginfod progress=" << a << "/" << b << endl;
1796 
1797   return interrupted;
1798 }
1799 
1800 
1801 // a little lru pool of debuginfod_client*s for reuse between query threads
1802 
1803 mutex dc_pool_lock;
1804 deque<debuginfod_client*> dc_pool;
1805 
debuginfod_pool_begin()1806 debuginfod_client* debuginfod_pool_begin()
1807 {
1808   unique_lock<mutex> lock(dc_pool_lock);
1809   if (dc_pool.size() > 0)
1810     {
1811       inc_metric("dc_pool_op_count","op","begin-reuse");
1812       debuginfod_client *c = dc_pool.front();
1813       dc_pool.pop_front();
1814       return c;
1815     }
1816   inc_metric("dc_pool_op_count","op","begin-new");
1817   return debuginfod_begin();
1818 }
1819 
1820 
debuginfod_pool_groom()1821 void debuginfod_pool_groom()
1822 {
1823   unique_lock<mutex> lock(dc_pool_lock);
1824   while (dc_pool.size() > 0)
1825     {
1826       inc_metric("dc_pool_op_count","op","end");
1827       debuginfod_end(dc_pool.front());
1828       dc_pool.pop_front();
1829     }
1830 }
1831 
1832 
debuginfod_pool_end(debuginfod_client * c)1833 void debuginfod_pool_end(debuginfod_client* c)
1834 {
1835   unique_lock<mutex> lock(dc_pool_lock);
1836   inc_metric("dc_pool_op_count","op","end-save");
1837   dc_pool.push_front(c); // accelerate reuse, vs. push_back
1838 }
1839 
1840 
1841 static struct MHD_Response*
handle_buildid(MHD_Connection * conn,const string & buildid,string & artifacttype,const string & suffix,int * result_fd)1842 handle_buildid (MHD_Connection* conn,
1843                 const string& buildid /* unsafe */,
1844                 string& artifacttype /* unsafe, cleanse on exception/return */,
1845                 const string& suffix /* unsafe */,
1846                 int *result_fd)
1847 {
1848   // validate artifacttype
1849   string atype_code;
1850   if (artifacttype == "debuginfo") atype_code = "D";
1851   else if (artifacttype == "executable") atype_code = "E";
1852   else if (artifacttype == "source") atype_code = "S";
1853   else {
1854     artifacttype = "invalid"; // PR28242 ensure http_resposes metrics don't propagate unclean user data
1855     throw reportable_exception("invalid artifacttype");
1856   }
1857 
1858   inc_metric("http_requests_total", "type", artifacttype);
1859 
1860   if (atype_code == "S" && suffix == "")
1861      throw reportable_exception("invalid source suffix");
1862 
1863   // validate buildid
1864   if ((buildid.size() < 2) || // not empty
1865       (buildid.size() % 2) || // even number
1866       (buildid.find_first_not_of("0123456789abcdef") != string::npos)) // pure tasty lowercase hex
1867     throw reportable_exception("invalid buildid");
1868 
1869   if (verbose > 1)
1870     obatched(clog) << "searching for buildid=" << buildid << " artifacttype=" << artifacttype
1871          << " suffix=" << suffix << endl;
1872 
1873   // If invoked from the scanner threads, use the scanners' read-write
1874   // connection.  Otherwise use the web query threads' read-only connection.
1875   sqlite3 *thisdb = (conn == 0) ? db : dbq;
1876 
1877   sqlite_ps *pp = 0;
1878 
1879   if (atype_code == "D")
1880     {
1881       pp = new sqlite_ps (thisdb, "mhd-query-d",
1882                           "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_d where buildid = ? "
1883                           "order by mtime desc");
1884       pp->reset();
1885       pp->bind(1, buildid);
1886     }
1887   else if (atype_code == "E")
1888     {
1889       pp = new sqlite_ps (thisdb, "mhd-query-e",
1890                           "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_e where buildid = ? "
1891                           "order by mtime desc");
1892       pp->reset();
1893       pp->bind(1, buildid);
1894     }
1895   else if (atype_code == "S")
1896     {
1897       // PR25548
1898       // Incoming source queries may come in with either dwarf-level OR canonicalized paths.
1899       // We let the query pass with either one.
1900 
1901       pp = new sqlite_ps (thisdb, "mhd-query-s",
1902                           "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_s where buildid = ? and artifactsrc in (?,?) "
1903                           "order by sharedprefix(source0,source0ref) desc, mtime desc");
1904       pp->reset();
1905       pp->bind(1, buildid);
1906       // NB: we don't store the non-canonicalized path names any more, but old databases
1907       // might have them (and no canon ones), so we keep searching for both.
1908       pp->bind(2, suffix);
1909       pp->bind(3, canon_pathname(suffix));
1910     }
1911   unique_ptr<sqlite_ps> ps_closer(pp); // release pp if exception or return
1912 
1913   // consume all the rows
1914   while (1)
1915     {
1916       int rc = pp->step();
1917       if (rc == SQLITE_DONE) break;
1918       if (rc != SQLITE_ROW)
1919         throw sqlite_exception(rc, "step");
1920 
1921       int64_t b_mtime = sqlite3_column_int64 (*pp, 0);
1922       string b_stype = string((const char*) sqlite3_column_text (*pp, 1) ?: ""); /* by DDL may not be NULL */
1923       string b_source0 = string((const char*) sqlite3_column_text (*pp, 2) ?: ""); /* may be NULL */
1924       string b_source1 = string((const char*) sqlite3_column_text (*pp, 3) ?: ""); /* may be NULL */
1925 
1926       if (verbose > 1)
1927         obatched(clog) << "found mtime=" << b_mtime << " stype=" << b_stype
1928              << " source0=" << b_source0 << " source1=" << b_source1 << endl;
1929 
1930       // Try accessing the located match.
1931       // XXX: in case of multiple matches, attempt them in parallel?
1932       auto r = handle_buildid_match (conn ? false : true,
1933                                      b_mtime, b_stype, b_source0, b_source1, result_fd);
1934       if (r)
1935         return r;
1936     }
1937   pp->reset();
1938 
1939   // We couldn't find it in the database.  Last ditch effort
1940   // is to defer to other debuginfo servers.
1941 
1942   int fd = -1;
1943   debuginfod_client *client = debuginfod_pool_begin ();
1944   if (client != NULL)
1945     {
1946       debuginfod_set_progressfn (client, & debuginfod_find_progress);
1947 
1948       if (conn)
1949         {
1950           // Transcribe incoming User-Agent:
1951           string ua = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
1952           string ua_complete = string("User-Agent: ") + ua;
1953           debuginfod_add_http_header (client, ua_complete.c_str());
1954 
1955           // Compute larger XFF:, for avoiding info loss during
1956           // federation, and for future cyclicity detection.
1957           string xff = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
1958           if (xff != "")
1959             xff += string(", "); // comma separated list
1960 
1961           unsigned int xff_count = 0;
1962           for (auto&& i : xff){
1963             if (i == ',') xff_count++;
1964           }
1965 
1966           // if X-Forwarded-For: exceeds N hops,
1967           // do not delegate a local lookup miss to upstream debuginfods.
1968           if (xff_count >= forwarded_ttl_limit)
1969             throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found, --forwared-ttl-limit reached \
1970 and will not query the upstream servers");
1971 
1972           // Compute the client's numeric IP address only - so can't merge with conninfo()
1973           const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
1974                                                                        MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1975           struct sockaddr *so = u ? u->client_addr : 0;
1976           char hostname[256] = ""; // RFC1035
1977           if (so && so->sa_family == AF_INET)
1978             (void) getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), NULL, 0,
1979                                 NI_NUMERICHOST);
1980           else if (so && so->sa_family == AF_INET6)
1981             (void) getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname), NULL, 0,
1982                                 NI_NUMERICHOST);
1983 
1984           string xff_complete = string("X-Forwarded-For: ")+xff+string(hostname);
1985           debuginfod_add_http_header (client, xff_complete.c_str());
1986         }
1987 
1988       if (artifacttype == "debuginfo")
1989 	fd = debuginfod_find_debuginfo (client,
1990 					(const unsigned char*) buildid.c_str(),
1991 					0, NULL);
1992       else if (artifacttype == "executable")
1993 	fd = debuginfod_find_executable (client,
1994 					 (const unsigned char*) buildid.c_str(),
1995 					 0, NULL);
1996       else if (artifacttype == "source")
1997 	fd = debuginfod_find_source (client,
1998 				     (const unsigned char*) buildid.c_str(),
1999 				     0, suffix.c_str(), NULL);
2000     }
2001   else
2002     fd = -errno; /* Set by debuginfod_begin.  */
2003   debuginfod_pool_end (client);
2004 
2005   if (fd >= 0)
2006     {
2007       inc_metric ("http_responses_total","result","upstream");
2008       struct stat s;
2009       int rc = fstat (fd, &s);
2010       if (rc == 0)
2011         {
2012           auto r = MHD_create_response_from_fd ((uint64_t) s.st_size, fd);
2013           if (r)
2014             {
2015               MHD_add_response_header (r, "Content-Type", "application/octet-stream");
2016               add_mhd_last_modified (r, s.st_mtime);
2017               if (verbose > 1)
2018                 obatched(clog) << "serving file from upstream debuginfod/cache" << endl;
2019               if (result_fd)
2020                 *result_fd = fd;
2021               return r; // NB: don't close fd; libmicrohttpd will
2022             }
2023         }
2024       close (fd);
2025     }
2026   else
2027     switch(fd)
2028       {
2029       case -ENOSYS:
2030         break;
2031       case -ENOENT:
2032         break;
2033       default: // some more tricky error
2034         throw libc_exception(-fd, "upstream debuginfod query failed");
2035       }
2036 
2037   throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found");
2038 }
2039 
2040 
2041 ////////////////////////////////////////////////////////////////////////
2042 
2043 static map<string,double> metrics; // arbitrary data for /metrics query
2044 // NB: store int64_t since all our metrics are integers; prometheus accepts double
2045 static mutex metrics_lock;
2046 // NB: these objects get released during the process exit via global dtors
2047 // do not call them from within other global dtors
2048 
2049 // utility function for assembling prometheus-compatible
2050 // name="escaped-value" strings
2051 // https://prometheus.io/docs/instrumenting/exposition_formats/
2052 static string
metric_label(const string & name,const string & value)2053 metric_label(const string& name, const string& value)
2054 {
2055   string x = name + "=\"";
2056   for (auto&& c : value)
2057     switch(c)
2058       {
2059       case '\\': x += "\\\\"; break;
2060       case '\"': x += "\\\""; break;
2061       case '\n': x += "\\n"; break;
2062       default: x += c; break;
2063       }
2064   x += "\"";
2065   return x;
2066 }
2067 
2068 
2069 // add prometheus-format metric name + label tuple (if any) + value
2070 
2071 static void
set_metric(const string & metric,double value)2072 set_metric(const string& metric, double value)
2073 {
2074   unique_lock<mutex> lock(metrics_lock);
2075   metrics[metric] = value;
2076 }
2077 #if 0 /* unused */
2078 static void
2079 inc_metric(const string& metric)
2080 {
2081   unique_lock<mutex> lock(metrics_lock);
2082   metrics[metric] ++;
2083 }
2084 #endif
2085 static void
set_metric(const string & metric,const string & lname,const string & lvalue,double value)2086 set_metric(const string& metric,
2087            const string& lname, const string& lvalue,
2088            double value)
2089 {
2090   string key = (metric + "{" + metric_label(lname, lvalue) + "}");
2091   unique_lock<mutex> lock(metrics_lock);
2092   metrics[key] = value;
2093 }
2094 
2095 static void
inc_metric(const string & metric,const string & lname,const string & lvalue)2096 inc_metric(const string& metric,
2097            const string& lname, const string& lvalue)
2098 {
2099   string key = (metric + "{" + metric_label(lname, lvalue) + "}");
2100   unique_lock<mutex> lock(metrics_lock);
2101   metrics[key] ++;
2102 }
2103 static void
add_metric(const string & metric,const string & lname,const string & lvalue,double value)2104 add_metric(const string& metric,
2105            const string& lname, const string& lvalue,
2106            double value)
2107 {
2108   string key = (metric + "{" + metric_label(lname, lvalue) + "}");
2109   unique_lock<mutex> lock(metrics_lock);
2110   metrics[key] += value;
2111 }
2112 #if 0
2113 static void
2114 add_metric(const string& metric,
2115            double value)
2116 {
2117   unique_lock<mutex> lock(metrics_lock);
2118   metrics[metric] += value;
2119 }
2120 #endif
2121 
2122 
2123 // and more for higher arity labels if needed
2124 
2125 static void
inc_metric(const string & metric,const string & lname,const string & lvalue,const string & rname,const string & rvalue)2126 inc_metric(const string& metric,
2127            const string& lname, const string& lvalue,
2128            const string& rname, const string& rvalue)
2129 {
2130   string key = (metric + "{"
2131                 + metric_label(lname, lvalue) + ","
2132                 + metric_label(rname, rvalue) + "}");
2133   unique_lock<mutex> lock(metrics_lock);
2134   metrics[key] ++;
2135 }
2136 static void
add_metric(const string & metric,const string & lname,const string & lvalue,const string & rname,const string & rvalue,double value)2137 add_metric(const string& metric,
2138            const string& lname, const string& lvalue,
2139            const string& rname, const string& rvalue,
2140            double value)
2141 {
2142   string key = (metric + "{"
2143                 + metric_label(lname, lvalue) + ","
2144                 + metric_label(rname, rvalue) + "}");
2145   unique_lock<mutex> lock(metrics_lock);
2146   metrics[key] += value;
2147 }
2148 
2149 static struct MHD_Response*
handle_metrics(off_t * size)2150 handle_metrics (off_t* size)
2151 {
2152   stringstream o;
2153   {
2154     unique_lock<mutex> lock(metrics_lock);
2155     for (auto&& i : metrics)
2156       o << i.first
2157         << " "
2158         << std::setprecision(std::numeric_limits<double>::digits10 + 1)
2159         << i.second
2160         << endl;
2161   }
2162   const string& os = o.str();
2163   MHD_Response* r = MHD_create_response_from_buffer (os.size(),
2164                                                      (void*) os.c_str(),
2165                                                      MHD_RESPMEM_MUST_COPY);
2166   *size = os.size();
2167   MHD_add_response_header (r, "Content-Type", "text/plain");
2168   return r;
2169 }
2170 
2171 static struct MHD_Response*
handle_root(off_t * size)2172 handle_root (off_t* size)
2173 {
2174   static string version = "debuginfod (" + string (PACKAGE_NAME) + ") "
2175 			  + string (PACKAGE_VERSION);
2176   MHD_Response* r = MHD_create_response_from_buffer (version.size (),
2177 						     (void *) version.c_str (),
2178 						     MHD_RESPMEM_PERSISTENT);
2179   *size = version.size ();
2180   MHD_add_response_header (r, "Content-Type", "text/plain");
2181   return r;
2182 }
2183 
2184 
2185 ////////////////////////////////////////////////////////////////////////
2186 
2187 
2188 /* libmicrohttpd callback */
2189 static MHD_RESULT
handler_cb(void *,struct MHD_Connection * connection,const char * url,const char * method,const char *,const char *,size_t *,void ** ptr)2190 handler_cb (void * /*cls*/,
2191             struct MHD_Connection *connection,
2192             const char *url,
2193             const char *method,
2194             const char * /*version*/,
2195             const char * /*upload_data*/,
2196             size_t * /*upload_data_size*/,
2197             void ** ptr)
2198 {
2199   struct MHD_Response *r = NULL;
2200   string url_copy = url;
2201 
2202   /* libmicrohttpd always makes (at least) two callbacks: once just
2203      past the headers, and one after the request body is finished
2204      being received.  If we process things early (first callback) and
2205      queue a response, libmicrohttpd would suppress http keep-alive
2206      (via connection->read_closed = true). */
2207   static int aptr; /* just some random object to use as a flag */
2208   if (&aptr != *ptr)
2209     {
2210       /* do never respond on first call */
2211       *ptr = &aptr;
2212       return MHD_YES;
2213     }
2214   *ptr = NULL;                     /* reset when done */
2215 
2216   const char *maxsize_string = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "X-DEBUGINFOD-MAXSIZE");
2217   long maxsize = 0;
2218   if (maxsize_string != NULL && maxsize_string[0] != '\0')
2219     maxsize = atol(maxsize_string);
2220   else
2221     maxsize = 0;
2222 
2223 #if MHD_VERSION >= 0x00097002
2224   enum MHD_Result rc;
2225 #else
2226   int rc = MHD_NO; // mhd
2227 #endif
2228   int http_code = 500;
2229   off_t http_size = -1;
2230   struct timespec ts_start, ts_end;
2231   clock_gettime (CLOCK_MONOTONIC, &ts_start);
2232   double afteryou = 0.0;
2233   string artifacttype, suffix;
2234 
2235   try
2236     {
2237       if (string(method) != "GET")
2238         throw reportable_exception(400, "we support GET only");
2239 
2240       /* Start decoding the URL. */
2241       size_t slash1 = url_copy.find('/', 1);
2242       string url1 = url_copy.substr(0, slash1); // ok even if slash1 not found
2243 
2244       if (slash1 != string::npos && url1 == "/buildid")
2245         {
2246           // PR27863: block this thread awhile if another thread is already busy
2247           // fetching the exact same thing.  This is better for Everyone.
2248           // The latecomer says "... after you!" and waits.
2249           add_metric ("thread_busy", "role", "http-buildid-after-you", 1);
2250 #ifdef HAVE_PTHREAD_SETNAME_NP
2251           (void) pthread_setname_np (pthread_self(), "mhd-buildid-after-you");
2252 #endif
2253           struct timespec tsay_start, tsay_end;
2254           clock_gettime (CLOCK_MONOTONIC, &tsay_start);
2255           static unique_set<string> busy_urls;
2256           unique_set_reserver<string> after_you(busy_urls, url_copy);
2257           clock_gettime (CLOCK_MONOTONIC, &tsay_end);
2258           afteryou = (tsay_end.tv_sec - tsay_start.tv_sec) + (tsay_end.tv_nsec - tsay_start.tv_nsec)/1.e9;
2259           add_metric ("thread_busy", "role", "http-buildid-after-you", -1);
2260 
2261           tmp_inc_metric m ("thread_busy", "role", "http-buildid");
2262 #ifdef HAVE_PTHREAD_SETNAME_NP
2263           (void) pthread_setname_np (pthread_self(), "mhd-buildid");
2264 #endif
2265           size_t slash2 = url_copy.find('/', slash1+1);
2266           if (slash2 == string::npos)
2267             throw reportable_exception("/buildid/ webapi error, need buildid");
2268 
2269           string buildid = url_copy.substr(slash1+1, slash2-slash1-1);
2270 
2271           size_t slash3 = url_copy.find('/', slash2+1);
2272 
2273           if (slash3 == string::npos)
2274             {
2275               artifacttype = url_copy.substr(slash2+1);
2276               suffix = "";
2277             }
2278           else
2279             {
2280               artifacttype = url_copy.substr(slash2+1, slash3-slash2-1);
2281               suffix = url_copy.substr(slash3); // include the slash in the suffix
2282             }
2283 
2284           // get the resulting fd so we can report its size
2285           int fd;
2286           r = handle_buildid(connection, buildid, artifacttype, suffix, &fd);
2287           if (r)
2288             {
2289               struct stat fs;
2290               if (fstat(fd, &fs) == 0)
2291                 http_size = fs.st_size;
2292               // libmicrohttpd will close (fd);
2293             }
2294         }
2295       else if (url1 == "/metrics")
2296         {
2297           tmp_inc_metric m ("thread_busy", "role", "http-metrics");
2298           artifacttype = "metrics";
2299           inc_metric("http_requests_total", "type", artifacttype);
2300           r = handle_metrics(& http_size);
2301         }
2302       else if (url1 == "/")
2303         {
2304           artifacttype = "/";
2305           inc_metric("http_requests_total", "type", artifacttype);
2306           r = handle_root(& http_size);
2307         }
2308       else
2309         throw reportable_exception("webapi error, unrecognized '" + url1 + "'");
2310 
2311       if (r == 0)
2312         throw reportable_exception("internal error, missing response");
2313 
2314       if (maxsize > 0 && http_size > maxsize)
2315         {
2316           MHD_destroy_response(r);
2317           throw reportable_exception(406, "File too large, max size=" + std::to_string(maxsize));
2318         }
2319 
2320       rc = MHD_queue_response (connection, MHD_HTTP_OK, r);
2321       http_code = MHD_HTTP_OK;
2322       MHD_destroy_response (r);
2323     }
2324   catch (const reportable_exception& e)
2325     {
2326       inc_metric("http_responses_total","result","error");
2327       e.report(clog);
2328       http_code = e.code;
2329       http_size = e.message.size();
2330       rc = e.mhd_send_response (connection);
2331     }
2332 
2333   clock_gettime (CLOCK_MONOTONIC, &ts_end);
2334   double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
2335   // afteryou: delay waiting for other client's identical query to complete
2336   // deltas: total latency, including afteryou waiting
2337   obatched(clog) << conninfo(connection)
2338                  << ' ' << method << ' ' << url
2339                  << ' ' << http_code << ' ' << http_size
2340                  << ' ' << (int)(afteryou*1000) << '+' << (int)((deltas-afteryou)*1000) << "ms"
2341                  << endl;
2342 
2343   // related prometheus metrics
2344   string http_code_str = to_string(http_code);
2345   add_metric("http_responses_transfer_bytes_sum",
2346              "code", http_code_str, "type", artifacttype, http_size);
2347   inc_metric("http_responses_transfer_bytes_count",
2348              "code", http_code_str, "type", artifacttype);
2349 
2350   add_metric("http_responses_duration_milliseconds_sum",
2351              "code", http_code_str, "type", artifacttype, deltas*1000); // prometheus prefers _seconds and floating point
2352   inc_metric("http_responses_duration_milliseconds_count",
2353              "code", http_code_str, "type", artifacttype);
2354 
2355   add_metric("http_responses_after_you_milliseconds_sum",
2356              "code", http_code_str, "type", artifacttype, afteryou*1000);
2357   inc_metric("http_responses_after_you_milliseconds_count",
2358              "code", http_code_str, "type", artifacttype);
2359 
2360   return rc;
2361 }
2362 
2363 
2364 ////////////////////////////////////////////////////////////////////////
2365 // borrowed originally from src/nm.c get_local_names()
2366 
2367 static void
dwarf_extract_source_paths(Elf * elf,set<string> & debug_sourcefiles)2368 dwarf_extract_source_paths (Elf *elf, set<string>& debug_sourcefiles)
2369   noexcept // no exceptions - so we can simplify the altdbg resource release at end
2370 {
2371   Dwarf* dbg = dwarf_begin_elf (elf, DWARF_C_READ, NULL);
2372   if (dbg == NULL)
2373     return;
2374 
2375   Dwarf* altdbg = NULL;
2376   int    altdbg_fd = -1;
2377 
2378   // DWZ handling: if we have an unsatisfied debug-alt-link, add an
2379   // empty string into the outgoing sourcefiles set, so the caller
2380   // should know that our data is incomplete.
2381   const char *alt_name_p;
2382   const void *alt_build_id; // elfutils-owned memory
2383   ssize_t sz = dwelf_dwarf_gnu_debugaltlink (dbg, &alt_name_p, &alt_build_id);
2384   if (sz > 0) // got one!
2385     {
2386       string buildid;
2387       unsigned char* build_id_bytes = (unsigned char*) alt_build_id;
2388       for (ssize_t idx=0; idx<sz; idx++)
2389         {
2390           buildid += "0123456789abcdef"[build_id_bytes[idx] >> 4];
2391           buildid += "0123456789abcdef"[build_id_bytes[idx] & 0xf];
2392         }
2393 
2394       if (verbose > 3)
2395         obatched(clog) << "Need altdebug buildid=" << buildid << endl;
2396 
2397       // but is it unsatisfied the normal elfutils ways?
2398       Dwarf* alt = dwarf_getalt (dbg);
2399       if (alt == NULL)
2400         {
2401           // Yup, unsatisfied the normal way.  Maybe we can satisfy it
2402           // from our own debuginfod database.
2403           int alt_fd;
2404           struct MHD_Response *r = 0;
2405           try
2406             {
2407               string artifacttype = "debuginfo";
2408               r = handle_buildid (0, buildid, artifacttype, "", &alt_fd);
2409             }
2410           catch (const reportable_exception& e)
2411             {
2412               // swallow exceptions
2413             }
2414 
2415           // NB: this is not actually recursive!  This invokes the web-query
2416           // path, which cannot get back into the scan code paths.
2417           if (r)
2418             {
2419               // Found it!
2420               altdbg_fd = dup(alt_fd); // ok if this fails, downstream failures ok
2421               alt = altdbg = dwarf_begin (altdbg_fd, DWARF_C_READ);
2422               // NB: must close this dwarf and this fd at the bottom of the function!
2423               MHD_destroy_response (r); // will close alt_fd
2424               if (alt)
2425                 dwarf_setalt (dbg, alt);
2426             }
2427         }
2428       else
2429         {
2430           // NB: dwarf_setalt(alt) inappropriate - already done!
2431           // NB: altdbg will stay 0 so nothing tries to redundantly dealloc.
2432         }
2433 
2434       if (alt)
2435         {
2436           if (verbose > 3)
2437             obatched(clog) << "Resolved altdebug buildid=" << buildid << endl;
2438         }
2439       else // (alt == NULL) - signal possible presence of poor debuginfo
2440         {
2441           debug_sourcefiles.insert("");
2442           if (verbose > 3)
2443             obatched(clog) << "Unresolved altdebug buildid=" << buildid << endl;
2444         }
2445     }
2446 
2447   Dwarf_Off offset = 0;
2448   Dwarf_Off old_offset;
2449   size_t hsize;
2450 
2451   while (dwarf_nextcu (dbg, old_offset = offset, &offset, &hsize, NULL, NULL, NULL) == 0)
2452     {
2453       Dwarf_Die cudie_mem;
2454       Dwarf_Die *cudie = dwarf_offdie (dbg, old_offset + hsize, &cudie_mem);
2455 
2456       if (cudie == NULL)
2457         continue;
2458       if (dwarf_tag (cudie) != DW_TAG_compile_unit)
2459         continue;
2460 
2461       const char *cuname = dwarf_diename(cudie) ?: "unknown";
2462 
2463       Dwarf_Files *files;
2464       size_t nfiles;
2465       if (dwarf_getsrcfiles (cudie, &files, &nfiles) != 0)
2466         continue;
2467 
2468       // extract DW_AT_comp_dir to resolve relative file names
2469       const char *comp_dir = "";
2470       const char *const *dirs;
2471       size_t ndirs;
2472       if (dwarf_getsrcdirs (files, &dirs, &ndirs) == 0 &&
2473           dirs[0] != NULL)
2474         comp_dir = dirs[0];
2475       if (comp_dir == NULL)
2476         comp_dir = "";
2477 
2478       if (verbose > 3)
2479         obatched(clog) << "searching for sources for cu=" << cuname << " comp_dir=" << comp_dir
2480                        << " #files=" << nfiles << " #dirs=" << ndirs << endl;
2481 
2482       if (comp_dir[0] == '\0' && cuname[0] != '/')
2483         {
2484           // This is a common symptom for dwz-compressed debug files,
2485           // where the altdebug file cannot be resolved.
2486           if (verbose > 3)
2487             obatched(clog) << "skipping cu=" << cuname << " due to empty comp_dir" << endl;
2488           continue;
2489         }
2490 
2491       for (size_t f = 1; f < nfiles; f++)
2492         {
2493           const char *hat = dwarf_filesrc (files, f, NULL, NULL);
2494           if (hat == NULL)
2495             continue;
2496 
2497           if (string(hat) == "<built-in>") // gcc intrinsics, don't bother record
2498             continue;
2499 
2500           string waldo;
2501           if (hat[0] == '/') // absolute
2502             waldo = (string (hat));
2503           else if (comp_dir[0] != '\0') // comp_dir relative
2504             waldo = (string (comp_dir) + string("/") + string (hat));
2505           else
2506            {
2507              if (verbose > 3)
2508                obatched(clog) << "skipping hat=" << hat << " due to empty comp_dir" << endl;
2509              continue;
2510            }
2511 
2512           // NB: this is the 'waldo' that a dbginfo client will have
2513           // to supply for us to give them the file The comp_dir
2514           // prefixing is a definite complication.  Otherwise we'd
2515           // have to return a setof comp_dirs (one per CU!) with
2516           // corresponding filesrc[] names, instead of one absolute
2517           // resoved set.  Maybe we'll have to do that anyway.  XXX
2518 
2519           if (verbose > 4)
2520             obatched(clog) << waldo
2521                            << (debug_sourcefiles.find(waldo)==debug_sourcefiles.end() ? " new" : " dup") <<  endl;
2522 
2523           debug_sourcefiles.insert (waldo);
2524         }
2525     }
2526 
2527   dwarf_end(dbg);
2528   if (altdbg)
2529     dwarf_end(altdbg);
2530   if (altdbg_fd >= 0)
2531     close(altdbg_fd);
2532 }
2533 
2534 
2535 
2536 static void
elf_classify(int fd,bool & executable_p,bool & debuginfo_p,string & buildid,set<string> & debug_sourcefiles)2537 elf_classify (int fd, bool &executable_p, bool &debuginfo_p, string &buildid, set<string>& debug_sourcefiles)
2538 {
2539   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
2540   if (elf == NULL)
2541     return;
2542 
2543   try // catch our types of errors and clean up the Elf* object
2544     {
2545       if (elf_kind (elf) != ELF_K_ELF)
2546         {
2547           elf_end (elf);
2548           return;
2549         }
2550 
2551       GElf_Ehdr ehdr_storage;
2552       GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_storage);
2553       if (ehdr == NULL)
2554         {
2555           elf_end (elf);
2556           return;
2557         }
2558       auto elf_type = ehdr->e_type;
2559 
2560       const void *build_id; // elfutils-owned memory
2561       ssize_t sz = dwelf_elf_gnu_build_id (elf, & build_id);
2562       if (sz <= 0)
2563         {
2564           // It's not a diagnostic-worthy error for an elf file to lack build-id.
2565           // It might just be very old.
2566           elf_end (elf);
2567           return;
2568         }
2569 
2570       // build_id is a raw byte array; convert to hexadecimal *lowercase*
2571       unsigned char* build_id_bytes = (unsigned char*) build_id;
2572       for (ssize_t idx=0; idx<sz; idx++)
2573         {
2574           buildid += "0123456789abcdef"[build_id_bytes[idx] >> 4];
2575           buildid += "0123456789abcdef"[build_id_bytes[idx] & 0xf];
2576         }
2577 
2578       // now decide whether it's an executable - namely, any allocatable section has
2579       // PROGBITS;
2580       if (elf_type == ET_EXEC || elf_type == ET_DYN)
2581         {
2582           size_t shnum;
2583           int rc = elf_getshdrnum (elf, &shnum);
2584           if (rc < 0)
2585             throw elfutils_exception(rc, "getshdrnum");
2586 
2587           executable_p = false;
2588           for (size_t sc = 0; sc < shnum; sc++)
2589             {
2590               Elf_Scn *scn = elf_getscn (elf, sc);
2591               if (scn == NULL)
2592                 continue;
2593 
2594               GElf_Shdr shdr_mem;
2595               GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2596               if (shdr == NULL)
2597                 continue;
2598 
2599               // allocated (loadable / vm-addr-assigned) section with available content?
2600               if ((shdr->sh_type == SHT_PROGBITS) && (shdr->sh_flags & SHF_ALLOC))
2601                 {
2602                   if (verbose > 4)
2603                     obatched(clog) << "executable due to SHF_ALLOC SHT_PROGBITS sc=" << sc << endl;
2604                   executable_p = true;
2605                   break; // no need to keep looking for others
2606                 }
2607             } // iterate over sections
2608         } // executable_p classification
2609 
2610       // now decide whether it's a debuginfo - namely, if it has any .debug* or .zdebug* sections
2611       // logic mostly stolen from fweimer@redhat.com's elfclassify drafts
2612       size_t shstrndx;
2613       int rc = elf_getshdrstrndx (elf, &shstrndx);
2614       if (rc < 0)
2615         throw elfutils_exception(rc, "getshdrstrndx");
2616 
2617       Elf_Scn *scn = NULL;
2618       bool symtab_p = false;
2619       bool bits_alloc_p = false;
2620       while (true)
2621         {
2622           scn = elf_nextscn (elf, scn);
2623           if (scn == NULL)
2624             break;
2625           GElf_Shdr shdr_storage;
2626           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_storage);
2627           if (shdr == NULL)
2628             break;
2629           const char *section_name = elf_strptr (elf, shstrndx, shdr->sh_name);
2630           if (section_name == NULL)
2631             break;
2632           if (startswith (section_name, ".debug_line") ||
2633               startswith (section_name, ".zdebug_line"))
2634             {
2635               debuginfo_p = true;
2636               dwarf_extract_source_paths (elf, debug_sourcefiles);
2637               break; // expecting only one .*debug_line, so no need to look for others
2638             }
2639           else if (startswith (section_name, ".debug_") ||
2640                    startswith (section_name, ".zdebug_"))
2641             {
2642               debuginfo_p = true;
2643               // NB: don't break; need to parse .debug_line for sources
2644             }
2645           else if (shdr->sh_type == SHT_SYMTAB)
2646             {
2647               symtab_p = true;
2648             }
2649           else if (shdr->sh_type != SHT_NOBITS
2650                    && shdr->sh_type != SHT_NOTE
2651                    && (shdr->sh_flags & SHF_ALLOC) != 0)
2652             {
2653               bits_alloc_p = true;
2654             }
2655         }
2656 
2657       // For more expansive elf/split-debuginfo classification, we
2658       // want to identify as debuginfo "strip -s"-produced files
2659       // without .debug_info* (like libicudata), but we don't want to
2660       // identify "strip -g" executables (with .symtab left there).
2661       if (symtab_p && !bits_alloc_p)
2662         debuginfo_p = true;
2663     }
2664   catch (const reportable_exception& e)
2665     {
2666       e.report(clog);
2667     }
2668   elf_end (elf);
2669 }
2670 
2671 
2672 static void
scan_source_file(const string & rps,const stat_t & st,sqlite_ps & ps_upsert_buildids,sqlite_ps & ps_upsert_files,sqlite_ps & ps_upsert_de,sqlite_ps & ps_upsert_s,sqlite_ps & ps_query,sqlite_ps & ps_scan_done,unsigned & fts_cached,unsigned & fts_executable,unsigned & fts_debuginfo,unsigned & fts_sourcefiles)2673 scan_source_file (const string& rps, const stat_t& st,
2674                   sqlite_ps& ps_upsert_buildids,
2675                   sqlite_ps& ps_upsert_files,
2676                   sqlite_ps& ps_upsert_de,
2677                   sqlite_ps& ps_upsert_s,
2678                   sqlite_ps& ps_query,
2679                   sqlite_ps& ps_scan_done,
2680                   unsigned& fts_cached,
2681                   unsigned& fts_executable,
2682                   unsigned& fts_debuginfo,
2683                   unsigned& fts_sourcefiles)
2684 {
2685   /* See if we know of it already. */
2686   int rc = ps_query
2687     .reset()
2688     .bind(1, rps)
2689     .bind(2, st.st_mtime)
2690     .step();
2691   ps_query.reset();
2692   if (rc == SQLITE_ROW) // i.e., a result, as opposed to DONE (no results)
2693     // no need to recheck a file/version we already know
2694     // specifically, no need to elf-begin a file we already determined is non-elf
2695     // (so is stored with buildid=NULL)
2696     {
2697       fts_cached++;
2698       return;
2699     }
2700 
2701   bool executable_p = false, debuginfo_p = false; // E and/or D
2702   string buildid;
2703   set<string> sourcefiles;
2704 
2705   int fd = open (rps.c_str(), O_RDONLY);
2706   try
2707     {
2708       if (fd >= 0)
2709         elf_classify (fd, executable_p, debuginfo_p, buildid, sourcefiles);
2710       else
2711         throw libc_exception(errno, string("open ") + rps);
2712       add_metric ("scanned_bytes_total","source","file",
2713                   st.st_size);
2714       inc_metric ("scanned_files_total","source","file");
2715     }
2716   // NB: we catch exceptions here too, so that we can
2717   // cache the corrupt-elf case (!executable_p &&
2718   // !debuginfo_p) just below, just as if we had an
2719   // EPERM error from open(2).
2720   catch (const reportable_exception& e)
2721     {
2722       e.report(clog);
2723     }
2724 
2725   if (fd >= 0)
2726     close (fd);
2727 
2728   // register this file name in the interning table
2729   ps_upsert_files
2730     .reset()
2731     .bind(1, rps)
2732     .step_ok_done();
2733 
2734   if (buildid == "")
2735     {
2736       // no point storing an elf file without buildid
2737       executable_p = false;
2738       debuginfo_p = false;
2739     }
2740   else
2741     {
2742       // register this build-id in the interning table
2743       ps_upsert_buildids
2744         .reset()
2745         .bind(1, buildid)
2746         .step_ok_done();
2747     }
2748 
2749   if (executable_p)
2750     fts_executable ++;
2751   if (debuginfo_p)
2752     fts_debuginfo ++;
2753   if (executable_p || debuginfo_p)
2754     {
2755       ps_upsert_de
2756         .reset()
2757         .bind(1, buildid)
2758         .bind(2, debuginfo_p ? 1 : 0)
2759         .bind(3, executable_p ? 1 : 0)
2760         .bind(4, rps)
2761         .bind(5, st.st_mtime)
2762         .step_ok_done();
2763     }
2764   if (executable_p)
2765     inc_metric("found_executable_total","source","files");
2766   if (debuginfo_p)
2767     inc_metric("found_debuginfo_total","source","files");
2768 
2769   if (sourcefiles.size() && buildid != "")
2770     {
2771       fts_sourcefiles += sourcefiles.size();
2772 
2773       for (auto&& dwarfsrc : sourcefiles)
2774         {
2775           char *srp = realpath(dwarfsrc.c_str(), NULL);
2776           if (srp == NULL) // also if DWZ unresolved dwarfsrc=""
2777             continue; // unresolvable files are not a serious problem
2778           // throw libc_exception(errno, "fts/file realpath " + srcpath);
2779           string srps = string(srp);
2780           free (srp);
2781 
2782           struct stat sfs;
2783           rc = stat(srps.c_str(), &sfs);
2784           if (rc != 0)
2785             continue;
2786 
2787           if (verbose > 2)
2788             obatched(clog) << "recorded buildid=" << buildid << " file=" << srps
2789                            << " mtime=" << sfs.st_mtime
2790                            << " as source " << dwarfsrc << endl;
2791 
2792           ps_upsert_files
2793             .reset()
2794             .bind(1, srps)
2795             .step_ok_done();
2796 
2797           // PR25548: store canonicalized dwarfsrc path
2798           string dwarfsrc_canon = canon_pathname (dwarfsrc);
2799           if (dwarfsrc_canon != dwarfsrc)
2800             {
2801               if (verbose > 3)
2802                 obatched(clog) << "canonicalized src=" << dwarfsrc << " alias=" << dwarfsrc_canon << endl;
2803             }
2804 
2805           ps_upsert_files
2806             .reset()
2807             .bind(1, dwarfsrc_canon)
2808             .step_ok_done();
2809 
2810           ps_upsert_s
2811             .reset()
2812             .bind(1, buildid)
2813             .bind(2, dwarfsrc_canon)
2814             .bind(3, srps)
2815             .bind(4, sfs.st_mtime)
2816             .step_ok_done();
2817 
2818           inc_metric("found_sourcerefs_total","source","files");
2819         }
2820     }
2821 
2822   ps_scan_done
2823     .reset()
2824     .bind(1, rps)
2825     .bind(2, st.st_mtime)
2826     .bind(3, st.st_size)
2827     .step_ok_done();
2828 
2829   if (verbose > 2)
2830     obatched(clog) << "recorded buildid=" << buildid << " file=" << rps
2831                    << " mtime=" << st.st_mtime << " atype="
2832                    << (executable_p ? "E" : "")
2833                    << (debuginfo_p ? "D" : "") << endl;
2834 }
2835 
2836 
2837 
2838 
2839 
2840 // Analyze given archive file of given age; record buildids / exec/debuginfo-ness of its
2841 // constituent files with given upsert statements.
2842 static void
archive_classify(const string & rps,string & archive_extension,sqlite_ps & ps_upsert_buildids,sqlite_ps & ps_upsert_files,sqlite_ps & ps_upsert_de,sqlite_ps & ps_upsert_sref,sqlite_ps & ps_upsert_sdef,time_t mtime,unsigned & fts_executable,unsigned & fts_debuginfo,unsigned & fts_sref,unsigned & fts_sdef,bool & fts_sref_complete_p)2843 archive_classify (const string& rps, string& archive_extension,
2844                   sqlite_ps& ps_upsert_buildids, sqlite_ps& ps_upsert_files,
2845                   sqlite_ps& ps_upsert_de, sqlite_ps& ps_upsert_sref, sqlite_ps& ps_upsert_sdef,
2846                   time_t mtime,
2847                   unsigned& fts_executable, unsigned& fts_debuginfo, unsigned& fts_sref, unsigned& fts_sdef,
2848                   bool& fts_sref_complete_p)
2849 {
2850   string archive_decoder = "/dev/null";
2851   for (auto&& arch : scan_archives)
2852     if (string_endswith(rps, arch.first))
2853       {
2854         archive_extension = arch.first;
2855         archive_decoder = arch.second;
2856       }
2857 
2858   FILE* fp;
2859   defer_dtor<FILE*,int>::dtor_fn dfn;
2860   if (archive_decoder != "cat")
2861     {
2862       string popen_cmd = archive_decoder + " " + shell_escape(rps);
2863       fp = popen (popen_cmd.c_str(), "r"); // "e" O_CLOEXEC?
2864       dfn = pclose;
2865       if (fp == NULL)
2866         throw libc_exception (errno, string("popen ") + popen_cmd);
2867     }
2868   else
2869     {
2870       fp = fopen (rps.c_str(), "r");
2871       dfn = fclose;
2872       if (fp == NULL)
2873         throw libc_exception (errno, string("fopen ") + rps);
2874     }
2875   defer_dtor<FILE*,int> fp_closer (fp, dfn);
2876 
2877   struct archive *a;
2878   a = archive_read_new();
2879   if (a == NULL)
2880     throw archive_exception("cannot create archive reader");
2881   defer_dtor<struct archive*,int> archive_closer (a, archive_read_free);
2882 
2883   int rc = archive_read_support_format_all(a);
2884   if (rc != ARCHIVE_OK)
2885     throw archive_exception(a, "cannot select all formats");
2886   rc = archive_read_support_filter_all(a);
2887   if (rc != ARCHIVE_OK)
2888     throw archive_exception(a, "cannot select all filters");
2889 
2890   rc = archive_read_open_FILE (a, fp);
2891   if (rc != ARCHIVE_OK)
2892     throw archive_exception(a, "cannot open archive from pipe");
2893 
2894   if (verbose > 3)
2895     obatched(clog) << "libarchive scanning " << rps << endl;
2896 
2897   while(1) // parse archive entries
2898     {
2899     if (interrupted)
2900       break;
2901 
2902     try
2903         {
2904           struct archive_entry *e;
2905           rc = archive_read_next_header (a, &e);
2906           if (rc != ARCHIVE_OK)
2907             break;
2908 
2909           if (! S_ISREG(archive_entry_mode (e))) // skip non-files completely
2910             continue;
2911 
2912           string fn = canonicalized_archive_entry_pathname (e);
2913 
2914           if (verbose > 3)
2915             obatched(clog) << "libarchive checking " << fn << endl;
2916 
2917           // extract this file to a temporary file
2918           char* tmppath = NULL;
2919           rc = asprintf (&tmppath, "%s/debuginfod.XXXXXX", tmpdir.c_str());
2920           if (rc < 0)
2921             throw libc_exception (ENOMEM, "cannot allocate tmppath");
2922           defer_dtor<void*,void> tmmpath_freer (tmppath, free);
2923           int fd = mkstemp (tmppath);
2924           if (fd < 0)
2925             throw libc_exception (errno, "cannot create temporary file");
2926           unlink (tmppath); // unlink now so OS will release the file as soon as we close the fd
2927           defer_dtor<int,int> minifd_closer (fd, close);
2928 
2929           rc = archive_read_data_into_fd (a, fd);
2930           if (rc != ARCHIVE_OK)
2931             throw archive_exception(a, "cannot extract file");
2932 
2933           // finally ... time to run elf_classify on this bad boy and update the database
2934           bool executable_p = false, debuginfo_p = false;
2935           string buildid;
2936           set<string> sourcefiles;
2937           elf_classify (fd, executable_p, debuginfo_p, buildid, sourcefiles);
2938           // NB: might throw
2939 
2940           if (buildid != "") // intern buildid
2941             {
2942               ps_upsert_buildids
2943                 .reset()
2944                 .bind(1, buildid)
2945                 .step_ok_done();
2946             }
2947 
2948           ps_upsert_files // register this rpm constituent file name in interning table
2949             .reset()
2950             .bind(1, fn)
2951             .step_ok_done();
2952 
2953           if (sourcefiles.size() > 0) // sref records needed
2954             {
2955               // NB: we intern each source file once.  Once raw, as it
2956               // appears in the DWARF file list coming back from
2957               // elf_classify() - because it'll end up in the
2958               // _norm.artifactsrc column.  We don't also put another
2959               // version with a '.' at the front, even though that's
2960               // how rpm/cpio packs names, because we hide that from
2961               // the database for storage efficiency.
2962 
2963               for (auto&& s : sourcefiles)
2964                 {
2965                   if (s == "")
2966                     {
2967                       fts_sref_complete_p = false;
2968                       continue;
2969                     }
2970 
2971                   // PR25548: store canonicalized source path
2972                   const string& dwarfsrc = s;
2973                   string dwarfsrc_canon = canon_pathname (dwarfsrc);
2974                   if (dwarfsrc_canon != dwarfsrc)
2975                     {
2976                       if (verbose > 3)
2977                         obatched(clog) << "canonicalized src=" << dwarfsrc << " alias=" << dwarfsrc_canon << endl;
2978                     }
2979 
2980                   ps_upsert_files
2981                     .reset()
2982                     .bind(1, dwarfsrc_canon)
2983                     .step_ok_done();
2984 
2985                   ps_upsert_sref
2986                     .reset()
2987                     .bind(1, buildid)
2988                     .bind(2, dwarfsrc_canon)
2989                     .step_ok_done();
2990 
2991                   fts_sref ++;
2992                 }
2993             }
2994 
2995           if (executable_p)
2996             fts_executable ++;
2997           if (debuginfo_p)
2998             fts_debuginfo ++;
2999 
3000           if (executable_p || debuginfo_p)
3001             {
3002               ps_upsert_de
3003                 .reset()
3004                 .bind(1, buildid)
3005                 .bind(2, debuginfo_p ? 1 : 0)
3006                 .bind(3, executable_p ? 1 : 0)
3007                 .bind(4, rps)
3008                 .bind(5, mtime)
3009                 .bind(6, fn)
3010                 .step_ok_done();
3011             }
3012           else // potential source - sdef record
3013             {
3014               fts_sdef ++;
3015               ps_upsert_sdef
3016                 .reset()
3017                 .bind(1, rps)
3018                 .bind(2, mtime)
3019                 .bind(3, fn)
3020                 .step_ok_done();
3021             }
3022 
3023           if ((verbose > 2) && (executable_p || debuginfo_p))
3024             obatched(clog) << "recorded buildid=" << buildid << " rpm=" << rps << " file=" << fn
3025                            << " mtime=" << mtime << " atype="
3026                            << (executable_p ? "E" : "")
3027                            << (debuginfo_p ? "D" : "")
3028                            << " sourcefiles=" << sourcefiles.size() << endl;
3029 
3030         }
3031       catch (const reportable_exception& e)
3032         {
3033           e.report(clog);
3034         }
3035     }
3036 }
3037 
3038 
3039 
3040 // scan for archive files such as .rpm
3041 static void
scan_archive_file(const string & rps,const stat_t & st,sqlite_ps & ps_upsert_buildids,sqlite_ps & ps_upsert_files,sqlite_ps & ps_upsert_de,sqlite_ps & ps_upsert_sref,sqlite_ps & ps_upsert_sdef,sqlite_ps & ps_query,sqlite_ps & ps_scan_done,unsigned & fts_cached,unsigned & fts_executable,unsigned & fts_debuginfo,unsigned & fts_sref,unsigned & fts_sdef)3042 scan_archive_file (const string& rps, const stat_t& st,
3043                    sqlite_ps& ps_upsert_buildids,
3044                    sqlite_ps& ps_upsert_files,
3045                    sqlite_ps& ps_upsert_de,
3046                    sqlite_ps& ps_upsert_sref,
3047                    sqlite_ps& ps_upsert_sdef,
3048                    sqlite_ps& ps_query,
3049                    sqlite_ps& ps_scan_done,
3050                    unsigned& fts_cached,
3051                    unsigned& fts_executable,
3052                    unsigned& fts_debuginfo,
3053                    unsigned& fts_sref,
3054                    unsigned& fts_sdef)
3055 {
3056   /* See if we know of it already. */
3057   int rc = ps_query
3058     .reset()
3059     .bind(1, rps)
3060     .bind(2, st.st_mtime)
3061     .step();
3062   ps_query.reset();
3063   if (rc == SQLITE_ROW) // i.e., a result, as opposed to DONE (no results)
3064     // no need to recheck a file/version we already know
3065     // specifically, no need to parse this archive again, since we already have
3066     // it as a D or E or S record,
3067     // (so is stored with buildid=NULL)
3068     {
3069       fts_cached ++;
3070       return;
3071     }
3072 
3073   // intern the archive file name
3074   ps_upsert_files
3075     .reset()
3076     .bind(1, rps)
3077     .step_ok_done();
3078 
3079   // extract the archive contents
3080   unsigned my_fts_executable = 0, my_fts_debuginfo = 0, my_fts_sref = 0, my_fts_sdef = 0;
3081   bool my_fts_sref_complete_p = true;
3082   try
3083     {
3084       string archive_extension;
3085       archive_classify (rps, archive_extension,
3086                         ps_upsert_buildids, ps_upsert_files,
3087                         ps_upsert_de, ps_upsert_sref, ps_upsert_sdef, // dalt
3088                         st.st_mtime,
3089                         my_fts_executable, my_fts_debuginfo, my_fts_sref, my_fts_sdef,
3090                         my_fts_sref_complete_p);
3091       add_metric ("scanned_bytes_total","source",archive_extension + " archive",
3092                   st.st_size);
3093       inc_metric ("scanned_files_total","source",archive_extension + " archive");
3094       add_metric("found_debuginfo_total","source",archive_extension + " archive",
3095                  my_fts_debuginfo);
3096       add_metric("found_executable_total","source",archive_extension + " archive",
3097                  my_fts_executable);
3098       add_metric("found_sourcerefs_total","source",archive_extension + " archive",
3099                  my_fts_sref);
3100     }
3101   catch (const reportable_exception& e)
3102     {
3103       e.report(clog);
3104     }
3105 
3106   if (verbose > 2)
3107     obatched(clog) << "scanned archive=" << rps
3108                    << " mtime=" << st.st_mtime
3109                    << " executables=" << my_fts_executable
3110                    << " debuginfos=" << my_fts_debuginfo
3111                    << " srefs=" << my_fts_sref
3112                    << " sdefs=" << my_fts_sdef
3113                    << endl;
3114 
3115   fts_executable += my_fts_executable;
3116   fts_debuginfo += my_fts_debuginfo;
3117   fts_sref += my_fts_sref;
3118   fts_sdef += my_fts_sdef;
3119 
3120   if (my_fts_sref_complete_p) // leave incomplete?
3121     ps_scan_done
3122       .reset()
3123       .bind(1, rps)
3124       .bind(2, st.st_mtime)
3125       .bind(3, st.st_size)
3126       .step_ok_done();
3127 }
3128 
3129 
3130 
3131 ////////////////////////////////////////////////////////////////////////
3132 
3133 
3134 
3135 // The thread that consumes file names off of the scanq.  We hold
3136 // the persistent sqlite_ps's at this level and delegate file/archive
3137 // scanning to other functions.
3138 static void*
thread_main_scanner(void * arg)3139 thread_main_scanner (void* arg)
3140 {
3141   (void) arg;
3142 
3143   // all the prepared statements fit to use, the _f_ set:
3144   sqlite_ps ps_f_upsert_buildids (db, "file-buildids-intern", "insert or ignore into " BUILDIDS "_buildids VALUES (NULL, ?);");
3145   sqlite_ps ps_f_upsert_files (db, "file-files-intern", "insert or ignore into " BUILDIDS "_files VALUES (NULL, ?);");
3146   sqlite_ps ps_f_upsert_de (db, "file-de-upsert",
3147                           "insert or ignore into " BUILDIDS "_f_de "
3148                           "(buildid, debuginfo_p, executable_p, file, mtime) "
3149                           "values ((select id from " BUILDIDS "_buildids where hex = ?),"
3150                           "        ?,?,"
3151                           "        (select id from " BUILDIDS "_files where name = ?), ?);");
3152   sqlite_ps ps_f_upsert_s (db, "file-s-upsert",
3153                          "insert or ignore into " BUILDIDS "_f_s "
3154                          "(buildid, artifactsrc, file, mtime) "
3155                          "values ((select id from " BUILDIDS "_buildids where hex = ?),"
3156                          "        (select id from " BUILDIDS "_files where name = ?),"
3157                          "        (select id from " BUILDIDS "_files where name = ?),"
3158                          "        ?);");
3159   sqlite_ps ps_f_query (db, "file-negativehit-find",
3160                         "select 1 from " BUILDIDS "_file_mtime_scanned where sourcetype = 'F' "
3161                         "and file = (select id from " BUILDIDS "_files where name = ?) and mtime = ?;");
3162   sqlite_ps ps_f_scan_done (db, "file-scanned",
3163                           "insert or ignore into " BUILDIDS "_file_mtime_scanned (sourcetype, file, mtime, size)"
3164                           "values ('F', (select id from " BUILDIDS "_files where name = ?), ?, ?);");
3165 
3166   // and now for the _r_ set
3167   sqlite_ps ps_r_upsert_buildids (db, "rpm-buildid-intern", "insert or ignore into " BUILDIDS "_buildids VALUES (NULL, ?);");
3168   sqlite_ps ps_r_upsert_files (db, "rpm-file-intern", "insert or ignore into " BUILDIDS "_files VALUES (NULL, ?);");
3169   sqlite_ps ps_r_upsert_de (db, "rpm-de-insert",
3170                           "insert or ignore into " BUILDIDS "_r_de (buildid, debuginfo_p, executable_p, file, mtime, content) values ("
3171                           "(select id from " BUILDIDS "_buildids where hex = ?), ?, ?, "
3172                           "(select id from " BUILDIDS "_files where name = ?), ?, "
3173                           "(select id from " BUILDIDS "_files where name = ?));");
3174   sqlite_ps ps_r_upsert_sref (db, "rpm-sref-insert",
3175                             "insert or ignore into " BUILDIDS "_r_sref (buildid, artifactsrc) values ("
3176                             "(select id from " BUILDIDS "_buildids where hex = ?), "
3177                             "(select id from " BUILDIDS "_files where name = ?));");
3178   sqlite_ps ps_r_upsert_sdef (db, "rpm-sdef-insert",
3179                             "insert or ignore into " BUILDIDS "_r_sdef (file, mtime, content) values ("
3180                             "(select id from " BUILDIDS "_files where name = ?), ?,"
3181                             "(select id from " BUILDIDS "_files where name = ?));");
3182   sqlite_ps ps_r_query (db, "rpm-negativehit-query",
3183                       "select 1 from " BUILDIDS "_file_mtime_scanned where "
3184                       "sourcetype = 'R' and file = (select id from " BUILDIDS "_files where name = ?) and mtime = ?;");
3185   sqlite_ps ps_r_scan_done (db, "rpm-scanned",
3186                           "insert or ignore into " BUILDIDS "_file_mtime_scanned (sourcetype, file, mtime, size)"
3187                           "values ('R', (select id from " BUILDIDS "_files where name = ?), ?, ?);");
3188 
3189 
3190   unsigned fts_cached = 0, fts_executable = 0, fts_debuginfo = 0, fts_sourcefiles = 0;
3191   unsigned fts_sref = 0, fts_sdef = 0;
3192 
3193   add_metric("thread_count", "role", "scan", 1);
3194   add_metric("thread_busy", "role", "scan", 1);
3195   while (! interrupted)
3196     {
3197       scan_payload p;
3198 
3199       add_metric("thread_busy", "role", "scan", -1);
3200       bool gotone = scanq.wait_front(p);
3201       add_metric("thread_busy", "role", "scan", 1);
3202 
3203       if (! gotone) continue; // go back to waiting
3204 
3205       try
3206         {
3207           bool scan_archive = false;
3208           for (auto&& arch : scan_archives)
3209             if (string_endswith(p.first, arch.first))
3210               scan_archive = true;
3211 
3212           if (scan_archive)
3213             scan_archive_file (p.first, p.second,
3214                                ps_r_upsert_buildids,
3215                                ps_r_upsert_files,
3216                                ps_r_upsert_de,
3217                                ps_r_upsert_sref,
3218                                ps_r_upsert_sdef,
3219                                ps_r_query,
3220                                ps_r_scan_done,
3221                                fts_cached,
3222                                fts_executable,
3223                                fts_debuginfo,
3224                                fts_sref,
3225                                fts_sdef);
3226 
3227           if (scan_files) // NB: maybe "else if" ?
3228             scan_source_file (p.first, p.second,
3229                               ps_f_upsert_buildids,
3230                               ps_f_upsert_files,
3231                               ps_f_upsert_de,
3232                               ps_f_upsert_s,
3233                               ps_f_query,
3234                               ps_f_scan_done,
3235                               fts_cached, fts_executable, fts_debuginfo, fts_sourcefiles);
3236         }
3237       catch (const reportable_exception& e)
3238         {
3239           e.report(cerr);
3240         }
3241 
3242       scanq.done_front(); // let idlers run
3243 
3244       if (fts_cached || fts_executable || fts_debuginfo || fts_sourcefiles || fts_sref || fts_sdef)
3245         {} // NB: not just if a successful scan - we might have encountered -ENOSPC & failed
3246       (void) statfs_free_enough_p(db_path, "database"); // report sqlite filesystem size
3247       (void) statfs_free_enough_p(tmpdir, "tmpdir"); // this too, in case of fdcache/tmpfile usage
3248 
3249       // finished a scanning step -- not a "loop", because we just
3250       // consume the traversal loop's work, whenever
3251       inc_metric("thread_work_total","role","scan");
3252     }
3253 
3254 
3255   add_metric("thread_busy", "role", "scan", -1);
3256   return 0;
3257 }
3258 
3259 
3260 
3261 // The thread that traverses all the source_paths and enqueues all the
3262 // matching files into the file/archive scan queue.
3263 static void
scan_source_paths()3264 scan_source_paths()
3265 {
3266   // NB: fedora 31 glibc/fts(3) crashes inside fts_read() on empty
3267   // path list.
3268   if (source_paths.empty())
3269     return;
3270 
3271   // Turn the source_paths into an fts(3)-compatible char**.  Since
3272   // source_paths[] does not change after argv processing, the
3273   // c_str()'s are safe to keep around awile.
3274   vector<const char *> sps;
3275   for (auto&& sp: source_paths)
3276     sps.push_back(sp.c_str());
3277   sps.push_back(NULL);
3278 
3279   FTS *fts = fts_open ((char * const *)sps.data(),
3280                       (traverse_logical ? FTS_LOGICAL : FTS_PHYSICAL|FTS_XDEV)
3281                       | FTS_NOCHDIR /* multithreaded */,
3282                       NULL);
3283   if (fts == NULL)
3284     throw libc_exception(errno, "cannot fts_open");
3285   defer_dtor<FTS*,int> fts_cleanup (fts, fts_close);
3286 
3287   struct timespec ts_start, ts_end;
3288   clock_gettime (CLOCK_MONOTONIC, &ts_start);
3289   unsigned fts_scanned = 0, fts_regex = 0;
3290 
3291   FTSENT *f;
3292   while ((f = fts_read (fts)) != NULL)
3293   {
3294     if (interrupted) break;
3295 
3296     if (sigusr2 != forced_groom_count) // stop early if groom triggered
3297       {
3298         scanq.clear(); // clear previously issued work for scanner threads
3299         break;
3300       }
3301 
3302     fts_scanned ++;
3303 
3304     if (verbose > 2)
3305       obatched(clog) << "fts traversing " << f->fts_path << endl;
3306 
3307     switch (f->fts_info)
3308       {
3309       case FTS_F:
3310         {
3311           /* Found a file.  Convert it to an absolute path, so
3312              the buildid database does not have relative path
3313              names that are unresolvable from a subsequent run
3314              in a different cwd. */
3315           char *rp = realpath(f->fts_path, NULL);
3316           if (rp == NULL)
3317             continue; // ignore dangling symlink or such
3318           string rps = string(rp);
3319           free (rp);
3320 
3321           bool ri = !regexec (&file_include_regex, rps.c_str(), 0, 0, 0);
3322           bool rx = !regexec (&file_exclude_regex, rps.c_str(), 0, 0, 0);
3323           if (!ri || rx)
3324             {
3325               if (verbose > 3)
3326                 obatched(clog) << "fts skipped by regex "
3327                                << (!ri ? "I" : "") << (rx ? "X" : "") << endl;
3328               fts_regex ++;
3329               if (!ri)
3330                 inc_metric("traversed_total","type","file-skipped-I");
3331               if (rx)
3332                 inc_metric("traversed_total","type","file-skipped-X");
3333             }
3334           else
3335             {
3336               scanq.push_back (make_pair(rps, *f->fts_statp));
3337               inc_metric("traversed_total","type","file");
3338             }
3339         }
3340         break;
3341 
3342       case FTS_ERR:
3343       case FTS_NS:
3344         // report on some types of errors because they may reflect fixable misconfiguration
3345         {
3346           auto x = libc_exception(f->fts_errno, string("fts traversal ") + string(f->fts_path));
3347           x.report(cerr);
3348         }
3349         inc_metric("traversed_total","type","error");
3350         break;
3351 
3352       case FTS_SL: // ignore, but count because debuginfod -L would traverse these
3353         inc_metric("traversed_total","type","symlink");
3354         break;
3355 
3356       case FTS_D: // ignore
3357         inc_metric("traversed_total","type","directory");
3358         break;
3359 
3360       default: // ignore
3361         inc_metric("traversed_total","type","other");
3362         break;
3363       }
3364   }
3365   clock_gettime (CLOCK_MONOTONIC, &ts_end);
3366   double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
3367 
3368   obatched(clog) << "fts traversed source paths in " << deltas << "s, scanned=" << fts_scanned
3369                  << ", regex-skipped=" << fts_regex << endl;
3370 }
3371 
3372 
3373 static void*
thread_main_fts_source_paths(void * arg)3374 thread_main_fts_source_paths (void* arg)
3375 {
3376   (void) arg; // ignore; we operate on global data
3377 
3378   set_metric("thread_tid", "role","traverse", tid());
3379   add_metric("thread_count", "role", "traverse", 1);
3380 
3381   time_t last_rescan = 0;
3382 
3383   while (! interrupted)
3384     {
3385       sleep (1);
3386       scanq.wait_idle(); // don't start a new traversal while scanners haven't finished the job
3387       scanq.done_idle(); // release the hounds
3388       if (interrupted) break;
3389 
3390       time_t now = time(NULL);
3391       bool rescan_now = false;
3392       if (last_rescan == 0) // at least one initial rescan is documented even for -t0
3393         rescan_now = true;
3394       if (rescan_s > 0 && (long)now > (long)(last_rescan + rescan_s))
3395         rescan_now = true;
3396       if (sigusr1 != forced_rescan_count)
3397         {
3398           forced_rescan_count = sigusr1;
3399           rescan_now = true;
3400         }
3401       if (rescan_now)
3402         {
3403           set_metric("thread_busy", "role","traverse", 1);
3404           try
3405             {
3406               scan_source_paths();
3407             }
3408           catch (const reportable_exception& e)
3409             {
3410               e.report(cerr);
3411             }
3412           last_rescan = time(NULL); // NB: now was before scanning
3413           // finished a traversal loop
3414           inc_metric("thread_work_total", "role","traverse");
3415           set_metric("thread_busy", "role","traverse", 0);
3416         }
3417     }
3418 
3419   return 0;
3420 }
3421 
3422 
3423 
3424 ////////////////////////////////////////////////////////////////////////
3425 
3426 static void
database_stats_report()3427 database_stats_report()
3428 {
3429   sqlite_ps ps_query (db, "database-overview",
3430                       "select label,quantity from " BUILDIDS "_stats");
3431 
3432   obatched(clog) << "database record counts:" << endl;
3433   while (1)
3434     {
3435       if (interrupted) break;
3436       if (sigusr1 != forced_rescan_count) // stop early if scan triggered
3437         break;
3438 
3439       int rc = ps_query.step();
3440       if (rc == SQLITE_DONE) break;
3441       if (rc != SQLITE_ROW)
3442         throw sqlite_exception(rc, "step");
3443 
3444       obatched(clog)
3445         << right << setw(20) << ((const char*) sqlite3_column_text(ps_query, 0) ?: (const char*) "NULL")
3446         << " "
3447         << (sqlite3_column_text(ps_query, 1) ?: (const unsigned char*) "NULL")
3448         << endl;
3449 
3450       set_metric("groom", "statistic",
3451                  ((const char*) sqlite3_column_text(ps_query, 0) ?: (const char*) "NULL"),
3452                  (sqlite3_column_double(ps_query, 1)));
3453     }
3454 }
3455 
3456 
3457 // Do a round of database grooming that might take many minutes to run.
groom()3458 void groom()
3459 {
3460   obatched(clog) << "grooming database" << endl;
3461 
3462   struct timespec ts_start, ts_end;
3463   clock_gettime (CLOCK_MONOTONIC, &ts_start);
3464 
3465   // scan for files that have disappeared
3466   sqlite_ps files (db, "check old files",
3467                    "select distinct s.mtime, s.file, f.name from "
3468                    BUILDIDS "_file_mtime_scanned s, " BUILDIDS "_files f "
3469                    "where f.id = s.file");
3470   // NB: Because _ftime_mtime_scanned can contain both F and
3471   // R records for the same file, this query would return duplicates if the
3472   // DISTINCT qualifier were not there.
3473   files.reset();
3474 
3475   // DECISION TIME - we enumerate stale fileids/mtimes
3476   deque<pair<int64_t,int64_t> > stale_fileid_mtime;
3477 
3478   time_t time_start = time(NULL);
3479   while(1)
3480     {
3481       // PR28514: limit grooming iteration to O(rescan time), to avoid
3482       // slow filesystem tests over many files locking out rescans for
3483       // too long.
3484       if (rescan_s > 0 && (long)time(NULL) > (long)(time_start + rescan_s))
3485         {
3486           inc_metric("groomed_total", "decision", "aborted");
3487           break;
3488         }
3489 
3490       if (interrupted) break;
3491 
3492       int rc = files.step();
3493       if (rc != SQLITE_ROW)
3494         break;
3495 
3496       int64_t mtime = sqlite3_column_int64 (files, 0);
3497       int64_t fileid = sqlite3_column_int64 (files, 1);
3498       const char* filename = ((const char*) sqlite3_column_text (files, 2) ?: "");
3499       struct stat s;
3500       bool reg_include = !regexec (&file_include_regex, filename, 0, 0, 0);
3501       bool reg_exclude = !regexec (&file_exclude_regex, filename, 0, 0, 0);
3502 
3503       rc = stat(filename, &s);
3504       if ( (regex_groom && reg_exclude && !reg_include) ||  rc < 0 || (mtime != (int64_t) s.st_mtime) )
3505         {
3506           if (verbose > 2)
3507             obatched(clog) << "groom: stale file=" << filename << " mtime=" << mtime << endl;
3508           stale_fileid_mtime.push_back(make_pair(fileid,mtime));
3509           inc_metric("groomed_total", "decision", "stale");
3510           set_metric("thread_work_pending","role","groom", stale_fileid_mtime.size());
3511         }
3512       else
3513         inc_metric("groomed_total", "decision", "fresh");
3514 
3515       if (sigusr1 != forced_rescan_count) // stop early if scan triggered
3516         break;
3517     }
3518   files.reset();
3519 
3520   // ACTION TIME
3521 
3522   // Now that we know which file/mtime tuples are stale, actually do
3523   // the deletion from the database.  Doing this during the SELECT
3524   // iteration above results in undefined behaviour in sqlite, as per
3525   // https://www.sqlite.org/isolation.html
3526 
3527   // We could shuffle stale_fileid_mtime[] here.  It'd let aborted
3528   // sequences of nuke operations resume at random locations, instead
3529   // of just starting over.  But it doesn't matter much either way,
3530   // as long as we make progress.
3531 
3532   sqlite_ps files_del_f_de (db, "nuke f_de", "delete from " BUILDIDS "_f_de where file = ? and mtime = ?");
3533   sqlite_ps files_del_r_de (db, "nuke r_de", "delete from " BUILDIDS "_r_de where file = ? and mtime = ?");
3534   sqlite_ps files_del_scan (db, "nuke f_m_s", "delete from " BUILDIDS "_file_mtime_scanned "
3535                             "where file = ? and mtime = ?");
3536 
3537   while (! stale_fileid_mtime.empty())
3538     {
3539       auto stale = stale_fileid_mtime.front();
3540       stale_fileid_mtime.pop_front();
3541       set_metric("thread_work_pending","role","groom", stale_fileid_mtime.size());
3542 
3543       // PR28514: limit grooming iteration to O(rescan time), to avoid
3544       // slow nuke_* queries over many files locking out rescans for too
3545       // long.  We iterate over the files in random() sequence to avoid
3546       // partial checks going over the same set.
3547       if (rescan_s > 0 && (long)time(NULL) > (long)(time_start + rescan_s))
3548         {
3549           inc_metric("groomed_total", "action", "aborted");
3550           break;
3551         }
3552 
3553       if (interrupted) break;
3554 
3555       int64_t fileid = stale.first;
3556       int64_t mtime = stale.second;
3557       files_del_f_de.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
3558       files_del_r_de.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
3559       files_del_scan.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
3560       inc_metric("groomed_total", "action", "cleaned");
3561 
3562        if (sigusr1 != forced_rescan_count) // stop early if scan triggered
3563         break;
3564     }
3565   stale_fileid_mtime.clear(); // no need for this any longer
3566   set_metric("thread_work_pending","role","groom", stale_fileid_mtime.size());
3567 
3568   // delete buildids with no references in _r_de or _f_de tables;
3569   // cascades to _r_sref & _f_s records
3570   sqlite_ps buildids_del (db, "nuke orphan buildids",
3571                           "delete from " BUILDIDS "_buildids "
3572                           "where not exists (select 1 from " BUILDIDS "_f_de d where " BUILDIDS "_buildids.id = d.buildid) "
3573                           "and not exists (select 1 from " BUILDIDS "_r_de d where " BUILDIDS "_buildids.id = d.buildid)");
3574   buildids_del.reset().step_ok_done();
3575 
3576   if (interrupted) return;
3577 
3578   // NB: "vacuum" is too heavy for even daily runs: it rewrites the entire db, so is done as maxigroom -G
3579   sqlite_ps g1 (db, "incremental vacuum", "pragma incremental_vacuum");
3580   g1.reset().step_ok_done();
3581   sqlite_ps g2 (db, "optimize", "pragma optimize");
3582   g2.reset().step_ok_done();
3583   sqlite_ps g3 (db, "wal checkpoint", "pragma wal_checkpoint=truncate");
3584   g3.reset().step_ok_done();
3585 
3586   database_stats_report();
3587 
3588   (void) statfs_free_enough_p(db_path, "database"); // report sqlite filesystem size
3589 
3590   sqlite3_db_release_memory(db); // shrink the process if possible
3591   sqlite3_db_release_memory(dbq); // ... for both connections
3592   debuginfod_pool_groom(); // and release any debuginfod_client objects we've been holding onto
3593 
3594   fdcache.limit(0,0,0,0); // release the fdcache contents
3595   fdcache.limit(fdcache_fds, fdcache_mbs, fdcache_prefetch_fds, fdcache_prefetch_mbs); // restore status quo parameters
3596 
3597   clock_gettime (CLOCK_MONOTONIC, &ts_end);
3598   double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
3599 
3600   obatched(clog) << "groomed database in " << deltas << "s" << endl;
3601 }
3602 
3603 
3604 static void*
thread_main_groom(void *)3605 thread_main_groom (void* /*arg*/)
3606 {
3607   set_metric("thread_tid", "role", "groom", tid());
3608   add_metric("thread_count", "role", "groom", 1);
3609 
3610   time_t last_groom = 0;
3611 
3612   while (1)
3613     {
3614       sleep (1);
3615       scanq.wait_idle(); // PR25394: block scanners during grooming!
3616       if (interrupted) break;
3617 
3618       time_t now = time(NULL);
3619       bool groom_now = false;
3620       if (last_groom == 0) // at least one initial groom is documented even for -g0
3621         groom_now = true;
3622       if (groom_s > 0 && (long)now > (long)(last_groom + groom_s))
3623         groom_now = true;
3624       if (sigusr2 != forced_groom_count)
3625         {
3626           forced_groom_count = sigusr2;
3627           groom_now = true;
3628         }
3629       if (groom_now)
3630         {
3631           set_metric("thread_busy", "role", "groom", 1);
3632           try
3633             {
3634               groom ();
3635             }
3636           catch (const sqlite_exception& e)
3637             {
3638               obatched(cerr) << e.message << endl;
3639             }
3640           last_groom = time(NULL); // NB: now was before grooming
3641           // finished a grooming loop
3642           inc_metric("thread_work_total", "role", "groom");
3643           set_metric("thread_busy", "role", "groom", 0);
3644         }
3645 
3646       scanq.done_idle();
3647     }
3648 
3649   return 0;
3650 }
3651 
3652 
3653 ////////////////////////////////////////////////////////////////////////
3654 
3655 
3656 static void
signal_handler(int)3657 signal_handler (int /* sig */)
3658 {
3659   interrupted ++;
3660 
3661   if (db)
3662     sqlite3_interrupt (db);
3663   if (dbq)
3664     sqlite3_interrupt (dbq);
3665 
3666   // NB: don't do anything else in here
3667 }
3668 
3669 static void
sigusr1_handler(int)3670 sigusr1_handler (int /* sig */)
3671 {
3672    sigusr1 ++;
3673   // NB: don't do anything else in here
3674 }
3675 
3676 static void
sigusr2_handler(int)3677 sigusr2_handler (int /* sig */)
3678 {
3679    sigusr2 ++;
3680   // NB: don't do anything else in here
3681 }
3682 
3683 
3684 
3685 
3686 
3687 // A user-defined sqlite function, to score the sharedness of the
3688 // prefix of two strings.  This is used to compare candidate debuginfo
3689 // / source-rpm names, so that the closest match
3690 // (directory-topology-wise closest) is found.  This is important in
3691 // case the same sref (source file name) is in many -debuginfo or
3692 // -debugsource RPMs, such as when multiple versions/releases of the
3693 // same package are in the database.
3694 
sqlite3_sharedprefix_fn(sqlite3_context * c,int argc,sqlite3_value ** argv)3695 static void sqlite3_sharedprefix_fn (sqlite3_context* c, int argc, sqlite3_value** argv)
3696 {
3697   if (argc != 2)
3698     sqlite3_result_error(c, "expect 2 string arguments", -1);
3699   else if ((sqlite3_value_type(argv[0]) != SQLITE_TEXT) ||
3700            (sqlite3_value_type(argv[1]) != SQLITE_TEXT))
3701     sqlite3_result_null(c);
3702   else
3703     {
3704       const unsigned char* a = sqlite3_value_text (argv[0]);
3705       const unsigned char* b = sqlite3_value_text (argv[1]);
3706       int i = 0;
3707       while (*a++ == *b++)
3708         i++;
3709       sqlite3_result_int (c, i);
3710     }
3711 }
3712 
3713 
3714 int
main(int argc,char * argv[])3715 main (int argc, char *argv[])
3716 {
3717   (void) setlocale (LC_ALL, "");
3718   (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
3719   (void) textdomain (PACKAGE_TARNAME);
3720 
3721   /* Tell the library which version we are expecting.  */
3722   elf_version (EV_CURRENT);
3723 
3724   tmpdir = string(getenv("TMPDIR") ?: "/tmp");
3725 
3726   /* Set computed default values. */
3727   db_path = string(getenv("HOME") ?: "/") + string("/.debuginfod.sqlite"); /* XDG? */
3728   int rc = regcomp (& file_include_regex, ".*", REG_EXTENDED|REG_NOSUB); // match everything
3729   if (rc != 0)
3730     error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
3731   rc = regcomp (& file_exclude_regex, "^$", REG_EXTENDED|REG_NOSUB); // match nothing
3732   if (rc != 0)
3733     error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
3734 
3735   // default parameters for fdcache are computed from system stats
3736   struct statfs sfs;
3737   rc = statfs(tmpdir.c_str(), &sfs);
3738   if (rc < 0)
3739     fdcache_mbs = 1024; // 1 gigabyte
3740   else
3741     fdcache_mbs = sfs.f_bavail * sfs.f_bsize / 1024 / 1024 / 4; // 25% of free space
3742   fdcache_mintmp = 25; // emergency flush at 25% remaining (75% full)
3743   fdcache_prefetch = 64; // guesstimate storage is this much less costly than re-decompression
3744   fdcache_fds = (concurrency + fdcache_prefetch) * 2;
3745 
3746   /* Parse and process arguments.  */
3747   int remaining;
3748   argp_program_version_hook = print_version; // this works
3749   (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
3750   if (remaining != argc)
3751       error (EXIT_FAILURE, 0,
3752              "unexpected argument: %s", argv[remaining]);
3753 
3754   if (scan_archives.size()==0 && !scan_files && source_paths.size()>0)
3755     obatched(clog) << "warning: without -F -R -U -Z, ignoring PATHs" << endl;
3756 
3757   fdcache.limit(fdcache_fds, fdcache_mbs, fdcache_prefetch_fds, fdcache_prefetch_mbs);
3758 
3759   (void) signal (SIGPIPE, SIG_IGN); // microhttpd can generate it incidentally, ignore
3760   (void) signal (SIGINT, signal_handler); // ^C
3761   (void) signal (SIGHUP, signal_handler); // EOF
3762   (void) signal (SIGTERM, signal_handler); // systemd
3763   (void) signal (SIGUSR1, sigusr1_handler); // end-user
3764   (void) signal (SIGUSR2, sigusr2_handler); // end-user
3765 
3766   /* Get database ready. */
3767   if (! passive_p)
3768     {
3769       rc = sqlite3_open_v2 (db_path.c_str(), &db, (SQLITE_OPEN_READWRITE
3770                                                    |SQLITE_OPEN_URI
3771                                                    |SQLITE_OPEN_PRIVATECACHE
3772                                                    |SQLITE_OPEN_CREATE
3773                                                    |SQLITE_OPEN_FULLMUTEX), /* thread-safe */
3774                             NULL);
3775       if (rc == SQLITE_CORRUPT)
3776         {
3777           (void) unlink (db_path.c_str());
3778           error (EXIT_FAILURE, 0,
3779                  "cannot open %s, deleted database: %s", db_path.c_str(), sqlite3_errmsg(db));
3780         }
3781       else if (rc)
3782         {
3783           error (EXIT_FAILURE, 0,
3784                  "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(db));
3785         }
3786     }
3787 
3788   // open the readonly query variant
3789   // NB: PRIVATECACHE allows web queries to operate in parallel with
3790   // much other grooming/scanning operation.
3791   rc = sqlite3_open_v2 (db_path.c_str(), &dbq, (SQLITE_OPEN_READONLY
3792                                                 |SQLITE_OPEN_URI
3793                                                 |SQLITE_OPEN_PRIVATECACHE
3794                                                 |SQLITE_OPEN_FULLMUTEX), /* thread-safe */
3795                         NULL);
3796   if (rc)
3797     {
3798       error (EXIT_FAILURE, 0,
3799              "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(dbq));
3800     }
3801 
3802 
3803   obatched(clog) << "opened database " << db_path
3804                  << (db?" rw":"") << (dbq?" ro":"") << endl;
3805   obatched(clog) << "sqlite version " << sqlite3_version << endl;
3806   obatched(clog) << "service mode " << (passive_p ? "passive":"active") << endl;
3807 
3808   // add special string-prefix-similarity function used in rpm sref/sdef resolution
3809   rc = sqlite3_create_function(dbq, "sharedprefix", 2, SQLITE_UTF8, NULL,
3810                                & sqlite3_sharedprefix_fn, NULL, NULL);
3811   if (rc != SQLITE_OK)
3812     error (EXIT_FAILURE, 0,
3813            "cannot create sharedprefix function: %s", sqlite3_errmsg(dbq));
3814 
3815   if (! passive_p)
3816     {
3817       if (verbose > 3)
3818         obatched(clog) << "ddl: " << DEBUGINFOD_SQLITE_DDL << endl;
3819       rc = sqlite3_exec (db, DEBUGINFOD_SQLITE_DDL, NULL, NULL, NULL);
3820       if (rc != SQLITE_OK)
3821         {
3822           error (EXIT_FAILURE, 0,
3823                  "cannot run database schema ddl: %s", sqlite3_errmsg(db));
3824         }
3825     }
3826 
3827   // Start httpd server threads.  Separate pool for IPv4 and IPv6, in
3828   // case the host only has one protocol stack.
3829   MHD_Daemon *d4 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
3830 #if MHD_VERSION >= 0x00095300
3831                                      | MHD_USE_INTERNAL_POLLING_THREAD
3832 #else
3833                                      | MHD_USE_SELECT_INTERNALLY
3834 #endif
3835                                      | MHD_USE_DEBUG, /* report errors to stderr */
3836                                      http_port,
3837                                      NULL, NULL, /* default accept policy */
3838                                      handler_cb, NULL, /* handler callback */
3839                                      MHD_OPTION_END);
3840   MHD_Daemon *d6 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
3841 #if MHD_VERSION >= 0x00095300
3842                                      | MHD_USE_INTERNAL_POLLING_THREAD
3843 #else
3844                                      | MHD_USE_SELECT_INTERNALLY
3845 #endif
3846                                      | MHD_USE_IPv6
3847                                      | MHD_USE_DEBUG, /* report errors to stderr */
3848                                      http_port,
3849                                      NULL, NULL, /* default accept policy */
3850                                      handler_cb, NULL, /* handler callback */
3851                                      MHD_OPTION_END);
3852 
3853   if (d4 == NULL && d6 == NULL) // neither ipv4 nor ipv6? boo
3854     {
3855       sqlite3 *database = db;
3856       sqlite3 *databaseq = dbq;
3857       db = dbq = 0; // for signal_handler not to freak
3858       sqlite3_close (databaseq);
3859       sqlite3_close (database);
3860       error (EXIT_FAILURE, 0, "cannot start http server at port %d", http_port);
3861     }
3862 
3863   obatched(clog) << "started http server on "
3864                  << (d4 != NULL ? "IPv4 " : "")
3865                  << (d6 != NULL ? "IPv6 " : "")
3866                  << "port=" << http_port << endl;
3867 
3868   // add maxigroom sql if -G given
3869   if (maxigroom)
3870     {
3871       obatched(clog) << "maxigrooming database, please wait." << endl;
3872       extra_ddl.push_back("create index if not exists " BUILDIDS "_r_sref_arc on " BUILDIDS "_r_sref(artifactsrc);");
3873       extra_ddl.push_back("delete from " BUILDIDS "_r_sdef where not exists (select 1 from " BUILDIDS "_r_sref b where " BUILDIDS "_r_sdef.content = b.artifactsrc);");
3874       extra_ddl.push_back("drop index if exists " BUILDIDS "_r_sref_arc;");
3875 
3876       // NB: we don't maxigroom the _files interning table.  It'd require a temp index on all the
3877       // tables that have file foreign-keys, which is a lot.
3878 
3879       // NB: with =delete, may take up 3x disk space total during vacuum process
3880       //     vs.  =off (only 2x but may corrupt database if program dies mid-vacuum)
3881       //     vs.  =wal (>3x observed, but safe)
3882       extra_ddl.push_back("pragma journal_mode=delete;");
3883       extra_ddl.push_back("vacuum;");
3884       extra_ddl.push_back("pragma journal_mode=wal;");
3885     }
3886 
3887   // run extra -D sql if given
3888   if (! passive_p)
3889     for (auto&& i: extra_ddl)
3890       {
3891         if (verbose > 1)
3892           obatched(clog) << "extra ddl:\n" << i << endl;
3893         rc = sqlite3_exec (db, i.c_str(), NULL, NULL, NULL);
3894         if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW)
3895           error (0, 0,
3896                  "warning: cannot run database extra ddl %s: %s", i.c_str(), sqlite3_errmsg(db));
3897 
3898         if (maxigroom)
3899           obatched(clog) << "maxigroomed database" << endl;
3900       }
3901 
3902   if (! passive_p)
3903     obatched(clog) << "search concurrency " << concurrency << endl;
3904   if (! passive_p)
3905     obatched(clog) << "rescan time " << rescan_s << endl;
3906   obatched(clog) << "fdcache fds " << fdcache_fds << endl;
3907   obatched(clog) << "fdcache mbs " << fdcache_mbs << endl;
3908   obatched(clog) << "fdcache prefetch " << fdcache_prefetch << endl;
3909   obatched(clog) << "fdcache tmpdir " << tmpdir << endl;
3910   obatched(clog) << "fdcache tmpdir min% " << fdcache_mintmp << endl;
3911   if (! passive_p)
3912     obatched(clog) << "groom time " << groom_s << endl;
3913   obatched(clog) << "prefetch fds " << fdcache_prefetch_fds << endl;
3914   obatched(clog) << "prefetch mbs " << fdcache_prefetch_mbs << endl;
3915   obatched(clog) << "forwarded ttl limit " << forwarded_ttl_limit << endl;
3916 
3917   if (scan_archives.size()>0)
3918     {
3919       obatched ob(clog);
3920       auto& o = ob << "accepting archive types ";
3921       for (auto&& arch : scan_archives)
3922 	o << arch.first << "(" << arch.second << ") ";
3923       o << endl;
3924     }
3925   const char* du = getenv(DEBUGINFOD_URLS_ENV_VAR);
3926   if (du && du[0] != '\0') // set to non-empty string?
3927     obatched(clog) << "upstream debuginfod servers: " << du << endl;
3928 
3929   vector<pthread_t> all_threads;
3930 
3931   if (! passive_p)
3932     {
3933       pthread_t pt;
3934       rc = pthread_create (& pt, NULL, thread_main_groom, NULL);
3935       if (rc)
3936         error (EXIT_FAILURE, rc, "cannot spawn thread to groom database\n");
3937       else
3938         {
3939 #ifdef HAVE_PTHREAD_SETNAME_NP
3940           (void) pthread_setname_np (pt, "groom");
3941 #endif
3942           all_threads.push_back(pt);
3943         }
3944 
3945       if (scan_files || scan_archives.size() > 0)
3946         {
3947           rc = pthread_create (& pt, NULL, thread_main_fts_source_paths, NULL);
3948           if (rc)
3949             error (EXIT_FAILURE, rc, "cannot spawn thread to traverse source paths\n");
3950 #ifdef HAVE_PTHREAD_SETNAME_NP
3951           (void) pthread_setname_np (pt, "traverse");
3952 #endif
3953           all_threads.push_back(pt);
3954 
3955           for (unsigned i=0; i<concurrency; i++)
3956             {
3957               rc = pthread_create (& pt, NULL, thread_main_scanner, NULL);
3958               if (rc)
3959                 error (EXIT_FAILURE, rc, "cannot spawn thread to scan source files / archives\n");
3960 #ifdef HAVE_PTHREAD_SETNAME_NP
3961               (void) pthread_setname_np (pt, "scan");
3962 #endif
3963               all_threads.push_back(pt);
3964             }
3965         }
3966     }
3967 
3968   /* Trivial main loop! */
3969   set_metric("ready", 1);
3970   while (! interrupted)
3971     pause ();
3972   scanq.nuke(); // wake up any remaining scanq-related threads, let them die
3973   set_metric("ready", 0);
3974 
3975   if (verbose)
3976     obatched(clog) << "stopping" << endl;
3977 
3978   /* Join all our threads. */
3979   for (auto&& it : all_threads)
3980     pthread_join (it, NULL);
3981 
3982   /* Stop all the web service threads. */
3983   if (d4) MHD_stop_daemon (d4);
3984   if (d6) MHD_stop_daemon (d6);
3985 
3986   if (! passive_p)
3987     {
3988       /* With all threads known dead, we can clean up the global resources. */
3989       rc = sqlite3_exec (db, DEBUGINFOD_SQLITE_CLEANUP_DDL, NULL, NULL, NULL);
3990       if (rc != SQLITE_OK)
3991         {
3992           error (0, 0,
3993                  "warning: cannot run database cleanup ddl: %s", sqlite3_errmsg(db));
3994         }
3995     }
3996 
3997   // NB: no problem with unconditional free here - an earlier failed regcomp would exit program
3998   (void) regfree (& file_include_regex);
3999   (void) regfree (& file_exclude_regex);
4000 
4001   sqlite3 *database = db;
4002   sqlite3 *databaseq = dbq;
4003   db = dbq = 0; // for signal_handler not to freak
4004   (void) sqlite3_close (databaseq);
4005   if (! passive_p)
4006     (void) sqlite3_close (database);
4007 
4008   return 0;
4009 }
4010